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>
50 #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 void can_init_stats(int caller);
71 static void can_stat_update(unsigned long data);
73 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
74 mode_t mode, read_proc_t* read_proc, void *data);
75 static void can_remove_proc_readentry(const char *name);
76 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
79 static int can_proc_read_version(char *page, char **start, off_t off,
80 int count, int *eof, void *data);
81 static int can_proc_read_stats(char *page, char **start, off_t off,
82 int count, int *eof, void *data);
83 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
84 int count, int *eof, void *data);
85 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
86 int count, int *eof, void *data);
87 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
88 int count, int *eof, void *data);
89 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
90 int count, int *eof, void *data);
91 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
92 int count, int *eof, void *data);
93 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
94 int count, int *eof, void *data);
95 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
96 int count, int *eof, void *data);
98 static struct proc_dir_entry *can_dir = NULL;
99 static struct proc_dir_entry *pde_version = NULL;
100 static struct proc_dir_entry *pde_stats = NULL;
101 static struct proc_dir_entry *pde_reset_stats = NULL;
102 static struct proc_dir_entry *pde_rcvlist_all = NULL;
103 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
104 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
105 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
106 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
107 static struct proc_dir_entry *pde_rcvlist_err = NULL;
109 struct timer_list stattimer; /* timer for statistics update */
111 struct s_stats stats; /* statistics */
112 struct s_pstats pstats;
114 extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
115 extern int stats_timer; /* module parameter. default: on */
118 * can_init_proc - create main CAN proc directory and procfs entries
120 void can_init_proc(void)
122 /* create /proc/net/can directory */
123 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
126 printk(KERN_INFO "can: failed to create /proc/%s . "
127 "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
131 can_dir->owner = THIS_MODULE;
133 /* own procfs entries from the AF_CAN core */
134 pde_version = can_create_proc_readentry(
135 CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
136 pde_stats = can_create_proc_readentry(
137 CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
138 pde_reset_stats = can_create_proc_readentry(
139 CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
140 pde_rcvlist_all = can_create_proc_readentry(
141 CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
142 pde_rcvlist_fil = can_create_proc_readentry(
143 CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
144 pde_rcvlist_inv = can_create_proc_readentry(
145 CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
146 pde_rcvlist_sff = can_create_proc_readentry(
147 CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
148 pde_rcvlist_eff = can_create_proc_readentry(
149 CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
150 pde_rcvlist_err = can_create_proc_readentry(
151 CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
154 /* the statistics are updated every second (timer triggered) */
155 stattimer.function = can_stat_update;
157 /* update every second */
158 stattimer.expires = jiffies + HZ;
159 /* start statistics timer */
160 add_timer(&stattimer);
165 * can_remove_proc - remove procfs entries and main CAN proc directory
167 void can_remove_proc(void)
170 can_remove_proc_readentry(CAN_PROC_VERSION);
173 can_remove_proc_readentry(CAN_PROC_STATS);
176 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
179 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
182 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
185 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
188 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
191 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
194 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
197 remove_proc_entry(CAN_PROC_DIR, NULL);
201 * proc read functions
204 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
205 struct net_device *dev)
208 struct hlist_node *n;
211 hlist_for_each_entry_rcu(r, n, rx_list, list) {
212 char *fmt = (r->can_id & CAN_EFF_FLAG)?
213 " %-5s %08X %08x %08x %08x %8ld %s\n" :
214 " %-5s %03X %08x %08x %08x %8ld %s\n";
216 len += snprintf(page + len, PAGE_SIZE - len, fmt,
217 DNAME(dev), r->can_id, r->mask,
218 (unsigned int)r->func, (unsigned int)r->data,
219 r->matches, r->ident);
221 /* does a typical line fit into the current buffer? */
223 /* 100 Bytes before end of buffer */
224 if (len > PAGE_SIZE - 100) {
225 /* mark output cut off */
226 len += snprintf(page + len, PAGE_SIZE - len,
236 static int can_print_recv_banner(char *page, int len)
239 * can1. 00000000 00000000 00000000
242 len += snprintf(page + len, PAGE_SIZE - len,
243 " device can_id can_mask function"
244 " userdata matches ident\n");
249 static int can_proc_read_stats(char *page, char **start, off_t off,
250 int count, int *eof, void *data)
254 len += snprintf(page + len, PAGE_SIZE - len, "\n");
255 len += snprintf(page + len, PAGE_SIZE - len,
256 " %8ld transmitted frames (TXF)\n", stats.tx_frames);
257 len += snprintf(page + len, PAGE_SIZE - len,
258 " %8ld received frames (RXF)\n", stats.rx_frames);
259 len += snprintf(page + len, PAGE_SIZE - len,
260 " %8ld matched frames (RXMF)\n", stats.matches);
262 len += snprintf(page + len, PAGE_SIZE - len, "\n");
264 len += snprintf(page + len, PAGE_SIZE - len,
265 " %8ld %% total match ratio (RXMR)\n",
266 stats.total_rx_match_ratio);
268 len += snprintf(page + len, PAGE_SIZE - len,
269 " %8ld frames/s total tx rate (TXR)\n",
270 stats.total_tx_rate);
271 len += snprintf(page + len, PAGE_SIZE - len,
272 " %8ld frames/s total rx rate (RXR)\n",
273 stats.total_rx_rate);
275 len += snprintf(page + len, PAGE_SIZE - len, "\n");
277 len += snprintf(page + len, PAGE_SIZE - len,
278 " %8ld %% current match ratio (CRXMR)\n",
279 stats.current_rx_match_ratio);
281 len += snprintf(page + len, PAGE_SIZE - len,
282 " %8ld frames/s current tx rate (CTXR)\n",
283 stats.current_tx_rate);
284 len += snprintf(page + len, PAGE_SIZE - len,
285 " %8ld frames/s current rx rate (CRXR)\n",
286 stats.current_rx_rate);
288 len += snprintf(page + len, PAGE_SIZE - len, "\n");
290 len += snprintf(page + len, PAGE_SIZE - len,
291 " %8ld %% max match ratio (MRXMR)\n",
292 stats.max_rx_match_ratio);
294 len += snprintf(page + len, PAGE_SIZE - len,
295 " %8ld frames/s max tx rate (MTXR)\n",
297 len += snprintf(page + len, PAGE_SIZE - len,
298 " %8ld frames/s max rx rate (MRXR)\n",
301 len += snprintf(page + len, PAGE_SIZE - len, "\n");
303 len += snprintf(page + len, PAGE_SIZE - len,
304 " %8ld current receive list entries (CRCV)\n",
306 len += snprintf(page + len, PAGE_SIZE - len,
307 " %8ld maximum receive list entries (MRCV)\n",
308 pstats.rcv_entries_max);
310 if (pstats.stats_reset)
311 len += snprintf(page + len, PAGE_SIZE - len,
312 "\n %8ld statistic resets (STR)\n",
315 len += snprintf(page + len, PAGE_SIZE - len, "\n");
321 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
322 int count, int *eof, void *data)
328 len += snprintf(page + len, PAGE_SIZE - len,
329 "CAN statistic reset #%ld done.\n",
336 static int can_proc_read_version(char *page, char **start, off_t off,
337 int count, int *eof, void *data)
341 len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
347 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
348 int count, int *eof, void *data)
351 struct dev_rcv_lists *d;
352 struct hlist_node *n;
355 len += snprintf(page + len, PAGE_SIZE - len,
356 "\nreceive list 'rx_all':\n");
358 /* find receive list for this device */
360 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
362 if (!hlist_empty(&d->rx_all)) {
363 len = can_print_recv_banner(page, len);
364 len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
366 len += snprintf(page + len, PAGE_SIZE - len,
367 " (%s: no entry)\n", DNAME(d->dev));
369 /* exit on end of buffer? */
370 if (len > PAGE_SIZE - 100)
375 len += snprintf(page + len, PAGE_SIZE - len, "\n");
381 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
382 int count, int *eof, void *data)
385 struct dev_rcv_lists *d;
386 struct hlist_node *n;
389 len += snprintf(page + len, PAGE_SIZE - len,
390 "\nreceive list 'rx_fil':\n");
392 /* find receive list for this device */
394 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
396 if (!hlist_empty(&d->rx_fil)) {
397 len = can_print_recv_banner(page, len);
398 len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
400 len += snprintf(page + len, PAGE_SIZE - len,
401 " (%s: no entry)\n", DNAME(d->dev));
403 /* exit on end of buffer? */
404 if (len > PAGE_SIZE - 100)
409 len += snprintf(page + len, PAGE_SIZE - len, "\n");
415 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
416 int count, int *eof, void *data)
419 struct dev_rcv_lists *d;
420 struct hlist_node *n;
423 len += snprintf(page + len, PAGE_SIZE - len,
424 "\nreceive list 'rx_inv':\n");
426 /* find receive list for this device */
428 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
430 if (!hlist_empty(&d->rx_inv)) {
431 len = can_print_recv_banner(page, len);
432 len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
434 len += snprintf(page + len, PAGE_SIZE - len,
435 " (%s: no entry)\n", DNAME(d->dev));
437 /* exit on end of buffer? */
438 if (len > PAGE_SIZE - 100)
443 len += snprintf(page + len, PAGE_SIZE - len, "\n");
449 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
450 int count, int *eof, void *data)
453 struct dev_rcv_lists *d;
454 struct hlist_node *n;
457 len += snprintf(page + len, PAGE_SIZE - len,
458 "\nreceive list 'rx_sff':\n");
460 /* find receive list for this device */
462 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
463 int i, all_empty = 1;
464 /* check wether at least one list is non-empty */
465 for (i = 0; i < 0x800; i++)
466 if (!hlist_empty(&d->rx_sff[i])) {
472 len = can_print_recv_banner(page, len);
473 for (i = 0; i < 0x800; i++) {
474 if (!hlist_empty(&d->rx_sff[i]) &&
475 len < PAGE_SIZE - 100)
476 len = can_print_rcvlist(page, len,
481 len += snprintf(page + len, PAGE_SIZE - len,
482 " (%s: no entry)\n", DNAME(d->dev));
484 /* exit on end of buffer? */
485 if (len > PAGE_SIZE - 100)
490 len += snprintf(page + len, PAGE_SIZE - len, "\n");
496 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
497 int count, int *eof, void *data)
500 struct dev_rcv_lists *d;
501 struct hlist_node *n;
504 len += snprintf(page + len, PAGE_SIZE - len,
505 "\nreceive list 'rx_eff':\n");
507 /* find receive list for this device */
509 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
511 if (!hlist_empty(&d->rx_eff)) {
512 len = can_print_recv_banner(page, len);
513 len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
515 len += snprintf(page + len, PAGE_SIZE - len,
516 " (%s: no entry)\n", DNAME(d->dev));
518 /* exit on end of buffer? */
519 if (len > PAGE_SIZE - 100)
524 len += snprintf(page + len, PAGE_SIZE - len, "\n");
530 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
531 int count, int *eof, void *data)
534 struct dev_rcv_lists *d;
535 struct hlist_node *n;
538 len += snprintf(page + len, PAGE_SIZE - len,
539 "\nreceive list 'rx_err':\n");
541 /* find receive list for this device */
543 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
545 if (!hlist_empty(&d->rx_err)) {
546 len = can_print_recv_banner(page, len);
547 len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
549 len += snprintf(page + len, PAGE_SIZE - len,
550 " (%s: no entry)\n", DNAME(d->dev));
552 /* exit on end of buffer? */
553 if (len > PAGE_SIZE - 100)
558 len += snprintf(page + len, PAGE_SIZE - len, "\n");
565 * proc utility functions
568 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
570 read_proc_t* read_proc,
574 return create_proc_read_entry(name, mode, can_dir, read_proc,
580 static void can_remove_proc_readentry(const char *name)
583 remove_proc_entry(name, can_dir);
586 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
589 unsigned long ret = 0;
591 if (oldjif == newjif)
594 /* see can_rcv() - this should NEVER happen! */
595 if (count > (ULONG_MAX / HZ)) {
596 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
601 ret = (count * HZ) / (newjif - oldjif);
607 * af_can statistics stuff
610 static void can_init_stats(int caller)
612 memset(&stats, 0, sizeof(stats));
613 stats.jiffies_init = jiffies;
614 pstats.stats_reset++;
617 static void can_stat_update(unsigned long data)
619 unsigned long j = jiffies; /* snapshot */
621 if (j < stats.jiffies_init) /* jiffies overflow */
624 /* stats.rx_frames is the definitively max. statistic value */
626 /* prevent overflow in calc_rate() */
627 if (stats.rx_frames > (ULONG_MAX / HZ))
630 /* matches overflow - very improbable */
631 if (stats.matches > (ULONG_MAX / 100))
634 /* calc total values */
636 stats.total_rx_match_ratio = (stats.matches * 100) /
639 stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
641 stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
644 /* calc current values */
645 if (stats.rx_frames_delta)
646 stats.current_rx_match_ratio =
647 (stats.matches_delta * 100) / stats.rx_frames_delta;
649 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
650 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
652 /* check / update maximum values */
653 if (stats.max_tx_rate < stats.current_tx_rate)
654 stats.max_tx_rate = stats.current_tx_rate;
656 if (stats.max_rx_rate < stats.current_rx_rate)
657 stats.max_rx_rate = stats.current_rx_rate;
659 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
660 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
662 /* clear values for 'current rate' calculation */
663 stats.tx_frames_delta = 0;
664 stats.rx_frames_delta = 0;
665 stats.matches_delta = 0;
668 stattimer.expires = jiffies + HZ; /* every second */
669 add_timer(&stattimer);