proc.c 15 KB

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