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;
43 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
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;
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 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;
121 sta = sta_info_get(local, hdr->addr1);
126 srctrl = sta->rate_ctrl_priv;
127 srctrl->tx_num_xmit++;
128 if (status->excessive_retries) {
129 srctrl->tx_num_failures++;
130 sta->tx_retry_failed++;
131 sta->tx_num_consecutive_failures++;
132 sta->tx_num_mpdu_fail++;
134 sta->tx_num_consecutive_failures = 0;
135 sta->tx_num_mpdu_ok++;
137 sta->tx_retry_count += status->retry_count;
138 sta->tx_num_mpdu_fail += status->retry_count;
140 if (time_after(jiffies,
141 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
142 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
144 srctrl->last_rate_change = jiffies;
146 per_failed = (100 * sta->tx_num_mpdu_fail) /
147 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
148 /* TODO: calculate average per_failed to make adjusting
149 * parameters easier */
151 if (net_ratelimit()) {
152 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
153 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
159 * XXX: Make these configurable once we have an
160 * interface to the rate control algorithms
162 if (per_failed > RATE_CONTROL_NUM_DOWN) {
163 rate_control_rate_dec(local, sta);
164 } else if (per_failed < RATE_CONTROL_NUM_UP) {
165 rate_control_rate_inc(local, sta);
167 srctrl->tx_avg_rate_sum += status->control.tx_rate->bitrate;
168 srctrl->tx_avg_rate_num++;
169 srctrl->tx_num_failures = 0;
170 srctrl->tx_num_xmit = 0;
171 } else if (sta->tx_num_consecutive_failures >=
172 RATE_CONTROL_EMERG_DEC) {
173 rate_control_rate_dec(local, sta);
176 if (time_after(jiffies, srctrl->avg_rate_update + 60 * HZ)) {
177 srctrl->avg_rate_update = jiffies;
178 if (srctrl->tx_avg_rate_num > 0) {
179 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
180 DECLARE_MAC_BUF(mac);
181 printk(KERN_DEBUG "%s: STA %s Average rate: "
183 dev->name, print_mac(mac, sta->addr),
184 srctrl->tx_avg_rate_sum /
185 srctrl->tx_avg_rate_num,
186 srctrl->tx_avg_rate_sum,
187 srctrl->tx_avg_rate_num);
188 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
189 srctrl->tx_avg_rate_sum = 0;
190 srctrl->tx_avg_rate_num = 0;
199 rate_control_simple_get_rate(void *priv, struct net_device *dev,
200 struct ieee80211_supported_band *sband,
202 struct rate_selection *sel)
204 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
205 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
206 struct ieee80211_sub_if_data *sdata;
207 struct sta_info *sta;
211 sta = sta_info_get(local, hdr->addr1);
213 /* Send management frames and broadcast/multicast data using lowest
215 fc = le16_to_cpu(hdr->frame_control);
216 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
217 is_multicast_ether_addr(hdr->addr1) || !sta) {
218 sel->rate = rate_lowest(local, sband, sta);
224 /* If a forced rate is in effect, select it. */
225 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
226 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
227 sta->txrate_idx = sdata->bss->force_unicast_rateidx;
229 rateidx = sta->txrate_idx;
231 if (rateidx >= sband->n_bitrates)
232 rateidx = sband->n_bitrates - 1;
234 sta->last_txrate_idx = rateidx;
238 sel->rate = &sband->bitrates[rateidx];
242 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
243 struct ieee80211_local *local,
244 struct sta_info *sta)
246 struct ieee80211_supported_band *sband;
248 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
250 /* TODO: This routine should consider using RSSI from previous packets
251 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
252 * Until that method is implemented, we will use the lowest supported rate
253 * as a workaround, */
254 sta->txrate_idx = rate_lowest_index(local, sband, sta);
258 static void * rate_control_simple_alloc(struct ieee80211_local *local)
260 struct global_rate_control *rctrl;
262 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
268 static void rate_control_simple_free(void *priv)
270 struct global_rate_control *rctrl = priv;
275 static void rate_control_simple_clear(void *priv)
280 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
282 struct sta_rate_control *rctrl;
284 rctrl = kzalloc(sizeof(*rctrl), gfp);
290 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
292 struct sta_rate_control *rctrl = priv_sta;
296 #ifdef CONFIG_MAC80211_DEBUGFS
298 static int open_file_generic(struct inode *inode, struct file *file)
300 file->private_data = inode->i_private;
304 static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
305 char __user *userbuf,
306 size_t count, loff_t *ppos)
308 struct sta_rate_control *srctrl = file->private_data;
311 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
312 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
315 static const struct file_operations sta_tx_avg_rate_sum_ops = {
316 .read = sta_tx_avg_rate_sum_read,
317 .open = open_file_generic,
320 static ssize_t sta_tx_avg_rate_num_read(struct file *file,
321 char __user *userbuf,
322 size_t count, loff_t *ppos)
324 struct sta_rate_control *srctrl = file->private_data;
327 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
328 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
331 static const struct file_operations sta_tx_avg_rate_num_ops = {
332 .read = sta_tx_avg_rate_num_read,
333 .open = open_file_generic,
336 static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
339 struct sta_rate_control *srctrl = priv_sta;
341 srctrl->tx_avg_rate_num_dentry =
342 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
343 dir, srctrl, &sta_tx_avg_rate_num_ops);
344 srctrl->tx_avg_rate_sum_dentry =
345 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
346 dir, srctrl, &sta_tx_avg_rate_sum_ops);
349 static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
351 struct sta_rate_control *srctrl = priv_sta;
353 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
354 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
358 static struct rate_control_ops mac80211_rcsimple = {
360 .tx_status = rate_control_simple_tx_status,
361 .get_rate = rate_control_simple_get_rate,
362 .rate_init = rate_control_simple_rate_init,
363 .clear = rate_control_simple_clear,
364 .alloc = rate_control_simple_alloc,
365 .free = rate_control_simple_free,
366 .alloc_sta = rate_control_simple_alloc_sta,
367 .free_sta = rate_control_simple_free_sta,
368 #ifdef CONFIG_MAC80211_DEBUGFS
369 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
370 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
374 MODULE_LICENSE("GPL");
375 MODULE_DESCRIPTION("Simple rate control algorithm");
377 int __init rc80211_simple_init(void)
379 return ieee80211_rate_control_register(&mac80211_rcsimple);
382 void rc80211_simple_exit(void)
384 ieee80211_rate_control_unregister(&mac80211_rcsimple);
387 #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
388 module_init(rc80211_simple_init);
389 module_exit(rc80211_simple_exit);