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/dev/ic/athrate-onoe.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 /*      $NetBSD: athrate-onoe.c,v 1.12.20.1 2009/08/07 06:43:27 snj Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer,
   12  *    without modification.
   13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
   14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
   15  *    redistribution must be conditioned upon including a substantially
   16  *    similar Disclaimer requirement for further binary redistribution.
   17  * 3. Neither the names of the above-listed copyright holders nor the names
   18  *    of any contributors may be used to endorse or promote products derived
   19  *    from this software without specific prior written permission.
   20  *
   21  * Alternatively, this software may be distributed under the terms of the
   22  * GNU General Public License ("GPL") version 2 as published by the Free
   23  * Software Foundation.
   24  *
   25  * NO WARRANTY
   26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
   29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
   30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
   31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
   34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   36  * THE POSSIBILITY OF SUCH DAMAGES.
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 #ifdef __FreeBSD__
   41 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
   42 #endif
   43 #ifdef __NetBSD__
   44 __KERNEL_RCSID(0, "$NetBSD: athrate-onoe.c,v 1.12.20.1 2009/08/07 06:43:27 snj Exp $");
   45 #endif
   46 
   47 /*
   48  * Atsushi Onoe's rate control algorithm.
   49  */
   50 #include "opt_inet.h"
   51 
   52 #include <sys/param.h>
   53 #include <sys/systm.h> 
   54 #include <sys/sysctl.h>
   55 #include <sys/kernel.h>
   56 #include <sys/errno.h>
   57 #include <sys/device.h>
   58 #include <sys/bus.h>
   59 #include <sys/socket.h>
   60  
   61 #include <net/if.h>
   62 #include <net/if_media.h>
   63 #include <net/if_arp.h>
   64 #include <net/if_ether.h>               /* XXX for ether_sprintf */
   65 
   66 #include <net80211/ieee80211_var.h>
   67 
   68 #include <net/bpf.h>
   69 
   70 #ifdef INET
   71 #include <netinet/in.h> 
   72 #endif
   73 
   74 #include <dev/ic/ath_netbsd.h>
   75 #include <dev/ic/athvar.h>
   76 #include <dev/ic/athrate-onoe.h>
   77 
   78 #include <external/isc/atheros_hal/dist/ah.h>
   79 
   80 #define ONOE_DEBUG
   81 #ifdef ONOE_DEBUG
   82 enum {
   83         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
   84 };
   85 #define DPRINTF(sc, _fmt, ...) do {                             \
   86         if (sc->sc_debug & ATH_DEBUG_RATE)                      \
   87                 printf(_fmt, __VA_ARGS__);                      \
   88 } while (0)
   89 #else
   90 #define DPRINTF(sc, _fmt, ...)
   91 #endif
   92 
   93 /*
   94  * Default parameters for the rate control algorithm.  These are
   95  * all tunable with sysctls.  The rate controller runs periodically
   96  * (each ath_rateinterval ms) analyzing transmit statistics for each
   97  * neighbor/station (when operating in station mode this is only the AP).
   98  * If transmits look to be working well over a sampling period then
   99  * it gives a "raise rate credit".  If transmits look to not be working
  100  * well than it deducts a credit.  If the credits cross a threshold then
  101  * the transmit rate is raised.  Various error conditions force the
  102  * the transmit rate to be dropped.
  103  *
  104  * The decision to issue/deduct a credit is based on the errors and
  105  * retries accumulated over the sampling period.  ath_rate_raise defines
  106  * the percent of retransmits for which a credit is issued/deducted.
  107  * ath_rate_raise_threshold defines the threshold on credits at which
  108  * the transmit rate is increased.
  109  *
  110  * XXX this algorithm is flawed.
  111  */
  112 static  int ath_rateinterval = 1000;            /* rate ctl interval (ms)  */
  113 static  int ath_rate_raise = 10;                /* add credit threshold */
  114 static  int ath_rate_raise_threshold = 10;      /* rate ctl raise threshold */
  115 
  116 static void     ath_ratectl(void *);
  117 static void     ath_rate_update(struct ath_softc *, struct ieee80211_node *,
  118                         int rate);
  119 static void     ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
  120 static void     ath_rate_ctl(void *, struct ieee80211_node *);
  121 
  122 void
  123 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
  124 {
  125         /* NB: assumed to be zero'd by caller */
  126         ath_rate_update(sc, &an->an_node, 0);
  127 }
  128 
  129 void
  130 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
  131 {
  132 }
  133 
  134 void
  135 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
  136         int shortPreamble, size_t frameLen,
  137         u_int8_t *rix, int *try0, u_int8_t *txrate)
  138 {
  139         struct onoe_node *on = ATH_NODE_ONOE(an);
  140 
  141         *rix = on->on_tx_rix0;
  142         *try0 = on->on_tx_try0;
  143         if (shortPreamble)
  144                 *txrate = on->on_tx_rate0sp;
  145         else
  146                 *txrate = on->on_tx_rate0;
  147 }
  148 
  149 void
  150 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
  151         struct ath_desc *ds, int shortPreamble, u_int8_t rix)
  152 {
  153         struct onoe_node *on = ATH_NODE_ONOE(an);
  154 
  155         ath_hal_setupxtxdesc(sc->sc_ah, ds
  156                 , on->on_tx_rate1sp, 2  /* series 1 */
  157                 , on->on_tx_rate2sp, 2  /* series 2 */
  158                 , on->on_tx_rate3sp, 2  /* series 3 */
  159         );
  160 }
  161 
  162 void
  163 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
  164         const struct ath_desc *ds, const struct ath_desc *ds0)
  165 {
  166         struct onoe_node *on = ATH_NODE_ONOE(an);
  167 
  168         if (ds->ds_txstat.ts_status == 0)
  169                 on->on_tx_ok++;
  170         else
  171                 on->on_tx_err++;
  172         on->on_tx_retr += ds->ds_txstat.ts_shortretry
  173                         + ds->ds_txstat.ts_longretry;
  174 }
  175 
  176 void
  177 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
  178 {
  179         if (isnew)
  180                 ath_rate_ctl_start(sc, &an->an_node);
  181 }
  182 
  183 static void
  184 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
  185 {
  186         struct ath_node *an = ATH_NODE(ni);
  187         struct onoe_node *on = ATH_NODE_ONOE(an);
  188         const HAL_RATE_TABLE *rt = sc->sc_currates;
  189         u_int8_t rix;
  190 
  191         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
  192 
  193         DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
  194             __func__, ether_sprintf(ni->ni_macaddr),
  195             ni->ni_rates.rs_nrates > 0 ?
  196                 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
  197 
  198         ni->ni_txrate = rate;
  199         /*
  200          * Before associating a node has no rate set setup
  201          * so we can't calculate any transmit codes to use.
  202          * This is ok since we should never be sending anything
  203          * but management frames and those always go at the
  204          * lowest hardware rate.
  205          */
  206         if (ni->ni_rates.rs_nrates == 0)
  207                 goto done;
  208         on->on_tx_rix0 = sc->sc_rixmap[
  209                 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
  210         on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
  211         
  212         on->on_tx_rate0sp = on->on_tx_rate0 |
  213                 rt->info[on->on_tx_rix0].shortPreamble;
  214         if (sc->sc_mrretry) {
  215                 /*
  216                  * Hardware supports multi-rate retry; setup two
  217                  * step-down retry rates and make the lowest rate
  218                  * be the ``last chance''.  We use 4, 2, 2, 2 tries
  219                  * respectively (4 is set here, the rest are fixed
  220                  * in the xmit routine).
  221                  */
  222                 on->on_tx_try0 = 1 + 3;         /* 4 tries at rate 0 */
  223                 if (--rate >= 0) {
  224                         rix = sc->sc_rixmap[
  225                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
  226                         on->on_tx_rate1 = rt->info[rix].rateCode;
  227                         on->on_tx_rate1sp = on->on_tx_rate1 |
  228                                 rt->info[rix].shortPreamble;
  229                 } else {
  230                         on->on_tx_rate1 = on->on_tx_rate1sp = 0;
  231                 }
  232                 if (--rate >= 0) {
  233                         rix = sc->sc_rixmap[
  234                                 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
  235                         on->on_tx_rate2 = rt->info[rix].rateCode;
  236                         on->on_tx_rate2sp = on->on_tx_rate2 |
  237                                 rt->info[rix].shortPreamble;
  238                 } else {
  239                         on->on_tx_rate2 = on->on_tx_rate2sp = 0;
  240                 }
  241                 if (rate > 0) {
  242                         /* NB: only do this if we didn't already do it above */
  243                         on->on_tx_rate3 = rt->info[0].rateCode;
  244                         on->on_tx_rate3sp =
  245                                 on->on_tx_rate3 | rt->info[0].shortPreamble;
  246                 } else {
  247                         on->on_tx_rate3 = on->on_tx_rate3sp = 0;
  248                 }
  249         } else {
  250                 on->on_tx_try0 = ATH_TXMAXTRY;  /* max tries at rate 0 */
  251                 on->on_tx_rate1 = on->on_tx_rate1sp = 0;
  252                 on->on_tx_rate2 = on->on_tx_rate2sp = 0;
  253                 on->on_tx_rate3 = on->on_tx_rate3sp = 0;
  254         }
  255 done:
  256         on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
  257 }
  258 
  259 /*
  260  * Set the starting transmit rate for a node.
  261  */
  262 static void
  263 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
  264 {
  265 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
  266         struct ieee80211com *ic = &sc->sc_ic;
  267         int srate;
  268 
  269         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
  270         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
  271                 /*
  272                  * No fixed rate is requested. For 11b start with
  273                  * the highest negotiated rate; otherwise, for 11g
  274                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
  275                  */
  276                 srate = ni->ni_rates.rs_nrates - 1;
  277                 if (sc->sc_curmode != IEEE80211_MODE_11B) {
  278                         /*
  279                          * Scan the negotiated rate set to find the
  280                          * closest rate.
  281                          */
  282                         /* NB: the rate set is assumed sorted */
  283                         for (; srate >= 0 && RATE(srate) > 72; srate--)
  284                                 ;
  285                         KASSERT(srate >= 0, ("bogus rate set"));
  286                 }
  287         } else {
  288                 /*
  289                  * A fixed rate is to be used; ic_fixed_rate is an
  290                  * index into the supported rate set.  Convert this
  291                  * to the index into the negotiated rate set for
  292                  * the node.  We know the rate is there because the
  293                  * rate set is checked when the station associates.
  294                  */
  295                 const struct ieee80211_rateset *rs =
  296                         &ic->ic_sup_rates[ic->ic_curmode];
  297                 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
  298                 /* NB: the rate set is assumed sorted */
  299                 srate = ni->ni_rates.rs_nrates - 1;
  300                 for (; srate >= 0 && RATE(srate) != r; srate--)
  301                         ;
  302                 KASSERT(srate >= 0,
  303                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
  304         }
  305         ath_rate_update(sc, ni, srate);
  306 #undef RATE
  307 }
  308 
  309 static void
  310 ath_rate_cb(void *arg, struct ieee80211_node *ni)
  311 {
  312         struct ath_softc *sc = arg;
  313 
  314         ath_rate_update(sc, ni, 0);
  315 }
  316 
  317 /*
  318  * Reset the rate control state for each 802.11 state transition.
  319  */
  320 void
  321 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
  322 {
  323         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
  324         struct ieee80211com *ic = &sc->sc_ic;
  325         struct ieee80211_node *ni;
  326 
  327         if (state == IEEE80211_S_INIT) {
  328                 callout_stop(&osc->timer);
  329                 return;
  330         }
  331         if (ic->ic_opmode == IEEE80211_M_STA) {
  332                 /*
  333                  * Reset local xmit state; this is really only
  334                  * meaningful when operating in station mode.
  335                  */
  336                 ni = ic->ic_bss;
  337                 if (state == IEEE80211_S_RUN) {
  338                         ath_rate_ctl_start(sc, ni);
  339                 } else {
  340                         ath_rate_update(sc, ni, 0);
  341                 }
  342         } else {
  343                 /*
  344                  * When operating as a station the node table holds
  345                  * the AP's that were discovered during scanning.
  346                  * For any other operating mode we want to reset the
  347                  * tx rate state of each node.
  348                  */
  349                 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
  350                 ath_rate_update(sc, ic->ic_bss, 0);
  351         }
  352         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
  353             state == IEEE80211_S_RUN) {
  354                 int interval;
  355                 /*
  356                  * Start the background rate control thread if we
  357                  * are not configured to use a fixed xmit rate.
  358                  */
  359                 interval = ath_rateinterval;
  360                 if (ic->ic_opmode == IEEE80211_M_STA)
  361                         interval /= 2;
  362                 callout_reset(&osc->timer, (interval * hz) / 1000,
  363                         ath_ratectl, &sc->sc_if);
  364         }
  365 }
  366 
  367 /* 
  368  * Examine and potentially adjust the transmit rate.
  369  */
  370 static void
  371 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
  372 {
  373         struct ath_softc *sc = arg;
  374         struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
  375         struct ieee80211_rateset *rs = &ni->ni_rates;
  376         int dir = 0, nrate, enough;
  377 
  378         /*
  379          * Rate control
  380          * XXX: very primitive version.
  381          */
  382         enough = (on->on_tx_ok + on->on_tx_err >= 10);
  383 
  384         /* no packet reached -> down */
  385         if (on->on_tx_err > 0 && on->on_tx_ok == 0)
  386                 dir = -1;
  387 
  388         /* all packets needs retry in average -> down */
  389         if (enough && on->on_tx_ok < on->on_tx_retr)
  390                 dir = -1;
  391 
  392         /* no error and less than rate_raise% of packets need retry -> up */
  393         if (enough && on->on_tx_err == 0 &&
  394             on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
  395                 dir = 1;
  396 
  397         DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
  398                 ether_sprintf(ni->ni_macaddr),
  399                 on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
  400                 on->on_tx_upper, dir);
  401 
  402         nrate = ni->ni_txrate;
  403         switch (dir) {
  404         case 0:
  405                 if (enough && on->on_tx_upper > 0)
  406                         on->on_tx_upper--;
  407                 break;
  408         case -1:
  409                 if (nrate > 0) {
  410                         nrate--;
  411                         sc->sc_stats.ast_rate_drop++;
  412                 }
  413                 on->on_tx_upper = 0;
  414                 break;
  415         case 1:
  416                 /* raise rate if we hit rate_raise_threshold */
  417                 if (++on->on_tx_upper < ath_rate_raise_threshold)
  418                         break;
  419                 on->on_tx_upper = 0;
  420                 if (nrate + 1 < rs->rs_nrates) {
  421                         nrate++;
  422                         sc->sc_stats.ast_rate_raise++;
  423                 }
  424                 break;
  425         }
  426 
  427         if (nrate != ni->ni_txrate) {
  428                 DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
  429                     __func__,
  430                     (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
  431                     (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
  432                     on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
  433                 ath_rate_update(sc, ni, nrate);
  434         } else if (enough)
  435                 on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
  436 }
  437 
  438 static void
  439 ath_ratectl(void *arg)
  440 {
  441         struct ifnet *ifp = arg;
  442         struct ath_softc *sc = ifp->if_softc;
  443         struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
  444         struct ieee80211com *ic = &sc->sc_ic;
  445         int interval;
  446 
  447         if (ifp->if_flags & IFF_RUNNING) {
  448                 sc->sc_stats.ast_rate_calls++;
  449 
  450                 if (ic->ic_opmode == IEEE80211_M_STA)
  451                         ath_rate_ctl(sc, ic->ic_bss);   /* NB: no reference */
  452                 else
  453                         ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
  454         }
  455         interval = ath_rateinterval;
  456         if (ic->ic_opmode == IEEE80211_M_STA)
  457                 interval /= 2;
  458         callout_reset(&osc->timer, (interval * hz) / 1000,
  459                 ath_ratectl, &sc->sc_if);
  460 }
  461 
  462 static void
  463 ath_rate_sysctlattach(struct ath_softc *sc)
  464 {
  465         struct sysctllog **clog = &sc->sc_sysctllog;
  466         const struct sysctlnode *cnode, *rnode;
  467 
  468         if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
  469                 return;
  470 
  471         SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_interval",
  472             "rate control: operation interval (ms)", rateinterval);
  473         /* XXX bounds check values */
  474         SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise",
  475             "rate control: retry threshold to credit rate raise (%%)",
  476             rate_raise);
  477         SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise_threshold",
  478             "rate control: # good periods before raising rate",
  479             rate_raise_threshold);
  480 }
  481 
  482 struct ath_ratectrl *
  483 ath_rate_attach(struct ath_softc *sc)
  484 {
  485         struct onoe_softc *osc;
  486 
  487         osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
  488         if (osc == NULL)
  489                 return NULL;
  490         osc->arc.arc_space = sizeof(struct onoe_node);
  491         callout_init(&osc->timer, 0);
  492         ath_rate_sysctlattach(sc);
  493 
  494         return &osc->arc;
  495 }
  496 
  497 void
  498 ath_rate_detach(struct ath_ratectrl *arc)
  499 {
  500         struct onoe_softc *osc = (struct onoe_softc *) arc;
  501 
  502         callout_stop(&osc->timer);
  503         free(osc, M_DEVBUF);
  504 }

Cache object: 1f7b62bcd2b6d8a96158e33dddef8a37


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