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