1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 Intel Corporation 3 */ 4 5 #include <unistd.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <fcntl.h> 9 #include <net/if.h> 10 #include <net/if_arp.h> 11 #include <errno.h> 12 #include <string.h> 13 #include <limits.h> 14 15 #include <rte_ether.h> 16 17 #include "vhost_kernel_tap.h" 18 #include "../virtio_logs.h" 19 #include "../virtio_pci.h" 20 21 static int 22 vhost_kernel_tap_set_offload(int fd, uint64_t features) 23 { 24 unsigned int offload = 0; 25 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)) 37 offload |= TUN_F_UFO; 38 } 39 40 if (offload != 0) { 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"); 44 return -ENOTSUP; 45 } 46 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", 51 strerror(errno)); 52 return -1; 53 } 54 } 55 } 56 57 return 0; 58 } 59 60 int 61 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq, 62 const char *mac, uint64_t features) 63 { 64 unsigned int tap_features; 65 char *tap_name = NULL; 66 int sndbuf = INT_MAX; 67 struct ifreq ifr; 68 int tapfd; 69 70 /* TODO: 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 74 */ 75 tapfd = open(PATH_NET_TUN, O_RDWR); 76 if (tapfd < 0) { 77 PMD_DRV_LOG(ERR, "fail to open %s: %s", 78 PATH_NET_TUN, strerror(errno)); 79 return -1; 80 } 81 82 /* Construct ifr */ 83 memset(&ifr, 0, sizeof(ifr)); 84 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 85 86 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) { 87 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno)); 88 goto error; 89 } 90 if (tap_features & IFF_ONE_QUEUE) 91 ifr.ifr_flags |= IFF_ONE_QUEUE; 92 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. 96 */ 97 if (tap_features & IFF_VNET_HDR) { 98 ifr.ifr_flags |= IFF_VNET_HDR; 99 } else { 100 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR"); 101 goto error; 102 } 103 104 if (req_mq) 105 ifr.ifr_flags |= IFF_MULTI_QUEUE; 106 107 if (*p_ifname) 108 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1); 109 else 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)); 113 goto error; 114 } 115 116 tap_name = strdup(ifr.ifr_name); 117 if (!tap_name) { 118 PMD_DRV_LOG(ERR, "strdup ifname failed: %s", strerror(errno)); 119 goto error; 120 } 121 122 fcntl(tapfd, F_SETFL, O_NONBLOCK); 123 124 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) { 125 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno)); 126 goto error; 127 } 128 129 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) { 130 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno)); 131 goto error; 132 } 133 134 vhost_kernel_tap_set_offload(tapfd, features); 135 136 memset(&ifr, 0, sizeof(ifr)); 137 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; 138 memcpy(ifr.ifr_hwaddr.sa_data, mac, RTE_ETHER_ADDR_LEN); 139 if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) { 140 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno)); 141 goto error; 142 } 143 144 free(*p_ifname); 145 *p_ifname = tap_name; 146 147 return tapfd; 148 error: 149 free(tap_name); 150 close(tapfd); 151 return -1; 152 } 153