]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/vhost-backend.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / hw / virtio / vhost-backend.c
CommitLineData
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"
1a1bfac9
NN
12#include "hw/virtio/vhost.h"
13#include "hw/virtio/vhost-backend.h"
14#include "qemu/error-report.h"
db725815 15#include "qemu/main-loop.h"
18658a3c
PB
16#include "standard-headers/linux/vhost_types.h"
17
108a6481 18#include "hw/virtio/vhost-vdpa.h"
299e6f19 19#ifdef CONFIG_VHOST_KERNEL
18658a3c
PB
20#include <linux/vhost.h>
21#include <sys/ioctl.h>
1a1bfac9
NN
22
23static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
24 void *arg)
25{
26 int fd = (uintptr_t) dev->opaque;
f2a6e6c4 27 int ret;
1a1bfac9
NN
28
29 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
30
f2a6e6c4
KW
31 ret = ioctl(fd, request, arg);
32 return ret < 0 ? -errno : ret;
1a1bfac9
NN
33}
34
28770ff9 35static int vhost_kernel_init(struct vhost_dev *dev, void *opaque, Error **errp)
1a1bfac9
NN
36{
37 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
38
39 dev->opaque = opaque;
40
41 return 0;
42}
43
44static int vhost_kernel_cleanup(struct vhost_dev *dev)
45{
46 int fd = (uintptr_t) dev->opaque;
47
48 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
49
2d88d9c6 50 return close(fd) < 0 ? -errno : 0;
1a1bfac9
NN
51}
52
2ce68e4c
IM
53static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
54{
55 int limit = 64;
56 char *s;
57
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);
6dcae534 61 if (val < INT_MAX && val > 0) {
08b9e0ba 62 g_free(s);
2ce68e4c
IM
63 return val;
64 }
65 error_report("ignoring invalid max_mem_regions value in vhost module:"
66 " %s", s);
67 }
08b9e0ba 68 g_free(s);
2ce68e4c
IM
69 return limit;
70}
71
21e70425
MAL
72static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
73 struct vhost_vring_file *file)
74{
75 return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file);
76}
77
78static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev,
79 struct vhost_scsi_target *target)
80{
81 return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target);
82}
83
84static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev,
85 struct vhost_scsi_target *target)
86{
87 return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target);
88}
89
90static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version)
91{
92 return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version);
93}
94
95static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base,
96 struct vhost_log *log)
c2bea314
MAL
97{
98 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
99}
100
21e70425
MAL
101static int vhost_kernel_set_mem_table(struct vhost_dev *dev,
102 struct vhost_memory *mem)
103{
104 return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem);
105}
106
107static int vhost_kernel_set_vring_addr(struct vhost_dev *dev,
108 struct vhost_vring_addr *addr)
109{
110 return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr);
111}
112
113static int vhost_kernel_set_vring_endian(struct vhost_dev *dev,
114 struct vhost_vring_state *ring)
115{
116 return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring);
117}
118
119static int vhost_kernel_set_vring_num(struct vhost_dev *dev,
120 struct vhost_vring_state *ring)
121{
122 return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring);
123}
124
125static int vhost_kernel_set_vring_base(struct vhost_dev *dev,
126 struct vhost_vring_state *ring)
127{
128 return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring);
129}
130
131static int vhost_kernel_get_vring_base(struct vhost_dev *dev,
132 struct vhost_vring_state *ring)
133{
134 return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring);
135}
136
137static int vhost_kernel_set_vring_kick(struct vhost_dev *dev,
138 struct vhost_vring_file *file)
139{
140 return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file);
141}
142
143static int vhost_kernel_set_vring_call(struct vhost_dev *dev,
144 struct vhost_vring_file *file)
145{
146 return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file);
147}
148
60dc3c5b
KK
149static int vhost_kernel_set_vring_err(struct vhost_dev *dev,
150 struct vhost_vring_file *file)
151{
152 return vhost_kernel_call(dev, VHOST_SET_VRING_ERR, file);
153}
154
69e87b32
JW
155static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,
156 struct vhost_vring_state *s)
157{
158 return vhost_kernel_call(dev, VHOST_SET_VRING_BUSYLOOP_TIMEOUT, s);
159}
160
9aad7819
MC
161static int vhost_kernel_new_worker(struct vhost_dev *dev,
162 struct vhost_worker_state *worker)
163{
164 return vhost_kernel_call(dev, VHOST_NEW_WORKER, worker);
165}
166
167static int vhost_kernel_free_worker(struct vhost_dev *dev,
168 struct vhost_worker_state *worker)
169{
170 return vhost_kernel_call(dev, VHOST_FREE_WORKER, worker);
171}
172
173static int vhost_kernel_attach_vring_worker(struct vhost_dev *dev,
174 struct vhost_vring_worker *worker)
175{
176 return vhost_kernel_call(dev, VHOST_ATTACH_VRING_WORKER, worker);
177}
178
179static int vhost_kernel_get_vring_worker(struct vhost_dev *dev,
180 struct vhost_vring_worker *worker)
181{
182 return vhost_kernel_call(dev, VHOST_GET_VRING_WORKER, worker);
183}
184
21e70425
MAL
185static int vhost_kernel_set_features(struct vhost_dev *dev,
186 uint64_t features)
187{
188 return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features);
189}
190
b37556ed
JW
191static int vhost_kernel_set_backend_cap(struct vhost_dev *dev)
192{
193 uint64_t features;
194 uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2;
195 int r;
196
197 if (vhost_kernel_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
198 return 0;
199 }
200
201 features &= f;
202 r = vhost_kernel_call(dev, VHOST_SET_BACKEND_FEATURES,
203 &features);
204 if (r) {
205 return 0;
206 }
207
208 dev->backend_cap = features;
209
210 return 0;
211}
212
21e70425
MAL
213static int vhost_kernel_get_features(struct vhost_dev *dev,
214 uint64_t *features)
215{
216 return vhost_kernel_call(dev, VHOST_GET_FEATURES, features);
217}
218
219static int vhost_kernel_set_owner(struct vhost_dev *dev)
220{
221 return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
222}
223
21e70425
MAL
224static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
225{
226 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
227
228 return idx - dev->vq_index;
229}
230
fc0b9b0e
SH
231static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev *dev,
232 uint64_t guest_cid)
233{
234 return vhost_kernel_call(dev, VHOST_VSOCK_SET_GUEST_CID, &guest_cid);
235}
236
237static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start)
238{
239 return vhost_kernel_call(dev, VHOST_VSOCK_SET_RUNNING, &start);
240}
fc0b9b0e 241
c471ad0e
JW
242static void vhost_kernel_iotlb_read(void *opaque)
243{
244 struct vhost_dev *dev = opaque;
c471ad0e
JW
245 ssize_t len;
246
b37556ed
JW
247 if (dev->backend_cap &
248 (0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)) {
249 struct vhost_msg_v2 msg;
250
251 while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
252 if (len < sizeof msg) {
253 error_report("Wrong vhost message len: %d", (int)len);
254 break;
255 }
256 if (msg.type != VHOST_IOTLB_MSG_V2) {
257 error_report("Unknown vhost iotlb message type");
258 break;
259 }
260
261 vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
c471ad0e 262 }
b37556ed
JW
263 } else {
264 struct vhost_msg msg;
265
266 while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
267 if (len < sizeof msg) {
268 error_report("Wrong vhost message len: %d", (int)len);
269 break;
270 }
271 if (msg.type != VHOST_IOTLB_MSG) {
272 error_report("Unknown vhost iotlb message type");
273 break;
274 }
275
276 vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
c471ad0e 277 }
c471ad0e 278 }
c471ad0e
JW
279}
280
020e571b
MC
281static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev *dev,
282 struct vhost_iotlb_msg *imsg)
c471ad0e 283{
b37556ed 284 if (dev->backend_cap & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)) {
8faf2f1d 285 struct vhost_msg_v2 msg = {};
c471ad0e 286
b37556ed
JW
287 msg.type = VHOST_IOTLB_MSG_V2;
288 msg.iotlb = *imsg;
c471ad0e 289
b37556ed
JW
290 if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
291 error_report("Fail to update device iotlb");
292 return -EFAULT;
293 }
294 } else {
8faf2f1d 295 struct vhost_msg msg = {};
b37556ed
JW
296
297 msg.type = VHOST_IOTLB_MSG;
298 msg.iotlb = *imsg;
299
300 if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
301 error_report("Fail to update device iotlb");
302 return -EFAULT;
303 }
c471ad0e
JW
304 }
305
306 return 0;
307}
308
309static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
310 int enabled)
311{
312 if (enabled)
313 qemu_set_fd_handler((uintptr_t)dev->opaque,
314 vhost_kernel_iotlb_read, NULL, dev);
315 else
316 qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
317}
318
9b1d929a 319const VhostOps kernel_ops = {
1a1bfac9 320 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
1a1bfac9 321 .vhost_backend_init = vhost_kernel_init,
fc57fd99 322 .vhost_backend_cleanup = vhost_kernel_cleanup,
2ce68e4c 323 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
21e70425
MAL
324 .vhost_net_set_backend = vhost_kernel_net_set_backend,
325 .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
326 .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
327 .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version,
328 .vhost_set_log_base = vhost_kernel_set_log_base,
329 .vhost_set_mem_table = vhost_kernel_set_mem_table,
330 .vhost_set_vring_addr = vhost_kernel_set_vring_addr,
331 .vhost_set_vring_endian = vhost_kernel_set_vring_endian,
332 .vhost_set_vring_num = vhost_kernel_set_vring_num,
333 .vhost_set_vring_base = vhost_kernel_set_vring_base,
334 .vhost_get_vring_base = vhost_kernel_get_vring_base,
335 .vhost_set_vring_kick = vhost_kernel_set_vring_kick,
336 .vhost_set_vring_call = vhost_kernel_set_vring_call,
60dc3c5b 337 .vhost_set_vring_err = vhost_kernel_set_vring_err,
69e87b32
JW
338 .vhost_set_vring_busyloop_timeout =
339 vhost_kernel_set_vring_busyloop_timeout,
9aad7819
MC
340 .vhost_get_vring_worker = vhost_kernel_get_vring_worker,
341 .vhost_attach_vring_worker = vhost_kernel_attach_vring_worker,
342 .vhost_new_worker = vhost_kernel_new_worker,
343 .vhost_free_worker = vhost_kernel_free_worker,
21e70425
MAL
344 .vhost_set_features = vhost_kernel_set_features,
345 .vhost_get_features = vhost_kernel_get_features,
b37556ed 346 .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
21e70425 347 .vhost_set_owner = vhost_kernel_set_owner,
21e70425 348 .vhost_get_vq_index = vhost_kernel_get_vq_index,
fc0b9b0e
SH
349 .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
350 .vhost_vsock_set_running = vhost_kernel_vsock_set_running,
c471ad0e 351 .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
020e571b 352 .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
1a1bfac9 353};
18658a3c 354#endif
1a1bfac9 355
020e571b
MC
356int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
357 uint64_t iova, uint64_t uaddr,
358 uint64_t len,
359 IOMMUAccessFlags perm)
360{
361 struct vhost_iotlb_msg imsg;
362
363 imsg.iova = iova;
364 imsg.uaddr = uaddr;
365 imsg.size = len;
366 imsg.type = VHOST_IOTLB_UPDATE;
367
368 switch (perm) {
369 case IOMMU_RO:
370 imsg.perm = VHOST_ACCESS_RO;
371 break;
372 case IOMMU_WO:
373 imsg.perm = VHOST_ACCESS_WO;
374 break;
375 case IOMMU_RW:
376 imsg.perm = VHOST_ACCESS_RW;
377 break;
378 default:
379 return -EINVAL;
380 }
381
384b557d
MC
382 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
383 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
384
385 return -ENODEV;
020e571b
MC
386}
387
388int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
389 uint64_t iova, uint64_t len)
390{
391 struct vhost_iotlb_msg imsg;
392
393 imsg.iova = iova;
394 imsg.size = len;
395 imsg.type = VHOST_IOTLB_INVALIDATE;
396
384b557d
MC
397 if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
398 return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
399
400 return -ENODEV;
020e571b
MC
401}
402
403int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
404 struct vhost_iotlb_msg *imsg)
405{
406 int ret = 0;
407
4d1ccc17
EP
408 if (unlikely(!dev->vdev)) {
409 error_report("Unexpected IOTLB message when virtio device is stopped");
410 return -EINVAL;
411 }
412
020e571b
MC
413 switch (imsg->type) {
414 case VHOST_IOTLB_MISS:
415 ret = vhost_device_iotlb_miss(dev, imsg->iova,
416 imsg->perm != VHOST_ACCESS_RO);
417 break;
418 case VHOST_IOTLB_ACCESS_FAIL:
419 /* FIXME: report device iotlb error */
420 error_report("Access failure IOTLB message type not supported");
421 ret = -ENOTSUP;
422 break;
423 case VHOST_IOTLB_UPDATE:
424 case VHOST_IOTLB_INVALIDATE:
425 default:
426 error_report("Unexpected IOTLB message type");
427 ret = -EINVAL;
428 break;
429 }
430
431 return ret;
432}