tests/qemu-iotests: Restrict tests using "--blockdev file" to the file protocol
[qemu/kevin.git] / backends / vhost-user.c
blob94c6a82d526da18a69ae0a49569eab5aeda07eda
1 /*
2 * QEMU vhost-user backend
4 * Copyright (C) 2018 Red Hat Inc
6 * Authors:
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"
23 int
24 vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
25 unsigned nvqs, Error **errp)
27 int ret;
29 assert(!b->vdev && vdev);
31 if (!vhost_user_init(&b->vhost_user, &b->chr, errp)) {
32 return -1;
35 b->vdev = vdev;
36 b->dev.nvqs = nvqs;
37 b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
39 ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
40 errp);
41 if (ret < 0) {
42 return -1;
45 return 0;
48 void
49 vhost_user_backend_start(VhostUserBackend *b)
51 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
52 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
53 int ret, i ;
55 if (b->started) {
56 return;
59 if (!k->set_guest_notifiers) {
60 error_report("binding does not support guest notifiers");
61 return;
64 ret = vhost_dev_enable_notifiers(&b->dev, b->vdev);
65 if (ret < 0) {
66 return;
69 ret = k->set_guest_notifiers(qbus->parent, b->dev.nvqs, true);
70 if (ret < 0) {
71 error_report("Error binding guest notifier");
72 goto err_host_notifiers;
75 b->dev.acked_features = b->vdev->guest_features;
76 ret = vhost_dev_start(&b->dev, b->vdev, true);
77 if (ret < 0) {
78 error_report("Error start vhost dev");
79 goto err_guest_notifiers;
82 /* guest_notifier_mask/pending not used yet, so just unmask
83 * everything here. virtio-pci will do the right thing by
84 * enabling/disabling irqfd.
86 for (i = 0; i < b->dev.nvqs; i++) {
87 vhost_virtqueue_mask(&b->dev, b->vdev,
88 b->dev.vq_index + i, false);
91 b->started = true;
92 return;
94 err_guest_notifiers:
95 k->set_guest_notifiers(qbus->parent, b->dev.nvqs, false);
96 err_host_notifiers:
97 vhost_dev_disable_notifiers(&b->dev, b->vdev);
100 void
101 vhost_user_backend_stop(VhostUserBackend *b)
103 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
104 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
105 int ret = 0;
107 if (!b->started) {
108 return;
111 vhost_dev_stop(&b->dev, b->vdev, true);
113 if (k->set_guest_notifiers) {
114 ret = k->set_guest_notifiers(qbus->parent,
115 b->dev.nvqs, false);
116 if (ret < 0) {
117 error_report("vhost guest notifier cleanup failed: %d", ret);
120 assert(ret >= 0);
122 vhost_dev_disable_notifiers(&b->dev, b->vdev);
123 b->started = false;
126 static void set_chardev(Object *obj, const char *value, Error **errp)
128 VhostUserBackend *b = VHOST_USER_BACKEND(obj);
129 Chardev *chr;
131 if (b->completed) {
132 error_setg(errp, "Property 'chardev' can no longer be set");
133 return;
136 g_free(b->chr_name);
137 b->chr_name = g_strdup(value);
139 chr = qemu_chr_find(b->chr_name);
140 if (chr == NULL) {
141 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
142 "Chardev '%s' not found", b->chr_name);
143 return;
146 if (!qemu_chr_fe_init(&b->chr, chr, errp)) {
147 return;
150 b->completed = true;
151 /* could call vhost_dev_init() so early message can be exchanged */
154 static char *get_chardev(Object *obj, Error **errp)
156 VhostUserBackend *b = VHOST_USER_BACKEND(obj);
157 Chardev *chr = qemu_chr_fe_get_driver(&b->chr);
159 if (chr && chr->label) {
160 return g_strdup(chr->label);
163 return NULL;
166 static void vhost_user_backend_class_init(ObjectClass *oc, void *data)
168 object_class_property_add_str(oc, "chardev", get_chardev, set_chardev);
171 static void vhost_user_backend_finalize(Object *obj)
173 VhostUserBackend *b = VHOST_USER_BACKEND(obj);
175 g_free(b->dev.vqs);
176 g_free(b->chr_name);
178 vhost_user_cleanup(&b->vhost_user);
179 qemu_chr_fe_deinit(&b->chr, true);
182 static const TypeInfo vhost_user_backend_info = {
183 .name = TYPE_VHOST_USER_BACKEND,
184 .parent = TYPE_OBJECT,
185 .instance_size = sizeof(VhostUserBackend),
186 .class_init = vhost_user_backend_class_init,
187 .instance_finalize = vhost_user_backend_finalize,
190 static void register_types(void)
192 type_register_static(&vhost_user_backend_info);
195 type_init(register_types);