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 29 30 #include <sys/ioctl.h> 31 #include <sys/wait.h> 32 #include <sys/socket.h> 33 #include <net/if.h> 34 35 #include "net/eth.h" 36 #include "net/net.h" 37 #include "clients.h" 38 #include "monitor/monitor.h" 39 #include "sysemu/sysemu.h" 40 #include "qapi/error.h" 41 #include "qemu/cutils.h" 42 #include "qemu/error-report.h" 43 #include "qemu/main-loop.h" 44 #include "qemu/sockets.h" 45 46 #include "net/tap.h" 47 48 #include "net/vhost_net.h" 49 50 typedef struct TAPState { 51 NetClientState nc; 52 int fd; 53 char down_script[1024]; 54 char down_script_arg[128]; 55 uint8_t buf[NET_BUFSIZE]; 56 bool read_poll; 57 bool write_poll; 58 bool using_vnet_hdr; 59 bool has_ufo; 60 bool has_uso; 61 bool enabled; 62 VHostNetState *vhost_net; 63 unsigned host_vnet_hdr_len; 64 Notifier exit; 65 } TAPState; 66 67 static void launch_script(const char *setup_script, const char *ifname, 68 int fd, Error **errp); 69 70 static void tap_send(void *opaque); 71 static void tap_writable(void *opaque); 72 73 static void tap_update_fd_handler(TAPState *s) 74 { 75 qemu_set_fd_handler(s->fd, 76 s->read_poll && s->enabled ? tap_send : NULL, 77 s->write_poll && s->enabled ? tap_writable : NULL, 78 s); 79 } 80 81 static void tap_read_poll(TAPState *s, bool enable) 82 { 83 s->read_poll = enable; 84 tap_update_fd_handler(s); 85 } 86 87 static void tap_write_poll(TAPState *s, bool enable) 88 { 89 s->write_poll = enable; 90 tap_update_fd_handler(s); 91 } 92 93 static void tap_writable(void *opaque) 94 { 95 TAPState *s = opaque; 96 97 tap_write_poll(s, false); 98 99 qemu_flush_queued_packets(&s->nc); 100 } 101 102 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) 103 { 104 ssize_t len; 105 106 len = RETRY_ON_EINTR(writev(s->fd, iov, iovcnt)); 107 108 if (len == -1 && errno == EAGAIN) { 109 tap_write_poll(s, true); 110 return 0; 111 } 112 113 return len; 114 } 115 116 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov, 117 int iovcnt) 118 { 119 TAPState *s = DO_UPCAST(TAPState, nc, nc); 120 const struct iovec *iovp = iov; 121 struct iovec iov_copy[iovcnt + 1]; 122 struct virtio_net_hdr_mrg_rxbuf hdr = { }; 123 124 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 125 iov_copy[0].iov_base = &hdr; 126 iov_copy[0].iov_len = s->host_vnet_hdr_len; 127 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); 128 iovp = iov_copy; 129 iovcnt++; 130 } 131 132 return tap_write_packet(s, iovp, iovcnt); 133 } 134 135 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size) 136 { 137 TAPState *s = DO_UPCAST(TAPState, nc, nc); 138 struct iovec iov[2]; 139 int iovcnt = 0; 140 struct virtio_net_hdr_mrg_rxbuf hdr = { }; 141 142 if (s->host_vnet_hdr_len) { 143 iov[iovcnt].iov_base = &hdr; 144 iov[iovcnt].iov_len = s->host_vnet_hdr_len; 145 iovcnt++; 146 } 147 148 iov[iovcnt].iov_base = (char *)buf; 149 iov[iovcnt].iov_len = size; 150 iovcnt++; 151 152 return tap_write_packet(s, iov, iovcnt); 153 } 154 155 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) 156 { 157 TAPState *s = DO_UPCAST(TAPState, nc, nc); 158 struct iovec iov[1]; 159 160 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 161 return tap_receive_raw(nc, buf, size); 162 } 163 164 iov[0].iov_base = (char *)buf; 165 iov[0].iov_len = size; 166 167 return tap_write_packet(s, iov, 1); 168 } 169 170 #ifndef __sun__ 171 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) 172 { 173 return read(tapfd, buf, maxlen); 174 } 175 #endif 176 177 static void tap_send_completed(NetClientState *nc, ssize_t len) 178 { 179 TAPState *s = DO_UPCAST(TAPState, nc, nc); 180 tap_read_poll(s, true); 181 } 182 183 static void tap_send(void *opaque) 184 { 185 TAPState *s = opaque; 186 int size; 187 int packets = 0; 188 189 while (true) { 190 uint8_t *buf = s->buf; 191 uint8_t min_pkt[ETH_ZLEN]; 192 size_t min_pktsz = sizeof(min_pkt); 193 194 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); 195 if (size <= 0) { 196 break; 197 } 198 199 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 200 buf += s->host_vnet_hdr_len; 201 size -= s->host_vnet_hdr_len; 202 } 203 204 if (net_peer_needs_padding(&s->nc)) { 205 if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { 206 buf = min_pkt; 207 size = min_pktsz; 208 } 209 } 210 211 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); 212 if (size == 0) { 213 tap_read_poll(s, false); 214 break; 215 } else if (size < 0) { 216 break; 217 } 218 219 /* 220 * When the host keeps receiving more packets while tap_send() is 221 * running we can hog the QEMU global mutex. Limit the number of 222 * packets that are processed per tap_send() callback to prevent 223 * stalling the guest. 224 */ 225 packets++; 226 if (packets >= 50) { 227 break; 228 } 229 } 230 } 231 232 static bool tap_has_ufo(NetClientState *nc) 233 { 234 TAPState *s = DO_UPCAST(TAPState, nc, nc); 235 236 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 237 238 return s->has_ufo; 239 } 240 241 static bool tap_has_uso(NetClientState *nc) 242 { 243 TAPState *s = DO_UPCAST(TAPState, nc, nc); 244 245 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 246 247 return s->has_uso; 248 } 249 250 static bool tap_has_vnet_hdr(NetClientState *nc) 251 { 252 TAPState *s = DO_UPCAST(TAPState, nc, nc); 253 254 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 255 256 return !!s->host_vnet_hdr_len; 257 } 258 259 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len) 260 { 261 TAPState *s = DO_UPCAST(TAPState, nc, nc); 262 263 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 264 265 return !!tap_probe_vnet_hdr_len(s->fd, len); 266 } 267 268 static int tap_get_vnet_hdr_len(NetClientState *nc) 269 { 270 TAPState *s = DO_UPCAST(TAPState, nc, nc); 271 272 return s->host_vnet_hdr_len; 273 } 274 275 static void tap_set_vnet_hdr_len(NetClientState *nc, int len) 276 { 277 TAPState *s = DO_UPCAST(TAPState, nc, nc); 278 279 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 280 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || 281 len == sizeof(struct virtio_net_hdr) || 282 len == sizeof(struct virtio_net_hdr_v1_hash)); 283 284 tap_fd_set_vnet_hdr_len(s->fd, len); 285 s->host_vnet_hdr_len = len; 286 } 287 288 static bool tap_get_using_vnet_hdr(NetClientState *nc) 289 { 290 TAPState *s = DO_UPCAST(TAPState, nc, nc); 291 292 return s->using_vnet_hdr; 293 } 294 295 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr) 296 { 297 TAPState *s = DO_UPCAST(TAPState, nc, nc); 298 299 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 300 assert(!!s->host_vnet_hdr_len == using_vnet_hdr); 301 302 s->using_vnet_hdr = using_vnet_hdr; 303 } 304 305 static int tap_set_vnet_le(NetClientState *nc, bool is_le) 306 { 307 TAPState *s = DO_UPCAST(TAPState, nc, nc); 308 309 return tap_fd_set_vnet_le(s->fd, is_le); 310 } 311 312 static int tap_set_vnet_be(NetClientState *nc, bool is_be) 313 { 314 TAPState *s = DO_UPCAST(TAPState, nc, nc); 315 316 return tap_fd_set_vnet_be(s->fd, is_be); 317 } 318 319 static void tap_set_offload(NetClientState *nc, int csum, int tso4, 320 int tso6, int ecn, int ufo, int uso4, int uso6) 321 { 322 TAPState *s = DO_UPCAST(TAPState, nc, nc); 323 if (s->fd < 0) { 324 return; 325 } 326 327 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo, uso4, uso6); 328 } 329 330 static void tap_exit_notify(Notifier *notifier, void *data) 331 { 332 TAPState *s = container_of(notifier, TAPState, exit); 333 Error *err = NULL; 334 335 if (s->down_script[0]) { 336 launch_script(s->down_script, s->down_script_arg, s->fd, &err); 337 if (err) { 338 error_report_err(err); 339 } 340 } 341 } 342 343 static void tap_cleanup(NetClientState *nc) 344 { 345 TAPState *s = DO_UPCAST(TAPState, nc, nc); 346 347 if (s->vhost_net) { 348 vhost_net_cleanup(s->vhost_net); 349 g_free(s->vhost_net); 350 s->vhost_net = NULL; 351 } 352 353 qemu_purge_queued_packets(nc); 354 355 tap_exit_notify(&s->exit, NULL); 356 qemu_remove_exit_notifier(&s->exit); 357 358 tap_read_poll(s, false); 359 tap_write_poll(s, false); 360 close(s->fd); 361 s->fd = -1; 362 } 363 364 static void tap_poll(NetClientState *nc, bool enable) 365 { 366 TAPState *s = DO_UPCAST(TAPState, nc, nc); 367 tap_read_poll(s, enable); 368 tap_write_poll(s, enable); 369 } 370 371 static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd) 372 { 373 TAPState *s = DO_UPCAST(TAPState, nc, nc); 374 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 375 376 return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0; 377 } 378 379 int tap_get_fd(NetClientState *nc) 380 { 381 TAPState *s = DO_UPCAST(TAPState, nc, nc); 382 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 383 return s->fd; 384 } 385 386 /* fd support */ 387 388 static NetClientInfo net_tap_info = { 389 .type = NET_CLIENT_DRIVER_TAP, 390 .size = sizeof(TAPState), 391 .receive = tap_receive, 392 .receive_raw = tap_receive_raw, 393 .receive_iov = tap_receive_iov, 394 .poll = tap_poll, 395 .cleanup = tap_cleanup, 396 .has_ufo = tap_has_ufo, 397 .has_uso = tap_has_uso, 398 .has_vnet_hdr = tap_has_vnet_hdr, 399 .has_vnet_hdr_len = tap_has_vnet_hdr_len, 400 .get_using_vnet_hdr = tap_get_using_vnet_hdr, 401 .using_vnet_hdr = tap_using_vnet_hdr, 402 .set_offload = tap_set_offload, 403 .get_vnet_hdr_len = tap_get_vnet_hdr_len, 404 .set_vnet_hdr_len = tap_set_vnet_hdr_len, 405 .set_vnet_le = tap_set_vnet_le, 406 .set_vnet_be = tap_set_vnet_be, 407 .set_steering_ebpf = tap_set_steering_ebpf, 408 }; 409 410 static TAPState *net_tap_fd_init(NetClientState *peer, 411 const char *model, 412 const char *name, 413 int fd, 414 int vnet_hdr) 415 { 416 NetClientState *nc; 417 TAPState *s; 418 419 nc = qemu_new_net_client(&net_tap_info, peer, model, name); 420 421 s = DO_UPCAST(TAPState, nc, nc); 422 423 s->fd = fd; 424 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; 425 s->using_vnet_hdr = false; 426 s->has_ufo = tap_probe_has_ufo(s->fd); 427 s->has_uso = tap_probe_has_uso(s->fd); 428 s->enabled = true; 429 tap_set_offload(&s->nc, 0, 0, 0, 0, 0, 0, 0); 430 /* 431 * Make sure host header length is set correctly in tap: 432 * it might have been modified by another instance of qemu. 433 */ 434 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) { 435 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len); 436 } 437 tap_read_poll(s, true); 438 s->vhost_net = NULL; 439 440 s->exit.notify = tap_exit_notify; 441 qemu_add_exit_notifier(&s->exit); 442 443 return s; 444 } 445 446 static void launch_script(const char *setup_script, const char *ifname, 447 int fd, Error **errp) 448 { 449 int pid, status; 450 char *args[3]; 451 char **parg; 452 453 /* try to launch network script */ 454 pid = fork(); 455 if (pid < 0) { 456 error_setg_errno(errp, errno, "could not launch network script %s", 457 setup_script); 458 return; 459 } 460 if (pid == 0) { 461 int open_max = sysconf(_SC_OPEN_MAX), i; 462 463 for (i = 3; i < open_max; i++) { 464 if (i != fd) { 465 close(i); 466 } 467 } 468 parg = args; 469 *parg++ = (char *)setup_script; 470 *parg++ = (char *)ifname; 471 *parg = NULL; 472 execv(setup_script, args); 473 _exit(1); 474 } else { 475 while (waitpid(pid, &status, 0) != pid) { 476 /* loop */ 477 } 478 479 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 480 return; 481 } 482 error_setg(errp, "network script %s failed with status %d", 483 setup_script, status); 484 } 485 } 486 487 static int recv_fd(int c) 488 { 489 int fd; 490 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))]; 491 struct msghdr msg = { 492 .msg_control = msgbuf, 493 .msg_controllen = sizeof(msgbuf), 494 }; 495 struct cmsghdr *cmsg; 496 struct iovec iov; 497 uint8_t req[1]; 498 ssize_t len; 499 500 cmsg = CMSG_FIRSTHDR(&msg); 501 cmsg->cmsg_level = SOL_SOCKET; 502 cmsg->cmsg_type = SCM_RIGHTS; 503 cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); 504 msg.msg_controllen = cmsg->cmsg_len; 505 506 iov.iov_base = req; 507 iov.iov_len = sizeof(req); 508 509 msg.msg_iov = &iov; 510 msg.msg_iovlen = 1; 511 512 len = recvmsg(c, &msg, 0); 513 if (len > 0) { 514 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); 515 return fd; 516 } 517 518 return len; 519 } 520 521 static int net_bridge_run_helper(const char *helper, const char *bridge, 522 Error **errp) 523 { 524 sigset_t oldmask, mask; 525 g_autofree char *default_helper = NULL; 526 int pid, status; 527 char *args[5]; 528 char **parg; 529 int sv[2]; 530 531 sigemptyset(&mask); 532 sigaddset(&mask, SIGCHLD); 533 sigprocmask(SIG_BLOCK, &mask, &oldmask); 534 535 if (!helper) { 536 helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER); 537 } 538 539 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { 540 error_setg_errno(errp, errno, "socketpair() failed"); 541 return -1; 542 } 543 544 /* try to launch bridge helper */ 545 pid = fork(); 546 if (pid < 0) { 547 error_setg_errno(errp, errno, "Can't fork bridge helper"); 548 return -1; 549 } 550 if (pid == 0) { 551 int open_max = sysconf(_SC_OPEN_MAX), i; 552 char *fd_buf = NULL; 553 char *br_buf = NULL; 554 char *helper_cmd = NULL; 555 556 for (i = 3; i < open_max; i++) { 557 if (i != sv[1]) { 558 close(i); 559 } 560 } 561 562 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]); 563 564 if (strrchr(helper, ' ') || strrchr(helper, '\t')) { 565 /* assume helper is a command */ 566 567 if (strstr(helper, "--br=") == NULL) { 568 br_buf = g_strdup_printf("%s%s", "--br=", bridge); 569 } 570 571 helper_cmd = g_strdup_printf("%s %s %s %s", helper, 572 "--use-vnet", fd_buf, br_buf ? br_buf : ""); 573 574 parg = args; 575 *parg++ = (char *)"sh"; 576 *parg++ = (char *)"-c"; 577 *parg++ = helper_cmd; 578 *parg++ = NULL; 579 580 execv("/bin/sh", args); 581 g_free(helper_cmd); 582 } else { 583 /* assume helper is just the executable path name */ 584 585 br_buf = g_strdup_printf("%s%s", "--br=", bridge); 586 587 parg = args; 588 *parg++ = (char *)helper; 589 *parg++ = (char *)"--use-vnet"; 590 *parg++ = fd_buf; 591 *parg++ = br_buf; 592 *parg++ = NULL; 593 594 execv(helper, args); 595 } 596 g_free(fd_buf); 597 g_free(br_buf); 598 _exit(1); 599 600 } else { 601 int fd; 602 int saved_errno; 603 604 close(sv[1]); 605 606 fd = RETRY_ON_EINTR(recv_fd(sv[0])); 607 saved_errno = errno; 608 609 close(sv[0]); 610 611 while (waitpid(pid, &status, 0) != pid) { 612 /* loop */ 613 } 614 sigprocmask(SIG_SETMASK, &oldmask, NULL); 615 if (fd < 0) { 616 error_setg_errno(errp, saved_errno, 617 "failed to recv file descriptor"); 618 return -1; 619 } 620 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { 621 error_setg(errp, "bridge helper failed"); 622 return -1; 623 } 624 return fd; 625 } 626 } 627 628 int net_init_bridge(const Netdev *netdev, const char *name, 629 NetClientState *peer, Error **errp) 630 { 631 const NetdevBridgeOptions *bridge; 632 const char *helper, *br; 633 TAPState *s; 634 int fd, vnet_hdr; 635 636 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE); 637 bridge = &netdev->u.bridge; 638 helper = bridge->helper; 639 br = bridge->br ?: DEFAULT_BRIDGE_INTERFACE; 640 641 fd = net_bridge_run_helper(helper, br, errp); 642 if (fd == -1) { 643 return -1; 644 } 645 646 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { 647 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 648 return -1; 649 } 650 vnet_hdr = tap_probe_vnet_hdr(fd, errp); 651 if (vnet_hdr < 0) { 652 close(fd); 653 return -1; 654 } 655 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); 656 657 qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br); 658 659 return 0; 660 } 661 662 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, 663 const char *setup_script, char *ifname, 664 size_t ifname_sz, int mq_required, Error **errp) 665 { 666 Error *err = NULL; 667 int fd, vnet_hdr_required; 668 669 if (tap->has_vnet_hdr) { 670 *vnet_hdr = tap->vnet_hdr; 671 vnet_hdr_required = *vnet_hdr; 672 } else { 673 *vnet_hdr = 1; 674 vnet_hdr_required = 0; 675 } 676 677 fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required, 678 mq_required, errp)); 679 if (fd < 0) { 680 return -1; 681 } 682 683 if (setup_script && 684 setup_script[0] != '\0' && 685 strcmp(setup_script, "no") != 0) { 686 launch_script(setup_script, ifname, fd, &err); 687 if (err) { 688 error_propagate(errp, err); 689 close(fd); 690 return -1; 691 } 692 } 693 694 return fd; 695 } 696 697 #define MAX_TAP_QUEUES 1024 698 699 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, 700 const char *model, const char *name, 701 const char *ifname, const char *script, 702 const char *downscript, const char *vhostfdname, 703 int vnet_hdr, int fd, Error **errp) 704 { 705 Error *err = NULL; 706 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); 707 int vhostfd; 708 709 tap_set_sndbuf(s->fd, tap, &err); 710 if (err) { 711 error_propagate(errp, err); 712 goto failed; 713 } 714 715 if (tap->fd || tap->fds) { 716 qemu_set_info_str(&s->nc, "fd=%d", fd); 717 } else if (tap->helper) { 718 qemu_set_info_str(&s->nc, "helper=%s", tap->helper); 719 } else { 720 qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname, 721 script, downscript); 722 723 if (strcmp(downscript, "no") != 0) { 724 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); 725 snprintf(s->down_script_arg, sizeof(s->down_script_arg), 726 "%s", ifname); 727 } 728 } 729 730 if (tap->has_vhost ? tap->vhost : 731 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { 732 VhostNetOptions options; 733 734 options.backend_type = VHOST_BACKEND_TYPE_KERNEL; 735 options.net_backend = &s->nc; 736 if (tap->has_poll_us) { 737 options.busyloop_timeout = tap->poll_us; 738 } else { 739 options.busyloop_timeout = 0; 740 } 741 742 if (vhostfdname) { 743 vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err); 744 if (vhostfd == -1) { 745 if (tap->has_vhostforce && tap->vhostforce) { 746 error_propagate(errp, err); 747 } else { 748 warn_report_err(err); 749 } 750 goto failed; 751 } 752 if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) { 753 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", 754 name, fd); 755 goto failed; 756 } 757 } else { 758 vhostfd = open("/dev/vhost-net", O_RDWR); 759 if (vhostfd < 0) { 760 if (tap->has_vhostforce && tap->vhostforce) { 761 error_setg_errno(errp, errno, 762 "tap: open vhost char device failed"); 763 } else { 764 warn_report("tap: open vhost char device failed: %s", 765 strerror(errno)); 766 } 767 goto failed; 768 } 769 if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) { 770 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 771 goto failed; 772 } 773 } 774 options.opaque = (void *)(uintptr_t)vhostfd; 775 options.nvqs = 2; 776 777 s->vhost_net = vhost_net_init(&options); 778 if (!s->vhost_net) { 779 if (tap->has_vhostforce && tap->vhostforce) { 780 error_setg(errp, VHOST_NET_INIT_FAILED); 781 } else { 782 warn_report(VHOST_NET_INIT_FAILED); 783 } 784 goto failed; 785 } 786 } else if (vhostfdname) { 787 error_setg(errp, "vhostfd(s)= is not valid without vhost"); 788 goto failed; 789 } 790 791 return; 792 793 failed: 794 qemu_del_net_client(&s->nc); 795 } 796 797 static int get_fds(char *str, char *fds[], int max) 798 { 799 char *ptr = str, *this; 800 size_t len = strlen(str); 801 int i = 0; 802 803 while (i < max && ptr < str + len) { 804 this = strchr(ptr, ':'); 805 806 if (this == NULL) { 807 fds[i] = g_strdup(ptr); 808 } else { 809 fds[i] = g_strndup(ptr, this - ptr); 810 } 811 812 i++; 813 if (this == NULL) { 814 break; 815 } else { 816 ptr = this + 1; 817 } 818 } 819 820 return i; 821 } 822 823 int net_init_tap(const Netdev *netdev, const char *name, 824 NetClientState *peer, Error **errp) 825 { 826 const NetdevTapOptions *tap; 827 int fd, vnet_hdr = 0, i = 0, queues; 828 /* for the no-fd, no-helper case */ 829 const char *script; 830 const char *downscript; 831 Error *err = NULL; 832 const char *vhostfdname; 833 char ifname[128]; 834 int ret = 0; 835 836 assert(netdev->type == NET_CLIENT_DRIVER_TAP); 837 tap = &netdev->u.tap; 838 queues = tap->has_queues ? tap->queues : 1; 839 vhostfdname = tap->vhostfd; 840 script = tap->script; 841 downscript = tap->downscript; 842 843 /* QEMU hubs do not support multiqueue tap, in this case peer is set. 844 * For -netdev, peer is always NULL. */ 845 if (peer && (tap->has_queues || tap->fds || tap->vhostfds)) { 846 error_setg(errp, "Multiqueue tap cannot be used with hubs"); 847 return -1; 848 } 849 850 if (tap->fd) { 851 if (tap->ifname || tap->script || tap->downscript || 852 tap->has_vnet_hdr || tap->helper || tap->has_queues || 853 tap->fds || tap->vhostfds) { 854 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " 855 "helper=, queues=, fds=, and vhostfds= " 856 "are invalid with fd="); 857 return -1; 858 } 859 860 fd = monitor_fd_param(monitor_cur(), tap->fd, errp); 861 if (fd == -1) { 862 return -1; 863 } 864 865 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { 866 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", 867 name, fd); 868 close(fd); 869 return -1; 870 } 871 872 vnet_hdr = tap_probe_vnet_hdr(fd, errp); 873 if (vnet_hdr < 0) { 874 close(fd); 875 return -1; 876 } 877 878 net_init_tap_one(tap, peer, "tap", name, NULL, 879 script, downscript, 880 vhostfdname, vnet_hdr, fd, &err); 881 if (err) { 882 error_propagate(errp, err); 883 close(fd); 884 return -1; 885 } 886 } else if (tap->fds) { 887 char **fds; 888 char **vhost_fds; 889 int nfds = 0, nvhosts = 0; 890 891 if (tap->ifname || tap->script || tap->downscript || 892 tap->has_vnet_hdr || tap->helper || tap->has_queues || 893 tap->vhostfd) { 894 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " 895 "helper=, queues=, and vhostfd= " 896 "are invalid with fds="); 897 return -1; 898 } 899 900 fds = g_new0(char *, MAX_TAP_QUEUES); 901 vhost_fds = g_new0(char *, MAX_TAP_QUEUES); 902 903 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES); 904 if (tap->vhostfds) { 905 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES); 906 if (nfds != nvhosts) { 907 error_setg(errp, "The number of fds passed does not match " 908 "the number of vhostfds passed"); 909 ret = -1; 910 goto free_fail; 911 } 912 } 913 914 for (i = 0; i < nfds; i++) { 915 fd = monitor_fd_param(monitor_cur(), fds[i], errp); 916 if (fd == -1) { 917 ret = -1; 918 goto free_fail; 919 } 920 921 ret = g_unix_set_fd_nonblocking(fd, true, NULL); 922 if (!ret) { 923 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", 924 name, fd); 925 goto free_fail; 926 } 927 928 if (i == 0) { 929 vnet_hdr = tap_probe_vnet_hdr(fd, errp); 930 if (vnet_hdr < 0) { 931 ret = -1; 932 goto free_fail; 933 } 934 } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) { 935 error_setg(errp, 936 "vnet_hdr not consistent across given tap fds"); 937 ret = -1; 938 goto free_fail; 939 } 940 941 net_init_tap_one(tap, peer, "tap", name, ifname, 942 script, downscript, 943 tap->vhostfds ? vhost_fds[i] : NULL, 944 vnet_hdr, fd, &err); 945 if (err) { 946 error_propagate(errp, err); 947 ret = -1; 948 goto free_fail; 949 } 950 } 951 952 free_fail: 953 for (i = 0; i < nvhosts; i++) { 954 g_free(vhost_fds[i]); 955 } 956 for (i = 0; i < nfds; i++) { 957 g_free(fds[i]); 958 } 959 g_free(fds); 960 g_free(vhost_fds); 961 return ret; 962 } else if (tap->helper) { 963 if (tap->ifname || tap->script || tap->downscript || 964 tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) { 965 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " 966 "queues=, and vhostfds= are invalid with helper="); 967 return -1; 968 } 969 970 fd = net_bridge_run_helper(tap->helper, 971 tap->br ?: DEFAULT_BRIDGE_INTERFACE, 972 errp); 973 if (fd == -1) { 974 return -1; 975 } 976 977 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { 978 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 979 return -1; 980 } 981 vnet_hdr = tap_probe_vnet_hdr(fd, errp); 982 if (vnet_hdr < 0) { 983 close(fd); 984 return -1; 985 } 986 987 net_init_tap_one(tap, peer, "bridge", name, ifname, 988 script, downscript, vhostfdname, 989 vnet_hdr, fd, &err); 990 if (err) { 991 error_propagate(errp, err); 992 close(fd); 993 return -1; 994 } 995 } else { 996 g_autofree char *default_script = NULL; 997 g_autofree char *default_downscript = NULL; 998 if (tap->vhostfds) { 999 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified"); 1000 return -1; 1001 } 1002 1003 if (!script) { 1004 script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT); 1005 } 1006 if (!downscript) { 1007 downscript = default_downscript = 1008 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT); 1009 } 1010 1011 if (tap->ifname) { 1012 pstrcpy(ifname, sizeof ifname, tap->ifname); 1013 } else { 1014 ifname[0] = '\0'; 1015 } 1016 1017 for (i = 0; i < queues; i++) { 1018 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, 1019 ifname, sizeof ifname, queues > 1, errp); 1020 if (fd == -1) { 1021 return -1; 1022 } 1023 1024 if (queues > 1 && i == 0 && !tap->ifname) { 1025 if (tap_fd_get_ifname(fd, ifname)) { 1026 error_setg(errp, "Fail to get ifname"); 1027 close(fd); 1028 return -1; 1029 } 1030 } 1031 1032 net_init_tap_one(tap, peer, "tap", name, ifname, 1033 i >= 1 ? "no" : script, 1034 i >= 1 ? "no" : downscript, 1035 vhostfdname, vnet_hdr, fd, &err); 1036 if (err) { 1037 error_propagate(errp, err); 1038 close(fd); 1039 return -1; 1040 } 1041 } 1042 } 1043 1044 return 0; 1045 } 1046 1047 VHostNetState *tap_get_vhost_net(NetClientState *nc) 1048 { 1049 TAPState *s = DO_UPCAST(TAPState, nc, nc); 1050 assert(nc->info->type == NET_CLIENT_DRIVER_TAP); 1051 return s->vhost_net; 1052 } 1053 1054 int tap_enable(NetClientState *nc) 1055 { 1056 TAPState *s = DO_UPCAST(TAPState, nc, nc); 1057 int ret; 1058 1059 if (s->enabled) { 1060 return 0; 1061 } else { 1062 ret = tap_fd_enable(s->fd); 1063 if (ret == 0) { 1064 s->enabled = true; 1065 tap_update_fd_handler(s); 1066 } 1067 return ret; 1068 } 1069 } 1070 1071 int tap_disable(NetClientState *nc) 1072 { 1073 TAPState *s = DO_UPCAST(TAPState, nc, nc); 1074 int ret; 1075 1076 if (s->enabled == 0) { 1077 return 0; 1078 } else { 1079 ret = tap_fd_disable(s->fd); 1080 if (ret == 0) { 1081 qemu_purge_queued_packets(nc); 1082 s->enabled = false; 1083 tap_update_fd_handler(s); 1084 } 1085 return ret; 1086 } 1087 } 1088