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