xref: /openbmc/qemu/net/tap.c (revision 36ebc7db)
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 "qemu/osdep.h"
27 #include "tap_int.h"
28 
29 
30 #include <sys/ioctl.h>
31 #include <sys/wait.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 
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"
45 
46 #include "net/tap.h"
47 
48 #include "net/vhost_net.h"
49 
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;
65 
66 static void launch_script(const char *setup_script, const char *ifname,
67                           int fd, Error **errp);
68 
69 static void tap_send(void *opaque);
70 static void tap_writable(void *opaque);
71 
72 static void tap_update_fd_handler(TAPState *s)
73 {
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);
78 }
79 
80 static void tap_read_poll(TAPState *s, bool enable)
81 {
82     s->read_poll = enable;
83     tap_update_fd_handler(s);
84 }
85 
86 static void tap_write_poll(TAPState *s, bool enable)
87 {
88     s->write_poll = enable;
89     tap_update_fd_handler(s);
90 }
91 
92 static void tap_writable(void *opaque)
93 {
94     TAPState *s = opaque;
95 
96     tap_write_poll(s, false);
97 
98     qemu_flush_queued_packets(&s->nc);
99 }
100 
101 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
102 {
103     ssize_t len;
104 
105     len = RETRY_ON_EINTR(writev(s->fd, iov, iovcnt));
106 
107     if (len == -1 && errno == EAGAIN) {
108         tap_write_poll(s, true);
109         return 0;
110     }
111 
112     return len;
113 }
114 
115 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
116                                int iovcnt)
117 {
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 = { };
122 
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++;
129     }
130 
131     return tap_write_packet(s, iovp, iovcnt);
132 }
133 
134 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
135 {
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 = { };
140 
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++;
145     }
146 
147     iov[iovcnt].iov_base = (char *)buf;
148     iov[iovcnt].iov_len  = size;
149     iovcnt++;
150 
151     return tap_write_packet(s, iov, iovcnt);
152 }
153 
154 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
155 {
156     TAPState *s = DO_UPCAST(TAPState, nc, nc);
157     struct iovec iov[1];
158 
159     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
160         return tap_receive_raw(nc, buf, size);
161     }
162 
163     iov[0].iov_base = (char *)buf;
164     iov[0].iov_len  = size;
165 
166     return tap_write_packet(s, iov, 1);
167 }
168 
169 #ifndef __sun__
170 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
171 {
172     return read(tapfd, buf, maxlen);
173 }
174 #endif
175 
176 static void tap_send_completed(NetClientState *nc, ssize_t len)
177 {
178     TAPState *s = DO_UPCAST(TAPState, nc, nc);
179     tap_read_poll(s, true);
180 }
181 
182 static void tap_send(void *opaque)
183 {
184     TAPState *s = opaque;
185     int size;
186     int packets = 0;
187 
188     while (true) {
189         uint8_t *buf = s->buf;
190         uint8_t min_pkt[ETH_ZLEN];
191         size_t min_pktsz = sizeof(min_pkt);
192 
193         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
194         if (size <= 0) {
195             break;
196         }
197 
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;
201         }
202 
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;
207             }
208         }
209 
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;
216         }
217 
218         /*
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.
223          */
224         packets++;
225         if (packets >= 50) {
226             break;
227         }
228     }
229 }
230 
231 static bool tap_has_ufo(NetClientState *nc)
232 {
233     TAPState *s = DO_UPCAST(TAPState, nc, nc);
234 
235     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
236 
237     return s->has_ufo;
238 }
239 
240 static bool tap_has_vnet_hdr(NetClientState *nc)
241 {
242     TAPState *s = DO_UPCAST(TAPState, nc, nc);
243 
244     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
245 
246     return !!s->host_vnet_hdr_len;
247 }
248 
249 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
250 {
251     TAPState *s = DO_UPCAST(TAPState, nc, nc);
252 
253     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
254 
255     return !!tap_probe_vnet_hdr_len(s->fd, len);
256 }
257 
258 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
259 {
260     TAPState *s = DO_UPCAST(TAPState, nc, nc);
261 
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));
266 
267     tap_fd_set_vnet_hdr_len(s->fd, len);
268     s->host_vnet_hdr_len = len;
269 }
270 
271 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
272 {
273     TAPState *s = DO_UPCAST(TAPState, nc, nc);
274 
275     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
276     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
277 
278     s->using_vnet_hdr = using_vnet_hdr;
279 }
280 
281 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
282 {
283     TAPState *s = DO_UPCAST(TAPState, nc, nc);
284 
285     return tap_fd_set_vnet_le(s->fd, is_le);
286 }
287 
288 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
289 {
290     TAPState *s = DO_UPCAST(TAPState, nc, nc);
291 
292     return tap_fd_set_vnet_be(s->fd, is_be);
293 }
294 
295 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
296                      int tso6, int ecn, int ufo)
297 {
298     TAPState *s = DO_UPCAST(TAPState, nc, nc);
299     if (s->fd < 0) {
300         return;
301     }
302 
303     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
304 }
305 
306 static void tap_exit_notify(Notifier *notifier, void *data)
307 {
308     TAPState *s = container_of(notifier, TAPState, exit);
309     Error *err = NULL;
310 
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);
315         }
316     }
317 }
318 
319 static void tap_cleanup(NetClientState *nc)
320 {
321     TAPState *s = DO_UPCAST(TAPState, nc, nc);
322 
323     if (s->vhost_net) {
324         vhost_net_cleanup(s->vhost_net);
325         g_free(s->vhost_net);
326         s->vhost_net = NULL;
327     }
328 
329     qemu_purge_queued_packets(nc);
330 
331     tap_exit_notify(&s->exit, NULL);
332     qemu_remove_exit_notifier(&s->exit);
333 
334     tap_read_poll(s, false);
335     tap_write_poll(s, false);
336     close(s->fd);
337     s->fd = -1;
338 }
339 
340 static void tap_poll(NetClientState *nc, bool enable)
341 {
342     TAPState *s = DO_UPCAST(TAPState, nc, nc);
343     tap_read_poll(s, enable);
344     tap_write_poll(s, enable);
345 }
346 
347 static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd)
348 {
349     TAPState *s = DO_UPCAST(TAPState, nc, nc);
350     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
351 
352     return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0;
353 }
354 
355 int tap_get_fd(NetClientState *nc)
356 {
357     TAPState *s = DO_UPCAST(TAPState, nc, nc);
358     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
359     return s->fd;
360 }
361 
362 /* fd support */
363 
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,
381 };
382 
383 static TAPState *net_tap_fd_init(NetClientState *peer,
384                                  const char *model,
385                                  const char *name,
386                                  int fd,
387                                  int vnet_hdr)
388 {
389     NetClientState *nc;
390     TAPState *s;
391 
392     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
393 
394     s = DO_UPCAST(TAPState, nc, nc);
395 
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);
402     /*
403      * Make sure host header length is set correctly in tap:
404      * it might have been modified by another instance of qemu.
405      */
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);
408     }
409     tap_read_poll(s, true);
410     s->vhost_net = NULL;
411 
412     s->exit.notify = tap_exit_notify;
413     qemu_add_exit_notifier(&s->exit);
414 
415     return s;
416 }
417 
418 static void launch_script(const char *setup_script, const char *ifname,
419                           int fd, Error **errp)
420 {
421     int pid, status;
422     char *args[3];
423     char **parg;
424 
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;
431     }
432     if (pid == 0) {
433         int open_max = sysconf(_SC_OPEN_MAX), i;
434 
435         for (i = 3; i < open_max; i++) {
436             if (i != fd) {
437                 close(i);
438             }
439         }
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 */
449         }
450 
451         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
452             return;
453         }
454         error_setg(errp, "network script %s failed with status %d",
455                    setup_script, status);
456     }
457 }
458 
459 static int recv_fd(int c)
460 {
461     int fd;
462     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
463     struct msghdr msg = {
464         .msg_control = msgbuf,
465         .msg_controllen = sizeof(msgbuf),
466     };
467     struct cmsghdr *cmsg;
468     struct iovec iov;
469     uint8_t req[1];
470     ssize_t len;
471 
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;
477 
478     iov.iov_base = req;
479     iov.iov_len = sizeof(req);
480 
481     msg.msg_iov = &iov;
482     msg.msg_iovlen = 1;
483 
484     len = recvmsg(c, &msg, 0);
485     if (len > 0) {
486         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
487         return fd;
488     }
489 
490     return len;
491 }
492 
493 static int net_bridge_run_helper(const char *helper, const char *bridge,
494                                  Error **errp)
495 {
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];
502 
503     sigemptyset(&mask);
504     sigaddset(&mask, SIGCHLD);
505     sigprocmask(SIG_BLOCK, &mask, &oldmask);
506 
507     if (!helper) {
508         helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
509     }
510 
511     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
512         error_setg_errno(errp, errno, "socketpair() failed");
513         return -1;
514     }
515 
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;
521     }
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;
527 
528         for (i = 3; i < open_max; i++) {
529             if (i != sv[1]) {
530                 close(i);
531             }
532         }
533 
534         fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
535 
536         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
537             /* assume helper is a command */
538 
539             if (strstr(helper, "--br=") == NULL) {
540                 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
541             }
542 
543             helper_cmd = g_strdup_printf("%s %s %s %s", helper,
544                             "--use-vnet", fd_buf, br_buf ? br_buf : "");
545 
546             parg = args;
547             *parg++ = (char *)"sh";
548             *parg++ = (char *)"-c";
549             *parg++ = helper_cmd;
550             *parg++ = NULL;
551 
552             execv("/bin/sh", args);
553             g_free(helper_cmd);
554         } else {
555             /* assume helper is just the executable path name */
556 
557             br_buf = g_strdup_printf("%s%s", "--br=", bridge);
558 
559             parg = args;
560             *parg++ = (char *)helper;
561             *parg++ = (char *)"--use-vnet";
562             *parg++ = fd_buf;
563             *parg++ = br_buf;
564             *parg++ = NULL;
565 
566             execv(helper, args);
567         }
568         g_free(fd_buf);
569         g_free(br_buf);
570         _exit(1);
571 
572     } else {
573         int fd;
574         int saved_errno;
575 
576         close(sv[1]);
577 
578         fd = RETRY_ON_EINTR(recv_fd(sv[0]));
579         saved_errno = errno;
580 
581         close(sv[0]);
582 
583         while (waitpid(pid, &status, 0) != pid) {
584             /* loop */
585         }
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;
591         }
592         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
593             error_setg(errp, "bridge helper failed");
594             return -1;
595         }
596         return fd;
597     }
598 }
599 
600 int net_init_bridge(const Netdev *netdev, const char *name,
601                     NetClientState *peer, Error **errp)
602 {
603     const NetdevBridgeOptions *bridge;
604     const char *helper, *br;
605     TAPState *s;
606     int fd, vnet_hdr;
607 
608     assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
609     bridge = &netdev->u.bridge;
610     helper = bridge->helper;
611     br     = bridge->br ?: DEFAULT_BRIDGE_INTERFACE;
612 
613     fd = net_bridge_run_helper(helper, br, errp);
614     if (fd == -1) {
615         return -1;
616     }
617 
618     if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
619         error_setg_errno(errp, errno, "Failed to set FD nonblocking");
620         return -1;
621     }
622     vnet_hdr = tap_probe_vnet_hdr(fd, errp);
623     if (vnet_hdr < 0) {
624         close(fd);
625         return -1;
626     }
627     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
628 
629     qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br);
630 
631     return 0;
632 }
633 
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)
637 {
638     Error *err = NULL;
639     int fd, vnet_hdr_required;
640 
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;
647     }
648 
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;
653     }
654 
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;
663         }
664     }
665 
666     return fd;
667 }
668 
669 #define MAX_TAP_QUEUES 1024
670 
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)
676 {
677     Error *err = NULL;
678     TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
679     int vhostfd;
680 
681     tap_set_sndbuf(s->fd, tap, &err);
682     if (err) {
683         error_propagate(errp, err);
684         goto failed;
685     }
686 
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);
694 
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);
699         }
700     }
701 
702     if (tap->has_vhost ? tap->vhost :
703         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
704         VhostNetOptions options;
705 
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;
712         }
713 
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);
721                 }
722                 goto failed;
723             }
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;
728             }
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));
738                 }
739                 goto failed;
740             }
741             if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
742                 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
743                 goto failed;
744             }
745         }
746         options.opaque = (void *)(uintptr_t)vhostfd;
747         options.nvqs = 2;
748 
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);
755             }
756             goto failed;
757         }
758     } else if (vhostfdname) {
759         error_setg(errp, "vhostfd(s)= is not valid without vhost");
760         goto failed;
761     }
762 
763     return;
764 
765 failed:
766     qemu_del_net_client(&s->nc);
767 }
768 
769 static int get_fds(char *str, char *fds[], int max)
770 {
771     char *ptr = str, *this;
772     size_t len = strlen(str);
773     int i = 0;
774 
775     while (i < max && ptr < str + len) {
776         this = strchr(ptr, ':');
777 
778         if (this == NULL) {
779             fds[i] = g_strdup(ptr);
780         } else {
781             fds[i] = g_strndup(ptr, this - ptr);
782         }
783 
784         i++;
785         if (this == NULL) {
786             break;
787         } else {
788             ptr = this + 1;
789         }
790     }
791 
792     return i;
793 }
794 
795 int net_init_tap(const Netdev *netdev, const char *name,
796                  NetClientState *peer, Error **errp)
797 {
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;
807 
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;
814 
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;
820     }
821 
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;
830         }
831 
832         fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
833         if (fd == -1) {
834             return -1;
835         }
836 
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;
842         }
843 
844         vnet_hdr = tap_probe_vnet_hdr(fd, errp);
845         if (vnet_hdr < 0) {
846             close(fd);
847             return -1;
848         }
849 
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;
857         }
858     } else if (tap->fds) {
859         char **fds;
860         char **vhost_fds;
861         int nfds = 0, nvhosts = 0;
862 
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;
870         }
871 
872         fds = g_new0(char *, MAX_TAP_QUEUES);
873         vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
874 
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;
883             }
884         }
885 
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;
891             }
892 
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;
898             }
899 
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;
905                 }
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;
911             }
912 
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;
921             }
922         }
923 
924 free_fail:
925         for (i = 0; i < nvhosts; i++) {
926             g_free(vhost_fds[i]);
927         }
928         for (i = 0; i < nfds; i++) {
929             g_free(fds[i]);
930         }
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;
940         }
941 
942         fd = net_bridge_run_helper(tap->helper,
943                                    tap->br ?: DEFAULT_BRIDGE_INTERFACE,
944                                    errp);
945         if (fd == -1) {
946             return -1;
947         }
948 
949         if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
950             error_setg_errno(errp, errno, "Failed to set FD nonblocking");
951             return -1;
952         }
953         vnet_hdr = tap_probe_vnet_hdr(fd, errp);
954         if (vnet_hdr < 0) {
955             close(fd);
956             return -1;
957         }
958 
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;
966         }
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;
973         }
974 
975         if (!script) {
976             script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
977         }
978         if (!downscript) {
979             downscript = default_downscript =
980                                  get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
981         }
982 
983         if (tap->ifname) {
984             pstrcpy(ifname, sizeof ifname, tap->ifname);
985         } else {
986             ifname[0] = '\0';
987         }
988 
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;
994             }
995 
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;
1001                 }
1002             }
1003 
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;
1012             }
1013         }
1014     }
1015 
1016     return 0;
1017 }
1018 
1019 VHostNetState *tap_get_vhost_net(NetClientState *nc)
1020 {
1021     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1022     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
1023     return s->vhost_net;
1024 }
1025 
1026 int tap_enable(NetClientState *nc)
1027 {
1028     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1029     int ret;
1030 
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);
1038         }
1039         return ret;
1040     }
1041 }
1042 
1043 int tap_disable(NetClientState *nc)
1044 {
1045     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1046     int ret;
1047 
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);
1056         }
1057         return ret;
1058     }
1059 }
1060