virtio-net: compat multiqueue support
[qemu/ar7.git] / net / tap.c
blob1bf760995dd495655f919b4f1d1398991b70f21a
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 "hw/vhost_net.h"
47 /* Maximum GSO packet size (64k) plus plenty of room for
48 * the ethernet and virtio_net headers
50 #define TAP_BUFSIZE (4096 + 65536)
52 typedef struct TAPState {
53 NetClientState nc;
54 int fd;
55 char down_script[1024];
56 char down_script_arg[128];
57 uint8_t buf[TAP_BUFSIZE];
58 bool read_poll;
59 bool write_poll;
60 bool using_vnet_hdr;
61 bool has_ufo;
62 bool enabled;
63 VHostNetState *vhost_net;
64 unsigned host_vnet_hdr_len;
65 } TAPState;
67 static int launch_script(const char *setup_script, const char *ifname, int fd);
69 static int tap_can_send(void *opaque);
70 static void tap_send(void *opaque);
71 static void tap_writable(void *opaque);
73 static void tap_update_fd_handler(TAPState *s)
75 qemu_set_fd_handler2(s->fd,
76 s->read_poll && s->enabled ? tap_can_send : NULL,
77 s->read_poll && s->enabled ? tap_send : NULL,
78 s->write_poll && s->enabled ? tap_writable : NULL,
79 s);
82 static void tap_read_poll(TAPState *s, bool enable)
84 s->read_poll = enable;
85 tap_update_fd_handler(s);
88 static void tap_write_poll(TAPState *s, bool enable)
90 s->write_poll = enable;
91 tap_update_fd_handler(s);
94 static void tap_writable(void *opaque)
96 TAPState *s = opaque;
98 tap_write_poll(s, false);
100 qemu_flush_queued_packets(&s->nc);
103 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
105 ssize_t len;
107 do {
108 len = writev(s->fd, iov, iovcnt);
109 } while (len == -1 && errno == EINTR);
111 if (len == -1 && errno == EAGAIN) {
112 tap_write_poll(s, true);
113 return 0;
116 return len;
119 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
120 int iovcnt)
122 TAPState *s = DO_UPCAST(TAPState, nc, nc);
123 const struct iovec *iovp = iov;
124 struct iovec iov_copy[iovcnt + 1];
125 struct virtio_net_hdr_mrg_rxbuf hdr = { };
127 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
128 iov_copy[0].iov_base = &hdr;
129 iov_copy[0].iov_len = s->host_vnet_hdr_len;
130 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
131 iovp = iov_copy;
132 iovcnt++;
135 return tap_write_packet(s, iovp, iovcnt);
138 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
140 TAPState *s = DO_UPCAST(TAPState, nc, nc);
141 struct iovec iov[2];
142 int iovcnt = 0;
143 struct virtio_net_hdr_mrg_rxbuf hdr = { };
145 if (s->host_vnet_hdr_len) {
146 iov[iovcnt].iov_base = &hdr;
147 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
148 iovcnt++;
151 iov[iovcnt].iov_base = (char *)buf;
152 iov[iovcnt].iov_len = size;
153 iovcnt++;
155 return tap_write_packet(s, iov, iovcnt);
158 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
160 TAPState *s = DO_UPCAST(TAPState, nc, nc);
161 struct iovec iov[1];
163 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
164 return tap_receive_raw(nc, buf, size);
167 iov[0].iov_base = (char *)buf;
168 iov[0].iov_len = size;
170 return tap_write_packet(s, iov, 1);
173 static int tap_can_send(void *opaque)
175 TAPState *s = opaque;
177 return qemu_can_send_packet(&s->nc);
180 #ifndef __sun__
181 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
183 return read(tapfd, buf, maxlen);
185 #endif
187 static void tap_send_completed(NetClientState *nc, ssize_t len)
189 TAPState *s = DO_UPCAST(TAPState, nc, nc);
190 tap_read_poll(s, true);
193 static void tap_send(void *opaque)
195 TAPState *s = opaque;
196 int size;
198 do {
199 uint8_t *buf = s->buf;
201 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
202 if (size <= 0) {
203 break;
206 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
207 buf += s->host_vnet_hdr_len;
208 size -= s->host_vnet_hdr_len;
211 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
212 if (size == 0) {
213 tap_read_poll(s, false);
215 } while (size > 0 && qemu_can_send_packet(&s->nc));
218 bool tap_has_ufo(NetClientState *nc)
220 TAPState *s = DO_UPCAST(TAPState, nc, nc);
222 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
224 return s->has_ufo;
227 int tap_has_vnet_hdr(NetClientState *nc)
229 TAPState *s = DO_UPCAST(TAPState, nc, nc);
231 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
233 return !!s->host_vnet_hdr_len;
236 int tap_has_vnet_hdr_len(NetClientState *nc, int len)
238 TAPState *s = DO_UPCAST(TAPState, nc, nc);
240 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
242 return tap_probe_vnet_hdr_len(s->fd, len);
245 void tap_set_vnet_hdr_len(NetClientState *nc, int len)
247 TAPState *s = DO_UPCAST(TAPState, nc, nc);
249 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
250 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
251 len == sizeof(struct virtio_net_hdr));
253 tap_fd_set_vnet_hdr_len(s->fd, len);
254 s->host_vnet_hdr_len = len;
257 void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
259 TAPState *s = DO_UPCAST(TAPState, nc, nc);
261 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
262 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
264 s->using_vnet_hdr = using_vnet_hdr;
267 void tap_set_offload(NetClientState *nc, int csum, int tso4,
268 int tso6, int ecn, int ufo)
270 TAPState *s = DO_UPCAST(TAPState, nc, nc);
271 if (s->fd < 0) {
272 return;
275 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
278 static void tap_cleanup(NetClientState *nc)
280 TAPState *s = DO_UPCAST(TAPState, nc, nc);
282 if (s->vhost_net) {
283 vhost_net_cleanup(s->vhost_net);
284 s->vhost_net = NULL;
287 qemu_purge_queued_packets(nc);
289 if (s->down_script[0])
290 launch_script(s->down_script, s->down_script_arg, s->fd);
292 tap_read_poll(s, false);
293 tap_write_poll(s, false);
294 close(s->fd);
295 s->fd = -1;
298 static void tap_poll(NetClientState *nc, bool enable)
300 TAPState *s = DO_UPCAST(TAPState, nc, nc);
301 tap_read_poll(s, enable);
302 tap_write_poll(s, enable);
305 int tap_get_fd(NetClientState *nc)
307 TAPState *s = DO_UPCAST(TAPState, nc, nc);
308 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
309 return s->fd;
312 /* fd support */
314 static NetClientInfo net_tap_info = {
315 .type = NET_CLIENT_OPTIONS_KIND_TAP,
316 .size = sizeof(TAPState),
317 .receive = tap_receive,
318 .receive_raw = tap_receive_raw,
319 .receive_iov = tap_receive_iov,
320 .poll = tap_poll,
321 .cleanup = tap_cleanup,
324 static TAPState *net_tap_fd_init(NetClientState *peer,
325 const char *model,
326 const char *name,
327 int fd,
328 int vnet_hdr)
330 NetClientState *nc;
331 TAPState *s;
333 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
335 s = DO_UPCAST(TAPState, nc, nc);
337 s->fd = fd;
338 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
339 s->using_vnet_hdr = false;
340 s->has_ufo = tap_probe_has_ufo(s->fd);
341 s->enabled = true;
342 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
344 * Make sure host header length is set correctly in tap:
345 * it might have been modified by another instance of qemu.
347 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
348 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
350 tap_read_poll(s, true);
351 s->vhost_net = NULL;
352 return s;
355 static int launch_script(const char *setup_script, const char *ifname, int fd)
357 int pid, status;
358 char *args[3];
359 char **parg;
361 /* try to launch network script */
362 pid = fork();
363 if (pid == 0) {
364 int open_max = sysconf(_SC_OPEN_MAX), i;
366 for (i = 0; i < open_max; i++) {
367 if (i != STDIN_FILENO &&
368 i != STDOUT_FILENO &&
369 i != STDERR_FILENO &&
370 i != fd) {
371 close(i);
374 parg = args;
375 *parg++ = (char *)setup_script;
376 *parg++ = (char *)ifname;
377 *parg = NULL;
378 execv(setup_script, args);
379 _exit(1);
380 } else if (pid > 0) {
381 while (waitpid(pid, &status, 0) != pid) {
382 /* loop */
385 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
386 return 0;
389 fprintf(stderr, "%s: could not launch network script\n", setup_script);
390 return -1;
393 static int recv_fd(int c)
395 int fd;
396 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
397 struct msghdr msg = {
398 .msg_control = msgbuf,
399 .msg_controllen = sizeof(msgbuf),
401 struct cmsghdr *cmsg;
402 struct iovec iov;
403 uint8_t req[1];
404 ssize_t len;
406 cmsg = CMSG_FIRSTHDR(&msg);
407 cmsg->cmsg_level = SOL_SOCKET;
408 cmsg->cmsg_type = SCM_RIGHTS;
409 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
410 msg.msg_controllen = cmsg->cmsg_len;
412 iov.iov_base = req;
413 iov.iov_len = sizeof(req);
415 msg.msg_iov = &iov;
416 msg.msg_iovlen = 1;
418 len = recvmsg(c, &msg, 0);
419 if (len > 0) {
420 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
421 return fd;
424 return len;
427 static int net_bridge_run_helper(const char *helper, const char *bridge)
429 sigset_t oldmask, mask;
430 int pid, status;
431 char *args[5];
432 char **parg;
433 int sv[2];
435 sigemptyset(&mask);
436 sigaddset(&mask, SIGCHLD);
437 sigprocmask(SIG_BLOCK, &mask, &oldmask);
439 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
440 return -1;
443 /* try to launch bridge helper */
444 pid = fork();
445 if (pid == 0) {
446 int open_max = sysconf(_SC_OPEN_MAX), i;
447 char fd_buf[6+10];
448 char br_buf[6+IFNAMSIZ] = {0};
449 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
451 for (i = 0; i < open_max; i++) {
452 if (i != STDIN_FILENO &&
453 i != STDOUT_FILENO &&
454 i != STDERR_FILENO &&
455 i != sv[1]) {
456 close(i);
460 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
462 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
463 /* assume helper is a command */
465 if (strstr(helper, "--br=") == NULL) {
466 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
469 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
470 helper, "--use-vnet", fd_buf, br_buf);
472 parg = args;
473 *parg++ = (char *)"sh";
474 *parg++ = (char *)"-c";
475 *parg++ = helper_cmd;
476 *parg++ = NULL;
478 execv("/bin/sh", args);
479 } else {
480 /* assume helper is just the executable path name */
482 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
484 parg = args;
485 *parg++ = (char *)helper;
486 *parg++ = (char *)"--use-vnet";
487 *parg++ = fd_buf;
488 *parg++ = br_buf;
489 *parg++ = NULL;
491 execv(helper, args);
493 _exit(1);
495 } else if (pid > 0) {
496 int fd;
498 close(sv[1]);
500 do {
501 fd = recv_fd(sv[0]);
502 } while (fd == -1 && errno == EINTR);
504 close(sv[0]);
506 while (waitpid(pid, &status, 0) != pid) {
507 /* loop */
509 sigprocmask(SIG_SETMASK, &oldmask, NULL);
510 if (fd < 0) {
511 fprintf(stderr, "failed to recv file descriptor\n");
512 return -1;
515 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
516 return fd;
519 fprintf(stderr, "failed to launch bridge helper\n");
520 return -1;
523 int net_init_bridge(const NetClientOptions *opts, const char *name,
524 NetClientState *peer)
526 const NetdevBridgeOptions *bridge;
527 const char *helper, *br;
529 TAPState *s;
530 int fd, vnet_hdr;
532 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
533 bridge = opts->bridge;
535 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
536 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
538 fd = net_bridge_run_helper(helper, br);
539 if (fd == -1) {
540 return -1;
543 fcntl(fd, F_SETFL, O_NONBLOCK);
545 vnet_hdr = tap_probe_vnet_hdr(fd);
547 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
548 if (!s) {
549 close(fd);
550 return -1;
553 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
554 br);
556 return 0;
559 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
560 const char *setup_script, char *ifname,
561 size_t ifname_sz, int mq_required)
563 int fd, vnet_hdr_required;
565 if (tap->has_vnet_hdr) {
566 *vnet_hdr = tap->vnet_hdr;
567 vnet_hdr_required = *vnet_hdr;
568 } else {
569 *vnet_hdr = 1;
570 vnet_hdr_required = 0;
573 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
574 mq_required));
575 if (fd < 0) {
576 return -1;
579 if (setup_script &&
580 setup_script[0] != '\0' &&
581 strcmp(setup_script, "no") != 0 &&
582 launch_script(setup_script, ifname, fd)) {
583 close(fd);
584 return -1;
587 return fd;
590 #define MAX_TAP_QUEUES 1024
592 static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
593 const char *model, const char *name,
594 const char *ifname, const char *script,
595 const char *downscript, const char *vhostfdname,
596 int vnet_hdr, int fd)
598 TAPState *s;
600 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
601 if (!s) {
602 close(fd);
603 return -1;
606 if (tap_set_sndbuf(s->fd, tap) < 0) {
607 return -1;
610 if (tap->has_fd || tap->has_fds) {
611 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
612 } else if (tap->has_helper) {
613 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
614 tap->helper);
615 } else {
616 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
617 "ifname=%s,script=%s,downscript=%s", ifname, script,
618 downscript);
620 if (strcmp(downscript, "no") != 0) {
621 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
622 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
623 "%s", ifname);
627 if (tap->has_vhost ? tap->vhost :
628 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
629 int vhostfd;
631 if (tap->has_vhostfd) {
632 vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
633 if (vhostfd == -1) {
634 return -1;
636 } else {
637 vhostfd = -1;
640 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
641 tap->has_vhostforce && tap->vhostforce);
642 if (!s->vhost_net) {
643 error_report("vhost-net requested but could not be initialized");
644 return -1;
646 } else if (tap->has_vhostfd || tap->has_vhostfds) {
647 error_report("vhostfd= is not valid without vhost");
648 return -1;
651 return 0;
654 static int get_fds(char *str, char *fds[], int max)
656 char *ptr = str, *this;
657 size_t len = strlen(str);
658 int i = 0;
660 while (i < max && ptr < str + len) {
661 this = strchr(ptr, ':');
663 if (this == NULL) {
664 fds[i] = g_strdup(ptr);
665 } else {
666 fds[i] = g_strndup(ptr, this - ptr);
669 i++;
670 if (this == NULL) {
671 break;
672 } else {
673 ptr = this + 1;
677 return i;
680 int net_init_tap(const NetClientOptions *opts, const char *name,
681 NetClientState *peer)
683 const NetdevTapOptions *tap;
684 int fd, vnet_hdr = 0, i = 0, queues;
685 /* for the no-fd, no-helper case */
686 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
687 const char *downscript = NULL;
688 const char *vhostfdname;
689 char ifname[128];
691 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
692 tap = opts->tap;
693 queues = tap->has_queues ? tap->queues : 1;
694 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
696 if (tap->has_fd) {
697 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
698 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
699 tap->has_fds) {
700 error_report("ifname=, script=, downscript=, vnet_hdr=, "
701 "helper=, queues=, and fds= are invalid with fd=");
702 return -1;
705 fd = monitor_handle_fd_param(cur_mon, tap->fd);
706 if (fd == -1) {
707 return -1;
710 fcntl(fd, F_SETFL, O_NONBLOCK);
712 vnet_hdr = tap_probe_vnet_hdr(fd);
714 if (net_init_tap_one(tap, peer, "tap", NULL, NULL,
715 script, downscript,
716 vhostfdname, vnet_hdr, fd)) {
717 return -1;
719 } else if (tap->has_fds) {
720 char *fds[MAX_TAP_QUEUES];
721 char *vhost_fds[MAX_TAP_QUEUES];
722 int nfds, nvhosts;
724 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
725 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
726 tap->has_fd) {
727 error_report("ifname=, script=, downscript=, vnet_hdr=, "
728 "helper=, queues=, and fd= are invalid with fds=");
729 return -1;
732 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
733 if (tap->has_vhostfds) {
734 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
735 if (nfds != nvhosts) {
736 error_report("The number of fds passed does not match the "
737 "number of vhostfds passed");
738 return -1;
742 for (i = 0; i < nfds; i++) {
743 fd = monitor_handle_fd_param(cur_mon, fds[i]);
744 if (fd == -1) {
745 return -1;
748 fcntl(fd, F_SETFL, O_NONBLOCK);
750 if (i == 0) {
751 vnet_hdr = tap_probe_vnet_hdr(fd);
752 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
753 error_report("vnet_hdr not consistent across given tap fds");
754 return -1;
757 if (net_init_tap_one(tap, peer, "tap", name, ifname,
758 script, downscript,
759 tap->has_vhostfds ? vhost_fds[i] : NULL,
760 vnet_hdr, fd)) {
761 return -1;
764 } else if (tap->has_helper) {
765 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
766 tap->has_vnet_hdr || tap->has_queues || tap->has_fds) {
767 error_report("ifname=, script=, downscript=, and vnet_hdr= "
768 "queues=, and fds= are invalid with helper=");
769 return -1;
772 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
773 if (fd == -1) {
774 return -1;
777 fcntl(fd, F_SETFL, O_NONBLOCK);
778 vnet_hdr = tap_probe_vnet_hdr(fd);
780 if (net_init_tap_one(tap, peer, "bridge", name, ifname,
781 script, downscript, vhostfdname,
782 vnet_hdr, fd)) {
783 return -1;
785 } else {
786 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
787 downscript = tap->has_downscript ? tap->downscript :
788 DEFAULT_NETWORK_DOWN_SCRIPT;
790 if (tap->has_ifname) {
791 pstrcpy(ifname, sizeof ifname, tap->ifname);
792 } else {
793 ifname[0] = '\0';
796 for (i = 0; i < queues; i++) {
797 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
798 ifname, sizeof ifname, queues > 1);
799 if (fd == -1) {
800 return -1;
803 if (queues > 1 && i == 0 && !tap->has_ifname) {
804 if (tap_fd_get_ifname(fd, ifname)) {
805 error_report("Fail to get ifname");
806 return -1;
810 if (net_init_tap_one(tap, peer, "tap", name, ifname,
811 i >= 1 ? "no" : script,
812 i >= 1 ? "no" : downscript,
813 vhostfdname, vnet_hdr, fd)) {
814 return -1;
819 return 0;
822 VHostNetState *tap_get_vhost_net(NetClientState *nc)
824 TAPState *s = DO_UPCAST(TAPState, nc, nc);
825 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
826 return s->vhost_net;
829 int tap_enable(NetClientState *nc)
831 TAPState *s = DO_UPCAST(TAPState, nc, nc);
832 int ret;
834 if (s->enabled) {
835 return 0;
836 } else {
837 ret = tap_fd_enable(s->fd);
838 if (ret == 0) {
839 s->enabled = true;
840 tap_update_fd_handler(s);
842 return ret;
846 int tap_disable(NetClientState *nc)
848 TAPState *s = DO_UPCAST(TAPState, nc, nc);
849 int ret;
851 if (s->enabled == 0) {
852 return 0;
853 } else {
854 ret = tap_fd_disable(s->fd);
855 if (ret == 0) {
856 qemu_purge_queued_packets(nc);
857 s->enabled = false;
858 tap_update_fd_handler(s);
860 return ret;