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