blob: c0040c9064a144a0f7afb3e47411eaed6ce13576 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/stddef.h> /* offsetof(), etc. */
22#include <linux/errno.h> /* return codes */
23#include <linux/kernel.h>
24#include <linux/slab.h> /* kmalloc(), kfree() */
Jesper Juhle49332b2005-05-01 08:59:08 -070025#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/string.h> /* inline mem*, str* functions */
27#include <linux/init.h> /* __initfunc et al. */
28#include <asm/byteorder.h> /* htons(), etc. */
29#include <asm/uaccess.h> /* copy_to_user */
30#include <asm/io.h>
31#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
33#include <linux/fs.h>
34#include <linux/netdevice.h>
35#include <linux/if_vlan.h>
36#include "vlanproc.h"
37#include "vlan.h"
38
39/****** Function Prototypes *************************************************/
40
41/* Methods for preparing data for reading proc entries */
42static int vlan_seq_show(struct seq_file *seq, void *v);
43static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
44static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
45static void vlan_seq_stop(struct seq_file *seq, void *);
46static int vlandev_seq_show(struct seq_file *seq, void *v);
47
48/*
49 * Global Data
50 */
51
52
53/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090054 * Names of the proc directory entries
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57static const char name_root[] = "vlan";
58static const char name_conf[] = "config";
59
60/*
61 * Structures for interfacing with the /proc filesystem.
62 * VLAN creates its own directory /proc/net/vlan with the folowing
63 * entries:
64 * config device status/configuration
65 * <device> entry for each device
66 */
67
68/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +090069 * Generic /proc/net/vlan/<file> file and inode operations
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71
Philippe De Muyter56b3d972007-07-10 23:07:31 -070072static const struct seq_operations vlan_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .start = vlan_seq_start,
74 .next = vlan_seq_next,
75 .stop = vlan_seq_stop,
76 .show = vlan_seq_show,
77};
78
79static int vlan_seq_open(struct inode *inode, struct file *file)
80{
81 return seq_open(file, &vlan_seq_ops);
82}
83
Arjan van de Ven9a321442007-02-12 00:55:35 -080084static const struct file_operations vlan_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .owner = THIS_MODULE,
86 .open = vlan_seq_open,
87 .read = seq_read,
88 .llseek = seq_lseek,
89 .release = seq_release,
90};
91
92/*
93 * /proc/net/vlan/<device> file and inode operations
94 */
95
96static int vlandev_seq_open(struct inode *inode, struct file *file)
97{
98 return single_open(file, vlandev_seq_show, PDE(inode)->data);
99}
100
Arjan van de Ven9a321442007-02-12 00:55:35 -0800101static const struct file_operations vlandev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .owner = THIS_MODULE,
103 .open = vlandev_seq_open,
104 .read = seq_read,
105 .llseek = seq_lseek,
106 .release = single_release,
107};
108
109/*
110 * Proc filesystem derectory entries.
111 */
112
113/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900114 * /proc/net/vlan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
116
117static struct proc_dir_entry *proc_vlan_dir;
118
119/*
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900120 * /proc/net/vlan/config
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
123static struct proc_dir_entry *proc_vlan_conf;
124
125/* Strings */
126static const char *vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
127 [VLAN_NAME_TYPE_RAW_PLUS_VID] = "VLAN_NAME_TYPE_RAW_PLUS_VID",
128 [VLAN_NAME_TYPE_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
129 [VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD]= "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
130 [VLAN_NAME_TYPE_PLUS_VID] = "VLAN_NAME_TYPE_PLUS_VID",
131};
132/*
133 * Interface functions
134 */
135
136/*
137 * Clean up /proc/net/vlan entries
138 */
139
140void vlan_proc_cleanup(void)
141{
142 if (proc_vlan_conf)
143 remove_proc_entry(name_conf, proc_vlan_dir);
144
145 if (proc_vlan_dir)
146 proc_net_remove(name_root);
147
148 /* Dynamically added entries should be cleaned up as their vlan_device
149 * is removed, so we should not have to take care of it here...
150 */
151}
152
153/*
154 * Create /proc/net/vlan entries
155 */
156
157int __init vlan_proc_init(void)
158{
159 proc_vlan_dir = proc_mkdir(name_root, proc_net);
160 if (proc_vlan_dir) {
161 proc_vlan_conf = create_proc_entry(name_conf,
162 S_IFREG|S_IRUSR|S_IWUSR,
163 proc_vlan_dir);
164 if (proc_vlan_conf) {
165 proc_vlan_conf->proc_fops = &vlan_fops;
166 return 0;
167 }
168 }
169 vlan_proc_cleanup();
170 return -ENOBUFS;
171}
172
173/*
174 * Add directory entry for VLAN device.
175 */
176
177int vlan_proc_add_dev (struct net_device *vlandev)
178{
179 struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
180
181 if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
182 printk(KERN_ERR
183 "ERROR: vlan_proc_add, device -:%s:- is NOT a VLAN\n",
184 vlandev->name);
185 return -EINVAL;
186 }
187
188 dev_info->dent = create_proc_entry(vlandev->name,
189 S_IFREG|S_IRUSR|S_IWUSR,
190 proc_vlan_dir);
191 if (!dev_info->dent)
192 return -ENOBUFS;
193
194 dev_info->dent->proc_fops = &vlandev_fops;
195 dev_info->dent->data = vlandev;
196
197#ifdef VLAN_DEBUG
198 printk(KERN_ERR "vlan_proc_add, device -:%s:- being added.\n",
199 vlandev->name);
200#endif
201 return 0;
202}
203
204/*
205 * Delete directory entry for VLAN device.
206 */
207int vlan_proc_rem_dev(struct net_device *vlandev)
208{
209 if (!vlandev) {
210 printk(VLAN_ERR "%s: invalid argument: %p\n",
211 __FUNCTION__, vlandev);
212 return -EINVAL;
213 }
214
215 if (!(vlandev->priv_flags & IFF_802_1Q_VLAN)) {
216 printk(VLAN_DBG "%s: invalid argument, device: %s is not a VLAN device, priv_flags: 0x%4hX.\n",
217 __FUNCTION__, vlandev->name, vlandev->priv_flags);
218 return -EINVAL;
219 }
220
221#ifdef VLAN_DEBUG
222 printk(VLAN_DBG "%s: dev: %p\n", __FUNCTION__, vlandev);
223#endif
224
225 /** NOTE: This will consume the memory pointed to by dent, it seems. */
226 if (VLAN_DEV_INFO(vlandev)->dent) {
227 remove_proc_entry(VLAN_DEV_INFO(vlandev)->dent->name, proc_vlan_dir);
228 VLAN_DEV_INFO(vlandev)->dent = NULL;
229 }
230
231 return 0;
232}
233
234/****** Proc filesystem entry points ****************************************/
235
236/*
237 * The following few functions build the content of /proc/net/vlan/config
238 */
239
Pavel Emelianov7562f872007-05-03 15:13:45 -0700240static inline int is_vlan_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700242 return dev->priv_flags & IFF_802_1Q_VLAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900245/* start read of /proc/net/vlan/config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
247{
248 struct net_device *dev;
249 loff_t i = 1;
250
251 read_lock(&dev_base_lock);
252
253 if (*pos == 0)
254 return SEQ_START_TOKEN;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900255
Pavel Emelianov7562f872007-05-03 15:13:45 -0700256 for_each_netdev(dev) {
257 if (!is_vlan_dev(dev))
258 continue;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900259
Pavel Emelianov7562f872007-05-03 15:13:45 -0700260 if (i++ == *pos)
261 return dev;
262 }
263
264 return NULL;
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
268{
Pavel Emelianov7562f872007-05-03 15:13:45 -0700269 struct net_device *dev;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ++*pos;
272
Pavel Emelianov7562f872007-05-03 15:13:45 -0700273 dev = (struct net_device *)v;
274 if (v == SEQ_START_TOKEN)
275 dev = net_device_entry(&dev_base_head);
276
277 for_each_netdev_continue(dev) {
278 if (!is_vlan_dev(dev))
279 continue;
280
281 return dev;
282 }
283
284 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static void vlan_seq_stop(struct seq_file *seq, void *v)
288{
289 read_unlock(&dev_base_lock);
290}
291
292static int vlan_seq_show(struct seq_file *seq, void *v)
293{
294 if (v == SEQ_START_TOKEN) {
295 const char *nmtype = NULL;
296
297 seq_puts(seq, "VLAN Dev name | VLAN ID\n");
298
299 if (vlan_name_type < ARRAY_SIZE(vlan_name_type_str))
300 nmtype = vlan_name_type_str[vlan_name_type];
301
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900302 seq_printf(seq, "Name-Type: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 nmtype ? nmtype : "UNKNOWN" );
304 } else {
305 const struct net_device *vlandev = v;
306 const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
307
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900308 seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dev_info->vlan_id, dev_info->real_dev->name);
310 }
311 return 0;
312}
313
314static int vlandev_seq_show(struct seq_file *seq, void *offset)
315{
316 struct net_device *vlandev = (struct net_device *) seq->private;
317 const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
318 struct net_device_stats *stats;
319 static const char fmt[] = "%30s %12lu\n";
320 int i;
321
322 if ((vlandev == NULL) || (!(vlandev->priv_flags & IFF_802_1Q_VLAN)))
323 return 0;
324
325 seq_printf(seq, "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
326 vlandev->name, dev_info->vlan_id,
327 (int)(dev_info->flags & 1), vlandev->priv_flags);
328
329
330 stats = vlan_dev_get_stats(vlandev);
331
332 seq_printf(seq, fmt, "total frames received", stats->rx_packets);
333 seq_printf(seq, fmt, "total bytes received", stats->rx_bytes);
334 seq_printf(seq, fmt, "Broadcast/Multicast Rcvd", stats->multicast);
335 seq_puts(seq, "\n");
336 seq_printf(seq, fmt, "total frames transmitted", stats->tx_packets);
337 seq_printf(seq, fmt, "total bytes transmitted", stats->tx_bytes);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900338 seq_printf(seq, fmt, "total headroom inc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 dev_info->cnt_inc_headroom_on_tx);
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900340 seq_printf(seq, fmt, "total encap on xmit",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dev_info->cnt_encap_on_xmit);
342 seq_printf(seq, "Device: %s", dev_info->real_dev->name);
343 /* now show all PRIORITY mappings relating to this VLAN */
YOSHIFUJI Hideaki122952f2007-02-09 23:24:25 +0900344 seq_printf(seq,
Patrick McHardy734423c2007-06-13 12:07:07 -0700345 "\nINGRESS priority mappings: 0:%u 1:%u 2:%u 3:%u 4:%u 5:%u 6:%u 7:%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dev_info->ingress_priority_map[0],
347 dev_info->ingress_priority_map[1],
348 dev_info->ingress_priority_map[2],
349 dev_info->ingress_priority_map[3],
350 dev_info->ingress_priority_map[4],
351 dev_info->ingress_priority_map[5],
352 dev_info->ingress_priority_map[6],
353 dev_info->ingress_priority_map[7]);
354
355 seq_printf(seq, "EGRESSS priority Mappings: ");
356 for (i = 0; i < 16; i++) {
357 const struct vlan_priority_tci_mapping *mp
358 = dev_info->egress_priority_map[i];
359 while (mp) {
Patrick McHardy734423c2007-06-13 12:07:07 -0700360 seq_printf(seq, "%u:%hu ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 mp->priority, ((mp->vlan_qos >> 13) & 0x7));
362 mp = mp->next;
363 }
364 }
365 seq_puts(seq, "\n");
366
367 return 0;
368}