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;
81 static int user_reset = 0;
84 * af_can statistics stuff
87 static void can_init_stats(void)
90 * This memset function is called from a timer context (when
91 * stattimer is active which is the default) OR in a process
92 * context (reading the proc_fs when stattimer is disabled).
94 spin_lock(&stats_lock);
95 memset(&stats, 0, sizeof(stats));
96 stats.jiffies_init = jiffies;
97 spin_unlock(&stats_lock);
107 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
110 unsigned long ret = 0;
112 if (oldjif == newjif)
115 /* see can_rcv() - this should NEVER happen! */
116 if (count > (ULONG_MAX / HZ)) {
117 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
122 ret = (count * HZ) / (newjif - oldjif);
127 void can_stat_update(unsigned long data)
129 unsigned long j = jiffies; /* snapshot */
131 /* restart counting in timer context on user request */
135 /* restart counting on jiffies overflow */
136 if (j < stats.jiffies_init)
139 /* stats.rx_frames is the definitively max. statistic value */
141 /* prevent overflow in calc_rate() */
142 if (stats.rx_frames > (ULONG_MAX / HZ))
145 /* matches overflow - very improbable */
146 if (stats.matches > (ULONG_MAX / 100))
149 /* calc total values */
151 stats.total_rx_match_ratio = (stats.matches * 100) /
154 stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
156 stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
159 /* calc current values */
160 if (stats.rx_frames_delta)
161 stats.current_rx_match_ratio =
162 (stats.matches_delta * 100) / stats.rx_frames_delta;
164 stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
165 stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
167 /* check / update maximum values */
168 if (stats.max_tx_rate < stats.current_tx_rate)
169 stats.max_tx_rate = stats.current_tx_rate;
171 if (stats.max_rx_rate < stats.current_rx_rate)
172 stats.max_rx_rate = stats.current_rx_rate;
174 if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
175 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
177 /* clear values for 'current rate' calculation */
178 stats.tx_frames_delta = 0;
179 stats.rx_frames_delta = 0;
180 stats.matches_delta = 0;
182 /* restart timer (one second) */
183 stattimer.expires = jiffies + HZ;
184 add_timer(&stattimer);
188 * proc read functions
191 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
192 struct net_device *dev)
195 struct hlist_node *n;
198 hlist_for_each_entry_rcu(r, n, rx_list, list) {
199 char *fmt = (r->can_id & CAN_EFF_FLAG)?
200 " %-5s %08X %08x %08x %08x %8ld %s\n" :
201 " %-5s %03X %08x %08x %08x %8ld %s\n";
203 len += snprintf(page + len, PAGE_SIZE - len, fmt,
204 DNAME(dev), r->can_id, r->mask,
205 (unsigned int)r->func, (unsigned int)r->data,
206 r->matches, r->ident);
208 /* does a typical line fit into the current buffer? */
210 /* 100 Bytes before end of buffer */
211 if (len > PAGE_SIZE - 100) {
212 /* mark output cut off */
213 len += snprintf(page + len, PAGE_SIZE - len,
223 static int can_print_recv_banner(char *page, int len)
226 * can1. 00000000 00000000 00000000
229 len += snprintf(page + len, PAGE_SIZE - len,
230 " device can_id can_mask function"
231 " userdata matches ident\n");
236 static int can_proc_read_stats(char *page, char **start, off_t off,
237 int count, int *eof, void *data)
241 len += snprintf(page + len, PAGE_SIZE - len, "\n");
242 len += snprintf(page + len, PAGE_SIZE - len,
243 " %8ld transmitted frames (TXF)\n", stats.tx_frames);
244 len += snprintf(page + len, PAGE_SIZE - len,
245 " %8ld received frames (RXF)\n", stats.rx_frames);
246 len += snprintf(page + len, PAGE_SIZE - len,
247 " %8ld matched frames (RXMF)\n", stats.matches);
249 len += snprintf(page + len, PAGE_SIZE - len, "\n");
251 if (stattimer.function == can_stat_update) {
252 len += snprintf(page + len, PAGE_SIZE - len,
253 " %8ld %% total match ratio (RXMR)\n",
254 stats.total_rx_match_ratio);
256 len += snprintf(page + len, PAGE_SIZE - len,
257 " %8ld frames/s total tx rate (TXR)\n",
258 stats.total_tx_rate);
259 len += snprintf(page + len, PAGE_SIZE - len,
260 " %8ld frames/s total rx rate (RXR)\n",
261 stats.total_rx_rate);
263 len += snprintf(page + len, PAGE_SIZE - len, "\n");
265 len += snprintf(page + len, PAGE_SIZE - len,
266 " %8ld %% current match ratio (CRXMR)\n",
267 stats.current_rx_match_ratio);
269 len += snprintf(page + len, PAGE_SIZE - len,
270 " %8ld frames/s current tx rate (CTXR)\n",
271 stats.current_tx_rate);
272 len += snprintf(page + len, PAGE_SIZE - len,
273 " %8ld frames/s current rx rate (CRXR)\n",
274 stats.current_rx_rate);
276 len += snprintf(page + len, PAGE_SIZE - len, "\n");
278 len += snprintf(page + len, PAGE_SIZE - len,
279 " %8ld %% max match ratio (MRXMR)\n",
280 stats.max_rx_match_ratio);
282 len += snprintf(page + len, PAGE_SIZE - len,
283 " %8ld frames/s max tx rate (MTXR)\n",
285 len += snprintf(page + len, PAGE_SIZE - len,
286 " %8ld frames/s max rx rate (MRXR)\n",
289 len += snprintf(page + len, PAGE_SIZE - len, "\n");
292 len += snprintf(page + len, PAGE_SIZE - len,
293 " %8ld current receive list entries (CRCV)\n",
295 len += snprintf(page + len, PAGE_SIZE - len,
296 " %8ld maximum receive list entries (MRCV)\n",
297 pstats.rcv_entries_max);
299 if (pstats.stats_reset)
300 len += snprintf(page + len, PAGE_SIZE - len,
301 "\n %8ld statistic resets (STR)\n",
304 if (pstats.user_reset)
305 len += snprintf(page + len, PAGE_SIZE - len,
306 " %8ld user statistic resets (USTR)\n",
309 len += snprintf(page + len, PAGE_SIZE - len, "\n");
315 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
316 int count, int *eof, void *data)
322 if (stattimer.function == can_stat_update) {
323 len += snprintf(page + len, PAGE_SIZE - len,
324 "Scheduled statistic reset #%ld.\n",
325 pstats.stats_reset + 1);
328 if (stats.jiffies_init != jiffies)
331 len += snprintf(page + len, PAGE_SIZE - len,
332 "Performed statistic reset #%ld.\n",
340 static int can_proc_read_version(char *page, char **start, off_t off,
341 int count, int *eof, void *data)
345 len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
351 static int can_proc_read_rcvlist_all(char *page, char **start, off_t off,
352 int count, int *eof, void *data)
355 struct dev_rcv_lists *d;
356 struct hlist_node *n;
359 len += snprintf(page + len, PAGE_SIZE - len,
360 "\nreceive list 'rx_all':\n");
362 /* find receive list for this device */
364 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
366 if (!hlist_empty(&d->rx_all)) {
367 len = can_print_recv_banner(page, len);
368 len = can_print_rcvlist(page, len, &d->rx_all, d->dev);
370 len += snprintf(page + len, PAGE_SIZE - len,
371 " (%s: no entry)\n", DNAME(d->dev));
373 /* exit on end of buffer? */
374 if (len > PAGE_SIZE - 100)
379 len += snprintf(page + len, PAGE_SIZE - len, "\n");
385 static int can_proc_read_rcvlist_fil(char *page, char **start, off_t off,
386 int count, int *eof, void *data)
389 struct dev_rcv_lists *d;
390 struct hlist_node *n;
393 len += snprintf(page + len, PAGE_SIZE - len,
394 "\nreceive list 'rx_fil':\n");
396 /* find receive list for this device */
398 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
400 if (!hlist_empty(&d->rx_fil)) {
401 len = can_print_recv_banner(page, len);
402 len = can_print_rcvlist(page, len, &d->rx_fil, d->dev);
404 len += snprintf(page + len, PAGE_SIZE - len,
405 " (%s: no entry)\n", DNAME(d->dev));
407 /* exit on end of buffer? */
408 if (len > PAGE_SIZE - 100)
413 len += snprintf(page + len, PAGE_SIZE - len, "\n");
419 static int can_proc_read_rcvlist_inv(char *page, char **start, off_t off,
420 int count, int *eof, void *data)
423 struct dev_rcv_lists *d;
424 struct hlist_node *n;
427 len += snprintf(page + len, PAGE_SIZE - len,
428 "\nreceive list 'rx_inv':\n");
430 /* find receive list for this device */
432 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
434 if (!hlist_empty(&d->rx_inv)) {
435 len = can_print_recv_banner(page, len);
436 len = can_print_rcvlist(page, len, &d->rx_inv, d->dev);
438 len += snprintf(page + len, PAGE_SIZE - len,
439 " (%s: no entry)\n", DNAME(d->dev));
441 /* exit on end of buffer? */
442 if (len > PAGE_SIZE - 100)
447 len += snprintf(page + len, PAGE_SIZE - len, "\n");
453 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
454 int count, int *eof, void *data)
457 struct dev_rcv_lists *d;
458 struct hlist_node *n;
461 len += snprintf(page + len, PAGE_SIZE - len,
462 "\nreceive list 'rx_sff':\n");
464 /* find receive list for this device */
466 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
467 int i, all_empty = 1;
468 /* check wether at least one list is non-empty */
469 for (i = 0; i < 0x800; i++)
470 if (!hlist_empty(&d->rx_sff[i])) {
476 len = can_print_recv_banner(page, len);
477 for (i = 0; i < 0x800; i++) {
478 if (!hlist_empty(&d->rx_sff[i]) &&
479 len < PAGE_SIZE - 100)
480 len = can_print_rcvlist(page, len,
485 len += snprintf(page + len, PAGE_SIZE - len,
486 " (%s: no entry)\n", DNAME(d->dev));
488 /* exit on end of buffer? */
489 if (len > PAGE_SIZE - 100)
494 len += snprintf(page + len, PAGE_SIZE - len, "\n");
500 static int can_proc_read_rcvlist_eff(char *page, char **start, off_t off,
501 int count, int *eof, void *data)
504 struct dev_rcv_lists *d;
505 struct hlist_node *n;
508 len += snprintf(page + len, PAGE_SIZE - len,
509 "\nreceive list 'rx_eff':\n");
511 /* find receive list for this device */
513 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
515 if (!hlist_empty(&d->rx_eff)) {
516 len = can_print_recv_banner(page, len);
517 len = can_print_rcvlist(page, len, &d->rx_eff, d->dev);
519 len += snprintf(page + len, PAGE_SIZE - len,
520 " (%s: no entry)\n", DNAME(d->dev));
522 /* exit on end of buffer? */
523 if (len > PAGE_SIZE - 100)
528 len += snprintf(page + len, PAGE_SIZE - len, "\n");
534 static int can_proc_read_rcvlist_err(char *page, char **start, off_t off,
535 int count, int *eof, void *data)
538 struct dev_rcv_lists *d;
539 struct hlist_node *n;
542 len += snprintf(page + len, PAGE_SIZE - len,
543 "\nreceive list 'rx_err':\n");
545 /* find receive list for this device */
547 hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
549 if (!hlist_empty(&d->rx_err)) {
550 len = can_print_recv_banner(page, len);
551 len = can_print_rcvlist(page, len, &d->rx_err, d->dev);
553 len += snprintf(page + len, PAGE_SIZE - len,
554 " (%s: no entry)\n", DNAME(d->dev));
556 /* exit on end of buffer? */
557 if (len > PAGE_SIZE - 100)
562 len += snprintf(page + len, PAGE_SIZE - len, "\n");
569 * proc utility functions
572 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
574 read_proc_t* read_proc,
578 return create_proc_read_entry(name, mode, can_dir, read_proc,
584 static void can_remove_proc_readentry(const char *name)
587 remove_proc_entry(name, can_dir);
591 * can_init_proc - create main CAN proc directory and procfs entries
593 void can_init_proc(void)
595 /* create /proc/net/can directory */
596 can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
599 printk(KERN_INFO "can: failed to create /proc/%s . "
600 "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
604 can_dir->owner = THIS_MODULE;
606 /* own procfs entries from the AF_CAN core */
607 pde_version = can_create_proc_readentry(CAN_PROC_VERSION,
608 0644, can_proc_read_version, NULL);
609 pde_stats = can_create_proc_readentry(CAN_PROC_STATS,
610 0644, can_proc_read_stats, NULL);
611 pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS,
612 0644, can_proc_read_reset_stats, NULL);
613 pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL,
614 0644, can_proc_read_rcvlist_all, NULL);
615 pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL,
616 0644, can_proc_read_rcvlist_fil, NULL);
617 pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV,
618 0644, can_proc_read_rcvlist_inv, NULL);
619 pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF,
620 0644, can_proc_read_rcvlist_sff, NULL);
621 pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF,
622 0644, can_proc_read_rcvlist_eff, NULL);
623 pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR,
624 0644, can_proc_read_rcvlist_err, NULL);
628 * can_remove_proc - remove procfs entries and main CAN proc directory
630 void can_remove_proc(void)
633 can_remove_proc_readentry(CAN_PROC_VERSION);
636 can_remove_proc_readentry(CAN_PROC_STATS);
639 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
642 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
645 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
648 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
651 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
654 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
657 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
660 remove_proc_entry(CAN_PROC_DIR, NULL);