wlan - Sync netproto/802_11 from FreeBSD part 1/N
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_ratectl.c
1 /*-
2  * Copyright (c) 2010 Rui Paulo <rpaulo@FreeBSD.org>
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/sbuf.h>
32 #include <sys/systm.h>
33 #include <sys/socket.h>
34 #include <sys/malloc.h>
35
36 #include <net/if.h>
37 #include <net/if_media.h>
38 #include <net/ethernet.h>
39 #include <net/route.h>
40
41 #include <netproto/802_11/ieee80211_var.h>
42 #include <netproto/802_11/ieee80211_ratectl.h>
43
44 static const struct ieee80211_ratectl *ratectls[IEEE80211_RATECTL_MAX];
45
46 static const char *ratectl_modnames[IEEE80211_RATECTL_MAX] = {
47         [IEEE80211_RATECTL_AMRR]        = "wlan_amrr",
48         [IEEE80211_RATECTL_RSSADAPT]    = "wlan_rssadapt",
49         [IEEE80211_RATECTL_ONOE]        = "wlan_onoe",
50         [IEEE80211_RATECTL_SAMPLE]      = "wlan_sample",
51         [IEEE80211_RATECTL_NONE]        = "wlan_none",
52 };
53
54 MALLOC_DEFINE(M_80211_RATECTL, "80211ratectl", "802.11 rate control");
55
56 void
57 ieee80211_ratectl_register(int type, const struct ieee80211_ratectl *ratectl)
58 {
59         if (type >= IEEE80211_RATECTL_MAX)
60                 return;
61         ratectls[type] = ratectl;
62 }
63
64 void
65 ieee80211_ratectl_unregister(int type)
66 {
67         if (type >= IEEE80211_RATECTL_MAX)
68                 return;
69         ratectls[type] = NULL;
70 }
71
72 static void
73 ieee80211_ratectl_sysctl_stats_node_iter(void *arg, struct ieee80211_node *ni)
74 {
75
76         struct sbuf *sb = (struct sbuf *) arg;
77 #if defined(__DragonFly__)
78         sbuf_printf(sb, "MAC: %6s\n", ether_sprintf(ni->ni_macaddr));
79 #else
80         sbuf_printf(sb, "MAC: %6D\n", ni->ni_macaddr, ":");
81 #endif
82         ieee80211_ratectl_node_stats(ni, sb);
83         sbuf_printf(sb, "\n");
84 }
85
86 static int
87 ieee80211_ratectl_sysctl_stats(SYSCTL_HANDLER_ARGS)
88 {
89         struct ieee80211vap *vap = arg1;
90         struct ieee80211com *ic = vap->iv_ic;
91         struct sbuf sb;
92         int error;
93
94 #if defined(__DragonFly__)
95 #else
96         error = sysctl_wire_old_buffer(req, 0);
97         if (error)
98                 return (error);
99 #endif
100         sbuf_new_for_sysctl(&sb, NULL, 8, req);
101 #if defined(__DragonFly__)
102 #else
103         sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
104 #endif
105
106         IEEE80211_LOCK(ic);
107         ieee80211_iterate_nodes(&ic->ic_sta,
108             ieee80211_ratectl_sysctl_stats_node_iter,
109             &sb);
110         IEEE80211_UNLOCK(ic);
111
112         error = sbuf_finish(&sb);
113         sbuf_delete(&sb);
114         return (error);
115 }
116
117 void
118 ieee80211_ratectl_init(struct ieee80211vap *vap)
119 {
120         if (vap->iv_rate == ratectls[IEEE80211_RATECTL_NONE])
121                 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_AMRR);
122         vap->iv_rate->ir_init(vap);
123
124         /* Attach generic stats sysctl */
125 #if defined(__DragonFly__)
126         SYSCTL_ADD_PROC(vap->iv_sysctl, SYSCTL_CHILDREN(vap->iv_oid), OID_AUTO,
127             "rate_stats", CTLTYPE_STRING | CTLFLAG_RD, vap,
128             0, ieee80211_ratectl_sysctl_stats, "A", "ratectl node stats");
129 #else
130         SYSCTL_ADD_PROC(vap->iv_sysctl, SYSCTL_CHILDREN(vap->iv_oid), OID_AUTO,
131             "rate_stats", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, vap,
132             0, ieee80211_ratectl_sysctl_stats, "A", "ratectl node stats");
133 #endif
134 }
135
136 void
137 ieee80211_ratectl_set(struct ieee80211vap *vap, int type)
138 {
139         if (type >= IEEE80211_RATECTL_MAX)
140                 return;
141         if (ratectls[type] == NULL) {
142                 ieee80211_load_module(ratectl_modnames[type]);
143                 if (ratectls[type] == NULL) {
144                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_RATECTL,
145                             "%s: unable to load algo %u, module %s\n",
146                             __func__, type, ratectl_modnames[type]);
147                         vap->iv_rate = ratectls[IEEE80211_RATECTL_NONE];
148                         return;
149                 }
150         }
151         vap->iv_rate = ratectls[type];
152 }