Skip to content

Commit e4196ff

Browse files
kuba-moogregkh
authored andcommitted
net: export a helper for adding up queue stats
[ Upstream commit 23fa6a2 ] Older drivers and drivers with lower queue counts often have a static array of queues, rather than allocating structs for each queue on demand. Add a helper for adding up qstats from a queue range. Expectation is that driver will pass a queue range [netdev->real_num_*x_queues, MAX). It was tempting to always use num_*x_queues as the end, but virtio seems to clamp its queue count after allocating the netdev. And this way we can trivaly reuse the helper for [0, real_..). Signed-off-by: Jakub Kicinski <kuba@kernel.org> Link: https://patch.msgid.link/20250507003221.823267-2-kuba@kernel.org Signed-off-by: Paolo Abeni <pabeni@redhat.com> Stable-dep-of: 001160e ("virtio-net: fix total qstat values") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent fb0b95d commit e4196ff

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

include/net/netdev_queues.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ struct netdev_stat_ops {
102102
struct netdev_queue_stats_tx *tx);
103103
};
104104

105+
void netdev_stat_queue_sum(struct net_device *netdev,
106+
int rx_start, int rx_end,
107+
struct netdev_queue_stats_rx *rx_sum,
108+
int tx_start, int tx_end,
109+
struct netdev_queue_stats_tx *tx_sum);
110+
105111
/**
106112
* struct netdev_queue_mgmt_ops - netdev ops for queue management
107113
*

net/core/netdev-genl.c

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -690,25 +690,66 @@ netdev_nl_stats_by_queue(struct net_device *netdev, struct sk_buff *rsp,
690690
return 0;
691691
}
692692

693+
/**
694+
* netdev_stat_queue_sum() - add up queue stats from range of queues
695+
* @netdev: net_device
696+
* @rx_start: index of the first Rx queue to query
697+
* @rx_end: index after the last Rx queue (first *not* to query)
698+
* @rx_sum: output Rx stats, should be already initialized
699+
* @tx_start: index of the first Tx queue to query
700+
* @tx_end: index after the last Tx queue (first *not* to query)
701+
* @tx_sum: output Tx stats, should be already initialized
702+
*
703+
* Add stats from [start, end) range of queue IDs to *x_sum structs.
704+
* The sum structs must be already initialized. Usually this
705+
* helper is invoked from the .get_base_stats callbacks of drivers
706+
* to account for stats of disabled queues. In that case the ranges
707+
* are usually [netdev->real_num_*x_queues, netdev->num_*x_queues).
708+
*/
709+
void netdev_stat_queue_sum(struct net_device *netdev,
710+
int rx_start, int rx_end,
711+
struct netdev_queue_stats_rx *rx_sum,
712+
int tx_start, int tx_end,
713+
struct netdev_queue_stats_tx *tx_sum)
714+
{
715+
const struct netdev_stat_ops *ops;
716+
struct netdev_queue_stats_rx rx;
717+
struct netdev_queue_stats_tx tx;
718+
int i;
719+
720+
ops = netdev->stat_ops;
721+
722+
for (i = rx_start; i < rx_end; i++) {
723+
memset(&rx, 0xff, sizeof(rx));
724+
if (ops->get_queue_stats_rx)
725+
ops->get_queue_stats_rx(netdev, i, &rx);
726+
netdev_nl_stats_add(rx_sum, &rx, sizeof(rx));
727+
}
728+
for (i = tx_start; i < tx_end; i++) {
729+
memset(&tx, 0xff, sizeof(tx));
730+
if (ops->get_queue_stats_tx)
731+
ops->get_queue_stats_tx(netdev, i, &tx);
732+
netdev_nl_stats_add(tx_sum, &tx, sizeof(tx));
733+
}
734+
}
735+
EXPORT_SYMBOL(netdev_stat_queue_sum);
736+
693737
static int
694738
netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp,
695739
const struct genl_info *info)
696740
{
697-
struct netdev_queue_stats_rx rx_sum, rx;
698-
struct netdev_queue_stats_tx tx_sum, tx;
699-
const struct netdev_stat_ops *ops;
741+
struct netdev_queue_stats_rx rx_sum;
742+
struct netdev_queue_stats_tx tx_sum;
700743
void *hdr;
701-
int i;
702744

703-
ops = netdev->stat_ops;
704745
/* Netdev can't guarantee any complete counters */
705-
if (!ops->get_base_stats)
746+
if (!netdev->stat_ops->get_base_stats)
706747
return 0;
707748

708749
memset(&rx_sum, 0xff, sizeof(rx_sum));
709750
memset(&tx_sum, 0xff, sizeof(tx_sum));
710751

711-
ops->get_base_stats(netdev, &rx_sum, &tx_sum);
752+
netdev->stat_ops->get_base_stats(netdev, &rx_sum, &tx_sum);
712753

713754
/* The op was there, but nothing reported, don't bother */
714755
if (!memchr_inv(&rx_sum, 0xff, sizeof(rx_sum)) &&
@@ -721,18 +762,8 @@ netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp,
721762
if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex))
722763
goto nla_put_failure;
723764

724-
for (i = 0; i < netdev->real_num_rx_queues; i++) {
725-
memset(&rx, 0xff, sizeof(rx));
726-
if (ops->get_queue_stats_rx)
727-
ops->get_queue_stats_rx(netdev, i, &rx);
728-
netdev_nl_stats_add(&rx_sum, &rx, sizeof(rx));
729-
}
730-
for (i = 0; i < netdev->real_num_tx_queues; i++) {
731-
memset(&tx, 0xff, sizeof(tx));
732-
if (ops->get_queue_stats_tx)
733-
ops->get_queue_stats_tx(netdev, i, &tx);
734-
netdev_nl_stats_add(&tx_sum, &tx, sizeof(tx));
735-
}
765+
netdev_stat_queue_sum(netdev, 0, netdev->real_num_rx_queues, &rx_sum,
766+
0, netdev->real_num_tx_queues, &tx_sum);
736767

737768
if (netdev_nl_stats_write_rx(rsp, &rx_sum) ||
738769
netdev_nl_stats_write_tx(rsp, &tx_sum))

0 commit comments

Comments
 (0)