xref: /openbmc/qemu/net/tap.c (revision 8ead6018)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2009 Red Hat, Inc.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25 
26 #include "tap_int.h"
27 
28 #include "config-host.h"
29 
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>
35 
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"
42 
43 #include "net/tap.h"
44 
45 #include "net/vhost_net.h"
46 
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;
61 
62 static int launch_script(const char *setup_script, const char *ifname, int fd);
63 
64 static int tap_can_send(void *opaque);
65 static void tap_send(void *opaque);
66 static void tap_writable(void *opaque);
67 
68 static void tap_update_fd_handler(TAPState *s)
69 {
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);
75 }
76 
77 static void tap_read_poll(TAPState *s, bool enable)
78 {
79     s->read_poll = enable;
80     tap_update_fd_handler(s);
81 }
82 
83 static void tap_write_poll(TAPState *s, bool enable)
84 {
85     s->write_poll = enable;
86     tap_update_fd_handler(s);
87 }
88 
89 static void tap_writable(void *opaque)
90 {
91     TAPState *s = opaque;
92 
93     tap_write_poll(s, false);
94 
95     qemu_flush_queued_packets(&s->nc);
96 }
97 
98 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
99 {
100     ssize_t len;
101 
102     do {
103         len = writev(s->fd, iov, iovcnt);
104     } while (len == -1 && errno == EINTR);
105 
106     if (len == -1 && errno == EAGAIN) {
107         tap_write_poll(s, true);
108         return 0;
109     }
110 
111     return len;
112 }
113 
114 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
115                                int iovcnt)
116 {
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 = { };
121 
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++;
128     }
129 
130     return tap_write_packet(s, iovp, iovcnt);
131 }
132 
133 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
134 {
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 = { };
139 
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++;
144     }
145 
146     iov[iovcnt].iov_base = (char *)buf;
147     iov[iovcnt].iov_len  = size;
148     iovcnt++;
149 
150     return tap_write_packet(s, iov, iovcnt);
151 }
152 
153 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
154 {
155     TAPState *s = DO_UPCAST(TAPState, nc, nc);
156     struct iovec iov[1];
157 
158     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
159         return tap_receive_raw(nc, buf, size);
160     }
161 
162     iov[0].iov_base = (char *)buf;
163     iov[0].iov_len  = size;
164 
165     return tap_write_packet(s, iov, 1);
166 }
167 
168 static int tap_can_send(void *opaque)
169 {
170     TAPState *s = opaque;
171 
172     return qemu_can_send_packet(&s->nc);
173 }
174 
175 #ifndef __sun__
176 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
177 {
178     return read(tapfd, buf, maxlen);
179 }
180 #endif
181 
182 static void tap_send_completed(NetClientState *nc, ssize_t len)
183 {
184     TAPState *s = DO_UPCAST(TAPState, nc, nc);
185     tap_read_poll(s, true);
186 }
187 
188 static void tap_send(void *opaque)
189 {
190     TAPState *s = opaque;
191     int size;
192 
193     do {
194         uint8_t *buf = s->buf;
195 
196         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
197         if (size <= 0) {
198             break;
199         }
200 
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;
204         }
205 
206         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
207         if (size == 0) {
208             tap_read_poll(s, false);
209         }
210     } while (size > 0 && qemu_can_send_packet(&s->nc));
211 }
212 
213 static bool tap_has_ufo(NetClientState *nc)
214 {
215     TAPState *s = DO_UPCAST(TAPState, nc, nc);
216 
217     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
218 
219     return s->has_ufo;
220 }
221 
222 static bool tap_has_vnet_hdr(NetClientState *nc)
223 {
224     TAPState *s = DO_UPCAST(TAPState, nc, nc);
225 
226     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
227 
228     return !!s->host_vnet_hdr_len;
229 }
230 
231 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
232 {
233     TAPState *s = DO_UPCAST(TAPState, nc, nc);
234 
235     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
236 
237     return !!tap_probe_vnet_hdr_len(s->fd, len);
238 }
239 
240 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
241 {
242     TAPState *s = DO_UPCAST(TAPState, nc, nc);
243 
244     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
245     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
246            len == sizeof(struct virtio_net_hdr));
247 
248     tap_fd_set_vnet_hdr_len(s->fd, len);
249     s->host_vnet_hdr_len = len;
250 }
251 
252 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
253 {
254     TAPState *s = DO_UPCAST(TAPState, nc, nc);
255 
256     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
257     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
258 
259     s->using_vnet_hdr = using_vnet_hdr;
260 }
261 
262 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
263                      int tso6, int ecn, int ufo)
264 {
265     TAPState *s = DO_UPCAST(TAPState, nc, nc);
266     if (s->fd < 0) {
267         return;
268     }
269 
270     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
271 }
272 
273 static void tap_cleanup(NetClientState *nc)
274 {
275     TAPState *s = DO_UPCAST(TAPState, nc, nc);
276 
277     if (s->vhost_net) {
278         vhost_net_cleanup(s->vhost_net);
279         s->vhost_net = NULL;
280     }
281 
282     qemu_purge_queued_packets(nc);
283 
284     if (s->down_script[0])
285         launch_script(s->down_script, s->down_script_arg, s->fd);
286 
287     tap_read_poll(s, false);
288     tap_write_poll(s, false);
289     close(s->fd);
290     s->fd = -1;
291 }
292 
293 static void tap_poll(NetClientState *nc, bool enable)
294 {
295     TAPState *s = DO_UPCAST(TAPState, nc, nc);
296     tap_read_poll(s, enable);
297     tap_write_poll(s, enable);
298 }
299 
300 int tap_get_fd(NetClientState *nc)
301 {
302     TAPState *s = DO_UPCAST(TAPState, nc, nc);
303     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
304     return s->fd;
305 }
306 
307 /* fd support */
308 
309 static NetClientInfo net_tap_info = {
310     .type = NET_CLIENT_OPTIONS_KIND_TAP,
311     .size = sizeof(TAPState),
312     .receive = tap_receive,
313     .receive_raw = tap_receive_raw,
314     .receive_iov = tap_receive_iov,
315     .poll = tap_poll,
316     .cleanup = tap_cleanup,
317     .has_ufo = tap_has_ufo,
318     .has_vnet_hdr = tap_has_vnet_hdr,
319     .has_vnet_hdr_len = tap_has_vnet_hdr_len,
320     .using_vnet_hdr = tap_using_vnet_hdr,
321     .set_offload = tap_set_offload,
322     .set_vnet_hdr_len = tap_set_vnet_hdr_len,
323 };
324 
325 static TAPState *net_tap_fd_init(NetClientState *peer,
326                                  const char *model,
327                                  const char *name,
328                                  int fd,
329                                  int vnet_hdr)
330 {
331     NetClientState *nc;
332     TAPState *s;
333 
334     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
335 
336     s = DO_UPCAST(TAPState, nc, nc);
337 
338     s->fd = fd;
339     s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
340     s->using_vnet_hdr = false;
341     s->has_ufo = tap_probe_has_ufo(s->fd);
342     s->enabled = true;
343     tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
344     /*
345      * Make sure host header length is set correctly in tap:
346      * it might have been modified by another instance of qemu.
347      */
348     if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
349         tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
350     }
351     tap_read_poll(s, true);
352     s->vhost_net = NULL;
353     return s;
354 }
355 
356 static int launch_script(const char *setup_script, const char *ifname, int fd)
357 {
358     int pid, status;
359     char *args[3];
360     char **parg;
361 
362     /* try to launch network script */
363     pid = fork();
364     if (pid == 0) {
365         int open_max = sysconf(_SC_OPEN_MAX), i;
366 
367         for (i = 0; i < open_max; i++) {
368             if (i != STDIN_FILENO &&
369                 i != STDOUT_FILENO &&
370                 i != STDERR_FILENO &&
371                 i != fd) {
372                 close(i);
373             }
374         }
375         parg = args;
376         *parg++ = (char *)setup_script;
377         *parg++ = (char *)ifname;
378         *parg = NULL;
379         execv(setup_script, args);
380         _exit(1);
381     } else if (pid > 0) {
382         while (waitpid(pid, &status, 0) != pid) {
383             /* loop */
384         }
385 
386         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
387             return 0;
388         }
389     }
390     fprintf(stderr, "%s: could not launch network script\n", setup_script);
391     return -1;
392 }
393 
394 static int recv_fd(int c)
395 {
396     int fd;
397     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
398     struct msghdr msg = {
399         .msg_control = msgbuf,
400         .msg_controllen = sizeof(msgbuf),
401     };
402     struct cmsghdr *cmsg;
403     struct iovec iov;
404     uint8_t req[1];
405     ssize_t len;
406 
407     cmsg = CMSG_FIRSTHDR(&msg);
408     cmsg->cmsg_level = SOL_SOCKET;
409     cmsg->cmsg_type = SCM_RIGHTS;
410     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
411     msg.msg_controllen = cmsg->cmsg_len;
412 
413     iov.iov_base = req;
414     iov.iov_len = sizeof(req);
415 
416     msg.msg_iov = &iov;
417     msg.msg_iovlen = 1;
418 
419     len = recvmsg(c, &msg, 0);
420     if (len > 0) {
421         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
422         return fd;
423     }
424 
425     return len;
426 }
427 
428 static int net_bridge_run_helper(const char *helper, const char *bridge)
429 {
430     sigset_t oldmask, mask;
431     int pid, status;
432     char *args[5];
433     char **parg;
434     int sv[2];
435 
436     sigemptyset(&mask);
437     sigaddset(&mask, SIGCHLD);
438     sigprocmask(SIG_BLOCK, &mask, &oldmask);
439 
440     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
441         return -1;
442     }
443 
444     /* try to launch bridge helper */
445     pid = fork();
446     if (pid == 0) {
447         int open_max = sysconf(_SC_OPEN_MAX), i;
448         char fd_buf[6+10];
449         char br_buf[6+IFNAMSIZ] = {0};
450         char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
451 
452         for (i = 0; i < open_max; i++) {
453             if (i != STDIN_FILENO &&
454                 i != STDOUT_FILENO &&
455                 i != STDERR_FILENO &&
456                 i != sv[1]) {
457                 close(i);
458             }
459         }
460 
461         snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
462 
463         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
464             /* assume helper is a command */
465 
466             if (strstr(helper, "--br=") == NULL) {
467                 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
468             }
469 
470             snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
471                      helper, "--use-vnet", fd_buf, br_buf);
472 
473             parg = args;
474             *parg++ = (char *)"sh";
475             *parg++ = (char *)"-c";
476             *parg++ = helper_cmd;
477             *parg++ = NULL;
478 
479             execv("/bin/sh", args);
480         } else {
481             /* assume helper is just the executable path name */
482 
483             snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
484 
485             parg = args;
486             *parg++ = (char *)helper;
487             *parg++ = (char *)"--use-vnet";
488             *parg++ = fd_buf;
489             *parg++ = br_buf;
490             *parg++ = NULL;
491 
492             execv(helper, args);
493         }
494         _exit(1);
495 
496     } else if (pid > 0) {
497         int fd;
498 
499         close(sv[1]);
500 
501         do {
502             fd = recv_fd(sv[0]);
503         } while (fd == -1 && errno == EINTR);
504 
505         close(sv[0]);
506 
507         while (waitpid(pid, &status, 0) != pid) {
508             /* loop */
509         }
510         sigprocmask(SIG_SETMASK, &oldmask, NULL);
511         if (fd < 0) {
512             fprintf(stderr, "failed to recv file descriptor\n");
513             return -1;
514         }
515 
516         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
517             return fd;
518         }
519     }
520     fprintf(stderr, "failed to launch bridge helper\n");
521     return -1;
522 }
523 
524 int net_init_bridge(const NetClientOptions *opts, const char *name,
525                     NetClientState *peer)
526 {
527     const NetdevBridgeOptions *bridge;
528     const char *helper, *br;
529 
530     TAPState *s;
531     int fd, vnet_hdr;
532 
533     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
534     bridge = opts->bridge;
535 
536     helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
537     br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;
538 
539     fd = net_bridge_run_helper(helper, br);
540     if (fd == -1) {
541         return -1;
542     }
543 
544     fcntl(fd, F_SETFL, O_NONBLOCK);
545 
546     vnet_hdr = tap_probe_vnet_hdr(fd);
547 
548     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
549     if (!s) {
550         close(fd);
551         return -1;
552     }
553 
554     snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
555              br);
556 
557     return 0;
558 }
559 
560 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
561                         const char *setup_script, char *ifname,
562                         size_t ifname_sz, int mq_required)
563 {
564     int fd, vnet_hdr_required;
565 
566     if (tap->has_vnet_hdr) {
567         *vnet_hdr = tap->vnet_hdr;
568         vnet_hdr_required = *vnet_hdr;
569     } else {
570         *vnet_hdr = 1;
571         vnet_hdr_required = 0;
572     }
573 
574     TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
575                       mq_required));
576     if (fd < 0) {
577         return -1;
578     }
579 
580     if (setup_script &&
581         setup_script[0] != '\0' &&
582         strcmp(setup_script, "no") != 0 &&
583         launch_script(setup_script, ifname, fd)) {
584         close(fd);
585         return -1;
586     }
587 
588     return fd;
589 }
590 
591 #define MAX_TAP_QUEUES 1024
592 
593 static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
594                             const char *model, const char *name,
595                             const char *ifname, const char *script,
596                             const char *downscript, const char *vhostfdname,
597                             int vnet_hdr, int fd)
598 {
599     TAPState *s;
600 
601     s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
602     if (!s) {
603         close(fd);
604         return -1;
605     }
606 
607     if (tap_set_sndbuf(s->fd, tap) < 0) {
608         return -1;
609     }
610 
611     if (tap->has_fd || tap->has_fds) {
612         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
613     } else if (tap->has_helper) {
614         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
615                  tap->helper);
616     } else {
617         snprintf(s->nc.info_str, sizeof(s->nc.info_str),
618                  "ifname=%s,script=%s,downscript=%s", ifname, script,
619                  downscript);
620 
621         if (strcmp(downscript, "no") != 0) {
622             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
623             snprintf(s->down_script_arg, sizeof(s->down_script_arg),
624                      "%s", ifname);
625         }
626     }
627 
628     if (tap->has_vhost ? tap->vhost :
629         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
630         int vhostfd;
631 
632         if (tap->has_vhostfd || tap->has_vhostfds) {
633             vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
634             if (vhostfd == -1) {
635                 return -1;
636             }
637         } else {
638             vhostfd = -1;
639         }
640 
641         s->vhost_net = vhost_net_init(&s->nc, vhostfd,
642                                       tap->has_vhostforce && tap->vhostforce);
643         if (!s->vhost_net) {
644             error_report("vhost-net requested but could not be initialized");
645             return -1;
646         }
647     } else if (tap->has_vhostfd || tap->has_vhostfds) {
648         error_report("vhostfd= is not valid without vhost");
649         return -1;
650     }
651 
652     return 0;
653 }
654 
655 static int get_fds(char *str, char *fds[], int max)
656 {
657     char *ptr = str, *this;
658     size_t len = strlen(str);
659     int i = 0;
660 
661     while (i < max && ptr < str + len) {
662         this = strchr(ptr, ':');
663 
664         if (this == NULL) {
665             fds[i] = g_strdup(ptr);
666         } else {
667             fds[i] = g_strndup(ptr, this - ptr);
668         }
669 
670         i++;
671         if (this == NULL) {
672             break;
673         } else {
674             ptr = this + 1;
675         }
676     }
677 
678     return i;
679 }
680 
681 int net_init_tap(const NetClientOptions *opts, const char *name,
682                  NetClientState *peer)
683 {
684     const NetdevTapOptions *tap;
685     int fd, vnet_hdr = 0, i = 0, queues;
686     /* for the no-fd, no-helper case */
687     const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
688     const char *downscript = NULL;
689     const char *vhostfdname;
690     char ifname[128];
691 
692     assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
693     tap = opts->tap;
694     queues = tap->has_queues ? tap->queues : 1;
695     vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
696 
697     /* QEMU vlans does not support multiqueue tap, in this case peer is set.
698      * For -netdev, peer is always NULL. */
699     if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
700         error_report("Multiqueue tap cannot be used with QEMU vlans");
701         return -1;
702     }
703 
704     if (tap->has_fd) {
705         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
706             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
707             tap->has_fds || tap->has_vhostfds) {
708             error_report("ifname=, script=, downscript=, vnet_hdr=, "
709                          "helper=, queues=, fds=, and vhostfds= "
710                          "are invalid with fd=");
711             return -1;
712         }
713 
714         fd = monitor_handle_fd_param(cur_mon, tap->fd);
715         if (fd == -1) {
716             return -1;
717         }
718 
719         fcntl(fd, F_SETFL, O_NONBLOCK);
720 
721         vnet_hdr = tap_probe_vnet_hdr(fd);
722 
723         if (net_init_tap_one(tap, peer, "tap", name, NULL,
724                              script, downscript,
725                              vhostfdname, vnet_hdr, fd)) {
726             return -1;
727         }
728     } else if (tap->has_fds) {
729         char *fds[MAX_TAP_QUEUES];
730         char *vhost_fds[MAX_TAP_QUEUES];
731         int nfds, nvhosts;
732 
733         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
734             tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
735             tap->has_vhostfd) {
736             error_report("ifname=, script=, downscript=, vnet_hdr=, "
737                          "helper=, queues=, and vhostfd= "
738                          "are invalid with fds=");
739             return -1;
740         }
741 
742         nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
743         if (tap->has_vhostfds) {
744             nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
745             if (nfds != nvhosts) {
746                 error_report("The number of fds passed does not match the "
747                              "number of vhostfds passed");
748                 return -1;
749             }
750         }
751 
752         for (i = 0; i < nfds; i++) {
753             fd = monitor_handle_fd_param(cur_mon, fds[i]);
754             if (fd == -1) {
755                 return -1;
756             }
757 
758             fcntl(fd, F_SETFL, O_NONBLOCK);
759 
760             if (i == 0) {
761                 vnet_hdr = tap_probe_vnet_hdr(fd);
762             } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
763                 error_report("vnet_hdr not consistent across given tap fds");
764                 return -1;
765             }
766 
767             if (net_init_tap_one(tap, peer, "tap", name, ifname,
768                                  script, downscript,
769                                  tap->has_vhostfds ? vhost_fds[i] : NULL,
770                                  vnet_hdr, fd)) {
771                 return -1;
772             }
773         }
774     } else if (tap->has_helper) {
775         if (tap->has_ifname || tap->has_script || tap->has_downscript ||
776             tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
777             error_report("ifname=, script=, downscript=, and vnet_hdr= "
778                          "queues=, and vhostfds= are invalid with helper=");
779             return -1;
780         }
781 
782         fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
783         if (fd == -1) {
784             return -1;
785         }
786 
787         fcntl(fd, F_SETFL, O_NONBLOCK);
788         vnet_hdr = tap_probe_vnet_hdr(fd);
789 
790         if (net_init_tap_one(tap, peer, "bridge", name, ifname,
791                              script, downscript, vhostfdname,
792                              vnet_hdr, fd)) {
793             return -1;
794         }
795     } else {
796         if (tap->has_vhostfds) {
797             error_report("vhostfds= is invalid if fds= wasn't specified");
798             return -1;
799         }
800         script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
801         downscript = tap->has_downscript ? tap->downscript :
802             DEFAULT_NETWORK_DOWN_SCRIPT;
803 
804         if (tap->has_ifname) {
805             pstrcpy(ifname, sizeof ifname, tap->ifname);
806         } else {
807             ifname[0] = '\0';
808         }
809 
810         for (i = 0; i < queues; i++) {
811             fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
812                               ifname, sizeof ifname, queues > 1);
813             if (fd == -1) {
814                 return -1;
815             }
816 
817             if (queues > 1 && i == 0 && !tap->has_ifname) {
818                 if (tap_fd_get_ifname(fd, ifname)) {
819                     error_report("Fail to get ifname");
820                     return -1;
821                 }
822             }
823 
824             if (net_init_tap_one(tap, peer, "tap", name, ifname,
825                                  i >= 1 ? "no" : script,
826                                  i >= 1 ? "no" : downscript,
827                                  vhostfdname, vnet_hdr, fd)) {
828                 return -1;
829             }
830         }
831     }
832 
833     return 0;
834 }
835 
836 VHostNetState *tap_get_vhost_net(NetClientState *nc)
837 {
838     TAPState *s = DO_UPCAST(TAPState, nc, nc);
839     assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
840     return s->vhost_net;
841 }
842 
843 int tap_enable(NetClientState *nc)
844 {
845     TAPState *s = DO_UPCAST(TAPState, nc, nc);
846     int ret;
847 
848     if (s->enabled) {
849         return 0;
850     } else {
851         ret = tap_fd_enable(s->fd);
852         if (ret == 0) {
853             s->enabled = true;
854             tap_update_fd_handler(s);
855         }
856         return ret;
857     }
858 }
859 
860 int tap_disable(NetClientState *nc)
861 {
862     TAPState *s = DO_UPCAST(TAPState, nc, nc);
863     int ret;
864 
865     if (s->enabled == 0) {
866         return 0;
867     } else {
868         ret = tap_fd_disable(s->fd);
869         if (ret == 0) {
870             qemu_purge_queued_packets(nc);
871             s->enabled = false;
872             tap_update_fd_handler(s);
873         }
874         return ret;
875     }
876 }
877