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
28 #include "config-host.h"
30 #include <sys/ioctl.h>
33 #include <sys/socket.h>
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qemu-common.h"
41 #include "qemu/error-report.h"
45 #include "net/vhost_net.h"
47 typedef struct TAPState
{
50 char down_script
[1024];
51 char down_script_arg
[128];
52 uint8_t buf
[NET_BUFSIZE
];
58 VHostNetState
*vhost_net
;
59 unsigned host_vnet_hdr_len
;
62 static void launch_script(const char *setup_script
, const char *ifname
,
63 int fd
, Error
**errp
);
65 static void tap_send(void *opaque
);
66 static void tap_writable(void *opaque
);
68 static void tap_update_fd_handler(TAPState
*s
)
70 qemu_set_fd_handler(s
->fd
,
71 s
->read_poll
&& s
->enabled
? tap_send
: NULL
,
72 s
->write_poll
&& s
->enabled
? tap_writable
: NULL
,
76 static void tap_read_poll(TAPState
*s
, bool enable
)
78 s
->read_poll
= enable
;
79 tap_update_fd_handler(s
);
82 static void tap_write_poll(TAPState
*s
, bool enable
)
84 s
->write_poll
= enable
;
85 tap_update_fd_handler(s
);
88 static void tap_writable(void *opaque
)
92 tap_write_poll(s
, false);
94 qemu_flush_queued_packets(&s
->nc
);
97 static ssize_t
tap_write_packet(TAPState
*s
, const struct iovec
*iov
, int iovcnt
)
102 len
= writev(s
->fd
, iov
, iovcnt
);
103 } while (len
== -1 && errno
== EINTR
);
105 if (len
== -1 && errno
== EAGAIN
) {
106 tap_write_poll(s
, true);
113 static ssize_t
tap_receive_iov(NetClientState
*nc
, const struct iovec
*iov
,
116 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
117 const struct iovec
*iovp
= iov
;
118 struct iovec iov_copy
[iovcnt
+ 1];
119 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
121 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
122 iov_copy
[0].iov_base
= &hdr
;
123 iov_copy
[0].iov_len
= s
->host_vnet_hdr_len
;
124 memcpy(&iov_copy
[1], iov
, iovcnt
* sizeof(*iov
));
129 return tap_write_packet(s
, iovp
, iovcnt
);
132 static ssize_t
tap_receive_raw(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
134 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
137 struct virtio_net_hdr_mrg_rxbuf hdr
= { };
139 if (s
->host_vnet_hdr_len
) {
140 iov
[iovcnt
].iov_base
= &hdr
;
141 iov
[iovcnt
].iov_len
= s
->host_vnet_hdr_len
;
145 iov
[iovcnt
].iov_base
= (char *)buf
;
146 iov
[iovcnt
].iov_len
= size
;
149 return tap_write_packet(s
, iov
, iovcnt
);
152 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
154 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
157 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
158 return tap_receive_raw(nc
, buf
, size
);
161 iov
[0].iov_base
= (char *)buf
;
162 iov
[0].iov_len
= size
;
164 return tap_write_packet(s
, iov
, 1);
168 ssize_t
tap_read_packet(int tapfd
, uint8_t *buf
, int maxlen
)
170 return read(tapfd
, buf
, maxlen
);
174 static void tap_send_completed(NetClientState
*nc
, ssize_t len
)
176 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
177 tap_read_poll(s
, true);
180 static void tap_send(void *opaque
)
182 TAPState
*s
= opaque
;
187 uint8_t *buf
= s
->buf
;
189 size
= tap_read_packet(s
->fd
, s
->buf
, sizeof(s
->buf
));
194 if (s
->host_vnet_hdr_len
&& !s
->using_vnet_hdr
) {
195 buf
+= s
->host_vnet_hdr_len
;
196 size
-= s
->host_vnet_hdr_len
;
199 size
= qemu_send_packet_async(&s
->nc
, buf
, size
, tap_send_completed
);
201 tap_read_poll(s
, false);
203 } else if (size
< 0) {
208 * When the host keeps receiving more packets while tap_send() is
209 * running we can hog the QEMU global mutex. Limit the number of
210 * packets that are processed per tap_send() callback to prevent
211 * stalling the guest.
220 static bool tap_has_ufo(NetClientState
*nc
)
222 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
224 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
229 static bool tap_has_vnet_hdr(NetClientState
*nc
)
231 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
233 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
235 return !!s
->host_vnet_hdr_len
;
238 static bool tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
240 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
242 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
244 return !!tap_probe_vnet_hdr_len(s
->fd
, len
);
247 static void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)
249 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
251 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
252 assert(len
== sizeof(struct virtio_net_hdr_mrg_rxbuf
) ||
253 len
== sizeof(struct virtio_net_hdr
));
255 tap_fd_set_vnet_hdr_len(s
->fd
, len
);
256 s
->host_vnet_hdr_len
= len
;
259 static void tap_using_vnet_hdr(NetClientState
*nc
, bool using_vnet_hdr
)
261 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
263 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
264 assert(!!s
->host_vnet_hdr_len
== using_vnet_hdr
);
266 s
->using_vnet_hdr
= using_vnet_hdr
;
269 static int tap_set_vnet_le(NetClientState
*nc
, bool is_le
)
271 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
273 return tap_fd_set_vnet_le(s
->fd
, is_le
);
276 static int tap_set_vnet_be(NetClientState
*nc
, bool is_be
)
278 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
280 return tap_fd_set_vnet_be(s
->fd
, is_be
);
283 static void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
284 int tso6
, int ecn
, int ufo
)
286 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
291 tap_fd_set_offload(s
->fd
, csum
, tso4
, tso6
, ecn
, ufo
);
294 static void tap_cleanup(NetClientState
*nc
)
296 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
300 vhost_net_cleanup(s
->vhost_net
);
304 qemu_purge_queued_packets(nc
);
306 if (s
->down_script
[0]) {
307 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
, &err
);
309 error_report_err(err
);
313 tap_read_poll(s
, false);
314 tap_write_poll(s
, false);
319 static void tap_poll(NetClientState
*nc
, bool enable
)
321 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
322 tap_read_poll(s
, enable
);
323 tap_write_poll(s
, enable
);
326 int tap_get_fd(NetClientState
*nc
)
328 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
329 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
335 static NetClientInfo net_tap_info
= {
336 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
337 .size
= sizeof(TAPState
),
338 .receive
= tap_receive
,
339 .receive_raw
= tap_receive_raw
,
340 .receive_iov
= tap_receive_iov
,
342 .cleanup
= tap_cleanup
,
343 .has_ufo
= tap_has_ufo
,
344 .has_vnet_hdr
= tap_has_vnet_hdr
,
345 .has_vnet_hdr_len
= tap_has_vnet_hdr_len
,
346 .using_vnet_hdr
= tap_using_vnet_hdr
,
347 .set_offload
= tap_set_offload
,
348 .set_vnet_hdr_len
= tap_set_vnet_hdr_len
,
349 .set_vnet_le
= tap_set_vnet_le
,
350 .set_vnet_be
= tap_set_vnet_be
,
353 static TAPState
*net_tap_fd_init(NetClientState
*peer
,
362 nc
= qemu_new_net_client(&net_tap_info
, peer
, model
, name
);
364 s
= DO_UPCAST(TAPState
, nc
, nc
);
367 s
->host_vnet_hdr_len
= vnet_hdr
? sizeof(struct virtio_net_hdr
) : 0;
368 s
->using_vnet_hdr
= false;
369 s
->has_ufo
= tap_probe_has_ufo(s
->fd
);
371 tap_set_offload(&s
->nc
, 0, 0, 0, 0, 0);
373 * Make sure host header length is set correctly in tap:
374 * it might have been modified by another instance of qemu.
376 if (tap_probe_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
)) {
377 tap_fd_set_vnet_hdr_len(s
->fd
, s
->host_vnet_hdr_len
);
379 tap_read_poll(s
, true);
384 static void launch_script(const char *setup_script
, const char *ifname
,
385 int fd
, Error
**errp
)
391 /* try to launch network script */
394 error_setg_errno(errp
, errno
, "could not launch network script %s",
399 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
401 for (i
= 3; i
< open_max
; i
++) {
407 *parg
++ = (char *)setup_script
;
408 *parg
++ = (char *)ifname
;
410 execv(setup_script
, args
);
413 while (waitpid(pid
, &status
, 0) != pid
) {
417 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
420 error_setg(errp
, "network script %s failed with status %d",
421 setup_script
, status
);
425 static int recv_fd(int c
)
428 uint8_t msgbuf
[CMSG_SPACE(sizeof(fd
))];
429 struct msghdr msg
= {
430 .msg_control
= msgbuf
,
431 .msg_controllen
= sizeof(msgbuf
),
433 struct cmsghdr
*cmsg
;
438 cmsg
= CMSG_FIRSTHDR(&msg
);
439 cmsg
->cmsg_level
= SOL_SOCKET
;
440 cmsg
->cmsg_type
= SCM_RIGHTS
;
441 cmsg
->cmsg_len
= CMSG_LEN(sizeof(fd
));
442 msg
.msg_controllen
= cmsg
->cmsg_len
;
445 iov
.iov_len
= sizeof(req
);
450 len
= recvmsg(c
, &msg
, 0);
452 memcpy(&fd
, CMSG_DATA(cmsg
), sizeof(fd
));
459 static int net_bridge_run_helper(const char *helper
, const char *bridge
,
462 sigset_t oldmask
, mask
;
469 sigaddset(&mask
, SIGCHLD
);
470 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
472 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
) == -1) {
473 error_setg_errno(errp
, errno
, "socketpair() failed");
477 /* try to launch bridge helper */
480 error_setg_errno(errp
, errno
, "Can't fork bridge helper");
484 int open_max
= sysconf(_SC_OPEN_MAX
), i
;
486 char br_buf
[6+IFNAMSIZ
] = {0};
487 char helper_cmd
[PATH_MAX
+ sizeof(fd_buf
) + sizeof(br_buf
) + 15];
489 for (i
= 3; i
< open_max
; i
++) {
495 snprintf(fd_buf
, sizeof(fd_buf
), "%s%d", "--fd=", sv
[1]);
497 if (strrchr(helper
, ' ') || strrchr(helper
, '\t')) {
498 /* assume helper is a command */
500 if (strstr(helper
, "--br=") == NULL
) {
501 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
504 snprintf(helper_cmd
, sizeof(helper_cmd
), "%s %s %s %s",
505 helper
, "--use-vnet", fd_buf
, br_buf
);
508 *parg
++ = (char *)"sh";
509 *parg
++ = (char *)"-c";
510 *parg
++ = helper_cmd
;
513 execv("/bin/sh", args
);
515 /* assume helper is just the executable path name */
517 snprintf(br_buf
, sizeof(br_buf
), "%s%s", "--br=", bridge
);
520 *parg
++ = (char *)helper
;
521 *parg
++ = (char *)"--use-vnet";
538 } while (fd
== -1 && errno
== EINTR
);
543 while (waitpid(pid
, &status
, 0) != pid
) {
546 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
548 error_setg_errno(errp
, saved_errno
,
549 "failed to recv file descriptor");
552 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
553 error_setg(errp
, "bridge helper failed");
560 int net_init_bridge(const NetClientOptions
*opts
, const char *name
,
561 NetClientState
*peer
, Error
**errp
)
563 const NetdevBridgeOptions
*bridge
;
564 const char *helper
, *br
;
568 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_BRIDGE
);
569 bridge
= opts
->u
.bridge
;
571 helper
= bridge
->has_helper
? bridge
->helper
: DEFAULT_BRIDGE_HELPER
;
572 br
= bridge
->has_br
? bridge
->br
: DEFAULT_BRIDGE_INTERFACE
;
574 fd
= net_bridge_run_helper(helper
, br
, errp
);
579 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
580 vnet_hdr
= tap_probe_vnet_hdr(fd
);
581 s
= net_tap_fd_init(peer
, "bridge", name
, fd
, vnet_hdr
);
583 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s,br=%s", helper
,
589 static int net_tap_init(const NetdevTapOptions
*tap
, int *vnet_hdr
,
590 const char *setup_script
, char *ifname
,
591 size_t ifname_sz
, int mq_required
, Error
**errp
)
594 int fd
, vnet_hdr_required
;
596 if (tap
->has_vnet_hdr
) {
597 *vnet_hdr
= tap
->vnet_hdr
;
598 vnet_hdr_required
= *vnet_hdr
;
601 vnet_hdr_required
= 0;
604 TFR(fd
= tap_open(ifname
, ifname_sz
, vnet_hdr
, vnet_hdr_required
,
611 setup_script
[0] != '\0' &&
612 strcmp(setup_script
, "no") != 0) {
613 launch_script(setup_script
, ifname
, fd
, &err
);
615 error_propagate(errp
, err
);
624 #define MAX_TAP_QUEUES 1024
626 static void net_init_tap_one(const NetdevTapOptions
*tap
, NetClientState
*peer
,
627 const char *model
, const char *name
,
628 const char *ifname
, const char *script
,
629 const char *downscript
, const char *vhostfdname
,
630 int vnet_hdr
, int fd
, Error
**errp
)
633 TAPState
*s
= net_tap_fd_init(peer
, model
, name
, fd
, vnet_hdr
);
636 tap_set_sndbuf(s
->fd
, tap
, &err
);
638 error_propagate(errp
, err
);
642 if (tap
->has_fd
|| tap
->has_fds
) {
643 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "fd=%d", fd
);
644 } else if (tap
->has_helper
) {
645 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
), "helper=%s",
648 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
649 "ifname=%s,script=%s,downscript=%s", ifname
, script
,
652 if (strcmp(downscript
, "no") != 0) {
653 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", downscript
);
654 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
),
659 if (tap
->has_vhost
? tap
->vhost
:
660 vhostfdname
|| (tap
->has_vhostforce
&& tap
->vhostforce
)) {
661 VhostNetOptions options
;
663 options
.backend_type
= VHOST_BACKEND_TYPE_KERNEL
;
664 options
.net_backend
= &s
->nc
;
666 if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
667 vhostfd
= monitor_fd_param(cur_mon
, vhostfdname
, &err
);
669 error_propagate(errp
, err
);
673 vhostfd
= open("/dev/vhost-net", O_RDWR
);
675 error_setg_errno(errp
, errno
,
676 "tap: open vhost char device failed");
680 options
.opaque
= (void *)(uintptr_t)vhostfd
;
682 s
->vhost_net
= vhost_net_init(&options
);
685 "vhost-net requested but could not be initialized");
688 } else if (tap
->has_vhostfd
|| tap
->has_vhostfds
) {
689 error_setg(errp
, "vhostfd= is not valid without vhost");
693 static int get_fds(char *str
, char *fds
[], int max
)
695 char *ptr
= str
, *this;
696 size_t len
= strlen(str
);
699 while (i
< max
&& ptr
< str
+ len
) {
700 this = strchr(ptr
, ':');
703 fds
[i
] = g_strdup(ptr
);
705 fds
[i
] = g_strndup(ptr
, this - ptr
);
719 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
720 NetClientState
*peer
, Error
**errp
)
722 const NetdevTapOptions
*tap
;
723 int fd
, vnet_hdr
= 0, i
= 0, queues
;
724 /* for the no-fd, no-helper case */
725 const char *script
= NULL
; /* suppress wrong "uninit'd use" gcc warning */
726 const char *downscript
= NULL
;
728 const char *vhostfdname
;
731 assert(opts
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
733 queues
= tap
->has_queues
? tap
->queues
: 1;
734 vhostfdname
= tap
->has_vhostfd
? tap
->vhostfd
: NULL
;
736 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
737 * For -netdev, peer is always NULL. */
738 if (peer
&& (tap
->has_queues
|| tap
->has_fds
|| tap
->has_vhostfds
)) {
739 error_setg(errp
, "Multiqueue tap cannot be used with QEMU vlans");
744 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
745 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
746 tap
->has_fds
|| tap
->has_vhostfds
) {
747 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
748 "helper=, queues=, fds=, and vhostfds= "
749 "are invalid with fd=");
753 fd
= monitor_fd_param(cur_mon
, tap
->fd
, &err
);
755 error_propagate(errp
, err
);
759 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
761 vnet_hdr
= tap_probe_vnet_hdr(fd
);
763 net_init_tap_one(tap
, peer
, "tap", name
, NULL
,
765 vhostfdname
, vnet_hdr
, fd
, &err
);
767 error_propagate(errp
, err
);
770 } else if (tap
->has_fds
) {
771 char *fds
[MAX_TAP_QUEUES
];
772 char *vhost_fds
[MAX_TAP_QUEUES
];
775 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
776 tap
->has_vnet_hdr
|| tap
->has_helper
|| tap
->has_queues
||
778 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
779 "helper=, queues=, and vhostfd= "
780 "are invalid with fds=");
784 nfds
= get_fds(tap
->fds
, fds
, MAX_TAP_QUEUES
);
785 if (tap
->has_vhostfds
) {
786 nvhosts
= get_fds(tap
->vhostfds
, vhost_fds
, MAX_TAP_QUEUES
);
787 if (nfds
!= nvhosts
) {
788 error_setg(errp
, "The number of fds passed does not match "
789 "the number of vhostfds passed");
794 for (i
= 0; i
< nfds
; i
++) {
795 fd
= monitor_fd_param(cur_mon
, fds
[i
], &err
);
797 error_propagate(errp
, err
);
801 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
804 vnet_hdr
= tap_probe_vnet_hdr(fd
);
805 } else if (vnet_hdr
!= tap_probe_vnet_hdr(fd
)) {
807 "vnet_hdr not consistent across given tap fds");
811 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
813 tap
->has_vhostfds
? vhost_fds
[i
] : NULL
,
816 error_propagate(errp
, err
);
820 } else if (tap
->has_helper
) {
821 if (tap
->has_ifname
|| tap
->has_script
|| tap
->has_downscript
||
822 tap
->has_vnet_hdr
|| tap
->has_queues
|| tap
->has_vhostfds
) {
823 error_setg(errp
, "ifname=, script=, downscript=, vnet_hdr=, "
824 "queues=, and vhostfds= are invalid with helper=");
828 fd
= net_bridge_run_helper(tap
->helper
, DEFAULT_BRIDGE_INTERFACE
,
834 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
835 vnet_hdr
= tap_probe_vnet_hdr(fd
);
837 net_init_tap_one(tap
, peer
, "bridge", name
, ifname
,
838 script
, downscript
, vhostfdname
,
841 error_propagate(errp
, err
);
846 if (tap
->has_vhostfds
) {
847 error_setg(errp
, "vhostfds= is invalid if fds= wasn't specified");
850 script
= tap
->has_script
? tap
->script
: DEFAULT_NETWORK_SCRIPT
;
851 downscript
= tap
->has_downscript
? tap
->downscript
:
852 DEFAULT_NETWORK_DOWN_SCRIPT
;
854 if (tap
->has_ifname
) {
855 pstrcpy(ifname
, sizeof ifname
, tap
->ifname
);
860 for (i
= 0; i
< queues
; i
++) {
861 fd
= net_tap_init(tap
, &vnet_hdr
, i
>= 1 ? "no" : script
,
862 ifname
, sizeof ifname
, queues
> 1, errp
);
867 if (queues
> 1 && i
== 0 && !tap
->has_ifname
) {
868 if (tap_fd_get_ifname(fd
, ifname
)) {
869 error_setg(errp
, "Fail to get ifname");
875 net_init_tap_one(tap
, peer
, "tap", name
, ifname
,
876 i
>= 1 ? "no" : script
,
877 i
>= 1 ? "no" : downscript
,
878 vhostfdname
, vnet_hdr
, fd
, &err
);
880 error_propagate(errp
, err
);
890 VHostNetState
*tap_get_vhost_net(NetClientState
*nc
)
892 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
893 assert(nc
->info
->type
== NET_CLIENT_OPTIONS_KIND_TAP
);
897 int tap_enable(NetClientState
*nc
)
899 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
905 ret
= tap_fd_enable(s
->fd
);
908 tap_update_fd_handler(s
);
914 int tap_disable(NetClientState
*nc
)
916 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
919 if (s
->enabled
== 0) {
922 ret
= tap_fd_disable(s
->fd
);
924 qemu_purge_queued_packets(nc
);
926 tap_update_fd_handler(s
);