LCOV - code coverage report
Current view: top level - spdk/examples/nvmf/nvmf - nvmf.c (source / functions) Hit Total Coverage
Test: Combined Lines: 311 470 66.2 %
Date: 2024-12-10 15:44:17 Functions: 32 33 97.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 136 617 22.0 %

           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                 :          0 : }
     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                 :          0 :         do {
     181   [ +  +  #  #  :   61865114 :                 if (spdk_ring_dequeue(nvmf_reactor->threads, (void **)&lw_thread, 1)) {
                   #  # ]
     182                 :   16376875 :                         thread = spdk_thread_get_from_ctx(lw_thread);
     183                 :            : 
     184                 :   16376875 :                         spdk_thread_poll(thread, 0, 0);
     185                 :            : 
     186         [ +  + ]:   16376875 :                         if (spdk_unlikely(spdk_thread_is_exited(thread))) {
     187                 :         12 :                                 spdk_thread_destroy(thread);
     188   [ -  +  +  +  :   16376863 :                         } else if (spdk_unlikely(lw_thread->resched)) {
             #  #  #  # ]
     189   [ #  #  #  # ]:      11960 :                                 lw_thread->resched = false;
     190                 :      11960 :                                 nvmf_schedule_spdk_thread(thread);
     191                 :          0 :                         } else {
     192   [ #  #  #  # ]:   16364903 :                                 spdk_ring_enqueue(nvmf_reactor->threads, (void **)&lw_thread, 1, NULL);
     193                 :            :                         }
     194                 :          0 :                 }
     195   [ -  +  +  + ]:   61865114 :         } while (!g_reactors_exit);
     196                 :            : 
     197                 :            :         /* free all the lightweight threads */
     198   [ +  +  #  #  :         18 :         while (spdk_ring_dequeue(nvmf_reactor->threads, (void **)&lw_thread, 1)) {
                   #  # ]
     199                 :          6 :                 thread = spdk_thread_get_from_ctx(lw_thread);
     200                 :          6 :                 spdk_set_thread(thread);
     201                 :            : 
     202         [ +  + ]:          6 :                 if (spdk_thread_is_exited(thread)) {
     203                 :          3 :                         spdk_thread_destroy(thread);
     204                 :          0 :                 } 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                 :      11975 : 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                 :      11975 :         cpumask = spdk_thread_get_cpumask(thread);
     231                 :            : 
     232                 :      11975 :         lw_thread = spdk_thread_get_ctx(thread);
     233   [ -  +  #  # ]:      11975 :         assert(lw_thread != NULL);
     234         [ -  + ]:      11975 :         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         [ -  + ]:      11975 :         pthread_mutex_lock(&g_mutex);
     241         [ +  - ]:      25256 :         for (i = 0; i < spdk_env_get_core_count(); i++) {
     242         [ +  + ]:      25256 :                 if (g_next_reactor == NULL) {
     243                 :       6312 :                         g_next_reactor = TAILQ_FIRST(&g_reactors);
     244                 :          0 :                 }
     245                 :      25256 :                 nvmf_reactor = g_next_reactor;
     246   [ #  #  #  #  :      25256 :                 g_next_reactor = TAILQ_NEXT(g_next_reactor, link);
                   #  # ]
     247                 :            : 
     248                 :            :                 /* each spdk_thread has the core affinity */
     249   [ +  +  #  #  :      25256 :                 if (spdk_cpuset_get_cpu(cpumask, nvmf_reactor->core)) {
                   #  # ]
     250   [ #  #  #  # ]:      11975 :                         spdk_ring_enqueue(nvmf_reactor->threads, (void **)&lw_thread, 1, NULL);
     251                 :      11975 :                         break;
     252                 :            :                 }
     253                 :          0 :         }
     254         [ -  + ]:      11975 :         pthread_mutex_unlock(&g_mutex);
     255                 :            : 
     256         [ -  + ]:      11975 :         if (i == spdk_env_get_core_count()) {
     257   [ #  #  #  # ]:          0 :                 fprintf(stderr, "failed to schedule spdk thread\n");
     258                 :          0 :                 return -1;
     259                 :            :         }
     260                 :      11975 :         return 0;
     261                 :          0 : }
     262                 :            : 
     263                 :            : static void
     264                 :      11984 : nvmf_request_spdk_thread_reschedule(struct spdk_thread *thread)
     265                 :            : {
     266                 :            :         struct nvmf_lw_thread *lw_thread;
     267                 :            : 
     268   [ -  +  #  # ]:      11984 :         assert(thread == spdk_get_thread());
     269                 :            : 
     270                 :      11984 :         lw_thread = spdk_thread_get_ctx(thread);
     271                 :            : 
     272   [ -  +  #  # ]:      11984 :         assert(lw_thread != NULL);
     273                 :            : 
     274   [ #  #  #  # ]:      11984 :         lw_thread->resched = true;
     275                 :      11984 : }
     276                 :            : 
     277                 :            : static int
     278                 :      11999 : nvmf_reactor_thread_op(struct spdk_thread *thread, enum spdk_thread_op op)
     279                 :            : {
     280      [ +  +  - ]:      11999 :         switch (op) {
     281                 :         15 :         case SPDK_THREAD_OP_NEW:
     282                 :         15 :                 return nvmf_schedule_spdk_thread(thread);
     283                 :      11984 :         case SPDK_THREAD_OP_RESCHED:
     284                 :      11984 :                 nvmf_request_spdk_thread_reschedule(thread);
     285                 :      11984 :                 return 0;
     286                 :          0 :         default:
     287                 :          0 :                 return -ENOTSUP;
     288                 :            :         }
     289                 :          0 : }
     290                 :            : 
     291                 :            : static bool
     292                 :      11999 : nvmf_reactor_thread_op_supported(enum spdk_thread_op op)
     293                 :            : {
     294         [ +  - ]:      11999 :         switch (op) {
     295                 :      11999 :         case SPDK_THREAD_OP_NEW:
     296                 :            :         case SPDK_THREAD_OP_RESCHED:
     297                 :      11999 :                 return true;
     298                 :          0 :         default:
     299                 :          0 :                 return false;
     300                 :            :         }
     301                 :          0 : }
     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_NUMA_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                 :          0 :                 } else {
     354                 :          9 :                         rc = spdk_env_thread_launch_pinned(i,
     355                 :            :                                                            nvmf_reactor_run,
     356                 :          0 :                                                            nvmf_reactor);
     357         [ -  + ]:          9 :                         if (rc) {
     358   [ #  #  #  # ]:          0 :                                 fprintf(stderr, "failed to pin reactor launch\n");
     359                 :          0 :                                 goto err_exit;
     360                 :            :                         }
     361                 :            :                 }
     362                 :          0 :         }
     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                 :          0 : }
     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                 :          0 :         }
     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                 :          0 :         } 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.size = SPDK_SIZEOF(&tgt_opts, discovery_filter);
     422   [ #  #  #  # ]:          3 :         tgt_opts.max_subsystems = g_nvmf_tgt.max_subsystems;
     423         [ -  + ]:          3 :         snprintf(tgt_opts.name, sizeof(tgt_opts.name), "%s", "nvmf_example");
     424                 :            :         /* Construct the default NVMe-oF target
     425                 :            :          * An NVMe-oF target is a collection of subsystems, namespace, and poll
     426                 :            :          * groups, and defines the scope of the NVMe-oF discovery service.
     427                 :            :          */
     428                 :          3 :         g_nvmf_tgt.tgt = spdk_nvmf_tgt_create(&tgt_opts);
     429         [ -  + ]:          3 :         if (g_nvmf_tgt.tgt == NULL) {
     430   [ #  #  #  # ]:          0 :                 fprintf(stderr, "spdk_nvmf_tgt_create() failed\n");
     431                 :          0 :                 goto error;
     432                 :            :         }
     433                 :            : 
     434                 :            :         /* Create and add discovery subsystem to the NVMe-oF target.
     435                 :            :          * NVMe-oF defines a discovery mechanism that a host uses to determine
     436                 :            :          * the NVM subsystems that expose namespaces that the host may access.
     437                 :            :          * It provides a host with following capabilities:
     438                 :            :          *      1,The ability to discover a list of NVM subsystems with namespaces
     439                 :            :          *        that are accessible to the host.
     440                 :            :          *      2,The ability to discover multiple paths to an NVM subsystem.
     441                 :            :          *      3,The ability to discover controllers that are statically configured.
     442                 :            :          */
     443                 :          3 :         subsystem = spdk_nvmf_subsystem_create(g_nvmf_tgt.tgt, SPDK_NVMF_DISCOVERY_NQN,
     444                 :            :                                                SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT, 0);
     445         [ -  + ]:          3 :         if (subsystem == NULL) {
     446   [ #  #  #  # ]:          0 :                 fprintf(stderr, "failed to create discovery nvmf library subsystem\n");
     447                 :          0 :                 goto error;
     448                 :            :         }
     449                 :            : 
     450                 :            :         /* Allow any host to access the discovery subsystem */
     451                 :          3 :         spdk_nvmf_subsystem_set_allow_any_host(subsystem, true);
     452                 :            : 
     453   [ -  +  -  + ]:          3 :         fprintf(stdout, "created a nvmf target service\n");
     454                 :            : 
     455                 :          3 :         g_target_state = NVMF_INIT_POLL_GROUPS;
     456                 :          3 :         return;
     457                 :            : 
     458                 :          0 : error:
     459                 :          0 :         g_target_state = NVMF_FINI_TARGET;
     460                 :          0 : }
     461                 :            : 
     462                 :            : static void
     463                 :          6 : nvmf_tgt_subsystem_stop_next(struct spdk_nvmf_subsystem *subsystem,
     464                 :            :                              void *cb_arg, int status)
     465                 :            : {
     466                 :            :         int rc;
     467                 :            : 
     468                 :          6 :         subsystem = spdk_nvmf_subsystem_get_next(subsystem);
     469         [ +  + ]:          6 :         if (subsystem) {
     470                 :          3 :                 rc = spdk_nvmf_subsystem_stop(subsystem,
     471                 :            :                                               nvmf_tgt_subsystem_stop_next,
     472                 :          0 :                                               cb_arg);
     473         [ -  + ]:          3 :                 if (rc) {
     474                 :          0 :                         nvmf_tgt_subsystem_stop_next(subsystem, cb_arg, 0);
     475   [ #  #  #  # ]:          0 :                         fprintf(stderr, "Unable to stop NVMe-oF subsystem. Trying others.\n");
     476                 :          0 :                 }
     477                 :          3 :                 return;
     478                 :            :         }
     479                 :            : 
     480   [ -  +  -  + ]:          3 :         fprintf(stdout, "all subsystems of target stopped\n");
     481                 :            : 
     482                 :          3 :         g_target_state = NVMF_FINI_POLL_GROUPS;
     483                 :          3 :         nvmf_target_advance_state();
     484                 :          0 : }
     485                 :            : 
     486                 :            : static void
     487                 :          3 : nvmf_tgt_stop_subsystems(struct nvmf_target *nvmf_tgt)
     488                 :            : {
     489                 :            :         struct spdk_nvmf_subsystem *subsystem;
     490                 :            :         int rc;
     491                 :            : 
     492   [ #  #  #  # ]:          3 :         subsystem = spdk_nvmf_subsystem_get_first(nvmf_tgt->tgt);
     493         [ +  - ]:          3 :         if (spdk_likely(subsystem)) {
     494                 :          3 :                 rc = spdk_nvmf_subsystem_stop(subsystem,
     495                 :            :                                               nvmf_tgt_subsystem_stop_next,
     496                 :            :                                               NULL);
     497         [ -  + ]:          3 :                 if (rc) {
     498                 :          0 :                         nvmf_tgt_subsystem_stop_next(subsystem, NULL, 0);
     499         [ #  # ]:          0 :                         fprintf(stderr, "Unable to stop NVMe-oF subsystem. Trying others.\n");
     500                 :          0 :                 }
     501                 :          0 :         } else {
     502                 :          0 :                 g_target_state = NVMF_FINI_POLL_GROUPS;
     503                 :            :         }
     504                 :          3 : }
     505                 :            : 
     506                 :            : static void
     507                 :          3 : nvmf_tgt_subsystem_start_next(struct spdk_nvmf_subsystem *subsystem,
     508                 :            :                               void *cb_arg, int status)
     509                 :            : {
     510                 :            :         int rc;
     511                 :            : 
     512                 :          3 :         subsystem = spdk_nvmf_subsystem_get_next(subsystem);
     513         [ -  + ]:          3 :         if (subsystem) {
     514                 :          0 :                 rc = spdk_nvmf_subsystem_start(subsystem, nvmf_tgt_subsystem_start_next,
     515                 :          0 :                                                cb_arg);
     516         [ #  # ]:          0 :                 if (rc) {
     517                 :          0 :                         g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
     518   [ #  #  #  # ]:          0 :                         fprintf(stderr, "Unable to start NVMe-oF subsystem. shutting down app.\n");
     519                 :          0 :                         nvmf_target_advance_state();
     520                 :          0 :                 }
     521                 :          0 :                 return;
     522                 :            :         }
     523                 :            : 
     524   [ -  +  -  + ]:          3 :         fprintf(stdout, "all subsystems of target started\n");
     525                 :            : 
     526                 :          3 :         g_target_state = NVMF_RUNNING;
     527                 :          3 :         nvmf_target_advance_state();
     528                 :          0 : }
     529                 :            : 
     530                 :            : static void
     531                 :          3 : nvmf_tgt_start_subsystems(struct nvmf_target *nvmf_tgt)
     532                 :            : {
     533                 :            :         struct spdk_nvmf_subsystem *subsystem;
     534                 :            :         int rc;
     535                 :            : 
     536                 :            :         /* Subsystem is the NVM subsystem which is a combine of namespaces
     537                 :            :          * except the discovery subsystem which is used for discovery service.
     538                 :            :          * It also controls the hosts that means the subsystem determines whether
     539                 :            :          * the host can access this subsystem.
     540                 :            :          */
     541   [ #  #  #  # ]:          3 :         subsystem = spdk_nvmf_subsystem_get_first(nvmf_tgt->tgt);
     542         [ +  - ]:          3 :         if (spdk_likely(subsystem)) {
     543                 :            :                 /* In SPDK there are three states in subsystem: Inactive, Active, Paused.
     544                 :            :                  * Start subsystem means make it from inactive to active that means
     545                 :            :                  * subsystem start to work or it can be accessed.
     546                 :            :                  */
     547                 :          3 :                 rc = spdk_nvmf_subsystem_start(subsystem,
     548                 :            :                                                nvmf_tgt_subsystem_start_next,
     549                 :            :                                                NULL);
     550         [ -  + ]:          3 :                 if (rc) {
     551         [ #  # ]:          0 :                         fprintf(stderr, "Unable to start NVMe-oF subsystem. shutting down app.\n");
     552                 :          0 :                         g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
     553                 :          0 :                 }
     554                 :          0 :         } else {
     555                 :          0 :                 g_target_state = NVMF_RUNNING;
     556                 :            :         }
     557                 :          3 : }
     558                 :            : 
     559                 :            : static void
     560                 :         12 : nvmf_tgt_create_poll_groups_done(void *ctx)
     561                 :            : {
     562                 :         12 :         struct nvmf_target_poll_group *pg = ctx;
     563                 :            : 
     564         [ +  + ]:         12 :         if (!g_next_pg) {
     565                 :          3 :                 g_next_pg = pg;
     566                 :          0 :         }
     567                 :            : 
     568   [ #  #  #  #  :         12 :         TAILQ_INSERT_TAIL(&g_poll_groups, pg, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     569                 :            : 
     570   [ -  +  #  # ]:         12 :         assert(g_num_poll_groups < spdk_env_get_core_count());
     571                 :            : 
     572         [ +  + ]:         12 :         if (++g_num_poll_groups == spdk_env_get_core_count()) {
     573   [ -  +  -  + ]:          3 :                 fprintf(stdout, "create targets's poll groups done\n");
     574                 :            : 
     575                 :          3 :                 g_target_state = NVMF_INIT_START_SUBSYSTEMS;
     576                 :          3 :                 nvmf_target_advance_state();
     577                 :          0 :         }
     578                 :         12 : }
     579                 :            : 
     580                 :            : static void
     581                 :         12 : nvmf_tgt_create_poll_group(void *ctx)
     582                 :            : {
     583                 :            :         struct nvmf_target_poll_group *pg;
     584                 :            : 
     585                 :         12 :         pg = calloc(1, sizeof(struct nvmf_target_poll_group));
     586         [ -  + ]:         12 :         if (!pg) {
     587   [ #  #  #  # ]:          0 :                 fprintf(stderr, "failed to allocate poll group\n");
     588         [ #  # ]:          0 :                 assert(false);
     589                 :            :                 return;
     590                 :            :         }
     591                 :            : 
     592   [ #  #  #  # ]:         12 :         pg->thread = spdk_get_thread();
     593   [ #  #  #  # ]:         12 :         pg->group = spdk_nvmf_poll_group_create(g_nvmf_tgt.tgt);
     594   [ -  +  #  #  :         12 :         if (!pg->group) {
                   #  # ]
     595   [ #  #  #  # ]:          0 :                 fprintf(stderr, "failed to create poll group of the target\n");
     596                 :          0 :                 free(pg);
     597         [ #  # ]:          0 :                 assert(false);
     598                 :            :                 return;
     599                 :            :         }
     600                 :            : 
     601                 :         12 :         spdk_thread_send_msg(g_init_thread, nvmf_tgt_create_poll_groups_done, pg);
     602                 :          0 : }
     603                 :            : 
     604                 :            : /* Create a lightweight thread per poll group instead of assuming a pool of lightweight
     605                 :            :  * threads already exist at start up time. A poll group is a collection of unrelated NVMe-oF
     606                 :            :  * connections. Each poll group is only accessed from the associated lightweight thread.
     607                 :            :  */
     608                 :            : static void
     609                 :          3 : nvmf_poll_groups_create(void)
     610                 :            : {
     611                 :          3 :         struct spdk_cpuset tmp_cpumask = {};
     612                 :            :         uint32_t i;
     613                 :          0 :         char thread_name[32];
     614                 :            :         struct spdk_thread *thread;
     615                 :            : 
     616   [ -  +  #  # ]:          3 :         assert(g_init_thread != NULL);
     617                 :            : 
     618         [ +  + ]:         15 :         SPDK_ENV_FOREACH_CORE(i) {
     619                 :         12 :                 spdk_cpuset_zero(&tmp_cpumask);
     620                 :         12 :                 spdk_cpuset_set_cpu(&tmp_cpumask, i, true);
     621         [ -  + ]:         12 :                 snprintf(thread_name, sizeof(thread_name), "nvmf_tgt_poll_group_%03u", i);
     622                 :            : 
     623                 :         12 :                 thread = spdk_thread_create(thread_name, &tmp_cpumask);
     624   [ -  +  #  # ]:         12 :                 assert(thread != NULL);
     625                 :            : 
     626                 :         12 :                 spdk_thread_send_msg(thread, nvmf_tgt_create_poll_group, NULL);
     627                 :          0 :         }
     628                 :          3 : }
     629                 :            : 
     630                 :            : static void
     631                 :         12 : _nvmf_tgt_destroy_poll_groups_done(void *ctx)
     632                 :            : {
     633   [ -  +  #  # ]:         12 :         assert(g_num_poll_groups > 0);
     634                 :            : 
     635         [ +  + ]:         12 :         if (--g_num_poll_groups == 0) {
     636   [ -  +  -  + ]:          3 :                 fprintf(stdout, "destroy targets's poll groups done\n");
     637                 :            : 
     638                 :          3 :                 g_target_state = NVMF_FINI_TARGET;
     639                 :          3 :                 nvmf_target_advance_state();
     640                 :          0 :         }
     641                 :         12 : }
     642                 :            : 
     643                 :            : static void
     644                 :         12 : nvmf_tgt_destroy_poll_groups_done(void *cb_arg, int status)
     645                 :            : {
     646                 :         12 :         struct nvmf_target_poll_group *pg = cb_arg;
     647                 :            : 
     648                 :         12 :         free(pg);
     649                 :            : 
     650                 :         12 :         spdk_thread_send_msg(g_fini_thread, _nvmf_tgt_destroy_poll_groups_done, NULL);
     651                 :            : 
     652                 :         12 :         spdk_thread_exit(spdk_get_thread());
     653                 :         12 : }
     654                 :            : 
     655                 :            : static void
     656                 :         12 : nvmf_tgt_destroy_poll_group(void *ctx)
     657                 :            : {
     658                 :         12 :         struct nvmf_target_poll_group *pg = ctx;
     659                 :            : 
     660   [ #  #  #  # ]:         12 :         spdk_nvmf_poll_group_destroy(pg->group, nvmf_tgt_destroy_poll_groups_done, pg);
     661                 :         12 : }
     662                 :            : 
     663                 :            : static void
     664                 :          3 : nvmf_poll_groups_destroy(void)
     665                 :            : {
     666                 :            :         struct nvmf_target_poll_group *pg, *tmp;
     667                 :            : 
     668                 :          3 :         g_fini_thread = spdk_get_thread();
     669   [ -  +  #  # ]:          3 :         assert(g_fini_thread != NULL);
     670                 :            : 
     671   [ +  +  #  #  :         15 :         TAILQ_FOREACH_SAFE(pg, &g_poll_groups, link, tmp) {
          #  #  #  #  #  
                      # ]
     672   [ +  +  #  #  :         12 :                 TAILQ_REMOVE(&g_poll_groups, pg, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     673   [ #  #  #  # ]:         12 :                 spdk_thread_send_msg(pg->thread, nvmf_tgt_destroy_poll_group, pg);
     674                 :          0 :         }
     675                 :          3 : }
     676                 :            : 
     677                 :            : static void
     678                 :          3 : nvmf_subsystem_fini_done(void *cb_arg)
     679                 :            : {
     680   [ -  +  -  + ]:          3 :         fprintf(stdout, "bdev subsystem finish successfully\n");
     681                 :          3 :         spdk_rpc_finish();
     682                 :          3 :         g_reactors_exit = true;
     683                 :          3 : }
     684                 :            : 
     685                 :            : static void
     686                 :          3 : nvmf_subsystem_init_done(int rc, void *cb_arg)
     687                 :            : {
     688   [ -  +  -  + ]:          3 :         fprintf(stdout, "bdev subsystem init successfully\n");
     689                 :            : 
     690                 :          3 :         rc = spdk_rpc_initialize(g_rpc_addr, NULL);
     691         [ -  + ]:          3 :         if (rc) {
     692                 :          0 :                 spdk_app_stop(rc);
     693                 :          0 :                 return;
     694                 :            :         }
     695                 :            : 
     696                 :          3 :         spdk_rpc_set_state(SPDK_RPC_RUNTIME);
     697                 :            : 
     698                 :          3 :         g_target_state = NVMF_INIT_TARGET;
     699                 :          3 :         nvmf_target_advance_state();
     700                 :          0 : }
     701                 :            : 
     702                 :            : static void
     703                 :      11984 : migrate_poll_group_by_rr(void *ctx)
     704                 :            : {
     705                 :            :         uint32_t current_core, next_core;
     706                 :      11984 :         struct spdk_cpuset cpumask = {};
     707                 :            : 
     708                 :      11984 :         current_core = spdk_env_get_current_core();
     709                 :      11984 :         next_core = spdk_env_get_next_core(current_core);
     710         [ +  + ]:      11984 :         if (next_core == UINT32_MAX) {
     711                 :       3011 :                 next_core = spdk_env_get_first_core();
     712                 :          0 :         }
     713                 :            : 
     714                 :      11984 :         spdk_cpuset_set_cpu(&cpumask, next_core, true);
     715                 :            : 
     716                 :      11984 :         spdk_thread_set_cpumask(&cpumask);
     717                 :      11984 : }
     718                 :            : 
     719                 :            : static int
     720                 :       2996 : migrate_poll_groups_by_rr(void *ctx)
     721                 :            : {
     722                 :            :         struct nvmf_target_poll_group *pg;
     723                 :            : 
     724   [ +  +  #  #  :      14980 :         TAILQ_FOREACH(pg, &g_poll_groups, link) {
             #  #  #  # ]
     725   [ #  #  #  # ]:      11984 :                 spdk_thread_send_msg(pg->thread, migrate_poll_group_by_rr, NULL);
     726                 :          0 :         }
     727                 :            : 
     728                 :       2996 :         return SPDK_POLLER_BUSY;
     729                 :            : }
     730                 :            : 
     731                 :            : static void
     732                 :         24 : nvmf_target_advance_state(void)
     733                 :            : {
     734                 :            :         enum nvmf_target_state prev_state;
     735                 :            : 
     736                 :          0 :         do {
     737                 :         27 :                 prev_state = g_target_state;
     738                 :            : 
     739   [ +  +  +  +  :         27 :                 switch (g_target_state) {
          +  +  +  +  +  
                      - ]
     740                 :          3 :                 case NVMF_INIT_SUBSYSTEM:
     741                 :            :                         /* initialize the bdev layer */
     742                 :          3 :                         spdk_subsystem_init(nvmf_subsystem_init_done, NULL);
     743                 :          3 :                         return;
     744                 :          3 :                 case NVMF_INIT_TARGET:
     745                 :          3 :                         nvmf_create_nvmf_tgt();
     746                 :          3 :                         break;
     747                 :          3 :                 case NVMF_INIT_POLL_GROUPS:
     748                 :          3 :                         nvmf_poll_groups_create();
     749                 :          3 :                         break;
     750                 :          3 :                 case NVMF_INIT_START_SUBSYSTEMS:
     751                 :          3 :                         nvmf_tgt_start_subsystems(&g_nvmf_tgt);
     752                 :          3 :                         break;
     753                 :          3 :                 case NVMF_RUNNING:
     754   [ -  +  -  + ]:          3 :                         fprintf(stdout, "nvmf target is running\n");
     755         [ +  - ]:          3 :                         if (g_migrate_pg_period_us != 0) {
     756                 :          3 :                                 g_migrate_pg_poller = SPDK_POLLER_REGISTER(migrate_poll_groups_by_rr, NULL,
     757                 :            :                                                       g_migrate_pg_period_us);
     758                 :          0 :                         }
     759                 :          3 :                         break;
     760                 :          3 :                 case NVMF_FINI_STOP_SUBSYSTEMS:
     761                 :          3 :                         spdk_poller_unregister(&g_migrate_pg_poller);
     762                 :          3 :                         nvmf_tgt_stop_subsystems(&g_nvmf_tgt);
     763                 :          3 :                         break;
     764                 :          3 :                 case NVMF_FINI_POLL_GROUPS:
     765                 :          3 :                         nvmf_poll_groups_destroy();
     766                 :          3 :                         break;
     767                 :          3 :                 case NVMF_FINI_TARGET:
     768                 :          3 :                         nvmf_destroy_nvmf_tgt();
     769                 :          3 :                         break;
     770                 :          3 :                 case NVMF_FINI_SUBSYSTEM:
     771                 :          3 :                         spdk_subsystem_fini(nvmf_subsystem_fini_done, NULL);
     772                 :          3 :                         break;
     773                 :            :                 }
     774         [ +  + ]:         24 :         } while (g_target_state != prev_state);
     775                 :          0 : }
     776                 :            : 
     777                 :            : static void
     778                 :          3 : nvmf_target_app_start(void *arg)
     779                 :            : {
     780                 :          3 :         g_target_state = NVMF_INIT_SUBSYSTEM;
     781                 :          3 :         nvmf_target_advance_state();
     782                 :          3 : }
     783                 :            : 
     784                 :            : static void
     785                 :          3 : _nvmf_shutdown_cb(void *ctx)
     786                 :            : {
     787                 :            :         /* Still in initialization state, defer shutdown operation */
     788         [ -  + ]:          3 :         if (g_target_state < NVMF_RUNNING) {
     789                 :          0 :                 spdk_thread_send_msg(spdk_get_thread(), _nvmf_shutdown_cb, NULL);
     790                 :          0 :                 return;
     791         [ -  + ]:          3 :         } else if (g_target_state > NVMF_RUNNING) {
     792                 :            :                 /* Already in Shutdown status, ignore the signal */
     793                 :          0 :                 return;
     794                 :            :         }
     795                 :            : 
     796                 :          3 :         g_target_state = NVMF_FINI_STOP_SUBSYSTEMS;
     797                 :          3 :         nvmf_target_advance_state();
     798                 :          0 : }
     799                 :            : 
     800                 :            : static void
     801                 :          3 : nvmf_shutdown_cb(int signo)
     802                 :            : {
     803   [ -  +  +  - ]:          3 :         if (!g_intr_received) {
     804                 :          3 :                 g_intr_received = true;
     805                 :          3 :                 spdk_thread_send_msg(g_init_thread, _nvmf_shutdown_cb, NULL);
     806                 :          0 :         }
     807                 :          3 : }
     808                 :            : 
     809                 :            : static int
     810                 :          3 : nvmf_setup_signal_handlers(void)
     811                 :            : {
     812                 :          0 :         struct sigaction        sigact;
     813                 :          0 :         sigset_t                sigmask;
     814                 :          3 :         int                     signals[] = {SIGINT, SIGTERM};
     815                 :          3 :         int                     num_signals = sizeof(signals) / sizeof(int);
     816                 :            :         int                     rc, i;
     817                 :            : 
     818         [ #  # ]:          3 :         rc = sigemptyset(&sigmask);
     819         [ -  + ]:          3 :         if (rc) {
     820         [ #  # ]:          0 :                 fprintf(stderr, "errno:%d--failed to empty signal set\n", errno);
     821                 :          0 :                 return rc;
     822                 :            :         }
     823         [ #  # ]:          3 :         memset(&sigact, 0, sizeof(sigact));
     824         [ #  # ]:          3 :         rc = sigemptyset(&sigact.sa_mask);
     825         [ -  + ]:          3 :         if (rc) {
     826         [ #  # ]:          0 :                 fprintf(stderr, "errno:%d--failed to empty signal set\n", errno);
     827                 :          0 :                 return rc;
     828                 :            :         }
     829                 :            : 
     830                 :            :         /* Install the same handler for SIGINT and SIGTERM */
     831                 :          3 :         sigact.sa_handler = nvmf_shutdown_cb;
     832                 :            : 
     833   [ +  +  #  # ]:          9 :         for (i = 0; i < num_signals; i++) {
     834   [ #  #  #  #  :          6 :                 rc = sigaction(signals[i], &sigact, NULL);
                   #  # ]
     835         [ -  + ]:          6 :                 if (rc < 0) {
     836         [ #  # ]:          0 :                         fprintf(stderr, "errno:%d--sigaction() failed\n", errno);
     837                 :          0 :                         return rc;
     838                 :            :                 }
     839   [ #  #  #  #  :          6 :                 rc = sigaddset(&sigmask, signals[i]);
             #  #  #  # ]
     840         [ -  + ]:          6 :                 if (rc) {
     841         [ #  # ]:          0 :                         fprintf(stderr, "errno:%d--failed to add set\n", errno);
     842                 :          0 :                         return rc;
     843                 :            :                 }
     844                 :          0 :         }
     845                 :            : 
     846                 :          3 :         pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL);
     847                 :            : 
     848                 :          3 :         return 0;
     849                 :          0 : }
     850                 :            : 
     851                 :            : int
     852                 :          3 : main(int argc, char **argv)
     853                 :            : {
     854                 :            :         int rc;
     855                 :          0 :         struct spdk_env_opts opts;
     856                 :            : 
     857         [ #  # ]:          3 :         opts.opts_size = sizeof(opts);
     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                 :          0 : }

Generated by: LCOV version 1.15