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