lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 05 Jun 2024 14:37:29 +0800
From: Jeremy Kerr <jk@...econstruct.com.au>
To: David Ahern <dsahern@...nel.org>, 
 "David S. Miller" <davem@...emloft.net>, Eric Dumazet <edumazet@...gle.com>, 
 Jakub Kicinski <kuba@...nel.org>, Paolo Abeni <pabeni@...hat.com>
Cc: netdev@...r.kernel.org
Subject: [PATCH 2/3] net: core: Implement dstats-type stats collections

We currently have dev_get_tstats64() for collecting per-cpu stats of
type pcpu_sw_netstats ("tstats"). However, tstats doesn't allow for
accounting tx/rx drops. We do have a stats variant that does have stats
for dropped packets: struct pcpu_dstats, but there are no core helpers
for using those stats.

The VRF driver uses dstats, by providing its own collation/fetch
functions to do so.

This change adds a common helpers for dstats-type collection, based on
the VRF driver's own (plus the unused tx_drops stat from there). We
will switch the VRF driver to use this in the next change.

Signed-off-by: Jeremy Kerr <jk@...econstruct.com.au>
---
 include/linux/netdevice.h |  3 +++
 net/core/dev.c            | 56 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index f148a01dd1d1..fdc3d8a6c0f4 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -4690,6 +4690,9 @@ void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
 void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
 			   const struct pcpu_sw_netstats __percpu *netstats);
 void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s);
+void dev_fetch_dstats(struct rtnl_link_stats64 *s,
+		      const struct pcpu_dstats __percpu *dstats);
+void dev_get_dstats64(struct net_device *dev, struct rtnl_link_stats64 *s);
 
 enum {
 	NESTED_SYNC_IMM_BIT,
diff --git a/net/core/dev.c b/net/core/dev.c
index e1bb6d7856d9..dc77529600ce 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10849,6 +10849,62 @@ void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
 }
 EXPORT_SYMBOL_GPL(dev_get_tstats64);
 
+/**
+ *	dev_fetch_dstats - collate per-cpu network dstats statistics
+ *	@s: place to store stats
+ *	@dstats: per-cpu network stats to read from
+ *
+ *	Read per-cpu network statistics from dev->dstats and populate the
+ *	related fields in @s.
+ */
+void dev_fetch_dstats(struct rtnl_link_stats64 *s,
+		      const struct pcpu_dstats __percpu *dstats)
+{
+	int cpu;
+
+	for_each_possible_cpu(cpu) {
+		u64 rx_packets, rx_bytes, rx_drops;
+		u64 tx_packets, tx_bytes, tx_drops;
+		const struct pcpu_dstats *dstats;
+		unsigned int start;
+
+		dstats = per_cpu_ptr(dstats, cpu);
+		do {
+			start = u64_stats_fetch_begin(&dstats->syncp);
+			rx_packets = u64_stats_read(&dstats->rx_packets);
+			rx_bytes   = u64_stats_read(&dstats->rx_bytes);
+			rx_drops   = u64_stats_read(&dstats->rx_drops);
+			tx_packets = u64_stats_read(&dstats->tx_packets);
+			tx_bytes   = u64_stats_read(&dstats->tx_bytes);
+			tx_drops   = u64_stats_read(&dstats->tx_drops);
+		} while (u64_stats_fetch_retry(&dstats->syncp, start));
+
+		s->rx_packets += rx_packets;
+		s->rx_bytes   += rx_bytes;
+		s->rx_dropped += rx_drops;
+		s->tx_packets += tx_packets;
+		s->tx_bytes   += tx_bytes;
+		s->tx_dropped += tx_drops;
+	}
+}
+EXPORT_SYMBOL_GPL(dev_fetch_dstats);
+
+/**
+ *	dev_get_dstats64 - ndo_get_stats64 implementation for dtstats-based
+ *	account.
+ *	@dev: device to get statistics from
+ *	@s: place to store stats
+ *
+ *	Populate @s from dev->stats and dev->dstats. Can be used as
+ *	ndo_get_stats64() callback.
+ */
+void dev_get_dstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
+{
+	netdev_stats_to_stats64(s, &dev->stats);
+	dev_fetch_dstats(s, dev->dstats);
+}
+EXPORT_SYMBOL_GPL(dev_get_dstats64);
+
 struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
 {
 	struct netdev_queue *queue = dev_ingress_queue(dev);

-- 
2.39.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ