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>
51 #include <linux/can/version.h>
59 #define CAN_PROC_VERSION "version"
60 #define CAN_PROC_STATS "stats"
61 #define CAN_PROC_RESET_STATS "reset_stats"
62 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
63 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
64 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
65 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
66 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
67 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
69 static void can_init_stats(int caller);
70 static void can_stat_update(unsigned long data);
72 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
73 mode_t mode, read_proc_t* read_proc, void *data);
74 static void can_remove_proc_readentry(const char *name);
75 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
78 static int can_proc_read_version(char *page, char **start, off_t off,
79 int count, int *eof, void *data);
80 static int can_proc_read_stats(char *page, char **start, off_t off,
81 int count, int *eof, void *data);
82 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
83 int count, int *eof, void *data);
84 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
85 int count, int *eof, void *data);
86 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
87 int count, int *eof, void *data);
88 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
89 int count, int *eof, void *data);
90 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
91 int count, int *eof, void *data);
92 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
93 int count, int *eof, void *data);
94 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
95 int count, int *eof, void *data);
97 static struct proc_dir_entry *can_dir = NULL;
98 static struct proc_dir_entry *pde_version = NULL;
99 static struct proc_dir_entry *pde_stats = NULL;
100 static struct proc_dir_entry *pde_reset_stats = NULL;
101 static struct proc_dir_entry *pde_rcvlist_all = NULL;
102 static struct proc_dir_entry *pde_rcvlist_fil = NULL;
103 static struct proc_dir_entry *pde_rcvlist_inv = NULL;
104 static struct proc_dir_entry *pde_rcvlist_sff = NULL;
105 static struct proc_dir_entry *pde_rcvlist_eff = NULL;
106 static struct proc_dir_entry *pde_rcvlist_err = NULL;
108 struct timer_list stattimer; /* timer for statistics update */
110 struct s_stats stats; /* statistics */
111 struct s_pstats pstats;
113 extern struct hlist_head rx_dev_list; /* rx dispatcher structures */
114 extern int stats_timer; /* module parameter. default: on */
116 /**************************************************/
117 /* procfs init / remove */
118 /**************************************************/
120 void can_init_proc(void)
125 /* create /proc/can directory */
126 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
129 printk(KERN_INFO "CAN: failed to create CAN_PROC_DIR. "
130 "CONFIG_PROC_FS missing?\n");
134 can_dir->owner = THIS_MODULE;
136 /* own procfs entries from the AF_CAN core */
137 pde_version = can_create_proc_readentry(
138 CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
139 pde_stats = can_create_proc_readentry(
140 CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
141 pde_reset_stats = can_create_proc_readentry(
142 CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
143 pde_rcvlist_all = can_create_proc_readentry(
144 CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
145 pde_rcvlist_fil = can_create_proc_readentry(
146 CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
147 pde_rcvlist_inv = can_create_proc_readentry(
148 CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
149 pde_rcvlist_sff = can_create_proc_readentry(
150 CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
151 pde_rcvlist_eff = can_create_proc_readentry(
152 CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
153 pde_rcvlist_err = can_create_proc_readentry(
154 CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
157 /* the statistics are updated every second (timer triggered) */
158 stattimer.function = can_stat_update;
160 stattimer.expires = jiffies + HZ; /* every second */
161 add_timer(&stattimer); /* start statistics timer */
165 void can_remove_proc(void)
169 can_remove_proc_readentry(CAN_PROC_VERSION);
172 can_remove_proc_readentry(CAN_PROC_STATS);
175 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
178 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
181 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
184 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
187 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
190 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
193 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
196 remove_proc_entry(CAN_PROC_DIR, NULL);
199 /**************************************************/
200 /* proc read functions */
201 /**************************************************/
203 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
204 struct net_device *dev)
207 struct hlist_node *n;
210 hlist_for_each_entry_rcu(r, n, rx_list, list) {
211 char *fmt = r->can_id & CAN_EFF_FLAG ? /* EFF & CAN_ID_ALL */
212 " %-5s %08X %08x %08x %08x %8ld %s\n" :
213 " %-5s %03X %08x %08x %08x %8ld %s\n";
215 len += snprintf(page + len, PAGE_SIZE - len, fmt,
216 DNAME(dev), r->can_id, r->mask,
217 (unsigned int)r->func, (unsigned int)r->data,
218 r->matches, r->ident);
220 /* does a typical line fit into the current buffer? */
221 /* 100 Bytes before end of buffer */
222 if (len > PAGE_SIZE - 100) {
223 /* mark output cut off */
224 len += snprintf(page + len, PAGE_SIZE - len,
234 static int can_print_recv_banner(char *page, int len)
236 /* can1. 00000000 00000000 00000000
238 len += snprintf(page + len, PAGE_SIZE - len,
239 " device can_id can_mask function"
240 " userdata matches ident\n");
245 static int can_proc_read_stats(char *page, char **start, off_t off,
246 int count, int *eof, void *data)
250 len += snprintf(page + len, PAGE_SIZE - len, "\n");
251 len += snprintf(page + len, PAGE_SIZE - len,
252 " %8ld transmitted frames (TXF)\n", stats.tx_frames);
253 len += snprintf(page + len, PAGE_SIZE - len,
254 " %8ld received frames (RXF)\n", stats.rx_frames);
255 len += snprintf(page + len, PAGE_SIZE - len,
256 " %8ld matched frames (RXMF)\n", stats.matches);
258 len += snprintf(page + len, PAGE_SIZE - len, "\n");
260 len += snprintf(page + len, PAGE_SIZE - len,
261 " %8ld %% total match ratio (RXMR)\n",
262 stats.total_rx_match_ratio);
264 len += snprintf(page + len, PAGE_SIZE - len,
265 " %8ld frames/s total tx rate (TXR)\n",
266 stats.total_tx_rate);
267 len += snprintf(page + len, PAGE_SIZE - len,
268 " %8ld frames/s total rx rate (RXR)\n",
269 stats.total_rx_rate);
271 len += snprintf(page + len, PAGE_SIZE - len, "\n");
273 len += snprintf(page + len, PAGE_SIZE - len,
274 " %8ld %% current match ratio (CRXMR)\n",
275 stats.current_rx_match_ratio);
277 len += snprintf(page + len, PAGE_SIZE - len,
278 " %8ld frames/s current tx rate (CTXR)\n",
279 stats.current_tx_rate);
280 len += snprintf(page + len, PAGE_SIZE - len,
281 " %8ld frames/s current rx rate (CRXR)\n",
282 stats.current_rx_rate);
284 len += snprintf(page + len, PAGE_SIZE - len, "\n");
286 len += snprintf(page + len, PAGE_SIZE - len,
287 " %8ld %% max match ratio (MRXMR)\n",
288 stats.max_rx_match_ratio);
290 len += snprintf(page + len, PAGE_SIZE - len,
291 " %8ld frames/s max tx rate (MTXR)\n",
293 len += snprintf(page + len, PAGE_SIZE - len,
294 " %8ld frames/s max rx rate (MRXR)\n",
297 len += snprintf(page + len, PAGE_SIZE - len, "\n");
299 len += snprintf(page + len, PAGE_SIZE - len,
300 " %8ld current receive list entries (CRCV)\n",
302 len += snprintf(page + len, PAGE_SIZE - len,
303 " %8ld maximum receive list entries (MRCV)\n",
304 pstats.rcv_entries_max);
306 if (pstats.stats_reset)
307 len += snprintf(page + len, PAGE_SIZE - len,
308 "\n %8ld statistic resets (STR)\n",
311 len += snprintf(page + len, PAGE_SIZE - len, "\n");
317 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
318 int count, int *eof, void *data)
324 len += snprintf(page + len, PAGE_SIZE - len,
325 "CAN statistic reset #%ld done.\n",
332 static int can_proc_read_version(char *page, char **start, off_t off,
333 int count, int *eof, void *data)
337 len += snprintf(page + len, PAGE_SIZE - len,
338 "%06X [ Volkswagen Group - Low Level CAN Framework"
339 " (LLCF) v%s ]\n", LLCF_VERSION_CODE, VERSION);
344 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
345 int count, int *eof, void *data)
348 struct dev_rcv_lists *d;
349 struct hlist_node *n;
352 len += snprintf(page + len, PAGE_SIZE - len,
353 "\nreceive list 'rx_all':\n");
355 /* find receive list for this device */
357 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
359 if (!hlist_empty(&d->rx_all)) {
360 len = can_print_recv_banner(page, len);
361 len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
363 len += snprintf(page + len, PAGE_SIZE - len,
364 " (%s: no entry)\n", DNAME(d->dev));
366 if (len > PAGE_SIZE - 100)
367 break; /* exit on end of buffer */
371 len += snprintf(page + len, PAGE_SIZE - len, "\n");
377 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
378 int count, int *eof, void *data)
381 struct dev_rcv_lists *d;
382 struct hlist_node *n;
385 len += snprintf(page + len, PAGE_SIZE - len,
386 "\nreceive list 'rx_fil':\n");
388 /* find receive list for this device */
390 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
392 if (!hlist_empty(&d->rx_fil)) {
393 len = can_print_recv_banner(page, len);
394 len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
396 len += snprintf(page + len, PAGE_SIZE - len,
397 " (%s: no entry)\n", DNAME(d->dev));
399 if (len > PAGE_SIZE - 100)
400 break; /* exit on end of buffer */
404 len += snprintf(page + len, PAGE_SIZE - len, "\n");
410 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
411 int count, int *eof, void *data)
414 struct dev_rcv_lists *d;
415 struct hlist_node *n;
418 len += snprintf(page + len, PAGE_SIZE - len,
419 "\nreceive list 'rx_inv':\n");
421 /* find receive list for this device */
423 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
425 if (!hlist_empty(&d->rx_inv)) {
426 len = can_print_recv_banner(page, len);
427 len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
429 len += snprintf(page + len, PAGE_SIZE - len,
430 " (%s: no entry)\n", DNAME(d->dev));
432 if (len > PAGE_SIZE - 100)
433 break; /* exit on end of buffer */
437 len += snprintf(page + len, PAGE_SIZE - len, "\n");
443 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
444 int count, int *eof, void *data)
447 struct dev_rcv_lists *d;
448 struct hlist_node *n;
451 len += snprintf(page + len, PAGE_SIZE - len,
452 "\nreceive list 'rx_sff':\n");
454 /* find receive list for this device */
456 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
457 int i, all_empty = 1;
458 /* check wether at least one list is non-empty */
459 for (i = 0; i < 0x800; i++)
460 if (!hlist_empty(&d->rx_sff[i])) {
466 len = can_print_recv_banner(page, len);
467 for (i = 0; i < 0x800; i++) {
468 if (!hlist_empty(&d->rx_sff[i]) &&
469 len < PAGE_SIZE - 100)
470 len = can_print_rcvlist(page, len,
475 len += snprintf(page + len, PAGE_SIZE - len,
476 " (%s: no entry)\n", DNAME(d->dev));
478 if (len > PAGE_SIZE - 100)
479 break; /* exit on end of buffer */
483 len += snprintf(page + len, PAGE_SIZE - len, "\n");
489 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
490 int count, int *eof, void *data)
493 struct dev_rcv_lists *d;
494 struct hlist_node *n;
497 len += snprintf(page + len, PAGE_SIZE - len,
498 "\nreceive list 'rx_eff':\n");
500 /* find receive list for this device */
502 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
504 if (!hlist_empty(&d->rx_eff)) {
505 len = can_print_recv_banner(page, len);
506 len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
508 len += snprintf(page + len, PAGE_SIZE - len,
509 " (%s: no entry)\n", DNAME(d->dev));
511 if (len > PAGE_SIZE - 100)
512 break; /* exit on end of buffer */
516 len += snprintf(page + len, PAGE_SIZE - len, "\n");
522 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
523 int count, int *eof, void *data)
526 struct dev_rcv_lists *d;
527 struct hlist_node *n;
530 len += snprintf(page + len, PAGE_SIZE - len,
531 "\nreceive list 'rx_err':\n");
533 /* find receive list for this device */
535 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
537 if (!hlist_empty(&d->rx_err)) {
538 len = can_print_recv_banner(page, len);
539 len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
541 len += snprintf(page + len, PAGE_SIZE - len,
542 " (%s: no entry)\n", DNAME(d->dev));
544 if (len > PAGE_SIZE - 100)
545 break; /* exit on end of buffer */
549 len += snprintf(page + len, PAGE_SIZE - len, "\n");
555 /**************************************************/
556 /* proc utility functions */
557 /**************************************************/
559 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
561 read_proc_t* read_proc,
565 return create_proc_read_entry(name, mode, can_dir, read_proc,
571 static void can_remove_proc_readentry(const char *name)
574 remove_proc_entry(name, can_dir);
577 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
580 unsigned long ret = 0;
582 if (oldjif == newjif)
585 /* see can_rcv() - this should NEVER happen! */
586 if (count > (ULONG_MAX / HZ)) {
587 printk(KERN_ERR "CAN: calc_rate: count exceeded! %ld\n",
592 ret = (count * HZ) / (newjif - oldjif);
597 /**************************************************/
598 /* af_can statistics stuff */
599 /**************************************************/
601 static void can_init_stats(int caller)
603 memset(&stats, 0, sizeof(stats));
604 stats.jiffies_init = jiffies;
605 pstats.stats_reset++;
608 static void can_stat_update(unsigned long data)
610 unsigned long j = jiffies; /* snapshot */
612 //DBG("CAN: can_stat_update() jiffies = %ld\n", j);
614 if (j < stats.jiffies_init) /* jiffies overflow */
617 /* stats.rx_frames is the definitively max. statistic value */
619 /* prevent overflow in calc_rate() */
620 if (stats.rx_frames > (ULONG_MAX / HZ))
621 can_init_stats(3); /* restart */
623 /* matches overflow - very improbable */
624 if (stats.matches > (ULONG_MAX / 100))
627 /* calc total values */
629 stats.total_rx_match_ratio = (stats.matches * 100) /
632 stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
634 stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
637 /* calc current values */
638 if (stats.rx_frames_delta)
639 stats.current_rx_match_ratio =
640 (stats.matches_delta * 100) / stats.rx_frames_delta;
642 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
643 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
645 /* check / update maximum values */
646 if (stats.max_tx_rate < stats.current_tx_rate)
647 stats.max_tx_rate = stats.current_tx_rate;
649 if (stats.max_rx_rate < stats.current_rx_rate)
650 stats.max_rx_rate = stats.current_rx_rate;
652 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
653 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
655 /* clear values for 'current rate' calculation */
656 stats.tx_frames_delta = 0;
657 stats.rx_frames_delta = 0;
658 stats.matches_delta = 0;
661 stattimer.expires = jiffies + HZ; /* every second */
662 add_timer(&stattimer);