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-bus.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
,
26 VHOST_INVALID_FEATURE_BIT
29 uint64_t vhost_vsock_common_get_features(VirtIODevice
*vdev
, uint64_t features
,
32 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
34 if (vvc
->seqpacket
!= ON_OFF_AUTO_OFF
) {
35 virtio_add_feature(&features
, VIRTIO_VSOCK_F_SEQPACKET
);
38 features
= vhost_get_features(&vvc
->vhost_dev
, feature_bits
, features
);
40 if (vvc
->seqpacket
== ON_OFF_AUTO_ON
&&
41 !virtio_has_feature(features
, VIRTIO_VSOCK_F_SEQPACKET
)) {
42 error_setg(errp
, "vhost-vsock backend doesn't support seqpacket");
48 int vhost_vsock_common_start(VirtIODevice
*vdev
)
50 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
51 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
52 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
56 if (!k
->set_guest_notifiers
) {
57 error_report("binding does not support guest notifiers");
61 ret
= vhost_dev_enable_notifiers(&vvc
->vhost_dev
, vdev
);
63 error_report("Error enabling host notifiers: %d", -ret
);
67 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, true);
69 error_report("Error binding guest notifier: %d", -ret
);
70 goto err_host_notifiers
;
73 vvc
->vhost_dev
.acked_features
= vdev
->guest_features
;
74 ret
= vhost_dev_start(&vvc
->vhost_dev
, vdev
, true);
76 error_report("Error starting vhost: %d", -ret
);
77 goto err_guest_notifiers
;
81 * guest_notifier_mask/pending not used yet, so just unmask
82 * everything here. virtio-pci will do the right thing by
83 * enabling/disabling irqfd.
85 for (i
= 0; i
< vvc
->vhost_dev
.nvqs
; i
++) {
86 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, i
, false);
92 k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
94 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
98 void vhost_vsock_common_stop(VirtIODevice
*vdev
)
100 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
101 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
102 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
105 if (!k
->set_guest_notifiers
) {
109 vhost_dev_stop(&vvc
->vhost_dev
, vdev
, true);
111 ret
= k
->set_guest_notifiers(qbus
->parent
, vvc
->vhost_dev
.nvqs
, false);
113 error_report("vhost guest notifier cleanup failed: %d", ret
);
117 vhost_dev_disable_notifiers(&vvc
->vhost_dev
, vdev
);
121 static void vhost_vsock_common_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
126 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
129 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
132 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
133 * as the macro of configure interrupt's IDX, If this driver does not
134 * support, the function will return
137 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
140 vhost_virtqueue_mask(&vvc
->vhost_dev
, vdev
, idx
, mask
);
143 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice
*vdev
,
146 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
149 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
150 * as the macro of configure interrupt's IDX, If this driver does not
151 * support, the function will return
154 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
157 return vhost_virtqueue_pending(&vvc
->vhost_dev
, idx
);
160 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon
*vvc
)
162 VirtQueueElement
*elem
;
163 VirtQueue
*vq
= vvc
->event_vq
;
164 struct virtio_vsock_event event
= {
165 .id
= cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
),
168 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
170 error_report("vhost-vsock missed transport reset event");
175 error_report("invalid vhost-vsock event virtqueue element with "
180 if (iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
181 &event
, sizeof(event
)) != sizeof(event
)) {
182 error_report("vhost-vsock event virtqueue element is too short");
186 virtqueue_push(vq
, elem
, sizeof(event
));
187 virtio_notify(VIRTIO_DEVICE(vvc
), vq
);
193 virtqueue_detach_element(vq
, elem
, 0);
197 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon
*vvc
)
199 if (!vvc
->post_load_timer
) {
203 timer_free(vvc
->post_load_timer
);
204 vvc
->post_load_timer
= NULL
;
207 static void vhost_vsock_common_post_load_timer_cb(void *opaque
)
209 VHostVSockCommon
*vvc
= opaque
;
211 vhost_vsock_common_post_load_timer_cleanup(vvc
);
212 vhost_vsock_common_send_transport_reset(vvc
);
215 int vhost_vsock_common_pre_save(void *opaque
)
217 VHostVSockCommon
*vvc
= opaque
;
220 * At this point, backend must be stopped, otherwise
221 * it might keep writing to memory.
223 assert(!vhost_dev_is_started(&vvc
->vhost_dev
));
228 int vhost_vsock_common_post_load(void *opaque
, int version_id
)
230 VHostVSockCommon
*vvc
= opaque
;
231 VirtIODevice
*vdev
= VIRTIO_DEVICE(vvc
);
233 if (virtio_queue_get_addr(vdev
, 2)) {
235 * Defer transport reset event to a vm clock timer so that virtqueue
236 * changes happen after migration has completed.
238 assert(!vvc
->post_load_timer
);
239 vvc
->post_load_timer
=
240 timer_new_ns(QEMU_CLOCK_VIRTUAL
,
241 vhost_vsock_common_post_load_timer_cb
,
243 timer_mod(vvc
->post_load_timer
, 1);
248 void vhost_vsock_common_realize(VirtIODevice
*vdev
)
250 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
252 virtio_init(vdev
, VIRTIO_ID_VSOCK
, sizeof(struct virtio_vsock_config
));
254 /* Receive and transmit queues belong to vhost */
255 vvc
->recv_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
256 vhost_vsock_common_handle_output
);
257 vvc
->trans_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
258 vhost_vsock_common_handle_output
);
260 /* The event queue belongs to QEMU */
261 vvc
->event_vq
= virtio_add_queue(vdev
, VHOST_VSOCK_QUEUE_SIZE
,
262 vhost_vsock_common_handle_output
);
264 vvc
->vhost_dev
.nvqs
= ARRAY_SIZE(vvc
->vhost_vqs
);
265 vvc
->vhost_dev
.vqs
= vvc
->vhost_vqs
;
267 vvc
->post_load_timer
= NULL
;
270 void vhost_vsock_common_unrealize(VirtIODevice
*vdev
)
272 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
274 vhost_vsock_common_post_load_timer_cleanup(vvc
);
276 virtio_delete_queue(vvc
->recv_vq
);
277 virtio_delete_queue(vvc
->trans_vq
);
278 virtio_delete_queue(vvc
->event_vq
);
279 virtio_cleanup(vdev
);
282 static struct vhost_dev
*vhost_vsock_common_get_vhost(VirtIODevice
*vdev
)
284 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
285 return &vvc
->vhost_dev
;
288 static Property vhost_vsock_common_properties
[] = {
289 DEFINE_PROP_ON_OFF_AUTO("seqpacket", VHostVSockCommon
, seqpacket
,
291 DEFINE_PROP_END_OF_LIST(),
294 static void vhost_vsock_common_class_init(ObjectClass
*klass
, void *data
)
296 DeviceClass
*dc
= DEVICE_CLASS(klass
);
297 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
299 device_class_set_props(dc
, vhost_vsock_common_properties
);
300 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
301 vdc
->guest_notifier_mask
= vhost_vsock_common_guest_notifier_mask
;
302 vdc
->guest_notifier_pending
= vhost_vsock_common_guest_notifier_pending
;
303 vdc
->get_vhost
= vhost_vsock_common_get_vhost
;
306 static const TypeInfo vhost_vsock_common_info
= {
307 .name
= TYPE_VHOST_VSOCK_COMMON
,
308 .parent
= TYPE_VIRTIO_DEVICE
,
309 .instance_size
= sizeof(VHostVSockCommon
),
310 .class_init
= vhost_vsock_common_class_init
,
314 static void vhost_vsock_common_register_types(void)
316 type_register_static(&vhost_vsock_common_info
);
319 type_init(vhost_vsock_common_register_types
)