From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=XLSw4kk01QDTlGyCDpjTbXYryNnPNFLUaBj1auxatEE=; b=LduJxkBQrSrNHSKIi9bNM0SVbJKluwGhNNT2aFEdJAIcXQtnCSqfTLw3qowTIv7B0k Y2sdAA3ItnlB0Eyk9/Y+cX69aGW3HCYVWUtXlTAPogCWeG65EVyUOxvhsxvbLSgI/FXL 1d2O6uk17pOmZozEIZASGi68BNeS0t94JAaVzmR0q7RaZTWdzRwxQ+2YVQ6SpOxOtYoa ogNJ3+MSqbpHQTIjYuj1zw3TFiNd4cDtzgkCvs8x6LTP/Cq33YL4P3S5oSfgqYivBiif gOykekdoeZpwX+yv2b5ceMCRDqgaPBLtKCCviCqQ/ZVVC+f568e5/iu/8ArkXLwT4h0I CsTQ== From: Heiner Kallweit References: Message-ID: <6d16a338-52f5-df69-0020-6bc771a7d498@gmail.com> Date: Mon, 12 Oct 2020 10:01:27 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: [Bridge] [PATCH net-next v2 01/12] net: add function dev_fetch_sw_netstats for fetching pcpu_sw_netstats List-Id: Linux Ethernet Bridging List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: David Miller , Jakub Kicinski , =?UTF-8?Q?Bj=c3=b8rn_Mork?= , Oliver Neukum , Igor Mitsyanko , Sergey Matyukevich , Kalle Valo , Roopa Prabhu , Nikolay Aleksandrov , Andrew Lunn , Vivien Didelot , Florian Fainelli , Vladimir Oltean , Alexey Kuznetsov , Hideaki YOSHIFUJI , Johannes Berg , Pravin B Shelar , Steffen Klassert , Herbert Xu Cc: linux-rdma@vger.kernel.org, "netdev@vger.kernel.org" , Linux USB Mailing List , linux-wireless , bridge@lists.linux-foundation.org In several places the same code is used to populate rtnl_link_stats64 fields with data from pcpu_sw_netstats. Therefore factor out this code to a new function dev_fetch_sw_netstats(). v2: - constify argument netstats - don't ignore netstats being NULL or an ERRPTR - switch to EXPORT_SYMBOL_GPL Signed-off-by: Heiner Kallweit --- include/linux/netdevice.h | 2 ++ net/core/dev.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index a0df43b13..fa1d8d624 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -4495,6 +4495,8 @@ struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, struct rtnl_link_stats64 *storage); void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, const struct net_device_stats *netdev_stats); +void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, + const struct pcpu_sw_netstats __percpu *netstats); extern int netdev_max_backlog; extern int netdev_tstamp_prequeue; diff --git a/net/core/dev.c b/net/core/dev.c index a146bac84..26bc10dec 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -10319,6 +10319,40 @@ struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, } EXPORT_SYMBOL(dev_get_stats); +/** + * dev_fetch_sw_netstats - get per-cpu network device statistics + * @s: place to store stats + * @netstats: per-cpu network stats to read from + * + * Read per-cpu network statistics and populate the related fields in @s. + */ +void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, + const struct pcpu_sw_netstats __percpu *netstats) +{ + int cpu; + + for_each_possible_cpu(cpu) { + const struct pcpu_sw_netstats *stats; + struct pcpu_sw_netstats tmp; + unsigned int start; + + stats = per_cpu_ptr(netstats, cpu); + do { + start = u64_stats_fetch_begin_irq(&stats->syncp); + tmp.rx_packets = stats->rx_packets; + tmp.rx_bytes = stats->rx_bytes; + tmp.tx_packets = stats->tx_packets; + tmp.tx_bytes = stats->tx_bytes; + } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); + + s->rx_packets += tmp.rx_packets; + s->rx_bytes += tmp.rx_bytes; + s->tx_packets += tmp.tx_packets; + s->tx_bytes += tmp.tx_bytes; + } +} +EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats); + struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) { struct netdev_queue *queue = dev_ingress_queue(dev); -- 2.28.0