xref: /spdk/lib/env_dpdk/init.c (revision f6c5e40aed498e983c84a75122315a35a74c8ac1)
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 static void
104 spdk_env_unlink_shared_files(void)
105 {
106 	char buffer[PATH_MAX];
107 
108 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_config", getpid());
109 	if (unlink(buffer)) {
110 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
111 	}
112 
113 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid());
114 	if (unlink(buffer)) {
115 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
116 	}
117 }
118 
119 void
120 spdk_env_opts_init(struct spdk_env_opts *opts)
121 {
122 	if (!opts) {
123 		return;
124 	}
125 
126 	memset(opts, 0, sizeof(*opts));
127 
128 	opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
129 	opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
130 	opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
131 	opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
132 	opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE;
133 	opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
134 }
135 
136 static void
137 spdk_free_args(char **args, int argcount)
138 {
139 	int i;
140 
141 	for (i = 0; i < argcount; i++) {
142 		free(args[i]);
143 	}
144 
145 	if (argcount) {
146 		free(args);
147 	}
148 }
149 
150 static char **
151 spdk_push_arg(char *args[], int *argcount, char *arg)
152 {
153 	char **tmp;
154 
155 	if (arg == NULL) {
156 		fprintf(stderr, "%s: NULL arg supplied\n", __func__);
157 		spdk_free_args(args, *argcount);
158 		return NULL;
159 	}
160 
161 	tmp = realloc(args, sizeof(char *) * (*argcount + 1));
162 	if (tmp == NULL) {
163 		spdk_free_args(args, *argcount);
164 		return NULL;
165 	}
166 
167 	tmp[*argcount] = arg;
168 	(*argcount)++;
169 
170 	return tmp;
171 }
172 
173 static void
174 spdk_destruct_eal_cmdline(void)
175 {
176 	spdk_free_args(eal_cmdline, eal_cmdline_argcount);
177 }
178 
179 
180 static int
181 spdk_build_eal_cmdline(const struct spdk_env_opts *opts)
182 {
183 	int argcount = 0;
184 	char **args;
185 
186 	args = NULL;
187 
188 	/* set the program name */
189 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s", opts->name));
190 	if (args == NULL) {
191 		return -1;
192 	}
193 
194 	/* set the coremask */
195 	/* NOTE: If coremask starts with '[' and ends with ']' it is a core list
196 	 */
197 	if (opts->core_mask[0] == '[') {
198 		char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
199 		int len = strlen(l_arg);
200 		if (l_arg[len - 1] == ']') {
201 			l_arg[len - 1] = '\0';
202 		}
203 		args = spdk_push_arg(args, &argcount, l_arg);
204 	} else {
205 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
206 	}
207 
208 	if (args == NULL) {
209 		return -1;
210 	}
211 
212 	/* set the memory channel number */
213 	if (opts->mem_channel > 0) {
214 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
215 		if (args == NULL) {
216 			return -1;
217 		}
218 	}
219 
220 	/* set the memory size */
221 	if (opts->mem_size > 0) {
222 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
223 		if (args == NULL) {
224 			return -1;
225 		}
226 	}
227 
228 	/* set the master core */
229 	if (opts->master_core > 0) {
230 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d",
231 				     opts->master_core));
232 		if (args == NULL) {
233 			return -1;
234 		}
235 	}
236 
237 	/* set no pci  if enabled */
238 	if (opts->no_pci) {
239 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
240 		if (args == NULL) {
241 			return -1;
242 		}
243 	}
244 
245 	/* create just one hugetlbfs file */
246 	if (opts->hugepage_single_segments) {
247 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--single-file-segments"));
248 		if (args == NULL) {
249 			return -1;
250 		}
251 	}
252 
253 	/* unlink hugepages after initialization */
254 	if (opts->unlink_hugepage) {
255 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--huge-unlink"));
256 		if (args == NULL) {
257 			return -1;
258 		}
259 	}
260 
261 #if RTE_VERSION >= RTE_VERSION_NUM(18, 05, 0, 0)
262 	/* SPDK holds off with using the new memory management model just yet */
263 	args = spdk_push_arg(args, &argcount, _sprintf_alloc("--legacy-mem"));
264 	if (args == NULL) {
265 		return -1;
266 	}
267 #endif
268 
269 	if (opts->num_pci_addr) {
270 		size_t i;
271 		char bdf[32];
272 		struct spdk_pci_addr *pci_addr =
273 				opts->pci_blacklist ? opts->pci_blacklist : opts->pci_whitelist;
274 
275 		for (i = 0; i < opts->num_pci_addr; i++) {
276 			spdk_pci_addr_fmt(bdf, 32, &pci_addr[i]);
277 			args = spdk_push_arg(args, &argcount, _sprintf_alloc("%s=%s",
278 					     (opts->pci_blacklist ? "--pci-blacklist" : "--pci-whitelist"),
279 					     bdf));
280 			if (args == NULL) {
281 				return -1;
282 			}
283 		}
284 	}
285 
286 #ifdef __linux__
287 	if (opts->shm_id < 0) {
288 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
289 				     getpid()));
290 		if (args == NULL) {
291 			return -1;
292 		}
293 	} else {
294 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
295 				     opts->shm_id));
296 		if (args == NULL) {
297 			return -1;
298 		}
299 
300 		/* Set the base virtual address - it must be an address that is not in the
301 		 * ASAN shadow region, otherwise ASAN-enabled builds will ignore the
302 		 * mmap hint.
303 		 *
304 		 * Ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
305 		 */
306 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x200000000000"));
307 		if (args == NULL) {
308 			return -1;
309 		}
310 
311 		/* set the process type */
312 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
313 		if (args == NULL) {
314 			return -1;
315 		}
316 	}
317 #endif
318 
319 	eal_cmdline = args;
320 	eal_cmdline_argcount = argcount;
321 	if (atexit(spdk_destruct_eal_cmdline) != 0) {
322 		fprintf(stderr, "Failed to register cleanup handler\n");
323 	}
324 
325 	return argcount;
326 }
327 
328 int spdk_env_init(const struct spdk_env_opts *opts)
329 {
330 	char **dpdk_args = NULL;
331 	int i, rc;
332 	int orig_optind;
333 
334 	rc = spdk_build_eal_cmdline(opts);
335 	if (rc < 0) {
336 		fprintf(stderr, "Invalid arguments to initialize DPDK\n");
337 		return -1;
338 	}
339 
340 	printf("Starting %s / %s initialization...\n", SPDK_VERSION_STRING, rte_version());
341 	printf("[ DPDK EAL parameters: ");
342 	for (i = 0; i < eal_cmdline_argcount; i++) {
343 		printf("%s ", eal_cmdline[i]);
344 	}
345 	printf("]\n");
346 
347 	/* DPDK rearranges the array we pass to it, so make a copy
348 	 * before passing so we can still free the individual strings
349 	 * correctly.
350 	 */
351 	dpdk_args = calloc(eal_cmdline_argcount, sizeof(char *));
352 	if (dpdk_args == NULL) {
353 		fprintf(stderr, "Failed to allocate dpdk_args\n");
354 		return -1;
355 	}
356 	memcpy(dpdk_args, eal_cmdline, sizeof(char *) * eal_cmdline_argcount);
357 
358 	fflush(stdout);
359 	orig_optind = optind;
360 	optind = 1;
361 	rc = rte_eal_init(eal_cmdline_argcount, dpdk_args);
362 	optind = orig_optind;
363 
364 	free(dpdk_args);
365 
366 	if (rc < 0) {
367 		fprintf(stderr, "Failed to initialize DPDK\n");
368 		return -1;
369 	}
370 
371 	if (opts->shm_id < 0 && !opts->hugepage_single_segments) {
372 		/*
373 		 * Unlink hugepage and config info files after init.  This will ensure they get
374 		 *  deleted on app exit, even if the app crashes and does not exit normally.
375 		 *  Only do this when not in multi-process mode, since for multi-process other
376 		 *  apps will need to open these files. These files are not created for
377 		 *  "single file segments".
378 		 */
379 		spdk_env_unlink_shared_files();
380 	}
381 
382 	if (spdk_mem_map_init() < 0) {
383 		fprintf(stderr, "Failed to allocate mem_map\n");
384 		return -1;
385 	}
386 	if (spdk_vtophys_init() < 0) {
387 		fprintf(stderr, "Failed to initialize vtophys\n");
388 		return -1;
389 	}
390 
391 	return 0;
392 }
393