]> git.dujemihanovic.xyz Git - linux.git/commitdiff
bpf: Fix dev's rx stats for bpf_redirect_peer traffic
authorPeilin Ye <peilin.ye@bytedance.com>
Tue, 14 Nov 2023 00:42:17 +0000 (01:42 +0100)
committerMartin KaFai Lau <martin.lau@kernel.org>
Mon, 20 Nov 2023 18:15:16 +0000 (10:15 -0800)
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>
net/core/dev.c
net/core/filter.c

index 0cc6e283edba9b983c7b92c37ff755b04f1fbb64..c879246be48d82f83072a48b19ec073df8efe27b 100644 (file)
@@ -10055,6 +10055,14 @@ static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
 {
        void __percpu *v;
 
+       /* Drivers implementing ndo_get_peer_dev must support tstat
+        * accounting, so that skb_do_redirect() can bump the dev's
+        * RX stats upon network namespace switch.
+        */
+       if (dev->netdev_ops->ndo_get_peer_dev &&
+           dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS)
+               return -EOPNOTSUPP;
+
        switch (dev->pcpu_stat_type) {
        case NETDEV_PCPU_STAT_NONE:
                return 0;
index 383f96b0a1c78026629ef4e6b8c172019d29d35c..cca810987c8dc93d1addac4df86658e7da271b05 100644 (file)
@@ -2492,6 +2492,7 @@ int skb_do_redirect(struct sk_buff *skb)
                             net_eq(net, dev_net(dev))))
                        goto out_drop;
                skb->dev = dev;
+               dev_sw_netstats_rx_add(dev, skb->len);
                return -EAGAIN;
        }
        return flags & BPF_F_NEIGH ?