proc.c 11 KB

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