2 * Parent class for vhost-vsock devices
4 * Copyright 2015-2020 Red Hat, Inc.
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
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"
19 #include "monitor/monitor.h"
21 const int feature_bits
[] = {
22 VIRTIO_VSOCK_F_SEQPACKET
,
23 VHOST_INVALID_FEATURE_BIT
26 uint64_t vhost_vsock_common_get_features(VirtIODevice
*vdev
, uint64_t features
,
29 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
31 if (vvc
->seqpacket
!= ON_OFF_AUTO_OFF
) {
32 virtio_add_feature(&features
, VIRTIO_VSOCK_F_SEQPACKET
);
35 features
= vhost_get_features(&vvc
->vhost_dev
, feature_bits
, features
);
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");
45 int vhost_vsock_common_start(VirtIODevice
*vdev
)
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
);
53 if (!k
->set_guest_notifiers
) {
54 error_report("binding does not support guest notifiers");
58 ret
= vhost_dev_enable_notifiers(&vvc
->vhost_dev
, vdev
);
60 error_report("Error enabling host notifiers: %d", -ret
);
64 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, true);
66 error_report("Error binding guest notifier: %d", -ret
);
67 goto err_host_notifiers
;
70 vvc
->vhost_dev
.acked_features
= vdev
->guest_features
;
71 ret
= vhost_dev_start(&vvc
->vhost_dev
, vdev
);
73 error_report("Error starting vhost: %d", -ret
);
74 goto err_guest_notifiers
;
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.
82 for (i
= 0; i
< vvc
->vhost_dev
.nvqs
; i
++) {
83 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, i
, false);
89 k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
91 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
95 void vhost_vsock_common_stop(VirtIODevice
*vdev
)
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
);
102 if (!k
->set_guest_notifiers
) {
106 vhost_dev_stop(&vvc
->vhost_dev
, vdev
);
108 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
110 error_report("vhost guest notifier cleanup failed: %d", ret
);
114 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
118 static void vhost_vsock_common_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
123 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
126 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
128 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, idx
, mask
);
131 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice
*vdev
,
134 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
136 return vhost_virtqueue_pending(&vvc
->vhost_dev
, idx
);
139 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon
*vvc
)
141 VirtQueueElement
*elem
;
142 VirtQueue
*vq
= vvc
->event_vq
;
143 struct virtio_vsock_event event
= {
144 .id
= cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
),
147 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
149 error_report("vhost-vsock missed transport reset event");
154 error_report("invalid vhost-vsock event virtqueue element with "
159 if (iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
160 &event
, sizeof(event
)) != sizeof(event
)) {
161 error_report("vhost-vsock event virtqueue element is too short");
165 virtqueue_push(vq
, elem
, sizeof(event
));
166 virtio_notify(VIRTIO_DEVICE(vvc
), vq
);
172 virtqueue_detach_element(vq
, elem
, 0);
176 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon
*vvc
)
178 if (!vvc
->post_load_timer
) {
182 timer_free(vvc
->post_load_timer
);
183 vvc
->post_load_timer
= NULL
;
186 static void vhost_vsock_common_post_load_timer_cb(void *opaque
)
188 VHostVSockCommon
*vvc
= opaque
;
190 vhost_vsock_common_post_load_timer_cleanup(vvc
);
191 vhost_vsock_common_send_transport_reset(vvc
);
194 int vhost_vsock_common_pre_save(void *opaque
)
196 VHostVSockCommon
*vvc
= opaque
;
199 * At this point, backend must be stopped, otherwise
200 * it might keep writing to memory.
202 assert(!vvc
->vhost_dev
.started
);
207 int vhost_vsock_common_post_load(void *opaque
, int version_id
)
209 VHostVSockCommon
*vvc
= opaque
;
210 VirtIODevice
*vdev
= VIRTIO_DEVICE(vvc
);
212 if (virtio_queue_get_addr(vdev
, 2)) {
214 * Defer transport reset event to a vm clock timer so that virtqueue
215 * changes happen after migration has completed.
217 assert(!vvc
->post_load_timer
);
218 vvc
->post_load_timer
=
219 timer_new_ns(QEMU_CLOCK_VIRTUAL
,
220 vhost_vsock_common_post_load_timer_cb
,
222 timer_mod(vvc
->post_load_timer
, 1);
227 void vhost_vsock_common_realize(VirtIODevice
*vdev
, const char *name
)
229 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
231 virtio_init(vdev
, name
, VIRTIO_ID_VSOCK
,
232 sizeof(struct virtio_vsock_config
));
234 /* Receive and transmit queues belong to vhost */
235 vvc
->recv_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
236 vhost_vsock_common_handle_output
);
237 vvc
->trans_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
238 vhost_vsock_common_handle_output
);
240 /* The event queue belongs to QEMU */
241 vvc
->event_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
242 vhost_vsock_common_handle_output
);
244 vvc
->vhost_dev
.nvqs
= ARRAY_SIZE(vvc
->vhost_vqs
);
245 vvc
->vhost_dev
.vqs
= vvc
->vhost_vqs
;
247 vvc
->post_load_timer
= NULL
;
250 void vhost_vsock_common_unrealize(VirtIODevice
*vdev
)
252 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
254 vhost_vsock_common_post_load_timer_cleanup(vvc
);
256 virtio_delete_queue(vvc
->recv_vq
);
257 virtio_delete_queue(vvc
->trans_vq
);
258 virtio_delete_queue(vvc
->event_vq
);
259 virtio_cleanup(vdev
);
262 static Property vhost_vsock_common_properties
[] = {
263 DEFINE_PROP_ON_OFF_AUTO("seqpacket", VHostVSockCommon
, seqpacket
,
265 DEFINE_PROP_END_OF_LIST(),
268 static void vhost_vsock_common_class_init(ObjectClass
*klass
, void *data
)
270 DeviceClass
*dc
= DEVICE_CLASS(klass
);
271 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
273 device_class_set_props(dc
, vhost_vsock_common_properties
);
274 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
275 vdc
->guest_notifier_mask
= vhost_vsock_common_guest_notifier_mask
;
276 vdc
->guest_notifier_pending
= vhost_vsock_common_guest_notifier_pending
;
279 static const TypeInfo vhost_vsock_common_info
= {
280 .name
= TYPE_VHOST_VSOCK_COMMON
,
281 .parent
= TYPE_VIRTIO_DEVICE
,
282 .instance_size
= sizeof(VHostVSockCommon
),
283 .class_init
= vhost_vsock_common_class_init
,
287 static void vhost_vsock_common_register_types(void)
289 type_register_static(&vhost_vsock_common_info
);
292 type_init(vhost_vsock_common_register_types
)