xref: /qemu/net/tap.c (revision a7c36ee4920ea3acc227a0248dd161693f207357)
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 
265281d757SMark McLoughlin #include "net/tap.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 
365281d757SMark McLoughlin #include "net.h"
375281d757SMark McLoughlin #include "sysemu.h"
385281d757SMark McLoughlin #include "qemu-char.h"
395281d757SMark McLoughlin #include "qemu-common.h"
402f792016SMarkus Armbruster #include "qemu-error.h"
415281d757SMark McLoughlin 
425281d757SMark McLoughlin #include "net/tap-linux.h"
435281d757SMark McLoughlin 
4482b0d80eSMichael S. Tsirkin #include "hw/vhost_net.h"
4582b0d80eSMichael S. Tsirkin 
465281d757SMark McLoughlin /* Maximum GSO packet size (64k) plus plenty of room for
475281d757SMark McLoughlin  * the ethernet and virtio_net headers
485281d757SMark McLoughlin  */
495281d757SMark McLoughlin #define TAP_BUFSIZE (4096 + 65536)
505281d757SMark McLoughlin 
515281d757SMark McLoughlin typedef struct TAPState {
523e35ba93SMark McLoughlin     VLANClientState nc;
535281d757SMark McLoughlin     int fd;
545281d757SMark McLoughlin     char down_script[1024];
555281d757SMark McLoughlin     char down_script_arg[128];
565281d757SMark McLoughlin     uint8_t buf[TAP_BUFSIZE];
575281d757SMark McLoughlin     unsigned int read_poll : 1;
585281d757SMark McLoughlin     unsigned int write_poll : 1;
595281d757SMark McLoughlin     unsigned int using_vnet_hdr : 1;
605281d757SMark McLoughlin     unsigned int has_ufo: 1;
6182b0d80eSMichael S. Tsirkin     VHostNetState *vhost_net;
62ef4252b1SMichael S. Tsirkin     unsigned host_vnet_hdr_len;
635281d757SMark McLoughlin } TAPState;
645281d757SMark McLoughlin 
655281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd);
665281d757SMark McLoughlin 
675281d757SMark McLoughlin static int tap_can_send(void *opaque);
685281d757SMark McLoughlin static void tap_send(void *opaque);
695281d757SMark McLoughlin static void tap_writable(void *opaque);
705281d757SMark McLoughlin 
715281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s)
725281d757SMark McLoughlin {
735281d757SMark McLoughlin     qemu_set_fd_handler2(s->fd,
745281d757SMark McLoughlin                          s->read_poll  ? tap_can_send : NULL,
755281d757SMark McLoughlin                          s->read_poll  ? tap_send     : NULL,
765281d757SMark McLoughlin                          s->write_poll ? tap_writable : NULL,
775281d757SMark McLoughlin                          s);
785281d757SMark McLoughlin }
795281d757SMark McLoughlin 
805281d757SMark McLoughlin static void tap_read_poll(TAPState *s, int enable)
815281d757SMark McLoughlin {
825281d757SMark McLoughlin     s->read_poll = !!enable;
835281d757SMark McLoughlin     tap_update_fd_handler(s);
845281d757SMark McLoughlin }
855281d757SMark McLoughlin 
865281d757SMark McLoughlin static void tap_write_poll(TAPState *s, int enable)
875281d757SMark McLoughlin {
885281d757SMark McLoughlin     s->write_poll = !!enable;
895281d757SMark McLoughlin     tap_update_fd_handler(s);
905281d757SMark McLoughlin }
915281d757SMark McLoughlin 
925281d757SMark McLoughlin static void tap_writable(void *opaque)
935281d757SMark McLoughlin {
945281d757SMark McLoughlin     TAPState *s = opaque;
955281d757SMark McLoughlin 
965281d757SMark McLoughlin     tap_write_poll(s, 0);
975281d757SMark McLoughlin 
983e35ba93SMark McLoughlin     qemu_flush_queued_packets(&s->nc);
995281d757SMark McLoughlin }
1005281d757SMark McLoughlin 
1015281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
1025281d757SMark McLoughlin {
1035281d757SMark McLoughlin     ssize_t len;
1045281d757SMark McLoughlin 
1055281d757SMark McLoughlin     do {
1065281d757SMark McLoughlin         len = writev(s->fd, iov, iovcnt);
1075281d757SMark McLoughlin     } while (len == -1 && errno == EINTR);
1085281d757SMark McLoughlin 
1095281d757SMark McLoughlin     if (len == -1 && errno == EAGAIN) {
1105281d757SMark McLoughlin         tap_write_poll(s, 1);
1115281d757SMark McLoughlin         return 0;
1125281d757SMark McLoughlin     }
1135281d757SMark McLoughlin 
1145281d757SMark McLoughlin     return len;
1155281d757SMark McLoughlin }
1165281d757SMark McLoughlin 
1173e35ba93SMark McLoughlin static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
1185281d757SMark McLoughlin                                int iovcnt)
1195281d757SMark McLoughlin {
1203e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1215281d757SMark McLoughlin     const struct iovec *iovp = iov;
1225281d757SMark McLoughlin     struct iovec iov_copy[iovcnt + 1];
123ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1245281d757SMark McLoughlin 
125ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1265281d757SMark McLoughlin         iov_copy[0].iov_base = &hdr;
127ef4252b1SMichael S. Tsirkin         iov_copy[0].iov_len =  s->host_vnet_hdr_len;
1285281d757SMark McLoughlin         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
1295281d757SMark McLoughlin         iovp = iov_copy;
1305281d757SMark McLoughlin         iovcnt++;
1315281d757SMark McLoughlin     }
1325281d757SMark McLoughlin 
1335281d757SMark McLoughlin     return tap_write_packet(s, iovp, iovcnt);
1345281d757SMark McLoughlin }
1355281d757SMark McLoughlin 
1363e35ba93SMark McLoughlin static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
1375281d757SMark McLoughlin {
1383e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1395281d757SMark McLoughlin     struct iovec iov[2];
1405281d757SMark McLoughlin     int iovcnt = 0;
141ef4252b1SMichael S. Tsirkin     struct virtio_net_hdr_mrg_rxbuf hdr = { };
1425281d757SMark McLoughlin 
143ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len) {
1445281d757SMark McLoughlin         iov[iovcnt].iov_base = &hdr;
145ef4252b1SMichael S. Tsirkin         iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
1465281d757SMark McLoughlin         iovcnt++;
1475281d757SMark McLoughlin     }
1485281d757SMark McLoughlin 
1495281d757SMark McLoughlin     iov[iovcnt].iov_base = (char *)buf;
1505281d757SMark McLoughlin     iov[iovcnt].iov_len  = size;
1515281d757SMark McLoughlin     iovcnt++;
1525281d757SMark McLoughlin 
1535281d757SMark McLoughlin     return tap_write_packet(s, iov, iovcnt);
1545281d757SMark McLoughlin }
1555281d757SMark McLoughlin 
1563e35ba93SMark McLoughlin static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
1575281d757SMark McLoughlin {
1583e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1595281d757SMark McLoughlin     struct iovec iov[1];
1605281d757SMark McLoughlin 
161ef4252b1SMichael S. Tsirkin     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
1623e35ba93SMark McLoughlin         return tap_receive_raw(nc, buf, size);
1635281d757SMark McLoughlin     }
1645281d757SMark McLoughlin 
1655281d757SMark McLoughlin     iov[0].iov_base = (char *)buf;
1665281d757SMark McLoughlin     iov[0].iov_len  = size;
1675281d757SMark McLoughlin 
1685281d757SMark McLoughlin     return tap_write_packet(s, iov, 1);
1695281d757SMark McLoughlin }
1705281d757SMark McLoughlin 
1715281d757SMark McLoughlin static int tap_can_send(void *opaque)
1725281d757SMark McLoughlin {
1735281d757SMark McLoughlin     TAPState *s = opaque;
1745281d757SMark McLoughlin 
1753e35ba93SMark McLoughlin     return qemu_can_send_packet(&s->nc);
1765281d757SMark McLoughlin }
1775281d757SMark McLoughlin 
178966ea5ecSMark McLoughlin #ifndef __sun__
179966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1805281d757SMark McLoughlin {
1815281d757SMark McLoughlin     return read(tapfd, buf, maxlen);
1825281d757SMark McLoughlin }
1835281d757SMark McLoughlin #endif
1845281d757SMark McLoughlin 
1853e35ba93SMark McLoughlin static void tap_send_completed(VLANClientState *nc, ssize_t len)
1865281d757SMark McLoughlin {
1873e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1885281d757SMark McLoughlin     tap_read_poll(s, 1);
1895281d757SMark McLoughlin }
1905281d757SMark McLoughlin 
1915281d757SMark McLoughlin static void tap_send(void *opaque)
1925281d757SMark McLoughlin {
1935281d757SMark McLoughlin     TAPState *s = opaque;
194be1636b3SMark McLoughlin     int size;
1955281d757SMark McLoughlin 
1965819c918SMark McLoughlin     do {
1975819c918SMark McLoughlin         uint8_t *buf = s->buf;
1985819c918SMark McLoughlin 
1995281d757SMark McLoughlin         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
2005281d757SMark McLoughlin         if (size <= 0) {
2015819c918SMark McLoughlin             break;
2025281d757SMark McLoughlin         }
2035281d757SMark McLoughlin 
204ef4252b1SMichael S. Tsirkin         if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
205ef4252b1SMichael S. Tsirkin             buf  += s->host_vnet_hdr_len;
206ef4252b1SMichael S. Tsirkin             size -= s->host_vnet_hdr_len;
2075281d757SMark McLoughlin         }
2085281d757SMark McLoughlin 
2093e35ba93SMark McLoughlin         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
2105281d757SMark McLoughlin         if (size == 0) {
2115281d757SMark McLoughlin             tap_read_poll(s, 0);
2125281d757SMark McLoughlin         }
2133e35ba93SMark McLoughlin     } while (size > 0 && qemu_can_send_packet(&s->nc));
2145281d757SMark McLoughlin }
2155281d757SMark McLoughlin 
2163e35ba93SMark McLoughlin int tap_has_ufo(VLANClientState *nc)
2175281d757SMark McLoughlin {
2183e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2195281d757SMark McLoughlin 
220665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
2215281d757SMark McLoughlin 
2225281d757SMark McLoughlin     return s->has_ufo;
2235281d757SMark McLoughlin }
2245281d757SMark McLoughlin 
2253e35ba93SMark McLoughlin int tap_has_vnet_hdr(VLANClientState *nc)
2265281d757SMark McLoughlin {
2273e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2285281d757SMark McLoughlin 
229665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
2305281d757SMark McLoughlin 
231ef4252b1SMichael S. Tsirkin     return !!s->host_vnet_hdr_len;
2325281d757SMark McLoughlin }
2335281d757SMark McLoughlin 
234445d892fSMichael S. Tsirkin int tap_has_vnet_hdr_len(VLANClientState *nc, int len)
235445d892fSMichael S. Tsirkin {
236445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
237445d892fSMichael S. Tsirkin 
238445d892fSMichael S. Tsirkin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
239445d892fSMichael S. Tsirkin 
240445d892fSMichael S. Tsirkin     return tap_probe_vnet_hdr_len(s->fd, len);
241445d892fSMichael S. Tsirkin }
242445d892fSMichael S. Tsirkin 
243445d892fSMichael S. Tsirkin void tap_set_vnet_hdr_len(VLANClientState *nc, int len)
244445d892fSMichael S. Tsirkin {
245445d892fSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
246445d892fSMichael S. Tsirkin 
247445d892fSMichael S. Tsirkin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
248445d892fSMichael S. Tsirkin     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
249445d892fSMichael S. Tsirkin            len == sizeof(struct virtio_net_hdr));
250445d892fSMichael S. Tsirkin 
251445d892fSMichael S. Tsirkin     tap_fd_set_vnet_hdr_len(s->fd, len);
252445d892fSMichael S. Tsirkin     s->host_vnet_hdr_len = len;
253445d892fSMichael S. Tsirkin }
254445d892fSMichael S. Tsirkin 
2553e35ba93SMark McLoughlin void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
2565281d757SMark McLoughlin {
2573e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2585281d757SMark McLoughlin 
2595281d757SMark McLoughlin     using_vnet_hdr = using_vnet_hdr != 0;
2605281d757SMark McLoughlin 
261665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_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 
2673e35ba93SMark McLoughlin void tap_set_offload(VLANClientState *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 
2783e35ba93SMark McLoughlin static void tap_cleanup(VLANClientState *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 
2925281d757SMark McLoughlin     tap_read_poll(s, 0);
2935281d757SMark McLoughlin     tap_write_poll(s, 0);
2945281d757SMark McLoughlin     close(s->fd);
29527a6375dSMichael S. Tsirkin     s->fd = -1;
2965281d757SMark McLoughlin }
2975281d757SMark McLoughlin 
298ceb69615SMichael S. Tsirkin static void tap_poll(VLANClientState *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 
30595d528a2SMichael S. Tsirkin int tap_get_fd(VLANClientState *nc)
30695d528a2SMichael S. Tsirkin {
30795d528a2SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
30895d528a2SMichael S. Tsirkin     assert(nc->info->type == NET_CLIENT_TYPE_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 = {
3153e35ba93SMark McLoughlin     .type = NET_CLIENT_TYPE_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 
3245281d757SMark McLoughlin static TAPState *net_tap_fd_init(VLANState *vlan,
3255281d757SMark McLoughlin                                  const char *model,
3265281d757SMark McLoughlin                                  const char *name,
3275281d757SMark McLoughlin                                  int fd,
3285281d757SMark McLoughlin                                  int vnet_hdr)
3295281d757SMark McLoughlin {
3303e35ba93SMark McLoughlin     VLANClientState *nc;
3315281d757SMark McLoughlin     TAPState *s;
3325281d757SMark McLoughlin 
3333e35ba93SMark McLoughlin     nc = qemu_new_net_client(&net_tap_info, vlan, NULL, 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;
3395281d757SMark McLoughlin     s->using_vnet_hdr = 0;
3409c282718SMark McLoughlin     s->has_ufo = tap_probe_has_ufo(s->fd);
3413e35ba93SMark McLoughlin     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
3425281d757SMark McLoughlin     tap_read_poll(s, 1);
34382b0d80eSMichael S. Tsirkin     s->vhost_net = NULL;
3445281d757SMark McLoughlin     return s;
3455281d757SMark McLoughlin }
3465281d757SMark McLoughlin 
3475281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd)
3485281d757SMark McLoughlin {
3495281d757SMark McLoughlin     int pid, status;
3505281d757SMark McLoughlin     char *args[3];
3515281d757SMark McLoughlin     char **parg;
3525281d757SMark McLoughlin 
3535281d757SMark McLoughlin     /* try to launch network script */
3545281d757SMark McLoughlin     pid = fork();
3555281d757SMark McLoughlin     if (pid == 0) {
3565281d757SMark McLoughlin         int open_max = sysconf(_SC_OPEN_MAX), i;
3575281d757SMark McLoughlin 
3585281d757SMark McLoughlin         for (i = 0; i < open_max; i++) {
3595281d757SMark McLoughlin             if (i != STDIN_FILENO &&
3605281d757SMark McLoughlin                 i != STDOUT_FILENO &&
3615281d757SMark McLoughlin                 i != STDERR_FILENO &&
3625281d757SMark McLoughlin                 i != fd) {
3635281d757SMark McLoughlin                 close(i);
3645281d757SMark McLoughlin             }
3655281d757SMark McLoughlin         }
3665281d757SMark McLoughlin         parg = args;
3675281d757SMark McLoughlin         *parg++ = (char *)setup_script;
3685281d757SMark McLoughlin         *parg++ = (char *)ifname;
3699678d950SBlue Swirl         *parg = NULL;
3705281d757SMark McLoughlin         execv(setup_script, args);
3715281d757SMark McLoughlin         _exit(1);
3725281d757SMark McLoughlin     } else if (pid > 0) {
3735281d757SMark McLoughlin         while (waitpid(pid, &status, 0) != pid) {
3745281d757SMark McLoughlin             /* loop */
3755281d757SMark McLoughlin         }
3765281d757SMark McLoughlin 
3775281d757SMark McLoughlin         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
3785281d757SMark McLoughlin             return 0;
3795281d757SMark McLoughlin         }
3805281d757SMark McLoughlin     }
3815281d757SMark McLoughlin     fprintf(stderr, "%s: could not launch network script\n", setup_script);
3825281d757SMark McLoughlin     return -1;
3835281d757SMark McLoughlin }
3845281d757SMark McLoughlin 
385*a7c36ee4SCorey Bryant static int recv_fd(int c)
386*a7c36ee4SCorey Bryant {
387*a7c36ee4SCorey Bryant     int fd;
388*a7c36ee4SCorey Bryant     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
389*a7c36ee4SCorey Bryant     struct msghdr msg = {
390*a7c36ee4SCorey Bryant         .msg_control = msgbuf,
391*a7c36ee4SCorey Bryant         .msg_controllen = sizeof(msgbuf),
392*a7c36ee4SCorey Bryant     };
393*a7c36ee4SCorey Bryant     struct cmsghdr *cmsg;
394*a7c36ee4SCorey Bryant     struct iovec iov;
395*a7c36ee4SCorey Bryant     uint8_t req[1];
396*a7c36ee4SCorey Bryant     ssize_t len;
397*a7c36ee4SCorey Bryant 
398*a7c36ee4SCorey Bryant     cmsg = CMSG_FIRSTHDR(&msg);
399*a7c36ee4SCorey Bryant     cmsg->cmsg_level = SOL_SOCKET;
400*a7c36ee4SCorey Bryant     cmsg->cmsg_type = SCM_RIGHTS;
401*a7c36ee4SCorey Bryant     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
402*a7c36ee4SCorey Bryant     msg.msg_controllen = cmsg->cmsg_len;
403*a7c36ee4SCorey Bryant 
404*a7c36ee4SCorey Bryant     iov.iov_base = req;
405*a7c36ee4SCorey Bryant     iov.iov_len = sizeof(req);
406*a7c36ee4SCorey Bryant 
407*a7c36ee4SCorey Bryant     msg.msg_iov = &iov;
408*a7c36ee4SCorey Bryant     msg.msg_iovlen = 1;
409*a7c36ee4SCorey Bryant 
410*a7c36ee4SCorey Bryant     len = recvmsg(c, &msg, 0);
411*a7c36ee4SCorey Bryant     if (len > 0) {
412*a7c36ee4SCorey Bryant         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
413*a7c36ee4SCorey Bryant         return fd;
414*a7c36ee4SCorey Bryant     }
415*a7c36ee4SCorey Bryant 
416*a7c36ee4SCorey Bryant     return len;
417*a7c36ee4SCorey Bryant }
418*a7c36ee4SCorey Bryant 
419*a7c36ee4SCorey Bryant static int net_bridge_run_helper(const char *helper, const char *bridge)
420*a7c36ee4SCorey Bryant {
421*a7c36ee4SCorey Bryant     sigset_t oldmask, mask;
422*a7c36ee4SCorey Bryant     int pid, status;
423*a7c36ee4SCorey Bryant     char *args[5];
424*a7c36ee4SCorey Bryant     char **parg;
425*a7c36ee4SCorey Bryant     int sv[2];
426*a7c36ee4SCorey Bryant 
427*a7c36ee4SCorey Bryant     sigemptyset(&mask);
428*a7c36ee4SCorey Bryant     sigaddset(&mask, SIGCHLD);
429*a7c36ee4SCorey Bryant     sigprocmask(SIG_BLOCK, &mask, &oldmask);
430*a7c36ee4SCorey Bryant 
431*a7c36ee4SCorey Bryant     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
432*a7c36ee4SCorey Bryant         return -1;
433*a7c36ee4SCorey Bryant     }
434*a7c36ee4SCorey Bryant 
435*a7c36ee4SCorey Bryant     /* try to launch bridge helper */
436*a7c36ee4SCorey Bryant     pid = fork();
437*a7c36ee4SCorey Bryant     if (pid == 0) {
438*a7c36ee4SCorey Bryant         int open_max = sysconf(_SC_OPEN_MAX), i;
439*a7c36ee4SCorey Bryant         char fd_buf[6+10];
440*a7c36ee4SCorey Bryant         char br_buf[6+IFNAMSIZ] = {0};
441*a7c36ee4SCorey Bryant         char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
442*a7c36ee4SCorey Bryant 
443*a7c36ee4SCorey Bryant         for (i = 0; i < open_max; i++) {
444*a7c36ee4SCorey Bryant             if (i != STDIN_FILENO &&
445*a7c36ee4SCorey Bryant                 i != STDOUT_FILENO &&
446*a7c36ee4SCorey Bryant                 i != STDERR_FILENO &&
447*a7c36ee4SCorey Bryant                 i != sv[1]) {
448*a7c36ee4SCorey Bryant                 close(i);
449*a7c36ee4SCorey Bryant             }
450*a7c36ee4SCorey Bryant         }
451*a7c36ee4SCorey Bryant 
452*a7c36ee4SCorey Bryant         snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
453*a7c36ee4SCorey Bryant 
454*a7c36ee4SCorey Bryant         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
455*a7c36ee4SCorey Bryant             /* assume helper is a command */
456*a7c36ee4SCorey Bryant 
457*a7c36ee4SCorey Bryant             if (strstr(helper, "--br=") == NULL) {
458*a7c36ee4SCorey Bryant                 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
459*a7c36ee4SCorey Bryant             }
460*a7c36ee4SCorey Bryant 
461*a7c36ee4SCorey Bryant             snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
462*a7c36ee4SCorey Bryant                      helper, "--use-vnet", fd_buf, br_buf);
463*a7c36ee4SCorey Bryant 
464*a7c36ee4SCorey Bryant             parg = args;
465*a7c36ee4SCorey Bryant             *parg++ = (char *)"sh";
466*a7c36ee4SCorey Bryant             *parg++ = (char *)"-c";
467*a7c36ee4SCorey Bryant             *parg++ = helper_cmd;
468*a7c36ee4SCorey Bryant             *parg++ = NULL;
469*a7c36ee4SCorey Bryant 
470*a7c36ee4SCorey Bryant             execv("/bin/sh", args);
471*a7c36ee4SCorey Bryant         } else {
472*a7c36ee4SCorey Bryant             /* assume helper is just the executable path name */
473*a7c36ee4SCorey Bryant 
474*a7c36ee4SCorey Bryant             snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
475*a7c36ee4SCorey Bryant 
476*a7c36ee4SCorey Bryant             parg = args;
477*a7c36ee4SCorey Bryant             *parg++ = (char *)helper;
478*a7c36ee4SCorey Bryant             *parg++ = (char *)"--use-vnet";
479*a7c36ee4SCorey Bryant             *parg++ = fd_buf;
480*a7c36ee4SCorey Bryant             *parg++ = br_buf;
481*a7c36ee4SCorey Bryant             *parg++ = NULL;
482*a7c36ee4SCorey Bryant 
483*a7c36ee4SCorey Bryant             execv(helper, args);
484*a7c36ee4SCorey Bryant         }
485*a7c36ee4SCorey Bryant         _exit(1);
486*a7c36ee4SCorey Bryant 
487*a7c36ee4SCorey Bryant     } else if (pid > 0) {
488*a7c36ee4SCorey Bryant         int fd;
489*a7c36ee4SCorey Bryant 
490*a7c36ee4SCorey Bryant         close(sv[1]);
491*a7c36ee4SCorey Bryant 
492*a7c36ee4SCorey Bryant         do {
493*a7c36ee4SCorey Bryant             fd = recv_fd(sv[0]);
494*a7c36ee4SCorey Bryant         } while (fd == -1 && errno == EINTR);
495*a7c36ee4SCorey Bryant 
496*a7c36ee4SCorey Bryant         close(sv[0]);
497*a7c36ee4SCorey Bryant 
498*a7c36ee4SCorey Bryant         while (waitpid(pid, &status, 0) != pid) {
499*a7c36ee4SCorey Bryant             /* loop */
500*a7c36ee4SCorey Bryant         }
501*a7c36ee4SCorey Bryant         sigprocmask(SIG_SETMASK, &oldmask, NULL);
502*a7c36ee4SCorey Bryant         if (fd < 0) {
503*a7c36ee4SCorey Bryant             fprintf(stderr, "failed to recv file descriptor\n");
504*a7c36ee4SCorey Bryant             return -1;
505*a7c36ee4SCorey Bryant         }
506*a7c36ee4SCorey Bryant 
507*a7c36ee4SCorey Bryant         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
508*a7c36ee4SCorey Bryant             return fd;
509*a7c36ee4SCorey Bryant         }
510*a7c36ee4SCorey Bryant     }
511*a7c36ee4SCorey Bryant     fprintf(stderr, "failed to launch bridge helper\n");
512*a7c36ee4SCorey Bryant     return -1;
513*a7c36ee4SCorey Bryant }
514*a7c36ee4SCorey Bryant 
515*a7c36ee4SCorey Bryant int net_init_bridge(QemuOpts *opts, Monitor *mon, const char *name,
516*a7c36ee4SCorey Bryant                     VLANState *vlan)
517*a7c36ee4SCorey Bryant {
518*a7c36ee4SCorey Bryant     TAPState *s;
519*a7c36ee4SCorey Bryant     int fd, vnet_hdr;
520*a7c36ee4SCorey Bryant 
521*a7c36ee4SCorey Bryant     if (!qemu_opt_get(opts, "br")) {
522*a7c36ee4SCorey Bryant         qemu_opt_set(opts, "br", DEFAULT_BRIDGE_INTERFACE);
523*a7c36ee4SCorey Bryant     }
524*a7c36ee4SCorey Bryant     if (!qemu_opt_get(opts, "helper")) {
525*a7c36ee4SCorey Bryant         qemu_opt_set(opts, "helper", DEFAULT_BRIDGE_HELPER);
526*a7c36ee4SCorey Bryant     }
527*a7c36ee4SCorey Bryant 
528*a7c36ee4SCorey Bryant     fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
529*a7c36ee4SCorey Bryant                                qemu_opt_get(opts, "br"));
530*a7c36ee4SCorey Bryant     if (fd == -1) {
531*a7c36ee4SCorey Bryant         return -1;
532*a7c36ee4SCorey Bryant     }
533*a7c36ee4SCorey Bryant 
534*a7c36ee4SCorey Bryant     fcntl(fd, F_SETFL, O_NONBLOCK);
535*a7c36ee4SCorey Bryant 
536*a7c36ee4SCorey Bryant     vnet_hdr = tap_probe_vnet_hdr(fd);
537*a7c36ee4SCorey Bryant 
538*a7c36ee4SCorey Bryant     s = net_tap_fd_init(vlan, "bridge", name, fd, vnet_hdr);
539*a7c36ee4SCorey Bryant     if (!s) {
540*a7c36ee4SCorey Bryant         close(fd);
541*a7c36ee4SCorey Bryant         return -1;
542*a7c36ee4SCorey Bryant     }
543*a7c36ee4SCorey Bryant 
544*a7c36ee4SCorey Bryant     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s",
545*a7c36ee4SCorey Bryant              qemu_opt_get(opts, "helper"), qemu_opt_get(opts, "br"));
546*a7c36ee4SCorey Bryant 
547*a7c36ee4SCorey Bryant     return 0;
548*a7c36ee4SCorey Bryant }
549*a7c36ee4SCorey Bryant 
5505281d757SMark McLoughlin static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
5515281d757SMark McLoughlin {
5525281d757SMark McLoughlin     int fd, vnet_hdr_required;
5535281d757SMark McLoughlin     char ifname[128] = {0,};
5545281d757SMark McLoughlin     const char *setup_script;
5555281d757SMark McLoughlin 
5565281d757SMark McLoughlin     if (qemu_opt_get(opts, "ifname")) {
5575281d757SMark McLoughlin         pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
5585281d757SMark McLoughlin     }
5595281d757SMark McLoughlin 
5605281d757SMark McLoughlin     *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
5615281d757SMark McLoughlin     if (qemu_opt_get(opts, "vnet_hdr")) {
5625281d757SMark McLoughlin         vnet_hdr_required = *vnet_hdr;
5635281d757SMark McLoughlin     } else {
5645281d757SMark McLoughlin         vnet_hdr_required = 0;
5655281d757SMark McLoughlin     }
5665281d757SMark McLoughlin 
5675281d757SMark McLoughlin     TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
5685281d757SMark McLoughlin     if (fd < 0) {
5695281d757SMark McLoughlin         return -1;
5705281d757SMark McLoughlin     }
5715281d757SMark McLoughlin 
5725281d757SMark McLoughlin     setup_script = qemu_opt_get(opts, "script");
5735281d757SMark McLoughlin     if (setup_script &&
5745281d757SMark McLoughlin         setup_script[0] != '\0' &&
5755281d757SMark McLoughlin         strcmp(setup_script, "no") != 0 &&
5765281d757SMark McLoughlin         launch_script(setup_script, ifname, fd)) {
5775281d757SMark McLoughlin         close(fd);
5785281d757SMark McLoughlin         return -1;
5795281d757SMark McLoughlin     }
5805281d757SMark McLoughlin 
5815281d757SMark McLoughlin     qemu_opt_set(opts, "ifname", ifname);
5825281d757SMark McLoughlin 
5835281d757SMark McLoughlin     return fd;
5845281d757SMark McLoughlin }
5855281d757SMark McLoughlin 
5865281d757SMark McLoughlin int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
5875281d757SMark McLoughlin {
5885281d757SMark McLoughlin     TAPState *s;
589df6c2a0fSMark McLoughlin     int fd, vnet_hdr = 0;
590*a7c36ee4SCorey Bryant     const char *model;
5915281d757SMark McLoughlin 
5925281d757SMark McLoughlin     if (qemu_opt_get(opts, "fd")) {
5935281d757SMark McLoughlin         if (qemu_opt_get(opts, "ifname") ||
5945281d757SMark McLoughlin             qemu_opt_get(opts, "script") ||
5955281d757SMark McLoughlin             qemu_opt_get(opts, "downscript") ||
596*a7c36ee4SCorey Bryant             qemu_opt_get(opts, "vnet_hdr") ||
597*a7c36ee4SCorey Bryant             qemu_opt_get(opts, "helper")) {
598*a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, vnet_hdr=, "
599*a7c36ee4SCorey Bryant                          "and helper= are invalid with fd=");
6005281d757SMark McLoughlin             return -1;
6015281d757SMark McLoughlin         }
6025281d757SMark McLoughlin 
6035281d757SMark McLoughlin         fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
6045281d757SMark McLoughlin         if (fd == -1) {
6055281d757SMark McLoughlin             return -1;
6065281d757SMark McLoughlin         }
6075281d757SMark McLoughlin 
6085281d757SMark McLoughlin         fcntl(fd, F_SETFL, O_NONBLOCK);
6095281d757SMark McLoughlin 
6105281d757SMark McLoughlin         vnet_hdr = tap_probe_vnet_hdr(fd);
611*a7c36ee4SCorey Bryant 
612*a7c36ee4SCorey Bryant         model = "tap";
613*a7c36ee4SCorey Bryant 
614*a7c36ee4SCorey Bryant     } else if (qemu_opt_get(opts, "helper")) {
615*a7c36ee4SCorey Bryant         if (qemu_opt_get(opts, "ifname") ||
616*a7c36ee4SCorey Bryant             qemu_opt_get(opts, "script") ||
617*a7c36ee4SCorey Bryant             qemu_opt_get(opts, "downscript") ||
618*a7c36ee4SCorey Bryant             qemu_opt_get(opts, "vnet_hdr")) {
619*a7c36ee4SCorey Bryant             error_report("ifname=, script=, downscript=, and vnet_hdr= "
620*a7c36ee4SCorey Bryant                          "are invalid with helper=");
621*a7c36ee4SCorey Bryant             return -1;
622*a7c36ee4SCorey Bryant         }
623*a7c36ee4SCorey Bryant 
624*a7c36ee4SCorey Bryant         fd = net_bridge_run_helper(qemu_opt_get(opts, "helper"),
625*a7c36ee4SCorey Bryant                                    DEFAULT_BRIDGE_INTERFACE);
626*a7c36ee4SCorey Bryant         if (fd == -1) {
627*a7c36ee4SCorey Bryant             return -1;
628*a7c36ee4SCorey Bryant         }
629*a7c36ee4SCorey Bryant 
630*a7c36ee4SCorey Bryant         fcntl(fd, F_SETFL, O_NONBLOCK);
631*a7c36ee4SCorey Bryant 
632*a7c36ee4SCorey Bryant         vnet_hdr = tap_probe_vnet_hdr(fd);
633*a7c36ee4SCorey Bryant 
634*a7c36ee4SCorey Bryant         model = "bridge";
635*a7c36ee4SCorey Bryant 
6365281d757SMark McLoughlin     } else {
6375281d757SMark McLoughlin         if (!qemu_opt_get(opts, "script")) {
6385281d757SMark McLoughlin             qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
6395281d757SMark McLoughlin         }
6405281d757SMark McLoughlin 
6415281d757SMark McLoughlin         if (!qemu_opt_get(opts, "downscript")) {
6425281d757SMark McLoughlin             qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
6435281d757SMark McLoughlin         }
6445281d757SMark McLoughlin 
6455281d757SMark McLoughlin         fd = net_tap_init(opts, &vnet_hdr);
646929fe497SJuergen Lock         if (fd == -1) {
647929fe497SJuergen Lock             return -1;
648929fe497SJuergen Lock         }
649*a7c36ee4SCorey Bryant 
650*a7c36ee4SCorey Bryant         model = "tap";
6515281d757SMark McLoughlin     }
6525281d757SMark McLoughlin 
653*a7c36ee4SCorey Bryant     s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
6545281d757SMark McLoughlin     if (!s) {
6555281d757SMark McLoughlin         close(fd);
6565281d757SMark McLoughlin         return -1;
6575281d757SMark McLoughlin     }
6585281d757SMark McLoughlin 
65915ac913bSMark McLoughlin     if (tap_set_sndbuf(s->fd, opts) < 0) {
6605281d757SMark McLoughlin         return -1;
6615281d757SMark McLoughlin     }
6625281d757SMark McLoughlin 
6635281d757SMark McLoughlin     if (qemu_opt_get(opts, "fd")) {
6643e35ba93SMark McLoughlin         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
665*a7c36ee4SCorey Bryant     } else if (qemu_opt_get(opts, "helper")) {
666*a7c36ee4SCorey Bryant         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
667*a7c36ee4SCorey Bryant                  "helper=%s", qemu_opt_get(opts, "helper"));
6685281d757SMark McLoughlin     } else {
6695281d757SMark McLoughlin         const char *ifname, *script, *downscript;
6705281d757SMark McLoughlin 
6715281d757SMark McLoughlin         ifname     = qemu_opt_get(opts, "ifname");
6725281d757SMark McLoughlin         script     = qemu_opt_get(opts, "script");
6735281d757SMark McLoughlin         downscript = qemu_opt_get(opts, "downscript");
6745281d757SMark McLoughlin 
6753e35ba93SMark McLoughlin         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
6765281d757SMark McLoughlin                  "ifname=%s,script=%s,downscript=%s",
6775281d757SMark McLoughlin                  ifname, script, downscript);
6785281d757SMark McLoughlin 
6795281d757SMark McLoughlin         if (strcmp(downscript, "no") != 0) {
6805281d757SMark McLoughlin             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
6815281d757SMark McLoughlin             snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
6825281d757SMark McLoughlin         }
6835281d757SMark McLoughlin     }
6845281d757SMark McLoughlin 
6855430a28fSmst@redhat.com     if (qemu_opt_get_bool(opts, "vhost", !!qemu_opt_get(opts, "vhostfd") ||
6865430a28fSmst@redhat.com                           qemu_opt_get_bool(opts, "vhostforce", false))) {
68782b0d80eSMichael S. Tsirkin         int vhostfd, r;
6885430a28fSmst@redhat.com         bool force = qemu_opt_get_bool(opts, "vhostforce", false);
68982b0d80eSMichael S. Tsirkin         if (qemu_opt_get(opts, "vhostfd")) {
69082b0d80eSMichael S. Tsirkin             r = net_handle_fd_param(mon, qemu_opt_get(opts, "vhostfd"));
69182b0d80eSMichael S. Tsirkin             if (r == -1) {
69282b0d80eSMichael S. Tsirkin                 return -1;
69382b0d80eSMichael S. Tsirkin             }
69482b0d80eSMichael S. Tsirkin             vhostfd = r;
69582b0d80eSMichael S. Tsirkin         } else {
69682b0d80eSMichael S. Tsirkin             vhostfd = -1;
69782b0d80eSMichael S. Tsirkin         }
6985430a28fSmst@redhat.com         s->vhost_net = vhost_net_init(&s->nc, vhostfd, force);
69982b0d80eSMichael S. Tsirkin         if (!s->vhost_net) {
70082b0d80eSMichael S. Tsirkin             error_report("vhost-net requested but could not be initialized");
70182b0d80eSMichael S. Tsirkin             return -1;
70282b0d80eSMichael S. Tsirkin         }
70382b0d80eSMichael S. Tsirkin     } else if (qemu_opt_get(opts, "vhostfd")) {
70482b0d80eSMichael S. Tsirkin         error_report("vhostfd= is not valid without vhost");
70582b0d80eSMichael S. Tsirkin         return -1;
70682b0d80eSMichael S. Tsirkin     }
70782b0d80eSMichael S. Tsirkin 
7085281d757SMark McLoughlin     return 0;
7095281d757SMark McLoughlin }
710b202554cSMichael S. Tsirkin 
711b202554cSMichael S. Tsirkin VHostNetState *tap_get_vhost_net(VLANClientState *nc)
712b202554cSMichael S. Tsirkin {
713b202554cSMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
714b202554cSMichael S. Tsirkin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
715b202554cSMichael S. Tsirkin     return s->vhost_net;
716b202554cSMichael S. Tsirkin }
717