rc80211_pid_algo.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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_local *local,
  67. struct sta_info *sta, int adj,
  68. struct rc_pid_rateinfo *rinfo)
  69. {
  70. struct ieee80211_sub_if_data *sdata;
  71. struct ieee80211_supported_band *sband;
  72. int cur_sorted, new_sorted, probe, tmp, n_bitrates, band;
  73. struct rc_pid_sta_info *spinfo = (void *)sta->rate_ctrl_priv;
  74. int cur = spinfo->txrate_idx;
  75. sdata = sta->sdata;
  76. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  77. band = sband->band;
  78. n_bitrates = sband->n_bitrates;
  79. /* Map passed arguments to sorted values. */
  80. cur_sorted = rinfo[cur].rev_index;
  81. new_sorted = cur_sorted + adj;
  82. /* Check limits. */
  83. if (new_sorted < 0)
  84. new_sorted = rinfo[0].rev_index;
  85. else if (new_sorted >= n_bitrates)
  86. new_sorted = rinfo[n_bitrates - 1].rev_index;
  87. tmp = new_sorted;
  88. if (adj < 0) {
  89. /* Ensure that the rate decrease isn't disadvantageous. */
  90. for (probe = cur_sorted; probe >= new_sorted; probe--)
  91. if (rinfo[probe].diff <= rinfo[cur_sorted].diff &&
  92. rate_supported(sta, band, rinfo[probe].index))
  93. tmp = probe;
  94. } else {
  95. /* Look for rate increase with zero (or below) cost. */
  96. for (probe = new_sorted + 1; probe < n_bitrates; probe++)
  97. if (rinfo[probe].diff <= rinfo[new_sorted].diff &&
  98. rate_supported(sta, band, rinfo[probe].index))
  99. tmp = probe;
  100. }
  101. /* Fit the rate found to the nearest supported rate. */
  102. do {
  103. if (rate_supported(sta, band, rinfo[tmp].index)) {
  104. spinfo->txrate_idx = rinfo[tmp].index;
  105. break;
  106. }
  107. if (adj < 0)
  108. tmp--;
  109. else
  110. tmp++;
  111. } while (tmp < n_bitrates && tmp >= 0);
  112. #ifdef CONFIG_MAC80211_DEBUGFS
  113. rate_control_pid_event_rate_change(&spinfo->events,
  114. spinfo->txrate_idx,
  115. sband->bitrates[spinfo->txrate_idx].bitrate);
  116. #endif
  117. }
  118. /* Normalize the failed frames per-rate differences. */
  119. static void rate_control_pid_normalize(struct rc_pid_info *pinfo, int l)
  120. {
  121. int i, norm_offset = pinfo->norm_offset;
  122. struct rc_pid_rateinfo *r = pinfo->rinfo;
  123. if (r[0].diff > norm_offset)
  124. r[0].diff -= norm_offset;
  125. else if (r[0].diff < -norm_offset)
  126. r[0].diff += norm_offset;
  127. for (i = 0; i < l - 1; i++)
  128. if (r[i + 1].diff > r[i].diff + norm_offset)
  129. r[i + 1].diff -= norm_offset;
  130. else if (r[i + 1].diff <= r[i].diff)
  131. r[i + 1].diff += norm_offset;
  132. }
  133. static void rate_control_pid_sample(struct rc_pid_info *pinfo,
  134. struct ieee80211_local *local,
  135. struct sta_info *sta)
  136. {
  137. struct ieee80211_sub_if_data *sdata = sta->sdata;
  138. struct rc_pid_sta_info *spinfo = sta->rate_ctrl_priv;
  139. struct rc_pid_rateinfo *rinfo = pinfo->rinfo;
  140. struct ieee80211_supported_band *sband;
  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. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  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. if (ieee80211_vif_is_mesh(&sdata->vif) && pf == 100)
  165. mesh_plink_broken(sta);
  166. pf <<= RC_PID_ARITH_SHIFT;
  167. sta->fail_avg = ((pf + (spinfo->last_pf << 3)) / 9)
  168. >> RC_PID_ARITH_SHIFT;
  169. }
  170. spinfo->tx_num_xmit = 0;
  171. spinfo->tx_num_failed = 0;
  172. /* If we just switched rate, update the rate behaviour info. */
  173. if (pinfo->oldrate != spinfo->txrate_idx) {
  174. i = rinfo[pinfo->oldrate].rev_index;
  175. j = rinfo[spinfo->txrate_idx].rev_index;
  176. tmp = (pf - spinfo->last_pf);
  177. tmp = RC_PID_DO_ARITH_RIGHT_SHIFT(tmp, RC_PID_ARITH_SHIFT);
  178. rinfo[j].diff = rinfo[i].diff + tmp;
  179. pinfo->oldrate = spinfo->txrate_idx;
  180. }
  181. rate_control_pid_normalize(pinfo, sband->n_bitrates);
  182. /* Compute the proportional, integral and derivative errors. */
  183. err_prop = (pinfo->target << RC_PID_ARITH_SHIFT) - pf;
  184. err_avg = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  185. spinfo->err_avg_sc = spinfo->err_avg_sc - err_avg + err_prop;
  186. err_int = spinfo->err_avg_sc >> pinfo->smoothing_shift;
  187. err_der = (pf - spinfo->last_pf) *
  188. (1 + pinfo->sharpen_factor * spinfo->sharp_cnt);
  189. spinfo->last_pf = pf;
  190. if (spinfo->sharp_cnt)
  191. spinfo->sharp_cnt--;
  192. #ifdef CONFIG_MAC80211_DEBUGFS
  193. rate_control_pid_event_pf_sample(&spinfo->events, pf, err_prop, err_int,
  194. err_der);
  195. #endif
  196. /* Compute the controller output. */
  197. adj = (err_prop * pinfo->coeff_p + err_int * pinfo->coeff_i
  198. + err_der * pinfo->coeff_d);
  199. adj = RC_PID_DO_ARITH_RIGHT_SHIFT(adj, 2 * RC_PID_ARITH_SHIFT);
  200. /* Change rate. */
  201. if (adj)
  202. rate_control_pid_adjust_rate(local, sta, adj, rinfo);
  203. }
  204. static void rate_control_pid_tx_status(void *priv, struct net_device *dev,
  205. struct sk_buff *skb)
  206. {
  207. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  208. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  209. struct ieee80211_sub_if_data *sdata;
  210. struct rc_pid_info *pinfo = priv;
  211. struct sta_info *sta;
  212. struct rc_pid_sta_info *spinfo;
  213. unsigned long period;
  214. struct ieee80211_supported_band *sband;
  215. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  216. rcu_read_lock();
  217. sta = sta_info_get(local, hdr->addr1);
  218. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  219. if (!sta)
  220. goto unlock;
  221. spinfo = sta->rate_ctrl_priv;
  222. /* Don't update the state if we're not controlling the rate. */
  223. sdata = sta->sdata;
  224. if (sdata->force_unicast_rateidx > -1) {
  225. spinfo->txrate_idx = sdata->max_ratectrl_rateidx;
  226. goto unlock;
  227. }
  228. /* Ignore all frames that were sent with a different rate than the rate
  229. * we currently advise mac80211 to use. */
  230. if (info->tx_rate_idx != spinfo->txrate_idx)
  231. goto unlock;
  232. spinfo->tx_num_xmit++;
  233. #ifdef CONFIG_MAC80211_DEBUGFS
  234. rate_control_pid_event_tx_status(&spinfo->events, info);
  235. #endif
  236. /* We count frames that totally failed to be transmitted as two bad
  237. * frames, those that made it out but had some retries as one good and
  238. * one bad frame. */
  239. if (info->status.excessive_retries) {
  240. spinfo->tx_num_failed += 2;
  241. spinfo->tx_num_xmit++;
  242. } else if (info->status.retry_count) {
  243. spinfo->tx_num_failed++;
  244. spinfo->tx_num_xmit++;
  245. }
  246. /* Update PID controller state. */
  247. period = (HZ * pinfo->sampling_period + 500) / 1000;
  248. if (!period)
  249. period = 1;
  250. if (time_after(jiffies, spinfo->last_sample + period))
  251. rate_control_pid_sample(pinfo, local, sta);
  252. unlock:
  253. rcu_read_unlock();
  254. }
  255. static void rate_control_pid_get_rate(void *priv, struct net_device *dev,
  256. struct ieee80211_supported_band *sband,
  257. struct sk_buff *skb,
  258. struct rate_selection *sel)
  259. {
  260. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  261. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  262. struct ieee80211_sub_if_data *sdata;
  263. struct rc_pid_sta_info *spinfo;
  264. struct sta_info *sta;
  265. int rateidx;
  266. u16 fc;
  267. rcu_read_lock();
  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_idx = rate_lowest_index(local, sband, sta);
  275. rcu_read_unlock();
  276. return;
  277. }
  278. /* If a forced rate is in effect, select it. */
  279. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  280. spinfo = (struct rc_pid_sta_info *)sta->rate_ctrl_priv;
  281. if (sdata->force_unicast_rateidx > -1)
  282. spinfo->txrate_idx = sdata->force_unicast_rateidx;
  283. rateidx = spinfo->txrate_idx;
  284. if (rateidx >= sband->n_bitrates)
  285. rateidx = sband->n_bitrates - 1;
  286. rcu_read_unlock();
  287. sel->rate_idx = rateidx;
  288. #ifdef CONFIG_MAC80211_DEBUGFS
  289. rate_control_pid_event_tx_rate(
  290. &((struct rc_pid_sta_info *) sta->rate_ctrl_priv)->events,
  291. rateidx, sband->bitrates[rateidx].bitrate);
  292. #endif
  293. }
  294. static void rate_control_pid_rate_init(void *priv, void *priv_sta,
  295. struct ieee80211_local *local,
  296. struct sta_info *sta)
  297. {
  298. /* TODO: This routine should consider using RSSI from previous packets
  299. * as we need to have IEEE 802.1X auth succeed immediately after assoc..
  300. * Until that method is implemented, we will use the lowest supported
  301. * rate as a workaround. */
  302. struct ieee80211_supported_band *sband;
  303. struct rc_pid_sta_info *spinfo = (void *)sta->rate_ctrl_priv;
  304. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  305. spinfo->txrate_idx = rate_lowest_index(local, sband, sta);
  306. sta->fail_avg = 0;
  307. }
  308. static void *rate_control_pid_alloc(struct ieee80211_local *local)
  309. {
  310. struct rc_pid_info *pinfo;
  311. struct rc_pid_rateinfo *rinfo;
  312. struct ieee80211_supported_band *sband;
  313. int i, j, tmp;
  314. bool s;
  315. #ifdef CONFIG_MAC80211_DEBUGFS
  316. struct rc_pid_debugfs_entries *de;
  317. #endif
  318. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  319. pinfo = kmalloc(sizeof(*pinfo), GFP_ATOMIC);
  320. if (!pinfo)
  321. return NULL;
  322. /* We can safely assume that sband won't change unless we get
  323. * reinitialized. */
  324. rinfo = kmalloc(sizeof(*rinfo) * sband->n_bitrates, GFP_ATOMIC);
  325. if (!rinfo) {
  326. kfree(pinfo);
  327. return NULL;
  328. }
  329. pinfo->target = RC_PID_TARGET_PF;
  330. pinfo->sampling_period = RC_PID_INTERVAL;
  331. pinfo->coeff_p = RC_PID_COEFF_P;
  332. pinfo->coeff_i = RC_PID_COEFF_I;
  333. pinfo->coeff_d = RC_PID_COEFF_D;
  334. pinfo->smoothing_shift = RC_PID_SMOOTHING_SHIFT;
  335. pinfo->sharpen_factor = RC_PID_SHARPENING_FACTOR;
  336. pinfo->sharpen_duration = RC_PID_SHARPENING_DURATION;
  337. pinfo->norm_offset = RC_PID_NORM_OFFSET;
  338. pinfo->rinfo = rinfo;
  339. pinfo->oldrate = 0;
  340. /* Sort the rates. This is optimized for the most common case (i.e.
  341. * almost-sorted CCK+OFDM rates). Kind of bubble-sort with reversed
  342. * mapping too. */
  343. for (i = 0; i < sband->n_bitrates; i++) {
  344. rinfo[i].index = i;
  345. rinfo[i].rev_index = i;
  346. if (RC_PID_FAST_START)
  347. rinfo[i].diff = 0;
  348. else
  349. rinfo[i].diff = i * pinfo->norm_offset;
  350. }
  351. for (i = 1; i < sband->n_bitrates; i++) {
  352. s = 0;
  353. for (j = 0; j < sband->n_bitrates - i; j++)
  354. if (unlikely(sband->bitrates[rinfo[j].index].bitrate >
  355. sband->bitrates[rinfo[j + 1].index].bitrate)) {
  356. tmp = rinfo[j].index;
  357. rinfo[j].index = rinfo[j + 1].index;
  358. rinfo[j + 1].index = tmp;
  359. rinfo[rinfo[j].index].rev_index = j;
  360. rinfo[rinfo[j + 1].index].rev_index = j + 1;
  361. s = 1;
  362. }
  363. if (!s)
  364. break;
  365. }
  366. #ifdef CONFIG_MAC80211_DEBUGFS
  367. de = &pinfo->dentries;
  368. de->dir = debugfs_create_dir("rc80211_pid",
  369. local->hw.wiphy->debugfsdir);
  370. de->target = debugfs_create_u32("target_pf", S_IRUSR | S_IWUSR,
  371. de->dir, &pinfo->target);
  372. de->sampling_period = debugfs_create_u32("sampling_period",
  373. S_IRUSR | S_IWUSR, de->dir,
  374. &pinfo->sampling_period);
  375. de->coeff_p = debugfs_create_u32("coeff_p", S_IRUSR | S_IWUSR,
  376. de->dir, &pinfo->coeff_p);
  377. de->coeff_i = debugfs_create_u32("coeff_i", S_IRUSR | S_IWUSR,
  378. de->dir, &pinfo->coeff_i);
  379. de->coeff_d = debugfs_create_u32("coeff_d", S_IRUSR | S_IWUSR,
  380. de->dir, &pinfo->coeff_d);
  381. de->smoothing_shift = debugfs_create_u32("smoothing_shift",
  382. S_IRUSR | S_IWUSR, de->dir,
  383. &pinfo->smoothing_shift);
  384. de->sharpen_factor = debugfs_create_u32("sharpen_factor",
  385. S_IRUSR | S_IWUSR, de->dir,
  386. &pinfo->sharpen_factor);
  387. de->sharpen_duration = debugfs_create_u32("sharpen_duration",
  388. S_IRUSR | S_IWUSR, de->dir,
  389. &pinfo->sharpen_duration);
  390. de->norm_offset = debugfs_create_u32("norm_offset",
  391. S_IRUSR | S_IWUSR, de->dir,
  392. &pinfo->norm_offset);
  393. #endif
  394. return pinfo;
  395. }
  396. static void rate_control_pid_free(void *priv)
  397. {
  398. struct rc_pid_info *pinfo = priv;
  399. #ifdef CONFIG_MAC80211_DEBUGFS
  400. struct rc_pid_debugfs_entries *de = &pinfo->dentries;
  401. debugfs_remove(de->norm_offset);
  402. debugfs_remove(de->sharpen_duration);
  403. debugfs_remove(de->sharpen_factor);
  404. debugfs_remove(de->smoothing_shift);
  405. debugfs_remove(de->coeff_d);
  406. debugfs_remove(de->coeff_i);
  407. debugfs_remove(de->coeff_p);
  408. debugfs_remove(de->sampling_period);
  409. debugfs_remove(de->target);
  410. debugfs_remove(de->dir);
  411. #endif
  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. if (spinfo == NULL)
  423. return NULL;
  424. spinfo->last_sample = jiffies;
  425. #ifdef CONFIG_MAC80211_DEBUGFS
  426. spin_lock_init(&spinfo->events.lock);
  427. init_waitqueue_head(&spinfo->events.waitqueue);
  428. #endif
  429. return spinfo;
  430. }
  431. static void rate_control_pid_free_sta(void *priv, void *priv_sta)
  432. {
  433. struct rc_pid_sta_info *spinfo = priv_sta;
  434. kfree(spinfo);
  435. }
  436. static struct rate_control_ops mac80211_rcpid = {
  437. .name = "pid",
  438. .tx_status = rate_control_pid_tx_status,
  439. .get_rate = rate_control_pid_get_rate,
  440. .rate_init = rate_control_pid_rate_init,
  441. .clear = rate_control_pid_clear,
  442. .alloc = rate_control_pid_alloc,
  443. .free = rate_control_pid_free,
  444. .alloc_sta = rate_control_pid_alloc_sta,
  445. .free_sta = rate_control_pid_free_sta,
  446. #ifdef CONFIG_MAC80211_DEBUGFS
  447. .add_sta_debugfs = rate_control_pid_add_sta_debugfs,
  448. .remove_sta_debugfs = rate_control_pid_remove_sta_debugfs,
  449. #endif
  450. };
  451. int __init rc80211_pid_init(void)
  452. {
  453. return ieee80211_rate_control_register(&mac80211_rcpid);
  454. }
  455. void rc80211_pid_exit(void)
  456. {
  457. ieee80211_rate_control_unregister(&mac80211_rcpid);
  458. }