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>
37 #include "monitor/monitor.h"
38 #include "sysemu/sysemu.h"
39 #include "qapi/error.h"
40 #include "qemu-common.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
;
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 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
205 tap_read_poll(s
, false);
207 } else if (size
< 0) {
212 * When the host keeps receiving more packets while tap_send() is
213 * running we can hog the QEMU global mutex. Limit the number of
214 * packets that are processed per tap_send() callback to prevent
215 * stalling the guest.
224 static bool tap_has_ufo(NetClientState
*nc
)
226 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
228 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
233 static bool tap_has_vnet_hdr(NetClientState
*nc
)
235 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
237 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
239 return !!s
->host_vnet_hdr_len
;
242 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
244 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
246 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
248 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
251 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
253 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
255 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
256 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
257 len
== sizeof(struct virtio_net_hdr
) ||
258 len
== sizeof(struct virtio_net_hdr_v1_hash
));
260 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
261 s
->host_vnet_hdr_len
= len
;
264 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
266 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
268 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
269 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
271 s
->using_vnet_hdr
= using_vnet_hdr
;
274 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
276 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
278 return tap_fd_set_vnet_le(s
->fd
, is_le
);
281 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
283 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
285 return tap_fd_set_vnet_be(s
->fd
, is_be
);
288 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
289 int tso6
, int ecn
, int ufo
)
291 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
296 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
299 static void tap_exit_notify(Notifier
*notifier
, void *data
)
301 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
304 if (s
->down_script
[0]) {
305 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
307 error_report_err(err
);
312 static void tap_cleanup(NetClientState
*nc
)
314 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
317 vhost_net_cleanup(s
->vhost_net
);
318 g_free(s
->vhost_net
);
322 qemu_purge_queued_packets(nc
);
324 tap_exit_notify(&s
->exit
, NULL
);
325 qemu_remove_exit_notifier(&s
->exit
);
327 tap_read_poll(s
, false);
328 tap_write_poll(s
, false);
333 static void tap_poll(NetClientState
*nc
, bool enable
)
335 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
336 tap_read_poll(s
, enable
);
337 tap_write_poll(s
, enable
);
340 int tap_get_fd(NetClientState
*nc
)
342 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
343 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
349 static NetClientInfo net_tap_info
= {
350 .type
= NET_CLIENT_DRIVER_TAP
,
351 .size
= sizeof(TAPState
),
352 .receive
= tap_receive
,
353 .receive_raw
= tap_receive_raw
,
354 .receive_iov
= tap_receive_iov
,
356 .cleanup
= tap_cleanup
,
357 .has_ufo
= tap_has_ufo
,
358 .has_vnet_hdr
= tap_has_vnet_hdr
,
359 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
360 .using_vnet_hdr
= tap_using_vnet_hdr
,
361 .set_offload
= tap_set_offload
,
362 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
363 .set_vnet_le
= tap_set_vnet_le
,
364 .set_vnet_be
= tap_set_vnet_be
,
367 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
376 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
378 s
= DO_UPCAST(TAPState
, nc
, nc
);
381 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
382 s
->using_vnet_hdr
= false;
383 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
385 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
387 * Make sure host header length is set correctly in tap:
388 * it might have been modified by another instance of qemu.
390 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
391 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
393 tap_read_poll(s
, true);
396 s
->exit
.notify
= tap_exit_notify
;
397 qemu_add_exit_notifier(&s
->exit
);
402 static void launch_script(const char *setup_script
, const char *ifname
,
403 int fd
, Error
**errp
)
409 /* try to launch network script */
412 error_setg_errno(errp
, errno
, "could not launch network script %s",
417 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
419 for (i
= 3; i
< open_max
; i
++) {
425 *parg
++ = (char *)setup_script
;
426 *parg
++ = (char *)ifname
;
428 execv(setup_script
, args
);
431 while (waitpid(pid
, &status
, 0) != pid
) {
435 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
438 error_setg(errp
, "network script %s failed with status %d",
439 setup_script
, status
);
443 static int recv_fd(int c
)
446 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
447 struct msghdr msg
= {
448 .msg_control
= msgbuf
,
449 .msg_controllen
= sizeof(msgbuf
),
451 struct cmsghdr
*cmsg
;
456 cmsg
= CMSG_FIRSTHDR(&msg
);
457 cmsg
->cmsg_level
= SOL_SOCKET
;
458 cmsg
->cmsg_type
= SCM_RIGHTS
;
459 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
460 msg
.msg_controllen
= cmsg
->cmsg_len
;
463 iov
.iov_len
= sizeof(req
);
468 len
= recvmsg(c
, &msg
, 0);
470 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
477 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
480 sigset_t oldmask
, mask
;
481 g_autofree
char *default_helper
= NULL
;
488 sigaddset(&mask
, SIGCHLD
);
489 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
492 helper
= default_helper
= get_relocated_path(DEFAULT_BRIDGE_HELPER
);
495 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
496 error_setg_errno(errp
, errno
, "socketpair() failed");
500 /* try to launch bridge helper */
503 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
507 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
510 char *helper_cmd
= NULL
;
512 for (i
= 3; i
< open_max
; i
++) {
518 fd_buf
= g_strdup_printf("%s%d", "--fd=", sv
[1]);
520 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
521 /* assume helper is a command */
523 if (strstr(helper
, "--br=") == NULL
) {
524 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
527 helper_cmd
= g_strdup_printf("%s %s %s %s", helper
,
528 "--use-vnet", fd_buf
, br_buf
? br_buf
: "");
531 *parg
++ = (char *)"sh";
532 *parg
++ = (char *)"-c";
533 *parg
++ = helper_cmd
;
536 execv("/bin/sh", args
);
539 /* assume helper is just the executable path name */
541 br_buf
= g_strdup_printf("%s%s", "--br=", bridge
);
544 *parg
++ = (char *)helper
;
545 *parg
++ = (char *)"--use-vnet";
564 } while (fd
== -1 && errno
== EINTR
);
569 while (waitpid(pid
, &status
, 0) != pid
) {
572 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
574 error_setg_errno(errp
, saved_errno
,
575 "failed to recv file descriptor");
578 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
579 error_setg(errp
, "bridge helper failed");
586 int net_init_bridge(const Netdev
*netdev
, const char *name
,
587 NetClientState
*peer
, Error
**errp
)
589 const NetdevBridgeOptions
*bridge
;
590 const char *helper
, *br
;
594 assert(netdev
->type
== NET_CLIENT_DRIVER_BRIDGE
);
595 bridge
= &netdev
->u
.bridge
;
596 helper
= bridge
->has_helper
? bridge
->helper
: NULL
;
597 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
599 fd
= net_bridge_run_helper(helper
, br
, errp
);
604 qemu_set_nonblock(fd
);
605 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
610 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
612 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
618 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
619 const char *setup_script
, char *ifname
,
620 size_t ifname_sz
, int mq_required
, Error
**errp
)
623 int fd
, vnet_hdr_required
;
625 if (tap
->has_vnet_hdr
) {
626 *vnet_hdr
= tap
->vnet_hdr
;
627 vnet_hdr_required
= *vnet_hdr
;
630 vnet_hdr_required
= 0;
633 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
640 setup_script
[0] != '\0' &&
641 strcmp(setup_script
, "no") != 0) {
642 launch_script(setup_script
, ifname
, fd
, &err
);
644 error_propagate(errp
, err
);
653 #define MAX_TAP_QUEUES 1024
655 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
656 const char *model
, const char *name
,
657 const char *ifname
, const char *script
,
658 const char *downscript
, const char *vhostfdname
,
659 int vnet_hdr
, int fd
, Error
**errp
)
662 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
665 tap_set_sndbuf(s
->fd
, tap
, &err
);
667 error_propagate(errp
, err
);
671 if (tap
->has_fd
|| tap
->has_fds
) {
672 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
673 } else if (tap
->has_helper
) {
674 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
677 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
678 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
681 if (strcmp(downscript
, "no") != 0) {
682 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
683 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
688 if (tap
->has_vhost
? tap
->vhost
:
689 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
690 VhostNetOptions options
;
692 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
693 options
.net_backend
= &s
->nc
;
694 if (tap
->has_poll_us
) {
695 options
.busyloop_timeout
= tap
->poll_us
;
697 options
.busyloop_timeout
= 0;
703 vhostfd
= monitor_fd_param(monitor_cur(), vhostfdname
, &err
);
705 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
706 error_propagate(errp
, err
);
708 warn_report_err(err
);
712 ret
= qemu_try_set_nonblock(vhostfd
);
714 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
719 vhostfd
= open("/dev/vhost-net", O_RDWR
);
721 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
722 error_setg_errno(errp
, errno
,
723 "tap: open vhost char device failed");
725 warn_report("tap: open vhost char device failed: %s",
730 qemu_set_nonblock(vhostfd
);
732 options
.opaque
= (void *)(uintptr_t)vhostfd
;
734 s
->vhost_net
= vhost_net_init(&options
);
736 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
737 error_setg(errp
, VHOST_NET_INIT_FAILED
);
739 warn_report(VHOST_NET_INIT_FAILED
);
743 } else if (vhostfdname
) {
744 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
748 static int get_fds(char *str
, char *fds
[], int max
)
750 char *ptr
= str
, *this;
751 size_t len
= strlen(str
);
754 while (i
< max
&& ptr
< str
+ len
) {
755 this = strchr(ptr
, ':');
758 fds
[i
] = g_strdup(ptr
);
760 fds
[i
] = g_strndup(ptr
, this - ptr
);
774 int net_init_tap(const Netdev
*netdev
, const char *name
,
775 NetClientState
*peer
, Error
**errp
)
777 const NetdevTapOptions
*tap
;
778 int fd
, vnet_hdr
= 0, i
= 0, queues
;
779 /* for the no-fd, no-helper case */
781 const char *downscript
;
783 const char *vhostfdname
;
787 assert(netdev
->type
== NET_CLIENT_DRIVER_TAP
);
788 tap
= &netdev
->u
.tap
;
789 queues
= tap
->has_queues
? tap
->queues
: 1;
790 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
791 script
= tap
->has_script
? tap
->script
: NULL
;
792 downscript
= tap
->has_downscript
? tap
->downscript
: NULL
;
794 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
795 * For -netdev, peer is always NULL. */
796 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
797 error_setg(errp
, "Multiqueue tap cannot be used with hubs");
802 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
803 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
804 tap
->has_fds
|| tap
->has_vhostfds
) {
805 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
806 "helper=, queues=, fds=, and vhostfds= "
807 "are invalid with fd=");
811 fd
= monitor_fd_param(monitor_cur(), tap
->fd
, errp
);
816 ret
= qemu_try_set_nonblock(fd
);
818 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
824 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
830 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
832 vhostfdname
, vnet_hdr
, fd
, &err
);
834 error_propagate(errp
, err
);
838 } else if (tap
->has_fds
) {
841 int nfds
= 0, nvhosts
= 0;
843 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
844 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
846 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
847 "helper=, queues=, and vhostfd= "
848 "are invalid with fds=");
852 fds
= g_new0(char *, MAX_TAP_QUEUES
);
853 vhost_fds
= g_new0(char *, MAX_TAP_QUEUES
);
855 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
856 if (tap
->has_vhostfds
) {
857 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
858 if (nfds
!= nvhosts
) {
859 error_setg(errp
, "The number of fds passed does not match "
860 "the number of vhostfds passed");
866 for (i
= 0; i
< nfds
; i
++) {
867 fd
= monitor_fd_param(monitor_cur(), fds
[i
], errp
);
873 ret
= qemu_try_set_nonblock(fd
);
875 error_setg_errno(errp
, -ret
, "%s: Can't use file descriptor %d",
881 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
885 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
, NULL
)) {
887 "vnet_hdr not consistent across given tap fds");
892 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
894 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
897 error_propagate(errp
, err
);
904 for (i
= 0; i
< nvhosts
; i
++) {
905 g_free(vhost_fds
[i
]);
907 for (i
= 0; i
< nfds
; i
++) {
913 } else if (tap
->has_helper
) {
914 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
915 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
916 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
917 "queues=, and vhostfds= are invalid with helper=");
921 fd
= net_bridge_run_helper(tap
->helper
,
923 tap
->br
: DEFAULT_BRIDGE_INTERFACE
,
929 qemu_set_nonblock(fd
);
930 vnet_hdr
= tap_probe_vnet_hdr(fd
, errp
);
936 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
937 script
, downscript
, vhostfdname
,
940 error_propagate(errp
, err
);
945 g_autofree
char *default_script
= NULL
;
946 g_autofree
char *default_downscript
= NULL
;
947 if (tap
->has_vhostfds
) {
948 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
953 script
= default_script
= get_relocated_path(DEFAULT_NETWORK_SCRIPT
);
956 downscript
= default_downscript
=
957 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT
);
960 if (tap
->has_ifname
) {
961 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
966 for (i
= 0; i
< queues
; i
++) {
967 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
968 ifname
, sizeof ifname
, queues
> 1, errp
);
973 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
974 if (tap_fd_get_ifname(fd
, ifname
)) {
975 error_setg(errp
, "Fail to get ifname");
981 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
982 i
>= 1 ? "no" : script
,
983 i
>= 1 ? "no" : downscript
,
984 vhostfdname
, vnet_hdr
, fd
, &err
);
986 error_propagate(errp
, err
);
996 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
998 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
999 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
1000 return s
->vhost_net
;
1003 int tap_enable(NetClientState
*nc
)
1005 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1011 ret
= tap_fd_enable(s
->fd
);
1014 tap_update_fd_handler(s
);
1020 int tap_disable(NetClientState
*nc
)
1022 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
1025 if (s
->enabled
== 0) {
1028 ret
= tap_fd_disable(s
->fd
);
1030 qemu_purge_queued_packets(nc
);
1032 tap_update_fd_handler(s
);