vhost-user.c 9.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 15
#include "clients.h"
#include "net/vhost_net.h"
#include "net/vhost-user.h"
#include "sysemu/char.h"
16
#include "qemu/config-file.h"
17
#include "qemu/error-report.h"
18
#include "qmp-commands.h"
19
#include "trace.h"
20 21 22 23 24

typedef struct VhostUserState {
    NetClientState nc;
    CharDriverState *chr;
    VHostNetState *vhost_net;
25
    guint watch;
26
    uint64_t acked_features;
27 28
} VhostUserState;

29 30 31 32 33
typedef struct VhostUserChardevProps {
    bool is_socket;
    bool is_unix;
} VhostUserChardevProps;

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

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

48 49 50 51 52
static int vhost_user_running(VhostUserState *s)
{
    return (s->vhost_net) ? 1 : 0;
}

53
static void vhost_user_stop(int queues, NetClientState *ncs[])
54
{
55 56
    VhostUserState *s;
    int i;
57

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

61 62 63 64
        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
        if (!vhost_user_running(s)) {
            continue;
        }
65

66
        if (s->vhost_net) {
67 68
            /* save acked features */
            s->acked_features = vhost_net_get_acked_features(s->vhost_net);
69 70 71 72
            vhost_net_cleanup(s->vhost_net);
            s->vhost_net = NULL;
        }
    }
73 74
}

75
static int vhost_user_start(int queues, NetClientState *ncs[])
76
{
77 78 79 80 81 82 83 84
    VhostNetOptions options;
    VhostUserState *s;
    int max_queues;
    int i;

    options.backend_type = VHOST_BACKEND_TYPE_USER;

    for (i = 0; i < queues; i++) {
85
        assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
86 87 88 89 90 91 92 93

        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
        if (vhost_user_running(s)) {
            continue;
        }

        options.net_backend = ncs[i];
        options.opaque      = s->chr;
J
Jason Wang 已提交
94
        options.busyloop_timeout = 0;
95 96
        s->vhost_net = vhost_net_init(&options);
        if (!s->vhost_net) {
97
            error_report("failed to init vhost_net for queue %d", i);
98 99 100 101 102 103
            goto err;
        }

        if (i == 0) {
            max_queues = vhost_net_get_max_queues(s->vhost_net);
            if (queues > max_queues) {
104 105
                error_report("you are asking more queues than supported: %d",
                             max_queues);
106 107 108
                goto err;
            }
        }
109 110
    }

111 112 113 114 115
    return 0;

err:
    vhost_user_stop(i + 1, ncs);
    return -1;
116 117
}

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

144 145 146
    return size;
}

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

151 152 153 154
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
        s->vhost_net = NULL;
    }
155 156 157 158 159
    if (s->chr) {
        qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
        qemu_chr_fe_release(s->chr);
        s->chr = NULL;
    }
160

161 162 163 164 165
    qemu_purge_queued_packets(nc);
}

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

    return true;
}

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

    return true;
}

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200
static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
                                           void *opaque)
{
    VhostUserState *s = opaque;
    uint8_t buf[1];

    /* We don't actually want to read anything, but CHR_EVENT_CLOSED will be
     * raised as a side-effect of the read.
     */
    qemu_chr_fe_read_all(s->chr, buf, sizeof(buf));

    return FALSE;
}

201 202
static void net_vhost_user_event(void *opaque, int event)
{
203 204 205 206 207
    const char *name = opaque;
    NetClientState *ncs[MAX_QUEUE_NUM];
    VhostUserState *s;
    Error *err = NULL;
    int queues;
208

209
    queues = qemu_find_net_clients_except(name, ncs,
210
                                          NET_CLIENT_DRIVER_NIC,
211
                                          MAX_QUEUE_NUM);
212 213
    assert(queues < MAX_QUEUE_NUM);

214
    s = DO_UPCAST(VhostUserState, nc, ncs[0]);
215
    trace_vhost_user_event(s->chr->label, event);
216 217
    switch (event) {
    case CHR_EVENT_OPENED:
218 219
        s->watch = qemu_chr_fe_add_watch(s->chr, G_IO_HUP,
                                         net_vhost_user_watch, s);
220
        if (vhost_user_start(queues, ncs) < 0) {
221 222
            qemu_chr_disconnect(s->chr);
            return;
223 224
        }
        qmp_set_link(name, true, &err);
225 226
        break;
    case CHR_EVENT_CLOSED:
227
        qmp_set_link(name, false, &err);
228
        vhost_user_stop(queues, ncs);
229 230
        g_source_remove(s->watch);
        s->watch = 0;
231 232
        break;
    }
233 234 235 236

    if (err) {
        error_report_err(err);
    }
237 238 239
}

static int net_vhost_user_init(NetClientState *peer, const char *device,
240 241
                               const char *name, CharDriverState *chr,
                               int queues)
242 243 244
{
    NetClientState *nc;
    VhostUserState *s;
245
    int i;
246

247 248 249
    assert(name);
    assert(queues > 0);

250 251
    for (i = 0; i < queues; i++) {
        nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
252

253 254
        snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
                 i, chr->label);
255

256
        nc->queue_index = i;
257

258 259 260
        s = DO_UPCAST(VhostUserState, nc, nc);
        s->chr = chr;
    }
261

262
    qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
263 264 265 266

    return 0;
}

267 268 269
static int net_vhost_chardev_opts(void *opaque,
                                  const char *name, const char *value,
                                  Error **errp)
270 271 272 273 274 275 276 277 278
{
    VhostUserChardevProps *props = opaque;

    if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
        props->is_socket = true;
    } else if (strcmp(name, "path") == 0) {
        props->is_unix = true;
    } else if (strcmp(name, "server") == 0) {
    } else {
279 280 281
        error_setg(errp,
                   "vhost-user does not support a chardev with option %s=%s",
                   name, value);
282 283 284 285 286
        return -1;
    }
    return 0;
}

287 288
static CharDriverState *net_vhost_parse_chardev(
    const NetdevVhostUserOptions *opts, Error **errp)
289 290 291 292 293
{
    CharDriverState *chr = qemu_chr_find(opts->chardev);
    VhostUserChardevProps props;

    if (chr == NULL) {
294
        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
295 296 297 298 299
        return NULL;
    }

    /* inspect chardev opts */
    memset(&props, 0, sizeof(props));
300
    if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
301 302 303 304
        return NULL;
    }

    if (!props.is_socket || !props.is_unix) {
305 306
        error_setg(errp, "chardev \"%s\" is not a unix socket",
                   opts->chardev);
307 308 309 310 311 312 313 314
        return NULL;
    }

    qemu_chr_fe_claim_no_fail(chr);

    return chr;
}

315
static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
316 317 318 319 320 321 322 323 324 325 326 327 328 329
{
    const char *name = opaque;
    const char *driver, *netdev;
    const char virtio_name[] = "virtio-net-";

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

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

    if (strcmp(netdev, name) == 0 &&
        strncmp(driver, virtio_name, strlen(virtio_name)) != 0) {
330
        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
331 332 333 334 335 336
        return -1;
    }

    return 0;
}

337
int net_init_vhost_user(const Netdev *netdev, const char *name,
338
                        NetClientState *peer, Error **errp)
339
{
340
    int queues;
341 342 343
    const NetdevVhostUserOptions *vhost_user_opts;
    CharDriverState *chr;

344 345
    assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
    vhost_user_opts = &netdev->u.vhost_user;
346

347
    chr = net_vhost_parse_chardev(vhost_user_opts, errp);
348 349 350 351 352 353
    if (!chr) {
        return -1;
    }

    /* verify net frontend */
    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
354
                          (char *)name, errp)) {
355 356 357
        return -1;
    }

358
    queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
359
    if (queues < 1 || queues > MAX_QUEUE_NUM) {
360
        error_setg(errp,
361 362
                   "vhost-user number of queues must be in range [1, %d]",
                   MAX_QUEUE_NUM);
363 364
        return -1;
    }
365

366
    return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
367
}