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/mac80211.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
;
37 struct ieee80211_hw_mode
*mode
;
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 mode
= local
->oper_hw_mode
;
48 maxrate
= sdata
->bss
? sdata
->bss
->max_ratectrl_rateidx
: -1;
50 if (i
> mode
->num_rates
)
51 i
= mode
->num_rates
- 2;
53 while (i
+ 1 < mode
->num_rates
) {
55 if (sta
->supp_rates
& BIT(i
) &&
56 mode
->rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
&&
57 (maxrate
< 0 || i
<= maxrate
)) {
65 static void rate_control_rate_dec(struct ieee80211_local
*local
,
68 struct ieee80211_sub_if_data
*sdata
;
69 struct ieee80211_hw_mode
*mode
;
72 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
73 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
74 /* forced unicast rate - do not change STA rate */
78 mode
= local
->oper_hw_mode
;
79 if (i
> mode
->num_rates
)
84 if (sta
->supp_rates
& BIT(i
) &&
85 mode
->rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
) {
93 static struct ieee80211_rate
*
94 rate_control_lowest_rate(struct ieee80211_local
*local
,
95 struct ieee80211_hw_mode
*mode
)
99 for (i
= 0; i
< mode
->num_rates
; i
++) {
100 struct ieee80211_rate
*rate
= &mode
->rates
[i
];
102 if (rate
->flags
& IEEE80211_RATE_SUPPORTED
)
106 printk(KERN_DEBUG
"rate_control_lowest_rate - no supported rates "
108 return &mode
->rates
[0];
112 struct global_rate_control
{
116 struct sta_rate_control
{
117 unsigned long last_rate_change
;
121 unsigned long avg_rate_update
;
127 static void rate_control_simple_tx_status(void *priv
, struct net_device
*dev
,
129 struct ieee80211_tx_status
*status
)
131 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
132 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
133 struct sta_info
*sta
;
134 struct sta_rate_control
*srctrl
;
136 sta
= sta_info_get(local
, hdr
->addr1
);
141 srctrl
= sta
->rate_ctrl_priv
;
142 srctrl
->tx_num_xmit
++;
143 if (status
->excessive_retries
) {
144 sta
->antenna_sel_tx
= sta
->antenna_sel_tx
== 1 ? 2 : 1;
145 sta
->antenna_sel_rx
= sta
->antenna_sel_rx
== 1 ? 2 : 1;
146 if (local
->sta_antenna_sel
== STA_ANTENNA_SEL_SW_CTRL_DEBUG
) {
147 printk(KERN_DEBUG
"%s: " MAC_FMT
" TX antenna --> %d "
148 "RX antenna --> %d (@%lu)\n",
149 dev
->name
, MAC_ARG(hdr
->addr1
),
150 sta
->antenna_sel_tx
, sta
->antenna_sel_rx
, jiffies
);
152 srctrl
->tx_num_failures
++;
153 sta
->tx_retry_failed
++;
154 sta
->tx_num_consecutive_failures
++;
155 sta
->tx_num_mpdu_fail
++;
157 sta
->last_ack_rssi
[0] = sta
->last_ack_rssi
[1];
158 sta
->last_ack_rssi
[1] = sta
->last_ack_rssi
[2];
159 sta
->last_ack_rssi
[2] = status
->ack_signal
;
160 sta
->tx_num_consecutive_failures
= 0;
161 sta
->tx_num_mpdu_ok
++;
163 sta
->tx_retry_count
+= status
->retry_count
;
164 sta
->tx_num_mpdu_fail
+= status
->retry_count
;
166 if (time_after(jiffies
,
167 srctrl
->last_rate_change
+ RATE_CONTROL_INTERVAL
) &&
168 srctrl
->tx_num_xmit
> RATE_CONTROL_MIN_TX
) {
170 srctrl
->last_rate_change
= jiffies
;
172 per_failed
= (100 * sta
->tx_num_mpdu_fail
) /
173 (sta
->tx_num_mpdu_fail
+ sta
->tx_num_mpdu_ok
);
174 /* TODO: calculate average per_failed to make adjusting
175 * parameters easier */
177 if (net_ratelimit()) {
178 printk(KERN_DEBUG
"MPDU fail=%d ok=%d per_failed=%d\n",
179 sta
->tx_num_mpdu_fail
, sta
->tx_num_mpdu_ok
,
184 if (per_failed
> local
->rate_ctrl_num_down
) {
185 rate_control_rate_dec(local
, sta
);
186 } else if (per_failed
< local
->rate_ctrl_num_up
) {
187 rate_control_rate_inc(local
, sta
);
189 srctrl
->tx_avg_rate_sum
+= status
->control
.rate
->rate
;
190 srctrl
->tx_avg_rate_num
++;
191 srctrl
->tx_num_failures
= 0;
192 srctrl
->tx_num_xmit
= 0;
193 } else if (sta
->tx_num_consecutive_failures
>=
194 RATE_CONTROL_EMERG_DEC
) {
195 rate_control_rate_dec(local
, sta
);
198 if (srctrl
->avg_rate_update
+ 60 * HZ
< jiffies
) {
199 srctrl
->avg_rate_update
= jiffies
;
200 if (srctrl
->tx_avg_rate_num
> 0) {
201 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
202 printk(KERN_DEBUG
"%s: STA " MAC_FMT
" Average rate: "
204 dev
->name
, MAC_ARG(sta
->addr
),
205 srctrl
->tx_avg_rate_sum
/
206 srctrl
->tx_avg_rate_num
,
207 srctrl
->tx_avg_rate_sum
,
208 srctrl
->tx_avg_rate_num
);
209 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
210 srctrl
->tx_avg_rate_sum
= 0;
211 srctrl
->tx_avg_rate_num
= 0;
219 static struct ieee80211_rate
*
220 rate_control_simple_get_rate(void *priv
, struct net_device
*dev
,
222 struct rate_control_extra
*extra
)
224 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
225 struct ieee80211_sub_if_data
*sdata
;
226 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
227 struct ieee80211_hw_mode
*mode
= extra
->mode
;
228 struct sta_info
*sta
;
229 int rateidx
, nonerp_idx
;
232 memset(extra
, 0, sizeof(*extra
));
234 fc
= le16_to_cpu(hdr
->frame_control
);
235 if ((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
||
236 (hdr
->addr1
[0] & 0x01)) {
237 /* Send management frames and broadcast/multicast data using
239 /* TODO: this could probably be improved.. */
240 return rate_control_lowest_rate(local
, mode
);
243 sta
= sta_info_get(local
, hdr
->addr1
);
246 return rate_control_lowest_rate(local
, mode
);
248 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
249 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1)
250 sta
->txrate
= sdata
->bss
->force_unicast_rateidx
;
252 rateidx
= sta
->txrate
;
254 if (rateidx
>= mode
->num_rates
)
255 rateidx
= mode
->num_rates
- 1;
257 sta
->last_txrate
= rateidx
;
258 nonerp_idx
= rateidx
;
259 while (nonerp_idx
> 0 &&
260 ((mode
->rates
[nonerp_idx
].flags
& IEEE80211_RATE_ERP
) ||
261 !(mode
->rates
[nonerp_idx
].flags
& IEEE80211_RATE_SUPPORTED
) ||
262 !(sta
->supp_rates
& BIT(nonerp_idx
))))
264 extra
->nonerp
= &mode
->rates
[nonerp_idx
];
268 return &mode
->rates
[rateidx
];
272 static void rate_control_simple_rate_init(void *priv
, void *priv_sta
,
273 struct ieee80211_local
*local
,
274 struct sta_info
*sta
)
276 struct ieee80211_hw_mode
*mode
;
279 mode
= local
->oper_hw_mode
;
280 /* TODO: what is a good starting rate for STA? About middle? Maybe not
281 * the lowest or the highest rate.. Could consider using RSSI from
282 * previous packets? Need to have IEEE 802.1X auth succeed immediately
284 for (i
= 0; i
< mode
->num_rates
; i
++) {
285 if ((sta
->supp_rates
& BIT(i
)) &&
286 (mode
->rates
[i
].flags
& IEEE80211_RATE_SUPPORTED
))
292 static void * rate_control_simple_alloc(struct ieee80211_local
*local
)
294 struct global_rate_control
*rctrl
;
296 rctrl
= kzalloc(sizeof(*rctrl
), GFP_ATOMIC
);
302 static void rate_control_simple_free(void *priv
)
304 struct global_rate_control
*rctrl
= priv
;
309 static void rate_control_simple_clear(void *priv
)
314 static void * rate_control_simple_alloc_sta(void *priv
, gfp_t gfp
)
316 struct sta_rate_control
*rctrl
;
318 rctrl
= kzalloc(sizeof(*rctrl
), gfp
);
324 static void rate_control_simple_free_sta(void *priv
, void *priv_sta
)
326 struct sta_rate_control
*rctrl
= priv_sta
;
331 static struct rate_control_ops rate_control_simple
= {
332 .module
= THIS_MODULE
,
334 .tx_status
= rate_control_simple_tx_status
,
335 .get_rate
= rate_control_simple_get_rate
,
336 .rate_init
= rate_control_simple_rate_init
,
337 .clear
= rate_control_simple_clear
,
338 .alloc
= rate_control_simple_alloc
,
339 .free
= rate_control_simple_free
,
340 .alloc_sta
= rate_control_simple_alloc_sta
,
341 .free_sta
= rate_control_simple_free_sta
,
345 static int __init
rate_control_simple_init(void)
347 return ieee80211_rate_control_register(&rate_control_simple
);
351 static void __exit
rate_control_simple_exit(void)
353 ieee80211_rate_control_unregister(&rate_control_simple
);
357 module_init(rate_control_simple_init
);
358 module_exit(rate_control_simple_exit
);
360 MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
361 MODULE_LICENSE("GPL");