vhost-user.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 *
 */

#include "clients.h"
#include "net/vhost_net.h"
#include "net/vhost-user.h"
#include "sysemu/char.h"
15
#include "qemu/config-file.h"
16
#include "qemu/error-report.h"
17
#include "qmp-commands.h"
18
#include "trace.h"
19 20 21 22 23 24 25

typedef struct VhostUserState {
    NetClientState nc;
    CharDriverState *chr;
    VHostNetState *vhost_net;
} VhostUserState;

26 27 28 29 30 31
typedef struct VhostUserChardevProps {
    bool is_socket;
    bool is_unix;
    bool is_server;
} VhostUserChardevProps;

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

static int vhost_user_running(VhostUserState *s)
{
    return (s->vhost_net) ? 1 : 0;
}

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

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

52 53 54 55
        s = DO_UPCAST(VhostUserState, nc, ncs[i]);
        if (!vhost_user_running(s)) {
            continue;
        }
56

57 58 59 60 61
        if (s->vhost_net) {
            vhost_net_cleanup(s->vhost_net);
            s->vhost_net = NULL;
        }
    }
62 63
}

64
static int vhost_user_start(int queues, NetClientState *ncs[])
65
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    VhostNetOptions options;
    VhostUserState *s;
    int max_queues;
    int i;

    options.backend_type = VHOST_BACKEND_TYPE_USER;

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

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

        options.net_backend = ncs[i];
        options.opaque      = s->chr;
        s->vhost_net = vhost_net_init(&options);
        if (!s->vhost_net) {
            error_report("failed to init vhost_net for queue %d\n", i);
            goto err;
        }

        if (i == 0) {
            max_queues = vhost_net_get_max_queues(s->vhost_net);
            if (queues > max_queues) {
                error_report("you are asking more queues than "
                             "supported: %d\n", max_queues);
                goto err;
            }
        }
97 98
    }

99 100 101 102 103
    return 0;

err:
    vhost_user_stop(i + 1, ncs);
    return -1;
104 105
}

106 107 108
static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
                                  size_t size)
{
109 110 111
    /* 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.
112
     */
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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;
        }
    }

132 133 134
    return size;
}

135 136 137 138
static void vhost_user_cleanup(NetClientState *nc)
{
    VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);

139 140 141 142 143
    if (s->vhost_net) {
        vhost_net_cleanup(s->vhost_net);
        s->vhost_net = NULL;
    }

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    qemu_purge_queued_packets(nc);
}

static bool vhost_user_has_vnet_hdr(NetClientState *nc)
{
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);

    return true;
}

static bool vhost_user_has_ufo(NetClientState *nc)
{
    assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);

    return true;
}

static NetClientInfo net_vhost_user_info = {
162
        .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
163
        .size = sizeof(VhostUserState),
164
        .receive = vhost_user_receive,
165 166 167 168 169 170 171
        .cleanup = vhost_user_cleanup,
        .has_vnet_hdr = vhost_user_has_vnet_hdr,
        .has_ufo = vhost_user_has_ufo,
};

static void net_vhost_user_event(void *opaque, int event)
{
172 173 174 175 176
    const char *name = opaque;
    NetClientState *ncs[MAX_QUEUE_NUM];
    VhostUserState *s;
    Error *err = NULL;
    int queues;
177

178 179 180 181
    queues = qemu_find_net_clients_except(name, ncs,
                                          NET_CLIENT_OPTIONS_KIND_NIC,
                                          MAX_QUEUE_NUM);
    s = DO_UPCAST(VhostUserState, nc, ncs[0]);
182
    trace_vhost_user_event(s->chr->label, event);
183 184
    switch (event) {
    case CHR_EVENT_OPENED:
185 186 187 188
        if (vhost_user_start(queues, ncs) < 0) {
            exit(1);
        }
        qmp_set_link(name, true, &err);
189 190
        break;
    case CHR_EVENT_CLOSED:
191 192
        qmp_set_link(name, true, &err);
        vhost_user_stop(queues, ncs);
193 194
        break;
    }
195 196 197 198

    if (err) {
        error_report_err(err);
    }
199 200 201
}

static int net_vhost_user_init(NetClientState *peer, const char *device,
202 203
                               const char *name, CharDriverState *chr,
                               int queues)
204 205 206
{
    NetClientState *nc;
    VhostUserState *s;
207
    int i;
208

209 210
    for (i = 0; i < queues; i++) {
        nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
211

212 213
        snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
                 i, chr->label);
214

215
        nc->queue_index = i;
216

217 218 219
        s = DO_UPCAST(VhostUserState, nc, nc);
        s->chr = chr;
    }
220

221
    qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, (void*)name);
222 223 224 225

    return 0;
}

226 227 228
static int net_vhost_chardev_opts(void *opaque,
                                  const char *name, const char *value,
                                  Error **errp)
229 230 231 232 233 234 235 236 237 238
{
    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) {
        props->is_server = true;
    } else {
239 240 241
        error_setg(errp,
                   "vhost-user does not support a chardev with option %s=%s",
                   name, value);
242 243 244 245 246
        return -1;
    }
    return 0;
}

247 248
static CharDriverState *net_vhost_parse_chardev(
    const NetdevVhostUserOptions *opts, Error **errp)
249 250 251 252 253
{
    CharDriverState *chr = qemu_chr_find(opts->chardev);
    VhostUserChardevProps props;

    if (chr == NULL) {
254
        error_setg(errp, "chardev \"%s\" not found", opts->chardev);
255 256 257 258 259
        return NULL;
    }

    /* inspect chardev opts */
    memset(&props, 0, sizeof(props));
260
    if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
261 262 263 264
        return NULL;
    }

    if (!props.is_socket || !props.is_unix) {
265 266
        error_setg(errp, "chardev \"%s\" is not a unix socket",
                   opts->chardev);
267 268 269 270 271 272 273 274
        return NULL;
    }

    qemu_chr_fe_claim_no_fail(chr);

    return chr;
}

275
static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
276 277 278 279 280 281 282 283 284 285 286 287 288 289
{
    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) {
290
        error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
291 292 293 294 295 296
        return -1;
    }

    return 0;
}

297
int net_init_vhost_user(const NetClientOptions *opts, const char *name,
298
                        NetClientState *peer, Error **errp)
299
{
300
    int queues;
301 302 303
    const NetdevVhostUserOptions *vhost_user_opts;
    CharDriverState *chr;

E
Eric Blake 已提交
304 305
    assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
    vhost_user_opts = opts->u.vhost_user;
306

307
    chr = net_vhost_parse_chardev(vhost_user_opts, errp);
308 309 310 311 312 313
    if (!chr) {
        return -1;
    }

    /* verify net frontend */
    if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
314
                          (char *)name, errp)) {
315 316 317
        return -1;
    }

318
    queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
319

320
    return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
321
}