rc80211_minstrel.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 16
  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 EWMSD (Exponentially Weighted Moving Standard Deviation) calculation
  32. */
  33. static inline int
  34. minstrel_ewmsd(int old_ewmsd, int cur_prob, int prob_ewma, int weight)
  35. {
  36. int diff, incr, tmp_var;
  37. /* calculate exponential weighted moving variance */
  38. diff = MINSTREL_TRUNC((cur_prob - prob_ewma) * 1000000);
  39. incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
  40. tmp_var = old_ewmsd * old_ewmsd;
  41. tmp_var = weight * (tmp_var + diff * incr / 1000000) / EWMA_DIV;
  42. /* return standard deviation */
  43. return (u16) int_sqrt(tmp_var);
  44. }
  45. struct minstrel_rate_stats {
  46. /* current / last sampling period attempts/success counters */
  47. u16 attempts, last_attempts;
  48. u16 success, last_success;
  49. /* total attempts/success counters */
  50. u64 att_hist, succ_hist;
  51. /* statistis of packet delivery probability
  52. * cur_prob - current prob within last update intervall
  53. * prob_ewma - exponential weighted moving average of prob
  54. * prob_ewmsd - exp. weighted moving standard deviation of prob */
  55. unsigned int cur_prob;
  56. unsigned int prob_ewma;
  57. u16 prob_ewmsd;
  58. /* maximum retry counts */
  59. u8 retry_count;
  60. u8 retry_count_rtscts;
  61. u8 sample_skipped;
  62. bool retry_updated;
  63. };
  64. struct minstrel_rate {
  65. int bitrate;
  66. s8 rix;
  67. u8 retry_count_cts;
  68. u8 adjusted_retry_count;
  69. unsigned int perfect_tx_time;
  70. unsigned int ack_time;
  71. int sample_limit;
  72. struct minstrel_rate_stats stats;
  73. };
  74. struct minstrel_sta_info {
  75. struct ieee80211_sta *sta;
  76. unsigned long last_stats_update;
  77. unsigned int sp_ack_dur;
  78. unsigned int rate_avg;
  79. unsigned int lowest_rix;
  80. u8 max_tp_rate[MAX_THR_RATES];
  81. u8 max_prob_rate;
  82. unsigned int total_packets;
  83. unsigned int sample_packets;
  84. int sample_deferred;
  85. unsigned int sample_row;
  86. unsigned int sample_column;
  87. int n_rates;
  88. struct minstrel_rate *r;
  89. bool prev_sample;
  90. /* sampling table */
  91. u8 *sample_table;
  92. #ifdef CONFIG_MAC80211_DEBUGFS
  93. struct dentry *dbg_stats;
  94. struct dentry *dbg_stats_csv;
  95. #endif
  96. };
  97. struct minstrel_priv {
  98. struct ieee80211_hw *hw;
  99. bool has_mrr;
  100. unsigned int cw_min;
  101. unsigned int cw_max;
  102. unsigned int max_retry;
  103. unsigned int segment_size;
  104. unsigned int update_interval;
  105. unsigned int lookaround_rate;
  106. unsigned int lookaround_rate_mrr;
  107. u8 cck_rates[4];
  108. #ifdef CONFIG_MAC80211_DEBUGFS
  109. /*
  110. * enable fixed rate processing per RC
  111. * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
  112. * - write -1 to enable RC processing again
  113. * - setting will be applied on next update
  114. */
  115. u32 fixed_rate_idx;
  116. struct dentry *dbg_fixed_rate;
  117. #endif
  118. };
  119. struct minstrel_debugfs_info {
  120. size_t len;
  121. char buf[];
  122. };
  123. extern const struct rate_control_ops mac80211_minstrel;
  124. void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
  125. void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
  126. /* Recalculate success probabilities and counters for a given rate using EWMA */
  127. void minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs);
  128. int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma);
  129. /* debugfs */
  130. int minstrel_stats_open(struct inode *inode, struct file *file);
  131. int minstrel_stats_csv_open(struct inode *inode, struct file *file);
  132. ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos);
  133. int minstrel_stats_release(struct inode *inode, struct file *file);
  134. #endif