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