1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Wed, 6 Aug 2025 10:49:54 +0200
3 Subject: [PATCH] mac80211: factor out part of
4 ieee80211_calc_expected_tx_airtime
6 Create ieee80211_rate_expected_tx_airtime helper function, which returns
7 the expected tx airtime for a given rate and packet length in units of
8 1024 usec, for more accuracy.
10 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 --- a/net/mac80211/airtime.c
14 +++ b/net/mac80211/airtime.c
15 @@ -685,7 +685,7 @@ static int ieee80211_fill_rx_status(stru
16 if (ieee80211_fill_rate_info(hw, stat, band, ri))
19 - if (!ieee80211_rate_valid(rate))
20 + if (!rate || !ieee80211_rate_valid(rate))
23 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
24 @@ -753,6 +753,53 @@ u32 ieee80211_calc_tx_airtime(struct iee
26 EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime);
28 +u32 ieee80211_rate_expected_tx_airtime(struct ieee80211_hw *hw,
29 + struct ieee80211_tx_rate *tx_rate,
30 + struct rate_info *ri,
31 + enum nl80211_band band,
32 + bool ampdu, int len)
34 + struct ieee80211_rx_status stat;
35 + u32 duration, overhead;
38 + if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len))
41 + if (stat.encoding == RX_ENC_LEGACY || !ampdu)
42 + return ieee80211_calc_rx_airtime(hw, &stat, len) * 1024;
44 + duration = ieee80211_get_rate_duration(hw, &stat, &overhead);
47 + * Assume that HT/VHT transmission on any AC except VO will
48 + * use aggregation. Since we don't have reliable reporting
49 + * of aggregation length, assume an average size based on the
51 + * This will not be very accurate, but much better than simply
52 + * assuming un-aggregated tx in all cases.
54 + if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */
56 + else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */
58 + else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */
60 + else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */
62 + else if (stat.encoding != RX_ENC_HE ||
63 + duration > 20 * 1024) /* <= HE40 MCS6 2S */
69 + duration /= AVG_PKT_SIZE;
70 + duration += (overhead * 1024 >> agg_shift);
75 u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
76 struct ieee80211_vif *vif,
77 struct ieee80211_sta *pubsta,
78 @@ -775,45 +822,13 @@ u32 ieee80211_calc_expected_tx_airtime(s
80 struct sta_info *sta = container_of(pubsta, struct sta_info,
82 - struct ieee80211_rx_status stat;
83 struct ieee80211_tx_rate *tx_rate = &sta->deflink.tx_stats.last_rate;
84 struct rate_info *ri = &sta->deflink.tx_stats.last_rate_info;
85 - u32 duration, overhead;
88 - if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len))
91 - if (stat.encoding == RX_ENC_LEGACY || !ampdu)
92 - return ieee80211_calc_rx_airtime(hw, &stat, len);
94 - duration = ieee80211_get_rate_duration(hw, &stat, &overhead);
96 - * Assume that HT/VHT transmission on any AC except VO will
97 - * use aggregation. Since we don't have reliable reporting
98 - * of aggregation length, assume an average size based on the
100 - * This will not be very accurate, but much better than simply
101 - * assuming un-aggregated tx in all cases.
103 - if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */
105 - else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */
107 - else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */
109 - else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */
111 - else if (stat.encoding != RX_ENC_HE ||
112 - duration > 20 * 1024) /* <= HE40 MCS6 2S */
119 - duration /= AVG_PKT_SIZE;
120 + duration = ieee80211_rate_expected_tx_airtime(hw, tx_rate, ri,
123 - duration += (overhead >> agg_shift);
125 return max_t(u32, duration, 4);
127 --- a/net/mac80211/ieee80211_i.h
128 +++ b/net/mac80211/ieee80211_i.h
129 @@ -2776,6 +2776,11 @@ u8 *ieee80211_get_bssid(struct ieee80211
131 extern const struct ethtool_ops ieee80211_ethtool_ops;
133 +u32 ieee80211_rate_expected_tx_airtime(struct ieee80211_hw *hw,
134 + struct ieee80211_tx_rate *tx_rate,
135 + struct rate_info *ri,
136 + enum nl80211_band band,
137 + bool ampdu, int len);
138 u32 ieee80211_calc_expected_tx_airtime(struct ieee80211_hw *hw,
139 struct ieee80211_vif *vif,
140 struct ieee80211_sta *pubsta,