xref: /openbmc/qemu/backends/vhost-user.c (revision f0984d40)
1 /*
2  * QEMU vhost-user backend
3  *
4  * Copyright (C) 2018 Red Hat Inc
5  *
6  * Authors:
7  *  Marc-André Lureau <marcandre.lureau@redhat.com>
8  *
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.
11  */
12 
13 
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"
22 
23 static bool
24 ioeventfd_enabled(void)
25 {
26     return kvm_enabled() && kvm_eventfds_enabled();
27 }
28 
29 int
30 vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
31                             unsigned nvqs, Error **errp)
32 {
33     int ret;
34 
35     assert(!b->vdev && vdev);
36 
37     if (!ioeventfd_enabled()) {
38         error_setg(errp, "vhost initialization failed: requires kvm");
39         return -1;
40     }
41 
42     if (!vhost_user_init(&b->vhost_user, &b->chr, errp)) {
43         return -1;
44     }
45 
46     b->vdev = vdev;
47     b->dev.nvqs = nvqs;
48     b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
49 
50     ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
51                          errp);
52     if (ret < 0) {
53         return -1;
54     }
55 
56     return 0;
57 }
58 
59 void
60 vhost_user_backend_start(VhostUserBackend *b)
61 {
62     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
63     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
64     int ret, i ;
65 
66     if (b->started) {
67         return;
68     }
69 
70     if (!k->set_guest_notifiers) {
71         error_report("binding does not support guest notifiers");
72         return;
73     }
74 
75     ret = vhost_dev_enable_notifiers(&b->dev, b->vdev);
76     if (ret < 0) {
77         return;
78     }
79 
80     ret = k->set_guest_notifiers(qbus->parent, b->dev.nvqs, true);
81     if (ret < 0) {
82         error_report("Error binding guest notifier");
83         goto err_host_notifiers;
84     }
85 
86     b->dev.acked_features = b->vdev->guest_features;
87     ret = vhost_dev_start(&b->dev, b->vdev, true);
88     if (ret < 0) {
89         error_report("Error start vhost dev");
90         goto err_guest_notifiers;
91     }
92 
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.
96      */
97     for (i = 0; i < b->dev.nvqs; i++) {
98         vhost_virtqueue_mask(&b->dev, b->vdev,
99                              b->dev.vq_index + i, false);
100     }
101 
102     b->started = true;
103     return;
104 
105 err_guest_notifiers:
106     k->set_guest_notifiers(qbus->parent, b->dev.nvqs, false);
107 err_host_notifiers:
108     vhost_dev_disable_notifiers(&b->dev, b->vdev);
109 }
110 
111 void
112 vhost_user_backend_stop(VhostUserBackend *b)
113 {
114     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
115     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
116     int ret = 0;
117 
118     if (!b->started) {
119         return;
120     }
121 
122     vhost_dev_stop(&b->dev, b->vdev, true);
123 
124     if (k->set_guest_notifiers) {
125         ret = k->set_guest_notifiers(qbus->parent,
126                                      b->dev.nvqs, false);
127         if (ret < 0) {
128             error_report("vhost guest notifier cleanup failed: %d", ret);
129         }
130     }
131     assert(ret >= 0);
132 
133     vhost_dev_disable_notifiers(&b->dev, b->vdev);
134     b->started = false;
135 }
136 
137 static void set_chardev(Object *obj, const char *value, Error **errp)
138 {
139     VhostUserBackend *b = VHOST_USER_BACKEND(obj);
140     Chardev *chr;
141 
142     if (b->completed) {
143         error_setg(errp, "Property 'chardev' can no longer be set");
144         return;
145     }
146 
147     g_free(b->chr_name);
148     b->chr_name = g_strdup(value);
149 
150     chr = qemu_chr_find(b->chr_name);
151     if (chr == NULL) {
152         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
153                   "Chardev '%s' not found", b->chr_name);
154         return;
155     }
156 
157     if (!qemu_chr_fe_init(&b->chr, chr, errp)) {
158         return;
159     }
160 
161     b->completed = true;
162     /* could call vhost_dev_init() so early message can be exchanged */
163 }
164 
165 static char *get_chardev(Object *obj, Error **errp)
166 {
167     VhostUserBackend *b = VHOST_USER_BACKEND(obj);
168     Chardev *chr = qemu_chr_fe_get_driver(&b->chr);
169 
170     if (chr && chr->label) {
171         return g_strdup(chr->label);
172     }
173 
174     return NULL;
175 }
176 
177 static void vhost_user_backend_class_init(ObjectClass *oc, void *data)
178 {
179     object_class_property_add_str(oc, "chardev", get_chardev, set_chardev);
180 }
181 
182 static void vhost_user_backend_finalize(Object *obj)
183 {
184     VhostUserBackend *b = VHOST_USER_BACKEND(obj);
185 
186     g_free(b->dev.vqs);
187     g_free(b->chr_name);
188 
189     vhost_user_cleanup(&b->vhost_user);
190     qemu_chr_fe_deinit(&b->chr, true);
191 }
192 
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,
199 };
200 
201 static void register_types(void)
202 {
203     type_register_static(&vhost_user_backend_info);
204 }
205 
206 type_init(register_types);
207