1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Red Hat Inc.
11 #include <rte_memory.h>
14 #include "virtio_user_dev.h"
16 #define VHOST_VDPA_SUPPORTED_BACKEND_FEATURES \
17 (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | \
18 1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
20 /* vhost kernel & vdpa ioctls */
21 #define VHOST_VIRTIO 0xAF
22 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
23 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
24 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
25 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
26 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
27 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
28 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
29 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
30 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
31 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
32 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
33 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
34 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
35 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
36 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
37 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
38 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
39 #define VHOST_VDPA_SET_VRING_ENABLE _IOW(VHOST_VIRTIO, 0x75, struct vhost_vring_state)
40 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
41 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
43 /* no alignment requirement */
44 struct vhost_iotlb_msg {
48 #define VHOST_ACCESS_RO 0x1
49 #define VHOST_ACCESS_WO 0x2
50 #define VHOST_ACCESS_RW 0x3
52 #define VHOST_IOTLB_MISS 1
53 #define VHOST_IOTLB_UPDATE 2
54 #define VHOST_IOTLB_INVALIDATE 3
55 #define VHOST_IOTLB_ACCESS_FAIL 4
56 #define VHOST_IOTLB_BATCH_BEGIN 5
57 #define VHOST_IOTLB_BATCH_END 6
61 #define VHOST_IOTLB_MSG_V2 0x2
67 struct vhost_iotlb_msg iotlb;
74 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
78 ret = ioctl(fd, request, arg);
80 PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
81 request, strerror(errno));
89 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
91 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
95 vhost_vdpa_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
97 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
101 vhost_vdpa_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
103 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
107 vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
111 ret = vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_FEATURES, features);
113 PMD_DRV_LOG(ERR, "Failed to get features");
117 /* Multiqueue not supported for now */
118 *features &= ~(1ULL << VIRTIO_NET_F_MQ);
120 /* Negotiated vDPA backend features */
121 ret = vhost_vdpa_get_protocol_features(dev, &dev->protocol_features);
123 PMD_DRV_LOG(ERR, "Failed to get backend features");
127 dev->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
129 ret = vhost_vdpa_set_protocol_features(dev, dev->protocol_features);
131 PMD_DRV_LOG(ERR, "Failed to set backend features");
139 vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
142 features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
144 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_FEATURES, &features);
148 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
150 struct vhost_msg msg = {};
152 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
155 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
156 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
160 msg.type = VHOST_IOTLB_MSG_V2;
161 msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
163 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
164 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
173 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
175 struct vhost_msg msg = {};
177 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
180 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
181 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
185 msg.type = VHOST_IOTLB_MSG_V2;
186 msg.iotlb.type = VHOST_IOTLB_BATCH_END;
188 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
189 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
198 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
199 uint64_t iova, size_t len)
201 struct vhost_msg msg = {};
203 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
204 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
208 msg.type = VHOST_IOTLB_MSG_V2;
209 msg.iotlb.type = VHOST_IOTLB_UPDATE;
210 msg.iotlb.iova = iova;
211 msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
212 msg.iotlb.size = len;
213 msg.iotlb.perm = VHOST_ACCESS_RW;
215 PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
216 __func__, iova, addr, len);
218 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
219 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
228 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
229 uint64_t iova, size_t len)
231 struct vhost_msg msg = {};
233 if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
234 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
238 msg.type = VHOST_IOTLB_MSG_V2;
239 msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
240 msg.iotlb.iova = iova;
241 msg.iotlb.size = len;
243 PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
244 __func__, iova, len);
246 if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
247 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
256 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
257 uint64_t iova, size_t len)
261 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
264 ret = vhost_vdpa_dma_map(dev, addr, iova, len);
266 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
273 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
274 uint64_t iova, size_t len)
278 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
281 ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
283 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
290 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
291 const struct rte_memseg *ms, size_t len, void *arg)
293 struct virtio_user_dev *dev = arg;
298 return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
302 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
305 struct virtio_user_dev *dev = arg;
307 /* skip external memory that isn't a heap */
308 if (msl->external && !msl->heap)
311 /* skip any segments with invalid IOVA addresses */
312 if (ms->iova == RTE_BAD_IOVA)
315 /* if IOVA mode is VA, we've already mapped the internal segments */
316 if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
319 return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
323 vhost_vdpa_set_memory_table(struct virtio_user_dev *dev)
327 if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
330 vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
332 if (rte_eal_iova_mode() == RTE_IOVA_VA) {
333 /* with IOVA as VA mode, we can get away with mapping contiguous
334 * chunks rather than going page-by-page.
336 ret = rte_memseg_contig_walk_thread_unsafe(
337 vhost_vdpa_map_contig, dev);
340 /* we have to continue the walk because we've skipped the
341 * external segments during the config walk.
344 ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
347 if (vhost_vdpa_iotlb_batch_end(dev) < 0)
354 vhost_vdpa_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
356 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
360 vhost_vdpa_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
362 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_NUM, state);
366 vhost_vdpa_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
368 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_BASE, state);
372 vhost_vdpa_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
374 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_VRING_BASE, state);
378 vhost_vdpa_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
380 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_CALL, file);
384 vhost_vdpa_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
386 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_KICK, file);
390 vhost_vdpa_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
392 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_ADDR, addr);
396 vhost_vdpa_get_status(struct virtio_user_dev *dev, uint8_t *status)
398 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_GET_STATUS, status);
402 vhost_vdpa_set_status(struct virtio_user_dev *dev, uint8_t status)
404 return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_STATUS, &status);
408 * Set up environment to talk with a vhost vdpa backend.
411 * - (-1) if fail to set up;
412 * - (>=0) if successful.
415 vhost_vdpa_setup(struct virtio_user_dev *dev)
417 uint32_t did = (uint32_t)-1;
419 dev->vhostfd = open(dev->path, O_RDWR);
420 if (dev->vhostfd < 0) {
421 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
422 dev->path, strerror(errno));
426 if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
427 did != VIRTIO_ID_NETWORK) {
428 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
436 vhost_vdpa_destroy(struct virtio_user_dev *dev __rte_unused)
442 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
448 if (dev->qp_enabled[pair_idx] == enable)
451 for (i = 0; i < 2; ++i) {
452 struct vhost_vring_state state = {
453 .index = pair_idx * 2 + i,
457 if (vhost_vdpa_set_vring_enable(dev, &state))
461 dev->qp_enabled[pair_idx] = enable;
467 vhost_vdpa_get_backend_features(uint64_t *features)
475 vhost_vdpa_update_link_state(struct virtio_user_dev *dev __rte_unused)
477 /* Nothing to update (for now?) */
482 vhost_vdpa_get_intr_fd(struct virtio_user_dev *dev __rte_unused)
484 /* No link state interrupt with Vhost-vDPA */
488 struct virtio_user_backend_ops virtio_ops_vdpa = {
489 .setup = vhost_vdpa_setup,
490 .destroy = vhost_vdpa_destroy,
491 .get_backend_features = vhost_vdpa_get_backend_features,
492 .set_owner = vhost_vdpa_set_owner,
493 .get_features = vhost_vdpa_get_features,
494 .set_features = vhost_vdpa_set_features,
495 .set_memory_table = vhost_vdpa_set_memory_table,
496 .set_vring_num = vhost_vdpa_set_vring_num,
497 .set_vring_base = vhost_vdpa_set_vring_base,
498 .get_vring_base = vhost_vdpa_get_vring_base,
499 .set_vring_call = vhost_vdpa_set_vring_call,
500 .set_vring_kick = vhost_vdpa_set_vring_kick,
501 .set_vring_addr = vhost_vdpa_set_vring_addr,
502 .get_status = vhost_vdpa_get_status,
503 .set_status = vhost_vdpa_set_status,
504 .enable_qp = vhost_vdpa_enable_queue_pair,
505 .dma_map = vhost_vdpa_dma_map_batch,
506 .dma_unmap = vhost_vdpa_dma_unmap_batch,
507 .update_link_state = vhost_vdpa_update_link_state,
508 .get_intr_fd = vhost_vdpa_get_intr_fd,