proc.c 15 KB

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