Subject: [PATCH v2 bpf-next 12/17] libbpf: Refactor get_xdp_info

From: David Ahern <[email-masked]>
Date: Thu, 23 Apr 2020 20:11:43 -0600

From: David Ahern <[email-masked]>

Move parsing of IFLA_XDP attribute to a helper that can be reused
for the new IFLA_XDP_EGRESS attribute.

Signed-off-by: David Ahern <[email-masked]>
---
 tools/lib/bpf/netlink.c | 43 +++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 0b709fd10bba..90ced689e53b 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -238,51 +238,60 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh,
 	return dump_link_nlmsg(cookie, ifi, tb);
 }
 
-static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
+static int process_xdp_attr(struct nlattr *tb, struct xdp_link_info *info)
 {
 	struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
-	struct xdp_id_md *xdp_id = cookie;
-	struct ifinfomsg *ifinfo = msg;
 	int ret;
 
-	if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
-		return 0;
-
-	if (!tb[IFLA_XDP])
-		return 0;
-
-	ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb[IFLA_XDP], NULL);
+	ret = libbpf_nla_parse_nested(xdp_tb, IFLA_XDP_MAX, tb, NULL);
 	if (ret)
 		return ret;
 
 	if (!xdp_tb[IFLA_XDP_ATTACHED])
 		return 0;
 
-	xdp_id->info.attach_mode = libbpf_nla_getattr_u8(
-		xdp_tb[IFLA_XDP_ATTACHED]);
+	info->attach_mode = libbpf_nla_getattr_u8(xdp_tb[IFLA_XDP_ATTACHED]);
 
-	if (xdp_id->info.attach_mode == XDP_ATTACHED_NONE)
+	if (info->attach_mode == XDP_ATTACHED_NONE)
 		return 0;
 
 	if (xdp_tb[IFLA_XDP_PROG_ID])
-		xdp_id->info.prog_id = libbpf_nla_getattr_u32(
+		info->prog_id = libbpf_nla_getattr_u32(
 			xdp_tb[IFLA_XDP_PROG_ID]);
 
 	if (xdp_tb[IFLA_XDP_SKB_PROG_ID])
-		xdp_id->info.skb_prog_id = libbpf_nla_getattr_u32(
+		info->skb_prog_id = libbpf_nla_getattr_u32(
 			xdp_tb[IFLA_XDP_SKB_PROG_ID]);
 
 	if (xdp_tb[IFLA_XDP_DRV_PROG_ID])
-		xdp_id->info.drv_prog_id = libbpf_nla_getattr_u32(
+		info->drv_prog_id = libbpf_nla_getattr_u32(
 			xdp_tb[IFLA_XDP_DRV_PROG_ID]);
 
 	if (xdp_tb[IFLA_XDP_HW_PROG_ID])
-		xdp_id->info.hw_prog_id = libbpf_nla_getattr_u32(
+		info->hw_prog_id = libbpf_nla_getattr_u32(
 			xdp_tb[IFLA_XDP_HW_PROG_ID]);
 
 	return 0;
 }
 
+static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
+{
+	struct xdp_id_md *xdp_id = cookie;
+	struct ifinfomsg *ifinfo = msg;
+	int ret;
+
+	if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
+		return 0;
+
+	if (tb[IFLA_XDP]) {
+		ret = process_xdp_attr(tb[IFLA_XDP], &xdp_id->info);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
 			  size_t info_size, __u32 flags)
 {
-- 
2.21.1 (Apple Git-122.3)