blob: c1c8b76a56af38c9f59960de2b6aa75b4b25f599 [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>
16
17#include <net/mac80211.h>
18#include "ieee80211_i.h"
19#include "ieee80211_rate.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070020#include "debugfs.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070021
22
23/* This is a minimal implementation of TX rate controlling that can be used
24 * as the default when no improved mechanisms are available. */
25
Mattias Nissler1abbe492007-12-20 13:50:07 +010026#define RATE_CONTROL_NUM_DOWN 20
27#define RATE_CONTROL_NUM_UP 15
Jiri Bencf0706e82007-05-05 11:45:53 -070028
29#define RATE_CONTROL_EMERG_DEC 2
30#define RATE_CONTROL_INTERVAL (HZ / 20)
31#define RATE_CONTROL_MIN_TX 10
32
Jiri Bencf0706e82007-05-05 11:45:53 -070033static void rate_control_rate_inc(struct ieee80211_local *local,
34 struct sta_info *sta)
35{
36 struct ieee80211_sub_if_data *sdata;
37 struct ieee80211_hw_mode *mode;
38 int i = sta->txrate;
39 int maxrate;
40
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 */
44 return;
45 }
46
47 mode = local->oper_hw_mode;
48 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
49
50 if (i > mode->num_rates)
51 i = mode->num_rates - 2;
52
53 while (i + 1 < mode->num_rates) {
54 i++;
55 if (sta->supp_rates & BIT(i) &&
56 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
57 (maxrate < 0 || i <= maxrate)) {
58 sta->txrate = i;
59 break;
60 }
61 }
62}
63
64
65static void rate_control_rate_dec(struct ieee80211_local *local,
66 struct sta_info *sta)
67{
68 struct ieee80211_sub_if_data *sdata;
69 struct ieee80211_hw_mode *mode;
70 int i = sta->txrate;
71
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 */
75 return;
76 }
77
78 mode = local->oper_hw_mode;
79 if (i > mode->num_rates)
80 i = mode->num_rates;
81
82 while (i > 0) {
83 i--;
84 if (sta->supp_rates & BIT(i) &&
85 mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
86 sta->txrate = i;
87 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 }
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);
177 }
178
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
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,
Mattias Nissler1abbe492007-12-20 13:50:07 +0100203 struct ieee80211_hw_mode *mode,
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;
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 struct sta_info *sta;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100210 int rateidx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700211
212 sta = sta_info_get(local, hdr->addr1);
213
Mattias Nissler1abbe492007-12-20 13:50:07 +0100214 if (!sta) {
215 sel->rate = rate_lowest(local, mode, NULL);
216 return;
217 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700218
219 rateidx = sta->txrate;
220
221 if (rateidx >= mode->num_rates)
222 rateidx = mode->num_rates - 1;
223
Jiri Bencf0706e82007-05-05 11:45:53 -0700224 sta_info_put(sta);
225
Mattias Nissler1abbe492007-12-20 13:50:07 +0100226 sel->rate = &mode->rates[rateidx];
Jiri Bencf0706e82007-05-05 11:45:53 -0700227}
228
229
230static void rate_control_simple_rate_init(void *priv, void *priv_sta,
231 struct ieee80211_local *local,
232 struct sta_info *sta)
233{
234 struct ieee80211_hw_mode *mode;
235 int i;
236 sta->txrate = 0;
237 mode = local->oper_hw_mode;
Larry Fingereef6caf2007-07-02 22:36:38 -0700238 /* 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, */
Jiri Bencf0706e82007-05-05 11:45:53 -0700242 for (i = 0; i < mode->num_rates; i++) {
243 if ((sta->supp_rates & BIT(i)) &&
Larry Fingereef6caf2007-07-02 22:36:38 -0700244 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700245 sta->txrate = i;
Larry Fingereef6caf2007-07-02 22:36:38 -0700246 break;
247 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700248 }
249}
250
251
252static void * rate_control_simple_alloc(struct ieee80211_local *local)
253{
254 struct global_rate_control *rctrl;
255
256 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
257
258 return rctrl;
259}
260
261
262static void rate_control_simple_free(void *priv)
263{
264 struct global_rate_control *rctrl = priv;
265 kfree(rctrl);
266}
267
268
269static void rate_control_simple_clear(void *priv)
270{
271}
272
273
274static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
275{
276 struct sta_rate_control *rctrl;
277
278 rctrl = kzalloc(sizeof(*rctrl), gfp);
279
280 return rctrl;
281}
282
283
284static void rate_control_simple_free_sta(void *priv, void *priv_sta)
285{
286 struct sta_rate_control *rctrl = priv_sta;
287 kfree(rctrl);
288}
289
Jiri Bence9f207f2007-05-05 11:46:38 -0700290#ifdef CONFIG_MAC80211_DEBUGFS
291
292static int open_file_generic(struct inode *inode, struct file *file)
293{
294 file->private_data = inode->i_private;
295 return 0;
296}
297
298static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
299 char __user *userbuf,
300 size_t count, loff_t *ppos)
301{
302 struct sta_rate_control *srctrl = file->private_data;
303 char buf[20];
304
305 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
306 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
307}
308
309static const struct file_operations sta_tx_avg_rate_sum_ops = {
310 .read = sta_tx_avg_rate_sum_read,
311 .open = open_file_generic,
312};
313
314static ssize_t sta_tx_avg_rate_num_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_num);
322 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
323}
324
325static const struct file_operations sta_tx_avg_rate_num_ops = {
326 .read = sta_tx_avg_rate_num_read,
327 .open = open_file_generic,
328};
329
330static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
331 struct dentry *dir)
332{
333 struct sta_rate_control *srctrl = priv_sta;
334
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);
341}
342
343static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
344{
345 struct sta_rate_control *srctrl = priv_sta;
346
347 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
348 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
349}
350#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700351
Johannes Bergac71c692007-10-28 14:17:44 +0100352struct rate_control_ops mac80211_rcsimple = {
Jiri Bencf0706e82007-05-05 11:45:53 -0700353 .name = "simple",
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,
Jiri Bence9f207f2007-05-05 11:46:38 -0700362#ifdef CONFIG_MAC80211_DEBUGFS
363 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
364 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
365#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700366};