vhost-user.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * vhost-user.c
 *
 * Copyright (c) 2013 Virtual Open Systems Sarl.
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
11
#include "qemu/osdep.h"
12 13 14
#include "clients.h"
#include "net/vhost_net.h"
#include "net/vhost-user.h"
15
#include "hw/virtio/vhost-user.h"
16
#include "chardev/char-fe.h"
17
#include "qapi/error.h"
18
#include "qapi/qapi-commands-net.h"
19
#include "qemu/config-file.h"
20
#include "qemu/error-report.h"
21
#include "qemu/option.h"
22
#include "trace.h"
23

24
typedef struct NetVhostUserState {
25
    NetClientState nc;
26
    CharBackend chr; /* only queue index 0 */
27
    VhostUserState *vhost_user;
28
    VHostNetState *vhost_net;
29
    guint watch;
30
    uint64_t acked_features;
31
    bool started;
32
} NetVhostUserState;
33 34 35

VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
{
36
    NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
37
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
38 39 40
    return s->vhost_net;
}

41 42
uint64_t vhost_user_get_acked_features(NetClientState *nc)
{
43
    NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
44
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
45 46 47
    return s->acked_features;
}

48
static void vhost_user_stop(int queues, NetClientState *ncs[])
49
{
50
    NetVhostUserState *s;
51
    int i;
52

53
    for (i = 0; i < queues; i++) {
54
        assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
55

56
        s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
57

58
        if (s->vhost_net) {
59
            /* save acked features */
60 61 62 63
            uint64_t features = vhost_net_get_acked_features(s->vhost_net);
            if (features) {
                s->acked_features = features;
            }
64 65 66
            vhost_net_cleanup(s->vhost_net);
        }
    }
67 68
}

69 70
static int vhost_user_start(int queues, NetClientState *ncs[],
                            VhostUserState *be)
71
{
72
    VhostNetOptions options;
73
    struct vhost_net *net = NULL;
74
    NetVhostUserState *s;
75 76 77 78 79 80
    int max_queues;
    int i;

    options.backend_type = VHOST_BACKEND_TYPE_USER;

    for (i = 0; i < queues; i++) {
81
        assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
82

83
        s = DO_UPCAST(NetVhostUserState, nc, ncs[i]);
84 85

        options.net_backend = ncs[i];
86
        options.opaque      = be;
J
Jason Wang 已提交
87
        options.busyloop_timeout = 0;
88 89
        net = vhost_net_init(&options);
        if (!net) {
90
            error_report("failed to init vhost_net for queue %d", i);
91 92 93 94
            goto err;
        }

        if (i == 0) {
95
            max_queues = vhost_net_get_max_queues(net);
96
            if (queues > max_queues) {
97 98
                error_report("you are asking more queues than supported: %d",
                             max_queues);
99 100 101
                goto err;
            }
        }
102 103 104 105 106 107

        if (s->vhost_net) {
            vhost_net_cleanup(s->vhost_net);
            g_free(s->vhost_net);
        }
        s->vhost_net = net;
108 109
    }

110 111 112
    return 0;

err:
113 114
    if (net) {
        vhost_net_cleanup(net);
L
linzhecheng 已提交
115
        g_free(net);
116 117
    }
    vhost_user_stop(i, ncs);
118
    return -1;
119 120
}

121 122 123
static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
                                  size_t size)
{
124 125 126
    /* In case of RARP (message size is 60) notify backup to send a fake RARP.
       This fake RARP will be sent by backend only for guest
       without GUEST_ANNOUNCE capability.
127
     */
128
    if (size == 60) {
129
        NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        int r;
        static int display_rarp_failure = 1;
        char mac_addr[6];

        /* extract guest mac address from the RARP message */
        memcpy(mac_addr, &buf[6], 6);

        r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);

        if ((r != 0) && (display_rarp_failure)) {
            fprintf(stderr,
                    "Vhost user backend fails to broadcast fake RARP\n");
            fflush(stderr);
            display_rarp_failure = 0;
        }
    }

147 148 149
    return size;
}

150
static void net_vhost_user_cleanup(NetClientState *nc)
151
{
152
    NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
153

154 155
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
156
        g_free(s->vhost_net);
157 158
        s->vhost_net = NULL;
    }
159
    if (nc->queue_index == 0) {
160 161 162 163
        if (s->watch) {
            g_source_remove(s->watch);
            s->watch = 0;
        }
164
        qemu_chr_fe_deinit(&s->chr, true);
165 166 167 168 169
        if (s->vhost_user) {
            vhost_user_cleanup(s->vhost_user);
            g_free(s->vhost_user);
            s->vhost_user = NULL;
        }
170
    }
171

172 173 174
    qemu_purge_queued_packets(nc);
}

175 176 177 178 179 180 181 182 183 184 185
static int vhost_user_set_vnet_endianness(NetClientState *nc,
                                          bool enable)
{
    /* Nothing to do.  If the server supports
     * VHOST_USER_PROTOCOL_F_CROSS_ENDIAN, it will get the
     * vnet header endianness from there.  If it doesn't, negotiation
     * fails.
     */
    return 0;
}

186 187
static bool vhost_user_has_vnet_hdr(NetClientState *nc)
{
188
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
189 190 191 192 193 194

    return true;
}

static bool vhost_user_has_ufo(NetClientState *nc)
{
195
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
196 197 198 199 200

    return true;
}

static NetClientInfo net_vhost_user_info = {
201
        .type = NET_CLIENT_DRIVER_VHOST_USER,
202
        .size = sizeof(NetVhostUserState),
203
        .receive = vhost_user_receive,
204
        .cleanup = net_vhost_user_cleanup,
205 206
        .has_vnet_hdr = vhost_user_has_vnet_hdr,
        .has_ufo = vhost_user_has_ufo,
207 208
        .set_vnet_be = vhost_user_set_vnet_endianness,
        .set_vnet_le = vhost_user_set_vnet_endianness,
209 210
};

211 212 213
static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
                                           void *opaque)
{
214
    NetVhostUserState *s = opaque;
215

216
    qemu_chr_fe_disconnect(&s->chr);
217

218 219 220 221 222 223 224 225 226
    return TRUE;
}

static void net_vhost_user_event(void *opaque, int event);

static void chr_closed_bh(void *opaque)
{
    const char *name = opaque;
    NetClientState *ncs[MAX_QUEUE_NUM];
227
    NetVhostUserState *s;
228 229 230 231 232 233 234 235
    Error *err = NULL;
    int queues;

    queues = qemu_find_net_clients_except(name, ncs,
                                          NET_CLIENT_DRIVER_NIC,
                                          MAX_QUEUE_NUM);
    assert(queues < MAX_QUEUE_NUM);

236
    s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
237 238 239 240 241

    qmp_set_link(name, false, &err);
    vhost_user_stop(queues, ncs);

    qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
242
                             NULL, opaque, NULL, true);
243 244 245 246

    if (err) {
        error_report_err(err);
    }
247 248
}

249 250
static void net_vhost_user_event(void *opaque, int event)
{
251 252
    const char *name = opaque;
    NetClientState *ncs[MAX_QUEUE_NUM];
253
    NetVhostUserState *s;
254
    Chardev *chr;
255 256
    Error *err = NULL;
    int queues;
257

258
    queues = qemu_find_net_clients_except(name, ncs,
259
                                          NET_CLIENT_DRIVER_NIC,
260
                                          MAX_QUEUE_NUM);
261 262
    assert(queues < MAX_QUEUE_NUM);

263
    s = DO_UPCAST(NetVhostUserState, nc, ncs[0]);
264 265
    chr = qemu_chr_fe_get_driver(&s->chr);
    trace_vhost_user_event(chr->label, event);
266 267
    switch (event) {
    case CHR_EVENT_OPENED:
268
        if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
269
            qemu_chr_fe_disconnect(&s->chr);
270
            return;
271
        }
272 273
        s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
                                         net_vhost_user_watch, s);
274
        qmp_set_link(name, true, &err);
275
        s->started = true;
276 277
        break;
    case CHR_EVENT_CLOSED:
278 279 280 281 282 283 284 285 286 287
        /* a close event may happen during a read/write, but vhost
         * code assumes the vhost_dev remains setup, so delay the
         * stop & clear to idle.
         * FIXME: better handle failure in vhost code, remove bh
         */
        if (s->watch) {
            AioContext *ctx = qemu_get_current_aio_context();

            g_source_remove(s->watch);
            s->watch = 0;
288
            qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
289 290 291 292
                                     NULL, NULL, false);

            aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
        }
293 294
        break;
    }
295 296 297 298

    if (err) {
        error_report_err(err);
    }
299 300 301
}

static int net_vhost_user_init(NetClientState *peer, const char *device,
302
                               const char *name, Chardev *chr,
303
                               int queues)
304
{
305
    Error *err = NULL;
306
    NetClientState *nc, *nc0 = NULL;
307 308
    VhostUserState *user = NULL;
    NetVhostUserState *s = NULL;
309
    int i;
310

311 312 313
    assert(name);
    assert(queues > 0);

314 315 316 317 318 319
    user = vhost_user_init();
    if (!user) {
        error_report("failed to init vhost_user");
        goto err;
    }

320 321 322 323 324
    for (i = 0; i < queues; i++) {
        nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
        snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
                 i, chr->label);
        nc->queue_index = i;
325 326
        if (!nc0) {
            nc0 = nc;
327
            s = DO_UPCAST(NetVhostUserState, nc, nc);
328 329
            if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
                error_report_err(err);
330
                goto err;
331
            }
332
            user->chr = &s->chr;
333
        }
334 335
        s = DO_UPCAST(NetVhostUserState, nc, nc);
        s->vhost_user = user;
336
    }
337

338
    s = DO_UPCAST(NetVhostUserState, nc, nc0);
339
    do {
340
        if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
341
            error_report_err(err);
342
            goto err;
343
        }
344
        qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
345 346
                                 net_vhost_user_event, NULL, nc0->name, NULL,
                                 true);
347
    } while (!s->started);
348

349 350
    assert(s->vhost_net);

351
    return 0;
352 353 354 355 356 357 358 359 360

err:
    if (user) {
        vhost_user_cleanup(user);
        g_free(user);
        if (s) {
            s->vhost_user = NULL;
        }
    }
361 362 363
    if (nc0) {
        qemu_del_net_client(nc0);
    }
364 365

    return -1;
366 367
}

368
static Chardev *net_vhost_claim_chardev(
369
    const NetdevVhostUserOptions *opts, Error **errp)
370
{
371
    Chardev *chr = qemu_chr_find(opts->chardev);
372 373

    if (chr == NULL) {
374
        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
375 376 377
        return NULL;
    }

378 379 380
    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
        error_setg(errp, "chardev \"%s\" is not reconnectable",
                   opts->chardev);
381 382
        return NULL;
    }
383 384
    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
        error_setg(errp, "chardev \"%s\" does not support FD passing",
385
                   opts->chardev);
386 387 388 389 390 391
        return NULL;
    }

    return chr;
}

392
static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
393 394 395 396 397 398 399 400 401 402 403 404
{
    const char *name = opaque;
    const char *driver, *netdev;

    driver = qemu_opt_get(opts, "driver");
    netdev = qemu_opt_get(opts, "netdev");

    if (!driver || !netdev) {
        return 0;
    }

    if (strcmp(netdev, name) == 0 &&
405
        !g_str_has_prefix(driver, "virtio-net-")) {
406
        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
407 408 409 410 411 412
        return -1;
    }

    return 0;
}

413
int net_init_vhost_user(const Netdev *netdev, const char *name,
414
                        NetClientState *peer, Error **errp)
415
{
416
    int queues;
417
    const NetdevVhostUserOptions *vhost_user_opts;
418
    Chardev *chr;
419

420 421
    assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
    vhost_user_opts = &netdev->u.vhost_user;
422

423
    chr = net_vhost_claim_chardev(vhost_user_opts, errp);
424 425 426 427 428 429
    if (!chr) {
        return -1;
    }

    /* verify net frontend */
    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
430
                          (char *)name, errp)) {
431 432 433
        return -1;
    }

434
    queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
435
    if (queues < 1 || queues > MAX_QUEUE_NUM) {
436
        error_setg(errp,
437 438
                   "vhost-user number of queues must be in range [1, %d]",
                   MAX_QUEUE_NUM);
439 440
        return -1;
    }
441

442
    return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
443
}