rc80211_simple.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /* This is a minimal implementation of TX rate controlling that can be used
  20. * as the default when no improved mechanisms are available. */
  21. #define RATE_CONTROL_EMERG_DEC 2
  22. #define RATE_CONTROL_INTERVAL (HZ / 20)
  23. #define RATE_CONTROL_MIN_TX 10
  24. MODULE_ALIAS("rc80211_default");
  25. static void rate_control_rate_inc(struct ieee80211_local *local,
  26. struct sta_info *sta)
  27. {
  28. struct ieee80211_sub_if_data *sdata;
  29. struct ieee80211_hw_mode *mode;
  30. int i = sta->txrate;
  31. int maxrate;
  32. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  33. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  34. /* forced unicast rate - do not change STA rate */
  35. return;
  36. }
  37. mode = local->oper_hw_mode;
  38. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  39. if (i > mode->num_rates)
  40. i = mode->num_rates - 2;
  41. while (i + 1 < mode->num_rates) {
  42. i++;
  43. if (sta->supp_rates & BIT(i) &&
  44. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
  45. (maxrate < 0 || i <= maxrate)) {
  46. sta->txrate = i;
  47. break;
  48. }
  49. }
  50. }
  51. static void rate_control_rate_dec(struct ieee80211_local *local,
  52. struct sta_info *sta)
  53. {
  54. struct ieee80211_sub_if_data *sdata;
  55. struct ieee80211_hw_mode *mode;
  56. int i = sta->txrate;
  57. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  58. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  59. /* forced unicast rate - do not change STA rate */
  60. return;
  61. }
  62. mode = local->oper_hw_mode;
  63. if (i > mode->num_rates)
  64. i = mode->num_rates;
  65. while (i > 0) {
  66. i--;
  67. if (sta->supp_rates & BIT(i) &&
  68. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
  69. sta->txrate = i;
  70. break;
  71. }
  72. }
  73. }
  74. static struct ieee80211_rate *
  75. rate_control_lowest_rate(struct ieee80211_local *local,
  76. struct ieee80211_hw_mode *mode)
  77. {
  78. int i;
  79. for (i = 0; i < mode->num_rates; i++) {
  80. struct ieee80211_rate *rate = &mode->rates[i];
  81. if (rate->flags & IEEE80211_RATE_SUPPORTED)
  82. return rate;
  83. }
  84. printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
  85. "found\n");
  86. return &mode->rates[0];
  87. }
  88. struct global_rate_control {
  89. int dummy;
  90. };
  91. struct sta_rate_control {
  92. unsigned long last_rate_change;
  93. u32 tx_num_failures;
  94. u32 tx_num_xmit;
  95. unsigned long avg_rate_update;
  96. u32 tx_avg_rate_sum;
  97. u32 tx_avg_rate_num;
  98. };
  99. static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
  100. struct sk_buff *skb,
  101. struct ieee80211_tx_status *status)
  102. {
  103. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  104. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  105. struct sta_info *sta;
  106. struct sta_rate_control *srctrl;
  107. sta = sta_info_get(local, hdr->addr1);
  108. if (!sta)
  109. return;
  110. srctrl = sta->rate_ctrl_priv;
  111. srctrl->tx_num_xmit++;
  112. if (status->excessive_retries) {
  113. sta->antenna_sel_tx = sta->antenna_sel_tx == 1 ? 2 : 1;
  114. sta->antenna_sel_rx = sta->antenna_sel_rx == 1 ? 2 : 1;
  115. if (local->sta_antenna_sel == STA_ANTENNA_SEL_SW_CTRL_DEBUG) {
  116. printk(KERN_DEBUG "%s: " MAC_FMT " TX antenna --> %d "
  117. "RX antenna --> %d (@%lu)\n",
  118. dev->name, MAC_ARG(hdr->addr1),
  119. sta->antenna_sel_tx, sta->antenna_sel_rx, jiffies);
  120. }
  121. srctrl->tx_num_failures++;
  122. sta->tx_retry_failed++;
  123. sta->tx_num_consecutive_failures++;
  124. sta->tx_num_mpdu_fail++;
  125. } else {
  126. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  127. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  128. sta->last_ack_rssi[2] = status->ack_signal;
  129. sta->tx_num_consecutive_failures = 0;
  130. sta->tx_num_mpdu_ok++;
  131. }
  132. sta->tx_retry_count += status->retry_count;
  133. sta->tx_num_mpdu_fail += status->retry_count;
  134. if (time_after(jiffies,
  135. srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
  136. srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
  137. u32 per_failed;
  138. srctrl->last_rate_change = jiffies;
  139. per_failed = (100 * sta->tx_num_mpdu_fail) /
  140. (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
  141. /* TODO: calculate average per_failed to make adjusting
  142. * parameters easier */
  143. #if 0
  144. if (net_ratelimit()) {
  145. printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
  146. sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
  147. per_failed);
  148. }
  149. #endif
  150. if (per_failed > local->rate_ctrl_num_down) {
  151. rate_control_rate_dec(local, sta);
  152. } else if (per_failed < local->rate_ctrl_num_up) {
  153. rate_control_rate_inc(local, sta);
  154. }
  155. srctrl->tx_avg_rate_sum += status->control.rate->rate;
  156. srctrl->tx_avg_rate_num++;
  157. srctrl->tx_num_failures = 0;
  158. srctrl->tx_num_xmit = 0;
  159. } else if (sta->tx_num_consecutive_failures >=
  160. RATE_CONTROL_EMERG_DEC) {
  161. rate_control_rate_dec(local, sta);
  162. }
  163. if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
  164. srctrl->avg_rate_update = jiffies;
  165. if (srctrl->tx_avg_rate_num > 0) {
  166. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  167. printk(KERN_DEBUG "%s: STA " MAC_FMT " Average rate: "
  168. "%d (%d/%d)\n",
  169. dev->name, MAC_ARG(sta->addr),
  170. srctrl->tx_avg_rate_sum /
  171. srctrl->tx_avg_rate_num,
  172. srctrl->tx_avg_rate_sum,
  173. srctrl->tx_avg_rate_num);
  174. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  175. srctrl->tx_avg_rate_sum = 0;
  176. srctrl->tx_avg_rate_num = 0;
  177. }
  178. }
  179. sta_info_put(sta);
  180. }
  181. static struct ieee80211_rate *
  182. rate_control_simple_get_rate(void *priv, struct net_device *dev,
  183. struct sk_buff *skb,
  184. struct rate_control_extra *extra)
  185. {
  186. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  187. struct ieee80211_sub_if_data *sdata;
  188. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  189. struct ieee80211_hw_mode *mode = extra->mode;
  190. struct sta_info *sta;
  191. int rateidx, nonerp_idx;
  192. u16 fc;
  193. memset(extra, 0, sizeof(*extra));
  194. fc = le16_to_cpu(hdr->frame_control);
  195. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  196. (hdr->addr1[0] & 0x01)) {
  197. /* Send management frames and broadcast/multicast data using
  198. * lowest rate. */
  199. /* TODO: this could probably be improved.. */
  200. return rate_control_lowest_rate(local, mode);
  201. }
  202. sta = sta_info_get(local, hdr->addr1);
  203. if (!sta)
  204. return rate_control_lowest_rate(local, mode);
  205. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  206. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1)
  207. sta->txrate = sdata->bss->force_unicast_rateidx;
  208. rateidx = sta->txrate;
  209. if (rateidx >= mode->num_rates)
  210. rateidx = mode->num_rates - 1;
  211. sta->last_txrate = rateidx;
  212. nonerp_idx = rateidx;
  213. while (nonerp_idx > 0 &&
  214. ((mode->rates[nonerp_idx].flags & IEEE80211_RATE_ERP) ||
  215. !(mode->rates[nonerp_idx].flags & IEEE80211_RATE_SUPPORTED) ||
  216. !(sta->supp_rates & BIT(nonerp_idx))))
  217. nonerp_idx--;
  218. extra->nonerp = &mode->rates[nonerp_idx];
  219. sta_info_put(sta);
  220. return &mode->rates[rateidx];
  221. }
  222. static void rate_control_simple_rate_init(void *priv, void *priv_sta,
  223. struct ieee80211_local *local,
  224. struct sta_info *sta)
  225. {
  226. struct ieee80211_hw_mode *mode;
  227. int i;
  228. sta->txrate = 0;
  229. mode = local->oper_hw_mode;
  230. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  231. * the lowest or the highest rate.. Could consider using RSSI from
  232. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  233. * after assoc.. */
  234. for (i = 0; i < mode->num_rates; i++) {
  235. if ((sta->supp_rates & BIT(i)) &&
  236. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
  237. sta->txrate = i;
  238. }
  239. }
  240. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  241. {
  242. struct global_rate_control *rctrl;
  243. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  244. return rctrl;
  245. }
  246. static void rate_control_simple_free(void *priv)
  247. {
  248. struct global_rate_control *rctrl = priv;
  249. kfree(rctrl);
  250. }
  251. static void rate_control_simple_clear(void *priv)
  252. {
  253. }
  254. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  255. {
  256. struct sta_rate_control *rctrl;
  257. rctrl = kzalloc(sizeof(*rctrl), gfp);
  258. return rctrl;
  259. }
  260. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  261. {
  262. struct sta_rate_control *rctrl = priv_sta;
  263. kfree(rctrl);
  264. }
  265. static struct rate_control_ops rate_control_simple = {
  266. .module = THIS_MODULE,
  267. .name = "simple",
  268. .tx_status = rate_control_simple_tx_status,
  269. .get_rate = rate_control_simple_get_rate,
  270. .rate_init = rate_control_simple_rate_init,
  271. .clear = rate_control_simple_clear,
  272. .alloc = rate_control_simple_alloc,
  273. .free = rate_control_simple_free,
  274. .alloc_sta = rate_control_simple_alloc_sta,
  275. .free_sta = rate_control_simple_free_sta,
  276. };
  277. static int __init rate_control_simple_init(void)
  278. {
  279. return ieee80211_rate_control_register(&rate_control_simple);
  280. }
  281. static void __exit rate_control_simple_exit(void)
  282. {
  283. ieee80211_rate_control_unregister(&rate_control_simple);
  284. }
  285. module_init(rate_control_simple_init);
  286. module_exit(rate_control_simple_exit);
  287. MODULE_DESCRIPTION("Simple rate control algorithm for ieee80211");
  288. MODULE_LICENSE("GPL");