2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005, Devicescape Software, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/config.h>
11 #include <linux/version.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/netdevice.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/compiler.h>
20 #include <net/ieee80211.h>
21 #include "ieee80211_i.h"
22 #include "rate_control.h"
25 /* This is a minimal implementation of TX rate controlling that can be used
26 * as the default when no improved mechanisms are available. */
29 #define RATE_CONTROL_EMERG_DEC 2
30 #define RATE_CONTROL_INTERVAL (HZ / 20)
31 #define RATE_CONTROL_MIN_TX 10
34 static void rate_control_rate_inc(struct ieee80211_local
*local
,
37 struct ieee80211_sub_if_data
*sdata
;
41 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
42 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
43 /* forced unicast rate - do not change STA rate */
47 maxrate
= sdata
->bss
? sdata
->bss
->max_ratectrl_rateidx
: -1;
49 if (i
> local
->num_curr_rates
)
50 i
= local
->num_curr_rates
- 2;
52 while (i
+ 1 < local
->num_curr_rates
) {
54 if (sta
->supp_rates
& BIT(i
) &&
55 local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
&&
56 (maxrate
< 0 || i
<= maxrate
)) {
64 static void rate_control_rate_dec(struct ieee80211_local
*local
,
67 struct ieee80211_sub_if_data
*sdata
;
70 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
71 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
72 /* forced unicast rate - do not change STA rate */
76 if (i
> local
->num_curr_rates
)
77 i
= local
->num_curr_rates
;
81 if (sta
->supp_rates
& BIT(i
) &&
82 local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
) {
90 static struct ieee80211_rate
*
91 rate_control_lowest_rate(struct ieee80211_local
*local
)
95 for (i
= 0; i
< local
->num_curr_rates
; i
++) {
96 struct ieee80211_rate
*rate
= &local
->curr_rates
[i
];
98 if (rate
->flags
& IEEE80211_RATE_SUPPORTED
103 printk(KERN_DEBUG
"rate_control_lowest_rate - no supported rates "
105 return &local
->curr_rates
[0];
109 struct global_rate_control
{
113 struct sta_rate_control
{
114 unsigned long last_rate_change
;
118 unsigned long avg_rate_update
;
124 static void rate_control_simple_tx_status(struct net_device
*dev
,
126 struct ieee80211_tx_status
*status
)
128 struct ieee80211_local
*local
= dev
->priv
;
129 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
130 struct sta_info
*sta
;
131 struct sta_rate_control
*srctrl
;
133 sta
= sta_info_get(local
, hdr
->addr1
);
138 srctrl
= sta
->rate_ctrl_priv
;
139 srctrl
->tx_num_xmit
++;
140 if (status
->excessive_retries
) {
141 sta
->antenna_sel
= sta
->antenna_sel
== 1 ? 2 : 1;
142 if (local
->sta_antenna_sel
== STA_ANTENNA_SEL_SW_CTRL_DEBUG
) {
143 printk(KERN_DEBUG
"%s: " MACSTR
" TX antenna --> %d "
145 dev
->name
, MAC2STR(hdr
->addr1
),
146 sta
->antenna_sel
, jiffies
);
148 srctrl
->tx_num_failures
++;
149 sta
->tx_retry_failed
++;
150 sta
->tx_num_consecutive_failures
++;
151 sta
->tx_num_mpdu_fail
++;
153 sta
->last_ack_rssi
[0] = sta
->last_ack_rssi
[1];
154 sta
->last_ack_rssi
[1] = sta
->last_ack_rssi
[2];
155 sta
->last_ack_rssi
[2] = status
->ack_signal
;
156 sta
->tx_num_consecutive_failures
= 0;
157 sta
->tx_num_mpdu_ok
++;
159 sta
->tx_retry_count
+= status
->retry_count
;
160 sta
->tx_num_mpdu_fail
+= status
->retry_count
;
162 if (time_after(jiffies
,
163 srctrl
->last_rate_change
+ RATE_CONTROL_INTERVAL
) &&
164 srctrl
->tx_num_xmit
> RATE_CONTROL_MIN_TX
) {
166 srctrl
->last_rate_change
= jiffies
;
168 per_failed
= (100 * sta
->tx_num_mpdu_fail
) /
169 (sta
->tx_num_mpdu_fail
+ sta
->tx_num_mpdu_ok
);
170 /* TODO: calculate average per_failed to make adjusting
171 * parameters easier */
173 if (net_ratelimit()) {
174 printk(KERN_DEBUG
"MPDU fail=%d ok=%d per_failed=%d\n",
175 sta
->tx_num_mpdu_fail
, sta
->tx_num_mpdu_ok
,
180 if (per_failed
> local
->rate_ctrl_num_down
) {
181 rate_control_rate_dec(local
, sta
);
182 } else if (per_failed
< local
->rate_ctrl_num_up
) {
183 rate_control_rate_inc(local
, sta
);
185 srctrl
->tx_avg_rate_sum
+= local
->curr_rates
[sta
->txrate
].rate
;
186 srctrl
->tx_avg_rate_num
++;
187 srctrl
->tx_num_failures
= 0;
188 srctrl
->tx_num_xmit
= 0;
189 } else if (sta
->tx_num_consecutive_failures
>=
190 RATE_CONTROL_EMERG_DEC
) {
191 rate_control_rate_dec(local
, sta
);
194 if (srctrl
->avg_rate_update
+ 60 * HZ
< jiffies
) {
195 srctrl
->avg_rate_update
= jiffies
;
196 if (srctrl
->tx_avg_rate_num
> 0) {
197 #ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
198 printk(KERN_DEBUG
"%s: STA " MACSTR
" Average rate: "
200 dev
->name
, MAC2STR(sta
->addr
),
201 srctrl
->tx_avg_rate_sum
/
202 srctrl
->tx_avg_rate_num
,
203 srctrl
->tx_avg_rate_sum
,
204 srctrl
->tx_avg_rate_num
);
205 #endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
206 srctrl
->tx_avg_rate_sum
= 0;
207 srctrl
->tx_avg_rate_num
= 0;
211 sta_info_release(local
, sta
);
215 static struct ieee80211_rate
*
216 rate_control_simple_get_rate(struct net_device
*dev
, struct sk_buff
*skb
,
217 struct rate_control_extra
*extra
)
219 struct ieee80211_local
*local
= dev
->priv
;
220 struct ieee80211_sub_if_data
*sdata
;
221 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
222 struct sta_info
*sta
;
223 int rateidx
, nonerp_idx
;
226 memset(extra
, 0, sizeof(*extra
));
228 fc
= le16_to_cpu(hdr
->frame_control
);
229 if (WLAN_FC_GET_TYPE(fc
) != WLAN_FC_TYPE_DATA
||
230 (hdr
->addr1
[0] & 0x01)) {
231 /* Send management frames and broadcast/multicast data using
233 /* TODO: this could probably be improved.. */
234 return rate_control_lowest_rate(local
);
237 sta
= sta_info_get(local
, hdr
->addr1
);
240 return rate_control_lowest_rate(local
);
242 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
243 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1)
244 sta
->txrate
= sdata
->bss
->force_unicast_rateidx
;
246 rateidx
= sta
->txrate
;
248 if (rateidx
>= local
->num_curr_rates
)
249 rateidx
= local
->num_curr_rates
- 1;
251 sta
->last_txrate
= rateidx
;
252 nonerp_idx
= rateidx
;
253 while (nonerp_idx
> 0 &&
254 ((local
->curr_rates
[nonerp_idx
].flags
& IEEE80211_RATE_ERP
) ||
255 !(local
->curr_rates
[nonerp_idx
].flags
&
256 IEEE80211_RATE_SUPPORTED
) ||
257 !(sta
->supp_rates
& BIT(nonerp_idx
))))
259 extra
->nonerp_idx
= nonerp_idx
;
260 extra
->nonerp
= &local
->curr_rates
[extra
->nonerp_idx
];
262 sta_info_release(local
, sta
);
264 return &local
->curr_rates
[rateidx
];
268 static void rate_control_simple_rate_init(struct ieee80211_local
*local
,
269 struct sta_info
*sta
)
273 /* TODO: what is a good starting rate for STA? About middle? Maybe not
274 * the lowest or the highest rate.. Could consider using RSSI from
275 * previous packets? Need to have IEEE 802.1X auth succeed immediately
277 for (i
= 0; i
< local
->num_curr_rates
; i
++) {
278 if ((sta
->supp_rates
& BIT(i
)) &&
279 (local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
))
285 static void * rate_control_simple_alloc(struct ieee80211_local
*local
)
287 struct global_rate_control
*rctrl
;
289 rctrl
= kmalloc(sizeof(*rctrl
), GFP_ATOMIC
);
293 memset(rctrl
, 0, sizeof(*rctrl
));
298 static void rate_control_simple_free(void *priv
)
300 struct global_rate_control
*rctrl
= priv
;
305 static void rate_control_simple_clear(void *priv
)
310 static void * rate_control_simple_alloc_sta(void)
312 struct sta_rate_control
*rctrl
;
314 rctrl
= kmalloc(sizeof(*rctrl
), GFP_ATOMIC
);
318 memset(rctrl
, 0, sizeof(*rctrl
));
323 static void rate_control_simple_free_sta(void *priv
)
325 struct sta_rate_control
*rctrl
= priv
;
330 static int rate_control_simple_status_sta(struct ieee80211_local
*local
,
331 struct sta_info
*sta
, char *buf
)
334 struct sta_rate_control
*srctrl
= sta
->rate_ctrl_priv
;
336 p
+= sprintf(p
, "tx_avg_rate_sum=%d\n", srctrl
->tx_avg_rate_sum
);
337 p
+= sprintf(p
, "tx_avg_rate_num=%d\n", srctrl
->tx_avg_rate_num
);
338 if (srctrl
->tx_avg_rate_num
)
339 p
+= sprintf(p
, "tx_avg_rate_avg=%d\n",
340 srctrl
->tx_avg_rate_sum
/
341 srctrl
->tx_avg_rate_num
);
346 static int rate_control_simple_status_global(struct ieee80211_local
*local
,
353 static struct rate_control_ops rate_control_simple
= {
355 .tx_status
= rate_control_simple_tx_status
,
356 .get_rate
= rate_control_simple_get_rate
,
357 .rate_init
= rate_control_simple_rate_init
,
358 .clear
= rate_control_simple_clear
,
359 .status_sta
= rate_control_simple_status_sta
,
360 .status_global
= rate_control_simple_status_global
,
361 .alloc
= rate_control_simple_alloc
,
362 .free
= rate_control_simple_free
,
363 .alloc_sta
= rate_control_simple_alloc_sta
,
364 .free_sta
= rate_control_simple_free_sta
,
368 int __init
rate_control_simple_init(void)
370 return ieee80211_rate_control_register(&rate_control_simple
);
374 static void __exit
rate_control_simple_exit(void)
376 ieee80211_rate_control_unregister(&rate_control_simple
);
380 module_init(rate_control_simple_init
);
381 module_exit(rate_control_simple_exit
);