rate.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. static 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. static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
  210. int n_bitrates, u32 mask)
  211. {
  212. int j;
  213. /* See whether the selected rate or anything below it is allowed. */
  214. for (j = rate->idx; j >= 0; j--) {
  215. if (mask & (1 << j)) {
  216. /* Okay, found a suitable rate. Use it. */
  217. rate->idx = j;
  218. return;
  219. }
  220. }
  221. /* Try to find a higher rate that would be allowed */
  222. for (j = rate->idx + 1; j < n_bitrates; j++) {
  223. if (mask & (1 << j)) {
  224. /* Okay, found a suitable rate. Use it. */
  225. rate->idx = j;
  226. return;
  227. }
  228. }
  229. /*
  230. * Uh.. No suitable rate exists. This should not really happen with
  231. * sane TX rate mask configurations. However, should someone manage to
  232. * configure supported rates and TX rate mask in incompatible way,
  233. * allow the frame to be transmitted with whatever the rate control
  234. * selected.
  235. */
  236. }
  237. void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
  238. struct sta_info *sta,
  239. struct ieee80211_tx_rate_control *txrc)
  240. {
  241. struct rate_control_ref *ref = sdata->local->rate_ctrl;
  242. void *priv_sta = NULL;
  243. struct ieee80211_sta *ista = NULL;
  244. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
  245. int i;
  246. u32 mask;
  247. if (sta) {
  248. ista = &sta->sta;
  249. priv_sta = sta->rate_ctrl_priv;
  250. }
  251. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  252. info->control.rates[i].idx = -1;
  253. info->control.rates[i].flags = 0;
  254. info->control.rates[i].count = 1;
  255. }
  256. if (sdata->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  257. return;
  258. ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
  259. /*
  260. * Try to enforce the rateidx mask the user wanted. skip this if the
  261. * default mask (allow all rates) is used to save some processing for
  262. * the common case.
  263. */
  264. mask = sdata->rc_rateidx_mask[info->band];
  265. if (mask != (1 << txrc->sband->n_bitrates) - 1) {
  266. if (sta) {
  267. /* Filter out rates that the STA does not support */
  268. mask &= sta->sta.supp_rates[info->band];
  269. }
  270. /*
  271. * Make sure the rate index selected for each TX rate is
  272. * included in the configured mask and change the rate indexes
  273. * if needed.
  274. */
  275. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  276. /* Rate masking supports only legacy rates for now */
  277. if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS)
  278. continue;
  279. rate_idx_match_mask(&info->control.rates[i],
  280. txrc->sband->n_bitrates, mask);
  281. }
  282. }
  283. BUG_ON(info->control.rates[0].idx < 0);
  284. }
  285. struct rate_control_ref *rate_control_get(struct rate_control_ref *ref)
  286. {
  287. kref_get(&ref->kref);
  288. return ref;
  289. }
  290. void rate_control_put(struct rate_control_ref *ref)
  291. {
  292. kref_put(&ref->kref, rate_control_release);
  293. }
  294. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  295. const char *name)
  296. {
  297. struct rate_control_ref *ref, *old;
  298. ASSERT_RTNL();
  299. if (local->open_count)
  300. return -EBUSY;
  301. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) {
  302. if (WARN_ON(!local->ops->set_rts_threshold))
  303. return -EINVAL;
  304. return 0;
  305. }
  306. ref = rate_control_alloc(name, local);
  307. if (!ref) {
  308. printk(KERN_WARNING "%s: Failed to select rate control "
  309. "algorithm\n", wiphy_name(local->hw.wiphy));
  310. return -ENOENT;
  311. }
  312. old = local->rate_ctrl;
  313. local->rate_ctrl = ref;
  314. if (old) {
  315. rate_control_put(old);
  316. sta_info_flush(local, NULL);
  317. }
  318. printk(KERN_DEBUG "%s: Selected rate control "
  319. "algorithm '%s'\n", wiphy_name(local->hw.wiphy),
  320. ref->ops->name);
  321. return 0;
  322. }
  323. void rate_control_deinitialize(struct ieee80211_local *local)
  324. {
  325. struct rate_control_ref *ref;
  326. ref = local->rate_ctrl;
  327. if (!ref)
  328. return;
  329. local->rate_ctrl = NULL;
  330. rate_control_put(ref);
  331. }