qapi/qom: Add ObjectOptions for rng-*, deprecate 'opened'
[qemu/ar7.git] / net / tap.c
blob12a08d54fe40be069488c63f821b1dd89a1068cf
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 "qemu/osdep.h"
27 #include "tap_int.h"
30 #include <sys/ioctl.h>
31 #include <sys/wait.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
35 #include "net/net.h"
36 #include "clients.h"
37 #include "monitor/monitor.h"
38 #include "sysemu/sysemu.h"
39 #include "qapi/error.h"
40 #include "qemu-common.h"
41 #include "qemu/cutils.h"
42 #include "qemu/error-report.h"
43 #include "qemu/main-loop.h"
44 #include "qemu/sockets.h"
46 #include "net/tap.h"
48 #include "net/vhost_net.h"
50 typedef struct TAPState {
51 NetClientState nc;
52 int fd;
53 char down_script[1024];
54 char down_script_arg[128];
55 uint8_t buf[NET_BUFSIZE];
56 bool read_poll;
57 bool write_poll;
58 bool using_vnet_hdr;
59 bool has_ufo;
60 bool enabled;
61 VHostNetState *vhost_net;
62 unsigned host_vnet_hdr_len;
63 Notifier exit;
64 } TAPState;
66 static void launch_script(const char *setup_script, const char *ifname,
67 int fd, Error **errp);
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_handler(s->fd,
75 s->read_poll && s->enabled ? tap_send : NULL,
76 s->write_poll && s->enabled ? tap_writable : NULL,
77 s);
80 static void tap_read_poll(TAPState *s, bool enable)
82 s->read_poll = enable;
83 tap_update_fd_handler(s);
86 static void tap_write_poll(TAPState *s, bool enable)
88 s->write_poll = enable;
89 tap_update_fd_handler(s);
92 static void tap_writable(void *opaque)
94 TAPState *s = opaque;
96 tap_write_poll(s, false);
98 qemu_flush_queued_packets(&s->nc);
101 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
103 ssize_t len;
105 do {
106 len = writev(s->fd, iov, iovcnt);
107 } while (len == -1 && errno == EINTR);
109 if (len == -1 && errno == EAGAIN) {
110 tap_write_poll(s, true);
111 return 0;
114 return len;
117 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
118 int iovcnt)
120 TAPState *s = DO_UPCAST(TAPState, nc, nc);
121 const struct iovec *iovp = iov;
122 struct iovec iov_copy[iovcnt + 1];
123 struct virtio_net_hdr_mrg_rxbuf hdr = { };
125 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
126 iov_copy[0].iov_base = &hdr;
127 iov_copy[0].iov_len = s->host_vnet_hdr_len;
128 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
129 iovp = iov_copy;
130 iovcnt++;
133 return tap_write_packet(s, iovp, iovcnt);
136 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
138 TAPState *s = DO_UPCAST(TAPState, nc, nc);
139 struct iovec iov[2];
140 int iovcnt = 0;
141 struct virtio_net_hdr_mrg_rxbuf hdr = { };
143 if (s->host_vnet_hdr_len) {
144 iov[iovcnt].iov_base = &hdr;
145 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
146 iovcnt++;
149 iov[iovcnt].iov_base = (char *)buf;
150 iov[iovcnt].iov_len = size;
151 iovcnt++;
153 return tap_write_packet(s, iov, iovcnt);
156 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
158 TAPState *s = DO_UPCAST(TAPState, nc, nc);
159 struct iovec iov[1];
161 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
162 return tap_receive_raw(nc, buf, size);
165 iov[0].iov_base = (char *)buf;
166 iov[0].iov_len = size;
168 return tap_write_packet(s, iov, 1);
171 #ifndef __sun__
172 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
174 return read(tapfd, buf, maxlen);
176 #endif
178 static void tap_send_completed(NetClientState *nc, ssize_t len)
180 TAPState *s = DO_UPCAST(TAPState, nc, nc);
181 tap_read_poll(s, true);
184 static void tap_send(void *opaque)
186 TAPState *s = opaque;
187 int size;
188 int packets = 0;
190 while (true) {
191 uint8_t *buf = s->buf;
193 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
194 if (size <= 0) {
195 break;
198 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
199 buf += s->host_vnet_hdr_len;
200 size -= s->host_vnet_hdr_len;
203 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
204 if (size == 0) {
205 tap_read_poll(s, false);
206 break;
207 } else if (size < 0) {
208 break;
212 * When the host keeps receiving more packets while tap_send() is
213 * running we can hog the QEMU global mutex. Limit the number of
214 * packets that are processed per tap_send() callback to prevent
215 * stalling the guest.
217 packets++;
218 if (packets >= 50) {
219 break;
224 static bool tap_has_ufo(NetClientState *nc)
226 TAPState *s = DO_UPCAST(TAPState, nc, nc);
228 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
230 return s->has_ufo;
233 static bool tap_has_vnet_hdr(NetClientState *nc)
235 TAPState *s = DO_UPCAST(TAPState, nc, nc);
237 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
239 return !!s->host_vnet_hdr_len;
242 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
244 TAPState *s = DO_UPCAST(TAPState, nc, nc);
246 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
248 return !!tap_probe_vnet_hdr_len(s->fd, len);
251 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
253 TAPState *s = DO_UPCAST(TAPState, nc, nc);
255 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
256 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
257 len == sizeof(struct virtio_net_hdr) ||
258 len == sizeof(struct virtio_net_hdr_v1_hash));
260 tap_fd_set_vnet_hdr_len(s->fd, len);
261 s->host_vnet_hdr_len = len;
264 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
266 TAPState *s = DO_UPCAST(TAPState, nc, nc);
268 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
269 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
271 s->using_vnet_hdr = using_vnet_hdr;
274 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
276 TAPState *s = DO_UPCAST(TAPState, nc, nc);
278 return tap_fd_set_vnet_le(s->fd, is_le);
281 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
283 TAPState *s = DO_UPCAST(TAPState, nc, nc);
285 return tap_fd_set_vnet_be(s->fd, is_be);
288 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
289 int tso6, int ecn, int ufo)
291 TAPState *s = DO_UPCAST(TAPState, nc, nc);
292 if (s->fd < 0) {
293 return;
296 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
299 static void tap_exit_notify(Notifier *notifier, void *data)
301 TAPState *s = container_of(notifier, TAPState, exit);
302 Error *err = NULL;
304 if (s->down_script[0]) {
305 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
306 if (err) {
307 error_report_err(err);
312 static void tap_cleanup(NetClientState *nc)
314 TAPState *s = DO_UPCAST(TAPState, nc, nc);
316 if (s->vhost_net) {
317 vhost_net_cleanup(s->vhost_net);
318 g_free(s->vhost_net);
319 s->vhost_net = NULL;
322 qemu_purge_queued_packets(nc);
324 tap_exit_notify(&s->exit, NULL);
325 qemu_remove_exit_notifier(&s->exit);
327 tap_read_poll(s, false);
328 tap_write_poll(s, false);
329 close(s->fd);
330 s->fd = -1;
333 static void tap_poll(NetClientState *nc, bool enable)
335 TAPState *s = DO_UPCAST(TAPState, nc, nc);
336 tap_read_poll(s, enable);
337 tap_write_poll(s, enable);
340 int tap_get_fd(NetClientState *nc)
342 TAPState *s = DO_UPCAST(TAPState, nc, nc);
343 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
344 return s->fd;
347 /* fd support */
349 static NetClientInfo net_tap_info = {
350 .type = NET_CLIENT_DRIVER_TAP,
351 .size = sizeof(TAPState),
352 .receive = tap_receive,
353 .receive_raw = tap_receive_raw,
354 .receive_iov = tap_receive_iov,
355 .poll = tap_poll,
356 .cleanup = tap_cleanup,
357 .has_ufo = tap_has_ufo,
358 .has_vnet_hdr = tap_has_vnet_hdr,
359 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
360 .using_vnet_hdr = tap_using_vnet_hdr,
361 .set_offload = tap_set_offload,
362 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
363 .set_vnet_le = tap_set_vnet_le,
364 .set_vnet_be = tap_set_vnet_be,
367 static TAPState *net_tap_fd_init(NetClientState *peer,
368 const char *model,
369 const char *name,
370 int fd,
371 int vnet_hdr)
373 NetClientState *nc;
374 TAPState *s;
376 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
378 s = DO_UPCAST(TAPState, nc, nc);
380 s->fd = fd;
381 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
382 s->using_vnet_hdr = false;
383 s->has_ufo = tap_probe_has_ufo(s->fd);
384 s->enabled = true;
385 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
387 * Make sure host header length is set correctly in tap:
388 * it might have been modified by another instance of qemu.
390 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
391 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
393 tap_read_poll(s, true);
394 s->vhost_net = NULL;
396 s->exit.notify = tap_exit_notify;
397 qemu_add_exit_notifier(&s->exit);
399 return s;
402 static void launch_script(const char *setup_script, const char *ifname,
403 int fd, Error **errp)
405 int pid, status;
406 char *args[3];
407 char **parg;
409 /* try to launch network script */
410 pid = fork();
411 if (pid < 0) {
412 error_setg_errno(errp, errno, "could not launch network script %s",
413 setup_script);
414 return;
416 if (pid == 0) {
417 int open_max = sysconf(_SC_OPEN_MAX), i;
419 for (i = 3; i < open_max; i++) {
420 if (i != fd) {
421 close(i);
424 parg = args;
425 *parg++ = (char *)setup_script;
426 *parg++ = (char *)ifname;
427 *parg = NULL;
428 execv(setup_script, args);
429 _exit(1);
430 } else {
431 while (waitpid(pid, &status, 0) != pid) {
432 /* loop */
435 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
436 return;
438 error_setg(errp, "network script %s failed with status %d",
439 setup_script, status);
443 static int recv_fd(int c)
445 int fd;
446 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
447 struct msghdr msg = {
448 .msg_control = msgbuf,
449 .msg_controllen = sizeof(msgbuf),
451 struct cmsghdr *cmsg;
452 struct iovec iov;
453 uint8_t req[1];
454 ssize_t len;
456 cmsg = CMSG_FIRSTHDR(&msg);
457 cmsg->cmsg_level = SOL_SOCKET;
458 cmsg->cmsg_type = SCM_RIGHTS;
459 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
460 msg.msg_controllen = cmsg->cmsg_len;
462 iov.iov_base = req;
463 iov.iov_len = sizeof(req);
465 msg.msg_iov = &iov;
466 msg.msg_iovlen = 1;
468 len = recvmsg(c, &msg, 0);
469 if (len > 0) {
470 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
471 return fd;
474 return len;
477 static int net_bridge_run_helper(const char *helper, const char *bridge,
478 Error **errp)
480 sigset_t oldmask, mask;
481 g_autofree char *default_helper = NULL;
482 int pid, status;
483 char *args[5];
484 char **parg;
485 int sv[2];
487 sigemptyset(&mask);
488 sigaddset(&mask, SIGCHLD);
489 sigprocmask(SIG_BLOCK, &mask, &oldmask);
491 if (!helper) {
492 helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
495 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
496 error_setg_errno(errp, errno, "socketpair() failed");
497 return -1;
500 /* try to launch bridge helper */
501 pid = fork();
502 if (pid < 0) {
503 error_setg_errno(errp, errno, "Can't fork bridge helper");
504 return -1;
506 if (pid == 0) {
507 int open_max = sysconf(_SC_OPEN_MAX), i;
508 char *fd_buf = NULL;
509 char *br_buf = NULL;
510 char *helper_cmd = NULL;
512 for (i = 3; i < open_max; i++) {
513 if (i != sv[1]) {
514 close(i);
518 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
520 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
521 /* assume helper is a command */
523 if (strstr(helper, "--br=") == NULL) {
524 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
527 helper_cmd = g_strdup_printf("%s %s %s %s", helper,
528 "--use-vnet", fd_buf, br_buf ? br_buf : "");
530 parg = args;
531 *parg++ = (char *)"sh";
532 *parg++ = (char *)"-c";
533 *parg++ = helper_cmd;
534 *parg++ = NULL;
536 execv("/bin/sh", args);
537 g_free(helper_cmd);
538 } else {
539 /* assume helper is just the executable path name */
541 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
543 parg = args;
544 *parg++ = (char *)helper;
545 *parg++ = (char *)"--use-vnet";
546 *parg++ = fd_buf;
547 *parg++ = br_buf;
548 *parg++ = NULL;
550 execv(helper, args);
552 g_free(fd_buf);
553 g_free(br_buf);
554 _exit(1);
556 } else {
557 int fd;
558 int saved_errno;
560 close(sv[1]);
562 do {
563 fd = recv_fd(sv[0]);
564 } while (fd == -1 && errno == EINTR);
565 saved_errno = errno;
567 close(sv[0]);
569 while (waitpid(pid, &status, 0) != pid) {
570 /* loop */
572 sigprocmask(SIG_SETMASK, &oldmask, NULL);
573 if (fd < 0) {
574 error_setg_errno(errp, saved_errno,
575 "failed to recv file descriptor");
576 return -1;
578 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
579 error_setg(errp, "bridge helper failed");
580 return -1;
582 return fd;
586 int net_init_bridge(const Netdev *netdev, const char *name,
587 NetClientState *peer, Error **errp)
589 const NetdevBridgeOptions *bridge;
590 const char *helper, *br;
591 TAPState *s;
592 int fd, vnet_hdr;
593 NetdevBridgeOptions *stored;
595 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
596 bridge = &netdev->u.bridge;
597 helper = bridge->has_helper ? bridge->helper : NULL;
598 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
600 fd = net_bridge_run_helper(helper, br, errp);
601 if (fd == -1) {
602 return -1;
605 qemu_set_nonblock(fd);
606 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
607 if (vnet_hdr < 0) {
608 close(fd);
609 return -1;
611 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
613 /* Store startup parameters */
614 s->nc.stored_config = g_new0(NetdevInfo, 1);
615 s->nc.stored_config->type = NET_BACKEND_BRIDGE;
616 stored = &s->nc.stored_config->u.bridge;
618 if (br) {
619 stored->has_br = true;
620 stored->br = g_strdup(br);
623 if (helper) {
624 stored->has_helper = true;
625 stored->helper = g_strdup(helper);
628 return 0;
631 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
632 const char *setup_script, char *ifname,
633 size_t ifname_sz, int mq_required, Error **errp)
635 Error *err = NULL;
636 int fd, vnet_hdr_required;
638 if (tap->has_vnet_hdr) {
639 *vnet_hdr = tap->vnet_hdr;
640 vnet_hdr_required = *vnet_hdr;
641 } else {
642 *vnet_hdr = 1;
643 vnet_hdr_required = 0;
646 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
647 mq_required, errp));
648 if (fd < 0) {
649 return -1;
652 if (setup_script &&
653 setup_script[0] != '\0' &&
654 strcmp(setup_script, "no") != 0) {
655 launch_script(setup_script, ifname, fd, &err);
656 if (err) {
657 error_propagate(errp, err);
658 close(fd);
659 return -1;
663 return fd;
666 #define MAX_TAP_QUEUES 1024
668 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
669 const char *model, const char *name,
670 const char *ifname, const char *script,
671 const char *downscript, const char *vhostfdname,
672 int vnet_hdr, int fd, NetdevInfo **common_stored,
673 Error **errp)
675 Error *err = NULL;
676 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
677 int vhostfd;
678 NetdevTapOptions *stored;
680 tap_set_sndbuf(s->fd, tap, &err);
681 if (err) {
682 error_propagate(errp, err);
683 return;
686 /* Store startup parameters */
687 if (!*common_stored) {
688 *common_stored = g_new0(NetdevInfo, 1);
689 (*common_stored)->type = NET_BACKEND_TAP;
690 s->nc.stored_config = *common_stored;
692 stored = &(*common_stored)->u.tap;
694 if (tap->has_sndbuf && !stored->has_sndbuf) {
695 stored->has_sndbuf = true;
696 stored->sndbuf = tap->sndbuf;
699 if (vnet_hdr && !stored->has_vnet_hdr) {
700 stored->has_vnet_hdr = true;
701 stored->vnet_hdr = true;
704 if (tap->has_fd || tap->has_fds) {
705 if (!stored->has_fds) {
706 stored->has_fds = true;
707 stored->fds = g_strdup_printf("%d", fd);
708 } else {
709 char *tmp_s = stored->fds;
710 stored->fds = g_strdup_printf("%s:%d", stored->fds, fd);
711 g_free(tmp_s);
713 } else if (tap->has_helper) {
714 if (!stored->has_helper) {
715 stored->has_helper = true;
716 stored->helper = g_strdup(tap->helper);
719 if (!stored->has_br) {
720 stored->has_br = true;
721 stored->br = tap->has_br ? g_strdup(tap->br) :
722 g_strdup(DEFAULT_BRIDGE_INTERFACE);
724 } else {
725 if (ifname && !stored->has_ifname) {
726 stored->has_ifname = true;
727 stored->ifname = g_strdup(ifname);
730 if (script && !stored->has_script) {
731 stored->has_script = true;
732 stored->script = g_strdup(script);
735 if (downscript && !stored->has_downscript) {
736 stored->has_downscript = true;
737 stored->downscript = g_strdup(downscript);
740 if (strcmp(downscript, "no") != 0) {
741 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
742 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
743 "%s", ifname);
747 if (tap->has_vhost ? tap->vhost :
748 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
749 VhostNetOptions options;
751 stored->has_vhost = true;
752 stored->vhost = true;
754 if (tap->has_vhostforce && tap->vhostforce) {
755 stored->has_vhostforce = true;
756 stored->vhostforce = true;
759 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
760 options.net_backend = &s->nc;
761 if (tap->has_poll_us) {
762 stored->has_poll_us = true;
763 stored->poll_us = tap->poll_us;
765 options.busyloop_timeout = tap->poll_us;
766 } else {
767 options.busyloop_timeout = 0;
770 if (vhostfdname) {
771 int ret;
773 vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
774 if (vhostfd == -1) {
775 if (tap->has_vhostforce && tap->vhostforce) {
776 error_propagate(errp, err);
777 } else {
778 warn_report_err(err);
780 return;
782 ret = qemu_try_set_nonblock(vhostfd);
783 if (ret < 0) {
784 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
785 name, fd);
786 return;
788 } else {
789 vhostfd = open("/dev/vhost-net", O_RDWR);
790 if (vhostfd < 0) {
791 if (tap->has_vhostforce && tap->vhostforce) {
792 error_setg_errno(errp, errno,
793 "tap: open vhost char device failed");
794 } else {
795 warn_report("tap: open vhost char device failed: %s",
796 strerror(errno));
798 return;
800 qemu_set_nonblock(vhostfd);
802 options.opaque = (void *)(uintptr_t)vhostfd;
804 if (!stored->has_vhostfds) {
805 stored->has_vhostfds = true;
806 stored->vhostfds = g_strdup_printf("%d", vhostfd);
807 } else {
808 char *tmp_s = stored->vhostfds;
809 stored->vhostfds = g_strdup_printf("%s:%d", stored->fds, vhostfd);
810 g_free(tmp_s);
813 s->vhost_net = vhost_net_init(&options);
814 if (!s->vhost_net) {
815 if (tap->has_vhostforce && tap->vhostforce) {
816 error_setg(errp, VHOST_NET_INIT_FAILED);
817 } else {
818 warn_report(VHOST_NET_INIT_FAILED);
820 return;
822 } else if (vhostfdname) {
823 error_setg(errp, "vhostfd(s)= is not valid without vhost");
827 static int get_fds(char *str, char *fds[], int max)
829 char *ptr = str, *this;
830 size_t len = strlen(str);
831 int i = 0;
833 while (i < max && ptr < str + len) {
834 this = strchr(ptr, ':');
836 if (this == NULL) {
837 fds[i] = g_strdup(ptr);
838 } else {
839 fds[i] = g_strndup(ptr, this - ptr);
842 i++;
843 if (this == NULL) {
844 break;
845 } else {
846 ptr = this + 1;
850 return i;
853 int net_init_tap(const Netdev *netdev, const char *name,
854 NetClientState *peer, Error **errp)
856 const NetdevTapOptions *tap;
857 int fd, vnet_hdr = 0, i = 0, queues;
858 /* for the no-fd, no-helper case */
859 const char *script;
860 const char *downscript;
861 Error *err = NULL;
862 const char *vhostfdname;
863 char ifname[128];
864 int ret = 0;
865 NetdevInfo *common_stored = NULL; /* will store configuration */
867 assert(netdev->type == NET_CLIENT_DRIVER_TAP);
868 tap = &netdev->u.tap;
869 queues = tap->has_queues ? tap->queues : 1;
870 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
871 script = tap->has_script ? tap->script : NULL;
872 downscript = tap->has_downscript ? tap->downscript : NULL;
874 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
875 * For -netdev, peer is always NULL. */
876 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
877 error_setg(errp, "Multiqueue tap cannot be used with hubs");
878 return -1;
881 if (tap->has_fd) {
882 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
883 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
884 tap->has_fds || tap->has_vhostfds) {
885 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
886 "helper=, queues=, fds=, and vhostfds= "
887 "are invalid with fd=");
888 return -1;
891 fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
892 if (fd == -1) {
893 return -1;
896 ret = qemu_try_set_nonblock(fd);
897 if (ret < 0) {
898 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
899 name, fd);
900 close(fd);
901 return -1;
904 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
905 if (vnet_hdr < 0) {
906 close(fd);
907 return -1;
910 net_init_tap_one(tap, peer, "tap", name, NULL,
911 script, downscript,
912 vhostfdname, vnet_hdr, fd, &common_stored, &err);
913 if (err) {
914 error_propagate(errp, err);
915 close(fd);
916 return -1;
918 } else if (tap->has_fds) {
919 char **fds;
920 char **vhost_fds;
921 int nfds = 0, nvhosts = 0;
923 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
924 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
925 tap->has_vhostfd) {
926 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
927 "helper=, queues=, and vhostfd= "
928 "are invalid with fds=");
929 return -1;
932 fds = g_new0(char *, MAX_TAP_QUEUES);
933 vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
935 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
936 if (tap->has_vhostfds) {
937 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
938 if (nfds != nvhosts) {
939 error_setg(errp, "The number of fds passed does not match "
940 "the number of vhostfds passed");
941 ret = -1;
942 goto free_fail;
946 for (i = 0; i < nfds; i++) {
947 fd = monitor_fd_param(monitor_cur(), fds[i], errp);
948 if (fd == -1) {
949 ret = -1;
950 goto free_fail;
953 ret = qemu_try_set_nonblock(fd);
954 if (ret < 0) {
955 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
956 name, fd);
957 goto free_fail;
960 if (i == 0) {
961 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
962 if (vnet_hdr < 0) {
963 goto free_fail;
965 } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
966 error_setg(errp,
967 "vnet_hdr not consistent across given tap fds");
968 ret = -1;
969 goto free_fail;
972 net_init_tap_one(tap, peer, "tap", name, ifname,
973 script, downscript,
974 tap->has_vhostfds ? vhost_fds[i] : NULL,
975 vnet_hdr, fd, &common_stored, &err);
976 if (err) {
977 error_propagate(errp, err);
978 ret = -1;
979 goto free_fail;
983 free_fail:
984 for (i = 0; i < nvhosts; i++) {
985 g_free(vhost_fds[i]);
987 for (i = 0; i < nfds; i++) {
988 g_free(fds[i]);
990 g_free(fds);
991 g_free(vhost_fds);
992 return ret;
993 } else if (tap->has_helper) {
994 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
995 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
996 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
997 "queues=, and vhostfds= are invalid with helper=");
998 return -1;
1001 fd = net_bridge_run_helper(tap->helper,
1002 tap->has_br ?
1003 tap->br : DEFAULT_BRIDGE_INTERFACE,
1004 errp);
1005 if (fd == -1) {
1006 return -1;
1009 qemu_set_nonblock(fd);
1010 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
1011 if (vnet_hdr < 0) {
1012 close(fd);
1013 return -1;
1016 net_init_tap_one(tap, peer, "bridge", name, ifname,
1017 script, downscript, vhostfdname,
1018 vnet_hdr, fd, &common_stored, &err);
1019 if (err) {
1020 error_propagate(errp, err);
1021 close(fd);
1022 return -1;
1024 } else {
1025 g_autofree char *default_script = NULL;
1026 g_autofree char *default_downscript = NULL;
1027 if (tap->has_vhostfds) {
1028 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
1029 return -1;
1032 if (!script) {
1033 script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
1035 if (!downscript) {
1036 downscript = default_downscript =
1037 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
1040 if (tap->has_ifname) {
1041 pstrcpy(ifname, sizeof ifname, tap->ifname);
1042 } else {
1043 ifname[0] = '\0';
1046 for (i = 0; i < queues; i++) {
1047 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
1048 ifname, sizeof ifname, queues > 1, errp);
1049 if (fd == -1) {
1050 return -1;
1053 if (queues > 1 && i == 0 && !tap->has_ifname) {
1054 if (tap_fd_get_ifname(fd, ifname)) {
1055 error_setg(errp, "Fail to get ifname");
1056 close(fd);
1057 return -1;
1061 net_init_tap_one(tap, peer, "tap", name, ifname,
1062 i >= 1 ? "no" : script,
1063 i >= 1 ? "no" : downscript,
1064 vhostfdname, vnet_hdr, fd,
1065 &common_stored, &err);
1066 if (err) {
1067 error_propagate(errp, err);
1068 close(fd);
1069 return -1;
1074 return 0;
1077 VHostNetState *tap_get_vhost_net(NetClientState *nc)
1079 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1080 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
1081 return s->vhost_net;
1084 int tap_enable(NetClientState *nc)
1086 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1087 int ret;
1089 if (s->enabled) {
1090 return 0;
1091 } else {
1092 ret = tap_fd_enable(s->fd);
1093 if (ret == 0) {
1094 s->enabled = true;
1095 tap_update_fd_handler(s);
1097 return ret;
1101 int tap_disable(NetClientState *nc)
1103 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1104 int ret;
1106 if (s->enabled == 0) {
1107 return 0;
1108 } else {
1109 ret = tap_fd_disable(s->fd);
1110 if (ret == 0) {
1111 qemu_purge_queued_packets(nc);
1112 s->enabled = false;
1113 tap_update_fd_handler(s);
1115 return ret;