Branch data Line data Source code
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, 2023 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 : : #include "spdk/file.h"
23 : : #include "spdk/config.h"
24 : : #include "event_internal.h"
25 : :
26 : : #define SPDK_APP_DEFAULT_LOG_LEVEL SPDK_LOG_NOTICE
27 : : #define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL SPDK_LOG_INFO
28 : : #define SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES SPDK_DEFAULT_NUM_TRACE_ENTRIES
29 : :
30 : : #define SPDK_APP_DPDK_DEFAULT_MEM_SIZE -1
31 : : #define SPDK_APP_DPDK_DEFAULT_MAIN_CORE -1
32 : : #define SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL -1
33 : : #define SPDK_APP_DPDK_DEFAULT_CORE_MASK "0x1"
34 : : #define SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR 0x200000000000
35 : : #define SPDK_APP_DEFAULT_CORE_LIMIT 0x140000000 /* 5 GiB */
36 : :
37 : : /* For core counts <= 63, the message memory pool size is set to
38 : : * SPDK_DEFAULT_MSG_MEMPOOL_SIZE.
39 : : * For core counts > 63, the message memory pool size is dependend on
40 : : * number of cores. Per core, it is calculated as SPDK_MSG_MEMPOOL_CACHE_SIZE
41 : : * multiplied by factor of 4 to have space for multiple spdk threads running
42 : : * on single core (e.g iscsi + nvmf + vhost ). */
43 : : #define SPDK_APP_PER_CORE_MSG_MEMPOOL_SIZE (4 * SPDK_MSG_MEMPOOL_CACHE_SIZE)
44 : :
45 : : struct spdk_app {
46 : : void *json_data;
47 : : size_t json_data_size;
48 : : bool json_config_ignore_errors;
49 : : bool stopped;
50 : : const char *rpc_addr;
51 : : const char **rpc_allowlist;
52 : : FILE *rpc_log_file;
53 : : enum spdk_log_level rpc_log_level;
54 : : int shm_id;
55 : : spdk_app_shutdown_cb shutdown_cb;
56 : : int rc;
57 : : };
58 : :
59 : : static struct spdk_app g_spdk_app;
60 : : static spdk_msg_fn g_start_fn = NULL;
61 : : static void *g_start_arg = NULL;
62 : : static bool g_delay_subsystem_init = false;
63 : : static bool g_shutdown_sig_received = false;
64 : : static char *g_executable_name;
65 : : static struct spdk_app_opts g_default_opts;
66 : : static bool g_disable_cpumask_locks = false;
67 : :
68 : : static int g_core_locks[SPDK_CONFIG_MAX_LCORES];
69 : :
70 : : static struct {
71 : : uint64_t irq;
72 : : uint64_t usr;
73 : : uint64_t sys;
74 : : } g_initial_stat[SPDK_CONFIG_MAX_LCORES];
75 : :
76 : : int
77 : 0 : spdk_app_get_shm_id(void)
78 : : {
79 : 0 : return g_spdk_app.shm_id;
80 : : }
81 : :
82 : : /* append one empty option to indicate the end of the array */
83 : : static const struct option g_cmdline_options[] = {
84 : : #define CONFIG_FILE_OPT_IDX 'c'
85 : : {"config", required_argument, NULL, CONFIG_FILE_OPT_IDX},
86 : : #define LIMIT_COREDUMP_OPT_IDX 'd'
87 : : {"limit-coredump", no_argument, NULL, LIMIT_COREDUMP_OPT_IDX},
88 : : #define TPOINT_GROUP_OPT_IDX 'e'
89 : : {"tpoint-group", required_argument, NULL, TPOINT_GROUP_OPT_IDX},
90 : : #define SINGLE_FILE_SEGMENTS_OPT_IDX 'g'
91 : : {"single-file-segments", no_argument, NULL, SINGLE_FILE_SEGMENTS_OPT_IDX},
92 : : #define HELP_OPT_IDX 'h'
93 : : {"help", no_argument, NULL, HELP_OPT_IDX},
94 : : #define SHM_ID_OPT_IDX 'i'
95 : : {"shm-id", required_argument, NULL, SHM_ID_OPT_IDX},
96 : : #define CPUMASK_OPT_IDX 'm'
97 : : {"cpumask", required_argument, NULL, CPUMASK_OPT_IDX},
98 : : #define MEM_CHANNELS_OPT_IDX 'n'
99 : : {"mem-channels", required_argument, NULL, MEM_CHANNELS_OPT_IDX},
100 : : #define MAIN_CORE_OPT_IDX 'p'
101 : : {"main-core", required_argument, NULL, MAIN_CORE_OPT_IDX},
102 : : #define RPC_SOCKET_OPT_IDX 'r'
103 : : {"rpc-socket", required_argument, NULL, RPC_SOCKET_OPT_IDX},
104 : : #define MEM_SIZE_OPT_IDX 's'
105 : : {"mem-size", required_argument, NULL, MEM_SIZE_OPT_IDX},
106 : : #define NO_PCI_OPT_IDX 'u'
107 : : {"no-pci", no_argument, NULL, NO_PCI_OPT_IDX},
108 : : #define VERSION_OPT_IDX 'v'
109 : : {"version", no_argument, NULL, VERSION_OPT_IDX},
110 : : #define PCI_BLOCKED_OPT_IDX 'B'
111 : : {"pci-blocked", required_argument, NULL, PCI_BLOCKED_OPT_IDX},
112 : : #define LOGFLAG_OPT_IDX 'L'
113 : : {"logflag", required_argument, NULL, LOGFLAG_OPT_IDX},
114 : : #define HUGE_UNLINK_OPT_IDX 'R'
115 : : {"huge-unlink", no_argument, NULL, HUGE_UNLINK_OPT_IDX},
116 : : #define PCI_ALLOWED_OPT_IDX 'A'
117 : : {"pci-allowed", required_argument, NULL, PCI_ALLOWED_OPT_IDX},
118 : : #define INTERRUPT_MODE_OPT_IDX 256
119 : : {"interrupt-mode", no_argument, NULL, INTERRUPT_MODE_OPT_IDX},
120 : : #define SILENCE_NOTICELOG_OPT_IDX 257
121 : : {"silence-noticelog", no_argument, NULL, SILENCE_NOTICELOG_OPT_IDX},
122 : : #define WAIT_FOR_RPC_OPT_IDX 258
123 : : {"wait-for-rpc", no_argument, NULL, WAIT_FOR_RPC_OPT_IDX},
124 : : #define HUGE_DIR_OPT_IDX 259
125 : : {"huge-dir", required_argument, NULL, HUGE_DIR_OPT_IDX},
126 : : #define NUM_TRACE_ENTRIES_OPT_IDX 260
127 : : {"num-trace-entries", required_argument, NULL, NUM_TRACE_ENTRIES_OPT_IDX},
128 : : #define JSON_CONFIG_OPT_IDX 262
129 : : {"json", required_argument, NULL, JSON_CONFIG_OPT_IDX},
130 : : #define JSON_CONFIG_IGNORE_INIT_ERRORS_IDX 263
131 : : {"json-ignore-init-errors", no_argument, NULL, JSON_CONFIG_IGNORE_INIT_ERRORS_IDX},
132 : : #define IOVA_MODE_OPT_IDX 264
133 : : {"iova-mode", required_argument, NULL, IOVA_MODE_OPT_IDX},
134 : : #define BASE_VIRTADDR_OPT_IDX 265
135 : : {"base-virtaddr", required_argument, NULL, BASE_VIRTADDR_OPT_IDX},
136 : : #define ENV_CONTEXT_OPT_IDX 266
137 : : {"env-context", required_argument, NULL, ENV_CONTEXT_OPT_IDX},
138 : : #define DISABLE_CPUMASK_LOCKS_OPT_IDX 267
139 : : {"disable-cpumask-locks", no_argument, NULL, DISABLE_CPUMASK_LOCKS_OPT_IDX},
140 : : #define RPCS_ALLOWED_OPT_IDX 268
141 : : {"rpcs-allowed", required_argument, NULL, RPCS_ALLOWED_OPT_IDX},
142 : : #define ENV_VF_TOKEN_OPT_IDX 269
143 : : {"vfio-vf-token", required_argument, NULL, ENV_VF_TOKEN_OPT_IDX},
144 : : #define MSG_MEMPOOL_SIZE_OPT_IDX 270
145 : : {"msg-mempool-size", required_argument, NULL, MSG_MEMPOOL_SIZE_OPT_IDX},
146 : : #define LCORES_OPT_IDX 271
147 : : {"lcores", required_argument, NULL, LCORES_OPT_IDX},
148 : : #define NO_HUGE_OPT_IDX 272
149 : : {"no-huge", no_argument, NULL, NO_HUGE_OPT_IDX},
150 : : #define NO_RPC_SERVER_OPT_IDX 273
151 : : {"no-rpc-server", no_argument, NULL, NO_RPC_SERVER_OPT_IDX},
152 : : #define ENFORCE_NUMA_OPT_IDX 274
153 : : {"enforce-numa", no_argument, NULL, ENFORCE_NUMA_OPT_IDX},
154 : : };
155 : :
156 : : static int
157 : 4033 : parse_proc_stat(unsigned int core, uint64_t *user, uint64_t *sys, uint64_t *irq)
158 : : {
159 : : FILE *f;
160 : 4033 : uint64_t i, soft_irq, cpu = 0;
161 : 4033 : int rc, found = 0;
162 : :
163 : 4033 : f = fopen("/proc/stat", "r");
164 [ - + ]: 4033 : if (!f) {
165 : 0 : return -1;
166 : : }
167 : :
168 [ + - ]: 13138 : for (i = 0; i <= core + 1; i++) {
169 : : /* scanf discards input with '*' in format,
170 : : * cpu;user;nice;system;idle;iowait;irq;softirq;steal;guest;guest_nice */
171 [ - + ]: 13138 : rc = fscanf(f, "cpu%li %li %*i %li %*i %*i %li %li %*i %*i %*i\n",
172 : : &cpu, user, sys, irq, &soft_irq);
173 [ - + ]: 13138 : if (rc != 5) {
174 : 0 : continue;
175 : : }
176 : :
177 : : /* some cores can be disabled, list may not be in order */
178 [ + + ]: 13138 : if (cpu == core) {
179 : 4033 : found = 1;
180 : 4033 : break;
181 : : }
182 : : }
183 : :
184 : 4033 : *irq += soft_irq;
185 : :
186 : 4033 : fclose(f);
187 [ + - ]: 4033 : return found ? 0 : -1;
188 : : }
189 : :
190 : : static int
191 : 3937 : init_proc_stat(unsigned int core)
192 : : {
193 : 1702 : uint64_t usr, sys, irq;
194 : :
195 [ - + ]: 3937 : if (core >= SPDK_CONFIG_MAX_LCORES) {
196 : 0 : return -1;
197 : : }
198 : :
199 [ - + ]: 3937 : if (parse_proc_stat(core, &usr, &sys, &irq) < 0) {
200 : 0 : return -1;
201 : : }
202 : :
203 : 3937 : g_initial_stat[core].irq = irq;
204 : 3937 : g_initial_stat[core].usr = usr;
205 : 3937 : g_initial_stat[core].sys = sys;
206 : :
207 : 3937 : return 0;
208 : : }
209 : :
210 : : int
211 : 96 : app_get_proc_stat(unsigned int core, uint64_t *usr, uint64_t *sys, uint64_t *irq)
212 : : {
213 : 0 : uint64_t _usr, _sys, _irq;
214 : :
215 [ - + ]: 96 : if (core >= SPDK_CONFIG_MAX_LCORES) {
216 : 0 : return -1;
217 : : }
218 : :
219 [ - + ]: 96 : if (parse_proc_stat(core, &_usr, &_sys, &_irq) < 0) {
220 : 0 : return -1;
221 : : }
222 : :
223 : 96 : *irq = _irq - g_initial_stat[core].irq;
224 : 96 : *usr = _usr - g_initial_stat[core].usr;
225 : 96 : *sys = _sys - g_initial_stat[core].sys;
226 : :
227 : 96 : return 0;
228 : : }
229 : :
230 : : static void
231 : 1505 : app_start_shutdown(void *ctx)
232 : : {
233 [ + + ]: 1505 : if (g_spdk_app.shutdown_cb) {
234 : 644 : g_spdk_app.shutdown_cb();
235 : 644 : g_spdk_app.shutdown_cb = NULL;
236 : : } else {
237 : 861 : spdk_app_stop(0);
238 : : }
239 : 1505 : }
240 : :
241 : : void
242 : 1505 : spdk_app_start_shutdown(void)
243 : : {
244 : 1505 : spdk_thread_send_critical_msg(spdk_thread_get_app_thread(), app_start_shutdown);
245 : 1505 : }
246 : :
247 : : static void
248 : 1500 : __shutdown_signal(int signo)
249 : : {
250 [ + + + - ]: 1500 : if (!g_shutdown_sig_received) {
251 : 1500 : g_shutdown_sig_received = true;
252 : 1500 : spdk_app_start_shutdown();
253 : : }
254 : 1500 : }
255 : :
256 : : static int
257 : 2888 : app_opts_validate(const char *app_opts)
258 : : {
259 : 2888 : int i = 0, j;
260 : :
261 [ + + ]: 35990 : for (i = 0; app_opts[i] != '\0'; i++) {
262 : : /* ignore getopt control characters */
263 [ + + + - : 33106 : if (app_opts[i] == ':' || app_opts[i] == '+' || app_opts[i] == '-') {
- + ]
264 : 13996 : continue;
265 : : }
266 : :
267 [ + + ]: 592290 : for (j = 0; SPDK_APP_GETOPT_STRING[j] != '\0'; j++) {
268 [ + + ]: 573184 : if (app_opts[i] == SPDK_APP_GETOPT_STRING[j]) {
269 : 4 : return app_opts[i];
270 : : }
271 : : }
272 : : }
273 : 2884 : return 0;
274 : : }
275 : :
276 : : static void
277 : 2855 : calculate_mempool_size(struct spdk_app_opts *opts,
278 : : struct spdk_app_opts *opts_user)
279 : : {
280 : 2855 : uint32_t core_count = spdk_env_get_core_count();
281 : :
282 [ + - ]: 2855 : if (!opts_user->msg_mempool_size) {
283 : : /* The user didn't specify msg_mempool_size, so let's calculate it.
284 : : Set the default (SPDK_DEFAULT_MSG_MEMPOOL_SIZE) if less than
285 : : 64 cores, and use 4k per core otherwise */
286 : 2855 : opts->msg_mempool_size = spdk_max(SPDK_DEFAULT_MSG_MEMPOOL_SIZE,
287 : : core_count * SPDK_APP_PER_CORE_MSG_MEMPOOL_SIZE);
288 : : } else {
289 : 0 : opts->msg_mempool_size = opts_user->msg_mempool_size;
290 : : }
291 : 2855 : }
292 : :
293 : : void
294 : 5810 : spdk_app_opts_init(struct spdk_app_opts *opts, size_t opts_size)
295 : : {
296 [ - + ]: 5810 : if (!opts) {
297 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
298 : 0 : return;
299 : : }
300 : :
301 [ - + ]: 5810 : if (!opts_size) {
302 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
303 : 0 : return;
304 : : }
305 : :
306 [ - + ]: 5810 : memset(opts, 0, opts_size);
307 : 5810 : opts->opts_size = opts_size;
308 : :
309 : : #define SET_FIELD(field, value) \
310 : : if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= opts_size) { \
311 : : opts->field = value; \
312 : : } \
313 : :
314 [ + - ]: 5810 : SET_FIELD(enable_coredump, true);
315 [ + - ]: 5810 : SET_FIELD(shm_id, -1);
316 [ + - ]: 5810 : SET_FIELD(mem_size, SPDK_APP_DPDK_DEFAULT_MEM_SIZE);
317 [ + - ]: 5810 : SET_FIELD(main_core, SPDK_APP_DPDK_DEFAULT_MAIN_CORE);
318 [ + - ]: 5810 : SET_FIELD(mem_channel, SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL);
319 [ + - ]: 5810 : SET_FIELD(base_virtaddr, SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR);
320 [ + - ]: 5810 : SET_FIELD(print_level, SPDK_APP_DEFAULT_LOG_PRINT_LEVEL);
321 [ + - ]: 5810 : SET_FIELD(rpc_addr, SPDK_DEFAULT_RPC_ADDR);
322 [ + - ]: 5810 : SET_FIELD(num_entries, SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
323 [ + - ]: 5810 : SET_FIELD(delay_subsystem_init, false);
324 [ + - ]: 5810 : SET_FIELD(disable_signal_handlers, false);
325 [ + - ]: 5810 : SET_FIELD(interrupt_mode, false);
326 [ + - ]: 5810 : SET_FIELD(enforce_numa, false);
327 : : /* Don't set msg_mempool_size here, it is set or calculated later */
328 [ + - ]: 5810 : SET_FIELD(rpc_allowlist, NULL);
329 [ + - ]: 5810 : SET_FIELD(rpc_log_file, NULL);
330 [ + - ]: 5810 : SET_FIELD(rpc_log_level, SPDK_LOG_DISABLED);
331 : : #undef SET_FIELD
332 : : }
333 : :
334 : : static int
335 : 2817 : app_setup_signal_handlers(struct spdk_app_opts *opts)
336 : : {
337 : 1340 : struct sigaction sigact;
338 : 1340 : sigset_t sigmask;
339 : : int rc;
340 : :
341 [ - + ]: 2817 : sigemptyset(&sigmask);
342 [ - + ]: 2817 : memset(&sigact, 0, sizeof(sigact));
343 [ - + ]: 2817 : sigemptyset(&sigact.sa_mask);
344 : :
345 : 2817 : sigact.sa_handler = SIG_IGN;
346 : 2817 : rc = sigaction(SIGPIPE, &sigact, NULL);
347 [ - + ]: 2817 : if (rc < 0) {
348 : 0 : SPDK_ERRLOG("sigaction(SIGPIPE) failed\n");
349 : 0 : return rc;
350 : : }
351 : :
352 : : /* Install the same handler for SIGINT and SIGTERM */
353 : 2817 : g_shutdown_sig_received = false;
354 : 2817 : sigact.sa_handler = __shutdown_signal;
355 : 2817 : rc = sigaction(SIGINT, &sigact, NULL);
356 [ - + ]: 2817 : if (rc < 0) {
357 : 0 : SPDK_ERRLOG("sigaction(SIGINT) failed\n");
358 : 0 : return rc;
359 : : }
360 [ - + ]: 2817 : sigaddset(&sigmask, SIGINT);
361 : :
362 : 2817 : rc = sigaction(SIGTERM, &sigact, NULL);
363 [ - + ]: 2817 : if (rc < 0) {
364 : 0 : SPDK_ERRLOG("sigaction(SIGTERM) failed\n");
365 : 0 : return rc;
366 : : }
367 [ - + ]: 2817 : sigaddset(&sigmask, SIGTERM);
368 : :
369 : 2817 : pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
370 : :
371 : 2817 : return 0;
372 : : }
373 : :
374 : : static void
375 : 2742 : app_start_application(int rc, void *arg1)
376 : : {
377 [ - + ]: 2742 : assert(spdk_thread_is_app_thread(NULL));
378 : :
379 [ - + ]: 2742 : if (rc) {
380 : 0 : SPDK_ERRLOG("Failed to load subsystems for RUNTIME state with code: %d\n", rc);
381 : 0 : spdk_app_stop(rc);
382 : 0 : return;
383 : : }
384 : :
385 [ + + ]: 2742 : if (g_spdk_app.rpc_addr) {
386 : 1476 : spdk_rpc_server_resume(g_spdk_app.rpc_addr);
387 : : }
388 : :
389 : 2742 : g_start_fn(g_start_arg);
390 : : }
391 : :
392 : : static void
393 : 2742 : app_subsystem_init_done(int rc, void *arg1)
394 : : {
395 [ - + ]: 2742 : if (rc) {
396 : 0 : SPDK_ERRLOG("Subsystem initialization failed with code: %d\n", rc);
397 : 0 : spdk_app_stop(rc);
398 : 0 : return;
399 : : }
400 : :
401 : 2742 : spdk_rpc_set_allowlist(g_spdk_app.rpc_allowlist);
402 : 2742 : spdk_rpc_set_state(SPDK_RPC_RUNTIME);
403 : :
404 [ + + ]: 2742 : if (g_spdk_app.json_data) {
405 : : /* Load SPDK_RPC_RUNTIME RPCs from config file */
406 [ - + ]: 1045 : assert(spdk_rpc_get_state() == SPDK_RPC_RUNTIME);
407 : 1045 : spdk_subsystem_load_config(g_spdk_app.json_data, g_spdk_app.json_data_size,
408 : : app_start_application, NULL,
409 [ - + ]: 1045 : !g_spdk_app.json_config_ignore_errors);
410 : : } else {
411 : 1697 : app_start_application(0, NULL);
412 : : }
413 : : }
414 : :
415 : : static void
416 : 2816 : app_do_spdk_subsystem_init(int rc, void *arg1)
417 : : {
418 : 1339 : struct spdk_rpc_opts opts;
419 : :
420 [ + + ]: 2816 : if (rc) {
421 : 54 : spdk_app_stop(rc);
422 : 185 : return;
423 : : }
424 : :
425 [ + + ]: 2762 : if (g_spdk_app.rpc_addr) {
426 : 1496 : opts.size = SPDK_SIZEOF(&opts, log_level);
427 : 1496 : opts.log_file = g_spdk_app.rpc_log_file;
428 : 1496 : opts.log_level = g_spdk_app.rpc_log_level;
429 : :
430 : 1496 : rc = spdk_rpc_initialize(g_spdk_app.rpc_addr, &opts);
431 [ + + ]: 1496 : if (rc) {
432 : 20 : spdk_app_stop(rc);
433 : 20 : return;
434 : : }
435 [ + + + + ]: 1476 : if (g_delay_subsystem_init) {
436 : 157 : return;
437 : : }
438 : 1319 : spdk_rpc_server_pause(g_spdk_app.rpc_addr);
439 : : } else {
440 [ - + - + ]: 1266 : SPDK_DEBUGLOG(app_rpc, "RPC server not started\n");
441 : : }
442 : 2585 : spdk_subsystem_init(app_subsystem_init_done, NULL);
443 : : }
444 : :
445 : : static int
446 : 8 : app_opts_add_pci_addr(struct spdk_app_opts *opts, struct spdk_pci_addr **list, char *bdf)
447 : : {
448 : 8 : struct spdk_pci_addr *tmp = *list;
449 : 8 : size_t i = opts->num_pci_addr;
450 : :
451 : 8 : tmp = realloc(tmp, sizeof(*tmp) * (i + 1));
452 [ - + ]: 8 : if (tmp == NULL) {
453 : 0 : SPDK_ERRLOG("realloc error\n");
454 : 0 : return -ENOMEM;
455 : : }
456 : :
457 : 8 : *list = tmp;
458 [ - + ]: 8 : if (spdk_pci_addr_parse(*list + i, bdf) < 0) {
459 : 0 : SPDK_ERRLOG("Invalid address %s\n", bdf);
460 : 0 : return -EINVAL;
461 : : }
462 : :
463 : 8 : opts->num_pci_addr++;
464 : 8 : return 0;
465 : : }
466 : :
467 : : static int
468 : 2855 : app_setup_env(struct spdk_app_opts *opts)
469 : : {
470 : 2855 : struct spdk_env_opts env_opts = {};
471 : : int rc;
472 : :
473 [ + + ]: 2855 : if (opts == NULL) {
474 : 57 : rc = spdk_env_init_ext(NULL);
475 [ - + ]: 57 : if (rc != 0) {
476 : 0 : SPDK_ERRLOG("Unable to reinitialize SPDK env\n");
477 : : }
478 : :
479 : 57 : return rc;
480 : : }
481 : :
482 : 2798 : spdk_env_opts_init_ext(&env_opts, sizeof(env_opts));
483 : :
484 : 2798 : env_opts.name = opts->name;
485 : 2798 : env_opts.core_mask = opts->reactor_mask;
486 : 2798 : env_opts.lcore_map = opts->lcore_map;
487 : 2798 : env_opts.shm_id = opts->shm_id;
488 : 2798 : env_opts.mem_channel = opts->mem_channel;
489 : 2798 : env_opts.main_core = opts->main_core;
490 : 2798 : env_opts.mem_size = opts->mem_size;
491 [ - + ]: 2798 : env_opts.hugepage_single_segments = opts->hugepage_single_segments;
492 [ - + ]: 2798 : env_opts.unlink_hugepage = opts->unlink_hugepage;
493 : 2798 : env_opts.hugedir = opts->hugedir;
494 [ - + ]: 2798 : env_opts.no_pci = opts->no_pci;
495 : 2798 : env_opts.num_pci_addr = opts->num_pci_addr;
496 : 2798 : env_opts.pci_blocked = opts->pci_blocked;
497 : 2798 : env_opts.pci_allowed = opts->pci_allowed;
498 : 2798 : env_opts.base_virtaddr = opts->base_virtaddr;
499 : 2798 : env_opts.env_context = opts->env_context;
500 : 2798 : env_opts.iova_mode = opts->iova_mode;
501 : 2798 : env_opts.vf_token = opts->vf_token;
502 [ - + ]: 2798 : env_opts.no_huge = opts->no_huge;
503 [ - + ]: 2798 : env_opts.enforce_numa = opts->enforce_numa;
504 : :
505 : 2798 : rc = spdk_env_init_ext(&env_opts);
506 : 2798 : free(env_opts.pci_blocked);
507 : 2798 : free(env_opts.pci_allowed);
508 : :
509 [ - + ]: 2798 : if (rc < 0) {
510 : 0 : SPDK_ERRLOG("Unable to initialize SPDK env\n");
511 : : }
512 : :
513 : 2798 : return rc;
514 : : }
515 : :
516 : : static int
517 : 2817 : app_setup_trace(struct spdk_app_opts *opts)
518 : : {
519 : 1340 : char shm_name[64];
520 : 2817 : uint64_t tpoint_group_mask, tpoint_mask = -1ULL;
521 : 2817 : char *end = NULL, *tpoint_group_mask_str, *tpoint_group_str = NULL;
522 : : char *tp_g_str, *tpoint_group, *tpoints;
523 : 2817 : bool error_found = false;
524 : : uint64_t group_id;
525 : :
526 [ + + ]: 2817 : if (opts->shm_id >= 0) {
527 : 373 : snprintf(shm_name, sizeof(shm_name), "/%s%s%d", opts->name,
528 : : SPDK_TRACE_SHM_NAME_BASE, opts->shm_id);
529 : : } else {
530 : 2444 : snprintf(shm_name, sizeof(shm_name), "/%s%spid%d", opts->name,
531 : 2444 : SPDK_TRACE_SHM_NAME_BASE, (int)getpid());
532 : : }
533 : :
534 [ - + ]: 2817 : if (spdk_trace_init(shm_name, opts->num_entries, 0) != 0) {
535 : 0 : return -1;
536 : : }
537 : :
538 [ + + ]: 2817 : if (opts->tpoint_group_mask == NULL) {
539 : 2591 : return 0;
540 : : }
541 : :
542 [ - + ]: 226 : tpoint_group_mask_str = strdup(opts->tpoint_group_mask);
543 [ - + ]: 226 : if (tpoint_group_mask_str == NULL) {
544 : 0 : SPDK_ERRLOG("Unable to get string of tpoint group mask from opts.\n");
545 : 0 : return -1;
546 : : }
547 : : /* Save a pointer to the original value of the tpoint group mask string
548 : : * to free later, because spdk_strsepq() modifies given char*. */
549 : 226 : tp_g_str = tpoint_group_mask_str;
550 [ + + ]: 452 : while ((tpoint_group_str = spdk_strsepq(&tpoint_group_mask_str, ",")) != NULL) {
551 [ - + - + ]: 226 : if (strchr(tpoint_group_str, ':')) {
552 : : /* Get the tpoint group mask */
553 : 0 : tpoint_group = spdk_strsepq(&tpoint_group_str, ":");
554 : : /* Get the tpoint mask inside that group */
555 : 0 : tpoints = spdk_strsepq(&tpoint_group_str, ":");
556 : :
557 : 0 : errno = 0;
558 [ # # ]: 0 : tpoint_group_mask = strtoull(tpoint_group, &end, 16);
559 [ # # # # ]: 0 : if (*end != '\0' || errno) {
560 : 0 : tpoint_group_mask = spdk_trace_create_tpoint_group_mask(tpoint_group);
561 [ # # ]: 0 : if (tpoint_group_mask == 0) {
562 : 0 : error_found = true;
563 : 0 : break;
564 : : }
565 : : }
566 : : /* Check if tpoint group mask has only one bit set.
567 : : * This is to avoid enabling individual tpoints in
568 : : * more than one tracepoint group at once. */
569 [ # # ]: 0 : if (!spdk_u64_is_pow2(tpoint_group_mask)) {
570 : 0 : SPDK_ERRLOG("Tpoint group mask: %s contains multiple tpoint groups.\n", tpoint_group);
571 : 0 : SPDK_ERRLOG("This is not supported, to prevent from activating tpoints by mistake.\n");
572 : 0 : error_found = true;
573 : 0 : break;
574 : : }
575 : :
576 : 0 : errno = 0;
577 [ # # ]: 0 : tpoint_mask = strtoull(tpoints, &end, 16);
578 [ # # # # ]: 0 : if (*end != '\0' || errno) {
579 : 0 : error_found = true;
580 : 0 : break;
581 : : }
582 : : } else {
583 : 226 : errno = 0;
584 [ - + ]: 226 : tpoint_group_mask = strtoull(tpoint_group_str, &end, 16);
585 [ + + - + ]: 226 : if (*end != '\0' || errno) {
586 : 23 : tpoint_group_mask = spdk_trace_create_tpoint_group_mask(tpoint_group_str);
587 [ - + ]: 23 : if (tpoint_group_mask == 0) {
588 : 0 : error_found = true;
589 : 0 : break;
590 : : }
591 : : }
592 : 226 : tpoint_mask = -1ULL;
593 : : }
594 : :
595 [ + + ]: 3842 : for (group_id = 0; group_id < SPDK_TRACE_MAX_GROUP_ID; ++group_id) {
596 [ + + + + ]: 3616 : if (tpoint_group_mask & (1 << group_id)) {
597 : 3218 : spdk_trace_set_tpoints(group_id, tpoint_mask);
598 : : }
599 : : }
600 : : }
601 : :
602 [ - + ]: 226 : if (error_found) {
603 : 0 : SPDK_ERRLOG("invalid tpoint mask %s\n", opts->tpoint_group_mask);
604 : 0 : free(tp_g_str);
605 : 0 : return -1;
606 : : } else {
607 : 226 : SPDK_NOTICELOG("Tracepoint Group Mask %s specified.\n", opts->tpoint_group_mask);
608 [ + + + + ]: 226 : SPDK_NOTICELOG("Use 'spdk_trace -s %s %s %d' to capture a snapshot of events at runtime.\n",
609 : : opts->name,
610 : : opts->shm_id >= 0 ? "-i" : "-p",
611 : : opts->shm_id >= 0 ? opts->shm_id : getpid());
612 : : #if defined(__linux__)
613 : 226 : SPDK_NOTICELOG("'spdk_trace' without parameters will also work if this is the only\n");
614 : 226 : SPDK_NOTICELOG("SPDK application currently running.\n");
615 : 226 : SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name);
616 : : #endif
617 : : }
618 : 226 : free(tp_g_str);
619 : :
620 : 226 : return 0;
621 : : }
622 : :
623 : : static void
624 : 2816 : bootstrap_fn(void *arg1)
625 : : {
626 : 2816 : spdk_rpc_set_allowlist(g_spdk_app.rpc_allowlist);
627 : :
628 [ + + ]: 2816 : if (g_spdk_app.json_data) {
629 : : /* Load SPDK_RPC_STARTUP RPCs from config file */
630 [ - + ]: 1099 : assert(spdk_rpc_get_state() == SPDK_RPC_STARTUP);
631 : 1099 : spdk_subsystem_load_config(g_spdk_app.json_data, g_spdk_app.json_data_size,
632 : : app_do_spdk_subsystem_init, NULL,
633 [ - + ]: 1099 : !g_spdk_app.json_config_ignore_errors);
634 : : } else {
635 : 1717 : app_do_spdk_subsystem_init(0, NULL);
636 : : }
637 : 2816 : }
638 : :
639 : : static void
640 : 2875 : app_copy_opts(struct spdk_app_opts *opts, struct spdk_app_opts *opts_user, size_t opts_size)
641 : : {
642 : 2875 : spdk_app_opts_init(opts, sizeof(*opts));
643 : 2875 : opts->opts_size = opts_size;
644 : :
645 : : #define SET_FIELD(field) \
646 : : if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= (opts->opts_size)) { \
647 : : opts->field = opts_user->field; \
648 : : } \
649 : :
650 [ + - ]: 2875 : SET_FIELD(name);
651 [ + - ]: 2875 : SET_FIELD(json_config_file);
652 [ + - - + ]: 2875 : SET_FIELD(json_config_ignore_errors);
653 [ + - ]: 2875 : SET_FIELD(rpc_addr);
654 [ + - ]: 2875 : SET_FIELD(reactor_mask);
655 [ + - ]: 2875 : SET_FIELD(lcore_map);
656 [ + - ]: 2875 : SET_FIELD(tpoint_group_mask);
657 [ + - ]: 2875 : SET_FIELD(shm_id);
658 [ + - ]: 2875 : SET_FIELD(shutdown_cb);
659 [ + - - + ]: 2875 : SET_FIELD(enable_coredump);
660 [ + - ]: 2875 : SET_FIELD(mem_channel);
661 [ + - ]: 2875 : SET_FIELD(main_core);
662 [ + - ]: 2875 : SET_FIELD(mem_size);
663 [ + - - + ]: 2875 : SET_FIELD(no_pci);
664 [ + - - + ]: 2875 : SET_FIELD(hugepage_single_segments);
665 [ + - - + ]: 2875 : SET_FIELD(unlink_hugepage);
666 [ + - - + ]: 2875 : SET_FIELD(no_huge);
667 [ + - ]: 2875 : SET_FIELD(hugedir);
668 [ + - ]: 2875 : SET_FIELD(print_level);
669 [ + - ]: 2875 : SET_FIELD(num_pci_addr);
670 [ + - ]: 2875 : SET_FIELD(pci_blocked);
671 [ + - ]: 2875 : SET_FIELD(pci_allowed);
672 [ + - ]: 2875 : SET_FIELD(iova_mode);
673 [ + - - + ]: 2875 : SET_FIELD(delay_subsystem_init);
674 [ + - ]: 2875 : SET_FIELD(num_entries);
675 [ + - ]: 2875 : SET_FIELD(env_context);
676 [ + - ]: 2875 : SET_FIELD(log);
677 [ + - ]: 2875 : SET_FIELD(base_virtaddr);
678 [ + - - + ]: 2875 : SET_FIELD(disable_signal_handlers);
679 [ + - - + ]: 2875 : SET_FIELD(interrupt_mode);
680 [ + - - + ]: 2875 : SET_FIELD(enforce_numa);
681 [ + - ]: 2875 : SET_FIELD(msg_mempool_size);
682 [ + - ]: 2875 : SET_FIELD(rpc_allowlist);
683 [ + - ]: 2875 : SET_FIELD(vf_token);
684 [ + - ]: 2875 : SET_FIELD(rpc_log_file);
685 [ + - ]: 2875 : SET_FIELD(rpc_log_level);
686 [ + - ]: 2875 : SET_FIELD(json_data);
687 [ + - ]: 2875 : SET_FIELD(json_data_size);
688 : :
689 : : /* You should not remove this statement, but need to update the assert statement
690 : : * if you add a new field, and also add a corresponding SET_FIELD statement */
691 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_app_opts) == 252, "Incorrect size");
692 : :
693 : : #undef SET_FIELD
694 : 2875 : }
695 : :
696 : : static int
697 : 2951 : unclaim_cpu_cores(uint32_t *failed_core)
698 : : {
699 : 1403 : char core_name[40];
700 : : uint32_t i;
701 : : int rc;
702 : :
703 [ + + ]: 377611 : for (i = 0; i < SPDK_CONFIG_MAX_LCORES; i++) {
704 [ + + ]: 374684 : if (g_core_locks[i] != -1) {
705 : 3860 : snprintf(core_name, sizeof(core_name), "/var/tmp/spdk_cpu_lock_%03d", i);
706 : 3860 : rc = close(g_core_locks[i]);
707 [ - + ]: 3860 : if (rc) {
708 : 0 : SPDK_ERRLOG("Failed to close lock fd for core %d, errno: %d\n", i, errno);
709 : 0 : goto error;
710 : : }
711 : :
712 : 3860 : g_core_locks[i] = -1;
713 : 3860 : rc = unlink(core_name);
714 [ + + ]: 3860 : if (rc) {
715 : 24 : SPDK_ERRLOG("Failed to unlink lock fd for core %d, errno: %d\n", i, errno);
716 : 24 : goto error;
717 : : }
718 : : }
719 : : }
720 : :
721 : 2927 : return 0;
722 : :
723 : 24 : error:
724 [ - + ]: 24 : if (failed_core != NULL) {
725 : : /* Set number of core we failed to claim. */
726 : 0 : *failed_core = i;
727 : : }
728 : 24 : return -1;
729 : : }
730 : :
731 : : static int
732 : 2833 : claim_cpu_cores(uint32_t *failed_core)
733 : : {
734 : 1349 : char core_name[40];
735 : : int core_fd, pid;
736 : : int *core_map;
737 : : uint32_t core;
738 : :
739 : 2833 : struct flock core_lock = {
740 : : .l_type = F_WRLCK,
741 : : .l_whence = SEEK_SET,
742 : : .l_start = 0,
743 : : .l_len = 0,
744 : : };
745 : :
746 [ + + ]: 6685 : SPDK_ENV_FOREACH_CORE(core) {
747 [ - + ]: 3909 : if (g_core_locks[core] != -1) {
748 : : /* If this core is locked already, do not try lock it again. */
749 : 0 : continue;
750 : : }
751 : :
752 : 3909 : snprintf(core_name, sizeof(core_name), "/var/tmp/spdk_cpu_lock_%03d", core);
753 : 3909 : core_fd = open(core_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
754 [ - + ]: 3909 : if (core_fd == -1) {
755 : 0 : SPDK_ERRLOG("Could not open %s (%s).\n", core_name, spdk_strerror(errno));
756 : : /* Return number of core we failed to claim. */
757 : 0 : goto error;
758 : : }
759 : :
760 [ - + ]: 3909 : if (ftruncate(core_fd, sizeof(int)) != 0) {
761 : 0 : SPDK_ERRLOG("Could not truncate %s (%s).\n", core_name, spdk_strerror(errno));
762 : 0 : close(core_fd);
763 : 0 : goto error;
764 : : }
765 : :
766 : 3909 : core_map = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, core_fd, 0);
767 [ - + ]: 3909 : if (core_map == MAP_FAILED) {
768 : 0 : SPDK_ERRLOG("Could not mmap core %s (%s).\n", core_name, spdk_strerror(errno));
769 : 0 : close(core_fd);
770 : 0 : goto error;
771 : : }
772 : :
773 [ + + ]: 3909 : if (fcntl(core_fd, F_SETLK, &core_lock) != 0) {
774 : 57 : pid = *core_map;
775 : 57 : SPDK_ERRLOG("Cannot create lock on core %" PRIu32 ", probably process %d has claimed it.\n",
776 : : core, pid);
777 : 57 : munmap(core_map, sizeof(int));
778 : 57 : close(core_fd);
779 : 57 : goto error;
780 : : }
781 : :
782 : : /* We write the PID to the core lock file so that other processes trying
783 : : * to claim the same core will know what process is holding the lock. */
784 : 3852 : *core_map = (int)getpid();
785 : 3852 : munmap(core_map, sizeof(int));
786 : 3852 : g_core_locks[core] = core_fd;
787 : : /* Keep core_fd open to maintain the lock. */
788 : : }
789 : :
790 : 2776 : return 0;
791 : :
792 : 57 : error:
793 [ + + ]: 57 : if (failed_core != NULL) {
794 : : /* Set number of core we failed to claim. */
795 : 19 : *failed_core = core;
796 : : }
797 : 57 : unclaim_cpu_cores(NULL);
798 : 57 : return -1;
799 : : }
800 : :
801 : : int
802 : 2875 : spdk_app_start(struct spdk_app_opts *opts_user, spdk_msg_fn start_fn,
803 : : void *arg1)
804 : : {
805 : : int rc;
806 : : char *tty;
807 : 2875 : struct spdk_cpuset tmp_cpumask = {};
808 : : static bool g_env_was_setup = false;
809 : 2875 : struct spdk_app_opts opts_local = {};
810 : 2875 : struct spdk_app_opts *opts = &opts_local;
811 : : uint32_t i, core;
812 : :
813 [ - + ]: 2875 : if (!opts_user) {
814 : 0 : SPDK_ERRLOG("opts_user should not be NULL\n");
815 : 0 : return 1;
816 : : }
817 : :
818 [ - + ]: 2875 : if (!opts_user->opts_size) {
819 : 0 : SPDK_ERRLOG("The opts_size in opts_user structure should not be zero value\n");
820 : 0 : return 1;
821 : : }
822 : :
823 [ - + ]: 2875 : if (opts_user->name == NULL) {
824 : 0 : SPDK_ERRLOG("spdk_app_opts::name not specified\n");
825 : 0 : return 1;
826 : : }
827 : :
828 : 2875 : app_copy_opts(opts, opts_user, opts_user->opts_size);
829 : :
830 [ - + ]: 2875 : if (!start_fn) {
831 : 0 : SPDK_ERRLOG("start_fn should not be NULL\n");
832 : 0 : return 1;
833 : : }
834 : :
835 [ + + + + : 2875 : if (!opts->rpc_addr && opts->delay_subsystem_init) {
+ + ]
836 : 20 : SPDK_ERRLOG("Cannot use '--wait-for-rpc' if no RPC server is going to be started.\n");
837 : 20 : return 1;
838 : : }
839 : :
840 [ + - + + ]: 2855 : if (!(opts->lcore_map || opts->reactor_mask)) {
841 : : /* Set default CPU mask */
842 : 658 : opts->reactor_mask = SPDK_APP_DPDK_DEFAULT_CORE_MASK;
843 : : }
844 : :
845 : 2855 : tty = ttyname(STDERR_FILENO);
846 [ + - - + ]: 5710 : if (opts->print_level > SPDK_LOG_WARN &&
847 [ - - ]: 2855 : isatty(STDERR_FILENO) &&
848 : 0 : tty &&
849 [ # # # # ]: 0 : !strncmp(tty, "/dev/tty", strlen("/dev/tty"))) {
850 : 0 : printf("Warning: printing stderr to console terminal without -q option specified.\n");
851 : 0 : printf("Suggest using --silence-noticelog to disable logging to stderr and\n");
852 : 0 : printf("monitor syslog, or redirect stderr to a file.\n");
853 : 0 : printf("(Delaying for 10 seconds...)\n");
854 : 0 : sleep(10);
855 : : }
856 : :
857 : 2855 : spdk_log_set_print_level(opts->print_level);
858 : :
859 : : #ifndef SPDK_NO_RLIMIT
860 [ + + + - ]: 2855 : if (opts->enable_coredump) {
861 : 1358 : struct rlimit core_limits;
862 : :
863 : 2855 : core_limits.rlim_cur = core_limits.rlim_max = SPDK_APP_DEFAULT_CORE_LIMIT;
864 : 2855 : setrlimit(RLIMIT_CORE, &core_limits);
865 : : }
866 : : #endif
867 : :
868 [ - + + + ]: 2855 : if (opts->interrupt_mode) {
869 : 1 : spdk_interrupt_mode_enable();
870 : : }
871 : :
872 : 2855 : memset(&g_spdk_app, 0, sizeof(g_spdk_app));
873 : :
874 [ - + ]: 2855 : g_spdk_app.json_config_ignore_errors = opts->json_config_ignore_errors;
875 : 2855 : g_spdk_app.rpc_addr = opts->rpc_addr;
876 : 2855 : g_spdk_app.rpc_allowlist = opts->rpc_allowlist;
877 : 2855 : g_spdk_app.rpc_log_file = opts->rpc_log_file;
878 : 2855 : g_spdk_app.rpc_log_level = opts->rpc_log_level;
879 : 2855 : g_spdk_app.shm_id = opts->shm_id;
880 : 2855 : g_spdk_app.shutdown_cb = opts->shutdown_cb;
881 : 2855 : g_spdk_app.rc = 0;
882 : 2855 : g_spdk_app.stopped = false;
883 : :
884 : 2855 : spdk_log_set_level(SPDK_APP_DEFAULT_LOG_LEVEL);
885 : :
886 : : /* Pass NULL to app_setup_env if SPDK app has been set up, in order to
887 : : * indicate that this is a reinitialization.
888 : : */
889 [ + + + + : 2855 : if (app_setup_env(g_env_was_setup ? NULL : opts) < 0) {
- + ]
890 : 0 : return 1;
891 : : }
892 : :
893 : : /* Calculate mempool size now that the env layer has configured the core count
894 : : * for the application */
895 : 2855 : calculate_mempool_size(opts, opts_user);
896 : :
897 : 2855 : spdk_log_open(opts->log);
898 : :
899 : : /* Initialize each lock to -1 to indicate "empty" status */
900 [ + + ]: 368295 : for (i = 0; i < SPDK_CONFIG_MAX_LCORES; i++) {
901 : 365440 : g_core_locks[i] = -1;
902 : : }
903 : :
904 [ + + + + ]: 2855 : if (!g_disable_cpumask_locks) {
905 [ + + ]: 2776 : if (claim_cpu_cores(NULL)) {
906 : 38 : SPDK_ERRLOG("Unable to acquire lock on assigned core mask - exiting.\n");
907 : 38 : return 1;
908 : : }
909 : : } else {
910 : 79 : SPDK_NOTICELOG("CPU core locks deactivated.\n");
911 : : }
912 : :
913 : 2817 : SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count());
914 : :
915 [ - + ]: 2817 : if ((rc = spdk_reactors_init(opts->msg_mempool_size)) != 0) {
916 : 0 : SPDK_ERRLOG("Reactor Initialization failed: rc = %d\n", rc);
917 : 0 : return 1;
918 : : }
919 : :
920 : 2817 : spdk_cpuset_set_cpu(&tmp_cpumask, spdk_env_get_current_core(), true);
921 : :
922 : : /* Now that the reactors have been initialized, we can create the app thread. */
923 : 2817 : spdk_thread_create("app_thread", &tmp_cpumask);
924 [ - + ]: 2817 : if (!spdk_thread_get_app_thread()) {
925 : 0 : SPDK_ERRLOG("Unable to create an spdk_thread for initialization\n");
926 : 0 : return 1;
927 : : }
928 : :
929 [ + + ]: 6754 : SPDK_ENV_FOREACH_CORE(core) {
930 : 3937 : rc = init_proc_stat(core);
931 [ - + ]: 3937 : if (rc) {
932 : 0 : SPDK_NOTICELOG("Unable to parse /proc/stat [core: %d].\n", core);
933 : : }
934 : : }
935 : : /*
936 : : * Disable and ignore trace setup if setting num_entries
937 : : * to be 0.
938 : : *
939 : : * Note the call to app_setup_trace() is located here
940 : : * ahead of app_setup_signal_handlers().
941 : : * That's because there is not an easy/direct clean
942 : : * way of unwinding alloc'd resources that can occur
943 : : * in app_setup_signal_handlers().
944 : : */
945 [ + - - + ]: 2817 : if (opts->num_entries != 0 && app_setup_trace(opts) != 0) {
946 : 0 : return 1;
947 : : }
948 : :
949 [ + + + + : 2817 : if (!opts->disable_signal_handlers && app_setup_signal_handlers(opts) != 0) {
- + ]
950 : 0 : return 1;
951 : : }
952 : :
953 [ - + ]: 2817 : g_delay_subsystem_init = opts->delay_subsystem_init;
954 : 2817 : g_start_fn = start_fn;
955 : 2817 : g_start_arg = arg1;
956 : :
957 [ + + ]: 2817 : if (opts->json_config_file != NULL) {
958 [ - + ]: 1100 : if (opts->json_data) {
959 : 0 : SPDK_ERRLOG("App opts json_config_file and json_data are mutually exclusive\n");
960 : 0 : return 1;
961 : : }
962 : :
963 : 1100 : g_spdk_app.json_data = spdk_posix_file_load_from_name(opts->json_config_file,
964 : : &g_spdk_app.json_data_size);
965 [ + + ]: 1100 : if (!g_spdk_app.json_data) {
966 : 1 : SPDK_ERRLOG("Read JSON configuration file %s failed: %s\n",
967 : : opts->json_config_file, spdk_strerror(errno));
968 : 1 : return 1;
969 : : }
970 [ - + ]: 1717 : } else if (opts->json_data) {
971 : 0 : g_spdk_app.json_data = calloc(1, opts->json_data_size);
972 [ # # ]: 0 : if (!g_spdk_app.json_data) {
973 : 0 : SPDK_ERRLOG("Failed to allocate JSON data buffer\n");
974 : 0 : return 1;
975 : : }
976 : :
977 [ # # # # ]: 0 : memcpy(g_spdk_app.json_data, opts->json_data, opts->json_data_size);
978 : 0 : g_spdk_app.json_data_size = opts->json_data_size;
979 : : }
980 : :
981 : 2816 : spdk_thread_send_msg(spdk_thread_get_app_thread(), bootstrap_fn, NULL);
982 : :
983 : : /* This blocks until spdk_app_stop is called */
984 : 2816 : spdk_reactors_start();
985 : :
986 : 2816 : g_env_was_setup = true;
987 : :
988 : 2816 : return g_spdk_app.rc;
989 : : }
990 : :
991 : : void
992 : 2875 : spdk_app_fini(void)
993 : : {
994 : 2875 : spdk_trace_cleanup();
995 : 2875 : spdk_reactors_fini();
996 : 2875 : spdk_env_fini();
997 : 2875 : spdk_log_close();
998 : 2875 : unclaim_cpu_cores(NULL);
999 : 2875 : }
1000 : :
1001 : : static void
1002 : 2816 : subsystem_fini_done(void *arg1)
1003 : : {
1004 : 2816 : spdk_rpc_finish();
1005 : 2816 : spdk_reactors_stop(NULL);
1006 : 2816 : }
1007 : :
1008 : : static void
1009 : 2951 : _start_subsystem_fini(void *arg1)
1010 : : {
1011 [ - + + + ]: 2951 : if (g_scheduling_in_progress) {
1012 : 135 : spdk_thread_send_msg(spdk_thread_get_app_thread(), _start_subsystem_fini, NULL);
1013 : 135 : return;
1014 : : }
1015 : :
1016 : 2816 : spdk_subsystem_fini(subsystem_fini_done, NULL);
1017 : : }
1018 : :
1019 : : static int
1020 : 20825 : log_deprecation_hits(void *ctx, struct spdk_deprecation *dep)
1021 : : {
1022 : 20825 : uint64_t hits = spdk_deprecation_get_hits(dep);
1023 : :
1024 [ + + ]: 20825 : if (hits == 0) {
1025 : 20776 : return 0;
1026 : : }
1027 : :
1028 : 49 : SPDK_WARNLOG("%s: deprecation '%s' scheduled for removal in %s hit %" PRIu64 " times\n",
1029 : : spdk_deprecation_get_tag(dep), spdk_deprecation_get_description(dep),
1030 : : spdk_deprecation_get_remove_release(dep), hits);
1031 : 49 : return 0;
1032 : : }
1033 : :
1034 : : static void
1035 : 2824 : app_stop(void *arg1)
1036 : : {
1037 : 2824 : free(g_spdk_app.json_data);
1038 : :
1039 [ + + ]: 2824 : if (g_spdk_app.rc == 0) {
1040 : 2816 : g_spdk_app.rc = (int)(intptr_t)arg1;
1041 : : }
1042 : :
1043 [ + + + + ]: 2824 : if (g_spdk_app.stopped) {
1044 : 8 : SPDK_NOTICELOG("spdk_app_stop called twice\n");
1045 : 8 : return;
1046 : : }
1047 : :
1048 : 2816 : g_spdk_app.stopped = true;
1049 : 2816 : spdk_log_for_each_deprecation(NULL, log_deprecation_hits);
1050 : 2816 : _start_subsystem_fini(NULL);
1051 : : }
1052 : :
1053 : : void
1054 : 2824 : spdk_app_stop(int rc)
1055 : : {
1056 [ + + ]: 2824 : if (rc) {
1057 : 201 : SPDK_WARNLOG("spdk_app_stop'd on non-zero\n");
1058 : : }
1059 : :
1060 : : /*
1061 : : * We want to run spdk_subsystem_fini() from the same thread where spdk_subsystem_init()
1062 : : * was called.
1063 : : */
1064 : 2824 : spdk_thread_send_msg(spdk_thread_get_app_thread(), app_stop, (void *)(intptr_t)rc);
1065 : 2824 : }
1066 : :
1067 : : static void
1068 : 41 : usage_memory_size(void)
1069 : : {
1070 : : #ifndef __linux__
1071 : : if (g_default_opts.mem_size <= 0) {
1072 : : printf("all hugepage memory)\n");
1073 : : } else
1074 : : #endif
1075 : : {
1076 [ - + ]: 41 : printf("%dMB)\n", g_default_opts.mem_size >= 0 ? g_default_opts.mem_size : 0);
1077 : : }
1078 : 41 : }
1079 : :
1080 : : static void
1081 : 41 : usage(void (*app_usage)(void))
1082 : : {
1083 [ - + ]: 41 : printf("%s [options]\n", g_executable_name);
1084 : : /* Keep entries inside categories roughly sorted by frequency of use. */
1085 [ - + ]: 41 : printf("\nCPU options:\n");
1086 [ - + ]: 41 : printf(" -m, --cpumask <mask or list> core mask (like 0xF) or core list of '[]' embraced for DPDK\n");
1087 [ - + ]: 41 : printf(" (like [0,1,10])\n");
1088 [ - + ]: 41 : printf(" --lcores <list> lcore to CPU mapping list. The list is in the format:\n");
1089 [ - + ]: 41 : printf(" <lcores[@CPUs]>[<,lcores[@CPUs]>...]\n");
1090 [ - + ]: 41 : printf(" lcores and cpus list are grouped by '(' and ')', e.g '--lcores \"(5-7)@(10-12)\"'\n");
1091 [ - + ]: 41 : printf(" Within the group, '-' is used for range separator,\n");
1092 [ - + ]: 41 : printf(" ',' is used for single number separator.\n");
1093 [ - + ]: 41 : printf(" '( )' can be omitted for single element group,\n");
1094 [ - + ]: 41 : printf(" '@' can be omitted if cpus and lcores have the same value\n");
1095 [ - + ]: 41 : printf(" --disable-cpumask-locks Disable CPU core lock files.\n");
1096 [ - + ]: 41 : printf(" --interrupt-mode set app to interrupt mode (Warning: CPU usage will be reduced only if all\n");
1097 [ - + ]: 41 : printf(" pollers in the app support interrupt mode)\n");
1098 [ - + ]: 41 : printf(" -p, --main-core <id> main (primary) core for DPDK\n");
1099 : :
1100 [ - + ]: 41 : printf("\nConfiguration options:\n");
1101 [ - + ]: 41 : printf(" -c, --config, --json <config> JSON config file\n");
1102 [ - + ]: 41 : printf(" -r, --rpc-socket <path> RPC listen address (default %s)\n", SPDK_DEFAULT_RPC_ADDR);
1103 [ - + ]: 41 : printf(" --no-rpc-server skip RPC server initialization. This option ignores '--rpc-socket' value.\n");
1104 [ - + ]: 41 : printf(" --wait-for-rpc wait for RPCs to initialize subsystems\n");
1105 [ - + ]: 41 : printf(" --rpcs-allowed comma-separated list of permitted RPCS\n");
1106 [ - + ]: 41 : printf(" --json-ignore-init-errors don't exit on invalid config entry\n");
1107 : :
1108 [ - + ]: 41 : printf("\nMemory options:\n");
1109 [ - + ]: 41 : printf(" --iova-mode <pa/va> set IOVA mode ('pa' for IOVA_PA and 'va' for IOVA_VA)\n");
1110 [ - + ]: 41 : printf(" --base-virtaddr <addr> the base virtual address for DPDK (default: 0x200000000000)\n");
1111 [ - + ]: 41 : printf(" --huge-dir <path> use a specific hugetlbfs mount to reserve memory from\n");
1112 [ - + ]: 41 : printf(" -R, --huge-unlink unlink huge files after initialization\n");
1113 [ - + ]: 41 : printf(" -n, --mem-channels <num> number of memory channels used for DPDK\n");
1114 [ - + ]: 41 : printf(" -s, --mem-size <size> memory size in MB for DPDK (default: ");
1115 : 41 : usage_memory_size();
1116 [ - + ]: 41 : printf(" --msg-mempool-size <size> global message memory pool size in count (default: %d)\n",
1117 : : SPDK_DEFAULT_MSG_MEMPOOL_SIZE);
1118 [ - + ]: 41 : printf(" --no-huge run without using hugepages\n");
1119 [ - + ]: 41 : printf(" --enforce-numa enforce NUMA allocations from the correct socket\n");
1120 [ - + ]: 41 : printf(" -i, --shm-id <id> shared memory ID (optional)\n");
1121 [ - + ]: 41 : printf(" -g, --single-file-segments force creating just one hugetlbfs file\n");
1122 : :
1123 [ - + ]: 41 : printf("\nPCI options:\n");
1124 [ - + ]: 41 : printf(" -A, --pci-allowed <bdf> pci addr to allow (-B and -A cannot be used at the same time)\n");
1125 [ - + ]: 41 : printf(" -B, --pci-blocked <bdf> pci addr to block (can be used more than once)\n");
1126 [ - + ]: 41 : printf(" -u, --no-pci disable PCI access\n");
1127 [ - + ]: 41 : printf(" --vfio-vf-token VF token (UUID) shared between SR-IOV PF and VFs for vfio_pci driver\n");
1128 : :
1129 [ - + ]: 41 : printf("\nLog options:\n");
1130 : 41 : spdk_log_usage(stdout, "-L");
1131 [ - + ]: 41 : printf(" --silence-noticelog disable notice level logging to stderr\n");
1132 : :
1133 [ - + ]: 41 : printf("\nTrace options:\n");
1134 [ - + ]: 41 : printf(" --num-trace-entries <num> number of trace entries for each core, must be power of 2,\n");
1135 [ - + ]: 41 : printf(" setting 0 to disable trace (default %d)\n",
1136 : : SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES);
1137 [ - + ]: 41 : printf(" Tracepoints vary in size and can use more than one trace entry.\n");
1138 : 41 : spdk_trace_mask_usage(stdout, "-e");
1139 : :
1140 [ - + ]: 41 : printf("\nOther options:\n");
1141 [ - + ]: 41 : printf(" -h, --help show this usage\n");
1142 [ - + ]: 41 : printf(" -v, --version print SPDK version\n");
1143 [ - + ]: 41 : printf(" -d, --limit-coredump do not set max coredump size to RLIM_INFINITY\n");
1144 [ - + ]: 41 : printf(" --env-context Opaque context for use of the env implementation\n");
1145 : :
1146 [ + + ]: 41 : if (app_usage) {
1147 [ - + ]: 29 : printf("\nApplication specific:\n");
1148 : 29 : app_usage();
1149 : : }
1150 : 41 : }
1151 : :
1152 : : spdk_app_parse_args_rvals_t
1153 : 2888 : spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts,
1154 : : const char *app_getopt_str, const struct option *app_long_opts,
1155 : : int (*app_parse)(int ch, char *arg),
1156 : : void (*app_usage)(void))
1157 : : {
1158 : 1398 : int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len;
1159 : : struct option *cmdline_options;
1160 : 2888 : char *cmdline_short_opts = NULL;
1161 : 2888 : char *shm_id_str = NULL;
1162 : 2888 : enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL;
1163 : : long int tmp;
1164 : :
1165 : 2888 : memcpy(&g_default_opts, opts, sizeof(g_default_opts));
1166 : :
1167 [ + + - + : 2888 : if (opts->json_config_file && access(opts->json_config_file, R_OK) != 0) {
- + ]
1168 : 0 : SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->json_config_file);
1169 : 0 : opts->json_config_file = NULL;
1170 : : }
1171 : :
1172 [ + + ]: 2888 : if (app_long_opts == NULL) {
1173 : 2363 : app_long_opts_len = 0;
1174 : : } else {
1175 : 525 : for (app_long_opts_len = 0;
1176 [ + + ]: 7302 : app_long_opts[app_long_opts_len].name != NULL;
1177 : 6777 : app_long_opts_len++);
1178 : : }
1179 : :
1180 : 2888 : global_long_opts_len = SPDK_COUNTOF(g_cmdline_options);
1181 : :
1182 : 2888 : cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options));
1183 [ - + ]: 2888 : if (!cmdline_options) {
1184 : 0 : SPDK_ERRLOG("Out of memory\n");
1185 : 0 : return SPDK_APP_PARSE_ARGS_FAIL;
1186 : : }
1187 : :
1188 [ - + - + ]: 2888 : memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options));
1189 [ + + ]: 2888 : if (app_long_opts) {
1190 [ - + - + ]: 525 : memcpy(&cmdline_options[global_long_opts_len], app_long_opts,
1191 : : app_long_opts_len * sizeof(*app_long_opts));
1192 : : }
1193 : :
1194 [ + - ]: 2888 : if (app_getopt_str != NULL) {
1195 : 2888 : ch = app_opts_validate(app_getopt_str);
1196 [ + + ]: 2888 : if (ch) {
1197 : 4 : SPDK_ERRLOG("Duplicated option '%c' between app-specific command line parameter and generic spdk opts.\n",
1198 : : ch);
1199 : 4 : goto out;
1200 : : }
1201 : :
1202 [ - + ]: 2884 : if (!app_parse) {
1203 : 0 : SPDK_ERRLOG("Parse function is required when app-specific command line parameters are provided.\n");
1204 : 0 : goto out;
1205 : : }
1206 : : }
1207 : :
1208 : 2884 : cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING);
1209 [ - + ]: 2884 : if (!cmdline_short_opts) {
1210 : 0 : SPDK_ERRLOG("Out of memory\n");
1211 : 0 : goto out;
1212 : : }
1213 : :
1214 : 2884 : g_executable_name = argv[0];
1215 : :
1216 [ + + - + : 14068 : while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) {
+ + ]
1217 [ + - + + : 11269 : switch (ch) {
+ + + + -
+ - + - +
+ - + - +
+ + + + +
+ - - - +
- + - + -
+ + ]
1218 : 1162 : case CONFIG_FILE_OPT_IDX:
1219 : : case JSON_CONFIG_OPT_IDX:
1220 : 1162 : opts->json_config_file = optarg;
1221 : 1162 : break;
1222 : 0 : case JSON_CONFIG_IGNORE_INIT_ERRORS_IDX:
1223 : 0 : opts->json_config_ignore_errors = true;
1224 : 0 : break;
1225 : 21 : case LIMIT_COREDUMP_OPT_IDX:
1226 : 21 : opts->enable_coredump = false;
1227 : 21 : break;
1228 : 222 : case TPOINT_GROUP_OPT_IDX:
1229 : 222 : opts->tpoint_group_mask = optarg;
1230 : 222 : break;
1231 : 34 : case SINGLE_FILE_SEGMENTS_OPT_IDX:
1232 : 34 : opts->hugepage_single_segments = true;
1233 : 34 : break;
1234 : 21 : case HELP_OPT_IDX:
1235 : 21 : usage(app_usage);
1236 : 21 : retval = SPDK_APP_PARSE_ARGS_HELP;
1237 : 21 : goto out;
1238 : 367 : case SHM_ID_OPT_IDX:
1239 : 367 : shm_id_str = optarg;
1240 : : /* a negative shm-id disables shared configuration file */
1241 [ - + ]: 367 : if (optarg[0] == '-') {
1242 : 0 : shm_id_str++;
1243 : : }
1244 : : /* check if the positive value of provided shm_id can be parsed as
1245 : : * an integer
1246 : : */
1247 : 367 : opts->shm_id = spdk_strtol(shm_id_str, 0);
1248 [ - + ]: 367 : if (opts->shm_id < 0) {
1249 : 0 : SPDK_ERRLOG("Invalid shared memory ID %s\n", optarg);
1250 : 0 : goto out;
1251 : : }
1252 [ - + ]: 367 : if (optarg[0] == '-') {
1253 : 0 : opts->shm_id = -opts->shm_id;
1254 : : }
1255 : 367 : break;
1256 : 998 : case CPUMASK_OPT_IDX:
1257 [ - + ]: 998 : if (opts->lcore_map) {
1258 : 0 : SPDK_ERRLOG("lcore map and core mask can't be set simultaneously\n");
1259 : 0 : goto out;
1260 : : }
1261 : 998 : opts->reactor_mask = optarg;
1262 : 998 : break;
1263 : 0 : case LCORES_OPT_IDX:
1264 [ # # ]: 0 : if (opts->reactor_mask) {
1265 : 0 : SPDK_ERRLOG("lcore map and core mask can't be set simultaneously\n");
1266 : 0 : goto out;
1267 : : }
1268 : 0 : opts->lcore_map = optarg;
1269 : 0 : break;
1270 : 79 : case DISABLE_CPUMASK_LOCKS_OPT_IDX:
1271 : 79 : g_disable_cpumask_locks = true;
1272 : 79 : break;
1273 : 0 : case MEM_CHANNELS_OPT_IDX:
1274 : 0 : opts->mem_channel = spdk_strtol(optarg, 0);
1275 [ # # ]: 0 : if (opts->mem_channel < 0) {
1276 : 0 : SPDK_ERRLOG("Invalid memory channel %s\n", optarg);
1277 : 0 : goto out;
1278 : : }
1279 : 0 : break;
1280 : 74 : case MAIN_CORE_OPT_IDX:
1281 : 74 : opts->main_core = spdk_strtol(optarg, 0);
1282 [ + + ]: 74 : if (opts->main_core < 0) {
1283 : 4 : SPDK_ERRLOG("Invalid main core %s\n", optarg);
1284 : 4 : goto out;
1285 : : }
1286 : 70 : break;
1287 : 0 : case SILENCE_NOTICELOG_OPT_IDX:
1288 : 0 : opts->print_level = SPDK_LOG_WARN;
1289 : 0 : break;
1290 : 714 : case RPC_SOCKET_OPT_IDX:
1291 : 714 : opts->rpc_addr = optarg;
1292 : 714 : break;
1293 : 60 : case NO_RPC_SERVER_OPT_IDX:
1294 : 60 : opts->rpc_addr = NULL;
1295 : 60 : break;
1296 : 0 : case ENFORCE_NUMA_OPT_IDX:
1297 : 0 : opts->enforce_numa = true;
1298 : 0 : break;
1299 : 116 : case MEM_SIZE_OPT_IDX: {
1300 : 38 : uint64_t mem_size_mb;
1301 : 38 : bool mem_size_has_prefix;
1302 : :
1303 : 116 : rc = spdk_parse_capacity(optarg, &mem_size_mb, &mem_size_has_prefix);
1304 [ - + ]: 116 : if (rc != 0) {
1305 : 0 : SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
1306 : 0 : usage(app_usage);
1307 : 0 : goto out;
1308 : : }
1309 : :
1310 [ - + - + ]: 116 : if (mem_size_has_prefix) {
1311 : : /* the mem size is in MB by default, so if a prefix was
1312 : : * specified, we need to manually convert to MB.
1313 : : */
1314 : 0 : mem_size_mb /= 1024 * 1024;
1315 : : }
1316 : :
1317 [ - + ]: 116 : if (mem_size_mb > INT_MAX) {
1318 : 0 : SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg);
1319 : 0 : usage(app_usage);
1320 : 0 : goto out;
1321 : : }
1322 : :
1323 : 116 : opts->mem_size = (int) mem_size_mb;
1324 : 116 : break;
1325 : : }
1326 : 0 : case MSG_MEMPOOL_SIZE_OPT_IDX:
1327 : 0 : tmp = spdk_strtol(optarg, 10);
1328 [ # # ]: 0 : if (tmp <= 0) {
1329 : 0 : SPDK_ERRLOG("Invalid message memory pool size %s\n", optarg);
1330 : 0 : goto out;
1331 : : }
1332 : :
1333 : 0 : opts->msg_mempool_size = (size_t)tmp;
1334 : 0 : break;
1335 : :
1336 : 4 : case NO_PCI_OPT_IDX:
1337 : 4 : opts->no_pci = true;
1338 : 4 : break;
1339 : 177 : case WAIT_FOR_RPC_OPT_IDX:
1340 : 177 : opts->delay_subsystem_init = true;
1341 : 177 : break;
1342 : 8 : case PCI_BLOCKED_OPT_IDX:
1343 [ - + ]: 8 : if (opts->pci_allowed) {
1344 : 0 : free(opts->pci_allowed);
1345 : 0 : opts->pci_allowed = NULL;
1346 : 0 : SPDK_ERRLOG("-B and -A cannot be used at the same time\n");
1347 : 0 : usage(app_usage);
1348 : 0 : goto out;
1349 : : }
1350 : :
1351 : 8 : rc = app_opts_add_pci_addr(opts, &opts->pci_blocked, optarg);
1352 [ - + ]: 8 : if (rc != 0) {
1353 : 0 : free(opts->pci_blocked);
1354 : 0 : opts->pci_blocked = NULL;
1355 : 0 : goto out;
1356 : : }
1357 : 8 : break;
1358 : :
1359 : 6 : case NO_HUGE_OPT_IDX:
1360 : 6 : opts->no_huge = true;
1361 : 6 : break;
1362 : :
1363 : 361 : case LOGFLAG_OPT_IDX:
1364 : 361 : rc = spdk_log_set_flag(optarg);
1365 [ - + ]: 361 : if (rc < 0) {
1366 : 0 : SPDK_ERRLOG("unknown flag: %s\n", optarg);
1367 : 0 : usage(app_usage);
1368 : 0 : goto out;
1369 : : }
1370 : : #ifdef DEBUG
1371 : 361 : opts->print_level = SPDK_LOG_DEBUG;
1372 : : #endif
1373 : 361 : break;
1374 : 1 : case HUGE_UNLINK_OPT_IDX:
1375 : 1 : opts->unlink_hugepage = true;
1376 : 1 : break;
1377 : 4 : case PCI_ALLOWED_OPT_IDX:
1378 [ + - ]: 4 : if (opts->pci_blocked) {
1379 : 4 : free(opts->pci_blocked);
1380 : 4 : opts->pci_blocked = NULL;
1381 : 4 : SPDK_ERRLOG("-B and -W cannot be used at the same time\n");
1382 : 4 : usage(app_usage);
1383 : 4 : goto out;
1384 : : }
1385 : :
1386 : 0 : rc = app_opts_add_pci_addr(opts, &opts->pci_allowed, optarg);
1387 [ # # ]: 0 : if (rc != 0) {
1388 : 0 : free(opts->pci_allowed);
1389 : 0 : opts->pci_allowed = NULL;
1390 : 0 : goto out;
1391 : : }
1392 : 0 : break;
1393 : 0 : case BASE_VIRTADDR_OPT_IDX:
1394 : 0 : tmp = spdk_strtoll(optarg, 0);
1395 [ # # ]: 0 : if (tmp <= 0) {
1396 : 0 : SPDK_ERRLOG("Invalid base-virtaddr %s\n", optarg);
1397 : 0 : usage(app_usage);
1398 : 0 : goto out;
1399 : : }
1400 : 0 : opts->base_virtaddr = (uint64_t)tmp;
1401 : 0 : break;
1402 : 0 : case HUGE_DIR_OPT_IDX:
1403 : 0 : opts->hugedir = optarg;
1404 : 0 : break;
1405 : 0 : case IOVA_MODE_OPT_IDX:
1406 : 0 : opts->iova_mode = optarg;
1407 : 0 : break;
1408 : 2 : case NUM_TRACE_ENTRIES_OPT_IDX:
1409 : 2 : tmp = spdk_strtoll(optarg, 0);
1410 [ - + ]: 2 : if (tmp < 0) {
1411 : 0 : SPDK_ERRLOG("Invalid num-trace-entries %s\n", optarg);
1412 : 0 : usage(app_usage);
1413 : 0 : goto out;
1414 : : }
1415 : 2 : opts->num_entries = (uint64_t)tmp;
1416 [ + - - + ]: 2 : if (opts->num_entries > 0 && opts->num_entries & (opts->num_entries - 1)) {
1417 : 0 : SPDK_ERRLOG("num-trace-entries must be power of 2\n");
1418 : 0 : usage(app_usage);
1419 : 0 : goto out;
1420 : : }
1421 : 2 : break;
1422 : 0 : case ENV_CONTEXT_OPT_IDX:
1423 : 0 : opts->env_context = optarg;
1424 : 0 : break;
1425 : 20 : case RPCS_ALLOWED_OPT_IDX:
1426 : 20 : opts->rpc_allowlist = (const char **)spdk_strarray_from_string(optarg, ",");
1427 [ - + ]: 20 : if (opts->rpc_allowlist == NULL) {
1428 : 0 : SPDK_ERRLOG("Invalid --rpcs-allowed argument\n");
1429 : 0 : usage(app_usage);
1430 : 0 : goto out;
1431 : : }
1432 : 20 : break;
1433 : 0 : case ENV_VF_TOKEN_OPT_IDX:
1434 : 0 : opts->vf_token = optarg;
1435 : 0 : break;
1436 : 1 : case INTERRUPT_MODE_OPT_IDX:
1437 : 1 : opts->interrupt_mode = true;
1438 : 1 : break;
1439 : 0 : case VERSION_OPT_IDX:
1440 [ # # ]: 0 : printf(SPDK_VERSION_STRING"\n");
1441 : 0 : retval = SPDK_APP_PARSE_ARGS_HELP;
1442 : 0 : goto out;
1443 : 16 : case '?':
1444 : : /*
1445 : : * In the event getopt() above detects an option
1446 : : * in argv that is NOT in the getopt_str,
1447 : : * getopt() will return a '?' indicating failure.
1448 : : */
1449 : 16 : usage(app_usage);
1450 : 16 : goto out;
1451 : 6801 : default:
1452 [ - + ]: 6801 : if (!app_parse) {
1453 : 0 : SPDK_ERRLOG("Unsupported app-specific command line parameter '%c'.\n", ch);
1454 : 0 : goto out;
1455 : : }
1456 : :
1457 : 6801 : rc = app_parse(ch, optarg);
1458 [ + + ]: 6801 : if (rc) {
1459 : 40 : SPDK_ERRLOG("Parsing app-specific command line parameter '%c' failed: %d\n", ch, rc);
1460 : 40 : goto out;
1461 : : }
1462 : : }
1463 : : }
1464 : :
1465 : 2799 : retval = SPDK_APP_PARSE_ARGS_SUCCESS;
1466 : 2888 : out:
1467 [ + + ]: 2888 : if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) {
1468 : 89 : free(opts->pci_blocked);
1469 : 89 : opts->pci_blocked = NULL;
1470 : 89 : free(opts->pci_allowed);
1471 : 89 : opts->pci_allowed = NULL;
1472 : 89 : spdk_strarray_free((char **)opts->rpc_allowlist);
1473 : 89 : opts->rpc_allowlist = NULL;
1474 : : }
1475 : 2888 : free(cmdline_short_opts);
1476 : 2888 : free(cmdline_options);
1477 : 2888 : return retval;
1478 : : }
1479 : :
1480 : : void
1481 : 0 : spdk_app_usage(void)
1482 : : {
1483 [ # # ]: 0 : if (g_executable_name == NULL) {
1484 : 0 : SPDK_ERRLOG("%s not valid before calling spdk_app_parse_args()\n", __func__);
1485 : 0 : return;
1486 : : }
1487 : :
1488 : 0 : usage(NULL);
1489 : : }
1490 : :
1491 : : static void
1492 : 157 : rpc_framework_start_init_cpl(int rc, void *arg1)
1493 : : {
1494 : 157 : struct spdk_jsonrpc_request *request = arg1;
1495 : :
1496 [ - + ]: 157 : assert(spdk_thread_is_app_thread(NULL));
1497 : :
1498 [ - + ]: 157 : if (rc) {
1499 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1500 : : "framework_initialization failed");
1501 : 0 : return;
1502 : : }
1503 : :
1504 : 157 : app_subsystem_init_done(0, NULL);
1505 : :
1506 : 157 : spdk_jsonrpc_send_bool_response(request, true);
1507 : : }
1508 : :
1509 : : static void
1510 : 157 : rpc_framework_start_init(struct spdk_jsonrpc_request *request,
1511 : : const struct spdk_json_val *params)
1512 : : {
1513 [ - + ]: 157 : if (params != NULL) {
1514 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1515 : : "framework_start_init requires no parameters");
1516 : 0 : return;
1517 : : }
1518 : :
1519 : 157 : spdk_rpc_server_pause(g_spdk_app.rpc_addr);
1520 : 157 : spdk_subsystem_init(rpc_framework_start_init_cpl, request);
1521 : : }
1522 : 2990 : SPDK_RPC_REGISTER("framework_start_init", rpc_framework_start_init, SPDK_RPC_STARTUP)
1523 : :
1524 : : struct subsystem_init_poller_ctx {
1525 : : struct spdk_poller *init_poller;
1526 : : struct spdk_jsonrpc_request *request;
1527 : : };
1528 : :
1529 : : static int
1530 : 474686 : rpc_subsystem_init_poller_ctx(void *ctx)
1531 : : {
1532 : 474686 : struct subsystem_init_poller_ctx *poller_ctx = ctx;
1533 : :
1534 [ + + ]: 474686 : if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1535 : 2 : spdk_jsonrpc_send_bool_response(poller_ctx->request, true);
1536 : 2 : spdk_poller_unregister(&poller_ctx->init_poller);
1537 : 2 : free(poller_ctx);
1538 : : }
1539 : :
1540 : 474686 : return SPDK_POLLER_BUSY;
1541 : : }
1542 : :
1543 : : static void
1544 : 10 : rpc_framework_wait_init(struct spdk_jsonrpc_request *request,
1545 : : const struct spdk_json_val *params)
1546 : : {
1547 : : struct subsystem_init_poller_ctx *ctx;
1548 : :
1549 [ + + ]: 10 : if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) {
1550 : 8 : spdk_jsonrpc_send_bool_response(request, true);
1551 : : } else {
1552 : 2 : ctx = malloc(sizeof(struct subsystem_init_poller_ctx));
1553 [ - + ]: 2 : if (ctx == NULL) {
1554 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
1555 : : "Unable to allocate memory for the request context\n");
1556 : 0 : return;
1557 : : }
1558 : 2 : ctx->request = request;
1559 : 2 : ctx->init_poller = SPDK_POLLER_REGISTER(rpc_subsystem_init_poller_ctx, ctx, 0);
1560 : : }
1561 : : }
1562 : 2990 : SPDK_RPC_REGISTER("framework_wait_init", rpc_framework_wait_init,
1563 : : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1564 : :
1565 : : static void
1566 : 19 : rpc_framework_disable_cpumask_locks(struct spdk_jsonrpc_request *request,
1567 : : const struct spdk_json_val *params)
1568 : : {
1569 : 9 : char msg[128];
1570 : : int rc;
1571 : 9 : uint32_t failed_core;
1572 : :
1573 [ - + ]: 19 : if (params != NULL) {
1574 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1575 : : "framework_disable_cpumask_locks"
1576 : : "requires no arguments");
1577 : 0 : return;
1578 : : }
1579 : :
1580 : 19 : rc = unclaim_cpu_cores(&failed_core);
1581 [ - + ]: 19 : if (rc) {
1582 [ # # ]: 0 : snprintf(msg, sizeof(msg), "Failed to unclaim CPU core: %" PRIu32, failed_core);
1583 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
1584 : 0 : return;
1585 : : }
1586 : :
1587 : 19 : spdk_jsonrpc_send_bool_response(request, true);
1588 : : }
1589 : 2990 : SPDK_RPC_REGISTER("framework_disable_cpumask_locks", rpc_framework_disable_cpumask_locks,
1590 : : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
1591 : :
1592 : : static void
1593 : 57 : rpc_framework_enable_cpumask_locks(struct spdk_jsonrpc_request *request,
1594 : : const struct spdk_json_val *params)
1595 : : {
1596 : 27 : char msg[128];
1597 : : int rc;
1598 : 27 : uint32_t failed_core;
1599 : :
1600 [ - + ]: 57 : if (params != NULL) {
1601 : 0 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
1602 : : "framework_enable_cpumask_locks"
1603 : : "requires no arguments");
1604 : 10 : return;
1605 : : }
1606 : :
1607 : 57 : rc = claim_cpu_cores(&failed_core);
1608 [ + + ]: 57 : if (rc) {
1609 [ - + ]: 19 : snprintf(msg, sizeof(msg), "Failed to claim CPU core: %" PRIu32, failed_core);
1610 : 19 : spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, msg);
1611 : 19 : return;
1612 : : }
1613 : :
1614 : 38 : spdk_jsonrpc_send_bool_response(request, true);
1615 : : }
1616 : 2990 : SPDK_RPC_REGISTER("framework_enable_cpumask_locks", rpc_framework_enable_cpumask_locks,
1617 : : SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
|