Debian Codegraph @

examples/mi-mctp-csi-test.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 */
5
6 /**
7 * mi-mctp-csi-test: open a MI connection over MCTP, and send two commands
8 * in parallel with different CSI buffers
9 */
10
11 #include <assert.h>
12 #include <ctype.h>
13 #include <err.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <pthread.h>
19
20 #include <libnvme-mi.h>
21
22 #include <ccan/array_size/array_size.h>
23 #include <ccan/endian/endian.h>
24
25 void fhexdump(FILE *fp, const unsigned char *buf, int len)
26 {
27 const int row_len = 16;
28 int i, j;
29
30 for (i = 0; i < len; i += row_len) {
31 char hbuf[row_len * strlen("00 ") + 1];
32 char cbuf[row_len + strlen("|") + 1];
33
34 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
35 unsigned char c = buf[i + j];
36
37 sprintf(hbuf + j * 3, "%02x ", c);
38
39 if (!isprint(c))
40 c = '.';
41
42 sprintf(cbuf + j, "%c", c);
43 }
44
45 strcat(cbuf, "|");
46
47 fprintf(fp, "%08x %*s |%s\n", i,
48 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
49 }
50 }
51
52 void hexdump(const unsigned char *buf, int len)
53 {
54 fhexdump(stdout, buf, len);
55 }
56
57 int do_get_log_page(nvme_mi_ep_t ep, int argc, char **argv)
58 {
59 struct nvme_get_log_args args = { 0 };
60 struct nvme_mi_ctrl *ctrl;
61 uint8_t buf[4096];
62 uint16_t ctrl_id;
63 int rc, tmp;
64
65 if (argc < 2) {
66 fprintf(stderr, "no controller ID specified\n");
67 return -1;
68 }
69
70 tmp = atoi(argv[1]);
71 if (tmp < 0 || tmp > 0xffff) {
72 fprintf(stderr, "invalid controller ID\n");
73 return -1;
74 }
75
76 ctrl_id = tmp & 0xffff;
77
78 args.args_size = sizeof(args);
79 args.log = buf;
80 args.len = sizeof(buf);
81
82 if (argc > 2) {
83 tmp = atoi(argv[2]);
84 args.lid = tmp & 0xff;
85 } else {
86 args.lid = 0x1;
87 }
88
89 ctrl = nvme_mi_init_ctrl(ep, ctrl_id);
90 if (!ctrl) {
91 warn("can't create controller");
92 return -1;
93 }
94
95 rc = nvme_mi_admin_get_log(ctrl, &args);
96 if (rc) {
97 warn("can't perform Get Log page command");
98 return -1;
99 }
100
101 printf("Get log page (log id = 0x%02x) data:\n", args.lid);
102 hexdump(buf, args.len);
103
104 return 0;
105 }
106
107 struct thread_struct {
108 nvme_mi_ep_t ep;
109 int argc;
110 char **argv;
111 int rc;
112 };
113
114 void *csi_thread_helper(void *context)
115 {
116 struct thread_struct *s = (struct thread_struct *) context;
117
118 s->rc = do_get_log_page(s->ep, s->argc, s->argv);
119 return NULL;
120 }
121
122 enum action {
123 ACTION_CSI_TEST,
124 };
125
126 int do_csi_test(nvme_root_t root, int net, __u8 eid,
127 int argc, char **argv)
128 {
129 int rc = 0;
130 nvme_mi_ep_t ep1, ep2;
131
132 ep1 = nvme_mi_open_mctp(root, net, eid);
133 if (!ep1)
134 errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
135 ep2 = nvme_mi_open_mctp(root, net, eid);
136 if (!ep2)
137 errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
138
139 pthread_t thread;
140
141 nvme_mi_set_csi(ep1, 0);//Not necessary, but to be explicit
142 nvme_mi_set_csi(ep2, 1);
143 struct thread_struct s;
144
145 s.ep = ep2;
146 s.argc = argc;
147 s.argv = argv;
148
149 // Create a new thread to run my_function
150 if (pthread_create(&thread, NULL, csi_thread_helper, &s)) {
151 fprintf(stderr, "Error creating thread\n");
152 return 1;
153 }
154
155 rc = do_get_log_page(ep1, argc, argv);
156
157 // Main thread continues to do other work
158 printf("Main thread finished with rc=%d\n", rc);
159
160 // Wait for the created thread to finish
161 if (pthread_join(thread, NULL)) {
162 fprintf(stderr, "Error joining thread\n");
163 return 2;
164 }
165
166 printf("Second thread finished with rc=%d\n", s.rc);
167
168 nvme_mi_close(ep1);
169 nvme_mi_close(ep2);
170
171 if (rc)
172 return rc;
173 if (s.rc)
174 return s.rc;
175 return 0;
176 }
177
178 static int do_action_endpoint(enum action action,
179 nvme_root_t root,
180 int net,
181 uint8_t eid,
182 int argc,
183 char **argv)
184 {
185 int rc;
186
187 switch (action) {
188 case ACTION_CSI_TEST:
189 rc = do_csi_test(root, net, eid, argc, argv);
190 break;
191 default:
192 /* This shouldn't be possible, as we should be covering all
193 * of the enum action options above. Hoever, keep the compilers
194 * happy and fail gracefully.
195 */
196 fprintf(stderr, "invalid action %d?\n", action);
197 rc = -1;
198 }
199 return rc;
200 }
201
202 int main(int argc, char **argv)
203 {
204 enum action action;
205 nvme_root_t root;
206 bool usage = true;
207 uint8_t eid = 0;
208 int rc = 0, net = 0;
209
210 if (argc >= 5) {
211 usage = false;
212 net = atoi(argv[1]);
213 eid = atoi(argv[2]) & 0xff;
214 argv += 2;
215 argc -= 2;
216 }
217
218 if (usage) {
219 fprintf(stderr,
220 "usage: %s <net> <eid> [action] [action args]\n",
221 argv[0]);
222 fprintf(stderr, "where action is:\n"
223 " csi-test <controller-id> [<log-id>]\n"
224 "\n"
225 );
226 return EXIT_FAILURE;
227 }
228
229 char *action_str = argv[1];
230
231 argc--;
232 argv++;
233
234 if (!strcmp(action_str, "csi-test")) {
235 action = ACTION_CSI_TEST;
236 } else {
237 fprintf(stderr, "invalid action '%s'\n", action_str);
238 return EXIT_FAILURE;
239 }
240
241 root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL);
242 if (!root)
243 err(EXIT_FAILURE, "can't create NVMe root");
244
245 rc = do_action_endpoint(action, root, net, eid, argc, argv);
246 nvme_mi_free_root(root);
247
248 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
249 }
250
251