xref: /openbmc/qemu/hw/virtio/vhost-vsock.c (revision f7160f32)
1 /*
2  * Virtio vsock device
3  *
4  * Copyright 2015 Red Hat, Inc.
5  *
6  * Authors:
7  *  Stefan Hajnoczi <stefanha@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * (at your option) any later version.  See the COPYING file in the
11  * top-level directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "standard-headers/linux/virtio_vsock.h"
16 #include "qapi/error.h"
17 #include "hw/virtio/virtio-access.h"
18 #include "qemu/error-report.h"
19 #include "hw/qdev-properties.h"
20 #include "hw/virtio/vhost-vsock.h"
21 #include "monitor/monitor.h"
22 
23 static void vhost_vsock_get_config(VirtIODevice *vdev, uint8_t *config)
24 {
25     VHostVSock *vsock = VHOST_VSOCK(vdev);
26     struct virtio_vsock_config vsockcfg = {};
27 
28     virtio_stq_p(vdev, &vsockcfg.guest_cid, vsock->conf.guest_cid);
29     memcpy(config, &vsockcfg, sizeof(vsockcfg));
30 }
31 
32 static int vhost_vsock_set_guest_cid(VirtIODevice *vdev)
33 {
34     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
35     VHostVSock *vsock = VHOST_VSOCK(vdev);
36     const VhostOps *vhost_ops = vvc->vhost_dev.vhost_ops;
37     int ret;
38 
39     if (!vhost_ops->vhost_vsock_set_guest_cid) {
40         return -ENOSYS;
41     }
42 
43     ret = vhost_ops->vhost_vsock_set_guest_cid(&vvc->vhost_dev,
44                                                vsock->conf.guest_cid);
45     if (ret < 0) {
46         return -errno;
47     }
48     return 0;
49 }
50 
51 static int vhost_vsock_set_running(VirtIODevice *vdev, int start)
52 {
53     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
54     const VhostOps *vhost_ops = vvc->vhost_dev.vhost_ops;
55     int ret;
56 
57     if (!vhost_ops->vhost_vsock_set_running) {
58         return -ENOSYS;
59     }
60 
61     ret = vhost_ops->vhost_vsock_set_running(&vvc->vhost_dev, start);
62     if (ret < 0) {
63         return -errno;
64     }
65     return 0;
66 }
67 
68 
69 static void vhost_vsock_set_status(VirtIODevice *vdev, uint8_t status)
70 {
71     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
72     bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
73     int ret;
74 
75     if (!vdev->vm_running) {
76         should_start = false;
77     }
78 
79     if (vvc->vhost_dev.started == should_start) {
80         return;
81     }
82 
83     if (should_start) {
84         ret = vhost_vsock_common_start(vdev);
85         if (ret < 0) {
86             return;
87         }
88 
89         ret = vhost_vsock_set_running(vdev, 1);
90         if (ret < 0) {
91             vhost_vsock_common_stop(vdev);
92             error_report("Error starting vhost vsock: %d", -ret);
93             return;
94         }
95     } else {
96         ret = vhost_vsock_set_running(vdev, 0);
97         if (ret < 0) {
98             error_report("vhost vsock set running failed: %d", ret);
99             return;
100         }
101 
102         vhost_vsock_common_stop(vdev);
103     }
104 }
105 
106 static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
107                                          uint64_t requested_features,
108                                          Error **errp)
109 {
110     /* No feature bits used yet */
111     return requested_features;
112 }
113 
114 static const VMStateDescription vmstate_virtio_vhost_vsock = {
115     .name = "virtio-vhost_vsock",
116     .minimum_version_id = VHOST_VSOCK_SAVEVM_VERSION,
117     .version_id = VHOST_VSOCK_SAVEVM_VERSION,
118     .fields = (VMStateField[]) {
119         VMSTATE_VIRTIO_DEVICE,
120         VMSTATE_END_OF_LIST()
121     },
122     .pre_save = vhost_vsock_common_pre_save,
123     .post_load = vhost_vsock_common_post_load,
124 };
125 
126 static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
127 {
128     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
129     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
130     VHostVSock *vsock = VHOST_VSOCK(dev);
131     int vhostfd;
132     int ret;
133 
134     /* Refuse to use reserved CID numbers */
135     if (vsock->conf.guest_cid <= 2) {
136         error_setg(errp, "guest-cid property must be greater than 2");
137         return;
138     }
139 
140     if (vsock->conf.guest_cid > UINT32_MAX) {
141         error_setg(errp, "guest-cid property must be a 32-bit number");
142         return;
143     }
144 
145     if (vsock->conf.vhostfd) {
146         vhostfd = monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp);
147         if (vhostfd == -1) {
148             error_prepend(errp, "vhost-vsock: unable to parse vhostfd: ");
149             return;
150         }
151     } else {
152         vhostfd = open("/dev/vhost-vsock", O_RDWR);
153         if (vhostfd < 0) {
154             error_setg_errno(errp, errno,
155                              "vhost-vsock: failed to open vhost device");
156             return;
157         }
158     }
159 
160     vhost_vsock_common_realize(vdev, "vhost-vsock");
161 
162     ret = vhost_dev_init(&vvc->vhost_dev, (void *)(uintptr_t)vhostfd,
163                          VHOST_BACKEND_TYPE_KERNEL, 0);
164     if (ret < 0) {
165         error_setg_errno(errp, -ret, "vhost-vsock: vhost_dev_init failed");
166         goto err_virtio;
167     }
168 
169     ret = vhost_vsock_set_guest_cid(vdev);
170     if (ret < 0) {
171         error_setg_errno(errp, -ret, "vhost-vsock: unable to set guest cid");
172         goto err_vhost_dev;
173     }
174 
175     return;
176 
177 err_vhost_dev:
178     vhost_dev_cleanup(&vvc->vhost_dev);
179     /* vhost_dev_cleanup() closes the vhostfd passed to vhost_dev_init() */
180     vhostfd = -1;
181 err_virtio:
182     vhost_vsock_common_unrealize(vdev);
183     if (vhostfd >= 0) {
184         close(vhostfd);
185     }
186     return;
187 }
188 
189 static void vhost_vsock_device_unrealize(DeviceState *dev)
190 {
191     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
192     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
193 
194     /* This will stop vhost backend if appropriate. */
195     vhost_vsock_set_status(vdev, 0);
196 
197     vhost_dev_cleanup(&vvc->vhost_dev);
198     vhost_vsock_common_unrealize(vdev);
199 }
200 
201 static Property vhost_vsock_properties[] = {
202     DEFINE_PROP_UINT64("guest-cid", VHostVSock, conf.guest_cid, 0),
203     DEFINE_PROP_STRING("vhostfd", VHostVSock, conf.vhostfd),
204     DEFINE_PROP_END_OF_LIST(),
205 };
206 
207 static void vhost_vsock_class_init(ObjectClass *klass, void *data)
208 {
209     DeviceClass *dc = DEVICE_CLASS(klass);
210     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
211 
212     device_class_set_props(dc, vhost_vsock_properties);
213     dc->vmsd = &vmstate_virtio_vhost_vsock;
214     vdc->realize = vhost_vsock_device_realize;
215     vdc->unrealize = vhost_vsock_device_unrealize;
216     vdc->get_features = vhost_vsock_get_features;
217     vdc->get_config = vhost_vsock_get_config;
218     vdc->set_status = vhost_vsock_set_status;
219 }
220 
221 static const TypeInfo vhost_vsock_info = {
222     .name = TYPE_VHOST_VSOCK,
223     .parent = TYPE_VHOST_VSOCK_COMMON,
224     .instance_size = sizeof(VHostVSock),
225     .class_init = vhost_vsock_class_init,
226 };
227 
228 static void vhost_vsock_register_types(void)
229 {
230     type_register_static(&vhost_vsock_info);
231 }
232 
233 type_init(vhost_vsock_register_types)
234