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