1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
10 #include <net/if_arp.h>
15 #include <rte_ether.h>
17 #include "vhost_kernel_tap.h"
18 #include "../virtio_logs.h"
19 #include "../virtio_pci.h"
22 vhost_kernel_tap_set_offload(int fd, uint64_t features)
24 unsigned int offload = 0;
26 if (features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
27 offload |= TUN_F_CSUM;
28 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO4))
29 offload |= TUN_F_TSO4;
30 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO6))
31 offload |= TUN_F_TSO6;
32 if (features & ((1ULL << VIRTIO_NET_F_GUEST_TSO4) |
33 (1ULL << VIRTIO_NET_F_GUEST_TSO6)) &&
34 (features & (1ULL << VIRTIO_NET_F_GUEST_ECN)))
35 offload |= TUN_F_TSO_ECN;
36 if (features & (1ULL << VIRTIO_NET_F_GUEST_UFO))
41 /* Check if our kernel supports TUNSETOFFLOAD */
42 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
43 PMD_DRV_LOG(ERR, "Kernel does't support TUNSETOFFLOAD\n");
47 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
48 offload &= ~TUN_F_UFO;
49 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
50 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s\n",
61 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
62 const char *mac, uint64_t features)
64 unsigned int tap_features;
65 char *tap_name = NULL;
71 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
72 * 2. get number of memory regions from vhost module parameter
73 * max_mem_regions, supported in newer version linux kernel
75 tapfd = open(PATH_NET_TUN, O_RDWR);
77 PMD_DRV_LOG(ERR, "fail to open %s: %s",
78 PATH_NET_TUN, strerror(errno));
83 memset(&ifr, 0, sizeof(ifr));
84 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
86 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
87 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
90 if (tap_features & IFF_ONE_QUEUE)
91 ifr.ifr_flags |= IFF_ONE_QUEUE;
93 /* Let tap instead of vhost-net handle vnet header, as the latter does
94 * not support offloading. And in this case, we should not set feature
95 * bit VHOST_NET_F_VIRTIO_NET_HDR.
97 if (tap_features & IFF_VNET_HDR) {
98 ifr.ifr_flags |= IFF_VNET_HDR;
100 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
105 ifr.ifr_flags |= IFF_MULTI_QUEUE;
108 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
110 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
111 if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
112 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
116 tap_name = strdup(ifr.ifr_name);
118 PMD_DRV_LOG(ERR, "strdup ifname failed: %s", strerror(errno));
122 fcntl(tapfd, F_SETFL, O_NONBLOCK);
124 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
125 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
129 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
130 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
134 vhost_kernel_tap_set_offload(tapfd, features);
136 memset(&ifr, 0, sizeof(ifr));
137 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
138 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
139 if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
140 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
145 *p_ifname = tap_name;