Skip to content

Commit 8e7b530

Browse files
peilin-yegregkh
authored andcommitted
bpf: Fix dev's rx stats for bpf_redirect_peer traffic
[ Upstream commit 024ee930cb3c9ae49e4266aee89cfde0ebb407e1 ] Traffic redirected by bpf_redirect_peer() (used by recent CNIs like Cilium) is not accounted for in the RX stats of supported devices (that is, veth and netkit), confusing user space metrics collectors such as cAdvisor [0], as reported by Youlun. Fix it by calling dev_sw_netstats_rx_add() in skb_do_redirect(), to update RX traffic counters. Devices that support ndo_get_peer_dev _must_ use the @tstats per-CPU counters (instead of @lstats, or @dstats). To make this more fool-proof, error out when ndo_get_peer_dev is set but @tstats are not selected. [0] Specifically, the "container_network_receive_{byte,packet}s_total" counters are affected. Fixes: 9aa1206 ("bpf: Add redirect_peer helper") Reported-by: Youlun Zhang <[email protected]> Signed-off-by: Peilin Ye <[email protected]> Co-developed-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Nikolay Aleksandrov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 877c81f commit 8e7b530

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

net/core/dev.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9995,6 +9995,14 @@ static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
99959995
{
99969996
void __percpu *v;
99979997

9998+
/* Drivers implementing ndo_get_peer_dev must support tstat
9999+
* accounting, so that skb_do_redirect() can bump the dev's
10000+
* RX stats upon network namespace switch.
10001+
*/
10002+
if (dev->netdev_ops->ndo_get_peer_dev &&
10003+
dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS)
10004+
return -EOPNOTSUPP;
10005+
999810006
switch (dev->pcpu_stat_type) {
999910007
case NETDEV_PCPU_STAT_NONE:
1000010008
return 0;

net/core/filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,7 @@ int skb_do_redirect(struct sk_buff *skb)
24912491
net_eq(net, dev_net(dev))))
24922492
goto out_drop;
24932493
skb->dev = dev;
2494+
dev_sw_netstats_rx_add(dev, skb->len);
24942495
return -EAGAIN;
24952496
}
24962497
return flags & BPF_F_NEIGH ?

0 commit comments

Comments
 (0)