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" 21#include "debugfs.h" 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 32MODULE_ALIAS("rc80211_default"); 33 34static 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 93 94static struct ieee80211_rate * 95rate_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 113struct global_rate_control { 114 int dummy; 115}; 116 117struct 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; 125 126#ifdef CONFIG_MAC80211_DEBUGFS 127 struct dentry *tx_avg_rate_sum_dentry; 128 struct dentry *tx_avg_rate_num_dentry; 129#endif 130}; 131 132 133static 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) { 150 sta->antenna_sel_tx = sta->antenna_sel_tx == 1 ? 2 : 1; 151 sta->antenna_sel_rx = sta->antenna_sel_rx == 1 ? 2 : 1; 152 if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) { 153 printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d " 154 "RX antenna --> %d (@%lu)\n", 155 dev->name, MAC_ARG(hdr->addr1), 156 sta->antenna_sel_tx, sta->antenna_sel_rx, jiffies); 157 } 158 srctrl->tx_num_failures++; 159 sta->tx_retry_failed++; 160 sta->tx_num_consecutive_failures++; 161 sta->tx_num_mpdu_fail++; 162 } else { 163 sta->last_ack_rssi[0] = sta->last_ack_rssi[1]; 164 sta->last_ack_rssi[1] = sta->last_ack_rssi[2]; 165 sta->last_ack_rssi[2] = status->ack_signal; 166 sta->tx_num_consecutive_failures = 0; 167 sta->tx_num_mpdu_ok++; 168 } 169 sta->tx_retry_count += status->retry_count; 170 sta->tx_num_mpdu_fail += status->retry_count; 171 172 if (time_after(jiffies, 173 srctrl->last_rate_change + RATE_CONTROL_INTERVAL) && 174 srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) { 175 u32 per_failed; 176 srctrl->last_rate_change = jiffies; 177 178 per_failed = (100 * sta->tx_num_mpdu_fail) / 179 (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok); 180 /* TODO: calculate average per_failed to make adjusting 181 * parameters easier */ 182 183 if (per_failed > local->rate_ctrl_num_down) { 184 rate_control_rate_dec(local, sta); 185 } else if (per_failed < local->rate_ctrl_num_up) { 186 rate_control_rate_inc(local, sta); 187 } 188 srctrl->tx_avg_rate_sum += status->control.rate->rate; 189 srctrl->tx_avg_rate_num++; 190 srctrl->tx_num_failures = 0; 191 srctrl->tx_num_xmit = 0; 192 } else if (sta->tx_num_consecutive_failures >= 193 RATE_CONTROL_EMERG_DEC) { 194 rate_control_rate_dec(local, sta); 195 } 196 197 if (srctrl->avg_rate_update + 60 * HZ < jiffies) { 198 srctrl->avg_rate_update = jiffies; 199 if (srctrl->tx_avg_rate_num > 0) { 200#ifdef CONFIG_MAC80211_VERBOSE_DEBUG 201 printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: " 202 "%d (%d/%d)\n", 203 dev->name, MAC_ARG(sta->addr), 204 srctrl->tx_avg_rate_sum / 205 srctrl->tx_avg_rate_num, 206 srctrl->tx_avg_rate_sum, 207 srctrl->tx_avg_rate_num); 208#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */ 209 srctrl->tx_avg_rate_sum = 0; 210 srctrl->tx_avg_rate_num = 0; 211 } 212 } 213 214 sta_info_put(sta); 215} 216 217 218static struct ieee80211_rate * 219rate_control_simple_get_rate(void *priv, struct net_device *dev, 220 struct sk_buff *skb, 221 struct rate_control_extra *extra) 222{ 223 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 224 struct ieee80211_sub_if_data *sdata; 225 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 226 struct ieee80211_hw_mode *mode = extra->mode; 227 struct sta_info *sta; 228 int rateidx, nonerp_idx; 229 u16 fc; 230 231 memset(extra, 0, sizeof(*extra)); 232 233 fc = le16_to_cpu(hdr->frame_control); 234 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || 235 (hdr->addr1[0] & 0x01)) { 236 /* Send management frames and broadcast/multicast data using 237 * lowest rate. */ 238 /* TODO: this could probably be improved.. */ 239 return rate_control_lowest_rate(local, mode); 240 } 241 242 sta = sta_info_get(local, hdr->addr1); 243 244 if (!sta) 245 return rate_control_lowest_rate(local, mode); 246 247 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 248 if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) 249 sta->txrate = sdata->bss->force_unicast_rateidx; 250 251 rateidx = sta->txrate; 252 253 if (rateidx >= mode->num_rates) 254 rateidx = mode->num_rates - 1; 255 256 sta->last_txrate = rateidx; 257 nonerp_idx = rateidx; 258 while (nonerp_idx > 0 && 259 ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) || 260 !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) || 261 !(sta->supp_rates & BIT(nonerp_idx)))) 262 nonerp_idx--; 263 extra->nonerp = &mode->rates[nonerp_idx]; 264 265 sta_info_put(sta); 266 267 return &mode->rates[rateidx]; 268} 269 270 271static void rate_control_simple_rate_init(void *priv, void *priv_sta, 272 struct ieee80211_local *local, 273 struct sta_info *sta) 274{ 275 struct ieee80211_hw_mode *mode; 276 int i; 277 sta->txrate = 0; 278 mode = local->oper_hw_mode; 279 /* TODO: what is a good starting rate for STA? About middle? Maybe not 280 * the lowest or the highest rate.. Could consider using RSSI from 281 * previous packets? Need to have IEEE 802.1X auth succeed immediately 282 * after assoc.. */ 283 for (i = 0; i < mode->num_rates; i++) { 284 if ((sta->supp_rates & BIT(i)) && 285 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) 286 sta->txrate = i; 287 } 288} 289 290 291static void * rate_control_simple_alloc(struct ieee80211_local *local) 292{ 293 struct global_rate_control *rctrl; 294 295 rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC); 296 297 return rctrl; 298} 299 300 301static void rate_control_simple_free(void *priv) 302{ 303 struct global_rate_control *rctrl = priv; 304 kfree(rctrl); 305} 306 307 308static void rate_control_simple_clear(void *priv) 309{ 310} 311 312 313static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp) 314{ 315 struct sta_rate_control *rctrl; 316 317 rctrl = kzalloc(sizeof(*rctrl), gfp); 318 319 return rctrl; 320} 321 322 323static void rate_control_simple_free_sta(void *priv, void *priv_sta) 324{ 325 struct sta_rate_control *rctrl = priv_sta; 326 kfree(rctrl); 327} 328 329#ifdef CONFIG_MAC80211_DEBUGFS 330 331static int open_file_generic(struct inode *inode, struct file *file) 332{ 333 file->private_data = inode->i_private; 334 return 0; 335} 336 337static ssize_t sta_tx_avg_rate_sum_read(struct file *file, 338 char __user *userbuf, 339 size_t count, loff_t *ppos) 340{ 341 struct sta_rate_control *srctrl = file->private_data; 342 char buf[20]; 343 344 sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum); 345 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); 346} 347 348static const struct file_operations sta_tx_avg_rate_sum_ops = { 349 .read = sta_tx_avg_rate_sum_read, 350 .open = open_file_generic, 351}; 352 353static ssize_t sta_tx_avg_rate_num_read(struct file *file, 354 char __user *userbuf, 355 size_t count, loff_t *ppos) 356{ 357 struct sta_rate_control *srctrl = file->private_data; 358 char buf[20]; 359 360 sprintf(buf, "%d\n", srctrl->tx_avg_rate_num); 361 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); 362} 363 364static const struct file_operations sta_tx_avg_rate_num_ops = { 365 .read = sta_tx_avg_rate_num_read, 366 .open = open_file_generic, 367}; 368 369static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta, 370 struct dentry *dir) 371{ 372 struct sta_rate_control *srctrl = priv_sta; 373 374 srctrl->tx_avg_rate_num_dentry = 375 debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400, 376 dir, srctrl, &sta_tx_avg_rate_num_ops); 377 srctrl->tx_avg_rate_sum_dentry = 378 debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400, 379 dir, srctrl, &sta_tx_avg_rate_sum_ops); 380} 381 382static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta) 383{ 384 struct sta_rate_control *srctrl = priv_sta; 385 386 debugfs_remove(srctrl->tx_avg_rate_sum_dentry); 387 debugfs_remove(srctrl->tx_avg_rate_num_dentry); 388} 389#endif 390 391static struct rate_control_ops rate_control_simple = { 392 .module = THIS_MODULE, 393 .name = "simple", 394 .tx_status = rate_control_simple_tx_status, 395 .get_rate = rate_control_simple_get_rate, 396 .rate_init = rate_control_simple_rate_init, 397 .clear = rate_control_simple_clear, 398 .alloc = rate_control_simple_alloc, 399 .free = rate_control_simple_free, 400 .alloc_sta = rate_control_simple_alloc_sta, 401 .free_sta = rate_control_simple_free_sta, 402#ifdef CONFIG_MAC80211_DEBUGFS 403 .add_sta_debugfs = rate_control_simple_add_sta_debugfs, 404 .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs, 405#endif 406}; 407 408 409static int __init rate_control_simple_init(void) 410{ 411 return ieee80211_rate_control_register(&rate_control_simple); 412} 413 414 415static void __exit rate_control_simple_exit(void) 416{ 417 ieee80211_rate_control_unregister(&rate_control_simple); 418} 419 420 421module_init(rate_control_simple_init); 422module_exit(rate_control_simple_exit); 423 424MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211"); 425MODULE_LICENSE("GPL"); 426