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