rc80211_pid.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005, Devicescape Software, Inc.
  4. * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
  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/netdevice.h>
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <net/mac80211.h>
  14. #include "ieee80211_rate.h"
  15. /* This is an implementation of a TX rate control algorithm that uses a PID
  16. * controller. Given a target failed frames rate, the controller decides about
  17. * TX rate changes to meet the target failed frames rate.
  18. *
  19. * The controller basically computes the following:
  20. *
  21. * adj = CP * err + CI * err_avg + CD * (err - last_err)
  22. *
  23. * where
  24. * adj adjustment value that is used to switch TX rate (see below)
  25. * err current error: target vs. current failed frames percentage
  26. * last_err last error
  27. * err_avg average (i.e. poor man's integral) of recent errors
  28. * CP Proportional coefficient
  29. * CI Integral coefficient
  30. * CD Derivative coefficient
  31. *
  32. * CP, CI, CD are subject to careful tuning.
  33. *
  34. * The integral component uses a exponential moving average approach instead of
  35. * an actual sliding window. The advantage is that we don't need to keep an
  36. * array of the last N error values and computation is easier.
  37. *
  38. * Once we have the adj value, we need to map it to a TX rate to be selected.
  39. * For now, we depend on the rates to be ordered in a way such that more robust
  40. * rates (i.e. such that exhibit a lower framed failed percentage) come first.
  41. * E.g. for the 802.11b/g case, we first have the b rates in ascending order,
  42. * then the g rates. The adj simply decides the index of the TX rate in the list
  43. * to switch to (relative to the current TX rate entry).
  44. *
  45. * Note that for the computations we use a fixed-point representation to avoid
  46. * floating point arithmetic. Hence, all values are shifted left by
  47. * RC_PID_ARITH_SHIFT.
  48. */
  49. /* Sampling period for measuring percentage of failed frames. */
  50. #define RC_PID_INTERVAL (HZ / 8)
  51. /* Exponential averaging smoothness (used for I part of PID controller) */
  52. #define RC_PID_SMOOTHING_SHIFT 3
  53. #define RC_PID_SMOOTHING (1 << RC_PID_SMOOTHING_SHIFT)
  54. /* Fixed point arithmetic shifting amount. */
  55. #define RC_PID_ARITH_SHIFT 8
  56. /* Fixed point arithmetic factor. */
  57. #define RC_PID_ARITH_FACTOR (1 << RC_PID_ARITH_SHIFT)
  58. /* Proportional PID component coefficient. */
  59. #define RC_PID_COEFF_P 15
  60. /* Integral PID component coefficient. */
  61. #define RC_PID_COEFF_I 9
  62. /* Derivative PID component coefficient. */
  63. #define RC_PID_COEFF_D 15
  64. /* Target failed frames rate for the PID controller. NB: This effectively gives
  65. * maximum failed frames percentage we're willing to accept. If the wireless
  66. * link quality is good, the controller will fail to adjust failed frames
  67. * percentage to the target. This is intentional.
  68. */
  69. #define RC_PID_TARGET_PF (11 << RC_PID_ARITH_SHIFT)
  70. struct rc_pid_sta_info {
  71. unsigned long last_change;
  72. unsigned long last_sample;
  73. u32 tx_num_failed;
  74. u32 tx_num_xmit;
  75. /* Average failed frames percentage error (i.e. actual vs. target
  76. * percentage), scaled by RC_PID_SMOOTHING. This value is computed
  77. * using using an exponential weighted average technique:
  78. *
  79. * (RC_PID_SMOOTHING - 1) * err_avg_old + err
  80. * err_avg = ------------------------------------------
  81. * RC_PID_SMOOTHING
  82. *
  83. * where err_avg is the new approximation, err_avg_old the previous one
  84. * and err is the error w.r.t. to the current failed frames percentage
  85. * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
  86. * given to the previous estimate, resulting in smoother behavior (i.e.
  87. * corresponding to a longer integration window).
  88. *
  89. * For computation, we actually don't use the above formula, but this
  90. * one:
  91. *
  92. * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
  93. *
  94. * where:
  95. * err_avg_scaled = err * RC_PID_SMOOTHING
  96. * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
  97. *
  98. * This avoids floating point numbers and the per_failed_old value can
  99. * easily be obtained by shifting per_failed_old_scaled right by
  100. * RC_PID_SMOOTHING_SHIFT.
  101. */
  102. s32 err_avg_sc;
  103. /* Last framed failes percentage sample */
  104. u32 last_pf;
  105. };
  106. /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
  107. * be tuned individually for each interface.
  108. */
  109. struct rc_pid_info {
  110. /* The failed frames percentage target. */
  111. u32 target;
  112. /* P, I and D coefficients. */
  113. s32 coeff_p;
  114. s32 coeff_i;
  115. s32 coeff_d;
  116. };
  117. static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
  118. struct sta_info *sta, int adj)
  119. {
  120. struct ieee80211_sub_if_data *sdata;
  121. struct ieee80211_hw_mode *mode;
  122. int newidx = sta->txrate + adj;
  123. int maxrate;
  124. int back = (adj > 0) ? 1 : -1;
  125. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  126. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  127. /* forced unicast rate - do not change STA rate */
  128. return;
  129. }
  130. mode = local->oper_hw_mode;
  131. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  132. if (newidx < 0)
  133. newidx = 0;
  134. else if (newidx >= mode->num_rates)
  135. newidx = mode->num_rates - 1;
  136. while (newidx != sta->txrate) {
  137. if (rate_supported(sta, mode, newidx) &&
  138. (maxrate < 0 || newidx <= maxrate)) {
  139. sta->txrate = newidx;
  140. break;
  141. }
  142. newidx += back;
  143. }
  144. }
  145. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  146. struct ieee80211_local *local,
  147. struct sta_info *sta)
  148. {
  149. struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
  150. u32 pf;
  151. s32 err_avg;
  152. s32 err_prop;
  153. s32 err_int;
  154. s32 err_der;
  155. int adj;
  156. spinfo = sta->rate_ctrl_priv;
  157. spinfo->last_sample = jiffies;
  158. /* If no frames were transmitted, we assume the old sample is
  159. * still a good measurement and copy it. */
  160. if (spinfo->tx_num_xmit == 0)
  161. pf = spinfo->last_pf;
  162. else {
  163. pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
  164. pf <<= RC_PID_ARITH_SHIFT;
  165. spinfo->tx_num_xmit = 0;
  166. spinfo->tx_num_failed = 0;
  167. }
  168. /* Compute the proportional, integral and derivative errors. */
  169. err_prop = RC_PID_TARGET_PF - pf;
  170. err_avg = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  171. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  172. err_int = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  173. err_der = pf - spinfo->last_pf;
  174. spinfo->last_pf = pf;
  175. /* Compute the controller output. */
  176. adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
  177. + err_der * pinfo->coeff_d);
  178. /* We need to do an arithmetic right shift. ISO C says this is
  179. * implementation defined for negative left operands. Hence, be
  180. * careful to get it right, also for negative values. */
  181. adj = (adj < 0) ? -((-adj) >> (2 * RC_PID_ARITH_SHIFT)) :
  182. adj >> (2 * RC_PID_ARITH_SHIFT);
  183. /* Change rate. */
  184. if (adj)
  185. rate_control_pid_adjust_rate(local, sta, adj);
  186. }
  187. static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
  188. struct sk_buff *skb,
  189. struct ieee80211_tx_status *status)
  190. {
  191. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  192. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  193. struct rc_pid_info *pinfo = priv;
  194. struct sta_info *sta;
  195. struct rc_pid_sta_info *spinfo;
  196. sta = sta_info_get(local, hdr->addr1);
  197. if (!sta)
  198. return;
  199. /* Ignore all frames that were sent with a different rate than the rate
  200. * we currently advise mac80211 to use. */
  201. if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
  202. return;
  203. spinfo = sta->rate_ctrl_priv;
  204. spinfo->tx_num_xmit++;
  205. /* We count frames that totally failed to be transmitted as two bad
  206. * frames, those that made it out but had some retries as one good and
  207. * one bad frame. */
  208. if (status->excessive_retries) {
  209. spinfo->tx_num_failed += 2;
  210. spinfo->tx_num_xmit++;
  211. } else if (status->retry_count) {
  212. spinfo->tx_num_failed++;
  213. spinfo->tx_num_xmit++;
  214. }
  215. if (status->excessive_retries) {
  216. sta->tx_retry_failed++;
  217. sta->tx_num_consecutive_failures++;
  218. sta->tx_num_mpdu_fail++;
  219. } else {
  220. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  221. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  222. sta->last_ack_rssi[2] = status->ack_signal;
  223. sta->tx_num_consecutive_failures = 0;
  224. sta->tx_num_mpdu_ok++;
  225. }
  226. sta->tx_retry_count += status->retry_count;
  227. sta->tx_num_mpdu_fail += status->retry_count;
  228. /* Update PID controller state. */
  229. if (time_after(jiffies, spinfo->last_sample + RC_PID_INTERVAL))
  230. rate_control_pid_sample(pinfo, local, sta);
  231. sta_info_put(sta);
  232. }
  233. static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
  234. struct ieee80211_hw_mode *mode,
  235. struct sk_buff *skb,
  236. struct rate_selection *sel)
  237. {
  238. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  239. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  240. struct sta_info *sta;
  241. int rateidx;
  242. sta = sta_info_get(local, hdr->addr1);
  243. if (!sta) {
  244. sel->rate = rate_lowest(local, mode, NULL);
  245. sta_info_put(sta);
  246. return;
  247. }
  248. rateidx = sta->txrate;
  249. if (rateidx >= mode->num_rates)
  250. rateidx = mode->num_rates - 1;
  251. sta_info_put(sta);
  252. sel->rate = &mode->rates[rateidx];
  253. }
  254. static void rate_control_pid_rate_init(void *priv, void *priv_sta,
  255. struct ieee80211_local *local,
  256. struct sta_info *sta)
  257. {
  258. /* TODO: This routine should consider using RSSI from previous packets
  259. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  260. * Until that method is implemented, we will use the lowest supported
  261. * rate as a workaround. */
  262. sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
  263. }
  264. static void *rate_control_pid_alloc(struct ieee80211_local *local)
  265. {
  266. struct rc_pid_info *pinfo;
  267. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  268. pinfo->target = RC_PID_TARGET_PF;
  269. pinfo->coeff_p = RC_PID_COEFF_P;
  270. pinfo->coeff_i = RC_PID_COEFF_I;
  271. pinfo->coeff_d = RC_PID_COEFF_D;
  272. return pinfo;
  273. }
  274. static void rate_control_pid_free(void *priv)
  275. {
  276. struct rc_pid_info *pinfo = priv;
  277. kfree(pinfo);
  278. }
  279. static void rate_control_pid_clear(void *priv)
  280. {
  281. }
  282. static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
  283. {
  284. struct rc_pid_sta_info *spinfo;
  285. spinfo = kzalloc(sizeof(*spinfo), gfp);
  286. return spinfo;
  287. }
  288. static void rate_control_pid_free_sta(void *priv, void *priv_sta)
  289. {
  290. struct rc_pid_sta_info *spinfo = priv_sta;
  291. kfree(spinfo);
  292. }
  293. struct rate_control_ops mac80211_rcpid = {
  294. .name = "pid",
  295. .tx_status = rate_control_pid_tx_status,
  296. .get_rate = rate_control_pid_get_rate,
  297. .rate_init = rate_control_pid_rate_init,
  298. .clear = rate_control_pid_clear,
  299. .alloc = rate_control_pid_alloc,
  300. .free = rate_control_pid_free,
  301. .alloc_sta = rate_control_pid_alloc_sta,
  302. .free_sta = rate_control_pid_free_sta,
  303. };