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