2 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
10 * Sponsored by Indranet Technologies Ltd
13 * Copyright (c) 2005 John Bicket
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer,
21 * without modification.
22 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
23 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
24 * redistribution must be conditioned upon including a substantially
25 * similar Disclaimer requirement for further binary redistribution.
26 * 3. Neither the names of the above-listed copyright holders nor the names
27 * of any contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * Alternatively, this software may be distributed under the terms of the
31 * GNU General Public License ("GPL") version 2 as published by the Free
32 * Software Foundation.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
38 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
40 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
43 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
45 * THE POSSIBILITY OF SUCH DAMAGES.
47 #include <linux/netdevice.h>
48 #include <linux/types.h>
49 #include <linux/skbuff.h>
50 #include <linux/debugfs.h>
51 #include <linux/random.h>
52 #include <linux/ieee80211.h>
53 #include <net/mac80211.h>
55 #include "rc80211_minstrel.h"
57 #define SAMPLE_COLUMNS 10
58 #define SAMPLE_TBL(_mi, _idx, _col) \
59 _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
61 /* convert mac80211 rate index to local array index */
63 rix_to_ndx(struct minstrel_sta_info
*mi
, int rix
)
66 for (i
= rix
; i
>= 0; i
--)
67 if (mi
->r
[i
].rix
== rix
)
69 WARN_ON(mi
->r
[i
].rix
!= rix
);
74 use_low_rate(struct sk_buff
*skb
)
76 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
77 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
80 fc
= le16_to_cpu(hdr
->frame_control
);
82 return ((info
->flags
& IEEE80211_TX_CTL_NO_ACK
) ||
83 (fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
||
84 is_multicast_ether_addr(hdr
->addr1
));
89 minstrel_update_stats(struct minstrel_priv
*mp
, struct minstrel_sta_info
*mi
)
91 u32 max_tp
= 0, index_max_tp
= 0, index_max_tp2
= 0;
92 u32 max_prob
= 0, index_max_prob
= 0;
97 mi
->stats_update
= jiffies
;
98 for (i
= 0; i
< mi
->n_rates
; i
++) {
99 struct minstrel_rate
*mr
= &mi
->r
[i
];
101 usecs
= mr
->perfect_tx_time
;
105 /* To avoid rounding issues, probabilities scale from 0 (0%)
108 p
= (mr
->success
* 18000) / mr
->attempts
;
109 mr
->succ_hist
+= mr
->success
;
110 mr
->att_hist
+= mr
->attempts
;
112 p
= ((p
* (100 - mp
->ewma_level
)) + (mr
->probability
*
113 mp
->ewma_level
)) / 100;
115 mr
->cur_tp
= p
* (1000000 / usecs
);
118 mr
->last_success
= mr
->success
;
119 mr
->last_attempts
= mr
->attempts
;
123 /* Sample less often below the 10% chance of success.
124 * Sample less often above the 95% chance of success. */
125 if ((mr
->probability
> 17100) || (mr
->probability
< 1800)) {
126 mr
->adjusted_retry_count
= mr
->retry_count
>> 1;
127 if (mr
->adjusted_retry_count
> 2)
128 mr
->adjusted_retry_count
= 2;
130 mr
->adjusted_retry_count
= mr
->retry_count
;
132 if (!mr
->adjusted_retry_count
)
133 mr
->adjusted_retry_count
= 2;
136 for (i
= 0; i
< mi
->n_rates
; i
++) {
137 struct minstrel_rate
*mr
= &mi
->r
[i
];
138 if (max_tp
< mr
->cur_tp
) {
142 if (max_prob
< mr
->probability
) {
144 max_prob
= mr
->probability
;
149 for (i
= 0; i
< mi
->n_rates
; i
++) {
150 struct minstrel_rate
*mr
= &mi
->r
[i
];
152 if (i
== index_max_tp
)
155 if (max_tp
< mr
->cur_tp
) {
160 mi
->max_tp_rate
= index_max_tp
;
161 mi
->max_tp_rate2
= index_max_tp2
;
162 mi
->max_prob_rate
= index_max_prob
;
166 minstrel_tx_status(void *priv
, struct ieee80211_supported_band
*sband
,
167 struct ieee80211_sta
*sta
, void *priv_sta
,
170 struct minstrel_sta_info
*mi
= priv_sta
;
171 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
172 struct ieee80211_tx_altrate
*ar
= info
->status
.retries
;
173 struct minstrel_priv
*mp
= priv
;
177 if (!info
->status
.excessive_retries
)
180 if (!mp
->has_mrr
|| (ar
[0].rate_idx
< 0)) {
181 ndx
= rix_to_ndx(mi
, info
->tx_rate_idx
);
182 tries
= info
->status
.retry_count
+ 1;
183 mi
->r
[ndx
].success
+= success
;
184 mi
->r
[ndx
].attempts
+= tries
;
188 for (i
= 0; i
< 4; i
++) {
189 if (ar
[i
].rate_idx
< 0)
192 ndx
= rix_to_ndx(mi
, ar
[i
].rate_idx
);
193 mi
->r
[ndx
].attempts
+= ar
[i
].limit
+ 1;
195 if ((i
!= 3) && (ar
[i
+ 1].rate_idx
< 0))
196 mi
->r
[ndx
].success
+= success
;
199 if ((info
->flags
& IEEE80211_TX_CTL_RATE_CTRL_PROBE
) && (i
>= 0))
202 if (mi
->sample_deferred
> 0)
203 mi
->sample_deferred
--;
207 static inline unsigned int
208 minstrel_get_retry_count(struct minstrel_rate
*mr
,
209 struct ieee80211_tx_info
*info
)
211 unsigned int retry
= mr
->adjusted_retry_count
;
213 if (info
->flags
& IEEE80211_TX_CTL_USE_RTS_CTS
)
214 retry
= max(2U, min(mr
->retry_count_rtscts
, retry
));
215 else if (info
->flags
& IEEE80211_TX_CTL_USE_CTS_PROTECT
)
216 retry
= max(2U, min(mr
->retry_count_cts
, retry
));
222 minstrel_get_next_sample(struct minstrel_sta_info
*mi
)
224 unsigned int sample_ndx
;
225 sample_ndx
= SAMPLE_TBL(mi
, mi
->sample_idx
, mi
->sample_column
);
227 if (mi
->sample_idx
> (mi
->n_rates
- 2)) {
230 if (mi
->sample_column
>= SAMPLE_COLUMNS
)
231 mi
->sample_column
= 0;
237 minstrel_get_rate(void *priv
, struct ieee80211_supported_band
*sband
,
238 struct ieee80211_sta
*sta
, void *priv_sta
,
239 struct sk_buff
*skb
, struct rate_selection
*sel
)
241 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
242 struct minstrel_sta_info
*mi
= priv_sta
;
243 struct minstrel_priv
*mp
= priv
;
244 struct ieee80211_tx_altrate
*ar
= info
->control
.retries
;
245 unsigned int ndx
, sample_ndx
= 0;
247 bool sample_slower
= false;
253 if (!sta
|| !mi
|| use_low_rate(skb
)) {
254 sel
->rate_idx
= rate_lowest_index(sband
, sta
);
260 /* mac80211 does not allow mrr for RTS/CTS */
261 if ((info
->flags
& IEEE80211_TX_CTL_USE_RTS_CTS
) ||
262 (info
->flags
& IEEE80211_TX_CTL_USE_CTS_PROTECT
))
265 if (time_after(jiffies
, mi
->stats_update
+ (mp
->update_interval
*
267 minstrel_update_stats(mp
, mi
);
269 ndx
= mi
->max_tp_rate
;
272 sample_rate
= mp
->lookaround_rate_mrr
;
274 sample_rate
= mp
->lookaround_rate
;
277 delta
= (mi
->packet_count
* sample_rate
/ 100) -
278 (mi
->sample_count
+ mi
->sample_deferred
/ 2);
280 /* delta > 0: sampling required */
282 if (mi
->packet_count
>= 10000) {
283 mi
->sample_deferred
= 0;
284 mi
->sample_count
= 0;
285 mi
->packet_count
= 0;
286 } else if (delta
> mi
->n_rates
* 2) {
287 /* With multi-rate retry, not every planned sample
288 * attempt actually gets used, due to the way the retry
289 * chain is set up - [max_tp,sample,prob,lowest] for
290 * sample_rate < max_tp.
292 * If there's too much sampling backlog and the link
293 * starts getting worse, minstrel would start bursting
294 * out lots of sampling frames, which would result
295 * in a large throughput loss. */
296 mi
->sample_count
+= (delta
- mi
->n_rates
* 2);
299 sample_ndx
= minstrel_get_next_sample(mi
);
301 sample_slower
= mrr
&& (mi
->r
[sample_ndx
].perfect_tx_time
>
302 mi
->r
[ndx
].perfect_tx_time
);
304 if (!sample_slower
) {
308 /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
309 * packets that have the sampling rate deferred to the
310 * second MRR stage. Increase the sample counter only
311 * if the deferred sample rate was actually used.
312 * Use the sample_deferred counter to make sure that
313 * the sampling is not done in large bursts */
314 info
->flags
|= IEEE80211_TX_CTL_RATE_CTRL_PROBE
;
315 mi
->sample_deferred
++;
318 sel
->rate_idx
= mi
->r
[ndx
].rix
;
319 info
->control
.retry_limit
= minstrel_get_retry_count(&mi
->r
[ndx
], info
);
322 ar
[0].rate_idx
= mi
->lowest_rix
;
323 ar
[0].limit
= mp
->max_retry
;
331 mrr_ndx
[0] = sample_ndx
;
333 mrr_ndx
[0] = mi
->max_tp_rate
;
335 mrr_ndx
[0] = mi
->max_tp_rate2
;
337 mrr_ndx
[1] = mi
->max_prob_rate
;
339 for (i
= 0; i
< 3; i
++) {
340 ar
[i
].rate_idx
= mi
->r
[mrr_ndx
[i
]].rix
;
341 ar
[i
].limit
= mi
->r
[mrr_ndx
[i
]].adjusted_retry_count
;
347 calc_rate_durations(struct minstrel_sta_info
*mi
, struct ieee80211_local
*local
,
348 struct minstrel_rate
*d
, struct ieee80211_rate
*rate
)
350 int erp
= !!(rate
->flags
& IEEE80211_RATE_ERP_G
);
352 d
->perfect_tx_time
= ieee80211_frame_duration(local
, 1200,
353 rate
->bitrate
, erp
, 1);
354 d
->ack_time
= ieee80211_frame_duration(local
, 10,
355 rate
->bitrate
, erp
, 1);
359 init_sample_table(struct minstrel_sta_info
*mi
)
361 unsigned int i
, col
, new_idx
;
362 unsigned int n_srates
= mi
->n_rates
- 1;
365 mi
->sample_column
= 0;
367 memset(mi
->sample_table
, 0, SAMPLE_COLUMNS
* mi
->n_rates
);
369 for (col
= 0; col
< SAMPLE_COLUMNS
; col
++) {
370 for (i
= 0; i
< n_srates
; i
++) {
371 get_random_bytes(rnd
, sizeof(rnd
));
372 new_idx
= (i
+ rnd
[i
& 7]) % n_srates
;
374 while (SAMPLE_TBL(mi
, new_idx
, col
) != 0)
375 new_idx
= (new_idx
+ 1) % n_srates
;
377 /* Don't sample the slowest rate (i.e. slowest base
378 * rate). We must presume that the slowest rate works
379 * fine, or else other management frames will also be
380 * failing and the link will break */
381 SAMPLE_TBL(mi
, new_idx
, col
) = i
+ 1;
387 minstrel_rate_init(void *priv
, struct ieee80211_supported_band
*sband
,
388 struct ieee80211_sta
*sta
, void *priv_sta
)
390 struct minstrel_sta_info
*mi
= priv_sta
;
391 struct minstrel_priv
*mp
= priv
;
392 struct ieee80211_local
*local
= hw_to_local(mp
->hw
);
393 struct ieee80211_rate
*ctl_rate
;
394 unsigned int i
, n
= 0;
395 unsigned int t_slot
= 9; /* FIXME: get real slot time */
397 mi
->lowest_rix
= rate_lowest_index(sband
, sta
);
398 ctl_rate
= &sband
->bitrates
[mi
->lowest_rix
];
399 mi
->sp_ack_dur
= ieee80211_frame_duration(local
, 10, ctl_rate
->bitrate
,
400 !!(ctl_rate
->flags
& IEEE80211_RATE_ERP_G
), 1);
402 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
403 struct minstrel_rate
*mr
= &mi
->r
[n
];
404 unsigned int tx_time
= 0, tx_time_cts
= 0, tx_time_rtscts
= 0;
405 unsigned int tx_time_single
;
406 unsigned int cw
= mp
->cw_min
;
408 if (!rate_supported(sta
, sband
->band
, i
))
411 memset(mr
, 0, sizeof(*mr
));
414 mr
->bitrate
= sband
->bitrates
[i
].bitrate
/ 5;
415 calc_rate_durations(mi
, local
, mr
,
416 &sband
->bitrates
[i
]);
418 /* calculate maximum number of retransmissions before
419 * fallback (based on maximum segment size) */
421 mr
->retry_count_cts
= 1;
422 mr
->retry_count_rtscts
= 1;
423 tx_time
= mr
->perfect_tx_time
+ mi
->sp_ack_dur
;
425 /* add one retransmission */
426 tx_time_single
= mr
->ack_time
+ mr
->perfect_tx_time
;
428 /* contention window */
429 tx_time_single
+= t_slot
+ min(cw
, mp
->cw_max
);
432 tx_time
+= tx_time_single
;
433 tx_time_cts
+= tx_time_single
+ mi
->sp_ack_dur
;
434 tx_time_rtscts
+= tx_time_single
+ 2 * mi
->sp_ack_dur
;
435 if ((tx_time_cts
< mp
->segment_size
) &&
436 (mr
->retry_count_cts
< mp
->max_retry
))
437 mr
->retry_count_cts
++;
438 if ((tx_time_rtscts
< mp
->segment_size
) &&
439 (mr
->retry_count_rtscts
< mp
->max_retry
))
440 mr
->retry_count_rtscts
++;
441 } while ((tx_time
< mp
->segment_size
) &&
442 (++mr
->retry_count
< mp
->max_retry
));
443 mr
->adjusted_retry_count
= mr
->retry_count
;
446 for (i
= n
; i
< sband
->n_bitrates
; i
++) {
447 struct minstrel_rate
*mr
= &mi
->r
[i
];
452 mi
->stats_update
= jiffies
;
454 init_sample_table(mi
);
458 minstrel_alloc_sta(void *priv
, struct ieee80211_sta
*sta
, gfp_t gfp
)
460 struct ieee80211_supported_band
*sband
;
461 struct minstrel_sta_info
*mi
;
462 struct minstrel_priv
*mp
= priv
;
463 struct ieee80211_hw
*hw
= mp
->hw
;
467 mi
= kzalloc(sizeof(struct minstrel_sta_info
), gfp
);
471 for (i
= 0; i
< IEEE80211_NUM_BANDS
; i
++) {
472 sband
= hw
->wiphy
->bands
[hw
->conf
.channel
->band
];
473 if (sband
->n_bitrates
> max_rates
)
474 max_rates
= sband
->n_bitrates
;
477 mi
->r
= kzalloc(sizeof(struct minstrel_rate
) * max_rates
, gfp
);
481 mi
->sample_table
= kmalloc(SAMPLE_COLUMNS
* max_rates
, gfp
);
482 if (!mi
->sample_table
)
485 mi
->stats_update
= jiffies
;
496 minstrel_free_sta(void *priv
, struct ieee80211_sta
*sta
, void *priv_sta
)
498 struct minstrel_sta_info
*mi
= priv_sta
;
500 kfree(mi
->sample_table
);
506 minstrel_clear(void *priv
)
511 minstrel_alloc(struct ieee80211_hw
*hw
, struct dentry
*debugfsdir
)
513 struct minstrel_priv
*mp
;
515 mp
= kzalloc(sizeof(struct minstrel_priv
), GFP_ATOMIC
);
519 /* contention window settings
520 * Just an approximation. Using the per-queue values would complicate
521 * the calculations and is probably unnecessary */
525 /* number of packets (in %) to use for sampling other rates
526 * sample less often for non-mrr packets, because the overhead
527 * is much higher than with mrr */
528 mp
->lookaround_rate
= 5;
529 mp
->lookaround_rate_mrr
= 10;
531 /* moving average weight for EWMA */
534 /* maximum time that the hw is allowed to stay in one MRR segment */
535 mp
->segment_size
= 6000;
537 if (hw
->max_altrate_tries
> 0)
538 mp
->max_retry
= hw
->max_altrate_tries
;
540 /* safe default, does not necessarily have to match hw properties */
543 if (hw
->max_altrates
>= 3)
547 mp
->update_interval
= 100;
553 minstrel_free(void *priv
)
558 static struct rate_control_ops mac80211_minstrel
= {
560 .tx_status
= minstrel_tx_status
,
561 .get_rate
= minstrel_get_rate
,
562 .rate_init
= minstrel_rate_init
,
563 .clear
= minstrel_clear
,
564 .alloc
= minstrel_alloc
,
565 .free
= minstrel_free
,
566 .alloc_sta
= minstrel_alloc_sta
,
567 .free_sta
= minstrel_free_sta
,
568 #ifdef CONFIG_MAC80211_DEBUGFS
569 .add_sta_debugfs
= minstrel_add_sta_debugfs
,
570 .remove_sta_debugfs
= minstrel_remove_sta_debugfs
,
575 rc80211_minstrel_init(void)
577 return ieee80211_rate_control_register(&mac80211_minstrel
);
581 rc80211_minstrel_exit(void)
583 ieee80211_rate_control_unregister(&mac80211_minstrel
);