xref: /spdk/lib/event/app.c (revision 0be5557cad778ce88a8836b8595f94352eec0600)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. 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 #include "spdk/version.h"
36 
37 #include "spdk_internal/event.h"
38 
39 #include "spdk/env.h"
40 #include "spdk/log.h"
41 #include "spdk/conf.h"
42 #include "spdk/thread.h"
43 #include "spdk/trace.h"
44 #include "spdk/string.h"
45 #include "spdk/rpc.h"
46 #include "spdk/util.h"
47 
48 #define SPDK_APP_DEFAULT_LOG_LEVEL		SPDK_LOG_NOTICE
49 #define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL	SPDK_LOG_INFO
50 #define SPDK_APP_DEFAULT_BACKTRACE_LOG_LEVEL	SPDK_LOG_ERROR
51 #define SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES	SPDK_DEFAULT_NUM_TRACE_ENTRIES
52 
53 #define SPDK_APP_DPDK_DEFAULT_MEM_SIZE		-1
54 #define SPDK_APP_DPDK_DEFAULT_MASTER_CORE	-1
55 #define SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL	-1
56 #define SPDK_APP_DPDK_DEFAULT_CORE_MASK		"0x1"
57 #define SPDK_APP_DEFAULT_CORE_LIMIT		0x140000000 /* 5 GiB */
58 
59 struct spdk_app {
60 	struct spdk_conf		*config;
61 	const char			*json_config_file;
62 	bool				json_config_ignore_errors;
63 	const char			*rpc_addr;
64 	int				shm_id;
65 	spdk_app_shutdown_cb		shutdown_cb;
66 	int				rc;
67 };
68 
69 static struct spdk_app g_spdk_app;
70 static spdk_msg_fn g_start_fn = NULL;
71 static void *g_start_arg = NULL;
72 static struct spdk_thread *g_app_thread = NULL;
73 static bool g_delay_subsystem_init = false;
74 static bool g_shutdown_sig_received = false;
75 static char *g_executable_name;
76 static struct spdk_app_opts g_default_opts;
77 
78 int
79 spdk_app_get_shm_id(void)
80 {
81 	return g_spdk_app.shm_id;
82 }
83 
84 /* append one empty option to indicate the end of the array */
85 static const struct option g_cmdline_options[] = {
86 #define CONFIG_FILE_OPT_IDX	'c'
87 	{"config",			required_argument,	NULL, CONFIG_FILE_OPT_IDX},
88 #define LIMIT_COREDUMP_OPT_IDX 'd'
89 	{"limit-coredump",		no_argument,		NULL, LIMIT_COREDUMP_OPT_IDX},
90 #define TPOINT_GROUP_MASK_OPT_IDX 'e'
91 	{"tpoint-group-mask",		required_argument,	NULL, TPOINT_GROUP_MASK_OPT_IDX},
92 #define SINGLE_FILE_SEGMENTS_OPT_IDX 'g'
93 	{"single-file-segments",	no_argument,		NULL, SINGLE_FILE_SEGMENTS_OPT_IDX},
94 #define HELP_OPT_IDX		'h'
95 	{"help",			no_argument,		NULL, HELP_OPT_IDX},
96 #define SHM_ID_OPT_IDX		'i'
97 	{"shm-id",			required_argument,	NULL, SHM_ID_OPT_IDX},
98 #define CPUMASK_OPT_IDX		'm'
99 	{"cpumask",			required_argument,	NULL, CPUMASK_OPT_IDX},
100 #define MEM_CHANNELS_OPT_IDX	'n'
101 	{"mem-channels",		required_argument,	NULL, MEM_CHANNELS_OPT_IDX},
102 #define MASTER_CORE_OPT_IDX	'p'
103 	{"master-core",			required_argument,	NULL, MASTER_CORE_OPT_IDX},
104 #define RPC_SOCKET_OPT_IDX	'r'
105 	{"rpc-socket",			required_argument,	NULL, RPC_SOCKET_OPT_IDX},
106 #define MEM_SIZE_OPT_IDX	's'
107 	{"mem-size",			required_argument,	NULL, MEM_SIZE_OPT_IDX},
108 #define NO_PCI_OPT_IDX		'u'
109 	{"no-pci",			no_argument,		NULL, NO_PCI_OPT_IDX},
110 #define VERSION_OPT_IDX		'v'
111 	{"version",			no_argument,		NULL, VERSION_OPT_IDX},
112 #define PCI_BLACKLIST_OPT_IDX	'B'
113 	{"pci-blacklist",		required_argument,	NULL, PCI_BLACKLIST_OPT_IDX},
114 #define LOGFLAG_OPT_IDX	'L'
115 	{"logflag",			required_argument,	NULL, LOGFLAG_OPT_IDX},
116 #define HUGE_UNLINK_OPT_IDX	'R'
117 	{"huge-unlink",			no_argument,		NULL, HUGE_UNLINK_OPT_IDX},
118 #define PCI_WHITELIST_OPT_IDX	'W'
119 	{"pci-whitelist",		required_argument,	NULL, PCI_WHITELIST_OPT_IDX},
120 #define SILENCE_NOTICELOG_OPT_IDX 257
121 	{"silence-noticelog",		no_argument,		NULL, SILENCE_NOTICELOG_OPT_IDX},
122 #define WAIT_FOR_RPC_OPT_IDX	258
123 	{"wait-for-rpc",		no_argument,		NULL, WAIT_FOR_RPC_OPT_IDX},
124 #define HUGE_DIR_OPT_IDX	259
125 	{"huge-dir",			required_argument,	NULL, HUGE_DIR_OPT_IDX},
126 #define NUM_TRACE_ENTRIES_OPT_IDX	260
127 	{"num-trace-entries",		required_argument,	NULL, NUM_TRACE_ENTRIES_OPT_IDX},
128 #define MAX_REACTOR_DELAY_OPT_IDX	261
129 	{"max-delay",			required_argument,	NULL, MAX_REACTOR_DELAY_OPT_IDX},
130 #define JSON_CONFIG_OPT_IDX		262
131 	{"json",			required_argument,	NULL, JSON_CONFIG_OPT_IDX},
132 #define JSON_CONFIG_IGNORE_INIT_ERRORS_IDX	263
133 	{"json-ignore-init-errors",	no_argument,		NULL, JSON_CONFIG_IGNORE_INIT_ERRORS_IDX},
134 };
135 
136 /* Global section */
137 #define GLOBAL_CONFIG_TMPL \
138 "# Configuration file\n" \
139 "#\n" \
140 "# Please write all parameters using ASCII.\n" \
141 "# The parameter must be quoted if it includes whitespace.\n" \
142 "#\n" \
143 "# Configuration syntax:\n" \
144 "# Spaces at head of line are deleted, other spaces are as separator\n" \
145 "# Lines starting with '#' are comments and not evaluated.\n" \
146 "# Lines ending with '\\' are concatenated with the next line.\n" \
147 "# Bracketed keys are section keys grouping the following value keys.\n" \
148 "# Number of section key is used as a tag number.\n" \
149 "#  Ex. [TargetNode1] = TargetNode section key with tag number 1\n" \
150 "[Global]\n" \
151 "  Comment \"Global section\"\n" \
152 "\n" \
153 "  # Users can restrict work items to only run on certain cores by\n" \
154 "  #  specifying a ReactorMask.  Default is to allow work items to run\n" \
155 "  #  on all cores.  Core 0 must be set in the mask if one is specified.\n" \
156 "  # Default: 0xFFFF (cores 0-15)\n" \
157 "  ReactorMask \"0x%s\"\n" \
158 "\n" \
159 "  # Tracepoint group mask for spdk trace buffers\n" \
160 "  # Default: 0x0 (all tracepoint groups disabled)\n" \
161 "  # Set to 0xFFFF to enable all tracepoint groups.\n" \
162 "  TpointGroupMask \"0x%" PRIX64 "\"\n" \
163 "\n" \
164 
165 static void
166 app_config_dump_global_section(FILE *fp)
167 {
168 	struct spdk_cpuset *coremask;
169 
170 	if (NULL == fp) {
171 		return;
172 	}
173 
174 	coremask = spdk_app_get_core_mask();
175 
176 	fprintf(fp, GLOBAL_CONFIG_TMPL, spdk_cpuset_fmt(coremask),
177 		spdk_trace_get_tpoint_group_mask());
178 }
179 
180 int
181 spdk_app_get_running_config(char **config_str, char *name)
182 {
183 	FILE *fp = NULL;
184 	int fd = -1;
185 	long length = 0, ret = 0;
186 	char vbuf[BUFSIZ];
187 	char config_template[64];
188 
189 	snprintf(config_template, sizeof(config_template), "/tmp/%s.XXXXXX", name);
190 	/* Create temporary file to hold config */
191 	fd = mkstemp(config_template);
192 	if (fd == -1) {
193 		SPDK_ERRLOG("mkstemp failed\n");
194 		return -1;
195 	}
196 	fp = fdopen(fd, "wb+");
197 	if (NULL == fp) {
198 		SPDK_ERRLOG("error opening tmpfile fd = %d\n", fd);
199 		return -1;
200 	}
201 
202 	/* Buffered IO */
203 	setvbuf(fp, vbuf, _IOFBF, BUFSIZ);
204 
205 	app_config_dump_global_section(fp);
206 	spdk_subsystem_config(fp);
207 
208 	length = ftell(fp);
209 
210 	*config_str = malloc(length + 1);
211 	if (!*config_str) {
212 		SPDK_ERRLOG("out-of-memory for config\n");
213 		fclose(fp);
214 		return -1;
215 	}
216 	fseek(fp, 0, SEEK_SET);
217 	ret = fread(*config_str, sizeof(char), length, fp);
218 	if (ret < length) {
219 		SPDK_ERRLOG("short read\n");
220 	}
221 	fclose(fp);
222 	(*config_str)[length] = '\0';
223 
224 	return 0;
225 }
226 
227 static void
228 app_start_shutdown(void *ctx)
229 {
230 	if (g_spdk_app.shutdown_cb) {
231 		g_spdk_app.shutdown_cb();
232 		g_spdk_app.shutdown_cb = NULL;
233 	} else {
234 		spdk_app_stop(0);
235 	}
236 }
237 
238 void
239 spdk_app_start_shutdown(void)
240 {
241 	spdk_thread_send_critical_msg(g_app_thread, app_start_shutdown);
242 }
243 
244 static void
245 __shutdown_signal(int signo)
246 {
247 	if (!g_shutdown_sig_received) {
248 		g_shutdown_sig_received = true;
249 		spdk_app_start_shutdown();
250 	}
251 }
252 
253 static int
254 app_opts_validate(const char *app_opts)
255 {
256 	int i = 0, j;
257 
258 	for (i = 0; app_opts[i] != '\0'; i++) {
259 		/* ignore getopt control characters */
260 		if (app_opts[i] == ':' || app_opts[i] == '+' || app_opts[i] == '-') {
261 			continue;
262 		}
263 
264 		for (j = 0; SPDK_APP_GETOPT_STRING[j] != '\0'; j++) {
265 			if (app_opts[i] == SPDK_APP_GETOPT_STRING[j]) {
266 				return app_opts[i];
267 			}
268 		}
269 	}
270 	return 0;
271 }
272 
273 void
274 spdk_app_opts_init(struct spdk_app_opts *opts)
275 {
276 	if (!opts) {
277 		return;
278 	}
279 
280 	memset(opts, 0, sizeof(*opts));
281 
282 	opts->enable_coredump = true;
283 	opts->shm_id = -1;
284 	opts->mem_size = SPDK_APP_DPDK_DEFAULT_MEM_SIZE;
285 	opts->master_core = SPDK_APP_DPDK_DEFAULT_MASTER_CORE;
286 	opts->mem_channel = SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL;
287 	opts->reactor_mask = NULL;
288 	opts->print_level = SPDK_APP_DEFAULT_LOG_PRINT_LEVEL;
289 	opts->rpc_addr = SPDK_DEFAULT_RPC_ADDR;
290 	opts->num_entries = SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES;
291 	opts->delay_subsystem_init = false;
292 }
293 
294 static int
295 app_setup_signal_handlers(struct spdk_app_opts *opts)
296 {
297 	struct sigaction	sigact;
298 	sigset_t		sigmask;
299 	int			rc;
300 
301 	sigemptyset(&sigmask);
302 	memset(&sigact, 0, sizeof(sigact));
303 	sigemptyset(&sigact.sa_mask);
304 
305 	sigact.sa_handler = SIG_IGN;
306 	rc = sigaction(SIGPIPE, &sigact, NULL);
307 	if (rc < 0) {
308 		SPDK_ERRLOG("sigaction(SIGPIPE) failed\n");
309 		return rc;
310 	}
311 
312 	/* Install the same handler for SIGINT and SIGTERM */
313 	sigact.sa_handler = __shutdown_signal;
314 
315 	rc = sigaction(SIGINT, &sigact, NULL);
316 	if (rc < 0) {
317 		SPDK_ERRLOG("sigaction(SIGINT) failed\n");
318 		return rc;
319 	}
320 	sigaddset(&sigmask, SIGINT);
321 
322 	rc = sigaction(SIGTERM, &sigact, NULL);
323 	if (rc < 0) {
324 		SPDK_ERRLOG("sigaction(SIGTERM) failed\n");
325 		return rc;
326 	}
327 	sigaddset(&sigmask, SIGTERM);
328 
329 	if (opts->usr1_handler != NULL) {
330 		sigact.sa_handler = opts->usr1_handler;
331 		rc = sigaction(SIGUSR1, &sigact, NULL);
332 		if (rc < 0) {
333 			SPDK_ERRLOG("sigaction(SIGUSR1) failed\n");
334 			return rc;
335 		}
336 		sigaddset(&sigmask, SIGUSR1);
337 	}
338 
339 	pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
340 
341 	return 0;
342 }
343 
344 static void
345 app_start_application(void)
346 {
347 	assert(spdk_get_thread() == g_app_thread);
348 
349 	g_start_fn(g_start_arg);
350 }
351 
352 static void
353 app_start_rpc(int rc, void *arg1)
354 {
355 	if (rc) {
356 		spdk_app_stop(rc);
357 		return;
358 	}
359 
360 	spdk_rpc_initialize(g_spdk_app.rpc_addr);
361 	if (!g_delay_subsystem_init) {
362 		spdk_rpc_set_state(SPDK_RPC_RUNTIME);
363 		app_start_application();
364 	}
365 }
366 
367 static struct spdk_conf *
368 app_setup_conf(const char *config_file)
369 {
370 	struct spdk_conf *config;
371 	int rc;
372 
373 	config = spdk_conf_allocate();
374 	assert(config != NULL);
375 	if (config_file) {
376 		rc = spdk_conf_read(config, config_file);
377 		if (rc != 0) {
378 			SPDK_ERRLOG("Could not read config file %s\n", config_file);
379 			goto error;
380 		}
381 		if (spdk_conf_first_section(config) == NULL) {
382 			SPDK_ERRLOG("Invalid config file %s\n", config_file);
383 			goto error;
384 		}
385 	}
386 	spdk_conf_set_as_default(config);
387 	return config;
388 
389 error:
390 	spdk_conf_free(config);
391 	return NULL;
392 }
393 
394 static int
395 app_opts_add_pci_addr(struct spdk_app_opts *opts, struct spdk_pci_addr **list, char *bdf)
396 {
397 	struct spdk_pci_addr *tmp = *list;
398 	size_t i = opts->num_pci_addr;
399 
400 	tmp = realloc(tmp, sizeof(*tmp) * (i + 1));
401 	if (tmp == NULL) {
402 		SPDK_ERRLOG("realloc error\n");
403 		return -ENOMEM;
404 	}
405 
406 	*list = tmp;
407 	if (spdk_pci_addr_parse(*list + i, bdf) < 0) {
408 		SPDK_ERRLOG("Invalid address %s\n", bdf);
409 		return -EINVAL;
410 	}
411 
412 	opts->num_pci_addr++;
413 	return 0;
414 }
415 
416 static int
417 app_read_config_file_global_params(struct spdk_app_opts *opts)
418 {
419 	struct spdk_conf_section *sp;
420 	char *bdf;
421 	int i, rc = 0;
422 
423 	sp = spdk_conf_find_section(NULL, "Global");
424 
425 	if (opts->shm_id == -1) {
426 		if (sp != NULL) {
427 			opts->shm_id = spdk_conf_section_get_intval(sp, "SharedMemoryID");
428 		}
429 	}
430 
431 	if (opts->reactor_mask == NULL) {
432 		if (sp && spdk_conf_section_get_val(sp, "ReactorMask")) {
433 			SPDK_ERRLOG("ReactorMask config option is deprecated.  Use -m/--cpumask\n"
434 				    "command line parameter instead.\n");
435 			opts->reactor_mask = spdk_conf_section_get_val(sp, "ReactorMask");
436 		} else {
437 			opts->reactor_mask = SPDK_APP_DPDK_DEFAULT_CORE_MASK;
438 		}
439 	}
440 
441 	if (!opts->no_pci && sp) {
442 		opts->no_pci = spdk_conf_section_get_boolval(sp, "NoPci", false);
443 	}
444 
445 	if (opts->tpoint_group_mask == NULL) {
446 		if (sp != NULL) {
447 			opts->tpoint_group_mask = spdk_conf_section_get_val(sp, "TpointGroupMask");
448 		}
449 	}
450 
451 	if (sp == NULL) {
452 		return 0;
453 	}
454 
455 	for (i = 0; ; i++) {
456 		bdf = spdk_conf_section_get_nmval(sp, "PciBlacklist", i, 0);
457 		if (!bdf) {
458 			break;
459 		}
460 
461 		rc = app_opts_add_pci_addr(opts, &opts->pci_blacklist, bdf);
462 		if (rc != 0) {
463 			free(opts->pci_blacklist);
464 			return rc;
465 		}
466 	}
467 
468 	for (i = 0; ; i++) {
469 		bdf = spdk_conf_section_get_nmval(sp, "PciWhitelist", i, 0);
470 		if (!bdf) {
471 			break;
472 		}
473 
474 		if (opts->pci_blacklist != NULL) {
475 			SPDK_ERRLOG("PciBlacklist and PciWhitelist cannot be used at the same time\n");
476 			free(opts->pci_blacklist);
477 			return -EINVAL;
478 		}
479 
480 		rc = app_opts_add_pci_addr(opts, &opts->pci_whitelist, bdf);
481 		if (rc != 0) {
482 			free(opts->pci_whitelist);
483 			return rc;
484 		}
485 	}
486 	return 0;
487 }
488 
489 static int
490 app_setup_env(struct spdk_app_opts *opts)
491 {
492 	struct spdk_env_opts env_opts = {};
493 	int rc;
494 
495 	spdk_env_opts_init(&env_opts);
496 
497 	env_opts.name = opts->name;
498 	env_opts.core_mask = opts->reactor_mask;
499 	env_opts.shm_id = opts->shm_id;
500 	env_opts.mem_channel = opts->mem_channel;
501 	env_opts.master_core = opts->master_core;
502 	env_opts.mem_size = opts->mem_size;
503 	env_opts.hugepage_single_segments = opts->hugepage_single_segments;
504 	env_opts.unlink_hugepage = opts->unlink_hugepage;
505 	env_opts.hugedir = opts->hugedir;
506 	env_opts.no_pci = opts->no_pci;
507 	env_opts.num_pci_addr = opts->num_pci_addr;
508 	env_opts.pci_blacklist = opts->pci_blacklist;
509 	env_opts.pci_whitelist = opts->pci_whitelist;
510 	env_opts.env_context = opts->env_context;
511 
512 	rc = spdk_env_init(&env_opts);
513 	free(env_opts.pci_blacklist);
514 	free(env_opts.pci_whitelist);
515 
516 	if (rc < 0) {
517 		SPDK_ERRLOG("Unable to initialize SPDK env\n");
518 	}
519 
520 	return rc;
521 }
522 
523 static int
524 app_setup_trace(struct spdk_app_opts *opts)
525 {
526 	char		shm_name[64];
527 	uint64_t	tpoint_group_mask;
528 	char		*end;
529 
530 	if (opts->shm_id >= 0) {
531 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", opts->name, opts->shm_id);
532 	} else {
533 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", opts->name, (int)getpid());
534 	}
535 
536 	if (spdk_trace_init(shm_name, opts->num_entries) != 0) {
537 		return -1;
538 	}
539 
540 	if (opts->tpoint_group_mask != NULL) {
541 		errno = 0;
542 		tpoint_group_mask = strtoull(opts->tpoint_group_mask, &end, 16);
543 		if (*end != '\0' || errno) {
544 			SPDK_ERRLOG("invalid tpoint mask %s\n", opts->tpoint_group_mask);
545 		} else {
546 			SPDK_NOTICELOG("Tracepoint Group Mask %s specified.\n", opts->tpoint_group_mask);
547 			SPDK_NOTICELOG("Use 'spdk_trace -s %s %s %d' to capture a snapshot of events at runtime.\n",
548 				       opts->name,
549 				       opts->shm_id >= 0 ? "-i" : "-p",
550 				       opts->shm_id >= 0 ? opts->shm_id : getpid());
551 #if defined(__linux__)
552 			SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name);
553 #endif
554 			spdk_trace_set_tpoint_group_mask(tpoint_group_mask);
555 		}
556 	}
557 
558 	return 0;
559 }
560 
561 static void
562 bootstrap_fn(void *arg1)
563 {
564 	if (g_spdk_app.json_config_file) {
565 		g_delay_subsystem_init = false;
566 		spdk_app_json_config_load(g_spdk_app.json_config_file, g_spdk_app.rpc_addr, app_start_rpc,
567 					  NULL, !g_spdk_app.json_config_ignore_errors);
568 	} else {
569 		if (!g_delay_subsystem_init) {
570 			spdk_subsystem_init(app_start_rpc, NULL);
571 		} else {
572 			spdk_rpc_initialize(g_spdk_app.rpc_addr);
573 		}
574 	}
575 }
576 
577 int
578 spdk_app_start(struct spdk_app_opts *opts, spdk_msg_fn start_fn,
579 	       void *arg1)
580 {
581 	struct spdk_conf	*config = NULL;
582 	int			rc;
583 	char			*tty;
584 	struct spdk_cpuset	tmp_cpumask = {};
585 
586 	if (!opts) {
587 		SPDK_ERRLOG("opts should not be NULL\n");
588 		return 1;
589 	}
590 
591 	if (!start_fn) {
592 		SPDK_ERRLOG("start_fn should not be NULL\n");
593 		return 1;
594 	}
595 
596 	tty = ttyname(STDERR_FILENO);
597 	if (opts->print_level > SPDK_LOG_WARN &&
598 	    isatty(STDERR_FILENO) &&
599 	    tty &&
600 	    !strncmp(tty, "/dev/tty", strlen("/dev/tty"))) {
601 		printf("Warning: printing stderr to console terminal without -q option specified.\n");
602 		printf("Suggest using --silence-noticelog to disable logging to stderr and\n");
603 		printf("monitor syslog, or redirect stderr to a file.\n");
604 		printf("(Delaying for 10 seconds...)\n");
605 		sleep(10);
606 	}
607 
608 	spdk_log_set_print_level(opts->print_level);
609 
610 #ifndef SPDK_NO_RLIMIT
611 	if (opts->enable_coredump) {
612 		struct rlimit core_limits;
613 
614 		core_limits.rlim_cur = core_limits.rlim_max = SPDK_APP_DEFAULT_CORE_LIMIT;
615 		setrlimit(RLIMIT_CORE, &core_limits);
616 	}
617 #endif
618 
619 	config = app_setup_conf(opts->config_file);
620 	if (config == NULL) {
621 		return 1;
622 	}
623 
624 	if (app_read_config_file_global_params(opts) < 0) {
625 		spdk_conf_free(config);
626 		return 1;
627 	}
628 
629 	memset(&g_spdk_app, 0, sizeof(g_spdk_app));
630 	g_spdk_app.config = config;
631 	g_spdk_app.json_config_file = opts->json_config_file;
632 	g_spdk_app.json_config_ignore_errors = opts->json_config_ignore_errors;
633 	g_spdk_app.rpc_addr = opts->rpc_addr;
634 	g_spdk_app.shm_id = opts->shm_id;
635 	g_spdk_app.shutdown_cb = opts->shutdown_cb;
636 	g_spdk_app.rc = 0;
637 
638 	spdk_log_set_level(SPDK_APP_DEFAULT_LOG_LEVEL);
639 	spdk_log_set_backtrace_level(SPDK_APP_DEFAULT_BACKTRACE_LOG_LEVEL);
640 
641 	if (app_setup_env(opts) < 0) {
642 		return 1;
643 	}
644 
645 	spdk_log_open(opts->log);
646 	SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count());
647 
648 	/*
649 	 * If mask not specified on command line or in configuration file,
650 	 *  reactor_mask will be 0x1 which will enable core 0 to run one
651 	 *  reactor.
652 	 */
653 	if ((rc = spdk_reactors_init()) != 0) {
654 		SPDK_ERRLOG("Reactor Initilization failed: rc = %d\n", rc);
655 		return 1;
656 	}
657 
658 	spdk_cpuset_set_cpu(&tmp_cpumask, spdk_env_get_current_core(), true);
659 
660 	/* Now that the reactors have been initialized, we can create an
661 	 * initialization thread. */
662 	g_app_thread = spdk_thread_create("app_thread", &tmp_cpumask);
663 	if (!g_app_thread) {
664 		SPDK_ERRLOG("Unable to create an spdk_thread for initialization\n");
665 		return 1;
666 	}
667 
668 	/*
669 	 * Note the call to app_setup_trace() is located here
670 	 * ahead of app_setup_signal_handlers().
671 	 * That's because there is not an easy/direct clean
672 	 * way of unwinding alloc'd resources that can occur
673 	 * in app_setup_signal_handlers().
674 	 */
675 	if (app_setup_trace(opts) != 0) {
676 		return 1;
677 	}
678 
679 	if ((rc = app_setup_signal_handlers(opts)) != 0) {
680 		return 1;
681 	}
682 
683 	g_delay_subsystem_init = opts->delay_subsystem_init;
684 	g_start_fn = start_fn;
685 	g_start_arg = arg1;
686 
687 	spdk_thread_send_msg(g_app_thread, bootstrap_fn, NULL);
688 
689 	/* This blocks until spdk_app_stop is called */
690 	spdk_reactors_start();
691 
692 	return g_spdk_app.rc;
693 }
694 
695 void
696 spdk_app_fini(void)
697 {
698 	spdk_trace_cleanup();
699 	spdk_reactors_fini();
700 	spdk_env_fini();
701 	spdk_conf_free(g_spdk_app.config);
702 	spdk_log_close();
703 }
704 
705 static void
706 _spdk_app_stop(void *arg1)
707 {
708 	spdk_rpc_finish();
709 	spdk_subsystem_fini(spdk_reactors_stop, NULL);
710 }
711 
712 void
713 spdk_app_stop(int rc)
714 {
715 	if (rc) {
716 		SPDK_WARNLOG("spdk_app_stop'd on non-zero\n");
717 	}
718 	g_spdk_app.rc = rc;
719 	/*
720 	 * We want to run spdk_subsystem_fini() from the same thread where spdk_subsystem_init()
721 	 * was called.
722 	 */
723 	spdk_thread_send_msg(g_app_thread, _spdk_app_stop, NULL);
724 }
725 
726 static void
727 usage(void (*app_usage)(void))
728 {
729 	printf("%s [options]\n", g_executable_name);
730 	printf("options:\n");
731 	printf(" -c, --config <config>     config file (default %s)\n",
732 	       g_default_opts.config_file != NULL ? g_default_opts.config_file : "none");
733 	printf("     --json <config>       JSON config file (default %s)\n",
734 	       g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none");
735 	printf("     --json-ignore-init-errors\n");
736 	printf("                           don't exit on invalid config entry\n");
737 	printf(" -d, --limit-coredump      do not set max coredump size to RLIM_INFINITY\n");
738 	printf(" -g, --single-file-segments\n");
739 	printf("                           force creating just one hugetlbfs file\n");
740 	printf(" -h, --help                show this usage\n");
741 	printf(" -i, --shm-id <id>         shared memory ID (optional)\n");
742 	printf(" -m, --cpumask <mask>      core mask for DPDK\n");
743 	printf(" -n, --mem-channels <num>  channel number of memory channels used for DPDK\n");
744 	printf(" -p, --master-core <id>    master (primary) core for DPDK\n");
745 	printf(" -r, --rpc-socket <path>   RPC listen address (default %s)\n", SPDK_DEFAULT_RPC_ADDR);
746 	printf(" -s, --mem-size <size>     memory size in MB for DPDK (default: ");
747 #ifndef __linux__
748 	if (g_default_opts.mem_size <= 0) {
749 		printf("all hugepage memory)\n");
750 	} else
751 #endif
752 	{
753 		printf("%dMB)\n", g_default_opts.mem_size >= 0 ? g_default_opts.mem_size : 0);
754 	}
755 	printf("     --silence-noticelog   disable notice level logging to stderr\n");
756 	printf(" -u, --no-pci              disable PCI access\n");
757 	printf("     --wait-for-rpc        wait for RPCs to initialize subsystems\n");
758 	printf("     --max-delay <num>     maximum reactor delay (in microseconds)\n");
759 	printf(" -B, --pci-blacklist <bdf>\n");
760 	printf("                           pci addr to blacklist (can be used more than once)\n");
761 	printf(" -R, --huge-unlink         unlink huge files after initialization\n");
762 	printf(" -v, --version             print SPDK version\n");
763 	printf(" -W, --pci-whitelist <bdf>\n");
764 	printf("                           pci addr to whitelist (-B and -W cannot be used at the same time)\n");
765 	printf("      --huge-dir <path>    use a specific hugetlbfs mount to reserve memory from\n");
766 	printf("      --num-trace-entries <num>   number of trace entries for each core, must be power of 2. (default %d)\n",
767 	       SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
768 	spdk_log_usage(stdout, "-L");
769 	spdk_trace_mask_usage(stdout, "-e");
770 	if (app_usage) {
771 		app_usage();
772 	}
773 }
774 
775 spdk_app_parse_args_rvals_t
776 spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
777 		    const char *app_getopt_str, struct option *app_long_opts,
778 		    int (*app_parse)(int ch, char *arg),
779 		    void (*app_usage)(void))
780 {
781 	int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len;
782 	struct option *cmdline_options;
783 	char *cmdline_short_opts = NULL;
784 	enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL;
785 	long int tmp;
786 
787 	memcpy(&g_default_opts, opts, sizeof(g_default_opts));
788 
789 	if (opts->config_file && access(opts->config_file, R_OK) != 0) {
790 		SPDK_WARNLOG("Can't read legacy configuration file '%s'\n", opts->config_file);
791 		opts->config_file = NULL;
792 	}
793 
794 	if (opts->json_config_file && access(opts->json_config_file, R_OK) != 0) {
795 		SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->json_config_file);
796 		opts->json_config_file = NULL;
797 	}
798 
799 	if (app_long_opts == NULL) {
800 		app_long_opts_len = 0;
801 	} else {
802 		for (app_long_opts_len = 0;
803 		     app_long_opts[app_long_opts_len].name != NULL;
804 		     app_long_opts_len++);
805 	}
806 
807 	global_long_opts_len = SPDK_COUNTOF(g_cmdline_options);
808 
809 	cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options));
810 	if (!cmdline_options) {
811 		SPDK_ERRLOG("Out of memory\n");
812 		return SPDK_APP_PARSE_ARGS_FAIL;
813 	}
814 
815 	memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options));
816 	if (app_long_opts) {
817 		memcpy(&cmdline_options[global_long_opts_len], app_long_opts,
818 		       app_long_opts_len * sizeof(*app_long_opts));
819 	}
820 
821 	if (app_getopt_str != NULL) {
822 		ch = app_opts_validate(app_getopt_str);
823 		if (ch) {
824 			SPDK_ERRLOG("Duplicated option '%c' between the generic and application specific spdk opts.\n",
825 				    ch);
826 			goto out;
827 		}
828 	}
829 
830 	cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
831 	if (!cmdline_short_opts) {
832 		SPDK_ERRLOG("Out of memory\n");
833 		goto out;
834 	}
835 
836 	g_executable_name = argv[0];
837 
838 	while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) {
839 		switch (ch) {
840 		case CONFIG_FILE_OPT_IDX:
841 			opts->config_file = optarg;
842 			break;
843 		case JSON_CONFIG_OPT_IDX:
844 			opts->json_config_file = optarg;
845 			break;
846 		case JSON_CONFIG_IGNORE_INIT_ERRORS_IDX:
847 			opts->json_config_ignore_errors = true;
848 			break;
849 		case LIMIT_COREDUMP_OPT_IDX:
850 			opts->enable_coredump = false;
851 			break;
852 		case TPOINT_GROUP_MASK_OPT_IDX:
853 			opts->tpoint_group_mask = optarg;
854 			break;
855 		case SINGLE_FILE_SEGMENTS_OPT_IDX:
856 			opts->hugepage_single_segments = true;
857 			break;
858 		case HELP_OPT_IDX:
859 			usage(app_usage);
860 			retval = SPDK_APP_PARSE_ARGS_HELP;
861 			goto out;
862 		case SHM_ID_OPT_IDX:
863 			opts->shm_id = spdk_strtol(optarg, 0);
864 			if (opts->shm_id < 0) {
865 				SPDK_ERRLOG("Invalid shared memory ID %s\n", optarg);
866 				goto out;
867 			}
868 			break;
869 		case CPUMASK_OPT_IDX:
870 			opts->reactor_mask = optarg;
871 			break;
872 		case MEM_CHANNELS_OPT_IDX:
873 			opts->mem_channel = spdk_strtol(optarg, 0);
874 			if (opts->mem_channel < 0) {
875 				SPDK_ERRLOG("Invalid memory channel %s\n", optarg);
876 				goto out;
877 			}
878 			break;
879 		case MASTER_CORE_OPT_IDX:
880 			opts->master_core = spdk_strtol(optarg, 0);
881 			if (opts->master_core < 0) {
882 				SPDK_ERRLOG("Invalid master core %s\n", optarg);
883 				goto out;
884 			}
885 			break;
886 		case SILENCE_NOTICELOG_OPT_IDX:
887 			opts->print_level = SPDK_LOG_WARN;
888 			break;
889 		case RPC_SOCKET_OPT_IDX:
890 			opts->rpc_addr = optarg;
891 			break;
892 		case MEM_SIZE_OPT_IDX: {
893 			uint64_t mem_size_mb;
894 			bool mem_size_has_prefix;
895 
896 			rc = spdk_parse_capacity(optarg, &mem_size_mb, &mem_size_has_prefix);
897 			if (rc != 0) {
898 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
899 				usage(app_usage);
900 				goto out;
901 			}
902 
903 			if (mem_size_has_prefix) {
904 				/* the mem size is in MB by default, so if a prefix was
905 				 * specified, we need to manually convert to MB.
906 				 */
907 				mem_size_mb /= 1024 * 1024;
908 			}
909 
910 			if (mem_size_mb > INT_MAX) {
911 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
912 				usage(app_usage);
913 				goto out;
914 			}
915 
916 			opts->mem_size = (int) mem_size_mb;
917 			break;
918 		}
919 		case NO_PCI_OPT_IDX:
920 			opts->no_pci = true;
921 			break;
922 		case WAIT_FOR_RPC_OPT_IDX:
923 			opts->delay_subsystem_init = true;
924 			break;
925 		case PCI_BLACKLIST_OPT_IDX:
926 			if (opts->pci_whitelist) {
927 				free(opts->pci_whitelist);
928 				opts->pci_whitelist = NULL;
929 				SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
930 				usage(app_usage);
931 				goto out;
932 			}
933 
934 			rc = app_opts_add_pci_addr(opts, &opts->pci_blacklist, optarg);
935 			if (rc != 0) {
936 				free(opts->pci_blacklist);
937 				opts->pci_blacklist = NULL;
938 				goto out;
939 			}
940 			break;
941 		case LOGFLAG_OPT_IDX:
942 #ifndef DEBUG
943 			SPDK_ERRLOG("%s must be configured with --enable-debug for -L flag\n",
944 				    argv[0]);
945 			usage(app_usage);
946 			goto out;
947 #else
948 			rc = spdk_log_set_flag(optarg);
949 			if (rc < 0) {
950 				SPDK_ERRLOG("unknown flag\n");
951 				usage(app_usage);
952 				goto out;
953 			}
954 			opts->print_level = SPDK_LOG_DEBUG;
955 			break;
956 #endif
957 		case HUGE_UNLINK_OPT_IDX:
958 			opts->unlink_hugepage = true;
959 			break;
960 		case PCI_WHITELIST_OPT_IDX:
961 			if (opts->pci_blacklist) {
962 				free(opts->pci_blacklist);
963 				opts->pci_blacklist = NULL;
964 				SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
965 				usage(app_usage);
966 				goto out;
967 			}
968 
969 			rc = app_opts_add_pci_addr(opts, &opts->pci_whitelist, optarg);
970 			if (rc != 0) {
971 				free(opts->pci_whitelist);
972 				opts->pci_whitelist = NULL;
973 				goto out;
974 			}
975 			break;
976 		case HUGE_DIR_OPT_IDX:
977 			opts->hugedir = optarg;
978 			break;
979 		case NUM_TRACE_ENTRIES_OPT_IDX:
980 			tmp = spdk_strtoll(optarg, 0);
981 			if (tmp <= 0) {
982 				SPDK_ERRLOG("Invalid num-trace-entries %s\n", optarg);
983 				usage(app_usage);
984 				goto out;
985 			}
986 			opts->num_entries = (uint64_t)tmp;
987 			if (opts->num_entries & (opts->num_entries - 1)) {
988 				SPDK_ERRLOG("num-trace-entries must be power of 2\n");
989 				usage(app_usage);
990 				goto out;
991 			}
992 			break;
993 		case MAX_REACTOR_DELAY_OPT_IDX:
994 			SPDK_ERRLOG("Deprecation warning: The maximum allowed latency parameter is no longer supported.\n");
995 			break;
996 		case VERSION_OPT_IDX:
997 			printf(SPDK_VERSION_STRING"\n");
998 			retval = SPDK_APP_PARSE_ARGS_HELP;
999 			goto out;
1000 		case '?':
1001 			/*
1002 			 * In the event getopt() above detects an option
1003 			 * in argv that is NOT in the getopt_str,
1004 			 * getopt() will return a '?' indicating failure.
1005 			 */
1006 			usage(app_usage);
1007 			goto out;
1008 		default:
1009 			rc = app_parse(ch, optarg);
1010 			if (rc) {
1011 				SPDK_ERRLOG("Parsing application specific arguments failed: %d\n", rc);
1012 				goto out;
1013 			}
1014 		}
1015 	}
1016 
1017 	if (opts->config_file && opts->json_config_file) {
1018 		SPDK_ERRLOG("ERROR: Legacy config and JSON config can't be used together.\n");
1019 		goto out;
1020 	}
1021 
1022 	if (opts->json_config_file && opts->delay_subsystem_init) {
1023 		SPDK_ERRLOG("ERROR: JSON configuration file can't be used together with --wait-for-rpc.\n");
1024 		goto out;
1025 	}
1026 
1027 	/* TBD: Replace warning by failure when RPCs for startup are prepared. */
1028 	if (opts->config_file && opts->delay_subsystem_init) {
1029 		fprintf(stderr,
1030 			"WARNING: --wait-for-rpc and config file are used at the same time. "
1031 			"- Please be careful one options might overwrite others.\n");
1032 	}
1033 
1034 	retval = SPDK_APP_PARSE_ARGS_SUCCESS;
1035 out:
1036 	if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) {
1037 		free(opts->pci_blacklist);
1038 		opts->pci_blacklist = NULL;
1039 		free(opts->pci_whitelist);
1040 		opts->pci_whitelist = NULL;
1041 	}
1042 	free(cmdline_short_opts);
1043 	free(cmdline_options);
1044 	return retval;
1045 }
1046 
1047 void
1048 spdk_app_usage(void)
1049 {
1050 	if (g_executable_name == NULL) {
1051 		SPDK_ERRLOG("%s not valid before calling spdk_app_parse_args()\n", __func__);
1052 		return;
1053 	}
1054 
1055 	usage(NULL);
1056 }
1057 
1058 static void
1059 rpc_framework_start_init_cpl(int rc, void *arg1)
1060 {
1061 	struct spdk_jsonrpc_request *request = arg1;
1062 	struct spdk_json_write_ctx *w;
1063 
1064 	assert(spdk_get_thread() == g_app_thread);
1065 
1066 	if (rc) {
1067 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1068 						 "framework_initialization failed");
1069 		return;
1070 	}
1071 
1072 	spdk_rpc_set_state(SPDK_RPC_RUNTIME);
1073 	app_start_application();
1074 
1075 	w = spdk_jsonrpc_begin_result(request);
1076 	spdk_json_write_bool(w, true);
1077 	spdk_jsonrpc_end_result(request, w);
1078 }
1079 
1080 static void
1081 rpc_framework_start_init(struct spdk_jsonrpc_request *request,
1082 			 const struct spdk_json_val *params)
1083 {
1084 	if (params != NULL) {
1085 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1086 						 "framework_start_init requires no parameters");
1087 		return;
1088 	}
1089 
1090 	spdk_subsystem_init(rpc_framework_start_init_cpl, request);
1091 }
1092 SPDK_RPC_REGISTER("framework_start_init", rpc_framework_start_init, SPDK_RPC_STARTUP)
1093 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_start_init, start_subsystem_init)
1094 
1095 struct subsystem_init_poller_ctx {
1096 	struct spdk_poller *init_poller;
1097 	struct spdk_jsonrpc_request *request;
1098 };
1099 
1100 static int
1101 rpc_subsystem_init_poller_ctx(void *ctx)
1102 {
1103 	struct spdk_json_write_ctx *w;
1104 	struct subsystem_init_poller_ctx *poller_ctx = ctx;
1105 
1106 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1107 		w = spdk_jsonrpc_begin_result(poller_ctx->request);
1108 		spdk_json_write_bool(w, true);
1109 		spdk_jsonrpc_end_result(poller_ctx->request, w);
1110 		spdk_poller_unregister(&poller_ctx->init_poller);
1111 		free(poller_ctx);
1112 	}
1113 
1114 	return 1;
1115 }
1116 
1117 static void
1118 rpc_framework_wait_init(struct spdk_jsonrpc_request *request,
1119 			const struct spdk_json_val *params)
1120 {
1121 	struct spdk_json_write_ctx *w;
1122 	struct subsystem_init_poller_ctx *ctx;
1123 
1124 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1125 		w = spdk_jsonrpc_begin_result(request);
1126 		spdk_json_write_bool(w, true);
1127 		spdk_jsonrpc_end_result(request, w);
1128 	} else {
1129 		ctx = malloc(sizeof(struct subsystem_init_poller_ctx));
1130 		if (ctx == NULL) {
1131 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1132 							 "Unable to allocate memory for the request context\n");
1133 			return;
1134 		}
1135 		ctx->request = request;
1136 		ctx->init_poller = SPDK_POLLER_REGISTER(rpc_subsystem_init_poller_ctx, ctx, 0);
1137 	}
1138 }
1139 SPDK_RPC_REGISTER("framework_wait_init", rpc_framework_wait_init,
1140 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1141 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_wait_init, wait_subsystem_init)
1142