proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 = timer_container_of(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, atomic_long_read(&r->matches),
  167. r->ident);
  168. }
  169. }
  170. static void can_print_recv_banner(struct seq_file *m)
  171. {
  172. /*
  173. * can1. 00000000 00000000 00000000
  174. * ....... 0 tp20
  175. */
  176. if (IS_ENABLED(CONFIG_64BIT))
  177. seq_puts(m, " device can_id can_mask function userdata matches ident\n");
  178. else
  179. seq_puts(m, " device can_id can_mask function userdata matches ident\n");
  180. }
  181. static int can_stats_proc_show(struct seq_file *m, void *v)
  182. {
  183. struct net *net = m->private;
  184. struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
  185. struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
  186. seq_putc(m, '\n');
  187. seq_printf(m, " %8ld transmitted frames (TXF)\n",
  188. atomic_long_read(&pkg_stats->tx_frames));
  189. seq_printf(m, " %8ld received frames (RXF)\n",
  190. atomic_long_read(&pkg_stats->rx_frames));
  191. seq_printf(m, " %8ld matched frames (RXMF)\n",
  192. atomic_long_read(&pkg_stats->matches));
  193. seq_putc(m, '\n');
  194. if (net->can.stattimer.function == can_stat_update) {
  195. seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
  196. pkg_stats->total_rx_match_ratio);
  197. seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
  198. pkg_stats->total_tx_rate);
  199. seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
  200. pkg_stats->total_rx_rate);
  201. seq_putc(m, '\n');
  202. seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
  203. pkg_stats->current_rx_match_ratio);
  204. seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
  205. pkg_stats->current_tx_rate);
  206. seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
  207. pkg_stats->current_rx_rate);
  208. seq_putc(m, '\n');
  209. seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
  210. pkg_stats->max_rx_match_ratio);
  211. seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
  212. pkg_stats->max_tx_rate);
  213. seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
  214. pkg_stats->max_rx_rate);
  215. seq_putc(m, '\n');
  216. }
  217. seq_printf(m, " %8ld current receive list entries (CRCV)\n",
  218. rcv_lists_stats->rcv_entries);
  219. seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
  220. rcv_lists_stats->rcv_entries_max);
  221. if (rcv_lists_stats->stats_reset)
  222. seq_printf(m, "\n %8ld statistic resets (STR)\n",
  223. rcv_lists_stats->stats_reset);
  224. if (rcv_lists_stats->user_reset)
  225. seq_printf(m, " %8ld user statistic resets (USTR)\n",
  226. rcv_lists_stats->user_reset);
  227. seq_putc(m, '\n');
  228. return 0;
  229. }
  230. static int can_reset_stats_proc_show(struct seq_file *m, void *v)
  231. {
  232. struct net *net = m->private;
  233. struct can_rcv_lists_stats *rcv_lists_stats = net->can.rcv_lists_stats;
  234. struct can_pkg_stats *pkg_stats = net->can.pkg_stats;
  235. user_reset = 1;
  236. if (net->can.stattimer.function == can_stat_update) {
  237. seq_printf(m, "Scheduled statistic reset #%ld.\n",
  238. rcv_lists_stats->stats_reset + 1);
  239. } else {
  240. if (pkg_stats->jiffies_init != jiffies)
  241. can_init_stats(net);
  242. seq_printf(m, "Performed statistic reset #%ld.\n",
  243. rcv_lists_stats->stats_reset);
  244. }
  245. return 0;
  246. }
  247. static inline void can_rcvlist_proc_show_one(struct seq_file *m, int idx,
  248. struct net_device *dev,
  249. struct can_dev_rcv_lists *dev_rcv_lists)
  250. {
  251. if (!hlist_empty(&dev_rcv_lists->rx[idx])) {
  252. can_print_recv_banner(m);
  253. can_print_rcvlist(m, &dev_rcv_lists->rx[idx], dev);
  254. } else
  255. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  256. }
  257. static int can_rcvlist_proc_show(struct seq_file *m, void *v)
  258. {
  259. /* double cast to prevent GCC warning */
  260. int idx = (int)(long)pde_data(m->file->f_inode);
  261. struct net_device *dev;
  262. struct can_dev_rcv_lists *dev_rcv_lists;
  263. struct net *net = m->private;
  264. seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]);
  265. rcu_read_lock();
  266. /* receive list for 'all' CAN devices (dev == NULL) */
  267. dev_rcv_lists = net->can.rx_alldev_list;
  268. can_rcvlist_proc_show_one(m, idx, NULL, dev_rcv_lists);
  269. /* receive list for registered CAN devices */
  270. for_each_netdev_rcu(net, dev) {
  271. struct can_ml_priv *can_ml = can_get_ml_priv(dev);
  272. if (can_ml)
  273. can_rcvlist_proc_show_one(m, idx, dev,
  274. &can_ml->dev_rcv_lists);
  275. }
  276. rcu_read_unlock();
  277. seq_putc(m, '\n');
  278. return 0;
  279. }
  280. static inline void can_rcvlist_proc_show_array(struct seq_file *m,
  281. struct net_device *dev,
  282. struct hlist_head *rcv_array,
  283. unsigned int rcv_array_sz)
  284. {
  285. unsigned int i;
  286. int all_empty = 1;
  287. /* check whether at least one list is non-empty */
  288. for (i = 0; i < rcv_array_sz; i++)
  289. if (!hlist_empty(&rcv_array[i])) {
  290. all_empty = 0;
  291. break;
  292. }
  293. if (!all_empty) {
  294. can_print_recv_banner(m);
  295. for (i = 0; i < rcv_array_sz; i++) {
  296. if (!hlist_empty(&rcv_array[i]))
  297. can_print_rcvlist(m, &rcv_array[i], dev);
  298. }
  299. } else
  300. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  301. }
  302. static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v)
  303. {
  304. struct net_device *dev;
  305. struct can_dev_rcv_lists *dev_rcv_lists;
  306. struct net *net = m->private;
  307. /* RX_SFF */
  308. seq_puts(m, "\nreceive list 'rx_sff':\n");
  309. rcu_read_lock();
  310. /* sff receive list for 'all' CAN devices (dev == NULL) */
  311. dev_rcv_lists = net->can.rx_alldev_list;
  312. can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_sff,
  313. ARRAY_SIZE(dev_rcv_lists->rx_sff));
  314. /* sff receive list for registered CAN devices */
  315. for_each_netdev_rcu(net, dev) {
  316. struct can_ml_priv *can_ml = can_get_ml_priv(dev);
  317. if (can_ml) {
  318. dev_rcv_lists = &can_ml->dev_rcv_lists;
  319. can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_sff,
  320. ARRAY_SIZE(dev_rcv_lists->rx_sff));
  321. }
  322. }
  323. rcu_read_unlock();
  324. seq_putc(m, '\n');
  325. return 0;
  326. }
  327. static int can_rcvlist_eff_proc_show(struct seq_file *m, void *v)
  328. {
  329. struct net_device *dev;
  330. struct can_dev_rcv_lists *dev_rcv_lists;
  331. struct net *net = m->private;
  332. /* RX_EFF */
  333. seq_puts(m, "\nreceive list 'rx_eff':\n");
  334. rcu_read_lock();
  335. /* eff receive list for 'all' CAN devices (dev == NULL) */
  336. dev_rcv_lists = net->can.rx_alldev_list;
  337. can_rcvlist_proc_show_array(m, NULL, dev_rcv_lists->rx_eff,
  338. ARRAY_SIZE(dev_rcv_lists->rx_eff));
  339. /* eff receive list for registered CAN devices */
  340. for_each_netdev_rcu(net, dev) {
  341. struct can_ml_priv *can_ml = can_get_ml_priv(dev);
  342. if (can_ml) {
  343. dev_rcv_lists = &can_ml->dev_rcv_lists;
  344. can_rcvlist_proc_show_array(m, dev, dev_rcv_lists->rx_eff,
  345. ARRAY_SIZE(dev_rcv_lists->rx_eff));
  346. }
  347. }
  348. rcu_read_unlock();
  349. seq_putc(m, '\n');
  350. return 0;
  351. }
  352. /*
  353. * can_init_proc - create main CAN proc directory and procfs entries
  354. */
  355. void can_init_proc(struct net *net)
  356. {
  357. /* create /proc/net/can directory */
  358. net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net);
  359. if (!net->can.proc_dir) {
  360. printk(KERN_INFO "can: failed to create /proc/net/can . "
  361. "CONFIG_PROC_FS missing?\n");
  362. return;
  363. }
  364. /* own procfs entries from the AF_CAN core */
  365. net->can.pde_stats = proc_create_net_single(CAN_PROC_STATS, 0644,
  366. net->can.proc_dir, can_stats_proc_show, NULL);
  367. net->can.pde_reset_stats = proc_create_net_single(CAN_PROC_RESET_STATS,
  368. 0644, net->can.proc_dir, can_reset_stats_proc_show,
  369. NULL);
  370. net->can.pde_rcvlist_err = proc_create_net_single(CAN_PROC_RCVLIST_ERR,
  371. 0644, net->can.proc_dir, can_rcvlist_proc_show,
  372. (void *)RX_ERR);
  373. net->can.pde_rcvlist_all = proc_create_net_single(CAN_PROC_RCVLIST_ALL,
  374. 0644, net->can.proc_dir, can_rcvlist_proc_show,
  375. (void *)RX_ALL);
  376. net->can.pde_rcvlist_fil = proc_create_net_single(CAN_PROC_RCVLIST_FIL,
  377. 0644, net->can.proc_dir, can_rcvlist_proc_show,
  378. (void *)RX_FIL);
  379. net->can.pde_rcvlist_inv = proc_create_net_single(CAN_PROC_RCVLIST_INV,
  380. 0644, net->can.proc_dir, can_rcvlist_proc_show,
  381. (void *)RX_INV);
  382. net->can.pde_rcvlist_eff = proc_create_net_single(CAN_PROC_RCVLIST_EFF,
  383. 0644, net->can.proc_dir, can_rcvlist_eff_proc_show, NULL);
  384. net->can.pde_rcvlist_sff = proc_create_net_single(CAN_PROC_RCVLIST_SFF,
  385. 0644, net->can.proc_dir, can_rcvlist_sff_proc_show, NULL);
  386. }
  387. /*
  388. * can_remove_proc - remove procfs entries and main CAN proc directory
  389. */
  390. void can_remove_proc(struct net *net)
  391. {
  392. if (!net->can.proc_dir)
  393. return;
  394. if (net->can.pde_stats)
  395. remove_proc_entry(CAN_PROC_STATS, net->can.proc_dir);
  396. if (net->can.pde_reset_stats)
  397. remove_proc_entry(CAN_PROC_RESET_STATS, net->can.proc_dir);
  398. if (net->can.pde_rcvlist_err)
  399. remove_proc_entry(CAN_PROC_RCVLIST_ERR, net->can.proc_dir);
  400. if (net->can.pde_rcvlist_all)
  401. remove_proc_entry(CAN_PROC_RCVLIST_ALL, net->can.proc_dir);
  402. if (net->can.pde_rcvlist_fil)
  403. remove_proc_entry(CAN_PROC_RCVLIST_FIL, net->can.proc_dir);
  404. if (net->can.pde_rcvlist_inv)
  405. remove_proc_entry(CAN_PROC_RCVLIST_INV, net->can.proc_dir);
  406. if (net->can.pde_rcvlist_eff)
  407. remove_proc_entry(CAN_PROC_RCVLIST_EFF, net->can.proc_dir);
  408. if (net->can.pde_rcvlist_sff)
  409. remove_proc_entry(CAN_PROC_RCVLIST_SFF, net->can.proc_dir);
  410. remove_proc_entry("can", net->proc_net);
  411. }