LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/nvme/nvme_ctrlr_ocssd_cmd.c - nvme_ctrlr_ocssd_cmd_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 56 58 96.6 %
Date: 2024-07-10 18:23:29 Functions: 12 20 60.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 15 78 19.2 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk_internal/cunit.h"
       7                 :            : #include "common/lib/test_env.c"
       8                 :            : 
       9                 :            : #include "nvme/nvme_ctrlr_ocssd_cmd.c"
      10                 :            : 
      11         [ -  + ]:          6 : DEFINE_STUB(spdk_nvme_ctrlr_get_first_active_ns, uint32_t,
      12                 :            :             (struct spdk_nvme_ctrlr *ctrlr), 1);
      13                 :            : 
      14                 :            : #define DECLARE_AND_CONSTRUCT_CTRLR()   \
      15                 :            :         struct spdk_nvme_ctrlr  ctrlr = {};     \
      16                 :            :         struct spdk_nvme_qpair  adminq = {};    \
      17                 :            :         struct nvme_request     req;            \
      18                 :            :                                                 \
      19                 :            :         STAILQ_INIT(&adminq.free_req);              \
      20                 :            :         STAILQ_INSERT_HEAD(&adminq.free_req, &req, stailq);     \
      21                 :            :         ctrlr.adminq = &adminq;     \
      22                 :            :         CU_ASSERT(pthread_mutex_init(&ctrlr.ctrlr_lock, NULL) == 0);
      23                 :            : 
      24                 :            : #define DECONSTRUCT_CTRLR() \
      25                 :            :         CU_ASSERT(pthread_mutex_destroy(&ctrlr.ctrlr_lock) == 0);
      26                 :            : 
      27                 :            : pid_t g_spdk_nvme_pid;
      28                 :            : struct nvme_request g_req;
      29                 :            : typedef void (*verify_request_fn_t)(struct nvme_request *req);
      30                 :            : verify_request_fn_t verify_fn;
      31                 :            : 
      32                 :            : static const uint32_t expected_geometry_ns = 1;
      33                 :            : 
      34                 :            : static int
      35                 :          6 : nvme_ns_cmp(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
      36                 :            : {
      37                 :          6 :         return ns1->id - ns2->id;
      38                 :            : }
      39                 :            : 
      40   [ -  +  -  +  :         18 : RB_GENERATE_STATIC(nvme_ns_tree, spdk_nvme_ns, node, nvme_ns_cmp);
          +  +  +  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
      41                 :            : 
      42                 :            : static struct spdk_nvme_ns g_inactive_ns = {};
      43                 :            : 
      44                 :            : struct spdk_nvme_ns *
      45                 :          6 : spdk_nvme_ctrlr_get_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid)
      46                 :            : {
      47                 :          5 :         struct spdk_nvme_ns tmp;
      48                 :            :         struct spdk_nvme_ns *ns;
      49                 :            : 
      50   [ +  -  -  + ]:          6 :         if (nsid < 1 || nsid > ctrlr->cdata.nn) {
      51                 :          0 :                 return NULL;
      52                 :            :         }
      53                 :            : 
      54                 :          6 :         tmp.id = nsid;
      55                 :          6 :         ns = RB_FIND(nvme_ns_tree, &ctrlr->ns, &tmp);
      56                 :            : 
      57         [ -  + ]:          6 :         if (ns == NULL) {
      58                 :          0 :                 return &g_inactive_ns;
      59                 :            :         }
      60                 :            : 
      61                 :          6 :         return ns;
      62                 :            : }
      63                 :            : 
      64                 :            : int
      65                 :          6 : nvme_ctrlr_submit_admin_request(struct spdk_nvme_ctrlr *ctrlr, struct nvme_request *req)
      66                 :            : {
      67                 :          6 :         verify_fn(req);
      68         [ -  + ]:          6 :         memset(req, 0, sizeof(*req));
      69                 :          6 :         return 0;
      70                 :            : }
      71                 :            : 
      72                 :            : struct nvme_request *
      73                 :          6 : nvme_allocate_request_user_copy(struct spdk_nvme_qpair *qpair, void *buffer, uint32_t payload_size,
      74                 :            :                                 spdk_nvme_cmd_cb cb_fn, void *cb_arg, bool host_to_controller)
      75                 :            : {
      76                 :            :         /* For the unit test, we don't actually need to copy the buffer */
      77                 :          6 :         return nvme_allocate_request_contig(qpair, buffer, payload_size, cb_fn, cb_arg);
      78                 :            : }
      79                 :            : 
      80                 :            : static void
      81                 :          6 : verify_geometry_cmd(struct nvme_request *req)
      82                 :            : {
      83                 :          6 :         CU_ASSERT(req->cmd.opc == SPDK_OCSSD_OPC_GEOMETRY);
      84                 :          6 :         CU_ASSERT(req->cmd.nsid == expected_geometry_ns);
      85                 :          6 : }
      86                 :            : 
      87                 :            : static void
      88                 :          6 : test_geometry_cmd(void)
      89                 :            : {
      90   [ +  -  -  + ]:          6 :         DECLARE_AND_CONSTRUCT_CTRLR();
      91                 :            : 
      92                 :          5 :         struct spdk_ocssd_geometry_data geo;
      93                 :            : 
      94                 :          6 :         verify_fn = verify_geometry_cmd;
      95                 :            : 
      96                 :          6 :         spdk_nvme_ocssd_ctrlr_cmd_geometry(&ctrlr, expected_geometry_ns, &geo,
      97                 :            :                                            sizeof(geo), NULL, NULL);
      98                 :            : 
      99         [ -  + ]:          6 :         DECONSTRUCT_CTRLR();
     100                 :          6 : }
     101                 :            : 
     102                 :            : static void
     103                 :          6 : test_spdk_nvme_ctrlr_is_ocssd_supported(void)
     104                 :            : {
     105                 :          6 :         struct spdk_nvme_ctrlr ctrlr = {};
     106                 :          6 :         struct spdk_nvme_ns ns = {};
     107                 :            :         bool rc;
     108                 :            : 
     109                 :          6 :         RB_INIT(&ctrlr.ns);
     110                 :          6 :         ns.id = 1;
     111                 :          6 :         RB_INSERT(nvme_ns_tree, &ctrlr.ns, &ns);
     112                 :            : 
     113                 :          6 :         ns.nsdata.vendor_specific[0] = 1;
     114                 :          6 :         ctrlr.quirks |= NVME_QUIRK_OCSSD;
     115                 :          6 :         ctrlr.cdata.vid = SPDK_PCI_VID_CNEXLABS;
     116                 :          6 :         ctrlr.cdata.nn = 1;
     117                 :            : 
     118                 :          6 :         rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
     119                 :          6 :         CU_ASSERT(rc == true);
     120                 :            : 
     121                 :            :         /* Clear quirks`s ocssd flag. */
     122                 :          6 :         ctrlr.quirks = 0;
     123                 :            : 
     124                 :          6 :         rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
     125                 :          6 :         CU_ASSERT(rc == false);
     126                 :            : 
     127                 :            :         /* NS count is 0. */
     128                 :          6 :         ctrlr.cdata.nn = 0;
     129                 :            : 
     130                 :          6 :         rc = spdk_nvme_ctrlr_is_ocssd_supported(&ctrlr);
     131                 :          6 :         CU_ASSERT(rc == false);
     132                 :          6 : }
     133                 :            : 
     134                 :            : int
     135                 :          6 : main(int argc, char **argv)
     136                 :            : {
     137                 :          6 :         CU_pSuite       suite = NULL;
     138                 :            :         unsigned int    num_failures;
     139                 :            : 
     140                 :          6 :         CU_initialize_registry();
     141                 :            : 
     142                 :          6 :         suite = CU_add_suite("nvme_ctrlr_cmd", NULL, NULL);
     143                 :            : 
     144                 :          6 :         CU_ADD_TEST(suite, test_geometry_cmd);
     145                 :          6 :         CU_ADD_TEST(suite, test_spdk_nvme_ctrlr_is_ocssd_supported);
     146                 :            : 
     147                 :          6 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     148                 :          6 :         CU_cleanup_registry();
     149                 :          6 :         return num_failures;
     150                 :            : }

Generated by: LCOV version 1.14