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