xref: /spdk/lib/env_dpdk/init.c (revision 66fc591ff7b1188e86b720feee0610a4b1f4fbe6)
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 <rte_config.h>
39 #include <rte_eal.h>
40 
41 #define SPDK_ENV_DPDK_DEFAULT_NAME		"spdk"
42 #define SPDK_ENV_DPDK_DEFAULT_SHM_ID		-1
43 #define SPDK_ENV_DPDK_DEFAULT_MEM_SIZE		-1
44 #define SPDK_ENV_DPDK_DEFAULT_MASTER_CORE	-1
45 #define SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL	-1
46 #define SPDK_ENV_DPDK_DEFAULT_CORE_MASK		"0x1"
47 
48 static char **eal_cmdline;
49 static int eal_cmdline_argcount;
50 
51 static char *
52 _sprintf_alloc(const char *format, ...)
53 {
54 	va_list args;
55 	va_list args_copy;
56 	char *buf;
57 	size_t bufsize;
58 	int rc;
59 
60 	va_start(args, format);
61 
62 	/* Try with a small buffer first. */
63 	bufsize = 32;
64 
65 	/* Limit maximum buffer size to something reasonable so we don't loop forever. */
66 	while (bufsize <= 1024 * 1024) {
67 		buf = malloc(bufsize);
68 		if (buf == NULL) {
69 			va_end(args);
70 			return NULL;
71 		}
72 
73 		va_copy(args_copy, args);
74 		rc = vsnprintf(buf, bufsize, format, args_copy);
75 		va_end(args_copy);
76 
77 		/*
78 		 * If vsnprintf() returned a count within our current buffer size, we are done.
79 		 * The count does not include the \0 terminator, so rc == bufsize is not OK.
80 		 */
81 		if (rc >= 0 && (size_t)rc < bufsize) {
82 			va_end(args);
83 			return buf;
84 		}
85 
86 		/*
87 		 * vsnprintf() should return the required space, but some libc versions do not
88 		 * implement this correctly, so just double the buffer size and try again.
89 		 *
90 		 * We don't need the data in buf, so rather than realloc(), use free() and malloc()
91 		 * again to avoid a copy.
92 		 */
93 		free(buf);
94 		bufsize *= 2;
95 	}
96 
97 	va_end(args);
98 	return NULL;
99 }
100 
101 static void
102 spdk_env_unlink_shared_files(void)
103 {
104 	char buffer[PATH_MAX];
105 
106 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_config", getpid());
107 	if (unlink(buffer)) {
108 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
109 	}
110 
111 	snprintf(buffer, PATH_MAX, "/var/run/.spdk_pid%d_hugepage_info", getpid());
112 	if (unlink(buffer)) {
113 		fprintf(stderr, "Unable to unlink shared memory file: %s. Error code: %d\n", buffer, errno);
114 	}
115 }
116 
117 void
118 spdk_env_opts_init(struct spdk_env_opts *opts)
119 {
120 	if (!opts) {
121 		return;
122 	}
123 
124 	memset(opts, 0, sizeof(*opts));
125 
126 	opts->name = SPDK_ENV_DPDK_DEFAULT_NAME;
127 	opts->core_mask = SPDK_ENV_DPDK_DEFAULT_CORE_MASK;
128 	opts->shm_id = SPDK_ENV_DPDK_DEFAULT_SHM_ID;
129 	opts->mem_size = SPDK_ENV_DPDK_DEFAULT_MEM_SIZE;
130 	opts->master_core = SPDK_ENV_DPDK_DEFAULT_MASTER_CORE;
131 	opts->mem_channel = SPDK_ENV_DPDK_DEFAULT_MEM_CHANNEL;
132 }
133 
134 static void
135 spdk_free_args(char **args, int argcount)
136 {
137 	int i;
138 
139 	for (i = 0; i < argcount; i++) {
140 		free(args[i]);
141 	}
142 
143 	if (argcount) {
144 		free(args);
145 	}
146 }
147 
148 static char **
149 spdk_push_arg(char *args[], int *argcount, char *arg)
150 {
151 	char **tmp;
152 
153 	if (arg == NULL) {
154 		fprintf(stderr, "%s: NULL arg supplied\n", __func__);
155 		spdk_free_args(args, *argcount);
156 		return NULL;
157 	}
158 
159 	tmp = realloc(args, sizeof(char *) * (*argcount + 1));
160 	if (tmp == NULL) {
161 		spdk_free_args(args, *argcount);
162 		return NULL;
163 	}
164 
165 	tmp[*argcount] = arg;
166 	(*argcount)++;
167 
168 	return tmp;
169 }
170 
171 static void
172 spdk_destruct_eal_cmdline(void)
173 {
174 	spdk_free_args(eal_cmdline, eal_cmdline_argcount);
175 }
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 	/* set the coremask */
193 	/* NOTE: If coremask starts with '[' and ends with ']' it is a core list
194 	 */
195 	if (opts->core_mask[0] == '[') {
196 		char *l_arg = _sprintf_alloc("-l %s", opts->core_mask + 1);
197 		int len = strlen(l_arg);
198 		if (l_arg[len - 1] == ']') {
199 			l_arg[len - 1] = '\0';
200 		}
201 		args = spdk_push_arg(args, &argcount, l_arg);
202 	} else {
203 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-c %s", opts->core_mask));
204 	}
205 
206 	if (args == NULL) {
207 		return -1;
208 	}
209 
210 	/* set the memory channel number */
211 	if (opts->mem_channel > 0) {
212 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-n %d", opts->mem_channel));
213 		if (args == NULL) {
214 			return -1;
215 		}
216 	}
217 
218 	/* set the memory size */
219 	if (opts->mem_size > 0) {
220 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("-m %d", opts->mem_size));
221 		if (args == NULL) {
222 			return -1;
223 		}
224 	}
225 
226 	/* set the master core */
227 	if (opts->master_core > 0) {
228 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--master-lcore=%d",
229 				     opts->master_core));
230 		if (args == NULL) {
231 			return -1;
232 		}
233 	}
234 
235 	/* set no pci  if enabled */
236 	if (opts->no_pci) {
237 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--no-pci"));
238 		if (args == NULL) {
239 			return -1;
240 		}
241 	}
242 
243 #ifdef __linux__
244 	if (opts->shm_id < 0) {
245 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk_pid%d",
246 				     getpid()));
247 		if (args == NULL) {
248 			return -1;
249 		}
250 	} else {
251 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--file-prefix=spdk%d",
252 				     opts->shm_id));
253 		if (args == NULL) {
254 			return -1;
255 		}
256 
257 		/* set the base virtual address */
258 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--base-virtaddr=0x1000000000"));
259 		if (args == NULL) {
260 			return -1;
261 		}
262 
263 		/* set the process type */
264 		args = spdk_push_arg(args, &argcount, _sprintf_alloc("--proc-type=auto"));
265 		if (args == NULL) {
266 			return -1;
267 		}
268 	}
269 #endif
270 
271 	eal_cmdline = args;
272 	eal_cmdline_argcount = argcount;
273 	if (atexit(spdk_destruct_eal_cmdline) != 0) {
274 		fprintf(stderr, "Failed to register cleanup handler\n");
275 	}
276 
277 	return argcount;
278 }
279 
280 int spdk_env_init(const struct spdk_env_opts *opts)
281 {
282 	char **dpdk_args = NULL;
283 	int i, rc;
284 	int orig_optind;
285 
286 	rc = spdk_build_eal_cmdline(opts);
287 	if (rc < 0) {
288 		fprintf(stderr, "Invalid arguments to initialize DPDK\n");
289 		return -1;
290 	}
291 
292 	printf("Starting %s initialization...\n", rte_version());
293 	printf("[ DPDK EAL parameters: ");
294 	for (i = 0; i < eal_cmdline_argcount; i++) {
295 		printf("%s ", eal_cmdline[i]);
296 	}
297 	printf("]\n");
298 
299 	/* DPDK rearranges the array we pass to it, so make a copy
300 	 * before passing so we can still free the individual strings
301 	 * correctly.
302 	 */
303 	dpdk_args = calloc(eal_cmdline_argcount, sizeof(char *));
304 	if (dpdk_args == NULL) {
305 		fprintf(stderr, "Failed to allocate dpdk_args\n");
306 		return -1;
307 	}
308 	memcpy(dpdk_args, eal_cmdline, sizeof(char *) * eal_cmdline_argcount);
309 
310 	fflush(stdout);
311 	orig_optind = optind;
312 	optind = 1;
313 	rc = rte_eal_init(eal_cmdline_argcount, dpdk_args);
314 	optind = orig_optind;
315 
316 	free(dpdk_args);
317 
318 	if (rc < 0) {
319 		fprintf(stderr, "Failed to initialize DPDK\n");
320 		return -1;
321 	}
322 
323 	if (opts->shm_id < 0) {
324 		/*
325 		 * Unlink hugepage and config info files after init.  This will ensure they get
326 		 *  deleted on app exit, even if the app crashes and does not exit normally.
327 		 *  Only do this when not in multi-process mode, since for multi-process other
328 		 *  apps will need to open these files.
329 		 */
330 		spdk_env_unlink_shared_files();
331 	}
332 
333 	if (spdk_mem_map_init() < 0) {
334 		fprintf(stderr, "Failed to allocate mem_map\n");
335 		return -1;
336 	}
337 	if (spdk_vtophys_init() < 0) {
338 		fprintf(stderr, "Failed to initialize vtophys\n");
339 		return -1;
340 	}
341 
342 	return 0;
343 }
344