Skip to content

Commit faf1ad6

Browse files
mkjjk-ozlabs
authored andcommitted
mi: provide a facility for transports to describe endpoints
This change adds a callback to struct nvme_mi_transport, allowing transports to way to describe the endpoint as a human-readable string. We call this in a new API function, nvme_mi_endpoint_desc(), which has a fallback implementation if no callback is provided. [split out from a previous patch by Jeremy Kerr, test added] Signed-off-by: Matt Johnston <[email protected]> Signed-off-by: Jeremy Kerr <[email protected]>
1 parent 2ed61af commit faf1ad6

6 files changed

Lines changed: 100 additions & 0 deletions

File tree

src/libnvme-mi.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LIBNVME_MI_1_1 {
1515
nvme_mi_admin_xfer;
1616
nvme_mi_admin_security_send;
1717
nvme_mi_admin_security_recv;
18+
nvme_mi_endpoint_desc;
1819
nvme_mi_open_mctp;
1920
local:
2021
*;

src/nvme/mi-mctp.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,26 @@ static void nvme_mi_mctp_close(struct nvme_mi_ep *ep)
194194
free(ep->transport_data);
195195
}
196196

197+
static int nvme_mi_mctp_desc_ep(struct nvme_mi_ep *ep, char *buf, size_t len)
198+
{
199+
struct nvme_mi_transport_mctp *mctp;
200+
201+
if (ep->transport != &nvme_mi_transport_mctp)
202+
return -1;
203+
204+
mctp = ep->transport_data;
205+
206+
snprintf(buf, len, "net %d eid %d", mctp->net, mctp->eid);
207+
208+
return 0;
209+
}
210+
197211
static const struct nvme_mi_transport nvme_mi_transport_mctp = {
198212
.name = "mctp",
199213
.mic_enabled = true,
200214
.submit = nvme_mi_mctp_submit,
201215
.close = nvme_mi_mctp_close,
216+
.desc_ep = nvme_mi_mctp_desc_ep,
202217
};
203218

204219
nvme_mi_ep_t nvme_mi_open_mctp(nvme_root_t root, unsigned int netid, __u8 eid)

src/nvme/mi.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <stdlib.h>
1111
#include <stdlib.h>
12+
#include <stdio.h>
1213

1314
#include <ccan/endian/endian.h>
1415

@@ -616,3 +617,33 @@ void nvme_mi_close_ctrl(nvme_mi_ctrl_t ctrl)
616617
{
617618
free(ctrl);
618619
}
620+
621+
char *nvme_mi_endpoint_desc(nvme_mi_ep_t ep)
622+
{
623+
char tsbuf[101], *s = NULL;
624+
size_t tslen;
625+
int rc;
626+
627+
rc = -1;
628+
memset(tsbuf, 0, sizeof(tsbuf));
629+
if (ep->transport->desc_ep)
630+
rc = ep->transport->desc_ep(ep, tsbuf, sizeof(tsbuf) - 1);
631+
632+
if (!rc) {
633+
/* don't overflow if the transport gives us an invalid string */
634+
tsbuf[sizeof(tsbuf)-1] = '\0';
635+
tslen = strlen(tsbuf);
636+
} else {
637+
tslen = 0;
638+
}
639+
640+
if (tslen)
641+
rc = asprintf(&s, "%s: %s", ep->transport->name, tsbuf);
642+
else
643+
rc = asprintf(&s, "%s endpoint", ep->transport->name);
644+
645+
if (rc < 0)
646+
return NULL;
647+
648+
return s;
649+
}

src/nvme/mi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ nvme_mi_ctrl_t nvme_mi_init_ctrl(nvme_mi_ep_t ep, __u16 ctrl_id);
337337
*/
338338
void nvme_mi_close_ctrl(nvme_mi_ctrl_t ctrl);
339339

340+
/**
341+
* nvme_mi_endpoint_desc - Get a string describing a MI endpoint.
342+
* @ep: endpoint to describe
343+
*
344+
* Generates a human-readable string describing the endpoint, with possibly
345+
* transport-specific data. The string is allocated during the call, and the
346+
* caller is responsible for free()-ing the string.
347+
*
348+
* Return: a newly-allocated string containing the endpoint description, or
349+
* NULL on failure.
350+
*/
351+
char *nvme_mi_endpoint_desc(nvme_mi_ep_t ep);
352+
340353
/* MI Command API: nvme_mi_mi_ prefix */
341354

342355
/**

src/nvme/private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ struct nvme_mi_transport {
180180
struct nvme_mi_req *req,
181181
struct nvme_mi_resp *resp);
182182
void (*close)(struct nvme_mi_ep *ep);
183+
int (*desc_ep)(struct nvme_mi_ep *ep, char *buf, size_t len);
183184
};
184185

185186
struct nvme_mi_ep {

test/mi.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef int (*test_submit_cb)(struct nvme_mi_ep *ep,
2424

2525
struct test_transport_data {
2626
unsigned int magic;
27+
bool named;
2728
test_submit_cb submit_cb;
2829
void *submit_cb_data;
2930
};
@@ -55,6 +56,21 @@ static void test_transport_close(struct nvme_mi_ep *ep)
5556
free(tpd);
5657
}
5758

59+
static int test_transport_desc_ep(struct nvme_mi_ep *ep,
60+
char *buf, size_t len)
61+
{
62+
struct test_transport_data *tpd = ep->transport_data;
63+
64+
assert(tpd->magic == test_transport_magic);
65+
66+
if (!tpd->named)
67+
return -1;
68+
69+
snprintf(buf, len, "test endpoint 0x%x", tpd->magic);
70+
71+
return 0;
72+
}
73+
5874
/* internal test helper to generate correct response crc */
5975
static void test_transport_resp_calc_mic(struct nvme_mi_resp *resp)
6076
{
@@ -72,6 +88,7 @@ static const struct nvme_mi_transport test_transport = {
7288
.mic_enabled = true,
7389
.submit = test_transport_submit,
7490
.close = test_transport_close,
91+
.desc_ep = test_transport_desc_ep,
7592
};
7693

7794
static void test_set_transport_callback(nvme_mi_ep_t ep, test_submit_cb cb,
@@ -96,6 +113,7 @@ nvme_mi_ep_t nvme_mi_open_test(nvme_root_t root)
96113
assert(tpd);
97114

98115
tpd->magic = test_transport_magic;
116+
tpd->named = true;
99117

100118
ep->transport = &test_transport;
101119
ep->transport_data = tpd;
@@ -173,6 +191,26 @@ static void test_transport_fail(nvme_mi_ep_t ep)
173191
assert(rc != 0);
174192
}
175193

194+
static void test_transport_describe(nvme_mi_ep_t ep)
195+
{
196+
struct test_transport_data *tpd;
197+
char *str;
198+
199+
tpd = (struct test_transport_data *)ep->transport_data;
200+
201+
tpd->named = false;
202+
str = nvme_mi_endpoint_desc(ep);
203+
assert(str);
204+
assert(!strcmp(str, "test-mi endpoint"));
205+
free(str);
206+
207+
tpd->named = true;
208+
str = nvme_mi_endpoint_desc(ep);
209+
assert(str);
210+
assert(!strcmp(str, "test-mi: test endpoint 0x74657374"));
211+
free(str);
212+
}
213+
176214
/* test: invalid crc */
177215
static int test_invalid_crc_cb(struct nvme_mi_ep *ep,
178216
struct nvme_mi_req *req,
@@ -321,6 +359,7 @@ struct test {
321359
} tests[] = {
322360
DEFINE_TEST(read_mi_data),
323361
DEFINE_TEST(transport_fail),
362+
DEFINE_TEST(transport_describe),
324363
DEFINE_TEST(invalid_crc),
325364
DEFINE_TEST(admin_id),
326365
DEFINE_TEST(admin_err_resp),

0 commit comments

Comments
 (0)