rc80211_pid.c 15 KB

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