diff mbox series

[net,v2] tun: add missing rx stats accounting in tun_xdp_act

Message ID 1705409818-28292-1-git-send-email-wangyunjian@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net,v2] tun: add missing rx stats accounting in tun_xdp_act | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1080 this patch: 1080
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang fail Errors and warnings before: 1095 this patch: 1101
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1095 this patch: 1095
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 31 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

wangyunjian Jan. 16, 2024, 12:56 p.m. UTC
There are few places on the receive path where packet receives and packet
drops were not accounted for. This patch fixes that issue.

Fixes: 8ae1aff0b331 ("tuntap: split out XDP logic")
Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
---
v2: add Fixes tag
---
 drivers/net/tun.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

Willem de Bruijn Jan. 16, 2024, 3:04 p.m. UTC | #1
Yunjian Wang wrote:
> There are few places on the receive path where packet receives and packet
> drops were not accounted for. This patch fixes that issue.
> 
> Fixes: 8ae1aff0b331 ("tuntap: split out XDP logic")

Before this commit this_cpu_inc(tun->pcpu_stats->rx_dropped) would
get called, so that is indeed a regression in that patch. Please add
that to the commit message.

This commit also adds new accounting of successfully received bytes
with dev_sw_netstats_rx_add. I don't know off the top of my head if
other devices account XDP_TX and XDP_REDIRECT in that counter. Either
way, good to be explicit about such subtle details. Oddly, before
8ae1aff0b331 those, too, would be attributed to rx_dropped, because
after the out: label.

> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
> v2: add Fixes tag
> ---
>  drivers/net/tun.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index afa5497f7c35..232e5319ac77 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1626,17 +1626,14 @@ static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
>  		       struct xdp_buff *xdp, u32 act)
>  {
>  	int err;
> +	unsigned int datasize = xdp->data_end - xdp->data;
>  
>  	switch (act) {
>  	case XDP_REDIRECT:
>  		err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
> -		if (err)
> -			return err;
>  		break;
>  	case XDP_TX:
>  		err = tun_xdp_tx(tun->dev, xdp);
> -		if (err < 0)
> -			return err;
>  		break;
>  	case XDP_PASS:
>  		break;

err is uninitialized in this case?

> @@ -1651,6 +1648,13 @@ static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
>  		break;
>  	}
>  
> +	if (err < 0) {
> +		act = err;
> +		dev_core_stats_rx_dropped_inc(tun->dev);
> +	} else if (act == XDP_REDIRECT || act == XDP_TX) {
> +		dev_sw_netstats_rx_add(tun->dev, datasize);
> +	}
> +
>  	return act;
>  }
>  
> -- 
> 2.41.0
>
Simon Horman Jan. 16, 2024, 7:27 p.m. UTC | #2
On Tue, Jan 16, 2024 at 08:56:58PM +0800, Yunjian Wang wrote:
> There are few places on the receive path where packet receives and packet
> drops were not accounted for. This patch fixes that issue.
> 
> Fixes: 8ae1aff0b331 ("tuntap: split out XDP logic")
> Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
> ---
> v2: add Fixes tag
> ---
>  drivers/net/tun.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index afa5497f7c35..232e5319ac77 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1626,17 +1626,14 @@ static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
>  		       struct xdp_buff *xdp, u32 act)
>  {
>  	int err;
> +	unsigned int datasize = xdp->data_end - xdp->data;

nit: if you post a v3 for some other reason then, as this is Networking
     code, please consider arranging local variables in reverse xmas tree
     order - longest line to shortest.

	unsigned int datasize = xdp->data_end - xdp->data;
	int err;

...
diff mbox series

Patch

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index afa5497f7c35..232e5319ac77 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1626,17 +1626,14 @@  static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
 		       struct xdp_buff *xdp, u32 act)
 {
 	int err;
+	unsigned int datasize = xdp->data_end - xdp->data;
 
 	switch (act) {
 	case XDP_REDIRECT:
 		err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
-		if (err)
-			return err;
 		break;
 	case XDP_TX:
 		err = tun_xdp_tx(tun->dev, xdp);
-		if (err < 0)
-			return err;
 		break;
 	case XDP_PASS:
 		break;
@@ -1651,6 +1648,13 @@  static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
 		break;
 	}
 
+	if (err < 0) {
+		act = err;
+		dev_core_stats_rx_dropped_inc(tun->dev);
+	} else if (act == XDP_REDIRECT || act == XDP_TX) {
+		dev_sw_netstats_rx_add(tun->dev, datasize);
+	}
+
 	return act;
 }