rc80211_pid.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
  3. * Copyright 2007, Stefano Brivio <stefano.brivio@polimi.it>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #ifndef RC80211_PID_H
  10. #define RC80211_PID_H
  11. /* Sampling period for measuring percentage of failed frames in ms. */
  12. #define RC_PID_INTERVAL 125
  13. /* Exponential averaging smoothness (used for I part of PID controller) */
  14. #define RC_PID_SMOOTHING_SHIFT 3
  15. #define RC_PID_SMOOTHING (1 << RC_PID_SMOOTHING_SHIFT)
  16. /* Sharpening factor (used for D part of PID controller) */
  17. #define RC_PID_SHARPENING_FACTOR 0
  18. #define RC_PID_SHARPENING_DURATION 0
  19. /* Fixed point arithmetic shifting amount. */
  20. #define RC_PID_ARITH_SHIFT 8
  21. /* Fixed point arithmetic factor. */
  22. #define RC_PID_ARITH_FACTOR (1 << RC_PID_ARITH_SHIFT)
  23. /* Proportional PID component coefficient. */
  24. #define RC_PID_COEFF_P 15
  25. /* Integral PID component coefficient. */
  26. #define RC_PID_COEFF_I 9
  27. /* Derivative PID component coefficient. */
  28. #define RC_PID_COEFF_D 15
  29. /* Target failed frames rate for the PID controller. NB: This effectively gives
  30. * maximum failed frames percentage we're willing to accept. If the wireless
  31. * link quality is good, the controller will fail to adjust failed frames
  32. * percentage to the target. This is intentional.
  33. */
  34. #define RC_PID_TARGET_PF 14
  35. /* Rate behaviour normalization quantity over time. */
  36. #define RC_PID_NORM_OFFSET 3
  37. /* Push high rates right after loading. */
  38. #define RC_PID_FAST_START 0
  39. /* Arithmetic right shift for positive and negative values for ISO C. */
  40. #define RC_PID_DO_ARITH_RIGHT_SHIFT(x, y) \
  41. ((x) < 0 ? -((-(x)) >> (y)) : (x) >> (y))
  42. enum rc_pid_event_type {
  43. RC_PID_EVENT_TYPE_TX_STATUS,
  44. RC_PID_EVENT_TYPE_RATE_CHANGE,
  45. RC_PID_EVENT_TYPE_TX_RATE,
  46. RC_PID_EVENT_TYPE_PF_SAMPLE,
  47. };
  48. union rc_pid_event_data {
  49. /* RC_PID_EVENT_TX_STATUS */
  50. struct {
  51. u32 flags;
  52. struct ieee80211_tx_info tx_status;
  53. };
  54. /* RC_PID_EVENT_TYPE_RATE_CHANGE */
  55. /* RC_PID_EVENT_TYPE_TX_RATE */
  56. struct {
  57. int index;
  58. int rate;
  59. };
  60. /* RC_PID_EVENT_TYPE_PF_SAMPLE */
  61. struct {
  62. s32 pf_sample;
  63. s32 prop_err;
  64. s32 int_err;
  65. s32 der_err;
  66. };
  67. };
  68. struct rc_pid_event {
  69. /* The time when the event occured */
  70. unsigned long timestamp;
  71. /* Event ID number */
  72. unsigned int id;
  73. /* Type of event */
  74. enum rc_pid_event_type type;
  75. /* type specific data */
  76. union rc_pid_event_data data;
  77. };
  78. /* Size of the event ring buffer. */
  79. #define RC_PID_EVENT_RING_SIZE 32
  80. struct rc_pid_event_buffer {
  81. /* Counter that generates event IDs */
  82. unsigned int ev_count;
  83. /* Ring buffer of events */
  84. struct rc_pid_event ring[RC_PID_EVENT_RING_SIZE];
  85. /* Index to the entry in events_buf to be reused */
  86. unsigned int next_entry;
  87. /* Lock that guards against concurrent access to this buffer struct */
  88. spinlock_t lock;
  89. /* Wait queue for poll/select and blocking I/O */
  90. wait_queue_head_t waitqueue;
  91. };
  92. struct rc_pid_events_file_info {
  93. /* The event buffer we read */
  94. struct rc_pid_event_buffer *events;
  95. /* The entry we have should read next */
  96. unsigned int next_entry;
  97. };
  98. /**
  99. * struct rc_pid_debugfs_entries - tunable parameters
  100. *
  101. * Algorithm parameters, tunable via debugfs.
  102. * @target: target percentage for failed frames
  103. * @sampling_period: error sampling interval in milliseconds
  104. * @coeff_p: absolute value of the proportional coefficient
  105. * @coeff_i: absolute value of the integral coefficient
  106. * @coeff_d: absolute value of the derivative coefficient
  107. * @smoothing_shift: absolute value of the integral smoothing factor (i.e.
  108. * amount of smoothing introduced by the exponential moving average)
  109. * @sharpen_factor: absolute value of the derivative sharpening factor (i.e.
  110. * amount of emphasis given to the derivative term after low activity
  111. * events)
  112. * @sharpen_duration: duration of the sharpening effect after the detected low
  113. * activity event, relative to sampling_period
  114. * @norm_offset: amount of normalization periodically performed on the learnt
  115. * rate behaviour values (lower means we should trust more what we learnt
  116. * about behaviour of rates, higher means we should trust more the natural
  117. * ordering of rates)
  118. */
  119. struct rc_pid_debugfs_entries {
  120. struct dentry *target;
  121. struct dentry *sampling_period;
  122. struct dentry *coeff_p;
  123. struct dentry *coeff_i;
  124. struct dentry *coeff_d;
  125. struct dentry *smoothing_shift;
  126. struct dentry *sharpen_factor;
  127. struct dentry *sharpen_duration;
  128. struct dentry *norm_offset;
  129. };
  130. void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
  131. struct ieee80211_tx_info *stat);
  132. void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
  133. int index, int rate);
  134. void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
  135. int index, int rate);
  136. void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
  137. s32 pf_sample, s32 prop_err,
  138. s32 int_err, s32 der_err);
  139. void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
  140. struct dentry *dir);
  141. void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta);
  142. struct rc_pid_sta_info {
  143. unsigned long last_change;
  144. unsigned long last_sample;
  145. u32 tx_num_failed;
  146. u32 tx_num_xmit;
  147. int txrate_idx;
  148. /* Average failed frames percentage error (i.e. actual vs. target
  149. * percentage), scaled by RC_PID_SMOOTHING. This value is computed
  150. * using using an exponential weighted average technique:
  151. *
  152. * (RC_PID_SMOOTHING - 1) * err_avg_old + err
  153. * err_avg = ------------------------------------------
  154. * RC_PID_SMOOTHING
  155. *
  156. * where err_avg is the new approximation, err_avg_old the previous one
  157. * and err is the error w.r.t. to the current failed frames percentage
  158. * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
  159. * given to the previous estimate, resulting in smoother behavior (i.e.
  160. * corresponding to a longer integration window).
  161. *
  162. * For computation, we actually don't use the above formula, but this
  163. * one:
  164. *
  165. * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
  166. *
  167. * where:
  168. * err_avg_scaled = err * RC_PID_SMOOTHING
  169. * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
  170. *
  171. * This avoids floating point numbers and the per_failed_old value can
  172. * easily be obtained by shifting per_failed_old_scaled right by
  173. * RC_PID_SMOOTHING_SHIFT.
  174. */
  175. s32 err_avg_sc;
  176. /* Last framed failes percentage sample. */
  177. u32 last_pf;
  178. /* Sharpening needed. */
  179. u8 sharp_cnt;
  180. #ifdef CONFIG_MAC80211_DEBUGFS
  181. /* Event buffer */
  182. struct rc_pid_event_buffer events;
  183. /* Events debugfs file entry */
  184. struct dentry *events_entry;
  185. #endif
  186. };
  187. /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
  188. * be tuned individually for each interface.
  189. */
  190. struct rc_pid_rateinfo {
  191. /* Map sorted rates to rates in ieee80211_hw_mode. */
  192. int index;
  193. /* Map rates in ieee80211_hw_mode to sorted rates. */
  194. int rev_index;
  195. /* Did we do any measurement on this rate? */
  196. bool valid;
  197. /* Comparison with the lowest rate. */
  198. int diff;
  199. };
  200. struct rc_pid_info {
  201. /* The failed frames percentage target. */
  202. unsigned int target;
  203. /* Rate at which failed frames percentage is sampled in 0.001s. */
  204. unsigned int sampling_period;
  205. /* P, I and D coefficients. */
  206. int coeff_p;
  207. int coeff_i;
  208. int coeff_d;
  209. /* Exponential averaging shift. */
  210. unsigned int smoothing_shift;
  211. /* Sharpening factor and duration. */
  212. unsigned int sharpen_factor;
  213. unsigned int sharpen_duration;
  214. /* Normalization offset. */
  215. unsigned int norm_offset;
  216. /* Rates information. */
  217. struct rc_pid_rateinfo *rinfo;
  218. /* Index of the last used rate. */
  219. int oldrate;
  220. #ifdef CONFIG_MAC80211_DEBUGFS
  221. /* Debugfs entries created for the parameters above. */
  222. struct rc_pid_debugfs_entries dentries;
  223. #endif
  224. };
  225. #endif /* RC80211_PID_H */