LCOV - code coverage report
Current view: top level - lib/eal/linux - eal_vfio_cdev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 164 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 15 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 88 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2025 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <dirent.h>
       6                 :            : #include <fcntl.h>
       7                 :            : #include <limits.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <string.h>
      10                 :            : #include <sys/ioctl.h>
      11                 :            : #include <unistd.h>
      12                 :            : 
      13                 :            : #include <uapi/linux/iommufd.h>
      14                 :            : #include <uapi/linux/vfio.h>
      15                 :            : 
      16                 :            : #include <rte_log.h>
      17                 :            : #include <rte_errno.h>
      18                 :            : #include <rte_memory.h>
      19                 :            : #include <rte_string_fns.h>
      20                 :            : 
      21                 :            : #include "eal_vfio.h"
      22                 :            : #include "eal_private.h"
      23                 :            : #include "eal_internal_cfg.h"
      24                 :            : 
      25                 :            : #define DEV_VFIO_IOMMUFD_PATH "/dev/iommu"
      26                 :            : #define DEV_VFIO_CDEV_FMT "/dev/vfio/devices/%s%d"
      27                 :            : 
      28                 :            : /*
      29                 :            :  * Global no-IOMMU mode does not mean every device is no-IOMMU: the kernel only
      30                 :            :  * creates no-IOMMU devices for devices without an IOMMU group, so cdev names
      31                 :            :  * are tried in this order regardless of IOVA mode.
      32                 :            :  */
      33                 :            : static const char * const cdev_prefixes[] = {"vfio", "noiommu-vfio"};
      34                 :            : 
      35                 :            : /* return length of the matching cdev prefix, or 0 if the name has none */
      36                 :            : static size_t
      37                 :          0 : cdev_match_prefix(const char *name)
      38                 :            : {
      39                 :            :         unsigned int i;
      40                 :            : 
      41         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(cdev_prefixes); i++) {
      42                 :          0 :                 size_t len = strlen(cdev_prefixes[i]);
      43                 :            : 
      44         [ #  # ]:          0 :                 if (strncmp(name, cdev_prefixes[i], len) == 0)
      45                 :          0 :                         return len;
      46                 :            :         }
      47                 :            :         return 0;
      48                 :            : }
      49                 :            : 
      50                 :            : static int vfio_cdev_dma_map(struct vfio_container *cfg);
      51                 :            : static int vfio_cdev_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr,
      52                 :            :                 uint64_t iova, uint64_t len, int do_map);
      53                 :            : 
      54                 :            : /* IOMMUFD cdev mode IOMMU operations */
      55                 :            : static const struct vfio_iommu_ops iommufd_ops = {
      56                 :            :         .type_id = 0, /* cdev mode doesn't use type_id */
      57                 :            :         .name = "IOMMUFD",
      58                 :            :         .partial_unmap = false,
      59                 :            :         .dma_map_func = &vfio_cdev_dma_map,
      60                 :            :         .dma_user_map_func = &vfio_cdev_dma_mem_map
      61                 :            : };
      62                 :            : 
      63                 :            : void
      64                 :          0 : vfio_cdev_setup_ops(void)
      65                 :            : {
      66                 :          0 :         vfio_global_cfg.ops = &iommufd_ops;
      67                 :          0 : }
      68                 :            : 
      69                 :            : static int
      70                 :          0 : vfio_cdev_dma_mem_map(struct vfio_container *cfg, uint64_t vaddr, uint64_t iova,
      71                 :            :                 uint64_t len, int do_map)
      72                 :            : {
      73                 :            :         struct iommu_ioas_map ioas_map;
      74                 :            :         struct iommu_ioas_unmap ioas_unmap;
      75                 :            :         int ret;
      76                 :            : 
      77         [ #  # ]:          0 :         if (do_map != 0) {
      78                 :            :                 memset(&ioas_map, 0, sizeof(ioas_map));
      79                 :          0 :                 ioas_map.size = sizeof(struct iommu_ioas_map);
      80                 :          0 :                 ioas_map.flags = IOMMU_IOAS_MAP_FIXED_IOVA |
      81                 :            :                         IOMMU_IOAS_MAP_READABLE |
      82                 :            :                         IOMMU_IOAS_MAP_WRITEABLE;
      83                 :          0 :                 ioas_map.ioas_id = cfg->cdev_cfg.ioas_id;
      84                 :          0 :                 ioas_map.user_va = vaddr;
      85                 :          0 :                 ioas_map.length = len;
      86                 :          0 :                 ioas_map.iova = iova;
      87                 :            : 
      88                 :          0 :                 ret = ioctl(cfg->container_fd, IOMMU_IOAS_MAP, &ioas_map);
      89         [ #  # ]:          0 :                 if (ret) {
      90                 :            :                         /**
      91                 :            :                          * In case the mapping was already done EEXIST will be
      92                 :            :                          * returned from kernel.
      93                 :            :                          */
      94         [ #  # ]:          0 :                         if (errno == EEXIST) {
      95                 :          0 :                                 EAL_LOG(DEBUG, "Memory segment is already mapped, skipping");
      96                 :            :                         } else {
      97                 :          0 :                                 EAL_LOG(ERR, "Cannot set up DMA remapping, error %i (%s)", errno,
      98                 :            :                                         strerror(errno));
      99                 :          0 :                                 return -1;
     100                 :            :                         }
     101                 :            :                 }
     102                 :            :         } else {
     103                 :            :                 memset(&ioas_unmap, 0, sizeof(ioas_unmap));
     104                 :          0 :                 ioas_unmap.size = sizeof(struct iommu_ioas_unmap);
     105                 :          0 :                 ioas_unmap.ioas_id = cfg->cdev_cfg.ioas_id;
     106                 :          0 :                 ioas_unmap.length = len;
     107                 :          0 :                 ioas_unmap.iova = iova;
     108                 :            : 
     109                 :          0 :                 ret = ioctl(cfg->container_fd, IOMMU_IOAS_UNMAP, &ioas_unmap);
     110         [ #  # ]:          0 :                 if (ret) {
     111                 :          0 :                         EAL_LOG(ERR, "Cannot clear DMA remapping, error %i (%s)", errno,
     112                 :            :                                 strerror(errno));
     113                 :          0 :                         return -1;
     114                 :            :                 }
     115                 :            :         }
     116                 :            : 
     117                 :            :         return 0;
     118                 :            : }
     119                 :            : 
     120                 :            : static int
     121                 :          0 : cdev_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
     122                 :            :                 void *arg)
     123                 :            : {
     124                 :            :         struct vfio_container *cfg = arg;
     125                 :            : 
     126                 :            :         /* skip external memory that isn't a heap */
     127   [ #  #  #  # ]:          0 :         if (msl->external && !msl->heap)
     128                 :            :                 return 0;
     129                 :            : 
     130                 :            :         /* skip any segments with invalid IOVA addresses */
     131         [ #  # ]:          0 :         if (ms->iova == RTE_BAD_IOVA)
     132                 :            :                 return 0;
     133                 :            : 
     134                 :          0 :         return vfio_cdev_dma_mem_map(cfg, ms->addr_64, ms->iova, ms->len, 1);
     135                 :            : }
     136                 :            : 
     137                 :            : static int
     138                 :          0 : vfio_cdev_dma_map(struct vfio_container *cfg)
     139                 :            : {
     140                 :          0 :         return rte_memseg_walk(cdev_map, cfg);
     141                 :            : }
     142                 :            : 
     143                 :            : int
     144                 :          0 : vfio_cdev_sync_ioas(struct vfio_container *cfg)
     145                 :            : {
     146                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     147                 :          0 :         struct rte_mp_reply mp_reply = {0};
     148                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     149                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     150                 :            : 
     151                 :          0 :         p->req = VFIO_SOCKET_REQ_IOAS_ID;
     152                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
     153                 :          0 :         mp_req.len_param = sizeof(*p);
     154                 :          0 :         mp_req.num_fds = 0;
     155                 :            : 
     156   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
     157                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     158                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     159   [ #  #  #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK && mp_rep->num_fds == 0) {
     160                 :          0 :                         cfg->cdev_cfg.ioas_id = p->ioas_id;
     161                 :          0 :                         free(mp_reply.msgs);
     162                 :          0 :                         return 0;
     163                 :            :                 }
     164                 :            :         }
     165                 :            : 
     166                 :          0 :         free(mp_reply.msgs);
     167                 :          0 :         EAL_LOG(ERR, "Cannot request ioas_id");
     168                 :          0 :         return -1;
     169                 :            : }
     170                 :            : 
     171                 :            : int
     172                 :          0 : vfio_cdev_setup_ioas(struct vfio_container *cfg)
     173                 :            : {
     174                 :            :         struct iommu_ioas_alloc ioas_alloc;
     175                 :            :         int ret;
     176                 :            : 
     177                 :            :         /* Allocate an IOAS */
     178                 :            :         memset(&ioas_alloc, 0, sizeof(ioas_alloc));
     179                 :          0 :         ioas_alloc.size = sizeof(struct iommu_ioas_alloc);
     180                 :            :         ioas_alloc.flags = 0;
     181                 :            : 
     182                 :          0 :         ret = ioctl(cfg->container_fd, IOMMU_IOAS_ALLOC, &ioas_alloc);
     183         [ #  # ]:          0 :         if (ret) {
     184                 :          0 :                 EAL_LOG(ERR, "Cannot allocate IOAS, error %i (%s)", errno, strerror(errno));
     185                 :          0 :                 return -1;
     186                 :            :         }
     187                 :          0 :         cfg->cdev_cfg.ioas_id = ioas_alloc.out_ioas_id;
     188                 :            : 
     189                 :          0 :         EAL_LOG(DEBUG, "Allocated IOAS with ID %u", cfg->cdev_cfg.ioas_id);
     190                 :          0 :         return 0;
     191                 :            : }
     192                 :            : 
     193                 :            : int
     194                 :          0 : vfio_cdev_get_iommufd(void)
     195                 :            : {
     196                 :            :         int iommufd;
     197                 :            : 
     198                 :            :         /* if not requesting via mp, open iommufd locally */
     199                 :            :         iommufd = open(DEV_VFIO_IOMMUFD_PATH, O_RDWR);
     200         [ #  # ]:          0 :         if (iommufd < 0) {
     201                 :          0 :                 EAL_LOG(ERR, "Cannot open %s: %s", DEV_VFIO_IOMMUFD_PATH, strerror(errno));
     202                 :          0 :                 return -1;
     203                 :            :         }
     204                 :            : 
     205                 :            :         return iommufd;
     206                 :            : }
     207                 :            : 
     208                 :            : int
     209                 :          0 : vfio_cdev_enable(struct vfio_container *cfg)
     210                 :            : {
     211                 :            :         int iommufd;
     212                 :            : 
     213                 :            :         /* Check if iommufd device exists */
     214         [ #  # ]:          0 :         if (access(DEV_VFIO_IOMMUFD_PATH, F_OK) != 0) {
     215                 :          0 :                 EAL_LOG(DEBUG, "IOMMUFD device does not exist, skipping VFIO cdev support...");
     216                 :          0 :                 return 1;
     217                 :            :         }
     218                 :            : 
     219                 :            :         /* open iommufd */
     220                 :          0 :         iommufd = vfio_cdev_get_iommufd();
     221         [ #  # ]:          0 :         if (iommufd < 0)
     222                 :            :                 return -1;
     223                 :            : 
     224                 :          0 :         cfg->container_fd = iommufd;
     225                 :          0 :         return 0;
     226                 :            : }
     227                 :            : 
     228                 :            : int
     229                 :          0 : vfio_cdev_get_device_num(const char *sysfs_base, const char *dev_addr, int *cdev_dev_num)
     230                 :            : {
     231                 :          0 :         char linkname[PATH_MAX] = {0};
     232                 :            :         size_t prefix_len;
     233                 :            :         char *dev_tok, *end;
     234                 :            :         int dev_num;
     235                 :            :         DIR *dir;
     236                 :            :         struct dirent *entry;
     237                 :            : 
     238                 :            :         /* check if vfio-dev directory exists for this device */
     239                 :            :         snprintf(linkname, sizeof(linkname), "%s/%s/vfio-dev", sysfs_base, dev_addr);
     240                 :            : 
     241                 :          0 :         dir = opendir(linkname);
     242         [ #  # ]:          0 :         if (dir == NULL) {
     243                 :            :                 /* device doesn't have vfio-dev, not bound to vfio-pci cdev */
     244                 :            :                 return 0;
     245                 :            :         }
     246                 :            : 
     247                 :            :         /* find the matching VFIO cdev entry */
     248         [ #  # ]:          0 :         while ((entry = readdir(dir)) != NULL) {
     249                 :          0 :                 prefix_len = cdev_match_prefix(entry->d_name);
     250         [ #  # ]:          0 :                 if (prefix_len == 0)
     251                 :          0 :                         continue;
     252                 :            : 
     253                 :            :                 /* parse device number after the cdev entry prefix */
     254                 :          0 :                 errno = 0;
     255                 :          0 :                 dev_tok = entry->d_name + prefix_len;
     256                 :          0 :                 end = dev_tok;
     257                 :          0 :                 dev_num = strtol(dev_tok, &end, 10);
     258   [ #  #  #  #  :          0 :                 if (end == dev_tok || *end != '\0' || errno != 0) {
                   #  # ]
     259                 :          0 :                         EAL_LOG(ERR, "%s error parsing VFIO cdev device number!", dev_addr);
     260                 :          0 :                         closedir(dir);
     261                 :          0 :                         return -1;
     262                 :            :                 }
     263                 :          0 :                 *cdev_dev_num = dev_num;
     264                 :          0 :                 closedir(dir);
     265                 :          0 :                 return 1;
     266                 :            :         }
     267                 :            : 
     268                 :          0 :         closedir(dir);
     269                 :            :         /* no vfio device found */
     270                 :          0 :         return 0;
     271                 :            : }
     272                 :            : 
     273                 :            : struct vfio_device *
     274                 :          0 : vfio_cdev_get_dev_by_num(struct vfio_container *cfg, int cdev_dev_num)
     275                 :            : {
     276                 :            :         struct vfio_device *dev;
     277                 :            :         /* find device handle */
     278   [ #  #  #  # ]:          0 :         VFIO_DEVICE_FOREACH_ACTIVE(cfg, dev) {
     279         [ #  # ]:          0 :                 if (dev->dev_num != cdev_dev_num)
     280                 :          0 :                         continue;
     281                 :          0 :                 return dev;
     282                 :            :         }
     283                 :            :         return NULL;
     284                 :            : }
     285                 :            : 
     286                 :            : static int
     287                 :          0 : cdev_open_device_fd(int cdev_dev_num)
     288                 :            : {
     289                 :          0 :         char devname[PATH_MAX] = {0};
     290                 :            :         int dev_fd = -1;
     291                 :            :         unsigned int i;
     292                 :            : 
     293         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(cdev_prefixes); i++) {
     294                 :            :                 snprintf(devname, sizeof(devname), DEV_VFIO_CDEV_FMT,
     295                 :          0 :                         cdev_prefixes[i], cdev_dev_num);
     296                 :            :                 dev_fd = open(devname, O_RDWR);
     297   [ #  #  #  # ]:          0 :                 if (dev_fd >= 0 || errno != ENOENT)
     298                 :            :                         break;
     299                 :            :         }
     300         [ #  # ]:          0 :         if (dev_fd < 0) {
     301                 :          0 :                 EAL_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
     302                 :          0 :                 return -1;
     303                 :            :         }
     304                 :            : 
     305                 :            :         return dev_fd;
     306                 :            : }
     307                 :            : 
     308                 :            : static int
     309                 :          0 : cdev_attach_device_to_iommufd(struct vfio_container *cfg, struct vfio_device *dev)
     310                 :            : {
     311                 :          0 :         struct vfio_device_bind_iommufd bind = {0};
     312                 :          0 :         struct vfio_device_attach_iommufd_pt attach = {0};
     313                 :            :         rte_uuid_t vf_token;
     314                 :            : 
     315                 :          0 :         rte_eal_vfio_get_vf_token(vf_token);
     316                 :            : 
     317                 :            :         /* try with token first */
     318         [ #  # ]:          0 :         if (!rte_uuid_is_null(vf_token)) {
     319                 :          0 :                 bind.flags = VFIO_DEVICE_BIND_FLAG_TOKEN;
     320                 :          0 :                 bind.token_uuid_ptr = (uintptr_t)&vf_token;
     321                 :          0 :                 bind.argsz = sizeof(bind);
     322                 :          0 :                 bind.iommufd = cfg->container_fd;
     323                 :            : 
     324                 :            :                 /* this may fail because the kernel is too old */
     325         [ #  # ]:          0 :                 if (ioctl(dev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind) < 0) {
     326                 :          0 :                         EAL_LOG(DEBUG, "Failed to bind device %d with VF token", dev->dev_num);
     327                 :          0 :                         EAL_LOG(NOTICE, "Unable to use VF tokens with current kernel version.");
     328                 :          0 :                         EAL_LOG(NOTICE, "Please use kernel >=6.17 or use group mode.");
     329                 :            :                         /* erase the bind structure */
     330                 :          0 :                         bind = (struct vfio_device_bind_iommufd){0};
     331                 :            :                 } else {
     332                 :          0 :                         goto attach;
     333                 :            :                 }
     334                 :            :         }
     335                 :          0 :         bind.flags = 0;
     336                 :          0 :         bind.argsz = sizeof(bind);
     337                 :          0 :         bind.iommufd = cfg->container_fd;
     338                 :            : 
     339         [ #  # ]:          0 :         if (ioctl(dev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind) < 0) {
     340                 :          0 :                 EAL_LOG(ERR, "Cannot bind device to IOMMUFD, error %i (%s)", errno,
     341                 :            :                         strerror(errno));
     342                 :          0 :                 return -1;
     343                 :            :         }
     344                 :            : 
     345                 :          0 : attach:
     346                 :            :         /* attach device to IOAS */
     347                 :          0 :         attach.argsz = sizeof(attach);
     348                 :          0 :         attach.flags = 0;
     349                 :          0 :         attach.pt_id = cfg->cdev_cfg.ioas_id;
     350                 :            : 
     351         [ #  # ]:          0 :         if (ioctl(dev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach) < 0) {
     352                 :          0 :                 EAL_LOG(ERR, "Cannot attach device to IOAS, error %i (%s)", errno,
     353                 :            :                         strerror(errno));
     354                 :          0 :                 return -1;
     355                 :            :         }
     356                 :            : 
     357                 :            :         return 0;
     358                 :            : }
     359                 :            : 
     360                 :            : static int
     361                 :          0 : vfio_cdev_request_dev_fd(struct vfio_device *dev)
     362                 :            : {
     363                 :            :         struct rte_mp_msg mp_req, *mp_rep;
     364                 :          0 :         struct rte_mp_reply mp_reply = {0};
     365                 :          0 :         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
     366                 :            :         struct vfio_mp_param *p = (struct vfio_mp_param *)mp_req.param;
     367                 :            :         int device_fd = -1;
     368                 :            : 
     369                 :            :         /* secondary process requests device fd from primary */
     370                 :          0 :         p->req = VFIO_SOCKET_REQ_CDEV;
     371                 :          0 :         p->cdev_dev_num = dev->dev_num;
     372                 :          0 :         rte_strscpy(mp_req.name, EAL_VFIO_MP, sizeof(mp_req.name));
     373                 :          0 :         mp_req.len_param = sizeof(*p);
     374                 :          0 :         mp_req.num_fds = 0;
     375                 :            : 
     376   [ #  #  #  # ]:          0 :         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 && mp_reply.nb_received == 1) {
     377                 :          0 :                 mp_rep = &mp_reply.msgs[0];
     378                 :            :                 p = (struct vfio_mp_param *)mp_rep->param;
     379   [ #  #  #  # ]:          0 :                 if (p->result == VFIO_SOCKET_OK && mp_rep->num_fds == 1)
     380                 :          0 :                         device_fd = mp_rep->fds[0];
     381                 :            :         }
     382                 :            : 
     383                 :          0 :         free(mp_reply.msgs);
     384                 :            : 
     385         [ #  # ]:          0 :         if (device_fd < 0) {
     386                 :          0 :                 EAL_LOG(ERR, "Cannot request device fd for vfio%d", dev->dev_num);
     387                 :          0 :                 return -1;
     388                 :            :         }
     389                 :          0 :         dev->fd = device_fd;
     390                 :            : 
     391                 :          0 :         return 0;
     392                 :            : }
     393                 :            : 
     394                 :            : int
     395                 :          0 : vfio_cdev_setup_device(struct vfio_container *cfg, struct vfio_device *dev)
     396                 :            : {
     397                 :            :         int device_fd;
     398                 :            : 
     399                 :            :         /* get device fd - primary or custom container opens it, secondary requests from primary */
     400   [ #  #  #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY || !vfio_container_is_default(cfg)) {
     401                 :          0 :                 device_fd = cdev_open_device_fd(dev->dev_num);
     402         [ #  # ]:          0 :                 if (device_fd < 0)
     403                 :            :                         return -1;
     404                 :          0 :                 dev->fd = device_fd;
     405                 :            : 
     406                 :            :                 /* attach device to iommufd */
     407         [ #  # ]:          0 :                 if (cdev_attach_device_to_iommufd(cfg, dev) < 0) {
     408                 :          0 :                         close(device_fd);
     409                 :          0 :                         dev->fd = -1;
     410                 :          0 :                         return -1;
     411                 :            :                 }
     412         [ #  # ]:          0 :         } else if (vfio_cdev_request_dev_fd(dev) < 0) {
     413                 :          0 :                 return -1;
     414                 :            :         }
     415                 :            :         return 0;
     416                 :            : }

Generated by: LCOV version 1.14