blob: a669bcb8739f2cc29ba484f88b3ac0f2eb048a8a [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005, Devicescape Software, Inc.
4 *
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 */
9
S.Çağlar Onurab466232008-02-14 17:36:47 +020010#include <linux/jiffies.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070011#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>
Johannes Berg4b475892008-01-02 15:17:03 +010017#include <linux/module.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018
19#include <net/mac80211.h>
20#include "ieee80211_i.h"
21#include "ieee80211_rate.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070022#include "debugfs.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070023
24
25/* This is a minimal implementation of TX rate controlling that can be used
26 * as the default when no improved mechanisms are available. */
27
Mattias Nissler1abbe492007-12-20 13:50:07 +010028#define RATE_CONTROL_NUM_DOWN 20
29#define RATE_CONTROL_NUM_UP 15
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#define RATE_CONTROL_EMERG_DEC 2
32#define RATE_CONTROL_INTERVAL (HZ / 20)
33#define RATE_CONTROL_MIN_TX 10
34
Jiri Bencf0706e82007-05-05 11:45:53 -070035static void rate_control_rate_inc(struct ieee80211_local *local,
36 struct sta_info *sta)
37{
38 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +010039 struct ieee80211_supported_band *sband;
40 int i = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -070041 int maxrate;
42
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 */
46 return;
47 }
48
Johannes Berg8318d782008-01-24 19:38:38 +010049 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -070050 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
51
Johannes Berg8318d782008-01-24 19:38:38 +010052 if (i > sband->n_bitrates)
53 i = sband->n_bitrates - 2;
Jiri Bencf0706e82007-05-05 11:45:53 -070054
Johannes Berg8318d782008-01-24 19:38:38 +010055 while (i + 1 < sband->n_bitrates) {
Jiri Bencf0706e82007-05-05 11:45:53 -070056 i++;
Johannes Berg8318d782008-01-24 19:38:38 +010057 if (rate_supported(sta, sband->band, i) &&
Jiri Bencf0706e82007-05-05 11:45:53 -070058 (maxrate < 0 || i <= maxrate)) {
Johannes Berg8318d782008-01-24 19:38:38 +010059 sta->txrate_idx = i;
Jiri Bencf0706e82007-05-05 11:45:53 -070060 break;
61 }
62 }
63}
64
65
66static void rate_control_rate_dec(struct ieee80211_local *local,
67 struct sta_info *sta)
68{
69 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +010070 struct ieee80211_supported_band *sband;
71 int i = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -070072
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;
77 }
78
Johannes Berg8318d782008-01-24 19:38:38 +010079 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
80 if (i > sband->n_bitrates)
81 i = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -070082
83 while (i > 0) {
84 i--;
Johannes Berg8318d782008-01-24 19:38:38 +010085 if (rate_supported(sta, sband->band, i)) {
86 sta->txrate_idx = i;
Jiri Bencf0706e82007-05-05 11:45:53 -070087 break;
88 }
89 }
90}
91
Jiri Bencf0706e82007-05-05 11:45:53 -070092struct global_rate_control {
93 int dummy;
94};
95
96struct sta_rate_control {
97 unsigned long last_rate_change;
98 u32 tx_num_failures;
99 u32 tx_num_xmit;
100
101 unsigned long avg_rate_update;
102 u32 tx_avg_rate_sum;
103 u32 tx_avg_rate_num;
Jiri Bence9f207f2007-05-05 11:46:38 -0700104
105#ifdef CONFIG_MAC80211_DEBUGFS
106 struct dentry *tx_avg_rate_sum_dentry;
107 struct dentry *tx_avg_rate_num_dentry;
108#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700109};
110
111
112static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
113 struct sk_buff *skb,
114 struct ieee80211_tx_status *status)
115{
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;
120
121 sta = sta_info_get(local, hdr->addr1);
122
123 if (!sta)
124 return;
125
126 srctrl = sta->rate_ctrl_priv;
127 srctrl->tx_num_xmit++;
128 if (status->excessive_retries) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700129 srctrl->tx_num_failures++;
130 sta->tx_retry_failed++;
131 sta->tx_num_consecutive_failures++;
132 sta->tx_num_mpdu_fail++;
133 } else {
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++;
139 }
140 sta->tx_retry_count += status->retry_count;
141 sta->tx_num_mpdu_fail += status->retry_count;
142
143 if (time_after(jiffies,
144 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
145 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
146 u32 per_failed;
147 srctrl->last_rate_change = jiffies;
148
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 */
153#if 0
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,
157 per_failed);
158 }
159#endif
160
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200161 /*
162 * XXX: Make these configurable once we have an
163 * interface to the rate control algorithms
164 */
165 if (per_failed > RATE_CONTROL_NUM_DOWN) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700166 rate_control_rate_dec(local, sta);
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200167 } else if (per_failed < RATE_CONTROL_NUM_UP) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700168 rate_control_rate_inc(local, sta);
169 }
Johannes Berg8318d782008-01-24 19:38:38 +0100170 srctrl->tx_avg_rate_sum += status->control.tx_rate->bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700171 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);
177 }
178
S.Çağlar Onurab466232008-02-14 17:36:47 +0200179 if (time_after(jiffies, srctrl->avg_rate_update + 60 * HZ)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700180 srctrl->avg_rate_update = jiffies;
181 if (srctrl->tx_avg_rate_num > 0) {
182#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700183 DECLARE_MAC_BUF(mac);
184 printk(KERN_DEBUG "%s: STA %s Average rate: "
Jiri Bencf0706e82007-05-05 11:45:53 -0700185 "%d (%d/%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700186 dev->name, print_mac(mac, sta->addr),
Jiri Bencf0706e82007-05-05 11:45:53 -0700187 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;
194 }
195 }
196
197 sta_info_put(sta);
198}
199
200
Mattias Nissler1abbe492007-12-20 13:50:07 +0100201static void
Jiri Bencf0706e82007-05-05 11:45:53 -0700202rate_control_simple_get_rate(void *priv, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +0100203 struct ieee80211_supported_band *sband,
Jiri Bencf0706e82007-05-05 11:45:53 -0700204 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +0100205 struct rate_selection *sel)
Jiri Bencf0706e82007-05-05 11:45:53 -0700206{
207 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michael Wu2bc454b2007-12-25 19:33:16 -0500209 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700210 struct sta_info *sta;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100211 int rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500212 u16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -0700213
214 sta = sta_info_get(local, hdr->addr1);
215
Michael Wu2bc454b2007-12-25 19:33:16 -0500216 /* Send management frames and broadcast/multicast data using lowest
217 * rate. */
218 fc = le16_to_cpu(hdr->frame_control);
219 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
220 is_multicast_ether_addr(hdr->addr1) || !sta) {
Johannes Berg8318d782008-01-24 19:38:38 +0100221 sel->rate = rate_lowest(local, sband, sta);
Michael Wu2bc454b2007-12-25 19:33:16 -0500222 if (sta)
223 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100224 return;
225 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700226
Michael Wu2bc454b2007-12-25 19:33:16 -0500227 /* If a forced rate is in effect, select it. */
228 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
Johannes Berg8318d782008-01-24 19:38:38 +0100230 sta->txrate_idx = sdata->bss->force_unicast_rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500231
Johannes Berg8318d782008-01-24 19:38:38 +0100232 rateidx = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700233
Johannes Berg8318d782008-01-24 19:38:38 +0100234 if (rateidx >= sband->n_bitrates)
235 rateidx = sband->n_bitrates - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700236
Johannes Berg8318d782008-01-24 19:38:38 +0100237 sta->last_txrate_idx = rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500238
Jiri Bencf0706e82007-05-05 11:45:53 -0700239 sta_info_put(sta);
240
Johannes Berg8318d782008-01-24 19:38:38 +0100241 sel->rate = &sband->bitrates[rateidx];
Jiri Bencf0706e82007-05-05 11:45:53 -0700242}
243
244
245static void rate_control_simple_rate_init(void *priv, void *priv_sta,
246 struct ieee80211_local *local,
247 struct sta_info *sta)
248{
Johannes Berg8318d782008-01-24 19:38:38 +0100249 struct ieee80211_supported_band *sband;
250
251 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
252
Larry Fingereef6caf2007-07-02 22:36:38 -0700253 /* TODO: This routine should consider using RSSI from previous packets
254 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
255 * Until that method is implemented, we will use the lowest supported rate
256 * as a workaround, */
Johannes Berg8318d782008-01-24 19:38:38 +0100257 sta->txrate_idx = rate_lowest_index(local, sband, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700258}
259
260
261static void * rate_control_simple_alloc(struct ieee80211_local *local)
262{
263 struct global_rate_control *rctrl;
264
265 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
266
267 return rctrl;
268}
269
270
271static void rate_control_simple_free(void *priv)
272{
273 struct global_rate_control *rctrl = priv;
274 kfree(rctrl);
275}
276
277
278static void rate_control_simple_clear(void *priv)
279{
280}
281
282
283static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
284{
285 struct sta_rate_control *rctrl;
286
287 rctrl = kzalloc(sizeof(*rctrl), gfp);
288
289 return rctrl;
290}
291
292
293static void rate_control_simple_free_sta(void *priv, void *priv_sta)
294{
295 struct sta_rate_control *rctrl = priv_sta;
296 kfree(rctrl);
297}
298
Jiri Bence9f207f2007-05-05 11:46:38 -0700299#ifdef CONFIG_MAC80211_DEBUGFS
300
301static int open_file_generic(struct inode *inode, struct file *file)
302{
303 file->private_data = inode->i_private;
304 return 0;
305}
306
307static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
308 char __user *userbuf,
309 size_t count, loff_t *ppos)
310{
311 struct sta_rate_control *srctrl = file->private_data;
312 char buf[20];
313
314 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
315 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
316}
317
318static const struct file_operations sta_tx_avg_rate_sum_ops = {
319 .read = sta_tx_avg_rate_sum_read,
320 .open = open_file_generic,
321};
322
323static ssize_t sta_tx_avg_rate_num_read(struct file *file,
324 char __user *userbuf,
325 size_t count, loff_t *ppos)
326{
327 struct sta_rate_control *srctrl = file->private_data;
328 char buf[20];
329
330 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
331 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
332}
333
334static const struct file_operations sta_tx_avg_rate_num_ops = {
335 .read = sta_tx_avg_rate_num_read,
336 .open = open_file_generic,
337};
338
339static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
340 struct dentry *dir)
341{
342 struct sta_rate_control *srctrl = priv_sta;
343
344 srctrl->tx_avg_rate_num_dentry =
345 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
346 dir, srctrl, &sta_tx_avg_rate_num_ops);
347 srctrl->tx_avg_rate_sum_dentry =
348 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
349 dir, srctrl, &sta_tx_avg_rate_sum_ops);
350}
351
352static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
353{
354 struct sta_rate_control *srctrl = priv_sta;
355
356 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
357 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
358}
359#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700360
Johannes Berg4b475892008-01-02 15:17:03 +0100361static struct rate_control_ops mac80211_rcsimple = {
Jiri Bencf0706e82007-05-05 11:45:53 -0700362 .name = "simple",
363 .tx_status = rate_control_simple_tx_status,
364 .get_rate = rate_control_simple_get_rate,
365 .rate_init = rate_control_simple_rate_init,
366 .clear = rate_control_simple_clear,
367 .alloc = rate_control_simple_alloc,
368 .free = rate_control_simple_free,
369 .alloc_sta = rate_control_simple_alloc_sta,
370 .free_sta = rate_control_simple_free_sta,
Jiri Bence9f207f2007-05-05 11:46:38 -0700371#ifdef CONFIG_MAC80211_DEBUGFS
372 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
373 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
374#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700375};
Johannes Berg4b475892008-01-02 15:17:03 +0100376
377MODULE_LICENSE("GPL");
378MODULE_DESCRIPTION("Simple rate control algorithm");
379
380int __init rc80211_simple_init(void)
381{
382 return ieee80211_rate_control_register(&mac80211_rcsimple);
383}
384
Johannes Bergf0b92052008-01-31 19:31:57 +0100385void rc80211_simple_exit(void)
Johannes Berg4b475892008-01-02 15:17:03 +0100386{
387 ieee80211_rate_control_unregister(&mac80211_rcsimple);
388}
389
390#ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
391module_init(rc80211_simple_init);
392module_exit(rc80211_simple_exit);
393#endif