]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/proc.c
round_jiffies() has been introduced in 2.6.20.
[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 and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43
44 #include <linux/module.h>
45 #include <linux/proc_fs.h>
46 #include <linux/list.h>
47 #include <linux/rcupdate.h>
48 #include <linux/can/core.h>
49
50 #include "af_can.h"
51
52 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
53 RCSID("$Id$");
54
55 /*
56  * proc filenames for the PF_CAN core
57  */
58
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"
68
69 static struct proc_dir_entry *can_dir;
70 static struct proc_dir_entry *pde_version;
71 static struct proc_dir_entry *pde_stats;
72 static struct proc_dir_entry *pde_reset_stats;
73 static struct proc_dir_entry *pde_rcvlist_all;
74 static struct proc_dir_entry *pde_rcvlist_fil;
75 static struct proc_dir_entry *pde_rcvlist_inv;
76 static struct proc_dir_entry *pde_rcvlist_sff;
77 static struct proc_dir_entry *pde_rcvlist_eff;
78 static struct proc_dir_entry *pde_rcvlist_err;
79
80 static int user_reset;
81
82 static const char rx_list_name[][8] = {
83         [RX_ERR] = "rx_err",
84         [RX_ALL] = "rx_all",
85         [RX_FIL] = "rx_fil",
86         [RX_INV] = "rx_inv",
87         [RX_EFF] = "rx_eff",
88 };
89
90 /*
91  * af_can statistics stuff
92  */
93
94 static void can_init_stats(void)
95 {
96         /*
97          * This memset function is called from a timer context (when
98          * stattimer is active which is the default) OR in a process
99          * context (reading the proc_fs when stattimer is disabled).
100          */
101         memset(&stats, 0, sizeof(stats));
102         stats.jiffies_init = jiffies;
103
104         pstats.stats_reset++;
105
106         if (user_reset) {
107                 user_reset = 0;
108                 pstats.user_reset++;
109         }
110 }
111
112 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
113                                unsigned long count)
114 {
115         unsigned long ret = 0;
116
117         if (oldjif == newjif)
118                 return 0;
119
120         /* see can_stat_update() - this should NEVER happen! */
121         if (count > (ULONG_MAX / HZ)) {
122                 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
123                        count);
124                 return 99999999;
125         }
126
127         ret = (count * HZ) / (newjif - oldjif);
128
129         return ret;
130 }
131
132 void can_stat_update(unsigned long data)
133 {
134         unsigned long j = jiffies; /* snapshot */
135
136         /* restart counting in timer context on user request */
137         if (user_reset)
138                 can_init_stats();
139
140         /* restart counting on jiffies overflow */
141         if (j < stats.jiffies_init)
142                 can_init_stats();
143
144         /* prevent overflow in calc_rate() */
145         if (stats.rx_frames > (ULONG_MAX / HZ))
146                 can_init_stats();
147
148         /* prevent overflow in calc_rate() */
149         if (stats.tx_frames > (ULONG_MAX / HZ))
150                 can_init_stats();
151
152         /* matches overflow - very improbable */
153         if (stats.matches > (ULONG_MAX / 100))
154                 can_init_stats();
155
156         /* calc total values */
157         if (stats.rx_frames)
158                 stats.total_rx_match_ratio = (stats.matches * 100) /
159                                                 stats.rx_frames;
160
161         stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
162                                         stats.tx_frames);
163         stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
164                                         stats.rx_frames);
165
166         /* calc current values */
167         if (stats.rx_frames_delta)
168                 stats.current_rx_match_ratio =
169                         (stats.matches_delta * 100) / stats.rx_frames_delta;
170
171         stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
172         stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
173
174         /* check / update maximum values */
175         if (stats.max_tx_rate < stats.current_tx_rate)
176                 stats.max_tx_rate = stats.current_tx_rate;
177
178         if (stats.max_rx_rate < stats.current_rx_rate)
179                 stats.max_rx_rate = stats.current_rx_rate;
180
181         if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
182                 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
183
184         /* clear values for 'current rate' calculation */
185         stats.tx_frames_delta = 0;
186         stats.rx_frames_delta = 0;
187         stats.matches_delta   = 0;
188
189         /* restart timer (one second) */
190 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
191         stattimer.expires = round_jiffies(jiffies + HZ);
192 #else
193         stattimer.expires = jiffies + HZ;
194 #endif
195         add_timer(&stattimer);
196 }
197
198 /*
199  * proc read functions
200  *
201  * From known use-cases we expect about 10 entries in a receive list to be
202  * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
203  *
204  */
205
206 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
207                              struct net_device *dev)
208 {
209         struct receiver *r;
210         struct hlist_node *n;
211
212         rcu_read_lock();
213         hlist_for_each_entry_rcu(r, n, rx_list, list) {
214                 char *fmt = (r->can_id & CAN_EFF_FLAG)?
215                         "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
216                         "   %-5s     %03X    %08x  %08lx  %08lx  %8ld  %s\n";
217
218                 len += snprintf(page + len, PAGE_SIZE - len, fmt,
219                                 DNAME(dev), r->can_id, r->mask,
220                                 (unsigned long)r->func, (unsigned long)r->data,
221                                 r->matches, r->ident);
222
223                 /* does a typical line fit into the current buffer? */
224
225                 /* 100 Bytes before end of buffer */
226                 if (len > PAGE_SIZE - 100) {
227                         /* mark output cut off */
228                         len += snprintf(page + len, PAGE_SIZE - len,
229                                         "   (..)\n");
230                         break;
231                 }
232         }
233         rcu_read_unlock();
234
235         return len;
236 }
237
238 static int can_print_recv_banner(char *page, int len)
239 {
240         /*
241          *                  can1.  00000000  00000000  00000000
242          *                 .......          0  tp20
243          */
244         len += snprintf(page + len, PAGE_SIZE - len,
245                         "  device   can_id   can_mask  function"
246                         "  userdata   matches  ident\n");
247
248         return len;
249 }
250
251 static int can_proc_read_stats(char *page, char **start, off_t off,
252                                int count, int *eof, void *data)
253 {
254         int len = 0;
255
256         len += snprintf(page + len, PAGE_SIZE - len, "\n");
257         len += snprintf(page + len, PAGE_SIZE - len,
258                         " %8ld transmitted frames (TXF)\n", stats.tx_frames);
259         len += snprintf(page + len, PAGE_SIZE - len,
260                         " %8ld received frames (RXF)\n", stats.rx_frames);
261         len += snprintf(page + len, PAGE_SIZE - len,
262                         " %8ld matched frames (RXMF)\n", stats.matches);
263
264         len += snprintf(page + len, PAGE_SIZE - len, "\n");
265
266         if (stattimer.function == can_stat_update) {
267                 len += snprintf(page + len, PAGE_SIZE - len,
268                                 " %8ld %% total match ratio (RXMR)\n",
269                                 stats.total_rx_match_ratio);
270
271                 len += snprintf(page + len, PAGE_SIZE - len,
272                                 " %8ld frames/s total tx rate (TXR)\n",
273                                 stats.total_tx_rate);
274                 len += snprintf(page + len, PAGE_SIZE - len,
275                                 " %8ld frames/s total rx rate (RXR)\n",
276                                 stats.total_rx_rate);
277
278                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
279
280                 len += snprintf(page + len, PAGE_SIZE - len,
281                                 " %8ld %% current match ratio (CRXMR)\n",
282                                 stats.current_rx_match_ratio);
283
284                 len += snprintf(page + len, PAGE_SIZE - len,
285                                 " %8ld frames/s current tx rate (CTXR)\n",
286                                 stats.current_tx_rate);
287                 len += snprintf(page + len, PAGE_SIZE - len,
288                                 " %8ld frames/s current rx rate (CRXR)\n",
289                                 stats.current_rx_rate);
290
291                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
292
293                 len += snprintf(page + len, PAGE_SIZE - len,
294                                 " %8ld %% max match ratio (MRXMR)\n",
295                                 stats.max_rx_match_ratio);
296
297                 len += snprintf(page + len, PAGE_SIZE - len,
298                                 " %8ld frames/s max tx rate (MTXR)\n",
299                                 stats.max_tx_rate);
300                 len += snprintf(page + len, PAGE_SIZE - len,
301                                 " %8ld frames/s max rx rate (MRXR)\n",
302                                 stats.max_rx_rate);
303
304                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
305         }
306
307         len += snprintf(page + len, PAGE_SIZE - len,
308                         " %8ld current receive list entries (CRCV)\n",
309                         pstats.rcv_entries);
310         len += snprintf(page + len, PAGE_SIZE - len,
311                         " %8ld maximum receive list entries (MRCV)\n",
312                         pstats.rcv_entries_max);
313
314         if (pstats.stats_reset)
315                 len += snprintf(page + len, PAGE_SIZE - len,
316                                 "\n %8ld statistic resets (STR)\n",
317                                 pstats.stats_reset);
318
319         if (pstats.user_reset)
320                 len += snprintf(page + len, PAGE_SIZE - len,
321                                 " %8ld user statistic resets (USTR)\n",
322                                 pstats.user_reset);
323
324         len += snprintf(page + len, PAGE_SIZE - len, "\n");
325
326         *eof = 1;
327         return len;
328 }
329
330 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
331                                      int count, int *eof, void *data)
332 {
333         int len = 0;
334
335         user_reset = 1;
336
337         if (stattimer.function == can_stat_update) {
338                 len += snprintf(page + len, PAGE_SIZE - len,
339                                 "Scheduled statistic reset #%ld.\n",
340                                 pstats.stats_reset + 1);
341
342         } else {
343                 if (stats.jiffies_init != jiffies)
344                         can_init_stats();
345
346                 len += snprintf(page + len, PAGE_SIZE - len,
347                                 "Performed statistic reset #%ld.\n",
348                                 pstats.stats_reset);
349         }
350
351         *eof = 1;
352         return len;
353 }
354
355 static int can_proc_read_version(char *page, char **start, off_t off,
356                                  int count, int *eof, void *data)
357 {
358         int len = 0;
359
360         len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
361                         CAN_VERSION_STRING);
362         *eof = 1;
363         return len;
364 }
365
366 static int can_proc_read_rcvlist(char *page, char **start, off_t off,
367                                  int count, int *eof, void *data)
368 {
369         /* double cast to prevent GCC warning */
370         int idx = (int)(long)data;
371         int len = 0;
372         struct dev_rcv_lists *d;
373         struct hlist_node *n;
374
375         len += snprintf(page + len, PAGE_SIZE - len,
376                         "\nreceive list '%s':\n", rx_list_name[idx]);
377
378         rcu_read_lock();
379         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
380
381                 if (!hlist_empty(&d->rx[idx])) {
382                         len = can_print_recv_banner(page, len);
383                         len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
384                 } else
385                         len += snprintf(page + len, PAGE_SIZE - len,
386                                         "  (%s: no entry)\n", DNAME(d->dev));
387
388                 /* exit on end of buffer? */
389                 if (len > PAGE_SIZE - 100)
390                         break;
391         }
392         rcu_read_unlock();
393
394         len += snprintf(page + len, PAGE_SIZE - len, "\n");
395
396         *eof = 1;
397         return len;
398 }
399
400 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
401                                      int count, int *eof, void *data)
402 {
403         int len = 0;
404         struct dev_rcv_lists *d;
405         struct hlist_node *n;
406
407         /* RX_SFF */
408         len += snprintf(page + len, PAGE_SIZE - len,
409                         "\nreceive list 'rx_sff':\n");
410
411         rcu_read_lock();
412         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
413                 int i, all_empty = 1;
414                 /* check wether at least one list is non-empty */
415                 for (i = 0; i < 0x800; i++)
416                         if (!hlist_empty(&d->rx_sff[i])) {
417                                 all_empty = 0;
418                                 break;
419                         }
420
421                 if (!all_empty) {
422                         len = can_print_recv_banner(page, len);
423                         for (i = 0; i < 0x800; i++) {
424                                 if (!hlist_empty(&d->rx_sff[i]) &&
425                                     len < PAGE_SIZE - 100)
426                                         len = can_print_rcvlist(page, len,
427                                                                 &d->rx_sff[i],
428                                                                 d->dev);
429                         }
430                 } else
431                         len += snprintf(page + len, PAGE_SIZE - len,
432                                         "  (%s: no entry)\n", DNAME(d->dev));
433
434                 /* exit on end of buffer? */
435                 if (len > PAGE_SIZE - 100)
436                         break;
437         }
438         rcu_read_unlock();
439
440         len += snprintf(page + len, PAGE_SIZE - len, "\n");
441
442         *eof = 1;
443         return len;
444 }
445
446 /*
447  * proc utility functions
448  */
449
450 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
451                                                         mode_t mode,
452                                                         read_proc_t *read_proc,
453                                                         void *data)
454 {
455         if (can_dir)
456                 return create_proc_read_entry(name, mode, can_dir, read_proc,
457                                               data);
458         else
459                 return NULL;
460 }
461
462 static void can_remove_proc_readentry(const char *name)
463 {
464         if (can_dir)
465                 remove_proc_entry(name, can_dir);
466 }
467
468 /*
469  * can_init_proc - create main CAN proc directory and procfs entries
470  */
471 void can_init_proc(void)
472 {
473         /* create /proc/net/can directory */
474 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
475         can_dir = proc_mkdir("can", init_net.proc_net);
476 #else
477         can_dir = proc_mkdir("can", proc_net);
478 #endif
479
480         if (!can_dir) {
481                 printk(KERN_INFO "can: failed to create /proc/net/can . "
482                        "CONFIG_PROC_FS missing?\n");
483                 return;
484         }
485
486         can_dir->owner = THIS_MODULE;
487
488         /* own procfs entries from the AF_CAN core */
489         pde_version     = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
490                                         can_proc_read_version, NULL);
491         pde_stats       = can_create_proc_readentry(CAN_PROC_STATS, 0644,
492                                         can_proc_read_stats, NULL);
493         pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
494                                         can_proc_read_reset_stats, NULL);
495         pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
496                                         can_proc_read_rcvlist, (void *)RX_ERR);
497         pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
498                                         can_proc_read_rcvlist, (void *)RX_ALL);
499         pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
500                                         can_proc_read_rcvlist, (void *)RX_FIL);
501         pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
502                                         can_proc_read_rcvlist, (void *)RX_INV);
503         pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
504                                         can_proc_read_rcvlist, (void *)RX_EFF);
505         pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
506                                         can_proc_read_rcvlist_sff, NULL);
507 }
508
509 /*
510  * can_remove_proc - remove procfs entries and main CAN proc directory
511  */
512 void can_remove_proc(void)
513 {
514         if (pde_version)
515                 can_remove_proc_readentry(CAN_PROC_VERSION);
516
517         if (pde_stats)
518                 can_remove_proc_readentry(CAN_PROC_STATS);
519
520         if (pde_reset_stats)
521                 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
522
523         if (pde_rcvlist_err)
524                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
525
526         if (pde_rcvlist_all)
527                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
528
529         if (pde_rcvlist_fil)
530                 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
531
532         if (pde_rcvlist_inv)
533                 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
534
535         if (pde_rcvlist_eff)
536                 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
537
538         if (pde_rcvlist_sff)
539                 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
540
541         if (can_dir)
542 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
543                 proc_net_remove(&init_net, "can");
544 #else
545                 proc_net_remove("can");
546 #endif
547 }