All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: martin.lau@kernel.org
Cc: kuba@kernel.org, razor@blackwall.org, sdf@google.com,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	Peilin Ye <peilin.ye@bytedance.com>,
	Youlun Zhang <zhangyoulun@bytedance.com>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH bpf v2 5/8] bpf: Fix dev's rx stats for bpf_redirect_peer traffic
Date: Sun, 12 Nov 2023 21:30:06 +0100	[thread overview]
Message-ID: <20231112203009.26073-6-daniel@iogearbox.net> (raw)
In-Reply-To: <20231112203009.26073-1-daniel@iogearbox.net>

From: Peilin Ye <peilin.ye@bytedance.com>

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>
---
 net/core/dev.c    | 8 ++++++++
 net/core/filter.c | 1 +
 2 files changed, 9 insertions(+)

diff --git a/net/core/dev.c b/net/core/dev.c
index 75db81496db5..5c9ab37298ac 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10053,6 +10053,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 -EINVAL;
+
 	switch (dev->pcpu_stat_type) {
 	case NETDEV_PCPU_STAT_NONE:
 		return 0;
diff --git a/net/core/filter.c b/net/core/filter.c
index 383f96b0a1c7..cca810987c8d 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -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 ?
-- 
2.34.1


  parent reply	other threads:[~2023-11-12 20:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-12 20:30 [PATCH bpf v2 0/8] bpf_redirect_peer fixes Daniel Borkmann
2023-11-12 20:30 ` [PATCH bpf v2 1/8] net, vrf: Move dstats structure to core Daniel Borkmann
2023-11-13  8:51   ` Nikolay Aleksandrov
2023-11-12 20:30 ` [PATCH bpf v2 2/8] net: Move {l,t,d}stats allocation to core and convert veth & vrf Daniel Borkmann
2023-11-13  8:52   ` Nikolay Aleksandrov
2023-11-13  9:57   ` Simon Horman
2023-11-13 13:04     ` Daniel Borkmann
2023-11-13 10:03   ` Simon Horman
2023-11-13 13:05     ` Daniel Borkmann
2023-11-13 16:15       ` Simon Horman
2023-11-12 20:30 ` [PATCH bpf v2 3/8] netkit: Add tstats per-CPU traffic counters Daniel Borkmann
2023-11-12 20:30 ` [PATCH bpf v2 4/8] veth: Use " Daniel Borkmann
2023-11-12 22:09   ` Peilin Ye
2023-11-12 22:12     ` Daniel Borkmann
2023-11-13  8:53   ` Nikolay Aleksandrov
2023-11-12 20:30 ` Daniel Borkmann [this message]
2023-11-13  8:54   ` [PATCH bpf v2 5/8] bpf: Fix dev's rx stats for bpf_redirect_peer traffic Nikolay Aleksandrov
2023-11-12 20:30 ` [PATCH bpf v2 6/8] bpf, netkit: Add indirect call wrapper for fetching peer dev Daniel Borkmann
2023-11-12 20:30 ` [PATCH bpf v2 7/8] selftests/bpf: De-veth-ize the tc_redirect test case Daniel Borkmann
2023-11-13  8:55   ` Nikolay Aleksandrov
2023-11-12 20:30 ` [PATCH bpf v2 8/8] selftests/bpf: Add netkit to tc_redirect selftest Daniel Borkmann
2023-11-13  8:55   ` Nikolay Aleksandrov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231112203009.26073-6-daniel@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=bpf@vger.kernel.org \
    --cc=kuba@kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=peilin.ye@bytedance.com \
    --cc=razor@blackwall.org \
    --cc=sdf@google.com \
    --cc=zhangyoulun@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.