1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 26 Dec 2020 14:23:47 +0100
3 Subject: [PATCH] mac80211: remove legacy minstrel rate control
5 Now that minstrel_ht supports legacy rates, it is no longer needed
7 Signed-off-by: Felix Fietkau <nbd@nbd.name>
9 delete mode 100644 net/mac80211/rc80211_minstrel.c
10 delete mode 100644 net/mac80211/rc80211_minstrel.h
11 delete mode 100644 net/mac80211/rc80211_minstrel_debugfs.c
13 --- a/net/mac80211/Makefile
14 +++ b/net/mac80211/Makefile
15 @@ -56,11 +56,9 @@ mac80211-$(CONFIG_PM) += pm.o
16 CFLAGS_trace.o := -I$(src)
18 rc80211_minstrel-y := \
19 - rc80211_minstrel.o \
22 rc80211_minstrel-$(CPTCFG_MAC80211_DEBUGFS) += \
23 - rc80211_minstrel_debugfs.o \
24 rc80211_minstrel_ht_debugfs.o
26 mac80211-$(CPTCFG_MAC80211_RC_MINSTREL) += $(rc80211_minstrel-y)
27 --- a/net/mac80211/rc80211_minstrel.c
31 - * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
33 - * This program is free software; you can redistribute it and/or modify
34 - * it under the terms of the GNU General Public License version 2 as
35 - * published by the Free Software Foundation.
37 - * Based on minstrel.c:
38 - * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
39 - * Sponsored by Indranet Technologies Ltd
41 - * Based on sample.c:
42 - * Copyright (c) 2005 John Bicket
43 - * All rights reserved.
45 - * Redistribution and use in source and binary forms, with or without
46 - * modification, are permitted provided that the following conditions
48 - * 1. Redistributions of source code must retain the above copyright
49 - * notice, this list of conditions and the following disclaimer,
50 - * without modification.
51 - * 2. Redistributions in binary form must reproduce at minimum a disclaimer
52 - * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
53 - * redistribution must be conditioned upon including a substantially
54 - * similar Disclaimer requirement for further binary redistribution.
55 - * 3. Neither the names of the above-listed copyright holders nor the names
56 - * of any contributors may be used to endorse or promote products derived
57 - * from this software without specific prior written permission.
59 - * Alternatively, this software may be distributed under the terms of the
60 - * GNU General Public License ("GPL") version 2 as published by the Free
61 - * Software Foundation.
64 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 - * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
67 - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
68 - * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
69 - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
71 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
72 - * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
73 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
74 - * THE POSSIBILITY OF SUCH DAMAGES.
76 -#include <linux/netdevice.h>
77 -#include <linux/types.h>
78 -#include <linux/skbuff.h>
79 -#include <linux/debugfs.h>
80 -#include <linux/random.h>
81 -#include <linux/ieee80211.h>
82 -#include <linux/slab.h>
83 -#include <net/mac80211.h>
85 -#include "rc80211_minstrel.h"
87 -#define SAMPLE_TBL(_mi, _idx, _col) \
88 - _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
90 -/* convert mac80211 rate index to local array index */
92 -rix_to_ndx(struct minstrel_sta_info *mi, int rix)
95 - for (i = rix; i >= 0; i--)
96 - if (mi->r[i].rix == rix)
101 -/* return current EMWA throughput */
102 -int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg)
106 - usecs = mr->perfect_tx_time;
110 - /* reset thr. below 10% success */
111 - if (mr->stats.prob_avg < MINSTREL_FRAC(10, 100))
114 - if (prob_avg > MINSTREL_FRAC(90, 100))
115 - return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs));
117 - return MINSTREL_TRUNC(100000 * (prob_avg / usecs));
120 -/* find & sort topmost throughput rates */
122 -minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
125 - struct minstrel_rate_stats *tmp_mrs;
126 - struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats;
128 - for (j = MAX_THR_RATES; j > 0; --j) {
129 - tmp_mrs = &mi->r[tp_list[j - 1]].stats;
130 - if (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_avg) <=
131 - minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_avg))
135 - if (j < MAX_THR_RATES - 1)
136 - memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
137 - if (j < MAX_THR_RATES)
142 -minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
143 - int offset, int idx)
145 - struct minstrel_rate *r = &mi->r[idx];
147 - ratetbl->rate[offset].idx = r->rix;
148 - ratetbl->rate[offset].count = r->adjusted_retry_count;
149 - ratetbl->rate[offset].count_cts = r->retry_count_cts;
150 - ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts;
154 -minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
156 - struct ieee80211_sta_rates *ratetbl;
159 - ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
163 - /* Start with max_tp_rate */
164 - minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
166 - if (mp->hw->max_rates >= 3) {
167 - /* At least 3 tx rates supported, use max_tp_rate2 next */
168 - minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
171 - if (mp->hw->max_rates >= 2) {
172 - /* At least 2 tx rates supported, use max_prob_rate next */
173 - minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
176 - /* Use lowest rate last */
177 - ratetbl->rate[i].idx = mi->lowest_rix;
178 - ratetbl->rate[i].count = mp->max_retry;
179 - ratetbl->rate[i].count_cts = mp->max_retry;
180 - ratetbl->rate[i].count_rts = mp->max_retry;
182 - rate_control_set_rates(mp->hw, mi->sta, ratetbl);
186 -* Recalculate statistics and counters of a given rate
189 -minstrel_calc_rate_stats(struct minstrel_priv *mp,
190 - struct minstrel_rate_stats *mrs)
192 - unsigned int cur_prob;
194 - if (unlikely(mrs->attempts > 0)) {
195 - mrs->sample_skipped = 0;
196 - cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
198 - minstrel_filter_avg_add(&mrs->prob_avg,
199 - &mrs->prob_avg_1, cur_prob);
200 - } else if (unlikely(!mrs->att_hist)) {
201 - mrs->prob_avg = cur_prob;
203 - /*update exponential weighted moving avarage */
204 - mrs->prob_avg = minstrel_ewma(mrs->prob_avg,
208 - mrs->att_hist += mrs->attempts;
209 - mrs->succ_hist += mrs->success;
211 - mrs->sample_skipped++;
214 - mrs->last_success = mrs->success;
215 - mrs->last_attempts = mrs->attempts;
221 -minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
223 - u8 tmp_tp_rate[MAX_THR_RATES];
224 - u8 tmp_prob_rate = 0;
225 - int i, tmp_cur_tp, tmp_prob_tp;
227 - for (i = 0; i < MAX_THR_RATES; i++)
228 - tmp_tp_rate[i] = 0;
230 - for (i = 0; i < mi->n_rates; i++) {
231 - struct minstrel_rate *mr = &mi->r[i];
232 - struct minstrel_rate_stats *mrs = &mi->r[i].stats;
233 - struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats;
235 - /* Update statistics of success probability per rate */
236 - minstrel_calc_rate_stats(mp, mrs);
238 - /* Sample less often below the 10% chance of success.
239 - * Sample less often above the 95% chance of success. */
240 - if (mrs->prob_avg > MINSTREL_FRAC(95, 100) ||
241 - mrs->prob_avg < MINSTREL_FRAC(10, 100)) {
242 - mr->adjusted_retry_count = mrs->retry_count >> 1;
243 - if (mr->adjusted_retry_count > 2)
244 - mr->adjusted_retry_count = 2;
245 - mr->sample_limit = 4;
247 - mr->sample_limit = -1;
248 - mr->adjusted_retry_count = mrs->retry_count;
250 - if (!mr->adjusted_retry_count)
251 - mr->adjusted_retry_count = 2;
253 - minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
255 - /* To determine the most robust rate (max_prob_rate) used at
256 - * 3rd mmr stage we distinct between two cases:
257 - * (1) if any success probabilitiy >= 95%, out of those rates
258 - * choose the maximum throughput rate as max_prob_rate
259 - * (2) if all success probabilities < 95%, the rate with
260 - * highest success probability is chosen as max_prob_rate */
261 - if (mrs->prob_avg >= MINSTREL_FRAC(95, 100)) {
262 - tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_avg);
263 - tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate],
264 - tmp_mrs->prob_avg);
265 - if (tmp_cur_tp >= tmp_prob_tp)
268 - if (mrs->prob_avg >= tmp_mrs->prob_avg)
273 - /* Assign the new rate set */
274 - memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
275 - mi->max_prob_rate = tmp_prob_rate;
277 -#ifdef CPTCFG_MAC80211_DEBUGFS
278 - /* use fixed index if set */
279 - if (mp->fixed_rate_idx != -1) {
280 - mi->max_tp_rate[0] = mp->fixed_rate_idx;
281 - mi->max_tp_rate[1] = mp->fixed_rate_idx;
282 - mi->max_prob_rate = mp->fixed_rate_idx;
286 - /* Reset update timer */
287 - mi->last_stats_update = jiffies;
289 - minstrel_update_rates(mp, mi);
293 -minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
294 - void *priv_sta, struct ieee80211_tx_status *st)
296 - struct ieee80211_tx_info *info = st->info;
297 - struct minstrel_priv *mp = priv;
298 - struct minstrel_sta_info *mi = priv_sta;
299 - struct ieee80211_tx_rate *ar = info->status.rates;
303 - success = !!(info->flags & IEEE80211_TX_STAT_ACK);
305 - for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
306 - if (ar[i].idx < 0 || !ar[i].count)
309 - ndx = rix_to_ndx(mi, ar[i].idx);
313 - mi->r[ndx].stats.attempts += ar[i].count;
315 - if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
316 - mi->r[ndx].stats.success += success;
319 - if (time_after(jiffies, mi->last_stats_update +
320 - mp->update_interval / (mp->new_avg ? 2 : 1)))
321 - minstrel_update_stats(mp, mi);
325 -static inline unsigned int
326 -minstrel_get_retry_count(struct minstrel_rate *mr,
327 - struct ieee80211_tx_info *info)
329 - u8 retry = mr->adjusted_retry_count;
331 - if (info->control.use_rts)
332 - retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry));
333 - else if (info->control.use_cts_prot)
334 - retry = max_t(u8, 2, min(mr->retry_count_cts, retry));
340 -minstrel_get_next_sample(struct minstrel_sta_info *mi)
342 - unsigned int sample_ndx;
343 - sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
345 - if ((int) mi->sample_row >= mi->n_rates) {
346 - mi->sample_row = 0;
347 - mi->sample_column++;
348 - if (mi->sample_column >= SAMPLE_COLUMNS)
349 - mi->sample_column = 0;
355 -minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
356 - void *priv_sta, struct ieee80211_tx_rate_control *txrc)
358 - struct sk_buff *skb = txrc->skb;
359 - struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
360 - struct minstrel_sta_info *mi = priv_sta;
361 - struct minstrel_priv *mp = priv;
362 - struct ieee80211_tx_rate *rate = &info->control.rates[0];
363 - struct minstrel_rate *msr, *mr;
368 - int sampling_ratio;
370 - /* check multi-rate-retry capabilities & adjust lookaround_rate */
371 - mrr_capable = mp->has_mrr &&
373 - !txrc->bss_conf->use_cts_prot;
375 - sampling_ratio = mp->lookaround_rate_mrr;
377 - sampling_ratio = mp->lookaround_rate;
379 - /* increase sum packet counter */
380 - mi->total_packets++;
382 -#ifdef CPTCFG_MAC80211_DEBUGFS
383 - if (mp->fixed_rate_idx != -1)
387 - /* Don't use EAPOL frames for sampling on non-mrr hw */
388 - if (mp->hw->max_rates == 1 &&
389 - (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
392 - delta = (mi->total_packets * sampling_ratio / 100) -
393 - mi->sample_packets;
395 - /* delta < 0: no sampling required */
396 - prev_sample = mi->prev_sample;
397 - mi->prev_sample = false;
398 - if (delta < 0 || (!mrr_capable && prev_sample))
401 - if (mi->total_packets >= 10000) {
402 - mi->sample_packets = 0;
403 - mi->total_packets = 0;
404 - } else if (delta > mi->n_rates * 2) {
405 - /* With multi-rate retry, not every planned sample
406 - * attempt actually gets used, due to the way the retry
407 - * chain is set up - [max_tp,sample,prob,lowest] for
408 - * sample_rate < max_tp.
410 - * If there's too much sampling backlog and the link
411 - * starts getting worse, minstrel would start bursting
412 - * out lots of sampling frames, which would result
413 - * in a large throughput loss. */
414 - mi->sample_packets += (delta - mi->n_rates * 2);
417 - /* get next random rate sample */
418 - ndx = minstrel_get_next_sample(mi);
420 - mr = &mi->r[mi->max_tp_rate[0]];
422 - /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
423 - * rate sampling method should be used.
424 - * Respect such rates that are not sampled for 20 interations.
426 - if (msr->perfect_tx_time < mr->perfect_tx_time ||
427 - msr->stats.sample_skipped >= 20) {
428 - if (!msr->sample_limit)
431 - mi->sample_packets++;
432 - if (msr->sample_limit > 0)
433 - msr->sample_limit--;
436 - /* If we're not using MRR and the sampling rate already
437 - * has a probability of >95%, we shouldn't be attempting
438 - * to use it, as this only wastes precious airtime */
439 - if (!mrr_capable &&
440 - (mi->r[ndx].stats.prob_avg > MINSTREL_FRAC(95, 100)))
443 - mi->prev_sample = true;
445 - rate->idx = mi->r[ndx].rix;
446 - rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
447 - info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
452 -calc_rate_durations(enum nl80211_band band,
453 - struct minstrel_rate *d,
454 - struct ieee80211_rate *rate,
455 - struct cfg80211_chan_def *chandef)
457 - int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
458 - int shift = ieee80211_chandef_get_shift(chandef);
460 - d->perfect_tx_time = ieee80211_frame_duration(band, 1200,
461 - DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
463 - d->ack_time = ieee80211_frame_duration(band, 10,
464 - DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
469 -init_sample_table(struct minstrel_sta_info *mi)
471 - unsigned int i, col, new_idx;
474 - mi->sample_column = 0;
475 - mi->sample_row = 0;
476 - memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
478 - for (col = 0; col < SAMPLE_COLUMNS; col++) {
479 - prandom_bytes(rnd, sizeof(rnd));
480 - for (i = 0; i < mi->n_rates; i++) {
481 - new_idx = (i + rnd[i & 7]) % mi->n_rates;
482 - while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
483 - new_idx = (new_idx + 1) % mi->n_rates;
485 - SAMPLE_TBL(mi, new_idx, col) = i;
491 -minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
492 - struct cfg80211_chan_def *chandef,
493 - struct ieee80211_sta *sta, void *priv_sta)
495 - struct minstrel_sta_info *mi = priv_sta;
496 - struct minstrel_priv *mp = priv;
497 - struct ieee80211_rate *ctl_rate;
498 - unsigned int i, n = 0;
499 - unsigned int t_slot = 9; /* FIXME: get real slot time */
503 - mi->lowest_rix = rate_lowest_index(sband, sta);
504 - ctl_rate = &sband->bitrates[mi->lowest_rix];
505 - mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
507 - !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1,
508 - ieee80211_chandef_get_shift(chandef));
510 - rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
511 - memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
512 - mi->max_prob_rate = 0;
514 - for (i = 0; i < sband->n_bitrates; i++) {
515 - struct minstrel_rate *mr = &mi->r[n];
516 - struct minstrel_rate_stats *mrs = &mi->r[n].stats;
517 - unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
518 - unsigned int tx_time_single;
519 - unsigned int cw = mp->cw_min;
522 - if (!rate_supported(sta, sband->band, i))
524 - if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
528 - memset(mr, 0, sizeof(*mr));
529 - memset(mrs, 0, sizeof(*mrs));
532 - shift = ieee80211_chandef_get_shift(chandef);
533 - mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
535 - calc_rate_durations(sband->band, mr, &sband->bitrates[i],
538 - /* calculate maximum number of retransmissions before
539 - * fallback (based on maximum segment size) */
540 - mr->sample_limit = -1;
541 - mrs->retry_count = 1;
542 - mr->retry_count_cts = 1;
543 - mrs->retry_count_rtscts = 1;
544 - tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
546 - /* add one retransmission */
547 - tx_time_single = mr->ack_time + mr->perfect_tx_time;
549 - /* contention window */
550 - tx_time_single += (t_slot * cw) >> 1;
551 - cw = min((cw << 1) | 1, mp->cw_max);
553 - tx_time += tx_time_single;
554 - tx_time_cts += tx_time_single + mi->sp_ack_dur;
555 - tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
556 - if ((tx_time_cts < mp->segment_size) &&
557 - (mr->retry_count_cts < mp->max_retry))
558 - mr->retry_count_cts++;
559 - if ((tx_time_rtscts < mp->segment_size) &&
560 - (mrs->retry_count_rtscts < mp->max_retry))
561 - mrs->retry_count_rtscts++;
562 - } while ((tx_time < mp->segment_size) &&
563 - (++mr->stats.retry_count < mp->max_retry));
564 - mr->adjusted_retry_count = mrs->retry_count;
565 - if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
566 - mr->retry_count_cts = mrs->retry_count;
569 - for (i = n; i < sband->n_bitrates; i++) {
570 - struct minstrel_rate *mr = &mi->r[i];
575 - mi->last_stats_update = jiffies;
577 - init_sample_table(mi);
578 - minstrel_update_rates(mp, mi);
581 -static u32 minstrel_get_expected_throughput(void *priv_sta)
583 - struct minstrel_sta_info *mi = priv_sta;
584 - struct minstrel_rate_stats *tmp_mrs;
585 - int idx = mi->max_tp_rate[0];
588 - /* convert pkt per sec in kbps (1200 is the average pkt size used for
591 - tmp_mrs = &mi->r[idx].stats;
592 - tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_avg) * 10;
593 - tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024;
598 -const struct rate_control_ops mac80211_minstrel = {
599 - .tx_status_ext = minstrel_tx_status,
600 - .get_rate = minstrel_get_rate,
601 - .rate_init = minstrel_rate_init,
602 - .get_expected_throughput = minstrel_get_expected_throughput,
604 --- a/net/mac80211/rc80211_minstrel.h
607 -/* SPDX-License-Identifier: GPL-2.0-only */
609 - * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
612 -#ifndef __RC_MINSTREL_H
613 -#define __RC_MINSTREL_H
615 -#define EWMA_LEVEL 96 /* ewma weighting factor [/EWMA_DIV] */
616 -#define EWMA_DIV 128
617 -#define SAMPLE_COLUMNS 10 /* number of columns in sample table */
619 -/* scaled fraction values */
620 -#define MINSTREL_SCALE 12
621 -#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
622 -#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
624 -/* number of highest throughput rates to consider*/
625 -#define MAX_THR_RATES 4
628 - * Coefficients for moving average with noise filter (period=16),
629 - * scaled by 10 bits
631 - * a1 = exp(-pi * sqrt(2) / period)
632 - * coeff2 = 2 * a1 * cos(sqrt(2) * 2 * pi / period)
633 - * coeff3 = -sqr(a1)
634 - * coeff1 = 1 - coeff2 - coeff3
636 -#define MINSTREL_AVG_COEFF1 (MINSTREL_FRAC(1, 1) - \
637 - MINSTREL_AVG_COEFF2 - \
638 - MINSTREL_AVG_COEFF3)
639 -#define MINSTREL_AVG_COEFF2 0x00001499
640 -#define MINSTREL_AVG_COEFF3 -0x0000092e
643 - * Perform EWMA (Exponentially Weighted Moving Average) calculation
646 -minstrel_ewma(int old, int new, int weight)
651 - incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
656 -static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
658 - s32 out_1 = *prev_1;
659 - s32 out_2 = *prev_2;
670 - val = MINSTREL_AVG_COEFF1 * in;
671 - val += MINSTREL_AVG_COEFF2 * out_1;
672 - val += MINSTREL_AVG_COEFF3 * out_2;
673 - val >>= MINSTREL_SCALE;
675 - if (val > 1 << MINSTREL_SCALE)
676 - val = 1 << MINSTREL_SCALE;
687 -struct minstrel_rate_stats {
688 - /* current / last sampling period attempts/success counters */
689 - u16 attempts, last_attempts;
690 - u16 success, last_success;
692 - /* total attempts/success counters */
693 - u32 att_hist, succ_hist;
695 - /* prob_avg - moving average of prob */
699 - /* maximum retry counts */
701 - u8 retry_count_rtscts;
704 - bool retry_updated;
707 -struct minstrel_rate {
711 - u8 retry_count_cts;
712 - u8 adjusted_retry_count;
714 - unsigned int perfect_tx_time;
715 - unsigned int ack_time;
719 - struct minstrel_rate_stats stats;
722 -struct minstrel_sta_info {
723 - struct ieee80211_sta *sta;
725 - unsigned long last_stats_update;
726 - unsigned int sp_ack_dur;
727 - unsigned int rate_avg;
729 - unsigned int lowest_rix;
731 - u8 max_tp_rate[MAX_THR_RATES];
733 - unsigned int total_packets;
734 - unsigned int sample_packets;
736 - unsigned int sample_row;
737 - unsigned int sample_column;
740 - struct minstrel_rate *r;
743 - /* sampling table */
747 -struct minstrel_priv {
748 - struct ieee80211_hw *hw;
752 - unsigned int cw_min;
753 - unsigned int cw_max;
754 - unsigned int max_retry;
755 - unsigned int segment_size;
756 - unsigned int update_interval;
757 - unsigned int lookaround_rate;
758 - unsigned int lookaround_rate_mrr;
761 - u8 ofdm_rates[NUM_NL80211_BANDS][8];
763 -#ifdef CPTCFG_MAC80211_DEBUGFS
765 - * enable fixed rate processing per RC
766 - * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
767 - * - write -1 to enable RC processing again
768 - * - setting will be applied on next update
770 - u32 fixed_rate_idx;
774 -struct minstrel_debugfs_info {
779 -extern const struct rate_control_ops mac80211_minstrel;
780 -void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
782 -/* Recalculate success probabilities and counters for a given rate using EWMA */
783 -void minstrel_calc_rate_stats(struct minstrel_priv *mp,
784 - struct minstrel_rate_stats *mrs);
785 -int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg);
788 -int minstrel_stats_open(struct inode *inode, struct file *file);
789 -int minstrel_stats_csv_open(struct inode *inode, struct file *file);
792 --- a/net/mac80211/rc80211_minstrel_debugfs.c
796 - * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
798 - * This program is free software; you can redistribute it and/or modify
799 - * it under the terms of the GNU General Public License version 2 as
800 - * published by the Free Software Foundation.
802 - * Based on minstrel.c:
803 - * Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
804 - * Sponsored by Indranet Technologies Ltd
806 - * Based on sample.c:
807 - * Copyright (c) 2005 John Bicket
808 - * All rights reserved.
810 - * Redistribution and use in source and binary forms, with or without
811 - * modification, are permitted provided that the following conditions
813 - * 1. Redistributions of source code must retain the above copyright
814 - * notice, this list of conditions and the following disclaimer,
815 - * without modification.
816 - * 2. Redistributions in binary form must reproduce at minimum a disclaimer
817 - * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
818 - * redistribution must be conditioned upon including a substantially
819 - * similar Disclaimer requirement for further binary redistribution.
820 - * 3. Neither the names of the above-listed copyright holders nor the names
821 - * of any contributors may be used to endorse or promote products derived
822 - * from this software without specific prior written permission.
824 - * Alternatively, this software may be distributed under the terms of the
825 - * GNU General Public License ("GPL") version 2 as published by the Free
826 - * Software Foundation.
829 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
830 - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
831 - * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
832 - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
833 - * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
834 - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
835 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
836 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
837 - * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
838 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
839 - * THE POSSIBILITY OF SUCH DAMAGES.
841 -#include <linux/netdevice.h>
842 -#include <linux/types.h>
843 -#include <linux/skbuff.h>
844 -#include <linux/debugfs.h>
845 -#include <linux/ieee80211.h>
846 -#include <linux/slab.h>
847 -#include <linux/export.h>
848 -#include <net/mac80211.h>
849 -#include "rc80211_minstrel.h"
852 -minstrel_stats_open(struct inode *inode, struct file *file)
854 - struct minstrel_sta_info *mi = inode->i_private;
855 - struct minstrel_debugfs_info *ms;
856 - unsigned int i, tp_max, tp_avg, eprob;
859 - ms = kmalloc(2048, GFP_KERNEL);
863 - file->private_data = ms;
865 - p += sprintf(p, "\n");
867 - "best __________rate_________ ____statistics___ ____last_____ ______sum-of________\n");
869 - "rate [name idx airtime max_tp] [avg(tp) avg(prob)] [retry|suc|att] [#success | #attempts]\n");
871 - for (i = 0; i < mi->n_rates; i++) {
872 - struct minstrel_rate *mr = &mi->r[i];
873 - struct minstrel_rate_stats *mrs = &mi->r[i].stats;
875 - *(p++) = (i == mi->max_tp_rate[0]) ? 'A' : ' ';
876 - *(p++) = (i == mi->max_tp_rate[1]) ? 'B' : ' ';
877 - *(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' ';
878 - *(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' ';
879 - *(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
881 - p += sprintf(p, " %3u%s ", mr->bitrate / 2,
882 - (mr->bitrate & 1 ? ".5" : " "));
883 - p += sprintf(p, "%3u ", i);
884 - p += sprintf(p, "%6u ", mr->perfect_tx_time);
886 - tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
887 - tp_avg = minstrel_get_tp_avg(mr, mrs->prob_avg);
888 - eprob = MINSTREL_TRUNC(mrs->prob_avg * 1000);
890 - p += sprintf(p, "%4u.%1u %4u.%1u %3u.%1u"
893 - tp_max / 10, tp_max % 10,
894 - tp_avg / 10, tp_avg % 10,
895 - eprob / 10, eprob % 10,
898 - mrs->last_attempts,
899 - (unsigned long long)mrs->succ_hist,
900 - (unsigned long long)mrs->att_hist);
902 - p += sprintf(p, "\nTotal packet count:: ideal %d "
903 - "lookaround %d\n\n",
904 - mi->total_packets - mi->sample_packets,
905 - mi->sample_packets);
906 - ms->len = p - ms->buf;
908 - WARN_ON(ms->len + sizeof(*ms) > 2048);
914 -minstrel_stats_csv_open(struct inode *inode, struct file *file)
916 - struct minstrel_sta_info *mi = inode->i_private;
917 - struct minstrel_debugfs_info *ms;
918 - unsigned int i, tp_max, tp_avg, eprob;
921 - ms = kmalloc(2048, GFP_KERNEL);
925 - file->private_data = ms;
928 - for (i = 0; i < mi->n_rates; i++) {
929 - struct minstrel_rate *mr = &mi->r[i];
930 - struct minstrel_rate_stats *mrs = &mi->r[i].stats;
932 - p += sprintf(p, "%s" ,((i == mi->max_tp_rate[0]) ? "A" : ""));
933 - p += sprintf(p, "%s" ,((i == mi->max_tp_rate[1]) ? "B" : ""));
934 - p += sprintf(p, "%s" ,((i == mi->max_tp_rate[2]) ? "C" : ""));
935 - p += sprintf(p, "%s" ,((i == mi->max_tp_rate[3]) ? "D" : ""));
936 - p += sprintf(p, "%s" ,((i == mi->max_prob_rate) ? "P" : ""));
938 - p += sprintf(p, ",%u%s", mr->bitrate / 2,
939 - (mr->bitrate & 1 ? ".5," : ","));
940 - p += sprintf(p, "%u,", i);
941 - p += sprintf(p, "%u,",mr->perfect_tx_time);
943 - tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
944 - tp_avg = minstrel_get_tp_avg(mr, mrs->prob_avg);
945 - eprob = MINSTREL_TRUNC(mrs->prob_avg * 1000);
947 - p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u,%u,%u,"
948 - "%llu,%llu,%d,%d\n",
949 - tp_max / 10, tp_max % 10,
950 - tp_avg / 10, tp_avg % 10,
951 - eprob / 10, eprob % 10,
954 - mrs->last_attempts,
955 - (unsigned long long)mrs->succ_hist,
956 - (unsigned long long)mrs->att_hist,
957 - mi->total_packets - mi->sample_packets,
958 - mi->sample_packets);
961 - ms->len = p - ms->buf;
963 - WARN_ON(ms->len + sizeof(*ms) > 2048);
967 --- a/net/mac80211/rc80211_minstrel_ht.c
968 +++ b/net/mac80211/rc80211_minstrel_ht.c
970 #include <net/mac80211.h>
972 #include "sta_info.h"
973 -#include "rc80211_minstrel.h"
974 #include "rc80211_minstrel_ht.h"
976 #define AVG_AMPDU_SIZE 16
977 @@ -716,6 +715,83 @@ out:
978 mi->sample_mode = MINSTREL_SAMPLE_ACTIVE;
982 +minstrel_ewma(int old, int new, int weight)
987 + incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
992 +static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
994 + s32 out_1 = *prev_1;
995 + s32 out_2 = *prev_2;
1006 + val = MINSTREL_AVG_COEFF1 * in;
1007 + val += MINSTREL_AVG_COEFF2 * out_1;
1008 + val += MINSTREL_AVG_COEFF3 * out_2;
1009 + val >>= MINSTREL_SCALE;
1011 + if (val > 1 << MINSTREL_SCALE)
1012 + val = 1 << MINSTREL_SCALE;
1024 +* Recalculate statistics and counters of a given rate
1027 +minstrel_ht_calc_rate_stats(struct minstrel_priv *mp,
1028 + struct minstrel_rate_stats *mrs)
1030 + unsigned int cur_prob;
1032 + if (unlikely(mrs->attempts > 0)) {
1033 + mrs->sample_skipped = 0;
1034 + cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
1035 + if (mp->new_avg) {
1036 + minstrel_filter_avg_add(&mrs->prob_avg,
1037 + &mrs->prob_avg_1, cur_prob);
1038 + } else if (unlikely(!mrs->att_hist)) {
1039 + mrs->prob_avg = cur_prob;
1041 + /*update exponential weighted moving avarage */
1042 + mrs->prob_avg = minstrel_ewma(mrs->prob_avg,
1046 + mrs->att_hist += mrs->attempts;
1047 + mrs->succ_hist += mrs->success;
1049 + mrs->sample_skipped++;
1052 + mrs->last_success = mrs->success;
1053 + mrs->last_attempts = mrs->attempts;
1055 + mrs->attempts = 0;
1059 * Update rate statistics and select new primary rates
1061 @@ -808,7 +884,7 @@ minstrel_ht_update_stats(struct minstrel
1063 mrs = &mg->rates[i];
1064 mrs->retry_updated = false;
1065 - minstrel_calc_rate_stats(mp, mrs);
1066 + minstrel_ht_calc_rate_stats(mp, mrs);
1067 cur_prob = mrs->prob_avg;
1069 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
1070 @@ -960,8 +1036,7 @@ minstrel_ht_tx_status(void *priv, struct
1071 void *priv_sta, struct ieee80211_tx_status *st)
1073 struct ieee80211_tx_info *info = st->info;
1074 - struct minstrel_ht_sta_priv *msp = priv_sta;
1075 - struct minstrel_ht_sta *mi = &msp->ht;
1076 + struct minstrel_ht_sta *mi = priv_sta;
1077 struct ieee80211_tx_rate *ar = info->status.rates;
1078 struct minstrel_rate_stats *rate, *rate2, *rate_sample = NULL;
1079 struct minstrel_priv *mp = priv;
1080 @@ -1372,8 +1447,7 @@ minstrel_ht_get_rate(void *priv, struct
1081 const struct mcs_group *sample_group;
1082 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
1083 struct ieee80211_tx_rate *rate = &info->status.rates[0];
1084 - struct minstrel_ht_sta_priv *msp = priv_sta;
1085 - struct minstrel_ht_sta *mi = &msp->ht;
1086 + struct minstrel_ht_sta *mi = priv_sta;
1087 struct minstrel_priv *mp = priv;
1090 @@ -1484,8 +1558,7 @@ minstrel_ht_update_caps(void *priv, stru
1091 struct ieee80211_sta *sta, void *priv_sta)
1093 struct minstrel_priv *mp = priv;
1094 - struct minstrel_ht_sta_priv *msp = priv_sta;
1095 - struct minstrel_ht_sta *mi = &msp->ht;
1096 + struct minstrel_ht_sta *mi = priv_sta;
1097 struct ieee80211_mcs_info *mcs = &sta->ht_cap.mcs;
1098 u16 ht_cap = sta->ht_cap.cap;
1099 struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1100 @@ -1647,7 +1720,7 @@ static void *
1101 minstrel_ht_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1103 struct ieee80211_supported_band *sband;
1104 - struct minstrel_ht_sta_priv *msp;
1105 + struct minstrel_ht_sta *mi;
1106 struct minstrel_priv *mp = priv;
1107 struct ieee80211_hw *hw = mp->hw;
1109 @@ -1659,35 +1732,13 @@ minstrel_ht_alloc_sta(void *priv, struct
1110 max_rates = sband->n_bitrates;
1113 - msp = kzalloc(sizeof(*msp), gfp);
1117 - msp->ratelist = kcalloc(max_rates, sizeof(struct minstrel_rate), gfp);
1118 - if (!msp->ratelist)
1121 - msp->sample_table = kmalloc_array(max_rates, SAMPLE_COLUMNS, gfp);
1122 - if (!msp->sample_table)
1128 - kfree(msp->ratelist);
1132 + return kzalloc(sizeof(*mi), gfp);
1136 minstrel_ht_free_sta(void *priv, struct ieee80211_sta *sta, void *priv_sta)
1138 - struct minstrel_ht_sta_priv *msp = priv_sta;
1140 - kfree(msp->sample_table);
1141 - kfree(msp->ratelist);
1147 @@ -1768,12 +1819,6 @@ minstrel_ht_alloc(struct ieee80211_hw *h
1151 - /* number of packets (in %) to use for sampling other rates
1152 - * sample less often for non-mrr packets, because the overhead
1153 - * is much higher than with mrr */
1154 - mp->lookaround_rate = 5;
1155 - mp->lookaround_rate_mrr = 10;
1157 /* maximum time that the hw is allowed to stay in one MRR segment */
1158 mp->segment_size = 6000;
1160 @@ -1821,8 +1866,7 @@ minstrel_ht_free(void *priv)
1162 static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1164 - struct minstrel_ht_sta_priv *msp = priv_sta;
1165 - struct minstrel_ht_sta *mi = &msp->ht;
1166 + struct minstrel_ht_sta *mi = priv_sta;
1167 int i, j, prob, tp_avg;
1169 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1170 --- a/net/mac80211/rc80211_minstrel_ht.h
1171 +++ b/net/mac80211/rc80211_minstrel_ht.h
1173 #ifndef __RC_MINSTREL_HT_H
1174 #define __RC_MINSTREL_HT_H
1176 +/* number of highest throughput rates to consider*/
1177 +#define MAX_THR_RATES 4
1178 +#define SAMPLE_COLUMNS 10 /* number of columns in sample table */
1180 +/* scaled fraction values */
1181 +#define MINSTREL_SCALE 12
1182 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
1183 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
1185 +#define EWMA_LEVEL 96 /* ewma weighting factor [/EWMA_DIV] */
1186 +#define EWMA_DIV 128
1189 + * Coefficients for moving average with noise filter (period=16),
1190 + * scaled by 10 bits
1192 + * a1 = exp(-pi * sqrt(2) / period)
1193 + * coeff2 = 2 * a1 * cos(sqrt(2) * 2 * pi / period)
1194 + * coeff3 = -sqr(a1)
1195 + * coeff1 = 1 - coeff2 - coeff3
1197 +#define MINSTREL_AVG_COEFF1 (MINSTREL_FRAC(1, 1) - \
1198 + MINSTREL_AVG_COEFF2 - \
1199 + MINSTREL_AVG_COEFF3)
1200 +#define MINSTREL_AVG_COEFF2 0x00001499
1201 +#define MINSTREL_AVG_COEFF3 -0x0000092e
1204 * The number of streams can be changed to 2 to reduce code
1205 * size and memory footprint.
1208 #define MCS_GROUP_RATES 10
1210 +struct minstrel_priv {
1211 + struct ieee80211_hw *hw;
1214 + u32 sample_switch;
1215 + unsigned int cw_min;
1216 + unsigned int cw_max;
1217 + unsigned int max_retry;
1218 + unsigned int segment_size;
1219 + unsigned int update_interval;
1222 + u8 ofdm_rates[NUM_NL80211_BANDS][8];
1224 +#ifdef CPTCFG_MAC80211_DEBUGFS
1226 + * enable fixed rate processing per RC
1227 + * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
1228 + * - write -1 to enable RC processing again
1229 + * - setting will be applied on next update
1231 + u32 fixed_rate_idx;
1239 @@ -42,6 +95,26 @@ extern const s16 minstrel_cck_bitrates[4
1240 extern const s16 minstrel_ofdm_bitrates[8];
1241 extern const struct mcs_group minstrel_mcs_groups[];
1243 +struct minstrel_rate_stats {
1244 + /* current / last sampling period attempts/success counters */
1245 + u16 attempts, last_attempts;
1246 + u16 success, last_success;
1248 + /* total attempts/success counters */
1249 + u32 att_hist, succ_hist;
1251 + /* prob_avg - moving average of prob */
1255 + /* maximum retry counts */
1257 + u8 retry_count_rtscts;
1259 + u8 sample_skipped;
1260 + bool retry_updated;
1263 struct minstrel_mcs_group_data {
1266 @@ -111,12 +184,6 @@ struct minstrel_ht_sta {
1267 struct minstrel_mcs_group_data groups[MINSTREL_GROUPS_NB];
1270 -struct minstrel_ht_sta_priv {
1271 - struct minstrel_ht_sta ht;
1273 - void *sample_table;
1276 void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
1277 int minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
1279 --- a/net/mac80211/rc80211_minstrel_ht_debugfs.c
1280 +++ b/net/mac80211/rc80211_minstrel_ht_debugfs.c
1282 #include <linux/ieee80211.h>
1283 #include <linux/export.h>
1284 #include <net/mac80211.h>
1285 -#include "rc80211_minstrel.h"
1286 #include "rc80211_minstrel_ht.h"
1288 +struct minstrel_debugfs_info {
1294 minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
1296 @@ -127,8 +131,7 @@ minstrel_ht_stats_dump(struct minstrel_h
1298 minstrel_ht_stats_open(struct inode *inode, struct file *file)
1300 - struct minstrel_ht_sta_priv *msp = inode->i_private;
1301 - struct minstrel_ht_sta *mi = &msp->ht;
1302 + struct minstrel_ht_sta *mi = inode->i_private;
1303 struct minstrel_debugfs_info *ms;
1306 @@ -276,8 +279,7 @@ minstrel_ht_stats_csv_dump(struct minstr
1308 minstrel_ht_stats_csv_open(struct inode *inode, struct file *file)
1310 - struct minstrel_ht_sta_priv *msp = inode->i_private;
1311 - struct minstrel_ht_sta *mi = &msp->ht;
1312 + struct minstrel_ht_sta *mi = inode->i_private;
1313 struct minstrel_debugfs_info *ms;
1316 @@ -313,10 +315,8 @@ static const struct file_operations mins
1318 minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
1320 - struct minstrel_ht_sta_priv *msp = priv_sta;
1322 - debugfs_create_file("rc_stats", 0444, dir, msp,
1323 + debugfs_create_file("rc_stats", 0444, dir, priv_sta,
1324 &minstrel_ht_stat_fops);
1325 - debugfs_create_file("rc_stats_csv", 0444, dir, msp,
1326 + debugfs_create_file("rc_stats_csv", 0444, dir, priv_sta,
1327 &minstrel_ht_stat_csv_fops);