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 26a245fc18SPaolo Bonzini #include "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" 37a245fc18SPaolo Bonzini #include "clients.h" 3837003adfSPaolo Bonzini #include "monitor.h" 395281d757SMark McLoughlin #include "sysemu.h" 405281d757SMark McLoughlin #include "qemu-char.h" 415281d757SMark McLoughlin #include "qemu-common.h" 422f792016SMarkus Armbruster #include "qemu-error.h" 435281d757SMark McLoughlin 445281d757SMark McLoughlin #include "net/tap-linux.h" 455281d757SMark McLoughlin 4682b0d80eSMichael S. Tsirkin #include "hw/vhost_net.h" 4782b0d80eSMichael S. Tsirkin 485281d757SMark McLoughlin /* Maximum GSO packet size (64k) plus plenty of room for 495281d757SMark McLoughlin * the ethernet and virtio_net headers 505281d757SMark McLoughlin */ 515281d757SMark McLoughlin #define TAP_BUFSIZE (4096 + 65536) 525281d757SMark McLoughlin 535281d757SMark McLoughlin typedef struct TAPState { 544e68f7a0SStefan Hajnoczi NetClientState nc; 555281d757SMark McLoughlin int fd; 565281d757SMark McLoughlin char down_script[1024]; 575281d757SMark McLoughlin char down_script_arg[128]; 585281d757SMark McLoughlin uint8_t buf[TAP_BUFSIZE]; 595281d757SMark McLoughlin unsigned int read_poll : 1; 605281d757SMark McLoughlin unsigned int write_poll : 1; 615281d757SMark McLoughlin unsigned int using_vnet_hdr : 1; 625281d757SMark McLoughlin unsigned int has_ufo: 1; 6382b0d80eSMichael S. Tsirkin VHostNetState *vhost_net; 64ef4252b1SMichael S. Tsirkin unsigned host_vnet_hdr_len; 655281d757SMark McLoughlin } TAPState; 665281d757SMark McLoughlin 675281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd); 685281d757SMark McLoughlin 695281d757SMark McLoughlin static int tap_can_send(void *opaque); 705281d757SMark McLoughlin static void tap_send(void *opaque); 715281d757SMark McLoughlin static void tap_writable(void *opaque); 725281d757SMark McLoughlin 735281d757SMark McLoughlin static void tap_update_fd_handler(TAPState *s) 745281d757SMark McLoughlin { 755281d757SMark McLoughlin qemu_set_fd_handler2(s->fd, 765281d757SMark McLoughlin s->read_poll ? tap_can_send : NULL, 775281d757SMark McLoughlin s->read_poll ? tap_send : NULL, 785281d757SMark McLoughlin s->write_poll ? tap_writable : NULL, 795281d757SMark McLoughlin s); 805281d757SMark McLoughlin } 815281d757SMark McLoughlin 825281d757SMark McLoughlin static void tap_read_poll(TAPState *s, int enable) 835281d757SMark McLoughlin { 845281d757SMark McLoughlin s->read_poll = !!enable; 855281d757SMark McLoughlin tap_update_fd_handler(s); 865281d757SMark McLoughlin } 875281d757SMark McLoughlin 885281d757SMark McLoughlin static void tap_write_poll(TAPState *s, int enable) 895281d757SMark McLoughlin { 905281d757SMark McLoughlin s->write_poll = !!enable; 915281d757SMark McLoughlin tap_update_fd_handler(s); 925281d757SMark McLoughlin } 935281d757SMark McLoughlin 945281d757SMark McLoughlin static void tap_writable(void *opaque) 955281d757SMark McLoughlin { 965281d757SMark McLoughlin TAPState *s = opaque; 975281d757SMark McLoughlin 985281d757SMark McLoughlin tap_write_poll(s, 0); 995281d757SMark McLoughlin 1003e35ba93SMark McLoughlin qemu_flush_queued_packets(&s->nc); 1015281d757SMark McLoughlin } 1025281d757SMark McLoughlin 1035281d757SMark McLoughlin static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) 1045281d757SMark McLoughlin { 1055281d757SMark McLoughlin ssize_t len; 1065281d757SMark McLoughlin 1075281d757SMark McLoughlin do { 1085281d757SMark McLoughlin len = writev(s->fd, iov, iovcnt); 1095281d757SMark McLoughlin } while (len == -1 && errno == EINTR); 1105281d757SMark McLoughlin 1115281d757SMark McLoughlin if (len == -1 && errno == EAGAIN) { 1125281d757SMark McLoughlin tap_write_poll(s, 1); 1135281d757SMark McLoughlin return 0; 1145281d757SMark McLoughlin } 1155281d757SMark McLoughlin 1165281d757SMark McLoughlin return len; 1175281d757SMark McLoughlin } 1185281d757SMark McLoughlin 1194e68f7a0SStefan Hajnoczi static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov, 1205281d757SMark McLoughlin int iovcnt) 1215281d757SMark McLoughlin { 1223e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 1235281d757SMark McLoughlin const struct iovec *iovp = iov; 1245281d757SMark McLoughlin struct iovec iov_copy[iovcnt + 1]; 125ef4252b1SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr = { }; 1265281d757SMark McLoughlin 127ef4252b1SMichael S. Tsirkin if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 1285281d757SMark McLoughlin iov_copy[0].iov_base = &hdr; 129ef4252b1SMichael S. Tsirkin iov_copy[0].iov_len = s->host_vnet_hdr_len; 1305281d757SMark McLoughlin memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); 1315281d757SMark McLoughlin iovp = iov_copy; 1325281d757SMark McLoughlin iovcnt++; 1335281d757SMark McLoughlin } 1345281d757SMark McLoughlin 1355281d757SMark McLoughlin return tap_write_packet(s, iovp, iovcnt); 1365281d757SMark McLoughlin } 1375281d757SMark McLoughlin 1384e68f7a0SStefan Hajnoczi static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size) 1395281d757SMark McLoughlin { 1403e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 1415281d757SMark McLoughlin struct iovec iov[2]; 1425281d757SMark McLoughlin int iovcnt = 0; 143ef4252b1SMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf hdr = { }; 1445281d757SMark McLoughlin 145ef4252b1SMichael S. Tsirkin if (s->host_vnet_hdr_len) { 1465281d757SMark McLoughlin iov[iovcnt].iov_base = &hdr; 147ef4252b1SMichael S. Tsirkin iov[iovcnt].iov_len = s->host_vnet_hdr_len; 1485281d757SMark McLoughlin iovcnt++; 1495281d757SMark McLoughlin } 1505281d757SMark McLoughlin 1515281d757SMark McLoughlin iov[iovcnt].iov_base = (char *)buf; 1525281d757SMark McLoughlin iov[iovcnt].iov_len = size; 1535281d757SMark McLoughlin iovcnt++; 1545281d757SMark McLoughlin 1555281d757SMark McLoughlin return tap_write_packet(s, iov, iovcnt); 1565281d757SMark McLoughlin } 1575281d757SMark McLoughlin 1584e68f7a0SStefan Hajnoczi static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) 1595281d757SMark McLoughlin { 1603e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 1615281d757SMark McLoughlin struct iovec iov[1]; 1625281d757SMark McLoughlin 163ef4252b1SMichael S. Tsirkin if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 1643e35ba93SMark McLoughlin return tap_receive_raw(nc, buf, size); 1655281d757SMark McLoughlin } 1665281d757SMark McLoughlin 1675281d757SMark McLoughlin iov[0].iov_base = (char *)buf; 1685281d757SMark McLoughlin iov[0].iov_len = size; 1695281d757SMark McLoughlin 1705281d757SMark McLoughlin return tap_write_packet(s, iov, 1); 1715281d757SMark McLoughlin } 1725281d757SMark McLoughlin 1735281d757SMark McLoughlin static int tap_can_send(void *opaque) 1745281d757SMark McLoughlin { 1755281d757SMark McLoughlin TAPState *s = opaque; 1765281d757SMark McLoughlin 1773e35ba93SMark McLoughlin return qemu_can_send_packet(&s->nc); 1785281d757SMark McLoughlin } 1795281d757SMark McLoughlin 180966ea5ecSMark McLoughlin #ifndef __sun__ 181966ea5ecSMark McLoughlin ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) 1825281d757SMark McLoughlin { 1835281d757SMark McLoughlin return read(tapfd, buf, maxlen); 1845281d757SMark McLoughlin } 1855281d757SMark McLoughlin #endif 1865281d757SMark McLoughlin 1874e68f7a0SStefan Hajnoczi static void tap_send_completed(NetClientState *nc, ssize_t len) 1885281d757SMark McLoughlin { 1893e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 1905281d757SMark McLoughlin tap_read_poll(s, 1); 1915281d757SMark McLoughlin } 1925281d757SMark McLoughlin 1935281d757SMark McLoughlin static void tap_send(void *opaque) 1945281d757SMark McLoughlin { 1955281d757SMark McLoughlin TAPState *s = opaque; 196be1636b3SMark McLoughlin int size; 1975281d757SMark McLoughlin 1985819c918SMark McLoughlin do { 1995819c918SMark McLoughlin uint8_t *buf = s->buf; 2005819c918SMark McLoughlin 2015281d757SMark McLoughlin size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); 2025281d757SMark McLoughlin if (size <= 0) { 2035819c918SMark McLoughlin break; 2045281d757SMark McLoughlin } 2055281d757SMark McLoughlin 206ef4252b1SMichael S. Tsirkin if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { 207ef4252b1SMichael S. Tsirkin buf += s->host_vnet_hdr_len; 208ef4252b1SMichael S. Tsirkin size -= s->host_vnet_hdr_len; 2095281d757SMark McLoughlin } 2105281d757SMark McLoughlin 2113e35ba93SMark McLoughlin size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); 2125281d757SMark McLoughlin if (size == 0) { 2135281d757SMark McLoughlin tap_read_poll(s, 0); 2145281d757SMark McLoughlin } 2153e35ba93SMark McLoughlin } while (size > 0 && qemu_can_send_packet(&s->nc)); 2165281d757SMark McLoughlin } 2175281d757SMark McLoughlin 2184e68f7a0SStefan Hajnoczi int tap_has_ufo(NetClientState *nc) 2195281d757SMark McLoughlin { 2203e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 2215281d757SMark McLoughlin 2222be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 2235281d757SMark McLoughlin 2245281d757SMark McLoughlin return s->has_ufo; 2255281d757SMark McLoughlin } 2265281d757SMark McLoughlin 2274e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr(NetClientState *nc) 2285281d757SMark McLoughlin { 2293e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 2305281d757SMark McLoughlin 2312be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 2325281d757SMark McLoughlin 233ef4252b1SMichael S. Tsirkin return !!s->host_vnet_hdr_len; 2345281d757SMark McLoughlin } 2355281d757SMark McLoughlin 2364e68f7a0SStefan Hajnoczi int tap_has_vnet_hdr_len(NetClientState *nc, int len) 237445d892fSMichael S. Tsirkin { 238445d892fSMichael S. Tsirkin TAPState *s = DO_UPCAST(TAPState, nc, nc); 239445d892fSMichael S. Tsirkin 2402be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 241445d892fSMichael S. Tsirkin 242445d892fSMichael S. Tsirkin return tap_probe_vnet_hdr_len(s->fd, len); 243445d892fSMichael S. Tsirkin } 244445d892fSMichael S. Tsirkin 2454e68f7a0SStefan Hajnoczi void tap_set_vnet_hdr_len(NetClientState *nc, int len) 246445d892fSMichael S. Tsirkin { 247445d892fSMichael S. Tsirkin TAPState *s = DO_UPCAST(TAPState, nc, nc); 248445d892fSMichael S. Tsirkin 2492be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 250445d892fSMichael S. Tsirkin assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || 251445d892fSMichael S. Tsirkin len == sizeof(struct virtio_net_hdr)); 252445d892fSMichael S. Tsirkin 253445d892fSMichael S. Tsirkin tap_fd_set_vnet_hdr_len(s->fd, len); 254445d892fSMichael S. Tsirkin s->host_vnet_hdr_len = len; 255445d892fSMichael S. Tsirkin } 256445d892fSMichael S. Tsirkin 2574e68f7a0SStefan Hajnoczi void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr) 2585281d757SMark McLoughlin { 2593e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 2605281d757SMark McLoughlin 2615281d757SMark McLoughlin using_vnet_hdr = using_vnet_hdr != 0; 2625281d757SMark McLoughlin 2632be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 264ef4252b1SMichael S. Tsirkin assert(!!s->host_vnet_hdr_len == using_vnet_hdr); 2655281d757SMark McLoughlin 2665281d757SMark McLoughlin s->using_vnet_hdr = using_vnet_hdr; 2675281d757SMark McLoughlin } 2685281d757SMark McLoughlin 2694e68f7a0SStefan Hajnoczi void tap_set_offload(NetClientState *nc, int csum, int tso4, 2705281d757SMark McLoughlin int tso6, int ecn, int ufo) 2715281d757SMark McLoughlin { 2723e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 27327a6375dSMichael S. Tsirkin if (s->fd < 0) { 27427a6375dSMichael S. Tsirkin return; 27527a6375dSMichael S. Tsirkin } 2765281d757SMark McLoughlin 27727a6375dSMichael S. Tsirkin tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); 2785281d757SMark McLoughlin } 2795281d757SMark McLoughlin 2804e68f7a0SStefan Hajnoczi static void tap_cleanup(NetClientState *nc) 2815281d757SMark McLoughlin { 2823e35ba93SMark McLoughlin TAPState *s = DO_UPCAST(TAPState, nc, nc); 2835281d757SMark McLoughlin 28482b0d80eSMichael S. Tsirkin if (s->vhost_net) { 28582b0d80eSMichael S. Tsirkin vhost_net_cleanup(s->vhost_net); 28643849424SMichael S. Tsirkin s->vhost_net = NULL; 28782b0d80eSMichael S. Tsirkin } 28882b0d80eSMichael S. Tsirkin 2893e35ba93SMark McLoughlin qemu_purge_queued_packets(nc); 2905281d757SMark McLoughlin 2915281d757SMark McLoughlin if (s->down_script[0]) 2925281d757SMark McLoughlin launch_script(s->down_script, s->down_script_arg, s->fd); 2935281d757SMark McLoughlin 2945281d757SMark McLoughlin tap_read_poll(s, 0); 2955281d757SMark McLoughlin tap_write_poll(s, 0); 2965281d757SMark McLoughlin close(s->fd); 29727a6375dSMichael S. Tsirkin s->fd = -1; 2985281d757SMark McLoughlin } 2995281d757SMark McLoughlin 3004e68f7a0SStefan Hajnoczi static void tap_poll(NetClientState *nc, bool enable) 301ceb69615SMichael S. Tsirkin { 302ceb69615SMichael S. Tsirkin TAPState *s = DO_UPCAST(TAPState, nc, nc); 303ceb69615SMichael S. Tsirkin tap_read_poll(s, enable); 304ceb69615SMichael S. Tsirkin tap_write_poll(s, enable); 305ceb69615SMichael S. Tsirkin } 306ceb69615SMichael S. Tsirkin 3074e68f7a0SStefan Hajnoczi int tap_get_fd(NetClientState *nc) 30895d528a2SMichael S. Tsirkin { 30995d528a2SMichael S. Tsirkin TAPState *s = DO_UPCAST(TAPState, nc, nc); 3102be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 31195d528a2SMichael S. Tsirkin return s->fd; 31295d528a2SMichael S. Tsirkin } 31395d528a2SMichael S. Tsirkin 3145281d757SMark McLoughlin /* fd support */ 3155281d757SMark McLoughlin 3163e35ba93SMark McLoughlin static NetClientInfo net_tap_info = { 3172be64a68SLaszlo Ersek .type = NET_CLIENT_OPTIONS_KIND_TAP, 3183e35ba93SMark McLoughlin .size = sizeof(TAPState), 3193e35ba93SMark McLoughlin .receive = tap_receive, 3203e35ba93SMark McLoughlin .receive_raw = tap_receive_raw, 3213e35ba93SMark McLoughlin .receive_iov = tap_receive_iov, 322ceb69615SMichael S. Tsirkin .poll = tap_poll, 3233e35ba93SMark McLoughlin .cleanup = tap_cleanup, 3243e35ba93SMark McLoughlin }; 3253e35ba93SMark McLoughlin 3264e68f7a0SStefan Hajnoczi static TAPState *net_tap_fd_init(NetClientState *peer, 3275281d757SMark McLoughlin const char *model, 3285281d757SMark McLoughlin const char *name, 3295281d757SMark McLoughlin int fd, 3305281d757SMark McLoughlin int vnet_hdr) 3315281d757SMark McLoughlin { 3324e68f7a0SStefan Hajnoczi NetClientState *nc; 3335281d757SMark McLoughlin TAPState *s; 3345281d757SMark McLoughlin 335ab5f3f84SStefan Hajnoczi nc = qemu_new_net_client(&net_tap_info, peer, model, name); 3363e35ba93SMark McLoughlin 3373e35ba93SMark McLoughlin s = DO_UPCAST(TAPState, nc, nc); 3383e35ba93SMark McLoughlin 3395281d757SMark McLoughlin s->fd = fd; 340ef4252b1SMichael S. Tsirkin s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; 3415281d757SMark McLoughlin s->using_vnet_hdr = 0; 3429c282718SMark McLoughlin s->has_ufo = tap_probe_has_ufo(s->fd); 3433e35ba93SMark McLoughlin tap_set_offload(&s->nc, 0, 0, 0, 0, 0); 344*58ddcd50SMichael S. Tsirkin /* 345*58ddcd50SMichael S. Tsirkin * Make sure host header length is set correctly in tap: 346*58ddcd50SMichael S. Tsirkin * it might have been modified by another instance of qemu. 347*58ddcd50SMichael S. Tsirkin */ 348*58ddcd50SMichael S. Tsirkin if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) { 349*58ddcd50SMichael S. Tsirkin tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len); 350*58ddcd50SMichael S. Tsirkin } 3515281d757SMark McLoughlin tap_read_poll(s, 1); 35282b0d80eSMichael S. Tsirkin s->vhost_net = NULL; 3535281d757SMark McLoughlin return s; 3545281d757SMark McLoughlin } 3555281d757SMark McLoughlin 3565281d757SMark McLoughlin static int launch_script(const char *setup_script, const char *ifname, int fd) 3575281d757SMark McLoughlin { 3585281d757SMark McLoughlin int pid, status; 3595281d757SMark McLoughlin char *args[3]; 3605281d757SMark McLoughlin char **parg; 3615281d757SMark McLoughlin 3625281d757SMark McLoughlin /* try to launch network script */ 3635281d757SMark McLoughlin pid = fork(); 3645281d757SMark McLoughlin if (pid == 0) { 3655281d757SMark McLoughlin int open_max = sysconf(_SC_OPEN_MAX), i; 3665281d757SMark McLoughlin 3675281d757SMark McLoughlin for (i = 0; i < open_max; i++) { 3685281d757SMark McLoughlin if (i != STDIN_FILENO && 3695281d757SMark McLoughlin i != STDOUT_FILENO && 3705281d757SMark McLoughlin i != STDERR_FILENO && 3715281d757SMark McLoughlin i != fd) { 3725281d757SMark McLoughlin close(i); 3735281d757SMark McLoughlin } 3745281d757SMark McLoughlin } 3755281d757SMark McLoughlin parg = args; 3765281d757SMark McLoughlin *parg++ = (char *)setup_script; 3775281d757SMark McLoughlin *parg++ = (char *)ifname; 3789678d950SBlue Swirl *parg = NULL; 3795281d757SMark McLoughlin execv(setup_script, args); 3805281d757SMark McLoughlin _exit(1); 3815281d757SMark McLoughlin } else if (pid > 0) { 3825281d757SMark McLoughlin while (waitpid(pid, &status, 0) != pid) { 3835281d757SMark McLoughlin /* loop */ 3845281d757SMark McLoughlin } 3855281d757SMark McLoughlin 3865281d757SMark McLoughlin if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 3875281d757SMark McLoughlin return 0; 3885281d757SMark McLoughlin } 3895281d757SMark McLoughlin } 3905281d757SMark McLoughlin fprintf(stderr, "%s: could not launch network script\n", setup_script); 3915281d757SMark McLoughlin return -1; 3925281d757SMark McLoughlin } 3935281d757SMark McLoughlin 394a7c36ee4SCorey Bryant static int recv_fd(int c) 395a7c36ee4SCorey Bryant { 396a7c36ee4SCorey Bryant int fd; 397a7c36ee4SCorey Bryant uint8_t msgbuf[CMSG_SPACE(sizeof(fd))]; 398a7c36ee4SCorey Bryant struct msghdr msg = { 399a7c36ee4SCorey Bryant .msg_control = msgbuf, 400a7c36ee4SCorey Bryant .msg_controllen = sizeof(msgbuf), 401a7c36ee4SCorey Bryant }; 402a7c36ee4SCorey Bryant struct cmsghdr *cmsg; 403a7c36ee4SCorey Bryant struct iovec iov; 404a7c36ee4SCorey Bryant uint8_t req[1]; 405a7c36ee4SCorey Bryant ssize_t len; 406a7c36ee4SCorey Bryant 407a7c36ee4SCorey Bryant cmsg = CMSG_FIRSTHDR(&msg); 408a7c36ee4SCorey Bryant cmsg->cmsg_level = SOL_SOCKET; 409a7c36ee4SCorey Bryant cmsg->cmsg_type = SCM_RIGHTS; 410a7c36ee4SCorey Bryant cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); 411a7c36ee4SCorey Bryant msg.msg_controllen = cmsg->cmsg_len; 412a7c36ee4SCorey Bryant 413a7c36ee4SCorey Bryant iov.iov_base = req; 414a7c36ee4SCorey Bryant iov.iov_len = sizeof(req); 415a7c36ee4SCorey Bryant 416a7c36ee4SCorey Bryant msg.msg_iov = &iov; 417a7c36ee4SCorey Bryant msg.msg_iovlen = 1; 418a7c36ee4SCorey Bryant 419a7c36ee4SCorey Bryant len = recvmsg(c, &msg, 0); 420a7c36ee4SCorey Bryant if (len > 0) { 421a7c36ee4SCorey Bryant memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); 422a7c36ee4SCorey Bryant return fd; 423a7c36ee4SCorey Bryant } 424a7c36ee4SCorey Bryant 425a7c36ee4SCorey Bryant return len; 426a7c36ee4SCorey Bryant } 427a7c36ee4SCorey Bryant 428a7c36ee4SCorey Bryant static int net_bridge_run_helper(const char *helper, const char *bridge) 429a7c36ee4SCorey Bryant { 430a7c36ee4SCorey Bryant sigset_t oldmask, mask; 431a7c36ee4SCorey Bryant int pid, status; 432a7c36ee4SCorey Bryant char *args[5]; 433a7c36ee4SCorey Bryant char **parg; 434a7c36ee4SCorey Bryant int sv[2]; 435a7c36ee4SCorey Bryant 436a7c36ee4SCorey Bryant sigemptyset(&mask); 437a7c36ee4SCorey Bryant sigaddset(&mask, SIGCHLD); 438a7c36ee4SCorey Bryant sigprocmask(SIG_BLOCK, &mask, &oldmask); 439a7c36ee4SCorey Bryant 440a7c36ee4SCorey Bryant if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { 441a7c36ee4SCorey Bryant return -1; 442a7c36ee4SCorey Bryant } 443a7c36ee4SCorey Bryant 444a7c36ee4SCorey Bryant /* try to launch bridge helper */ 445a7c36ee4SCorey Bryant pid = fork(); 446a7c36ee4SCorey Bryant if (pid == 0) { 447a7c36ee4SCorey Bryant int open_max = sysconf(_SC_OPEN_MAX), i; 448a7c36ee4SCorey Bryant char fd_buf[6+10]; 449a7c36ee4SCorey Bryant char br_buf[6+IFNAMSIZ] = {0}; 450a7c36ee4SCorey Bryant char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15]; 451a7c36ee4SCorey Bryant 452a7c36ee4SCorey Bryant for (i = 0; i < open_max; i++) { 453a7c36ee4SCorey Bryant if (i != STDIN_FILENO && 454a7c36ee4SCorey Bryant i != STDOUT_FILENO && 455a7c36ee4SCorey Bryant i != STDERR_FILENO && 456a7c36ee4SCorey Bryant i != sv[1]) { 457a7c36ee4SCorey Bryant close(i); 458a7c36ee4SCorey Bryant } 459a7c36ee4SCorey Bryant } 460a7c36ee4SCorey Bryant 461a7c36ee4SCorey Bryant snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]); 462a7c36ee4SCorey Bryant 463a7c36ee4SCorey Bryant if (strrchr(helper, ' ') || strrchr(helper, '\t')) { 464a7c36ee4SCorey Bryant /* assume helper is a command */ 465a7c36ee4SCorey Bryant 466a7c36ee4SCorey Bryant if (strstr(helper, "--br=") == NULL) { 467a7c36ee4SCorey Bryant snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); 468a7c36ee4SCorey Bryant } 469a7c36ee4SCorey Bryant 470a7c36ee4SCorey Bryant snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s", 471a7c36ee4SCorey Bryant helper, "--use-vnet", fd_buf, br_buf); 472a7c36ee4SCorey Bryant 473a7c36ee4SCorey Bryant parg = args; 474a7c36ee4SCorey Bryant *parg++ = (char *)"sh"; 475a7c36ee4SCorey Bryant *parg++ = (char *)"-c"; 476a7c36ee4SCorey Bryant *parg++ = helper_cmd; 477a7c36ee4SCorey Bryant *parg++ = NULL; 478a7c36ee4SCorey Bryant 479a7c36ee4SCorey Bryant execv("/bin/sh", args); 480a7c36ee4SCorey Bryant } else { 481a7c36ee4SCorey Bryant /* assume helper is just the executable path name */ 482a7c36ee4SCorey Bryant 483a7c36ee4SCorey Bryant snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); 484a7c36ee4SCorey Bryant 485a7c36ee4SCorey Bryant parg = args; 486a7c36ee4SCorey Bryant *parg++ = (char *)helper; 487a7c36ee4SCorey Bryant *parg++ = (char *)"--use-vnet"; 488a7c36ee4SCorey Bryant *parg++ = fd_buf; 489a7c36ee4SCorey Bryant *parg++ = br_buf; 490a7c36ee4SCorey Bryant *parg++ = NULL; 491a7c36ee4SCorey Bryant 492a7c36ee4SCorey Bryant execv(helper, args); 493a7c36ee4SCorey Bryant } 494a7c36ee4SCorey Bryant _exit(1); 495a7c36ee4SCorey Bryant 496a7c36ee4SCorey Bryant } else if (pid > 0) { 497a7c36ee4SCorey Bryant int fd; 498a7c36ee4SCorey Bryant 499a7c36ee4SCorey Bryant close(sv[1]); 500a7c36ee4SCorey Bryant 501a7c36ee4SCorey Bryant do { 502a7c36ee4SCorey Bryant fd = recv_fd(sv[0]); 503a7c36ee4SCorey Bryant } while (fd == -1 && errno == EINTR); 504a7c36ee4SCorey Bryant 505a7c36ee4SCorey Bryant close(sv[0]); 506a7c36ee4SCorey Bryant 507a7c36ee4SCorey Bryant while (waitpid(pid, &status, 0) != pid) { 508a7c36ee4SCorey Bryant /* loop */ 509a7c36ee4SCorey Bryant } 510a7c36ee4SCorey Bryant sigprocmask(SIG_SETMASK, &oldmask, NULL); 511a7c36ee4SCorey Bryant if (fd < 0) { 512a7c36ee4SCorey Bryant fprintf(stderr, "failed to recv file descriptor\n"); 513a7c36ee4SCorey Bryant return -1; 514a7c36ee4SCorey Bryant } 515a7c36ee4SCorey Bryant 516a7c36ee4SCorey Bryant if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 517a7c36ee4SCorey Bryant return fd; 518a7c36ee4SCorey Bryant } 519a7c36ee4SCorey Bryant } 520a7c36ee4SCorey Bryant fprintf(stderr, "failed to launch bridge helper\n"); 521a7c36ee4SCorey Bryant return -1; 522a7c36ee4SCorey Bryant } 523a7c36ee4SCorey Bryant 5241a0c0958SLaszlo Ersek int net_init_bridge(const NetClientOptions *opts, const char *name, 5254e68f7a0SStefan Hajnoczi NetClientState *peer) 526a7c36ee4SCorey Bryant { 527f79b51b0SLaszlo Ersek const NetdevBridgeOptions *bridge; 528f79b51b0SLaszlo Ersek const char *helper, *br; 529f79b51b0SLaszlo Ersek 530a7c36ee4SCorey Bryant TAPState *s; 531a7c36ee4SCorey Bryant int fd, vnet_hdr; 532a7c36ee4SCorey Bryant 533f79b51b0SLaszlo Ersek assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE); 534f79b51b0SLaszlo Ersek bridge = opts->bridge; 535a7c36ee4SCorey Bryant 536f79b51b0SLaszlo Ersek helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER; 537f79b51b0SLaszlo Ersek br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE; 538f79b51b0SLaszlo Ersek 539f79b51b0SLaszlo Ersek fd = net_bridge_run_helper(helper, br); 540a7c36ee4SCorey Bryant if (fd == -1) { 541a7c36ee4SCorey Bryant return -1; 542a7c36ee4SCorey Bryant } 543a7c36ee4SCorey Bryant 544a7c36ee4SCorey Bryant fcntl(fd, F_SETFL, O_NONBLOCK); 545a7c36ee4SCorey Bryant 546a7c36ee4SCorey Bryant vnet_hdr = tap_probe_vnet_hdr(fd); 547a7c36ee4SCorey Bryant 548d33d93b2SStefan Hajnoczi s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); 549a7c36ee4SCorey Bryant if (!s) { 550a7c36ee4SCorey Bryant close(fd); 551a7c36ee4SCorey Bryant return -1; 552a7c36ee4SCorey Bryant } 553a7c36ee4SCorey Bryant 554f79b51b0SLaszlo Ersek snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper, 555f79b51b0SLaszlo Ersek br); 556a7c36ee4SCorey Bryant 557a7c36ee4SCorey Bryant return 0; 558a7c36ee4SCorey Bryant } 559a7c36ee4SCorey Bryant 56008c573a8SLaszlo Ersek static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, 56108c573a8SLaszlo Ersek const char *setup_script, char *ifname, 56208c573a8SLaszlo Ersek size_t ifname_sz) 5635281d757SMark McLoughlin { 5645281d757SMark McLoughlin int fd, vnet_hdr_required; 5655281d757SMark McLoughlin 56608c573a8SLaszlo Ersek if (tap->has_ifname) { 56708c573a8SLaszlo Ersek pstrcpy(ifname, ifname_sz, tap->ifname); 56808c573a8SLaszlo Ersek } else { 56908c573a8SLaszlo Ersek assert(ifname_sz > 0); 57008c573a8SLaszlo Ersek ifname[0] = '\0'; 5715281d757SMark McLoughlin } 5725281d757SMark McLoughlin 57308c573a8SLaszlo Ersek if (tap->has_vnet_hdr) { 57408c573a8SLaszlo Ersek *vnet_hdr = tap->vnet_hdr; 5755281d757SMark McLoughlin vnet_hdr_required = *vnet_hdr; 5765281d757SMark McLoughlin } else { 57708c573a8SLaszlo Ersek *vnet_hdr = 1; 5785281d757SMark McLoughlin vnet_hdr_required = 0; 5795281d757SMark McLoughlin } 5805281d757SMark McLoughlin 58108c573a8SLaszlo Ersek TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required)); 5825281d757SMark McLoughlin if (fd < 0) { 5835281d757SMark McLoughlin return -1; 5845281d757SMark McLoughlin } 5855281d757SMark McLoughlin 5865281d757SMark McLoughlin if (setup_script && 5875281d757SMark McLoughlin setup_script[0] != '\0' && 5885281d757SMark McLoughlin strcmp(setup_script, "no") != 0 && 5895281d757SMark McLoughlin launch_script(setup_script, ifname, fd)) { 5905281d757SMark McLoughlin close(fd); 5915281d757SMark McLoughlin return -1; 5925281d757SMark McLoughlin } 5935281d757SMark McLoughlin 5945281d757SMark McLoughlin return fd; 5955281d757SMark McLoughlin } 5965281d757SMark McLoughlin 5971a0c0958SLaszlo Ersek int net_init_tap(const NetClientOptions *opts, const char *name, 5984e68f7a0SStefan Hajnoczi NetClientState *peer) 5995281d757SMark McLoughlin { 60008c573a8SLaszlo Ersek const NetdevTapOptions *tap; 60108c573a8SLaszlo Ersek 602df6c2a0fSMark McLoughlin int fd, vnet_hdr = 0; 603a7c36ee4SCorey Bryant const char *model; 60408c573a8SLaszlo Ersek TAPState *s; 6055281d757SMark McLoughlin 60608c573a8SLaszlo Ersek /* for the no-fd, no-helper case */ 60708c573a8SLaszlo Ersek const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */ 60808c573a8SLaszlo Ersek char ifname[128]; 60908c573a8SLaszlo Ersek 61008c573a8SLaszlo Ersek assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP); 61108c573a8SLaszlo Ersek tap = opts->tap; 61208c573a8SLaszlo Ersek 61308c573a8SLaszlo Ersek if (tap->has_fd) { 61408c573a8SLaszlo Ersek if (tap->has_ifname || tap->has_script || tap->has_downscript || 61508c573a8SLaszlo Ersek tap->has_vnet_hdr || tap->has_helper) { 616a7c36ee4SCorey Bryant error_report("ifname=, script=, downscript=, vnet_hdr=, " 617a7c36ee4SCorey Bryant "and helper= are invalid with fd="); 6185281d757SMark McLoughlin return -1; 6195281d757SMark McLoughlin } 6205281d757SMark McLoughlin 621a96ed02fSNicholas Bellinger fd = monitor_handle_fd_param(cur_mon, tap->fd); 6225281d757SMark McLoughlin if (fd == -1) { 6235281d757SMark McLoughlin return -1; 6245281d757SMark McLoughlin } 6255281d757SMark McLoughlin 6265281d757SMark McLoughlin fcntl(fd, F_SETFL, O_NONBLOCK); 6275281d757SMark McLoughlin 6285281d757SMark McLoughlin vnet_hdr = tap_probe_vnet_hdr(fd); 629a7c36ee4SCorey Bryant 630a7c36ee4SCorey Bryant model = "tap"; 631a7c36ee4SCorey Bryant 63208c573a8SLaszlo Ersek } else if (tap->has_helper) { 63308c573a8SLaszlo Ersek if (tap->has_ifname || tap->has_script || tap->has_downscript || 63408c573a8SLaszlo Ersek tap->has_vnet_hdr) { 635a7c36ee4SCorey Bryant error_report("ifname=, script=, downscript=, and vnet_hdr= " 636a7c36ee4SCorey Bryant "are invalid with helper="); 637a7c36ee4SCorey Bryant return -1; 638a7c36ee4SCorey Bryant } 639a7c36ee4SCorey Bryant 64008c573a8SLaszlo Ersek fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE); 641a7c36ee4SCorey Bryant if (fd == -1) { 642a7c36ee4SCorey Bryant return -1; 643a7c36ee4SCorey Bryant } 644a7c36ee4SCorey Bryant 645a7c36ee4SCorey Bryant fcntl(fd, F_SETFL, O_NONBLOCK); 646a7c36ee4SCorey Bryant 647a7c36ee4SCorey Bryant vnet_hdr = tap_probe_vnet_hdr(fd); 648a7c36ee4SCorey Bryant 649a7c36ee4SCorey Bryant model = "bridge"; 650a7c36ee4SCorey Bryant 6515281d757SMark McLoughlin } else { 65208c573a8SLaszlo Ersek script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT; 65308c573a8SLaszlo Ersek fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname); 654929fe497SJuergen Lock if (fd == -1) { 655929fe497SJuergen Lock return -1; 656929fe497SJuergen Lock } 657a7c36ee4SCorey Bryant 658a7c36ee4SCorey Bryant model = "tap"; 6595281d757SMark McLoughlin } 6605281d757SMark McLoughlin 661d33d93b2SStefan Hajnoczi s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); 6625281d757SMark McLoughlin if (!s) { 6635281d757SMark McLoughlin close(fd); 6645281d757SMark McLoughlin return -1; 6655281d757SMark McLoughlin } 6665281d757SMark McLoughlin 66708c573a8SLaszlo Ersek if (tap_set_sndbuf(s->fd, tap) < 0) { 6685281d757SMark McLoughlin return -1; 6695281d757SMark McLoughlin } 6705281d757SMark McLoughlin 67108c573a8SLaszlo Ersek if (tap->has_fd) { 6723e35ba93SMark McLoughlin snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); 67308c573a8SLaszlo Ersek } else if (tap->has_helper) { 67408c573a8SLaszlo Ersek snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s", 67508c573a8SLaszlo Ersek tap->helper); 6765281d757SMark McLoughlin } else { 67708c573a8SLaszlo Ersek const char *downscript; 6785281d757SMark McLoughlin 67908c573a8SLaszlo Ersek downscript = tap->has_downscript ? tap->downscript : 68008c573a8SLaszlo Ersek DEFAULT_NETWORK_DOWN_SCRIPT; 6815281d757SMark McLoughlin 6823e35ba93SMark McLoughlin snprintf(s->nc.info_str, sizeof(s->nc.info_str), 68308c573a8SLaszlo Ersek "ifname=%s,script=%s,downscript=%s", ifname, script, 68408c573a8SLaszlo Ersek downscript); 6855281d757SMark McLoughlin 6865281d757SMark McLoughlin if (strcmp(downscript, "no") != 0) { 6875281d757SMark McLoughlin snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); 6885281d757SMark McLoughlin snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); 6895281d757SMark McLoughlin } 6905281d757SMark McLoughlin } 6915281d757SMark McLoughlin 69208c573a8SLaszlo Ersek if (tap->has_vhost ? tap->vhost : 69308c573a8SLaszlo Ersek tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) { 69408c573a8SLaszlo Ersek int vhostfd; 69508c573a8SLaszlo Ersek 69608c573a8SLaszlo Ersek if (tap->has_vhostfd) { 697a96ed02fSNicholas Bellinger vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd); 69808c573a8SLaszlo Ersek if (vhostfd == -1) { 69982b0d80eSMichael S. Tsirkin return -1; 70082b0d80eSMichael S. Tsirkin } 70182b0d80eSMichael S. Tsirkin } else { 70282b0d80eSMichael S. Tsirkin vhostfd = -1; 70382b0d80eSMichael S. Tsirkin } 70408c573a8SLaszlo Ersek 70508c573a8SLaszlo Ersek s->vhost_net = vhost_net_init(&s->nc, vhostfd, 70608c573a8SLaszlo Ersek tap->has_vhostforce && tap->vhostforce); 70782b0d80eSMichael S. Tsirkin if (!s->vhost_net) { 70882b0d80eSMichael S. Tsirkin error_report("vhost-net requested but could not be initialized"); 70982b0d80eSMichael S. Tsirkin return -1; 71082b0d80eSMichael S. Tsirkin } 71108c573a8SLaszlo Ersek } else if (tap->has_vhostfd) { 71282b0d80eSMichael S. Tsirkin error_report("vhostfd= is not valid without vhost"); 71382b0d80eSMichael S. Tsirkin return -1; 71482b0d80eSMichael S. Tsirkin } 71582b0d80eSMichael S. Tsirkin 7165281d757SMark McLoughlin return 0; 7175281d757SMark McLoughlin } 718b202554cSMichael S. Tsirkin 7194e68f7a0SStefan Hajnoczi VHostNetState *tap_get_vhost_net(NetClientState *nc) 720b202554cSMichael S. Tsirkin { 721b202554cSMichael S. Tsirkin TAPState *s = DO_UPCAST(TAPState, nc, nc); 7222be64a68SLaszlo Ersek assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); 723b202554cSMichael S. Tsirkin return s->vhost_net; 724b202554cSMichael S. Tsirkin } 725