Skip to content

Commit b003fe7

Browse files
committed
mi: Introduce asynchronous event message handling
Added new functionality to mi.c and mi-mctp.c to handle AEMs. Included new example mi-mctp-ae.c for usage. Signed-off-by: Chuck Horkin <[email protected]>
1 parent 8ad28af commit b003fe7

9 files changed

Lines changed: 1521 additions & 46 deletions

File tree

examples/meson.build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ executable(
4040
include_directories: [incdir, internal_incdir]
4141
)
4242

43+
executable(
44+
'mi-mctp-ae',
45+
['mi-mctp-ae.c'],
46+
dependencies: libnvme_mi_dep,
47+
include_directories: [incdir, internal_incdir]
48+
)
49+
4350
if libdbus_dep.found()
4451
executable(
4552
'mi-conf',

examples/mi-mctp-ae.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/**
3+
* This file is part of libnvme.
4+
*/
5+
6+
/**
7+
* mi-mctp-ae: open a MI connection over MCTP, supporting asynchronous event messages
8+
*/
9+
10+
#include <assert.h>
11+
#include <ctype.h>
12+
#include <err.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <stddef.h>
16+
#include <string.h>
17+
#include <errno.h>
18+
#include <unistd.h> // for usleep
19+
20+
#include <libnvme-mi.h>
21+
#include <poll.h>
22+
23+
#include <ccan/array_size/array_size.h>
24+
#include <ccan/endian/endian.h>
25+
#include <sys/select.h>
26+
27+
// Function to print the byte array
28+
static void print_byte_array(void *data, size_t len)
29+
{
30+
uint8_t *byte_data = (uint8_t *)data;
31+
32+
for (size_t i = 0; i < len; ++i)
33+
printf("%02X ", byte_data[i]);
34+
printf("\n");
35+
}
36+
37+
static void print_event_info(struct nvme_mi_event *event)
38+
{
39+
printf("aeoi: %02X\n", event->aeoi);
40+
printf("aeocidi: %04X\n", event->aeocidi);
41+
printf("aessi: %02X\n", event->aessi);
42+
43+
printf("specific_info: ");
44+
if (event->spec_info_len && event->spec_info)
45+
print_byte_array(event->spec_info, event->spec_info_len);
46+
else
47+
printf("EMPTY\n");
48+
49+
printf("vendor_specific_info: ");
50+
if (event->vend_spec_info_len && event->vend_spec_info)
51+
print_byte_array(event->vend_spec_info, event->vend_spec_info_len);
52+
else
53+
printf("EMPTY\n");
54+
}
55+
56+
enum nvme_mi_aem_handler_next_action aem_handler(nvme_mi_ep_t ep, size_t num_events, void *userdata)
57+
{
58+
uint32_t *count = (uint32_t *) userdata;
59+
*count = *count+1;
60+
61+
printf("Received notification #%d with %zu events:\n", *count, num_events);
62+
for (int i = 0; i < num_events; i++) {
63+
struct nvme_mi_event *event = nvme_mi_aem_get_next_event(ep);
64+
65+
if (event == NULL)
66+
printf("Unexpected NULL event\n");
67+
else {
68+
printf("Event:\n");
69+
print_event_info(event);
70+
printf("\n");
71+
}
72+
}
73+
74+
return NVME_MI_AEM_HNA_ACK;
75+
}
76+
77+
int main(int argc, char **argv)
78+
{
79+
nvme_root_t root;
80+
nvme_mi_ep_t ep;
81+
bool usage = true;
82+
uint8_t eid = 0;
83+
int rc = 0, net = 0;
84+
struct nvme_mi_aem_callbacks aem_cb_info = {0};
85+
uint32_t notification_counter = 0;
86+
bool enabled[256] = {0};
87+
88+
const uint8_t AEM_FD_INDEX = 0;
89+
const uint8_t STD_IN_FD_INDEX = 1;
90+
91+
if (argc > 3) {
92+
usage = false;
93+
net = atoi(argv[1]);
94+
eid = atoi(argv[2]) & 0xff;
95+
argv += 2;
96+
argc -= 2;
97+
98+
int event_count = argc - 1;
99+
100+
for (int i = 0; i < event_count; i++) {
101+
int event = atoi(argv[1+i]);
102+
103+
aem_cb_info.enabled[event] = true;
104+
}
105+
}
106+
107+
if (usage) {
108+
fprintf(stderr,
109+
"usage: %s <net> <eid> [AE #s separated by spaces]\n",
110+
argv[0]);
111+
return EXIT_FAILURE;
112+
}
113+
114+
root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL);
115+
if (!root)
116+
err(EXIT_FAILURE, "can't create NVMe root");
117+
118+
ep = nvme_mi_open_mctp(root, net, eid);
119+
if (!ep)
120+
errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid);
121+
122+
aem_cb_info.aem_handler = aem_handler;
123+
124+
rc = nvme_mi_aem_get_enabled(ep, enabled, sizeof(enabled));
125+
if (rc)
126+
errx(EXIT_FAILURE, "Can't query enabled aems:%d (%d)", rc, errno);
127+
printf("The following events are now enabled:\n");
128+
for (int i = 0; i < sizeof(enabled); i++) {
129+
if (enabled[i])
130+
printf("Event: %d\n", i);
131+
}
132+
133+
rc = nvme_mi_aem_enable(ep, true, true, true, 1, 100, &aem_cb_info, &notification_counter);
134+
if (rc)
135+
errx(EXIT_FAILURE, "Can't enable aem:%d (%d)", rc, errno);
136+
137+
rc = nvme_mi_aem_get_enabled(ep, enabled, sizeof(enabled));
138+
if (rc)
139+
errx(EXIT_FAILURE, "Can't query enabled aems:%d (%d)", rc, errno);
140+
141+
142+
143+
struct pollfd fds[2];
144+
145+
fds[AEM_FD_INDEX].fd = nvme_mi_aem_get_fd(ep);
146+
if (fds[AEM_FD_INDEX].fd < 0)
147+
errx(EXIT_FAILURE, "Can't get aem fd\n");
148+
149+
fds[STD_IN_FD_INDEX].fd = STDIN_FILENO;
150+
151+
fds[AEM_FD_INDEX].events = POLLIN;
152+
fds[STD_IN_FD_INDEX].events = POLLIN;
153+
154+
printf("Press any key to exit\n");
155+
while (1) {
156+
int poll_timeout = 500; // Timeout in milliseconds
157+
158+
rc = poll(fds, 2, poll_timeout);
159+
160+
if (rc == -1) {
161+
perror("poll");
162+
break;
163+
} else if (rc == 0) {
164+
//printf("No data within %d milliseconds.\n", timeout);
165+
} else {
166+
//Time to do the work
167+
if (fds[AEM_FD_INDEX].revents & POLLIN) {
168+
rc = nvme_mi_aem_process(ep, &notification_counter);
169+
if (rc)
170+
errx(EXIT_FAILURE,
171+
"nvme_mi_aem_process failed with:%d (%d)",
172+
rc,
173+
errno);
174+
}
175+
if (fds[STD_IN_FD_INDEX].revents & POLLIN)
176+
break;//we are done
177+
}
178+
}
179+
180+
//Cleanup
181+
nvme_mi_aem_disable(ep);
182+
nvme_mi_close(ep);
183+
nvme_mi_free_root(root);
184+
185+
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
186+
}
187+
188+

src/libnvme-mi.map

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
LIBNVME_MI_1_12 {
22
global:
33
nvme_mi_mi_xfer;
4+
nvme_mi_aem_get_fd;
5+
nvme_mi_aem_enable;
6+
nvme_mi_aem_process;
7+
nvme_mi_aem_disable;
8+
nvme_mi_aem_get_next_event;
9+
nvme_mi_aem_get_enabled;
410
};
511

612
LIBNVME_MI_1_11 {

0 commit comments

Comments
 (0)