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