Skip to content

Commit 6e042b6

Browse files
kuba-mooSasha Levin
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 7371f2d commit 6e042b6

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
@@ -92,6 +92,12 @@ struct netdev_stat_ops {
9292
struct netdev_queue_stats_tx *tx);
9393
};
9494

95+
void netdev_stat_queue_sum(struct net_device *netdev,
96+
int rx_start, int rx_end,
97+
struct netdev_queue_stats_rx *rx_sum,
98+
int tx_start, int tx_end,
99+
struct netdev_queue_stats_tx *tx_sum);
100+
95101
/**
96102
* struct netdev_queue_mgmt_ops - netdev ops for queue management
97103
*

net/core/netdev-genl.c

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -616,25 +616,66 @@ netdev_nl_stats_by_queue(struct net_device *netdev, struct sk_buff *rsp,
616616
return 0;
617617
}
618618

619+
/**
620+
* netdev_stat_queue_sum() - add up queue stats from range of queues
621+
* @netdev: net_device
622+
* @rx_start: index of the first Rx queue to query
623+
* @rx_end: index after the last Rx queue (first *not* to query)
624+
* @rx_sum: output Rx stats, should be already initialized
625+
* @tx_start: index of the first Tx queue to query
626+
* @tx_end: index after the last Tx queue (first *not* to query)
627+
* @tx_sum: output Tx stats, should be already initialized
628+
*
629+
* Add stats from [start, end) range of queue IDs to *x_sum structs.
630+
* The sum structs must be already initialized. Usually this
631+
* helper is invoked from the .get_base_stats callbacks of drivers
632+
* to account for stats of disabled queues. In that case the ranges
633+
* are usually [netdev->real_num_*x_queues, netdev->num_*x_queues).
634+
*/
635+
void netdev_stat_queue_sum(struct net_device *netdev,
636+
int rx_start, int rx_end,
637+
struct netdev_queue_stats_rx *rx_sum,
638+
int tx_start, int tx_end,
639+
struct netdev_queue_stats_tx *tx_sum)
640+
{
641+
const struct netdev_stat_ops *ops;
642+
struct netdev_queue_stats_rx rx;
643+
struct netdev_queue_stats_tx tx;
644+
int i;
645+
646+
ops = netdev->stat_ops;
647+
648+
for (i = rx_start; i < rx_end; i++) {
649+
memset(&rx, 0xff, sizeof(rx));
650+
if (ops->get_queue_stats_rx)
651+
ops->get_queue_stats_rx(netdev, i, &rx);
652+
netdev_nl_stats_add(rx_sum, &rx, sizeof(rx));
653+
}
654+
for (i = tx_start; i < tx_end; i++) {
655+
memset(&tx, 0xff, sizeof(tx));
656+
if (ops->get_queue_stats_tx)
657+
ops->get_queue_stats_tx(netdev, i, &tx);
658+
netdev_nl_stats_add(tx_sum, &tx, sizeof(tx));
659+
}
660+
}
661+
EXPORT_SYMBOL(netdev_stat_queue_sum);
662+
619663
static int
620664
netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp,
621665
const struct genl_info *info)
622666
{
623-
struct netdev_queue_stats_rx rx_sum, rx;
624-
struct netdev_queue_stats_tx tx_sum, tx;
625-
const struct netdev_stat_ops *ops;
667+
struct netdev_queue_stats_rx rx_sum;
668+
struct netdev_queue_stats_tx tx_sum;
626669
void *hdr;
627-
int i;
628670

629-
ops = netdev->stat_ops;
630671
/* Netdev can't guarantee any complete counters */
631-
if (!ops->get_base_stats)
672+
if (!netdev->stat_ops->get_base_stats)
632673
return 0;
633674

634675
memset(&rx_sum, 0xff, sizeof(rx_sum));
635676
memset(&tx_sum, 0xff, sizeof(tx_sum));
636677

637-
ops->get_base_stats(netdev, &rx_sum, &tx_sum);
678+
netdev->stat_ops->get_base_stats(netdev, &rx_sum, &tx_sum);
638679

639680
/* The op was there, but nothing reported, don't bother */
640681
if (!memchr_inv(&rx_sum, 0xff, sizeof(rx_sum)) &&
@@ -647,18 +688,8 @@ netdev_nl_stats_by_netdev(struct net_device *netdev, struct sk_buff *rsp,
647688
if (nla_put_u32(rsp, NETDEV_A_QSTATS_IFINDEX, netdev->ifindex))
648689
goto nla_put_failure;
649690

650-
for (i = 0; i < netdev->real_num_rx_queues; i++) {
651-
memset(&rx, 0xff, sizeof(rx));
652-
if (ops->get_queue_stats_rx)
653-
ops->get_queue_stats_rx(netdev, i, &rx);
654-
netdev_nl_stats_add(&rx_sum, &rx, sizeof(rx));
655-
}
656-
for (i = 0; i < netdev->real_num_tx_queues; i++) {
657-
memset(&tx, 0xff, sizeof(tx));
658-
if (ops->get_queue_stats_tx)
659-
ops->get_queue_stats_tx(netdev, i, &tx);
660-
netdev_nl_stats_add(&tx_sum, &tx, sizeof(tx));
661-
}
691+
netdev_stat_queue_sum(netdev, 0, netdev->real_num_rx_queues, &rx_sum,
692+
0, netdev->real_num_tx_queues, &tx_sum);
662693

663694
if (netdev_nl_stats_write_rx(rsp, &rx_sum) ||
664695
netdev_nl_stats_write_tx(rsp, &tx_sum))

0 commit comments

Comments
 (0)