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 static void vhost_vsock_get_config(VirtIODevice
*vdev
, uint8_t *config
)
26 VHostVSock
*vsock
= VHOST_VSOCK(vdev
);
27 struct virtio_vsock_config vsockcfg
= {};
29 virtio_stq_p(vdev
, &vsockcfg
.guest_cid
, vsock
->conf
.guest_cid
);
30 memcpy(config
, &vsockcfg
, sizeof(vsockcfg
));
33 static int vhost_vsock_set_guest_cid(VirtIODevice
*vdev
)
35 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
36 VHostVSock
*vsock
= VHOST_VSOCK(vdev
);
37 const VhostOps
*vhost_ops
= vvc
->vhost_dev
.vhost_ops
;
40 if (!vhost_ops
->vhost_vsock_set_guest_cid
) {
44 ret
= vhost_ops
->vhost_vsock_set_guest_cid(&vvc
->vhost_dev
,
45 vsock
->conf
.guest_cid
);
52 static int vhost_vsock_set_running(VirtIODevice
*vdev
, int start
)
54 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
55 const VhostOps
*vhost_ops
= vvc
->vhost_dev
.vhost_ops
;
58 if (!vhost_ops
->vhost_vsock_set_running
) {
62 ret
= vhost_ops
->vhost_vsock_set_running(&vvc
->vhost_dev
, start
);
70 static void vhost_vsock_set_status(VirtIODevice
*vdev
, uint8_t status
)
72 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(vdev
);
73 bool should_start
= status
& VIRTIO_CONFIG_S_DRIVER_OK
;
76 if (!vdev
->vm_running
) {
80 if (vvc
->vhost_dev
.started
== should_start
) {
85 ret
= vhost_vsock_common_start(vdev
);
90 ret
= vhost_vsock_set_running(vdev
, 1);
92 vhost_vsock_common_stop(vdev
);
93 error_report("Error starting vhost vsock: %d", -ret
);
97 ret
= vhost_vsock_set_running(vdev
, 0);
99 error_report("vhost vsock set running failed: %d", ret
);
103 vhost_vsock_common_stop(vdev
);
107 static uint64_t vhost_vsock_get_features(VirtIODevice
*vdev
,
108 uint64_t requested_features
,
111 return vhost_vsock_common_get_features(vdev
, requested_features
, errp
);
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()
122 .pre_save
= vhost_vsock_common_pre_save
,
123 .post_load
= vhost_vsock_common_post_load
,
126 static void vhost_vsock_device_realize(DeviceState
*dev
, Error
**errp
)
128 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(dev
);
129 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
130 VHostVSock
*vsock
= VHOST_VSOCK(dev
);
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");
140 if (vsock
->conf
.guest_cid
> UINT32_MAX
) {
141 error_setg(errp
, "guest-cid property must be a 32-bit number");
145 if (vsock
->conf
.vhostfd
) {
146 vhostfd
= monitor_fd_param(monitor_cur(), vsock
->conf
.vhostfd
, errp
);
148 error_prepend(errp
, "vhost-vsock: unable to parse vhostfd: ");
152 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
153 error_setg_errno(errp
, errno
,
154 "vhost-vsock: unable to set non-blocking mode");
158 vhostfd
= open("/dev/vhost-vsock", O_RDWR
);
160 error_setg_errno(errp
, errno
,
161 "vhost-vsock: failed to open vhost device");
165 if (!g_unix_set_fd_nonblocking(vhostfd
, true, NULL
)) {
166 error_setg_errno(errp
, errno
,
167 "Failed to set FD nonblocking");
172 vhost_vsock_common_realize(vdev
);
174 ret
= vhost_dev_init(&vvc
->vhost_dev
, (void *)(uintptr_t)vhostfd
,
175 VHOST_BACKEND_TYPE_KERNEL
, 0, errp
);
178 * vhostfd is closed by vhost_dev_cleanup, which is called
179 * by vhost_dev_init on initialization error.
184 ret
= vhost_vsock_set_guest_cid(vdev
);
186 error_setg_errno(errp
, -ret
, "vhost-vsock: unable to set guest cid");
193 /* vhost_dev_cleanup() closes the vhostfd passed to vhost_dev_init() */
194 vhost_dev_cleanup(&vvc
->vhost_dev
);
196 vhost_vsock_common_unrealize(vdev
);
199 static void vhost_vsock_device_unrealize(DeviceState
*dev
)
201 VHostVSockCommon
*vvc
= VHOST_VSOCK_COMMON(dev
);
202 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
204 /* This will stop vhost backend if appropriate. */
205 vhost_vsock_set_status(vdev
, 0);
207 vhost_dev_cleanup(&vvc
->vhost_dev
);
208 vhost_vsock_common_unrealize(vdev
);
211 static Property vhost_vsock_properties
[] = {
212 DEFINE_PROP_UINT64("guest-cid", VHostVSock
, conf
.guest_cid
, 0),
213 DEFINE_PROP_STRING("vhostfd", VHostVSock
, conf
.vhostfd
),
214 DEFINE_PROP_END_OF_LIST(),
217 static void vhost_vsock_class_init(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
220 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
222 device_class_set_props(dc
, vhost_vsock_properties
);
223 dc
->vmsd
= &vmstate_virtio_vhost_vsock
;
224 vdc
->realize
= vhost_vsock_device_realize
;
225 vdc
->unrealize
= vhost_vsock_device_unrealize
;
226 vdc
->get_features
= vhost_vsock_get_features
;
227 vdc
->get_config
= vhost_vsock_get_config
;
228 vdc
->set_status
= vhost_vsock_set_status
;
231 static const TypeInfo vhost_vsock_info
= {
232 .name
= TYPE_VHOST_VSOCK
,
233 .parent
= TYPE_VHOST_VSOCK_COMMON
,
234 .instance_size
= sizeof(VHostVSock
),
235 .class_init
= vhost_vsock_class_init
,
238 static void vhost_vsock_register_types(void)
240 type_register_static(&vhost_vsock_info
);
243 type_init(vhost_vsock_register_types
)