nl80211: fix monitor flags
[deliverable/linux.git] / net / mac80211 / rc80211_minstrel.c
CommitLineData
cccf129f
FF
1/*
2 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3 *
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.
7 *
8 * Based on minstrel.c:
9 * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
10 * Sponsored by Indranet Technologies Ltd
11 *
12 * Based on sample.c:
13 * Copyright (c) 2005 John Bicket
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
29 *
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.
33 *
34 * NO WARRANTY
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.
46 */
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>
54#include "rate.h"
55#include "rc80211_minstrel.h"
56
57#define SAMPLE_COLUMNS 10
58#define SAMPLE_TBL(_mi, _idx, _col) \
59 _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
60
61/* convert mac80211 rate index to local array index */
62static inline int
63rix_to_ndx(struct minstrel_sta_info *mi, int rix)
64{
65 int i = rix;
66 for (i = rix; i >= 0; i--)
67 if (mi->r[i].rix == rix)
68 break;
69 WARN_ON(mi->r[i].rix != rix);
70 return i;
71}
72
73static inline bool
74use_low_rate(struct sk_buff *skb)
75{
76 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
77 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
78 u16 fc;
79
80 fc = le16_to_cpu(hdr->frame_control);
81
82 return ((info->flags & IEEE80211_TX_CTL_NO_ACK) ||
83 (fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
84 is_multicast_ether_addr(hdr->addr1));
85}
86
87
88static void
89minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
90{
91 u32 max_tp = 0, index_max_tp = 0, index_max_tp2 = 0;
92 u32 max_prob = 0, index_max_prob = 0;
93 u32 usecs;
94 u32 p;
95 int i;
96
97 mi->stats_update = jiffies;
98 for (i = 0; i < mi->n_rates; i++) {
99 struct minstrel_rate *mr = &mi->r[i];
100
101 usecs = mr->perfect_tx_time;
102 if (!usecs)
103 usecs = 1000000;
104
105 /* To avoid rounding issues, probabilities scale from 0 (0%)
106 * to 18000 (100%) */
107 if (mr->attempts) {
108 p = (mr->success * 18000) / mr->attempts;
109 mr->succ_hist += mr->success;
110 mr->att_hist += mr->attempts;
111 mr->cur_prob = p;
112 p = ((p * (100 - mp->ewma_level)) + (mr->probability *
113 mp->ewma_level)) / 100;
114 mr->probability = p;
115 mr->cur_tp = p * (1000000 / usecs);
116 }
117
118 mr->last_success = mr->success;
119 mr->last_attempts = mr->attempts;
120 mr->success = 0;
121 mr->attempts = 0;
122
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;
129 } else {
130 mr->adjusted_retry_count = mr->retry_count;
131 }
132 if (!mr->adjusted_retry_count)
133 mr->adjusted_retry_count = 2;
134 }
135
136 for (i = 0; i < mi->n_rates; i++) {
137 struct minstrel_rate *mr = &mi->r[i];
138 if (max_tp < mr->cur_tp) {
139 index_max_tp = i;
140 max_tp = mr->cur_tp;
141 }
142 if (max_prob < mr->probability) {
143 index_max_prob = i;
144 max_prob = mr->probability;
145 }
146 }
147
148 max_tp = 0;
149 for (i = 0; i < mi->n_rates; i++) {
150 struct minstrel_rate *mr = &mi->r[i];
151
152 if (i == index_max_tp)
153 continue;
154
155 if (max_tp < mr->cur_tp) {
156 index_max_tp2 = i;
157 max_tp = mr->cur_tp;
158 }
159 }
160 mi->max_tp_rate = index_max_tp;
161 mi->max_tp_rate2 = index_max_tp2;
162 mi->max_prob_rate = index_max_prob;
163}
164
165static void
166minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
167 struct ieee80211_sta *sta, void *priv_sta,
168 struct sk_buff *skb)
169{
170 struct minstrel_sta_info *mi = priv_sta;
171 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e6a9854b
JB
172 struct ieee80211_tx_rate *ar = info->status.rates;
173 int i, ndx;
174 int success;
cccf129f 175
e6a9854b 176 success = !!(info->flags & IEEE80211_TX_STAT_ACK);
cccf129f 177
e6a9854b
JB
178 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
179 if (ar[i].idx < 0)
cccf129f
FF
180 break;
181
e6a9854b
JB
182 ndx = rix_to_ndx(mi, ar[i].idx);
183 mi->r[ndx].attempts += ar[i].count;
cccf129f 184
e6a9854b 185 if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
cccf129f
FF
186 mi->r[ndx].success += success;
187 }
188
189 if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0))
190 mi->sample_count++;
191
192 if (mi->sample_deferred > 0)
193 mi->sample_deferred--;
194}
195
196
197static inline unsigned int
198minstrel_get_retry_count(struct minstrel_rate *mr,
199 struct ieee80211_tx_info *info)
200{
201 unsigned int retry = mr->adjusted_retry_count;
202
e6a9854b 203 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
cccf129f 204 retry = max(2U, min(mr->retry_count_rtscts, retry));
e6a9854b 205 else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
cccf129f
FF
206 retry = max(2U, min(mr->retry_count_cts, retry));
207 return retry;
208}
209
210
211static int
212minstrel_get_next_sample(struct minstrel_sta_info *mi)
213{
214 unsigned int sample_ndx;
215 sample_ndx = SAMPLE_TBL(mi, mi->sample_idx, mi->sample_column);
216 mi->sample_idx++;
217 if (mi->sample_idx > (mi->n_rates - 2)) {
218 mi->sample_idx = 0;
219 mi->sample_column++;
220 if (mi->sample_column >= SAMPLE_COLUMNS)
221 mi->sample_column = 0;
222 }
223 return sample_ndx;
224}
225
226void
e6a9854b
JB
227minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
228 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
cccf129f 229{
e6a9854b
JB
230 struct sk_buff *skb = txrc->skb;
231 struct ieee80211_supported_band *sband = txrc->sband;
cccf129f
FF
232 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
233 struct minstrel_sta_info *mi = priv_sta;
234 struct minstrel_priv *mp = priv;
e6a9854b 235 struct ieee80211_tx_rate *ar = info->control.rates;
cccf129f
FF
236 unsigned int ndx, sample_ndx = 0;
237 bool mrr;
238 bool sample_slower = false;
239 bool sample = false;
240 int i, delta;
241 int mrr_ndx[3];
242 int sample_rate;
243
244 if (!sta || !mi || use_low_rate(skb)) {
e6a9854b
JB
245 ar[0].idx = rate_lowest_index(sband, sta);
246 ar[0].count = mp->max_retry;
cccf129f
FF
247 return;
248 }
249
e6a9854b 250 mrr = mp->has_mrr && !txrc->rts && !txrc->bss_conf->use_cts_prot;
cccf129f
FF
251
252 if (time_after(jiffies, mi->stats_update + (mp->update_interval *
253 HZ) / 1000))
254 minstrel_update_stats(mp, mi);
255
256 ndx = mi->max_tp_rate;
257
258 if (mrr)
259 sample_rate = mp->lookaround_rate_mrr;
260 else
261 sample_rate = mp->lookaround_rate;
262
263 mi->packet_count++;
264 delta = (mi->packet_count * sample_rate / 100) -
265 (mi->sample_count + mi->sample_deferred / 2);
266
267 /* delta > 0: sampling required */
268 if (delta > 0) {
269 if (mi->packet_count >= 10000) {
270 mi->sample_deferred = 0;
271 mi->sample_count = 0;
272 mi->packet_count = 0;
273 } else if (delta > mi->n_rates * 2) {
274 /* With multi-rate retry, not every planned sample
275 * attempt actually gets used, due to the way the retry
276 * chain is set up - [max_tp,sample,prob,lowest] for
277 * sample_rate < max_tp.
278 *
279 * If there's too much sampling backlog and the link
280 * starts getting worse, minstrel would start bursting
281 * out lots of sampling frames, which would result
282 * in a large throughput loss. */
283 mi->sample_count += (delta - mi->n_rates * 2);
284 }
285
286 sample_ndx = minstrel_get_next_sample(mi);
287 sample = true;
288 sample_slower = mrr && (mi->r[sample_ndx].perfect_tx_time >
289 mi->r[ndx].perfect_tx_time);
290
291 if (!sample_slower) {
292 ndx = sample_ndx;
293 mi->sample_count++;
294 } else {
295 /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
296 * packets that have the sampling rate deferred to the
297 * second MRR stage. Increase the sample counter only
298 * if the deferred sample rate was actually used.
299 * Use the sample_deferred counter to make sure that
300 * the sampling is not done in large bursts */
301 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
302 mi->sample_deferred++;
303 }
304 }
e6a9854b
JB
305 ar[0].idx = mi->r[ndx].rix;
306 ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info);
cccf129f
FF
307
308 if (!mrr) {
e6a9854b
JB
309 ar[1].idx = mi->lowest_rix;
310 ar[1].count = mp->max_retry;
cccf129f
FF
311 return;
312 }
313
314 /* MRR setup */
315 if (sample) {
316 if (sample_slower)
317 mrr_ndx[0] = sample_ndx;
318 else
319 mrr_ndx[0] = mi->max_tp_rate;
320 } else {
321 mrr_ndx[0] = mi->max_tp_rate2;
322 }
323 mrr_ndx[1] = mi->max_prob_rate;
324 mrr_ndx[2] = 0;
e6a9854b
JB
325 for (i = 1; i < 4; i++) {
326 ar[i].idx = mi->r[mrr_ndx[i - 1]].rix;
327 ar[i].count = mi->r[mrr_ndx[i - 1]].adjusted_retry_count;
cccf129f
FF
328 }
329}
330
331
332static void
333calc_rate_durations(struct minstrel_sta_info *mi, struct ieee80211_local *local,
334 struct minstrel_rate *d, struct ieee80211_rate *rate)
335{
336 int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
337
338 d->perfect_tx_time = ieee80211_frame_duration(local, 1200,
339 rate->bitrate, erp, 1);
340 d->ack_time = ieee80211_frame_duration(local, 10,
341 rate->bitrate, erp, 1);
342}
343
344static void
345init_sample_table(struct minstrel_sta_info *mi)
346{
347 unsigned int i, col, new_idx;
348 unsigned int n_srates = mi->n_rates - 1;
349 u8 rnd[8];
350
351 mi->sample_column = 0;
352 mi->sample_idx = 0;
353 memset(mi->sample_table, 0, SAMPLE_COLUMNS * mi->n_rates);
354
355 for (col = 0; col < SAMPLE_COLUMNS; col++) {
356 for (i = 0; i < n_srates; i++) {
357 get_random_bytes(rnd, sizeof(rnd));
358 new_idx = (i + rnd[i & 7]) % n_srates;
359
360 while (SAMPLE_TBL(mi, new_idx, col) != 0)
361 new_idx = (new_idx + 1) % n_srates;
362
363 /* Don't sample the slowest rate (i.e. slowest base
364 * rate). We must presume that the slowest rate works
365 * fine, or else other management frames will also be
366 * failing and the link will break */
367 SAMPLE_TBL(mi, new_idx, col) = i + 1;
368 }
369 }
370}
371
372static void
373minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
374 struct ieee80211_sta *sta, void *priv_sta)
375{
376 struct minstrel_sta_info *mi = priv_sta;
377 struct minstrel_priv *mp = priv;
378 struct minstrel_rate *mr_ctl;
379 unsigned int i, n = 0;
380 unsigned int t_slot = 9; /* FIXME: get real slot time */
381
382 mi->lowest_rix = rate_lowest_index(sband, sta);
383 mr_ctl = &mi->r[rix_to_ndx(mi, mi->lowest_rix)];
384 mi->sp_ack_dur = mr_ctl->ack_time;
385
386 for (i = 0; i < sband->n_bitrates; i++) {
387 struct minstrel_rate *mr = &mi->r[n];
388 unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
389 unsigned int tx_time_single;
390 unsigned int cw = mp->cw_min;
391
392 if (!rate_supported(sta, sband->band, i))
393 continue;
394 n++;
395 memset(mr, 0, sizeof(*mr));
396
397 mr->rix = i;
398 mr->bitrate = sband->bitrates[i].bitrate / 5;
399 calc_rate_durations(mi, hw_to_local(mp->hw), mr,
400 &sband->bitrates[i]);
401
402 /* calculate maximum number of retransmissions before
403 * fallback (based on maximum segment size) */
404 mr->retry_count = 1;
405 mr->retry_count_cts = 1;
406 mr->retry_count_rtscts = 1;
407 tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
408 do {
409 /* add one retransmission */
410 tx_time_single = mr->ack_time + mr->perfect_tx_time;
411
412 /* contention window */
413 tx_time_single += t_slot + min(cw, mp->cw_max);
414 cw = (cw + 1) << 1;
415
416 tx_time += tx_time_single;
417 tx_time_cts += tx_time_single + mi->sp_ack_dur;
418 tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
419 if ((tx_time_cts < mp->segment_size) &&
420 (mr->retry_count_cts < mp->max_retry))
421 mr->retry_count_cts++;
422 if ((tx_time_rtscts < mp->segment_size) &&
423 (mr->retry_count_rtscts < mp->max_retry))
424 mr->retry_count_rtscts++;
425 } while ((tx_time < mp->segment_size) &&
426 (++mr->retry_count < mp->max_retry));
427 mr->adjusted_retry_count = mr->retry_count;
428 }
429
430 for (i = n; i < sband->n_bitrates; i++) {
431 struct minstrel_rate *mr = &mi->r[i];
432 mr->rix = -1;
433 }
434
435 mi->n_rates = n;
436 mi->stats_update = jiffies;
437
438 init_sample_table(mi);
439}
440
441static void *
442minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
443{
444 struct ieee80211_supported_band *sband;
445 struct minstrel_sta_info *mi;
446 struct minstrel_priv *mp = priv;
447 struct ieee80211_hw *hw = mp->hw;
448 int max_rates = 0;
449 int i;
450
451 mi = kzalloc(sizeof(struct minstrel_sta_info), gfp);
452 if (!mi)
453 return NULL;
454
455 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
456 sband = hw->wiphy->bands[hw->conf.channel->band];
457 if (sband->n_bitrates > max_rates)
458 max_rates = sband->n_bitrates;
459 }
460
461 mi->r = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
462 if (!mi->r)
463 goto error;
464
465 mi->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
466 if (!mi->sample_table)
467 goto error1;
468
469 mi->stats_update = jiffies;
470 return mi;
471
472error1:
473 kfree(mi->r);
474error:
475 kfree(mi);
476 return NULL;
477}
478
479static void
480minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
481{
482 struct minstrel_sta_info *mi = priv_sta;
483
484 kfree(mi->sample_table);
485 kfree(mi->r);
486 kfree(mi);
487}
488
489static void
490minstrel_clear(void *priv)
491{
492}
493
494static void *
495minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
496{
497 struct minstrel_priv *mp;
498
499 mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
500 if (!mp)
501 return NULL;
502
503 /* contention window settings
504 * Just an approximation. Using the per-queue values would complicate
505 * the calculations and is probably unnecessary */
506 mp->cw_min = 15;
507 mp->cw_max = 1023;
508
509 /* number of packets (in %) to use for sampling other rates
510 * sample less often for non-mrr packets, because the overhead
511 * is much higher than with mrr */
512 mp->lookaround_rate = 5;
513 mp->lookaround_rate_mrr = 10;
514
515 /* moving average weight for EWMA */
516 mp->ewma_level = 75;
517
518 /* maximum time that the hw is allowed to stay in one MRR segment */
519 mp->segment_size = 6000;
520
e6a9854b
JB
521 if (hw->max_rate_tries > 0)
522 mp->max_retry = hw->max_rate_tries;
cccf129f
FF
523 else
524 /* safe default, does not necessarily have to match hw properties */
525 mp->max_retry = 7;
526
e6a9854b 527 if (hw->max_rates >= 4)
cccf129f
FF
528 mp->has_mrr = true;
529
530 mp->hw = hw;
531 mp->update_interval = 100;
532
533 return mp;
534}
535
536static void
537minstrel_free(void *priv)
538{
539 kfree(priv);
540}
541
542static struct rate_control_ops mac80211_minstrel = {
543 .name = "minstrel",
544 .tx_status = minstrel_tx_status,
545 .get_rate = minstrel_get_rate,
546 .rate_init = minstrel_rate_init,
547 .clear = minstrel_clear,
548 .alloc = minstrel_alloc,
549 .free = minstrel_free,
550 .alloc_sta = minstrel_alloc_sta,
551 .free_sta = minstrel_free_sta,
552#ifdef CONFIG_MAC80211_DEBUGFS
553 .add_sta_debugfs = minstrel_add_sta_debugfs,
554 .remove_sta_debugfs = minstrel_remove_sta_debugfs,
555#endif
556};
557
558int __init
559rc80211_minstrel_init(void)
560{
561 return ieee80211_rate_control_register(&mac80211_minstrel);
562}
563
564void
565rc80211_minstrel_exit(void)
566{
567 ieee80211_rate_control_unregister(&mac80211_minstrel);
568}
569
This page took 0.060639 seconds and 5 git commands to generate.