]>
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" |
5281d757 | 41 | #include "qemu-common.h" |
f348b6d1 | 42 | #include "qemu/cutils.h" |
1de7afc9 | 43 | #include "qemu/error-report.h" |
db725815 | 44 | #include "qemu/main-loop.h" |
d542800d | 45 | #include "qemu/sockets.h" |
5281d757 | 46 | |
1422e32d | 47 | #include "net/tap.h" |
5281d757 | 48 | |
0d09e41a | 49 | #include "net/vhost_net.h" |
82b0d80e | 50 | |
5281d757 | 51 | typedef struct TAPState { |
4e68f7a0 | 52 | NetClientState nc; |
5281d757 MM |
53 | int fd; |
54 | char down_script[1024]; | |
55 | char down_script_arg[128]; | |
d32fcad3 | 56 | uint8_t buf[NET_BUFSIZE]; |
ec45f083 JW |
57 | bool read_poll; |
58 | bool write_poll; | |
59 | bool using_vnet_hdr; | |
60 | bool has_ufo; | |
16dbaf90 | 61 | bool enabled; |
82b0d80e | 62 | VHostNetState *vhost_net; |
ef4252b1 | 63 | unsigned host_vnet_hdr_len; |
9e32ff32 | 64 | Notifier exit; |
5281d757 MM |
65 | } TAPState; |
66 | ||
ac4fcf56 MA |
67 | static void launch_script(const char *setup_script, const char *ifname, |
68 | int fd, Error **errp); | |
5281d757 | 69 | |
5281d757 MM |
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 | { | |
82e1cc4b FZ |
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); | |
5281d757 MM |
79 | } |
80 | ||
ec45f083 | 81 | static void tap_read_poll(TAPState *s, bool enable) |
5281d757 | 82 | { |
ec45f083 | 83 | s->read_poll = enable; |
5281d757 MM |
84 | tap_update_fd_handler(s); |
85 | } | |
86 | ||
ec45f083 | 87 | static void tap_write_poll(TAPState *s, bool enable) |
5281d757 | 88 | { |
ec45f083 | 89 | s->write_poll = enable; |
5281d757 MM |
90 | tap_update_fd_handler(s); |
91 | } | |
92 | ||
93 | static void tap_writable(void *opaque) | |
94 | { | |
95 | TAPState *s = opaque; | |
96 | ||
ec45f083 | 97 | tap_write_poll(s, false); |
5281d757 | 98 | |
3e35ba93 | 99 | qemu_flush_queued_packets(&s->nc); |
5281d757 MM |
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) { | |
ec45f083 | 111 | tap_write_poll(s, true); |
5281d757 MM |
112 | return 0; |
113 | } | |
114 | ||
115 | return len; | |
116 | } | |
117 | ||
4e68f7a0 | 118 | static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov, |
5281d757 MM |
119 | int iovcnt) |
120 | { | |
3e35ba93 | 121 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
122 | const struct iovec *iovp = iov; |
123 | struct iovec iov_copy[iovcnt + 1]; | |
ef4252b1 | 124 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
5281d757 | 125 | |
ef4252b1 | 126 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
5281d757 | 127 | iov_copy[0].iov_base = &hdr; |
ef4252b1 | 128 | iov_copy[0].iov_len = s->host_vnet_hdr_len; |
5281d757 MM |
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 | ||
4e68f7a0 | 137 | static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 138 | { |
3e35ba93 | 139 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
140 | struct iovec iov[2]; |
141 | int iovcnt = 0; | |
ef4252b1 | 142 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
5281d757 | 143 | |
ef4252b1 | 144 | if (s->host_vnet_hdr_len) { |
5281d757 | 145 | iov[iovcnt].iov_base = &hdr; |
ef4252b1 | 146 | iov[iovcnt].iov_len = s->host_vnet_hdr_len; |
5281d757 MM |
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 | ||
4e68f7a0 | 157 | static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 158 | { |
3e35ba93 | 159 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
160 | struct iovec iov[1]; |
161 | ||
ef4252b1 | 162 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
3e35ba93 | 163 | return tap_receive_raw(nc, buf, size); |
5281d757 MM |
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 | ||
966ea5ec MM |
172 | #ifndef __sun__ |
173 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
5281d757 MM |
174 | { |
175 | return read(tapfd, buf, maxlen); | |
176 | } | |
177 | #endif | |
178 | ||
4e68f7a0 | 179 | static void tap_send_completed(NetClientState *nc, ssize_t len) |
5281d757 | 180 | { |
3e35ba93 | 181 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
ec45f083 | 182 | tap_read_poll(s, true); |
5281d757 MM |
183 | } |
184 | ||
185 | static void tap_send(void *opaque) | |
186 | { | |
187 | TAPState *s = opaque; | |
188 | int size; | |
756ae78b | 189 | int packets = 0; |
5281d757 | 190 | |
a90a7425 | 191 | while (true) { |
5819c918 | 192 | uint8_t *buf = s->buf; |
969e50b6 BM |
193 | uint8_t min_pkt[ETH_ZLEN]; |
194 | size_t min_pktsz = sizeof(min_pkt); | |
5819c918 MM |
195 | |
196 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
197 | if (size <= 0) { | |
198 | break; | |
199 | } | |
200 | ||
ef4252b1 MT |
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; | |
5819c918 MM |
204 | } |
205 | ||
bc38e31b | 206 | if (net_peer_needs_padding(&s->nc)) { |
969e50b6 BM |
207 | if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { |
208 | buf = min_pkt; | |
209 | size = min_pktsz; | |
210 | } | |
211 | } | |
212 | ||
3e35ba93 | 213 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
5819c918 | 214 | if (size == 0) { |
ec45f083 | 215 | tap_read_poll(s, false); |
68e5ec64 SH |
216 | break; |
217 | } else if (size < 0) { | |
218 | break; | |
5819c918 | 219 | } |
756ae78b WK |
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 | } | |
68e5ec64 | 231 | } |
5281d757 MM |
232 | } |
233 | ||
3bac80d3 | 234 | static bool tap_has_ufo(NetClientState *nc) |
5281d757 | 235 | { |
3e35ba93 | 236 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 237 | |
f394b2e2 | 238 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
5281d757 MM |
239 | |
240 | return s->has_ufo; | |
241 | } | |
242 | ||
3bac80d3 | 243 | static bool tap_has_vnet_hdr(NetClientState *nc) |
5281d757 | 244 | { |
3e35ba93 | 245 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 246 | |
f394b2e2 | 247 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
5281d757 | 248 | |
ef4252b1 | 249 | return !!s->host_vnet_hdr_len; |
5281d757 MM |
250 | } |
251 | ||
3bac80d3 | 252 | static bool tap_has_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
253 | { |
254 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
255 | ||
f394b2e2 | 256 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
445d892f | 257 | |
e96dfd11 | 258 | return !!tap_probe_vnet_hdr_len(s->fd, len); |
445d892f MT |
259 | } |
260 | ||
3bac80d3 | 261 | static void tap_set_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
262 | { |
263 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
264 | ||
f394b2e2 | 265 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
445d892f | 266 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
fbbdbdde YB |
267 | len == sizeof(struct virtio_net_hdr) || |
268 | len == sizeof(struct virtio_net_hdr_v1_hash)); | |
445d892f MT |
269 | |
270 | tap_fd_set_vnet_hdr_len(s->fd, len); | |
271 | s->host_vnet_hdr_len = len; | |
272 | } | |
273 | ||
3bac80d3 | 274 | static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr) |
5281d757 | 275 | { |
3e35ba93 | 276 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 277 | |
f394b2e2 | 278 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
ef4252b1 | 279 | assert(!!s->host_vnet_hdr_len == using_vnet_hdr); |
5281d757 MM |
280 | |
281 | s->using_vnet_hdr = using_vnet_hdr; | |
282 | } | |
283 | ||
c80cd6bb GK |
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 | ||
3bac80d3 | 298 | static void tap_set_offload(NetClientState *nc, int csum, int tso4, |
5281d757 MM |
299 | int tso6, int ecn, int ufo) |
300 | { | |
3e35ba93 | 301 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
27a6375d MT |
302 | if (s->fd < 0) { |
303 | return; | |
304 | } | |
5281d757 | 305 | |
27a6375d | 306 | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
5281d757 MM |
307 | } |
308 | ||
9e32ff32 MAL |
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 | ||
4e68f7a0 | 322 | static void tap_cleanup(NetClientState *nc) |
5281d757 | 323 | { |
3e35ba93 | 324 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 325 | |
82b0d80e MT |
326 | if (s->vhost_net) { |
327 | vhost_net_cleanup(s->vhost_net); | |
e6bcb1b6 | 328 | g_free(s->vhost_net); |
43849424 | 329 | s->vhost_net = NULL; |
82b0d80e MT |
330 | } |
331 | ||
3e35ba93 | 332 | qemu_purge_queued_packets(nc); |
5281d757 | 333 | |
9e32ff32 MAL |
334 | tap_exit_notify(&s->exit, NULL); |
335 | qemu_remove_exit_notifier(&s->exit); | |
5281d757 | 336 | |
ec45f083 JW |
337 | tap_read_poll(s, false); |
338 | tap_write_poll(s, false); | |
5281d757 | 339 | close(s->fd); |
27a6375d | 340 | s->fd = -1; |
5281d757 MM |
341 | } |
342 | ||
4e68f7a0 | 343 | static void tap_poll(NetClientState *nc, bool enable) |
ceb69615 MT |
344 | { |
345 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
346 | tap_read_poll(s, enable); | |
347 | tap_write_poll(s, enable); | |
348 | } | |
349 | ||
8f364e34 AM |
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 | ||
4e68f7a0 | 358 | int tap_get_fd(NetClientState *nc) |
95d528a2 MT |
359 | { |
360 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
f394b2e2 | 361 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
95d528a2 MT |
362 | return s->fd; |
363 | } | |
364 | ||
5281d757 MM |
365 | /* fd support */ |
366 | ||
3e35ba93 | 367 | static NetClientInfo net_tap_info = { |
f394b2e2 | 368 | .type = NET_CLIENT_DRIVER_TAP, |
3e35ba93 MM |
369 | .size = sizeof(TAPState), |
370 | .receive = tap_receive, | |
371 | .receive_raw = tap_receive_raw, | |
372 | .receive_iov = tap_receive_iov, | |
ceb69615 | 373 | .poll = tap_poll, |
3e35ba93 | 374 | .cleanup = tap_cleanup, |
2e753bcc VM |
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, | |
c80cd6bb GK |
381 | .set_vnet_le = tap_set_vnet_le, |
382 | .set_vnet_be = tap_set_vnet_be, | |
8f364e34 | 383 | .set_steering_ebpf = tap_set_steering_ebpf, |
3e35ba93 MM |
384 | }; |
385 | ||
4e68f7a0 | 386 | static TAPState *net_tap_fd_init(NetClientState *peer, |
5281d757 MM |
387 | const char *model, |
388 | const char *name, | |
389 | int fd, | |
390 | int vnet_hdr) | |
391 | { | |
4e68f7a0 | 392 | NetClientState *nc; |
5281d757 | 393 | TAPState *s; |
5281d757 | 394 | |
ab5f3f84 | 395 | nc = qemu_new_net_client(&net_tap_info, peer, model, name); |
3e35ba93 MM |
396 | |
397 | s = DO_UPCAST(TAPState, nc, nc); | |
398 | ||
5281d757 | 399 | s->fd = fd; |
ef4252b1 | 400 | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
ec45f083 | 401 | s->using_vnet_hdr = false; |
9c282718 | 402 | s->has_ufo = tap_probe_has_ufo(s->fd); |
16dbaf90 | 403 | s->enabled = true; |
3e35ba93 | 404 | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
58ddcd50 MT |
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 | } | |
ec45f083 | 412 | tap_read_poll(s, true); |
82b0d80e | 413 | s->vhost_net = NULL; |
9e32ff32 MAL |
414 | |
415 | s->exit.notify = tap_exit_notify; | |
416 | qemu_add_exit_notifier(&s->exit); | |
417 | ||
5281d757 MM |
418 | return s; |
419 | } | |
420 | ||
ac4fcf56 MA |
421 | static void launch_script(const char *setup_script, const char *ifname, |
422 | int fd, Error **errp) | |
5281d757 | 423 | { |
5281d757 MM |
424 | int pid, status; |
425 | char *args[3]; | |
426 | char **parg; | |
427 | ||
5281d757 MM |
428 | /* try to launch network script */ |
429 | pid = fork(); | |
ac4fcf56 MA |
430 | if (pid < 0) { |
431 | error_setg_errno(errp, errno, "could not launch network script %s", | |
432 | setup_script); | |
433 | return; | |
434 | } | |
5281d757 MM |
435 | if (pid == 0) { |
436 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
437 | ||
13a12f86 PG |
438 | for (i = 3; i < open_max; i++) { |
439 | if (i != fd) { | |
5281d757 MM |
440 | close(i); |
441 | } | |
442 | } | |
443 | parg = args; | |
444 | *parg++ = (char *)setup_script; | |
445 | *parg++ = (char *)ifname; | |
9678d950 | 446 | *parg = NULL; |
5281d757 MM |
447 | execv(setup_script, args); |
448 | _exit(1); | |
ac4fcf56 | 449 | } else { |
5281d757 MM |
450 | while (waitpid(pid, &status, 0) != pid) { |
451 | /* loop */ | |
452 | } | |
5281d757 MM |
453 | |
454 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
ac4fcf56 | 455 | return; |
5281d757 | 456 | } |
ac4fcf56 MA |
457 | error_setg(errp, "network script %s failed with status %d", |
458 | setup_script, status); | |
5281d757 | 459 | } |
5281d757 MM |
460 | } |
461 | ||
a7c36ee4 CB |
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 | ||
a8a21be9 MA |
496 | static int net_bridge_run_helper(const char *helper, const char *bridge, |
497 | Error **errp) | |
a7c36ee4 CB |
498 | { |
499 | sigset_t oldmask, mask; | |
63c4db4c | 500 | g_autofree char *default_helper = NULL; |
a7c36ee4 CB |
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 | ||
63c4db4c PB |
510 | if (!helper) { |
511 | helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER); | |
512 | } | |
513 | ||
a7c36ee4 | 514 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
a8a21be9 | 515 | error_setg_errno(errp, errno, "socketpair() failed"); |
a7c36ee4 CB |
516 | return -1; |
517 | } | |
518 | ||
519 | /* try to launch bridge helper */ | |
520 | pid = fork(); | |
a8a21be9 MA |
521 | if (pid < 0) { |
522 | error_setg_errno(errp, errno, "Can't fork bridge helper"); | |
523 | return -1; | |
524 | } | |
a7c36ee4 CB |
525 | if (pid == 0) { |
526 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
389abe1d PP |
527 | char *fd_buf = NULL; |
528 | char *br_buf = NULL; | |
529 | char *helper_cmd = NULL; | |
a7c36ee4 | 530 | |
13a12f86 PG |
531 | for (i = 3; i < open_max; i++) { |
532 | if (i != sv[1]) { | |
a7c36ee4 CB |
533 | close(i); |
534 | } | |
535 | } | |
536 | ||
389abe1d | 537 | fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]); |
a7c36ee4 CB |
538 | |
539 | if (strrchr(helper, ' ') || strrchr(helper, '\t')) { | |
540 | /* assume helper is a command */ | |
541 | ||
542 | if (strstr(helper, "--br=") == NULL) { | |
389abe1d | 543 | br_buf = g_strdup_printf("%s%s", "--br=", bridge); |
a7c36ee4 CB |
544 | } |
545 | ||
389abe1d PP |
546 | helper_cmd = g_strdup_printf("%s %s %s %s", helper, |
547 | "--use-vnet", fd_buf, br_buf ? br_buf : ""); | |
a7c36ee4 CB |
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); | |
389abe1d | 556 | g_free(helper_cmd); |
a7c36ee4 CB |
557 | } else { |
558 | /* assume helper is just the executable path name */ | |
559 | ||
389abe1d | 560 | br_buf = g_strdup_printf("%s%s", "--br=", bridge); |
a7c36ee4 CB |
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 | } | |
389abe1d PP |
571 | g_free(fd_buf); |
572 | g_free(br_buf); | |
a7c36ee4 CB |
573 | _exit(1); |
574 | ||
a8a21be9 | 575 | } else { |
a7c36ee4 | 576 | int fd; |
a8a21be9 | 577 | int saved_errno; |
a7c36ee4 CB |
578 | |
579 | close(sv[1]); | |
580 | ||
581 | do { | |
582 | fd = recv_fd(sv[0]); | |
583 | } while (fd == -1 && errno == EINTR); | |
a8a21be9 | 584 | saved_errno = errno; |
a7c36ee4 CB |
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) { | |
a8a21be9 MA |
593 | error_setg_errno(errp, saved_errno, |
594 | "failed to recv file descriptor"); | |
a7c36ee4 CB |
595 | return -1; |
596 | } | |
a8a21be9 MA |
597 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
598 | error_setg(errp, "bridge helper failed"); | |
599 | return -1; | |
a7c36ee4 | 600 | } |
a8a21be9 | 601 | return fd; |
a7c36ee4 | 602 | } |
a7c36ee4 CB |
603 | } |
604 | ||
cebea510 | 605 | int net_init_bridge(const Netdev *netdev, const char *name, |
a30ecde6 | 606 | NetClientState *peer, Error **errp) |
a7c36ee4 | 607 | { |
f79b51b0 LE |
608 | const NetdevBridgeOptions *bridge; |
609 | const char *helper, *br; | |
a7c36ee4 CB |
610 | TAPState *s; |
611 | int fd, vnet_hdr; | |
612 | ||
f394b2e2 EB |
613 | assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE); |
614 | bridge = &netdev->u.bridge; | |
63c4db4c | 615 | helper = bridge->has_helper ? bridge->helper : NULL; |
f79b51b0 | 616 | br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE; |
a7c36ee4 | 617 | |
a8a21be9 | 618 | fd = net_bridge_run_helper(helper, br, errp); |
a7c36ee4 CB |
619 | if (fd == -1) { |
620 | return -1; | |
621 | } | |
622 | ||
ab79237a | 623 | qemu_set_nonblock(fd); |
e7b347d0 DB |
624 | vnet_hdr = tap_probe_vnet_hdr(fd, errp); |
625 | if (vnet_hdr < 0) { | |
626 | close(fd); | |
627 | return -1; | |
628 | } | |
d33d93b2 | 629 | s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); |
a7c36ee4 | 630 | |
56e6f594 JW |
631 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper, |
632 | br); | |
d89b4f83 | 633 | |
a7c36ee4 CB |
634 | return 0; |
635 | } | |
636 | ||
08c573a8 LE |
637 | static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, |
638 | const char *setup_script, char *ifname, | |
468dd824 | 639 | size_t ifname_sz, int mq_required, Error **errp) |
5281d757 | 640 | { |
ac4fcf56 | 641 | Error *err = NULL; |
5281d757 | 642 | int fd, vnet_hdr_required; |
5281d757 | 643 | |
08c573a8 LE |
644 | if (tap->has_vnet_hdr) { |
645 | *vnet_hdr = tap->vnet_hdr; | |
5281d757 MM |
646 | vnet_hdr_required = *vnet_hdr; |
647 | } else { | |
08c573a8 | 648 | *vnet_hdr = 1; |
5281d757 MM |
649 | vnet_hdr_required = 0; |
650 | } | |
651 | ||
264986e2 | 652 | TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required, |
468dd824 | 653 | mq_required, errp)); |
5281d757 MM |
654 | if (fd < 0) { |
655 | return -1; | |
656 | } | |
657 | ||
5281d757 MM |
658 | if (setup_script && |
659 | setup_script[0] != '\0' && | |
ac4fcf56 MA |
660 | strcmp(setup_script, "no") != 0) { |
661 | launch_script(setup_script, ifname, fd, &err); | |
662 | if (err) { | |
468dd824 | 663 | error_propagate(errp, err); |
ac4fcf56 MA |
664 | close(fd); |
665 | return -1; | |
666 | } | |
5281d757 MM |
667 | } |
668 | ||
5281d757 MM |
669 | return fd; |
670 | } | |
671 | ||
264986e2 JW |
672 | #define MAX_TAP_QUEUES 1024 |
673 | ||
445f116c MA |
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, | |
f9bb0c1f | 678 | int vnet_hdr, int fd, Error **errp) |
5193e5fb | 679 | { |
1677f4c6 | 680 | Error *err = NULL; |
da4a4eac | 681 | TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); |
81647a65 | 682 | int vhostfd; |
5193e5fb | 683 | |
80b832c3 MA |
684 | tap_set_sndbuf(s->fd, tap, &err); |
685 | if (err) { | |
445f116c MA |
686 | error_propagate(errp, err); |
687 | return; | |
5193e5fb JW |
688 | } |
689 | ||
264986e2 | 690 | if (tap->has_fd || tap->has_fds) { |
56e6f594 | 691 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
5193e5fb | 692 | } else if (tap->has_helper) { |
56e6f594 JW |
693 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s", |
694 | tap->helper); | |
5193e5fb | 695 | } else { |
56e6f594 JW |
696 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
697 | "ifname=%s,script=%s,downscript=%s", ifname, script, | |
698 | downscript); | |
d89b4f83 | 699 | |
5193e5fb JW |
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)) { | |
81647a65 NN |
709 | VhostNetOptions options; |
710 | ||
1a1bfac9 | 711 | options.backend_type = VHOST_BACKEND_TYPE_KERNEL; |
81647a65 | 712 | options.net_backend = &s->nc; |
69e87b32 JW |
713 | if (tap->has_poll_us) { |
714 | options.busyloop_timeout = tap->poll_us; | |
715 | } else { | |
716 | options.busyloop_timeout = 0; | |
717 | } | |
5193e5fb | 718 | |
3a2d44f6 | 719 | if (vhostfdname) { |
894022e6 LV |
720 | int ret; |
721 | ||
947e4744 | 722 | vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err); |
5193e5fb | 723 | if (vhostfd == -1) { |
46d4d36d JZ |
724 | if (tap->has_vhostforce && tap->vhostforce) { |
725 | error_propagate(errp, err); | |
726 | } else { | |
727 | warn_report_err(err); | |
728 | } | |
445f116c | 729 | return; |
5193e5fb | 730 | } |
894022e6 LV |
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 | } | |
5193e5fb | 737 | } else { |
81647a65 NN |
738 | vhostfd = open("/dev/vhost-net", O_RDWR); |
739 | if (vhostfd < 0) { | |
46d4d36d JZ |
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 | } | |
445f116c | 747 | return; |
81647a65 | 748 | } |
ab79237a | 749 | qemu_set_nonblock(vhostfd); |
5193e5fb | 750 | } |
81647a65 | 751 | options.opaque = (void *)(uintptr_t)vhostfd; |
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 | } | |
445f116c | 760 | return; |
5193e5fb | 761 | } |
3a2d44f6 | 762 | } else if (vhostfdname) { |
69e87b32 | 763 | error_setg(errp, "vhostfd(s)= is not valid without vhost"); |
5193e5fb | 764 | } |
5193e5fb JW |
765 | } |
766 | ||
264986e2 JW |
767 | static int get_fds(char *str, char *fds[], int max) |
768 | { | |
769 | char *ptr = str, *this; | |
770 | size_t len = strlen(str); | |
771 | int i = 0; | |
772 | ||
773 | while (i < max && ptr < str + len) { | |
774 | this = strchr(ptr, ':'); | |
775 | ||
776 | if (this == NULL) { | |
777 | fds[i] = g_strdup(ptr); | |
778 | } else { | |
779 | fds[i] = g_strndup(ptr, this - ptr); | |
780 | } | |
781 | ||
782 | i++; | |
783 | if (this == NULL) { | |
784 | break; | |
785 | } else { | |
786 | ptr = this + 1; | |
787 | } | |
788 | } | |
789 | ||
790 | return i; | |
791 | } | |
792 | ||
cebea510 | 793 | int net_init_tap(const Netdev *netdev, const char *name, |
a30ecde6 | 794 | NetClientState *peer, Error **errp) |
5281d757 | 795 | { |
08c573a8 | 796 | const NetdevTapOptions *tap; |
264986e2 | 797 | int fd, vnet_hdr = 0, i = 0, queues; |
08c573a8 | 798 | /* for the no-fd, no-helper case */ |
63c4db4c PB |
799 | const char *script; |
800 | const char *downscript; | |
1677f4c6 | 801 | Error *err = NULL; |
264986e2 | 802 | const char *vhostfdname; |
08c573a8 | 803 | char ifname[128]; |
894022e6 | 804 | int ret = 0; |
08c573a8 | 805 | |
f394b2e2 EB |
806 | assert(netdev->type == NET_CLIENT_DRIVER_TAP); |
807 | tap = &netdev->u.tap; | |
264986e2 JW |
808 | queues = tap->has_queues ? tap->queues : 1; |
809 | vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL; | |
63c4db4c PB |
810 | script = tap->has_script ? tap->script : NULL; |
811 | downscript = tap->has_downscript ? tap->downscript : NULL; | |
5281d757 | 812 | |
442da403 | 813 | /* QEMU hubs do not support multiqueue tap, in this case peer is set. |
ce675a75 JW |
814 | * For -netdev, peer is always NULL. */ |
815 | if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) { | |
442da403 | 816 | error_setg(errp, "Multiqueue tap cannot be used with hubs"); |
ce675a75 JW |
817 | return -1; |
818 | } | |
819 | ||
08c573a8 LE |
820 | if (tap->has_fd) { |
821 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
264986e2 | 822 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || |
c87826a8 | 823 | tap->has_fds || tap->has_vhostfds) { |
a3088177 MA |
824 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
825 | "helper=, queues=, fds=, and vhostfds= " | |
826 | "are invalid with fd="); | |
5281d757 MM |
827 | return -1; |
828 | } | |
829 | ||
947e4744 | 830 | fd = monitor_fd_param(monitor_cur(), tap->fd, errp); |
5281d757 MM |
831 | if (fd == -1) { |
832 | return -1; | |
833 | } | |
834 | ||
894022e6 LV |
835 | ret = qemu_try_set_nonblock(fd); |
836 | if (ret < 0) { | |
837 | error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d", | |
838 | name, fd); | |
f012bec8 | 839 | close(fd); |
894022e6 LV |
840 | return -1; |
841 | } | |
5281d757 | 842 | |
e7b347d0 DB |
843 | vnet_hdr = tap_probe_vnet_hdr(fd, errp); |
844 | if (vnet_hdr < 0) { | |
845 | close(fd); | |
846 | return -1; | |
847 | } | |
a7c36ee4 | 848 | |
445f116c MA |
849 | net_init_tap_one(tap, peer, "tap", name, NULL, |
850 | script, downscript, | |
f9bb0c1f | 851 | vhostfdname, vnet_hdr, fd, &err); |
445f116c | 852 | if (err) { |
a3088177 | 853 | error_propagate(errp, err); |
f012bec8 | 854 | close(fd); |
264986e2 JW |
855 | return -1; |
856 | } | |
857 | } else if (tap->has_fds) { | |
fac7d7b1 PM |
858 | char **fds; |
859 | char **vhost_fds; | |
323e7c11 | 860 | int nfds = 0, nvhosts = 0; |
264986e2 JW |
861 | |
862 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
863 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || | |
c87826a8 | 864 | tap->has_vhostfd) { |
a3088177 MA |
865 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
866 | "helper=, queues=, and vhostfd= " | |
867 | "are invalid with fds="); | |
264986e2 JW |
868 | return -1; |
869 | } | |
870 | ||
fac7d7b1 PM |
871 | fds = g_new0(char *, MAX_TAP_QUEUES); |
872 | vhost_fds = g_new0(char *, MAX_TAP_QUEUES); | |
873 | ||
264986e2 JW |
874 | nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES); |
875 | if (tap->has_vhostfds) { | |
876 | nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES); | |
877 | if (nfds != nvhosts) { | |
a3088177 MA |
878 | error_setg(errp, "The number of fds passed does not match " |
879 | "the number of vhostfds passed"); | |
323e7c11 | 880 | ret = -1; |
091a6b2a | 881 | goto free_fail; |
264986e2 JW |
882 | } |
883 | } | |
884 | ||
885 | for (i = 0; i < nfds; i++) { | |
947e4744 | 886 | fd = monitor_fd_param(monitor_cur(), fds[i], errp); |
264986e2 | 887 | if (fd == -1) { |
323e7c11 | 888 | ret = -1; |
091a6b2a | 889 | goto free_fail; |
264986e2 JW |
890 | } |
891 | ||
894022e6 LV |
892 | ret = qemu_try_set_nonblock(fd); |
893 | if (ret < 0) { | |
894 | error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d", | |
895 | name, fd); | |
896 | goto free_fail; | |
897 | } | |
a7c36ee4 | 898 | |
264986e2 | 899 | if (i == 0) { |
e7b347d0 DB |
900 | vnet_hdr = tap_probe_vnet_hdr(fd, errp); |
901 | if (vnet_hdr < 0) { | |
902 | goto free_fail; | |
903 | } | |
904 | } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) { | |
a3088177 MA |
905 | error_setg(errp, |
906 | "vnet_hdr not consistent across given tap fds"); | |
323e7c11 | 907 | ret = -1; |
091a6b2a | 908 | goto free_fail; |
264986e2 JW |
909 | } |
910 | ||
445f116c MA |
911 | net_init_tap_one(tap, peer, "tap", name, ifname, |
912 | script, downscript, | |
913 | tap->has_vhostfds ? vhost_fds[i] : NULL, | |
f9bb0c1f | 914 | vnet_hdr, fd, &err); |
445f116c | 915 | if (err) { |
a3088177 | 916 | error_propagate(errp, err); |
323e7c11 | 917 | ret = -1; |
091a6b2a | 918 | goto free_fail; |
264986e2 JW |
919 | } |
920 | } | |
091a6b2a PB |
921 | |
922 | free_fail: | |
323e7c11 YW |
923 | for (i = 0; i < nvhosts; i++) { |
924 | g_free(vhost_fds[i]); | |
925 | } | |
091a6b2a PB |
926 | for (i = 0; i < nfds; i++) { |
927 | g_free(fds[i]); | |
091a6b2a PB |
928 | } |
929 | g_free(fds); | |
930 | g_free(vhost_fds); | |
323e7c11 | 931 | return ret; |
08c573a8 LE |
932 | } else if (tap->has_helper) { |
933 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
c87826a8 | 934 | tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) { |
a3088177 MA |
935 | error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " |
936 | "queues=, and vhostfds= are invalid with helper="); | |
a7c36ee4 CB |
937 | return -1; |
938 | } | |
939 | ||
584613ea AK |
940 | fd = net_bridge_run_helper(tap->helper, |
941 | tap->has_br ? | |
942 | tap->br : DEFAULT_BRIDGE_INTERFACE, | |
a8a21be9 | 943 | errp); |
a7c36ee4 CB |
944 | if (fd == -1) { |
945 | return -1; | |
946 | } | |
947 | ||
ab79237a | 948 | qemu_set_nonblock(fd); |
e7b347d0 DB |
949 | vnet_hdr = tap_probe_vnet_hdr(fd, errp); |
950 | if (vnet_hdr < 0) { | |
951 | close(fd); | |
952 | return -1; | |
953 | } | |
a7c36ee4 | 954 | |
445f116c MA |
955 | net_init_tap_one(tap, peer, "bridge", name, ifname, |
956 | script, downscript, vhostfdname, | |
f9bb0c1f | 957 | vnet_hdr, fd, &err); |
445f116c | 958 | if (err) { |
a3088177 | 959 | error_propagate(errp, err); |
84f8f3da | 960 | close(fd); |
264986e2 JW |
961 | return -1; |
962 | } | |
5281d757 | 963 | } else { |
63c4db4c PB |
964 | g_autofree char *default_script = NULL; |
965 | g_autofree char *default_downscript = NULL; | |
c87826a8 | 966 | if (tap->has_vhostfds) { |
a3088177 | 967 | error_setg(errp, "vhostfds= is invalid if fds= wasn't specified"); |
c87826a8 JW |
968 | return -1; |
969 | } | |
63c4db4c PB |
970 | |
971 | if (!script) { | |
972 | script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT); | |
973 | } | |
974 | if (!downscript) { | |
9925990d KZ |
975 | downscript = default_downscript = |
976 | get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT); | |
63c4db4c | 977 | } |
264986e2 JW |
978 | |
979 | if (tap->has_ifname) { | |
980 | pstrcpy(ifname, sizeof ifname, tap->ifname); | |
981 | } else { | |
982 | ifname[0] = '\0'; | |
929fe497 | 983 | } |
a7c36ee4 | 984 | |
264986e2 JW |
985 | for (i = 0; i < queues; i++) { |
986 | fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, | |
a3088177 | 987 | ifname, sizeof ifname, queues > 1, errp); |
264986e2 JW |
988 | if (fd == -1) { |
989 | return -1; | |
990 | } | |
991 | ||
992 | if (queues > 1 && i == 0 && !tap->has_ifname) { | |
993 | if (tap_fd_get_ifname(fd, ifname)) { | |
a3088177 | 994 | error_setg(errp, "Fail to get ifname"); |
84f8f3da | 995 | close(fd); |
264986e2 JW |
996 | return -1; |
997 | } | |
998 | } | |
999 | ||
445f116c MA |
1000 | net_init_tap_one(tap, peer, "tap", name, ifname, |
1001 | i >= 1 ? "no" : script, | |
1002 | i >= 1 ? "no" : downscript, | |
f9bb0c1f | 1003 | vhostfdname, vnet_hdr, fd, &err); |
445f116c | 1004 | if (err) { |
a3088177 | 1005 | error_propagate(errp, err); |
84f8f3da | 1006 | close(fd); |
264986e2 JW |
1007 | return -1; |
1008 | } | |
1009 | } | |
5281d757 MM |
1010 | } |
1011 | ||
264986e2 | 1012 | return 0; |
5281d757 | 1013 | } |
b202554c | 1014 | |
4e68f7a0 | 1015 | VHostNetState *tap_get_vhost_net(NetClientState *nc) |
b202554c MT |
1016 | { |
1017 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
f394b2e2 | 1018 | assert(nc->info->type == NET_CLIENT_DRIVER_TAP); |
b202554c MT |
1019 | return s->vhost_net; |
1020 | } | |
16dbaf90 JW |
1021 | |
1022 | int tap_enable(NetClientState *nc) | |
1023 | { | |
1024 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
1025 | int ret; | |
1026 | ||
1027 | if (s->enabled) { | |
1028 | return 0; | |
1029 | } else { | |
1030 | ret = tap_fd_enable(s->fd); | |
1031 | if (ret == 0) { | |
1032 | s->enabled = true; | |
1033 | tap_update_fd_handler(s); | |
1034 | } | |
1035 | return ret; | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | int tap_disable(NetClientState *nc) | |
1040 | { | |
1041 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
1042 | int ret; | |
1043 | ||
1044 | if (s->enabled == 0) { | |
1045 | return 0; | |
1046 | } else { | |
1047 | ret = tap_fd_disable(s->fd); | |
1048 | if (ret == 0) { | |
1049 | qemu_purge_queued_packets(nc); | |
1050 | s->enabled = false; | |
1051 | tap_update_fd_handler(s); | |
1052 | } | |
1053 | return ret; | |
1054 | } | |
1055 | } |