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