xref: /spdk/lib/event/app.c (revision d73077b84a71985da1db1c9847ea7c042189bae2)
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/thread.h"
42 #include "spdk/trace.h"
43 #include "spdk/string.h"
44 #include "spdk/rpc.h"
45 #include "spdk/util.h"
46 
47 #define SPDK_APP_DEFAULT_LOG_LEVEL		SPDK_LOG_NOTICE
48 #define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL	SPDK_LOG_INFO
49 #define SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES	SPDK_DEFAULT_NUM_TRACE_ENTRIES
50 
51 #define SPDK_APP_DPDK_DEFAULT_MEM_SIZE		-1
52 #define SPDK_APP_DPDK_DEFAULT_MASTER_CORE	-1
53 #define SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL	-1
54 #define SPDK_APP_DPDK_DEFAULT_CORE_MASK		"0x1"
55 #define SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR	0x200000000000
56 #define SPDK_APP_DEFAULT_CORE_LIMIT		0x140000000 /* 5 GiB */
57 
58 struct spdk_app {
59 	const char			*json_config_file;
60 	bool				json_config_ignore_errors;
61 	const char			*rpc_addr;
62 	int				shm_id;
63 	spdk_app_shutdown_cb		shutdown_cb;
64 	int				rc;
65 };
66 
67 static struct spdk_app g_spdk_app;
68 static spdk_msg_fn g_start_fn = NULL;
69 static void *g_start_arg = NULL;
70 static struct spdk_thread *g_app_thread = NULL;
71 static bool g_delay_subsystem_init = false;
72 static bool g_shutdown_sig_received = false;
73 static char *g_executable_name;
74 static struct spdk_app_opts g_default_opts;
75 
76 int
77 spdk_app_get_shm_id(void)
78 {
79 	return g_spdk_app.shm_id;
80 }
81 
82 /* append one empty option to indicate the end of the array */
83 static const struct option g_cmdline_options[] = {
84 #define CONFIG_FILE_OPT_IDX	'c'
85 	{"config",			required_argument,	NULL, CONFIG_FILE_OPT_IDX},
86 #define LIMIT_COREDUMP_OPT_IDX 'd'
87 	{"limit-coredump",		no_argument,		NULL, LIMIT_COREDUMP_OPT_IDX},
88 #define TPOINT_GROUP_MASK_OPT_IDX 'e'
89 	{"tpoint-group-mask",		required_argument,	NULL, TPOINT_GROUP_MASK_OPT_IDX},
90 #define SINGLE_FILE_SEGMENTS_OPT_IDX 'g'
91 	{"single-file-segments",	no_argument,		NULL, SINGLE_FILE_SEGMENTS_OPT_IDX},
92 #define HELP_OPT_IDX		'h'
93 	{"help",			no_argument,		NULL, HELP_OPT_IDX},
94 #define SHM_ID_OPT_IDX		'i'
95 	{"shm-id",			required_argument,	NULL, SHM_ID_OPT_IDX},
96 #define CPUMASK_OPT_IDX		'm'
97 	{"cpumask",			required_argument,	NULL, CPUMASK_OPT_IDX},
98 #define MEM_CHANNELS_OPT_IDX	'n'
99 	{"mem-channels",		required_argument,	NULL, MEM_CHANNELS_OPT_IDX},
100 #define MASTER_CORE_OPT_IDX	'p'
101 	{"master-core",			required_argument,	NULL, MASTER_CORE_OPT_IDX},
102 #define RPC_SOCKET_OPT_IDX	'r'
103 	{"rpc-socket",			required_argument,	NULL, RPC_SOCKET_OPT_IDX},
104 #define MEM_SIZE_OPT_IDX	's'
105 	{"mem-size",			required_argument,	NULL, MEM_SIZE_OPT_IDX},
106 #define NO_PCI_OPT_IDX		'u'
107 	{"no-pci",			no_argument,		NULL, NO_PCI_OPT_IDX},
108 #define VERSION_OPT_IDX		'v'
109 	{"version",			no_argument,		NULL, VERSION_OPT_IDX},
110 #define PCI_BLACKLIST_OPT_IDX	'B'
111 	{"pci-blacklist",		required_argument,	NULL, PCI_BLACKLIST_OPT_IDX},
112 #define LOGFLAG_OPT_IDX		'L'
113 	{"logflag",			required_argument,	NULL, LOGFLAG_OPT_IDX},
114 #define HUGE_UNLINK_OPT_IDX	'R'
115 	{"huge-unlink",			no_argument,		NULL, HUGE_UNLINK_OPT_IDX},
116 #define PCI_WHITELIST_OPT_IDX	'W'
117 	{"pci-whitelist",		required_argument,	NULL, PCI_WHITELIST_OPT_IDX},
118 #define SILENCE_NOTICELOG_OPT_IDX 257
119 	{"silence-noticelog",		no_argument,		NULL, SILENCE_NOTICELOG_OPT_IDX},
120 #define WAIT_FOR_RPC_OPT_IDX	258
121 	{"wait-for-rpc",		no_argument,		NULL, WAIT_FOR_RPC_OPT_IDX},
122 #define HUGE_DIR_OPT_IDX	259
123 	{"huge-dir",			required_argument,	NULL, HUGE_DIR_OPT_IDX},
124 #define NUM_TRACE_ENTRIES_OPT_IDX	260
125 	{"num-trace-entries",		required_argument,	NULL, NUM_TRACE_ENTRIES_OPT_IDX},
126 #define MAX_REACTOR_DELAY_OPT_IDX	261
127 	{"max-delay",			required_argument,	NULL, MAX_REACTOR_DELAY_OPT_IDX},
128 #define JSON_CONFIG_OPT_IDX		262
129 	{"json",			required_argument,	NULL, JSON_CONFIG_OPT_IDX},
130 #define JSON_CONFIG_IGNORE_INIT_ERRORS_IDX	263
131 	{"json-ignore-init-errors",	no_argument,		NULL, JSON_CONFIG_IGNORE_INIT_ERRORS_IDX},
132 #define IOVA_MODE_OPT_IDX	264
133 	{"iova-mode",			required_argument,	NULL, IOVA_MODE_OPT_IDX},
134 #define BASE_VIRTADDR_OPT_IDX	265
135 	{"base-virtaddr",		required_argument,	NULL, BASE_VIRTADDR_OPT_IDX},
136 };
137 
138 static void
139 app_start_shutdown(void *ctx)
140 {
141 	if (g_spdk_app.shutdown_cb) {
142 		g_spdk_app.shutdown_cb();
143 		g_spdk_app.shutdown_cb = NULL;
144 	} else {
145 		spdk_app_stop(0);
146 	}
147 }
148 
149 void
150 spdk_app_start_shutdown(void)
151 {
152 	spdk_thread_send_critical_msg(g_app_thread, app_start_shutdown);
153 }
154 
155 static void
156 __shutdown_signal(int signo)
157 {
158 	if (!g_shutdown_sig_received) {
159 		g_shutdown_sig_received = true;
160 		spdk_app_start_shutdown();
161 	}
162 }
163 
164 static int
165 app_opts_validate(const char *app_opts)
166 {
167 	int i = 0, j;
168 
169 	for (i = 0; app_opts[i] != '\0'; i++) {
170 		/* ignore getopt control characters */
171 		if (app_opts[i] == ':' || app_opts[i] == '+' || app_opts[i] == '-') {
172 			continue;
173 		}
174 
175 		for (j = 0; SPDK_APP_GETOPT_STRING[j] != '\0'; j++) {
176 			if (app_opts[i] == SPDK_APP_GETOPT_STRING[j]) {
177 				return app_opts[i];
178 			}
179 		}
180 	}
181 	return 0;
182 }
183 
184 void
185 spdk_app_opts_init(struct spdk_app_opts *opts)
186 {
187 	if (!opts) {
188 		return;
189 	}
190 
191 	memset(opts, 0, sizeof(*opts));
192 
193 	opts->enable_coredump = true;
194 	opts->shm_id = -1;
195 	opts->mem_size = SPDK_APP_DPDK_DEFAULT_MEM_SIZE;
196 	opts->master_core = SPDK_APP_DPDK_DEFAULT_MASTER_CORE;
197 	opts->mem_channel = SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL;
198 	opts->reactor_mask = SPDK_APP_DPDK_DEFAULT_CORE_MASK;
199 	opts->base_virtaddr = SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR;
200 	opts->print_level = SPDK_APP_DEFAULT_LOG_PRINT_LEVEL;
201 	opts->rpc_addr = SPDK_DEFAULT_RPC_ADDR;
202 	opts->num_entries = SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES;
203 	opts->delay_subsystem_init = false;
204 }
205 
206 static int
207 app_setup_signal_handlers(struct spdk_app_opts *opts)
208 {
209 	struct sigaction	sigact;
210 	sigset_t		sigmask;
211 	int			rc;
212 
213 	sigemptyset(&sigmask);
214 	memset(&sigact, 0, sizeof(sigact));
215 	sigemptyset(&sigact.sa_mask);
216 
217 	sigact.sa_handler = SIG_IGN;
218 	rc = sigaction(SIGPIPE, &sigact, NULL);
219 	if (rc < 0) {
220 		SPDK_ERRLOG("sigaction(SIGPIPE) failed\n");
221 		return rc;
222 	}
223 
224 	/* Install the same handler for SIGINT and SIGTERM */
225 	g_shutdown_sig_received = false;
226 	sigact.sa_handler = __shutdown_signal;
227 	rc = sigaction(SIGINT, &sigact, NULL);
228 	if (rc < 0) {
229 		SPDK_ERRLOG("sigaction(SIGINT) failed\n");
230 		return rc;
231 	}
232 	sigaddset(&sigmask, SIGINT);
233 
234 	rc = sigaction(SIGTERM, &sigact, NULL);
235 	if (rc < 0) {
236 		SPDK_ERRLOG("sigaction(SIGTERM) failed\n");
237 		return rc;
238 	}
239 	sigaddset(&sigmask, SIGTERM);
240 
241 	pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
242 
243 	return 0;
244 }
245 
246 static void
247 app_start_application(void)
248 {
249 	assert(spdk_get_thread() == g_app_thread);
250 
251 	g_start_fn(g_start_arg);
252 }
253 
254 static void
255 app_start_rpc(int rc, void *arg1)
256 {
257 	if (rc) {
258 		spdk_app_stop(rc);
259 		return;
260 	}
261 
262 	spdk_rpc_initialize(g_spdk_app.rpc_addr);
263 	if (!g_delay_subsystem_init) {
264 		spdk_rpc_set_state(SPDK_RPC_RUNTIME);
265 		app_start_application();
266 	}
267 }
268 
269 static int
270 app_opts_add_pci_addr(struct spdk_app_opts *opts, struct spdk_pci_addr **list, char *bdf)
271 {
272 	struct spdk_pci_addr *tmp = *list;
273 	size_t i = opts->num_pci_addr;
274 
275 	tmp = realloc(tmp, sizeof(*tmp) * (i + 1));
276 	if (tmp == NULL) {
277 		SPDK_ERRLOG("realloc error\n");
278 		return -ENOMEM;
279 	}
280 
281 	*list = tmp;
282 	if (spdk_pci_addr_parse(*list + i, bdf) < 0) {
283 		SPDK_ERRLOG("Invalid address %s\n", bdf);
284 		return -EINVAL;
285 	}
286 
287 	opts->num_pci_addr++;
288 	return 0;
289 }
290 
291 static int
292 app_setup_env(struct spdk_app_opts *opts)
293 {
294 	struct spdk_env_opts env_opts = {};
295 	int rc;
296 
297 	if (opts == NULL) {
298 		rc = spdk_env_init(NULL);
299 		if (rc != 0) {
300 			SPDK_ERRLOG("Unable to reinitialize SPDK env\n");
301 		}
302 
303 		return rc;
304 	}
305 
306 	spdk_env_opts_init(&env_opts);
307 
308 	env_opts.name = opts->name;
309 	env_opts.core_mask = opts->reactor_mask;
310 	env_opts.shm_id = opts->shm_id;
311 	env_opts.mem_channel = opts->mem_channel;
312 	env_opts.master_core = opts->master_core;
313 	env_opts.mem_size = opts->mem_size;
314 	env_opts.hugepage_single_segments = opts->hugepage_single_segments;
315 	env_opts.unlink_hugepage = opts->unlink_hugepage;
316 	env_opts.hugedir = opts->hugedir;
317 	env_opts.no_pci = opts->no_pci;
318 	env_opts.num_pci_addr = opts->num_pci_addr;
319 	env_opts.pci_blacklist = opts->pci_blacklist;
320 	env_opts.pci_whitelist = opts->pci_whitelist;
321 	env_opts.base_virtaddr = opts->base_virtaddr;
322 	env_opts.env_context = opts->env_context;
323 	env_opts.iova_mode = opts->iova_mode;
324 
325 	rc = spdk_env_init(&env_opts);
326 	free(env_opts.pci_blacklist);
327 	free(env_opts.pci_whitelist);
328 
329 	if (rc < 0) {
330 		SPDK_ERRLOG("Unable to initialize SPDK env\n");
331 	}
332 
333 	return rc;
334 }
335 
336 static int
337 app_setup_trace(struct spdk_app_opts *opts)
338 {
339 	char		shm_name[64];
340 	uint64_t	tpoint_group_mask;
341 	char		*end;
342 
343 	if (opts->shm_id >= 0) {
344 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", opts->name, opts->shm_id);
345 	} else {
346 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", opts->name, (int)getpid());
347 	}
348 
349 	if (spdk_trace_init(shm_name, opts->num_entries) != 0) {
350 		return -1;
351 	}
352 
353 	if (opts->tpoint_group_mask != NULL) {
354 		errno = 0;
355 		tpoint_group_mask = strtoull(opts->tpoint_group_mask, &end, 16);
356 		if (*end != '\0' || errno) {
357 			SPDK_ERRLOG("invalid tpoint mask %s\n", opts->tpoint_group_mask);
358 		} else {
359 			SPDK_NOTICELOG("Tracepoint Group Mask %s specified.\n", opts->tpoint_group_mask);
360 			SPDK_NOTICELOG("Use 'spdk_trace -s %s %s %d' to capture a snapshot of events at runtime.\n",
361 				       opts->name,
362 				       opts->shm_id >= 0 ? "-i" : "-p",
363 				       opts->shm_id >= 0 ? opts->shm_id : getpid());
364 #if defined(__linux__)
365 			SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name);
366 #endif
367 			spdk_trace_set_tpoint_group_mask(tpoint_group_mask);
368 		}
369 	}
370 
371 	return 0;
372 }
373 
374 static void
375 bootstrap_fn(void *arg1)
376 {
377 	if (g_spdk_app.json_config_file) {
378 		g_delay_subsystem_init = false;
379 		spdk_app_json_config_load(g_spdk_app.json_config_file, g_spdk_app.rpc_addr, app_start_rpc,
380 					  NULL, !g_spdk_app.json_config_ignore_errors);
381 	} else {
382 		if (!g_delay_subsystem_init) {
383 			spdk_subsystem_init(app_start_rpc, NULL);
384 		} else {
385 			spdk_rpc_initialize(g_spdk_app.rpc_addr);
386 		}
387 	}
388 }
389 
390 int
391 spdk_app_start(struct spdk_app_opts *opts, spdk_msg_fn start_fn,
392 	       void *arg1)
393 {
394 	int			rc;
395 	char			*tty;
396 	struct spdk_cpuset	tmp_cpumask = {};
397 	static bool		g_env_was_setup = false;
398 
399 	if (!opts) {
400 		SPDK_ERRLOG("opts should not be NULL\n");
401 		return 1;
402 	}
403 
404 	if (opts->config_file) {
405 		SPDK_ERRLOG("opts->config_file is deprecated.  Use opts->json_config_file instead.\n");
406 		/* For now we will just treat config_file as json_config_file.  But if both were
407 		 * specified we will return an error here.
408 		 */
409 		if (opts->json_config_file) {
410 			SPDK_ERRLOG("Setting both opts->config_file and opts->json_config_file not allowed.\n");
411 			return 1;
412 		}
413 		opts->json_config_file = opts->config_file;
414 	}
415 
416 	if (!start_fn) {
417 		SPDK_ERRLOG("start_fn should not be NULL\n");
418 		return 1;
419 	}
420 
421 	tty = ttyname(STDERR_FILENO);
422 	if (opts->print_level > SPDK_LOG_WARN &&
423 	    isatty(STDERR_FILENO) &&
424 	    tty &&
425 	    !strncmp(tty, "/dev/tty", strlen("/dev/tty"))) {
426 		printf("Warning: printing stderr to console terminal without -q option specified.\n");
427 		printf("Suggest using --silence-noticelog to disable logging to stderr and\n");
428 		printf("monitor syslog, or redirect stderr to a file.\n");
429 		printf("(Delaying for 10 seconds...)\n");
430 		sleep(10);
431 	}
432 
433 	spdk_log_set_print_level(opts->print_level);
434 
435 #ifndef SPDK_NO_RLIMIT
436 	if (opts->enable_coredump) {
437 		struct rlimit core_limits;
438 
439 		core_limits.rlim_cur = core_limits.rlim_max = SPDK_APP_DEFAULT_CORE_LIMIT;
440 		setrlimit(RLIMIT_CORE, &core_limits);
441 	}
442 #endif
443 
444 	memset(&g_spdk_app, 0, sizeof(g_spdk_app));
445 	g_spdk_app.json_config_file = opts->json_config_file;
446 	g_spdk_app.json_config_ignore_errors = opts->json_config_ignore_errors;
447 	g_spdk_app.rpc_addr = opts->rpc_addr;
448 	g_spdk_app.shm_id = opts->shm_id;
449 	g_spdk_app.shutdown_cb = opts->shutdown_cb;
450 	g_spdk_app.rc = 0;
451 
452 	spdk_log_set_level(SPDK_APP_DEFAULT_LOG_LEVEL);
453 
454 	/* Pass NULL to app_setup_env if SPDK app has been set up, in order to
455 	 * indicate that this is a reinitialization.
456 	 */
457 	if (app_setup_env(g_env_was_setup ? NULL : opts) < 0) {
458 		return 1;
459 	}
460 
461 	spdk_log_open(opts->log);
462 	SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count());
463 
464 	if ((rc = spdk_reactors_init()) != 0) {
465 		SPDK_ERRLOG("Reactor Initilization failed: rc = %d\n", rc);
466 		return 1;
467 	}
468 
469 	spdk_cpuset_set_cpu(&tmp_cpumask, spdk_env_get_current_core(), true);
470 
471 	/* Now that the reactors have been initialized, we can create an
472 	 * initialization thread. */
473 	g_app_thread = spdk_thread_create("app_thread", &tmp_cpumask);
474 	if (!g_app_thread) {
475 		SPDK_ERRLOG("Unable to create an spdk_thread for initialization\n");
476 		return 1;
477 	}
478 
479 	/*
480 	 * Note the call to app_setup_trace() is located here
481 	 * ahead of app_setup_signal_handlers().
482 	 * That's because there is not an easy/direct clean
483 	 * way of unwinding alloc'd resources that can occur
484 	 * in app_setup_signal_handlers().
485 	 */
486 	if (app_setup_trace(opts) != 0) {
487 		return 1;
488 	}
489 
490 	if (app_setup_signal_handlers(opts) != 0) {
491 		return 1;
492 	}
493 
494 	g_delay_subsystem_init = opts->delay_subsystem_init;
495 	g_start_fn = start_fn;
496 	g_start_arg = arg1;
497 
498 	spdk_thread_send_msg(g_app_thread, bootstrap_fn, NULL);
499 
500 	/* This blocks until spdk_app_stop is called */
501 	spdk_reactors_start();
502 
503 	g_env_was_setup = true;
504 
505 	return g_spdk_app.rc;
506 }
507 
508 void
509 spdk_app_fini(void)
510 {
511 	spdk_trace_cleanup();
512 	spdk_reactors_fini();
513 	spdk_env_fini();
514 	spdk_log_close();
515 }
516 
517 static void
518 app_stop(void *arg1)
519 {
520 	spdk_rpc_finish();
521 	spdk_subsystem_fini(spdk_reactors_stop, NULL);
522 }
523 
524 void
525 spdk_app_stop(int rc)
526 {
527 	if (rc) {
528 		SPDK_WARNLOG("spdk_app_stop'd on non-zero\n");
529 	}
530 	g_spdk_app.rc = rc;
531 	/*
532 	 * We want to run spdk_subsystem_fini() from the same thread where spdk_subsystem_init()
533 	 * was called.
534 	 */
535 	spdk_thread_send_msg(g_app_thread, app_stop, NULL);
536 }
537 
538 static void
539 usage(void (*app_usage)(void))
540 {
541 	printf("%s [options]\n", g_executable_name);
542 	printf("options:\n");
543 	printf(" -c, --config <config>     JSON config file (default %s)\n",
544 	       g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none");
545 	printf("     --json <config>       JSON config file (default %s)\n",
546 	       g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none");
547 	printf("     --json-ignore-init-errors\n");
548 	printf("                           don't exit on invalid config entry\n");
549 	printf(" -d, --limit-coredump      do not set max coredump size to RLIM_INFINITY\n");
550 	printf(" -g, --single-file-segments\n");
551 	printf("                           force creating just one hugetlbfs file\n");
552 	printf(" -h, --help                show this usage\n");
553 	printf(" -i, --shm-id <id>         shared memory ID (optional)\n");
554 	printf(" -m, --cpumask <mask>      core mask for DPDK\n");
555 	printf(" -n, --mem-channels <num>  channel number of memory channels used for DPDK\n");
556 	printf(" -p, --master-core <id>    master (primary) core for DPDK\n");
557 	printf(" -r, --rpc-socket <path>   RPC listen address (default %s)\n", SPDK_DEFAULT_RPC_ADDR);
558 	printf(" -s, --mem-size <size>     memory size in MB for DPDK (default: ");
559 #ifndef __linux__
560 	if (g_default_opts.mem_size <= 0) {
561 		printf("all hugepage memory)\n");
562 	} else
563 #endif
564 	{
565 		printf("%dMB)\n", g_default_opts.mem_size >= 0 ? g_default_opts.mem_size : 0);
566 	}
567 	printf("     --silence-noticelog   disable notice level logging to stderr\n");
568 	printf(" -u, --no-pci              disable PCI access\n");
569 	printf("     --wait-for-rpc        wait for RPCs to initialize subsystems\n");
570 	printf("     --max-delay <num>     maximum reactor delay (in microseconds)\n");
571 	printf(" -B, --pci-blacklist <bdf>\n");
572 	printf("                           pci addr to blacklist (can be used more than once)\n");
573 	printf(" -R, --huge-unlink         unlink huge files after initialization\n");
574 	printf(" -v, --version             print SPDK version\n");
575 	printf(" -W, --pci-whitelist <bdf>\n");
576 	printf("                           pci addr to whitelist (-B and -W cannot be used at the same time)\n");
577 	printf("      --huge-dir <path>    use a specific hugetlbfs mount to reserve memory from\n");
578 	printf("      --iova-mode <pa/va>  set IOVA mode ('pa' for IOVA_PA and 'va' for IOVA_VA)\n");
579 	printf("      --base-virtaddr <addr>      the base virtual address for DPDK (default: 0x200000000000)\n");
580 	printf("      --num-trace-entries <num>   number of trace entries for each core, must be power of 2. (default %d)\n",
581 	       SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
582 	spdk_log_usage(stdout, "-L");
583 	spdk_trace_mask_usage(stdout, "-e");
584 	if (app_usage) {
585 		app_usage();
586 	}
587 }
588 
589 spdk_app_parse_args_rvals_t
590 spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
591 		    const char *app_getopt_str, struct option *app_long_opts,
592 		    int (*app_parse)(int ch, char *arg),
593 		    void (*app_usage)(void))
594 {
595 	int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len;
596 	struct option *cmdline_options;
597 	char *cmdline_short_opts = NULL;
598 	enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL;
599 	long int tmp;
600 
601 	memcpy(&g_default_opts, opts, sizeof(g_default_opts));
602 
603 	if (opts->config_file && access(opts->config_file, R_OK) != 0) {
604 		SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->config_file);
605 		opts->config_file = NULL;
606 	}
607 
608 	if (opts->json_config_file && access(opts->json_config_file, R_OK) != 0) {
609 		SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->json_config_file);
610 		opts->json_config_file = NULL;
611 	}
612 
613 	if (app_long_opts == NULL) {
614 		app_long_opts_len = 0;
615 	} else {
616 		for (app_long_opts_len = 0;
617 		     app_long_opts[app_long_opts_len].name != NULL;
618 		     app_long_opts_len++);
619 	}
620 
621 	global_long_opts_len = SPDK_COUNTOF(g_cmdline_options);
622 
623 	cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options));
624 	if (!cmdline_options) {
625 		SPDK_ERRLOG("Out of memory\n");
626 		return SPDK_APP_PARSE_ARGS_FAIL;
627 	}
628 
629 	memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options));
630 	if (app_long_opts) {
631 		memcpy(&cmdline_options[global_long_opts_len], app_long_opts,
632 		       app_long_opts_len * sizeof(*app_long_opts));
633 	}
634 
635 	if (app_getopt_str != NULL) {
636 		ch = app_opts_validate(app_getopt_str);
637 		if (ch) {
638 			SPDK_ERRLOG("Duplicated option '%c' between the generic and application specific spdk opts.\n",
639 				    ch);
640 			goto out;
641 		}
642 	}
643 
644 	cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
645 	if (!cmdline_short_opts) {
646 		SPDK_ERRLOG("Out of memory\n");
647 		goto out;
648 	}
649 
650 	g_executable_name = argv[0];
651 
652 	while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) {
653 		switch (ch) {
654 		case CONFIG_FILE_OPT_IDX:
655 		case JSON_CONFIG_OPT_IDX:
656 			opts->json_config_file = optarg;
657 			break;
658 		case JSON_CONFIG_IGNORE_INIT_ERRORS_IDX:
659 			opts->json_config_ignore_errors = true;
660 			break;
661 		case LIMIT_COREDUMP_OPT_IDX:
662 			opts->enable_coredump = false;
663 			break;
664 		case TPOINT_GROUP_MASK_OPT_IDX:
665 			opts->tpoint_group_mask = optarg;
666 			break;
667 		case SINGLE_FILE_SEGMENTS_OPT_IDX:
668 			opts->hugepage_single_segments = true;
669 			break;
670 		case HELP_OPT_IDX:
671 			usage(app_usage);
672 			retval = SPDK_APP_PARSE_ARGS_HELP;
673 			goto out;
674 		case SHM_ID_OPT_IDX:
675 			opts->shm_id = spdk_strtol(optarg, 0);
676 			if (opts->shm_id < 0) {
677 				SPDK_ERRLOG("Invalid shared memory ID %s\n", optarg);
678 				goto out;
679 			}
680 			break;
681 		case CPUMASK_OPT_IDX:
682 			opts->reactor_mask = optarg;
683 			break;
684 		case MEM_CHANNELS_OPT_IDX:
685 			opts->mem_channel = spdk_strtol(optarg, 0);
686 			if (opts->mem_channel < 0) {
687 				SPDK_ERRLOG("Invalid memory channel %s\n", optarg);
688 				goto out;
689 			}
690 			break;
691 		case MASTER_CORE_OPT_IDX:
692 			opts->master_core = spdk_strtol(optarg, 0);
693 			if (opts->master_core < 0) {
694 				SPDK_ERRLOG("Invalid master core %s\n", optarg);
695 				goto out;
696 			}
697 			break;
698 		case SILENCE_NOTICELOG_OPT_IDX:
699 			opts->print_level = SPDK_LOG_WARN;
700 			break;
701 		case RPC_SOCKET_OPT_IDX:
702 			opts->rpc_addr = optarg;
703 			break;
704 		case MEM_SIZE_OPT_IDX: {
705 			uint64_t mem_size_mb;
706 			bool mem_size_has_prefix;
707 
708 			rc = spdk_parse_capacity(optarg, &mem_size_mb, &mem_size_has_prefix);
709 			if (rc != 0) {
710 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
711 				usage(app_usage);
712 				goto out;
713 			}
714 
715 			if (mem_size_has_prefix) {
716 				/* the mem size is in MB by default, so if a prefix was
717 				 * specified, we need to manually convert to MB.
718 				 */
719 				mem_size_mb /= 1024 * 1024;
720 			}
721 
722 			if (mem_size_mb > INT_MAX) {
723 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
724 				usage(app_usage);
725 				goto out;
726 			}
727 
728 			opts->mem_size = (int) mem_size_mb;
729 			break;
730 		}
731 		case NO_PCI_OPT_IDX:
732 			opts->no_pci = true;
733 			break;
734 		case WAIT_FOR_RPC_OPT_IDX:
735 			opts->delay_subsystem_init = true;
736 			break;
737 		case PCI_BLACKLIST_OPT_IDX:
738 			if (opts->pci_whitelist) {
739 				free(opts->pci_whitelist);
740 				opts->pci_whitelist = NULL;
741 				SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
742 				usage(app_usage);
743 				goto out;
744 			}
745 
746 			rc = app_opts_add_pci_addr(opts, &opts->pci_blacklist, optarg);
747 			if (rc != 0) {
748 				free(opts->pci_blacklist);
749 				opts->pci_blacklist = NULL;
750 				goto out;
751 			}
752 			break;
753 		case LOGFLAG_OPT_IDX:
754 			rc = spdk_log_set_flag(optarg);
755 			if (rc < 0) {
756 				SPDK_ERRLOG("unknown flag\n");
757 				usage(app_usage);
758 				goto out;
759 			}
760 #ifdef DEBUG
761 			opts->print_level = SPDK_LOG_DEBUG;
762 #endif
763 			break;
764 		case HUGE_UNLINK_OPT_IDX:
765 			opts->unlink_hugepage = true;
766 			break;
767 		case PCI_WHITELIST_OPT_IDX:
768 			if (opts->pci_blacklist) {
769 				free(opts->pci_blacklist);
770 				opts->pci_blacklist = NULL;
771 				SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
772 				usage(app_usage);
773 				goto out;
774 			}
775 
776 			rc = app_opts_add_pci_addr(opts, &opts->pci_whitelist, optarg);
777 			if (rc != 0) {
778 				free(opts->pci_whitelist);
779 				opts->pci_whitelist = NULL;
780 				goto out;
781 			}
782 			break;
783 		case BASE_VIRTADDR_OPT_IDX:
784 			tmp = spdk_strtoll(optarg, 0);
785 			if (tmp <= 0) {
786 				SPDK_ERRLOG("Invalid base-virtaddr %s\n", optarg);
787 				usage(app_usage);
788 				goto out;
789 			}
790 			opts->base_virtaddr = (uint64_t)tmp;
791 			break;
792 		case HUGE_DIR_OPT_IDX:
793 			opts->hugedir = optarg;
794 			break;
795 		case IOVA_MODE_OPT_IDX:
796 			opts->iova_mode = optarg;
797 			break;
798 		case NUM_TRACE_ENTRIES_OPT_IDX:
799 			tmp = spdk_strtoll(optarg, 0);
800 			if (tmp <= 0) {
801 				SPDK_ERRLOG("Invalid num-trace-entries %s\n", optarg);
802 				usage(app_usage);
803 				goto out;
804 			}
805 			opts->num_entries = (uint64_t)tmp;
806 			if (opts->num_entries & (opts->num_entries - 1)) {
807 				SPDK_ERRLOG("num-trace-entries must be power of 2\n");
808 				usage(app_usage);
809 				goto out;
810 			}
811 			break;
812 		case MAX_REACTOR_DELAY_OPT_IDX:
813 			SPDK_ERRLOG("Deprecation warning: The maximum allowed latency parameter is no longer supported.\n");
814 			break;
815 		case VERSION_OPT_IDX:
816 			printf(SPDK_VERSION_STRING"\n");
817 			retval = SPDK_APP_PARSE_ARGS_HELP;
818 			goto out;
819 		case '?':
820 			/*
821 			 * In the event getopt() above detects an option
822 			 * in argv that is NOT in the getopt_str,
823 			 * getopt() will return a '?' indicating failure.
824 			 */
825 			usage(app_usage);
826 			goto out;
827 		default:
828 			rc = app_parse(ch, optarg);
829 			if (rc) {
830 				SPDK_ERRLOG("Parsing application specific arguments failed: %d\n", rc);
831 				goto out;
832 			}
833 		}
834 	}
835 
836 	if (opts->json_config_file && opts->delay_subsystem_init) {
837 		SPDK_ERRLOG("JSON configuration file can't be used together with --wait-for-rpc.\n");
838 		goto out;
839 	}
840 
841 	retval = SPDK_APP_PARSE_ARGS_SUCCESS;
842 out:
843 	if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) {
844 		free(opts->pci_blacklist);
845 		opts->pci_blacklist = NULL;
846 		free(opts->pci_whitelist);
847 		opts->pci_whitelist = NULL;
848 	}
849 	free(cmdline_short_opts);
850 	free(cmdline_options);
851 	return retval;
852 }
853 
854 void
855 spdk_app_usage(void)
856 {
857 	if (g_executable_name == NULL) {
858 		SPDK_ERRLOG("%s not valid before calling spdk_app_parse_args()\n", __func__);
859 		return;
860 	}
861 
862 	usage(NULL);
863 }
864 
865 static void
866 rpc_framework_start_init_cpl(int rc, void *arg1)
867 {
868 	struct spdk_jsonrpc_request *request = arg1;
869 
870 	assert(spdk_get_thread() == g_app_thread);
871 
872 	if (rc) {
873 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
874 						 "framework_initialization failed");
875 		return;
876 	}
877 
878 	spdk_rpc_set_state(SPDK_RPC_RUNTIME);
879 	app_start_application();
880 
881 	spdk_jsonrpc_send_bool_response(request, true);
882 }
883 
884 static void
885 rpc_framework_start_init(struct spdk_jsonrpc_request *request,
886 			 const struct spdk_json_val *params)
887 {
888 	if (params != NULL) {
889 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
890 						 "framework_start_init requires no parameters");
891 		return;
892 	}
893 
894 	spdk_subsystem_init(rpc_framework_start_init_cpl, request);
895 }
896 SPDK_RPC_REGISTER("framework_start_init", rpc_framework_start_init, SPDK_RPC_STARTUP)
897 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_start_init, start_subsystem_init)
898 
899 struct subsystem_init_poller_ctx {
900 	struct spdk_poller *init_poller;
901 	struct spdk_jsonrpc_request *request;
902 };
903 
904 static int
905 rpc_subsystem_init_poller_ctx(void *ctx)
906 {
907 	struct subsystem_init_poller_ctx *poller_ctx = ctx;
908 
909 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
910 		spdk_jsonrpc_send_bool_response(poller_ctx->request, true);
911 		spdk_poller_unregister(&poller_ctx->init_poller);
912 		free(poller_ctx);
913 	}
914 
915 	return SPDK_POLLER_BUSY;
916 }
917 
918 static void
919 rpc_framework_wait_init(struct spdk_jsonrpc_request *request,
920 			const struct spdk_json_val *params)
921 {
922 	struct subsystem_init_poller_ctx *ctx;
923 
924 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
925 		spdk_jsonrpc_send_bool_response(request, true);
926 	} else {
927 		ctx = malloc(sizeof(struct subsystem_init_poller_ctx));
928 		if (ctx == NULL) {
929 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
930 							 "Unable to allocate memory for the request context\n");
931 			return;
932 		}
933 		ctx->request = request;
934 		ctx->init_poller = SPDK_POLLER_REGISTER(rpc_subsystem_init_poller_ctx, ctx, 0);
935 	}
936 }
937 SPDK_RPC_REGISTER("framework_wait_init", rpc_framework_wait_init,
938 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
939 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_wait_init, wait_subsystem_init)
940