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