4 * Copyright 2015 Red Hat, Inc.
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.
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 "qemu/sockets.h"
20 #include "hw/qdev-properties.h"
21 #include "hw/virtio/vhost-vsock.h"
22 #include "monitor/monitor.h"
24 const int feature_bits
[] = {
25 VIRTIO_VSOCK_F_SEQPACKET
,
26 VHOST_INVALID_FEATURE_BIT
29 static void vhost_vsock_get_config(VirtIODevice
*vdev
, uint8_t *config
)
31 VHostVSock
*vsock
= VHOST_VSOCK(vdev
);
32 struct virtio_vsock_config vsockcfg
= {};
34 virtio_stq_p(vdev
, &vsockcfg
.guest_cid
, vsock
->conf
.guest_cid
);
35 memcpy(config
, &vsockcfg
, sizeof(vsockcfg
));
38 static int vhost_vsock_set_guest_cid(VirtIODevice
*vdev
)
40 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
41 VHostVSock
*vsock
= VHOST_VSOCK(vdev
);
42 const VhostOps
*vhost_ops
= vvc
->vhost_dev
.vhost_ops
;
45 if (!vhost_ops
->vhost_vsock_set_guest_cid
) {
49 ret
= vhost_ops
->vhost_vsock_set_guest_cid(&vvc
->vhost_dev
,
50 vsock
->conf
.guest_cid
);
57 static int vhost_vsock_set_running(VirtIODevice
*vdev
, int start
)
59 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
60 const VhostOps
*vhost_ops
= vvc
->vhost_dev
.vhost_ops
;
63 if (!vhost_ops
->vhost_vsock_set_running
) {
67 ret
= vhost_ops
->vhost_vsock_set_running(&vvc
->vhost_dev
, start
);
75 static void vhost_vsock_set_status(VirtIODevice
*vdev
, uint8_t status
)
77 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
78 bool should_start
= status
& VIRTIO_CONFIG_S_DRIVER_OK
;
81 if (!vdev
->vm_running
) {
85 if (vvc
->vhost_dev
.started
== should_start
) {
90 ret
= vhost_vsock_common_start(vdev
);
95 ret
= vhost_vsock_set_running(vdev
, 1);
97 vhost_vsock_common_stop(vdev
);
98 error_report("Error starting vhost vsock: %d", -ret
);
102 ret
= vhost_vsock_set_running(vdev
, 0);
104 error_report("vhost vsock set running failed: %d", ret
);
108 vhost_vsock_common_stop(vdev
);
112 static uint64_t vhost_vsock_get_features(VirtIODevice
*vdev
,
113 uint64_t requested_features
,
116 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
118 virtio_add_feature(&requested_features
, VIRTIO_VSOCK_F_SEQPACKET
);
119 return vhost_get_features(&vvc
->vhost_dev
, feature_bits
,
123 static const VMStateDescription vmstate_virtio_vhost_vsock
= {
124 .name
= "virtio-vhost_vsock",
125 .minimum_version_id
= VHOST_VSOCK_SAVEVM_VERSION
,
126 .version_id
= VHOST_VSOCK_SAVEVM_VERSION
,
127 .fields
= (VMStateField
[]) {
128 VMSTATE_VIRTIO_DEVICE
,
129 VMSTATE_END_OF_LIST()
131 .pre_save
= vhost_vsock_common_pre_save
,
132 .post_load
= vhost_vsock_common_post_load
,
135 static void vhost_vsock_device_realize(DeviceState
*dev
, Error
**errp
)
137 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(dev
);
138 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
139 VHostVSock
*vsock
= VHOST_VSOCK(dev
);
143 /* Refuse to use reserved CID numbers */
144 if (vsock
->conf
.guest_cid
<= 2) {
145 error_setg(errp
, "guest-cid property must be greater than 2");
149 if (vsock
->conf
.guest_cid
> UINT32_MAX
) {
150 error_setg(errp
, "guest-cid property must be a 32-bit number");
154 if (vsock
->conf
.vhostfd
) {
155 vhostfd
= monitor_fd_param(monitor_cur(), vsock
->conf
.vhostfd
, errp
);
157 error_prepend(errp
, "vhost-vsock: unable to parse vhostfd: ");
161 ret
= qemu_try_set_nonblock(vhostfd
);
163 error_setg_errno(errp
, -ret
,
164 "vhost-vsock: unable to set non-blocking mode");
168 vhostfd
= open("/dev/vhost-vsock", O_RDWR
);
170 error_setg_errno(errp
, errno
,
171 "vhost-vsock: failed to open vhost device");
175 qemu_set_nonblock(vhostfd
);
178 vhost_vsock_common_realize(vdev
, "vhost-vsock");
180 ret
= vhost_dev_init(&vvc
->vhost_dev
, (void *)(uintptr_t)vhostfd
,
181 VHOST_BACKEND_TYPE_KERNEL
, 0, errp
);
186 ret
= vhost_vsock_set_guest_cid(vdev
);
188 error_setg_errno(errp
, -ret
, "vhost-vsock: unable to set guest cid");
195 vhost_dev_cleanup(&vvc
->vhost_dev
);
196 /* vhost_dev_cleanup() closes the vhostfd passed to vhost_dev_init() */
199 vhost_vsock_common_unrealize(vdev
);
206 static void vhost_vsock_device_unrealize(DeviceState
*dev
)
208 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(dev
);
209 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
211 /* This will stop vhost backend if appropriate. */
212 vhost_vsock_set_status(vdev
, 0);
214 vhost_dev_cleanup(&vvc
->vhost_dev
);
215 vhost_vsock_common_unrealize(vdev
);
218 static Property vhost_vsock_properties
[] = {
219 DEFINE_PROP_UINT64("guest-cid", VHostVSock
, conf
.guest_cid
, 0),
220 DEFINE_PROP_STRING("vhostfd", VHostVSock
, conf
.vhostfd
),
221 DEFINE_PROP_END_OF_LIST(),
224 static void vhost_vsock_class_init(ObjectClass
*klass
, void *data
)
226 DeviceClass
*dc
= DEVICE_CLASS(klass
);
227 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
229 device_class_set_props(dc
, vhost_vsock_properties
);
230 dc
->vmsd
= &vmstate_virtio_vhost_vsock
;
231 vdc
->realize
= vhost_vsock_device_realize
;
232 vdc
->unrealize
= vhost_vsock_device_unrealize
;
233 vdc
->get_features
= vhost_vsock_get_features
;
234 vdc
->get_config
= vhost_vsock_get_config
;
235 vdc
->set_status
= vhost_vsock_set_status
;
238 static const TypeInfo vhost_vsock_info
= {
239 .name
= TYPE_VHOST_VSOCK
,
240 .parent
= TYPE_VHOST_VSOCK_COMMON
,
241 .instance_size
= sizeof(VHostVSock
),
242 .class_init
= vhost_vsock_class_init
,
245 static void vhost_vsock_register_types(void)
247 type_register_static(&vhost_vsock_info
);
250 type_init(vhost_vsock_register_types
)