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