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/qdev-properties.h" 19 #include "hw/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-user-fs.h" 24 #include "monitor/monitor.h" 25 26 static void vuf_get_config(VirtIODevice *vdev, uint8_t *config) 27 { 28 VHostUserFS *fs = VHOST_USER_FS(vdev); 29 struct virtio_fs_config fscfg = {}; 30 31 memcpy((char *)fscfg.tag, fs->conf.tag, 32 MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag))); 33 34 virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues); 35 36 memcpy(config, &fscfg, sizeof(fscfg)); 37 } 38 39 static void vuf_start(VirtIODevice *vdev) 40 { 41 VHostUserFS *fs = VHOST_USER_FS(vdev); 42 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 43 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 44 int ret; 45 int i; 46 47 if (!k->set_guest_notifiers) { 48 error_report("binding does not support guest notifiers"); 49 return; 50 } 51 52 ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev); 53 if (ret < 0) { 54 error_report("Error enabling host notifiers: %d", -ret); 55 return; 56 } 57 58 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true); 59 if (ret < 0) { 60 error_report("Error binding guest notifier: %d", -ret); 61 goto err_host_notifiers; 62 } 63 64 fs->vhost_dev.acked_features = vdev->guest_features; 65 ret = vhost_dev_start(&fs->vhost_dev, vdev); 66 if (ret < 0) { 67 error_report("Error starting vhost: %d", -ret); 68 goto err_guest_notifiers; 69 } 70 71 /* 72 * guest_notifier_mask/pending not used yet, so just unmask 73 * everything here. virtio-pci will do the right thing by 74 * enabling/disabling irqfd. 75 */ 76 for (i = 0; i < fs->vhost_dev.nvqs; i++) { 77 vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false); 78 } 79 80 return; 81 82 err_guest_notifiers: 83 k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false); 84 err_host_notifiers: 85 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev); 86 } 87 88 static void vuf_stop(VirtIODevice *vdev) 89 { 90 VHostUserFS *fs = VHOST_USER_FS(vdev); 91 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 92 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 93 int ret; 94 95 if (!k->set_guest_notifiers) { 96 return; 97 } 98 99 vhost_dev_stop(&fs->vhost_dev, vdev); 100 101 ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false); 102 if (ret < 0) { 103 error_report("vhost guest notifier cleanup failed: %d", ret); 104 return; 105 } 106 107 vhost_dev_disable_notifiers(&fs->vhost_dev, vdev); 108 } 109 110 static void vuf_set_status(VirtIODevice *vdev, uint8_t status) 111 { 112 VHostUserFS *fs = VHOST_USER_FS(vdev); 113 bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK; 114 115 if (!vdev->vm_running) { 116 should_start = false; 117 } 118 119 if (fs->vhost_dev.started == should_start) { 120 return; 121 } 122 123 if (should_start) { 124 vuf_start(vdev); 125 } else { 126 vuf_stop(vdev); 127 } 128 } 129 130 static uint64_t vuf_get_features(VirtIODevice *vdev, 131 uint64_t requested_features, 132 Error **errp) 133 { 134 /* No feature bits used yet */ 135 return requested_features; 136 } 137 138 static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq) 139 { 140 /* 141 * Not normally called; it's the daemon that handles the queue; 142 * however virtio's cleanup path can call this. 143 */ 144 } 145 146 static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx, 147 bool mask) 148 { 149 VHostUserFS *fs = VHOST_USER_FS(vdev); 150 151 vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask); 152 } 153 154 static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx) 155 { 156 VHostUserFS *fs = VHOST_USER_FS(vdev); 157 158 return vhost_virtqueue_pending(&fs->vhost_dev, idx); 159 } 160 161 static void vuf_device_realize(DeviceState *dev, Error **errp) 162 { 163 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 164 VHostUserFS *fs = VHOST_USER_FS(dev); 165 unsigned int i; 166 size_t len; 167 int ret; 168 169 if (!fs->conf.chardev.chr) { 170 error_setg(errp, "missing chardev"); 171 return; 172 } 173 174 if (!fs->conf.tag) { 175 error_setg(errp, "missing tag property"); 176 return; 177 } 178 len = strlen(fs->conf.tag); 179 if (len == 0) { 180 error_setg(errp, "tag property cannot be empty"); 181 return; 182 } 183 if (len > sizeof_field(struct virtio_fs_config, tag)) { 184 error_setg(errp, "tag property must be %zu bytes or less", 185 sizeof_field(struct virtio_fs_config, tag)); 186 return; 187 } 188 189 if (fs->conf.num_request_queues == 0) { 190 error_setg(errp, "num-request-queues property must be larger than 0"); 191 return; 192 } 193 194 if (!is_power_of_2(fs->conf.queue_size)) { 195 error_setg(errp, "queue-size property must be a power of 2"); 196 return; 197 } 198 199 if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) { 200 error_setg(errp, "queue-size property must be %u or smaller", 201 VIRTQUEUE_MAX_SIZE); 202 return; 203 } 204 205 if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) { 206 return; 207 } 208 209 virtio_init(vdev, "vhost-user-fs", VIRTIO_ID_FS, 210 sizeof(struct virtio_fs_config)); 211 212 /* Hiprio queue */ 213 fs->hiprio_vq = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); 214 215 /* Request queues */ 216 fs->req_vqs = g_new(VirtQueue *, fs->conf.num_request_queues); 217 for (i = 0; i < fs->conf.num_request_queues; i++) { 218 fs->req_vqs[i] = virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output); 219 } 220 221 /* 1 high prio queue, plus the number configured */ 222 fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues; 223 fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs); 224 ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user, 225 VHOST_BACKEND_TYPE_USER, 0); 226 if (ret < 0) { 227 error_setg_errno(errp, -ret, "vhost_dev_init failed"); 228 goto err_virtio; 229 } 230 231 return; 232 233 err_virtio: 234 vhost_user_cleanup(&fs->vhost_user); 235 virtio_delete_queue(fs->hiprio_vq); 236 for (i = 0; i < fs->conf.num_request_queues; i++) { 237 virtio_delete_queue(fs->req_vqs[i]); 238 } 239 g_free(fs->req_vqs); 240 virtio_cleanup(vdev); 241 g_free(fs->vhost_dev.vqs); 242 return; 243 } 244 245 static void vuf_device_unrealize(DeviceState *dev) 246 { 247 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 248 VHostUserFS *fs = VHOST_USER_FS(dev); 249 int i; 250 251 /* This will stop vhost backend if appropriate. */ 252 vuf_set_status(vdev, 0); 253 254 vhost_dev_cleanup(&fs->vhost_dev); 255 256 vhost_user_cleanup(&fs->vhost_user); 257 258 virtio_delete_queue(fs->hiprio_vq); 259 for (i = 0; i < fs->conf.num_request_queues; i++) { 260 virtio_delete_queue(fs->req_vqs[i]); 261 } 262 g_free(fs->req_vqs); 263 virtio_cleanup(vdev); 264 g_free(fs->vhost_dev.vqs); 265 fs->vhost_dev.vqs = NULL; 266 } 267 268 static const VMStateDescription vuf_vmstate = { 269 .name = "vhost-user-fs", 270 .unmigratable = 1, 271 }; 272 273 static Property vuf_properties[] = { 274 DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev), 275 DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag), 276 DEFINE_PROP_UINT16("num-request-queues", VHostUserFS, 277 conf.num_request_queues, 1), 278 DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128), 279 DEFINE_PROP_END_OF_LIST(), 280 }; 281 282 static void vuf_class_init(ObjectClass *klass, void *data) 283 { 284 DeviceClass *dc = DEVICE_CLASS(klass); 285 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 286 287 device_class_set_props(dc, vuf_properties); 288 dc->vmsd = &vuf_vmstate; 289 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 290 vdc->realize = vuf_device_realize; 291 vdc->unrealize = vuf_device_unrealize; 292 vdc->get_features = vuf_get_features; 293 vdc->get_config = vuf_get_config; 294 vdc->set_status = vuf_set_status; 295 vdc->guest_notifier_mask = vuf_guest_notifier_mask; 296 vdc->guest_notifier_pending = vuf_guest_notifier_pending; 297 } 298 299 static const TypeInfo vuf_info = { 300 .name = TYPE_VHOST_USER_FS, 301 .parent = TYPE_VIRTIO_DEVICE, 302 .instance_size = sizeof(VHostUserFS), 303 .class_init = vuf_class_init, 304 }; 305 306 static void vuf_register_types(void) 307 { 308 type_register_static(&vuf_info); 309 } 310 311 type_init(vuf_register_types) 312