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