Split ifnet serialize step 1/many: Add if_{serialize,deserialize,tryserialize}()
[dragonfly.git] / sys / netproto / 802_11 / wlan_ratectl / onoe / ieee80211_ratectl_onoe.c
1 /*
2  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.8.2.3 2006/02/24 19:51:11 sam Exp $
37  * $DragonFly: src/sys/netproto/802_11/wlan_ratectl/onoe/ieee80211_ratectl_onoe.c,v 1.10 2008/01/15 09:01:13 sephe Exp $
38  */
39
40 /*
41  * Atsushi Onoe's rate control algorithm.
42  */
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <sys/serialize.h>
50  
51 #include <net/if.h>
52 #include <net/if_media.h>
53 #include <net/if_arp.h>
54
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
57
58 #define ONOE_DEBUG
59
60 #ifdef ONOE_DEBUG
61 #define DPRINTF(osc, lv, fmt, ...) do {         \
62         if ((osc)->param->onoe_debug >= lv)     \
63                 kprintf(fmt, __VA_ARGS__);      \
64 } while (0)
65 #else
66 #define DPRINTF(osc, lv, fmt, ...)
67 #endif
68
69 struct onoe_softc {
70         struct ieee80211com     *ic;
71         struct callout          timer;          /* periodic timer */
72
73         struct sysctl_ctx_list  sysctl_ctx;
74         struct sysctl_oid       *sysctl_oid;
75
76         struct ieee80211_onoe_param *param;
77 #define raise           param->onoe_raise
78 #define raise_threshold param->onoe_raise_threshold
79 };
80
81 struct onoe_data {
82         u_int           od_tx_ok;       /* tx ok pkt */
83         u_int           od_tx_err;      /* tx !ok pkt */
84         u_int           od_tx_retr;     /* tx retry count */
85         int             od_tx_upper;    /* tx upper rate req cnt */
86 };
87
88 /*
89  * Default parameters for the rate control algorithm.  These are
90  * all tunable with sysctls.  The rate controller runs periodically
91  * (each ath_rateinterval ms) analyzing transmit statistics for each
92  * neighbor/station (when operating in station mode this is only the AP).
93  * If transmits look to be working well over a sampling period then
94  * it gives a "raise rate credit".  If transmits look to not be working
95  * well than it deducts a credit.  If the credits cross a threshold then
96  * the transmit rate is raised.  Various error conditions force the
97  * the transmit rate to be dropped.
98  *
99  * The decision to issue/deduct a credit is based on the errors and
100  * retries accumulated over the sampling period.  ath_rate_raise defines
101  * the percent of retransmits for which a credit is issued/deducted.
102  * ath_rate_raise_threshold defines the threshold on credits at which
103  * the transmit rate is increased.
104  *
105  * XXX this algorithm is flawed.
106  */
107
108 static void     *onoe_attach(struct ieee80211com *);
109 static void     onoe_detach(void *);
110 static void     onoe_data_free(struct ieee80211_node *);
111 static void     onoe_data_alloc(struct ieee80211_node *);
112 static void     onoe_data_dup(const struct ieee80211_node *,
113                               struct ieee80211_node *);
114 static void     onoe_newstate(void *, enum ieee80211_state);
115 static void     onoe_tx_complete(void *, struct ieee80211_node *, int,
116                                  const struct ieee80211_ratectl_res[],
117                                  int, int, int, int);
118 static void     onoe_newassoc(void *, struct ieee80211_node *, int);
119 static int      onoe_findrate(void *, struct ieee80211_node *, int,
120                               int[], int);
121
122 static void     onoe_sysctl_attach(struct onoe_softc *);
123 static void     onoe_update(struct onoe_softc *, struct ieee80211_node *, int);
124 static void     onoe_start(struct onoe_softc *, struct ieee80211_node *);
125 static void     onoe_tick(void *);
126 static void     onoe_ratectl(void *, struct ieee80211_node *);
127 static void     onoe_gather_stats(struct onoe_softc *, struct ieee80211_node *);
128
129 static const struct ieee80211_ratectl onoe = {
130         .rc_name        = "onoe",
131         .rc_ratectl     = IEEE80211_RATECTL_ONOE,
132         .rc_attach      = onoe_attach,
133         .rc_detach      = onoe_detach,
134         .rc_data_alloc  = onoe_data_alloc,
135         .rc_data_free   = onoe_data_free,
136         .rc_data_dup    = onoe_data_dup,
137         .rc_newstate    = onoe_newstate,
138         .rc_tx_complete = onoe_tx_complete,
139         .rc_newassoc    = onoe_newassoc,
140         .rc_findrate    = onoe_findrate
141 };
142
143 static u_int    onoe_nrefs;
144
145 MALLOC_DEFINE(M_ONOE_RATECTL_DATA, "onoe_ratectl_data",
146               "onoe rate control data");
147
148 static void
149 onoe_tx_complete(void *arg __unused, struct ieee80211_node *ni,
150                  int frame_len __unused,
151                  const struct ieee80211_ratectl_res res[] __unused,
152                  int res_len __unused,
153                  int data_retries, int rts_retries __unused, int is_fail)
154 {
155         struct onoe_data *od = ni->ni_rate_data;
156
157         if (od == NULL)
158                 return;
159
160         if (is_fail)
161                 od->od_tx_err++;
162         else
163                 od->od_tx_ok++;
164
165         od->od_tx_retr += data_retries;
166 }
167
168 static void
169 onoe_newassoc(void *arg, struct ieee80211_node *ni, int is_new)
170 {
171         if (is_new)
172                 onoe_start(arg, ni);
173 }
174
175 static int
176 onoe_findrate(void *arg, struct ieee80211_node *ni,
177               int frame_len __unused, int rateidx[], int rateidx_len)
178 {
179         struct onoe_softc *osc = arg;
180         int i, rate_idx;
181
182         if (ni->ni_txrate >= ni->ni_rates.rs_nrates) {
183                 DPRINTF(osc, 5, "%s: number of rates changed, restart\n",
184                         __func__);
185                 onoe_start(osc, ni);
186         }
187         rate_idx = ni->ni_txrate;
188
189         for (i = 0; i < rateidx_len; ++i) {
190                 if (rate_idx < 0)
191                         break;
192                 rateidx[i] = rate_idx--;
193         }
194         if (rateidx_len > 1)
195                 rateidx[rateidx_len - 1] = 0;
196         return i;
197 }
198
199 static void
200 onoe_update(struct onoe_softc *osc, struct ieee80211_node *ni, int nrate)
201 {
202         struct onoe_data *od = ni->ni_rate_data;
203
204         DPRINTF(osc, 1, "%s: set xmit rate for %6D to %dM\n", __func__,
205                 ni->ni_macaddr, ":",
206                 ni->ni_rates.rs_nrates > 0 ?
207                 IEEE80211_RS_RATE(&ni->ni_rates, nrate) / 2 : 0);
208
209         ni->ni_txrate = nrate;
210
211         if (od == NULL) {
212                 onoe_data_alloc(ni);
213         } else {
214                 od->od_tx_ok = 0;
215                 od->od_tx_err = 0;
216                 od->od_tx_retr = 0;
217                 od->od_tx_upper = 0;
218         }
219 }
220
221 /*
222  * Set the starting transmit rate for a node.
223  */
224 static void
225 onoe_start(struct onoe_softc *osc, struct ieee80211_node *ni)
226 {
227 #define RATE(_ix)       IEEE80211_RS_RATE(&ni->ni_rates, (_ix))
228         struct ieee80211com *ic = osc->ic;
229         int srate;
230
231         KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
232         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
233                 /*
234                  * For adhoc or ibss mode, start from the lowest rate.
235                  */
236                 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
237                     ic->ic_opmode == IEEE80211_M_IBSS) {
238                         onoe_update(osc, ni, 0);
239                         return;
240                 }
241
242                 /*
243                  * No fixed rate is requested. For 11b start with
244                  * the highest negotiated rate; otherwise, for 11g
245                  * and 11a, we start "in the middle" at 24Mb or 36Mb.
246                  */
247                 srate = ni->ni_rates.rs_nrates - 1;
248                 if (ic->ic_curmode != IEEE80211_MODE_11B) {
249                         /*
250                          * Scan the negotiated rate set to find the
251                          * closest rate.
252                          */
253                         /* NB: the rate set is assumed sorted */
254                         for (; srate >= 0 && RATE(srate) > 72; srate--)
255                                 ;
256                         KASSERT(srate >= 0, ("bogus rate set"));
257                 }
258         } else {
259                 /*
260                  * A fixed rate is to be used; ic_fixed_rate is an
261                  * index into the supported rate set.  Convert this
262                  * to the index into the negotiated rate set for
263                  * the node.  We know the rate is there because the
264                  * rate set is checked when the station associates.
265                  */
266                 const struct ieee80211_rateset *rs =
267                         &ic->ic_sup_rates[ic->ic_curmode];
268                 int r = IEEE80211_RS_RATE(rs, ic->ic_fixed_rate);
269
270                 /* NB: the rate set is assumed sorted */
271                 srate = ni->ni_rates.rs_nrates - 1;
272                 for (; srate >= 0 && RATE(srate) != r; srate--)
273                         ;
274                 KASSERT(srate >= 0,
275                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
276         }
277         onoe_update(osc, ni, srate);
278 #undef RATE
279 }
280
281 static void
282 onoe_rate_cb(void *arg, struct ieee80211_node *ni)
283 {
284         onoe_update(arg, ni, 0);
285 }
286
287 static void
288 onoe_newstate(void *arg, enum ieee80211_state state)
289 {
290         struct onoe_softc *osc = arg;
291         struct ieee80211com *ic = osc->ic;
292         struct ieee80211_node *ni;
293
294         if (state == IEEE80211_S_INIT) {
295                 callout_stop(&osc->timer);
296                 return;
297         }
298
299         if (ic->ic_opmode == IEEE80211_M_STA) {
300                 /*
301                  * Reset local xmit state; this is really only
302                  * meaningful when operating in station mode.
303                  */
304                 ni = ic->ic_bss;
305                 if (state == IEEE80211_S_RUN)
306                         onoe_start(osc, ni);
307                 else
308                         onoe_update(osc, ni, 0);
309         } else {
310                 /*
311                  * When operating as a station the node table holds
312                  * the AP's that were discovered during scanning.
313                  * For any other operating mode we want to reset the
314                  * tx rate state of each node.
315                  */
316                 ieee80211_iterate_nodes(&ic->ic_sta, onoe_rate_cb, osc);
317                 onoe_update(osc, ic->ic_bss, 0);
318         }
319
320         if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
321             state == IEEE80211_S_RUN) {
322                 int interval;
323
324                 /*
325                  * Start the background rate control thread if we
326                  * are not configured to use a fixed xmit rate.
327                  */
328                 interval = osc->param->onoe_interval;
329                 if (ic->ic_opmode == IEEE80211_M_STA)
330                         interval /= 2;
331                 callout_reset(&osc->timer, (interval * hz) / 1000,
332                               onoe_tick, osc);
333         }
334 }
335
336 static void
337 onoe_gather_stats(struct onoe_softc *osc, struct ieee80211_node *ni)
338 {
339         struct onoe_data *od = ni->ni_rate_data;
340         struct ieee80211com *ic = osc->ic;
341         const struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
342         struct ieee80211_ratectl_stats stats;
343
344         st->rc_st_stats(ic, ni, &stats);
345
346         od->od_tx_ok += stats.stats_pkt_ok;
347         od->od_tx_err += stats.stats_pkt_err;
348         od->od_tx_retr += stats.stats_retries;
349 }
350
351 static void
352 onoe_ratectl(void *arg, struct ieee80211_node *ni)
353 {
354         struct onoe_softc *osc = arg;
355         struct onoe_data *od = ni->ni_rate_data;
356         const struct ieee80211_ratectl_state *st = &osc->ic->ic_ratectl;
357         struct ieee80211_rateset *rs = &ni->ni_rates;
358         int dir = 0, nrate, enough;
359
360         if (od == NULL) {
361                 /* We are no ready to go, set TX rate to lowest one */
362                 ni->ni_txrate = 0;
363                 return;
364         }
365
366         if (st->rc_st_stats != NULL)
367                 onoe_gather_stats(osc, ni);
368
369         /*
370          * Rate control
371          * XXX: very primitive version.
372          */
373         enough = (od->od_tx_ok + od->od_tx_err >= 10);
374
375         /* no packet reached -> down */
376         if (od->od_tx_err > 0 && od->od_tx_ok == 0)
377                 dir = -1;
378
379         /* all packets needs retry in average -> down */
380         if (enough && od->od_tx_ok < od->od_tx_retr)
381                 dir = -1;
382
383         /* no error and less than rate_raise% of packets need retry -> up */
384         if (enough && od->od_tx_err == 0 &&
385             od->od_tx_retr < (od->od_tx_ok * osc->raise) / 100)
386                 dir = 1;
387
388         DPRINTF(osc, 10, "%6D: ok %d err %d retr %d upper %d dir %d\n",
389                 ni->ni_macaddr, ":",
390                 od->od_tx_ok, od->od_tx_err, od->od_tx_retr,
391                 od->od_tx_upper, dir);
392
393         nrate = ni->ni_txrate;
394         switch (dir) {
395         case 0:
396                 if (enough && od->od_tx_upper > 0)
397                         od->od_tx_upper--;
398                 break;
399         case -1:
400                 if (nrate > 0)
401                         nrate--;
402                 od->od_tx_upper = 0;
403                 break;
404         case 1:
405                 /* raise rate if we hit rate_raise_threshold */
406                 if (++od->od_tx_upper < osc->raise_threshold)
407                         break;
408                 od->od_tx_upper = 0;
409                 if (nrate + 1 < rs->rs_nrates)
410                         nrate++;
411                 break;
412         }
413
414         if (nrate != ni->ni_txrate) {
415                 DPRINTF(osc, 5, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
416                         __func__,
417                         IEEE80211_RS_RATE(rs, ni->ni_txrate) / 2,
418                         IEEE80211_RS_RATE(rs, nrate) / 2,
419                         od->od_tx_ok, od->od_tx_err, od->od_tx_retr);
420                 onoe_update(osc, ni, nrate);
421         } else if (enough) {
422                 od->od_tx_ok = od->od_tx_err = od->od_tx_retr = 0;
423         }
424 }
425
426 static void
427 onoe_tick(void *arg)
428 {
429         struct onoe_softc *osc = arg;
430         struct ieee80211com *ic = osc->ic;
431         struct ifnet *ifp = &ic->ic_if;
432         int interval;
433
434         ifnet_serialize_all(ifp);
435
436         if (ifp->if_flags & IFF_RUNNING) {
437                 if (ic->ic_opmode == IEEE80211_M_STA)
438                         onoe_ratectl(osc, ic->ic_bss);  /* NB: no reference */
439                 else
440                         ieee80211_iterate_nodes(&ic->ic_sta, onoe_ratectl, osc);
441         }
442
443         interval = osc->param->onoe_interval;
444         if (ic->ic_opmode == IEEE80211_M_STA)
445                 interval /= 2;
446         callout_reset(&osc->timer, (interval * hz) / 1000, onoe_tick, osc);
447
448         ifnet_deserialize_all(ifp);
449 }
450
451 static void
452 onoe_sysctl_attach(struct onoe_softc *osc)
453 {
454         sysctl_ctx_init(&osc->sysctl_ctx);
455         osc->sysctl_oid = SYSCTL_ADD_NODE(&osc->sysctl_ctx,
456                 SYSCTL_CHILDREN(osc->ic->ic_sysctl_oid),
457                 OID_AUTO, "onoe_ratectl", CTLFLAG_RD, 0, "");
458         if (osc->sysctl_oid == NULL) {
459                 kprintf("wlan_ratectl_onoe: create sysctl tree failed\n");
460                 return;
461         }
462
463         SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
464                        OID_AUTO, "interval", CTLFLAG_RW,
465                        &osc->param->onoe_interval, 0,
466                        "rate control: operation interval (ms)");
467
468         /* XXX bounds check values */
469         SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
470                        OID_AUTO, "raise", CTLFLAG_RW,
471                        &osc->param->onoe_raise, 0,
472                        "rate control: "
473                        "retry threshold to credit rate raise (%%)");
474
475         SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
476                        OID_AUTO, "raise_threshold", CTLFLAG_RW,
477                        &osc->param->onoe_raise_threshold, 0,
478                        "rate control: # good periods before raising rate");
479
480         SYSCTL_ADD_INT(&osc->sysctl_ctx, SYSCTL_CHILDREN(osc->sysctl_oid),
481                        OID_AUTO, "debug", CTLFLAG_RW,
482                        &osc->param->onoe_debug, 0,
483                        "rate control: debug level");
484 }
485
486 static void *
487 onoe_attach(struct ieee80211com *ic)
488 {
489         struct onoe_softc *osc;
490
491         onoe_nrefs++;
492
493         osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK | M_ZERO);
494
495         osc->ic = ic;
496         callout_init(&osc->timer);
497         osc->param = ic->ic_ratectl.rc_st_attach(ic, IEEE80211_RATECTL_ONOE);
498
499         onoe_sysctl_attach(osc);
500
501         onoe_newstate(osc, ic->ic_state);
502
503         return osc;
504 }
505
506 static void
507 _onoe_data_free(void *arg __unused, struct ieee80211_node *ni)
508 {
509         onoe_data_free(ni);
510 }
511
512 static void
513 onoe_detach(void *arg)
514 {
515         struct onoe_softc *osc = arg;
516         struct ieee80211com *ic = osc->ic;
517
518         onoe_newstate(osc, IEEE80211_S_INIT);
519
520         ieee80211_iterate_nodes(&ic->ic_sta, _onoe_data_free, NULL);
521         ieee80211_iterate_nodes(&ic->ic_scan, _onoe_data_free, NULL);
522
523         if (osc->sysctl_oid != NULL)
524                 sysctl_ctx_free(&osc->sysctl_ctx);
525         kfree(osc, M_DEVBUF);
526
527         onoe_nrefs--;
528 }
529
530 static void
531 onoe_data_free(struct ieee80211_node *ni)
532 {
533         if (ni->ni_rate_data != NULL) {
534                 kfree(ni->ni_rate_data, M_ONOE_RATECTL_DATA);
535                 ni->ni_rate_data = NULL;
536         }
537 }
538
539 static void
540 onoe_data_alloc(struct ieee80211_node *ni)
541 {
542         KKASSERT(ni->ni_rate_data == NULL);
543         ni->ni_rate_data = kmalloc(sizeof(struct onoe_data),
544                                   M_ONOE_RATECTL_DATA, M_NOWAIT | M_ZERO);
545 }
546
547 static void
548 onoe_data_dup(const struct ieee80211_node *oni, struct ieee80211_node *nni)
549 {
550         if (oni->ni_rate_data == NULL || nni->ni_rate_data == NULL)
551                 return;
552
553         bcopy(oni->ni_rate_data, nni->ni_rate_data, sizeof(struct onoe_data));
554 }
555
556 static int
557 onoe_modevent(module_t mod, int type, void *unused)
558 {
559         switch (type) {
560         case MOD_LOAD:
561                 ieee80211_ratectl_register(&onoe);
562                 return 0;
563         case MOD_UNLOAD:
564                 if (onoe_nrefs) {
565                         kprintf("wlan_ratectl_onoe: still in use "
566                                "(%u dynamic refs)\n", onoe_nrefs);
567                         return EBUSY;
568                 }
569                 ieee80211_ratectl_unregister(&onoe);
570                 return 0;
571         }
572         return EINVAL;
573 }
574
575 static moduledata_t onoe_mod = {
576         "wlan_ratectl_onoe",
577         onoe_modevent,
578         0
579 };
580 DECLARE_MODULE(wlan_ratectl_onoe, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
581 MODULE_VERSION(wlan_ratectl_onoe, 1);
582 MODULE_DEPEND(wlan_ratectl_onoe, wlan, 1, 1, 1);