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
)
105 len
= RETRY_ON_EINTR(writev(s
->fd
, iov
, iovcnt
));
107 if (len
== -1 && errno
== EAGAIN
) {
108 tap_write_poll(s
, true);
115 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
118 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
119 const struct iovec
*iovp
= iov
;
120 struct iovec iov_copy
[iovcnt
+ 1];
121 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
123 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
124 iov_copy
[0].iov_base
= &hdr
;
125 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
126 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
131 return tap_write_packet(s
, iovp
, iovcnt
);
134 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
136 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
139 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
141 if (s
->host_vnet_hdr_len
) {
142 iov
[iovcnt
].iov_base
= &hdr
;
143 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
147 iov
[iovcnt
].iov_base
= (char *)buf
;
148 iov
[iovcnt
].iov_len
= size
;
151 return tap_write_packet(s
, iov
, iovcnt
);
154 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
156 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
159 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
160 return tap_receive_raw(nc
, buf
, size
);
163 iov
[0].iov_base
= (char *)buf
;
164 iov
[0].iov_len
= size
;
166 return tap_write_packet(s
, iov
, 1);
170 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
172 return read(tapfd
, buf
, maxlen
);
176 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
178 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
179 tap_read_poll(s
, true);
182 static void tap_send(void *opaque
)
184 TAPState
*s
= opaque
;
189 uint8_t *buf
= s
->buf
;
190 uint8_t min_pkt
[ETH_ZLEN
];
191 size_t min_pktsz
= sizeof(min_pkt
);
193 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
198 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
199 buf
+= s
->host_vnet_hdr_len
;
200 size
-= s
->host_vnet_hdr_len
;
203 if (net_peer_needs_padding(&s
->nc
)) {
204 if (eth_pad_short_frame(min_pkt
, &min_pktsz
, buf
, size
)) {
210 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
212 tap_read_poll(s
, false);
214 } else if (size
< 0) {
219 * When the host keeps receiving more packets while tap_send() is
220 * running we can hog the QEMU global mutex. Limit the number of
221 * packets that are processed per tap_send() callback to prevent
222 * stalling the guest.
231 static bool tap_has_ufo(NetClientState
*nc
)
233 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
235 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
240 static bool tap_has_vnet_hdr(NetClientState
*nc
)
242 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
244 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
246 return !!s
->host_vnet_hdr_len
;
249 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
251 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
253 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
255 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
258 static int tap_get_vnet_hdr_len(NetClientState
*nc
)
260 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
262 return s
->host_vnet_hdr_len
;
265 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
267 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
269 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
270 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
271 len
== sizeof(struct virtio_net_hdr
) ||
272 len
== sizeof(struct virtio_net_hdr_v1_hash
));
274 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
275 s
->host_vnet_hdr_len
= len
;
278 static bool tap_get_using_vnet_hdr(NetClientState
*nc
)
280 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
282 return s
->using_vnet_hdr
;
285 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
287 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
289 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
290 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
292 s
->using_vnet_hdr
= using_vnet_hdr
;
295 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
297 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
299 return tap_fd_set_vnet_le(s
->fd
, is_le
);
302 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
304 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
306 return tap_fd_set_vnet_be(s
->fd
, is_be
);
309 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
310 int tso6
, int ecn
, int ufo
)
312 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
317 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
320 static void tap_exit_notify(Notifier
*notifier
, void *data
)
322 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
325 if (s
->down_script
[0]) {
326 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
328 error_report_err(err
);
333 static void tap_cleanup(NetClientState
*nc
)
335 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
338 vhost_net_cleanup(s
->vhost_net
);
339 g_free(s
->vhost_net
);
343 qemu_purge_queued_packets(nc
);
345 tap_exit_notify(&s
->exit
, NULL
);
346 qemu_remove_exit_notifier(&s
->exit
);
348 tap_read_poll(s
, false);
349 tap_write_poll(s
, false);
354 static void tap_poll(NetClientState
*nc
, bool enable
)
356 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
357 tap_read_poll(s
, enable
);
358 tap_write_poll(s
, enable
);
361 static bool tap_set_steering_ebpf(NetClientState
*nc
, int prog_fd
)
363 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
364 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
366 return tap_fd_set_steering_ebpf(s
->fd
, prog_fd
) == 0;
369 int tap_get_fd(NetClientState
*nc
)
371 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
372 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
378 static NetClientInfo net_tap_info
= {
379 .type
= NET_CLIENT_DRIVER_TAP
,
380 .size
= sizeof(TAPState
),
381 .receive
= tap_receive
,
382 .receive_raw
= tap_receive_raw
,
383 .receive_iov
= tap_receive_iov
,
385 .cleanup
= tap_cleanup
,
386 .has_ufo
= tap_has_ufo
,
387 .has_vnet_hdr
= tap_has_vnet_hdr
,
388 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
389 .get_using_vnet_hdr
= tap_get_using_vnet_hdr
,
390 .using_vnet_hdr
= tap_using_vnet_hdr
,
391 .set_offload
= tap_set_offload
,
392 .get_vnet_hdr_len
= tap_get_vnet_hdr_len
,
393 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
394 .set_vnet_le
= tap_set_vnet_le
,
395 .set_vnet_be
= tap_set_vnet_be
,
396 .set_steering_ebpf
= tap_set_steering_ebpf
,
399 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
408 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
410 s
= DO_UPCAST(TAPState
, nc
, nc
);
413 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
414 s
->using_vnet_hdr
= false;
415 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
417 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
419 * Make sure host header length is set correctly in tap:
420 * it might have been modified by another instance of qemu.
422 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
423 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
425 tap_read_poll(s
, true);
428 s
->exit
.notify
= tap_exit_notify
;
429 qemu_add_exit_notifier(&s
->exit
);
434 static void launch_script(const char *setup_script
, const char *ifname
,
435 int fd
, Error
**errp
)
441 /* try to launch network script */
444 error_setg_errno(errp
, errno
, "could not launch network script %s",
449 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
451 for (i
= 3; i
< open_max
; i
++) {
457 *parg
++ = (char *)setup_script
;
458 *parg
++ = (char *)ifname
;
460 execv(setup_script
, args
);
463 while (waitpid(pid
, &status
, 0) != pid
) {
467 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
470 error_setg(errp
, "network script %s failed with status %d",
471 setup_script
, status
);
475 static int recv_fd(int c
)
478 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
479 struct msghdr msg
= {
480 .msg_control
= msgbuf
,
481 .msg_controllen
= sizeof(msgbuf
),
483 struct cmsghdr
*cmsg
;
488 cmsg
= CMSG_FIRSTHDR(&msg
);
489 cmsg
->cmsg_level
= SOL_SOCKET
;
490 cmsg
->cmsg_type
= SCM_RIGHTS
;
491 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
492 msg
.msg_controllen
= cmsg
->cmsg_len
;
495 iov
.iov_len
= sizeof(req
);
500 len
= recvmsg(c
, &msg
, 0);
502 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
509 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
512 sigset_t oldmask
, mask
;
513 g_autofree
char *default_helper
= NULL
;
520 sigaddset(&mask
, SIGCHLD
);
521 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
524 helper
= default_helper
= get_relocated_path(DEFAULT_BRIDGE_HELPER
);
527 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
528 error_setg_errno(errp
, errno
, "socketpair() failed");
532 /* try to launch bridge helper */
535 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
539 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
542 char *helper_cmd
= NULL
;
544 for (i
= 3; i
< open_max
; i
++) {
550 fd_buf
= g_strdup_printf("%s%d", "--fd=", sv
[1]);
552 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
553 /* assume helper is a command */
555 if (strstr(helper
, "--br=") == NULL
) {
556 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
559 helper_cmd
= g_strdup_printf("%s %s %s %s", helper
,
560 "--use-vnet", fd_buf
, br_buf
? br_buf
: "");
563 *parg
++ = (char *)"sh";
564 *parg
++ = (char *)"-c";
565 *parg
++ = helper_cmd
;
568 execv("/bin/sh", args
);
571 /* assume helper is just the executable path name */
573 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
576 *parg
++ = (char *)helper
;
577 *parg
++ = (char *)"--use-vnet";
594 fd
= RETRY_ON_EINTR(recv_fd(sv
[0]));
599 while (waitpid(pid
, &status
, 0) != pid
) {
602 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
604 error_setg_errno(errp
, saved_errno
,
605 "failed to recv file descriptor");
608 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
609 error_setg(errp
, "bridge helper failed");
616 int net_init_bridge(const Netdev
*netdev
, const char *name
,
617 NetClientState
*peer
, Error
**errp
)
619 const NetdevBridgeOptions
*bridge
;
620 const char *helper
, *br
;
624 assert(netdev
->type
== NET_CLIENT_DRIVER_BRIDGE
);
625 bridge
= &netdev
->u
.bridge
;
626 helper
= bridge
->helper
;
627 br
= bridge
->br
?: DEFAULT_BRIDGE_INTERFACE
;
629 fd
= net_bridge_run_helper(helper
, br
, errp
);
634 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
635 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
638 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
643 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
645 qemu_set_info_str(&s
->nc
, "helper=%s,br=%s", helper
, br
);
650 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
651 const char *setup_script
, char *ifname
,
652 size_t ifname_sz
, int mq_required
, Error
**errp
)
655 int fd
, vnet_hdr_required
;
657 if (tap
->has_vnet_hdr
) {
658 *vnet_hdr
= tap
->vnet_hdr
;
659 vnet_hdr_required
= *vnet_hdr
;
662 vnet_hdr_required
= 0;
665 fd
= RETRY_ON_EINTR(tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
672 setup_script
[0] != '\0' &&
673 strcmp(setup_script
, "no") != 0) {
674 launch_script(setup_script
, ifname
, fd
, &err
);
676 error_propagate(errp
, err
);
685 #define MAX_TAP_QUEUES 1024
687 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
688 const char *model
, const char *name
,
689 const char *ifname
, const char *script
,
690 const char *downscript
, const char *vhostfdname
,
691 int vnet_hdr
, int fd
, Error
**errp
)
694 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
697 tap_set_sndbuf(s
->fd
, tap
, &err
);
699 error_propagate(errp
, err
);
703 if (tap
->fd
|| tap
->fds
) {
704 qemu_set_info_str(&s
->nc
, "fd=%d", fd
);
705 } else if (tap
->helper
) {
706 qemu_set_info_str(&s
->nc
, "helper=%s", tap
->helper
);
708 qemu_set_info_str(&s
->nc
, "ifname=%s,script=%s,downscript=%s", ifname
,
711 if (strcmp(downscript
, "no") != 0) {
712 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
713 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
718 if (tap
->has_vhost
? tap
->vhost
:
719 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
720 VhostNetOptions options
;
722 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
723 options
.net_backend
= &s
->nc
;
724 if (tap
->has_poll_us
) {
725 options
.busyloop_timeout
= tap
->poll_us
;
727 options
.busyloop_timeout
= 0;
731 vhostfd
= monitor_fd_param(monitor_cur(), vhostfdname
, &err
);
733 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
734 error_propagate(errp
, err
);
736 warn_report_err(err
);
740 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
741 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
746 vhostfd
= open("/dev/vhost-net", O_RDWR
);
748 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
749 error_setg_errno(errp
, errno
,
750 "tap: open vhost char device failed");
752 warn_report("tap: open vhost char device failed: %s",
757 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
758 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
762 options
.opaque
= (void *)(uintptr_t)vhostfd
;
765 s
->vhost_net
= vhost_net_init(&options
);
767 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
768 error_setg(errp
, VHOST_NET_INIT_FAILED
);
770 warn_report(VHOST_NET_INIT_FAILED
);
774 } else if (vhostfdname
) {
775 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
782 qemu_del_net_client(&s
->nc
);
785 static int get_fds(char *str
, char *fds
[], int max
)
787 char *ptr
= str
, *this;
788 size_t len
= strlen(str
);
791 while (i
< max
&& ptr
< str
+ len
) {
792 this = strchr(ptr
, ':');
795 fds
[i
] = g_strdup(ptr
);
797 fds
[i
] = g_strndup(ptr
, this - ptr
);
811 int net_init_tap(const Netdev
*netdev
, const char *name
,
812 NetClientState
*peer
, Error
**errp
)
814 const NetdevTapOptions
*tap
;
815 int fd
, vnet_hdr
= 0, i
= 0, queues
;
816 /* for the no-fd, no-helper case */
818 const char *downscript
;
820 const char *vhostfdname
;
824 assert(netdev
->type
== NET_CLIENT_DRIVER_TAP
);
825 tap
= &netdev
->u
.tap
;
826 queues
= tap
->has_queues
? tap
->queues
: 1;
827 vhostfdname
= tap
->vhostfd
;
828 script
= tap
->script
;
829 downscript
= tap
->downscript
;
831 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
832 * For -netdev, peer is always NULL. */
833 if (peer
&& (tap
->has_queues
|| tap
->fds
|| tap
->vhostfds
)) {
834 error_setg(errp
, "Multiqueue tap cannot be used with hubs");
839 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
840 tap
->has_vnet_hdr
|| tap
->helper
|| tap
->has_queues
||
841 tap
->fds
|| tap
->vhostfds
) {
842 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
843 "helper=, queues=, fds=, and vhostfds= "
844 "are invalid with fd=");
848 fd
= monitor_fd_param(monitor_cur(), tap
->fd
, errp
);
853 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
854 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
860 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
866 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
868 vhostfdname
, vnet_hdr
, fd
, &err
);
870 error_propagate(errp
, err
);
874 } else if (tap
->fds
) {
877 int nfds
= 0, nvhosts
= 0;
879 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
880 tap
->has_vnet_hdr
|| tap
->helper
|| tap
->has_queues
||
882 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
883 "helper=, queues=, and vhostfd= "
884 "are invalid with fds=");
888 fds
= g_new0(char *, MAX_TAP_QUEUES
);
889 vhost_fds
= g_new0(char *, MAX_TAP_QUEUES
);
891 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
893 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
894 if (nfds
!= nvhosts
) {
895 error_setg(errp
, "The number of fds passed does not match "
896 "the number of vhostfds passed");
902 for (i
= 0; i
< nfds
; i
++) {
903 fd
= monitor_fd_param(monitor_cur(), fds
[i
], errp
);
909 ret
= g_unix_set_fd_nonblocking(fd
, true, NULL
);
911 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
917 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
922 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
, NULL
)) {
924 "vnet_hdr not consistent across given tap fds");
929 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
931 tap
->vhostfds
? vhost_fds
[i
] : NULL
,
934 error_propagate(errp
, err
);
941 for (i
= 0; i
< nvhosts
; i
++) {
942 g_free(vhost_fds
[i
]);
944 for (i
= 0; i
< nfds
; i
++) {
950 } else if (tap
->helper
) {
951 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
952 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->vhostfds
) {
953 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
954 "queues=, and vhostfds= are invalid with helper=");
958 fd
= net_bridge_run_helper(tap
->helper
,
959 tap
->br
?: DEFAULT_BRIDGE_INTERFACE
,
965 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
966 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
969 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
975 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
976 script
, downscript
, vhostfdname
,
979 error_propagate(errp
, err
);
984 g_autofree
char *default_script
= NULL
;
985 g_autofree
char *default_downscript
= NULL
;
987 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
992 script
= default_script
= get_relocated_path(DEFAULT_NETWORK_SCRIPT
);
995 downscript
= default_downscript
=
996 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT
);
1000 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
1005 for (i
= 0; i
< queues
; i
++) {
1006 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
1007 ifname
, sizeof ifname
, queues
> 1, errp
);
1012 if (queues
> 1 && i
== 0 && !tap
->ifname
) {
1013 if (tap_fd_get_ifname(fd
, ifname
)) {
1014 error_setg(errp
, "Fail to get ifname");
1020 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
1021 i
>= 1 ? "no" : script
,
1022 i
>= 1 ? "no" : downscript
,
1023 vhostfdname
, vnet_hdr
, fd
, &err
);
1025 error_propagate(errp
, err
);
1035 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
1037 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1038 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
1039 return s
->vhost_net
;
1042 int tap_enable(NetClientState
*nc
)
1044 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1050 ret
= tap_fd_enable(s
->fd
);
1053 tap_update_fd_handler(s
);
1059 int tap_disable(NetClientState
*nc
)
1061 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1064 if (s
->enabled
== 0) {
1067 ret
= tap_fd_disable(s
->fd
);
1069 qemu_purge_queued_packets(nc
);
1071 tap_update_fd_handler(s
);