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 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;
82 * af_can statistics stuff
85 static void can_init_stats(void)
87 memset(&stats, 0, sizeof(stats));
88 stats.jiffies_init = jiffies;
92 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
95 unsigned long ret = 0;
100 /* see can_rcv() - this should NEVER happen! */
101 if (count > (ULONG_MAX / HZ)) {
102 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
107 ret = (count * HZ) / (newjif - oldjif);
112 void can_stat_update(unsigned long data)
114 unsigned long j = jiffies; /* snapshot */
116 if (j < stats.jiffies_init) /* jiffies overflow */
119 /* stats.rx_frames is the definitively max. statistic value */
121 /* prevent overflow in calc_rate() */
122 if (stats.rx_frames > (ULONG_MAX / HZ))
125 /* matches overflow - very improbable */
126 if (stats.matches > (ULONG_MAX / 100))
129 /* calc total values */
131 stats.total_rx_match_ratio = (stats.matches * 100) /
134 stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
136 stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
139 /* calc current values */
140 if (stats.rx_frames_delta)
141 stats.current_rx_match_ratio =
142 (stats.matches_delta * 100) / stats.rx_frames_delta;
144 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
145 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
147 /* check / update maximum values */
148 if (stats.max_tx_rate < stats.current_tx_rate)
149 stats.max_tx_rate = stats.current_tx_rate;
151 if (stats.max_rx_rate < stats.current_rx_rate)
152 stats.max_rx_rate = stats.current_rx_rate;
154 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
155 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
157 /* clear values for 'current rate' calculation */
158 stats.tx_frames_delta = 0;
159 stats.rx_frames_delta = 0;
160 stats.matches_delta = 0;
163 stattimer.expires = jiffies + HZ; /* every second */
164 add_timer(&stattimer);
168 * proc read functions
171 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
172 struct net_device *dev)
175 struct hlist_node *n;
178 hlist_for_each_entry_rcu(r, n, rx_list, list) {
179 char *fmt = (r->can_id & CAN_EFF_FLAG)?
180 " %-5s %08X %08x %08x %08x %8ld %s\n" :
181 " %-5s %03X %08x %08x %08x %8ld %s\n";
183 len += snprintf(page + len, PAGE_SIZE - len, fmt,
184 DNAME(dev), r->can_id, r->mask,
185 (unsigned int)r->func, (unsigned int)r->data,
186 r->matches, r->ident);
188 /* does a typical line fit into the current buffer? */
190 /* 100 Bytes before end of buffer */
191 if (len > PAGE_SIZE - 100) {
192 /* mark output cut off */
193 len += snprintf(page + len, PAGE_SIZE - len,
203 static int can_print_recv_banner(char *page, int len)
206 * can1. 00000000 00000000 00000000
209 len += snprintf(page + len, PAGE_SIZE - len,
210 " device can_id can_mask function"
211 " userdata matches ident\n");
216 static int can_proc_read_stats(char *page, char **start, off_t off,
217 int count, int *eof, void *data)
221 len += snprintf(page + len, PAGE_SIZE - len, "\n");
222 len += snprintf(page + len, PAGE_SIZE - len,
223 " %8ld transmitted frames (TXF)\n", stats.tx_frames);
224 len += snprintf(page + len, PAGE_SIZE - len,
225 " %8ld received frames (RXF)\n", stats.rx_frames);
226 len += snprintf(page + len, PAGE_SIZE - len,
227 " %8ld matched frames (RXMF)\n", stats.matches);
229 len += snprintf(page + len, PAGE_SIZE - len, "\n");
231 len += snprintf(page + len, PAGE_SIZE - len,
232 " %8ld %% total match ratio (RXMR)\n",
233 stats.total_rx_match_ratio);
235 len += snprintf(page + len, PAGE_SIZE - len,
236 " %8ld frames/s total tx rate (TXR)\n",
237 stats.total_tx_rate);
238 len += snprintf(page + len, PAGE_SIZE - len,
239 " %8ld frames/s total rx rate (RXR)\n",
240 stats.total_rx_rate);
242 len += snprintf(page + len, PAGE_SIZE - len, "\n");
244 len += snprintf(page + len, PAGE_SIZE - len,
245 " %8ld %% current match ratio (CRXMR)\n",
246 stats.current_rx_match_ratio);
248 len += snprintf(page + len, PAGE_SIZE - len,
249 " %8ld frames/s current tx rate (CTXR)\n",
250 stats.current_tx_rate);
251 len += snprintf(page + len, PAGE_SIZE - len,
252 " %8ld frames/s current rx rate (CRXR)\n",
253 stats.current_rx_rate);
255 len += snprintf(page + len, PAGE_SIZE - len, "\n");
257 len += snprintf(page + len, PAGE_SIZE - len,
258 " %8ld %% max match ratio (MRXMR)\n",
259 stats.max_rx_match_ratio);
261 len += snprintf(page + len, PAGE_SIZE - len,
262 " %8ld frames/s max tx rate (MTXR)\n",
264 len += snprintf(page + len, PAGE_SIZE - len,
265 " %8ld frames/s max rx rate (MRXR)\n",
268 len += snprintf(page + len, PAGE_SIZE - len, "\n");
270 len += snprintf(page + len, PAGE_SIZE - len,
271 " %8ld current receive list entries (CRCV)\n",
273 len += snprintf(page + len, PAGE_SIZE - len,
274 " %8ld maximum receive list entries (MRCV)\n",
275 pstats.rcv_entries_max);
277 if (pstats.stats_reset)
278 len += snprintf(page + len, PAGE_SIZE - len,
279 "\n %8ld statistic resets (STR)\n",
282 len += snprintf(page + len, PAGE_SIZE - len, "\n");
288 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
289 int count, int *eof, void *data)
295 len += snprintf(page + len, PAGE_SIZE - len,
296 "CAN statistic reset #%ld done.\n",
303 static int can_proc_read_version(char *page, char **start, off_t off,
304 int count, int *eof, void *data)
308 len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
314 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
315 int count, int *eof, void *data)
318 struct dev_rcv_lists *d;
319 struct hlist_node *n;
322 len += snprintf(page + len, PAGE_SIZE - len,
323 "\nreceive list 'rx_all':\n");
325 /* find receive list for this device */
327 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
329 if (!hlist_empty(&d->rx_all)) {
330 len = can_print_recv_banner(page, len);
331 len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
333 len += snprintf(page + len, PAGE_SIZE - len,
334 " (%s: no entry)\n", DNAME(d->dev));
336 /* exit on end of buffer? */
337 if (len > PAGE_SIZE - 100)
342 len += snprintf(page + len, PAGE_SIZE - len, "\n");
348 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
349 int count, int *eof, void *data)
352 struct dev_rcv_lists *d;
353 struct hlist_node *n;
356 len += snprintf(page + len, PAGE_SIZE - len,
357 "\nreceive list 'rx_fil':\n");
359 /* find receive list for this device */
361 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
363 if (!hlist_empty(&d->rx_fil)) {
364 len = can_print_recv_banner(page, len);
365 len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
367 len += snprintf(page + len, PAGE_SIZE - len,
368 " (%s: no entry)\n", DNAME(d->dev));
370 /* exit on end of buffer? */
371 if (len > PAGE_SIZE - 100)
376 len += snprintf(page + len, PAGE_SIZE - len, "\n");
382 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
383 int count, int *eof, void *data)
386 struct dev_rcv_lists *d;
387 struct hlist_node *n;
390 len += snprintf(page + len, PAGE_SIZE - len,
391 "\nreceive list 'rx_inv':\n");
393 /* find receive list for this device */
395 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
397 if (!hlist_empty(&d->rx_inv)) {
398 len = can_print_recv_banner(page, len);
399 len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
401 len += snprintf(page + len, PAGE_SIZE - len,
402 " (%s: no entry)\n", DNAME(d->dev));
404 /* exit on end of buffer? */
405 if (len > PAGE_SIZE - 100)
410 len += snprintf(page + len, PAGE_SIZE - len, "\n");
416 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
417 int count, int *eof, void *data)
420 struct dev_rcv_lists *d;
421 struct hlist_node *n;
424 len += snprintf(page + len, PAGE_SIZE - len,
425 "\nreceive list 'rx_sff':\n");
427 /* find receive list for this device */
429 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
430 int i, all_empty = 1;
431 /* check wether at least one list is non-empty */
432 for (i = 0; i < 0x800; i++)
433 if (!hlist_empty(&d->rx_sff[i])) {
439 len = can_print_recv_banner(page, len);
440 for (i = 0; i < 0x800; i++) {
441 if (!hlist_empty(&d->rx_sff[i]) &&
442 len < PAGE_SIZE - 100)
443 len = can_print_rcvlist(page, len,
448 len += snprintf(page + len, PAGE_SIZE - len,
449 " (%s: no entry)\n", DNAME(d->dev));
451 /* exit on end of buffer? */
452 if (len > PAGE_SIZE - 100)
457 len += snprintf(page + len, PAGE_SIZE - len, "\n");
463 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
464 int count, int *eof, void *data)
467 struct dev_rcv_lists *d;
468 struct hlist_node *n;
471 len += snprintf(page + len, PAGE_SIZE - len,
472 "\nreceive list 'rx_eff':\n");
474 /* find receive list for this device */
476 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
478 if (!hlist_empty(&d->rx_eff)) {
479 len = can_print_recv_banner(page, len);
480 len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
482 len += snprintf(page + len, PAGE_SIZE - len,
483 " (%s: no entry)\n", DNAME(d->dev));
485 /* exit on end of buffer? */
486 if (len > PAGE_SIZE - 100)
491 len += snprintf(page + len, PAGE_SIZE - len, "\n");
497 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
498 int count, int *eof, void *data)
501 struct dev_rcv_lists *d;
502 struct hlist_node *n;
505 len += snprintf(page + len, PAGE_SIZE - len,
506 "\nreceive list 'rx_err':\n");
508 /* find receive list for this device */
510 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
512 if (!hlist_empty(&d->rx_err)) {
513 len = can_print_recv_banner(page, len);
514 len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
516 len += snprintf(page + len, PAGE_SIZE - len,
517 " (%s: no entry)\n", DNAME(d->dev));
519 /* exit on end of buffer? */
520 if (len > PAGE_SIZE - 100)
525 len += snprintf(page + len, PAGE_SIZE - len, "\n");
532 * proc utility functions
535 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
537 read_proc_t* read_proc,
541 return create_proc_read_entry(name, mode, can_dir, read_proc,
547 static void can_remove_proc_readentry(const char *name)
550 remove_proc_entry(name, can_dir);
554 * can_init_proc - create main CAN proc directory and procfs entries
556 void can_init_proc(void)
558 /* create /proc/net/can directory */
559 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
562 printk(KERN_INFO "can: failed to create /proc/%s . "
563 "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
567 can_dir->owner = THIS_MODULE;
569 /* own procfs entries from the AF_CAN core */
570 pde_version = can_create_proc_readentry(
571 CAN_PROC_VERSION, 0644, can_proc_read_version, NULL);
572 pde_stats = can_create_proc_readentry(
573 CAN_PROC_STATS, 0644, can_proc_read_stats, NULL);
574 pde_reset_stats = can_create_proc_readentry(
575 CAN_PROC_RESET_STATS, 0644, can_proc_read_reset_stats, NULL);
576 pde_rcvlist_all = can_create_proc_readentry(
577 CAN_PROC_RCVLIST_ALL, 0644, can_proc_read_rcvlist_all, NULL);
578 pde_rcvlist_fil = can_create_proc_readentry(
579 CAN_PROC_RCVLIST_FIL, 0644, can_proc_read_rcvlist_fil, NULL);
580 pde_rcvlist_inv = can_create_proc_readentry(
581 CAN_PROC_RCVLIST_INV, 0644, can_proc_read_rcvlist_inv, NULL);
582 pde_rcvlist_sff = can_create_proc_readentry(
583 CAN_PROC_RCVLIST_SFF, 0644, can_proc_read_rcvlist_sff, NULL);
584 pde_rcvlist_eff = can_create_proc_readentry(
585 CAN_PROC_RCVLIST_EFF, 0644, can_proc_read_rcvlist_eff, NULL);
586 pde_rcvlist_err = can_create_proc_readentry(
587 CAN_PROC_RCVLIST_ERR, 0644, can_proc_read_rcvlist_err, NULL);
591 * can_remove_proc - remove procfs entries and main CAN proc directory
593 void can_remove_proc(void)
596 can_remove_proc_readentry(CAN_PROC_VERSION);
599 can_remove_proc_readentry(CAN_PROC_STATS);
602 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
605 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
608 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
611 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
614 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
617 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
620 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
623 remove_proc_entry(CAN_PROC_DIR, NULL);