]> git.proxmox.com Git - mirror_qemu.git/blame - net/tap.c
net: consolidate NetClientState header files into one
[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
a245fc18 26#include "tap.h"
5281d757
MM
27
28#include "config-host.h"
29
5281d757
MM
30#include <sys/ioctl.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
71f4effc 33#include <sys/socket.h>
5281d757
MM
34#include <net/if.h>
35
36#include "net.h"
a245fc18 37#include "clients.h"
37003adf 38#include "monitor.h"
5281d757
MM
39#include "sysemu.h"
40#include "qemu-char.h"
41#include "qemu-common.h"
2f792016 42#include "qemu-error.h"
5281d757 43
5281d757 44#include "net/tap-linux.h"
5281d757 45
82b0d80e
MT
46#include "hw/vhost_net.h"
47
5281d757
MM
48/* Maximum GSO packet size (64k) plus plenty of room for
49 * the ethernet and virtio_net headers
50 */
51#define TAP_BUFSIZE (4096 + 65536)
52
53typedef struct TAPState {
4e68f7a0 54 NetClientState nc;
5281d757
MM
55 int fd;
56 char down_script[1024];
57 char down_script_arg[128];
58 uint8_t buf[TAP_BUFSIZE];
59 unsigned int read_poll : 1;
60 unsigned int write_poll : 1;
5281d757
MM
61 unsigned int using_vnet_hdr : 1;
62 unsigned int has_ufo: 1;
82b0d80e 63 VHostNetState *vhost_net;
ef4252b1 64 unsigned host_vnet_hdr_len;
5281d757
MM
65} TAPState;
66
67static int launch_script(const char *setup_script, const char *ifname, int fd);
68
69static int tap_can_send(void *opaque);
70static void tap_send(void *opaque);
71static void tap_writable(void *opaque);
72
73static void tap_update_fd_handler(TAPState *s)
74{
75 qemu_set_fd_handler2(s->fd,
76 s->read_poll ? tap_can_send : NULL,
77 s->read_poll ? tap_send : NULL,
78 s->write_poll ? tap_writable : NULL,
79 s);
80}
81
82static void tap_read_poll(TAPState *s, int enable)
83{
84 s->read_poll = !!enable;
85 tap_update_fd_handler(s);
86}
87
88static void tap_write_poll(TAPState *s, int enable)
89{
90 s->write_poll = !!enable;
91 tap_update_fd_handler(s);
92}
93
94static void tap_writable(void *opaque)
95{
96 TAPState *s = opaque;
97
98 tap_write_poll(s, 0);
99
3e35ba93 100 qemu_flush_queued_packets(&s->nc);
5281d757
MM
101}
102
103static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
104{
105 ssize_t len;
106
107 do {
108 len = writev(s->fd, iov, iovcnt);
109 } while (len == -1 && errno == EINTR);
110
111 if (len == -1 && errno == EAGAIN) {
112 tap_write_poll(s, 1);
113 return 0;
114 }
115
116 return len;
117}
118
4e68f7a0 119static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
5281d757
MM
120 int iovcnt)
121{
3e35ba93 122 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
123 const struct iovec *iovp = iov;
124 struct iovec iov_copy[iovcnt + 1];
ef4252b1 125 struct virtio_net_hdr_mrg_rxbuf hdr = { };
5281d757 126
ef4252b1 127 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
5281d757 128 iov_copy[0].iov_base = &hdr;
ef4252b1 129 iov_copy[0].iov_len = s->host_vnet_hdr_len;
5281d757
MM
130 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
131 iovp = iov_copy;
132 iovcnt++;
133 }
134
135 return tap_write_packet(s, iovp, iovcnt);
136}
137
4e68f7a0 138static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
5281d757 139{
3e35ba93 140 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
141 struct iovec iov[2];
142 int iovcnt = 0;
ef4252b1 143 struct virtio_net_hdr_mrg_rxbuf hdr = { };
5281d757 144
ef4252b1 145 if (s->host_vnet_hdr_len) {
5281d757 146 iov[iovcnt].iov_base = &hdr;
ef4252b1 147 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
5281d757
MM
148 iovcnt++;
149 }
150
151 iov[iovcnt].iov_base = (char *)buf;
152 iov[iovcnt].iov_len = size;
153 iovcnt++;
154
155 return tap_write_packet(s, iov, iovcnt);
156}
157
4e68f7a0 158static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
5281d757 159{
3e35ba93 160 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
161 struct iovec iov[1];
162
ef4252b1 163 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
3e35ba93 164 return tap_receive_raw(nc, buf, size);
5281d757
MM
165 }
166
167 iov[0].iov_base = (char *)buf;
168 iov[0].iov_len = size;
169
170 return tap_write_packet(s, iov, 1);
171}
172
173static int tap_can_send(void *opaque)
174{
175 TAPState *s = opaque;
176
3e35ba93 177 return qemu_can_send_packet(&s->nc);
5281d757
MM
178}
179
966ea5ec
MM
180#ifndef __sun__
181ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
5281d757
MM
182{
183 return read(tapfd, buf, maxlen);
184}
185#endif
186
4e68f7a0 187static void tap_send_completed(NetClientState *nc, ssize_t len)
5281d757 188{
3e35ba93 189 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
190 tap_read_poll(s, 1);
191}
192
193static void tap_send(void *opaque)
194{
195 TAPState *s = opaque;
196 int size;
197
5819c918
MM
198 do {
199 uint8_t *buf = s->buf;
200
201 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
202 if (size <= 0) {
203 break;
204 }
205
ef4252b1
MT
206 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
207 buf += s->host_vnet_hdr_len;
208 size -= s->host_vnet_hdr_len;
5819c918
MM
209 }
210
3e35ba93 211 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
5819c918
MM
212 if (size == 0) {
213 tap_read_poll(s, 0);
214 }
3e35ba93 215 } while (size > 0 && qemu_can_send_packet(&s->nc));
5281d757
MM
216}
217
4e68f7a0 218int tap_has_ufo(NetClientState *nc)
5281d757 219{
3e35ba93 220 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 221
2be64a68 222 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
5281d757
MM
223
224 return s->has_ufo;
225}
226
4e68f7a0 227int tap_has_vnet_hdr(NetClientState *nc)
5281d757 228{
3e35ba93 229 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 230
2be64a68 231 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
5281d757 232
ef4252b1 233 return !!s->host_vnet_hdr_len;
5281d757
MM
234}
235
4e68f7a0 236int tap_has_vnet_hdr_len(NetClientState *nc, int len)
445d892f
MT
237{
238 TAPState *s = DO_UPCAST(TAPState, nc, nc);
239
2be64a68 240 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
445d892f
MT
241
242 return tap_probe_vnet_hdr_len(s->fd, len);
243}
244
4e68f7a0 245void tap_set_vnet_hdr_len(NetClientState *nc, int len)
445d892f
MT
246{
247 TAPState *s = DO_UPCAST(TAPState, nc, nc);
248
2be64a68 249 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
445d892f
MT
250 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
251 len == sizeof(struct virtio_net_hdr));
252
253 tap_fd_set_vnet_hdr_len(s->fd, len);
254 s->host_vnet_hdr_len = len;
255}
256
4e68f7a0 257void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr)
5281d757 258{
3e35ba93 259 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
260
261 using_vnet_hdr = using_vnet_hdr != 0;
262
2be64a68 263 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
ef4252b1 264 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
5281d757
MM
265
266 s->using_vnet_hdr = using_vnet_hdr;
267}
268
4e68f7a0 269void tap_set_offload(NetClientState *nc, int csum, int tso4,
5281d757
MM
270 int tso6, int ecn, int ufo)
271{
3e35ba93 272 TAPState *s = DO_UPCAST(TAPState, nc, nc);
27a6375d
MT
273 if (s->fd < 0) {
274 return;
275 }
5281d757 276
27a6375d 277 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
5281d757
MM
278}
279
4e68f7a0 280static void tap_cleanup(NetClientState *nc)
5281d757 281{
3e35ba93 282 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 283
82b0d80e
MT
284 if (s->vhost_net) {
285 vhost_net_cleanup(s->vhost_net);
43849424 286 s->vhost_net = NULL;
82b0d80e
MT
287 }
288
3e35ba93 289 qemu_purge_queued_packets(nc);
5281d757
MM
290
291 if (s->down_script[0])
292 launch_script(s->down_script, s->down_script_arg, s->fd);
293
294 tap_read_poll(s, 0);
295 tap_write_poll(s, 0);
296 close(s->fd);
27a6375d 297 s->fd = -1;
5281d757
MM
298}
299
4e68f7a0 300static void tap_poll(NetClientState *nc, bool enable)
ceb69615
MT
301{
302 TAPState *s = DO_UPCAST(TAPState, nc, nc);
303 tap_read_poll(s, enable);
304 tap_write_poll(s, enable);
305}
306
4e68f7a0 307int tap_get_fd(NetClientState *nc)
95d528a2
MT
308{
309 TAPState *s = DO_UPCAST(TAPState, nc, nc);
2be64a68 310 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
95d528a2
MT
311 return s->fd;
312}
313
5281d757
MM
314/* fd support */
315
3e35ba93 316static NetClientInfo net_tap_info = {
2be64a68 317 .type = NET_CLIENT_OPTIONS_KIND_TAP,
3e35ba93
MM
318 .size = sizeof(TAPState),
319 .receive = tap_receive,
320 .receive_raw = tap_receive_raw,
321 .receive_iov = tap_receive_iov,
ceb69615 322 .poll = tap_poll,
3e35ba93
MM
323 .cleanup = tap_cleanup,
324};
325
4e68f7a0 326static TAPState *net_tap_fd_init(NetClientState *peer,
5281d757
MM
327 const char *model,
328 const char *name,
329 int fd,
330 int vnet_hdr)
331{
4e68f7a0 332 NetClientState *nc;
5281d757 333 TAPState *s;
5281d757 334
ab5f3f84 335 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
3e35ba93
MM
336
337 s = DO_UPCAST(TAPState, nc, nc);
338
5281d757 339 s->fd = fd;
ef4252b1 340 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
5281d757 341 s->using_vnet_hdr = 0;
9c282718 342 s->has_ufo = tap_probe_has_ufo(s->fd);
3e35ba93 343 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
5281d757 344 tap_read_poll(s, 1);
82b0d80e 345 s->vhost_net = NULL;
5281d757
MM
346 return s;
347}
348
5281d757
MM
349static int launch_script(const char *setup_script, const char *ifname, int fd)
350{
5281d757
MM
351 int pid, status;
352 char *args[3];
353 char **parg;
354
5281d757
MM
355 /* try to launch network script */
356 pid = fork();
357 if (pid == 0) {
358 int open_max = sysconf(_SC_OPEN_MAX), i;
359
360 for (i = 0; i < open_max; i++) {
361 if (i != STDIN_FILENO &&
362 i != STDOUT_FILENO &&
363 i != STDERR_FILENO &&
364 i != fd) {
365 close(i);
366 }
367 }
368 parg = args;
369 *parg++ = (char *)setup_script;
370 *parg++ = (char *)ifname;
9678d950 371 *parg = NULL;
5281d757
MM
372 execv(setup_script, args);
373 _exit(1);
374 } else if (pid > 0) {
375 while (waitpid(pid, &status, 0) != pid) {
376 /* loop */
377 }
5281d757
MM
378
379 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
380 return 0;
381 }
382 }
383 fprintf(stderr, "%s: could not launch network script\n", setup_script);
384 return -1;
385}
386
a7c36ee4
CB
387static int recv_fd(int c)
388{
389 int fd;
390 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
391 struct msghdr msg = {
392 .msg_control = msgbuf,
393 .msg_controllen = sizeof(msgbuf),
394 };
395 struct cmsghdr *cmsg;
396 struct iovec iov;
397 uint8_t req[1];
398 ssize_t len;
399
400 cmsg = CMSG_FIRSTHDR(&msg);
401 cmsg->cmsg_level = SOL_SOCKET;
402 cmsg->cmsg_type = SCM_RIGHTS;
403 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
404 msg.msg_controllen = cmsg->cmsg_len;
405
406 iov.iov_base = req;
407 iov.iov_len = sizeof(req);
408
409 msg.msg_iov = &iov;
410 msg.msg_iovlen = 1;
411
412 len = recvmsg(c, &msg, 0);
413 if (len > 0) {
414 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
415 return fd;
416 }
417
418 return len;
419}
420
421static int net_bridge_run_helper(const char *helper, const char *bridge)
422{
423 sigset_t oldmask, mask;
424 int pid, status;
425 char *args[5];
426 char **parg;
427 int sv[2];
428
429 sigemptyset(&mask);
430 sigaddset(&mask, SIGCHLD);
431 sigprocmask(SIG_BLOCK, &mask, &oldmask);
432
433 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
434 return -1;
435 }
436
437 /* try to launch bridge helper */
438 pid = fork();
439 if (pid == 0) {
440 int open_max = sysconf(_SC_OPEN_MAX), i;
441 char fd_buf[6+10];
442 char br_buf[6+IFNAMSIZ] = {0};
443 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
444
445 for (i = 0; i < open_max; i++) {
446 if (i != STDIN_FILENO &&
447 i != STDOUT_FILENO &&
448 i != STDERR_FILENO &&
449 i != sv[1]) {
450 close(i);
451 }
452 }
453
454 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
455
456 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
457 /* assume helper is a command */
458
459 if (strstr(helper, "--br=") == NULL) {
460 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
461 }
462
463 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
464 helper, "--use-vnet", fd_buf, br_buf);
465
466 parg = args;
467 *parg++ = (char *)"sh";
468 *parg++ = (char *)"-c";
469 *parg++ = helper_cmd;
470 *parg++ = NULL;
471
472 execv("/bin/sh", args);
473 } else {
474 /* assume helper is just the executable path name */
475
476 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
477
478 parg = args;
479 *parg++ = (char *)helper;
480 *parg++ = (char *)"--use-vnet";
481 *parg++ = fd_buf;
482 *parg++ = br_buf;
483 *parg++ = NULL;
484
485 execv(helper, args);
486 }
487 _exit(1);
488
489 } else if (pid > 0) {
490 int fd;
491
492 close(sv[1]);
493
494 do {
495 fd = recv_fd(sv[0]);
496 } while (fd == -1 && errno == EINTR);
497
498 close(sv[0]);
499
500 while (waitpid(pid, &status, 0) != pid) {
501 /* loop */
502 }
503 sigprocmask(SIG_SETMASK, &oldmask, NULL);
504 if (fd < 0) {
505 fprintf(stderr, "failed to recv file descriptor\n");
506 return -1;
507 }
508
509 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
510 return fd;
511 }
512 }
513 fprintf(stderr, "failed to launch bridge helper\n");
514 return -1;
515}
516
1a0c0958 517int net_init_bridge(const NetClientOptions *opts, const char *name,
4e68f7a0 518 NetClientState *peer)
a7c36ee4 519{
f79b51b0
LE
520 const NetdevBridgeOptions *bridge;
521 const char *helper, *br;
522
a7c36ee4
CB
523 TAPState *s;
524 int fd, vnet_hdr;
525
f79b51b0
LE
526 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
527 bridge = opts->bridge;
528
529 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
530 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
a7c36ee4 531
f79b51b0 532 fd = net_bridge_run_helper(helper, br);
a7c36ee4
CB
533 if (fd == -1) {
534 return -1;
535 }
536
537 fcntl(fd, F_SETFL, O_NONBLOCK);
538
539 vnet_hdr = tap_probe_vnet_hdr(fd);
540
d33d93b2 541 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
a7c36ee4
CB
542 if (!s) {
543 close(fd);
544 return -1;
545 }
546
f79b51b0
LE
547 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
548 br);
a7c36ee4
CB
549
550 return 0;
551}
552
08c573a8
LE
553static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
554 const char *setup_script, char *ifname,
555 size_t ifname_sz)
5281d757
MM
556{
557 int fd, vnet_hdr_required;
5281d757 558
08c573a8
LE
559 if (tap->has_ifname) {
560 pstrcpy(ifname, ifname_sz, tap->ifname);
561 } else {
562 assert(ifname_sz > 0);
563 ifname[0] = '\0';
5281d757
MM
564 }
565
08c573a8
LE
566 if (tap->has_vnet_hdr) {
567 *vnet_hdr = tap->vnet_hdr;
5281d757
MM
568 vnet_hdr_required = *vnet_hdr;
569 } else {
08c573a8 570 *vnet_hdr = 1;
5281d757
MM
571 vnet_hdr_required = 0;
572 }
573
08c573a8 574 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
5281d757
MM
575 if (fd < 0) {
576 return -1;
577 }
578
5281d757
MM
579 if (setup_script &&
580 setup_script[0] != '\0' &&
581 strcmp(setup_script, "no") != 0 &&
582 launch_script(setup_script, ifname, fd)) {
583 close(fd);
584 return -1;
585 }
586
5281d757
MM
587 return fd;
588}
589
1a0c0958 590int net_init_tap(const NetClientOptions *opts, const char *name,
4e68f7a0 591 NetClientState *peer)
5281d757 592{
08c573a8
LE
593 const NetdevTapOptions *tap;
594
df6c2a0f 595 int fd, vnet_hdr = 0;
a7c36ee4 596 const char *model;
08c573a8
LE
597 TAPState *s;
598
599 /* for the no-fd, no-helper case */
600 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
601 char ifname[128];
602
603 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
604 tap = opts->tap;
5281d757 605
08c573a8
LE
606 if (tap->has_fd) {
607 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
608 tap->has_vnet_hdr || tap->has_helper) {
a7c36ee4
CB
609 error_report("ifname=, script=, downscript=, vnet_hdr=, "
610 "and helper= are invalid with fd=");
5281d757
MM
611 return -1;
612 }
613
a96ed02f 614 fd = monitor_handle_fd_param(cur_mon, tap->fd);
5281d757
MM
615 if (fd == -1) {
616 return -1;
617 }
618
619 fcntl(fd, F_SETFL, O_NONBLOCK);
620
621 vnet_hdr = tap_probe_vnet_hdr(fd);
a7c36ee4
CB
622
623 model = "tap";
624
08c573a8
LE
625 } else if (tap->has_helper) {
626 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
627 tap->has_vnet_hdr) {
a7c36ee4
CB
628 error_report("ifname=, script=, downscript=, and vnet_hdr= "
629 "are invalid with helper=");
630 return -1;
631 }
632
08c573a8 633 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
a7c36ee4
CB
634 if (fd == -1) {
635 return -1;
636 }
637
638 fcntl(fd, F_SETFL, O_NONBLOCK);
639
640 vnet_hdr = tap_probe_vnet_hdr(fd);
641
642 model = "bridge";
643
5281d757 644 } else {
08c573a8
LE
645 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
646 fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
929fe497
JL
647 if (fd == -1) {
648 return -1;
649 }
a7c36ee4
CB
650
651 model = "tap";
5281d757
MM
652 }
653
d33d93b2 654 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
5281d757
MM
655 if (!s) {
656 close(fd);
657 return -1;
658 }
659
08c573a8 660 if (tap_set_sndbuf(s->fd, tap) < 0) {
5281d757
MM
661 return -1;
662 }
663
08c573a8 664 if (tap->has_fd) {
3e35ba93 665 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
08c573a8
LE
666 } else if (tap->has_helper) {
667 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
668 tap->helper);
5281d757 669 } else {
08c573a8 670 const char *downscript;
5281d757 671
08c573a8
LE
672 downscript = tap->has_downscript ? tap->downscript :
673 DEFAULT_NETWORK_DOWN_SCRIPT;
5281d757 674
3e35ba93 675 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
08c573a8
LE
676 "ifname=%s,script=%s,downscript=%s", ifname, script,
677 downscript);
5281d757
MM
678
679 if (strcmp(downscript, "no") != 0) {
680 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
681 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
682 }
683 }
684
08c573a8
LE
685 if (tap->has_vhost ? tap->vhost :
686 tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) {
687 int vhostfd;
688
689 if (tap->has_vhostfd) {
a96ed02f 690 vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd);
08c573a8 691 if (vhostfd == -1) {
82b0d80e
MT
692 return -1;
693 }
82b0d80e
MT
694 } else {
695 vhostfd = -1;
696 }
08c573a8
LE
697
698 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
699 tap->has_vhostforce && tap->vhostforce);
82b0d80e
MT
700 if (!s->vhost_net) {
701 error_report("vhost-net requested but could not be initialized");
702 return -1;
703 }
08c573a8 704 } else if (tap->has_vhostfd) {
82b0d80e
MT
705 error_report("vhostfd= is not valid without vhost");
706 return -1;
707 }
708
5281d757
MM
709 return 0;
710}
b202554c 711
4e68f7a0 712VHostNetState *tap_get_vhost_net(NetClientState *nc)
b202554c
MT
713{
714 TAPState *s = DO_UPCAST(TAPState, nc, nc);
2be64a68 715 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
b202554c
MT
716 return s->vhost_net;
717}