add support for if{down,up} -a and implement proper start/stop/restart for /etc/init...
[openwrt/staging/florian.git] / package / d80211 / src / rc80211_simple.c
1 /*
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
10 #include <linux/module.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
18 #include <net/d80211.h>
19 #include "ieee80211_i.h"
20 #include "ieee80211_rate.h"
21
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
26
27 #define RATE_CONTROL_EMERG_DEC 2
28 #define RATE_CONTROL_INTERVAL (HZ / 20)
29 #define RATE_CONTROL_MIN_TX 10
30
31 MODULE_ALIAS("rc80211_default");
32
33 static void rate_control_rate_inc(struct ieee80211_local *local,
34 struct sta_info *sta)
35 {
36 struct ieee80211_sub_if_data *sdata;
37 int i = sta->txrate;
38 int maxrate;
39
40 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
41 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
42 /* forced unicast rate - do not change STA rate */
43 return;
44 }
45
46 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
47
48 if (i > local->num_curr_rates)
49 i = local->num_curr_rates - 2;
50
51 while (i + 1 < local->num_curr_rates) {
52 i++;
53 if (sta->supp_rates & BIT(i) &&
54 local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED &&
55 (maxrate < 0 || i <= maxrate)) {
56 sta->txrate = i;
57 break;
58 }
59 }
60 }
61
62
63 static void rate_control_rate_dec(struct ieee80211_local *local,
64 struct sta_info *sta)
65 {
66 struct ieee80211_sub_if_data *sdata;
67 int i = sta->txrate;
68
69 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
70 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
71 /* forced unicast rate - do not change STA rate */
72 return;
73 }
74
75 if (i > local->num_curr_rates)
76 i = local->num_curr_rates;
77
78 while (i > 0) {
79 i--;
80 if (sta->supp_rates & BIT(i) &&
81 local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED) {
82 sta->txrate = i;
83 break;
84 }
85 }
86 }
87
88
89 static struct ieee80211_rate *
90 rate_control_lowest_rate(struct ieee80211_local *local)
91 {
92 int i;
93
94 for (i = 0; i < local->num_curr_rates; i++) {
95 struct ieee80211_rate *rate = &local->curr_rates[i];
96
97 if (rate->flags & IEEE80211_RATE_SUPPORTED
98 )
99 return rate;
100 }
101
102 printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
103 "found\n");
104 return &local->curr_rates[0];
105 }
106
107
108 struct global_rate_control {
109 int dummy;
110 };
111
112 struct sta_rate_control {
113 unsigned long last_rate_change;
114 u32 tx_num_failures;
115 u32 tx_num_xmit;
116
117 unsigned long avg_rate_update;
118 u32 tx_avg_rate_sum;
119 u32 tx_avg_rate_num;
120 };
121
122
123 static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
124 struct sk_buff *skb,
125 struct ieee80211_tx_status *status)
126 {
127 struct ieee80211_local *local = dev->ieee80211_ptr;
128 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
129 struct sta_info *sta;
130 struct sta_rate_control *srctrl;
131
132 sta = sta_info_get(local, hdr->addr1);
133
134 if (!sta)
135 return;
136
137 srctrl = sta->rate_ctrl_priv;
138 srctrl->tx_num_xmit++;
139 if (status->excessive_retries) {
140 sta->antenna_sel = sta->antenna_sel == 1 ? 2 : 1;
141 if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
142 printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d "
143 "(@%lu)\n",
144 dev->name, MAC_ARG(hdr->addr1),
145 sta->antenna_sel, jiffies);
146 }
147 srctrl->tx_num_failures++;
148 sta->tx_retry_failed++;
149 sta->tx_num_consecutive_failures++;
150 sta->tx_num_mpdu_fail++;
151 } else {
152 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
153 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
154 sta->last_ack_rssi[2] = status->ack_signal;
155 sta->tx_num_consecutive_failures = 0;
156 sta->tx_num_mpdu_ok++;
157 }
158 sta->tx_retry_count += status->retry_count;
159 sta->tx_num_mpdu_fail += status->retry_count;
160
161 if (time_after(jiffies,
162 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
163 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
164 u32 per_failed;
165 srctrl->last_rate_change = jiffies;
166
167 per_failed = (100 * sta->tx_num_mpdu_fail) /
168 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
169 /* TODO: calculate average per_failed to make adjusting
170 * parameters easier */
171 #if 0
172 if (net_ratelimit()) {
173 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
174 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
175 per_failed);
176 }
177 #endif
178
179 if (per_failed > local->rate_ctrl_num_down) {
180 rate_control_rate_dec(local, sta);
181 } else if (per_failed < local->rate_ctrl_num_up) {
182 rate_control_rate_inc(local, sta);
183 }
184 srctrl->tx_avg_rate_sum += local->curr_rates[sta->txrate].rate;
185 srctrl->tx_avg_rate_num++;
186 srctrl->tx_num_failures = 0;
187 srctrl->tx_num_xmit = 0;
188 } else if (sta->tx_num_consecutive_failures >=
189 RATE_CONTROL_EMERG_DEC) {
190 rate_control_rate_dec(local, sta);
191 }
192
193 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
194 srctrl->avg_rate_update = jiffies;
195 if (srctrl->tx_avg_rate_num > 0) {
196 #ifdef CONFIG_D80211_VERBOSE_DEBUG
197 printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
198 "%d (%d/%d)\n",
199 dev->name, MAC_ARG(sta->addr),
200 srctrl->tx_avg_rate_sum /
201 srctrl->tx_avg_rate_num,
202 srctrl->tx_avg_rate_sum,
203 srctrl->tx_avg_rate_num);
204 #endif /* CONFIG_D80211_VERBOSE_DEBUG */
205 srctrl->tx_avg_rate_sum = 0;
206 srctrl->tx_avg_rate_num = 0;
207 }
208 }
209
210 sta_info_put(sta);
211 }
212
213
214 static struct ieee80211_rate *
215 rate_control_simple_get_rate(void *priv, struct net_device *dev,
216 struct sk_buff *skb,
217 struct rate_control_extra *extra)
218 {
219 struct ieee80211_local *local = dev->ieee80211_ptr;
220 struct ieee80211_sub_if_data *sdata;
221 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
222 struct sta_info *sta;
223 int rateidx, nonerp_idx;
224 u16 fc;
225
226 memset(extra, 0, sizeof(*extra));
227
228 fc = le16_to_cpu(hdr->frame_control);
229 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
230 (hdr->addr1[0] & 0x01)) {
231 /* Send management frames and broadcast/multicast data using
232 * lowest rate. */
233 /* TODO: this could probably be improved.. */
234 return rate_control_lowest_rate(local);
235 }
236
237 sta = sta_info_get(local, hdr->addr1);
238
239 if (!sta)
240 return rate_control_lowest_rate(local);
241
242 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
243 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
244 sta->txrate = sdata->bss->force_unicast_rateidx;
245
246 rateidx = sta->txrate;
247
248 if (rateidx >= local->num_curr_rates)
249 rateidx = local->num_curr_rates - 1;
250
251 sta->last_txrate = rateidx;
252 nonerp_idx = rateidx;
253 while (nonerp_idx > 0 &&
254 ((local->curr_rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
255 !(local->curr_rates[nonerp_idx].flags &
256 IEEE80211_RATE_SUPPORTED) ||
257 !(sta->supp_rates & BIT(nonerp_idx))))
258 nonerp_idx--;
259 extra->nonerp_idx = nonerp_idx;
260 extra->nonerp = &local->curr_rates[extra->nonerp_idx];
261
262 sta_info_put(sta);
263
264 return &local->curr_rates[rateidx];
265 }
266
267
268 static void rate_control_simple_rate_init(void *priv, void *priv_sta,
269 struct ieee80211_local *local,
270 struct sta_info *sta)
271 {
272 int i;
273 sta->txrate = 0;
274 /* TODO: what is a good starting rate for STA? About middle? Maybe not
275 * the lowest or the highest rate.. Could consider using RSSI from
276 * previous packets? Need to have IEEE 802.1X auth succeed immediately
277 * after assoc.. */
278 for (i = 0; i < local->num_curr_rates; i++) {
279 if ((sta->supp_rates & BIT(i)) &&
280 (local->curr_rates[i].flags & IEEE80211_RATE_SUPPORTED))
281 sta->txrate = i;
282 }
283 }
284
285
286 static void * rate_control_simple_alloc(struct ieee80211_local *local)
287 {
288 struct global_rate_control *rctrl;
289
290 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
291
292 return rctrl;
293 }
294
295
296 static void rate_control_simple_free(void *priv)
297 {
298 struct global_rate_control *rctrl = priv;
299 kfree(rctrl);
300 }
301
302
303 static void rate_control_simple_clear(void *priv)
304 {
305 }
306
307
308 static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
309 {
310 struct sta_rate_control *rctrl;
311
312 rctrl = kzalloc(sizeof(*rctrl), gfp);
313
314 return rctrl;
315 }
316
317
318 static void rate_control_simple_free_sta(void *priv, void *priv_sta)
319 {
320 struct sta_rate_control *rctrl = priv_sta;
321 kfree(rctrl);
322 }
323
324 static ssize_t show_sta_tx_avg_rate_sum(const struct sta_info *sta, char *buf)
325 {
326 struct sta_rate_control *srctrl = sta->rate_ctrl_priv;
327
328 return sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
329 }
330
331 static ssize_t show_sta_tx_avg_rate_num(const struct sta_info *sta, char *buf)
332 {
333 struct sta_rate_control *srctrl = sta->rate_ctrl_priv;
334
335 return sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
336 }
337
338 static struct sta_attribute sta_attr_tx_avg_rate_sum =
339 __ATTR(tx_avg_rate_sum, S_IRUSR, show_sta_tx_avg_rate_sum, NULL);
340 static struct sta_attribute sta_attr_tx_avg_rate_num =
341 __ATTR(tx_avg_rate_num, S_IRUSR, show_sta_tx_avg_rate_num, NULL);
342
343 static struct attribute *rate_control_simple_sta_attrs[] = {
344 &sta_attr_tx_avg_rate_sum.attr,
345 &sta_attr_tx_avg_rate_num.attr,
346 NULL,
347 };
348
349 static struct attribute_group rate_control_simple_sta_group = {
350 .name = "rate_control_simple",
351 .attrs = rate_control_simple_sta_attrs,
352 };
353
354 static int rate_control_simple_add_sta_attrs(void *priv, void *priv_sta,
355 struct kobject *kobj)
356 {
357 return sysfs_create_group(kobj, &rate_control_simple_sta_group);
358 }
359
360 static void rate_control_simple_remove_sta_attrs(void *priv, void *priv_sta,
361 struct kobject *kobj)
362 {
363 sysfs_remove_group(kobj, &rate_control_simple_sta_group);
364 }
365
366 static struct rate_control_ops rate_control_simple = {
367 .module = THIS_MODULE,
368 .name = "simple",
369 .tx_status = rate_control_simple_tx_status,
370 .get_rate = rate_control_simple_get_rate,
371 .rate_init = rate_control_simple_rate_init,
372 .clear = rate_control_simple_clear,
373 .alloc = rate_control_simple_alloc,
374 .free = rate_control_simple_free,
375 .alloc_sta = rate_control_simple_alloc_sta,
376 .free_sta = rate_control_simple_free_sta,
377 .add_sta_attrs = rate_control_simple_add_sta_attrs,
378 .remove_sta_attrs = rate_control_simple_remove_sta_attrs,
379 };
380
381
382 static int __init rate_control_simple_init(void)
383 {
384 return ieee80211_rate_control_register(&rate_control_simple);
385 }
386
387
388 static void __exit rate_control_simple_exit(void)
389 {
390 ieee80211_rate_control_unregister(&rate_control_simple);
391 }
392
393
394 module_init(rate_control_simple_init);
395 module_exit(rate_control_simple_exit);
396
397 MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
398 MODULE_LICENSE("GPL");