]>
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; | |
192 | ||
5819c918 MM |
193 | do { |
194 | uint8_t *buf = s->buf; | |
195 | ||
196 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
197 | if (size <= 0) { | |
198 | break; | |
199 | } | |
200 | ||
ef4252b1 MT |
201 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
202 | buf += s->host_vnet_hdr_len; | |
203 | size -= s->host_vnet_hdr_len; | |
5819c918 MM |
204 | } |
205 | ||
3e35ba93 | 206 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
5819c918 | 207 | if (size == 0) { |
ec45f083 | 208 | tap_read_poll(s, false); |
5819c918 | 209 | } |
3e35ba93 | 210 | } while (size > 0 && qemu_can_send_packet(&s->nc)); |
5281d757 MM |
211 | } |
212 | ||
ec45f083 | 213 | bool tap_has_ufo(NetClientState *nc) |
5281d757 | 214 | { |
3e35ba93 | 215 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 216 | |
2be64a68 | 217 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
5281d757 MM |
218 | |
219 | return s->has_ufo; | |
220 | } | |
221 | ||
4e68f7a0 | 222 | int tap_has_vnet_hdr(NetClientState *nc) |
5281d757 | 223 | { |
3e35ba93 | 224 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 225 | |
2be64a68 | 226 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
5281d757 | 227 | |
ef4252b1 | 228 | return !!s->host_vnet_hdr_len; |
5281d757 MM |
229 | } |
230 | ||
4e68f7a0 | 231 | int tap_has_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
232 | { |
233 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
234 | ||
2be64a68 | 235 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
445d892f MT |
236 | |
237 | return tap_probe_vnet_hdr_len(s->fd, len); | |
238 | } | |
239 | ||
4e68f7a0 | 240 | void tap_set_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
241 | { |
242 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
243 | ||
2be64a68 | 244 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
445d892f MT |
245 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
246 | len == sizeof(struct virtio_net_hdr)); | |
247 | ||
248 | tap_fd_set_vnet_hdr_len(s->fd, len); | |
249 | s->host_vnet_hdr_len = len; | |
250 | } | |
251 | ||
ec45f083 | 252 | void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr) |
5281d757 | 253 | { |
3e35ba93 | 254 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 255 | |
2be64a68 | 256 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
ef4252b1 | 257 | assert(!!s->host_vnet_hdr_len == using_vnet_hdr); |
5281d757 MM |
258 | |
259 | s->using_vnet_hdr = using_vnet_hdr; | |
260 | } | |
261 | ||
4e68f7a0 | 262 | void tap_set_offload(NetClientState *nc, int csum, int tso4, |
5281d757 MM |
263 | int tso6, int ecn, int ufo) |
264 | { | |
3e35ba93 | 265 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
27a6375d MT |
266 | if (s->fd < 0) { |
267 | return; | |
268 | } | |
5281d757 | 269 | |
27a6375d | 270 | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
5281d757 MM |
271 | } |
272 | ||
4e68f7a0 | 273 | static void tap_cleanup(NetClientState *nc) |
5281d757 | 274 | { |
3e35ba93 | 275 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 276 | |
82b0d80e MT |
277 | if (s->vhost_net) { |
278 | vhost_net_cleanup(s->vhost_net); | |
43849424 | 279 | s->vhost_net = NULL; |
82b0d80e MT |
280 | } |
281 | ||
3e35ba93 | 282 | qemu_purge_queued_packets(nc); |
5281d757 MM |
283 | |
284 | if (s->down_script[0]) | |
285 | launch_script(s->down_script, s->down_script_arg, s->fd); | |
286 | ||
ec45f083 JW |
287 | tap_read_poll(s, false); |
288 | tap_write_poll(s, false); | |
5281d757 | 289 | close(s->fd); |
27a6375d | 290 | s->fd = -1; |
5281d757 MM |
291 | } |
292 | ||
4e68f7a0 | 293 | static void tap_poll(NetClientState *nc, bool enable) |
ceb69615 MT |
294 | { |
295 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
296 | tap_read_poll(s, enable); | |
297 | tap_write_poll(s, enable); | |
298 | } | |
299 | ||
4e68f7a0 | 300 | int tap_get_fd(NetClientState *nc) |
95d528a2 MT |
301 | { |
302 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
2be64a68 | 303 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
95d528a2 MT |
304 | return s->fd; |
305 | } | |
306 | ||
5281d757 MM |
307 | /* fd support */ |
308 | ||
3e35ba93 | 309 | static NetClientInfo net_tap_info = { |
2be64a68 | 310 | .type = NET_CLIENT_OPTIONS_KIND_TAP, |
3e35ba93 MM |
311 | .size = sizeof(TAPState), |
312 | .receive = tap_receive, | |
313 | .receive_raw = tap_receive_raw, | |
314 | .receive_iov = tap_receive_iov, | |
ceb69615 | 315 | .poll = tap_poll, |
3e35ba93 MM |
316 | .cleanup = tap_cleanup, |
317 | }; | |
318 | ||
4e68f7a0 | 319 | static TAPState *net_tap_fd_init(NetClientState *peer, |
5281d757 MM |
320 | const char *model, |
321 | const char *name, | |
322 | int fd, | |
323 | int vnet_hdr) | |
324 | { | |
4e68f7a0 | 325 | NetClientState *nc; |
5281d757 | 326 | TAPState *s; |
5281d757 | 327 | |
ab5f3f84 | 328 | nc = qemu_new_net_client(&net_tap_info, peer, model, name); |
3e35ba93 MM |
329 | |
330 | s = DO_UPCAST(TAPState, nc, nc); | |
331 | ||
5281d757 | 332 | s->fd = fd; |
ef4252b1 | 333 | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
ec45f083 | 334 | s->using_vnet_hdr = false; |
9c282718 | 335 | s->has_ufo = tap_probe_has_ufo(s->fd); |
16dbaf90 | 336 | s->enabled = true; |
3e35ba93 | 337 | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
58ddcd50 MT |
338 | /* |
339 | * Make sure host header length is set correctly in tap: | |
340 | * it might have been modified by another instance of qemu. | |
341 | */ | |
342 | if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) { | |
343 | tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len); | |
344 | } | |
ec45f083 | 345 | tap_read_poll(s, true); |
82b0d80e | 346 | s->vhost_net = NULL; |
5281d757 MM |
347 | return s; |
348 | } | |
349 | ||
5281d757 MM |
350 | static int launch_script(const char *setup_script, const char *ifname, int fd) |
351 | { | |
5281d757 MM |
352 | int pid, status; |
353 | char *args[3]; | |
354 | char **parg; | |
355 | ||
5281d757 MM |
356 | /* try to launch network script */ |
357 | pid = fork(); | |
358 | if (pid == 0) { | |
359 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
360 | ||
361 | for (i = 0; i < open_max; i++) { | |
362 | if (i != STDIN_FILENO && | |
363 | i != STDOUT_FILENO && | |
364 | i != STDERR_FILENO && | |
365 | i != fd) { | |
366 | close(i); | |
367 | } | |
368 | } | |
369 | parg = args; | |
370 | *parg++ = (char *)setup_script; | |
371 | *parg++ = (char *)ifname; | |
9678d950 | 372 | *parg = NULL; |
5281d757 MM |
373 | execv(setup_script, args); |
374 | _exit(1); | |
375 | } else if (pid > 0) { | |
376 | while (waitpid(pid, &status, 0) != pid) { | |
377 | /* loop */ | |
378 | } | |
5281d757 MM |
379 | |
380 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
381 | return 0; | |
382 | } | |
383 | } | |
384 | fprintf(stderr, "%s: could not launch network script\n", setup_script); | |
385 | return -1; | |
386 | } | |
387 | ||
a7c36ee4 CB |
388 | static int recv_fd(int c) |
389 | { | |
390 | int fd; | |
391 | uint8_t msgbuf[CMSG_SPACE(sizeof(fd))]; | |
392 | struct msghdr msg = { | |
393 | .msg_control = msgbuf, | |
394 | .msg_controllen = sizeof(msgbuf), | |
395 | }; | |
396 | struct cmsghdr *cmsg; | |
397 | struct iovec iov; | |
398 | uint8_t req[1]; | |
399 | ssize_t len; | |
400 | ||
401 | cmsg = CMSG_FIRSTHDR(&msg); | |
402 | cmsg->cmsg_level = SOL_SOCKET; | |
403 | cmsg->cmsg_type = SCM_RIGHTS; | |
404 | cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); | |
405 | msg.msg_controllen = cmsg->cmsg_len; | |
406 | ||
407 | iov.iov_base = req; | |
408 | iov.iov_len = sizeof(req); | |
409 | ||
410 | msg.msg_iov = &iov; | |
411 | msg.msg_iovlen = 1; | |
412 | ||
413 | len = recvmsg(c, &msg, 0); | |
414 | if (len > 0) { | |
415 | memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); | |
416 | return fd; | |
417 | } | |
418 | ||
419 | return len; | |
420 | } | |
421 | ||
422 | static int net_bridge_run_helper(const char *helper, const char *bridge) | |
423 | { | |
424 | sigset_t oldmask, mask; | |
425 | int pid, status; | |
426 | char *args[5]; | |
427 | char **parg; | |
428 | int sv[2]; | |
429 | ||
430 | sigemptyset(&mask); | |
431 | sigaddset(&mask, SIGCHLD); | |
432 | sigprocmask(SIG_BLOCK, &mask, &oldmask); | |
433 | ||
434 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { | |
435 | return -1; | |
436 | } | |
437 | ||
438 | /* try to launch bridge helper */ | |
439 | pid = fork(); | |
440 | if (pid == 0) { | |
441 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
442 | char fd_buf[6+10]; | |
443 | char br_buf[6+IFNAMSIZ] = {0}; | |
444 | char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15]; | |
445 | ||
446 | for (i = 0; i < open_max; i++) { | |
447 | if (i != STDIN_FILENO && | |
448 | i != STDOUT_FILENO && | |
449 | i != STDERR_FILENO && | |
450 | i != sv[1]) { | |
451 | close(i); | |
452 | } | |
453 | } | |
454 | ||
455 | snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]); | |
456 | ||
457 | if (strrchr(helper, ' ') || strrchr(helper, '\t')) { | |
458 | /* assume helper is a command */ | |
459 | ||
460 | if (strstr(helper, "--br=") == NULL) { | |
461 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); | |
462 | } | |
463 | ||
464 | snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s", | |
465 | helper, "--use-vnet", fd_buf, br_buf); | |
466 | ||
467 | parg = args; | |
468 | *parg++ = (char *)"sh"; | |
469 | *parg++ = (char *)"-c"; | |
470 | *parg++ = helper_cmd; | |
471 | *parg++ = NULL; | |
472 | ||
473 | execv("/bin/sh", args); | |
474 | } else { | |
475 | /* assume helper is just the executable path name */ | |
476 | ||
477 | snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge); | |
478 | ||
479 | parg = args; | |
480 | *parg++ = (char *)helper; | |
481 | *parg++ = (char *)"--use-vnet"; | |
482 | *parg++ = fd_buf; | |
483 | *parg++ = br_buf; | |
484 | *parg++ = NULL; | |
485 | ||
486 | execv(helper, args); | |
487 | } | |
488 | _exit(1); | |
489 | ||
490 | } else if (pid > 0) { | |
491 | int fd; | |
492 | ||
493 | close(sv[1]); | |
494 | ||
495 | do { | |
496 | fd = recv_fd(sv[0]); | |
497 | } while (fd == -1 && errno == EINTR); | |
498 | ||
499 | close(sv[0]); | |
500 | ||
501 | while (waitpid(pid, &status, 0) != pid) { | |
502 | /* loop */ | |
503 | } | |
504 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
505 | if (fd < 0) { | |
506 | fprintf(stderr, "failed to recv file descriptor\n"); | |
507 | return -1; | |
508 | } | |
509 | ||
510 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
511 | return fd; | |
512 | } | |
513 | } | |
514 | fprintf(stderr, "failed to launch bridge helper\n"); | |
515 | return -1; | |
516 | } | |
517 | ||
1a0c0958 | 518 | int net_init_bridge(const NetClientOptions *opts, const char *name, |
4e68f7a0 | 519 | NetClientState *peer) |
a7c36ee4 | 520 | { |
f79b51b0 LE |
521 | const NetdevBridgeOptions *bridge; |
522 | const char *helper, *br; | |
523 | ||
a7c36ee4 CB |
524 | TAPState *s; |
525 | int fd, vnet_hdr; | |
526 | ||
f79b51b0 LE |
527 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE); |
528 | bridge = opts->bridge; | |
529 | ||
530 | helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER; | |
531 | br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE; | |
a7c36ee4 | 532 | |
f79b51b0 | 533 | fd = net_bridge_run_helper(helper, br); |
a7c36ee4 CB |
534 | if (fd == -1) { |
535 | return -1; | |
536 | } | |
537 | ||
538 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
539 | ||
540 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
541 | ||
d33d93b2 | 542 | s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); |
a7c36ee4 CB |
543 | if (!s) { |
544 | close(fd); | |
545 | return -1; | |
546 | } | |
547 | ||
f79b51b0 LE |
548 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper, |
549 | br); | |
a7c36ee4 CB |
550 | |
551 | return 0; | |
552 | } | |
553 | ||
08c573a8 LE |
554 | static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, |
555 | const char *setup_script, char *ifname, | |
264986e2 | 556 | size_t ifname_sz, int mq_required) |
5281d757 MM |
557 | { |
558 | int fd, vnet_hdr_required; | |
5281d757 | 559 | |
08c573a8 LE |
560 | if (tap->has_vnet_hdr) { |
561 | *vnet_hdr = tap->vnet_hdr; | |
5281d757 MM |
562 | vnet_hdr_required = *vnet_hdr; |
563 | } else { | |
08c573a8 | 564 | *vnet_hdr = 1; |
5281d757 MM |
565 | vnet_hdr_required = 0; |
566 | } | |
567 | ||
264986e2 JW |
568 | TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required, |
569 | mq_required)); | |
5281d757 MM |
570 | if (fd < 0) { |
571 | return -1; | |
572 | } | |
573 | ||
5281d757 MM |
574 | if (setup_script && |
575 | setup_script[0] != '\0' && | |
576 | strcmp(setup_script, "no") != 0 && | |
577 | launch_script(setup_script, ifname, fd)) { | |
578 | close(fd); | |
579 | return -1; | |
580 | } | |
581 | ||
5281d757 MM |
582 | return fd; |
583 | } | |
584 | ||
264986e2 JW |
585 | #define MAX_TAP_QUEUES 1024 |
586 | ||
5193e5fb JW |
587 | static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, |
588 | const char *model, const char *name, | |
589 | const char *ifname, const char *script, | |
590 | const char *downscript, const char *vhostfdname, | |
591 | int vnet_hdr, int fd) | |
592 | { | |
593 | TAPState *s; | |
594 | ||
595 | s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); | |
596 | if (!s) { | |
597 | close(fd); | |
598 | return -1; | |
599 | } | |
600 | ||
601 | if (tap_set_sndbuf(s->fd, tap) < 0) { | |
602 | return -1; | |
603 | } | |
604 | ||
264986e2 | 605 | if (tap->has_fd || tap->has_fds) { |
5193e5fb JW |
606 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
607 | } else if (tap->has_helper) { | |
608 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s", | |
609 | tap->helper); | |
610 | } else { | |
5193e5fb JW |
611 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
612 | "ifname=%s,script=%s,downscript=%s", ifname, script, | |
613 | downscript); | |
614 | ||
615 | if (strcmp(downscript, "no") != 0) { | |
616 | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); | |
617 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), | |
618 | "%s", ifname); | |
619 | } | |
620 | } | |
621 | ||
622 | if (tap->has_vhost ? tap->vhost : | |
623 | vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { | |
624 | int vhostfd; | |
625 | ||
7873df40 | 626 | if (tap->has_vhostfd || tap->has_vhostfds) { |
5193e5fb JW |
627 | vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname); |
628 | if (vhostfd == -1) { | |
629 | return -1; | |
630 | } | |
631 | } else { | |
632 | vhostfd = -1; | |
633 | } | |
634 | ||
635 | s->vhost_net = vhost_net_init(&s->nc, vhostfd, | |
636 | tap->has_vhostforce && tap->vhostforce); | |
637 | if (!s->vhost_net) { | |
638 | error_report("vhost-net requested but could not be initialized"); | |
639 | return -1; | |
640 | } | |
264986e2 | 641 | } else if (tap->has_vhostfd || tap->has_vhostfds) { |
5193e5fb JW |
642 | error_report("vhostfd= is not valid without vhost"); |
643 | return -1; | |
644 | } | |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
264986e2 JW |
649 | static int get_fds(char *str, char *fds[], int max) |
650 | { | |
651 | char *ptr = str, *this; | |
652 | size_t len = strlen(str); | |
653 | int i = 0; | |
654 | ||
655 | while (i < max && ptr < str + len) { | |
656 | this = strchr(ptr, ':'); | |
657 | ||
658 | if (this == NULL) { | |
659 | fds[i] = g_strdup(ptr); | |
660 | } else { | |
661 | fds[i] = g_strndup(ptr, this - ptr); | |
662 | } | |
663 | ||
664 | i++; | |
665 | if (this == NULL) { | |
666 | break; | |
667 | } else { | |
668 | ptr = this + 1; | |
669 | } | |
670 | } | |
671 | ||
672 | return i; | |
673 | } | |
674 | ||
1a0c0958 | 675 | int net_init_tap(const NetClientOptions *opts, const char *name, |
4e68f7a0 | 676 | NetClientState *peer) |
5281d757 | 677 | { |
08c573a8 | 678 | const NetdevTapOptions *tap; |
264986e2 | 679 | int fd, vnet_hdr = 0, i = 0, queues; |
08c573a8 LE |
680 | /* for the no-fd, no-helper case */ |
681 | const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */ | |
5193e5fb | 682 | const char *downscript = NULL; |
264986e2 | 683 | const char *vhostfdname; |
08c573a8 LE |
684 | char ifname[128]; |
685 | ||
686 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP); | |
687 | tap = opts->tap; | |
264986e2 JW |
688 | queues = tap->has_queues ? tap->queues : 1; |
689 | vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL; | |
5281d757 | 690 | |
ce675a75 JW |
691 | /* QEMU vlans does not support multiqueue tap, in this case peer is set. |
692 | * For -netdev, peer is always NULL. */ | |
693 | if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) { | |
085d8134 | 694 | error_report("Multiqueue tap cannot be used with QEMU vlans"); |
ce675a75 JW |
695 | return -1; |
696 | } | |
697 | ||
08c573a8 LE |
698 | if (tap->has_fd) { |
699 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
264986e2 | 700 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || |
c87826a8 | 701 | tap->has_fds || tap->has_vhostfds) { |
a7c36ee4 | 702 | error_report("ifname=, script=, downscript=, vnet_hdr=, " |
c87826a8 JW |
703 | "helper=, queues=, fds=, and vhostfds= " |
704 | "are invalid with fd="); | |
5281d757 MM |
705 | return -1; |
706 | } | |
707 | ||
a96ed02f | 708 | fd = monitor_handle_fd_param(cur_mon, tap->fd); |
5281d757 MM |
709 | if (fd == -1) { |
710 | return -1; | |
711 | } | |
712 | ||
713 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
714 | ||
715 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
a7c36ee4 | 716 | |
02cd8090 | 717 | if (net_init_tap_one(tap, peer, "tap", name, NULL, |
264986e2 JW |
718 | script, downscript, |
719 | vhostfdname, vnet_hdr, fd)) { | |
720 | return -1; | |
721 | } | |
722 | } else if (tap->has_fds) { | |
723 | char *fds[MAX_TAP_QUEUES]; | |
724 | char *vhost_fds[MAX_TAP_QUEUES]; | |
725 | int nfds, nvhosts; | |
726 | ||
727 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
728 | tap->has_vnet_hdr || tap->has_helper || tap->has_queues || | |
c87826a8 | 729 | tap->has_vhostfd) { |
264986e2 | 730 | error_report("ifname=, script=, downscript=, vnet_hdr=, " |
c87826a8 JW |
731 | "helper=, queues=, and vhostfd= " |
732 | "are invalid with fds="); | |
264986e2 JW |
733 | return -1; |
734 | } | |
735 | ||
736 | nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES); | |
737 | if (tap->has_vhostfds) { | |
738 | nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES); | |
739 | if (nfds != nvhosts) { | |
740 | error_report("The number of fds passed does not match the " | |
741 | "number of vhostfds passed"); | |
742 | return -1; | |
743 | } | |
744 | } | |
745 | ||
746 | for (i = 0; i < nfds; i++) { | |
747 | fd = monitor_handle_fd_param(cur_mon, fds[i]); | |
748 | if (fd == -1) { | |
749 | return -1; | |
750 | } | |
751 | ||
752 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
a7c36ee4 | 753 | |
264986e2 JW |
754 | if (i == 0) { |
755 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
756 | } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) { | |
757 | error_report("vnet_hdr not consistent across given tap fds"); | |
758 | return -1; | |
759 | } | |
760 | ||
761 | if (net_init_tap_one(tap, peer, "tap", name, ifname, | |
762 | script, downscript, | |
763 | tap->has_vhostfds ? vhost_fds[i] : NULL, | |
764 | vnet_hdr, fd)) { | |
765 | return -1; | |
766 | } | |
767 | } | |
08c573a8 LE |
768 | } else if (tap->has_helper) { |
769 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
c87826a8 | 770 | tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) { |
a7c36ee4 | 771 | error_report("ifname=, script=, downscript=, and vnet_hdr= " |
c87826a8 | 772 | "queues=, and vhostfds= are invalid with helper="); |
a7c36ee4 CB |
773 | return -1; |
774 | } | |
775 | ||
08c573a8 | 776 | fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE); |
a7c36ee4 CB |
777 | if (fd == -1) { |
778 | return -1; | |
779 | } | |
780 | ||
781 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
a7c36ee4 CB |
782 | vnet_hdr = tap_probe_vnet_hdr(fd); |
783 | ||
264986e2 JW |
784 | if (net_init_tap_one(tap, peer, "bridge", name, ifname, |
785 | script, downscript, vhostfdname, | |
786 | vnet_hdr, fd)) { | |
787 | return -1; | |
788 | } | |
5281d757 | 789 | } else { |
c87826a8 JW |
790 | if (tap->has_vhostfds) { |
791 | error_report("vhostfds= is invalid if fds= wasn't specified"); | |
792 | return -1; | |
793 | } | |
08c573a8 | 794 | script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT; |
5193e5fb JW |
795 | downscript = tap->has_downscript ? tap->downscript : |
796 | DEFAULT_NETWORK_DOWN_SCRIPT; | |
264986e2 JW |
797 | |
798 | if (tap->has_ifname) { | |
799 | pstrcpy(ifname, sizeof ifname, tap->ifname); | |
800 | } else { | |
801 | ifname[0] = '\0'; | |
929fe497 | 802 | } |
a7c36ee4 | 803 | |
264986e2 JW |
804 | for (i = 0; i < queues; i++) { |
805 | fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, | |
806 | ifname, sizeof ifname, queues > 1); | |
807 | if (fd == -1) { | |
808 | return -1; | |
809 | } | |
810 | ||
811 | if (queues > 1 && i == 0 && !tap->has_ifname) { | |
812 | if (tap_fd_get_ifname(fd, ifname)) { | |
813 | error_report("Fail to get ifname"); | |
814 | return -1; | |
815 | } | |
816 | } | |
817 | ||
818 | if (net_init_tap_one(tap, peer, "tap", name, ifname, | |
819 | i >= 1 ? "no" : script, | |
820 | i >= 1 ? "no" : downscript, | |
821 | vhostfdname, vnet_hdr, fd)) { | |
822 | return -1; | |
823 | } | |
824 | } | |
5281d757 MM |
825 | } |
826 | ||
264986e2 | 827 | return 0; |
5281d757 | 828 | } |
b202554c | 829 | |
4e68f7a0 | 830 | VHostNetState *tap_get_vhost_net(NetClientState *nc) |
b202554c MT |
831 | { |
832 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
2be64a68 | 833 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
b202554c MT |
834 | return s->vhost_net; |
835 | } | |
16dbaf90 JW |
836 | |
837 | int tap_enable(NetClientState *nc) | |
838 | { | |
839 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
840 | int ret; | |
841 | ||
842 | if (s->enabled) { | |
843 | return 0; | |
844 | } else { | |
845 | ret = tap_fd_enable(s->fd); | |
846 | if (ret == 0) { | |
847 | s->enabled = true; | |
848 | tap_update_fd_handler(s); | |
849 | } | |
850 | return ret; | |
851 | } | |
852 | } | |
853 | ||
854 | int tap_disable(NetClientState *nc) | |
855 | { | |
856 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
857 | int ret; | |
858 | ||
859 | if (s->enabled == 0) { | |
860 | return 0; | |
861 | } else { | |
862 | ret = tap_fd_disable(s->fd); | |
863 | if (ret == 0) { | |
864 | qemu_purge_queued_packets(nc); | |
865 | s->enabled = false; | |
866 | tap_update_fd_handler(s); | |
867 | } | |
868 | return ret; | |
869 | } | |
870 | } |