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