-
Notifications
You must be signed in to change notification settings - Fork 6
/
debugfs.c
273 lines (229 loc) · 6.18 KB
/
debugfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (C) 2013:
* Simon Wunderlich <[email protected]>
* Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "wifi_statistics.h"
static struct dentry *ws_debugfs;
static ssize_t read_debug_active(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ws_monif *monif = (struct ws_monif *)file->private_data;
char buf[32];
ssize_t len;
len = sprintf(buf, "%d\n", atomic_read(&monif->active));
return simple_read_from_buffer(user_buf, count, ppos, buf, len);
}
static ssize_t write_debug_active(struct file *file,
const char __user *user_buf, size_t count,
loff_t *ppos)
{
struct ws_monif *monif = (struct ws_monif *)file->private_data;
char buf[32];
ssize_t len;
unsigned long old, active;
len = min(count, sizeof(buf) - 1);
if (copy_from_user(buf, user_buf, len))
return -EFAULT;
buf[len] = '\0';
if (kstrtoul(buf, 0, &active))
return -EINVAL;
active = !!active;
rtnl_lock();
old = atomic_read(&monif->active);
if (old != active) {
if (active)
ws_monif_activate(monif);
else
ws_monif_deactivate(monif);
}
rtnl_unlock();
return count;
}
const struct file_operations active_fops = {
.owner = THIS_MODULE,
.open = simple_open,
.write = write_debug_active,
.read = read_debug_active,
.llseek = default_llseek,
};
int ws_sta_seq_read(struct seq_file *seq, void *offset)
{
struct ws_monif *monif = (struct ws_monif *)seq->private;
struct ws_hash *hash = &monif->hash;
struct hlist_head *head;
struct ws_sta *ws_sta;
bool first = true;
int i;
if (!atomic_read(&monif->active))
return -1;
ws_sta_seq_print_head(seq);
for (i = 0; i < WS_HASH_SIZE; i++) {
head = &hash->table[i];
rcu_read_lock();
hlist_for_each_entry_rcu(ws_sta, head, hash_entry) {
if (!first)
seq_puts(seq, ",");
ws_sta_seq_print(ws_sta, seq, offset);
first = false;
}
rcu_read_unlock();
}
ws_sta_seq_print_tail(seq);
return 0;
}
int ws_sta_seq_read_reset(struct seq_file *seq, void *offset)
{
struct ws_monif *monif = (struct ws_monif *)seq->private;
struct ws_hash *hash = &monif->hash;
struct hlist_head *head;
struct hlist_node *node;
spinlock_t *list_lock; /* protects write access to the hash lists */
struct ws_sta *ws_sta;
int i;
bool first = true;
if (!atomic_read(&monif->active))
return -1;
ws_sta_seq_print_head(seq);
for (i = 0; i < WS_HASH_SIZE; i++) {
head = &hash->table[i];
list_lock = &hash->list_locks[i];
/* TODO: can we write while doing spinlocks?! */
spin_lock_bh(list_lock);
hlist_for_each_entry_safe(ws_sta, node, head, hash_entry) {
if (!first)
seq_puts(seq, ",");
ws_sta_seq_print(ws_sta, seq, offset);
hlist_del_rcu(&ws_sta->hash_entry);
ws_sta_free_ref(ws_sta);
first = false;
}
spin_unlock_bh(list_lock);
}
ws_sta_seq_print_tail(seq);
return 0;
}
static int ws_sta_debug_open(struct inode *inode, struct file *file)
{
struct ws_monif *monif = (struct ws_monif *)inode->i_private;
if (!atomic_read(&monif->active))
return -1;
switch (monif->ws_mode) {
case MODE_READ:
return single_open(file, ws_sta_seq_read, monif);
default:
case MODE_RESET:
return single_open(file, ws_sta_seq_read_reset, monif);
}
}
const struct file_operations stats_fops = {
.owner = THIS_MODULE,
.open = ws_sta_debug_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static ssize_t read_file_mode(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ws_monif *monif = (struct ws_monif *)file->private_data;
char *mode;
ssize_t len;
switch (monif->ws_mode) {
case MODE_READ:
mode = "read\n";
break;
case MODE_RESET:
mode = "reset\n";
break;
default:
mode = "unknown\n";
break;
}
len = strlen(mode);
return simple_read_from_buffer(user_buf, count, ppos, mode, len);
}
static ssize_t write_file_mode(struct file *file, const char __user *user_buf,
size_t count, loff_t *ppos)
{
struct ws_monif *monif = (struct ws_monif *)file->private_data;
char buf[32];
ssize_t len;
len = min(count, sizeof(buf) - 1);
if (copy_from_user(buf, user_buf, len))
return -EFAULT;
buf[len] = '\0';
if (strncmp("read", buf, 4) == 0)
monif->ws_mode = MODE_READ;
else if (strncmp("reset", buf, 4) == 0)
monif->ws_mode = MODE_RESET;
else
return -EINVAL;
return count;
}
const struct file_operations mode_fops = {
.owner = THIS_MODULE,
.open = simple_open,
.write = write_file_mode,
.read = read_file_mode,
.llseek = default_llseek,
};
void ws_debugfs_monif_init(struct ws_monif *monif)
{
struct dentry *file;
monif->dir = debugfs_create_dir(monif->net_dev->name, ws_debugfs);
if (monif->dir == ERR_PTR(-ENODEV))
monif->dir = NULL;
if (!monif->dir)
goto err;
file = debugfs_create_file("stats",
S_IFREG | S_IRUGO, monif->dir, monif,
&stats_fops);
if (!file)
goto err;
file = debugfs_create_file("active",
S_IFREG | S_IRUGO | S_IWUGO, monif->dir,
monif, &active_fops);
if (!file)
goto err;
file = debugfs_create_file("mode",
S_IFREG | S_IRUGO | S_IWUGO, monif->dir,
monif, &mode_fops);
if (!file)
goto err;
return;
err:
debugfs_remove_recursive(monif->dir);
}
void ws_debugfs_monif_clean(struct ws_monif *monif)
{
debugfs_remove_recursive(monif->dir);
monif->dir = NULL;
}
void ws_debugfs_init(void)
{
ws_debugfs = debugfs_create_dir("wifi_statistics", NULL);
if (ws_debugfs == ERR_PTR(-ENODEV))
ws_debugfs = NULL;
if (!ws_debugfs)
debugfs_remove_recursive(ws_debugfs);
}
void ws_debugfs_destroy(void)
{
debugfs_remove_recursive(ws_debugfs);
ws_debugfs = NULL;
}