2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2017 Intel Corporation.
|
2017-01-12 18:25:17 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
2017-01-12 18:25:17 +00:00
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "env_internal.h"
|
2017-01-12 18:25:17 +00:00
|
|
|
|
2018-03-02 21:33:19 +00:00
|
|
|
#include "spdk/version.h"
|
2018-12-13 15:54:01 +00:00
|
|
|
#include "spdk/env_dpdk.h"
|
2020-04-20 19:47:03 +00:00
|
|
|
#include "spdk/log.h"
|
2018-03-02 21:33:19 +00:00
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
#include <rte_config.h>
|
|
|
|
#include <rte_eal.h>
|
2019-10-15 14:36:04 +00:00
|
|
|
#include <rte_errno.h>
|
2020-03-02 05:24:43 +00:00
|
|
|
#include <rte_vfio.h>
|
2017-01-12 18:25:17 +00:00
|
|
|
|
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_NAME "spdk"
|
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_SHM_ID -1
|
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE -1
|
2020-11-30 19:44:19 +00:00
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_MAIN_CORE -1
|
2017-01-12 18:25:17 +00:00
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL -1
|
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_CORE_MASK "0x1"
|
2020-06-04 15:57:19 +00:00
|
|
|
#define SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR 0x200000000000
|
2017-01-12 18:25:17 +00:00
|
|
|
|
2020-11-25 21:05:09 +00:00
|
|
|
#if RTE_VERSION < RTE_VERSION_NUM(20, 11, 0, 0)
|
|
|
|
#define DPDK_ALLOW_PARAM "--pci-whitelist"
|
|
|
|
#define DPDK_BLOCK_PARAM "--pci-blacklist"
|
2020-11-30 19:44:19 +00:00
|
|
|
#define DPDK_MAIN_CORE_PARAM "--master-lcore"
|
2020-11-25 21:35:26 +00:00
|
|
|
#else
|
|
|
|
#define DPDK_ALLOW_PARAM "--allow"
|
|
|
|
#define DPDK_BLOCK_PARAM "--block"
|
2020-11-30 19:44:19 +00:00
|
|
|
#define DPDK_MAIN_CORE_PARAM "--main-lcore"
|
2020-11-25 21:05:09 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-27 17:55:03 +00:00
|
|
|
static char **g_eal_cmdline;
|
|
|
|
static int g_eal_cmdline_argcount;
|
2019-02-27 17:52:47 +00:00
|
|
|
static bool g_external_init = true;
|
2017-10-16 18:16:38 +00:00
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
static char *
|
|
|
|
_sprintf_alloc(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_list args_copy;
|
|
|
|
char *buf;
|
|
|
|
size_t bufsize;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
|
|
/* Try with a small buffer first. */
|
|
|
|
bufsize = 32;
|
|
|
|
|
|
|
|
/* Limit maximum buffer size to something reasonable so we don't loop forever. */
|
|
|
|
while (bufsize <= 1024 * 1024) {
|
|
|
|
buf = malloc(bufsize);
|
|
|
|
if (buf == NULL) {
|
|
|
|
va_end(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_copy(args_copy, args);
|
|
|
|
rc = vsnprintf(buf, bufsize, format, args_copy);
|
|
|
|
va_end(args_copy);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If vsnprintf() returned a count within our current buffer size, we are done.
|
|
|
|
* The count does not include the \0 terminator, so rc == bufsize is not OK.
|
|
|
|
*/
|
|
|
|
if (rc >= 0 && (size_t)rc < bufsize) {
|
|
|
|
va_end(args);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vsnprintf() should return the required space, but some libc versions do not
|
|
|
|
* implement this correctly, so just double the buffer size and try again.
|
|
|
|
*
|
|
|
|
* We don't need the data in buf, so rather than realloc(), use free() and malloc()
|
|
|
|
* again to avoid a copy.
|
|
|
|
*/
|
|
|
|
free(buf);
|
|
|
|
bufsize *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_env_opts_init(struct spdk_env_opts *opts)
|
|
|
|
{
|
|
|
|
if (!opts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(opts, 0, sizeof(*opts));
|
|
|
|
|
|
|
|
opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
|
|
|
|
opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
|
|
|
|
opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
|
2017-06-16 08:41:13 +00:00
|
|
|
opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
|
2020-11-30 19:44:19 +00:00
|
|
|
opts->main_core = SPDK_ENV_DPDK_DEFAULT_MAIN_CORE;
|
2017-06-16 08:41:13 +00:00
|
|
|
opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
|
2020-06-04 15:57:19 +00:00
|
|
|
opts->base_virtaddr = SPDK_ENV_DPDK_DEFAULT_BASE_VIRTADDR;
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-05-10 22:57:07 +00:00
|
|
|
free_args(char **args, int argcount)
|
2017-01-12 18:25:17 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-05-08 06:03:22 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
for (i = 0; i < argcount; i++) {
|
|
|
|
free(args[i]);
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:57:01 +00:00
|
|
|
if (argcount) {
|
|
|
|
free(args);
|
|
|
|
}
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2020-05-10 22:57:07 +00:00
|
|
|
push_arg(char *args[], int *argcount, char *arg)
|
2017-01-12 18:25:17 +00:00
|
|
|
{
|
|
|
|
char **tmp;
|
|
|
|
|
|
|
|
if (arg == NULL) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("%s: NULL arg supplied\n", __func__);
|
2020-05-10 22:57:07 +00:00
|
|
|
free_args(args, *argcount);
|
2017-01-12 18:25:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = realloc(args, sizeof(char *) * (*argcount + 1));
|
|
|
|
if (tmp == NULL) {
|
2019-02-18 14:40:26 +00:00
|
|
|
free(arg);
|
2020-05-10 22:57:07 +00:00
|
|
|
free_args(args, *argcount);
|
2017-01-12 18:25:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp[*argcount] = arg;
|
|
|
|
(*argcount)++;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:28:14 +00:00
|
|
|
#if defined(__linux__) && defined(__x86_64__)
|
|
|
|
|
|
|
|
/* TODO: Can likely get this value from rlimits in the future */
|
|
|
|
#define SPDK_IOMMU_VA_REQUIRED_WIDTH 48
|
|
|
|
#define VTD_CAP_MGAW_SHIFT 16
|
|
|
|
#define VTD_CAP_MGAW_MASK (0x3F << VTD_CAP_MGAW_SHIFT)
|
2022-09-07 15:42:23 +00:00
|
|
|
#define RD_AMD_CAP_VASIZE_SHIFT 15
|
|
|
|
#define RD_AMD_CAP_VASIZE_MASK (0x7F << RD_AMD_CAP_VASIZE_SHIFT)
|
|
|
|
|
2019-11-19 18:28:14 +00:00
|
|
|
static int
|
2020-05-10 22:57:07 +00:00
|
|
|
get_iommu_width(void)
|
2019-11-19 18:28:14 +00:00
|
|
|
{
|
2022-10-13 19:26:06 +00:00
|
|
|
int width = 0;
|
|
|
|
glob_t glob_results = {};
|
|
|
|
|
|
|
|
/* Break * and / into separate strings to appease check_format.sh comment style check. */
|
|
|
|
glob("/sys/devices/virtual/iommu/dmar*" "/intel-iommu/cap", 0, NULL, &glob_results);
|
|
|
|
glob("/sys/class/iommu/ivhd*" "/amd-iommu/cap", GLOB_APPEND, NULL, &glob_results);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < glob_results.gl_pathc; i++) {
|
|
|
|
const char *filename = glob_results.gl_pathv[0];
|
|
|
|
FILE *file = fopen(filename, "r");
|
|
|
|
uint64_t cap_reg = 0;
|
|
|
|
|
|
|
|
if (file != NULL && fscanf(file, "%" PRIx64, &cap_reg) == 1) {
|
|
|
|
if (strstr(filename, "intel-iommu") != NULL) {
|
|
|
|
/* We have an Intel IOMMU */
|
|
|
|
int mgaw = ((cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
|
|
|
|
|
|
|
|
if (width == 0 || (mgaw > 0 && mgaw < width)) {
|
|
|
|
width = mgaw;
|
|
|
|
}
|
|
|
|
} else if (strstr(filename, "amd-iommu") != NULL) {
|
|
|
|
/* We have an AMD IOMMU */
|
|
|
|
int mgaw = ((cap_reg & RD_AMD_CAP_VASIZE_MASK) >> RD_AMD_CAP_VASIZE_SHIFT) + 1;
|
|
|
|
|
|
|
|
if (width == 0 || (mgaw > 0 && mgaw < width)) {
|
|
|
|
width = mgaw;
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 18:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:26:06 +00:00
|
|
|
globfree(&glob_results);
|
2019-11-19 18:28:14 +00:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
static int
|
2020-05-10 22:57:07 +00:00
|
|
|
build_eal_cmdline(const struct spdk_env_opts *opts)
|
2017-01-12 18:25:17 +00:00
|
|
|
{
|
|
|
|
int argcount = 0;
|
|
|
|
char **args;
|
|
|
|
|
|
|
|
args = NULL;
|
|
|
|
|
|
|
|
/* set the program name */
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:14:22 +00:00
|
|
|
/* disable shared configuration files when in single process mode. This allows for cleaner shutdown */
|
|
|
|
if (opts->shm_id < 0) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf"));
|
2018-08-08 17:14:22 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:59:38 +00:00
|
|
|
/*
|
|
|
|
* Set the coremask:
|
|
|
|
*
|
|
|
|
* - if it starts with '-', we presume it's literal EAL arguments such
|
|
|
|
* as --lcores.
|
|
|
|
*
|
|
|
|
* - if it starts with '[', we presume it's a core list to use with the
|
|
|
|
* -l option.
|
|
|
|
*
|
|
|
|
* - otherwise, it's a CPU mask of the form "0xff.." as expected by the
|
|
|
|
* -c option.
|
2017-12-21 16:48:31 +00:00
|
|
|
*/
|
2021-05-18 11:59:38 +00:00
|
|
|
if (opts->core_mask[0] == '-') {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->core_mask));
|
|
|
|
} else if (opts->core_mask[0] == '[') {
|
2017-12-21 16:48:31 +00:00
|
|
|
char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
|
2020-01-31 14:39:11 +00:00
|
|
|
|
|
|
|
if (l_arg != NULL) {
|
|
|
|
int len = strlen(l_arg);
|
|
|
|
|
|
|
|
if (l_arg[len - 1] == ']') {
|
|
|
|
l_arg[len - 1] = '\0';
|
|
|
|
}
|
2017-12-21 16:48:31 +00:00
|
|
|
}
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, l_arg);
|
2017-12-21 16:48:31 +00:00
|
|
|
} else {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
|
2017-12-21 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the memory channel number */
|
2017-06-16 08:41:13 +00:00
|
|
|
if (opts->mem_channel > 0) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the memory size */
|
2018-08-14 08:45:49 +00:00
|
|
|
if (opts->mem_size >= 0) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 19:44:19 +00:00
|
|
|
/* set the main core */
|
|
|
|
if (opts->main_core > 0) {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s=%d",
|
|
|
|
DPDK_MAIN_CORE_PARAM, opts->main_core));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 07:31:55 +00:00
|
|
|
/* set no pci if enabled */
|
|
|
|
if (opts->no_pci) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
|
2017-06-14 07:31:55 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 08:19:09 +00:00
|
|
|
/* create just one hugetlbfs file */
|
|
|
|
if (opts->hugepage_single_segments) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
|
2018-02-23 08:19:09 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:58:59 +00:00
|
|
|
/* unlink hugepages after initialization */
|
2022-01-06 08:54:37 +00:00
|
|
|
/* Note: Automatically unlink hugepage when shm_id < 0, since it means we're not using
|
|
|
|
* multi-process so we don't need the hugepage links anymore. But we need to make sure
|
|
|
|
* we don't specify --huge-unlink implicitly if --single-file-segments was specified since
|
|
|
|
* DPDK doesn't support that.
|
|
|
|
*/
|
|
|
|
if (opts->unlink_hugepage ||
|
|
|
|
(opts->shm_id < 0 && !opts->hugepage_single_segments)) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
|
2018-07-12 07:58:59 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 06:10:11 +00:00
|
|
|
/* use a specific hugetlbfs mount */
|
|
|
|
if (opts->hugedir) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir));
|
2018-11-06 06:10:11 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:39:37 +00:00
|
|
|
if (opts->num_pci_addr) {
|
|
|
|
size_t i;
|
|
|
|
char bdf[32];
|
|
|
|
struct spdk_pci_addr *pci_addr =
|
2020-11-25 21:35:26 +00:00
|
|
|
opts->pci_blocked ? opts->pci_blocked : opts->pci_allowed;
|
2018-03-30 17:39:37 +00:00
|
|
|
|
|
|
|
for (i = 0; i < opts->num_pci_addr; i++) {
|
|
|
|
spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]);
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s=%s",
|
2020-11-25 21:35:26 +00:00
|
|
|
(opts->pci_blocked ? DPDK_BLOCK_PARAM : DPDK_ALLOW_PARAM),
|
2020-05-10 22:57:07 +00:00
|
|
|
bdf));
|
2018-03-30 17:39:37 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:29:20 +00:00
|
|
|
/* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages.
|
|
|
|
* This can be overridden by specifying the same option in opts->env_context
|
|
|
|
*/
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, strdup("--log-level=lib.eal:6"));
|
2019-02-18 11:29:20 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:12:08 +00:00
|
|
|
/* Lower default CRYPTO loglevel to RTE_LOG_ERR to avoid a ton of init msgs.
|
|
|
|
* This can be overridden by specifying the same option in opts->env_context
|
|
|
|
*/
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, strdup("--log-level=lib.cryptodev:5"));
|
2019-05-10 22:12:08 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:20:20 +00:00
|
|
|
/* `user1` log type is used by rte_vhost, which prints an INFO log for each received
|
|
|
|
* vhost user message. We don't want that. The same log type is also used by a couple
|
|
|
|
* of other DPDK libs, but none of which we make use right now. If necessary, this can
|
|
|
|
* be overridden via opts->env_context.
|
|
|
|
*/
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, strdup("--log-level=user1:6"));
|
2017-07-28 16:20:20 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:29:20 +00:00
|
|
|
if (opts->env_context) {
|
2021-08-06 22:00:25 +00:00
|
|
|
char *ptr = strdup(opts->env_context);
|
|
|
|
char *tok = strtok(ptr, " \t");
|
|
|
|
|
|
|
|
/* DPDK expects each argument as a separate string in the argv
|
|
|
|
* array, so we need to tokenize here in case the caller
|
|
|
|
* passed multiple arguments in the env_context string.
|
|
|
|
*/
|
|
|
|
while (tok != NULL) {
|
|
|
|
args = push_arg(args, &argcount, strdup(tok));
|
|
|
|
tok = strtok(NULL, " \t");
|
2019-02-18 11:29:20 +00:00
|
|
|
}
|
2021-08-06 22:00:25 +00:00
|
|
|
|
|
|
|
free(ptr);
|
2019-02-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
#ifdef __linux__
|
2019-11-19 17:09:00 +00:00
|
|
|
|
2020-06-16 17:26:30 +00:00
|
|
|
if (opts->iova_mode) {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=%s", opts->iova_mode));
|
2020-03-02 05:24:43 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-16 17:26:30 +00:00
|
|
|
} else {
|
|
|
|
/* When using vfio with enable_unsafe_noiommu_mode=Y, we need iova-mode=pa,
|
|
|
|
* but DPDK guesses it should be iova-mode=va. Add a check and force
|
|
|
|
* iova-mode=pa here. */
|
|
|
|
if (rte_vfio_noiommu_is_enabled()) {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
|
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 05:24:43 +00:00
|
|
|
|
2019-11-19 18:28:14 +00:00
|
|
|
#if defined(__x86_64__)
|
2020-06-16 17:26:30 +00:00
|
|
|
/* DPDK by default guesses that it should be using iova-mode=va so that it can
|
|
|
|
* support running as an unprivileged user. However, some systems (especially
|
|
|
|
* virtual machines) don't have an IOMMU capable of handling the full virtual
|
|
|
|
* address space and DPDK doesn't currently catch that. Add a check in SPDK
|
|
|
|
* and force iova-mode=pa here. */
|
|
|
|
if (get_iommu_width() < SPDK_IOMMU_VA_REQUIRED_WIDTH) {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
|
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(__PPC64__)
|
|
|
|
/* On Linux + PowerPC, DPDK doesn't support VA mode at all. Unfortunately, it doesn't correctly
|
|
|
|
* auto-detect at the moment, so we'll just force it here. */
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
|
2019-11-19 18:28:14 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-11-19 17:09:00 +00:00
|
|
|
#endif
|
2020-06-16 17:26:30 +00:00
|
|
|
}
|
2019-11-19 17:09:00 +00:00
|
|
|
|
2019-11-19 18:28:14 +00:00
|
|
|
|
2018-10-30 08:11:32 +00:00
|
|
|
/* Set the base virtual address - it must be an address that is not in the
|
|
|
|
* ASAN shadow region, otherwise ASAN-enabled builds will ignore the
|
|
|
|
* mmap hint.
|
|
|
|
*
|
|
|
|
* Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
|
|
|
|
*/
|
2020-06-04 15:57:19 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x%" PRIx64, opts->base_virtaddr));
|
2018-10-30 08:11:32 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:53:04 +00:00
|
|
|
/* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood.
|
|
|
|
* This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two
|
|
|
|
* physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split
|
|
|
|
* the memory for a buffer over two allocations meaning the buffer will be split over a memory region.
|
|
|
|
*/
|
2019-12-06 23:45:16 +00:00
|
|
|
if (!opts->env_context || strstr(opts->env_context, "--legacy-mem") == NULL) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations"));
|
2019-03-22 06:27:07 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-02-25 23:53:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:25:17 +00:00
|
|
|
if (opts->shm_id < 0) {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
|
|
|
|
getpid()));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
|
|
|
|
opts->shm_id));
|
2017-01-12 18:25:17 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-08 18:08:03 +00:00
|
|
|
/* set the process type */
|
2020-05-10 22:57:07 +00:00
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
|
2017-03-08 18:08:03 +00:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
2022-09-08 09:29:15 +00:00
|
|
|
|
|
|
|
/* --vfio-vf-token used for VF initialized by vfio_pci driver. */
|
|
|
|
if (opts->vf_token) {
|
|
|
|
args = push_arg(args, &argcount, _sprintf_alloc("--vfio-vf-token=%s",
|
|
|
|
opts->vf_token));
|
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 18:25:17 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-27 17:55:03 +00:00
|
|
|
g_eal_cmdline = args;
|
|
|
|
g_eal_cmdline_argcount = argcount;
|
2017-01-12 18:25:17 +00:00
|
|
|
return argcount;
|
|
|
|
}
|
|
|
|
|
2018-12-13 15:54:01 +00:00
|
|
|
int
|
2019-12-06 23:26:49 +00:00
|
|
|
spdk_env_dpdk_post_init(bool legacy_mem)
|
2018-12-13 15:54:01 +00:00
|
|
|
{
|
2019-10-15 14:36:04 +00:00
|
|
|
int rc;
|
|
|
|
|
2022-09-22 00:48:23 +00:00
|
|
|
rc = pci_env_init();
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("pci_env_init() failed\n");
|
|
|
|
return rc;
|
|
|
|
}
|
2018-12-13 15:54:01 +00:00
|
|
|
|
2020-04-07 07:32:40 +00:00
|
|
|
rc = mem_map_init(legacy_mem);
|
2019-10-15 14:36:04 +00:00
|
|
|
if (rc < 0) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("Failed to allocate mem_map\n");
|
2019-10-15 14:36:04 +00:00
|
|
|
return rc;
|
2018-12-13 15:54:01 +00:00
|
|
|
}
|
2019-10-15 14:36:04 +00:00
|
|
|
|
2020-04-07 07:32:40 +00:00
|
|
|
rc = vtophys_init();
|
2019-10-15 14:36:04 +00:00
|
|
|
if (rc < 0) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("Failed to initialize vtophys\n");
|
2019-10-15 14:36:04 +00:00
|
|
|
return rc;
|
2018-12-13 15:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:14:19 +00:00
|
|
|
void
|
|
|
|
spdk_env_dpdk_post_fini(void)
|
|
|
|
{
|
2020-05-26 09:54:02 +00:00
|
|
|
pci_env_fini();
|
2019-03-25 20:17:23 +00:00
|
|
|
|
2020-05-10 22:57:07 +00:00
|
|
|
free_args(g_eal_cmdline, g_eal_cmdline_argcount);
|
2020-05-08 06:03:22 +00:00
|
|
|
g_eal_cmdline = NULL;
|
|
|
|
g_eal_cmdline_argcount = 0;
|
2019-03-11 12:14:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 15:54:01 +00:00
|
|
|
int
|
|
|
|
spdk_env_init(const struct spdk_env_opts *opts)
|
2017-01-12 18:25:17 +00:00
|
|
|
{
|
|
|
|
char **dpdk_args = NULL;
|
2017-10-16 18:16:38 +00:00
|
|
|
int i, rc;
|
2017-06-12 19:55:37 +00:00
|
|
|
int orig_optind;
|
2019-12-06 23:26:49 +00:00
|
|
|
bool legacy_mem;
|
2017-01-12 18:25:17 +00:00
|
|
|
|
2020-05-08 06:03:22 +00:00
|
|
|
/* If SPDK env has been initialized before, then only pci env requires
|
|
|
|
* reinitialization.
|
|
|
|
*/
|
|
|
|
if (g_external_init == false) {
|
|
|
|
if (opts != NULL) {
|
|
|
|
fprintf(stderr, "Invalid arguments to reinitialize SPDK env\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Starting %s / %s reinitialization...\n", SPDK_VERSION_STRING, rte_version());
|
|
|
|
pci_env_reinit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts == NULL) {
|
|
|
|
fprintf(stderr, "NULL arguments to initialize DPDK\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:57:07 +00:00
|
|
|
rc = build_eal_cmdline(opts);
|
2017-10-16 18:16:38 +00:00
|
|
|
if (rc < 0) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("Invalid arguments to initialize DPDK\n");
|
2019-10-15 14:36:04 +00:00
|
|
|
return -EINVAL;
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_PRINTF("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
|
|
|
|
SPDK_PRINTF("[ DPDK EAL parameters: ");
|
2019-02-27 17:55:03 +00:00
|
|
|
for (i = 0; i < g_eal_cmdline_argcount; i++) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_PRINTF("%s ", g_eal_cmdline[i]);
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_PRINTF("]\n");
|
2017-01-12 18:25:17 +00:00
|
|
|
|
|
|
|
/* DPDK rearranges the array we pass to it, so make a copy
|
|
|
|
* before passing so we can still free the individual strings
|
|
|
|
* correctly.
|
|
|
|
*/
|
2019-02-27 17:55:03 +00:00
|
|
|
dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *));
|
2017-03-23 08:54:35 +00:00
|
|
|
if (dpdk_args == NULL) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("Failed to allocate dpdk_args\n");
|
2019-10-15 14:36:04 +00:00
|
|
|
return -ENOMEM;
|
2017-03-23 08:54:35 +00:00
|
|
|
}
|
2019-02-27 17:55:03 +00:00
|
|
|
memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount);
|
2017-01-12 18:25:17 +00:00
|
|
|
|
|
|
|
fflush(stdout);
|
2017-06-12 19:55:37 +00:00
|
|
|
orig_optind = optind;
|
|
|
|
optind = 1;
|
2019-02-27 17:55:03 +00:00
|
|
|
rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args);
|
2017-06-12 19:55:37 +00:00
|
|
|
optind = orig_optind;
|
2017-01-12 18:25:17 +00:00
|
|
|
|
|
|
|
free(dpdk_args);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2019-10-15 14:46:53 +00:00
|
|
|
if (rte_errno == EALREADY) {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("DPDK already initialized\n");
|
2019-10-15 14:46:53 +00:00
|
|
|
} else {
|
2020-04-20 19:47:03 +00:00
|
|
|
SPDK_ERRLOG("Failed to initialize DPDK\n");
|
2019-10-15 14:46:53 +00:00
|
|
|
}
|
2019-10-15 14:36:04 +00:00
|
|
|
return -rte_errno;
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
2017-02-21 22:42:50 +00:00
|
|
|
|
2019-12-06 23:26:49 +00:00
|
|
|
legacy_mem = false;
|
|
|
|
if (opts->env_context && strstr(opts->env_context, "--legacy-mem") != NULL) {
|
|
|
|
legacy_mem = true;
|
|
|
|
}
|
|
|
|
|
2020-07-08 13:57:43 +00:00
|
|
|
rc = spdk_env_dpdk_post_init(legacy_mem);
|
|
|
|
if (rc == 0) {
|
|
|
|
g_external_init = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2017-01-12 18:25:17 +00:00
|
|
|
}
|
2019-02-27 17:52:47 +00:00
|
|
|
|
2022-01-13 07:04:47 +00:00
|
|
|
/* We use priority 101 which is the highest priority level available
|
|
|
|
* to applications (the toolchains reserve 1 to 100 for internal usage).
|
|
|
|
* This ensures this destructor runs last, after any other destructors
|
|
|
|
* that might still need the environment up and running.
|
|
|
|
*/
|
|
|
|
__attribute__((destructor(101))) static void
|
2022-01-05 23:02:25 +00:00
|
|
|
dpdk_cleanup(void)
|
|
|
|
{
|
|
|
|
/* Only call rte_eal_cleanup if the SPDK env library called rte_eal_init. */
|
|
|
|
if (!g_external_init) {
|
|
|
|
rte_eal_cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:14:19 +00:00
|
|
|
void
|
|
|
|
spdk_env_fini(void)
|
|
|
|
{
|
|
|
|
spdk_env_dpdk_post_fini();
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:52:47 +00:00
|
|
|
bool
|
|
|
|
spdk_env_dpdk_external_init(void)
|
|
|
|
{
|
|
|
|
return g_external_init;
|
|
|
|
}
|