Commit c3f69309 authored by Stephen Hemminger's avatar Stephen Hemminger

[VLAN]: Convert over to seq_file.

parent 67ef59de
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <asm/uaccess.h> /* copy_to_user */ #include <asm/uaccess.h> /* copy_to_user */
#include <asm/io.h> #include <asm/io.h>
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/if_vlan.h> #include <linux/if_vlan.h>
...@@ -38,28 +39,28 @@ ...@@ -38,28 +39,28 @@
/****** Function Prototypes *************************************************/ /****** Function Prototypes *************************************************/
/* Proc filesystem interface */
static ssize_t vlan_proc_read(struct file *file, char *buf, size_t count,
loff_t *ppos);
/* Methods for preparing data for reading proc entries */ /* Methods for preparing data for reading proc entries */
static int vlan_seq_show(struct seq_file *seq, void *v);
static int vlan_config_get_info(char *buf, char **start, off_t offs, int len); static void *vlan_seq_start(struct seq_file *seq, loff_t *pos);
static int vlandev_get_info(char *buf, char **start, off_t offs, int len); static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos);
static void vlan_seq_stop(struct seq_file *seq, void *);
static int vlandev_seq_show(struct seq_file *seq, void *v);
/* Miscellaneous */ /* Miscellaneous */
#define SEQ_START_TOKEN ((void *) 1)
/* /*
* Global Data * Global Data
*/ */
/* /*
* Names of the proc directory entries * Names of the proc directory entries
*/ */
static char name_root[] = "vlan"; static const char name_root[] = "vlan";
static char name_conf[] = "config"; static const char name_conf[] = "config";
static char term_msg[] = "***KERNEL: Out of buffer space!***\n";
/* /*
* Structures for interfacing with the /proc filesystem. * Structures for interfacing with the /proc filesystem.
...@@ -73,20 +74,41 @@ static char term_msg[] = "***KERNEL: Out of buffer space!***\n"; ...@@ -73,20 +74,41 @@ static char term_msg[] = "***KERNEL: Out of buffer space!***\n";
* Generic /proc/net/vlan/<file> file and inode operations * Generic /proc/net/vlan/<file> file and inode operations
*/ */
static struct seq_operations vlan_seq_ops = {
.start = vlan_seq_start,
.next = vlan_seq_next,
.stop = vlan_seq_stop,
.show = vlan_seq_show,
};
static int vlan_seq_open(struct inode *inode, struct file *file)
{
return seq_open(file, &vlan_seq_ops);
}
static struct file_operations vlan_fops = { static struct file_operations vlan_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.read = vlan_proc_read, .open = vlan_seq_open,
.ioctl = NULL, /* vlan_proc_ioctl */ .read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
}; };
/* /*
* /proc/net/vlan/<device> file and inode operations * /proc/net/vlan/<device> file and inode operations
*/ */
static int vlandev_seq_open(struct inode *inode, struct file *file)
{
return single_open(file, vlandev_seq_show, PDE(inode)->data);
}
static struct file_operations vlandev_fops = { static struct file_operations vlandev_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.read = vlan_proc_read, .open = vlandev_seq_open,
.ioctl =NULL, /* vlan_proc_ioctl */ .read = seq_read,
.llseek = seq_lseek,
.release = single_release,
}; };
/* /*
...@@ -106,8 +128,12 @@ static struct proc_dir_entry *proc_vlan_dir; ...@@ -106,8 +128,12 @@ static struct proc_dir_entry *proc_vlan_dir;
static struct proc_dir_entry *proc_vlan_conf; static struct proc_dir_entry *proc_vlan_conf;
/* Strings */ /* Strings */
static char conf_hdr[] = "VLAN Dev name | VLAN ID\n"; static const char *vlan_name_type_str[VLAN_NAME_TYPE_HIGHEST] = {
[VLAN_NAME_TYPE_RAW_PLUS_VID] = "VLAN_NAME_TYPE_RAW_PLUS_VID",
[VLAN_NAME_TYPE_PLUS_VID_NO_PAD] = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD",
[VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD]= "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD",
[VLAN_NAME_TYPE_PLUS_VID] = "VLAN_NAME_TYPE_PLUS_VID",
};
/* /*
* Interface functions * Interface functions
*/ */
...@@ -142,7 +168,6 @@ int __init vlan_proc_init(void) ...@@ -142,7 +168,6 @@ int __init vlan_proc_init(void)
proc_vlan_dir); proc_vlan_dir);
if (proc_vlan_conf) { if (proc_vlan_conf) {
proc_vlan_conf->proc_fops = &vlan_fops; proc_vlan_conf->proc_fops = &vlan_fops;
proc_vlan_conf->get_info = vlan_config_get_info;
return 0; return 0;
} }
} }
...@@ -172,7 +197,6 @@ int vlan_proc_add_dev (struct net_device *vlandev) ...@@ -172,7 +197,6 @@ int vlan_proc_add_dev (struct net_device *vlandev)
return -ENOBUFS; return -ENOBUFS;
dev_info->dent->proc_fops = &vlandev_fops; dev_info->dent->proc_fops = &vlandev_fops;
dev_info->dent->get_info = &vlandev_get_info;
dev_info->dent->data = vlandev; dev_info->dent->data = vlandev;
#ifdef VLAN_DEBUG #ifdef VLAN_DEBUG
...@@ -213,185 +237,103 @@ int vlan_proc_rem_dev(struct net_device *vlandev) ...@@ -213,185 +237,103 @@ int vlan_proc_rem_dev(struct net_device *vlandev)
/****** Proc filesystem entry points ****************************************/ /****** Proc filesystem entry points ****************************************/
/* /*
* Read VLAN proc directory entry. * The following few functions build the content of /proc/net/vlan/config
* This is universal routine for reading all entries in /proc/net/vlan
* directory. Each directory entry contains a pointer to the 'method' for
* preparing data for that entry.
* o verify arguments
* o allocate kernel buffer
* o call get_info() to prepare data
* o copy data to user space
* o release kernel buffer
*
* Return: number of bytes copied to user space (0, if no data)
* <0 error
*/ */
static ssize_t vlan_proc_read(struct file *file, char *buf,
size_t count, loff_t *ppos)
{
struct inode *inode = file->f_dentry->d_inode;
struct proc_dir_entry *dent;
char *page;
int pos, offs, len;
if (count <= 0) /* starting at dev, find a VLAN device */
return 0; struct net_device *vlan_skip(struct net_device *dev)
{
while (dev && !(dev->priv_flags & IFF_802_1Q_VLAN))
dev = dev->next;
dent = PDE(inode); return dev;
if ((dent == NULL) || (dent->get_info == NULL)) }
return 0;
page = kmalloc(VLAN_PROC_BUFSZ, GFP_KERNEL); /* start read of /proc/net/vlan/config */
VLAN_MEM_DBG("page malloc, addr: %p size: %i\n", static void *vlan_seq_start(struct seq_file *seq, loff_t *pos)
page, VLAN_PROC_BUFSZ); {
struct net_device *dev;
loff_t i = 1;
if (page == NULL) read_lock(&dev_base_lock);
return -ENOBUFS;
pos = dent->get_info(page, dent->data, 0, 0); if (*pos == 0)
offs = file->f_pos; return SEQ_START_TOKEN;
if (offs < pos) {
len = min_t(int, pos - offs, count);
if (copy_to_user(buf, (page + offs), len)) {
kfree(page);
return -EFAULT;
}
file->f_pos += len; for (dev = vlan_skip(dev_base); dev && i < *pos;
} else { dev = vlan_skip(dev->next), ++i);
len = 0;
}
kfree(page); return (i == *pos) ? dev : NULL;
VLAN_FMEM_DBG("page free, addr: %p\n", page);
return len;
} }
/* static void *vlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
* The following few functions build the content of /proc/net/vlan/config
*/
static int vlan_proc_get_vlan_info(char* buf, unsigned int cnt)
{ {
struct net_device *vlandev = NULL; ++*pos;
struct vlan_group *grp = NULL;
int h, i;
char *nm_type = NULL;
struct vlan_dev_info *dev_info = NULL;
#ifdef VLAN_DEBUG return vlan_skip((v == SEQ_START_TOKEN)
printk(VLAN_DBG __FUNCTION__ ": cnt == %i\n", cnt); ? dev_base
#endif : ((struct net_device *)v)->next);
}
if (vlan_name_type == VLAN_NAME_TYPE_RAW_PLUS_VID) { static void vlan_seq_stop(struct seq_file *seq, void *v)
nm_type = "VLAN_NAME_TYPE_RAW_PLUS_VID"; {
} else if (vlan_name_type == VLAN_NAME_TYPE_PLUS_VID_NO_PAD) { read_unlock(&dev_base_lock);
nm_type = "VLAN_NAME_TYPE_PLUS_VID_NO_PAD"; }
} else if (vlan_name_type == VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD) {
nm_type = "VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD";
} else if (vlan_name_type == VLAN_NAME_TYPE_PLUS_VID) {
nm_type = "VLAN_NAME_TYPE_PLUS_VID";
} else {
nm_type = "UNKNOWN";
}
cnt += sprintf(buf + cnt, "Name-Type: %s\n", nm_type); static int vlan_seq_show(struct seq_file *seq, void *v)
{
if (v == SEQ_START_TOKEN) {
const char *nmtype = NULL;
spin_lock_bh(&vlan_group_lock); seq_puts(seq, "VLAN Dev name | VLAN ID\n");
for (h = 0; h < VLAN_GRP_HASH_SIZE; h++) {
for (grp = vlan_group_hash[h]; grp != NULL; grp = grp->next) {
for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
vlandev = grp->vlan_devices[i];
if (!vlandev)
continue;
if ((cnt + 100) > VLAN_PROC_BUFSZ) { if (vlan_name_type < ARRAY_SIZE(vlan_name_type_str))
if ((cnt+strlen(term_msg)) < VLAN_PROC_BUFSZ) nmtype = vlan_name_type_str[vlan_name_type];
cnt += sprintf(buf+cnt, "%s", term_msg);
goto out; seq_printf(seq, "Name-Type: %s\n",
} nmtype ? nmtype : "UNKNOWN" );
} else {
const struct net_device *vlandev = v;
const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
dev_info = VLAN_DEV_INFO(vlandev); seq_printf(seq, "%-15s| %d | %s\n", vlandev->name,
cnt += sprintf(buf + cnt, "%-15s| %d | %s\n", dev_info->vlan_id, dev_info->real_dev->name);
vlandev->name,
dev_info->vlan_id,
dev_info->real_dev->name);
}
} }
} return 0;
out:
spin_unlock_bh(&vlan_group_lock);
return cnt;
}
/*
* Prepare data for reading 'Config' entry.
* Return length of data.
*/
static int vlan_config_get_info(char *buf, char **start,
off_t offs, int len)
{
strcpy(buf, conf_hdr);
return vlan_proc_get_vlan_info(buf, (unsigned int)(strlen(conf_hdr)));
} }
/* static int vlandev_seq_show(struct seq_file *seq, void *offset)
* Prepare data for reading <device> entry.
* Return length of data.
*
* On entry, the 'start' argument will contain a pointer to VLAN device
* data space.
*/
static int vlandev_get_info(char *buf, char **start,
off_t offs, int len)
{ {
struct net_device *vlandev = (void *) start; struct net_device *vlandev = (struct net_device *) seq->private;
struct net_device_stats *stats = NULL; const struct vlan_dev_info *dev_info = VLAN_DEV_INFO(vlandev);
struct vlan_dev_info *dev_info = NULL; struct net_device_stats *stats;
struct vlan_priority_tci_mapping *mp; static const char *fmt = "%30s %12lu\n";
int cnt = 0;
int i; int i;
if ((vlandev == NULL) || (!(vlandev->priv_flags & IFF_802_1Q_VLAN))) if ((vlandev == NULL) || (!(vlandev->priv_flags & IFF_802_1Q_VLAN)))
return 0; return 0;
dev_info = VLAN_DEV_INFO(vlandev); seq_printf(seq, "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
cnt += sprintf(buf + cnt, "%s VID: %d REORDER_HDR: %i dev->priv_flags: %hx\n",
vlandev->name, dev_info->vlan_id, vlandev->name, dev_info->vlan_id,
(int)(dev_info->flags & 1), vlandev->priv_flags); (int)(dev_info->flags & 1), vlandev->priv_flags);
stats = vlan_dev_get_stats(vlandev);
cnt += sprintf(buf + cnt, "%30s: %12lu\n",
"total frames received", stats->rx_packets);
cnt += sprintf(buf + cnt, "%30s: %12lu\n", stats = vlan_dev_get_stats(vlandev);
"total bytes received", stats->rx_bytes);
cnt += sprintf(buf + cnt, "%30s: %12lu\n",
"Broadcast/Multicast Rcvd", stats->multicast);
cnt += sprintf(buf + cnt, "\n%30s: %12lu\n",
"total frames transmitted", stats->tx_packets);
cnt += sprintf(buf + cnt, "%30s: %12lu\n",
"total bytes transmitted", stats->tx_bytes);
cnt += sprintf(buf + cnt, "%30s: %12lu\n",
"total headroom inc", dev_info->cnt_inc_headroom_on_tx);
cnt += sprintf(buf + cnt, "%30s: %12lu\n",
"total encap on xmit", dev_info->cnt_encap_on_xmit);
cnt += sprintf(buf + cnt, "Device: %s", dev_info->real_dev->name);
seq_printf(seq, fmt, "total frames received", stats->rx_packets);
seq_printf(seq, fmt, "total bytes received", stats->rx_bytes);
seq_printf(seq, fmt, "Broadcast/Multicast Rcvd", stats->multicast);
seq_puts(seq, "\n");
seq_printf(seq, fmt, "total frames transmitted", stats->tx_packets);
seq_printf(seq, fmt, "total bytes transmitted", stats->tx_bytes);
seq_printf(seq, fmt, "total headroom inc",
dev_info->cnt_inc_headroom_on_tx);
seq_printf(seq, fmt, "total encap on xmit",
dev_info->cnt_encap_on_xmit);
seq_printf(seq, "Device: %s", dev_info->real_dev->name);
/* now show all PRIORITY mappings relating to this VLAN */ /* now show all PRIORITY mappings relating to this VLAN */
cnt += sprintf(buf + cnt, "\nINGRESS priority mappings: 0:%lu 1:%lu 2:%lu 3:%lu 4:%lu 5:%lu 6:%lu 7:%lu\n", seq_printf(seq,
"\nINGRESS priority mappings: 0:%lu 1:%lu 2:%lu 3:%lu 4:%lu 5:%lu 6:%lu 7:%lu\n",
dev_info->ingress_priority_map[0], dev_info->ingress_priority_map[0],
dev_info->ingress_priority_map[1], dev_info->ingress_priority_map[1],
dev_info->ingress_priority_map[2], dev_info->ingress_priority_map[2],
...@@ -401,38 +343,17 @@ static int vlandev_get_info(char *buf, char **start, ...@@ -401,38 +343,17 @@ static int vlandev_get_info(char *buf, char **start,
dev_info->ingress_priority_map[6], dev_info->ingress_priority_map[6],
dev_info->ingress_priority_map[7]); dev_info->ingress_priority_map[7]);
if ((cnt + 100) > VLAN_PROC_BUFSZ) { seq_printf(seq, "EGRESSS priority Mappings: ");
if ((cnt + strlen(term_msg)) >= VLAN_PROC_BUFSZ) {
/* should never get here */
return cnt;
} else {
cnt += sprintf(buf + cnt, "%s", term_msg);
return cnt;
}
}
cnt += sprintf(buf + cnt, "EGRESSS priority Mappings: ");
for (i = 0; i < 16; i++) { for (i = 0; i < 16; i++) {
mp = dev_info->egress_priority_map[i]; const struct vlan_priority_tci_mapping *mp
= dev_info->egress_priority_map[i];
while (mp) { while (mp) {
cnt += sprintf(buf + cnt, "%lu:%hu ", seq_printf(seq, "%lu:%hu ",
mp->priority, ((mp->vlan_qos >> 13) & 0x7)); mp->priority, ((mp->vlan_qos >> 13) & 0x7));
if ((cnt + 100) > VLAN_PROC_BUFSZ) {
if ((cnt + strlen(term_msg)) >= VLAN_PROC_BUFSZ) {
/* should never get here */
return cnt;
} else {
cnt += sprintf(buf + cnt, "%s", term_msg);
return cnt;
}
}
mp = mp->next; mp = mp->next;
} }
} }
seq_puts(seq, "\n");
cnt += sprintf(buf + cnt, "\n"); return 0;
return cnt;
} }
...@@ -3,13 +3,10 @@ ...@@ -3,13 +3,10 @@
#ifdef CONFIG_PROC_FS #ifdef CONFIG_PROC_FS
int vlan_proc_init(void); int vlan_proc_init(void);
int vlan_proc_rem_dev(struct net_device *vlandev); int vlan_proc_rem_dev(struct net_device *vlandev);
int vlan_proc_add_dev (struct net_device *vlandev); int vlan_proc_add_dev (struct net_device *vlandev);
void vlan_proc_cleanup (void); void vlan_proc_cleanup (void);
#define VLAN_PROC_BUFSZ (4096) /* buffer size for printing proc info */
#else /* No CONFIG_PROC_FS */ #else /* No CONFIG_PROC_FS */
#define vlan_proc_init() (0) #define vlan_proc_init() (0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment