4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
39 #include <rte_memory.h>
40 #include <rte_eal_memconfig.h>
43 #include "virtio_user_dev.h"
44 #include "vhost_kernel_tap.h"
46 struct vhost_memory_kernel {
49 struct vhost_memory_region regions[0];
52 /* vhost kernel ioctls */
53 #define VHOST_VIRTIO 0xAF
54 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
55 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
56 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
57 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
58 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
59 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
60 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
61 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
62 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
63 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
64 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
65 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
66 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
67 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
68 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
70 static uint64_t max_regions = 64;
73 get_vhost_kernel_max_regions(void)
76 char buf[20] = {'\0'};
78 fd = open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY);
82 if (read(fd, buf, sizeof(buf) - 1) > 0)
83 max_regions = strtoull(buf, NULL, 10);
88 static uint64_t vhost_req_user_to_kernel[] = {
89 [VHOST_USER_SET_OWNER] = VHOST_SET_OWNER,
90 [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
91 [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
92 [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
93 [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
94 [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
95 [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
96 [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
97 [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
98 [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
99 [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
102 /* By default, vhost kernel module allows 64 regions, but DPDK allows
103 * 256 segments. As a relief, below function merges those virtually
104 * adjacent memsegs into one region.
106 static struct vhost_memory_kernel *
107 prepare_vhost_memory_kernel(void)
109 uint32_t i, j, k = 0;
110 struct rte_memseg *seg;
111 struct vhost_memory_region *mr;
112 struct vhost_memory_kernel *vm;
114 vm = malloc(sizeof(struct vhost_memory_kernel) +
116 sizeof(struct vhost_memory_region));
118 for (i = 0; i < RTE_MAX_MEMSEG; ++i) {
119 seg = &rte_eal_get_configuration()->mem_config->memseg[i];
125 for (j = 0; j < k; ++j) {
126 mr = &vm->regions[j];
128 if (mr->userspace_addr + mr->memory_size ==
129 (uint64_t)(uintptr_t)seg->addr) {
130 mr->memory_size += seg->len;
135 if ((uint64_t)(uintptr_t)seg->addr + seg->len ==
136 mr->userspace_addr) {
137 mr->guest_phys_addr =
138 (uint64_t)(uintptr_t)seg->addr;
140 (uint64_t)(uintptr_t)seg->addr;
141 mr->memory_size += seg->len;
150 mr = &vm->regions[k++];
151 /* use vaddr here! */
152 mr->guest_phys_addr = (uint64_t)(uintptr_t)seg->addr;
153 mr->userspace_addr = (uint64_t)(uintptr_t)seg->addr;
154 mr->memory_size = seg->len;
157 if (k >= max_regions) {
169 vhost_kernel_ioctl(struct virtio_user_dev *dev,
170 enum vhost_user_request req,
176 struct vhost_memory_kernel *vm = NULL;
178 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
180 req_kernel = vhost_req_user_to_kernel[req];
182 if (req_kernel == VHOST_SET_MEM_TABLE) {
183 vm = prepare_vhost_memory_kernel();
189 /* We don't need memory protection here */
190 if (req_kernel == VHOST_SET_FEATURES)
191 *(uint64_t *)arg &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
193 for (i = 0; i < dev->max_queue_pairs; ++i) {
194 if (dev->vhostfds[i] < 0)
197 ret = ioctl(dev->vhostfds[i], req_kernel, arg);
206 PMD_DRV_LOG(ERR, "%s failed: %s",
207 vhost_msg_strings[req], strerror(errno));
213 * Set up environment to talk with a vhost kernel backend.
216 * - (-1) if fail to set up;
217 * - (>=0) if successful.
220 vhost_kernel_setup(struct virtio_user_dev *dev)
225 get_vhost_kernel_max_regions();
227 for (i = 0; i < dev->max_queue_pairs; ++i) {
228 vhostfd = open(dev->path, O_RDWR);
230 PMD_DRV_LOG(ERR, "fail to open %s, %s",
231 dev->path, strerror(errno));
235 dev->vhostfds[i] = vhostfd;
242 vhost_kernel_set_backend(int vhostfd, int tapfd)
244 struct vhost_vring_file f;
248 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
249 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
255 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
256 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
265 vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
273 vhostfd = dev->vhostfds[pair_idx];
276 if (dev->tapfds[pair_idx]) {
277 close(dev->tapfds[pair_idx]);
278 dev->tapfds[pair_idx] = -1;
280 return vhost_kernel_set_backend(vhostfd, -1);
281 } else if (dev->tapfds[pair_idx] >= 0) {
285 if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
286 (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
287 hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
289 hdr_size = sizeof(struct virtio_net_hdr);
291 tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size);
293 PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
297 if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
298 PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
303 dev->tapfds[pair_idx] = tapfd;
307 struct virtio_user_backend_ops ops_kernel = {
308 .setup = vhost_kernel_setup,
309 .send_request = vhost_kernel_ioctl,
310 .enable_qp = vhost_kernel_enable_queue_pair