vhost: add vhost_set_log_base op
[qemu/ar7.git] / hw / virtio / vhost-backend.c
blob5846c37f6d6f50650bfe238ce61f5e2a78730f21
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 "hw/virtio/vhost.h"
12 #include "hw/virtio/vhost-backend.h"
13 #include "qemu/error-report.h"
14 #include "linux/vhost.h"
16 #include <sys/ioctl.h>
18 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
19 void *arg)
21 int fd = (uintptr_t) dev->opaque;
23 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
25 return ioctl(fd, request, arg);
28 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque)
30 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
32 dev->opaque = opaque;
34 return 0;
37 static int vhost_kernel_cleanup(struct vhost_dev *dev)
39 int fd = (uintptr_t) dev->opaque;
41 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
43 return close(fd);
46 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
48 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
50 return idx - dev->vq_index;
53 static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
55 int limit = 64;
56 char *s;
58 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
59 &s, NULL, NULL)) {
60 uint64_t val = g_ascii_strtoull(s, NULL, 10);
61 if (!((val == G_MAXUINT64 || !val) && errno)) {
62 return val;
64 error_report("ignoring invalid max_mem_regions value in vhost module:"
65 " %s", s);
67 return limit;
70 static int vhost_set_log_base(struct vhost_dev *dev, uint64_t base)
72 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
75 static const VhostOps kernel_ops = {
76 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
77 .vhost_call = vhost_kernel_call,
78 .vhost_backend_init = vhost_kernel_init,
79 .vhost_backend_cleanup = vhost_kernel_cleanup,
80 .vhost_backend_get_vq_index = vhost_kernel_get_vq_index,
81 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
82 .vhost_set_log_base = vhost_set_log_base,
85 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
87 int r = 0;
89 switch (backend_type) {
90 case VHOST_BACKEND_TYPE_KERNEL:
91 dev->vhost_ops = &kernel_ops;
92 break;
93 case VHOST_BACKEND_TYPE_USER:
94 dev->vhost_ops = &user_ops;
95 break;
96 default:
97 error_report("Unknown vhost backend type");
98 r = -1;
101 return r;