proc.c 12 KB

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