xref: /openbmc/qemu/net/tap.c (revision 3df4c288)
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 has_uso;
61     bool enabled;
62     VHostNetState *vhost_net;
63     unsigned host_vnet_hdr_len;
64     Notifier exit;
65 } TAPState;
66 
67 static void launch_script(const char *setup_script, const char *ifname,
68                           int fd, Error **errp);
69 
70 static void tap_send(void *opaque);
71 static void tap_writable(void *opaque);
72 
73 static void tap_update_fd_handler(TAPState *s)
74 {
75     qemu_set_fd_handler(s->fd,
76                         s->read_poll && s->enabled ? tap_send : NULL,
77                         s->write_poll && s->enabled ? tap_writable : NULL,
78                         s);
79 }
80 
81 static void tap_read_poll(TAPState *s, bool enable)
82 {
83     s->read_poll = enable;
84     tap_update_fd_handler(s);
85 }
86 
87 static void tap_write_poll(TAPState *s, bool enable)
88 {
89     s->write_poll = enable;
90     tap_update_fd_handler(s);
91 }
92 
93 static void tap_writable(void *opaque)
94 {
95     TAPState *s = opaque;
96 
97     tap_write_poll(s, false);
98 
99     qemu_flush_queued_packets(&s->nc);
100 }
101 
102 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
103 {
104     ssize_t len;
105 
106     len = RETRY_ON_EINTR(writev(s->fd, iov, iovcnt));
107 
108     if (len == -1 && errno == EAGAIN) {
109         tap_write_poll(s, true);
110         return 0;
111     }
112 
113     return len;
114 }
115 
116 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
117                                int iovcnt)
118 {
119     TAPState *s = DO_UPCAST(TAPState, nc, nc);
120     const struct iovec *iovp = iov;
121     g_autofree struct iovec *iov_copy = NULL;
122     struct virtio_net_hdr_mrg_rxbuf hdr = { };
123 
124     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
125         iov_copy = g_new(struct iovec, iovcnt + 1);
126         iov_copy[0].iov_base = &hdr;
127         iov_copy[0].iov_len =  s->host_vnet_hdr_len;
128         memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
129         iovp = iov_copy;
130         iovcnt++;
131     }
132 
133     return tap_write_packet(s, iovp, iovcnt);
134 }
135 
136 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
137 {
138     TAPState *s = DO_UPCAST(TAPState, nc, nc);
139     struct iovec iov[2];
140     int iovcnt = 0;
141     struct virtio_net_hdr_mrg_rxbuf hdr = { };
142 
143     if (s->host_vnet_hdr_len) {
144         iov[iovcnt].iov_base = &hdr;
145         iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
146         iovcnt++;
147     }
148 
149     iov[iovcnt].iov_base = (char *)buf;
150     iov[iovcnt].iov_len  = size;
151     iovcnt++;
152 
153     return tap_write_packet(s, iov, iovcnt);
154 }
155 
156 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
157 {
158     TAPState *s = DO_UPCAST(TAPState, nc, nc);
159     struct iovec iov[1];
160 
161     if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
162         return tap_receive_raw(nc, buf, size);
163     }
164 
165     iov[0].iov_base = (char *)buf;
166     iov[0].iov_len  = size;
167 
168     return tap_write_packet(s, iov, 1);
169 }
170 
171 #ifndef __sun__
172 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
173 {
174     return read(tapfd, buf, maxlen);
175 }
176 #endif
177 
178 static void tap_send_completed(NetClientState *nc, ssize_t len)
179 {
180     TAPState *s = DO_UPCAST(TAPState, nc, nc);
181     tap_read_poll(s, true);
182 }
183 
184 static void tap_send(void *opaque)
185 {
186     TAPState *s = opaque;
187     int size;
188     int packets = 0;
189 
190     while (true) {
191         uint8_t *buf = s->buf;
192         uint8_t min_pkt[ETH_ZLEN];
193         size_t min_pktsz = sizeof(min_pkt);
194 
195         size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
196         if (size <= 0) {
197             break;
198         }
199 
200         if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
201             buf  += s->host_vnet_hdr_len;
202             size -= s->host_vnet_hdr_len;
203         }
204 
205         if (net_peer_needs_padding(&s->nc)) {
206             if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) {
207                 buf = min_pkt;
208                 size = min_pktsz;
209             }
210         }
211 
212         size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
213         if (size == 0) {
214             tap_read_poll(s, false);
215             break;
216         } else if (size < 0) {
217             break;
218         }
219 
220         /*
221          * When the host keeps receiving more packets while tap_send() is
222          * running we can hog the BQL.  Limit the number of
223          * packets that are processed per tap_send() callback to prevent
224          * stalling the guest.
225          */
226         packets++;
227         if (packets >= 50) {
228             break;
229         }
230     }
231 }
232 
233 static bool tap_has_ufo(NetClientState *nc)
234 {
235     TAPState *s = DO_UPCAST(TAPState, nc, nc);
236 
237     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
238 
239     return s->has_ufo;
240 }
241 
242 static bool tap_has_uso(NetClientState *nc)
243 {
244     TAPState *s = DO_UPCAST(TAPState, nc, nc);
245 
246     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
247 
248     return s->has_uso;
249 }
250 
251 static bool tap_has_vnet_hdr(NetClientState *nc)
252 {
253     TAPState *s = DO_UPCAST(TAPState, nc, nc);
254 
255     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
256 
257     return !!s->host_vnet_hdr_len;
258 }
259 
260 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
261 {
262     TAPState *s = DO_UPCAST(TAPState, nc, nc);
263 
264     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
265 
266     return !!tap_probe_vnet_hdr_len(s->fd, len);
267 }
268 
269 static int tap_get_vnet_hdr_len(NetClientState *nc)
270 {
271     TAPState *s = DO_UPCAST(TAPState, nc, nc);
272 
273     return s->host_vnet_hdr_len;
274 }
275 
276 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
277 {
278     TAPState *s = DO_UPCAST(TAPState, nc, nc);
279 
280     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
281     assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
282            len == sizeof(struct virtio_net_hdr) ||
283            len == sizeof(struct virtio_net_hdr_v1_hash));
284 
285     tap_fd_set_vnet_hdr_len(s->fd, len);
286     s->host_vnet_hdr_len = len;
287 }
288 
289 static bool tap_get_using_vnet_hdr(NetClientState *nc)
290 {
291     TAPState *s = DO_UPCAST(TAPState, nc, nc);
292 
293     return s->using_vnet_hdr;
294 }
295 
296 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
297 {
298     TAPState *s = DO_UPCAST(TAPState, nc, nc);
299 
300     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
301     assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
302 
303     s->using_vnet_hdr = using_vnet_hdr;
304 }
305 
306 static int tap_set_vnet_le(NetClientState *nc, bool is_le)
307 {
308     TAPState *s = DO_UPCAST(TAPState, nc, nc);
309 
310     return tap_fd_set_vnet_le(s->fd, is_le);
311 }
312 
313 static int tap_set_vnet_be(NetClientState *nc, bool is_be)
314 {
315     TAPState *s = DO_UPCAST(TAPState, nc, nc);
316 
317     return tap_fd_set_vnet_be(s->fd, is_be);
318 }
319 
320 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
321                      int tso6, int ecn, int ufo, int uso4, int uso6)
322 {
323     TAPState *s = DO_UPCAST(TAPState, nc, nc);
324     if (s->fd < 0) {
325         return;
326     }
327 
328     tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo, uso4, uso6);
329 }
330 
331 static void tap_exit_notify(Notifier *notifier, void *data)
332 {
333     TAPState *s = container_of(notifier, TAPState, exit);
334     Error *err = NULL;
335 
336     if (s->down_script[0]) {
337         launch_script(s->down_script, s->down_script_arg, s->fd, &err);
338         if (err) {
339             error_report_err(err);
340         }
341     }
342 }
343 
344 static void tap_cleanup(NetClientState *nc)
345 {
346     TAPState *s = DO_UPCAST(TAPState, nc, nc);
347 
348     if (s->vhost_net) {
349         vhost_net_cleanup(s->vhost_net);
350         g_free(s->vhost_net);
351         s->vhost_net = NULL;
352     }
353 
354     qemu_purge_queued_packets(nc);
355 
356     tap_exit_notify(&s->exit, NULL);
357     qemu_remove_exit_notifier(&s->exit);
358 
359     tap_read_poll(s, false);
360     tap_write_poll(s, false);
361     close(s->fd);
362     s->fd = -1;
363 }
364 
365 static void tap_poll(NetClientState *nc, bool enable)
366 {
367     TAPState *s = DO_UPCAST(TAPState, nc, nc);
368     tap_read_poll(s, enable);
369     tap_write_poll(s, enable);
370 }
371 
372 static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd)
373 {
374     TAPState *s = DO_UPCAST(TAPState, nc, nc);
375     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
376 
377     return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0;
378 }
379 
380 int tap_get_fd(NetClientState *nc)
381 {
382     TAPState *s = DO_UPCAST(TAPState, nc, nc);
383     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
384     return s->fd;
385 }
386 
387 /* fd support */
388 
389 static NetClientInfo net_tap_info = {
390     .type = NET_CLIENT_DRIVER_TAP,
391     .size = sizeof(TAPState),
392     .receive = tap_receive,
393     .receive_raw = tap_receive_raw,
394     .receive_iov = tap_receive_iov,
395     .poll = tap_poll,
396     .cleanup = tap_cleanup,
397     .has_ufo = tap_has_ufo,
398     .has_uso = tap_has_uso,
399     .has_vnet_hdr = tap_has_vnet_hdr,
400     .has_vnet_hdr_len = tap_has_vnet_hdr_len,
401     .get_using_vnet_hdr = tap_get_using_vnet_hdr,
402     .using_vnet_hdr = tap_using_vnet_hdr,
403     .set_offload = tap_set_offload,
404     .get_vnet_hdr_len = tap_get_vnet_hdr_len,
405     .set_vnet_hdr_len = tap_set_vnet_hdr_len,
406     .set_vnet_le = tap_set_vnet_le,
407     .set_vnet_be = tap_set_vnet_be,
408     .set_steering_ebpf = tap_set_steering_ebpf,
409 };
410 
411 static TAPState *net_tap_fd_init(NetClientState *peer,
412                                  const char *model,
413                                  const char *name,
414                                  int fd,
415                                  int vnet_hdr)
416 {
417     NetClientState *nc;
418     TAPState *s;
419 
420     nc = qemu_new_net_client(&net_tap_info, peer, model, name);
421 
422     s = DO_UPCAST(TAPState, nc, nc);
423 
424     s->fd = fd;
425     s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
426     s->using_vnet_hdr = false;
427     s->has_ufo = tap_probe_has_ufo(s->fd);
428     s->has_uso = tap_probe_has_uso(s->fd);
429     s->enabled = true;
430     tap_set_offload(&s->nc, 0, 0, 0, 0, 0, 0, 0);
431     /*
432      * Make sure host header length is set correctly in tap:
433      * it might have been modified by another instance of qemu.
434      */
435     if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
436         tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
437     }
438     tap_read_poll(s, true);
439     s->vhost_net = NULL;
440 
441     s->exit.notify = tap_exit_notify;
442     qemu_add_exit_notifier(&s->exit);
443 
444     return s;
445 }
446 
447 static void launch_script(const char *setup_script, const char *ifname,
448                           int fd, Error **errp)
449 {
450     int pid, status;
451     char *args[3];
452     char **parg;
453 
454     /* try to launch network script */
455     pid = fork();
456     if (pid < 0) {
457         error_setg_errno(errp, errno, "could not launch network script %s",
458                          setup_script);
459         return;
460     }
461     if (pid == 0) {
462         int open_max = sysconf(_SC_OPEN_MAX), i;
463 
464         for (i = 3; i < open_max; i++) {
465             if (i != fd) {
466                 close(i);
467             }
468         }
469         parg = args;
470         *parg++ = (char *)setup_script;
471         *parg++ = (char *)ifname;
472         *parg = NULL;
473         execv(setup_script, args);
474         _exit(1);
475     } else {
476         while (waitpid(pid, &status, 0) != pid) {
477             /* loop */
478         }
479 
480         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
481             return;
482         }
483         error_setg(errp, "network script %s failed with status %d",
484                    setup_script, status);
485     }
486 }
487 
488 static int recv_fd(int c)
489 {
490     int fd;
491     uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
492     struct msghdr msg = {
493         .msg_control = msgbuf,
494         .msg_controllen = sizeof(msgbuf),
495     };
496     struct cmsghdr *cmsg;
497     struct iovec iov;
498     uint8_t req[1];
499     ssize_t len;
500 
501     cmsg = CMSG_FIRSTHDR(&msg);
502     cmsg->cmsg_level = SOL_SOCKET;
503     cmsg->cmsg_type = SCM_RIGHTS;
504     cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
505     msg.msg_controllen = cmsg->cmsg_len;
506 
507     iov.iov_base = req;
508     iov.iov_len = sizeof(req);
509 
510     msg.msg_iov = &iov;
511     msg.msg_iovlen = 1;
512 
513     len = recvmsg(c, &msg, 0);
514     if (len > 0) {
515         memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
516         return fd;
517     }
518 
519     return len;
520 }
521 
522 static int net_bridge_run_helper(const char *helper, const char *bridge,
523                                  Error **errp)
524 {
525     sigset_t oldmask, mask;
526     g_autofree char *default_helper = NULL;
527     int pid, status;
528     char *args[5];
529     char **parg;
530     int sv[2];
531 
532     sigemptyset(&mask);
533     sigaddset(&mask, SIGCHLD);
534     sigprocmask(SIG_BLOCK, &mask, &oldmask);
535 
536     if (!helper) {
537         helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
538     }
539 
540     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
541         error_setg_errno(errp, errno, "socketpair() failed");
542         return -1;
543     }
544 
545     /* try to launch bridge helper */
546     pid = fork();
547     if (pid < 0) {
548         error_setg_errno(errp, errno, "Can't fork bridge helper");
549         return -1;
550     }
551     if (pid == 0) {
552         int open_max = sysconf(_SC_OPEN_MAX), i;
553         char *fd_buf = NULL;
554         char *br_buf = NULL;
555         char *helper_cmd = NULL;
556 
557         for (i = 3; i < open_max; i++) {
558             if (i != sv[1]) {
559                 close(i);
560             }
561         }
562 
563         fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
564 
565         if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
566             /* assume helper is a command */
567 
568             if (strstr(helper, "--br=") == NULL) {
569                 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
570             }
571 
572             helper_cmd = g_strdup_printf("%s %s %s %s", helper,
573                             "--use-vnet", fd_buf, br_buf ? br_buf : "");
574 
575             parg = args;
576             *parg++ = (char *)"sh";
577             *parg++ = (char *)"-c";
578             *parg++ = helper_cmd;
579             *parg++ = NULL;
580 
581             execv("/bin/sh", args);
582             g_free(helper_cmd);
583         } else {
584             /* assume helper is just the executable path name */
585 
586             br_buf = g_strdup_printf("%s%s", "--br=", bridge);
587 
588             parg = args;
589             *parg++ = (char *)helper;
590             *parg++ = (char *)"--use-vnet";
591             *parg++ = fd_buf;
592             *parg++ = br_buf;
593             *parg++ = NULL;
594 
595             execv(helper, args);
596         }
597         g_free(fd_buf);
598         g_free(br_buf);
599         _exit(1);
600 
601     } else {
602         int fd;
603         int saved_errno;
604 
605         close(sv[1]);
606 
607         fd = RETRY_ON_EINTR(recv_fd(sv[0]));
608         saved_errno = errno;
609 
610         close(sv[0]);
611 
612         while (waitpid(pid, &status, 0) != pid) {
613             /* loop */
614         }
615         sigprocmask(SIG_SETMASK, &oldmask, NULL);
616         if (fd < 0) {
617             error_setg_errno(errp, saved_errno,
618                              "failed to recv file descriptor");
619             return -1;
620         }
621         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
622             error_setg(errp, "bridge helper failed");
623             return -1;
624         }
625         return fd;
626     }
627 }
628 
629 int net_init_bridge(const Netdev *netdev, const char *name,
630                     NetClientState *peer, Error **errp)
631 {
632     const NetdevBridgeOptions *bridge;
633     const char *helper, *br;
634     TAPState *s;
635     int fd, vnet_hdr;
636 
637     assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
638     bridge = &netdev->u.bridge;
639     helper = bridge->helper;
640     br     = bridge->br ?: DEFAULT_BRIDGE_INTERFACE;
641 
642     fd = net_bridge_run_helper(helper, br, errp);
643     if (fd == -1) {
644         return -1;
645     }
646 
647     if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
648         error_setg_errno(errp, errno, "Failed to set FD nonblocking");
649         return -1;
650     }
651     vnet_hdr = tap_probe_vnet_hdr(fd, errp);
652     if (vnet_hdr < 0) {
653         close(fd);
654         return -1;
655     }
656     s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
657 
658     qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br);
659 
660     return 0;
661 }
662 
663 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
664                         const char *setup_script, char *ifname,
665                         size_t ifname_sz, int mq_required, Error **errp)
666 {
667     Error *err = NULL;
668     int fd, vnet_hdr_required;
669 
670     if (tap->has_vnet_hdr) {
671         *vnet_hdr = tap->vnet_hdr;
672         vnet_hdr_required = *vnet_hdr;
673     } else {
674         *vnet_hdr = 1;
675         vnet_hdr_required = 0;
676     }
677 
678     fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
679                       mq_required, errp));
680     if (fd < 0) {
681         return -1;
682     }
683 
684     if (setup_script &&
685         setup_script[0] != '\0' &&
686         strcmp(setup_script, "no") != 0) {
687         launch_script(setup_script, ifname, fd, &err);
688         if (err) {
689             error_propagate(errp, err);
690             close(fd);
691             return -1;
692         }
693     }
694 
695     return fd;
696 }
697 
698 #define MAX_TAP_QUEUES 1024
699 
700 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
701                              const char *model, const char *name,
702                              const char *ifname, const char *script,
703                              const char *downscript, const char *vhostfdname,
704                              int vnet_hdr, int fd, Error **errp)
705 {
706     Error *err = NULL;
707     TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
708     int vhostfd;
709 
710     tap_set_sndbuf(s->fd, tap, &err);
711     if (err) {
712         error_propagate(errp, err);
713         goto failed;
714     }
715 
716     if (tap->fd || tap->fds) {
717         qemu_set_info_str(&s->nc, "fd=%d", fd);
718     } else if (tap->helper) {
719         qemu_set_info_str(&s->nc, "helper=%s", tap->helper);
720     } else {
721         qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname,
722                           script, downscript);
723 
724         if (strcmp(downscript, "no") != 0) {
725             snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
726             snprintf(s->down_script_arg, sizeof(s->down_script_arg),
727                      "%s", ifname);
728         }
729     }
730 
731     if (tap->has_vhost ? tap->vhost :
732         vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
733         VhostNetOptions options;
734 
735         options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
736         options.net_backend = &s->nc;
737         if (tap->has_poll_us) {
738             options.busyloop_timeout = tap->poll_us;
739         } else {
740             options.busyloop_timeout = 0;
741         }
742 
743         if (vhostfdname) {
744             vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
745             if (vhostfd == -1) {
746                 error_propagate(errp, err);
747                 goto failed;
748             }
749             if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
750                 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
751                                  name, fd);
752                 goto failed;
753             }
754         } else {
755             vhostfd = open("/dev/vhost-net", O_RDWR);
756             if (vhostfd < 0) {
757                 error_setg_errno(errp, errno,
758                                  "tap: open vhost char device failed");
759                 goto failed;
760             }
761             if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) {
762                 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
763                 goto failed;
764             }
765         }
766         options.opaque = (void *)(uintptr_t)vhostfd;
767         options.nvqs = 2;
768 
769         s->vhost_net = vhost_net_init(&options);
770         if (!s->vhost_net) {
771             error_setg(errp,
772                        "vhost-net requested but could not be initialized");
773             goto failed;
774         }
775     } else if (vhostfdname) {
776         error_setg(errp, "vhostfd(s)= is not valid without vhost");
777         goto failed;
778     }
779 
780     return;
781 
782 failed:
783     qemu_del_net_client(&s->nc);
784 }
785 
786 static int get_fds(char *str, char *fds[], int max)
787 {
788     char *ptr = str, *this;
789     size_t len = strlen(str);
790     int i = 0;
791 
792     while (i < max && ptr < str + len) {
793         this = strchr(ptr, ':');
794 
795         if (this == NULL) {
796             fds[i] = g_strdup(ptr);
797         } else {
798             fds[i] = g_strndup(ptr, this - ptr);
799         }
800 
801         i++;
802         if (this == NULL) {
803             break;
804         } else {
805             ptr = this + 1;
806         }
807     }
808 
809     return i;
810 }
811 
812 int net_init_tap(const Netdev *netdev, const char *name,
813                  NetClientState *peer, Error **errp)
814 {
815     const NetdevTapOptions *tap;
816     int fd, vnet_hdr = 0, i = 0, queues;
817     /* for the no-fd, no-helper case */
818     const char *script;
819     const char *downscript;
820     Error *err = NULL;
821     const char *vhostfdname;
822     char ifname[128];
823     int ret = 0;
824 
825     assert(netdev->type == NET_CLIENT_DRIVER_TAP);
826     tap = &netdev->u.tap;
827     queues = tap->has_queues ? tap->queues : 1;
828     vhostfdname = tap->vhostfd;
829     script = tap->script;
830     downscript = tap->downscript;
831 
832     /* QEMU hubs do not support multiqueue tap, in this case peer is set.
833      * For -netdev, peer is always NULL. */
834     if (peer && (tap->has_queues || tap->fds || tap->vhostfds)) {
835         error_setg(errp, "Multiqueue tap cannot be used with hubs");
836         return -1;
837     }
838 
839     if (tap->fd) {
840         if (tap->ifname || tap->script || tap->downscript ||
841             tap->has_vnet_hdr || tap->helper || tap->has_queues ||
842             tap->fds || tap->vhostfds) {
843             error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
844                        "helper=, queues=, fds=, and vhostfds= "
845                        "are invalid with fd=");
846             return -1;
847         }
848 
849         fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
850         if (fd == -1) {
851             return -1;
852         }
853 
854         if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
855             error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
856                              name, fd);
857             close(fd);
858             return -1;
859         }
860 
861         vnet_hdr = tap_probe_vnet_hdr(fd, errp);
862         if (vnet_hdr < 0) {
863             close(fd);
864             return -1;
865         }
866 
867         net_init_tap_one(tap, peer, "tap", name, NULL,
868                          script, downscript,
869                          vhostfdname, vnet_hdr, fd, &err);
870         if (err) {
871             error_propagate(errp, err);
872             close(fd);
873             return -1;
874         }
875     } else if (tap->fds) {
876         char **fds;
877         char **vhost_fds;
878         int nfds = 0, nvhosts = 0;
879 
880         if (tap->ifname || tap->script || tap->downscript ||
881             tap->has_vnet_hdr || tap->helper || tap->has_queues ||
882             tap->vhostfd) {
883             error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
884                        "helper=, queues=, and vhostfd= "
885                        "are invalid with fds=");
886             return -1;
887         }
888 
889         fds = g_new0(char *, MAX_TAP_QUEUES);
890         vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
891 
892         nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
893         if (tap->vhostfds) {
894             nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
895             if (nfds != nvhosts) {
896                 error_setg(errp, "The number of fds passed does not match "
897                            "the number of vhostfds passed");
898                 ret = -1;
899                 goto free_fail;
900             }
901         }
902 
903         for (i = 0; i < nfds; i++) {
904             fd = monitor_fd_param(monitor_cur(), fds[i], errp);
905             if (fd == -1) {
906                 ret = -1;
907                 goto free_fail;
908             }
909 
910             ret = g_unix_set_fd_nonblocking(fd, true, NULL);
911             if (!ret) {
912                 error_setg_errno(errp, errno, "%s: Can't use file descriptor %d",
913                                  name, fd);
914                 goto free_fail;
915             }
916 
917             if (i == 0) {
918                 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
919                 if (vnet_hdr < 0) {
920                     ret = -1;
921                     goto free_fail;
922                 }
923             } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
924                 error_setg(errp,
925                            "vnet_hdr not consistent across given tap fds");
926                 ret = -1;
927                 goto free_fail;
928             }
929 
930             net_init_tap_one(tap, peer, "tap", name, ifname,
931                              script, downscript,
932                              tap->vhostfds ? vhost_fds[i] : NULL,
933                              vnet_hdr, fd, &err);
934             if (err) {
935                 error_propagate(errp, err);
936                 ret = -1;
937                 goto free_fail;
938             }
939         }
940 
941 free_fail:
942         for (i = 0; i < nvhosts; i++) {
943             g_free(vhost_fds[i]);
944         }
945         for (i = 0; i < nfds; i++) {
946             g_free(fds[i]);
947         }
948         g_free(fds);
949         g_free(vhost_fds);
950         return ret;
951     } else if (tap->helper) {
952         if (tap->ifname || tap->script || tap->downscript ||
953             tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) {
954             error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
955                        "queues=, and vhostfds= are invalid with helper=");
956             return -1;
957         }
958 
959         fd = net_bridge_run_helper(tap->helper,
960                                    tap->br ?: DEFAULT_BRIDGE_INTERFACE,
961                                    errp);
962         if (fd == -1) {
963             return -1;
964         }
965 
966         if (!g_unix_set_fd_nonblocking(fd, true, NULL)) {
967             error_setg_errno(errp, errno, "Failed to set FD nonblocking");
968             return -1;
969         }
970         vnet_hdr = tap_probe_vnet_hdr(fd, errp);
971         if (vnet_hdr < 0) {
972             close(fd);
973             return -1;
974         }
975 
976         net_init_tap_one(tap, peer, "bridge", name, ifname,
977                          script, downscript, vhostfdname,
978                          vnet_hdr, fd, &err);
979         if (err) {
980             error_propagate(errp, err);
981             close(fd);
982             return -1;
983         }
984     } else {
985         g_autofree char *default_script = NULL;
986         g_autofree char *default_downscript = NULL;
987         if (tap->vhostfds) {
988             error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
989             return -1;
990         }
991 
992         if (!script) {
993             script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
994         }
995         if (!downscript) {
996             downscript = default_downscript =
997                                  get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
998         }
999 
1000         if (tap->ifname) {
1001             pstrcpy(ifname, sizeof ifname, tap->ifname);
1002         } else {
1003             ifname[0] = '\0';
1004         }
1005 
1006         for (i = 0; i < queues; i++) {
1007             fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
1008                               ifname, sizeof ifname, queues > 1, errp);
1009             if (fd == -1) {
1010                 return -1;
1011             }
1012 
1013             if (queues > 1 && i == 0 && !tap->ifname) {
1014                 if (tap_fd_get_ifname(fd, ifname)) {
1015                     error_setg(errp, "Fail to get ifname");
1016                     close(fd);
1017                     return -1;
1018                 }
1019             }
1020 
1021             net_init_tap_one(tap, peer, "tap", name, ifname,
1022                              i >= 1 ? "no" : script,
1023                              i >= 1 ? "no" : downscript,
1024                              vhostfdname, vnet_hdr, fd, &err);
1025             if (err) {
1026                 error_propagate(errp, err);
1027                 close(fd);
1028                 return -1;
1029             }
1030         }
1031     }
1032 
1033     return 0;
1034 }
1035 
1036 VHostNetState *tap_get_vhost_net(NetClientState *nc)
1037 {
1038     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1039     assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
1040     return s->vhost_net;
1041 }
1042 
1043 int tap_enable(NetClientState *nc)
1044 {
1045     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1046     int ret;
1047 
1048     if (s->enabled) {
1049         return 0;
1050     } else {
1051         ret = tap_fd_enable(s->fd);
1052         if (ret == 0) {
1053             s->enabled = true;
1054             tap_update_fd_handler(s);
1055         }
1056         return ret;
1057     }
1058 }
1059 
1060 int tap_disable(NetClientState *nc)
1061 {
1062     TAPState *s = DO_UPCAST(TAPState, nc, nc);
1063     int ret;
1064 
1065     if (s->enabled == 0) {
1066         return 0;
1067     } else {
1068         ret = tap_fd_disable(s->fd);
1069         if (ret == 0) {
1070             qemu_purge_queued_packets(nc);
1071             s->enabled = false;
1072             tap_update_fd_handler(s);
1073         }
1074         return ret;
1075     }
1076 }
1077