]> git.proxmox.com Git - mirror_qemu.git/blob - net/tap.c
tap: Permit incremental conversion of tap_open() to Error
[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 "tap_int.h"
27
28 #include "config-host.h"
29
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <net/if.h>
35
36 #include "net/net.h"
37 #include "clients.h"
38 #include "monitor/monitor.h"
39 #include "sysemu/sysemu.h"
40 #include "qemu-common.h"
41 #include "qemu/error-report.h"
42
43 #include "net/tap.h"
44
45 #include "net/vhost_net.h"
46
47 typedef struct TAPState {
48 NetClientState nc;
49 int fd;
50 char down_script[1024];
51 char down_script_arg[128];
52 uint8_t buf[NET_BUFSIZE];
53 bool read_poll;
54 bool write_poll;
55 bool using_vnet_hdr;
56 bool has_ufo;
57 bool enabled;
58 VHostNetState *vhost_net;
59 unsigned host_vnet_hdr_len;
60 } TAPState;
61
62 static void launch_script(const char *setup_script, const char *ifname,
63 int fd, Error **errp);
64
65 static int tap_can_send(void *opaque);
66 static void tap_send(void *opaque);
67 static void tap_writable(void *opaque);
68
69 static void tap_update_fd_handler(TAPState *s)
70 {
71 qemu_set_fd_handler2(s->fd,
72 s->read_poll && s->enabled ? tap_can_send : NULL,
73 s->read_poll && s->enabled ? tap_send : NULL,
74 s->write_poll && s->enabled ? tap_writable : NULL,
75 s);
76 }
77
78 static void tap_read_poll(TAPState *s, bool enable)
79 {
80 s->read_poll = enable;
81 tap_update_fd_handler(s);
82 }
83
84 static void tap_write_poll(TAPState *s, bool enable)
85 {
86 s->write_poll = enable;
87 tap_update_fd_handler(s);
88 }
89
90 static void tap_writable(void *opaque)
91 {
92 TAPState *s = opaque;
93
94 tap_write_poll(s, false);
95
96 qemu_flush_queued_packets(&s->nc);
97 }
98
99 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
100 {
101 ssize_t len;
102
103 do {
104 len = writev(s->fd, iov, iovcnt);
105 } while (len == -1 && errno == EINTR);
106
107 if (len == -1 && errno == EAGAIN) {
108 tap_write_poll(s, true);
109 return 0;
110 }
111
112 return len;
113 }
114
115 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
116 int iovcnt)
117 {
118 TAPState *s = DO_UPCAST(TAPState, nc, nc);
119 const struct iovec *iovp = iov;
120 struct iovec iov_copy[iovcnt + 1];
121 struct virtio_net_hdr_mrg_rxbuf hdr = { };
122
123 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
124 iov_copy[0].iov_base = &hdr;
125 iov_copy[0].iov_len = s->host_vnet_hdr_len;
126 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
127 iovp = iov_copy;
128 iovcnt++;
129 }
130
131 return tap_write_packet(s, iovp, iovcnt);
132 }
133
134 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
135 {
136 TAPState *s = DO_UPCAST(TAPState, nc, nc);
137 struct iovec iov[2];
138 int iovcnt = 0;
139 struct virtio_net_hdr_mrg_rxbuf hdr = { };
140
141 if (s->host_vnet_hdr_len) {
142 iov[iovcnt].iov_base = &hdr;
143 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
144 iovcnt++;
145 }
146
147 iov[iovcnt].iov_base = (char *)buf;
148 iov[iovcnt].iov_len = size;
149 iovcnt++;
150
151 return tap_write_packet(s, iov, iovcnt);
152 }
153
154 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
155 {
156 TAPState *s = DO_UPCAST(TAPState, nc, nc);
157 struct iovec iov[1];
158
159 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
160 return tap_receive_raw(nc, buf, size);
161 }
162
163 iov[0].iov_base = (char *)buf;
164 iov[0].iov_len = size;
165
166 return tap_write_packet(s, iov, 1);
167 }
168
169 static int tap_can_send(void *opaque)
170 {
171 TAPState *s = opaque;
172
173 return qemu_can_send_packet(&s->nc);
174 }
175
176 #ifndef __sun__
177 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
178 {
179 return read(tapfd, buf, maxlen);
180 }
181 #endif
182
183 static void tap_send_completed(NetClientState *nc, ssize_t len)
184 {
185 TAPState *s = DO_UPCAST(TAPState, nc, nc);
186 tap_read_poll(s, true);
187 }
188
189 static void tap_send(void *opaque)
190 {
191 TAPState *s = opaque;
192 int size;
193 int packets = 0;
194
195 while (qemu_can_send_packet(&s->nc)) {
196 uint8_t *buf = s->buf;
197
198 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
199 if (size <= 0) {
200 break;
201 }
202
203 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
204 buf += s->host_vnet_hdr_len;
205 size -= s->host_vnet_hdr_len;
206 }
207
208 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
209 if (size == 0) {
210 tap_read_poll(s, false);
211 break;
212 } else if (size < 0) {
213 break;
214 }
215
216 /*
217 * When the host keeps receiving more packets while tap_send() is
218 * running we can hog the QEMU global mutex. Limit the number of
219 * packets that are processed per tap_send() callback to prevent
220 * stalling the guest.
221 */
222 packets++;
223 if (packets >= 50) {
224 break;
225 }
226 }
227 }
228
229 static bool tap_has_ufo(NetClientState *nc)
230 {
231 TAPState *s = DO_UPCAST(TAPState, nc, nc);
232
233 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
234
235 return s->has_ufo;
236 }
237
238 static bool tap_has_vnet_hdr(NetClientState *nc)
239 {
240 TAPState *s = DO_UPCAST(TAPState, nc, nc);
241
242 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
243
244 return !!s->host_vnet_hdr_len;
245 }
246
247 static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
248 {
249 TAPState *s = DO_UPCAST(TAPState, nc, nc);
250
251 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
252
253 return !!tap_probe_vnet_hdr_len(s->fd, len);
254 }
255
256 static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
257 {
258 TAPState *s = DO_UPCAST(TAPState, nc, nc);
259
260 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
261 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
262 len == sizeof(struct virtio_net_hdr));
263
264 tap_fd_set_vnet_hdr_len(s->fd, len);
265 s->host_vnet_hdr_len = len;
266 }
267
268 static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
269 {
270 TAPState *s = DO_UPCAST(TAPState, nc, nc);
271
272 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
273 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
274
275 s->using_vnet_hdr = using_vnet_hdr;
276 }
277
278 static void tap_set_offload(NetClientState *nc, int csum, int tso4,
279 int tso6, int ecn, int ufo)
280 {
281 TAPState *s = DO_UPCAST(TAPState, nc, nc);
282 if (s->fd < 0) {
283 return;
284 }
285
286 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
287 }
288
289 static void tap_cleanup(NetClientState *nc)
290 {
291 TAPState *s = DO_UPCAST(TAPState, nc, nc);
292 Error *err = NULL;
293
294 if (s->vhost_net) {
295 vhost_net_cleanup(s->vhost_net);
296 s->vhost_net = NULL;
297 }
298
299 qemu_purge_queued_packets(nc);
300
301 if (s->down_script[0]) {
302 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
303 if (err) {
304 error_report_err(err);
305 }
306 }
307
308 tap_read_poll(s, false);
309 tap_write_poll(s, false);
310 close(s->fd);
311 s->fd = -1;
312 }
313
314 static void tap_poll(NetClientState *nc, bool enable)
315 {
316 TAPState *s = DO_UPCAST(TAPState, nc, nc);
317 tap_read_poll(s, enable);
318 tap_write_poll(s, enable);
319 }
320
321 int tap_get_fd(NetClientState *nc)
322 {
323 TAPState *s = DO_UPCAST(TAPState, nc, nc);
324 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
325 return s->fd;
326 }
327
328 /* fd support */
329
330 static NetClientInfo net_tap_info = {
331 .type = NET_CLIENT_OPTIONS_KIND_TAP,
332 .size = sizeof(TAPState),
333 .receive = tap_receive,
334 .receive_raw = tap_receive_raw,
335 .receive_iov = tap_receive_iov,
336 .poll = tap_poll,
337 .cleanup = tap_cleanup,
338 .has_ufo = tap_has_ufo,
339 .has_vnet_hdr = tap_has_vnet_hdr,
340 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
341 .using_vnet_hdr = tap_using_vnet_hdr,
342 .set_offload = tap_set_offload,
343 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
344 };
345
346 static TAPState *net_tap_fd_init(NetClientState *peer,
347 const char *model,
348 const char *name,
349 int fd,
350 int vnet_hdr)
351 {
352 NetClientState *nc;
353 TAPState *s;
354
355 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
356
357 s = DO_UPCAST(TAPState, nc, nc);
358
359 s->fd = fd;
360 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
361 s->using_vnet_hdr = false;
362 s->has_ufo = tap_probe_has_ufo(s->fd);
363 s->enabled = true;
364 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
365 /*
366 * Make sure host header length is set correctly in tap:
367 * it might have been modified by another instance of qemu.
368 */
369 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
370 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
371 }
372 tap_read_poll(s, true);
373 s->vhost_net = NULL;
374 return s;
375 }
376
377 static void launch_script(const char *setup_script, const char *ifname,
378 int fd, Error **errp)
379 {
380 int pid, status;
381 char *args[3];
382 char **parg;
383
384 /* try to launch network script */
385 pid = fork();
386 if (pid < 0) {
387 error_setg_errno(errp, errno, "could not launch network script %s",
388 setup_script);
389 return;
390 }
391 if (pid == 0) {
392 int open_max = sysconf(_SC_OPEN_MAX), i;
393
394 for (i = 3; i < open_max; i++) {
395 if (i != fd) {
396 close(i);
397 }
398 }
399 parg = args;
400 *parg++ = (char *)setup_script;
401 *parg++ = (char *)ifname;
402 *parg = NULL;
403 execv(setup_script, args);
404 _exit(1);
405 } else {
406 while (waitpid(pid, &status, 0) != pid) {
407 /* loop */
408 }
409
410 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
411 return;
412 }
413 error_setg(errp, "network script %s failed with status %d",
414 setup_script, status);
415 }
416 }
417
418 static int recv_fd(int c)
419 {
420 int fd;
421 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
422 struct msghdr msg = {
423 .msg_control = msgbuf,
424 .msg_controllen = sizeof(msgbuf),
425 };
426 struct cmsghdr *cmsg;
427 struct iovec iov;
428 uint8_t req[1];
429 ssize_t len;
430
431 cmsg = CMSG_FIRSTHDR(&msg);
432 cmsg->cmsg_level = SOL_SOCKET;
433 cmsg->cmsg_type = SCM_RIGHTS;
434 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
435 msg.msg_controllen = cmsg->cmsg_len;
436
437 iov.iov_base = req;
438 iov.iov_len = sizeof(req);
439
440 msg.msg_iov = &iov;
441 msg.msg_iovlen = 1;
442
443 len = recvmsg(c, &msg, 0);
444 if (len > 0) {
445 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
446 return fd;
447 }
448
449 return len;
450 }
451
452 static int net_bridge_run_helper(const char *helper, const char *bridge,
453 Error **errp)
454 {
455 sigset_t oldmask, mask;
456 int pid, status;
457 char *args[5];
458 char **parg;
459 int sv[2];
460
461 sigemptyset(&mask);
462 sigaddset(&mask, SIGCHLD);
463 sigprocmask(SIG_BLOCK, &mask, &oldmask);
464
465 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
466 error_setg_errno(errp, errno, "socketpair() failed");
467 return -1;
468 }
469
470 /* try to launch bridge helper */
471 pid = fork();
472 if (pid < 0) {
473 error_setg_errno(errp, errno, "Can't fork bridge helper");
474 return -1;
475 }
476 if (pid == 0) {
477 int open_max = sysconf(_SC_OPEN_MAX), i;
478 char fd_buf[6+10];
479 char br_buf[6+IFNAMSIZ] = {0};
480 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
481
482 for (i = 3; i < open_max; i++) {
483 if (i != sv[1]) {
484 close(i);
485 }
486 }
487
488 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
489
490 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
491 /* assume helper is a command */
492
493 if (strstr(helper, "--br=") == NULL) {
494 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
495 }
496
497 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
498 helper, "--use-vnet", fd_buf, br_buf);
499
500 parg = args;
501 *parg++ = (char *)"sh";
502 *parg++ = (char *)"-c";
503 *parg++ = helper_cmd;
504 *parg++ = NULL;
505
506 execv("/bin/sh", args);
507 } else {
508 /* assume helper is just the executable path name */
509
510 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
511
512 parg = args;
513 *parg++ = (char *)helper;
514 *parg++ = (char *)"--use-vnet";
515 *parg++ = fd_buf;
516 *parg++ = br_buf;
517 *parg++ = NULL;
518
519 execv(helper, args);
520 }
521 _exit(1);
522
523 } else {
524 int fd;
525 int saved_errno;
526
527 close(sv[1]);
528
529 do {
530 fd = recv_fd(sv[0]);
531 } while (fd == -1 && errno == EINTR);
532 saved_errno = errno;
533
534 close(sv[0]);
535
536 while (waitpid(pid, &status, 0) != pid) {
537 /* loop */
538 }
539 sigprocmask(SIG_SETMASK, &oldmask, NULL);
540 if (fd < 0) {
541 error_setg_errno(errp, saved_errno,
542 "failed to recv file descriptor");
543 return -1;
544 }
545 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
546 error_setg(errp, "bridge helper failed");
547 return -1;
548 }
549 return fd;
550 }
551 }
552
553 int net_init_bridge(const NetClientOptions *opts, const char *name,
554 NetClientState *peer, Error **errp)
555 {
556 const NetdevBridgeOptions *bridge;
557 const char *helper, *br;
558 TAPState *s;
559 int fd, vnet_hdr;
560
561 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
562 bridge = opts->bridge;
563
564 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
565 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
566
567 fd = net_bridge_run_helper(helper, br, errp);
568 if (fd == -1) {
569 return -1;
570 }
571
572 fcntl(fd, F_SETFL, O_NONBLOCK);
573 vnet_hdr = tap_probe_vnet_hdr(fd);
574 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
575
576 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
577 br);
578
579 return 0;
580 }
581
582 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
583 const char *setup_script, char *ifname,
584 size_t ifname_sz, int mq_required, Error **errp)
585 {
586 Error *err = NULL;
587 int fd, vnet_hdr_required;
588
589 if (tap->has_vnet_hdr) {
590 *vnet_hdr = tap->vnet_hdr;
591 vnet_hdr_required = *vnet_hdr;
592 } else {
593 *vnet_hdr = 1;
594 vnet_hdr_required = 0;
595 }
596
597 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
598 mq_required, errp));
599 if (fd < 0) {
600 /* FIXME drop when all tap_open() store an Error */
601 if (errp && !*errp) {
602 error_setg(errp, "can't open tap device");
603 }
604 return -1;
605 }
606
607 if (setup_script &&
608 setup_script[0] != '\0' &&
609 strcmp(setup_script, "no") != 0) {
610 launch_script(setup_script, ifname, fd, &err);
611 if (err) {
612 error_propagate(errp, err);
613 close(fd);
614 return -1;
615 }
616 }
617
618 return fd;
619 }
620
621 #define MAX_TAP_QUEUES 1024
622
623 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
624 const char *model, const char *name,
625 const char *ifname, const char *script,
626 const char *downscript, const char *vhostfdname,
627 int vnet_hdr, int fd, Error **errp)
628 {
629 Error *err = NULL;
630 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
631 int vhostfd;
632
633 tap_set_sndbuf(s->fd, tap, &err);
634 if (err) {
635 error_propagate(errp, err);
636 return;
637 }
638
639 if (tap->has_fd || tap->has_fds) {
640 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
641 } else if (tap->has_helper) {
642 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
643 tap->helper);
644 } else {
645 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
646 "ifname=%s,script=%s,downscript=%s", ifname, script,
647 downscript);
648
649 if (strcmp(downscript, "no") != 0) {
650 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
651 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
652 "%s", ifname);
653 }
654 }
655
656 if (tap->has_vhost ? tap->vhost :
657 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
658 VhostNetOptions options;
659
660 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
661 options.net_backend = &s->nc;
662 options.force = tap->has_vhostforce && tap->vhostforce;
663
664 if (tap->has_vhostfd || tap->has_vhostfds) {
665 vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
666 if (vhostfd == -1) {
667 error_propagate(errp, err);
668 return;
669 }
670 } else {
671 vhostfd = open("/dev/vhost-net", O_RDWR);
672 if (vhostfd < 0) {
673 error_setg_errno(errp, errno,
674 "tap: open vhost char device failed");
675 return;
676 }
677 }
678 options.opaque = (void *)(uintptr_t)vhostfd;
679
680 s->vhost_net = vhost_net_init(&options);
681 if (!s->vhost_net) {
682 error_setg(errp,
683 "vhost-net requested but could not be initialized");
684 return;
685 }
686 } else if (tap->has_vhostfd || tap->has_vhostfds) {
687 error_setg(errp, "vhostfd= is not valid without vhost");
688 }
689 }
690
691 static int get_fds(char *str, char *fds[], int max)
692 {
693 char *ptr = str, *this;
694 size_t len = strlen(str);
695 int i = 0;
696
697 while (i < max && ptr < str + len) {
698 this = strchr(ptr, ':');
699
700 if (this == NULL) {
701 fds[i] = g_strdup(ptr);
702 } else {
703 fds[i] = g_strndup(ptr, this - ptr);
704 }
705
706 i++;
707 if (this == NULL) {
708 break;
709 } else {
710 ptr = this + 1;
711 }
712 }
713
714 return i;
715 }
716
717 int net_init_tap(const NetClientOptions *opts, const char *name,
718 NetClientState *peer, Error **errp)
719 {
720 /* FIXME error_setg(errp, ...) on failure */
721 const NetdevTapOptions *tap;
722 int fd, vnet_hdr = 0, i = 0, queues;
723 /* for the no-fd, no-helper case */
724 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
725 const char *downscript = NULL;
726 Error *err = NULL;
727 const char *vhostfdname;
728 char ifname[128];
729
730 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
731 tap = opts->tap;
732 queues = tap->has_queues ? tap->queues : 1;
733 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
734
735 /* QEMU vlans does not support multiqueue tap, in this case peer is set.
736 * For -netdev, peer is always NULL. */
737 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
738 error_report("Multiqueue tap cannot be used with QEMU vlans");
739 return -1;
740 }
741
742 if (tap->has_fd) {
743 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
744 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
745 tap->has_fds || tap->has_vhostfds) {
746 error_report("ifname=, script=, downscript=, vnet_hdr=, "
747 "helper=, queues=, fds=, and vhostfds= "
748 "are invalid with fd=");
749 return -1;
750 }
751
752 fd = monitor_fd_param(cur_mon, tap->fd, &err);
753 if (fd == -1) {
754 error_report_err(err);
755 return -1;
756 }
757
758 fcntl(fd, F_SETFL, O_NONBLOCK);
759
760 vnet_hdr = tap_probe_vnet_hdr(fd);
761
762 net_init_tap_one(tap, peer, "tap", name, NULL,
763 script, downscript,
764 vhostfdname, vnet_hdr, fd, &err);
765 if (err) {
766 error_report_err(err);
767 return -1;
768 }
769 } else if (tap->has_fds) {
770 char *fds[MAX_TAP_QUEUES];
771 char *vhost_fds[MAX_TAP_QUEUES];
772 int nfds, nvhosts;
773
774 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
775 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
776 tap->has_vhostfd) {
777 error_report("ifname=, script=, downscript=, vnet_hdr=, "
778 "helper=, queues=, and vhostfd= "
779 "are invalid with fds=");
780 return -1;
781 }
782
783 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
784 if (tap->has_vhostfds) {
785 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
786 if (nfds != nvhosts) {
787 error_report("The number of fds passed does not match the "
788 "number of vhostfds passed");
789 return -1;
790 }
791 }
792
793 for (i = 0; i < nfds; i++) {
794 fd = monitor_fd_param(cur_mon, fds[i], &err);
795 if (fd == -1) {
796 error_report_err(err);
797 return -1;
798 }
799
800 fcntl(fd, F_SETFL, O_NONBLOCK);
801
802 if (i == 0) {
803 vnet_hdr = tap_probe_vnet_hdr(fd);
804 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
805 error_report("vnet_hdr not consistent across given tap fds");
806 return -1;
807 }
808
809 net_init_tap_one(tap, peer, "tap", name, ifname,
810 script, downscript,
811 tap->has_vhostfds ? vhost_fds[i] : NULL,
812 vnet_hdr, fd, &err);
813 if (err) {
814 error_report_err(err);
815 return -1;
816 }
817 }
818 } else if (tap->has_helper) {
819 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
820 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
821 error_report("ifname=, script=, downscript=, and vnet_hdr= "
822 "queues=, and vhostfds= are invalid with helper=");
823 return -1;
824 }
825
826 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE,
827 errp);
828 if (fd == -1) {
829 return -1;
830 }
831
832 fcntl(fd, F_SETFL, O_NONBLOCK);
833 vnet_hdr = tap_probe_vnet_hdr(fd);
834
835 net_init_tap_one(tap, peer, "bridge", name, ifname,
836 script, downscript, vhostfdname,
837 vnet_hdr, fd, &err);
838 if (err) {
839 error_report_err(err);
840 close(fd);
841 return -1;
842 }
843 } else {
844 if (tap->has_vhostfds) {
845 error_report("vhostfds= is invalid if fds= wasn't specified");
846 return -1;
847 }
848 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
849 downscript = tap->has_downscript ? tap->downscript :
850 DEFAULT_NETWORK_DOWN_SCRIPT;
851
852 if (tap->has_ifname) {
853 pstrcpy(ifname, sizeof ifname, tap->ifname);
854 } else {
855 ifname[0] = '\0';
856 }
857
858 for (i = 0; i < queues; i++) {
859 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
860 ifname, sizeof ifname, queues > 1, &err);
861 if (fd == -1) {
862 error_report_err(err);
863 return -1;
864 }
865
866 if (queues > 1 && i == 0 && !tap->has_ifname) {
867 if (tap_fd_get_ifname(fd, ifname)) {
868 error_report("Fail to get ifname");
869 close(fd);
870 return -1;
871 }
872 }
873
874 net_init_tap_one(tap, peer, "tap", name, ifname,
875 i >= 1 ? "no" : script,
876 i >= 1 ? "no" : downscript,
877 vhostfdname, vnet_hdr, fd, &err);
878 if (err) {
879 error_report_err(err);
880 close(fd);
881 return -1;
882 }
883 }
884 }
885
886 return 0;
887 }
888
889 VHostNetState *tap_get_vhost_net(NetClientState *nc)
890 {
891 TAPState *s = DO_UPCAST(TAPState, nc, nc);
892 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
893 return s->vhost_net;
894 }
895
896 int tap_enable(NetClientState *nc)
897 {
898 TAPState *s = DO_UPCAST(TAPState, nc, nc);
899 int ret;
900
901 if (s->enabled) {
902 return 0;
903 } else {
904 ret = tap_fd_enable(s->fd);
905 if (ret == 0) {
906 s->enabled = true;
907 tap_update_fd_handler(s);
908 }
909 return ret;
910 }
911 }
912
913 int tap_disable(NetClientState *nc)
914 {
915 TAPState *s = DO_UPCAST(TAPState, nc, nc);
916 int ret;
917
918 if (s->enabled == 0) {
919 return 0;
920 } else {
921 ret = tap_fd_disable(s->fd);
922 if (ret == 0) {
923 qemu_purge_queued_packets(nc);
924 s->enabled = false;
925 tap_update_fd_handler(s);
926 }
927 return ret;
928 }
929 }