rc80211_minstrel_ht_debugfs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
  4. */
  5. #include <linux/netdevice.h>
  6. #include <linux/types.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/ieee80211.h>
  10. #include <linux/export.h>
  11. #include <net/mac80211.h>
  12. #include "rc80211_minstrel_ht.h"
  13. struct minstrel_debugfs_info {
  14. size_t len;
  15. char buf[];
  16. };
  17. static ssize_t
  18. minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
  19. {
  20. struct minstrel_debugfs_info *ms;
  21. ms = file->private_data;
  22. return simple_read_from_buffer(buf, len, ppos, ms->buf, ms->len);
  23. }
  24. static int
  25. minstrel_stats_release(struct inode *inode, struct file *file)
  26. {
  27. kfree(file->private_data);
  28. return 0;
  29. }
  30. static bool
  31. minstrel_ht_is_sample_rate(struct minstrel_ht_sta *mi, int idx)
  32. {
  33. int type, i;
  34. for (type = 0; type < ARRAY_SIZE(mi->sample); type++)
  35. for (i = 0; i < MINSTREL_SAMPLE_RATES; i++)
  36. if (mi->sample[type].cur_sample_rates[i] == idx)
  37. return true;
  38. return false;
  39. }
  40. static char *
  41. minstrel_ht_stats_dump(struct minstrel_ht_sta *mi, int i, char *p)
  42. {
  43. const struct mcs_group *mg;
  44. unsigned int j, tp_max, tp_avg, eprob, tx_time;
  45. char htmode = '2';
  46. char gimode = 'L';
  47. u32 gflags;
  48. if (!mi->supported[i])
  49. return p;
  50. mg = &minstrel_mcs_groups[i];
  51. gflags = mg->flags;
  52. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  53. htmode = '4';
  54. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  55. htmode = '8';
  56. if (gflags & IEEE80211_TX_RC_SHORT_GI)
  57. gimode = 'S';
  58. for (j = 0; j < MCS_GROUP_RATES; j++) {
  59. struct minstrel_rate_stats *mrs = &mi->groups[i].rates[j];
  60. int idx = MI_RATE(i, j);
  61. unsigned int duration;
  62. if (!(mi->supported[i] & BIT(j)))
  63. continue;
  64. if (gflags & IEEE80211_TX_RC_MCS) {
  65. p += sprintf(p, "HT%c0 ", htmode);
  66. p += sprintf(p, "%cGI ", gimode);
  67. p += sprintf(p, "%d ", mg->streams);
  68. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  69. p += sprintf(p, "VHT%c0 ", htmode);
  70. p += sprintf(p, "%cGI ", gimode);
  71. p += sprintf(p, "%d ", mg->streams);
  72. } else if (i == MINSTREL_OFDM_GROUP) {
  73. p += sprintf(p, "OFDM ");
  74. p += sprintf(p, "1 ");
  75. } else {
  76. p += sprintf(p, "CCK ");
  77. p += sprintf(p, "%cP ", j < 4 ? 'L' : 'S');
  78. p += sprintf(p, "1 ");
  79. }
  80. *(p++) = (idx == mi->max_tp_rate[0]) ? 'A' : ' ';
  81. *(p++) = (idx == mi->max_tp_rate[1]) ? 'B' : ' ';
  82. *(p++) = (idx == mi->max_tp_rate[2]) ? 'C' : ' ';
  83. *(p++) = (idx == mi->max_tp_rate[3]) ? 'D' : ' ';
  84. *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' ';
  85. *(p++) = minstrel_ht_is_sample_rate(mi, idx) ? 'S' : ' ';
  86. if (gflags & IEEE80211_TX_RC_MCS) {
  87. p += sprintf(p, " MCS%-2u", (mg->streams - 1) * 8 + j);
  88. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  89. p += sprintf(p, " MCS%-1u/%1u", j, mg->streams);
  90. } else {
  91. int r;
  92. if (i == MINSTREL_OFDM_GROUP)
  93. r = minstrel_ofdm_bitrates[j % 8];
  94. else
  95. r = minstrel_cck_bitrates[j % 4];
  96. p += sprintf(p, " %2u.%1uM", r / 10, r % 10);
  97. }
  98. p += sprintf(p, " %3u ", idx);
  99. /* tx_time[rate(i)] in usec */
  100. duration = mg->duration[j];
  101. duration <<= mg->shift;
  102. tx_time = DIV_ROUND_CLOSEST(duration, 1000);
  103. p += sprintf(p, "%6u ", tx_time);
  104. tp_max = minstrel_ht_get_tp_avg(mi, i, j, MINSTREL_FRAC(100, 100));
  105. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, mrs->prob_avg);
  106. eprob = MINSTREL_TRUNC(mrs->prob_avg * 1000);
  107. p += sprintf(p, "%4u.%1u %4u.%1u %3u.%1u"
  108. " %3u %3u %-3u "
  109. "%9llu %-9llu\n",
  110. tp_max / 10, tp_max % 10,
  111. tp_avg / 10, tp_avg % 10,
  112. eprob / 10, eprob % 10,
  113. mrs->retry_count,
  114. mrs->last_success,
  115. mrs->last_attempts,
  116. (unsigned long long)mrs->succ_hist,
  117. (unsigned long long)mrs->att_hist);
  118. }
  119. return p;
  120. }
  121. static int
  122. minstrel_ht_stats_open(struct inode *inode, struct file *file)
  123. {
  124. struct minstrel_ht_sta *mi = inode->i_private;
  125. struct minstrel_debugfs_info *ms;
  126. unsigned int i;
  127. char *p;
  128. ms = kmalloc(32768, GFP_KERNEL);
  129. if (!ms)
  130. return -ENOMEM;
  131. file->private_data = ms;
  132. p = ms->buf;
  133. p += sprintf(p, "\n");
  134. p += sprintf(p,
  135. " best ____________rate__________ ____statistics___ _____last____ ______sum-of________\n");
  136. p += sprintf(p,
  137. "mode guard # rate [name idx airtime max_tp] [avg(tp) avg(prob)] [retry|suc|att] [#success | #attempts]\n");
  138. p = minstrel_ht_stats_dump(mi, MINSTREL_CCK_GROUP, p);
  139. for (i = 0; i < MINSTREL_CCK_GROUP; i++)
  140. p = minstrel_ht_stats_dump(mi, i, p);
  141. for (i++; i < ARRAY_SIZE(mi->groups); i++)
  142. p = minstrel_ht_stats_dump(mi, i, p);
  143. p += sprintf(p, "\nTotal packet count:: ideal %d "
  144. "lookaround %d\n",
  145. max(0, (int) mi->total_packets - (int) mi->sample_packets),
  146. mi->sample_packets);
  147. if (mi->avg_ampdu_len)
  148. p += sprintf(p, "Average # of aggregated frames per A-MPDU: %d.%d\n",
  149. MINSTREL_TRUNC(mi->avg_ampdu_len),
  150. MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
  151. ms->len = p - ms->buf;
  152. WARN_ON(ms->len + sizeof(*ms) > 32768);
  153. return nonseekable_open(inode, file);
  154. }
  155. static const struct file_operations minstrel_ht_stat_fops = {
  156. .owner = THIS_MODULE,
  157. .open = minstrel_ht_stats_open,
  158. .read = minstrel_stats_read,
  159. .release = minstrel_stats_release,
  160. };
  161. static char *
  162. minstrel_ht_stats_csv_dump(struct minstrel_ht_sta *mi, int i, char *p)
  163. {
  164. const struct mcs_group *mg;
  165. unsigned int j, tp_max, tp_avg, eprob, tx_time;
  166. char htmode = '2';
  167. char gimode = 'L';
  168. u32 gflags;
  169. if (!mi->supported[i])
  170. return p;
  171. mg = &minstrel_mcs_groups[i];
  172. gflags = mg->flags;
  173. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  174. htmode = '4';
  175. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  176. htmode = '8';
  177. if (gflags & IEEE80211_TX_RC_SHORT_GI)
  178. gimode = 'S';
  179. for (j = 0; j < MCS_GROUP_RATES; j++) {
  180. struct minstrel_rate_stats *mrs = &mi->groups[i].rates[j];
  181. int idx = MI_RATE(i, j);
  182. unsigned int duration;
  183. if (!(mi->supported[i] & BIT(j)))
  184. continue;
  185. if (gflags & IEEE80211_TX_RC_MCS) {
  186. p += sprintf(p, "HT%c0,", htmode);
  187. p += sprintf(p, "%cGI,", gimode);
  188. p += sprintf(p, "%d,", mg->streams);
  189. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  190. p += sprintf(p, "VHT%c0,", htmode);
  191. p += sprintf(p, "%cGI,", gimode);
  192. p += sprintf(p, "%d,", mg->streams);
  193. } else if (i == MINSTREL_OFDM_GROUP) {
  194. p += sprintf(p, "OFDM,,1,");
  195. } else {
  196. p += sprintf(p, "CCK,");
  197. p += sprintf(p, "%cP,", j < 4 ? 'L' : 'S');
  198. p += sprintf(p, "1,");
  199. }
  200. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[0]) ? "A" : ""));
  201. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[1]) ? "B" : ""));
  202. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[2]) ? "C" : ""));
  203. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[3]) ? "D" : ""));
  204. p += sprintf(p, "%s" ,((idx == mi->max_prob_rate) ? "P" : ""));
  205. p += sprintf(p, "%s", (minstrel_ht_is_sample_rate(mi, idx) ? "S" : ""));
  206. if (gflags & IEEE80211_TX_RC_MCS) {
  207. p += sprintf(p, ",MCS%-2u,", (mg->streams - 1) * 8 + j);
  208. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  209. p += sprintf(p, ",MCS%-1u/%1u,", j, mg->streams);
  210. } else {
  211. int r;
  212. if (i == MINSTREL_OFDM_GROUP)
  213. r = minstrel_ofdm_bitrates[j % 8];
  214. else
  215. r = minstrel_cck_bitrates[j % 4];
  216. p += sprintf(p, ",%2u.%1uM,", r / 10, r % 10);
  217. }
  218. p += sprintf(p, "%u,", idx);
  219. duration = mg->duration[j];
  220. duration <<= mg->shift;
  221. tx_time = DIV_ROUND_CLOSEST(duration, 1000);
  222. p += sprintf(p, "%u,", tx_time);
  223. tp_max = minstrel_ht_get_tp_avg(mi, i, j, MINSTREL_FRAC(100, 100));
  224. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, mrs->prob_avg);
  225. eprob = MINSTREL_TRUNC(mrs->prob_avg * 1000);
  226. p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u,%u,"
  227. "%u,%llu,%llu,",
  228. tp_max / 10, tp_max % 10,
  229. tp_avg / 10, tp_avg % 10,
  230. eprob / 10, eprob % 10,
  231. mrs->retry_count,
  232. mrs->last_success,
  233. mrs->last_attempts,
  234. (unsigned long long)mrs->succ_hist,
  235. (unsigned long long)mrs->att_hist);
  236. p += sprintf(p, "%d,%d,%d.%d\n",
  237. max(0, (int) mi->total_packets -
  238. (int) mi->sample_packets),
  239. mi->sample_packets,
  240. MINSTREL_TRUNC(mi->avg_ampdu_len),
  241. MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
  242. }
  243. return p;
  244. }
  245. static int
  246. minstrel_ht_stats_csv_open(struct inode *inode, struct file *file)
  247. {
  248. struct minstrel_ht_sta *mi = inode->i_private;
  249. struct minstrel_debugfs_info *ms;
  250. unsigned int i;
  251. char *p;
  252. ms = kmalloc(32768, GFP_KERNEL);
  253. if (!ms)
  254. return -ENOMEM;
  255. file->private_data = ms;
  256. p = ms->buf;
  257. p = minstrel_ht_stats_csv_dump(mi, MINSTREL_CCK_GROUP, p);
  258. for (i = 0; i < MINSTREL_CCK_GROUP; i++)
  259. p = minstrel_ht_stats_csv_dump(mi, i, p);
  260. for (i++; i < ARRAY_SIZE(mi->groups); i++)
  261. p = minstrel_ht_stats_csv_dump(mi, i, p);
  262. ms->len = p - ms->buf;
  263. WARN_ON(ms->len + sizeof(*ms) > 32768);
  264. return nonseekable_open(inode, file);
  265. }
  266. static const struct file_operations minstrel_ht_stat_csv_fops = {
  267. .owner = THIS_MODULE,
  268. .open = minstrel_ht_stats_csv_open,
  269. .read = minstrel_stats_read,
  270. .release = minstrel_stats_release,
  271. };
  272. void
  273. minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
  274. {
  275. debugfs_create_file("rc_stats", 0444, dir, priv_sta,
  276. &minstrel_ht_stat_fops);
  277. debugfs_create_file("rc_stats_csv", 0444, dir, priv_sta,
  278. &minstrel_ht_stat_csv_fops);
  279. }