rc80211_pid_algo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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-2008, 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 <linux/debugfs.h>
  15. #include <net/mac80211.h>
  16. #include "rate.h"
  17. #include "mesh.h"
  18. #include "rc80211_pid.h"
  19. /* This is an implementation of a TX rate control algorithm that uses a PID
  20. * controller. Given a target failed frames rate, the controller decides about
  21. * TX rate changes to meet the target failed frames rate.
  22. *
  23. * The controller basically computes the following:
  24. *
  25. * adj = CP * err + CI * err_avg + CD * (err - last_err) * (1 + sharpening)
  26. *
  27. * where
  28. * adj adjustment value that is used to switch TX rate (see below)
  29. * err current error: target vs. current failed frames percentage
  30. * last_err last error
  31. * err_avg average (i.e. poor man's integral) of recent errors
  32. * sharpening non-zero when fast response is needed (i.e. right after
  33. * association or no frames sent for a long time), heading
  34. * to zero over time
  35. * CP Proportional coefficient
  36. * CI Integral coefficient
  37. * CD Derivative coefficient
  38. *
  39. * CP, CI, CD are subject to careful tuning.
  40. *
  41. * The integral component uses a exponential moving average approach instead of
  42. * an actual sliding window. The advantage is that we don't need to keep an
  43. * array of the last N error values and computation is easier.
  44. *
  45. * Once we have the adj value, we map it to a rate by means of a learning
  46. * algorithm. This algorithm keeps the state of the percentual failed frames
  47. * difference between rates. The behaviour of the lowest available rate is kept
  48. * as a reference value, and every time we switch between two rates, we compute
  49. * the difference between the failed frames each rate exhibited. By doing so,
  50. * we compare behaviours which different rates exhibited in adjacent timeslices,
  51. * thus the comparison is minimally affected by external conditions. This
  52. * difference gets propagated to the whole set of measurements, so that the
  53. * reference is always the same. Periodically, we normalize this set so that
  54. * recent events weigh the most. By comparing the adj value with this set, we
  55. * avoid pejorative switches to lower rates and allow for switches to higher
  56. * rates if they behaved well.
  57. *
  58. * Note that for the computations we use a fixed-point representation to avoid
  59. * floating point arithmetic. Hence, all values are shifted left by
  60. * RC_PID_ARITH_SHIFT.
  61. */
  62. /* Adjust the rate while ensuring that we won't switch to a lower rate if it
  63. * exhibited a worse failed frames behaviour and we'll choose the highest rate
  64. * whose failed frames behaviour is not worse than the one of the original rate
  65. * target. While at it, check that the new rate is valid. */
  66. static void rate_control_pid_adjust_rate(struct ieee80211_supported_band *sband,
  67. struct ieee80211_sta *sta,
  68. struct rc_pid_sta_info *spinfo, int adj,
  69. struct rc_pid_rateinfo *rinfo)
  70. {
  71. int cur_sorted, new_sorted, probe, tmp, n_bitrates, band;
  72. int cur = spinfo->txrate_idx;
  73. band = sband->band;
  74. n_bitrates = sband->n_bitrates;
  75. /* Map passed arguments to sorted values. */
  76. cur_sorted = rinfo[cur].rev_index;
  77. new_sorted = cur_sorted + adj;
  78. /* Check limits. */
  79. if (new_sorted < 0)
  80. new_sorted = rinfo[0].rev_index;
  81. else if (new_sorted >= n_bitrates)
  82. new_sorted = rinfo[n_bitrates - 1].rev_index;
  83. tmp = new_sorted;
  84. if (adj < 0) {
  85. /* Ensure that the rate decrease isn't disadvantageous. */
  86. for (probe = cur_sorted; probe >= new_sorted; probe--)
  87. if (rinfo[probe].diff <= rinfo[cur_sorted].diff &&
  88. rate_supported(sta, band, rinfo[probe].index))
  89. tmp = probe;
  90. } else {
  91. /* Look for rate increase with zero (or below) cost. */
  92. for (probe = new_sorted + 1; probe < n_bitrates; probe++)
  93. if (rinfo[probe].diff <= rinfo[new_sorted].diff &&
  94. rate_supported(sta, band, rinfo[probe].index))
  95. tmp = probe;
  96. }
  97. /* Fit the rate found to the nearest supported rate. */
  98. do {
  99. if (rate_supported(sta, band, rinfo[tmp].index)) {
  100. spinfo->txrate_idx = rinfo[tmp].index;
  101. break;
  102. }
  103. if (adj < 0)
  104. tmp--;
  105. else
  106. tmp++;
  107. } while (tmp < n_bitrates && tmp >= 0);
  108. #ifdef CONFIG_MAC80211_DEBUGFS
  109. rate_control_pid_event_rate_change(&spinfo->events,
  110. spinfo->txrate_idx,
  111. sband->bitrates[spinfo->txrate_idx].bitrate);
  112. #endif
  113. }
  114. /* Normalize the failed frames per-rate differences. */
  115. static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
  116. {
  117. int i, norm_offset = pinfo->norm_offset;
  118. struct rc_pid_rateinfo *r = pinfo->rinfo;
  119. if (r[0].diff > norm_offset)
  120. r[0].diff -= norm_offset;
  121. else if (r[0].diff < -norm_offset)
  122. r[0].diff += norm_offset;
  123. for (i = 0; i < l - 1; i++)
  124. if (r[i + 1].diff > r[i].diff + norm_offset)
  125. r[i + 1].diff -= norm_offset;
  126. else if (r[i + 1].diff <= r[i].diff)
  127. r[i + 1].diff += norm_offset;
  128. }
  129. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  130. struct ieee80211_supported_band *sband,
  131. struct ieee80211_sta *sta,
  132. struct rc_pid_sta_info *spinfo)
  133. {
  134. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  135. u32 pf;
  136. s32 err_avg;
  137. u32 err_prop;
  138. u32 err_int;
  139. u32 err_der;
  140. int adj, i, j, tmp;
  141. unsigned long period;
  142. /* In case nothing happened during the previous control interval, turn
  143. * the sharpening factor on. */
  144. period = (HZ * pinfo->sampling_period + 500) / 1000;
  145. if (!period)
  146. period = 1;
  147. if (jiffies - spinfo->last_sample > 2 * period)
  148. spinfo->sharp_cnt = pinfo->sharpen_duration;
  149. spinfo->last_sample = jiffies;
  150. /* This should never happen, but in case, we assume the old sample is
  151. * still a good measurement and copy it. */
  152. if (unlikely(spinfo->tx_num_xmit == 0))
  153. pf = spinfo->last_pf;
  154. else {
  155. /* XXX: BAD HACK!!! */
  156. struct sta_info *si = container_of(sta, struct sta_info, sta);
  157. pf = spinfo->tx_num_failed * 100 / spinfo->tx_num_xmit;
  158. if (ieee80211_vif_is_mesh(&si->sdata->vif) && pf == 100)
  159. mesh_plink_broken(si);
  160. pf <<= RC_PID_ARITH_SHIFT;
  161. si->fail_avg = ((pf + (spinfo->last_pf << 3)) / 9)
  162. >> 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 != spinfo->txrate_idx) {
  168. i = rinfo[pinfo->oldrate].rev_index;
  169. j = rinfo[spinfo->txrate_idx].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 = spinfo->txrate_idx;
  174. }
  175. rate_control_pid_normalize(pinfo, sband->n_bitrates);
  176. /* Compute the proportional, integral and derivative errors. */
  177. err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
  178. err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  179. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  180. err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  181. err_der = (pf - spinfo->last_pf) *
  182. (1 + pinfo->sharpen_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(sband, sta, spinfo, adj, rinfo);
  197. }
  198. static void rate_control_pid_tx_status(void *priv, struct ieee80211_supported_band *sband,
  199. struct ieee80211_sta *sta, void *priv_sta,
  200. struct sk_buff *skb)
  201. {
  202. struct rc_pid_info *pinfo = priv;
  203. struct rc_pid_sta_info *spinfo = priv_sta;
  204. unsigned long period;
  205. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  206. if (!spinfo)
  207. return;
  208. /* Ignore all frames that were sent with a different rate than the rate
  209. * we currently advise mac80211 to use. */
  210. if (info->status.rates[0].idx != spinfo->txrate_idx)
  211. return;
  212. spinfo->tx_num_xmit++;
  213. #ifdef CONFIG_MAC80211_DEBUGFS
  214. rate_control_pid_event_tx_status(&spinfo->events, info);
  215. #endif
  216. /* We count frames that totally failed to be transmitted as two bad
  217. * frames, those that made it out but had some retries as one good and
  218. * one bad frame. */
  219. if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
  220. spinfo->tx_num_failed += 2;
  221. spinfo->tx_num_xmit++;
  222. } else if (info->status.rates[0].count > 1) {
  223. spinfo->tx_num_failed++;
  224. spinfo->tx_num_xmit++;
  225. }
  226. /* Update PID controller state. */
  227. period = (HZ * pinfo->sampling_period + 500) / 1000;
  228. if (!period)
  229. period = 1;
  230. if (time_after(jiffies, spinfo->last_sample + period))
  231. rate_control_pid_sample(pinfo, sband, sta, spinfo);
  232. }
  233. static void
  234. rate_control_pid_get_rate(void *priv, struct ieee80211_sta *sta,
  235. void *priv_sta,
  236. struct ieee80211_tx_rate_control *txrc)
  237. {
  238. struct sk_buff *skb = txrc->skb;
  239. struct ieee80211_supported_band *sband = txrc->sband;
  240. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  241. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  242. struct rc_pid_sta_info *spinfo = priv_sta;
  243. int rateidx;
  244. u16 fc;
  245. if (txrc->rts)
  246. info->control.rates[0].count =
  247. txrc->hw->conf.long_frame_max_tx_count;
  248. else
  249. info->control.rates[0].count =
  250. txrc->hw->conf.short_frame_max_tx_count;
  251. /* Send management frames and NO_ACK data using lowest rate. */
  252. fc = le16_to_cpu(hdr->frame_control);
  253. if (!sta || !spinfo ||
  254. (fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
  255. info->flags & IEEE80211_TX_CTL_NO_ACK) {
  256. info->control.rates[0].idx = rate_lowest_index(sband, sta);
  257. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  258. info->control.rates[0].count = 1;
  259. return;
  260. }
  261. rateidx = spinfo->txrate_idx;
  262. if (rateidx >= sband->n_bitrates)
  263. rateidx = sband->n_bitrates - 1;
  264. info->control.rates[0].idx = rateidx;
  265. #ifdef CONFIG_MAC80211_DEBUGFS
  266. rate_control_pid_event_tx_rate(&spinfo->events,
  267. rateidx, sband->bitrates[rateidx].bitrate);
  268. #endif
  269. }
  270. static void
  271. rate_control_pid_rate_init(void *priv, struct ieee80211_supported_band *sband,
  272. struct ieee80211_sta *sta, void *priv_sta)
  273. {
  274. struct rc_pid_sta_info *spinfo = priv_sta;
  275. struct rc_pid_info *pinfo = priv;
  276. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  277. struct sta_info *si;
  278. int i, j, tmp;
  279. bool s;
  280. /* TODO: This routine should consider using RSSI from previous packets
  281. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  282. * Until that method is implemented, we will use the lowest supported
  283. * rate as a workaround. */
  284. /* Sort the rates. This is optimized for the most common case (i.e.
  285. * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
  286. * mapping too. */
  287. for (i = 0; i < sband->n_bitrates; i++) {
  288. rinfo[i].index = i;
  289. rinfo[i].rev_index = i;
  290. if (RC_PID_FAST_START)
  291. rinfo[i].diff = 0;
  292. else
  293. rinfo[i].diff = i * pinfo->norm_offset;
  294. }
  295. for (i = 1; i < sband->n_bitrates; i++) {
  296. s = 0;
  297. for (j = 0; j < sband->n_bitrates - i; j++)
  298. if (unlikely(sband->bitrates[rinfo[j].index].bitrate >
  299. sband->bitrates[rinfo[j + 1].index].bitrate)) {
  300. tmp = rinfo[j].index;
  301. rinfo[j].index = rinfo[j + 1].index;
  302. rinfo[j + 1].index = tmp;
  303. rinfo[rinfo[j].index].rev_index = j;
  304. rinfo[rinfo[j + 1].index].rev_index = j + 1;
  305. s = 1;
  306. }
  307. if (!s)
  308. break;
  309. }
  310. spinfo->txrate_idx = rate_lowest_index(sband, sta);
  311. /* HACK */
  312. si = container_of(sta, struct sta_info, sta);
  313. si->fail_avg = 0;
  314. }
  315. static void *rate_control_pid_alloc(struct ieee80211_hw *hw,
  316. struct dentry *debugfsdir)
  317. {
  318. struct rc_pid_info *pinfo;
  319. struct rc_pid_rateinfo *rinfo;
  320. struct ieee80211_supported_band *sband;
  321. int i, max_rates = 0;
  322. #ifdef CONFIG_MAC80211_DEBUGFS
  323. struct rc_pid_debugfs_entries *de;
  324. #endif
  325. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  326. if (!pinfo)
  327. return NULL;
  328. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  329. sband = hw->wiphy->bands[i];
  330. if (sband && sband->n_bitrates > max_rates)
  331. max_rates = sband->n_bitrates;
  332. }
  333. rinfo = kmalloc(sizeof(*rinfo) * max_rates, GFP_ATOMIC);
  334. if (!rinfo) {
  335. kfree(pinfo);
  336. return NULL;
  337. }
  338. pinfo->target = RC_PID_TARGET_PF;
  339. pinfo->sampling_period = RC_PID_INTERVAL;
  340. pinfo->coeff_p = RC_PID_COEFF_P;
  341. pinfo->coeff_i = RC_PID_COEFF_I;
  342. pinfo->coeff_d = RC_PID_COEFF_D;
  343. pinfo->smoothing_shift = RC_PID_SMOOTHING_SHIFT;
  344. pinfo->sharpen_factor = RC_PID_SHARPENING_FACTOR;
  345. pinfo->sharpen_duration = RC_PID_SHARPENING_DURATION;
  346. pinfo->norm_offset = RC_PID_NORM_OFFSET;
  347. pinfo->rinfo = rinfo;
  348. pinfo->oldrate = 0;
  349. #ifdef CONFIG_MAC80211_DEBUGFS
  350. de = &pinfo->dentries;
  351. de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
  352. debugfsdir, &pinfo->target);
  353. de->sampling_period = debugfs_create_u32("sampling_period",
  354. S_IRUSR | S_IWUSR, debugfsdir,
  355. &pinfo->sampling_period);
  356. de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
  357. debugfsdir, (u32 *)&pinfo->coeff_p);
  358. de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
  359. debugfsdir, (u32 *)&pinfo->coeff_i);
  360. de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
  361. debugfsdir, (u32 *)&pinfo->coeff_d);
  362. de->smoothing_shift = debugfs_create_u32("smoothing_shift",
  363. S_IRUSR | S_IWUSR, debugfsdir,
  364. &pinfo->smoothing_shift);
  365. de->sharpen_factor = debugfs_create_u32("sharpen_factor",
  366. S_IRUSR | S_IWUSR, debugfsdir,
  367. &pinfo->sharpen_factor);
  368. de->sharpen_duration = debugfs_create_u32("sharpen_duration",
  369. S_IRUSR | S_IWUSR, debugfsdir,
  370. &pinfo->sharpen_duration);
  371. de->norm_offset = debugfs_create_u32("norm_offset",
  372. S_IRUSR | S_IWUSR, debugfsdir,
  373. &pinfo->norm_offset);
  374. #endif
  375. return pinfo;
  376. }
  377. static void rate_control_pid_free(void *priv)
  378. {
  379. struct rc_pid_info *pinfo = priv;
  380. #ifdef CONFIG_MAC80211_DEBUGFS
  381. struct rc_pid_debugfs_entries *de = &pinfo->dentries;
  382. debugfs_remove(de->norm_offset);
  383. debugfs_remove(de->sharpen_duration);
  384. debugfs_remove(de->sharpen_factor);
  385. debugfs_remove(de->smoothing_shift);
  386. debugfs_remove(de->coeff_d);
  387. debugfs_remove(de->coeff_i);
  388. debugfs_remove(de->coeff_p);
  389. debugfs_remove(de->sampling_period);
  390. debugfs_remove(de->target);
  391. #endif
  392. kfree(pinfo->rinfo);
  393. kfree(pinfo);
  394. }
  395. static void *rate_control_pid_alloc_sta(void *priv, struct ieee80211_sta *sta,
  396. gfp_t gfp)
  397. {
  398. struct rc_pid_sta_info *spinfo;
  399. spinfo = kzalloc(sizeof(*spinfo), gfp);
  400. if (spinfo == NULL)
  401. return NULL;
  402. spinfo->last_sample = jiffies;
  403. #ifdef CONFIG_MAC80211_DEBUGFS
  404. spin_lock_init(&spinfo->events.lock);
  405. init_waitqueue_head(&spinfo->events.waitqueue);
  406. #endif
  407. return spinfo;
  408. }
  409. static void rate_control_pid_free_sta(void *priv, struct ieee80211_sta *sta,
  410. void *priv_sta)
  411. {
  412. kfree(priv_sta);
  413. }
  414. static 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. .alloc = rate_control_pid_alloc,
  420. .free = rate_control_pid_free,
  421. .alloc_sta = rate_control_pid_alloc_sta,
  422. .free_sta = rate_control_pid_free_sta,
  423. #ifdef CONFIG_MAC80211_DEBUGFS
  424. .add_sta_debugfs = rate_control_pid_add_sta_debugfs,
  425. .remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
  426. #endif
  427. };
  428. int __init rc80211_pid_init(void)
  429. {
  430. return ieee80211_rate_control_register(&mac80211_rcpid);
  431. }
  432. void rc80211_pid_exit(void)
  433. {
  434. ieee80211_rate_control_unregister(&mac80211_rcpid);
  435. }