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/sockets.h"
47 #include "net/vhost_net.h"
49 typedef struct TAPState
{
52 char down_script
[1024];
53 char down_script_arg
[128];
54 uint8_t buf
[NET_BUFSIZE
];
60 VHostNetState
*vhost_net
;
61 unsigned host_vnet_hdr_len
;
65 static void launch_script(const char *setup_script
, const char *ifname
,
66 int fd
, Error
**errp
);
68 static void tap_send(void *opaque
);
69 static void tap_writable(void *opaque
);
71 static void tap_update_fd_handler(TAPState
*s
)
73 qemu_set_fd_handler(s
->fd
,
74 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
75 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
79 static void tap_read_poll(TAPState
*s
, bool enable
)
81 s
->read_poll
= enable
;
82 tap_update_fd_handler(s
);
85 static void tap_write_poll(TAPState
*s
, bool enable
)
87 s
->write_poll
= enable
;
88 tap_update_fd_handler(s
);
91 static void tap_writable(void *opaque
)
95 tap_write_poll(s
, false);
97 qemu_flush_queued_packets(&s
->nc
);
100 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
105 len
= writev(s
->fd
, iov
, iovcnt
);
106 } while (len
== -1 && errno
== EINTR
);
108 if (len
== -1 && errno
== EAGAIN
) {
109 tap_write_poll(s
, true);
116 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
119 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
120 const struct iovec
*iovp
= iov
;
121 struct iovec iov_copy
[iovcnt
+ 1];
122 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
124 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
125 iov_copy
[0].iov_base
= &hdr
;
126 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
127 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
132 return tap_write_packet(s
, iovp
, iovcnt
);
135 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
137 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
140 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
142 if (s
->host_vnet_hdr_len
) {
143 iov
[iovcnt
].iov_base
= &hdr
;
144 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
148 iov
[iovcnt
].iov_base
= (char *)buf
;
149 iov
[iovcnt
].iov_len
= size
;
152 return tap_write_packet(s
, iov
, iovcnt
);
155 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
157 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
160 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
161 return tap_receive_raw(nc
, buf
, size
);
164 iov
[0].iov_base
= (char *)buf
;
165 iov
[0].iov_len
= size
;
167 return tap_write_packet(s
, iov
, 1);
171 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
173 return read(tapfd
, buf
, maxlen
);
177 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
179 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
180 tap_read_poll(s
, true);
183 static void tap_send(void *opaque
)
185 TAPState
*s
= opaque
;
190 uint8_t *buf
= s
->buf
;
192 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
197 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
198 buf
+= s
->host_vnet_hdr_len
;
199 size
-= s
->host_vnet_hdr_len
;
202 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
204 tap_read_poll(s
, false);
206 } else if (size
< 0) {
211 * When the host keeps receiving more packets while tap_send() is
212 * running we can hog the QEMU global mutex. Limit the number of
213 * packets that are processed per tap_send() callback to prevent
214 * stalling the guest.
223 static bool tap_has_ufo(NetClientState
*nc
)
225 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
227 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
232 static bool tap_has_vnet_hdr(NetClientState
*nc
)
234 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
236 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
238 return !!s
->host_vnet_hdr_len
;
241 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
243 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
245 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
247 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
250 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
252 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
254 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
255 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
256 len
== sizeof(struct virtio_net_hdr
));
258 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
259 s
->host_vnet_hdr_len
= len
;
262 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
264 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
266 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
267 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
269 s
->using_vnet_hdr
= using_vnet_hdr
;
272 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
274 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
276 return tap_fd_set_vnet_le(s
->fd
, is_le
);
279 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
281 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
283 return tap_fd_set_vnet_be(s
->fd
, is_be
);
286 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
287 int tso6
, int ecn
, int ufo
)
289 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
294 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
297 static void tap_exit_notify(Notifier
*notifier
, void *data
)
299 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
302 if (s
->down_script
[0]) {
303 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
305 error_report_err(err
);
310 static void tap_cleanup(NetClientState
*nc
)
312 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
315 vhost_net_cleanup(s
->vhost_net
);
316 g_free(s
->vhost_net
);
320 qemu_purge_queued_packets(nc
);
322 tap_exit_notify(&s
->exit
, NULL
);
323 qemu_remove_exit_notifier(&s
->exit
);
325 tap_read_poll(s
, false);
326 tap_write_poll(s
, false);
331 static void tap_poll(NetClientState
*nc
, bool enable
)
333 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
334 tap_read_poll(s
, enable
);
335 tap_write_poll(s
, enable
);
338 int tap_get_fd(NetClientState
*nc
)
340 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
341 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
347 static NetClientInfo net_tap_info
= {
348 .type
= NET_CLIENT_DRIVER_TAP
,
349 .size
= sizeof(TAPState
),
350 .receive
= tap_receive
,
351 .receive_raw
= tap_receive_raw
,
352 .receive_iov
= tap_receive_iov
,
354 .cleanup
= tap_cleanup
,
355 .has_ufo
= tap_has_ufo
,
356 .has_vnet_hdr
= tap_has_vnet_hdr
,
357 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
358 .using_vnet_hdr
= tap_using_vnet_hdr
,
359 .set_offload
= tap_set_offload
,
360 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
361 .set_vnet_le
= tap_set_vnet_le
,
362 .set_vnet_be
= tap_set_vnet_be
,
365 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
374 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
376 s
= DO_UPCAST(TAPState
, nc
, nc
);
379 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
380 s
->using_vnet_hdr
= false;
381 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
383 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
385 * Make sure host header length is set correctly in tap:
386 * it might have been modified by another instance of qemu.
388 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
389 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
391 tap_read_poll(s
, true);
394 s
->exit
.notify
= tap_exit_notify
;
395 qemu_add_exit_notifier(&s
->exit
);
400 static void launch_script(const char *setup_script
, const char *ifname
,
401 int fd
, Error
**errp
)
407 /* try to launch network script */
410 error_setg_errno(errp
, errno
, "could not launch network script %s",
415 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
417 for (i
= 3; i
< open_max
; i
++) {
423 *parg
++ = (char *)setup_script
;
424 *parg
++ = (char *)ifname
;
426 execv(setup_script
, args
);
429 while (waitpid(pid
, &status
, 0) != pid
) {
433 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
436 error_setg(errp
, "network script %s failed with status %d",
437 setup_script
, status
);
441 static int recv_fd(int c
)
444 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
445 struct msghdr msg
= {
446 .msg_control
= msgbuf
,
447 .msg_controllen
= sizeof(msgbuf
),
449 struct cmsghdr
*cmsg
;
454 cmsg
= CMSG_FIRSTHDR(&msg
);
455 cmsg
->cmsg_level
= SOL_SOCKET
;
456 cmsg
->cmsg_type
= SCM_RIGHTS
;
457 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
458 msg
.msg_controllen
= cmsg
->cmsg_len
;
461 iov
.iov_len
= sizeof(req
);
466 len
= recvmsg(c
, &msg
, 0);
468 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
475 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
478 sigset_t oldmask
, mask
;
485 sigaddset(&mask
, SIGCHLD
);
486 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
488 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
489 error_setg_errno(errp
, errno
, "socketpair() failed");
493 /* try to launch bridge helper */
496 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
500 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
502 char br_buf
[6+IFNAMSIZ
] = {0};
503 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
505 for (i
= 3; i
< open_max
; i
++) {
511 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
513 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
514 /* assume helper is a command */
516 if (strstr(helper
, "--br=") == NULL
) {
517 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
520 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
521 helper
, "--use-vnet", fd_buf
, br_buf
);
524 *parg
++ = (char *)"sh";
525 *parg
++ = (char *)"-c";
526 *parg
++ = helper_cmd
;
529 execv("/bin/sh", args
);
531 /* assume helper is just the executable path name */
533 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
536 *parg
++ = (char *)helper
;
537 *parg
++ = (char *)"--use-vnet";
554 } while (fd
== -1 && errno
== EINTR
);
559 while (waitpid(pid
, &status
, 0) != pid
) {
562 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
564 error_setg_errno(errp
, saved_errno
,
565 "failed to recv file descriptor");
568 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
569 error_setg(errp
, "bridge helper failed");
576 int net_init_bridge(const Netdev
*netdev
, const char *name
,
577 NetClientState
*peer
, Error
**errp
)
579 const NetdevBridgeOptions
*bridge
;
580 const char *helper
, *br
;
584 assert(netdev
->type
== NET_CLIENT_DRIVER_BRIDGE
);
585 bridge
= &netdev
->u
.bridge
;
587 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
588 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
590 fd
= net_bridge_run_helper(helper
, br
, errp
);
595 qemu_set_nonblock(fd
);
596 vnet_hdr
= tap_probe_vnet_hdr(fd
);
597 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
599 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
605 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
606 const char *setup_script
, char *ifname
,
607 size_t ifname_sz
, int mq_required
, Error
**errp
)
610 int fd
, vnet_hdr_required
;
612 if (tap
->has_vnet_hdr
) {
613 *vnet_hdr
= tap
->vnet_hdr
;
614 vnet_hdr_required
= *vnet_hdr
;
617 vnet_hdr_required
= 0;
620 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
627 setup_script
[0] != '\0' &&
628 strcmp(setup_script
, "no") != 0) {
629 launch_script(setup_script
, ifname
, fd
, &err
);
631 error_propagate(errp
, err
);
640 #define MAX_TAP_QUEUES 1024
642 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
643 const char *model
, const char *name
,
644 const char *ifname
, const char *script
,
645 const char *downscript
, const char *vhostfdname
,
646 int vnet_hdr
, int fd
, Error
**errp
)
649 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
652 tap_set_sndbuf(s
->fd
, tap
, &err
);
654 error_propagate(errp
, err
);
658 if (tap
->has_fd
|| tap
->has_fds
) {
659 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
660 } else if (tap
->has_helper
) {
661 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
664 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
665 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
668 if (strcmp(downscript
, "no") != 0) {
669 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
670 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
675 if (tap
->has_vhost
? tap
->vhost
:
676 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
677 VhostNetOptions options
;
679 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
680 options
.net_backend
= &s
->nc
;
681 if (tap
->has_poll_us
) {
682 options
.busyloop_timeout
= tap
->poll_us
;
684 options
.busyloop_timeout
= 0;
688 vhostfd
= monitor_fd_param(cur_mon
, vhostfdname
, &err
);
690 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
691 error_propagate(errp
, err
);
693 warn_report_err(err
);
697 qemu_set_nonblock(vhostfd
);
699 vhostfd
= open("/dev/vhost-net", O_RDWR
);
701 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
702 error_setg_errno(errp
, errno
,
703 "tap: open vhost char device failed");
705 warn_report("tap: open vhost char device failed: %s",
710 qemu_set_nonblock(vhostfd
);
712 options
.opaque
= (void *)(uintptr_t)vhostfd
;
714 s
->vhost_net
= vhost_net_init(&options
);
716 if (tap
->has_vhostforce
&& tap
->vhostforce
) {
717 error_setg(errp
, VHOST_NET_INIT_FAILED
);
719 warn_report(VHOST_NET_INIT_FAILED
);
723 } else if (vhostfdname
) {
724 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
728 static int get_fds(char *str
, char *fds
[], int max
)
730 char *ptr
= str
, *this;
731 size_t len
= strlen(str
);
734 while (i
< max
&& ptr
< str
+ len
) {
735 this = strchr(ptr
, ':');
738 fds
[i
] = g_strdup(ptr
);
740 fds
[i
] = g_strndup(ptr
, this - ptr
);
754 int net_init_tap(const Netdev
*netdev
, const char *name
,
755 NetClientState
*peer
, Error
**errp
)
757 const NetdevTapOptions
*tap
;
758 int fd
, vnet_hdr
= 0, i
= 0, queues
;
759 /* for the no-fd, no-helper case */
760 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
761 const char *downscript
= NULL
;
763 const char *vhostfdname
;
766 assert(netdev
->type
== NET_CLIENT_DRIVER_TAP
);
767 tap
= &netdev
->u
.tap
;
768 queues
= tap
->has_queues
? tap
->queues
: 1;
769 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
771 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
772 * For -netdev, peer is always NULL. */
773 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
774 error_setg(errp
, "Multiqueue tap cannot be used with hubs");
779 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
780 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
781 tap
->has_fds
|| tap
->has_vhostfds
) {
782 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
783 "helper=, queues=, fds=, and vhostfds= "
784 "are invalid with fd=");
788 fd
= monitor_fd_param(cur_mon
, tap
->fd
, &err
);
790 error_propagate(errp
, err
);
794 qemu_set_nonblock(fd
);
796 vnet_hdr
= tap_probe_vnet_hdr(fd
);
798 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
800 vhostfdname
, vnet_hdr
, fd
, &err
);
802 error_propagate(errp
, err
);
805 } else if (tap
->has_fds
) {
808 int nfds
= 0, nvhosts
= 0;
811 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
812 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
814 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
815 "helper=, queues=, and vhostfd= "
816 "are invalid with fds=");
820 fds
= g_new0(char *, MAX_TAP_QUEUES
);
821 vhost_fds
= g_new0(char *, MAX_TAP_QUEUES
);
823 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
824 if (tap
->has_vhostfds
) {
825 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
826 if (nfds
!= nvhosts
) {
827 error_setg(errp
, "The number of fds passed does not match "
828 "the number of vhostfds passed");
834 for (i
= 0; i
< nfds
; i
++) {
835 fd
= monitor_fd_param(cur_mon
, fds
[i
], &err
);
837 error_propagate(errp
, err
);
842 qemu_set_nonblock(fd
);
845 vnet_hdr
= tap_probe_vnet_hdr(fd
);
846 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
848 "vnet_hdr not consistent across given tap fds");
853 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
855 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
858 error_propagate(errp
, err
);
865 for (i
= 0; i
< nvhosts
; i
++) {
866 g_free(vhost_fds
[i
]);
868 for (i
= 0; i
< nfds
; i
++) {
874 } else if (tap
->has_helper
) {
875 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
876 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
877 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
878 "queues=, and vhostfds= are invalid with helper=");
882 fd
= net_bridge_run_helper(tap
->helper
,
884 tap
->br
: DEFAULT_BRIDGE_INTERFACE
,
890 qemu_set_nonblock(fd
);
891 vnet_hdr
= tap_probe_vnet_hdr(fd
);
893 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
894 script
, downscript
, vhostfdname
,
897 error_propagate(errp
, err
);
902 if (tap
->has_vhostfds
) {
903 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
906 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
907 downscript
= tap
->has_downscript
? tap
->downscript
:
908 DEFAULT_NETWORK_DOWN_SCRIPT
;
910 if (tap
->has_ifname
) {
911 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
916 for (i
= 0; i
< queues
; i
++) {
917 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
918 ifname
, sizeof ifname
, queues
> 1, errp
);
923 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
924 if (tap_fd_get_ifname(fd
, ifname
)) {
925 error_setg(errp
, "Fail to get ifname");
931 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
932 i
>= 1 ? "no" : script
,
933 i
>= 1 ? "no" : downscript
,
934 vhostfdname
, vnet_hdr
, fd
, &err
);
936 error_propagate(errp
, err
);
946 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
948 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
949 assert(nc
->info
->type
== NET_CLIENT_DRIVER_TAP
);
953 int tap_enable(NetClientState
*nc
)
955 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
961 ret
= tap_fd_enable(s
->fd
);
964 tap_update_fd_handler(s
);
970 int tap_disable(NetClientState
*nc
)
972 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
975 if (s
->enabled
== 0) {
978 ret
= tap_fd_disable(s
->fd
);
980 qemu_purge_queued_packets(nc
);
982 tap_update_fd_handler(s
);