iotests: 095: Use TEST_IMG override instead of "mv"
[qemu/ar7.git] / net / tap.c
blob85c4142d15534b6ad7dca419397092afb8ebdb8a
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 "tap_int.h"
28 #include "config-host.h"
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <net/if.h>
36 #include "net/net.h"
37 #include "clients.h"
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qemu-common.h"
41 #include "qemu/error-report.h"
43 #include "net/tap.h"
45 #include "net/vhost_net.h"
47 typedef struct TAPState {
48 NetClientState nc;
49 int fd;
50 char down_script[1024];
51 char down_script_arg[128];
52 uint8_t buf[NET_BUFSIZE];
53 bool read_poll;
54 bool write_poll;
55 bool using_vnet_hdr;
56 bool has_ufo;
57 bool enabled;
58 VHostNetState *vhost_net;
59 unsigned host_vnet_hdr_len;
60 } TAPState;
62 static void launch_script(const char *setup_script, const char *ifname,
63 int fd, Error **errp);
65 static void tap_send(void *opaque);
66 static void tap_writable(void *opaque);
68 static void tap_update_fd_handler(TAPState *s)
70 qemu_set_fd_handler(s->fd,
71 s->read_poll && s->enabled ? tap_send : NULL,
72 s->write_poll && s->enabled ? tap_writable : NULL,
73 s);
76 static void tap_read_poll(TAPState *s, bool enable)
78 s->read_poll = enable;
79 tap_update_fd_handler(s);
82 static void tap_write_poll(TAPState *s, bool enable)
84 s->write_poll = enable;
85 tap_update_fd_handler(s);
88 static void tap_writable(void *opaque)
90 TAPState *s = opaque;
92 tap_write_poll(s, false);
94 qemu_flush_queued_packets(&s->nc);
97 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
99 ssize_t len;
101 do {
102 len = writev(s->fd, iov, iovcnt);
103 } while (len == -1 && errno == EINTR);
105 if (len == -1 && errno == EAGAIN) {
106 tap_write_poll(s, true);
107 return 0;
110 return len;
113 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
114 int iovcnt)
116 TAPState *s = DO_UPCAST(TAPState, nc, nc);
117 const struct iovec *iovp = iov;
118 struct iovec iov_copy[iovcnt + 1];
119 struct virtio_net_hdr_mrg_rxbuf hdr = { };
121 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
122 iov_copy[0].iov_base = &hdr;
123 iov_copy[0].iov_len = s->host_vnet_hdr_len;
124 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
125 iovp = iov_copy;
126 iovcnt++;
129 return tap_write_packet(s, iovp, iovcnt);
132 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
134 TAPState *s = DO_UPCAST(TAPState, nc, nc);
135 struct iovec iov[2];
136 int iovcnt = 0;
137 struct virtio_net_hdr_mrg_rxbuf hdr = { };
139 if (s->host_vnet_hdr_len) {
140 iov[iovcnt].iov_base = &hdr;
141 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
142 iovcnt++;
145 iov[iovcnt].iov_base = (char *)buf;
146 iov[iovcnt].iov_len = size;
147 iovcnt++;
149 return tap_write_packet(s, iov, iovcnt);
152 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
154 TAPState *s = DO_UPCAST(TAPState, nc, nc);
155 struct iovec iov[1];
157 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
158 return tap_receive_raw(nc, buf, size);
161 iov[0].iov_base = (char *)buf;
162 iov[0].iov_len = size;
164 return tap_write_packet(s, iov, 1);
167 #ifndef __sun__
168 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
170 return read(tapfd, buf, maxlen);
172 #endif
174 static void tap_send_completed(NetClientState *nc, ssize_t len)
176 TAPState *s = DO_UPCAST(TAPState, nc, nc);
177 tap_read_poll(s, true);
180 static void tap_send(void *opaque)
182 TAPState *s = opaque;
183 int size;
184 int packets = 0;
186 while (true) {
187 uint8_t *buf = s->buf;
189 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
190 if (size <= 0) {
191 break;
194 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
195 buf += s->host_vnet_hdr_len;
196 size -= s->host_vnet_hdr_len;
199 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
200 if (size == 0) {
201 tap_read_poll(s, false);
202 break;
203 } else if (size < 0) {
204 break;
208 * When the host keeps receiving more packets while tap_send() is
209 * running we can hog the QEMU global mutex. Limit the number of
210 * packets that are processed per tap_send() callback to prevent
211 * stalling the guest.
213 packets++;
214 if (packets >= 50) {
215 break;
220 static bool tap_has_ufo(NetClientState *nc)
222 TAPState *s = DO_UPCAST(TAPState, nc, nc);
224 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
226 return s->has_ufo;
229 static bool tap_has_vnet_hdr(NetClientState *nc)
231 TAPState *s = DO_UPCAST(TAPState, nc, nc);
233 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
235 return !!s->host_vnet_hdr_len;
238 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
240 TAPState *s = DO_UPCAST(TAPState, nc, nc);
242 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
244 return !!tap_probe_vnet_hdr_len(s->fd, len);
247 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
249 TAPState *s = DO_UPCAST(TAPState, nc, nc);
251 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
252 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
253 len == sizeof(struct virtio_net_hdr));
255 tap_fd_set_vnet_hdr_len(s->fd, len);
256 s->host_vnet_hdr_len = len;
259 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
261 TAPState *s = DO_UPCAST(TAPState, nc, nc);
263 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
264 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
266 s->using_vnet_hdr = using_vnet_hdr;
269 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
271 TAPState *s = DO_UPCAST(TAPState, nc, nc);
273 return tap_fd_set_vnet_le(s->fd, is_le);
276 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
278 TAPState *s = DO_UPCAST(TAPState, nc, nc);
280 return tap_fd_set_vnet_be(s->fd, is_be);
283 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
284 int tso6, int ecn, int ufo)
286 TAPState *s = DO_UPCAST(TAPState, nc, nc);
287 if (s->fd < 0) {
288 return;
291 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
294 static void tap_cleanup(NetClientState *nc)
296 TAPState *s = DO_UPCAST(TAPState, nc, nc);
297 Error *err = NULL;
299 if (s->vhost_net) {
300 vhost_net_cleanup(s->vhost_net);
301 s->vhost_net = NULL;
304 qemu_purge_queued_packets(nc);
306 if (s->down_script[0]) {
307 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
308 if (err) {
309 error_report_err(err);
313 tap_read_poll(s, false);
314 tap_write_poll(s, false);
315 close(s->fd);
316 s->fd = -1;
319 static void tap_poll(NetClientState *nc, bool enable)
321 TAPState *s = DO_UPCAST(TAPState, nc, nc);
322 tap_read_poll(s, enable);
323 tap_write_poll(s, enable);
326 int tap_get_fd(NetClientState *nc)
328 TAPState *s = DO_UPCAST(TAPState, nc, nc);
329 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
330 return s->fd;
333 /* fd support */
335 static NetClientInfo net_tap_info = {
336 .type = NET_CLIENT_OPTIONS_KIND_TAP,
337 .size = sizeof(TAPState),
338 .receive = tap_receive,
339 .receive_raw = tap_receive_raw,
340 .receive_iov = tap_receive_iov,
341 .poll = tap_poll,
342 .cleanup = tap_cleanup,
343 .has_ufo = tap_has_ufo,
344 .has_vnet_hdr = tap_has_vnet_hdr,
345 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
346 .using_vnet_hdr = tap_using_vnet_hdr,
347 .set_offload = tap_set_offload,
348 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
349 .set_vnet_le = tap_set_vnet_le,
350 .set_vnet_be = tap_set_vnet_be,
353 static TAPState *net_tap_fd_init(NetClientState *peer,
354 const char *model,
355 const char *name,
356 int fd,
357 int vnet_hdr)
359 NetClientState *nc;
360 TAPState *s;
362 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
364 s = DO_UPCAST(TAPState, nc, nc);
366 s->fd = fd;
367 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
368 s->using_vnet_hdr = false;
369 s->has_ufo = tap_probe_has_ufo(s->fd);
370 s->enabled = true;
371 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
373 * Make sure host header length is set correctly in tap:
374 * it might have been modified by another instance of qemu.
376 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
377 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
379 tap_read_poll(s, true);
380 s->vhost_net = NULL;
381 return s;
384 static void launch_script(const char *setup_script, const char *ifname,
385 int fd, Error **errp)
387 int pid, status;
388 char *args[3];
389 char **parg;
391 /* try to launch network script */
392 pid = fork();
393 if (pid < 0) {
394 error_setg_errno(errp, errno, "could not launch network script %s",
395 setup_script);
396 return;
398 if (pid == 0) {
399 int open_max = sysconf(_SC_OPEN_MAX), i;
401 for (i = 3; i < open_max; i++) {
402 if (i != fd) {
403 close(i);
406 parg = args;
407 *parg++ = (char *)setup_script;
408 *parg++ = (char *)ifname;
409 *parg = NULL;
410 execv(setup_script, args);
411 _exit(1);
412 } else {
413 while (waitpid(pid, &status, 0) != pid) {
414 /* loop */
417 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
418 return;
420 error_setg(errp, "network script %s failed with status %d",
421 setup_script, status);
425 static int recv_fd(int c)
427 int fd;
428 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
429 struct msghdr msg = {
430 .msg_control = msgbuf,
431 .msg_controllen = sizeof(msgbuf),
433 struct cmsghdr *cmsg;
434 struct iovec iov;
435 uint8_t req[1];
436 ssize_t len;
438 cmsg = CMSG_FIRSTHDR(&msg);
439 cmsg->cmsg_level = SOL_SOCKET;
440 cmsg->cmsg_type = SCM_RIGHTS;
441 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
442 msg.msg_controllen = cmsg->cmsg_len;
444 iov.iov_base = req;
445 iov.iov_len = sizeof(req);
447 msg.msg_iov = &iov;
448 msg.msg_iovlen = 1;
450 len = recvmsg(c, &msg, 0);
451 if (len > 0) {
452 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
453 return fd;
456 return len;
459 static int net_bridge_run_helper(const char *helper, const char *bridge,
460 Error **errp)
462 sigset_t oldmask, mask;
463 int pid, status;
464 char *args[5];
465 char **parg;
466 int sv[2];
468 sigemptyset(&mask);
469 sigaddset(&mask, SIGCHLD);
470 sigprocmask(SIG_BLOCK, &mask, &oldmask);
472 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
473 error_setg_errno(errp, errno, "socketpair() failed");
474 return -1;
477 /* try to launch bridge helper */
478 pid = fork();
479 if (pid < 0) {
480 error_setg_errno(errp, errno, "Can't fork bridge helper");
481 return -1;
483 if (pid == 0) {
484 int open_max = sysconf(_SC_OPEN_MAX), i;
485 char fd_buf[6+10];
486 char br_buf[6+IFNAMSIZ] = {0};
487 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
489 for (i = 3; i < open_max; i++) {
490 if (i != sv[1]) {
491 close(i);
495 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
497 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
498 /* assume helper is a command */
500 if (strstr(helper, "--br=") == NULL) {
501 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
504 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
505 helper, "--use-vnet", fd_buf, br_buf);
507 parg = args;
508 *parg++ = (char *)"sh";
509 *parg++ = (char *)"-c";
510 *parg++ = helper_cmd;
511 *parg++ = NULL;
513 execv("/bin/sh", args);
514 } else {
515 /* assume helper is just the executable path name */
517 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
519 parg = args;
520 *parg++ = (char *)helper;
521 *parg++ = (char *)"--use-vnet";
522 *parg++ = fd_buf;
523 *parg++ = br_buf;
524 *parg++ = NULL;
526 execv(helper, args);
528 _exit(1);
530 } else {
531 int fd;
532 int saved_errno;
534 close(sv[1]);
536 do {
537 fd = recv_fd(sv[0]);
538 } while (fd == -1 && errno == EINTR);
539 saved_errno = errno;
541 close(sv[0]);
543 while (waitpid(pid, &status, 0) != pid) {
544 /* loop */
546 sigprocmask(SIG_SETMASK, &oldmask, NULL);
547 if (fd < 0) {
548 error_setg_errno(errp, saved_errno,
549 "failed to recv file descriptor");
550 return -1;
552 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
553 error_setg(errp, "bridge helper failed");
554 return -1;
556 return fd;
560 int net_init_bridge(const NetClientOptions *opts, const char *name,
561 NetClientState *peer, Error **errp)
563 const NetdevBridgeOptions *bridge;
564 const char *helper, *br;
565 TAPState *s;
566 int fd, vnet_hdr;
568 assert(opts->type == NET_CLIENT_OPTIONS_KIND_BRIDGE);
569 bridge = opts->u.bridge;
571 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
572 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
574 fd = net_bridge_run_helper(helper, br, errp);
575 if (fd == -1) {
576 return -1;
579 fcntl(fd, F_SETFL, O_NONBLOCK);
580 vnet_hdr = tap_probe_vnet_hdr(fd);
581 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
583 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
584 br);
586 return 0;
589 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
590 const char *setup_script, char *ifname,
591 size_t ifname_sz, int mq_required, Error **errp)
593 Error *err = NULL;
594 int fd, vnet_hdr_required;
596 if (tap->has_vnet_hdr) {
597 *vnet_hdr = tap->vnet_hdr;
598 vnet_hdr_required = *vnet_hdr;
599 } else {
600 *vnet_hdr = 1;
601 vnet_hdr_required = 0;
604 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
605 mq_required, errp));
606 if (fd < 0) {
607 return -1;
610 if (setup_script &&
611 setup_script[0] != '\0' &&
612 strcmp(setup_script, "no") != 0) {
613 launch_script(setup_script, ifname, fd, &err);
614 if (err) {
615 error_propagate(errp, err);
616 close(fd);
617 return -1;
621 return fd;
624 #define MAX_TAP_QUEUES 1024
626 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
627 const char *model, const char *name,
628 const char *ifname, const char *script,
629 const char *downscript, const char *vhostfdname,
630 int vnet_hdr, int fd, Error **errp)
632 Error *err = NULL;
633 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
634 int vhostfd;
636 tap_set_sndbuf(s->fd, tap, &err);
637 if (err) {
638 error_propagate(errp, err);
639 return;
642 if (tap->has_fd || tap->has_fds) {
643 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
644 } else if (tap->has_helper) {
645 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
646 tap->helper);
647 } else {
648 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
649 "ifname=%s,script=%s,downscript=%s", ifname, script,
650 downscript);
652 if (strcmp(downscript, "no") != 0) {
653 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
654 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
655 "%s", ifname);
659 if (tap->has_vhost ? tap->vhost :
660 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
661 VhostNetOptions options;
663 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
664 options.net_backend = &s->nc;
666 if (tap->has_vhostfd || tap->has_vhostfds) {
667 vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
668 if (vhostfd == -1) {
669 error_propagate(errp, err);
670 return;
672 } else {
673 vhostfd = open("/dev/vhost-net", O_RDWR);
674 if (vhostfd < 0) {
675 error_setg_errno(errp, errno,
676 "tap: open vhost char device failed");
677 return;
680 options.opaque = (void *)(uintptr_t)vhostfd;
682 s->vhost_net = vhost_net_init(&options);
683 if (!s->vhost_net) {
684 error_setg(errp,
685 "vhost-net requested but could not be initialized");
686 return;
688 } else if (tap->has_vhostfd || tap->has_vhostfds) {
689 error_setg(errp, "vhostfd= is not valid without vhost");
693 static int get_fds(char *str, char *fds[], int max)
695 char *ptr = str, *this;
696 size_t len = strlen(str);
697 int i = 0;
699 while (i < max && ptr < str + len) {
700 this = strchr(ptr, ':');
702 if (this == NULL) {
703 fds[i] = g_strdup(ptr);
704 } else {
705 fds[i] = g_strndup(ptr, this - ptr);
708 i++;
709 if (this == NULL) {
710 break;
711 } else {
712 ptr = this + 1;
716 return i;
719 int net_init_tap(const NetClientOptions *opts, const char *name,
720 NetClientState *peer, Error **errp)
722 const NetdevTapOptions *tap;
723 int fd, vnet_hdr = 0, i = 0, queues;
724 /* for the no-fd, no-helper case */
725 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
726 const char *downscript = NULL;
727 Error *err = NULL;
728 const char *vhostfdname;
729 char ifname[128];
731 assert(opts->type == NET_CLIENT_OPTIONS_KIND_TAP);
732 tap = opts->u.tap;
733 queues = tap->has_queues ? tap->queues : 1;
734 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
736 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
737 * For -netdev, peer is always NULL. */
738 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
739 error_setg(errp, "Multiqueue tap cannot be used with QEMU vlans");
740 return -1;
743 if (tap->has_fd) {
744 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
745 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
746 tap->has_fds || tap->has_vhostfds) {
747 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
748 "helper=, queues=, fds=, and vhostfds= "
749 "are invalid with fd=");
750 return -1;
753 fd = monitor_fd_param(cur_mon, tap->fd, &err);
754 if (fd == -1) {
755 error_propagate(errp, err);
756 return -1;
759 fcntl(fd, F_SETFL, O_NONBLOCK);
761 vnet_hdr = tap_probe_vnet_hdr(fd);
763 net_init_tap_one(tap, peer, "tap", name, NULL,
764 script, downscript,
765 vhostfdname, vnet_hdr, fd, &err);
766 if (err) {
767 error_propagate(errp, err);
768 return -1;
770 } else if (tap->has_fds) {
771 char *fds[MAX_TAP_QUEUES];
772 char *vhost_fds[MAX_TAP_QUEUES];
773 int nfds, nvhosts;
775 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
776 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
777 tap->has_vhostfd) {
778 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
779 "helper=, queues=, and vhostfd= "
780 "are invalid with fds=");
781 return -1;
784 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
785 if (tap->has_vhostfds) {
786 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
787 if (nfds != nvhosts) {
788 error_setg(errp, "The number of fds passed does not match "
789 "the number of vhostfds passed");
790 return -1;
794 for (i = 0; i < nfds; i++) {
795 fd = monitor_fd_param(cur_mon, fds[i], &err);
796 if (fd == -1) {
797 error_propagate(errp, err);
798 return -1;
801 fcntl(fd, F_SETFL, O_NONBLOCK);
803 if (i == 0) {
804 vnet_hdr = tap_probe_vnet_hdr(fd);
805 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
806 error_setg(errp,
807 "vnet_hdr not consistent across given tap fds");
808 return -1;
811 net_init_tap_one(tap, peer, "tap", name, ifname,
812 script, downscript,
813 tap->has_vhostfds ? vhost_fds[i] : NULL,
814 vnet_hdr, fd, &err);
815 if (err) {
816 error_propagate(errp, err);
817 return -1;
820 } else if (tap->has_helper) {
821 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
822 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
823 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
824 "queues=, and vhostfds= are invalid with helper=");
825 return -1;
828 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE,
829 errp);
830 if (fd == -1) {
831 return -1;
834 fcntl(fd, F_SETFL, O_NONBLOCK);
835 vnet_hdr = tap_probe_vnet_hdr(fd);
837 net_init_tap_one(tap, peer, "bridge", name, ifname,
838 script, downscript, vhostfdname,
839 vnet_hdr, fd, &err);
840 if (err) {
841 error_propagate(errp, err);
842 close(fd);
843 return -1;
845 } else {
846 if (tap->has_vhostfds) {
847 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
848 return -1;
850 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
851 downscript = tap->has_downscript ? tap->downscript :
852 DEFAULT_NETWORK_DOWN_SCRIPT;
854 if (tap->has_ifname) {
855 pstrcpy(ifname, sizeof ifname, tap->ifname);
856 } else {
857 ifname[0] = '\0';
860 for (i = 0; i < queues; i++) {
861 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
862 ifname, sizeof ifname, queues > 1, errp);
863 if (fd == -1) {
864 return -1;
867 if (queues > 1 && i == 0 && !tap->has_ifname) {
868 if (tap_fd_get_ifname(fd, ifname)) {
869 error_setg(errp, "Fail to get ifname");
870 close(fd);
871 return -1;
875 net_init_tap_one(tap, peer, "tap", name, ifname,
876 i >= 1 ? "no" : script,
877 i >= 1 ? "no" : downscript,
878 vhostfdname, vnet_hdr, fd, &err);
879 if (err) {
880 error_propagate(errp, err);
881 close(fd);
882 return -1;
887 return 0;
890 VHostNetState *tap_get_vhost_net(NetClientState *nc)
892 TAPState *s = DO_UPCAST(TAPState, nc, nc);
893 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
894 return s->vhost_net;
897 int tap_enable(NetClientState *nc)
899 TAPState *s = DO_UPCAST(TAPState, nc, nc);
900 int ret;
902 if (s->enabled) {
903 return 0;
904 } else {
905 ret = tap_fd_enable(s->fd);
906 if (ret == 0) {
907 s->enabled = true;
908 tap_update_fd_handler(s);
910 return ret;
914 int tap_disable(NetClientState *nc)
916 TAPState *s = DO_UPCAST(TAPState, nc, nc);
917 int ret;
919 if (s->enabled == 0) {
920 return 0;
921 } else {
922 ret = tap_fd_disable(s->fd);
923 if (ret == 0) {
924 qemu_purge_queued_packets(nc);
925 s->enabled = false;
926 tap_update_fd_handler(s);
928 return ret;