rc80211_minstrel_ht_debugfs.c 8.8 KB

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