xref: /qemu/net/tap.c (revision 5193e5fbb52a33f1f684b0d42d29a452dfd29e4a)
15281d757SMark McLoughlin /*
25281d757SMark McLoughlin  * QEMU System Emulator
35281d757SMark McLoughlin  *
45281d757SMark McLoughlin  * Copyright (c) 2003-2008 Fabrice Bellard
55281d757SMark McLoughlin  * Copyright (c) 2009 Red Hat, Inc.
65281d757SMark McLoughlin  *
75281d757SMark McLoughlin  * Permission is hereby granted, free of charge, to any person obtaining a copy
85281d757SMark McLoughlin  * of this software and associated documentation files (the "Software"), to deal
95281d757SMark McLoughlin  * in the Software without restriction, including without limitation the rights
105281d757SMark McLoughlin  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
115281d757SMark McLoughlin  * copies of the Software, and to permit persons to whom the Software is
125281d757SMark McLoughlin  * furnished to do so, subject to the following conditions:
135281d757SMark McLoughlin  *
145281d757SMark McLoughlin  * The above copyright notice and this permission notice shall be included in
155281d757SMark McLoughlin  * all copies or substantial portions of the Software.
165281d757SMark McLoughlin  *
175281d757SMark McLoughlin  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185281d757SMark McLoughlin  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195281d757SMark McLoughlin  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
205281d757SMark McLoughlin  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
215281d757SMark McLoughlin  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
225281d757SMark McLoughlin  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
235281d757SMark McLoughlin  * THE SOFTWARE.
245281d757SMark McLoughlin  */
255281d757SMark McLoughlin 
261422e32dSPaolo Bonzini #include "tap_int.h"
275281d757SMark McLoughlin 
285281d757SMark McLoughlin #include "config-host.h"
295281d757SMark McLoughlin 
305281d757SMark McLoughlin #include <sys/ioctl.h>
315281d757SMark McLoughlin #include <sys/stat.h>
325281d757SMark McLoughlin #include <sys/wait.h>
3371f4effcSAlexander Graf #include <sys/socket.h>
345281d757SMark McLoughlin #include <net/if.h>
355281d757SMark McLoughlin 
361422e32dSPaolo Bonzini #include "net/net.h"
37a245fc18SPaolo Bonzini #include "clients.h"
3883c9089eSPaolo Bonzini #include "monitor/monitor.h"
399c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
405281d757SMark McLoughlin #include "qemu-common.h"
411de7afc9SPaolo Bonzini #include "qemu/error-report.h"
425281d757SMark McLoughlin 
431422e32dSPaolo Bonzini #include "net/tap.h"
445281d757SMark McLoughlin 
4582b0d80eSMichael S. Tsirkin #include "hw/vhost_net.h"
4682b0d80eSMichael S. Tsirkin 
475281d757SMark McLoughlin /* Maximum GSO packet size (64k) plus plenty of room for
485281d757SMark McLoughlin  * the ethernet and virtio_net headers
495281d757SMark McLoughlin  */
505281d757SMark McLoughlin #define TAP_BUFSIZE (4096 + 65536)
515281d757SMark McLoughlin 
525281d757SMark McLoughlin typedef struct TAPState {
534e68f7a0SStefan Hajnoczi     NetClientState nc;
545281d757SMark McLoughlin     int fd;
555281d757SMark McLoughlin     char down_script[1024];
565281d757SMark McLoughlin     char down_script_arg[128];
575281d757SMark McLoughlin     uint8_t buf[TAP_BUFSIZE];
58ec45f083SJason Wang     bool read_poll;
59ec45f083SJason Wang     bool write_poll;
60ec45f083SJason Wang     bool using_vnet_hdr;
61ec45f083SJason Wang     bool has_ufo;
6282b0d80eSMichael S. Tsirkin     VHostNetState *vhost_net;
63ef4252b1SMichael S. Tsirkin     unsigned host_vnet_hdr_len;
645281d757SMark McLoughlin } TAPState;
655281d757SMark McLoughlin 
665281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd);
675281d757SMark McLoughlin 
685281d757SMark McLoughlin static int tap_can_send(void *opaque);
695281d757SMark McLoughlin static void tap_send(void *opaque);
705281d757SMark McLoughlin static void tap_writable(void *opaque);
715281d757SMark McLoughlin 
725281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s)
735281d757SMark McLoughlin {
745281d757SMark McLoughlin     qemu_set_fd_handler2(s->fd,
755281d757SMark McLoughlin                          s->read_poll  ? tap_can_send : NULL,
765281d757SMark McLoughlin                          s->read_poll  ? tap_send     : NULL,
775281d757SMark McLoughlin                          s->write_poll ? tap_writable : NULL,
785281d757SMark McLoughlin                          s);
795281d757SMark McLoughlin }
805281d757SMark McLoughlin 
81ec45f083SJason Wang static void tap_read_poll(TAPState *s, bool enable)
825281d757SMark McLoughlin {
83ec45f083SJason Wang     s->read_poll = enable;
845281d757SMark McLoughlin     tap_update_fd_handler(s);
855281d757SMark McLoughlin }
865281d757SMark McLoughlin 
87ec45f083SJason Wang static void tap_write_poll(TAPState *s, bool enable)
885281d757SMark McLoughlin {
89ec45f083SJason Wang     s->write_poll = enable;
905281d757SMark McLoughlin     tap_update_fd_handler(s);
915281d757SMark McLoughlin }
925281d757SMark McLoughlin 
935281d757SMark McLoughlin static void tap_writable(void *opaque)
945281d757SMark McLoughlin {
955281d757SMark McLoughlin     TAPState *s = opaque;
965281d757SMark McLoughlin 
97ec45f083SJason Wang     tap_write_poll(s, false);
985281d757SMark McLoughlin 
993e35ba93SMark McLoughlin     qemu_flush_queued_packets(&s->nc);
1005281d757SMark McLoughlin }
1015281d757SMark McLoughlin 
1025281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
1035281d757SMark McLoughlin {
1045281d757SMark McLoughlin     ssize_t len;
1055281d757SMark McLoughlin 
1065281d757SMark McLoughlin     do {
1075281d757SMark McLoughlin         len = writev(s->fd, iov, iovcnt);
1085281d757SMark McLoughlin     } while (len == -1 && errno == EINTR);
1095281d757SMark McLoughlin 
1105281d757SMark McLoughlin     if (len == -1 && errno == EAGAIN) {
111ec45f083SJason Wang         tap_write_poll(s, true);
1125281d757SMark McLoughlin         return 0;
1135281d757SMark McLoughlin     }
1145281d757SMark McLoughlin 
1155281d757SMark McLoughlin     return len;
1165281d757SMark McLoughlin }
1175281d757SMark McLoughlin 
1184e68f7a0SStefan Hajnoczi static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
1195281d757SMark McLoughlin                                int iovcnt)
1205281d757SMark McLoughlin {
1213e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1225281d757SMark McLoughlin     const struct iovec *iovp = iov;
1235281d757SMark McLoughlin     struct iovec iov_copy[iovcnt + 1];
124ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1255281d757SMark McLoughlin 
126ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1275281d757SMark McLoughlin         iov_copy[0].iov_base = &hdr;
128ef4252b1SMichael S. Tsirkin         iov_copy[0].iov_len =  s->host_vnet_hdr_len;
1295281d757SMark McLoughlin         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
1305281d757SMark McLoughlin         iovp = iov_copy;
1315281d757SMark McLoughlin         iovcnt++;
1325281d757SMark McLoughlin     }
1335281d757SMark McLoughlin 
1345281d757SMark McLoughlin     return tap_write_packet(s, iovp, iovcnt);
1355281d757SMark McLoughlin }
1365281d757SMark McLoughlin 
1374e68f7a0SStefan Hajnoczi static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
1385281d757SMark McLoughlin {
1393e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1405281d757SMark McLoughlin     struct iovec iov[2];
1415281d757SMark McLoughlin     int iovcnt = 0;
142ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1435281d757SMark McLoughlin 
144ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len) {
1455281d757SMark McLoughlin         iov[iovcnt].iov_base = &hdr;
146ef4252b1SMichael S. Tsirkin         iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
1475281d757SMark McLoughlin         iovcnt++;
1485281d757SMark McLoughlin     }
1495281d757SMark McLoughlin 
1505281d757SMark McLoughlin     iov[iovcnt].iov_base = (char *)buf;
1515281d757SMark McLoughlin     iov[iovcnt].iov_len  = size;
1525281d757SMark McLoughlin     iovcnt++;
1535281d757SMark McLoughlin 
1545281d757SMark McLoughlin     return tap_write_packet(s, iov, iovcnt);
1555281d757SMark McLoughlin }
1565281d757SMark McLoughlin 
1574e68f7a0SStefan Hajnoczi static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1585281d757SMark McLoughlin {
1593e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1605281d757SMark McLoughlin     struct iovec iov[1];
1615281d757SMark McLoughlin 
162ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1633e35ba93SMark McLoughlin         return tap_receive_raw(nc, buf, size);
1645281d757SMark McLoughlin     }
1655281d757SMark McLoughlin 
1665281d757SMark McLoughlin     iov[0].iov_base = (char *)buf;
1675281d757SMark McLoughlin     iov[0].iov_len  = size;
1685281d757SMark McLoughlin 
1695281d757SMark McLoughlin     return tap_write_packet(s, iov, 1);
1705281d757SMark McLoughlin }
1715281d757SMark McLoughlin 
1725281d757SMark McLoughlin static int tap_can_send(void *opaque)
1735281d757SMark McLoughlin {
1745281d757SMark McLoughlin     TAPState *s = opaque;
1755281d757SMark McLoughlin 
1763e35ba93SMark McLoughlin     return qemu_can_send_packet(&s->nc);
1775281d757SMark McLoughlin }
1785281d757SMark McLoughlin 
179966ea5ecSMark McLoughlin #ifndef __sun__
180966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1815281d757SMark McLoughlin {
1825281d757SMark McLoughlin     return read(tapfd, buf, maxlen);
1835281d757SMark McLoughlin }
1845281d757SMark McLoughlin #endif
1855281d757SMark McLoughlin 
1864e68f7a0SStefan Hajnoczi static void tap_send_completed(NetClientState *nc, ssize_t len)
1875281d757SMark McLoughlin {
1883e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
189ec45f083SJason Wang     tap_read_poll(s, true);
1905281d757SMark McLoughlin }
1915281d757SMark McLoughlin 
1925281d757SMark McLoughlin static void tap_send(void *opaque)
1935281d757SMark McLoughlin {
1945281d757SMark McLoughlin     TAPState *s = opaque;
195be1636b3SMark McLoughlin     int size;
1965281d757SMark McLoughlin 
1975819c918SMark McLoughlin     do {
1985819c918SMark McLoughlin         uint8_t *buf = s->buf;
1995819c918SMark McLoughlin 
2005281d757SMark McLoughlin         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
2015281d757SMark McLoughlin         if (size <= 0) {
2025819c918SMark McLoughlin             break;
2035281d757SMark McLoughlin         }
2045281d757SMark McLoughlin 
205ef4252b1SMichael S. Tsirkin         if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
206ef4252b1SMichael S. Tsirkin             buf  += s->host_vnet_hdr_len;
207ef4252b1SMichael S. Tsirkin             size -= s->host_vnet_hdr_len;
2085281d757SMark McLoughlin         }
2095281d757SMark McLoughlin 
2103e35ba93SMark McLoughlin         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
2115281d757SMark McLoughlin         if (size == 0) {
212ec45f083SJason Wang             tap_read_poll(s, false);
2135281d757SMark McLoughlin         }
2143e35ba93SMark McLoughlin     } while (size > 0 && qemu_can_send_packet(&s->nc));
2155281d757SMark McLoughlin }
2165281d757SMark McLoughlin 
217ec45f083SJason Wang bool tap_has_ufo(NetClientState *nc)
2185281d757SMark McLoughlin {
2193e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2205281d757SMark McLoughlin 
2212be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2225281d757SMark McLoughlin 
2235281d757SMark McLoughlin     return s->has_ufo;
2245281d757SMark McLoughlin }
2255281d757SMark McLoughlin 
2264e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr(NetClientState *nc)
2275281d757SMark McLoughlin {
2283e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2295281d757SMark McLoughlin 
2302be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2315281d757SMark McLoughlin 
232ef4252b1SMichael S. Tsirkin     return !!s->host_vnet_hdr_len;
2335281d757SMark McLoughlin }
2345281d757SMark McLoughlin 
2354e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr_len(NetClientState *nc, int len)
236445d892fSMichael S. Tsirkin {
237445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
238445d892fSMichael S. Tsirkin 
2392be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
240445d892fSMichael S. Tsirkin 
241445d892fSMichael S. Tsirkin     return tap_probe_vnet_hdr_len(s->fd, len);
242445d892fSMichael S. Tsirkin }
243445d892fSMichael S. Tsirkin 
2444e68f7a0SStefan Hajnoczi void tap_set_vnet_hdr_len(NetClientState *nc, int len)
245445d892fSMichael S. Tsirkin {
246445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
247445d892fSMichael S. Tsirkin 
2482be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
249445d892fSMichael S. Tsirkin     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
250445d892fSMichael S. Tsirkin            len == sizeof(struct virtio_net_hdr));
251445d892fSMichael S. Tsirkin 
252445d892fSMichael S. Tsirkin     tap_fd_set_vnet_hdr_len(s->fd, len);
253445d892fSMichael S. Tsirkin     s->host_vnet_hdr_len = len;
254445d892fSMichael S. Tsirkin }
255445d892fSMichael S. Tsirkin 
256ec45f083SJason Wang void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
2575281d757SMark McLoughlin {
2583e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2595281d757SMark McLoughlin 
2602be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
261ef4252b1SMichael S. Tsirkin     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
2625281d757SMark McLoughlin 
2635281d757SMark McLoughlin     s->using_vnet_hdr = using_vnet_hdr;
2645281d757SMark McLoughlin }
2655281d757SMark McLoughlin 
2664e68f7a0SStefan Hajnoczi void tap_set_offload(NetClientState *nc, int csum, int tso4,
2675281d757SMark McLoughlin                      int tso6, int ecn, int ufo)
2685281d757SMark McLoughlin {
2693e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
27027a6375dSMichael S. Tsirkin     if (s->fd < 0) {
27127a6375dSMichael S. Tsirkin         return;
27227a6375dSMichael S. Tsirkin     }
2735281d757SMark McLoughlin 
27427a6375dSMichael S. Tsirkin     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
2755281d757SMark McLoughlin }
2765281d757SMark McLoughlin 
2774e68f7a0SStefan Hajnoczi static void tap_cleanup(NetClientState *nc)
2785281d757SMark McLoughlin {
2793e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2805281d757SMark McLoughlin 
28182b0d80eSMichael S. Tsirkin     if (s->vhost_net) {
28282b0d80eSMichael S. Tsirkin         vhost_net_cleanup(s->vhost_net);
28343849424SMichael S. Tsirkin         s->vhost_net = NULL;
28482b0d80eSMichael S. Tsirkin     }
28582b0d80eSMichael S. Tsirkin 
2863e35ba93SMark McLoughlin     qemu_purge_queued_packets(nc);
2875281d757SMark McLoughlin 
2885281d757SMark McLoughlin     if (s->down_script[0])
2895281d757SMark McLoughlin         launch_script(s->down_script, s->down_script_arg, s->fd);
2905281d757SMark McLoughlin 
291ec45f083SJason Wang     tap_read_poll(s, false);
292ec45f083SJason Wang     tap_write_poll(s, false);
2935281d757SMark McLoughlin     close(s->fd);
29427a6375dSMichael S. Tsirkin     s->fd = -1;
2955281d757SMark McLoughlin }
2965281d757SMark McLoughlin 
2974e68f7a0SStefan Hajnoczi static void tap_poll(NetClientState *nc, bool enable)
298ceb69615SMichael S. Tsirkin {
299ceb69615SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
300ceb69615SMichael S. Tsirkin     tap_read_poll(s, enable);
301ceb69615SMichael S. Tsirkin     tap_write_poll(s, enable);
302ceb69615SMichael S. Tsirkin }
303ceb69615SMichael S. Tsirkin 
3044e68f7a0SStefan Hajnoczi int tap_get_fd(NetClientState *nc)
30595d528a2SMichael S. Tsirkin {
30695d528a2SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
3072be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
30895d528a2SMichael S. Tsirkin     return s->fd;
30995d528a2SMichael S. Tsirkin }
31095d528a2SMichael S. Tsirkin 
3115281d757SMark McLoughlin /* fd support */
3125281d757SMark McLoughlin 
3133e35ba93SMark McLoughlin static NetClientInfo net_tap_info = {
3142be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_TAP,
3153e35ba93SMark McLoughlin     .size = sizeof(TAPState),
3163e35ba93SMark McLoughlin     .receive = tap_receive,
3173e35ba93SMark McLoughlin     .receive_raw = tap_receive_raw,
3183e35ba93SMark McLoughlin     .receive_iov = tap_receive_iov,
319ceb69615SMichael S. Tsirkin     .poll = tap_poll,
3203e35ba93SMark McLoughlin     .cleanup = tap_cleanup,
3213e35ba93SMark McLoughlin };
3223e35ba93SMark McLoughlin 
3234e68f7a0SStefan Hajnoczi static TAPState *net_tap_fd_init(NetClientState *peer,
3245281d757SMark McLoughlin                                  const char *model,
3255281d757SMark McLoughlin                                  const char *name,
3265281d757SMark McLoughlin                                  int fd,
3275281d757SMark McLoughlin                                  int vnet_hdr)
3285281d757SMark McLoughlin {
3294e68f7a0SStefan Hajnoczi     NetClientState *nc;
3305281d757SMark McLoughlin     TAPState *s;
3315281d757SMark McLoughlin 
332ab5f3f84SStefan Hajnoczi     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
3333e35ba93SMark McLoughlin 
3343e35ba93SMark McLoughlin     s = DO_UPCAST(TAPState, nc, nc);
3353e35ba93SMark McLoughlin 
3365281d757SMark McLoughlin     s->fd = fd;
337ef4252b1SMichael S. Tsirkin     s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
338ec45f083SJason Wang     s->using_vnet_hdr = false;
3399c282718SMark McLoughlin     s->has_ufo = tap_probe_has_ufo(s->fd);
3403e35ba93SMark McLoughlin     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
34158ddcd50SMichael S. Tsirkin     /*
34258ddcd50SMichael S. Tsirkin      * Make sure host header length is set correctly in tap:
34358ddcd50SMichael S. Tsirkin      * it might have been modified by another instance of qemu.
34458ddcd50SMichael S. Tsirkin      */
34558ddcd50SMichael S. Tsirkin     if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
34658ddcd50SMichael S. Tsirkin         tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
34758ddcd50SMichael S. Tsirkin     }
348ec45f083SJason Wang     tap_read_poll(s, true);
34982b0d80eSMichael S. Tsirkin     s->vhost_net = NULL;
3505281d757SMark McLoughlin     return s;
3515281d757SMark McLoughlin }
3525281d757SMark McLoughlin 
3535281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd)
3545281d757SMark McLoughlin {
3555281d757SMark McLoughlin     int pid, status;
3565281d757SMark McLoughlin     char *args[3];
3575281d757SMark McLoughlin     char **parg;
3585281d757SMark McLoughlin 
3595281d757SMark McLoughlin     /* try to launch network script */
3605281d757SMark McLoughlin     pid = fork();
3615281d757SMark McLoughlin     if (pid == 0) {
3625281d757SMark McLoughlin         int open_max = sysconf(_SC_OPEN_MAX), i;
3635281d757SMark McLoughlin 
3645281d757SMark McLoughlin         for (i = 0; i < open_max; i++) {
3655281d757SMark McLoughlin             if (i != STDIN_FILENO &&
3665281d757SMark McLoughlin                 i != STDOUT_FILENO &&
3675281d757SMark McLoughlin                 i != STDERR_FILENO &&
3685281d757SMark McLoughlin                 i != fd) {
3695281d757SMark McLoughlin                 close(i);
3705281d757SMark McLoughlin             }
3715281d757SMark McLoughlin         }
3725281d757SMark McLoughlin         parg = args;
3735281d757SMark McLoughlin         *parg++ = (char *)setup_script;
3745281d757SMark McLoughlin         *parg++ = (char *)ifname;
3759678d950SBlue Swirl         *parg = NULL;
3765281d757SMark McLoughlin         execv(setup_script, args);
3775281d757SMark McLoughlin         _exit(1);
3785281d757SMark McLoughlin     } else if (pid > 0) {
3795281d757SMark McLoughlin         while (waitpid(pid, &status, 0) != pid) {
3805281d757SMark McLoughlin             /* loop */
3815281d757SMark McLoughlin         }
3825281d757SMark McLoughlin 
3835281d757SMark McLoughlin         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
3845281d757SMark McLoughlin             return 0;
3855281d757SMark McLoughlin         }
3865281d757SMark McLoughlin     }
3875281d757SMark McLoughlin     fprintf(stderr, "%s: could not launch network script\n", setup_script);
3885281d757SMark McLoughlin     return -1;
3895281d757SMark McLoughlin }
3905281d757SMark McLoughlin 
391a7c36ee4SCorey Bryant static int recv_fd(int c)
392a7c36ee4SCorey Bryant {
393a7c36ee4SCorey Bryant     int fd;
394a7c36ee4SCorey Bryant     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
395a7c36ee4SCorey Bryant     struct msghdr msg = {
396a7c36ee4SCorey Bryant         .msg_control = msgbuf,
397a7c36ee4SCorey Bryant         .msg_controllen = sizeof(msgbuf),
398a7c36ee4SCorey Bryant     };
399a7c36ee4SCorey Bryant     struct cmsghdr *cmsg;
400a7c36ee4SCorey Bryant     struct iovec iov;
401a7c36ee4SCorey Bryant     uint8_t req[1];
402a7c36ee4SCorey Bryant     ssize_t len;
403a7c36ee4SCorey Bryant 
404a7c36ee4SCorey Bryant     cmsg = CMSG_FIRSTHDR(&msg);
405a7c36ee4SCorey Bryant     cmsg->cmsg_level = SOL_SOCKET;
406a7c36ee4SCorey Bryant     cmsg->cmsg_type = SCM_RIGHTS;
407a7c36ee4SCorey Bryant     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
408a7c36ee4SCorey Bryant     msg.msg_controllen = cmsg->cmsg_len;
409a7c36ee4SCorey Bryant 
410a7c36ee4SCorey Bryant     iov.iov_base = req;
411a7c36ee4SCorey Bryant     iov.iov_len = sizeof(req);
412a7c36ee4SCorey Bryant 
413a7c36ee4SCorey Bryant     msg.msg_iov = &iov;
414a7c36ee4SCorey Bryant     msg.msg_iovlen = 1;
415a7c36ee4SCorey Bryant 
416a7c36ee4SCorey Bryant     len = recvmsg(c, &msg, 0);
417a7c36ee4SCorey Bryant     if (len > 0) {
418a7c36ee4SCorey Bryant         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
419a7c36ee4SCorey Bryant         return fd;
420a7c36ee4SCorey Bryant     }
421a7c36ee4SCorey Bryant 
422a7c36ee4SCorey Bryant     return len;
423a7c36ee4SCorey Bryant }
424a7c36ee4SCorey Bryant 
425a7c36ee4SCorey Bryant static int net_bridge_run_helper(const char *helper, const char *bridge)
426a7c36ee4SCorey Bryant {
427a7c36ee4SCorey Bryant     sigset_t oldmask, mask;
428a7c36ee4SCorey Bryant     int pid, status;
429a7c36ee4SCorey Bryant     char *args[5];
430a7c36ee4SCorey Bryant     char **parg;
431a7c36ee4SCorey Bryant     int sv[2];
432a7c36ee4SCorey Bryant 
433a7c36ee4SCorey Bryant     sigemptyset(&mask);
434a7c36ee4SCorey Bryant     sigaddset(&mask, SIGCHLD);
435a7c36ee4SCorey Bryant     sigprocmask(SIG_BLOCK, &mask, &oldmask);
436a7c36ee4SCorey Bryant 
437a7c36ee4SCorey Bryant     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
438a7c36ee4SCorey Bryant         return -1;
439a7c36ee4SCorey Bryant     }
440a7c36ee4SCorey Bryant 
441a7c36ee4SCorey Bryant     /* try to launch bridge helper */
442a7c36ee4SCorey Bryant     pid = fork();
443a7c36ee4SCorey Bryant     if (pid == 0) {
444a7c36ee4SCorey Bryant         int open_max = sysconf(_SC_OPEN_MAX), i;
445a7c36ee4SCorey Bryant         char fd_buf[6+10];
446a7c36ee4SCorey Bryant         char br_buf[6+IFNAMSIZ] = {0};
447a7c36ee4SCorey Bryant         char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
448a7c36ee4SCorey Bryant 
449a7c36ee4SCorey Bryant         for (i = 0; i < open_max; i++) {
450a7c36ee4SCorey Bryant             if (i != STDIN_FILENO &&
451a7c36ee4SCorey Bryant                 i != STDOUT_FILENO &&
452a7c36ee4SCorey Bryant                 i != STDERR_FILENO &&
453a7c36ee4SCorey Bryant                 i != sv[1]) {
454a7c36ee4SCorey Bryant                 close(i);
455a7c36ee4SCorey Bryant             }
456a7c36ee4SCorey Bryant         }
457a7c36ee4SCorey Bryant 
458a7c36ee4SCorey Bryant         snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
459a7c36ee4SCorey Bryant 
460a7c36ee4SCorey Bryant         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
461a7c36ee4SCorey Bryant             /* assume helper is a command */
462a7c36ee4SCorey Bryant 
463a7c36ee4SCorey Bryant             if (strstr(helper, "--br=") == NULL) {
464a7c36ee4SCorey Bryant                 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
465a7c36ee4SCorey Bryant             }
466a7c36ee4SCorey Bryant 
467a7c36ee4SCorey Bryant             snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
468a7c36ee4SCorey Bryant                      helper, "--use-vnet", fd_buf, br_buf);
469a7c36ee4SCorey Bryant 
470a7c36ee4SCorey Bryant             parg = args;
471a7c36ee4SCorey Bryant             *parg++ = (char *)"sh";
472a7c36ee4SCorey Bryant             *parg++ = (char *)"-c";
473a7c36ee4SCorey Bryant             *parg++ = helper_cmd;
474a7c36ee4SCorey Bryant             *parg++ = NULL;
475a7c36ee4SCorey Bryant 
476a7c36ee4SCorey Bryant             execv("/bin/sh", args);
477a7c36ee4SCorey Bryant         } else {
478a7c36ee4SCorey Bryant             /* assume helper is just the executable path name */
479a7c36ee4SCorey Bryant 
480a7c36ee4SCorey Bryant             snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
481a7c36ee4SCorey Bryant 
482a7c36ee4SCorey Bryant             parg = args;
483a7c36ee4SCorey Bryant             *parg++ = (char *)helper;
484a7c36ee4SCorey Bryant             *parg++ = (char *)"--use-vnet";
485a7c36ee4SCorey Bryant             *parg++ = fd_buf;
486a7c36ee4SCorey Bryant             *parg++ = br_buf;
487a7c36ee4SCorey Bryant             *parg++ = NULL;
488a7c36ee4SCorey Bryant 
489a7c36ee4SCorey Bryant             execv(helper, args);
490a7c36ee4SCorey Bryant         }
491a7c36ee4SCorey Bryant         _exit(1);
492a7c36ee4SCorey Bryant 
493a7c36ee4SCorey Bryant     } else if (pid > 0) {
494a7c36ee4SCorey Bryant         int fd;
495a7c36ee4SCorey Bryant 
496a7c36ee4SCorey Bryant         close(sv[1]);
497a7c36ee4SCorey Bryant 
498a7c36ee4SCorey Bryant         do {
499a7c36ee4SCorey Bryant             fd = recv_fd(sv[0]);
500a7c36ee4SCorey Bryant         } while (fd == -1 && errno == EINTR);
501a7c36ee4SCorey Bryant 
502a7c36ee4SCorey Bryant         close(sv[0]);
503a7c36ee4SCorey Bryant 
504a7c36ee4SCorey Bryant         while (waitpid(pid, &status, 0) != pid) {
505a7c36ee4SCorey Bryant             /* loop */
506a7c36ee4SCorey Bryant         }
507a7c36ee4SCorey Bryant         sigprocmask(SIG_SETMASK, &oldmask, NULL);
508a7c36ee4SCorey Bryant         if (fd < 0) {
509a7c36ee4SCorey Bryant             fprintf(stderr, "failed to recv file descriptor\n");
510a7c36ee4SCorey Bryant             return -1;
511a7c36ee4SCorey Bryant         }
512a7c36ee4SCorey Bryant 
513a7c36ee4SCorey Bryant         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
514a7c36ee4SCorey Bryant             return fd;
515a7c36ee4SCorey Bryant         }
516a7c36ee4SCorey Bryant     }
517a7c36ee4SCorey Bryant     fprintf(stderr, "failed to launch bridge helper\n");
518a7c36ee4SCorey Bryant     return -1;
519a7c36ee4SCorey Bryant }
520a7c36ee4SCorey Bryant 
5211a0c0958SLaszlo Ersek int net_init_bridge(const NetClientOptions *opts, const char *name,
5224e68f7a0SStefan Hajnoczi                     NetClientState *peer)
523a7c36ee4SCorey Bryant {
524f79b51b0SLaszlo Ersek     const NetdevBridgeOptions *bridge;
525f79b51b0SLaszlo Ersek     const char *helper, *br;
526f79b51b0SLaszlo Ersek 
527a7c36ee4SCorey Bryant     TAPState *s;
528a7c36ee4SCorey Bryant     int fd, vnet_hdr;
529a7c36ee4SCorey Bryant 
530f79b51b0SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
531f79b51b0SLaszlo Ersek     bridge = opts->bridge;
532a7c36ee4SCorey Bryant 
533f79b51b0SLaszlo Ersek     helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
534f79b51b0SLaszlo Ersek     br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;
535f79b51b0SLaszlo Ersek 
536f79b51b0SLaszlo Ersek     fd = net_bridge_run_helper(helper, br);
537a7c36ee4SCorey Bryant     if (fd == -1) {
538a7c36ee4SCorey Bryant         return -1;
539a7c36ee4SCorey Bryant     }
540a7c36ee4SCorey Bryant 
541a7c36ee4SCorey Bryant     fcntl(fd, F_SETFL, O_NONBLOCK);
542a7c36ee4SCorey Bryant 
543a7c36ee4SCorey Bryant     vnet_hdr = tap_probe_vnet_hdr(fd);
544a7c36ee4SCorey Bryant 
545d33d93b2SStefan Hajnoczi     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
546a7c36ee4SCorey Bryant     if (!s) {
547a7c36ee4SCorey Bryant         close(fd);
548a7c36ee4SCorey Bryant         return -1;
549a7c36ee4SCorey Bryant     }
550a7c36ee4SCorey Bryant 
551f79b51b0SLaszlo Ersek     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
552f79b51b0SLaszlo Ersek              br);
553a7c36ee4SCorey Bryant 
554a7c36ee4SCorey Bryant     return 0;
555a7c36ee4SCorey Bryant }
556a7c36ee4SCorey Bryant 
55708c573a8SLaszlo Ersek static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
55808c573a8SLaszlo Ersek                         const char *setup_script, char *ifname,
55908c573a8SLaszlo Ersek                         size_t ifname_sz)
5605281d757SMark McLoughlin {
5615281d757SMark McLoughlin     int fd, vnet_hdr_required;
5625281d757SMark McLoughlin 
56308c573a8SLaszlo Ersek     if (tap->has_ifname) {
56408c573a8SLaszlo Ersek         pstrcpy(ifname, ifname_sz, tap->ifname);
56508c573a8SLaszlo Ersek     } else {
56608c573a8SLaszlo Ersek         assert(ifname_sz > 0);
56708c573a8SLaszlo Ersek         ifname[0] = '\0';
5685281d757SMark McLoughlin     }
5695281d757SMark McLoughlin 
57008c573a8SLaszlo Ersek     if (tap->has_vnet_hdr) {
57108c573a8SLaszlo Ersek         *vnet_hdr = tap->vnet_hdr;
5725281d757SMark McLoughlin         vnet_hdr_required = *vnet_hdr;
5735281d757SMark McLoughlin     } else {
57408c573a8SLaszlo Ersek         *vnet_hdr = 1;
5755281d757SMark McLoughlin         vnet_hdr_required = 0;
5765281d757SMark McLoughlin     }
5775281d757SMark McLoughlin 
57808c573a8SLaszlo Ersek     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
5795281d757SMark McLoughlin     if (fd < 0) {
5805281d757SMark McLoughlin         return -1;
5815281d757SMark McLoughlin     }
5825281d757SMark McLoughlin 
5835281d757SMark McLoughlin     if (setup_script &&
5845281d757SMark McLoughlin         setup_script[0] != '\0' &&
5855281d757SMark McLoughlin         strcmp(setup_script, "no") != 0 &&
5865281d757SMark McLoughlin         launch_script(setup_script, ifname, fd)) {
5875281d757SMark McLoughlin         close(fd);
5885281d757SMark McLoughlin         return -1;
5895281d757SMark McLoughlin     }
5905281d757SMark McLoughlin 
5915281d757SMark McLoughlin     return fd;
5925281d757SMark McLoughlin }
5935281d757SMark McLoughlin 
594*5193e5fbSJason Wang static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
595*5193e5fbSJason Wang                             const char *model, const char *name,
596*5193e5fbSJason Wang                             const char *ifname, const char *script,
597*5193e5fbSJason Wang                             const char *downscript, const char *vhostfdname,
598*5193e5fbSJason Wang                             int vnet_hdr, int fd)
599*5193e5fbSJason Wang {
600*5193e5fbSJason Wang     TAPState *s;
601*5193e5fbSJason Wang 
602*5193e5fbSJason Wang     s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
603*5193e5fbSJason Wang     if (!s) {
604*5193e5fbSJason Wang         close(fd);
605*5193e5fbSJason Wang         return -1;
606*5193e5fbSJason Wang     }
607*5193e5fbSJason Wang 
608*5193e5fbSJason Wang     if (tap_set_sndbuf(s->fd, tap) < 0) {
609*5193e5fbSJason Wang         return -1;
610*5193e5fbSJason Wang     }
611*5193e5fbSJason Wang 
612*5193e5fbSJason Wang     if (tap->has_fd) {
613*5193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
614*5193e5fbSJason Wang     } else if (tap->has_helper) {
615*5193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
616*5193e5fbSJason Wang                  tap->helper);
617*5193e5fbSJason Wang     } else {
618*5193e5fbSJason Wang         const char *downscript;
619*5193e5fbSJason Wang 
620*5193e5fbSJason Wang         downscript = tap->has_downscript ? tap->downscript :
621*5193e5fbSJason Wang             DEFAULT_NETWORK_DOWN_SCRIPT;
622*5193e5fbSJason Wang 
623*5193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
624*5193e5fbSJason Wang                  "ifname=%s,script=%s,downscript=%s", ifname, script,
625*5193e5fbSJason Wang                  downscript);
626*5193e5fbSJason Wang 
627*5193e5fbSJason Wang         if (strcmp(downscript, "no") != 0) {
628*5193e5fbSJason Wang             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
629*5193e5fbSJason Wang             snprintf(s->down_script_arg, sizeof(s->down_script_arg),
630*5193e5fbSJason Wang                      "%s", ifname);
631*5193e5fbSJason Wang         }
632*5193e5fbSJason Wang     }
633*5193e5fbSJason Wang 
634*5193e5fbSJason Wang     if (tap->has_vhost ? tap->vhost :
635*5193e5fbSJason Wang         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
636*5193e5fbSJason Wang         int vhostfd;
637*5193e5fbSJason Wang 
638*5193e5fbSJason Wang         if (tap->has_vhostfd) {
639*5193e5fbSJason Wang             vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
640*5193e5fbSJason Wang             if (vhostfd == -1) {
641*5193e5fbSJason Wang                 return -1;
642*5193e5fbSJason Wang             }
643*5193e5fbSJason Wang         } else {
644*5193e5fbSJason Wang             vhostfd = -1;
645*5193e5fbSJason Wang         }
646*5193e5fbSJason Wang 
647*5193e5fbSJason Wang         s->vhost_net = vhost_net_init(&s->nc, vhostfd,
648*5193e5fbSJason Wang                                       tap->has_vhostforce && tap->vhostforce);
649*5193e5fbSJason Wang         if (!s->vhost_net) {
650*5193e5fbSJason Wang             error_report("vhost-net requested but could not be initialized");
651*5193e5fbSJason Wang             return -1;
652*5193e5fbSJason Wang         }
653*5193e5fbSJason Wang     } else if (tap->has_vhostfd) {
654*5193e5fbSJason Wang         error_report("vhostfd= is not valid without vhost");
655*5193e5fbSJason Wang         return -1;
656*5193e5fbSJason Wang     }
657*5193e5fbSJason Wang 
658*5193e5fbSJason Wang     return 0;
659*5193e5fbSJason Wang }
660*5193e5fbSJason Wang 
6611a0c0958SLaszlo Ersek int net_init_tap(const NetClientOptions *opts, const char *name,
6624e68f7a0SStefan Hajnoczi                  NetClientState *peer)
6635281d757SMark McLoughlin {
66408c573a8SLaszlo Ersek     const NetdevTapOptions *tap;
66508c573a8SLaszlo Ersek 
666df6c2a0fSMark McLoughlin     int fd, vnet_hdr = 0;
667a7c36ee4SCorey Bryant     const char *model;
6685281d757SMark McLoughlin 
66908c573a8SLaszlo Ersek     /* for the no-fd, no-helper case */
67008c573a8SLaszlo Ersek     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
671*5193e5fbSJason Wang     const char *downscript = NULL;
67208c573a8SLaszlo Ersek     char ifname[128];
67308c573a8SLaszlo Ersek 
67408c573a8SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
67508c573a8SLaszlo Ersek     tap = opts->tap;
67608c573a8SLaszlo Ersek 
67708c573a8SLaszlo Ersek     if (tap->has_fd) {
67808c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
67908c573a8SLaszlo Ersek             tap->has_vnet_hdr || tap->has_helper) {
680a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, vnet_hdr=, "
681a7c36ee4SCorey Bryant                          "and helper= are invalid with fd=");
6825281d757SMark McLoughlin             return -1;
6835281d757SMark McLoughlin         }
6845281d757SMark McLoughlin 
685a96ed02fSNicholas Bellinger         fd = monitor_handle_fd_param(cur_mon, tap->fd);
6865281d757SMark McLoughlin         if (fd == -1) {
6875281d757SMark McLoughlin             return -1;
6885281d757SMark McLoughlin         }
6895281d757SMark McLoughlin 
6905281d757SMark McLoughlin         fcntl(fd, F_SETFL, O_NONBLOCK);
6915281d757SMark McLoughlin 
6925281d757SMark McLoughlin         vnet_hdr = tap_probe_vnet_hdr(fd);
693a7c36ee4SCorey Bryant 
694a7c36ee4SCorey Bryant         model = "tap";
695a7c36ee4SCorey Bryant 
69608c573a8SLaszlo Ersek     } else if (tap->has_helper) {
69708c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
69808c573a8SLaszlo Ersek             tap->has_vnet_hdr) {
699a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, and vnet_hdr= "
700a7c36ee4SCorey Bryant                          "are invalid with helper=");
701a7c36ee4SCorey Bryant             return -1;
702a7c36ee4SCorey Bryant         }
703a7c36ee4SCorey Bryant 
70408c573a8SLaszlo Ersek         fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
705a7c36ee4SCorey Bryant         if (fd == -1) {
706a7c36ee4SCorey Bryant             return -1;
707a7c36ee4SCorey Bryant         }
708a7c36ee4SCorey Bryant 
709a7c36ee4SCorey Bryant         fcntl(fd, F_SETFL, O_NONBLOCK);
710a7c36ee4SCorey Bryant 
711a7c36ee4SCorey Bryant         vnet_hdr = tap_probe_vnet_hdr(fd);
712a7c36ee4SCorey Bryant 
713a7c36ee4SCorey Bryant         model = "bridge";
714a7c36ee4SCorey Bryant 
7155281d757SMark McLoughlin     } else {
71608c573a8SLaszlo Ersek         script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
717*5193e5fbSJason Wang         downscript = tap->has_downscript ? tap->downscript :
718*5193e5fbSJason Wang             DEFAULT_NETWORK_DOWN_SCRIPT;
71908c573a8SLaszlo Ersek         fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
720929fe497SJuergen Lock         if (fd == -1) {
721929fe497SJuergen Lock             return -1;
722929fe497SJuergen Lock         }
723a7c36ee4SCorey Bryant 
724a7c36ee4SCorey Bryant         model = "tap";
7255281d757SMark McLoughlin     }
7265281d757SMark McLoughlin 
727*5193e5fbSJason Wang     return net_init_tap_one(tap, peer, model, name, ifname, script,
728*5193e5fbSJason Wang                             downscript, tap->has_vhostfd ? tap->vhostfd : NULL,
729*5193e5fbSJason Wang                             vnet_hdr, fd);
7305281d757SMark McLoughlin }
731b202554cSMichael S. Tsirkin 
7324e68f7a0SStefan Hajnoczi VHostNetState *tap_get_vhost_net(NetClientState *nc)
733b202554cSMichael S. Tsirkin {
734b202554cSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
7352be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
736b202554cSMichael S. Tsirkin     return s->vhost_net;
737b202554cSMichael S. Tsirkin }
738