rc80211_simple.c 9.9 KB

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