rc80211_minstrel.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __RC_MINSTREL_H
  9. #define __RC_MINSTREL_H
  10. #define EWMA_LEVEL 96 /* ewma weighting factor [/EWMA_DIV] */
  11. #define EWMA_DIV 128
  12. #define SAMPLE_COLUMNS 10 /* number of columns in sample table */
  13. /* scaled fraction values */
  14. #define MINSTREL_SCALE 12
  15. #define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
  16. #define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
  17. /* number of highest throughput rates to consider*/
  18. #define MAX_THR_RATES 4
  19. /*
  20. * Perform EWMA (Exponentially Weighted Moving Average) calculation
  21. */
  22. static inline int
  23. minstrel_ewma(int old, int new, int weight)
  24. {
  25. int diff, incr;
  26. diff = new - old;
  27. incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
  28. return old + incr;
  29. }
  30. /*
  31. * Perform EWMV (Exponentially Weighted Moving Variance) calculation
  32. */
  33. static inline int
  34. minstrel_ewmv(int old_ewmv, int cur_prob, int prob_ewma, int weight)
  35. {
  36. int diff, incr;
  37. diff = cur_prob - prob_ewma;
  38. incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
  39. return weight * (old_ewmv + MINSTREL_TRUNC(diff * incr)) / EWMA_DIV;
  40. }
  41. struct minstrel_rate_stats {
  42. /* current / last sampling period attempts/success counters */
  43. u16 attempts, last_attempts;
  44. u16 success, last_success;
  45. /* total attempts/success counters */
  46. u32 att_hist, succ_hist;
  47. /* statistis of packet delivery probability
  48. * prob_ewma - exponential weighted moving average of prob
  49. * prob_ewmsd - exp. weighted moving standard deviation of prob */
  50. u16 prob_ewma;
  51. u16 prob_ewmv;
  52. /* maximum retry counts */
  53. u8 retry_count;
  54. u8 retry_count_rtscts;
  55. u8 sample_skipped;
  56. bool retry_updated;
  57. };
  58. struct minstrel_rate {
  59. int bitrate;
  60. s8 rix;
  61. u8 retry_count_cts;
  62. u8 adjusted_retry_count;
  63. unsigned int perfect_tx_time;
  64. unsigned int ack_time;
  65. int sample_limit;
  66. struct minstrel_rate_stats stats;
  67. };
  68. struct minstrel_sta_info {
  69. struct ieee80211_sta *sta;
  70. unsigned long last_stats_update;
  71. unsigned int sp_ack_dur;
  72. unsigned int rate_avg;
  73. unsigned int lowest_rix;
  74. u8 max_tp_rate[MAX_THR_RATES];
  75. u8 max_prob_rate;
  76. unsigned int total_packets;
  77. unsigned int sample_packets;
  78. int sample_deferred;
  79. unsigned int sample_row;
  80. unsigned int sample_column;
  81. int n_rates;
  82. struct minstrel_rate *r;
  83. bool prev_sample;
  84. /* sampling table */
  85. u8 *sample_table;
  86. #ifdef CONFIG_MAC80211_DEBUGFS
  87. struct dentry *dbg_stats;
  88. struct dentry *dbg_stats_csv;
  89. #endif
  90. };
  91. struct minstrel_priv {
  92. struct ieee80211_hw *hw;
  93. bool has_mrr;
  94. unsigned int cw_min;
  95. unsigned int cw_max;
  96. unsigned int max_retry;
  97. unsigned int segment_size;
  98. unsigned int update_interval;
  99. unsigned int lookaround_rate;
  100. unsigned int lookaround_rate_mrr;
  101. u8 cck_rates[4];
  102. #ifdef CONFIG_MAC80211_DEBUGFS
  103. /*
  104. * enable fixed rate processing per RC
  105. * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
  106. * - write -1 to enable RC processing again
  107. * - setting will be applied on next update
  108. */
  109. u32 fixed_rate_idx;
  110. struct dentry *dbg_fixed_rate;
  111. #endif
  112. };
  113. struct minstrel_debugfs_info {
  114. size_t len;
  115. char buf[];
  116. };
  117. /* Get EWMSD (Exponentially Weighted Moving Standard Deviation) * 10 */
  118. static inline int
  119. minstrel_get_ewmsd10(struct minstrel_rate_stats *mrs)
  120. {
  121. unsigned int ewmv = mrs->prob_ewmv;
  122. return int_sqrt(MINSTREL_TRUNC(ewmv * 1000 * 1000));
  123. }
  124. extern const struct rate_control_ops mac80211_minstrel;
  125. void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
  126. void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
  127. /* Recalculate success probabilities and counters for a given rate using EWMA */
  128. void minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs);
  129. int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma);
  130. /* debugfs */
  131. int minstrel_stats_open(struct inode *inode, struct file *file);
  132. int minstrel_stats_csv_open(struct inode *inode, struct file *file);
  133. ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos);
  134. int minstrel_stats_release(struct inode *inode, struct file *file);
  135. #endif