Skip to content

Commit

Permalink
Extracted IFLA_LINKINFO processing to a function (#168)
Browse files Browse the repository at this point in the history
- Refactoring to improve readability and maintainability.

Signed-off-by: Derek Foster <[email protected]>
  • Loading branch information
ffoulkes authored Nov 4, 2024
1 parent 1931fc5 commit df1c893
Showing 1 changed file with 64 additions and 38 deletions.
102 changes: 64 additions & 38 deletions switchlink/switchlink_link.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,67 @@ static void process_info_lag_member_data_attr(
}
}

/*
* Routine Description:
* Processes the IFLA_LINKINFO attribute.
*
* Arguments:
* [in] attr - netlink attribute
* [inout] attrs - link attributes
* [out] link_type - link type
* [out] slave_link_type - slave link type
* [out] create_lag_member - flag to create LAG member
*
* Return Values:
* void
*/
static void process_linkinfo_attr(const struct nlattr* attr,
struct link_attrs* attrs,
switchlink_link_type_t* link_type,
switchlink_link_type_t* slave_link_type,
bool* create_lag_member) {
const struct nlattr *linkinfo, *infodata, *infoslavedata;
int linkinfo_attr_type;
int attrlen = nla_len(attr);

// IFLA_LINKINFO is a container type
nla_for_each_nested(linkinfo, attr, attrlen) {
linkinfo_attr_type = nla_type(linkinfo);
switch (linkinfo_attr_type) {
case IFLA_INFO_KIND:
*link_type = get_link_type(nla_get_string(linkinfo));
break;
case IFLA_INFO_DATA:
// IFLA_INFO_DATA is a container type
if (*link_type == SWITCHLINK_LINK_TYPE_VXLAN) {
nla_for_each_nested(infodata, linkinfo, attrlen) {
process_info_data_attr(infodata, attrs);
}
} else if (*link_type == SWITCHLINK_LINK_TYPE_BOND) {
nla_for_each_nested(infodata, linkinfo, attrlen) {
process_info_lag_data_attr(infodata, attrs);
}
}
break;
case IFLA_INFO_SLAVE_KIND:
*slave_link_type = get_link_type(nla_get_string(linkinfo));
break;
case IFLA_INFO_SLAVE_DATA:
if (*slave_link_type == SWITCHLINK_LINK_TYPE_BOND) {
#ifdef LAG_OPTION
*create_lag_member = true;
#endif
nla_for_each_nested(infoslavedata, linkinfo, attrlen) {
process_info_lag_member_data_attr(infoslavedata, attrs);
}
}
break;
default:
break;
}
}
}

/*
* Routine Description:
* Processes netlink link messages.
Expand All @@ -209,11 +270,10 @@ static void process_info_lag_member_data_attr(
*/
void switchlink_process_link_msg(const struct nlmsghdr* nlmsg, int msgtype) {
int hdrlen, attrlen;
const struct nlattr *attr, *linkinfo, *infodata, *infoslavedata;
const struct nlattr* attr;
const struct ifinfomsg* ifmsg;
switchlink_link_type_t link_type = SWITCHLINK_LINK_TYPE_NONE;
switchlink_link_type_t slave_link_type = SWITCHLINK_LINK_TYPE_NONE;
int linkinfo_attr_type;

switchlink_db_interface_info_t intf_info = {0};
struct link_attrs attrs = {0};
Expand Down Expand Up @@ -247,42 +307,8 @@ void switchlink_process_link_msg(const struct nlmsghdr* nlmsg, int msgtype) {
krnlmon_log_debug("IFLA Operstate: %d\n", attrs.oper_state);
break;
case IFLA_LINKINFO:
// IFLA_LINKINFO is a container type
nla_for_each_nested(linkinfo, attr, attrlen) {
linkinfo_attr_type = nla_type(linkinfo);
switch (linkinfo_attr_type) {
case IFLA_INFO_KIND:
link_type = get_link_type(nla_get_string(linkinfo));
break;
case IFLA_INFO_DATA:
// IFLA_INFO_DATA is a container type
if (link_type == SWITCHLINK_LINK_TYPE_VXLAN) {
nla_for_each_nested(infodata, linkinfo, attrlen) {
process_info_data_attr(infodata, &attrs);
}
} else if (link_type == SWITCHLINK_LINK_TYPE_BOND) {
nla_for_each_nested(infodata, linkinfo, attrlen) {
process_info_lag_data_attr(infodata, &attrs);
}
}
break;
case IFLA_INFO_SLAVE_KIND:
slave_link_type = get_link_type(nla_get_string(linkinfo));
break;
case IFLA_INFO_SLAVE_DATA:
if (slave_link_type == SWITCHLINK_LINK_TYPE_BOND) {
#ifdef LAG_OPTION
create_lag_member = true;
#endif
nla_for_each_nested(infoslavedata, linkinfo, attrlen) {
process_info_lag_member_data_attr(infoslavedata, &attrs);
}
}
break;
default:
break;
}
}
process_linkinfo_attr(attr, &attrs, &link_type, &slave_link_type,
&create_lag_member);
break;
case IFLA_ADDRESS:
// IFLA_ADDRESS for kind "sit" is 4 octets
Expand Down

0 comments on commit df1c893

Please sign in to comment.