rc80211_pid_algo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. j = r[cur].rev_index;
  70. i = j + adj;
  71. if (i < 0)
  72. return r[0].index;
  73. if (i >= l - 1)
  74. return r[l - 1].index;
  75. tmp = i;
  76. if (adj < 0) {
  77. for (k = j; k >= i; k--)
  78. if (r[k].diff <= r[j].diff)
  79. tmp = k;
  80. } else {
  81. for (k = i + 1; k + i < l; k++)
  82. if (r[k].diff <= r[i].diff)
  83. tmp = k;
  84. }
  85. return r[tmp].index;
  86. }
  87. static void rate_control_pid_adjust_rate(struct ieee80211_local *local,
  88. struct sta_info *sta, int adj,
  89. struct rc_pid_rateinfo *rinfo)
  90. {
  91. struct ieee80211_sub_if_data *sdata;
  92. struct ieee80211_hw_mode *mode;
  93. int newidx;
  94. int maxrate;
  95. int back = (adj > 0) ? 1 : -1;
  96. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  97. if (sdata->bss && sdata->bss->force_unicast_rateidx > -1) {
  98. /* forced unicast rate - do not change STA rate */
  99. return;
  100. }
  101. mode = local->oper_hw_mode;
  102. maxrate = sdata->bss ? sdata->bss->max_ratectrl_rateidx : -1;
  103. newidx = rate_control_pid_shift_adjust(rinfo, adj, sta->txrate,
  104. mode->num_rates);
  105. while (newidx != sta->txrate) {
  106. if (rate_supported(sta, mode, newidx) &&
  107. (maxrate < 0 || newidx <= maxrate)) {
  108. sta->txrate = newidx;
  109. break;
  110. }
  111. newidx += back;
  112. }
  113. #ifdef CONFIG_MAC80211_DEBUGFS
  114. rate_control_pid_event_rate_change(
  115. &((struct rc_pid_sta_info *)sta->rate_ctrl_priv)->events,
  116. newidx, mode->rates[newidx].rate);
  117. #endif
  118. }
  119. /* Normalize the failed frames per-rate differences. */
  120. static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
  121. {
  122. int i, norm_offset = pinfo->norm_offset;
  123. struct rc_pid_rateinfo *r = pinfo->rinfo;
  124. if (r[0].diff > norm_offset)
  125. r[0].diff -= norm_offset;
  126. else if (r[0].diff < -norm_offset)
  127. r[0].diff += norm_offset;
  128. for (i = 0; i < l - 1; i++)
  129. if (r[i + 1].diff > r[i].diff + norm_offset)
  130. r[i + 1].diff -= norm_offset;
  131. else if (r[i + 1].diff <= r[i].diff)
  132. r[i + 1].diff += norm_offset;
  133. }
  134. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  135. struct ieee80211_local *local,
  136. struct sta_info *sta)
  137. {
  138. struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
  139. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  140. struct ieee80211_hw_mode *mode;
  141. u32 pf;
  142. s32 err_avg;
  143. u32 err_prop;
  144. u32 err_int;
  145. u32 err_der;
  146. int adj, i, j, tmp;
  147. unsigned long period;
  148. mode = local->oper_hw_mode;
  149. spinfo = sta->rate_ctrl_priv;
  150. /* In case nothing happened during the previous control interval, turn
  151. * the sharpening factor on. */
  152. period = (HZ * pinfo->sampling_period + 500) / 1000;
  153. if (!period)
  154. period = 1;
  155. if (jiffies - spinfo->last_sample > 2 * period)
  156. spinfo->sharp_cnt = pinfo->sharpen_duration;
  157. spinfo->last_sample = jiffies;
  158. /* This should never happen, but in case, we assume the old sample is
  159. * still a good measurement and copy it. */
  160. if (unlikely(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. }
  166. spinfo->tx_num_xmit = 0;
  167. spinfo->tx_num_failed = 0;
  168. /* If we just switched rate, update the rate behaviour info. */
  169. if (pinfo->oldrate != sta->txrate) {
  170. i = rinfo[pinfo->oldrate].rev_index;
  171. j = rinfo[sta->txrate].rev_index;
  172. tmp = (pf - spinfo->last_pf);
  173. tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);
  174. rinfo[j].diff = rinfo[i].diff + tmp;
  175. pinfo->oldrate = sta->txrate;
  176. }
  177. rate_control_pid_normalize(pinfo, mode->num_rates);
  178. /* Compute the proportional, integral and derivative errors. */
  179. err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
  180. err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  181. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  182. err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  183. err_der = (pf - spinfo->last_pf) *
  184. (1 + pinfo->sharpen_factor * spinfo->sharp_cnt);
  185. spinfo->last_pf = pf;
  186. if (spinfo->sharp_cnt)
  187. spinfo->sharp_cnt--;
  188. #ifdef CONFIG_MAC80211_DEBUGFS
  189. rate_control_pid_event_pf_sample(&spinfo->events, pf, err_prop, err_int,
  190. err_der);
  191. #endif
  192. /* Compute the controller output. */
  193. adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
  194. + err_der * pinfo->coeff_d);
  195. adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
  196. /* Change rate. */
  197. if (adj)
  198. rate_control_pid_adjust_rate(local, sta, adj, rinfo);
  199. }
  200. static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
  201. struct sk_buff *skb,
  202. struct ieee80211_tx_status *status)
  203. {
  204. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  205. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  206. struct rc_pid_info *pinfo = priv;
  207. struct sta_info *sta;
  208. struct rc_pid_sta_info *spinfo;
  209. unsigned long period;
  210. sta = sta_info_get(local, hdr->addr1);
  211. if (!sta)
  212. return;
  213. /* Ignore all frames that were sent with a different rate than the rate
  214. * we currently advise mac80211 to use. */
  215. if (status->control.rate != &local->oper_hw_mode->rates[sta->txrate])
  216. return;
  217. spinfo = sta->rate_ctrl_priv;
  218. spinfo->tx_num_xmit++;
  219. #ifdef CONFIG_MAC80211_DEBUGFS
  220. rate_control_pid_event_tx_status(&spinfo->events, status);
  221. #endif
  222. /* We count frames that totally failed to be transmitted as two bad
  223. * frames, those that made it out but had some retries as one good and
  224. * one bad frame. */
  225. if (status->excessive_retries) {
  226. spinfo->tx_num_failed += 2;
  227. spinfo->tx_num_xmit++;
  228. } else if (status->retry_count) {
  229. spinfo->tx_num_failed++;
  230. spinfo->tx_num_xmit++;
  231. }
  232. if (status->excessive_retries) {
  233. sta->tx_retry_failed++;
  234. sta->tx_num_consecutive_failures++;
  235. sta->tx_num_mpdu_fail++;
  236. } else {
  237. sta->last_ack_rssi[0] = sta->last_ack_rssi[1];
  238. sta->last_ack_rssi[1] = sta->last_ack_rssi[2];
  239. sta->last_ack_rssi[2] = status->ack_signal;
  240. sta->tx_num_consecutive_failures = 0;
  241. sta->tx_num_mpdu_ok++;
  242. }
  243. sta->tx_retry_count += status->retry_count;
  244. sta->tx_num_mpdu_fail += status->retry_count;
  245. /* Update PID controller state. */
  246. period = (HZ * pinfo->sampling_period + 500) / 1000;
  247. if (!period)
  248. period = 1;
  249. if (time_after(jiffies, spinfo->last_sample + period))
  250. rate_control_pid_sample(pinfo, local, sta);
  251. sta_info_put(sta);
  252. }
  253. static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
  254. struct ieee80211_hw_mode *mode,
  255. struct sk_buff *skb,
  256. struct rate_selection *sel)
  257. {
  258. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  259. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  260. struct sta_info *sta;
  261. int rateidx;
  262. sta = sta_info_get(local, hdr->addr1);
  263. if (!sta) {
  264. sel->rate = rate_lowest(local, mode, NULL);
  265. sta_info_put(sta);
  266. return;
  267. }
  268. rateidx = sta->txrate;
  269. if (rateidx >= mode->num_rates)
  270. rateidx = mode->num_rates - 1;
  271. sta_info_put(sta);
  272. sel->rate = &mode->rates[rateidx];
  273. #ifdef CONFIG_MAC80211_DEBUGFS
  274. rate_control_pid_event_tx_rate(
  275. &((struct rc_pid_sta_info *) sta->rate_ctrl_priv)->events,
  276. rateidx, mode->rates[rateidx].rate);
  277. #endif
  278. }
  279. static void rate_control_pid_rate_init(void *priv, void *priv_sta,
  280. struct ieee80211_local *local,
  281. struct sta_info *sta)
  282. {
  283. /* TODO: This routine should consider using RSSI from previous packets
  284. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  285. * Until that method is implemented, we will use the lowest supported
  286. * rate as a workaround. */
  287. sta->txrate = rate_lowest_index(local, local->oper_hw_mode, sta);
  288. }
  289. static void *rate_control_pid_alloc(struct ieee80211_local *local)
  290. {
  291. struct rc_pid_info *pinfo;
  292. struct rc_pid_rateinfo *rinfo;
  293. struct ieee80211_hw_mode *mode;
  294. int i, j, tmp;
  295. bool s;
  296. #ifdef CONFIG_MAC80211_DEBUGFS
  297. struct rc_pid_debugfs_entries *de;
  298. #endif
  299. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  300. if (!pinfo)
  301. return NULL;
  302. /* We can safely assume that oper_hw_mode won't change unless we get
  303. * reinitialized. */
  304. mode = local->oper_hw_mode;
  305. rinfo = kmalloc(sizeof(*rinfo) * mode->num_rates, GFP_ATOMIC);
  306. if (!rinfo) {
  307. kfree(pinfo);
  308. return NULL;
  309. }
  310. /* Sort the rates. This is optimized for the most common case (i.e.
  311. * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
  312. * mapping too. */
  313. for (i = 0; i < mode->num_rates; i++) {
  314. rinfo[i].index = i;
  315. rinfo[i].rev_index = i;
  316. if (pinfo->fast_start)
  317. rinfo[i].diff = 0;
  318. else
  319. rinfo[i].diff = i * pinfo->norm_offset;
  320. }
  321. for (i = 1; i < mode->num_rates; i++) {
  322. s = 0;
  323. for (j = 0; j < mode->num_rates - i; j++)
  324. if (unlikely(mode->rates[rinfo[j].index].rate >
  325. mode->rates[rinfo[j + 1].index].rate)) {
  326. tmp = rinfo[j].index;
  327. rinfo[j].index = rinfo[j + 1].index;
  328. rinfo[j + 1].index = tmp;
  329. rinfo[rinfo[j].index].rev_index = j;
  330. rinfo[rinfo[j + 1].index].rev_index = j + 1;
  331. s = 1;
  332. }
  333. if (!s)
  334. break;
  335. }
  336. pinfo->target = RC_PID_TARGET_PF;
  337. pinfo->sampling_period = RC_PID_INTERVAL;
  338. pinfo->coeff_p = RC_PID_COEFF_P;
  339. pinfo->coeff_i = RC_PID_COEFF_I;
  340. pinfo->coeff_d = RC_PID_COEFF_D;
  341. pinfo->smoothing_shift = RC_PID_SMOOTHING_SHIFT;
  342. pinfo->sharpen_factor = RC_PID_SHARPENING_FACTOR;
  343. pinfo->sharpen_duration = RC_PID_SHARPENING_DURATION;
  344. pinfo->norm_offset = RC_PID_NORM_OFFSET;
  345. pinfo->fast_start = RC_PID_FAST_START;
  346. pinfo->rinfo = rinfo;
  347. pinfo->oldrate = 0;
  348. #ifdef CONFIG_MAC80211_DEBUGFS
  349. de = &pinfo->dentries;
  350. de->dir = debugfs_create_dir("rc80211_pid",
  351. local->hw.wiphy->debugfsdir);
  352. de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
  353. de->dir, &pinfo->target);
  354. de->sampling_period = debugfs_create_u32("sampling_period",
  355. S_IRUSR | S_IWUSR, de->dir,
  356. &pinfo->sampling_period);
  357. de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
  358. de->dir, &pinfo->coeff_p);
  359. de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
  360. de->dir, &pinfo->coeff_i);
  361. de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
  362. de->dir, &pinfo->coeff_d);
  363. de->smoothing_shift = debugfs_create_u32("smoothing_shift",
  364. S_IRUSR | S_IWUSR, de->dir,
  365. &pinfo->smoothing_shift);
  366. de->sharpen_factor = debugfs_create_u32("sharpen_factor",
  367. S_IRUSR | S_IWUSR, de->dir,
  368. &pinfo->sharpen_factor);
  369. de->sharpen_duration = debugfs_create_u32("sharpen_duration",
  370. S_IRUSR | S_IWUSR, de->dir,
  371. &pinfo->sharpen_duration);
  372. de->norm_offset = debugfs_create_u32("norm_offset",
  373. S_IRUSR | S_IWUSR, de->dir,
  374. &pinfo->norm_offset);
  375. de->fast_start = debugfs_create_bool("fast_start",
  376. S_IRUSR | S_IWUSR, de->dir,
  377. &pinfo->fast_start);
  378. #endif
  379. return pinfo;
  380. }
  381. static void rate_control_pid_free(void *priv)
  382. {
  383. struct rc_pid_info *pinfo = priv;
  384. #ifdef CONFIG_MAC80211_DEBUGFS
  385. struct rc_pid_debugfs_entries *de = &pinfo->dentries;
  386. debugfs_remove(de->fast_start);
  387. debugfs_remove(de->norm_offset);
  388. debugfs_remove(de->sharpen_duration);
  389. debugfs_remove(de->sharpen_factor);
  390. debugfs_remove(de->smoothing_shift);
  391. debugfs_remove(de->coeff_d);
  392. debugfs_remove(de->coeff_i);
  393. debugfs_remove(de->coeff_p);
  394. debugfs_remove(de->sampling_period);
  395. debugfs_remove(de->target);
  396. debugfs_remove(de->dir);
  397. #endif
  398. kfree(pinfo->rinfo);
  399. kfree(pinfo);
  400. }
  401. static void rate_control_pid_clear(void *priv)
  402. {
  403. }
  404. static void *rate_control_pid_alloc_sta(void *priv, gfp_t gfp)
  405. {
  406. struct rc_pid_sta_info *spinfo;
  407. spinfo = kzalloc(sizeof(*spinfo), gfp);
  408. if (spinfo == NULL)
  409. return NULL;
  410. #ifdef CONFIG_MAC80211_DEBUGFS
  411. spin_lock_init(&spinfo->events.lock);
  412. init_waitqueue_head(&spinfo->events.waitqueue);
  413. #endif
  414. return spinfo;
  415. }
  416. static void rate_control_pid_free_sta(void *priv, void *priv_sta)
  417. {
  418. struct rc_pid_sta_info *spinfo = priv_sta;
  419. kfree(spinfo);
  420. }
  421. struct rate_control_ops mac80211_rcpid = {
  422. .name = "pid",
  423. .tx_status = rate_control_pid_tx_status,
  424. .get_rate = rate_control_pid_get_rate,
  425. .rate_init = rate_control_pid_rate_init,
  426. .clear = rate_control_pid_clear,
  427. .alloc = rate_control_pid_alloc,
  428. .free = rate_control_pid_free,
  429. .alloc_sta = rate_control_pid_alloc_sta,
  430. .free_sta = rate_control_pid_free_sta,
  431. #ifdef CONFIG_MAC80211_DEBUGFS
  432. .add_sta_debugfs = rate_control_pid_add_sta_debugfs,
  433. .remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
  434. #endif
  435. };