xref: /openbmc/qemu/hw/virtio/vhost-vsock-common.c (revision ab1b2ba9)
1 /*
2  * Parent class for vhost-vsock devices
3  *
4  * Copyright 2015-2020 Red Hat, Inc.
5  *
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  */
10 
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"
21 
22 const int feature_bits[] = {
23     VIRTIO_VSOCK_F_SEQPACKET,
24     VIRTIO_F_RING_RESET,
25     VHOST_INVALID_FEATURE_BIT
26 };
27 
28 uint64_t vhost_vsock_common_get_features(VirtIODevice *vdev, uint64_t features,
29                                          Error **errp)
30 {
31     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
32 
33     if (vvc->seqpacket != ON_OFF_AUTO_OFF) {
34         virtio_add_feature(&features, VIRTIO_VSOCK_F_SEQPACKET);
35     }
36 
37     features = vhost_get_features(&vvc->vhost_dev, feature_bits, features);
38 
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");
42     }
43 
44     return features;
45 }
46 
47 int vhost_vsock_common_start(VirtIODevice *vdev)
48 {
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;
54 
55     if (!k->set_guest_notifiers) {
56         error_report("binding does not support guest notifiers");
57         return -ENOSYS;
58     }
59 
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;
64     }
65 
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;
70     }
71 
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;
77     }
78 
79     /*
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.
83      */
84     for (i = 0; i < vvc->vhost_dev.nvqs; i++) {
85         vhost_virtqueue_mask(&vvc->vhost_dev, vdev, i, false);
86     }
87 
88     return 0;
89 
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;
95 }
96 
97 void vhost_vsock_common_stop(VirtIODevice *vdev)
98 {
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;
103 
104     if (!k->set_guest_notifiers) {
105         return;
106     }
107 
108     vhost_dev_stop(&vvc->vhost_dev, vdev);
109 
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;
114     }
115 
116     vhost_dev_disable_notifiers(&vvc->vhost_dev, vdev);
117 }
118 
119 
120 static void vhost_vsock_common_handle_output(VirtIODevice *vdev, VirtQueue *vq)
121 {
122     /* Do nothing */
123 }
124 
125 static void vhost_vsock_common_guest_notifier_mask(VirtIODevice *vdev, int idx,
126                                             bool mask)
127 {
128     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
129 
130     vhost_virtqueue_mask(&vvc->vhost_dev, vdev, idx, mask);
131 }
132 
133 static bool vhost_vsock_common_guest_notifier_pending(VirtIODevice *vdev,
134                                                int idx)
135 {
136     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
137 
138     return vhost_virtqueue_pending(&vvc->vhost_dev, idx);
139 }
140 
141 static void vhost_vsock_common_send_transport_reset(VHostVSockCommon *vvc)
142 {
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),
147     };
148 
149     elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
150     if (!elem) {
151         error_report("vhost-vsock missed transport reset event");
152         return;
153     }
154 
155     if (elem->out_num) {
156         error_report("invalid vhost-vsock event virtqueue element with "
157                      "out buffers");
158         goto err;
159     }
160 
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;
165     }
166 
167     virtqueue_push(vq, elem, sizeof(event));
168     virtio_notify(VIRTIO_DEVICE(vvc), vq);
169 
170     g_free(elem);
171     return;
172 
173 err:
174     virtqueue_detach_element(vq, elem, 0);
175     g_free(elem);
176 }
177 
178 static void vhost_vsock_common_post_load_timer_cleanup(VHostVSockCommon *vvc)
179 {
180     if (!vvc->post_load_timer) {
181         return;
182     }
183 
184     timer_free(vvc->post_load_timer);
185     vvc->post_load_timer = NULL;
186 }
187 
188 static void vhost_vsock_common_post_load_timer_cb(void *opaque)
189 {
190     VHostVSockCommon *vvc = opaque;
191 
192     vhost_vsock_common_post_load_timer_cleanup(vvc);
193     vhost_vsock_common_send_transport_reset(vvc);
194 }
195 
196 int vhost_vsock_common_pre_save(void *opaque)
197 {
198     VHostVSockCommon *vvc = opaque;
199 
200     /*
201      * At this point, backend must be stopped, otherwise
202      * it might keep writing to memory.
203      */
204     assert(!vhost_dev_is_started(&vvc->vhost_dev));
205 
206     return 0;
207 }
208 
209 int vhost_vsock_common_post_load(void *opaque, int version_id)
210 {
211     VHostVSockCommon *vvc = opaque;
212     VirtIODevice *vdev = VIRTIO_DEVICE(vvc);
213 
214     if (virtio_queue_get_addr(vdev, 2)) {
215         /*
216          * Defer transport reset event to a vm clock timer so that virtqueue
217          * changes happen after migration has completed.
218          */
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);
225     }
226     return 0;
227 }
228 
229 void vhost_vsock_common_realize(VirtIODevice *vdev)
230 {
231     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
232 
233     virtio_init(vdev, VIRTIO_ID_VSOCK, sizeof(struct virtio_vsock_config));
234 
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);
240 
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);
244 
245     vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
246     vvc->vhost_dev.vqs = vvc->vhost_vqs;
247 
248     vvc->post_load_timer = NULL;
249 }
250 
251 void vhost_vsock_common_unrealize(VirtIODevice *vdev)
252 {
253     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
254 
255     vhost_vsock_common_post_load_timer_cleanup(vvc);
256 
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);
261 }
262 
263 static struct vhost_dev *vhost_vsock_common_get_vhost(VirtIODevice *vdev)
264 {
265     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
266     return &vvc->vhost_dev;
267 }
268 
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(),
273 };
274 
275 static void vhost_vsock_common_class_init(ObjectClass *klass, void *data)
276 {
277     DeviceClass *dc = DEVICE_CLASS(klass);
278     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
279 
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;
285 }
286 
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,
293 };
294 
295 static void vhost_vsock_common_register_types(void)
296 {
297     type_register_static(&vhost_vsock_common_info);
298 }
299 
300 type_init(vhost_vsock_common_register_types)
301