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];
63 VHostNetState *vhost_net;
64 unsigned host_vnet_hdr_len;
67 static int launch_script(const char *setup_script, const char *ifname, int fd);
69 static int tap_can_send(void *opaque);
70 static void tap_send(void *opaque);
71 static void tap_writable(void *opaque);
73 static void tap_update_fd_handler(TAPState *s)
75 qemu_set_fd_handler2(s->fd,
76 s->read_poll && s->enabled ? tap_can_send : NULL,
77 s->read_poll && s->enabled ? tap_send : NULL,
78 s->write_poll && s->enabled ? tap_writable : NULL,
82 static void tap_read_poll(TAPState *s, bool enable)
84 s->read_poll = enable;
85 tap_update_fd_handler(s);
88 static void tap_write_poll(TAPState *s, bool enable)
90 s->write_poll = enable;
91 tap_update_fd_handler(s);
94 static void tap_writable(void *opaque)
98 tap_write_poll(s, false);
100 qemu_flush_queued_packets(&s->nc);
103 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
108 len = writev(s->fd, iov, iovcnt);
109 } while (len == -1 && errno == EINTR);
111 if (len == -1 && errno == EAGAIN) {
112 tap_write_poll(s, true);
119 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
122 TAPState *s = DO_UPCAST(TAPState, nc, nc);
123 const struct iovec *iovp = iov;
124 struct iovec iov_copy[iovcnt + 1];
125 struct virtio_net_hdr_mrg_rxbuf hdr = { };
127 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
128 iov_copy[0].iov_base = &hdr;
129 iov_copy[0].iov_len = s->host_vnet_hdr_len;
130 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
135 return tap_write_packet(s, iovp, iovcnt);
138 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
140 TAPState *s = DO_UPCAST(TAPState, nc, nc);
143 struct virtio_net_hdr_mrg_rxbuf hdr = { };
145 if (s->host_vnet_hdr_len) {
146 iov[iovcnt].iov_base = &hdr;
147 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
151 iov[iovcnt].iov_base = (char *)buf;
152 iov[iovcnt].iov_len = size;
155 return tap_write_packet(s, iov, iovcnt);
158 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
160 TAPState *s = DO_UPCAST(TAPState, nc, nc);
163 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
164 return tap_receive_raw(nc, buf, size);
167 iov[0].iov_base = (char *)buf;
168 iov[0].iov_len = size;
170 return tap_write_packet(s, iov, 1);
173 static int tap_can_send(void *opaque)
175 TAPState *s = opaque;
177 return qemu_can_send_packet(&s->nc);
181 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
183 return read(tapfd, buf, maxlen);
187 static void tap_send_completed(NetClientState *nc, ssize_t len)
189 TAPState *s = DO_UPCAST(TAPState, nc, nc);
190 tap_read_poll(s, true);
193 static void tap_send(void *opaque)
195 TAPState *s = opaque;
199 uint8_t *buf = s->buf;
201 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
206 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
207 buf += s->host_vnet_hdr_len;
208 size -= s->host_vnet_hdr_len;
211 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
213 tap_read_poll(s, false);
215 } while (size > 0 && qemu_can_send_packet(&s->nc));
218 bool tap_has_ufo(NetClientState *nc)
220 TAPState *s = DO_UPCAST(TAPState, nc, nc);
222 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
227 int tap_has_vnet_hdr(NetClientState *nc)
229 TAPState *s = DO_UPCAST(TAPState, nc, nc);
231 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
233 return !!s->host_vnet_hdr_len;
236 int tap_has_vnet_hdr_len(NetClientState *nc, int len)
238 TAPState *s = DO_UPCAST(TAPState, nc, nc);
240 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
242 return tap_probe_vnet_hdr_len(s->fd, len);
245 void tap_set_vnet_hdr_len(NetClientState *nc, int len)
247 TAPState *s = DO_UPCAST(TAPState, nc, nc);
249 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
250 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
251 len == sizeof(struct virtio_net_hdr));
253 tap_fd_set_vnet_hdr_len(s->fd, len);
254 s->host_vnet_hdr_len = len;
257 void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
259 TAPState *s = DO_UPCAST(TAPState, nc, nc);
261 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
262 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
264 s->using_vnet_hdr = using_vnet_hdr;
267 void tap_set_offload(NetClientState *nc, int csum, int tso4,
268 int tso6, int ecn, int ufo)
270 TAPState *s = DO_UPCAST(TAPState, nc, nc);
275 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
278 static void tap_cleanup(NetClientState *nc)
280 TAPState *s = DO_UPCAST(TAPState, nc, nc);
283 vhost_net_cleanup(s->vhost_net);
287 qemu_purge_queued_packets(nc);
289 if (s->down_script[0])
290 launch_script(s->down_script, s->down_script_arg, s->fd);
292 tap_read_poll(s, false);
293 tap_write_poll(s, false);
298 static void tap_poll(NetClientState *nc, bool enable)
300 TAPState *s = DO_UPCAST(TAPState, nc, nc);
301 tap_read_poll(s, enable);
302 tap_write_poll(s, enable);
305 int tap_get_fd(NetClientState *nc)
307 TAPState *s = DO_UPCAST(TAPState, nc, nc);
308 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
314 static NetClientInfo net_tap_info = {
315 .type = NET_CLIENT_OPTIONS_KIND_TAP,
316 .size = sizeof(TAPState),
317 .receive = tap_receive,
318 .receive_raw = tap_receive_raw,
319 .receive_iov = tap_receive_iov,
321 .cleanup = tap_cleanup,
324 static TAPState *net_tap_fd_init(NetClientState *peer,
333 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
335 s = DO_UPCAST(TAPState, nc, nc);
338 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
339 s->using_vnet_hdr = false;
340 s->has_ufo = tap_probe_has_ufo(s->fd);
342 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
344 * Make sure host header length is set correctly in tap:
345 * it might have been modified by another instance of qemu.
347 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
348 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
350 tap_read_poll(s, true);
355 static int launch_script(const char *setup_script, const char *ifname, int fd)
361 /* try to launch network script */
364 int open_max = sysconf(_SC_OPEN_MAX), i;
366 for (i = 0; i < open_max; i++) {
367 if (i != STDIN_FILENO &&
368 i != STDOUT_FILENO &&
369 i != STDERR_FILENO &&
375 *parg++ = (char *)setup_script;
376 *parg++ = (char *)ifname;
378 execv(setup_script, args);
380 } else if (pid > 0) {
381 while (waitpid(pid, &status, 0) != pid) {
385 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
389 fprintf(stderr, "%s: could not launch network script\n", setup_script);
393 static int recv_fd(int c)
396 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
397 struct msghdr msg = {
398 .msg_control = msgbuf,
399 .msg_controllen = sizeof(msgbuf),
401 struct cmsghdr *cmsg;
406 cmsg = CMSG_FIRSTHDR(&msg);
407 cmsg->cmsg_level = SOL_SOCKET;
408 cmsg->cmsg_type = SCM_RIGHTS;
409 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
410 msg.msg_controllen = cmsg->cmsg_len;
413 iov.iov_len = sizeof(req);
418 len = recvmsg(c, &msg, 0);
420 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
427 static int net_bridge_run_helper(const char *helper, const char *bridge)
429 sigset_t oldmask, mask;
436 sigaddset(&mask, SIGCHLD);
437 sigprocmask(SIG_BLOCK, &mask, &oldmask);
439 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
443 /* try to launch bridge helper */
446 int open_max = sysconf(_SC_OPEN_MAX), i;
448 char br_buf[6+IFNAMSIZ] = {0};
449 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
451 for (i = 0; i < open_max; i++) {
452 if (i != STDIN_FILENO &&
453 i != STDOUT_FILENO &&
454 i != STDERR_FILENO &&
460 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
462 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
463 /* assume helper is a command */
465 if (strstr(helper, "--br=") == NULL) {
466 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
469 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
470 helper, "--use-vnet", fd_buf, br_buf);
473 *parg++ = (char *)"sh";
474 *parg++ = (char *)"-c";
475 *parg++ = helper_cmd;
478 execv("/bin/sh", args);
480 /* assume helper is just the executable path name */
482 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
485 *parg++ = (char *)helper;
486 *parg++ = (char *)"--use-vnet";
495 } else if (pid > 0) {
502 } while (fd == -1 && errno == EINTR);
506 while (waitpid(pid, &status, 0) != pid) {
509 sigprocmask(SIG_SETMASK, &oldmask, NULL);
511 fprintf(stderr, "failed to recv file descriptor\n");
515 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
519 fprintf(stderr, "failed to launch bridge helper\n");
523 int net_init_bridge(const NetClientOptions *opts, const char *name,
524 NetClientState *peer)
526 const NetdevBridgeOptions *bridge;
527 const char *helper, *br;
532 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
533 bridge = opts->bridge;
535 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
536 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
538 fd = net_bridge_run_helper(helper, br);
543 fcntl(fd, F_SETFL, O_NONBLOCK);
545 vnet_hdr = tap_probe_vnet_hdr(fd);
547 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
553 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
559 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
560 const char *setup_script, char *ifname,
561 size_t ifname_sz, int mq_required)
563 int fd, vnet_hdr_required;
565 if (tap->has_vnet_hdr) {
566 *vnet_hdr = tap->vnet_hdr;
567 vnet_hdr_required = *vnet_hdr;
570 vnet_hdr_required = 0;
573 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
580 setup_script[0] != '\0' &&
581 strcmp(setup_script, "no") != 0 &&
582 launch_script(setup_script, ifname, fd)) {
590 #define MAX_TAP_QUEUES 1024
592 static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
593 const char *model, const char *name,
594 const char *ifname, const char *script,
595 const char *downscript, const char *vhostfdname,
596 int vnet_hdr, int fd)
600 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
606 if (tap_set_sndbuf(s->fd, tap) < 0) {
610 if (tap->has_fd || tap->has_fds) {
611 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
612 } else if (tap->has_helper) {
613 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
616 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
617 "ifname=%s,script=%s,downscript=%s", ifname, script,
620 if (strcmp(downscript, "no") != 0) {
621 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
622 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
627 if (tap->has_vhost ? tap->vhost :
628 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
631 if (tap->has_vhostfd) {
632 vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
640 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
641 tap->has_vhostforce && tap->vhostforce);
643 error_report("vhost-net requested but could not be initialized");
646 } else if (tap->has_vhostfd || tap->has_vhostfds) {
647 error_report("vhostfd= is not valid without vhost");
654 static int get_fds(char *str, char *fds[], int max)
656 char *ptr = str, *this;
657 size_t len = strlen(str);
660 while (i < max && ptr < str + len) {
661 this = strchr(ptr, ':');
664 fds[i] = g_strdup(ptr);
666 fds[i] = g_strndup(ptr, this - ptr);
680 int net_init_tap(const NetClientOptions *opts, const char *name,
681 NetClientState *peer)
683 const NetdevTapOptions *tap;
684 int fd, vnet_hdr = 0, i = 0, queues;
685 /* for the no-fd, no-helper case */
686 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
687 const char *downscript = NULL;
688 const char *vhostfdname;
691 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
693 queues = tap->has_queues ? tap->queues : 1;
694 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
697 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
698 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
700 error_report("ifname=, script=, downscript=, vnet_hdr=, "
701 "helper=, queues=, and fds= are invalid with fd=");
705 fd = monitor_handle_fd_param(cur_mon, tap->fd);
710 fcntl(fd, F_SETFL, O_NONBLOCK);
712 vnet_hdr = tap_probe_vnet_hdr(fd);
714 if (net_init_tap_one(tap, peer, "tap", NULL, NULL,
716 vhostfdname, vnet_hdr, fd)) {
719 } else if (tap->has_fds) {
720 char *fds[MAX_TAP_QUEUES];
721 char *vhost_fds[MAX_TAP_QUEUES];
724 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
725 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
727 error_report("ifname=, script=, downscript=, vnet_hdr=, "
728 "helper=, queues=, and fd= are invalid with fds=");
732 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
733 if (tap->has_vhostfds) {
734 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
735 if (nfds != nvhosts) {
736 error_report("The number of fds passed does not match the "
737 "number of vhostfds passed");
742 for (i = 0; i < nfds; i++) {
743 fd = monitor_handle_fd_param(cur_mon, fds[i]);
748 fcntl(fd, F_SETFL, O_NONBLOCK);
751 vnet_hdr = tap_probe_vnet_hdr(fd);
752 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
753 error_report("vnet_hdr not consistent across given tap fds");
757 if (net_init_tap_one(tap, peer, "tap", name, ifname,
759 tap->has_vhostfds ? vhost_fds[i] : NULL,
764 } else if (tap->has_helper) {
765 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
766 tap->has_vnet_hdr || tap->has_queues || tap->has_fds) {
767 error_report("ifname=, script=, downscript=, and vnet_hdr= "
768 "queues=, and fds= are invalid with helper=");
772 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
777 fcntl(fd, F_SETFL, O_NONBLOCK);
778 vnet_hdr = tap_probe_vnet_hdr(fd);
780 if (net_init_tap_one(tap, peer, "bridge", name, ifname,
781 script, downscript, vhostfdname,
786 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
787 downscript = tap->has_downscript ? tap->downscript :
788 DEFAULT_NETWORK_DOWN_SCRIPT;
790 if (tap->has_ifname) {
791 pstrcpy(ifname, sizeof ifname, tap->ifname);
796 for (i = 0; i < queues; i++) {
797 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
798 ifname, sizeof ifname, queues > 1);
803 if (queues > 1 && i == 0 && !tap->has_ifname) {
804 if (tap_fd_get_ifname(fd, ifname)) {
805 error_report("Fail to get ifname");
810 if (net_init_tap_one(tap, peer, "tap", name, ifname,
811 i >= 1 ? "no" : script,
812 i >= 1 ? "no" : downscript,
813 vhostfdname, vnet_hdr, fd)) {
822 VHostNetState *tap_get_vhost_net(NetClientState *nc)
824 TAPState *s = DO_UPCAST(TAPState, nc, nc);
825 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
829 int tap_enable(NetClientState *nc)
831 TAPState *s = DO_UPCAST(TAPState, nc, nc);
837 ret = tap_fd_enable(s->fd);
840 tap_update_fd_handler(s);
846 int tap_disable(NetClientState *nc)
848 TAPState *s = DO_UPCAST(TAPState, nc, nc);
851 if (s->enabled == 0) {
854 ret = tap_fd_disable(s->fd);
856 qemu_purge_queued_packets(nc);
858 tap_update_fd_handler(s);