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