rc80211_minstrel_ht_debugfs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 Felix Fietkau <[email protected]>
  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. .llseek = no_llseek,
  161. };
  162. static char *
  163. minstrel_ht_stats_csv_dump(struct minstrel_ht_sta *mi, int i, char *p)
  164. {
  165. const struct mcs_group *mg;
  166. unsigned int j, tp_max, tp_avg, eprob, tx_time;
  167. char htmode = '2';
  168. char gimode = 'L';
  169. u32 gflags;
  170. if (!mi->supported[i])
  171. return p;
  172. mg = &minstrel_mcs_groups[i];
  173. gflags = mg->flags;
  174. if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  175. htmode = '4';
  176. else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  177. htmode = '8';
  178. if (gflags & IEEE80211_TX_RC_SHORT_GI)
  179. gimode = 'S';
  180. for (j = 0; j < MCS_GROUP_RATES; j++) {
  181. struct minstrel_rate_stats *mrs = &mi->groups[i].rates[j];
  182. int idx = MI_RATE(i, j);
  183. unsigned int duration;
  184. if (!(mi->supported[i] & BIT(j)))
  185. continue;
  186. if (gflags & IEEE80211_TX_RC_MCS) {
  187. p += sprintf(p, "HT%c0,", htmode);
  188. p += sprintf(p, "%cGI,", gimode);
  189. p += sprintf(p, "%d,", mg->streams);
  190. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  191. p += sprintf(p, "VHT%c0,", htmode);
  192. p += sprintf(p, "%cGI,", gimode);
  193. p += sprintf(p, "%d,", mg->streams);
  194. } else if (i == MINSTREL_OFDM_GROUP) {
  195. p += sprintf(p, "OFDM,,1,");
  196. } else {
  197. p += sprintf(p, "CCK,");
  198. p += sprintf(p, "%cP,", j < 4 ? 'L' : 'S');
  199. p += sprintf(p, "1,");
  200. }
  201. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[0]) ? "A" : ""));
  202. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[1]) ? "B" : ""));
  203. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[2]) ? "C" : ""));
  204. p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[3]) ? "D" : ""));
  205. p += sprintf(p, "%s" ,((idx == mi->max_prob_rate) ? "P" : ""));
  206. p += sprintf(p, "%s", (minstrel_ht_is_sample_rate(mi, idx) ? "S" : ""));
  207. if (gflags & IEEE80211_TX_RC_MCS) {
  208. p += sprintf(p, ",MCS%-2u,", (mg->streams - 1) * 8 + j);
  209. } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
  210. p += sprintf(p, ",MCS%-1u/%1u,", j, mg->streams);
  211. } else {
  212. int r;
  213. if (i == MINSTREL_OFDM_GROUP)
  214. r = minstrel_ofdm_bitrates[j % 8];
  215. else
  216. r = minstrel_cck_bitrates[j % 4];
  217. p += sprintf(p, ",%2u.%1uM,", r / 10, r % 10);
  218. }
  219. p += sprintf(p, "%u,", idx);
  220. duration = mg->duration[j];
  221. duration <<= mg->shift;
  222. tx_time = DIV_ROUND_CLOSEST(duration, 1000);
  223. p += sprintf(p, "%u,", tx_time);
  224. tp_max = minstrel_ht_get_tp_avg(mi, i, j, MINSTREL_FRAC(100, 100));
  225. tp_avg = minstrel_ht_get_tp_avg(mi, i, j, mrs->prob_avg);
  226. eprob = MINSTREL_TRUNC(mrs->prob_avg * 1000);
  227. p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u,%u,"
  228. "%u,%llu,%llu,",
  229. tp_max / 10, tp_max % 10,
  230. tp_avg / 10, tp_avg % 10,
  231. eprob / 10, eprob % 10,
  232. mrs->retry_count,
  233. mrs->last_success,
  234. mrs->last_attempts,
  235. (unsigned long long)mrs->succ_hist,
  236. (unsigned long long)mrs->att_hist);
  237. p += sprintf(p, "%d,%d,%d.%d\n",
  238. max(0, (int) mi->total_packets -
  239. (int) mi->sample_packets),
  240. mi->sample_packets,
  241. MINSTREL_TRUNC(mi->avg_ampdu_len),
  242. MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
  243. }
  244. return p;
  245. }
  246. static int
  247. minstrel_ht_stats_csv_open(struct inode *inode, struct file *file)
  248. {
  249. struct minstrel_ht_sta *mi = inode->i_private;
  250. struct minstrel_debugfs_info *ms;
  251. unsigned int i;
  252. char *p;
  253. ms = kmalloc(32768, GFP_KERNEL);
  254. if (!ms)
  255. return -ENOMEM;
  256. file->private_data = ms;
  257. p = ms->buf;
  258. p = minstrel_ht_stats_csv_dump(mi, MINSTREL_CCK_GROUP, p);
  259. for (i = 0; i < MINSTREL_CCK_GROUP; i++)
  260. p = minstrel_ht_stats_csv_dump(mi, i, p);
  261. for (i++; i < ARRAY_SIZE(mi->groups); i++)
  262. p = minstrel_ht_stats_csv_dump(mi, i, p);
  263. ms->len = p - ms->buf;
  264. WARN_ON(ms->len + sizeof(*ms) > 32768);
  265. return nonseekable_open(inode, file);
  266. }
  267. static const struct file_operations minstrel_ht_stat_csv_fops = {
  268. .owner = THIS_MODULE,
  269. .open = minstrel_ht_stats_csv_open,
  270. .read = minstrel_stats_read,
  271. .release = minstrel_stats_release,
  272. .llseek = no_llseek,
  273. };
  274. void
  275. minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
  276. {
  277. debugfs_create_file("rc_stats", 0444, dir, priv_sta,
  278. &minstrel_ht_stat_fops);
  279. debugfs_create_file("rc_stats_csv", 0444, dir, priv_sta,
  280. &minstrel_ht_stat_csv_fops);
  281. }