1 /* 2 * Parent class for vhost-vsock devices 3 * 4 * Copyright 2015-2020 Red Hat, Inc. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or 7 * (at your option) any later version. See the COPYING file in the 8 * top-level directory. 9 */ 10 11 #include "qemu/osdep.h" 12 #include "standard-headers/linux/virtio_vsock.h" 13 #include "qapi/error.h" 14 #include "hw/virtio/virtio-access.h" 15 #include "qemu/error-report.h" 16 #include "hw/qdev-properties.h" 17 #include "hw/virtio/vhost-vsock.h" 18 #include "qemu/iov.h" 19 #include "monitor/monitor.h" 20 21 const int feature_bits[] = { 22 VIRTIO_VSOCK_F_SEQPACKET, 23 VHOST_INVALID_FEATURE_BIT 24 }; 25 26 uint64_t vhost_vsock_common_get_features(VirtIODevice *vdev, uint64_t features, 27 Error **errp) 28 { 29 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 30 31 if (vvc->seqpacket != ON_OFF_AUTO_OFF) { 32 virtio_add_feature(&features, VIRTIO_VSOCK_F_SEQPACKET); 33 } 34 35 features = vhost_get_features(&vvc->vhost_dev, feature_bits, features); 36 37 if (vvc->seqpacket == ON_OFF_AUTO_ON && 38 !virtio_has_feature(features, VIRTIO_VSOCK_F_SEQPACKET)) { 39 error_setg(errp, "vhost-vsock backend doesn't support seqpacket"); 40 } 41 42 return features; 43 } 44 45 int vhost_vsock_common_start(VirtIODevice *vdev) 46 { 47 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 48 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 49 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 50 int ret; 51 int i; 52 53 if (!k->set_guest_notifiers) { 54 error_report("binding does not support guest notifiers"); 55 return -ENOSYS; 56 } 57 58 ret = vhost_dev_enable_notifiers(&vvc->vhost_dev, vdev); 59 if (ret < 0) { 60 error_report("Error enabling host notifiers: %d", -ret); 61 return ret; 62 } 63 64 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, true); 65 if (ret < 0) { 66 error_report("Error binding guest notifier: %d", -ret); 67 goto err_host_notifiers; 68 } 69 70 vvc->vhost_dev.acked_features = vdev->guest_features; 71 ret = vhost_dev_start(&vvc->vhost_dev, vdev); 72 if (ret < 0) { 73 error_report("Error starting vhost: %d", -ret); 74 goto err_guest_notifiers; 75 } 76 77 /* 78 * guest_notifier_mask/pending not used yet, so just unmask 79 * everything here. virtio-pci will do the right thing by 80 * enabling/disabling irqfd. 81 */ 82 for (i = 0; i < vvc->vhost_dev.nvqs; i++) { 83 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, i, false); 84 } 85 86 return 0; 87 88 err_guest_notifiers: 89 k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false); 90 err_host_notifiers: 91 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev); 92 return ret; 93 } 94 95 void vhost_vsock_common_stop(VirtIODevice *vdev) 96 { 97 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 98 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); 99 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); 100 int ret; 101 102 if (!k->set_guest_notifiers) { 103 return; 104 } 105 106 vhost_dev_stop(&vvc->vhost_dev, vdev); 107 108 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false); 109 if (ret < 0) { 110 error_report("vhost guest notifier cleanup failed: %d", ret); 111 return; 112 } 113 114 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev); 115 } 116 117 118 static void vhost_vsock_common_handle_output(VirtIODevice *vdev, VirtQueue *vq) 119 { 120 /* Do nothing */ 121 } 122 123 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice *vdev, int idx, 124 bool mask) 125 { 126 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 127 128 if (idx == VIRTIO_CONFIG_IRQ_IDX) { 129 return; 130 } 131 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask); 132 } 133 134 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev, 135 int idx) 136 { 137 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 138 139 if (idx == VIRTIO_CONFIG_IRQ_IDX) { 140 return false; 141 } 142 return vhost_virtqueue_pending(&vvc->vhost_dev, idx); 143 } 144 145 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon *vvc) 146 { 147 VirtQueueElement *elem; 148 VirtQueue *vq = vvc->event_vq; 149 struct virtio_vsock_event event = { 150 .id = cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET), 151 }; 152 153 elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 154 if (!elem) { 155 error_report("vhost-vsock missed transport reset event"); 156 return; 157 } 158 159 if (elem->out_num) { 160 error_report("invalid vhost-vsock event virtqueue element with " 161 "out buffers"); 162 goto out; 163 } 164 165 if (iov_from_buf(elem->in_sg, elem->in_num, 0, 166 &event, sizeof(event)) != sizeof(event)) { 167 error_report("vhost-vsock event virtqueue element is too short"); 168 goto out; 169 } 170 171 virtqueue_push(vq, elem, sizeof(event)); 172 virtio_notify(VIRTIO_DEVICE(vvc), vq); 173 174 out: 175 g_free(elem); 176 } 177 178 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon *vvc) 179 { 180 if (!vvc->post_load_timer) { 181 return; 182 } 183 184 timer_free(vvc->post_load_timer); 185 vvc->post_load_timer = NULL; 186 } 187 188 static void vhost_vsock_common_post_load_timer_cb(void *opaque) 189 { 190 VHostVSockCommon *vvc = opaque; 191 192 vhost_vsock_common_post_load_timer_cleanup(vvc); 193 vhost_vsock_common_send_transport_reset(vvc); 194 } 195 196 int vhost_vsock_common_pre_save(void *opaque) 197 { 198 VHostVSockCommon *vvc = opaque; 199 200 /* 201 * At this point, backend must be stopped, otherwise 202 * it might keep writing to memory. 203 */ 204 assert(!vvc->vhost_dev.started); 205 206 return 0; 207 } 208 209 int vhost_vsock_common_post_load(void *opaque, int version_id) 210 { 211 VHostVSockCommon *vvc = opaque; 212 VirtIODevice *vdev = VIRTIO_DEVICE(vvc); 213 214 if (virtio_queue_get_addr(vdev, 2)) { 215 /* 216 * Defer transport reset event to a vm clock timer so that virtqueue 217 * changes happen after migration has completed. 218 */ 219 assert(!vvc->post_load_timer); 220 vvc->post_load_timer = 221 timer_new_ns(QEMU_CLOCK_VIRTUAL, 222 vhost_vsock_common_post_load_timer_cb, 223 vvc); 224 timer_mod(vvc->post_load_timer, 1); 225 } 226 return 0; 227 } 228 229 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) 230 { 231 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 232 233 virtio_init(vdev, name, VIRTIO_ID_VSOCK, 234 sizeof(struct virtio_vsock_config)); 235 236 /* Receive and transmit queues belong to vhost */ 237 vvc->recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, 238 vhost_vsock_common_handle_output); 239 vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, 240 vhost_vsock_common_handle_output); 241 242 /* The event queue belongs to QEMU */ 243 vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, 244 vhost_vsock_common_handle_output); 245 246 vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs); 247 vvc->vhost_dev.vqs = vvc->vhost_vqs; 248 249 vvc->post_load_timer = NULL; 250 } 251 252 void vhost_vsock_common_unrealize(VirtIODevice *vdev) 253 { 254 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev); 255 256 vhost_vsock_common_post_load_timer_cleanup(vvc); 257 258 virtio_delete_queue(vvc->recv_vq); 259 virtio_delete_queue(vvc->trans_vq); 260 virtio_delete_queue(vvc->event_vq); 261 virtio_cleanup(vdev); 262 } 263 264 static Property vhost_vsock_common_properties[] = { 265 DEFINE_PROP_ON_OFF_AUTO("seqpacket", VHostVSockCommon, seqpacket, 266 ON_OFF_AUTO_AUTO), 267 DEFINE_PROP_END_OF_LIST(), 268 }; 269 270 static void vhost_vsock_common_class_init(ObjectClass *klass, void *data) 271 { 272 DeviceClass *dc = DEVICE_CLASS(klass); 273 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 274 275 device_class_set_props(dc, vhost_vsock_common_properties); 276 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 277 vdc->guest_notifier_mask = vhost_vsock_common_guest_notifier_mask; 278 vdc->guest_notifier_pending = vhost_vsock_common_guest_notifier_pending; 279 } 280 281 static const TypeInfo vhost_vsock_common_info = { 282 .name = TYPE_VHOST_VSOCK_COMMON, 283 .parent = TYPE_VIRTIO_DEVICE, 284 .instance_size = sizeof(VHostVSockCommon), 285 .class_init = vhost_vsock_common_class_init, 286 .abstract = true, 287 }; 288 289 static void vhost_vsock_common_register_types(void) 290 { 291 type_register_static(&vhost_vsock_common_info); 292 } 293 294 type_init(vhost_vsock_common_register_types) 295