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
28 #include "config-host.h"
31 #include <sys/ioctl.h>
34 #include <sys/socket.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 {
53 char down_script[1024];
54 char down_script_arg[128];
55 uint8_t buf[TAP_BUFSIZE];
56 Notifier exit_notifier;
57 unsigned int read_poll : 1;
58 unsigned int write_poll : 1;
59 unsigned int has_vnet_hdr : 1;
60 unsigned int using_vnet_hdr : 1;
61 unsigned int has_ufo: 1;
64 static int launch_script(const char *setup_script, const char *ifname, int fd);
66 static int tap_can_send(void *opaque);
67 static void tap_send(void *opaque);
68 static void tap_writable(void *opaque);
70 static void tap_update_fd_handler(TAPState *s)
72 qemu_set_fd_handler2(s->fd,
73 s->read_poll ? tap_can_send : NULL,
74 s->read_poll ? tap_send : NULL,
75 s->write_poll ? tap_writable : NULL,
79 static void tap_read_poll(TAPState *s, int enable)
81 s->read_poll = !!enable;
82 tap_update_fd_handler(s);
85 static void tap_write_poll(TAPState *s, int enable)
87 s->write_poll = !!enable;
88 tap_update_fd_handler(s);
91 static void tap_writable(void *opaque)
97 qemu_flush_queued_packets(&s->nc);
100 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
105 len = writev(s->fd, iov, iovcnt);
106 } while (len == -1 && errno == EINTR);
108 if (len == -1 && errno == EAGAIN) {
109 tap_write_poll(s, 1);
116 static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
119 TAPState *s = DO_UPCAST(TAPState, nc, nc);
120 const struct iovec *iovp = iov;
121 struct iovec iov_copy[iovcnt + 1];
122 struct virtio_net_hdr hdr = { 0, };
124 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
125 iov_copy[0].iov_base = &hdr;
126 iov_copy[0].iov_len = sizeof(hdr);
127 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
132 return tap_write_packet(s, iovp, iovcnt);
135 static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
137 TAPState *s = DO_UPCAST(TAPState, nc, nc);
140 struct virtio_net_hdr hdr = { 0, };
142 if (s->has_vnet_hdr) {
143 iov[iovcnt].iov_base = &hdr;
144 iov[iovcnt].iov_len = sizeof(hdr);
148 iov[iovcnt].iov_base = (char *)buf;
149 iov[iovcnt].iov_len = size;
152 return tap_write_packet(s, iov, iovcnt);
155 static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
157 TAPState *s = DO_UPCAST(TAPState, nc, nc);
160 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
161 return tap_receive_raw(nc, buf, size);
164 iov[0].iov_base = (char *)buf;
165 iov[0].iov_len = size;
167 return tap_write_packet(s, iov, 1);
170 static int tap_can_send(void *opaque)
172 TAPState *s = opaque;
174 return qemu_can_send_packet(&s->nc);
178 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
180 return read(tapfd, buf, maxlen);
184 static void tap_send_completed(VLANClientState *nc, ssize_t len)
186 TAPState *s = DO_UPCAST(TAPState, nc, nc);
190 static void tap_send(void *opaque)
192 TAPState *s = opaque;
196 uint8_t *buf = s->buf;
198 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
203 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
204 buf += sizeof(struct virtio_net_hdr);
205 size -= sizeof(struct virtio_net_hdr);
208 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
212 } while (size > 0 && qemu_can_send_packet(&s->nc));
215 int tap_has_ufo(VLANClientState *nc)
217 TAPState *s = DO_UPCAST(TAPState, nc, nc);
219 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
224 int tap_has_vnet_hdr(VLANClientState *nc)
226 TAPState *s = DO_UPCAST(TAPState, nc, nc);
228 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
230 return s->has_vnet_hdr;
233 void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
235 TAPState *s = DO_UPCAST(TAPState, nc, nc);
237 using_vnet_hdr = using_vnet_hdr != 0;
239 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
240 assert(s->has_vnet_hdr == using_vnet_hdr);
242 s->using_vnet_hdr = using_vnet_hdr;
245 void tap_set_offload(VLANClientState *nc, int csum, int tso4,
246 int tso6, int ecn, int ufo)
248 TAPState *s = DO_UPCAST(TAPState, nc, nc);
250 return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
253 static void tap_cleanup(VLANClientState *nc)
255 TAPState *s = DO_UPCAST(TAPState, nc, nc);
257 qemu_purge_queued_packets(nc);
259 if (s->down_script[0])
260 launch_script(s->down_script, s->down_script_arg, s->fd);
263 tap_write_poll(s, 0);
265 exit_notifier_remove(&s->exit_notifier);
268 /* Instead of exiting gracefully, we're exiting because someone called
269 * exit(), make sure to invoke down script at least
271 static void tap_cleanup_at_exit(Notifier *notifier)
273 TAPState *s = container_of(notifier, TAPState, exit_notifier);
275 if (s->down_script[0]) {
276 launch_script(s->down_script, s->down_script_arg, s->fd);
280 static void tap_poll(VLANClientState *nc, bool enable)
282 TAPState *s = DO_UPCAST(TAPState, nc, nc);
283 tap_read_poll(s, enable);
284 tap_write_poll(s, enable);
289 static NetClientInfo net_tap_info = {
290 .type = NET_CLIENT_TYPE_TAP,
291 .size = sizeof(TAPState),
292 .receive = tap_receive,
293 .receive_raw = tap_receive_raw,
294 .receive_iov = tap_receive_iov,
296 .cleanup = tap_cleanup,
299 static TAPState *net_tap_fd_init(VLANState *vlan,
308 nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
310 s = DO_UPCAST(TAPState, nc, nc);
313 s->has_vnet_hdr = vnet_hdr != 0;
314 s->using_vnet_hdr = 0;
315 s->has_ufo = tap_probe_has_ufo(s->fd);
316 s->exit_notifier.notify = tap_cleanup_at_exit;
317 exit_notifier_add(&s->exit_notifier);
318 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
323 static int launch_script(const char *setup_script, const char *ifname, int fd)
325 sigset_t oldmask, mask;
331 sigaddset(&mask, SIGCHLD);
332 sigprocmask(SIG_BLOCK, &mask, &oldmask);
334 /* try to launch network script */
337 int open_max = sysconf(_SC_OPEN_MAX), i;
339 for (i = 0; i < open_max; i++) {
340 if (i != STDIN_FILENO &&
341 i != STDOUT_FILENO &&
342 i != STDERR_FILENO &&
348 *parg++ = (char *)setup_script;
349 *parg++ = (char *)ifname;
351 execv(setup_script, args);
353 } else if (pid > 0) {
354 while (waitpid(pid, &status, 0) != pid) {
357 sigprocmask(SIG_SETMASK, &oldmask, NULL);
359 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
363 fprintf(stderr, "%s: could not launch network script\n", setup_script);
367 static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
369 int fd, vnet_hdr_required;
370 char ifname[128] = {0,};
371 const char *setup_script;
373 if (qemu_opt_get(opts, "ifname")) {
374 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
377 *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
378 if (qemu_opt_get(opts, "vnet_hdr")) {
379 vnet_hdr_required = *vnet_hdr;
381 vnet_hdr_required = 0;
384 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
389 setup_script = qemu_opt_get(opts, "script");
391 setup_script[0] != '\0' &&
392 strcmp(setup_script, "no") != 0 &&
393 launch_script(setup_script, ifname, fd)) {
398 qemu_opt_set(opts, "ifname", ifname);
403 int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
406 int fd, vnet_hdr = 0;
408 if (qemu_opt_get(opts, "fd")) {
409 if (qemu_opt_get(opts, "ifname") ||
410 qemu_opt_get(opts, "script") ||
411 qemu_opt_get(opts, "downscript") ||
412 qemu_opt_get(opts, "vnet_hdr")) {
413 error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
417 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
422 fcntl(fd, F_SETFL, O_NONBLOCK);
424 vnet_hdr = tap_probe_vnet_hdr(fd);
426 if (!qemu_opt_get(opts, "script")) {
427 qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
430 if (!qemu_opt_get(opts, "downscript")) {
431 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
434 fd = net_tap_init(opts, &vnet_hdr);
440 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
446 if (tap_set_sndbuf(s->fd, opts) < 0) {
450 if (qemu_opt_get(opts, "fd")) {
451 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
453 const char *ifname, *script, *downscript;
455 ifname = qemu_opt_get(opts, "ifname");
456 script = qemu_opt_get(opts, "script");
457 downscript = qemu_opt_get(opts, "downscript");
459 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
460 "ifname=%s,script=%s,downscript=%s",
461 ifname, script, downscript);
463 if (strcmp(downscript, "no") != 0) {
464 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
465 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);