]>
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 | ||
26 | #include "net/tap.h" | |
27 | ||
28 | #include "config-host.h" | |
29 | ||
30 | #include <signal.h> | |
31 | #include <sys/ioctl.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/wait.h> | |
71f4effc | 34 | #include <sys/socket.h> |
5281d757 MM |
35 | #include <net/if.h> |
36 | ||
37 | #include "net.h" | |
38 | #include "sysemu.h" | |
39 | #include "qemu-char.h" | |
40 | #include "qemu-common.h" | |
2f792016 | 41 | #include "qemu-error.h" |
5281d757 | 42 | |
5281d757 | 43 | #include "net/tap-linux.h" |
5281d757 | 44 | |
5281d757 MM |
45 | /* Maximum GSO packet size (64k) plus plenty of room for |
46 | * the ethernet and virtio_net headers | |
47 | */ | |
48 | #define TAP_BUFSIZE (4096 + 65536) | |
49 | ||
50 | typedef struct TAPState { | |
3e35ba93 | 51 | VLANClientState nc; |
5281d757 MM |
52 | int fd; |
53 | char down_script[1024]; | |
54 | char down_script_arg[128]; | |
55 | uint8_t buf[TAP_BUFSIZE]; | |
56 | unsigned int read_poll : 1; | |
57 | unsigned int write_poll : 1; | |
58 | unsigned int has_vnet_hdr : 1; | |
59 | unsigned int using_vnet_hdr : 1; | |
60 | unsigned int has_ufo: 1; | |
61 | } TAPState; | |
62 | ||
63 | static int launch_script(const char *setup_script, const char *ifname, int fd); | |
64 | ||
65 | static int tap_can_send(void *opaque); | |
66 | static void tap_send(void *opaque); | |
67 | static void tap_writable(void *opaque); | |
68 | ||
69 | static void tap_update_fd_handler(TAPState *s) | |
70 | { | |
71 | qemu_set_fd_handler2(s->fd, | |
72 | s->read_poll ? tap_can_send : NULL, | |
73 | s->read_poll ? tap_send : NULL, | |
74 | s->write_poll ? tap_writable : NULL, | |
75 | s); | |
76 | } | |
77 | ||
78 | static void tap_read_poll(TAPState *s, int enable) | |
79 | { | |
80 | s->read_poll = !!enable; | |
81 | tap_update_fd_handler(s); | |
82 | } | |
83 | ||
84 | static void tap_write_poll(TAPState *s, int enable) | |
85 | { | |
86 | s->write_poll = !!enable; | |
87 | tap_update_fd_handler(s); | |
88 | } | |
89 | ||
90 | static void tap_writable(void *opaque) | |
91 | { | |
92 | TAPState *s = opaque; | |
93 | ||
94 | tap_write_poll(s, 0); | |
95 | ||
3e35ba93 | 96 | qemu_flush_queued_packets(&s->nc); |
5281d757 MM |
97 | } |
98 | ||
99 | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) | |
100 | { | |
101 | ssize_t len; | |
102 | ||
103 | do { | |
104 | len = writev(s->fd, iov, iovcnt); | |
105 | } while (len == -1 && errno == EINTR); | |
106 | ||
107 | if (len == -1 && errno == EAGAIN) { | |
108 | tap_write_poll(s, 1); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | return len; | |
113 | } | |
114 | ||
3e35ba93 | 115 | static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov, |
5281d757 MM |
116 | int iovcnt) |
117 | { | |
3e35ba93 | 118 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
119 | const struct iovec *iovp = iov; |
120 | struct iovec iov_copy[iovcnt + 1]; | |
121 | struct virtio_net_hdr hdr = { 0, }; | |
122 | ||
123 | if (s->has_vnet_hdr && !s->using_vnet_hdr) { | |
124 | iov_copy[0].iov_base = &hdr; | |
125 | iov_copy[0].iov_len = sizeof(hdr); | |
126 | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); | |
127 | iovp = iov_copy; | |
128 | iovcnt++; | |
129 | } | |
130 | ||
131 | return tap_write_packet(s, iovp, iovcnt); | |
132 | } | |
133 | ||
3e35ba93 | 134 | static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 135 | { |
3e35ba93 | 136 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
137 | struct iovec iov[2]; |
138 | int iovcnt = 0; | |
139 | struct virtio_net_hdr hdr = { 0, }; | |
140 | ||
141 | if (s->has_vnet_hdr) { | |
142 | iov[iovcnt].iov_base = &hdr; | |
143 | iov[iovcnt].iov_len = sizeof(hdr); | |
144 | iovcnt++; | |
145 | } | |
146 | ||
147 | iov[iovcnt].iov_base = (char *)buf; | |
148 | iov[iovcnt].iov_len = size; | |
149 | iovcnt++; | |
150 | ||
151 | return tap_write_packet(s, iov, iovcnt); | |
152 | } | |
153 | ||
3e35ba93 | 154 | static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
5281d757 | 155 | { |
3e35ba93 | 156 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
157 | struct iovec iov[1]; |
158 | ||
159 | if (s->has_vnet_hdr && !s->using_vnet_hdr) { | |
3e35ba93 | 160 | return tap_receive_raw(nc, buf, size); |
5281d757 MM |
161 | } |
162 | ||
163 | iov[0].iov_base = (char *)buf; | |
164 | iov[0].iov_len = size; | |
165 | ||
166 | return tap_write_packet(s, iov, 1); | |
167 | } | |
168 | ||
169 | static int tap_can_send(void *opaque) | |
170 | { | |
171 | TAPState *s = opaque; | |
172 | ||
3e35ba93 | 173 | return qemu_can_send_packet(&s->nc); |
5281d757 MM |
174 | } |
175 | ||
966ea5ec MM |
176 | #ifndef __sun__ |
177 | ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
5281d757 MM |
178 | { |
179 | return read(tapfd, buf, maxlen); | |
180 | } | |
181 | #endif | |
182 | ||
3e35ba93 | 183 | static void tap_send_completed(VLANClientState *nc, ssize_t len) |
5281d757 | 184 | { |
3e35ba93 | 185 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
186 | tap_read_poll(s, 1); |
187 | } | |
188 | ||
189 | static void tap_send(void *opaque) | |
190 | { | |
191 | TAPState *s = opaque; | |
192 | int size; | |
193 | ||
5819c918 MM |
194 | do { |
195 | uint8_t *buf = s->buf; | |
196 | ||
197 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
198 | if (size <= 0) { | |
199 | break; | |
200 | } | |
201 | ||
202 | if (s->has_vnet_hdr && !s->using_vnet_hdr) { | |
203 | buf += sizeof(struct virtio_net_hdr); | |
204 | size -= sizeof(struct virtio_net_hdr); | |
205 | } | |
206 | ||
3e35ba93 | 207 | size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); |
5819c918 MM |
208 | if (size == 0) { |
209 | tap_read_poll(s, 0); | |
210 | } | |
3e35ba93 | 211 | } while (size > 0 && qemu_can_send_packet(&s->nc)); |
5281d757 MM |
212 | } |
213 | ||
3e35ba93 | 214 | int tap_has_ufo(VLANClientState *nc) |
5281d757 | 215 | { |
3e35ba93 | 216 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 217 | |
665a3b07 | 218 | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
5281d757 MM |
219 | |
220 | return s->has_ufo; | |
221 | } | |
222 | ||
3e35ba93 | 223 | int tap_has_vnet_hdr(VLANClientState *nc) |
5281d757 | 224 | { |
3e35ba93 | 225 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 226 | |
665a3b07 | 227 | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
5281d757 MM |
228 | |
229 | return s->has_vnet_hdr; | |
230 | } | |
231 | ||
3e35ba93 | 232 | void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr) |
5281d757 | 233 | { |
3e35ba93 | 234 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 MM |
235 | |
236 | using_vnet_hdr = using_vnet_hdr != 0; | |
237 | ||
665a3b07 | 238 | assert(nc->info->type == NET_CLIENT_TYPE_TAP); |
5281d757 MM |
239 | assert(s->has_vnet_hdr == using_vnet_hdr); |
240 | ||
241 | s->using_vnet_hdr = using_vnet_hdr; | |
242 | } | |
243 | ||
3e35ba93 | 244 | void tap_set_offload(VLANClientState *nc, int csum, int tso4, |
5281d757 MM |
245 | int tso6, int ecn, int ufo) |
246 | { | |
3e35ba93 | 247 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 248 | |
1faac1f7 | 249 | return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo); |
5281d757 MM |
250 | } |
251 | ||
3e35ba93 | 252 | static void tap_cleanup(VLANClientState *nc) |
5281d757 | 253 | { |
3e35ba93 | 254 | TAPState *s = DO_UPCAST(TAPState, nc, nc); |
5281d757 | 255 | |
3e35ba93 | 256 | qemu_purge_queued_packets(nc); |
5281d757 MM |
257 | |
258 | if (s->down_script[0]) | |
259 | launch_script(s->down_script, s->down_script_arg, s->fd); | |
260 | ||
261 | tap_read_poll(s, 0); | |
262 | tap_write_poll(s, 0); | |
263 | close(s->fd); | |
5281d757 MM |
264 | } |
265 | ||
ceb69615 MT |
266 | static void tap_poll(VLANClientState *nc, bool enable) |
267 | { | |
268 | TAPState *s = DO_UPCAST(TAPState, nc, nc); | |
269 | tap_read_poll(s, enable); | |
270 | tap_write_poll(s, enable); | |
271 | } | |
272 | ||
5281d757 MM |
273 | /* fd support */ |
274 | ||
3e35ba93 MM |
275 | static NetClientInfo net_tap_info = { |
276 | .type = NET_CLIENT_TYPE_TAP, | |
277 | .size = sizeof(TAPState), | |
278 | .receive = tap_receive, | |
279 | .receive_raw = tap_receive_raw, | |
280 | .receive_iov = tap_receive_iov, | |
ceb69615 | 281 | .poll = tap_poll, |
3e35ba93 MM |
282 | .cleanup = tap_cleanup, |
283 | }; | |
284 | ||
5281d757 MM |
285 | static TAPState *net_tap_fd_init(VLANState *vlan, |
286 | const char *model, | |
287 | const char *name, | |
288 | int fd, | |
289 | int vnet_hdr) | |
290 | { | |
3e35ba93 | 291 | VLANClientState *nc; |
5281d757 | 292 | TAPState *s; |
5281d757 | 293 | |
3e35ba93 MM |
294 | nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name); |
295 | ||
296 | s = DO_UPCAST(TAPState, nc, nc); | |
297 | ||
5281d757 MM |
298 | s->fd = fd; |
299 | s->has_vnet_hdr = vnet_hdr != 0; | |
300 | s->using_vnet_hdr = 0; | |
9c282718 | 301 | s->has_ufo = tap_probe_has_ufo(s->fd); |
3e35ba93 | 302 | tap_set_offload(&s->nc, 0, 0, 0, 0, 0); |
5281d757 MM |
303 | tap_read_poll(s, 1); |
304 | return s; | |
305 | } | |
306 | ||
5281d757 MM |
307 | static int launch_script(const char *setup_script, const char *ifname, int fd) |
308 | { | |
309 | sigset_t oldmask, mask; | |
310 | int pid, status; | |
311 | char *args[3]; | |
312 | char **parg; | |
313 | ||
314 | sigemptyset(&mask); | |
315 | sigaddset(&mask, SIGCHLD); | |
316 | sigprocmask(SIG_BLOCK, &mask, &oldmask); | |
317 | ||
318 | /* try to launch network script */ | |
319 | pid = fork(); | |
320 | if (pid == 0) { | |
321 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
322 | ||
323 | for (i = 0; i < open_max; i++) { | |
324 | if (i != STDIN_FILENO && | |
325 | i != STDOUT_FILENO && | |
326 | i != STDERR_FILENO && | |
327 | i != fd) { | |
328 | close(i); | |
329 | } | |
330 | } | |
331 | parg = args; | |
332 | *parg++ = (char *)setup_script; | |
333 | *parg++ = (char *)ifname; | |
334 | *parg++ = NULL; | |
335 | execv(setup_script, args); | |
336 | _exit(1); | |
337 | } else if (pid > 0) { | |
338 | while (waitpid(pid, &status, 0) != pid) { | |
339 | /* loop */ | |
340 | } | |
341 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
342 | ||
343 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
344 | return 0; | |
345 | } | |
346 | } | |
347 | fprintf(stderr, "%s: could not launch network script\n", setup_script); | |
348 | return -1; | |
349 | } | |
350 | ||
351 | static int net_tap_init(QemuOpts *opts, int *vnet_hdr) | |
352 | { | |
353 | int fd, vnet_hdr_required; | |
354 | char ifname[128] = {0,}; | |
355 | const char *setup_script; | |
356 | ||
357 | if (qemu_opt_get(opts, "ifname")) { | |
358 | pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); | |
359 | } | |
360 | ||
361 | *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1); | |
362 | if (qemu_opt_get(opts, "vnet_hdr")) { | |
363 | vnet_hdr_required = *vnet_hdr; | |
364 | } else { | |
365 | vnet_hdr_required = 0; | |
366 | } | |
367 | ||
368 | TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required)); | |
369 | if (fd < 0) { | |
370 | return -1; | |
371 | } | |
372 | ||
373 | setup_script = qemu_opt_get(opts, "script"); | |
374 | if (setup_script && | |
375 | setup_script[0] != '\0' && | |
376 | strcmp(setup_script, "no") != 0 && | |
377 | launch_script(setup_script, ifname, fd)) { | |
378 | close(fd); | |
379 | return -1; | |
380 | } | |
381 | ||
382 | qemu_opt_set(opts, "ifname", ifname); | |
383 | ||
384 | return fd; | |
385 | } | |
386 | ||
387 | int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan) | |
388 | { | |
389 | TAPState *s; | |
df6c2a0f | 390 | int fd, vnet_hdr = 0; |
5281d757 MM |
391 | |
392 | if (qemu_opt_get(opts, "fd")) { | |
393 | if (qemu_opt_get(opts, "ifname") || | |
394 | qemu_opt_get(opts, "script") || | |
395 | qemu_opt_get(opts, "downscript") || | |
396 | qemu_opt_get(opts, "vnet_hdr")) { | |
1ecda02b | 397 | error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd="); |
5281d757 MM |
398 | return -1; |
399 | } | |
400 | ||
401 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); | |
402 | if (fd == -1) { | |
403 | return -1; | |
404 | } | |
405 | ||
406 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
407 | ||
408 | vnet_hdr = tap_probe_vnet_hdr(fd); | |
409 | } else { | |
410 | if (!qemu_opt_get(opts, "script")) { | |
411 | qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT); | |
412 | } | |
413 | ||
414 | if (!qemu_opt_get(opts, "downscript")) { | |
415 | qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT); | |
416 | } | |
417 | ||
418 | fd = net_tap_init(opts, &vnet_hdr); | |
929fe497 JL |
419 | if (fd == -1) { |
420 | return -1; | |
421 | } | |
5281d757 MM |
422 | } |
423 | ||
424 | s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr); | |
425 | if (!s) { | |
426 | close(fd); | |
427 | return -1; | |
428 | } | |
429 | ||
15ac913b | 430 | if (tap_set_sndbuf(s->fd, opts) < 0) { |
5281d757 MM |
431 | return -1; |
432 | } | |
433 | ||
434 | if (qemu_opt_get(opts, "fd")) { | |
3e35ba93 | 435 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd); |
5281d757 MM |
436 | } else { |
437 | const char *ifname, *script, *downscript; | |
438 | ||
439 | ifname = qemu_opt_get(opts, "ifname"); | |
440 | script = qemu_opt_get(opts, "script"); | |
441 | downscript = qemu_opt_get(opts, "downscript"); | |
442 | ||
3e35ba93 | 443 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
5281d757 MM |
444 | "ifname=%s,script=%s,downscript=%s", |
445 | ifname, script, downscript); | |
446 | ||
447 | if (strcmp(downscript, "no") != 0) { | |
448 | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); | |
449 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); | |
450 | } | |
451 | } | |
452 | ||
5281d757 MM |
453 | return 0; |
454 | } |