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 void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
260 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
262 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
263 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
264 len
== sizeof(struct virtio_net_hdr
) ||
265 len
== sizeof(struct virtio_net_hdr_v1_hash
));
267 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
268 s
->host_vnet_hdr_len
= len
;
271 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
273 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
275 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
276 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
278 s
->using_vnet_hdr
= using_vnet_hdr
;
281 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
283 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
285 return tap_fd_set_vnet_le(s
->fd
, is_le
);
288 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
290 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
292 return tap_fd_set_vnet_be(s
->fd
, is_be
);
295 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
296 int tso6
, int ecn
, int ufo
)
298 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
303 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
306 static void tap_exit_notify(Notifier
*notifier
, void *data
)
308 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
311 if (s
->down_script
[0]) {
312 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
314 error_report_err(err
);
319 static void tap_cleanup(NetClientState
*nc
)
321 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
324 vhost_net_cleanup(s
->vhost_net
);
325 g_free(s
->vhost_net
);
329 qemu_purge_queued_packets(nc
);
331 tap_exit_notify(&s
->exit
, NULL
);
332 qemu_remove_exit_notifier(&s
->exit
);
334 tap_read_poll(s
, false);
335 tap_write_poll(s
, false);
340 static void tap_poll(NetClientState
*nc
, bool enable
)
342 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
343 tap_read_poll(s
, enable
);
344 tap_write_poll(s
, enable
);
347 static bool tap_set_steering_ebpf(NetClientState
*nc
, int prog_fd
)
349 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
350 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
352 return tap_fd_set_steering_ebpf(s
->fd
, prog_fd
) == 0;
355 int tap_get_fd(NetClientState
*nc
)
357 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
358 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
364 static NetClientInfo net_tap_info
= {
365 .type
= NET_CLIENT_DRIVER_TAP
,
366 .size
= sizeof(TAPState
),
367 .receive
= tap_receive
,
368 .receive_raw
= tap_receive_raw
,
369 .receive_iov
= tap_receive_iov
,
371 .cleanup
= tap_cleanup
,
372 .has_ufo
= tap_has_ufo
,
373 .has_vnet_hdr
= tap_has_vnet_hdr
,
374 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
375 .using_vnet_hdr
= tap_using_vnet_hdr
,
376 .set_offload
= tap_set_offload
,
377 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
378 .set_vnet_le
= tap_set_vnet_le
,
379 .set_vnet_be
= tap_set_vnet_be
,
380 .set_steering_ebpf
= tap_set_steering_ebpf
,
383 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
392 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
394 s
= DO_UPCAST(TAPState
, nc
, nc
);
397 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
398 s
->using_vnet_hdr
= false;
399 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
401 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
403 * Make sure host header length is set correctly in tap:
404 * it might have been modified by another instance of qemu.
406 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
407 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
409 tap_read_poll(s
, true);
412 s
->exit
.notify
= tap_exit_notify
;
413 qemu_add_exit_notifier(&s
->exit
);
418 static void launch_script(const char *setup_script
, const char *ifname
,
419 int fd
, Error
**errp
)
425 /* try to launch network script */
428 error_setg_errno(errp
, errno
, "could not launch network script %s",
433 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
435 for (i
= 3; i
< open_max
; i
++) {
441 *parg
++ = (char *)setup_script
;
442 *parg
++ = (char *)ifname
;
444 execv(setup_script
, args
);
447 while (waitpid(pid
, &status
, 0) != pid
) {
451 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
454 error_setg(errp
, "network script %s failed with status %d",
455 setup_script
, status
);
459 static int recv_fd(int c
)
462 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
463 struct msghdr msg
= {
464 .msg_control
= msgbuf
,
465 .msg_controllen
= sizeof(msgbuf
),
467 struct cmsghdr
*cmsg
;
472 cmsg
= CMSG_FIRSTHDR(&msg
);
473 cmsg
->cmsg_level
= SOL_SOCKET
;
474 cmsg
->cmsg_type
= SCM_RIGHTS
;
475 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
476 msg
.msg_controllen
= cmsg
->cmsg_len
;
479 iov
.iov_len
= sizeof(req
);
484 len
= recvmsg(c
, &msg
, 0);
486 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
493 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
496 sigset_t oldmask
, mask
;
497 g_autofree
char *default_helper
= NULL
;
504 sigaddset(&mask
, SIGCHLD
);
505 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
508 helper
= default_helper
= get_relocated_path(DEFAULT_BRIDGE_HELPER
);
511 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
512 error_setg_errno(errp
, errno
, "socketpair() failed");
516 /* try to launch bridge helper */
519 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
523 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
526 char *helper_cmd
= NULL
;
528 for (i
= 3; i
< open_max
; i
++) {
534 fd_buf
= g_strdup_printf("%s%d", "--fd=", sv
[1]);
536 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
537 /* assume helper is a command */
539 if (strstr(helper
, "--br=") == NULL
) {
540 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
543 helper_cmd
= g_strdup_printf("%s %s %s %s", helper
,
544 "--use-vnet", fd_buf
, br_buf
? br_buf
: "");
547 *parg
++ = (char *)"sh";
548 *parg
++ = (char *)"-c";
549 *parg
++ = helper_cmd
;
552 execv("/bin/sh", args
);
555 /* assume helper is just the executable path name */
557 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
560 *parg
++ = (char *)helper
;
561 *parg
++ = (char *)"--use-vnet";
578 fd
= RETRY_ON_EINTR(recv_fd(sv
[0]));
583 while (waitpid(pid
, &status
, 0) != pid
) {
586 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
588 error_setg_errno(errp
, saved_errno
,
589 "failed to recv file descriptor");
592 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
593 error_setg(errp
, "bridge helper failed");
600 int net_init_bridge(const Netdev
*netdev
, const char *name
,
601 NetClientState
*peer
, Error
**errp
)
603 const NetdevBridgeOptions
*bridge
;
604 const char *helper
, *br
;
608 assert(netdev
->type
== NET_CLIENT_DRIVER_BRIDGE
);
609 bridge
= &netdev
->u
.bridge
;
610 helper
= bridge
->helper
;
611 br
= bridge
->br
?: DEFAULT_BRIDGE_INTERFACE
;
613 fd
= net_bridge_run_helper(helper
, br
, errp
);
618 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
619 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
622 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
627 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
629 qemu_set_info_str(&s
->nc
, "helper=%s,br=%s", helper
, br
);
634 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
635 const char *setup_script
, char *ifname
,
636 size_t ifname_sz
, int mq_required
, Error
**errp
)
639 int fd
, vnet_hdr_required
;
641 if (tap
->has_vnet_hdr
) {
642 *vnet_hdr
= tap
->vnet_hdr
;
643 vnet_hdr_required
= *vnet_hdr
;
646 vnet_hdr_required
= 0;
649 fd
= RETRY_ON_EINTR(tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
656 setup_script
[0] != '\0' &&
657 strcmp(setup_script
, "no") != 0) {
658 launch_script(setup_script
, ifname
, fd
, &err
);
660 error_propagate(errp
, err
);
669 #define MAX_TAP_QUEUES 1024
671 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
672 const char *model
, const char *name
,
673 const char *ifname
, const char *script
,
674 const char *downscript
, const char *vhostfdname
,
675 int vnet_hdr
, int fd
, Error
**errp
)
678 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
681 tap_set_sndbuf(s
->fd
, tap
, &err
);
683 error_propagate(errp
, err
);
687 if (tap
->fd
|| tap
->fds
) {
688 qemu_set_info_str(&s
->nc
, "fd=%d", fd
);
689 } else if (tap
->helper
) {
690 qemu_set_info_str(&s
->nc
, "helper=%s", tap
->helper
);
692 qemu_set_info_str(&s
->nc
, "ifname=%s,script=%s,downscript=%s", ifname
,
695 if (strcmp(downscript
, "no") != 0) {
696 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
697 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
702 if (tap
->has_vhost
? tap
->vhost
:
703 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
704 VhostNetOptions options
;
706 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
707 options
.net_backend
= &s
->nc
;
708 if (tap
->has_poll_us
) {
709 options
.busyloop_timeout
= tap
->poll_us
;
711 options
.busyloop_timeout
= 0;
715 vhostfd
= monitor_fd_param(monitor_cur(), vhostfdname
, &err
);
717 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
718 error_propagate(errp
, err
);
720 warn_report_err(err
);
724 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
725 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
730 vhostfd
= open("/dev/vhost-net", O_RDWR
);
732 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
733 error_setg_errno(errp
, errno
,
734 "tap: open vhost char device failed");
736 warn_report("tap: open vhost char device failed: %s",
741 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
742 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
746 options
.opaque
= (void *)(uintptr_t)vhostfd
;
749 s
->vhost_net
= vhost_net_init(&options
);
751 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
752 error_setg(errp
, VHOST_NET_INIT_FAILED
);
754 warn_report(VHOST_NET_INIT_FAILED
);
758 } else if (vhostfdname
) {
759 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
766 qemu_del_net_client(&s
->nc
);
769 static int get_fds(char *str
, char *fds
[], int max
)
771 char *ptr
= str
, *this;
772 size_t len
= strlen(str
);
775 while (i
< max
&& ptr
< str
+ len
) {
776 this = strchr(ptr
, ':');
779 fds
[i
] = g_strdup(ptr
);
781 fds
[i
] = g_strndup(ptr
, this - ptr
);
795 int net_init_tap(const Netdev
*netdev
, const char *name
,
796 NetClientState
*peer
, Error
**errp
)
798 const NetdevTapOptions
*tap
;
799 int fd
, vnet_hdr
= 0, i
= 0, queues
;
800 /* for the no-fd, no-helper case */
802 const char *downscript
;
804 const char *vhostfdname
;
808 assert(netdev
->type
== NET_CLIENT_DRIVER_TAP
);
809 tap
= &netdev
->u
.tap
;
810 queues
= tap
->has_queues
? tap
->queues
: 1;
811 vhostfdname
= tap
->vhostfd
;
812 script
= tap
->script
;
813 downscript
= tap
->downscript
;
815 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
816 * For -netdev, peer is always NULL. */
817 if (peer
&& (tap
->has_queues
|| tap
->fds
|| tap
->vhostfds
)) {
818 error_setg(errp
, "Multiqueue tap cannot be used with hubs");
823 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
824 tap
->has_vnet_hdr
|| tap
->helper
|| tap
->has_queues
||
825 tap
->fds
|| tap
->vhostfds
) {
826 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
827 "helper=, queues=, fds=, and vhostfds= "
828 "are invalid with fd=");
832 fd
= monitor_fd_param(monitor_cur(), tap
->fd
, errp
);
837 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
838 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
844 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
850 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
852 vhostfdname
, vnet_hdr
, fd
, &err
);
854 error_propagate(errp
, err
);
858 } else if (tap
->fds
) {
861 int nfds
= 0, nvhosts
= 0;
863 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
864 tap
->has_vnet_hdr
|| tap
->helper
|| tap
->has_queues
||
866 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
867 "helper=, queues=, and vhostfd= "
868 "are invalid with fds=");
872 fds
= g_new0(char *, MAX_TAP_QUEUES
);
873 vhost_fds
= g_new0(char *, MAX_TAP_QUEUES
);
875 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
877 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
878 if (nfds
!= nvhosts
) {
879 error_setg(errp
, "The number of fds passed does not match "
880 "the number of vhostfds passed");
886 for (i
= 0; i
< nfds
; i
++) {
887 fd
= monitor_fd_param(monitor_cur(), fds
[i
], errp
);
893 ret
= g_unix_set_fd_nonblocking(fd
, true, NULL
);
895 error_setg_errno(errp
, errno
, "%s: Can't use file descriptor %d",
901 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
906 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
, NULL
)) {
908 "vnet_hdr not consistent across given tap fds");
913 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
915 tap
->vhostfds
? vhost_fds
[i
] : NULL
,
918 error_propagate(errp
, err
);
925 for (i
= 0; i
< nvhosts
; i
++) {
926 g_free(vhost_fds
[i
]);
928 for (i
= 0; i
< nfds
; i
++) {
934 } else if (tap
->helper
) {
935 if (tap
->ifname
|| tap
->script
|| tap
->downscript
||
936 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->vhostfds
) {
937 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
938 "queues=, and vhostfds= are invalid with helper=");
942 fd
= net_bridge_run_helper(tap
->helper
,
943 tap
->br
?: DEFAULT_BRIDGE_INTERFACE
,
949 if (!g_unix_set_fd_nonblocking(fd
, true, NULL
)) {
950 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
953 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
959 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
960 script
, downscript
, vhostfdname
,
963 error_propagate(errp
, err
);
968 g_autofree
char *default_script
= NULL
;
969 g_autofree
char *default_downscript
= NULL
;
971 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
976 script
= default_script
= get_relocated_path(DEFAULT_NETWORK_SCRIPT
);
979 downscript
= default_downscript
=
980 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT
);
984 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
989 for (i
= 0; i
< queues
; i
++) {
990 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
991 ifname
, sizeof ifname
, queues
> 1, errp
);
996 if (queues
> 1 && i
== 0 && !tap
->ifname
) {
997 if (tap_fd_get_ifname(fd
, ifname
)) {
998 error_setg(errp
, "Fail to get ifname");
1004 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
1005 i
>= 1 ? "no" : script
,
1006 i
>= 1 ? "no" : downscript
,
1007 vhostfdname
, vnet_hdr
, fd
, &err
);
1009 error_propagate(errp
, err
);
1019 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
1021 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1022 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
1023 return s
->vhost_net
;
1026 int tap_enable(NetClientState
*nc
)
1028 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1034 ret
= tap_fd_enable(s
->fd
);
1037 tap_update_fd_handler(s
);
1043 int tap_disable(NetClientState
*nc
)
1045 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1048 if (s
->enabled
== 0) {
1051 ret
= tap_fd_disable(s
->fd
);
1053 qemu_purge_queued_packets(nc
);
1055 tap_update_fd_handler(s
);