bffb66dfe73f9a62fe5b6bea2ef23e0e30eaf6dd
[openwrt/staging/ldir.git] /
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
5
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.
9
10 Signed-off-by: Felix Fietkau <nbd@nbd.name>
11 ---
12
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))
17 return 0;
18
19 - if (!ieee80211_rate_valid(rate))
20 + if (!rate || !ieee80211_rate_valid(rate))
21 return -1;
22
23 if (rate->flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
24 @@ -753,6 +753,53 @@ u32 ieee80211_calc_tx_airtime(struct iee
25 }
26 EXPORT_SYMBOL_GPL(ieee80211_calc_tx_airtime);
27
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)
33 +{
34 + struct ieee80211_rx_status stat;
35 + u32 duration, overhead;
36 + u8 agg_shift;
37 +
38 + if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len))
39 + return 0;
40 +
41 + if (stat.encoding == RX_ENC_LEGACY || !ampdu)
42 + return ieee80211_calc_rx_airtime(hw, &stat, len) * 1024;
43 +
44 + duration = ieee80211_get_rate_duration(hw, &stat, &overhead);
45 +
46 + /*
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
50 + * tx rate.
51 + * This will not be very accurate, but much better than simply
52 + * assuming un-aggregated tx in all cases.
53 + */
54 + if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */
55 + agg_shift = 1;
56 + else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */
57 + agg_shift = 2;
58 + else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */
59 + agg_shift = 3;
60 + else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */
61 + agg_shift = 4;
62 + else if (stat.encoding != RX_ENC_HE ||
63 + duration > 20 * 1024) /* <= HE40 MCS6 2S */
64 + agg_shift = 5;
65 + else
66 + agg_shift = 6;
67 +
68 + duration *= len;
69 + duration /= AVG_PKT_SIZE;
70 + duration += (overhead * 1024 >> agg_shift);
71 +
72 + return duration;
73 +}
74 +
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
79 if (pubsta) {
80 struct sta_info *sta = container_of(pubsta, struct sta_info,
81 sta);
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;
86 - u8 agg_shift;
87 -
88 - if (ieee80211_fill_rx_status(&stat, hw, tx_rate, ri, band, len))
89 - return 0;
90 -
91 - if (stat.encoding == RX_ENC_LEGACY || !ampdu)
92 - return ieee80211_calc_rx_airtime(hw, &stat, len);
93 -
94 - duration = ieee80211_get_rate_duration(hw, &stat, &overhead);
95 - /*
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
99 - * tx rate.
100 - * This will not be very accurate, but much better than simply
101 - * assuming un-aggregated tx in all cases.
102 - */
103 - if (duration > 400 * 1024) /* <= VHT20 MCS2 1S */
104 - agg_shift = 1;
105 - else if (duration > 250 * 1024) /* <= VHT20 MCS3 1S or MCS1 2S */
106 - agg_shift = 2;
107 - else if (duration > 150 * 1024) /* <= VHT20 MCS5 1S or MCS2 2S */
108 - agg_shift = 3;
109 - else if (duration > 70 * 1024) /* <= VHT20 MCS5 2S */
110 - agg_shift = 4;
111 - else if (stat.encoding != RX_ENC_HE ||
112 - duration > 20 * 1024) /* <= HE40 MCS6 2S */
113 - agg_shift = 5;
114 - else
115 - agg_shift = 6;
116 + u32 duration;
117
118 - duration *= len;
119 - duration /= AVG_PKT_SIZE;
120 + duration = ieee80211_rate_expected_tx_airtime(hw, tx_rate, ri,
121 + band, true, len);
122 duration /= 1024;
123 - duration += (overhead >> agg_shift);
124
125 return max_t(u32, duration, 4);
126 }
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
130
131 extern const struct ethtool_ops ieee80211_ethtool_ops;
132
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,