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_reset_pkg_stats(struct can_pkg_stats *pkg_stats) 80 { 81 atomic_long_set(&pkg_stats->rx_frames, 0); 82 atomic_long_set(&pkg_stats->tx_frames, 0); 83 atomic_long_set(&pkg_stats->matches, 0); 84 85 pkg_stats->total_rx_rate = 0; 86 pkg_stats->total_tx_rate = 0; 87 pkg_stats->total_rx_match_ratio = 0; 88 89 pkg_stats->current_rx_rate = 0; 90 pkg_stats->current_tx_rate = 0; 91 pkg_stats->current_rx_match_ratio = 0; 92 93 pkg_stats->max_rx_rate = 0; 94 pkg_stats->max_tx_rate = 0; 95 pkg_stats->max_rx_match_ratio = 0; 96 97 atomic_long_set(&pkg_stats->rx_frames_delta, 0); 98 atomic_long_set(&pkg_stats->tx_frames_delta, 0); 99 atomic_long_set(&pkg_stats->matches_delta, 0); 100 } 101 102 static void can_init_stats(struct net *net) 103 { 104 struct can_pkg_stats *pkg_stats = net->can.pkg_stats; 105 struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats; 106 /* 107 * This stats reset is called from a timer context (when 108 * can_stattimer is active which is the default) OR in a process 109 * context (reading the proc_fs when can_stattimer is disabled). 110 */ 111 can_reset_pkg_stats(pkg_stats); 112 pkg_stats->jiffies_init = jiffies; 113 114 rcv_lists_stats->stats_reset++; 115 116 if (user_reset) { 117 user_reset = 0; 118 rcv_lists_stats->user_reset++; 119 } 120 } 121 122 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif, 123 unsigned long count) 124 { 125 if (oldjif == newjif) 126 return 0; 127 128 /* see can_stat_update() - this should NEVER happen! */ 129 if (count > (ULONG_MAX / HZ)) { 130 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n", 131 count); 132 return 99999999; 133 } 134 135 return (count * HZ) / (newjif - oldjif); 136 } 137 138 void can_stat_update(struct timer_list *t) 139 { 140 struct net *net = timer_container_of(net, t, can.stattimer); 141 struct can_pkg_stats *pkg_stats = net->can.pkg_stats; 142 unsigned long j = jiffies; /* snapshot */ 143 144 long rx_frames = atomic_long_read(&pkg_stats->rx_frames); 145 long tx_frames = atomic_long_read(&pkg_stats->tx_frames); 146 long matches = atomic_long_read(&pkg_stats->matches); 147 /* fetch and clear values for 'current rate' calculation */ 148 long rx_frames_delta = atomic_long_xchg(&pkg_stats->rx_frames_delta, 0); 149 long tx_frames_delta = atomic_long_xchg(&pkg_stats->tx_frames_delta, 0); 150 long matches_delta = atomic_long_xchg(&pkg_stats->matches_delta, 0); 151 152 /* 153 * Restart counting in timer context on user request, on jiffies 154 * overflow or to prevent overflows in calc_rate() and in the 155 * match ratio calculation. The local snapshots are stale after 156 * the reset, so skip the calculation for this interval. 157 */ 158 if (user_reset || j < pkg_stats->jiffies_init || 159 rx_frames > (LONG_MAX / HZ) || tx_frames > (LONG_MAX / HZ) || 160 matches > (LONG_MAX / 100)) { 161 can_init_stats(net); 162 goto out; 163 } 164 165 /* calc total values */ 166 if (rx_frames) 167 pkg_stats->total_rx_match_ratio = (matches * 100) / rx_frames; 168 169 pkg_stats->total_tx_rate = calc_rate(pkg_stats->jiffies_init, j, 170 tx_frames); 171 pkg_stats->total_rx_rate = calc_rate(pkg_stats->jiffies_init, j, 172 rx_frames); 173 174 /* calc current values */ 175 if (rx_frames_delta) 176 pkg_stats->current_rx_match_ratio = 177 (matches_delta * 100) / rx_frames_delta; 178 179 pkg_stats->current_tx_rate = calc_rate(0, HZ, tx_frames_delta); 180 pkg_stats->current_rx_rate = calc_rate(0, HZ, rx_frames_delta); 181 182 /* check / update maximum values */ 183 if (pkg_stats->max_tx_rate < pkg_stats->current_tx_rate) 184 pkg_stats->max_tx_rate = pkg_stats->current_tx_rate; 185 186 if (pkg_stats->max_rx_rate < pkg_stats->current_rx_rate) 187 pkg_stats->max_rx_rate = pkg_stats->current_rx_rate; 188 189 if (pkg_stats->max_rx_match_ratio < pkg_stats->current_rx_match_ratio) 190 pkg_stats->max_rx_match_ratio = pkg_stats->current_rx_match_ratio; 191 192 out: 193 /* restart timer (one second) */ 194 mod_timer(&net->can.stattimer, round_jiffies(jiffies + HZ)); 195 } 196 197 /* 198 * proc read functions 199 */ 200 201 static void can_print_rcvlist(struct seq_file *m, struct hlist_head *rx_list, 202 struct net_device *dev) 203 { 204 struct receiver *r; 205 206 hlist_for_each_entry_rcu(r, rx_list, list) { 207 char symcheck[4]; 208 char *fmt = (r->can_id & CAN_EFF_FLAG)? 209 " %6s %08x %08x %8ld %10llu" : 210 " %6s %03x %08x %8ld %10llu"; 211 212 seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask, 213 atomic_long_read(&r->matches), r->ino); 214 215 /* %ps falls back to the raw text address when the symbol 216 * can not be resolved (KALLSYMS=n, stripped module symbols) 217 */ 218 snprintf(symcheck, sizeof(symcheck), "%ps", r->func); 219 if (strncmp(symcheck, "0x", 2)) 220 seq_printf(m, " %ps\n", r->func); 221 else 222 seq_puts(m, " (unknown)\n"); 223 } 224 } 225 226 static void can_print_recv_banner(struct seq_file *m) 227 { 228 /* 229 * device can_id can_mask matches sock_inode function 230 * vcan0 000 00000000 0 43798 raw_rcv [can_raw] 231 */ 232 seq_puts(m, " device can_id can_mask matches sock_inode function\n"); 233 } 234 235 static int can_stats_proc_show(struct seq_file *m, void *v) 236 { 237 struct net *net = m->private; 238 struct can_pkg_stats *pkg_stats = net->can.pkg_stats; 239 struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats; 240 241 seq_putc(m, '\n'); 242 seq_printf(m, " %8ld transmitted frames (TXF)\n", 243 atomic_long_read(&pkg_stats->tx_frames)); 244 seq_printf(m, " %8ld received frames (RXF)\n", 245 atomic_long_read(&pkg_stats->rx_frames)); 246 seq_printf(m, " %8ld matched frames (RXMF)\n", 247 atomic_long_read(&pkg_stats->matches)); 248 249 seq_putc(m, '\n'); 250 251 if (net->can.stattimer.function == can_stat_update) { 252 seq_printf(m, " %8ld %% total match ratio (RXMR)\n", 253 pkg_stats->total_rx_match_ratio); 254 255 seq_printf(m, " %8ld frames/s total tx rate (TXR)\n", 256 pkg_stats->total_tx_rate); 257 seq_printf(m, " %8ld frames/s total rx rate (RXR)\n", 258 pkg_stats->total_rx_rate); 259 260 seq_putc(m, '\n'); 261 262 seq_printf(m, " %8ld %% current match ratio (CRXMR)\n", 263 pkg_stats->current_rx_match_ratio); 264 265 seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n", 266 pkg_stats->current_tx_rate); 267 seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n", 268 pkg_stats->current_rx_rate); 269 270 seq_putc(m, '\n'); 271 272 seq_printf(m, " %8ld %% max match ratio (MRXMR)\n", 273 pkg_stats->max_rx_match_ratio); 274 275 seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n", 276 pkg_stats->max_tx_rate); 277 seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n", 278 pkg_stats->max_rx_rate); 279 280 seq_putc(m, '\n'); 281 } 282 283 seq_printf(m, " %8ld current receive list entries (CRCV)\n", 284 rcv_lists_stats->rcv_entries); 285 seq_printf(m, " %8ld maximum receive list entries (MRCV)\n", 286 rcv_lists_stats->rcv_entries_max); 287 288 if (rcv_lists_stats->stats_reset) 289 seq_printf(m, "\n %8ld statistic resets (STR)\n", 290 rcv_lists_stats->stats_reset); 291 292 if (rcv_lists_stats->user_reset) 293 seq_printf(m, " %8ld user statistic resets (USTR)\n", 294 rcv_lists_stats->user_reset); 295 296 seq_putc(m, '\n'); 297 return 0; 298 } 299 300 static int can_reset_stats_proc_show(struct seq_file *m, void *v) 301 { 302 struct net *net = m->private; 303 struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats; 304 struct can_pkg_stats *pkg_stats = net->can.pkg_stats; 305 306 user_reset = 1; 307 308 if (net->can.stattimer.function == can_stat_update) { 309 seq_printf(m, "Scheduled statistic reset #%ld.\n", 310 rcv_lists_stats->stats_reset + 1); 311 } else { 312 if (pkg_stats->jiffies_init != jiffies) 313 can_init_stats(net); 314 315 seq_printf(m, "Performed statistic reset #%ld.\n", 316 rcv_lists_stats->stats_reset); 317 } 318 return 0; 319 } 320 321 static inline void can_rcvlist_proc_show_one(struct seq_file *m, int idx, 322 struct net_device *dev, 323 struct can_dev_rcv_lists *dev_rcv_lists) 324 { 325 if (!hlist_empty(&dev_rcv_lists->rx[idx])) { 326 can_print_recv_banner(m); 327 can_print_rcvlist(m, &dev_rcv_lists->rx[idx], dev); 328 } else 329 seq_printf(m, " (%s: no entry)\n", DNAME(dev)); 330 331 } 332 333 static int can_rcvlist_proc_show(struct seq_file *m, void *v) 334 { 335 /* double cast to prevent GCC warning */ 336 int idx = (int)(long)pde_data(m->file->f_inode); 337 struct net_device *dev; 338 struct can_dev_rcv_lists *dev_rcv_lists; 339 struct net *net = m->private; 340 341 seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]); 342 343 rcu_read_lock(); 344 345 /* receive list for 'all' CAN devices (dev == NULL) */ 346 dev_rcv_lists = net->can.rx_alldev_list; 347 can_rcvlist_proc_show_one(m, idx, NULL, dev_rcv_lists); 348 349 /* receive list for registered CAN devices */ 350 for_each_netdev_rcu(net, dev) { 351 struct can_ml_priv *can_ml = can_get_ml_priv(dev); 352 353 if (can_ml) 354 can_rcvlist_proc_show_one(m, idx, dev, 355 &can_ml->dev_rcv_lists); 356 } 357 358 rcu_read_unlock(); 359 360 seq_putc(m, '\n'); 361 return 0; 362 } 363 364 static inline void can_rcvlist_proc_show_array(struct seq_file *m, 365 struct net_device *dev, 366 struct hlist_head *rcv_array, 367 unsigned int rcv_array_sz) 368 { 369 unsigned int i; 370 int all_empty = 1; 371 372 /* check whether at least one list is non-empty */ 373 for (i = 0; i < rcv_array_sz; i++) 374 if (!hlist_empty(&rcv_array[i])) { 375 all_empty = 0; 376 break; 377 } 378 379 if (!all_empty) { 380 can_print_recv_banner(m); 381 for (i = 0; i < rcv_array_sz; i++) { 382 if (!hlist_empty(&rcv_array[i])) 383 can_print_rcvlist(m, &rcv_array[i], dev); 384 } 385 } else 386 seq_printf(m, " (%s: no entry)\n", DNAME(dev)); 387 } 388 389 static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v) 390 { 391 struct net_device *dev; 392 struct can_dev_rcv_lists *dev_rcv_lists; 393 struct net *net = m->private; 394 395 /* RX_SFF */ 396 seq_puts(m, "\nreceive list 'rx_sff':\n"); 397 398 rcu_read_lock(); 399 400 /* sff receive list for 'all' CAN devices (dev == NULL) */ 401 dev_rcv_lists = net->can.rx_alldev_list; 402 can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_sff, 403 ARRAY_SIZE(dev_rcv_lists->rx_sff)); 404 405 /* sff receive list for registered CAN devices */ 406 for_each_netdev_rcu(net, dev) { 407 struct can_ml_priv *can_ml = can_get_ml_priv(dev); 408 409 if (can_ml) { 410 dev_rcv_lists = &can_ml->dev_rcv_lists; 411 can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_sff, 412 ARRAY_SIZE(dev_rcv_lists->rx_sff)); 413 } 414 } 415 416 rcu_read_unlock(); 417 418 seq_putc(m, '\n'); 419 return 0; 420 } 421 422 static int can_rcvlist_eff_proc_show(struct seq_file *m, void *v) 423 { 424 struct net_device *dev; 425 struct can_dev_rcv_lists *dev_rcv_lists; 426 struct net *net = m->private; 427 428 /* RX_EFF */ 429 seq_puts(m, "\nreceive list 'rx_eff':\n"); 430 431 rcu_read_lock(); 432 433 /* eff receive list for 'all' CAN devices (dev == NULL) */ 434 dev_rcv_lists = net->can.rx_alldev_list; 435 can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_eff, 436 ARRAY_SIZE(dev_rcv_lists->rx_eff)); 437 438 /* eff receive list for registered CAN devices */ 439 for_each_netdev_rcu(net, dev) { 440 struct can_ml_priv *can_ml = can_get_ml_priv(dev); 441 442 if (can_ml) { 443 dev_rcv_lists = &can_ml->dev_rcv_lists; 444 can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_eff, 445 ARRAY_SIZE(dev_rcv_lists->rx_eff)); 446 } 447 } 448 449 rcu_read_unlock(); 450 451 seq_putc(m, '\n'); 452 return 0; 453 } 454 455 /* 456 * can_init_proc - create main CAN proc directory and procfs entries 457 */ 458 void can_init_proc(struct net *net) 459 { 460 /* create /proc/net/can directory */ 461 net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net); 462 463 if (!net->can.proc_dir) { 464 printk(KERN_INFO "can: failed to create /proc/net/can . " 465 "CONFIG_PROC_FS missing?\n"); 466 return; 467 } 468 469 /* own procfs entries from the AF_CAN core */ 470 net->can.pde_stats = proc_create_net_single(CAN_PROC_STATS, 0644, 471 net->can.proc_dir, can_stats_proc_show, NULL); 472 net->can.pde_reset_stats = proc_create_net_single(CAN_PROC_RESET_STATS, 473 0644, net->can.proc_dir, can_reset_stats_proc_show, 474 NULL); 475 net->can.pde_rcvlist_err = proc_create_net_single(CAN_PROC_RCVLIST_ERR, 476 0644, net->can.proc_dir, can_rcvlist_proc_show, 477 (void *)RX_ERR); 478 net->can.pde_rcvlist_all = proc_create_net_single(CAN_PROC_RCVLIST_ALL, 479 0644, net->can.proc_dir, can_rcvlist_proc_show, 480 (void *)RX_ALL); 481 net->can.pde_rcvlist_fil = proc_create_net_single(CAN_PROC_RCVLIST_FIL, 482 0644, net->can.proc_dir, can_rcvlist_proc_show, 483 (void *)RX_FIL); 484 net->can.pde_rcvlist_inv = proc_create_net_single(CAN_PROC_RCVLIST_INV, 485 0644, net->can.proc_dir, can_rcvlist_proc_show, 486 (void *)RX_INV); 487 net->can.pde_rcvlist_eff = proc_create_net_single(CAN_PROC_RCVLIST_EFF, 488 0644, net->can.proc_dir, can_rcvlist_eff_proc_show, NULL); 489 net->can.pde_rcvlist_sff = proc_create_net_single(CAN_PROC_RCVLIST_SFF, 490 0644, net->can.proc_dir, can_rcvlist_sff_proc_show, NULL); 491 } 492 493 /* 494 * can_remove_proc - remove procfs entries and main CAN proc directory 495 */ 496 void can_remove_proc(struct net *net) 497 { 498 if (!net->can.proc_dir) 499 return; 500 501 if (net->can.pde_stats) 502 remove_proc_entry(CAN_PROC_STATS, net->can.proc_dir); 503 504 if (net->can.pde_reset_stats) 505 remove_proc_entry(CAN_PROC_RESET_STATS, net->can.proc_dir); 506 507 if (net->can.pde_rcvlist_err) 508 remove_proc_entry(CAN_PROC_RCVLIST_ERR, net->can.proc_dir); 509 510 if (net->can.pde_rcvlist_all) 511 remove_proc_entry(CAN_PROC_RCVLIST_ALL, net->can.proc_dir); 512 513 if (net->can.pde_rcvlist_fil) 514 remove_proc_entry(CAN_PROC_RCVLIST_FIL, net->can.proc_dir); 515 516 if (net->can.pde_rcvlist_inv) 517 remove_proc_entry(CAN_PROC_RCVLIST_INV, net->can.proc_dir); 518 519 if (net->can.pde_rcvlist_eff) 520 remove_proc_entry(CAN_PROC_RCVLIST_EFF, net->can.proc_dir); 521 522 if (net->can.pde_rcvlist_sff) 523 remove_proc_entry(CAN_PROC_RCVLIST_SFF, net->can.proc_dir); 524 525 remove_proc_entry("can", net->proc_net); 526 } 527