xref: /spdk/lib/env_dpdk/init.c (revision 1fc4165fe9bf8512483356ad8e6d27f793f2e3db)
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 
44 #define SPDK_ENV_DPDK_DEFAULT_NAME		"spdk"
45 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID		-1
46 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE		-1
47 #define SPDK_ENV_DPDK_DEFAULT_MASTER_CORE	-1
48 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL	-1
49 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK		"0x1"
50 
51 static char **g_eal_cmdline;
52 static int g_eal_cmdline_argcount;
53 static bool g_external_init = true;
54 
55 static char *
56 _sprintf_alloc(const char *format, ...)
57 {
58 	va_list args;
59 	va_list args_copy;
60 	char *buf;
61 	size_t bufsize;
62 	int rc;
63 
64 	va_start(args, format);
65 
66 	/* Try with a small buffer first. */
67 	bufsize = 32;
68 
69 	/* Limit maximum buffer size to something reasonable so we don't loop forever. */
70 	while (bufsize <= 1024 * 1024) {
71 		buf = malloc(bufsize);
72 		if (buf == NULL) {
73 			va_end(args);
74 			return NULL;
75 		}
76 
77 		va_copy(args_copy, args);
78 		rc = vsnprintf(buf, bufsize, format, args_copy);
79 		va_end(args_copy);
80 
81 		/*
82 		 * If vsnprintf() returned a count within our current buffer size, we are done.
83 		 * The count does not include the \0 terminator, so rc == bufsize is not OK.
84 		 */
85 		if (rc >= 0 && (size_t)rc < bufsize) {
86 			va_end(args);
87 			return buf;
88 		}
89 
90 		/*
91 		 * vsnprintf() should return the required space, but some libc versions do not
92 		 * implement this correctly, so just double the buffer size and try again.
93 		 *
94 		 * We don't need the data in buf, so rather than realloc(), use free() and malloc()
95 		 * again to avoid a copy.
96 		 */
97 		free(buf);
98 		bufsize *= 2;
99 	}
100 
101 	va_end(args);
102 	return NULL;
103 }
104 
105 static void
106 spdk_env_unlink_shared_files(void)
107 {
108 	/* Starting with DPDK 18.05, there are more files with unpredictable paths
109 	 * and filenames. The --no-shconf option prevents from creating them, but
110 	 * only for DPDK 18.08+. For DPDK 18.05 we just leave them be.
111 	 */
112 #if RTE_VERSION < RTE_VERSION_NUM(18, 05, 0, 0)
113 	char buffer[PATH_MAX];
114 
115 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid());
116 	if (unlink(buffer)) {
117 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
118 	}
119 #endif
120 }
121 
122 void
123 spdk_env_opts_init(struct spdk_env_opts *opts)
124 {
125 	if (!opts) {
126 		return;
127 	}
128 
129 	memset(opts, 0, sizeof(*opts));
130 
131 	opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
132 	opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
133 	opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
134 	opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
135 	opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE;
136 	opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
137 }
138 
139 static void
140 spdk_free_args(char **args, int argcount)
141 {
142 	int i;
143 
144 	for (i = 0; i < argcount; i++) {
145 		free(args[i]);
146 	}
147 
148 	if (argcount) {
149 		free(args);
150 	}
151 }
152 
153 static char **
154 spdk_push_arg(char *args[], int *argcount, char *arg)
155 {
156 	char **tmp;
157 
158 	if (arg == NULL) {
159 		fprintf(stderr, "%s: NULL arg supplied\n", __func__);
160 		spdk_free_args(args, *argcount);
161 		return NULL;
162 	}
163 
164 	tmp = realloc(args, sizeof(char *) * (*argcount + 1));
165 	if (tmp == NULL) {
166 		free(arg);
167 		spdk_free_args(args, *argcount);
168 		return NULL;
169 	}
170 
171 	tmp[*argcount] = arg;
172 	(*argcount)++;
173 
174 	return tmp;
175 }
176 
177 static void
178 spdk_destruct_eal_cmdline(void)
179 {
180 	spdk_free_args(g_eal_cmdline, g_eal_cmdline_argcount);
181 }
182 
183 
184 static int
185 spdk_build_eal_cmdline(const struct spdk_env_opts *opts)
186 {
187 	int argcount = 0;
188 	char **args;
189 
190 	args = NULL;
191 
192 	/* set the program name */
193 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
194 	if (args == NULL) {
195 		return -1;
196 	}
197 
198 	/* disable shared configuration files when in single process mode. This allows for cleaner shutdown */
199 	if (opts->shm_id < 0) {
200 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--no-shconf"));
201 		if (args == NULL) {
202 			return -1;
203 		}
204 	}
205 
206 	/* set the coremask */
207 	/* NOTE: If coremask starts with '[' and ends with ']' it is a core list
208 	 */
209 	if (opts->core_mask[0] == '[') {
210 		char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
211 		int len = strlen(l_arg);
212 		if (l_arg[len - 1] == ']') {
213 			l_arg[len - 1] = '\0';
214 		}
215 		args = spdk_push_arg(args, &argcount, l_arg);
216 	} else {
217 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
218 	}
219 
220 	if (args == NULL) {
221 		return -1;
222 	}
223 
224 	/* set the memory channel number */
225 	if (opts->mem_channel > 0) {
226 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
227 		if (args == NULL) {
228 			return -1;
229 		}
230 	}
231 
232 	/* set the memory size */
233 	if (opts->mem_size >= 0) {
234 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
235 		if (args == NULL) {
236 			return -1;
237 		}
238 	}
239 
240 	/* set the master core */
241 	if (opts->master_core > 0) {
242 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d",
243 				     opts->master_core));
244 		if (args == NULL) {
245 			return -1;
246 		}
247 	}
248 
249 	/* set no pci  if enabled */
250 	if (opts->no_pci) {
251 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
252 		if (args == NULL) {
253 			return -1;
254 		}
255 	}
256 
257 	/* create just one hugetlbfs file */
258 	if (opts->hugepage_single_segments) {
259 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
260 		if (args == NULL) {
261 			return -1;
262 		}
263 	}
264 
265 	/* unlink hugepages after initialization */
266 	if (opts->unlink_hugepage) {
267 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
268 		if (args == NULL) {
269 			return -1;
270 		}
271 	}
272 
273 	/* use a specific hugetlbfs mount */
274 	if (opts->hugedir) {
275 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-dir=%s", opts->hugedir));
276 		if (args == NULL) {
277 			return -1;
278 		}
279 	}
280 
281 #if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0) && RTE_VERSION < RTE_VERSION_NUM(18, 5, 1, 0)
282 	/* Dynamic memory management is buggy in DPDK 18.05.0. Don't use it. */
283 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("--legacy-mem"));
284 	if (args == NULL) {
285 		return -1;
286 	}
287 #endif
288 
289 	if (opts->num_pci_addr) {
290 		size_t i;
291 		char bdf[32];
292 		struct spdk_pci_addr *pci_addr =
293 				opts->pci_blacklist ? opts->pci_blacklist : opts->pci_whitelist;
294 
295 		for (i = 0; i < opts->num_pci_addr; i++) {
296 			spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]);
297 			args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s=%s",
298 					     (opts->pci_blacklist ? "--pci-blacklist" : "--pci-whitelist"),
299 					     bdf));
300 			if (args == NULL) {
301 				return -1;
302 			}
303 		}
304 	}
305 
306 	/* Lower default EAL loglevel to RTE_LOG_NOTICE - normal, but significant messages.
307 	 * This can be overridden by specifying the same option in opts->env_context
308 	 */
309 	args = spdk_push_arg(args, &argcount, strdup("--log-level=lib.eal:6"));
310 	if (args == NULL) {
311 		return -1;
312 	}
313 
314 	if (opts->env_context) {
315 		args = spdk_push_arg(args, &argcount, strdup(opts->env_context));
316 		if (args == NULL) {
317 			return -1;
318 		}
319 	}
320 
321 #ifdef __linux__
322 	/* Set the base virtual address - it must be an address that is not in the
323 	 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the
324 	 * mmap hint.
325 	 *
326 	 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
327 	 */
328 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x200000000000"));
329 	if (args == NULL) {
330 		return -1;
331 	}
332 
333 	/* --match-allocation prevents DPDK from merging or splitting system memory allocations under the hood.
334 	 * This is critical for RDMA when attempting to use an rte_mempool based buffer pool. If DPDK merges two
335 	 * physically or IOVA contiguous memory regions, then when we go to allocate a buffer pool, it can split
336 	 * the memory for a buffer over two allocations meaning the buffer will be split over a memory region.
337 	 */
338 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0)
339 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", "--match-allocations"));
340 	if (args == NULL) {
341 		return -1;
342 	}
343 #endif
344 
345 	if (opts->shm_id < 0) {
346 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
347 				     getpid()));
348 		if (args == NULL) {
349 			return -1;
350 		}
351 	} else {
352 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
353 				     opts->shm_id));
354 		if (args == NULL) {
355 			return -1;
356 		}
357 
358 		/* set the process type */
359 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
360 		if (args == NULL) {
361 			return -1;
362 		}
363 	}
364 #endif
365 
366 	g_eal_cmdline = args;
367 	g_eal_cmdline_argcount = argcount;
368 	if (atexit(spdk_destruct_eal_cmdline) != 0) {
369 		fprintf(stderr, "Failed to register cleanup handler\n");
370 	}
371 
372 	return argcount;
373 }
374 
375 int
376 spdk_env_dpdk_post_init(void)
377 {
378 	spdk_pci_init();
379 
380 	if (spdk_mem_map_init() < 0) {
381 		fprintf(stderr, "Failed to allocate mem_map\n");
382 		return -1;
383 	}
384 	if (spdk_vtophys_init() < 0) {
385 		fprintf(stderr, "Failed to initialize vtophys\n");
386 		return -1;
387 	}
388 
389 	return 0;
390 }
391 
392 int
393 spdk_env_init(const struct spdk_env_opts *opts)
394 {
395 	char **dpdk_args = NULL;
396 	int i, rc;
397 	int orig_optind;
398 
399 	g_external_init = false;
400 
401 	rc = spdk_build_eal_cmdline(opts);
402 	if (rc < 0) {
403 		fprintf(stderr, "Invalid arguments to initialize DPDK\n");
404 		return -1;
405 	}
406 
407 	printf("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
408 	printf("[ DPDK EAL parameters: ");
409 	for (i = 0; i < g_eal_cmdline_argcount; i++) {
410 		printf("%s ", g_eal_cmdline[i]);
411 	}
412 	printf("]\n");
413 
414 	/* DPDK rearranges the array we pass to it, so make a copy
415 	 * before passing so we can still free the individual strings
416 	 * correctly.
417 	 */
418 	dpdk_args = calloc(g_eal_cmdline_argcount, sizeof(char *));
419 	if (dpdk_args == NULL) {
420 		fprintf(stderr, "Failed to allocate dpdk_args\n");
421 		return -1;
422 	}
423 	memcpy(dpdk_args, g_eal_cmdline, sizeof(char *) * g_eal_cmdline_argcount);
424 
425 	fflush(stdout);
426 	orig_optind = optind;
427 	optind = 1;
428 	rc = rte_eal_init(g_eal_cmdline_argcount, dpdk_args);
429 	optind = orig_optind;
430 
431 	free(dpdk_args);
432 
433 	if (rc < 0) {
434 		fprintf(stderr, "Failed to initialize DPDK\n");
435 		return -1;
436 	}
437 
438 	if (opts->shm_id < 0 && !opts->hugepage_single_segments) {
439 		/*
440 		 * Unlink hugepage and config info files after init.  This will ensure they get
441 		 *  deleted on app exit, even if the app crashes and does not exit normally.
442 		 *  Only do this when not in multi-process mode, since for multi-process other
443 		 *  apps will need to open these files. These files are not created for
444 		 *  "single file segments".
445 		 */
446 		spdk_env_unlink_shared_files();
447 	}
448 
449 	return spdk_env_dpdk_post_init();
450 }
451 
452 bool
453 spdk_env_dpdk_external_init(void)
454 {
455 	return g_external_init;
456 }
457