rate.c 11 KB

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