rc80211_simple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include <linux/module.h>
  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. #include <net/mac80211.h>
  17. #include "ieee80211_i.h"
  18. #include "ieee80211_rate.h"
  19. #include "debugfs.h"
  20. /* This is a minimal implementation of TX rate controlling that can be used
  21. * as the default when no improved mechanisms are available. */
  22. #define RATE_CONTROL_EMERG_DEC 2
  23. #define RATE_CONTROL_INTERVAL (HZ / 20)
  24. #define RATE_CONTROL_MIN_TX 10
  25. MODULE_ALIAS("rc80211_default");
  26. static void rate_control_rate_inc(struct ieee80211_local *local,
  27. struct sta_info *sta)
  28. {
  29. struct ieee80211_sub_if_data *sdata;
  30. struct ieee80211_hw_mode *mode;
  31. int i = sta->txrate;
  32. int maxrate;
  33. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  34. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  35. /* forced unicast rate - do not change STA rate */
  36. return;
  37. }
  38. mode = local->oper_hw_mode;
  39. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  40. if (i > mode->num_rates)
  41. i = mode->num_rates - 2;
  42. while (i + 1 < mode->num_rates) {
  43. i++;
  44. if (sta->supp_rates & BIT(i) &&
  45. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
  46. (maxrate < 0 || i <= maxrate)) {
  47. sta->txrate = i;
  48. break;
  49. }
  50. }
  51. }
  52. static void rate_control_rate_dec(struct ieee80211_local *local,
  53. struct sta_info *sta)
  54. {
  55. struct ieee80211_sub_if_data *sdata;
  56. struct ieee80211_hw_mode *mode;
  57. int i = sta->txrate;
  58. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  59. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  60. /* forced unicast rate - do not change STA rate */
  61. return;
  62. }
  63. mode = local->oper_hw_mode;
  64. if (i > mode->num_rates)
  65. i = mode->num_rates;
  66. while (i > 0) {
  67. i--;
  68. if (sta->supp_rates & BIT(i) &&
  69. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
  70. sta->txrate = i;
  71. break;
  72. }
  73. }
  74. }
  75. static struct ieee80211_rate *
  76. rate_control_lowest_rate(struct ieee80211_local *local,
  77. struct ieee80211_hw_mode *mode)
  78. {
  79. int i;
  80. for (i = 0; i < mode->num_rates; i++) {
  81. struct ieee80211_rate *rate = &mode->rates[i];
  82. if (rate->flags & IEEE80211_RATE_SUPPORTED)
  83. return rate;
  84. }
  85. printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
  86. "found\n");
  87. return &mode->rates[0];
  88. }
  89. struct global_rate_control {
  90. int dummy;
  91. };
  92. struct sta_rate_control {
  93. unsigned long last_rate_change;
  94. u32 tx_num_failures;
  95. u32 tx_num_xmit;
  96. unsigned long avg_rate_update;
  97. u32 tx_avg_rate_sum;
  98. u32 tx_avg_rate_num;
  99. #ifdef CONFIG_MAC80211_DEBUGFS
  100. struct dentry *tx_avg_rate_sum_dentry;
  101. struct dentry *tx_avg_rate_num_dentry;
  102. #endif
  103. };
  104. static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
  105. struct sk_buff *skb,
  106. struct ieee80211_tx_status *status)
  107. {
  108. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  109. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  110. struct sta_info *sta;
  111. struct sta_rate_control *srctrl;
  112. sta = sta_info_get(local, hdr->addr1);
  113. if (!sta)
  114. return;
  115. srctrl = sta->rate_ctrl_priv;
  116. srctrl->tx_num_xmit++;
  117. if (status->excessive_retries) {
  118. sta->antenna_sel_tx = sta->antenna_sel_tx == 1 ? 2 : 1;
  119. sta->antenna_sel_rx = sta->antenna_sel_rx == 1 ? 2 : 1;
  120. if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
  121. printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d "
  122. "RX antenna --> %d (@%lu)\n",
  123. dev->name, MAC_ARG(hdr->addr1),
  124. sta->antenna_sel_tx, sta->antenna_sel_rx, jiffies);
  125. }
  126. srctrl->tx_num_failures++;
  127. sta->tx_retry_failed++;
  128. sta->tx_num_consecutive_failures++;
  129. sta->tx_num_mpdu_fail++;
  130. } else {
  131. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  132. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  133. sta->last_ack_rssi[2] = status->ack_signal;
  134. sta->tx_num_consecutive_failures = 0;
  135. sta->tx_num_mpdu_ok++;
  136. }
  137. sta->tx_retry_count += status->retry_count;
  138. sta->tx_num_mpdu_fail += status->retry_count;
  139. if (time_after(jiffies,
  140. srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
  141. srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
  142. u32 per_failed;
  143. srctrl->last_rate_change = jiffies;
  144. per_failed = (100 * sta->tx_num_mpdu_fail) /
  145. (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
  146. /* TODO: calculate average per_failed to make adjusting
  147. * parameters easier */
  148. #if 0
  149. if (net_ratelimit()) {
  150. printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
  151. sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
  152. per_failed);
  153. }
  154. #endif
  155. /*
  156. * XXX: Make these configurable once we have an
  157. * interface to the rate control algorithms
  158. */
  159. if (per_failed > RATE_CONTROL_NUM_DOWN) {
  160. rate_control_rate_dec(local, sta);
  161. } else if (per_failed < RATE_CONTROL_NUM_UP) {
  162. rate_control_rate_inc(local, sta);
  163. }
  164. srctrl->tx_avg_rate_sum += status->control.rate->rate;
  165. srctrl->tx_avg_rate_num++;
  166. srctrl->tx_num_failures = 0;
  167. srctrl->tx_num_xmit = 0;
  168. } else if (sta->tx_num_consecutive_failures >=
  169. RATE_CONTROL_EMERG_DEC) {
  170. rate_control_rate_dec(local, sta);
  171. }
  172. if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
  173. srctrl->avg_rate_update = jiffies;
  174. if (srctrl->tx_avg_rate_num > 0) {
  175. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  176. printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
  177. "%d (%d/%d)\n",
  178. dev->name, MAC_ARG(sta->addr),
  179. srctrl->tx_avg_rate_sum /
  180. srctrl->tx_avg_rate_num,
  181. srctrl->tx_avg_rate_sum,
  182. srctrl->tx_avg_rate_num);
  183. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  184. srctrl->tx_avg_rate_sum = 0;
  185. srctrl->tx_avg_rate_num = 0;
  186. }
  187. }
  188. sta_info_put(sta);
  189. }
  190. static struct ieee80211_rate *
  191. rate_control_simple_get_rate(void *priv, struct net_device *dev,
  192. struct sk_buff *skb,
  193. struct rate_control_extra *extra)
  194. {
  195. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  196. struct ieee80211_sub_if_data *sdata;
  197. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  198. struct ieee80211_hw_mode *mode = extra->mode;
  199. struct sta_info *sta;
  200. int rateidx, nonerp_idx;
  201. u16 fc;
  202. memset(extra, 0, sizeof(*extra));
  203. fc = le16_to_cpu(hdr->frame_control);
  204. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  205. (hdr->addr1[0] & 0x01)) {
  206. /* Send management frames and broadcast/multicast data using
  207. * lowest rate. */
  208. /* TODO: this could probably be improved.. */
  209. return rate_control_lowest_rate(local, mode);
  210. }
  211. sta = sta_info_get(local, hdr->addr1);
  212. if (!sta)
  213. return rate_control_lowest_rate(local, mode);
  214. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  215. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
  216. sta->txrate = sdata->bss->force_unicast_rateidx;
  217. rateidx = sta->txrate;
  218. if (rateidx >= mode->num_rates)
  219. rateidx = mode->num_rates - 1;
  220. sta->last_txrate = rateidx;
  221. nonerp_idx = rateidx;
  222. while (nonerp_idx > 0 &&
  223. ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
  224. !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
  225. !(sta->supp_rates & BIT(nonerp_idx))))
  226. nonerp_idx--;
  227. extra->nonerp = &mode->rates[nonerp_idx];
  228. sta_info_put(sta);
  229. return &mode->rates[rateidx];
  230. }
  231. static void rate_control_simple_rate_init(void *priv, void *priv_sta,
  232. struct ieee80211_local *local,
  233. struct sta_info *sta)
  234. {
  235. struct ieee80211_hw_mode *mode;
  236. int i;
  237. sta->txrate = 0;
  238. mode = local->oper_hw_mode;
  239. /* TODO: This routine should consider using RSSI from previous packets
  240. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  241. * Until that method is implemented, we will use the lowest supported rate
  242. * as a workaround, */
  243. for (i = 0; i < mode->num_rates; i++) {
  244. if ((sta->supp_rates & BIT(i)) &&
  245. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
  246. sta->txrate = i;
  247. break;
  248. }
  249. }
  250. }
  251. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  252. {
  253. struct global_rate_control *rctrl;
  254. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  255. return rctrl;
  256. }
  257. static void rate_control_simple_free(void *priv)
  258. {
  259. struct global_rate_control *rctrl = priv;
  260. kfree(rctrl);
  261. }
  262. static void rate_control_simple_clear(void *priv)
  263. {
  264. }
  265. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  266. {
  267. struct sta_rate_control *rctrl;
  268. rctrl = kzalloc(sizeof(*rctrl), gfp);
  269. return rctrl;
  270. }
  271. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  272. {
  273. struct sta_rate_control *rctrl = priv_sta;
  274. kfree(rctrl);
  275. }
  276. #ifdef CONFIG_MAC80211_DEBUGFS
  277. static int open_file_generic(struct inode *inode, struct file *file)
  278. {
  279. file->private_data = inode->i_private;
  280. return 0;
  281. }
  282. static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
  283. char __user *userbuf,
  284. size_t count, loff_t *ppos)
  285. {
  286. struct sta_rate_control *srctrl = file->private_data;
  287. char buf[20];
  288. sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
  289. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  290. }
  291. static const struct file_operations sta_tx_avg_rate_sum_ops = {
  292. .read = sta_tx_avg_rate_sum_read,
  293. .open = open_file_generic,
  294. };
  295. static ssize_t sta_tx_avg_rate_num_read(struct file *file,
  296. char __user *userbuf,
  297. size_t count, loff_t *ppos)
  298. {
  299. struct sta_rate_control *srctrl = file->private_data;
  300. char buf[20];
  301. sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
  302. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  303. }
  304. static const struct file_operations sta_tx_avg_rate_num_ops = {
  305. .read = sta_tx_avg_rate_num_read,
  306. .open = open_file_generic,
  307. };
  308. static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
  309. struct dentry *dir)
  310. {
  311. struct sta_rate_control *srctrl = priv_sta;
  312. srctrl->tx_avg_rate_num_dentry =
  313. debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
  314. dir, srctrl, &sta_tx_avg_rate_num_ops);
  315. srctrl->tx_avg_rate_sum_dentry =
  316. debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
  317. dir, srctrl, &sta_tx_avg_rate_sum_ops);
  318. }
  319. static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
  320. {
  321. struct sta_rate_control *srctrl = priv_sta;
  322. debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
  323. debugfs_remove(srctrl->tx_avg_rate_num_dentry);
  324. }
  325. #endif
  326. static struct rate_control_ops rate_control_simple = {
  327. .module = THIS_MODULE,
  328. .name = "simple",
  329. .tx_status = rate_control_simple_tx_status,
  330. .get_rate = rate_control_simple_get_rate,
  331. .rate_init = rate_control_simple_rate_init,
  332. .clear = rate_control_simple_clear,
  333. .alloc = rate_control_simple_alloc,
  334. .free = rate_control_simple_free,
  335. .alloc_sta = rate_control_simple_alloc_sta,
  336. .free_sta = rate_control_simple_free_sta,
  337. #ifdef CONFIG_MAC80211_DEBUGFS
  338. .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
  339. .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
  340. #endif
  341. };
  342. static int __init rate_control_simple_init(void)
  343. {
  344. return ieee80211_rate_control_register(&rate_control_simple);
  345. }
  346. static void __exit rate_control_simple_exit(void)
  347. {
  348. ieee80211_rate_control_unregister(&rate_control_simple);
  349. }
  350. module_init(rate_control_simple_init);
  351. module_exit(rate_control_simple_exit);
  352. MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
  353. MODULE_LICENSE("GPL");