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>
38 #include "qemu-char.h"
39 #include "qemu-common.h"
41 #include "net/tap-linux.h"
43 /* Maximum GSO packet size (64k) plus plenty of room for
44 * the ethernet and virtio_net headers
46 #define TAP_BUFSIZE (4096 + 65536)
48 typedef struct TAPState {
51 char down_script[1024];
52 char down_script_arg[128];
53 uint8_t buf[TAP_BUFSIZE];
54 unsigned int read_poll : 1;
55 unsigned int write_poll : 1;
56 unsigned int has_vnet_hdr : 1;
57 unsigned int using_vnet_hdr : 1;
58 unsigned int has_ufo: 1;
61 static int launch_script(const char *setup_script, const char *ifname, int fd);
63 static int tap_can_send(void *opaque);
64 static void tap_send(void *opaque);
65 static void tap_writable(void *opaque);
67 static void tap_update_fd_handler(TAPState *s)
69 qemu_set_fd_handler2(s->fd,
70 s->read_poll ? tap_can_send : NULL,
71 s->read_poll ? tap_send : NULL,
72 s->write_poll ? tap_writable : NULL,
76 static void tap_read_poll(TAPState *s, int enable)
78 s->read_poll = !!enable;
79 tap_update_fd_handler(s);
82 static void tap_write_poll(TAPState *s, int enable)
84 s->write_poll = !!enable;
85 tap_update_fd_handler(s);
88 static void tap_writable(void *opaque)
94 qemu_flush_queued_packets(s->vc);
97 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
102 len = writev(s->fd, iov, iovcnt);
103 } while (len == -1 && errno == EINTR);
105 if (len == -1 && errno == EAGAIN) {
106 tap_write_poll(s, 1);
113 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
116 TAPState *s = vc->opaque;
117 const struct iovec *iovp = iov;
118 struct iovec iov_copy[iovcnt + 1];
119 struct virtio_net_hdr hdr = { 0, };
121 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
122 iov_copy[0].iov_base = &hdr;
123 iov_copy[0].iov_len = sizeof(hdr);
124 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
129 return tap_write_packet(s, iovp, iovcnt);
132 static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
134 TAPState *s = vc->opaque;
137 struct virtio_net_hdr hdr = { 0, };
139 if (s->has_vnet_hdr) {
140 iov[iovcnt].iov_base = &hdr;
141 iov[iovcnt].iov_len = sizeof(hdr);
145 iov[iovcnt].iov_base = (char *)buf;
146 iov[iovcnt].iov_len = size;
149 return tap_write_packet(s, iov, iovcnt);
152 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
154 TAPState *s = vc->opaque;
157 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
158 return tap_receive_raw(vc, buf, size);
161 iov[0].iov_base = (char *)buf;
162 iov[0].iov_len = size;
164 return tap_write_packet(s, iov, 1);
167 static int tap_can_send(void *opaque)
169 TAPState *s = opaque;
171 return qemu_can_send_packet(s->vc);
175 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
177 return read(tapfd, buf, maxlen);
181 static void tap_send_completed(VLANClientState *vc, ssize_t len)
183 TAPState *s = vc->opaque;
187 static void tap_send(void *opaque)
189 TAPState *s = opaque;
193 uint8_t *buf = s->buf;
195 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
200 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
201 buf += sizeof(struct virtio_net_hdr);
202 size -= sizeof(struct virtio_net_hdr);
205 size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed);
212 /* sndbuf should be set to a value lower than the tx queue
213 * capacity of any destination network interface.
214 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
215 * a good default, given a 1500 byte MTU.
217 #define TAP_DEFAULT_SNDBUF 1024*1024
219 static int tap_set_sndbuf(TAPState *s, QemuOpts *opts)
223 sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
228 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) {
229 qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno));
235 int tap_has_ufo(VLANClientState *vc)
237 TAPState *s = vc->opaque;
239 assert(vc->type == NET_CLIENT_TYPE_TAP);
244 int tap_has_vnet_hdr(VLANClientState *vc)
246 TAPState *s = vc->opaque;
248 assert(vc->type == NET_CLIENT_TYPE_TAP);
250 return s->has_vnet_hdr;
253 void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr)
255 TAPState *s = vc->opaque;
257 using_vnet_hdr = using_vnet_hdr != 0;
259 assert(vc->type == NET_CLIENT_TYPE_TAP);
260 assert(s->has_vnet_hdr == using_vnet_hdr);
262 s->using_vnet_hdr = using_vnet_hdr;
265 static int tap_probe_vnet_hdr(int fd)
269 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
270 qemu_error("TUNGETIFF ioctl() failed: %s\n", strerror(errno));
274 return ifr.ifr_flags & IFF_VNET_HDR;
277 void tap_set_offload(VLANClientState *vc, int csum, int tso4,
278 int tso6, int ecn, int ufo)
280 TAPState *s = vc->opaque;
281 unsigned int offload = 0;
284 offload |= TUN_F_CSUM;
286 offload |= TUN_F_TSO4;
288 offload |= TUN_F_TSO6;
289 if ((tso4 || tso6) && ecn)
290 offload |= TUN_F_TSO_ECN;
292 offload |= TUN_F_UFO;
295 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) {
296 offload &= ~TUN_F_UFO;
297 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) {
298 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
304 static void tap_cleanup(VLANClientState *vc)
306 TAPState *s = vc->opaque;
308 qemu_purge_queued_packets(vc);
310 if (s->down_script[0])
311 launch_script(s->down_script, s->down_script_arg, s->fd);
314 tap_write_poll(s, 0);
321 static TAPState *net_tap_fd_init(VLANState *vlan,
328 unsigned int offload;
330 s = qemu_mallocz(sizeof(TAPState));
332 s->has_vnet_hdr = vnet_hdr != 0;
333 s->using_vnet_hdr = 0;
334 s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_TAP,
335 vlan, NULL, model, name, NULL,
336 tap_receive, tap_receive_raw,
337 tap_receive_iov, tap_cleanup, s);
339 /* Check if tap supports UFO */
340 offload = TUN_F_CSUM | TUN_F_UFO;
341 if (ioctl(s->fd, TUNSETOFFLOAD, offload) == 0)
343 tap_set_offload(s->vc, 0, 0, 0, 0, 0);
348 static int launch_script(const char *setup_script, const char *ifname, int fd)
350 sigset_t oldmask, mask;
356 sigaddset(&mask, SIGCHLD);
357 sigprocmask(SIG_BLOCK, &mask, &oldmask);
359 /* try to launch network script */
362 int open_max = sysconf(_SC_OPEN_MAX), i;
364 for (i = 0; i < open_max; i++) {
365 if (i != STDIN_FILENO &&
366 i != STDOUT_FILENO &&
367 i != STDERR_FILENO &&
373 *parg++ = (char *)setup_script;
374 *parg++ = (char *)ifname;
376 execv(setup_script, args);
378 } else if (pid > 0) {
379 while (waitpid(pid, &status, 0) != pid) {
382 sigprocmask(SIG_SETMASK, &oldmask, NULL);
384 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
388 fprintf(stderr, "%s: could not launch network script\n", setup_script);
392 static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
394 int fd, vnet_hdr_required;
395 char ifname[128] = {0,};
396 const char *setup_script;
398 if (qemu_opt_get(opts, "ifname")) {
399 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
402 *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
403 if (qemu_opt_get(opts, "vnet_hdr")) {
404 vnet_hdr_required = *vnet_hdr;
406 vnet_hdr_required = 0;
409 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
414 setup_script = qemu_opt_get(opts, "script");
416 setup_script[0] != '\0' &&
417 strcmp(setup_script, "no") != 0 &&
418 launch_script(setup_script, ifname, fd)) {
423 qemu_opt_set(opts, "ifname", ifname);
428 int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
433 if (qemu_opt_get(opts, "fd")) {
434 if (qemu_opt_get(opts, "ifname") ||
435 qemu_opt_get(opts, "script") ||
436 qemu_opt_get(opts, "downscript") ||
437 qemu_opt_get(opts, "vnet_hdr")) {
438 qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
442 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
447 fcntl(fd, F_SETFL, O_NONBLOCK);
449 vnet_hdr = tap_probe_vnet_hdr(fd);
451 if (!qemu_opt_get(opts, "script")) {
452 qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
455 if (!qemu_opt_get(opts, "downscript")) {
456 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
459 fd = net_tap_init(opts, &vnet_hdr);
462 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
468 if (tap_set_sndbuf(s, opts) < 0) {
472 if (qemu_opt_get(opts, "fd")) {
473 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
475 const char *ifname, *script, *downscript;
477 ifname = qemu_opt_get(opts, "ifname");
478 script = qemu_opt_get(opts, "script");
479 downscript = qemu_opt_get(opts, "downscript");
481 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
482 "ifname=%s,script=%s,downscript=%s",
483 ifname, script, downscript);
485 if (strcmp(downscript, "no") != 0) {
486 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
487 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
492 vlan->nb_host_devs++;