xref: /spdk/lib/event/app.c (revision c680e3a05b1a903c18bf3f75b732765607126f45)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
3  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 #include "spdk/version.h"
9 
10 #include "spdk_internal/event.h"
11 
12 #include "spdk/assert.h"
13 #include "spdk/env.h"
14 #include "spdk/init.h"
15 #include "spdk/log.h"
16 #include "spdk/thread.h"
17 #include "spdk/trace.h"
18 #include "spdk/string.h"
19 #include "spdk/scheduler.h"
20 #include "spdk/rpc.h"
21 #include "spdk/util.h"
22 
23 #define SPDK_APP_DEFAULT_LOG_LEVEL		SPDK_LOG_NOTICE
24 #define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL	SPDK_LOG_INFO
25 #define SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES	SPDK_DEFAULT_NUM_TRACE_ENTRIES
26 
27 #define SPDK_APP_DPDK_DEFAULT_MEM_SIZE		-1
28 #define SPDK_APP_DPDK_DEFAULT_MAIN_CORE		-1
29 #define SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL	-1
30 #define SPDK_APP_DPDK_DEFAULT_CORE_MASK		"0x1"
31 #define SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR	0x200000000000
32 #define SPDK_APP_DEFAULT_CORE_LIMIT		0x140000000 /* 5 GiB */
33 
34 #define MAX_CPU_CORES				128
35 
36 struct spdk_app {
37 	const char			*json_config_file;
38 	bool				json_config_ignore_errors;
39 	bool				stopped;
40 	const char			*rpc_addr;
41 	const char			**rpc_allowlist;
42 	int				shm_id;
43 	spdk_app_shutdown_cb		shutdown_cb;
44 	int				rc;
45 };
46 
47 static struct spdk_app g_spdk_app;
48 static spdk_msg_fn g_start_fn = NULL;
49 static void *g_start_arg = NULL;
50 static struct spdk_thread *g_app_thread = NULL;
51 static bool g_delay_subsystem_init = false;
52 static bool g_shutdown_sig_received = false;
53 static char *g_executable_name;
54 static struct spdk_app_opts g_default_opts;
55 static bool g_disable_cpumask_locks = false;
56 
57 static int g_core_locks[MAX_CPU_CORES];
58 
59 int
60 spdk_app_get_shm_id(void)
61 {
62 	return g_spdk_app.shm_id;
63 }
64 
65 /* append one empty option to indicate the end of the array */
66 static const struct option g_cmdline_options[] = {
67 #define CONFIG_FILE_OPT_IDX	'c'
68 	{"config",			required_argument,	NULL, CONFIG_FILE_OPT_IDX},
69 #define LIMIT_COREDUMP_OPT_IDX 'd'
70 	{"limit-coredump",		no_argument,		NULL, LIMIT_COREDUMP_OPT_IDX},
71 #define TPOINT_GROUP_OPT_IDX 'e'
72 	{"tpoint-group",		required_argument,	NULL, TPOINT_GROUP_OPT_IDX},
73 #define SINGLE_FILE_SEGMENTS_OPT_IDX 'g'
74 	{"single-file-segments",	no_argument,		NULL, SINGLE_FILE_SEGMENTS_OPT_IDX},
75 #define HELP_OPT_IDX		'h'
76 	{"help",			no_argument,		NULL, HELP_OPT_IDX},
77 #define SHM_ID_OPT_IDX		'i'
78 	{"shm-id",			required_argument,	NULL, SHM_ID_OPT_IDX},
79 #define CPUMASK_OPT_IDX		'm'
80 	{"cpumask",			required_argument,	NULL, CPUMASK_OPT_IDX},
81 #define MEM_CHANNELS_OPT_IDX	'n'
82 	{"mem-channels",		required_argument,	NULL, MEM_CHANNELS_OPT_IDX},
83 #define MAIN_CORE_OPT_IDX	'p'
84 	{"main-core",			required_argument,	NULL, MAIN_CORE_OPT_IDX},
85 	{"master-core",			required_argument,	NULL, MAIN_CORE_OPT_IDX}, /* deprecated */
86 #define RPC_SOCKET_OPT_IDX	'r'
87 	{"rpc-socket",			required_argument,	NULL, RPC_SOCKET_OPT_IDX},
88 #define MEM_SIZE_OPT_IDX	's'
89 	{"mem-size",			required_argument,	NULL, MEM_SIZE_OPT_IDX},
90 #define NO_PCI_OPT_IDX		'u'
91 	{"no-pci",			no_argument,		NULL, NO_PCI_OPT_IDX},
92 #define VERSION_OPT_IDX		'v'
93 	{"version",			no_argument,		NULL, VERSION_OPT_IDX},
94 #define PCI_BLOCKED_OPT_IDX	'B'
95 	{"pci-blocked",			required_argument,	NULL, PCI_BLOCKED_OPT_IDX},
96 	{"pci-blacklist",		required_argument,	NULL, PCI_BLOCKED_OPT_IDX}, /* deprecated */
97 #define LOGFLAG_OPT_IDX		'L'
98 	{"logflag",			required_argument,	NULL, LOGFLAG_OPT_IDX},
99 #define HUGE_UNLINK_OPT_IDX	'R'
100 	{"huge-unlink",			no_argument,		NULL, HUGE_UNLINK_OPT_IDX},
101 #define PCI_ALLOWED_OPT_IDX	'A'
102 	{"pci-allowed",			required_argument,	NULL, PCI_ALLOWED_OPT_IDX},
103 #define PCI_WHITELIST_OPT_IDX	'W'
104 	{"pci-whitelist",		required_argument,	NULL, PCI_WHITELIST_OPT_IDX}, /* deprecated */
105 #define SILENCE_NOTICELOG_OPT_IDX 257
106 	{"silence-noticelog",		no_argument,		NULL, SILENCE_NOTICELOG_OPT_IDX},
107 #define WAIT_FOR_RPC_OPT_IDX	258
108 	{"wait-for-rpc",		no_argument,		NULL, WAIT_FOR_RPC_OPT_IDX},
109 #define HUGE_DIR_OPT_IDX	259
110 	{"huge-dir",			required_argument,	NULL, HUGE_DIR_OPT_IDX},
111 #define NUM_TRACE_ENTRIES_OPT_IDX	260
112 	{"num-trace-entries",		required_argument,	NULL, NUM_TRACE_ENTRIES_OPT_IDX},
113 #define MAX_REACTOR_DELAY_OPT_IDX	261
114 	{"max-delay",			required_argument,	NULL, MAX_REACTOR_DELAY_OPT_IDX},
115 #define JSON_CONFIG_OPT_IDX		262
116 	{"json",			required_argument,	NULL, JSON_CONFIG_OPT_IDX},
117 #define JSON_CONFIG_IGNORE_INIT_ERRORS_IDX	263
118 	{"json-ignore-init-errors",	no_argument,		NULL, JSON_CONFIG_IGNORE_INIT_ERRORS_IDX},
119 #define IOVA_MODE_OPT_IDX	264
120 	{"iova-mode",			required_argument,	NULL, IOVA_MODE_OPT_IDX},
121 #define BASE_VIRTADDR_OPT_IDX	265
122 	{"base-virtaddr",		required_argument,	NULL, BASE_VIRTADDR_OPT_IDX},
123 #define ENV_CONTEXT_OPT_IDX	266
124 	{"env-context",			required_argument,	NULL, ENV_CONTEXT_OPT_IDX},
125 #define DISABLE_CPUMASK_LOCKS_OPT_IDX	267
126 	{"disable-cpumask-locks",	no_argument,		NULL, DISABLE_CPUMASK_LOCKS_OPT_IDX},
127 #define RPCS_ALLOWED_OPT_IDX	268
128 	{"rpcs-allowed",		required_argument,	NULL, RPCS_ALLOWED_OPT_IDX}
129 };
130 
131 static void
132 app_start_shutdown(void *ctx)
133 {
134 	if (g_spdk_app.shutdown_cb) {
135 		g_spdk_app.shutdown_cb();
136 		g_spdk_app.shutdown_cb = NULL;
137 	} else {
138 		spdk_app_stop(0);
139 	}
140 }
141 
142 void
143 spdk_app_start_shutdown(void)
144 {
145 	spdk_thread_send_critical_msg(g_app_thread, app_start_shutdown);
146 }
147 
148 static void
149 __shutdown_signal(int signo)
150 {
151 	if (!g_shutdown_sig_received) {
152 		g_shutdown_sig_received = true;
153 		spdk_app_start_shutdown();
154 	}
155 }
156 
157 static int
158 app_opts_validate(const char *app_opts)
159 {
160 	int i = 0, j;
161 
162 	for (i = 0; app_opts[i] != '\0'; i++) {
163 		/* ignore getopt control characters */
164 		if (app_opts[i] == ':' || app_opts[i] == '+' || app_opts[i] == '-') {
165 			continue;
166 		}
167 
168 		for (j = 0; SPDK_APP_GETOPT_STRING[j] != '\0'; j++) {
169 			if (app_opts[i] == SPDK_APP_GETOPT_STRING[j]) {
170 				return app_opts[i];
171 			}
172 		}
173 	}
174 	return 0;
175 }
176 
177 void
178 spdk_app_opts_init(struct spdk_app_opts *opts, size_t opts_size)
179 {
180 	if (!opts) {
181 		SPDK_ERRLOG("opts should not be NULL\n");
182 		return;
183 	}
184 
185 	if (!opts_size) {
186 		SPDK_ERRLOG("opts_size should not be zero value\n");
187 		return;
188 	}
189 
190 	memset(opts, 0, opts_size);
191 	opts->opts_size = opts_size;
192 
193 #define SET_FIELD(field, value) \
194 	if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= opts_size) { \
195 		opts->field = value; \
196 	} \
197 
198 	SET_FIELD(enable_coredump, true);
199 	SET_FIELD(shm_id, -1);
200 	SET_FIELD(mem_size, SPDK_APP_DPDK_DEFAULT_MEM_SIZE);
201 	SET_FIELD(main_core, SPDK_APP_DPDK_DEFAULT_MAIN_CORE);
202 	SET_FIELD(mem_channel, SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL);
203 	SET_FIELD(reactor_mask, SPDK_APP_DPDK_DEFAULT_CORE_MASK);
204 	SET_FIELD(base_virtaddr, SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR);
205 	SET_FIELD(print_level, SPDK_APP_DEFAULT_LOG_PRINT_LEVEL);
206 	SET_FIELD(rpc_addr, SPDK_DEFAULT_RPC_ADDR);
207 	SET_FIELD(num_entries, SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
208 	SET_FIELD(delay_subsystem_init, false);
209 	SET_FIELD(disable_signal_handlers, false);
210 	SET_FIELD(msg_mempool_size, SPDK_DEFAULT_MSG_MEMPOOL_SIZE);
211 	SET_FIELD(rpc_allowlist, NULL);
212 #undef SET_FIELD
213 }
214 
215 static int
216 app_setup_signal_handlers(struct spdk_app_opts *opts)
217 {
218 	struct sigaction	sigact;
219 	sigset_t		sigmask;
220 	int			rc;
221 
222 	sigemptyset(&sigmask);
223 	memset(&sigact, 0, sizeof(sigact));
224 	sigemptyset(&sigact.sa_mask);
225 
226 	sigact.sa_handler = SIG_IGN;
227 	rc = sigaction(SIGPIPE, &sigact, NULL);
228 	if (rc < 0) {
229 		SPDK_ERRLOG("sigaction(SIGPIPE) failed\n");
230 		return rc;
231 	}
232 
233 	/* Install the same handler for SIGINT and SIGTERM */
234 	g_shutdown_sig_received = false;
235 	sigact.sa_handler = __shutdown_signal;
236 	rc = sigaction(SIGINT, &sigact, NULL);
237 	if (rc < 0) {
238 		SPDK_ERRLOG("sigaction(SIGINT) failed\n");
239 		return rc;
240 	}
241 	sigaddset(&sigmask, SIGINT);
242 
243 	rc = sigaction(SIGTERM, &sigact, NULL);
244 	if (rc < 0) {
245 		SPDK_ERRLOG("sigaction(SIGTERM) failed\n");
246 		return rc;
247 	}
248 	sigaddset(&sigmask, SIGTERM);
249 
250 	pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
251 
252 	return 0;
253 }
254 
255 static void
256 app_start_application(void)
257 {
258 	assert(spdk_get_thread() == g_app_thread);
259 
260 	g_start_fn(g_start_arg);
261 }
262 
263 static void
264 app_start_rpc(int rc, void *arg1)
265 {
266 	if (rc) {
267 		spdk_app_stop(rc);
268 		return;
269 	}
270 
271 	spdk_rpc_set_allowlist(g_spdk_app.rpc_allowlist);
272 
273 	rc = spdk_rpc_initialize(g_spdk_app.rpc_addr);
274 	if (rc) {
275 		spdk_app_stop(rc);
276 		return;
277 	}
278 
279 	if (!g_delay_subsystem_init) {
280 		spdk_rpc_set_state(SPDK_RPC_RUNTIME);
281 		app_start_application();
282 	}
283 }
284 
285 static int
286 app_opts_add_pci_addr(struct spdk_app_opts *opts, struct spdk_pci_addr **list, char *bdf)
287 {
288 	struct spdk_pci_addr *tmp = *list;
289 	size_t i = opts->num_pci_addr;
290 
291 	tmp = realloc(tmp, sizeof(*tmp) * (i + 1));
292 	if (tmp == NULL) {
293 		SPDK_ERRLOG("realloc error\n");
294 		return -ENOMEM;
295 	}
296 
297 	*list = tmp;
298 	if (spdk_pci_addr_parse(*list + i, bdf) < 0) {
299 		SPDK_ERRLOG("Invalid address %s\n", bdf);
300 		return -EINVAL;
301 	}
302 
303 	opts->num_pci_addr++;
304 	return 0;
305 }
306 
307 static int
308 app_setup_env(struct spdk_app_opts *opts)
309 {
310 	struct spdk_env_opts env_opts = {};
311 	int rc;
312 
313 	if (opts == NULL) {
314 		rc = spdk_env_init(NULL);
315 		if (rc != 0) {
316 			SPDK_ERRLOG("Unable to reinitialize SPDK env\n");
317 		}
318 
319 		return rc;
320 	}
321 
322 	spdk_env_opts_init(&env_opts);
323 
324 	env_opts.name = opts->name;
325 	env_opts.core_mask = opts->reactor_mask;
326 	env_opts.shm_id = opts->shm_id;
327 	env_opts.mem_channel = opts->mem_channel;
328 	env_opts.main_core = opts->main_core;
329 	env_opts.mem_size = opts->mem_size;
330 	env_opts.hugepage_single_segments = opts->hugepage_single_segments;
331 	env_opts.unlink_hugepage = opts->unlink_hugepage;
332 	env_opts.hugedir = opts->hugedir;
333 	env_opts.no_pci = opts->no_pci;
334 	env_opts.num_pci_addr = opts->num_pci_addr;
335 	env_opts.pci_blocked = opts->pci_blocked;
336 	env_opts.pci_allowed = opts->pci_allowed;
337 	env_opts.base_virtaddr = opts->base_virtaddr;
338 	env_opts.env_context = opts->env_context;
339 	env_opts.iova_mode = opts->iova_mode;
340 
341 	rc = spdk_env_init(&env_opts);
342 	free(env_opts.pci_blocked);
343 	free(env_opts.pci_allowed);
344 
345 	if (rc < 0) {
346 		SPDK_ERRLOG("Unable to initialize SPDK env\n");
347 	}
348 
349 	return rc;
350 }
351 
352 static int
353 app_setup_trace(struct spdk_app_opts *opts)
354 {
355 	char		shm_name[64];
356 	uint64_t	tpoint_group_mask, tpoint_mask = -1ULL;
357 	char		*end = NULL, *tpoint_group_mask_str, *tpoint_group_str = NULL;
358 	char		*tp_g_str, *tpoint_group, *tpoints;
359 	bool		error_found = false;
360 	uint64_t	group_id;
361 
362 	if (opts->shm_id >= 0) {
363 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", opts->name, opts->shm_id);
364 	} else {
365 		snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", opts->name, (int)getpid());
366 	}
367 
368 	if (spdk_trace_init(shm_name, opts->num_entries) != 0) {
369 		return -1;
370 	}
371 
372 	if (opts->tpoint_group_mask == NULL) {
373 		return 0;
374 	}
375 
376 	tpoint_group_mask_str = strdup(opts->tpoint_group_mask);
377 	if (tpoint_group_mask_str == NULL) {
378 		SPDK_ERRLOG("Unable to get string of tpoint group mask from opts.\n");
379 		return -1;
380 	}
381 	/* Save a pointer to the original value of the tpoint group mask string
382 	 * to free later, because spdk_strsepq() modifies given char*. */
383 	tp_g_str = tpoint_group_mask_str;
384 	while ((tpoint_group_str = spdk_strsepq(&tpoint_group_mask_str, ",")) != NULL) {
385 		if (strchr(tpoint_group_str, ':')) {
386 			/* Get the tpoint group mask */
387 			tpoint_group = spdk_strsepq(&tpoint_group_str, ":");
388 			/* Get the tpoint mask inside that group */
389 			tpoints = spdk_strsepq(&tpoint_group_str, ":");
390 
391 			errno = 0;
392 			tpoint_group_mask = strtoull(tpoint_group, &end, 16);
393 			if (*end != '\0' || errno) {
394 				tpoint_group_mask = spdk_trace_create_tpoint_group_mask(tpoint_group);
395 				if (tpoint_group_mask == 0) {
396 					error_found = true;
397 					break;
398 				}
399 			}
400 			/* Check if tpoint group mask has only one bit set.
401 			 * This is to avoid enabling individual tpoints in
402 			 * more than one tracepoint group at once. */
403 			if (!spdk_u64_is_pow2(tpoint_group_mask)) {
404 				SPDK_ERRLOG("Tpoint group mask: %s contains multiple tpoint groups.\n", tpoint_group);
405 				SPDK_ERRLOG("This is not supported, to prevent from activating tpoints by mistake.\n");
406 				error_found = true;
407 				break;
408 			}
409 
410 			errno = 0;
411 			tpoint_mask = strtoull(tpoints, &end, 16);
412 			if (*end != '\0' || errno) {
413 				error_found = true;
414 				break;
415 			}
416 		} else {
417 			errno = 0;
418 			tpoint_group_mask = strtoull(tpoint_group_str, &end, 16);
419 			if (*end != '\0' || errno) {
420 				tpoint_group_mask = spdk_trace_create_tpoint_group_mask(tpoint_group_str);
421 				if (tpoint_group_mask == 0) {
422 					error_found = true;
423 					break;
424 				}
425 			}
426 			tpoint_mask = -1ULL;
427 		}
428 
429 		for (group_id = 0; group_id < SPDK_TRACE_MAX_GROUP_ID; ++group_id) {
430 			if (tpoint_group_mask & (1 << group_id)) {
431 				spdk_trace_set_tpoints(group_id, tpoint_mask);
432 			}
433 		}
434 	}
435 
436 	if (error_found) {
437 		SPDK_ERRLOG("invalid tpoint mask %s\n", opts->tpoint_group_mask);
438 		return -1;
439 	} else {
440 		SPDK_NOTICELOG("Tracepoint Group Mask %s specified.\n", opts->tpoint_group_mask);
441 		SPDK_NOTICELOG("Use 'spdk_trace -s %s %s %d' to capture a snapshot of events at runtime.\n",
442 			       opts->name,
443 			       opts->shm_id >= 0 ? "-i" : "-p",
444 			       opts->shm_id >= 0 ? opts->shm_id : getpid());
445 #if defined(__linux__)
446 		SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name);
447 #endif
448 	}
449 	free(tp_g_str);
450 
451 	return 0;
452 }
453 
454 static void
455 bootstrap_fn(void *arg1)
456 {
457 	int rc;
458 
459 	if (g_spdk_app.json_config_file) {
460 		g_delay_subsystem_init = false;
461 		spdk_subsystem_init_from_json_config(g_spdk_app.json_config_file, g_spdk_app.rpc_addr,
462 						     app_start_rpc,
463 						     NULL, !g_spdk_app.json_config_ignore_errors);
464 	} else {
465 		if (!g_delay_subsystem_init) {
466 			spdk_subsystem_init(app_start_rpc, NULL);
467 		} else {
468 			spdk_rpc_set_allowlist(g_spdk_app.rpc_allowlist);
469 
470 			rc = spdk_rpc_initialize(g_spdk_app.rpc_addr);
471 			if (rc) {
472 				spdk_app_stop(rc);
473 				return;
474 			}
475 		}
476 	}
477 }
478 
479 static void
480 app_copy_opts(struct spdk_app_opts *opts, struct spdk_app_opts *opts_user, size_t opts_size)
481 {
482 	spdk_app_opts_init(opts, sizeof(*opts));
483 	opts->opts_size = opts_size;
484 
485 #define SET_FIELD(field) \
486         if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= (opts->opts_size)) { \
487 		opts->field = opts_user->field; \
488 	} \
489 
490 	SET_FIELD(name);
491 	SET_FIELD(json_config_file);
492 	SET_FIELD(json_config_ignore_errors);
493 	SET_FIELD(rpc_addr);
494 	SET_FIELD(reactor_mask);
495 	SET_FIELD(tpoint_group_mask);
496 	SET_FIELD(shm_id);
497 	SET_FIELD(shutdown_cb);
498 	SET_FIELD(enable_coredump);
499 	SET_FIELD(mem_channel);
500 	SET_FIELD(main_core);
501 	SET_FIELD(mem_size);
502 	SET_FIELD(no_pci);
503 	SET_FIELD(hugepage_single_segments);
504 	SET_FIELD(unlink_hugepage);
505 	SET_FIELD(hugedir);
506 	SET_FIELD(print_level);
507 	SET_FIELD(num_pci_addr);
508 	SET_FIELD(pci_blocked);
509 	SET_FIELD(pci_allowed);
510 	SET_FIELD(iova_mode);
511 	SET_FIELD(delay_subsystem_init);
512 	SET_FIELD(num_entries);
513 	SET_FIELD(env_context);
514 	SET_FIELD(log);
515 	SET_FIELD(base_virtaddr);
516 	SET_FIELD(disable_signal_handlers);
517 	SET_FIELD(msg_mempool_size);
518 	SET_FIELD(rpc_allowlist);
519 
520 	/* You should not remove this statement, but need to update the assert statement
521 	 * if you add a new field, and also add a corresponding SET_FIELD statement */
522 	SPDK_STATIC_ASSERT(sizeof(struct spdk_app_opts) == 208, "Incorrect size");
523 
524 #undef SET_FIELD
525 }
526 
527 static void
528 unclaim_cpu_cores(void)
529 {
530 	char core_name[40];
531 	uint32_t i;
532 
533 	for (i = 0; i < MAX_CPU_CORES; i++) {
534 		if (g_core_locks[i] != -1) {
535 			snprintf(core_name, sizeof(core_name), "/var/tmp/spdk_cpu_lock_%03d", i);
536 			close(g_core_locks[i]);
537 			g_core_locks[i] = -1;
538 			unlink(core_name);
539 		}
540 	}
541 }
542 
543 static int
544 claim_cpu_cores(uint32_t *failed_core)
545 {
546 	char core_name[40];
547 	int core_fd, pid;
548 	int *core_map;
549 	uint32_t core;
550 
551 	struct flock core_lock = {
552 		.l_type = F_WRLCK,
553 		.l_whence = SEEK_SET,
554 		.l_start = 0,
555 		.l_len = 0,
556 	};
557 
558 	SPDK_ENV_FOREACH_CORE(core) {
559 		if (g_core_locks[core] != -1) {
560 			/* If this core is locked already, do not try lock it again. */
561 			continue;
562 		}
563 
564 		snprintf(core_name, sizeof(core_name), "/var/tmp/spdk_cpu_lock_%03d", core);
565 		core_fd = open(core_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
566 		if (core_fd == -1) {
567 			SPDK_ERRLOG("Could not open %s (%s).\n", core_name, spdk_strerror(errno));
568 			/* Return number of core we failed to claim. */
569 			goto error;
570 		}
571 
572 		if (ftruncate(core_fd, sizeof(int)) != 0) {
573 			SPDK_ERRLOG("Could not truncate %s (%s).\n", core_name, spdk_strerror(errno));
574 			close(core_fd);
575 			goto error;
576 		}
577 
578 		core_map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, core_fd, 0);
579 		if (core_map == MAP_FAILED) {
580 			SPDK_ERRLOG("Could not mmap core %s (%s).\n", core_name, spdk_strerror(errno));
581 			close(core_fd);
582 			goto error;
583 		}
584 
585 		if (fcntl(core_fd, F_SETLK, &core_lock) != 0) {
586 			pid = *core_map;
587 			SPDK_ERRLOG("Cannot create lock on core %" PRIu32 ", probably process %d has claimed it.\n",
588 				    core, pid);
589 			munmap(core_map, sizeof(int));
590 			close(core_fd);
591 			goto error;
592 		}
593 
594 		/* We write the PID to the core lock file so that other processes trying
595 		* to claim the same core will know what process is holding the lock. */
596 		*core_map = (int)getpid();
597 		munmap(core_map, sizeof(int));
598 		g_core_locks[core] = core_fd;
599 		/* Keep core_fd open to maintain the lock. */
600 	}
601 
602 	return 0;
603 
604 error:
605 	if (failed_core != NULL) {
606 		/* Set number of core we failed to claim. */
607 		*failed_core = core;
608 	}
609 	unclaim_cpu_cores();
610 	return -1;
611 }
612 
613 int
614 spdk_app_start(struct spdk_app_opts *opts_user, spdk_msg_fn start_fn,
615 	       void *arg1)
616 {
617 	int			rc;
618 	char			*tty;
619 	struct spdk_cpuset	tmp_cpumask = {};
620 	static bool		g_env_was_setup = false;
621 	struct spdk_app_opts opts_local = {};
622 	struct spdk_app_opts *opts = &opts_local;
623 	uint32_t i;
624 
625 	if (!opts_user) {
626 		SPDK_ERRLOG("opts_user should not be NULL\n");
627 		return 1;
628 	}
629 
630 	if (!opts_user->opts_size) {
631 		SPDK_ERRLOG("The opts_size in opts_user structure should not be zero value\n");
632 		return 1;
633 	}
634 
635 	if (opts_user->name == NULL) {
636 		SPDK_ERRLOG("spdk_app_opts::name not specified\n");
637 		return 1;
638 	}
639 
640 	app_copy_opts(opts, opts_user, opts_user->opts_size);
641 
642 	if (!start_fn) {
643 		SPDK_ERRLOG("start_fn should not be NULL\n");
644 		return 1;
645 	}
646 
647 	tty = ttyname(STDERR_FILENO);
648 	if (opts->print_level > SPDK_LOG_WARN &&
649 	    isatty(STDERR_FILENO) &&
650 	    tty &&
651 	    !strncmp(tty, "/dev/tty", strlen("/dev/tty"))) {
652 		printf("Warning: printing stderr to console terminal without -q option specified.\n");
653 		printf("Suggest using --silence-noticelog to disable logging to stderr and\n");
654 		printf("monitor syslog, or redirect stderr to a file.\n");
655 		printf("(Delaying for 10 seconds...)\n");
656 		sleep(10);
657 	}
658 
659 	spdk_log_set_print_level(opts->print_level);
660 
661 #ifndef SPDK_NO_RLIMIT
662 	if (opts->enable_coredump) {
663 		struct rlimit core_limits;
664 
665 		core_limits.rlim_cur = core_limits.rlim_max = SPDK_APP_DEFAULT_CORE_LIMIT;
666 		setrlimit(RLIMIT_CORE, &core_limits);
667 	}
668 #endif
669 
670 	memset(&g_spdk_app, 0, sizeof(g_spdk_app));
671 	g_spdk_app.json_config_file = opts->json_config_file;
672 	g_spdk_app.json_config_ignore_errors = opts->json_config_ignore_errors;
673 	g_spdk_app.rpc_addr = opts->rpc_addr;
674 	g_spdk_app.rpc_allowlist = opts->rpc_allowlist;
675 	g_spdk_app.shm_id = opts->shm_id;
676 	g_spdk_app.shutdown_cb = opts->shutdown_cb;
677 	g_spdk_app.rc = 0;
678 	g_spdk_app.stopped = false;
679 
680 	spdk_log_set_level(SPDK_APP_DEFAULT_LOG_LEVEL);
681 
682 	/* Pass NULL to app_setup_env if SPDK app has been set up, in order to
683 	 * indicate that this is a reinitialization.
684 	 */
685 	if (app_setup_env(g_env_was_setup ? NULL : opts) < 0) {
686 		return 1;
687 	}
688 
689 	spdk_log_open(opts->log);
690 
691 	/* Initialize each lock to -1 to indicate "empty" status */
692 	for (i = 0; i < MAX_CPU_CORES; i++) {
693 		g_core_locks[i] = -1;
694 	}
695 
696 	if (!g_disable_cpumask_locks) {
697 		if (claim_cpu_cores(NULL)) {
698 			SPDK_ERRLOG("Unable to acquire lock on assigned core mask - exiting.\n");
699 			return 1;
700 		}
701 	} else {
702 		SPDK_NOTICELOG("CPU core locks deactivated.\n");
703 	}
704 
705 	SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count());
706 
707 	if ((rc = spdk_reactors_init(opts->msg_mempool_size)) != 0) {
708 		SPDK_ERRLOG("Reactor Initialization failed: rc = %d\n", rc);
709 		return 1;
710 	}
711 
712 	spdk_cpuset_set_cpu(&tmp_cpumask, spdk_env_get_current_core(), true);
713 
714 	/* Now that the reactors have been initialized, we can create an
715 	 * initialization thread. */
716 	g_app_thread = spdk_thread_create("app_thread", &tmp_cpumask);
717 	if (!g_app_thread) {
718 		SPDK_ERRLOG("Unable to create an spdk_thread for initialization\n");
719 		return 1;
720 	}
721 
722 	/*
723 	 * Disable and ignore trace setup if setting num_entries
724 	 * to be 0.
725 	 *
726 	 * Note the call to app_setup_trace() is located here
727 	 * ahead of app_setup_signal_handlers().
728 	 * That's because there is not an easy/direct clean
729 	 * way of unwinding alloc'd resources that can occur
730 	 * in app_setup_signal_handlers().
731 	 */
732 	if (opts->num_entries != 0 && app_setup_trace(opts) != 0) {
733 		return 1;
734 	}
735 
736 	if (!opts->disable_signal_handlers && app_setup_signal_handlers(opts) != 0) {
737 		return 1;
738 	}
739 
740 	g_delay_subsystem_init = opts->delay_subsystem_init;
741 	g_start_fn = start_fn;
742 	g_start_arg = arg1;
743 
744 	spdk_thread_send_msg(g_app_thread, bootstrap_fn, NULL);
745 
746 	/* This blocks until spdk_app_stop is called */
747 	spdk_reactors_start();
748 
749 	g_env_was_setup = true;
750 
751 	return g_spdk_app.rc;
752 }
753 
754 void
755 spdk_app_fini(void)
756 {
757 	spdk_trace_cleanup();
758 	spdk_reactors_fini();
759 	spdk_env_fini();
760 	spdk_log_close();
761 	unclaim_cpu_cores();
762 }
763 
764 static void
765 _start_subsystem_fini(void *arg1)
766 {
767 	if (g_scheduling_in_progress) {
768 		spdk_thread_send_msg(g_app_thread, _start_subsystem_fini, NULL);
769 		return;
770 	}
771 
772 	spdk_subsystem_fini(spdk_reactors_stop, NULL);
773 }
774 
775 static void
776 app_stop(void *arg1)
777 {
778 	if (g_spdk_app.rc == 0) {
779 		g_spdk_app.rc = (int)(intptr_t)arg1;
780 	}
781 
782 	if (g_spdk_app.stopped) {
783 		SPDK_NOTICELOG("spdk_app_stop called twice\n");
784 		return;
785 	}
786 
787 	spdk_rpc_finish();
788 	g_spdk_app.stopped = true;
789 	_start_subsystem_fini(NULL);
790 }
791 
792 void
793 spdk_app_stop(int rc)
794 {
795 	if (rc) {
796 		SPDK_WARNLOG("spdk_app_stop'd on non-zero\n");
797 	}
798 
799 	/*
800 	 * We want to run spdk_subsystem_fini() from the same thread where spdk_subsystem_init()
801 	 * was called.
802 	 */
803 	spdk_thread_send_msg(g_app_thread, app_stop, (void *)(intptr_t)rc);
804 }
805 
806 struct spdk_thread *
807 _spdk_get_app_thread(void)
808 {
809 	return g_app_thread;
810 }
811 
812 static void
813 usage(void (*app_usage)(void))
814 {
815 	printf("%s [options]\n", g_executable_name);
816 	printf("options:\n");
817 	printf(" -c, --config <config>     JSON config file (default %s)\n",
818 	       g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none");
819 	printf("     --json <config>       JSON config file (default %s)\n",
820 	       g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none");
821 	printf("     --json-ignore-init-errors\n");
822 	printf("                           don't exit on invalid config entry\n");
823 	printf(" -d, --limit-coredump      do not set max coredump size to RLIM_INFINITY\n");
824 	printf(" -g, --single-file-segments\n");
825 	printf("                           force creating just one hugetlbfs file\n");
826 	printf(" -h, --help                show this usage\n");
827 	printf(" -i, --shm-id <id>         shared memory ID (optional)\n");
828 	printf(" -m, --cpumask <mask or list>    core mask (like 0xF) or core list of '[]' embraced (like [0,1,10]) for DPDK\n");
829 	printf(" -n, --mem-channels <num>  channel number of memory channels used for DPDK\n");
830 	printf(" -p, --main-core <id>      main (primary) core for DPDK\n");
831 	printf(" -r, --rpc-socket <path>   RPC listen address (default %s)\n", SPDK_DEFAULT_RPC_ADDR);
832 	printf(" -s, --mem-size <size>     memory size in MB for DPDK (default: ");
833 #ifndef __linux__
834 	if (g_default_opts.mem_size <= 0) {
835 		printf("all hugepage memory)\n");
836 	} else
837 #endif
838 	{
839 		printf("%dMB)\n", g_default_opts.mem_size >= 0 ? g_default_opts.mem_size : 0);
840 	}
841 	printf("     --disable-cpumask-locks    Disable CPU core lock files.\n");
842 	printf("     --silence-noticelog   disable notice level logging to stderr\n");
843 	printf(" -u, --no-pci              disable PCI access\n");
844 	printf("     --wait-for-rpc        wait for RPCs to initialize subsystems\n");
845 	printf("     --max-delay <num>     maximum reactor delay (in microseconds)\n");
846 	printf(" -B, --pci-blocked <bdf>\n");
847 	printf("                           pci addr to block (can be used more than once)\n");
848 	printf(" -R, --huge-unlink         unlink huge files after initialization\n");
849 	printf(" -v, --version             print SPDK version\n");
850 	printf(" -A, --pci-allowed <bdf>\n");
851 	printf("                           pci addr to allow (-B and -A cannot be used at the same time)\n");
852 	printf("     --huge-dir <path>     use a specific hugetlbfs mount to reserve memory from\n");
853 	printf("     --iova-mode <pa/va>   set IOVA mode ('pa' for IOVA_PA and 'va' for IOVA_VA)\n");
854 	printf("     --base-virtaddr <addr>      the base virtual address for DPDK (default: 0x200000000000)\n");
855 	printf("     --num-trace-entries <num>   number of trace entries for each core, must be power of 2, setting 0 to disable trace (default %d)\n",
856 	       SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
857 	printf("     --rpcs-allowed	   comma-separated list of permitted RPCS\n");
858 	printf("     --env-context         Opaque context for use of the env implementation\n");
859 	spdk_log_usage(stdout, "-L");
860 	spdk_trace_mask_usage(stdout, "-e");
861 	if (app_usage) {
862 		app_usage();
863 	}
864 }
865 
866 spdk_app_parse_args_rvals_t
867 spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
868 		    const char *app_getopt_str, const struct option *app_long_opts,
869 		    int (*app_parse)(int ch, char *arg),
870 		    void (*app_usage)(void))
871 {
872 	int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len;
873 	struct option *cmdline_options;
874 	char *cmdline_short_opts = NULL;
875 	char *shm_id_str = NULL;
876 	enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL;
877 	long int tmp;
878 
879 	memcpy(&g_default_opts, opts, sizeof(g_default_opts));
880 
881 	if (opts->json_config_file && access(opts->json_config_file, R_OK) != 0) {
882 		SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->json_config_file);
883 		opts->json_config_file = NULL;
884 	}
885 
886 	if (app_long_opts == NULL) {
887 		app_long_opts_len = 0;
888 	} else {
889 		for (app_long_opts_len = 0;
890 		     app_long_opts[app_long_opts_len].name != NULL;
891 		     app_long_opts_len++);
892 	}
893 
894 	global_long_opts_len = SPDK_COUNTOF(g_cmdline_options);
895 
896 	cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options));
897 	if (!cmdline_options) {
898 		SPDK_ERRLOG("Out of memory\n");
899 		return SPDK_APP_PARSE_ARGS_FAIL;
900 	}
901 
902 	memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options));
903 	if (app_long_opts) {
904 		memcpy(&cmdline_options[global_long_opts_len], app_long_opts,
905 		       app_long_opts_len * sizeof(*app_long_opts));
906 	}
907 
908 	if (app_getopt_str != NULL) {
909 		ch = app_opts_validate(app_getopt_str);
910 		if (ch) {
911 			SPDK_ERRLOG("Duplicated option '%c' between the generic and application specific spdk opts.\n",
912 				    ch);
913 			goto out;
914 		}
915 	}
916 
917 	cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
918 	if (!cmdline_short_opts) {
919 		SPDK_ERRLOG("Out of memory\n");
920 		goto out;
921 	}
922 
923 	g_executable_name = argv[0];
924 
925 	while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) {
926 		switch (ch) {
927 		case CONFIG_FILE_OPT_IDX:
928 		case JSON_CONFIG_OPT_IDX:
929 			opts->json_config_file = optarg;
930 			break;
931 		case JSON_CONFIG_IGNORE_INIT_ERRORS_IDX:
932 			opts->json_config_ignore_errors = true;
933 			break;
934 		case LIMIT_COREDUMP_OPT_IDX:
935 			opts->enable_coredump = false;
936 			break;
937 		case TPOINT_GROUP_OPT_IDX:
938 			opts->tpoint_group_mask = optarg;
939 			break;
940 		case SINGLE_FILE_SEGMENTS_OPT_IDX:
941 			opts->hugepage_single_segments = true;
942 			break;
943 		case HELP_OPT_IDX:
944 			usage(app_usage);
945 			retval = SPDK_APP_PARSE_ARGS_HELP;
946 			goto out;
947 		case SHM_ID_OPT_IDX:
948 			shm_id_str = optarg;
949 			/* a negative shm-id disables shared configuration file */
950 			if (optarg[0] == '-') {
951 				shm_id_str++;
952 			}
953 			/* check if the positive value of provided shm_id can be parsed as
954 			 * an integer
955 			 */
956 			opts->shm_id = spdk_strtol(shm_id_str, 0);
957 			if (opts->shm_id < 0) {
958 				SPDK_ERRLOG("Invalid shared memory ID %s\n", optarg);
959 				goto out;
960 			}
961 			if (optarg[0] == '-') {
962 				opts->shm_id = -opts->shm_id;
963 			}
964 			break;
965 		case CPUMASK_OPT_IDX:
966 			opts->reactor_mask = optarg;
967 			break;
968 		case DISABLE_CPUMASK_LOCKS_OPT_IDX:
969 			g_disable_cpumask_locks = true;
970 			break;
971 		case MEM_CHANNELS_OPT_IDX:
972 			opts->mem_channel = spdk_strtol(optarg, 0);
973 			if (opts->mem_channel < 0) {
974 				SPDK_ERRLOG("Invalid memory channel %s\n", optarg);
975 				goto out;
976 			}
977 			break;
978 		case MAIN_CORE_OPT_IDX:
979 			opts->main_core = spdk_strtol(optarg, 0);
980 			if (opts->main_core < 0) {
981 				SPDK_ERRLOG("Invalid main core %s\n", optarg);
982 				goto out;
983 			}
984 			break;
985 		case SILENCE_NOTICELOG_OPT_IDX:
986 			opts->print_level = SPDK_LOG_WARN;
987 			break;
988 		case RPC_SOCKET_OPT_IDX:
989 			opts->rpc_addr = optarg;
990 			break;
991 		case MEM_SIZE_OPT_IDX: {
992 			uint64_t mem_size_mb;
993 			bool mem_size_has_prefix;
994 
995 			rc = spdk_parse_capacity(optarg, &mem_size_mb, &mem_size_has_prefix);
996 			if (rc != 0) {
997 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
998 				usage(app_usage);
999 				goto out;
1000 			}
1001 
1002 			if (mem_size_has_prefix) {
1003 				/* the mem size is in MB by default, so if a prefix was
1004 				 * specified, we need to manually convert to MB.
1005 				 */
1006 				mem_size_mb /= 1024 * 1024;
1007 			}
1008 
1009 			if (mem_size_mb > INT_MAX) {
1010 				SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
1011 				usage(app_usage);
1012 				goto out;
1013 			}
1014 
1015 			opts->mem_size = (int) mem_size_mb;
1016 			break;
1017 		}
1018 		case NO_PCI_OPT_IDX:
1019 			opts->no_pci = true;
1020 			break;
1021 		case WAIT_FOR_RPC_OPT_IDX:
1022 			opts->delay_subsystem_init = true;
1023 			break;
1024 		case PCI_BLOCKED_OPT_IDX:
1025 			if (opts->pci_allowed) {
1026 				free(opts->pci_allowed);
1027 				opts->pci_allowed = NULL;
1028 				SPDK_ERRLOG("-B and -A cannot be used at the same time\n");
1029 				usage(app_usage);
1030 				goto out;
1031 			}
1032 
1033 			rc = app_opts_add_pci_addr(opts, &opts->pci_blocked, optarg);
1034 			if (rc != 0) {
1035 				free(opts->pci_blocked);
1036 				opts->pci_blocked = NULL;
1037 				goto out;
1038 			}
1039 			break;
1040 		case LOGFLAG_OPT_IDX:
1041 			rc = spdk_log_set_flag(optarg);
1042 			if (rc < 0) {
1043 				SPDK_ERRLOG("unknown flag\n");
1044 				usage(app_usage);
1045 				goto out;
1046 			}
1047 #ifdef DEBUG
1048 			opts->print_level = SPDK_LOG_DEBUG;
1049 #endif
1050 			break;
1051 		case HUGE_UNLINK_OPT_IDX:
1052 			opts->unlink_hugepage = true;
1053 			break;
1054 		case PCI_WHITELIST_OPT_IDX:
1055 			SPDK_WARNLOG("-W/--pci-whitelist is deprecated.  Use -A/--pci-allowed.\n");
1056 		/* fallthrough */
1057 		case PCI_ALLOWED_OPT_IDX:
1058 			if (opts->pci_blocked) {
1059 				free(opts->pci_blocked);
1060 				opts->pci_blocked = NULL;
1061 				SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
1062 				usage(app_usage);
1063 				goto out;
1064 			}
1065 
1066 			rc = app_opts_add_pci_addr(opts, &opts->pci_allowed, optarg);
1067 			if (rc != 0) {
1068 				free(opts->pci_allowed);
1069 				opts->pci_allowed = NULL;
1070 				goto out;
1071 			}
1072 			break;
1073 		case BASE_VIRTADDR_OPT_IDX:
1074 			tmp = spdk_strtoll(optarg, 0);
1075 			if (tmp <= 0) {
1076 				SPDK_ERRLOG("Invalid base-virtaddr %s\n", optarg);
1077 				usage(app_usage);
1078 				goto out;
1079 			}
1080 			opts->base_virtaddr = (uint64_t)tmp;
1081 			break;
1082 		case HUGE_DIR_OPT_IDX:
1083 			opts->hugedir = optarg;
1084 			break;
1085 		case IOVA_MODE_OPT_IDX:
1086 			opts->iova_mode = optarg;
1087 			break;
1088 		case NUM_TRACE_ENTRIES_OPT_IDX:
1089 			tmp = spdk_strtoll(optarg, 0);
1090 			if (tmp < 0) {
1091 				SPDK_ERRLOG("Invalid num-trace-entries %s\n", optarg);
1092 				usage(app_usage);
1093 				goto out;
1094 			}
1095 			opts->num_entries = (uint64_t)tmp;
1096 			if (opts->num_entries > 0 && opts->num_entries & (opts->num_entries - 1)) {
1097 				SPDK_ERRLOG("num-trace-entries must be power of 2\n");
1098 				usage(app_usage);
1099 				goto out;
1100 			}
1101 			break;
1102 		case MAX_REACTOR_DELAY_OPT_IDX:
1103 			SPDK_ERRLOG("Deprecation warning: The maximum allowed latency parameter is no longer supported.\n");
1104 			break;
1105 		case ENV_CONTEXT_OPT_IDX:
1106 			opts->env_context = optarg;
1107 			break;
1108 		case RPCS_ALLOWED_OPT_IDX:
1109 			opts->rpc_allowlist = (const char **)spdk_strarray_from_string(optarg, ",");
1110 			if (opts->rpc_allowlist == NULL) {
1111 				SPDK_ERRLOG("Invalid --rpcs-allowed argument\n");
1112 				usage(app_usage);
1113 				goto out;
1114 			}
1115 			break;
1116 		case VERSION_OPT_IDX:
1117 			printf(SPDK_VERSION_STRING"\n");
1118 			retval = SPDK_APP_PARSE_ARGS_HELP;
1119 			goto out;
1120 		case '?':
1121 			/*
1122 			 * In the event getopt() above detects an option
1123 			 * in argv that is NOT in the getopt_str,
1124 			 * getopt() will return a '?' indicating failure.
1125 			 */
1126 			usage(app_usage);
1127 			goto out;
1128 		default:
1129 			rc = app_parse(ch, optarg);
1130 			if (rc) {
1131 				SPDK_ERRLOG("Parsing application specific arguments failed: %d\n", rc);
1132 				goto out;
1133 			}
1134 		}
1135 	}
1136 
1137 	if (opts->json_config_file && opts->delay_subsystem_init) {
1138 		SPDK_ERRLOG("JSON configuration file can't be used together with --wait-for-rpc.\n");
1139 		goto out;
1140 	}
1141 
1142 	retval = SPDK_APP_PARSE_ARGS_SUCCESS;
1143 out:
1144 	if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) {
1145 		free(opts->pci_blocked);
1146 		opts->pci_blocked = NULL;
1147 		free(opts->pci_allowed);
1148 		opts->pci_allowed = NULL;
1149 		spdk_strarray_free((char **)opts->rpc_allowlist);
1150 		opts->rpc_allowlist = NULL;
1151 	}
1152 	free(cmdline_short_opts);
1153 	free(cmdline_options);
1154 	return retval;
1155 }
1156 
1157 void
1158 spdk_app_usage(void)
1159 {
1160 	if (g_executable_name == NULL) {
1161 		SPDK_ERRLOG("%s not valid before calling spdk_app_parse_args()\n", __func__);
1162 		return;
1163 	}
1164 
1165 	usage(NULL);
1166 }
1167 
1168 static void
1169 rpc_framework_start_init_cpl(int rc, void *arg1)
1170 {
1171 	struct spdk_jsonrpc_request *request = arg1;
1172 
1173 	assert(spdk_get_thread() == g_app_thread);
1174 
1175 	if (rc) {
1176 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1177 						 "framework_initialization failed");
1178 		return;
1179 	}
1180 
1181 	spdk_rpc_set_state(SPDK_RPC_RUNTIME);
1182 	app_start_application();
1183 
1184 	spdk_jsonrpc_send_bool_response(request, true);
1185 }
1186 
1187 static void
1188 rpc_framework_start_init(struct spdk_jsonrpc_request *request,
1189 			 const struct spdk_json_val *params)
1190 {
1191 	if (params != NULL) {
1192 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1193 						 "framework_start_init requires no parameters");
1194 		return;
1195 	}
1196 
1197 	spdk_subsystem_init(rpc_framework_start_init_cpl, request);
1198 }
1199 SPDK_RPC_REGISTER("framework_start_init", rpc_framework_start_init, SPDK_RPC_STARTUP)
1200 
1201 struct subsystem_init_poller_ctx {
1202 	struct spdk_poller *init_poller;
1203 	struct spdk_jsonrpc_request *request;
1204 };
1205 
1206 static int
1207 rpc_subsystem_init_poller_ctx(void *ctx)
1208 {
1209 	struct subsystem_init_poller_ctx *poller_ctx = ctx;
1210 
1211 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1212 		spdk_jsonrpc_send_bool_response(poller_ctx->request, true);
1213 		spdk_poller_unregister(&poller_ctx->init_poller);
1214 		free(poller_ctx);
1215 	}
1216 
1217 	return SPDK_POLLER_BUSY;
1218 }
1219 
1220 static void
1221 rpc_framework_wait_init(struct spdk_jsonrpc_request *request,
1222 			const struct spdk_json_val *params)
1223 {
1224 	struct subsystem_init_poller_ctx *ctx;
1225 
1226 	if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1227 		spdk_jsonrpc_send_bool_response(request, true);
1228 	} else {
1229 		ctx = malloc(sizeof(struct subsystem_init_poller_ctx));
1230 		if (ctx == NULL) {
1231 			spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1232 							 "Unable to allocate memory for the request context\n");
1233 			return;
1234 		}
1235 		ctx->request = request;
1236 		ctx->init_poller = SPDK_POLLER_REGISTER(rpc_subsystem_init_poller_ctx, ctx, 0);
1237 	}
1238 }
1239 SPDK_RPC_REGISTER("framework_wait_init", rpc_framework_wait_init,
1240 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1241 
1242 static void
1243 rpc_framework_disable_cpumask_locks(struct spdk_jsonrpc_request *request,
1244 				    const struct spdk_json_val *params)
1245 {
1246 	if (params != NULL) {
1247 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1248 						 "framework_disable_cpumask_locks"
1249 						 "requires no arguments");
1250 		return;
1251 	}
1252 
1253 	unclaim_cpu_cores();
1254 	spdk_jsonrpc_send_bool_response(request, true);
1255 }
1256 SPDK_RPC_REGISTER("framework_disable_cpumask_locks", rpc_framework_disable_cpumask_locks,
1257 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1258 
1259 static void
1260 rpc_framework_enable_cpumask_locks(struct spdk_jsonrpc_request *request,
1261 				   const struct spdk_json_val *params)
1262 {
1263 	char msg[128];
1264 	int rc;
1265 	uint32_t failed_core;
1266 
1267 	if (params != NULL) {
1268 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1269 						 "framework_enable_cpumask_locks"
1270 						 "requires no arguments");
1271 		return;
1272 	}
1273 
1274 	rc = claim_cpu_cores(&failed_core);
1275 	if (rc) {
1276 		snprintf(msg, sizeof(msg), "Failed to claim CPU core: %" PRIu32, failed_core);
1277 		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
1278 		return;
1279 	}
1280 
1281 	spdk_jsonrpc_send_bool_response(request, true);
1282 }
1283 SPDK_RPC_REGISTER("framework_enable_cpumask_locks", rpc_framework_enable_cpumask_locks,
1284 		  SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1285