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