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