LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/nvmf/subsystem.c - subsystem_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1264 1343 94.1 %
Date: 2024-11-15 11:38:22 Functions: 57 113 50.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 496 678 73.2 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "common/lib/ut_multithread.c"
       9                 :            : #include "spdk_internal/cunit.h"
      10                 :            : #include "spdk/nvmf.h"
      11                 :            : #include "spdk_internal/mock.h"
      12                 :            : 
      13                 :            : #include "spdk/bdev_module.h"
      14                 :            : #include "nvmf/subsystem.c"
      15                 :            : #include "nvmf/transport.c"
      16                 :            : 
      17                 :          4 : SPDK_LOG_REGISTER_COMPONENT(nvmf)
      18                 :            : 
      19   [ -  +  #  # ]:         20 : DEFINE_STUB(spdk_bdev_module_claim_bdev,
      20                 :            :             int,
      21                 :            :             (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
      22                 :            :              struct spdk_bdev_module *module), 0);
      23                 :            : 
      24                 :         24 : DEFINE_STUB_V(spdk_bdev_module_release_bdev,
      25                 :            :               (struct spdk_bdev *bdev));
      26                 :            : 
      27   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_desc_get_block_size, uint32_t,
      28                 :            :             (struct spdk_bdev_desc *desc), 512);
      29                 :            : 
      30   [ -  +  #  # ]:         20 : DEFINE_STUB(spdk_bdev_desc_get_md_size, uint32_t,
      31                 :            :             (struct spdk_bdev_desc *desc), 0);
      32                 :            : 
      33   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_desc_is_md_interleaved, bool,
      34                 :            :             (struct spdk_bdev_desc *desc), false);
      35                 :            : 
      36   [ -  +  -  + ]:         20 : DEFINE_STUB(spdk_bdev_io_type_supported, bool,
      37                 :            :             (struct spdk_bdev *bdev,
      38                 :            :              enum spdk_bdev_io_type io_type), false);
      39                 :            : 
      40                 :          0 : DEFINE_STUB_V(spdk_nvmf_send_discovery_log_notice,
      41                 :            :               (struct spdk_nvmf_tgt *tgt, const char *hostnqn));
      42   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_nvmf_qpair_disconnect, int, (struct spdk_nvmf_qpair *qpair), 0);
      43                 :            : 
      44   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_nvmf_request_complete,
      45                 :            :             int,
      46                 :            :             (struct spdk_nvmf_request *req), 0);
      47                 :            : 
      48   [ #  #  #  # ]:          0 : DEFINE_STUB(nvmf_ctrlr_async_event_ana_change_notice,
      49                 :            :             int,
      50                 :            :             (struct spdk_nvmf_ctrlr *ctrlr), 0);
      51                 :            : 
      52   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_nvme_transport_id_trtype_str,
      53                 :            :             const char *,
      54                 :            :             (enum spdk_nvme_transport_type trtype), NULL);
      55                 :            : 
      56   [ -  +  -  + ]:         20 : DEFINE_STUB(spdk_bdev_is_zoned, bool,
      57                 :            :             (const struct spdk_bdev *bdev), false);
      58                 :            : 
      59   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_max_zone_append_size, uint32_t,
      60                 :            :             (const struct spdk_bdev *bdev), 0);
      61                 :            : 
      62   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_mempool_lookup, struct spdk_mempool *,
      63                 :            :             (const char *name), NULL);
      64                 :            : 
      65   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_nvme_transport_id_adrfam_str, const char *,
      66                 :            :             (enum spdk_nvmf_adrfam adrfam), NULL);
      67                 :            : 
      68   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_nvmf_qpair_get_listen_trid, int,
      69                 :            :             (struct spdk_nvmf_qpair *qpair,
      70                 :            :              struct spdk_nvme_transport_id *trid), 0);
      71   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_key_dup, struct spdk_key *, (struct spdk_key *k), NULL);
      72   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_key_get_name, const char *, (struct spdk_key *k), NULL);
      73                 :         16 : DEFINE_STUB_V(spdk_keyring_put_key, (struct spdk_key *k));
      74   [ #  #  #  # ]:          0 : DEFINE_STUB(nvmf_auth_is_supported, bool, (void), false);
      75   [ #  #  #  # ]:          0 : DEFINE_STUB(nvmf_tgt_update_mdns_prr, int, (struct spdk_nvmf_tgt *tgt), 0);
      76                 :            : 
      77   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_module_name, const char *, (const struct spdk_bdev *bdev), "nvme");
      78   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_module_ctx, void *, (struct spdk_bdev_desc *desc), NULL);
      79   [ -  +  #  # ]:         20 : DEFINE_STUB(spdk_bdev_get_nvme_nsid, uint32_t, (struct spdk_bdev *bdev), 0);
      80                 :            : 
      81                 :            : static struct spdk_nvmf_transport g_transport = {};
      82                 :            : 
      83                 :            : struct spdk_nvmf_subsystem *
      84                 :         88 : spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn)
      85                 :            : {
      86                 :         88 :         return NULL;
      87                 :            : }
      88                 :            : 
      89                 :            : struct spdk_nvmf_transport *
      90                 :          0 : spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name)
      91                 :            : {
      92   [ #  #  #  # ]:          0 :         if (strncmp(transport_name, SPDK_NVME_TRANSPORT_NAME_RDMA, SPDK_NVMF_TRSTRING_MAX_LEN)) {
      93                 :          0 :                 return &g_transport;
      94                 :            :         }
      95                 :            : 
      96                 :          0 :         return NULL;
      97                 :          0 : }
      98                 :            : 
      99                 :            : int
     100                 :          0 : nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
     101                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     102                 :            : {
     103                 :          0 :         return 0;
     104                 :            : }
     105                 :            : 
     106                 :            : int
     107                 :          0 : nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
     108                 :            :                               struct spdk_nvmf_subsystem *subsystem,
     109                 :            :                               spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
     110                 :            : {
     111                 :          0 :         return 0;
     112                 :            : }
     113                 :            : 
     114                 :            : void
     115                 :          0 : nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
     116                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
     117                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
     118                 :            : {
     119                 :          0 : }
     120                 :            : 
     121                 :            : void
     122                 :          0 : nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
     123                 :            :                                 struct spdk_nvmf_subsystem *subsystem,
     124                 :            :                                 uint32_t nsid,
     125                 :            :                                 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
     126                 :            : {
     127                 :          0 : }
     128                 :            : 
     129                 :            : void
     130                 :          0 : nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
     131                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
     132                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
     133                 :            : {
     134                 :          0 : }
     135                 :            : 
     136                 :            : int
     137                 :          0 : spdk_nvme_transport_id_parse_trtype(enum spdk_nvme_transport_type *trtype, const char *str)
     138                 :            : {
     139   [ #  #  #  # ]:          0 :         if (trtype == NULL || str == NULL) {
     140                 :          0 :                 return -EINVAL;
     141                 :            :         }
     142                 :            : 
     143   [ #  #  #  # ]:          0 :         if (strcasecmp(str, "PCIe") == 0) {
     144                 :          0 :                 *trtype = SPDK_NVME_TRANSPORT_PCIE;
     145   [ #  #  #  # ]:          0 :         } else if (strcasecmp(str, "RDMA") == 0) {
     146                 :          0 :                 *trtype = SPDK_NVME_TRANSPORT_RDMA;
     147                 :          0 :         } else {
     148                 :          0 :                 return -ENOENT;
     149                 :            :         }
     150                 :          0 :         return 0;
     151                 :          0 : }
     152                 :            : 
     153                 :            : int
     154                 :          0 : spdk_nvme_transport_id_compare(const struct spdk_nvme_transport_id *trid1,
     155                 :            :                                const struct spdk_nvme_transport_id *trid2)
     156                 :            : {
     157                 :          0 :         return 0;
     158                 :            : }
     159                 :            : 
     160                 :            : int32_t
     161                 :          0 : spdk_nvme_ctrlr_process_admin_completions(struct spdk_nvme_ctrlr *ctrlr)
     162                 :            : {
     163                 :          0 :         return -1;
     164                 :            : }
     165                 :            : 
     166                 :            : int32_t
     167                 :          0 : spdk_nvme_qpair_process_completions(struct spdk_nvme_qpair *qpair, uint32_t max_completions)
     168                 :            : {
     169                 :          0 :         return -1;
     170                 :            : }
     171                 :            : 
     172                 :            : int
     173                 :          0 : spdk_nvme_detach(struct spdk_nvme_ctrlr *ctrlr)
     174                 :            : {
     175                 :          0 :         return -1;
     176                 :            : }
     177                 :            : 
     178                 :            : void
     179                 :          0 : nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr)
     180                 :            : {
     181                 :          0 : }
     182                 :            : 
     183                 :            : static struct spdk_nvmf_ctrlr *g_ns_changed_ctrlr = NULL;
     184                 :            : static uint32_t g_ns_changed_nsid = 0;
     185                 :            : void
     186                 :         24 : nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid)
     187                 :            : {
     188                 :         24 :         g_ns_changed_ctrlr = ctrlr;
     189                 :         24 :         g_ns_changed_nsid = nsid;
     190                 :         24 : }
     191                 :            : 
     192                 :            : 
     193                 :            : static struct spdk_nvmf_ctrlr *g_async_event_ctrlr = NULL;
     194                 :            : int
     195                 :         12 : nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr)
     196                 :            : {
     197                 :         12 :         g_async_event_ctrlr = ctrlr;
     198                 :         12 :         return 0;
     199                 :            : }
     200                 :            : 
     201                 :            : static struct spdk_bdev g_bdevs[] = {
     202                 :            :         { .name = "bdev1" },
     203                 :            :         { .name = "bdev2" },
     204                 :            :         { .name = "bdev3", .ctratt.raw = 0x80000 },
     205                 :            : };
     206                 :            : 
     207                 :            : struct spdk_bdev_desc {
     208                 :            :         struct spdk_bdev        *bdev;
     209                 :            : };
     210                 :            : 
     211                 :            : int
     212                 :         20 : spdk_bdev_open_ext_v2(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
     213                 :            :                       void *event_ctx, struct spdk_bdev_open_opts *opts,
     214                 :            :                       struct spdk_bdev_desc **_desc)
     215                 :            : {
     216                 :            :         struct spdk_bdev_desc *desc;
     217                 :            :         size_t i;
     218                 :            : 
     219         [ +  - ]:         36 :         for (i = 0; i < sizeof(g_bdevs); i++) {
     220   [ +  +  -  +  :         36 :                 if (strcmp(bdev_name, g_bdevs[i].name) == 0) {
                   +  + ]
     221                 :            : 
     222                 :         20 :                         desc = calloc(1, sizeof(*desc));
     223         [ -  + ]:         20 :                         SPDK_CU_ASSERT_FATAL(desc != NULL);
     224                 :            : 
     225                 :         20 :                         desc->bdev = &g_bdevs[i];
     226                 :         20 :                         *_desc = desc;
     227                 :         20 :                         return 0;
     228                 :            :                 }
     229                 :          4 :         }
     230                 :            : 
     231                 :          0 :         return -EINVAL;
     232                 :          5 : }
     233                 :            : 
     234                 :            : void
     235                 :         24 : spdk_bdev_close(struct spdk_bdev_desc *desc)
     236                 :            : {
     237                 :         24 :         free(desc);
     238                 :         24 : }
     239                 :            : 
     240                 :            : struct spdk_bdev *
     241                 :         20 : spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
     242                 :            : {
     243                 :         20 :         return desc->bdev;
     244                 :            : }
     245                 :            : 
     246                 :            : const char *
     247                 :          0 : spdk_bdev_get_name(const struct spdk_bdev *bdev)
     248                 :            : {
     249                 :          0 :         return "test";
     250                 :            : }
     251                 :            : 
     252                 :            : const struct spdk_uuid *
     253                 :         84 : spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
     254                 :            : {
     255                 :         84 :         return &bdev->uuid;
     256                 :            : }
     257                 :            : 
     258                 :         28 : union spdk_bdev_nvme_ctratt spdk_bdev_get_nvme_ctratt(struct spdk_bdev *bdev)
     259                 :            : {
     260                 :         28 :         return bdev->ctratt;
     261                 :            : }
     262                 :            : 
     263                 :            : static void
     264                 :          4 : test_spdk_nvmf_subsystem_add_ns(void)
     265                 :            : {
     266                 :          4 :         struct spdk_nvmf_tgt tgt = {};
     267                 :          4 :         struct spdk_nvmf_subsystem subsystem = {
     268                 :            :                 .max_nsid = 1024,
     269                 :            :                 .ns = NULL,
     270                 :            :                 .tgt = &tgt,
     271                 :            :         };
     272                 :          3 :         struct spdk_nvmf_ns_opts ns_opts;
     273                 :            :         uint32_t nsid;
     274                 :            :         int rc;
     275                 :            : 
     276                 :          4 :         subsystem.ns = calloc(subsystem.max_nsid, sizeof(struct spdk_nvmf_subsystem_ns *));
     277         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
     278                 :          4 :         subsystem.ana_group = calloc(subsystem.max_nsid, sizeof(uint32_t));
     279         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ana_group != NULL);
     280                 :            : 
     281                 :          4 :         tgt.max_subsystems = 1024;
     282                 :          4 :         RB_INIT(&tgt.subsystems);
     283                 :            : 
     284                 :            :         /* Request a specific NSID */
     285                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
     286                 :          4 :         ns_opts.nsid = 5;
     287                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev2", &ns_opts, sizeof(ns_opts), NULL);
     288                 :          4 :         CU_ASSERT(nsid == 5);
     289                 :          4 :         CU_ASSERT(subsystem.max_nsid == 1024);
     290         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns[nsid - 1] != NULL);
     291                 :          4 :         CU_ASSERT(subsystem.ns[nsid - 1]->bdev == &g_bdevs[1]);
     292                 :            : 
     293                 :            :         /* Request an NSID that is already in use */
     294                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
     295                 :          4 :         ns_opts.nsid = 5;
     296                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev2", &ns_opts, sizeof(ns_opts), NULL);
     297                 :          4 :         CU_ASSERT(nsid == 0);
     298                 :          4 :         CU_ASSERT(subsystem.max_nsid == 1024);
     299                 :            : 
     300                 :            :         /* Request 0xFFFFFFFF (invalid NSID, reserved for broadcast) */
     301                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
     302                 :          4 :         ns_opts.nsid = 0xFFFFFFFF;
     303                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev2", &ns_opts, sizeof(ns_opts), NULL);
     304                 :          4 :         CU_ASSERT(nsid == 0);
     305                 :          4 :         CU_ASSERT(subsystem.max_nsid == 1024);
     306                 :            : 
     307                 :          4 :         rc = spdk_nvmf_subsystem_remove_ns(&subsystem, 5);
     308                 :          4 :         CU_ASSERT(rc == 0);
     309                 :            : 
     310                 :          4 :         free(subsystem.ns);
     311                 :          4 :         free(subsystem.ana_group);
     312                 :          4 : }
     313                 :            : 
     314                 :            : static void
     315                 :          4 : test_spdk_nvmf_subsystem_add_fdp_ns(void)
     316                 :            : {
     317                 :          4 :         struct spdk_nvmf_tgt tgt = {};
     318                 :          4 :         struct spdk_nvmf_subsystem subsystem = {
     319                 :            :                 .max_nsid = 1024,
     320                 :            :                 .ns = NULL,
     321                 :            :                 .tgt = &tgt,
     322                 :            :         };
     323                 :          3 :         struct spdk_nvmf_ns_opts ns_opts;
     324                 :            :         uint32_t nsid;
     325                 :            :         int rc;
     326                 :            : 
     327                 :          4 :         subsystem.ns = calloc(subsystem.max_nsid, sizeof(struct spdk_nvmf_subsystem_ns *));
     328         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
     329                 :          4 :         subsystem.ana_group = calloc(subsystem.max_nsid, sizeof(uint32_t));
     330         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ana_group != NULL);
     331                 :            : 
     332                 :          4 :         tgt.max_subsystems = 1024;
     333                 :          4 :         RB_INIT(&tgt.subsystems);
     334                 :            : 
     335         [ -  + ]:          4 :         CU_ASSERT(subsystem.fdp_supported == false);
     336                 :            : 
     337                 :            :         /* Add a FDP supported namespace to the subsystem */
     338                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
     339                 :          4 :         ns_opts.nsid = 3;
     340                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev3", &ns_opts, sizeof(ns_opts), NULL);
     341                 :          4 :         CU_ASSERT(nsid == 3);
     342                 :          4 :         CU_ASSERT(subsystem.max_nsid == 1024);
     343         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns[nsid - 1] != NULL);
     344                 :          4 :         CU_ASSERT(subsystem.ns[nsid - 1]->bdev == &g_bdevs[2]);
     345         [ -  + ]:          4 :         CU_ASSERT(subsystem.fdp_supported == true);
     346                 :            : 
     347                 :            :         /* Try to add a non FDP supported namespace to the subsystem */
     348                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
     349                 :          4 :         ns_opts.nsid = 5;
     350                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev2", &ns_opts, sizeof(ns_opts), NULL);
     351                 :          4 :         CU_ASSERT(nsid == 0);
     352                 :          4 :         CU_ASSERT(subsystem.max_nsid == 1024);
     353         [ -  + ]:          4 :         CU_ASSERT(subsystem.fdp_supported == true);
     354                 :            : 
     355                 :            :         /* Remove last FDP namespace from the subsystem */
     356                 :          4 :         rc = spdk_nvmf_subsystem_remove_ns(&subsystem, 3);
     357                 :          4 :         CU_ASSERT(rc == 0);
     358         [ -  + ]:          4 :         CU_ASSERT(subsystem.fdp_supported == false);
     359                 :            : 
     360                 :          4 :         free(subsystem.ns);
     361                 :          4 :         free(subsystem.ana_group);
     362                 :          4 : }
     363                 :            : 
     364                 :            : static void
     365                 :          4 : nvmf_test_create_subsystem(void)
     366                 :            : {
     367                 :          4 :         struct spdk_nvmf_tgt tgt = {};
     368                 :          3 :         char nqn[256];
     369                 :            :         struct spdk_nvmf_subsystem *subsystem;
     370                 :            :         int rc;
     371                 :            : 
     372                 :          4 :         tgt.max_subsystems = 1024;
     373                 :          4 :         tgt.subsystem_ids = spdk_bit_array_create(tgt.max_subsystems);
     374                 :          4 :         RB_INIT(&tgt.subsystems);
     375                 :            : 
     376                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:subsystem1");
     377                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     378         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     379         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     380                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     381                 :          4 :         CU_ASSERT(rc == 0);
     382                 :            : 
     383                 :            :         /* valid name with complex reverse domain */
     384                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk-full--rev-domain.name:subsystem1");
     385                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     386         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     387         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     388                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     389                 :          4 :         CU_ASSERT(rc == 0);
     390                 :            : 
     391                 :            :         /* Valid name discovery controller */
     392                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:subsystem1");
     393                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     394         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     395         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     396                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     397                 :          4 :         CU_ASSERT(rc == 0);
     398                 :            : 
     399                 :            :         /* Invalid name, no user supplied string */
     400                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:");
     401                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     402         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     403                 :            : 
     404                 :            :         /* Valid name, only contains top-level domain name */
     405                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:subsystem1");
     406                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     407         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     408         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     409                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     410                 :          4 :         CU_ASSERT(rc == 0);
     411                 :            : 
     412                 :            :         /* Invalid name, domain label > 63 characters */
     413                 :          4 :         snprintf(nqn, sizeof(nqn),
     414                 :            :                  "nqn.2016-06.io.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz:sub");
     415                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     416         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     417                 :            : 
     418                 :            :         /* Invalid name, domain label starts with digit */
     419                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.3spdk:sub");
     420                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     421         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     422                 :            : 
     423                 :            :         /* Invalid name, domain label starts with - */
     424                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.-spdk:subsystem1");
     425                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     426         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     427                 :            : 
     428                 :            :         /* Invalid name, domain label ends with - */
     429                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk-:subsystem1");
     430                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     431         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     432                 :            : 
     433                 :            :         /* Invalid name, domain label with multiple consecutive periods */
     434                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io..spdk:subsystem1");
     435                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     436         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     437                 :            : 
     438                 :            :         /* Longest valid name */
     439                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:");
     440         [ -  + ]:          4 :         memset(nqn + strlen(nqn), 'a', 223 - strlen(nqn));
     441                 :          4 :         nqn[223] = '\0';
     442                 :          4 :         CU_ASSERT(strlen(nqn) == 223);
     443                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     444         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     445         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     446                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     447                 :          4 :         CU_ASSERT(rc == 0);
     448                 :            : 
     449                 :            :         /* Invalid name, too long */
     450                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:");
     451         [ -  + ]:          4 :         memset(nqn + strlen(nqn), 'a', 224 - strlen(nqn));
     452                 :          4 :         nqn[224] = '\0';
     453                 :          4 :         CU_ASSERT(strlen(nqn) == 224);
     454                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     455                 :          4 :         CU_ASSERT(subsystem == NULL);
     456                 :            : 
     457                 :            :         /* Valid name using uuid format */
     458                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2014-08.org.nvmexpress:uuid:ff9b6406-0fc8-4779-80ca-4dca14bda0d2");
     459                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     460         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     461         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     462                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     463                 :          4 :         CU_ASSERT(rc == 0);
     464                 :            : 
     465                 :            :         /* Invalid name user string contains an invalid utf-8 character */
     466                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:\xFFsubsystem1");
     467                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     468         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     469                 :            : 
     470                 :            :         /* Valid name with non-ascii but valid utf-8 characters */
     471                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2016-06.io.spdk:\xe1\x8a\x88subsystem1\xca\x80");
     472                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     473         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
     474         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, nqn);
     475                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
     476                 :          4 :         CU_ASSERT(rc == 0);
     477                 :            : 
     478                 :            :         /* Invalid uuid (too long) */
     479                 :          4 :         snprintf(nqn, sizeof(nqn),
     480                 :            :                  "nqn.2014-08.org.nvmexpress:uuid:ff9b6406-0fc8-4779-80ca-4dca14bda0d2aaaa");
     481                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     482         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     483                 :            : 
     484                 :            :         /* Invalid uuid (dashes placed incorrectly) */
     485                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2014-08.org.nvmexpress:uuid:ff9b64-060fc8-4779-80ca-4dca14bda0d2");
     486                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     487         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     488                 :            : 
     489                 :            :         /* Invalid uuid (invalid characters in uuid) */
     490                 :          4 :         snprintf(nqn, sizeof(nqn), "nqn.2014-08.org.nvmexpress:uuid:ff9hg406-0fc8-4779-80ca-4dca14bda0d2");
     491                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
     492         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem == NULL);
     493                 :            : 
     494                 :          4 :         spdk_bit_array_free(&tgt.subsystem_ids);
     495                 :          4 : }
     496                 :            : 
     497                 :            : static void
     498                 :          4 : test_spdk_nvmf_subsystem_set_sn(void)
     499                 :            : {
     500                 :          4 :         struct spdk_nvmf_subsystem subsystem = {};
     501                 :            : 
     502                 :            :         /* Basic valid serial number */
     503                 :          4 :         CU_ASSERT(spdk_nvmf_subsystem_set_sn(&subsystem, "abcd xyz") == 0);
     504                 :          4 :         CU_ASSERT(strcmp(subsystem.sn, "abcd xyz") == 0);
     505                 :            : 
     506                 :            :         /* Exactly 20 characters (valid) */
     507                 :          4 :         CU_ASSERT(spdk_nvmf_subsystem_set_sn(&subsystem, "12345678901234567890") == 0);
     508                 :          4 :         CU_ASSERT(strcmp(subsystem.sn, "12345678901234567890") == 0);
     509                 :            : 
     510                 :            :         /* 21 characters (too long, invalid) */
     511                 :          4 :         CU_ASSERT(spdk_nvmf_subsystem_set_sn(&subsystem, "123456789012345678901") < 0);
     512                 :            : 
     513                 :            :         /* Non-ASCII characters (invalid) */
     514                 :          4 :         CU_ASSERT(spdk_nvmf_subsystem_set_sn(&subsystem, "abcd\txyz") < 0);
     515                 :          4 : }
     516                 :            : 
     517                 :            : static void
     518                 :          4 : test_spdk_nvmf_ns_visible(void)
     519                 :            : {
     520                 :          4 :         struct spdk_nvmf_subsystem subsystem = {};
     521                 :          4 :         struct spdk_nvmf_ns ns1 = {
     522                 :            :                 .nsid = 1,
     523                 :            :                 .anagrpid = 1,
     524                 :            :                 .always_visible = false
     525                 :            :         };
     526                 :          4 :         struct spdk_nvmf_ns ns2 = {
     527                 :            :                 .nsid = 2,
     528                 :            :                 .anagrpid = 2,
     529                 :            :                 .always_visible = false
     530                 :            :         };
     531                 :            :         struct spdk_nvmf_ns *ns3;
     532                 :          4 :         struct spdk_nvmf_ctrlr ctrlrA = {
     533                 :            :                 .subsys = &subsystem
     534                 :            :         };
     535                 :          4 :         struct spdk_nvmf_ctrlr ctrlrB = {
     536                 :            :                 .subsys = &subsystem
     537                 :            :         };
     538                 :            :         struct spdk_thread *thread;
     539                 :          4 :         struct spdk_nvmf_tgt tgt = {};
     540                 :            :         uint32_t nsid;
     541                 :            :         int rc;
     542                 :            : 
     543                 :          4 :         thread = spdk_get_thread();
     544         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(thread != NULL);
     545                 :          4 :         ctrlrA.thread = thread;
     546                 :          4 :         ctrlrB.thread = thread;
     547                 :            : 
     548                 :          4 :         subsystem.max_nsid = 1024;
     549                 :          4 :         subsystem.ns = calloc(subsystem.max_nsid, sizeof(subsystem.ns));
     550         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
     551                 :          4 :         subsystem.ana_group = calloc(subsystem.max_nsid, sizeof(uint32_t));
     552         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ana_group != NULL);
     553                 :          4 :         TAILQ_INIT(&tgt.transports);
     554                 :          4 :         subsystem.tgt = &tgt;
     555                 :            : 
     556                 :          4 :         subsystem.ns[1] = &ns1;
     557                 :          4 :         subsystem.ns[2] = &ns2;
     558                 :          4 :         ns3 = calloc(1, sizeof(*ns3));
     559         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(ns3 != NULL);
     560                 :          4 :         ns3->nsid = 3;
     561                 :          4 :         ns3->anagrpid = 3;
     562                 :          4 :         subsystem.ana_group[ns3->anagrpid - 1] = 1;
     563                 :          4 :         subsystem.ns[3] = ns3;
     564                 :            : 
     565         [ -  + ]:          4 :         snprintf(ctrlrA.hostnqn, sizeof(ctrlrA.hostnqn), "nqn.2016-06.io.spdk:host1");
     566                 :          4 :         ctrlrA.visible_ns = spdk_bit_array_create(subsystem.max_nsid);
     567         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(ctrlrA.visible_ns != NULL);
     568         [ -  + ]:          4 :         snprintf(ctrlrB.hostnqn, sizeof(ctrlrB.hostnqn), "nqn.2016-06.io.spdk:host2");
     569                 :          4 :         ctrlrB.visible_ns = spdk_bit_array_create(subsystem.max_nsid);
     570         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(ctrlrB.visible_ns != NULL);
     571                 :            : 
     572                 :            :         /* Add two controllers ctrlrA and ctrlrB */
     573                 :          4 :         TAILQ_INIT(&subsystem.ctrlrs);
     574                 :          4 :         TAILQ_INSERT_TAIL(&subsystem.ctrlrs, &ctrlrA, link);
     575                 :          4 :         TAILQ_INSERT_TAIL(&subsystem.ctrlrs, &ctrlrB, link);
     576                 :            : 
     577                 :            :         /* Invalid host nqn */
     578                 :          4 :         nsid = 1;
     579                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, "", 0);
     580                 :          4 :         CU_ASSERT(rc == -EINVAL);
     581                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, NULL, 0);
     582                 :          4 :         CU_ASSERT(rc == -EINVAL);
     583                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, NULL, 0);
     584                 :          4 :         CU_ASSERT(rc == -EINVAL);
     585                 :            : 
     586                 :            :         /* Invalid nsid */
     587                 :          4 :         nsid = 0;
     588                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     589                 :          4 :         CU_ASSERT(rc == -EINVAL);
     590                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     591                 :          4 :         CU_ASSERT(rc == -EINVAL);
     592                 :            : 
     593                 :            :         /* Unallocated ns */
     594                 :          4 :         nsid = 1;
     595                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     596                 :          4 :         CU_ASSERT(rc == -ENOENT);
     597                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     598                 :          4 :         CU_ASSERT(rc == -ENOENT);
     599                 :            : 
     600                 :            :         /* Attach any is active => do not allow individual host control */
     601                 :          4 :         ns1.always_visible = true;
     602                 :          4 :         nsid = 2;
     603                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     604                 :          4 :         CU_ASSERT(rc == -EPERM);
     605                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     606                 :          4 :         CU_ASSERT(rc == -EPERM);
     607                 :          4 :         ns1.always_visible = false;
     608                 :            : 
     609                 :            :         /* Attach ctrlrA to namespace 2 hot + cold */
     610                 :          4 :         nsid = 2;
     611                 :          4 :         g_async_event_ctrlr = NULL;
     612                 :          4 :         g_ns_changed_ctrlr = NULL;
     613                 :          4 :         g_ns_changed_nsid = 0;
     614                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     615                 :          4 :         CU_ASSERT(rc == 0);
     616                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrA.hostnqn) != NULL);
     617                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrB.hostnqn) == NULL);
     618                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrA.hostnqn) == NULL);
     619                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrB.hostnqn) == NULL);
     620                 :          4 :         CU_ASSERT(nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     621                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     622                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid + 1));
     623                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid + 1));
     624                 :            :         /* check last ns_changed */
     625                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == &ctrlrA);
     626                 :          4 :         CU_ASSERT(g_ns_changed_nsid == nsid);
     627                 :            :         /* check async_event */
     628                 :          4 :         poll_threads();
     629                 :          4 :         CU_ASSERT(g_async_event_ctrlr == &ctrlrA);
     630                 :            : 
     631                 :            :         /* Attach ctrlrA to namespace 2 again => should not create any ns change/async event */
     632                 :          4 :         g_async_event_ctrlr = NULL;
     633                 :          4 :         g_ns_changed_ctrlr = NULL;
     634                 :          4 :         g_ns_changed_nsid = 0;
     635                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     636                 :          4 :         CU_ASSERT(rc == 0);
     637                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrA.hostnqn) != NULL);
     638                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrB.hostnqn) == NULL);
     639                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrA.hostnqn) == NULL);
     640                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrB.hostnqn) == NULL);
     641                 :          4 :         CU_ASSERT(nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     642                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     643                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid + 1));
     644                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid + 1));
     645                 :            :         /* check last ns_changed */
     646                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == NULL);
     647                 :          4 :         CU_ASSERT(g_ns_changed_nsid == 0);
     648                 :            :         /* check async_event */
     649                 :          4 :         poll_threads();
     650                 :          4 :         CU_ASSERT(g_async_event_ctrlr == NULL);
     651                 :            : 
     652                 :            :         /* Detach ctrlrA from namespace 2 hot + cold */
     653                 :          4 :         g_async_event_ctrlr = NULL;
     654                 :          4 :         g_ns_changed_ctrlr = NULL;
     655                 :          4 :         g_ns_changed_nsid = 0;
     656                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     657                 :          4 :         CU_ASSERT(rc == 0);
     658                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrA.hostnqn) == NULL);
     659                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrB.hostnqn) == NULL);
     660                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrA.hostnqn) == NULL);
     661                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrB.hostnqn) == NULL);
     662                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     663                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     664                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid + 1));
     665                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid + 1));
     666                 :            :         /* check last ns_changed */
     667                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == &ctrlrA);
     668                 :          4 :         CU_ASSERT(g_ns_changed_nsid == nsid);
     669                 :            :         /* check async_event */
     670                 :          4 :         poll_threads();
     671                 :          4 :         CU_ASSERT(g_async_event_ctrlr == &ctrlrA);
     672                 :            : 
     673                 :            :         /* Detach ctrlrA from namespace 2 again hot + cold */
     674                 :          4 :         g_async_event_ctrlr = NULL;
     675                 :          4 :         g_ns_changed_ctrlr = NULL;
     676                 :          4 :         g_ns_changed_nsid = 0;
     677                 :          4 :         rc = spdk_nvmf_ns_remove_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     678                 :          4 :         CU_ASSERT(rc == 0);
     679                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrA.hostnqn) == NULL);
     680                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns1, ctrlrB.hostnqn) == NULL);
     681                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrA.hostnqn) == NULL);
     682                 :          4 :         CU_ASSERT(nvmf_ns_find_host(&ns2, ctrlrB.hostnqn) == NULL);
     683                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     684                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     685                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid + 1));
     686                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid + 1));
     687                 :            :         /* check last ns_changed */
     688                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == NULL);
     689                 :          4 :         CU_ASSERT(g_ns_changed_nsid == 0);
     690                 :            :         /* check async_event */
     691                 :          4 :         poll_threads();
     692                 :          4 :         CU_ASSERT(g_async_event_ctrlr == NULL);
     693                 :            : 
     694                 :            :         /* Attach ctrlrA to namespace 4 hot + cold => remove ns */
     695                 :          4 :         nsid = 4;
     696                 :          4 :         g_async_event_ctrlr = NULL;
     697                 :          4 :         g_ns_changed_ctrlr = NULL;
     698                 :          4 :         g_ns_changed_nsid = 0;
     699                 :          4 :         rc = spdk_nvmf_ns_add_host(&subsystem, nsid, ctrlrA.hostnqn, 0);
     700                 :          4 :         CU_ASSERT(rc == 0);
     701                 :          4 :         CU_ASSERT(nvmf_ns_find_host(ns3, ctrlrA.hostnqn) != NULL);
     702                 :          4 :         CU_ASSERT(nvmf_ns_find_host(ns3, ctrlrB.hostnqn) == NULL);
     703                 :          4 :         CU_ASSERT(nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     704                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     705                 :            :         /* check last ns_changed */
     706                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == &ctrlrA);
     707                 :          4 :         CU_ASSERT(g_ns_changed_nsid == nsid);
     708                 :            :         /* check async_event */
     709                 :          4 :         poll_threads();
     710                 :          4 :         CU_ASSERT(g_async_event_ctrlr == &ctrlrA);
     711                 :            : 
     712                 :          4 :         g_async_event_ctrlr = NULL;
     713                 :          4 :         g_ns_changed_ctrlr = NULL;
     714                 :          4 :         g_ns_changed_nsid = 0;
     715                 :          4 :         rc = spdk_nvmf_subsystem_remove_ns(&subsystem, nsid);
     716                 :          4 :         CU_ASSERT(rc == 0);
     717                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrA, nsid));
     718                 :          4 :         CU_ASSERT(!nvmf_ctrlr_ns_is_visible(&ctrlrB, nsid));
     719                 :            :         /* check last ns_changed */
     720                 :          4 :         CU_ASSERT(g_ns_changed_ctrlr == &ctrlrA);
     721                 :          4 :         CU_ASSERT(g_ns_changed_nsid == nsid);
     722                 :            : 
     723                 :          4 :         free(ctrlrA.visible_ns);
     724                 :          4 :         free(ctrlrB.visible_ns);
     725                 :          4 :         free(subsystem.ana_group);
     726                 :          4 :         free(subsystem.ns);
     727                 :          4 : }
     728                 :            : 
     729                 :            : /*
     730                 :            :  * Reservation Unit Test Configuration
     731                 :            :  *       --------             --------    --------
     732                 :            :  *      | Host A |           | Host B |  | Host C |
     733                 :            :  *       --------             --------    --------
     734                 :            :  *      /        \               |           |
     735                 :            :  *  --------   --------       -------     -------
     736                 :            :  * |Ctrlr1_A| |Ctrlr2_A|     |Ctrlr_B|   |Ctrlr_C|
     737                 :            :  *  --------   --------       -------     -------
     738                 :            :  *    \           \              /           /
     739                 :            :  *     \           \            /           /
     740                 :            :  *      \           \          /           /
     741                 :            :  *      --------------------------------------
     742                 :            :  *     |            NAMESPACE 1               |
     743                 :            :  *      --------------------------------------
     744                 :            :  */
     745                 :            : static struct spdk_nvmf_subsystem g_subsystem;
     746                 :            : static struct spdk_nvmf_ctrlr g_ctrlr1_A, g_ctrlr2_A, g_ctrlr_B, g_ctrlr_C;
     747                 :            : static struct spdk_nvmf_ns g_ns;
     748                 :            : struct spdk_nvmf_subsystem_pg_ns_info g_ns_info;
     749                 :            : 
     750                 :            : void
     751                 :          0 : nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr)
     752                 :            : {
     753                 :          0 : }
     754                 :            : 
     755                 :            : static void
     756                 :         44 : ut_reservation_init(void)
     757                 :            : {
     758                 :            : 
     759                 :         44 :         TAILQ_INIT(&g_subsystem.ctrlrs);
     760                 :            : 
     761         [ -  + ]:         44 :         memset(&g_ns, 0, sizeof(g_ns));
     762                 :         44 :         TAILQ_INIT(&g_ns.registrants);
     763                 :         44 :         g_ns.subsystem = &g_subsystem;
     764                 :         44 :         g_ns.ptpl_file = NULL;
     765                 :         44 :         g_ns.ptpl_activated = false;
     766                 :         44 :         spdk_uuid_generate(&g_bdevs[0].uuid);
     767                 :         44 :         g_ns.bdev = &g_bdevs[0];
     768                 :            : 
     769                 :            :         /* Host A has two controllers */
     770                 :         44 :         spdk_uuid_generate(&g_ctrlr1_A.hostid);
     771                 :         44 :         TAILQ_INIT(&g_ctrlr1_A.log_head);
     772                 :         44 :         g_ctrlr1_A.subsys = &g_subsystem;
     773                 :         44 :         g_ctrlr1_A.num_avail_log_pages = 0;
     774                 :         44 :         TAILQ_INSERT_TAIL(&g_subsystem.ctrlrs, &g_ctrlr1_A, link);
     775                 :         44 :         spdk_uuid_copy(&g_ctrlr2_A.hostid, &g_ctrlr1_A.hostid);
     776                 :         44 :         TAILQ_INIT(&g_ctrlr2_A.log_head);
     777                 :         44 :         g_ctrlr2_A.subsys = &g_subsystem;
     778                 :         44 :         g_ctrlr2_A.num_avail_log_pages = 0;
     779                 :         44 :         TAILQ_INSERT_TAIL(&g_subsystem.ctrlrs, &g_ctrlr2_A, link);
     780                 :            : 
     781                 :            :         /* Host B has 1 controller */
     782                 :         44 :         spdk_uuid_generate(&g_ctrlr_B.hostid);
     783                 :         44 :         TAILQ_INIT(&g_ctrlr_B.log_head);
     784                 :         44 :         g_ctrlr_B.subsys = &g_subsystem;
     785                 :         44 :         g_ctrlr_B.num_avail_log_pages = 0;
     786                 :         44 :         TAILQ_INSERT_TAIL(&g_subsystem.ctrlrs, &g_ctrlr_B, link);
     787                 :            : 
     788                 :            :         /* Host C has 1 controller */
     789                 :         44 :         spdk_uuid_generate(&g_ctrlr_C.hostid);
     790                 :         44 :         TAILQ_INIT(&g_ctrlr_C.log_head);
     791                 :         44 :         g_ctrlr_C.subsys = &g_subsystem;
     792                 :         44 :         g_ctrlr_C.num_avail_log_pages = 0;
     793                 :         44 :         TAILQ_INSERT_TAIL(&g_subsystem.ctrlrs, &g_ctrlr_C, link);
     794                 :         44 : }
     795                 :            : 
     796                 :            : static void
     797                 :         44 : ut_reservation_deinit(void)
     798                 :            : {
     799                 :            :         struct spdk_nvmf_registrant *reg, *tmp;
     800                 :            :         struct spdk_nvmf_reservation_log *log, *log_tmp;
     801                 :            :         struct spdk_nvmf_ctrlr *ctrlr, *ctrlr_tmp;
     802                 :            : 
     803   [ +  +  +  + ]:        104 :         TAILQ_FOREACH_SAFE(reg, &g_ns.registrants, link, tmp) {
     804         [ +  + ]:         60 :                 TAILQ_REMOVE(&g_ns.registrants, reg, link);
     805                 :         60 :                 free(reg);
     806                 :         15 :         }
     807   [ +  +  -  + ]:         44 :         TAILQ_FOREACH_SAFE(log, &g_ctrlr1_A.log_head, link, log_tmp) {
     808         [ #  # ]:          0 :                 TAILQ_REMOVE(&g_ctrlr1_A.log_head, log, link);
     809                 :          0 :                 free(log);
     810                 :          0 :         }
     811                 :         44 :         g_ctrlr1_A.num_avail_log_pages = 0;
     812   [ +  +  -  + ]:         44 :         TAILQ_FOREACH_SAFE(log, &g_ctrlr2_A.log_head, link, log_tmp) {
     813         [ #  # ]:          0 :                 TAILQ_REMOVE(&g_ctrlr2_A.log_head, log, link);
     814                 :          0 :                 free(log);
     815                 :          0 :         }
     816                 :         44 :         g_ctrlr2_A.num_avail_log_pages = 0;
     817   [ +  +  -  + ]:         44 :         TAILQ_FOREACH_SAFE(log, &g_ctrlr_B.log_head, link, log_tmp) {
     818         [ #  # ]:          0 :                 TAILQ_REMOVE(&g_ctrlr_B.log_head, log, link);
     819                 :          0 :                 free(log);
     820                 :          0 :         }
     821                 :         44 :         g_ctrlr_B.num_avail_log_pages = 0;
     822   [ +  +  -  + ]:         44 :         TAILQ_FOREACH_SAFE(log, &g_ctrlr_C.log_head, link, log_tmp) {
     823         [ #  # ]:          0 :                 TAILQ_REMOVE(&g_ctrlr_C.log_head, log, link);
     824                 :          0 :                 free(log);
     825                 :          0 :         }
     826                 :         44 :         g_ctrlr_C.num_avail_log_pages = 0;
     827                 :            : 
     828   [ +  +  +  + ]:        220 :         TAILQ_FOREACH_SAFE(ctrlr, &g_subsystem.ctrlrs, link, ctrlr_tmp) {
     829         [ +  + ]:        176 :                 TAILQ_REMOVE(&g_subsystem.ctrlrs, ctrlr, link);
     830                 :         44 :         }
     831                 :         44 : }
     832                 :            : 
     833                 :            : static struct spdk_nvmf_request *
     834                 :         76 : ut_reservation_build_req(uint32_t length)
     835                 :            : {
     836                 :            :         struct spdk_nvmf_request *req;
     837                 :            : 
     838                 :         76 :         req = calloc(1, sizeof(*req));
     839         [ +  + ]:         76 :         assert(req != NULL);
     840                 :            : 
     841                 :         76 :         SPDK_IOV_ONE(req->iov, &req->iovcnt, calloc(1, length), length);
     842         [ +  + ]:         76 :         assert(req->iov[0].iov_base != NULL);
     843                 :         76 :         req->length = length;
     844                 :            : 
     845                 :         76 :         req->cmd = (union nvmf_h2c_msg *)calloc(1, sizeof(union nvmf_h2c_msg));
     846         [ +  + ]:         76 :         assert(req->cmd != NULL);
     847                 :            : 
     848                 :         76 :         req->rsp = (union nvmf_c2h_msg *)calloc(1, sizeof(union nvmf_c2h_msg));
     849         [ +  + ]:         76 :         assert(req->rsp != NULL);
     850                 :            : 
     851                 :         76 :         return req;
     852                 :            : }
     853                 :            : 
     854                 :            : static void
     855                 :         76 : ut_reservation_free_req(struct spdk_nvmf_request *req)
     856                 :            : {
     857                 :         76 :         free(req->cmd);
     858                 :         76 :         free(req->rsp);
     859                 :         76 :         free(req->iov[0].iov_base);
     860                 :         76 :         free(req);
     861                 :         76 : }
     862                 :            : 
     863                 :            : static void
     864                 :        180 : ut_reservation_build_register_request(struct spdk_nvmf_request *req,
     865                 :            :                                       uint8_t rrega, uint8_t iekey,
     866                 :            :                                       uint8_t cptpl, uint64_t crkey,
     867                 :            :                                       uint64_t nrkey)
     868                 :            : {
     869                 :        135 :         struct spdk_nvme_reservation_register_data key;
     870                 :        180 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     871                 :            : 
     872                 :        180 :         key.crkey = crkey;
     873                 :        180 :         key.nrkey = nrkey;
     874                 :        180 :         cmd->cdw10 = 0;
     875                 :        180 :         cmd->cdw10_bits.resv_register.rrega = rrega;
     876                 :        180 :         cmd->cdw10_bits.resv_register.iekey = iekey;
     877                 :        180 :         cmd->cdw10_bits.resv_register.cptpl = cptpl;
     878                 :        180 :         memcpy(req->iov[0].iov_base, &key, sizeof(key));
     879                 :        180 : }
     880                 :            : 
     881                 :            : static void
     882                 :         52 : ut_reservation_build_acquire_request(struct spdk_nvmf_request *req,
     883                 :            :                                      uint8_t racqa, uint8_t iekey,
     884                 :            :                                      uint8_t rtype, uint64_t crkey,
     885                 :            :                                      uint64_t prkey)
     886                 :            : {
     887                 :         39 :         struct spdk_nvme_reservation_acquire_data key;
     888                 :         52 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     889                 :            : 
     890                 :         52 :         key.crkey = crkey;
     891                 :         52 :         key.prkey = prkey;
     892                 :         52 :         cmd->cdw10 = 0;
     893                 :         52 :         cmd->cdw10_bits.resv_acquire.racqa = racqa;
     894                 :         52 :         cmd->cdw10_bits.resv_acquire.iekey = iekey;
     895                 :         52 :         cmd->cdw10_bits.resv_acquire.rtype = rtype;
     896                 :         52 :         memcpy(req->iov[0].iov_base, &key, sizeof(key));
     897                 :         52 : }
     898                 :            : 
     899                 :            : static void
     900                 :         24 : ut_reservation_build_release_request(struct spdk_nvmf_request *req,
     901                 :            :                                      uint8_t rrela, uint8_t iekey,
     902                 :            :                                      uint8_t rtype, uint64_t crkey)
     903                 :            : {
     904                 :         24 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     905                 :            : 
     906                 :         24 :         cmd->cdw10 = 0;
     907                 :         24 :         cmd->cdw10_bits.resv_release.rrela = rrela;
     908                 :         24 :         cmd->cdw10_bits.resv_release.iekey = iekey;
     909                 :         24 :         cmd->cdw10_bits.resv_release.rtype = rtype;
     910                 :         24 :         memcpy(req->iov[0].iov_base, &crkey, sizeof(crkey));
     911                 :         24 : }
     912                 :            : 
     913                 :            : /*
     914                 :            :  * Construct four registrants for other test cases.
     915                 :            :  *
     916                 :            :  * g_ctrlr1_A register with key 0xa1.
     917                 :            :  * g_ctrlr2_A register with key 0xa1.
     918                 :            :  * g_ctrlr_B register with key 0xb1.
     919                 :            :  * g_ctrlr_C register with key 0xc1.
     920                 :            :  * */
     921                 :            : static void
     922                 :         32 : ut_reservation_build_registrants(void)
     923                 :            : {
     924                 :            :         struct spdk_nvmf_request *req;
     925                 :            :         struct spdk_nvme_cpl *rsp;
     926                 :            :         struct spdk_nvmf_registrant *reg;
     927                 :            :         uint32_t gen;
     928                 :            : 
     929                 :         32 :         req = ut_reservation_build_req(16);
     930                 :         32 :         rsp = &req->rsp->nvme_cpl;
     931         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(req != NULL);
     932                 :         32 :         gen = g_ns.gen;
     933                 :            : 
     934                 :            :         /* TEST CASE: g_ctrlr1_A register with a new key */
     935                 :         32 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY,
     936                 :            :                                               0, 0, 0, 0xa1);
     937                 :         32 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
     938         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
     939                 :         32 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
     940         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(reg->rkey == 0xa1);
     941         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(g_ns.gen == gen + 1);
     942                 :            : 
     943                 :            :         /* TEST CASE: g_ctrlr2_A register with a new key, because it has same
     944                 :            :          * Host Identifier with g_ctrlr1_A, so the register key should same.
     945                 :            :          */
     946                 :         32 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY,
     947                 :            :                                               0, 0, 0, 0xa2);
     948                 :         32 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr2_A, req);
     949                 :            :         /* Reservation conflict for other key than 0xa1 */
     950         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_RESERVATION_CONFLICT);
     951                 :            : 
     952                 :            :         /* g_ctrlr_B register with a new key */
     953                 :         32 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY,
     954                 :            :                                               0, 0, 0, 0xb1);
     955                 :         32 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
     956         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
     957                 :         32 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
     958         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(reg->rkey == 0xb1);
     959         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(g_ns.gen == gen + 2);
     960                 :            : 
     961                 :            :         /* g_ctrlr_C register with a new key */
     962                 :         32 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY,
     963                 :            :                                               0, 0, 0, 0xc1);
     964                 :         32 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_C, req);
     965         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
     966                 :         32 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_C.hostid);
     967         [ +  + ]:         32 :         SPDK_CU_ASSERT_FATAL(reg->rkey == 0xc1);
     968         [ -  + ]:         32 :         SPDK_CU_ASSERT_FATAL(g_ns.gen == gen + 3);
     969                 :            : 
     970                 :         32 :         ut_reservation_free_req(req);
     971                 :         32 : }
     972                 :            : 
     973                 :            : static void
     974                 :          4 : test_reservation_register(void)
     975                 :            : {
     976                 :            :         struct spdk_nvmf_request *req;
     977                 :            :         struct spdk_nvme_cpl *rsp;
     978                 :            :         struct spdk_nvmf_registrant *reg;
     979                 :            :         uint32_t gen;
     980                 :            : 
     981                 :          4 :         ut_reservation_init();
     982                 :            : 
     983                 :          4 :         req = ut_reservation_build_req(16);
     984                 :          4 :         rsp = &req->rsp->nvme_cpl;
     985         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
     986                 :            : 
     987                 :          4 :         ut_reservation_build_registrants();
     988                 :            : 
     989                 :            :         /* TEST CASE: Replace g_ctrlr1_A with a new key */
     990                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REPLACE_KEY,
     991                 :            :                                               0, 0, 0xa1, 0xa11);
     992                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
     993         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
     994                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
     995         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg->rkey == 0xa11);
     996                 :            : 
     997                 :            :         /* TEST CASE: Host A with g_ctrlr1_A get reservation with
     998                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE
     999                 :            :          */
    1000                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1001                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE, 0xa11, 0x0);
    1002                 :          4 :         gen = g_ns.gen;
    1003                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr1_A, req);
    1004         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1005                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1006         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE);
    1007         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.crkey == 0xa11);
    1008         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == reg);
    1009         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.gen == gen);
    1010                 :            : 
    1011                 :            :         /* TEST CASE: g_ctrlr_C unregister with IEKEY enabled */
    1012                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_UNREGISTER_KEY,
    1013                 :            :                                               1, 0, 0, 0);
    1014                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_C, req);
    1015         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1016                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_C.hostid);
    1017         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1018                 :            : 
    1019                 :            :         /* TEST CASE: g_ctrlr_B unregister with correct key */
    1020                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_UNREGISTER_KEY,
    1021                 :            :                                               0, 0, 0xb1, 0);
    1022                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
    1023         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1024                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1025         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1026                 :            : 
    1027                 :            :         /* TEST CASE: No registrant now, g_ctrlr_B replace new key with IEKEY disabled */
    1028                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REPLACE_KEY,
    1029                 :            :                                               0, 0, 0, 0xb1);
    1030                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
    1031         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc != SPDK_NVME_SC_SUCCESS);
    1032                 :            : 
    1033                 :            :         /* TEST CASE: No registrant now, g_ctrlr_B replace new key with IEKEY enabled */
    1034                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REPLACE_KEY,
    1035                 :            :                                               1, 0, 0, 0xb1);
    1036                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
    1037         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1038                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1039         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1040                 :            : 
    1041                 :            :         /* TEST CASE: g_ctrlr_B replace new key with IEKEY enabled and wrong crkey  */
    1042                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REPLACE_KEY,
    1043                 :            :                                               1, 0, 0xff, 0xb2);
    1044                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
    1045         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1046                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1047         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1048         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg->rkey == 0xb2);
    1049                 :            : 
    1050                 :            :         /* TEST CASE: g_ctrlr1_A unregister with correct key,
    1051                 :            :          * reservation should be removed as well.
    1052                 :            :          */
    1053                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_UNREGISTER_KEY,
    1054                 :            :                                               0, 0, 0xa11, 0);
    1055                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    1056         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1057                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1058         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1059         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1060         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.crkey == 0);
    1061         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == NULL);
    1062                 :            : 
    1063                 :          4 :         ut_reservation_free_req(req);
    1064                 :          4 :         ut_reservation_deinit();
    1065                 :          4 : }
    1066                 :            : 
    1067                 :            : static void
    1068                 :          4 : test_reservation_register_with_ptpl(void)
    1069                 :            : {
    1070                 :            :         struct spdk_nvmf_request *req;
    1071                 :            :         struct spdk_nvme_cpl *rsp;
    1072                 :            :         struct spdk_nvmf_registrant *reg;
    1073                 :          4 :         bool update_sgroup = false;
    1074                 :            :         int rc;
    1075                 :          3 :         struct spdk_nvmf_reservation_info info;
    1076                 :            : 
    1077                 :          4 :         ut_reservation_init();
    1078                 :            : 
    1079                 :          4 :         req = ut_reservation_build_req(16);
    1080                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1081         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1082                 :            : 
    1083                 :            :         /* TEST CASE: No persistent file, register with PTPL enabled will fail */
    1084                 :          4 :         g_ns.ptpl_file = NULL;
    1085                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY, 0,
    1086                 :            :                                               SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS, 0, 0xa1);
    1087                 :          4 :         update_sgroup = nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    1088         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == false);
    1089         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc != SPDK_NVME_SC_SUCCESS);
    1090                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1091         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1092                 :            : 
    1093                 :            :         /* TEST CASE: Enable PTPL */
    1094                 :          4 :         g_ns.ptpl_file = "/tmp/Ns1PR.cfg";
    1095                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY, 0,
    1096                 :            :                                               SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS, 0, 0xa1);
    1097                 :          4 :         update_sgroup = nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    1098         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    1099         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1100   [ +  +  -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.ptpl_activated == true);
    1101                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    1102         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1103                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1104         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1105         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(!spdk_uuid_compare(&g_ctrlr1_A.hostid, &reg->hostid));
    1106                 :            :         /* Load reservation information from configuration file */
    1107         [ -  + ]:          4 :         memset(&info, 0, sizeof(info));
    1108                 :          4 :         rc = nvmf_ns_reservation_load(&g_ns, &info);
    1109         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1110         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.ptpl_activated == true);
    1111                 :            : 
    1112                 :            :         /* TEST CASE: Disable PTPL */
    1113                 :          4 :         rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
    1114                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY, 0,
    1115                 :            :                                               SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON, 0, 0xa1);
    1116                 :          4 :         update_sgroup = nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    1117         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    1118         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1119   [ +  +  -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.ptpl_activated == false);
    1120                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    1121         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1122                 :          4 :         rc = nvmf_ns_reservation_load(&g_ns, &info);
    1123         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc < 0);
    1124         [ -  + ]:          4 :         unlink(g_ns.ptpl_file);
    1125                 :            : 
    1126                 :          4 :         ut_reservation_free_req(req);
    1127                 :          4 :         ut_reservation_deinit();
    1128                 :          4 : }
    1129                 :            : 
    1130                 :            : static void
    1131                 :          4 : test_reservation_acquire_preempt_1(void)
    1132                 :            : {
    1133                 :            :         struct spdk_nvmf_request *req;
    1134                 :            :         struct spdk_nvme_cpl *rsp;
    1135                 :            :         struct spdk_nvmf_registrant *reg;
    1136                 :            :         uint32_t gen;
    1137                 :            : 
    1138                 :          4 :         ut_reservation_init();
    1139                 :            : 
    1140                 :          4 :         req = ut_reservation_build_req(16);
    1141                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1142         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1143                 :            : 
    1144                 :          4 :         ut_reservation_build_registrants();
    1145                 :            : 
    1146                 :          4 :         gen = g_ns.gen;
    1147                 :            :         /* ACQUIRE: Host A with g_ctrlr1_A acquire reservation with
    1148                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE.
    1149                 :            :          */
    1150                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1151                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xa1, 0x0);
    1152                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr1_A, req);
    1153         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1154                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1155         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1156         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.crkey == 0xa1);
    1157         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == reg);
    1158         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.gen == gen);
    1159                 :            : 
    1160                 :            :         /* TEST CASE: g_ctrlr1_A holds the reservation, g_ctrlr_B preempt g_ctrl1_A,
    1161                 :            :          * g_ctrl1_A registrant is unregistered.
    1162                 :            :          */
    1163                 :          4 :         gen = g_ns.gen;
    1164                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_PREEMPT, 0,
    1165                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS, 0xb1, 0xa1);
    1166                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1167         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1168                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1169         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1170                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1171         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1172         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == reg);
    1173                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_C.hostid);
    1174         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1175         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS);
    1176         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.gen > gen);
    1177                 :            : 
    1178                 :            :         /* TEST CASE: g_ctrlr_B holds the reservation, g_ctrlr_C preempt g_ctrlr_B
    1179                 :            :          * with valid key and PRKEY set to 0, all registrants other the host that issued
    1180                 :            :          * the command are unregistered.
    1181                 :            :          */
    1182                 :          4 :         gen = g_ns.gen;
    1183                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_PREEMPT, 0,
    1184                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS, 0xc1, 0x0);
    1185                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_C, req);
    1186         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1187                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr2_A.hostid);
    1188         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1189                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1190         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1191                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_C.hostid);
    1192         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1193         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == reg);
    1194         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS);
    1195         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.gen > gen);
    1196                 :            : 
    1197                 :          4 :         ut_reservation_free_req(req);
    1198                 :          4 :         ut_reservation_deinit();
    1199                 :          4 : }
    1200                 :            : 
    1201                 :            : static void
    1202                 :          4 : test_reservation_acquire_release_with_ptpl(void)
    1203                 :            : {
    1204                 :            :         struct spdk_nvmf_request *req;
    1205                 :            :         struct spdk_nvme_cpl *rsp;
    1206                 :            :         struct spdk_nvmf_registrant *reg;
    1207                 :          4 :         bool update_sgroup = false;
    1208                 :          3 :         struct spdk_uuid holder_uuid;
    1209                 :            :         int rc;
    1210                 :          3 :         struct spdk_nvmf_reservation_info info;
    1211                 :            : 
    1212                 :          4 :         ut_reservation_init();
    1213                 :            : 
    1214                 :          4 :         req = ut_reservation_build_req(16);
    1215                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1216         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1217                 :            : 
    1218                 :            :         /* TEST CASE: Enable PTPL */
    1219                 :          4 :         g_ns.ptpl_file = "/tmp/Ns1PR.cfg";
    1220                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY, 0,
    1221                 :            :                                               SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS, 0, 0xa1);
    1222                 :          4 :         update_sgroup = nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    1223         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    1224         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1225   [ +  +  -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.ptpl_activated == true);
    1226                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    1227         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1228                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1229         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1230         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(!spdk_uuid_compare(&g_ctrlr1_A.hostid, &reg->hostid));
    1231                 :            :         /* Load reservation information from configuration file */
    1232         [ -  + ]:          4 :         memset(&info, 0, sizeof(info));
    1233                 :          4 :         rc = nvmf_ns_reservation_load(&g_ns, &info);
    1234         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1235         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.ptpl_activated == true);
    1236                 :            : 
    1237                 :            :         /* TEST CASE: Acquire the reservation */
    1238                 :          4 :         rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
    1239                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1240                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xa1, 0x0);
    1241                 :          4 :         update_sgroup = nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr1_A, req);
    1242         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    1243         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1244                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    1245         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1246         [ -  + ]:          4 :         memset(&info, 0, sizeof(info));
    1247                 :          4 :         rc = nvmf_ns_reservation_load(&g_ns, &info);
    1248         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1249         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.ptpl_activated == true);
    1250         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1251         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.crkey == 0xa1);
    1252                 :          4 :         spdk_uuid_parse(&holder_uuid, info.holder_uuid);
    1253         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(!spdk_uuid_compare(&g_ctrlr1_A.hostid, &holder_uuid));
    1254                 :            : 
    1255                 :            :         /* TEST CASE: Release the reservation */
    1256                 :          4 :         rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
    1257                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_RELEASE, 0,
    1258                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xa1);
    1259                 :          4 :         update_sgroup = nvmf_ns_reservation_release(&g_ns, &g_ctrlr1_A, req);
    1260         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    1261         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1262                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    1263         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1264         [ -  + ]:          4 :         memset(&info, 0, sizeof(info));
    1265                 :          4 :         rc = nvmf_ns_reservation_load(&g_ns, &info);
    1266         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    1267         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.rtype == 0);
    1268         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.crkey == 0);
    1269         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(info.ptpl_activated == true);
    1270         [ -  + ]:          4 :         unlink(g_ns.ptpl_file);
    1271                 :            : 
    1272                 :          4 :         ut_reservation_free_req(req);
    1273                 :          4 :         ut_reservation_deinit();
    1274                 :          4 : }
    1275                 :            : 
    1276                 :            : static void
    1277                 :          4 : test_reservation_release(void)
    1278                 :            : {
    1279                 :            :         struct spdk_nvmf_request *req;
    1280                 :            :         struct spdk_nvme_cpl *rsp;
    1281                 :            :         struct spdk_nvmf_registrant *reg;
    1282                 :            : 
    1283                 :          4 :         ut_reservation_init();
    1284                 :            : 
    1285                 :          4 :         req = ut_reservation_build_req(16);
    1286                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1287         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1288                 :            : 
    1289                 :          4 :         ut_reservation_build_registrants();
    1290                 :            : 
    1291                 :            :         /* ACQUIRE: Host A with g_ctrlr1_A get reservation with
    1292                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS
    1293                 :            :          */
    1294                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1295                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS, 0xa1, 0x0);
    1296                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr1_A, req);
    1297         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1298                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1299         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS);
    1300         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == reg);
    1301                 :            : 
    1302                 :            :         /* Test Case: Host B release the reservation */
    1303                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_RELEASE, 0,
    1304                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS, 0xb1);
    1305                 :          4 :         nvmf_ns_reservation_release(&g_ns, &g_ctrlr_B, req);
    1306         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1307         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1308         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.crkey == 0);
    1309         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.holder == NULL);
    1310                 :            : 
    1311                 :            :         /* Test Case: Host C clear the registrants */
    1312                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_CLEAR, 0,
    1313                 :            :                                              0, 0xc1);
    1314                 :          4 :         nvmf_ns_reservation_release(&g_ns, &g_ctrlr_C, req);
    1315         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1316                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr1_A.hostid);
    1317         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1318                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr2_A.hostid);
    1319         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1320                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_B.hostid);
    1321         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1322                 :          4 :         reg = nvmf_ns_reservation_get_registrant(&g_ns, &g_ctrlr_C.hostid);
    1323         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg == NULL);
    1324                 :            : 
    1325                 :          4 :         ut_reservation_free_req(req);
    1326                 :          4 :         ut_reservation_deinit();
    1327                 :          4 : }
    1328                 :            : 
    1329                 :            : void
    1330                 :         88 : nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
    1331                 :            :                                   struct spdk_nvmf_ns *ns,
    1332                 :            :                                   enum spdk_nvme_reservation_notification_log_page_type type)
    1333                 :            : {
    1334                 :         88 :         ctrlr->num_avail_log_pages++;
    1335                 :         88 : }
    1336                 :            : 
    1337                 :            : static void
    1338                 :          4 : test_reservation_unregister_notification(void)
    1339                 :            : {
    1340                 :            :         struct spdk_nvmf_request *req;
    1341                 :            :         struct spdk_nvme_cpl *rsp;
    1342                 :            : 
    1343                 :          4 :         ut_reservation_init();
    1344                 :            : 
    1345                 :          4 :         req = ut_reservation_build_req(16);
    1346         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1347                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1348                 :            : 
    1349                 :          4 :         ut_reservation_build_registrants();
    1350                 :            : 
    1351                 :            :         /* ACQUIRE: Host B with g_ctrlr_B get reservation with
    1352                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY
    1353                 :            :          */
    1354                 :          4 :         rsp->status.sc = 0xff;
    1355                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1356                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xb1, 0x0);
    1357                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1358         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1359         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1360                 :            : 
    1361                 :            :         /* Test Case : g_ctrlr_B holds the reservation, g_ctrlr_B unregister the registration.
    1362                 :            :          * Reservation release notification sends to g_ctrlr1_A/g_ctrlr2_A/g_ctrlr_C only for
    1363                 :            :          * SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY or SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY
    1364                 :            :          * type.
    1365                 :            :          */
    1366                 :          4 :         rsp->status.sc = 0xff;
    1367                 :          4 :         g_ctrlr1_A.num_avail_log_pages = 0;
    1368                 :          4 :         g_ctrlr2_A.num_avail_log_pages = 0;
    1369                 :          4 :         g_ctrlr_B.num_avail_log_pages = 5;
    1370                 :          4 :         g_ctrlr_C.num_avail_log_pages = 0;
    1371                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_UNREGISTER_KEY,
    1372                 :            :                                               0, 0, 0xb1, 0);
    1373                 :          4 :         nvmf_ns_reservation_register(&g_ns, &g_ctrlr_B, req);
    1374         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1375         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1376         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr1_A.num_avail_log_pages);
    1377         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr2_A.num_avail_log_pages);
    1378         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_B.num_avail_log_pages);
    1379         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr_C.num_avail_log_pages);
    1380                 :            : 
    1381                 :          4 :         ut_reservation_free_req(req);
    1382                 :          4 :         ut_reservation_deinit();
    1383                 :          4 : }
    1384                 :            : 
    1385                 :            : static void
    1386                 :          4 : test_reservation_release_notification(void)
    1387                 :            : {
    1388                 :            :         struct spdk_nvmf_request *req;
    1389                 :            :         struct spdk_nvme_cpl *rsp;
    1390                 :            : 
    1391                 :          4 :         ut_reservation_init();
    1392                 :            : 
    1393                 :          4 :         req = ut_reservation_build_req(16);
    1394         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1395                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1396                 :            : 
    1397                 :          4 :         ut_reservation_build_registrants();
    1398                 :            : 
    1399                 :            :         /* ACQUIRE: Host B with g_ctrlr_B get reservation with
    1400                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY
    1401                 :            :          */
    1402                 :          4 :         rsp->status.sc = 0xff;
    1403                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1404                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xb1, 0x0);
    1405                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1406         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1407         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1408                 :            : 
    1409                 :            :         /* Test Case : g_ctrlr_B holds the reservation, g_ctrlr_B release the reservation.
    1410                 :            :          * Reservation release notification sends to g_ctrlr1_A/g_ctrlr2_A/g_ctrlr_C.
    1411                 :            :          */
    1412                 :          4 :         rsp->status.sc = 0xff;
    1413                 :          4 :         g_ctrlr1_A.num_avail_log_pages = 0;
    1414                 :          4 :         g_ctrlr2_A.num_avail_log_pages = 0;
    1415                 :          4 :         g_ctrlr_B.num_avail_log_pages = 5;
    1416                 :          4 :         g_ctrlr_C.num_avail_log_pages = 0;
    1417                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_RELEASE, 0,
    1418                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xb1);
    1419                 :          4 :         nvmf_ns_reservation_release(&g_ns, &g_ctrlr_B, req);
    1420         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1421         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1422         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr1_A.num_avail_log_pages);
    1423         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr2_A.num_avail_log_pages);
    1424         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_B.num_avail_log_pages);
    1425         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr_C.num_avail_log_pages);
    1426                 :            : 
    1427                 :          4 :         ut_reservation_free_req(req);
    1428                 :          4 :         ut_reservation_deinit();
    1429                 :          4 : }
    1430                 :            : 
    1431                 :            : static void
    1432                 :          4 : test_reservation_release_notification_write_exclusive(void)
    1433                 :            : {
    1434                 :            :         struct spdk_nvmf_request *req;
    1435                 :            :         struct spdk_nvme_cpl *rsp;
    1436                 :            : 
    1437                 :          4 :         ut_reservation_init();
    1438                 :            : 
    1439                 :          4 :         req = ut_reservation_build_req(16);
    1440         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1441                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1442                 :            : 
    1443                 :          4 :         ut_reservation_build_registrants();
    1444                 :            : 
    1445                 :            :         /* ACQUIRE: Host B with g_ctrlr_B get reservation with
    1446                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE
    1447                 :            :          */
    1448                 :          4 :         rsp->status.sc = 0xff;
    1449                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1450                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE, 0xb1, 0x0);
    1451                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1452         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1453         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE);
    1454                 :            : 
    1455                 :            :         /* Test Case : g_ctrlr_B holds the reservation, g_ctrlr_B release the reservation.
    1456                 :            :          * Because the reservation type is SPDK_NVME_RESERVE_WRITE_EXCLUSIVE,
    1457                 :            :          * no reservation notification occurs.
    1458                 :            :          */
    1459                 :          4 :         rsp->status.sc = 0xff;
    1460                 :          4 :         g_ctrlr1_A.num_avail_log_pages = 5;
    1461                 :          4 :         g_ctrlr2_A.num_avail_log_pages = 5;
    1462                 :          4 :         g_ctrlr_B.num_avail_log_pages = 5;
    1463                 :          4 :         g_ctrlr_C.num_avail_log_pages = 5;
    1464                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_RELEASE, 0,
    1465                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE, 0xb1);
    1466                 :          4 :         nvmf_ns_reservation_release(&g_ns, &g_ctrlr_B, req);
    1467         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1468         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1469         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr1_A.num_avail_log_pages);
    1470         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr2_A.num_avail_log_pages);
    1471         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_B.num_avail_log_pages);
    1472         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_C.num_avail_log_pages);
    1473                 :            : 
    1474                 :          4 :         ut_reservation_free_req(req);
    1475                 :          4 :         ut_reservation_deinit();
    1476                 :          4 : }
    1477                 :            : 
    1478                 :            : static void
    1479                 :          4 : test_reservation_clear_notification(void)
    1480                 :            : {
    1481                 :            :         struct spdk_nvmf_request *req;
    1482                 :            :         struct spdk_nvme_cpl *rsp;
    1483                 :            : 
    1484                 :          4 :         ut_reservation_init();
    1485                 :            : 
    1486                 :          4 :         req = ut_reservation_build_req(16);
    1487         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1488                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1489                 :            : 
    1490                 :          4 :         ut_reservation_build_registrants();
    1491                 :            : 
    1492                 :            :         /* ACQUIRE: Host B with g_ctrlr_B get reservation with
    1493                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY
    1494                 :            :          */
    1495                 :          4 :         rsp->status.sc = 0xff;
    1496                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1497                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xb1, 0x0);
    1498                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1499         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1500         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1501                 :            : 
    1502                 :            :         /* Test Case : g_ctrlr_B holds the reservation, g_ctrlr_B clear the reservation.
    1503                 :            :          * Reservation Preempted notification sends to g_ctrlr1_A/g_ctrlr2_A/g_ctrlr_C.
    1504                 :            :          */
    1505                 :          4 :         rsp->status.sc = 0xff;
    1506                 :          4 :         g_ctrlr1_A.num_avail_log_pages = 0;
    1507                 :          4 :         g_ctrlr2_A.num_avail_log_pages = 0;
    1508                 :          4 :         g_ctrlr_B.num_avail_log_pages = 5;
    1509                 :          4 :         g_ctrlr_C.num_avail_log_pages = 0;
    1510                 :          4 :         ut_reservation_build_release_request(req, SPDK_NVME_RESERVE_CLEAR, 0,
    1511                 :            :                                              0, 0xb1);
    1512                 :          4 :         nvmf_ns_reservation_release(&g_ns, &g_ctrlr_B, req);
    1513         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1514         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == 0);
    1515         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr1_A.num_avail_log_pages);
    1516         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr2_A.num_avail_log_pages);
    1517         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_B.num_avail_log_pages);
    1518         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr_C.num_avail_log_pages);
    1519                 :            : 
    1520                 :          4 :         ut_reservation_free_req(req);
    1521                 :          4 :         ut_reservation_deinit();
    1522                 :          4 : }
    1523                 :            : 
    1524                 :            : static void
    1525                 :          4 : test_reservation_preempt_notification(void)
    1526                 :            : {
    1527                 :            :         struct spdk_nvmf_request *req;
    1528                 :            :         struct spdk_nvme_cpl *rsp;
    1529                 :            : 
    1530                 :          4 :         ut_reservation_init();
    1531                 :            : 
    1532                 :          4 :         req = ut_reservation_build_req(16);
    1533         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    1534                 :          4 :         rsp = &req->rsp->nvme_cpl;
    1535                 :            : 
    1536                 :          4 :         ut_reservation_build_registrants();
    1537                 :            : 
    1538                 :            :         /* ACQUIRE: Host B with g_ctrlr_B get reservation with
    1539                 :            :          * type SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY
    1540                 :            :          */
    1541                 :          4 :         rsp->status.sc = 0xff;
    1542                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    1543                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xb1, 0x0);
    1544                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_B, req);
    1545         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1546         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    1547                 :            : 
    1548                 :            :         /* Test Case : g_ctrlr_B holds the reservation, g_ctrlr_C preempt g_ctrlr_B,
    1549                 :            :          * g_ctrlr_B registrant is unregistered, and reservation is preempted.
    1550                 :            :          * Registration Preempted notification sends to g_ctrlr_B.
    1551                 :            :          * Reservation Preempted notification sends to g_ctrlr1_A/g_ctrlr2_A.
    1552                 :            :          */
    1553                 :          4 :         rsp->status.sc = 0xff;
    1554                 :          4 :         g_ctrlr1_A.num_avail_log_pages = 0;
    1555                 :          4 :         g_ctrlr2_A.num_avail_log_pages = 0;
    1556                 :          4 :         g_ctrlr_B.num_avail_log_pages = 0;
    1557                 :          4 :         g_ctrlr_C.num_avail_log_pages = 5;
    1558                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_PREEMPT, 0,
    1559                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS, 0xc1, 0xb1);
    1560                 :          4 :         nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr_C, req);
    1561         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    1562         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS);
    1563         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr1_A.num_avail_log_pages);
    1564         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr2_A.num_avail_log_pages);
    1565         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(1 == g_ctrlr_B.num_avail_log_pages);
    1566         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(5 == g_ctrlr_C.num_avail_log_pages);
    1567                 :            : 
    1568                 :          4 :         ut_reservation_free_req(req);
    1569                 :          4 :         ut_reservation_deinit();
    1570                 :          4 : }
    1571                 :            : 
    1572                 :            : static int
    1573                 :          0 : nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf)
    1574                 :            : {
    1575                 :          0 :         return 0;
    1576                 :            : }
    1577                 :            : 
    1578                 :            : static void
    1579                 :          0 : nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf)
    1580                 :            : {
    1581                 :          0 : }
    1582                 :            : 
    1583                 :            : static void
    1584                 :          4 : test_spdk_nvmf_ns_event(void)
    1585                 :            : {
    1586                 :          4 :         struct spdk_nvmf_tgt tgt = {};
    1587                 :          4 :         struct spdk_nvmf_subsystem subsystem = {
    1588                 :            :                 .max_nsid = 1024,
    1589                 :            :                 .ns = NULL,
    1590                 :            :                 .tgt = &tgt,
    1591                 :          1 :                 .state_changes = TAILQ_HEAD_INITIALIZER(subsystem.state_changes),
    1592                 :            :         };
    1593                 :          4 :         struct spdk_nvmf_ctrlr ctrlr = {
    1594                 :            :                 .subsys = &subsystem
    1595                 :            :         };
    1596                 :          3 :         struct spdk_nvmf_ns_opts ns_opts;
    1597                 :            :         uint32_t nsid;
    1598                 :            :         struct spdk_bdev *bdev;
    1599                 :            :         struct spdk_thread *thread;
    1600                 :            : 
    1601                 :          4 :         ctrlr.visible_ns = spdk_bit_array_create(1);
    1602                 :          4 :         spdk_bit_array_set(ctrlr.visible_ns, 0);
    1603                 :            : 
    1604                 :          4 :         thread = spdk_get_thread();
    1605         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(thread != NULL);
    1606                 :            : 
    1607                 :          4 :         subsystem.ns = calloc(subsystem.max_nsid, sizeof(struct spdk_nvmf_subsystem_ns *));
    1608         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
    1609                 :          4 :         subsystem.ana_group = calloc(subsystem.max_nsid, sizeof(uint32_t));
    1610         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ana_group != NULL);
    1611                 :            : 
    1612                 :          4 :         tgt.max_subsystems = 1024;
    1613                 :          4 :         tgt.subsystem_ids = spdk_bit_array_create(tgt.max_subsystems);
    1614                 :          4 :         RB_INIT(&tgt.subsystems);
    1615                 :            : 
    1616                 :          4 :         spdk_io_device_register(&tgt,
    1617                 :            :                                 nvmf_tgt_create_poll_group,
    1618                 :            :                                 nvmf_tgt_destroy_poll_group,
    1619                 :            :                                 sizeof(struct spdk_nvmf_poll_group),
    1620                 :            :                                 NULL);
    1621                 :            : 
    1622                 :            :         /* Add one namespace */
    1623                 :          4 :         spdk_nvmf_ns_opts_get_defaults(&ns_opts, sizeof(ns_opts));
    1624                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, "bdev1", &ns_opts, sizeof(ns_opts), NULL);
    1625                 :          4 :         CU_ASSERT(nsid == 1);
    1626                 :          4 :         CU_ASSERT(NULL != subsystem.ns[0]);
    1627                 :          4 :         CU_ASSERT(subsystem.ns[nsid - 1]->bdev == &g_bdevs[nsid - 1]);
    1628                 :            : 
    1629                 :          4 :         bdev = subsystem.ns[nsid - 1]->bdev;
    1630                 :            : 
    1631                 :            :         /* Add one controller */
    1632                 :          4 :         TAILQ_INIT(&subsystem.ctrlrs);
    1633                 :          4 :         TAILQ_INSERT_TAIL(&subsystem.ctrlrs, &ctrlr, link);
    1634                 :            : 
    1635                 :            :         /* Namespace resize event */
    1636                 :          4 :         subsystem.state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1637                 :          4 :         g_ns_changed_nsid = 0xFFFFFFFF;
    1638                 :          4 :         g_ns_changed_ctrlr = NULL;
    1639                 :          4 :         nvmf_ns_event(SPDK_BDEV_EVENT_RESIZE, bdev, subsystem.ns[0]);
    1640                 :          4 :         CU_ASSERT(SPDK_NVMF_SUBSYSTEM_PAUSING == subsystem.state);
    1641                 :            : 
    1642                 :          4 :         poll_threads();
    1643                 :          4 :         CU_ASSERT(1 == g_ns_changed_nsid);
    1644                 :          4 :         CU_ASSERT(&ctrlr == g_ns_changed_ctrlr);
    1645                 :          4 :         CU_ASSERT(SPDK_NVMF_SUBSYSTEM_ACTIVE == subsystem.state);
    1646                 :            : 
    1647                 :            :         /* Namespace remove event */
    1648                 :          4 :         subsystem.state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1649                 :          4 :         g_ns_changed_nsid = 0xFFFFFFFF;
    1650                 :          4 :         g_ns_changed_ctrlr = NULL;
    1651                 :          4 :         nvmf_ns_event(SPDK_BDEV_EVENT_REMOVE, bdev, subsystem.ns[0]);
    1652                 :          4 :         CU_ASSERT(SPDK_NVMF_SUBSYSTEM_PAUSING == subsystem.state);
    1653                 :          4 :         CU_ASSERT(0xFFFFFFFF == g_ns_changed_nsid);
    1654                 :          4 :         CU_ASSERT(NULL == g_ns_changed_ctrlr);
    1655                 :            : 
    1656                 :          4 :         poll_threads();
    1657                 :          4 :         CU_ASSERT(1 == g_ns_changed_nsid);
    1658                 :          4 :         CU_ASSERT(&ctrlr == g_ns_changed_ctrlr);
    1659                 :          4 :         CU_ASSERT(NULL == subsystem.ns[0]);
    1660                 :          4 :         CU_ASSERT(SPDK_NVMF_SUBSYSTEM_ACTIVE == subsystem.state);
    1661                 :            : 
    1662                 :          4 :         spdk_io_device_unregister(&tgt, NULL);
    1663                 :            : 
    1664                 :          4 :         poll_threads();
    1665                 :            : 
    1666                 :          4 :         free(subsystem.ns);
    1667                 :          4 :         free(subsystem.ana_group);
    1668                 :          4 :         spdk_bit_array_free(&ctrlr.visible_ns);
    1669                 :          4 :         spdk_bit_array_free(&tgt.subsystem_ids);
    1670                 :          4 : }
    1671                 :            : 
    1672                 :            : static void
    1673                 :          4 : test_nvmf_ns_reservation_add_remove_registrant(void)
    1674                 :            : {
    1675                 :          4 :         struct spdk_nvmf_ns ns = {};
    1676                 :          4 :         struct spdk_nvmf_ctrlr ctrlr = {};
    1677                 :          4 :         struct spdk_nvmf_registrant *reg = NULL;
    1678                 :            :         int rc;
    1679                 :            : 
    1680                 :          4 :         TAILQ_INIT(&ns.registrants);
    1681                 :          4 :         spdk_uuid_generate(&ctrlr.hostid);
    1682                 :            : 
    1683                 :          4 :         rc = nvmf_ns_reservation_add_registrant(&ns, &ctrlr, 0xa11);
    1684                 :          4 :         CU_ASSERT(rc == 0);
    1685                 :          4 :         reg = TAILQ_FIRST(&ns.registrants);
    1686         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    1687                 :          4 :         CU_ASSERT(ns.gen == 1);
    1688                 :          4 :         CU_ASSERT(reg->rkey == 0xa11);
    1689         [ -  + ]:          4 :         CU_ASSERT(!strncmp((uint8_t *)&reg->hostid, (uint8_t *)&ctrlr.hostid, sizeof(ctrlr.hostid)));
    1690                 :            : 
    1691                 :          4 :         nvmf_ns_reservation_remove_registrant(&ns, reg);
    1692                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&ns.registrants));
    1693                 :          4 :         CU_ASSERT(ns.gen == 2);
    1694                 :          4 : }
    1695                 :            : 
    1696                 :            : static void
    1697                 :          0 : test_nvmf_subsystem_destroy_cb(void *cb_arg)
    1698                 :            : {
    1699                 :          0 : }
    1700                 :            : 
    1701                 :            : static void
    1702                 :          4 : test_nvmf_subsystem_add_ctrlr(void)
    1703                 :            : {
    1704                 :            :         int rc;
    1705                 :          4 :         struct spdk_nvmf_ctrlr ctrlr = {};
    1706                 :          4 :         struct spdk_nvmf_tgt tgt = {};
    1707                 :          4 :         char nqn[256] = "nqn.2016-06.io.spdk:subsystem1";
    1708                 :          4 :         struct spdk_nvmf_subsystem *subsystem = NULL;
    1709                 :            : 
    1710                 :          4 :         tgt.max_subsystems = 1024;
    1711                 :          4 :         tgt.subsystem_ids = spdk_bit_array_create(tgt.max_subsystems);
    1712                 :          4 :         RB_INIT(&tgt.subsystems);
    1713                 :            : 
    1714                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, nqn, SPDK_NVMF_SUBTYPE_NVME, 0);
    1715         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
    1716                 :          4 :         ctrlr.subsys = subsystem;
    1717                 :            : 
    1718                 :          4 :         ctrlr.dynamic_ctrlr = true;
    1719                 :          4 :         rc = nvmf_subsystem_add_ctrlr(subsystem, &ctrlr);
    1720                 :          4 :         CU_ASSERT(rc == 0);
    1721                 :          4 :         CU_ASSERT(!TAILQ_EMPTY(&subsystem->ctrlrs));
    1722                 :          4 :         CU_ASSERT(ctrlr.cntlid == 1);
    1723                 :          4 :         CU_ASSERT(nvmf_subsystem_get_ctrlr(subsystem, 1) == &ctrlr);
    1724                 :            : 
    1725                 :          4 :         nvmf_subsystem_remove_ctrlr(subsystem, &ctrlr);
    1726                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&subsystem->ctrlrs));
    1727                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, test_nvmf_subsystem_destroy_cb, NULL);
    1728                 :          4 :         CU_ASSERT(rc == 0);
    1729                 :          4 :         spdk_bit_array_free(&tgt.subsystem_ids);
    1730                 :          4 : }
    1731                 :            : 
    1732                 :            : static void
    1733                 :          0 : _add_transport_cb(void *arg, int status)
    1734                 :            : {
    1735                 :          0 :         CU_ASSERT(status == 0);
    1736                 :          0 : }
    1737                 :            : 
    1738                 :            : static int
    1739                 :          4 : transport_subsystem_add_host_err(struct spdk_nvmf_transport *transport,
    1740                 :            :                                  const struct spdk_nvmf_subsystem *subsystem,
    1741                 :            :                                  const char *hostnqn,
    1742                 :            :                                  const struct spdk_json_val *transport_specific)
    1743                 :            : {
    1744                 :          4 :         return -1;
    1745                 :            : }
    1746                 :            : 
    1747                 :            : void
    1748                 :          4 : spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt,
    1749                 :            :                             struct spdk_nvmf_transport *transport,
    1750                 :            :                             spdk_nvmf_tgt_add_transport_done_fn cb_fn,
    1751                 :            :                             void *cb_arg)
    1752                 :            : {
    1753                 :          4 :         TAILQ_INSERT_TAIL(&tgt->transports, transport, link);
    1754                 :          4 : }
    1755                 :            : 
    1756                 :            : static struct spdk_nvmf_transport *
    1757                 :          4 : transport_create(struct spdk_nvmf_transport_opts *opts)
    1758                 :            : {
    1759                 :          4 :         return &g_transport;
    1760                 :            : }
    1761                 :            : 
    1762                 :            : static void
    1763                 :          4 : test_spdk_nvmf_subsystem_add_host(void)
    1764                 :            : {
    1765                 :          4 :         struct spdk_nvmf_tgt tgt = {};
    1766                 :          4 :         struct spdk_nvmf_subsystem *subsystem = NULL;
    1767                 :            :         int rc;
    1768                 :          4 :         const char hostnqn[] = "nqn.2016-06.io.spdk:host1";
    1769                 :          4 :         const char subsystemnqn[] = "nqn.2016-06.io.spdk:subsystem1";
    1770                 :          4 :         struct spdk_nvmf_transport_opts opts = {
    1771                 :            :                 .opts_size = sizeof(struct spdk_nvmf_transport_opts),
    1772                 :            :                 .io_unit_size = 8192
    1773                 :            :         };
    1774                 :          4 :         const struct spdk_nvmf_transport_ops test_ops = {
    1775                 :            :                 .name = "transport_ut",
    1776                 :            :                 .create = transport_create,
    1777                 :            :                 .subsystem_add_host = transport_subsystem_add_host_err,
    1778                 :            :         };
    1779                 :            :         struct spdk_nvmf_transport *transport;
    1780                 :            : 
    1781                 :          4 :         tgt.max_subsystems = 1024;
    1782                 :          4 :         tgt.subsystem_ids = spdk_bit_array_create(tgt.max_subsystems);
    1783                 :          4 :         RB_INIT(&tgt.subsystems);
    1784                 :            : 
    1785                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, subsystemnqn, SPDK_NVMF_SUBTYPE_NVME, 0);
    1786         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
    1787         [ -  + ]:          4 :         CU_ASSERT_STRING_EQUAL(subsystem->subnqn, subsystemnqn);
    1788                 :            : 
    1789                 :          4 :         rc = spdk_nvmf_subsystem_add_host(subsystem, hostnqn, NULL);
    1790                 :          4 :         CU_ASSERT(rc == 0);
    1791                 :          4 :         CU_ASSERT(!TAILQ_EMPTY(&subsystem->hosts));
    1792                 :            : 
    1793                 :            :         /* Add existing nqn, this function isn't allowed to be called if the nqn was previously added. */
    1794                 :          4 :         rc = spdk_nvmf_subsystem_add_host(subsystem, hostnqn, NULL);
    1795                 :          4 :         CU_ASSERT(rc == -EINVAL);
    1796                 :            : 
    1797                 :          4 :         rc = spdk_nvmf_subsystem_remove_host(subsystem, hostnqn);
    1798                 :          4 :         CU_ASSERT(rc == 0);
    1799                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&subsystem->hosts));
    1800                 :            : 
    1801                 :            :         /* No available nqn */
    1802                 :          4 :         rc = spdk_nvmf_subsystem_remove_host(subsystem, hostnqn);
    1803                 :          4 :         CU_ASSERT(rc == -ENOENT);
    1804                 :            : 
    1805                 :            :         /* Ensure hostnqn list remains empty after transport callback fails */
    1806                 :          4 :         spdk_nvmf_transport_register(&test_ops);
    1807                 :          4 :         transport = spdk_nvmf_transport_create("transport_ut", &opts);
    1808         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(transport != NULL);
    1809                 :            : 
    1810                 :          4 :         TAILQ_INIT(&tgt.transports);
    1811                 :          4 :         spdk_nvmf_tgt_add_transport(&tgt, transport, _add_transport_cb, 0);
    1812                 :            : 
    1813                 :          4 :         rc = spdk_nvmf_subsystem_add_host(subsystem, hostnqn, NULL);
    1814                 :          4 :         CU_ASSERT(rc != 0);
    1815                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&subsystem->hosts));
    1816                 :            : 
    1817                 :          4 :         spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
    1818                 :          4 :         spdk_bit_array_free(&tgt.subsystem_ids);
    1819                 :          4 : }
    1820                 :            : 
    1821                 :            : static void
    1822                 :          4 : test_nvmf_ns_reservation_report(void)
    1823                 :            : {
    1824                 :          4 :         struct spdk_nvmf_ns ns = {};
    1825                 :          4 :         struct spdk_nvmf_ctrlr ctrlr = {};
    1826                 :          4 :         struct spdk_nvmf_request req = {};
    1827                 :          4 :         union nvmf_h2c_msg cmd = {};
    1828                 :          4 :         union nvmf_c2h_msg rsp = {};
    1829                 :            :         struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data;
    1830                 :            :         struct spdk_nvme_reservation_status_extended_data *status_data;
    1831                 :            :         struct spdk_nvmf_registrant *reg;
    1832                 :            :         void *data;
    1833                 :            : 
    1834                 :          4 :         data = calloc(1, sizeof(*status_data) + sizeof(*ctrlr_data) * 2);
    1835                 :          4 :         reg = calloc(2, sizeof(struct spdk_nvmf_registrant));
    1836   [ +  +  +  -  :          4 :         SPDK_CU_ASSERT_FATAL(data != NULL && reg != NULL);
                   -  + ]
    1837                 :            : 
    1838                 :          4 :         req.length = sizeof(*status_data) + sizeof(*ctrlr_data) * 2;
    1839                 :          4 :         SPDK_IOV_ONE(req.iov, &req.iovcnt, data, req.length);
    1840                 :            : 
    1841                 :          4 :         req.cmd = &cmd;
    1842                 :          4 :         req.rsp = &rsp;
    1843                 :          4 :         ns.gen = 1;
    1844                 :          4 :         ns.rtype = SPDK_NVME_RESERVE_WRITE_EXCLUSIVE;
    1845                 :          4 :         ns.ptpl_activated = true;
    1846                 :          4 :         cmd.nvme_cmd.cdw11_bits.resv_report.eds = true;
    1847                 :          4 :         cmd.nvme_cmd.cdw10 = 100;
    1848                 :          4 :         reg[0].rkey = 0xa;
    1849                 :          4 :         reg[1].rkey = 0xb;
    1850                 :          4 :         spdk_uuid_generate(&reg[0].hostid);
    1851                 :          4 :         spdk_uuid_generate(&reg[1].hostid);
    1852                 :          4 :         TAILQ_INIT(&ns.registrants);
    1853                 :          4 :         TAILQ_INSERT_TAIL(&ns.registrants, &reg[0], link);
    1854                 :          4 :         TAILQ_INSERT_TAIL(&ns.registrants, &reg[1], link);
    1855                 :            : 
    1856                 :          4 :         nvmf_ns_reservation_report(&ns, &ctrlr, &req);
    1857                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sct == SPDK_NVME_SCT_GENERIC);
    1858                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sc == SPDK_NVME_SC_SUCCESS);
    1859                 :            :         /* Get ctrlr data and status data pointers */
    1860                 :          4 :         ctrlr_data = (void *)((char *)req.iov[0].iov_base + sizeof(*status_data));
    1861                 :          4 :         status_data = (void *)req.iov[0].iov_base;
    1862   [ +  +  +  +  :          4 :         SPDK_CU_ASSERT_FATAL(status_data != NULL && ctrlr_data != NULL);
                   -  + ]
    1863                 :          4 :         CU_ASSERT(status_data->data.gen == 1);
    1864                 :          4 :         CU_ASSERT(status_data->data.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE);
    1865                 :          4 :         CU_ASSERT(status_data->data.ptpls == true);
    1866                 :          4 :         CU_ASSERT(status_data->data.regctl == 2);
    1867                 :          4 :         CU_ASSERT(ctrlr_data->cntlid == 0xffff);
    1868                 :          4 :         CU_ASSERT(ctrlr_data->rcsts.status == false);
    1869                 :          4 :         CU_ASSERT(ctrlr_data->rkey ==  0xa);
    1870                 :          4 :         CU_ASSERT(!spdk_uuid_compare((struct spdk_uuid *)ctrlr_data->hostid, &reg[0].hostid));
    1871                 :            :         /* Check second ctrlr data */
    1872                 :          4 :         ctrlr_data++;
    1873                 :          4 :         CU_ASSERT(ctrlr_data->cntlid == 0xffff);
    1874                 :          4 :         CU_ASSERT(ctrlr_data->rcsts.status == false);
    1875                 :          4 :         CU_ASSERT(ctrlr_data->rkey ==  0xb);
    1876                 :          4 :         CU_ASSERT(!spdk_uuid_compare((struct spdk_uuid *)ctrlr_data->hostid, &reg[1].hostid));
    1877                 :            : 
    1878                 :            :         /* extended controller data structure */
    1879                 :          4 :         spdk_iov_memset(req.iov, req.iovcnt, 0);
    1880         [ -  + ]:          4 :         memset(req.rsp, 0, sizeof(*req.rsp));
    1881                 :          4 :         cmd.nvme_cmd.cdw11_bits.resv_report.eds = false;
    1882                 :            : 
    1883                 :          4 :         nvmf_ns_reservation_report(&ns, &ctrlr, &req);
    1884                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sc == SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT);
    1885                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sct == SPDK_NVME_SCT_GENERIC);
    1886                 :            : 
    1887                 :            :         /* Transfer length invalid */
    1888                 :          4 :         spdk_iov_memset(req.iov, req.iovcnt, 0);
    1889         [ -  + ]:          4 :         memset(req.rsp, 0, sizeof(*req.rsp));
    1890                 :          4 :         cmd.nvme_cmd.cdw11_bits.resv_report.eds = true;
    1891                 :          4 :         cmd.nvme_cmd.cdw10 = 0;
    1892                 :            : 
    1893                 :          4 :         nvmf_ns_reservation_report(&ns, &ctrlr, &req);
    1894                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sc == SPDK_NVME_SC_INTERNAL_DEVICE_ERROR);
    1895                 :          4 :         CU_ASSERT(req.rsp->nvme_cpl.status.sct == SPDK_NVME_SCT_GENERIC);
    1896                 :            : 
    1897                 :          4 :         free(req.iov[0].iov_base);
    1898                 :          4 :         free(reg);
    1899                 :          4 : }
    1900                 :            : 
    1901                 :            : static void
    1902                 :          4 : test_nvmf_nqn_is_valid(void)
    1903                 :            : {
    1904                 :            :         bool rc;
    1905                 :          4 :         char uuid[SPDK_NVMF_UUID_STRING_LEN + 1] = {};
    1906                 :          4 :         char nqn[SPDK_NVMF_NQN_MAX_LEN + 1] = {};
    1907                 :          4 :         struct spdk_uuid s_uuid = {};
    1908                 :            : 
    1909                 :          4 :         spdk_uuid_generate(&s_uuid);
    1910                 :          4 :         spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid);
    1911                 :            : 
    1912                 :            :         /* discovery nqn */
    1913         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
    1914                 :            : 
    1915                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1916                 :          4 :         CU_ASSERT(rc == true);
    1917                 :            : 
    1918                 :            :         /* nqn with uuid */
    1919         [ -  + ]:          4 :         memset(nqn, 0xff, sizeof(nqn));
    1920         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s%s", SPDK_NVMF_NQN_UUID_PRE, uuid);
    1921                 :            : 
    1922                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1923                 :          4 :         CU_ASSERT(rc == true);
    1924                 :            : 
    1925                 :            :         /* Check nqn valid reverse domain */
    1926         [ -  + ]:          4 :         memset(nqn, 0xff, sizeof(nqn));
    1927         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s", "nqn.2016-06.io.spdk:cnode1");
    1928                 :            : 
    1929                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1930                 :          4 :         CU_ASSERT(rc == true);
    1931                 :            : 
    1932                 :            :         /* Invalid nqn length */
    1933         [ -  + ]:          4 :         memset(nqn, 0xff, sizeof(nqn));
    1934         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s", "nqn.");
    1935                 :            : 
    1936                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1937                 :          4 :         CU_ASSERT(rc == false);
    1938                 :            : 
    1939                 :            :         /* Copy uuid to the nqn string, but omit the last character to make it invalid */
    1940         [ -  + ]:          4 :         memset(nqn, 0, SPDK_NVMF_NQN_MAX_LEN + 1);
    1941         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s", SPDK_NVMF_NQN_UUID_PRE);
    1942   [ -  +  -  + ]:          4 :         memcpy(&nqn[SPDK_NVMF_NQN_UUID_PRE_LEN], uuid, SPDK_NVMF_UUID_STRING_LEN - 1);
    1943                 :            : 
    1944                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1945                 :          4 :         CU_ASSERT(rc == false);
    1946                 :            : 
    1947                 :            :         /* Invalid domain */
    1948         [ -  + ]:          4 :         memset(nqn, 0xff, SPDK_NVMF_NQN_MAX_LEN + 1);
    1949         [ -  + ]:          4 :         snprintf(nqn, sizeof(nqn), "%s", "nqn.2016-06.io...spdk:cnode1");
    1950                 :            : 
    1951                 :          4 :         rc = nvmf_nqn_is_valid(nqn);
    1952                 :          4 :         CU_ASSERT(rc == false);
    1953                 :          4 : }
    1954                 :            : 
    1955                 :            : static void
    1956                 :          4 : test_nvmf_ns_reservation_restore(void)
    1957                 :            : {
    1958                 :          4 :         struct spdk_nvmf_ns ns = {};
    1959                 :          4 :         struct spdk_nvmf_reservation_info info = {};
    1960                 :          4 :         struct spdk_bdev bdev = {};
    1961                 :          4 :         struct spdk_uuid s_uuid = {};
    1962                 :            :         struct spdk_nvmf_registrant *reg0, *reg1;
    1963                 :          4 :         char uuid[SPDK_UUID_STRING_LEN] = {};
    1964                 :            :         int rc;
    1965                 :            : 
    1966                 :          4 :         ns.bdev = &bdev;
    1967                 :          4 :         TAILQ_INIT(&ns.registrants);
    1968                 :          4 :         info.ptpl_activated = true;
    1969                 :          4 :         info.num_regs = 2;
    1970                 :          4 :         info.rtype = SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS;
    1971                 :          4 :         info.registrants[0].rkey = 0xb;
    1972                 :          4 :         info.registrants[1].rkey = 0xc;
    1973                 :            : 
    1974                 :            :         /* Generate and prepare uuids, make sure bdev and info uuid are the same */
    1975                 :          4 :         spdk_uuid_generate(&s_uuid);
    1976                 :          4 :         spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid);
    1977         [ -  + ]:          4 :         snprintf(info.holder_uuid, SPDK_UUID_STRING_LEN, "%s", uuid);
    1978         [ -  + ]:          4 :         snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid);
    1979         [ -  + ]:          4 :         snprintf(info.registrants[0].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid);
    1980                 :          4 :         spdk_uuid_copy(&bdev.uuid, &s_uuid);
    1981                 :          4 :         spdk_uuid_generate(&s_uuid);
    1982                 :          4 :         spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid);
    1983         [ -  + ]:          4 :         snprintf(info.registrants[1].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid);
    1984                 :            : 
    1985                 :            :         /* info->rkey not exist in registrants */
    1986                 :          4 :         info.crkey = 0xa;
    1987                 :            : 
    1988                 :          4 :         rc = nvmf_ns_reservation_restore(&ns, &info);
    1989                 :          4 :         CU_ASSERT(rc == -EINVAL);
    1990                 :            : 
    1991                 :            :         /* info->rkey exists in registrants */
    1992                 :          4 :         info.crkey = 0xb;
    1993                 :            : 
    1994                 :          4 :         rc = nvmf_ns_reservation_restore(&ns, &info);
    1995                 :          4 :         CU_ASSERT(rc == 0);
    1996                 :          4 :         CU_ASSERT(ns.crkey == 0xb);
    1997                 :          4 :         CU_ASSERT(ns.rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS);
    1998         [ -  + ]:          4 :         CU_ASSERT(ns.ptpl_activated == true);
    1999                 :            :         /* Check two registrant`s rkey */
    2000                 :          4 :         reg0 = TAILQ_FIRST(&ns.registrants);
    2001                 :          4 :         reg1 = TAILQ_NEXT(reg0, link);
    2002                 :          4 :         CU_ASSERT(ns.holder == reg0);
    2003                 :          4 :         CU_ASSERT(reg0->rkey == 0xb);
    2004                 :          4 :         CU_ASSERT(reg1->rkey == 0xc);
    2005                 :            : 
    2006                 :          4 :         rc = nvmf_ns_reservation_clear_all_registrants(&ns);
    2007                 :          4 :         CU_ASSERT(rc == 2);
    2008                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&ns.registrants));
    2009                 :            : 
    2010                 :            :         /* Existing bdev UUID is different with configuration */
    2011                 :          4 :         spdk_uuid_generate(&s_uuid);
    2012                 :          4 :         spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid);
    2013         [ -  + ]:          4 :         snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid);
    2014                 :          4 :         spdk_uuid_generate(&s_uuid);
    2015                 :          4 :         spdk_uuid_copy(&bdev.uuid, &s_uuid);
    2016                 :            : 
    2017                 :          4 :         rc = nvmf_ns_reservation_restore(&ns, &info);
    2018                 :          4 :         CU_ASSERT(rc == -EINVAL);
    2019                 :            : 
    2020                 :            :         /* Check restore without reservation */
    2021                 :          4 :         spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), &bdev.uuid);
    2022                 :          4 :         info.rtype = 0;
    2023                 :          4 :         info.crkey = 0;
    2024         [ -  + ]:          4 :         memset(info.holder_uuid, 0, SPDK_UUID_STRING_LEN);
    2025                 :            : 
    2026                 :          4 :         rc = nvmf_ns_reservation_restore(&ns, &info);
    2027                 :          4 :         CU_ASSERT(rc == 0);
    2028                 :          4 :         CU_ASSERT(ns.crkey == 0);
    2029                 :          4 :         CU_ASSERT(ns.rtype == 0);
    2030         [ -  + ]:          4 :         CU_ASSERT(ns.ptpl_activated == true);
    2031                 :          4 :         CU_ASSERT(ns.holder == NULL);
    2032                 :          4 :         reg0 = TAILQ_FIRST(&ns.registrants);
    2033                 :          4 :         reg1 = TAILQ_NEXT(reg0, link);
    2034                 :          4 :         CU_ASSERT(reg0->rkey == 0xb);
    2035                 :          4 :         CU_ASSERT(reg1->rkey == 0xc);
    2036                 :            : 
    2037                 :          4 :         rc = nvmf_ns_reservation_clear_all_registrants(&ns);
    2038                 :          4 :         CU_ASSERT(rc == 2);
    2039                 :          4 :         CU_ASSERT(TAILQ_EMPTY(&ns.registrants));
    2040                 :          4 : }
    2041                 :            : 
    2042                 :            : static void
    2043                 :          4 : ut_nvmf_subsystem_paused(struct spdk_nvmf_subsystem *subsystem, void *ctx, int status)
    2044                 :            : {
    2045                 :          4 :         CU_ASSERT_EQUAL(status, 0);
    2046                 :          4 :         CU_ASSERT_EQUAL(subsystem->state, SPDK_NVMF_SUBSYSTEM_PAUSED);
    2047                 :          4 : }
    2048                 :            : 
    2049                 :            : static void
    2050                 :          4 : test_nvmf_subsystem_state_change(void)
    2051                 :            : {
    2052                 :          4 :         struct spdk_nvmf_tgt tgt = {};
    2053                 :            :         struct spdk_nvmf_subsystem *subsystem, *discovery_subsystem;
    2054                 :            :         int rc;
    2055                 :            : 
    2056                 :          4 :         tgt.max_subsystems = 1024;
    2057                 :          4 :         tgt.subsystem_ids = spdk_bit_array_create(tgt.max_subsystems);
    2058                 :          4 :         RB_INIT(&tgt.subsystems);
    2059                 :            : 
    2060                 :          4 :         discovery_subsystem = spdk_nvmf_subsystem_create(&tgt, SPDK_NVMF_DISCOVERY_NQN,
    2061                 :            :                               SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT, 0);
    2062         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(discovery_subsystem != NULL);
    2063                 :          4 :         subsystem = spdk_nvmf_subsystem_create(&tgt, "nqn.2016-06.io.spdk:subsystem1",
    2064                 :            :                                                SPDK_NVMF_SUBTYPE_NVME, 0);
    2065         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem != NULL);
    2066                 :            : 
    2067                 :          4 :         spdk_io_device_register(&tgt,
    2068                 :            :                                 nvmf_tgt_create_poll_group,
    2069                 :            :                                 nvmf_tgt_destroy_poll_group,
    2070                 :            :                                 sizeof(struct spdk_nvmf_poll_group),
    2071                 :            :                                 NULL);
    2072                 :            : 
    2073                 :          4 :         rc = spdk_nvmf_subsystem_start(discovery_subsystem, NULL, NULL);
    2074                 :          4 :         CU_ASSERT(rc == 0);
    2075                 :          4 :         poll_threads();
    2076                 :          4 :         CU_ASSERT(discovery_subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVE);
    2077                 :          4 :         rc = spdk_nvmf_subsystem_start(subsystem, NULL, NULL);
    2078                 :          4 :         CU_ASSERT(rc == 0);
    2079                 :          4 :         poll_threads();
    2080                 :          4 :         CU_ASSERT(subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVE);
    2081                 :            : 
    2082                 :          4 :         rc = spdk_nvmf_subsystem_pause(subsystem, SPDK_NVME_GLOBAL_NS_TAG,
    2083                 :            :                                        ut_nvmf_subsystem_paused, NULL);
    2084                 :          4 :         CU_ASSERT(rc == 0);
    2085                 :          4 :         rc = spdk_nvmf_subsystem_stop(subsystem, NULL, NULL);
    2086                 :          4 :         CU_ASSERT(rc == 0);
    2087                 :          4 :         poll_threads();
    2088                 :          4 :         CU_ASSERT(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE);
    2089                 :            : 
    2090                 :          4 :         rc = spdk_nvmf_subsystem_stop(discovery_subsystem, NULL, NULL);
    2091                 :          4 :         CU_ASSERT(rc == 0);
    2092                 :          4 :         poll_threads();
    2093                 :          4 :         CU_ASSERT(discovery_subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE);
    2094                 :          4 :         rc = spdk_nvmf_subsystem_stop(subsystem, NULL, NULL);
    2095                 :          4 :         CU_ASSERT(rc == 0);
    2096                 :          4 :         poll_threads();
    2097                 :          4 :         CU_ASSERT(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE);
    2098                 :            : 
    2099                 :          4 :         rc = spdk_nvmf_subsystem_destroy(subsystem, NULL, NULL);
    2100                 :          4 :         CU_ASSERT(rc == 0);
    2101                 :          4 :         rc = spdk_nvmf_subsystem_destroy(discovery_subsystem, NULL, NULL);
    2102                 :          4 :         CU_ASSERT(rc == 0);
    2103                 :            : 
    2104                 :          4 :         spdk_io_device_unregister(&tgt, NULL);
    2105                 :          4 :         poll_threads();
    2106                 :            : 
    2107                 :          4 :         spdk_bit_array_free(&tgt.subsystem_ids);
    2108                 :          4 : }
    2109                 :            : 
    2110                 :            : static bool
    2111                 :         16 : ut_is_ptpl_capable(const struct spdk_nvmf_ns *ns)
    2112                 :            : {
    2113                 :         16 :         return true;
    2114                 :            : }
    2115                 :            : 
    2116                 :            : static struct spdk_nvmf_reservation_info g_resv_info;
    2117                 :            : 
    2118                 :            : static int
    2119                 :          8 : ut_update_reservation(const struct spdk_nvmf_ns *ns, const struct spdk_nvmf_reservation_info *info)
    2120                 :            : {
    2121                 :          8 :         g_resv_info = *info;
    2122                 :            : 
    2123                 :          8 :         return 0;
    2124                 :            : }
    2125                 :            : 
    2126                 :            : static int
    2127                 :          4 : ut_load_reservation(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info)
    2128                 :            : {
    2129                 :          4 :         *info = g_resv_info;
    2130                 :            : 
    2131                 :          4 :         return 0;
    2132                 :            : }
    2133                 :            : 
    2134                 :            : static void
    2135                 :          4 : test_nvmf_reservation_custom_ops(void)
    2136                 :            : {
    2137                 :          4 :         struct spdk_nvmf_ns_reservation_ops ops = {
    2138                 :            :                 .is_ptpl_capable = ut_is_ptpl_capable,
    2139                 :            :                 .update = ut_update_reservation,
    2140                 :            :                 .load = ut_load_reservation,
    2141                 :            :         };
    2142                 :            :         struct spdk_nvmf_request *req;
    2143                 :            :         struct spdk_nvme_cpl *rsp;
    2144                 :            :         struct spdk_nvmf_registrant *reg;
    2145                 :          4 :         bool update_sgroup = false;
    2146                 :          4 :         struct spdk_nvmf_tgt tgt = {};
    2147                 :          4 :         struct spdk_nvmf_subsystem subsystem = {
    2148                 :            :                 .max_nsid = 4,
    2149                 :            :                 .tgt = &tgt,
    2150                 :            :         };
    2151                 :            :         uint32_t nsid;
    2152                 :            :         struct spdk_nvmf_ns *ns;
    2153                 :            :         int rc;
    2154                 :            : 
    2155                 :          4 :         subsystem.ns = calloc(subsystem.max_nsid, sizeof(struct spdk_nvmf_subsystem_ns *));
    2156         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
    2157                 :          4 :         subsystem.ana_group = calloc(subsystem.max_nsid, sizeof(uint32_t));
    2158         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(subsystem.ana_group != NULL);
    2159                 :            : 
    2160                 :          4 :         spdk_nvmf_set_custom_ns_reservation_ops(&ops);
    2161                 :            : 
    2162                 :          4 :         ut_reservation_init();
    2163                 :            : 
    2164                 :          4 :         req = ut_reservation_build_req(16);
    2165                 :          4 :         rsp = &req->rsp->nvme_cpl;
    2166         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(req != NULL);
    2167                 :            : 
    2168                 :            :         /* Add a registrant and activate ptpl */
    2169                 :          4 :         ut_reservation_build_register_request(req, SPDK_NVME_RESERVE_REGISTER_KEY, 0,
    2170                 :            :                                               SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS, 0, 0xa1);
    2171                 :          4 :         update_sgroup = nvmf_ns_reservation_register(&g_ns, &g_ctrlr1_A, req);
    2172         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    2173         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    2174   [ +  +  -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(g_ns.ptpl_activated == true);
    2175                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    2176         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    2177                 :            : 
    2178                 :            :         /* Acquire a reservation */
    2179                 :          4 :         rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
    2180                 :          4 :         ut_reservation_build_acquire_request(req, SPDK_NVME_RESERVE_ACQUIRE, 0,
    2181                 :            :                                              SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY, 0xa1, 0x0);
    2182                 :          4 :         update_sgroup = nvmf_ns_reservation_acquire(&g_ns, &g_ctrlr1_A, req);
    2183         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(update_sgroup == true);
    2184         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rsp->status.sc == SPDK_NVME_SC_SUCCESS);
    2185                 :          4 :         rc = nvmf_ns_update_reservation_info(&g_ns);
    2186         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(rc == 0);
    2187                 :            : 
    2188                 :            :         /* Add the namespace using a different subsystem.
    2189                 :            :          * Reservation information should be restored. */
    2190                 :          4 :         nsid = spdk_nvmf_subsystem_add_ns_ext(&subsystem, g_ns.bdev->name, NULL, 0, NULL);
    2191                 :          4 :         CU_ASSERT(nsid == 1);
    2192                 :            : 
    2193                 :          4 :         ns = _nvmf_subsystem_get_ns(&subsystem, nsid);
    2194         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(ns != NULL);
    2195                 :          4 :         CU_ASSERT(ns->crkey == 0xa1);
    2196                 :          4 :         CU_ASSERT(ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY);
    2197         [ -  + ]:          4 :         CU_ASSERT(ns->ptpl_activated == true);
    2198                 :            : 
    2199                 :          4 :         reg = nvmf_ns_reservation_get_registrant(ns, &g_ctrlr1_A.hostid);
    2200         [ +  + ]:          4 :         SPDK_CU_ASSERT_FATAL(reg != NULL);
    2201         [ -  + ]:          4 :         SPDK_CU_ASSERT_FATAL(!spdk_uuid_compare(&g_ctrlr1_A.hostid, &reg->hostid));
    2202                 :          4 :         CU_ASSERT(reg == ns->holder);
    2203                 :            : 
    2204                 :          4 :         rc = spdk_nvmf_subsystem_remove_ns(&subsystem, nsid);
    2205                 :          4 :         CU_ASSERT(rc == 0);
    2206                 :            : 
    2207                 :          4 :         free(subsystem.ns);
    2208                 :          4 :         free(subsystem.ana_group);
    2209                 :          4 :         ut_reservation_free_req(req);
    2210                 :          4 :         ut_reservation_deinit();
    2211                 :          4 : }
    2212                 :            : 
    2213                 :            : int
    2214                 :          4 : main(int argc, char **argv)
    2215                 :            : {
    2216                 :          4 :         CU_pSuite       suite = NULL;
    2217                 :            :         unsigned int    num_failures;
    2218                 :            : 
    2219                 :          4 :         CU_initialize_registry();
    2220                 :            : 
    2221                 :          4 :         suite = CU_add_suite("nvmf", NULL, NULL);
    2222                 :            : 
    2223                 :          4 :         CU_ADD_TEST(suite, nvmf_test_create_subsystem);
    2224                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_subsystem_add_ns);
    2225                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_subsystem_add_fdp_ns);
    2226                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_subsystem_set_sn);
    2227                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_ns_visible);
    2228                 :          4 :         CU_ADD_TEST(suite, test_reservation_register);
    2229                 :          4 :         CU_ADD_TEST(suite, test_reservation_register_with_ptpl);
    2230                 :          4 :         CU_ADD_TEST(suite, test_reservation_acquire_preempt_1);
    2231                 :          4 :         CU_ADD_TEST(suite, test_reservation_acquire_release_with_ptpl);
    2232                 :          4 :         CU_ADD_TEST(suite, test_reservation_release);
    2233                 :          4 :         CU_ADD_TEST(suite, test_reservation_unregister_notification);
    2234                 :          4 :         CU_ADD_TEST(suite, test_reservation_release_notification);
    2235                 :          4 :         CU_ADD_TEST(suite, test_reservation_release_notification_write_exclusive);
    2236                 :          4 :         CU_ADD_TEST(suite, test_reservation_clear_notification);
    2237                 :          4 :         CU_ADD_TEST(suite, test_reservation_preempt_notification);
    2238                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_ns_event);
    2239                 :          4 :         CU_ADD_TEST(suite, test_nvmf_ns_reservation_add_remove_registrant);
    2240                 :          4 :         CU_ADD_TEST(suite, test_nvmf_subsystem_add_ctrlr);
    2241                 :          4 :         CU_ADD_TEST(suite, test_spdk_nvmf_subsystem_add_host);
    2242                 :          4 :         CU_ADD_TEST(suite, test_nvmf_ns_reservation_report);
    2243                 :          4 :         CU_ADD_TEST(suite, test_nvmf_nqn_is_valid);
    2244                 :          4 :         CU_ADD_TEST(suite, test_nvmf_ns_reservation_restore);
    2245                 :          4 :         CU_ADD_TEST(suite, test_nvmf_subsystem_state_change);
    2246                 :          4 :         CU_ADD_TEST(suite, test_nvmf_reservation_custom_ops);
    2247                 :            : 
    2248                 :          4 :         allocate_threads(1);
    2249                 :          4 :         set_thread(0);
    2250                 :            : 
    2251                 :          4 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
    2252                 :          4 :         CU_cleanup_registry();
    2253                 :            : 
    2254                 :          4 :         free_threads();
    2255                 :            : 
    2256                 :          4 :         return num_failures;
    2257                 :            : }

Generated by: LCOV version 1.15