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 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 10 | #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 Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 20 | #include "debugfs.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 26 | #define RATE_CONTROL_NUM_DOWN 20 |
| 27 | #define RATE_CONTROL_NUM_UP 15 |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 28 | |
| 29 | #define RATE_CONTROL_EMERG_DEC 2 |
| 30 | #define RATE_CONTROL_INTERVAL (HZ / 20) |
| 31 | #define RATE_CONTROL_MIN_TX 10 |
| 32 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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 | 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 | |
| 65 | static 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 92 | struct global_rate_control { |
| 93 | int dummy; |
| 94 | }; |
| 95 | |
| 96 | struct 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 Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 104 | |
| 105 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 106 | struct dentry *tx_avg_rate_sum_dentry; |
| 107 | struct dentry *tx_avg_rate_num_dentry; |
| 108 | #endif |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | |
| 112 | static 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 129 | 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 Berg | 3ef8bed | 2007-07-10 19:32:09 +0200 | [diff] [blame] | 161 | /* |
| 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 166 | rate_control_rate_dec(local, sta); |
Johannes Berg | 3ef8bed | 2007-07-10 19:32:09 +0200 | [diff] [blame] | 167 | } else if (per_failed < RATE_CONTROL_NUM_UP) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 168 | 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 Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 183 | DECLARE_MAC_BUF(mac); |
| 184 | printk(KERN_DEBUG "%s: STA %s Average rate: " |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 185 | "%d (%d/%d)\n", |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 186 | dev->name, print_mac(mac, sta->addr), |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 187 | 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 Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 201 | static void |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 202 | rate_control_simple_get_rate(void *priv, struct net_device *dev, |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 203 | struct ieee80211_hw_mode *mode, |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 204 | struct sk_buff *skb, |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 205 | struct rate_selection *sel) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 206 | { |
| 207 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 208 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 209 | struct sta_info *sta; |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 210 | int rateidx; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 211 | |
| 212 | sta = sta_info_get(local, hdr->addr1); |
| 213 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 214 | if (!sta) { |
| 215 | sel->rate = rate_lowest(local, mode, NULL); |
| 216 | return; |
| 217 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 218 | |
| 219 | rateidx = sta->txrate; |
| 220 | |
| 221 | if (rateidx >= mode->num_rates) |
| 222 | rateidx = mode->num_rates - 1; |
| 223 | |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 224 | sta_info_put(sta); |
| 225 | |
Mattias Nissler | 1abbe49 | 2007-12-20 13:50:07 +0100 | [diff] [blame^] | 226 | sel->rate = &mode->rates[rateidx]; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
| 230 | static 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 Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 238 | /* 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 242 | for (i = 0; i < mode->num_rates; i++) { |
| 243 | if ((sta->supp_rates & BIT(i)) && |
Larry Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 244 | (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 245 | sta->txrate = i; |
Larry Finger | eef6caf | 2007-07-02 22:36:38 -0700 | [diff] [blame] | 246 | break; |
| 247 | } |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | static 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 | |
| 262 | static void rate_control_simple_free(void *priv) |
| 263 | { |
| 264 | struct global_rate_control *rctrl = priv; |
| 265 | kfree(rctrl); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static void rate_control_simple_clear(void *priv) |
| 270 | { |
| 271 | } |
| 272 | |
| 273 | |
| 274 | static 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 | |
| 284 | static 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 Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 290 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 291 | |
| 292 | static int open_file_generic(struct inode *inode, struct file *file) |
| 293 | { |
| 294 | file->private_data = inode->i_private; |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static 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 | |
| 309 | static 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 | |
| 314 | static 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 | |
| 325 | static 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 | |
| 330 | static 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 | |
| 343 | static 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 351 | |
Johannes Berg | ac71c69 | 2007-10-28 14:17:44 +0100 | [diff] [blame] | 352 | struct rate_control_ops mac80211_rcsimple = { |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 353 | .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 Benc | e9f207f | 2007-05-05 11:46:38 -0700 | [diff] [blame] | 362 | #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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 366 | }; |