vhost-user.c 10.6 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 "chardev/char-fe.h"
16
#include "qapi/error.h"
17
#include "qemu/config-file.h"
18
#include "qemu/error-report.h"
19
#include "qmp-commands.h"
20
#include "trace.h"
21 22 23

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

VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
{
    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
34
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
35 36 37
    return s->vhost_net;
}

38 39 40
uint64_t vhost_user_get_acked_features(NetClientState *nc)
{
    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
41
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
42 43 44
    return s->acked_features;
}

45
static void vhost_user_stop(int queues, NetClientState *ncs[])
46
{
47 48
    VhostUserState *s;
    int i;
49

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

53
        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
54

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

66
static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
67
{
68
    VhostNetOptions options;
69
    struct vhost_net *net = NULL;
70 71 72 73 74 75 76
    VhostUserState *s;
    int max_queues;
    int i;

    options.backend_type = VHOST_BACKEND_TYPE_USER;

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

        s = DO_UPCAST(VhostUserState, nc, ncs[i]);

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

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

        if (s->vhost_net) {
            vhost_net_cleanup(s->vhost_net);
            g_free(s->vhost_net);
        }
        s->vhost_net = net;
104 105
    }

106 107 108
    return 0;

err:
109 110 111 112
    if (net) {
        vhost_net_cleanup(net);
    }
    vhost_user_stop(i, ncs);
113
    return -1;
114 115
}

116 117 118
static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
                                  size_t size)
{
119 120 121
    /* 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.
122
     */
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    if (size == 60) {
        VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
        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;
        }
    }

142 143 144
    return size;
}

145 146 147 148
static void vhost_user_cleanup(NetClientState *nc)
{
    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);

149 150
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
151
        g_free(s->vhost_net);
152 153
        s->vhost_net = NULL;
    }
154
    if (nc->queue_index == 0) {
155 156 157 158
        if (s->watch) {
            g_source_remove(s->watch);
            s->watch = 0;
        }
159
        qemu_chr_fe_deinit(&s->chr, true);
160
    }
161

162 163 164 165 166
    qemu_purge_queued_packets(nc);
}

static bool vhost_user_has_vnet_hdr(NetClientState *nc)
{
167
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
168 169 170 171 172 173

    return true;
}

static bool vhost_user_has_ufo(NetClientState *nc)
{
174
    assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
175 176 177 178 179

    return true;
}

static NetClientInfo net_vhost_user_info = {
180
        .type = NET_CLIENT_DRIVER_VHOST_USER,
181
        .size = sizeof(VhostUserState),
182
        .receive = vhost_user_receive,
183 184 185 186 187
        .cleanup = vhost_user_cleanup,
        .has_vnet_hdr = vhost_user_has_vnet_hdr,
        .has_ufo = vhost_user_has_ufo,
};

188 189 190 191 192
static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
                                           void *opaque)
{
    VhostUserState *s = opaque;

193
    qemu_chr_fe_disconnect(&s->chr);
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    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];
    VhostUserState *s;
    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);

    s = DO_UPCAST(VhostUserState, nc, ncs[0]);

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

    qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
219
                             NULL, opaque, NULL, true);
220 221 222 223

    if (err) {
        error_report_err(err);
    }
224 225
}

226 227
static void net_vhost_user_event(void *opaque, int event)
{
228 229 230
    const char *name = opaque;
    NetClientState *ncs[MAX_QUEUE_NUM];
    VhostUserState *s;
231
    Chardev *chr;
232 233
    Error *err = NULL;
    int queues;
234

235
    queues = qemu_find_net_clients_except(name, ncs,
236
                                          NET_CLIENT_DRIVER_NIC,
237
                                          MAX_QUEUE_NUM);
238 239
    assert(queues < MAX_QUEUE_NUM);

240
    s = DO_UPCAST(VhostUserState, nc, ncs[0]);
241 242
    chr = qemu_chr_fe_get_driver(&s->chr);
    trace_vhost_user_event(chr->label, event);
243 244
    switch (event) {
    case CHR_EVENT_OPENED:
245
        if (vhost_user_start(queues, ncs, &s->chr) < 0) {
246
            qemu_chr_fe_disconnect(&s->chr);
247
            return;
248
        }
249 250
        s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
                                         net_vhost_user_watch, s);
251
        qmp_set_link(name, true, &err);
252
        s->started = true;
253 254
        break;
    case CHR_EVENT_CLOSED:
255 256 257 258 259 260 261 262 263 264
        /* 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;
265
            qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL, NULL,
266 267 268 269
                                     NULL, NULL, false);

            aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
        }
270 271
        break;
    }
272 273 274 275

    if (err) {
        error_report_err(err);
    }
276 277 278
}

static int net_vhost_user_init(NetClientState *peer, const char *device,
279
                               const char *name, Chardev *chr,
280
                               int queues)
281
{
282
    Error *err = NULL;
283
    NetClientState *nc, *nc0 = NULL;
284
    VhostUserState *s;
285
    int i;
286

287 288 289
    assert(name);
    assert(queues > 0);

290 291 292 293 294
    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;
295 296 297 298 299 300 301
        if (!nc0) {
            nc0 = nc;
            s = DO_UPCAST(VhostUserState, nc, nc);
            if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
                error_report_err(err);
                return -1;
            }
302
        }
303

304
    }
305

306 307
    s = DO_UPCAST(VhostUserState, nc, nc0);
    do {
308
        if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
309 310 311
            error_report_err(err);
            return -1;
        }
312
        qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
313 314
                                 net_vhost_user_event, NULL, nc0->name, NULL,
                                 true);
315
    } while (!s->started);
316

317 318
    assert(s->vhost_net);

319 320 321
    return 0;
}

322
static Chardev *net_vhost_claim_chardev(
323
    const NetdevVhostUserOptions *opts, Error **errp)
324
{
325
    Chardev *chr = qemu_chr_find(opts->chardev);
326 327

    if (chr == NULL) {
328
        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
329 330 331
        return NULL;
    }

332 333 334
    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
        error_setg(errp, "chardev \"%s\" is not reconnectable",
                   opts->chardev);
335 336
        return NULL;
    }
337 338
    if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
        error_setg(errp, "chardev \"%s\" does not support FD passing",
339
                   opts->chardev);
340 341 342 343 344 345
        return NULL;
    }

    return chr;
}

346
static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
347 348 349 350 351 352 353 354 355 356 357 358
{
    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 &&
359
        !g_str_has_prefix(driver, "virtio-net-")) {
360
        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
361 362 363 364 365 366
        return -1;
    }

    return 0;
}

367
int net_init_vhost_user(const Netdev *netdev, const char *name,
368
                        NetClientState *peer, Error **errp)
369
{
370
    int queues;
371
    const NetdevVhostUserOptions *vhost_user_opts;
372
    Chardev *chr;
373

374 375
    assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
    vhost_user_opts = &netdev->u.vhost_user;
376

377
    chr = net_vhost_claim_chardev(vhost_user_opts, errp);
378 379 380 381 382 383
    if (!chr) {
        return -1;
    }

    /* verify net frontend */
    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
384
                          (char *)name, errp)) {
385 386 387
        return -1;
    }

388
    queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
389
    if (queues < 1 || queues > MAX_QUEUE_NUM) {
390
        error_setg(errp,
391 392
                   "vhost-user number of queues must be in range [1, %d]",
                   MAX_QUEUE_NUM);
393 394
        return -1;
    }
395

396
    return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
397
}