1 /*
2 * Vhost-user vsock virtio device
3 *
4 * Copyright 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
13 #include "qapi/error.h"
14 #include "qemu/error-report.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/qdev-properties-system.h"
17 #include "hw/virtio/vhost-user-vsock.h"
18
19 static const int user_feature_bits[] = {
20 VIRTIO_F_VERSION_1,
21 VIRTIO_RING_F_INDIRECT_DESC,
22 VIRTIO_RING_F_EVENT_IDX,
23 VIRTIO_F_NOTIFY_ON_EMPTY,
24 VIRTIO_F_IN_ORDER,
25 VIRTIO_F_NOTIFICATION_DATA,
26 VHOST_INVALID_FEATURE_BIT
27 };
28
vuv_get_config(VirtIODevice * vdev,uint8_t * config)29 static void vuv_get_config(VirtIODevice *vdev, uint8_t *config)
30 {
31 VHostUserVSock *vsock = VHOST_USER_VSOCK(vdev);
32
33 memcpy(config, &vsock->vsockcfg, sizeof(struct virtio_vsock_config));
34 }
35
vuv_handle_config_change(struct vhost_dev * dev)36 static int vuv_handle_config_change(struct vhost_dev *dev)
37 {
38 VHostUserVSock *vsock = VHOST_USER_VSOCK(dev->vdev);
39 Error *local_err = NULL;
40 int ret = vhost_dev_get_config(dev, (uint8_t *)&vsock->vsockcfg,
41 sizeof(struct virtio_vsock_config),
42 &local_err);
43 if (ret < 0) {
44 error_report_err(local_err);
45 return -1;
46 }
47
48 virtio_notify_config(dev->vdev);
49
50 return 0;
51 }
52
53 const VhostDevConfigOps vsock_ops = {
54 .vhost_dev_config_notifier = vuv_handle_config_change,
55 };
56
vuv_set_status(VirtIODevice * vdev,uint8_t status)57 static int vuv_set_status(VirtIODevice *vdev, uint8_t status)
58 {
59 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
60 bool should_start = virtio_device_should_start(vdev, status);
61 int ret;
62
63 if (vhost_dev_is_started(&vvc->vhost_dev) == should_start) {
64 return 0;
65 }
66
67 if (should_start) {
68 ret = vhost_vsock_common_start(vdev);
69 if (ret < 0) {
70 return ret;
71 }
72 } else {
73 ret = vhost_vsock_common_stop(vdev);
74 if (ret < 0) {
75 return ret;
76 }
77 }
78 return 0;
79 }
80
vuv_get_features(VirtIODevice * vdev,uint64_t features,Error ** errp)81 static uint64_t vuv_get_features(VirtIODevice *vdev,
82 uint64_t features,
83 Error **errp)
84 {
85 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
86
87 features = vhost_get_features(&vvc->vhost_dev, user_feature_bits, features);
88
89 return vhost_vsock_common_get_features(vdev, features, errp);
90 }
91
92 static const VMStateDescription vuv_vmstate = {
93 .name = "vhost-user-vsock",
94 .unmigratable = 1,
95 };
96
vuv_device_realize(DeviceState * dev,Error ** errp)97 static void vuv_device_realize(DeviceState *dev, Error **errp)
98 {
99 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
100 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
101 VHostUserVSock *vsock = VHOST_USER_VSOCK(dev);
102 int ret;
103
104 if (!vsock->conf.chardev.chr) {
105 error_setg(errp, "missing chardev");
106 return;
107 }
108
109 if (!vhost_user_init(&vsock->vhost_user, &vsock->conf.chardev, errp)) {
110 return;
111 }
112
113 vhost_vsock_common_realize(vdev);
114
115 vhost_dev_set_config_notifier(&vvc->vhost_dev, &vsock_ops);
116
117 ret = vhost_dev_init(&vvc->vhost_dev, &vsock->vhost_user,
118 VHOST_BACKEND_TYPE_USER, 0, errp);
119 if (ret < 0) {
120 goto err_virtio;
121 }
122
123 ret = vhost_dev_get_config(&vvc->vhost_dev, (uint8_t *)&vsock->vsockcfg,
124 sizeof(struct virtio_vsock_config), errp);
125 if (ret < 0) {
126 goto err_vhost_dev;
127 }
128
129 return;
130
131 err_vhost_dev:
132 vhost_dev_cleanup(&vvc->vhost_dev);
133 err_virtio:
134 vhost_vsock_common_unrealize(vdev);
135 vhost_user_cleanup(&vsock->vhost_user);
136 }
137
vuv_device_unrealize(DeviceState * dev)138 static void vuv_device_unrealize(DeviceState *dev)
139 {
140 VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
141 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
142 VHostUserVSock *vsock = VHOST_USER_VSOCK(dev);
143
144 /* This will stop vhost backend if appropriate. */
145 vuv_set_status(vdev, 0);
146
147 vhost_dev_cleanup(&vvc->vhost_dev);
148
149 vhost_vsock_common_unrealize(vdev);
150
151 vhost_user_cleanup(&vsock->vhost_user);
152
153 }
154
155 static const Property vuv_properties[] = {
156 DEFINE_PROP_CHR("chardev", VHostUserVSock, conf.chardev),
157 };
158
vuv_class_init(ObjectClass * klass,const void * data)159 static void vuv_class_init(ObjectClass *klass, const void *data)
160 {
161 DeviceClass *dc = DEVICE_CLASS(klass);
162 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
163
164 device_class_set_props(dc, vuv_properties);
165 dc->vmsd = &vuv_vmstate;
166 vdc->realize = vuv_device_realize;
167 vdc->unrealize = vuv_device_unrealize;
168 vdc->get_features = vuv_get_features;
169 vdc->get_config = vuv_get_config;
170 vdc->set_status = vuv_set_status;
171 }
172
173 static const TypeInfo vuv_info = {
174 .name = TYPE_VHOST_USER_VSOCK,
175 .parent = TYPE_VHOST_VSOCK_COMMON,
176 .instance_size = sizeof(VHostUserVSock),
177 .class_init = vuv_class_init,
178 };
179
vuv_register_types(void)180 static void vuv_register_types(void)
181 {
182 type_register_static(&vuv_info);
183 }
184
185 type_init(vuv_register_types)
186