xref: /linux/net/can/proc.c (revision 46eee1661aa9b49966e6c43d07126fe408edda57)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /*
3  * proc.c - procfs support for Protocol family CAN core module
4  *
5  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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, in which case the provisions of the
23  * GPL apply INSTEAD OF those given above.
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  */
42 
43 #include <linux/module.h>
44 #include <linux/proc_fs.h>
45 #include <linux/list.h>
46 #include <linux/rcupdate.h>
47 #include <linux/if_arp.h>
48 #include <linux/can/can-ml.h>
49 #include <linux/can/core.h>
50 
51 #include "af_can.h"
52 
53 /*
54  * proc filenames for the PF_CAN core
55  */
56 
57 #define CAN_PROC_STATS       "stats"
58 #define CAN_PROC_RESET_STATS "reset_stats"
59 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
60 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
61 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
62 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
63 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
64 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
65 
66 static int user_reset;
67 
68 static const char rx_list_name[][8] = {
69 	[RX_ERR] = "rx_err",
70 	[RX_ALL] = "rx_all",
71 	[RX_FIL] = "rx_fil",
72 	[RX_INV] = "rx_inv",
73 };
74 
75 /*
76  * af_can statistics stuff
77  */
78 
79 static void can_init_stats(struct net *net)
80 {
81 	struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
82 	struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
83 	/*
84 	 * This memset function is called from a timer context (when
85 	 * can_stattimer is active which is the default) OR in a process
86 	 * context (reading the proc_fs when can_stattimer is disabled).
87 	 */
88 	memset(pkg_stats, 0, sizeof(struct can_pkg_stats));
89 	pkg_stats->jiffies_init = jiffies;
90 
91 	rcv_lists_stats->stats_reset++;
92 
93 	if (user_reset) {
94 		user_reset = 0;
95 		rcv_lists_stats->user_reset++;
96 	}
97 }
98 
99 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
100 			       unsigned long count)
101 {
102 	if (oldjif == newjif)
103 		return 0;
104 
105 	/* see can_stat_update() - this should NEVER happen! */
106 	if (count > (ULONG_MAX / HZ)) {
107 		printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
108 		       count);
109 		return 99999999;
110 	}
111 
112 	return (count * HZ) / (newjif - oldjif);
113 }
114 
115 void can_stat_update(struct timer_list *t)
116 {
117 	struct net *net = timer_container_of(net, t, can.stattimer);
118 	struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
119 	unsigned long j = jiffies; /* snapshot */
120 
121 	long rx_frames = atomic_long_read(&pkg_stats->rx_frames);
122 	long tx_frames = atomic_long_read(&pkg_stats->tx_frames);
123 	long matches = atomic_long_read(&pkg_stats->matches);
124 	long rx_frames_delta = atomic_long_read(&pkg_stats->rx_frames_delta);
125 	long tx_frames_delta = atomic_long_read(&pkg_stats->tx_frames_delta);
126 	long matches_delta = atomic_long_read(&pkg_stats->matches_delta);
127 
128 	/* restart counting in timer context on user request */
129 	if (user_reset)
130 		can_init_stats(net);
131 
132 	/* restart counting on jiffies overflow */
133 	if (j < pkg_stats->jiffies_init)
134 		can_init_stats(net);
135 
136 	/* prevent overflow in calc_rate() */
137 	if (rx_frames > (LONG_MAX / HZ))
138 		can_init_stats(net);
139 
140 	/* prevent overflow in calc_rate() */
141 	if (tx_frames > (LONG_MAX / HZ))
142 		can_init_stats(net);
143 
144 	/* matches overflow - very improbable */
145 	if (matches > (LONG_MAX / 100))
146 		can_init_stats(net);
147 
148 	/* calc total values */
149 	if (rx_frames)
150 		pkg_stats->total_rx_match_ratio = (matches * 100) / rx_frames;
151 
152 	pkg_stats->total_tx_rate = calc_rate(pkg_stats->jiffies_init, j,
153 					    tx_frames);
154 	pkg_stats->total_rx_rate = calc_rate(pkg_stats->jiffies_init, j,
155 					    rx_frames);
156 
157 	/* calc current values */
158 	if (rx_frames_delta)
159 		pkg_stats->current_rx_match_ratio =
160 			(matches_delta * 100) /	rx_frames_delta;
161 
162 	pkg_stats->current_tx_rate = calc_rate(0, HZ, tx_frames_delta);
163 	pkg_stats->current_rx_rate = calc_rate(0, HZ, rx_frames_delta);
164 
165 	/* check / update maximum values */
166 	if (pkg_stats->max_tx_rate < pkg_stats->current_tx_rate)
167 		pkg_stats->max_tx_rate = pkg_stats->current_tx_rate;
168 
169 	if (pkg_stats->max_rx_rate < pkg_stats->current_rx_rate)
170 		pkg_stats->max_rx_rate = pkg_stats->current_rx_rate;
171 
172 	if (pkg_stats->max_rx_match_ratio < pkg_stats->current_rx_match_ratio)
173 		pkg_stats->max_rx_match_ratio = pkg_stats->current_rx_match_ratio;
174 
175 	/* clear values for 'current rate' calculation */
176 	atomic_long_set(&pkg_stats->tx_frames_delta, 0);
177 	atomic_long_set(&pkg_stats->rx_frames_delta, 0);
178 	atomic_long_set(&pkg_stats->matches_delta, 0);
179 
180 	/* restart timer (one second) */
181 	mod_timer(&net->can.stattimer, round_jiffies(jiffies + HZ));
182 }
183 
184 /*
185  * proc read functions
186  */
187 
188 static void can_print_rcvlist(struct seq_file *m, struct hlist_head *rx_list,
189 			      struct net_device *dev)
190 {
191 	struct receiver *r;
192 
193 	hlist_for_each_entry_rcu(r, rx_list, list) {
194 		char *fmt = (r->can_id & CAN_EFF_FLAG)?
195 			"   %-5s  %08x  %08x  %pK  %pK  %8ld  %s\n" :
196 			"   %-5s     %03x    %08x  %pK  %pK  %8ld  %s\n";
197 
198 		seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask,
199 			   r->func, r->data, atomic_long_read(&r->matches),
200 			   r->ident);
201 	}
202 }
203 
204 static void can_print_recv_banner(struct seq_file *m)
205 {
206 	/*
207 	 *                  can1.  00000000  00000000  00000000
208 	 *                 .......          0  tp20
209 	 */
210 	if (IS_ENABLED(CONFIG_64BIT))
211 		seq_puts(m, "  device   can_id   can_mask      function          userdata       matches  ident\n");
212 	else
213 		seq_puts(m, "  device   can_id   can_mask  function  userdata   matches  ident\n");
214 }
215 
216 static int can_stats_proc_show(struct seq_file *m, void *v)
217 {
218 	struct net *net = m->private;
219 	struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
220 	struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
221 
222 	seq_putc(m, '\n');
223 	seq_printf(m, " %8ld transmitted frames (TXF)\n",
224 		   atomic_long_read(&pkg_stats->tx_frames));
225 	seq_printf(m, " %8ld received frames (RXF)\n",
226 		   atomic_long_read(&pkg_stats->rx_frames));
227 	seq_printf(m, " %8ld matched frames (RXMF)\n",
228 		   atomic_long_read(&pkg_stats->matches));
229 
230 	seq_putc(m, '\n');
231 
232 	if (net->can.stattimer.function == can_stat_update) {
233 		seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
234 				pkg_stats->total_rx_match_ratio);
235 
236 		seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
237 				pkg_stats->total_tx_rate);
238 		seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
239 				pkg_stats->total_rx_rate);
240 
241 		seq_putc(m, '\n');
242 
243 		seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
244 				pkg_stats->current_rx_match_ratio);
245 
246 		seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
247 				pkg_stats->current_tx_rate);
248 		seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
249 				pkg_stats->current_rx_rate);
250 
251 		seq_putc(m, '\n');
252 
253 		seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
254 				pkg_stats->max_rx_match_ratio);
255 
256 		seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
257 				pkg_stats->max_tx_rate);
258 		seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
259 				pkg_stats->max_rx_rate);
260 
261 		seq_putc(m, '\n');
262 	}
263 
264 	seq_printf(m, " %8ld current receive list entries (CRCV)\n",
265 			rcv_lists_stats->rcv_entries);
266 	seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
267 			rcv_lists_stats->rcv_entries_max);
268 
269 	if (rcv_lists_stats->stats_reset)
270 		seq_printf(m, "\n %8ld statistic resets (STR)\n",
271 				rcv_lists_stats->stats_reset);
272 
273 	if (rcv_lists_stats->user_reset)
274 		seq_printf(m, " %8ld user statistic resets (USTR)\n",
275 				rcv_lists_stats->user_reset);
276 
277 	seq_putc(m, '\n');
278 	return 0;
279 }
280 
281 static int can_reset_stats_proc_show(struct seq_file *m, void *v)
282 {
283 	struct net *net = m->private;
284 	struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
285 	struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
286 
287 	user_reset = 1;
288 
289 	if (net->can.stattimer.function == can_stat_update) {
290 		seq_printf(m, "Scheduled statistic reset #%ld.\n",
291 				rcv_lists_stats->stats_reset + 1);
292 	} else {
293 		if (pkg_stats->jiffies_init != jiffies)
294 			can_init_stats(net);
295 
296 		seq_printf(m, "Performed statistic reset #%ld.\n",
297 				rcv_lists_stats->stats_reset);
298 	}
299 	return 0;
300 }
301 
302 static inline void can_rcvlist_proc_show_one(struct seq_file *m, int idx,
303 					     struct net_device *dev,
304 					     struct can_dev_rcv_lists *dev_rcv_lists)
305 {
306 	if (!hlist_empty(&dev_rcv_lists->rx[idx])) {
307 		can_print_recv_banner(m);
308 		can_print_rcvlist(m, &dev_rcv_lists->rx[idx], dev);
309 	} else
310 		seq_printf(m, "  (%s: no entry)\n", DNAME(dev));
311 
312 }
313 
314 static int can_rcvlist_proc_show(struct seq_file *m, void *v)
315 {
316 	/* double cast to prevent GCC warning */
317 	int idx = (int)(long)pde_data(m->file->f_inode);
318 	struct net_device *dev;
319 	struct can_dev_rcv_lists *dev_rcv_lists;
320 	struct net *net = m->private;
321 
322 	seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]);
323 
324 	rcu_read_lock();
325 
326 	/* receive list for 'all' CAN devices (dev == NULL) */
327 	dev_rcv_lists = net->can.rx_alldev_list;
328 	can_rcvlist_proc_show_one(m, idx, NULL, dev_rcv_lists);
329 
330 	/* receive list for registered CAN devices */
331 	for_each_netdev_rcu(net, dev) {
332 		struct can_ml_priv *can_ml = can_get_ml_priv(dev);
333 
334 		if (can_ml)
335 			can_rcvlist_proc_show_one(m, idx, dev,
336 						  &can_ml->dev_rcv_lists);
337 	}
338 
339 	rcu_read_unlock();
340 
341 	seq_putc(m, '\n');
342 	return 0;
343 }
344 
345 static inline void can_rcvlist_proc_show_array(struct seq_file *m,
346 					       struct net_device *dev,
347 					       struct hlist_head *rcv_array,
348 					       unsigned int rcv_array_sz)
349 {
350 	unsigned int i;
351 	int all_empty = 1;
352 
353 	/* check whether at least one list is non-empty */
354 	for (i = 0; i < rcv_array_sz; i++)
355 		if (!hlist_empty(&rcv_array[i])) {
356 			all_empty = 0;
357 			break;
358 		}
359 
360 	if (!all_empty) {
361 		can_print_recv_banner(m);
362 		for (i = 0; i < rcv_array_sz; i++) {
363 			if (!hlist_empty(&rcv_array[i]))
364 				can_print_rcvlist(m, &rcv_array[i], dev);
365 		}
366 	} else
367 		seq_printf(m, "  (%s: no entry)\n", DNAME(dev));
368 }
369 
370 static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v)
371 {
372 	struct net_device *dev;
373 	struct can_dev_rcv_lists *dev_rcv_lists;
374 	struct net *net = m->private;
375 
376 	/* RX_SFF */
377 	seq_puts(m, "\nreceive list 'rx_sff':\n");
378 
379 	rcu_read_lock();
380 
381 	/* sff receive list for 'all' CAN devices (dev == NULL) */
382 	dev_rcv_lists = net->can.rx_alldev_list;
383 	can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_sff,
384 				    ARRAY_SIZE(dev_rcv_lists->rx_sff));
385 
386 	/* sff receive list for registered CAN devices */
387 	for_each_netdev_rcu(net, dev) {
388 		struct can_ml_priv *can_ml = can_get_ml_priv(dev);
389 
390 		if (can_ml) {
391 			dev_rcv_lists = &can_ml->dev_rcv_lists;
392 			can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_sff,
393 						    ARRAY_SIZE(dev_rcv_lists->rx_sff));
394 		}
395 	}
396 
397 	rcu_read_unlock();
398 
399 	seq_putc(m, '\n');
400 	return 0;
401 }
402 
403 static int can_rcvlist_eff_proc_show(struct seq_file *m, void *v)
404 {
405 	struct net_device *dev;
406 	struct can_dev_rcv_lists *dev_rcv_lists;
407 	struct net *net = m->private;
408 
409 	/* RX_EFF */
410 	seq_puts(m, "\nreceive list 'rx_eff':\n");
411 
412 	rcu_read_lock();
413 
414 	/* eff receive list for 'all' CAN devices (dev == NULL) */
415 	dev_rcv_lists = net->can.rx_alldev_list;
416 	can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_eff,
417 				    ARRAY_SIZE(dev_rcv_lists->rx_eff));
418 
419 	/* eff receive list for registered CAN devices */
420 	for_each_netdev_rcu(net, dev) {
421 		struct can_ml_priv *can_ml = can_get_ml_priv(dev);
422 
423 		if (can_ml) {
424 			dev_rcv_lists = &can_ml->dev_rcv_lists;
425 			can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_eff,
426 						    ARRAY_SIZE(dev_rcv_lists->rx_eff));
427 		}
428 	}
429 
430 	rcu_read_unlock();
431 
432 	seq_putc(m, '\n');
433 	return 0;
434 }
435 
436 /*
437  * can_init_proc - create main CAN proc directory and procfs entries
438  */
439 void can_init_proc(struct net *net)
440 {
441 	/* create /proc/net/can directory */
442 	net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net);
443 
444 	if (!net->can.proc_dir) {
445 		printk(KERN_INFO "can: failed to create /proc/net/can . "
446 			   "CONFIG_PROC_FS missing?\n");
447 		return;
448 	}
449 
450 	/* own procfs entries from the AF_CAN core */
451 	net->can.pde_stats = proc_create_net_single(CAN_PROC_STATS, 0644,
452 			net->can.proc_dir, can_stats_proc_show, NULL);
453 	net->can.pde_reset_stats = proc_create_net_single(CAN_PROC_RESET_STATS,
454 			0644, net->can.proc_dir, can_reset_stats_proc_show,
455 			NULL);
456 	net->can.pde_rcvlist_err = proc_create_net_single(CAN_PROC_RCVLIST_ERR,
457 			0644, net->can.proc_dir, can_rcvlist_proc_show,
458 			(void *)RX_ERR);
459 	net->can.pde_rcvlist_all = proc_create_net_single(CAN_PROC_RCVLIST_ALL,
460 			0644, net->can.proc_dir, can_rcvlist_proc_show,
461 			(void *)RX_ALL);
462 	net->can.pde_rcvlist_fil = proc_create_net_single(CAN_PROC_RCVLIST_FIL,
463 			0644, net->can.proc_dir, can_rcvlist_proc_show,
464 			(void *)RX_FIL);
465 	net->can.pde_rcvlist_inv = proc_create_net_single(CAN_PROC_RCVLIST_INV,
466 			0644, net->can.proc_dir, can_rcvlist_proc_show,
467 			(void *)RX_INV);
468 	net->can.pde_rcvlist_eff = proc_create_net_single(CAN_PROC_RCVLIST_EFF,
469 			0644, net->can.proc_dir, can_rcvlist_eff_proc_show, NULL);
470 	net->can.pde_rcvlist_sff = proc_create_net_single(CAN_PROC_RCVLIST_SFF,
471 			0644, net->can.proc_dir, can_rcvlist_sff_proc_show, NULL);
472 }
473 
474 /*
475  * can_remove_proc - remove procfs entries and main CAN proc directory
476  */
477 void can_remove_proc(struct net *net)
478 {
479 	if (!net->can.proc_dir)
480 		return;
481 
482 	if (net->can.pde_stats)
483 		remove_proc_entry(CAN_PROC_STATS, net->can.proc_dir);
484 
485 	if (net->can.pde_reset_stats)
486 		remove_proc_entry(CAN_PROC_RESET_STATS, net->can.proc_dir);
487 
488 	if (net->can.pde_rcvlist_err)
489 		remove_proc_entry(CAN_PROC_RCVLIST_ERR, net->can.proc_dir);
490 
491 	if (net->can.pde_rcvlist_all)
492 		remove_proc_entry(CAN_PROC_RCVLIST_ALL, net->can.proc_dir);
493 
494 	if (net->can.pde_rcvlist_fil)
495 		remove_proc_entry(CAN_PROC_RCVLIST_FIL, net->can.proc_dir);
496 
497 	if (net->can.pde_rcvlist_inv)
498 		remove_proc_entry(CAN_PROC_RCVLIST_INV, net->can.proc_dir);
499 
500 	if (net->can.pde_rcvlist_eff)
501 		remove_proc_entry(CAN_PROC_RCVLIST_EFF, net->can.proc_dir);
502 
503 	if (net->can.pde_rcvlist_sff)
504 		remove_proc_entry(CAN_PROC_RCVLIST_SFF, net->can.proc_dir);
505 
506 	remove_proc_entry("can", net->proc_net);
507 }
508