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_var.h>
38 #include <net/if_media.h>
39 #include <net/ethernet.h>
40 #include <net/route.h>
41 
42 #include <netproto/802_11/ieee80211_var.h>
43 #include <netproto/802_11/ieee80211_ratectl.h>
44 
45 static const struct ieee80211_ratectl *ratectls[IEEE80211_RATECTL_MAX];
46 
47 static const char *ratectl_modnames[IEEE80211_RATECTL_MAX] = {
48 	[IEEE80211_RATECTL_AMRR]	= "wlan_amrr",
49 	[IEEE80211_RATECTL_RSSADAPT]	= "wlan_rssadapt",
50 	[IEEE80211_RATECTL_ONOE]	= "wlan_onoe",
51 	[IEEE80211_RATECTL_SAMPLE]	= "wlan_sample",
52 	[IEEE80211_RATECTL_NONE]	= "wlan_none",
53 };
54 
55 MALLOC_DEFINE(M_80211_RATECTL, "80211ratectl", "802.11 rate control");
56 
57 void
58 ieee80211_ratectl_register(int type, const struct ieee80211_ratectl *ratectl)
59 {
60 	if (type >= IEEE80211_RATECTL_MAX)
61 		return;
62 	ratectls[type] = ratectl;
63 }
64 
65 void
66 ieee80211_ratectl_unregister(int type)
67 {
68 	if (type >= IEEE80211_RATECTL_MAX)
69 		return;
70 	ratectls[type] = NULL;
71 }
72 
73 static void
74 ieee80211_ratectl_sysctl_stats_node_iter(void *arg, struct ieee80211_node *ni)
75 {
76 
77 	struct sbuf *sb = (struct sbuf *) arg;
78 #if defined(__DragonFly__)
79 	sbuf_printf(sb, "MAC: %6s\n", ether_sprintf(ni->ni_macaddr));
80 #else
81 	sbuf_printf(sb, "MAC: %6D\n", ni->ni_macaddr, ":");
82 #endif
83 	ieee80211_ratectl_node_stats(ni, sb);
84 	sbuf_printf(sb, "\n");
85 }
86 
87 static int
88 ieee80211_ratectl_sysctl_stats(SYSCTL_HANDLER_ARGS)
89 {
90 	struct ieee80211vap *vap = arg1;
91 	struct ieee80211com *ic = vap->iv_ic;
92 	struct sbuf sb;
93 	int error;
94 
95 #if defined(__DragonFly__)
96 #else
97 	error = sysctl_wire_old_buffer(req, 0);
98 	if (error)
99 		return (error);
100 #endif
101 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
102 #if defined(__DragonFly__)
103 #else
104 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
105 #endif
106 
107 	IEEE80211_LOCK(ic);
108 	ieee80211_iterate_nodes(&ic->ic_sta,
109 	    ieee80211_ratectl_sysctl_stats_node_iter,
110 	    &sb);
111 	IEEE80211_UNLOCK(ic);
112 
113 	error = sbuf_finish(&sb);
114 	sbuf_delete(&sb);
115 	return (error);
116 }
117 
118 void
119 ieee80211_ratectl_init(struct ieee80211vap *vap)
120 {
121 	if (vap->iv_rate == ratectls[IEEE80211_RATECTL_NONE])
122 		ieee80211_ratectl_set(vap, IEEE80211_RATECTL_AMRR);
123 	vap->iv_rate->ir_init(vap);
124 
125 	/* Attach generic stats sysctl */
126 #if defined(__DragonFly__)
127 	SYSCTL_ADD_PROC(vap->iv_sysctl, SYSCTL_CHILDREN(vap->iv_oid), OID_AUTO,
128 	    "rate_stats", CTLTYPE_STRING | CTLFLAG_RD, vap,
129 	    0, ieee80211_ratectl_sysctl_stats, "A", "ratectl node stats");
130 #else
131 	SYSCTL_ADD_PROC(vap->iv_sysctl, SYSCTL_CHILDREN(vap->iv_oid), OID_AUTO,
132 	    "rate_stats", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, vap,
133 	    0, ieee80211_ratectl_sysctl_stats, "A", "ratectl node stats");
134 #endif
135 }
136 
137 void
138 ieee80211_ratectl_set(struct ieee80211vap *vap, int type)
139 {
140 	if (type >= IEEE80211_RATECTL_MAX)
141 		return;
142 	if (ratectls[type] == NULL) {
143 		ieee80211_load_module(ratectl_modnames[type]);
144 		if (ratectls[type] == NULL) {
145 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_RATECTL,
146 			    "%s: unable to load algo %u, module %s\n",
147 			    __func__, type, ratectl_modnames[type]);
148 			vap->iv_rate = ratectls[IEEE80211_RATECTL_NONE];
149 			return;
150 		}
151 	}
152 	vap->iv_rate = ratectls[type];
153 }
154