Skip to contentSkip to conversation

netns: Add get_net_ns_by_id() to get a reference to a network namespace from its nsid

PATCH In review Vivien Chappelier 1 participant 1 message Oct 28, 2008

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

PATCH 3/6 netns: Add get_net_ns_by_id() to get a reference to a network namespace from its nsid

This patch adds a __get_net_ns_by_id() to obtain look for and obtain a reference to a network namespace via its nsid. The corresponding get_net_ns_by_id() takes the rtnl_lock.

---
 include/net/net_namespace.h |   34 +++++++++++++++++++++++++++++++---
 net/core/net_namespace.c    |   12 ++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)
include/net/net_namespace.h +31 -3
index 8bde629..afb2578 100644
@@ -104,8 +104,13 @@ static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
 
 extern struct list_head net_namespace_list;
 
+
+#define for_each_net(VAR)				\
+	list_for_each_entry(VAR, &net_namespace_list, list)
+
 #ifdef CONFIG_NET_NS
 extern void __put_net(struct net *net);
+extern struct net *get_net_ns_by_id(int id);
 
 static inline int net_alive(struct net *net)
 {
@@ -141,6 +146,19 @@ int net_eq(const struct net *net1, const struct net *net2)
 {
 	return net1 == net2;
 }
+
+static inline struct net *__get_net_ns_by_id(int id)
+{
+	struct net *net;
+
+	for_each_net(net)
+		if (net->id == id)
+			return maybe_get_net(net);
+
+	return NULL;
+}
+extern struct net *get_net_ns_by_id(int id);
+
 #else
 
 static inline int net_alive(struct net *net)
@@ -167,6 +185,19 @@ int net_eq(const struct net *net1, const struct net *net2)
 {
 	return 1;
 }
+
+static inline struct net *__get_net_ns_by_id(int id)
+{
+	if (id == 0)
+		return &init_net;
+	return NULL;
+}
+
+static inline struct net *get_net_ns_by_id(int id)
+{
+	return __get_net_ns_by_id(id);
+}
+
 #endif
 
 
@@ -195,9 +226,6 @@ static inline void release_net(struct net *net)
 #endif
 
 
-#define for_each_net(VAR)				\
-	list_for_each_entry(VAR, &net_namespace_list, list)
-
 #ifdef CONFIG_NET_NS
 #define __net_init
 #define __net_exit
net/core/net_namespace.c +12 -0
index 418abe0..596cb83 100644
@@ -203,6 +203,18 @@ void __put_net(struct net *net)
 }
 EXPORT_SYMBOL_GPL(__put_net);
 
+struct net *get_net_ns_by_id(int id)
+{
+	struct net *net;
+
+	rtnl_lock();
+	net = __get_net_ns_by_id(id);
+	rtnl_unlock();
+
+	return net;
+}
+EXPORT_SYMBOL_GPL(get_net_ns_by_id);
+
 #else
 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
 {
Signature · 1 line
1.5.4.4