rc80211_pid_algo.c 16 KB

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