netdevsim: Add support for ethtool stats and add
Subsystems (6)
cover netdevsim: Add support for ethtool stats and add
This series adds support for querying standard interface stats and driver-specific stats with ethtool -S. This allows hardware-independent testing of ethtool stats reporting. Driver-specific stats are incremented every 100ms once enabled through a debugfs toggle.
Also adds a selftest for ethtool -S for netdevsim.
The implementation of mock stats is heavily based on the mock L3 stats support added by commit 1a6d7ae7d63c45("netdevsim: Introduce support for L3 offload xstats").
Note: Further replies will come from my school email address, [email protected], as I will soon lose access to my Red Hat email.
Dennis Chen (2):
netdevsim: Add mock stats for ethtool
selftests: netdevsim: Add test for ethtool stats
Kamal Heib (1):
netdevsim: Support ethtool stats
drivers/net/netdevsim/ethtool.c | 183 ++++++++++++++++++ drivers/net/netdevsim/netdev.c | 1 + drivers/net/netdevsim/netdevsim.h | 11 ++ .../selftests/drivers/net/netdevsim/Makefile | 1 + .../drivers/net/netdevsim/ethtool-common.sh | 13 ++ .../drivers/net/netdevsim/ethtool-stats.sh | 36 ++++ 6 files changed, 245 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/ethtool-stats.sh
Signature · 1 line
2.50.1
PATCH 1/3 netdevsim: Support ethtool stats
From: Kamal Heib <[email protected]>
Add support for reporting the netdevsim stats via ethtool.
--- drivers/net/netdevsim/ethtool.c | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
drivers/net/netdevsim/ethtool.c +58 -0
@@ -7,6 +7,26 @@#include "netdevsim.h" +struct nsim_stat_desc { +char desc[ETH_GSTRING_LEN ]; +size_t offset; +}; + +#define NSIM_STAT_ENTRY (s) { \ + .desc = #s, \ + .offset = offsetof(struct rtnl_link_stats64, s) } + +static const struct nsim_stat_desc nsim_stats_desc[] = { +NSIM_STAT_ENTRY (tx_packets), +NSIM_STAT_ENTRY (rx_packets), +NSIM_STAT_ENTRY (tx_bytes), +NSIM_STAT_ENTRY (rx_bytes), +NSIM_STAT_ENTRY (tx_dropped), +NSIM_STAT_ENTRY (rx_dropped), +}; + +#define NSIM_STATS_LEN ARRAY_SIZE(nsim_stats_desc) +static void nsim_get_pause_stats (struct net_device *dev,struct ethtool_pause_stats *pause_stats)
@@ -182,6 +202,41 @@ static int nsim_get_ts_info(struct net_device *dev,return 0 ; } +static int nsim_sset_count (struct net_device *dev,int sset) +{ +switch (sset) { +case ETH_SS_STATS : +return NSIM_STATS_LEN ; +default : +return -EOPNOTSUPP ; + } +} + +static void nsim_get_strings (struct net_device *dev,u32 sset,u8 *data) +{ +int i; + +switch (sset) { +case ETH_SS_STATS : +for (i =0 ; i <NSIM_STATS_LEN ; i++) +ethtool_puts (&data, nsim_stats_desc[i].desc); +break ; + } +} + +static void nsim_get_ethtool_stats (struct net_device *dev, +struct ethtool_stats *stats, +u64 *data) +{ +struct rtnl_link_stats64 rtstats = {}; +int i; + +dev_get_stats (dev, &rtstats); + +for (i =0 ; i <NSIM_STATS_LEN ; i++) + data[i] = *(u64 *)((u8 *)&rtstats + nsim_stats_desc[i].offset); +} +static const struct ethtool_ops nsim_ethtool_ops = { .supported_coalesce_params =ETHTOOL_COALESCE_ALL_PARAMS , .supported_ring_params = ETHTOOL_RING_USE_TCP_DATA_SPLIT |
@@ -199,6 +254,9 @@ static const struct ethtool_ops nsim_ethtool_ops = { .set_fecparam = nsim_set_fecparam, .get_fec_stats = nsim_get_fec_stats, .get_ts_info = nsim_get_ts_info, + .get_sset_count = nsim_sset_count, + .get_strings = nsim_get_strings, + .get_ethtool_stats = nsim_get_ethtool_stats, };static void nsim_ethtool_ring_init (struct netdevsim *ns)
Signature · 1 line
2.50.1
PATCH 2/3 netdevsim: Add mock stats for ethtool
Add mock stats exposed through ethtool -S for netdevsim. The stats are incremented every 100ms. Mock stats are enabled/disabled through a debugfs toggle.
# echo y > /sys/kernel/debug/netdevsim/$DEV/ports/0/ethtool/mock_stats/enabled
--- drivers/net/netdevsim/ethtool.c | 127 +++++++++++++++++++++++++++++- drivers/net/netdevsim/netdev.c | 1 + drivers/net/netdevsim/netdevsim.h | 11 +++ 3 files changed, 138 insertions(+), 1 deletion(-)
drivers/net/netdevsim/ethtool.c +126 -1
@@ -16,6 +16,10 @@ struct nsim_stat_desc { .desc = #s, \ .offset = offsetof(struct rtnl_link_stats64, s) } +#define NSIM_MOCK_STAT_ENTRY (s) { \ + .desc = #s, \ + .offset = offsetof(struct nsim_mock_stats, s) } +static const struct nsim_stat_desc nsim_stats_desc[] = {NSIM_STAT_ENTRY (tx_packets),NSIM_STAT_ENTRY (rx_packets),
@@ -27,6 +31,14 @@ static const struct nsim_stat_desc nsim_stats_desc[] = {#define NSIM_STATS_LEN ARRAY_SIZE(nsim_stats_desc) +#define NSIM_MOCK_STATS_LEN ARRAY_SIZE(nsim_mock_stats_desc) + +static const struct nsim_stat_desc nsim_mock_stats_desc[] = { +NSIM_MOCK_STAT_ENTRY (hw_out_of_sequence), +NSIM_MOCK_STAT_ENTRY (hw_out_of_buffer), +NSIM_MOCK_STAT_ENTRY (hw_packet_seq_err), +}; +static void nsim_get_pause_stats (struct net_device *dev,struct ethtool_pause_stats *pause_stats)
@@ -204,9 +216,12 @@ static int nsim_get_ts_info(struct net_device *dev,static int nsim_sset_count (struct net_device *dev,int sset) { +struct netdevsim *ns =netdev_priv (dev); +switch (sset) {case ETH_SS_STATS : -return NSIM_STATS_LEN ; +return ns->ethtool.mock_stats.enabled ? +NSIM_STATS_LEN +NSIM_MOCK_STATS_LEN :NSIM_STATS_LEN ;default :return -EOPNOTSUPP ; }
@@ -214,20 +229,51 @@ static int nsim_sset_count(struct net_device *dev, int sset)static void nsim_get_strings (struct net_device *dev,u32 sset,u8 *data) { +struct netdevsim *ns =netdev_priv (dev); +int i;switch (sset) {case ETH_SS_STATS :for (i =0 ; i <NSIM_STATS_LEN ; i++)ethtool_puts (&data, nsim_stats_desc[i].desc); +if (ns->ethtool.mock_stats.enabled) +for (i =0 ; i <NSIM_MOCK_STATS_LEN ; i++) +ethtool_puts (&data, + nsim_mock_stats_desc[i].desc); +break ; } } +static void nsim_ethtool_add_mock_stats (struct netdevsim *ns, +u64 *data) +{ +unsigned int start, i; +const u8 *stats_base; +const u64_stats_t *p; +size_t offset; + + stats_base = (const u8 *)&ns->ethtool.mock_stats; + + data +=NSIM_STATS_LEN ; + +do { + start =u64_stats_fetch_begin (&ns->ethtool.mock_stats.syncp); +for (i =0 ; i <NSIM_MOCK_STATS_LEN ; i++) { + offset = nsim_mock_stats_desc[i].offset; + + p = (const u64_stats_t *)(stats_base + offset); + data[i] =u64_stats_read (p); + } + }while (u64_stats_fetch_retry (&ns->ethtool.mock_stats.syncp, start)); +} +static void nsim_get_ethtool_stats (struct net_device *dev,struct ethtool_stats *stats,u64 *data) { +struct netdevsim *ns;struct rtnl_link_stats64 rtstats = {};int i;
@@ -235,6 +281,33 @@ static void nsim_get_ethtool_stats(struct net_device *dev,for (i =0 ; i <NSIM_STATS_LEN ; i++) data[i] = *(u64 *)((u8 *)&rtstats + nsim_stats_desc[i].offset); + + ns =netdev_priv (dev); + +if (ns->ethtool.mock_stats.enabled) +nsim_ethtool_add_mock_stats (ns, data); +} + +#define NSIM_MOCK_STATS_INTERVAL_MS 100 + +static void nsim_mock_stats_traffic_bump (struct nsim_mock_stats *stats) +{ +if (stats->enabled) { + stats->hw_out_of_buffer +=1 ; + stats->hw_out_of_sequence +=1 ; + stats->hw_packet_seq_err +=1 ; + } +} + +static void nsim_mock_stats_traffic_work (struct work_struct *work) +{ +struct nsim_mock_stats *stats; + + stats = container_of(work, struct nsim_mock_stats, traffic_dw.work); +nsim_mock_stats_traffic_bump (stats); + +schedule_delayed_work (&stats->traffic_dw, +msecs_to_jiffies (NSIM_MOCK_STATS_INTERVAL_MS )); }static const struct ethtool_ops nsim_ethtool_ops = {
@@ -269,6 +342,44 @@ static void nsim_ethtool_ring_init(struct netdevsim *ns) ns->ethtool.ring.tx_max_pending =4096 ; } +static void mock_stats_reset (struct nsim_mock_stats *mock_stats) +{ + mock_stats->hw_out_of_buffer =0 ; + mock_stats->hw_out_of_sequence =0 ; + mock_stats->hw_packet_seq_err =0 ; +} + +static ssize_t mock_stats_enabled_write (struct file *filp, +const char __user *ubuf, +size_t count, +loff_t *offp) +{ +bool enabled; +int r; +struct nsim_mock_stats *mock_stats = filp->private_data; +struct dentry *dentry = filp->f_path.dentry; + + r =kstrtobool_from_user (ubuf, count, &enabled); +if (!r) { + r =debugfs_file_get (dentry); +if (unlikely (r)) +return r; + + mock_stats->enabled = enabled; +if (!enabled) +mock_stats_reset (mock_stats); + +debugfs_file_put (dentry); + } + +return count; +} + +static struct debugfs_short_fops mock_stats_fops = { + .write = mock_stats_enabled_write, + .llseek = generic_file_llseek +}; +void nsim_ethtool_init (struct netdevsim *ns) {struct dentry *ethtool, *dir;
@@ -305,4 +416,18 @@ void nsim_ethtool_init(struct netdevsim *ns) &ns->ethtool.ring.rx_mini_max_pending);debugfs_create_u32 ("tx_max_pending" ,0600 , dir, &ns->ethtool.ring.tx_max_pending); + + dir =debugfs_create_dir ("mock_stats" , ethtool); +debugfs_create_file ("enabled" ,0600 , dir, &ns->ethtool.mock_stats, + &mock_stats_fops); + +INIT_DELAYED_WORK (&ns->ethtool.mock_stats.traffic_dw, + &nsim_mock_stats_traffic_work); +schedule_delayed_work (&ns->ethtool.mock_stats.traffic_dw, +msecs_to_jiffies (NSIM_MOCK_STATS_INTERVAL_MS )); +} + +void nsim_ethtool_exit (struct netdevsim *ns) +{ +cancel_delayed_work_sync (&ns->ethtool.mock_stats.traffic_dw); }
drivers/net/netdevsim/netdev.c +1 -0
@@ -1143,6 +1143,7 @@ void nsim_destroy(struct netdevsim *ns)rtnl_unlock ();if (nsim_dev_port_is_pf (ns->nsim_dev_port))nsim_exit_netdevsim (ns); +nsim_ethtool_exit (ns);/* Put this intentionally late to exercise the orphaning path */ if (ns->page) {
drivers/net/netdevsim/netdevsim.h +11 -0
@@ -82,6 +82,15 @@ struct nsim_ethtool_pauseparam {bool report_stats_tx; }; +struct nsim_mock_stats { +u64 hw_out_of_sequence; +u64 hw_out_of_buffer; +u64 hw_packet_seq_err; +struct u64_stats_sync syncp; +struct delayed_work traffic_dw; +bool enabled; +}; +struct nsim_ethtool {u32 get_err;u32 set_err;
@@ -90,6 +99,7 @@ struct nsim_ethtool {struct ethtool_coalesce coalesce;struct ethtool_ringparam ring;struct ethtool_fecparam fec; +struct nsim_mock_stats mock_stats; };struct nsim_rq {
@@ -150,6 +160,7 @@ void nsim_destroy(struct netdevsim *ns);bool netdev_is_nsim (struct net_device *dev);void nsim_ethtool_init (struct netdevsim *ns); +void nsim_ethtool_exit (struct netdevsim *ns);void nsim_udp_tunnels_debugfs_create (struct nsim_dev *nsim_dev); int nsim_udp_tunnels_info_create(struct nsim_dev *nsim_dev,
Signature · 1 line
2.50.1
PATCH 3/3 selftests: netdevsim: Add test for ethtool stats
Add a test that verifies ethtool correctly exposes driver-specific stats.
--- .../selftests/drivers/net/netdevsim/Makefile | 1 + .../drivers/net/netdevsim/ethtool-common.sh | 13 +++++++ .../drivers/net/netdevsim/ethtool-stats.sh | 36 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/ethtool-stats.sh
tools/testing/selftests/drivers/net/netdevsim/Makefile +1 -0
@@ -8,6 +8,7 @@ TEST_PROGS = devlink.sh \ ethtool-fec.sh \ ethtool-pause.sh \ ethtool-ring.sh \ + ethtool-stats.sh \ fib.sh \ fib_notifications.sh \ hw_stats_l3.sh \
tools/testing/selftests/drivers/net/netdevsim/ethtool-common.sh +13 -0
@@ -42,6 +42,19 @@ function check { ((num_passes++)) } +function check_code { + local code=$1 + local msg=$2 + + if ((err)); then + echo -e $msg + ((num_errors++)) + return + fi + + ((num_passes++)) +} + function make_netdev { # Make a netdevsim old_netdevs=$(ls /sys/class/net)
tools/testing/selftests/drivers/net/netdevsim/ethtool-stats.sh new file +36 -0
@@ -0,0 +1,36 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +source ethtool-common.sh + +set -o pipefail + +NSIM_NETDEV=$(make_netdev) +MOCK_STATS_DFS=$NSIM_DEV_DFS/ethtool/mock_stats/enabled + +echo y > $MOCK_STATS_DFS + +stat=$(ethtool -S $NSIM_NETDEV | grep "hw_out_of_buffer" | awk '{print $2}') +((stat == 0)) +check_code $? "ethtool stats show > 0 packets immediately after enabling" + +sleep 2.5 + +stat=$(ethtool -S $NSIM_NETDEV | grep "hw_out_of_buffer" | awk '{print $2}') +((stat >= 20)) +check_code $? "ethtool stats show < 20 packets after 2.5s passed" + +echo n > $MOCK_STATS_DFS +echo y > $MOCK_STATS_DFS + +stat=$(ethtool -S $NSIM_NETDEV | grep "hw_out_of_buffer" | awk '{print $2}') +((stat == 0)) +check_code $? "ethtool stats show > 0 packets after disabling and re-enabling" + +if [ $num_errors -eq 0 ]; then + echo "PASSED all $((num_passes)) checks" + exit 0 +else + echo "FAILED $num_errors/$((num_errors+num_passes)) checks" + exit 1 +fi
Signature · 1 line
2.50.1
Jakub Kicinski
maintainerQuoted 5 lines · On Tue, 5 Aug 2025 17:33:53 -0400 Dennis Chen wrote:
This series adds support for querying standard interface stats and driver-specific stats with ethtool -S. This allows hardware-independent testing of ethtool stats reporting. Driver-specific stats are incremented every 100ms once enabled through a debugfs toggle.
Quoted 8 lines
Also adds a selftest for ethtool -S for netdevsim.
The implementation of mock stats is heavily based on the mock L3 stats support added by commit 1a6d7ae7d63c45("netdevsim: Introduce support for L3 offload xstats").
Note: Further replies will come from my school email address, [email protected], as I will soon lose access to my Red Hat email.
The tests for netdevsim must test something meaningful in the kernel. This submission really looks like you need it to test some user space code.
Signature · 1 line
pw-bot: reject
Quoted 13 lines · Jakub Kicinski
Quoted 11 lines · On Tue, 5 Aug 2025 17:33:53 -0400 Dennis Chen wrote:
This series adds support for querying standard interface stats and driver-specific stats with ethtool -S. This allows hardware-independent testing of ethtool stats reporting. Driver-specific stats are incremented every 100ms once enabled through a debugfs toggle.
Also adds a selftest for ethtool -S for netdevsim.
The implementation of mock stats is heavily based on the mock L3 stats support added by commit 1a6d7ae7d63c45("netdevsim: Introduce support for L3 offload xstats").
Quoted 8 lines
Quoted 2 lines
Note: Further replies will come from my school email address, [email protected], as I will soon lose access to my Red Hat email.
The tests for netdevsim must test something meaningful in the kernel. This submission really looks like you need it to test some user space code.
--
pw-bot: reject
Hi Jakub,
This test would help verify that ethtool_ops correctly propagates stats to userspace, would that not be significant enough for a test?
My thought was that it would be similar to the patches for ethtool --show-phys here:
https://lore.kernel.org/netdev/[email protected]/
Thanks,
Dennis Chen
Jakub Kicinski
maintainerQuoted 10 lines · On Wed, 6 Aug 2025 10:05:27 -0400 Dennis Chen wrote:
Quoted 3 lines
The tests for netdevsim must test something meaningful in the kernel. This submission really looks like you need it to test some user space code.
This test would help verify that
ethtool_opscorrectly propagates stats to userspace, would that not be significant enough for a test?My thought was that it would be similar to the patches for ethtool --show-phys here:
https://lore.kernel.org/netdev/[email protected]/
What are you trying to test. Like, what _actually_ made you and Kamal write these patches? I can't think of many bugs in the area. And you clearly have zero familiarity with our recommendation of what stats to report and how :/ so if anything your code is hurting more than helping.
Simon Horman
reviewerQuoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 6 Aug 2025 10:05:27 -0400 Dennis Chen wrote:
Quoted 3 lines
The tests for netdevsim must test something meaningful in the kernel. This submission really looks like you need it to test some user space code.
This test would help verify that
ethtool_opscorrectly propagates stats to userspace, would that not be significant enough for a test?My thought was that it would be similar to the patches for ethtool --show-phys here:
https://lore.kernel.org/netdev/[email protected]/
Quoted 5 lines
What are you trying to test. Like, what _actually_ made you and Kamal write these patches? I can't think of many bugs in the area. And you clearly have zero familiarity with our recommendation of what stats to report and how :/ so if anything your code is hurting more than helping.
Thanks Jakub,
I'll work with the team to find a better approach.