Skip to content

Commit df1c893

Browse files
authored
Extracted IFLA_LINKINFO processing to a function (#168)
- Refactoring to improve readability and maintainability. Signed-off-by: Derek Foster <[email protected]>
1 parent 1931fc5 commit df1c893

File tree

1 file changed

+64
-38
lines changed

1 file changed

+64
-38
lines changed

switchlink/switchlink_link.c

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,67 @@ static void process_info_lag_member_data_attr(
196196
}
197197
}
198198

199+
/*
200+
* Routine Description:
201+
* Processes the IFLA_LINKINFO attribute.
202+
*
203+
* Arguments:
204+
* [in] attr - netlink attribute
205+
* [inout] attrs - link attributes
206+
* [out] link_type - link type
207+
* [out] slave_link_type - slave link type
208+
* [out] create_lag_member - flag to create LAG member
209+
*
210+
* Return Values:
211+
* void
212+
*/
213+
static void process_linkinfo_attr(const struct nlattr* attr,
214+
struct link_attrs* attrs,
215+
switchlink_link_type_t* link_type,
216+
switchlink_link_type_t* slave_link_type,
217+
bool* create_lag_member) {
218+
const struct nlattr *linkinfo, *infodata, *infoslavedata;
219+
int linkinfo_attr_type;
220+
int attrlen = nla_len(attr);
221+
222+
// IFLA_LINKINFO is a container type
223+
nla_for_each_nested(linkinfo, attr, attrlen) {
224+
linkinfo_attr_type = nla_type(linkinfo);
225+
switch (linkinfo_attr_type) {
226+
case IFLA_INFO_KIND:
227+
*link_type = get_link_type(nla_get_string(linkinfo));
228+
break;
229+
case IFLA_INFO_DATA:
230+
// IFLA_INFO_DATA is a container type
231+
if (*link_type == SWITCHLINK_LINK_TYPE_VXLAN) {
232+
nla_for_each_nested(infodata, linkinfo, attrlen) {
233+
process_info_data_attr(infodata, attrs);
234+
}
235+
} else if (*link_type == SWITCHLINK_LINK_TYPE_BOND) {
236+
nla_for_each_nested(infodata, linkinfo, attrlen) {
237+
process_info_lag_data_attr(infodata, attrs);
238+
}
239+
}
240+
break;
241+
case IFLA_INFO_SLAVE_KIND:
242+
*slave_link_type = get_link_type(nla_get_string(linkinfo));
243+
break;
244+
case IFLA_INFO_SLAVE_DATA:
245+
if (*slave_link_type == SWITCHLINK_LINK_TYPE_BOND) {
246+
#ifdef LAG_OPTION
247+
*create_lag_member = true;
248+
#endif
249+
nla_for_each_nested(infoslavedata, linkinfo, attrlen) {
250+
process_info_lag_member_data_attr(infoslavedata, attrs);
251+
}
252+
}
253+
break;
254+
default:
255+
break;
256+
}
257+
}
258+
}
259+
199260
/*
200261
* Routine Description:
201262
* Processes netlink link messages.
@@ -209,11 +270,10 @@ static void process_info_lag_member_data_attr(
209270
*/
210271
void switchlink_process_link_msg(const struct nlmsghdr* nlmsg, int msgtype) {
211272
int hdrlen, attrlen;
212-
const struct nlattr *attr, *linkinfo, *infodata, *infoslavedata;
273+
const struct nlattr* attr;
213274
const struct ifinfomsg* ifmsg;
214275
switchlink_link_type_t link_type = SWITCHLINK_LINK_TYPE_NONE;
215276
switchlink_link_type_t slave_link_type = SWITCHLINK_LINK_TYPE_NONE;
216-
int linkinfo_attr_type;
217277

218278
switchlink_db_interface_info_t intf_info = {0};
219279
struct link_attrs attrs = {0};
@@ -247,42 +307,8 @@ void switchlink_process_link_msg(const struct nlmsghdr* nlmsg, int msgtype) {
247307
krnlmon_log_debug("IFLA Operstate: %d\n", attrs.oper_state);
248308
break;
249309
case IFLA_LINKINFO:
250-
// IFLA_LINKINFO is a container type
251-
nla_for_each_nested(linkinfo, attr, attrlen) {
252-
linkinfo_attr_type = nla_type(linkinfo);
253-
switch (linkinfo_attr_type) {
254-
case IFLA_INFO_KIND:
255-
link_type = get_link_type(nla_get_string(linkinfo));
256-
break;
257-
case IFLA_INFO_DATA:
258-
// IFLA_INFO_DATA is a container type
259-
if (link_type == SWITCHLINK_LINK_TYPE_VXLAN) {
260-
nla_for_each_nested(infodata, linkinfo, attrlen) {
261-
process_info_data_attr(infodata, &attrs);
262-
}
263-
} else if (link_type == SWITCHLINK_LINK_TYPE_BOND) {
264-
nla_for_each_nested(infodata, linkinfo, attrlen) {
265-
process_info_lag_data_attr(infodata, &attrs);
266-
}
267-
}
268-
break;
269-
case IFLA_INFO_SLAVE_KIND:
270-
slave_link_type = get_link_type(nla_get_string(linkinfo));
271-
break;
272-
case IFLA_INFO_SLAVE_DATA:
273-
if (slave_link_type == SWITCHLINK_LINK_TYPE_BOND) {
274-
#ifdef LAG_OPTION
275-
create_lag_member = true;
276-
#endif
277-
nla_for_each_nested(infoslavedata, linkinfo, attrlen) {
278-
process_info_lag_member_data_attr(infoslavedata, &attrs);
279-
}
280-
}
281-
break;
282-
default:
283-
break;
284-
}
285-
}
310+
process_linkinfo_attr(attr, &attrs, &link_type, &slave_link_type,
311+
&create_lag_member);
286312
break;
287313
case IFLA_ADDRESS:
288314
// IFLA_ADDRESS for kind "sit" is 4 octets

0 commit comments

Comments
 (0)