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