mac80211: better rate control algorithm selection
[linux-2.6/libata-dev.git] / net / mac80211 / rc80211_simple.c
blob33de6f967e551d991c7247e97d819e416a05d35e
1 /*
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.
8 */
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"
21 #include "debugfs.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,
35 struct sta_info *sta)
37 struct ieee80211_sub_if_data *sdata;
38 struct ieee80211_hw_mode *mode;
39 int i = sta->txrate;
40 int maxrate;
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 */
45 return;
48 mode = local->oper_hw_mode;
49 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
51 if (i > mode->num_rates)
52 i = mode->num_rates - 2;
54 while (i + 1 < mode->num_rates) {
55 i++;
56 if (sta->supp_rates & BIT(i) &&
57 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
58 (maxrate < 0 || i <= maxrate)) {
59 sta->txrate = i;
60 break;
66 static void rate_control_rate_dec(struct ieee80211_local *local,
67 struct sta_info *sta)
69 struct ieee80211_sub_if_data *sdata;
70 struct ieee80211_hw_mode *mode;
71 int i = sta->txrate;
73 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
74 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
75 /* forced unicast rate - do not change STA rate */
76 return;
79 mode = local->oper_hw_mode;
80 if (i > mode->num_rates)
81 i = mode->num_rates;
83 while (i > 0) {
84 i--;
85 if (sta->supp_rates & BIT(i) &&
86 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
87 sta->txrate = i;
88 break;
93 struct global_rate_control {
94 int dummy;
97 struct sta_rate_control {
98 unsigned long last_rate_change;
99 u32 tx_num_failures;
100 u32 tx_num_xmit;
102 unsigned long avg_rate_update;
103 u32 tx_avg_rate_sum;
104 u32 tx_avg_rate_num;
106 #ifdef CONFIG_MAC80211_DEBUGFS
107 struct dentry *tx_avg_rate_sum_dentry;
108 struct dentry *tx_avg_rate_num_dentry;
109 #endif
113 static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
114 struct sk_buff *skb,
115 struct ieee80211_tx_status *status)
117 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
118 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
119 struct sta_info *sta;
120 struct sta_rate_control *srctrl;
122 sta = sta_info_get(local, hdr->addr1);
124 if (!sta)
125 return;
127 srctrl = sta->rate_ctrl_priv;
128 srctrl->tx_num_xmit++;
129 if (status->excessive_retries) {
130 srctrl->tx_num_failures++;
131 sta->tx_retry_failed++;
132 sta->tx_num_consecutive_failures++;
133 sta->tx_num_mpdu_fail++;
134 } else {
135 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
136 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
137 sta->last_ack_rssi[2] = status->ack_signal;
138 sta->tx_num_consecutive_failures = 0;
139 sta->tx_num_mpdu_ok++;
141 sta->tx_retry_count += status->retry_count;
142 sta->tx_num_mpdu_fail += status->retry_count;
144 if (time_after(jiffies,
145 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
146 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
147 u32 per_failed;
148 srctrl->last_rate_change = jiffies;
150 per_failed = (100 * sta->tx_num_mpdu_fail) /
151 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
152 /* TODO: calculate average per_failed to make adjusting
153 * parameters easier */
154 #if 0
155 if (net_ratelimit()) {
156 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
157 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
158 per_failed);
160 #endif
163 * XXX: Make these configurable once we have an
164 * interface to the rate control algorithms
166 if (per_failed > RATE_CONTROL_NUM_DOWN) {
167 rate_control_rate_dec(local, sta);
168 } else if (per_failed < RATE_CONTROL_NUM_UP) {
169 rate_control_rate_inc(local, sta);
171 srctrl->tx_avg_rate_sum += status->control.rate->rate;
172 srctrl->tx_avg_rate_num++;
173 srctrl->tx_num_failures = 0;
174 srctrl->tx_num_xmit = 0;
175 } else if (sta->tx_num_consecutive_failures >=
176 RATE_CONTROL_EMERG_DEC) {
177 rate_control_rate_dec(local, sta);
180 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
181 srctrl->avg_rate_update = jiffies;
182 if (srctrl->tx_avg_rate_num > 0) {
183 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
184 DECLARE_MAC_BUF(mac);
185 printk(KERN_DEBUG "%s: STA %s Average rate: "
186 "%d (%d/%d)\n",
187 dev->name, print_mac(mac, sta->addr),
188 srctrl->tx_avg_rate_sum /
189 srctrl->tx_avg_rate_num,
190 srctrl->tx_avg_rate_sum,
191 srctrl->tx_avg_rate_num);
192 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
193 srctrl->tx_avg_rate_sum = 0;
194 srctrl->tx_avg_rate_num = 0;
198 sta_info_put(sta);
202 static void
203 rate_control_simple_get_rate(void *priv, struct net_device *dev,
204 struct ieee80211_hw_mode *mode,
205 struct sk_buff *skb,
206 struct rate_selection *sel)
208 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
209 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
210 struct sta_info *sta;
211 int rateidx;
213 sta = sta_info_get(local, hdr->addr1);
215 if (!sta) {
216 sel->rate = rate_lowest(local, mode, NULL);
217 return;
220 rateidx = sta->txrate;
222 if (rateidx >= mode->num_rates)
223 rateidx = mode->num_rates - 1;
225 sta_info_put(sta);
227 sel->rate = &mode->rates[rateidx];
231 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
232 struct ieee80211_local *local,
233 struct sta_info *sta)
235 struct ieee80211_hw_mode *mode;
236 int i;
237 sta->txrate = 0;
238 mode = local->oper_hw_mode;
239 /* TODO: This routine should consider using RSSI from previous packets
240 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
241 * Until that method is implemented, we will use the lowest supported rate
242 * as a workaround, */
243 for (i = 0; i < mode->num_rates; i++) {
244 if ((sta->supp_rates & BIT(i)) &&
245 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
246 sta->txrate = i;
247 break;
253 static void * rate_control_simple_alloc(struct ieee80211_local *local)
255 struct global_rate_control *rctrl;
257 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
259 return rctrl;
263 static void rate_control_simple_free(void *priv)
265 struct global_rate_control *rctrl = priv;
266 kfree(rctrl);
270 static void rate_control_simple_clear(void *priv)
275 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
277 struct sta_rate_control *rctrl;
279 rctrl = kzalloc(sizeof(*rctrl), gfp);
281 return rctrl;
285 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
287 struct sta_rate_control *rctrl = priv_sta;
288 kfree(rctrl);
291 #ifdef CONFIG_MAC80211_DEBUGFS
293 static int open_file_generic(struct inode *inode, struct file *file)
295 file->private_data = inode->i_private;
296 return 0;
299 static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
300 char __user *userbuf,
301 size_t count, loff_t *ppos)
303 struct sta_rate_control *srctrl = file->private_data;
304 char buf[20];
306 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
307 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
310 static const struct file_operations sta_tx_avg_rate_sum_ops = {
311 .read = sta_tx_avg_rate_sum_read,
312 .open = open_file_generic,
315 static ssize_t sta_tx_avg_rate_num_read(struct file *file,
316 char __user *userbuf,
317 size_t count, loff_t *ppos)
319 struct sta_rate_control *srctrl = file->private_data;
320 char buf[20];
322 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
323 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
326 static const struct file_operations sta_tx_avg_rate_num_ops = {
327 .read = sta_tx_avg_rate_num_read,
328 .open = open_file_generic,
331 static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
332 struct dentry *dir)
334 struct sta_rate_control *srctrl = priv_sta;
336 srctrl->tx_avg_rate_num_dentry =
337 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
338 dir, srctrl, &sta_tx_avg_rate_num_ops);
339 srctrl->tx_avg_rate_sum_dentry =
340 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
341 dir, srctrl, &sta_tx_avg_rate_sum_ops);
344 static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
346 struct sta_rate_control *srctrl = priv_sta;
348 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
349 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
351 #endif
353 static struct rate_control_ops mac80211_rcsimple = {
354 .name = "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 .alloc = rate_control_simple_alloc,
360 .free = rate_control_simple_free,
361 .alloc_sta = rate_control_simple_alloc_sta,
362 .free_sta = rate_control_simple_free_sta,
363 #ifdef CONFIG_MAC80211_DEBUGFS
364 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
365 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
366 #endif
369 MODULE_LICENSE("GPL");
370 MODULE_DESCRIPTION("Simple rate control algorithm");
372 int __init rc80211_simple_init(void)
374 return ieee80211_rate_control_register(&mac80211_rcsimple);
377 void __exit rc80211_simple_exit(void)
379 ieee80211_rate_control_unregister(&mac80211_rcsimple);
382 #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
383 module_init(rc80211_simple_init);
384 module_exit(rc80211_simple_exit);
385 #endif