proc.c 12 KB

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