monitor/qmp: Update comment for commit 4eaca8de268
[qemu/armbru.git] / hw / virtio / vhost-backend.c
blob48905383f8e2730a06bc6b3303f6aa75c7cf3f67
1 /*
2 * vhost-backend
4 * Copyright (c) 2013 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "hw/virtio/vhost.h"
13 #include "hw/virtio/vhost-backend.h"
14 #include "qemu/error-report.h"
15 #include "qemu/main-loop.h"
16 #include "standard-headers/linux/vhost_types.h"
18 #ifdef CONFIG_VHOST_KERNEL
19 #include <linux/vhost.h>
20 #include <sys/ioctl.h>
22 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
23 void *arg)
25 int fd = (uintptr_t) dev->opaque;
27 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
29 return ioctl(fd, request, arg);
32 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque)
34 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
36 dev->opaque = opaque;
38 return 0;
41 static int vhost_kernel_cleanup(struct vhost_dev *dev)
43 int fd = (uintptr_t) dev->opaque;
45 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
47 return close(fd);
50 static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
52 int limit = 64;
53 char *s;
55 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
56 &s, NULL, NULL)) {
57 uint64_t val = g_ascii_strtoull(s, NULL, 10);
58 if (!((val == G_MAXUINT64 || !val) && errno)) {
59 g_free(s);
60 return val;
62 error_report("ignoring invalid max_mem_regions value in vhost module:"
63 " %s", s);
65 g_free(s);
66 return limit;
69 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
70 struct vhost_vring_file *file)
72 return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file);
75 static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev,
76 struct vhost_scsi_target *target)
78 return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target);
81 static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev,
82 struct vhost_scsi_target *target)
84 return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target);
87 static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version)
89 return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version);
92 static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base,
93 struct vhost_log *log)
95 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
98 static int vhost_kernel_set_mem_table(struct vhost_dev *dev,
99 struct vhost_memory *mem)
101 return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem);
104 static int vhost_kernel_set_vring_addr(struct vhost_dev *dev,
105 struct vhost_vring_addr *addr)
107 return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr);
110 static int vhost_kernel_set_vring_endian(struct vhost_dev *dev,
111 struct vhost_vring_state *ring)
113 return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring);
116 static int vhost_kernel_set_vring_num(struct vhost_dev *dev,
117 struct vhost_vring_state *ring)
119 return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring);
122 static int vhost_kernel_set_vring_base(struct vhost_dev *dev,
123 struct vhost_vring_state *ring)
125 return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring);
128 static int vhost_kernel_get_vring_base(struct vhost_dev *dev,
129 struct vhost_vring_state *ring)
131 return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring);
134 static int vhost_kernel_set_vring_kick(struct vhost_dev *dev,
135 struct vhost_vring_file *file)
137 return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file);
140 static int vhost_kernel_set_vring_call(struct vhost_dev *dev,
141 struct vhost_vring_file *file)
143 return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file);
146 static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,
147 struct vhost_vring_state *s)
149 return vhost_kernel_call(dev, VHOST_SET_VRING_BUSYLOOP_TIMEOUT, s);
152 static int vhost_kernel_set_features(struct vhost_dev *dev,
153 uint64_t features)
155 return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features);
158 static int vhost_kernel_get_features(struct vhost_dev *dev,
159 uint64_t *features)
161 return vhost_kernel_call(dev, VHOST_GET_FEATURES, features);
164 static int vhost_kernel_set_owner(struct vhost_dev *dev)
166 return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
169 static int vhost_kernel_reset_device(struct vhost_dev *dev)
171 return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
174 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
176 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
178 return idx - dev->vq_index;
181 #ifdef CONFIG_VHOST_VSOCK
182 static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev *dev,
183 uint64_t guest_cid)
185 return vhost_kernel_call(dev, VHOST_VSOCK_SET_GUEST_CID, &guest_cid);
188 static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start)
190 return vhost_kernel_call(dev, VHOST_VSOCK_SET_RUNNING, &start);
192 #endif /* CONFIG_VHOST_VSOCK */
194 static void vhost_kernel_iotlb_read(void *opaque)
196 struct vhost_dev *dev = opaque;
197 struct vhost_msg msg;
198 ssize_t len;
200 while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
201 if (len < sizeof msg) {
202 error_report("Wrong vhost message len: %d", (int)len);
203 break;
205 if (msg.type != VHOST_IOTLB_MSG) {
206 error_report("Unknown vhost iotlb message type");
207 break;
210 vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
214 static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev *dev,
215 struct vhost_iotlb_msg *imsg)
217 struct vhost_msg msg;
219 msg.type = VHOST_IOTLB_MSG;
220 msg.iotlb = *imsg;
222 if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
223 error_report("Fail to update device iotlb");
224 return -EFAULT;
227 return 0;
230 static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
231 int enabled)
233 if (enabled)
234 qemu_set_fd_handler((uintptr_t)dev->opaque,
235 vhost_kernel_iotlb_read, NULL, dev);
236 else
237 qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
240 static const VhostOps kernel_ops = {
241 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
242 .vhost_backend_init = vhost_kernel_init,
243 .vhost_backend_cleanup = vhost_kernel_cleanup,
244 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
245 .vhost_net_set_backend = vhost_kernel_net_set_backend,
246 .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
247 .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
248 .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version,
249 .vhost_set_log_base = vhost_kernel_set_log_base,
250 .vhost_set_mem_table = vhost_kernel_set_mem_table,
251 .vhost_set_vring_addr = vhost_kernel_set_vring_addr,
252 .vhost_set_vring_endian = vhost_kernel_set_vring_endian,
253 .vhost_set_vring_num = vhost_kernel_set_vring_num,
254 .vhost_set_vring_base = vhost_kernel_set_vring_base,
255 .vhost_get_vring_base = vhost_kernel_get_vring_base,
256 .vhost_set_vring_kick = vhost_kernel_set_vring_kick,
257 .vhost_set_vring_call = vhost_kernel_set_vring_call,
258 .vhost_set_vring_busyloop_timeout =
259 vhost_kernel_set_vring_busyloop_timeout,
260 .vhost_set_features = vhost_kernel_set_features,
261 .vhost_get_features = vhost_kernel_get_features,
262 .vhost_set_owner = vhost_kernel_set_owner,
263 .vhost_reset_device = vhost_kernel_reset_device,
264 .vhost_get_vq_index = vhost_kernel_get_vq_index,
265 #ifdef CONFIG_VHOST_VSOCK
266 .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
267 .vhost_vsock_set_running = vhost_kernel_vsock_set_running,
268 #endif /* CONFIG_VHOST_VSOCK */
269 .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
270 .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
272 #endif
274 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
276 int r = 0;
278 switch (backend_type) {
279 #ifdef CONFIG_VHOST_KERNEL
280 case VHOST_BACKEND_TYPE_KERNEL:
281 dev->vhost_ops = &kernel_ops;
282 break;
283 #endif
284 #ifdef CONFIG_VHOST_USER
285 case VHOST_BACKEND_TYPE_USER:
286 dev->vhost_ops = &user_ops;
287 break;
288 #endif
289 default:
290 error_report("Unknown vhost backend type");
291 r = -1;
294 return r;
297 int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
298 uint64_t iova, uint64_t uaddr,
299 uint64_t len,
300 IOMMUAccessFlags perm)
302 struct vhost_iotlb_msg imsg;
304 imsg.iova = iova;
305 imsg.uaddr = uaddr;
306 imsg.size = len;
307 imsg.type = VHOST_IOTLB_UPDATE;
309 switch (perm) {
310 case IOMMU_RO:
311 imsg.perm = VHOST_ACCESS_RO;
312 break;
313 case IOMMU_WO:
314 imsg.perm = VHOST_ACCESS_WO;
315 break;
316 case IOMMU_RW:
317 imsg.perm = VHOST_ACCESS_RW;
318 break;
319 default:
320 return -EINVAL;
323 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
324 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
326 return -ENODEV;
329 int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
330 uint64_t iova, uint64_t len)
332 struct vhost_iotlb_msg imsg;
334 imsg.iova = iova;
335 imsg.size = len;
336 imsg.type = VHOST_IOTLB_INVALIDATE;
338 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
339 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
341 return -ENODEV;
344 int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
345 struct vhost_iotlb_msg *imsg)
347 int ret = 0;
349 switch (imsg->type) {
350 case VHOST_IOTLB_MISS:
351 ret = vhost_device_iotlb_miss(dev, imsg->iova,
352 imsg->perm != VHOST_ACCESS_RO);
353 break;
354 case VHOST_IOTLB_ACCESS_FAIL:
355 /* FIXME: report device iotlb error */
356 error_report("Access failure IOTLB message type not supported");
357 ret = -ENOTSUP;
358 break;
359 case VHOST_IOTLB_UPDATE:
360 case VHOST_IOTLB_INVALIDATE:
361 default:
362 error_report("Unexpected IOTLB message type");
363 ret = -EINVAL;
364 break;
367 return ret;