Merge remote-tracking branch 'remotes/agraf/tags/signed-s390-for-upstream' into staging
[qemu.git] / net / tap.c
blobaa8b3f5c8cde39e705be48c297f1bf46641f8389
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "tap_int.h"
28 #include "config-host.h"
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>
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"
43 #include "net/tap.h"
45 #include "net/vhost_net.h"
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;
62 static void launch_script(const char *setup_script, const char *ifname,
63 int fd, Error **errp);
65 static void tap_send(void *opaque);
66 static void tap_writable(void *opaque);
68 static void tap_update_fd_handler(TAPState *s)
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);
76 static void tap_read_poll(TAPState *s, bool enable)
78 s->read_poll = enable;
79 tap_update_fd_handler(s);
82 static void tap_write_poll(TAPState *s, bool enable)
84 s->write_poll = enable;
85 tap_update_fd_handler(s);
88 static void tap_writable(void *opaque)
90 TAPState *s = opaque;
92 tap_write_poll(s, false);
94 qemu_flush_queued_packets(&s->nc);
97 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
99 ssize_t len;
101 do {
102 len = writev(s->fd, iov, iovcnt);
103 } while (len == -1 && errno == EINTR);
105 if (len == -1 && errno == EAGAIN) {
106 tap_write_poll(s, true);
107 return 0;
110 return len;
113 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
114 int iovcnt)
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 = { };
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++;
129 return tap_write_packet(s, iovp, iovcnt);
132 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
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 = { };
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++;
145 iov[iovcnt].iov_base = (char *)buf;
146 iov[iovcnt].iov_len = size;
147 iovcnt++;
149 return tap_write_packet(s, iov, iovcnt);
152 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
154 TAPState *s = DO_UPCAST(TAPState, nc, nc);
155 struct iovec iov[1];
157 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
158 return tap_receive_raw(nc, buf, size);
161 iov[0].iov_base = (char *)buf;
162 iov[0].iov_len = size;
164 return tap_write_packet(s, iov, 1);
167 #ifndef __sun__
168 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
170 return read(tapfd, buf, maxlen);
172 #endif
174 static void tap_send_completed(NetClientState *nc, ssize_t len)
176 TAPState *s = DO_UPCAST(TAPState, nc, nc);
177 tap_read_poll(s, true);
180 static void tap_send(void *opaque)
182 TAPState *s = opaque;
183 int size;
184 int packets = 0;
186 while (true) {
187 uint8_t *buf = s->buf;
189 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
190 if (size <= 0) {
191 break;
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;
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;
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.
213 packets++;
214 if (packets >= 50) {
215 break;
220 static bool tap_has_ufo(NetClientState *nc)
222 TAPState *s = DO_UPCAST(TAPState, nc, nc);
224 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
226 return s->has_ufo;
229 static bool tap_has_vnet_hdr(NetClientState *nc)
231 TAPState *s = DO_UPCAST(TAPState, nc, nc);
233 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
235 return !!s->host_vnet_hdr_len;
238 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
240 TAPState *s = DO_UPCAST(TAPState, nc, nc);
242 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
244 return !!tap_probe_vnet_hdr_len(s->fd, len);
247 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
249 TAPState *s = DO_UPCAST(TAPState, nc, nc);
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));
255 tap_fd_set_vnet_hdr_len(s->fd, len);
256 s->host_vnet_hdr_len = len;
259 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
261 TAPState *s = DO_UPCAST(TAPState, nc, nc);
263 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
264 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
266 s->using_vnet_hdr = using_vnet_hdr;
269 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
270 int tso6, int ecn, int ufo)
272 TAPState *s = DO_UPCAST(TAPState, nc, nc);
273 if (s->fd < 0) {
274 return;
277 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
280 static void tap_cleanup(NetClientState *nc)
282 TAPState *s = DO_UPCAST(TAPState, nc, nc);
283 Error *err = NULL;
285 if (s->vhost_net) {
286 vhost_net_cleanup(s->vhost_net);
287 s->vhost_net = NULL;
290 qemu_purge_queued_packets(nc);
292 if (s->down_script[0]) {
293 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
294 if (err) {
295 error_report_err(err);
299 tap_read_poll(s, false);
300 tap_write_poll(s, false);
301 close(s->fd);
302 s->fd = -1;
305 static void tap_poll(NetClientState *nc, bool enable)
307 TAPState *s = DO_UPCAST(TAPState, nc, nc);
308 tap_read_poll(s, enable);
309 tap_write_poll(s, enable);
312 int tap_get_fd(NetClientState *nc)
314 TAPState *s = DO_UPCAST(TAPState, nc, nc);
315 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
316 return s->fd;
319 /* fd support */
321 static NetClientInfo net_tap_info = {
322 .type = NET_CLIENT_OPTIONS_KIND_TAP,
323 .size = sizeof(TAPState),
324 .receive = tap_receive,
325 .receive_raw = tap_receive_raw,
326 .receive_iov = tap_receive_iov,
327 .poll = tap_poll,
328 .cleanup = tap_cleanup,
329 .has_ufo = tap_has_ufo,
330 .has_vnet_hdr = tap_has_vnet_hdr,
331 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
332 .using_vnet_hdr = tap_using_vnet_hdr,
333 .set_offload = tap_set_offload,
334 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
337 static TAPState *net_tap_fd_init(NetClientState *peer,
338 const char *model,
339 const char *name,
340 int fd,
341 int vnet_hdr)
343 NetClientState *nc;
344 TAPState *s;
346 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
348 s = DO_UPCAST(TAPState, nc, nc);
350 s->fd = fd;
351 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
352 s->using_vnet_hdr = false;
353 s->has_ufo = tap_probe_has_ufo(s->fd);
354 s->enabled = true;
355 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
357 * Make sure host header length is set correctly in tap:
358 * it might have been modified by another instance of qemu.
360 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
361 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
363 tap_read_poll(s, true);
364 s->vhost_net = NULL;
365 return s;
368 static void launch_script(const char *setup_script, const char *ifname,
369 int fd, Error **errp)
371 int pid, status;
372 char *args[3];
373 char **parg;
375 /* try to launch network script */
376 pid = fork();
377 if (pid < 0) {
378 error_setg_errno(errp, errno, "could not launch network script %s",
379 setup_script);
380 return;
382 if (pid == 0) {
383 int open_max = sysconf(_SC_OPEN_MAX), i;
385 for (i = 3; i < open_max; i++) {
386 if (i != fd) {
387 close(i);
390 parg = args;
391 *parg++ = (char *)setup_script;
392 *parg++ = (char *)ifname;
393 *parg = NULL;
394 execv(setup_script, args);
395 _exit(1);
396 } else {
397 while (waitpid(pid, &status, 0) != pid) {
398 /* loop */
401 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
402 return;
404 error_setg(errp, "network script %s failed with status %d",
405 setup_script, status);
409 static int recv_fd(int c)
411 int fd;
412 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
413 struct msghdr msg = {
414 .msg_control = msgbuf,
415 .msg_controllen = sizeof(msgbuf),
417 struct cmsghdr *cmsg;
418 struct iovec iov;
419 uint8_t req[1];
420 ssize_t len;
422 cmsg = CMSG_FIRSTHDR(&msg);
423 cmsg->cmsg_level = SOL_SOCKET;
424 cmsg->cmsg_type = SCM_RIGHTS;
425 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
426 msg.msg_controllen = cmsg->cmsg_len;
428 iov.iov_base = req;
429 iov.iov_len = sizeof(req);
431 msg.msg_iov = &iov;
432 msg.msg_iovlen = 1;
434 len = recvmsg(c, &msg, 0);
435 if (len > 0) {
436 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
437 return fd;
440 return len;
443 static int net_bridge_run_helper(const char *helper, const char *bridge,
444 Error **errp)
446 sigset_t oldmask, mask;
447 int pid, status;
448 char *args[5];
449 char **parg;
450 int sv[2];
452 sigemptyset(&mask);
453 sigaddset(&mask, SIGCHLD);
454 sigprocmask(SIG_BLOCK, &mask, &oldmask);
456 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
457 error_setg_errno(errp, errno, "socketpair() failed");
458 return -1;
461 /* try to launch bridge helper */
462 pid = fork();
463 if (pid < 0) {
464 error_setg_errno(errp, errno, "Can't fork bridge helper");
465 return -1;
467 if (pid == 0) {
468 int open_max = sysconf(_SC_OPEN_MAX), i;
469 char fd_buf[6+10];
470 char br_buf[6+IFNAMSIZ] = {0};
471 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
473 for (i = 3; i < open_max; i++) {
474 if (i != sv[1]) {
475 close(i);
479 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
481 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
482 /* assume helper is a command */
484 if (strstr(helper, "--br=") == NULL) {
485 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
488 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
489 helper, "--use-vnet", fd_buf, br_buf);
491 parg = args;
492 *parg++ = (char *)"sh";
493 *parg++ = (char *)"-c";
494 *parg++ = helper_cmd;
495 *parg++ = NULL;
497 execv("/bin/sh", args);
498 } else {
499 /* assume helper is just the executable path name */
501 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
503 parg = args;
504 *parg++ = (char *)helper;
505 *parg++ = (char *)"--use-vnet";
506 *parg++ = fd_buf;
507 *parg++ = br_buf;
508 *parg++ = NULL;
510 execv(helper, args);
512 _exit(1);
514 } else {
515 int fd;
516 int saved_errno;
518 close(sv[1]);
520 do {
521 fd = recv_fd(sv[0]);
522 } while (fd == -1 && errno == EINTR);
523 saved_errno = errno;
525 close(sv[0]);
527 while (waitpid(pid, &status, 0) != pid) {
528 /* loop */
530 sigprocmask(SIG_SETMASK, &oldmask, NULL);
531 if (fd < 0) {
532 error_setg_errno(errp, saved_errno,
533 "failed to recv file descriptor");
534 return -1;
536 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
537 error_setg(errp, "bridge helper failed");
538 return -1;
540 return fd;
544 int net_init_bridge(const NetClientOptions *opts, const char *name,
545 NetClientState *peer, Error **errp)
547 const NetdevBridgeOptions *bridge;
548 const char *helper, *br;
549 TAPState *s;
550 int fd, vnet_hdr;
552 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
553 bridge = opts->bridge;
555 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
556 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
558 fd = net_bridge_run_helper(helper, br, errp);
559 if (fd == -1) {
560 return -1;
563 fcntl(fd, F_SETFL, O_NONBLOCK);
564 vnet_hdr = tap_probe_vnet_hdr(fd);
565 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
567 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
568 br);
570 return 0;
573 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
574 const char *setup_script, char *ifname,
575 size_t ifname_sz, int mq_required, Error **errp)
577 Error *err = NULL;
578 int fd, vnet_hdr_required;
580 if (tap->has_vnet_hdr) {
581 *vnet_hdr = tap->vnet_hdr;
582 vnet_hdr_required = *vnet_hdr;
583 } else {
584 *vnet_hdr = 1;
585 vnet_hdr_required = 0;
588 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
589 mq_required, errp));
590 if (fd < 0) {
591 return -1;
594 if (setup_script &&
595 setup_script[0] != '\0' &&
596 strcmp(setup_script, "no") != 0) {
597 launch_script(setup_script, ifname, fd, &err);
598 if (err) {
599 error_propagate(errp, err);
600 close(fd);
601 return -1;
605 return fd;
608 #define MAX_TAP_QUEUES 1024
610 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
611 const char *model, const char *name,
612 const char *ifname, const char *script,
613 const char *downscript, const char *vhostfdname,
614 int vnet_hdr, int fd, Error **errp)
616 Error *err = NULL;
617 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
618 int vhostfd;
620 tap_set_sndbuf(s->fd, tap, &err);
621 if (err) {
622 error_propagate(errp, err);
623 return;
626 if (tap->has_fd || tap->has_fds) {
627 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
628 } else if (tap->has_helper) {
629 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
630 tap->helper);
631 } else {
632 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
633 "ifname=%s,script=%s,downscript=%s", ifname, script,
634 downscript);
636 if (strcmp(downscript, "no") != 0) {
637 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
638 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
639 "%s", ifname);
643 if (tap->has_vhost ? tap->vhost :
644 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
645 VhostNetOptions options;
647 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
648 options.net_backend = &s->nc;
649 options.force = tap->has_vhostforce && tap->vhostforce;
651 if (tap->has_vhostfd || tap->has_vhostfds) {
652 vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
653 if (vhostfd == -1) {
654 error_propagate(errp, err);
655 return;
657 } else {
658 vhostfd = open("/dev/vhost-net", O_RDWR);
659 if (vhostfd < 0) {
660 error_setg_errno(errp, errno,
661 "tap: open vhost char device failed");
662 return;
665 options.opaque = (void *)(uintptr_t)vhostfd;
667 s->vhost_net = vhost_net_init(&options);
668 if (!s->vhost_net) {
669 error_setg(errp,
670 "vhost-net requested but could not be initialized");
671 return;
673 } else if (tap->has_vhostfd || tap->has_vhostfds) {
674 error_setg(errp, "vhostfd= is not valid without vhost");
678 static int get_fds(char *str, char *fds[], int max)
680 char *ptr = str, *this;
681 size_t len = strlen(str);
682 int i = 0;
684 while (i < max && ptr < str + len) {
685 this = strchr(ptr, ':');
687 if (this == NULL) {
688 fds[i] = g_strdup(ptr);
689 } else {
690 fds[i] = g_strndup(ptr, this - ptr);
693 i++;
694 if (this == NULL) {
695 break;
696 } else {
697 ptr = this + 1;
701 return i;
704 int net_init_tap(const NetClientOptions *opts, const char *name,
705 NetClientState *peer, Error **errp)
707 const NetdevTapOptions *tap;
708 int fd, vnet_hdr = 0, i = 0, queues;
709 /* for the no-fd, no-helper case */
710 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
711 const char *downscript = NULL;
712 Error *err = NULL;
713 const char *vhostfdname;
714 char ifname[128];
716 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
717 tap = opts->tap;
718 queues = tap->has_queues ? tap->queues : 1;
719 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
721 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
722 * For -netdev, peer is always NULL. */
723 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
724 error_setg(errp, "Multiqueue tap cannot be used with QEMU vlans");
725 return -1;
728 if (tap->has_fd) {
729 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
730 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
731 tap->has_fds || tap->has_vhostfds) {
732 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
733 "helper=, queues=, fds=, and vhostfds= "
734 "are invalid with fd=");
735 return -1;
738 fd = monitor_fd_param(cur_mon, tap->fd, &err);
739 if (fd == -1) {
740 error_propagate(errp, err);
741 return -1;
744 fcntl(fd, F_SETFL, O_NONBLOCK);
746 vnet_hdr = tap_probe_vnet_hdr(fd);
748 net_init_tap_one(tap, peer, "tap", name, NULL,
749 script, downscript,
750 vhostfdname, vnet_hdr, fd, &err);
751 if (err) {
752 error_propagate(errp, err);
753 return -1;
755 } else if (tap->has_fds) {
756 char *fds[MAX_TAP_QUEUES];
757 char *vhost_fds[MAX_TAP_QUEUES];
758 int nfds, nvhosts;
760 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
761 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
762 tap->has_vhostfd) {
763 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
764 "helper=, queues=, and vhostfd= "
765 "are invalid with fds=");
766 return -1;
769 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
770 if (tap->has_vhostfds) {
771 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
772 if (nfds != nvhosts) {
773 error_setg(errp, "The number of fds passed does not match "
774 "the number of vhostfds passed");
775 return -1;
779 for (i = 0; i < nfds; i++) {
780 fd = monitor_fd_param(cur_mon, fds[i], &err);
781 if (fd == -1) {
782 error_propagate(errp, err);
783 return -1;
786 fcntl(fd, F_SETFL, O_NONBLOCK);
788 if (i == 0) {
789 vnet_hdr = tap_probe_vnet_hdr(fd);
790 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
791 error_setg(errp,
792 "vnet_hdr not consistent across given tap fds");
793 return -1;
796 net_init_tap_one(tap, peer, "tap", name, ifname,
797 script, downscript,
798 tap->has_vhostfds ? vhost_fds[i] : NULL,
799 vnet_hdr, fd, &err);
800 if (err) {
801 error_propagate(errp, err);
802 return -1;
805 } else if (tap->has_helper) {
806 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
807 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
808 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
809 "queues=, and vhostfds= are invalid with helper=");
810 return -1;
813 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE,
814 errp);
815 if (fd == -1) {
816 return -1;
819 fcntl(fd, F_SETFL, O_NONBLOCK);
820 vnet_hdr = tap_probe_vnet_hdr(fd);
822 net_init_tap_one(tap, peer, "bridge", name, ifname,
823 script, downscript, vhostfdname,
824 vnet_hdr, fd, &err);
825 if (err) {
826 error_propagate(errp, err);
827 close(fd);
828 return -1;
830 } else {
831 if (tap->has_vhostfds) {
832 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
833 return -1;
835 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
836 downscript = tap->has_downscript ? tap->downscript :
837 DEFAULT_NETWORK_DOWN_SCRIPT;
839 if (tap->has_ifname) {
840 pstrcpy(ifname, sizeof ifname, tap->ifname);
841 } else {
842 ifname[0] = '\0';
845 for (i = 0; i < queues; i++) {
846 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
847 ifname, sizeof ifname, queues > 1, errp);
848 if (fd == -1) {
849 return -1;
852 if (queues > 1 && i == 0 && !tap->has_ifname) {
853 if (tap_fd_get_ifname(fd, ifname)) {
854 error_setg(errp, "Fail to get ifname");
855 close(fd);
856 return -1;
860 net_init_tap_one(tap, peer, "tap", name, ifname,
861 i >= 1 ? "no" : script,
862 i >= 1 ? "no" : downscript,
863 vhostfdname, vnet_hdr, fd, &err);
864 if (err) {
865 error_propagate(errp, err);
866 close(fd);
867 return -1;
872 return 0;
875 VHostNetState *tap_get_vhost_net(NetClientState *nc)
877 TAPState *s = DO_UPCAST(TAPState, nc, nc);
878 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
879 return s->vhost_net;
882 int tap_enable(NetClientState *nc)
884 TAPState *s = DO_UPCAST(TAPState, nc, nc);
885 int ret;
887 if (s->enabled) {
888 return 0;
889 } else {
890 ret = tap_fd_enable(s->fd);
891 if (ret == 0) {
892 s->enabled = true;
893 tap_update_fd_handler(s);
895 return ret;
899 int tap_disable(NetClientState *nc)
901 TAPState *s = DO_UPCAST(TAPState, nc, nc);
902 int ret;
904 if (s->enabled == 0) {
905 return 0;
906 } else {
907 ret = tap_fd_disable(s->fd);
908 if (ret == 0) {
909 qemu_purge_queued_packets(nc);
910 s->enabled = false;
911 tap_update_fd_handler(s);
913 return ret;