rc80211_simple.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "ieee80211_rate.h"
  18. #include "debugfs.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. static void rate_control_rate_inc(struct ieee80211_local *local,
  25. struct sta_info *sta)
  26. {
  27. struct ieee80211_sub_if_data *sdata;
  28. struct ieee80211_hw_mode *mode;
  29. int i = sta->txrate;
  30. int maxrate;
  31. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  32. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  33. /* forced unicast rate - do not change STA rate */
  34. return;
  35. }
  36. mode = local->oper_hw_mode;
  37. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  38. if (i > mode->num_rates)
  39. i = mode->num_rates - 2;
  40. while (i + 1 < mode->num_rates) {
  41. i++;
  42. if (sta->supp_rates & BIT(i) &&
  43. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED &&
  44. (maxrate < 0 || i <= maxrate)) {
  45. sta->txrate = i;
  46. break;
  47. }
  48. }
  49. }
  50. static void rate_control_rate_dec(struct ieee80211_local *local,
  51. struct sta_info *sta)
  52. {
  53. struct ieee80211_sub_if_data *sdata;
  54. struct ieee80211_hw_mode *mode;
  55. int i = sta->txrate;
  56. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  57. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  58. /* forced unicast rate - do not change STA rate */
  59. return;
  60. }
  61. mode = local->oper_hw_mode;
  62. if (i > mode->num_rates)
  63. i = mode->num_rates;
  64. while (i > 0) {
  65. i--;
  66. if (sta->supp_rates & BIT(i) &&
  67. mode->rates[i].flags & IEEE80211_RATE_SUPPORTED) {
  68. sta->txrate = i;
  69. break;
  70. }
  71. }
  72. }
  73. static struct ieee80211_rate *
  74. rate_control_lowest_rate(struct ieee80211_local *local,
  75. struct ieee80211_hw_mode *mode)
  76. {
  77. int i;
  78. for (i = 0; i < mode->num_rates; i++) {
  79. struct ieee80211_rate *rate = &mode->rates[i];
  80. if (rate->flags & IEEE80211_RATE_SUPPORTED)
  81. return rate;
  82. }
  83. printk(KERN_DEBUG "rate_control_lowest_rate - no supported rates "
  84. "found\n");
  85. return &mode->rates[0];
  86. }
  87. struct global_rate_control {
  88. int dummy;
  89. };
  90. struct sta_rate_control {
  91. unsigned long last_rate_change;
  92. u32 tx_num_failures;
  93. u32 tx_num_xmit;
  94. unsigned long avg_rate_update;
  95. u32 tx_avg_rate_sum;
  96. u32 tx_avg_rate_num;
  97. #ifdef CONFIG_MAC80211_DEBUGFS
  98. struct dentry *tx_avg_rate_sum_dentry;
  99. struct dentry *tx_avg_rate_num_dentry;
  100. #endif
  101. };
  102. static void rate_control_simple_tx_status(void *priv, struct net_device *dev,
  103. struct sk_buff *skb,
  104. struct ieee80211_tx_status *status)
  105. {
  106. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  107. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  108. struct sta_info *sta;
  109. struct sta_rate_control *srctrl;
  110. sta = sta_info_get(local, hdr->addr1);
  111. if (!sta)
  112. return;
  113. srctrl = sta->rate_ctrl_priv;
  114. srctrl->tx_num_xmit++;
  115. if (status->excessive_retries) {
  116. srctrl->tx_num_failures++;
  117. sta->tx_retry_failed++;
  118. sta->tx_num_consecutive_failures++;
  119. sta->tx_num_mpdu_fail++;
  120. } else {
  121. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  122. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  123. sta->last_ack_rssi[2] = status->ack_signal;
  124. sta->tx_num_consecutive_failures = 0;
  125. sta->tx_num_mpdu_ok++;
  126. }
  127. sta->tx_retry_count += status->retry_count;
  128. sta->tx_num_mpdu_fail += status->retry_count;
  129. if (time_after(jiffies,
  130. srctrl->last_rate_change + RATE_CONTROL_INTERVAL) &&
  131. srctrl->tx_num_xmit > RATE_CONTROL_MIN_TX) {
  132. u32 per_failed;
  133. srctrl->last_rate_change = jiffies;
  134. per_failed = (100 * sta->tx_num_mpdu_fail) /
  135. (sta->tx_num_mpdu_fail + sta->tx_num_mpdu_ok);
  136. /* TODO: calculate average per_failed to make adjusting
  137. * parameters easier */
  138. #if 0
  139. if (net_ratelimit()) {
  140. printk(KERN_DEBUG "MPDU fail=%d ok=%d per_failed=%d\n",
  141. sta->tx_num_mpdu_fail, sta->tx_num_mpdu_ok,
  142. per_failed);
  143. }
  144. #endif
  145. /*
  146. * XXX: Make these configurable once we have an
  147. * interface to the rate control algorithms
  148. */
  149. if (per_failed > RATE_CONTROL_NUM_DOWN) {
  150. rate_control_rate_dec(local, sta);
  151. } else if (per_failed < RATE_CONTROL_NUM_UP) {
  152. rate_control_rate_inc(local, sta);
  153. }
  154. srctrl->tx_avg_rate_sum += status->control.rate->rate;
  155. srctrl->tx_avg_rate_num++;
  156. srctrl->tx_num_failures = 0;
  157. srctrl->tx_num_xmit = 0;
  158. } else if (sta->tx_num_consecutive_failures >=
  159. RATE_CONTROL_EMERG_DEC) {
  160. rate_control_rate_dec(local, sta);
  161. }
  162. if (srctrl->avg_rate_update + 60 * HZ < jiffies) {
  163. srctrl->avg_rate_update = jiffies;
  164. if (srctrl->tx_avg_rate_num > 0) {
  165. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  166. DECLARE_MAC_BUF(mac);
  167. printk(KERN_DEBUG "%s: STA %s Average rate: "
  168. "%d (%d/%d)\n",
  169. dev->name, print_mac(mac, 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: This routine should consider using RSSI from previous packets
  231. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  232. * Until that method is implemented, we will use the lowest supported rate
  233. * as a workaround, */
  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. break;
  239. }
  240. }
  241. }
  242. static void * rate_control_simple_alloc(struct ieee80211_local *local)
  243. {
  244. struct global_rate_control *rctrl;
  245. rctrl = kzalloc(sizeof(*rctrl), GFP_ATOMIC);
  246. return rctrl;
  247. }
  248. static void rate_control_simple_free(void *priv)
  249. {
  250. struct global_rate_control *rctrl = priv;
  251. kfree(rctrl);
  252. }
  253. static void rate_control_simple_clear(void *priv)
  254. {
  255. }
  256. static void * rate_control_simple_alloc_sta(void *priv, gfp_t gfp)
  257. {
  258. struct sta_rate_control *rctrl;
  259. rctrl = kzalloc(sizeof(*rctrl), gfp);
  260. return rctrl;
  261. }
  262. static void rate_control_simple_free_sta(void *priv, void *priv_sta)
  263. {
  264. struct sta_rate_control *rctrl = priv_sta;
  265. kfree(rctrl);
  266. }
  267. #ifdef CONFIG_MAC80211_DEBUGFS
  268. static int open_file_generic(struct inode *inode, struct file *file)
  269. {
  270. file->private_data = inode->i_private;
  271. return 0;
  272. }
  273. static ssize_t sta_tx_avg_rate_sum_read(struct file *file,
  274. char __user *userbuf,
  275. size_t count, loff_t *ppos)
  276. {
  277. struct sta_rate_control *srctrl = file->private_data;
  278. char buf[20];
  279. sprintf(buf, "%d\n", srctrl->tx_avg_rate_sum);
  280. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  281. }
  282. static const struct file_operations sta_tx_avg_rate_sum_ops = {
  283. .read = sta_tx_avg_rate_sum_read,
  284. .open = open_file_generic,
  285. };
  286. static ssize_t sta_tx_avg_rate_num_read(struct file *file,
  287. char __user *userbuf,
  288. size_t count, loff_t *ppos)
  289. {
  290. struct sta_rate_control *srctrl = file->private_data;
  291. char buf[20];
  292. sprintf(buf, "%d\n", srctrl->tx_avg_rate_num);
  293. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  294. }
  295. static const struct file_operations sta_tx_avg_rate_num_ops = {
  296. .read = sta_tx_avg_rate_num_read,
  297. .open = open_file_generic,
  298. };
  299. static void rate_control_simple_add_sta_debugfs(void *priv, void *priv_sta,
  300. struct dentry *dir)
  301. {
  302. struct sta_rate_control *srctrl = priv_sta;
  303. srctrl->tx_avg_rate_num_dentry =
  304. debugfs_create_file("rc_simple_sta_tx_avg_rate_num", 0400,
  305. dir, srctrl, &sta_tx_avg_rate_num_ops);
  306. srctrl->tx_avg_rate_sum_dentry =
  307. debugfs_create_file("rc_simple_sta_tx_avg_rate_sum", 0400,
  308. dir, srctrl, &sta_tx_avg_rate_sum_ops);
  309. }
  310. static void rate_control_simple_remove_sta_debugfs(void *priv, void *priv_sta)
  311. {
  312. struct sta_rate_control *srctrl = priv_sta;
  313. debugfs_remove(srctrl->tx_avg_rate_sum_dentry);
  314. debugfs_remove(srctrl->tx_avg_rate_num_dentry);
  315. }
  316. #endif
  317. struct rate_control_ops mac80211_rcsimple = {
  318. .name = "simple",
  319. .tx_status = rate_control_simple_tx_status,
  320. .get_rate = rate_control_simple_get_rate,
  321. .rate_init = rate_control_simple_rate_init,
  322. .clear = rate_control_simple_clear,
  323. .alloc = rate_control_simple_alloc,
  324. .free = rate_control_simple_free,
  325. .alloc_sta = rate_control_simple_alloc_sta,
  326. .free_sta = rate_control_simple_free_sta,
  327. #ifdef CONFIG_MAC80211_DEBUGFS
  328. .add_sta_debugfs = rate_control_simple_add_sta_debugfs,
  329. .remove_sta_debugfs = rate_control_simple_remove_sta_debugfs,
  330. #endif
  331. };