usb: Fix compilation for Windows
[qemu/ar7.git] / hw / virtio / vhost-backend.c
blobb35890289f3bee7a2067188e902cceca9f98a4b1
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 "linux/vhost.h"
17 #include <sys/ioctl.h>
19 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
20 void *arg)
22 int fd = (uintptr_t) dev->opaque;
24 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
26 return ioctl(fd, request, arg);
29 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque)
31 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
33 dev->opaque = opaque;
35 return 0;
38 static int vhost_kernel_cleanup(struct vhost_dev *dev)
40 int fd = (uintptr_t) dev->opaque;
42 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
44 return close(fd);
47 static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
49 int limit = 64;
50 char *s;
52 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
53 &s, NULL, NULL)) {
54 uint64_t val = g_ascii_strtoull(s, NULL, 10);
55 if (!((val == G_MAXUINT64 || !val) && errno)) {
56 return val;
58 error_report("ignoring invalid max_mem_regions value in vhost module:"
59 " %s", s);
61 return limit;
64 static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
65 struct vhost_vring_file *file)
67 return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file);
70 static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev,
71 struct vhost_scsi_target *target)
73 return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target);
76 static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev,
77 struct vhost_scsi_target *target)
79 return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target);
82 static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version)
84 return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version);
87 static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base,
88 struct vhost_log *log)
90 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
93 static int vhost_kernel_set_mem_table(struct vhost_dev *dev,
94 struct vhost_memory *mem)
96 return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem);
99 static int vhost_kernel_set_vring_addr(struct vhost_dev *dev,
100 struct vhost_vring_addr *addr)
102 return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr);
105 static int vhost_kernel_set_vring_endian(struct vhost_dev *dev,
106 struct vhost_vring_state *ring)
108 return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring);
111 static int vhost_kernel_set_vring_num(struct vhost_dev *dev,
112 struct vhost_vring_state *ring)
114 return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring);
117 static int vhost_kernel_set_vring_base(struct vhost_dev *dev,
118 struct vhost_vring_state *ring)
120 return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring);
123 static int vhost_kernel_get_vring_base(struct vhost_dev *dev,
124 struct vhost_vring_state *ring)
126 return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring);
129 static int vhost_kernel_set_vring_kick(struct vhost_dev *dev,
130 struct vhost_vring_file *file)
132 return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file);
135 static int vhost_kernel_set_vring_call(struct vhost_dev *dev,
136 struct vhost_vring_file *file)
138 return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file);
141 static int vhost_kernel_set_features(struct vhost_dev *dev,
142 uint64_t features)
144 return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features);
147 static int vhost_kernel_get_features(struct vhost_dev *dev,
148 uint64_t *features)
150 return vhost_kernel_call(dev, VHOST_GET_FEATURES, features);
153 static int vhost_kernel_set_owner(struct vhost_dev *dev)
155 return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
158 static int vhost_kernel_reset_device(struct vhost_dev *dev)
160 return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
163 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
165 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
167 return idx - dev->vq_index;
170 static const VhostOps kernel_ops = {
171 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
172 .vhost_backend_init = vhost_kernel_init,
173 .vhost_backend_cleanup = vhost_kernel_cleanup,
174 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
175 .vhost_net_set_backend = vhost_kernel_net_set_backend,
176 .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
177 .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
178 .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version,
179 .vhost_set_log_base = vhost_kernel_set_log_base,
180 .vhost_set_mem_table = vhost_kernel_set_mem_table,
181 .vhost_set_vring_addr = vhost_kernel_set_vring_addr,
182 .vhost_set_vring_endian = vhost_kernel_set_vring_endian,
183 .vhost_set_vring_num = vhost_kernel_set_vring_num,
184 .vhost_set_vring_base = vhost_kernel_set_vring_base,
185 .vhost_get_vring_base = vhost_kernel_get_vring_base,
186 .vhost_set_vring_kick = vhost_kernel_set_vring_kick,
187 .vhost_set_vring_call = vhost_kernel_set_vring_call,
188 .vhost_set_features = vhost_kernel_set_features,
189 .vhost_get_features = vhost_kernel_get_features,
190 .vhost_set_owner = vhost_kernel_set_owner,
191 .vhost_reset_device = vhost_kernel_reset_device,
192 .vhost_get_vq_index = vhost_kernel_get_vq_index,
195 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
197 int r = 0;
199 switch (backend_type) {
200 case VHOST_BACKEND_TYPE_KERNEL:
201 dev->vhost_ops = &kernel_ops;
202 break;
203 case VHOST_BACKEND_TYPE_USER:
204 dev->vhost_ops = &user_ops;
205 break;
206 default:
207 error_report("Unknown vhost backend type");
208 r = -1;
211 return r;