rc80211_simple.c 10 KB

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