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