tap.c 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * QEMU System Emulator
 *
 * Copyright (c) 2003-2008 Fabrice Bellard
 * Copyright (c) 2009 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

26
#include "tap.h"
27 28 29 30 31 32

#include "config-host.h"

#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
A
Alexander Graf 已提交
33
#include <sys/socket.h>
34 35 36
#include <net/if.h>

#include "net.h"
37
#include "clients.h"
38
#include "monitor.h"
39 40 41
#include "sysemu.h"
#include "qemu-char.h"
#include "qemu-common.h"
42
#include "qemu-error.h"
43 44 45

#include "net/tap-linux.h"

46 47
#include "hw/vhost_net.h"

48 49 50 51 52 53
/* Maximum GSO packet size (64k) plus plenty of room for
 * the ethernet and virtio_net headers
 */
#define TAP_BUFSIZE (4096 + 65536)

typedef struct TAPState {
54
    NetClientState nc;
55 56 57 58 59 60 61 62
    int fd;
    char down_script[1024];
    char down_script_arg[128];
    uint8_t buf[TAP_BUFSIZE];
    unsigned int read_poll : 1;
    unsigned int write_poll : 1;
    unsigned int using_vnet_hdr : 1;
    unsigned int has_ufo: 1;
63
    VHostNetState *vhost_net;
64
    unsigned host_vnet_hdr_len;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
} TAPState;

static int launch_script(const char *setup_script, const char *ifname, int fd);

static int tap_can_send(void *opaque);
static void tap_send(void *opaque);
static void tap_writable(void *opaque);

static void tap_update_fd_handler(TAPState *s)
{
    qemu_set_fd_handler2(s->fd,
                         s->read_poll  ? tap_can_send : NULL,
                         s->read_poll  ? tap_send     : NULL,
                         s->write_poll ? tap_writable : NULL,
                         s);
}

static void tap_read_poll(TAPState *s, int enable)
{
    s->read_poll = !!enable;
    tap_update_fd_handler(s);
}

static void tap_write_poll(TAPState *s, int enable)
{
    s->write_poll = !!enable;
    tap_update_fd_handler(s);
}

static void tap_writable(void *opaque)
{
    TAPState *s = opaque;

    tap_write_poll(s, 0);

100
    qemu_flush_queued_packets(&s->nc);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
{
    ssize_t len;

    do {
        len = writev(s->fd, iov, iovcnt);
    } while (len == -1 && errno == EINTR);

    if (len == -1 && errno == EAGAIN) {
        tap_write_poll(s, 1);
        return 0;
    }

    return len;
}

119
static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
120 121
                               int iovcnt)
{
122
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
123 124
    const struct iovec *iovp = iov;
    struct iovec iov_copy[iovcnt + 1];
125
    struct virtio_net_hdr_mrg_rxbuf hdr = { };
126

127
    if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
128
        iov_copy[0].iov_base = &hdr;
129
        iov_copy[0].iov_len =  s->host_vnet_hdr_len;
130 131 132 133 134 135 136 137
        memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
        iovp = iov_copy;
        iovcnt++;
    }

    return tap_write_packet(s, iovp, iovcnt);
}

138
static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
139
{
140
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
141 142
    struct iovec iov[2];
    int iovcnt = 0;
143
    struct virtio_net_hdr_mrg_rxbuf hdr = { };
144

145
    if (s->host_vnet_hdr_len) {
146
        iov[iovcnt].iov_base = &hdr;
147
        iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
148 149 150 151 152 153 154 155 156 157
        iovcnt++;
    }

    iov[iovcnt].iov_base = (char *)buf;
    iov[iovcnt].iov_len  = size;
    iovcnt++;

    return tap_write_packet(s, iov, iovcnt);
}

158
static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
159
{
160
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
161 162
    struct iovec iov[1];

163
    if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
164
        return tap_receive_raw(nc, buf, size);
165 166 167 168 169 170 171 172 173 174 175 176
    }

    iov[0].iov_base = (char *)buf;
    iov[0].iov_len  = size;

    return tap_write_packet(s, iov, 1);
}

static int tap_can_send(void *opaque)
{
    TAPState *s = opaque;

177
    return qemu_can_send_packet(&s->nc);
178 179
}

180 181
#ifndef __sun__
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
182 183 184 185 186
{
    return read(tapfd, buf, maxlen);
}
#endif

187
static void tap_send_completed(NetClientState *nc, ssize_t len)
188
{
189
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
190 191 192 193 194 195 196 197
    tap_read_poll(s, 1);
}

static void tap_send(void *opaque)
{
    TAPState *s = opaque;
    int size;

M
Mark McLoughlin 已提交
198 199 200 201 202 203 204 205
    do {
        uint8_t *buf = s->buf;

        size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
        if (size <= 0) {
            break;
        }

206 207 208
        if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
            buf  += s->host_vnet_hdr_len;
            size -= s->host_vnet_hdr_len;
M
Mark McLoughlin 已提交
209 210
        }

211
        size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
M
Mark McLoughlin 已提交
212 213 214
        if (size == 0) {
            tap_read_poll(s, 0);
        }
215
    } while (size > 0 && qemu_can_send_packet(&s->nc));
216 217
}

218
int tap_has_ufo(NetClientState *nc)
219
{
220
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
221

222
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
223 224 225 226

    return s->has_ufo;
}

227
int tap_has_vnet_hdr(NetClientState *nc)
228
{
229
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
230

231
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
232

233
    return !!s->host_vnet_hdr_len;
234 235
}

236
int tap_has_vnet_hdr_len(NetClientState *nc, int len)
237 238 239
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);

240
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
241 242 243 244

    return tap_probe_vnet_hdr_len(s->fd, len);
}

245
void tap_set_vnet_hdr_len(NetClientState *nc, int len)
246 247 248
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);

249
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
250 251 252 253 254 255 256
    assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
           len == sizeof(struct virtio_net_hdr));

    tap_fd_set_vnet_hdr_len(s->fd, len);
    s->host_vnet_hdr_len = len;
}

257
void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr)
258
{
259
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
260 261 262

    using_vnet_hdr = using_vnet_hdr != 0;

263
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
264
    assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
265 266 267 268

    s->using_vnet_hdr = using_vnet_hdr;
}

269
void tap_set_offload(NetClientState *nc, int csum, int tso4,
270 271
                     int tso6, int ecn, int ufo)
{
272
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
273 274 275
    if (s->fd < 0) {
        return;
    }
276

277
    tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
278 279
}

280
static void tap_cleanup(NetClientState *nc)
281
{
282
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
283

284 285
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
286
        s->vhost_net = NULL;
287 288
    }

289
    qemu_purge_queued_packets(nc);
290 291 292 293 294 295 296

    if (s->down_script[0])
        launch_script(s->down_script, s->down_script_arg, s->fd);

    tap_read_poll(s, 0);
    tap_write_poll(s, 0);
    close(s->fd);
297
    s->fd = -1;
298 299
}

300
static void tap_poll(NetClientState *nc, bool enable)
301 302 303 304 305 306
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
    tap_read_poll(s, enable);
    tap_write_poll(s, enable);
}

307
int tap_get_fd(NetClientState *nc)
308 309
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
310
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
311 312 313
    return s->fd;
}

314 315
/* fd support */

316
static NetClientInfo net_tap_info = {
317
    .type = NET_CLIENT_OPTIONS_KIND_TAP,
318 319 320 321
    .size = sizeof(TAPState),
    .receive = tap_receive,
    .receive_raw = tap_receive_raw,
    .receive_iov = tap_receive_iov,
322
    .poll = tap_poll,
323 324 325
    .cleanup = tap_cleanup,
};

326
static TAPState *net_tap_fd_init(NetClientState *peer,
327 328 329 330 331
                                 const char *model,
                                 const char *name,
                                 int fd,
                                 int vnet_hdr)
{
332
    NetClientState *nc;
333 334
    TAPState *s;

335
    nc = qemu_new_net_client(&net_tap_info, peer, model, name);
336 337 338

    s = DO_UPCAST(TAPState, nc, nc);

339
    s->fd = fd;
340
    s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
341
    s->using_vnet_hdr = 0;
342
    s->has_ufo = tap_probe_has_ufo(s->fd);
343
    tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
344
    tap_read_poll(s, 1);
345
    s->vhost_net = NULL;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    return s;
}

static int launch_script(const char *setup_script, const char *ifname, int fd)
{
    int pid, status;
    char *args[3];
    char **parg;

    /* try to launch network script */
    pid = fork();
    if (pid == 0) {
        int open_max = sysconf(_SC_OPEN_MAX), i;

        for (i = 0; i < open_max; i++) {
            if (i != STDIN_FILENO &&
                i != STDOUT_FILENO &&
                i != STDERR_FILENO &&
                i != fd) {
                close(i);
            }
        }
        parg = args;
        *parg++ = (char *)setup_script;
        *parg++ = (char *)ifname;
371
        *parg = NULL;
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        execv(setup_script, args);
        _exit(1);
    } else if (pid > 0) {
        while (waitpid(pid, &status, 0) != pid) {
            /* loop */
        }

        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
            return 0;
        }
    }
    fprintf(stderr, "%s: could not launch network script\n", setup_script);
    return -1;
}

C
Corey Bryant 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
static int recv_fd(int c)
{
    int fd;
    uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
    struct msghdr msg = {
        .msg_control = msgbuf,
        .msg_controllen = sizeof(msgbuf),
    };
    struct cmsghdr *cmsg;
    struct iovec iov;
    uint8_t req[1];
    ssize_t len;

    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
    msg.msg_controllen = cmsg->cmsg_len;

    iov.iov_base = req;
    iov.iov_len = sizeof(req);

    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

    len = recvmsg(c, &msg, 0);
    if (len > 0) {
        memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
        return fd;
    }

    return len;
}

static int net_bridge_run_helper(const char *helper, const char *bridge)
{
    sigset_t oldmask, mask;
    int pid, status;
    char *args[5];
    char **parg;
    int sv[2];

    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    sigprocmask(SIG_BLOCK, &mask, &oldmask);

    if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
        return -1;
    }

    /* try to launch bridge helper */
    pid = fork();
    if (pid == 0) {
        int open_max = sysconf(_SC_OPEN_MAX), i;
        char fd_buf[6+10];
        char br_buf[6+IFNAMSIZ] = {0};
        char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];

        for (i = 0; i < open_max; i++) {
            if (i != STDIN_FILENO &&
                i != STDOUT_FILENO &&
                i != STDERR_FILENO &&
                i != sv[1]) {
                close(i);
            }
        }

        snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);

        if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
            /* assume helper is a command */

            if (strstr(helper, "--br=") == NULL) {
                snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
            }

            snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
                     helper, "--use-vnet", fd_buf, br_buf);

            parg = args;
            *parg++ = (char *)"sh";
            *parg++ = (char *)"-c";
            *parg++ = helper_cmd;
            *parg++ = NULL;

            execv("/bin/sh", args);
        } else {
            /* assume helper is just the executable path name */

            snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);

            parg = args;
            *parg++ = (char *)helper;
            *parg++ = (char *)"--use-vnet";
            *parg++ = fd_buf;
            *parg++ = br_buf;
            *parg++ = NULL;

            execv(helper, args);
        }
        _exit(1);

    } else if (pid > 0) {
        int fd;

        close(sv[1]);

        do {
            fd = recv_fd(sv[0]);
        } while (fd == -1 && errno == EINTR);

        close(sv[0]);

        while (waitpid(pid, &status, 0) != pid) {
            /* loop */
        }
        sigprocmask(SIG_SETMASK, &oldmask, NULL);
        if (fd < 0) {
            fprintf(stderr, "failed to recv file descriptor\n");
            return -1;
        }

        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
            return fd;
        }
    }
    fprintf(stderr, "failed to launch bridge helper\n");
    return -1;
}

517
int net_init_bridge(const NetClientOptions *opts, const char *name,
518
                    NetClientState *peer)
C
Corey Bryant 已提交
519
{
520 521 522
    const NetdevBridgeOptions *bridge;
    const char *helper, *br;

C
Corey Bryant 已提交
523 524 525
    TAPState *s;
    int fd, vnet_hdr;

526 527 528 529 530
    assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
    bridge = opts->bridge;

    helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
    br     = bridge->has_br     ? bridge->br     : DEFAULT_BRIDGE_INTERFACE;
C
Corey Bryant 已提交
531

532
    fd = net_bridge_run_helper(helper, br);
C
Corey Bryant 已提交
533 534 535 536 537 538 539 540
    if (fd == -1) {
        return -1;
    }

    fcntl(fd, F_SETFL, O_NONBLOCK);

    vnet_hdr = tap_probe_vnet_hdr(fd);

541
    s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
C
Corey Bryant 已提交
542 543 544 545 546
    if (!s) {
        close(fd);
        return -1;
    }

547 548
    snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
             br);
C
Corey Bryant 已提交
549 550 551 552

    return 0;
}

553 554 555
static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
                        const char *setup_script, char *ifname,
                        size_t ifname_sz)
556 557 558
{
    int fd, vnet_hdr_required;

559 560 561 562 563
    if (tap->has_ifname) {
        pstrcpy(ifname, ifname_sz, tap->ifname);
    } else {
        assert(ifname_sz > 0);
        ifname[0] = '\0';
564 565
    }

566 567
    if (tap->has_vnet_hdr) {
        *vnet_hdr = tap->vnet_hdr;
568 569
        vnet_hdr_required = *vnet_hdr;
    } else {
570
        *vnet_hdr = 1;
571 572 573
        vnet_hdr_required = 0;
    }

574
    TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    if (fd < 0) {
        return -1;
    }

    if (setup_script &&
        setup_script[0] != '\0' &&
        strcmp(setup_script, "no") != 0 &&
        launch_script(setup_script, ifname, fd)) {
        close(fd);
        return -1;
    }

    return fd;
}

590
int net_init_tap(const NetClientOptions *opts, const char *name,
591
                 NetClientState *peer)
592
{
593 594
    const NetdevTapOptions *tap;

595
    int fd, vnet_hdr = 0;
C
Corey Bryant 已提交
596
    const char *model;
597 598 599 600 601 602 603 604
    TAPState *s;

    /* for the no-fd, no-helper case */
    const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
    char ifname[128];

    assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
    tap = opts->tap;
605

606 607 608
    if (tap->has_fd) {
        if (tap->has_ifname || tap->has_script || tap->has_downscript ||
            tap->has_vnet_hdr || tap->has_helper) {
C
Corey Bryant 已提交
609 610
            error_report("ifname=, script=, downscript=, vnet_hdr=, "
                         "and helper= are invalid with fd=");
611 612 613
            return -1;
        }

614
        fd = monitor_handle_fd_param(cur_mon, tap->fd);
615 616 617 618 619 620 621
        if (fd == -1) {
            return -1;
        }

        fcntl(fd, F_SETFL, O_NONBLOCK);

        vnet_hdr = tap_probe_vnet_hdr(fd);
C
Corey Bryant 已提交
622 623 624

        model = "tap";

625 626 627
    } else if (tap->has_helper) {
        if (tap->has_ifname || tap->has_script || tap->has_downscript ||
            tap->has_vnet_hdr) {
C
Corey Bryant 已提交
628 629 630 631 632
            error_report("ifname=, script=, downscript=, and vnet_hdr= "
                         "are invalid with helper=");
            return -1;
        }

633
        fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
C
Corey Bryant 已提交
634 635 636 637 638 639 640 641 642 643
        if (fd == -1) {
            return -1;
        }

        fcntl(fd, F_SETFL, O_NONBLOCK);

        vnet_hdr = tap_probe_vnet_hdr(fd);

        model = "bridge";

644
    } else {
645 646
        script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
        fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
647 648 649
        if (fd == -1) {
            return -1;
        }
C
Corey Bryant 已提交
650 651

        model = "tap";
652 653
    }

654
    s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
655 656 657 658 659
    if (!s) {
        close(fd);
        return -1;
    }

660
    if (tap_set_sndbuf(s->fd, tap) < 0) {
661 662 663
        return -1;
    }

664
    if (tap->has_fd) {
665
        snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
666 667 668
    } else if (tap->has_helper) {
        snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
                 tap->helper);
669
    } else {
670
        const char *downscript;
671

672 673
        downscript = tap->has_downscript ? tap->downscript :
                                           DEFAULT_NETWORK_DOWN_SCRIPT;
674

675
        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
676 677
                 "ifname=%s,script=%s,downscript=%s", ifname, script,
                 downscript);
678 679 680 681 682 683 684

        if (strcmp(downscript, "no") != 0) {
            snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
            snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
        }
    }

685 686 687 688 689
    if (tap->has_vhost ? tap->vhost :
        tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) {
        int vhostfd;

        if (tap->has_vhostfd) {
690
            vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd);
691
            if (vhostfd == -1) {
692 693 694 695 696
                return -1;
            }
        } else {
            vhostfd = -1;
        }
697 698 699

        s->vhost_net = vhost_net_init(&s->nc, vhostfd,
                                      tap->has_vhostforce && tap->vhostforce);
700 701 702 703
        if (!s->vhost_net) {
            error_report("vhost-net requested but could not be initialized");
            return -1;
        }
704
    } else if (tap->has_vhostfd) {
705 706 707 708
        error_report("vhostfd= is not valid without vhost");
        return -1;
    }

709 710
    return 0;
}
711

712
VHostNetState *tap_get_vhost_net(NetClientState *nc)
713 714
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
715
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
716 717
    return s->vhost_net;
}