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