/net/can/proc.c

http://github.com/mirrors/linux · C · 503 lines · 320 code · 95 blank · 88 comment · 49 complexity · 9ba369bcb1b4c907d8042467b7c0e774 MD5 · raw file

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