1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
11 #include <rte_memory.h>
14 #include "virtio_user_dev.h"
15 #include "vhost_kernel_tap.h"
17 struct vhost_memory_kernel {
20 struct vhost_memory_region regions[0];
23 /* vhost kernel ioctls */
24 #define VHOST_VIRTIO 0xAF
25 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
26 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
27 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
28 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
29 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
30 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
31 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
32 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
33 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
34 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
35 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
36 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
37 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
38 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
39 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
41 /* with below features, vhost kernel does not need to do the checksum and TSO,
42 * these info will be passed to virtio_user through virtio net header.
44 #define VHOST_KERNEL_GUEST_OFFLOADS_MASK \
45 ((1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
46 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
47 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
48 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
49 (1ULL << VIRTIO_NET_F_GUEST_UFO))
51 /* with below features, when flows from virtio_user to vhost kernel
52 * (1) if flows goes up through the kernel networking stack, it does not need
53 * to verify checksum, which can save CPU cycles;
54 * (2) if flows goes through a Linux bridge and outside from an interface
55 * (kernel driver), checksum and TSO will be done by GSO in kernel or even
56 * offloaded into real physical device.
58 #define VHOST_KERNEL_HOST_OFFLOADS_MASK \
59 ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
60 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
61 (1ULL << VIRTIO_NET_F_CSUM))
63 static uint64_t max_regions = 64;
66 get_vhost_kernel_max_regions(void)
69 char buf[20] = {'\0'};
71 fd = open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY);
75 if (read(fd, buf, sizeof(buf) - 1) > 0)
76 max_regions = strtoull(buf, NULL, 10);
82 vhost_kernel_ioctl(int fd, uint64_t request, void *arg)
86 ret = ioctl(fd, request, arg);
88 PMD_DRV_LOG(ERR, "Vhost-kernel ioctl %"PRIu64" failed (%s)",
89 request, strerror(errno));
97 vhost_kernel_set_owner(struct virtio_user_dev *dev)
99 return vhost_kernel_ioctl(dev->vhostfds[0], VHOST_SET_OWNER, NULL);
103 vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
106 unsigned int tap_features;
108 ret = vhost_kernel_ioctl(dev->vhostfds[0], VHOST_GET_FEATURES, features);
110 PMD_DRV_LOG(ERR, "Failed to get features");
114 ret = tap_support_features(&tap_features);
116 PMD_DRV_LOG(ERR, "Failed to get TAP features");
120 /* with tap as the backend, all these features are supported
121 * but not claimed by vhost-net, so we add them back when
122 * reporting to upper layer.
124 if (tap_features & IFF_VNET_HDR) {
125 *features |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
126 *features |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
129 /* vhost_kernel will not declare this feature, but it does
130 * support multi-queue.
132 if (tap_features & IFF_MULTI_QUEUE)
133 *features |= (1ull << VIRTIO_NET_F_MQ);
139 vhost_kernel_set_features(struct virtio_user_dev *dev, uint64_t features)
141 /* We don't need memory protection here */
142 features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
143 /* VHOST kernel does not know about below flags */
144 features &= ~VHOST_KERNEL_GUEST_OFFLOADS_MASK;
145 features &= ~VHOST_KERNEL_HOST_OFFLOADS_MASK;
146 features &= ~(1ULL << VIRTIO_NET_F_MQ);
148 return vhost_kernel_ioctl(dev->vhostfds[0], VHOST_SET_FEATURES, &features);
151 static uint64_t vhost_req_user_to_kernel[] = {
152 [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
153 [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
154 [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
155 [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
156 [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
157 [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
158 [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
159 [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
163 add_memseg_list(const struct rte_memseg_list *msl, void *arg)
165 struct vhost_memory_kernel *vm = arg;
166 struct vhost_memory_region *mr;
173 if (vm->nregions >= max_regions)
176 start_addr = msl->base_va;
177 len = msl->page_sz * msl->memseg_arr.len;
179 mr = &vm->regions[vm->nregions++];
181 mr->guest_phys_addr = (uint64_t)(uintptr_t)start_addr;
182 mr->userspace_addr = (uint64_t)(uintptr_t)start_addr;
183 mr->memory_size = len;
184 mr->mmap_offset = 0; /* flags_padding */
186 PMD_DRV_LOG(DEBUG, "index=%u addr=%p len=%" PRIu64,
187 vm->nregions - 1, start_addr, len);
192 /* By default, vhost kernel module allows 64 regions, but DPDK may
193 * have much more memory regions. Below function will treat each
194 * contiguous memory space reserved by DPDK as one region.
196 static struct vhost_memory_kernel *
197 prepare_vhost_memory_kernel(void)
199 struct vhost_memory_kernel *vm;
201 vm = malloc(sizeof(struct vhost_memory_kernel) +
203 sizeof(struct vhost_memory_region));
211 * The memory lock has already been taken by memory subsystem
212 * or virtio_user_start_device().
214 if (rte_memseg_list_walk_thread_unsafe(add_memseg_list, vm) < 0) {
223 vhost_kernel_send_request(struct virtio_user_dev *dev,
224 enum vhost_user_request req,
230 struct vhost_memory_kernel *vm = NULL;
232 unsigned int queue_sel;
234 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
236 req_kernel = vhost_req_user_to_kernel[req];
238 if (req_kernel == VHOST_SET_MEM_TABLE) {
239 vm = prepare_vhost_memory_kernel();
245 switch (req_kernel) {
246 case VHOST_SET_VRING_NUM:
247 case VHOST_SET_VRING_ADDR:
248 case VHOST_SET_VRING_BASE:
249 case VHOST_GET_VRING_BASE:
250 case VHOST_SET_VRING_KICK:
251 case VHOST_SET_VRING_CALL:
252 queue_sel = *(unsigned int *)arg;
253 vhostfd = dev->vhostfds[queue_sel / 2];
254 *(unsigned int *)arg = queue_sel % 2;
255 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
256 vhostfd, *(unsigned int *)arg);
262 for (i = 0; i < dev->max_queue_pairs; ++i) {
263 if (dev->vhostfds[i] < 0)
266 ret = ioctl(dev->vhostfds[i], req_kernel, arg);
271 ret = ioctl(vhostfd, req_kernel, arg);
278 PMD_DRV_LOG(ERR, "%s failed: %s",
279 vhost_msg_strings[req], strerror(errno));
285 * Set up environment to talk with a vhost kernel backend.
288 * - (-1) if fail to set up;
289 * - (>=0) if successful.
292 vhost_kernel_setup(struct virtio_user_dev *dev)
297 get_vhost_kernel_max_regions();
299 for (i = 0; i < dev->max_queue_pairs; ++i) {
300 vhostfd = open(dev->path, O_RDWR);
302 PMD_DRV_LOG(ERR, "fail to open %s, %s",
303 dev->path, strerror(errno));
307 dev->vhostfds[i] = vhostfd;
314 vhost_kernel_set_backend(int vhostfd, int tapfd)
316 struct vhost_vring_file f;
320 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
321 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
327 if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
328 PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
337 vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
344 int req_mq = (dev->max_queue_pairs > 1);
346 vhostfd = dev->vhostfds[pair_idx];
348 if (dev->qp_enabled[pair_idx] == enable)
352 tapfd = dev->tapfds[pair_idx];
353 if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
354 PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
357 if (req_mq && vhost_kernel_tap_set_queue(tapfd, false) < 0) {
358 PMD_DRV_LOG(ERR, "fail to disable tap for vhost kernel");
361 dev->qp_enabled[pair_idx] = false;
365 if (dev->tapfds[pair_idx] >= 0) {
366 tapfd = dev->tapfds[pair_idx];
367 if (vhost_kernel_tap_set_offload(tapfd, dev->features) == -1)
369 if (req_mq && vhost_kernel_tap_set_queue(tapfd, true) < 0) {
370 PMD_DRV_LOG(ERR, "fail to enable tap for vhost kernel");
376 if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
377 (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
378 hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
380 hdr_size = sizeof(struct virtio_net_hdr);
382 tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
383 (char *)dev->mac_addr, dev->features);
385 PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
389 dev->tapfds[pair_idx] = tapfd;
392 if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
393 PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
397 dev->qp_enabled[pair_idx] = true;
401 struct virtio_user_backend_ops virtio_ops_kernel = {
402 .setup = vhost_kernel_setup,
403 .set_owner = vhost_kernel_set_owner,
404 .get_features = vhost_kernel_get_features,
405 .set_features = vhost_kernel_set_features,
406 .send_request = vhost_kernel_send_request,
407 .enable_qp = vhost_kernel_enable_queue_pair