diff mbox series

[iproute2-next,10/12] ip: nexthop: add a helper which retrieves and prints cached nh entry

Message ID 20210930113844.1829373-11-razor@blackwall.org (mailing list archive)
State Accepted
Commit cb3d18c29e20fa95bd98a614cc5a86f9a1336115
Delegated to: David Ahern
Headers show
Series ip: nexthop: cache nexthops and print routes' nh info | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Nikolay Aleksandrov Sept. 30, 2021, 11:38 a.m. UTC
From: Nikolay Aleksandrov <nikolay@nvidia.com>

Add a helper which looks for a nexthop in the cache and if not found
reads the entry from the kernel and caches it. Finally the entry is
printed.

Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
---
 ip/ipnexthop.c | 16 ++++++++++++++++
 ip/nh_common.h |  3 +++
 2 files changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c
index e0f0f78460c9..31462c57d299 100644
--- a/ip/ipnexthop.c
+++ b/ip/ipnexthop.c
@@ -602,6 +602,22 @@  static void ipnh_cache_del(struct nh_entry *nhe)
 	free(nhe);
 }
 
+void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj,
+			    __u32 nh_id)
+{
+	struct nh_entry *nhe = ipnh_cache_get(nh_id);
+
+	if (!nhe) {
+		nhe = ipnh_cache_add(nh_id);
+		if (!nhe)
+			return;
+	}
+
+	if (fp_prefix)
+		print_string(PRINT_FP, NULL, "%s", fp_prefix);
+	__print_nexthop_entry(fp, jsobj, nhe, false);
+}
+
 int print_nexthop(struct nlmsghdr *n, void *arg)
 {
 	struct nhmsg *nhm = NLMSG_DATA(n);
diff --git a/ip/nh_common.h b/ip/nh_common.h
index ee84d968d8dd..b448f1b5530b 100644
--- a/ip/nh_common.h
+++ b/ip/nh_common.h
@@ -46,4 +46,7 @@  struct nh_entry {
 	struct nexthop_grp	*nh_groups;
 };
 
+void print_cache_nexthop_id(FILE *fp, const char *fp_prefix, const char *jsobj,
+			    __u32 nh_id);
+
 #endif /* __NH_COMMON_H__ */