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: 1258 1327 94.8 %
Date: 2024-07-12 16:34:31 Functions: 56 105 53.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 285 612 46.6 %

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

Generated by: LCOV version 1.14