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
;
64 static void launch_script(const char *setup_script
, const char *ifname
,
65 int fd
, Error
**errp
);
67 static void tap_send(void *opaque
);
68 static void tap_writable(void *opaque
);
70 static void tap_update_fd_handler(TAPState
*s
)
72 qemu_set_fd_handler(s
->fd
,
73 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
74 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
78 static void tap_read_poll(TAPState
*s
, bool enable
)
80 s
->read_poll
= enable
;
81 tap_update_fd_handler(s
);
84 static void tap_write_poll(TAPState
*s
, bool enable
)
86 s
->write_poll
= enable
;
87 tap_update_fd_handler(s
);
90 static void tap_writable(void *opaque
)
94 tap_write_poll(s
, false);
96 qemu_flush_queued_packets(&s
->nc
);
99 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
104 len
= writev(s
->fd
, iov
, iovcnt
);
105 } while (len
== -1 && errno
== EINTR
);
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
;
191 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
196 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
197 buf
+= s
->host_vnet_hdr_len
;
198 size
-= s
->host_vnet_hdr_len
;
201 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
203 tap_read_poll(s
, false);
205 } else if (size
< 0) {
210 * When the host keeps receiving more packets while tap_send() is
211 * running we can hog the QEMU global mutex. Limit the number of
212 * packets that are processed per tap_send() callback to prevent
213 * stalling the guest.
222 static bool tap_has_ufo(NetClientState
*nc
)
224 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
226 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
231 static bool tap_has_vnet_hdr(NetClientState
*nc
)
233 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
235 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
237 return !!s
->host_vnet_hdr_len
;
240 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
242 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
244 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
246 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
249 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
251 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
253 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
254 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
255 len
== sizeof(struct virtio_net_hdr
));
257 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
258 s
->host_vnet_hdr_len
= len
;
261 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
263 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
265 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
266 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
268 s
->using_vnet_hdr
= using_vnet_hdr
;
271 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
273 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
275 return tap_fd_set_vnet_le(s
->fd
, is_le
);
278 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
280 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
282 return tap_fd_set_vnet_be(s
->fd
, is_be
);
285 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
286 int tso6
, int ecn
, int ufo
)
288 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
293 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
296 static void tap_exit_notify(Notifier
*notifier
, void *data
)
298 TAPState
*s
= container_of(notifier
, TAPState
, exit
);
301 if (s
->down_script
[0]) {
302 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
304 error_report_err(err
);
309 static void tap_cleanup(NetClientState
*nc
)
311 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
314 vhost_net_cleanup(s
->vhost_net
);
318 qemu_purge_queued_packets(nc
);
320 tap_exit_notify(&s
->exit
, NULL
);
321 qemu_remove_exit_notifier(&s
->exit
);
323 tap_read_poll(s
, false);
324 tap_write_poll(s
, false);
329 static void tap_poll(NetClientState
*nc
, bool enable
)
331 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
332 tap_read_poll(s
, enable
);
333 tap_write_poll(s
, enable
);
336 int tap_get_fd(NetClientState
*nc
)
338 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
339 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
345 static NetClientInfo net_tap_info
= {
346 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
347 .size
= sizeof(TAPState
),
348 .receive
= tap_receive
,
349 .receive_raw
= tap_receive_raw
,
350 .receive_iov
= tap_receive_iov
,
352 .cleanup
= tap_cleanup
,
353 .has_ufo
= tap_has_ufo
,
354 .has_vnet_hdr
= tap_has_vnet_hdr
,
355 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
356 .using_vnet_hdr
= tap_using_vnet_hdr
,
357 .set_offload
= tap_set_offload
,
358 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
359 .set_vnet_le
= tap_set_vnet_le
,
360 .set_vnet_be
= tap_set_vnet_be
,
363 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
372 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
374 s
= DO_UPCAST(TAPState
, nc
, nc
);
377 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
378 s
->using_vnet_hdr
= false;
379 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
381 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
383 * Make sure host header length is set correctly in tap:
384 * it might have been modified by another instance of qemu.
386 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
387 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
389 tap_read_poll(s
, true);
392 s
->exit
.notify
= tap_exit_notify
;
393 qemu_add_exit_notifier(&s
->exit
);
398 static void launch_script(const char *setup_script
, const char *ifname
,
399 int fd
, Error
**errp
)
405 /* try to launch network script */
408 error_setg_errno(errp
, errno
, "could not launch network script %s",
413 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
415 for (i
= 3; i
< open_max
; i
++) {
421 *parg
++ = (char *)setup_script
;
422 *parg
++ = (char *)ifname
;
424 execv(setup_script
, args
);
427 while (waitpid(pid
, &status
, 0) != pid
) {
431 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
434 error_setg(errp
, "network script %s failed with status %d",
435 setup_script
, status
);
439 static int recv_fd(int c
)
442 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
443 struct msghdr msg
= {
444 .msg_control
= msgbuf
,
445 .msg_controllen
= sizeof(msgbuf
),
447 struct cmsghdr
*cmsg
;
452 cmsg
= CMSG_FIRSTHDR(&msg
);
453 cmsg
->cmsg_level
= SOL_SOCKET
;
454 cmsg
->cmsg_type
= SCM_RIGHTS
;
455 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
456 msg
.msg_controllen
= cmsg
->cmsg_len
;
459 iov
.iov_len
= sizeof(req
);
464 len
= recvmsg(c
, &msg
, 0);
466 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
473 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
476 sigset_t oldmask
, mask
;
483 sigaddset(&mask
, SIGCHLD
);
484 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
486 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
487 error_setg_errno(errp
, errno
, "socketpair() failed");
491 /* try to launch bridge helper */
494 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
498 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
500 char br_buf
[6+IFNAMSIZ
] = {0};
501 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
503 for (i
= 3; i
< open_max
; i
++) {
509 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
511 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
512 /* assume helper is a command */
514 if (strstr(helper
, "--br=") == NULL
) {
515 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
518 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
519 helper
, "--use-vnet", fd_buf
, br_buf
);
522 *parg
++ = (char *)"sh";
523 *parg
++ = (char *)"-c";
524 *parg
++ = helper_cmd
;
527 execv("/bin/sh", args
);
529 /* assume helper is just the executable path name */
531 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
534 *parg
++ = (char *)helper
;
535 *parg
++ = (char *)"--use-vnet";
552 } while (fd
== -1 && errno
== EINTR
);
557 while (waitpid(pid
, &status
, 0) != pid
) {
560 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
562 error_setg_errno(errp
, saved_errno
,
563 "failed to recv file descriptor");
566 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
567 error_setg(errp
, "bridge helper failed");
574 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
575 NetClientState
*peer
, Error
**errp
)
577 const NetdevBridgeOptions
*bridge
;
578 const char *helper
, *br
;
582 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
583 bridge
= opts
->u
.bridge
.data
;
585 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
586 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
588 fd
= net_bridge_run_helper(helper
, br
, errp
);
593 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
594 vnet_hdr
= tap_probe_vnet_hdr(fd
);
595 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
597 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
603 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
604 const char *setup_script
, char *ifname
,
605 size_t ifname_sz
, int mq_required
, Error
**errp
)
608 int fd
, vnet_hdr_required
;
610 if (tap
->has_vnet_hdr
) {
611 *vnet_hdr
= tap
->vnet_hdr
;
612 vnet_hdr_required
= *vnet_hdr
;
615 vnet_hdr_required
= 0;
618 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
625 setup_script
[0] != '\0' &&
626 strcmp(setup_script
, "no") != 0) {
627 launch_script(setup_script
, ifname
, fd
, &err
);
629 error_propagate(errp
, err
);
638 #define MAX_TAP_QUEUES 1024
640 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
641 const char *model
, const char *name
,
642 const char *ifname
, const char *script
,
643 const char *downscript
, const char *vhostfdname
,
644 int vnet_hdr
, int fd
, Error
**errp
)
647 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
650 tap_set_sndbuf(s
->fd
, tap
, &err
);
652 error_propagate(errp
, err
);
656 if (tap
->has_fd
|| tap
->has_fds
) {
657 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
658 } else if (tap
->has_helper
) {
659 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
662 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
663 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
666 if (strcmp(downscript
, "no") != 0) {
667 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
668 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
673 if (tap
->has_vhost
? tap
->vhost
:
674 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
675 VhostNetOptions options
;
677 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
678 options
.net_backend
= &s
->nc
;
679 if (tap
->has_poll_us
) {
680 options
.busyloop_timeout
= tap
->poll_us
;
682 options
.busyloop_timeout
= 0;
686 vhostfd
= monitor_fd_param(cur_mon
, vhostfdname
, &err
);
688 error_propagate(errp
, err
);
692 vhostfd
= open("/dev/vhost-net", O_RDWR
);
694 error_setg_errno(errp
, errno
,
695 "tap: open vhost char device failed");
699 options
.opaque
= (void *)(uintptr_t)vhostfd
;
701 s
->vhost_net
= vhost_net_init(&options
);
704 "vhost-net requested but could not be initialized");
707 } else if (vhostfdname
) {
708 error_setg(errp
, "vhostfd(s)= is not valid without vhost");
712 static int get_fds(char *str
, char *fds
[], int max
)
714 char *ptr
= str
, *this;
715 size_t len
= strlen(str
);
718 while (i
< max
&& ptr
< str
+ len
) {
719 this = strchr(ptr
, ':');
722 fds
[i
] = g_strdup(ptr
);
724 fds
[i
] = g_strndup(ptr
, this - ptr
);
738 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
739 NetClientState
*peer
, Error
**errp
)
741 const NetdevTapOptions
*tap
;
742 int fd
, vnet_hdr
= 0, i
= 0, queues
;
743 /* for the no-fd, no-helper case */
744 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
745 const char *downscript
= NULL
;
747 const char *vhostfdname
;
750 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
751 tap
= opts
->u
.tap
.data
;
752 queues
= tap
->has_queues
? tap
->queues
: 1;
753 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
755 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
756 * For -netdev, peer is always NULL. */
757 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
758 error_setg(errp
, "Multiqueue tap cannot be used with QEMU vlans");
763 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
764 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
765 tap
->has_fds
|| tap
->has_vhostfds
) {
766 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
767 "helper=, queues=, fds=, and vhostfds= "
768 "are invalid with fd=");
772 fd
= monitor_fd_param(cur_mon
, tap
->fd
, &err
);
774 error_propagate(errp
, err
);
778 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
780 vnet_hdr
= tap_probe_vnet_hdr(fd
);
782 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
784 vhostfdname
, vnet_hdr
, fd
, &err
);
786 error_propagate(errp
, err
);
789 } else if (tap
->has_fds
) {
790 char **fds
= g_new(char *, MAX_TAP_QUEUES
);
791 char **vhost_fds
= g_new(char *, MAX_TAP_QUEUES
);
794 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
795 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
797 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
798 "helper=, queues=, and vhostfd= "
799 "are invalid with fds=");
803 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
804 if (tap
->has_vhostfds
) {
805 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
806 if (nfds
!= nvhosts
) {
807 error_setg(errp
, "The number of fds passed does not match "
808 "the number of vhostfds passed");
813 for (i
= 0; i
< nfds
; i
++) {
814 fd
= monitor_fd_param(cur_mon
, fds
[i
], &err
);
816 error_propagate(errp
, err
);
820 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
823 vnet_hdr
= tap_probe_vnet_hdr(fd
);
824 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
826 "vnet_hdr not consistent across given tap fds");
830 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
832 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
835 error_propagate(errp
, err
);
841 } else if (tap
->has_helper
) {
842 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
843 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
844 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
845 "queues=, and vhostfds= are invalid with helper=");
849 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
,
855 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
856 vnet_hdr
= tap_probe_vnet_hdr(fd
);
858 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
859 script
, downscript
, vhostfdname
,
862 error_propagate(errp
, err
);
867 if (tap
->has_vhostfds
) {
868 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
871 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
872 downscript
= tap
->has_downscript
? tap
->downscript
:
873 DEFAULT_NETWORK_DOWN_SCRIPT
;
875 if (tap
->has_ifname
) {
876 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
881 for (i
= 0; i
< queues
; i
++) {
882 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
883 ifname
, sizeof ifname
, queues
> 1, errp
);
888 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
889 if (tap_fd_get_ifname(fd
, ifname
)) {
890 error_setg(errp
, "Fail to get ifname");
896 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
897 i
>= 1 ? "no" : script
,
898 i
>= 1 ? "no" : downscript
,
899 vhostfdname
, vnet_hdr
, fd
, &err
);
901 error_propagate(errp
, err
);
911 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
913 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
914 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
918 int tap_enable(NetClientState
*nc
)
920 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
926 ret
= tap_fd_enable(s
->fd
);
929 tap_update_fd_handler(s
);
935 int tap_disable(NetClientState
*nc
)
937 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
940 if (s
->enabled
== 0) {
943 ret
= tap_fd_disable(s
->fd
);
945 qemu_purge_queued_packets(nc
);
947 tap_update_fd_handler(s
);