Skip to contentSkip to conversation

Report netns cookie in FIB lookup tracepoints

PATCH Applied Ido Schimmel 4 participants 10 messages Oct 8 – Oct 13

Subsystems (6)
lore
Share & tools
lore

Ido Schimmel

author maintainer
#0 lore raw

v2 cover Report netns cookie in FIB lookup tracepoints

The fib:fib_table_lookup and fib6:fib6_table_lookup tracepoints do not report the network namespace in which the lookup was performed, so lookups performed in different namespaces cannot be told apart. The recorded PID does not help either, as lookups in the receive path are performed in softIRQ context.

This is a problem, for example, for netns-aware tracers [1] and multi-ASIC systems where each ASIC and its ports reside in a separate network namespace.

This patchset reports the network namespace cookie in both tracepoints, as commit 27cb3de7f43a ("net: add net cookie for net device trace events") did for the net device tracepoints. This allows filtering lookups performed in a specific network namespace, for example:

 # perf record -a -e fib:fib_table_lookup --filter 'net_cookie == 12'

The cookie of a given network namespace can be retrieved using "ip netns cookie" [2].

Patch #1 reports the cookie in the IPv6 tracepoint, which is already passed the network namespace.

Patch #2 passes the network namespace to fib_table_lookup() and from there to the IPv4 tracepoint.

Patch #3 reports the cookie in the IPv4 tracepoint.

v2:
* Patch #2: Make the network namespace the last argument of the fib:fib_table_lookup tracepoint. I kept the tags since the change is minimal.

v1:
https://lore.kernel.org/netdev/[email protected]/

[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/

Ido Schimmel (3):
ipv6: Report netns cookie in fib6_table_lookup tracepoint
ipv4: Pass netns to fib_table_lookup()
ipv4: Report netns cookie in fib_table_lookup tracepoint

 include/net/ip_fib.h        | 12 +++++++-----
 include/trace/events/fib.h  | 13 +++++++++----
 include/trace/events/fib6.h |  8 ++++++--
 net/core/filter.c           |  2 +-
 net/ipv4/devinet.c          |  3 ++-
 net/ipv4/fib_frontend.c     |  6 ++++--
 net/ipv4/fib_rules.c        |  2 +-
 net/ipv4/fib_semantics.c    |  2 +-
 net/ipv4/fib_trie.c         | 14 ++++++++------
 9 files changed, 39 insertions(+), 23 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH v2 1/3 ipv6: Report netns cookie in fib6_table_lookup tracepoint

The first argument of the tracepoint is a pointer to the network namespace in which the FIB lookup was performed, but the tracepoint does not report information about it in the associated trace record, making it impossible to tell apart lookups performed in different namespaces.

Report the network namespace cookie, as commit 27cb3de7f43a ("net: add net cookie for net device trace events") did for the net device tracepoints.

---

Notes:
Sashiko: There is a pre-existing issue here regarding an uninitialized gateway address. I will take care of it separately.

 include/trace/events/fib6.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/trace/events/fib6.h +6 -2
index 8d22b2e98d48..e58facd2d7d4 100644
@@ -34,6 +34,7 @@ TRACE_EVENT(fib6_table_lookup,
 		__field(        u8,	rt_type		)
 		__array(		char,	name,	IFNAMSIZ )
 		__array(		__u8,	gw,	16	 )
+		__field(	u64,	net_cookie	)
 	),
 
 	TP_fast_assign(
@@ -76,13 +77,16 @@ TRACE_EVENT(fib6_table_lookup,
 			in6 = (struct in6_addr *)__entry->gw;
 			*in6 = res->nh->fib_nh_gw6;
 		}
+
+		__entry->net_cookie = net->net_cookie;
 	),
 
-	TP_printk("table %3u oif %d iif %d proto %u %pI6c/%u -> %pI6c/%u flowlabel %#x tos %d scope %d flags %x ==> dev %s gw %pI6c err %d",
+	TP_printk("table %3u oif %d iif %d proto %u %pI6c/%u -> %pI6c/%u flowlabel %#x tos %d scope %d flags %x ==> dev %s gw %pI6c err %d net_cookie %llu",
 		  __entry->tb_id, __entry->oif, __entry->iif, __entry->proto,
 		  __entry->src, __entry->sport, __entry->dst, __entry->dport,
 		  __entry->flowlabel, __entry->tos, __entry->scope,
-		  __entry->flags, __entry->name, __entry->gw, __entry->err)
+		  __entry->flags, __entry->name, __entry->gw, __entry->err,
+		  __entry->net_cookie)
 );
 
 #endif /* _TRACE_FIB6_H */
Signature · 1 line
2.55.0
maintainer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH net-next v2 1/3] ipv6: Report netns cookie in fib6_table_lookup tracepoint

Quoted 13 lines · Ido Schimmel

The first argument of the tracepoint is a pointer to the network namespace in which the FIB lookup was performed, but the tracepoint does not report information about it in the associated trace record, making it impossible to tell apart lookups performed in different namespaces.

Report the network namespace cookie, as commit 27cb3de7f43a ("net: add net cookie for net device trace events") did for the net device tracepoints.

Quoted 9 lines

---

Notes:
Sashiko: There is a pre-existing issue here regarding an uninitialized gateway address. I will take care of it separately.

 include/trace/events/fib6.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
author maintainer
#3 lore raw

PATCH v2 2/3 ipv4: Pass netns to fib_table_lookup()

In reply to Ido Schimmel

The next patch will report the network namespace cookie in the fib_table_lookup tracepoint. As a preparation, pass the namespace to fib_table_lookup() and from there to the tracepoint, like the fib6_table_lookup tracepoint. All the callers already have the namespace at hand.

Add the namespace as the last tracepoint argument so that the positions of the existing arguments do not change and BPF programs that attach to the raw tracepoint are not affected.

No functional changes intended.

---
 include/net/ip_fib.h       | 12 +++++++-----
 include/trace/events/fib.h |  5 +++--
 net/core/filter.c          |  2 +-
 net/ipv4/devinet.c         |  3 ++-
 net/ipv4/fib_frontend.c    |  6 ++++--
 net/ipv4/fib_rules.c       |  2 +-
 net/ipv4/fib_semantics.c   |  2 +-
 net/ipv4/fib_trie.c        | 14 ++++++++------
 8 files changed, 27 insertions(+), 19 deletions(-)
include/net/ip_fib.h +7 -5
index 0a35355fb0f3..fb446f01a65d 100644
@@ -275,8 +275,9 @@ struct fib_dump_filter {
 	struct net_device	*dev;
 };
 
-int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
-		     struct fib_result *res, int fib_flags);
+int fib_table_lookup(struct net *net, struct fib_table *tb,
+		     const struct flowi4 *flp, struct fib_result *res,
+		     int fib_flags);
 int fib_table_insert(struct net *, struct fib_table *, struct fib_config *,
 		     struct netlink_ext_ack *extack);
 int fib_table_delete(struct net *, struct fib_table *, struct fib_config *,
@@ -323,7 +324,8 @@ static inline int fib_lookup(struct net *net, const struct flowi4 *flp,
 
 	tb = fib_get_table(net, RT_TABLE_MAIN);
 	if (tb)
-		err = fib_table_lookup(tb, flp, res, flags | FIB_LOOKUP_NOREF);
+		err = fib_table_lookup(net, tb, flp, res,
+				       flags | FIB_LOOKUP_NOREF);
 
 	if (err == -EAGAIN)
 		err = -ENETUNREACH;
@@ -387,14 +389,14 @@ static inline int fib_lookup(struct net *net, struct flowi4 *flp,
 
 	tb = rcu_dereference_rtnl(net->ipv4.fib_main);
 	if (tb)
-		err = fib_table_lookup(tb, flp, res, flags);
+		err = fib_table_lookup(net, tb, flp, res, flags);
 
 	if (err != -EAGAIN)
 		goto out;
 
 	tb = rcu_dereference_rtnl(net->ipv4.fib_default);
 	if (tb)
-		err = fib_table_lookup(tb, flp, res, flags);
+		err = fib_table_lookup(net, tb, flp, res, flags);
 
 	if (err == -EAGAIN)
 		err = -ENETUNREACH;
include/trace/events/fib.h +3 -2
index feb28b359eff..341905b5edf3 100644
@@ -15,9 +15,10 @@
 TRACE_EVENT(fib_table_lookup,
 
 	TP_PROTO(u32 tb_id, const struct flowi4 *flp,
-		 const struct fib_nh_common *nhc, int err),
+		 const struct fib_nh_common *nhc, int err,
+		 const struct net *net),
 
-	TP_ARGS(tb_id, flp, nhc, err),
+	TP_ARGS(tb_id, flp, nhc, err, net),
 
 	TP_STRUCT__entry(
 		__field(	u32,	tb_id		)
net/core/filter.c +1 -1
index 70dc621672f2..3ee1a093337e 100644
@@ -6413,7 +6413,7 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
 		if (unlikely(!tb))
 			return BPF_FIB_LKUP_RET_NOT_FWDED;
 
-		err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
+		err = fib_table_lookup(net, tb, &fl4, &res, FIB_LOOKUP_NOREF);
 	} else {
 		if (flags & BPF_FIB_LOOKUP_MARK)
 			fl4.flowi4_mark = params->mark;
net/ipv4/devinet.c +2 -1
index 2ed4c706ca9e..c106d56eb2f7 100644
@@ -157,7 +157,8 @@ struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
 		 */
 		local = fib_get_table(net, RT_TABLE_LOCAL);
 		if (local &&
-		    !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) &&
+		    !fib_table_lookup(net, local, &fl4, &res,
+				      FIB_LOOKUP_NOREF) &&
 		    res.type == RTN_LOCAL)
 			result = FIB_RES_DEV(res);
 	} else {
net/ipv4/fib_frontend.c +4 -2
index 8a3dc04e8cac..c80c1b36f1fd 100644
@@ -243,7 +243,8 @@ static inline unsigned int __inet_dev_addr_type(struct net *net,
 	table = fib_get_table(net, tb_id);
 	if (table) {
 		ret = RTN_UNICAST;
-		if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
+		if (!fib_table_lookup(net, table, &fl4, &res,
+				      FIB_LOOKUP_NOREF)) {
 			struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0);
 
 			if (!dev || dev == nhc->nhc_dev)
@@ -1400,7 +1401,8 @@ static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
 		local_bh_disable();
 
 		frn->tb_id = tb->tb_id;
-		frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
+		frn->err = fib_table_lookup(net, tb, &fl4, &res,
+					    FIB_LOOKUP_NOREF);
 
 		if (!frn->err) {
 			frn->prefixlen = res.prefixlen;
net/ipv4/fib_rules.c +1 -1
index 060501b376a8..28ba35125383 100644
@@ -136,7 +136,7 @@ INDIRECT_CALLABLE_SCOPE int fib4_rule_action(struct fib_rule *rule,
 	tb_id = fib_rule_get_table(rule, arg);
 	tbl = fib_get_table(rule->fr_net, tb_id);
 	if (tbl)
-		err = fib_table_lookup(tbl, &flp->u.ip4,
+		err = fib_table_lookup(rule->fr_net, tbl, &flp->u.ip4,
 				       (struct fib_result *)arg->result,
 				       arg->flags);
 
net/ipv4/fib_semantics.c +1 -1
index 235c51a6f8e5..24def626739b 100644
@@ -1221,7 +1221,7 @@ static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
 			tbl = fib_get_table(net, table);
 
 		if (tbl)
-			err = fib_table_lookup(tbl, &fl4, &res,
+			err = fib_table_lookup(net, tbl, &fl4, &res,
 					       FIB_LOOKUP_IGNORE_LINKSTATE |
 					       FIB_LOOKUP_NOREF);
 
net/ipv4/fib_trie.c +8 -6
index acb1e4385914..9ad76de68081 100644
@@ -1417,8 +1417,9 @@ bool fib_lookup_good_nhc(const struct fib_nh_common *nhc, int fib_flags,
 }
 
 /* should be called with rcu_read_lock */
-int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
-		     struct fib_result *res, int fib_flags)
+int fib_table_lookup(struct net *net, struct fib_table *tb,
+		     const struct flowi4 *flp, struct fib_result *res,
+		     int fib_flags)
 {
 	struct trie *t = (struct trie *) tb->tb_data;
 #ifdef CONFIG_IP_FIB_TRIE_STATS
@@ -1435,7 +1436,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
 
 	n = get_child_rcu(pn, cindex);
 	if (!n) {
-		trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN);
+		trace_fib_table_lookup(tb->tb_id, flp, NULL, -EAGAIN, net);
 		return -EAGAIN;
 	}
 
@@ -1522,7 +1523,8 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
 				 */
 				if (IS_TRIE(pn)) {
 					trace_fib_table_lookup(tb->tb_id, flp,
-							       NULL, -EAGAIN);
+							       NULL, -EAGAIN,
+							       net);
 					return -EAGAIN;
 				}
 #ifdef CONFIG_IP_FIB_TRIE_STATS
@@ -1569,7 +1571,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
 #ifdef CONFIG_IP_FIB_TRIE_STATS
 			this_cpu_inc(stats->semantic_match_passed);
 #endif
-			trace_fib_table_lookup(tb->tb_id, flp, NULL, err);
+			trace_fib_table_lookup(tb->tb_id, flp, NULL, err, net);
 			return err;
 		}
 		if (fi->fib_flags & RTNH_F_DEAD)
@@ -1610,7 +1612,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
 #ifdef CONFIG_IP_FIB_TRIE_STATS
 			this_cpu_inc(stats->semantic_match_passed);
 #endif
-			trace_fib_table_lookup(tb->tb_id, flp, nhc, err);
+			trace_fib_table_lookup(tb->tb_id, flp, nhc, err, net);
 
 			return err;
 		}
Signature · 1 line
2.55.0
maintainer Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH net-next v2 2/3] ipv4: Pass netns to fib_table_lookup()

Quoted 16 lines · Ido Schimmel

The next patch will report the network namespace cookie in the fib_table_lookup tracepoint. As a preparation, pass the namespace to fib_table_lookup() and from there to the tracepoint, like the fib6_table_lookup tracepoint. All the callers already have the namespace at hand.

Add the namespace as the last tracepoint argument so that the positions of the existing arguments do not change and BPF programs that attach to the raw tracepoint are not affected.

No functional changes intended.

Quoted 11 lines
---
 include/net/ip_fib.h       | 12 +++++++-----
 include/trace/events/fib.h |  5 +++--
 net/core/filter.c          |  2 +-
 net/ipv4/devinet.c         |  3 ++-
 net/ipv4/fib_frontend.c    |  6 ++++--
 net/ipv4/fib_rules.c       |  2 +-
 net/ipv4/fib_semantics.c   |  2 +-
 net/ipv4/fib_trie.c        | 14 ++++++++------
 8 files changed, 27 insertions(+), 19 deletions(-)
author maintainer
#5 lore raw

PATCH v2 3/3 ipv4: Report netns cookie in fib_table_lookup tracepoint

In reply to Ido Schimmel

The fib_table_lookup tracepoint does not report the network namespace in which the lookup was performed, making it impossible to tell apart lookups performed in different namespaces.

Report the network namespace cookie now that the namespace is passed to the tracepoint.

---

Notes:
Sashiko: There is a pre-existing issue here regarding an uninitialized gateway address. I will take care of it separately.

 include/trace/events/fib.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/trace/events/fib.h +6 -2
index 341905b5edf3..e0ee14d6ca45 100644
@@ -36,6 +36,7 @@ TRACE_EVENT(fib_table_lookup,
 		__field(	u16,	sport		)
 		__field(	u16,	dport		)
 		__array(char,  name,   IFNAMSIZ )
+		__field(	u64,	net_cookie	)
 	),
 
 	TP_fast_assign(
@@ -91,13 +92,16 @@ TRACE_EVENT(fib_table_lookup,
 			in6 = (struct in6_addr *)__entry->gw6;
 			*in6 = in6addr_any;
 		}
+
+		__entry->net_cookie = net->net_cookie;
 	),
 
-	TP_printk("table %u oif %d iif %d proto %u %pI4/%u -> %pI4/%u tos %d scope %d flags %x ==> dev %s gw %pI4/%pI6c err %d",
+	TP_printk("table %u oif %d iif %d proto %u %pI4/%u -> %pI4/%u tos %d scope %d flags %x ==> dev %s gw %pI4/%pI6c err %d net_cookie %llu",
 		  __entry->tb_id, __entry->oif, __entry->iif, __entry->proto,
 		  __entry->src, __entry->sport, __entry->dst, __entry->dport,
 		  __entry->tos, __entry->scope, __entry->flags,
-		  __entry->name, __entry->gw4, __entry->gw6, __entry->err)
+		  __entry->name, __entry->gw4, __entry->gw6, __entry->err,
+		  __entry->net_cookie)
 );
 #endif /* _TRACE_FIB_H */
 
Signature · 1 line
2.55.0
maintainer Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH net-next v2 3/3] ipv4: Report netns cookie in fib_table_lookup tracepoint

Quoted 11 lines · Ido Schimmel

The fib_table_lookup tracepoint does not report the network namespace in which the lookup was performed, making it impossible to tell apart lookups performed in different namespaces.

Report the network namespace cookie now that the namespace is passed to the tracepoint.

Quoted 9 lines

---

Notes:
Sashiko: There is a pre-existing issue here regarding an uninitialized gateway address. I will take care of it separately.

 include/trace/events/fib.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
maintainer
#7 lore raw
In reply to Ido Schimmel
Quoted 20 lines · Ido Schimmel

The fib:fib_table_lookup and fib6:fib6_table_lookup tracepoints do not report the network namespace in which the lookup was performed, so lookups performed in different namespaces cannot be told apart. The recorded PID does not help either, as lookups in the receive path are performed in softIRQ context.

This is a problem, for example, for netns-aware tracers [1] and multi-ASIC systems where each ASIC and its ports reside in a separate network namespace.

This patchset reports the network namespace cookie in both tracepoints, as commit 27cb3de7f43a ("net: add net cookie for net device trace events") did for the net device tracepoints. This allows filtering lookups performed in a specific network namespace, for example:

  # perf record -a -e fib:fib_table_lookup --filter 'net_cookie == 12'

The cookie of a given network namespace can be retrieved using "ip netns cookie" [2].

Quoted 7 lines

Patch #1 reports the cookie in the IPv6 tracepoint, which is already passed the network namespace.

Patch #2 passes the network namespace to fib_table_lookup() and from there to the IPv4 tracepoint.

Patch #3 reports the cookie in the IPv4 tracepoint.

Not intended to block the above (could be eventually a follow-up), but I'm wondering if you have considered adding a net (or net_cookie) field to the fib_table struct instead of passing the net as an additional argument to the lookup functions - AFAICS the latter are also called in critical path and the additional arg could be unneeded (very small) overhead.

/P

author maintainer
#8 lore raw
Quoted 5 lines · Paolo Abeni

Not intended to block the above (could be eventually a follow-up), but I'm wondering if you have considered adding a net (or net_cookie) field to the fib_table struct instead of passing the net as an additional argument to the lookup functions - AFAICS the latter are also called in critical path and the additional arg could be unneeded (very small) overhead.

Makes sense.

I tried to be consistent with IPv6, but fib6_table_lookup() needs the netns argument regardless of the tracepoint. The fib_table struct still fits in a single cache line with the new field. Will send the patches tomorrow.

#9 lore raw
In reply to Ido Schimmel
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 11 lines · On Thu, 8 Oct 2026 16:31:55 +0300 you wrote:

The fib:fib_table_lookup and fib6:fib6_table_lookup tracepoints do not report the network namespace in which the lookup was performed, so lookups performed in different namespaces cannot be told apart. The recorded PID does not help either, as lookups in the receive path are performed in softIRQ context.

This is a problem, for example, for netns-aware tracers [1] and multi-ASIC systems where each ASIC and its ports reside in a separate network namespace.

[...]

Here is the summary with links:
- [net-next,v2,1/3] ipv6: Report netns cookie in fib6_table_lookup tracepoint

    https://git.kernel.org/netdev/net-next/c/ffe378c06c10
  - [net-next,v2,2/3] ipv4: Pass netns to fib_table_lookup()
    https://git.kernel.org/netdev/net-next/c/c436a624f89e
  - [net-next,v2,3/3] ipv4: Report netns cookie in fib_table_lookup tracepoint
    https://git.kernel.org/netdev/net-next/c/a780f51d5bc8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html