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