rc80211_pid_algo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005, Devicescape Software, Inc.
  4. * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
  5. * Copyright 2007, Stefano Brivio <stefano.brivio@polimi.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/skbuff.h>
  14. #include <net/mac80211.h>
  15. #include "ieee80211_rate.h"
  16. #include "rc80211_pid.h"
  17. /* This is an implementation of a TX rate control algorithm that uses a PID
  18. * controller. Given a target failed frames rate, the controller decides about
  19. * TX rate changes to meet the target failed frames rate.
  20. *
  21. * The controller basically computes the following:
  22. *
  23. * adj = CP * err + CI * err_avg + CD * (err - last_err) * (1 + sharpening)
  24. *
  25. * where
  26. * adj adjustment value that is used to switch TX rate (see below)
  27. * err current error: target vs. current failed frames percentage
  28. * last_err last error
  29. * err_avg average (i.e. poor man's integral) of recent errors
  30. * sharpening non-zero when fast response is needed (i.e. right after
  31. * association or no frames sent for a long time), heading
  32. * to zero over time
  33. * CP Proportional coefficient
  34. * CI Integral coefficient
  35. * CD Derivative coefficient
  36. *
  37. * CP, CI, CD are subject to careful tuning.
  38. *
  39. * The integral component uses a exponential moving average approach instead of
  40. * an actual sliding window. The advantage is that we don't need to keep an
  41. * array of the last N error values and computation is easier.
  42. *
  43. * Once we have the adj value, we map it to a rate by means of a learning
  44. * algorithm. This algorithm keeps the state of the percentual failed frames
  45. * difference between rates. The behaviour of the lowest available rate is kept
  46. * as a reference value, and every time we switch between two rates, we compute
  47. * the difference between the failed frames each rate exhibited. By doing so,
  48. * we compare behaviours which different rates exhibited in adjacent timeslices,
  49. * thus the comparison is minimally affected by external conditions. This
  50. * difference gets propagated to the whole set of measurements, so that the
  51. * reference is always the same. Periodically, we normalize this set so that
  52. * recent events weigh the most. By comparing the adj value with this set, we
  53. * avoid pejorative switches to lower rates and allow for switches to higher
  54. * rates if they behaved well.
  55. *
  56. * Note that for the computations we use a fixed-point representation to avoid
  57. * floating point arithmetic. Hence, all values are shifted left by
  58. * RC_PID_ARITH_SHIFT.
  59. */
  60. /* Shift the adjustment so that we won't switch to a lower rate if it exhibited
  61. * a worse failed frames behaviour and we'll choose the highest rate whose
  62. * failed frames behaviour is not worse than the one of the original rate
  63. * target. While at it, check that the adjustment is within the ranges. Then,
  64. * provide the new rate index. */
  65. static int rate_control_pid_shift_adjust(struct rc_pid_rateinfo *r,
  66. int adj, int cur, int l)
  67. {
  68. int i, j, k, tmp;
  69. if (cur + adj < 0)
  70. return 0;
  71. if (cur + adj >= l)
  72. return l - 1;
  73. i = r[cur + adj].rev_index;
  74. j = r[cur].rev_index;
  75. if (adj < 0) {
  76. tmp = i;
  77. for (k = j; k >= i; k--)
  78. if (r[k].diff <= r[j].diff)
  79. tmp = k;
  80. return r[tmp].index;
  81. } else if (adj > 0) {
  82. tmp = i;
  83. for (k = i + 1; k + i < l; k++)
  84. if (r[k].diff <= r[i].diff)
  85. tmp = k;
  86. return r[tmp].index;
  87. }
  88. return cur + adj;
  89. }
  90. static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
  91. struct sta_info *sta, int adj,
  92. struct rc_pid_rateinfo *rinfo)
  93. {
  94. struct ieee80211_sub_if_data *sdata;
  95. struct ieee80211_hw_mode *mode;
  96. int newidx;
  97. int maxrate;
  98. int back = (adj > 0) ? 1 : -1;
  99. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  100. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  101. /* forced unicast rate - do not change STA rate */
  102. return;
  103. }
  104. mode = local->oper_hw_mode;
  105. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  106. newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate,
  107. mode->num_rates);
  108. while (newidx != sta->txrate) {
  109. if (rate_supported(sta, mode, newidx) &&
  110. (maxrate < 0 || newidx <= maxrate)) {
  111. sta->txrate = newidx;
  112. break;
  113. }
  114. newidx += back;
  115. }
  116. #ifdef CONFIG_MAC80211_DEBUGFS
  117. rate_control_pid_event_rate_change(
  118. &((struct rc_pid_sta_info *)sta->rate_ctrl_priv)->events,
  119. newidx, mode->rates[newidx].rate);
  120. #endif
  121. }
  122. /* Normalize the failed frames per-rate differences. */
  123. static void rate_control_pid_normalize(struct rc_pid_rateinfo *r, int l)
  124. {
  125. int i;
  126. if (r[0].diff > RC_PID_NORM_OFFSET)
  127. r[0].diff -= RC_PID_NORM_OFFSET;
  128. else if (r[0].diff < -RC_PID_NORM_OFFSET)
  129. r[0].diff += RC_PID_NORM_OFFSET;
  130. for (i = 0; i < l - 1; i++)
  131. if (r[i + 1].diff > r[i].diff + RC_PID_NORM_OFFSET)
  132. r[i + 1].diff -= RC_PID_NORM_OFFSET;
  133. else if (r[i + 1].diff <= r[i].diff)
  134. r[i + 1].diff += RC_PID_NORM_OFFSET;
  135. }
  136. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  137. struct ieee80211_local *local,
  138. struct sta_info *sta)
  139. {
  140. struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
  141. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  142. struct ieee80211_hw_mode *mode;
  143. u32 pf;
  144. s32 err_avg;
  145. s32 err_prop;
  146. s32 err_int;
  147. s32 err_der;
  148. int adj, i, j, tmp;
  149. mode = local->oper_hw_mode;
  150. spinfo = sta->rate_ctrl_priv;
  151. /* In case nothing happened during the previous control interval, turn
  152. * the sharpening factor on. */
  153. if (jiffies - spinfo->last_sample > 2 * RC_PID_INTERVAL)
  154. spinfo->sharp_cnt = RC_PID_SHARPENING_DURATION;
  155. spinfo->last_sample = jiffies;
  156. /* This should never happen, but in case, we assume the old sample is
  157. * still a good measurement and copy it. */
  158. if (unlikely(spinfo->tx_num_xmit == 0))
  159. pf = spinfo->last_pf;
  160. else {
  161. pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
  162. pf <<= RC_PID_ARITH_SHIFT;
  163. }
  164. spinfo->tx_num_xmit = 0;
  165. spinfo->tx_num_failed = 0;
  166. /* If we just switched rate, update the rate behaviour info. */
  167. if (pinfo->oldrate != sta->txrate) {
  168. i = rinfo[pinfo->oldrate].rev_index;
  169. j = rinfo[sta->txrate].rev_index;
  170. tmp = (pf - spinfo->last_pf);
  171. tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);
  172. rinfo[j].diff = rinfo[i].diff + tmp;
  173. pinfo->oldrate = sta->txrate;
  174. }
  175. rate_control_pid_normalize(rinfo, mode->num_rates);
  176. /* Compute the proportional, integral and derivative errors. */
  177. err_prop = RC_PID_TARGET_PF - pf;
  178. err_avg = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  179. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  180. err_int = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  181. err_der = pf - spinfo->last_pf
  182. * (1 + RC_PID_SHARPENING_FACTOR * spinfo->sharp_cnt);
  183. spinfo->last_pf = pf;
  184. if (spinfo->sharp_cnt)
  185. spinfo->sharp_cnt--;
  186. #ifdef CONFIG_MAC80211_DEBUGFS
  187. rate_control_pid_event_pf_sample(&spinfo->events, pf, err_prop, err_int,
  188. err_der);
  189. #endif
  190. /* Compute the controller output. */
  191. adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
  192. + err_der * pinfo->coeff_d);
  193. adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
  194. /* Change rate. */
  195. if (adj)
  196. rate_control_pid_adjust_rate(local, sta, adj, rinfo);
  197. }
  198. static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
  199. struct sk_buff *skb,
  200. struct ieee80211_tx_status *status)
  201. {
  202. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  203. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  204. struct rc_pid_info *pinfo = priv;
  205. struct sta_info *sta;
  206. struct rc_pid_sta_info *spinfo;
  207. sta = sta_info_get(local, hdr->addr1);
  208. if (!sta)
  209. return;
  210. /* Ignore all frames that were sent with a different rate than the rate
  211. * we currently advise mac80211 to use. */
  212. if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
  213. return;
  214. spinfo = sta->rate_ctrl_priv;
  215. spinfo->tx_num_xmit++;
  216. #ifdef CONFIG_MAC80211_DEBUGFS
  217. rate_control_pid_event_tx_status(&spinfo->events, status);
  218. #endif
  219. /* We count frames that totally failed to be transmitted as two bad
  220. * frames, those that made it out but had some retries as one good and
  221. * one bad frame. */
  222. if (status->excessive_retries) {
  223. spinfo->tx_num_failed += 2;
  224. spinfo->tx_num_xmit++;
  225. } else if (status->retry_count) {
  226. spinfo->tx_num_failed++;
  227. spinfo->tx_num_xmit++;
  228. }
  229. if (status->excessive_retries) {
  230. sta->tx_retry_failed++;
  231. sta->tx_num_consecutive_failures++;
  232. sta->tx_num_mpdu_fail++;
  233. } else {
  234. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  235. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  236. sta->last_ack_rssi[2] = status->ack_signal;
  237. sta->tx_num_consecutive_failures = 0;
  238. sta->tx_num_mpdu_ok++;
  239. }
  240. sta->tx_retry_count += status->retry_count;
  241. sta->tx_num_mpdu_fail += status->retry_count;
  242. /* Update PID controller state. */
  243. if (time_after(jiffies, spinfo->last_sample + RC_PID_INTERVAL))
  244. rate_control_pid_sample(pinfo, local, sta);
  245. sta_info_put(sta);
  246. }
  247. static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
  248. struct ieee80211_hw_mode *mode,
  249. struct sk_buff *skb,
  250. struct rate_selection *sel)
  251. {
  252. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  253. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  254. struct sta_info *sta;
  255. int rateidx;
  256. sta = sta_info_get(local, hdr->addr1);
  257. if (!sta) {
  258. sel->rate = rate_lowest(local, mode, NULL);
  259. sta_info_put(sta);
  260. return;
  261. }
  262. rateidx = sta->txrate;
  263. if (rateidx >= mode->num_rates)
  264. rateidx = mode->num_rates - 1;
  265. sta_info_put(sta);
  266. sel->rate = &mode->rates[rateidx];
  267. #ifdef CONFIG_MAC80211_DEBUGFS
  268. rate_control_pid_event_tx_rate(
  269. &((struct rc_pid_sta_info *) sta->rate_ctrl_priv)->events,
  270. rateidx, mode->rates[rateidx].rate);
  271. #endif
  272. }
  273. static void rate_control_pid_rate_init(void *priv, void *priv_sta,
  274. struct ieee80211_local *local,
  275. struct sta_info *sta)
  276. {
  277. /* TODO: This routine should consider using RSSI from previous packets
  278. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  279. * Until that method is implemented, we will use the lowest supported
  280. * rate as a workaround. */
  281. sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
  282. }
  283. static void *rate_control_pid_alloc(struct ieee80211_local *local)
  284. {
  285. struct rc_pid_info *pinfo;
  286. struct rc_pid_rateinfo *rinfo;
  287. struct ieee80211_hw_mode *mode;
  288. int i, j, tmp;
  289. bool s;
  290. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  291. if (!pinfo)
  292. return NULL;
  293. /* We can safely assume that oper_hw_mode won't change unless we get
  294. * reinitialized. */
  295. mode = local->oper_hw_mode;
  296. rinfo = kmalloc(sizeof(*rinfo) * mode->num_rates, GFP_ATOMIC);
  297. if (!rinfo) {
  298. kfree(pinfo);
  299. return NULL;
  300. }
  301. /* Sort the rates. This is optimized for the most common case (i.e.
  302. * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
  303. * mapping too. */
  304. for (i = 0; i < mode->num_rates; i++) {
  305. rinfo[i].index = i;
  306. rinfo[i].rev_index = i;
  307. if (RC_PID_FAST_START)
  308. rinfo[i].diff = 0;
  309. else
  310. rinfo[i].diff = i * RC_PID_NORM_OFFSET;
  311. }
  312. for (i = 1; i < mode->num_rates; i++) {
  313. s = 0;
  314. for (j = 0; j < mode->num_rates - i; j++)
  315. if (unlikely(mode->rates[rinfo[j].index].rate >
  316. mode->rates[rinfo[j + 1].index].rate)) {
  317. tmp = rinfo[j].index;
  318. rinfo[j].index = rinfo[j + 1].index;
  319. rinfo[j + 1].index = tmp;
  320. rinfo[rinfo[j].index].rev_index = j;
  321. rinfo[rinfo[j + 1].index].rev_index = j + 1;
  322. s = 1;
  323. }
  324. if (!s)
  325. break;
  326. }
  327. pinfo->target = RC_PID_TARGET_PF;
  328. pinfo->coeff_p = RC_PID_COEFF_P;
  329. pinfo->coeff_i = RC_PID_COEFF_I;
  330. pinfo->coeff_d = RC_PID_COEFF_D;
  331. pinfo->rinfo = rinfo;
  332. pinfo->oldrate = 0;
  333. return pinfo;
  334. }
  335. static void rate_control_pid_free(void *priv)
  336. {
  337. struct rc_pid_info *pinfo = priv;
  338. kfree(pinfo->rinfo);
  339. kfree(pinfo);
  340. }
  341. static void rate_control_pid_clear(void *priv)
  342. {
  343. }
  344. static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
  345. {
  346. struct rc_pid_sta_info *spinfo;
  347. spinfo = kzalloc(sizeof(*spinfo), gfp);
  348. if (spinfo == NULL)
  349. return NULL;
  350. #ifdef CONFIG_MAC80211_DEBUGFS
  351. spin_lock_init(&spinfo->events.lock);
  352. init_waitqueue_head(&spinfo->events.waitqueue);
  353. #endif
  354. return spinfo;
  355. }
  356. static void rate_control_pid_free_sta(void *priv, void *priv_sta)
  357. {
  358. struct rc_pid_sta_info *spinfo = priv_sta;
  359. kfree(spinfo);
  360. }
  361. struct rate_control_ops mac80211_rcpid = {
  362. .name = "pid",
  363. .tx_status = rate_control_pid_tx_status,
  364. .get_rate = rate_control_pid_get_rate,
  365. .rate_init = rate_control_pid_rate_init,
  366. .clear = rate_control_pid_clear,
  367. .alloc = rate_control_pid_alloc,
  368. .free = rate_control_pid_free,
  369. .alloc_sta = rate_control_pid_alloc_sta,
  370. .free_sta = rate_control_pid_free_sta,
  371. #ifdef CONFIG_MAC80211_DEBUGFS
  372. .add_sta_debugfs = rate_control_pid_add_sta_debugfs,
  373. .remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
  374. #endif
  375. };