]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/proc.c
Added missing overflow check for stats.tx_frames.
[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;
71 static struct proc_dir_entry *pde_version;
72 static struct proc_dir_entry *pde_stats;
73 static struct proc_dir_entry *pde_reset_stats;
74 static struct proc_dir_entry *pde_rcvlist_all;
75 static struct proc_dir_entry *pde_rcvlist_fil;
76 static struct proc_dir_entry *pde_rcvlist_inv;
77 static struct proc_dir_entry *pde_rcvlist_sff;
78 static struct proc_dir_entry *pde_rcvlist_eff;
79 static struct proc_dir_entry *pde_rcvlist_err;
80
81 static int user_reset;
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_stat_update() - 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         /* prevent overflow in calc_rate() */
146         if (stats.rx_frames > (ULONG_MAX / HZ))
147                 can_init_stats();
148
149         /* prevent overflow in calc_rate() */
150         if (stats.tx_frames > (ULONG_MAX / HZ))
151                 can_init_stats();
152
153         /* matches overflow - very improbable */
154         if (stats.matches > (ULONG_MAX / 100))
155                 can_init_stats();
156
157         /* calc total values */
158         if (stats.rx_frames)
159                 stats.total_rx_match_ratio = (stats.matches * 100) /
160                                                 stats.rx_frames;
161
162         stats.total_tx_rate = calc_rate(stats.jiffies_init, j,
163                                         stats.tx_frames);
164         stats.total_rx_rate = calc_rate(stats.jiffies_init, j,
165                                         stats.rx_frames);
166
167         /* calc current values */
168         if (stats.rx_frames_delta)
169                 stats.current_rx_match_ratio =
170                         (stats.matches_delta * 100) / stats.rx_frames_delta;
171
172         stats.current_tx_rate = calc_rate(0, HZ, stats.tx_frames_delta);
173         stats.current_rx_rate = calc_rate(0, HZ, stats.rx_frames_delta);
174
175         /* check / update maximum values */
176         if (stats.max_tx_rate < stats.current_tx_rate)
177                 stats.max_tx_rate = stats.current_tx_rate;
178
179         if (stats.max_rx_rate < stats.current_rx_rate)
180                 stats.max_rx_rate = stats.current_rx_rate;
181
182         if (stats.max_rx_match_ratio < stats.current_rx_match_ratio)
183                 stats.max_rx_match_ratio = stats.current_rx_match_ratio;
184
185         /* clear values for 'current rate' calculation */
186         stats.tx_frames_delta = 0;
187         stats.rx_frames_delta = 0;
188         stats.matches_delta   = 0;
189
190         /* restart timer (one second) */
191         stattimer.expires = round_jiffies(jiffies + HZ);
192         add_timer(&stattimer);
193 }
194
195 /*
196  * proc read functions
197  *
198  * From known use-cases we expect about 10 entries in a receive list to be
199  * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
200  *
201  */
202
203 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
204                              struct net_device *dev)
205 {
206         struct receiver *r;
207         struct hlist_node *n;
208
209         rcu_read_lock();
210         hlist_for_each_entry_rcu(r, n, rx_list, list) {
211                 char *fmt = (r->can_id & CAN_EFF_FLAG)?
212                         "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
213                         "   %-5s     %03X    %08x  %08lx  %08lx  %8ld  %s\n";
214
215                 len += snprintf(page + len, PAGE_SIZE - len, fmt,
216                                 DNAME(dev), r->can_id, r->mask,
217                                 (unsigned long)r->func, (unsigned long)r->data,
218                                 r->matches, r->ident);
219
220                 /* does a typical line fit into the current buffer? */
221
222                 /* 100 Bytes before end of buffer */
223                 if (len > PAGE_SIZE - 100) {
224                         /* mark output cut off */
225                         len += snprintf(page + len, PAGE_SIZE - len,
226                                         "   (..)\n");
227                         break;
228                 }
229         }
230         rcu_read_unlock();
231
232         return len;
233 }
234
235 static int can_print_recv_banner(char *page, int len)
236 {
237         /*
238          *                  can1.  00000000  00000000  00000000
239          *                 .......          0  tp20
240          */
241         len += snprintf(page + len, PAGE_SIZE - len,
242                         "  device   can_id   can_mask  function"
243                         "  userdata   matches  ident\n");
244
245         return len;
246 }
247
248 static int can_proc_read_stats(char *page, char **start, off_t off,
249                                int count, int *eof, void *data)
250 {
251         int len = 0;
252
253         len += snprintf(page + len, PAGE_SIZE - len, "\n");
254         len += snprintf(page + len, PAGE_SIZE - len,
255                         " %8ld transmitted frames (TXF)\n", stats.tx_frames);
256         len += snprintf(page + len, PAGE_SIZE - len,
257                         " %8ld received frames (RXF)\n", stats.rx_frames);
258         len += snprintf(page + len, PAGE_SIZE - len,
259                         " %8ld matched frames (RXMF)\n", stats.matches);
260
261         len += snprintf(page + len, PAGE_SIZE - len, "\n");
262
263         if (stattimer.function == can_stat_update) {
264                 len += snprintf(page + len, PAGE_SIZE - len,
265                                 " %8ld %% total match ratio (RXMR)\n",
266                                 stats.total_rx_match_ratio);
267
268                 len += snprintf(page + len, PAGE_SIZE - len,
269                                 " %8ld frames/s total tx rate (TXR)\n",
270                                 stats.total_tx_rate);
271                 len += snprintf(page + len, PAGE_SIZE - len,
272                                 " %8ld frames/s total rx rate (RXR)\n",
273                                 stats.total_rx_rate);
274
275                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
276
277                 len += snprintf(page + len, PAGE_SIZE - len,
278                                 " %8ld %% current match ratio (CRXMR)\n",
279                                 stats.current_rx_match_ratio);
280
281                 len += snprintf(page + len, PAGE_SIZE - len,
282                                 " %8ld frames/s current tx rate (CTXR)\n",
283                                 stats.current_tx_rate);
284                 len += snprintf(page + len, PAGE_SIZE - len,
285                                 " %8ld frames/s current rx rate (CRXR)\n",
286                                 stats.current_rx_rate);
287
288                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
289
290                 len += snprintf(page + len, PAGE_SIZE - len,
291                                 " %8ld %% max match ratio (MRXMR)\n",
292                                 stats.max_rx_match_ratio);
293
294                 len += snprintf(page + len, PAGE_SIZE - len,
295                                 " %8ld frames/s max tx rate (MTXR)\n",
296                                 stats.max_tx_rate);
297                 len += snprintf(page + len, PAGE_SIZE - len,
298                                 " %8ld frames/s max rx rate (MRXR)\n",
299                                 stats.max_rx_rate);
300
301                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
302         }
303
304         len += snprintf(page + len, PAGE_SIZE - len,
305                         " %8ld current receive list entries (CRCV)\n",
306                         pstats.rcv_entries);
307         len += snprintf(page + len, PAGE_SIZE - len,
308                         " %8ld maximum receive list entries (MRCV)\n",
309                         pstats.rcv_entries_max);
310
311         if (pstats.stats_reset)
312                 len += snprintf(page + len, PAGE_SIZE - len,
313                                 "\n %8ld statistic resets (STR)\n",
314                                 pstats.stats_reset);
315
316         if (pstats.user_reset)
317                 len += snprintf(page + len, PAGE_SIZE - len,
318                                 " %8ld user statistic resets (USTR)\n",
319                                 pstats.user_reset);
320
321         len += snprintf(page + len, PAGE_SIZE - len, "\n");
322
323         *eof = 1;
324         return len;
325 }
326
327 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
328                                      int count, int *eof, void *data)
329 {
330         int len = 0;
331
332         user_reset = 1;
333
334         if (stattimer.function == can_stat_update) {
335                 len += snprintf(page + len, PAGE_SIZE - len,
336                                 "Scheduled statistic reset #%ld.\n",
337                                 pstats.stats_reset + 1);
338
339         } else {
340                 if (stats.jiffies_init != jiffies)
341                         can_init_stats();
342
343                 len += snprintf(page + len, PAGE_SIZE - len,
344                                 "Performed statistic reset #%ld.\n",
345                                 pstats.stats_reset);
346         }
347
348         *eof = 1;
349         return len;
350 }
351
352 static int can_proc_read_version(char *page, char **start, off_t off,
353                                  int count, int *eof, void *data)
354 {
355         int len = 0;
356
357         len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
358                         CAN_VERSION_STRING);
359         *eof = 1;
360         return len;
361 }
362
363 static int can_proc_read_rcvlist(char *page, char **start, off_t off,
364                                  int count, int *eof, void *data)
365 {
366         /* double cast to prevent GCC warning */
367         int idx = (int)(long)data;
368         int len = 0;
369         struct dev_rcv_lists *d;
370         struct hlist_node *n;
371
372         len += snprintf(page + len, PAGE_SIZE - len,
373                         "\nreceive list '%s':\n", rx_list_name[idx]);
374
375         rcu_read_lock();
376         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
377
378                 if (!hlist_empty(&d->rx[idx])) {
379                         len = can_print_recv_banner(page, len);
380                         len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
381                 } else
382                         len += snprintf(page + len, PAGE_SIZE - len,
383                                         "  (%s: no entry)\n", DNAME(d->dev));
384
385                 /* exit on end of buffer? */
386                 if (len > PAGE_SIZE - 100)
387                         break;
388         }
389         rcu_read_unlock();
390
391         len += snprintf(page + len, PAGE_SIZE - len, "\n");
392
393         *eof = 1;
394         return len;
395 }
396
397 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
398                                      int count, int *eof, void *data)
399 {
400         int len = 0;
401         struct dev_rcv_lists *d;
402         struct hlist_node *n;
403
404         /* RX_SFF */
405         len += snprintf(page + len, PAGE_SIZE - len,
406                         "\nreceive list 'rx_sff':\n");
407
408         rcu_read_lock();
409         hlist_for_each_entry_rcu(d, n, &rx_dev_list, list) {
410                 int i, all_empty = 1;
411                 /* check wether at least one list is non-empty */
412                 for (i = 0; i < 0x800; i++)
413                         if (!hlist_empty(&d->rx_sff[i])) {
414                                 all_empty = 0;
415                                 break;
416                         }
417
418                 if (!all_empty) {
419                         len = can_print_recv_banner(page, len);
420                         for (i = 0; i < 0x800; i++) {
421                                 if (!hlist_empty(&d->rx_sff[i]) &&
422                                     len < PAGE_SIZE - 100)
423                                         len = can_print_rcvlist(page, len,
424                                                                 &d->rx_sff[i],
425                                                                 d->dev);
426                         }
427                 } else
428                         len += snprintf(page + len, PAGE_SIZE - len,
429                                         "  (%s: no entry)\n", DNAME(d->dev));
430
431                 /* exit on end of buffer? */
432                 if (len > PAGE_SIZE - 100)
433                         break;
434         }
435         rcu_read_unlock();
436
437         len += snprintf(page + len, PAGE_SIZE - len, "\n");
438
439         *eof = 1;
440         return len;
441 }
442
443 /*
444  * proc utility functions
445  */
446
447 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
448                                                         mode_t mode,
449                                                         read_proc_t *read_proc,
450                                                         void *data)
451 {
452         if (can_dir)
453                 return create_proc_read_entry(name, mode, can_dir, read_proc,
454                                               data);
455         else
456                 return NULL;
457 }
458
459 static void can_remove_proc_readentry(const char *name)
460 {
461         if (can_dir)
462                 remove_proc_entry(name, can_dir);
463 }
464
465 /*
466  * can_init_proc - create main CAN proc directory and procfs entries
467  */
468 void can_init_proc(void)
469 {
470         /* create /proc/net/can directory */
471 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
472         can_dir = proc_mkdir("can", init_net.proc_net);
473 #else
474         can_dir = proc_mkdir("can", proc_net);
475 #endif
476
477         if (!can_dir) {
478                 printk(KERN_INFO "can: failed to create /proc/net/can . "
479                        "CONFIG_PROC_FS missing?\n");
480                 return;
481         }
482
483         can_dir->owner = THIS_MODULE;
484
485         /* own procfs entries from the AF_CAN core */
486         pde_version     = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
487                                         can_proc_read_version, NULL);
488         pde_stats       = can_create_proc_readentry(CAN_PROC_STATS, 0644,
489                                         can_proc_read_stats, NULL);
490         pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
491                                         can_proc_read_reset_stats, NULL);
492         pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
493                                         can_proc_read_rcvlist, (void *)RX_ERR);
494         pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
495                                         can_proc_read_rcvlist, (void *)RX_ALL);
496         pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
497                                         can_proc_read_rcvlist, (void *)RX_FIL);
498         pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
499                                         can_proc_read_rcvlist, (void *)RX_INV);
500         pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
501                                         can_proc_read_rcvlist, (void *)RX_EFF);
502         pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
503                                         can_proc_read_rcvlist_sff, NULL);
504 }
505
506 /*
507  * can_remove_proc - remove procfs entries and main CAN proc directory
508  */
509 void can_remove_proc(void)
510 {
511         if (pde_version)
512                 can_remove_proc_readentry(CAN_PROC_VERSION);
513
514         if (pde_stats)
515                 can_remove_proc_readentry(CAN_PROC_STATS);
516
517         if (pde_reset_stats)
518                 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
519
520         if (pde_rcvlist_err)
521                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
522
523         if (pde_rcvlist_all)
524                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
525
526         if (pde_rcvlist_fil)
527                 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
528
529         if (pde_rcvlist_inv)
530                 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
531
532         if (pde_rcvlist_eff)
533                 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
534
535         if (pde_rcvlist_sff)
536                 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
537
538         if (can_dir)
539 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
540                 proc_net_remove(&init_net, "can");
541 #else
542                 proc_net_remove("can");
543 #endif
544 }