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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: releng/11.2/sys/net80211/ieee80211_rssadapt.c 321724 2017-07-30 18:29:28Z 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/systm.h>
   37 #include <sys/kernel.h>
   38 #include <sys/malloc.h>
   39 #include <sys/module.h>
   40 #include <sys/socket.h>
   41 #include <sys/sysctl.h>
   42 
   43 #include <net/if.h>
   44 #include <net/if_var.h>
   45 #include <net/if_media.h>
   46 #include <net/ethernet.h>
   47 
   48 #include <net80211/ieee80211_var.h>
   49 #include <net80211/ieee80211_rssadapt.h>
   50 #include <net80211/ieee80211_ratectl.h>
   51 
   52 struct rssadapt_expavgctl {
   53         /* RSS threshold decay. */
   54         u_int rc_decay_denom;
   55         u_int rc_decay_old;
   56         /* RSS threshold update. */
   57         u_int rc_thresh_denom;
   58         u_int rc_thresh_old;
   59         /* RSS average update. */
   60         u_int rc_avgrssi_denom;
   61         u_int rc_avgrssi_old;
   62 };
   63 
   64 static struct rssadapt_expavgctl master_expavgctl = {
   65         .rc_decay_denom = 16,
   66         .rc_decay_old = 15,
   67         .rc_thresh_denom = 8,
   68         .rc_thresh_old = 4,
   69         .rc_avgrssi_denom = 8,
   70         .rc_avgrssi_old = 4
   71 };
   72 
   73 #ifdef interpolate
   74 #undef interpolate
   75 #endif
   76 #define interpolate(parm, old, new) ((parm##_old * (old) + \
   77                                      (parm##_denom - parm##_old) * (new)) / \
   78                                     parm##_denom)
   79 
   80 static void     rssadapt_setinterval(const struct ieee80211vap *, int);
   81 static void     rssadapt_init(struct ieee80211vap *);
   82 static void     rssadapt_deinit(struct ieee80211vap *);
   83 static void     rssadapt_updatestats(struct ieee80211_rssadapt_node *);
   84 static void     rssadapt_node_init(struct ieee80211_node *);
   85 static void     rssadapt_node_deinit(struct ieee80211_node *);
   86 static int      rssadapt_rate(struct ieee80211_node *, void *, uint32_t);
   87 static void     rssadapt_lower_rate(struct ieee80211_rssadapt_node *, int, int);
   88 static void     rssadapt_raise_rate(struct ieee80211_rssadapt_node *,
   89                         int, int);
   90 static void     rssadapt_tx_complete(const struct ieee80211vap *,
   91                         const struct ieee80211_node *, int,
   92                         void *, void *);
   93 static void     rssadapt_sysctlattach(struct ieee80211vap *,
   94                         struct sysctl_ctx_list *, struct sysctl_oid *);
   95 
   96 /* number of references from net80211 layer */
   97 static  int nrefs = 0;
   98 
   99 static const struct ieee80211_ratectl rssadapt = {
  100         .ir_name        = "rssadapt",
  101         .ir_attach      = NULL,
  102         .ir_detach      = NULL,
  103         .ir_init        = rssadapt_init,
  104         .ir_deinit      = rssadapt_deinit,
  105         .ir_node_init   = rssadapt_node_init,
  106         .ir_node_deinit = rssadapt_node_deinit,
  107         .ir_rate        = rssadapt_rate,
  108         .ir_tx_complete = rssadapt_tx_complete,
  109         .ir_tx_update   = NULL,
  110         .ir_setinterval = rssadapt_setinterval,
  111 };
  112 IEEE80211_RATECTL_MODULE(rssadapt, 1);
  113 IEEE80211_RATECTL_ALG(rssadapt, IEEE80211_RATECTL_RSSADAPT, rssadapt);
  114 
  115 static void
  116 rssadapt_setinterval(const struct ieee80211vap *vap, int msecs)
  117 {
  118         struct ieee80211_rssadapt *rs = vap->iv_rs;
  119         int t;
  120 
  121         if (msecs < 100)
  122                 msecs = 100;
  123         t = msecs_to_ticks(msecs);
  124         rs->interval = (t < 1) ? 1 : t;
  125 }
  126 
  127 static void
  128 rssadapt_init(struct ieee80211vap *vap)
  129 {
  130         struct ieee80211_rssadapt *rs;
  131 
  132         KASSERT(vap->iv_rs == NULL, ("%s: iv_rs already initialized",
  133             __func__));
  134 
  135         nrefs++;                /* XXX locking */
  136         vap->iv_rs = rs = IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt),
  137             M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  138         if (rs == NULL) {
  139                 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
  140                 return;
  141         }
  142         rs->vap = vap;
  143         rssadapt_setinterval(vap, 500 /* msecs */);
  144         rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
  145 }
  146 
  147 static void
  148 rssadapt_deinit(struct ieee80211vap *vap)
  149 {
  150         IEEE80211_FREE(vap->iv_rs, M_80211_RATECTL);
  151         KASSERT(nrefs > 0, ("imbalanced attach/detach"));
  152         nrefs--;                /* XXX locking */
  153 }
  154 
  155 static void
  156 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
  157 {
  158         long interval;
  159 
  160         ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
  161         ra->ra_nfail = ra->ra_nok = 0;
  162 
  163         /*
  164          * A node is eligible for its rate to be raised every 1/10 to 10
  165          * seconds, more eligible in proportion to recent packet rates.
  166          */
  167         interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
  168         ra->ra_raise_interval = msecs_to_ticks(interval);
  169 }
  170 
  171 static void
  172 rssadapt_node_init(struct ieee80211_node *ni)
  173 {
  174         struct ieee80211_rssadapt_node *ra;
  175         struct ieee80211vap *vap = ni->ni_vap;
  176         struct ieee80211_rssadapt *rsa = vap->iv_rs;
  177         const struct ieee80211_rateset *rs = &ni->ni_rates;
  178 
  179         if (ni->ni_rctls == NULL) {
  180                 ni->ni_rctls = ra = 
  181                     IEEE80211_MALLOC(sizeof(struct ieee80211_rssadapt_node),
  182                         M_80211_RATECTL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
  183                 if (ra == NULL) {
  184                         if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
  185                             "structure\n");
  186                         return;
  187                 }
  188         } else
  189                 ra = ni->ni_rctls;
  190         ra->ra_rs = rsa;
  191         ra->ra_rates = *rs;
  192         rssadapt_updatestats(ra);
  193 
  194         /* pick initial rate */
  195         for (ra->ra_rix = rs->rs_nrates - 1;
  196              ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
  197              ra->ra_rix--)
  198                 ;
  199         ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
  200         ra->ra_ticks = ticks;
  201 
  202         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  203             "RSSADAPT initial rate %d", ni->ni_txrate);
  204 }
  205 
  206 static void
  207 rssadapt_node_deinit(struct ieee80211_node *ni)
  208 {
  209 
  210         IEEE80211_FREE(ni->ni_rctls, M_80211_RATECTL);
  211 }
  212 
  213 static __inline int
  214 bucket(int pktlen)
  215 {
  216         int i, top, thridx;
  217 
  218         for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
  219              i < IEEE80211_RSSADAPT_BKTS;
  220              i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
  221                 thridx = i;
  222                 if (pktlen <= top)
  223                         break;
  224         }
  225         return thridx;
  226 }
  227 
  228 static int
  229 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
  230 {
  231         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  232         u_int pktlen = iarg;
  233         const struct ieee80211_rateset *rs = &ra->ra_rates;
  234         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  235         int rix, rssi;
  236 
  237         if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
  238                 rssadapt_updatestats(ra);
  239                 ra->ra_ticks = ticks;
  240         }
  241 
  242         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  243 
  244         /* XXX this is average rssi, should be using last value */
  245         rssi = ni->ni_ic->ic_node_getrssi(ni);
  246         for (rix = rs->rs_nrates-1; rix >= 0; rix--)
  247                 if ((*thrs)[rix] < (rssi << 8))
  248                         break;
  249         if (rix != ra->ra_rix) {
  250                 /* update public rate */
  251                 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
  252                 ra->ra_rix = rix;
  253 
  254                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  255                     "RSSADAPT new rate %d (pktlen %d rssi %d)",
  256                     ni->ni_txrate, pktlen, rssi);
  257         }
  258         return rix;
  259 }
  260 
  261 /*
  262  * Adapt the data rate to suit the conditions.  When a transmitted
  263  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
  264  * raise the RSS threshold for transmitting packets of similar length at
  265  * the same data rate.
  266  */
  267 static void
  268 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  269 {
  270         uint16_t last_thr;
  271         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  272         u_int rix;
  273 
  274         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  275 
  276         rix = ra->ra_rix;
  277         last_thr = (*thrs)[rix];
  278         (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
  279             last_thr, (rssi << 8));
  280 
  281         IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  282             "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
  283             ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  284             last_thr, (*thrs)[rix], rssi);
  285 }
  286 
  287 static void
  288 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  289 {
  290         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  291         uint16_t newthr, oldthr;
  292         int rix;
  293 
  294         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  295 
  296         rix = ra->ra_rix;
  297         if ((*thrs)[rix + 1] > (*thrs)[rix]) {
  298                 oldthr = (*thrs)[rix + 1];
  299                 if ((*thrs)[rix] == 0)
  300                         newthr = (rssi << 8);
  301                 else
  302                         newthr = (*thrs)[rix];
  303                 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
  304                     oldthr, newthr);
  305 
  306                 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  307                     "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
  308                     ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  309                     oldthr, newthr, rssi);
  310 
  311                 ra->ra_last_raise = ticks;
  312         }
  313 }
  314 
  315 static void
  316 rssadapt_tx_complete(const struct ieee80211vap *vap,
  317     const struct ieee80211_node *ni, int success, void *arg1, void *arg2)
  318 {
  319         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  320         int pktlen = *(int *)arg1, rssi = *(int *)arg2;
  321 
  322         if (success) {
  323                 ra->ra_nok++;
  324                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
  325                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
  326                         rssadapt_raise_rate(ra, pktlen, rssi);
  327         } else {
  328                 ra->ra_nfail++;
  329                 rssadapt_lower_rate(ra, pktlen, rssi);
  330         }
  331 }
  332 
  333 static int
  334 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
  335 {
  336         struct ieee80211vap *vap = arg1;
  337         struct ieee80211_rssadapt *rs = vap->iv_rs;
  338         int msecs = ticks_to_msecs(rs->interval);
  339         int error;
  340 
  341         error = sysctl_handle_int(oidp, &msecs, 0, req);
  342         if (error || !req->newptr)
  343                 return error;
  344         rssadapt_setinterval(vap, msecs);
  345         return 0;
  346 }
  347 
  348 static void
  349 rssadapt_sysctlattach(struct ieee80211vap *vap,
  350     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
  351 {
  352 
  353         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  354             "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
  355             0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
  356 }

Cache object: db12502795aa17f99ca7bbc2399da9dd


[ 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.