rc80211_simple.c 10 KB

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