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