summaryrefslogtreecommitdiffstats
path: root/src/spdk/test/nvme/cuse/cuse.c
blob: fe5c26f0c313dae5971a1ad2b54769bcb8fb1b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "spdk_cunit.h"

#include "common/lib/test_env.c"
#include "nvme/nvme_cuse.c"

DEFINE_STUB(nvme_io_msg_send, int, (struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid,
				    spdk_nvme_io_msg_fn fn, void *arg), 0);

DEFINE_STUB(spdk_nvme_ctrlr_alloc_cmb_io_buffer, void *, (struct spdk_nvme_ctrlr *ctrlr,
		size_t size), NULL);

DEFINE_STUB(spdk_nvme_ctrlr_cmd_admin_raw, int, (struct spdk_nvme_ctrlr *ctrlr,
		struct spdk_nvme_cmd *cmd, void *buf, uint32_t len,
		spdk_nvme_cmd_cb cb_fn, void *cb_arg), 0);

DEFINE_STUB(spdk_nvme_ctrlr_get_num_ns, uint32_t, (struct spdk_nvme_ctrlr *ctrlr), 128);

static uint32_t g_active_num_ns = 4;
static uint32_t g_active_nsid_min = 1;

bool
spdk_nvme_ctrlr_is_active_ns(struct spdk_nvme_ctrlr *ctrlr, uint32_t nsid)
{
	return nsid >= g_active_nsid_min && nsid < g_active_num_ns + g_active_nsid_min;
}

DEFINE_STUB(spdk_nvme_ctrlr_reset, int, (struct spdk_nvme_ctrlr *ctrlr), 0);

DEFINE_STUB(spdk_nvme_ns_cmd_read, int, (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair,
		void *payload,
		uint64_t lba, uint32_t lba_count, spdk_nvme_cmd_cb cb_fn, void *cb_arg,
		uint32_t io_flags), 0);

DEFINE_STUB(spdk_nvme_ns_cmd_write, int, (struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair,
		void *payload,
		uint64_t lba, uint32_t lba_count, spdk_nvme_cmd_cb cb_fn, void *cb_arg,
		uint32_t io_flags), 0);

DEFINE_STUB(spdk_nvme_ns_get_num_sectors, uint64_t, (struct spdk_nvme_ns *ns), 0);

DEFINE_STUB(spdk_nvme_ns_get_sector_size, uint32_t, (struct spdk_nvme_ns *ns), 0);

DEFINE_STUB_V(spdk_unaffinitize_thread, (void));

DEFINE_STUB(spdk_nvme_ctrlr_get_ns, struct spdk_nvme_ns *, (struct spdk_nvme_ctrlr *ctrlr,
		uint32_t nsid), NULL);

static bool
wait_for_file(char *filename, bool exists)
{
	int i;

	for (i = 0; i < 1000; i++) {
		if ((access(filename, F_OK) != -1) ^ (!exists)) {
			return true;
		}
		usleep(100);
	}
	return false;
}

static void
verify_devices(struct spdk_nvme_ctrlr *ctrlr)
{
	char ctrlr_name[256];
	size_t ctrlr_name_size;
	char ctrlr_dev[256], ns_dev[256 + 10];
	uint32_t nsid, num_ns;
	int rv;

	ctrlr_name_size = sizeof(ctrlr_name);
	rv = spdk_nvme_cuse_get_ctrlr_name(ctrlr, ctrlr_name, &ctrlr_name_size);
	SPDK_CU_ASSERT_FATAL(rv == 0);

	rv = snprintf(ctrlr_dev, sizeof(ctrlr_dev), "/dev/%s", ctrlr_name);
	CU_ASSERT(rv > 0);
	CU_ASSERT(wait_for_file(ctrlr_dev, true));

	num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr);

	for (nsid = 1; nsid <= num_ns; nsid++) {
		snprintf(ns_dev, sizeof(ns_dev), "%sn%" PRIu32, ctrlr_dev, nsid);
		if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nsid)) {
			CU_ASSERT(wait_for_file(ns_dev, true));
		} else {
			CU_ASSERT(wait_for_file(ns_dev, false));
		}
	}

	/* Next one should never exist */
	snprintf(ns_dev, sizeof(ns_dev), "%sn%" PRIu32, ctrlr_dev, nsid);
	CU_ASSERT(wait_for_file(ns_dev, false));
}

static void
test_cuse_update(void)
{
	int rc;
	struct spdk_nvme_ctrlr	ctrlr = {};

	rc = nvme_cuse_start(&ctrlr);
	CU_ASSERT(rc == 0);

	g_active_num_ns = 4;
	g_active_nsid_min = 1;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 0;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 4;
	g_active_nsid_min = spdk_nvme_ctrlr_get_num_ns(&ctrlr) - g_active_num_ns;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 2;
	g_active_nsid_min = 2;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 10;
	g_active_nsid_min = 5;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 5;
	g_active_nsid_min = 3;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	g_active_num_ns = 6;
	g_active_nsid_min = 1;
	nvme_cuse_update(&ctrlr);
	verify_devices(&ctrlr);

	nvme_cuse_stop(&ctrlr);
}

int main(int argc, char **argv)
{
	CU_pSuite	suite = NULL;
	unsigned int	num_failures;

	CU_set_error_action(CUEA_ABORT);
	CU_initialize_registry();
	suite = CU_add_suite("nvme_cuse", NULL, NULL);
	CU_ADD_TEST(suite, test_cuse_update);
	CU_basic_set_mode(CU_BRM_VERBOSE);
	CU_basic_run_tests();
	num_failures = CU_get_number_of_failures();
	CU_cleanup_registry();
	return num_failures;
}