LCOV - code coverage report
Current view: top level - spdk/lib/env_dpdk - init.c (source / functions) Hit Total Coverage
Test: Combined Lines: 213 315 67.6 %
Date: 2024-07-11 09:45:31 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 153 284 53.9 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "env_internal.h"
       9                 :            : 
      10                 :            : #include "spdk/version.h"
      11                 :            : #include "spdk/env_dpdk.h"
      12                 :            : #include "spdk/log.h"
      13                 :            : #include "spdk/config.h"
      14                 :            : 
      15                 :            : #include <openssl/ssl.h>
      16                 :            : #include <openssl/err.h>
      17                 :            : 
      18                 :            : #include <rte_config.h>
      19                 :            : #include <rte_eal.h>
      20                 :            : #include <rte_errno.h>
      21                 :            : #include <rte_vfio.h>
      22                 :            : 
      23                 :            : #define SPDK_ENV_DPDK_DEFAULT_NAME              "spdk"
      24                 :            : #define SPDK_ENV_DPDK_DEFAULT_SHM_ID            -1
      25                 :            : #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE          -1
      26                 :            : #define SPDK_ENV_DPDK_DEFAULT_MAIN_CORE         -1
      27                 :            : #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL       -1
      28                 :            : #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK         "0x1"
      29                 :            : #define SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR     0x200000000000
      30                 :            : 
      31                 :            : #define DPDK_ALLOW_PARAM        "--allow"
      32                 :            : #define DPDK_BLOCK_PARAM        "--block"
      33                 :            : #define DPDK_MAIN_CORE_PARAM    "--main-lcore"
      34                 :            : 
      35                 :            : static char **g_eal_cmdline;
      36                 :            : static int g_eal_cmdline_argcount;
      37                 :            : static bool g_external_init = true;
      38                 :            : 
      39                 :            : static char *
      40                 :      74771 : _sprintf_alloc(const char *format, ...)
      41                 :            : {
      42                 :      31611 :         va_list args;
      43                 :      31611 :         va_list args_copy;
      44                 :            :         char *buf;
      45                 :            :         size_t bufsize;
      46                 :            :         int rc;
      47                 :            : 
      48                 :      74771 :         va_start(args, format);
      49                 :            : 
      50                 :            :         /* Try with a small buffer first. */
      51                 :      74771 :         bufsize = 32;
      52                 :            : 
      53                 :            :         /* Limit maximum buffer size to something reasonable so we don't loop forever. */
      54         [ +  - ]:     163706 :         while (bufsize <= 1024 * 1024) {
      55                 :     163706 :                 buf = malloc(bufsize);
      56         [ -  + ]:     163706 :                 if (buf == NULL) {
      57                 :          0 :                         va_end(args);
      58                 :          0 :                         return NULL;
      59                 :            :                 }
      60                 :            : 
      61                 :     163706 :                 va_copy(args_copy, args);
      62         [ -  + ]:     163706 :                 rc = vsnprintf(buf, bufsize, format, args_copy);
      63                 :     163706 :                 va_end(args_copy);
      64                 :            : 
      65                 :            :                 /*
      66                 :            :                  * If vsnprintf() returned a count within our current buffer size, we are done.
      67                 :            :                  * The count does not include the \0 terminator, so rc == bufsize is not OK.
      68                 :            :                  */
      69   [ +  -  +  + ]:     163706 :                 if (rc >= 0 && (size_t)rc < bufsize) {
      70                 :      74771 :                         va_end(args);
      71                 :      74771 :                         return buf;
      72                 :            :                 }
      73                 :            : 
      74                 :            :                 /*
      75                 :            :                  * vsnprintf() should return the required space, but some libc versions do not
      76                 :            :                  * implement this correctly, so just double the buffer size and try again.
      77                 :            :                  *
      78                 :            :                  * We don't need the data in buf, so rather than realloc(), use free() and malloc()
      79                 :            :                  * again to avoid a copy.
      80                 :            :                  */
      81                 :      88935 :                 free(buf);
      82                 :      88935 :                 bufsize *= 2;
      83                 :            :         }
      84                 :            : 
      85                 :          0 :         va_end(args);
      86                 :          0 :         return NULL;
      87                 :            : }
      88                 :            : 
      89                 :            : void
      90                 :       3509 : spdk_env_opts_init(struct spdk_env_opts *opts)
      91                 :            : {
      92         [ -  + ]:       3509 :         if (!opts) {
      93                 :          0 :                 return;
      94                 :            :         }
      95                 :            : 
      96         [ -  + ]:       3509 :         memset(opts, 0, sizeof(*opts));
      97                 :            : 
      98                 :       3509 :         opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
      99                 :       3509 :         opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
     100                 :       3509 :         opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
     101                 :       3509 :         opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
     102                 :       3509 :         opts->main_core = SPDK_ENV_DPDK_DEFAULT_MAIN_CORE;
     103                 :       3509 :         opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
     104                 :       3509 :         opts->base_virtaddr = SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR;
     105                 :            : }
     106                 :            : 
     107                 :            : static void
     108                 :       3447 : free_args(char **args, int argcount)
     109                 :            : {
     110                 :            :         int i;
     111                 :            : 
     112         [ +  + ]:       3447 :         if (args == NULL) {
     113                 :         60 :                 return;
     114                 :            :         }
     115                 :            : 
     116         [ +  + ]:      42893 :         for (i = 0; i < argcount; i++) {
     117                 :      39506 :                 free(args[i]);
     118                 :            :         }
     119                 :            : 
     120         [ +  - ]:       3387 :         if (argcount) {
     121                 :       3387 :                 free(args);
     122                 :            :         }
     123                 :            : }
     124                 :            : 
     125                 :            : static char **
     126                 :      40907 : push_arg(char *args[], int *argcount, char *arg)
     127                 :            : {
     128                 :            :         char **tmp;
     129                 :            : 
     130         [ -  + ]:      40907 :         if (arg == NULL) {
     131                 :          0 :                 SPDK_ERRLOG("%s: NULL arg supplied\n", __func__);
     132                 :          0 :                 free_args(args, *argcount);
     133                 :          0 :                 return NULL;
     134                 :            :         }
     135                 :            : 
     136                 :      40907 :         tmp = realloc(args, sizeof(char *) * (*argcount + 1));
     137         [ -  + ]:      40907 :         if (tmp == NULL) {
     138                 :          0 :                 free(arg);
     139                 :          0 :                 free_args(args, *argcount);
     140                 :          0 :                 return NULL;
     141                 :            :         }
     142                 :            : 
     143                 :      40907 :         tmp[*argcount] = arg;
     144                 :      40907 :         (*argcount)++;
     145                 :            : 
     146                 :      40907 :         return tmp;
     147                 :            : }
     148                 :            : 
     149                 :            : #if defined(__linux__) && defined(__x86_64__)
     150                 :            : 
     151                 :            : /* TODO: Can likely get this value from rlimits in the future */
     152                 :            : #define SPDK_IOMMU_VA_REQUIRED_WIDTH 48
     153                 :            : #define VTD_CAP_MGAW_SHIFT 16
     154                 :            : #define VTD_CAP_MGAW_MASK (0x3F << VTD_CAP_MGAW_SHIFT)
     155                 :            : #define RD_AMD_CAP_VASIZE_SHIFT 15
     156                 :            : #define RD_AMD_CAP_VASIZE_MASK (0x7F << RD_AMD_CAP_VASIZE_SHIFT)
     157                 :            : 
     158                 :            : static int
     159                 :       3505 : get_iommu_width(void)
     160                 :            : {
     161                 :       3505 :         int width = 0;
     162                 :       3505 :         glob_t glob_results = {};
     163                 :            : 
     164                 :            :         /* Break * and / into separate strings to appease check_format.sh comment style check. */
     165                 :       3505 :         glob("/sys/devices/virtual/iommu/dmar*" "/intel-iommu/cap", 0, NULL, &glob_results);
     166                 :       3505 :         glob("/sys/class/iommu/ivhd*" "/amd-iommu/cap", GLOB_APPEND, NULL, &glob_results);
     167                 :            : 
     168         [ +  + ]:      11398 :         for (size_t i = 0; i < glob_results.gl_pathc; i++) {
     169                 :       7893 :                 const char *filename = glob_results.gl_pathv[0];
     170                 :       7893 :                 FILE *file = fopen(filename, "r");
     171                 :       7893 :                 uint64_t cap_reg = 0;
     172                 :            : 
     173         [ -  + ]:       7893 :                 if (file == NULL) {
     174                 :          0 :                         continue;
     175                 :            :                 }
     176                 :            : 
     177         [ +  - ]:       7893 :                 if (fscanf(file, "%" PRIx64, &cap_reg) == 1) {
     178   [ -  +  +  - ]:       7893 :                         if (strstr(filename, "intel-iommu") != NULL) {
     179                 :            :                                 /* We have an Intel IOMMU */
     180                 :       7893 :                                 int mgaw = ((cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
     181                 :            : 
     182   [ +  +  +  -  :       7893 :                                 if (width == 0 || (mgaw > 0 && mgaw < width)) {
                   -  + ]
     183                 :       1148 :                                         width = mgaw;
     184                 :            :                                 }
     185   [ #  #  #  # ]:          0 :                         } else if (strstr(filename, "amd-iommu") != NULL) {
     186                 :            :                                 /* We have an AMD IOMMU */
     187                 :          0 :                                 int mgaw = ((cap_reg & RD_AMD_CAP_VASIZE_MASK) >> RD_AMD_CAP_VASIZE_SHIFT) + 1;
     188                 :            : 
     189   [ #  #  #  #  :          0 :                                 if (width == 0 || (mgaw > 0 && mgaw < width)) {
                   #  # ]
     190                 :          0 :                                         width = mgaw;
     191                 :            :                                 }
     192                 :            :                         }
     193                 :            :                 }
     194                 :            : 
     195                 :       7893 :                 fclose(file);
     196                 :            :         }
     197                 :            : 
     198                 :       3505 :         globfree(&glob_results);
     199                 :       3505 :         return width;
     200                 :            : }
     201                 :            : 
     202                 :            : #endif
     203                 :            : 
     204                 :            : static int
     205                 :       3511 : build_eal_cmdline(const struct spdk_env_opts *opts)
     206                 :            : {
     207                 :       3511 :         int argcount = 0;
     208                 :            :         char **args;
     209                 :            :         bool no_huge;
     210                 :            : 
     211                 :       3511 :         args = NULL;
     212   [ +  +  +  +  :       3511 :         no_huge = opts->no_huge || (opts->env_context && strstr(opts->env_context, "--no-huge") != NULL);
          +  +  -  +  -  
                      + ]
     213                 :            : 
     214                 :            :         /* set the program name */
     215                 :       3511 :         args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
     216         [ -  + ]:       3511 :         if (args == NULL) {
     217                 :          0 :                 return -1;
     218                 :            :         }
     219                 :            : 
     220                 :            :         /* disable shared configuration files when in single process mode. This allows for cleaner shutdown */
     221         [ +  + ]:       3511 :         if (opts->shm_id < 0) {
     222                 :       2943 :                 args = push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf"));
     223         [ -  + ]:       2943 :                 if (args == NULL) {
     224                 :          0 :                         return -1;
     225                 :            :                 }
     226                 :            :         }
     227                 :            : 
     228                 :            :         /* Either lcore_map or core_mask must be set. If both, or none specified, fail */
     229         [ -  + ]:       3511 :         if ((opts->core_mask == NULL) == (opts->lcore_map == NULL)) {
     230   [ #  #  #  # ]:          0 :                 if (opts->core_mask && opts->lcore_map) {
     231         [ #  # ]:          0 :                         fprintf(stderr,
     232                 :            :                                 "Both, lcore map and core mask are provided, while only one can be set\n");
     233                 :            :                 } else {
     234         [ #  # ]:          0 :                         fprintf(stderr, "Core mask or lcore map must be specified\n");
     235                 :            :                 }
     236                 :          0 :                 free_args(args, argcount);
     237                 :          0 :                 return -1;
     238                 :            :         }
     239                 :            : 
     240         [ -  + ]:       3511 :         if (opts->lcore_map) {
     241                 :            :                 /* If lcore list is set, generate --lcores parameter */
     242                 :          0 :                 args = push_arg(args, &argcount, _sprintf_alloc("--lcores=%s", opts->lcore_map));
     243         [ -  + ]:       3511 :         } else if (opts->core_mask[0] == '-') {
     244                 :            :                 /*
     245                 :            :                  * Set the coremask:
     246                 :            :                  *
     247                 :            :                  * - if it starts with '-', we presume it's literal EAL arguments such
     248                 :            :                  *   as --lcores.
     249                 :            :                  *
     250                 :            :                  * - if it starts with '[', we presume it's a core list to use with the
     251                 :            :                  *   -l option.
     252                 :            :                  *
     253                 :            :                  * - otherwise, it's a CPU mask of the form "0xff.." as expected by the
     254                 :            :                  *   -c option.
     255                 :            :                  */
     256                 :          0 :                 args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->core_mask));
     257         [ +  + ]:       3511 :         } else if (opts->core_mask[0] == '[') {
     258                 :         20 :                 char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
     259                 :            : 
     260         [ +  - ]:         20 :                 if (l_arg != NULL) {
     261         [ -  + ]:         20 :                         int len = strlen(l_arg);
     262                 :            : 
     263         [ +  - ]:         20 :                         if (l_arg[len - 1] == ']') {
     264                 :         20 :                                 l_arg[len - 1] = '\0';
     265                 :            :                         }
     266                 :            :                 }
     267                 :         20 :                 args = push_arg(args, &argcount, l_arg);
     268                 :            :         } else {
     269                 :       3491 :                 args = push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
     270                 :            :         }
     271                 :            : 
     272         [ -  + ]:       3511 :         if (args == NULL) {
     273                 :          0 :                 return -1;
     274                 :            :         }
     275                 :            : 
     276                 :            :         /* set the memory channel number */
     277         [ +  + ]:       3511 :         if (opts->mem_channel > 0) {
     278                 :         62 :                 args = push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
     279         [ -  + ]:         62 :                 if (args == NULL) {
     280                 :          0 :                         return -1;
     281                 :            :                 }
     282                 :            :         }
     283                 :            : 
     284                 :            :         /* set the memory size */
     285         [ +  + ]:       3511 :         if (opts->mem_size >= 0) {
     286                 :        245 :                 args = push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
     287         [ -  + ]:        245 :                 if (args == NULL) {
     288                 :          0 :                         return -1;
     289                 :            :                 }
     290                 :            :         }
     291                 :            : 
     292                 :            :         /* set no huge pages */
     293   [ -  +  +  + ]:       3511 :         if (opts->no_huge) {
     294                 :          6 :                 mem_disable_huge_pages();
     295                 :            :         }
     296                 :            : 
     297                 :            :         /* set the main core */
     298         [ +  + ]:       3511 :         if (opts->main_core > 0) {
     299                 :         27 :                 args = push_arg(args, &argcount, _sprintf_alloc("%s=%d",
     300                 :         25 :                                 DPDK_MAIN_CORE_PARAM, opts->main_core));
     301         [ -  + ]:         27 :                 if (args == NULL) {
     302                 :          0 :                         return -1;
     303                 :            :                 }
     304                 :            :         }
     305                 :            : 
     306                 :            :         /* set no pci  if enabled */
     307   [ -  +  +  + ]:       3511 :         if (opts->no_pci) {
     308                 :        139 :                 args = push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
     309         [ -  + ]:        139 :                 if (args == NULL) {
     310                 :          0 :                         return -1;
     311                 :            :                 }
     312                 :            :         }
     313                 :            : 
     314         [ +  + ]:       3511 :         if (no_huge) {
     315   [ -  +  +  -  :          6 :                 if (opts->hugepage_single_segments || opts->unlink_hugepage || opts->hugedir) {
          -  +  +  -  -  
                      + ]
     316         [ #  # ]:          0 :                         fprintf(stderr, "--no-huge invalid with other hugepage options\n");
     317                 :          0 :                         free_args(args, argcount);
     318                 :          0 :                         return -1;
     319                 :            :                 }
     320                 :            : 
     321         [ -  + ]:          6 :                 if (opts->mem_size < 0) {
     322         [ #  # ]:          0 :                         fprintf(stderr,
     323                 :            :                                 "Disabling hugepages requires specifying how much memory "
     324                 :            :                                 "will be allocated using -s parameter\n");
     325                 :          0 :                         free_args(args, argcount);
     326                 :          0 :                         return -1;
     327                 :            :                 }
     328                 :            : 
     329                 :            :                 /* iova-mode=pa is incompatible with no_huge */
     330         [ -  + ]:          6 :                 if (opts->iova_mode &&
     331   [ #  #  #  # ]:          0 :                     (strcmp(opts->iova_mode, "pa") == 0)) {
     332         [ #  # ]:          0 :                         fprintf(stderr, "iova-mode=pa is incompatible with specified "
     333                 :            :                                 "no-huge parameter\n");
     334                 :          0 :                         free_args(args, argcount);
     335                 :          0 :                         return -1;
     336                 :            :                 }
     337                 :            : 
     338                 :          6 :                 args = push_arg(args, &argcount, _sprintf_alloc("--no-huge"));
     339                 :          6 :                 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=va"));
     340                 :            : 
     341                 :            :         } else {
     342                 :            :                 /* create just one hugetlbfs file */
     343   [ +  +  +  + ]:       3505 :                 if (opts->hugepage_single_segments) {
     344                 :         43 :                         args = push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
     345         [ -  + ]:         43 :                         if (args == NULL) {
     346                 :          0 :                                 return -1;
     347                 :            :                         }
     348                 :            :                 }
     349                 :            : 
     350                 :            :                 /* unlink hugepages after initialization */
     351                 :            :                 /* Note: Automatically unlink hugepage when shm_id < 0, since it means we're not using
     352                 :            :                  * multi-process so we don't need the hugepage links anymore.  But we need to make sure
     353                 :            :                  * we don't specify --huge-unlink implicitly if --single-file-segments was specified since
     354                 :            :                  * DPDK doesn't support that.
     355                 :            :                  */
     356   [ +  +  +  + ]:       3505 :                 if (opts->unlink_hugepage ||
     357   [ +  +  +  +  :       3504 :                     (opts->shm_id < 0 && !opts->hugepage_single_segments)) {
                   +  + ]
     358                 :       2897 :                         args = push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
     359         [ -  + ]:       2897 :                         if (args == NULL) {
     360                 :          0 :                                 return -1;
     361                 :            :                         }
     362                 :            :                 }
     363                 :            : 
     364                 :            :                 /* use a specific hugetlbfs mount */
     365         [ -  + ]:       3505 :                 if (opts->hugedir) {
     366                 :          0 :                         args = push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir));
     367         [ #  # ]:          0 :                         if (args == NULL) {
     368                 :          0 :                                 return -1;
     369                 :            :                         }
     370                 :            :                 }
     371                 :            :         }
     372                 :            : 
     373         [ -  + ]:       3511 :         if (opts->num_pci_addr) {
     374                 :            :                 size_t i;
     375                 :          0 :                 char bdf[32];
     376                 :          0 :                 struct spdk_pci_addr *pci_addr =
     377         [ #  # ]:          0 :                                 opts->pci_blocked ? opts->pci_blocked : opts->pci_allowed;
     378                 :            : 
     379         [ #  # ]:          0 :                 for (i = 0; i < opts->num_pci_addr; i++) {
     380                 :          0 :                         spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]);
     381                 :          0 :                         args = push_arg(args, &argcount, _sprintf_alloc("%s=%s",
     382         [ #  # ]:          0 :                                         (opts->pci_blocked ? DPDK_BLOCK_PARAM : DPDK_ALLOW_PARAM),
     383                 :            :                                         bdf));
     384         [ #  # ]:          0 :                         if (args == NULL) {
     385                 :          0 :                                 return -1;
     386                 :            :                         }
     387                 :            :                 }
     388                 :            :         }
     389                 :            : 
     390                 :            :         /* Disable DPDK telemetry information by default, can be modified with env_context.
     391                 :            :          * Prevents creation of dpdk_telemetry socket and additional pthread for it.
     392                 :            :          */
     393                 :       3511 :         args = push_arg(args, &argcount, _sprintf_alloc("--no-telemetry"));
     394         [ -  + ]:       3511 :         if (args == NULL) {
     395                 :          0 :                 return -1;
     396                 :            :         }
     397                 :            : 
     398                 :            :         /* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages.
     399                 :            :          * This can be overridden by specifying the same option in opts->env_context
     400                 :            :          */
     401                 :       3511 :         args = push_arg(args, &argcount, strdup("--log-level=lib.eal:6"));
     402         [ -  + ]:       3511 :         if (args == NULL) {
     403                 :          0 :                 return -1;
     404                 :            :         }
     405                 :            : 
     406                 :            :         /* Lower default CRYPTO loglevel to RTE_LOG_ERR to avoid a ton of init msgs.
     407                 :            :          * This can be overridden by specifying the same option in opts->env_context
     408                 :            :          */
     409                 :       3511 :         args = push_arg(args, &argcount, strdup("--log-level=lib.cryptodev:5"));
     410         [ -  + ]:       3511 :         if (args == NULL) {
     411                 :          0 :                 return -1;
     412                 :            :         }
     413                 :            : 
     414                 :            :         /* `user1` log type is used by rte_vhost, which prints an INFO log for each received
     415                 :            :          * vhost user message. We don't want that. The same log type is also used by a couple
     416                 :            :          * of other DPDK libs, but none of which we make use right now. If necessary, this can
     417                 :            :          * be overridden via opts->env_context.
     418                 :            :          */
     419                 :       3511 :         args = push_arg(args, &argcount, strdup("--log-level=user1:6"));
     420         [ -  + ]:       3511 :         if (args == NULL) {
     421                 :          0 :                 return -1;
     422                 :            :         }
     423                 :            : 
     424         [ +  + ]:       3511 :         if (opts->env_context) {
     425         [ -  + ]:         21 :                 char *ptr = strdup(opts->env_context);
     426                 :         21 :                 char *tok = strtok(ptr, " \t");
     427                 :            : 
     428                 :            :                 /* DPDK expects each argument as a separate string in the argv
     429                 :            :                  * array, so we need to tokenize here in case the caller
     430                 :            :                  * passed multiple arguments in the env_context string.
     431                 :            :                  */
     432         [ +  + ]:         42 :                 while (tok != NULL) {
     433         [ -  + ]:         21 :                         args = push_arg(args, &argcount, strdup(tok));
     434                 :         21 :                         tok = strtok(NULL, " \t");
     435                 :            :                 }
     436                 :            : 
     437                 :         21 :                 free(ptr);
     438                 :            :         }
     439                 :            : 
     440                 :            : #ifdef __linux__
     441                 :            : 
     442         [ -  + ]:       3511 :         if (opts->iova_mode) {
     443                 :            :                 /* iova-mode=pa is incompatible with no_huge */
     444                 :          0 :                 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=%s", opts->iova_mode));
     445         [ #  # ]:          0 :                 if (args == NULL) {
     446                 :          0 :                         return -1;
     447                 :            :                 }
     448                 :            :         } else {
     449                 :            :                 /* When using vfio with enable_unsafe_noiommu_mode=Y, we need iova-mode=pa,
     450                 :            :                  * but DPDK guesses it should be iova-mode=va. Add a check and force
     451                 :            :                  * iova-mode=pa here. */
     452   [ +  +  -  + ]:       3511 :                 if (!no_huge && rte_vfio_noiommu_is_enabled()) {
     453                 :          0 :                         args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
     454         [ #  # ]:          0 :                         if (args == NULL) {
     455                 :          0 :                                 return -1;
     456                 :            :                         }
     457                 :            :                 }
     458                 :            : 
     459                 :            : #if defined(__x86_64__)
     460                 :            :                 /* DPDK by default guesses that it should be using iova-mode=va so that it can
     461                 :            :                  * support running as an unprivileged user. However, some systems (especially
     462                 :            :                  * virtual machines) don't have an IOMMU capable of handling the full virtual
     463                 :            :                  * address space and DPDK doesn't currently catch that. Add a check in SPDK
     464                 :            :                  * and force iova-mode=pa here. */
     465   [ +  +  +  + ]:       3511 :                 if (!no_huge && get_iommu_width() < SPDK_IOMMU_VA_REQUIRED_WIDTH) {
     466                 :       2357 :                         args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
     467         [ -  + ]:       2357 :                         if (args == NULL) {
     468                 :          0 :                                 return -1;
     469                 :            :                         }
     470                 :            :                 }
     471                 :            : #elif defined(__PPC64__)
     472                 :            :                 /* On Linux + PowerPC, DPDK doesn't support VA mode at all. Unfortunately, it doesn't correctly
     473                 :            :                  * auto-detect at the moment, so we'll just force it here. */
     474                 :            :                 args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
     475                 :            :                 if (args == NULL) {
     476                 :            :                         return -1;
     477                 :            :                 }
     478                 :            : #endif
     479                 :            :         }
     480                 :            : 
     481                 :            : 
     482                 :            :         /* Set the base virtual address - it must be an address that is not in the
     483                 :            :          * ASAN shadow region, otherwise ASAN-enabled builds will ignore the
     484                 :            :          * mmap hint.
     485                 :            :          *
     486                 :            :          * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
     487                 :            :          */
     488                 :       3511 :         args = push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x%" PRIx64, opts->base_virtaddr));
     489         [ -  + ]:       3511 :         if (args == NULL) {
     490                 :          0 :                 return -1;
     491                 :            :         }
     492                 :            : 
     493                 :            :         /* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood.
     494                 :            :          * This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two
     495                 :            :          * physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split
     496                 :            :          * the memory for a buffer over two allocations meaning the buffer will be split over a memory region.
     497                 :            :          */
     498                 :            : 
     499                 :            :         /* --no-huge is incompatible with --match-allocations
     500                 :            :          * Ref:  https://doc.dpdk.org/guides/prog_guide/env_abstraction_layer.html#hugepage-allocation-matching
     501                 :            :          */
     502         [ +  + ]:       3511 :         if (!no_huge &&
     503   [ +  +  +  +  :       3505 :             (!opts->env_context || strstr(opts->env_context, "--legacy-mem") == NULL)) {
                   +  - ]
     504                 :       3505 :                 args = push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations"));
     505         [ -  + ]:       3505 :                 if (args == NULL) {
     506                 :          0 :                         return -1;
     507                 :            :                 }
     508                 :            :         }
     509                 :            : 
     510         [ +  + ]:       3511 :         if (opts->shm_id < 0) {
     511                 :       2943 :                 args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
     512                 :            :                                 getpid()));
     513         [ -  + ]:       2943 :                 if (args == NULL) {
     514                 :          0 :                         return -1;
     515                 :            :                 }
     516                 :            :         } else {
     517                 :        568 :                 args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
     518                 :        495 :                                 opts->shm_id));
     519         [ -  + ]:        568 :                 if (args == NULL) {
     520                 :          0 :                         return -1;
     521                 :            :                 }
     522                 :            : 
     523                 :            :                 /* set the process type */
     524                 :        568 :                 args = push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
     525         [ -  + ]:        568 :                 if (args == NULL) {
     526                 :          0 :                         return -1;
     527                 :            :                 }
     528                 :            :         }
     529                 :            : 
     530                 :            :         /* --vfio-vf-token used for VF initialized by vfio_pci driver. */
     531         [ -  + ]:       3511 :         if (opts->vf_token) {
     532                 :          0 :                 args = push_arg(args, &argcount, _sprintf_alloc("--vfio-vf-token=%s",
     533                 :          0 :                                 opts->vf_token));
     534         [ #  # ]:          0 :                 if (args == NULL) {
     535                 :          0 :                         return -1;
     536                 :            :                 }
     537                 :            :         }
     538                 :            : #endif
     539                 :            : 
     540                 :       3511 :         g_eal_cmdline = args;
     541                 :       3511 :         g_eal_cmdline_argcount = argcount;
     542                 :       3511 :         return argcount;
     543                 :            : }
     544                 :            : 
     545                 :            : int
     546                 :       3532 : spdk_env_dpdk_post_init(bool legacy_mem)
     547                 :            : {
     548                 :            :         int rc;
     549                 :            : 
     550                 :       3532 :         rc = pci_env_init();
     551         [ -  + ]:       3532 :         if (rc < 0) {
     552                 :          0 :                 SPDK_ERRLOG("pci_env_init() failed\n");
     553                 :          0 :                 return rc;
     554                 :            :         }
     555                 :            : 
     556                 :       3532 :         rc = mem_map_init(legacy_mem);
     557         [ -  + ]:       3532 :         if (rc < 0) {
     558                 :          0 :                 SPDK_ERRLOG("Failed to allocate mem_map\n");
     559                 :          0 :                 return rc;
     560                 :            :         }
     561                 :            : 
     562                 :       3532 :         rc = vtophys_init();
     563         [ -  + ]:       3532 :         if (rc < 0) {
     564                 :          0 :                 SPDK_ERRLOG("Failed to initialize vtophys\n");
     565                 :          0 :                 return rc;
     566                 :            :         }
     567                 :            : 
     568                 :       3532 :         return 0;
     569                 :            : }
     570                 :            : 
     571                 :            : void
     572                 :       3447 : spdk_env_dpdk_post_fini(void)
     573                 :            : {
     574                 :       3447 :         pci_env_fini();
     575                 :            : 
     576                 :       3447 :         free_args(g_eal_cmdline, g_eal_cmdline_argcount);
     577                 :       3447 :         g_eal_cmdline = NULL;
     578                 :       3447 :         g_eal_cmdline_argcount = 0;
     579                 :       3447 : }
     580                 :            : 
     581                 :            : int
     582                 :       3571 : spdk_env_init(const struct spdk_env_opts *opts)
     583                 :            : {
     584                 :       3571 :         char **dpdk_args = NULL;
     585                 :       3571 :         char *args_print = NULL, *args_tmp = NULL;
     586                 :            :         OPENSSL_INIT_SETTINGS *settings;
     587                 :            :         int i, rc;
     588                 :            :         int orig_optind;
     589                 :            :         bool legacy_mem;
     590                 :            : 
     591                 :            :         /* If SPDK env has been initialized before, then only pci env requires
     592                 :            :          * reinitialization.
     593                 :            :          */
     594   [ +  +  +  + ]:       3571 :         if (g_external_init == false) {
     595         [ -  + ]:         60 :                 if (opts != NULL) {
     596         [ #  # ]:          0 :                         fprintf(stderr, "Invalid arguments to reinitialize SPDK env\n");
     597                 :          0 :                         return -EINVAL;
     598                 :            :                 }
     599                 :            : 
     600                 :         60 :                 printf("Starting %s / %s reinitialization...\n", SPDK_VERSION_STRING, rte_version());
     601                 :         60 :                 pci_env_reinit();
     602                 :            : 
     603                 :         60 :                 return 0;
     604                 :            :         }
     605                 :            : 
     606         [ -  + ]:       3511 :         if (opts == NULL) {
     607         [ #  # ]:          0 :                 fprintf(stderr, "NULL arguments to initialize DPDK\n");
     608                 :          0 :                 return -EINVAL;
     609                 :            :         }
     610                 :            : 
     611                 :       3511 :         settings = OPENSSL_INIT_new();
     612         [ -  + ]:       3511 :         if (!settings) {
     613         [ #  # ]:          0 :                 fprintf(stderr, "Failed to create openssl settings object\n");
     614                 :          0 :                 ERR_print_errors_fp(stderr);
     615                 :          0 :                 return -ENOMEM;
     616                 :            :         }
     617                 :            : 
     618                 :       3511 :         OPENSSL_INIT_set_config_file_flags(settings, 0);
     619                 :       3511 :         rc = OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, settings);
     620         [ -  + ]:       3511 :         if (rc != 1) {
     621         [ #  # ]:          0 :                 fprintf(stderr, "Failed to initialize OpenSSL\n");
     622                 :          0 :                 ERR_print_errors_fp(stderr);
     623                 :          0 :                 return -EINVAL;
     624                 :            :         }
     625                 :       3511 :         OPENSSL_INIT_free(settings);
     626                 :            : 
     627                 :       3511 :         rc = build_eal_cmdline(opts);
     628         [ -  + ]:       3511 :         if (rc < 0) {
     629                 :          0 :                 SPDK_ERRLOG("Invalid arguments to initialize DPDK\n");
     630                 :          0 :                 return -EINVAL;
     631                 :            :         }
     632                 :            : 
     633                 :       3511 :         SPDK_PRINTF("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
     634                 :            : 
     635                 :       3511 :         args_print = _sprintf_alloc("[ DPDK EAL parameters: ");
     636         [ -  + ]:       3511 :         if (args_print == NULL) {
     637                 :          0 :                 return -ENOMEM;
     638                 :            :         }
     639         [ +  + ]:      44418 :         for (i = 0; i < g_eal_cmdline_argcount; i++) {
     640                 :      40907 :                 args_tmp = args_print;
     641                 :      40907 :                 args_print = _sprintf_alloc("%s%s ", args_tmp, g_eal_cmdline[i]);
     642         [ -  + ]:      40907 :                 if (args_print == NULL) {
     643                 :          0 :                         free(args_tmp);
     644                 :          0 :                         return -ENOMEM;
     645                 :            :                 }
     646                 :      40907 :                 free(args_tmp);
     647                 :            :         }
     648                 :       3511 :         SPDK_PRINTF("%s]\n", args_print);
     649                 :       3511 :         free(args_print);
     650                 :            : 
     651                 :            :         /* DPDK rearranges the array we pass to it, so make a copy
     652                 :            :          * before passing so we can still free the individual strings
     653                 :            :          * correctly.
     654                 :            :          */
     655                 :       3511 :         dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *));
     656         [ -  + ]:       3511 :         if (dpdk_args == NULL) {
     657                 :          0 :                 SPDK_ERRLOG("Failed to allocate dpdk_args\n");
     658                 :          0 :                 return -ENOMEM;
     659                 :            :         }
     660   [ -  +  -  + ]:       3511 :         memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount);
     661                 :            : 
     662                 :       3511 :         fflush(stdout);
     663                 :       3511 :         orig_optind = optind;
     664                 :       3511 :         optind = 1;
     665                 :       3511 :         rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args);
     666                 :       3511 :         optind = orig_optind;
     667                 :            : 
     668                 :       3511 :         free(dpdk_args);
     669                 :            : 
     670         [ -  + ]:       3511 :         if (rc < 0) {
     671         [ #  # ]:          0 :                 if (rte_errno == EALREADY) {
     672                 :          0 :                         SPDK_ERRLOG("DPDK already initialized\n");
     673                 :            :                 } else {
     674                 :          0 :                         SPDK_ERRLOG("Failed to initialize DPDK\n");
     675                 :            :                 }
     676                 :          0 :                 return -rte_errno;
     677                 :            :         }
     678                 :            : 
     679                 :       3511 :         legacy_mem = false;
     680   [ +  +  -  +  :       3511 :         if (opts->env_context && strstr(opts->env_context, "--legacy-mem") != NULL) {
                   -  + ]
     681                 :          0 :                 legacy_mem = true;
     682                 :            :         }
     683                 :            : 
     684                 :       3511 :         rc = spdk_env_dpdk_post_init(legacy_mem);
     685         [ +  - ]:       3511 :         if (rc == 0) {
     686                 :       3511 :                 g_external_init = false;
     687                 :            :         }
     688                 :            : 
     689                 :       3511 :         return rc;
     690                 :            : }
     691                 :            : 
     692                 :            : /* We use priority 101 which is the highest priority level available
     693                 :            :  * to applications (the toolchains reserve 1 to 100 for internal usage).
     694                 :            :  * This ensures this destructor runs last, after any other destructors
     695                 :            :  * that might still need the environment up and running.
     696                 :            :  */
     697                 :            : __attribute__((destructor(101))) static void
     698                 :       3713 : dpdk_cleanup(void)
     699                 :            : {
     700                 :            :         /* Only call rte_eal_cleanup if the SPDK env library called rte_eal_init. */
     701   [ +  +  +  + ]:       3713 :         if (!g_external_init) {
     702                 :       3511 :                 rte_eal_cleanup();
     703                 :            :         }
     704                 :       3713 : }
     705                 :            : 
     706                 :            : void
     707                 :       3447 : spdk_env_fini(void)
     708                 :            : {
     709                 :       3447 :         spdk_env_dpdk_post_fini();
     710                 :       3447 : }
     711                 :            : 
     712                 :            : bool
     713                 :      41881 : spdk_env_dpdk_external_init(void)
     714                 :            : {
     715         [ -  + ]:      41881 :         return g_external_init;
     716                 :            : }

Generated by: LCOV version 1.14