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