proc.c 15 KB

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