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