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/jiffies.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>
17 #include <linux/module.h>
19 #include <net/mac80211.h>
20 #include "ieee80211_i.h"
21 #include "ieee80211_rate.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. */
28 #define RATE_CONTROL_NUM_DOWN 20
29 #define RATE_CONTROL_NUM_UP 15
31 #define RATE_CONTROL_EMERG_DEC 2
32 #define RATE_CONTROL_INTERVAL (HZ / 20)
33 #define RATE_CONTROL_MIN_TX 10
35 static void rate_control_rate_inc(struct ieee80211_local
*local
,
38 struct ieee80211_sub_if_data
*sdata
;
39 struct ieee80211_supported_band
*sband
;
40 int i
= sta
->txrate_idx
;
44 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
45 /* forced unicast rate - do not change STA rate */
49 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
50 maxrate
= sdata
->bss
? sdata
->bss
->max_ratectrl_rateidx
: -1;
52 if (i
> sband
->n_bitrates
)
53 i
= sband
->n_bitrates
- 2;
55 while (i
+ 1 < sband
->n_bitrates
) {
57 if (rate_supported(sta
, sband
->band
, i
) &&
58 (maxrate
< 0 || i
<= maxrate
)) {
66 static void rate_control_rate_dec(struct ieee80211_local
*local
,
69 struct ieee80211_sub_if_data
*sdata
;
70 struct ieee80211_supported_band
*sband
;
71 int i
= sta
->txrate_idx
;
74 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1) {
75 /* forced unicast rate - do not change STA rate */
79 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
80 if (i
> sband
->n_bitrates
)
81 i
= sband
->n_bitrates
;
85 if (rate_supported(sta
, sband
->band
, i
)) {
92 struct global_rate_control
{
96 struct sta_rate_control
{
97 unsigned long last_rate_change
;
101 unsigned long avg_rate_update
;
105 #ifdef CONFIG_MAC80211_DEBUGFS
106 struct dentry
*tx_avg_rate_sum_dentry
;
107 struct dentry
*tx_avg_rate_num_dentry
;
112 static void rate_control_simple_tx_status(void *priv
, struct net_device
*dev
,
114 struct ieee80211_tx_status
*status
)
116 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
117 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
118 struct sta_info
*sta
;
119 struct sta_rate_control
*srctrl
;
123 sta
= sta_info_get(local
, hdr
->addr1
);
128 srctrl
= sta
->rate_ctrl_priv
;
129 srctrl
->tx_num_xmit
++;
130 if (status
->excessive_retries
) {
131 srctrl
->tx_num_failures
++;
132 sta
->tx_retry_failed
++;
133 sta
->tx_num_consecutive_failures
++;
134 sta
->tx_num_mpdu_fail
++;
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 (time_after(jiffies
, srctrl
->avg_rate_update
+ 60 * HZ
)) {
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;
202 rate_control_simple_get_rate(void *priv
, struct net_device
*dev
,
203 struct ieee80211_supported_band
*sband
,
205 struct rate_selection
*sel
)
207 struct ieee80211_local
*local
= wdev_priv(dev
->ieee80211_ptr
);
208 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
209 struct ieee80211_sub_if_data
*sdata
;
210 struct sta_info
*sta
;
216 sta
= sta_info_get(local
, hdr
->addr1
);
218 /* Send management frames and broadcast/multicast data using lowest
220 fc
= le16_to_cpu(hdr
->frame_control
);
221 if ((fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
||
222 is_multicast_ether_addr(hdr
->addr1
) || !sta
) {
223 sel
->rate
= rate_lowest(local
, sband
, sta
);
228 /* If a forced rate is in effect, select it. */
229 sdata
= IEEE80211_DEV_TO_SUB_IF(dev
);
230 if (sdata
->bss
&& sdata
->bss
->force_unicast_rateidx
> -1)
231 sta
->txrate_idx
= sdata
->bss
->force_unicast_rateidx
;
233 rateidx
= sta
->txrate_idx
;
235 if (rateidx
>= sband
->n_bitrates
)
236 rateidx
= sband
->n_bitrates
- 1;
238 sta
->last_txrate_idx
= rateidx
;
242 sel
->rate
= &sband
->bitrates
[rateidx
];
246 static void rate_control_simple_rate_init(void *priv
, void *priv_sta
,
247 struct ieee80211_local
*local
,
248 struct sta_info
*sta
)
250 struct ieee80211_supported_band
*sband
;
252 sband
= local
->hw
.wiphy
->bands
[local
->hw
.conf
.channel
->band
];
254 /* TODO: This routine should consider using RSSI from previous packets
255 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
256 * Until that method is implemented, we will use the lowest supported rate
257 * as a workaround, */
258 sta
->txrate_idx
= rate_lowest_index(local
, sband
, sta
);
262 static void * rate_control_simple_alloc(struct ieee80211_local
*local
)
264 struct global_rate_control
*rctrl
;
266 rctrl
= kzalloc(sizeof(*rctrl
), GFP_ATOMIC
);
272 static void rate_control_simple_free(void *priv
)
274 struct global_rate_control
*rctrl
= priv
;
279 static void rate_control_simple_clear(void *priv
)
284 static void * rate_control_simple_alloc_sta(void *priv
, gfp_t gfp
)
286 struct sta_rate_control
*rctrl
;
288 rctrl
= kzalloc(sizeof(*rctrl
), gfp
);
294 static void rate_control_simple_free_sta(void *priv
, void *priv_sta
)
296 struct sta_rate_control
*rctrl
= priv_sta
;
300 #ifdef CONFIG_MAC80211_DEBUGFS
302 static int open_file_generic(struct inode
*inode
, struct file
*file
)
304 file
->private_data
= inode
->i_private
;
308 static ssize_t
sta_tx_avg_rate_sum_read(struct file
*file
,
309 char __user
*userbuf
,
310 size_t count
, loff_t
*ppos
)
312 struct sta_rate_control
*srctrl
= file
->private_data
;
315 sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_sum
);
316 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, strlen(buf
));
319 static const struct file_operations sta_tx_avg_rate_sum_ops
= {
320 .read
= sta_tx_avg_rate_sum_read
,
321 .open
= open_file_generic
,
324 static ssize_t
sta_tx_avg_rate_num_read(struct file
*file
,
325 char __user
*userbuf
,
326 size_t count
, loff_t
*ppos
)
328 struct sta_rate_control
*srctrl
= file
->private_data
;
331 sprintf(buf
, "%d\n", srctrl
->tx_avg_rate_num
);
332 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, strlen(buf
));
335 static const struct file_operations sta_tx_avg_rate_num_ops
= {
336 .read
= sta_tx_avg_rate_num_read
,
337 .open
= open_file_generic
,
340 static void rate_control_simple_add_sta_debugfs(void *priv
, void *priv_sta
,
343 struct sta_rate_control
*srctrl
= priv_sta
;
345 srctrl
->tx_avg_rate_num_dentry
=
346 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
347 dir
, srctrl
, &sta_tx_avg_rate_num_ops
);
348 srctrl
->tx_avg_rate_sum_dentry
=
349 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
350 dir
, srctrl
, &sta_tx_avg_rate_sum_ops
);
353 static void rate_control_simple_remove_sta_debugfs(void *priv
, void *priv_sta
)
355 struct sta_rate_control
*srctrl
= priv_sta
;
357 debugfs_remove(srctrl
->tx_avg_rate_sum_dentry
);
358 debugfs_remove(srctrl
->tx_avg_rate_num_dentry
);
362 static struct rate_control_ops mac80211_rcsimple
= {
364 .tx_status
= rate_control_simple_tx_status
,
365 .get_rate
= rate_control_simple_get_rate
,
366 .rate_init
= rate_control_simple_rate_init
,
367 .clear
= rate_control_simple_clear
,
368 .alloc
= rate_control_simple_alloc
,
369 .free
= rate_control_simple_free
,
370 .alloc_sta
= rate_control_simple_alloc_sta
,
371 .free_sta
= rate_control_simple_free_sta
,
372 #ifdef CONFIG_MAC80211_DEBUGFS
373 .add_sta_debugfs
= rate_control_simple_add_sta_debugfs
,
374 .remove_sta_debugfs
= rate_control_simple_remove_sta_debugfs
,
378 MODULE_LICENSE("GPL");
379 MODULE_DESCRIPTION("Simple rate control algorithm");
381 int __init
rc80211_simple_init(void)
383 return ieee80211_rate_control_register(&mac80211_rcsimple
);
386 void rc80211_simple_exit(void)
388 ieee80211_rate_control_unregister(&mac80211_rcsimple
);
391 #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
392 module_init(rc80211_simple_init
);
393 module_exit(rc80211_simple_exit
);