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"
30 #include <sys/ioctl.h>
33 #include <sys/socket.h>
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qemu-common.h"
41 #include "qemu/error-report.h"
45 #include "hw/vhost_net.h"
47 /* Maximum GSO packet size (64k) plus plenty of room for
48 * the ethernet and virtio_net headers
50 #define TAP_BUFSIZE (4096 + 65536)
52 typedef struct TAPState {
55 char down_script[1024];
56 char down_script_arg[128];
57 uint8_t buf[TAP_BUFSIZE];
62 VHostNetState *vhost_net;
63 unsigned host_vnet_hdr_len;
66 static int launch_script(const char *setup_script, const char *ifname, int fd);
68 static int tap_can_send(void *opaque);
69 static void tap_send(void *opaque);
70 static void tap_writable(void *opaque);
72 static void tap_update_fd_handler(TAPState *s)
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,
81 static void tap_read_poll(TAPState *s, bool enable)
83 s->read_poll = enable;
84 tap_update_fd_handler(s);
87 static void tap_write_poll(TAPState *s, bool enable)
89 s->write_poll = enable;
90 tap_update_fd_handler(s);
93 static void tap_writable(void *opaque)
97 tap_write_poll(s, false);
99 qemu_flush_queued_packets(&s->nc);
102 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
107 len = writev(s->fd, iov, iovcnt);
108 } while (len == -1 && errno == EINTR);
110 if (len == -1 && errno == EAGAIN) {
111 tap_write_poll(s, true);
118 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
121 TAPState *s = DO_UPCAST(TAPState, nc, nc);
122 const struct iovec *iovp = iov;
123 struct iovec iov_copy[iovcnt + 1];
124 struct virtio_net_hdr_mrg_rxbuf hdr = { };
126 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
127 iov_copy[0].iov_base = &hdr;
128 iov_copy[0].iov_len = s->host_vnet_hdr_len;
129 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
134 return tap_write_packet(s, iovp, iovcnt);
137 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
139 TAPState *s = DO_UPCAST(TAPState, nc, nc);
142 struct virtio_net_hdr_mrg_rxbuf hdr = { };
144 if (s->host_vnet_hdr_len) {
145 iov[iovcnt].iov_base = &hdr;
146 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
150 iov[iovcnt].iov_base = (char *)buf;
151 iov[iovcnt].iov_len = size;
154 return tap_write_packet(s, iov, iovcnt);
157 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
159 TAPState *s = DO_UPCAST(TAPState, nc, nc);
162 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
163 return tap_receive_raw(nc, buf, size);
166 iov[0].iov_base = (char *)buf;
167 iov[0].iov_len = size;
169 return tap_write_packet(s, iov, 1);
172 static int tap_can_send(void *opaque)
174 TAPState *s = opaque;
176 return qemu_can_send_packet(&s->nc);
180 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
182 return read(tapfd, buf, maxlen);
186 static void tap_send_completed(NetClientState *nc, ssize_t len)
188 TAPState *s = DO_UPCAST(TAPState, nc, nc);
189 tap_read_poll(s, true);
192 static void tap_send(void *opaque)
194 TAPState *s = opaque;
198 uint8_t *buf = s->buf;
200 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
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;
210 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
212 tap_read_poll(s, false);
214 } while (size > 0 && qemu_can_send_packet(&s->nc));
217 bool tap_has_ufo(NetClientState *nc)
219 TAPState *s = DO_UPCAST(TAPState, nc, nc);
221 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
226 int tap_has_vnet_hdr(NetClientState *nc)
228 TAPState *s = DO_UPCAST(TAPState, nc, nc);
230 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
232 return !!s->host_vnet_hdr_len;
235 int tap_has_vnet_hdr_len(NetClientState *nc, int len)
237 TAPState *s = DO_UPCAST(TAPState, nc, nc);
239 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
241 return tap_probe_vnet_hdr_len(s->fd, len);
244 void tap_set_vnet_hdr_len(NetClientState *nc, int len)
246 TAPState *s = DO_UPCAST(TAPState, nc, nc);
248 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
249 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
250 len == sizeof(struct virtio_net_hdr));
252 tap_fd_set_vnet_hdr_len(s->fd, len);
253 s->host_vnet_hdr_len = len;
256 void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
258 TAPState *s = DO_UPCAST(TAPState, nc, nc);
260 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
261 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
263 s->using_vnet_hdr = using_vnet_hdr;
266 void tap_set_offload(NetClientState *nc, int csum, int tso4,
267 int tso6, int ecn, int ufo)
269 TAPState *s = DO_UPCAST(TAPState, nc, nc);
274 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
277 static void tap_cleanup(NetClientState *nc)
279 TAPState *s = DO_UPCAST(TAPState, nc, nc);
282 vhost_net_cleanup(s->vhost_net);
286 qemu_purge_queued_packets(nc);
288 if (s->down_script[0])
289 launch_script(s->down_script, s->down_script_arg, s->fd);
291 tap_read_poll(s, false);
292 tap_write_poll(s, false);
297 static void tap_poll(NetClientState *nc, bool enable)
299 TAPState *s = DO_UPCAST(TAPState, nc, nc);
300 tap_read_poll(s, enable);
301 tap_write_poll(s, enable);
304 int tap_get_fd(NetClientState *nc)
306 TAPState *s = DO_UPCAST(TAPState, nc, nc);
307 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
313 static NetClientInfo net_tap_info = {
314 .type = NET_CLIENT_OPTIONS_KIND_TAP,
315 .size = sizeof(TAPState),
316 .receive = tap_receive,
317 .receive_raw = tap_receive_raw,
318 .receive_iov = tap_receive_iov,
320 .cleanup = tap_cleanup,
323 static TAPState *net_tap_fd_init(NetClientState *peer,
332 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
334 s = DO_UPCAST(TAPState, nc, nc);
337 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
338 s->using_vnet_hdr = false;
339 s->has_ufo = tap_probe_has_ufo(s->fd);
340 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
342 * Make sure host header length is set correctly in tap:
343 * it might have been modified by another instance of qemu.
345 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
346 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
348 tap_read_poll(s, true);
353 static int launch_script(const char *setup_script, const char *ifname, int fd)
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) {
383 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
387 fprintf(stderr, "%s: could not launch network script\n", setup_script);
391 static int recv_fd(int c)
394 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
395 struct msghdr msg = {
396 .msg_control = msgbuf,
397 .msg_controllen = sizeof(msgbuf),
399 struct cmsghdr *cmsg;
404 cmsg = CMSG_FIRSTHDR(&msg);
405 cmsg->cmsg_level = SOL_SOCKET;
406 cmsg->cmsg_type = SCM_RIGHTS;
407 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
408 msg.msg_controllen = cmsg->cmsg_len;
411 iov.iov_len = sizeof(req);
416 len = recvmsg(c, &msg, 0);
418 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
425 static int net_bridge_run_helper(const char *helper, const char *bridge)
427 sigset_t oldmask, mask;
434 sigaddset(&mask, SIGCHLD);
435 sigprocmask(SIG_BLOCK, &mask, &oldmask);
437 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
441 /* try to launch bridge helper */
444 int open_max = sysconf(_SC_OPEN_MAX), i;
446 char br_buf[6+IFNAMSIZ] = {0};
447 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
449 for (i = 0; i < open_max; i++) {
450 if (i != STDIN_FILENO &&
451 i != STDOUT_FILENO &&
452 i != STDERR_FILENO &&
458 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
460 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
461 /* assume helper is a command */
463 if (strstr(helper, "--br=") == NULL) {
464 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
467 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
468 helper, "--use-vnet", fd_buf, br_buf);
471 *parg++ = (char *)"sh";
472 *parg++ = (char *)"-c";
473 *parg++ = helper_cmd;
476 execv("/bin/sh", args);
478 /* assume helper is just the executable path name */
480 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
483 *parg++ = (char *)helper;
484 *parg++ = (char *)"--use-vnet";
493 } else if (pid > 0) {
500 } while (fd == -1 && errno == EINTR);
504 while (waitpid(pid, &status, 0) != pid) {
507 sigprocmask(SIG_SETMASK, &oldmask, NULL);
509 fprintf(stderr, "failed to recv file descriptor\n");
513 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
517 fprintf(stderr, "failed to launch bridge helper\n");
521 int net_init_bridge(const NetClientOptions *opts, const char *name,
522 NetClientState *peer)
524 const NetdevBridgeOptions *bridge;
525 const char *helper, *br;
530 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
531 bridge = opts->bridge;
533 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
534 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
536 fd = net_bridge_run_helper(helper, br);
541 fcntl(fd, F_SETFL, O_NONBLOCK);
543 vnet_hdr = tap_probe_vnet_hdr(fd);
545 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
551 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
557 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
558 const char *setup_script, char *ifname,
561 int fd, vnet_hdr_required;
563 if (tap->has_ifname) {
564 pstrcpy(ifname, ifname_sz, tap->ifname);
566 assert(ifname_sz > 0);
570 if (tap->has_vnet_hdr) {
571 *vnet_hdr = tap->vnet_hdr;
572 vnet_hdr_required = *vnet_hdr;
575 vnet_hdr_required = 0;
578 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
584 setup_script[0] != '\0' &&
585 strcmp(setup_script, "no") != 0 &&
586 launch_script(setup_script, ifname, fd)) {
594 static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
595 const char *model, const char *name,
596 const char *ifname, const char *script,
597 const char *downscript, const char *vhostfdname,
598 int vnet_hdr, int fd)
602 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
608 if (tap_set_sndbuf(s->fd, tap) < 0) {
613 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
614 } else if (tap->has_helper) {
615 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
618 const char *downscript;
620 downscript = tap->has_downscript ? tap->downscript :
621 DEFAULT_NETWORK_DOWN_SCRIPT;
623 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
624 "ifname=%s,script=%s,downscript=%s", ifname, script,
627 if (strcmp(downscript, "no") != 0) {
628 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
629 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
634 if (tap->has_vhost ? tap->vhost :
635 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
638 if (tap->has_vhostfd) {
639 vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
647 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
648 tap->has_vhostforce && tap->vhostforce);
650 error_report("vhost-net requested but could not be initialized");
653 } else if (tap->has_vhostfd) {
654 error_report("vhostfd= is not valid without vhost");
661 int net_init_tap(const NetClientOptions *opts, const char *name,
662 NetClientState *peer)
664 const NetdevTapOptions *tap;
666 int fd, vnet_hdr = 0;
669 /* for the no-fd, no-helper case */
670 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
671 const char *downscript = NULL;
674 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
678 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
679 tap->has_vnet_hdr || tap->has_helper) {
680 error_report("ifname=, script=, downscript=, vnet_hdr=, "
681 "and helper= are invalid with fd=");
685 fd = monitor_handle_fd_param(cur_mon, tap->fd);
690 fcntl(fd, F_SETFL, O_NONBLOCK);
692 vnet_hdr = tap_probe_vnet_hdr(fd);
696 } else if (tap->has_helper) {
697 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
699 error_report("ifname=, script=, downscript=, and vnet_hdr= "
700 "are invalid with helper=");
704 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
709 fcntl(fd, F_SETFL, O_NONBLOCK);
711 vnet_hdr = tap_probe_vnet_hdr(fd);
716 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
717 downscript = tap->has_downscript ? tap->downscript :
718 DEFAULT_NETWORK_DOWN_SCRIPT;
719 fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
727 return net_init_tap_one(tap, peer, model, name, ifname, script,
728 downscript, tap->has_vhostfd ? tap->vhostfd : NULL,
732 VHostNetState *tap_get_vhost_net(NetClientState *nc)
734 TAPState *s = DO_UPCAST(TAPState, nc, nc);
735 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);