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
26 #include "qemu/osdep.h"
30 #include <sys/ioctl.h>
32 #include <sys/socket.h>
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qapi/error.h"
41 #include "qemu-common.h"
42 #include "qemu/cutils.h"
43 #include "qemu/error-report.h"
44 #include "qemu/main-loop.h"
45 #include "qemu/sockets.h"
49 #include "net/vhost_net.h"
51 typedef struct TAPState {
54 char down_script[1024];
55 char down_script_arg[128];
56 uint8_t buf[NET_BUFSIZE];
62 VHostNetState *vhost_net;
63 unsigned host_vnet_hdr_len;
67 static void launch_script(const char *setup_script, const char *ifname,
68 int fd, Error **errp);
70 static void tap_send(void *opaque);
71 static void tap_writable(void *opaque);
73 static void tap_update_fd_handler(TAPState *s)
75 qemu_set_fd_handler(s->fd,
76 s->read_poll && s->enabled ? tap_send : NULL,
77 s->write_poll && s->enabled ? tap_writable : NULL,
81 static void tap_read_poll(TAPState *s, bool enable)
83 s->read_poll = enable;
84 tap_update_fd_handler(s);
87 static void tap_write_poll(TAPState *s, bool enable)
89 s->write_poll = enable;
90 tap_update_fd_handler(s);
93 static void tap_writable(void *opaque)
97 tap_write_poll(s, false);
99 qemu_flush_queued_packets(&s->nc);
102 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
107 len = writev(s->fd, iov, iovcnt);
108 } while (len == -1 && errno == EINTR);
110 if (len == -1 && errno == EAGAIN) {
111 tap_write_poll(s, true);
118 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
121 TAPState *s = DO_UPCAST(TAPState, nc, nc);
122 const struct iovec *iovp = iov;
123 struct iovec iov_copy[iovcnt + 1];
124 struct virtio_net_hdr_mrg_rxbuf hdr = { };
126 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
127 iov_copy[0].iov_base = &hdr;
128 iov_copy[0].iov_len = s->host_vnet_hdr_len;
129 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
134 return tap_write_packet(s, iovp, iovcnt);
137 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
139 TAPState *s = DO_UPCAST(TAPState, nc, nc);
142 struct virtio_net_hdr_mrg_rxbuf hdr = { };
144 if (s->host_vnet_hdr_len) {
145 iov[iovcnt].iov_base = &hdr;
146 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
150 iov[iovcnt].iov_base = (char *)buf;
151 iov[iovcnt].iov_len = size;
154 return tap_write_packet(s, iov, iovcnt);
157 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
159 TAPState *s = DO_UPCAST(TAPState, nc, nc);
162 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
163 return tap_receive_raw(nc, buf, size);
166 iov[0].iov_base = (char *)buf;
167 iov[0].iov_len = size;
169 return tap_write_packet(s, iov, 1);
173 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
175 return read(tapfd, buf, maxlen);
179 static void tap_send_completed(NetClientState *nc, ssize_t len)
181 TAPState *s = DO_UPCAST(TAPState, nc, nc);
182 tap_read_poll(s, true);
185 static void tap_send(void *opaque)
187 TAPState *s = opaque;
192 uint8_t *buf = s->buf;
193 uint8_t min_pkt[ETH_ZLEN];
194 size_t min_pktsz = sizeof(min_pkt);
196 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
201 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
202 buf += s->host_vnet_hdr_len;
203 size -= s->host_vnet_hdr_len;
206 if (!s->nc.peer->do_not_pad) {
207 if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) {
213 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
215 tap_read_poll(s, false);
217 } else if (size < 0) {
222 * When the host keeps receiving more packets while tap_send() is
223 * running we can hog the QEMU global mutex. Limit the number of
224 * packets that are processed per tap_send() callback to prevent
225 * stalling the guest.
234 static bool tap_has_ufo(NetClientState *nc)
236 TAPState *s = DO_UPCAST(TAPState, nc, nc);
238 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
243 static bool tap_has_vnet_hdr(NetClientState *nc)
245 TAPState *s = DO_UPCAST(TAPState, nc, nc);
247 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
249 return !!s->host_vnet_hdr_len;
252 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
254 TAPState *s = DO_UPCAST(TAPState, nc, nc);
256 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
258 return !!tap_probe_vnet_hdr_len(s->fd, len);
261 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
263 TAPState *s = DO_UPCAST(TAPState, nc, nc);
265 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
266 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
267 len == sizeof(struct virtio_net_hdr) ||
268 len == sizeof(struct virtio_net_hdr_v1_hash));
270 tap_fd_set_vnet_hdr_len(s->fd, len);
271 s->host_vnet_hdr_len = len;
274 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
276 TAPState *s = DO_UPCAST(TAPState, nc, nc);
278 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
279 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
281 s->using_vnet_hdr = using_vnet_hdr;
284 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
286 TAPState *s = DO_UPCAST(TAPState, nc, nc);
288 return tap_fd_set_vnet_le(s->fd, is_le);
291 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
293 TAPState *s = DO_UPCAST(TAPState, nc, nc);
295 return tap_fd_set_vnet_be(s->fd, is_be);
298 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
299 int tso6, int ecn, int ufo)
301 TAPState *s = DO_UPCAST(TAPState, nc, nc);
306 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
309 static void tap_exit_notify(Notifier *notifier, void *data)
311 TAPState *s = container_of(notifier, TAPState, exit);
314 if (s->down_script[0]) {
315 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
317 error_report_err(err);
322 static void tap_cleanup(NetClientState *nc)
324 TAPState *s = DO_UPCAST(TAPState, nc, nc);
327 vhost_net_cleanup(s->vhost_net);
328 g_free(s->vhost_net);
332 qemu_purge_queued_packets(nc);
334 tap_exit_notify(&s->exit, NULL);
335 qemu_remove_exit_notifier(&s->exit);
337 tap_read_poll(s, false);
338 tap_write_poll(s, false);
343 static void tap_poll(NetClientState *nc, bool enable)
345 TAPState *s = DO_UPCAST(TAPState, nc, nc);
346 tap_read_poll(s, enable);
347 tap_write_poll(s, enable);
350 int tap_get_fd(NetClientState *nc)
352 TAPState *s = DO_UPCAST(TAPState, nc, nc);
353 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
359 static NetClientInfo net_tap_info = {
360 .type = NET_CLIENT_DRIVER_TAP,
361 .size = sizeof(TAPState),
362 .receive = tap_receive,
363 .receive_raw = tap_receive_raw,
364 .receive_iov = tap_receive_iov,
366 .cleanup = tap_cleanup,
367 .has_ufo = tap_has_ufo,
368 .has_vnet_hdr = tap_has_vnet_hdr,
369 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
370 .using_vnet_hdr = tap_using_vnet_hdr,
371 .set_offload = tap_set_offload,
372 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
373 .set_vnet_le = tap_set_vnet_le,
374 .set_vnet_be = tap_set_vnet_be,
377 static TAPState *net_tap_fd_init(NetClientState *peer,
386 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
388 s = DO_UPCAST(TAPState, nc, nc);
391 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
392 s->using_vnet_hdr = false;
393 s->has_ufo = tap_probe_has_ufo(s->fd);
395 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
397 * Make sure host header length is set correctly in tap:
398 * it might have been modified by another instance of qemu.
400 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
401 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
403 tap_read_poll(s, true);
406 s->exit.notify = tap_exit_notify;
407 qemu_add_exit_notifier(&s->exit);
412 static void launch_script(const char *setup_script, const char *ifname,
413 int fd, Error **errp)
419 /* try to launch network script */
422 error_setg_errno(errp, errno, "could not launch network script %s",
427 int open_max = sysconf(_SC_OPEN_MAX), i;
429 for (i = 3; i < open_max; i++) {
435 *parg++ = (char *)setup_script;
436 *parg++ = (char *)ifname;
438 execv(setup_script, args);
441 while (waitpid(pid, &status, 0) != pid) {
445 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
448 error_setg(errp, "network script %s failed with status %d",
449 setup_script, status);
453 static int recv_fd(int c)
456 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
457 struct msghdr msg = {
458 .msg_control = msgbuf,
459 .msg_controllen = sizeof(msgbuf),
461 struct cmsghdr *cmsg;
466 cmsg = CMSG_FIRSTHDR(&msg);
467 cmsg->cmsg_level = SOL_SOCKET;
468 cmsg->cmsg_type = SCM_RIGHTS;
469 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
470 msg.msg_controllen = cmsg->cmsg_len;
473 iov.iov_len = sizeof(req);
478 len = recvmsg(c, &msg, 0);
480 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
487 static int net_bridge_run_helper(const char *helper, const char *bridge,
490 sigset_t oldmask, mask;
491 g_autofree char *default_helper = NULL;
498 sigaddset(&mask, SIGCHLD);
499 sigprocmask(SIG_BLOCK, &mask, &oldmask);
502 helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
505 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
506 error_setg_errno(errp, errno, "socketpair() failed");
510 /* try to launch bridge helper */
513 error_setg_errno(errp, errno, "Can't fork bridge helper");
517 int open_max = sysconf(_SC_OPEN_MAX), i;
520 char *helper_cmd = NULL;
522 for (i = 3; i < open_max; i++) {
528 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
530 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
531 /* assume helper is a command */
533 if (strstr(helper, "--br=") == NULL) {
534 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
537 helper_cmd = g_strdup_printf("%s %s %s %s", helper,
538 "--use-vnet", fd_buf, br_buf ? br_buf : "");
541 *parg++ = (char *)"sh";
542 *parg++ = (char *)"-c";
543 *parg++ = helper_cmd;
546 execv("/bin/sh", args);
549 /* assume helper is just the executable path name */
551 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
554 *parg++ = (char *)helper;
555 *parg++ = (char *)"--use-vnet";
574 } while (fd == -1 && errno == EINTR);
579 while (waitpid(pid, &status, 0) != pid) {
582 sigprocmask(SIG_SETMASK, &oldmask, NULL);
584 error_setg_errno(errp, saved_errno,
585 "failed to recv file descriptor");
588 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
589 error_setg(errp, "bridge helper failed");
596 int net_init_bridge(const Netdev *netdev, const char *name,
597 NetClientState *peer, Error **errp)
599 const NetdevBridgeOptions *bridge;
600 const char *helper, *br;
603 NetdevBridgeOptions *stored;
605 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
606 bridge = &netdev->u.bridge;
607 helper = bridge->has_helper ? bridge->helper : NULL;
608 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
610 fd = net_bridge_run_helper(helper, br, errp);
615 qemu_set_nonblock(fd);
616 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
621 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
623 /* Store startup parameters */
624 s->nc.stored_config = g_new0(NetdevInfo, 1);
625 s->nc.stored_config->type = NET_BACKEND_BRIDGE;
626 stored = &s->nc.stored_config->u.bridge;
629 stored->has_br = true;
630 stored->br = g_strdup(br);
634 stored->has_helper = true;
635 stored->helper = g_strdup(helper);
641 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
642 const char *setup_script, char *ifname,
643 size_t ifname_sz, int mq_required, Error **errp)
646 int fd, vnet_hdr_required;
648 if (tap->has_vnet_hdr) {
649 *vnet_hdr = tap->vnet_hdr;
650 vnet_hdr_required = *vnet_hdr;
653 vnet_hdr_required = 0;
656 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
663 setup_script[0] != '\0' &&
664 strcmp(setup_script, "no") != 0) {
665 launch_script(setup_script, ifname, fd, &err);
667 error_propagate(errp, err);
676 #define MAX_TAP_QUEUES 1024
678 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
679 const char *model, const char *name,
680 const char *ifname, const char *script,
681 const char *downscript, const char *vhostfdname,
682 int vnet_hdr, int fd, NetdevInfo **common_stored,
686 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
688 NetdevTapOptions *stored;
690 tap_set_sndbuf(s->fd, tap, &err);
692 error_propagate(errp, err);
696 /* Store startup parameters */
697 if (!*common_stored) {
698 *common_stored = g_new0(NetdevInfo, 1);
699 (*common_stored)->type = NET_BACKEND_TAP;
700 s->nc.stored_config = *common_stored;
702 stored = &(*common_stored)->u.tap;
704 if (tap->has_sndbuf && !stored->has_sndbuf) {
705 stored->has_sndbuf = true;
706 stored->sndbuf = tap->sndbuf;
709 if (vnet_hdr && !stored->has_vnet_hdr) {
710 stored->has_vnet_hdr = true;
711 stored->vnet_hdr = true;
714 if (tap->has_fd || tap->has_fds) {
715 if (!stored->has_fds) {
716 stored->has_fds = true;
717 stored->fds = g_strdup_printf("%d", fd);
719 char *tmp_s = stored->fds;
720 stored->fds = g_strdup_printf("%s:%d", stored->fds, fd);
723 } else if (tap->has_helper) {
724 if (!stored->has_helper) {
725 stored->has_helper = true;
726 stored->helper = g_strdup(tap->helper);
729 if (!stored->has_br) {
730 stored->has_br = true;
731 stored->br = tap->has_br ? g_strdup(tap->br) :
732 g_strdup(DEFAULT_BRIDGE_INTERFACE);
735 if (ifname && !stored->has_ifname) {
736 stored->has_ifname = true;
737 stored->ifname = g_strdup(ifname);
740 if (script && !stored->has_script) {
741 stored->has_script = true;
742 stored->script = g_strdup(script);
745 if (downscript && !stored->has_downscript) {
746 stored->has_downscript = true;
747 stored->downscript = g_strdup(downscript);
750 if (strcmp(downscript, "no") != 0) {
751 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
752 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
757 if (tap->has_vhost ? tap->vhost :
758 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
759 VhostNetOptions options;
761 stored->has_vhost = true;
762 stored->vhost = true;
764 if (tap->has_vhostforce && tap->vhostforce) {
765 stored->has_vhostforce = true;
766 stored->vhostforce = true;
769 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
770 options.net_backend = &s->nc;
771 if (tap->has_poll_us) {
772 stored->has_poll_us = true;
773 stored->poll_us = tap->poll_us;
775 options.busyloop_timeout = tap->poll_us;
777 options.busyloop_timeout = 0;
783 vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
785 if (tap->has_vhostforce && tap->vhostforce) {
786 error_propagate(errp, err);
788 warn_report_err(err);
792 ret = qemu_try_set_nonblock(vhostfd);
794 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
799 vhostfd = open("/dev/vhost-net", O_RDWR);
801 if (tap->has_vhostforce && tap->vhostforce) {
802 error_setg_errno(errp, errno,
803 "tap: open vhost char device failed");
805 warn_report("tap: open vhost char device failed: %s",
810 qemu_set_nonblock(vhostfd);
812 options.opaque = (void *)(uintptr_t)vhostfd;
814 if (!stored->has_vhostfds) {
815 stored->has_vhostfds = true;
816 stored->vhostfds = g_strdup_printf("%d", vhostfd);
818 char *tmp_s = stored->vhostfds;
819 stored->vhostfds = g_strdup_printf("%s:%d", stored->fds, vhostfd);
823 s->vhost_net = vhost_net_init(&options);
825 if (tap->has_vhostforce && tap->vhostforce) {
826 error_setg(errp, VHOST_NET_INIT_FAILED);
828 warn_report(VHOST_NET_INIT_FAILED);
832 } else if (vhostfdname) {
833 error_setg(errp, "vhostfd(s)= is not valid without vhost");
837 static int get_fds(char *str, char *fds[], int max)
839 char *ptr = str, *this;
840 size_t len = strlen(str);
843 while (i < max && ptr < str + len) {
844 this = strchr(ptr, ':');
847 fds[i] = g_strdup(ptr);
849 fds[i] = g_strndup(ptr, this - ptr);
863 int net_init_tap(const Netdev *netdev, const char *name,
864 NetClientState *peer, Error **errp)
866 const NetdevTapOptions *tap;
867 int fd, vnet_hdr = 0, i = 0, queues;
868 /* for the no-fd, no-helper case */
870 const char *downscript;
872 const char *vhostfdname;
875 NetdevInfo *common_stored = NULL; /* will store configuration */
877 assert(netdev->type == NET_CLIENT_DRIVER_TAP);
878 tap = &netdev->u.tap;
879 queues = tap->has_queues ? tap->queues : 1;
880 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
881 script = tap->has_script ? tap->script : NULL;
882 downscript = tap->has_downscript ? tap->downscript : NULL;
884 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
885 * For -netdev, peer is always NULL. */
886 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
887 error_setg(errp, "Multiqueue tap cannot be used with hubs");
892 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
893 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
894 tap->has_fds || tap->has_vhostfds) {
895 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
896 "helper=, queues=, fds=, and vhostfds= "
897 "are invalid with fd=");
901 fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
906 ret = qemu_try_set_nonblock(fd);
908 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
914 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
920 net_init_tap_one(tap, peer, "tap", name, NULL,
922 vhostfdname, vnet_hdr, fd, &common_stored, &err);
924 error_propagate(errp, err);
928 } else if (tap->has_fds) {
931 int nfds = 0, nvhosts = 0;
933 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
934 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
936 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
937 "helper=, queues=, and vhostfd= "
938 "are invalid with fds=");
942 fds = g_new0(char *, MAX_TAP_QUEUES);
943 vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
945 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
946 if (tap->has_vhostfds) {
947 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
948 if (nfds != nvhosts) {
949 error_setg(errp, "The number of fds passed does not match "
950 "the number of vhostfds passed");
956 for (i = 0; i < nfds; i++) {
957 fd = monitor_fd_param(monitor_cur(), fds[i], errp);
963 ret = qemu_try_set_nonblock(fd);
965 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
971 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
975 } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
977 "vnet_hdr not consistent across given tap fds");
982 net_init_tap_one(tap, peer, "tap", name, ifname,
984 tap->has_vhostfds ? vhost_fds[i] : NULL,
985 vnet_hdr, fd, &common_stored, &err);
987 error_propagate(errp, err);
994 for (i = 0; i < nvhosts; i++) {
995 g_free(vhost_fds[i]);
997 for (i = 0; i < nfds; i++) {
1003 } else if (tap->has_helper) {
1004 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
1005 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
1006 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
1007 "queues=, and vhostfds= are invalid with helper=");
1011 fd = net_bridge_run_helper(tap->helper,
1013 tap->br : DEFAULT_BRIDGE_INTERFACE,
1019 qemu_set_nonblock(fd);
1020 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
1026 net_init_tap_one(tap, peer, "bridge", name, ifname,
1027 script, downscript, vhostfdname,
1028 vnet_hdr, fd, &common_stored, &err);
1030 error_propagate(errp, err);
1035 g_autofree char *default_script = NULL;
1036 g_autofree char *default_downscript = NULL;
1037 if (tap->has_vhostfds) {
1038 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
1043 script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
1046 downscript = default_downscript =
1047 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
1050 if (tap->has_ifname) {
1051 pstrcpy(ifname, sizeof ifname, tap->ifname);
1056 for (i = 0; i < queues; i++) {
1057 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
1058 ifname, sizeof ifname, queues > 1, errp);
1063 if (queues > 1 && i == 0 && !tap->has_ifname) {
1064 if (tap_fd_get_ifname(fd, ifname)) {
1065 error_setg(errp, "Fail to get ifname");
1071 net_init_tap_one(tap, peer, "tap", name, ifname,
1072 i >= 1 ? "no" : script,
1073 i >= 1 ? "no" : downscript,
1074 vhostfdname, vnet_hdr, fd,
1075 &common_stored, &err);
1077 error_propagate(errp, err);
1087 VHostNetState *tap_get_vhost_net(NetClientState *nc)
1089 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1090 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
1091 return s->vhost_net;
1094 int tap_enable(NetClientState *nc)
1096 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1102 ret = tap_fd_enable(s->fd);
1105 tap_update_fd_handler(s);
1111 int tap_disable(NetClientState *nc)
1113 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1116 if (s->enabled == 0) {
1119 ret = tap_fd_disable(s->fd);
1121 qemu_purge_queued_packets(nc);
1123 tap_update_fd_handler(s);