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

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

Generated by: LCOV version 1.15