blob: c4678905a1421ed0e359e61022d53b7d95a4f74d [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;
Johannes Berg8318d782008-01-24 19:38:38 +010038 struct ieee80211_supported_band *sband;
39 int i = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -070040 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
Johannes Berg8318d782008-01-24 19:38:38 +010048 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -070049 maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
50
Johannes Berg8318d782008-01-24 19:38:38 +010051 if (i > sband->n_bitrates)
52 i = sband->n_bitrates - 2;
Jiri Bencf0706e82007-05-05 11:45:53 -070053
Johannes Berg8318d782008-01-24 19:38:38 +010054 while (i + 1 < sband->n_bitrates) {
Jiri Bencf0706e82007-05-05 11:45:53 -070055 i++;
Johannes Berg8318d782008-01-24 19:38:38 +010056 if (rate_supported(sta, sband->band, i) &&
Jiri Bencf0706e82007-05-05 11:45:53 -070057 (maxrate < 0 || i <= maxrate)) {
Johannes Berg8318d782008-01-24 19:38:38 +010058 sta->txrate_idx = i;
Jiri Bencf0706e82007-05-05 11:45:53 -070059 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;
Johannes Berg8318d782008-01-24 19:38:38 +010069 struct ieee80211_supported_band *sband;
70 int i = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -070071
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
Johannes Berg8318d782008-01-24 19:38:38 +010078 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
79 if (i > sband->n_bitrates)
80 i = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -070081
82 while (i > 0) {
83 i--;
Johannes Berg8318d782008-01-24 19:38:38 +010084 if (rate_supported(sta, sband->band, i)) {
85 sta->txrate_idx = i;
Jiri Bencf0706e82007-05-05 11:45:53 -070086 break;
87 }
88 }
89}
90
Jiri Bencf0706e82007-05-05 11:45:53 -070091struct global_rate_control {
92 int dummy;
93};
94
95struct sta_rate_control {
96 unsigned long last_rate_change;
97 u32 tx_num_failures;
98 u32 tx_num_xmit;
99
100 unsigned long avg_rate_update;
101 u32 tx_avg_rate_sum;
102 u32 tx_avg_rate_num;
Jiri Bence9f207f2007-05-05 11:46:38 -0700103
104#ifdef CONFIG_MAC80211_DEBUGFS
105 struct dentry *tx_avg_rate_sum_dentry;
106 struct dentry *tx_avg_rate_num_dentry;
107#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700108};
109
110
111static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
112 struct sk_buff *skb,
113 struct ieee80211_tx_status *status)
114{
115 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
116 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
117 struct sta_info *sta;
118 struct sta_rate_control *srctrl;
119
120 sta = sta_info_get(local, hdr->addr1);
121
122 if (!sta)
123 return;
124
125 srctrl = sta->rate_ctrl_priv;
126 srctrl->tx_num_xmit++;
127 if (status->excessive_retries) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700128 srctrl->tx_num_failures++;
129 sta->tx_retry_failed++;
130 sta->tx_num_consecutive_failures++;
131 sta->tx_num_mpdu_fail++;
132 } else {
133 sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
134 sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
135 sta->last_ack_rssi[2] = status->ack_signal;
136 sta->tx_num_consecutive_failures = 0;
137 sta->tx_num_mpdu_ok++;
138 }
139 sta->tx_retry_count += status->retry_count;
140 sta->tx_num_mpdu_fail += status->retry_count;
141
142 if (time_after(jiffies,
143 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
144 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
145 u32 per_failed;
146 srctrl->last_rate_change = jiffies;
147
148 per_failed = (100 * sta->tx_num_mpdu_fail) /
149 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
150 /* TODO: calculate average per_failed to make adjusting
151 * parameters easier */
152#if 0
153 if (net_ratelimit()) {
154 printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
155 sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
156 per_failed);
157 }
158#endif
159
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200160 /*
161 * XXX: Make these configurable once we have an
162 * interface to the rate control algorithms
163 */
164 if (per_failed > RATE_CONTROL_NUM_DOWN) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700165 rate_control_rate_dec(local, sta);
Johannes Berg3ef8bed2007-07-10 19:32:09 +0200166 } else if (per_failed < RATE_CONTROL_NUM_UP) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700167 rate_control_rate_inc(local, sta);
168 }
Johannes Berg8318d782008-01-24 19:38:38 +0100169 srctrl->tx_avg_rate_sum += status->control.tx_rate->bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700170 srctrl->tx_avg_rate_num++;
171 srctrl->tx_num_failures = 0;
172 srctrl->tx_num_xmit = 0;
173 } else if (sta->tx_num_consecutive_failures >=
174 RATE_CONTROL_EMERG_DEC) {
175 rate_control_rate_dec(local, sta);
176 }
177
178 if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
179 srctrl->avg_rate_update = jiffies;
180 if (srctrl->tx_avg_rate_num > 0) {
181#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700182 DECLARE_MAC_BUF(mac);
183 printk(KERN_DEBUG "%s: STA %s Average rate: "
Jiri Bencf0706e82007-05-05 11:45:53 -0700184 "%d (%d/%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700185 dev->name, print_mac(mac, sta->addr),
Jiri Bencf0706e82007-05-05 11:45:53 -0700186 srctrl->tx_avg_rate_sum /
187 srctrl->tx_avg_rate_num,
188 srctrl->tx_avg_rate_sum,
189 srctrl->tx_avg_rate_num);
190#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
191 srctrl->tx_avg_rate_sum = 0;
192 srctrl->tx_avg_rate_num = 0;
193 }
194 }
195
196 sta_info_put(sta);
197}
198
199
Mattias Nissler1abbe492007-12-20 13:50:07 +0100200static void
Jiri Bencf0706e82007-05-05 11:45:53 -0700201rate_control_simple_get_rate(void *priv, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +0100202 struct ieee80211_supported_band *sband,
Jiri Bencf0706e82007-05-05 11:45:53 -0700203 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +0100204 struct rate_selection *sel)
Jiri Bencf0706e82007-05-05 11:45:53 -0700205{
206 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Michael Wu2bc454b2007-12-25 19:33:16 -0500208 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 struct sta_info *sta;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100210 int rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500211 u16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -0700212
213 sta = sta_info_get(local, hdr->addr1);
214
Michael Wu2bc454b2007-12-25 19:33:16 -0500215 /* Send management frames and broadcast/multicast data using lowest
216 * rate. */
217 fc = le16_to_cpu(hdr->frame_control);
218 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
219 is_multicast_ether_addr(hdr->addr1) || !sta) {
Johannes Berg8318d782008-01-24 19:38:38 +0100220 sel->rate = rate_lowest(local, sband, sta);
Michael Wu2bc454b2007-12-25 19:33:16 -0500221 if (sta)
222 sta_info_put(sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100223 return;
224 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700225
Michael Wu2bc454b2007-12-25 19:33:16 -0500226 /* If a forced rate is in effect, select it. */
227 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
228 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
Johannes Berg8318d782008-01-24 19:38:38 +0100229 sta->txrate_idx = sdata->bss->force_unicast_rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500230
Johannes Berg8318d782008-01-24 19:38:38 +0100231 rateidx = sta->txrate_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700232
Johannes Berg8318d782008-01-24 19:38:38 +0100233 if (rateidx >= sband->n_bitrates)
234 rateidx = sband->n_bitrates - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700235
Johannes Berg8318d782008-01-24 19:38:38 +0100236 sta->last_txrate_idx = rateidx;
Michael Wu2bc454b2007-12-25 19:33:16 -0500237
Jiri Bencf0706e82007-05-05 11:45:53 -0700238 sta_info_put(sta);
239
Johannes Berg8318d782008-01-24 19:38:38 +0100240 sel->rate = &sband->bitrates[rateidx];
Jiri Bencf0706e82007-05-05 11:45:53 -0700241}
242
243
244static void rate_control_simple_rate_init(void *priv, void *priv_sta,
245 struct ieee80211_local *local,
246 struct sta_info *sta)
247{
Johannes Berg8318d782008-01-24 19:38:38 +0100248 struct ieee80211_supported_band *sband;
249
250 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
251
Larry Fingereef6caf2007-07-02 22:36:38 -0700252 /* TODO: This routine should consider using RSSI from previous packets
253 * as we need to have IEEE 802.1X auth succeed immediately after assoc..
254 * Until that method is implemented, we will use the lowest supported rate
255 * as a workaround, */
Johannes Berg8318d782008-01-24 19:38:38 +0100256 sta->txrate_idx = rate_lowest_index(local, sband, sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700257}
258
259
260static void * rate_control_simple_alloc(struct ieee80211_local *local)
261{
262 struct global_rate_control *rctrl;
263
264 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
265
266 return rctrl;
267}
268
269
270static void rate_control_simple_free(void *priv)
271{
272 struct global_rate_control *rctrl = priv;
273 kfree(rctrl);
274}
275
276
277static void rate_control_simple_clear(void *priv)
278{
279}
280
281
282static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
283{
284 struct sta_rate_control *rctrl;
285
286 rctrl = kzalloc(sizeof(*rctrl), gfp);
287
288 return rctrl;
289}
290
291
292static void rate_control_simple_free_sta(void *priv, void *priv_sta)
293{
294 struct sta_rate_control *rctrl = priv_sta;
295 kfree(rctrl);
296}
297
Jiri Bence9f207f2007-05-05 11:46:38 -0700298#ifdef CONFIG_MAC80211_DEBUGFS
299
300static int open_file_generic(struct inode *inode, struct file *file)
301{
302 file->private_data = inode->i_private;
303 return 0;
304}
305
306static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
307 char __user *userbuf,
308 size_t count, loff_t *ppos)
309{
310 struct sta_rate_control *srctrl = file->private_data;
311 char buf[20];
312
313 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
314 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
315}
316
317static const struct file_operations sta_tx_avg_rate_sum_ops = {
318 .read = sta_tx_avg_rate_sum_read,
319 .open = open_file_generic,
320};
321
322static ssize_t sta_tx_avg_rate_num_read(struct file *file,
323 char __user *userbuf,
324 size_t count, loff_t *ppos)
325{
326 struct sta_rate_control *srctrl = file->private_data;
327 char buf[20];
328
329 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
330 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
331}
332
333static const struct file_operations sta_tx_avg_rate_num_ops = {
334 .read = sta_tx_avg_rate_num_read,
335 .open = open_file_generic,
336};
337
338static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
339 struct dentry *dir)
340{
341 struct sta_rate_control *srctrl = priv_sta;
342
343 srctrl->tx_avg_rate_num_dentry =
344 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
345 dir, srctrl, &sta_tx_avg_rate_num_ops);
346 srctrl->tx_avg_rate_sum_dentry =
347 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
348 dir, srctrl, &sta_tx_avg_rate_sum_ops);
349}
350
351static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
352{
353 struct sta_rate_control *srctrl = priv_sta;
354
355 debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
356 debugfs_remove(srctrl->tx_avg_rate_num_dentry);
357}
358#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700359
Johannes Berg4b475892008-01-02 15:17:03 +0100360static struct rate_control_ops mac80211_rcsimple = {
Jiri Bencf0706e82007-05-05 11:45:53 -0700361 .name = "simple",
362 .tx_status = rate_control_simple_tx_status,
363 .get_rate = rate_control_simple_get_rate,
364 .rate_init = rate_control_simple_rate_init,
365 .clear = rate_control_simple_clear,
366 .alloc = rate_control_simple_alloc,
367 .free = rate_control_simple_free,
368 .alloc_sta = rate_control_simple_alloc_sta,
369 .free_sta = rate_control_simple_free_sta,
Jiri Bence9f207f2007-05-05 11:46:38 -0700370#ifdef CONFIG_MAC80211_DEBUGFS
371 .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
372 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
373#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700374};
Johannes Berg4b475892008-01-02 15:17:03 +0100375
376MODULE_LICENSE("GPL");
377MODULE_DESCRIPTION("Simple rate control algorithm");
378
379int __init rc80211_simple_init(void)
380{
381 return ieee80211_rate_control_register(&mac80211_rcsimple);
382}
383
Johannes Bergf0b92052008-01-31 19:31:57 +0100384void rc80211_simple_exit(void)
Johannes Berg4b475892008-01-02 15:17:03 +0100385{
386 ieee80211_rate_control_unregister(&mac80211_rcsimple);
387}
388
389#ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
390module_init(rc80211_simple_init);
391module_exit(rc80211_simple_exit);
392#endif