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  * Based on minstrel.c:
9  *   Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
10  *   Sponsored by Indranet Technologies Ltd
11  *
12  * Based on sample.c:
13  *   Copyright (c) 2005 John Bicket
14  *   All rights reserved.
15  *
16  *   Redistribution and use in source and binary forms, with or without
17  *   modification, are permitted provided that the following conditions
18  *   are met:
19  *   1. Redistributions of source code must retain the above copyright
20  *      notice, this list of conditions and the following disclaimer,
21  *      without modification.
22  *   2. Redistributions in binary form must reproduce at minimum a disclaimer
23  *      similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
24  *      redistribution must be conditioned upon including a substantially
25  *      similar Disclaimer requirement for further binary redistribution.
26  *   3. Neither the names of the above-listed copyright holders nor the names
27  *      of any contributors may be used to endorse or promote products derived
28  *      from this software without specific prior written permission.
29  *
30  *   Alternatively, this software may be distributed under the terms of the
31  *   GNU General Public License ("GPL") version 2 as published by the Free
32  *   Software Foundation.
33  *
34  *   NO WARRANTY
35  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  *   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  *   LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
38  *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39  *   THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
40  *   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41  *   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
43  *   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44  *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
45  *   THE POSSIBILITY OF SUCH DAMAGES.
46  */
47 #include <linux/netdevice.h>
48 #include <linux/types.h>
49 #include <linux/skbuff.h>
50 #include <linux/debugfs.h>
51 #include <linux/ieee80211.h>
52 #include <linux/slab.h>
53 #include <linux/export.h>
54 #include <net/mac80211.h>
55 #include "rc80211_minstrel.h"
56 
57 ssize_t
minstrel_stats_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)58 minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
59 {
60 	struct minstrel_debugfs_info *ms;
61 
62 	ms = file->private_data;
63 	return simple_read_from_buffer(buf, len, ppos, ms->buf, ms->len);
64 }
65 
66 int
minstrel_stats_release(struct inode * inode,struct file * file)67 minstrel_stats_release(struct inode *inode, struct file *file)
68 {
69 	kfree(file->private_data);
70 	return 0;
71 }
72 
73 int
minstrel_stats_open(struct inode * inode,struct file * file)74 minstrel_stats_open(struct inode *inode, struct file *file)
75 {
76 	struct minstrel_sta_info *mi = inode->i_private;
77 	struct minstrel_debugfs_info *ms;
78 	unsigned int i, tp_max, tp_avg, eprob;
79 	char *p;
80 
81 	ms = kmalloc(2048, GFP_KERNEL);
82 	if (!ms)
83 		return -ENOMEM;
84 
85 	file->private_data = ms;
86 	p = ms->buf;
87 	p += sprintf(p, "\n");
88 	p += sprintf(p,
89 		     "best   __________rate_________    ________statistics________    ____last_____    ______sum-of________\n");
90 	p += sprintf(p,
91 		     "rate  [name idx airtime max_tp]  [avg(tp) avg(prob) sd(prob)]  [retry|suc|att]  [#success | #attempts]\n");
92 
93 	for (i = 0; i < mi->n_rates; i++) {
94 		struct minstrel_rate *mr = &mi->r[i];
95 		struct minstrel_rate_stats *mrs = &mi->r[i].stats;
96 		unsigned int prob_ewmsd;
97 
98 		*(p++) = (i == mi->max_tp_rate[0]) ? 'A' : ' ';
99 		*(p++) = (i == mi->max_tp_rate[1]) ? 'B' : ' ';
100 		*(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' ';
101 		*(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' ';
102 		*(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
103 
104 		p += sprintf(p, " %3u%s ", mr->bitrate / 2,
105 				(mr->bitrate & 1 ? ".5" : "  "));
106 		p += sprintf(p, "%3u  ", i);
107 		p += sprintf(p, "%6u ", mr->perfect_tx_time);
108 
109 		tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
110 		tp_avg = minstrel_get_tp_avg(mr, mrs->prob_ewma);
111 		eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
112 		prob_ewmsd = minstrel_get_ewmsd10(mrs);
113 
114 		p += sprintf(p, "%4u.%1u    %4u.%1u     %3u.%1u    %3u.%1u"
115 				"     %3u   %3u %-3u   "
116 				"%9llu   %-9llu\n",
117 				tp_max / 10, tp_max % 10,
118 				tp_avg / 10, tp_avg % 10,
119 				eprob / 10, eprob % 10,
120 				prob_ewmsd / 10, prob_ewmsd % 10,
121 				mrs->retry_count,
122 				mrs->last_success,
123 				mrs->last_attempts,
124 				(unsigned long long)mrs->succ_hist,
125 				(unsigned long long)mrs->att_hist);
126 	}
127 	p += sprintf(p, "\nTotal packet count::    ideal %d      "
128 			"lookaround %d\n\n",
129 			mi->total_packets - mi->sample_packets,
130 			mi->sample_packets);
131 	ms->len = p - ms->buf;
132 
133 	WARN_ON(ms->len + sizeof(*ms) > 2048);
134 
135 	return 0;
136 }
137 
138 static const struct file_operations minstrel_stat_fops = {
139 	.owner = THIS_MODULE,
140 	.open = minstrel_stats_open,
141 	.read = minstrel_stats_read,
142 	.release = minstrel_stats_release,
143 	.llseek = default_llseek,
144 };
145 
146 int
minstrel_stats_csv_open(struct inode * inode,struct file * file)147 minstrel_stats_csv_open(struct inode *inode, struct file *file)
148 {
149 	struct minstrel_sta_info *mi = inode->i_private;
150 	struct minstrel_debugfs_info *ms;
151 	unsigned int i, tp_max, tp_avg, eprob;
152 	char *p;
153 
154 	ms = kmalloc(2048, GFP_KERNEL);
155 	if (!ms)
156 		return -ENOMEM;
157 
158 	file->private_data = ms;
159 	p = ms->buf;
160 
161 	for (i = 0; i < mi->n_rates; i++) {
162 		struct minstrel_rate *mr = &mi->r[i];
163 		struct minstrel_rate_stats *mrs = &mi->r[i].stats;
164 		unsigned int prob_ewmsd;
165 
166 		p += sprintf(p, "%s" ,((i == mi->max_tp_rate[0]) ? "A" : ""));
167 		p += sprintf(p, "%s" ,((i == mi->max_tp_rate[1]) ? "B" : ""));
168 		p += sprintf(p, "%s" ,((i == mi->max_tp_rate[2]) ? "C" : ""));
169 		p += sprintf(p, "%s" ,((i == mi->max_tp_rate[3]) ? "D" : ""));
170 		p += sprintf(p, "%s" ,((i == mi->max_prob_rate) ? "P" : ""));
171 
172 		p += sprintf(p, ",%u%s", mr->bitrate / 2,
173 				(mr->bitrate & 1 ? ".5," : ","));
174 		p += sprintf(p, "%u,", i);
175 		p += sprintf(p, "%u,",mr->perfect_tx_time);
176 
177 		tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
178 		tp_avg = minstrel_get_tp_avg(mr, mrs->prob_ewma);
179 		eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
180 		prob_ewmsd = minstrel_get_ewmsd10(mrs);
181 
182 		p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u.%u,%u,%u,%u,"
183 				"%llu,%llu,%d,%d\n",
184 				tp_max / 10, tp_max % 10,
185 				tp_avg / 10, tp_avg % 10,
186 				eprob / 10, eprob % 10,
187 				prob_ewmsd / 10, prob_ewmsd % 10,
188 				mrs->retry_count,
189 				mrs->last_success,
190 				mrs->last_attempts,
191 				(unsigned long long)mrs->succ_hist,
192 				(unsigned long long)mrs->att_hist,
193 				mi->total_packets - mi->sample_packets,
194 				mi->sample_packets);
195 
196 	}
197 	ms->len = p - ms->buf;
198 
199 	WARN_ON(ms->len + sizeof(*ms) > 2048);
200 
201 	return 0;
202 }
203 
204 static const struct file_operations minstrel_stat_csv_fops = {
205 	.owner = THIS_MODULE,
206 	.open = minstrel_stats_csv_open,
207 	.read = minstrel_stats_read,
208 	.release = minstrel_stats_release,
209 	.llseek = default_llseek,
210 };
211 
212 void
minstrel_add_sta_debugfs(void * priv,void * priv_sta,struct dentry * dir)213 minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
214 {
215 	struct minstrel_sta_info *mi = priv_sta;
216 
217 	mi->dbg_stats = debugfs_create_file("rc_stats", 0444, dir, mi,
218 					    &minstrel_stat_fops);
219 
220 	mi->dbg_stats_csv = debugfs_create_file("rc_stats_csv", 0444, dir, mi,
221 						&minstrel_stat_csv_fops);
222 }
223 
224 void
minstrel_remove_sta_debugfs(void * priv,void * priv_sta)225 minstrel_remove_sta_debugfs(void *priv, void *priv_sta)
226 {
227 	struct minstrel_sta_info *mi = priv_sta;
228 
229 	debugfs_remove(mi->dbg_stats);
230 
231 	debugfs_remove(mi->dbg_stats_csv);
232 }
233