rate.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/rtnetlink.h>
  12. #include "rate.h"
  13. #include "ieee80211_i.h"
  14. #include "debugfs.h"
  15. struct rate_control_alg {
  16. struct list_head list;
  17. struct rate_control_ops *ops;
  18. };
  19. static LIST_HEAD(rate_ctrl_algs);
  20. static DEFINE_MUTEX(rate_ctrl_mutex);
  21. static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
  22. module_param(ieee80211_default_rc_algo, charp, 0644);
  23. MODULE_PARM_DESC(ieee80211_default_rc_algo,
  24. "Default rate control algorithm for mac80211 to use");
  25. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  26. {
  27. struct rate_control_alg *alg;
  28. if (!ops->name)
  29. return -EINVAL;
  30. mutex_lock(&rate_ctrl_mutex);
  31. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  32. if (!strcmp(alg->ops->name, ops->name)) {
  33. /* don't register an algorithm twice */
  34. WARN_ON(1);
  35. mutex_unlock(&rate_ctrl_mutex);
  36. return -EALREADY;
  37. }
  38. }
  39. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  40. if (alg == NULL) {
  41. mutex_unlock(&rate_ctrl_mutex);
  42. return -ENOMEM;
  43. }
  44. alg->ops = ops;
  45. list_add_tail(&alg->list, &rate_ctrl_algs);
  46. mutex_unlock(&rate_ctrl_mutex);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(ieee80211_rate_control_register);
  50. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  51. {
  52. struct rate_control_alg *alg;
  53. mutex_lock(&rate_ctrl_mutex);
  54. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  55. if (alg->ops == ops) {
  56. list_del(&alg->list);
  57. kfree(alg);
  58. break;
  59. }
  60. }
  61. mutex_unlock(&rate_ctrl_mutex);
  62. }
  63. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  64. static struct rate_control_ops *
  65. ieee80211_try_rate_control_ops_get(const char *name)
  66. {
  67. struct rate_control_alg *alg;
  68. struct rate_control_ops *ops = NULL;
  69. if (!name)
  70. return NULL;
  71. mutex_lock(&rate_ctrl_mutex);
  72. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  73. if (!strcmp(alg->ops->name, name))
  74. if (try_module_get(alg->ops->module)) {
  75. ops = alg->ops;
  76. break;
  77. }
  78. }
  79. mutex_unlock(&rate_ctrl_mutex);
  80. return ops;
  81. }
  82. /* Get the rate control algorithm. */
  83. static struct rate_control_ops *
  84. ieee80211_rate_control_ops_get(const char *name)
  85. {
  86. struct rate_control_ops *ops;
  87. const char *alg_name;
  88. if (!name)
  89. alg_name = ieee80211_default_rc_algo;
  90. else
  91. alg_name = name;
  92. ops = ieee80211_try_rate_control_ops_get(alg_name);
  93. if (!ops) {
  94. request_module("rc80211_%s", alg_name);
  95. ops = ieee80211_try_rate_control_ops_get(alg_name);
  96. }
  97. if (!ops && name)
  98. /* try default if specific alg requested but not found */
  99. ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
  100. /* try built-in one if specific alg requested but not found */
  101. if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
  102. ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
  103. return ops;
  104. }
  105. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  106. {
  107. module_put(ops->module);
  108. }
  109. #ifdef CONFIG_MAC80211_DEBUGFS
  110. static ssize_t rcname_read(struct file *file, char __user *userbuf,
  111. size_t count, loff_t *ppos)
  112. {
  113. struct rate_control_ref *ref = file->private_data;
  114. int len = strlen(ref->ops->name);
  115. return simple_read_from_buffer(userbuf, count, ppos,
  116. ref->ops->name, len);
  117. }
  118. static const struct file_operations rcname_ops = {
  119. .read = rcname_read,
  120. .open = mac80211_open_file_generic,
  121. };
  122. #endif
  123. struct rate_control_ref *rate_control_alloc(const char *name,
  124. struct ieee80211_local *local)
  125. {
  126. struct dentry *debugfsdir = NULL;
  127. struct rate_control_ref *ref;
  128. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  129. if (!ref)
  130. goto fail_ref;
  131. kref_init(&ref->kref);
  132. ref->local = local;
  133. ref->ops = ieee80211_rate_control_ops_get(name);
  134. if (!ref->ops)
  135. goto fail_ops;
  136. #ifdef CONFIG_MAC80211_DEBUGFS
  137. debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
  138. local->debugfs.rcdir = debugfsdir;
  139. local->debugfs.rcname = debugfs_create_file("name", 0400, debugfsdir,
  140. ref, &rcname_ops);
  141. #endif
  142. ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
  143. if (!ref->priv)
  144. goto fail_priv;
  145. return ref;
  146. fail_priv:
  147. ieee80211_rate_control_ops_put(ref->ops);
  148. fail_ops:
  149. kfree(ref);
  150. fail_ref:
  151. return NULL;
  152. }
  153. static void rate_control_release(struct kref *kref)
  154. {
  155. struct rate_control_ref *ctrl_ref;
  156. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  157. ctrl_ref->ops->free(ctrl_ref->priv);
  158. #ifdef CONFIG_MAC80211_DEBUGFS
  159. debugfs_remove(ctrl_ref->local->debugfs.rcname);
  160. ctrl_ref->local->debugfs.rcname = NULL;
  161. debugfs_remove(ctrl_ref->local->debugfs.rcdir);
  162. ctrl_ref->local->debugfs.rcdir = NULL;
  163. #endif
  164. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  165. kfree(ctrl_ref);
  166. }
  167. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  168. struct sta_info *sta,
  169. struct ieee80211_tx_rate_control *txrc)
  170. {
  171. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  172. void *priv_sta = NULL;
  173. struct ieee80211_sta *ista = NULL;
  174. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  175. int i;
  176. if (sta) {
  177. ista = &sta->sta;
  178. priv_sta = sta->rate_ctrl_priv;
  179. }
  180. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  181. info->control.rates[i].idx = -1;
  182. info->control.rates[i].flags = 0;
  183. info->control.rates[i].count = 1;
  184. }
  185. if (sta && sdata->force_unicast_rateidx > -1) {
  186. info->control.rates[0].idx = sdata->force_unicast_rateidx;
  187. } else {
  188. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  189. info->flags |= IEEE80211_TX_INTFL_RCALGO;
  190. }
  191. /*
  192. * try to enforce the maximum rate the user wanted
  193. */
  194. if (sdata->max_ratectrl_rateidx > -1)
  195. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  196. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS)
  197. continue;
  198. info->control.rates[i].idx =
  199. min_t(s8, info->control.rates[i].idx,
  200. sdata->max_ratectrl_rateidx);
  201. }
  202. BUG_ON(info->control.rates[0].idx < 0);
  203. }
  204. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  205. {
  206. kref_get(&ref->kref);
  207. return ref;
  208. }
  209. void rate_control_put(struct rate_control_ref *ref)
  210. {
  211. kref_put(&ref->kref, rate_control_release);
  212. }
  213. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  214. const char *name)
  215. {
  216. struct rate_control_ref *ref, *old;
  217. ASSERT_RTNL();
  218. if (local->open_count || netif_running(local->mdev))
  219. return -EBUSY;
  220. ref = rate_control_alloc(name, local);
  221. if (!ref) {
  222. printk(KERN_WARNING "%s: Failed to select rate control "
  223. "algorithm\n", wiphy_name(local->hw.wiphy));
  224. return -ENOENT;
  225. }
  226. old = local->rate_ctrl;
  227. local->rate_ctrl = ref;
  228. if (old) {
  229. rate_control_put(old);
  230. sta_info_flush(local, NULL);
  231. }
  232. printk(KERN_DEBUG "%s: Selected rate control "
  233. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  234. ref->ops->name);
  235. return 0;
  236. }
  237. void rate_control_deinitialize(struct ieee80211_local *local)
  238. {
  239. struct rate_control_ref *ref;
  240. ref = local->rate_ctrl;
  241. local->rate_ctrl = NULL;
  242. rate_control_put(ref);
  243. }