Skip to content

Commit de7d508

Browse files
edumazetdavem330
authored andcommitted
net: provide dev_lstats_read() helper
Many network drivers use hand-coded implementation of the same thing, let's factorize things so that u64_stats_t adoption is done once. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0f030bd commit de7d508

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎drivers/net/loopback.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ static netdev_tx_t loopback_xmit(struct sk_buff *skb,
9999
return NETDEV_TX_OK;
100100
}
101101

102-
static void loopback_get_stats64(struct net_device *dev,
103-
struct rtnl_link_stats64 *stats)
102+
void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes)
104103
{
105-
u64 bytes = 0;
106-
u64 packets = 0;
107104
int i;
108105

106+
*packets = 0;
107+
*bytes = 0;
108+
109109
for_each_possible_cpu(i) {
110110
const struct pcpu_lstats *lb_stats;
111111
u64 tbytes, tpackets;
@@ -114,12 +114,22 @@ static void loopback_get_stats64(struct net_device *dev,
114114
lb_stats = per_cpu_ptr(dev->lstats, i);
115115
do {
116116
start = u64_stats_fetch_begin_irq(&lb_stats->syncp);
117-
tbytes = lb_stats->bytes;
118117
tpackets = lb_stats->packets;
118+
tbytes = lb_stats->bytes;
119119
} while (u64_stats_fetch_retry_irq(&lb_stats->syncp, start));
120-
bytes += tbytes;
121-
packets += tpackets;
120+
*bytes += tbytes;
121+
*packets += tpackets;
122122
}
123+
}
124+
EXPORT_SYMBOL(dev_lstats_read);
125+
126+
static void loopback_get_stats64(struct net_device *dev,
127+
struct rtnl_link_stats64 *stats)
128+
{
129+
u64 packets, bytes;
130+
131+
dev_lstats_read(dev, &packets, &bytes);
132+
123133
stats->rx_packets = packets;
124134
stats->tx_packets = packets;
125135
stats->rx_bytes = bytes;

‎include/linux/netdevice.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,8 @@ struct pcpu_lstats {
24012401
struct u64_stats_sync syncp;
24022402
} __aligned(2 * sizeof(u64));
24032403

2404+
void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes);
2405+
24042406
#define __netdev_alloc_pcpu_stats(type, gfp) \
24052407
({ \
24062408
typeof(type) __percpu *pcpu_stats = alloc_percpu_gfp(type, gfp);\

0 commit comments

Comments
 (0)