commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / net / 8021q / vlanproc.c
1 /******************************************************************************
2  * vlanproc.c   VLAN Module. /proc filesystem interface.
3  *
4  *              This module is completely hardware-independent and provides
5  *              access to the router using Linux /proc filesystem.
6  *
7  * Author:      Ben Greear, <greearb@candelatech.com> coppied from wanproc.c
8  *               by: Gene Kozin <genek@compuserve.com>
9  *
10  * Copyright:   (c) 1998 Ben Greear
11  *
12  *              This program is free software; you can redistribute it and/or
13  *              modify it under the terms of the GNU General Public License
14  *              as published by the Free Software Foundation; either version
15  *              2 of the License, or (at your option) any later version.
16  * ============================================================================
17  * Jan 20, 1998        Ben Greear     Initial Version
18  *****************************************************************************/
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/stddef.h>       /* offsetof(), etc. */
23 #include <linux/errno.h>        /* return codes */
24 #include <linux/kernel.h>
25 #include <linux/slab.h>         /* kmalloc(), kfree() */
26 #include <linux/mm.h>           /* verify_area(), etc. */
27 #include <linux/string.h>       /* inline mem*, str* functions */
28 #include <linux/init.h>         /* __initfunc et al. */
29 #include <asm/byteorder.h>      /* htons(), etc. */
30 #include <asm/uaccess.h>        /* copy_to_user */
31 #include <asm/io.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/fs.h>
35 #include <linux/netdevice.h>
36 #include <linux/if_vlan.h>
37 #include "vlanproc.h"
38 #include "vlan.h"
39
40 /****** Function Prototypes *************************************************/
41
42 /* Methods for preparing data for reading proc entries */
43 static int vlan_seq_show(struct seq_file *seq, void *v);
44 static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
45 static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
46 static void vlan_seq_stop(struct seq_file *seq, void *);
47 static int vlandev_seq_show(struct seq_file *seq, void *v);
48
49 /* Miscellaneous */
50 #define SEQ_START_TOKEN         ((void *) 1)
51
52
53 /*
54  *      Global Data
55  */
56
57
58 /*
59  *      Names of the proc directory entries 
60  */
61
62 static const char name_root[]    = "vlan";
63 static const char name_conf[]    = "config";
64
65 /*
66  *      Structures for interfacing with the /proc filesystem.
67  *      VLAN creates its own directory /proc/net/vlan with the folowing
68  *      entries:
69  *      config          device status/configuration
70  *      <device>        entry for each  device
71  */
72
73 /*
74  *      Generic /proc/net/vlan/<file> file and inode operations 
75  */
76
77 static struct seq_operations vlan_seq_ops = {
78         .start = vlan_seq_start,
79         .next = vlan_seq_next,
80         .stop = vlan_seq_stop,
81         .show = vlan_seq_show,
82 };
83
84 static int vlan_seq_open(struct inode *inode, struct file *file)
85 {
86         return seq_open(file, &vlan_seq_ops);
87 }
88
89 static struct file_operations vlan_fops = {
90         .owner   = THIS_MODULE,
91         .open    = vlan_seq_open,
92         .read    = seq_read,
93         .llseek  = seq_lseek,
94         .release = seq_release,
95 };
96
97 /*
98  *      /proc/net/vlan/<device> file and inode operations
99  */
100
101 static int vlandev_seq_open(struct inode *inode, struct file *file)
102 {
103         return single_open(file, vlandev_seq_show, PDE(inode)->data);
104 }
105
106 static struct file_operations vlandev_fops = {
107         .owner = THIS_MODULE,
108         .open    = vlandev_seq_open,
109         .read    = seq_read,
110         .llseek  = seq_lseek,
111         .release = single_release,
112 };
113
114 /*
115  * Proc filesystem derectory entries.
116  */
117
118 /*
119  *      /proc/net/vlan 
120  */
121
122 static struct proc_dir_entry *proc_vlan_dir;
123
124 /*
125  *      /proc/net/vlan/config 
126  */
127
128 static struct proc_dir_entry *proc_vlan_conf;
129
130 /* Strings */
131 static const char *vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
132     [VLAN_NAME_TYPE_RAW_PLUS_VID]       = "VLAN_NAME_TYPE_RAW_PLUS_VID",
133     [VLAN_NAME_TYPE_PLUS_VID_NO_PAD]    = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
134     [VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD]= "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
135     [VLAN_NAME_TYPE_PLUS_VID]           = "VLAN_NAME_TYPE_PLUS_VID",
136 };
137 /*
138  *      Interface functions
139  */
140
141 /*
142  *      Clean up /proc/net/vlan entries
143  */
144
145 void vlan_proc_cleanup(void)
146 {
147         if (proc_vlan_conf)
148                 remove_proc_entry(name_conf, proc_vlan_dir);
149
150         if (proc_vlan_dir)
151                 proc_net_remove(name_root);
152
153         /* Dynamically added entries should be cleaned up as their vlan_device
154          * is removed, so we should not have to take care of it here...
155          */
156 }
157
158 /*
159  *      Create /proc/net/vlan entries
160  */
161
162 int __init vlan_proc_init(void)
163 {
164         proc_vlan_dir = proc_mkdir(name_root, proc_net);
165         if (proc_vlan_dir) {
166                 proc_vlan_conf = create_proc_entry(name_conf,
167                                                    S_IFREG|S_IRUSR|S_IWUSR,
168                                                    proc_vlan_dir);
169                 if (proc_vlan_conf) {
170                         proc_vlan_conf->proc_fops = &vlan_fops;
171                         return 0;
172                 }
173         }
174         vlan_proc_cleanup();
175         return -ENOBUFS;
176 }
177
178 /*
179  *      Add directory entry for VLAN device.
180  */
181
182 int vlan_proc_add_dev (struct net_device *vlandev)
183 {
184         struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
185
186         if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
187                 printk(KERN_ERR
188                        "ERROR:  vlan_proc_add, device -:%s:- is NOT a VLAN\n",
189                        vlandev->name);
190                 return -EINVAL;
191         }
192
193         dev_info->dent = create_proc_entry(vlandev->name,
194                                            S_IFREG|S_IRUSR|S_IWUSR,
195                                            proc_vlan_dir);
196         if (!dev_info->dent)
197                 return -ENOBUFS;
198
199         dev_info->dent->proc_fops = &vlandev_fops;
200         dev_info->dent->data = vlandev;
201
202 #ifdef VLAN_DEBUG
203         printk(KERN_ERR "vlan_proc_add, device -:%s:- being added.\n",
204                vlandev->name);
205 #endif
206         return 0;
207 }
208
209 /*
210  *      Delete directory entry for VLAN device.
211  */
212 int vlan_proc_rem_dev(struct net_device *vlandev)
213 {
214         if (!vlandev) {
215                 printk(VLAN_ERR "%s: invalid argument: %p\n",
216                         __FUNCTION__, vlandev);
217                 return -EINVAL;
218         }
219
220         if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
221                 printk(VLAN_DBG "%s: invalid argument, device: %s is not a VLAN device, priv_flags: 0x%4hX.\n",
222                         __FUNCTION__, vlandev->name, vlandev->priv_flags);
223                 return -EINVAL;
224         }
225
226 #ifdef VLAN_DEBUG
227         printk(VLAN_DBG __FUNCTION__ ": dev: %p\n", vlandev);
228 #endif
229
230         /** NOTE:  This will consume the memory pointed to by dent, it seems. */
231         remove_proc_entry(VLAN_DEV_INFO(vlandev)->dent->name, proc_vlan_dir);
232         VLAN_DEV_INFO(vlandev)->dent = NULL;
233
234         return 0;
235 }
236
237 /****** Proc filesystem entry points ****************************************/
238
239 /*
240  * The following few functions build the content of /proc/net/vlan/config
241  */
242
243 /* starting at dev, find a VLAN device */
244 struct net_device *vlan_skip(struct net_device *dev) 
245 {
246         while (dev && !(dev->priv_flags & IFF_802_1Q_VLAN)) 
247                 dev = dev->next;
248
249         return dev;
250 }
251
252 /* start read of /proc/net/vlan/config */ 
253 static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
254 {
255         struct net_device *dev;
256         loff_t i = 1;
257
258         read_lock(&dev_base_lock);
259
260         if (*pos == 0)
261                 return SEQ_START_TOKEN;
262         
263         for (dev = vlan_skip(dev_base); dev && i < *pos; 
264              dev = vlan_skip(dev->next), ++i);
265                 
266         return  (i == *pos) ? dev : NULL;
267
268
269 static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
270 {
271         ++*pos;
272
273         return vlan_skip((v == SEQ_START_TOKEN)  
274                             ? dev_base 
275                             : ((struct net_device *)v)->next);
276 }
277
278 static void vlan_seq_stop(struct seq_file *seq, void *v)
279 {
280         read_unlock(&dev_base_lock);
281 }
282
283 static int vlan_seq_show(struct seq_file *seq, void *v)
284 {
285         if (v == SEQ_START_TOKEN) {
286                 const char *nmtype = NULL;
287
288                 seq_puts(seq, "VLAN Dev name     | VLAN ID\n");
289
290                 if (vlan_name_type < ARRAY_SIZE(vlan_name_type_str))
291                     nmtype =  vlan_name_type_str[vlan_name_type];
292
293                 seq_printf(seq, "Name-Type: %s\n", 
294                            nmtype ? nmtype :  "UNKNOWN" );
295         } else {
296                 const struct net_device *vlandev = v;
297                 const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
298
299                 seq_printf(seq, "%-15s| %d  | %s\n",  vlandev->name,  
300                            dev_info->vlan_id,    dev_info->real_dev->name);
301         }
302         return 0;
303 }
304
305 static int vlandev_seq_show(struct seq_file *seq, void *offset)
306 {
307         struct net_device *vlandev = (struct net_device *) seq->private;
308         const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
309         struct net_device_stats *stats;
310         static const char *fmt = "%30s %12lu\n";
311         int i;
312
313         if ((vlandev == NULL) || (!(vlandev->priv_flags & IFF_802_1Q_VLAN)))
314                 return 0;
315
316         seq_printf(seq, "%s  VID: %d     REORDER_HDR: %i  dev->priv_flags: %hx\n",
317                        vlandev->name, dev_info->vlan_id,
318                        (int)(dev_info->flags & 1), vlandev->priv_flags);
319
320
321         stats = vlan_dev_get_stats(vlandev);
322
323         seq_printf(seq, fmt, "total frames received", stats->rx_packets);
324         seq_printf(seq, fmt, "total bytes received", stats->rx_bytes);
325         seq_printf(seq, fmt, "Broadcast/Multicast Rcvd", stats->multicast);
326         seq_puts(seq, "\n");
327         seq_printf(seq, fmt, "total frames transmitted", stats->tx_packets);
328         seq_printf(seq, fmt, "total bytes transmitted", stats->tx_bytes);
329         seq_printf(seq, fmt, "total headroom inc", 
330                    dev_info->cnt_inc_headroom_on_tx);
331         seq_printf(seq, fmt, "total encap on xmit", 
332                    dev_info->cnt_encap_on_xmit);
333         seq_printf(seq, "Device: %s", dev_info->real_dev->name);
334         /* now show all PRIORITY mappings relating to this VLAN */
335         seq_printf(seq, 
336                        "\nINGRESS priority mappings: 0:%lu  1:%lu  2:%lu  3:%lu  4:%lu  5:%lu  6:%lu 7:%lu\n",
337                        dev_info->ingress_priority_map[0],
338                        dev_info->ingress_priority_map[1],
339                        dev_info->ingress_priority_map[2],
340                        dev_info->ingress_priority_map[3],
341                        dev_info->ingress_priority_map[4],
342                        dev_info->ingress_priority_map[5],
343                        dev_info->ingress_priority_map[6],
344                        dev_info->ingress_priority_map[7]);
345
346         seq_printf(seq, "EGRESSS priority Mappings: ");
347         for (i = 0; i < 16; i++) {
348                 const struct vlan_priority_tci_mapping *mp
349                         = dev_info->egress_priority_map[i];
350                 while (mp) {
351                         seq_printf(seq, "%lu:%hu ",
352                                    mp->priority, ((mp->vlan_qos >> 13) & 0x7));
353                         mp = mp->next;
354                 }
355         }
356         seq_puts(seq, "\n");
357
358         return 0;
359 }