2 * proc.c - procfs support for Protocol family CAN core module
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/module.h>
46 #include <linux/proc_fs.h>
47 #include <linux/list.h>
48 #include <linux/rcupdate.h>
49 #include <linux/can/core.h>
53 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
57 * proc filenames for the PF_CAN core
60 #define CAN_PROC_VERSION "version"
61 #define CAN_PROC_STATS "stats"
62 #define CAN_PROC_RESET_STATS "reset_stats"
63 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
64 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
65 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
66 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
67 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
68 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
70 static struct proc_dir_entry *can_dir = NULL;
71 static struct proc_dir_entry *pde_version = NULL;
72 static struct proc_dir_entry *pde_stats = NULL;
73 static struct proc_dir_entry *pde_reset_stats = NULL;
74 static struct proc_dir_entry *pde_rcvlist_all = NULL;
75 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
76 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
77 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
78 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
79 static struct proc_dir_entry *pde_rcvlist_err = NULL;
81 static int user_reset = 0;
83 static const char *rx_list_name[] = {
92 * af_can statistics stuff
95 static void can_init_stats(void)
98 * This memset function is called from a timer context (when
99 * stattimer is active which is the default) OR in a process
100 * context (reading the proc_fs when stattimer is disabled).
102 memset(&stats, 0, sizeof(stats));
103 stats.jiffies_init = jiffies;
105 pstats.stats_reset++;
113 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
116 unsigned long ret = 0;
118 if (oldjif == newjif)
121 /* see can_rcv() - this should NEVER happen! */
122 if (count > (ULONG_MAX / HZ)) {
123 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
128 ret = (count * HZ) / (newjif - oldjif);
133 void can_stat_update(unsigned long data)
135 unsigned long j = jiffies; /* snapshot */
137 /* restart counting in timer context on user request */
141 /* restart counting on jiffies overflow */
142 if (j < stats.jiffies_init)
145 /* stats.rx_frames is the definitively max. statistic value */
147 /* prevent overflow in calc_rate() */
148 if (stats.rx_frames > (ULONG_MAX / HZ))
151 /* matches overflow - very improbable */
152 if (stats.matches > (ULONG_MAX / 100))
155 /* calc total values */
157 stats.total_rx_match_ratio = (stats.matches * 100) /
160 stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
162 stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
165 /* calc current values */
166 if (stats.rx_frames_delta)
167 stats.current_rx_match_ratio =
168 (stats.matches_delta * 100) / stats.rx_frames_delta;
170 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
171 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
173 /* check / update maximum values */
174 if (stats.max_tx_rate < stats.current_tx_rate)
175 stats.max_tx_rate = stats.current_tx_rate;
177 if (stats.max_rx_rate < stats.current_rx_rate)
178 stats.max_rx_rate = stats.current_rx_rate;
180 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
181 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
183 /* clear values for 'current rate' calculation */
184 stats.tx_frames_delta = 0;
185 stats.rx_frames_delta = 0;
186 stats.matches_delta = 0;
188 /* restart timer (one second) */
189 stattimer.expires = jiffies + HZ;
190 add_timer(&stattimer);
194 * proc read functions
196 * From known use-cases we expect about 10 entries in a receive list to be
197 * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
201 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
202 struct net_device *dev)
205 struct hlist_node *n;
208 hlist_for_each_entry_rcu(r, n, rx_list, list) {
209 char *fmt = (r->can_id & CAN_EFF_FLAG)?
210 " %-5s %08X %08x %08x %08x %8ld %s\n" :
211 " %-5s %03X %08x %08x %08x %8ld %s\n";
213 len += snprintf(page + len, PAGE_SIZE - len, fmt,
214 DNAME(dev), r->can_id, r->mask,
215 (unsigned int)r->func, (unsigned int)r->data,
216 r->matches, r->ident);
218 /* does a typical line fit into the current buffer? */
220 /* 100 Bytes before end of buffer */
221 if (len > PAGE_SIZE - 100) {
222 /* mark output cut off */
223 len += snprintf(page + len, PAGE_SIZE - len,
233 static int can_print_recv_banner(char *page, int len)
236 * can1. 00000000 00000000 00000000
239 len += snprintf(page + len, PAGE_SIZE - len,
240 " device can_id can_mask function"
241 " userdata matches ident\n");
246 static int can_proc_read_stats(char *page, char **start, off_t off,
247 int count, int *eof, void *data)
251 len += snprintf(page + len, PAGE_SIZE - len, "\n");
252 len += snprintf(page + len, PAGE_SIZE - len,
253 " %8ld transmitted frames (TXF)\n", stats.tx_frames);
254 len += snprintf(page + len, PAGE_SIZE - len,
255 " %8ld received frames (RXF)\n", stats.rx_frames);
256 len += snprintf(page + len, PAGE_SIZE - len,
257 " %8ld matched frames (RXMF)\n", stats.matches);
259 len += snprintf(page + len, PAGE_SIZE - len, "\n");
261 if (stattimer.function == can_stat_update) {
262 len += snprintf(page + len, PAGE_SIZE - len,
263 " %8ld %% total match ratio (RXMR)\n",
264 stats.total_rx_match_ratio);
266 len += snprintf(page + len, PAGE_SIZE - len,
267 " %8ld frames/s total tx rate (TXR)\n",
268 stats.total_tx_rate);
269 len += snprintf(page + len, PAGE_SIZE - len,
270 " %8ld frames/s total rx rate (RXR)\n",
271 stats.total_rx_rate);
273 len += snprintf(page + len, PAGE_SIZE - len, "\n");
275 len += snprintf(page + len, PAGE_SIZE - len,
276 " %8ld %% current match ratio (CRXMR)\n",
277 stats.current_rx_match_ratio);
279 len += snprintf(page + len, PAGE_SIZE - len,
280 " %8ld frames/s current tx rate (CTXR)\n",
281 stats.current_tx_rate);
282 len += snprintf(page + len, PAGE_SIZE - len,
283 " %8ld frames/s current rx rate (CRXR)\n",
284 stats.current_rx_rate);
286 len += snprintf(page + len, PAGE_SIZE - len, "\n");
288 len += snprintf(page + len, PAGE_SIZE - len,
289 " %8ld %% max match ratio (MRXMR)\n",
290 stats.max_rx_match_ratio);
292 len += snprintf(page + len, PAGE_SIZE - len,
293 " %8ld frames/s max tx rate (MTXR)\n",
295 len += snprintf(page + len, PAGE_SIZE - len,
296 " %8ld frames/s max rx rate (MRXR)\n",
299 len += snprintf(page + len, PAGE_SIZE - len, "\n");
302 len += snprintf(page + len, PAGE_SIZE - len,
303 " %8ld current receive list entries (CRCV)\n",
305 len += snprintf(page + len, PAGE_SIZE - len,
306 " %8ld maximum receive list entries (MRCV)\n",
307 pstats.rcv_entries_max);
309 if (pstats.stats_reset)
310 len += snprintf(page + len, PAGE_SIZE - len,
311 "\n %8ld statistic resets (STR)\n",
314 if (pstats.user_reset)
315 len += snprintf(page + len, PAGE_SIZE - len,
316 " %8ld user statistic resets (USTR)\n",
319 len += snprintf(page + len, PAGE_SIZE - len, "\n");
325 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
326 int count, int *eof, void *data)
332 if (stattimer.function == can_stat_update) {
333 len += snprintf(page + len, PAGE_SIZE - len,
334 "Scheduled statistic reset #%ld.\n",
335 pstats.stats_reset + 1);
338 if (stats.jiffies_init != jiffies)
341 len += snprintf(page + len, PAGE_SIZE - len,
342 "Performed statistic reset #%ld.\n",
350 static int can_proc_read_version(char *page, char **start, off_t off,
351 int count, int *eof, void *data)
355 len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
361 static int can_proc_read_rcvlist(char *page, char **start, off_t off,
362 int count, int *eof, void *data, int idx)
365 struct dev_rcv_lists *d;
366 struct hlist_node *n;
368 len += snprintf(page + len, PAGE_SIZE - len,
369 "\nreceive list '%s':\n", rx_list_name[idx]);
371 /* find receive list for this device */
373 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
375 if (!hlist_empty(&d->rx[idx])) {
376 len = can_print_recv_banner(page, len);
377 len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
379 len += snprintf(page + len, PAGE_SIZE - len,
380 " (%s: no entry)\n", DNAME(d->dev));
382 /* exit on end of buffer? */
383 if (len > PAGE_SIZE - 100)
388 len += snprintf(page + len, PAGE_SIZE - len, "\n");
394 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
395 int count, int *eof, void *data)
397 return can_proc_read_rcvlist(page, start, off, count, eof, data, RX_ALL);
400 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
401 int count, int *eof, void *data)
403 return can_proc_read_rcvlist(page, start, off, count, eof, data, RX_FIL);
406 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
407 int count, int *eof, void *data)
409 return can_proc_read_rcvlist(page, start, off, count, eof, data, RX_INV);
412 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
413 int count, int *eof, void *data)
415 return can_proc_read_rcvlist(page, start, off, count, eof, data, RX_EFF);
418 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
419 int count, int *eof, void *data)
421 return can_proc_read_rcvlist(page, start, off, count, eof, data, RX_ERR);
424 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
425 int count, int *eof, void *data)
428 struct dev_rcv_lists *d;
429 struct hlist_node *n;
432 len += snprintf(page + len, PAGE_SIZE - len,
433 "\nreceive list 'rx_sff':\n");
435 /* find receive list for this device */
437 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
438 int i, all_empty = 1;
439 /* check wether at least one list is non-empty */
440 for (i = 0; i < 0x800; i++)
441 if (!hlist_empty(&d->rx_sff[i])) {
447 len = can_print_recv_banner(page, len);
448 for (i = 0; i < 0x800; i++) {
449 if (!hlist_empty(&d->rx_sff[i]) &&
450 len < PAGE_SIZE - 100)
451 len = can_print_rcvlist(page, len,
456 len += snprintf(page + len, PAGE_SIZE - len,
457 " (%s: no entry)\n", DNAME(d->dev));
459 /* exit on end of buffer? */
460 if (len > PAGE_SIZE - 100)
465 len += snprintf(page + len, PAGE_SIZE - len, "\n");
472 * proc utility functions
475 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
477 read_proc_t* read_proc,
481 return create_proc_read_entry(name, mode, can_dir, read_proc,
487 static void can_remove_proc_readentry(const char *name)
490 remove_proc_entry(name, can_dir);
494 * can_init_proc - create main CAN proc directory and procfs entries
496 void can_init_proc(void)
498 /* create /proc/net/can directory */
499 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
502 printk(KERN_INFO "can: failed to create /proc/%s . "
503 "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
507 can_dir->owner = THIS_MODULE;
509 /* own procfs entries from the AF_CAN core */
510 pde_version = can_create_proc_readentry(CAN_PROC_VERSION,
511 0644, can_proc_read_version, NULL);
512 pde_stats = can_create_proc_readentry(CAN_PROC_STATS,
513 0644, can_proc_read_stats, NULL);
514 pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS,
515 0644, can_proc_read_reset_stats, NULL);
516 pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL,
517 0644, can_proc_read_rcvlist_all, NULL);
518 pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL,
519 0644, can_proc_read_rcvlist_fil, NULL);
520 pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV,
521 0644, can_proc_read_rcvlist_inv, NULL);
522 pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF,
523 0644, can_proc_read_rcvlist_sff, NULL);
524 pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF,
525 0644, can_proc_read_rcvlist_eff, NULL);
526 pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR,
527 0644, can_proc_read_rcvlist_err, NULL);
531 * can_remove_proc - remove procfs entries and main CAN proc directory
533 void can_remove_proc(void)
536 can_remove_proc_readentry(CAN_PROC_VERSION);
539 can_remove_proc_readentry(CAN_PROC_STATS);
542 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
545 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
548 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
551 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
554 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
557 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
560 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
563 remove_proc_entry(CAN_PROC_DIR, NULL);