rc80211_simple.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. srctrl->tx_num_failures++;
  119. sta->tx_retry_failed++;
  120. sta->tx_num_consecutive_failures++;
  121. sta->tx_num_mpdu_fail++;
  122. } else {
  123. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  124. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  125. sta->last_ack_rssi[2] = status->ack_signal;
  126. sta->tx_num_consecutive_failures = 0;
  127. sta->tx_num_mpdu_ok++;
  128. }
  129. sta->tx_retry_count += status->retry_count;
  130. sta->tx_num_mpdu_fail += status->retry_count;
  131. if (time_after(jiffies,
  132. srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
  133. srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
  134. u32 per_failed;
  135. srctrl->last_rate_change = jiffies;
  136. per_failed = (100 * sta->tx_num_mpdu_fail) /
  137. (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
  138. /* TODO: calculate average per_failed to make adjusting
  139. * parameters easier */
  140. #if 0
  141. if (net_ratelimit()) {
  142. printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
  143. sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
  144. per_failed);
  145. }
  146. #endif
  147. /*
  148. * XXX: Make these configurable once we have an
  149. * interface to the rate control algorithms
  150. */
  151. if (per_failed > RATE_CONTROL_NUM_DOWN) {
  152. rate_control_rate_dec(local, sta);
  153. } else if (per_failed < RATE_CONTROL_NUM_UP) {
  154. rate_control_rate_inc(local, sta);
  155. }
  156. srctrl->tx_avg_rate_sum += status->control.rate->rate;
  157. srctrl->tx_avg_rate_num++;
  158. srctrl->tx_num_failures = 0;
  159. srctrl->tx_num_xmit = 0;
  160. } else if (sta->tx_num_consecutive_failures >=
  161. RATE_CONTROL_EMERG_DEC) {
  162. rate_control_rate_dec(local, sta);
  163. }
  164. if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
  165. srctrl->avg_rate_update = jiffies;
  166. if (srctrl->tx_avg_rate_num > 0) {
  167. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  168. DECLARE_MAC_BUF(mac);
  169. printk(KERN_DEBUG "%s: STA %s Average rate: "
  170. "%d (%d/%d)\n",
  171. dev->name, print_mac(mac, sta->addr),
  172. srctrl->tx_avg_rate_sum /
  173. srctrl->tx_avg_rate_num,
  174. srctrl->tx_avg_rate_sum,
  175. srctrl->tx_avg_rate_num);
  176. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  177. srctrl->tx_avg_rate_sum = 0;
  178. srctrl->tx_avg_rate_num = 0;
  179. }
  180. }
  181. sta_info_put(sta);
  182. }
  183. static struct ieee80211_rate *
  184. rate_control_simple_get_rate(void *priv, struct net_device *dev,
  185. struct sk_buff *skb,
  186. struct rate_control_extra *extra)
  187. {
  188. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  189. struct ieee80211_sub_if_data *sdata;
  190. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  191. struct ieee80211_hw_mode *mode = extra->mode;
  192. struct sta_info *sta;
  193. int rateidx, nonerp_idx;
  194. u16 fc;
  195. memset(extra, 0, sizeof(*extra));
  196. fc = le16_to_cpu(hdr->frame_control);
  197. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  198. (hdr->addr1[0] & 0x01)) {
  199. /* Send management frames and broadcast/multicast data using
  200. * lowest rate. */
  201. /* TODO: this could probably be improved.. */
  202. return rate_control_lowest_rate(local, mode);
  203. }
  204. sta = sta_info_get(local, hdr->addr1);
  205. if (!sta)
  206. return rate_control_lowest_rate(local, mode);
  207. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  208. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
  209. sta->txrate = sdata->bss->force_unicast_rateidx;
  210. rateidx = sta->txrate;
  211. if (rateidx >= mode->num_rates)
  212. rateidx = mode->num_rates - 1;
  213. sta->last_txrate = rateidx;
  214. nonerp_idx = rateidx;
  215. while (nonerp_idx > 0 &&
  216. ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
  217. !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
  218. !(sta->supp_rates & BIT(nonerp_idx))))
  219. nonerp_idx--;
  220. extra->nonerp = &mode->rates[nonerp_idx];
  221. sta_info_put(sta);
  222. return &mode->rates[rateidx];
  223. }
  224. static void rate_control_simple_rate_init(void *priv, void *priv_sta,
  225. struct ieee80211_local *local,
  226. struct sta_info *sta)
  227. {
  228. struct ieee80211_hw_mode *mode;
  229. int i;
  230. sta->txrate = 0;
  231. mode = local->oper_hw_mode;
  232. /* TODO: This routine should consider using RSSI from previous packets
  233. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  234. * Until that method is implemented, we will use the lowest supported rate
  235. * as a workaround, */
  236. for (i = 0; i < mode->num_rates; i++) {
  237. if ((sta->supp_rates & BIT(i)) &&
  238. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED)) {
  239. sta->txrate = i;
  240. break;
  241. }
  242. }
  243. }
  244. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  245. {
  246. struct global_rate_control *rctrl;
  247. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  248. return rctrl;
  249. }
  250. static void rate_control_simple_free(void *priv)
  251. {
  252. struct global_rate_control *rctrl = priv;
  253. kfree(rctrl);
  254. }
  255. static void rate_control_simple_clear(void *priv)
  256. {
  257. }
  258. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  259. {
  260. struct sta_rate_control *rctrl;
  261. rctrl = kzalloc(sizeof(*rctrl), gfp);
  262. return rctrl;
  263. }
  264. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  265. {
  266. struct sta_rate_control *rctrl = priv_sta;
  267. kfree(rctrl);
  268. }
  269. #ifdef CONFIG_MAC80211_DEBUGFS
  270. static int open_file_generic(struct inode *inode, struct file *file)
  271. {
  272. file->private_data = inode->i_private;
  273. return 0;
  274. }
  275. static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
  276. char __user *userbuf,
  277. size_t count, loff_t *ppos)
  278. {
  279. struct sta_rate_control *srctrl = file->private_data;
  280. char buf[20];
  281. sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
  282. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  283. }
  284. static const struct file_operations sta_tx_avg_rate_sum_ops = {
  285. .read = sta_tx_avg_rate_sum_read,
  286. .open = open_file_generic,
  287. };
  288. static ssize_t sta_tx_avg_rate_num_read(struct file *file,
  289. char __user *userbuf,
  290. size_t count, loff_t *ppos)
  291. {
  292. struct sta_rate_control *srctrl = file->private_data;
  293. char buf[20];
  294. sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
  295. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  296. }
  297. static const struct file_operations sta_tx_avg_rate_num_ops = {
  298. .read = sta_tx_avg_rate_num_read,
  299. .open = open_file_generic,
  300. };
  301. static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
  302. struct dentry *dir)
  303. {
  304. struct sta_rate_control *srctrl = priv_sta;
  305. srctrl->tx_avg_rate_num_dentry =
  306. debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
  307. dir, srctrl, &sta_tx_avg_rate_num_ops);
  308. srctrl->tx_avg_rate_sum_dentry =
  309. debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
  310. dir, srctrl, &sta_tx_avg_rate_sum_ops);
  311. }
  312. static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
  313. {
  314. struct sta_rate_control *srctrl = priv_sta;
  315. debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
  316. debugfs_remove(srctrl->tx_avg_rate_num_dentry);
  317. }
  318. #endif
  319. static struct rate_control_ops rate_control_simple = {
  320. .module = THIS_MODULE,
  321. .name = "simple",
  322. .tx_status = rate_control_simple_tx_status,
  323. .get_rate = rate_control_simple_get_rate,
  324. .rate_init = rate_control_simple_rate_init,
  325. .clear = rate_control_simple_clear,
  326. .alloc = rate_control_simple_alloc,
  327. .free = rate_control_simple_free,
  328. .alloc_sta = rate_control_simple_alloc_sta,
  329. .free_sta = rate_control_simple_free_sta,
  330. #ifdef CONFIG_MAC80211_DEBUGFS
  331. .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
  332. .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
  333. #endif
  334. };
  335. static int __init rate_control_simple_init(void)
  336. {
  337. return ieee80211_rate_control_register(&rate_control_simple);
  338. }
  339. static void __exit rate_control_simple_exit(void)
  340. {
  341. ieee80211_rate_control_unregister(&rate_control_simple);
  342. }
  343. subsys_initcall(rate_control_simple_init);
  344. module_exit(rate_control_simple_exit);
  345. MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
  346. MODULE_LICENSE("GPL");