Debian Codegraph @

test/ioctl/mock.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <sys/ioctl.h>
8 #include <dlfcn.h>
9
10 #include <nvme/ioctl.h>
11
12 #include "mock.h"
13 #include "util.h"
14
15 struct mock_cmds {
16 const char *name;
17 const struct mock_cmd *cmds;
18 size_t remaining_cmds;
19 };
20
21 static int mock_fd = -1;
22 static struct mock_cmds mock_admin_cmds = {.name = "admin"};
23 static struct mock_cmds mock_io_cmds = {.name = "IO"};
24
25 static void set_mock_cmds(
26 struct mock_cmds *mock_cmds, const struct mock_cmd *cmds, size_t len)
27 {
28 mock_cmds->cmds = cmds;
29 mock_cmds->remaining_cmds = len;
30 }
31
32 static void mock_cmds_done(const struct mock_cmds *mock_cmds)
33 {
34 check(!mock_cmds->remaining_cmds,
35 "%zu %s commands not executed",
36 mock_cmds->remaining_cmds, mock_cmds->name);
37 }
38
39 void set_mock_fd(int fd)
40 {
41 mock_fd = fd;
42 }
43
44 void set_mock_admin_cmds(const struct mock_cmd *cmds, size_t len)
45 {
46 set_mock_cmds(&mock_admin_cmds, cmds, len);
47 }
48
49 void set_mock_io_cmds(const struct mock_cmd *cmds, size_t len)
50 {
51 set_mock_cmds(&mock_io_cmds, cmds, len);
52 }
53
54 void end_mock_cmds(void)
55 {
56 mock_cmds_done(&mock_admin_cmds);
57 mock_cmds_done(&mock_io_cmds);
58 }
59
60 #define execute_ioctl(cmd, mock_cmd) ({ \
61 check((cmd)->opcode == (mock_cmd)->opcode, \
62 "got opcode 0x%" PRIx8 ", expected 0x%" PRIx8, \
63 (cmd)->opcode, (mock_cmd)->opcode); \
64 check((cmd)->flags == (mock_cmd)->flags, \
65 "got flags 0x%" PRIx8 ", expected 0x%" PRIx8, \
66 (cmd)->flags, (mock_cmd)->flags); \
67 check((cmd)->nsid == (mock_cmd)->nsid, \
68 "got nsid 0x%" PRIx32 ", expected 0x%" PRIx32, \
69 (cmd)->nsid, (mock_cmd)->nsid); \
70 check((cmd)->cdw2 == (mock_cmd)->cdw2, \
71 "got cdw2 0x%" PRIx32 ", expected 0x%" PRIx32, \
72 (cmd)->cdw2, (mock_cmd)->cdw2); \
73 check((cmd)->cdw3 == (mock_cmd)->cdw3, \
74 "got cdw3 0x%" PRIx32 ", expected 0x%" PRIx32, \
75 (cmd)->cdw3, (mock_cmd)->cdw3); \
76 check((cmd)->metadata_len == (mock_cmd)->metadata_len, \
77 "got metadata_len %" PRIu32 ", expected %" PRIu32, \
78 (cmd)->metadata_len, (mock_cmd)->metadata_len); \
79 if ((cmd)->metadata_len) { \
80 cmp((void const *)(uintptr_t)(cmd)->metadata, \
81 (mock_cmd)->metadata, \
82 (cmd)->metadata_len, \
83 "incorrect metadata"); \
84 } \
85 __u32 data_len = (cmd)->data_len; \
86 check(data_len == (mock_cmd)->data_len, \
87 "got data_len %" PRIu32 ", expected %" PRIu32, \
88 data_len, (mock_cmd)->data_len); \
89 void *data = (void *)(uintptr_t)(cmd)->addr; \
90 if ((mock_cmd)->in_data) { \
91 cmp(data, (mock_cmd)->in_data, data_len, "incorrect data"); \
92 } \
93 check((cmd)->cdw10 == (mock_cmd)->cdw10, \
94 "got cdw10 0x%" PRIx32 ", expected 0x%" PRIx32, \
95 (cmd)->cdw10, (mock_cmd)->cdw10); \
96 check((cmd)->cdw11 == (mock_cmd)->cdw11, \
97 "got cdw11 0x%" PRIx32 ", expected 0x%" PRIx32, \
98 (cmd)->cdw11, (mock_cmd)->cdw11); \
99 check((cmd)->cdw12 == (mock_cmd)->cdw12, \
100 "got cdw12 0x%" PRIx32 ", expected 0x%" PRIx32, \
101 (cmd)->cdw12, (mock_cmd)->cdw12); \
102 check((cmd)->cdw13 == (mock_cmd)->cdw13, \
103 "got cdw13 0x%" PRIx32 ", expected 0x%" PRIx32, \
104 (cmd)->cdw13, (mock_cmd)->cdw13); \
105 check((cmd)->cdw14 == (mock_cmd)->cdw14, \
106 "got cdw14 0x%" PRIx32 ", expected 0x%" PRIx32, \
107 (cmd)->cdw14, (mock_cmd)->cdw14); \
108 check((cmd)->cdw15 == (mock_cmd)->cdw15, \
109 "got cdw15 0x%" PRIx32 ", expected 0x%" PRIx32, \
110 (cmd)->cdw15, (mock_cmd)->cdw15); \
111 check((cmd)->timeout_ms == (mock_cmd)->timeout_ms, \
112 "got timeout_ms %" PRIu32 ", expected %" PRIu32, \
113 (cmd)->timeout_ms, (mock_cmd)->timeout_ms); \
114 (cmd)->result = (mock_cmd)->result; \
115 const void *out_data = (mock_cmd)->out_data; \
116 if (out_data) { \
117 memcpy(data, out_data, (mock_cmd)->out_data_len ?: data_len); \
118 } \
119 })
120
121 #if defined(HAVE_GLIBC_IOCTL) && HAVE_GLIBC_IOCTL == 1
122 typedef int (*ioctl_func_t)(int, unsigned long, void *);
123 int ioctl(int fd, unsigned long request, ...)
124 #else
125 typedef int (*ioctl_func_t)(int, int, void *);
126 int ioctl(int fd, int request, ...)
127 #endif
128 {
129 ioctl_func_t real_ioctl = NULL;
130 struct mock_cmds *mock_cmds;
131 bool result64;
132 const struct mock_cmd *mock_cmd;
133 va_list args;
134 void *cmd;
135
136 switch (request) {
137 case NVME_IOCTL_ADMIN_CMD:
138 mock_cmds = &mock_admin_cmds;
139 result64 = false;
140 break;
141 case NVME_IOCTL_ADMIN64_CMD:
142 mock_cmds = &mock_admin_cmds;
143 result64 = true;
144 break;
145 case NVME_IOCTL_IO_CMD:
146 mock_cmds = &mock_io_cmds;
147 result64 = false;
148 break;
149 case NVME_IOCTL_IO64_CMD:
150 mock_cmds = &mock_io_cmds;
151 result64 = true;
152 break;
153 default:
154 #if HAVE_LIBC_LDSYM
155 real_ioctl = dlsym(RTLD_NEXT, "ioctl");
156 if (!real_ioctl)
157 fail("Error: dlsym failed to find original ioctl\n");
158 #else
159 fail("Error: unhandled ioctl\n");
160 #endif
161 }
162
163 va_start(args, request);
164 cmd = va_arg(args, void *);
165 va_end(args);
166
167 if (real_ioctl)
168 return real_ioctl(fd, request, cmd);
169
170 check(fd == mock_fd, "got fd %d, expected %d", fd, mock_fd);
171 check(mock_cmds->remaining_cmds,
172 "unexpected %s command", mock_cmds->name);
173 mock_cmd = mock_cmds->cmds++;
174 mock_cmds->remaining_cmds--;
175
176 if (result64) {
177 execute_ioctl((struct nvme_passthru_cmd64 *)cmd, mock_cmd);
178 } else {
179 check((uint32_t)mock_cmd->result == mock_cmd->result,
180 "expected 64-bit %s for result %" PRIu64,
181 __func__, mock_cmd->result);
182 execute_ioctl((struct nvme_passthru_cmd *)cmd, mock_cmd);
183 }
184 if (mock_cmd->err < 0) {
185 errno = -mock_cmd->err;
186 return -1;
187 }
188
189 return mock_cmd->err;
190 }
191
192 /* mock io_uring_get_probe, just fail */
193 struct io_uring_probe *io_uring_get_probe(void)
194 {
195 return 0;
196 }