From df1c893505f836d03211d893a6d12ddf3cfa800a Mon Sep 17 00:00:00 2001 From: Derek G Foster Date: Mon, 4 Nov 2024 08:38:37 -0800 Subject: [PATCH] Extracted IFLA_LINKINFO processing to a function (#168) - Refactoring to improve readability and maintainability. Signed-off-by: Derek Foster --- switchlink/switchlink_link.c | 102 ++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 38 deletions(-) diff --git a/switchlink/switchlink_link.c b/switchlink/switchlink_link.c index 19a0f3d..2657d99 100644 --- a/switchlink/switchlink_link.c +++ b/switchlink/switchlink_link.c @@ -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. @@ -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}; @@ -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