Skip to contentSkip to conversation

vlan: add sysfs support

RFC In review Stephen Hemminger 3 participants 5 messages May 1, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 vlan: add sysfs support

Add basic sysfs support for vlan device. It creates an entry in the vlan pseudo-device to display tag.

	/sys/class/net/eth0.5/vlan_id

It would be nice at some future to have something like:

	/sys/class/net/eth0/tags/5  -> ../../eth0.5

as well.

There is a race with udev though. The sysfs entry can't be created until after the the class_device is registered but the registration doesn't happen until rtnl_unlock after register_netdevice. The class_device registration causes the hotplug event, so the hotplug user program might get there before vlan_id is created.

net/8021q/Makefile +2 -4
@@ -6,7 +6,5 @@
 
 8021q-objs := vlan.o vlan_dev.o
 
-ifeq ($(CONFIG_PROC_FS),y)
-8021q-objs += vlanproc.o
-endif
-
+8021q-$(CONFIG_PROC_FS) += vlanproc.o
+8021q-$(CONFIG_SYSFS) += vlan_sysfs.o
net/8021q/vlan.c +5 -0
@@ -232,6 +232,8 @@
 			/* Remove proc entry */
 			vlan_proc_rem_dev(dev);
 
+			vlan_sysfs_remove(dev);
+
 			/* Take it out of our own structures, but be sure to
 			 * interlock with HW accelerating devices or SW vlan
 			 * input packet processing.
@@ -557,6 +559,9 @@
 
 	rtnl_unlock();
 
+	if (vlan_sysfs_add(new_dev))
+		printk(KERN_WARNING "VLAN: failed to create sysfs entry for %s\n",
+		       new_dev->name);
 
 #ifdef VLAN_DEBUG
 	printk(VLAN_DBG "Allocated new device successfully, returning.\n");
net/8021q/vlan.h +9 -0
@@ -69,4 +69,13 @@
 int vlan_dev_get_vid(const char* dev_name, unsigned short* result);
 void vlan_dev_set_multicast_list(struct net_device *vlan_dev);
 
+#ifdef CONFIG_SYSFS
+int vlan_sysfs_add(struct net_device *dev);
+void vlan_sysfs_remove(struct net_device *dev);
+#else
+#define vlan_sysfs_add(dev)	(0)
+#define vlan_sysfs_remove(dev)  do { } while (0)
+#endif
+
+
 #endif /* !(__BEN_VLAN_802_1Q_INC__) */
net/8021q/vlan_sysfs.c new file +38 -0
@@ -0,0 +1,38 @@
+/*
+ * VLAN sysfs interface.
+ *
+ * Basic access to vlan information via sysfs.
+ * Authors:
+ * Stephen Hemminger		<[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/netdevice.h>
+#include "vlan.h"
+
+static ssize_t show_vlan_id(struct class_device *cd, char *buf)
+{
+	struct net_device *vlandev = container_of(cd, struct net_device, class_dev);
+
+	return sprintf(buf, "%d\n", VLAN_DEV_INFO(vlandev)->vlan_id);
+}
+
+static CLASS_DEVICE_ATTR(vlan_id, S_IRUGO, show_vlan_id, NULL);
+
+int vlan_sysfs_add(struct net_device *dev)
+{
+	return class_device_create_file(&dev->class_dev,
+					&class_device_attr_vlan_id);
+}
+
+void vlan_sysfs_remove(struct net_device *dev)
+{
+	return class_device_remove_file(&dev->class_dev,
+					&class_device_attr_vlan_id);
+
+}
#1 lore raw
Quoted 18 lines · Stephen Hemminger

Add basic sysfs support for vlan device. It creates an entry in the vlan pseudo-device to display tag.

	/sys/class/net/eth0.5/vlan_id

It would be nice at some future to have something like:

	/sys/class/net/eth0/tags/5  -> ../../eth0.5

as well.

There is a race with udev though. The sysfs entry can't be created until after the the class_device is registered but the registration doesn't happen until rtnl_unlock after register_netdevice. The class_device registration causes the hotplug event, so the hotplug user program might get there before vlan_id is created.

net/8021q/Makefile line 7

Quoted 2 lines
net/8021q/Makefile +0 -0
@@ -6,7 +6,5 @@
 
 8021q-objs := vlan.o vlan_dev.o

Please make this

net/8021q/Makefile line 8

Quoted 1 line
net/8021q/Makefile +0 -0
 8021q-y := vlan.o vlan_dev.o

To avoid confusion.

net/8021q/Makefile line 10

Quoted 7 lines
net/8021q/Makefile +2 -4
-ifeq ($(CONFIG_PROC_FS),y)
-8021q-objs += vlanproc.o
-endif
-
+8021q-$(CONFIG_PROC_FS) += vlanproc.o
+8021q-$(CONFIG_SYSFS) += vlan_sysfs.o

Sam

#2 lore raw
In reply to Stephen Hemminger
Quoted 3 lines · Stephen Hemminger

Add basic sysfs support for vlan device. It creates an entry in the vlan pseudo-device to display tag.

	/sys/class/net/eth0.5/vlan_id

Looks fine to me, though I don't know enough about sysfs to notice any bugs with it...

I do have a question: Will this work for > 256 vlans?

/proc had a limit of around 256 directories last I checked, so just curious.

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#3 lore raw
Quoted 8 lines · On Mon, 01 May 2006 15:07:14 -0700 Ben Greear <[email protected]> wrote:
Quoted 3 lines · Stephen Hemminger

Add basic sysfs support for vlan device. It creates an entry in the vlan pseudo-device to display tag.

	/sys/class/net/eth0.5/vlan_id

Looks fine to me, though I don't know enough about sysfs to notice any bugs with it...

Quoted 8 lines

I do have a question: Will this work for > 256 vlans?

/proc had a limit of around 256 directories last I checked, so just curious.

Thanks,
Ben

There should be no limits caused by sysfs...

#4 lore raw

PATCH 1/1 vlan: sysfs support

In reply to Stephen Hemminger

Here is a more complete version of vlan sysfs support. Sysfs support is good because it means scripts don't have to depend on parsing interface names that may change.

Assuming there is a vlan 5 added to eth0 then the following links are added:

/sys/class/net/eth0/vlan_group/5 -> ../../../../class/net/eth0.5 /sys/class/net/eth0.5/vlan_device -> ../../../class/net/eth0 and one file /sys/class/net/eth0/vlan_id that contains "5\n"

It handles the reference counts on the vlan_groups properly, but still has a race between hotplug and creating these sysfs objects because of the chicken/egg problem with register_netdevice interface.

net/8021q/Makefile +3 -5
@@ -4,9 +4,7 @@
 
 obj-$(CONFIG_VLAN_8021Q) += 8021q.o
 
-8021q-objs := vlan.o vlan_dev.o
-
-ifeq ($(CONFIG_PROC_FS),y)
-8021q-objs += vlanproc.o
-endif
+8021q-y := vlan.o vlan_dev.o
 
+8021q-$(CONFIG_PROC_FS) += vlanproc.o
+8021q-$(CONFIG_SYSFS) += vlan_sysfs.o
net/8021q/vlan.c +26 -3
@@ -193,9 +193,21 @@
 	return NULL;
 }
 
+/* Callback after last packet and any open sysfs handles are gone. */
+static void vlan_grp_release(struct kobject *kobj)
+{
+	kfree(container_of(kobj, struct vlan_group, kobj));
+}
+
+static struct kobj_type vlan_grp_ktype = {
+	.release = vlan_grp_release,
+};
+
+/* Callback after last packet in flight has passed. */
 static void vlan_rcu_free(struct rcu_head *rcu)
 {
-	kfree(container_of(rcu, struct vlan_group, rcu));
+	struct vlan_group *grp = container_of(rcu, struct vlan_group, rcu);
+	kobject_put(&grp->kobj);
 }
 
 
@@ -232,6 +244,8 @@
 			/* Remove proc entry */
 			vlan_proc_rem_dev(dev);
 
+			vlan_sysfs_remove(dev, real_dev,  grp, vlan_id);
+
 			/* Take it out of our own structures, but be sure to
 			 * interlock with HW accelerating devices or SW vlan
 			 * input packet processing.
@@ -265,6 +279,8 @@
 				hlist_del_rcu(&grp->hlist);
 
 				/* Free the group, after all cpu's are done. */
+				kobject_uevent(&grp->kobj, KOBJ_REMOVE);
+				kobject_del(&grp->kobj);
 				call_rcu(&grp->rcu, vlan_rcu_free);
 
 				grp = NULL;
@@ -531,13 +547,17 @@
 	 * so it cannot "appear" on us.
 	 */
 	if (!grp) { /* need to add a new group */
-		grp = kmalloc(sizeof(struct vlan_group), GFP_KERNEL);
+		grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
 		if (!grp)
 			goto out_free_unregister;
 					
 		/* printk(KERN_ALERT "VLAN REGISTER:  Allocated new group.\n"); */
-		memset(grp, 0, sizeof(struct vlan_group));
 		grp->real_dev_ifindex = real_dev->ifindex;
+		kobject_init(&grp->kobj);
+		kobject_set_name(&grp->kobj, "vlan_group");
+		grp->kobj.parent = &real_dev->class_dev.kobj;
+		grp->kobj.ktype = &vlan_grp_ktype;
+		kobject_register(&grp->kobj);
 
 		hlist_add_head_rcu(&grp->hlist, 
 				   &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
@@ -557,6 +577,9 @@
 
 	rtnl_unlock();
 
+	if (vlan_sysfs_add(new_dev, real_dev, grp, VLAN_ID))
+		printk(KERN_WARNING "VLAN: failed to create sysfs entry for %s\n",
+		       new_dev->name);
 
 #ifdef VLAN_DEBUG
 	printk(VLAN_DBG "Allocated new device successfully, returning.\n");
net/8021q/vlan.h +11 -0
@@ -69,4 +69,15 @@
 int vlan_dev_get_vid(const char* dev_name, unsigned short* result);
 void vlan_dev_set_multicast_list(struct net_device *vlan_dev);
 
+#ifdef CONFIG_SYSFS
+int vlan_sysfs_add(struct net_device *dev, struct net_device *real_dev,
+		   struct vlan_group *grp, unsigned short tag);
+void vlan_sysfs_remove(struct net_device *dev, struct net_device *real_dev,
+		       struct vlan_group *grp, unsigned short tag);
+#else
+#define vlan_sysfs_add(dev, realdev, grp, tag)	(0)
+#define vlan_sysfs_remove(dev, realdev, grp, tag)  do { } while (0)
+#endif
+
+
 #endif /* !(__BEN_VLAN_802_1Q_INC__) */
net/8021q/vlan_sysfs.c new file +59 -0

59 lines not on this page · raw message

include/linux/if_vlan.h +1 -0
@@ -77,6 +77,7 @@
 	struct hlist_node	hlist;	/* linked list */
 	struct net_device *vlan_devices[VLAN_GROUP_ARRAY_LEN];
 	struct rcu_head		rcu;
+	struct kobject 	        kobj;
 };
 
 struct vlan_priority_tci_mapping {