| 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * Copyright (c) 2009 Red Hat, Inc. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "qemu/osdep.h" |
| 27 | #include "tap_int.h" |
| 28 | #include "tap-linux.h" |
| 29 | #include "net/tap.h" |
| 30 | |
| 31 | #include <net/if.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | |
| 34 | #include "qapi/error.h" |
| 35 | #include "qemu/error-report.h" |
| 36 | #include "qemu/cutils.h" |
| 37 | |
| 38 | #define PATH_NET_TUN "/dev/net/tun" |
| 39 | |
| 40 | int tap_open(char *ifname, int ifname_size, int *vnet_hdr, |
| 41 | int vnet_hdr_required, int mq_required, Error **errp) |
| 42 | { |
| 43 | struct ifreq ifr; |
| 44 | int fd, ret; |
| 45 | int len = sizeof(struct virtio_net_hdr); |
| 46 | unsigned int features; |
| 47 | |
| 48 | |
| 49 | ret = if_nametoindex(ifname); |
| 50 | if (ret) { |
| 51 | g_autofree char *file = g_strdup_printf("/dev/tap%d", ret); |
| 52 | fd = open(file, O_RDWR); |
| 53 | } else { |
| 54 | fd = -1; |
| 55 | } |
| 56 | |
| 57 | if (fd < 0) { |
| 58 | fd = RETRY_ON_EINTR(open(PATH_NET_TUN, O_RDWR)); |
| 59 | if (fd < 0) { |
| 60 | error_setg_file_open(errp, errno, PATH_NET_TUN); |
| 61 | return -1; |
| 62 | } |
| 63 | } |
| 64 | memset(&ifr, 0, sizeof(ifr)); |
| 65 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
| 66 | |
| 67 | if (ioctl(fd, TUNGETFEATURES, &features) == -1) { |
| 68 | warn_report("TUNGETFEATURES failed: %s", strerror(errno)); |
| 69 | features = 0; |
| 70 | } |
| 71 | |
| 72 | if (features & IFF_ONE_QUEUE) { |
| 73 | ifr.ifr_flags |= IFF_ONE_QUEUE; |
| 74 | } |
| 75 | |
| 76 | if (*vnet_hdr) { |
| 77 | if (features & IFF_VNET_HDR) { |
| 78 | *vnet_hdr = 1; |
| 79 | ifr.ifr_flags |= IFF_VNET_HDR; |
| 80 | } else { |
| 81 | *vnet_hdr = 0; |
| 82 | } |
| 83 | |
| 84 | if (vnet_hdr_required && !*vnet_hdr) { |
| 85 | error_setg(errp, "vnet_hdr=1 requested, but no kernel " |
| 86 | "support for IFF_VNET_HDR available"); |
| 87 | close(fd); |
| 88 | return -1; |
| 89 | } |
| 90 | /* |
| 91 | * Make sure vnet header size has the default value: for a persistent |
| 92 | * tap it might have been modified e.g. by another instance of qemu. |
| 93 | * Ignore errors since old kernels do not support this ioctl: in this |
| 94 | * case the header size implicitly has the correct value. |
| 95 | */ |
| 96 | ioctl(fd, TUNSETVNETHDRSZ, &len); |
| 97 | } |
| 98 | |
| 99 | if (mq_required) { |
| 100 | if (!(features & IFF_MULTI_QUEUE)) { |
| 101 | error_setg(errp, "multiqueue required, but no kernel " |
| 102 | "support for IFF_MULTI_QUEUE available"); |
| 103 | close(fd); |
| 104 | return -1; |
| 105 | } else { |
| 106 | ifr.ifr_flags |= IFF_MULTI_QUEUE; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (ifname[0] != '\0') |
| 111 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 112 | else |
| 113 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
| 114 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 115 | if (ret != 0) { |
| 116 | if (ifname[0] != '\0') { |
| 117 | error_setg_errno(errp, errno, "could not configure %s (%s)", |
| 118 | PATH_NET_TUN, ifr.ifr_name); |
| 119 | } else { |
| 120 | error_setg_errno(errp, errno, "could not configure %s", |
| 121 | PATH_NET_TUN); |
| 122 | } |
| 123 | close(fd); |
| 124 | return -1; |
| 125 | } |
| 126 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
| 127 | |
| 128 | if (!qemu_set_blocking(fd, false, errp)) { |
| 129 | close(fd); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | return fd; |
| 134 | } |
| 135 | |
| 136 | /* sndbuf implements a kind of flow control for tap. |
| 137 | * Unfortunately when it's enabled, and packets are sent |
| 138 | * to other guests on the same host, the receiver |
| 139 | * can lock up the transmitter indefinitely. |
| 140 | * |
| 141 | * To avoid packet loss, sndbuf should be set to a value lower than the tx |
| 142 | * queue capacity of any destination network interface. |
| 143 | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is |
| 144 | * a good value, given a 1500 byte MTU. |
| 145 | */ |
| 146 | bool tap_set_sndbuf(int fd, int sndbuf, Error **errp) |
| 147 | { |
| 148 | if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1) { |
| 149 | error_setg_errno(errp, errno, "TUNSETSNDBUF ioctl failed"); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | int tap_probe_vnet_hdr(int fd, Error **errp) |
| 157 | { |
| 158 | struct ifreq ifr; |
| 159 | memset(&ifr, 0, sizeof(ifr)); |
| 160 | |
| 161 | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
| 162 | /* TUNGETIFF is available since kernel v2.6.27 */ |
| 163 | error_setg_errno(errp, errno, |
| 164 | "Unable to query TUNGETIFF on FD %d", fd); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | return ifr.ifr_flags & IFF_VNET_HDR; |
| 169 | } |
| 170 | |
| 171 | int tap_probe_has_ufo(int fd) |
| 172 | { |
| 173 | unsigned offload; |
| 174 | |
| 175 | offload = TUN_F_CSUM | TUN_F_UFO; |
| 176 | |
| 177 | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) |
| 178 | return 0; |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | int tap_probe_has_uso(int fd) |
| 184 | { |
| 185 | unsigned offload; |
| 186 | |
| 187 | offload = TUN_F_CSUM | TUN_F_USO4 | TUN_F_USO6; |
| 188 | |
| 189 | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) { |
| 190 | return 0; |
| 191 | } |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | bool tap_probe_has_tunnel(int fd) |
| 196 | { |
| 197 | unsigned offload; |
| 198 | |
| 199 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_UDP_TUNNEL_GSO; |
| 200 | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) { |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | void tap_fd_set_vnet_hdr_len(int fd, int len) |
| 207 | { |
| 208 | int ret; |
| 209 | |
| 210 | ret = ioctl(fd, TUNSETVNETHDRSZ, &len); |
| 211 | if (ret != 0) { |
| 212 | error_report("TUNSETVNETHDRSZ ioctl() failed: %s.", strerror(errno)); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int tap_fd_set_vnet_le(int fd, int is_le) |
| 217 | { |
| 218 | int arg = is_le ? 1 : 0; |
| 219 | |
| 220 | if (!ioctl(fd, TUNSETVNETLE, &arg)) { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* Check if our kernel supports TUNSETVNETLE */ |
| 225 | if (errno == EINVAL) { |
| 226 | return -errno; |
| 227 | } |
| 228 | |
| 229 | error_report("TUNSETVNETLE ioctl() failed: %s.", strerror(errno)); |
| 230 | abort(); |
| 231 | } |
| 232 | |
| 233 | int tap_fd_set_vnet_be(int fd, int is_be) |
| 234 | { |
| 235 | int arg = is_be ? 1 : 0; |
| 236 | |
| 237 | if (!ioctl(fd, TUNSETVNETBE, &arg)) { |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* Check if our kernel supports TUNSETVNETBE */ |
| 242 | if (errno == EINVAL) { |
| 243 | return -errno; |
| 244 | } |
| 245 | |
| 246 | error_report("TUNSETVNETBE ioctl() failed: %s.", strerror(errno)); |
| 247 | abort(); |
| 248 | } |
| 249 | |
| 250 | void tap_fd_set_offload(int fd, const NetOffloads *ol) |
| 251 | { |
| 252 | unsigned int offload = 0; |
| 253 | |
| 254 | /* Check if our kernel supports TUNSETOFFLOAD */ |
| 255 | if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | if (ol->csum) { |
| 260 | offload |= TUN_F_CSUM; |
| 261 | if (ol->tso4) { |
| 262 | offload |= TUN_F_TSO4; |
| 263 | } |
| 264 | if (ol->tso6) { |
| 265 | offload |= TUN_F_TSO6; |
| 266 | } |
| 267 | if ((ol->tso4 || ol->tso6) && ol->ecn) { |
| 268 | offload |= TUN_F_TSO_ECN; |
| 269 | } |
| 270 | if (ol->ufo) { |
| 271 | offload |= TUN_F_UFO; |
| 272 | } |
| 273 | if (ol->uso4) { |
| 274 | offload |= TUN_F_USO4; |
| 275 | } |
| 276 | if (ol->uso6) { |
| 277 | offload |= TUN_F_USO6; |
| 278 | } |
| 279 | if (ol->tnl) { |
| 280 | offload |= TUN_F_UDP_TUNNEL_GSO; |
| 281 | } |
| 282 | if (ol->tnl_csum) { |
| 283 | offload |= TUN_F_UDP_TUNNEL_GSO_CSUM; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
| 288 | offload &= ~(TUN_F_USO4 | TUN_F_USO6); |
| 289 | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
| 290 | offload &= ~TUN_F_UFO; |
| 291 | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { |
| 292 | fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n", |
| 293 | strerror(errno)); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* Enable a specific queue of tap. */ |
| 300 | int tap_fd_enable(int fd) |
| 301 | { |
| 302 | struct ifreq ifr; |
| 303 | int ret; |
| 304 | |
| 305 | memset(&ifr, 0, sizeof(ifr)); |
| 306 | |
| 307 | ifr.ifr_flags = IFF_ATTACH_QUEUE; |
| 308 | ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr); |
| 309 | |
| 310 | if (ret != 0) { |
| 311 | error_report("could not enable queue"); |
| 312 | } |
| 313 | |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* Disable a specific queue of tap/ */ |
| 318 | int tap_fd_disable(int fd) |
| 319 | { |
| 320 | struct ifreq ifr; |
| 321 | int ret; |
| 322 | |
| 323 | memset(&ifr, 0, sizeof(ifr)); |
| 324 | |
| 325 | ifr.ifr_flags = IFF_DETACH_QUEUE; |
| 326 | ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr); |
| 327 | |
| 328 | if (ret != 0) { |
| 329 | error_report("could not disable queue"); |
| 330 | } |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | int tap_fd_get_ifname(int fd, char *ifname) |
| 336 | { |
| 337 | struct ifreq ifr; |
| 338 | |
| 339 | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
| 340 | error_report("TUNGETIFF ioctl() failed: %s", |
| 341 | strerror(errno)); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | int tap_fd_set_steering_ebpf(int fd, int prog_fd) |
| 350 | { |
| 351 | if (ioctl(fd, TUNSETSTEERINGEBPF, (void *) &prog_fd) != 0) { |
| 352 | error_report("Issue while setting TUNSETSTEERINGEBPF:" |
| 353 | " %s with fd: %d, prog_fd: %d", |
| 354 | strerror(errno), fd, prog_fd); |
| 355 | |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | } |