proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* net/atm/proc.c - ATM /proc interface
  2. *
  3. * Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA
  4. *
  5. * seq_file api usage by romieu@fr.zoreil.com
  6. *
  7. * Evaluating the efficiency of the whole thing if left as an exercise to
  8. * the reader.
  9. */
  10. #include <linux/module.h> /* for EXPORT_SYMBOL */
  11. #include <linux/string.h>
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/fs.h>
  15. #include <linux/stat.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/errno.h>
  19. #include <linux/atm.h>
  20. #include <linux/atmdev.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/atmclip.h>
  23. #include <linux/init.h> /* for __init */
  24. #include <linux/slab.h>
  25. #include <net/net_namespace.h>
  26. #include <net/atmclip.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/param.h> /* for HZ */
  29. #include <linux/atomic.h>
  30. #include "resources.h"
  31. #include "common.h" /* atm_proc_init prototype */
  32. #include "signaling.h" /* to get sigd - ugly too */
  33. static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
  34. size_t count, loff_t *pos);
  35. static const struct file_operations proc_atm_dev_ops = {
  36. .owner = THIS_MODULE,
  37. .read = proc_dev_atm_read,
  38. .llseek = noop_llseek,
  39. };
  40. static void add_stats(struct seq_file *seq, const char *aal,
  41. const struct k_atm_aal_stats *stats)
  42. {
  43. seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
  44. atomic_read(&stats->tx), atomic_read(&stats->tx_err),
  45. atomic_read(&stats->rx), atomic_read(&stats->rx_err),
  46. atomic_read(&stats->rx_drop));
  47. }
  48. static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
  49. {
  50. int i;
  51. seq_printf(seq, "%3d %-8s", dev->number, dev->type);
  52. for (i = 0; i < ESI_LEN; i++)
  53. seq_printf(seq, "%02x", dev->esi[i]);
  54. seq_puts(seq, " ");
  55. add_stats(seq, "0", &dev->stats.aal0);
  56. seq_puts(seq, " ");
  57. add_stats(seq, "5", &dev->stats.aal5);
  58. seq_printf(seq, "\t[%d]", atomic_read(&dev->refcnt));
  59. seq_putc(seq, '\n');
  60. }
  61. struct vcc_state {
  62. int bucket;
  63. struct sock *sk;
  64. int family;
  65. };
  66. static inline int compare_family(struct sock *sk, int family)
  67. {
  68. return !family || (sk->sk_family == family);
  69. }
  70. static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
  71. {
  72. struct sock *sk = *sock;
  73. if (sk == SEQ_START_TOKEN) {
  74. for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
  75. struct hlist_head *head = &vcc_hash[*bucket];
  76. sk = hlist_empty(head) ? NULL : __sk_head(head);
  77. if (sk)
  78. break;
  79. }
  80. l--;
  81. }
  82. try_again:
  83. for (; sk; sk = sk_next(sk)) {
  84. l -= compare_family(sk, family);
  85. if (l < 0)
  86. goto out;
  87. }
  88. if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
  89. sk = sk_head(&vcc_hash[*bucket]);
  90. goto try_again;
  91. }
  92. sk = SEQ_START_TOKEN;
  93. out:
  94. *sock = sk;
  95. return (l < 0);
  96. }
  97. static inline void *vcc_walk(struct vcc_state *state, loff_t l)
  98. {
  99. return __vcc_walk(&state->sk, state->family, &state->bucket, l) ?
  100. state : NULL;
  101. }
  102. static int __vcc_seq_open(struct inode *inode, struct file *file,
  103. int family, const struct seq_operations *ops)
  104. {
  105. struct vcc_state *state;
  106. state = __seq_open_private(file, ops, sizeof(*state));
  107. if (state == NULL)
  108. return -ENOMEM;
  109. state->family = family;
  110. return 0;
  111. }
  112. static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
  113. __acquires(vcc_sklist_lock)
  114. {
  115. struct vcc_state *state = seq->private;
  116. loff_t left = *pos;
  117. read_lock(&vcc_sklist_lock);
  118. state->sk = SEQ_START_TOKEN;
  119. return left ? vcc_walk(state, left) : SEQ_START_TOKEN;
  120. }
  121. static void vcc_seq_stop(struct seq_file *seq, void *v)
  122. __releases(vcc_sklist_lock)
  123. {
  124. read_unlock(&vcc_sklist_lock);
  125. }
  126. static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  127. {
  128. struct vcc_state *state = seq->private;
  129. v = vcc_walk(state, 1);
  130. *pos += !!PTR_ERR(v);
  131. return v;
  132. }
  133. static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
  134. {
  135. static const char *const class_name[] = {
  136. "off", "UBR", "CBR", "VBR", "ABR"};
  137. static const char *const aal_name[] = {
  138. "---", "1", "2", "3/4", /* 0- 3 */
  139. "???", "5", "???", "???", /* 4- 7 */
  140. "???", "???", "???", "???", /* 8-11 */
  141. "???", "0", "???", "???"}; /* 12-15 */
  142. seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
  143. vcc->dev->number, vcc->vpi, vcc->vci,
  144. vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
  145. aal_name[vcc->qos.aal], vcc->qos.rxtp.min_pcr,
  146. class_name[vcc->qos.rxtp.traffic_class],
  147. vcc->qos.txtp.min_pcr,
  148. class_name[vcc->qos.txtp.traffic_class]);
  149. if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
  150. struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
  151. struct net_device *dev;
  152. dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
  153. seq_printf(seq, "CLIP, Itf:%s, Encap:",
  154. dev ? dev->name : "none?");
  155. seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
  156. }
  157. seq_putc(seq, '\n');
  158. }
  159. static const char *vcc_state(struct atm_vcc *vcc)
  160. {
  161. static const char *const map[] = { ATM_VS2TXT_MAP };
  162. return map[ATM_VF2VS(vcc->flags)];
  163. }
  164. static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
  165. {
  166. struct sock *sk = sk_atm(vcc);
  167. seq_printf(seq, "%pK ", vcc);
  168. if (!vcc->dev)
  169. seq_printf(seq, "Unassigned ");
  170. else
  171. seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
  172. vcc->vci);
  173. switch (sk->sk_family) {
  174. case AF_ATMPVC:
  175. seq_printf(seq, "PVC");
  176. break;
  177. case AF_ATMSVC:
  178. seq_printf(seq, "SVC");
  179. break;
  180. default:
  181. seq_printf(seq, "%3d", sk->sk_family);
  182. }
  183. seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n",
  184. vcc->flags, sk->sk_err,
  185. sk_wmem_alloc_get(sk), sk->sk_sndbuf,
  186. sk_rmem_alloc_get(sk), sk->sk_rcvbuf,
  187. atomic_read(&sk->sk_refcnt));
  188. }
  189. static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
  190. {
  191. if (!vcc->dev)
  192. seq_printf(seq, sizeof(void *) == 4 ?
  193. "N/A@%pK%10s" : "N/A@%pK%2s", vcc, "");
  194. else
  195. seq_printf(seq, "%3d %3d %5d ",
  196. vcc->dev->number, vcc->vpi, vcc->vci);
  197. seq_printf(seq, "%-10s ", vcc_state(vcc));
  198. seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
  199. *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
  200. if (*vcc->remote.sas_addr.prv) {
  201. int i;
  202. for (i = 0; i < ATM_ESA_LEN; i++)
  203. seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
  204. }
  205. seq_putc(seq, '\n');
  206. }
  207. static int atm_dev_seq_show(struct seq_file *seq, void *v)
  208. {
  209. static char atm_dev_banner[] =
  210. "Itf Type ESI/\"MAC\"addr "
  211. "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
  212. if (v == &atm_devs)
  213. seq_puts(seq, atm_dev_banner);
  214. else {
  215. struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
  216. atm_dev_info(seq, dev);
  217. }
  218. return 0;
  219. }
  220. static const struct seq_operations atm_dev_seq_ops = {
  221. .start = atm_dev_seq_start,
  222. .next = atm_dev_seq_next,
  223. .stop = atm_dev_seq_stop,
  224. .show = atm_dev_seq_show,
  225. };
  226. static int atm_dev_seq_open(struct inode *inode, struct file *file)
  227. {
  228. return seq_open(file, &atm_dev_seq_ops);
  229. }
  230. static const struct file_operations devices_seq_fops = {
  231. .open = atm_dev_seq_open,
  232. .read = seq_read,
  233. .llseek = seq_lseek,
  234. .release = seq_release,
  235. };
  236. static int pvc_seq_show(struct seq_file *seq, void *v)
  237. {
  238. static char atm_pvc_banner[] =
  239. "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
  240. if (v == SEQ_START_TOKEN)
  241. seq_puts(seq, atm_pvc_banner);
  242. else {
  243. struct vcc_state *state = seq->private;
  244. struct atm_vcc *vcc = atm_sk(state->sk);
  245. pvc_info(seq, vcc);
  246. }
  247. return 0;
  248. }
  249. static const struct seq_operations pvc_seq_ops = {
  250. .start = vcc_seq_start,
  251. .next = vcc_seq_next,
  252. .stop = vcc_seq_stop,
  253. .show = pvc_seq_show,
  254. };
  255. static int pvc_seq_open(struct inode *inode, struct file *file)
  256. {
  257. return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops);
  258. }
  259. static const struct file_operations pvc_seq_fops = {
  260. .open = pvc_seq_open,
  261. .read = seq_read,
  262. .llseek = seq_lseek,
  263. .release = seq_release_private,
  264. };
  265. static int vcc_seq_show(struct seq_file *seq, void *v)
  266. {
  267. if (v == SEQ_START_TOKEN) {
  268. seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
  269. "Address ", "Itf VPI VCI Fam Flags Reply "
  270. "Send buffer Recv buffer [refcnt]\n");
  271. } else {
  272. struct vcc_state *state = seq->private;
  273. struct atm_vcc *vcc = atm_sk(state->sk);
  274. vcc_info(seq, vcc);
  275. }
  276. return 0;
  277. }
  278. static const struct seq_operations vcc_seq_ops = {
  279. .start = vcc_seq_start,
  280. .next = vcc_seq_next,
  281. .stop = vcc_seq_stop,
  282. .show = vcc_seq_show,
  283. };
  284. static int vcc_seq_open(struct inode *inode, struct file *file)
  285. {
  286. return __vcc_seq_open(inode, file, 0, &vcc_seq_ops);
  287. }
  288. static const struct file_operations vcc_seq_fops = {
  289. .open = vcc_seq_open,
  290. .read = seq_read,
  291. .llseek = seq_lseek,
  292. .release = seq_release_private,
  293. };
  294. static int svc_seq_show(struct seq_file *seq, void *v)
  295. {
  296. static const char atm_svc_banner[] =
  297. "Itf VPI VCI State Remote\n";
  298. if (v == SEQ_START_TOKEN)
  299. seq_puts(seq, atm_svc_banner);
  300. else {
  301. struct vcc_state *state = seq->private;
  302. struct atm_vcc *vcc = atm_sk(state->sk);
  303. svc_info(seq, vcc);
  304. }
  305. return 0;
  306. }
  307. static const struct seq_operations svc_seq_ops = {
  308. .start = vcc_seq_start,
  309. .next = vcc_seq_next,
  310. .stop = vcc_seq_stop,
  311. .show = svc_seq_show,
  312. };
  313. static int svc_seq_open(struct inode *inode, struct file *file)
  314. {
  315. return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops);
  316. }
  317. static const struct file_operations svc_seq_fops = {
  318. .open = svc_seq_open,
  319. .read = seq_read,
  320. .llseek = seq_lseek,
  321. .release = seq_release_private,
  322. };
  323. static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
  324. size_t count, loff_t *pos)
  325. {
  326. struct atm_dev *dev;
  327. unsigned long page;
  328. int length;
  329. if (count == 0)
  330. return 0;
  331. page = get_zeroed_page(GFP_KERNEL);
  332. if (!page)
  333. return -ENOMEM;
  334. dev = PDE_DATA(file_inode(file));
  335. if (!dev->ops->proc_read)
  336. length = -EINVAL;
  337. else {
  338. length = dev->ops->proc_read(dev, pos, (char *)page);
  339. if (length > count)
  340. length = -EINVAL;
  341. }
  342. if (length >= 0) {
  343. if (copy_to_user(buf, (char *)page, length))
  344. length = -EFAULT;
  345. (*pos)++;
  346. }
  347. free_page(page);
  348. return length;
  349. }
  350. struct proc_dir_entry *atm_proc_root;
  351. EXPORT_SYMBOL(atm_proc_root);
  352. int atm_proc_dev_register(struct atm_dev *dev)
  353. {
  354. int error;
  355. /* No proc info */
  356. if (!dev->ops->proc_read)
  357. return 0;
  358. error = -ENOMEM;
  359. dev->proc_name = kasprintf(GFP_KERNEL, "%s:%d", dev->type, dev->number);
  360. if (!dev->proc_name)
  361. goto err_out;
  362. dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
  363. &proc_atm_dev_ops, dev);
  364. if (!dev->proc_entry)
  365. goto err_free_name;
  366. return 0;
  367. err_free_name:
  368. kfree(dev->proc_name);
  369. err_out:
  370. return error;
  371. }
  372. void atm_proc_dev_deregister(struct atm_dev *dev)
  373. {
  374. if (!dev->ops->proc_read)
  375. return;
  376. remove_proc_entry(dev->proc_name, atm_proc_root);
  377. kfree(dev->proc_name);
  378. }
  379. static struct atm_proc_entry {
  380. char *name;
  381. const struct file_operations *proc_fops;
  382. struct proc_dir_entry *dirent;
  383. } atm_proc_ents[] = {
  384. { .name = "devices", .proc_fops = &devices_seq_fops },
  385. { .name = "pvc", .proc_fops = &pvc_seq_fops },
  386. { .name = "svc", .proc_fops = &svc_seq_fops },
  387. { .name = "vc", .proc_fops = &vcc_seq_fops },
  388. { .name = NULL, .proc_fops = NULL }
  389. };
  390. static void atm_proc_dirs_remove(void)
  391. {
  392. static struct atm_proc_entry *e;
  393. for (e = atm_proc_ents; e->name; e++) {
  394. if (e->dirent)
  395. remove_proc_entry(e->name, atm_proc_root);
  396. }
  397. remove_proc_entry("atm", init_net.proc_net);
  398. }
  399. int __init atm_proc_init(void)
  400. {
  401. static struct atm_proc_entry *e;
  402. int ret;
  403. atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
  404. if (!atm_proc_root)
  405. goto err_out;
  406. for (e = atm_proc_ents; e->name; e++) {
  407. struct proc_dir_entry *dirent;
  408. dirent = proc_create(e->name, S_IRUGO,
  409. atm_proc_root, e->proc_fops);
  410. if (!dirent)
  411. goto err_out_remove;
  412. e->dirent = dirent;
  413. }
  414. ret = 0;
  415. out:
  416. return ret;
  417. err_out_remove:
  418. atm_proc_dirs_remove();
  419. err_out:
  420. ret = -ENOMEM;
  421. goto out;
  422. }
  423. void atm_proc_exit(void)
  424. {
  425. atm_proc_dirs_remove();
  426. }