Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2021 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2019, 2021 Mellanox Technologies LTD. All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk_internal/cunit.h"
8 : : #include "common/lib/test_env.c"
9 : : #include "nvmf/nvmf.c"
10 : : #include "spdk/bdev_module.h"
11 : :
12 : 5 : DEFINE_STUB_V(nvmf_transport_poll_group_destroy, (struct spdk_nvmf_transport_poll_group *group));
13 : 0 : DEFINE_STUB_V(nvmf_ctrlr_destruct, (struct spdk_nvmf_ctrlr *ctrlr));
14 : 0 : DEFINE_STUB_V(nvmf_transport_qpair_fini, (struct spdk_nvmf_qpair *qpair,
15 : : spdk_nvmf_transport_qpair_fini_cb cb_fn,
16 : : void *cb_arg));
17 : 0 : DEFINE_STUB_V(nvmf_qpair_free_aer, (struct spdk_nvmf_qpair *qpair));
18 : 0 : DEFINE_STUB_V(nvmf_qpair_abort_pending_zcopy_reqs, (struct spdk_nvmf_qpair *qpair));
19 [ - + ]: 5 : DEFINE_STUB(nvmf_transport_poll_group_create, struct spdk_nvmf_transport_poll_group *,
20 : : (struct spdk_nvmf_transport *transport,
21 : : struct spdk_nvmf_poll_group *group), NULL);
22 [ - + ]: 5 : DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_desc *desc),
23 : : NULL);
24 [ # # ]: 0 : DEFINE_STUB(nvmf_ctrlr_async_event_ns_notice, int, (struct spdk_nvmf_ctrlr *ctrlr), 0);
25 [ # # ]: 0 : DEFINE_STUB(nvmf_ctrlr_async_event_ana_change_notice, int,
26 : : (struct spdk_nvmf_ctrlr *ctrlr), 0);
27 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_poll_group_remove, int, (struct spdk_nvmf_transport_poll_group *group,
28 : : struct spdk_nvmf_qpair *qpair), 0);
29 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_req_free, int, (struct spdk_nvmf_request *req), 0);
30 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_poll_group_poll, int, (struct spdk_nvmf_transport_poll_group *group), 0);
31 : 0 : DEFINE_STUB_V(nvmf_subsystem_remove_all_listeners, (struct spdk_nvmf_subsystem *subsystem,
32 : : bool stop));
33 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_destroy, int, (struct spdk_nvmf_subsystem *subsystem,
34 : : nvmf_subsystem_destroy_cb cpl_cb, void *cpl_cb_arg), 0);
35 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_first_listener, struct spdk_nvmf_subsystem_listener *,
36 : : (struct spdk_nvmf_subsystem *subsystem), NULL);
37 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_next_listener, struct spdk_nvmf_subsystem_listener *,
38 : : (struct spdk_nvmf_subsystem *subsystem,
39 : : struct spdk_nvmf_subsystem_listener *prev_listener), NULL);
40 [ - + ]: 5 : DEFINE_STUB(spdk_nvmf_subsystem_get_next, struct spdk_nvmf_subsystem *,
41 : : (struct spdk_nvmf_subsystem *subsystem), NULL);
42 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_nqn, const char *,
43 : : (const struct spdk_nvmf_subsystem *subsystem), NULL);
44 [ # # # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_allow_any_host, bool,
45 : : (const struct spdk_nvmf_subsystem *subsystem), true);
46 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_sn, const char *,
47 : : (const struct spdk_nvmf_subsystem *subsystem),
48 : : NULL);
49 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_mn, const char *,
50 : : (const struct spdk_nvmf_subsystem *subsystem),
51 : : NULL);
52 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_max_namespaces, uint32_t,
53 : : (const struct spdk_nvmf_subsystem *subsystem), 0);
54 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_min_cntlid, uint16_t,
55 : : (const struct spdk_nvmf_subsystem *subsystem), 0);
56 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_max_cntlid, uint16_t,
57 : : (const struct spdk_nvmf_subsystem *subsystem), 0);
58 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_listener_get_trid, const struct spdk_nvme_transport_id *,
59 : : (struct spdk_nvmf_subsystem_listener *listener), NULL);
60 [ # # ]: 0 : DEFINE_STUB(spdk_nvme_transport_id_adrfam_str, const char *, (enum spdk_nvmf_adrfam adrfam), NULL);
61 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_first_host, struct spdk_nvmf_host *,
62 : : (struct spdk_nvmf_subsystem *subsystem), 0);
63 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_host_get_nqn, const char *, (const struct spdk_nvmf_host *host), NULL);
64 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_next_host, struct spdk_nvmf_host *,
65 : : (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *prev_host), NULL);
66 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_first_ns, struct spdk_nvmf_ns *,
67 : : (struct spdk_nvmf_subsystem *subsystem), NULL);
68 [ # # # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_ana_reporting, bool, (struct spdk_nvmf_subsystem *subsystem),
69 : : false);
70 : 0 : DEFINE_STUB_V(spdk_nvmf_ns_get_opts, (const struct spdk_nvmf_ns *ns,
71 : : struct spdk_nvmf_ns_opts *opts, size_t opts_size));
72 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_ns_get_id, uint32_t, (const struct spdk_nvmf_ns *ns), 0);
73 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_ns_get_bdev, struct spdk_bdev *, (struct spdk_nvmf_ns *ns), NULL);
74 [ # # ]: 0 : DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), 0);
75 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_next_ns, struct spdk_nvmf_ns *,
76 : : (struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns), NULL);
77 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_transport_listen, int, (struct spdk_nvmf_transport *transport,
78 : : const struct spdk_nvme_transport_id *trid,
79 : : struct spdk_nvmf_listen_opts *opts), 0);
80 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_transport_stop_listen, int,
81 : : (struct spdk_nvmf_transport *transport,
82 : : const struct spdk_nvme_transport_id *trid), 0)
83 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_get_optimal_poll_group, struct spdk_nvmf_transport_poll_group *,
84 : : (struct spdk_nvmf_transport *transport, struct spdk_nvmf_qpair *qpair), NULL);
85 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_poll_group_add, int,
86 : : (struct spdk_nvmf_transport_poll_group *group,
87 : : struct spdk_nvmf_qpair *qpair), 0);
88 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_qpair_get_peer_trid, int,
89 : : (struct spdk_nvmf_qpair *qpair,
90 : : struct spdk_nvme_transport_id *trid), 0);
91 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_qpair_get_local_trid, int,
92 : : (struct spdk_nvmf_qpair *qpair,
93 : : struct spdk_nvme_transport_id *trid), 0);
94 [ # # ]: 0 : DEFINE_STUB(nvmf_transport_qpair_get_listen_trid, int,
95 : : (struct spdk_nvmf_qpair *qpair,
96 : : struct spdk_nvme_transport_id *trid), 0);
97 : 0 : DEFINE_STUB_V(spdk_nvmf_request_exec, (struct spdk_nvmf_request *req));
98 : 0 : DEFINE_STUB_V(spdk_nvmf_request_zcopy_start, (struct spdk_nvmf_request *req));
99 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_get_transport_name, const char *,
100 : : (struct spdk_nvmf_transport *transport), NULL);
101 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_transport_destroy, int, (struct spdk_nvmf_transport *transport,
102 : : spdk_nvmf_transport_destroy_done_cb cb_fn, void *cb_arg), 0);
103 [ # # ]: 0 : DEFINE_STUB(spdk_nvmf_subsystem_get_type, enum spdk_nvmf_subtype,
104 : : (struct spdk_nvmf_subsystem *subsystem), 0);
105 [ - + ]: 5 : DEFINE_STUB(spdk_nvmf_subsystem_get_first, struct spdk_nvmf_subsystem *,
106 : : (struct spdk_nvmf_tgt *tgt), NULL);
107 : 0 : DEFINE_STUB_V(nvmf_transport_dump_opts, (struct spdk_nvmf_transport *transport,
108 : : struct spdk_json_write_ctx *w, bool named));
109 : 0 : DEFINE_STUB_V(nvmf_transport_listen_dump_trid, (const struct spdk_nvme_transport_id *trid,
110 : : struct spdk_json_write_ctx *w));
111 : 0 : DEFINE_STUB_V(nvmf_transport_listen_dump_opts, (struct spdk_nvmf_transport *transport,
112 : : const struct spdk_nvme_transport_id *trid, struct spdk_json_write_ctx *w));
113 [ # # ]: 0 : DEFINE_STUB(spdk_nvme_transport_id_compare, int, (const struct spdk_nvme_transport_id *trid1,
114 : : const struct spdk_nvme_transport_id *trid2), 0);
115 : 0 : DEFINE_STUB_V(nvmf_update_discovery_log, (struct spdk_nvmf_tgt *tgt, const char *hostnqn));
116 [ # # # # ]: 0 : DEFINE_STUB(nvmf_nqn_is_valid, bool, (const char *nqn), true);
117 [ # # # # ]: 0 : DEFINE_STUB(nvmf_nqn_is_discovery, bool, (const char *nqn), true);
118 [ # # ]: 0 : DEFINE_STUB(spdk_key_get_name, const char *, (struct spdk_key *k), NULL);
119 [ # # ]: 0 : DEFINE_STUB(nvmf_qpair_auth_init, int, (struct spdk_nvmf_qpair *q), 0);
120 : 0 : DEFINE_STUB_V(nvmf_qpair_auth_destroy, (struct spdk_nvmf_qpair *q));
121 : :
122 : : struct spdk_io_channel {
123 : : struct spdk_thread *thread;
124 : : struct io_device *dev;
125 : : uint32_t ref;
126 : : uint32_t destroy_ref;
127 : : TAILQ_ENTRY(spdk_io_channel) tailq;
128 : : spdk_io_channel_destroy_cb destroy_cb;
129 : :
130 : : uint8_t _padding[48];
131 : : };
132 : :
133 : : uint64_t
134 : 5 : spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
135 : : {
136 : 5 : return bdev->blockcnt;
137 : : }
138 : :
139 : : const struct spdk_uuid *
140 : 5 : spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
141 : : {
142 : 5 : return &bdev->uuid;
143 : : }
144 : :
145 : : static void
146 : 5 : test_nvmf_tgt_create_poll_group(void)
147 : : {
148 : : int rc;
149 : 5 : struct spdk_thread *thread = NULL;
150 : 5 : struct spdk_nvmf_tgt tgt = {};
151 : 5 : struct spdk_nvmf_poll_group group = {};
152 : 5 : struct spdk_nvmf_transport transport = {};
153 : 5 : struct spdk_nvmf_subsystem subsystem = {};
154 : 5 : struct spdk_nvmf_ns ns = {};
155 : 5 : struct spdk_bdev bdev = {};
156 : 5 : struct spdk_io_channel ch = {};
157 : 5 : struct spdk_nvmf_transport_poll_group transport_pg = {};
158 : :
159 : 5 : thread = spdk_thread_create(NULL, NULL);
160 [ - + ]: 5 : SPDK_CU_ASSERT_FATAL(thread != NULL);
161 : 5 : spdk_set_thread(thread);
162 : :
163 : : /* Create group with single subsystem */
164 : 5 : ch.thread = thread;
165 : 5 : MOCK_SET(spdk_bdev_get_io_channel, &ch);
166 : :
167 : 5 : tgt.max_subsystems = 1;
168 : 5 : RB_INIT(&tgt.subsystems);
169 : :
170 : : /* Make sure subsystem has enough in subnqn so it can be
171 : : * inserted into RB-tree.
172 : : */
173 : 5 : snprintf(subsystem.subnqn, sizeof(subsystem.subnqn), "abc");
174 : 5 : RB_INSERT(subsystem_tree, &tgt.subsystems, &subsystem);
175 : 5 : subsystem.id = 0;
176 : 5 : subsystem.max_nsid = 1;
177 : 5 : subsystem.ns = calloc(1, sizeof(struct spdk_nvmf_ns *));
178 [ - + ]: 5 : SPDK_CU_ASSERT_FATAL(subsystem.ns != NULL);
179 : 5 : MOCK_SET(spdk_nvmf_subsystem_get_first, &subsystem);
180 : :
181 : 5 : subsystem.ns[0] = &ns;
182 : 5 : ns.crkey = 0xaa;
183 : 5 : ns.rtype = 0xbb;
184 : 5 : TAILQ_INIT(&ns.registrants);
185 : 5 : ns.bdev = &bdev;
186 : 5 : spdk_uuid_generate(&bdev.uuid);
187 : 5 : bdev.blockcnt = 512;
188 : :
189 : 5 : TAILQ_INIT(&tgt.transports);
190 : 5 : TAILQ_INIT(&tgt.poll_groups);
191 : 5 : tgt.num_poll_groups = 0;
192 : 5 : pthread_mutex_init(&tgt.mutex, NULL);
193 : 5 : transport.tgt = &tgt;
194 : 5 : TAILQ_INSERT_TAIL(&tgt.transports, &transport, link);
195 : :
196 : 5 : MOCK_SET(nvmf_transport_poll_group_create, &transport_pg);
197 : 5 : rc = nvmf_tgt_create_poll_group((void *)&tgt, (void *)&group);
198 : 5 : MOCK_SET(nvmf_transport_poll_group_create, NULL);
199 : 5 : CU_ASSERT(rc == 0);
200 : 5 : CU_ASSERT(group.num_sgroups == 1);
201 : 5 : CU_ASSERT(group.sgroups != NULL);
202 : 5 : CU_ASSERT(group.sgroups[0].state == SPDK_NVMF_SUBSYSTEM_ACTIVE);
203 : 5 : CU_ASSERT(group.sgroups[0].ns_info[0].channel == &ch);
204 [ - + ]: 5 : CU_ASSERT(!memcmp(&group.sgroups[0].ns_info[0].uuid, &bdev.uuid, 16));
205 : 5 : CU_ASSERT(group.sgroups[0].ns_info[0].num_blocks == 512);
206 : 5 : CU_ASSERT(group.sgroups[0].ns_info[0].crkey == 0xaa);
207 : 5 : CU_ASSERT(group.sgroups[0].ns_info[0].rtype == 0xbb);
208 : 5 : CU_ASSERT(TAILQ_FIRST(&tgt.poll_groups) == &group);
209 : 5 : CU_ASSERT(tgt.num_poll_groups == 1);
210 : 5 : CU_ASSERT(group.thread == thread);
211 : 5 : CU_ASSERT(group.poller != NULL);
212 : :
213 : 5 : nvmf_tgt_destroy_poll_group((void *)&tgt, (void *)&group);
214 : 5 : CU_ASSERT(TAILQ_EMPTY(&tgt.poll_groups));
215 : 5 : CU_ASSERT(tgt.num_poll_groups == 0);
216 : 5 : free(subsystem.ns);
217 [ - - - + ]: 5 : MOCK_CLEAR(spdk_nvmf_subsystem_get_first);
218 : :
219 : 5 : spdk_thread_exit(thread);
220 [ + + ]: 10 : while (!spdk_thread_is_exited(thread)) {
221 : 5 : spdk_thread_poll(thread, 0, 0);
222 : : }
223 : 5 : spdk_thread_destroy(thread);
224 [ - - - + ]: 5 : MOCK_CLEAR(spdk_bdev_get_io_channel);
225 : 5 : }
226 : :
227 : : int
228 : 5 : main(int argc, char **argv)
229 : : {
230 : 5 : CU_pSuite suite = NULL;
231 : : unsigned int num_failures;
232 : :
233 : 5 : CU_initialize_registry();
234 : :
235 : 5 : suite = CU_add_suite("nvmf", NULL, NULL);
236 : :
237 : 5 : CU_ADD_TEST(suite, test_nvmf_tgt_create_poll_group);
238 : :
239 : 5 : num_failures = spdk_ut_run_tests(argc, argv, NULL);
240 : 5 : CU_cleanup_registry();
241 : 5 : return num_failures;
242 : : }
|