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.
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
,
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
);
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
);
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
)
58 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
60 uint64_t val
= g_ascii_strtoull(s
, NULL
, 10);
61 if (!((val
== G_MAXUINT64
|| !val
) && errno
)) {
64 error_report("ignoring invalid max_mem_regions value in vhost module:"
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
)
89 switch (backend_type
) {
90 case VHOST_BACKEND_TYPE_KERNEL
:
91 dev
->vhost_ops
= &kernel_ops
;
93 case VHOST_BACKEND_TYPE_USER
:
94 dev
->vhost_ops
= &user_ops
;
97 error_report("Unknown vhost backend type");