1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 6 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 #include "spdk/version.h" 37 38 #include "spdk_internal/event.h" 39 40 #include "spdk/assert.h" 41 #include "spdk/env.h" 42 #include "spdk/init.h" 43 #include "spdk/log.h" 44 #include "spdk/thread.h" 45 #include "spdk/trace.h" 46 #include "spdk/string.h" 47 #include "spdk/scheduler.h" 48 #include "spdk/rpc.h" 49 #include "spdk/util.h" 50 51 #define SPDK_APP_DEFAULT_LOG_LEVEL SPDK_LOG_NOTICE 52 #define SPDK_APP_DEFAULT_LOG_PRINT_LEVEL SPDK_LOG_INFO 53 #define SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES SPDK_DEFAULT_NUM_TRACE_ENTRIES 54 55 #define SPDK_APP_DPDK_DEFAULT_MEM_SIZE -1 56 #define SPDK_APP_DPDK_DEFAULT_MAIN_CORE -1 57 #define SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL -1 58 #define SPDK_APP_DPDK_DEFAULT_CORE_MASK "0x1" 59 #define SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR 0x200000000000 60 #define SPDK_APP_DEFAULT_CORE_LIMIT 0x140000000 /* 5 GiB */ 61 62 struct spdk_app { 63 const char *json_config_file; 64 bool json_config_ignore_errors; 65 bool stopped; 66 const char *rpc_addr; 67 int shm_id; 68 spdk_app_shutdown_cb shutdown_cb; 69 int rc; 70 }; 71 72 static struct spdk_app g_spdk_app; 73 static spdk_msg_fn g_start_fn = NULL; 74 static void *g_start_arg = NULL; 75 static struct spdk_thread *g_app_thread = NULL; 76 static bool g_delay_subsystem_init = false; 77 static bool g_shutdown_sig_received = false; 78 static char *g_executable_name; 79 static struct spdk_app_opts g_default_opts; 80 81 int 82 spdk_app_get_shm_id(void) 83 { 84 return g_spdk_app.shm_id; 85 } 86 87 /* append one empty option to indicate the end of the array */ 88 static const struct option g_cmdline_options[] = { 89 #define CONFIG_FILE_OPT_IDX 'c' 90 {"config", required_argument, NULL, CONFIG_FILE_OPT_IDX}, 91 #define LIMIT_COREDUMP_OPT_IDX 'd' 92 {"limit-coredump", no_argument, NULL, LIMIT_COREDUMP_OPT_IDX}, 93 #define TPOINT_GROUP_MASK_OPT_IDX 'e' 94 {"tpoint-group-mask", required_argument, NULL, TPOINT_GROUP_MASK_OPT_IDX}, 95 #define SINGLE_FILE_SEGMENTS_OPT_IDX 'g' 96 {"single-file-segments", no_argument, NULL, SINGLE_FILE_SEGMENTS_OPT_IDX}, 97 #define HELP_OPT_IDX 'h' 98 {"help", no_argument, NULL, HELP_OPT_IDX}, 99 #define SHM_ID_OPT_IDX 'i' 100 {"shm-id", required_argument, NULL, SHM_ID_OPT_IDX}, 101 #define CPUMASK_OPT_IDX 'm' 102 {"cpumask", required_argument, NULL, CPUMASK_OPT_IDX}, 103 #define MEM_CHANNELS_OPT_IDX 'n' 104 {"mem-channels", required_argument, NULL, MEM_CHANNELS_OPT_IDX}, 105 #define MAIN_CORE_OPT_IDX 'p' 106 {"main-core", required_argument, NULL, MAIN_CORE_OPT_IDX}, 107 {"master-core", required_argument, NULL, MAIN_CORE_OPT_IDX}, /* deprecated */ 108 #define RPC_SOCKET_OPT_IDX 'r' 109 {"rpc-socket", required_argument, NULL, RPC_SOCKET_OPT_IDX}, 110 #define MEM_SIZE_OPT_IDX 's' 111 {"mem-size", required_argument, NULL, MEM_SIZE_OPT_IDX}, 112 #define NO_PCI_OPT_IDX 'u' 113 {"no-pci", no_argument, NULL, NO_PCI_OPT_IDX}, 114 #define VERSION_OPT_IDX 'v' 115 {"version", no_argument, NULL, VERSION_OPT_IDX}, 116 #define PCI_BLOCKED_OPT_IDX 'B' 117 {"pci-blocked", required_argument, NULL, PCI_BLOCKED_OPT_IDX}, 118 {"pci-blacklist", required_argument, NULL, PCI_BLOCKED_OPT_IDX}, /* deprecated */ 119 #define LOGFLAG_OPT_IDX 'L' 120 {"logflag", required_argument, NULL, LOGFLAG_OPT_IDX}, 121 #define HUGE_UNLINK_OPT_IDX 'R' 122 {"huge-unlink", no_argument, NULL, HUGE_UNLINK_OPT_IDX}, 123 #define PCI_ALLOWED_OPT_IDX 'A' 124 {"pci-allowed", required_argument, NULL, PCI_ALLOWED_OPT_IDX}, 125 #define PCI_WHITELIST_OPT_IDX 'W' 126 {"pci-whitelist", required_argument, NULL, PCI_WHITELIST_OPT_IDX}, /* deprecated */ 127 #define SILENCE_NOTICELOG_OPT_IDX 257 128 {"silence-noticelog", no_argument, NULL, SILENCE_NOTICELOG_OPT_IDX}, 129 #define WAIT_FOR_RPC_OPT_IDX 258 130 {"wait-for-rpc", no_argument, NULL, WAIT_FOR_RPC_OPT_IDX}, 131 #define HUGE_DIR_OPT_IDX 259 132 {"huge-dir", required_argument, NULL, HUGE_DIR_OPT_IDX}, 133 #define NUM_TRACE_ENTRIES_OPT_IDX 260 134 {"num-trace-entries", required_argument, NULL, NUM_TRACE_ENTRIES_OPT_IDX}, 135 #define MAX_REACTOR_DELAY_OPT_IDX 261 136 {"max-delay", required_argument, NULL, MAX_REACTOR_DELAY_OPT_IDX}, 137 #define JSON_CONFIG_OPT_IDX 262 138 {"json", required_argument, NULL, JSON_CONFIG_OPT_IDX}, 139 #define JSON_CONFIG_IGNORE_INIT_ERRORS_IDX 263 140 {"json-ignore-init-errors", no_argument, NULL, JSON_CONFIG_IGNORE_INIT_ERRORS_IDX}, 141 #define IOVA_MODE_OPT_IDX 264 142 {"iova-mode", required_argument, NULL, IOVA_MODE_OPT_IDX}, 143 #define BASE_VIRTADDR_OPT_IDX 265 144 {"base-virtaddr", required_argument, NULL, BASE_VIRTADDR_OPT_IDX}, 145 #define ENV_CONTEXT_OPT_IDX 266 146 {"env-context", required_argument, NULL, ENV_CONTEXT_OPT_IDX}, 147 }; 148 149 static void 150 app_start_shutdown(void *ctx) 151 { 152 if (g_spdk_app.shutdown_cb) { 153 g_spdk_app.shutdown_cb(); 154 g_spdk_app.shutdown_cb = NULL; 155 } else { 156 spdk_app_stop(0); 157 } 158 } 159 160 void 161 spdk_app_start_shutdown(void) 162 { 163 spdk_thread_send_critical_msg(g_app_thread, app_start_shutdown); 164 } 165 166 static void 167 __shutdown_signal(int signo) 168 { 169 if (!g_shutdown_sig_received) { 170 g_shutdown_sig_received = true; 171 spdk_app_start_shutdown(); 172 } 173 } 174 175 static int 176 app_opts_validate(const char *app_opts) 177 { 178 int i = 0, j; 179 180 for (i = 0; app_opts[i] != '\0'; i++) { 181 /* ignore getopt control characters */ 182 if (app_opts[i] == ':' || app_opts[i] == '+' || app_opts[i] == '-') { 183 continue; 184 } 185 186 for (j = 0; SPDK_APP_GETOPT_STRING[j] != '\0'; j++) { 187 if (app_opts[i] == SPDK_APP_GETOPT_STRING[j]) { 188 return app_opts[i]; 189 } 190 } 191 } 192 return 0; 193 } 194 195 void 196 spdk_app_opts_init(struct spdk_app_opts *opts, size_t opts_size) 197 { 198 if (!opts) { 199 SPDK_ERRLOG("opts should not be NULL\n"); 200 return; 201 } 202 203 if (!opts_size) { 204 SPDK_ERRLOG("opts_size should not be zero value\n"); 205 return; 206 } 207 208 memset(opts, 0, opts_size); 209 opts->opts_size = opts_size; 210 211 #define SET_FIELD(field, value) \ 212 if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= opts_size) { \ 213 opts->field = value; \ 214 } \ 215 216 SET_FIELD(enable_coredump, true); 217 SET_FIELD(shm_id, -1); 218 SET_FIELD(mem_size, SPDK_APP_DPDK_DEFAULT_MEM_SIZE); 219 SET_FIELD(main_core, SPDK_APP_DPDK_DEFAULT_MAIN_CORE); 220 SET_FIELD(mem_channel, SPDK_APP_DPDK_DEFAULT_MEM_CHANNEL); 221 SET_FIELD(reactor_mask, SPDK_APP_DPDK_DEFAULT_CORE_MASK); 222 SET_FIELD(base_virtaddr, SPDK_APP_DPDK_DEFAULT_BASE_VIRTADDR); 223 SET_FIELD(print_level, SPDK_APP_DEFAULT_LOG_PRINT_LEVEL); 224 SET_FIELD(rpc_addr, SPDK_DEFAULT_RPC_ADDR); 225 SET_FIELD(num_entries, SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES); 226 SET_FIELD(delay_subsystem_init, false); 227 SET_FIELD(disable_signal_handlers, false); 228 #undef SET_FIELD 229 } 230 231 static int 232 app_setup_signal_handlers(struct spdk_app_opts *opts) 233 { 234 struct sigaction sigact; 235 sigset_t sigmask; 236 int rc; 237 238 sigemptyset(&sigmask); 239 memset(&sigact, 0, sizeof(sigact)); 240 sigemptyset(&sigact.sa_mask); 241 242 sigact.sa_handler = SIG_IGN; 243 rc = sigaction(SIGPIPE, &sigact, NULL); 244 if (rc < 0) { 245 SPDK_ERRLOG("sigaction(SIGPIPE) failed\n"); 246 return rc; 247 } 248 249 /* Install the same handler for SIGINT and SIGTERM */ 250 g_shutdown_sig_received = false; 251 sigact.sa_handler = __shutdown_signal; 252 rc = sigaction(SIGINT, &sigact, NULL); 253 if (rc < 0) { 254 SPDK_ERRLOG("sigaction(SIGINT) failed\n"); 255 return rc; 256 } 257 sigaddset(&sigmask, SIGINT); 258 259 rc = sigaction(SIGTERM, &sigact, NULL); 260 if (rc < 0) { 261 SPDK_ERRLOG("sigaction(SIGTERM) failed\n"); 262 return rc; 263 } 264 sigaddset(&sigmask, SIGTERM); 265 266 pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL); 267 268 return 0; 269 } 270 271 static void 272 app_start_application(void) 273 { 274 assert(spdk_get_thread() == g_app_thread); 275 276 g_start_fn(g_start_arg); 277 } 278 279 static void 280 app_start_rpc(int rc, void *arg1) 281 { 282 if (rc) { 283 spdk_app_stop(rc); 284 return; 285 } 286 287 rc = spdk_rpc_initialize(g_spdk_app.rpc_addr); 288 if (rc) { 289 spdk_app_stop(rc); 290 return; 291 } 292 293 if (!g_delay_subsystem_init) { 294 spdk_rpc_set_state(SPDK_RPC_RUNTIME); 295 app_start_application(); 296 } 297 } 298 299 static int 300 app_opts_add_pci_addr(struct spdk_app_opts *opts, struct spdk_pci_addr **list, char *bdf) 301 { 302 struct spdk_pci_addr *tmp = *list; 303 size_t i = opts->num_pci_addr; 304 305 tmp = realloc(tmp, sizeof(*tmp) * (i + 1)); 306 if (tmp == NULL) { 307 SPDK_ERRLOG("realloc error\n"); 308 return -ENOMEM; 309 } 310 311 *list = tmp; 312 if (spdk_pci_addr_parse(*list + i, bdf) < 0) { 313 SPDK_ERRLOG("Invalid address %s\n", bdf); 314 return -EINVAL; 315 } 316 317 opts->num_pci_addr++; 318 return 0; 319 } 320 321 static int 322 app_setup_env(struct spdk_app_opts *opts) 323 { 324 struct spdk_env_opts env_opts = {}; 325 int rc; 326 327 if (opts == NULL) { 328 rc = spdk_env_init(NULL); 329 if (rc != 0) { 330 SPDK_ERRLOG("Unable to reinitialize SPDK env\n"); 331 } 332 333 return rc; 334 } 335 336 spdk_env_opts_init(&env_opts); 337 338 env_opts.name = opts->name; 339 env_opts.core_mask = opts->reactor_mask; 340 env_opts.shm_id = opts->shm_id; 341 env_opts.mem_channel = opts->mem_channel; 342 env_opts.main_core = opts->main_core; 343 env_opts.mem_size = opts->mem_size; 344 env_opts.hugepage_single_segments = opts->hugepage_single_segments; 345 env_opts.unlink_hugepage = opts->unlink_hugepage; 346 env_opts.hugedir = opts->hugedir; 347 env_opts.no_pci = opts->no_pci; 348 env_opts.num_pci_addr = opts->num_pci_addr; 349 env_opts.pci_blocked = opts->pci_blocked; 350 env_opts.pci_allowed = opts->pci_allowed; 351 env_opts.base_virtaddr = opts->base_virtaddr; 352 env_opts.env_context = opts->env_context; 353 env_opts.iova_mode = opts->iova_mode; 354 355 rc = spdk_env_init(&env_opts); 356 free(env_opts.pci_blocked); 357 free(env_opts.pci_allowed); 358 359 if (rc < 0) { 360 SPDK_ERRLOG("Unable to initialize SPDK env\n"); 361 } 362 363 return rc; 364 } 365 366 static int 367 app_setup_trace(struct spdk_app_opts *opts) 368 { 369 char shm_name[64]; 370 uint64_t tpoint_group_mask; 371 char *end; 372 373 if (opts->shm_id >= 0) { 374 snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", opts->name, opts->shm_id); 375 } else { 376 snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", opts->name, (int)getpid()); 377 } 378 379 if (spdk_trace_init(shm_name, opts->num_entries) != 0) { 380 return -1; 381 } 382 383 if (opts->tpoint_group_mask != NULL) { 384 errno = 0; 385 tpoint_group_mask = strtoull(opts->tpoint_group_mask, &end, 16); 386 if (*end != '\0' || errno) { 387 SPDK_ERRLOG("invalid tpoint mask %s\n", opts->tpoint_group_mask); 388 } else { 389 SPDK_NOTICELOG("Tracepoint Group Mask %s specified.\n", opts->tpoint_group_mask); 390 SPDK_NOTICELOG("Use 'spdk_trace -s %s %s %d' to capture a snapshot of events at runtime.\n", 391 opts->name, 392 opts->shm_id >= 0 ? "-i" : "-p", 393 opts->shm_id >= 0 ? opts->shm_id : getpid()); 394 #if defined(__linux__) 395 SPDK_NOTICELOG("Or copy /dev/shm%s for offline analysis/debug.\n", shm_name); 396 #endif 397 spdk_trace_set_tpoint_group_mask(tpoint_group_mask); 398 } 399 } 400 401 return 0; 402 } 403 404 static void 405 bootstrap_fn(void *arg1) 406 { 407 int rc; 408 409 if (g_spdk_app.json_config_file) { 410 g_delay_subsystem_init = false; 411 spdk_subsystem_init_from_json_config(g_spdk_app.json_config_file, g_spdk_app.rpc_addr, 412 app_start_rpc, 413 NULL, !g_spdk_app.json_config_ignore_errors); 414 } else { 415 if (!g_delay_subsystem_init) { 416 spdk_subsystem_init(app_start_rpc, NULL); 417 } else { 418 rc = spdk_rpc_initialize(g_spdk_app.rpc_addr); 419 if (rc) { 420 spdk_app_stop(rc); 421 return; 422 } 423 } 424 } 425 } 426 427 static void 428 app_copy_opts(struct spdk_app_opts *opts, struct spdk_app_opts *opts_user, size_t opts_size) 429 { 430 spdk_app_opts_init(opts, sizeof(*opts)); 431 opts->opts_size = opts_size; 432 433 #define SET_FIELD(field) \ 434 if (offsetof(struct spdk_app_opts, field) + sizeof(opts->field) <= (opts->opts_size)) { \ 435 opts->field = opts_user->field; \ 436 } \ 437 438 SET_FIELD(name); 439 SET_FIELD(json_config_file); 440 SET_FIELD(json_config_ignore_errors); 441 SET_FIELD(rpc_addr); 442 SET_FIELD(reactor_mask); 443 SET_FIELD(tpoint_group_mask); 444 SET_FIELD(shm_id); 445 SET_FIELD(shutdown_cb); 446 SET_FIELD(enable_coredump); 447 SET_FIELD(mem_channel); 448 SET_FIELD(main_core); 449 SET_FIELD(mem_size); 450 SET_FIELD(no_pci); 451 SET_FIELD(hugepage_single_segments); 452 SET_FIELD(unlink_hugepage); 453 SET_FIELD(hugedir); 454 SET_FIELD(print_level); 455 SET_FIELD(num_pci_addr); 456 SET_FIELD(pci_blocked); 457 SET_FIELD(pci_allowed); 458 SET_FIELD(iova_mode); 459 SET_FIELD(delay_subsystem_init); 460 SET_FIELD(num_entries); 461 SET_FIELD(env_context); 462 SET_FIELD(log); 463 SET_FIELD(base_virtaddr); 464 SET_FIELD(disable_signal_handlers); 465 466 /* You should not remove this statement, but need to update the assert statement 467 * if you add a new field, and also add a corresponding SET_FIELD statement */ 468 SPDK_STATIC_ASSERT(sizeof(struct spdk_app_opts) == 192, "Incorrect size"); 469 470 #undef SET_FIELD 471 } 472 473 int 474 spdk_app_start(struct spdk_app_opts *opts_user, spdk_msg_fn start_fn, 475 void *arg1) 476 { 477 int rc; 478 char *tty; 479 struct spdk_cpuset tmp_cpumask = {}; 480 static bool g_env_was_setup = false; 481 struct spdk_app_opts opts_local = {}; 482 struct spdk_app_opts *opts = &opts_local; 483 484 if (!opts_user) { 485 SPDK_ERRLOG("opts_user should not be NULL\n"); 486 return 1; 487 } 488 489 if (!opts_user->opts_size) { 490 SPDK_ERRLOG("The opts_size in opts_user structure should not be zero value\n"); 491 return 1; 492 } 493 494 app_copy_opts(opts, opts_user, opts_user->opts_size); 495 496 if (!start_fn) { 497 SPDK_ERRLOG("start_fn should not be NULL\n"); 498 return 1; 499 } 500 501 tty = ttyname(STDERR_FILENO); 502 if (opts->print_level > SPDK_LOG_WARN && 503 isatty(STDERR_FILENO) && 504 tty && 505 !strncmp(tty, "/dev/tty", strlen("/dev/tty"))) { 506 printf("Warning: printing stderr to console terminal without -q option specified.\n"); 507 printf("Suggest using --silence-noticelog to disable logging to stderr and\n"); 508 printf("monitor syslog, or redirect stderr to a file.\n"); 509 printf("(Delaying for 10 seconds...)\n"); 510 sleep(10); 511 } 512 513 spdk_log_set_print_level(opts->print_level); 514 515 #ifndef SPDK_NO_RLIMIT 516 if (opts->enable_coredump) { 517 struct rlimit core_limits; 518 519 core_limits.rlim_cur = core_limits.rlim_max = SPDK_APP_DEFAULT_CORE_LIMIT; 520 setrlimit(RLIMIT_CORE, &core_limits); 521 } 522 #endif 523 524 memset(&g_spdk_app, 0, sizeof(g_spdk_app)); 525 g_spdk_app.json_config_file = opts->json_config_file; 526 g_spdk_app.json_config_ignore_errors = opts->json_config_ignore_errors; 527 g_spdk_app.rpc_addr = opts->rpc_addr; 528 g_spdk_app.shm_id = opts->shm_id; 529 g_spdk_app.shutdown_cb = opts->shutdown_cb; 530 g_spdk_app.rc = 0; 531 g_spdk_app.stopped = false; 532 533 spdk_log_set_level(SPDK_APP_DEFAULT_LOG_LEVEL); 534 535 /* Pass NULL to app_setup_env if SPDK app has been set up, in order to 536 * indicate that this is a reinitialization. 537 */ 538 if (app_setup_env(g_env_was_setup ? NULL : opts) < 0) { 539 return 1; 540 } 541 542 spdk_log_open(opts->log); 543 SPDK_NOTICELOG("Total cores available: %d\n", spdk_env_get_core_count()); 544 545 if ((rc = spdk_reactors_init()) != 0) { 546 SPDK_ERRLOG("Reactor Initialization failed: rc = %d\n", rc); 547 return 1; 548 } 549 550 spdk_cpuset_set_cpu(&tmp_cpumask, spdk_env_get_current_core(), true); 551 552 /* Now that the reactors have been initialized, we can create an 553 * initialization thread. */ 554 g_app_thread = spdk_thread_create("app_thread", &tmp_cpumask); 555 if (!g_app_thread) { 556 SPDK_ERRLOG("Unable to create an spdk_thread for initialization\n"); 557 return 1; 558 } 559 560 /* 561 * Disable and ignore trace setup if setting num_entries 562 * to be 0. 563 * 564 * Note the call to app_setup_trace() is located here 565 * ahead of app_setup_signal_handlers(). 566 * That's because there is not an easy/direct clean 567 * way of unwinding alloc'd resources that can occur 568 * in app_setup_signal_handlers(). 569 */ 570 if (opts->num_entries != 0 && app_setup_trace(opts) != 0) { 571 return 1; 572 } 573 574 if (!opts->disable_signal_handlers && app_setup_signal_handlers(opts) != 0) { 575 return 1; 576 } 577 578 g_delay_subsystem_init = opts->delay_subsystem_init; 579 g_start_fn = start_fn; 580 g_start_arg = arg1; 581 582 spdk_thread_send_msg(g_app_thread, bootstrap_fn, NULL); 583 584 /* This blocks until spdk_app_stop is called */ 585 spdk_reactors_start(); 586 587 g_env_was_setup = true; 588 589 return g_spdk_app.rc; 590 } 591 592 void 593 spdk_app_fini(void) 594 { 595 spdk_trace_cleanup(); 596 spdk_reactors_fini(); 597 spdk_env_fini(); 598 spdk_log_close(); 599 } 600 601 static void 602 _start_subsystem_fini(void *arg1) 603 { 604 if (g_scheduling_in_progress) { 605 spdk_thread_send_msg(g_app_thread, _start_subsystem_fini, NULL); 606 return; 607 } 608 609 spdk_subsystem_fini(spdk_reactors_stop, NULL); 610 } 611 612 static void 613 app_stop(void *arg1) 614 { 615 if (g_spdk_app.rc == 0) { 616 g_spdk_app.rc = (int)(intptr_t)arg1; 617 } 618 619 if (g_spdk_app.stopped) { 620 SPDK_NOTICELOG("spdk_app_stop called twice\n"); 621 return; 622 } 623 624 spdk_rpc_finish(); 625 g_spdk_app.stopped = true; 626 _start_subsystem_fini(NULL); 627 } 628 629 void 630 spdk_app_stop(int rc) 631 { 632 if (rc) { 633 SPDK_WARNLOG("spdk_app_stop'd on non-zero\n"); 634 } 635 636 /* 637 * We want to run spdk_subsystem_fini() from the same thread where spdk_subsystem_init() 638 * was called. 639 */ 640 spdk_thread_send_msg(g_app_thread, app_stop, (void *)(intptr_t)rc); 641 } 642 643 struct spdk_thread * 644 _spdk_get_app_thread(void) 645 { 646 return g_app_thread; 647 } 648 649 static void 650 usage(void (*app_usage)(void)) 651 { 652 printf("%s [options]\n", g_executable_name); 653 printf("options:\n"); 654 printf(" -c, --config <config> JSON config file (default %s)\n", 655 g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none"); 656 printf(" --json <config> JSON config file (default %s)\n", 657 g_default_opts.json_config_file != NULL ? g_default_opts.json_config_file : "none"); 658 printf(" --json-ignore-init-errors\n"); 659 printf(" don't exit on invalid config entry\n"); 660 printf(" -d, --limit-coredump do not set max coredump size to RLIM_INFINITY\n"); 661 printf(" -g, --single-file-segments\n"); 662 printf(" force creating just one hugetlbfs file\n"); 663 printf(" -h, --help show this usage\n"); 664 printf(" -i, --shm-id <id> shared memory ID (optional)\n"); 665 printf(" -m, --cpumask <mask or list> core mask (like 0xF) or core list of '[]' embraced (like [0,1,10]) for DPDK\n"); 666 printf(" -n, --mem-channels <num> channel number of memory channels used for DPDK\n"); 667 printf(" -p, --main-core <id> main (primary) core for DPDK\n"); 668 printf(" -r, --rpc-socket <path> RPC listen address (default %s)\n", SPDK_DEFAULT_RPC_ADDR); 669 printf(" -s, --mem-size <size> memory size in MB for DPDK (default: "); 670 #ifndef __linux__ 671 if (g_default_opts.mem_size <= 0) { 672 printf("all hugepage memory)\n"); 673 } else 674 #endif 675 { 676 printf("%dMB)\n", g_default_opts.mem_size >= 0 ? g_default_opts.mem_size : 0); 677 } 678 printf(" --silence-noticelog disable notice level logging to stderr\n"); 679 printf(" -u, --no-pci disable PCI access\n"); 680 printf(" --wait-for-rpc wait for RPCs to initialize subsystems\n"); 681 printf(" --max-delay <num> maximum reactor delay (in microseconds)\n"); 682 printf(" -B, --pci-blocked <bdf>\n"); 683 printf(" pci addr to block (can be used more than once)\n"); 684 printf(" -R, --huge-unlink unlink huge files after initialization\n"); 685 printf(" -v, --version print SPDK version\n"); 686 printf(" -A, --pci-allowed <bdf>\n"); 687 printf(" pci addr to allow (-B and -A cannot be used at the same time)\n"); 688 printf(" --huge-dir <path> use a specific hugetlbfs mount to reserve memory from\n"); 689 printf(" --iova-mode <pa/va> set IOVA mode ('pa' for IOVA_PA and 'va' for IOVA_VA)\n"); 690 printf(" --base-virtaddr <addr> the base virtual address for DPDK (default: 0x200000000000)\n"); 691 printf(" --num-trace-entries <num> number of trace entries for each core, must be power of 2, setting 0 to disable trace (default %d)\n", 692 SPDK_APP_DEFAULT_NUM_TRACE_ENTRIES); 693 printf(" --env-context Opaque context for use of the env implementation\n"); 694 spdk_log_usage(stdout, "-L"); 695 spdk_trace_mask_usage(stdout, "-e"); 696 if (app_usage) { 697 app_usage(); 698 } 699 } 700 701 spdk_app_parse_args_rvals_t 702 spdk_app_parse_args(int argc, char **argv, struct spdk_app_opts *opts, 703 const char *app_getopt_str, struct option *app_long_opts, 704 int (*app_parse)(int ch, char *arg), 705 void (*app_usage)(void)) 706 { 707 int ch, rc, opt_idx, global_long_opts_len, app_long_opts_len; 708 struct option *cmdline_options; 709 char *cmdline_short_opts = NULL; 710 enum spdk_app_parse_args_rvals retval = SPDK_APP_PARSE_ARGS_FAIL; 711 long int tmp; 712 713 memcpy(&g_default_opts, opts, sizeof(g_default_opts)); 714 715 if (opts->json_config_file && access(opts->json_config_file, R_OK) != 0) { 716 SPDK_WARNLOG("Can't read JSON configuration file '%s'\n", opts->json_config_file); 717 opts->json_config_file = NULL; 718 } 719 720 if (app_long_opts == NULL) { 721 app_long_opts_len = 0; 722 } else { 723 for (app_long_opts_len = 0; 724 app_long_opts[app_long_opts_len].name != NULL; 725 app_long_opts_len++); 726 } 727 728 global_long_opts_len = SPDK_COUNTOF(g_cmdline_options); 729 730 cmdline_options = calloc(global_long_opts_len + app_long_opts_len + 1, sizeof(*cmdline_options)); 731 if (!cmdline_options) { 732 SPDK_ERRLOG("Out of memory\n"); 733 return SPDK_APP_PARSE_ARGS_FAIL; 734 } 735 736 memcpy(&cmdline_options[0], g_cmdline_options, sizeof(g_cmdline_options)); 737 if (app_long_opts) { 738 memcpy(&cmdline_options[global_long_opts_len], app_long_opts, 739 app_long_opts_len * sizeof(*app_long_opts)); 740 } 741 742 if (app_getopt_str != NULL) { 743 ch = app_opts_validate(app_getopt_str); 744 if (ch) { 745 SPDK_ERRLOG("Duplicated option '%c' between the generic and application specific spdk opts.\n", 746 ch); 747 goto out; 748 } 749 } 750 751 cmdline_short_opts = spdk_sprintf_alloc("%s%s", app_getopt_str, SPDK_APP_GETOPT_STRING); 752 if (!cmdline_short_opts) { 753 SPDK_ERRLOG("Out of memory\n"); 754 goto out; 755 } 756 757 g_executable_name = argv[0]; 758 759 while ((ch = getopt_long(argc, argv, cmdline_short_opts, cmdline_options, &opt_idx)) != -1) { 760 switch (ch) { 761 case CONFIG_FILE_OPT_IDX: 762 case JSON_CONFIG_OPT_IDX: 763 opts->json_config_file = optarg; 764 break; 765 case JSON_CONFIG_IGNORE_INIT_ERRORS_IDX: 766 opts->json_config_ignore_errors = true; 767 break; 768 case LIMIT_COREDUMP_OPT_IDX: 769 opts->enable_coredump = false; 770 break; 771 case TPOINT_GROUP_MASK_OPT_IDX: 772 opts->tpoint_group_mask = optarg; 773 break; 774 case SINGLE_FILE_SEGMENTS_OPT_IDX: 775 opts->hugepage_single_segments = true; 776 break; 777 case HELP_OPT_IDX: 778 usage(app_usage); 779 retval = SPDK_APP_PARSE_ARGS_HELP; 780 goto out; 781 case SHM_ID_OPT_IDX: 782 opts->shm_id = spdk_strtol(optarg, 0); 783 if (opts->shm_id < 0) { 784 SPDK_ERRLOG("Invalid shared memory ID %s\n", optarg); 785 goto out; 786 } 787 break; 788 case CPUMASK_OPT_IDX: 789 opts->reactor_mask = optarg; 790 break; 791 case MEM_CHANNELS_OPT_IDX: 792 opts->mem_channel = spdk_strtol(optarg, 0); 793 if (opts->mem_channel < 0) { 794 SPDK_ERRLOG("Invalid memory channel %s\n", optarg); 795 goto out; 796 } 797 break; 798 case MAIN_CORE_OPT_IDX: 799 opts->main_core = spdk_strtol(optarg, 0); 800 if (opts->main_core < 0) { 801 SPDK_ERRLOG("Invalid main core %s\n", optarg); 802 goto out; 803 } 804 break; 805 case SILENCE_NOTICELOG_OPT_IDX: 806 opts->print_level = SPDK_LOG_WARN; 807 break; 808 case RPC_SOCKET_OPT_IDX: 809 opts->rpc_addr = optarg; 810 break; 811 case MEM_SIZE_OPT_IDX: { 812 uint64_t mem_size_mb; 813 bool mem_size_has_prefix; 814 815 rc = spdk_parse_capacity(optarg, &mem_size_mb, &mem_size_has_prefix); 816 if (rc != 0) { 817 SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg); 818 usage(app_usage); 819 goto out; 820 } 821 822 if (mem_size_has_prefix) { 823 /* the mem size is in MB by default, so if a prefix was 824 * specified, we need to manually convert to MB. 825 */ 826 mem_size_mb /= 1024 * 1024; 827 } 828 829 if (mem_size_mb > INT_MAX) { 830 SPDK_ERRLOG("invalid memory pool size `-s %s`\n", optarg); 831 usage(app_usage); 832 goto out; 833 } 834 835 opts->mem_size = (int) mem_size_mb; 836 break; 837 } 838 case NO_PCI_OPT_IDX: 839 opts->no_pci = true; 840 break; 841 case WAIT_FOR_RPC_OPT_IDX: 842 opts->delay_subsystem_init = true; 843 break; 844 case PCI_BLOCKED_OPT_IDX: 845 if (opts->pci_allowed) { 846 free(opts->pci_allowed); 847 opts->pci_allowed = NULL; 848 SPDK_ERRLOG("-B and -A cannot be used at the same time\n"); 849 usage(app_usage); 850 goto out; 851 } 852 853 rc = app_opts_add_pci_addr(opts, &opts->pci_blocked, optarg); 854 if (rc != 0) { 855 free(opts->pci_blocked); 856 opts->pci_blocked = NULL; 857 goto out; 858 } 859 break; 860 case LOGFLAG_OPT_IDX: 861 rc = spdk_log_set_flag(optarg); 862 if (rc < 0) { 863 SPDK_ERRLOG("unknown flag\n"); 864 usage(app_usage); 865 goto out; 866 } 867 #ifdef DEBUG 868 opts->print_level = SPDK_LOG_DEBUG; 869 #endif 870 break; 871 case HUGE_UNLINK_OPT_IDX: 872 opts->unlink_hugepage = true; 873 break; 874 case PCI_WHITELIST_OPT_IDX: 875 SPDK_WARNLOG("-W/--pci-whitelist is deprecated. Use -A/--pci-allowed.\n"); 876 /* fallthrough */ 877 case PCI_ALLOWED_OPT_IDX: 878 if (opts->pci_blocked) { 879 free(opts->pci_blocked); 880 opts->pci_blocked = NULL; 881 SPDK_ERRLOG("-B and -W cannot be used at the same time\n"); 882 usage(app_usage); 883 goto out; 884 } 885 886 rc = app_opts_add_pci_addr(opts, &opts->pci_allowed, optarg); 887 if (rc != 0) { 888 free(opts->pci_allowed); 889 opts->pci_allowed = NULL; 890 goto out; 891 } 892 break; 893 case BASE_VIRTADDR_OPT_IDX: 894 tmp = spdk_strtoll(optarg, 0); 895 if (tmp <= 0) { 896 SPDK_ERRLOG("Invalid base-virtaddr %s\n", optarg); 897 usage(app_usage); 898 goto out; 899 } 900 opts->base_virtaddr = (uint64_t)tmp; 901 break; 902 case HUGE_DIR_OPT_IDX: 903 opts->hugedir = optarg; 904 break; 905 case IOVA_MODE_OPT_IDX: 906 opts->iova_mode = optarg; 907 break; 908 case NUM_TRACE_ENTRIES_OPT_IDX: 909 tmp = spdk_strtoll(optarg, 0); 910 if (tmp < 0) { 911 SPDK_ERRLOG("Invalid num-trace-entries %s\n", optarg); 912 usage(app_usage); 913 goto out; 914 } 915 opts->num_entries = (uint64_t)tmp; 916 if (opts->num_entries > 0 && opts->num_entries & (opts->num_entries - 1)) { 917 SPDK_ERRLOG("num-trace-entries must be power of 2\n"); 918 usage(app_usage); 919 goto out; 920 } 921 break; 922 case MAX_REACTOR_DELAY_OPT_IDX: 923 SPDK_ERRLOG("Deprecation warning: The maximum allowed latency parameter is no longer supported.\n"); 924 break; 925 case ENV_CONTEXT_OPT_IDX: 926 opts->env_context = optarg; 927 break; 928 case VERSION_OPT_IDX: 929 printf(SPDK_VERSION_STRING"\n"); 930 retval = SPDK_APP_PARSE_ARGS_HELP; 931 goto out; 932 case '?': 933 /* 934 * In the event getopt() above detects an option 935 * in argv that is NOT in the getopt_str, 936 * getopt() will return a '?' indicating failure. 937 */ 938 usage(app_usage); 939 goto out; 940 default: 941 rc = app_parse(ch, optarg); 942 if (rc) { 943 SPDK_ERRLOG("Parsing application specific arguments failed: %d\n", rc); 944 goto out; 945 } 946 } 947 } 948 949 if (opts->json_config_file && opts->delay_subsystem_init) { 950 SPDK_ERRLOG("JSON configuration file can't be used together with --wait-for-rpc.\n"); 951 goto out; 952 } 953 954 retval = SPDK_APP_PARSE_ARGS_SUCCESS; 955 out: 956 if (retval != SPDK_APP_PARSE_ARGS_SUCCESS) { 957 free(opts->pci_blocked); 958 opts->pci_blocked = NULL; 959 free(opts->pci_allowed); 960 opts->pci_allowed = NULL; 961 } 962 free(cmdline_short_opts); 963 free(cmdline_options); 964 return retval; 965 } 966 967 void 968 spdk_app_usage(void) 969 { 970 if (g_executable_name == NULL) { 971 SPDK_ERRLOG("%s not valid before calling spdk_app_parse_args()\n", __func__); 972 return; 973 } 974 975 usage(NULL); 976 } 977 978 static void 979 rpc_framework_start_init_cpl(int rc, void *arg1) 980 { 981 struct spdk_jsonrpc_request *request = arg1; 982 983 assert(spdk_get_thread() == g_app_thread); 984 985 if (rc) { 986 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 987 "framework_initialization failed"); 988 return; 989 } 990 991 spdk_rpc_set_state(SPDK_RPC_RUNTIME); 992 app_start_application(); 993 994 spdk_jsonrpc_send_bool_response(request, true); 995 } 996 997 static void 998 rpc_framework_start_init(struct spdk_jsonrpc_request *request, 999 const struct spdk_json_val *params) 1000 { 1001 if (params != NULL) { 1002 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, 1003 "framework_start_init requires no parameters"); 1004 return; 1005 } 1006 1007 spdk_subsystem_init(rpc_framework_start_init_cpl, request); 1008 } 1009 SPDK_RPC_REGISTER("framework_start_init", rpc_framework_start_init, SPDK_RPC_STARTUP) 1010 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_start_init, start_subsystem_init) 1011 1012 struct subsystem_init_poller_ctx { 1013 struct spdk_poller *init_poller; 1014 struct spdk_jsonrpc_request *request; 1015 }; 1016 1017 static int 1018 rpc_subsystem_init_poller_ctx(void *ctx) 1019 { 1020 struct subsystem_init_poller_ctx *poller_ctx = ctx; 1021 1022 if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) { 1023 spdk_jsonrpc_send_bool_response(poller_ctx->request, true); 1024 spdk_poller_unregister(&poller_ctx->init_poller); 1025 free(poller_ctx); 1026 } 1027 1028 return SPDK_POLLER_BUSY; 1029 } 1030 1031 static void 1032 rpc_framework_wait_init(struct spdk_jsonrpc_request *request, 1033 const struct spdk_json_val *params) 1034 { 1035 struct subsystem_init_poller_ctx *ctx; 1036 1037 if (spdk_rpc_get_state() == SPDK_RPC_RUNTIME) { 1038 spdk_jsonrpc_send_bool_response(request, true); 1039 } else { 1040 ctx = malloc(sizeof(struct subsystem_init_poller_ctx)); 1041 if (ctx == NULL) { 1042 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR, 1043 "Unable to allocate memory for the request context\n"); 1044 return; 1045 } 1046 ctx->request = request; 1047 ctx->init_poller = SPDK_POLLER_REGISTER(rpc_subsystem_init_poller_ctx, ctx, 0); 1048 } 1049 } 1050 SPDK_RPC_REGISTER("framework_wait_init", rpc_framework_wait_init, 1051 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME) 1052 SPDK_RPC_REGISTER_ALIAS_DEPRECATED(framework_wait_init, wait_subsystem_init) 1053