Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * Copyright (c) 2009 Red Hat, Inc. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
Peter Maydell | 2744d92 | 2016-01-29 17:50:00 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 27 | #include "tap_int.h" |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 28 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 29 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 31 | #include <sys/wait.h> |
Alexander Graf | 71f4eff | 2009-10-30 22:27:00 +0100 | [diff] [blame] | 32 | #include <sys/socket.h> |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 33 | #include <net/if.h> |
| 34 | |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 35 | #include "net/net.h" |
Paolo Bonzini | a245fc1 | 2012-09-17 18:43:51 +0200 | [diff] [blame] | 36 | #include "clients.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 37 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 38 | #include "sysemu/sysemu.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 39 | #include "qapi/error.h" |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 40 | #include "qemu-common.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 41 | #include "qemu/cutils.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 42 | #include "qemu/error-report.h" |
Brijesh Singh | d542800 | 2018-04-06 13:51:25 -0500 | [diff] [blame] | 43 | #include "qemu/sockets.h" |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 44 | |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 45 | #include "net/tap.h" |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 46 | |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 47 | #include "net/vhost_net.h" |
Michael S. Tsirkin | 82b0d80 | 2010-03-17 13:08:24 +0200 | [diff] [blame] | 48 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 49 | typedef struct TAPState { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 50 | NetClientState nc; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 51 | int fd; |
| 52 | char down_script[1024]; |
| 53 | char down_script_arg[128]; |
Scott Feldman | d32fcad | 2013-03-18 11:43:44 -0700 | [diff] [blame] | 54 | uint8_t buf[NET_BUFSIZE]; |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 55 | bool read_poll; |
| 56 | bool write_poll; |
| 57 | bool using_vnet_hdr; |
| 58 | bool has_ufo; |
Jason Wang | 16dbaf9 | 2013-01-30 19:12:32 +0800 | [diff] [blame] | 59 | bool enabled; |
Michael S. Tsirkin | 82b0d80 | 2010-03-17 13:08:24 +0200 | [diff] [blame] | 60 | VHostNetState *vhost_net; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 61 | unsigned host_vnet_hdr_len; |
Marc-André Lureau | 9e32ff3 | 2016-07-11 16:48:47 +0200 | [diff] [blame] | 62 | Notifier exit; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 63 | } TAPState; |
| 64 | |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 65 | static void launch_script(const char *setup_script, const char *ifname, |
| 66 | int fd, Error **errp); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 67 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 68 | static void tap_send(void *opaque); |
| 69 | static void tap_writable(void *opaque); |
| 70 | |
| 71 | static void tap_update_fd_handler(TAPState *s) |
| 72 | { |
Fam Zheng | 82e1cc4 | 2015-06-04 14:45:18 +0800 | [diff] [blame] | 73 | qemu_set_fd_handler(s->fd, |
| 74 | s->read_poll && s->enabled ? tap_send : NULL, |
| 75 | s->write_poll && s->enabled ? tap_writable : NULL, |
| 76 | s); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 79 | static void tap_read_poll(TAPState *s, bool enable) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 80 | { |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 81 | s->read_poll = enable; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 82 | tap_update_fd_handler(s); |
| 83 | } |
| 84 | |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 85 | static void tap_write_poll(TAPState *s, bool enable) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 86 | { |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 87 | s->write_poll = enable; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 88 | tap_update_fd_handler(s); |
| 89 | } |
| 90 | |
| 91 | static void tap_writable(void *opaque) |
| 92 | { |
| 93 | TAPState *s = opaque; |
| 94 | |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 95 | tap_write_poll(s, false); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 96 | |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 97 | qemu_flush_queued_packets(&s->nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) |
| 101 | { |
| 102 | ssize_t len; |
| 103 | |
| 104 | do { |
| 105 | len = writev(s->fd, iov, iovcnt); |
| 106 | } while (len == -1 && errno == EINTR); |
| 107 | |
| 108 | if (len == -1 && errno == EAGAIN) { |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 109 | tap_write_poll(s, true); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | return len; |
| 114 | } |
| 115 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 116 | static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov, |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 117 | int iovcnt) |
| 118 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 119 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 120 | const struct iovec *iovp = iov; |
| 121 | struct iovec iov_copy[iovcnt + 1]; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 122 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 123 | |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 124 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 125 | iov_copy[0].iov_base = &hdr; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 126 | iov_copy[0].iov_len = s->host_vnet_hdr_len; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 127 | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
| 128 | iovp = iov_copy; |
| 129 | iovcnt++; |
| 130 | } |
| 131 | |
| 132 | return tap_write_packet(s, iovp, iovcnt); |
| 133 | } |
| 134 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 135 | static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 136 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 137 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 138 | struct iovec iov[2]; |
| 139 | int iovcnt = 0; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 140 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 141 | |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 142 | if (s->host_vnet_hdr_len) { |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 143 | iov[iovcnt].iov_base = &hdr; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 144 | iov[iovcnt].iov_len = s->host_vnet_hdr_len; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 145 | iovcnt++; |
| 146 | } |
| 147 | |
| 148 | iov[iovcnt].iov_base = (char *)buf; |
| 149 | iov[iovcnt].iov_len = size; |
| 150 | iovcnt++; |
| 151 | |
| 152 | return tap_write_packet(s, iov, iovcnt); |
| 153 | } |
| 154 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 155 | static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 156 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 157 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 158 | struct iovec iov[1]; |
| 159 | |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 160 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 161 | return tap_receive_raw(nc, buf, size); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | iov[0].iov_base = (char *)buf; |
| 165 | iov[0].iov_len = size; |
| 166 | |
| 167 | return tap_write_packet(s, iov, 1); |
| 168 | } |
| 169 | |
Mark McLoughlin | 966ea5e | 2009-10-22 17:49:09 +0100 | [diff] [blame] | 170 | #ifndef __sun__ |
| 171 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 172 | { |
| 173 | return read(tapfd, buf, maxlen); |
| 174 | } |
| 175 | #endif |
| 176 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 177 | static void tap_send_completed(NetClientState *nc, ssize_t len) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 178 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 179 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 180 | tap_read_poll(s, true); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void tap_send(void *opaque) |
| 184 | { |
| 185 | TAPState *s = opaque; |
| 186 | int size; |
Wangkai (Kevin,C) | 756ae78 | 2014-07-18 09:33:42 +0000 | [diff] [blame] | 187 | int packets = 0; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 188 | |
Fam Zheng | a90a742 | 2015-06-04 14:45:17 +0800 | [diff] [blame] | 189 | while (true) { |
Mark McLoughlin | 5819c91 | 2009-10-27 18:16:39 +0000 | [diff] [blame] | 190 | uint8_t *buf = s->buf; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 191 | |
Mark McLoughlin | 5819c91 | 2009-10-27 18:16:39 +0000 | [diff] [blame] | 192 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); |
| 193 | if (size <= 0) { |
| 194 | break; |
| 195 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 196 | |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 197 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
| 198 | buf += s->host_vnet_hdr_len; |
| 199 | size -= s->host_vnet_hdr_len; |
Mark McLoughlin | 5819c91 | 2009-10-27 18:16:39 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 202 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
Mark McLoughlin | 5819c91 | 2009-10-27 18:16:39 +0000 | [diff] [blame] | 203 | if (size == 0) { |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 204 | tap_read_poll(s, false); |
Stefan Hajnoczi | 68e5ec6 | 2014-03-08 16:00:43 +0100 | [diff] [blame] | 205 | break; |
| 206 | } else if (size < 0) { |
| 207 | break; |
Mark McLoughlin | 5819c91 | 2009-10-27 18:16:39 +0000 | [diff] [blame] | 208 | } |
Wangkai (Kevin,C) | 756ae78 | 2014-07-18 09:33:42 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * When the host keeps receiving more packets while tap_send() is |
| 212 | * running we can hog the QEMU global mutex. Limit the number of |
| 213 | * packets that are processed per tap_send() callback to prevent |
| 214 | * stalling the guest. |
| 215 | */ |
| 216 | packets++; |
| 217 | if (packets >= 50) { |
| 218 | break; |
| 219 | } |
Stefan Hajnoczi | 68e5ec6 | 2014-03-08 16:00:43 +0100 | [diff] [blame] | 220 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 223 | static bool tap_has_ufo(NetClientState *nc) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 224 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 225 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 226 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 227 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 228 | |
| 229 | return s->has_ufo; |
| 230 | } |
| 231 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 232 | static bool tap_has_vnet_hdr(NetClientState *nc) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 233 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 234 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 235 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 236 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 237 | |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 238 | return !!s->host_vnet_hdr_len; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 241 | static bool tap_has_vnet_hdr_len(NetClientState *nc, int len) |
Michael S. Tsirkin | 445d892 | 2010-07-16 11:16:06 +0300 | [diff] [blame] | 242 | { |
| 243 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 244 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 245 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Michael S. Tsirkin | 445d892 | 2010-07-16 11:16:06 +0300 | [diff] [blame] | 246 | |
Vincenzo Maffione | e96dfd1 | 2014-02-06 17:02:15 +0100 | [diff] [blame] | 247 | return !!tap_probe_vnet_hdr_len(s->fd, len); |
Michael S. Tsirkin | 445d892 | 2010-07-16 11:16:06 +0300 | [diff] [blame] | 248 | } |
| 249 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 250 | static void tap_set_vnet_hdr_len(NetClientState *nc, int len) |
Michael S. Tsirkin | 445d892 | 2010-07-16 11:16:06 +0300 | [diff] [blame] | 251 | { |
| 252 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 253 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 254 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Michael S. Tsirkin | 445d892 | 2010-07-16 11:16:06 +0300 | [diff] [blame] | 255 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
| 256 | len == sizeof(struct virtio_net_hdr)); |
| 257 | |
| 258 | tap_fd_set_vnet_hdr_len(s->fd, len); |
| 259 | s->host_vnet_hdr_len = len; |
| 260 | } |
| 261 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 262 | static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 263 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 264 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 265 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 266 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 267 | assert(!!s->host_vnet_hdr_len == using_vnet_hdr); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 268 | |
| 269 | s->using_vnet_hdr = using_vnet_hdr; |
| 270 | } |
| 271 | |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 272 | static int tap_set_vnet_le(NetClientState *nc, bool is_le) |
| 273 | { |
| 274 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 275 | |
| 276 | return tap_fd_set_vnet_le(s->fd, is_le); |
| 277 | } |
| 278 | |
| 279 | static int tap_set_vnet_be(NetClientState *nc, bool is_be) |
| 280 | { |
| 281 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 282 | |
| 283 | return tap_fd_set_vnet_be(s->fd, is_be); |
| 284 | } |
| 285 | |
Vincenzo Maffione | 3bac80d | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 286 | static void tap_set_offload(NetClientState *nc, int csum, int tso4, |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 287 | int tso6, int ecn, int ufo) |
| 288 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 289 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Michael S. Tsirkin | 27a6375 | 2010-10-31 19:06:47 +0200 | [diff] [blame] | 290 | if (s->fd < 0) { |
| 291 | return; |
| 292 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 293 | |
Michael S. Tsirkin | 27a6375 | 2010-10-31 19:06:47 +0200 | [diff] [blame] | 294 | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Marc-André Lureau | 9e32ff3 | 2016-07-11 16:48:47 +0200 | [diff] [blame] | 297 | static void tap_exit_notify(Notifier *notifier, void *data) |
| 298 | { |
| 299 | TAPState *s = container_of(notifier, TAPState, exit); |
| 300 | Error *err = NULL; |
| 301 | |
| 302 | if (s->down_script[0]) { |
| 303 | launch_script(s->down_script, s->down_script_arg, s->fd, &err); |
| 304 | if (err) { |
| 305 | error_report_err(err); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 310 | static void tap_cleanup(NetClientState *nc) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 311 | { |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 312 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 313 | |
Michael S. Tsirkin | 82b0d80 | 2010-03-17 13:08:24 +0200 | [diff] [blame] | 314 | if (s->vhost_net) { |
| 315 | vhost_net_cleanup(s->vhost_net); |
Marc-André Lureau | e6bcb1b | 2016-07-27 01:15:12 +0400 | [diff] [blame] | 316 | g_free(s->vhost_net); |
Michael S. Tsirkin | 4384942 | 2010-10-27 20:03:43 +0200 | [diff] [blame] | 317 | s->vhost_net = NULL; |
Michael S. Tsirkin | 82b0d80 | 2010-03-17 13:08:24 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 320 | qemu_purge_queued_packets(nc); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 321 | |
Marc-André Lureau | 9e32ff3 | 2016-07-11 16:48:47 +0200 | [diff] [blame] | 322 | tap_exit_notify(&s->exit, NULL); |
| 323 | qemu_remove_exit_notifier(&s->exit); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 324 | |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 325 | tap_read_poll(s, false); |
| 326 | tap_write_poll(s, false); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 327 | close(s->fd); |
Michael S. Tsirkin | 27a6375 | 2010-10-31 19:06:47 +0200 | [diff] [blame] | 328 | s->fd = -1; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 331 | static void tap_poll(NetClientState *nc, bool enable) |
Michael S. Tsirkin | ceb6961 | 2009-12-24 14:46:29 +0200 | [diff] [blame] | 332 | { |
| 333 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 334 | tap_read_poll(s, enable); |
| 335 | tap_write_poll(s, enable); |
| 336 | } |
| 337 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 338 | int tap_get_fd(NetClientState *nc) |
Michael S. Tsirkin | 95d528a | 2010-03-17 13:07:50 +0200 | [diff] [blame] | 339 | { |
| 340 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 341 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Michael S. Tsirkin | 95d528a | 2010-03-17 13:07:50 +0200 | [diff] [blame] | 342 | return s->fd; |
| 343 | } |
| 344 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 345 | /* fd support */ |
| 346 | |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 347 | static NetClientInfo net_tap_info = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 348 | .type = NET_CLIENT_DRIVER_TAP, |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 349 | .size = sizeof(TAPState), |
| 350 | .receive = tap_receive, |
| 351 | .receive_raw = tap_receive_raw, |
| 352 | .receive_iov = tap_receive_iov, |
Michael S. Tsirkin | ceb6961 | 2009-12-24 14:46:29 +0200 | [diff] [blame] | 353 | .poll = tap_poll, |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 354 | .cleanup = tap_cleanup, |
Vincenzo Maffione | 2e753bc | 2014-02-06 17:02:17 +0100 | [diff] [blame] | 355 | .has_ufo = tap_has_ufo, |
| 356 | .has_vnet_hdr = tap_has_vnet_hdr, |
| 357 | .has_vnet_hdr_len = tap_has_vnet_hdr_len, |
| 358 | .using_vnet_hdr = tap_using_vnet_hdr, |
| 359 | .set_offload = tap_set_offload, |
| 360 | .set_vnet_hdr_len = tap_set_vnet_hdr_len, |
Greg Kurz | c80cd6b | 2015-06-17 15:23:44 +0200 | [diff] [blame] | 361 | .set_vnet_le = tap_set_vnet_le, |
| 362 | .set_vnet_be = tap_set_vnet_be, |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 365 | static TAPState *net_tap_fd_init(NetClientState *peer, |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 366 | const char *model, |
| 367 | const char *name, |
| 368 | int fd, |
| 369 | int vnet_hdr) |
| 370 | { |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 371 | NetClientState *nc; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 372 | TAPState *s; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 373 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame] | 374 | nc = qemu_new_net_client(&net_tap_info, peer, model, name); |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 375 | |
| 376 | s = DO_UPCAST(TAPState, nc, nc); |
| 377 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 378 | s->fd = fd; |
Michael S. Tsirkin | ef4252b | 2010-07-13 17:55:31 +0300 | [diff] [blame] | 379 | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 380 | s->using_vnet_hdr = false; |
Mark McLoughlin | 9c28271 | 2009-10-22 17:49:16 +0100 | [diff] [blame] | 381 | s->has_ufo = tap_probe_has_ufo(s->fd); |
Jason Wang | 16dbaf9 | 2013-01-30 19:12:32 +0800 | [diff] [blame] | 382 | s->enabled = true; |
Mark McLoughlin | 3e35ba9 | 2009-11-25 18:49:04 +0000 | [diff] [blame] | 383 | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
Michael S. Tsirkin | 58ddcd5 | 2012-11-13 12:23:23 +0200 | [diff] [blame] | 384 | /* |
| 385 | * Make sure host header length is set correctly in tap: |
| 386 | * it might have been modified by another instance of qemu. |
| 387 | */ |
| 388 | if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) { |
| 389 | tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len); |
| 390 | } |
Jason Wang | ec45f08 | 2013-01-30 19:12:20 +0800 | [diff] [blame] | 391 | tap_read_poll(s, true); |
Michael S. Tsirkin | 82b0d80 | 2010-03-17 13:08:24 +0200 | [diff] [blame] | 392 | s->vhost_net = NULL; |
Marc-André Lureau | 9e32ff3 | 2016-07-11 16:48:47 +0200 | [diff] [blame] | 393 | |
| 394 | s->exit.notify = tap_exit_notify; |
| 395 | qemu_add_exit_notifier(&s->exit); |
| 396 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 397 | return s; |
| 398 | } |
| 399 | |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 400 | static void launch_script(const char *setup_script, const char *ifname, |
| 401 | int fd, Error **errp) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 402 | { |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 403 | int pid, status; |
| 404 | char *args[3]; |
| 405 | char **parg; |
| 406 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 407 | /* try to launch network script */ |
| 408 | pid = fork(); |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 409 | if (pid < 0) { |
| 410 | error_setg_errno(errp, errno, "could not launch network script %s", |
| 411 | setup_script); |
| 412 | return; |
| 413 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 414 | if (pid == 0) { |
| 415 | int open_max = sysconf(_SC_OPEN_MAX), i; |
| 416 | |
Pankaj Gupta | 13a12f8 | 2014-03-12 22:24:27 +0530 | [diff] [blame] | 417 | for (i = 3; i < open_max; i++) { |
| 418 | if (i != fd) { |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 419 | close(i); |
| 420 | } |
| 421 | } |
| 422 | parg = args; |
| 423 | *parg++ = (char *)setup_script; |
| 424 | *parg++ = (char *)ifname; |
Blue Swirl | 9678d95 | 2010-04-25 18:35:52 +0000 | [diff] [blame] | 425 | *parg = NULL; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 426 | execv(setup_script, args); |
| 427 | _exit(1); |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 428 | } else { |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 429 | while (waitpid(pid, &status, 0) != pid) { |
| 430 | /* loop */ |
| 431 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 432 | |
| 433 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 434 | return; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 435 | } |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 436 | error_setg(errp, "network script %s failed with status %d", |
| 437 | setup_script, status); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 438 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 441 | static int recv_fd(int c) |
| 442 | { |
| 443 | int fd; |
| 444 | uint8_t msgbuf[CMSG_SPACE(sizeof(fd))]; |
| 445 | struct msghdr msg = { |
| 446 | .msg_control = msgbuf, |
| 447 | .msg_controllen = sizeof(msgbuf), |
| 448 | }; |
| 449 | struct cmsghdr *cmsg; |
| 450 | struct iovec iov; |
| 451 | uint8_t req[1]; |
| 452 | ssize_t len; |
| 453 | |
| 454 | cmsg = CMSG_FIRSTHDR(&msg); |
| 455 | cmsg->cmsg_level = SOL_SOCKET; |
| 456 | cmsg->cmsg_type = SCM_RIGHTS; |
| 457 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); |
| 458 | msg.msg_controllen = cmsg->cmsg_len; |
| 459 | |
| 460 | iov.iov_base = req; |
| 461 | iov.iov_len = sizeof(req); |
| 462 | |
| 463 | msg.msg_iov = &iov; |
| 464 | msg.msg_iovlen = 1; |
| 465 | |
| 466 | len = recvmsg(c, &msg, 0); |
| 467 | if (len > 0) { |
| 468 | memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); |
| 469 | return fd; |
| 470 | } |
| 471 | |
| 472 | return len; |
| 473 | } |
| 474 | |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 475 | static int net_bridge_run_helper(const char *helper, const char *bridge, |
| 476 | Error **errp) |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 477 | { |
| 478 | sigset_t oldmask, mask; |
| 479 | int pid, status; |
| 480 | char *args[5]; |
| 481 | char **parg; |
| 482 | int sv[2]; |
| 483 | |
| 484 | sigemptyset(&mask); |
| 485 | sigaddset(&mask, SIGCHLD); |
| 486 | sigprocmask(SIG_BLOCK, &mask, &oldmask); |
| 487 | |
| 488 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 489 | error_setg_errno(errp, errno, "socketpair() failed"); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 490 | return -1; |
| 491 | } |
| 492 | |
| 493 | /* try to launch bridge helper */ |
| 494 | pid = fork(); |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 495 | if (pid < 0) { |
| 496 | error_setg_errno(errp, errno, "Can't fork bridge helper"); |
| 497 | return -1; |
| 498 | } |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 499 | if (pid == 0) { |
| 500 | int open_max = sysconf(_SC_OPEN_MAX), i; |
| 501 | char fd_buf[6+10]; |
| 502 | char br_buf[6+IFNAMSIZ] = {0}; |
| 503 | char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15]; |
| 504 | |
Pankaj Gupta | 13a12f8 | 2014-03-12 22:24:27 +0530 | [diff] [blame] | 505 | for (i = 3; i < open_max; i++) { |
| 506 | if (i != sv[1]) { |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 507 | close(i); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]); |
| 512 | |
| 513 | if (strrchr(helper, ' ') || strrchr(helper, '\t')) { |
| 514 | /* assume helper is a command */ |
| 515 | |
| 516 | if (strstr(helper, "--br=") == NULL) { |
| 517 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); |
| 518 | } |
| 519 | |
| 520 | snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s", |
| 521 | helper, "--use-vnet", fd_buf, br_buf); |
| 522 | |
| 523 | parg = args; |
| 524 | *parg++ = (char *)"sh"; |
| 525 | *parg++ = (char *)"-c"; |
| 526 | *parg++ = helper_cmd; |
| 527 | *parg++ = NULL; |
| 528 | |
| 529 | execv("/bin/sh", args); |
| 530 | } else { |
| 531 | /* assume helper is just the executable path name */ |
| 532 | |
| 533 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); |
| 534 | |
| 535 | parg = args; |
| 536 | *parg++ = (char *)helper; |
| 537 | *parg++ = (char *)"--use-vnet"; |
| 538 | *parg++ = fd_buf; |
| 539 | *parg++ = br_buf; |
| 540 | *parg++ = NULL; |
| 541 | |
| 542 | execv(helper, args); |
| 543 | } |
| 544 | _exit(1); |
| 545 | |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 546 | } else { |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 547 | int fd; |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 548 | int saved_errno; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 549 | |
| 550 | close(sv[1]); |
| 551 | |
| 552 | do { |
| 553 | fd = recv_fd(sv[0]); |
| 554 | } while (fd == -1 && errno == EINTR); |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 555 | saved_errno = errno; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 556 | |
| 557 | close(sv[0]); |
| 558 | |
| 559 | while (waitpid(pid, &status, 0) != pid) { |
| 560 | /* loop */ |
| 561 | } |
| 562 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
| 563 | if (fd < 0) { |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 564 | error_setg_errno(errp, saved_errno, |
| 565 | "failed to recv file descriptor"); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 566 | return -1; |
| 567 | } |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 568 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 569 | error_setg(errp, "bridge helper failed"); |
| 570 | return -1; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 571 | } |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 572 | return fd; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 573 | } |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 574 | } |
| 575 | |
KÅ‘vĂ¡gĂ³, ZoltĂ¡n | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 576 | int net_init_bridge(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 577 | NetClientState *peer, Error **errp) |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 578 | { |
Laszlo Ersek | f79b51b | 2012-07-17 16:17:20 +0200 | [diff] [blame] | 579 | const NetdevBridgeOptions *bridge; |
| 580 | const char *helper, *br; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 581 | TAPState *s; |
| 582 | int fd, vnet_hdr; |
| 583 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 584 | assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE); |
| 585 | bridge = &netdev->u.bridge; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 586 | |
Laszlo Ersek | f79b51b | 2012-07-17 16:17:20 +0200 | [diff] [blame] | 587 | helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER; |
| 588 | br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE; |
| 589 | |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 590 | fd = net_bridge_run_helper(helper, br, errp); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 591 | if (fd == -1) { |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | fcntl(fd, F_SETFL, O_NONBLOCK); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 596 | vnet_hdr = tap_probe_vnet_hdr(fd); |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 597 | s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 598 | |
Laszlo Ersek | f79b51b | 2012-07-17 16:17:20 +0200 | [diff] [blame] | 599 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper, |
| 600 | br); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 605 | static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, |
| 606 | const char *setup_script, char *ifname, |
Markus Armbruster | 468dd82 | 2015-05-15 13:58:58 +0200 | [diff] [blame] | 607 | size_t ifname_sz, int mq_required, Error **errp) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 608 | { |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 609 | Error *err = NULL; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 610 | int fd, vnet_hdr_required; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 611 | |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 612 | if (tap->has_vnet_hdr) { |
| 613 | *vnet_hdr = tap->vnet_hdr; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 614 | vnet_hdr_required = *vnet_hdr; |
| 615 | } else { |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 616 | *vnet_hdr = 1; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 617 | vnet_hdr_required = 0; |
| 618 | } |
| 619 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 620 | TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required, |
Markus Armbruster | 468dd82 | 2015-05-15 13:58:58 +0200 | [diff] [blame] | 621 | mq_required, errp)); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 622 | if (fd < 0) { |
| 623 | return -1; |
| 624 | } |
| 625 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 626 | if (setup_script && |
| 627 | setup_script[0] != '\0' && |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 628 | strcmp(setup_script, "no") != 0) { |
| 629 | launch_script(setup_script, ifname, fd, &err); |
| 630 | if (err) { |
Markus Armbruster | 468dd82 | 2015-05-15 13:58:58 +0200 | [diff] [blame] | 631 | error_propagate(errp, err); |
Markus Armbruster | ac4fcf5 | 2015-05-15 13:58:57 +0200 | [diff] [blame] | 632 | close(fd); |
| 633 | return -1; |
| 634 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 637 | return fd; |
| 638 | } |
| 639 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 640 | #define MAX_TAP_QUEUES 1024 |
| 641 | |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 642 | static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, |
| 643 | const char *model, const char *name, |
| 644 | const char *ifname, const char *script, |
| 645 | const char *downscript, const char *vhostfdname, |
| 646 | int vnet_hdr, int fd, Error **errp) |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 647 | { |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 648 | Error *err = NULL; |
Markus Armbruster | da4a4ea | 2015-05-15 13:58:53 +0200 | [diff] [blame] | 649 | TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 650 | int vhostfd; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 651 | |
Markus Armbruster | 80b832c | 2015-05-15 13:58:55 +0200 | [diff] [blame] | 652 | tap_set_sndbuf(s->fd, tap, &err); |
| 653 | if (err) { |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 654 | error_propagate(errp, err); |
| 655 | return; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 656 | } |
| 657 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 658 | if (tap->has_fd || tap->has_fds) { |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 659 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
| 660 | } else if (tap->has_helper) { |
| 661 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s", |
| 662 | tap->helper); |
| 663 | } else { |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 664 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
| 665 | "ifname=%s,script=%s,downscript=%s", ifname, script, |
| 666 | downscript); |
| 667 | |
| 668 | if (strcmp(downscript, "no") != 0) { |
| 669 | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); |
| 670 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), |
| 671 | "%s", ifname); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (tap->has_vhost ? tap->vhost : |
| 676 | vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 677 | VhostNetOptions options; |
| 678 | |
Nikolay Nikolaev | 1a1bfac | 2014-05-27 15:05:49 +0300 | [diff] [blame] | 679 | options.backend_type = VHOST_BACKEND_TYPE_KERNEL; |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 680 | options.net_backend = &s->nc; |
Jason Wang | 69e87b3 | 2016-07-06 09:57:55 +0800 | [diff] [blame] | 681 | if (tap->has_poll_us) { |
| 682 | options.busyloop_timeout = tap->poll_us; |
| 683 | } else { |
| 684 | options.busyloop_timeout = 0; |
| 685 | } |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 686 | |
Paolo Bonzini | 3a2d44f | 2016-02-26 00:05:57 +0100 | [diff] [blame] | 687 | if (vhostfdname) { |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 688 | vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err); |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 689 | if (vhostfd == -1) { |
Jay Zhou | 46d4d36 | 2018-03-02 17:04:44 +0800 | [diff] [blame] | 690 | if (tap->has_vhostforce && tap->vhostforce) { |
| 691 | error_propagate(errp, err); |
| 692 | } else { |
| 693 | warn_report_err(err); |
| 694 | } |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 695 | return; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 696 | } |
Brijesh Singh | d542800 | 2018-04-06 13:51:25 -0500 | [diff] [blame] | 697 | qemu_set_nonblock(vhostfd); |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 698 | } else { |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 699 | vhostfd = open("/dev/vhost-net", O_RDWR); |
| 700 | if (vhostfd < 0) { |
Jay Zhou | 46d4d36 | 2018-03-02 17:04:44 +0800 | [diff] [blame] | 701 | if (tap->has_vhostforce && tap->vhostforce) { |
| 702 | error_setg_errno(errp, errno, |
| 703 | "tap: open vhost char device failed"); |
| 704 | } else { |
| 705 | warn_report("tap: open vhost char device failed: %s", |
| 706 | strerror(errno)); |
| 707 | } |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 708 | return; |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 709 | } |
Jason Wang | c471ad0 | 2017-01-11 12:32:12 +0800 | [diff] [blame] | 710 | fcntl(vhostfd, F_SETFL, O_NONBLOCK); |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 711 | } |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 712 | options.opaque = (void *)(uintptr_t)vhostfd; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 713 | |
Nikolay Nikolaev | 81647a6 | 2014-05-27 15:05:22 +0300 | [diff] [blame] | 714 | s->vhost_net = vhost_net_init(&options); |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 715 | if (!s->vhost_net) { |
Jay Zhou | 46d4d36 | 2018-03-02 17:04:44 +0800 | [diff] [blame] | 716 | if (tap->has_vhostforce && tap->vhostforce) { |
| 717 | error_setg(errp, VHOST_NET_INIT_FAILED); |
| 718 | } else { |
| 719 | warn_report(VHOST_NET_INIT_FAILED); |
| 720 | } |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 721 | return; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 722 | } |
Paolo Bonzini | 3a2d44f | 2016-02-26 00:05:57 +0100 | [diff] [blame] | 723 | } else if (vhostfdname) { |
Jason Wang | 69e87b3 | 2016-07-06 09:57:55 +0800 | [diff] [blame] | 724 | error_setg(errp, "vhostfd(s)= is not valid without vhost"); |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 725 | } |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 726 | } |
| 727 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 728 | static int get_fds(char *str, char *fds[], int max) |
| 729 | { |
| 730 | char *ptr = str, *this; |
| 731 | size_t len = strlen(str); |
| 732 | int i = 0; |
| 733 | |
| 734 | while (i < max && ptr < str + len) { |
| 735 | this = strchr(ptr, ':'); |
| 736 | |
| 737 | if (this == NULL) { |
| 738 | fds[i] = g_strdup(ptr); |
| 739 | } else { |
| 740 | fds[i] = g_strndup(ptr, this - ptr); |
| 741 | } |
| 742 | |
| 743 | i++; |
| 744 | if (this == NULL) { |
| 745 | break; |
| 746 | } else { |
| 747 | ptr = this + 1; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | return i; |
| 752 | } |
| 753 | |
KÅ‘vĂ¡gĂ³, ZoltĂ¡n | cebea51 | 2016-07-13 21:50:12 -0600 | [diff] [blame] | 754 | int net_init_tap(const Netdev *netdev, const char *name, |
Markus Armbruster | a30ecde | 2015-05-15 13:58:50 +0200 | [diff] [blame] | 755 | NetClientState *peer, Error **errp) |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 756 | { |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 757 | const NetdevTapOptions *tap; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 758 | int fd, vnet_hdr = 0, i = 0, queues; |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 759 | /* for the no-fd, no-helper case */ |
| 760 | const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */ |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 761 | const char *downscript = NULL; |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 762 | Error *err = NULL; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 763 | const char *vhostfdname; |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 764 | char ifname[128]; |
| 765 | |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 766 | assert(netdev->type == NET_CLIENT_DRIVER_TAP); |
| 767 | tap = &netdev->u.tap; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 768 | queues = tap->has_queues ? tap->queues : 1; |
| 769 | vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL; |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 770 | |
Thomas Huth | 442da40 | 2018-04-30 20:02:24 +0200 | [diff] [blame] | 771 | /* QEMU hubs do not support multiqueue tap, in this case peer is set. |
Jason Wang | ce675a7 | 2013-02-21 11:05:56 +0800 | [diff] [blame] | 772 | * For -netdev, peer is always NULL. */ |
| 773 | if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) { |
Thomas Huth | 442da40 | 2018-04-30 20:02:24 +0200 | [diff] [blame] | 774 | error_setg(errp, "Multiqueue tap cannot be used with hubs"); |
Jason Wang | ce675a7 | 2013-02-21 11:05:56 +0800 | [diff] [blame] | 775 | return -1; |
| 776 | } |
| 777 | |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 778 | if (tap->has_fd) { |
| 779 | if (tap->has_ifname || tap->has_script || tap->has_downscript || |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 780 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || |
Jason Wang | c87826a | 2013-06-04 13:18:17 +0800 | [diff] [blame] | 781 | tap->has_fds || tap->has_vhostfds) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 782 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
| 783 | "helper=, queues=, fds=, and vhostfds= " |
| 784 | "are invalid with fd="); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 788 | fd = monitor_fd_param(cur_mon, tap->fd, &err); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 789 | if (fd == -1) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 790 | error_propagate(errp, err); |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 795 | |
| 796 | vnet_hdr = tap_probe_vnet_hdr(fd); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 797 | |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 798 | net_init_tap_one(tap, peer, "tap", name, NULL, |
| 799 | script, downscript, |
| 800 | vhostfdname, vnet_hdr, fd, &err); |
| 801 | if (err) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 802 | error_propagate(errp, err); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 803 | return -1; |
| 804 | } |
| 805 | } else if (tap->has_fds) { |
Peter Maydell | fac7d7b | 2017-01-10 19:21:54 +0000 | [diff] [blame] | 806 | char **fds; |
| 807 | char **vhost_fds; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 808 | int nfds, nvhosts; |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 809 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 810 | if (tap->has_ifname || tap->has_script || tap->has_downscript || |
| 811 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || |
Jason Wang | c87826a | 2013-06-04 13:18:17 +0800 | [diff] [blame] | 812 | tap->has_vhostfd) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 813 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
| 814 | "helper=, queues=, and vhostfd= " |
| 815 | "are invalid with fds="); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 816 | return -1; |
| 817 | } |
| 818 | |
Peter Maydell | fac7d7b | 2017-01-10 19:21:54 +0000 | [diff] [blame] | 819 | fds = g_new0(char *, MAX_TAP_QUEUES); |
| 820 | vhost_fds = g_new0(char *, MAX_TAP_QUEUES); |
| 821 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 822 | nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES); |
| 823 | if (tap->has_vhostfds) { |
| 824 | nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES); |
| 825 | if (nfds != nvhosts) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 826 | error_setg(errp, "The number of fds passed does not match " |
| 827 | "the number of vhostfds passed"); |
Paolo Bonzini | 091a6b2 | 2016-07-15 10:56:07 +0200 | [diff] [blame] | 828 | goto free_fail; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | for (i = 0; i < nfds; i++) { |
Markus Armbruster | 1677f4c | 2015-02-09 14:03:19 +0100 | [diff] [blame] | 833 | fd = monitor_fd_param(cur_mon, fds[i], &err); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 834 | if (fd == -1) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 835 | error_propagate(errp, err); |
Paolo Bonzini | 091a6b2 | 2016-07-15 10:56:07 +0200 | [diff] [blame] | 836 | goto free_fail; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 840 | |
| 841 | if (i == 0) { |
| 842 | vnet_hdr = tap_probe_vnet_hdr(fd); |
| 843 | } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 844 | error_setg(errp, |
| 845 | "vnet_hdr not consistent across given tap fds"); |
Paolo Bonzini | 091a6b2 | 2016-07-15 10:56:07 +0200 | [diff] [blame] | 846 | goto free_fail; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 847 | } |
| 848 | |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 849 | net_init_tap_one(tap, peer, "tap", name, ifname, |
| 850 | script, downscript, |
| 851 | tap->has_vhostfds ? vhost_fds[i] : NULL, |
| 852 | vnet_hdr, fd, &err); |
| 853 | if (err) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 854 | error_propagate(errp, err); |
Paolo Bonzini | 091a6b2 | 2016-07-15 10:56:07 +0200 | [diff] [blame] | 855 | goto free_fail; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 856 | } |
| 857 | } |
Zhou Jie | 11196e9 | 2016-04-26 09:26:01 +0800 | [diff] [blame] | 858 | g_free(fds); |
| 859 | g_free(vhost_fds); |
Paolo Bonzini | 091a6b2 | 2016-07-15 10:56:07 +0200 | [diff] [blame] | 860 | return 0; |
| 861 | |
| 862 | free_fail: |
| 863 | for (i = 0; i < nfds; i++) { |
| 864 | g_free(fds[i]); |
| 865 | g_free(vhost_fds[i]); |
| 866 | } |
| 867 | g_free(fds); |
| 868 | g_free(vhost_fds); |
| 869 | return -1; |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 870 | } else if (tap->has_helper) { |
| 871 | if (tap->has_ifname || tap->has_script || tap->has_downscript || |
Jason Wang | c87826a | 2013-06-04 13:18:17 +0800 | [diff] [blame] | 872 | tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 873 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
| 874 | "queues=, and vhostfds= are invalid with helper="); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 875 | return -1; |
| 876 | } |
| 877 | |
Alexey Kardashevskiy | 584613e | 2016-09-13 17:11:54 +1000 | [diff] [blame] | 878 | fd = net_bridge_run_helper(tap->helper, |
| 879 | tap->has_br ? |
| 880 | tap->br : DEFAULT_BRIDGE_INTERFACE, |
Markus Armbruster | a8a21be | 2015-05-15 13:58:54 +0200 | [diff] [blame] | 881 | errp); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 882 | if (fd == -1) { |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | fcntl(fd, F_SETFL, O_NONBLOCK); |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 887 | vnet_hdr = tap_probe_vnet_hdr(fd); |
| 888 | |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 889 | net_init_tap_one(tap, peer, "bridge", name, ifname, |
| 890 | script, downscript, vhostfdname, |
| 891 | vnet_hdr, fd, &err); |
| 892 | if (err) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 893 | error_propagate(errp, err); |
Gonglei | 84f8f3d | 2014-11-02 13:37:17 +0800 | [diff] [blame] | 894 | close(fd); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 895 | return -1; |
| 896 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 897 | } else { |
Jason Wang | c87826a | 2013-06-04 13:18:17 +0800 | [diff] [blame] | 898 | if (tap->has_vhostfds) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 899 | error_setg(errp, "vhostfds= is invalid if fds= wasn't specified"); |
Jason Wang | c87826a | 2013-06-04 13:18:17 +0800 | [diff] [blame] | 900 | return -1; |
| 901 | } |
Laszlo Ersek | 08c573a | 2012-07-17 16:17:19 +0200 | [diff] [blame] | 902 | script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT; |
Jason Wang | 5193e5f | 2013-01-30 19:12:30 +0800 | [diff] [blame] | 903 | downscript = tap->has_downscript ? tap->downscript : |
| 904 | DEFAULT_NETWORK_DOWN_SCRIPT; |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 905 | |
| 906 | if (tap->has_ifname) { |
| 907 | pstrcpy(ifname, sizeof ifname, tap->ifname); |
| 908 | } else { |
| 909 | ifname[0] = '\0'; |
Juergen Lock | 929fe49 | 2009-11-20 23:23:03 +0100 | [diff] [blame] | 910 | } |
Corey Bryant | a7c36ee | 2012-01-26 09:42:27 -0500 | [diff] [blame] | 911 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 912 | for (i = 0; i < queues; i++) { |
| 913 | fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 914 | ifname, sizeof ifname, queues > 1, errp); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 915 | if (fd == -1) { |
| 916 | return -1; |
| 917 | } |
| 918 | |
| 919 | if (queues > 1 && i == 0 && !tap->has_ifname) { |
| 920 | if (tap_fd_get_ifname(fd, ifname)) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 921 | error_setg(errp, "Fail to get ifname"); |
Gonglei | 84f8f3d | 2014-11-02 13:37:17 +0800 | [diff] [blame] | 922 | close(fd); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 923 | return -1; |
| 924 | } |
| 925 | } |
| 926 | |
Markus Armbruster | 445f116 | 2015-05-15 13:58:56 +0200 | [diff] [blame] | 927 | net_init_tap_one(tap, peer, "tap", name, ifname, |
| 928 | i >= 1 ? "no" : script, |
| 929 | i >= 1 ? "no" : downscript, |
| 930 | vhostfdname, vnet_hdr, fd, &err); |
| 931 | if (err) { |
Markus Armbruster | a308817 | 2015-05-15 13:59:03 +0200 | [diff] [blame] | 932 | error_propagate(errp, err); |
Gonglei | 84f8f3d | 2014-11-02 13:37:17 +0800 | [diff] [blame] | 933 | close(fd); |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | } |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Jason Wang | 264986e | 2013-01-30 19:12:34 +0800 | [diff] [blame] | 939 | return 0; |
Mark McLoughlin | 5281d75 | 2009-10-22 17:49:07 +0100 | [diff] [blame] | 940 | } |
Michael S. Tsirkin | b202554 | 2010-03-17 13:08:38 +0200 | [diff] [blame] | 941 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 942 | VHostNetState *tap_get_vhost_net(NetClientState *nc) |
Michael S. Tsirkin | b202554 | 2010-03-17 13:08:38 +0200 | [diff] [blame] | 943 | { |
| 944 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 945 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
Michael S. Tsirkin | b202554 | 2010-03-17 13:08:38 +0200 | [diff] [blame] | 946 | return s->vhost_net; |
| 947 | } |
Jason Wang | 16dbaf9 | 2013-01-30 19:12:32 +0800 | [diff] [blame] | 948 | |
| 949 | int tap_enable(NetClientState *nc) |
| 950 | { |
| 951 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 952 | int ret; |
| 953 | |
| 954 | if (s->enabled) { |
| 955 | return 0; |
| 956 | } else { |
| 957 | ret = tap_fd_enable(s->fd); |
| 958 | if (ret == 0) { |
| 959 | s->enabled = true; |
| 960 | tap_update_fd_handler(s); |
| 961 | } |
| 962 | return ret; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | int tap_disable(NetClientState *nc) |
| 967 | { |
| 968 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
| 969 | int ret; |
| 970 | |
| 971 | if (s->enabled == 0) { |
| 972 | return 0; |
| 973 | } else { |
| 974 | ret = tap_fd_disable(s->fd); |
| 975 | if (ret == 0) { |
| 976 | qemu_purge_queued_packets(nc); |
| 977 | s->enabled = false; |
| 978 | tap_update_fd_handler(s); |
| 979 | } |
| 980 | return ret; |
| 981 | } |
| 982 | } |