gitlab: Use plain docker in container-template.yml
[qemu/kevin.git] / net / tap.c
blob7d7bc1dc5f702f2917ca78b1ae57cea23611f94d
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/eth.h"
36 #include "net/net.h"
37 #include "clients.h"
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qapi/error.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 len = RETRY_ON_EINTR(writev(s->fd, iov, iovcnt));
107 if (len == -1 && errno == EAGAIN) {
108 tap_write_poll(s, true);
109 return 0;
112 return len;
115 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
116 int iovcnt)
118 TAPState *s = DO_UPCAST(TAPState, nc, nc);
119 const struct iovec *iovp = iov;
120 struct iovec iov_copy[iovcnt + 1];
121 struct virtio_net_hdr_mrg_rxbuf hdr = { };
123 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
124 iov_copy[0].iov_base = &hdr;
125 iov_copy[0].iov_len = s->host_vnet_hdr_len;
126 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
127 iovp = iov_copy;
128 iovcnt++;
131 return tap_write_packet(s, iovp, iovcnt);
134 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
136 TAPState *s = DO_UPCAST(TAPState, nc, nc);
137 struct iovec iov[2];
138 int iovcnt = 0;
139 struct virtio_net_hdr_mrg_rxbuf hdr = { };
141 if (s->host_vnet_hdr_len) {
142 iov[iovcnt].iov_base = &hdr;
143 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
144 iovcnt++;
147 iov[iovcnt].iov_base = (char *)buf;
148 iov[iovcnt].iov_len = size;
149 iovcnt++;
151 return tap_write_packet(s, iov, iovcnt);
154 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
156 TAPState *s = DO_UPCAST(TAPState, nc, nc);
157 struct iovec iov[1];
159 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
160 return tap_receive_raw(nc, buf, size);
163 iov[0].iov_base = (char *)buf;
164 iov[0].iov_len = size;
166 return tap_write_packet(s, iov, 1);
169 #ifndef __sun__
170 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
172 return read(tapfd, buf, maxlen);
174 #endif
176 static void tap_send_completed(NetClientState *nc, ssize_t len)
178 TAPState *s = DO_UPCAST(TAPState, nc, nc);
179 tap_read_poll(s, true);
182 static void tap_send(void *opaque)
184 TAPState *s = opaque;
185 int size;
186 int packets = 0;
188 while (true) {
189 uint8_t *buf = s->buf;
190 uint8_t min_pkt[ETH_ZLEN];
191 size_t min_pktsz = sizeof(min_pkt);
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 if (net_peer_needs_padding(&s->nc)) {
204 if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) {
205 buf = min_pkt;
206 size = min_pktsz;
210 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
211 if (size == 0) {
212 tap_read_poll(s, false);
213 break;
214 } else if (size < 0) {
215 break;
219 * When the host keeps receiving more packets while tap_send() is
220 * running we can hog the QEMU global mutex. Limit the number of
221 * packets that are processed per tap_send() callback to prevent
222 * stalling the guest.
224 packets++;
225 if (packets >= 50) {
226 break;
231 static bool tap_has_ufo(NetClientState *nc)
233 TAPState *s = DO_UPCAST(TAPState, nc, nc);
235 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
237 return s->has_ufo;
240 static bool tap_has_vnet_hdr(NetClientState *nc)
242 TAPState *s = DO_UPCAST(TAPState, nc, nc);
244 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
246 return !!s->host_vnet_hdr_len;
249 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
251 TAPState *s = DO_UPCAST(TAPState, nc, nc);
253 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
255 return !!tap_probe_vnet_hdr_len(s->fd, len);
258 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
260 TAPState *s = DO_UPCAST(TAPState, nc, nc);
262 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
263 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
264 len == sizeof(struct virtio_net_hdr) ||
265 len == sizeof(struct virtio_net_hdr_v1_hash));
267 tap_fd_set_vnet_hdr_len(s->fd, len);
268 s->host_vnet_hdr_len = len;
271 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
273 TAPState *s = DO_UPCAST(TAPState, nc, nc);
275 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
276 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
278 s->using_vnet_hdr = using_vnet_hdr;
281 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
283 TAPState *s = DO_UPCAST(TAPState, nc, nc);
285 return tap_fd_set_vnet_le(s->fd, is_le);
288 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
290 TAPState *s = DO_UPCAST(TAPState, nc, nc);
292 return tap_fd_set_vnet_be(s->fd, is_be);
295 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
296 int tso6, int ecn, int ufo)
298 TAPState *s = DO_UPCAST(TAPState, nc, nc);
299 if (s->fd < 0) {
300 return;
303 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
306 static void tap_exit_notify(Notifier *notifier, void *data)
308 TAPState *s = container_of(notifier, TAPState, exit);
309 Error *err = NULL;
311 if (s->down_script[0]) {
312 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
313 if (err) {
314 error_report_err(err);
319 static void tap_cleanup(NetClientState *nc)
321 TAPState *s = DO_UPCAST(TAPState, nc, nc);
323 if (s->vhost_net) {
324 vhost_net_cleanup(s->vhost_net);
325 g_free(s->vhost_net);
326 s->vhost_net = NULL;
329 qemu_purge_queued_packets(nc);
331 tap_exit_notify(&s->exit, NULL);
332 qemu_remove_exit_notifier(&s->exit);
334 tap_read_poll(s, false);
335 tap_write_poll(s, false);
336 close(s->fd);
337 s->fd = -1;
340 static void tap_poll(NetClientState *nc, bool enable)
342 TAPState *s = DO_UPCAST(TAPState, nc, nc);
343 tap_read_poll(s, enable);
344 tap_write_poll(s, enable);
347 static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd)
349 TAPState *s = DO_UPCAST(TAPState, nc, nc);
350 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
352 return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0;
355 int tap_get_fd(NetClientState *nc)
357 TAPState *s = DO_UPCAST(TAPState, nc, nc);
358 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
359 return s->fd;
362 /* fd support */
364 static NetClientInfo net_tap_info = {
365 .type = NET_CLIENT_DRIVER_TAP,
366 .size = sizeof(TAPState),
367 .receive = tap_receive,
368 .receive_raw = tap_receive_raw,
369 .receive_iov = tap_receive_iov,
370 .poll = tap_poll,
371 .cleanup = tap_cleanup,
372 .has_ufo = tap_has_ufo,
373 .has_vnet_hdr = tap_has_vnet_hdr,
374 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
375 .using_vnet_hdr = tap_using_vnet_hdr,
376 .set_offload = tap_set_offload,
377 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
378 .set_vnet_le = tap_set_vnet_le,
379 .set_vnet_be = tap_set_vnet_be,
380 .set_steering_ebpf = tap_set_steering_ebpf,
383 static TAPState *net_tap_fd_init(NetClientState *peer,
384 const char *model,
385 const char *name,
386 int fd,
387 int vnet_hdr)
389 NetClientState *nc;
390 TAPState *s;
392 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
394 s = DO_UPCAST(TAPState, nc, nc);
396 s->fd = fd;
397 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
398 s->using_vnet_hdr = false;
399 s->has_ufo = tap_probe_has_ufo(s->fd);
400 s->enabled = true;
401 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
403 * Make sure host header length is set correctly in tap:
404 * it might have been modified by another instance of qemu.
406 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
407 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
409 tap_read_poll(s, true);
410 s->vhost_net = NULL;
412 s->exit.notify = tap_exit_notify;
413 qemu_add_exit_notifier(&s->exit);
415 return s;
418 static void launch_script(const char *setup_script, const char *ifname,
419 int fd, Error **errp)
421 int pid, status;
422 char *args[3];
423 char **parg;
425 /* try to launch network script */
426 pid = fork();
427 if (pid < 0) {
428 error_setg_errno(errp, errno, "could not launch network script %s",
429 setup_script);
430 return;
432 if (pid == 0) {
433 int open_max = sysconf(_SC_OPEN_MAX), i;
435 for (i = 3; i < open_max; i++) {
436 if (i != fd) {
437 close(i);
440 parg = args;
441 *parg++ = (char *)setup_script;
442 *parg++ = (char *)ifname;
443 *parg = NULL;
444 execv(setup_script, args);
445 _exit(1);
446 } else {
447 while (waitpid(pid, &status, 0) != pid) {
448 /* loop */
451 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
452 return;
454 error_setg(errp, "network script %s failed with status %d",
455 setup_script, status);
459 static int recv_fd(int c)
461 int fd;
462 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
463 struct msghdr msg = {
464 .msg_control = msgbuf,
465 .msg_controllen = sizeof(msgbuf),
467 struct cmsghdr *cmsg;
468 struct iovec iov;
469 uint8_t req[1];
470 ssize_t len;
472 cmsg = CMSG_FIRSTHDR(&msg);
473 cmsg->cmsg_level = SOL_SOCKET;
474 cmsg->cmsg_type = SCM_RIGHTS;
475 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
476 msg.msg_controllen = cmsg->cmsg_len;
478 iov.iov_base = req;
479 iov.iov_len = sizeof(req);
481 msg.msg_iov = &iov;
482 msg.msg_iovlen = 1;
484 len = recvmsg(c, &msg, 0);
485 if (len > 0) {
486 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
487 return fd;
490 return len;
493 static int net_bridge_run_helper(const char *helper, const char *bridge,
494 Error **errp)
496 sigset_t oldmask, mask;
497 g_autofree char *default_helper = NULL;
498 int pid, status;
499 char *args[5];
500 char **parg;
501 int sv[2];
503 sigemptyset(&mask);
504 sigaddset(&mask, SIGCHLD);
505 sigprocmask(SIG_BLOCK, &mask, &oldmask);
507 if (!helper) {
508 helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
511 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
512 error_setg_errno(errp, errno, "socketpair() failed");
513 return -1;
516 /* try to launch bridge helper */
517 pid = fork();
518 if (pid < 0) {
519 error_setg_errno(errp, errno, "Can't fork bridge helper");
520 return -1;
522 if (pid == 0) {
523 int open_max = sysconf(_SC_OPEN_MAX), i;
524 char *fd_buf = NULL;
525 char *br_buf = NULL;
526 char *helper_cmd = NULL;
528 for (i = 3; i < open_max; i++) {
529 if (i != sv[1]) {
530 close(i);
534 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
536 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
537 /* assume helper is a command */
539 if (strstr(helper, "--br=") == NULL) {
540 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
543 helper_cmd = g_strdup_printf("%s %s %s %s", helper,
544 "--use-vnet", fd_buf, br_buf ? br_buf : "");
546 parg = args;
547 *parg++ = (char *)"sh";
548 *parg++ = (char *)"-c";
549 *parg++ = helper_cmd;
550 *parg++ = NULL;
552 execv("/bin/sh", args);
553 g_free(helper_cmd);
554 } else {
555 /* assume helper is just the executable path name */
557 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
559 parg = args;
560 *parg++ = (char *)helper;
561 *parg++ = (char *)"--use-vnet";
562 *parg++ = fd_buf;
563 *parg++ = br_buf;
564 *parg++ = NULL;
566 execv(helper, args);
568 g_free(fd_buf);
569 g_free(br_buf);
570 _exit(1);
572 } else {
573 int fd;
574 int saved_errno;
576 close(sv[1]);
578 fd = RETRY_ON_EINTR(recv_fd(sv[0]));
579 saved_errno = errno;
581 close(sv[0]);
583 while (waitpid(pid, &status, 0) != pid) {
584 /* loop */
586 sigprocmask(SIG_SETMASK, &oldmask, NULL);
587 if (fd < 0) {
588 error_setg_errno(errp, saved_errno,
589 "failed to recv file descriptor");
590 return -1;
592 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
593 error_setg(errp, "bridge helper failed");
594 return -1;
596 return fd;
600 int net_init_bridge(const Netdev *netdev, const char *name,
601 NetClientState *peer, Error **errp)
603 const NetdevBridgeOptions *bridge;
604 const char *helper, *br;
605 TAPState *s;
606 int fd, vnet_hdr;
608 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
609 bridge = &netdev->u.bridge;
610 helper = bridge->helper;
611 br = bridge->br ?: DEFAULT_BRIDGE_INTERFACE;
613 fd = net_bridge_run_helper(helper, br, errp);
614 if (fd == -1) {
615 return -1;
618 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
619 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
620 return -1;
622 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
623 if (vnet_hdr < 0) {
624 close(fd);
625 return -1;
627 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
629 qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br);
631 return 0;
634 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
635 const char *setup_script, char *ifname,
636 size_t ifname_sz, int mq_required, Error **errp)
638 Error *err = NULL;
639 int fd, vnet_hdr_required;
641 if (tap->has_vnet_hdr) {
642 *vnet_hdr = tap->vnet_hdr;
643 vnet_hdr_required = *vnet_hdr;
644 } else {
645 *vnet_hdr = 1;
646 vnet_hdr_required = 0;
649 fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
650 mq_required, errp));
651 if (fd < 0) {
652 return -1;
655 if (setup_script &&
656 setup_script[0] != '\0' &&
657 strcmp(setup_script, "no") != 0) {
658 launch_script(setup_script, ifname, fd, &err);
659 if (err) {
660 error_propagate(errp, err);
661 close(fd);
662 return -1;
666 return fd;
669 #define MAX_TAP_QUEUES 1024
671 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
672 const char *model, const char *name,
673 const char *ifname, const char *script,
674 const char *downscript, const char *vhostfdname,
675 int vnet_hdr, int fd, Error **errp)
677 Error *err = NULL;
678 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
679 int vhostfd;
681 tap_set_sndbuf(s->fd, tap, &err);
682 if (err) {
683 error_propagate(errp, err);
684 goto failed;
687 if (tap->fd || tap->fds) {
688 qemu_set_info_str(&s->nc, "fd=%d", fd);
689 } else if (tap->helper) {
690 qemu_set_info_str(&s->nc, "helper=%s", tap->helper);
691 } else {
692 qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname,
693 script, downscript);
695 if (strcmp(downscript, "no") != 0) {
696 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
697 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
698 "%s", ifname);
702 if (tap->has_vhost ? tap->vhost :
703 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
704 VhostNetOptions options;
706 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
707 options.net_backend = &s->nc;
708 if (tap->has_poll_us) {
709 options.busyloop_timeout = tap->poll_us;
710 } else {
711 options.busyloop_timeout = 0;
714 if (vhostfdname) {
715 vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
716 if (vhostfd == -1) {
717 if (tap->has_vhostforce && tap->vhostforce) {
718 error_propagate(errp, err);
719 } else {
720 warn_report_err(err);
722 goto failed;
724 if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
725 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
726 name, fd);
727 goto failed;
729 } else {
730 vhostfd = open("/dev/vhost-net", O_RDWR);
731 if (vhostfd < 0) {
732 if (tap->has_vhostforce && tap->vhostforce) {
733 error_setg_errno(errp, errno,
734 "tap: open vhost char device failed");
735 } else {
736 warn_report("tap: open vhost char device failed: %s",
737 strerror(errno));
739 goto failed;
741 if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
742 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
743 goto failed;
746 options.opaque = (void *)(uintptr_t)vhostfd;
747 options.nvqs = 2;
749 s->vhost_net = vhost_net_init(&options);
750 if (!s->vhost_net) {
751 if (tap->has_vhostforce && tap->vhostforce) {
752 error_setg(errp, VHOST_NET_INIT_FAILED);
753 } else {
754 warn_report(VHOST_NET_INIT_FAILED);
756 goto failed;
758 } else if (vhostfdname) {
759 error_setg(errp, "vhostfd(s)= is not valid without vhost");
760 goto failed;
763 return;
765 failed:
766 qemu_del_net_client(&s->nc);
769 static int get_fds(char *str, char *fds[], int max)
771 char *ptr = str, *this;
772 size_t len = strlen(str);
773 int i = 0;
775 while (i < max && ptr < str + len) {
776 this = strchr(ptr, ':');
778 if (this == NULL) {
779 fds[i] = g_strdup(ptr);
780 } else {
781 fds[i] = g_strndup(ptr, this - ptr);
784 i++;
785 if (this == NULL) {
786 break;
787 } else {
788 ptr = this + 1;
792 return i;
795 int net_init_tap(const Netdev *netdev, const char *name,
796 NetClientState *peer, Error **errp)
798 const NetdevTapOptions *tap;
799 int fd, vnet_hdr = 0, i = 0, queues;
800 /* for the no-fd, no-helper case */
801 const char *script;
802 const char *downscript;
803 Error *err = NULL;
804 const char *vhostfdname;
805 char ifname[128];
806 int ret = 0;
808 assert(netdev->type == NET_CLIENT_DRIVER_TAP);
809 tap = &netdev->u.tap;
810 queues = tap->has_queues ? tap->queues : 1;
811 vhostfdname = tap->vhostfd;
812 script = tap->script;
813 downscript = tap->downscript;
815 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
816 * For -netdev, peer is always NULL. */
817 if (peer && (tap->has_queues || tap->fds || tap->vhostfds)) {
818 error_setg(errp, "Multiqueue tap cannot be used with hubs");
819 return -1;
822 if (tap->fd) {
823 if (tap->ifname || tap->script || tap->downscript ||
824 tap->has_vnet_hdr || tap->helper || tap->has_queues ||
825 tap->fds || tap->vhostfds) {
826 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
827 "helper=, queues=, fds=, and vhostfds= "
828 "are invalid with fd=");
829 return -1;
832 fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
833 if (fd == -1) {
834 return -1;
837 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
838 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
839 name, fd);
840 close(fd);
841 return -1;
844 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
845 if (vnet_hdr < 0) {
846 close(fd);
847 return -1;
850 net_init_tap_one(tap, peer, "tap", name, NULL,
851 script, downscript,
852 vhostfdname, vnet_hdr, fd, &err);
853 if (err) {
854 error_propagate(errp, err);
855 close(fd);
856 return -1;
858 } else if (tap->fds) {
859 char **fds;
860 char **vhost_fds;
861 int nfds = 0, nvhosts = 0;
863 if (tap->ifname || tap->script || tap->downscript ||
864 tap->has_vnet_hdr || tap->helper || tap->has_queues ||
865 tap->vhostfd) {
866 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
867 "helper=, queues=, and vhostfd= "
868 "are invalid with fds=");
869 return -1;
872 fds = g_new0(char *, MAX_TAP_QUEUES);
873 vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
875 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
876 if (tap->vhostfds) {
877 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
878 if (nfds != nvhosts) {
879 error_setg(errp, "The number of fds passed does not match "
880 "the number of vhostfds passed");
881 ret = -1;
882 goto free_fail;
886 for (i = 0; i < nfds; i++) {
887 fd = monitor_fd_param(monitor_cur(), fds[i], errp);
888 if (fd == -1) {
889 ret = -1;
890 goto free_fail;
893 ret = g_unix_set_fd_nonblocking(fd, true, NULL);
894 if (!ret) {
895 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
896 name, fd);
897 goto free_fail;
900 if (i == 0) {
901 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
902 if (vnet_hdr < 0) {
903 ret = -1;
904 goto free_fail;
906 } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
907 error_setg(errp,
908 "vnet_hdr not consistent across given tap fds");
909 ret = -1;
910 goto free_fail;
913 net_init_tap_one(tap, peer, "tap", name, ifname,
914 script, downscript,
915 tap->vhostfds ? vhost_fds[i] : NULL,
916 vnet_hdr, fd, &err);
917 if (err) {
918 error_propagate(errp, err);
919 ret = -1;
920 goto free_fail;
924 free_fail:
925 for (i = 0; i < nvhosts; i++) {
926 g_free(vhost_fds[i]);
928 for (i = 0; i < nfds; i++) {
929 g_free(fds[i]);
931 g_free(fds);
932 g_free(vhost_fds);
933 return ret;
934 } else if (tap->helper) {
935 if (tap->ifname || tap->script || tap->downscript ||
936 tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) {
937 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
938 "queues=, and vhostfds= are invalid with helper=");
939 return -1;
942 fd = net_bridge_run_helper(tap->helper,
943 tap->br ?: DEFAULT_BRIDGE_INTERFACE,
944 errp);
945 if (fd == -1) {
946 return -1;
949 if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
950 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
951 return -1;
953 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
954 if (vnet_hdr < 0) {
955 close(fd);
956 return -1;
959 net_init_tap_one(tap, peer, "bridge", name, ifname,
960 script, downscript, vhostfdname,
961 vnet_hdr, fd, &err);
962 if (err) {
963 error_propagate(errp, err);
964 close(fd);
965 return -1;
967 } else {
968 g_autofree char *default_script = NULL;
969 g_autofree char *default_downscript = NULL;
970 if (tap->vhostfds) {
971 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
972 return -1;
975 if (!script) {
976 script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
978 if (!downscript) {
979 downscript = default_downscript =
980 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
983 if (tap->ifname) {
984 pstrcpy(ifname, sizeof ifname, tap->ifname);
985 } else {
986 ifname[0] = '\0';
989 for (i = 0; i < queues; i++) {
990 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
991 ifname, sizeof ifname, queues > 1, errp);
992 if (fd == -1) {
993 return -1;
996 if (queues > 1 && i == 0 && !tap->ifname) {
997 if (tap_fd_get_ifname(fd, ifname)) {
998 error_setg(errp, "Fail to get ifname");
999 close(fd);
1000 return -1;
1004 net_init_tap_one(tap, peer, "tap", name, ifname,
1005 i >= 1 ? "no" : script,
1006 i >= 1 ? "no" : downscript,
1007 vhostfdname, vnet_hdr, fd, &err);
1008 if (err) {
1009 error_propagate(errp, err);
1010 close(fd);
1011 return -1;
1016 return 0;
1019 VHostNetState *tap_get_vhost_net(NetClientState *nc)
1021 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1022 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
1023 return s->vhost_net;
1026 int tap_enable(NetClientState *nc)
1028 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1029 int ret;
1031 if (s->enabled) {
1032 return 0;
1033 } else {
1034 ret = tap_fd_enable(s->fd);
1035 if (ret == 0) {
1036 s->enabled = true;
1037 tap_update_fd_handler(s);
1039 return ret;
1043 int tap_disable(NetClientState *nc)
1045 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1046 int ret;
1048 if (s->enabled == 0) {
1049 return 0;
1050 } else {
1051 ret = tap_fd_disable(s->fd);
1052 if (ret == 0) {
1053 qemu_purge_queued_packets(nc);
1054 s->enabled = false;
1055 tap_update_fd_handler(s);
1057 return ret;