Revert "usb-linux: remove unreachable default in switch statement"
[qemu/aliguori-queue.git] / net / tap.c
blob672b0ee0b49c226d41f9dc44ce6e8ac8706b41a9
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "net/tap.h"
28 #include "config-host.h"
30 #include <signal.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
37 #include "net.h"
38 #include "sysemu.h"
39 #include "qemu-char.h"
40 #include "qemu-common.h"
41 #include "qemu-error.h"
43 #include "net/tap-linux.h"
45 /* Maximum GSO packet size (64k) plus plenty of room for
46 * the ethernet and virtio_net headers
48 #define TAP_BUFSIZE (4096 + 65536)
50 typedef struct TAPState {
51 VLANClientState nc;
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;
63 static int launch_script(const char *setup_script, const char *ifname, int fd);
65 static int tap_can_send(void *opaque);
66 static void tap_send(void *opaque);
67 static void tap_writable(void *opaque);
69 static void tap_update_fd_handler(TAPState *s)
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);
78 static void tap_read_poll(TAPState *s, int enable)
80 s->read_poll = !!enable;
81 tap_update_fd_handler(s);
84 static void tap_write_poll(TAPState *s, int enable)
86 s->write_poll = !!enable;
87 tap_update_fd_handler(s);
90 static void tap_writable(void *opaque)
92 TAPState *s = opaque;
94 tap_write_poll(s, 0);
96 qemu_flush_queued_packets(&s->nc);
99 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
101 ssize_t len;
103 do {
104 len = writev(s->fd, iov, iovcnt);
105 } while (len == -1 && errno == EINTR);
107 if (len == -1 && errno == EAGAIN) {
108 tap_write_poll(s, 1);
109 return 0;
112 return len;
115 static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
116 int iovcnt)
118 TAPState *s = DO_UPCAST(TAPState, nc, nc);
119 const struct iovec *iovp = iov;
120 struct iovec iov_copy[iovcnt + 1];
121 struct virtio_net_hdr hdr = { 0, };
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++;
131 return tap_write_packet(s, iovp, iovcnt);
134 static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
136 TAPState *s = DO_UPCAST(TAPState, nc, nc);
137 struct iovec iov[2];
138 int iovcnt = 0;
139 struct virtio_net_hdr hdr = { 0, };
141 if (s->has_vnet_hdr) {
142 iov[iovcnt].iov_base = &hdr;
143 iov[iovcnt].iov_len = sizeof(hdr);
144 iovcnt++;
147 iov[iovcnt].iov_base = (char *)buf;
148 iov[iovcnt].iov_len = size;
149 iovcnt++;
151 return tap_write_packet(s, iov, iovcnt);
154 static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
156 TAPState *s = DO_UPCAST(TAPState, nc, nc);
157 struct iovec iov[1];
159 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
160 return tap_receive_raw(nc, buf, size);
163 iov[0].iov_base = (char *)buf;
164 iov[0].iov_len = size;
166 return tap_write_packet(s, iov, 1);
169 static int tap_can_send(void *opaque)
171 TAPState *s = opaque;
173 return qemu_can_send_packet(&s->nc);
176 #ifndef __sun__
177 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
179 return read(tapfd, buf, maxlen);
181 #endif
183 static void tap_send_completed(VLANClientState *nc, ssize_t len)
185 TAPState *s = DO_UPCAST(TAPState, nc, nc);
186 tap_read_poll(s, 1);
189 static void tap_send(void *opaque)
191 TAPState *s = opaque;
192 int size;
194 do {
195 uint8_t *buf = s->buf;
197 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
198 if (size <= 0) {
199 break;
202 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
203 buf += sizeof(struct virtio_net_hdr);
204 size -= sizeof(struct virtio_net_hdr);
207 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
208 if (size == 0) {
209 tap_read_poll(s, 0);
211 } while (size > 0 && qemu_can_send_packet(&s->nc));
214 int tap_has_ufo(VLANClientState *nc)
216 TAPState *s = DO_UPCAST(TAPState, nc, nc);
218 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
220 return s->has_ufo;
223 int tap_has_vnet_hdr(VLANClientState *nc)
225 TAPState *s = DO_UPCAST(TAPState, nc, nc);
227 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
229 return s->has_vnet_hdr;
232 void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
234 TAPState *s = DO_UPCAST(TAPState, nc, nc);
236 using_vnet_hdr = using_vnet_hdr != 0;
238 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
239 assert(s->has_vnet_hdr == using_vnet_hdr);
241 s->using_vnet_hdr = using_vnet_hdr;
244 void tap_set_offload(VLANClientState *nc, int csum, int tso4,
245 int tso6, int ecn, int ufo)
247 TAPState *s = DO_UPCAST(TAPState, nc, nc);
249 return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
252 static void tap_cleanup(VLANClientState *nc)
254 TAPState *s = DO_UPCAST(TAPState, nc, nc);
256 qemu_purge_queued_packets(nc);
258 if (s->down_script[0])
259 launch_script(s->down_script, s->down_script_arg, s->fd);
261 tap_read_poll(s, 0);
262 tap_write_poll(s, 0);
263 close(s->fd);
266 static void tap_poll(VLANClientState *nc, bool enable)
268 TAPState *s = DO_UPCAST(TAPState, nc, nc);
269 tap_read_poll(s, enable);
270 tap_write_poll(s, enable);
273 /* fd support */
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,
281 .poll = tap_poll,
282 .cleanup = tap_cleanup,
285 static TAPState *net_tap_fd_init(VLANState *vlan,
286 const char *model,
287 const char *name,
288 int fd,
289 int vnet_hdr)
291 VLANClientState *nc;
292 TAPState *s;
294 nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
296 s = DO_UPCAST(TAPState, nc, nc);
298 s->fd = fd;
299 s->has_vnet_hdr = vnet_hdr != 0;
300 s->using_vnet_hdr = 0;
301 s->has_ufo = tap_probe_has_ufo(s->fd);
302 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
303 tap_read_poll(s, 1);
304 return s;
307 static int launch_script(const char *setup_script, const char *ifname, int fd)
309 sigset_t oldmask, mask;
310 int pid, status;
311 char *args[3];
312 char **parg;
314 sigemptyset(&mask);
315 sigaddset(&mask, SIGCHLD);
316 sigprocmask(SIG_BLOCK, &mask, &oldmask);
318 /* try to launch network script */
319 pid = fork();
320 if (pid == 0) {
321 int open_max = sysconf(_SC_OPEN_MAX), i;
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);
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 */
341 sigprocmask(SIG_SETMASK, &oldmask, NULL);
343 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
344 return 0;
347 fprintf(stderr, "%s: could not launch network script\n", setup_script);
348 return -1;
351 static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
353 int fd, vnet_hdr_required;
354 char ifname[128] = {0,};
355 const char *setup_script;
357 if (qemu_opt_get(opts, "ifname")) {
358 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
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;
368 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
369 if (fd < 0) {
370 return -1;
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;
382 qemu_opt_set(opts, "ifname", ifname);
384 return fd;
387 int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
389 TAPState *s;
390 int fd, vnet_hdr = 0;
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")) {
397 error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
398 return -1;
401 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
402 if (fd == -1) {
403 return -1;
406 fcntl(fd, F_SETFL, O_NONBLOCK);
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);
414 if (!qemu_opt_get(opts, "downscript")) {
415 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
418 fd = net_tap_init(opts, &vnet_hdr);
419 if (fd == -1) {
420 return -1;
424 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
425 if (!s) {
426 close(fd);
427 return -1;
430 if (tap_set_sndbuf(s->fd, opts) < 0) {
431 return -1;
434 if (qemu_opt_get(opts, "fd")) {
435 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
436 } else {
437 const char *ifname, *script, *downscript;
439 ifname = qemu_opt_get(opts, "ifname");
440 script = qemu_opt_get(opts, "script");
441 downscript = qemu_opt_get(opts, "downscript");
443 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
444 "ifname=%s,script=%s,downscript=%s",
445 ifname, script, downscript);
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);
453 return 0;