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 "qemu-common.h"
40 #include "qemu/error-report.h"
44 #include "net/vhost_net.h"
46 typedef struct TAPState
{
49 char down_script
[1024];
50 char down_script_arg
[128];
51 uint8_t buf
[NET_BUFSIZE
];
57 VHostNetState
*vhost_net
;
58 unsigned host_vnet_hdr_len
;
61 static void launch_script(const char *setup_script
, const char *ifname
,
62 int fd
, Error
**errp
);
64 static void tap_send(void *opaque
);
65 static void tap_writable(void *opaque
);
67 static void tap_update_fd_handler(TAPState
*s
)
69 qemu_set_fd_handler(s
->fd
,
70 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
71 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
75 static void tap_read_poll(TAPState
*s
, bool enable
)
77 s
->read_poll
= enable
;
78 tap_update_fd_handler(s
);
81 static void tap_write_poll(TAPState
*s
, bool enable
)
83 s
->write_poll
= enable
;
84 tap_update_fd_handler(s
);
87 static void tap_writable(void *opaque
)
91 tap_write_poll(s
, false);
93 qemu_flush_queued_packets(&s
->nc
);
96 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
101 len
= writev(s
->fd
, iov
, iovcnt
);
102 } while (len
== -1 && errno
== EINTR
);
104 if (len
== -1 && errno
== EAGAIN
) {
105 tap_write_poll(s
, true);
112 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
115 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
116 const struct iovec
*iovp
= iov
;
117 struct iovec iov_copy
[iovcnt
+ 1];
118 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
120 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
121 iov_copy
[0].iov_base
= &hdr
;
122 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
123 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
128 return tap_write_packet(s
, iovp
, iovcnt
);
131 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
133 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
136 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
138 if (s
->host_vnet_hdr_len
) {
139 iov
[iovcnt
].iov_base
= &hdr
;
140 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
144 iov
[iovcnt
].iov_base
= (char *)buf
;
145 iov
[iovcnt
].iov_len
= size
;
148 return tap_write_packet(s
, iov
, iovcnt
);
151 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
153 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
156 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
157 return tap_receive_raw(nc
, buf
, size
);
160 iov
[0].iov_base
= (char *)buf
;
161 iov
[0].iov_len
= size
;
163 return tap_write_packet(s
, iov
, 1);
167 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
169 return read(tapfd
, buf
, maxlen
);
173 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
175 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
176 tap_read_poll(s
, true);
179 static void tap_send(void *opaque
)
181 TAPState
*s
= opaque
;
186 uint8_t *buf
= s
->buf
;
188 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
193 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
194 buf
+= s
->host_vnet_hdr_len
;
195 size
-= s
->host_vnet_hdr_len
;
198 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
200 tap_read_poll(s
, false);
202 } else if (size
< 0) {
207 * When the host keeps receiving more packets while tap_send() is
208 * running we can hog the QEMU global mutex. Limit the number of
209 * packets that are processed per tap_send() callback to prevent
210 * stalling the guest.
219 static bool tap_has_ufo(NetClientState
*nc
)
221 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
223 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
228 static bool tap_has_vnet_hdr(NetClientState
*nc
)
230 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
232 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
234 return !!s
->host_vnet_hdr_len
;
237 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
239 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
241 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
243 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
246 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
248 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
250 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
251 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
252 len
== sizeof(struct virtio_net_hdr
));
254 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
255 s
->host_vnet_hdr_len
= len
;
258 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
260 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
262 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
263 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
265 s
->using_vnet_hdr
= using_vnet_hdr
;
268 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
270 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
272 return tap_fd_set_vnet_le(s
->fd
, is_le
);
275 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
277 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
279 return tap_fd_set_vnet_be(s
->fd
, is_be
);
282 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
283 int tso6
, int ecn
, int ufo
)
285 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
290 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
293 static void tap_cleanup(NetClientState
*nc
)
295 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
299 vhost_net_cleanup(s
->vhost_net
);
303 qemu_purge_queued_packets(nc
);
305 if (s
->down_script
[0]) {
306 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
308 error_report_err(err
);
312 tap_read_poll(s
, false);
313 tap_write_poll(s
, false);
318 static void tap_poll(NetClientState
*nc
, bool enable
)
320 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
321 tap_read_poll(s
, enable
);
322 tap_write_poll(s
, enable
);
325 int tap_get_fd(NetClientState
*nc
)
327 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
328 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
334 static NetClientInfo net_tap_info
= {
335 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
336 .size
= sizeof(TAPState
),
337 .receive
= tap_receive
,
338 .receive_raw
= tap_receive_raw
,
339 .receive_iov
= tap_receive_iov
,
341 .cleanup
= tap_cleanup
,
342 .has_ufo
= tap_has_ufo
,
343 .has_vnet_hdr
= tap_has_vnet_hdr
,
344 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
345 .using_vnet_hdr
= tap_using_vnet_hdr
,
346 .set_offload
= tap_set_offload
,
347 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
348 .set_vnet_le
= tap_set_vnet_le
,
349 .set_vnet_be
= tap_set_vnet_be
,
352 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
361 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
363 s
= DO_UPCAST(TAPState
, nc
, nc
);
366 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
367 s
->using_vnet_hdr
= false;
368 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
370 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
372 * Make sure host header length is set correctly in tap:
373 * it might have been modified by another instance of qemu.
375 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
376 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
378 tap_read_poll(s
, true);
383 static void launch_script(const char *setup_script
, const char *ifname
,
384 int fd
, Error
**errp
)
390 /* try to launch network script */
393 error_setg_errno(errp
, errno
, "could not launch network script %s",
398 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
400 for (i
= 3; i
< open_max
; i
++) {
406 *parg
++ = (char *)setup_script
;
407 *parg
++ = (char *)ifname
;
409 execv(setup_script
, args
);
412 while (waitpid(pid
, &status
, 0) != pid
) {
416 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
419 error_setg(errp
, "network script %s failed with status %d",
420 setup_script
, status
);
424 static int recv_fd(int c
)
427 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
428 struct msghdr msg
= {
429 .msg_control
= msgbuf
,
430 .msg_controllen
= sizeof(msgbuf
),
432 struct cmsghdr
*cmsg
;
437 cmsg
= CMSG_FIRSTHDR(&msg
);
438 cmsg
->cmsg_level
= SOL_SOCKET
;
439 cmsg
->cmsg_type
= SCM_RIGHTS
;
440 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
441 msg
.msg_controllen
= cmsg
->cmsg_len
;
444 iov
.iov_len
= sizeof(req
);
449 len
= recvmsg(c
, &msg
, 0);
451 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
458 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
461 sigset_t oldmask
, mask
;
468 sigaddset(&mask
, SIGCHLD
);
469 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
471 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
472 error_setg_errno(errp
, errno
, "socketpair() failed");
476 /* try to launch bridge helper */
479 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
483 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
485 char br_buf
[6+IFNAMSIZ
] = {0};
486 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
488 for (i
= 3; i
< open_max
; i
++) {
494 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
496 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
497 /* assume helper is a command */
499 if (strstr(helper
, "--br=") == NULL
) {
500 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
503 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
504 helper
, "--use-vnet", fd_buf
, br_buf
);
507 *parg
++ = (char *)"sh";
508 *parg
++ = (char *)"-c";
509 *parg
++ = helper_cmd
;
512 execv("/bin/sh", args
);
514 /* assume helper is just the executable path name */
516 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
519 *parg
++ = (char *)helper
;
520 *parg
++ = (char *)"--use-vnet";
537 } while (fd
== -1 && errno
== EINTR
);
542 while (waitpid(pid
, &status
, 0) != pid
) {
545 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
547 error_setg_errno(errp
, saved_errno
,
548 "failed to recv file descriptor");
551 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
552 error_setg(errp
, "bridge helper failed");
559 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
560 NetClientState
*peer
, Error
**errp
)
562 const NetdevBridgeOptions
*bridge
;
563 const char *helper
, *br
;
567 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
568 bridge
= opts
->u
.bridge
;
570 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
571 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
573 fd
= net_bridge_run_helper(helper
, br
, errp
);
578 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
579 vnet_hdr
= tap_probe_vnet_hdr(fd
);
580 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
582 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
588 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
589 const char *setup_script
, char *ifname
,
590 size_t ifname_sz
, int mq_required
, Error
**errp
)
593 int fd
, vnet_hdr_required
;
595 if (tap
->has_vnet_hdr
) {
596 *vnet_hdr
= tap
->vnet_hdr
;
597 vnet_hdr_required
= *vnet_hdr
;
600 vnet_hdr_required
= 0;
603 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
610 setup_script
[0] != '\0' &&
611 strcmp(setup_script
, "no") != 0) {
612 launch_script(setup_script
, ifname
, fd
, &err
);
614 error_propagate(errp
, err
);
623 #define MAX_TAP_QUEUES 1024
625 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
626 const char *model
, const char *name
,
627 const char *ifname
, const char *script
,
628 const char *downscript
, const char *vhostfdname
,
629 int vnet_hdr
, int fd
, Error
**errp
)
632 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
635 tap_set_sndbuf(s
->fd
, tap
, &err
);
637 error_propagate(errp
, err
);
641 if (tap
->has_fd
|| tap
->has_fds
) {
642 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
643 } else if (tap
->has_helper
) {
644 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
647 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
648 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
651 if (strcmp(downscript
, "no") != 0) {
652 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
653 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
658 if (tap
->has_vhost
? tap
->vhost
:
659 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
660 VhostNetOptions options
;
662 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
663 options
.net_backend
= &s
->nc
;
665 if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
666 vhostfd
= monitor_fd_param(cur_mon
, vhostfdname
, &err
);
668 error_propagate(errp
, err
);
672 vhostfd
= open("/dev/vhost-net", O_RDWR
);
674 error_setg_errno(errp
, errno
,
675 "tap: open vhost char device failed");
679 options
.opaque
= (void *)(uintptr_t)vhostfd
;
681 s
->vhost_net
= vhost_net_init(&options
);
684 "vhost-net requested but could not be initialized");
687 } else if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
688 error_setg(errp
, "vhostfd= is not valid without vhost");
692 static int get_fds(char *str
, char *fds
[], int max
)
694 char *ptr
= str
, *this;
695 size_t len
= strlen(str
);
698 while (i
< max
&& ptr
< str
+ len
) {
699 this = strchr(ptr
, ':');
702 fds
[i
] = g_strdup(ptr
);
704 fds
[i
] = g_strndup(ptr
, this - ptr
);
718 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
719 NetClientState
*peer
, Error
**errp
)
721 const NetdevTapOptions
*tap
;
722 int fd
, vnet_hdr
= 0, i
= 0, queues
;
723 /* for the no-fd, no-helper case */
724 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
725 const char *downscript
= NULL
;
727 const char *vhostfdname
;
730 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
732 queues
= tap
->has_queues
? tap
->queues
: 1;
733 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
735 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
736 * For -netdev, peer is always NULL. */
737 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
738 error_setg(errp
, "Multiqueue tap cannot be used with QEMU vlans");
743 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
744 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
745 tap
->has_fds
|| tap
->has_vhostfds
) {
746 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
747 "helper=, queues=, fds=, and vhostfds= "
748 "are invalid with fd=");
752 fd
= monitor_fd_param(cur_mon
, tap
->fd
, &err
);
754 error_propagate(errp
, err
);
758 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
760 vnet_hdr
= tap_probe_vnet_hdr(fd
);
762 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
764 vhostfdname
, vnet_hdr
, fd
, &err
);
766 error_propagate(errp
, err
);
769 } else if (tap
->has_fds
) {
770 char *fds
[MAX_TAP_QUEUES
];
771 char *vhost_fds
[MAX_TAP_QUEUES
];
774 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
775 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
777 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
778 "helper=, queues=, and vhostfd= "
779 "are invalid with fds=");
783 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
784 if (tap
->has_vhostfds
) {
785 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
786 if (nfds
!= nvhosts
) {
787 error_setg(errp
, "The number of fds passed does not match "
788 "the number of vhostfds passed");
793 for (i
= 0; i
< nfds
; i
++) {
794 fd
= monitor_fd_param(cur_mon
, fds
[i
], &err
);
796 error_propagate(errp
, err
);
800 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
803 vnet_hdr
= tap_probe_vnet_hdr(fd
);
804 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
806 "vnet_hdr not consistent across given tap fds");
810 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
812 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
815 error_propagate(errp
, err
);
819 } else if (tap
->has_helper
) {
820 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
821 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
822 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
823 "queues=, and vhostfds= are invalid with helper=");
827 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
,
833 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
834 vnet_hdr
= tap_probe_vnet_hdr(fd
);
836 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
837 script
, downscript
, vhostfdname
,
840 error_propagate(errp
, err
);
845 if (tap
->has_vhostfds
) {
846 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
849 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
850 downscript
= tap
->has_downscript
? tap
->downscript
:
851 DEFAULT_NETWORK_DOWN_SCRIPT
;
853 if (tap
->has_ifname
) {
854 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
859 for (i
= 0; i
< queues
; i
++) {
860 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
861 ifname
, sizeof ifname
, queues
> 1, errp
);
866 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
867 if (tap_fd_get_ifname(fd
, ifname
)) {
868 error_setg(errp
, "Fail to get ifname");
874 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
875 i
>= 1 ? "no" : script
,
876 i
>= 1 ? "no" : downscript
,
877 vhostfdname
, vnet_hdr
, fd
, &err
);
879 error_propagate(errp
, err
);
889 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
891 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
892 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
896 int tap_enable(NetClientState
*nc
)
898 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
904 ret
= tap_fd_enable(s
->fd
);
907 tap_update_fd_handler(s
);
913 int tap_disable(NetClientState
*nc
)
915 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
918 if (s
->enabled
== 0) {
921 ret
= tap_fd_disable(s
->fd
);
923 qemu_purge_queued_packets(nc
);
925 tap_update_fd_handler(s
);