rc80211_pid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. /* This is an implementation of a TX rate control algorithm that uses a PID
  17. * controller. Given a target failed frames rate, the controller decides about
  18. * TX rate changes to meet the target failed frames rate.
  19. *
  20. * The controller basically computes the following:
  21. *
  22. * adj = CP * err + CI * err_avg + CD * (err - last_err)
  23. *
  24. * where
  25. * adj adjustment value that is used to switch TX rate (see below)
  26. * err current error: target vs. current failed frames percentage
  27. * last_err last error
  28. * err_avg average (i.e. poor man's integral) of recent errors
  29. * CP Proportional coefficient
  30. * CI Integral coefficient
  31. * CD Derivative coefficient
  32. *
  33. * CP, CI, CD are subject to careful tuning.
  34. *
  35. * The integral component uses a exponential moving average approach instead of
  36. * an actual sliding window. The advantage is that we don't need to keep an
  37. * array of the last N error values and computation is easier.
  38. *
  39. * Once we have the adj value, we map it to a rate by means of a learning
  40. * algorithm. This algorithm keeps the state of the percentual failed frames
  41. * difference between rates. The behaviour of the lowest available rate is kept
  42. * as a reference value, and every time we switch between two rates, we compute
  43. * the difference between the failed frames each rate exhibited. By doing so,
  44. * we compare behaviours which different rates exhibited in adjacent timeslices,
  45. * thus the comparison is minimally affected by external conditions. This
  46. * difference gets propagated to the whole set of measurements, so that the
  47. * reference is always the same. Periodically, we normalize this set so that
  48. * recent events weigh the most. By comparing the adj value with this set, we
  49. * avoid pejorative switches to lower rates and allow for switches to higher
  50. * rates if they behaved well.
  51. *
  52. * Note that for the computations we use a fixed-point representation to avoid
  53. * floating point arithmetic. Hence, all values are shifted left by
  54. * RC_PID_ARITH_SHIFT.
  55. */
  56. /* Sampling period for measuring percentage of failed frames. */
  57. #define RC_PID_INTERVAL (HZ / 8)
  58. /* Exponential averaging smoothness (used for I part of PID controller) */
  59. #define RC_PID_SMOOTHING_SHIFT 3
  60. #define RC_PID_SMOOTHING (1 << RC_PID_SMOOTHING_SHIFT)
  61. /* Fixed point arithmetic shifting amount. */
  62. #define RC_PID_ARITH_SHIFT 8
  63. /* Fixed point arithmetic factor. */
  64. #define RC_PID_ARITH_FACTOR (1 << RC_PID_ARITH_SHIFT)
  65. /* Proportional PID component coefficient. */
  66. #define RC_PID_COEFF_P 15
  67. /* Integral PID component coefficient. */
  68. #define RC_PID_COEFF_I 9
  69. /* Derivative PID component coefficient. */
  70. #define RC_PID_COEFF_D 15
  71. /* Target failed frames rate for the PID controller. NB: This effectively gives
  72. * maximum failed frames percentage we're willing to accept. If the wireless
  73. * link quality is good, the controller will fail to adjust failed frames
  74. * percentage to the target. This is intentional.
  75. */
  76. #define RC_PID_TARGET_PF (11 << RC_PID_ARITH_SHIFT)
  77. /* Rate behaviour normalization quantity over time. */
  78. #define RC_PID_NORM_OFFSET 3
  79. /* Push high rates right after loading. */
  80. #define RC_PID_FAST_START 0
  81. /* Arithmetic right shift for positive and negative values for ISO C. */
  82. #define RC_PID_DO_ARITH_RIGHT_SHIFT(x, y) \
  83. (x) < 0 ? -((-(x)) >> (y)) : (x) >> (y)
  84. struct rc_pid_sta_info {
  85. unsigned long last_change;
  86. unsigned long last_sample;
  87. u32 tx_num_failed;
  88. u32 tx_num_xmit;
  89. /* Average failed frames percentage error (i.e. actual vs. target
  90. * percentage), scaled by RC_PID_SMOOTHING. This value is computed
  91. * using using an exponential weighted average technique:
  92. *
  93. * (RC_PID_SMOOTHING - 1) * err_avg_old + err
  94. * err_avg = ------------------------------------------
  95. * RC_PID_SMOOTHING
  96. *
  97. * where err_avg is the new approximation, err_avg_old the previous one
  98. * and err is the error w.r.t. to the current failed frames percentage
  99. * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
  100. * given to the previous estimate, resulting in smoother behavior (i.e.
  101. * corresponding to a longer integration window).
  102. *
  103. * For computation, we actually don't use the above formula, but this
  104. * one:
  105. *
  106. * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
  107. *
  108. * where:
  109. * err_avg_scaled = err * RC_PID_SMOOTHING
  110. * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
  111. *
  112. * This avoids floating point numbers and the per_failed_old value can
  113. * easily be obtained by shifting per_failed_old_scaled right by
  114. * RC_PID_SMOOTHING_SHIFT.
  115. */
  116. s32 err_avg_sc;
  117. /* Last framed failes percentage sample */
  118. u32 last_pf;
  119. };
  120. /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
  121. * be tuned individually for each interface.
  122. */
  123. struct rc_pid_rateinfo {
  124. /* Map sorted rates to rates in ieee80211_hw_mode. */
  125. int index;
  126. /* Map rates in ieee80211_hw_mode to sorted rates. */
  127. int rev_index;
  128. /* Comparison with the lowest rate. */
  129. int diff;
  130. };
  131. struct rc_pid_info {
  132. /* The failed frames percentage target. */
  133. u32 target;
  134. /* P, I and D coefficients. */
  135. s32 coeff_p;
  136. s32 coeff_i;
  137. s32 coeff_d;
  138. /* Rates information. */
  139. struct rc_pid_rateinfo *rinfo;
  140. /* Index of the last used rate. */
  141. int oldrate;
  142. };
  143. /* Shift the adjustment so that we won't switch to a lower rate if it exhibited
  144. * a worse failed frames behaviour and we'll choose the highest rate whose
  145. * failed frames behaviour is not worse than the one of the original rate
  146. * target. While at it, check that the adjustment is within the ranges. Then,
  147. * provide the new rate index. */
  148. static int rate_control_pid_shift_adjust(struct rc_pid_rateinfo *r,
  149. int adj, int cur, int l)
  150. {
  151. int i, j, k, tmp;
  152. if (cur + adj < 0)
  153. return 0;
  154. if (cur + adj >= l)
  155. return l - 1;
  156. i = r[cur + adj].rev_index;
  157. j = r[cur].rev_index;
  158. if (adj < 0) {
  159. tmp = i;
  160. for (k = j; k >= i; k--)
  161. if (r[k].diff <= r[j].diff)
  162. tmp = k;
  163. return r[tmp].index;
  164. } else if (adj > 0) {
  165. tmp = i;
  166. for (k = i + 1; k + i < l; k++)
  167. if (r[k].diff <= r[i].diff)
  168. tmp = k;
  169. return r[tmp].index;
  170. }
  171. return cur + adj;
  172. }
  173. static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
  174. struct sta_info *sta, int adj,
  175. struct rc_pid_rateinfo *rinfo)
  176. {
  177. struct ieee80211_sub_if_data *sdata;
  178. struct ieee80211_hw_mode *mode;
  179. int newidx;
  180. int maxrate;
  181. int back = (adj > 0) ? 1 : -1;
  182. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  183. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  184. /* forced unicast rate - do not change STA rate */
  185. return;
  186. }
  187. mode = local->oper_hw_mode;
  188. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  189. newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate,
  190. mode->num_rates);
  191. while (newidx != sta->txrate) {
  192. if (rate_supported(sta, mode, newidx) &&
  193. (maxrate < 0 || newidx <= maxrate)) {
  194. sta->txrate = newidx;
  195. break;
  196. }
  197. newidx += back;
  198. }
  199. }
  200. /* Normalize the failed frames per-rate differences. */
  201. static void rate_control_pid_normalize(struct rc_pid_rateinfo *r, int l)
  202. {
  203. int i;
  204. if (r[0].diff > RC_PID_NORM_OFFSET)
  205. r[0].diff -= RC_PID_NORM_OFFSET;
  206. else if (r[0].diff < -RC_PID_NORM_OFFSET)
  207. r[0].diff += RC_PID_NORM_OFFSET;
  208. for (i = 0; i < l - 1; i++)
  209. if (r[i + 1].diff > r[i].diff + RC_PID_NORM_OFFSET)
  210. r[i + 1].diff -= RC_PID_NORM_OFFSET;
  211. else if (r[i + 1].diff <= r[i].diff)
  212. r[i + 1].diff += RC_PID_NORM_OFFSET;
  213. }
  214. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  215. struct ieee80211_local *local,
  216. struct sta_info *sta)
  217. {
  218. struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
  219. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  220. struct ieee80211_hw_mode *mode;
  221. u32 pf;
  222. s32 err_avg;
  223. s32 err_prop;
  224. s32 err_int;
  225. s32 err_der;
  226. int adj, i, j, tmp;
  227. mode = local->oper_hw_mode;
  228. spinfo = sta->rate_ctrl_priv;
  229. spinfo->last_sample = jiffies;
  230. /* If no frames were transmitted, we assume the old sample is
  231. * still a good measurement and copy it. */
  232. if (spinfo->tx_num_xmit == 0)
  233. pf = spinfo->last_pf;
  234. else {
  235. pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
  236. pf <<= RC_PID_ARITH_SHIFT;
  237. spinfo->tx_num_xmit = 0;
  238. spinfo->tx_num_failed = 0;
  239. }
  240. /* If we just switched rate, update the rate behaviour info. */
  241. if (pinfo->oldrate != sta->txrate) {
  242. i = rinfo[pinfo->oldrate].rev_index;
  243. j = rinfo[sta->txrate].rev_index;
  244. tmp = (pf - spinfo->last_pf);
  245. tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);
  246. rinfo[j].diff = rinfo[i].diff + tmp;
  247. pinfo->oldrate = sta->txrate;
  248. }
  249. rate_control_pid_normalize(rinfo, mode->num_rates);
  250. /* Compute the proportional, integral and derivative errors. */
  251. err_prop = RC_PID_TARGET_PF - pf;
  252. err_avg = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  253. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  254. err_int = spinfo->err_avg_sc >> RC_PID_SMOOTHING_SHIFT;
  255. err_der = pf - spinfo->last_pf;
  256. spinfo->last_pf = pf;
  257. /* Compute the controller output. */
  258. adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
  259. + err_der * pinfo->coeff_d);
  260. adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
  261. /* Change rate. */
  262. if (adj)
  263. rate_control_pid_adjust_rate(local, sta, adj, rinfo);
  264. }
  265. static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
  266. struct sk_buff *skb,
  267. struct ieee80211_tx_status *status)
  268. {
  269. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  270. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  271. struct rc_pid_info *pinfo = priv;
  272. struct sta_info *sta;
  273. struct rc_pid_sta_info *spinfo;
  274. sta = sta_info_get(local, hdr->addr1);
  275. if (!sta)
  276. return;
  277. /* Ignore all frames that were sent with a different rate than the rate
  278. * we currently advise mac80211 to use. */
  279. if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
  280. return;
  281. spinfo = sta->rate_ctrl_priv;
  282. spinfo->tx_num_xmit++;
  283. /* We count frames that totally failed to be transmitted as two bad
  284. * frames, those that made it out but had some retries as one good and
  285. * one bad frame. */
  286. if (status->excessive_retries) {
  287. spinfo->tx_num_failed += 2;
  288. spinfo->tx_num_xmit++;
  289. } else if (status->retry_count) {
  290. spinfo->tx_num_failed++;
  291. spinfo->tx_num_xmit++;
  292. }
  293. if (status->excessive_retries) {
  294. sta->tx_retry_failed++;
  295. sta->tx_num_consecutive_failures++;
  296. sta->tx_num_mpdu_fail++;
  297. } else {
  298. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  299. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  300. sta->last_ack_rssi[2] = status->ack_signal;
  301. sta->tx_num_consecutive_failures = 0;
  302. sta->tx_num_mpdu_ok++;
  303. }
  304. sta->tx_retry_count += status->retry_count;
  305. sta->tx_num_mpdu_fail += status->retry_count;
  306. /* Update PID controller state. */
  307. if (time_after(jiffies, spinfo->last_sample + RC_PID_INTERVAL))
  308. rate_control_pid_sample(pinfo, local, sta);
  309. sta_info_put(sta);
  310. }
  311. static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
  312. struct ieee80211_hw_mode *mode,
  313. struct sk_buff *skb,
  314. struct rate_selection *sel)
  315. {
  316. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  317. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  318. struct sta_info *sta;
  319. int rateidx;
  320. sta = sta_info_get(local, hdr->addr1);
  321. if (!sta) {
  322. sel->rate = rate_lowest(local, mode, NULL);
  323. sta_info_put(sta);
  324. return;
  325. }
  326. rateidx = sta->txrate;
  327. if (rateidx >= mode->num_rates)
  328. rateidx = mode->num_rates - 1;
  329. sta_info_put(sta);
  330. sel->rate = &mode->rates[rateidx];
  331. }
  332. static void rate_control_pid_rate_init(void *priv, void *priv_sta,
  333. struct ieee80211_local *local,
  334. struct sta_info *sta)
  335. {
  336. /* TODO: This routine should consider using RSSI from previous packets
  337. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  338. * Until that method is implemented, we will use the lowest supported
  339. * rate as a workaround. */
  340. sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
  341. }
  342. static void *rate_control_pid_alloc(struct ieee80211_local *local)
  343. {
  344. struct rc_pid_info *pinfo;
  345. struct rc_pid_rateinfo *rinfo;
  346. struct ieee80211_hw_mode *mode;
  347. int i, j, tmp;
  348. bool s;
  349. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  350. if (!pinfo)
  351. return NULL;
  352. /* We can safely assume that oper_hw_mode won't change unless we get
  353. * reinitialized. */
  354. mode = local->oper_hw_mode;
  355. rinfo = kmalloc(sizeof(*rinfo) * mode->num_rates, GFP_ATOMIC);
  356. if (!rinfo) {
  357. kfree(pinfo);
  358. return NULL;
  359. }
  360. /* Sort the rates. This is optimized for the most common case (i.e.
  361. * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
  362. * mapping too. */
  363. for (i = 0; i < mode->num_rates; i++) {
  364. rinfo[i].index = i;
  365. rinfo[i].rev_index = i;
  366. if (RC_PID_FAST_START)
  367. rinfo[i].diff = 0;
  368. else
  369. rinfo[i].diff = i * RC_PID_NORM_OFFSET;
  370. }
  371. for (i = 1; i < mode->num_rates; i++) {
  372. s = 0;
  373. for (j = 0; j < mode->num_rates - i; j++)
  374. if (unlikely(mode->rates[rinfo[j].index].rate >
  375. mode->rates[rinfo[j + 1].index].rate)) {
  376. tmp = rinfo[j].index;
  377. rinfo[j].index = rinfo[j + 1].index;
  378. rinfo[j + 1].index = tmp;
  379. rinfo[rinfo[j].index].rev_index = j;
  380. rinfo[rinfo[j + 1].index].rev_index = j + 1;
  381. s = 1;
  382. }
  383. if (!s)
  384. break;
  385. }
  386. pinfo->target = RC_PID_TARGET_PF;
  387. pinfo->coeff_p = RC_PID_COEFF_P;
  388. pinfo->coeff_i = RC_PID_COEFF_I;
  389. pinfo->coeff_d = RC_PID_COEFF_D;
  390. pinfo->rinfo = rinfo;
  391. pinfo->oldrate = 0;
  392. return pinfo;
  393. }
  394. static void rate_control_pid_free(void *priv)
  395. {
  396. struct rc_pid_info *pinfo = priv;
  397. kfree(pinfo->rinfo);
  398. kfree(pinfo);
  399. }
  400. static void rate_control_pid_clear(void *priv)
  401. {
  402. }
  403. static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
  404. {
  405. struct rc_pid_sta_info *spinfo;
  406. spinfo = kzalloc(sizeof(*spinfo), gfp);
  407. return spinfo;
  408. }
  409. static void rate_control_pid_free_sta(void *priv, void *priv_sta)
  410. {
  411. struct rc_pid_sta_info *spinfo = priv_sta;
  412. kfree(spinfo);
  413. }
  414. struct rate_control_ops mac80211_rcpid = {
  415. .name = "pid",
  416. .tx_status = rate_control_pid_tx_status,
  417. .get_rate = rate_control_pid_get_rate,
  418. .rate_init = rate_control_pid_rate_init,
  419. .clear = rate_control_pid_clear,
  420. .alloc = rate_control_pid_alloc,
  421. .free = rate_control_pid_free,
  422. .alloc_sta = rate_control_pid_alloc_sta,
  423. .free_sta = rate_control_pid_free_sta,
  424. };