hw/display/next-fb: Fix comment typo
[qemu/ar7.git] / hw / virtio / vhost-vsock-common.c
bloba67a275de2d4eed7f940e476af846f394ffa1ac7
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-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"
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);
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);
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);
130 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask);
133 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev,
134 int idx)
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));
150 if (!elem) {
151 error_report("vhost-vsock missed transport reset event");
152 return;
155 if (elem->out_num) {
156 error_report("invalid vhost-vsock event virtqueue element with "
157 "out buffers");
158 goto err;
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");
164 goto err;
167 virtqueue_push(vq, elem, sizeof(event));
168 virtio_notify(VIRTIO_DEVICE(vvc), vq);
170 g_free(elem);
171 return;
173 err:
174 virtqueue_detach_element(vq, elem, 0);
175 g_free(elem);
178 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon *vvc)
180 if (!vvc->post_load_timer) {
181 return;
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));
206 return 0;
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,
223 vvc);
224 timer_mod(vvc->post_load_timer, 1);
226 return 0;
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,
271 ON_OFF_AUTO_AUTO),
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,
292 .abstract = true,
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)