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