proc.c 15 KB

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