1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <sys/epoll.h>
11 #include <linux/virtio_net.h>
14 #include <rte_eal_paging.h>
15 #include <rte_malloc.h>
16 #include <rte_memory.h>
17 #include <rte_bus_pci.h>
18 #include <rte_vhost.h>
20 #include <vdpa_driver.h>
22 #include <rte_spinlock.h>
24 #include <rte_kvargs.h>
25 #include <rte_devargs.h>
27 #include "base/ifcvf.h"
29 RTE_LOG_REGISTER(ifcvf_vdpa_logtype, pmd.vdpa.ifcvf, NOTICE);
30 #define DRV_LOG(level, fmt, args...) \
31 rte_log(RTE_LOG_ ## level, ifcvf_vdpa_logtype, \
32 "IFCVF %s(): " fmt "\n", __func__, ##args)
34 #define IFCVF_USED_RING_LEN(size) \
35 ((size) * sizeof(struct vring_used_elem) + sizeof(uint16_t) * 3)
37 #define IFCVF_VDPA_MODE "vdpa"
38 #define IFCVF_SW_FALLBACK_LM "sw-live-migration"
40 #define THREAD_NAME_LEN 16
42 static const char * const ifcvf_valid_arguments[] = {
48 struct ifcvf_internal {
49 struct rte_pci_device *pdev;
52 int vfio_container_fd;
55 pthread_t tid; /* thread for notify relay */
58 struct rte_vdpa_device *vdev;
61 rte_atomic32_t started;
62 rte_atomic32_t dev_attached;
63 rte_atomic32_t running;
66 bool sw_fallback_running;
67 /* mediated vring for sw fallback */
68 struct vring m_vring[IFCVF_MAX_QUEUES * 2];
69 /* eventfd for used ring interrupt */
70 int intr_fd[IFCVF_MAX_QUEUES * 2];
73 struct internal_list {
74 TAILQ_ENTRY(internal_list) next;
75 struct ifcvf_internal *internal;
78 TAILQ_HEAD(internal_list_head, internal_list);
79 static struct internal_list_head internal_list =
80 TAILQ_HEAD_INITIALIZER(internal_list);
82 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
84 static void update_used_ring(struct ifcvf_internal *internal, uint16_t qid);
86 static struct internal_list *
87 find_internal_resource_by_vdev(struct rte_vdpa_device *vdev)
90 struct internal_list *list;
92 pthread_mutex_lock(&internal_list_lock);
94 TAILQ_FOREACH(list, &internal_list, next) {
95 if (vdev == list->internal->vdev) {
101 pthread_mutex_unlock(&internal_list_lock);
109 static struct internal_list *
110 find_internal_resource_by_dev(struct rte_pci_device *pdev)
113 struct internal_list *list;
115 pthread_mutex_lock(&internal_list_lock);
117 TAILQ_FOREACH(list, &internal_list, next) {
118 if (!rte_pci_addr_cmp(&pdev->addr,
119 &list->internal->pdev->addr)) {
125 pthread_mutex_unlock(&internal_list_lock);
134 ifcvf_vfio_setup(struct ifcvf_internal *internal)
136 struct rte_pci_device *dev = internal->pdev;
137 char devname[RTE_DEV_NAME_MAX_LEN] = {0};
141 internal->vfio_dev_fd = -1;
142 internal->vfio_group_fd = -1;
143 internal->vfio_container_fd = -1;
145 rte_pci_device_name(&dev->addr, devname, RTE_DEV_NAME_MAX_LEN);
146 ret = rte_vfio_get_group_num(rte_pci_get_sysfs_path(), devname,
149 DRV_LOG(ERR, "%s failed to get IOMMU group", devname);
153 internal->vfio_container_fd = rte_vfio_container_create();
154 if (internal->vfio_container_fd < 0)
157 internal->vfio_group_fd = rte_vfio_container_group_bind(
158 internal->vfio_container_fd, iommu_group_num);
159 if (internal->vfio_group_fd < 0)
162 if (rte_pci_map_device(dev))
165 internal->vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
167 for (i = 0; i < RTE_MIN(PCI_MAX_RESOURCE, IFCVF_PCI_MAX_RESOURCE);
169 internal->hw.mem_resource[i].addr =
170 internal->pdev->mem_resource[i].addr;
171 internal->hw.mem_resource[i].phys_addr =
172 internal->pdev->mem_resource[i].phys_addr;
173 internal->hw.mem_resource[i].len =
174 internal->pdev->mem_resource[i].len;
180 rte_vfio_container_destroy(internal->vfio_container_fd);
185 ifcvf_dma_map(struct ifcvf_internal *internal, bool do_map)
189 struct rte_vhost_memory *mem = NULL;
190 int vfio_container_fd;
192 ret = rte_vhost_get_mem_table(internal->vid, &mem);
194 DRV_LOG(ERR, "failed to get VM memory layout.");
198 vfio_container_fd = internal->vfio_container_fd;
200 for (i = 0; i < mem->nregions; i++) {
201 struct rte_vhost_mem_region *reg;
203 reg = &mem->regions[i];
204 DRV_LOG(INFO, "%s, region %u: HVA 0x%" PRIx64 ", "
205 "GPA 0x%" PRIx64 ", size 0x%" PRIx64 ".",
206 do_map ? "DMA map" : "DMA unmap", i,
207 reg->host_user_addr, reg->guest_phys_addr, reg->size);
210 ret = rte_vfio_container_dma_map(vfio_container_fd,
211 reg->host_user_addr, reg->guest_phys_addr,
214 DRV_LOG(ERR, "DMA map failed.");
218 ret = rte_vfio_container_dma_unmap(vfio_container_fd,
219 reg->host_user_addr, reg->guest_phys_addr,
222 DRV_LOG(ERR, "DMA unmap failed.");
235 hva_to_gpa(int vid, uint64_t hva)
237 struct rte_vhost_memory *mem = NULL;
238 struct rte_vhost_mem_region *reg;
242 if (rte_vhost_get_mem_table(vid, &mem) < 0)
245 for (i = 0; i < mem->nregions; i++) {
246 reg = &mem->regions[i];
248 if (hva >= reg->host_user_addr &&
249 hva < reg->host_user_addr + reg->size) {
250 gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
262 vdpa_ifcvf_start(struct ifcvf_internal *internal)
264 struct ifcvf_hw *hw = &internal->hw;
267 struct rte_vhost_vring vq;
271 nr_vring = rte_vhost_get_vring_num(vid);
272 rte_vhost_get_negotiated_features(vid, &hw->req_features);
274 for (i = 0; i < nr_vring; i++) {
275 rte_vhost_get_vhost_vring(vid, i, &vq);
276 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
278 DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
281 hw->vring[i].desc = gpa;
283 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
285 DRV_LOG(ERR, "Fail to get GPA for available ring.");
288 hw->vring[i].avail = gpa;
290 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
292 DRV_LOG(ERR, "Fail to get GPA for used ring.");
295 hw->vring[i].used = gpa;
297 hw->vring[i].size = vq.size;
298 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
299 &hw->vring[i].last_used_idx);
303 return ifcvf_start_hw(&internal->hw);
307 vdpa_ifcvf_stop(struct ifcvf_internal *internal)
309 struct ifcvf_hw *hw = &internal->hw;
312 uint64_t features = 0;
313 uint64_t log_base = 0, log_size = 0;
319 for (i = 0; i < hw->nr_vring; i++)
320 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
321 hw->vring[i].last_used_idx);
326 rte_vhost_get_negotiated_features(vid, &features);
327 if (RTE_VHOST_NEED_LOG(features)) {
328 ifcvf_disable_logging(hw);
329 rte_vhost_get_log_base(internal->vid, &log_base, &log_size);
330 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
331 log_base, IFCVF_LOG_BASE, log_size);
333 * IFCVF marks dirty memory pages for only packet buffer,
334 * SW helps to mark the used ring as dirty after device stops.
336 for (i = 0; i < hw->nr_vring; i++) {
337 len = IFCVF_USED_RING_LEN(hw->vring[i].size);
338 rte_vhost_log_used_vring(vid, i, 0, len);
343 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + \
344 sizeof(int) * (IFCVF_MAX_QUEUES * 2 + 1))
346 vdpa_enable_vfio_intr(struct ifcvf_internal *internal, bool m_rx)
349 uint32_t i, nr_vring;
350 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
351 struct vfio_irq_set *irq_set;
353 struct rte_vhost_vring vring;
358 nr_vring = rte_vhost_get_vring_num(internal->vid);
360 irq_set = (struct vfio_irq_set *)irq_set_buf;
361 irq_set->argsz = sizeof(irq_set_buf);
362 irq_set->count = nr_vring + 1;
363 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
364 VFIO_IRQ_SET_ACTION_TRIGGER;
365 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
367 fd_ptr = (int *)&irq_set->data;
368 fd_ptr[RTE_INTR_VEC_ZERO_OFFSET] =
369 rte_intr_fd_get(internal->pdev->intr_handle);
371 for (i = 0; i < nr_vring; i++)
372 internal->intr_fd[i] = -1;
374 for (i = 0; i < nr_vring; i++) {
375 rte_vhost_get_vhost_vring(internal->vid, i, &vring);
376 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = vring.callfd;
377 if ((i & 1) == 0 && m_rx == true) {
378 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
380 DRV_LOG(ERR, "can't setup eventfd: %s",
384 internal->intr_fd[i] = fd;
385 fd_ptr[RTE_INTR_VEC_RXTX_OFFSET + i] = fd;
389 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
391 DRV_LOG(ERR, "Error enabling MSI-X interrupts: %s",
400 vdpa_disable_vfio_intr(struct ifcvf_internal *internal)
403 uint32_t i, nr_vring;
404 char irq_set_buf[MSIX_IRQ_SET_BUF_LEN];
405 struct vfio_irq_set *irq_set;
407 irq_set = (struct vfio_irq_set *)irq_set_buf;
408 irq_set->argsz = sizeof(irq_set_buf);
410 irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
411 irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
414 nr_vring = rte_vhost_get_vring_num(internal->vid);
415 for (i = 0; i < nr_vring; i++) {
416 if (internal->intr_fd[i] >= 0)
417 close(internal->intr_fd[i]);
418 internal->intr_fd[i] = -1;
421 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
423 DRV_LOG(ERR, "Error disabling MSI-X interrupts: %s",
432 notify_relay(void *arg)
434 int i, kickfd, epfd, nfds = 0;
436 struct epoll_event events[IFCVF_MAX_QUEUES * 2];
437 struct epoll_event ev;
440 struct rte_vhost_vring vring;
441 struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
442 struct ifcvf_hw *hw = &internal->hw;
444 q_num = rte_vhost_get_vring_num(internal->vid);
446 epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
448 DRV_LOG(ERR, "failed to create epoll instance.");
451 internal->epfd = epfd;
454 for (qid = 0; qid < q_num; qid++) {
455 ev.events = EPOLLIN | EPOLLPRI;
456 rte_vhost_get_vhost_vring(internal->vid, qid, &vring);
457 ev.data.u64 = qid | (uint64_t)vring.kickfd << 32;
458 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
459 DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
465 nfds = epoll_wait(epfd, events, q_num, -1);
469 DRV_LOG(ERR, "epoll_wait return fail\n");
473 for (i = 0; i < nfds; i++) {
474 qid = events[i].data.u32;
475 kickfd = (uint32_t)(events[i].data.u64 >> 32);
477 nbytes = read(kickfd, &buf, 8);
479 if (errno == EINTR ||
480 errno == EWOULDBLOCK ||
483 DRV_LOG(INFO, "Error reading "
490 ifcvf_notify_queue(hw, qid);
498 setup_notify_relay(struct ifcvf_internal *internal)
500 char name[THREAD_NAME_LEN];
503 snprintf(name, sizeof(name), "ifc-notify-%d", internal->vid);
504 ret = rte_ctrl_thread_create(&internal->tid, name, NULL, notify_relay,
507 DRV_LOG(ERR, "failed to create notify relay pthread.");
515 unset_notify_relay(struct ifcvf_internal *internal)
520 pthread_cancel(internal->tid);
521 pthread_join(internal->tid, &status);
525 if (internal->epfd >= 0)
526 close(internal->epfd);
533 update_datapath(struct ifcvf_internal *internal)
537 rte_spinlock_lock(&internal->lock);
539 if (!rte_atomic32_read(&internal->running) &&
540 (rte_atomic32_read(&internal->started) &&
541 rte_atomic32_read(&internal->dev_attached))) {
542 ret = ifcvf_dma_map(internal, true);
546 ret = vdpa_enable_vfio_intr(internal, false);
550 ret = vdpa_ifcvf_start(internal);
554 ret = setup_notify_relay(internal);
558 rte_atomic32_set(&internal->running, 1);
559 } else if (rte_atomic32_read(&internal->running) &&
560 (!rte_atomic32_read(&internal->started) ||
561 !rte_atomic32_read(&internal->dev_attached))) {
562 ret = unset_notify_relay(internal);
566 vdpa_ifcvf_stop(internal);
568 ret = vdpa_disable_vfio_intr(internal);
572 ret = ifcvf_dma_map(internal, false);
576 rte_atomic32_set(&internal->running, 0);
579 rte_spinlock_unlock(&internal->lock);
582 rte_spinlock_unlock(&internal->lock);
587 m_ifcvf_start(struct ifcvf_internal *internal)
589 struct ifcvf_hw *hw = &internal->hw;
590 uint32_t i, nr_vring;
592 struct rte_vhost_vring vq;
594 uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
598 memset(&vq, 0, sizeof(vq));
600 nr_vring = rte_vhost_get_vring_num(vid);
601 rte_vhost_get_negotiated_features(vid, &hw->req_features);
603 for (i = 0; i < nr_vring; i++) {
604 rte_vhost_get_vhost_vring(vid, i, &vq);
606 size = RTE_ALIGN_CEIL(vring_size(vq.size, rte_mem_page_size()),
607 rte_mem_page_size());
608 vring_buf = rte_zmalloc("ifcvf", size, rte_mem_page_size());
609 vring_init(&internal->m_vring[i], vq.size, vring_buf,
610 rte_mem_page_size());
612 ret = rte_vfio_container_dma_map(internal->vfio_container_fd,
613 (uint64_t)(uintptr_t)vring_buf, m_vring_iova, size);
615 DRV_LOG(ERR, "mediated vring DMA map failed.");
619 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.desc);
621 DRV_LOG(ERR, "Fail to get GPA for descriptor ring.");
624 hw->vring[i].desc = gpa;
626 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.avail);
628 DRV_LOG(ERR, "Fail to get GPA for available ring.");
631 hw->vring[i].avail = gpa;
633 /* Direct I/O for Tx queue, relay for Rx queue */
635 gpa = hva_to_gpa(vid, (uint64_t)(uintptr_t)vq.used);
637 DRV_LOG(ERR, "Fail to get GPA for used ring.");
640 hw->vring[i].used = gpa;
642 hw->vring[i].used = m_vring_iova +
643 (char *)internal->m_vring[i].used -
644 (char *)internal->m_vring[i].desc;
647 hw->vring[i].size = vq.size;
649 rte_vhost_get_vring_base(vid, i,
650 &internal->m_vring[i].avail->idx,
651 &internal->m_vring[i].used->idx);
653 rte_vhost_get_vring_base(vid, i, &hw->vring[i].last_avail_idx,
654 &hw->vring[i].last_used_idx);
656 m_vring_iova += size;
658 hw->nr_vring = nr_vring;
660 return ifcvf_start_hw(&internal->hw);
663 for (i = 0; i < nr_vring; i++)
664 if (internal->m_vring[i].desc)
665 rte_free(internal->m_vring[i].desc);
671 m_ifcvf_stop(struct ifcvf_internal *internal)
675 struct rte_vhost_vring vq;
676 struct ifcvf_hw *hw = &internal->hw;
677 uint64_t m_vring_iova = IFCVF_MEDIATED_VRING;
683 for (i = 0; i < hw->nr_vring; i++) {
684 /* synchronize remaining new used entries if any */
686 update_used_ring(internal, i);
688 rte_vhost_get_vhost_vring(vid, i, &vq);
689 len = IFCVF_USED_RING_LEN(vq.size);
690 rte_vhost_log_used_vring(vid, i, 0, len);
692 size = RTE_ALIGN_CEIL(vring_size(vq.size, rte_mem_page_size()),
693 rte_mem_page_size());
694 rte_vfio_container_dma_unmap(internal->vfio_container_fd,
695 (uint64_t)(uintptr_t)internal->m_vring[i].desc,
698 rte_vhost_set_vring_base(vid, i, hw->vring[i].last_avail_idx,
699 hw->vring[i].last_used_idx);
700 rte_free(internal->m_vring[i].desc);
701 m_vring_iova += size;
708 update_used_ring(struct ifcvf_internal *internal, uint16_t qid)
710 rte_vdpa_relay_vring_used(internal->vid, qid, &internal->m_vring[qid]);
711 rte_vhost_vring_call(internal->vid, qid);
715 vring_relay(void *arg)
717 int i, vid, epfd, fd, nfds;
718 struct ifcvf_internal *internal = (struct ifcvf_internal *)arg;
719 struct rte_vhost_vring vring;
721 struct epoll_event events[IFCVF_MAX_QUEUES * 4];
722 struct epoll_event ev;
727 q_num = rte_vhost_get_vring_num(vid);
729 /* add notify fd and interrupt fd to epoll */
730 epfd = epoll_create(IFCVF_MAX_QUEUES * 2);
732 DRV_LOG(ERR, "failed to create epoll instance.");
735 internal->epfd = epfd;
738 for (qid = 0; qid < q_num; qid++) {
739 ev.events = EPOLLIN | EPOLLPRI;
740 rte_vhost_get_vhost_vring(vid, qid, &vring);
741 ev.data.u64 = qid << 1 | (uint64_t)vring.kickfd << 32;
742 if (epoll_ctl(epfd, EPOLL_CTL_ADD, vring.kickfd, &ev) < 0) {
743 DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
748 for (qid = 0; qid < q_num; qid += 2) {
749 ev.events = EPOLLIN | EPOLLPRI;
750 /* leave a flag to mark it's for interrupt */
751 ev.data.u64 = 1 | qid << 1 |
752 (uint64_t)internal->intr_fd[qid] << 32;
753 if (epoll_ctl(epfd, EPOLL_CTL_ADD, internal->intr_fd[qid], &ev)
755 DRV_LOG(ERR, "epoll add error: %s", strerror(errno));
758 update_used_ring(internal, qid);
761 /* start relay with a first kick */
762 for (qid = 0; qid < q_num; qid++)
763 ifcvf_notify_queue(&internal->hw, qid);
765 /* listen to the events and react accordingly */
767 nfds = epoll_wait(epfd, events, q_num * 2, -1);
771 DRV_LOG(ERR, "epoll_wait return fail\n");
775 for (i = 0; i < nfds; i++) {
776 fd = (uint32_t)(events[i].data.u64 >> 32);
778 nbytes = read(fd, &buf, 8);
780 if (errno == EINTR ||
781 errno == EWOULDBLOCK ||
784 DRV_LOG(INFO, "Error reading "
791 qid = events[i].data.u32 >> 1;
793 if (events[i].data.u32 & 1)
794 update_used_ring(internal, qid);
796 ifcvf_notify_queue(&internal->hw, qid);
804 setup_vring_relay(struct ifcvf_internal *internal)
806 char name[THREAD_NAME_LEN];
809 snprintf(name, sizeof(name), "ifc-vring-%d", internal->vid);
810 ret = rte_ctrl_thread_create(&internal->tid, name, NULL, vring_relay,
813 DRV_LOG(ERR, "failed to create ring relay pthread.");
821 unset_vring_relay(struct ifcvf_internal *internal)
826 pthread_cancel(internal->tid);
827 pthread_join(internal->tid, &status);
831 if (internal->epfd >= 0)
832 close(internal->epfd);
839 ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
842 int vid = internal->vid;
844 /* stop the direct IO data path */
845 unset_notify_relay(internal);
846 vdpa_ifcvf_stop(internal);
847 vdpa_disable_vfio_intr(internal);
849 ret = rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, false);
850 if (ret && ret != -ENOTSUP)
853 /* set up interrupt for interrupt relay */
854 ret = vdpa_enable_vfio_intr(internal, true);
859 ret = m_ifcvf_start(internal);
863 /* set up vring relay thread */
864 ret = setup_vring_relay(internal);
868 rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true);
870 internal->sw_fallback_running = true;
875 m_ifcvf_stop(internal);
877 vdpa_disable_vfio_intr(internal);
879 ifcvf_dma_map(internal, false);
885 ifcvf_dev_config(int vid)
887 struct rte_vdpa_device *vdev;
888 struct internal_list *list;
889 struct ifcvf_internal *internal;
891 vdev = rte_vhost_get_vdpa_device(vid);
892 list = find_internal_resource_by_vdev(vdev);
894 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
898 internal = list->internal;
900 rte_atomic32_set(&internal->dev_attached, 1);
901 update_datapath(internal);
903 if (rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true) != 0)
904 DRV_LOG(NOTICE, "vDPA (%s): software relay is used.",
907 internal->configured = 1;
912 ifcvf_dev_close(int vid)
914 struct rte_vdpa_device *vdev;
915 struct internal_list *list;
916 struct ifcvf_internal *internal;
918 vdev = rte_vhost_get_vdpa_device(vid);
919 list = find_internal_resource_by_vdev(vdev);
921 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
925 internal = list->internal;
927 if (internal->sw_fallback_running) {
928 /* unset ring relay */
929 unset_vring_relay(internal);
932 m_ifcvf_stop(internal);
934 /* remove interrupt setting */
935 vdpa_disable_vfio_intr(internal);
937 /* unset DMA map for guest memory */
938 ifcvf_dma_map(internal, false);
940 internal->sw_fallback_running = false;
942 rte_atomic32_set(&internal->dev_attached, 0);
943 update_datapath(internal);
946 internal->configured = 0;
951 ifcvf_set_features(int vid)
953 uint64_t features = 0;
954 struct rte_vdpa_device *vdev;
955 struct internal_list *list;
956 struct ifcvf_internal *internal;
957 uint64_t log_base = 0, log_size = 0;
959 vdev = rte_vhost_get_vdpa_device(vid);
960 list = find_internal_resource_by_vdev(vdev);
962 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
966 internal = list->internal;
967 rte_vhost_get_negotiated_features(vid, &features);
969 if (!RTE_VHOST_NEED_LOG(features))
972 if (internal->sw_lm) {
973 ifcvf_sw_fallback_switchover(internal);
975 rte_vhost_get_log_base(vid, &log_base, &log_size);
976 rte_vfio_container_dma_map(internal->vfio_container_fd,
977 log_base, IFCVF_LOG_BASE, log_size);
978 ifcvf_enable_logging(&internal->hw, IFCVF_LOG_BASE, log_size);
985 ifcvf_get_vfio_group_fd(int vid)
987 struct rte_vdpa_device *vdev;
988 struct internal_list *list;
990 vdev = rte_vhost_get_vdpa_device(vid);
991 list = find_internal_resource_by_vdev(vdev);
993 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
997 return list->internal->vfio_group_fd;
1001 ifcvf_get_vfio_device_fd(int vid)
1003 struct rte_vdpa_device *vdev;
1004 struct internal_list *list;
1006 vdev = rte_vhost_get_vdpa_device(vid);
1007 list = find_internal_resource_by_vdev(vdev);
1009 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1013 return list->internal->vfio_dev_fd;
1017 ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
1019 struct rte_vdpa_device *vdev;
1020 struct internal_list *list;
1021 struct ifcvf_internal *internal;
1022 struct vfio_region_info reg = { .argsz = sizeof(reg) };
1025 vdev = rte_vhost_get_vdpa_device(vid);
1026 list = find_internal_resource_by_vdev(vdev);
1028 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1032 internal = list->internal;
1034 reg.index = ifcvf_get_notify_region(&internal->hw);
1035 ret = ioctl(internal->vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ®);
1037 DRV_LOG(ERR, "Get not get device region info: %s",
1042 *offset = ifcvf_get_queue_notify_off(&internal->hw, qid) + reg.offset;
1049 ifcvf_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
1051 struct internal_list *list;
1053 list = find_internal_resource_by_vdev(vdev);
1055 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1059 *queue_num = list->internal->max_queues;
1065 ifcvf_get_vdpa_features(struct rte_vdpa_device *vdev, uint64_t *features)
1067 struct internal_list *list;
1069 list = find_internal_resource_by_vdev(vdev);
1071 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1075 *features = list->internal->features;
1080 #define VDPA_SUPPORTED_PROTOCOL_FEATURES \
1081 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
1082 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ | \
1083 1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD | \
1084 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
1085 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD | \
1086 1ULL << VHOST_USER_PROTOCOL_F_STATUS)
1088 ifcvf_get_protocol_features(struct rte_vdpa_device *vdev, uint64_t *features)
1092 *features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
1097 ifcvf_set_vring_state(int vid, int vring, int state)
1099 struct rte_vdpa_device *vdev;
1100 struct internal_list *list;
1101 struct ifcvf_internal *internal;
1102 struct ifcvf_hw *hw;
1103 struct ifcvf_pci_common_cfg *cfg;
1106 vdev = rte_vhost_get_vdpa_device(vid);
1107 list = find_internal_resource_by_vdev(vdev);
1109 DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
1113 internal = list->internal;
1114 if (vring < 0 || vring >= internal->max_queues * 2) {
1115 DRV_LOG(ERR, "Vring index %d not correct", vring);
1120 if (!internal->configured)
1123 cfg = hw->common_cfg;
1124 IFCVF_WRITE_REG16(vring, &cfg->queue_select);
1125 IFCVF_WRITE_REG16(!!state, &cfg->queue_enable);
1127 if (!state && hw->vring[vring].enable) {
1128 ret = vdpa_disable_vfio_intr(internal);
1133 if (state && !hw->vring[vring].enable) {
1134 ret = vdpa_enable_vfio_intr(internal, false);
1140 hw->vring[vring].enable = !!state;
1144 static struct rte_vdpa_dev_ops ifcvf_ops = {
1145 .get_queue_num = ifcvf_get_queue_num,
1146 .get_features = ifcvf_get_vdpa_features,
1147 .get_protocol_features = ifcvf_get_protocol_features,
1148 .dev_conf = ifcvf_dev_config,
1149 .dev_close = ifcvf_dev_close,
1150 .set_vring_state = ifcvf_set_vring_state,
1151 .set_features = ifcvf_set_features,
1152 .migration_done = NULL,
1153 .get_vfio_group_fd = ifcvf_get_vfio_group_fd,
1154 .get_vfio_device_fd = ifcvf_get_vfio_device_fd,
1155 .get_notify_area = ifcvf_get_notify_area,
1159 open_int(const char *key __rte_unused, const char *value, void *extra_args)
1161 uint16_t *n = extra_args;
1163 if (value == NULL || extra_args == NULL)
1166 *n = (uint16_t)strtoul(value, NULL, 0);
1167 if (*n == USHRT_MAX && errno == ERANGE)
1174 ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1175 struct rte_pci_device *pci_dev)
1178 struct ifcvf_internal *internal = NULL;
1179 struct internal_list *list = NULL;
1181 int sw_fallback_lm = 0;
1182 struct rte_kvargs *kvlist = NULL;
1185 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1188 if (!pci_dev->device.devargs)
1191 kvlist = rte_kvargs_parse(pci_dev->device.devargs->args,
1192 ifcvf_valid_arguments);
1196 /* probe only when vdpa mode is specified */
1197 if (rte_kvargs_count(kvlist, IFCVF_VDPA_MODE) == 0) {
1198 rte_kvargs_free(kvlist);
1202 ret = rte_kvargs_process(kvlist, IFCVF_VDPA_MODE, &open_int,
1204 if (ret < 0 || vdpa_mode == 0) {
1205 rte_kvargs_free(kvlist);
1209 list = rte_zmalloc("ifcvf", sizeof(*list), 0);
1213 internal = rte_zmalloc("ifcvf", sizeof(*internal), 0);
1214 if (internal == NULL)
1217 internal->pdev = pci_dev;
1218 rte_spinlock_init(&internal->lock);
1220 if (ifcvf_vfio_setup(internal) < 0) {
1221 DRV_LOG(ERR, "failed to setup device %s", pci_dev->name);
1225 if (ifcvf_init_hw(&internal->hw, internal->pdev) < 0) {
1226 DRV_LOG(ERR, "failed to init device %s", pci_dev->name);
1230 internal->configured = 0;
1231 internal->max_queues = IFCVF_MAX_QUEUES;
1232 features = ifcvf_get_features(&internal->hw);
1233 internal->features = (features &
1234 ~(1ULL << VIRTIO_F_IOMMU_PLATFORM)) |
1235 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
1236 (1ULL << VIRTIO_NET_F_CTRL_VQ) |
1237 (1ULL << VIRTIO_NET_F_STATUS) |
1238 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) |
1239 (1ULL << VHOST_F_LOG_ALL);
1241 list->internal = internal;
1243 if (rte_kvargs_count(kvlist, IFCVF_SW_FALLBACK_LM)) {
1244 ret = rte_kvargs_process(kvlist, IFCVF_SW_FALLBACK_LM,
1245 &open_int, &sw_fallback_lm);
1249 internal->sw_lm = sw_fallback_lm;
1251 internal->vdev = rte_vdpa_register_device(&pci_dev->device, &ifcvf_ops);
1252 if (internal->vdev == NULL) {
1253 DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
1257 pthread_mutex_lock(&internal_list_lock);
1258 TAILQ_INSERT_TAIL(&internal_list, list, next);
1259 pthread_mutex_unlock(&internal_list_lock);
1261 rte_atomic32_set(&internal->started, 1);
1262 update_datapath(internal);
1264 rte_kvargs_free(kvlist);
1268 rte_kvargs_free(kvlist);
1275 ifcvf_pci_remove(struct rte_pci_device *pci_dev)
1277 struct ifcvf_internal *internal;
1278 struct internal_list *list;
1280 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1283 list = find_internal_resource_by_dev(pci_dev);
1285 DRV_LOG(ERR, "Invalid device: %s", pci_dev->name);
1289 internal = list->internal;
1290 rte_atomic32_set(&internal->started, 0);
1291 update_datapath(internal);
1293 rte_pci_unmap_device(internal->pdev);
1294 rte_vfio_container_destroy(internal->vfio_container_fd);
1295 rte_vdpa_unregister_device(internal->vdev);
1297 pthread_mutex_lock(&internal_list_lock);
1298 TAILQ_REMOVE(&internal_list, list, next);
1299 pthread_mutex_unlock(&internal_list_lock);
1308 * IFCVF has the same vendor ID and device ID as virtio net PCI
1309 * device, with its specific subsystem vendor ID and device ID.
1311 static const struct rte_pci_id pci_id_ifcvf_map[] = {
1312 { .class_id = RTE_CLASS_ANY_ID,
1313 .vendor_id = IFCVF_VENDOR_ID,
1314 .device_id = IFCVF_DEVICE_ID,
1315 .subsystem_vendor_id = IFCVF_SUBSYS_VENDOR_ID,
1316 .subsystem_device_id = IFCVF_SUBSYS_DEVICE_ID,
1319 { .vendor_id = 0, /* sentinel */
1323 static struct rte_pci_driver rte_ifcvf_vdpa = {
1324 .id_table = pci_id_ifcvf_map,
1326 .probe = ifcvf_pci_probe,
1327 .remove = ifcvf_pci_remove,
1330 RTE_PMD_REGISTER_PCI(net_ifcvf, rte_ifcvf_vdpa);
1331 RTE_PMD_REGISTER_PCI_TABLE(net_ifcvf, pci_id_ifcvf_map);
1332 RTE_PMD_REGISTER_KMOD_DEP(net_ifcvf, "* vfio-pci");