ui/vnc: fix handling of VNC_FEATURE_XVP
[qemu/kevin.git] / hw / virtio / vhost-vsock-common.c
blob12ea87d7a7067463fe0e43b625a252f1cc4ff114
1 /*
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
8 * top-level directory.
9 */
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"
19 #include "qemu/iov.h"
20 #include "monitor/monitor.h"
22 const int feature_bits[] = {
23 VIRTIO_VSOCK_F_SEQPACKET,
24 VIRTIO_F_RING_RESET,
25 VHOST_INVALID_FEATURE_BIT
28 uint64_t vhost_vsock_common_get_features(VirtIODevice *vdev, uint64_t features,
29 Error **errp)
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");
44 return features;
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);
52 int ret;
53 int i;
55 if (!k->set_guest_notifiers) {
56 error_report("binding does not support guest notifiers");
57 return -ENOSYS;
60 ret = vhost_dev_enable_notifiers(&vvc->vhost_dev, vdev);
61 if (ret < 0) {
62 error_report("Error enabling host notifiers: %d", -ret);
63 return ret;
66 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, true);
67 if (ret < 0) {
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);
74 if (ret < 0) {
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);
88 return 0;
90 err_guest_notifiers:
91 k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false);
92 err_host_notifiers:
93 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev);
94 return ret;
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);
102 int ret;
104 if (!k->set_guest_notifiers) {
105 return;
108 vhost_dev_stop(&vvc->vhost_dev, vdev, true);
110 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false);
111 if (ret < 0) {
112 error_report("vhost guest notifier cleanup failed: %d", ret);
113 return;
116 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev);
120 static void vhost_vsock_common_handle_output(VirtIODevice *vdev, VirtQueue *vq)
122 /* Do nothing */
125 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice *vdev, int idx,
126 bool mask)
128 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
131 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
132 * as the macro of configure interrupt's IDX, If this driver does not
133 * support, the function will return
136 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
137 return;
139 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask);
142 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev,
143 int idx)
145 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
148 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
149 * as the macro of configure interrupt's IDX, If this driver does not
150 * support, the function will return
153 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
154 return false;
156 return vhost_virtqueue_pending(&vvc->vhost_dev, idx);
159 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon *vvc)
161 VirtQueueElement *elem;
162 VirtQueue *vq = vvc->event_vq;
163 struct virtio_vsock_event event = {
164 .id = cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET),
167 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
168 if (!elem) {
169 error_report("vhost-vsock missed transport reset event");
170 return;
173 if (elem->out_num) {
174 error_report("invalid vhost-vsock event virtqueue element with "
175 "out buffers");
176 goto err;
179 if (iov_from_buf(elem->in_sg, elem->in_num, 0,
180 &event, sizeof(event)) != sizeof(event)) {
181 error_report("vhost-vsock event virtqueue element is too short");
182 goto err;
185 virtqueue_push(vq, elem, sizeof(event));
186 virtio_notify(VIRTIO_DEVICE(vvc), vq);
188 g_free(elem);
189 return;
191 err:
192 virtqueue_detach_element(vq, elem, 0);
193 g_free(elem);
196 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon *vvc)
198 if (!vvc->post_load_timer) {
199 return;
202 timer_free(vvc->post_load_timer);
203 vvc->post_load_timer = NULL;
206 static void vhost_vsock_common_post_load_timer_cb(void *opaque)
208 VHostVSockCommon *vvc = opaque;
210 vhost_vsock_common_post_load_timer_cleanup(vvc);
211 vhost_vsock_common_send_transport_reset(vvc);
214 int vhost_vsock_common_pre_save(void *opaque)
216 VHostVSockCommon *vvc = opaque;
219 * At this point, backend must be stopped, otherwise
220 * it might keep writing to memory.
222 assert(!vhost_dev_is_started(&vvc->vhost_dev));
224 return 0;
227 int vhost_vsock_common_post_load(void *opaque, int version_id)
229 VHostVSockCommon *vvc = opaque;
230 VirtIODevice *vdev = VIRTIO_DEVICE(vvc);
232 if (virtio_queue_get_addr(vdev, 2)) {
234 * Defer transport reset event to a vm clock timer so that virtqueue
235 * changes happen after migration has completed.
237 assert(!vvc->post_load_timer);
238 vvc->post_load_timer =
239 timer_new_ns(QEMU_CLOCK_VIRTUAL,
240 vhost_vsock_common_post_load_timer_cb,
241 vvc);
242 timer_mod(vvc->post_load_timer, 1);
244 return 0;
247 void vhost_vsock_common_realize(VirtIODevice *vdev)
249 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
251 virtio_init(vdev, VIRTIO_ID_VSOCK, sizeof(struct virtio_vsock_config));
253 /* Receive and transmit queues belong to vhost */
254 vvc->recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
255 vhost_vsock_common_handle_output);
256 vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
257 vhost_vsock_common_handle_output);
259 /* The event queue belongs to QEMU */
260 vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
261 vhost_vsock_common_handle_output);
263 vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
264 vvc->vhost_dev.vqs = vvc->vhost_vqs;
266 vvc->post_load_timer = NULL;
269 void vhost_vsock_common_unrealize(VirtIODevice *vdev)
271 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
273 vhost_vsock_common_post_load_timer_cleanup(vvc);
275 virtio_delete_queue(vvc->recv_vq);
276 virtio_delete_queue(vvc->trans_vq);
277 virtio_delete_queue(vvc->event_vq);
278 virtio_cleanup(vdev);
281 static struct vhost_dev *vhost_vsock_common_get_vhost(VirtIODevice *vdev)
283 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
284 return &vvc->vhost_dev;
287 static Property vhost_vsock_common_properties[] = {
288 DEFINE_PROP_ON_OFF_AUTO("seqpacket", VHostVSockCommon, seqpacket,
289 ON_OFF_AUTO_AUTO),
290 DEFINE_PROP_END_OF_LIST(),
293 static void vhost_vsock_common_class_init(ObjectClass *klass, void *data)
295 DeviceClass *dc = DEVICE_CLASS(klass);
296 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
298 device_class_set_props(dc, vhost_vsock_common_properties);
299 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
300 vdc->guest_notifier_mask = vhost_vsock_common_guest_notifier_mask;
301 vdc->guest_notifier_pending = vhost_vsock_common_guest_notifier_pending;
302 vdc->get_vhost = vhost_vsock_common_get_vhost;
305 static const TypeInfo vhost_vsock_common_info = {
306 .name = TYPE_VHOST_VSOCK_COMMON,
307 .parent = TYPE_VIRTIO_DEVICE,
308 .instance_size = sizeof(VHostVSockCommon),
309 .class_init = vhost_vsock_common_class_init,
310 .abstract = true,
313 static void vhost_vsock_common_register_types(void)
315 type_register_static(&vhost_vsock_common_info);
318 type_init(vhost_vsock_common_register_types)