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