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