rc80211_simple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if (per_failed > local->rate_ctrl_num_down) {
  156. rate_control_rate_dec(local, sta);
  157. } else if (per_failed < local->rate_ctrl_num_up) {
  158. rate_control_rate_inc(local, sta);
  159. }
  160. srctrl->tx_avg_rate_sum += status->control.rate->rate;
  161. srctrl->tx_avg_rate_num++;
  162. srctrl->tx_num_failures = 0;
  163. srctrl->tx_num_xmit = 0;
  164. } else if (sta->tx_num_consecutive_failures >=
  165. RATE_CONTROL_EMERG_DEC) {
  166. rate_control_rate_dec(local, sta);
  167. }
  168. if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
  169. srctrl->avg_rate_update = jiffies;
  170. if (srctrl->tx_avg_rate_num > 0) {
  171. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  172. printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
  173. "%d (%d/%d)\n",
  174. dev->name, MAC_ARG(sta->addr),
  175. srctrl->tx_avg_rate_sum /
  176. srctrl->tx_avg_rate_num,
  177. srctrl->tx_avg_rate_sum,
  178. srctrl->tx_avg_rate_num);
  179. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  180. srctrl->tx_avg_rate_sum = 0;
  181. srctrl->tx_avg_rate_num = 0;
  182. }
  183. }
  184. sta_info_put(sta);
  185. }
  186. static struct ieee80211_rate *
  187. rate_control_simple_get_rate(void *priv, struct net_device *dev,
  188. struct sk_buff *skb,
  189. struct rate_control_extra *extra)
  190. {
  191. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  192. struct ieee80211_sub_if_data *sdata;
  193. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  194. struct ieee80211_hw_mode *mode = extra->mode;
  195. struct sta_info *sta;
  196. int rateidx, nonerp_idx;
  197. u16 fc;
  198. memset(extra, 0, sizeof(*extra));
  199. fc = le16_to_cpu(hdr->frame_control);
  200. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  201. (hdr->addr1[0] & 0x01)) {
  202. /* Send management frames and broadcast/multicast data using
  203. * lowest rate. */
  204. /* TODO: this could probably be improved.. */
  205. return rate_control_lowest_rate(local, mode);
  206. }
  207. sta = sta_info_get(local, hdr->addr1);
  208. if (!sta)
  209. return rate_control_lowest_rate(local, mode);
  210. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  211. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
  212. sta->txrate = sdata->bss->force_unicast_rateidx;
  213. rateidx = sta->txrate;
  214. if (rateidx >= mode->num_rates)
  215. rateidx = mode->num_rates - 1;
  216. sta->last_txrate = rateidx;
  217. nonerp_idx = rateidx;
  218. while (nonerp_idx > 0 &&
  219. ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
  220. !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
  221. !(sta->supp_rates & BIT(nonerp_idx))))
  222. nonerp_idx--;
  223. extra->nonerp = &mode->rates[nonerp_idx];
  224. sta_info_put(sta);
  225. return &mode->rates[rateidx];
  226. }
  227. static void rate_control_simple_rate_init(void *priv, void *priv_sta,
  228. struct ieee80211_local *local,
  229. struct sta_info *sta)
  230. {
  231. struct ieee80211_hw_mode *mode;
  232. int i;
  233. sta->txrate = 0;
  234. mode = local->oper_hw_mode;
  235. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  236. * the lowest or the highest rate.. Could consider using RSSI from
  237. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  238. * after assoc.. */
  239. for (i = 0; i < mode->num_rates; i++) {
  240. if ((sta->supp_rates & BIT(i)) &&
  241. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
  242. sta->txrate = i;
  243. }
  244. }
  245. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  246. {
  247. struct global_rate_control *rctrl;
  248. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  249. return rctrl;
  250. }
  251. static void rate_control_simple_free(void *priv)
  252. {
  253. struct global_rate_control *rctrl = priv;
  254. kfree(rctrl);
  255. }
  256. static void rate_control_simple_clear(void *priv)
  257. {
  258. }
  259. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  260. {
  261. struct sta_rate_control *rctrl;
  262. rctrl = kzalloc(sizeof(*rctrl), gfp);
  263. return rctrl;
  264. }
  265. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  266. {
  267. struct sta_rate_control *rctrl = priv_sta;
  268. kfree(rctrl);
  269. }
  270. #ifdef CONFIG_MAC80211_DEBUGFS
  271. static int open_file_generic(struct inode *inode, struct file *file)
  272. {
  273. file->private_data = inode->i_private;
  274. return 0;
  275. }
  276. static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
  277. char __user *userbuf,
  278. size_t count, loff_t *ppos)
  279. {
  280. struct sta_rate_control *srctrl = file->private_data;
  281. char buf[20];
  282. sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
  283. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  284. }
  285. static const struct file_operations sta_tx_avg_rate_sum_ops = {
  286. .read = sta_tx_avg_rate_sum_read,
  287. .open = open_file_generic,
  288. };
  289. static ssize_t sta_tx_avg_rate_num_read(struct file *file,
  290. char __user *userbuf,
  291. size_t count, loff_t *ppos)
  292. {
  293. struct sta_rate_control *srctrl = file->private_data;
  294. char buf[20];
  295. sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
  296. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  297. }
  298. static const struct file_operations sta_tx_avg_rate_num_ops = {
  299. .read = sta_tx_avg_rate_num_read,
  300. .open = open_file_generic,
  301. };
  302. static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
  303. struct dentry *dir)
  304. {
  305. struct sta_rate_control *srctrl = priv_sta;
  306. srctrl->tx_avg_rate_num_dentry =
  307. debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
  308. dir, srctrl, &sta_tx_avg_rate_num_ops);
  309. srctrl->tx_avg_rate_sum_dentry =
  310. debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
  311. dir, srctrl, &sta_tx_avg_rate_sum_ops);
  312. }
  313. static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
  314. {
  315. struct sta_rate_control *srctrl = priv_sta;
  316. debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
  317. debugfs_remove(srctrl->tx_avg_rate_num_dentry);
  318. }
  319. #endif
  320. static struct rate_control_ops rate_control_simple = {
  321. .module = THIS_MODULE,
  322. .name = "simple",
  323. .tx_status = rate_control_simple_tx_status,
  324. .get_rate = rate_control_simple_get_rate,
  325. .rate_init = rate_control_simple_rate_init,
  326. .clear = rate_control_simple_clear,
  327. .alloc = rate_control_simple_alloc,
  328. .free = rate_control_simple_free,
  329. .alloc_sta = rate_control_simple_alloc_sta,
  330. .free_sta = rate_control_simple_free_sta,
  331. #ifdef CONFIG_MAC80211_DEBUGFS
  332. .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
  333. .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
  334. #endif
  335. };
  336. static int __init rate_control_simple_init(void)
  337. {
  338. return ieee80211_rate_control_register(&rate_control_simple);
  339. }
  340. static void __exit rate_control_simple_exit(void)
  341. {
  342. ieee80211_rate_control_unregister(&rate_control_simple);
  343. }
  344. module_init(rate_control_simple_init);
  345. module_exit(rate_control_simple_exit);
  346. MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
  347. MODULE_LICENSE("GPL");