proc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 int user_reset;
  61. static const char rx_list_name[][8] = {
  62. [RX_ERR] = "rx_err",
  63. [RX_ALL] = "rx_all",
  64. [RX_FIL] = "rx_fil",
  65. [RX_INV] = "rx_inv",
  66. };
  67. /*
  68. * af_can statistics stuff
  69. */
  70. static void can_init_stats(struct net *net)
  71. {
  72. struct s_stats *can_stats = net->can.can_stats;
  73. struct s_pstats *can_pstats = net->can.can_pstats;
  74. /*
  75. * This memset function is called from a timer context (when
  76. * can_stattimer is active which is the default) OR in a process
  77. * context (reading the proc_fs when can_stattimer is disabled).
  78. */
  79. memset(can_stats, 0, sizeof(struct s_stats));
  80. can_stats->jiffies_init = jiffies;
  81. can_pstats->stats_reset++;
  82. if (user_reset) {
  83. user_reset = 0;
  84. can_pstats->user_reset++;
  85. }
  86. }
  87. static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
  88. unsigned long count)
  89. {
  90. unsigned long rate;
  91. if (oldjif == newjif)
  92. return 0;
  93. /* see can_stat_update() - this should NEVER happen! */
  94. if (count > (ULONG_MAX / HZ)) {
  95. printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
  96. count);
  97. return 99999999;
  98. }
  99. rate = (count * HZ) / (newjif - oldjif);
  100. return rate;
  101. }
  102. void can_stat_update(unsigned long data)
  103. {
  104. struct net *net = (struct net *)data;
  105. struct s_stats *can_stats = net->can.can_stats;
  106. unsigned long j = jiffies; /* snapshot */
  107. /* restart counting in timer context on user request */
  108. if (user_reset)
  109. can_init_stats(net);
  110. /* restart counting on jiffies overflow */
  111. if (j < can_stats->jiffies_init)
  112. can_init_stats(net);
  113. /* prevent overflow in calc_rate() */
  114. if (can_stats->rx_frames > (ULONG_MAX / HZ))
  115. can_init_stats(net);
  116. /* prevent overflow in calc_rate() */
  117. if (can_stats->tx_frames > (ULONG_MAX / HZ))
  118. can_init_stats(net);
  119. /* matches overflow - very improbable */
  120. if (can_stats->matches > (ULONG_MAX / 100))
  121. can_init_stats(net);
  122. /* calc total values */
  123. if (can_stats->rx_frames)
  124. can_stats->total_rx_match_ratio = (can_stats->matches * 100) /
  125. can_stats->rx_frames;
  126. can_stats->total_tx_rate = calc_rate(can_stats->jiffies_init, j,
  127. can_stats->tx_frames);
  128. can_stats->total_rx_rate = calc_rate(can_stats->jiffies_init, j,
  129. can_stats->rx_frames);
  130. /* calc current values */
  131. if (can_stats->rx_frames_delta)
  132. can_stats->current_rx_match_ratio =
  133. (can_stats->matches_delta * 100) /
  134. can_stats->rx_frames_delta;
  135. can_stats->current_tx_rate = calc_rate(0, HZ, can_stats->tx_frames_delta);
  136. can_stats->current_rx_rate = calc_rate(0, HZ, can_stats->rx_frames_delta);
  137. /* check / update maximum values */
  138. if (can_stats->max_tx_rate < can_stats->current_tx_rate)
  139. can_stats->max_tx_rate = can_stats->current_tx_rate;
  140. if (can_stats->max_rx_rate < can_stats->current_rx_rate)
  141. can_stats->max_rx_rate = can_stats->current_rx_rate;
  142. if (can_stats->max_rx_match_ratio < can_stats->current_rx_match_ratio)
  143. can_stats->max_rx_match_ratio = can_stats->current_rx_match_ratio;
  144. /* clear values for 'current rate' calculation */
  145. can_stats->tx_frames_delta = 0;
  146. can_stats->rx_frames_delta = 0;
  147. can_stats->matches_delta = 0;
  148. /* restart timer (one second) */
  149. mod_timer(&net->can.can_stattimer, round_jiffies(jiffies + HZ));
  150. }
  151. /*
  152. * proc read functions
  153. */
  154. static void can_print_rcvlist(struct seq_file *m, struct hlist_head *rx_list,
  155. struct net_device *dev)
  156. {
  157. struct receiver *r;
  158. hlist_for_each_entry_rcu(r, rx_list, list) {
  159. char *fmt = (r->can_id & CAN_EFF_FLAG)?
  160. " %-5s %08x %08x %pK %pK %8ld %s\n" :
  161. " %-5s %03x %08x %pK %pK %8ld %s\n";
  162. seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask,
  163. r->func, r->data, r->matches, r->ident);
  164. }
  165. }
  166. static void can_print_recv_banner(struct seq_file *m)
  167. {
  168. /*
  169. * can1. 00000000 00000000 00000000
  170. * ....... 0 tp20
  171. */
  172. seq_puts(m, " device can_id can_mask function"
  173. " userdata matches ident\n");
  174. }
  175. static int can_stats_proc_show(struct seq_file *m, void *v)
  176. {
  177. struct net *net = m->private;
  178. struct s_stats *can_stats = net->can.can_stats;
  179. struct s_pstats *can_pstats = net->can.can_pstats;
  180. seq_putc(m, '\n');
  181. seq_printf(m, " %8ld transmitted frames (TXF)\n", can_stats->tx_frames);
  182. seq_printf(m, " %8ld received frames (RXF)\n", can_stats->rx_frames);
  183. seq_printf(m, " %8ld matched frames (RXMF)\n", can_stats->matches);
  184. seq_putc(m, '\n');
  185. if (net->can.can_stattimer.function == can_stat_update) {
  186. seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
  187. can_stats->total_rx_match_ratio);
  188. seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
  189. can_stats->total_tx_rate);
  190. seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
  191. can_stats->total_rx_rate);
  192. seq_putc(m, '\n');
  193. seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
  194. can_stats->current_rx_match_ratio);
  195. seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
  196. can_stats->current_tx_rate);
  197. seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
  198. can_stats->current_rx_rate);
  199. seq_putc(m, '\n');
  200. seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
  201. can_stats->max_rx_match_ratio);
  202. seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
  203. can_stats->max_tx_rate);
  204. seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
  205. can_stats->max_rx_rate);
  206. seq_putc(m, '\n');
  207. }
  208. seq_printf(m, " %8ld current receive list entries (CRCV)\n",
  209. can_pstats->rcv_entries);
  210. seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
  211. can_pstats->rcv_entries_max);
  212. if (can_pstats->stats_reset)
  213. seq_printf(m, "\n %8ld statistic resets (STR)\n",
  214. can_pstats->stats_reset);
  215. if (can_pstats->user_reset)
  216. seq_printf(m, " %8ld user statistic resets (USTR)\n",
  217. can_pstats->user_reset);
  218. seq_putc(m, '\n');
  219. return 0;
  220. }
  221. static int can_stats_proc_open(struct inode *inode, struct file *file)
  222. {
  223. return single_open_net(inode, file, can_stats_proc_show);
  224. }
  225. static const struct file_operations can_stats_proc_fops = {
  226. .owner = THIS_MODULE,
  227. .open = can_stats_proc_open,
  228. .read = seq_read,
  229. .llseek = seq_lseek,
  230. .release = single_release,
  231. };
  232. static int can_reset_stats_proc_show(struct seq_file *m, void *v)
  233. {
  234. struct net *net = m->private;
  235. struct s_pstats *can_pstats = net->can.can_pstats;
  236. struct s_stats *can_stats = net->can.can_stats;
  237. user_reset = 1;
  238. if (net->can.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(net);
  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_net(inode, file, can_reset_stats_proc_show);
  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_net(inode, file, can_version_proc_show);
  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)PDE_DATA(m->file->f_inode);
  290. struct net_device *dev;
  291. struct dev_rcv_lists *d;
  292. struct net *net = m->private;
  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 = net->can.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(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_net(inode, file, can_rcvlist_proc_show);
  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_proc_show_array(struct seq_file *m,
  319. struct net_device *dev,
  320. struct hlist_head *rcv_array,
  321. unsigned int rcv_array_sz)
  322. {
  323. unsigned int i;
  324. int all_empty = 1;
  325. /* check whether at least one list is non-empty */
  326. for (i = 0; i < rcv_array_sz; i++)
  327. if (!hlist_empty(&rcv_array[i])) {
  328. all_empty = 0;
  329. break;
  330. }
  331. if (!all_empty) {
  332. can_print_recv_banner(m);
  333. for (i = 0; i < rcv_array_sz; i++) {
  334. if (!hlist_empty(&rcv_array[i]))
  335. can_print_rcvlist(m, &rcv_array[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. struct net *net = m->private;
  345. /* RX_SFF */
  346. seq_puts(m, "\nreceive list 'rx_sff':\n");
  347. rcu_read_lock();
  348. /* sff receive list for 'all' CAN devices (dev == NULL) */
  349. d = net->can.can_rx_alldev_list;
  350. can_rcvlist_proc_show_array(m, NULL, d->rx_sff, ARRAY_SIZE(d->rx_sff));
  351. /* sff receive list for registered CAN devices */
  352. for_each_netdev_rcu(net, dev) {
  353. if (dev->type == ARPHRD_CAN && dev->ml_priv) {
  354. d = dev->ml_priv;
  355. can_rcvlist_proc_show_array(m, dev, d->rx_sff,
  356. ARRAY_SIZE(d->rx_sff));
  357. }
  358. }
  359. rcu_read_unlock();
  360. seq_putc(m, '\n');
  361. return 0;
  362. }
  363. static int can_rcvlist_sff_proc_open(struct inode *inode, struct file *file)
  364. {
  365. return single_open_net(inode, file, can_rcvlist_sff_proc_show);
  366. }
  367. static const struct file_operations can_rcvlist_sff_proc_fops = {
  368. .owner = THIS_MODULE,
  369. .open = can_rcvlist_sff_proc_open,
  370. .read = seq_read,
  371. .llseek = seq_lseek,
  372. .release = single_release,
  373. };
  374. static int can_rcvlist_eff_proc_show(struct seq_file *m, void *v)
  375. {
  376. struct net_device *dev;
  377. struct dev_rcv_lists *d;
  378. struct net *net = m->private;
  379. /* RX_EFF */
  380. seq_puts(m, "\nreceive list 'rx_eff':\n");
  381. rcu_read_lock();
  382. /* eff receive list for 'all' CAN devices (dev == NULL) */
  383. d = net->can.can_rx_alldev_list;
  384. can_rcvlist_proc_show_array(m, NULL, d->rx_eff, ARRAY_SIZE(d->rx_eff));
  385. /* eff receive list for registered CAN devices */
  386. for_each_netdev_rcu(net, dev) {
  387. if (dev->type == ARPHRD_CAN && dev->ml_priv) {
  388. d = dev->ml_priv;
  389. can_rcvlist_proc_show_array(m, dev, d->rx_eff,
  390. ARRAY_SIZE(d->rx_eff));
  391. }
  392. }
  393. rcu_read_unlock();
  394. seq_putc(m, '\n');
  395. return 0;
  396. }
  397. static int can_rcvlist_eff_proc_open(struct inode *inode, struct file *file)
  398. {
  399. return single_open_net(inode, file, can_rcvlist_eff_proc_show);
  400. }
  401. static const struct file_operations can_rcvlist_eff_proc_fops = {
  402. .owner = THIS_MODULE,
  403. .open = can_rcvlist_eff_proc_open,
  404. .read = seq_read,
  405. .llseek = seq_lseek,
  406. .release = single_release,
  407. };
  408. /*
  409. * can_init_proc - create main CAN proc directory and procfs entries
  410. */
  411. void can_init_proc(struct net *net)
  412. {
  413. /* create /proc/net/can directory */
  414. net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net);
  415. if (!net->can.proc_dir) {
  416. printk(KERN_INFO "can: failed to create /proc/net/can . "
  417. "CONFIG_PROC_FS missing?\n");
  418. return;
  419. }
  420. /* own procfs entries from the AF_CAN core */
  421. net->can.pde_version = proc_create(CAN_PROC_VERSION, 0644,
  422. net->can.proc_dir,
  423. &can_version_proc_fops);
  424. net->can.pde_stats = proc_create(CAN_PROC_STATS, 0644,
  425. net->can.proc_dir,
  426. &can_stats_proc_fops);
  427. net->can.pde_reset_stats = proc_create(CAN_PROC_RESET_STATS, 0644,
  428. net->can.proc_dir,
  429. &can_reset_stats_proc_fops);
  430. net->can.pde_rcvlist_err = proc_create_data(CAN_PROC_RCVLIST_ERR, 0644,
  431. net->can.proc_dir,
  432. &can_rcvlist_proc_fops,
  433. (void *)RX_ERR);
  434. net->can.pde_rcvlist_all = proc_create_data(CAN_PROC_RCVLIST_ALL, 0644,
  435. net->can.proc_dir,
  436. &can_rcvlist_proc_fops,
  437. (void *)RX_ALL);
  438. net->can.pde_rcvlist_fil = proc_create_data(CAN_PROC_RCVLIST_FIL, 0644,
  439. net->can.proc_dir,
  440. &can_rcvlist_proc_fops,
  441. (void *)RX_FIL);
  442. net->can.pde_rcvlist_inv = proc_create_data(CAN_PROC_RCVLIST_INV, 0644,
  443. net->can.proc_dir,
  444. &can_rcvlist_proc_fops,
  445. (void *)RX_INV);
  446. net->can.pde_rcvlist_eff = proc_create(CAN_PROC_RCVLIST_EFF, 0644,
  447. net->can.proc_dir,
  448. &can_rcvlist_eff_proc_fops);
  449. net->can.pde_rcvlist_sff = proc_create(CAN_PROC_RCVLIST_SFF, 0644,
  450. net->can.proc_dir,
  451. &can_rcvlist_sff_proc_fops);
  452. }
  453. /*
  454. * can_remove_proc - remove procfs entries and main CAN proc directory
  455. */
  456. void can_remove_proc(struct net *net)
  457. {
  458. if (!net->can.proc_dir)
  459. return;
  460. if (net->can.pde_version)
  461. remove_proc_entry(CAN_PROC_VERSION, net->can.proc_dir);
  462. if (net->can.pde_stats)
  463. remove_proc_entry(CAN_PROC_STATS, net->can.proc_dir);
  464. if (net->can.pde_reset_stats)
  465. remove_proc_entry(CAN_PROC_RESET_STATS, net->can.proc_dir);
  466. if (net->can.pde_rcvlist_err)
  467. remove_proc_entry(CAN_PROC_RCVLIST_ERR, net->can.proc_dir);
  468. if (net->can.pde_rcvlist_all)
  469. remove_proc_entry(CAN_PROC_RCVLIST_ALL, net->can.proc_dir);
  470. if (net->can.pde_rcvlist_fil)
  471. remove_proc_entry(CAN_PROC_RCVLIST_FIL, net->can.proc_dir);
  472. if (net->can.pde_rcvlist_inv)
  473. remove_proc_entry(CAN_PROC_RCVLIST_INV, net->can.proc_dir);
  474. if (net->can.pde_rcvlist_eff)
  475. remove_proc_entry(CAN_PROC_RCVLIST_EFF, net->can.proc_dir);
  476. if (net->can.pde_rcvlist_sff)
  477. remove_proc_entry(CAN_PROC_RCVLIST_SFF, net->can.proc_dir);
  478. remove_proc_entry("can", net->proc_net);
  479. }