rc80211_pid.h 7.7 KB

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