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"
46 #include "net/vhost_net.h"
48 typedef struct TAPState
{
51 char down_script
[1024];
52 char down_script_arg
[128];
53 uint8_t buf
[NET_BUFSIZE
];
59 VHostNetState
*vhost_net
;
60 unsigned host_vnet_hdr_len
;
63 static void launch_script(const char *setup_script
, const char *ifname
,
64 int fd
, Error
**errp
);
66 static void tap_send(void *opaque
);
67 static void tap_writable(void *opaque
);
69 static void tap_update_fd_handler(TAPState
*s
)
71 qemu_set_fd_handler(s
->fd
,
72 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
73 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
77 static void tap_read_poll(TAPState
*s
, bool enable
)
79 s
->read_poll
= enable
;
80 tap_update_fd_handler(s
);
83 static void tap_write_poll(TAPState
*s
, bool enable
)
85 s
->write_poll
= enable
;
86 tap_update_fd_handler(s
);
89 static void tap_writable(void *opaque
)
93 tap_write_poll(s
, false);
95 qemu_flush_queued_packets(&s
->nc
);
98 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
103 len
= writev(s
->fd
, iov
, iovcnt
);
104 } while (len
== -1 && errno
== EINTR
);
106 if (len
== -1 && errno
== EAGAIN
) {
107 tap_write_poll(s
, true);
114 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
117 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
118 const struct iovec
*iovp
= iov
;
119 struct iovec iov_copy
[iovcnt
+ 1];
120 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
122 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
123 iov_copy
[0].iov_base
= &hdr
;
124 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
125 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
130 return tap_write_packet(s
, iovp
, iovcnt
);
133 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
135 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
138 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
140 if (s
->host_vnet_hdr_len
) {
141 iov
[iovcnt
].iov_base
= &hdr
;
142 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
146 iov
[iovcnt
].iov_base
= (char *)buf
;
147 iov
[iovcnt
].iov_len
= size
;
150 return tap_write_packet(s
, iov
, iovcnt
);
153 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
155 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
158 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
159 return tap_receive_raw(nc
, buf
, size
);
162 iov
[0].iov_base
= (char *)buf
;
163 iov
[0].iov_len
= size
;
165 return tap_write_packet(s
, iov
, 1);
169 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
171 return read(tapfd
, buf
, maxlen
);
175 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
177 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
178 tap_read_poll(s
, true);
181 static void tap_send(void *opaque
)
183 TAPState
*s
= opaque
;
188 uint8_t *buf
= s
->buf
;
190 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
195 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
196 buf
+= s
->host_vnet_hdr_len
;
197 size
-= s
->host_vnet_hdr_len
;
200 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
202 tap_read_poll(s
, false);
204 } else if (size
< 0) {
209 * When the host keeps receiving more packets while tap_send() is
210 * running we can hog the QEMU global mutex. Limit the number of
211 * packets that are processed per tap_send() callback to prevent
212 * stalling the guest.
221 static bool tap_has_ufo(NetClientState
*nc
)
223 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
225 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
230 static bool tap_has_vnet_hdr(NetClientState
*nc
)
232 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
234 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
236 return !!s
->host_vnet_hdr_len
;
239 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
241 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
243 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
245 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
248 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
250 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
252 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
253 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
254 len
== sizeof(struct virtio_net_hdr
));
256 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
257 s
->host_vnet_hdr_len
= len
;
260 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
262 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
264 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
265 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
267 s
->using_vnet_hdr
= using_vnet_hdr
;
270 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
272 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
274 return tap_fd_set_vnet_le(s
->fd
, is_le
);
277 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
279 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
281 return tap_fd_set_vnet_be(s
->fd
, is_be
);
284 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
285 int tso6
, int ecn
, int ufo
)
287 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
292 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
295 static void tap_cleanup(NetClientState
*nc
)
297 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
301 vhost_net_cleanup(s
->vhost_net
);
305 qemu_purge_queued_packets(nc
);
307 if (s
->down_script
[0]) {
308 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
310 error_report_err(err
);
314 tap_read_poll(s
, false);
315 tap_write_poll(s
, false);
320 static void tap_poll(NetClientState
*nc
, bool enable
)
322 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
323 tap_read_poll(s
, enable
);
324 tap_write_poll(s
, enable
);
327 int tap_get_fd(NetClientState
*nc
)
329 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
330 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
336 static NetClientInfo net_tap_info
= {
337 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
338 .size
= sizeof(TAPState
),
339 .receive
= tap_receive
,
340 .receive_raw
= tap_receive_raw
,
341 .receive_iov
= tap_receive_iov
,
343 .cleanup
= tap_cleanup
,
344 .has_ufo
= tap_has_ufo
,
345 .has_vnet_hdr
= tap_has_vnet_hdr
,
346 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
347 .using_vnet_hdr
= tap_using_vnet_hdr
,
348 .set_offload
= tap_set_offload
,
349 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
350 .set_vnet_le
= tap_set_vnet_le
,
351 .set_vnet_be
= tap_set_vnet_be
,
354 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
363 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
365 s
= DO_UPCAST(TAPState
, nc
, nc
);
368 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
369 s
->using_vnet_hdr
= false;
370 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
372 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
374 * Make sure host header length is set correctly in tap:
375 * it might have been modified by another instance of qemu.
377 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
378 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
380 tap_read_poll(s
, true);
385 static void launch_script(const char *setup_script
, const char *ifname
,
386 int fd
, Error
**errp
)
392 /* try to launch network script */
395 error_setg_errno(errp
, errno
, "could not launch network script %s",
400 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
402 for (i
= 3; i
< open_max
; i
++) {
408 *parg
++ = (char *)setup_script
;
409 *parg
++ = (char *)ifname
;
411 execv(setup_script
, args
);
414 while (waitpid(pid
, &status
, 0) != pid
) {
418 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
421 error_setg(errp
, "network script %s failed with status %d",
422 setup_script
, status
);
426 static int recv_fd(int c
)
429 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
430 struct msghdr msg
= {
431 .msg_control
= msgbuf
,
432 .msg_controllen
= sizeof(msgbuf
),
434 struct cmsghdr
*cmsg
;
439 cmsg
= CMSG_FIRSTHDR(&msg
);
440 cmsg
->cmsg_level
= SOL_SOCKET
;
441 cmsg
->cmsg_type
= SCM_RIGHTS
;
442 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
443 msg
.msg_controllen
= cmsg
->cmsg_len
;
446 iov
.iov_len
= sizeof(req
);
451 len
= recvmsg(c
, &msg
, 0);
453 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
460 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
463 sigset_t oldmask
, mask
;
470 sigaddset(&mask
, SIGCHLD
);
471 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
473 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
474 error_setg_errno(errp
, errno
, "socketpair() failed");
478 /* try to launch bridge helper */
481 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
485 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
487 char br_buf
[6+IFNAMSIZ
] = {0};
488 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
490 for (i
= 3; i
< open_max
; i
++) {
496 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
498 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
499 /* assume helper is a command */
501 if (strstr(helper
, "--br=") == NULL
) {
502 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
505 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
506 helper
, "--use-vnet", fd_buf
, br_buf
);
509 *parg
++ = (char *)"sh";
510 *parg
++ = (char *)"-c";
511 *parg
++ = helper_cmd
;
514 execv("/bin/sh", args
);
516 /* assume helper is just the executable path name */
518 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
521 *parg
++ = (char *)helper
;
522 *parg
++ = (char *)"--use-vnet";
539 } while (fd
== -1 && errno
== EINTR
);
544 while (waitpid(pid
, &status
, 0) != pid
) {
547 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
549 error_setg_errno(errp
, saved_errno
,
550 "failed to recv file descriptor");
553 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
554 error_setg(errp
, "bridge helper failed");
561 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
562 NetClientState
*peer
, Error
**errp
)
564 const NetdevBridgeOptions
*bridge
;
565 const char *helper
, *br
;
569 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
570 bridge
= opts
->u
.bridge
.data
;
572 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
573 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
575 fd
= net_bridge_run_helper(helper
, br
, errp
);
580 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
581 vnet_hdr
= tap_probe_vnet_hdr(fd
);
582 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
584 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
590 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
591 const char *setup_script
, char *ifname
,
592 size_t ifname_sz
, int mq_required
, Error
**errp
)
595 int fd
, vnet_hdr_required
;
597 if (tap
->has_vnet_hdr
) {
598 *vnet_hdr
= tap
->vnet_hdr
;
599 vnet_hdr_required
= *vnet_hdr
;
602 vnet_hdr_required
= 0;
605 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
612 setup_script
[0] != '\0' &&
613 strcmp(setup_script
, "no") != 0) {
614 launch_script(setup_script
, ifname
, fd
, &err
);
616 error_propagate(errp
, err
);
625 #define MAX_TAP_QUEUES 1024
627 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
628 const char *model
, const char *name
,
629 const char *ifname
, const char *script
,
630 const char *downscript
, const char *vhostfdname
,
631 int vnet_hdr
, int fd
, Error
**errp
)
634 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
637 tap_set_sndbuf(s
->fd
, tap
, &err
);
639 error_propagate(errp
, err
);
643 if (tap
->has_fd
|| tap
->has_fds
) {
644 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
645 } else if (tap
->has_helper
) {
646 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
649 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
650 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
653 if (strcmp(downscript
, "no") != 0) {
654 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
655 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
660 if (tap
->has_vhost
? tap
->vhost
:
661 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
662 VhostNetOptions options
;
664 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
665 options
.net_backend
= &s
->nc
;
666 if (tap
->has_poll_us
) {
667 options
.busyloop_timeout
= tap
->poll_us
;
669 options
.busyloop_timeout
= 0;
673 vhostfd
= monitor_fd_param(cur_mon
, vhostfdname
, &err
);
675 error_propagate(errp
, err
);
679 vhostfd
= open("/dev/vhost-net", O_RDWR
);
681 error_setg_errno(errp
, errno
,
682 "tap: open vhost char device failed");
686 options
.opaque
= (void *)(uintptr_t)vhostfd
;
688 s
->vhost_net
= vhost_net_init(&options
);
691 "vhost-net requested but could not be initialized");
694 } else if (vhostfdname
) {
695 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
699 static int get_fds(char *str
, char *fds
[], int max
)
701 char *ptr
= str
, *this;
702 size_t len
= strlen(str
);
705 while (i
< max
&& ptr
< str
+ len
) {
706 this = strchr(ptr
, ':');
709 fds
[i
] = g_strdup(ptr
);
711 fds
[i
] = g_strndup(ptr
, this - ptr
);
725 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
726 NetClientState
*peer
, Error
**errp
)
728 const NetdevTapOptions
*tap
;
729 int fd
, vnet_hdr
= 0, i
= 0, queues
;
730 /* for the no-fd, no-helper case */
731 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
732 const char *downscript
= NULL
;
734 const char *vhostfdname
;
737 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
738 tap
= opts
->u
.tap
.data
;
739 queues
= tap
->has_queues
? tap
->queues
: 1;
740 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
742 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
743 * For -netdev, peer is always NULL. */
744 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
745 error_setg(errp
, "Multiqueue tap cannot be used with QEMU vlans");
750 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
751 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
752 tap
->has_fds
|| tap
->has_vhostfds
) {
753 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
754 "helper=, queues=, fds=, and vhostfds= "
755 "are invalid with fd=");
759 fd
= monitor_fd_param(cur_mon
, tap
->fd
, &err
);
761 error_propagate(errp
, err
);
765 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
767 vnet_hdr
= tap_probe_vnet_hdr(fd
);
769 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
771 vhostfdname
, vnet_hdr
, fd
, &err
);
773 error_propagate(errp
, err
);
776 } else if (tap
->has_fds
) {
777 char **fds
= g_new(char *, MAX_TAP_QUEUES
);
778 char **vhost_fds
= g_new(char *, MAX_TAP_QUEUES
);
781 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
782 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
784 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
785 "helper=, queues=, and vhostfd= "
786 "are invalid with fds=");
790 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
791 if (tap
->has_vhostfds
) {
792 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
793 if (nfds
!= nvhosts
) {
794 error_setg(errp
, "The number of fds passed does not match "
795 "the number of vhostfds passed");
800 for (i
= 0; i
< nfds
; i
++) {
801 fd
= monitor_fd_param(cur_mon
, fds
[i
], &err
);
803 error_propagate(errp
, err
);
807 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
810 vnet_hdr
= tap_probe_vnet_hdr(fd
);
811 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
813 "vnet_hdr not consistent across given tap fds");
817 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
819 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
822 error_propagate(errp
, err
);
828 } else if (tap
->has_helper
) {
829 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
830 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
831 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
832 "queues=, and vhostfds= are invalid with helper=");
836 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
,
842 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
843 vnet_hdr
= tap_probe_vnet_hdr(fd
);
845 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
846 script
, downscript
, vhostfdname
,
849 error_propagate(errp
, err
);
854 if (tap
->has_vhostfds
) {
855 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
858 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
859 downscript
= tap
->has_downscript
? tap
->downscript
:
860 DEFAULT_NETWORK_DOWN_SCRIPT
;
862 if (tap
->has_ifname
) {
863 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
868 for (i
= 0; i
< queues
; i
++) {
869 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
870 ifname
, sizeof ifname
, queues
> 1, errp
);
875 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
876 if (tap_fd_get_ifname(fd
, ifname
)) {
877 error_setg(errp
, "Fail to get ifname");
883 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
884 i
>= 1 ? "no" : script
,
885 i
>= 1 ? "no" : downscript
,
886 vhostfdname
, vnet_hdr
, fd
, &err
);
888 error_propagate(errp
, err
);
898 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
900 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
901 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
905 int tap_enable(NetClientState
*nc
)
907 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
913 ret
= tap_fd_enable(s
->fd
);
916 tap_update_fd_handler(s
);
922 int tap_disable(NetClientState
*nc
)
924 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
927 if (s
->enabled
== 0) {
930 ret
= tap_fd_disable(s
->fd
);
932 qemu_purge_queued_packets(nc
);
934 tap_update_fd_handler(s
);