1 /*
2 * vhost-user-blk host device
3 *
4 * Copyright(C) 2017 Intel Corporation.
5 *
6 * Authors:
7 * Changpeng Liu <changpeng.liu@intel.com>
8 *
9 * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by:
10 * Felipe Franciosi <felipe@nutanix.com>
11 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12 * Nicholas Bellinger <nab@risingtidesystems.com>
13 *
14 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
15 * See the COPYING.LIB file in the top-level directory.
16 *
17 */
18
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/cutils.h"
23 #include "hw/qdev-core.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/qdev-properties-system.h"
26 #include "hw/virtio/virtio-blk-common.h"
27 #include "hw/virtio/vhost.h"
28 #include "hw/virtio/vhost-user-blk.h"
29 #include "hw/virtio/virtio.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
32 #include "system/system.h"
33 #include "system/runstate.h"
34
35 static const int user_feature_bits[] = {
36 VIRTIO_BLK_F_SIZE_MAX,
37 VIRTIO_BLK_F_SEG_MAX,
38 VIRTIO_BLK_F_GEOMETRY,
39 VIRTIO_BLK_F_BLK_SIZE,
40 VIRTIO_BLK_F_TOPOLOGY,
41 VIRTIO_BLK_F_MQ,
42 VIRTIO_BLK_F_RO,
43 VIRTIO_BLK_F_FLUSH,
44 VIRTIO_BLK_F_CONFIG_WCE,
45 VIRTIO_BLK_F_DISCARD,
46 VIRTIO_BLK_F_WRITE_ZEROES,
47 VIRTIO_F_VERSION_1,
48 VIRTIO_RING_F_INDIRECT_DESC,
49 VIRTIO_RING_F_EVENT_IDX,
50 VIRTIO_F_NOTIFY_ON_EMPTY,
51 VIRTIO_F_RING_PACKED,
52 VIRTIO_F_IOMMU_PLATFORM,
53 VIRTIO_F_RING_RESET,
54 VIRTIO_F_IN_ORDER,
55 VIRTIO_F_NOTIFICATION_DATA,
56 VHOST_INVALID_FEATURE_BIT
57 };
58
59 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event);
60
vhost_user_blk_update_config(VirtIODevice * vdev,uint8_t * config)61 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
62 {
63 VHostUserBlk *s = VHOST_USER_BLK(vdev);
64
65 /* Our num_queues overrides the device backend */
66 virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues);
67
68 memcpy(config, &s->blkcfg, vdev->config_len);
69 }
70
vhost_user_blk_set_config(VirtIODevice * vdev,const uint8_t * config)71 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
72 {
73 VHostUserBlk *s = VHOST_USER_BLK(vdev);
74 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
75 int ret;
76
77 if (blkcfg->wce == s->blkcfg.wce) {
78 return;
79 }
80
81 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
82 offsetof(struct virtio_blk_config, wce),
83 sizeof(blkcfg->wce),
84 VHOST_SET_CONFIG_TYPE_FRONTEND);
85 if (ret) {
86 error_report("set device config space failed");
87 return;
88 }
89
90 s->blkcfg.wce = blkcfg->wce;
91 }
92
vhost_user_blk_sync_config(DeviceState * dev,Error ** errp)93 static int vhost_user_blk_sync_config(DeviceState *dev, Error **errp)
94 {
95 int ret;
96 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
97 VHostUserBlk *s = VHOST_USER_BLK(vdev);
98
99 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
100 vdev->config_len, errp);
101 if (ret < 0) {
102 return ret;
103 }
104
105 memcpy(vdev->config, &s->blkcfg, vdev->config_len);
106 virtio_notify_config(vdev);
107
108 return 0;
109 }
110
vhost_user_blk_handle_config_change(struct vhost_dev * dev)111 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
112 {
113 int ret;
114 Error *local_err = NULL;
115
116 if (!dev->started) {
117 return 0;
118 }
119
120 ret = vhost_user_blk_sync_config(DEVICE(dev->vdev), &local_err);
121 if (ret < 0) {
122 error_report_err(local_err);
123 return ret;
124 }
125
126 return 0;
127 }
128
129 const VhostDevConfigOps blk_ops = {
130 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
131 };
132
vhost_user_blk_start(VirtIODevice * vdev,Error ** errp)133 static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp)
134 {
135 VHostUserBlk *s = VHOST_USER_BLK(vdev);
136 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
137 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
138 int i, ret;
139
140 if (!k->set_guest_notifiers) {
141 error_setg(errp, "binding does not support guest notifiers");
142 return -ENOSYS;
143 }
144
145 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
146 if (ret < 0) {
147 error_setg_errno(errp, -ret, "Error enabling host notifiers");
148 return ret;
149 }
150
151 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
152 if (ret < 0) {
153 error_setg_errno(errp, -ret, "Error binding guest notifier");
154 goto err_host_notifiers;
155 }
156
157 s->dev.acked_features = vdev->guest_features;
158
159 ret = vhost_dev_prepare_inflight(&s->dev, vdev);
160 if (ret < 0) {
161 error_setg_errno(errp, -ret, "Error setting inflight format");
162 goto err_guest_notifiers;
163 }
164
165 if (!s->inflight->addr) {
166 ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight);
167 if (ret < 0) {
168 error_setg_errno(errp, -ret, "Error getting inflight");
169 goto err_guest_notifiers;
170 }
171 }
172
173 ret = vhost_dev_set_inflight(&s->dev, s->inflight);
174 if (ret < 0) {
175 error_setg_errno(errp, -ret, "Error setting inflight");
176 goto err_guest_notifiers;
177 }
178
179 /* guest_notifier_mask/pending not used yet, so just unmask
180 * everything here. virtio-pci will do the right thing by
181 * enabling/disabling irqfd.
182 */
183 for (i = 0; i < s->dev.nvqs; i++) {
184 vhost_virtqueue_mask(&s->dev, vdev, i, false);
185 }
186
187 s->dev.vq_index_end = s->dev.nvqs;
188 ret = vhost_dev_start(&s->dev, vdev, true);
189 if (ret < 0) {
190 error_setg_errno(errp, -ret, "Error starting vhost");
191 goto err_guest_notifiers;
192 }
193 s->started_vu = true;
194
195 return ret;
196
197 err_guest_notifiers:
198 for (i = 0; i < s->dev.nvqs; i++) {
199 vhost_virtqueue_mask(&s->dev, vdev, i, true);
200 }
201 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
202 err_host_notifiers:
203 vhost_dev_disable_notifiers(&s->dev, vdev);
204 return ret;
205 }
206
vhost_user_blk_stop(VirtIODevice * vdev)207 static int vhost_user_blk_stop(VirtIODevice *vdev)
208 {
209 VHostUserBlk *s = VHOST_USER_BLK(vdev);
210 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
211 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
212 int ret;
213
214 if (!s->started_vu) {
215 return 0;
216 }
217 s->started_vu = false;
218
219 if (!k->set_guest_notifiers) {
220 return 0;
221 }
222
223 ret = vhost_dev_stop(&s->dev, vdev, true);
224
225 if (k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false) < 0) {
226 error_report("vhost guest notifier cleanup failed: %d", ret);
227 return -1;
228 }
229
230 vhost_dev_disable_notifiers(&s->dev, vdev);
231 return ret;
232 }
233
vhost_user_blk_set_status(VirtIODevice * vdev,uint8_t status)234 static int vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
235 {
236 VHostUserBlk *s = VHOST_USER_BLK(vdev);
237 bool should_start = virtio_device_should_start(vdev, status);
238 Error *local_err = NULL;
239 int ret;
240
241 if (!s->connected) {
242 return -1;
243 }
244
245 if (vhost_dev_is_started(&s->dev) == should_start) {
246 return 0;
247 }
248
249 if (should_start) {
250 ret = vhost_user_blk_start(vdev, &local_err);
251 if (ret < 0) {
252 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
253 qemu_chr_fe_disconnect(&s->chardev);
254 }
255 } else {
256 ret = vhost_user_blk_stop(vdev);
257 if (ret < 0) {
258 return ret;
259 }
260 }
261 return 0;
262 }
263
vhost_user_blk_get_features(VirtIODevice * vdev,uint64_t features,Error ** errp)264 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
265 uint64_t features,
266 Error **errp)
267 {
268 VHostUserBlk *s = VHOST_USER_BLK(vdev);
269
270 /* Turn on pre-defined features */
271 virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX);
272 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
273 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
274 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
275 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
276 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
277 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
278
279 if (s->num_queues > 1) {
280 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
281 }
282
283 return vhost_get_features(&s->dev, user_feature_bits, features);
284 }
285
vhost_user_blk_handle_output(VirtIODevice * vdev,VirtQueue * vq)286 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
287 {
288 VHostUserBlk *s = VHOST_USER_BLK(vdev);
289 Error *local_err = NULL;
290 int i, ret;
291
292 if (!vdev->start_on_kick) {
293 return;
294 }
295
296 if (!s->connected) {
297 return;
298 }
299
300 if (vhost_dev_is_started(&s->dev)) {
301 return;
302 }
303
304 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
305 * vhost here instead of waiting for .set_status().
306 */
307 ret = vhost_user_blk_start(vdev, &local_err);
308 if (ret < 0) {
309 error_reportf_err(local_err, "vhost-user-blk: vhost start failed: ");
310 qemu_chr_fe_disconnect(&s->chardev);
311 return;
312 }
313
314 /* Kick right away to begin processing requests already in vring */
315 for (i = 0; i < s->dev.nvqs; i++) {
316 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
317
318 if (!virtio_queue_get_desc_addr(vdev, i)) {
319 continue;
320 }
321 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
322 }
323 }
324
vhost_user_blk_reset(VirtIODevice * vdev)325 static void vhost_user_blk_reset(VirtIODevice *vdev)
326 {
327 VHostUserBlk *s = VHOST_USER_BLK(vdev);
328
329 vhost_dev_free_inflight(s->inflight);
330 }
331
vhost_user_blk_connect(DeviceState * dev,Error ** errp)332 static int vhost_user_blk_connect(DeviceState *dev, Error **errp)
333 {
334 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
335 VHostUserBlk *s = VHOST_USER_BLK(vdev);
336 int ret = 0;
337
338 if (s->connected) {
339 return 0;
340 }
341
342 s->dev.num_queues = s->num_queues;
343 s->dev.nvqs = s->num_queues;
344 s->dev.vqs = s->vhost_vqs;
345 s->dev.vq_index = 0;
346 s->dev.backend_features = 0;
347
348 vhost_dev_set_config_notifier(&s->dev, &blk_ops);
349
350 s->vhost_user.supports_config = true;
351 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
352 errp);
353 if (ret < 0) {
354 return ret;
355 }
356
357 s->connected = true;
358
359 /* restore vhost state */
360 if (virtio_device_started(vdev, vdev->status)) {
361 ret = vhost_user_blk_start(vdev, errp);
362 }
363
364 return ret;
365 }
366
vhost_user_blk_disconnect(DeviceState * dev)367 static void vhost_user_blk_disconnect(DeviceState *dev)
368 {
369 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
370 VHostUserBlk *s = VHOST_USER_BLK(vdev);
371
372 if (!s->connected) {
373 goto done;
374 }
375 s->connected = false;
376
377 vhost_user_blk_stop(vdev);
378
379 vhost_dev_cleanup(&s->dev);
380
381 done:
382 /* Re-instate the event handler for new connections */
383 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
384 NULL, dev, NULL, true);
385 }
386
vhost_user_blk_event(void * opaque,QEMUChrEvent event)387 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
388 {
389 DeviceState *dev = opaque;
390 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
391 VHostUserBlk *s = VHOST_USER_BLK(vdev);
392 Error *local_err = NULL;
393
394 switch (event) {
395 case CHR_EVENT_OPENED:
396 if (vhost_user_blk_connect(dev, &local_err) < 0) {
397 error_report_err(local_err);
398 qemu_chr_fe_disconnect(&s->chardev);
399 return;
400 }
401 break;
402 case CHR_EVENT_CLOSED:
403 /* defer close until later to avoid circular close */
404 vhost_user_async_close(dev, &s->chardev, &s->dev,
405 vhost_user_blk_disconnect);
406 break;
407 case CHR_EVENT_BREAK:
408 case CHR_EVENT_MUX_IN:
409 case CHR_EVENT_MUX_OUT:
410 /* Ignore */
411 break;
412 }
413 }
414
vhost_user_blk_realize_connect(VHostUserBlk * s,Error ** errp)415 static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp)
416 {
417 DeviceState *dev = DEVICE(s);
418 int ret;
419
420 s->connected = false;
421
422 ret = qemu_chr_fe_wait_connected(&s->chardev, errp);
423 if (ret < 0) {
424 return ret;
425 }
426
427 ret = vhost_user_blk_connect(dev, errp);
428 if (ret < 0) {
429 qemu_chr_fe_disconnect(&s->chardev);
430 return ret;
431 }
432 assert(s->connected);
433
434 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
435 VIRTIO_DEVICE(s)->config_len, errp);
436 if (ret < 0) {
437 qemu_chr_fe_disconnect(&s->chardev);
438 vhost_dev_cleanup(&s->dev);
439 return ret;
440 }
441
442 return 0;
443 }
444
vhost_user_blk_device_realize(DeviceState * dev,Error ** errp)445 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
446 {
447 ERRP_GUARD();
448 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
449 VHostUserBlk *s = VHOST_USER_BLK(vdev);
450 size_t config_size;
451 int retries;
452 int i, ret;
453
454 if (!s->chardev.chr) {
455 error_setg(errp, "chardev is mandatory");
456 return;
457 }
458
459 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) {
460 s->num_queues = 1;
461 }
462 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
463 error_setg(errp, "invalid number of IO queues");
464 return;
465 }
466
467 if (!s->queue_size) {
468 error_setg(errp, "queue size must be non-zero");
469 return;
470 }
471 if (s->queue_size > VIRTQUEUE_MAX_SIZE) {
472 error_setg(errp, "queue size must not exceed %d",
473 VIRTQUEUE_MAX_SIZE);
474 return;
475 }
476
477 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
478 return;
479 }
480
481 config_size = virtio_get_config_size(&virtio_blk_cfg_size_params,
482 vdev->host_features);
483 virtio_init(vdev, VIRTIO_ID_BLOCK, config_size);
484
485 s->virtqs = g_new(VirtQueue *, s->num_queues);
486 for (i = 0; i < s->num_queues; i++) {
487 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
488 vhost_user_blk_handle_output);
489 }
490
491 s->inflight = g_new0(struct vhost_inflight, 1);
492 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
493
494 retries = VU_REALIZE_CONN_RETRIES;
495 assert(!*errp);
496 do {
497 if (*errp) {
498 error_prepend(errp, "Reconnecting after error: ");
499 error_report_err(*errp);
500 *errp = NULL;
501 }
502 ret = vhost_user_blk_realize_connect(s, errp);
503 } while (ret < 0 && retries--);
504
505 if (ret < 0) {
506 goto virtio_err;
507 }
508
509 /* we're fully initialized, now we can operate, so add the handler */
510 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL,
511 vhost_user_blk_event, NULL, (void *)dev,
512 NULL, true);
513 return;
514
515 virtio_err:
516 g_free(s->vhost_vqs);
517 s->vhost_vqs = NULL;
518 g_free(s->inflight);
519 s->inflight = NULL;
520 for (i = 0; i < s->num_queues; i++) {
521 virtio_delete_queue(s->virtqs[i]);
522 }
523 g_free(s->virtqs);
524 virtio_cleanup(vdev);
525 vhost_user_cleanup(&s->vhost_user);
526 }
527
vhost_user_blk_device_unrealize(DeviceState * dev)528 static void vhost_user_blk_device_unrealize(DeviceState *dev)
529 {
530 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
531 VHostUserBlk *s = VHOST_USER_BLK(dev);
532 int i;
533
534 virtio_set_status(vdev, 0);
535 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL,
536 NULL, NULL, NULL, false);
537 vhost_dev_cleanup(&s->dev);
538 vhost_dev_free_inflight(s->inflight);
539 g_free(s->vhost_vqs);
540 s->vhost_vqs = NULL;
541 g_free(s->inflight);
542 s->inflight = NULL;
543
544 for (i = 0; i < s->num_queues; i++) {
545 virtio_delete_queue(s->virtqs[i]);
546 }
547 g_free(s->virtqs);
548 virtio_cleanup(vdev);
549 vhost_user_cleanup(&s->vhost_user);
550 }
551
vhost_user_blk_instance_init(Object * obj)552 static void vhost_user_blk_instance_init(Object *obj)
553 {
554 VHostUserBlk *s = VHOST_USER_BLK(obj);
555
556 device_add_bootindex_property(obj, &s->bootindex, "bootindex",
557 "/disk@0,0", DEVICE(obj));
558 }
559
vhost_user_blk_get_vhost(VirtIODevice * vdev)560 static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev)
561 {
562 VHostUserBlk *s = VHOST_USER_BLK(vdev);
563 return &s->dev;
564 }
565
566 static const VMStateDescription vmstate_vhost_user_blk = {
567 .name = "vhost-user-blk",
568 .minimum_version_id = 1,
569 .version_id = 1,
570 .fields = (const VMStateField[]) {
571 VMSTATE_VIRTIO_DEVICE,
572 VMSTATE_END_OF_LIST()
573 },
574 };
575
576 static const Property vhost_user_blk_properties[] = {
577 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
578 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues,
579 VHOST_USER_BLK_AUTO_NUM_QUEUES),
580 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
581 DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features,
582 VIRTIO_BLK_F_CONFIG_WCE, true),
583 DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features,
584 VIRTIO_BLK_F_DISCARD, true),
585 DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features,
586 VIRTIO_BLK_F_WRITE_ZEROES, true),
587 };
588
vhost_user_blk_class_init(ObjectClass * klass,const void * data)589 static void vhost_user_blk_class_init(ObjectClass *klass, const void *data)
590 {
591 DeviceClass *dc = DEVICE_CLASS(klass);
592 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
593
594 device_class_set_props(dc, vhost_user_blk_properties);
595 dc->vmsd = &vmstate_vhost_user_blk;
596 dc->sync_config = vhost_user_blk_sync_config;
597 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
598 vdc->realize = vhost_user_blk_device_realize;
599 vdc->unrealize = vhost_user_blk_device_unrealize;
600 vdc->get_config = vhost_user_blk_update_config;
601 vdc->set_config = vhost_user_blk_set_config;
602 vdc->get_features = vhost_user_blk_get_features;
603 vdc->set_status = vhost_user_blk_set_status;
604 vdc->reset = vhost_user_blk_reset;
605 vdc->get_vhost = vhost_user_blk_get_vhost;
606 }
607
608 static const TypeInfo vhost_user_blk_info = {
609 .name = TYPE_VHOST_USER_BLK,
610 .parent = TYPE_VIRTIO_DEVICE,
611 .instance_size = sizeof(VHostUserBlk),
612 .instance_init = vhost_user_blk_instance_init,
613 .class_init = vhost_user_blk_class_init,
614 };
615
virtio_register_types(void)616 static void virtio_register_types(void)
617 {
618 type_register_static(&vhost_user_blk_info);
619 }
620
621 type_init(virtio_register_types)
622