add the new compat-wireless for 2.6.27 + multi-rate retry and minstrel patches, renam...
[openwrt.git] / package / mac80211 / patches / 300-minstrel.patch
1 --- /dev/null
2 +++ b/net/mac80211/rc80211_minstrel.c
3 @@ -0,0 +1,583 @@
4 +/*
5 + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 + *
7 + * This program is free software; you can redistribute it and/or modify
8 + * it under the terms of the GNU General Public License version 2 as
9 + * published by the Free Software Foundation.
10 + *
11 + * Based on minstrel.c:
12 + * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
13 + * Sponsored by Indranet Technologies Ltd
14 + *
15 + * Based on sample.c:
16 + * Copyright (c) 2005 John Bicket
17 + * All rights reserved.
18 + *
19 + * Redistribution and use in source and binary forms, with or without
20 + * modification, are permitted provided that the following conditions
21 + * are met:
22 + * 1. Redistributions of source code must retain the above copyright
23 + * notice, this list of conditions and the following disclaimer,
24 + * without modification.
25 + * 2. Redistributions in binary form must reproduce at minimum a disclaimer
26 + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
27 + * redistribution must be conditioned upon including a substantially
28 + * similar Disclaimer requirement for further binary redistribution.
29 + * 3. Neither the names of the above-listed copyright holders nor the names
30 + * of any contributors may be used to endorse or promote products derived
31 + * from this software without specific prior written permission.
32 + *
33 + * Alternatively, this software may be distributed under the terms of the
34 + * GNU General Public License ("GPL") version 2 as published by the Free
35 + * Software Foundation.
36 + *
37 + * NO WARRANTY
38 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
41 + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
42 + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
43 + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
46 + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
48 + * THE POSSIBILITY OF SUCH DAMAGES.
49 + */
50 +#include <linux/netdevice.h>
51 +#include <linux/types.h>
52 +#include <linux/skbuff.h>
53 +#include <linux/debugfs.h>
54 +#include <linux/random.h>
55 +#include <linux/ieee80211.h>
56 +#include <net/mac80211.h>
57 +#include "rate.h"
58 +#include "rc80211_minstrel.h"
59 +
60 +#define SAMPLE_COLUMNS 10
61 +#define SAMPLE_TBL(_mi, _idx, _col) \
62 + _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
63 +
64 +/* convert mac80211 rate index to local array index */
65 +static inline int
66 +rix_to_ndx(struct minstrel_sta_info *mi, int rix)
67 +{
68 + int i = rix;
69 + for (i = rix; i >= 0; i--)
70 + if (mi->r[i].rix == rix)
71 + break;
72 + WARN_ON(mi->r[i].rix != rix);
73 + return i;
74 +}
75 +
76 +static inline bool
77 +use_low_rate(struct sk_buff *skb)
78 +{
79 + struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
80 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
81 + u16 fc;
82 +
83 + fc = le16_to_cpu(hdr->frame_control);
84 +
85 + return ((info->flags & IEEE80211_TX_CTL_NO_ACK) ||
86 + (fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
87 + is_multicast_ether_addr(hdr->addr1));
88 +}
89 +
90 +
91 +static void
92 +minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
93 +{
94 + u32 max_tp = 0, index_max_tp = 0, index_max_tp2 = 0;
95 + u32 max_prob = 0, index_max_prob = 0;
96 + u32 usecs;
97 + u32 p;
98 + int i;
99 +
100 + mi->stats_update = jiffies;
101 + for (i = 0; i < mi->n_rates; i++) {
102 + struct minstrel_rate *mr = &mi->r[i];
103 +
104 + usecs = mr->perfect_tx_time;
105 + if (!usecs)
106 + usecs = 1000000;
107 +
108 + /* To avoid rounding issues, probabilities scale from 0 (0%)
109 + * to 18000 (100%) */
110 + if (mr->attempts) {
111 + p = (mr->success * 18000) / mr->attempts;
112 + mr->succ_hist += mr->success;
113 + mr->att_hist += mr->attempts;
114 + mr->cur_prob = p;
115 + p = ((p * (100 - mp->ewma_level)) + (mr->probability *
116 + mp->ewma_level)) / 100;
117 + mr->probability = p;
118 + mr->cur_tp = p * (1000000 / usecs);
119 + }
120 +
121 + mr->last_success = mr->success;
122 + mr->last_attempts = mr->attempts;
123 + mr->success = 0;
124 + mr->attempts = 0;
125 +
126 + /* Sample less often below the 10% chance of success.
127 + * Sample less often above the 95% chance of success. */
128 + if ((mr->probability > 17100) || (mr->probability < 1800)) {
129 + mr->adjusted_retry_count = mr->retry_count >> 1;
130 + if (mr->adjusted_retry_count > 2)
131 + mr->adjusted_retry_count = 2;
132 + } else {
133 + mr->adjusted_retry_count = mr->retry_count;
134 + }
135 + if (!mr->adjusted_retry_count)
136 + mr->adjusted_retry_count = 2;
137 + }
138 +
139 + for (i = 0; i < mi->n_rates; i++) {
140 + struct minstrel_rate *mr = &mi->r[i];
141 + if (max_tp < mr->cur_tp) {
142 + index_max_tp = i;
143 + max_tp = mr->cur_tp;
144 + }
145 + if (max_prob < mr->probability) {
146 + index_max_prob = i;
147 + max_prob = mr->probability;
148 + }
149 + }
150 +
151 + max_tp = 0;
152 + for (i = 0; i < mi->n_rates; i++) {
153 + struct minstrel_rate *mr = &mi->r[i];
154 +
155 + if (i == index_max_tp)
156 + continue;
157 +
158 + if (max_tp < mr->cur_tp) {
159 + index_max_tp2 = i;
160 + max_tp = mr->cur_tp;
161 + }
162 + }
163 + mi->max_tp_rate = index_max_tp;
164 + mi->max_tp_rate2 = index_max_tp2;
165 + mi->max_prob_rate = index_max_prob;
166 +}
167 +
168 +static void
169 +minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
170 + struct ieee80211_sta *sta, void *priv_sta,
171 + struct sk_buff *skb)
172 +{
173 + struct minstrel_sta_info *mi = priv_sta;
174 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
175 + struct ieee80211_tx_altrate *ar = info->status.retries;
176 + struct minstrel_priv *mp = priv;
177 + int i, ndx, tries;
178 + int success = 0;
179 +
180 + if (!info->status.excessive_retries)
181 + success = 1;
182 +
183 + if (!mp->has_mrr || (ar[0].rate_idx < 0)) {
184 + ndx = rix_to_ndx(mi, info->tx_rate_idx);
185 + tries = info->status.retry_count + 1;
186 + mi->r[ndx].success += success;
187 + mi->r[ndx].attempts += tries;
188 + return;
189 + }
190 +
191 + for (i = 0; i < 4; i++) {
192 + if (ar[i].rate_idx < 0)
193 + break;
194 +
195 + ndx = rix_to_ndx(mi, ar[i].rate_idx);
196 + mi->r[ndx].attempts += ar[i].limit + 1;
197 +
198 + if ((i != 3) && (ar[i + 1].rate_idx < 0))
199 + mi->r[ndx].success += success;
200 + }
201 +
202 + if ((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) && (i >= 0))
203 + mi->sample_count++;
204 +
205 + if (mi->sample_deferred > 0)
206 + mi->sample_deferred--;
207 +}
208 +
209 +
210 +static inline unsigned int
211 +minstrel_get_retry_count(struct minstrel_rate *mr,
212 + struct ieee80211_tx_info *info)
213 +{
214 + unsigned int retry = mr->adjusted_retry_count;
215 +
216 + if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
217 + retry = max(2U, min(mr->retry_count_rtscts, retry));
218 + else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
219 + retry = max(2U, min(mr->retry_count_cts, retry));
220 + return retry;
221 +}
222 +
223 +
224 +static int
225 +minstrel_get_next_sample(struct minstrel_sta_info *mi)
226 +{
227 + unsigned int sample_ndx;
228 + sample_ndx = SAMPLE_TBL(mi, mi->sample_idx, mi->sample_column);
229 + mi->sample_idx++;
230 + if (mi->sample_idx > (mi->n_rates - 2)) {
231 + mi->sample_idx = 0;
232 + mi->sample_column++;
233 + if (mi->sample_column >= SAMPLE_COLUMNS)
234 + mi->sample_column = 0;
235 + }
236 + return sample_ndx;
237 +}
238 +
239 +void
240 +minstrel_get_rate(void *priv, struct ieee80211_supported_band *sband,
241 + struct ieee80211_sta *sta, void *priv_sta,
242 + struct sk_buff *skb, struct rate_selection *sel)
243 +{
244 + struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
245 + struct minstrel_sta_info *mi = priv_sta;
246 + struct minstrel_priv *mp = priv;
247 + struct ieee80211_tx_altrate *ar = info->control.retries;
248 + unsigned int ndx, sample_ndx = 0;
249 + bool mrr;
250 + bool sample_slower = false;
251 + bool sample = false;
252 + int i, delta;
253 + int mrr_ndx[3];
254 + int sample_rate;
255 +
256 + if (!sta || !mi || use_low_rate(skb)) {
257 + sel->rate_idx = rate_lowest_index(sband, sta);
258 + return;
259 + }
260 +
261 + mrr = mp->has_mrr;
262 +
263 + /* mac80211 does not allow mrr for RTS/CTS */
264 + if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) ||
265 + (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT))
266 + mrr = false;
267 +
268 + if (time_after(jiffies, mi->stats_update + (mp->update_interval *
269 + HZ) / 1000))
270 + minstrel_update_stats(mp, mi);
271 +
272 + ndx = mi->max_tp_rate;
273 +
274 + if (mrr)
275 + sample_rate = mp->lookaround_rate_mrr;
276 + else
277 + sample_rate = mp->lookaround_rate;
278 +
279 + mi->packet_count++;
280 + delta = (mi->packet_count * sample_rate / 100) -
281 + (mi->sample_count + mi->sample_deferred / 2);
282 +
283 + /* delta > 0: sampling required */
284 + if (delta > 0) {
285 + if (mi->packet_count >= 10000) {
286 + mi->sample_deferred = 0;
287 + mi->sample_count = 0;
288 + mi->packet_count = 0;
289 + } else if (delta > mi->n_rates * 2) {
290 + /* With multi-rate retry, not every planned sample
291 + * attempt actually gets used, due to the way the retry
292 + * chain is set up - [max_tp,sample,prob,lowest] for
293 + * sample_rate < max_tp.
294 + *
295 + * If there's too much sampling backlog and the link
296 + * starts getting worse, minstrel would start bursting
297 + * out lots of sampling frames, which would result
298 + * in a large throughput loss. */
299 + mi->sample_count += (delta - mi->n_rates * 2);
300 + }
301 +
302 + sample_ndx = minstrel_get_next_sample(mi);
303 + sample = true;
304 + sample_slower = mrr && (mi->r[sample_ndx].perfect_tx_time >
305 + mi->r[ndx].perfect_tx_time);
306 +
307 + if (!sample_slower) {
308 + ndx = sample_ndx;
309 + mi->sample_count++;
310 + } else {
311 + /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
312 + * packets that have the sampling rate deferred to the
313 + * second MRR stage. Increase the sample counter only
314 + * if the deferred sample rate was actually used.
315 + * Use the sample_deferred counter to make sure that
316 + * the sampling is not done in large bursts */
317 + info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
318 + mi->sample_deferred++;
319 + }
320 + }
321 + sel->rate_idx = mi->r[ndx].rix;
322 + info->control.retry_limit = minstrel_get_retry_count(&mi->r[ndx], info);
323 +
324 + if (!mrr) {
325 + ar[0].rate_idx = mi->lowest_rix;
326 + ar[0].limit = mp->max_retry;
327 + ar[1].rate_idx = -1;
328 + return;
329 + }
330 +
331 + /* MRR setup */
332 + if (sample) {
333 + if (sample_slower)
334 + mrr_ndx[0] = sample_ndx;
335 + else
336 + mrr_ndx[0] = mi->max_tp_rate;
337 + } else {
338 + mrr_ndx[0] = mi->max_tp_rate2;
339 + }
340 + mrr_ndx[1] = mi->max_prob_rate;
341 + mrr_ndx[2] = 0;
342 + for (i = 0; i < 3; i++) {
343 + ar[i].rate_idx = mi->r[mrr_ndx[i]].rix;
344 + ar[i].limit = mi->r[mrr_ndx[i]].adjusted_retry_count;
345 + }
346 +}
347 +
348 +
349 +static void
350 +calc_rate_durations(struct minstrel_sta_info *mi, struct ieee80211_local *local,
351 + struct minstrel_rate *d, struct ieee80211_rate *rate)
352 +{
353 + int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
354 +
355 + d->perfect_tx_time = ieee80211_frame_duration(local, 1200,
356 + rate->bitrate, erp, 1);
357 + d->ack_time = ieee80211_frame_duration(local, 10,
358 + rate->bitrate, erp, 1);
359 +}
360 +
361 +static void
362 +init_sample_table(struct minstrel_sta_info *mi)
363 +{
364 + unsigned int i, col, new_idx;
365 + unsigned int n_srates = mi->n_rates - 1;
366 + u8 rnd[8];
367 +
368 + mi->sample_column = 0;
369 + mi->sample_idx = 0;
370 + memset(mi->sample_table, 0, SAMPLE_COLUMNS * mi->n_rates);
371 +
372 + for (col = 0; col < SAMPLE_COLUMNS; col++) {
373 + for (i = 0; i < n_srates; i++) {
374 + get_random_bytes(rnd, sizeof(rnd));
375 + new_idx = (i + rnd[i & 7]) % n_srates;
376 +
377 + while (SAMPLE_TBL(mi, new_idx, col) != 0)
378 + new_idx = (new_idx + 1) % n_srates;
379 +
380 + /* Don't sample the slowest rate (i.e. slowest base
381 + * rate). We must presume that the slowest rate works
382 + * fine, or else other management frames will also be
383 + * failing and the link will break */
384 + SAMPLE_TBL(mi, new_idx, col) = i + 1;
385 + }
386 + }
387 +}
388 +
389 +static void
390 +minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
391 + struct ieee80211_sta *sta, void *priv_sta)
392 +{
393 + struct minstrel_sta_info *mi = priv_sta;
394 + struct minstrel_priv *mp = priv;
395 + struct minstrel_rate *mr_ctl;
396 + unsigned int i, n = 0;
397 + unsigned int t_slot = 9; /* FIXME: get real slot time */
398 +
399 + mi->lowest_rix = rate_lowest_index(sband, sta);
400 + mr_ctl = &mi->r[rix_to_ndx(mi, mi->lowest_rix)];
401 + mi->sp_ack_dur = mr_ctl->ack_time;
402 +
403 + for (i = 0; i < sband->n_bitrates; i++) {
404 + struct minstrel_rate *mr = &mi->r[n];
405 + unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
406 + unsigned int tx_time_single;
407 + unsigned int cw = mp->cw_min;
408 +
409 + if (!rate_supported(sta, sband->band, i))
410 + continue;
411 + n++;
412 + memset(mr, 0, sizeof(*mr));
413 +
414 + mr->rix = i;
415 + mr->bitrate = sband->bitrates[i].bitrate / 5;
416 + calc_rate_durations(mi, hw_to_local(mp->hw), mr,
417 + &sband->bitrates[i]);
418 +
419 + /* calculate maximum number of retransmissions before
420 + * fallback (based on maximum segment size) */
421 + mr->retry_count = 1;
422 + mr->retry_count_cts = 1;
423 + mr->retry_count_rtscts = 1;
424 + tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
425 + do {
426 + /* add one retransmission */
427 + tx_time_single = mr->ack_time + mr->perfect_tx_time;
428 +
429 + /* contention window */
430 + tx_time_single += t_slot + min(cw, mp->cw_max);
431 + cw = (cw + 1) << 1;
432 +
433 + tx_time += tx_time_single;
434 + tx_time_cts += tx_time_single + mi->sp_ack_dur;
435 + tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
436 + if ((tx_time_cts < mp->segment_size) &&
437 + (mr->retry_count_cts < mp->max_retry))
438 + mr->retry_count_cts++;
439 + if ((tx_time_rtscts < mp->segment_size) &&
440 + (mr->retry_count_rtscts < mp->max_retry))
441 + mr->retry_count_rtscts++;
442 + } while ((tx_time < mp->segment_size) &&
443 + (++mr->retry_count < mp->max_retry));
444 + mr->adjusted_retry_count = mr->retry_count;
445 + }
446 +
447 + for (i = n; i < sband->n_bitrates; i++) {
448 + struct minstrel_rate *mr = &mi->r[i];
449 + mr->rix = -1;
450 + }
451 +
452 + mi->n_rates = n;
453 + mi->stats_update = jiffies;
454 +
455 + init_sample_table(mi);
456 +}
457 +
458 +static void *
459 +minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
460 +{
461 + struct ieee80211_supported_band *sband;
462 + struct minstrel_sta_info *mi;
463 + struct minstrel_priv *mp = priv;
464 + struct ieee80211_hw *hw = mp->hw;
465 + int max_rates = 0;
466 + int i;
467 +
468 + mi = kzalloc(sizeof(struct minstrel_sta_info), gfp);
469 + if (!mi)
470 + return NULL;
471 +
472 + for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
473 + sband = hw->wiphy->bands[hw->conf.channel->band];
474 + if (sband->n_bitrates > max_rates)
475 + max_rates = sband->n_bitrates;
476 + }
477 +
478 + mi->r = kzalloc(sizeof(struct minstrel_rate) * max_rates, gfp);
479 + if (!mi->r)
480 + goto error;
481 +
482 + mi->sample_table = kmalloc(SAMPLE_COLUMNS * max_rates, gfp);
483 + if (!mi->sample_table)
484 + goto error1;
485 +
486 + mi->stats_update = jiffies;
487 + return mi;
488 +
489 +error1:
490 + kfree(mi->r);
491 +error:
492 + kfree(mi);
493 + return NULL;
494 +}
495 +
496 +static void
497 +minstrel_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
498 +{
499 + struct minstrel_sta_info *mi = priv_sta;
500 +
501 + kfree(mi->sample_table);
502 + kfree(mi->r);
503 + kfree(mi);
504 +}
505 +
506 +static void
507 +minstrel_clear(void *priv)
508 +{
509 +}
510 +
511 +static void *
512 +minstrel_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
513 +{
514 + struct minstrel_priv *mp;
515 +
516 + mp = kzalloc(sizeof(struct minstrel_priv), GFP_ATOMIC);
517 + if (!mp)
518 + return NULL;
519 +
520 + /* contention window settings
521 + * Just an approximation. Using the per-queue values would complicate
522 + * the calculations and is probably unnecessary */
523 + mp->cw_min = 15;
524 + mp->cw_max = 1023;
525 +
526 + /* number of packets (in %) to use for sampling other rates
527 + * sample less often for non-mrr packets, because the overhead
528 + * is much higher than with mrr */
529 + mp->lookaround_rate = 5;
530 + mp->lookaround_rate_mrr = 10;
531 +
532 + /* moving average weight for EWMA */
533 + mp->ewma_level = 75;
534 +
535 + /* maximum time that the hw is allowed to stay in one MRR segment */
536 + mp->segment_size = 6000;
537 +
538 + if (hw->max_altrate_tries > 0)
539 + mp->max_retry = hw->max_altrate_tries;
540 + else
541 + /* safe default, does not necessarily have to match hw properties */
542 + mp->max_retry = 7;
543 +
544 + if (hw->max_altrates >= 3)
545 + mp->has_mrr = true;
546 +
547 + mp->hw = hw;
548 + mp->update_interval = 100;
549 +
550 + return mp;
551 +}
552 +
553 +static void
554 +minstrel_free(void *priv)
555 +{
556 + kfree(priv);
557 +}
558 +
559 +static struct rate_control_ops mac80211_minstrel = {
560 + .name = "minstrel",
561 + .tx_status = minstrel_tx_status,
562 + .get_rate = minstrel_get_rate,
563 + .rate_init = minstrel_rate_init,
564 + .clear = minstrel_clear,
565 + .alloc = minstrel_alloc,
566 + .free = minstrel_free,
567 + .alloc_sta = minstrel_alloc_sta,
568 + .free_sta = minstrel_free_sta,
569 +#ifdef CONFIG_MAC80211_DEBUGFS
570 + .add_sta_debugfs = minstrel_add_sta_debugfs,
571 + .remove_sta_debugfs = minstrel_remove_sta_debugfs,
572 +#endif
573 +};
574 +
575 +int __init
576 +rc80211_minstrel_init(void)
577 +{
578 + return ieee80211_rate_control_register(&mac80211_minstrel);
579 +}
580 +
581 +void
582 +rc80211_minstrel_exit(void)
583 +{
584 + ieee80211_rate_control_unregister(&mac80211_minstrel);
585 +}
586 +
587 --- a/net/mac80211/Makefile
588 +++ b/net/mac80211/Makefile
589 @@ -41,4 +41,8 @@
590 rc80211_pid-y := rc80211_pid_algo.o
591 rc80211_pid-$(CONFIG_MAC80211_DEBUGFS) += rc80211_pid_debugfs.o
592
593 +rc80211_minstrel-y := rc80211_minstrel.o
594 +rc80211_minstrel-$(CONFIG_MAC80211_DEBUGFS) += rc80211_minstrel_debugfs.o
595 +
596 mac80211-$(CONFIG_MAC80211_RC_PID) += $(rc80211_pid-y)
597 +mac80211-$(CONFIG_MAC80211_RC_MINSTREL) += $(rc80211_minstrel-y)
598 --- a/net/mac80211/main.c
599 +++ b/net/mac80211/main.c
600 @@ -1015,6 +1015,10 @@
601 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
602 IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
603
604 + ret = rc80211_minstrel_init();
605 + if (ret)
606 + return ret;
607 +
608 ret = rc80211_pid_init();
609 if (ret)
610 return ret;
611 @@ -1027,6 +1031,7 @@
612 static void __exit ieee80211_exit(void)
613 {
614 rc80211_pid_exit();
615 + rc80211_minstrel_exit();
616
617 /*
618 * For key todo, it'll be empty by now but the work
619 --- a/net/mac80211/rate.h
620 +++ b/net/mac80211/rate.h
621 @@ -125,4 +125,18 @@
622 }
623 #endif
624
625 +#ifdef CONFIG_MAC80211_RC_MINSTREL
626 +extern int rc80211_minstrel_init(void);
627 +extern void rc80211_minstrel_exit(void);
628 +#else
629 +static inline int rc80211_minstrel_init(void)
630 +{
631 + return 0;
632 +}
633 +static inline void rc80211_minstrel_exit(void)
634 +{
635 +}
636 +#endif
637 +
638 +
639 #endif /* IEEE80211_RATE_H */
640 --- /dev/null
641 +++ b/net/mac80211/rc80211_minstrel.h
642 @@ -0,0 +1,85 @@
643 +/*
644 + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
645 + *
646 + * This program is free software; you can redistribute it and/or modify
647 + * it under the terms of the GNU General Public License version 2 as
648 + * published by the Free Software Foundation.
649 + */
650 +
651 +#ifndef __RC_MINSTREL_H
652 +#define __RC_MINSTREL_H
653 +
654 +struct minstrel_rate {
655 + int bitrate;
656 + int rix;
657 +
658 + unsigned int perfect_tx_time;
659 + unsigned int ack_time;
660 +
661 + unsigned int retry_count;
662 + unsigned int retry_count_cts;
663 + unsigned int retry_count_rtscts;
664 + unsigned int adjusted_retry_count;
665 +
666 + u32 success;
667 + u32 attempts;
668 + u32 last_attempts;
669 + u32 last_success;
670 +
671 + /* parts per thousand */
672 + u32 cur_prob;
673 + u32 probability;
674 +
675 + /* per-rate throughput */
676 + u32 cur_tp;
677 + u32 throughput;
678 +
679 + u64 succ_hist;
680 + u64 att_hist;
681 +};
682 +
683 +struct minstrel_sta_info {
684 + unsigned long stats_update;
685 + unsigned int sp_ack_dur;
686 + unsigned int rate_avg;
687 +
688 + unsigned int lowest_rix;
689 +
690 + unsigned int max_tp_rate;
691 + unsigned int max_tp_rate2;
692 + unsigned int max_prob_rate;
693 + unsigned int packet_count;
694 + unsigned int sample_count;
695 + int sample_deferred;
696 +
697 + unsigned int sample_idx;
698 + unsigned int sample_column;
699 +
700 + int n_rates;
701 + struct minstrel_rate *r;
702 +
703 + /* sampling table */
704 + u8 *sample_table;
705 +
706 +#ifdef CONFIG_MAC80211_DEBUGFS
707 + struct dentry *dbg_stats;
708 +#endif
709 +};
710 +
711 +struct minstrel_priv {
712 + struct ieee80211_hw *hw;
713 + bool has_mrr;
714 + unsigned int cw_min;
715 + unsigned int cw_max;
716 + unsigned int max_retry;
717 + unsigned int ewma_level;
718 + unsigned int segment_size;
719 + unsigned int update_interval;
720 + unsigned int lookaround_rate;
721 + unsigned int lookaround_rate_mrr;
722 +};
723 +
724 +void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
725 +void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
726 +
727 +#endif
728 --- /dev/null
729 +++ b/net/mac80211/rc80211_minstrel_debugfs.c
730 @@ -0,0 +1,164 @@
731 +/*
732 + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
733 + *
734 + * This program is free software; you can redistribute it and/or modify
735 + * it under the terms of the GNU General Public License version 2 as
736 + * published by the Free Software Foundation.
737 + *
738 + * Based on minstrel.c:
739 + * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
740 + * Sponsored by Indranet Technologies Ltd
741 + *
742 + * Based on sample.c:
743 + * Copyright (c) 2005 John Bicket
744 + * All rights reserved.
745 + *
746 + * Redistribution and use in source and binary forms, with or without
747 + * modification, are permitted provided that the following conditions
748 + * are met:
749 + * 1. Redistributions of source code must retain the above copyright
750 + * notice, this list of conditions and the following disclaimer,
751 + * without modification.
752 + * 2. Redistributions in binary form must reproduce at minimum a disclaimer
753 + * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
754 + * redistribution must be conditioned upon including a substantially
755 + * similar Disclaimer requirement for further binary redistribution.
756 + * 3. Neither the names of the above-listed copyright holders nor the names
757 + * of any contributors may be used to endorse or promote products derived
758 + * from this software without specific prior written permission.
759 + *
760 + * Alternatively, this software may be distributed under the terms of the
761 + * GNU General Public License ("GPL") version 2 as published by the Free
762 + * Software Foundation.
763 + *
764 + * NO WARRANTY
765 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
766 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
767 + * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
768 + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
769 + * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
770 + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
771 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
772 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
773 + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
774 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
775 + * THE POSSIBILITY OF SUCH DAMAGES.
776 + */
777 +#include <linux/netdevice.h>
778 +#include <linux/types.h>
779 +#include <linux/skbuff.h>
780 +#include <linux/debugfs.h>
781 +#include <linux/ieee80211.h>
782 +#include <net/mac80211.h>
783 +#include "rc80211_minstrel.h"
784 +
785 +struct minstrel_stats_info {
786 + struct minstrel_sta_info *mi;
787 + char buf[4096];
788 + size_t len;
789 +};
790 +
791 +static int
792 +minstrel_stats_open(struct inode *inode, struct file *file)
793 +{
794 + struct minstrel_sta_info *mi = inode->i_private;
795 + struct minstrel_stats_info *ms;
796 + unsigned int i, tp, prob, eprob;
797 + char *p;
798 +
799 + ms = kmalloc(sizeof(*ms), GFP_KERNEL);
800 + if (!ms)
801 + return -ENOMEM;
802 +
803 + file->private_data = ms;
804 + p = ms->buf;
805 + p += sprintf(p, "rate throughput ewma prob this prob "
806 + "this succ/attempt success attempts\n");
807 + for (i = 0; i < mi->n_rates; i++) {
808 + struct minstrel_rate *mr = &mi->r[i];
809 +
810 + *(p++) = (i == mi->max_tp_rate) ? 'T' : ' ';
811 + *(p++) = (i == mi->max_tp_rate2) ? 't' : ' ';
812 + *(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
813 + p += sprintf(p, "%3u%s", mr->bitrate / 2,
814 + (mr->bitrate & 1 ? ".5" : " "));
815 +
816 + tp = ((mr->cur_tp * 96) / 18000) >> 10;
817 + prob = mr->cur_prob / 18;
818 + eprob = mr->probability / 18;
819 +
820 + p += sprintf(p, " %6u.%1u %6u.%1u %6u.%1u "
821 + "%3u(%3u) %8llu %8llu\n",
822 + tp / 10, tp % 10,
823 + eprob / 10, eprob % 10,
824 + prob / 10, prob % 10,
825 + mr->last_success,
826 + mr->last_attempts,
827 + mr->succ_hist,
828 + mr->att_hist);
829 + }
830 + p += sprintf(p, "\nTotal packet count:: ideal %d "
831 + "lookaround %d\n\n",
832 + mi->packet_count - mi->sample_count,
833 + mi->sample_count);
834 + ms->len = p - ms->buf;
835 +
836 + return 0;
837 +}
838 +
839 +static int
840 +minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *o)
841 +{
842 + struct minstrel_stats_info *ms;
843 + char *src;
844 +
845 + ms = file->private_data;
846 + src = ms->buf;
847 +
848 + len = min(len, ms->len);
849 + if (len <= *o)
850 + return 0;
851 +
852 + src += *o;
853 + len -= *o;
854 + *o += len;
855 +
856 + if (copy_to_user(buf, src, len))
857 + return -EFAULT;
858 +
859 + return len;
860 +}
861 +
862 +static int
863 +minstrel_stats_release(struct inode *inode, struct file *file)
864 +{
865 + struct minstrel_stats_info *ms = file->private_data;
866 +
867 + kfree(ms);
868 +
869 + return 0;
870 +}
871 +
872 +static struct file_operations minstrel_stat_fops = {
873 + .owner = THIS_MODULE,
874 + .open = minstrel_stats_open,
875 + .read = minstrel_stats_read,
876 + .release = minstrel_stats_release,
877 +};
878 +
879 +void
880 +minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
881 +{
882 + struct minstrel_sta_info *mi = priv_sta;
883 +
884 + mi->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, mi,
885 + &minstrel_stat_fops);
886 +}
887 +
888 +void
889 +minstrel_remove_sta_debugfs(void *priv, void *priv_sta)
890 +{
891 + struct minstrel_sta_info *mi = priv_sta;
892 +
893 + debugfs_remove(mi->dbg_stats);
894 +}
895 --- a/net/mac80211/Kconfig
896 +++ b/net/mac80211/Kconfig
897 @@ -22,6 +22,11 @@
898 mac80211 that uses a PID controller to select the TX
899 rate.
900
901 +config MAC80211_RC_MINSTREL
902 + bool "Minstrel"
903 + ---help---
904 + This option enables the 'minstrel' TX rate control algorithm
905 +
906 choice
907 prompt "Default rate control algorithm"
908 default MAC80211_RC_DEFAULT_PID
909 @@ -39,11 +44,19 @@
910 default rate control algorithm. You should choose
911 this unless you know what you are doing.
912
913 +config MAC80211_RC_DEFAULT_MINSTREL
914 + bool "Minstrel"
915 + depends on MAC80211_RC_MINSTREL
916 + ---help---
917 + Select Minstrel as the default rate control algorithm.
918 +
919 +
920 endchoice
921
922 config MAC80211_RC_DEFAULT
923 string
924 default "pid" if MAC80211_RC_DEFAULT_PID
925 + default "minstrel" if MAC80211_RC_DEFAULT_MINSTREL
926 default ""
927
928 endmenu
This page took 0.086993 seconds and 5 git commands to generate.