rate.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. struct rate_control_alg {
  15. struct list_head list;
  16. struct rate_control_ops *ops;
  17. };
  18. static LIST_HEAD(rate_ctrl_algs);
  19. static DEFINE_MUTEX(rate_ctrl_mutex);
  20. static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
  21. module_param(ieee80211_default_rc_algo, charp, 0644);
  22. MODULE_PARM_DESC(ieee80211_default_rc_algo,
  23. "Default rate control algorithm for mac80211 to use");
  24. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  25. {
  26. struct rate_control_alg *alg;
  27. if (!ops->name)
  28. return -EINVAL;
  29. mutex_lock(&rate_ctrl_mutex);
  30. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  31. if (!strcmp(alg->ops->name, ops->name)) {
  32. /* don't register an algorithm twice */
  33. WARN_ON(1);
  34. mutex_unlock(&rate_ctrl_mutex);
  35. return -EALREADY;
  36. }
  37. }
  38. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  39. if (alg == NULL) {
  40. mutex_unlock(&rate_ctrl_mutex);
  41. return -ENOMEM;
  42. }
  43. alg->ops = ops;
  44. list_add_tail(&alg->list, &rate_ctrl_algs);
  45. mutex_unlock(&rate_ctrl_mutex);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(ieee80211_rate_control_register);
  49. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  50. {
  51. struct rate_control_alg *alg;
  52. mutex_lock(&rate_ctrl_mutex);
  53. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  54. if (alg->ops == ops) {
  55. list_del(&alg->list);
  56. kfree(alg);
  57. break;
  58. }
  59. }
  60. mutex_unlock(&rate_ctrl_mutex);
  61. }
  62. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  63. static struct rate_control_ops *
  64. ieee80211_try_rate_control_ops_get(const char *name)
  65. {
  66. struct rate_control_alg *alg;
  67. struct rate_control_ops *ops = NULL;
  68. if (!name)
  69. return NULL;
  70. mutex_lock(&rate_ctrl_mutex);
  71. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  72. if (!strcmp(alg->ops->name, name))
  73. if (try_module_get(alg->ops->module)) {
  74. ops = alg->ops;
  75. break;
  76. }
  77. }
  78. mutex_unlock(&rate_ctrl_mutex);
  79. return ops;
  80. }
  81. /* Get the rate control algorithm. */
  82. static struct rate_control_ops *
  83. ieee80211_rate_control_ops_get(const char *name)
  84. {
  85. struct rate_control_ops *ops;
  86. const char *alg_name;
  87. if (!name)
  88. alg_name = ieee80211_default_rc_algo;
  89. else
  90. alg_name = name;
  91. ops = ieee80211_try_rate_control_ops_get(alg_name);
  92. if (!ops) {
  93. request_module("rc80211_%s", alg_name);
  94. ops = ieee80211_try_rate_control_ops_get(alg_name);
  95. }
  96. if (!ops && name)
  97. /* try default if specific alg requested but not found */
  98. ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
  99. /* try built-in one if specific alg requested but not found */
  100. if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
  101. ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
  102. return ops;
  103. }
  104. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  105. {
  106. module_put(ops->module);
  107. }
  108. struct rate_control_ref *rate_control_alloc(const char *name,
  109. struct ieee80211_local *local)
  110. {
  111. struct rate_control_ref *ref;
  112. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  113. if (!ref)
  114. goto fail_ref;
  115. kref_init(&ref->kref);
  116. ref->ops = ieee80211_rate_control_ops_get(name);
  117. if (!ref->ops)
  118. goto fail_ops;
  119. ref->priv = ref->ops->alloc(local);
  120. if (!ref->priv)
  121. goto fail_priv;
  122. return ref;
  123. fail_priv:
  124. ieee80211_rate_control_ops_put(ref->ops);
  125. fail_ops:
  126. kfree(ref);
  127. fail_ref:
  128. return NULL;
  129. }
  130. static void rate_control_release(struct kref *kref)
  131. {
  132. struct rate_control_ref *ctrl_ref;
  133. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  134. ctrl_ref->ops->free(ctrl_ref->priv);
  135. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  136. kfree(ctrl_ref);
  137. }
  138. void rate_control_get_rate(struct net_device *dev,
  139. struct ieee80211_supported_band *sband,
  140. struct sk_buff *skb,
  141. struct rate_selection *sel)
  142. {
  143. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  144. struct rate_control_ref *ref = local->rate_ctrl;
  145. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  146. struct sta_info *sta;
  147. int i;
  148. rcu_read_lock();
  149. sta = sta_info_get(local, hdr->addr1);
  150. memset(sel, 0, sizeof(struct rate_selection));
  151. ref->ops->get_rate(ref->priv, dev, sband, skb, sel);
  152. /* Select a non-ERP backup rate. */
  153. if (!sel->nonerp) {
  154. for (i = 0; i < sband->n_bitrates; i++) {
  155. struct ieee80211_rate *rate = &sband->bitrates[i];
  156. if (sel->rate->bitrate < rate->bitrate)
  157. break;
  158. if (rate_supported(sta, sband->band, i) &&
  159. !(rate->flags & IEEE80211_RATE_ERP_G))
  160. sel->nonerp = rate;
  161. }
  162. }
  163. rcu_read_unlock();
  164. }
  165. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  166. {
  167. kref_get(&ref->kref);
  168. return ref;
  169. }
  170. void rate_control_put(struct rate_control_ref *ref)
  171. {
  172. kref_put(&ref->kref, rate_control_release);
  173. }
  174. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  175. const char *name)
  176. {
  177. struct rate_control_ref *ref, *old;
  178. ASSERT_RTNL();
  179. if (local->open_count || netif_running(local->mdev))
  180. return -EBUSY;
  181. ref = rate_control_alloc(name, local);
  182. if (!ref) {
  183. printk(KERN_WARNING "%s: Failed to select rate control "
  184. "algorithm\n", wiphy_name(local->hw.wiphy));
  185. return -ENOENT;
  186. }
  187. old = local->rate_ctrl;
  188. local->rate_ctrl = ref;
  189. if (old) {
  190. rate_control_put(old);
  191. sta_info_flush(local, NULL);
  192. }
  193. printk(KERN_DEBUG "%s: Selected rate control "
  194. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  195. ref->ops->name);
  196. return 0;
  197. }
  198. void rate_control_deinitialize(struct ieee80211_local *local)
  199. {
  200. struct rate_control_ref *ref;
  201. ref = local->rate_ctrl;
  202. local->rate_ctrl = NULL;
  203. rate_control_put(ref);
  204. }