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;
70 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
71 * 2. get number of memory regions from vhost module parameter
72 * max_mem_regions, supported in newer version linux kernel
74 tapfd = open(PATH_NET_TUN, O_RDWR);
76 PMD_DRV_LOG(ERR, "fail to open %s: %s",
77 PATH_NET_TUN, strerror(errno));
82 memset(&ifr, 0, sizeof(ifr));
83 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
85 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
86 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
89 if (tap_features & IFF_ONE_QUEUE)
90 ifr.ifr_flags |= IFF_ONE_QUEUE;
92 /* Let tap instead of vhost-net handle vnet header, as the latter does
93 * not support offloading. And in this case, we should not set feature
94 * bit VHOST_NET_F_VIRTIO_NET_HDR.
96 if (tap_features & IFF_VNET_HDR) {
97 ifr.ifr_flags |= IFF_VNET_HDR;
99 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
104 ifr.ifr_flags |= IFF_MULTI_QUEUE;
107 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
109 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
110 if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
111 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
115 fcntl(tapfd, F_SETFL, O_NONBLOCK);
117 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
118 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
122 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
123 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
127 vhost_kernel_tap_set_offload(tapfd, features);
129 memset(&ifr, 0, sizeof(ifr));
130 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
131 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
132 if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
133 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
138 *p_ifname = strdup(ifr.ifr_name);