diff mbox series

[intel-next,2/2] net/i40e: add support for per queue netlink stats

Message ID 20240410043936.206169-3-jdamato@fastly.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series i40e: Add support for netlink API | expand

Checks

Context Check Description
netdev/series_format warning Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 942 this patch: 942
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 953 this patch: 953
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 No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 953 this patch: 953
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 126 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 209 this patch: 209
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-10--06-00 (tests: 962)

Commit Message

Joe Damato April 10, 2024, 4:39 a.m. UTC
Make i40e compatible with the newly added netlink per queue stats.

$ ./cli.py --spec ../../../Documentation/netlink/specs/netdev.yaml \
           --dump qstats-get --json '{"scope": "queue"}'

[{'ifindex': 3,
  'queue-id': 0,
  'queue-type': 'rx',
  'rx-alloc-fail': 0,
  'rx-bytes': 45540208,
  'rx-packets': 57112},
 {'ifindex': 3,
  'queue-id': 1,
  'queue-type': 'rx',
  'rx-alloc-fail': 0,
  'rx-bytes': 8717844,
  'rx-packets': 31256},
...

Comparing to ethtool to verify:

$ ethtool -S eth3 | egrep 'rx-[01]\.'
     rx-0.packets: 57112
     rx-0.bytes: 45540208
     rx-1.packets: 31256
     rx-1.bytes: 8717844

Signed-off-by: Joe Damato <jdamato@fastly.com>
---
 drivers/net/ethernet/intel/i40e/i40e_main.c | 102 ++++++++++++++++++++
 1 file changed, 102 insertions(+)
diff mbox series

Patch

diff --git a/drivers/net/ethernet/intel/i40e/i40e_main.c b/drivers/net/ethernet/intel/i40e/i40e_main.c
index 6384a0c73a05..40574f54a380 100644
--- a/drivers/net/ethernet/intel/i40e/i40e_main.c
+++ b/drivers/net/ethernet/intel/i40e/i40e_main.c
@@ -6,6 +6,7 @@ 
 #include <linux/if_bridge.h>
 #include <linux/if_macvlan.h>
 #include <linux/module.h>
+#include <net/netdev_queues.h>
 #include <net/pkt_cls.h>
 #include <net/xdp_sock_drv.h>
 
@@ -515,6 +516,100 @@  static void i40e_get_netdev_stats_struct(struct net_device *netdev,
 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
 }
 
+static void i40e_get_queue_stats_rx(struct net_device *dev, int i,
+				    struct netdev_queue_stats_rx *stats)
+{
+	struct i40e_netdev_priv *np = netdev_priv(dev);
+	struct i40e_vsi *vsi = np->vsi;
+	struct i40e_ring *ring;
+	unsigned int start;
+
+	stats->bytes = 0xff;
+	stats->packets = 0xff;
+	stats->alloc_fail = 0xff;
+
+	if (test_bit(__I40E_VSI_DOWN, vsi->state))
+		return;
+
+	if (!vsi->rx_rings)
+		return;
+
+	rcu_read_lock();
+	ring = READ_ONCE(vsi->rx_rings[i]);
+	if (ring) {
+		do {
+			start = u64_stats_fetch_begin(&ring->syncp);
+			stats->packets = ring->stats.packets;
+			stats->bytes = ring->stats.bytes;
+			stats->alloc_fail = ring->rx_stats.alloc_page_failed;
+		} while (u64_stats_fetch_retry(&ring->syncp, start));
+	}
+	rcu_read_unlock();
+}
+
+static void i40e_get_queue_stats_tx(struct net_device *dev, int i,
+				    struct netdev_queue_stats_tx *stats)
+{
+	struct i40e_netdev_priv *np = netdev_priv(dev);
+	struct i40e_vsi *vsi = np->vsi;
+	struct i40e_ring *ring;
+	unsigned int start;
+
+	stats->bytes = 0xff;
+	stats->packets = 0xff;
+
+	if (test_bit(__I40E_VSI_DOWN, vsi->state))
+		return;
+
+	if (!vsi->tx_rings)
+		return;
+
+	rcu_read_lock();
+	ring = READ_ONCE(vsi->tx_rings[i]);
+	if (ring) {
+		do {
+			start = u64_stats_fetch_begin(&ring->syncp);
+			stats->packets = ring->stats.packets;
+			stats->bytes = ring->stats.bytes;
+		} while (u64_stats_fetch_retry(&ring->syncp, start));
+	}
+	rcu_read_unlock();
+}
+
+static void i40e_get_base_stats(struct net_device *dev,
+				struct netdev_queue_stats_rx *rx,
+				struct netdev_queue_stats_tx *tx)
+{
+	struct i40e_netdev_priv *np = netdev_priv(dev);
+	struct rtnl_link_stats64 stats = {0};
+	struct i40e_vsi *vsi = np->vsi;
+
+	rx->bytes = 0xff;
+	rx->packets = 0xff;
+	rx->alloc_fail = 0xff;
+
+	tx->bytes = 0xff;
+	tx->packets = 0xff;
+
+	if (test_bit(__I40E_VSI_DOWN, vsi->state))
+		return;
+
+	if (!vsi->tx_rings)
+		return;
+
+	if (!vsi->num_queue_pairs)
+		return;
+
+	i40e_get_netdev_stats_struct(dev, &stats);
+
+	rx->bytes = stats.rx_bytes;
+	rx->packets = stats.rx_packets;
+	rx->alloc_fail = vsi->rx_buf_failed;
+
+	tx->bytes = stats.tx_bytes;
+	tx->packets = stats.tx_packets;
+}
+
 /**
  * i40e_vsi_reset_stats - Resets all stats of the given vsi
  * @vsi: the VSI to have its stats reset
@@ -13693,6 +13788,12 @@  static const struct net_device_ops i40e_netdev_ops = {
 	.ndo_dfwd_del_station	= i40e_fwd_del,
 };
 
+static const struct netdev_stat_ops i40e_stat_ops = {
+	.get_queue_stats_rx     = i40e_get_queue_stats_rx,
+	.get_queue_stats_tx     = i40e_get_queue_stats_tx,
+	.get_base_stats         = i40e_get_base_stats,
+};
+
 /**
  * i40e_config_netdev - Setup the netdev flags
  * @vsi: the VSI being configured
@@ -13854,6 +13955,7 @@  static int i40e_config_netdev(struct i40e_vsi *vsi)
 	/* Setup netdev TC information */
 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
 
+	netdev->stat_ops = &i40e_stat_ops;
 	netdev->netdev_ops = &i40e_netdev_ops;
 	netdev->watchdog_timeo = 5 * HZ;
 	i40e_set_ethtool_ops(netdev);