]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/proc.c
Move branch ha/locktest to trunk/kernel/2.6, making it the ne trunk version.
[socketcan-devel.git] / kernel / 2.6 / net / can / proc.c
1 /*
2  * proc.c - procfs support for Protocol family CAN core module
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
19  *
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.
24  *
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.
27  *
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
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
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>
50
51 #include "af_can.h"
52
53 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
54 RCSID("$Id$");
55
56 /*
57  * proc filenames for the PF_CAN core
58  */
59
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"
69
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;
80
81 static int user_reset = 0;
82
83 static const char *rx_list_name[] = {
84         [RX_ERR] = "rx_err",
85         [RX_ALL] = "rx_all",
86         [RX_FIL] = "rx_fil",
87         [RX_INV] = "rx_inv",
88         [RX_EFF] = "rx_eff",
89 };
90
91 /*
92  * af_can statistics stuff
93  */
94
95 static void can_init_stats(void)
96 {
97         /*
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).
101          */
102         memset(&stats, 0, sizeof(stats));
103         stats.jiffies_init = jiffies;
104
105         pstats.stats_reset++;
106
107         if (user_reset) {
108                 user_reset = 0;
109                 pstats.user_reset++;
110         }
111 }
112
113 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
114                                unsigned long count)
115 {
116         unsigned long ret = 0;
117
118         if (oldjif == newjif)
119                 return 0;
120
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",
124                        count);
125                 return 99999999;
126         }
127
128         ret = (count * HZ) / (newjif - oldjif);
129
130         return ret;
131 }
132
133 void can_stat_update(unsigned long data)
134 {
135         unsigned long j = jiffies; /* snapshot */
136
137         /* restart counting in timer context on user request */
138         if (user_reset)
139                 can_init_stats();
140
141         /* restart counting on jiffies overflow */
142         if (j < stats.jiffies_init)
143                 can_init_stats();
144
145         /* stats.rx_frames is the definitively max. statistic value */
146
147         /* prevent overflow in calc_rate() */
148         if (stats.rx_frames > (ULONG_MAX / HZ))
149                 can_init_stats();
150
151         /* matches overflow - very improbable */
152         if (stats.matches > (ULONG_MAX / 100))
153                 can_init_stats();
154
155         /* calc total values */
156         if (stats.rx_frames)
157                 stats.total_rx_match_ratio = (stats.matches * 100) /
158                                                 stats.rx_frames;
159
160         stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
161                                         stats.tx_frames);
162         stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
163                                         stats.rx_frames);
164
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;
169
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);
172
173         /* check / update maximum values */
174         if (stats.max_tx_rate < stats.current_tx_rate)
175                 stats.max_tx_rate = stats.current_tx_rate;
176
177         if (stats.max_rx_rate < stats.current_rx_rate)
178                 stats.max_rx_rate = stats.current_rx_rate;
179
180         if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
181                 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
182
183         /* clear values for 'current rate' calculation */
184         stats.tx_frames_delta = 0;
185         stats.rx_frames_delta = 0;
186         stats.matches_delta   = 0;
187
188         /* restart timer (one second) */
189         stattimer.expires = jiffies + HZ;
190         add_timer(&stattimer);
191 }
192
193 /*
194  * proc read functions
195  *
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.
198  *
199  */
200
201 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
202                              struct net_device *dev)
203 {
204         struct receiver *r;
205         struct hlist_node *n;
206
207         rcu_read_lock();
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";
212
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);
217
218                 /* does a typical line fit into the current buffer? */
219
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,
224                                         "   (..)\n");
225                         break;
226                 }
227         }
228         rcu_read_unlock();
229
230         return len;
231 }
232
233 static int can_print_recv_banner(char *page, int len)
234 {
235         /*
236          *                  can1.  00000000  00000000  00000000
237          *                 .......          0  tp20
238          */
239         len += snprintf(page + len, PAGE_SIZE - len,
240                         "  device   can_id   can_mask  function"
241                         "  userdata   matches  ident\n");
242
243         return len;
244 }
245
246 static int can_proc_read_stats(char *page, char **start, off_t off,
247                                int count, int *eof, void *data)
248 {
249         int len = 0;
250
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);
258
259         len += snprintf(page + len, PAGE_SIZE - len, "\n");
260
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);
265
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);
272
273                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
274
275                 len += snprintf(page + len, PAGE_SIZE - len,
276                                 " %8ld %% current match ratio (CRXMR)\n",
277                                 stats.current_rx_match_ratio);
278
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);
285
286                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
287
288                 len += snprintf(page + len, PAGE_SIZE - len,
289                                 " %8ld %% max match ratio (MRXMR)\n",
290                                 stats.max_rx_match_ratio);
291
292                 len += snprintf(page + len, PAGE_SIZE - len,
293                                 " %8ld frames/s max tx rate (MTXR)\n",
294                                 stats.max_tx_rate);
295                 len += snprintf(page + len, PAGE_SIZE - len,
296                                 " %8ld frames/s max rx rate (MRXR)\n",
297                                 stats.max_rx_rate);
298
299                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
300         }
301
302         len += snprintf(page + len, PAGE_SIZE - len,
303                         " %8ld current receive list entries (CRCV)\n",
304                         pstats.rcv_entries);
305         len += snprintf(page + len, PAGE_SIZE - len,
306                         " %8ld maximum receive list entries (MRCV)\n",
307                         pstats.rcv_entries_max);
308
309         if (pstats.stats_reset)
310                 len += snprintf(page + len, PAGE_SIZE - len,
311                                 "\n %8ld statistic resets (STR)\n",
312                                 pstats.stats_reset);
313
314         if (pstats.user_reset)
315                 len += snprintf(page + len, PAGE_SIZE - len,
316                                 " %8ld user statistic resets (USTR)\n",
317                                 pstats.user_reset);
318
319         len += snprintf(page + len, PAGE_SIZE - len, "\n");
320
321         *eof = 1;
322         return len;
323 }
324
325 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
326                                      int count, int *eof, void *data)
327 {
328         int len = 0;
329
330         user_reset = 1;
331
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);
336
337         } else {
338                 if (stats.jiffies_init != jiffies)
339                         can_init_stats();
340
341                 len += snprintf(page + len, PAGE_SIZE - len,
342                                 "Performed statistic reset #%ld.\n",
343                                 pstats.stats_reset);
344         }
345
346         *eof = 1;
347         return len;
348 }
349
350 static int can_proc_read_version(char *page, char **start, off_t off,
351                                  int count, int *eof, void *data)
352 {
353         int len = 0;
354
355         len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
356                         CAN_VERSION_STRING);
357         *eof = 1;
358         return len;
359 }
360
361 static int can_proc_read_rcvlist(char *page, char **start, off_t off,
362                                  int count, int *eof, void *data)
363 {
364         int idx = (int)data;
365         int len = 0;
366         struct dev_rcv_lists *d;
367         struct hlist_node *n;
368
369         len += snprintf(page + len, PAGE_SIZE - len,
370                         "\nreceive list '%s':\n", rx_list_name[idx]);
371
372         rcu_read_lock();
373         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
374
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);
378                 } else
379                         len += snprintf(page + len, PAGE_SIZE - len,
380                                         "  (%s: no entry)\n", DNAME(d->dev));
381
382                 /* exit on end of buffer? */
383                 if (len > PAGE_SIZE - 100)
384                         break;
385         }
386         rcu_read_unlock();
387
388         len += snprintf(page + len, PAGE_SIZE - len, "\n");
389
390         *eof = 1;
391         return len;
392 }
393
394 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
395                                      int count, int *eof, void *data)
396 {
397         int len = 0;
398         struct dev_rcv_lists *d;
399         struct hlist_node *n;
400
401         /* RX_SFF */
402         len += snprintf(page + len, PAGE_SIZE - len,
403                         "\nreceive list 'rx_sff':\n");
404
405         rcu_read_lock();
406         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
407                 int i, all_empty = 1;
408                 /* check wether at least one list is non-empty */
409                 for (i = 0; i < 0x800; i++)
410                         if (!hlist_empty(&d->rx_sff[i])) {
411                                 all_empty = 0;
412                                 break;
413                         }
414
415                 if (!all_empty) {
416                         len = can_print_recv_banner(page, len);
417                         for (i = 0; i < 0x800; i++) {
418                                 if (!hlist_empty(&d->rx_sff[i]) &&
419                                     len < PAGE_SIZE - 100)
420                                         len = can_print_rcvlist(page, len,
421                                                                 &d->rx_sff[i],
422                                                                 d->dev);
423                         }
424                 } else
425                         len += snprintf(page + len, PAGE_SIZE - len,
426                                         "  (%s: no entry)\n", DNAME(d->dev));
427
428                 /* exit on end of buffer? */
429                 if (len > PAGE_SIZE - 100)
430                         break;
431         }
432         rcu_read_unlock();
433
434         len += snprintf(page + len, PAGE_SIZE - len, "\n");
435
436         *eof = 1;
437         return len;
438 }
439
440 /*
441  * proc utility functions
442  */
443
444 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
445                                                         mode_t mode,
446                                                         read_proc_t* read_proc,
447                                                         void *data)
448 {
449         if (can_dir)
450                 return create_proc_read_entry(name, mode, can_dir, read_proc,
451                                               data);
452         else
453                 return NULL;
454 }
455
456 static void can_remove_proc_readentry(const char *name)
457 {
458         if (can_dir)
459                 remove_proc_entry(name, can_dir);
460 }
461
462 /*
463  * can_init_proc - create main CAN proc directory and procfs entries
464  */
465 void can_init_proc(void)
466 {
467         /* create /proc/net/can directory */
468         can_dir = proc_mkdir(CAN_PROC_DIR, NULL);
469
470         if (!can_dir) {
471                 printk(KERN_INFO "can: failed to create /proc/%s . "
472                        "CONFIG_PROC_FS missing?\n", CAN_PROC_DIR);
473                 return;
474         }
475
476         can_dir->owner = THIS_MODULE;
477
478         /* own procfs entries from the AF_CAN core */
479         pde_version     = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
480                                         can_proc_read_version, NULL);
481         pde_stats       = can_create_proc_readentry(CAN_PROC_STATS, 0644,
482                                         can_proc_read_stats, NULL);
483         pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
484                                         can_proc_read_reset_stats, NULL);
485         pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
486                                         can_proc_read_rcvlist, (void*)RX_ERR);
487         pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
488                                         can_proc_read_rcvlist, (void*)RX_ALL);
489         pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
490                                         can_proc_read_rcvlist, (void*)RX_FIL);
491         pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
492                                         can_proc_read_rcvlist, (void*)RX_INV);
493         pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
494                                         can_proc_read_rcvlist, (void*)RX_EFF);
495         pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
496                                         can_proc_read_rcvlist_sff, NULL);
497 }
498
499 /*
500  * can_remove_proc - remove procfs entries and main CAN proc directory
501  */
502 void can_remove_proc(void)
503 {
504         if (pde_version)
505                 can_remove_proc_readentry(CAN_PROC_VERSION);
506
507         if (pde_stats)
508                 can_remove_proc_readentry(CAN_PROC_STATS);
509
510         if (pde_reset_stats)
511                 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
512
513         if (pde_rcvlist_err)
514                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
515
516         if (pde_rcvlist_all)
517                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
518
519         if (pde_rcvlist_fil)
520                 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
521
522         if (pde_rcvlist_inv)
523                 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
524
525         if (pde_rcvlist_eff)
526                 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
527
528         if (pde_rcvlist_sff)
529                 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
530
531         if (can_dir)
532                 remove_proc_entry(CAN_PROC_DIR, NULL);
533 }