c1bf3531ae
"name" is freed after visiting options, instead use the first NetClientState name. Adds a few assert() for clarifying and checking some impossible states. READ of size 1 at 0x602000000990 thread T0 #0 0x7f6b251c570c (/lib64/libasan.so.2+0x4770c) #1 0x5566dc380600 in qemu_find_net_clients_except net/net.c:824 #2 0x5566dc39bac7 in net_vhost_user_event net/vhost-user.c:193 #3 0x5566dbee862a in qemu_chr_be_event /home/elmarco/src/qemu/qemu-char.c:201 #4 0x5566dbef2890 in tcp_chr_disconnect /home/elmarco/src/qemu/qemu-char.c:2790 #5 0x5566dbef2d0b in tcp_chr_sync_read /home/elmarco/src/qemu/qemu-char.c:2835 #6 0x5566dbee8a99 in qemu_chr_fe_read_all /home/elmarco/src/qemu/qemu-char.c:295 #7 0x5566dc39b964 in net_vhost_user_watch net/vhost-user.c:180 #8 0x5566dc5a06c7 in qio_channel_fd_source_dispatch io/channel-watch.c:70 #9 0x7f6b1aa2ab87 in g_main_dispatch /home/elmarco/src/gnome/glib/glib/gmain.c:3154 #10 0x7f6b1aa2b9cb in g_main_context_dispatch /home/elmarco/src/gnome/glib/glib/gmain.c:3769 #11 0x5566dc475ed4 in glib_pollfds_poll /home/elmarco/src/qemu/main-loop.c:212 #12 0x5566dc476029 in os_host_main_loop_wait /home/elmarco/src/qemu/main-loop.c:257 #13 0x5566dc476165 in main_loop_wait /home/elmarco/src/qemu/main-loop.c:505 #14 0x5566dbf08d31 in main_loop /home/elmarco/src/qemu/vl.c:1932 #15 0x5566dbf16783 in main /home/elmarco/src/qemu/vl.c:4646 #16 0x7f6b180bb57f in __libc_start_main (/lib64/libc.so.6+0x2057f) #17 0x5566dbbf5348 in _start (/home/elmarco/src/qemu/x86_64-softmmu/qemu-system-x86_64+0x3f9348) 0x602000000990 is located 0 bytes inside of 5-byte region [0x602000000990,0x602000000995) freed by thread T0 here: #0 0x7f6b2521666a in __interceptor_free (/lib64/libasan.so.2+0x9866a) #1 0x7f6b1aa332a4 in g_free /home/elmarco/src/gnome/glib/glib/gmem.c:189 #2 0x5566dc5f416f in qapi_dealloc_type_str qapi/qapi-dealloc-visitor.c:134 #3 0x5566dc5f3268 in visit_type_str qapi/qapi-visit-core.c:196 #4 0x5566dc5ced58 in visit_type_Netdev_fields /home/elmarco/src/qemu/qapi-visit.c:5936 #5 0x5566dc5cef71 in visit_type_Netdev /home/elmarco/src/qemu/qapi-visit.c:5960 #6 0x5566dc381a8d in net_visit net/net.c:1049 #7 0x5566dc381c37 in net_client_init net/net.c:1076 #8 0x5566dc3839e2 in net_init_netdev net/net.c:1473 #9 0x5566dc63cc0a in qemu_opts_foreach util/qemu-option.c:1112 #10 0x5566dc383b36 in net_init_clients net/net.c:1499 #11 0x5566dbf15d86 in main /home/elmarco/src/qemu/vl.c:4397 #12 0x7f6b180bb57f in __libc_start_main (/lib64/libc.so.6+0x2057f) Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
334 lines
8.6 KiB
C
334 lines
8.6 KiB
C
/*
|
|
* 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 "qemu/osdep.h"
|
|
#include "clients.h"
|
|
#include "net/vhost_net.h"
|
|
#include "net/vhost-user.h"
|
|
#include "sysemu/char.h"
|
|
#include "qemu/config-file.h"
|
|
#include "qemu/error-report.h"
|
|
#include "qmp-commands.h"
|
|
#include "trace.h"
|
|
|
|
typedef struct VhostUserState {
|
|
NetClientState nc;
|
|
CharDriverState *chr;
|
|
VHostNetState *vhost_net;
|
|
} VhostUserState;
|
|
|
|
typedef struct VhostUserChardevProps {
|
|
bool is_socket;
|
|
bool is_unix;
|
|
bool is_server;
|
|
} VhostUserChardevProps;
|
|
|
|
VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
|
|
{
|
|
VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
|
|
assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
|
|
return s->vhost_net;
|
|
}
|
|
|
|
static int vhost_user_running(VhostUserState *s)
|
|
{
|
|
return (s->vhost_net) ? 1 : 0;
|
|
}
|
|
|
|
static void vhost_user_stop(int queues, NetClientState *ncs[])
|
|
{
|
|
VhostUserState *s;
|
|
int i;
|
|
|
|
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;
|
|
}
|
|
|
|
if (s->vhost_net) {
|
|
vhost_net_cleanup(s->vhost_net);
|
|
s->vhost_net = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int vhost_user_start(int queues, NetClientState *ncs[])
|
|
{
|
|
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", 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",
|
|
max_queues);
|
|
goto err;
|
|
}
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
|
|
err:
|
|
vhost_user_stop(i + 1, ncs);
|
|
return -1;
|
|
}
|
|
|
|
static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
|
|
size_t size)
|
|
{
|
|
/* 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.
|
|
*/
|
|
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;
|
|
}
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
static void vhost_user_cleanup(NetClientState *nc)
|
|
{
|
|
VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
|
|
|
|
if (s->vhost_net) {
|
|
vhost_net_cleanup(s->vhost_net);
|
|
s->vhost_net = NULL;
|
|
}
|
|
|
|
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 = {
|
|
.type = NET_CLIENT_OPTIONS_KIND_VHOST_USER,
|
|
.size = sizeof(VhostUserState),
|
|
.receive = vhost_user_receive,
|
|
.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)
|
|
{
|
|
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_OPTIONS_KIND_NIC,
|
|
MAX_QUEUE_NUM);
|
|
assert(queues < MAX_QUEUE_NUM);
|
|
|
|
s = DO_UPCAST(VhostUserState, nc, ncs[0]);
|
|
trace_vhost_user_event(s->chr->label, event);
|
|
switch (event) {
|
|
case CHR_EVENT_OPENED:
|
|
if (vhost_user_start(queues, ncs) < 0) {
|
|
exit(1);
|
|
}
|
|
qmp_set_link(name, true, &err);
|
|
break;
|
|
case CHR_EVENT_CLOSED:
|
|
qmp_set_link(name, false, &err);
|
|
vhost_user_stop(queues, ncs);
|
|
break;
|
|
}
|
|
|
|
if (err) {
|
|
error_report_err(err);
|
|
}
|
|
}
|
|
|
|
static int net_vhost_user_init(NetClientState *peer, const char *device,
|
|
const char *name, CharDriverState *chr,
|
|
int queues)
|
|
{
|
|
NetClientState *nc;
|
|
VhostUserState *s;
|
|
int i;
|
|
|
|
assert(name);
|
|
assert(queues > 0);
|
|
|
|
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;
|
|
|
|
s = DO_UPCAST(VhostUserState, nc, nc);
|
|
s->chr = chr;
|
|
}
|
|
|
|
qemu_chr_add_handlers(chr, NULL, NULL, net_vhost_user_event, nc[0].name);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int net_vhost_chardev_opts(void *opaque,
|
|
const char *name, const char *value,
|
|
Error **errp)
|
|
{
|
|
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 {
|
|
error_setg(errp,
|
|
"vhost-user does not support a chardev with option %s=%s",
|
|
name, value);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static CharDriverState *net_vhost_parse_chardev(
|
|
const NetdevVhostUserOptions *opts, Error **errp)
|
|
{
|
|
CharDriverState *chr = qemu_chr_find(opts->chardev);
|
|
VhostUserChardevProps props;
|
|
|
|
if (chr == NULL) {
|
|
error_setg(errp, "chardev \"%s\" not found", opts->chardev);
|
|
return NULL;
|
|
}
|
|
|
|
/* inspect chardev opts */
|
|
memset(&props, 0, sizeof(props));
|
|
if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
|
|
return NULL;
|
|
}
|
|
|
|
if (!props.is_socket || !props.is_unix) {
|
|
error_setg(errp, "chardev \"%s\" is not a unix socket",
|
|
opts->chardev);
|
|
return NULL;
|
|
}
|
|
|
|
qemu_chr_fe_claim_no_fail(chr);
|
|
|
|
return chr;
|
|
}
|
|
|
|
static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
|
|
{
|
|
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) {
|
|
error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int net_init_vhost_user(const NetClientOptions *opts, const char *name,
|
|
NetClientState *peer, Error **errp)
|
|
{
|
|
int queues;
|
|
const NetdevVhostUserOptions *vhost_user_opts;
|
|
CharDriverState *chr;
|
|
|
|
assert(opts->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER);
|
|
vhost_user_opts = opts->u.vhost_user;
|
|
|
|
chr = net_vhost_parse_chardev(vhost_user_opts, errp);
|
|
if (!chr) {
|
|
return -1;
|
|
}
|
|
|
|
/* verify net frontend */
|
|
if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
|
|
(char *)name, errp)) {
|
|
return -1;
|
|
}
|
|
|
|
queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
|
|
if (queues < 1 || queues > MAX_QUEUE_NUM) {
|
|
error_setg(errp,
|
|
"vhost-user number of queues must be in range [1, %d]",
|
|
MAX_QUEUE_NUM);
|
|
return -1;
|
|
}
|
|
|
|
return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
|
|
}
|