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/8.1/sys/net80211/ieee80211_rssadapt.c 207912 2010-05-11 11:08:15Z rpaulo $     */
    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         vap->iv_rs = rs = malloc(sizeof(struct ieee80211_rssadapt),
  132             M_80211_RATECTL, M_NOWAIT|M_ZERO);
  133         if (rs == NULL) {
  134                 if_printf(vap->iv_ifp, "couldn't alloc ratectl structure\n");
  135                 return;
  136         }
  137         rs->vap = vap;
  138         rssadapt_setinterval(vap, 500 /* msecs */);
  139         rssadapt_sysctlattach(vap, vap->iv_sysctl, vap->iv_oid);
  140 }
  141 
  142 static void
  143 rssadapt_deinit(struct ieee80211vap *vap)
  144 {
  145         free(vap->iv_rs, M_80211_RATECTL);
  146 }
  147 
  148 static void
  149 rssadapt_updatestats(struct ieee80211_rssadapt_node *ra)
  150 {
  151         long interval;
  152 
  153         ra->ra_pktrate = (ra->ra_pktrate + 10*(ra->ra_nfail + ra->ra_nok))/2;
  154         ra->ra_nfail = ra->ra_nok = 0;
  155 
  156         /*
  157          * A node is eligible for its rate to be raised every 1/10 to 10
  158          * seconds, more eligible in proportion to recent packet rates.
  159          */
  160         interval = MAX(10*1000, 10*1000 / MAX(1, 10 * ra->ra_pktrate));
  161         ra->ra_raise_interval = msecs_to_ticks(interval);
  162 }
  163 
  164 static void
  165 rssadapt_node_init(struct ieee80211_node *ni)
  166 {
  167         struct ieee80211_rssadapt_node *ra;
  168         struct ieee80211vap *vap = ni->ni_vap;
  169         struct ieee80211_rssadapt *rsa = vap->iv_rs;
  170         const struct ieee80211_rateset *rs = &ni->ni_rates;
  171 
  172         ni->ni_rctls = ra = malloc(sizeof(struct ieee80211_rssadapt_node),
  173             M_80211_RATECTL, M_NOWAIT|M_ZERO);
  174         if (ra == NULL) {
  175                 if_printf(vap->iv_ifp, "couldn't alloc per-node ratectl "
  176                     "structure\n");
  177                 return;
  178         }
  179         ra->ra_rs = rsa;
  180         ra->ra_rates = *rs;
  181         rssadapt_updatestats(ra);
  182 
  183         /* pick initial rate */
  184         for (ra->ra_rix = rs->rs_nrates - 1;
  185              ra->ra_rix > 0 && (rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL) > 72;
  186              ra->ra_rix--)
  187                 ;
  188         ni->ni_txrate = rs->rs_rates[ra->ra_rix] & IEEE80211_RATE_VAL;
  189         ra->ra_ticks = ticks;
  190 
  191         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  192             "RSSADAPT initial rate %d", ni->ni_txrate);
  193 }
  194 
  195 static void
  196 rssadapt_node_deinit(struct ieee80211_node *ni)
  197 {
  198 
  199         free(ni->ni_rctls, M_80211_RATECTL);
  200 }
  201 
  202 static __inline int
  203 bucket(int pktlen)
  204 {
  205         int i, top, thridx;
  206 
  207         for (i = 0, top = IEEE80211_RSSADAPT_BKT0;
  208              i < IEEE80211_RSSADAPT_BKTS;
  209              i++, top <<= IEEE80211_RSSADAPT_BKTPOWER) {
  210                 thridx = i;
  211                 if (pktlen <= top)
  212                         break;
  213         }
  214         return thridx;
  215 }
  216 
  217 static int
  218 rssadapt_rate(struct ieee80211_node *ni, void *arg __unused, uint32_t iarg)
  219 {
  220         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  221         u_int pktlen = iarg;
  222         const struct ieee80211_rateset *rs = &ra->ra_rates;
  223         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  224         int rix, rssi;
  225 
  226         if ((ticks - ra->ra_ticks) > ra->ra_rs->interval) {
  227                 rssadapt_updatestats(ra);
  228                 ra->ra_ticks = ticks;
  229         }
  230 
  231         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  232 
  233         /* XXX this is average rssi, should be using last value */
  234         rssi = ni->ni_ic->ic_node_getrssi(ni);
  235         for (rix = rs->rs_nrates-1; rix >= 0; rix--)
  236                 if ((*thrs)[rix] < (rssi << 8))
  237                         break;
  238         if (rix != ra->ra_rix) {
  239                 /* update public rate */
  240                 ni->ni_txrate = ni->ni_rates.rs_rates[rix] & IEEE80211_RATE_VAL;
  241                 ra->ra_rix = rix;
  242 
  243                 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
  244                     "RSSADAPT new rate %d (pktlen %d rssi %d)",
  245                     ni->ni_txrate, pktlen, rssi);
  246         }
  247         return rix;
  248 }
  249 
  250 /*
  251  * Adapt the data rate to suit the conditions.  When a transmitted
  252  * packet is dropped after RAL_RSSADAPT_RETRY_LIMIT retransmissions,
  253  * raise the RSS threshold for transmitting packets of similar length at
  254  * the same data rate.
  255  */
  256 static void
  257 rssadapt_lower_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  258 {
  259         uint16_t last_thr;
  260         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  261         u_int rix;
  262 
  263         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  264 
  265         rix = ra->ra_rix;
  266         last_thr = (*thrs)[rix];
  267         (*thrs)[rix] = interpolate(master_expavgctl.rc_thresh,
  268             last_thr, (rssi << 8));
  269 
  270         IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  271             "RSSADAPT lower threshold for rate %d (last_thr %d new thr %d rssi %d)\n",
  272             ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  273             last_thr, (*thrs)[rix], rssi);
  274 }
  275 
  276 static void
  277 rssadapt_raise_rate(struct ieee80211_rssadapt_node *ra, int pktlen, int rssi)
  278 {
  279         uint16_t (*thrs)[IEEE80211_RATE_SIZE];
  280         uint16_t newthr, oldthr;
  281         int rix;
  282 
  283         thrs = &ra->ra_rate_thresh[bucket(pktlen)];
  284 
  285         rix = ra->ra_rix;
  286         if ((*thrs)[rix + 1] > (*thrs)[rix]) {
  287                 oldthr = (*thrs)[rix + 1];
  288                 if ((*thrs)[rix] == 0)
  289                         newthr = (rssi << 8);
  290                 else
  291                         newthr = (*thrs)[rix];
  292                 (*thrs)[rix + 1] = interpolate(master_expavgctl.rc_decay,
  293                     oldthr, newthr);
  294 
  295                 IEEE80211_DPRINTF(ra->ra_rs->vap, IEEE80211_MSG_RATECTL,
  296                     "RSSADAPT raise threshold for rate %d (oldthr %d newthr %d rssi %d)\n",
  297                     ra->ra_rates.rs_rates[rix + 1] & IEEE80211_RATE_VAL,
  298                     oldthr, newthr, rssi);
  299 
  300                 ra->ra_last_raise = ticks;
  301         }
  302 }
  303 
  304 static void
  305 rssadapt_tx_complete(const struct ieee80211vap *vap,
  306     const struct ieee80211_node *ni, int success, void *arg1, void *arg2)
  307 {
  308         struct ieee80211_rssadapt_node *ra = ni->ni_rctls;
  309         int pktlen = *(int *)arg1, rssi = *(int *)arg2;
  310 
  311         if (success) {
  312                 ra->ra_nok++;
  313                 if ((ra->ra_rix + 1) < ra->ra_rates.rs_nrates &&
  314                     (ticks - ra->ra_last_raise) >= ra->ra_raise_interval)
  315                         rssadapt_raise_rate(ra, pktlen, rssi);
  316         } else {
  317                 ra->ra_nfail++;
  318                 rssadapt_lower_rate(ra, pktlen, rssi);
  319         }
  320 }
  321 
  322 static int
  323 rssadapt_sysctl_interval(SYSCTL_HANDLER_ARGS)
  324 {
  325         struct ieee80211vap *vap = arg1;
  326         struct ieee80211_rssadapt *rs = vap->iv_rs;
  327         int msecs = ticks_to_msecs(rs->interval);
  328         int error;
  329 
  330         error = sysctl_handle_int(oidp, &msecs, 0, req);
  331         if (error || !req->newptr)
  332                 return error;
  333         rssadapt_setinterval(vap, msecs);
  334         return 0;
  335 }
  336 
  337 static void
  338 rssadapt_sysctlattach(struct ieee80211vap *vap,
  339     struct sysctl_ctx_list *ctx, struct sysctl_oid *tree)
  340 {
  341 
  342         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
  343             "rssadapt_rate_interval", CTLTYPE_INT | CTLFLAG_RW, vap,
  344             0, rssadapt_sysctl_interval, "I", "rssadapt operation interval (ms)");
  345 }

Cache object: 224587a69f316de44a3d45c30b872f11


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