master
c 458 lines 12.7 KB
Raw
1 /*
2 * Vhost-user filesystem virtio device
3 *
4 * Copyright 2018-2019 Red Hat, Inc.
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15 #include <sys/ioctl.h>
16 #include "standard-headers/linux/virtio_fs.h"
17 #include "qapi/error.h"
18 #include "hw/core/qdev-properties.h"
19 #include "hw/core/qdev-properties-system.h"
20 #include "hw/virtio/virtio-bus.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "qemu/error-report.h"
23 #include "hw/virtio/vhost.h"
24 #include "hw/virtio/vhost-user-fs.h"
25 #include "monitor/monitor.h"
26 #include "system/system.h"
27
28 static const int user_feature_bits[] = {
29 VIRTIO_F_VERSION_1,
30 VIRTIO_RING_F_INDIRECT_DESC,
31 VIRTIO_RING_F_EVENT_IDX,
32 VIRTIO_F_NOTIFY_ON_EMPTY,
33 VIRTIO_F_RING_PACKED,
34 VIRTIO_F_IOMMU_PLATFORM,
35 VIRTIO_F_RING_RESET,
36 VIRTIO_F_IN_ORDER,
37 VIRTIO_F_NOTIFICATION_DATA,
38 VHOST_INVALID_FEATURE_BIT
39 };
40
41 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config)
42 {
43 VHostUserFS *fs = VHOST_USER_FS(vdev);
44 struct virtio_fs_config fscfg = {};
45
46 memcpy((char *)fscfg.tag, fs->conf.tag,
47 MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag)));
48
49 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues);
50
51 memcpy(config, &fscfg, sizeof(fscfg));
52 }
53
54 static void vuf_start(VirtIODevice *vdev)
55 {
56 VHostUserFS *fs = VHOST_USER_FS(vdev);
57 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
58 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
59 int ret;
60 int i;
61
62 if (!k->set_guest_notifiers) {
63 error_report("binding does not support guest notifiers");
64 return;
65 }
66
67 ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev);
68 if (ret < 0) {
69 error_report("Error enabling host notifiers: %d", -ret);
70 return;
71 }
72
73 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true);
74 if (ret < 0) {
75 error_report("Error binding guest notifier: %d", -ret);
76 goto err_host_notifiers;
77 }
78
79 fs->vhost_dev.acked_features = vdev->guest_features;
80 ret = vhost_dev_start(&fs->vhost_dev, vdev, true);
81 if (ret < 0) {
82 error_report("Error starting vhost: %d", -ret);
83 goto err_guest_notifiers;
84 }
85
86 /*
87 * guest_notifier_mask/pending not used yet, so just unmask
88 * everything here. virtio-pci will do the right thing by
89 * enabling/disabling irqfd.
90 */
91 for (i = 0; i < fs->vhost_dev.nvqs; i++) {
92 vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false);
93 }
94
95 return;
96
97 err_guest_notifiers:
98 k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
99 err_host_notifiers:
100 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
101 }
102
103 static int vuf_stop(VirtIODevice *vdev)
104 {
105 VHostUserFS *fs = VHOST_USER_FS(vdev);
106 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
107 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
108 int ret, err;
109
110 if (!k->set_guest_notifiers) {
111 return 0;
112 }
113
114 ret = vhost_dev_stop(&fs->vhost_dev, vdev, true);
115
116 err = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
117 if (err < 0) {
118 error_report("vhost guest notifier cleanup failed: %d", err);
119 return err;
120 }
121
122 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
123 return ret;
124 }
125
126 static int vuf_set_status(VirtIODevice *vdev, uint8_t status)
127 {
128 VHostUserFS *fs = VHOST_USER_FS(vdev);
129 bool should_start = virtio_device_should_start(vdev, status);
130
131 if (vhost_dev_is_started(&fs->vhost_dev) == should_start) {
132 return 0;
133 }
134
135 if (should_start) {
136 vuf_start(vdev);
137 } else {
138 int ret;
139 ret = vuf_stop(vdev);
140 if (ret < 0) {
141 return ret;
142 }
143 }
144 return 0;
145 }
146
147 static uint64_t vuf_get_features(VirtIODevice *vdev,
148 uint64_t features,
149 Error **errp)
150 {
151 VHostUserFS *fs = VHOST_USER_FS(vdev);
152
153 return vhost_get_features(&fs->vhost_dev, user_feature_bits, features);
154 }
155
156 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
157 {
158 /*
159 * Not normally called; it's the daemon that handles the queue;
160 * however virtio's cleanup path can call this.
161 */
162 }
163
164 static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
165 bool mask)
166 {
167 VHostUserFS *fs = VHOST_USER_FS(vdev);
168
169 /*
170 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
171 * as the macro of configure interrupt's IDX, If this driver does not
172 * support, the function will return
173 */
174
175 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
176 return;
177 }
178 vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
179 }
180
181 static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
182 {
183 VHostUserFS *fs = VHOST_USER_FS(vdev);
184
185 /*
186 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
187 * as the macro of configure interrupt's IDX, If this driver does not
188 * support, the function will return
189 */
190
191 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
192 return false;
193 }
194 return vhost_virtqueue_pending(&fs->vhost_dev, idx);
195 }
196
197 static void vuf_device_realize(DeviceState *dev, Error **errp)
198 {
199 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
200 VHostUserFS *fs = VHOST_USER_FS(dev);
201 unsigned int i;
202 size_t len;
203 int ret;
204
205 if (!fs->conf.chardev.chr) {
206 error_setg(errp, "missing chardev");
207 return;
208 }
209
210 if (!fs->conf.tag) {
211 error_setg(errp, "missing tag property");
212 return;
213 }
214 len = strlen(fs->conf.tag);
215 if (len == 0) {
216 error_setg(errp, "tag property cannot be empty");
217 return;
218 }
219 if (len > sizeof_field(struct virtio_fs_config, tag)) {
220 error_setg(errp, "tag property must be %zu bytes or less",
221 sizeof_field(struct virtio_fs_config, tag));
222 return;
223 }
224
225 if (fs->conf.num_request_queues == 0) {
226 error_setg(errp, "num-request-queues property must be larger than 0");
227 return;
228 }
229
230 if (!is_power_of_2(fs->conf.queue_size)) {
231 error_setg(errp, "queue-size property must be a power of 2");
232 return;
233 }
234
235 if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) {
236 error_setg(errp, "queue-size property must be %u or smaller",
237 VIRTQUEUE_MAX_SIZE);
238 return;
239 }
240
241 if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) {
242 return;
243 }
244
245 virtio_init(vdev, VIRTIO_ID_FS, sizeof(struct virtio_fs_config));
246
247 /* Hiprio queue */
248 fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
249
250 /* Request queues */
251 fs->req_vqs = g_new(VirtQueue *, fs->conf.num_request_queues);
252 for (i = 0; i < fs->conf.num_request_queues; i++) {
253 fs->req_vqs[i] = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
254 }
255
256 /* 1 high prio queue, plus the number configured */
257 fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues;
258 fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs);
259 ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user,
260 VHOST_BACKEND_TYPE_USER, 0, errp);
261 if (ret < 0) {
262 goto err_virtio;
263 }
264
265 return;
266
267 err_virtio:
268 vhost_user_cleanup(&fs->vhost_user);
269 virtio_delete_queue(fs->hiprio_vq);
270 for (i = 0; i < fs->conf.num_request_queues; i++) {
271 virtio_delete_queue(fs->req_vqs[i]);
272 }
273 g_free(fs->req_vqs);
274 virtio_cleanup(vdev);
275 g_free(fs->vhost_dev.vqs);
276 }
277
278 static void vuf_device_unrealize(DeviceState *dev)
279 {
280 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
281 VHostUserFS *fs = VHOST_USER_FS(dev);
282 struct vhost_virtqueue *vhost_vqs = fs->vhost_dev.vqs;
283 int i;
284
285 /* This will stop vhost backend if appropriate. */
286 vuf_set_status(vdev, 0);
287
288 vhost_dev_cleanup(&fs->vhost_dev);
289
290 vhost_user_cleanup(&fs->vhost_user);
291
292 virtio_delete_queue(fs->hiprio_vq);
293 for (i = 0; i < fs->conf.num_request_queues; i++) {
294 virtio_delete_queue(fs->req_vqs[i]);
295 }
296 g_free(fs->req_vqs);
297 virtio_cleanup(vdev);
298 g_free(vhost_vqs);
299 }
300
301 static struct vhost_dev *vuf_get_vhost(VirtIODevice *vdev)
302 {
303 VHostUserFS *fs = VHOST_USER_FS(vdev);
304 return &fs->vhost_dev;
305 }
306
307 /**
308 * Fetch the internal state from virtiofsd and save it to `f`.
309 */
310 static int vuf_save_state(QEMUFile *f, void *pv, size_t size,
311 const VMStateField *field, JSONWriter *vmdesc)
312 {
313 VirtIODevice *vdev = pv;
314 VHostUserFS *fs = VHOST_USER_FS(vdev);
315 Error *local_error = NULL;
316 int ret;
317
318 ret = vhost_save_backend_state(&fs->vhost_dev, f, &local_error);
319 if (ret < 0) {
320 error_reportf_err(local_error,
321 "Error saving back-end state of %s device %s "
322 "(tag: \"%s\"): ",
323 vdev->name, vdev->parent_obj.canonical_path,
324 fs->conf.tag ?: "<none>");
325 return ret;
326 }
327
328 return 0;
329 }
330
331 /**
332 * Load virtiofsd's internal state from `f` and send it over to virtiofsd.
333 */
334 static int vuf_load_state(QEMUFile *f, void *pv, size_t size,
335 const VMStateField *field)
336 {
337 VirtIODevice *vdev = pv;
338 VHostUserFS *fs = VHOST_USER_FS(vdev);
339 Error *local_error = NULL;
340 int ret;
341
342 ret = vhost_load_backend_state(&fs->vhost_dev, f, &local_error);
343 if (ret < 0) {
344 error_reportf_err(local_error,
345 "Error loading back-end state of %s device %s "
346 "(tag: \"%s\"): ",
347 vdev->name, vdev->parent_obj.canonical_path,
348 fs->conf.tag ?: "<none>");
349 return ret;
350 }
351
352 return 0;
353 }
354
355 static bool vuf_is_internal_migration(void *opaque)
356 {
357 /* TODO: Return false when an external migration is requested */
358 return true;
359 }
360
361 static int vuf_check_migration_support(void *opaque)
362 {
363 VirtIODevice *vdev = opaque;
364 VHostUserFS *fs = VHOST_USER_FS(vdev);
365
366 if (!vhost_supports_device_state(&fs->vhost_dev)) {
367 error_report("Back-end of %s device %s (tag: \"%s\") does not support "
368 "migration through qemu",
369 vdev->name, vdev->parent_obj.canonical_path,
370 fs->conf.tag ?: "<none>");
371 return -ENOTSUP;
372 }
373
374 return 0;
375 }
376
377 static const VMStateDescription vuf_backend_vmstate;
378
379 static const VMStateDescription vuf_vmstate = {
380 .name = "vhost-user-fs",
381 .version_id = 0,
382 .fields = (const VMStateField[]) {
383 VMSTATE_VIRTIO_DEVICE,
384 VMSTATE_END_OF_LIST()
385 },
386 .subsections = (const VMStateDescription * const []) {
387 &vuf_backend_vmstate,
388 NULL,
389 }
390 };
391
392 static const VMStateDescription vuf_backend_vmstate = {
393 .name = "vhost-user-fs-backend",
394 .version_id = 0,
395 .needed = vuf_is_internal_migration,
396 .pre_load = vuf_check_migration_support,
397 .pre_save = vuf_check_migration_support,
398 .fields = (const VMStateField[]) {
399 {
400 .name = "back-end",
401 .info = &(const VMStateInfo) {
402 .name = "virtio-fs back-end state",
403 .get = vuf_load_state,
404 .put = vuf_save_state,
405 },
406 },
407 VMSTATE_END_OF_LIST()
408 },
409 };
410
411 static const Property vuf_properties[] = {
412 DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev),
413 DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag),
414 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS,
415 conf.num_request_queues, 1),
416 DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128),
417 };
418
419 static void vuf_instance_init(Object *obj)
420 {
421 VHostUserFS *fs = VHOST_USER_FS(obj);
422
423 device_add_bootindex_property(obj, &fs->bootindex, "bootindex",
424 "/filesystem@0", DEVICE(obj));
425 }
426
427 static void vuf_class_init(ObjectClass *klass, const void *data)
428 {
429 DeviceClass *dc = DEVICE_CLASS(klass);
430 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
431
432 device_class_set_props(dc, vuf_properties);
433 dc->vmsd = &vuf_vmstate;
434 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
435 vdc->realize = vuf_device_realize;
436 vdc->unrealize = vuf_device_unrealize;
437 vdc->get_features = vuf_get_features;
438 vdc->get_config = vuf_get_config;
439 vdc->set_status = vuf_set_status;
440 vdc->guest_notifier_mask = vuf_guest_notifier_mask;
441 vdc->guest_notifier_pending = vuf_guest_notifier_pending;
442 vdc->get_vhost = vuf_get_vhost;
443 }
444
445 static const TypeInfo vuf_info = {
446 .name = TYPE_VHOST_USER_FS,
447 .parent = TYPE_VIRTIO_DEVICE,
448 .instance_size = sizeof(VHostUserFS),
449 .instance_init = vuf_instance_init,
450 .class_init = vuf_class_init,
451 };
452
453 static void vuf_register_types(void)
454 {
455 type_register_static(&vuf_info);
456 }
457
458 type_init(vuf_register_types)