xref: /qemu/net/tap.c (revision ce675a7579fea498397c5d2da3c5367671e9f02a)
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;
6216dbaf90SJason Wang     bool enabled;
6382b0d80eSMichael S. Tsirkin     VHostNetState *vhost_net;
64ef4252b1SMichael S. Tsirkin     unsigned host_vnet_hdr_len;
655281d757SMark McLoughlin } TAPState;
665281d757SMark McLoughlin 
675281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd);
685281d757SMark McLoughlin 
695281d757SMark McLoughlin static int tap_can_send(void *opaque);
705281d757SMark McLoughlin static void tap_send(void *opaque);
715281d757SMark McLoughlin static void tap_writable(void *opaque);
725281d757SMark McLoughlin 
735281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s)
745281d757SMark McLoughlin {
755281d757SMark McLoughlin     qemu_set_fd_handler2(s->fd,
7616dbaf90SJason Wang                          s->read_poll && s->enabled ? tap_can_send : NULL,
7716dbaf90SJason Wang                          s->read_poll && s->enabled ? tap_send     : NULL,
7816dbaf90SJason Wang                          s->write_poll && s->enabled ? tap_writable : NULL,
795281d757SMark McLoughlin                          s);
805281d757SMark McLoughlin }
815281d757SMark McLoughlin 
82ec45f083SJason Wang static void tap_read_poll(TAPState *s, bool enable)
835281d757SMark McLoughlin {
84ec45f083SJason Wang     s->read_poll = enable;
855281d757SMark McLoughlin     tap_update_fd_handler(s);
865281d757SMark McLoughlin }
875281d757SMark McLoughlin 
88ec45f083SJason Wang static void tap_write_poll(TAPState *s, bool enable)
895281d757SMark McLoughlin {
90ec45f083SJason Wang     s->write_poll = enable;
915281d757SMark McLoughlin     tap_update_fd_handler(s);
925281d757SMark McLoughlin }
935281d757SMark McLoughlin 
945281d757SMark McLoughlin static void tap_writable(void *opaque)
955281d757SMark McLoughlin {
965281d757SMark McLoughlin     TAPState *s = opaque;
975281d757SMark McLoughlin 
98ec45f083SJason Wang     tap_write_poll(s, false);
995281d757SMark McLoughlin 
1003e35ba93SMark McLoughlin     qemu_flush_queued_packets(&s->nc);
1015281d757SMark McLoughlin }
1025281d757SMark McLoughlin 
1035281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
1045281d757SMark McLoughlin {
1055281d757SMark McLoughlin     ssize_t len;
1065281d757SMark McLoughlin 
1075281d757SMark McLoughlin     do {
1085281d757SMark McLoughlin         len = writev(s->fd, iov, iovcnt);
1095281d757SMark McLoughlin     } while (len == -1 && errno == EINTR);
1105281d757SMark McLoughlin 
1115281d757SMark McLoughlin     if (len == -1 && errno == EAGAIN) {
112ec45f083SJason Wang         tap_write_poll(s, true);
1135281d757SMark McLoughlin         return 0;
1145281d757SMark McLoughlin     }
1155281d757SMark McLoughlin 
1165281d757SMark McLoughlin     return len;
1175281d757SMark McLoughlin }
1185281d757SMark McLoughlin 
1194e68f7a0SStefan Hajnoczi static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
1205281d757SMark McLoughlin                                int iovcnt)
1215281d757SMark McLoughlin {
1223e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1235281d757SMark McLoughlin     const struct iovec *iovp = iov;
1245281d757SMark McLoughlin     struct iovec iov_copy[iovcnt + 1];
125ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1265281d757SMark McLoughlin 
127ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1285281d757SMark McLoughlin         iov_copy[0].iov_base = &hdr;
129ef4252b1SMichael S. Tsirkin         iov_copy[0].iov_len =  s->host_vnet_hdr_len;
1305281d757SMark McLoughlin         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
1315281d757SMark McLoughlin         iovp = iov_copy;
1325281d757SMark McLoughlin         iovcnt++;
1335281d757SMark McLoughlin     }
1345281d757SMark McLoughlin 
1355281d757SMark McLoughlin     return tap_write_packet(s, iovp, iovcnt);
1365281d757SMark McLoughlin }
1375281d757SMark McLoughlin 
1384e68f7a0SStefan Hajnoczi static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
1395281d757SMark McLoughlin {
1403e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1415281d757SMark McLoughlin     struct iovec iov[2];
1425281d757SMark McLoughlin     int iovcnt = 0;
143ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1445281d757SMark McLoughlin 
145ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len) {
1465281d757SMark McLoughlin         iov[iovcnt].iov_base = &hdr;
147ef4252b1SMichael S. Tsirkin         iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
1485281d757SMark McLoughlin         iovcnt++;
1495281d757SMark McLoughlin     }
1505281d757SMark McLoughlin 
1515281d757SMark McLoughlin     iov[iovcnt].iov_base = (char *)buf;
1525281d757SMark McLoughlin     iov[iovcnt].iov_len  = size;
1535281d757SMark McLoughlin     iovcnt++;
1545281d757SMark McLoughlin 
1555281d757SMark McLoughlin     return tap_write_packet(s, iov, iovcnt);
1565281d757SMark McLoughlin }
1575281d757SMark McLoughlin 
1584e68f7a0SStefan Hajnoczi static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1595281d757SMark McLoughlin {
1603e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1615281d757SMark McLoughlin     struct iovec iov[1];
1625281d757SMark McLoughlin 
163ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1643e35ba93SMark McLoughlin         return tap_receive_raw(nc, buf, size);
1655281d757SMark McLoughlin     }
1665281d757SMark McLoughlin 
1675281d757SMark McLoughlin     iov[0].iov_base = (char *)buf;
1685281d757SMark McLoughlin     iov[0].iov_len  = size;
1695281d757SMark McLoughlin 
1705281d757SMark McLoughlin     return tap_write_packet(s, iov, 1);
1715281d757SMark McLoughlin }
1725281d757SMark McLoughlin 
1735281d757SMark McLoughlin static int tap_can_send(void *opaque)
1745281d757SMark McLoughlin {
1755281d757SMark McLoughlin     TAPState *s = opaque;
1765281d757SMark McLoughlin 
1773e35ba93SMark McLoughlin     return qemu_can_send_packet(&s->nc);
1785281d757SMark McLoughlin }
1795281d757SMark McLoughlin 
180966ea5ecSMark McLoughlin #ifndef __sun__
181966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1825281d757SMark McLoughlin {
1835281d757SMark McLoughlin     return read(tapfd, buf, maxlen);
1845281d757SMark McLoughlin }
1855281d757SMark McLoughlin #endif
1865281d757SMark McLoughlin 
1874e68f7a0SStefan Hajnoczi static void tap_send_completed(NetClientState *nc, ssize_t len)
1885281d757SMark McLoughlin {
1893e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
190ec45f083SJason Wang     tap_read_poll(s, true);
1915281d757SMark McLoughlin }
1925281d757SMark McLoughlin 
1935281d757SMark McLoughlin static void tap_send(void *opaque)
1945281d757SMark McLoughlin {
1955281d757SMark McLoughlin     TAPState *s = opaque;
196be1636b3SMark McLoughlin     int size;
1975281d757SMark McLoughlin 
1985819c918SMark McLoughlin     do {
1995819c918SMark McLoughlin         uint8_t *buf = s->buf;
2005819c918SMark McLoughlin 
2015281d757SMark McLoughlin         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
2025281d757SMark McLoughlin         if (size <= 0) {
2035819c918SMark McLoughlin             break;
2045281d757SMark McLoughlin         }
2055281d757SMark McLoughlin 
206ef4252b1SMichael S. Tsirkin         if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
207ef4252b1SMichael S. Tsirkin             buf  += s->host_vnet_hdr_len;
208ef4252b1SMichael S. Tsirkin             size -= s->host_vnet_hdr_len;
2095281d757SMark McLoughlin         }
2105281d757SMark McLoughlin 
2113e35ba93SMark McLoughlin         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
2125281d757SMark McLoughlin         if (size == 0) {
213ec45f083SJason Wang             tap_read_poll(s, false);
2145281d757SMark McLoughlin         }
2153e35ba93SMark McLoughlin     } while (size > 0 && qemu_can_send_packet(&s->nc));
2165281d757SMark McLoughlin }
2175281d757SMark McLoughlin 
218ec45f083SJason Wang bool tap_has_ufo(NetClientState *nc)
2195281d757SMark McLoughlin {
2203e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2215281d757SMark McLoughlin 
2222be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2235281d757SMark McLoughlin 
2245281d757SMark McLoughlin     return s->has_ufo;
2255281d757SMark McLoughlin }
2265281d757SMark McLoughlin 
2274e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr(NetClientState *nc)
2285281d757SMark McLoughlin {
2293e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2305281d757SMark McLoughlin 
2312be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2325281d757SMark McLoughlin 
233ef4252b1SMichael S. Tsirkin     return !!s->host_vnet_hdr_len;
2345281d757SMark McLoughlin }
2355281d757SMark McLoughlin 
2364e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr_len(NetClientState *nc, int len)
237445d892fSMichael S. Tsirkin {
238445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
239445d892fSMichael S. Tsirkin 
2402be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
241445d892fSMichael S. Tsirkin 
242445d892fSMichael S. Tsirkin     return tap_probe_vnet_hdr_len(s->fd, len);
243445d892fSMichael S. Tsirkin }
244445d892fSMichael S. Tsirkin 
2454e68f7a0SStefan Hajnoczi void tap_set_vnet_hdr_len(NetClientState *nc, int len)
246445d892fSMichael S. Tsirkin {
247445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
248445d892fSMichael S. Tsirkin 
2492be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
250445d892fSMichael S. Tsirkin     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
251445d892fSMichael S. Tsirkin            len == sizeof(struct virtio_net_hdr));
252445d892fSMichael S. Tsirkin 
253445d892fSMichael S. Tsirkin     tap_fd_set_vnet_hdr_len(s->fd, len);
254445d892fSMichael S. Tsirkin     s->host_vnet_hdr_len = len;
255445d892fSMichael S. Tsirkin }
256445d892fSMichael S. Tsirkin 
257ec45f083SJason Wang void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
2585281d757SMark McLoughlin {
2593e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2605281d757SMark McLoughlin 
2612be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
262ef4252b1SMichael S. Tsirkin     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
2635281d757SMark McLoughlin 
2645281d757SMark McLoughlin     s->using_vnet_hdr = using_vnet_hdr;
2655281d757SMark McLoughlin }
2665281d757SMark McLoughlin 
2674e68f7a0SStefan Hajnoczi void tap_set_offload(NetClientState *nc, int csum, int tso4,
2685281d757SMark McLoughlin                      int tso6, int ecn, int ufo)
2695281d757SMark McLoughlin {
2703e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
27127a6375dSMichael S. Tsirkin     if (s->fd < 0) {
27227a6375dSMichael S. Tsirkin         return;
27327a6375dSMichael S. Tsirkin     }
2745281d757SMark McLoughlin 
27527a6375dSMichael S. Tsirkin     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
2765281d757SMark McLoughlin }
2775281d757SMark McLoughlin 
2784e68f7a0SStefan Hajnoczi static void tap_cleanup(NetClientState *nc)
2795281d757SMark McLoughlin {
2803e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2815281d757SMark McLoughlin 
28282b0d80eSMichael S. Tsirkin     if (s->vhost_net) {
28382b0d80eSMichael S. Tsirkin         vhost_net_cleanup(s->vhost_net);
28443849424SMichael S. Tsirkin         s->vhost_net = NULL;
28582b0d80eSMichael S. Tsirkin     }
28682b0d80eSMichael S. Tsirkin 
2873e35ba93SMark McLoughlin     qemu_purge_queued_packets(nc);
2885281d757SMark McLoughlin 
2895281d757SMark McLoughlin     if (s->down_script[0])
2905281d757SMark McLoughlin         launch_script(s->down_script, s->down_script_arg, s->fd);
2915281d757SMark McLoughlin 
292ec45f083SJason Wang     tap_read_poll(s, false);
293ec45f083SJason Wang     tap_write_poll(s, false);
2945281d757SMark McLoughlin     close(s->fd);
29527a6375dSMichael S. Tsirkin     s->fd = -1;
2965281d757SMark McLoughlin }
2975281d757SMark McLoughlin 
2984e68f7a0SStefan Hajnoczi static void tap_poll(NetClientState *nc, bool enable)
299ceb69615SMichael S. Tsirkin {
300ceb69615SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
301ceb69615SMichael S. Tsirkin     tap_read_poll(s, enable);
302ceb69615SMichael S. Tsirkin     tap_write_poll(s, enable);
303ceb69615SMichael S. Tsirkin }
304ceb69615SMichael S. Tsirkin 
3054e68f7a0SStefan Hajnoczi int tap_get_fd(NetClientState *nc)
30695d528a2SMichael S. Tsirkin {
30795d528a2SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
3082be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
30995d528a2SMichael S. Tsirkin     return s->fd;
31095d528a2SMichael S. Tsirkin }
31195d528a2SMichael S. Tsirkin 
3125281d757SMark McLoughlin /* fd support */
3135281d757SMark McLoughlin 
3143e35ba93SMark McLoughlin static NetClientInfo net_tap_info = {
3152be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_TAP,
3163e35ba93SMark McLoughlin     .size = sizeof(TAPState),
3173e35ba93SMark McLoughlin     .receive = tap_receive,
3183e35ba93SMark McLoughlin     .receive_raw = tap_receive_raw,
3193e35ba93SMark McLoughlin     .receive_iov = tap_receive_iov,
320ceb69615SMichael S. Tsirkin     .poll = tap_poll,
3213e35ba93SMark McLoughlin     .cleanup = tap_cleanup,
3223e35ba93SMark McLoughlin };
3233e35ba93SMark McLoughlin 
3244e68f7a0SStefan Hajnoczi static TAPState *net_tap_fd_init(NetClientState *peer,
3255281d757SMark McLoughlin                                  const char *model,
3265281d757SMark McLoughlin                                  const char *name,
3275281d757SMark McLoughlin                                  int fd,
3285281d757SMark McLoughlin                                  int vnet_hdr)
3295281d757SMark McLoughlin {
3304e68f7a0SStefan Hajnoczi     NetClientState *nc;
3315281d757SMark McLoughlin     TAPState *s;
3325281d757SMark McLoughlin 
333ab5f3f84SStefan Hajnoczi     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
3343e35ba93SMark McLoughlin 
3353e35ba93SMark McLoughlin     s = DO_UPCAST(TAPState, nc, nc);
3363e35ba93SMark McLoughlin 
3375281d757SMark McLoughlin     s->fd = fd;
338ef4252b1SMichael S. Tsirkin     s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
339ec45f083SJason Wang     s->using_vnet_hdr = false;
3409c282718SMark McLoughlin     s->has_ufo = tap_probe_has_ufo(s->fd);
34116dbaf90SJason Wang     s->enabled = true;
3423e35ba93SMark McLoughlin     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
34358ddcd50SMichael S. Tsirkin     /*
34458ddcd50SMichael S. Tsirkin      * Make sure host header length is set correctly in tap:
34558ddcd50SMichael S. Tsirkin      * it might have been modified by another instance of qemu.
34658ddcd50SMichael S. Tsirkin      */
34758ddcd50SMichael S. Tsirkin     if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
34858ddcd50SMichael S. Tsirkin         tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
34958ddcd50SMichael S. Tsirkin     }
350ec45f083SJason Wang     tap_read_poll(s, true);
35182b0d80eSMichael S. Tsirkin     s->vhost_net = NULL;
3525281d757SMark McLoughlin     return s;
3535281d757SMark McLoughlin }
3545281d757SMark McLoughlin 
3555281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd)
3565281d757SMark McLoughlin {
3575281d757SMark McLoughlin     int pid, status;
3585281d757SMark McLoughlin     char *args[3];
3595281d757SMark McLoughlin     char **parg;
3605281d757SMark McLoughlin 
3615281d757SMark McLoughlin     /* try to launch network script */
3625281d757SMark McLoughlin     pid = fork();
3635281d757SMark McLoughlin     if (pid == 0) {
3645281d757SMark McLoughlin         int open_max = sysconf(_SC_OPEN_MAX), i;
3655281d757SMark McLoughlin 
3665281d757SMark McLoughlin         for (i = 0; i < open_max; i++) {
3675281d757SMark McLoughlin             if (i != STDIN_FILENO &&
3685281d757SMark McLoughlin                 i != STDOUT_FILENO &&
3695281d757SMark McLoughlin                 i != STDERR_FILENO &&
3705281d757SMark McLoughlin                 i != fd) {
3715281d757SMark McLoughlin                 close(i);
3725281d757SMark McLoughlin             }
3735281d757SMark McLoughlin         }
3745281d757SMark McLoughlin         parg = args;
3755281d757SMark McLoughlin         *parg++ = (char *)setup_script;
3765281d757SMark McLoughlin         *parg++ = (char *)ifname;
3779678d950SBlue Swirl         *parg = NULL;
3785281d757SMark McLoughlin         execv(setup_script, args);
3795281d757SMark McLoughlin         _exit(1);
3805281d757SMark McLoughlin     } else if (pid > 0) {
3815281d757SMark McLoughlin         while (waitpid(pid, &status, 0) != pid) {
3825281d757SMark McLoughlin             /* loop */
3835281d757SMark McLoughlin         }
3845281d757SMark McLoughlin 
3855281d757SMark McLoughlin         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
3865281d757SMark McLoughlin             return 0;
3875281d757SMark McLoughlin         }
3885281d757SMark McLoughlin     }
3895281d757SMark McLoughlin     fprintf(stderr, "%s: could not launch network script\n", setup_script);
3905281d757SMark McLoughlin     return -1;
3915281d757SMark McLoughlin }
3925281d757SMark McLoughlin 
393a7c36ee4SCorey Bryant static int recv_fd(int c)
394a7c36ee4SCorey Bryant {
395a7c36ee4SCorey Bryant     int fd;
396a7c36ee4SCorey Bryant     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
397a7c36ee4SCorey Bryant     struct msghdr msg = {
398a7c36ee4SCorey Bryant         .msg_control = msgbuf,
399a7c36ee4SCorey Bryant         .msg_controllen = sizeof(msgbuf),
400a7c36ee4SCorey Bryant     };
401a7c36ee4SCorey Bryant     struct cmsghdr *cmsg;
402a7c36ee4SCorey Bryant     struct iovec iov;
403a7c36ee4SCorey Bryant     uint8_t req[1];
404a7c36ee4SCorey Bryant     ssize_t len;
405a7c36ee4SCorey Bryant 
406a7c36ee4SCorey Bryant     cmsg = CMSG_FIRSTHDR(&msg);
407a7c36ee4SCorey Bryant     cmsg->cmsg_level = SOL_SOCKET;
408a7c36ee4SCorey Bryant     cmsg->cmsg_type = SCM_RIGHTS;
409a7c36ee4SCorey Bryant     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
410a7c36ee4SCorey Bryant     msg.msg_controllen = cmsg->cmsg_len;
411a7c36ee4SCorey Bryant 
412a7c36ee4SCorey Bryant     iov.iov_base = req;
413a7c36ee4SCorey Bryant     iov.iov_len = sizeof(req);
414a7c36ee4SCorey Bryant 
415a7c36ee4SCorey Bryant     msg.msg_iov = &iov;
416a7c36ee4SCorey Bryant     msg.msg_iovlen = 1;
417a7c36ee4SCorey Bryant 
418a7c36ee4SCorey Bryant     len = recvmsg(c, &msg, 0);
419a7c36ee4SCorey Bryant     if (len > 0) {
420a7c36ee4SCorey Bryant         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
421a7c36ee4SCorey Bryant         return fd;
422a7c36ee4SCorey Bryant     }
423a7c36ee4SCorey Bryant 
424a7c36ee4SCorey Bryant     return len;
425a7c36ee4SCorey Bryant }
426a7c36ee4SCorey Bryant 
427a7c36ee4SCorey Bryant static int net_bridge_run_helper(const char *helper, const char *bridge)
428a7c36ee4SCorey Bryant {
429a7c36ee4SCorey Bryant     sigset_t oldmask, mask;
430a7c36ee4SCorey Bryant     int pid, status;
431a7c36ee4SCorey Bryant     char *args[5];
432a7c36ee4SCorey Bryant     char **parg;
433a7c36ee4SCorey Bryant     int sv[2];
434a7c36ee4SCorey Bryant 
435a7c36ee4SCorey Bryant     sigemptyset(&mask);
436a7c36ee4SCorey Bryant     sigaddset(&mask, SIGCHLD);
437a7c36ee4SCorey Bryant     sigprocmask(SIG_BLOCK, &mask, &oldmask);
438a7c36ee4SCorey Bryant 
439a7c36ee4SCorey Bryant     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
440a7c36ee4SCorey Bryant         return -1;
441a7c36ee4SCorey Bryant     }
442a7c36ee4SCorey Bryant 
443a7c36ee4SCorey Bryant     /* try to launch bridge helper */
444a7c36ee4SCorey Bryant     pid = fork();
445a7c36ee4SCorey Bryant     if (pid == 0) {
446a7c36ee4SCorey Bryant         int open_max = sysconf(_SC_OPEN_MAX), i;
447a7c36ee4SCorey Bryant         char fd_buf[6+10];
448a7c36ee4SCorey Bryant         char br_buf[6+IFNAMSIZ] = {0};
449a7c36ee4SCorey Bryant         char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
450a7c36ee4SCorey Bryant 
451a7c36ee4SCorey Bryant         for (i = 0; i < open_max; i++) {
452a7c36ee4SCorey Bryant             if (i != STDIN_FILENO &&
453a7c36ee4SCorey Bryant                 i != STDOUT_FILENO &&
454a7c36ee4SCorey Bryant                 i != STDERR_FILENO &&
455a7c36ee4SCorey Bryant                 i != sv[1]) {
456a7c36ee4SCorey Bryant                 close(i);
457a7c36ee4SCorey Bryant             }
458a7c36ee4SCorey Bryant         }
459a7c36ee4SCorey Bryant 
460a7c36ee4SCorey Bryant         snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
461a7c36ee4SCorey Bryant 
462a7c36ee4SCorey Bryant         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
463a7c36ee4SCorey Bryant             /* assume helper is a command */
464a7c36ee4SCorey Bryant 
465a7c36ee4SCorey Bryant             if (strstr(helper, "--br=") == NULL) {
466a7c36ee4SCorey Bryant                 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
467a7c36ee4SCorey Bryant             }
468a7c36ee4SCorey Bryant 
469a7c36ee4SCorey Bryant             snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
470a7c36ee4SCorey Bryant                      helper, "--use-vnet", fd_buf, br_buf);
471a7c36ee4SCorey Bryant 
472a7c36ee4SCorey Bryant             parg = args;
473a7c36ee4SCorey Bryant             *parg++ = (char *)"sh";
474a7c36ee4SCorey Bryant             *parg++ = (char *)"-c";
475a7c36ee4SCorey Bryant             *parg++ = helper_cmd;
476a7c36ee4SCorey Bryant             *parg++ = NULL;
477a7c36ee4SCorey Bryant 
478a7c36ee4SCorey Bryant             execv("/bin/sh", args);
479a7c36ee4SCorey Bryant         } else {
480a7c36ee4SCorey Bryant             /* assume helper is just the executable path name */
481a7c36ee4SCorey Bryant 
482a7c36ee4SCorey Bryant             snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
483a7c36ee4SCorey Bryant 
484a7c36ee4SCorey Bryant             parg = args;
485a7c36ee4SCorey Bryant             *parg++ = (char *)helper;
486a7c36ee4SCorey Bryant             *parg++ = (char *)"--use-vnet";
487a7c36ee4SCorey Bryant             *parg++ = fd_buf;
488a7c36ee4SCorey Bryant             *parg++ = br_buf;
489a7c36ee4SCorey Bryant             *parg++ = NULL;
490a7c36ee4SCorey Bryant 
491a7c36ee4SCorey Bryant             execv(helper, args);
492a7c36ee4SCorey Bryant         }
493a7c36ee4SCorey Bryant         _exit(1);
494a7c36ee4SCorey Bryant 
495a7c36ee4SCorey Bryant     } else if (pid > 0) {
496a7c36ee4SCorey Bryant         int fd;
497a7c36ee4SCorey Bryant 
498a7c36ee4SCorey Bryant         close(sv[1]);
499a7c36ee4SCorey Bryant 
500a7c36ee4SCorey Bryant         do {
501a7c36ee4SCorey Bryant             fd = recv_fd(sv[0]);
502a7c36ee4SCorey Bryant         } while (fd == -1 && errno == EINTR);
503a7c36ee4SCorey Bryant 
504a7c36ee4SCorey Bryant         close(sv[0]);
505a7c36ee4SCorey Bryant 
506a7c36ee4SCorey Bryant         while (waitpid(pid, &status, 0) != pid) {
507a7c36ee4SCorey Bryant             /* loop */
508a7c36ee4SCorey Bryant         }
509a7c36ee4SCorey Bryant         sigprocmask(SIG_SETMASK, &oldmask, NULL);
510a7c36ee4SCorey Bryant         if (fd < 0) {
511a7c36ee4SCorey Bryant             fprintf(stderr, "failed to recv file descriptor\n");
512a7c36ee4SCorey Bryant             return -1;
513a7c36ee4SCorey Bryant         }
514a7c36ee4SCorey Bryant 
515a7c36ee4SCorey Bryant         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
516a7c36ee4SCorey Bryant             return fd;
517a7c36ee4SCorey Bryant         }
518a7c36ee4SCorey Bryant     }
519a7c36ee4SCorey Bryant     fprintf(stderr, "failed to launch bridge helper\n");
520a7c36ee4SCorey Bryant     return -1;
521a7c36ee4SCorey Bryant }
522a7c36ee4SCorey Bryant 
5231a0c0958SLaszlo Ersek int net_init_bridge(const NetClientOptions *opts, const char *name,
5244e68f7a0SStefan Hajnoczi                     NetClientState *peer)
525a7c36ee4SCorey Bryant {
526f79b51b0SLaszlo Ersek     const NetdevBridgeOptions *bridge;
527f79b51b0SLaszlo Ersek     const char *helper, *br;
528f79b51b0SLaszlo Ersek 
529a7c36ee4SCorey Bryant     TAPState *s;
530a7c36ee4SCorey Bryant     int fd, vnet_hdr;
531a7c36ee4SCorey Bryant 
532f79b51b0SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
533f79b51b0SLaszlo Ersek     bridge = opts->bridge;
534a7c36ee4SCorey Bryant 
535f79b51b0SLaszlo Ersek     helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
536f79b51b0SLaszlo Ersek     br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;
537f79b51b0SLaszlo Ersek 
538f79b51b0SLaszlo Ersek     fd = net_bridge_run_helper(helper, br);
539a7c36ee4SCorey Bryant     if (fd == -1) {
540a7c36ee4SCorey Bryant         return -1;
541a7c36ee4SCorey Bryant     }
542a7c36ee4SCorey Bryant 
543a7c36ee4SCorey Bryant     fcntl(fd, F_SETFL, O_NONBLOCK);
544a7c36ee4SCorey Bryant 
545a7c36ee4SCorey Bryant     vnet_hdr = tap_probe_vnet_hdr(fd);
546a7c36ee4SCorey Bryant 
547d33d93b2SStefan Hajnoczi     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
548a7c36ee4SCorey Bryant     if (!s) {
549a7c36ee4SCorey Bryant         close(fd);
550a7c36ee4SCorey Bryant         return -1;
551a7c36ee4SCorey Bryant     }
552a7c36ee4SCorey Bryant 
553f79b51b0SLaszlo Ersek     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
554f79b51b0SLaszlo Ersek              br);
555a7c36ee4SCorey Bryant 
556a7c36ee4SCorey Bryant     return 0;
557a7c36ee4SCorey Bryant }
558a7c36ee4SCorey Bryant 
55908c573a8SLaszlo Ersek static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
56008c573a8SLaszlo Ersek                         const char *setup_script, char *ifname,
561264986e2SJason Wang                         size_t ifname_sz, int mq_required)
5625281d757SMark McLoughlin {
5635281d757SMark McLoughlin     int fd, vnet_hdr_required;
5645281d757SMark McLoughlin 
56508c573a8SLaszlo Ersek     if (tap->has_vnet_hdr) {
56608c573a8SLaszlo Ersek         *vnet_hdr = tap->vnet_hdr;
5675281d757SMark McLoughlin         vnet_hdr_required = *vnet_hdr;
5685281d757SMark McLoughlin     } else {
56908c573a8SLaszlo Ersek         *vnet_hdr = 1;
5705281d757SMark McLoughlin         vnet_hdr_required = 0;
5715281d757SMark McLoughlin     }
5725281d757SMark McLoughlin 
573264986e2SJason Wang     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
574264986e2SJason Wang                       mq_required));
5755281d757SMark McLoughlin     if (fd < 0) {
5765281d757SMark McLoughlin         return -1;
5775281d757SMark McLoughlin     }
5785281d757SMark McLoughlin 
5795281d757SMark McLoughlin     if (setup_script &&
5805281d757SMark McLoughlin         setup_script[0] != '\0' &&
5815281d757SMark McLoughlin         strcmp(setup_script, "no") != 0 &&
5825281d757SMark McLoughlin         launch_script(setup_script, ifname, fd)) {
5835281d757SMark McLoughlin         close(fd);
5845281d757SMark McLoughlin         return -1;
5855281d757SMark McLoughlin     }
5865281d757SMark McLoughlin 
5875281d757SMark McLoughlin     return fd;
5885281d757SMark McLoughlin }
5895281d757SMark McLoughlin 
590264986e2SJason Wang #define MAX_TAP_QUEUES 1024
591264986e2SJason Wang 
5925193e5fbSJason Wang static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
5935193e5fbSJason Wang                             const char *model, const char *name,
5945193e5fbSJason Wang                             const char *ifname, const char *script,
5955193e5fbSJason Wang                             const char *downscript, const char *vhostfdname,
5965193e5fbSJason Wang                             int vnet_hdr, int fd)
5975193e5fbSJason Wang {
5985193e5fbSJason Wang     TAPState *s;
5995193e5fbSJason Wang 
6005193e5fbSJason Wang     s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
6015193e5fbSJason Wang     if (!s) {
6025193e5fbSJason Wang         close(fd);
6035193e5fbSJason Wang         return -1;
6045193e5fbSJason Wang     }
6055193e5fbSJason Wang 
6065193e5fbSJason Wang     if (tap_set_sndbuf(s->fd, tap) < 0) {
6075193e5fbSJason Wang         return -1;
6085193e5fbSJason Wang     }
6095193e5fbSJason Wang 
610264986e2SJason Wang     if (tap->has_fd || tap->has_fds) {
6115193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
6125193e5fbSJason Wang     } else if (tap->has_helper) {
6135193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
6145193e5fbSJason Wang                  tap->helper);
6155193e5fbSJason Wang     } else {
6165193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
6175193e5fbSJason Wang                  "ifname=%s,script=%s,downscript=%s", ifname, script,
6185193e5fbSJason Wang                  downscript);
6195193e5fbSJason Wang 
6205193e5fbSJason Wang         if (strcmp(downscript, "no") != 0) {
6215193e5fbSJason Wang             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
6225193e5fbSJason Wang             snprintf(s->down_script_arg, sizeof(s->down_script_arg),
6235193e5fbSJason Wang                      "%s", ifname);
6245193e5fbSJason Wang         }
6255193e5fbSJason Wang     }
6265193e5fbSJason Wang 
6275193e5fbSJason Wang     if (tap->has_vhost ? tap->vhost :
6285193e5fbSJason Wang         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
6295193e5fbSJason Wang         int vhostfd;
6305193e5fbSJason Wang 
6315193e5fbSJason Wang         if (tap->has_vhostfd) {
6325193e5fbSJason Wang             vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
6335193e5fbSJason Wang             if (vhostfd == -1) {
6345193e5fbSJason Wang                 return -1;
6355193e5fbSJason Wang             }
6365193e5fbSJason Wang         } else {
6375193e5fbSJason Wang             vhostfd = -1;
6385193e5fbSJason Wang         }
6395193e5fbSJason Wang 
6405193e5fbSJason Wang         s->vhost_net = vhost_net_init(&s->nc, vhostfd,
6415193e5fbSJason Wang                                       tap->has_vhostforce && tap->vhostforce);
6425193e5fbSJason Wang         if (!s->vhost_net) {
6435193e5fbSJason Wang             error_report("vhost-net requested but could not be initialized");
6445193e5fbSJason Wang             return -1;
6455193e5fbSJason Wang         }
646264986e2SJason Wang     } else if (tap->has_vhostfd || tap->has_vhostfds) {
6475193e5fbSJason Wang         error_report("vhostfd= is not valid without vhost");
6485193e5fbSJason Wang         return -1;
6495193e5fbSJason Wang     }
6505193e5fbSJason Wang 
6515193e5fbSJason Wang     return 0;
6525193e5fbSJason Wang }
6535193e5fbSJason Wang 
654264986e2SJason Wang static int get_fds(char *str, char *fds[], int max)
655264986e2SJason Wang {
656264986e2SJason Wang     char *ptr = str, *this;
657264986e2SJason Wang     size_t len = strlen(str);
658264986e2SJason Wang     int i = 0;
659264986e2SJason Wang 
660264986e2SJason Wang     while (i < max && ptr < str + len) {
661264986e2SJason Wang         this = strchr(ptr, ':');
662264986e2SJason Wang 
663264986e2SJason Wang         if (this == NULL) {
664264986e2SJason Wang             fds[i] = g_strdup(ptr);
665264986e2SJason Wang         } else {
666264986e2SJason Wang             fds[i] = g_strndup(ptr, this - ptr);
667264986e2SJason Wang         }
668264986e2SJason Wang 
669264986e2SJason Wang         i++;
670264986e2SJason Wang         if (this == NULL) {
671264986e2SJason Wang             break;
672264986e2SJason Wang         } else {
673264986e2SJason Wang             ptr = this + 1;
674264986e2SJason Wang         }
675264986e2SJason Wang     }
676264986e2SJason Wang 
677264986e2SJason Wang     return i;
678264986e2SJason Wang }
679264986e2SJason Wang 
6801a0c0958SLaszlo Ersek int net_init_tap(const NetClientOptions *opts, const char *name,
6814e68f7a0SStefan Hajnoczi                  NetClientState *peer)
6825281d757SMark McLoughlin {
68308c573a8SLaszlo Ersek     const NetdevTapOptions *tap;
684264986e2SJason Wang     int fd, vnet_hdr = 0, i = 0, queues;
68508c573a8SLaszlo Ersek     /* for the no-fd, no-helper case */
68608c573a8SLaszlo Ersek     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
6875193e5fbSJason Wang     const char *downscript = NULL;
688264986e2SJason Wang     const char *vhostfdname;
68908c573a8SLaszlo Ersek     char ifname[128];
69008c573a8SLaszlo Ersek 
69108c573a8SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
69208c573a8SLaszlo Ersek     tap = opts->tap;
693264986e2SJason Wang     queues = tap->has_queues ? tap->queues : 1;
694264986e2SJason Wang     vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
69508c573a8SLaszlo Ersek 
696*ce675a75SJason Wang     /* QEMU vlans does not support multiqueue tap, in this case peer is set.
697*ce675a75SJason Wang      * For -netdev, peer is always NULL. */
698*ce675a75SJason Wang     if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
699*ce675a75SJason Wang         error_report("Multiqueue tap cannnot be used with QEMU vlans");
700*ce675a75SJason Wang         return -1;
701*ce675a75SJason Wang     }
702*ce675a75SJason Wang 
70308c573a8SLaszlo Ersek     if (tap->has_fd) {
70408c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
705264986e2SJason Wang             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
706264986e2SJason Wang             tap->has_fds) {
707a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, vnet_hdr=, "
708264986e2SJason Wang                          "helper=, queues=, and fds= are invalid with fd=");
7095281d757SMark McLoughlin             return -1;
7105281d757SMark McLoughlin         }
7115281d757SMark McLoughlin 
712a96ed02fSNicholas Bellinger         fd = monitor_handle_fd_param(cur_mon, tap->fd);
7135281d757SMark McLoughlin         if (fd == -1) {
7145281d757SMark McLoughlin             return -1;
7155281d757SMark McLoughlin         }
7165281d757SMark McLoughlin 
7175281d757SMark McLoughlin         fcntl(fd, F_SETFL, O_NONBLOCK);
7185281d757SMark McLoughlin 
7195281d757SMark McLoughlin         vnet_hdr = tap_probe_vnet_hdr(fd);
720a7c36ee4SCorey Bryant 
72102cd8090SAnthony Liguori         if (net_init_tap_one(tap, peer, "tap", name, NULL,
722264986e2SJason Wang                              script, downscript,
723264986e2SJason Wang                              vhostfdname, vnet_hdr, fd)) {
724264986e2SJason Wang             return -1;
725264986e2SJason Wang         }
726264986e2SJason Wang     } else if (tap->has_fds) {
727264986e2SJason Wang         char *fds[MAX_TAP_QUEUES];
728264986e2SJason Wang         char *vhost_fds[MAX_TAP_QUEUES];
729264986e2SJason Wang         int nfds, nvhosts;
730a7c36ee4SCorey Bryant 
731264986e2SJason Wang         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
732264986e2SJason Wang             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
733264986e2SJason Wang             tap->has_fd) {
734264986e2SJason Wang             error_report("ifname=, script=, downscript=, vnet_hdr=, "
735264986e2SJason Wang                          "helper=, queues=, and fd= are invalid with fds=");
736264986e2SJason Wang             return -1;
737264986e2SJason Wang         }
738264986e2SJason Wang 
739264986e2SJason Wang         nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
740264986e2SJason Wang         if (tap->has_vhostfds) {
741264986e2SJason Wang             nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
742264986e2SJason Wang             if (nfds != nvhosts) {
743264986e2SJason Wang                 error_report("The number of fds passed does not match the "
744264986e2SJason Wang                              "number of vhostfds passed");
745264986e2SJason Wang                 return -1;
746264986e2SJason Wang             }
747264986e2SJason Wang         }
748264986e2SJason Wang 
749264986e2SJason Wang         for (i = 0; i < nfds; i++) {
750264986e2SJason Wang             fd = monitor_handle_fd_param(cur_mon, fds[i]);
751264986e2SJason Wang             if (fd == -1) {
752264986e2SJason Wang                 return -1;
753264986e2SJason Wang             }
754264986e2SJason Wang 
755264986e2SJason Wang             fcntl(fd, F_SETFL, O_NONBLOCK);
756264986e2SJason Wang 
757264986e2SJason Wang             if (i == 0) {
758264986e2SJason Wang                 vnet_hdr = tap_probe_vnet_hdr(fd);
759264986e2SJason Wang             } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
760264986e2SJason Wang                 error_report("vnet_hdr not consistent across given tap fds");
761264986e2SJason Wang                 return -1;
762264986e2SJason Wang             }
763264986e2SJason Wang 
764264986e2SJason Wang             if (net_init_tap_one(tap, peer, "tap", name, ifname,
765264986e2SJason Wang                                  script, downscript,
766264986e2SJason Wang                                  tap->has_vhostfds ? vhost_fds[i] : NULL,
767264986e2SJason Wang                                  vnet_hdr, fd)) {
768264986e2SJason Wang                 return -1;
769264986e2SJason Wang             }
770264986e2SJason Wang         }
77108c573a8SLaszlo Ersek     } else if (tap->has_helper) {
77208c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
773264986e2SJason Wang             tap->has_vnet_hdr || tap->has_queues || tap->has_fds) {
774a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, and vnet_hdr= "
775264986e2SJason Wang                          "queues=, and fds= are invalid with helper=");
776a7c36ee4SCorey Bryant             return -1;
777a7c36ee4SCorey Bryant         }
778a7c36ee4SCorey Bryant 
77908c573a8SLaszlo Ersek         fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
780a7c36ee4SCorey Bryant         if (fd == -1) {
781a7c36ee4SCorey Bryant             return -1;
782a7c36ee4SCorey Bryant         }
783a7c36ee4SCorey Bryant 
784a7c36ee4SCorey Bryant         fcntl(fd, F_SETFL, O_NONBLOCK);
785a7c36ee4SCorey Bryant         vnet_hdr = tap_probe_vnet_hdr(fd);
786a7c36ee4SCorey Bryant 
787264986e2SJason Wang         if (net_init_tap_one(tap, peer, "bridge", name, ifname,
788264986e2SJason Wang                              script, downscript, vhostfdname,
789264986e2SJason Wang                              vnet_hdr, fd)) {
790264986e2SJason Wang             return -1;
791264986e2SJason Wang         }
7925281d757SMark McLoughlin     } else {
79308c573a8SLaszlo Ersek         script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
7945193e5fbSJason Wang         downscript = tap->has_downscript ? tap->downscript :
7955193e5fbSJason Wang             DEFAULT_NETWORK_DOWN_SCRIPT;
796264986e2SJason Wang 
797264986e2SJason Wang         if (tap->has_ifname) {
798264986e2SJason Wang             pstrcpy(ifname, sizeof ifname, tap->ifname);
799264986e2SJason Wang         } else {
800264986e2SJason Wang             ifname[0] = '\0';
801264986e2SJason Wang         }
802264986e2SJason Wang 
803264986e2SJason Wang         for (i = 0; i < queues; i++) {
804264986e2SJason Wang             fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
805264986e2SJason Wang                               ifname, sizeof ifname, queues > 1);
806929fe497SJuergen Lock             if (fd == -1) {
807929fe497SJuergen Lock                 return -1;
808929fe497SJuergen Lock             }
809a7c36ee4SCorey Bryant 
810264986e2SJason Wang             if (queues > 1 && i == 0 && !tap->has_ifname) {
811264986e2SJason Wang                 if (tap_fd_get_ifname(fd, ifname)) {
812264986e2SJason Wang                     error_report("Fail to get ifname");
813264986e2SJason Wang                     return -1;
814264986e2SJason Wang                 }
8155281d757SMark McLoughlin             }
8165281d757SMark McLoughlin 
817264986e2SJason Wang             if (net_init_tap_one(tap, peer, "tap", name, ifname,
818264986e2SJason Wang                                  i >= 1 ? "no" : script,
819264986e2SJason Wang                                  i >= 1 ? "no" : downscript,
820264986e2SJason Wang                                  vhostfdname, vnet_hdr, fd)) {
821264986e2SJason Wang                 return -1;
822264986e2SJason Wang             }
823264986e2SJason Wang         }
824264986e2SJason Wang     }
825264986e2SJason Wang 
826264986e2SJason Wang     return 0;
8275281d757SMark McLoughlin }
828b202554cSMichael S. Tsirkin 
8294e68f7a0SStefan Hajnoczi VHostNetState *tap_get_vhost_net(NetClientState *nc)
830b202554cSMichael S. Tsirkin {
831b202554cSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
8322be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
833b202554cSMichael S. Tsirkin     return s->vhost_net;
834b202554cSMichael S. Tsirkin }
83516dbaf90SJason Wang 
83616dbaf90SJason Wang int tap_enable(NetClientState *nc)
83716dbaf90SJason Wang {
83816dbaf90SJason Wang     TAPState *s = DO_UPCAST(TAPState, nc, nc);
83916dbaf90SJason Wang     int ret;
84016dbaf90SJason Wang 
84116dbaf90SJason Wang     if (s->enabled) {
84216dbaf90SJason Wang         return 0;
84316dbaf90SJason Wang     } else {
84416dbaf90SJason Wang         ret = tap_fd_enable(s->fd);
84516dbaf90SJason Wang         if (ret == 0) {
84616dbaf90SJason Wang             s->enabled = true;
84716dbaf90SJason Wang             tap_update_fd_handler(s);
84816dbaf90SJason Wang         }
84916dbaf90SJason Wang         return ret;
85016dbaf90SJason Wang     }
85116dbaf90SJason Wang }
85216dbaf90SJason Wang 
85316dbaf90SJason Wang int tap_disable(NetClientState *nc)
85416dbaf90SJason Wang {
85516dbaf90SJason Wang     TAPState *s = DO_UPCAST(TAPState, nc, nc);
85616dbaf90SJason Wang     int ret;
85716dbaf90SJason Wang 
85816dbaf90SJason Wang     if (s->enabled == 0) {
85916dbaf90SJason Wang         return 0;
86016dbaf90SJason Wang     } else {
86116dbaf90SJason Wang         ret = tap_fd_disable(s->fd);
86216dbaf90SJason Wang         if (ret == 0) {
86316dbaf90SJason Wang             qemu_purge_queued_packets(nc);
86416dbaf90SJason Wang             s->enabled = false;
86516dbaf90SJason Wang             tap_update_fd_handler(s);
86616dbaf90SJason Wang         }
86716dbaf90SJason Wang         return ret;
86816dbaf90SJason Wang     }
86916dbaf90SJason Wang }
870