The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net80211/ieee80211_rssadapt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/10.4/sys/net80211/ieee80211_rssadapt.c 321725 2017-07-30 18:38:05Z avos $      */
    2 /* $NetBSD: ieee80211_rssadapt.c,v 1.9 2005/02/26 22:45:09 perry Exp $ */
    3 /*-
    4  * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
    5  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or
    8  * without modification, are permitted provided that the following
    9  * conditions are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above
   13  *    copyright notice, this list of conditions and the following
   14  *    disclaimer in the documentation and/or other materials provided
   15  *    with the distribution.
   16  * 3. The name of David Young may not be used to endorse or promote
   17  *    products derived from this software without specific prior
   18  *    written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
   21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
   24  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   26  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
   31  * OF SUCH DAMAGE.
   32  */
   33 #include "opt_wlan.h"
   34 
   35 #include <sys/param.h>
   36 #include <sys/kernel.h>
   37 #include <sys/module.h>
   38 #include <sys/socket.h>
   39 #include <sys/sysctl.h>
   40 
   41 #include <net/if.h>
   42 #include <net/if_media.h>
   43 
   44 #include <net80211/ieee80211_var.h>
   45 #include <net80211/ieee80211_rssadapt.h>
   46 #include <net80211/ieee80211_ratectl.h>
   47 
   48 struct rssadapt_expavgctl {
   49         /* RSS threshold decay. */
   50         u_int rc_decay_denom;
   51         u_int rc_decay_old;
   52         /* RSS threshold update. */
   53         u_int rc_thresh_denom;
   54         u_int rc_thresh_old;
   55         /* RSS average update. */
   56         u_int rc_avgrssi_denom;
   57         u_int rc_avgrssi_old;
   58 };
   59 
   60 static struct rssadapt_expavgctl master_expavgctl = {
   61         .rc_decay_denom = 16,
   62         .rc_decay_old = 15,
   63         .rc_thresh_denom = 8,
   64         .rc_thresh_old = 4,
   65         .rc_avgrssi_denom = 8,
   66         .rc_avgrssi_old = 4
   67 };
   68 
   69 #ifdef interpolate
   70 #undef interpolate
   71 #endif
   72 #define interpolate(parm, old, new) ((parm##_old * (old) + \
   73                                      (parm##_denom - parm##_old) * (new)) / \
   74                                     parm##_denom)
   75 
   76 static void     rssadapt_setinterval(const struct ieee80211vap *, int);
   77 static void     rssadapt_init(struct ieee80211vap *);
   78 static void     rssadapt_deinit(struct ieee80211vap *);
   79 static void     rssadapt_updatestats(struct ieee80211_rssadapt_node *);
   80 static void     rssadapt_node_init(struct ieee80211_node *);
   81 static void     rssadapt_node_deinit(struct ieee80211_node *);
   82 static int      rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
   83 static void     rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
   84 static void     rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
   85                         int, int);
   86 static void     rssadapt_tx_complete(const struct ieee80211vap *,
   87                         const struct ieee80211_node *, int,
   88                         void *, void *);
   89 static void     rssadapt_sysctlattach(struct ieee80211vap *,
   90                         struct sysctl_ctx_list *, struct sysctl_oid *);
   91 
   92 /* number of references from net80211 layer */
   93 static  int nrefs = 0;
   94 
   95 static const struct ieee80211_ratectl rssadapt = {
   96         .ir_name        = "rssadapt",
   97         .ir_attach      = NULL,
   98         .ir_detach      = NULL,
   99         .ir_init        = rssadapt_init,
  100         .ir_deinit      = rssadapt_deinit,
  101         .ir_node_init   = rssadapt_node_init,
  102         .ir_node_deinit = rssadapt_node_deinit,
  103         .ir_rate        = rssadapt_rate,
  104         .ir_tx_complete = rssadapt_tx_complete,
  105         .ir_tx_update   = NULL,
  106         .ir_setinterval = rssadapt_setinterval,
  107 };
  108 IEEE80211_RATECTL_MODULE(rssadapt, 1);
  109 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
  110 
  111 static void
  112 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
  113 {
  114         struct ieee80211_rssadapt *rs = vap->iv_rs;
  115         int t;
  116 
  117         if (msecs < 100)
  118                 msecs = 100;
  119         t = msecs_to_ticks(msecs);
  120         rs->interval = (t < 1) ? 1 : t;
  121 }
  122 
  123 static void
  124 rssadapt_init(struct ieee80211vap *vap)
  125 {
  126         struct ieee80211_rssadapt *rs;
  127 
  128         KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
  129             __func__));
  130 
  131         nrefs++;                /* XXX locking */
  132         vap->iv_rs = rs = malloc(sizeof(struct ieee80211_rssadapt),
  133             M_80211_RATECTL, M_NOWAIT|M_ZERO);
  134         if (rs == NULL) {
  135                 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
  136                 return;
  137         }
  138         rs->vap = vap;
  139         rssadapt_setinterval(vap, 500 /* msecs */);
  140         rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
  141 }
  142 
  143 static void
  144 rssadapt_deinit(struct ieee80211vap *vap)
  145 {
  146         free(vap->iv_rs, M_80211_RATECTL);
  147         KASSERT(nrefs > 0, ("imbalanced attach/detach"));
  148         nrefs--;                /* XXX locking */
  149 }
  150 
  151 static void
  152 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
  153 {
  154         long interval;
  155 
  156         ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
  157         ra->ra_nfail = ra->ra_nok = 0;
  158 
  159         /*
  160          * A node is eligible for its rate to be raised every 1/10 to 10
  161          * seconds, more eligible in proportion to recent packet rates.
  162          */
  163         interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
  164         ra->ra_raise_interval = msecs_to_ticks(interval);
  165 }
  166 
  167 static void
  168 rssadapt_node_init(struct ieee80211_node *ni)
  169 {
  170         struct ieee80211_rssadapt_node *ra;
  171         struct ieee80211vap *vap = ni->ni_vap;
  172         struct ieee80211_rssadapt *rsa = vap->iv_rs;
  173         const struct ieee80211_rateset *rs = &ni->ni_rates;
  174 
  175         if (ni->ni_rctls == NULL) {
  176                 ni->ni_rctls = ra = 
  177                     malloc(sizeof(struct ieee80211_rssadapt_node),
  178                         M_80211_RATECTL, M_NOWAIT|M_ZERO);
  179                 if (ra == NULL) {
  180                         if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
  181                             "structure\n");
  182                         return;
  183                 }
  184         } else
  185                 ra = ni->ni_rctls;
  186         ra->ra_rs = rsa;
  187         ra->ra_rates = *rs;
  188         rssadapt_updatestats(ra);
  189 
  190         /* pick initial rate */
  191         for (ra->ra_rix = rs->rs_nrates - 1;
  192              ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
  193              ra->ra_rix--)
  194                 ;
  195         ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
  196         ra->ra_ticks = ticks;
  197 
  198         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  199             "RSSADAPT initial rate %d", ni->ni_txrate);
  200 }
  201 
  202 static void
  203 rssadapt_node_deinit(struct ieee80211_node *ni)
  204 {
  205 
  206         free(ni->ni_rctls, M_80211_RATECTL);
  207 }
  208 
  209 static __inline int
  210 bucket(int pktlen)
  211 {
  212         int i, top, thridx;
  213 
  214         for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
  215              i < IEEE80211_RSSADAPT_BKTS;
  216              i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
  217                 thridx = i;
  218                 if (pktlen <= top)
  219                         break;
  220         }
  221         return thridx;
  222 }
  223 
  224 static int
  225 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
  226 {
  227         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  228         u_int pktlen = iarg;
  229         const struct ieee80211_rateset *rs = &ra->ra_rates;
  230         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  231         int rix, rssi;
  232 
  233         if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
  234                 rssadapt_updatestats(ra);
  235                 ra->ra_ticks = ticks;
  236         }
  237 
  238         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  239 
  240         /* XXX this is average rssi, should be using last value */
  241         rssi = ni->ni_ic->ic_node_getrssi(ni);
  242         for (rix = rs->rs_nrates-1; rix >= 0; rix--)
  243                 if ((*thrs)[rix] < (rssi << 8))
  244                         break;
  245         if (rix != ra->ra_rix) {
  246                 /* update public rate */
  247                 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
  248                 ra->ra_rix = rix;
  249 
  250                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  251                     "RSSADAPT new rate %d (pktlen %d rssi %d)",
  252                     ni->ni_txrate, pktlen, rssi);
  253         }
  254         return rix;
  255 }
  256 
  257 /*
  258  * Adapt the data rate to suit the conditions.  When a transmitted
  259  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
  260  * raise the RSS threshold for transmitting packets of similar length at
  261  * the same data rate.
  262  */
  263 static void
  264 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  265 {
  266         uint16_t last_thr;
  267         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  268         u_int rix;
  269 
  270         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  271 
  272         rix = ra->ra_rix;
  273         last_thr = (*thrs)[rix];
  274         (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
  275             last_thr, (rssi << 8));
  276 
  277         IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  278             "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
  279             ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  280             last_thr, (*thrs)[rix], rssi);
  281 }
  282 
  283 static void
  284 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  285 {
  286         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  287         uint16_t newthr, oldthr;
  288         int rix;
  289 
  290         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  291 
  292         rix = ra->ra_rix;
  293         if ((*thrs)[rix + 1] > (*thrs)[rix]) {
  294                 oldthr = (*thrs)[rix + 1];
  295                 if ((*thrs)[rix] == 0)
  296                         newthr = (rssi << 8);
  297                 else
  298                         newthr = (*thrs)[rix];
  299                 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
  300                     oldthr, newthr);
  301 
  302                 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  303                     "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
  304                     ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  305                     oldthr, newthr, rssi);
  306 
  307                 ra->ra_last_raise = ticks;
  308         }
  309 }
  310 
  311 static void
  312 rssadapt_tx_complete(const struct ieee80211vap *vap,
  313     const struct ieee80211_node *ni, int success, void *arg1, void *arg2)
  314 {
  315         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  316         int pktlen = *(int *)arg1, rssi = *(int *)arg2;
  317 
  318         if (success) {
  319                 ra->ra_nok++;
  320                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
  321                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
  322                         rssadapt_raise_rate(ra, pktlen, rssi);
  323         } else {
  324                 ra->ra_nfail++;
  325                 rssadapt_lower_rate(ra, pktlen, rssi);
  326         }
  327 }
  328 
  329 static int
  330 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
  331 {
  332         struct ieee80211vap *vap = arg1;
  333         struct ieee80211_rssadapt *rs = vap->iv_rs;
  334         int msecs = ticks_to_msecs(rs->interval);
  335         int error;
  336 
  337         error = sysctl_handle_int(oidp, &msecs, 0, req);
  338         if (error || !req->newptr)
  339                 return error;
  340         rssadapt_setinterval(vap, msecs);
  341         return 0;
  342 }
  343 
  344 static void
  345 rssadapt_sysctlattach(struct ieee80211vap *vap,
  346     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
  347 {
  348 
  349         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  350             "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
  351             0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
  352 }

Cache object: c3b5cca29f3eecafc283a9e4a87b3d20


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.