proc.c 12 KB

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