Skip to content

Commit fa8e65d

Browse files
mctpd: add initializer for MCTP control messages
Currently, RQDI bits initialization is duplicated in every message handler. This commit extracts those common logics into functions in mctp-control-spec.h. Signed-off-by: Khang D Nguyen <[email protected]>
1 parent 0d13310 commit fa8e65d

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

src/mctp-control-spec.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* Derived from libmctp's libmctp-cmds.h */
44
#pragma once
55

6+
#include <assert.h>
67
#include <stdint.h>
78
#include <linux/mctp.h>
89

@@ -308,3 +309,22 @@ struct mctp_ctrl_resp_resolve_endpoint_id {
308309
#define GET_ROUTING_ENTRY_TYPE(field) \
309310
(((field) >> MCTP_ROUTING_ENTRY_TYPE_SHIFT) & \
310311
MCTP_ROUTING_ENTRY_TYPE_MASK)
312+
313+
#define RQDI_REQ (1 << 7)
314+
#define RQDI_RESP 0x0
315+
#define RQDI_IID_MASK 0x1f
316+
317+
static inline void mctp_ctrl_msg_hdr_init_req(struct mctp_ctrl_msg_hdr *req,
318+
uint8_t iid, uint8_t command_code)
319+
{
320+
assert(iid <= RQDI_IID_MASK);
321+
req->command_code = command_code;
322+
req->rq_dgram_inst = iid | RQDI_REQ;
323+
}
324+
325+
static inline void mctp_ctrl_msg_hdr_init_resp(struct mctp_ctrl_msg_hdr *resp,
326+
struct mctp_ctrl_msg_hdr req)
327+
{
328+
resp->command_code = req.command_code;
329+
resp->rq_dgram_inst = (req.rq_dgram_inst & RQDI_IID_MASK) | RQDI_RESP;
330+
}

src/mctpd.c

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ static mctp_eid_t eid_alloc_max = 0xfe;
6363
// arbitrary sanity
6464
static size_t MAX_PEER_SIZE = 1000000;
6565

66-
static const uint8_t RQDI_REQ = 1 << 7;
67-
static const uint8_t RQDI_RESP = 0x0;
68-
static const uint8_t IID_MASK = 0x1f;
69-
static const uint8_t RQDI_IID_MASK = 0x1f;
70-
7166
struct dest_phys {
7267
int ifindex;
7368
uint8_t hwaddr[MAX_ADDR_LEN];
@@ -645,9 +640,7 @@ static int handle_control_set_endpoint_id(struct ctx *ctx, int sd,
645640
}
646641
req = (void *)buf;
647642

648-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
649-
resp->ctrl_hdr.rq_dgram_inst =
650-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
643+
mctp_ctrl_msg_hdr_init_resp(&respi.ctrl_hdr, req->ctrl_hdr);
651644
resp->completion_code = MCTP_CTRL_CC_SUCCESS;
652645
resp->status = 0x01 << 4; // Already assigned, TODO
653646
resp->eid_set = local_addr(ctx, addr->smctp_ifindex);
@@ -668,7 +661,7 @@ handle_control_get_version_support(struct ctx *ctx, int sd,
668661
struct mctp_ctrl_resp_get_mctp_ver_support *resp = NULL;
669662
uint32_t *versions = NULL;
670663
// space for 4 versions
671-
uint8_t respbuf[sizeof(*resp) + 4 * sizeof(*versions)];
664+
uint8_t respbuf[sizeof(*resp) + 4 * sizeof(*versions)] = { 0 };
672665
size_t resp_len;
673666

674667
if (buf_size < sizeof(struct mctp_ctrl_cmd_get_mctp_ver_support)) {
@@ -678,7 +671,7 @@ handle_control_get_version_support(struct ctx *ctx, int sd,
678671

679672
req = (void *)buf;
680673
resp = (void *)respbuf;
681-
memset(resp, 0x0, sizeof(*resp));
674+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
682675
versions = (void *)(resp + 1);
683676
switch (req->msg_type_number) {
684677
case 0xff: // Base Protocol
@@ -699,9 +692,6 @@ handle_control_get_version_support(struct ctx *ctx, int sd,
699692
resp_len = sizeof(*resp);
700693
}
701694

702-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
703-
resp->ctrl_hdr.rq_dgram_inst =
704-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
705695
return reply_message(ctx, sd, resp, resp_len, addr);
706696
}
707697

@@ -719,9 +709,7 @@ static int handle_control_get_endpoint_id(struct ctx *ctx, int sd,
719709
}
720710

721711
req = (void *)buf;
722-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
723-
resp->ctrl_hdr.rq_dgram_inst =
724-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
712+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
725713

726714
resp->eid = local_addr(ctx, addr->smctp_ifindex);
727715
if (ctx->default_role == ENDPOINT_ROLE_BUS_OWNER)
@@ -740,7 +728,6 @@ handle_control_get_endpoint_uuid(struct ctx *ctx, int sd,
740728
const uint8_t *buf, const size_t buf_size)
741729
{
742730
struct mctp_ctrl_cmd_get_uuid *req = NULL;
743-
;
744731
struct mctp_ctrl_resp_get_uuid respi = { 0 }, *resp = &respi;
745732

746733
if (buf_size < sizeof(*req)) {
@@ -749,9 +736,7 @@ handle_control_get_endpoint_uuid(struct ctx *ctx, int sd,
749736
}
750737

751738
req = (void *)buf;
752-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
753-
resp->ctrl_hdr.rq_dgram_inst =
754-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
739+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
755740
memcpy(resp->uuid, ctx->uuid, sizeof(resp->uuid));
756741
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
757742
}
@@ -761,9 +746,8 @@ static int handle_control_get_message_type_support(
761746
const uint8_t *buf, const size_t buf_size)
762747
{
763748
struct mctp_ctrl_cmd_get_msg_type_support *req = NULL;
764-
;
765749
struct mctp_ctrl_resp_get_msg_type_support *resp = NULL;
766-
uint8_t resp_buf[sizeof(*resp) + 1];
750+
uint8_t resp_buf[sizeof(*resp) + 1] = { 0 };
767751
size_t resp_len;
768752

769753
if (buf_size < sizeof(*req)) {
@@ -773,9 +757,7 @@ static int handle_control_get_message_type_support(
773757

774758
req = (void *)buf;
775759
resp = (void *)resp_buf;
776-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
777-
resp->ctrl_hdr.rq_dgram_inst =
778-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
760+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
779761

780762
// Only control messages supported
781763
resp->msg_type_count = 1;
@@ -792,7 +774,7 @@ handle_control_resolve_endpoint_id(struct ctx *ctx, int sd,
792774
{
793775
struct mctp_ctrl_cmd_resolve_endpoint_id *req = NULL;
794776
struct mctp_ctrl_resp_resolve_endpoint_id *resp = NULL;
795-
uint8_t resp_buf[sizeof(*resp) + MAX_ADDR_LEN];
777+
uint8_t resp_buf[sizeof(*resp) + MAX_ADDR_LEN] = { 0 };
796778
size_t resp_len;
797779
struct peer *peer = NULL;
798780

@@ -803,11 +785,7 @@ handle_control_resolve_endpoint_id(struct ctx *ctx, int sd,
803785

804786
req = (void *)buf;
805787
resp = (void *)resp_buf;
806-
memset(resp, 0x0, sizeof(*resp));
807-
resp->ctrl_hdr.command_code = req->ctrl_hdr.command_code;
808-
resp->ctrl_hdr.rq_dgram_inst =
809-
(req->ctrl_hdr.rq_dgram_inst & IID_MASK) | RQDI_RESP;
810-
788+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, req->ctrl_hdr);
811789
peer = find_peer_by_addr(ctx, req->eid, addr->smctp_base.smctp_network);
812790
if (!peer) {
813791
resp->completion_code = MCTP_CTRL_CC_ERROR;
@@ -840,9 +818,7 @@ static int handle_control_unsupported(struct ctx *ctx, int sd,
840818
}
841819

842820
req = (void *)buf;
843-
resp->ctrl_hdr.command_code = req->command_code;
844-
resp->ctrl_hdr.rq_dgram_inst = (req->rq_dgram_inst & IID_MASK) |
845-
RQDI_RESP;
821+
mctp_ctrl_msg_hdr_init_resp(&resp->ctrl_hdr, *req);
846822
resp->completion_code = MCTP_CTRL_CC_ERROR_UNSUPPORTED_CMD;
847823
return reply_message(ctx, sd, resp, sizeof(*resp), addr);
848824
}
@@ -1377,8 +1353,10 @@ static int endpoint_send_set_endpoint_id(const struct peer *peer,
13771353
rc = -1;
13781354

13791355
iid = mctp_next_iid(peer->ctx);
1380-
req.ctrl_hdr.rq_dgram_inst = RQDI_REQ | iid;
1381-
req.ctrl_hdr.command_code = MCTP_CTRL_CMD_SET_ENDPOINT_ID;
1356+
1357+
mctp_ctrl_msg_hdr_init_req(&req.ctrl_hdr, iid,
1358+
MCTP_CTRL_CMD_SET_ENDPOINT_ID);
1359+
13821360
req.operation =
13831361
mctp_ctrl_cmd_set_eid_set_eid; // TODO: do we want Force?
13841362
req.eid = peer->eid;
@@ -1803,8 +1781,9 @@ static int query_get_endpoint_id(struct ctx *ctx, const dest_phys *dest,
18031781

18041782
iid = mctp_next_iid(ctx);
18051783

1806-
req.ctrl_hdr.rq_dgram_inst = RQDI_REQ | iid;
1807-
req.ctrl_hdr.command_code = MCTP_CTRL_CMD_GET_ENDPOINT_ID;
1784+
mctp_ctrl_msg_hdr_init_req(&req.ctrl_hdr, iid,
1785+
MCTP_CTRL_CMD_GET_ENDPOINT_ID);
1786+
18081787
rc = endpoint_query_phys(ctx, dest, MCTP_CTRL_HDR_MSG_TYPE, &req,
18091788
sizeof(req), &buf, &buf_size, &addr);
18101789
if (rc < 0)
@@ -1907,8 +1886,8 @@ static int query_get_peer_msgtypes(struct peer *peer)
19071886
peer->message_types = NULL;
19081887
iid = mctp_next_iid(peer->ctx);
19091888

1910-
req.ctrl_hdr.rq_dgram_inst = RQDI_REQ | iid;
1911-
req.ctrl_hdr.command_code = MCTP_CTRL_CMD_GET_MESSAGE_TYPE_SUPPORT;
1889+
mctp_ctrl_msg_hdr_init_req(&req.ctrl_hdr, iid,
1890+
MCTP_CTRL_CMD_GET_MESSAGE_TYPE_SUPPORT);
19121891

19131892
rc = endpoint_query_peer(peer, MCTP_CTRL_HDR_MSG_TYPE, &req,
19141893
sizeof(req), &buf, &buf_size, &addr);
@@ -1967,8 +1946,9 @@ static int query_get_peer_uuid_by_phys(struct ctx *ctx, const dest_phys *dest,
19671946
int rc;
19681947

19691948
iid = mctp_next_iid(ctx);
1970-
req.ctrl_hdr.rq_dgram_inst = RQDI_REQ | iid;
1971-
req.ctrl_hdr.command_code = MCTP_CTRL_CMD_GET_ENDPOINT_UUID;
1949+
1950+
mctp_ctrl_msg_hdr_init_req(&req.ctrl_hdr, iid,
1951+
MCTP_CTRL_CMD_GET_ENDPOINT_UUID);
19721952

19731953
rc = endpoint_query_phys(ctx, dest, MCTP_CTRL_HDR_MSG_TYPE, &req,
19741954
sizeof(req), &buf, &buf_size, &addr);
@@ -2006,8 +1986,9 @@ static int query_get_peer_uuid(struct peer *peer)
20061986
}
20071987

20081988
iid = mctp_next_iid(peer->ctx);
2009-
req.ctrl_hdr.rq_dgram_inst = RQDI_REQ | iid;
2010-
req.ctrl_hdr.command_code = MCTP_CTRL_CMD_GET_ENDPOINT_UUID;
1989+
1990+
mctp_ctrl_msg_hdr_init_req(&req.ctrl_hdr, iid,
1991+
MCTP_CTRL_CMD_GET_ENDPOINT_UUID);
20111992

20121993
rc = endpoint_query_peer(peer, MCTP_CTRL_HDR_MSG_TYPE, &req,
20131994
sizeof(req), &buf, &buf_size, &addr);

0 commit comments

Comments
 (0)