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.h"
18 #include "hw/virtio/vhost-vsock.h"
20 #include "monitor/monitor.h"
22 const int feature_bits
[] = {
23 VIRTIO_VSOCK_F_SEQPACKET
,
25 VHOST_INVALID_FEATURE_BIT
28 uint64_t vhost_vsock_common_get_features(VirtIODevice
*vdev
, uint64_t features
,
31 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
33 if (vvc
->seqpacket
!= ON_OFF_AUTO_OFF
) {
34 virtio_add_feature(&features
, VIRTIO_VSOCK_F_SEQPACKET
);
37 features
= vhost_get_features(&vvc
->vhost_dev
, feature_bits
, features
);
39 if (vvc
->seqpacket
== ON_OFF_AUTO_ON
&&
40 !virtio_has_feature(features
, VIRTIO_VSOCK_F_SEQPACKET
)) {
41 error_setg(errp
, "vhost-vsock backend doesn't support seqpacket");
47 int vhost_vsock_common_start(VirtIODevice
*vdev
)
49 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
50 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
51 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
55 if (!k
->set_guest_notifiers
) {
56 error_report("binding does not support guest notifiers");
60 ret
= vhost_dev_enable_notifiers(&vvc
->vhost_dev
, vdev
);
62 error_report("Error enabling host notifiers: %d", -ret
);
66 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, true);
68 error_report("Error binding guest notifier: %d", -ret
);
69 goto err_host_notifiers
;
72 vvc
->vhost_dev
.acked_features
= vdev
->guest_features
;
73 ret
= vhost_dev_start(&vvc
->vhost_dev
, vdev
, true);
75 error_report("Error starting vhost: %d", -ret
);
76 goto err_guest_notifiers
;
80 * guest_notifier_mask/pending not used yet, so just unmask
81 * everything here. virtio-pci will do the right thing by
82 * enabling/disabling irqfd.
84 for (i
= 0; i
< vvc
->vhost_dev
.nvqs
; i
++) {
85 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, i
, false);
91 k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
93 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
97 void vhost_vsock_common_stop(VirtIODevice
*vdev
)
99 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
100 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
101 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
104 if (!k
->set_guest_notifiers
) {
108 vhost_dev_stop(&vvc
->vhost_dev
, vdev
, true);
110 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
112 error_report("vhost guest notifier cleanup failed: %d", ret
);
116 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
120 static void vhost_vsock_common_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
125 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
128 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
130 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, idx
, mask
);
133 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice
*vdev
,
136 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
138 return vhost_virtqueue_pending(&vvc
->vhost_dev
, idx
);
141 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon
*vvc
)
143 VirtQueueElement
*elem
;
144 VirtQueue
*vq
= vvc
->event_vq
;
145 struct virtio_vsock_event event
= {
146 .id
= cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
),
149 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
151 error_report("vhost-vsock missed transport reset event");
156 error_report("invalid vhost-vsock event virtqueue element with "
161 if (iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
162 &event
, sizeof(event
)) != sizeof(event
)) {
163 error_report("vhost-vsock event virtqueue element is too short");
167 virtqueue_push(vq
, elem
, sizeof(event
));
168 virtio_notify(VIRTIO_DEVICE(vvc
), vq
);
174 virtqueue_detach_element(vq
, elem
, 0);
178 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon
*vvc
)
180 if (!vvc
->post_load_timer
) {
184 timer_free(vvc
->post_load_timer
);
185 vvc
->post_load_timer
= NULL
;
188 static void vhost_vsock_common_post_load_timer_cb(void *opaque
)
190 VHostVSockCommon
*vvc
= opaque
;
192 vhost_vsock_common_post_load_timer_cleanup(vvc
);
193 vhost_vsock_common_send_transport_reset(vvc
);
196 int vhost_vsock_common_pre_save(void *opaque
)
198 VHostVSockCommon
*vvc
= opaque
;
201 * At this point, backend must be stopped, otherwise
202 * it might keep writing to memory.
204 assert(!vhost_dev_is_started(&vvc
->vhost_dev
));
209 int vhost_vsock_common_post_load(void *opaque
, int version_id
)
211 VHostVSockCommon
*vvc
= opaque
;
212 VirtIODevice
*vdev
= VIRTIO_DEVICE(vvc
);
214 if (virtio_queue_get_addr(vdev
, 2)) {
216 * Defer transport reset event to a vm clock timer so that virtqueue
217 * changes happen after migration has completed.
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
,
224 timer_mod(vvc
->post_load_timer
, 1);
229 void vhost_vsock_common_realize(VirtIODevice
*vdev
)
231 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
233 virtio_init(vdev
, VIRTIO_ID_VSOCK
, sizeof(struct virtio_vsock_config
));
235 /* Receive and transmit queues belong to vhost */
236 vvc
->recv_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
237 vhost_vsock_common_handle_output
);
238 vvc
->trans_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
239 vhost_vsock_common_handle_output
);
241 /* The event queue belongs to QEMU */
242 vvc
->event_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
243 vhost_vsock_common_handle_output
);
245 vvc
->vhost_dev
.nvqs
= ARRAY_SIZE(vvc
->vhost_vqs
);
246 vvc
->vhost_dev
.vqs
= vvc
->vhost_vqs
;
248 vvc
->post_load_timer
= NULL
;
251 void vhost_vsock_common_unrealize(VirtIODevice
*vdev
)
253 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
255 vhost_vsock_common_post_load_timer_cleanup(vvc
);
257 virtio_delete_queue(vvc
->recv_vq
);
258 virtio_delete_queue(vvc
->trans_vq
);
259 virtio_delete_queue(vvc
->event_vq
);
260 virtio_cleanup(vdev
);
263 static struct vhost_dev
*vhost_vsock_common_get_vhost(VirtIODevice
*vdev
)
265 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
266 return &vvc
->vhost_dev
;
269 static Property vhost_vsock_common_properties
[] = {
270 DEFINE_PROP_ON_OFF_AUTO("seqpacket", VHostVSockCommon
, seqpacket
,
272 DEFINE_PROP_END_OF_LIST(),
275 static void vhost_vsock_common_class_init(ObjectClass
*klass
, void *data
)
277 DeviceClass
*dc
= DEVICE_CLASS(klass
);
278 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
280 device_class_set_props(dc
, vhost_vsock_common_properties
);
281 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
282 vdc
->guest_notifier_mask
= vhost_vsock_common_guest_notifier_mask
;
283 vdc
->guest_notifier_pending
= vhost_vsock_common_guest_notifier_pending
;
284 vdc
->get_vhost
= vhost_vsock_common_get_vhost
;
287 static const TypeInfo vhost_vsock_common_info
= {
288 .name
= TYPE_VHOST_VSOCK_COMMON
,
289 .parent
= TYPE_VIRTIO_DEVICE
,
290 .instance_size
= sizeof(VHostVSockCommon
),
291 .class_init
= vhost_vsock_common_class_init
,
295 static void vhost_vsock_common_register_types(void)
297 type_register_static(&vhost_vsock_common_info
);
300 type_init(vhost_vsock_common_register_types
)