MAINTAINERS: mark megasas as maintained
[qemu/ar7.git] / net / tap.c
blob8847ce100a17f7c1d638a33557f0f4ff7a7af874
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 int launch_script(const char *setup_script, const char *ifname, int fd);
64 static int tap_can_send(void *opaque);
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_handler2(s->fd,
71 s->read_poll && s->enabled ? tap_can_send : NULL,
72 s->read_poll && s->enabled ? tap_send : NULL,
73 s->write_poll && s->enabled ? tap_writable : NULL,
74 s);
77 static void tap_read_poll(TAPState *s, bool enable)
79 s->read_poll = enable;
80 tap_update_fd_handler(s);
83 static void tap_write_poll(TAPState *s, bool enable)
85 s->write_poll = enable;
86 tap_update_fd_handler(s);
89 static void tap_writable(void *opaque)
91 TAPState *s = opaque;
93 tap_write_poll(s, false);
95 qemu_flush_queued_packets(&s->nc);
98 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
100 ssize_t len;
102 do {
103 len = writev(s->fd, iov, iovcnt);
104 } while (len == -1 && errno == EINTR);
106 if (len == -1 && errno == EAGAIN) {
107 tap_write_poll(s, true);
108 return 0;
111 return len;
114 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
115 int iovcnt)
117 TAPState *s = DO_UPCAST(TAPState, nc, nc);
118 const struct iovec *iovp = iov;
119 struct iovec iov_copy[iovcnt + 1];
120 struct virtio_net_hdr_mrg_rxbuf hdr = { };
122 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
123 iov_copy[0].iov_base = &hdr;
124 iov_copy[0].iov_len = s->host_vnet_hdr_len;
125 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
126 iovp = iov_copy;
127 iovcnt++;
130 return tap_write_packet(s, iovp, iovcnt);
133 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
135 TAPState *s = DO_UPCAST(TAPState, nc, nc);
136 struct iovec iov[2];
137 int iovcnt = 0;
138 struct virtio_net_hdr_mrg_rxbuf hdr = { };
140 if (s->host_vnet_hdr_len) {
141 iov[iovcnt].iov_base = &hdr;
142 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
143 iovcnt++;
146 iov[iovcnt].iov_base = (char *)buf;
147 iov[iovcnt].iov_len = size;
148 iovcnt++;
150 return tap_write_packet(s, iov, iovcnt);
153 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
155 TAPState *s = DO_UPCAST(TAPState, nc, nc);
156 struct iovec iov[1];
158 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
159 return tap_receive_raw(nc, buf, size);
162 iov[0].iov_base = (char *)buf;
163 iov[0].iov_len = size;
165 return tap_write_packet(s, iov, 1);
168 static int tap_can_send(void *opaque)
170 TAPState *s = opaque;
172 return qemu_can_send_packet(&s->nc);
175 #ifndef __sun__
176 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
178 return read(tapfd, buf, maxlen);
180 #endif
182 static void tap_send_completed(NetClientState *nc, ssize_t len)
184 TAPState *s = DO_UPCAST(TAPState, nc, nc);
185 tap_read_poll(s, true);
188 static void tap_send(void *opaque)
190 TAPState *s = opaque;
191 int size;
193 while (qemu_can_send_packet(&s->nc)) {
194 uint8_t *buf = s->buf;
196 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
197 if (size <= 0) {
198 break;
201 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
202 buf += s->host_vnet_hdr_len;
203 size -= s->host_vnet_hdr_len;
206 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
207 if (size == 0) {
208 tap_read_poll(s, false);
209 break;
210 } else if (size < 0) {
211 break;
216 static bool tap_has_ufo(NetClientState *nc)
218 TAPState *s = DO_UPCAST(TAPState, nc, nc);
220 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
222 return s->has_ufo;
225 static bool tap_has_vnet_hdr(NetClientState *nc)
227 TAPState *s = DO_UPCAST(TAPState, nc, nc);
229 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
231 return !!s->host_vnet_hdr_len;
234 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
236 TAPState *s = DO_UPCAST(TAPState, nc, nc);
238 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
240 return !!tap_probe_vnet_hdr_len(s->fd, len);
243 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
245 TAPState *s = DO_UPCAST(TAPState, nc, nc);
247 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
248 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
249 len == sizeof(struct virtio_net_hdr));
251 tap_fd_set_vnet_hdr_len(s->fd, len);
252 s->host_vnet_hdr_len = len;
255 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
257 TAPState *s = DO_UPCAST(TAPState, nc, nc);
259 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
260 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
262 s->using_vnet_hdr = using_vnet_hdr;
265 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
266 int tso6, int ecn, int ufo)
268 TAPState *s = DO_UPCAST(TAPState, nc, nc);
269 if (s->fd < 0) {
270 return;
273 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
276 static void tap_cleanup(NetClientState *nc)
278 TAPState *s = DO_UPCAST(TAPState, nc, nc);
280 if (s->vhost_net) {
281 vhost_net_cleanup(s->vhost_net);
282 s->vhost_net = NULL;
285 qemu_purge_queued_packets(nc);
287 if (s->down_script[0])
288 launch_script(s->down_script, s->down_script_arg, s->fd);
290 tap_read_poll(s, false);
291 tap_write_poll(s, false);
292 close(s->fd);
293 s->fd = -1;
296 static void tap_poll(NetClientState *nc, bool enable)
298 TAPState *s = DO_UPCAST(TAPState, nc, nc);
299 tap_read_poll(s, enable);
300 tap_write_poll(s, enable);
303 int tap_get_fd(NetClientState *nc)
305 TAPState *s = DO_UPCAST(TAPState, nc, nc);
306 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
307 return s->fd;
310 /* fd support */
312 static NetClientInfo net_tap_info = {
313 .type = NET_CLIENT_OPTIONS_KIND_TAP,
314 .size = sizeof(TAPState),
315 .receive = tap_receive,
316 .receive_raw = tap_receive_raw,
317 .receive_iov = tap_receive_iov,
318 .poll = tap_poll,
319 .cleanup = tap_cleanup,
320 .has_ufo = tap_has_ufo,
321 .has_vnet_hdr = tap_has_vnet_hdr,
322 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
323 .using_vnet_hdr = tap_using_vnet_hdr,
324 .set_offload = tap_set_offload,
325 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
328 static TAPState *net_tap_fd_init(NetClientState *peer,
329 const char *model,
330 const char *name,
331 int fd,
332 int vnet_hdr)
334 NetClientState *nc;
335 TAPState *s;
337 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
339 s = DO_UPCAST(TAPState, nc, nc);
341 s->fd = fd;
342 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
343 s->using_vnet_hdr = false;
344 s->has_ufo = tap_probe_has_ufo(s->fd);
345 s->enabled = true;
346 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
348 * Make sure host header length is set correctly in tap:
349 * it might have been modified by another instance of qemu.
351 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
352 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
354 tap_read_poll(s, true);
355 s->vhost_net = NULL;
356 return s;
359 static int launch_script(const char *setup_script, const char *ifname, int fd)
361 int pid, status;
362 char *args[3];
363 char **parg;
365 /* try to launch network script */
366 pid = fork();
367 if (pid == 0) {
368 int open_max = sysconf(_SC_OPEN_MAX), i;
370 for (i = 0; i < open_max; i++) {
371 if (i != STDIN_FILENO &&
372 i != STDOUT_FILENO &&
373 i != STDERR_FILENO &&
374 i != fd) {
375 close(i);
378 parg = args;
379 *parg++ = (char *)setup_script;
380 *parg++ = (char *)ifname;
381 *parg = NULL;
382 execv(setup_script, args);
383 _exit(1);
384 } else if (pid > 0) {
385 while (waitpid(pid, &status, 0) != pid) {
386 /* loop */
389 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
390 return 0;
393 fprintf(stderr, "%s: could not launch network script\n", setup_script);
394 return -1;
397 static int recv_fd(int c)
399 int fd;
400 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
401 struct msghdr msg = {
402 .msg_control = msgbuf,
403 .msg_controllen = sizeof(msgbuf),
405 struct cmsghdr *cmsg;
406 struct iovec iov;
407 uint8_t req[1];
408 ssize_t len;
410 cmsg = CMSG_FIRSTHDR(&msg);
411 cmsg->cmsg_level = SOL_SOCKET;
412 cmsg->cmsg_type = SCM_RIGHTS;
413 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
414 msg.msg_controllen = cmsg->cmsg_len;
416 iov.iov_base = req;
417 iov.iov_len = sizeof(req);
419 msg.msg_iov = &iov;
420 msg.msg_iovlen = 1;
422 len = recvmsg(c, &msg, 0);
423 if (len > 0) {
424 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
425 return fd;
428 return len;
431 static int net_bridge_run_helper(const char *helper, const char *bridge)
433 sigset_t oldmask, mask;
434 int pid, status;
435 char *args[5];
436 char **parg;
437 int sv[2];
439 sigemptyset(&mask);
440 sigaddset(&mask, SIGCHLD);
441 sigprocmask(SIG_BLOCK, &mask, &oldmask);
443 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
444 return -1;
447 /* try to launch bridge helper */
448 pid = fork();
449 if (pid == 0) {
450 int open_max = sysconf(_SC_OPEN_MAX), i;
451 char fd_buf[6+10];
452 char br_buf[6+IFNAMSIZ] = {0};
453 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
455 for (i = 0; i < open_max; i++) {
456 if (i != STDIN_FILENO &&
457 i != STDOUT_FILENO &&
458 i != STDERR_FILENO &&
459 i != sv[1]) {
460 close(i);
464 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
466 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
467 /* assume helper is a command */
469 if (strstr(helper, "--br=") == NULL) {
470 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
473 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
474 helper, "--use-vnet", fd_buf, br_buf);
476 parg = args;
477 *parg++ = (char *)"sh";
478 *parg++ = (char *)"-c";
479 *parg++ = helper_cmd;
480 *parg++ = NULL;
482 execv("/bin/sh", args);
483 } else {
484 /* assume helper is just the executable path name */
486 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
488 parg = args;
489 *parg++ = (char *)helper;
490 *parg++ = (char *)"--use-vnet";
491 *parg++ = fd_buf;
492 *parg++ = br_buf;
493 *parg++ = NULL;
495 execv(helper, args);
497 _exit(1);
499 } else if (pid > 0) {
500 int fd;
502 close(sv[1]);
504 do {
505 fd = recv_fd(sv[0]);
506 } while (fd == -1 && errno == EINTR);
508 close(sv[0]);
510 while (waitpid(pid, &status, 0) != pid) {
511 /* loop */
513 sigprocmask(SIG_SETMASK, &oldmask, NULL);
514 if (fd < 0) {
515 fprintf(stderr, "failed to recv file descriptor\n");
516 return -1;
519 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
520 return fd;
523 fprintf(stderr, "failed to launch bridge helper\n");
524 return -1;
527 int net_init_bridge(const NetClientOptions *opts, const char *name,
528 NetClientState *peer)
530 const NetdevBridgeOptions *bridge;
531 const char *helper, *br;
533 TAPState *s;
534 int fd, vnet_hdr;
536 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
537 bridge = opts->bridge;
539 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
540 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
542 fd = net_bridge_run_helper(helper, br);
543 if (fd == -1) {
544 return -1;
547 fcntl(fd, F_SETFL, O_NONBLOCK);
549 vnet_hdr = tap_probe_vnet_hdr(fd);
551 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
552 if (!s) {
553 close(fd);
554 return -1;
557 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
558 br);
560 return 0;
563 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
564 const char *setup_script, char *ifname,
565 size_t ifname_sz, int mq_required)
567 int fd, vnet_hdr_required;
569 if (tap->has_vnet_hdr) {
570 *vnet_hdr = tap->vnet_hdr;
571 vnet_hdr_required = *vnet_hdr;
572 } else {
573 *vnet_hdr = 1;
574 vnet_hdr_required = 0;
577 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
578 mq_required));
579 if (fd < 0) {
580 return -1;
583 if (setup_script &&
584 setup_script[0] != '\0' &&
585 strcmp(setup_script, "no") != 0 &&
586 launch_script(setup_script, ifname, fd)) {
587 close(fd);
588 return -1;
591 return fd;
594 #define MAX_TAP_QUEUES 1024
596 static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
597 const char *model, const char *name,
598 const char *ifname, const char *script,
599 const char *downscript, const char *vhostfdname,
600 int vnet_hdr, int fd)
602 TAPState *s;
604 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
605 if (!s) {
606 close(fd);
607 return -1;
610 if (tap_set_sndbuf(s->fd, tap) < 0) {
611 return -1;
614 if (tap->has_fd || tap->has_fds) {
615 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
616 } else if (tap->has_helper) {
617 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
618 tap->helper);
619 } else {
620 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
621 "ifname=%s,script=%s,downscript=%s", ifname, script,
622 downscript);
624 if (strcmp(downscript, "no") != 0) {
625 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
626 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
627 "%s", ifname);
631 if (tap->has_vhost ? tap->vhost :
632 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
633 int vhostfd;
635 if (tap->has_vhostfd || tap->has_vhostfds) {
636 vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
637 if (vhostfd == -1) {
638 return -1;
640 } else {
641 vhostfd = -1;
644 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
645 tap->has_vhostforce && tap->vhostforce);
646 if (!s->vhost_net) {
647 error_report("vhost-net requested but could not be initialized");
648 return -1;
650 } else if (tap->has_vhostfd || tap->has_vhostfds) {
651 error_report("vhostfd= is not valid without vhost");
652 return -1;
655 return 0;
658 static int get_fds(char *str, char *fds[], int max)
660 char *ptr = str, *this;
661 size_t len = strlen(str);
662 int i = 0;
664 while (i < max && ptr < str + len) {
665 this = strchr(ptr, ':');
667 if (this == NULL) {
668 fds[i] = g_strdup(ptr);
669 } else {
670 fds[i] = g_strndup(ptr, this - ptr);
673 i++;
674 if (this == NULL) {
675 break;
676 } else {
677 ptr = this + 1;
681 return i;
684 int net_init_tap(const NetClientOptions *opts, const char *name,
685 NetClientState *peer)
687 const NetdevTapOptions *tap;
688 int fd, vnet_hdr = 0, i = 0, queues;
689 /* for the no-fd, no-helper case */
690 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
691 const char *downscript = NULL;
692 const char *vhostfdname;
693 char ifname[128];
695 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
696 tap = opts->tap;
697 queues = tap->has_queues ? tap->queues : 1;
698 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
700 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
701 * For -netdev, peer is always NULL. */
702 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
703 error_report("Multiqueue tap cannot be used with QEMU vlans");
704 return -1;
707 if (tap->has_fd) {
708 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
709 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
710 tap->has_fds || tap->has_vhostfds) {
711 error_report("ifname=, script=, downscript=, vnet_hdr=, "
712 "helper=, queues=, fds=, and vhostfds= "
713 "are invalid with fd=");
714 return -1;
717 fd = monitor_handle_fd_param(cur_mon, tap->fd);
718 if (fd == -1) {
719 return -1;
722 fcntl(fd, F_SETFL, O_NONBLOCK);
724 vnet_hdr = tap_probe_vnet_hdr(fd);
726 if (net_init_tap_one(tap, peer, "tap", name, NULL,
727 script, downscript,
728 vhostfdname, vnet_hdr, fd)) {
729 return -1;
731 } else if (tap->has_fds) {
732 char *fds[MAX_TAP_QUEUES];
733 char *vhost_fds[MAX_TAP_QUEUES];
734 int nfds, nvhosts;
736 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
737 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
738 tap->has_vhostfd) {
739 error_report("ifname=, script=, downscript=, vnet_hdr=, "
740 "helper=, queues=, and vhostfd= "
741 "are invalid with fds=");
742 return -1;
745 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
746 if (tap->has_vhostfds) {
747 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
748 if (nfds != nvhosts) {
749 error_report("The number of fds passed does not match the "
750 "number of vhostfds passed");
751 return -1;
755 for (i = 0; i < nfds; i++) {
756 fd = monitor_handle_fd_param(cur_mon, fds[i]);
757 if (fd == -1) {
758 return -1;
761 fcntl(fd, F_SETFL, O_NONBLOCK);
763 if (i == 0) {
764 vnet_hdr = tap_probe_vnet_hdr(fd);
765 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
766 error_report("vnet_hdr not consistent across given tap fds");
767 return -1;
770 if (net_init_tap_one(tap, peer, "tap", name, ifname,
771 script, downscript,
772 tap->has_vhostfds ? vhost_fds[i] : NULL,
773 vnet_hdr, fd)) {
774 return -1;
777 } else if (tap->has_helper) {
778 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
779 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
780 error_report("ifname=, script=, downscript=, and vnet_hdr= "
781 "queues=, and vhostfds= are invalid with helper=");
782 return -1;
785 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
786 if (fd == -1) {
787 return -1;
790 fcntl(fd, F_SETFL, O_NONBLOCK);
791 vnet_hdr = tap_probe_vnet_hdr(fd);
793 if (net_init_tap_one(tap, peer, "bridge", name, ifname,
794 script, downscript, vhostfdname,
795 vnet_hdr, fd)) {
796 return -1;
798 } else {
799 if (tap->has_vhostfds) {
800 error_report("vhostfds= is invalid if fds= wasn't specified");
801 return -1;
803 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
804 downscript = tap->has_downscript ? tap->downscript :
805 DEFAULT_NETWORK_DOWN_SCRIPT;
807 if (tap->has_ifname) {
808 pstrcpy(ifname, sizeof ifname, tap->ifname);
809 } else {
810 ifname[0] = '\0';
813 for (i = 0; i < queues; i++) {
814 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
815 ifname, sizeof ifname, queues > 1);
816 if (fd == -1) {
817 return -1;
820 if (queues > 1 && i == 0 && !tap->has_ifname) {
821 if (tap_fd_get_ifname(fd, ifname)) {
822 error_report("Fail to get ifname");
823 return -1;
827 if (net_init_tap_one(tap, peer, "tap", name, ifname,
828 i >= 1 ? "no" : script,
829 i >= 1 ? "no" : downscript,
830 vhostfdname, vnet_hdr, fd)) {
831 return -1;
836 return 0;
839 VHostNetState *tap_get_vhost_net(NetClientState *nc)
841 TAPState *s = DO_UPCAST(TAPState, nc, nc);
842 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
843 return s->vhost_net;
846 int tap_enable(NetClientState *nc)
848 TAPState *s = DO_UPCAST(TAPState, nc, nc);
849 int ret;
851 if (s->enabled) {
852 return 0;
853 } else {
854 ret = tap_fd_enable(s->fd);
855 if (ret == 0) {
856 s->enabled = true;
857 tap_update_fd_handler(s);
859 return ret;
863 int tap_disable(NetClientState *nc)
865 TAPState *s = DO_UPCAST(TAPState, nc, nc);
866 int ret;
868 if (s->enabled == 0) {
869 return 0;
870 } else {
871 ret = tap_fd_disable(s->fd);
872 if (ret == 0) {
873 qemu_purge_queued_packets(nc);
874 s->enabled = false;
875 tap_update_fd_handler(s);
877 return ret;