net/intel: fix potential rx stats underflow
cover net/intel: fix potential rx stats underflow
The Rx packet count reported by ice, ice DCF and iavf can momentarily jump to an enormous invalid value under load. ipackets is derived by subtracting the discarded packet count from the sum of the unicast, multicast and broadcast counters. That sum already includes the discarded packets, so the result is the number actually delivered. The inputs are sampled from separate sources at slightly different instants, so the discard count can briefly exceed the measured sum. Because the arithmetic is unsigned, the subtraction wraps to a huge value and is reported as a hugely incorrect packet count and rate.
All three share the bug but not the fix, because they differ in how much control they have over the sampling.
The ice PF reads its counters directly from hardware registers, so the order is under the driver's control. Reading the discard register before the other three, combined with the fact that the counters only ever increase and the delivered-packet sum always includes the discards, makes it impossible for the discard count to exceed the later sum. No clamping is needed and the subtraction can never underflow.
ice DCF and iavf receive their counters from the PF in a single virtchnl message and cannot influence the order in which the PF sampled them. There a reorder is not available, so the subtraction is made saturating:
when the discard count exceeds the sum, essentially nothing was delivered, so zero is reported instead of underflowing.
Ciara Loftus (3):
net/ice: fix Rx packets statistics underflow net/ice: fix DCF Rx packets statistics underflow net/iavf: fix Rx packets statistics underflow
drivers/net/intel/iavf/iavf_ethdev.c | 12 ++++++++++-- drivers/net/intel/ice/ice_dcf_ethdev.c | 12 ++++++++++-- drivers/net/intel/ice/ice_ethdev.c | 11 +++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 net/ice: fix Rx packets statistics underflow
The number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets = rx_unicast + rx_multicast + rx_broadcast - rx_discards
The unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting rx_discards yields only the packets delivered to the application. However, each of these counters is read from a separate hardware register, and the reads happen at slightly different instants. Under load, rx_discards (read last) can momentarily exceed the sum of the unicast, multicast and broadcast counters (read earlier). As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.
Read the rx_discards register before the unicast, multicast and broadcast registers. As all of these counters only ever increase, and the unicast, multicast and broadcast counters always include the discarded packets, sampling rx_discards first guarantees it can never exceed the later sum of the other three, so the subtraction can never underflow.
--- drivers/net/intel/ice/ice_ethdev.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/intel/ice/ice_ethdev.c +9 -2
@@ -6457,6 +6457,15 @@ ice_update_vsi_stats(struct ice_vsi *vsi)struct ice_hw *hw =ICE_VSI_TO_HW (vsi);int idx =rte_le_to_cpu_16 (vsi->vsi_id); +/* +* Unicast/multicast/broadcast counters include discarded packets. Received packets is +* calculated by deducting discards from unicast/multicast/broadcast. To prevent a +* potential underflow, read discards first to guarantee it is smaller than +* unicast/multicast/broadcast. +*/ +ice_stat_update_32 (hw,GLV_RDPC (idx), vsi->offset_loaded, + &oes->rx_discards, &nes->rx_discards); +ice_stat_update_40 (hw,GLV_GORCH (idx),GLV_GORCL (idx), vsi->offset_loaded, &oes->rx_bytes, &nes->rx_bytes);
@@ -6483,8 +6492,6 @@ ice_update_vsi_stats(struct ice_vsi *vsi) nes->rx_bytes -= (nes->rx_unicast + nes->rx_multicast + nes->rx_broadcast) *RTE_ETHER_CRC_LEN ; -ice_stat_update_32 (hw,GLV_RDPC (idx), vsi->offset_loaded, - &oes->rx_discards, &nes->rx_discards);/* GLV_REPC not supported */ /* GLV_RMPC not supported */ ice_stat_update_32(hw, GLSWID_RUPP(idx), vsi->offset_loaded,
Signature · 1 line
2.43.0
Re: [PATCH 1/3] net/ice: fix Rx packets statistics underflow
Quoted 23 lines · Ciara Loftus
The number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets =
rx_unicast+rx_multicast+rx_broadcast-rx_discardsThe unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting
rx_discardsyields only the packets delivered to the application. However, each of these counters is read from a separate hardware register, and the reads happen at slightly different instants. Under load,rx_discards(read last) can momentarily exceed the sum of the unicast, multicast and broadcast counters (read earlier). As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.Read the
rx_discardsregister before the unicast, multicast and broadcast registers. As all of these counters only ever increase, and the unicast, multicast and broadcast counters always include the discarded packets, samplingrx_discardsfirst guarantees it can never exceed the later sum of the other three, so the subtraction can never underflow.
Quoted 5 lines
Fixes a37bde56314d ("net/ice: support statistics")Signed-off-by Ciara Loftus <[email protected]>---
PATCH 2/3 net/ice: fix DCF Rx packets statistics underflow
In reply to Ciara LoftusThe number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets = rx_unicast + rx_multicast + rx_broadcast - rx_discards
The unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting rx_discards yields only the packets delivered to the application. These values are provided by the PF in a virtchnl_eth_stats message; the PF samples them from separate sources and the order in which they are read cannot be guaranteed. Under load, rx_discards can therefore momentarily exceed the sum of the unicast, multicast and broadcast counters. As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.
The read order cannot be guaranteed, so use a saturating subtraction:
when rx_discards exceeds the sum of the unicast, multicast and broadcast counters essentially nothing was delivered, so report zero instead of underflowing.
--- drivers/net/intel/ice/ice_dcf_ethdev.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/intel/ice/ice_dcf_ethdev.c +10 -2
@@ -1523,8 +1523,16 @@ ice_dcf_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, ret =ice_dcf_query_stats (hw, &pstats);if (ret ==0 ) {ice_dcf_update_stats (&hw->eth_stats_offset, &pstats); - stats->ipackets = pstats.rx_unicast + pstats.rx_multicast + - pstats.rx_broadcast - pstats.rx_discards; + stats->ipackets = pstats.rx_unicast + pstats.rx_multicast + pstats.rx_broadcast; +/* +* Unicast/multicast/broadcast counters include discarded packets, so subtract +* rx_discards to report only the packets delivered to the application. The +* counters are sampled from separate sources and can be momentarily inconsistent +* under load. If rx_discards exceeds their sum then essentially nothing was +* delivered, so saturate at zero rather than underflow. +*/ + stats->ipackets = stats->ipackets >= pstats.rx_discards ? + stats->ipackets - pstats.rx_discards :0 ; stats->opackets = pstats.tx_broadcast + pstats.tx_multicast + pstats.tx_unicast; stats->imissed = pstats.rx_discards;
Signature · 1 line
2.43.0
Re: [PATCH 2/3] net/ice: fix DCF Rx packets statistics underflow
Quoted 21 lines · Ciara Loftus
The number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets =
rx_unicast+rx_multicast+rx_broadcast-rx_discardsThe unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting
rx_discardsyields only the packets delivered to the application. These values are provided by the PF in avirtchnl_eth_statsmessage; the PF samples them from separate sources and the order in which they are read cannot be guaranteed. Under load,rx_discardscan therefore momentarily exceed the sum of the unicast, multicast and broadcast counters. As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.The read order cannot be guaranteed, so use a saturating subtraction:
whenrx_discardsexceeds the sum of the unicast, multicast and broadcast counters essentially nothing was delivered, so report zero instead of underflowing.
Quoted 5 lines
Fixes c9f889e99616 ("net/ice: enable stats for DCF")Signed-off-by Ciara Loftus <[email protected]>---
PATCH 3/3 net/iavf: fix Rx packets statistics underflow
In reply to Ciara LoftusThe number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets = rx_unicast + rx_multicast + rx_broadcast - rx_discards
The unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting rx_discards yields only the packets delivered to the application. These values are provided by the PF in a virtchnl_eth_stats message; the PF samples them from separate sources and the VF cannot guarantee the order in which they are read. Under load, rx_discards can therefore momentarily exceed the sum of the unicast, multicast and broadcast counters. As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.
The read order cannot be guaranteed from the VF, so use a saturating
subtraction: when rx_discards exceeds the sum of the unicast, multicast and broadcast counters essentially nothing was delivered, so report zero instead of underflowing.
--- drivers/net/intel/iavf/iavf_ethdev.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/net/intel/iavf/iavf_ethdev.c +10 -2
@@ -1887,8 +1887,16 @@ iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats, RTE_ETH_RX_OFFLOAD_KEEP_CRC) ? 0 :RTE_ETHER_CRC_LEN ;iavf_update_stats (vsi, &pstats); - stats->ipackets = pstats.rx_unicast + pstats.rx_multicast + - pstats.rx_broadcast - pstats.rx_discards; + stats->ipackets = pstats.rx_unicast + pstats.rx_multicast + pstats.rx_broadcast; +/* +* Unicast/multicast/broadcast counters include discarded packets, so subtract +* rx_discards to report only the packets delivered to the application. The +* counters are sampled from separate sources and can be momentarily inconsistent +* under load. If rx_discards exceeds their sum then essentially nothing was +* delivered, so saturate at zero rather than underflow. +*/ + stats->ipackets = stats->ipackets >= pstats.rx_discards ? + stats->ipackets - pstats.rx_discards :0 ; stats->opackets = pstats.tx_broadcast + pstats.tx_multicast + pstats.tx_unicast; stats->imissed = pstats.rx_discards;
Signature · 1 line
2.43.0
Re: [PATCH 3/3] net/iavf: fix Rx packets statistics underflow
Quoted 21 lines · Ciara Loftus
The number of Rx packets is computed as the sum of the unicast, multicast and broadcast packet counters, minus the discarded packet count:
ipackets =
rx_unicast+rx_multicast+rx_broadcast-rx_discardsThe unicast, multicast and broadcast counters already include the packets that were subsequently dropped, so subtracting
rx_discardsyields only the packets delivered to the application. These values are provided by the PF in avirtchnl_eth_statsmessage; the PF samples them from separate sources and the VF cannot guarantee the order in which they are read. Under load,rx_discardscan therefore momentarily exceed the sum of the unicast, multicast and broadcast counters. As ipackets is unsigned, the subtraction then wraps to a huge bogus value, reported to the application as an enormous Rx packet count and packet rate.The read order cannot be guaranteed from the VF, so use a saturating
subtraction: whenrx_discardsexceeds the sum of the unicast, multicast and broadcast counters essentially nothing was delivered, so report zero instead of underflowing.
Quoted 5 lines
Fixes e71ffcc1008e ("net/iavf: fix Rx total stats")Signed-off-by Ciara Loftus <[email protected]>---
Quoted 26 lines · Ciara Loftus
The Rx packet count reported by ice, ice DCF and iavf can momentarily jump to an enormous invalid value under load. ipackets is derived by subtracting the discarded packet count from the sum of the unicast, multicast and broadcast counters. That sum already includes the discarded packets, so the result is the number actually delivered. The inputs are sampled from separate sources at slightly different instants, so the discard count can briefly exceed the measured sum. Because the arithmetic is unsigned, the subtraction wraps to a huge value and is reported as a hugely incorrect packet count and rate.
All three share the bug but not the fix, because they differ in how much control they have over the sampling.
The ice PF reads its counters directly from hardware registers, so the order is under the driver's control. Reading the discard register before the other three, combined with the fact that the counters only ever increase and the delivered-packet sum always includes the discards, makes it impossible for the discard count to exceed the later sum. No clamping is needed and the subtraction can never underflow.
ice DCF and iavf receive their counters from the PF in a single virtchnl message and cannot influence the order in which the PF sampled them. There a reorder is not available, so the subtraction is made saturating:
when the discard count exceeds the sum, essentially nothing was delivered, so zero is reported instead of underflowing.
Quoted 5 lines
Ciara Loftus (3):
net/ice: fix Rx packets statistics underflow net/ice: fix DCF Rx packets statistics underflow net/iavf: fix Rx packets statistics underflow
Series applied to dpdk-next-net-intel. I was tempted to squash into one patch, but kept them as three because the fix is not identical in all cases (iavf being different).
Thanks,
/Bruce