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