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