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 : 58222 : _sprintf_alloc(const char *format, ...)
41 : : {
42 : 23571 : va_list args;
43 : 23571 : va_list args_copy;
44 : : char *buf;
45 : : size_t bufsize;
46 : : int rc;
47 : :
48 : 58222 : va_start(args, format);
49 : :
50 : : /* Try with a small buffer first. */
51 : 58222 : bufsize = 32;
52 : :
53 : : /* Limit maximum buffer size to something reasonable so we don't loop forever. */
54 [ + - ]: 133621 : while (bufsize <= 1024 * 1024) {
55 : 133621 : buf = malloc(bufsize);
56 [ - + ]: 133621 : if (buf == NULL) {
57 : 0 : va_end(args);
58 : 0 : return NULL;
59 : : }
60 : :
61 : 133621 : va_copy(args_copy, args);
62 [ - + ]: 133621 : rc = vsnprintf(buf, bufsize, format, args_copy);
63 : 133621 : 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 [ + - + + ]: 133621 : if (rc >= 0 && (size_t)rc < bufsize) {
70 : 58222 : va_end(args);
71 : 58222 : 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 : 75399 : free(buf);
82 : 75399 : bufsize *= 2;
83 : : }
84 : :
85 : 0 : va_end(args);
86 : 0 : return NULL;
87 : : }
88 : :
89 : : void
90 : 5204 : spdk_env_opts_init(struct spdk_env_opts *opts)
91 : : {
92 [ - + ]: 5204 : if (!opts) {
93 : 0 : return;
94 : : }
95 : :
96 [ - + ]: 5204 : memset(opts, 0, sizeof(*opts));
97 : :
98 : 5204 : opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
99 : 5204 : opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
100 : 5204 : opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
101 : 5204 : opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
102 : 5204 : opts->main_core = SPDK_ENV_DPDK_DEFAULT_MAIN_CORE;
103 : 5204 : opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
104 : 5204 : 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 : 2565 : free_args(char **args, int argcount)
116 : : {
117 : : int i;
118 : :
119 [ + + ]: 2565 : if (args == NULL) {
120 : 84 : return;
121 : : }
122 : :
123 [ + + ]: 34015 : for (i = 0; i < argcount; i++) {
124 : 31534 : free(args[i]);
125 : : }
126 : :
127 [ + - ]: 2481 : if (argcount) {
128 : 2481 : free(args);
129 : : }
130 : : }
131 : :
132 : : static char **
133 : 33029 : push_arg(char *args[], int *argcount, char *arg)
134 : : {
135 : : char **tmp;
136 : :
137 [ - + ]: 33029 : 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 : 33029 : tmp = realloc(args, sizeof(char *) * (*argcount + 1));
144 [ - + ]: 33029 : if (tmp == NULL) {
145 : 0 : free(arg);
146 : 0 : free_args(args, *argcount);
147 : 0 : return NULL;
148 : : }
149 : :
150 : 33029 : tmp[*argcount] = arg;
151 : 33029 : (*argcount)++;
152 : :
153 : 33029 : 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 : 2597 : get_iommu_width(void)
167 : : {
168 : 2597 : int width = 0;
169 : 2597 : glob_t glob_results = {};
170 : :
171 : : /* Break * and / into separate strings to appease check_format.sh comment style check. */
172 : 2597 : glob("/sys/devices/virtual/iommu/dmar*" "/intel-iommu/cap", 0, NULL, &glob_results);
173 : 2597 : glob("/sys/class/iommu/ivhd*" "/amd-iommu/cap", GLOB_APPEND, NULL, &glob_results);
174 : :
175 [ + + ]: 6341 : for (size_t i = 0; i < glob_results.gl_pathc; i++) {
176 : 3744 : const char *filename = glob_results.gl_pathv[0];
177 : 3744 : FILE *file = fopen(filename, "r");
178 : 3744 : uint64_t cap_reg = 0;
179 : :
180 [ - + ]: 3744 : if (file == NULL) {
181 : 0 : continue;
182 : : }
183 : :
184 [ + - # # ]: 3744 : if (fscanf(file, "%" PRIx64, &cap_reg) == 1) {
185 [ - + + - ]: 3744 : if (strstr(filename, "intel-iommu") != NULL) {
186 : : /* We have an Intel IOMMU */
187 : 3744 : int mgaw = ((cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
188 : :
189 [ + + + - : 3744 : if (width == 0 || (mgaw > 0 && mgaw < width)) {
- + ]
190 : 821 : 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 [ - + ]: 3744 : fclose(file);
203 : : }
204 : :
205 : 2597 : globfree(&glob_results);
206 : 2597 : return width;
207 : : }
208 : :
209 : : #endif
210 : :
211 : : static int
212 : 2603 : build_eal_cmdline(const struct spdk_env_opts *opts)
213 : : {
214 : 2603 : int argcount = 0;
215 : : char **args;
216 : : bool no_huge;
217 : :
218 : 2603 : args = NULL;
219 [ + + + + : 2603 : no_huge = opts->no_huge || (opts->env_context && strstr(opts->env_context, "--no-huge") != NULL);
+ + - + -
+ ]
220 : :
221 : : /* set the program name */
222 : 2603 : args = push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
223 [ - + ]: 2603 : 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 [ + + ]: 2603 : if (opts->shm_id < 0) {
229 : 2110 : args = push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf"));
230 [ - + ]: 2110 : 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 [ - + ]: 2603 : 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 [ - + ]: 2603 : 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 [ - + ]: 2603 : } 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 [ + + ]: 2603 : } else if (opts->core_mask[0] == '[') {
265 : 23 : char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
266 : :
267 [ + - ]: 23 : if (l_arg != NULL) {
268 [ - + ]: 23 : int len = strlen(l_arg);
269 : :
270 [ + - ]: 23 : if (l_arg[len - 1] == ']') {
271 : 23 : l_arg[len - 1] = '\0';
272 : : }
273 : : }
274 : 23 : args = push_arg(args, &argcount, l_arg);
275 : : } else {
276 : 2580 : args = push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
277 : : }
278 : :
279 [ - + ]: 2603 : if (args == NULL) {
280 : 0 : return -1;
281 : : }
282 : :
283 : : /* set the memory channel number */
284 [ + + ]: 2603 : if (opts->mem_channel > 0) {
285 : 68 : args = push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
286 [ - + ]: 68 : if (args == NULL) {
287 : 0 : return -1;
288 : : }
289 : : }
290 : :
291 : : /* set the memory size */
292 [ + + ]: 2603 : if (opts->mem_size >= 0) {
293 : 242 : args = push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
294 [ - + ]: 242 : if (args == NULL) {
295 : 0 : return -1;
296 : : }
297 : : }
298 : :
299 : : /* set no huge pages */
300 [ - + + + ]: 2603 : if (opts->no_huge) {
301 : 6 : mem_disable_huge_pages();
302 : : }
303 : :
304 : : /* set the main core */
305 [ + + ]: 2603 : if (opts->main_core > 0) {
306 : 31 : args = push_arg(args, &argcount, _sprintf_alloc("%s=%d",
307 : 31 : DPDK_MAIN_CORE_PARAM, opts->main_core));
308 [ - + ]: 31 : if (args == NULL) {
309 : 0 : return -1;
310 : : }
311 : : }
312 : :
313 : : /* set no pci if enabled */
314 [ - + + + ]: 2603 : if (opts->no_pci) {
315 : 139 : args = push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
316 [ - + ]: 139 : if (args == NULL) {
317 : 0 : return -1;
318 : : }
319 : : }
320 : :
321 [ + + ]: 2603 : if (no_huge) {
322 [ - + + - : 6 : 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 [ - + ]: 6 : 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 [ - + ]: 6 : 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 : 6 : args = push_arg(args, &argcount, _sprintf_alloc("--no-huge"));
346 : 6 : args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=va"));
347 : :
348 : : } else {
349 : : /* create just one hugetlbfs file */
350 [ + + + + ]: 2597 : if (opts->hugepage_single_segments) {
351 : 40 : args = push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
352 [ - + ]: 40 : 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 [ + + + + ]: 2597 : if (opts->unlink_hugepage ||
364 [ + + + + : 2596 : (opts->shm_id < 0 && !opts->hugepage_single_segments)) {
+ + ]
365 : 2067 : args = push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
366 [ - + ]: 2067 : if (args == NULL) {
367 : 0 : return -1;
368 : : }
369 : : }
370 : :
371 : : /* use a specific hugetlbfs mount */
372 [ - + ]: 2597 : 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 [ - + ]: 2603 : 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 : 2603 : args = push_arg(args, &argcount, _sprintf_alloc("--no-telemetry"));
401 [ - + ]: 2603 : 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 : 2603 : args = push_arg(args, &argcount, strdup("--log-level=lib.eal:6"));
409 [ - + ]: 2603 : 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 : 2603 : args = push_arg(args, &argcount, strdup("--log-level=lib.cryptodev:5"));
417 [ - + ]: 2603 : 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 : 2603 : args = push_arg(args, &argcount, strdup("--log-level=lib.power:5"));
425 [ - + ]: 2603 : 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 : 2603 : args = push_arg(args, &argcount, strdup("--log-level=user1:6"));
435 [ - + ]: 2603 : if (args == NULL) {
436 : 0 : return -1;
437 : : }
438 : :
439 : : #ifdef __linux__
440 : :
441 [ - + ]: 2603 : 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 [ + + - + ]: 2603 : 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 [ + + + + ]: 2603 : if (!no_huge && get_iommu_width() < SPDK_IOMMU_VA_REQUIRED_WIDTH) {
465 : 1776 : args = push_arg(args, &argcount, _sprintf_alloc("--iova-mode=pa"));
466 [ - + ]: 1776 : 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 : 2603 : args = push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x%" PRIx64, opts->base_virtaddr));
488 [ - + ]: 2603 : 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 [ + + ]: 2603 : if (!no_huge &&
502 [ + + + + : 2597 : (!opts->env_context || strstr(opts->env_context, "--legacy-mem") == NULL)) {
+ - ]
503 : 2597 : args = push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations"));
504 [ - + ]: 2597 : if (args == NULL) {
505 : 0 : return -1;
506 : : }
507 : : }
508 : :
509 [ + + ]: 2603 : if (opts->shm_id < 0) {
510 : 2110 : args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
511 : : getpid()));
512 [ - + ]: 2110 : if (args == NULL) {
513 : 0 : return -1;
514 : : }
515 : : } else {
516 : 493 : args = push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
517 : 493 : opts->shm_id));
518 [ - + ]: 493 : if (args == NULL) {
519 : 0 : return -1;
520 : : }
521 : :
522 : : /* set the process type */
523 : 493 : args = push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
524 [ - + ]: 493 : if (args == NULL) {
525 : 0 : return -1;
526 : : }
527 : : }
528 : :
529 : : /* --vfio-vf-token used for VF initialized by vfio_pci driver. */
530 [ - + ]: 2603 : 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 [ + + ]: 2603 : if (opts->env_context) {
540 [ - + ]: 27 : char *ptr = strdup(opts->env_context);
541 : 27 : 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 [ + + ]: 54 : while (tok != NULL) {
548 [ - + ]: 27 : args = push_arg(args, &argcount, strdup(tok));
549 : 27 : tok = strtok(NULL, " \t");
550 : : }
551 : :
552 : 27 : free(ptr);
553 : : }
554 : :
555 : 2603 : g_eal_cmdline = args;
556 : 2603 : g_eal_cmdline_argcount = argcount;
557 : 2603 : return argcount;
558 : : }
559 : :
560 : : int
561 : 2624 : spdk_env_dpdk_post_init(bool legacy_mem)
562 : : {
563 : : int rc;
564 : :
565 : 2624 : rc = pci_env_init();
566 [ - + ]: 2624 : if (rc < 0) {
567 : 0 : SPDK_ERRLOG("pci_env_init() failed\n");
568 : 0 : return rc;
569 : : }
570 : :
571 : 2624 : rc = mem_map_init(legacy_mem);
572 [ - + ]: 2624 : if (rc < 0) {
573 : 0 : SPDK_ERRLOG("Failed to allocate mem_map\n");
574 : 0 : return rc;
575 : : }
576 : :
577 : 2624 : rc = vtophys_init();
578 [ - + ]: 2624 : if (rc < 0) {
579 : 0 : SPDK_ERRLOG("Failed to initialize vtophys\n");
580 : 0 : return rc;
581 : : }
582 : :
583 : 2624 : return 0;
584 : : }
585 : :
586 : : void
587 : 2565 : spdk_env_dpdk_post_fini(void)
588 : : {
589 : 2565 : pci_env_fini();
590 : :
591 : 2565 : free_args(g_eal_cmdline, g_eal_cmdline_argcount);
592 : 2565 : g_eal_cmdline = NULL;
593 : 2565 : g_eal_cmdline_argcount = 0;
594 : 2565 : }
595 : :
596 : : static void
597 : 2603 : env_copy_opts(struct spdk_env_opts *opts, const struct spdk_env_opts *opts_user,
598 : : size_t user_opts_size)
599 : : {
600 : 2603 : opts->opts_size = sizeof(*opts);
601 : 2603 : spdk_env_opts_init(opts);
602 [ - + - + ]: 2603 : 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 : 2603 : }
611 : :
612 : : int
613 : 2666 : spdk_env_init(const struct spdk_env_opts *opts_user)
614 : : {
615 : 2666 : struct spdk_env_opts opts_local = {};
616 : 2666 : struct spdk_env_opts *opts = &opts_local;
617 : 2666 : char **dpdk_args = NULL;
618 : 2666 : 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 [ + + + + ]: 2666 : if (g_external_init == false) {
629 [ - + ]: 63 : if (opts_user != NULL) {
630 [ # # ]: 0 : fprintf(stderr, "Invalid arguments to reinitialize SPDK env\n");
631 : 0 : return -EINVAL;
632 : : }
633 : :
634 : 63 : printf("Starting %s / %s reinitialization...\n", SPDK_VERSION_STRING, rte_version());
635 : 63 : pci_env_reinit();
636 : :
637 : 63 : return 0;
638 : : }
639 : :
640 [ - + ]: 2603 : if (opts_user == NULL) {
641 [ # # ]: 0 : fprintf(stderr, "NULL arguments to initialize DPDK\n");
642 : 0 : return -EINVAL;
643 : : }
644 : :
645 : 2603 : min_opts_size = offsetof(struct spdk_env_opts, opts_size) + sizeof(opts->opts_size);
646 : 2603 : user_opts_size = opts_user->opts_size;
647 [ + - ]: 2603 : if (user_opts_size < min_opts_size) {
648 : 2603 : fprintf(stderr, "Invalid opts->opts_size %d too small, please set opts_size correctly\n",
649 [ - + ]: 2603 : (int)opts_user->opts_size);
650 : 2603 : user_opts_size = min_opts_size;
651 : : }
652 : :
653 : 2603 : env_copy_opts(opts, opts_user, user_opts_size);
654 : :
655 : 2603 : settings = OPENSSL_INIT_new();
656 [ - + ]: 2603 : 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 : 2603 : OPENSSL_INIT_set_config_file_flags(settings, 0);
664 : : #endif
665 : 2603 : rc = OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, settings);
666 [ - + ]: 2603 : 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 : 2603 : OPENSSL_INIT_free(settings);
672 : :
673 : 2603 : rc = build_eal_cmdline(opts);
674 [ - + ]: 2603 : if (rc < 0) {
675 : 0 : SPDK_ERRLOG("Invalid arguments to initialize DPDK\n");
676 : 0 : return -EINVAL;
677 : : }
678 : :
679 : 2603 : SPDK_PRINTF("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
680 : :
681 : 2603 : args_print = _sprintf_alloc("[ DPDK EAL parameters: ");
682 [ - + ]: 2603 : if (args_print == NULL) {
683 : 0 : return -ENOMEM;
684 : : }
685 [ + + ]: 35632 : for (i = 0; i < g_eal_cmdline_argcount; i++) {
686 : 33029 : args_tmp = args_print;
687 : 33029 : args_print = _sprintf_alloc("%s%s ", args_tmp, g_eal_cmdline[i]);
688 [ - + ]: 33029 : if (args_print == NULL) {
689 : 0 : free(args_tmp);
690 : 0 : return -ENOMEM;
691 : : }
692 : 33029 : free(args_tmp);
693 : : }
694 : 2603 : SPDK_PRINTF("%s]\n", args_print);
695 : 2603 : 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 : 2603 : dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *));
702 [ - + ]: 2603 : if (dpdk_args == NULL) {
703 : 0 : SPDK_ERRLOG("Failed to allocate dpdk_args\n");
704 : 0 : return -ENOMEM;
705 : : }
706 [ - + - + ]: 2603 : memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount);
707 : :
708 : 2603 : fflush(stdout);
709 : 2603 : orig_optind = optind;
710 : 2603 : optind = 1;
711 : 2603 : rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args);
712 : 2603 : optind = orig_optind;
713 : :
714 : 2603 : free(dpdk_args);
715 : :
716 [ - + ]: 2603 : 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 : 2603 : legacy_mem = false;
726 [ + + - + : 2603 : if (opts->env_context && strstr(opts->env_context, "--legacy-mem") != NULL) {
- + ]
727 : 0 : legacy_mem = true;
728 : : }
729 : :
730 : 2603 : rc = spdk_env_dpdk_post_init(legacy_mem);
731 [ + - ]: 2603 : if (rc == 0) {
732 : 2603 : g_external_init = false;
733 : : }
734 : :
735 : 2603 : 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 : 2764 : dpdk_cleanup(void)
745 : : {
746 : : /* Only call rte_eal_cleanup if the SPDK env library called rte_eal_init. */
747 [ + + + + ]: 2764 : if (!g_external_init) {
748 : 2603 : rte_eal_cleanup();
749 : : }
750 : 2764 : }
751 : :
752 : : void
753 : 2565 : spdk_env_fini(void)
754 : : {
755 : 2565 : spdk_env_dpdk_post_fini();
756 : 2565 : }
757 : :
758 : : bool
759 : 34929 : spdk_env_dpdk_external_init(void)
760 : : {
761 [ - + ]: 34929 : return g_external_init;
762 : : }
|