Skip to contentSkip to conversation

Handle uevent per namespace

RFC PATCH In review Daniel Lezcano 3 participants 5 messages Nov 24, 2008

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

RFC PATCH 1/1 Handle uevent per namespace

Subject: Handle uevent per namespace
From: Daniel Lezcano <[email protected]>

At present when a network device is destroyed, inside a network namespace, and this device has the same name as one network device belonging to the initial network namespace (eg. eth0), the udev daemon will disable the interface in the initial network namespace.

IMHO, udev should not receive this event. The uevents should be per namespace or at least do not send events when not for the initial network namespace.

The following patch is a RFC for making uevent namespace aware. I don't know this part of the kernel code, so I am pretty sure t is not the right way to do that :)

I tried to avoid, in the kobject code, to follow up the fields to net_device because that will add adherence between the generic kobject code and the other kernel subsystem.

---
 include/linux/kobject.h     |    5 +++++
 include/net/net_namespace.h |    3 +++
 lib/kobject.c               |    4 ++++
 lib/kobject_uevent.c        |   30 ++++++++++++++++++++++--------
 net/core/net-sysfs.c        |    1 +
 5 files changed, 35 insertions(+), 8 deletions(-)
include/linux/kobject.h +5 -0
Index: net-next-2.6/include/linux/kobject.h
===================================================================
@@ -56,6 +56,8 @@ enum kobject_action {
 	KOBJ_MAX
 };
 
+struct net;
+
 struct kobject {
 	const char		*name;
 	struct list_head	entry;
@@ -63,6 +65,9 @@ struct kobject {
 	struct kset		*kset;
 	struct kobj_type	*ktype;
 	struct sysfs_dirent	*sd;
+#ifdef CONFIG_NET
+	struct net              *net;
+#endif
 	struct kref		kref;
 	unsigned int state_initialized:1;
 	unsigned int state_in_sysfs:1;
include/net/net_namespace.h +3 -0
Index: net-next-2.6/include/net/net_namespace.h
===================================================================
@@ -75,6 +75,9 @@ struct net {
 #endif
 #endif
 	struct net_generic	*gen;
+#ifdef CONFIG_HOTPLUG
+	struct sock             *uevent_sock;
+#endif
 };
 
 
lib/kobject.c +4 -0
Index: net-next-2.6/lib/kobject.c
===================================================================
@@ -17,6 +17,7 @@
 #include <linux/module.h>
 #include <linux/stat.h>
 #include <linux/slab.h>
+#include <net/net_namespace.h>
 
 /*
  * populate_dir - populate directory with attributes.
@@ -148,6 +149,9 @@ static void kobject_init_internal(struct
 		return;
 	kref_init(&kobj->kref);
 	INIT_LIST_HEAD(&kobj->entry);
+#ifdef CONFIG_NET
+	kobj->net = &init_net;
+#endif
 	kobj->state_in_sysfs = 0;
 	kobj->state_add_uevent_sent = 0;
 	kobj->state_remove_uevent_sent = 0;
lib/kobject_uevent.c +22 -8
Index: net-next-2.6/lib/kobject_uevent.c
===================================================================
@@ -28,9 +28,6 @@
 u64 uevent_seqnum;
 char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
 static DEFINE_SPINLOCK(sequence_lock);
-#if defined(CONFIG_NET)
-static struct sock *uevent_sock;
-#endif
 
 /* the strings here must match the enum in include/linux/kobject.h */
 static const char *kobject_actions[] = {
@@ -203,7 +200,7 @@ int kobject_uevent_env(struct kobject *k
 
 #if defined(CONFIG_NET)
 	/* send netlink message */
-	if (uevent_sock) {
+	if (kobj->net->uevent_sock) {
 		struct sk_buff *skb;
 		size_t len;
 
@@ -225,7 +222,8 @@ int kobject_uevent_env(struct kobject *k
 			}
 
 			NETLINK_CB(skb).dst_group = 1;
-			netlink_broadcast(uevent_sock, skb, 0, 1, GFP_KERNEL);
+			netlink_broadcast(kobj->net->uevent_sock,
+					  skb, 0, 1, GFP_KERNEL);
 		}
 	}
 #endif
@@ -307,11 +305,11 @@ int add_uevent_var(struct kobj_uevent_en
 EXPORT_SYMBOL_GPL(add_uevent_var);
 
 #if defined(CONFIG_NET)
-static int __init kobject_uevent_init(void)
+static int kobject_uevent_net_init(struct net *net)
 {
-	uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
+	net->uevent_sock = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT,
 					    1, NULL, NULL, THIS_MODULE);
-	if (!uevent_sock) {
+	if (!net->uevent_sock) {
 		printk(KERN_ERR
 		       "kobject_uevent: unable to create netlink socket!\n");
 		return -ENODEV;
@@ -320,5 +318,21 @@ static int __init kobject_uevent_init(vo
 	return 0;
 }
 
+static void kobject_uevent_net_exit(struct net *net)
+{
+	netlink_kernel_release(net->uevent_sock);
+}
+
+static struct pernet_operations kobject_uevent_net_ops = {
+	.init = kobject_uevent_net_init,
+	.exit = kobject_uevent_net_exit,
+};
+
+
+static int __init kobject_uevent_init(void)
+{
+	return register_pernet_subsys(&kobject_uevent_net_ops);
+}
+
 postcore_initcall(kobject_uevent_init);
 #endif
net/core/net-sysfs.c +1 -0
Index: net-next-2.6/net/core/net-sysfs.c
===================================================================
@@ -491,6 +491,7 @@ int netdev_register_kobject(struct net_d
 	dev->class = &net_class;
 	dev->platform_data = net;
 	dev->groups = groups;
+	dev->kobj.net = dev_net(net);
 
 	BUILD_BUG_ON(BUS_ID_SIZE < IFNAMSIZ);
 	dev_set_name(dev, net->name);
#1 lore raw

On Mon, Nov 24, 2008 at 11:50, Daniel Lezcano <[email protected]> wrote:

struct kobject {
const char *name;
struct list_head entry;
@@ -63,6 +65,9 @@ struct kobject {
struct kset *kset;
struct kobj_type *ktype;
struct sysfs_dirent *sd;
+#ifdef CONFIG_NET
+ struct net *net;
+#endif
struct kref kref;
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;

We cannot do that. Network specific stuff does not belong into kobjects. Kobjects are not in any way subsystem specific, and we need to keep it that way.

Thanks,
Kay

#2 lore raw
Quoted 16 lines · Kay Sievers

On Mon, Nov 24, 2008 at 11:50, Daniel Lezcano <[email protected]> wrote:

struct kobject {

include/linux/kobject.h +3 -0
 	const char		*name;
 	struct list_head	entry;
@@ -63,6 +65,9 @@ struct kobject {
 	struct kset		*kset;
 	struct kobj_type	*ktype;
 	struct sysfs_dirent	*sd;
+#ifdef CONFIG_NET
+	struct net              *net;
+#endif
 	struct kref		kref;
 	unsigned int state_initialized:1;
 	unsigned int state_in_sysfs:1;
Quoted 3 lines

We cannot do that. Network specific stuff does not belong into kobjects. Kobjects are not in any way subsystem specific, and we need to keep it that way.

That makes sense :)

Is there a way to follow up from the kobject, the netdev associated with it ? I mean in the function kobject_uevent_env, how can I check the event is related to a network device and retrieve the struct net_device from it ?

Thanks.
-- Daniel

#3 lore raw
In reply to Daniel Lezcano
Quoted 5 lines · Daniel Lezcano

Subject: Handle uevent per namespace
From: Daniel Lezcano <[email protected]>

Quoted 8 lines

At present when a network device is destroyed, inside a network namespace, and this device has the same name as one network device belonging to the initial network namespace (eg. eth0), the udev daemon will disable the interface in the initial network namespace.

IMHO, udev should not receive this event. The uevents should be per namespace or at least do not send events when not for the initial network namespace.

IMHO, network namespaces are a mess and not something that you should be doing at all :)

Quoted 3 lines

The following patch is a RFC for making uevent namespace aware. I don't know this part of the kernel code, so I am pretty sure t is not the right way to do that :)

Like Kay said, please don't change the kobject core for this, try just filtering in the network core the events that you handle there.

good luck,

greg k-h

#4 lore raw
Quoted 14 lines · Greg KH
Quoted 11 lines · Daniel Lezcano

Subject: Handle uevent per namespace
From: Daniel Lezcano <[email protected]>

At present when a network device is destroyed, inside a network namespace, and this device has the same name as one network device belonging to the initial network namespace (eg. eth0), the udev daemon will disable the interface in the initial network namespace.

IMHO, udev should not receive this event. The uevents should be per namespace or at least do not send events when not for the initial network namespace.

Quoted 8 lines

IMHO, network namespaces are a mess and not something that you should be doing at all :)

Quoted 2 lines

The following patch is a RFC for making uevent namespace aware. I don't
know this part of the kernel code, so I am pretty sure t is not the right way to do that :)

Like Kay said, please don't change the kobject core for this, try just filtering in the network core the events that you handle there.

Oh, why I didn't think about that :)
That will be a cleaner and a smaller patch.

Thanks.
-- Daniel