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