Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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/mac80211.h> |
| 19 | #include "ieee80211_i.h" |
| 20 | #include "ieee80211_rate.h" |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 21 | #include "debugfs.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 22 | |
| 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 | |
| 27 | |
| 28 | #define RATE_CONTROL_EMERG_DEC 2 |
| 29 | #define RATE_CONTROL_INTERVAL (HZ / 20) |
| 30 | #define RATE_CONTROL_MIN_TX 10 |
| 31 | |
| 32 | MODULE_ALIAS("rc80211_default"); |
| 33 | |
| 34 | static 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 | |
| 66 | static 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 | |
| 93 | |
| 94 | static struct ieee80211_rate * |
| 95 | rate_control_lowest_rate(struct ieee80211_local *local, |
| 96 | struct ieee80211_hw_mode *mode) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < mode->num_rates; i++) { |
| 101 | struct ieee80211_rate *rate = &mode->rates[i]; |
| 102 | |
| 103 | if (rate->flags & IEEE80211_RATE_SUPPORTED) |
| 104 | return rate; |
| 105 | } |
| 106 | |
| 107 | printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates " |
| 108 | "found\n"); |
| 109 | return &mode->rates[0]; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | struct global_rate_control { |
| 114 | int dummy; |
| 115 | }; |
| 116 | |
| 117 | struct sta_rate_control { |
| 118 | unsigned long last_rate_change; |
| 119 | u32 tx_num_failures; |
| 120 | u32 tx_num_xmit; |
| 121 | |
| 122 | unsigned long avg_rate_update; |
| 123 | u32 tx_avg_rate_sum; |
| 124 | u32 tx_avg_rate_num; |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 125 | |
| 126 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 127 | struct dentry *tx_avg_rate_sum_dentry; |
| 128 | struct dentry *tx_avg_rate_num_dentry; |
| 129 | #endif |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | |
| 133 | static void rate_control_simple_tx_status(void *priv, struct net_device *dev, |
| 134 | struct sk_buff *skb, |
| 135 | struct ieee80211_tx_status *status) |
| 136 | { |
| 137 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 138 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 139 | struct sta_info *sta; |
| 140 | struct sta_rate_control *srctrl; |
| 141 | |
| 142 | sta = sta_info_get(local, hdr->addr1); |
| 143 | |
| 144 | if (!sta) |
| 145 | return; |
| 146 | |
| 147 | srctrl = sta->rate_ctrl_priv; |
| 148 | srctrl->tx_num_xmit++; |
| 149 | if (status->excessive_retries) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 150 | srctrl->tx_num_failures++; |
| 151 | sta->tx_retry_failed++; |
| 152 | sta->tx_num_consecutive_failures++; |
| 153 | sta->tx_num_mpdu_fail++; |
| 154 | } else { |
| 155 | sta->last_ack_rssi[0] = sta->last_ack_rssi[1]; |
| 156 | sta->last_ack_rssi[1] = sta->last_ack_rssi[2]; |
| 157 | sta->last_ack_rssi[2] = status->ack_signal; |
| 158 | sta->tx_num_consecutive_failures = 0; |
| 159 | sta->tx_num_mpdu_ok++; |
| 160 | } |
| 161 | sta->tx_retry_count += status->retry_count; |
| 162 | sta->tx_num_mpdu_fail += status->retry_count; |
| 163 | |
| 164 | if (time_after(jiffies, |
| 165 | srctrl->last_rate_change + RATE_CONTROL_INTERVAL) && |
| 166 | srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) { |
| 167 | u32 per_failed; |
| 168 | srctrl->last_rate_change = jiffies; |
| 169 | |
| 170 | per_failed = (100 * sta->tx_num_mpdu_fail) / |
| 171 | (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok); |
| 172 | /* TODO: calculate average per_failed to make adjusting |
| 173 | * parameters easier */ |
| 174 | #if 0 |
| 175 | if (net_ratelimit()) { |
| 176 | printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n", |
| 177 | sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok, |
| 178 | per_failed); |
| 179 | } |
| 180 | #endif |
| 181 | |
Johannes Berg | 3ef8bed | 2007-07-10 19:32:09 +0200 | [diff] [blame] | 182 | /* |
| 183 | * XXX: Make these configurable once we have an |
| 184 | * interface to the rate control algorithms |
| 185 | */ |
| 186 | if (per_failed > RATE_CONTROL_NUM_DOWN) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 187 | rate_control_rate_dec(local, sta); |
Johannes Berg | 3ef8bed | 2007-07-10 19:32:09 +0200 | [diff] [blame] | 188 | } else if (per_failed < RATE_CONTROL_NUM_UP) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 189 | rate_control_rate_inc(local, sta); |
| 190 | } |
| 191 | srctrl->tx_avg_rate_sum += status->control.rate->rate; |
| 192 | srctrl->tx_avg_rate_num++; |
| 193 | srctrl->tx_num_failures = 0; |
| 194 | srctrl->tx_num_xmit = 0; |
| 195 | } else if (sta->tx_num_consecutive_failures >= |
| 196 | RATE_CONTROL_EMERG_DEC) { |
| 197 | rate_control_rate_dec(local, sta); |
| 198 | } |
| 199 | |
| 200 | if (srctrl->avg_rate_update + 60 * HZ < jiffies) { |
| 201 | srctrl->avg_rate_update = jiffies; |
| 202 | if (srctrl->tx_avg_rate_num > 0) { |
| 203 | #ifdef CONFIG_MAC80211_VERBOSE_DEBUG |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 204 | DECLARE_MAC_BUF(mac); |
| 205 | printk(KERN_DEBUG "%s: STA %s Average rate: " |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 206 | "%d (%d/%d)\n", |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 207 | dev->name, print_mac(mac, sta->addr), |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 208 | srctrl->tx_avg_rate_sum / |
| 209 | srctrl->tx_avg_rate_num, |
| 210 | srctrl->tx_avg_rate_sum, |
| 211 | srctrl->tx_avg_rate_num); |
| 212 | #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ |
| 213 | srctrl->tx_avg_rate_sum = 0; |
| 214 | srctrl->tx_avg_rate_num = 0; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | sta_info_put(sta); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static struct ieee80211_rate * |
| 223 | rate_control_simple_get_rate(void *priv, struct net_device *dev, |
| 224 | struct sk_buff *skb, |
| 225 | struct rate_control_extra *extra) |
| 226 | { |
| 227 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
| 228 | struct ieee80211_sub_if_data *sdata; |
| 229 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 230 | struct ieee80211_hw_mode *mode = extra->mode; |
| 231 | struct sta_info *sta; |
| 232 | int rateidx, nonerp_idx; |
| 233 | u16 fc; |
| 234 | |
| 235 | memset(extra, 0, sizeof(*extra)); |
| 236 | |
| 237 | fc = le16_to_cpu(hdr->frame_control); |
| 238 | if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || |
| 239 | (hdr->addr1[0] & 0x01)) { |
| 240 | /* Send management frames and broadcast/multicast data using |
| 241 | * lowest rate. */ |
| 242 | /* TODO: this could probably be improved.. */ |
| 243 | return rate_control_lowest_rate(local, mode); |
| 244 | } |
| 245 | |
| 246 | sta = sta_info_get(local, hdr->addr1); |
| 247 | |
| 248 | if (!sta) |
| 249 | return rate_control_lowest_rate(local, mode); |
| 250 | |
| 251 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
| 252 | if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) |
| 253 | sta->txrate = sdata->bss->force_unicast_rateidx; |
| 254 | |
| 255 | rateidx = sta->txrate; |
| 256 | |
| 257 | if (rateidx >= mode->num_rates) |
| 258 | rateidx = mode->num_rates - 1; |
| 259 | |
| 260 | sta->last_txrate = rateidx; |
| 261 | nonerp_idx = rateidx; |
| 262 | while (nonerp_idx > 0 && |
| 263 | ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) || |
| 264 | !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) || |
| 265 | !(sta->supp_rates & BIT(nonerp_idx)))) |
| 266 | nonerp_idx--; |
| 267 | extra->nonerp = &mode->rates[nonerp_idx]; |
| 268 | |
| 269 | sta_info_put(sta); |
| 270 | |
| 271 | return &mode->rates[rateidx]; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static void rate_control_simple_rate_init(void *priv, void *priv_sta, |
| 276 | struct ieee80211_local *local, |
| 277 | struct sta_info *sta) |
| 278 | { |
| 279 | struct ieee80211_hw_mode *mode; |
| 280 | int i; |
| 281 | sta->txrate = 0; |
| 282 | mode = local->oper_hw_mode; |
Larry Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 283 | /* TODO: This routine should consider using RSSI from previous packets |
| 284 | * as we need to have IEEE 802.1X auth succeed immediately after assoc.. |
| 285 | * Until that method is implemented, we will use the lowest supported rate |
| 286 | * as a workaround, */ |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 287 | for (i = 0; i < mode->num_rates; i++) { |
| 288 | if ((sta->supp_rates & BIT(i)) && |
Larry Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 289 | (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 290 | sta->txrate = i; |
Larry Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 291 | break; |
| 292 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static void * rate_control_simple_alloc(struct ieee80211_local *local) |
| 298 | { |
| 299 | struct global_rate_control *rctrl; |
| 300 | |
| 301 | rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC); |
| 302 | |
| 303 | return rctrl; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | static void rate_control_simple_free(void *priv) |
| 308 | { |
| 309 | struct global_rate_control *rctrl = priv; |
| 310 | kfree(rctrl); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static void rate_control_simple_clear(void *priv) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | |
| 319 | static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp) |
| 320 | { |
| 321 | struct sta_rate_control *rctrl; |
| 322 | |
| 323 | rctrl = kzalloc(sizeof(*rctrl), gfp); |
| 324 | |
| 325 | return rctrl; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | static void rate_control_simple_free_sta(void *priv, void *priv_sta) |
| 330 | { |
| 331 | struct sta_rate_control *rctrl = priv_sta; |
| 332 | kfree(rctrl); |
| 333 | } |
| 334 | |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 335 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 336 | |
| 337 | static int open_file_generic(struct inode *inode, struct file *file) |
| 338 | { |
| 339 | file->private_data = inode->i_private; |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static ssize_t sta_tx_avg_rate_sum_read(struct file *file, |
| 344 | char __user *userbuf, |
| 345 | size_t count, loff_t *ppos) |
| 346 | { |
| 347 | struct sta_rate_control *srctrl = file->private_data; |
| 348 | char buf[20]; |
| 349 | |
| 350 | sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum); |
| 351 | return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); |
| 352 | } |
| 353 | |
| 354 | static const struct file_operations sta_tx_avg_rate_sum_ops = { |
| 355 | .read = sta_tx_avg_rate_sum_read, |
| 356 | .open = open_file_generic, |
| 357 | }; |
| 358 | |
| 359 | static ssize_t sta_tx_avg_rate_num_read(struct file *file, |
| 360 | char __user *userbuf, |
| 361 | size_t count, loff_t *ppos) |
| 362 | { |
| 363 | struct sta_rate_control *srctrl = file->private_data; |
| 364 | char buf[20]; |
| 365 | |
| 366 | sprintf(buf, "%d\n", srctrl->tx_avg_rate_num); |
| 367 | return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); |
| 368 | } |
| 369 | |
| 370 | static const struct file_operations sta_tx_avg_rate_num_ops = { |
| 371 | .read = sta_tx_avg_rate_num_read, |
| 372 | .open = open_file_generic, |
| 373 | }; |
| 374 | |
| 375 | static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta, |
| 376 | struct dentry *dir) |
| 377 | { |
| 378 | struct sta_rate_control *srctrl = priv_sta; |
| 379 | |
| 380 | srctrl->tx_avg_rate_num_dentry = |
| 381 | debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400, |
| 382 | dir, srctrl, &sta_tx_avg_rate_num_ops); |
| 383 | srctrl->tx_avg_rate_sum_dentry = |
| 384 | debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400, |
| 385 | dir, srctrl, &sta_tx_avg_rate_sum_ops); |
| 386 | } |
| 387 | |
| 388 | static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta) |
| 389 | { |
| 390 | struct sta_rate_control *srctrl = priv_sta; |
| 391 | |
| 392 | debugfs_remove(srctrl->tx_avg_rate_sum_dentry); |
| 393 | debugfs_remove(srctrl->tx_avg_rate_num_dentry); |
| 394 | } |
| 395 | #endif |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 396 | |
| 397 | static struct rate_control_ops rate_control_simple = { |
| 398 | .module = THIS_MODULE, |
| 399 | .name = "simple", |
| 400 | .tx_status = rate_control_simple_tx_status, |
| 401 | .get_rate = rate_control_simple_get_rate, |
| 402 | .rate_init = rate_control_simple_rate_init, |
| 403 | .clear = rate_control_simple_clear, |
| 404 | .alloc = rate_control_simple_alloc, |
| 405 | .free = rate_control_simple_free, |
| 406 | .alloc_sta = rate_control_simple_alloc_sta, |
| 407 | .free_sta = rate_control_simple_free_sta, |
Jiri Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 409 | .add_sta_debugfs = rate_control_simple_add_sta_debugfs, |
| 410 | .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs, |
| 411 | #endif |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | |
| 415 | static int __init rate_control_simple_init(void) |
| 416 | { |
| 417 | return ieee80211_rate_control_register(&rate_control_simple); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | static void __exit rate_control_simple_exit(void) |
| 422 | { |
| 423 | ieee80211_rate_control_unregister(&rate_control_simple); |
| 424 | } |
| 425 | |
| 426 | |
Johannes Berg | ca9938f | 2007-09-11 12:50:32 +0200 | [diff] [blame] | 427 | subsys_initcall(rate_control_simple_init); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 428 | module_exit(rate_control_simple_exit); |
| 429 | |
| 430 | MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211"); |
| 431 | MODULE_LICENSE("GPL"); |