rate.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
  140. #endif
  141. ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
  142. if (!ref->priv)
  143. goto fail_priv;
  144. return ref;
  145. fail_priv:
  146. ieee80211_rate_control_ops_put(ref->ops);
  147. fail_ops:
  148. kfree(ref);
  149. fail_ref:
  150. return NULL;
  151. }
  152. static void rate_control_release(struct kref *kref)
  153. {
  154. struct rate_control_ref *ctrl_ref;
  155. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  156. ctrl_ref->ops->free(ctrl_ref->priv);
  157. #ifdef CONFIG_MAC80211_DEBUGFS
  158. debugfs_remove_recursive(ctrl_ref->local->debugfs.rcdir);
  159. ctrl_ref->local->debugfs.rcdir = NULL;
  160. #endif
  161. ieee80211_rate_control_ops_put(ctrl_ref->ops);
  162. kfree(ctrl_ref);
  163. }
  164. static bool rc_no_data_or_no_ack(struct ieee80211_tx_rate_control *txrc)
  165. {
  166. struct sk_buff *skb = txrc->skb;
  167. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  168. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  169. __le16 fc;
  170. fc = hdr->frame_control;
  171. return ((info->flags & IEEE80211_TX_CTL_NO_ACK) || !ieee80211_is_data(fc));
  172. }
  173. static void rc_send_low_broadcast(s8 *idx, u32 basic_rates, u8 max_rate_idx)
  174. {
  175. u8 i;
  176. if (basic_rates == 0)
  177. return; /* assume basic rates unknown and accept rate */
  178. if (*idx < 0)
  179. return;
  180. if (basic_rates & (1 << *idx))
  181. return; /* selected rate is a basic rate */
  182. for (i = *idx + 1; i <= max_rate_idx; i++) {
  183. if (basic_rates & (1 << i)) {
  184. *idx = i;
  185. return;
  186. }
  187. }
  188. /* could not find a basic rate; use original selection */
  189. }
  190. bool rate_control_send_low(struct ieee80211_sta *sta,
  191. void *priv_sta,
  192. struct ieee80211_tx_rate_control *txrc)
  193. {
  194. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  195. if (!sta || !priv_sta || rc_no_data_or_no_ack(txrc)) {
  196. info->control.rates[0].idx = rate_lowest_index(txrc->sband, sta);
  197. info->control.rates[0].count =
  198. (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  199. 1 : txrc->hw->max_rate_tries;
  200. if (!sta && txrc->ap)
  201. rc_send_low_broadcast(&info->control.rates[0].idx,
  202. txrc->bss_conf->basic_rates,
  203. txrc->sband->n_bitrates);
  204. return true;
  205. }
  206. return false;
  207. }
  208. EXPORT_SYMBOL(rate_control_send_low);
  209. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  210. struct sta_info *sta,
  211. struct ieee80211_tx_rate_control *txrc)
  212. {
  213. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  214. void *priv_sta = NULL;
  215. struct ieee80211_sta *ista = NULL;
  216. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  217. int i;
  218. if (sta) {
  219. ista = &sta->sta;
  220. priv_sta = sta->rate_ctrl_priv;
  221. }
  222. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  223. info->control.rates[i].idx = -1;
  224. info->control.rates[i].flags = 0;
  225. info->control.rates[i].count = 1;
  226. }
  227. if (sta && sdata->force_unicast_rateidx > -1) {
  228. info->control.rates[0].idx = sdata->force_unicast_rateidx;
  229. } else {
  230. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  231. info->flags |= IEEE80211_TX_INTFL_RCALGO;
  232. }
  233. /*
  234. * try to enforce the maximum rate the user wanted
  235. */
  236. if (sdata->max_ratectrl_rateidx > -1)
  237. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  238. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS)
  239. continue;
  240. info->control.rates[i].idx =
  241. min_t(s8, info->control.rates[i].idx,
  242. sdata->max_ratectrl_rateidx);
  243. }
  244. BUG_ON(info->control.rates[0].idx < 0);
  245. }
  246. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  247. {
  248. kref_get(&ref->kref);
  249. return ref;
  250. }
  251. void rate_control_put(struct rate_control_ref *ref)
  252. {
  253. kref_put(&ref->kref, rate_control_release);
  254. }
  255. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  256. const char *name)
  257. {
  258. struct rate_control_ref *ref, *old;
  259. ASSERT_RTNL();
  260. if (local->open_count)
  261. return -EBUSY;
  262. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
  263. if (WARN_ON(!local->ops->set_rts_threshold))
  264. return -EINVAL;
  265. return 0;
  266. }
  267. ref = rate_control_alloc(name, local);
  268. if (!ref) {
  269. printk(KERN_WARNING "%s: Failed to select rate control "
  270. "algorithm\n", wiphy_name(local->hw.wiphy));
  271. return -ENOENT;
  272. }
  273. old = local->rate_ctrl;
  274. local->rate_ctrl = ref;
  275. if (old) {
  276. rate_control_put(old);
  277. sta_info_flush(local, NULL);
  278. }
  279. printk(KERN_DEBUG "%s: Selected rate control "
  280. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  281. ref->ops->name);
  282. return 0;
  283. }
  284. void rate_control_deinitialize(struct ieee80211_local *local)
  285. {
  286. struct rate_control_ref *ref;
  287. ref = local->rate_ctrl;
  288. if (!ref)
  289. return;
  290. local->rate_ctrl = NULL;
  291. rate_control_put(ref);
  292. }