rc80211_simple.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 = sta->sdata;
  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 = sta->sdata;
  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. rcu_read_lock();
  99. sta = sta_info_get(local, hdr->addr1);
  100. if (!sta)
  101. goto unlock;
  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->tx_num_consecutive_failures = 0;
  111. sta->tx_num_mpdu_ok++;
  112. }
  113. sta->tx_retry_count += status->retry_count;
  114. sta->tx_num_mpdu_fail += status->retry_count;
  115. if (time_after(jiffies,
  116. srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
  117. srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
  118. u32 per_failed;
  119. srctrl->last_rate_change = jiffies;
  120. per_failed = (100 * sta->tx_num_mpdu_fail) /
  121. (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
  122. /* TODO: calculate average per_failed to make adjusting
  123. * parameters easier */
  124. #if 0
  125. if (net_ratelimit()) {
  126. printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
  127. sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
  128. per_failed);
  129. }
  130. #endif
  131. /*
  132. * XXX: Make these configurable once we have an
  133. * interface to the rate control algorithms
  134. */
  135. if (per_failed > RATE_CONTROL_NUM_DOWN) {
  136. rate_control_rate_dec(local, sta);
  137. } else if (per_failed < RATE_CONTROL_NUM_UP) {
  138. rate_control_rate_inc(local, sta);
  139. }
  140. srctrl->tx_avg_rate_sum += status->control.tx_rate->bitrate;
  141. srctrl->tx_avg_rate_num++;
  142. srctrl->tx_num_failures = 0;
  143. srctrl->tx_num_xmit = 0;
  144. } else if (sta->tx_num_consecutive_failures >=
  145. RATE_CONTROL_EMERG_DEC) {
  146. rate_control_rate_dec(local, sta);
  147. }
  148. if (time_after(jiffies, srctrl->avg_rate_update + 60 * HZ)) {
  149. srctrl->avg_rate_update = jiffies;
  150. if (srctrl->tx_avg_rate_num > 0) {
  151. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  152. DECLARE_MAC_BUF(mac);
  153. printk(KERN_DEBUG "%s: STA %s Average rate: "
  154. "%d (%d/%d)\n",
  155. dev->name, print_mac(mac, sta->addr),
  156. srctrl->tx_avg_rate_sum /
  157. srctrl->tx_avg_rate_num,
  158. srctrl->tx_avg_rate_sum,
  159. srctrl->tx_avg_rate_num);
  160. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  161. srctrl->tx_avg_rate_sum = 0;
  162. srctrl->tx_avg_rate_num = 0;
  163. }
  164. }
  165. unlock:
  166. rcu_read_unlock();
  167. }
  168. static void
  169. rate_control_simple_get_rate(void *priv, struct net_device *dev,
  170. struct ieee80211_supported_band *sband,
  171. struct sk_buff *skb,
  172. struct rate_selection *sel)
  173. {
  174. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  175. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  176. struct ieee80211_sub_if_data *sdata;
  177. struct sta_info *sta;
  178. int rateidx;
  179. u16 fc;
  180. rcu_read_lock();
  181. sta = sta_info_get(local, hdr->addr1);
  182. /* Send management frames and broadcast/multicast data using lowest
  183. * rate. */
  184. fc = le16_to_cpu(hdr->frame_control);
  185. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  186. is_multicast_ether_addr(hdr->addr1) || !sta) {
  187. sel->rate = rate_lowest(local, sband, sta);
  188. rcu_read_unlock();
  189. return;
  190. }
  191. /* If a forced rate is in effect, select it. */
  192. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  193. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
  194. sta->txrate_idx = sdata->bss->force_unicast_rateidx;
  195. rateidx = sta->txrate_idx;
  196. if (rateidx >= sband->n_bitrates)
  197. rateidx = sband->n_bitrates - 1;
  198. sta->last_txrate_idx = rateidx;
  199. rcu_read_unlock();
  200. sel->rate = &sband->bitrates[rateidx];
  201. }
  202. static void rate_control_simple_rate_init(void *priv, void *priv_sta,
  203. struct ieee80211_local *local,
  204. struct sta_info *sta)
  205. {
  206. struct ieee80211_supported_band *sband;
  207. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  208. /* TODO: This routine should consider using RSSI from previous packets
  209. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  210. * Until that method is implemented, we will use the lowest supported rate
  211. * as a workaround, */
  212. sta->txrate_idx = rate_lowest_index(local, sband, sta);
  213. }
  214. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  215. {
  216. struct global_rate_control *rctrl;
  217. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  218. return rctrl;
  219. }
  220. static void rate_control_simple_free(void *priv)
  221. {
  222. struct global_rate_control *rctrl = priv;
  223. kfree(rctrl);
  224. }
  225. static void rate_control_simple_clear(void *priv)
  226. {
  227. }
  228. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  229. {
  230. struct sta_rate_control *rctrl;
  231. rctrl = kzalloc(sizeof(*rctrl), gfp);
  232. return rctrl;
  233. }
  234. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  235. {
  236. struct sta_rate_control *rctrl = priv_sta;
  237. kfree(rctrl);
  238. }
  239. #ifdef CONFIG_MAC80211_DEBUGFS
  240. static int open_file_generic(struct inode *inode, struct file *file)
  241. {
  242. file->private_data = inode->i_private;
  243. return 0;
  244. }
  245. static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
  246. char __user *userbuf,
  247. size_t count, loff_t *ppos)
  248. {
  249. struct sta_rate_control *srctrl = file->private_data;
  250. char buf[20];
  251. sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
  252. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  253. }
  254. static const struct file_operations sta_tx_avg_rate_sum_ops = {
  255. .read = sta_tx_avg_rate_sum_read,
  256. .open = open_file_generic,
  257. };
  258. static ssize_t sta_tx_avg_rate_num_read(struct file *file,
  259. char __user *userbuf,
  260. size_t count, loff_t *ppos)
  261. {
  262. struct sta_rate_control *srctrl = file->private_data;
  263. char buf[20];
  264. sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
  265. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  266. }
  267. static const struct file_operations sta_tx_avg_rate_num_ops = {
  268. .read = sta_tx_avg_rate_num_read,
  269. .open = open_file_generic,
  270. };
  271. static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
  272. struct dentry *dir)
  273. {
  274. struct sta_rate_control *srctrl = priv_sta;
  275. srctrl->tx_avg_rate_num_dentry =
  276. debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
  277. dir, srctrl, &sta_tx_avg_rate_num_ops);
  278. srctrl->tx_avg_rate_sum_dentry =
  279. debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
  280. dir, srctrl, &sta_tx_avg_rate_sum_ops);
  281. }
  282. static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
  283. {
  284. struct sta_rate_control *srctrl = priv_sta;
  285. debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
  286. debugfs_remove(srctrl->tx_avg_rate_num_dentry);
  287. }
  288. #endif
  289. static struct rate_control_ops mac80211_rcsimple = {
  290. .name = "simple",
  291. .tx_status = rate_control_simple_tx_status,
  292. .get_rate = rate_control_simple_get_rate,
  293. .rate_init = rate_control_simple_rate_init,
  294. .clear = rate_control_simple_clear,
  295. .alloc = rate_control_simple_alloc,
  296. .free = rate_control_simple_free,
  297. .alloc_sta = rate_control_simple_alloc_sta,
  298. .free_sta = rate_control_simple_free_sta,
  299. #ifdef CONFIG_MAC80211_DEBUGFS
  300. .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
  301. .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
  302. #endif
  303. };
  304. MODULE_LICENSE("GPL");
  305. MODULE_DESCRIPTION("Simple rate control algorithm");
  306. int __init rc80211_simple_init(void)
  307. {
  308. return ieee80211_rate_control_register(&mac80211_rcsimple);
  309. }
  310. void rc80211_simple_exit(void)
  311. {
  312. ieee80211_rate_control_unregister(&mac80211_rcsimple);
  313. }
  314. #ifdef CONFIG_MAC80211_RC_SIMPLE_MODULE
  315. module_init(rc80211_simple_init);
  316. module_exit(rc80211_simple_exit);
  317. #endif