Skip to content

Commit 024ee93

Browse files
peilin-yeMartin KaFai Lau
authored andcommitted
bpf: Fix dev's rx stats for bpf_redirect_peer traffic
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]>
1 parent 6f2684b commit 024ee93

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
@@ -10055,6 +10055,14 @@ static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
1005510055
{
1005610056
void __percpu *v;
1005710057

10058+
/* Drivers implementing ndo_get_peer_dev must support tstat
10059+
* accounting, so that skb_do_redirect() can bump the dev's
10060+
* RX stats upon network namespace switch.
10061+
*/
10062+
if (dev->netdev_ops->ndo_get_peer_dev &&
10063+
dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS)
10064+
return -EOPNOTSUPP;
10065+
1005810066
switch (dev->pcpu_stat_type) {
1005910067
case NETDEV_PCPU_STAT_NONE:
1006010068
return 0;

net/core/filter.c

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

0 commit comments

Comments
 (0)