rate.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <linux/slab.h>
  13. #include "rate.h"
  14. #include "ieee80211_i.h"
  15. #include "debugfs.h"
  16. struct rate_control_alg {
  17. struct list_head list;
  18. struct rate_control_ops *ops;
  19. };
  20. static LIST_HEAD(rate_ctrl_algs);
  21. static DEFINE_MUTEX(rate_ctrl_mutex);
  22. static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
  23. module_param(ieee80211_default_rc_algo, charp, 0644);
  24. MODULE_PARM_DESC(ieee80211_default_rc_algo,
  25. "Default rate control algorithm for mac80211 to use");
  26. int ieee80211_rate_control_register(struct rate_control_ops *ops)
  27. {
  28. struct rate_control_alg *alg;
  29. if (!ops->name)
  30. return -EINVAL;
  31. mutex_lock(&rate_ctrl_mutex);
  32. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  33. if (!strcmp(alg->ops->name, ops->name)) {
  34. /* don't register an algorithm twice */
  35. WARN_ON(1);
  36. mutex_unlock(&rate_ctrl_mutex);
  37. return -EALREADY;
  38. }
  39. }
  40. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  41. if (alg == NULL) {
  42. mutex_unlock(&rate_ctrl_mutex);
  43. return -ENOMEM;
  44. }
  45. alg->ops = ops;
  46. list_add_tail(&alg->list, &rate_ctrl_algs);
  47. mutex_unlock(&rate_ctrl_mutex);
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(ieee80211_rate_control_register);
  51. void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
  52. {
  53. struct rate_control_alg *alg;
  54. mutex_lock(&rate_ctrl_mutex);
  55. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  56. if (alg->ops == ops) {
  57. list_del(&alg->list);
  58. kfree(alg);
  59. break;
  60. }
  61. }
  62. mutex_unlock(&rate_ctrl_mutex);
  63. }
  64. EXPORT_SYMBOL(ieee80211_rate_control_unregister);
  65. static struct rate_control_ops *
  66. ieee80211_try_rate_control_ops_get(const char *name)
  67. {
  68. struct rate_control_alg *alg;
  69. struct rate_control_ops *ops = NULL;
  70. if (!name)
  71. return NULL;
  72. mutex_lock(&rate_ctrl_mutex);
  73. list_for_each_entry(alg, &rate_ctrl_algs, list) {
  74. if (!strcmp(alg->ops->name, name))
  75. if (try_module_get(alg->ops->module)) {
  76. ops = alg->ops;
  77. break;
  78. }
  79. }
  80. mutex_unlock(&rate_ctrl_mutex);
  81. return ops;
  82. }
  83. /* Get the rate control algorithm. */
  84. static struct rate_control_ops *
  85. ieee80211_rate_control_ops_get(const char *name)
  86. {
  87. struct rate_control_ops *ops;
  88. const char *alg_name;
  89. kparam_block_sysfs_write(ieee80211_default_rc_algo);
  90. if (!name)
  91. alg_name = ieee80211_default_rc_algo;
  92. else
  93. alg_name = name;
  94. ops = ieee80211_try_rate_control_ops_get(alg_name);
  95. if (!ops) {
  96. request_module("rc80211_%s", alg_name);
  97. ops = ieee80211_try_rate_control_ops_get(alg_name);
  98. }
  99. if (!ops && name)
  100. /* try default if specific alg requested but not found */
  101. ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
  102. /* try built-in one if specific alg requested but not found */
  103. if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
  104. ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
  105. kparam_unblock_sysfs_write(ieee80211_default_rc_algo);
  106. return ops;
  107. }
  108. static void ieee80211_rate_control_ops_put(struct rate_control_ops *ops)
  109. {
  110. module_put(ops->module);
  111. }
  112. #ifdef CONFIG_MAC80211_DEBUGFS
  113. static ssize_t rcname_read(struct file *file, char __user *userbuf,
  114. size_t count, loff_t *ppos)
  115. {
  116. struct rate_control_ref *ref = file->private_data;
  117. int len = strlen(ref->ops->name);
  118. return simple_read_from_buffer(userbuf, count, ppos,
  119. ref->ops->name, len);
  120. }
  121. static const struct file_operations rcname_ops = {
  122. .read = rcname_read,
  123. .open = mac80211_open_file_generic,
  124. };
  125. #endif
  126. static struct rate_control_ref *rate_control_alloc(const char *name,
  127. struct ieee80211_local *local)
  128. {
  129. struct dentry *debugfsdir = NULL;
  130. struct rate_control_ref *ref;
  131. ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
  132. if (!ref)
  133. goto fail_ref;
  134. kref_init(&ref->kref);
  135. ref->local = local;
  136. ref->ops = ieee80211_rate_control_ops_get(name);
  137. if (!ref->ops)
  138. goto fail_ops;
  139. #ifdef CONFIG_MAC80211_DEBUGFS
  140. debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
  141. local->debugfs.rcdir = debugfsdir;
  142. debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
  143. #endif
  144. ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
  145. if (!ref->priv)
  146. goto fail_priv;
  147. return ref;
  148. fail_priv:
  149. ieee80211_rate_control_ops_put(ref->ops);
  150. fail_ops:
  151. kfree(ref);
  152. fail_ref:
  153. return NULL;
  154. }
  155. static void rate_control_release(struct kref *kref)
  156. {
  157. struct rate_control_ref *ctrl_ref;
  158. ctrl_ref = container_of(kref, struct rate_control_ref, kref);
  159. ctrl_ref->ops->free(ctrl_ref->priv);
  160. #ifdef CONFIG_MAC80211_DEBUGFS
  161. debugfs_remove_recursive(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. static bool rc_no_data_or_no_ack(struct ieee80211_tx_rate_control *txrc)
  168. {
  169. struct sk_buff *skb = txrc->skb;
  170. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  171. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  172. __le16 fc;
  173. fc = hdr->frame_control;
  174. return ((info->flags & IEEE80211_TX_CTL_NO_ACK) || !ieee80211_is_data(fc));
  175. }
  176. static void rc_send_low_broadcast(s8 *idx, u32 basic_rates, u8 max_rate_idx)
  177. {
  178. u8 i;
  179. if (basic_rates == 0)
  180. return; /* assume basic rates unknown and accept rate */
  181. if (*idx < 0)
  182. return;
  183. if (basic_rates & (1 << *idx))
  184. return; /* selected rate is a basic rate */
  185. for (i = *idx + 1; i <= max_rate_idx; i++) {
  186. if (basic_rates & (1 << i)) {
  187. *idx = i;
  188. return;
  189. }
  190. }
  191. /* could not find a basic rate; use original selection */
  192. }
  193. bool rate_control_send_low(struct ieee80211_sta *sta,
  194. void *priv_sta,
  195. struct ieee80211_tx_rate_control *txrc)
  196. {
  197. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  198. if (!sta || !priv_sta || rc_no_data_or_no_ack(txrc)) {
  199. info->control.rates[0].idx = rate_lowest_index(txrc->sband, sta);
  200. info->control.rates[0].count =
  201. (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
  202. 1 : txrc->hw->max_rate_tries;
  203. if (!sta && txrc->ap)
  204. rc_send_low_broadcast(&info->control.rates[0].idx,
  205. txrc->bss_conf->basic_rates,
  206. txrc->sband->n_bitrates);
  207. return true;
  208. }
  209. return false;
  210. }
  211. EXPORT_SYMBOL(rate_control_send_low);
  212. static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
  213. int n_bitrates, u32 mask)
  214. {
  215. int j;
  216. /* See whether the selected rate or anything below it is allowed. */
  217. for (j = rate->idx; j >= 0; j--) {
  218. if (mask & (1 << j)) {
  219. /* Okay, found a suitable rate. Use it. */
  220. rate->idx = j;
  221. return;
  222. }
  223. }
  224. /* Try to find a higher rate that would be allowed */
  225. for (j = rate->idx + 1; j < n_bitrates; j++) {
  226. if (mask & (1 << j)) {
  227. /* Okay, found a suitable rate. Use it. */
  228. rate->idx = j;
  229. return;
  230. }
  231. }
  232. /*
  233. * Uh.. No suitable rate exists. This should not really happen with
  234. * sane TX rate mask configurations. However, should someone manage to
  235. * configure supported rates and TX rate mask in incompatible way,
  236. * allow the frame to be transmitted with whatever the rate control
  237. * selected.
  238. */
  239. }
  240. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  241. struct sta_info *sta,
  242. struct ieee80211_tx_rate_control *txrc)
  243. {
  244. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  245. void *priv_sta = NULL;
  246. struct ieee80211_sta *ista = NULL;
  247. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  248. int i;
  249. u32 mask;
  250. if (sta) {
  251. ista = &sta->sta;
  252. priv_sta = sta->rate_ctrl_priv;
  253. }
  254. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  255. info->control.rates[i].idx = -1;
  256. info->control.rates[i].flags = 0;
  257. info->control.rates[i].count = 1;
  258. }
  259. if (sdata->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  260. return;
  261. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  262. /*
  263. * Try to enforce the rateidx mask the user wanted. skip this if the
  264. * default mask (allow all rates) is used to save some processing for
  265. * the common case.
  266. */
  267. mask = sdata->rc_rateidx_mask[info->band];
  268. if (mask != (1 << txrc->sband->n_bitrates) - 1) {
  269. if (sta) {
  270. /* Filter out rates that the STA does not support */
  271. mask &= sta->sta.supp_rates[info->band];
  272. }
  273. /*
  274. * Make sure the rate index selected for each TX rate is
  275. * included in the configured mask and change the rate indexes
  276. * if needed.
  277. */
  278. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  279. /* Rate masking supports only legacy rates for now */
  280. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS)
  281. continue;
  282. rate_idx_match_mask(&info->control.rates[i],
  283. txrc->sband->n_bitrates, mask);
  284. }
  285. }
  286. BUG_ON(info->control.rates[0].idx < 0);
  287. }
  288. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  289. {
  290. kref_get(&ref->kref);
  291. return ref;
  292. }
  293. void rate_control_put(struct rate_control_ref *ref)
  294. {
  295. kref_put(&ref->kref, rate_control_release);
  296. }
  297. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  298. const char *name)
  299. {
  300. struct rate_control_ref *ref, *old;
  301. ASSERT_RTNL();
  302. if (local->open_count)
  303. return -EBUSY;
  304. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
  305. if (WARN_ON(!local->ops->set_rts_threshold))
  306. return -EINVAL;
  307. return 0;
  308. }
  309. ref = rate_control_alloc(name, local);
  310. if (!ref) {
  311. printk(KERN_WARNING "%s: Failed to select rate control "
  312. "algorithm\n", wiphy_name(local->hw.wiphy));
  313. return -ENOENT;
  314. }
  315. old = local->rate_ctrl;
  316. local->rate_ctrl = ref;
  317. if (old) {
  318. rate_control_put(old);
  319. sta_info_flush(local, NULL);
  320. }
  321. printk(KERN_DEBUG "%s: Selected rate control "
  322. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  323. ref->ops->name);
  324. return 0;
  325. }
  326. void rate_control_deinitialize(struct ieee80211_local *local)
  327. {
  328. struct rate_control_ref *ref;
  329. ref = local->rate_ctrl;
  330. if (!ref)
  331. return;
  332. local->rate_ctrl = NULL;
  333. rate_control_put(ref);
  334. }