proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * proc.c - procfs support for Protocol family CAN core module
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. */
  41. #include <linux/module.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/list.h>
  44. #include <linux/rcupdate.h>
  45. #include <linux/if_arp.h>
  46. #include <linux/can/core.h>
  47. #include "af_can.h"
  48. /*
  49. * proc filenames for the PF_CAN core
  50. */
  51. #define CAN_PROC_VERSION "version"
  52. #define CAN_PROC_STATS "stats"
  53. #define CAN_PROC_RESET_STATS "reset_stats"
  54. #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
  55. #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
  56. #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
  57. #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
  58. #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
  59. #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
  60. static int user_reset;
  61. static const char rx_list_name[][8] = {
  62. [RX_ERR] = "rx_err",
  63. [RX_ALL] = "rx_all",
  64. [RX_FIL] = "rx_fil",
  65. [RX_INV] = "rx_inv",
  66. };
  67. /*
  68. * af_can statistics stuff
  69. */
  70. static void can_init_stats(struct net *net)
  71. {
  72. struct s_stats *can_stats = net->can.can_stats;
  73. struct s_pstats *can_pstats = net->can.can_pstats;
  74. /*
  75. * This memset function is called from a timer context (when
  76. * can_stattimer is active which is the default) OR in a process
  77. * context (reading the proc_fs when can_stattimer is disabled).
  78. */
  79. memset(can_stats, 0, sizeof(struct s_stats));
  80. can_stats->jiffies_init = jiffies;
  81. can_pstats->stats_reset++;
  82. if (user_reset) {
  83. user_reset = 0;
  84. can_pstats->user_reset++;
  85. }
  86. }
  87. static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
  88. unsigned long count)
  89. {
  90. unsigned long rate;
  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. rate = (count * HZ) / (newjif - oldjif);
  100. return rate;
  101. }
  102. void can_stat_update(struct timer_list *t)
  103. {
  104. struct net *net = from_timer(net, t, can.can_stattimer);
  105. struct s_stats *can_stats = net->can.can_stats;
  106. unsigned long j = jiffies; /* snapshot */
  107. /* restart counting in timer context on user request */
  108. if (user_reset)
  109. can_init_stats(net);
  110. /* restart counting on jiffies overflow */
  111. if (j < can_stats->jiffies_init)
  112. can_init_stats(net);
  113. /* prevent overflow in calc_rate() */
  114. if (can_stats->rx_frames > (ULONG_MAX / HZ))
  115. can_init_stats(net);
  116. /* prevent overflow in calc_rate() */
  117. if (can_stats->tx_frames > (ULONG_MAX / HZ))
  118. can_init_stats(net);
  119. /* matches overflow - very improbable */
  120. if (can_stats->matches > (ULONG_MAX / 100))
  121. can_init_stats(net);
  122. /* calc total values */
  123. if (can_stats->rx_frames)
  124. can_stats->total_rx_match_ratio = (can_stats->matches * 100) /
  125. can_stats->rx_frames;
  126. can_stats->total_tx_rate = calc_rate(can_stats->jiffies_init, j,
  127. can_stats->tx_frames);
  128. can_stats->total_rx_rate = calc_rate(can_stats->jiffies_init, j,
  129. can_stats->rx_frames);
  130. /* calc current values */
  131. if (can_stats->rx_frames_delta)
  132. can_stats->current_rx_match_ratio =
  133. (can_stats->matches_delta * 100) /
  134. can_stats->rx_frames_delta;
  135. can_stats->current_tx_rate = calc_rate(0, HZ, can_stats->tx_frames_delta);
  136. can_stats->current_rx_rate = calc_rate(0, HZ, can_stats->rx_frames_delta);
  137. /* check / update maximum values */
  138. if (can_stats->max_tx_rate < can_stats->current_tx_rate)
  139. can_stats->max_tx_rate = can_stats->current_tx_rate;
  140. if (can_stats->max_rx_rate < can_stats->current_rx_rate)
  141. can_stats->max_rx_rate = can_stats->current_rx_rate;
  142. if (can_stats->max_rx_match_ratio < can_stats->current_rx_match_ratio)
  143. can_stats->max_rx_match_ratio = can_stats->current_rx_match_ratio;
  144. /* clear values for 'current rate' calculation */
  145. can_stats->tx_frames_delta = 0;
  146. can_stats->rx_frames_delta = 0;
  147. can_stats->matches_delta = 0;
  148. /* restart timer (one second) */
  149. mod_timer(&net->can.can_stattimer, round_jiffies(jiffies + HZ));
  150. }
  151. /*
  152. * proc read functions
  153. */
  154. static void can_print_rcvlist(struct seq_file *m, struct hlist_head *rx_list,
  155. struct net_device *dev)
  156. {
  157. struct receiver *r;
  158. hlist_for_each_entry_rcu(r, rx_list, list) {
  159. char *fmt = (r->can_id & CAN_EFF_FLAG)?
  160. " %-5s %08x %08x %pK %pK %8ld %s\n" :
  161. " %-5s %03x %08x %pK %pK %8ld %s\n";
  162. seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask,
  163. r->func, r->data, r->matches, r->ident);
  164. }
  165. }
  166. static void can_print_recv_banner(struct seq_file *m)
  167. {
  168. /*
  169. * can1. 00000000 00000000 00000000
  170. * ....... 0 tp20
  171. */
  172. seq_puts(m, " device can_id can_mask function"
  173. " userdata matches ident\n");
  174. }
  175. static int can_stats_proc_show(struct seq_file *m, void *v)
  176. {
  177. struct net *net = m->private;
  178. struct s_stats *can_stats = net->can.can_stats;
  179. struct s_pstats *can_pstats = net->can.can_pstats;
  180. seq_putc(m, '\n');
  181. seq_printf(m, " %8ld transmitted frames (TXF)\n", can_stats->tx_frames);
  182. seq_printf(m, " %8ld received frames (RXF)\n", can_stats->rx_frames);
  183. seq_printf(m, " %8ld matched frames (RXMF)\n", can_stats->matches);
  184. seq_putc(m, '\n');
  185. if (net->can.can_stattimer.function == can_stat_update) {
  186. seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
  187. can_stats->total_rx_match_ratio);
  188. seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
  189. can_stats->total_tx_rate);
  190. seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
  191. can_stats->total_rx_rate);
  192. seq_putc(m, '\n');
  193. seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
  194. can_stats->current_rx_match_ratio);
  195. seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
  196. can_stats->current_tx_rate);
  197. seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
  198. can_stats->current_rx_rate);
  199. seq_putc(m, '\n');
  200. seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
  201. can_stats->max_rx_match_ratio);
  202. seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
  203. can_stats->max_tx_rate);
  204. seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
  205. can_stats->max_rx_rate);
  206. seq_putc(m, '\n');
  207. }
  208. seq_printf(m, " %8ld current receive list entries (CRCV)\n",
  209. can_pstats->rcv_entries);
  210. seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
  211. can_pstats->rcv_entries_max);
  212. if (can_pstats->stats_reset)
  213. seq_printf(m, "\n %8ld statistic resets (STR)\n",
  214. can_pstats->stats_reset);
  215. if (can_pstats->user_reset)
  216. seq_printf(m, " %8ld user statistic resets (USTR)\n",
  217. can_pstats->user_reset);
  218. seq_putc(m, '\n');
  219. return 0;
  220. }
  221. static int can_reset_stats_proc_show(struct seq_file *m, void *v)
  222. {
  223. struct net *net = m->private;
  224. struct s_pstats *can_pstats = net->can.can_pstats;
  225. struct s_stats *can_stats = net->can.can_stats;
  226. user_reset = 1;
  227. if (net->can.can_stattimer.function == can_stat_update) {
  228. seq_printf(m, "Scheduled statistic reset #%ld.\n",
  229. can_pstats->stats_reset + 1);
  230. } else {
  231. if (can_stats->jiffies_init != jiffies)
  232. can_init_stats(net);
  233. seq_printf(m, "Performed statistic reset #%ld.\n",
  234. can_pstats->stats_reset);
  235. }
  236. return 0;
  237. }
  238. static int can_version_proc_show(struct seq_file *m, void *v)
  239. {
  240. seq_printf(m, "%s\n", CAN_VERSION_STRING);
  241. return 0;
  242. }
  243. static inline void can_rcvlist_proc_show_one(struct seq_file *m, int idx,
  244. struct net_device *dev,
  245. struct can_dev_rcv_lists *d)
  246. {
  247. if (!hlist_empty(&d->rx[idx])) {
  248. can_print_recv_banner(m);
  249. can_print_rcvlist(m, &d->rx[idx], dev);
  250. } else
  251. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  252. }
  253. static int can_rcvlist_proc_show(struct seq_file *m, void *v)
  254. {
  255. /* double cast to prevent GCC warning */
  256. int idx = (int)(long)PDE_DATA(m->file->f_inode);
  257. struct net_device *dev;
  258. struct can_dev_rcv_lists *d;
  259. struct net *net = m->private;
  260. seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]);
  261. rcu_read_lock();
  262. /* receive list for 'all' CAN devices (dev == NULL) */
  263. d = net->can.can_rx_alldev_list;
  264. can_rcvlist_proc_show_one(m, idx, NULL, d);
  265. /* receive list for registered CAN devices */
  266. for_each_netdev_rcu(net, dev) {
  267. if (dev->type == ARPHRD_CAN && dev->ml_priv)
  268. can_rcvlist_proc_show_one(m, idx, dev, dev->ml_priv);
  269. }
  270. rcu_read_unlock();
  271. seq_putc(m, '\n');
  272. return 0;
  273. }
  274. static inline void can_rcvlist_proc_show_array(struct seq_file *m,
  275. struct net_device *dev,
  276. struct hlist_head *rcv_array,
  277. unsigned int rcv_array_sz)
  278. {
  279. unsigned int i;
  280. int all_empty = 1;
  281. /* check whether at least one list is non-empty */
  282. for (i = 0; i < rcv_array_sz; i++)
  283. if (!hlist_empty(&rcv_array[i])) {
  284. all_empty = 0;
  285. break;
  286. }
  287. if (!all_empty) {
  288. can_print_recv_banner(m);
  289. for (i = 0; i < rcv_array_sz; i++) {
  290. if (!hlist_empty(&rcv_array[i]))
  291. can_print_rcvlist(m, &rcv_array[i], dev);
  292. }
  293. } else
  294. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  295. }
  296. static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v)
  297. {
  298. struct net_device *dev;
  299. struct can_dev_rcv_lists *d;
  300. struct net *net = m->private;
  301. /* RX_SFF */
  302. seq_puts(m, "\nreceive list 'rx_sff':\n");
  303. rcu_read_lock();
  304. /* sff receive list for 'all' CAN devices (dev == NULL) */
  305. d = net->can.can_rx_alldev_list;
  306. can_rcvlist_proc_show_array(m, NULL, d->rx_sff, ARRAY_SIZE(d->rx_sff));
  307. /* sff receive list for registered CAN devices */
  308. for_each_netdev_rcu(net, dev) {
  309. if (dev->type == ARPHRD_CAN && dev->ml_priv) {
  310. d = dev->ml_priv;
  311. can_rcvlist_proc_show_array(m, dev, d->rx_sff,
  312. ARRAY_SIZE(d->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 *d;
  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. d = net->can.can_rx_alldev_list;
  329. can_rcvlist_proc_show_array(m, NULL, d->rx_eff, ARRAY_SIZE(d->rx_eff));
  330. /* eff receive list for registered CAN devices */
  331. for_each_netdev_rcu(net, dev) {
  332. if (dev->type == ARPHRD_CAN && dev->ml_priv) {
  333. d = dev->ml_priv;
  334. can_rcvlist_proc_show_array(m, dev, d->rx_eff,
  335. ARRAY_SIZE(d->rx_eff));
  336. }
  337. }
  338. rcu_read_unlock();
  339. seq_putc(m, '\n');
  340. return 0;
  341. }
  342. /*
  343. * can_init_proc - create main CAN proc directory and procfs entries
  344. */
  345. void can_init_proc(struct net *net)
  346. {
  347. /* create /proc/net/can directory */
  348. net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net);
  349. if (!net->can.proc_dir) {
  350. printk(KERN_INFO "can: failed to create /proc/net/can . "
  351. "CONFIG_PROC_FS missing?\n");
  352. return;
  353. }
  354. /* own procfs entries from the AF_CAN core */
  355. net->can.pde_version = proc_create_net_single(CAN_PROC_VERSION, 0644,
  356. net->can.proc_dir, can_version_proc_show, NULL);
  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.pde_version)
  385. remove_proc_entry(CAN_PROC_VERSION, net->can.proc_dir);
  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. if (net->can.proc_dir)
  403. remove_proc_entry("can", net->proc_net);
  404. }