tap.c 23.2 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.
 */

P
Paolo Bonzini 已提交
26
#include "tap_int.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
#include <net/if.h>

P
Paolo Bonzini 已提交
36
#include "net/net.h"
37
#include "clients.h"
38
#include "monitor/monitor.h"
39
#include "sysemu/sysemu.h"
40
#include "qemu-common.h"
41
#include "qemu/error-report.h"
42

P
Paolo Bonzini 已提交
43
#include "net/tap.h"
44

P
Paolo Bonzini 已提交
45
#include "net/vhost_net.h"
46

47
typedef struct TAPState {
48
    NetClientState nc;
49 50 51
    int fd;
    char down_script[1024];
    char down_script_arg[128];
52
    uint8_t buf[NET_BUFSIZE];
53 54 55 56
    bool read_poll;
    bool write_poll;
    bool using_vnet_hdr;
    bool has_ufo;
57
    bool enabled;
58
    VHostNetState *vhost_net;
59
    unsigned host_vnet_hdr_len;
60 61 62 63 64 65 66 67 68 69 70
} 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,
71 72 73
                         s->read_poll && s->enabled ? tap_can_send : NULL,
                         s->read_poll && s->enabled ? tap_send     : NULL,
                         s->write_poll && s->enabled ? tap_writable : NULL,
74 75 76
                         s);
}

77
static void tap_read_poll(TAPState *s, bool enable)
78
{
79
    s->read_poll = enable;
80 81 82
    tap_update_fd_handler(s);
}

83
static void tap_write_poll(TAPState *s, bool enable)
84
{
85
    s->write_poll = enable;
86 87 88 89 90 91 92
    tap_update_fd_handler(s);
}

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

93
    tap_write_poll(s, false);
94

95
    qemu_flush_queued_packets(&s->nc);
96 97 98 99 100 101 102 103 104 105 106
}

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) {
107
        tap_write_poll(s, true);
108 109 110 111 112 113
        return 0;
    }

    return len;
}

114
static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
115 116
                               int iovcnt)
{
117
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
118 119
    const struct iovec *iovp = iov;
    struct iovec iov_copy[iovcnt + 1];
120
    struct virtio_net_hdr_mrg_rxbuf hdr = { };
121

122
    if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
123
        iov_copy[0].iov_base = &hdr;
124
        iov_copy[0].iov_len =  s->host_vnet_hdr_len;
125 126 127 128 129 130 131 132
        memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
        iovp = iov_copy;
        iovcnt++;
    }

    return tap_write_packet(s, iovp, iovcnt);
}

133
static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
134
{
135
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
136 137
    struct iovec iov[2];
    int iovcnt = 0;
138
    struct virtio_net_hdr_mrg_rxbuf hdr = { };
139

140
    if (s->host_vnet_hdr_len) {
141
        iov[iovcnt].iov_base = &hdr;
142
        iov[iovcnt].iov_len  = s->host_vnet_hdr_len;
143 144 145 146 147 148 149 150 151 152
        iovcnt++;
    }

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

    return tap_write_packet(s, iov, iovcnt);
}

153
static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
154
{
155
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
156 157
    struct iovec iov[1];

158
    if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
159
        return tap_receive_raw(nc, buf, size);
160 161 162 163 164 165 166 167 168 169 170 171
    }

    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;

172
    return qemu_can_send_packet(&s->nc);
173 174
}

175 176
#ifndef __sun__
ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
177 178 179 180 181
{
    return read(tapfd, buf, maxlen);
}
#endif

182
static void tap_send_completed(NetClientState *nc, ssize_t len)
183
{
184
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
185
    tap_read_poll(s, true);
186 187 188 189 190 191 192
}

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

S
Stefan Hajnoczi 已提交
193
    while (qemu_can_send_packet(&s->nc)) {
M
Mark McLoughlin 已提交
194 195 196 197 198 199 200
        uint8_t *buf = s->buf;

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

201 202 203
        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 已提交
204 205
        }

206
        size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
M
Mark McLoughlin 已提交
207
        if (size == 0) {
208
            tap_read_poll(s, false);
S
Stefan Hajnoczi 已提交
209 210 211
            break;
        } else if (size < 0) {
            break;
M
Mark McLoughlin 已提交
212
        }
S
Stefan Hajnoczi 已提交
213
    }
214 215
}

216
static bool tap_has_ufo(NetClientState *nc)
217
{
218
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
219

220
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
221 222 223 224

    return s->has_ufo;
}

225
static bool tap_has_vnet_hdr(NetClientState *nc)
226
{
227
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
228

229
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
230

231
    return !!s->host_vnet_hdr_len;
232 233
}

234
static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
235 236 237
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);

238
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
239

240
    return !!tap_probe_vnet_hdr_len(s->fd, len);
241 242
}

243
static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
244 245 246
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);

247
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
248 249 250 251 252 253 254
    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;
}

255
static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
256
{
257
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
258

259
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
260
    assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
261 262 263 264

    s->using_vnet_hdr = using_vnet_hdr;
}

265
static void tap_set_offload(NetClientState *nc, int csum, int tso4,
266 267
                     int tso6, int ecn, int ufo)
{
268
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
269 270 271
    if (s->fd < 0) {
        return;
    }
272

273
    tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
274 275
}

276
static void tap_cleanup(NetClientState *nc)
277
{
278
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
279

280 281
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
282
        s->vhost_net = NULL;
283 284
    }

285
    qemu_purge_queued_packets(nc);
286 287 288 289

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

290 291
    tap_read_poll(s, false);
    tap_write_poll(s, false);
292
    close(s->fd);
293
    s->fd = -1;
294 295
}

296
static void tap_poll(NetClientState *nc, bool enable)
297 298 299 300 301 302
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
    tap_read_poll(s, enable);
    tap_write_poll(s, enable);
}

303
int tap_get_fd(NetClientState *nc)
304 305
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
306
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
307 308 309
    return s->fd;
}

310 311
/* fd support */

312
static NetClientInfo net_tap_info = {
313
    .type = NET_CLIENT_OPTIONS_KIND_TAP,
314 315 316 317
    .size = sizeof(TAPState),
    .receive = tap_receive,
    .receive_raw = tap_receive_raw,
    .receive_iov = tap_receive_iov,
318
    .poll = tap_poll,
319
    .cleanup = tap_cleanup,
320 321 322 323 324 325
    .has_ufo = tap_has_ufo,
    .has_vnet_hdr = tap_has_vnet_hdr,
    .has_vnet_hdr_len = tap_has_vnet_hdr_len,
    .using_vnet_hdr = tap_using_vnet_hdr,
    .set_offload = tap_set_offload,
    .set_vnet_hdr_len = tap_set_vnet_hdr_len,
326 327
};

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

337
    nc = qemu_new_net_client(&net_tap_info, peer, model, name);
338 339 340

    s = DO_UPCAST(TAPState, nc, nc);

341
    s->fd = fd;
342
    s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
343
    s->using_vnet_hdr = false;
344
    s->has_ufo = tap_probe_has_ufo(s->fd);
345
    s->enabled = true;
346
    tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
347 348 349 350 351 352 353
    /*
     * Make sure host header length is set correctly in tap:
     * it might have been modified by another instance of qemu.
     */
    if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
        tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
    }
354
    tap_read_poll(s, true);
355
    s->vhost_net = NULL;
356 357 358 359 360 361 362 363 364 365 366 367 368 369
    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;

370 371
        for (i = 3; i < open_max; i++) {
            if (i != fd) {
372 373 374 375 376 377
                close(i);
            }
        }
        parg = args;
        *parg++ = (char *)setup_script;
        *parg++ = (char *)ifname;
378
        *parg = NULL;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        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 已提交
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
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];

452 453
        for (i = 3; i < open_max; i++) {
            if (i != sv[1]) {
C
Corey Bryant 已提交
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 517 518 519 520
                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;
}

521
int net_init_bridge(const NetClientOptions *opts, const char *name,
522
                    NetClientState *peer)
C
Corey Bryant 已提交
523
{
524 525 526
    const NetdevBridgeOptions *bridge;
    const char *helper, *br;

C
Corey Bryant 已提交
527 528 529
    TAPState *s;
    int fd, vnet_hdr;

530 531 532 533 534
    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 已提交
535

536
    fd = net_bridge_run_helper(helper, br);
C
Corey Bryant 已提交
537 538 539 540 541 542 543 544
    if (fd == -1) {
        return -1;
    }

    fcntl(fd, F_SETFL, O_NONBLOCK);

    vnet_hdr = tap_probe_vnet_hdr(fd);

545
    s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
C
Corey Bryant 已提交
546 547 548 549 550
    if (!s) {
        close(fd);
        return -1;
    }

551 552
    snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
             br);
C
Corey Bryant 已提交
553 554 555 556

    return 0;
}

557 558
static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
                        const char *setup_script, char *ifname,
J
Jason Wang 已提交
559
                        size_t ifname_sz, int mq_required)
560 561 562
{
    int fd, vnet_hdr_required;

563 564
    if (tap->has_vnet_hdr) {
        *vnet_hdr = tap->vnet_hdr;
565 566
        vnet_hdr_required = *vnet_hdr;
    } else {
567
        *vnet_hdr = 1;
568 569 570
        vnet_hdr_required = 0;
    }

J
Jason Wang 已提交
571 572
    TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
                      mq_required));
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    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;
}

J
Jason Wang 已提交
588 589
#define MAX_TAP_QUEUES 1024

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
                            const char *model, const char *name,
                            const char *ifname, const char *script,
                            const char *downscript, const char *vhostfdname,
                            int vnet_hdr, int fd)
{
    TAPState *s;

    s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
    if (!s) {
        close(fd);
        return -1;
    }

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

J
Jason Wang 已提交
608
    if (tap->has_fd || tap->has_fds) {
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
        snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
    } else if (tap->has_helper) {
        snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
                 tap->helper);
    } else {
        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
                 "ifname=%s,script=%s,downscript=%s", ifname, script,
                 downscript);

        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);
        }
    }

    if (tap->has_vhost ? tap->vhost :
        vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
        int vhostfd;

J
Jason Wang 已提交
629
        if (tap->has_vhostfd || tap->has_vhostfds) {
630 631 632 633 634 635 636 637 638 639 640 641 642 643
            vhostfd = monitor_handle_fd_param(cur_mon, vhostfdname);
            if (vhostfd == -1) {
                return -1;
            }
        } else {
            vhostfd = -1;
        }

        s->vhost_net = vhost_net_init(&s->nc, vhostfd,
                                      tap->has_vhostforce && tap->vhostforce);
        if (!s->vhost_net) {
            error_report("vhost-net requested but could not be initialized");
            return -1;
        }
J
Jason Wang 已提交
644
    } else if (tap->has_vhostfd || tap->has_vhostfds) {
645 646 647 648 649 650 651
        error_report("vhostfd= is not valid without vhost");
        return -1;
    }

    return 0;
}

J
Jason Wang 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
static int get_fds(char *str, char *fds[], int max)
{
    char *ptr = str, *this;
    size_t len = strlen(str);
    int i = 0;

    while (i < max && ptr < str + len) {
        this = strchr(ptr, ':');

        if (this == NULL) {
            fds[i] = g_strdup(ptr);
        } else {
            fds[i] = g_strndup(ptr, this - ptr);
        }

        i++;
        if (this == NULL) {
            break;
        } else {
            ptr = this + 1;
        }
    }

    return i;
}

678
int net_init_tap(const NetClientOptions *opts, const char *name,
679
                 NetClientState *peer)
680
{
681
    const NetdevTapOptions *tap;
J
Jason Wang 已提交
682
    int fd, vnet_hdr = 0, i = 0, queues;
683 684
    /* for the no-fd, no-helper case */
    const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
685
    const char *downscript = NULL;
J
Jason Wang 已提交
686
    const char *vhostfdname;
687 688 689 690
    char ifname[128];

    assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
    tap = opts->tap;
J
Jason Wang 已提交
691 692
    queues = tap->has_queues ? tap->queues : 1;
    vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
693

694 695 696
    /* QEMU vlans does not support multiqueue tap, in this case peer is set.
     * For -netdev, peer is always NULL. */
    if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
P
Peter Maydell 已提交
697
        error_report("Multiqueue tap cannot be used with QEMU vlans");
698 699 700
        return -1;
    }

701 702
    if (tap->has_fd) {
        if (tap->has_ifname || tap->has_script || tap->has_downscript ||
J
Jason Wang 已提交
703
            tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
704
            tap->has_fds || tap->has_vhostfds) {
C
Corey Bryant 已提交
705
            error_report("ifname=, script=, downscript=, vnet_hdr=, "
706 707
                         "helper=, queues=, fds=, and vhostfds= "
                         "are invalid with fd=");
708 709 710
            return -1;
        }

711
        fd = monitor_handle_fd_param(cur_mon, tap->fd);
712 713 714 715 716 717 718
        if (fd == -1) {
            return -1;
        }

        fcntl(fd, F_SETFL, O_NONBLOCK);

        vnet_hdr = tap_probe_vnet_hdr(fd);
C
Corey Bryant 已提交
719

A
Anthony Liguori 已提交
720
        if (net_init_tap_one(tap, peer, "tap", name, NULL,
J
Jason Wang 已提交
721 722 723 724 725 726 727 728 729 730 731
                             script, downscript,
                             vhostfdname, vnet_hdr, fd)) {
            return -1;
        }
    } else if (tap->has_fds) {
        char *fds[MAX_TAP_QUEUES];
        char *vhost_fds[MAX_TAP_QUEUES];
        int nfds, nvhosts;

        if (tap->has_ifname || tap->has_script || tap->has_downscript ||
            tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
732
            tap->has_vhostfd) {
J
Jason Wang 已提交
733
            error_report("ifname=, script=, downscript=, vnet_hdr=, "
734 735
                         "helper=, queues=, and vhostfd= "
                         "are invalid with fds=");
J
Jason Wang 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
            return -1;
        }

        nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
        if (tap->has_vhostfds) {
            nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
            if (nfds != nvhosts) {
                error_report("The number of fds passed does not match the "
                             "number of vhostfds passed");
                return -1;
            }
        }

        for (i = 0; i < nfds; i++) {
            fd = monitor_handle_fd_param(cur_mon, fds[i]);
            if (fd == -1) {
                return -1;
            }

            fcntl(fd, F_SETFL, O_NONBLOCK);
C
Corey Bryant 已提交
756

J
Jason Wang 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770
            if (i == 0) {
                vnet_hdr = tap_probe_vnet_hdr(fd);
            } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
                error_report("vnet_hdr not consistent across given tap fds");
                return -1;
            }

            if (net_init_tap_one(tap, peer, "tap", name, ifname,
                                 script, downscript,
                                 tap->has_vhostfds ? vhost_fds[i] : NULL,
                                 vnet_hdr, fd)) {
                return -1;
            }
        }
771 772
    } else if (tap->has_helper) {
        if (tap->has_ifname || tap->has_script || tap->has_downscript ||
773
            tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
C
Corey Bryant 已提交
774
            error_report("ifname=, script=, downscript=, and vnet_hdr= "
775
                         "queues=, and vhostfds= are invalid with helper=");
C
Corey Bryant 已提交
776 777 778
            return -1;
        }

779
        fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
C
Corey Bryant 已提交
780 781 782 783 784 785 786
        if (fd == -1) {
            return -1;
        }

        fcntl(fd, F_SETFL, O_NONBLOCK);
        vnet_hdr = tap_probe_vnet_hdr(fd);

J
Jason Wang 已提交
787 788 789 790 791
        if (net_init_tap_one(tap, peer, "bridge", name, ifname,
                             script, downscript, vhostfdname,
                             vnet_hdr, fd)) {
            return -1;
        }
792
    } else {
793 794 795 796
        if (tap->has_vhostfds) {
            error_report("vhostfds= is invalid if fds= wasn't specified");
            return -1;
        }
797
        script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
798 799
        downscript = tap->has_downscript ? tap->downscript :
            DEFAULT_NETWORK_DOWN_SCRIPT;
J
Jason Wang 已提交
800 801 802 803 804

        if (tap->has_ifname) {
            pstrcpy(ifname, sizeof ifname, tap->ifname);
        } else {
            ifname[0] = '\0';
805
        }
C
Corey Bryant 已提交
806

J
Jason Wang 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        for (i = 0; i < queues; i++) {
            fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
                              ifname, sizeof ifname, queues > 1);
            if (fd == -1) {
                return -1;
            }

            if (queues > 1 && i == 0 && !tap->has_ifname) {
                if (tap_fd_get_ifname(fd, ifname)) {
                    error_report("Fail to get ifname");
                    return -1;
                }
            }

            if (net_init_tap_one(tap, peer, "tap", name, ifname,
                                 i >= 1 ? "no" : script,
                                 i >= 1 ? "no" : downscript,
                                 vhostfdname, vnet_hdr, fd)) {
                return -1;
            }
        }
828 829
    }

J
Jason Wang 已提交
830
    return 0;
831
}
832

833
VHostNetState *tap_get_vhost_net(NetClientState *nc)
834 835
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
836
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
837 838
    return s->vhost_net;
}
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

int tap_enable(NetClientState *nc)
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
    int ret;

    if (s->enabled) {
        return 0;
    } else {
        ret = tap_fd_enable(s->fd);
        if (ret == 0) {
            s->enabled = true;
            tap_update_fd_handler(s);
        }
        return ret;
    }
}

int tap_disable(NetClientState *nc)
{
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
    int ret;

    if (s->enabled == 0) {
        return 0;
    } else {
        ret = tap_fd_disable(s->fd);
        if (ret == 0) {
            qemu_purge_queued_packets(nc);
            s->enabled = false;
            tap_update_fd_handler(s);
        }
        return ret;
    }
}