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