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/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/compiler.h>
18 #include <net/d80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
23 /* This is a minimal implementation of TX rate controlling that can be used
24 * as the default when no improved mechanisms are available. */
27 #define RATE_CONTROL_EMERG_DEC 2
28 #define RATE_CONTROL_INTERVAL (HZ / 20)
29 #define RATE_CONTROL_MIN_TX 10
31 MODULE_ALIAS("rc80211_default");
33 static void rate_control_rate_inc(struct ieee80211_local
*local
,
36 struct ieee80211_sub_if_data
*sdata
;
40 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
41 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
42 /* forced unicast rate - do not change STA rate */
46 maxrate
= sdata
->bss
? sdata
->bss
->max_ratectrl_rateidx
: -1;
48 if (i
> local
->num_curr_rates
)
49 i
= local
->num_curr_rates
- 2;
51 while (i
+ 1 < local
->num_curr_rates
) {
53 if (sta
->supp_rates
& BIT(i
) &&
54 local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
&&
55 (maxrate
< 0 || i
<= maxrate
)) {
63 static void rate_control_rate_dec(struct ieee80211_local
*local
,
66 struct ieee80211_sub_if_data
*sdata
;
69 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
70 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
71 /* forced unicast rate - do not change STA rate */
75 if (i
> local
->num_curr_rates
)
76 i
= local
->num_curr_rates
;
80 if (sta
->supp_rates
& BIT(i
) &&
81 local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
) {
89 static struct ieee80211_rate
*
90 rate_control_lowest_rate(struct ieee80211_local
*local
)
94 for (i
= 0; i
< local
->num_curr_rates
; i
++) {
95 struct ieee80211_rate
*rate
= &local
->curr_rates
[i
];
97 if (rate
->flags
& IEEE80211_RATE_SUPPORTED
102 printk(KERN_DEBUG
"rate_control_lowest_rate - no supported rates "
104 return &local
->curr_rates
[0];
108 struct global_rate_control
{
112 struct sta_rate_control
{
113 unsigned long last_rate_change
;
117 unsigned long avg_rate_update
;
123 static void rate_control_simple_tx_status(void *priv
, struct net_device
*dev
,
125 struct ieee80211_tx_status
*status
)
127 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
128 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
129 struct sta_info
*sta
;
130 struct sta_rate_control
*srctrl
;
132 sta
= sta_info_get(local
, hdr
->addr1
);
137 srctrl
= sta
->rate_ctrl_priv
;
138 srctrl
->tx_num_xmit
++;
139 if (status
->excessive_retries
) {
140 sta
->antenna_sel
= sta
->antenna_sel
== 1 ? 2 : 1;
141 if (local
->sta_antenna_sel
== STA_ANTENNA_SEL_SW_CTRL_DEBUG
) {
142 printk(KERN_DEBUG
"%s: " MAC_FMT
" TX antenna --> %d "
144 dev
->name
, MAC_ARG(hdr
->addr1
),
145 sta
->antenna_sel
, jiffies
);
147 srctrl
->tx_num_failures
++;
148 sta
->tx_retry_failed
++;
149 sta
->tx_num_consecutive_failures
++;
150 sta
->tx_num_mpdu_fail
++;
152 sta
->last_ack_rssi
[0] = sta
->last_ack_rssi
[1];
153 sta
->last_ack_rssi
[1] = sta
->last_ack_rssi
[2];
154 sta
->last_ack_rssi
[2] = status
->ack_signal
;
155 sta
->tx_num_consecutive_failures
= 0;
156 sta
->tx_num_mpdu_ok
++;
158 sta
->tx_retry_count
+= status
->retry_count
;
159 sta
->tx_num_mpdu_fail
+= status
->retry_count
;
161 if (time_after(jiffies
,
162 srctrl
->last_rate_change
+ RATE_CONTROL_INTERVAL
) &&
163 srctrl
->tx_num_xmit
> RATE_CONTROL_MIN_TX
) {
165 srctrl
->last_rate_change
= jiffies
;
167 per_failed
= (100 * sta
->tx_num_mpdu_fail
) /
168 (sta
->tx_num_mpdu_fail
+ sta
->tx_num_mpdu_ok
);
169 /* TODO: calculate average per_failed to make adjusting
170 * parameters easier */
172 if (net_ratelimit()) {
173 printk(KERN_DEBUG
"MPDU fail=%d ok=%d per_failed=%d\n",
174 sta
->tx_num_mpdu_fail
, sta
->tx_num_mpdu_ok
,
179 if (per_failed
> local
->rate_ctrl_num_down
) {
180 rate_control_rate_dec(local
, sta
);
181 } else if (per_failed
< local
->rate_ctrl_num_up
) {
182 rate_control_rate_inc(local
, sta
);
184 srctrl
->tx_avg_rate_sum
+= local
->curr_rates
[sta
->txrate
].rate
;
185 srctrl
->tx_avg_rate_num
++;
186 srctrl
->tx_num_failures
= 0;
187 srctrl
->tx_num_xmit
= 0;
188 } else if (sta
->tx_num_consecutive_failures
>=
189 RATE_CONTROL_EMERG_DEC
) {
190 rate_control_rate_dec(local
, sta
);
193 if (srctrl
->avg_rate_update
+ 60 * HZ
< jiffies
) {
194 srctrl
->avg_rate_update
= jiffies
;
195 if (srctrl
->tx_avg_rate_num
> 0) {
196 #ifdef CONFIG_D80211_VERBOSE_DEBUG
197 printk(KERN_DEBUG
"%s: STA " MAC_FMT
" Average rate: "
199 dev
->name
, MAC_ARG(sta
->addr
),
200 srctrl
->tx_avg_rate_sum
/
201 srctrl
->tx_avg_rate_num
,
202 srctrl
->tx_avg_rate_sum
,
203 srctrl
->tx_avg_rate_num
);
204 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
205 srctrl
->tx_avg_rate_sum
= 0;
206 srctrl
->tx_avg_rate_num
= 0;
214 static struct ieee80211_rate
*
215 rate_control_simple_get_rate(void *priv
, struct net_device
*dev
,
217 struct rate_control_extra
*extra
)
219 struct ieee80211_local
*local
= dev
->ieee80211_ptr
;
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 ((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_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
];
264 return &local
->curr_rates
[rateidx
];
268 static void rate_control_simple_rate_init(void *priv
, void *priv_sta
,
269 struct ieee80211_local
*local
,
270 struct sta_info
*sta
)
274 /* TODO: what is a good starting rate for STA? About middle? Maybe not
275 * the lowest or the highest rate.. Could consider using RSSI from
276 * previous packets? Need to have IEEE 802.1X auth succeed immediately
278 for (i
= 0; i
< local
->num_curr_rates
; i
++) {
279 if ((sta
->supp_rates
& BIT(i
)) &&
280 (local
->curr_rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
))
286 static void * rate_control_simple_alloc(struct ieee80211_local
*local
)
288 struct global_rate_control
*rctrl
;
290 rctrl
= kzalloc(sizeof(*rctrl
), GFP_ATOMIC
);
296 static void rate_control_simple_free(void *priv
)
298 struct global_rate_control
*rctrl
= priv
;
303 static void rate_control_simple_clear(void *priv
)
308 static void * rate_control_simple_alloc_sta(void *priv
, gfp_t gfp
)
310 struct sta_rate_control
*rctrl
;
312 rctrl
= kzalloc(sizeof(*rctrl
), gfp
);
318 static void rate_control_simple_free_sta(void *priv
, void *priv_sta
)
320 struct sta_rate_control
*rctrl
= priv_sta
;
324 static ssize_t
show_sta_tx_avg_rate_sum(const struct sta_info
*sta
, char *buf
)
326 struct sta_rate_control
*srctrl
= sta
->rate_ctrl_priv
;
328 return sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_sum
);
331 static ssize_t
show_sta_tx_avg_rate_num(const struct sta_info
*sta
, char *buf
)
333 struct sta_rate_control
*srctrl
= sta
->rate_ctrl_priv
;
335 return sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_num
);
338 static struct sta_attribute sta_attr_tx_avg_rate_sum
=
339 __ATTR(tx_avg_rate_sum
, S_IRUSR
, show_sta_tx_avg_rate_sum
, NULL
);
340 static struct sta_attribute sta_attr_tx_avg_rate_num
=
341 __ATTR(tx_avg_rate_num
, S_IRUSR
, show_sta_tx_avg_rate_num
, NULL
);
343 static struct attribute
*rate_control_simple_sta_attrs
[] = {
344 &sta_attr_tx_avg_rate_sum
.attr
,
345 &sta_attr_tx_avg_rate_num
.attr
,
349 static struct attribute_group rate_control_simple_sta_group
= {
350 .name
= "rate_control_simple",
351 .attrs
= rate_control_simple_sta_attrs
,
354 static int rate_control_simple_add_sta_attrs(void *priv
, void *priv_sta
,
355 struct kobject
*kobj
)
357 return sysfs_create_group(kobj
, &rate_control_simple_sta_group
);
360 static void rate_control_simple_remove_sta_attrs(void *priv
, void *priv_sta
,
361 struct kobject
*kobj
)
363 sysfs_remove_group(kobj
, &rate_control_simple_sta_group
);
366 static struct rate_control_ops rate_control_simple
= {
367 .module
= THIS_MODULE
,
369 .tx_status
= rate_control_simple_tx_status
,
370 .get_rate
= rate_control_simple_get_rate
,
371 .rate_init
= rate_control_simple_rate_init
,
372 .clear
= rate_control_simple_clear
,
373 .alloc
= rate_control_simple_alloc
,
374 .free
= rate_control_simple_free
,
375 .alloc_sta
= rate_control_simple_alloc_sta
,
376 .free_sta
= rate_control_simple_free_sta
,
377 .add_sta_attrs
= rate_control_simple_add_sta_attrs
,
378 .remove_sta_attrs
= rate_control_simple_remove_sta_attrs
,
382 static int __init
rate_control_simple_init(void)
384 return ieee80211_rate_control_register(&rate_control_simple
);
388 static void __exit
rate_control_simple_exit(void)
390 ieee80211_rate_control_unregister(&rate_control_simple
);
394 module_init(rate_control_simple_init
);
395 module_exit(rate_control_simple_exit
);
397 MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
398 MODULE_LICENSE("GPL");