proc.c 16 KB

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