1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
10 #include <rte_memory.h>
13 #include "virtio_user_dev.h"
14 #include "vhost_kernel_tap.h"
16 struct vhost_memory_kernel {
19 struct vhost_memory_region regions[0];
22 /* vhost kernel ioctls */
23 #define VHOST_VIRTIO 0xAF
24 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
25 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
26 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
27 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
28 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
29 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
30 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
31 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
32 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
33 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
34 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
35 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
36 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
37 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
38 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
40 static uint64_t max_regions = 64;
43 get_vhost_kernel_max_regions(void)
46 char buf[20] = {'\0'};
48 fd = open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY);
52 if (read(fd, buf, sizeof(buf) - 1) > 0)
53 max_regions = strtoull(buf, NULL, 10);
58 static uint64_t vhost_req_user_to_kernel[] = {
59 [VHOST_USER_SET_OWNER] = VHOST_SET_OWNER,
60 [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
61 [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
62 [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
63 [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
64 [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
65 [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
66 [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
67 [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
68 [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
69 [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
73 add_memseg_list(const struct rte_memseg_list *msl, void *arg)
75 struct vhost_memory_kernel *vm = arg;
76 struct vhost_memory_region *mr;
83 if (vm->nregions >= max_regions)
86 start_addr = msl->base_va;
87 len = msl->page_sz * msl->memseg_arr.len;
89 mr = &vm->regions[vm->nregions++];
91 mr->guest_phys_addr = (uint64_t)(uintptr_t)start_addr;
92 mr->userspace_addr = (uint64_t)(uintptr_t)start_addr;
93 mr->memory_size = len;
94 mr->mmap_offset = 0; /* flags_padding */
96 PMD_DRV_LOG(DEBUG, "index=%u addr=%p len=%" PRIu64,
97 vm->nregions - 1, start_addr, len);
102 /* By default, vhost kernel module allows 64 regions, but DPDK may
103 * have much more memory regions. Below function will treat each
104 * contiguous memory space reserved by DPDK as one region.
106 static struct vhost_memory_kernel *
107 prepare_vhost_memory_kernel(void)
109 struct vhost_memory_kernel *vm;
111 vm = malloc(sizeof(struct vhost_memory_kernel) +
113 sizeof(struct vhost_memory_region));
121 * The memory lock has already been taken by memory subsystem
122 * or virtio_user_start_device().
124 if (rte_memseg_list_walk_thread_unsafe(add_memseg_list, vm) < 0) {
132 /* with below features, vhost kernel does not need to do the checksum and TSO,
133 * these info will be passed to virtio_user through virtio net header.
135 #define VHOST_KERNEL_GUEST_OFFLOADS_MASK \
136 ((1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
137 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
138 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
139 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
140 (1ULL << VIRTIO_NET_F_GUEST_UFO))
142 /* with below features, when flows from virtio_user to vhost kernel
143 * (1) if flows goes up through the kernel networking stack, it does not need
144 * to verify checksum, which can save CPU cycles;
145 * (2) if flows goes through a Linux bridge and outside from an interface
146 * (kernel driver), checksum and TSO will be done by GSO in kernel or even
147 * offloaded into real physical device.
149 #define VHOST_KERNEL_HOST_OFFLOADS_MASK \
150 ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
151 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
152 (1ULL << VIRTIO_NET_F_CSUM))
155 tap_support_features(void)
158 unsigned int tap_features;
160 tapfd = open(PATH_NET_TUN, O_RDWR);
162 PMD_DRV_LOG(ERR, "fail to open %s: %s",
163 PATH_NET_TUN, strerror(errno));
167 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
168 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
178 vhost_kernel_ioctl(struct virtio_user_dev *dev,
179 enum vhost_user_request req,
185 struct vhost_memory_kernel *vm = NULL;
187 unsigned int queue_sel;
188 unsigned int features;
190 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
192 req_kernel = vhost_req_user_to_kernel[req];
194 if (req_kernel == VHOST_SET_MEM_TABLE) {
195 vm = prepare_vhost_memory_kernel();
201 if (req_kernel == VHOST_SET_FEATURES) {
202 /* We don't need memory protection here */
203 *(uint64_t *)arg &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
205 /* VHOST kernel does not know about below flags */
206 *(uint64_t *)arg &= ~VHOST_KERNEL_GUEST_OFFLOADS_MASK;
207 *(uint64_t *)arg &= ~VHOST_KERNEL_HOST_OFFLOADS_MASK;
209 *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
212 switch (req_kernel) {
213 case VHOST_SET_VRING_NUM:
214 case VHOST_SET_VRING_ADDR:
215 case VHOST_SET_VRING_BASE:
216 case VHOST_GET_VRING_BASE:
217 case VHOST_SET_VRING_KICK:
218 case VHOST_SET_VRING_CALL:
219 queue_sel = *(unsigned int *)arg;
220 vhostfd = dev->vhostfds[queue_sel / 2];
221 *(unsigned int *)arg = queue_sel % 2;
222 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
223 vhostfd, *(unsigned int *)arg);
229 for (i = 0; i < dev->max_queue_pairs; ++i) {
230 if (dev->vhostfds[i] < 0)
233 ret = ioctl(dev->vhostfds[i], req_kernel, arg);
238 ret = ioctl(vhostfd, req_kernel, arg);
241 if (!ret && req_kernel == VHOST_GET_FEATURES) {
242 features = tap_support_features();
243 /* with tap as the backend, all these features are supported
244 * but not claimed by vhost-net, so we add them back when
245 * reporting to upper layer.
247 if (features & IFF_VNET_HDR) {
248 *((uint64_t *)arg) |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
249 *((uint64_t *)arg) |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
252 /* vhost_kernel will not declare this feature, but it does
253 * support multi-queue.
255 if (features & IFF_MULTI_QUEUE)
256 *(uint64_t *)arg |= (1ull << VIRTIO_NET_F_MQ);
263 PMD_DRV_LOG(ERR, "%s failed: %s",
264 vhost_msg_strings[req], strerror(errno));
270 * Set up environment to talk with a vhost kernel backend.
273 * - (-1) if fail to set up;
274 * - (>=0) if successful.
277 vhost_kernel_setup(struct virtio_user_dev *dev)
282 get_vhost_kernel_max_regions();
284 for (i = 0; i < dev->max_queue_pairs; ++i) {
285 vhostfd = open(dev->path, O_RDWR);
287 PMD_DRV_LOG(ERR, "fail to open %s, %s",
288 dev->path, strerror(errno));
292 dev->vhostfds[i] = vhostfd;
299 vhost_kernel_set_backend(int vhostfd, int tapfd)
301 struct vhost_vring_file f;
305 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
306 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
312 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
313 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
322 vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
329 int req_mq = (dev->max_queue_pairs > 1);
331 vhostfd = dev->vhostfds[pair_idx];
334 if (dev->tapfds[pair_idx] >= 0) {
335 close(dev->tapfds[pair_idx]);
336 dev->tapfds[pair_idx] = -1;
338 return vhost_kernel_set_backend(vhostfd, -1);
339 } else if (dev->tapfds[pair_idx] >= 0) {
343 if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
344 (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
345 hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
347 hdr_size = sizeof(struct virtio_net_hdr);
349 tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
350 (char *)dev->mac_addr, dev->features);
352 PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
356 if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
357 PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
362 dev->tapfds[pair_idx] = tapfd;
366 struct virtio_user_backend_ops virtio_ops_kernel = {
367 .setup = vhost_kernel_setup,
368 .send_request = vhost_kernel_ioctl,
369 .enable_qp = vhost_kernel_enable_queue_pair