proc.c 12 KB

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