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/init.h>
11 #include <linux/netdevice.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/skbuff.h>
15 #include <linux/compiler.h>
16 #include <linux/module.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
24 /* This is a minimal implementation of TX rate controlling that can be used
25 * as the default when no improved mechanisms are available. */
27 #define RATE_CONTROL_NUM_DOWN 20
28 #define RATE_CONTROL_NUM_UP 15
30 #define RATE_CONTROL_EMERG_DEC 2
31 #define RATE_CONTROL_INTERVAL (HZ / 20)
32 #define RATE_CONTROL_MIN_TX 10
34 static void rate_control_rate_inc(struct ieee80211_local
*local
,
37 struct ieee80211_sub_if_data
*sdata
;
38 struct ieee80211_supported_band
*sband
;
39 int i
= sta
->txrate_idx
;
42 sdata
= IEEE80211_DEV_TO_SUB_IF(sta
->dev
);
43 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
44 /* forced unicast rate - do not change STA rate */
48 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
49 maxrate
= sdata
->bss
? sdata
->bss
->max_ratectrl_rateidx
: -1;
51 if (i
> sband
->n_bitrates
)
52 i
= sband
->n_bitrates
- 2;
54 while (i
+ 1 < sband
->n_bitrates
) {
56 if (rate_supported(sta
, sband
->band
, i
) &&
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_supported_band
*sband
;
70 int i
= sta
->txrate_idx
;
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 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
79 if (i
> sband
->n_bitrates
)
80 i
= sband
->n_bitrates
;
84 if (rate_supported(sta
, sband
->band
, i
)) {
91 struct global_rate_control
{
95 struct sta_rate_control
{
96 unsigned long last_rate_change
;
100 unsigned long avg_rate_update
;
104 #ifdef CONFIG_MAC80211_DEBUGFS
105 struct dentry
*tx_avg_rate_sum_dentry
;
106 struct dentry
*tx_avg_rate_num_dentry
;
111 static void rate_control_simple_tx_status(void *priv
, struct net_device
*dev
,
113 struct ieee80211_tx_status
*status
)
115 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
116 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
117 struct sta_info
*sta
;
118 struct sta_rate_control
*srctrl
;
120 sta
= sta_info_get(local
, hdr
->addr1
);
125 srctrl
= sta
->rate_ctrl_priv
;
126 srctrl
->tx_num_xmit
++;
127 if (status
->excessive_retries
) {
128 srctrl
->tx_num_failures
++;
129 sta
->tx_retry_failed
++;
130 sta
->tx_num_consecutive_failures
++;
131 sta
->tx_num_mpdu_fail
++;
133 sta
->last_ack_rssi
[0] = sta
->last_ack_rssi
[1];
134 sta
->last_ack_rssi
[1] = sta
->last_ack_rssi
[2];
135 sta
->last_ack_rssi
[2] = status
->ack_signal
;
136 sta
->tx_num_consecutive_failures
= 0;
137 sta
->tx_num_mpdu_ok
++;
139 sta
->tx_retry_count
+= status
->retry_count
;
140 sta
->tx_num_mpdu_fail
+= status
->retry_count
;
142 if (time_after(jiffies
,
143 srctrl
->last_rate_change
+ RATE_CONTROL_INTERVAL
) &&
144 srctrl
->tx_num_xmit
> RATE_CONTROL_MIN_TX
) {
146 srctrl
->last_rate_change
= jiffies
;
148 per_failed
= (100 * sta
->tx_num_mpdu_fail
) /
149 (sta
->tx_num_mpdu_fail
+ sta
->tx_num_mpdu_ok
);
150 /* TODO: calculate average per_failed to make adjusting
151 * parameters easier */
153 if (net_ratelimit()) {
154 printk(KERN_DEBUG
"MPDU fail=%d ok=%d per_failed=%d\n",
155 sta
->tx_num_mpdu_fail
, sta
->tx_num_mpdu_ok
,
161 * XXX: Make these configurable once we have an
162 * interface to the rate control algorithms
164 if (per_failed
> RATE_CONTROL_NUM_DOWN
) {
165 rate_control_rate_dec(local
, sta
);
166 } else if (per_failed
< RATE_CONTROL_NUM_UP
) {
167 rate_control_rate_inc(local
, sta
);
169 srctrl
->tx_avg_rate_sum
+= status
->control
.tx_rate
->bitrate
;
170 srctrl
->tx_avg_rate_num
++;
171 srctrl
->tx_num_failures
= 0;
172 srctrl
->tx_num_xmit
= 0;
173 } else if (sta
->tx_num_consecutive_failures
>=
174 RATE_CONTROL_EMERG_DEC
) {
175 rate_control_rate_dec(local
, sta
);
178 if (srctrl
->avg_rate_update
+ 60 * HZ
< jiffies
) {
179 srctrl
->avg_rate_update
= jiffies
;
180 if (srctrl
->tx_avg_rate_num
> 0) {
181 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
182 DECLARE_MAC_BUF(mac
);
183 printk(KERN_DEBUG
"%s: STA %s Average rate: "
185 dev
->name
, print_mac(mac
, sta
->addr
),
186 srctrl
->tx_avg_rate_sum
/
187 srctrl
->tx_avg_rate_num
,
188 srctrl
->tx_avg_rate_sum
,
189 srctrl
->tx_avg_rate_num
);
190 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
191 srctrl
->tx_avg_rate_sum
= 0;
192 srctrl
->tx_avg_rate_num
= 0;
201 rate_control_simple_get_rate(void *priv
, struct net_device
*dev
,
202 struct ieee80211_supported_band
*sband
,
204 struct rate_selection
*sel
)
206 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
207 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
208 struct ieee80211_sub_if_data
*sdata
;
209 struct sta_info
*sta
;
213 sta
= sta_info_get(local
, hdr
->addr1
);
215 /* Send management frames and broadcast/multicast data using lowest
217 fc
= le16_to_cpu(hdr
->frame_control
);
218 if ((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
||
219 is_multicast_ether_addr(hdr
->addr1
) || !sta
) {
220 sel
->rate
= rate_lowest(local
, sband
, sta
);
226 /* If a forced rate is in effect, select it. */
227 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
228 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1)
229 sta
->txrate_idx
= sdata
->bss
->force_unicast_rateidx
;
231 rateidx
= sta
->txrate_idx
;
233 if (rateidx
>= sband
->n_bitrates
)
234 rateidx
= sband
->n_bitrates
- 1;
236 sta
->last_txrate_idx
= rateidx
;
240 sel
->rate
= &sband
->bitrates
[rateidx
];
244 static void rate_control_simple_rate_init(void *priv
, void *priv_sta
,
245 struct ieee80211_local
*local
,
246 struct sta_info
*sta
)
248 struct ieee80211_supported_band
*sband
;
250 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
252 /* TODO: This routine should consider using RSSI from previous packets
253 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
254 * Until that method is implemented, we will use the lowest supported rate
255 * as a workaround, */
256 sta
->txrate_idx
= rate_lowest_index(local
, sband
, sta
);
260 static void * rate_control_simple_alloc(struct ieee80211_local
*local
)
262 struct global_rate_control
*rctrl
;
264 rctrl
= kzalloc(sizeof(*rctrl
), GFP_ATOMIC
);
270 static void rate_control_simple_free(void *priv
)
272 struct global_rate_control
*rctrl
= priv
;
277 static void rate_control_simple_clear(void *priv
)
282 static void * rate_control_simple_alloc_sta(void *priv
, gfp_t gfp
)
284 struct sta_rate_control
*rctrl
;
286 rctrl
= kzalloc(sizeof(*rctrl
), gfp
);
292 static void rate_control_simple_free_sta(void *priv
, void *priv_sta
)
294 struct sta_rate_control
*rctrl
= priv_sta
;
298 #ifdef CONFIG_MAC80211_DEBUGFS
300 static int open_file_generic(struct inode
*inode
, struct file
*file
)
302 file
->private_data
= inode
->i_private
;
306 static ssize_t
sta_tx_avg_rate_sum_read(struct file
*file
,
307 char __user
*userbuf
,
308 size_t count
, loff_t
*ppos
)
310 struct sta_rate_control
*srctrl
= file
->private_data
;
313 sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_sum
);
314 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, strlen(buf
));
317 static const struct file_operations sta_tx_avg_rate_sum_ops
= {
318 .read
= sta_tx_avg_rate_sum_read
,
319 .open
= open_file_generic
,
322 static ssize_t
sta_tx_avg_rate_num_read(struct file
*file
,
323 char __user
*userbuf
,
324 size_t count
, loff_t
*ppos
)
326 struct sta_rate_control
*srctrl
= file
->private_data
;
329 sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_num
);
330 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, strlen(buf
));
333 static const struct file_operations sta_tx_avg_rate_num_ops
= {
334 .read
= sta_tx_avg_rate_num_read
,
335 .open
= open_file_generic
,
338 static void rate_control_simple_add_sta_debugfs(void *priv
, void *priv_sta
,
341 struct sta_rate_control
*srctrl
= priv_sta
;
343 srctrl
->tx_avg_rate_num_dentry
=
344 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
345 dir
, srctrl
, &sta_tx_avg_rate_num_ops
);
346 srctrl
->tx_avg_rate_sum_dentry
=
347 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
348 dir
, srctrl
, &sta_tx_avg_rate_sum_ops
);
351 static void rate_control_simple_remove_sta_debugfs(void *priv
, void *priv_sta
)
353 struct sta_rate_control
*srctrl
= priv_sta
;
355 debugfs_remove(srctrl
->tx_avg_rate_sum_dentry
);
356 debugfs_remove(srctrl
->tx_avg_rate_num_dentry
);
360 static struct rate_control_ops mac80211_rcsimple
= {
362 .tx_status
= rate_control_simple_tx_status
,
363 .get_rate
= rate_control_simple_get_rate
,
364 .rate_init
= rate_control_simple_rate_init
,
365 .clear
= rate_control_simple_clear
,
366 .alloc
= rate_control_simple_alloc
,
367 .free
= rate_control_simple_free
,
368 .alloc_sta
= rate_control_simple_alloc_sta
,
369 .free_sta
= rate_control_simple_free_sta
,
370 #ifdef CONFIG_MAC80211_DEBUGFS
371 .add_sta_debugfs
= rate_control_simple_add_sta_debugfs
,
372 .remove_sta_debugfs
= rate_control_simple_remove_sta_debugfs
,
376 MODULE_LICENSE("GPL");
377 MODULE_DESCRIPTION("Simple rate control algorithm");
379 int __init
rc80211_simple_init(void)
381 return ieee80211_rate_control_register(&mac80211_rcsimple
);
384 void rc80211_simple_exit(void)
386 ieee80211_rate_control_unregister(&mac80211_rcsimple
);
389 #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
390 module_init(rc80211_simple_init
);
391 module_exit(rc80211_simple_exit
);