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"
24 /* This is a minimal implementation of TX rate controlling that can be used
25 * as the default when no improved mechanisms are available. */
28 #define RATE_CONTROL_EMERG_DEC 2
29 #define RATE_CONTROL_INTERVAL (HZ / 20)
30 #define RATE_CONTROL_MIN_TX 10
32 MODULE_ALIAS("rc80211_default");
34 static void rate_control_rate_inc(struct ieee80211_local *local,
37 struct ieee80211_sub_if_data *sdata;
38 struct ieee80211_hw_mode *mode;
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 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) {
56 if (sta->supp_rates & BIT(i) &&
57 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
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_hw_mode *mode;
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 */
79 mode = local->oper_hw_mode;
80 if (i > mode->num_rates)
85 if (sta->supp_rates & BIT(i) &&
86 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
94 static struct ieee80211_rate *
95 rate_control_lowest_rate(struct ieee80211_local *local,
96 struct ieee80211_hw_mode *mode)
100 for (i = 0; i < mode->num_rates; i++) {
101 struct ieee80211_rate *rate = &mode->rates[i];
103 if (rate->flags & IEEE80211_RATE_SUPPORTED)
107 printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
109 return &mode->rates[0];
113 struct global_rate_control {
117 struct sta_rate_control {
118 unsigned long last_rate_change;
122 unsigned long avg_rate_update;
126 #ifdef CONFIG_MAC80211_DEBUGFS
127 struct dentry *tx_avg_rate_sum_dentry;
128 struct dentry *tx_avg_rate_num_dentry;
133 static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
135 struct ieee80211_tx_status *status)
137 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
138 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
139 struct sta_info *sta;
140 struct sta_rate_control *srctrl;
142 sta = sta_info_get(local, hdr->addr1);
147 srctrl = sta->rate_ctrl_priv;
148 srctrl->tx_num_xmit++;
149 if (status->excessive_retries) {
150 sta->antenna_sel_tx = sta->antenna_sel_tx == 1 ? 2 : 1;
151 sta->antenna_sel_rx = sta->antenna_sel_rx == 1 ? 2 : 1;
152 if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
153 printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d "
154 "RX antenna --> %d (@%lu)\n",
155 dev->name, MAC_ARG(hdr->addr1),
156 sta->antenna_sel_tx, sta->antenna_sel_rx, jiffies);
158 srctrl->tx_num_failures++;
159 sta->tx_retry_failed++;
160 sta->tx_num_consecutive_failures++;
161 sta->tx_num_mpdu_fail++;
163 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
164 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
165 sta->last_ack_rssi[2] = status->ack_signal;
166 sta->tx_num_consecutive_failures = 0;
167 sta->tx_num_mpdu_ok++;
169 sta->tx_retry_count += status->retry_count;
170 sta->tx_num_mpdu_fail += status->retry_count;
172 if (time_after(jiffies,
173 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
174 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
176 srctrl->last_rate_change = jiffies;
178 per_failed = (100 * sta->tx_num_mpdu_fail) /
179 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
180 /* TODO: calculate average per_failed to make adjusting
181 * parameters easier */
183 if (net_ratelimit()) {
184 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
185 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
191 * XXX: Make these configurable once we have an
192 * interface to the rate control algorithms
194 if (per_failed > RATE_CONTROL_NUM_DOWN) {
195 rate_control_rate_dec(local, sta);
196 } else if (per_failed < RATE_CONTROL_NUM_UP) {
197 rate_control_rate_inc(local, sta);
199 srctrl->tx_avg_rate_sum += status->control.rate->rate;
200 srctrl->tx_avg_rate_num++;
201 srctrl->tx_num_failures = 0;
202 srctrl->tx_num_xmit = 0;
203 } else if (sta->tx_num_consecutive_failures >=
204 RATE_CONTROL_EMERG_DEC) {
205 rate_control_rate_dec(local, sta);
208 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
209 srctrl->avg_rate_update = jiffies;
210 if (srctrl->tx_avg_rate_num > 0) {
211 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
212 printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
214 dev->name, MAC_ARG(sta->addr),
215 srctrl->tx_avg_rate_sum /
216 srctrl->tx_avg_rate_num,
217 srctrl->tx_avg_rate_sum,
218 srctrl->tx_avg_rate_num);
219 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
220 srctrl->tx_avg_rate_sum = 0;
221 srctrl->tx_avg_rate_num = 0;
229 static struct ieee80211_rate *
230 rate_control_simple_get_rate(void *priv, struct net_device *dev,
232 struct rate_control_extra *extra)
234 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
235 struct ieee80211_sub_if_data *sdata;
236 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
237 struct ieee80211_hw_mode *mode = extra->mode;
238 struct sta_info *sta;
239 int rateidx, nonerp_idx;
242 memset(extra, 0, sizeof(*extra));
244 fc = le16_to_cpu(hdr->frame_control);
245 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
246 (hdr->addr1[0] & 0x01)) {
247 /* Send management frames and broadcast/multicast data using
249 /* TODO: this could probably be improved.. */
250 return rate_control_lowest_rate(local, mode);
253 sta = sta_info_get(local, hdr->addr1);
256 return rate_control_lowest_rate(local, mode);
258 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
259 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
260 sta->txrate = sdata->bss->force_unicast_rateidx;
262 rateidx = sta->txrate;
264 if (rateidx >= mode->num_rates)
265 rateidx = mode->num_rates - 1;
267 sta->last_txrate = rateidx;
268 nonerp_idx = rateidx;
269 while (nonerp_idx > 0 &&
270 ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
271 !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
272 !(sta->supp_rates & BIT(nonerp_idx))))
274 extra->nonerp = &mode->rates[nonerp_idx];
278 return &mode->rates[rateidx];
282 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
283 struct ieee80211_local *local,
284 struct sta_info *sta)
286 struct ieee80211_hw_mode *mode;
289 mode = local->oper_hw_mode;
290 /* TODO: This routine should consider using RSSI from previous packets
291 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
292 * Until that method is implemented, we will use the lowest supported rate
293 * as a workaround, */
294 for (i = 0; i < mode->num_rates; i++) {
295 if ((sta->supp_rates & BIT(i)) &&
296 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
304 static void * rate_control_simple_alloc(struct ieee80211_local *local)
306 struct global_rate_control *rctrl;
308 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
314 static void rate_control_simple_free(void *priv)
316 struct global_rate_control *rctrl = priv;
321 static void rate_control_simple_clear(void *priv)
326 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
328 struct sta_rate_control *rctrl;
330 rctrl = kzalloc(sizeof(*rctrl), gfp);
336 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
338 struct sta_rate_control *rctrl = priv_sta;
342 #ifdef CONFIG_MAC80211_DEBUGFS
344 static int open_file_generic(struct inode *inode, struct file *file)
346 file->private_data = inode->i_private;
350 static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
351 char __user *userbuf,
352 size_t count, loff_t *ppos)
354 struct sta_rate_control *srctrl = file->private_data;
357 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
358 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
361 static const struct file_operations sta_tx_avg_rate_sum_ops = {
362 .read = sta_tx_avg_rate_sum_read,
363 .open = open_file_generic,
366 static ssize_t sta_tx_avg_rate_num_read(struct file *file,
367 char __user *userbuf,
368 size_t count, loff_t *ppos)
370 struct sta_rate_control *srctrl = file->private_data;
373 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
374 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
377 static const struct file_operations sta_tx_avg_rate_num_ops = {
378 .read = sta_tx_avg_rate_num_read,
379 .open = open_file_generic,
382 static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
385 struct sta_rate_control *srctrl = priv_sta;
387 srctrl->tx_avg_rate_num_dentry =
388 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
389 dir, srctrl, &sta_tx_avg_rate_num_ops);
390 srctrl->tx_avg_rate_sum_dentry =
391 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
392 dir, srctrl, &sta_tx_avg_rate_sum_ops);
395 static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
397 struct sta_rate_control *srctrl = priv_sta;
399 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
400 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
404 static struct rate_control_ops rate_control_simple = {
405 .module = THIS_MODULE,
407 .tx_status = rate_control_simple_tx_status,
408 .get_rate = rate_control_simple_get_rate,
409 .rate_init = rate_control_simple_rate_init,
410 .clear = rate_control_simple_clear,
411 .alloc = rate_control_simple_alloc,
412 .free = rate_control_simple_free,
413 .alloc_sta = rate_control_simple_alloc_sta,
414 .free_sta = rate_control_simple_free_sta,
415 #ifdef CONFIG_MAC80211_DEBUGFS
416 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
417 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
422 static int __init rate_control_simple_init(void)
424 return ieee80211_rate_control_register(&rate_control_simple);
428 static void __exit rate_control_simple_exit(void)
430 ieee80211_rate_control_unregister(&rate_control_simple);
434 module_init(rate_control_simple_init);
435 module_exit(rate_control_simple_exit);
437 MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
438 MODULE_LICENSE("GPL");