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