qemu/net/vhost-user.c
<<
>>
Prefs
   1/*
   2 * vhost-user.c
   3 *
   4 * Copyright (c) 2013 Virtual Open Systems Sarl.
   5 *
   6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   7 * See the COPYING file in the top-level directory.
   8 *
   9 */
  10
  11#include "qemu/osdep.h"
  12#include "clients.h"
  13#include "net/vhost_net.h"
  14#include "net/vhost-user.h"
  15#include "chardev/char-fe.h"
  16#include "qemu/config-file.h"
  17#include "qemu/error-report.h"
  18#include "qmp-commands.h"
  19#include "trace.h"
  20
  21typedef struct VhostUserState {
  22    NetClientState nc;
  23    CharBackend chr; /* only queue index 0 */
  24    VHostNetState *vhost_net;
  25    guint watch;
  26    uint64_t acked_features;
  27    bool started;
  28} VhostUserState;
  29
  30VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
  31{
  32    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  33    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  34    return s->vhost_net;
  35}
  36
  37uint64_t vhost_user_get_acked_features(NetClientState *nc)
  38{
  39    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  40    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  41    return s->acked_features;
  42}
  43
  44static void vhost_user_stop(int queues, NetClientState *ncs[])
  45{
  46    VhostUserState *s;
  47    int i;
  48
  49    for (i = 0; i < queues; i++) {
  50        assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  51
  52        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
  53
  54        if (s->vhost_net) {
  55            /* save acked features */
  56            uint64_t features = vhost_net_get_acked_features(s->vhost_net);
  57            if (features) {
  58                s->acked_features = features;
  59            }
  60            vhost_net_cleanup(s->vhost_net);
  61        }
  62    }
  63}
  64
  65static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
  66{
  67    VhostNetOptions options;
  68    struct vhost_net *net = NULL;
  69    VhostUserState *s;
  70    int max_queues;
  71    int i;
  72
  73    options.backend_type = VHOST_BACKEND_TYPE_USER;
  74
  75    for (i = 0; i < queues; i++) {
  76        assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  77
  78        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
  79
  80        options.net_backend = ncs[i];
  81        options.opaque      = be;
  82        options.busyloop_timeout = 0;
  83        net = vhost_net_init(&options);
  84        if (!net) {
  85            error_report("failed to init vhost_net for queue %d", i);
  86            goto err;
  87        }
  88
  89        if (i == 0) {
  90            max_queues = vhost_net_get_max_queues(net);
  91            if (queues > max_queues) {
  92                error_report("you are asking more queues than supported: %d",
  93                             max_queues);
  94                goto err;
  95            }
  96        }
  97
  98        if (s->vhost_net) {
  99            vhost_net_cleanup(s->vhost_net);
 100            g_free(s->vhost_net);
 101        }
 102        s->vhost_net = net;
 103    }
 104
 105    return 0;
 106
 107err:
 108    if (net) {
 109        vhost_net_cleanup(net);
 110    }
 111    vhost_user_stop(i, ncs);
 112    return -1;
 113}
 114
 115static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
 116                                  size_t size)
 117{
 118    /* In case of RARP (message size is 60) notify backup to send a fake RARP.
 119       This fake RARP will be sent by backend only for guest
 120       without GUEST_ANNOUNCE capability.
 121     */
 122    if (size == 60) {
 123        VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
 124        int r;
 125        static int display_rarp_failure = 1;
 126        char mac_addr[6];
 127
 128        /* extract guest mac address from the RARP message */
 129        memcpy(mac_addr, &buf[6], 6);
 130
 131        r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
 132
 133        if ((r != 0) && (display_rarp_failure)) {
 134            fprintf(stderr,
 135                    "Vhost user backend fails to broadcast fake RARP\n");
 136            fflush(stderr);
 137            display_rarp_failure = 0;
 138        }
 139    }
 140
 141    return size;
 142}
 143
 144static void vhost_user_cleanup(NetClientState *nc)
 145{
 146    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
 147
 148    if (s->vhost_net) {
 149        vhost_net_cleanup(s->vhost_net);
 150        g_free(s->vhost_net);
 151        s->vhost_net = NULL;
 152    }
 153    if (nc->queue_index == 0) {
 154        if (s->watch) {
 155            g_source_remove(s->watch);
 156            s->watch = 0;
 157        }
 158        qemu_chr_fe_deinit(&s->chr, true);
 159    }
 160
 161    qemu_purge_queued_packets(nc);
 162}
 163
 164static bool vhost_user_has_vnet_hdr(NetClientState *nc)
 165{
 166    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
 167
 168    return true;
 169}
 170
 171static bool vhost_user_has_ufo(NetClientState *nc)
 172{
 173    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
 174
 175    return true;
 176}
 177
 178static NetClientInfo net_vhost_user_info = {
 179        .type = NET_CLIENT_DRIVER_VHOST_USER,
 180        .size = sizeof(VhostUserState),
 181        .receive = vhost_user_receive,
 182        .cleanup = vhost_user_cleanup,
 183        .has_vnet_hdr = vhost_user_has_vnet_hdr,
 184        .has_ufo = vhost_user_has_ufo,
 185};
 186
 187static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
 188                                           void *opaque)
 189{
 190    VhostUserState *s = opaque;
 191
 192    qemu_chr_fe_disconnect(&s->chr);
 193
 194    return TRUE;
 195}
 196
 197static void net_vhost_user_event(void *opaque, int event);
 198
 199static void chr_closed_bh(void *opaque)
 200{
 201    const char *name = opaque;
 202    NetClientState *ncs[MAX_QUEUE_NUM];
 203    VhostUserState *s;
 204    Error *err = NULL;
 205    int queues;
 206
 207    queues = qemu_find_net_clients_except(name, ncs,
 208                                          NET_CLIENT_DRIVER_NIC,
 209                                          MAX_QUEUE_NUM);
 210    assert(queues < MAX_QUEUE_NUM);
 211
 212    s = DO_UPCAST(VhostUserState, nc, ncs[0]);
 213
 214    qmp_set_link(name, false, &err);
 215    vhost_user_stop(queues, ncs);
 216
 217    qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
 218                             NULL, opaque, NULL, true);
 219
 220    if (err) {
 221        error_report_err(err);
 222    }
 223}
 224
 225static void net_vhost_user_event(void *opaque, int event)
 226{
 227    const char *name = opaque;
 228    NetClientState *ncs[MAX_QUEUE_NUM];
 229    VhostUserState *s;
 230    Chardev *chr;
 231    Error *err = NULL;
 232    int queues;
 233
 234    queues = qemu_find_net_clients_except(name, ncs,
 235                                          NET_CLIENT_DRIVER_NIC,
 236                                          MAX_QUEUE_NUM);
 237    assert(queues < MAX_QUEUE_NUM);
 238
 239    s = DO_UPCAST(VhostUserState, nc, ncs[0]);
 240    chr = qemu_chr_fe_get_driver(&s->chr);
 241    trace_vhost_user_event(chr->label, event);
 242    switch (event) {
 243    case CHR_EVENT_OPENED:
 244        if (vhost_user_start(queues, ncs, &s->chr) < 0) {
 245            qemu_chr_fe_disconnect(&s->chr);
 246            return;
 247        }
 248        s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
 249                                         net_vhost_user_watch, s);
 250        qmp_set_link(name, true, &err);
 251        s->started = true;
 252        break;
 253    case CHR_EVENT_CLOSED:
 254        /* a close event may happen during a read/write, but vhost
 255         * code assumes the vhost_dev remains setup, so delay the
 256         * stop & clear to idle.
 257         * FIXME: better handle failure in vhost code, remove bh
 258         */
 259        if (s->watch) {
 260            AioContext *ctx = qemu_get_current_aio_context();
 261
 262            g_source_remove(s->watch);
 263            s->watch = 0;
 264            qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
 265                                     NULL, NULL, false);
 266
 267            aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
 268        }
 269        break;
 270    }
 271
 272    if (err) {
 273        error_report_err(err);
 274    }
 275}
 276
 277static int net_vhost_user_init(NetClientState *peer, const char *device,
 278                               const char *name, Chardev *chr,
 279                               int queues)
 280{
 281    Error *err = NULL;
 282    NetClientState *nc, *nc0 = NULL;
 283    VhostUserState *s;
 284    int i;
 285
 286    assert(name);
 287    assert(queues > 0);
 288
 289    for (i = 0; i < queues; i++) {
 290        nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
 291        snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
 292                 i, chr->label);
 293        nc->queue_index = i;
 294        if (!nc0) {
 295            nc0 = nc;
 296            s = DO_UPCAST(VhostUserState, nc, nc);
 297            if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
 298                error_report_err(err);
 299                return -1;
 300            }
 301        }
 302
 303    }
 304
 305    s = DO_UPCAST(VhostUserState, nc, nc0);
 306    do {
 307        if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
 308            error_report_err(err);
 309            return -1;
 310        }
 311        qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
 312                                 net_vhost_user_event, NULL, nc0->name, NULL,
 313                                 true);
 314    } while (!s->started);
 315
 316    assert(s->vhost_net);
 317
 318    return 0;
 319}
 320
 321static Chardev *net_vhost_claim_chardev(
 322    const NetdevVhostUserOptions *opts, Error **errp)
 323{
 324    Chardev *chr = qemu_chr_find(opts->chardev);
 325
 326    if (chr == NULL) {
 327        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
 328        return NULL;
 329    }
 330
 331    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
 332        error_setg(errp, "chardev \"%s\" is not reconnectable",
 333                   opts->chardev);
 334        return NULL;
 335    }
 336    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
 337        error_setg(errp, "chardev \"%s\" does not support FD passing",
 338                   opts->chardev);
 339        return NULL;
 340    }
 341
 342    return chr;
 343}
 344
 345static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
 346{
 347    const char *name = opaque;
 348    const char *driver, *netdev;
 349
 350    driver = qemu_opt_get(opts, "driver");
 351    netdev = qemu_opt_get(opts, "netdev");
 352
 353    if (!driver || !netdev) {
 354        return 0;
 355    }
 356
 357    if (strcmp(netdev, name) == 0 &&
 358        !g_str_has_prefix(driver, "virtio-net-")) {
 359        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
 360        return -1;
 361    }
 362
 363    return 0;
 364}
 365
 366int net_init_vhost_user(const Netdev *netdev, const char *name,
 367                        NetClientState *peer, Error **errp)
 368{
 369    int queues;
 370    const NetdevVhostUserOptions *vhost_user_opts;
 371    Chardev *chr;
 372
 373    assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
 374    vhost_user_opts = &netdev->u.vhost_user;
 375
 376    chr = net_vhost_claim_chardev(vhost_user_opts, errp);
 377    if (!chr) {
 378        return -1;
 379    }
 380
 381    /* verify net frontend */
 382    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
 383                          (char *)name, errp)) {
 384        return -1;
 385    }
 386
 387    queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
 388    if (queues < 1 || queues > MAX_QUEUE_NUM) {
 389        error_setg(errp,
 390                   "vhost-user number of queues must be in range [1, %d]",
 391                   MAX_QUEUE_NUM);
 392        return -1;
 393    }
 394
 395    return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
 396}
 397