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/cutils.h"
42 #include "qemu/error-report.h"
43 #include "qemu/main-loop.h"
44 #include "qemu/sockets.h"
48 #include "net/vhost_net.h"
50 typedef struct TAPState
{
53 char down_script
[1024];
54 char down_script_arg
[128];
55 uint8_t buf
[NET_BUFSIZE
];
61 VHostNetState
*vhost_net
;
62 unsigned host_vnet_hdr_len
;
66 static void launch_script(const char *setup_script
, const char *ifname
,
67 int fd
, Error
**errp
);
69 static void tap_send(void *opaque
);
70 static void tap_writable(void *opaque
);
72 static void tap_update_fd_handler(TAPState
*s
)
74 qemu_set_fd_handler(s
->fd
,
75 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
76 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
80 static void tap_read_poll(TAPState
*s
, bool enable
)
82 s
->read_poll
= enable
;
83 tap_update_fd_handler(s
);
86 static void tap_write_poll(TAPState
*s
, bool enable
)
88 s
->write_poll
= enable
;
89 tap_update_fd_handler(s
);
92 static void tap_writable(void *opaque
)
96 tap_write_poll(s
, false);
98 qemu_flush_queued_packets(&s
->nc
);
101 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
106 len
= writev(s
->fd
, iov
, iovcnt
);
107 } while (len
== -1 && errno
== EINTR
);
109 if (len
== -1 && errno
== EAGAIN
) {
110 tap_write_poll(s
, true);
117 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
120 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
121 const struct iovec
*iovp
= iov
;
122 struct iovec iov_copy
[iovcnt
+ 1];
123 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
125 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
126 iov_copy
[0].iov_base
= &hdr
;
127 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
128 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
133 return tap_write_packet(s
, iovp
, iovcnt
);
136 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
138 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
141 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
143 if (s
->host_vnet_hdr_len
) {
144 iov
[iovcnt
].iov_base
= &hdr
;
145 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
149 iov
[iovcnt
].iov_base
= (char *)buf
;
150 iov
[iovcnt
].iov_len
= size
;
153 return tap_write_packet(s
, iov
, iovcnt
);
156 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
158 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
161 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
162 return tap_receive_raw(nc
, buf
, size
);
165 iov
[0].iov_base
= (char *)buf
;
166 iov
[0].iov_len
= size
;
168 return tap_write_packet(s
, iov
, 1);
172 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
174 return read(tapfd
, buf
, maxlen
);
178 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
180 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
181 tap_read_poll(s
, true);
184 static void tap_send(void *opaque
)
186 TAPState
*s
= opaque
;
191 uint8_t *buf
= s
->buf
;
192 uint8_t min_pkt
[ETH_ZLEN
];
193 size_t min_pktsz
= sizeof(min_pkt
);
195 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
200 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
201 buf
+= s
->host_vnet_hdr_len
;
202 size
-= s
->host_vnet_hdr_len
;
205 if (net_peer_needs_padding(&s
->nc
)) {
206 if (eth_pad_short_frame(min_pkt
, &min_pktsz
, buf
, size
)) {
212 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
214 tap_read_poll(s
, false);
216 } else if (size
< 0) {
221 * When the host keeps receiving more packets while tap_send() is
222 * running we can hog the QEMU global mutex. Limit the number of
223 * packets that are processed per tap_send() callback to prevent
224 * stalling the guest.
233 static bool tap_has_ufo(NetClientState
*nc
)
235 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
237 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
242 static bool tap_has_vnet_hdr(NetClientState
*nc
)
244 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
246 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
248 return !!s
->host_vnet_hdr_len
;
251 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
253 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
255 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
257 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
260 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
262 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
264 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
265 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
266 len
== sizeof(struct virtio_net_hdr
) ||
267 len
== sizeof(struct virtio_net_hdr_v1_hash
));
269 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
270 s
->host_vnet_hdr_len
= len
;
273 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
275 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
277 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
278 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
280 s
->using_vnet_hdr
= using_vnet_hdr
;
283 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
285 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
287 return tap_fd_set_vnet_le(s
->fd
, is_le
);
290 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
292 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
294 return tap_fd_set_vnet_be(s
->fd
, is_be
);
297 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
298 int tso6
, int ecn
, int ufo
)
300 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
305 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
308 static void tap_exit_notify(Notifier
*notifier
, void *data
)
310 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
313 if (s
->down_script
[0]) {
314 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
316 error_report_err(err
);
321 static void tap_cleanup(NetClientState
*nc
)
323 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
326 vhost_net_cleanup(s
->vhost_net
);
327 g_free(s
->vhost_net
);
331 qemu_purge_queued_packets(nc
);
333 tap_exit_notify(&s
->exit
, NULL
);
334 qemu_remove_exit_notifier(&s
->exit
);
336 tap_read_poll(s
, false);
337 tap_write_poll(s
, false);
342 static void tap_poll(NetClientState
*nc
, bool enable
)
344 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
345 tap_read_poll(s
, enable
);
346 tap_write_poll(s
, enable
);
349 static bool tap_set_steering_ebpf(NetClientState
*nc
, int prog_fd
)
351 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
352 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
354 return tap_fd_set_steering_ebpf(s
->fd
, prog_fd
) == 0;
357 int tap_get_fd(NetClientState
*nc
)
359 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
360 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
366 static NetClientInfo net_tap_info
= {
367 .type
= NET_CLIENT_DRIVER_TAP
,
368 .size
= sizeof(TAPState
),
369 .receive
= tap_receive
,
370 .receive_raw
= tap_receive_raw
,
371 .receive_iov
= tap_receive_iov
,
373 .cleanup
= tap_cleanup
,
374 .has_ufo
= tap_has_ufo
,
375 .has_vnet_hdr
= tap_has_vnet_hdr
,
376 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
377 .using_vnet_hdr
= tap_using_vnet_hdr
,
378 .set_offload
= tap_set_offload
,
379 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
380 .set_vnet_le
= tap_set_vnet_le
,
381 .set_vnet_be
= tap_set_vnet_be
,
382 .set_steering_ebpf
= tap_set_steering_ebpf
,
385 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
394 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
396 s
= DO_UPCAST(TAPState
, nc
, nc
);
399 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
400 s
->using_vnet_hdr
= false;
401 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
403 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
405 * Make sure host header length is set correctly in tap:
406 * it might have been modified by another instance of qemu.
408 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
409 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
411 tap_read_poll(s
, true);
414 s
->exit
.notify
= tap_exit_notify
;
415 qemu_add_exit_notifier(&s
->exit
);
420 static void launch_script(const char *setup_script
, const char *ifname
,
421 int fd
, Error
**errp
)
427 /* try to launch network script */
430 error_setg_errno(errp
, errno
, "could not launch network script %s",
435 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
437 for (i
= 3; i
< open_max
; i
++) {
443 *parg
++ = (char *)setup_script
;
444 *parg
++ = (char *)ifname
;
446 execv(setup_script
, args
);
449 while (waitpid(pid
, &status
, 0) != pid
) {
453 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
456 error_setg(errp
, "network script %s failed with status %d",
457 setup_script
, status
);
461 static int recv_fd(int c
)
464 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
465 struct msghdr msg
= {
466 .msg_control
= msgbuf
,
467 .msg_controllen
= sizeof(msgbuf
),
469 struct cmsghdr
*cmsg
;
474 cmsg
= CMSG_FIRSTHDR(&msg
);
475 cmsg
->cmsg_level
= SOL_SOCKET
;
476 cmsg
->cmsg_type
= SCM_RIGHTS
;
477 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
478 msg
.msg_controllen
= cmsg
->cmsg_len
;
481 iov
.iov_len
= sizeof(req
);
486 len
= recvmsg(c
, &msg
, 0);
488 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
495 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
498 sigset_t oldmask
, mask
;
499 g_autofree
char *default_helper
= NULL
;
506 sigaddset(&mask
, SIGCHLD
);
507 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
510 helper
= default_helper
= get_relocated_path(DEFAULT_BRIDGE_HELPER
);
513 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
514 error_setg_errno(errp
, errno
, "socketpair() failed");
518 /* try to launch bridge helper */
521 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
525 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
528 char *helper_cmd
= NULL
;
530 for (i
= 3; i
< open_max
; i
++) {
536 fd_buf
= g_strdup_printf("%s%d", "--fd=", sv
[1]);
538 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
539 /* assume helper is a command */
541 if (strstr(helper
, "--br=") == NULL
) {
542 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
545 helper_cmd
= g_strdup_printf("%s %s %s %s", helper
,
546 "--use-vnet", fd_buf
, br_buf
? br_buf
: "");
549 *parg
++ = (char *)"sh";
550 *parg
++ = (char *)"-c";
551 *parg
++ = helper_cmd
;
554 execv("/bin/sh", args
);
557 /* assume helper is just the executable path name */
559 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
562 *parg
++ = (char *)helper
;
563 *parg
++ = (char *)"--use-vnet";
582 } while (fd
== -1 && errno
== EINTR
);
587 while (waitpid(pid
, &status
, 0) != pid
) {
590 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
592 error_setg_errno(errp
, saved_errno
,
593 "failed to recv file descriptor");
596 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
597 error_setg(errp
, "bridge helper failed");
604 int net_init_bridge(const Netdev
*netdev
, const char *name
,
605 NetClientState
*peer
, Error
**errp
)
607 const NetdevBridgeOptions
*bridge
;
608 const char *helper
, *br
;
612 assert(netdev
->type
== NET_CLIENT_DRIVER_BRIDGE
);
613 bridge
= &netdev
->u
.bridge
;
614 helper
= bridge
->has_helper
? bridge
->helper
: NULL
;
615 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
617 fd
= net_bridge_run_helper(helper
, br
, errp
);
622 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
623 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
626 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
631 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
633 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
639 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
640 const char *setup_script
, char *ifname
,
641 size_t ifname_sz
, int mq_required
, Error
**errp
)
644 int fd
, vnet_hdr_required
;
646 if (tap
->has_vnet_hdr
) {
647 *vnet_hdr
= tap
->vnet_hdr
;
648 vnet_hdr_required
= *vnet_hdr
;
651 vnet_hdr_required
= 0;
654 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
661 setup_script
[0] != '\0' &&
662 strcmp(setup_script
, "no") != 0) {
663 launch_script(setup_script
, ifname
, fd
, &err
);
665 error_propagate(errp
, err
);
674 #define MAX_TAP_QUEUES 1024
676 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
677 const char *model
, const char *name
,
678 const char *ifname
, const char *script
,
679 const char *downscript
, const char *vhostfdname
,
680 int vnet_hdr
, int fd
, Error
**errp
)
683 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
686 tap_set_sndbuf(s
->fd
, tap
, &err
);
688 error_propagate(errp
, err
);
692 if (tap
->has_fd
|| tap
->has_fds
) {
693 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
694 } else if (tap
->has_helper
) {
695 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
698 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
699 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
702 if (strcmp(downscript
, "no") != 0) {
703 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
704 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
709 if (tap
->has_vhost
? tap
->vhost
:
710 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
711 VhostNetOptions options
;
713 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
714 options
.net_backend
= &s
->nc
;
715 if (tap
->has_poll_us
) {
716 options
.busyloop_timeout
= tap
->poll_us
;
718 options
.busyloop_timeout
= 0;
722 vhostfd
= monitor_fd_param(monitor_cur(), vhostfdname
, &err
);
724 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
725 error_propagate(errp
, err
);
727 warn_report_err(err
);
731 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
732 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
737 vhostfd
= open("/dev/vhost-net", O_RDWR
);
739 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
740 error_setg_errno(errp
, errno
,
741 "tap: open vhost char device failed");
743 warn_report("tap: open vhost char device failed: %s",
748 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
749 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
753 options
.opaque
= (void *)(uintptr_t)vhostfd
;
756 s
->vhost_net
= vhost_net_init(&options
);
758 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
759 error_setg(errp
, VHOST_NET_INIT_FAILED
);
761 warn_report(VHOST_NET_INIT_FAILED
);
765 } else if (vhostfdname
) {
766 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
770 static int get_fds(char *str
, char *fds
[], int max
)
772 char *ptr
= str
, *this;
773 size_t len
= strlen(str
);
776 while (i
< max
&& ptr
< str
+ len
) {
777 this = strchr(ptr
, ':');
780 fds
[i
] = g_strdup(ptr
);
782 fds
[i
] = g_strndup(ptr
, this - ptr
);
796 int net_init_tap(const Netdev
*netdev
, const char *name
,
797 NetClientState
*peer
, Error
**errp
)
799 const NetdevTapOptions
*tap
;
800 int fd
, vnet_hdr
= 0, i
= 0, queues
;
801 /* for the no-fd, no-helper case */
803 const char *downscript
;
805 const char *vhostfdname
;
809 assert(netdev
->type
== NET_CLIENT_DRIVER_TAP
);
810 tap
= &netdev
->u
.tap
;
811 queues
= tap
->has_queues
? tap
->queues
: 1;
812 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
813 script
= tap
->has_script
? tap
->script
: NULL
;
814 downscript
= tap
->has_downscript
? tap
->downscript
: NULL
;
816 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
817 * For -netdev, peer is always NULL. */
818 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
819 error_setg(errp
, "Multiqueue tap cannot be used with hubs");
824 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
825 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
826 tap
->has_fds
|| tap
->has_vhostfds
) {
827 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
828 "helper=, queues=, fds=, and vhostfds= "
829 "are invalid with fd=");
833 fd
= monitor_fd_param(monitor_cur(), tap
->fd
, errp
);
838 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
839 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
845 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
851 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
853 vhostfdname
, vnet_hdr
, fd
, &err
);
855 error_propagate(errp
, err
);
859 } else if (tap
->has_fds
) {
862 int nfds
= 0, nvhosts
= 0;
864 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
865 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
867 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
868 "helper=, queues=, and vhostfd= "
869 "are invalid with fds=");
873 fds
= g_new0(char *, MAX_TAP_QUEUES
);
874 vhost_fds
= g_new0(char *, MAX_TAP_QUEUES
);
876 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
877 if (tap
->has_vhostfds
) {
878 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
879 if (nfds
!= nvhosts
) {
880 error_setg(errp
, "The number of fds passed does not match "
881 "the number of vhostfds passed");
887 for (i
= 0; i
< nfds
; i
++) {
888 fd
= monitor_fd_param(monitor_cur(), fds
[i
], errp
);
894 ret
= g_unix_set_fd_nonblocking(fd
, true, NULL
);
896 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
902 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
907 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
, NULL
)) {
909 "vnet_hdr not consistent across given tap fds");
914 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
916 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
919 error_propagate(errp
, err
);
926 for (i
= 0; i
< nvhosts
; i
++) {
927 g_free(vhost_fds
[i
]);
929 for (i
= 0; i
< nfds
; i
++) {
935 } else if (tap
->has_helper
) {
936 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
937 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
938 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
939 "queues=, and vhostfds= are invalid with helper=");
943 fd
= net_bridge_run_helper(tap
->helper
,
945 tap
->br
: DEFAULT_BRIDGE_INTERFACE
,
951 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
952 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
955 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
961 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
962 script
, downscript
, vhostfdname
,
965 error_propagate(errp
, err
);
970 g_autofree
char *default_script
= NULL
;
971 g_autofree
char *default_downscript
= NULL
;
972 if (tap
->has_vhostfds
) {
973 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
978 script
= default_script
= get_relocated_path(DEFAULT_NETWORK_SCRIPT
);
981 downscript
= default_downscript
=
982 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT
);
985 if (tap
->has_ifname
) {
986 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
991 for (i
= 0; i
< queues
; i
++) {
992 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
993 ifname
, sizeof ifname
, queues
> 1, errp
);
998 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
999 if (tap_fd_get_ifname(fd
, ifname
)) {
1000 error_setg(errp
, "Fail to get ifname");
1006 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
1007 i
>= 1 ? "no" : script
,
1008 i
>= 1 ? "no" : downscript
,
1009 vhostfdname
, vnet_hdr
, fd
, &err
);
1011 error_propagate(errp
, err
);
1021 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
1023 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1024 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
1025 return s
->vhost_net
;
1028 int tap_enable(NetClientState
*nc
)
1030 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1036 ret
= tap_fd_enable(s
->fd
);
1039 tap_update_fd_handler(s
);
1045 int tap_disable(NetClientState
*nc
)
1047 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1050 if (s
->enabled
== 0) {
1053 ret
= tap_fd_disable(s
->fd
);
1055 qemu_purge_queued_packets(nc
);
1057 tap_update_fd_handler(s
);