exec/memory: Use struct Object typedef
[qemu/ar7.git] / hw / virtio / vhost-vsock-common.c
blob4ad6e234adfc0b1eeb860def23a05ca36a29b919
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-vsock.h"
18 #include "qemu/iov.h"
19 #include "monitor/monitor.h"
21 int vhost_vsock_common_start(VirtIODevice *vdev)
23 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
24 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
25 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
26 int ret;
27 int i;
29 if (!k->set_guest_notifiers) {
30 error_report("binding does not support guest notifiers");
31 return -ENOSYS;
34 ret = vhost_dev_enable_notifiers(&vvc->vhost_dev, vdev);
35 if (ret < 0) {
36 error_report("Error enabling host notifiers: %d", -ret);
37 return ret;
40 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, true);
41 if (ret < 0) {
42 error_report("Error binding guest notifier: %d", -ret);
43 goto err_host_notifiers;
46 vvc->vhost_dev.acked_features = vdev->guest_features;
47 ret = vhost_dev_start(&vvc->vhost_dev, vdev);
48 if (ret < 0) {
49 error_report("Error starting vhost: %d", -ret);
50 goto err_guest_notifiers;
54 * guest_notifier_mask/pending not used yet, so just unmask
55 * everything here. virtio-pci will do the right thing by
56 * enabling/disabling irqfd.
58 for (i = 0; i < vvc->vhost_dev.nvqs; i++) {
59 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, i, false);
62 return 0;
64 err_guest_notifiers:
65 k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false);
66 err_host_notifiers:
67 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev);
68 return ret;
71 void vhost_vsock_common_stop(VirtIODevice *vdev)
73 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
74 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
75 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
76 int ret;
78 if (!k->set_guest_notifiers) {
79 return;
82 vhost_dev_stop(&vvc->vhost_dev, vdev);
84 ret = k->set_guest_notifiers(qbus->parent, vvc->vhost_dev.nvqs, false);
85 if (ret < 0) {
86 error_report("vhost guest notifier cleanup failed: %d", ret);
87 return;
90 vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev);
94 static void vhost_vsock_common_handle_output(VirtIODevice *vdev, VirtQueue *vq)
96 /* Do nothing */
99 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice *vdev, int idx,
100 bool mask)
102 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
104 vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask);
107 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev,
108 int idx)
110 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
112 return vhost_virtqueue_pending(&vvc->vhost_dev, idx);
115 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon *vvc)
117 VirtQueueElement *elem;
118 VirtQueue *vq = vvc->event_vq;
119 struct virtio_vsock_event event = {
120 .id = cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET),
123 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
124 if (!elem) {
125 error_report("vhost-vsock missed transport reset event");
126 return;
129 if (elem->out_num) {
130 error_report("invalid vhost-vsock event virtqueue element with "
131 "out buffers");
132 goto out;
135 if (iov_from_buf(elem->in_sg, elem->in_num, 0,
136 &event, sizeof(event)) != sizeof(event)) {
137 error_report("vhost-vsock event virtqueue element is too short");
138 goto out;
141 virtqueue_push(vq, elem, sizeof(event));
142 virtio_notify(VIRTIO_DEVICE(vvc), vq);
144 out:
145 g_free(elem);
148 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon *vvc)
150 if (!vvc->post_load_timer) {
151 return;
154 timer_free(vvc->post_load_timer);
155 vvc->post_load_timer = NULL;
158 static void vhost_vsock_common_post_load_timer_cb(void *opaque)
160 VHostVSockCommon *vvc = opaque;
162 vhost_vsock_common_post_load_timer_cleanup(vvc);
163 vhost_vsock_common_send_transport_reset(vvc);
166 int vhost_vsock_common_pre_save(void *opaque)
168 VHostVSockCommon *vvc = opaque;
171 * At this point, backend must be stopped, otherwise
172 * it might keep writing to memory.
174 assert(!vvc->vhost_dev.started);
176 return 0;
179 int vhost_vsock_common_post_load(void *opaque, int version_id)
181 VHostVSockCommon *vvc = opaque;
182 VirtIODevice *vdev = VIRTIO_DEVICE(vvc);
184 if (virtio_queue_get_addr(vdev, 2)) {
186 * Defer transport reset event to a vm clock timer so that virtqueue
187 * changes happen after migration has completed.
189 assert(!vvc->post_load_timer);
190 vvc->post_load_timer =
191 timer_new_ns(QEMU_CLOCK_VIRTUAL,
192 vhost_vsock_common_post_load_timer_cb,
193 vvc);
194 timer_mod(vvc->post_load_timer, 1);
196 return 0;
199 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
201 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
203 virtio_init(vdev, name, VIRTIO_ID_VSOCK,
204 sizeof(struct virtio_vsock_config));
206 /* Receive and transmit queues belong to vhost */
207 vvc->recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
208 vhost_vsock_common_handle_output);
209 vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
210 vhost_vsock_common_handle_output);
212 /* The event queue belongs to QEMU */
213 vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
214 vhost_vsock_common_handle_output);
216 vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
217 vvc->vhost_dev.vqs = vvc->vhost_vqs;
219 vvc->post_load_timer = NULL;
222 void vhost_vsock_common_unrealize(VirtIODevice *vdev)
224 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
226 vhost_vsock_common_post_load_timer_cleanup(vvc);
228 virtio_delete_queue(vvc->recv_vq);
229 virtio_delete_queue(vvc->trans_vq);
230 virtio_delete_queue(vvc->event_vq);
231 virtio_cleanup(vdev);
234 static void vhost_vsock_common_class_init(ObjectClass *klass, void *data)
236 DeviceClass *dc = DEVICE_CLASS(klass);
237 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
239 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
240 vdc->guest_notifier_mask = vhost_vsock_common_guest_notifier_mask;
241 vdc->guest_notifier_pending = vhost_vsock_common_guest_notifier_pending;
244 static const TypeInfo vhost_vsock_common_info = {
245 .name = TYPE_VHOST_VSOCK_COMMON,
246 .parent = TYPE_VIRTIO_DEVICE,
247 .instance_size = sizeof(VHostVSockCommon),
248 .class_init = vhost_vsock_common_class_init,
249 .abstract = true,
252 static void vhost_vsock_common_register_types(void)
254 type_register_static(&vhost_vsock_common_info);
257 type_init(vhost_vsock_common_register_types)