netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] vlan: add sysfs support
@ 2006-05-01 21:08 Stephen Hemminger
  2006-05-01 21:31 ` Sam Ravnborg
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Stephen Hemminger @ 2006-05-01 21:08 UTC (permalink / raw)
  To: Ben Greear, David S. Miller; +Cc: netdev, vlan

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.


--- vlan.orig/net/8021q/Makefile	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/Makefile	2006-05-01 14:04:56.000000000 -0700
@@ -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
--- vlan.orig/net/8021q/vlan.c	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/vlan.c	2006-05-01 13:48:35.000000000 -0700
@@ -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");
--- vlan.orig/net/8021q/vlan.h	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/vlan.h	2006-05-01 13:17:16.000000000 -0700
@@ -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__) */
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ vlan/net/8021q/vlan_sysfs.c	2006-05-01 14:04:49.000000000 -0700
@@ -0,0 +1,38 @@
+/*
+ * VLAN sysfs interface.
+ *
+ * Basic access to vlan information via sysfs.
+ * Authors:
+ * Stephen Hemminger		<shemminger@osdl.org>
+ *
+ * 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);
+
+}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC] vlan: add sysfs support
  2006-05-01 21:08 [RFC] vlan: add sysfs support Stephen Hemminger
@ 2006-05-01 21:31 ` Sam Ravnborg
  2006-05-01 22:07 ` Ben Greear
  2006-05-01 23:28 ` [PATCH] vlan: " Stephen Hemminger
  2 siblings, 0 replies; 5+ messages in thread
From: Sam Ravnborg @ 2006-05-01 21:31 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: Ben Greear, David S. Miller, netdev, vlan

On Mon, May 01, 2006 at 02:08:34PM -0700, Stephen Hemminger wrote:
> 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.
> 
> 
> --- vlan.orig/net/8021q/Makefile	2006-05-01 12:50:58.000000000 -0700
> +++ vlan/net/8021q/Makefile	2006-05-01 14:04:56.000000000 -0700
> @@ -6,7 +6,5 @@
>  
>  8021q-objs := vlan.o vlan_dev.o
Please make this
>  8021q-y := vlan.o vlan_dev.o

To avoid confusion.


>  
> -ifeq ($(CONFIG_PROC_FS),y)
> -8021q-objs += vlanproc.o
> -endif
> -
> +8021q-$(CONFIG_PROC_FS) += vlanproc.o
> +8021q-$(CONFIG_SYSFS) += vlan_sysfs.o

	Sam

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC] vlan: add sysfs support
  2006-05-01 21:08 [RFC] vlan: add sysfs support Stephen Hemminger
  2006-05-01 21:31 ` Sam Ravnborg
@ 2006-05-01 22:07 ` Ben Greear
  2006-05-01 22:26   ` Stephen Hemminger
  2006-05-01 23:28 ` [PATCH] vlan: " Stephen Hemminger
  2 siblings, 1 reply; 5+ messages in thread
From: Ben Greear @ 2006-05-01 22:07 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: David S. Miller, netdev, vlan

Stephen Hemminger wrote:
> 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

-- 
Ben Greear <greearb@candelatech.com>
Candela Technologies Inc  http://www.candelatech.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC] vlan: add sysfs support
  2006-05-01 22:07 ` Ben Greear
@ 2006-05-01 22:26   ` Stephen Hemminger
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Hemminger @ 2006-05-01 22:26 UTC (permalink / raw)
  To: Ben Greear; +Cc: David S. Miller, netdev, vlan

On Mon, 01 May 2006 15:07:14 -0700
Ben Greear <greearb@candelatech.com> wrote:

> Stephen Hemminger wrote:
> > 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
> 

There should be no limits caused by sysfs...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] vlan: sysfs support
  2006-05-01 21:08 [RFC] vlan: add sysfs support Stephen Hemminger
  2006-05-01 21:31 ` Sam Ravnborg
  2006-05-01 22:07 ` Ben Greear
@ 2006-05-01 23:28 ` Stephen Hemminger
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen Hemminger @ 2006-05-01 23:28 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: Ben Greear, David S. Miller, netdev, vlan

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.


Signed-off-by: Stephen Hemminger <shemminger@osdl.org>

--- vlan.orig/net/8021q/Makefile	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/Makefile	2006-05-01 15:27:55.000000000 -0700
@@ -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
--- vlan.orig/net/8021q/vlan.c	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/vlan.c	2006-05-01 16:17:12.000000000 -0700
@@ -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");
--- vlan.orig/net/8021q/vlan.h	2006-05-01 12:50:58.000000000 -0700
+++ vlan/net/8021q/vlan.h	2006-05-01 16:08:18.000000000 -0700
@@ -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__) */
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ vlan/net/8021q/vlan_sysfs.c	2006-05-01 16:08:18.000000000 -0700
@@ -0,0 +1,59 @@
+/*
+ * VLAN sysfs interface.
+ *
+ * Basic access to vlan information via sysfs.
+ * Authors:
+ * Stephen Hemminger		<shemminger@osdl.org>
+ *
+ * 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, struct net_device *real_dev,
+		   struct vlan_group *grp, unsigned short tag)
+{
+	int ret;
+	char buf[IFNAMSIZ];
+
+	ret = sysfs_create_link(&dev->class_dev.kobj,
+				&real_dev->class_dev.kobj, "vlan_device");
+	if (ret)
+		goto out;
+
+	sprintf(buf, "%d", tag);
+	ret = sysfs_create_link(&grp->kobj, &dev->class_dev.kobj, buf);
+	if (ret)
+		goto out;
+
+	ret = class_device_create_file(&dev->class_dev, &class_device_attr_vlan_id);
+ out:
+	return ret;
+}
+
+void vlan_sysfs_remove(struct net_device *dev, struct net_device *real_dev,
+		       struct vlan_group *grp, unsigned short id)
+{
+	char buf[IFNAMSIZ];
+
+	sprintf(buf, "%d", id);
+	sysfs_remove_link(&grp->kobj, buf);
+	sysfs_remove_link(&dev->class_dev.kobj, "vlan_device");
+
+	return class_device_remove_file(&dev->class_dev,
+					&class_device_attr_vlan_id);
+}
--- vlan.orig/include/linux/if_vlan.h	2006-05-01 16:07:16.000000000 -0700
+++ vlan/include/linux/if_vlan.h	2006-05-01 16:08:18.000000000 -0700
@@ -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 {

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-05-01 23:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-01 21:08 [RFC] vlan: add sysfs support Stephen Hemminger
2006-05-01 21:31 ` Sam Ravnborg
2006-05-01 22:07 ` Ben Greear
2006-05-01 22:26   ` Stephen Hemminger
2006-05-01 23:28 ` [PATCH] vlan: " Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).