Skip to content

Use Vendor message types for mctp-bench, mctp-echo #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

(cd obj; python3 ../tests/mctpd/__init__.py)

3. mctp-bench, mctp-req, mctp-echo: Message format has changed to use a
vendor-defined message type, rather than MCTP type 1.

## [2.1] - 2024-12-16

### Fixed
Expand Down
17 changes: 14 additions & 3 deletions src/mctp-bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@

#include "mctp.h"

// Code Construct allocation
static const uint8_t VENDOR_TYPE_BENCH[3] = { 0xcc, 0xde, 0xf1 };
static const uint8_t MCTP_TYPE_VENDOR_PCIE = 0x7e;

struct mctp_bench_send_args {
mctp_eid_t eid;
size_t len;
int net;
};

struct msg_header {
uint8_t vendor_prefix[sizeof(VENDOR_TYPE_BENCH)];
uint16_t magic;
uint32_t seq_no;
};
} __attribute__((packed));

struct mctp_stats {
size_t total_received_len, curr_packet_len;
Expand Down Expand Up @@ -116,6 +121,12 @@ static int handle_incoming_msg(struct recv_ctx *recv_ctx)
}

hdr = (struct msg_header *)recv_ctx->buf;
if (memcmp(hdr->vendor_prefix, VENDOR_TYPE_BENCH, sizeof(VENDOR_TYPE_BENCH)) != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check that we have at least sizeof(VENDOR_TYPE_BENCH).

But I guess we're not checking that we have a full header either; we should probably add that, before the initial (struct msg_header *) cast.

warnx("recv: unexpected vendor prefix %02x %02x %02x",
hdr->vendor_prefix[0], hdr->vendor_prefix[1], hdr->vendor_prefix[2]
);
return -1;
}
if (hdr->magic != MAGIC_VAL) {
warnx("recv: expected magic:\"%x\", got:\"%x\"\n", MAGIC_VAL,
hdr->magic);
Expand Down Expand Up @@ -158,7 +169,7 @@ static int mctp_bench_recv()
addr.smctp_family = AF_MCTP;
addr.smctp_network = MCTP_NET_ANY;
addr.smctp_addr.s_addr = MCTP_ADDR_ANY;
addr.smctp_type = 1;
addr.smctp_type = MCTP_TYPE_VENDOR_PCIE;
addr.smctp_tag = MCTP_TAG_OWNER;

recv_ctx.buf = malloc(MAX_LEN);
Expand Down Expand Up @@ -283,7 +294,7 @@ static int mctp_bench_send(struct mctp_bench_send_args send_args)
addr.smctp_family = AF_MCTP;
addr.smctp_network = send_args.net;
addr.smctp_addr.s_addr = send_args.eid;
addr.smctp_type = 1;
addr.smctp_type = MCTP_TYPE_VENDOR_PCIE;
printf("send: eid = %d, net = %d, type = %d, msg_len = %zu bytes\n",
send_args.eid, send_args.net, addr.smctp_type, send_args.len);

Expand Down
28 changes: 25 additions & 3 deletions src/mctp-echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#include <sys/socket.h>
#include "mctp.h"

// Code Construct allocation
static const uint8_t VENDOR_TYPE_ECHO[3] = { 0xcc, 0xde, 0xf0 };
static const uint8_t MCTP_TYPE_VENDOR_PCIE = 0x7e;

int main(void)
{
struct sockaddr_mctp addr;
Expand All @@ -32,7 +36,7 @@ int main(void)
addr.smctp_family = AF_MCTP;
addr.smctp_network = MCTP_NET_ANY;
addr.smctp_addr.s_addr = MCTP_ADDR_ANY;
addr.smctp_type = 1;
addr.smctp_type = MCTP_TYPE_VENDOR_PCIE;
addr.smctp_tag = MCTP_TAG_OWNER;

buflen = 0;
Expand Down Expand Up @@ -70,11 +74,29 @@ int main(void)
continue;
}

printf("echo: message from (net %d, eid %d), tag %d, type %d: len %zd, 0x%02x ..., responding\n",
if (len < (ssize_t)sizeof(VENDOR_TYPE_ECHO)) {
warnx("echo: short message from (net %d, eid %d), tag %d, type 0x%x: len %zd.\n",
addr.smctp_network, addr.smctp_addr.s_addr,
addr.smctp_tag,
addr.smctp_type,
len);
continue;
}

if (memcmp(buf, VENDOR_TYPE_ECHO, sizeof(VENDOR_TYPE_ECHO)) != 0) {
warnx("echo: unexpected vendor ID from (net %d, eid %d), tag %d, type 0x%x, len %zd.\n",
addr.smctp_network, addr.smctp_addr.s_addr,
addr.smctp_tag,
addr.smctp_type,
len);
continue;
}

printf("echo: message from (net %d, eid %d), tag %d, type 0x%x: len %zd, responding\n",
addr.smctp_network, addr.smctp_addr.s_addr,
addr.smctp_tag,
addr.smctp_type,
len, buf[0]);
len);

addr.smctp_tag &= ~MCTP_TAG_OWNER;

Expand Down
50 changes: 30 additions & 20 deletions src/mctp-req.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ static const int DEFAULT_NET = 1;
static const mctp_eid_t DEFAULT_EID = 8;
static const size_t DEFAULT_LEN = 1;

// Code Construct allocation
static const uint8_t VENDOR_TYPE_ECHO[3] = { 0xcc, 0xde, 0xf0 };
static const uint8_t MCTP_TYPE_VENDOR_PCIE = 0x7e;

/* lladdrlen != -1 to ignore ifindex/lladdr */
static int mctp_req(unsigned int net, mctp_eid_t eid,
unsigned int ifindex, uint8_t *lladdr, int lladdrlen,
uint8_t *data, size_t len)
{
struct sockaddr_mctp_ext addr;
unsigned char *buf, *rxbuf;
unsigned char *payload, *buf;
socklen_t addrlen;
int rc, sd, val;
size_t i;
size_t i, buf_len;

sd = socket(AF_MCTP, SOCK_DGRAM, 0);
if (sd < 0)
Expand All @@ -42,20 +46,23 @@ static int mctp_req(unsigned int net, mctp_eid_t eid,
addr.smctp_base.smctp_family = AF_MCTP;
addr.smctp_base.smctp_network = net;
addr.smctp_base.smctp_addr.s_addr = eid;
addr.smctp_base.smctp_type = 1;
addr.smctp_base.smctp_type = MCTP_TYPE_VENDOR_PCIE;
addr.smctp_base.smctp_tag = MCTP_TAG_OWNER;
printf("req: sending to (net %d, eid %d), type %d\n",
printf("req: sending to (net %d, eid %d), type 0x%x\n",
net, eid, addr.smctp_base.smctp_type);

rxbuf = malloc(len);
if (!rxbuf)
buf_len = len + sizeof(VENDOR_TYPE_ECHO);
buf = malloc(buf_len);
if (!buf)
err(EXIT_FAILURE, "malloc");
memcpy(buf, VENDOR_TYPE_ECHO, sizeof(VENDOR_TYPE_ECHO));
payload = &buf[sizeof(VENDOR_TYPE_ECHO)];

if (data) {
buf = data;
memcpy(payload, data, len);
} else {
buf = rxbuf;
for (i = 0; i < len; i++)
buf[i] = i & 0xff;
payload[i] = i & 0xff;
}

/* extended addressing */
Expand All @@ -76,20 +83,20 @@ static int mctp_req(unsigned int net, mctp_eid_t eid,


/* send data */
rc = sendto(sd, buf, len, 0,
rc = sendto(sd, buf, buf_len, 0,
(struct sockaddr *)&addr, addrlen);
if (rc != (int)len)
err(EXIT_FAILURE, "sendto(%zd)", len);
if (rc != (int)buf_len)
err(EXIT_FAILURE, "sendto(%zd)", buf_len);

/* receive response */
addrlen = sizeof(addr);
rc = recvfrom(sd, rxbuf, len, MSG_TRUNC,
rc = recvfrom(sd, buf, buf_len, MSG_TRUNC,
(struct sockaddr *)&addr, &addrlen);
if (rc < 0)
err(EXIT_FAILURE, "recvfrom");
else if ((size_t)rc != len)
else if ((size_t)rc != buf_len)
errx(EXIT_FAILURE, "unexpected length: got %d, exp %zd",
rc, len);
rc, buf_len);

if (!(addrlen == sizeof(struct sockaddr_mctp_ext) ||
addrlen == sizeof(struct sockaddr_mctp)))
Expand All @@ -98,24 +105,27 @@ static int mctp_req(unsigned int net, mctp_eid_t eid,
sizeof(struct sockaddr_mctp));


printf("req: message from (net %d, eid %d) type %d len %zd: 0x%02x..\n",
printf("req: message from (net %d, eid %d) type 0x%x len %zd\n",
addr.smctp_base.smctp_network, addr.smctp_base.smctp_addr.s_addr,
addr.smctp_base.smctp_type,
len,
rxbuf[0]);
len);
if (addrlen == sizeof(struct sockaddr_mctp_ext)) {
printf(" ext ifindex %d ha[0]=0x%02x len %hhu\n",
addr.smctp_ifindex,
addr.smctp_haddr[0], addr.smctp_halen);
}

if (memcmp(buf, VENDOR_TYPE_ECHO, sizeof(VENDOR_TYPE_ECHO)) != 0) {
errx(EXIT_FAILURE, "unexpected vendor ID");
}

for (i = 0; i < len; i++) {
uint8_t exp = data ? data[i] : i & 0xff;
if (rxbuf[i] != exp)
if (payload[i] != exp)
errx(EXIT_FAILURE,
"payload mismatch at byte 0x%zx; "
"sent 0x%02x, received 0x%02x",
i, exp, rxbuf[i]);
i, exp, buf[i]);
}

return 0;
Expand Down