2 * QEMU vhost-user backend
4 * Copyright (C) 2018 Red Hat Inc
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/error-report.h"
19 #include "qom/object_interfaces.h"
20 #include "sysemu/vhost-user-backend.h"
21 #include "sysemu/kvm.h"
22 #include "io/channel-command.h"
23 #include "hw/virtio/virtio-bus.h"
26 ioeventfd_enabled(void)
28 return kvm_enabled() && kvm_eventfds_enabled();
32 vhost_user_backend_dev_init(VhostUserBackend
*b
, VirtIODevice
*vdev
,
33 unsigned nvqs
, Error
**errp
)
37 assert(!b
->vdev
&& vdev
);
39 if (!ioeventfd_enabled()) {
40 error_setg(errp
, "vhost initialization failed: requires kvm");
44 if (!vhost_user_init(&b
->vhost_user
, &b
->chr
, errp
)) {
50 b
->dev
.vqs
= g_new(struct vhost_virtqueue
, nvqs
);
52 ret
= vhost_dev_init(&b
->dev
, &b
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0);
54 error_setg_errno(errp
, -ret
, "vhost initialization failed");
62 vhost_user_backend_start(VhostUserBackend
*b
)
64 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(b
->vdev
)));
65 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
72 if (!k
->set_guest_notifiers
) {
73 error_report("binding does not support guest notifiers");
77 ret
= vhost_dev_enable_notifiers(&b
->dev
, b
->vdev
);
82 ret
= k
->set_guest_notifiers(qbus
->parent
, b
->dev
.nvqs
, true);
84 error_report("Error binding guest notifier");
85 goto err_host_notifiers
;
88 b
->dev
.acked_features
= b
->vdev
->guest_features
;
89 ret
= vhost_dev_start(&b
->dev
, b
->vdev
);
91 error_report("Error start vhost dev");
92 goto err_guest_notifiers
;
95 /* guest_notifier_mask/pending not used yet, so just unmask
96 * everything here. virtio-pci will do the right thing by
97 * enabling/disabling irqfd.
99 for (i
= 0; i
< b
->dev
.nvqs
; i
++) {
100 vhost_virtqueue_mask(&b
->dev
, b
->vdev
,
101 b
->dev
.vq_index
+ i
, false);
108 k
->set_guest_notifiers(qbus
->parent
, b
->dev
.nvqs
, false);
110 vhost_dev_disable_notifiers(&b
->dev
, b
->vdev
);
114 vhost_user_backend_stop(VhostUserBackend
*b
)
116 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(b
->vdev
)));
117 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
124 vhost_dev_stop(&b
->dev
, b
->vdev
);
126 if (k
->set_guest_notifiers
) {
127 ret
= k
->set_guest_notifiers(qbus
->parent
,
130 error_report("vhost guest notifier cleanup failed: %d", ret
);
135 vhost_dev_disable_notifiers(&b
->dev
, b
->vdev
);
139 static void set_chardev(Object
*obj
, const char *value
, Error
**errp
)
141 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
145 error_setg(errp
, QERR_PERMISSION_DENIED
);
150 b
->chr_name
= g_strdup(value
);
152 chr
= qemu_chr_find(b
->chr_name
);
154 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
155 "Chardev '%s' not found", b
->chr_name
);
159 if (!qemu_chr_fe_init(&b
->chr
, chr
, errp
)) {
164 /* could call vhost_dev_init() so early message can be exchanged */
167 static char *get_chardev(Object
*obj
, Error
**errp
)
169 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
170 Chardev
*chr
= qemu_chr_fe_get_driver(&b
->chr
);
172 if (chr
&& chr
->label
) {
173 return g_strdup(chr
->label
);
179 static void vhost_user_backend_init(Object
*obj
)
181 object_property_add_str(obj
, "chardev", get_chardev
, set_chardev
, NULL
);
184 static void vhost_user_backend_finalize(Object
*obj
)
186 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
191 vhost_user_cleanup(&b
->vhost_user
);
192 qemu_chr_fe_deinit(&b
->chr
, true);
195 static const TypeInfo vhost_user_backend_info
= {
196 .name
= TYPE_VHOST_USER_BACKEND
,
197 .parent
= TYPE_OBJECT
,
198 .instance_size
= sizeof(VhostUserBackend
),
199 .instance_init
= vhost_user_backend_init
,
200 .instance_finalize
= vhost_user_backend_finalize
,
201 .class_size
= sizeof(VhostUserBackendClass
),
204 static void register_types(void)
206 type_register_static(&vhost_user_backend_info
);
209 type_init(register_types
);