blob: 9a78b116acffbdca0b00ba2df579ce87ec6cd2f2 [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
Jiri Bencf0706e82007-05-05 11:45:53 -070010#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>
Johannes Berg4b475892008-01-02 15:17:03 +010016#include <linux/module.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017
18#include <net/mac80211.h>
19#include "ieee80211_i.h"
20#include "ieee80211_rate.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070021#include "debugfs.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070022
23
24/* This is a minimal implementation of TX rate controlling that can be used
25 * as the default when no improved mechanisms are available. */
26
Mattias Nissler1abbe492007-12-20 13:50:07 +010027#define RATE_CONTROL_NUM_DOWN 20
28#define RATE_CONTROL_NUM_UP 15
Jiri Bencf0706e82007-05-05 11:45:53 -070029
30#define RATE_CONTROL_EMERG_DEC 2
31#define RATE_CONTROL_INTERVAL (HZ / 20)
32#define RATE_CONTROL_MIN_TX 10
33
Jiri Bencf0706e82007-05-05 11:45:53 -070034static void rate_control_rate_inc(struct ieee80211_local *local,
35 struct sta_info *sta)
36{
37 struct ieee80211_sub_if_data *sdata;
38 struct ieee80211_hw_mode *mode;
39 int i = sta->txrate;
40 int maxrate;
41
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;
46 }
47
48 mode = local->oper_hw_mode;
49 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
50
51 if (i > mode->num_rates)
52 i = mode->num_rates - 2;
53
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;
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;
70 struct ieee80211_hw_mode *mode;
71 int i = sta->txrate;
72
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
79 mode = local->oper_hw_mode;
80 if (i > mode->num_rates)
81 i = mode->num_rates;
82
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;
89 }
90 }
91}
92
Jiri Bencf0706e82007-05-05 11:45:53 -070093struct global_rate_control {
94 int dummy;
95};
96
97struct sta_rate_control {
98 unsigned long last_rate_change;
99 u32 tx_num_failures;
100 u32 tx_num_xmit;
101
102 unsigned long avg_rate_update;
103 u32 tx_avg_rate_sum;
104 u32 tx_avg_rate_num;
Jiri Bence9f207f2007-05-05 11:46:38 -0700105
106#ifdef CONFIG_MAC80211_DEBUGFS
107 struct dentry *tx_avg_rate_sum_dentry;
108 struct dentry *tx_avg_rate_num_dentry;
109#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700110};
111
112
113static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
114 struct sk_buff *skb,
115 struct ieee80211_tx_status *status)
116{
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;
121
122 sta = sta_info_get(local, hdr->addr1);
123
124 if (!sta)
125 return;
126
127 srctrl = sta->rate_ctrl_priv;
128 srctrl->tx_num_xmit++;
129 if (status->excessive_retries) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700130 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++;
140 }
141 sta->tx_retry_count += status->retry_count;
142 sta->tx_num_mpdu_fail += status->retry_count;
143
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;
149
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);
159 }
160#endif
161
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200162 /*
163 * XXX: Make these configurable once we have an
164 * interface to the rate control algorithms
165 */
166 if (per_failed > RATE_CONTROL_NUM_DOWN) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700167 rate_control_rate_dec(local, sta);
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200168 } else if (per_failed < RATE_CONTROL_NUM_UP) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700169 rate_control_rate_inc(local, sta);
170 }
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);
178 }
179
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
Joe Perches0795af52007-10-03 17:59:30 -0700184 DECLARE_MAC_BUF(mac);
185 printk(KERN_DEBUG "%s: STA %s Average rate: "
Jiri Bencf0706e82007-05-05 11:45:53 -0700186 "%d (%d/%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700187 dev->name, print_mac(mac, sta->addr),
Jiri Bencf0706e82007-05-05 11:45:53 -0700188 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;
195 }
196 }
197
198 sta_info_put(sta);
199}
200
201
Mattias Nissler1abbe492007-12-20 13:50:07 +0100202static void
Jiri Bencf0706e82007-05-05 11:45:53 -0700203rate_control_simple_get_rate(void *priv, struct net_device *dev,
Mattias Nissler1abbe492007-12-20 13:50:07 +0100204 struct ieee80211_hw_mode *mode,
Jiri Bencf0706e82007-05-05 11:45:53 -0700205 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +0100206 struct rate_selection *sel)
Jiri Bencf0706e82007-05-05 11:45:53 -0700207{
208 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michael Wu2bc454b2007-12-25 19:33:16 -0500210 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700211 struct sta_info *sta;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100212 int rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500213 u16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -0700214
215 sta = sta_info_get(local, hdr->addr1);
216
Michael Wu2bc454b2007-12-25 19:33:16 -0500217 /* Send management frames and broadcast/multicast data using lowest
218 * rate. */
219 fc = le16_to_cpu(hdr->frame_control);
220 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
221 is_multicast_ether_addr(hdr->addr1) || !sta) {
222 sel->rate = rate_lowest(local, mode, sta);
223 if (sta)
224 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100225 return;
226 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700227
Michael Wu2bc454b2007-12-25 19:33:16 -0500228 /* If a forced rate is in effect, select it. */
229 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
230 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
231 sta->txrate = sdata->bss->force_unicast_rateidx;
232
Jiri Bencf0706e82007-05-05 11:45:53 -0700233 rateidx = sta->txrate;
234
235 if (rateidx >= mode->num_rates)
236 rateidx = mode->num_rates - 1;
237
Michael Wu2bc454b2007-12-25 19:33:16 -0500238 sta->last_txrate = rateidx;
239
Jiri Bencf0706e82007-05-05 11:45:53 -0700240 sta_info_put(sta);
241
Mattias Nissler1abbe492007-12-20 13:50:07 +0100242 sel->rate = &mode->rates[rateidx];
Jiri Bencf0706e82007-05-05 11:45:53 -0700243}
244
245
246static void rate_control_simple_rate_init(void *priv, void *priv_sta,
247 struct ieee80211_local *local,
248 struct sta_info *sta)
249{
250 struct ieee80211_hw_mode *mode;
251 int i;
252 sta->txrate = 0;
253 mode = local->oper_hw_mode;
Larry Fingereef6caf2007-07-02 22:36:38 -0700254 /* TODO: This routine should consider using RSSI from previous packets
255 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
256 * Until that method is implemented, we will use the lowest supported rate
257 * as a workaround, */
Jiri Bencf0706e82007-05-05 11:45:53 -0700258 for (i = 0; i < mode->num_rates; i++) {
259 if ((sta->supp_rates & BIT(i)) &&
Larry Fingereef6caf2007-07-02 22:36:38 -0700260 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700261 sta->txrate = i;
Larry Fingereef6caf2007-07-02 22:36:38 -0700262 break;
263 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700264 }
265}
266
267
268static void * rate_control_simple_alloc(struct ieee80211_local *local)
269{
270 struct global_rate_control *rctrl;
271
272 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
273
274 return rctrl;
275}
276
277
278static void rate_control_simple_free(void *priv)
279{
280 struct global_rate_control *rctrl = priv;
281 kfree(rctrl);
282}
283
284
285static void rate_control_simple_clear(void *priv)
286{
287}
288
289
290static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
291{
292 struct sta_rate_control *rctrl;
293
294 rctrl = kzalloc(sizeof(*rctrl), gfp);
295
296 return rctrl;
297}
298
299
300static void rate_control_simple_free_sta(void *priv, void *priv_sta)
301{
302 struct sta_rate_control *rctrl = priv_sta;
303 kfree(rctrl);
304}
305
Jiri Bence9f207f2007-05-05 11:46:38 -0700306#ifdef CONFIG_MAC80211_DEBUGFS
307
308static int open_file_generic(struct inode *inode, struct file *file)
309{
310 file->private_data = inode->i_private;
311 return 0;
312}
313
314static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
315 char __user *userbuf,
316 size_t count, loff_t *ppos)
317{
318 struct sta_rate_control *srctrl = file->private_data;
319 char buf[20];
320
321 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
322 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
323}
324
325static const struct file_operations sta_tx_avg_rate_sum_ops = {
326 .read = sta_tx_avg_rate_sum_read,
327 .open = open_file_generic,
328};
329
330static ssize_t sta_tx_avg_rate_num_read(struct file *file,
331 char __user *userbuf,
332 size_t count, loff_t *ppos)
333{
334 struct sta_rate_control *srctrl = file->private_data;
335 char buf[20];
336
337 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
338 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
339}
340
341static const struct file_operations sta_tx_avg_rate_num_ops = {
342 .read = sta_tx_avg_rate_num_read,
343 .open = open_file_generic,
344};
345
346static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
347 struct dentry *dir)
348{
349 struct sta_rate_control *srctrl = priv_sta;
350
351 srctrl->tx_avg_rate_num_dentry =
352 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
353 dir, srctrl, &sta_tx_avg_rate_num_ops);
354 srctrl->tx_avg_rate_sum_dentry =
355 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
356 dir, srctrl, &sta_tx_avg_rate_sum_ops);
357}
358
359static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
360{
361 struct sta_rate_control *srctrl = priv_sta;
362
363 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
364 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
365}
366#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700367
Johannes Berg4b475892008-01-02 15:17:03 +0100368static struct rate_control_ops mac80211_rcsimple = {
Jiri Bencf0706e82007-05-05 11:45:53 -0700369 .name = "simple",
370 .tx_status = rate_control_simple_tx_status,
371 .get_rate = rate_control_simple_get_rate,
372 .rate_init = rate_control_simple_rate_init,
373 .clear = rate_control_simple_clear,
374 .alloc = rate_control_simple_alloc,
375 .free = rate_control_simple_free,
376 .alloc_sta = rate_control_simple_alloc_sta,
377 .free_sta = rate_control_simple_free_sta,
Jiri Bence9f207f2007-05-05 11:46:38 -0700378#ifdef CONFIG_MAC80211_DEBUGFS
379 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
380 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
381#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700382};
Johannes Berg4b475892008-01-02 15:17:03 +0100383
384MODULE_LICENSE("GPL");
385MODULE_DESCRIPTION("Simple rate control algorithm");
386
387int __init rc80211_simple_init(void)
388{
389 return ieee80211_rate_control_register(&mac80211_rcsimple);
390}
391
Johannes Bergf0b92052008-01-31 19:31:57 +0100392void rc80211_simple_exit(void)
Johannes Berg4b475892008-01-02 15:17:03 +0100393{
394 ieee80211_rate_control_unregister(&mac80211_rcsimple);
395}
396
397#ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
398module_init(rc80211_simple_init);
399module_exit(rc80211_simple_exit);
400#endif