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