Skip to content

Commit 011547c

Browse files
mctp: separate addr add and remove codepath
Currently, mctp addr add and mctp addr del are joined into one codepath in cmd_addr_addremove, then split off based on the RTM_COMMAND value. This is an attempt to split those into separate code path, only extract the inner steps as common functions. Signed-off-by: Khang D Nguyen <[email protected]>
1 parent e3021b6 commit 011547c

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

src/mctp.c

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -741,20 +741,32 @@ static int cmd_addr_show(struct ctx *ctx, int argc, const char **argv)
741741
return 0;
742742
}
743743

744-
745-
// cmdname is for error messages.
746-
// rtm_command is RTM_NEWADDR or RTM_DELADDR
747-
static int cmd_addr_addremove(struct ctx *ctx,
748-
const char* cmdname, int rtm_command,
749-
int argc, const char **argv)
744+
static int parse_eid(const char* eidstr, uint8_t* eid)
750745
{
751-
const char *eidstr, *linkstr;
752746
uint32_t tmp;
747+
if (parse_uint32(eidstr, &tmp) < 0 || tmp > 0xff) {
748+
return -1;
749+
}
750+
*eid = tmp & 0xff;
751+
return 0;
752+
}
753+
754+
static int parse_ifindex(struct ctx *ctx, const char* linkstr, int* ifindex)
755+
{
756+
*ifindex = mctp_nl_ifindex_byname(ctx->nl, linkstr);
757+
if (!*ifindex) {
758+
return -1;
759+
}
760+
return 0;
761+
}
762+
763+
static int cmd_addr_add(struct ctx *ctx, int argc, const char **argv)
764+
{
753765
uint8_t eid;
754766
int ifindex;
755767

756768
if (argc != 4) {
757-
warnx("%s: invalid command line arguments", cmdname);
769+
warnx("add: invalid command line arguments");
758770
return -1;
759771
}
760772

@@ -765,34 +777,47 @@ static int cmd_addr_addremove(struct ctx *ctx,
765777

766778
mctp_ops_init();
767779

768-
eidstr = argv[1];
769-
linkstr = argv[3];
770-
771-
ifindex = mctp_nl_ifindex_byname(ctx->nl, linkstr);
772-
if (!ifindex) {
773-
warnx("invalid device %s", linkstr);
780+
if (parse_eid(argv[1], &eid) < 0) {
781+
warnx("invalid address %s", argv[1]);
774782
return -1;
775783
}
776784

777-
if (parse_uint32(eidstr, &tmp) < 0 || tmp > 0xff) {
778-
warnx("invalid address %s", eidstr);
785+
if (parse_ifindex(ctx, argv[3], &ifindex) < 0) {
786+
warnx("invalid device %s", argv[3]);
779787
return -1;
780788
}
781-
eid = tmp & 0xff;
782789

783-
return rtm_command == RTM_NEWADDR ?
784-
mctp_nl_addr_add(ctx->nl, eid, ifindex) :
785-
mctp_nl_addr_del(ctx->nl, eid, ifindex);
786-
}
787-
788-
static int cmd_addr_add(struct ctx *ctx, int argc, const char **argv)
789-
{
790-
return cmd_addr_addremove(ctx, "add", RTM_NEWADDR, argc, argv);
790+
return mctp_nl_addr_add(ctx->nl, eid, ifindex);
791791
}
792792

793793
static int cmd_addr_remove(struct ctx *ctx, int argc, const char **argv)
794794
{
795-
return cmd_addr_addremove(ctx, "del", RTM_DELADDR, argc, argv);
795+
uint8_t eid;
796+
int ifindex;
797+
798+
if (argc != 4) {
799+
warnx("remove: invalid command line arguments");
800+
return -1;
801+
}
802+
803+
if (strcmp(argv[2], "dev")) {
804+
warnx("invalid dev spec");
805+
return -1;
806+
}
807+
808+
mctp_ops_init();
809+
810+
if (parse_eid(argv[1], &eid) < 0) {
811+
warnx("invalid address %s", argv[1]);
812+
return -1;
813+
}
814+
815+
if (parse_ifindex(ctx, argv[3], &ifindex) < 0) {
816+
warnx("invalid device %s", argv[3]);
817+
return -1;
818+
}
819+
820+
return mctp_nl_addr_del(ctx->nl, eid, ifindex);
796821
}
797822

798823
static int cmd_addr(struct ctx *ctx, int argc, const char **argv)

0 commit comments

Comments
 (0)