xref: /spdk/lib/env_dpdk/init.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "env_internal.h"
37 
38 #include "spdk/version.h"
39 #include "spdk/env_dpdk.h"
40 
41 #include <rte_config.h>
42 #include <rte_eal.h>
43 #include <rte_errno.h>
44 
45 #define SPDK_ENV_DPDK_DEFAULT_NAME		"spdk"
46 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID		-1
47 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE		-1
48 #define SPDK_ENV_DPDK_DEFAULT_MASTER_CORE	-1
49 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL	-1
50 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK		"0x1"
51 
52 static char **g_eal_cmdline;
53 static int g_eal_cmdline_argcount;
54 static bool g_external_init = true;
55 
56 static char *
57 _sprintf_alloc(const char *format, ...)
58 {
59 	va_list args;
60 	va_list args_copy;
61 	char *buf;
62 	size_t bufsize;
63 	int rc;
64 
65 	va_start(args, format);
66 
67 	/* Try with a small buffer first. */
68 	bufsize = 32;
69 
70 	/* Limit maximum buffer size to something reasonable so we don't loop forever. */
71 	while (bufsize <= 1024 * 1024) {
72 		buf = malloc(bufsize);
73 		if (buf == NULL) {
74 			va_end(args);
75 			return NULL;
76 		}
77 
78 		va_copy(args_copy, args);
79 		rc = vsnprintf(buf, bufsize, format, args_copy);
80 		va_end(args_copy);
81 
82 		/*
83 		 * If vsnprintf() returned a count within our current buffer size, we are done.
84 		 * The count does not include the \0 terminator, so rc == bufsize is not OK.
85 		 */
86 		if (rc >= 0 && (size_t)rc < bufsize) {
87 			va_end(args);
88 			return buf;
89 		}
90 
91 		/*
92 		 * vsnprintf() should return the required space, but some libc versions do not
93 		 * implement this correctly, so just double the buffer size and try again.
94 		 *
95 		 * We don't need the data in buf, so rather than realloc(), use free() and malloc()
96 		 * again to avoid a copy.
97 		 */
98 		free(buf);
99 		bufsize *= 2;
100 	}
101 
102 	va_end(args);
103 	return NULL;
104 }
105 
106 static void
107 spdk_env_unlink_shared_files(void)
108 {
109 	/* Starting with DPDK 18.05, there are more files with unpredictable paths
110 	 * and filenames. The --no-shconf option prevents from creating them, but
111 	 * only for DPDK 18.08+. For DPDK 18.05 we just leave them be.
112 	 */
113 #if RTE_VERSION < RTE_VERSION_NUM(18, 05, 0, 0)
114 	char buffer[PATH_MAX];
115 
116 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid());
117 	if (unlink(buffer)) {
118 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
119 	}
120 #endif
121 }
122 
123 void
124 spdk_env_opts_init(struct spdk_env_opts *opts)
125 {
126 	if (!opts) {
127 		return;
128 	}
129 
130 	memset(opts, 0, sizeof(*opts));
131 
132 	opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
133 	opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
134 	opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
135 	opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
136 	opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE;
137 	opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
138 }
139 
140 static void
141 spdk_free_args(char **args, int argcount)
142 {
143 	int i;
144 
145 	for (i = 0; i < argcount; i++) {
146 		free(args[i]);
147 	}
148 
149 	if (argcount) {
150 		free(args);
151 	}
152 }
153 
154 static char **
155 spdk_push_arg(char *args[], int *argcount, char *arg)
156 {
157 	char **tmp;
158 
159 	if (arg == NULL) {
160 		fprintf(stderr, "%s: NULL arg supplied\n", __func__);
161 		spdk_free_args(args, *argcount);
162 		return NULL;
163 	}
164 
165 	tmp = realloc(args, sizeof(char *) * (*argcount + 1));
166 	if (tmp == NULL) {
167 		free(arg);
168 		spdk_free_args(args, *argcount);
169 		return NULL;
170 	}
171 
172 	tmp[*argcount] = arg;
173 	(*argcount)++;
174 
175 	return tmp;
176 }
177 
178 static int
179 spdk_build_eal_cmdline(const struct spdk_env_opts *opts)
180 {
181 	int argcount = 0;
182 	char **args;
183 
184 	args = NULL;
185 
186 	/* set the program name */
187 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
188 	if (args == NULL) {
189 		return -1;
190 	}
191 
192 	/* disable shared configuration files when in single process mode. This allows for cleaner shutdown */
193 	if (opts->shm_id < 0) {
194 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf"));
195 		if (args == NULL) {
196 			return -1;
197 		}
198 	}
199 
200 	/* set the coremask */
201 	/* NOTE: If coremask starts with '[' and ends with ']' it is a core list
202 	 */
203 	if (opts->core_mask[0] == '[') {
204 		char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
205 		int len = strlen(l_arg);
206 		if (l_arg[len - 1] == ']') {
207 			l_arg[len - 1] = '\0';
208 		}
209 		args = spdk_push_arg(args, &argcount, l_arg);
210 	} else {
211 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
212 	}
213 
214 	if (args == NULL) {
215 		return -1;
216 	}
217 
218 	/* set the memory channel number */
219 	if (opts->mem_channel > 0) {
220 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
221 		if (args == NULL) {
222 			return -1;
223 		}
224 	}
225 
226 	/* set the memory size */
227 	if (opts->mem_size >= 0) {
228 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
229 		if (args == NULL) {
230 			return -1;
231 		}
232 	}
233 
234 	/* set the master core */
235 	if (opts->master_core > 0) {
236 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d",
237 				     opts->master_core));
238 		if (args == NULL) {
239 			return -1;
240 		}
241 	}
242 
243 	/* set no pci  if enabled */
244 	if (opts->no_pci) {
245 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
246 		if (args == NULL) {
247 			return -1;
248 		}
249 	}
250 
251 	/* create just one hugetlbfs file */
252 	if (opts->hugepage_single_segments) {
253 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
254 		if (args == NULL) {
255 			return -1;
256 		}
257 	}
258 
259 	/* unlink hugepages after initialization */
260 	if (opts->unlink_hugepage) {
261 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
262 		if (args == NULL) {
263 			return -1;
264 		}
265 	}
266 
267 	/* use a specific hugetlbfs mount */
268 	if (opts->hugedir) {
269 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir));
270 		if (args == NULL) {
271 			return -1;
272 		}
273 	}
274 
275 #if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0) && RTE_VERSION < RTE_VERSION_NUM(18, 5, 1, 0)
276 	/* Dynamic memory management is buggy in DPDK 18.05.0. Don't use it. */
277 	if (!opts->env_context || strcmp(opts->env_context, "--legacy-mem") != 0) {
278 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--legacy-mem"));
279 		if (args == NULL) {
280 			return -1;
281 		}
282 	}
283 #endif
284 
285 	if (opts->num_pci_addr) {
286 		size_t i;
287 		char bdf[32];
288 		struct spdk_pci_addr *pci_addr =
289 				opts->pci_blacklist ? opts->pci_blacklist : opts->pci_whitelist;
290 
291 		for (i = 0; i < opts->num_pci_addr; i++) {
292 			spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]);
293 			args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s=%s",
294 					     (opts->pci_blacklist ? "--pci-blacklist" : "--pci-whitelist"),
295 					     bdf));
296 			if (args == NULL) {
297 				return -1;
298 			}
299 		}
300 	}
301 
302 	/* The following log-level options are not understood by older DPDKs */
303 #if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0)
304 	/* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages.
305 	 * This can be overridden by specifying the same option in opts->env_context
306 	 */
307 	args = spdk_push_arg(args, &argcount, strdup("--log-level=lib.eal:6"));
308 	if (args == NULL) {
309 		return -1;
310 	}
311 
312 	/* Lower default CRYPTO loglevel to RTE_LOG_ERR to avoid a ton of init msgs.
313 	 * This can be overridden by specifying the same option in opts->env_context
314 	 */
315 	args = spdk_push_arg(args, &argcount, strdup("--log-level=lib.cryptodev:5"));
316 	if (args == NULL) {
317 		return -1;
318 	}
319 
320 	/* `user1` log type is used by rte_vhost, which prints an INFO log for each received
321 	 * vhost user message. We don't want that. The same log type is also used by a couple
322 	 * of other DPDK libs, but none of which we make use right now. If necessary, this can
323 	 * be overridden via opts->env_context.
324 	 */
325 	args = spdk_push_arg(args, &argcount, strdup("--log-level=user1:6"));
326 	if (args == NULL) {
327 		return -1;
328 	}
329 #endif
330 
331 	if (opts->env_context) {
332 		args = spdk_push_arg(args, &argcount, strdup(opts->env_context));
333 		if (args == NULL) {
334 			return -1;
335 		}
336 	}
337 
338 #ifdef __linux__
339 	/* Set the base virtual address - it must be an address that is not in the
340 	 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the
341 	 * mmap hint.
342 	 *
343 	 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
344 	 */
345 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x200000000000"));
346 	if (args == NULL) {
347 		return -1;
348 	}
349 
350 	/* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood.
351 	 * This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two
352 	 * physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split
353 	 * the memory for a buffer over two allocations meaning the buffer will be split over a memory region.
354 	 */
355 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0)
356 	if (!opts->env_context || strcmp(opts->env_context, "--legacy-mem") != 0) {
357 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations"));
358 		if (args == NULL) {
359 			return -1;
360 		}
361 	}
362 #endif
363 
364 	if (opts->shm_id < 0) {
365 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
366 				     getpid()));
367 		if (args == NULL) {
368 			return -1;
369 		}
370 	} else {
371 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
372 				     opts->shm_id));
373 		if (args == NULL) {
374 			return -1;
375 		}
376 
377 		/* set the process type */
378 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
379 		if (args == NULL) {
380 			return -1;
381 		}
382 	}
383 #endif
384 
385 	g_eal_cmdline = args;
386 	g_eal_cmdline_argcount = argcount;
387 	return argcount;
388 }
389 
390 int
391 spdk_env_dpdk_post_init(void)
392 {
393 	int rc;
394 
395 	spdk_pci_init();
396 
397 	rc = spdk_mem_map_init();
398 	if (rc < 0) {
399 		fprintf(stderr, "Failed to allocate mem_map\n");
400 		return rc;
401 	}
402 
403 	rc = spdk_vtophys_init();
404 	if (rc < 0) {
405 		fprintf(stderr, "Failed to initialize vtophys\n");
406 		return rc;
407 	}
408 
409 	return 0;
410 }
411 
412 void
413 spdk_env_dpdk_post_fini(void)
414 {
415 	spdk_pci_fini();
416 
417 	spdk_free_args(g_eal_cmdline, g_eal_cmdline_argcount);
418 }
419 
420 int
421 spdk_env_init(const struct spdk_env_opts *opts)
422 {
423 	char **dpdk_args = NULL;
424 	int i, rc;
425 	int orig_optind;
426 
427 	g_external_init = false;
428 
429 	rc = spdk_build_eal_cmdline(opts);
430 	if (rc < 0) {
431 		fprintf(stderr, "Invalid arguments to initialize DPDK\n");
432 		return -EINVAL;
433 	}
434 
435 	printf("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
436 	printf("[ DPDK EAL parameters: ");
437 	for (i = 0; i < g_eal_cmdline_argcount; i++) {
438 		printf("%s ", g_eal_cmdline[i]);
439 	}
440 	printf("]\n");
441 
442 	/* DPDK rearranges the array we pass to it, so make a copy
443 	 * before passing so we can still free the individual strings
444 	 * correctly.
445 	 */
446 	dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *));
447 	if (dpdk_args == NULL) {
448 		fprintf(stderr, "Failed to allocate dpdk_args\n");
449 		return -ENOMEM;
450 	}
451 	memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount);
452 
453 	fflush(stdout);
454 	orig_optind = optind;
455 	optind = 1;
456 	rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args);
457 	optind = orig_optind;
458 
459 	free(dpdk_args);
460 
461 	if (rc < 0) {
462 		if (rte_errno == EALREADY) {
463 			fprintf(stderr, "DPDK already initialized\n");
464 		} else {
465 			fprintf(stderr, "Failed to initialize DPDK\n");
466 		}
467 		return -rte_errno;
468 	}
469 
470 	if (opts->shm_id < 0 && !opts->hugepage_single_segments) {
471 		/*
472 		 * Unlink hugepage and config info files after init.  This will ensure they get
473 		 *  deleted on app exit, even if the app crashes and does not exit normally.
474 		 *  Only do this when not in multi-process mode, since for multi-process other
475 		 *  apps will need to open these files. These files are not created for
476 		 *  "single file segments".
477 		 */
478 		spdk_env_unlink_shared_files();
479 	}
480 
481 	return spdk_env_dpdk_post_init();
482 }
483 
484 void
485 spdk_env_fini(void)
486 {
487 	spdk_env_dpdk_post_fini();
488 }
489 
490 bool
491 spdk_env_dpdk_external_init(void)
492 {
493 	return g_external_init;
494 }
495