proc.c 14 KB

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