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>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "ieee80211_rate.h"
23 /* This is a minimal implementation of TX rate controlling that can be used
24 * as the default when no improved mechanisms are available. */
26 #define RATE_CONTROL_NUM_DOWN 20
27 #define RATE_CONTROL_NUM_UP 15
29 #define RATE_CONTROL_EMERG_DEC 2
30 #define RATE_CONTROL_INTERVAL (HZ / 20)
31 #define RATE_CONTROL_MIN_TX 10
33 static void rate_control_rate_inc(struct ieee80211_local *local,
36 struct ieee80211_sub_if_data *sdata;
37 struct ieee80211_hw_mode *mode;
41 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
42 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
43 /* forced unicast rate - do not change STA rate */
47 mode = local->oper_hw_mode;
48 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
50 if (i > mode->num_rates)
51 i = mode->num_rates - 2;
53 while (i + 1 < mode->num_rates) {
55 if (sta->supp_rates & BIT(i) &&
56 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
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_hw_mode *mode;
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 mode = local->oper_hw_mode;
79 if (i > mode->num_rates)
84 if (sta->supp_rates & BIT(i) &&
85 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
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->last_ack_rssi[0] = sta->last_ack_rssi[1];
135 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
136 sta->last_ack_rssi[2] = status->ack_signal;
137 sta->tx_num_consecutive_failures = 0;
138 sta->tx_num_mpdu_ok++;
140 sta->tx_retry_count += status->retry_count;
141 sta->tx_num_mpdu_fail += status->retry_count;
143 if (time_after(jiffies,
144 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
145 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
147 srctrl->last_rate_change = jiffies;
149 per_failed = (100 * sta->tx_num_mpdu_fail) /
150 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
151 /* TODO: calculate average per_failed to make adjusting
152 * parameters easier */
154 if (net_ratelimit()) {
155 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
156 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
162 * XXX: Make these configurable once we have an
163 * interface to the rate control algorithms
165 if (per_failed > RATE_CONTROL_NUM_DOWN) {
166 rate_control_rate_dec(local, sta);
167 } else if (per_failed < RATE_CONTROL_NUM_UP) {
168 rate_control_rate_inc(local, sta);
170 srctrl->tx_avg_rate_sum += status->control.rate->rate;
171 srctrl->tx_avg_rate_num++;
172 srctrl->tx_num_failures = 0;
173 srctrl->tx_num_xmit = 0;
174 } else if (sta->tx_num_consecutive_failures >=
175 RATE_CONTROL_EMERG_DEC) {
176 rate_control_rate_dec(local, sta);
179 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
180 srctrl->avg_rate_update = jiffies;
181 if (srctrl->tx_avg_rate_num > 0) {
182 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
183 DECLARE_MAC_BUF(mac);
184 printk(KERN_DEBUG "%s: STA %s Average rate: "
186 dev->name, print_mac(mac, sta->addr),
187 srctrl->tx_avg_rate_sum /
188 srctrl->tx_avg_rate_num,
189 srctrl->tx_avg_rate_sum,
190 srctrl->tx_avg_rate_num);
191 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
192 srctrl->tx_avg_rate_sum = 0;
193 srctrl->tx_avg_rate_num = 0;
202 rate_control_simple_get_rate(void *priv, struct net_device *dev,
203 struct ieee80211_hw_mode *mode,
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 sta_info *sta;
212 sta = sta_info_get(local, hdr->addr1);
215 sel->rate = rate_lowest(local, mode, NULL);
219 rateidx = sta->txrate;
221 if (rateidx >= mode->num_rates)
222 rateidx = mode->num_rates - 1;
226 sel->rate = &mode->rates[rateidx];
230 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
231 struct ieee80211_local *local,
232 struct sta_info *sta)
234 struct ieee80211_hw_mode *mode;
237 mode = local->oper_hw_mode;
238 /* TODO: This routine should consider using RSSI from previous packets
239 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
240 * Until that method is implemented, we will use the lowest supported rate
241 * as a workaround, */
242 for (i = 0; i < mode->num_rates; i++) {
243 if ((sta->supp_rates & BIT(i)) &&
244 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
252 static void * rate_control_simple_alloc(struct ieee80211_local *local)
254 struct global_rate_control *rctrl;
256 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
262 static void rate_control_simple_free(void *priv)
264 struct global_rate_control *rctrl = priv;
269 static void rate_control_simple_clear(void *priv)
274 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
276 struct sta_rate_control *rctrl;
278 rctrl = kzalloc(sizeof(*rctrl), gfp);
284 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
286 struct sta_rate_control *rctrl = priv_sta;
290 #ifdef CONFIG_MAC80211_DEBUGFS
292 static int open_file_generic(struct inode *inode, struct file *file)
294 file->private_data = inode->i_private;
298 static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
299 char __user *userbuf,
300 size_t count, loff_t *ppos)
302 struct sta_rate_control *srctrl = file->private_data;
305 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
306 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
309 static const struct file_operations sta_tx_avg_rate_sum_ops = {
310 .read = sta_tx_avg_rate_sum_read,
311 .open = open_file_generic,
314 static ssize_t sta_tx_avg_rate_num_read(struct file *file,
315 char __user *userbuf,
316 size_t count, loff_t *ppos)
318 struct sta_rate_control *srctrl = file->private_data;
321 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
322 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
325 static const struct file_operations sta_tx_avg_rate_num_ops = {
326 .read = sta_tx_avg_rate_num_read,
327 .open = open_file_generic,
330 static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
333 struct sta_rate_control *srctrl = priv_sta;
335 srctrl->tx_avg_rate_num_dentry =
336 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
337 dir, srctrl, &sta_tx_avg_rate_num_ops);
338 srctrl->tx_avg_rate_sum_dentry =
339 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
340 dir, srctrl, &sta_tx_avg_rate_sum_ops);
343 static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
345 struct sta_rate_control *srctrl = priv_sta;
347 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
348 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
352 struct rate_control_ops mac80211_rcsimple = {
354 .tx_status = rate_control_simple_tx_status,
355 .get_rate = rate_control_simple_get_rate,
356 .rate_init = rate_control_simple_rate_init,
357 .clear = rate_control_simple_clear,
358 .alloc = rate_control_simple_alloc,
359 .free = rate_control_simple_free,
360 .alloc_sta = rate_control_simple_alloc_sta,
361 .free_sta = rate_control_simple_free_sta,
362 #ifdef CONFIG_MAC80211_DEBUGFS
363 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
364 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,