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"
15 #include "qapi/error.h"
16 #include "qemu/error-report.h"
17 #include "qom/object_interfaces.h"
18 #include "sysemu/vhost-user-backend.h"
19 #include "sysemu/kvm.h"
20 #include "io/channel-command.h"
21 #include "hw/virtio/virtio-bus.h"
24 ioeventfd_enabled(void)
26 return kvm_enabled() && kvm_eventfds_enabled();
30 vhost_user_backend_dev_init(VhostUserBackend
*b
, VirtIODevice
*vdev
,
31 unsigned nvqs
, Error
**errp
)
35 assert(!b
->vdev
&& vdev
);
37 if (!ioeventfd_enabled()) {
38 error_setg(errp
, "vhost initialization failed: requires kvm");
42 if (!vhost_user_init(&b
->vhost_user
, &b
->chr
, errp
)) {
48 b
->dev
.vqs
= g_new0(struct vhost_virtqueue
, nvqs
);
50 ret
= vhost_dev_init(&b
->dev
, &b
->vhost_user
, VHOST_BACKEND_TYPE_USER
, 0,
60 vhost_user_backend_start(VhostUserBackend
*b
)
62 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(b
->vdev
)));
63 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
70 if (!k
->set_guest_notifiers
) {
71 error_report("binding does not support guest notifiers");
75 ret
= vhost_dev_enable_notifiers(&b
->dev
, b
->vdev
);
80 ret
= k
->set_guest_notifiers(qbus
->parent
, b
->dev
.nvqs
, true);
82 error_report("Error binding guest notifier");
83 goto err_host_notifiers
;
86 b
->dev
.acked_features
= b
->vdev
->guest_features
;
87 ret
= vhost_dev_start(&b
->dev
, b
->vdev
, true);
89 error_report("Error start vhost dev");
90 goto err_guest_notifiers
;
93 /* guest_notifier_mask/pending not used yet, so just unmask
94 * everything here. virtio-pci will do the right thing by
95 * enabling/disabling irqfd.
97 for (i
= 0; i
< b
->dev
.nvqs
; i
++) {
98 vhost_virtqueue_mask(&b
->dev
, b
->vdev
,
99 b
->dev
.vq_index
+ i
, false);
106 k
->set_guest_notifiers(qbus
->parent
, b
->dev
.nvqs
, false);
108 vhost_dev_disable_notifiers(&b
->dev
, b
->vdev
);
112 vhost_user_backend_stop(VhostUserBackend
*b
)
114 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(b
->vdev
)));
115 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
122 vhost_dev_stop(&b
->dev
, b
->vdev
, true);
124 if (k
->set_guest_notifiers
) {
125 ret
= k
->set_guest_notifiers(qbus
->parent
,
128 error_report("vhost guest notifier cleanup failed: %d", ret
);
133 vhost_dev_disable_notifiers(&b
->dev
, b
->vdev
);
137 static void set_chardev(Object
*obj
, const char *value
, Error
**errp
)
139 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
143 error_setg(errp
, "Property 'chardev' can no longer be set");
148 b
->chr_name
= g_strdup(value
);
150 chr
= qemu_chr_find(b
->chr_name
);
152 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
153 "Chardev '%s' not found", b
->chr_name
);
157 if (!qemu_chr_fe_init(&b
->chr
, chr
, errp
)) {
162 /* could call vhost_dev_init() so early message can be exchanged */
165 static char *get_chardev(Object
*obj
, Error
**errp
)
167 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
168 Chardev
*chr
= qemu_chr_fe_get_driver(&b
->chr
);
170 if (chr
&& chr
->label
) {
171 return g_strdup(chr
->label
);
177 static void vhost_user_backend_class_init(ObjectClass
*oc
, void *data
)
179 object_class_property_add_str(oc
, "chardev", get_chardev
, set_chardev
);
182 static void vhost_user_backend_finalize(Object
*obj
)
184 VhostUserBackend
*b
= VHOST_USER_BACKEND(obj
);
189 vhost_user_cleanup(&b
->vhost_user
);
190 qemu_chr_fe_deinit(&b
->chr
, true);
193 static const TypeInfo vhost_user_backend_info
= {
194 .name
= TYPE_VHOST_USER_BACKEND
,
195 .parent
= TYPE_OBJECT
,
196 .instance_size
= sizeof(VhostUserBackend
),
197 .class_init
= vhost_user_backend_class_init
,
198 .instance_finalize
= vhost_user_backend_finalize
,
201 static void register_types(void)
203 type_register_static(&vhost_user_backend_info
);
206 type_init(register_types
);