proc.c 12 KB

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