proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. struct hlist_node *n;
  166. hlist_for_each_entry_rcu(r, n, rx_list, list) {
  167. char *fmt = (r->can_id & CAN_EFF_FLAG)?
  168. " %-5s %08x %08x %pK %pK %8ld %s\n" :
  169. " %-5s %03x %08x %pK %pK %8ld %s\n";
  170. seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask,
  171. r->func, r->data, r->matches, r->ident);
  172. }
  173. }
  174. static void can_print_recv_banner(struct seq_file *m)
  175. {
  176. /*
  177. * can1. 00000000 00000000 00000000
  178. * ....... 0 tp20
  179. */
  180. seq_puts(m, " device can_id can_mask function"
  181. " userdata matches ident\n");
  182. }
  183. static int can_stats_proc_show(struct seq_file *m, void *v)
  184. {
  185. seq_putc(m, '\n');
  186. seq_printf(m, " %8ld transmitted frames (TXF)\n", can_stats.tx_frames);
  187. seq_printf(m, " %8ld received frames (RXF)\n", can_stats.rx_frames);
  188. seq_printf(m, " %8ld matched frames (RXMF)\n", can_stats.matches);
  189. seq_putc(m, '\n');
  190. if (can_stattimer.function == can_stat_update) {
  191. seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
  192. can_stats.total_rx_match_ratio);
  193. seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
  194. can_stats.total_tx_rate);
  195. seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
  196. can_stats.total_rx_rate);
  197. seq_putc(m, '\n');
  198. seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
  199. can_stats.current_rx_match_ratio);
  200. seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
  201. can_stats.current_tx_rate);
  202. seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
  203. can_stats.current_rx_rate);
  204. seq_putc(m, '\n');
  205. seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
  206. can_stats.max_rx_match_ratio);
  207. seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
  208. can_stats.max_tx_rate);
  209. seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
  210. can_stats.max_rx_rate);
  211. seq_putc(m, '\n');
  212. }
  213. seq_printf(m, " %8ld current receive list entries (CRCV)\n",
  214. can_pstats.rcv_entries);
  215. seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
  216. can_pstats.rcv_entries_max);
  217. if (can_pstats.stats_reset)
  218. seq_printf(m, "\n %8ld statistic resets (STR)\n",
  219. can_pstats.stats_reset);
  220. if (can_pstats.user_reset)
  221. seq_printf(m, " %8ld user statistic resets (USTR)\n",
  222. can_pstats.user_reset);
  223. seq_putc(m, '\n');
  224. return 0;
  225. }
  226. static int can_stats_proc_open(struct inode *inode, struct file *file)
  227. {
  228. return single_open(file, can_stats_proc_show, NULL);
  229. }
  230. static const struct file_operations can_stats_proc_fops = {
  231. .owner = THIS_MODULE,
  232. .open = can_stats_proc_open,
  233. .read = seq_read,
  234. .llseek = seq_lseek,
  235. .release = single_release,
  236. };
  237. static int can_reset_stats_proc_show(struct seq_file *m, void *v)
  238. {
  239. user_reset = 1;
  240. if (can_stattimer.function == can_stat_update) {
  241. seq_printf(m, "Scheduled statistic reset #%ld.\n",
  242. can_pstats.stats_reset + 1);
  243. } else {
  244. if (can_stats.jiffies_init != jiffies)
  245. can_init_stats();
  246. seq_printf(m, "Performed statistic reset #%ld.\n",
  247. can_pstats.stats_reset);
  248. }
  249. return 0;
  250. }
  251. static int can_reset_stats_proc_open(struct inode *inode, struct file *file)
  252. {
  253. return single_open(file, can_reset_stats_proc_show, NULL);
  254. }
  255. static const struct file_operations can_reset_stats_proc_fops = {
  256. .owner = THIS_MODULE,
  257. .open = can_reset_stats_proc_open,
  258. .read = seq_read,
  259. .llseek = seq_lseek,
  260. .release = single_release,
  261. };
  262. static int can_version_proc_show(struct seq_file *m, void *v)
  263. {
  264. seq_printf(m, "%s\n", CAN_VERSION_STRING);
  265. return 0;
  266. }
  267. static int can_version_proc_open(struct inode *inode, struct file *file)
  268. {
  269. return single_open(file, can_version_proc_show, NULL);
  270. }
  271. static const struct file_operations can_version_proc_fops = {
  272. .owner = THIS_MODULE,
  273. .open = can_version_proc_open,
  274. .read = seq_read,
  275. .llseek = seq_lseek,
  276. .release = single_release,
  277. };
  278. static inline void can_rcvlist_proc_show_one(struct seq_file *m, int idx,
  279. struct net_device *dev,
  280. struct dev_rcv_lists *d)
  281. {
  282. if (!hlist_empty(&d->rx[idx])) {
  283. can_print_recv_banner(m);
  284. can_print_rcvlist(m, &d->rx[idx], dev);
  285. } else
  286. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  287. }
  288. static int can_rcvlist_proc_show(struct seq_file *m, void *v)
  289. {
  290. /* double cast to prevent GCC warning */
  291. int idx = (int)(long)m->private;
  292. struct net_device *dev;
  293. struct dev_rcv_lists *d;
  294. seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]);
  295. rcu_read_lock();
  296. /* receive list for 'all' CAN devices (dev == NULL) */
  297. d = &can_rx_alldev_list;
  298. can_rcvlist_proc_show_one(m, idx, NULL, d);
  299. /* receive list for registered CAN devices */
  300. for_each_netdev_rcu(&init_net, dev) {
  301. if (dev->type == ARPHRD_CAN && dev->ml_priv)
  302. can_rcvlist_proc_show_one(m, idx, dev, dev->ml_priv);
  303. }
  304. rcu_read_unlock();
  305. seq_putc(m, '\n');
  306. return 0;
  307. }
  308. static int can_rcvlist_proc_open(struct inode *inode, struct file *file)
  309. {
  310. return single_open(file, can_rcvlist_proc_show, PDE(inode)->data);
  311. }
  312. static const struct file_operations can_rcvlist_proc_fops = {
  313. .owner = THIS_MODULE,
  314. .open = can_rcvlist_proc_open,
  315. .read = seq_read,
  316. .llseek = seq_lseek,
  317. .release = single_release,
  318. };
  319. static inline void can_rcvlist_sff_proc_show_one(struct seq_file *m,
  320. struct net_device *dev,
  321. struct dev_rcv_lists *d)
  322. {
  323. int i;
  324. int 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], dev);
  336. }
  337. } else
  338. seq_printf(m, " (%s: no entry)\n", DNAME(dev));
  339. }
  340. static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v)
  341. {
  342. struct net_device *dev;
  343. struct dev_rcv_lists *d;
  344. /* RX_SFF */
  345. seq_puts(m, "\nreceive list 'rx_sff':\n");
  346. rcu_read_lock();
  347. /* sff receive list for 'all' CAN devices (dev == NULL) */
  348. d = &can_rx_alldev_list;
  349. can_rcvlist_sff_proc_show_one(m, NULL, d);
  350. /* sff receive list for registered CAN devices */
  351. for_each_netdev_rcu(&init_net, dev) {
  352. if (dev->type == ARPHRD_CAN && dev->ml_priv)
  353. can_rcvlist_sff_proc_show_one(m, dev, dev->ml_priv);
  354. }
  355. rcu_read_unlock();
  356. seq_putc(m, '\n');
  357. return 0;
  358. }
  359. static int can_rcvlist_sff_proc_open(struct inode *inode, struct file *file)
  360. {
  361. return single_open(file, can_rcvlist_sff_proc_show, NULL);
  362. }
  363. static const struct file_operations can_rcvlist_sff_proc_fops = {
  364. .owner = THIS_MODULE,
  365. .open = can_rcvlist_sff_proc_open,
  366. .read = seq_read,
  367. .llseek = seq_lseek,
  368. .release = single_release,
  369. };
  370. /*
  371. * proc utility functions
  372. */
  373. static void can_remove_proc_readentry(const char *name)
  374. {
  375. if (can_dir)
  376. remove_proc_entry(name, can_dir);
  377. }
  378. /*
  379. * can_init_proc - create main CAN proc directory and procfs entries
  380. */
  381. void can_init_proc(void)
  382. {
  383. /* create /proc/net/can directory */
  384. can_dir = proc_mkdir("can", init_net.proc_net);
  385. if (!can_dir) {
  386. printk(KERN_INFO "can: failed to create /proc/net/can . "
  387. "CONFIG_PROC_FS missing?\n");
  388. return;
  389. }
  390. /* own procfs entries from the AF_CAN core */
  391. pde_version = proc_create(CAN_PROC_VERSION, 0644, can_dir,
  392. &can_version_proc_fops);
  393. pde_stats = proc_create(CAN_PROC_STATS, 0644, can_dir,
  394. &can_stats_proc_fops);
  395. pde_reset_stats = proc_create(CAN_PROC_RESET_STATS, 0644, can_dir,
  396. &can_reset_stats_proc_fops);
  397. pde_rcvlist_err = proc_create_data(CAN_PROC_RCVLIST_ERR, 0644, can_dir,
  398. &can_rcvlist_proc_fops, (void *)RX_ERR);
  399. pde_rcvlist_all = proc_create_data(CAN_PROC_RCVLIST_ALL, 0644, can_dir,
  400. &can_rcvlist_proc_fops, (void *)RX_ALL);
  401. pde_rcvlist_fil = proc_create_data(CAN_PROC_RCVLIST_FIL, 0644, can_dir,
  402. &can_rcvlist_proc_fops, (void *)RX_FIL);
  403. pde_rcvlist_inv = proc_create_data(CAN_PROC_RCVLIST_INV, 0644, can_dir,
  404. &can_rcvlist_proc_fops, (void *)RX_INV);
  405. pde_rcvlist_eff = proc_create_data(CAN_PROC_RCVLIST_EFF, 0644, can_dir,
  406. &can_rcvlist_proc_fops, (void *)RX_EFF);
  407. pde_rcvlist_sff = proc_create(CAN_PROC_RCVLIST_SFF, 0644, can_dir,
  408. &can_rcvlist_sff_proc_fops);
  409. }
  410. /*
  411. * can_remove_proc - remove procfs entries and main CAN proc directory
  412. */
  413. void can_remove_proc(void)
  414. {
  415. if (pde_version)
  416. can_remove_proc_readentry(CAN_PROC_VERSION);
  417. if (pde_stats)
  418. can_remove_proc_readentry(CAN_PROC_STATS);
  419. if (pde_reset_stats)
  420. can_remove_proc_readentry(CAN_PROC_RESET_STATS);
  421. if (pde_rcvlist_err)
  422. can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
  423. if (pde_rcvlist_all)
  424. can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
  425. if (pde_rcvlist_fil)
  426. can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
  427. if (pde_rcvlist_inv)
  428. can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
  429. if (pde_rcvlist_eff)
  430. can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
  431. if (pde_rcvlist_sff)
  432. can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
  433. if (can_dir)
  434. proc_net_remove(&init_net, "can");
  435. }