blob: 49487b313f221cf6394fa2c918fc67858ae1f45f [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
Joe Perches2e1e9842008-04-10 03:33:03 -070081 if (sk == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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 }
Joe Perches2e1e9842008-04-10 03:33:03 -0700101 sk = SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102out:
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800118 state = __seq_open_private(file, ops, sizeof(*state));
119 if (state == NULL)
120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 state->family = family;
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static void *vcc_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800127 __acquires(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct vcc_state *state = seq->private;
130 loff_t left = *pos;
131
132 read_lock(&vcc_sklist_lock);
Joe Perches2e1e9842008-04-10 03:33:03 -0700133 state->sk = SEQ_START_TOKEN;
134 return left ? vcc_walk(state, left) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137static void vcc_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet5c17d5f2008-01-15 03:29:50 -0800138 __releases(vcc_sklist_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 read_unlock(&vcc_sklist_lock);
141}
142
143static void *vcc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
144{
145 struct vcc_state *state = seq->private;
146
147 v = vcc_walk(state, 1);
148 *pos += !!PTR_ERR(v);
149 return v;
150}
151
152static void pvc_info(struct seq_file *seq, struct atm_vcc *vcc)
153{
154 static const char *class_name[] = { "off","UBR","CBR","VBR","ABR" };
155 static const char *aal_name[] = {
156 "---", "1", "2", "3/4", /* 0- 3 */
157 "???", "5", "???", "???", /* 4- 7 */
158 "???", "???", "???", "???", /* 8-11 */
159 "???", "0", "???", "???"}; /* 12-15 */
160
161 seq_printf(seq, "%3d %3d %5d %-3s %7d %-5s %7d %-6s",
162 vcc->dev->number,vcc->vpi,vcc->vci,
Denis Cheng8b14a532007-09-16 16:41:29 -0700163 vcc->qos.aal >= ARRAY_SIZE(aal_name) ? "err" :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 aal_name[vcc->qos.aal],vcc->qos.rxtp.min_pcr,
165 class_name[vcc->qos.rxtp.traffic_class],vcc->qos.txtp.min_pcr,
166 class_name[vcc->qos.txtp.traffic_class]);
167 if (test_bit(ATM_VF_IS_CLIP, &vcc->flags)) {
168 struct clip_vcc *clip_vcc = CLIP_VCC(vcc);
169 struct net_device *dev;
170
171 dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : NULL;
172 seq_printf(seq, "CLIP, Itf:%s, Encap:",
173 dev ? dev->name : "none?");
174 seq_printf(seq, "%s", clip_vcc->encap ? "LLC/SNAP" : "None");
175 }
176 seq_putc(seq, '\n');
177}
178
179static const char *vcc_state(struct atm_vcc *vcc)
180{
181 static const char *map[] = { ATM_VS2TXT_MAP };
182
183 return map[ATM_VF2VS(vcc->flags)];
184}
185
186static void vcc_info(struct seq_file *seq, struct atm_vcc *vcc)
187{
188 struct sock *sk = sk_atm(vcc);
189
190 seq_printf(seq, "%p ", vcc);
191 if (!vcc->dev)
192 seq_printf(seq, "Unassigned ");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900193 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 seq_printf(seq, "%3d %3d %5d ", vcc->dev->number, vcc->vpi,
195 vcc->vci);
196 switch (sk->sk_family) {
197 case AF_ATMPVC:
198 seq_printf(seq, "PVC");
199 break;
200 case AF_ATMSVC:
201 seq_printf(seq, "SVC");
202 break;
203 default:
204 seq_printf(seq, "%3d", sk->sk_family);
205 }
206 seq_printf(seq, " %04lx %5d %7d/%7d %7d/%7d [%d]\n", vcc->flags, sk->sk_err,
207 atomic_read(&sk->sk_wmem_alloc), sk->sk_sndbuf,
208 atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf,
209 atomic_read(&sk->sk_refcnt));
210}
211
212static void svc_info(struct seq_file *seq, struct atm_vcc *vcc)
213{
214 if (!vcc->dev)
215 seq_printf(seq, sizeof(void *) == 4 ?
216 "N/A@%p%10s" : "N/A@%p%2s", vcc, "");
217 else
218 seq_printf(seq, "%3d %3d %5d ",
219 vcc->dev->number, vcc->vpi, vcc->vci);
220 seq_printf(seq, "%-10s ", vcc_state(vcc));
221 seq_printf(seq, "%s%s", vcc->remote.sas_addr.pub,
222 *vcc->remote.sas_addr.pub && *vcc->remote.sas_addr.prv ? "+" : "");
223 if (*vcc->remote.sas_addr.prv) {
224 int i;
225
226 for (i = 0; i < ATM_ESA_LEN; i++)
227 seq_printf(seq, "%02x", vcc->remote.sas_addr.prv[i]);
228 }
229 seq_putc(seq, '\n');
230}
231
232static int atm_dev_seq_show(struct seq_file *seq, void *v)
233{
234 static char atm_dev_banner[] =
235 "Itf Type ESI/\"MAC\"addr "
236 "AAL(TX,err,RX,err,drop) ... [refcnt]\n";
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900237
Joe Perches2e1e9842008-04-10 03:33:03 -0700238 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 seq_puts(seq, atm_dev_banner);
240 else {
241 struct atm_dev *dev = list_entry(v, struct atm_dev, dev_list);
242
243 atm_dev_info(seq, dev);
244 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900247
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700248static const struct seq_operations atm_dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .start = atm_dev_seq_start,
250 .next = atm_dev_seq_next,
251 .stop = atm_dev_seq_stop,
252 .show = atm_dev_seq_show,
253};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int atm_dev_seq_open(struct inode *inode, struct file *file)
256{
257 return seq_open(file, &atm_dev_seq_ops);
258}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900259
Arjan van de Ven9a321442007-02-12 00:55:35 -0800260static const struct file_operations devices_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .open = atm_dev_seq_open,
262 .read = seq_read,
263 .llseek = seq_lseek,
264 .release = seq_release,
265};
266
267static int pvc_seq_show(struct seq_file *seq, void *v)
268{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900269 static char atm_pvc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 "Itf VPI VCI AAL RX(PCR,Class) TX(PCR,Class)\n";
271
Joe Perches2e1e9842008-04-10 03:33:03 -0700272 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 seq_puts(seq, atm_pvc_banner);
274 else {
275 struct vcc_state *state = seq->private;
276 struct atm_vcc *vcc = atm_sk(state->sk);
277
278 pvc_info(seq, vcc);
279 }
280 return 0;
281}
282
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700283static const struct seq_operations pvc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .start = vcc_seq_start,
285 .next = vcc_seq_next,
286 .stop = vcc_seq_stop,
287 .show = pvc_seq_show,
288};
289
290static int pvc_seq_open(struct inode *inode, struct file *file)
291{
292 return __vcc_seq_open(inode, file, PF_ATMPVC, &pvc_seq_ops);
293}
294
Arjan van de Ven9a321442007-02-12 00:55:35 -0800295static const struct file_operations pvc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .open = pvc_seq_open,
297 .read = seq_read,
298 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800299 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302static int vcc_seq_show(struct seq_file *seq, void *v)
303{
Joe Perches2e1e9842008-04-10 03:33:03 -0700304 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900305 seq_printf(seq, sizeof(void *) == 4 ? "%-8s%s" : "%-16s%s",
306 "Address ", "Itf VPI VCI Fam Flags Reply "
307 "Send buffer Recv buffer [refcnt]\n");
308 } else {
309 struct vcc_state *state = seq->private;
310 struct atm_vcc *vcc = atm_sk(state->sk);
311
312 vcc_info(seq, vcc);
313 }
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900316
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700317static const struct seq_operations vcc_seq_ops = {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900318 .start = vcc_seq_start,
319 .next = vcc_seq_next,
320 .stop = vcc_seq_stop,
321 .show = vcc_seq_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int vcc_seq_open(struct inode *inode, struct file *file)
325{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900326 return __vcc_seq_open(inode, file, 0, &vcc_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900328
Arjan van de Ven9a321442007-02-12 00:55:35 -0800329static const struct file_operations vcc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 .open = vcc_seq_open,
331 .read = seq_read,
332 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800333 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336static int svc_seq_show(struct seq_file *seq, void *v)
337{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900338 static char atm_svc_banner[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 "Itf VPI VCI State Remote\n";
340
Joe Perches2e1e9842008-04-10 03:33:03 -0700341 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 seq_puts(seq, atm_svc_banner);
343 else {
344 struct vcc_state *state = seq->private;
345 struct atm_vcc *vcc = atm_sk(state->sk);
346
347 svc_info(seq, vcc);
348 }
349 return 0;
350}
351
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700352static const struct seq_operations svc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .start = vcc_seq_start,
354 .next = vcc_seq_next,
355 .stop = vcc_seq_stop,
356 .show = svc_seq_show,
357};
358
359static int svc_seq_open(struct inode *inode, struct file *file)
360{
361 return __vcc_seq_open(inode, file, PF_ATMSVC, &svc_seq_ops);
362}
363
Arjan van de Ven9a321442007-02-12 00:55:35 -0800364static const struct file_operations svc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 .open = svc_seq_open,
366 .read = seq_read,
367 .llseek = seq_lseek,
Pavel Emelyanov9a8c09e2008-02-29 11:37:02 -0800368 .release = seq_release_private,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371static ssize_t proc_dev_atm_read(struct file *file, char __user *buf,
372 size_t count, loff_t *pos)
373{
374 struct atm_dev *dev;
375 unsigned long page;
376 int length;
377
378 if (count == 0) return 0;
379 page = get_zeroed_page(GFP_KERNEL);
380 if (!page) return -ENOMEM;
Josef Sipek76a0f172006-12-08 02:36:52 -0800381 dev = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!dev->ops->proc_read)
383 length = -EINVAL;
384 else {
385 length = dev->ops->proc_read(dev,pos,(char *) page);
386 if (length > count) length = -EINVAL;
387 }
388 if (length >= 0) {
389 if (copy_to_user(buf,(char *) page,length)) length = -EFAULT;
390 (*pos)++;
391 }
392 free_page(page);
393 return length;
394}
395
396
397struct proc_dir_entry *atm_proc_root;
398EXPORT_SYMBOL(atm_proc_root);
399
400
401int atm_proc_dev_register(struct atm_dev *dev)
402{
403 int digits,num;
404 int error;
405
406 /* No proc info */
407 if (!dev->ops->proc_read)
408 return 0;
409
410 error = -ENOMEM;
411 digits = 0;
412 for (num = dev->number; num; num /= 10) digits++;
413 if (!digits) digits++;
414
415 dev->proc_name = kmalloc(strlen(dev->type) + digits + 2, GFP_KERNEL);
416 if (!dev->proc_name)
417 goto err_out;
418 sprintf(dev->proc_name,"%s:%d",dev->type, dev->number);
419
Denis V. Lunev0c896522008-05-02 04:08:30 -0700420 dev->proc_entry = proc_create_data(dev->proc_name, 0, atm_proc_root,
421 &proc_atm_dev_ops, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!dev->proc_entry)
423 goto err_free_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425err_free_name:
426 kfree(dev->proc_name);
427err_out:
428 return error;
429}
430
431
432void atm_proc_dev_deregister(struct atm_dev *dev)
433{
434 if (!dev->ops->proc_read)
435 return;
436
437 remove_proc_entry(dev->proc_name, atm_proc_root);
438 kfree(dev->proc_name);
439}
440
441static struct atm_proc_entry {
442 char *name;
Arjan van de Ven9a321442007-02-12 00:55:35 -0800443 const struct file_operations *proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct proc_dir_entry *dirent;
445} atm_proc_ents[] = {
446 { .name = "devices", .proc_fops = &devices_seq_fops },
447 { .name = "pvc", .proc_fops = &pvc_seq_fops },
448 { .name = "svc", .proc_fops = &svc_seq_fops },
449 { .name = "vc", .proc_fops = &vcc_seq_fops },
450 { .name = NULL, .proc_fops = NULL }
451};
452
453static void atm_proc_dirs_remove(void)
454{
455 static struct atm_proc_entry *e;
456
457 for (e = atm_proc_ents; e->name; e++) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900458 if (e->dirent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 remove_proc_entry(e->name, atm_proc_root);
460 }
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800461 proc_net_remove(&init_net, "atm");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464int __init atm_proc_init(void)
465{
466 static struct atm_proc_entry *e;
467 int ret;
468
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800469 atm_proc_root = proc_net_mkdir(&init_net, "atm", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!atm_proc_root)
471 goto err_out;
472 for (e = atm_proc_ents; e->name; e++) {
473 struct proc_dir_entry *dirent;
474
Wang Chen16e297b2008-02-28 13:55:45 -0800475 dirent = proc_create(e->name, S_IRUGO,
476 atm_proc_root, e->proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (!dirent)
478 goto err_out_remove;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dirent->owner = THIS_MODULE;
480 e->dirent = dirent;
481 }
482 ret = 0;
483out:
484 return ret;
485
486err_out_remove:
487 atm_proc_dirs_remove();
488err_out:
489 ret = -ENOMEM;
490 goto out;
491}
492
Kevin Hilmanb9c6e3e2006-08-15 02:02:33 -0700493void atm_proc_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 atm_proc_dirs_remove();
496}