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