]>
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 | ||
a245fc18 | 26 | #include "tap.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 | ||
36 | #include "net.h" | |
a245fc18 | 37 | #include "clients.h" |
37003adf | 38 | #include "monitor.h" |
5281d757 | 39 | #include "sysemu.h" |
5281d757 | 40 | #include "qemu-common.h" |
2f792016 | 41 | #include "qemu-error.h" |
5281d757 | 42 | |
5281d757 | 43 | #include "net/tap-linux.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]; | |
58 | unsigned int read_poll : 1; | |
59 | unsigned int write_poll : 1; | |
5281d757 MM |
60 | unsigned int using_vnet_hdr : 1; |
61 | unsigned int has_ufo: 1; | |
82b0d80e | 62 | VHostNetState *vhost_net; |
ef4252b1 | 63 | unsigned host_vnet_hdr_len; |
5281d757 MM |
64 | } TAPState; |
65 | ||
66 | static int launch_script(const char *setup_script, const char *ifname, int fd); | |
67 | ||
68 | static int tap_can_send(void *opaque); | |
69 | static void tap_send(void *opaque); | |
70 | static void tap_writable(void *opaque); | |
71 | ||
72 | static void tap_update_fd_handler(TAPState *s) | |
73 | { | |
74 | qemu_set_fd_handler2(s->fd, | |
75 | s->read_poll ? tap_can_send : NULL, | |
76 | s->read_poll ? tap_send : NULL, | |
77 | s->write_poll ? tap_writable : NULL, | |
78 | s); | |
79 | } | |
80 | ||
81 | static void tap_read_poll(TAPState *s, int enable) | |
82 | { | |
83 | s->read_poll = !!enable; | |
84 | tap_update_fd_handler(s); | |
85 | } | |
86 | ||
87 | static void tap_write_poll(TAPState *s, int enable) | |
88 | { | |
89 | s->write_poll = !!enable; | |
90 | tap_update_fd_handler(s); | |
91 | } | |
92 | ||
93 | static void tap_writable(void *opaque) | |
94 | { | |
95 | TAPState *s = opaque; | |
96 | ||
97 | tap_write_poll(s, 0); | |
98 | ||
3e35ba93 | 99 | qemu_flush_queued_packets(&s->nc); |
5281d757 MM |
100 | } |
101 | ||
102 | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) | |
103 | { | |
104 | ssize_t len; | |
105 | ||
106 | do { | |
107 | len = writev(s->fd, iov, iovcnt); | |
108 | } while (len == -1 && errno == EINTR); | |
109 | ||
110 | if (len == -1 && errno == EAGAIN) { | |
111 | tap_write_poll(s, 1); | |
112 | return 0; | |
113 | } | |
114 | ||
115 | return len; | |
116 | } | |
117 | ||
4e68f7a0 | 118 | static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov, |
5281d757 MM |
119 | int iovcnt) |
120 | { | |
3e35ba93 | 121 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
122 | const struct iovec *iovp = iov; |
123 | struct iovec iov_copy[iovcnt + 1]; | |
ef4252b1 | 124 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
5281d757 | 125 | |
ef4252b1 | 126 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
5281d757 | 127 | iov_copy[0].iov_base = &hdr; |
ef4252b1 | 128 | iov_copy[0].iov_len = s->host_vnet_hdr_len; |
5281d757 MM |
129 | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
130 | iovp = iov_copy; | |
131 | iovcnt++; | |
132 | } | |
133 | ||
134 | return tap_write_packet(s, iovp, iovcnt); | |
135 | } | |
136 | ||
4e68f7a0 | 137 | static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 138 | { |
3e35ba93 | 139 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
140 | struct iovec iov[2]; |
141 | int iovcnt = 0; | |
ef4252b1 | 142 | struct virtio_net_hdr_mrg_rxbuf hdr = { }; |
5281d757 | 143 | |
ef4252b1 | 144 | if (s->host_vnet_hdr_len) { |
5281d757 | 145 | iov[iovcnt].iov_base = &hdr; |
ef4252b1 | 146 | iov[iovcnt].iov_len = s->host_vnet_hdr_len; |
5281d757 MM |
147 | iovcnt++; |
148 | } | |
149 | ||
150 | iov[iovcnt].iov_base = (char *)buf; | |
151 | iov[iovcnt].iov_len = size; | |
152 | iovcnt++; | |
153 | ||
154 | return tap_write_packet(s, iov, iovcnt); | |
155 | } | |
156 | ||
4e68f7a0 | 157 | static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 158 | { |
3e35ba93 | 159 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
160 | struct iovec iov[1]; |
161 | ||
ef4252b1 | 162 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
3e35ba93 | 163 | return tap_receive_raw(nc, buf, size); |
5281d757 MM |
164 | } |
165 | ||
166 | iov[0].iov_base = (char *)buf; | |
167 | iov[0].iov_len = size; | |
168 | ||
169 | return tap_write_packet(s, iov, 1); | |
170 | } | |
171 | ||
172 | static int tap_can_send(void *opaque) | |
173 | { | |
174 | TAPState *s = opaque; | |
175 | ||
3e35ba93 | 176 | return qemu_can_send_packet(&s->nc); |
5281d757 MM |
177 | } |
178 | ||
966ea5ec MM |
179 | #ifndef __sun__ |
180 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
5281d757 MM |
181 | { |
182 | return read(tapfd, buf, maxlen); | |
183 | } | |
184 | #endif | |
185 | ||
4e68f7a0 | 186 | static void tap_send_completed(NetClientState *nc, ssize_t len) |
5281d757 | 187 | { |
3e35ba93 | 188 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
189 | tap_read_poll(s, 1); |
190 | } | |
191 | ||
192 | static void tap_send(void *opaque) | |
193 | { | |
194 | TAPState *s = opaque; | |
195 | int size; | |
196 | ||
5819c918 MM |
197 | do { |
198 | uint8_t *buf = s->buf; | |
199 | ||
200 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
201 | if (size <= 0) { | |
202 | break; | |
203 | } | |
204 | ||
ef4252b1 MT |
205 | if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { |
206 | buf += s->host_vnet_hdr_len; | |
207 | size -= s->host_vnet_hdr_len; | |
5819c918 MM |
208 | } |
209 | ||
3e35ba93 | 210 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
5819c918 MM |
211 | if (size == 0) { |
212 | tap_read_poll(s, 0); | |
213 | } | |
3e35ba93 | 214 | } while (size > 0 && qemu_can_send_packet(&s->nc)); |
5281d757 MM |
215 | } |
216 | ||
4e68f7a0 | 217 | int tap_has_ufo(NetClientState *nc) |
5281d757 | 218 | { |
3e35ba93 | 219 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 220 | |
2be64a68 | 221 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
5281d757 MM |
222 | |
223 | return s->has_ufo; | |
224 | } | |
225 | ||
4e68f7a0 | 226 | int tap_has_vnet_hdr(NetClientState *nc) |
5281d757 | 227 | { |
3e35ba93 | 228 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 229 | |
2be64a68 | 230 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
5281d757 | 231 | |
ef4252b1 | 232 | return !!s->host_vnet_hdr_len; |
5281d757 MM |
233 | } |
234 | ||
4e68f7a0 | 235 | int tap_has_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
236 | { |
237 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
238 | ||
2be64a68 | 239 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
445d892f MT |
240 | |
241 | return tap_probe_vnet_hdr_len(s->fd, len); | |
242 | } | |
243 | ||
4e68f7a0 | 244 | void tap_set_vnet_hdr_len(NetClientState *nc, int len) |
445d892f MT |
245 | { |
246 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
247 | ||
2be64a68 | 248 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
445d892f MT |
249 | assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) || |
250 | len == sizeof(struct virtio_net_hdr)); | |
251 | ||
252 | tap_fd_set_vnet_hdr_len(s->fd, len); | |
253 | s->host_vnet_hdr_len = len; | |
254 | } | |
255 | ||
4e68f7a0 | 256 | void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr) |
5281d757 | 257 | { |
3e35ba93 | 258 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
259 | |
260 | using_vnet_hdr = using_vnet_hdr != 0; | |
261 | ||
2be64a68 | 262 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
ef4252b1 | 263 | assert(!!s->host_vnet_hdr_len == using_vnet_hdr); |
5281d757 MM |
264 | |
265 | s->using_vnet_hdr = using_vnet_hdr; | |
266 | } | |
267 | ||
4e68f7a0 | 268 | void tap_set_offload(NetClientState *nc, int csum, int tso4, |
5281d757 MM |
269 | int tso6, int ecn, int ufo) |
270 | { | |
3e35ba93 | 271 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
27a6375d MT |
272 | if (s->fd < 0) { |
273 | return; | |
274 | } | |
5281d757 | 275 | |
27a6375d | 276 | tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
5281d757 MM |
277 | } |
278 | ||
4e68f7a0 | 279 | static void tap_cleanup(NetClientState *nc) |
5281d757 | 280 | { |
3e35ba93 | 281 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 282 | |
82b0d80e MT |
283 | if (s->vhost_net) { |
284 | vhost_net_cleanup(s->vhost_net); | |
43849424 | 285 | s->vhost_net = NULL; |
82b0d80e MT |
286 | } |
287 | ||
3e35ba93 | 288 | qemu_purge_queued_packets(nc); |
5281d757 MM |
289 | |
290 | if (s->down_script[0]) | |
291 | launch_script(s->down_script, s->down_script_arg, s->fd); | |
292 | ||
293 | tap_read_poll(s, 0); | |
294 | tap_write_poll(s, 0); | |
295 | close(s->fd); | |
27a6375d | 296 | s->fd = -1; |
5281d757 MM |
297 | } |
298 | ||
4e68f7a0 | 299 | static void tap_poll(NetClientState *nc, bool enable) |
ceb69615 MT |
300 | { |
301 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
302 | tap_read_poll(s, enable); | |
303 | tap_write_poll(s, enable); | |
304 | } | |
305 | ||
4e68f7a0 | 306 | int tap_get_fd(NetClientState *nc) |
95d528a2 MT |
307 | { |
308 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
2be64a68 | 309 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
95d528a2 MT |
310 | return s->fd; |
311 | } | |
312 | ||
5281d757 MM |
313 | /* fd support */ |
314 | ||
3e35ba93 | 315 | static NetClientInfo net_tap_info = { |
2be64a68 | 316 | .type = NET_CLIENT_OPTIONS_KIND_TAP, |
3e35ba93 MM |
317 | .size = sizeof(TAPState), |
318 | .receive = tap_receive, | |
319 | .receive_raw = tap_receive_raw, | |
320 | .receive_iov = tap_receive_iov, | |
ceb69615 | 321 | .poll = tap_poll, |
3e35ba93 MM |
322 | .cleanup = tap_cleanup, |
323 | }; | |
324 | ||
4e68f7a0 | 325 | static TAPState *net_tap_fd_init(NetClientState *peer, |
5281d757 MM |
326 | const char *model, |
327 | const char *name, | |
328 | int fd, | |
329 | int vnet_hdr) | |
330 | { | |
4e68f7a0 | 331 | NetClientState *nc; |
5281d757 | 332 | TAPState *s; |
5281d757 | 333 | |
ab5f3f84 | 334 | nc = qemu_new_net_client(&net_tap_info, peer, model, name); |
3e35ba93 MM |
335 | |
336 | s = DO_UPCAST(TAPState, nc, nc); | |
337 | ||
5281d757 | 338 | s->fd = fd; |
ef4252b1 | 339 | s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; |
5281d757 | 340 | s->using_vnet_hdr = 0; |
9c282718 | 341 | s->has_ufo = tap_probe_has_ufo(s->fd); |
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 | } | |
5281d757 | 350 | tap_read_poll(s, 1); |
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, | |
561 | size_t ifname_sz) | |
5281d757 MM |
562 | { |
563 | int fd, vnet_hdr_required; | |
5281d757 | 564 | |
08c573a8 LE |
565 | if (tap->has_ifname) { |
566 | pstrcpy(ifname, ifname_sz, tap->ifname); | |
567 | } else { | |
568 | assert(ifname_sz > 0); | |
569 | ifname[0] = '\0'; | |
5281d757 MM |
570 | } |
571 | ||
08c573a8 LE |
572 | if (tap->has_vnet_hdr) { |
573 | *vnet_hdr = tap->vnet_hdr; | |
5281d757 MM |
574 | vnet_hdr_required = *vnet_hdr; |
575 | } else { | |
08c573a8 | 576 | *vnet_hdr = 1; |
5281d757 MM |
577 | vnet_hdr_required = 0; |
578 | } | |
579 | ||
08c573a8 | 580 | TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required)); |
5281d757 MM |
581 | if (fd < 0) { |
582 | return -1; | |
583 | } | |
584 | ||
5281d757 MM |
585 | if (setup_script && |
586 | setup_script[0] != '\0' && | |
587 | strcmp(setup_script, "no") != 0 && | |
588 | launch_script(setup_script, ifname, fd)) { | |
589 | close(fd); | |
590 | return -1; | |
591 | } | |
592 | ||
5281d757 MM |
593 | return fd; |
594 | } | |
595 | ||
1a0c0958 | 596 | int net_init_tap(const NetClientOptions *opts, const char *name, |
4e68f7a0 | 597 | NetClientState *peer) |
5281d757 | 598 | { |
08c573a8 LE |
599 | const NetdevTapOptions *tap; |
600 | ||
df6c2a0f | 601 | int fd, vnet_hdr = 0; |
a7c36ee4 | 602 | const char *model; |
08c573a8 LE |
603 | TAPState *s; |
604 | ||
605 | /* for the no-fd, no-helper case */ | |
606 | const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */ | |
607 | char ifname[128]; | |
608 | ||
609 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP); | |
610 | tap = opts->tap; | |
5281d757 | 611 | |
08c573a8 LE |
612 | if (tap->has_fd) { |
613 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
614 | tap->has_vnet_hdr || tap->has_helper) { | |
a7c36ee4 CB |
615 | error_report("ifname=, script=, downscript=, vnet_hdr=, " |
616 | "and helper= are invalid with fd="); | |
5281d757 MM |
617 | return -1; |
618 | } | |
619 | ||
a96ed02f | 620 | fd = monitor_handle_fd_param(cur_mon, tap->fd); |
5281d757 MM |
621 | if (fd == -1) { |
622 | return -1; | |
623 | } | |
624 | ||
625 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
626 | ||
627 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
a7c36ee4 CB |
628 | |
629 | model = "tap"; | |
630 | ||
08c573a8 LE |
631 | } else if (tap->has_helper) { |
632 | if (tap->has_ifname || tap->has_script || tap->has_downscript || | |
633 | tap->has_vnet_hdr) { | |
a7c36ee4 CB |
634 | error_report("ifname=, script=, downscript=, and vnet_hdr= " |
635 | "are invalid with helper="); | |
636 | return -1; | |
637 | } | |
638 | ||
08c573a8 | 639 | fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE); |
a7c36ee4 CB |
640 | if (fd == -1) { |
641 | return -1; | |
642 | } | |
643 | ||
644 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
645 | ||
646 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
647 | ||
648 | model = "bridge"; | |
649 | ||
5281d757 | 650 | } else { |
08c573a8 LE |
651 | script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT; |
652 | fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname); | |
929fe497 JL |
653 | if (fd == -1) { |
654 | return -1; | |
655 | } | |
a7c36ee4 CB |
656 | |
657 | model = "tap"; | |
5281d757 MM |
658 | } |
659 | ||
d33d93b2 | 660 | s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); |
5281d757 MM |
661 | if (!s) { |
662 | close(fd); | |
663 | return -1; | |
664 | } | |
665 | ||
08c573a8 | 666 | if (tap_set_sndbuf(s->fd, tap) < 0) { |
5281d757 MM |
667 | return -1; |
668 | } | |
669 | ||
08c573a8 | 670 | if (tap->has_fd) { |
3e35ba93 | 671 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
08c573a8 LE |
672 | } else if (tap->has_helper) { |
673 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s", | |
674 | tap->helper); | |
5281d757 | 675 | } else { |
08c573a8 | 676 | const char *downscript; |
5281d757 | 677 | |
08c573a8 LE |
678 | downscript = tap->has_downscript ? tap->downscript : |
679 | DEFAULT_NETWORK_DOWN_SCRIPT; | |
5281d757 | 680 | |
3e35ba93 | 681 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
08c573a8 LE |
682 | "ifname=%s,script=%s,downscript=%s", ifname, script, |
683 | downscript); | |
5281d757 MM |
684 | |
685 | if (strcmp(downscript, "no") != 0) { | |
686 | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); | |
687 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); | |
688 | } | |
689 | } | |
690 | ||
08c573a8 LE |
691 | if (tap->has_vhost ? tap->vhost : |
692 | tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) { | |
693 | int vhostfd; | |
694 | ||
695 | if (tap->has_vhostfd) { | |
a96ed02f | 696 | vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd); |
08c573a8 | 697 | if (vhostfd == -1) { |
82b0d80e MT |
698 | return -1; |
699 | } | |
82b0d80e MT |
700 | } else { |
701 | vhostfd = -1; | |
702 | } | |
08c573a8 LE |
703 | |
704 | s->vhost_net = vhost_net_init(&s->nc, vhostfd, | |
705 | tap->has_vhostforce && tap->vhostforce); | |
82b0d80e MT |
706 | if (!s->vhost_net) { |
707 | error_report("vhost-net requested but could not be initialized"); | |
708 | return -1; | |
709 | } | |
08c573a8 | 710 | } else if (tap->has_vhostfd) { |
82b0d80e MT |
711 | error_report("vhostfd= is not valid without vhost"); |
712 | return -1; | |
713 | } | |
714 | ||
5281d757 MM |
715 | return 0; |
716 | } | |
b202554c | 717 | |
4e68f7a0 | 718 | VHostNetState *tap_get_vhost_net(NetClientState *nc) |
b202554c MT |
719 | { |
720 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
2be64a68 | 721 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP); |
b202554c MT |
722 | return s->vhost_net; |
723 | } |