xref: /qemu/net/tap.c (revision 756ae78b275a0625b4559a8fc448df4c6eb331a8)
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 
450d09e41aSPaolo Bonzini #include "net/vhost_net.h"
4682b0d80eSMichael S. Tsirkin 
475281d757SMark McLoughlin typedef struct TAPState {
484e68f7a0SStefan Hajnoczi     NetClientState nc;
495281d757SMark McLoughlin     int fd;
505281d757SMark McLoughlin     char down_script[1024];
515281d757SMark McLoughlin     char down_script_arg[128];
52d32fcad3SScott Feldman     uint8_t buf[NET_BUFSIZE];
53ec45f083SJason Wang     bool read_poll;
54ec45f083SJason Wang     bool write_poll;
55ec45f083SJason Wang     bool using_vnet_hdr;
56ec45f083SJason Wang     bool has_ufo;
5716dbaf90SJason Wang     bool enabled;
5882b0d80eSMichael S. Tsirkin     VHostNetState *vhost_net;
59ef4252b1SMichael S. Tsirkin     unsigned host_vnet_hdr_len;
605281d757SMark McLoughlin } TAPState;
615281d757SMark McLoughlin 
625281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd);
635281d757SMark McLoughlin 
645281d757SMark McLoughlin static int tap_can_send(void *opaque);
655281d757SMark McLoughlin static void tap_send(void *opaque);
665281d757SMark McLoughlin static void tap_writable(void *opaque);
675281d757SMark McLoughlin 
685281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s)
695281d757SMark McLoughlin {
705281d757SMark McLoughlin     qemu_set_fd_handler2(s->fd,
7116dbaf90SJason Wang                          s->read_poll && s->enabled ? tap_can_send : NULL,
7216dbaf90SJason Wang                          s->read_poll && s->enabled ? tap_send     : NULL,
7316dbaf90SJason Wang                          s->write_poll && s->enabled ? tap_writable : NULL,
745281d757SMark McLoughlin                          s);
755281d757SMark McLoughlin }
765281d757SMark McLoughlin 
77ec45f083SJason Wang static void tap_read_poll(TAPState *s, bool enable)
785281d757SMark McLoughlin {
79ec45f083SJason Wang     s->read_poll = enable;
805281d757SMark McLoughlin     tap_update_fd_handler(s);
815281d757SMark McLoughlin }
825281d757SMark McLoughlin 
83ec45f083SJason Wang static void tap_write_poll(TAPState *s, bool enable)
845281d757SMark McLoughlin {
85ec45f083SJason Wang     s->write_poll = enable;
865281d757SMark McLoughlin     tap_update_fd_handler(s);
875281d757SMark McLoughlin }
885281d757SMark McLoughlin 
895281d757SMark McLoughlin static void tap_writable(void *opaque)
905281d757SMark McLoughlin {
915281d757SMark McLoughlin     TAPState *s = opaque;
925281d757SMark McLoughlin 
93ec45f083SJason Wang     tap_write_poll(s, false);
945281d757SMark McLoughlin 
953e35ba93SMark McLoughlin     qemu_flush_queued_packets(&s->nc);
965281d757SMark McLoughlin }
975281d757SMark McLoughlin 
985281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
995281d757SMark McLoughlin {
1005281d757SMark McLoughlin     ssize_t len;
1015281d757SMark McLoughlin 
1025281d757SMark McLoughlin     do {
1035281d757SMark McLoughlin         len = writev(s->fd, iov, iovcnt);
1045281d757SMark McLoughlin     } while (len == -1 && errno == EINTR);
1055281d757SMark McLoughlin 
1065281d757SMark McLoughlin     if (len == -1 && errno == EAGAIN) {
107ec45f083SJason Wang         tap_write_poll(s, true);
1085281d757SMark McLoughlin         return 0;
1095281d757SMark McLoughlin     }
1105281d757SMark McLoughlin 
1115281d757SMark McLoughlin     return len;
1125281d757SMark McLoughlin }
1135281d757SMark McLoughlin 
1144e68f7a0SStefan Hajnoczi static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
1155281d757SMark McLoughlin                                int iovcnt)
1165281d757SMark McLoughlin {
1173e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1185281d757SMark McLoughlin     const struct iovec *iovp = iov;
1195281d757SMark McLoughlin     struct iovec iov_copy[iovcnt + 1];
120ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1215281d757SMark McLoughlin 
122ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1235281d757SMark McLoughlin         iov_copy[0].iov_base = &hdr;
124ef4252b1SMichael S. Tsirkin         iov_copy[0].iov_len =  s->host_vnet_hdr_len;
1255281d757SMark McLoughlin         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
1265281d757SMark McLoughlin         iovp = iov_copy;
1275281d757SMark McLoughlin         iovcnt++;
1285281d757SMark McLoughlin     }
1295281d757SMark McLoughlin 
1305281d757SMark McLoughlin     return tap_write_packet(s, iovp, iovcnt);
1315281d757SMark McLoughlin }
1325281d757SMark McLoughlin 
1334e68f7a0SStefan Hajnoczi static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
1345281d757SMark McLoughlin {
1353e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1365281d757SMark McLoughlin     struct iovec iov[2];
1375281d757SMark McLoughlin     int iovcnt = 0;
138ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1395281d757SMark McLoughlin 
140ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len) {
1415281d757SMark McLoughlin         iov[iovcnt].iov_base = &hdr;
142ef4252b1SMichael S. Tsirkin         iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
1435281d757SMark McLoughlin         iovcnt++;
1445281d757SMark McLoughlin     }
1455281d757SMark McLoughlin 
1465281d757SMark McLoughlin     iov[iovcnt].iov_base = (char *)buf;
1475281d757SMark McLoughlin     iov[iovcnt].iov_len  = size;
1485281d757SMark McLoughlin     iovcnt++;
1495281d757SMark McLoughlin 
1505281d757SMark McLoughlin     return tap_write_packet(s, iov, iovcnt);
1515281d757SMark McLoughlin }
1525281d757SMark McLoughlin 
1534e68f7a0SStefan Hajnoczi static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1545281d757SMark McLoughlin {
1553e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1565281d757SMark McLoughlin     struct iovec iov[1];
1575281d757SMark McLoughlin 
158ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1593e35ba93SMark McLoughlin         return tap_receive_raw(nc, buf, size);
1605281d757SMark McLoughlin     }
1615281d757SMark McLoughlin 
1625281d757SMark McLoughlin     iov[0].iov_base = (char *)buf;
1635281d757SMark McLoughlin     iov[0].iov_len  = size;
1645281d757SMark McLoughlin 
1655281d757SMark McLoughlin     return tap_write_packet(s, iov, 1);
1665281d757SMark McLoughlin }
1675281d757SMark McLoughlin 
1685281d757SMark McLoughlin static int tap_can_send(void *opaque)
1695281d757SMark McLoughlin {
1705281d757SMark McLoughlin     TAPState *s = opaque;
1715281d757SMark McLoughlin 
1723e35ba93SMark McLoughlin     return qemu_can_send_packet(&s->nc);
1735281d757SMark McLoughlin }
1745281d757SMark McLoughlin 
175966ea5ecSMark McLoughlin #ifndef __sun__
176966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1775281d757SMark McLoughlin {
1785281d757SMark McLoughlin     return read(tapfd, buf, maxlen);
1795281d757SMark McLoughlin }
1805281d757SMark McLoughlin #endif
1815281d757SMark McLoughlin 
1824e68f7a0SStefan Hajnoczi static void tap_send_completed(NetClientState *nc, ssize_t len)
1835281d757SMark McLoughlin {
1843e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
185ec45f083SJason Wang     tap_read_poll(s, true);
1865281d757SMark McLoughlin }
1875281d757SMark McLoughlin 
1885281d757SMark McLoughlin static void tap_send(void *opaque)
1895281d757SMark McLoughlin {
1905281d757SMark McLoughlin     TAPState *s = opaque;
191be1636b3SMark McLoughlin     int size;
192*756ae78bSWangkai (Kevin,C)     int packets = 0;
1935281d757SMark McLoughlin 
19468e5ec64SStefan Hajnoczi     while (qemu_can_send_packet(&s->nc)) {
1955819c918SMark McLoughlin         uint8_t *buf = s->buf;
1965819c918SMark McLoughlin 
1975281d757SMark McLoughlin         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1985281d757SMark McLoughlin         if (size <= 0) {
1995819c918SMark McLoughlin             break;
2005281d757SMark McLoughlin         }
2015281d757SMark McLoughlin 
202ef4252b1SMichael S. Tsirkin         if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
203ef4252b1SMichael S. Tsirkin             buf  += s->host_vnet_hdr_len;
204ef4252b1SMichael S. Tsirkin             size -= s->host_vnet_hdr_len;
2055281d757SMark McLoughlin         }
2065281d757SMark McLoughlin 
2073e35ba93SMark McLoughlin         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
2085281d757SMark McLoughlin         if (size == 0) {
209ec45f083SJason Wang             tap_read_poll(s, false);
21068e5ec64SStefan Hajnoczi             break;
21168e5ec64SStefan Hajnoczi         } else if (size < 0) {
21268e5ec64SStefan Hajnoczi             break;
2135281d757SMark McLoughlin         }
214*756ae78bSWangkai (Kevin,C) 
215*756ae78bSWangkai (Kevin,C)         /*
216*756ae78bSWangkai (Kevin,C)          * When the host keeps receiving more packets while tap_send() is
217*756ae78bSWangkai (Kevin,C)          * running we can hog the QEMU global mutex.  Limit the number of
218*756ae78bSWangkai (Kevin,C)          * packets that are processed per tap_send() callback to prevent
219*756ae78bSWangkai (Kevin,C)          * stalling the guest.
220*756ae78bSWangkai (Kevin,C)          */
221*756ae78bSWangkai (Kevin,C)         packets++;
222*756ae78bSWangkai (Kevin,C)         if (packets >= 50) {
223*756ae78bSWangkai (Kevin,C)             break;
224*756ae78bSWangkai (Kevin,C)         }
22568e5ec64SStefan Hajnoczi     }
2265281d757SMark McLoughlin }
2275281d757SMark McLoughlin 
2283bac80d3SVincenzo Maffione static bool tap_has_ufo(NetClientState *nc)
2295281d757SMark McLoughlin {
2303e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2315281d757SMark McLoughlin 
2322be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2335281d757SMark McLoughlin 
2345281d757SMark McLoughlin     return s->has_ufo;
2355281d757SMark McLoughlin }
2365281d757SMark McLoughlin 
2373bac80d3SVincenzo Maffione static bool tap_has_vnet_hdr(NetClientState *nc)
2385281d757SMark McLoughlin {
2393e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2405281d757SMark McLoughlin 
2412be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
2425281d757SMark McLoughlin 
243ef4252b1SMichael S. Tsirkin     return !!s->host_vnet_hdr_len;
2445281d757SMark McLoughlin }
2455281d757SMark McLoughlin 
2463bac80d3SVincenzo Maffione static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
247445d892fSMichael S. Tsirkin {
248445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
249445d892fSMichael S. Tsirkin 
2502be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
251445d892fSMichael S. Tsirkin 
252e96dfd11SVincenzo Maffione     return !!tap_probe_vnet_hdr_len(s->fd, len);
253445d892fSMichael S. Tsirkin }
254445d892fSMichael S. Tsirkin 
2553bac80d3SVincenzo Maffione static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
256445d892fSMichael S. Tsirkin {
257445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
258445d892fSMichael S. Tsirkin 
2592be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
260445d892fSMichael S. Tsirkin     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
261445d892fSMichael S. Tsirkin            len == sizeof(struct virtio_net_hdr));
262445d892fSMichael S. Tsirkin 
263445d892fSMichael S. Tsirkin     tap_fd_set_vnet_hdr_len(s->fd, len);
264445d892fSMichael S. Tsirkin     s->host_vnet_hdr_len = len;
265445d892fSMichael S. Tsirkin }
266445d892fSMichael S. Tsirkin 
2673bac80d3SVincenzo Maffione static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
2685281d757SMark McLoughlin {
2693e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2705281d757SMark McLoughlin 
2712be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
272ef4252b1SMichael S. Tsirkin     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
2735281d757SMark McLoughlin 
2745281d757SMark McLoughlin     s->using_vnet_hdr = using_vnet_hdr;
2755281d757SMark McLoughlin }
2765281d757SMark McLoughlin 
2773bac80d3SVincenzo Maffione static void tap_set_offload(NetClientState *nc, int csum, int tso4,
2785281d757SMark McLoughlin                      int tso6, int ecn, int ufo)
2795281d757SMark McLoughlin {
2803e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
28127a6375dSMichael S. Tsirkin     if (s->fd < 0) {
28227a6375dSMichael S. Tsirkin         return;
28327a6375dSMichael S. Tsirkin     }
2845281d757SMark McLoughlin 
28527a6375dSMichael S. Tsirkin     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
2865281d757SMark McLoughlin }
2875281d757SMark McLoughlin 
2884e68f7a0SStefan Hajnoczi static void tap_cleanup(NetClientState *nc)
2895281d757SMark McLoughlin {
2903e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2915281d757SMark McLoughlin 
29282b0d80eSMichael S. Tsirkin     if (s->vhost_net) {
29382b0d80eSMichael S. Tsirkin         vhost_net_cleanup(s->vhost_net);
29443849424SMichael S. Tsirkin         s->vhost_net = NULL;
29582b0d80eSMichael S. Tsirkin     }
29682b0d80eSMichael S. Tsirkin 
2973e35ba93SMark McLoughlin     qemu_purge_queued_packets(nc);
2985281d757SMark McLoughlin 
2995281d757SMark McLoughlin     if (s->down_script[0])
3005281d757SMark McLoughlin         launch_script(s->down_script, s->down_script_arg, s->fd);
3015281d757SMark McLoughlin 
302ec45f083SJason Wang     tap_read_poll(s, false);
303ec45f083SJason Wang     tap_write_poll(s, false);
3045281d757SMark McLoughlin     close(s->fd);
30527a6375dSMichael S. Tsirkin     s->fd = -1;
3065281d757SMark McLoughlin }
3075281d757SMark McLoughlin 
3084e68f7a0SStefan Hajnoczi static void tap_poll(NetClientState *nc, bool enable)
309ceb69615SMichael S. Tsirkin {
310ceb69615SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
311ceb69615SMichael S. Tsirkin     tap_read_poll(s, enable);
312ceb69615SMichael S. Tsirkin     tap_write_poll(s, enable);
313ceb69615SMichael S. Tsirkin }
314ceb69615SMichael S. Tsirkin 
3154e68f7a0SStefan Hajnoczi int tap_get_fd(NetClientState *nc)
31695d528a2SMichael S. Tsirkin {
31795d528a2SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
3182be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
31995d528a2SMichael S. Tsirkin     return s->fd;
32095d528a2SMichael S. Tsirkin }
32195d528a2SMichael S. Tsirkin 
3225281d757SMark McLoughlin /* fd support */
3235281d757SMark McLoughlin 
3243e35ba93SMark McLoughlin static NetClientInfo net_tap_info = {
3252be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_TAP,
3263e35ba93SMark McLoughlin     .size = sizeof(TAPState),
3273e35ba93SMark McLoughlin     .receive = tap_receive,
3283e35ba93SMark McLoughlin     .receive_raw = tap_receive_raw,
3293e35ba93SMark McLoughlin     .receive_iov = tap_receive_iov,
330ceb69615SMichael S. Tsirkin     .poll = tap_poll,
3313e35ba93SMark McLoughlin     .cleanup = tap_cleanup,
3322e753bccSVincenzo Maffione     .has_ufo = tap_has_ufo,
3332e753bccSVincenzo Maffione     .has_vnet_hdr = tap_has_vnet_hdr,
3342e753bccSVincenzo Maffione     .has_vnet_hdr_len = tap_has_vnet_hdr_len,
3352e753bccSVincenzo Maffione     .using_vnet_hdr = tap_using_vnet_hdr,
3362e753bccSVincenzo Maffione     .set_offload = tap_set_offload,
3372e753bccSVincenzo Maffione     .set_vnet_hdr_len = tap_set_vnet_hdr_len,
3383e35ba93SMark McLoughlin };
3393e35ba93SMark McLoughlin 
3404e68f7a0SStefan Hajnoczi static TAPState *net_tap_fd_init(NetClientState *peer,
3415281d757SMark McLoughlin                                  const char *model,
3425281d757SMark McLoughlin                                  const char *name,
3435281d757SMark McLoughlin                                  int fd,
3445281d757SMark McLoughlin                                  int vnet_hdr)
3455281d757SMark McLoughlin {
3464e68f7a0SStefan Hajnoczi     NetClientState *nc;
3475281d757SMark McLoughlin     TAPState *s;
3485281d757SMark McLoughlin 
349ab5f3f84SStefan Hajnoczi     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
3503e35ba93SMark McLoughlin 
3513e35ba93SMark McLoughlin     s = DO_UPCAST(TAPState, nc, nc);
3523e35ba93SMark McLoughlin 
3535281d757SMark McLoughlin     s->fd = fd;
354ef4252b1SMichael S. Tsirkin     s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
355ec45f083SJason Wang     s->using_vnet_hdr = false;
3569c282718SMark McLoughlin     s->has_ufo = tap_probe_has_ufo(s->fd);
35716dbaf90SJason Wang     s->enabled = true;
3583e35ba93SMark McLoughlin     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
35958ddcd50SMichael S. Tsirkin     /*
36058ddcd50SMichael S. Tsirkin      * Make sure host header length is set correctly in tap:
36158ddcd50SMichael S. Tsirkin      * it might have been modified by another instance of qemu.
36258ddcd50SMichael S. Tsirkin      */
36358ddcd50SMichael S. Tsirkin     if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
36458ddcd50SMichael S. Tsirkin         tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
36558ddcd50SMichael S. Tsirkin     }
366ec45f083SJason Wang     tap_read_poll(s, true);
36782b0d80eSMichael S. Tsirkin     s->vhost_net = NULL;
3685281d757SMark McLoughlin     return s;
3695281d757SMark McLoughlin }
3705281d757SMark McLoughlin 
3715281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd)
3725281d757SMark McLoughlin {
3735281d757SMark McLoughlin     int pid, status;
3745281d757SMark McLoughlin     char *args[3];
3755281d757SMark McLoughlin     char **parg;
3765281d757SMark McLoughlin 
3775281d757SMark McLoughlin     /* try to launch network script */
3785281d757SMark McLoughlin     pid = fork();
3795281d757SMark McLoughlin     if (pid == 0) {
3805281d757SMark McLoughlin         int open_max = sysconf(_SC_OPEN_MAX), i;
3815281d757SMark McLoughlin 
38213a12f86SPankaj Gupta         for (i = 3; i < open_max; i++) {
38313a12f86SPankaj Gupta             if (i != fd) {
3845281d757SMark McLoughlin                 close(i);
3855281d757SMark McLoughlin             }
3865281d757SMark McLoughlin         }
3875281d757SMark McLoughlin         parg = args;
3885281d757SMark McLoughlin         *parg++ = (char *)setup_script;
3895281d757SMark McLoughlin         *parg++ = (char *)ifname;
3909678d950SBlue Swirl         *parg = NULL;
3915281d757SMark McLoughlin         execv(setup_script, args);
3925281d757SMark McLoughlin         _exit(1);
3935281d757SMark McLoughlin     } else if (pid > 0) {
3945281d757SMark McLoughlin         while (waitpid(pid, &status, 0) != pid) {
3955281d757SMark McLoughlin             /* loop */
3965281d757SMark McLoughlin         }
3975281d757SMark McLoughlin 
3985281d757SMark McLoughlin         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
3995281d757SMark McLoughlin             return 0;
4005281d757SMark McLoughlin         }
4015281d757SMark McLoughlin     }
4025281d757SMark McLoughlin     fprintf(stderr, "%s: could not launch network script\n", setup_script);
4035281d757SMark McLoughlin     return -1;
4045281d757SMark McLoughlin }
4055281d757SMark McLoughlin 
406a7c36ee4SCorey Bryant static int recv_fd(int c)
407a7c36ee4SCorey Bryant {
408a7c36ee4SCorey Bryant     int fd;
409a7c36ee4SCorey Bryant     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
410a7c36ee4SCorey Bryant     struct msghdr msg = {
411a7c36ee4SCorey Bryant         .msg_control = msgbuf,
412a7c36ee4SCorey Bryant         .msg_controllen = sizeof(msgbuf),
413a7c36ee4SCorey Bryant     };
414a7c36ee4SCorey Bryant     struct cmsghdr *cmsg;
415a7c36ee4SCorey Bryant     struct iovec iov;
416a7c36ee4SCorey Bryant     uint8_t req[1];
417a7c36ee4SCorey Bryant     ssize_t len;
418a7c36ee4SCorey Bryant 
419a7c36ee4SCorey Bryant     cmsg = CMSG_FIRSTHDR(&msg);
420a7c36ee4SCorey Bryant     cmsg->cmsg_level = SOL_SOCKET;
421a7c36ee4SCorey Bryant     cmsg->cmsg_type = SCM_RIGHTS;
422a7c36ee4SCorey Bryant     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
423a7c36ee4SCorey Bryant     msg.msg_controllen = cmsg->cmsg_len;
424a7c36ee4SCorey Bryant 
425a7c36ee4SCorey Bryant     iov.iov_base = req;
426a7c36ee4SCorey Bryant     iov.iov_len = sizeof(req);
427a7c36ee4SCorey Bryant 
428a7c36ee4SCorey Bryant     msg.msg_iov = &iov;
429a7c36ee4SCorey Bryant     msg.msg_iovlen = 1;
430a7c36ee4SCorey Bryant 
431a7c36ee4SCorey Bryant     len = recvmsg(c, &msg, 0);
432a7c36ee4SCorey Bryant     if (len > 0) {
433a7c36ee4SCorey Bryant         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
434a7c36ee4SCorey Bryant         return fd;
435a7c36ee4SCorey Bryant     }
436a7c36ee4SCorey Bryant 
437a7c36ee4SCorey Bryant     return len;
438a7c36ee4SCorey Bryant }
439a7c36ee4SCorey Bryant 
440a7c36ee4SCorey Bryant static int net_bridge_run_helper(const char *helper, const char *bridge)
441a7c36ee4SCorey Bryant {
442a7c36ee4SCorey Bryant     sigset_t oldmask, mask;
443a7c36ee4SCorey Bryant     int pid, status;
444a7c36ee4SCorey Bryant     char *args[5];
445a7c36ee4SCorey Bryant     char **parg;
446a7c36ee4SCorey Bryant     int sv[2];
447a7c36ee4SCorey Bryant 
448a7c36ee4SCorey Bryant     sigemptyset(&mask);
449a7c36ee4SCorey Bryant     sigaddset(&mask, SIGCHLD);
450a7c36ee4SCorey Bryant     sigprocmask(SIG_BLOCK, &mask, &oldmask);
451a7c36ee4SCorey Bryant 
452a7c36ee4SCorey Bryant     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
453a7c36ee4SCorey Bryant         return -1;
454a7c36ee4SCorey Bryant     }
455a7c36ee4SCorey Bryant 
456a7c36ee4SCorey Bryant     /* try to launch bridge helper */
457a7c36ee4SCorey Bryant     pid = fork();
458a7c36ee4SCorey Bryant     if (pid == 0) {
459a7c36ee4SCorey Bryant         int open_max = sysconf(_SC_OPEN_MAX), i;
460a7c36ee4SCorey Bryant         char fd_buf[6+10];
461a7c36ee4SCorey Bryant         char br_buf[6+IFNAMSIZ] = {0};
462a7c36ee4SCorey Bryant         char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
463a7c36ee4SCorey Bryant 
46413a12f86SPankaj Gupta         for (i = 3; i < open_max; i++) {
46513a12f86SPankaj Gupta             if (i != sv[1]) {
466a7c36ee4SCorey Bryant                 close(i);
467a7c36ee4SCorey Bryant             }
468a7c36ee4SCorey Bryant         }
469a7c36ee4SCorey Bryant 
470a7c36ee4SCorey Bryant         snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
471a7c36ee4SCorey Bryant 
472a7c36ee4SCorey Bryant         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
473a7c36ee4SCorey Bryant             /* assume helper is a command */
474a7c36ee4SCorey Bryant 
475a7c36ee4SCorey Bryant             if (strstr(helper, "--br=") == NULL) {
476a7c36ee4SCorey Bryant                 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
477a7c36ee4SCorey Bryant             }
478a7c36ee4SCorey Bryant 
479a7c36ee4SCorey Bryant             snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
480a7c36ee4SCorey Bryant                      helper, "--use-vnet", fd_buf, br_buf);
481a7c36ee4SCorey Bryant 
482a7c36ee4SCorey Bryant             parg = args;
483a7c36ee4SCorey Bryant             *parg++ = (char *)"sh";
484a7c36ee4SCorey Bryant             *parg++ = (char *)"-c";
485a7c36ee4SCorey Bryant             *parg++ = helper_cmd;
486a7c36ee4SCorey Bryant             *parg++ = NULL;
487a7c36ee4SCorey Bryant 
488a7c36ee4SCorey Bryant             execv("/bin/sh", args);
489a7c36ee4SCorey Bryant         } else {
490a7c36ee4SCorey Bryant             /* assume helper is just the executable path name */
491a7c36ee4SCorey Bryant 
492a7c36ee4SCorey Bryant             snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
493a7c36ee4SCorey Bryant 
494a7c36ee4SCorey Bryant             parg = args;
495a7c36ee4SCorey Bryant             *parg++ = (char *)helper;
496a7c36ee4SCorey Bryant             *parg++ = (char *)"--use-vnet";
497a7c36ee4SCorey Bryant             *parg++ = fd_buf;
498a7c36ee4SCorey Bryant             *parg++ = br_buf;
499a7c36ee4SCorey Bryant             *parg++ = NULL;
500a7c36ee4SCorey Bryant 
501a7c36ee4SCorey Bryant             execv(helper, args);
502a7c36ee4SCorey Bryant         }
503a7c36ee4SCorey Bryant         _exit(1);
504a7c36ee4SCorey Bryant 
505a7c36ee4SCorey Bryant     } else if (pid > 0) {
506a7c36ee4SCorey Bryant         int fd;
507a7c36ee4SCorey Bryant 
508a7c36ee4SCorey Bryant         close(sv[1]);
509a7c36ee4SCorey Bryant 
510a7c36ee4SCorey Bryant         do {
511a7c36ee4SCorey Bryant             fd = recv_fd(sv[0]);
512a7c36ee4SCorey Bryant         } while (fd == -1 && errno == EINTR);
513a7c36ee4SCorey Bryant 
514a7c36ee4SCorey Bryant         close(sv[0]);
515a7c36ee4SCorey Bryant 
516a7c36ee4SCorey Bryant         while (waitpid(pid, &status, 0) != pid) {
517a7c36ee4SCorey Bryant             /* loop */
518a7c36ee4SCorey Bryant         }
519a7c36ee4SCorey Bryant         sigprocmask(SIG_SETMASK, &oldmask, NULL);
520a7c36ee4SCorey Bryant         if (fd < 0) {
521a7c36ee4SCorey Bryant             fprintf(stderr, "failed to recv file descriptor\n");
522a7c36ee4SCorey Bryant             return -1;
523a7c36ee4SCorey Bryant         }
524a7c36ee4SCorey Bryant 
525a7c36ee4SCorey Bryant         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
526a7c36ee4SCorey Bryant             return fd;
527a7c36ee4SCorey Bryant         }
528a7c36ee4SCorey Bryant     }
529a7c36ee4SCorey Bryant     fprintf(stderr, "failed to launch bridge helper\n");
530a7c36ee4SCorey Bryant     return -1;
531a7c36ee4SCorey Bryant }
532a7c36ee4SCorey Bryant 
5331a0c0958SLaszlo Ersek int net_init_bridge(const NetClientOptions *opts, const char *name,
5344e68f7a0SStefan Hajnoczi                     NetClientState *peer)
535a7c36ee4SCorey Bryant {
536f79b51b0SLaszlo Ersek     const NetdevBridgeOptions *bridge;
537f79b51b0SLaszlo Ersek     const char *helper, *br;
538f79b51b0SLaszlo Ersek 
539a7c36ee4SCorey Bryant     TAPState *s;
540a7c36ee4SCorey Bryant     int fd, vnet_hdr;
541a7c36ee4SCorey Bryant 
542f79b51b0SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
543f79b51b0SLaszlo Ersek     bridge = opts->bridge;
544a7c36ee4SCorey Bryant 
545f79b51b0SLaszlo Ersek     helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
546f79b51b0SLaszlo Ersek     br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;
547f79b51b0SLaszlo Ersek 
548f79b51b0SLaszlo Ersek     fd = net_bridge_run_helper(helper, br);
549a7c36ee4SCorey Bryant     if (fd == -1) {
550a7c36ee4SCorey Bryant         return -1;
551a7c36ee4SCorey Bryant     }
552a7c36ee4SCorey Bryant 
553a7c36ee4SCorey Bryant     fcntl(fd, F_SETFL, O_NONBLOCK);
554a7c36ee4SCorey Bryant 
555a7c36ee4SCorey Bryant     vnet_hdr = tap_probe_vnet_hdr(fd);
556a7c36ee4SCorey Bryant 
557d33d93b2SStefan Hajnoczi     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
558a7c36ee4SCorey Bryant     if (!s) {
559a7c36ee4SCorey Bryant         close(fd);
560a7c36ee4SCorey Bryant         return -1;
561a7c36ee4SCorey Bryant     }
562a7c36ee4SCorey Bryant 
563f79b51b0SLaszlo Ersek     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
564f79b51b0SLaszlo Ersek              br);
565a7c36ee4SCorey Bryant 
566a7c36ee4SCorey Bryant     return 0;
567a7c36ee4SCorey Bryant }
568a7c36ee4SCorey Bryant 
56908c573a8SLaszlo Ersek static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
57008c573a8SLaszlo Ersek                         const char *setup_script, char *ifname,
571264986e2SJason Wang                         size_t ifname_sz, int mq_required)
5725281d757SMark McLoughlin {
5735281d757SMark McLoughlin     int fd, vnet_hdr_required;
5745281d757SMark McLoughlin 
57508c573a8SLaszlo Ersek     if (tap->has_vnet_hdr) {
57608c573a8SLaszlo Ersek         *vnet_hdr = tap->vnet_hdr;
5775281d757SMark McLoughlin         vnet_hdr_required = *vnet_hdr;
5785281d757SMark McLoughlin     } else {
57908c573a8SLaszlo Ersek         *vnet_hdr = 1;
5805281d757SMark McLoughlin         vnet_hdr_required = 0;
5815281d757SMark McLoughlin     }
5825281d757SMark McLoughlin 
583264986e2SJason Wang     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
584264986e2SJason Wang                       mq_required));
5855281d757SMark McLoughlin     if (fd < 0) {
5865281d757SMark McLoughlin         return -1;
5875281d757SMark McLoughlin     }
5885281d757SMark McLoughlin 
5895281d757SMark McLoughlin     if (setup_script &&
5905281d757SMark McLoughlin         setup_script[0] != '\0' &&
5915281d757SMark McLoughlin         strcmp(setup_script, "no") != 0 &&
5925281d757SMark McLoughlin         launch_script(setup_script, ifname, fd)) {
5935281d757SMark McLoughlin         close(fd);
5945281d757SMark McLoughlin         return -1;
5955281d757SMark McLoughlin     }
5965281d757SMark McLoughlin 
5975281d757SMark McLoughlin     return fd;
5985281d757SMark McLoughlin }
5995281d757SMark McLoughlin 
600264986e2SJason Wang #define MAX_TAP_QUEUES 1024
601264986e2SJason Wang 
6025193e5fbSJason Wang static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
6035193e5fbSJason Wang                             const char *model, const char *name,
6045193e5fbSJason Wang                             const char *ifname, const char *script,
6055193e5fbSJason Wang                             const char *downscript, const char *vhostfdname,
6065193e5fbSJason Wang                             int vnet_hdr, int fd)
6075193e5fbSJason Wang {
6085193e5fbSJason Wang     TAPState *s;
60981647a65SNikolay Nikolaev     int vhostfd;
6105193e5fbSJason Wang 
6115193e5fbSJason Wang     s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
6125193e5fbSJason Wang     if (!s) {
6135193e5fbSJason Wang         return -1;
6145193e5fbSJason Wang     }
6155193e5fbSJason Wang 
6165193e5fbSJason Wang     if (tap_set_sndbuf(s->fd, tap) < 0) {
6175193e5fbSJason Wang         return -1;
6185193e5fbSJason Wang     }
6195193e5fbSJason Wang 
620264986e2SJason Wang     if (tap->has_fd || tap->has_fds) {
6215193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
6225193e5fbSJason Wang     } else if (tap->has_helper) {
6235193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
6245193e5fbSJason Wang                  tap->helper);
6255193e5fbSJason Wang     } else {
6265193e5fbSJason Wang         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
6275193e5fbSJason Wang                  "ifname=%s,script=%s,downscript=%s", ifname, script,
6285193e5fbSJason Wang                  downscript);
6295193e5fbSJason Wang 
6305193e5fbSJason Wang         if (strcmp(downscript, "no") != 0) {
6315193e5fbSJason Wang             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
6325193e5fbSJason Wang             snprintf(s->down_script_arg, sizeof(s->down_script_arg),
6335193e5fbSJason Wang                      "%s", ifname);
6345193e5fbSJason Wang         }
6355193e5fbSJason Wang     }
6365193e5fbSJason Wang 
6375193e5fbSJason Wang     if (tap->has_vhost ? tap->vhost :
6385193e5fbSJason Wang         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
63981647a65SNikolay Nikolaev         VhostNetOptions options;
64081647a65SNikolay Nikolaev 
6411a1bfac9SNikolay Nikolaev         options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
64281647a65SNikolay Nikolaev         options.net_backend = &s->nc;
64381647a65SNikolay Nikolaev         options.force = tap->has_vhostforce && tap->vhostforce;
6445193e5fbSJason Wang 
6457873df40SJason Wang         if (tap->has_vhostfd || tap->has_vhostfds) {
6465193e5fbSJason Wang             vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
6475193e5fbSJason Wang             if (vhostfd == -1) {
6485193e5fbSJason Wang                 return -1;
6495193e5fbSJason Wang             }
6505193e5fbSJason Wang         } else {
65181647a65SNikolay Nikolaev             vhostfd = open("/dev/vhost-net", O_RDWR);
65281647a65SNikolay Nikolaev             if (vhostfd < 0) {
65381647a65SNikolay Nikolaev                 error_report("tap: open vhost char device failed: %s",
65481647a65SNikolay Nikolaev                            strerror(errno));
65581647a65SNikolay Nikolaev                 return -1;
6565193e5fbSJason Wang             }
65781647a65SNikolay Nikolaev         }
65881647a65SNikolay Nikolaev         options.opaque = (void *)(uintptr_t)vhostfd;
6595193e5fbSJason Wang 
66081647a65SNikolay Nikolaev         s->vhost_net = vhost_net_init(&options);
6615193e5fbSJason Wang         if (!s->vhost_net) {
6625193e5fbSJason Wang             error_report("vhost-net requested but could not be initialized");
6635193e5fbSJason Wang             return -1;
6645193e5fbSJason Wang         }
665264986e2SJason Wang     } else if (tap->has_vhostfd || tap->has_vhostfds) {
6665193e5fbSJason Wang         error_report("vhostfd= is not valid without vhost");
6675193e5fbSJason Wang         return -1;
6685193e5fbSJason Wang     }
6695193e5fbSJason Wang 
6705193e5fbSJason Wang     return 0;
6715193e5fbSJason Wang }
6725193e5fbSJason Wang 
673264986e2SJason Wang static int get_fds(char *str, char *fds[], int max)
674264986e2SJason Wang {
675264986e2SJason Wang     char *ptr = str, *this;
676264986e2SJason Wang     size_t len = strlen(str);
677264986e2SJason Wang     int i = 0;
678264986e2SJason Wang 
679264986e2SJason Wang     while (i < max && ptr < str + len) {
680264986e2SJason Wang         this = strchr(ptr, ':');
681264986e2SJason Wang 
682264986e2SJason Wang         if (this == NULL) {
683264986e2SJason Wang             fds[i] = g_strdup(ptr);
684264986e2SJason Wang         } else {
685264986e2SJason Wang             fds[i] = g_strndup(ptr, this - ptr);
686264986e2SJason Wang         }
687264986e2SJason Wang 
688264986e2SJason Wang         i++;
689264986e2SJason Wang         if (this == NULL) {
690264986e2SJason Wang             break;
691264986e2SJason Wang         } else {
692264986e2SJason Wang             ptr = this + 1;
693264986e2SJason Wang         }
694264986e2SJason Wang     }
695264986e2SJason Wang 
696264986e2SJason Wang     return i;
697264986e2SJason Wang }
698264986e2SJason Wang 
6991a0c0958SLaszlo Ersek int net_init_tap(const NetClientOptions *opts, const char *name,
7004e68f7a0SStefan Hajnoczi                  NetClientState *peer)
7015281d757SMark McLoughlin {
70208c573a8SLaszlo Ersek     const NetdevTapOptions *tap;
703264986e2SJason Wang     int fd, vnet_hdr = 0, i = 0, queues;
70408c573a8SLaszlo Ersek     /* for the no-fd, no-helper case */
70508c573a8SLaszlo Ersek     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
7065193e5fbSJason Wang     const char *downscript = NULL;
707264986e2SJason Wang     const char *vhostfdname;
70808c573a8SLaszlo Ersek     char ifname[128];
70908c573a8SLaszlo Ersek 
71008c573a8SLaszlo Ersek     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
71108c573a8SLaszlo Ersek     tap = opts->tap;
712264986e2SJason Wang     queues = tap->has_queues ? tap->queues : 1;
713264986e2SJason Wang     vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
71408c573a8SLaszlo Ersek 
715ce675a75SJason Wang     /* QEMU vlans does not support multiqueue tap, in this case peer is set.
716ce675a75SJason Wang      * For -netdev, peer is always NULL. */
717ce675a75SJason Wang     if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
718085d8134SPeter Maydell         error_report("Multiqueue tap cannot be used with QEMU vlans");
719ce675a75SJason Wang         return -1;
720ce675a75SJason Wang     }
721ce675a75SJason Wang 
72208c573a8SLaszlo Ersek     if (tap->has_fd) {
72308c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
724264986e2SJason Wang             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
725c87826a8SJason Wang             tap->has_fds || tap->has_vhostfds) {
726a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, vnet_hdr=, "
727c87826a8SJason Wang                          "helper=, queues=, fds=, and vhostfds= "
728c87826a8SJason Wang                          "are invalid with fd=");
7295281d757SMark McLoughlin             return -1;
7305281d757SMark McLoughlin         }
7315281d757SMark McLoughlin 
732a96ed02fSNicholas Bellinger         fd = monitor_handle_fd_param(cur_mon, tap->fd);
7335281d757SMark McLoughlin         if (fd == -1) {
7345281d757SMark McLoughlin             return -1;
7355281d757SMark McLoughlin         }
7365281d757SMark McLoughlin 
7375281d757SMark McLoughlin         fcntl(fd, F_SETFL, O_NONBLOCK);
7385281d757SMark McLoughlin 
7395281d757SMark McLoughlin         vnet_hdr = tap_probe_vnet_hdr(fd);
740a7c36ee4SCorey Bryant 
74102cd8090SAnthony Liguori         if (net_init_tap_one(tap, peer, "tap", name, NULL,
742264986e2SJason Wang                              script, downscript,
743264986e2SJason Wang                              vhostfdname, vnet_hdr, fd)) {
744264986e2SJason Wang             return -1;
745264986e2SJason Wang         }
746264986e2SJason Wang     } else if (tap->has_fds) {
747264986e2SJason Wang         char *fds[MAX_TAP_QUEUES];
748264986e2SJason Wang         char *vhost_fds[MAX_TAP_QUEUES];
749264986e2SJason Wang         int nfds, nvhosts;
750a7c36ee4SCorey Bryant 
751264986e2SJason Wang         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
752264986e2SJason Wang             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
753c87826a8SJason Wang             tap->has_vhostfd) {
754264986e2SJason Wang             error_report("ifname=, script=, downscript=, vnet_hdr=, "
755c87826a8SJason Wang                          "helper=, queues=, and vhostfd= "
756c87826a8SJason Wang                          "are invalid with fds=");
757264986e2SJason Wang             return -1;
758264986e2SJason Wang         }
759264986e2SJason Wang 
760264986e2SJason Wang         nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
761264986e2SJason Wang         if (tap->has_vhostfds) {
762264986e2SJason Wang             nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
763264986e2SJason Wang             if (nfds != nvhosts) {
764264986e2SJason Wang                 error_report("The number of fds passed does not match the "
765264986e2SJason Wang                              "number of vhostfds passed");
766264986e2SJason Wang                 return -1;
767264986e2SJason Wang             }
768264986e2SJason Wang         }
769264986e2SJason Wang 
770264986e2SJason Wang         for (i = 0; i < nfds; i++) {
771264986e2SJason Wang             fd = monitor_handle_fd_param(cur_mon, fds[i]);
772264986e2SJason Wang             if (fd == -1) {
773264986e2SJason Wang                 return -1;
774264986e2SJason Wang             }
775264986e2SJason Wang 
776264986e2SJason Wang             fcntl(fd, F_SETFL, O_NONBLOCK);
777264986e2SJason Wang 
778264986e2SJason Wang             if (i == 0) {
779264986e2SJason Wang                 vnet_hdr = tap_probe_vnet_hdr(fd);
780264986e2SJason Wang             } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
781264986e2SJason Wang                 error_report("vnet_hdr not consistent across given tap fds");
782264986e2SJason Wang                 return -1;
783264986e2SJason Wang             }
784264986e2SJason Wang 
785264986e2SJason Wang             if (net_init_tap_one(tap, peer, "tap", name, ifname,
786264986e2SJason Wang                                  script, downscript,
787264986e2SJason Wang                                  tap->has_vhostfds ? vhost_fds[i] : NULL,
788264986e2SJason Wang                                  vnet_hdr, fd)) {
789264986e2SJason Wang                 return -1;
790264986e2SJason Wang             }
791264986e2SJason Wang         }
79208c573a8SLaszlo Ersek     } else if (tap->has_helper) {
79308c573a8SLaszlo Ersek         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
794c87826a8SJason Wang             tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
795a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, and vnet_hdr= "
796c87826a8SJason Wang                          "queues=, and vhostfds= are invalid with helper=");
797a7c36ee4SCorey Bryant             return -1;
798a7c36ee4SCorey Bryant         }
799a7c36ee4SCorey Bryant 
80008c573a8SLaszlo Ersek         fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
801a7c36ee4SCorey Bryant         if (fd == -1) {
802a7c36ee4SCorey Bryant             return -1;
803a7c36ee4SCorey Bryant         }
804a7c36ee4SCorey Bryant 
805a7c36ee4SCorey Bryant         fcntl(fd, F_SETFL, O_NONBLOCK);
806a7c36ee4SCorey Bryant         vnet_hdr = tap_probe_vnet_hdr(fd);
807a7c36ee4SCorey Bryant 
808264986e2SJason Wang         if (net_init_tap_one(tap, peer, "bridge", name, ifname,
809264986e2SJason Wang                              script, downscript, vhostfdname,
810264986e2SJason Wang                              vnet_hdr, fd)) {
81184f8f3daSGonglei             close(fd);
812264986e2SJason Wang             return -1;
813264986e2SJason Wang         }
8145281d757SMark McLoughlin     } else {
815c87826a8SJason Wang         if (tap->has_vhostfds) {
816c87826a8SJason Wang             error_report("vhostfds= is invalid if fds= wasn't specified");
817c87826a8SJason Wang             return -1;
818c87826a8SJason Wang         }
81908c573a8SLaszlo Ersek         script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
8205193e5fbSJason Wang         downscript = tap->has_downscript ? tap->downscript :
8215193e5fbSJason Wang             DEFAULT_NETWORK_DOWN_SCRIPT;
822264986e2SJason Wang 
823264986e2SJason Wang         if (tap->has_ifname) {
824264986e2SJason Wang             pstrcpy(ifname, sizeof ifname, tap->ifname);
825264986e2SJason Wang         } else {
826264986e2SJason Wang             ifname[0] = '\0';
827264986e2SJason Wang         }
828264986e2SJason Wang 
829264986e2SJason Wang         for (i = 0; i < queues; i++) {
830264986e2SJason Wang             fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
831264986e2SJason Wang                               ifname, sizeof ifname, queues > 1);
832929fe497SJuergen Lock             if (fd == -1) {
833929fe497SJuergen Lock                 return -1;
834929fe497SJuergen Lock             }
835a7c36ee4SCorey Bryant 
836264986e2SJason Wang             if (queues > 1 && i == 0 && !tap->has_ifname) {
837264986e2SJason Wang                 if (tap_fd_get_ifname(fd, ifname)) {
838264986e2SJason Wang                     error_report("Fail to get ifname");
83984f8f3daSGonglei                     close(fd);
840264986e2SJason Wang                     return -1;
841264986e2SJason Wang                 }
8425281d757SMark McLoughlin             }
8435281d757SMark McLoughlin 
844264986e2SJason Wang             if (net_init_tap_one(tap, peer, "tap", name, ifname,
845264986e2SJason Wang                                  i >= 1 ? "no" : script,
846264986e2SJason Wang                                  i >= 1 ? "no" : downscript,
847264986e2SJason Wang                                  vhostfdname, vnet_hdr, fd)) {
84884f8f3daSGonglei                 close(fd);
849264986e2SJason Wang                 return -1;
850264986e2SJason Wang             }
851264986e2SJason Wang         }
852264986e2SJason Wang     }
853264986e2SJason Wang 
854264986e2SJason Wang     return 0;
8555281d757SMark McLoughlin }
856b202554cSMichael S. Tsirkin 
8574e68f7a0SStefan Hajnoczi VHostNetState *tap_get_vhost_net(NetClientState *nc)
858b202554cSMichael S. Tsirkin {
859b202554cSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
8602be64a68SLaszlo Ersek     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
861b202554cSMichael S. Tsirkin     return s->vhost_net;
862b202554cSMichael S. Tsirkin }
86316dbaf90SJason Wang 
86416dbaf90SJason Wang int tap_enable(NetClientState *nc)
86516dbaf90SJason Wang {
86616dbaf90SJason Wang     TAPState *s = DO_UPCAST(TAPState, nc, nc);
86716dbaf90SJason Wang     int ret;
86816dbaf90SJason Wang 
86916dbaf90SJason Wang     if (s->enabled) {
87016dbaf90SJason Wang         return 0;
87116dbaf90SJason Wang     } else {
87216dbaf90SJason Wang         ret = tap_fd_enable(s->fd);
87316dbaf90SJason Wang         if (ret == 0) {
87416dbaf90SJason Wang             s->enabled = true;
87516dbaf90SJason Wang             tap_update_fd_handler(s);
87616dbaf90SJason Wang         }
87716dbaf90SJason Wang         return ret;
87816dbaf90SJason Wang     }
87916dbaf90SJason Wang }
88016dbaf90SJason Wang 
88116dbaf90SJason Wang int tap_disable(NetClientState *nc)
88216dbaf90SJason Wang {
88316dbaf90SJason Wang     TAPState *s = DO_UPCAST(TAPState, nc, nc);
88416dbaf90SJason Wang     int ret;
88516dbaf90SJason Wang 
88616dbaf90SJason Wang     if (s->enabled == 0) {
88716dbaf90SJason Wang         return 0;
88816dbaf90SJason Wang     } else {
88916dbaf90SJason Wang         ret = tap_fd_disable(s->fd);
89016dbaf90SJason Wang         if (ret == 0) {
89116dbaf90SJason Wang             qemu_purge_queued_packets(nc);
89216dbaf90SJason Wang             s->enabled = false;
89316dbaf90SJason Wang             tap_update_fd_handler(s);
89416dbaf90SJason Wang         }
89516dbaf90SJason Wang         return ret;
89616dbaf90SJason Wang     }
89716dbaf90SJason Wang }
898