Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2019 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : :
8 : : #include "spdk/env.h"
9 : : #include "spdk/event.h"
10 : : #include "spdk/init.h"
11 : : #include "spdk/string.h"
12 : : #include "spdk/thread.h"
13 : : #include "spdk/bdev.h"
14 : : #include "spdk/rpc.h"
15 : : #include "spdk/nvmf.h"
16 : : #include "spdk/likely.h"
17 : : #include "spdk/util.h"
18 : :
19 : : #include "spdk_internal/event.h"
20 : :
21 : : #define NVMF_DEFAULT_SUBSYSTEMS 32
22 : : #define NVMF_GETOPT_STRING "g:i:m:n:p:r:s:u:h"
23 : :
24 : : static const char *g_rpc_addr = SPDK_DEFAULT_RPC_ADDR;
25 : :
26 : : enum nvmf_target_state {
27 : : NVMF_INIT_SUBSYSTEM = 0,
28 : : NVMF_INIT_TARGET,
29 : : NVMF_INIT_POLL_GROUPS,
30 : : NVMF_INIT_START_SUBSYSTEMS,
31 : : NVMF_RUNNING,
32 : : NVMF_FINI_STOP_SUBSYSTEMS,
33 : : NVMF_FINI_POLL_GROUPS,
34 : : NVMF_FINI_TARGET,
35 : : NVMF_FINI_SUBSYSTEM,
36 : : };
37 : :
38 : : struct nvmf_lw_thread {
39 : : TAILQ_ENTRY(nvmf_lw_thread) link;
40 : : bool resched;
41 : : };
42 : :
43 : : struct nvmf_reactor {
44 : : uint32_t core;
45 : :
46 : : struct spdk_ring *threads;
47 : : TAILQ_ENTRY(nvmf_reactor) link;
48 : : };
49 : :
50 : : struct nvmf_target_poll_group {
51 : : struct spdk_nvmf_poll_group *group;
52 : : struct spdk_thread *thread;
53 : :
54 : : TAILQ_ENTRY(nvmf_target_poll_group) link;
55 : : };
56 : :
57 : : struct nvmf_target {
58 : : struct spdk_nvmf_tgt *tgt;
59 : :
60 : : int max_subsystems;
61 : : };
62 : :
63 : : TAILQ_HEAD(, nvmf_reactor) g_reactors = TAILQ_HEAD_INITIALIZER(g_reactors);
64 : : TAILQ_HEAD(, nvmf_target_poll_group) g_poll_groups = TAILQ_HEAD_INITIALIZER(g_poll_groups);
65 : : static uint32_t g_num_poll_groups = 0;
66 : :
67 : : static struct nvmf_reactor *g_main_reactor = NULL;
68 : : static struct nvmf_reactor *g_next_reactor = NULL;
69 : : static struct spdk_thread *g_init_thread = NULL;
70 : : static struct spdk_thread *g_fini_thread = NULL;
71 : : static struct nvmf_target g_nvmf_tgt = {
72 : : .max_subsystems = NVMF_DEFAULT_SUBSYSTEMS,
73 : : };
74 : :
75 : : static struct nvmf_target_poll_group *g_next_pg = NULL;
76 : : static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
77 : : static bool g_reactors_exit = false;
78 : : static enum nvmf_target_state g_target_state;
79 : : static bool g_intr_received = false;
80 : :
81 : : static uint32_t g_migrate_pg_period_us = 0;
82 : : static struct spdk_poller *g_migrate_pg_poller = NULL;
83 : :
84 : : static void nvmf_target_advance_state(void);
85 : : static int nvmf_schedule_spdk_thread(struct spdk_thread *thread);
86 : :
87 : : static void
88 : 0 : usage(char *program_name)
89 : : {
90 [ # # ]: 0 : printf("%s options", program_name);
91 : 0 : printf("\n");
92 [ # # ]: 0 : printf("\t[-g period of round robin poll group migration (us) (default: 0 (disabled))]\n");
93 [ # # ]: 0 : printf("\t[-h show this usage]\n");
94 [ # # ]: 0 : printf("\t[-i shared memory ID (optional)]\n");
95 [ # # ]: 0 : printf("\t[-m core mask for DPDK]\n");
96 [ # # ]: 0 : printf("\t[-n max subsystems for target(default: 32)]\n");
97 [ # # ]: 0 : printf("\t[-r RPC listen address (default /var/tmp/spdk.sock)]\n");
98 [ # # ]: 0 : printf("\t[-s memory size in MB for DPDK (default: 0MB)]\n");
99 [ # # ]: 0 : printf("\t[-u disable PCI access]\n");
100 [ # # ]: 0 : printf("\t[--no-huge SPDK is run without hugepages]\n");
101 : 0 : }
102 : :
103 : : static const struct option g_nvmf_cmdline_opts[] = {
104 : : #define NVMF_NO_HUGE 257
105 : : {"no-huge", no_argument, NULL, NVMF_NO_HUGE},
106 : : {0, 0, 0, 0}
107 : : };
108 : :
109 : : static int
110 : 3 : parse_args(int argc, char **argv, struct spdk_env_opts *opts)
111 : : {
112 : 0 : int op, opt_index;
113 : : long int value;
114 : :
115 [ - + - + : 12 : while ((op = getopt_long(argc, argv, NVMF_GETOPT_STRING, g_nvmf_cmdline_opts, &opt_index)) != -1) {
+ + ]
116 [ + + + - : 9 : switch (op) {
- - - - -
- ]
117 : 3 : case 'g':
118 : 3 : value = spdk_strtol(optarg, 10);
119 [ - + ]: 3 : if (value < 0) {
120 [ # # # # ]: 0 : fprintf(stderr, "converting a string to integer failed\n");
121 : 0 : return -EINVAL;
122 : : }
123 : 3 : g_migrate_pg_period_us = value;
124 : 3 : break;
125 : 3 : case 'i':
126 : 3 : value = spdk_strtol(optarg, 10);
127 [ - + ]: 3 : if (value < 0) {
128 [ # # # # ]: 0 : fprintf(stderr, "converting a string to integer failed\n");
129 : 0 : return -EINVAL;
130 : : }
131 : 3 : opts->shm_id = value;
132 : 3 : break;
133 : 3 : case 'm':
134 : 3 : opts->core_mask = optarg;
135 : 3 : break;
136 : 0 : case 'n':
137 : 0 : g_nvmf_tgt.max_subsystems = spdk_strtol(optarg, 10);
138 [ # # ]: 0 : if (g_nvmf_tgt.max_subsystems < 0) {
139 [ # # # # ]: 0 : fprintf(stderr, "converting a string to integer failed\n");
140 : 0 : return -EINVAL;
141 : : }
142 : 0 : break;
143 : 0 : case 'r':
144 : 0 : g_rpc_addr = optarg;
145 : 0 : break;
146 : 0 : case 's':
147 : 0 : value = spdk_strtol(optarg, 10);
148 [ # # ]: 0 : if (value < 0) {
149 [ # # # # ]: 0 : fprintf(stderr, "converting a string to integer failed\n");
150 : 0 : return -EINVAL;
151 : : }
152 : 0 : opts->mem_size = value;
153 : 0 : break;
154 : 0 : case 'u':
155 : 0 : opts->no_pci = true;
156 : 0 : break;
157 : 0 : case 'h':
158 : 0 : usage(argv[0]);
159 : 0 : exit(EXIT_SUCCESS);
160 : 0 : case NVMF_NO_HUGE:
161 : 0 : opts->no_huge = true;
162 : 0 : break;
163 : 0 : default:
164 : 0 : usage(argv[0]);
165 : 0 : return 1;
166 : : }
167 : : }
168 : :
169 : 3 : return 0;
170 : : }
171 : :
172 : : static int
173 : 12 : nvmf_reactor_run(void *arg)
174 : : {
175 : 12 : struct nvmf_reactor *nvmf_reactor = arg;
176 : 0 : struct nvmf_lw_thread *lw_thread;
177 : : struct spdk_thread *thread;
178 : :
179 : : /* run all the lightweight threads in this nvmf_reactor by FIFO. */
180 : : do {
181 [ + + ]: 63616155 : if (spdk_ring_dequeue(nvmf_reactor->threads, (void **)&lw_thread, 1)) {
182 : 12859253 : thread = spdk_thread_get_from_ctx(lw_thread);
183 : :
184 : 12859253 : spdk_thread_poll(thread, 0, 0);
185 : :
186 [ + + + + ]: 12859253 : if (spdk_unlikely(spdk_thread_is_exited(thread) &&
187 : : spdk_thread_is_idle(thread))) {
188 : 9 : spdk_thread_destroy(thread);
189 [ - + + + ]: 12859244 : } else if (spdk_unlikely(lw_thread->resched)) {
190 : 11702 : lw_thread->resched = false;
191 : 11702 : nvmf_schedule_spdk_thread(thread);
192 : : } else {
193 : 12847542 : spdk_ring_enqueue(nvmf_reactor->threads, (void **)&lw_thread, 1, NULL);
194 : : }
195 : : }
196 [ - + + + ]: 63616155 : } while (!g_reactors_exit);
197 : :
198 : : /* free all the lightweight threads */
199 [ + + ]: 21 : while (spdk_ring_dequeue(nvmf_reactor->threads, (void **)&lw_thread, 1)) {
200 : 9 : thread = spdk_thread_get_from_ctx(lw_thread);
201 : 9 : spdk_set_thread(thread);
202 : :
203 [ + + ]: 9 : if (spdk_thread_is_exited(thread)) {
204 : 6 : spdk_thread_destroy(thread);
205 : : } else {
206 : : /* This thread is not exited yet, and may need to communicate with other threads
207 : : * to be exited. So mark it as exiting, and check again after traversing other threads.
208 : : */
209 : 3 : spdk_thread_exit(thread);
210 : 3 : spdk_thread_poll(thread, 0, 0);
211 : 3 : spdk_ring_enqueue(nvmf_reactor->threads, (void **)&lw_thread, 1, NULL);
212 : : }
213 : : }
214 : :
215 : 12 : return 0;
216 : : }
217 : :
218 : : static int
219 : 11717 : nvmf_schedule_spdk_thread(struct spdk_thread *thread)
220 : : {
221 : : struct nvmf_reactor *nvmf_reactor;
222 : 0 : struct nvmf_lw_thread *lw_thread;
223 : : struct spdk_cpuset *cpumask;
224 : : uint32_t i;
225 : :
226 : : /* Lightweight threads may have a requested cpumask.
227 : : * This is a request only - the scheduler does not have to honor it.
228 : : * For this scheduler implementation, each reactor is pinned to
229 : : * a particular core so honoring the request is reasonably easy.
230 : : */
231 : 11717 : cpumask = spdk_thread_get_cpumask(thread);
232 : :
233 : 11717 : lw_thread = spdk_thread_get_ctx(thread);
234 [ - + ]: 11717 : assert(lw_thread != NULL);
235 [ - + ]: 11717 : memset(lw_thread, 0, sizeof(*lw_thread));
236 : :
237 : : /* assign lightweight threads to nvmf reactor(core)
238 : : * Here we use the mutex.The way the actual SPDK event framework
239 : : * solves this is by using internal rings for messages between reactors
240 : : */
241 [ - + ]: 11717 : pthread_mutex_lock(&g_mutex);
242 [ + - ]: 27572 : for (i = 0; i < spdk_env_get_core_count(); i++) {
243 [ + + ]: 27572 : if (g_next_reactor == NULL) {
244 : 6891 : g_next_reactor = TAILQ_FIRST(&g_reactors);
245 : : }
246 : 27572 : nvmf_reactor = g_next_reactor;
247 : 27572 : g_next_reactor = TAILQ_NEXT(g_next_reactor, link);
248 : :
249 : : /* each spdk_thread has the core affinity */
250 [ + + ]: 27572 : if (spdk_cpuset_get_cpu(cpumask, nvmf_reactor->core)) {
251 : 11717 : spdk_ring_enqueue(nvmf_reactor->threads, (void **)&lw_thread, 1, NULL);
252 : 11717 : break;
253 : : }
254 : : }
255 [ - + ]: 11717 : pthread_mutex_unlock(&g_mutex);
256 : :
257 [ - + ]: 11717 : if (i == spdk_env_get_core_count()) {
258 [ # # # # ]: 0 : fprintf(stderr, "failed to schedule spdk thread\n");
259 : 0 : return -1;
260 : : }
261 : 11717 : return 0;
262 : : }
263 : :
264 : : static void
265 : 11724 : nvmf_request_spdk_thread_reschedule(struct spdk_thread *thread)
266 : : {
267 : : struct nvmf_lw_thread *lw_thread;
268 : :
269 [ - + ]: 11724 : assert(thread == spdk_get_thread());
270 : :
271 : 11724 : lw_thread = spdk_thread_get_ctx(thread);
272 : :
273 [ - + ]: 11724 : assert(lw_thread != NULL);
274 : :
275 : 11724 : lw_thread->resched = true;
276 : 11724 : }
277 : :
278 : : static int
279 : 11739 : nvmf_reactor_thread_op(struct spdk_thread *thread, enum spdk_thread_op op)
280 : : {
281 [ + + - ]: 11739 : switch (op) {
282 : 15 : case SPDK_THREAD_OP_NEW:
283 : 15 : return nvmf_schedule_spdk_thread(thread);
284 : 11724 : case SPDK_THREAD_OP_RESCHED:
285 : 11724 : nvmf_request_spdk_thread_reschedule(thread);
286 : 11724 : return 0;
287 : 0 : default:
288 : 0 : return -ENOTSUP;
289 : : }
290 : : }
291 : :
292 : : static bool
293 : 11739 : nvmf_reactor_thread_op_supported(enum spdk_thread_op op)
294 : : {
295 [ + - ]: 11739 : switch (op) {
296 : 11739 : case SPDK_THREAD_OP_NEW:
297 : : case SPDK_THREAD_OP_RESCHED:
298 : 11739 : return true;
299 : 0 : default:
300 : 0 : return false;
301 : : }
302 : : }
303 : :
304 : : static int
305 : 3 : nvmf_init_threads(void)
306 : : {
307 : : int rc;
308 : : uint32_t i;
309 : 0 : char thread_name[32];
310 : : struct nvmf_reactor *nvmf_reactor;
311 : 0 : struct spdk_cpuset cpumask;
312 : 3 : uint32_t main_core = spdk_env_get_current_core();
313 : :
314 : : /* Whenever SPDK creates a new lightweight thread it will call
315 : : * nvmf_schedule_spdk_thread asking for the application to begin
316 : : * polling it via spdk_thread_poll(). Each lightweight thread in
317 : : * SPDK optionally allocates extra memory to be used by the application
318 : : * framework. The size of the extra memory allocated is the second parameter.
319 : : */
320 : 3 : spdk_thread_lib_init_ext(nvmf_reactor_thread_op, nvmf_reactor_thread_op_supported,
321 : : sizeof(struct nvmf_lw_thread), SPDK_DEFAULT_MSG_MEMPOOL_SIZE);
322 : :
323 : : /* Spawn one system thread per CPU core. The system thread is called a reactor.
324 : : * SPDK will spawn lightweight threads that must be mapped to reactors in
325 : : * nvmf_schedule_spdk_thread. Using a single system thread per CPU core is a
326 : : * choice unique to this application. SPDK itself does not require this specific
327 : : * threading model. For example, another viable threading model would be
328 : : * dynamically scheduling the lightweight threads onto a thread pool using a
329 : : * work queue.
330 : : */
331 [ + + ]: 15 : SPDK_ENV_FOREACH_CORE(i) {
332 : 12 : nvmf_reactor = calloc(1, sizeof(struct nvmf_reactor));
333 [ - + ]: 12 : if (!nvmf_reactor) {
334 [ # # # # ]: 0 : fprintf(stderr, "failed to alloc nvmf reactor\n");
335 : 0 : rc = -ENOMEM;
336 : 0 : goto err_exit;
337 : : }
338 : :
339 : 12 : nvmf_reactor->core = i;
340 : :
341 : 12 : nvmf_reactor->threads = spdk_ring_create(SPDK_RING_TYPE_MP_SC, 1024, SPDK_ENV_SOCKET_ID_ANY);
342 [ - + ]: 12 : if (!nvmf_reactor->threads) {
343 [ # # # # ]: 0 : fprintf(stderr, "failed to alloc ring\n");
344 : 0 : free(nvmf_reactor);
345 : 0 : rc = -ENOMEM;
346 : 0 : goto err_exit;
347 : : }
348 : :
349 : 12 : TAILQ_INSERT_TAIL(&g_reactors, nvmf_reactor, link);
350 : :
351 [ + + ]: 12 : if (i == main_core) {
352 : 3 : g_main_reactor = nvmf_reactor;
353 : 3 : g_next_reactor = g_main_reactor;
354 : : } else {
355 : 9 : rc = spdk_env_thread_launch_pinned(i,
356 : : nvmf_reactor_run,
357 : : nvmf_reactor);
358 [ - + ]: 9 : if (rc) {
359 [ # # # # ]: 0 : fprintf(stderr, "failed to pin reactor launch\n");
360 : 0 : goto err_exit;
361 : : }
362 : : }
363 : : }
364 : :
365 : : /* Spawn a lightweight thread only on the current core to manage this application. */
366 : 3 : spdk_cpuset_zero(&cpumask);
367 : 3 : spdk_cpuset_set_cpu(&cpumask, main_core, true);
368 [ - + ]: 3 : snprintf(thread_name, sizeof(thread_name), "nvmf_main_thread");
369 : 3 : g_init_thread = spdk_thread_create(thread_name, &cpumask);
370 [ - + ]: 3 : if (!g_init_thread) {
371 [ # # # # ]: 0 : fprintf(stderr, "failed to create spdk thread\n");
372 : 0 : return -1;
373 : : }
374 : :
375 [ - + - + ]: 3 : fprintf(stdout, "nvmf threads initialize successfully\n");
376 : 3 : return 0;
377 : :
378 : 0 : err_exit:
379 : 0 : return rc;
380 : : }
381 : :
382 : : static void
383 : 3 : nvmf_destroy_threads(void)
384 : : {
385 : : struct nvmf_reactor *nvmf_reactor, *tmp;
386 : :
387 [ + + ]: 15 : TAILQ_FOREACH_SAFE(nvmf_reactor, &g_reactors, link, tmp) {
388 : 12 : spdk_ring_free(nvmf_reactor->threads);
389 : 12 : free(nvmf_reactor);
390 : : }
391 : :
392 [ - + ]: 3 : pthread_mutex_destroy(&g_mutex);
393 : 3 : spdk_thread_lib_fini();
394 [ - + - + ]: 3 : fprintf(stdout, "nvmf threads destroy successfully\n");
395 : 3 : }
396 : :
397 : : static void
398 : 3 : nvmf_tgt_destroy_done(void *ctx, int status)
399 : : {
400 [ - + - + ]: 3 : fprintf(stdout, "destroyed the nvmf target service\n");
401 : :
402 : 3 : g_target_state = NVMF_FINI_SUBSYSTEM;
403 : 3 : nvmf_target_advance_state();
404 : 3 : }
405 : :
406 : : static void
407 : 3 : nvmf_destroy_nvmf_tgt(void)
408 : : {
409 [ + - ]: 3 : if (g_nvmf_tgt.tgt) {
410 : 3 : spdk_nvmf_tgt_destroy(g_nvmf_tgt.tgt, nvmf_tgt_destroy_done, NULL);
411 : : } else {
412 : 0 : g_target_state = NVMF_FINI_SUBSYSTEM;
413 : : }
414 : 3 : }
415 : :
416 : : static void
417 : 3 : nvmf_create_nvmf_tgt(void)
418 : : {
419 : : struct spdk_nvmf_subsystem *subsystem;
420 : 3 : struct spdk_nvmf_target_opts tgt_opts = {};
421 : :
422 : 3 : tgt_opts.size = SPDK_SIZEOF(&tgt_opts, discovery_filter);
423 : 3 : tgt_opts.max_subsystems = g_nvmf_tgt.max_subsystems;
424 [ - + ]: 3 : snprintf(tgt_opts.name, sizeof(tgt_opts.name), "%s", "nvmf_example");
425 : : /* Construct the default NVMe-oF target
426 : : * An NVMe-oF target is a collection of subsystems, namespace, and poll
427 : : * groups, and defines the scope of the NVMe-oF discovery service.
428 : : */
429 : 3 : g_nvmf_tgt.tgt = spdk_nvmf_tgt_create(&tgt_opts);
430 [ - + ]: 3 : if (g_nvmf_tgt.tgt == NULL) {
431 [ # # # # ]: 0 : fprintf(stderr, "spdk_nvmf_tgt_create() failed\n");
432 : 0 : goto error;
433 : : }
434 : :
435 : : /* Create and add discovery subsystem to the NVMe-oF target.
436 : : * NVMe-oF defines a discovery mechanism that a host uses to determine
437 : : * the NVM subsystems that expose namespaces that the host may access.
438 : : * It provides a host with following capabilities:
439 : : * 1,The ability to discover a list of NVM subsystems with namespaces
440 : : * that are accessible to the host.
441 : : * 2,The ability to discover multiple paths to an NVM subsystem.
442 : : * 3,The ability to discover controllers that are statically configured.
443 : : */
444 : 3 : subsystem = spdk_nvmf_subsystem_create(g_nvmf_tgt.tgt, SPDK_NVMF_DISCOVERY_NQN,
445 : : SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT, 0);
446 [ - + ]: 3 : if (subsystem == NULL) {
447 [ # # # # ]: 0 : fprintf(stderr, "failed to create discovery nvmf library subsystem\n");
448 : 0 : goto error;
449 : : }
450 : :
451 : : /* Allow any host to access the discovery subsystem */
452 : 3 : spdk_nvmf_subsystem_set_allow_any_host(subsystem, true);
453 : :
454 [ - + - + ]: 3 : fprintf(stdout, "created a nvmf target service\n");
455 : :
456 : 3 : g_target_state = NVMF_INIT_POLL_GROUPS;
457 : 3 : return;
458 : :
459 : 0 : error:
460 : 0 : g_target_state = NVMF_FINI_TARGET;
461 : : }
462 : :
463 : : static void
464 : 6 : nvmf_tgt_subsystem_stop_next(struct spdk_nvmf_subsystem *subsystem,
465 : : void *cb_arg, int status)
466 : : {
467 : : int rc;
468 : :
469 : 6 : subsystem = spdk_nvmf_subsystem_get_next(subsystem);
470 [ + + ]: 6 : if (subsystem) {
471 : 3 : rc = spdk_nvmf_subsystem_stop(subsystem,
472 : : nvmf_tgt_subsystem_stop_next,
473 : : cb_arg);
474 [ - + ]: 3 : if (rc) {
475 : 0 : nvmf_tgt_subsystem_stop_next(subsystem, cb_arg, 0);
476 [ # # # # ]: 0 : fprintf(stderr, "Unable to stop NVMe-oF subsystem. Trying others.\n");
477 : : }
478 : 3 : return;
479 : : }
480 : :
481 [ - + - + ]: 3 : fprintf(stdout, "all subsystems of target stopped\n");
482 : :
483 : 3 : g_target_state = NVMF_FINI_POLL_GROUPS;
484 : 3 : nvmf_target_advance_state();
485 : : }
486 : :
487 : : static void
488 : 3 : nvmf_tgt_stop_subsystems(struct nvmf_target *nvmf_tgt)
489 : : {
490 : : struct spdk_nvmf_subsystem *subsystem;
491 : : int rc;
492 : :
493 : 3 : subsystem = spdk_nvmf_subsystem_get_first(nvmf_tgt->tgt);
494 [ + - ]: 3 : if (spdk_likely(subsystem)) {
495 : 3 : rc = spdk_nvmf_subsystem_stop(subsystem,
496 : : nvmf_tgt_subsystem_stop_next,
497 : : NULL);
498 [ - + ]: 3 : if (rc) {
499 : 0 : nvmf_tgt_subsystem_stop_next(subsystem, NULL, 0);
500 [ # # ]: 0 : fprintf(stderr, "Unable to stop NVMe-oF subsystem. Trying others.\n");
501 : : }
502 : : } else {
503 : 0 : g_target_state = NVMF_FINI_POLL_GROUPS;
504 : : }
505 : 3 : }
506 : :
507 : : static void
508 : 3 : nvmf_tgt_subsystem_start_next(struct spdk_nvmf_subsystem *subsystem,
509 : : void *cb_arg, int status)
510 : : {
511 : : int rc;
512 : :
513 : 3 : subsystem = spdk_nvmf_subsystem_get_next(subsystem);
514 [ - + ]: 3 : if (subsystem) {
515 : 0 : rc = spdk_nvmf_subsystem_start(subsystem, nvmf_tgt_subsystem_start_next,
516 : : cb_arg);
517 [ # # ]: 0 : if (rc) {
518 : 0 : g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
519 [ # # # # ]: 0 : fprintf(stderr, "Unable to start NVMe-oF subsystem. shutting down app.\n");
520 : 0 : nvmf_target_advance_state();
521 : : }
522 : 0 : return;
523 : : }
524 : :
525 [ - + - + ]: 3 : fprintf(stdout, "all subsystems of target started\n");
526 : :
527 : 3 : g_target_state = NVMF_RUNNING;
528 : 3 : nvmf_target_advance_state();
529 : : }
530 : :
531 : : static void
532 : 3 : nvmf_tgt_start_subsystems(struct nvmf_target *nvmf_tgt)
533 : : {
534 : : struct spdk_nvmf_subsystem *subsystem;
535 : : int rc;
536 : :
537 : : /* Subsystem is the NVM subsystem which is a combine of namespaces
538 : : * except the discovery subsystem which is used for discovery service.
539 : : * It also controls the hosts that means the subsystem determines whether
540 : : * the host can access this subsystem.
541 : : */
542 : 3 : subsystem = spdk_nvmf_subsystem_get_first(nvmf_tgt->tgt);
543 [ + - ]: 3 : if (spdk_likely(subsystem)) {
544 : : /* In SPDK there are three states in subsystem: Inactive, Active, Paused.
545 : : * Start subsystem means make it from inactive to active that means
546 : : * subsystem start to work or it can be accessed.
547 : : */
548 : 3 : rc = spdk_nvmf_subsystem_start(subsystem,
549 : : nvmf_tgt_subsystem_start_next,
550 : : NULL);
551 [ - + ]: 3 : if (rc) {
552 [ # # ]: 0 : fprintf(stderr, "Unable to start NVMe-oF subsystem. shutting down app.\n");
553 : 0 : g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
554 : : }
555 : : } else {
556 : 0 : g_target_state = NVMF_RUNNING;
557 : : }
558 : 3 : }
559 : :
560 : : static void
561 : 12 : nvmf_tgt_create_poll_groups_done(void *ctx)
562 : : {
563 : 12 : struct nvmf_target_poll_group *pg = ctx;
564 : :
565 [ + + ]: 12 : if (!g_next_pg) {
566 : 3 : g_next_pg = pg;
567 : : }
568 : :
569 : 12 : TAILQ_INSERT_TAIL(&g_poll_groups, pg, link);
570 : :
571 [ - + ]: 12 : assert(g_num_poll_groups < spdk_env_get_core_count());
572 : :
573 [ + + ]: 12 : if (++g_num_poll_groups == spdk_env_get_core_count()) {
574 [ - + - + ]: 3 : fprintf(stdout, "create targets's poll groups done\n");
575 : :
576 : 3 : g_target_state = NVMF_INIT_START_SUBSYSTEMS;
577 : 3 : nvmf_target_advance_state();
578 : : }
579 : 12 : }
580 : :
581 : : static void
582 : 12 : nvmf_tgt_create_poll_group(void *ctx)
583 : : {
584 : : struct nvmf_target_poll_group *pg;
585 : :
586 : 12 : pg = calloc(1, sizeof(struct nvmf_target_poll_group));
587 [ - + ]: 12 : if (!pg) {
588 [ # # # # ]: 0 : fprintf(stderr, "failed to allocate poll group\n");
589 : 0 : assert(false);
590 : : return;
591 : : }
592 : :
593 : 12 : pg->thread = spdk_get_thread();
594 : 12 : pg->group = spdk_nvmf_poll_group_create(g_nvmf_tgt.tgt);
595 [ - + ]: 12 : if (!pg->group) {
596 [ # # # # ]: 0 : fprintf(stderr, "failed to create poll group of the target\n");
597 : 0 : free(pg);
598 : 0 : assert(false);
599 : : return;
600 : : }
601 : :
602 : 12 : spdk_thread_send_msg(g_init_thread, nvmf_tgt_create_poll_groups_done, pg);
603 : : }
604 : :
605 : : /* Create a lightweight thread per poll group instead of assuming a pool of lightweight
606 : : * threads already exist at start up time. A poll group is a collection of unrelated NVMe-oF
607 : : * connections. Each poll group is only accessed from the associated lightweight thread.
608 : : */
609 : : static void
610 : 3 : nvmf_poll_groups_create(void)
611 : : {
612 : 3 : struct spdk_cpuset tmp_cpumask = {};
613 : : uint32_t i;
614 : 0 : char thread_name[32];
615 : : struct spdk_thread *thread;
616 : :
617 [ - + ]: 3 : assert(g_init_thread != NULL);
618 : :
619 [ + + ]: 15 : SPDK_ENV_FOREACH_CORE(i) {
620 : 12 : spdk_cpuset_zero(&tmp_cpumask);
621 : 12 : spdk_cpuset_set_cpu(&tmp_cpumask, i, true);
622 [ - + ]: 12 : snprintf(thread_name, sizeof(thread_name), "nvmf_tgt_poll_group_%03u", i);
623 : :
624 : 12 : thread = spdk_thread_create(thread_name, &tmp_cpumask);
625 [ - + ]: 12 : assert(thread != NULL);
626 : :
627 : 12 : spdk_thread_send_msg(thread, nvmf_tgt_create_poll_group, NULL);
628 : : }
629 : 3 : }
630 : :
631 : : static void
632 : 12 : _nvmf_tgt_destroy_poll_groups_done(void *ctx)
633 : : {
634 [ - + ]: 12 : assert(g_num_poll_groups > 0);
635 : :
636 [ + + ]: 12 : if (--g_num_poll_groups == 0) {
637 [ - + - + ]: 3 : fprintf(stdout, "destroy targets's poll groups done\n");
638 : :
639 : 3 : g_target_state = NVMF_FINI_TARGET;
640 : 3 : nvmf_target_advance_state();
641 : : }
642 : 12 : }
643 : :
644 : : static void
645 : 12 : nvmf_tgt_destroy_poll_groups_done(void *cb_arg, int status)
646 : : {
647 : 12 : struct nvmf_target_poll_group *pg = cb_arg;
648 : :
649 : 12 : free(pg);
650 : :
651 : 12 : spdk_thread_send_msg(g_fini_thread, _nvmf_tgt_destroy_poll_groups_done, NULL);
652 : :
653 : 12 : spdk_thread_exit(spdk_get_thread());
654 : 12 : }
655 : :
656 : : static void
657 : 12 : nvmf_tgt_destroy_poll_group(void *ctx)
658 : : {
659 : 12 : struct nvmf_target_poll_group *pg = ctx;
660 : :
661 : 12 : spdk_nvmf_poll_group_destroy(pg->group, nvmf_tgt_destroy_poll_groups_done, pg);
662 : 12 : }
663 : :
664 : : static void
665 : 3 : nvmf_poll_groups_destroy(void)
666 : : {
667 : : struct nvmf_target_poll_group *pg, *tmp;
668 : :
669 : 3 : g_fini_thread = spdk_get_thread();
670 [ - + ]: 3 : assert(g_fini_thread != NULL);
671 : :
672 [ + + ]: 15 : TAILQ_FOREACH_SAFE(pg, &g_poll_groups, link, tmp) {
673 [ + + ]: 12 : TAILQ_REMOVE(&g_poll_groups, pg, link);
674 : 12 : spdk_thread_send_msg(pg->thread, nvmf_tgt_destroy_poll_group, pg);
675 : : }
676 : 3 : }
677 : :
678 : : static void
679 : 3 : nvmf_subsystem_fini_done(void *cb_arg)
680 : : {
681 [ - + - + ]: 3 : fprintf(stdout, "bdev subsystem finish successfully\n");
682 : 3 : spdk_rpc_finish();
683 : 3 : g_reactors_exit = true;
684 : 3 : }
685 : :
686 : : static void
687 : 3 : nvmf_subsystem_init_done(int rc, void *cb_arg)
688 : : {
689 [ - + - + ]: 3 : fprintf(stdout, "bdev subsystem init successfully\n");
690 : :
691 : 3 : rc = spdk_rpc_initialize(g_rpc_addr, NULL);
692 [ - + ]: 3 : if (rc) {
693 : 0 : spdk_app_stop(rc);
694 : 0 : return;
695 : : }
696 : :
697 : 3 : spdk_rpc_set_state(SPDK_RPC_RUNTIME);
698 : :
699 : 3 : g_target_state = NVMF_INIT_TARGET;
700 : 3 : nvmf_target_advance_state();
701 : : }
702 : :
703 : : static void
704 : 11724 : migrate_poll_group_by_rr(void *ctx)
705 : : {
706 : : uint32_t current_core, next_core;
707 : 11724 : struct spdk_cpuset cpumask = {};
708 : :
709 : 11724 : current_core = spdk_env_get_current_core();
710 : 11724 : next_core = spdk_env_get_next_core(current_core);
711 [ + + ]: 11724 : if (next_core == UINT32_MAX) {
712 : 2942 : next_core = spdk_env_get_first_core();
713 : : }
714 : :
715 : 11724 : spdk_cpuset_set_cpu(&cpumask, next_core, true);
716 : :
717 : 11724 : spdk_thread_set_cpumask(&cpumask);
718 : 11724 : }
719 : :
720 : : static int
721 : 2931 : migrate_poll_groups_by_rr(void *ctx)
722 : : {
723 : : struct nvmf_target_poll_group *pg;
724 : :
725 [ + + ]: 14655 : TAILQ_FOREACH(pg, &g_poll_groups, link) {
726 : 11724 : spdk_thread_send_msg(pg->thread, migrate_poll_group_by_rr, NULL);
727 : : }
728 : :
729 : 2931 : return SPDK_POLLER_BUSY;
730 : : }
731 : :
732 : : static void
733 : 24 : nvmf_target_advance_state(void)
734 : : {
735 : : enum nvmf_target_state prev_state;
736 : :
737 : : do {
738 : 27 : prev_state = g_target_state;
739 : :
740 [ + + + + : 27 : switch (g_target_state) {
+ + + + +
- ]
741 : 3 : case NVMF_INIT_SUBSYSTEM:
742 : : /* initialize the bdev layer */
743 : 3 : spdk_subsystem_init(nvmf_subsystem_init_done, NULL);
744 : 3 : return;
745 : 3 : case NVMF_INIT_TARGET:
746 : 3 : nvmf_create_nvmf_tgt();
747 : 3 : break;
748 : 3 : case NVMF_INIT_POLL_GROUPS:
749 : 3 : nvmf_poll_groups_create();
750 : 3 : break;
751 : 3 : case NVMF_INIT_START_SUBSYSTEMS:
752 : 3 : nvmf_tgt_start_subsystems(&g_nvmf_tgt);
753 : 3 : break;
754 : 3 : case NVMF_RUNNING:
755 [ - + - + ]: 3 : fprintf(stdout, "nvmf target is running\n");
756 [ + - ]: 3 : if (g_migrate_pg_period_us != 0) {
757 : 3 : g_migrate_pg_poller = SPDK_POLLER_REGISTER(migrate_poll_groups_by_rr, NULL,
758 : : g_migrate_pg_period_us);
759 : : }
760 : 3 : break;
761 : 3 : case NVMF_FINI_STOP_SUBSYSTEMS:
762 : 3 : spdk_poller_unregister(&g_migrate_pg_poller);
763 : 3 : nvmf_tgt_stop_subsystems(&g_nvmf_tgt);
764 : 3 : break;
765 : 3 : case NVMF_FINI_POLL_GROUPS:
766 : 3 : nvmf_poll_groups_destroy();
767 : 3 : break;
768 : 3 : case NVMF_FINI_TARGET:
769 : 3 : nvmf_destroy_nvmf_tgt();
770 : 3 : break;
771 : 3 : case NVMF_FINI_SUBSYSTEM:
772 : 3 : spdk_subsystem_fini(nvmf_subsystem_fini_done, NULL);
773 : 3 : break;
774 : : }
775 [ + + ]: 24 : } while (g_target_state != prev_state);
776 : : }
777 : :
778 : : static void
779 : 3 : nvmf_target_app_start(void *arg)
780 : : {
781 : 3 : g_target_state = NVMF_INIT_SUBSYSTEM;
782 : 3 : nvmf_target_advance_state();
783 : 3 : }
784 : :
785 : : static void
786 : 3 : _nvmf_shutdown_cb(void *ctx)
787 : : {
788 : : /* Still in initialization state, defer shutdown operation */
789 [ - + ]: 3 : if (g_target_state < NVMF_RUNNING) {
790 : 0 : spdk_thread_send_msg(spdk_get_thread(), _nvmf_shutdown_cb, NULL);
791 : 0 : return;
792 [ - + ]: 3 : } else if (g_target_state > NVMF_RUNNING) {
793 : : /* Already in Shutdown status, ignore the signal */
794 : 0 : return;
795 : : }
796 : :
797 : 3 : g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
798 : 3 : nvmf_target_advance_state();
799 : : }
800 : :
801 : : static void
802 : 3 : nvmf_shutdown_cb(int signo)
803 : : {
804 [ - + + - ]: 3 : if (!g_intr_received) {
805 : 3 : g_intr_received = true;
806 : 3 : spdk_thread_send_msg(g_init_thread, _nvmf_shutdown_cb, NULL);
807 : : }
808 : 3 : }
809 : :
810 : : static int
811 : 3 : nvmf_setup_signal_handlers(void)
812 : : {
813 : 0 : struct sigaction sigact;
814 : 0 : sigset_t sigmask;
815 : 3 : int signals[] = {SIGINT, SIGTERM};
816 : 3 : int num_signals = sizeof(signals) / sizeof(int);
817 : : int rc, i;
818 : :
819 : 3 : rc = sigemptyset(&sigmask);
820 [ - + ]: 3 : if (rc) {
821 [ # # ]: 0 : fprintf(stderr, "errno:%d--failed to empty signal set\n", errno);
822 : 0 : return rc;
823 : : }
824 : 3 : memset(&sigact, 0, sizeof(sigact));
825 : 3 : rc = sigemptyset(&sigact.sa_mask);
826 [ - + ]: 3 : if (rc) {
827 [ # # ]: 0 : fprintf(stderr, "errno:%d--failed to empty signal set\n", errno);
828 : 0 : return rc;
829 : : }
830 : :
831 : : /* Install the same handler for SIGINT and SIGTERM */
832 : 3 : sigact.sa_handler = nvmf_shutdown_cb;
833 : :
834 [ + + ]: 9 : for (i = 0; i < num_signals; i++) {
835 : 6 : rc = sigaction(signals[i], &sigact, NULL);
836 [ - + ]: 6 : if (rc < 0) {
837 [ # # ]: 0 : fprintf(stderr, "errno:%d--sigaction() failed\n", errno);
838 : 0 : return rc;
839 : : }
840 : 6 : rc = sigaddset(&sigmask, signals[i]);
841 [ - + ]: 6 : if (rc) {
842 [ # # ]: 0 : fprintf(stderr, "errno:%d--failed to add set\n", errno);
843 : 0 : return rc;
844 : : }
845 : : }
846 : :
847 : 3 : pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
848 : :
849 : 3 : return 0;
850 : : }
851 : :
852 : : int
853 : 3 : main(int argc, char **argv)
854 : : {
855 : : int rc;
856 : 0 : struct spdk_env_opts opts;
857 : :
858 : 3 : spdk_env_opts_init(&opts);
859 : 3 : opts.name = "nvmf-example";
860 : :
861 : 3 : rc = parse_args(argc, argv, &opts);
862 [ - + ]: 3 : if (rc != 0) {
863 : 0 : return rc;
864 : : }
865 : :
866 [ - + ]: 3 : if (spdk_env_init(&opts) < 0) {
867 [ # # # # ]: 0 : fprintf(stderr, "unable to initialize SPDK env\n");
868 : 0 : return -EINVAL;
869 : : }
870 : :
871 : : /* Initialize the threads */
872 : 3 : rc = nvmf_init_threads();
873 [ - + ]: 3 : assert(rc == 0);
874 : :
875 : : /* Send a message to the thread assigned to the main reactor
876 : : * that continues initialization. This is how we bootstrap the
877 : : * program so that all code from here on is running on an SPDK thread.
878 : : */
879 [ - + ]: 3 : assert(g_init_thread != NULL);
880 : :
881 : 3 : rc = nvmf_setup_signal_handlers();
882 [ - + ]: 3 : assert(rc == 0);
883 : :
884 : 3 : spdk_thread_send_msg(g_init_thread, nvmf_target_app_start, NULL);
885 : :
886 : 3 : nvmf_reactor_run(g_main_reactor);
887 : :
888 : 3 : spdk_env_thread_wait_all();
889 : 3 : nvmf_destroy_threads();
890 : :
891 : 3 : spdk_env_fini();
892 : :
893 : 3 : return rc;
894 : : }
|