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