xref: /qemu/net/tap.c (revision 82b0d80ef6a37b46bac1311c31935dc130a5a703)
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 <signal.h>
315281d757SMark McLoughlin #include <sys/ioctl.h>
325281d757SMark McLoughlin #include <sys/stat.h>
335281d757SMark McLoughlin #include <sys/wait.h>
3471f4effcSAlexander Graf #include <sys/socket.h>
355281d757SMark McLoughlin #include <net/if.h>
365281d757SMark McLoughlin 
375281d757SMark McLoughlin #include "net.h"
385281d757SMark McLoughlin #include "sysemu.h"
395281d757SMark McLoughlin #include "qemu-char.h"
405281d757SMark McLoughlin #include "qemu-common.h"
412f792016SMarkus Armbruster #include "qemu-error.h"
425281d757SMark McLoughlin 
435281d757SMark McLoughlin #include "net/tap-linux.h"
445281d757SMark McLoughlin 
45*82b0d80eSMichael S. Tsirkin #include "hw/vhost_net.h"
46*82b0d80eSMichael 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 {
533e35ba93SMark McLoughlin     VLANClientState 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];
585281d757SMark McLoughlin     unsigned int read_poll : 1;
595281d757SMark McLoughlin     unsigned int write_poll : 1;
605281d757SMark McLoughlin     unsigned int has_vnet_hdr : 1;
615281d757SMark McLoughlin     unsigned int using_vnet_hdr : 1;
625281d757SMark McLoughlin     unsigned int has_ufo: 1;
63*82b0d80eSMichael S. Tsirkin     VHostNetState *vhost_net;
645281d757SMark McLoughlin } TAPState;
655281d757SMark McLoughlin 
665281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd);
675281d757SMark McLoughlin 
685281d757SMark McLoughlin static int tap_can_send(void *opaque);
695281d757SMark McLoughlin static void tap_send(void *opaque);
705281d757SMark McLoughlin static void tap_writable(void *opaque);
715281d757SMark McLoughlin 
725281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s)
735281d757SMark McLoughlin {
745281d757SMark McLoughlin     qemu_set_fd_handler2(s->fd,
755281d757SMark McLoughlin                          s->read_poll  ? tap_can_send : NULL,
765281d757SMark McLoughlin                          s->read_poll  ? tap_send     : NULL,
775281d757SMark McLoughlin                          s->write_poll ? tap_writable : NULL,
785281d757SMark McLoughlin                          s);
795281d757SMark McLoughlin }
805281d757SMark McLoughlin 
815281d757SMark McLoughlin static void tap_read_poll(TAPState *s, int enable)
825281d757SMark McLoughlin {
835281d757SMark McLoughlin     s->read_poll = !!enable;
845281d757SMark McLoughlin     tap_update_fd_handler(s);
855281d757SMark McLoughlin }
865281d757SMark McLoughlin 
875281d757SMark McLoughlin static void tap_write_poll(TAPState *s, int enable)
885281d757SMark McLoughlin {
895281d757SMark McLoughlin     s->write_poll = !!enable;
905281d757SMark McLoughlin     tap_update_fd_handler(s);
915281d757SMark McLoughlin }
925281d757SMark McLoughlin 
935281d757SMark McLoughlin static void tap_writable(void *opaque)
945281d757SMark McLoughlin {
955281d757SMark McLoughlin     TAPState *s = opaque;
965281d757SMark McLoughlin 
975281d757SMark McLoughlin     tap_write_poll(s, 0);
985281d757SMark McLoughlin 
993e35ba93SMark McLoughlin     qemu_flush_queued_packets(&s->nc);
1005281d757SMark McLoughlin }
1015281d757SMark McLoughlin 
1025281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
1035281d757SMark McLoughlin {
1045281d757SMark McLoughlin     ssize_t len;
1055281d757SMark McLoughlin 
1065281d757SMark McLoughlin     do {
1075281d757SMark McLoughlin         len = writev(s->fd, iov, iovcnt);
1085281d757SMark McLoughlin     } while (len == -1 && errno == EINTR);
1095281d757SMark McLoughlin 
1105281d757SMark McLoughlin     if (len == -1 && errno == EAGAIN) {
1115281d757SMark McLoughlin         tap_write_poll(s, 1);
1125281d757SMark McLoughlin         return 0;
1135281d757SMark McLoughlin     }
1145281d757SMark McLoughlin 
1155281d757SMark McLoughlin     return len;
1165281d757SMark McLoughlin }
1175281d757SMark McLoughlin 
1183e35ba93SMark McLoughlin static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
1195281d757SMark McLoughlin                                int iovcnt)
1205281d757SMark McLoughlin {
1213e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1225281d757SMark McLoughlin     const struct iovec *iovp = iov;
1235281d757SMark McLoughlin     struct iovec iov_copy[iovcnt + 1];
1245281d757SMark McLoughlin     struct virtio_net_hdr hdr = { 0, };
1255281d757SMark McLoughlin 
1265281d757SMark McLoughlin     if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1275281d757SMark McLoughlin         iov_copy[0].iov_base = &hdr;
1285281d757SMark McLoughlin         iov_copy[0].iov_len =  sizeof(hdr);
1295281d757SMark McLoughlin         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
1305281d757SMark McLoughlin         iovp = iov_copy;
1315281d757SMark McLoughlin         iovcnt++;
1325281d757SMark McLoughlin     }
1335281d757SMark McLoughlin 
1345281d757SMark McLoughlin     return tap_write_packet(s, iovp, iovcnt);
1355281d757SMark McLoughlin }
1365281d757SMark McLoughlin 
1373e35ba93SMark McLoughlin static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
1385281d757SMark McLoughlin {
1393e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1405281d757SMark McLoughlin     struct iovec iov[2];
1415281d757SMark McLoughlin     int iovcnt = 0;
1425281d757SMark McLoughlin     struct virtio_net_hdr hdr = { 0, };
1435281d757SMark McLoughlin 
1445281d757SMark McLoughlin     if (s->has_vnet_hdr) {
1455281d757SMark McLoughlin         iov[iovcnt].iov_base = &hdr;
1465281d757SMark McLoughlin         iov[iovcnt].iov_len  = sizeof(hdr);
1475281d757SMark McLoughlin         iovcnt++;
1485281d757SMark McLoughlin     }
1495281d757SMark McLoughlin 
1505281d757SMark McLoughlin     iov[iovcnt].iov_base = (char *)buf;
1515281d757SMark McLoughlin     iov[iovcnt].iov_len  = size;
1525281d757SMark McLoughlin     iovcnt++;
1535281d757SMark McLoughlin 
1545281d757SMark McLoughlin     return tap_write_packet(s, iov, iovcnt);
1555281d757SMark McLoughlin }
1565281d757SMark McLoughlin 
1573e35ba93SMark McLoughlin static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
1585281d757SMark McLoughlin {
1593e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1605281d757SMark McLoughlin     struct iovec iov[1];
1615281d757SMark McLoughlin 
1625281d757SMark McLoughlin     if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1633e35ba93SMark McLoughlin         return tap_receive_raw(nc, buf, size);
1645281d757SMark McLoughlin     }
1655281d757SMark McLoughlin 
1665281d757SMark McLoughlin     iov[0].iov_base = (char *)buf;
1675281d757SMark McLoughlin     iov[0].iov_len  = size;
1685281d757SMark McLoughlin 
1695281d757SMark McLoughlin     return tap_write_packet(s, iov, 1);
1705281d757SMark McLoughlin }
1715281d757SMark McLoughlin 
1725281d757SMark McLoughlin static int tap_can_send(void *opaque)
1735281d757SMark McLoughlin {
1745281d757SMark McLoughlin     TAPState *s = opaque;
1755281d757SMark McLoughlin 
1763e35ba93SMark McLoughlin     return qemu_can_send_packet(&s->nc);
1775281d757SMark McLoughlin }
1785281d757SMark McLoughlin 
179966ea5ecSMark McLoughlin #ifndef __sun__
180966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1815281d757SMark McLoughlin {
1825281d757SMark McLoughlin     return read(tapfd, buf, maxlen);
1835281d757SMark McLoughlin }
1845281d757SMark McLoughlin #endif
1855281d757SMark McLoughlin 
1863e35ba93SMark McLoughlin static void tap_send_completed(VLANClientState *nc, ssize_t len)
1875281d757SMark McLoughlin {
1883e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1895281d757SMark McLoughlin     tap_read_poll(s, 1);
1905281d757SMark McLoughlin }
1915281d757SMark McLoughlin 
1925281d757SMark McLoughlin static void tap_send(void *opaque)
1935281d757SMark McLoughlin {
1945281d757SMark McLoughlin     TAPState *s = opaque;
195be1636b3SMark McLoughlin     int size;
1965281d757SMark McLoughlin 
1975819c918SMark McLoughlin     do {
1985819c918SMark McLoughlin         uint8_t *buf = s->buf;
1995819c918SMark McLoughlin 
2005281d757SMark McLoughlin         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
2015281d757SMark McLoughlin         if (size <= 0) {
2025819c918SMark McLoughlin             break;
2035281d757SMark McLoughlin         }
2045281d757SMark McLoughlin 
2055281d757SMark McLoughlin         if (s->has_vnet_hdr && !s->using_vnet_hdr) {
2065281d757SMark McLoughlin             buf  += sizeof(struct virtio_net_hdr);
2075281d757SMark McLoughlin             size -= sizeof(struct virtio_net_hdr);
2085281d757SMark McLoughlin         }
2095281d757SMark McLoughlin 
2103e35ba93SMark McLoughlin         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
2115281d757SMark McLoughlin         if (size == 0) {
2125281d757SMark McLoughlin             tap_read_poll(s, 0);
2135281d757SMark McLoughlin         }
2143e35ba93SMark McLoughlin     } while (size > 0 && qemu_can_send_packet(&s->nc));
2155281d757SMark McLoughlin }
2165281d757SMark McLoughlin 
2173e35ba93SMark McLoughlin int tap_has_ufo(VLANClientState *nc)
2185281d757SMark McLoughlin {
2193e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2205281d757SMark McLoughlin 
221665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
2225281d757SMark McLoughlin 
2235281d757SMark McLoughlin     return s->has_ufo;
2245281d757SMark McLoughlin }
2255281d757SMark McLoughlin 
2263e35ba93SMark McLoughlin int tap_has_vnet_hdr(VLANClientState *nc)
2275281d757SMark McLoughlin {
2283e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2295281d757SMark McLoughlin 
230665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
2315281d757SMark McLoughlin 
2325281d757SMark McLoughlin     return s->has_vnet_hdr;
2335281d757SMark McLoughlin }
2345281d757SMark McLoughlin 
2353e35ba93SMark McLoughlin void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
2365281d757SMark McLoughlin {
2373e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2385281d757SMark McLoughlin 
2395281d757SMark McLoughlin     using_vnet_hdr = using_vnet_hdr != 0;
2405281d757SMark McLoughlin 
241665a3b07SMark McLoughlin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
2425281d757SMark McLoughlin     assert(s->has_vnet_hdr == using_vnet_hdr);
2435281d757SMark McLoughlin 
2445281d757SMark McLoughlin     s->using_vnet_hdr = using_vnet_hdr;
2455281d757SMark McLoughlin }
2465281d757SMark McLoughlin 
2473e35ba93SMark McLoughlin void tap_set_offload(VLANClientState *nc, int csum, int tso4,
2485281d757SMark McLoughlin                      int tso6, int ecn, int ufo)
2495281d757SMark McLoughlin {
2503e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2515281d757SMark McLoughlin 
2521faac1f7SMark McLoughlin     return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
2535281d757SMark McLoughlin }
2545281d757SMark McLoughlin 
2553e35ba93SMark McLoughlin static void tap_cleanup(VLANClientState *nc)
2565281d757SMark McLoughlin {
2573e35ba93SMark McLoughlin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
2585281d757SMark McLoughlin 
259*82b0d80eSMichael S. Tsirkin     if (s->vhost_net) {
260*82b0d80eSMichael S. Tsirkin         vhost_net_cleanup(s->vhost_net);
261*82b0d80eSMichael S. Tsirkin     }
262*82b0d80eSMichael S. Tsirkin 
2633e35ba93SMark McLoughlin     qemu_purge_queued_packets(nc);
2645281d757SMark McLoughlin 
2655281d757SMark McLoughlin     if (s->down_script[0])
2665281d757SMark McLoughlin         launch_script(s->down_script, s->down_script_arg, s->fd);
2675281d757SMark McLoughlin 
2685281d757SMark McLoughlin     tap_read_poll(s, 0);
2695281d757SMark McLoughlin     tap_write_poll(s, 0);
2705281d757SMark McLoughlin     close(s->fd);
2715281d757SMark McLoughlin }
2725281d757SMark McLoughlin 
273ceb69615SMichael S. Tsirkin static void tap_poll(VLANClientState *nc, bool enable)
274ceb69615SMichael S. Tsirkin {
275ceb69615SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
276ceb69615SMichael S. Tsirkin     tap_read_poll(s, enable);
277ceb69615SMichael S. Tsirkin     tap_write_poll(s, enable);
278ceb69615SMichael S. Tsirkin }
279ceb69615SMichael S. Tsirkin 
28095d528a2SMichael S. Tsirkin int tap_get_fd(VLANClientState *nc)
28195d528a2SMichael S. Tsirkin {
28295d528a2SMichael S. Tsirkin     TAPState *s = DO_UPCAST(TAPState, nc, nc);
28395d528a2SMichael S. Tsirkin     assert(nc->info->type == NET_CLIENT_TYPE_TAP);
28495d528a2SMichael S. Tsirkin     return s->fd;
28595d528a2SMichael S. Tsirkin }
28695d528a2SMichael S. Tsirkin 
2875281d757SMark McLoughlin /* fd support */
2885281d757SMark McLoughlin 
2893e35ba93SMark McLoughlin static NetClientInfo net_tap_info = {
2903e35ba93SMark McLoughlin     .type = NET_CLIENT_TYPE_TAP,
2913e35ba93SMark McLoughlin     .size = sizeof(TAPState),
2923e35ba93SMark McLoughlin     .receive = tap_receive,
2933e35ba93SMark McLoughlin     .receive_raw = tap_receive_raw,
2943e35ba93SMark McLoughlin     .receive_iov = tap_receive_iov,
295ceb69615SMichael S. Tsirkin     .poll = tap_poll,
2963e35ba93SMark McLoughlin     .cleanup = tap_cleanup,
2973e35ba93SMark McLoughlin };
2983e35ba93SMark McLoughlin 
2995281d757SMark McLoughlin static TAPState *net_tap_fd_init(VLANState *vlan,
3005281d757SMark McLoughlin                                  const char *model,
3015281d757SMark McLoughlin                                  const char *name,
3025281d757SMark McLoughlin                                  int fd,
3035281d757SMark McLoughlin                                  int vnet_hdr)
3045281d757SMark McLoughlin {
3053e35ba93SMark McLoughlin     VLANClientState *nc;
3065281d757SMark McLoughlin     TAPState *s;
3075281d757SMark McLoughlin 
3083e35ba93SMark McLoughlin     nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
3093e35ba93SMark McLoughlin 
3103e35ba93SMark McLoughlin     s = DO_UPCAST(TAPState, nc, nc);
3113e35ba93SMark McLoughlin 
3125281d757SMark McLoughlin     s->fd = fd;
3135281d757SMark McLoughlin     s->has_vnet_hdr = vnet_hdr != 0;
3145281d757SMark McLoughlin     s->using_vnet_hdr = 0;
3159c282718SMark McLoughlin     s->has_ufo = tap_probe_has_ufo(s->fd);
3163e35ba93SMark McLoughlin     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
3175281d757SMark McLoughlin     tap_read_poll(s, 1);
318*82b0d80eSMichael S. Tsirkin     s->vhost_net = NULL;
3195281d757SMark McLoughlin     return s;
3205281d757SMark McLoughlin }
3215281d757SMark McLoughlin 
3225281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd)
3235281d757SMark McLoughlin {
3245281d757SMark McLoughlin     sigset_t oldmask, mask;
3255281d757SMark McLoughlin     int pid, status;
3265281d757SMark McLoughlin     char *args[3];
3275281d757SMark McLoughlin     char **parg;
3285281d757SMark McLoughlin 
3295281d757SMark McLoughlin     sigemptyset(&mask);
3305281d757SMark McLoughlin     sigaddset(&mask, SIGCHLD);
3315281d757SMark McLoughlin     sigprocmask(SIG_BLOCK, &mask, &oldmask);
3325281d757SMark McLoughlin 
3335281d757SMark McLoughlin     /* try to launch network script */
3345281d757SMark McLoughlin     pid = fork();
3355281d757SMark McLoughlin     if (pid == 0) {
3365281d757SMark McLoughlin         int open_max = sysconf(_SC_OPEN_MAX), i;
3375281d757SMark McLoughlin 
3385281d757SMark McLoughlin         for (i = 0; i < open_max; i++) {
3395281d757SMark McLoughlin             if (i != STDIN_FILENO &&
3405281d757SMark McLoughlin                 i != STDOUT_FILENO &&
3415281d757SMark McLoughlin                 i != STDERR_FILENO &&
3425281d757SMark McLoughlin                 i != fd) {
3435281d757SMark McLoughlin                 close(i);
3445281d757SMark McLoughlin             }
3455281d757SMark McLoughlin         }
3465281d757SMark McLoughlin         parg = args;
3475281d757SMark McLoughlin         *parg++ = (char *)setup_script;
3485281d757SMark McLoughlin         *parg++ = (char *)ifname;
3495281d757SMark McLoughlin         *parg++ = NULL;
3505281d757SMark McLoughlin         execv(setup_script, args);
3515281d757SMark McLoughlin         _exit(1);
3525281d757SMark McLoughlin     } else if (pid > 0) {
3535281d757SMark McLoughlin         while (waitpid(pid, &status, 0) != pid) {
3545281d757SMark McLoughlin             /* loop */
3555281d757SMark McLoughlin         }
3565281d757SMark McLoughlin         sigprocmask(SIG_SETMASK, &oldmask, NULL);
3575281d757SMark McLoughlin 
3585281d757SMark McLoughlin         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
3595281d757SMark McLoughlin             return 0;
3605281d757SMark McLoughlin         }
3615281d757SMark McLoughlin     }
3625281d757SMark McLoughlin     fprintf(stderr, "%s: could not launch network script\n", setup_script);
3635281d757SMark McLoughlin     return -1;
3645281d757SMark McLoughlin }
3655281d757SMark McLoughlin 
3665281d757SMark McLoughlin static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
3675281d757SMark McLoughlin {
3685281d757SMark McLoughlin     int fd, vnet_hdr_required;
3695281d757SMark McLoughlin     char ifname[128] = {0,};
3705281d757SMark McLoughlin     const char *setup_script;
3715281d757SMark McLoughlin 
3725281d757SMark McLoughlin     if (qemu_opt_get(opts, "ifname")) {
3735281d757SMark McLoughlin         pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
3745281d757SMark McLoughlin     }
3755281d757SMark McLoughlin 
3765281d757SMark McLoughlin     *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
3775281d757SMark McLoughlin     if (qemu_opt_get(opts, "vnet_hdr")) {
3785281d757SMark McLoughlin         vnet_hdr_required = *vnet_hdr;
3795281d757SMark McLoughlin     } else {
3805281d757SMark McLoughlin         vnet_hdr_required = 0;
3815281d757SMark McLoughlin     }
3825281d757SMark McLoughlin 
3835281d757SMark McLoughlin     TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
3845281d757SMark McLoughlin     if (fd < 0) {
3855281d757SMark McLoughlin         return -1;
3865281d757SMark McLoughlin     }
3875281d757SMark McLoughlin 
3885281d757SMark McLoughlin     setup_script = qemu_opt_get(opts, "script");
3895281d757SMark McLoughlin     if (setup_script &&
3905281d757SMark McLoughlin         setup_script[0] != '\0' &&
3915281d757SMark McLoughlin         strcmp(setup_script, "no") != 0 &&
3925281d757SMark McLoughlin         launch_script(setup_script, ifname, fd)) {
3935281d757SMark McLoughlin         close(fd);
3945281d757SMark McLoughlin         return -1;
3955281d757SMark McLoughlin     }
3965281d757SMark McLoughlin 
3975281d757SMark McLoughlin     qemu_opt_set(opts, "ifname", ifname);
3985281d757SMark McLoughlin 
3995281d757SMark McLoughlin     return fd;
4005281d757SMark McLoughlin }
4015281d757SMark McLoughlin 
4025281d757SMark McLoughlin int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
4035281d757SMark McLoughlin {
4045281d757SMark McLoughlin     TAPState *s;
405df6c2a0fSMark McLoughlin     int fd, vnet_hdr = 0;
4065281d757SMark McLoughlin 
4075281d757SMark McLoughlin     if (qemu_opt_get(opts, "fd")) {
4085281d757SMark McLoughlin         if (qemu_opt_get(opts, "ifname") ||
4095281d757SMark McLoughlin             qemu_opt_get(opts, "script") ||
4105281d757SMark McLoughlin             qemu_opt_get(opts, "downscript") ||
4115281d757SMark McLoughlin             qemu_opt_get(opts, "vnet_hdr")) {
4121ecda02bSMarkus Armbruster             error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
4135281d757SMark McLoughlin             return -1;
4145281d757SMark McLoughlin         }
4155281d757SMark McLoughlin 
4165281d757SMark McLoughlin         fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
4175281d757SMark McLoughlin         if (fd == -1) {
4185281d757SMark McLoughlin             return -1;
4195281d757SMark McLoughlin         }
4205281d757SMark McLoughlin 
4215281d757SMark McLoughlin         fcntl(fd, F_SETFL, O_NONBLOCK);
4225281d757SMark McLoughlin 
4235281d757SMark McLoughlin         vnet_hdr = tap_probe_vnet_hdr(fd);
4245281d757SMark McLoughlin     } else {
4255281d757SMark McLoughlin         if (!qemu_opt_get(opts, "script")) {
4265281d757SMark McLoughlin             qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
4275281d757SMark McLoughlin         }
4285281d757SMark McLoughlin 
4295281d757SMark McLoughlin         if (!qemu_opt_get(opts, "downscript")) {
4305281d757SMark McLoughlin             qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
4315281d757SMark McLoughlin         }
4325281d757SMark McLoughlin 
4335281d757SMark McLoughlin         fd = net_tap_init(opts, &vnet_hdr);
434929fe497SJuergen Lock         if (fd == -1) {
435929fe497SJuergen Lock             return -1;
436929fe497SJuergen Lock         }
4375281d757SMark McLoughlin     }
4385281d757SMark McLoughlin 
4395281d757SMark McLoughlin     s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
4405281d757SMark McLoughlin     if (!s) {
4415281d757SMark McLoughlin         close(fd);
4425281d757SMark McLoughlin         return -1;
4435281d757SMark McLoughlin     }
4445281d757SMark McLoughlin 
44515ac913bSMark McLoughlin     if (tap_set_sndbuf(s->fd, opts) < 0) {
4465281d757SMark McLoughlin         return -1;
4475281d757SMark McLoughlin     }
4485281d757SMark McLoughlin 
4495281d757SMark McLoughlin     if (qemu_opt_get(opts, "fd")) {
4503e35ba93SMark McLoughlin         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
4515281d757SMark McLoughlin     } else {
4525281d757SMark McLoughlin         const char *ifname, *script, *downscript;
4535281d757SMark McLoughlin 
4545281d757SMark McLoughlin         ifname     = qemu_opt_get(opts, "ifname");
4555281d757SMark McLoughlin         script     = qemu_opt_get(opts, "script");
4565281d757SMark McLoughlin         downscript = qemu_opt_get(opts, "downscript");
4575281d757SMark McLoughlin 
4583e35ba93SMark McLoughlin         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
4595281d757SMark McLoughlin                  "ifname=%s,script=%s,downscript=%s",
4605281d757SMark McLoughlin                  ifname, script, downscript);
4615281d757SMark McLoughlin 
4625281d757SMark McLoughlin         if (strcmp(downscript, "no") != 0) {
4635281d757SMark McLoughlin             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
4645281d757SMark McLoughlin             snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
4655281d757SMark McLoughlin         }
4665281d757SMark McLoughlin     }
4675281d757SMark McLoughlin 
468*82b0d80eSMichael S. Tsirkin     if (qemu_opt_get_bool(opts, "vhost", !!qemu_opt_get(opts, "vhostfd"))) {
469*82b0d80eSMichael S. Tsirkin         int vhostfd, r;
470*82b0d80eSMichael S. Tsirkin         if (qemu_opt_get(opts, "vhostfd")) {
471*82b0d80eSMichael S. Tsirkin             r = net_handle_fd_param(mon, qemu_opt_get(opts, "vhostfd"));
472*82b0d80eSMichael S. Tsirkin             if (r == -1) {
473*82b0d80eSMichael S. Tsirkin                 return -1;
474*82b0d80eSMichael S. Tsirkin             }
475*82b0d80eSMichael S. Tsirkin             vhostfd = r;
476*82b0d80eSMichael S. Tsirkin         } else {
477*82b0d80eSMichael S. Tsirkin             vhostfd = -1;
478*82b0d80eSMichael S. Tsirkin         }
479*82b0d80eSMichael S. Tsirkin         s->vhost_net = vhost_net_init(&s->nc, vhostfd);
480*82b0d80eSMichael S. Tsirkin         if (!s->vhost_net) {
481*82b0d80eSMichael S. Tsirkin             error_report("vhost-net requested but could not be initialized");
482*82b0d80eSMichael S. Tsirkin             return -1;
483*82b0d80eSMichael S. Tsirkin         }
484*82b0d80eSMichael S. Tsirkin     } else if (qemu_opt_get(opts, "vhostfd")) {
485*82b0d80eSMichael S. Tsirkin         error_report("vhostfd= is not valid without vhost");
486*82b0d80eSMichael S. Tsirkin         return -1;
487*82b0d80eSMichael S. Tsirkin     }
488*82b0d80eSMichael S. Tsirkin 
4895281d757SMark McLoughlin     return 0;
4905281d757SMark McLoughlin }
491