]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/vhost-backend.c
vhost-user: send log shm fd along with log_base
[mirror_qemu.git] / hw / virtio / vhost-backend.c
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
11 #include "hw/virtio/vhost.h"
12 #include "hw/virtio/vhost-backend.h"
13 #include "qemu/error-report.h"
14 #include "linux/vhost.h"
15
16 #include <sys/ioctl.h>
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
46 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
47 {
48 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
49
50 return idx - dev->vq_index;
51 }
52
53 static 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);
61 if (!((val == G_MAXUINT64 || !val) && errno)) {
62 return val;
63 }
64 error_report("ignoring invalid max_mem_regions value in vhost module:"
65 " %s", s);
66 }
67 return limit;
68 }
69
70 static int vhost_set_log_base(struct vhost_dev *dev, uint64_t base,
71 struct vhost_log *log)
72 {
73 return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
74 }
75
76 static const VhostOps kernel_ops = {
77 .backend_type = VHOST_BACKEND_TYPE_KERNEL,
78 .vhost_call = vhost_kernel_call,
79 .vhost_backend_init = vhost_kernel_init,
80 .vhost_backend_cleanup = vhost_kernel_cleanup,
81 .vhost_backend_get_vq_index = vhost_kernel_get_vq_index,
82 .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
83 .vhost_set_log_base = vhost_set_log_base,
84 };
85
86 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
87 {
88 int r = 0;
89
90 switch (backend_type) {
91 case VHOST_BACKEND_TYPE_KERNEL:
92 dev->vhost_ops = &kernel_ops;
93 break;
94 case VHOST_BACKEND_TYPE_USER:
95 dev->vhost_ops = &user_ops;
96 break;
97 default:
98 error_report("Unknown vhost backend type");
99 r = -1;
100 }
101
102 return r;
103 }