]>
Commit | Line | Data |
---|---|---|
1a1bfac9 NN |
1 | /* |
2 | * vhost-backend | |
3 | * | |
4 | * Copyright (c) 2013 Virtual Open Systems Sarl. | |
5 | * | |
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. | |
8 | * | |
9 | */ | |
10 | ||
9b8bfe21 | 11 | #include "qemu/osdep.h" |
a9c94277 MA |
12 | #include <linux/vhost.h> |
13 | #include <sys/ioctl.h> | |
1a1bfac9 NN |
14 | #include "hw/virtio/vhost.h" |
15 | #include "hw/virtio/vhost-backend.h" | |
16 | #include "qemu/error-report.h" | |
1a1bfac9 NN |
17 | |
18 | static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request, | |
19 | void *arg) | |
20 | { | |
21 | int fd = (uintptr_t) dev->opaque; | |
22 | ||
23 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); | |
24 | ||
25 | return ioctl(fd, request, arg); | |
26 | } | |
27 | ||
28 | static int vhost_kernel_init(struct vhost_dev *dev, void *opaque) | |
29 | { | |
30 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); | |
31 | ||
32 | dev->opaque = opaque; | |
33 | ||
34 | return 0; | |
35 | } | |
36 | ||
37 | static int vhost_kernel_cleanup(struct vhost_dev *dev) | |
38 | { | |
39 | int fd = (uintptr_t) dev->opaque; | |
40 | ||
41 | assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); | |
42 | ||
43 | return close(fd); | |
44 | } | |
45 | ||
2ce68e4c IM |
46 | static int vhost_kernel_memslots_limit(struct vhost_dev *dev) |
47 | { | |
48 | int limit = 64; | |
49 | char *s; | |
50 | ||
51 | if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions", | |
52 | &s, NULL, NULL)) { | |
53 | uint64_t val = g_ascii_strtoull(s, NULL, 10); | |
54 | if (!((val == G_MAXUINT64 || !val) && errno)) { | |
08b9e0ba | 55 | g_free(s); |
2ce68e4c IM |
56 | return val; |
57 | } | |
58 | error_report("ignoring invalid max_mem_regions value in vhost module:" | |
59 | " %s", s); | |
60 | } | |
08b9e0ba | 61 | g_free(s); |
2ce68e4c IM |
62 | return limit; |
63 | } | |
64 | ||
21e70425 MAL |
65 | static int vhost_kernel_net_set_backend(struct vhost_dev *dev, |
66 | struct vhost_vring_file *file) | |
67 | { | |
68 | return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file); | |
69 | } | |
70 | ||
71 | static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev, | |
72 | struct vhost_scsi_target *target) | |
73 | { | |
74 | return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target); | |
75 | } | |
76 | ||
77 | static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev, | |
78 | struct vhost_scsi_target *target) | |
79 | { | |
80 | return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target); | |
81 | } | |
82 | ||
83 | static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version) | |
84 | { | |
85 | return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version); | |
86 | } | |
87 | ||
88 | static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base, | |
89 | struct vhost_log *log) | |
c2bea314 MAL |
90 | { |
91 | return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base); | |
92 | } | |
93 | ||
21e70425 MAL |
94 | static int vhost_kernel_set_mem_table(struct vhost_dev *dev, |
95 | struct vhost_memory *mem) | |
96 | { | |
97 | return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem); | |
98 | } | |
99 | ||
100 | static int vhost_kernel_set_vring_addr(struct vhost_dev *dev, | |
101 | struct vhost_vring_addr *addr) | |
102 | { | |
103 | return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr); | |
104 | } | |
105 | ||
106 | static int vhost_kernel_set_vring_endian(struct vhost_dev *dev, | |
107 | struct vhost_vring_state *ring) | |
108 | { | |
109 | return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring); | |
110 | } | |
111 | ||
112 | static int vhost_kernel_set_vring_num(struct vhost_dev *dev, | |
113 | struct vhost_vring_state *ring) | |
114 | { | |
115 | return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring); | |
116 | } | |
117 | ||
118 | static int vhost_kernel_set_vring_base(struct vhost_dev *dev, | |
119 | struct vhost_vring_state *ring) | |
120 | { | |
121 | return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring); | |
122 | } | |
123 | ||
124 | static int vhost_kernel_get_vring_base(struct vhost_dev *dev, | |
125 | struct vhost_vring_state *ring) | |
126 | { | |
127 | return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring); | |
128 | } | |
129 | ||
130 | static int vhost_kernel_set_vring_kick(struct vhost_dev *dev, | |
131 | struct vhost_vring_file *file) | |
132 | { | |
133 | return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file); | |
134 | } | |
135 | ||
136 | static int vhost_kernel_set_vring_call(struct vhost_dev *dev, | |
137 | struct vhost_vring_file *file) | |
138 | { | |
139 | return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file); | |
140 | } | |
141 | ||
69e87b32 JW |
142 | static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev, |
143 | struct vhost_vring_state *s) | |
144 | { | |
145 | return vhost_kernel_call(dev, VHOST_SET_VRING_BUSYLOOP_TIMEOUT, s); | |
146 | } | |
147 | ||
21e70425 MAL |
148 | static int vhost_kernel_set_features(struct vhost_dev *dev, |
149 | uint64_t features) | |
150 | { | |
151 | return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features); | |
152 | } | |
153 | ||
154 | static int vhost_kernel_get_features(struct vhost_dev *dev, | |
155 | uint64_t *features) | |
156 | { | |
157 | return vhost_kernel_call(dev, VHOST_GET_FEATURES, features); | |
158 | } | |
159 | ||
160 | static int vhost_kernel_set_owner(struct vhost_dev *dev) | |
161 | { | |
162 | return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL); | |
163 | } | |
164 | ||
165 | static int vhost_kernel_reset_device(struct vhost_dev *dev) | |
166 | { | |
60915dc4 | 167 | return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL); |
21e70425 MAL |
168 | } |
169 | ||
170 | static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx) | |
171 | { | |
172 | assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); | |
173 | ||
174 | return idx - dev->vq_index; | |
175 | } | |
176 | ||
fc0b9b0e SH |
177 | #ifdef CONFIG_VHOST_VSOCK |
178 | static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev *dev, | |
179 | uint64_t guest_cid) | |
180 | { | |
181 | return vhost_kernel_call(dev, VHOST_VSOCK_SET_GUEST_CID, &guest_cid); | |
182 | } | |
183 | ||
184 | static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start) | |
185 | { | |
186 | return vhost_kernel_call(dev, VHOST_VSOCK_SET_RUNNING, &start); | |
187 | } | |
188 | #endif /* CONFIG_VHOST_VSOCK */ | |
189 | ||
c471ad0e JW |
190 | static void vhost_kernel_iotlb_read(void *opaque) |
191 | { | |
192 | struct vhost_dev *dev = opaque; | |
193 | struct vhost_msg msg; | |
194 | ssize_t len; | |
195 | ||
196 | while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) { | |
c471ad0e JW |
197 | if (len < sizeof msg) { |
198 | error_report("Wrong vhost message len: %d", (int)len); | |
199 | break; | |
200 | } | |
201 | if (msg.type != VHOST_IOTLB_MSG) { | |
202 | error_report("Unknown vhost iotlb message type"); | |
203 | break; | |
204 | } | |
c471ad0e | 205 | |
020e571b | 206 | vhost_backend_handle_iotlb_msg(dev, &msg.iotlb); |
c471ad0e | 207 | } |
c471ad0e JW |
208 | } |
209 | ||
020e571b MC |
210 | static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev *dev, |
211 | struct vhost_iotlb_msg *imsg) | |
c471ad0e JW |
212 | { |
213 | struct vhost_msg msg; | |
214 | ||
215 | msg.type = VHOST_IOTLB_MSG; | |
020e571b | 216 | msg.iotlb = *imsg; |
c471ad0e JW |
217 | |
218 | if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) { | |
020e571b | 219 | error_report("Fail to update device iotlb"); |
c471ad0e JW |
220 | return -EFAULT; |
221 | } | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev, | |
227 | int enabled) | |
228 | { | |
229 | if (enabled) | |
230 | qemu_set_fd_handler((uintptr_t)dev->opaque, | |
231 | vhost_kernel_iotlb_read, NULL, dev); | |
232 | else | |
233 | qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL); | |
234 | } | |
235 | ||
1a1bfac9 NN |
236 | static const VhostOps kernel_ops = { |
237 | .backend_type = VHOST_BACKEND_TYPE_KERNEL, | |
1a1bfac9 | 238 | .vhost_backend_init = vhost_kernel_init, |
fc57fd99 | 239 | .vhost_backend_cleanup = vhost_kernel_cleanup, |
2ce68e4c | 240 | .vhost_backend_memslots_limit = vhost_kernel_memslots_limit, |
21e70425 MAL |
241 | .vhost_net_set_backend = vhost_kernel_net_set_backend, |
242 | .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint, | |
243 | .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint, | |
244 | .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version, | |
245 | .vhost_set_log_base = vhost_kernel_set_log_base, | |
246 | .vhost_set_mem_table = vhost_kernel_set_mem_table, | |
247 | .vhost_set_vring_addr = vhost_kernel_set_vring_addr, | |
248 | .vhost_set_vring_endian = vhost_kernel_set_vring_endian, | |
249 | .vhost_set_vring_num = vhost_kernel_set_vring_num, | |
250 | .vhost_set_vring_base = vhost_kernel_set_vring_base, | |
251 | .vhost_get_vring_base = vhost_kernel_get_vring_base, | |
252 | .vhost_set_vring_kick = vhost_kernel_set_vring_kick, | |
253 | .vhost_set_vring_call = vhost_kernel_set_vring_call, | |
69e87b32 JW |
254 | .vhost_set_vring_busyloop_timeout = |
255 | vhost_kernel_set_vring_busyloop_timeout, | |
21e70425 MAL |
256 | .vhost_set_features = vhost_kernel_set_features, |
257 | .vhost_get_features = vhost_kernel_get_features, | |
258 | .vhost_set_owner = vhost_kernel_set_owner, | |
259 | .vhost_reset_device = vhost_kernel_reset_device, | |
260 | .vhost_get_vq_index = vhost_kernel_get_vq_index, | |
fc0b9b0e SH |
261 | #ifdef CONFIG_VHOST_VSOCK |
262 | .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid, | |
263 | .vhost_vsock_set_running = vhost_kernel_vsock_set_running, | |
264 | #endif /* CONFIG_VHOST_VSOCK */ | |
c471ad0e | 265 | .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, |
020e571b | 266 | .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg, |
1a1bfac9 NN |
267 | }; |
268 | ||
269 | int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type) | |
270 | { | |
271 | int r = 0; | |
272 | ||
273 | switch (backend_type) { | |
274 | case VHOST_BACKEND_TYPE_KERNEL: | |
275 | dev->vhost_ops = &kernel_ops; | |
276 | break; | |
5f6f6664 NN |
277 | case VHOST_BACKEND_TYPE_USER: |
278 | dev->vhost_ops = &user_ops; | |
279 | break; | |
1a1bfac9 | 280 | default: |
c6547661 | 281 | error_report("Unknown vhost backend type"); |
1a1bfac9 NN |
282 | r = -1; |
283 | } | |
284 | ||
285 | return r; | |
286 | } | |
020e571b MC |
287 | |
288 | int vhost_backend_update_device_iotlb(struct vhost_dev *dev, | |
289 | uint64_t iova, uint64_t uaddr, | |
290 | uint64_t len, | |
291 | IOMMUAccessFlags perm) | |
292 | { | |
293 | struct vhost_iotlb_msg imsg; | |
294 | ||
295 | imsg.iova = iova; | |
296 | imsg.uaddr = uaddr; | |
297 | imsg.size = len; | |
298 | imsg.type = VHOST_IOTLB_UPDATE; | |
299 | ||
300 | switch (perm) { | |
301 | case IOMMU_RO: | |
302 | imsg.perm = VHOST_ACCESS_RO; | |
303 | break; | |
304 | case IOMMU_WO: | |
305 | imsg.perm = VHOST_ACCESS_WO; | |
306 | break; | |
307 | case IOMMU_RW: | |
308 | imsg.perm = VHOST_ACCESS_RW; | |
309 | break; | |
310 | default: | |
311 | return -EINVAL; | |
312 | } | |
313 | ||
384b557d MC |
314 | if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) |
315 | return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); | |
316 | ||
317 | return -ENODEV; | |
020e571b MC |
318 | } |
319 | ||
320 | int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev, | |
321 | uint64_t iova, uint64_t len) | |
322 | { | |
323 | struct vhost_iotlb_msg imsg; | |
324 | ||
325 | imsg.iova = iova; | |
326 | imsg.size = len; | |
327 | imsg.type = VHOST_IOTLB_INVALIDATE; | |
328 | ||
384b557d MC |
329 | if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) |
330 | return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); | |
331 | ||
332 | return -ENODEV; | |
020e571b MC |
333 | } |
334 | ||
335 | int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev, | |
336 | struct vhost_iotlb_msg *imsg) | |
337 | { | |
338 | int ret = 0; | |
339 | ||
340 | switch (imsg->type) { | |
341 | case VHOST_IOTLB_MISS: | |
342 | ret = vhost_device_iotlb_miss(dev, imsg->iova, | |
343 | imsg->perm != VHOST_ACCESS_RO); | |
344 | break; | |
345 | case VHOST_IOTLB_ACCESS_FAIL: | |
346 | /* FIXME: report device iotlb error */ | |
347 | error_report("Access failure IOTLB message type not supported"); | |
348 | ret = -ENOTSUP; | |
349 | break; | |
350 | case VHOST_IOTLB_UPDATE: | |
351 | case VHOST_IOTLB_INVALIDATE: | |
352 | default: | |
353 | error_report("Unexpected IOTLB message type"); | |
354 | ret = -EINVAL; | |
355 | break; | |
356 | } | |
357 | ||
358 | return ret; | |
359 | } |