proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 int can_print_rcvlist(char *page, int len, 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. len += snprintf(page + len, PAGE_SIZE - len, fmt,
  177. DNAME(dev), r->can_id, r->mask,
  178. (unsigned long)r->func, (unsigned long)r->data,
  179. r->matches, r->ident);
  180. /* does a typical line fit into the current buffer? */
  181. /* 100 Bytes before end of buffer */
  182. if (len > PAGE_SIZE - 100) {
  183. /* mark output cut off */
  184. len += snprintf(page + len, PAGE_SIZE - len,
  185. " (..)\n");
  186. break;
  187. }
  188. }
  189. rcu_read_unlock();
  190. return len;
  191. }
  192. static int can_print_recv_banner(char *page, int len)
  193. {
  194. /*
  195. * can1. 00000000 00000000 00000000
  196. * ....... 0 tp20
  197. */
  198. len += snprintf(page + len, PAGE_SIZE - len,
  199. " device can_id can_mask function"
  200. " userdata matches ident\n");
  201. return len;
  202. }
  203. static int can_proc_read_stats(char *page, char **start, off_t off,
  204. int count, int *eof, void *data)
  205. {
  206. int len = 0;
  207. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  208. len += snprintf(page + len, PAGE_SIZE - len,
  209. " %8ld transmitted frames (TXF)\n",
  210. can_stats.tx_frames);
  211. len += snprintf(page + len, PAGE_SIZE - len,
  212. " %8ld received frames (RXF)\n", can_stats.rx_frames);
  213. len += snprintf(page + len, PAGE_SIZE - len,
  214. " %8ld matched frames (RXMF)\n", can_stats.matches);
  215. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  216. if (can_stattimer.function == can_stat_update) {
  217. len += snprintf(page + len, PAGE_SIZE - len,
  218. " %8ld %% total match ratio (RXMR)\n",
  219. can_stats.total_rx_match_ratio);
  220. len += snprintf(page + len, PAGE_SIZE - len,
  221. " %8ld frames/s total tx rate (TXR)\n",
  222. can_stats.total_tx_rate);
  223. len += snprintf(page + len, PAGE_SIZE - len,
  224. " %8ld frames/s total rx rate (RXR)\n",
  225. can_stats.total_rx_rate);
  226. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  227. len += snprintf(page + len, PAGE_SIZE - len,
  228. " %8ld %% current match ratio (CRXMR)\n",
  229. can_stats.current_rx_match_ratio);
  230. len += snprintf(page + len, PAGE_SIZE - len,
  231. " %8ld frames/s current tx rate (CTXR)\n",
  232. can_stats.current_tx_rate);
  233. len += snprintf(page + len, PAGE_SIZE - len,
  234. " %8ld frames/s current rx rate (CRXR)\n",
  235. can_stats.current_rx_rate);
  236. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  237. len += snprintf(page + len, PAGE_SIZE - len,
  238. " %8ld %% max match ratio (MRXMR)\n",
  239. can_stats.max_rx_match_ratio);
  240. len += snprintf(page + len, PAGE_SIZE - len,
  241. " %8ld frames/s max tx rate (MTXR)\n",
  242. can_stats.max_tx_rate);
  243. len += snprintf(page + len, PAGE_SIZE - len,
  244. " %8ld frames/s max rx rate (MRXR)\n",
  245. can_stats.max_rx_rate);
  246. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  247. }
  248. len += snprintf(page + len, PAGE_SIZE - len,
  249. " %8ld current receive list entries (CRCV)\n",
  250. can_pstats.rcv_entries);
  251. len += snprintf(page + len, PAGE_SIZE - len,
  252. " %8ld maximum receive list entries (MRCV)\n",
  253. can_pstats.rcv_entries_max);
  254. if (can_pstats.stats_reset)
  255. len += snprintf(page + len, PAGE_SIZE - len,
  256. "\n %8ld statistic resets (STR)\n",
  257. can_pstats.stats_reset);
  258. if (can_pstats.user_reset)
  259. len += snprintf(page + len, PAGE_SIZE - len,
  260. " %8ld user statistic resets (USTR)\n",
  261. can_pstats.user_reset);
  262. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  263. *eof = 1;
  264. return len;
  265. }
  266. static int can_proc_read_reset_stats(char *page, char **start, off_t off,
  267. int count, int *eof, void *data)
  268. {
  269. int len = 0;
  270. user_reset = 1;
  271. if (can_stattimer.function == can_stat_update) {
  272. len += snprintf(page + len, PAGE_SIZE - len,
  273. "Scheduled statistic reset #%ld.\n",
  274. can_pstats.stats_reset + 1);
  275. } else {
  276. if (can_stats.jiffies_init != jiffies)
  277. can_init_stats();
  278. len += snprintf(page + len, PAGE_SIZE - len,
  279. "Performed statistic reset #%ld.\n",
  280. can_pstats.stats_reset);
  281. }
  282. *eof = 1;
  283. return len;
  284. }
  285. static int can_proc_read_version(char *page, char **start, off_t off,
  286. int count, int *eof, void *data)
  287. {
  288. int len = 0;
  289. len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
  290. CAN_VERSION_STRING);
  291. *eof = 1;
  292. return len;
  293. }
  294. static int can_proc_read_rcvlist(char *page, char **start, off_t off,
  295. int count, int *eof, void *data)
  296. {
  297. /* double cast to prevent GCC warning */
  298. int idx = (int)(long)data;
  299. int len = 0;
  300. struct dev_rcv_lists *d;
  301. struct hlist_node *n;
  302. len += snprintf(page + len, PAGE_SIZE - len,
  303. "\nreceive list '%s':\n", rx_list_name[idx]);
  304. rcu_read_lock();
  305. hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
  306. if (!hlist_empty(&d->rx[idx])) {
  307. len = can_print_recv_banner(page, len);
  308. len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
  309. } else
  310. len += snprintf(page + len, PAGE_SIZE - len,
  311. " (%s: no entry)\n", DNAME(d->dev));
  312. /* exit on end of buffer? */
  313. if (len > PAGE_SIZE - 100)
  314. break;
  315. }
  316. rcu_read_unlock();
  317. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  318. *eof = 1;
  319. return len;
  320. }
  321. static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
  322. int count, int *eof, void *data)
  323. {
  324. int len = 0;
  325. struct dev_rcv_lists *d;
  326. struct hlist_node *n;
  327. /* RX_SFF */
  328. len += snprintf(page + len, PAGE_SIZE - len,
  329. "\nreceive list 'rx_sff':\n");
  330. rcu_read_lock();
  331. hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
  332. int i, all_empty = 1;
  333. /* check wether at least one list is non-empty */
  334. for (i = 0; i < 0x800; i++)
  335. if (!hlist_empty(&d->rx_sff[i])) {
  336. all_empty = 0;
  337. break;
  338. }
  339. if (!all_empty) {
  340. len = can_print_recv_banner(page, len);
  341. for (i = 0; i < 0x800; i++) {
  342. if (!hlist_empty(&d->rx_sff[i]) &&
  343. len < PAGE_SIZE - 100)
  344. len = can_print_rcvlist(page, len,
  345. &d->rx_sff[i],
  346. d->dev);
  347. }
  348. } else
  349. len += snprintf(page + len, PAGE_SIZE - len,
  350. " (%s: no entry)\n", DNAME(d->dev));
  351. /* exit on end of buffer? */
  352. if (len > PAGE_SIZE - 100)
  353. break;
  354. }
  355. rcu_read_unlock();
  356. len += snprintf(page + len, PAGE_SIZE - len, "\n");
  357. *eof = 1;
  358. return len;
  359. }
  360. /*
  361. * proc utility functions
  362. */
  363. static struct proc_dir_entry *can_create_proc_readentry(const char *name,
  364. mode_t mode,
  365. read_proc_t *read_proc,
  366. void *data)
  367. {
  368. if (can_dir)
  369. return create_proc_read_entry(name, mode, can_dir, read_proc,
  370. data);
  371. else
  372. return NULL;
  373. }
  374. static void can_remove_proc_readentry(const char *name)
  375. {
  376. if (can_dir)
  377. remove_proc_entry(name, can_dir);
  378. }
  379. /*
  380. * can_init_proc - create main CAN proc directory and procfs entries
  381. */
  382. void can_init_proc(void)
  383. {
  384. /* create /proc/net/can directory */
  385. can_dir = proc_mkdir("can", init_net.proc_net);
  386. if (!can_dir) {
  387. printk(KERN_INFO "can: failed to create /proc/net/can . "
  388. "CONFIG_PROC_FS missing?\n");
  389. return;
  390. }
  391. /* own procfs entries from the AF_CAN core */
  392. pde_version = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
  393. can_proc_read_version, NULL);
  394. pde_stats = can_create_proc_readentry(CAN_PROC_STATS, 0644,
  395. can_proc_read_stats, NULL);
  396. pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
  397. can_proc_read_reset_stats, NULL);
  398. pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
  399. can_proc_read_rcvlist, (void *)RX_ERR);
  400. pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
  401. can_proc_read_rcvlist, (void *)RX_ALL);
  402. pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
  403. can_proc_read_rcvlist, (void *)RX_FIL);
  404. pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
  405. can_proc_read_rcvlist, (void *)RX_INV);
  406. pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
  407. can_proc_read_rcvlist, (void *)RX_EFF);
  408. pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
  409. can_proc_read_rcvlist_sff, NULL);
  410. }
  411. /*
  412. * can_remove_proc - remove procfs entries and main CAN proc directory
  413. */
  414. void can_remove_proc(void)
  415. {
  416. if (pde_version)
  417. can_remove_proc_readentry(CAN_PROC_VERSION);
  418. if (pde_stats)
  419. can_remove_proc_readentry(CAN_PROC_STATS);
  420. if (pde_reset_stats)
  421. can_remove_proc_readentry(CAN_PROC_RESET_STATS);
  422. if (pde_rcvlist_err)
  423. can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
  424. if (pde_rcvlist_all)
  425. can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
  426. if (pde_rcvlist_fil)
  427. can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
  428. if (pde_rcvlist_inv)
  429. can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
  430. if (pde_rcvlist_eff)
  431. can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
  432. if (pde_rcvlist_sff)
  433. can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
  434. if (can_dir)
  435. proc_net_remove(&init_net, "can");
  436. }