blob: e9693aed7ef8f1cce3f87bc21ea2ba94ec582b38 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h> /* for EXPORT_SYMBOL */
12#include <linux/string.h>
13#include <linux/types.h>
14#include <linux/mm.h>
15#include <linux/fs.h>
16#include <linux/stat.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
19#include <linux/errno.h>
20#include <linux/atm.h>
21#include <linux/atmdev.h>
22#include <linux/netdevice.h>
23#include <linux/atmclip.h>
24#include <linux/init.h> /* for __init */
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020025#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/atmclip.h>
27#include <asm/uaccess.h>
28#include <asm/atomic.h>
29#include <asm/param.h> /* for HZ */
30#include "resources.h"
31#include "common.h" /* atm_proc_init prototype */
32#include "signaling.h" /* to get sigd - ugly too */
33
34static ssize_t proc_dev_atm_read(struct file *file,char __user *buf,size_t count,
35 loff_t *pos);
36
Arjan van de Ven9a321442007-02-12 00:55:35 -080037static const struct file_operations proc_atm_dev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .owner = THIS_MODULE,
39 .read = proc_dev_atm_read,
40};
41
42static void add_stats(struct seq_file *seq, const char *aal,
43 const struct k_atm_aal_stats *stats)
44{
45 seq_printf(seq, "%s ( %d %d %d %d %d )", aal,
46 atomic_read(&stats->tx),atomic_read(&stats->tx_err),
47 atomic_read(&stats->rx),atomic_read(&stats->rx_err),
48 atomic_read(&stats->rx_drop));
49}
50
51static void atm_dev_info(struct seq_file *seq, const struct atm_dev *dev)
52{
53 int i;
54
55 seq_printf(seq, "%3d %-8s", dev->number, dev->type);
56 for (i = 0; i < ESI_LEN; i++)
57 seq_printf(seq, "%02x", dev->esi[i]);
58 seq_puts(seq, " ");
59 add_stats(seq, "0", &dev->stats.aal0);
60 seq_puts(seq, " ");
61 add_stats(seq, "5", &dev->stats.aal5);
62 seq_printf(seq, "\t[%d]", atomic_read(&dev->refcnt));
63 seq_putc(seq, '\n');
64}
65
66struct vcc_state {
67 int bucket;
68 struct sock *sk;
69 int family;
70};
71
72static inline int compare_family(struct sock *sk, int family)
73{
74 return !family || (sk->sk_family == family);
75}
76
77static int __vcc_walk(struct sock **sock, int family, int *bucket, loff_t l)
78{
79 struct sock *sk = *sock;
80
81 if (sk == (void *)1) {
82 for (*bucket = 0; *bucket < VCC_HTABLE_SIZE; ++*bucket) {
83 struct hlist_head *head = &vcc_hash[*bucket];
84
85 sk = hlist_empty(head) ? NULL : __sk_head(head);
86 if (sk)
87 break;
88 }
89 l--;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091try_again:
92 for (; sk; sk = sk_next(sk)) {
93 l -= compare_family(sk, family);
94 if (l < 0)
95 goto out;
96 }
97 if (!sk && ++*bucket < VCC_HTABLE_SIZE) {
98 sk = sk_head(&vcc_hash[*bucket]);
99 goto try_again;
100 }
101 sk = (void *)1;
102out:
103 *sock = sk;
104 return (l < 0);
105}
106
107static inline void *vcc_walk(struct vcc_state *state, loff_t l)
108{
109 return __vcc_walk(&state->sk, state->family, &state->bucket, l) ?
110 state : NULL;
111}
112
113static int __vcc_seq_open(struct inode *inode, struct file *file,
Al Viroececfde2007-07-15 21:01:12 +0100114 int family, const struct seq_operations *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct vcc_state *state;
117 struct seq_file *seq;
118 int rc = -ENOMEM;
119
120 state = kmalloc(sizeof(*state), GFP_KERNEL);
121 if (!state)
122 goto out;
123
124 rc = seq_open(file, ops);
125 if (rc)
126 goto out_kfree;
127
128 state->family = family;
129
130 seq = file->private_data;
131 seq->private = state;
132out:
133 return rc;
134out_kfree:
135 kfree(state);
136 goto out;
137}
138
139static int vcc_seq_release(struct inode *inode, struct file *file)
140{
141 return seq_release_private(inode, file);
142}
143
144static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800145 __acquires(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct vcc_state *state = seq->private;
148 loff_t left = *pos;
149
150 read_lock(&vcc_sklist_lock);
151 state->sk = (void *)1;
152 return left ? vcc_walk(state, left) : (void *)1;
153}
154
155static void vcc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800156 __releases(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 read_unlock(&vcc_sklist_lock);
159}
160
161static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
162{
163 struct vcc_state *state = seq->private;
164
165 v = vcc_walk(state, 1);
166 *pos += !!PTR_ERR(v);
167 return v;
168}
169
170static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
171{
172 static const char *class_name[] = { "off","UBR","CBR","VBR","ABR" };
173 static const char *aal_name[] = {
174 "---", "1", "2", "3/4", /* 0- 3 */
175 "???", "5", "???", "???", /* 4- 7 */
176 "???", "???", "???", "???", /* 8-11 */
177 "???", "0", "???", "???"}; /* 12-15 */
178
179 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
180 vcc->dev->number,vcc->vpi,vcc->vci,
Denis Cheng8b14a532007-09-16 16:41:29 -0700181 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 aal_name[vcc->qos.aal],vcc->qos.rxtp.min_pcr,
183 class_name[vcc->qos.rxtp.traffic_class],vcc->qos.txtp.min_pcr,
184 class_name[vcc->qos.txtp.traffic_class]);
185 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
186 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
187 struct net_device *dev;
188
189 dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
190 seq_printf(seq, "CLIP, Itf:%s, Encap:",
191 dev ? dev->name : "none?");
192 seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
193 }
194 seq_putc(seq, '\n');
195}
196
197static const char *vcc_state(struct atm_vcc *vcc)
198{
199 static const char *map[] = { ATM_VS2TXT_MAP };
200
201 return map[ATM_VF2VS(vcc->flags)];
202}
203
204static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
205{
206 struct sock *sk = sk_atm(vcc);
207
208 seq_printf(seq, "%p ", vcc);
209 if (!vcc->dev)
210 seq_printf(seq, "Unassigned ");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900211 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
213 vcc->vci);
214 switch (sk->sk_family) {
215 case AF_ATMPVC:
216 seq_printf(seq, "PVC");
217 break;
218 case AF_ATMSVC:
219 seq_printf(seq, "SVC");
220 break;
221 default:
222 seq_printf(seq, "%3d", sk->sk_family);
223 }
224 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n", vcc->flags, sk->sk_err,
225 atomic_read(&sk->sk_wmem_alloc), sk->sk_sndbuf,
226 atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf,
227 atomic_read(&sk->sk_refcnt));
228}
229
230static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
231{
232 if (!vcc->dev)
233 seq_printf(seq, sizeof(void *) == 4 ?
234 "N/A@%p%10s" : "N/A@%p%2s", vcc, "");
235 else
236 seq_printf(seq, "%3d %3d %5d ",
237 vcc->dev->number, vcc->vpi, vcc->vci);
238 seq_printf(seq, "%-10s ", vcc_state(vcc));
239 seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
240 *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
241 if (*vcc->remote.sas_addr.prv) {
242 int i;
243
244 for (i = 0; i < ATM_ESA_LEN; i++)
245 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
246 }
247 seq_putc(seq, '\n');
248}
249
250static int atm_dev_seq_show(struct seq_file *seq, void *v)
251{
252 static char atm_dev_banner[] =
253 "Itf Type ESI/\"MAC\"addr "
254 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (v == (void *)1)
257 seq_puts(seq, atm_dev_banner);
258 else {
259 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
260
261 atm_dev_info(seq, dev);
262 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900265
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700266static const struct seq_operations atm_dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .start = atm_dev_seq_start,
268 .next = atm_dev_seq_next,
269 .stop = atm_dev_seq_stop,
270 .show = atm_dev_seq_show,
271};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int atm_dev_seq_open(struct inode *inode, struct file *file)
274{
275 return seq_open(file, &atm_dev_seq_ops);
276}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900277
Arjan van de Ven9a321442007-02-12 00:55:35 -0800278static const struct file_operations devices_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 .open = atm_dev_seq_open,
280 .read = seq_read,
281 .llseek = seq_lseek,
282 .release = seq_release,
283};
284
285static int pvc_seq_show(struct seq_file *seq, void *v)
286{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900287 static char atm_pvc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
289
290 if (v == (void *)1)
291 seq_puts(seq, atm_pvc_banner);
292 else {
293 struct vcc_state *state = seq->private;
294 struct atm_vcc *vcc = atm_sk(state->sk);
295
296 pvc_info(seq, vcc);
297 }
298 return 0;
299}
300
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700301static const struct seq_operations pvc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 .start = vcc_seq_start,
303 .next = vcc_seq_next,
304 .stop = vcc_seq_stop,
305 .show = pvc_seq_show,
306};
307
308static int pvc_seq_open(struct inode *inode, struct file *file)
309{
310 return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops);
311}
312
Arjan van de Ven9a321442007-02-12 00:55:35 -0800313static const struct file_operations pvc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 .open = pvc_seq_open,
315 .read = seq_read,
316 .llseek = seq_lseek,
317 .release = vcc_seq_release,
318};
319
320static int vcc_seq_show(struct seq_file *seq, void *v)
321{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900322 if (v == (void *)1) {
323 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
324 "Address ", "Itf VPI VCI Fam Flags Reply "
325 "Send buffer Recv buffer [refcnt]\n");
326 } else {
327 struct vcc_state *state = seq->private;
328 struct atm_vcc *vcc = atm_sk(state->sk);
329
330 vcc_info(seq, vcc);
331 }
332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900334
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700335static const struct seq_operations vcc_seq_ops = {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900336 .start = vcc_seq_start,
337 .next = vcc_seq_next,
338 .stop = vcc_seq_stop,
339 .show = vcc_seq_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static int vcc_seq_open(struct inode *inode, struct file *file)
343{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900344 return __vcc_seq_open(inode, file, 0, &vcc_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900346
Arjan van de Ven9a321442007-02-12 00:55:35 -0800347static const struct file_operations vcc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 .open = vcc_seq_open,
349 .read = seq_read,
350 .llseek = seq_lseek,
351 .release = vcc_seq_release,
352};
353
354static int svc_seq_show(struct seq_file *seq, void *v)
355{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900356 static char atm_svc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 "Itf VPI VCI State Remote\n";
358
359 if (v == (void *)1)
360 seq_puts(seq, atm_svc_banner);
361 else {
362 struct vcc_state *state = seq->private;
363 struct atm_vcc *vcc = atm_sk(state->sk);
364
365 svc_info(seq, vcc);
366 }
367 return 0;
368}
369
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700370static const struct seq_operations svc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .start = vcc_seq_start,
372 .next = vcc_seq_next,
373 .stop = vcc_seq_stop,
374 .show = svc_seq_show,
375};
376
377static int svc_seq_open(struct inode *inode, struct file *file)
378{
379 return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops);
380}
381
Arjan van de Ven9a321442007-02-12 00:55:35 -0800382static const struct file_operations svc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 .open = svc_seq_open,
384 .read = seq_read,
385 .llseek = seq_lseek,
386 .release = vcc_seq_release,
387};
388
389static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
390 size_t count, loff_t *pos)
391{
392 struct atm_dev *dev;
393 unsigned long page;
394 int length;
395
396 if (count == 0) return 0;
397 page = get_zeroed_page(GFP_KERNEL);
398 if (!page) return -ENOMEM;
Josef Sipek76a0f172006-12-08 02:36:52 -0800399 dev = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!dev->ops->proc_read)
401 length = -EINVAL;
402 else {
403 length = dev->ops->proc_read(dev,pos,(char *) page);
404 if (length > count) length = -EINVAL;
405 }
406 if (length >= 0) {
407 if (copy_to_user(buf,(char *) page,length)) length = -EFAULT;
408 (*pos)++;
409 }
410 free_page(page);
411 return length;
412}
413
414
415struct proc_dir_entry *atm_proc_root;
416EXPORT_SYMBOL(atm_proc_root);
417
418
419int atm_proc_dev_register(struct atm_dev *dev)
420{
421 int digits,num;
422 int error;
423
424 /* No proc info */
425 if (!dev->ops->proc_read)
426 return 0;
427
428 error = -ENOMEM;
429 digits = 0;
430 for (num = dev->number; num; num /= 10) digits++;
431 if (!digits) digits++;
432
433 dev->proc_name = kmalloc(strlen(dev->type) + digits + 2, GFP_KERNEL);
434 if (!dev->proc_name)
435 goto err_out;
436 sprintf(dev->proc_name,"%s:%d",dev->type, dev->number);
437
Wang Chen16e297b2008-02-28 13:55:45 -0800438 dev->proc_entry = proc_create(dev->proc_name, 0, atm_proc_root,
439 &proc_atm_dev_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!dev->proc_entry)
441 goto err_free_name;
442 dev->proc_entry->data = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dev->proc_entry->owner = THIS_MODULE;
444 return 0;
445err_free_name:
446 kfree(dev->proc_name);
447err_out:
448 return error;
449}
450
451
452void atm_proc_dev_deregister(struct atm_dev *dev)
453{
454 if (!dev->ops->proc_read)
455 return;
456
457 remove_proc_entry(dev->proc_name, atm_proc_root);
458 kfree(dev->proc_name);
459}
460
461static struct atm_proc_entry {
462 char *name;
Arjan van de Ven9a321442007-02-12 00:55:35 -0800463 const struct file_operations *proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct proc_dir_entry *dirent;
465} atm_proc_ents[] = {
466 { .name = "devices", .proc_fops = &devices_seq_fops },
467 { .name = "pvc", .proc_fops = &pvc_seq_fops },
468 { .name = "svc", .proc_fops = &svc_seq_fops },
469 { .name = "vc", .proc_fops = &vcc_seq_fops },
470 { .name = NULL, .proc_fops = NULL }
471};
472
473static void atm_proc_dirs_remove(void)
474{
475 static struct atm_proc_entry *e;
476
477 for (e = atm_proc_ents; e->name; e++) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900478 if (e->dirent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 remove_proc_entry(e->name, atm_proc_root);
480 }
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800481 proc_net_remove(&init_net, "atm");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484int __init atm_proc_init(void)
485{
486 static struct atm_proc_entry *e;
487 int ret;
488
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800489 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!atm_proc_root)
491 goto err_out;
492 for (e = atm_proc_ents; e->name; e++) {
493 struct proc_dir_entry *dirent;
494
Wang Chen16e297b2008-02-28 13:55:45 -0800495 dirent = proc_create(e->name, S_IRUGO,
496 atm_proc_root, e->proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!dirent)
498 goto err_out_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dirent->owner = THIS_MODULE;
500 e->dirent = dirent;
501 }
502 ret = 0;
503out:
504 return ret;
505
506err_out_remove:
507 atm_proc_dirs_remove();
508err_out:
509 ret = -ENOMEM;
510 goto out;
511}
512
Kevin Hilmanb9c6e3e2006-08-15 02:02:33 -0700513void atm_proc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 atm_proc_dirs_remove();
516}