2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/netproto/802_11/wlan/ieee80211_ratectl.c,v 1.5 2008/01/15 09:01:13 sephe Exp $
37 #include <sys/param.h>
38 #include <sys/kernel.h>
41 #include <net/if_media.h>
42 #include <net/if_arp.h>
43 #include <net/route.h>
45 #include <netproto/802_11/ieee80211_var.h>
47 static const struct ieee80211_ratectl *ratectls[IEEE80211_RATECTL_MAX] = {
48 [IEEE80211_RATECTL_NONE] = &ieee80211_ratectl_none
51 static const char *ratectl_modname[IEEE80211_RATECTL_MAX] = {
52 [IEEE80211_RATECTL_ONOE] = "wlan_ratectl_onoe",
53 [IEEE80211_RATECTL_AMRR] = "wlan_ratectl_amrr",
54 [IEEE80211_RATECTL_SAMPLE] = "wlan_ratectl_sample"
58 ieee80211_ratectl_attach(struct ieee80211vap *vap)
60 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
61 u_int cur_ratectl = rc_st->rc_st_ratectl;
63 rc_st->rc_st_ratectl_cap |= IEEE80211_RATECTL_CAP_NONE;
64 rc_st->rc_st_ratectl = IEEE80211_RATECTL_NONE;
66 ieee80211_ratectl_change(vap, cur_ratectl);
70 ieee80211_ratectl_detach(struct ieee80211vap *vap)
72 ieee80211_ratectl_change(vap, IEEE80211_RATECTL_NONE);
76 ieee80211_ratectl_register(const struct ieee80211_ratectl *rc)
81 if (rc->rc_ratectl >= IEEE80211_RATECTL_MAX) {
82 kprintf("%s: rate control %s has an invalid index %d\n",
83 __func__, rc->rc_name, rc->rc_ratectl);
86 if (ratectls[rc->rc_ratectl] != NULL &&
87 ratectls[rc->rc_ratectl] != rc) {
88 kprintf("%s: rate control index %d is registered by %s\n",
89 __func__, rc->rc_ratectl,
90 ratectls[rc->rc_ratectl]->rc_name);
94 ratectls[rc->rc_ratectl] = rc;
98 ieee80211_ratectl_unregister(const struct ieee80211_ratectl *rc)
103 if (rc->rc_ratectl >= IEEE80211_RATECTL_MAX) {
104 kprintf("%s: rate control %s has an invalid index %d\n",
105 __func__, rc->rc_name, rc->rc_ratectl);
108 if (ratectls[rc->rc_ratectl] != NULL &&
109 ratectls[rc->rc_ratectl] != rc) {
110 kprintf("%s: rate control index %d is registered by %s\n",
111 __func__, rc->rc_ratectl,
112 ratectls[rc->rc_ratectl]->rc_name);
117 * Indiviual rate control module MUST maintain reference count itself.
119 ratectls[rc->rc_ratectl] = NULL;
123 ieee80211_ratectl_change(struct ieee80211vap *vap, u_int rc_idx)
125 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
126 const struct ieee80211_ratectl *rc, *rc_old;
128 if (rc_idx == rc_st->rc_st_ratectl) {
129 /* Nothing need to be changed */
133 if ((IEEE80211_RATECTL_CAP(rc_idx) & rc_st->rc_st_ratectl_cap) == 0) {
134 /* We are not capable to do requested rate control */
138 rc = ratectls[rc_idx];
140 /* Try load the rate control module */
141 ieee80211_load_module(ratectl_modname[rc_idx]);
144 * If rate control module loaded it should immediately
145 * call ieee80211_ratectl_register() which will fill in
146 * the entry in the 'ratectls' array.
148 rc = ratectls[rc_idx];
150 kprintf("%s: can't load requested rate control module",
156 /* Detach old rate control */
157 rc_old = ratectls[rc_st->rc_st_ratectl];
158 rc_old->rc_detach(rc_st->rc_st_ctx);
160 /* Attach new rate control */
161 rc_st->rc_st_ratectl = rc_idx;
162 rc_st->rc_st_ctx = rc->rc_attach(vap);
168 ieee80211_ratectl_data_alloc(struct ieee80211_node *ni)
170 struct ieee80211vap *vap = ni->ni_vap;
171 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
172 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
174 rc->rc_data_alloc(ni);
178 ieee80211_ratectl_data_dup(const struct ieee80211_node *oni,
179 struct ieee80211_node *nni)
181 struct ieee80211vap *vap = oni->ni_vap;
182 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
183 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
185 rc->rc_data_dup(oni, nni);
189 ieee80211_ratectl_data_free(struct ieee80211_node *ni)
191 struct ieee80211vap *vap = ni->ni_vap;
192 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
193 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
195 rc->rc_data_free(ni);
199 ieee80211_ratectl_newstate(struct ieee80211vap *vap, enum ieee80211_state state)
201 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
202 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
204 rc->rc_newstate(rc_st->rc_st_ctx, state);
208 ieee80211_ratectl_tx_complete(struct ieee80211_node *ni, int frame_len,
209 const struct ieee80211_ratectl_res res[],
210 int res_len, int data_retries, int rts_retries,
213 struct ieee80211vap *vap = ni->ni_vap;
214 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
215 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
217 rc->rc_tx_complete(rc_st->rc_st_ctx, ni, frame_len, res, res_len,
218 data_retries, rts_retries, is_fail);
222 ieee80211_ratectl_newassoc(struct ieee80211_node *ni, int is_new)
224 struct ieee80211vap *vap = ni->ni_vap;
225 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
226 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
228 rc->rc_newassoc(rc_st->rc_st_ctx, ni, is_new);
232 ieee80211_ratectl_findrate(struct ieee80211_node *ni, int frame_len,
233 int rateidx[], int rateidx_len)
235 struct ieee80211vap *vap = ni->ni_vap;
236 struct ieee80211_ratectl_state *rc_st = &vap->iv_ratectl;
237 const struct ieee80211_ratectl *rc = ratectls[rc_st->rc_st_ratectl];
239 KKASSERT(rateidx_len > 0);
241 return rc->rc_findrate(rc_st->rc_st_ctx, ni, frame_len,
242 rateidx, rateidx_len);