Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 9aa1206e8f48 ("bpf: Add redirect_peer helper")
Reported-by: Youlun Zhang <zhangyoulun@bytedance.com>
Signed-off-by: Peilin Ye <peilin.ye@bytedance.com>
Co-developed-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Link: https://lore.kernel.org/r/20231114004220.6495-6-daniel@iogearbox.net
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

authored by

Peilin Ye and committed by
Martin KaFai Lau
024ee930 6f2684bf

+9
+8
net/core/dev.c
··· 10055 10055 { 10056 10056 void __percpu *v; 10057 10057 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 + 10058 10066 switch (dev->pcpu_stat_type) { 10059 10067 case NETDEV_PCPU_STAT_NONE: 10060 10068 return 0;
+1
net/core/filter.c
··· 2492 2492 net_eq(net, dev_net(dev)))) 2493 2493 goto out_drop; 2494 2494 skb->dev = dev; 2495 + dev_sw_netstats_rx_add(dev, skb->len); 2495 2496 return -EAGAIN; 2496 2497 } 2497 2498 return flags & BPF_F_NEIGH ?