rc80211_pid.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. struct ieee80211_tx_info tx_status;
  52. };
  53. /* RC_PID_EVENT_TYPE_RATE_CHANGE */
  54. /* RC_PID_EVENT_TYPE_TX_RATE */
  55. struct {
  56. int index;
  57. int rate;
  58. };
  59. /* RC_PID_EVENT_TYPE_PF_SAMPLE */
  60. struct {
  61. s32 pf_sample;
  62. s32 prop_err;
  63. s32 int_err;
  64. s32 der_err;
  65. };
  66. };
  67. struct rc_pid_event {
  68. /* The time when the event occured */
  69. unsigned long timestamp;
  70. /* Event ID number */
  71. unsigned int id;
  72. /* Type of event */
  73. enum rc_pid_event_type type;
  74. /* type specific data */
  75. union rc_pid_event_data data;
  76. };
  77. /* Size of the event ring buffer. */
  78. #define RC_PID_EVENT_RING_SIZE 32
  79. struct rc_pid_event_buffer {
  80. /* Counter that generates event IDs */
  81. unsigned int ev_count;
  82. /* Ring buffer of events */
  83. struct rc_pid_event ring[RC_PID_EVENT_RING_SIZE];
  84. /* Index to the entry in events_buf to be reused */
  85. unsigned int next_entry;
  86. /* Lock that guards against concurrent access to this buffer struct */
  87. spinlock_t lock;
  88. /* Wait queue for poll/select and blocking I/O */
  89. wait_queue_head_t waitqueue;
  90. };
  91. struct rc_pid_events_file_info {
  92. /* The event buffer we read */
  93. struct rc_pid_event_buffer *events;
  94. /* The entry we have should read next */
  95. unsigned int next_entry;
  96. };
  97. /**
  98. * struct rc_pid_debugfs_entries - tunable parameters
  99. *
  100. * Algorithm parameters, tunable via debugfs.
  101. * @target: target percentage for failed frames
  102. * @sampling_period: error sampling interval in milliseconds
  103. * @coeff_p: absolute value of the proportional coefficient
  104. * @coeff_i: absolute value of the integral coefficient
  105. * @coeff_d: absolute value of the derivative coefficient
  106. * @smoothing_shift: absolute value of the integral smoothing factor (i.e.
  107. * amount of smoothing introduced by the exponential moving average)
  108. * @sharpen_factor: absolute value of the derivative sharpening factor (i.e.
  109. * amount of emphasis given to the derivative term after low activity
  110. * events)
  111. * @sharpen_duration: duration of the sharpening effect after the detected low
  112. * activity event, relative to sampling_period
  113. * @norm_offset: amount of normalization periodically performed on the learnt
  114. * rate behaviour values (lower means we should trust more what we learnt
  115. * about behaviour of rates, higher means we should trust more the natural
  116. * ordering of rates)
  117. */
  118. struct rc_pid_debugfs_entries {
  119. struct dentry *target;
  120. struct dentry *sampling_period;
  121. struct dentry *coeff_p;
  122. struct dentry *coeff_i;
  123. struct dentry *coeff_d;
  124. struct dentry *smoothing_shift;
  125. struct dentry *sharpen_factor;
  126. struct dentry *sharpen_duration;
  127. struct dentry *norm_offset;
  128. };
  129. void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
  130. struct ieee80211_tx_info *stat);
  131. void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
  132. int index, int rate);
  133. void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
  134. int index, int rate);
  135. void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
  136. s32 pf_sample, s32 prop_err,
  137. s32 int_err, s32 der_err);
  138. void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
  139. struct dentry *dir);
  140. void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta);
  141. struct rc_pid_sta_info {
  142. unsigned long last_change;
  143. unsigned long last_sample;
  144. u32 tx_num_failed;
  145. u32 tx_num_xmit;
  146. int txrate_idx;
  147. /* Average failed frames percentage error (i.e. actual vs. target
  148. * percentage), scaled by RC_PID_SMOOTHING. This value is computed
  149. * using using an exponential weighted average technique:
  150. *
  151. * (RC_PID_SMOOTHING - 1) * err_avg_old + err
  152. * err_avg = ------------------------------------------
  153. * RC_PID_SMOOTHING
  154. *
  155. * where err_avg is the new approximation, err_avg_old the previous one
  156. * and err is the error w.r.t. to the current failed frames percentage
  157. * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
  158. * given to the previous estimate, resulting in smoother behavior (i.e.
  159. * corresponding to a longer integration window).
  160. *
  161. * For computation, we actually don't use the above formula, but this
  162. * one:
  163. *
  164. * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
  165. *
  166. * where:
  167. * err_avg_scaled = err * RC_PID_SMOOTHING
  168. * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
  169. *
  170. * This avoids floating point numbers and the per_failed_old value can
  171. * easily be obtained by shifting per_failed_old_scaled right by
  172. * RC_PID_SMOOTHING_SHIFT.
  173. */
  174. s32 err_avg_sc;
  175. /* Last framed failes percentage sample. */
  176. u32 last_pf;
  177. /* Sharpening needed. */
  178. u8 sharp_cnt;
  179. #ifdef CONFIG_MAC80211_DEBUGFS
  180. /* Event buffer */
  181. struct rc_pid_event_buffer events;
  182. /* Events debugfs file entry */
  183. struct dentry *events_entry;
  184. #endif
  185. };
  186. /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
  187. * be tuned individually for each interface.
  188. */
  189. struct rc_pid_rateinfo {
  190. /* Map sorted rates to rates in ieee80211_hw_mode. */
  191. int index;
  192. /* Map rates in ieee80211_hw_mode to sorted rates. */
  193. int rev_index;
  194. /* Did we do any measurement on this rate? */
  195. bool valid;
  196. /* Comparison with the lowest rate. */
  197. int diff;
  198. };
  199. struct rc_pid_info {
  200. /* The failed frames percentage target. */
  201. unsigned int target;
  202. /* Rate at which failed frames percentage is sampled in 0.001s. */
  203. unsigned int sampling_period;
  204. /* P, I and D coefficients. */
  205. int coeff_p;
  206. int coeff_i;
  207. int coeff_d;
  208. /* Exponential averaging shift. */
  209. unsigned int smoothing_shift;
  210. /* Sharpening factor and duration. */
  211. unsigned int sharpen_factor;
  212. unsigned int sharpen_duration;
  213. /* Normalization offset. */
  214. unsigned int norm_offset;
  215. /* Rates information. */
  216. struct rc_pid_rateinfo *rinfo;
  217. /* Index of the last used rate. */
  218. int oldrate;
  219. #ifdef CONFIG_MAC80211_DEBUGFS
  220. /* Debugfs entries created for the parameters above. */
  221. struct rc_pid_debugfs_entries dentries;
  222. #endif
  223. };
  224. #endif /* RC80211_PID_H */