netns: enhance netlink interface for nsid
Subsystems (2)
cover netns: enhance netlink interface for nsid
The first patch has been cherry-picked from the net tree to avoid any conflict later.
The second patch is a small cleanup. The third patch implements notifications for netns id events. And the last one allows to dump existing netns id from userland.
iproute2 patches are available, I can send them on demand.
The diffstat does not include the first patch.
include/uapi/linux/rtnetlink.h | 4 ++ net/core/net_namespace.c | 103 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 10 deletions(-)
Comments are welcome.
Regards,
Nicolas
PATCH 1/4 netns: don't clear nsid too early on removal
With the current code, ids are removed too early.
Suppose you have an ipip interface that stands in the netns foo and its link part in the netns bar (so the netns bar has an nsid into the netns foo). Now, you remove the netns bar:
- the bar nsid into the netns foo is removed
- the netns exit method of ipip is called, thus our ipip iface is removed:
=> a netlink message is sent in the netns foo to advertise this deletion => this netlink message requests an nsid for bar, thus a new nsid is
allocated for bar and never removed.
We must remove nsids when we are sure that nobody will refer to netns currently cleaned.
---
This patch comes from the net tree.
net/core/net_namespace.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
net/core/net_namespace.c +15 -9
@@ -338,7 +338,7 @@ static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */static void cleanup_net (struct work_struct *work) {const struct pernet_operations *ops; -struct net *net, *tmp; +struct net *net, *tmp, *peer;struct list_head net_kill_list;LIST_HEAD (net_exit_list);
@@ -354,14 +354,6 @@ static void cleanup_net(struct work_struct *work)list_for_each_entry (net, &net_kill_list, cleanup_list) {list_del_rcu (&net->list);list_add_tail (&net->exit_list, &net_exit_list); -for_each_net (tmp) { -int id =__peernet2id (tmp, net, false); - -if (id >=0 ) -idr_remove (&tmp->netns_ids, id); - } -idr_destroy (&net->netns_ids); - }rtnl_unlock ();
@@ -387,12 +379,26 @@ static void cleanup_net(struct work_struct *work)*/ rcu_barrier (); +rtnl_lock ();/* Finally it is safe to free my network namespace structure */ list_for_each_entry_safe (net, tmp, &net_exit_list, exit_list) { +/* Unreference net from all peers (no need to loop over +* net_exit_list because idr_destroy() will be called for each +* element of this list. +*/ +for_each_net (peer) { +int id =__peernet2id (peer, net, false); + +if (id >=0 ) +idr_remove (&peer->netns_ids, id); + } +idr_destroy (&net->netns_ids); +list_del_init (&net->exit_list);put_user_ns (net->user_ns);net_drop_ns (net); } +rtnl_unlock (); } static DECLARE_WORK(net_cleanup_work, cleanup_net);
Signature · 1 line
2.2.2
Re: [PATCH net-next 1/4] netns: don't clear nsid too early on removal
Quoted 10 lines · Nicolas Dichtel
With the current code, ids are removed too early.
Suppose you have an ipip interface that stands in the netns foo and its link part in the netns bar (so the netns bar has an nsid into the netns foo). Now, you remove the netns bar:
- the bar nsid into the netns foo is removed
- the netns exit method of ipip is called, thus our ipip iface is removed:
=> a netlink message is sent in the netns foo to advertise this deletion => this netlink message requests an nsid for bar, thus a new nsid is
allocated for bar and never removed.
Quoted 2 lines
We must remove nsids when we are sure that nobody will refer to netns currently cleaned.
I missed this issue but I have grave reservations about moving this destruction of ids later.
It should not be possible to find by any kind of lookup network namespaces that are going through cleanup net.
There should be no network sockets and thus no in flight rtnl traffic at the time cleanup_net is metioned so I don't see how this patch fixes the mentioned commit.
I have a second issue with the fact that the code is unnecessarily quadratic. We should keep a list of the issues netns ids and just revoke them instead of walking the whole network namespaces.
I strongly suspect that this change makes it possible to create a network device whose bottom is in a network namespace we are destroying after we have destroyed all of the network devices in that namespace and
otherwise cleaned up. Beyond that I can not reason about this patch
because it opens up a huge number of races.
Quoted 65 lines
Fixes 0c7aecd4bde4 ("netns: add rtnl cmd to add and get peer netns ids")Signed-off-by Nicolas Dichtel <[email protected]>Signed-off-by David S. Miller <[email protected]>(cherry picked from commit 4217291e592da0e4258b652e82e5428639d29acc)---
This patch comes from the net tree.
net/core/net_namespace.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)net/core/net_namespace.c +15
-9@@ -338,7 +338,7 @@ static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */static void cleanup_net (struct work_struct *work) {const struct pernet_operations *ops; -struct net *net, *tmp; +struct net *net, *tmp, *peer;struct list_head net_kill_list;LIST_HEAD (net_exit_list);@@ -354,14 +354,6 @@ static void cleanup_net(struct work_struct *work)list_for_each_entry (net, &net_kill_list, cleanup_list) {list_del_rcu (&net->list);list_add_tail (&net->exit_list, &net_exit_list); -for_each_net (tmp) { -int id =__peernet2id (tmp, net, false); - -if (id >=0 ) -idr_remove (&tmp->netns_ids, id); - } -idr_destroy (&net->netns_ids); - }rtnl_unlock ();@@ -387,12 +379,26 @@ static void cleanup_net(struct work_struct *work)*/ rcu_barrier (); +rtnl_lock ();/* Finally it is safe to free my network namespace structure */ list_for_each_entry_safe (net, tmp, &net_exit_list, exit_list) { +/* Unreference net from all peers (no need to loop over +* net_exit_list because idr_destroy() will be called for each +* element of this list. +*/ +for_each_net (peer) { +int id =__peernet2id (peer, net, false); + +if (id >=0 ) +idr_remove (&peer->netns_ids, id); + } +idr_destroy (&net->netns_ids); +list_del_init (&net->exit_list);put_user_ns (net->user_ns);net_drop_ns (net); } +rtnl_unlock (); } static DECLARE_WORK(net_cleanup_work, cleanup_net);
Re: [PATCH net-next 1/4] netns: don't clear nsid too early on removal
Le 02/04/2015 20:51, Eric W. Biederman a écrit :
[snip]
Quoted 4 lines
There should be no network sockets and thus no in flight rtnl traffic at the time
cleanup_netis metioned so I don't see how this patch fixes the mentioned commit.
Yes and no.
Yes, there is no network sockets into this netns, *but* modules build netlink messages because they don't know if there are listeners or not.
Quoted 10 lines
I have a second issue with the fact that the code is unnecessarily quadratic. We should keep a list of the issues netns ids and just revoke them instead of walking the whole network namespaces.
I strongly suspect that this change makes it possible to create a network device whose bottom is in a network namespace we are destroying after we have destroyed all of the network devices in that namespace and
otherwise cleaned up. Beyond that I can not reason about this patch
because it opens up a huge number of races.
Ok, you're probably right.
I will send an update.
PATCH 1/2 Revert "netns: don't clear nsid too early on removal"
This reverts
commit 4217291e592d ("netns: don't clear nsid too early on removal").
This is not the right fix, it introduces races.
--- net/core/net_namespace.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
net/core/net_namespace.c +9 -15
@@ -349,7 +349,7 @@ static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */static void cleanup_net (struct work_struct *work) {const struct pernet_operations *ops; -struct net *net, *tmp, *peer; +struct net *net, *tmp;struct list_head net_kill_list;LIST_HEAD (net_exit_list);
@@ -365,6 +365,14 @@ static void cleanup_net(struct work_struct *work)list_for_each_entry (net, &net_kill_list, cleanup_list) {list_del_rcu (&net->list);list_add_tail (&net->exit_list, &net_exit_list); +for_each_net (tmp) { +int id =__peernet2id (tmp, net, false); + +if (id >=0 ) +idr_remove (&tmp->netns_ids, id); + } +idr_destroy (&net->netns_ids); + }rtnl_unlock ();
@@ -390,26 +398,12 @@ static void cleanup_net(struct work_struct *work)*/ rcu_barrier (); -rtnl_lock ();/* Finally it is safe to free my network namespace structure */ list_for_each_entry_safe (net, tmp, &net_exit_list, exit_list) { -/* Unreference net from all peers (no need to loop over -* net_exit_list because idr_destroy() will be called for each -* element of this list. -*/ -for_each_net (peer) { -int id =__peernet2id (peer, net, false); - -if (id >=0 ) -idr_remove (&peer->netns_ids, id); - } -idr_destroy (&net->netns_ids); -list_del_init (&net->exit_list);put_user_ns (net->user_ns);net_drop_ns (net); } -rtnl_unlock (); } static DECLARE_WORK(net_cleanup_work, cleanup_net);
Signature · 1 line
2.2.2
PATCH 2/2 netns: don't allocate an id for dead netns
First, let's explain the problem.
Suppose you have an ipip interface that stands in the netns foo and its link part in the netns bar (so the netns bar has an nsid into the netns foo). Now, you remove the netns bar:
- the bar nsid into the netns foo is removed
- the netns exit method of ipip is called, thus our ipip iface is removed:
=> a netlink message is built in the netns foo to advertise this deletion => this netlink message requests an nsid for bar, thus a new nsid is
allocated for bar and never removed.
This patch adds a check in peernet2id() so that an id cannot be allocated for a netns which is currently destroyed.
--- net/core/net_namespace.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/core/net_namespace.c +3 -1
@@ -198,8 +198,10 @@ static int __peernet2id(struct net *net, struct net *peer, bool alloc)*/ int peernet2id (struct net *net,struct net *peer) { -int id =__peernet2id (net, peer, true); +bool alloc =atomic_read (&peer->count) ==0 ? false : true; +int id; + id =__peernet2id (net, peer, alloc);return id >=0 ? id :NETNSA_NSID_NOT_ASSIGNED ; }EXPORT_SYMBOL (peernet2id);
Signature · 1 line
2.2.2
Re: [PATCH net 2/2] netns: don't allocate an id for dead netns
From: Nicolas Dichtel <[email protected]>
Date: Fri, 3 Apr 2015 12:02:37 +0200
Quoted 10 lines
First, let's explain the problem.
Suppose you have an ipip interface that stands in the netns foo and its link part in the netns bar (so the netns bar has an nsid into the netns foo). Now, you remove the netns bar:
- the bar nsid into the netns foo is removed
- the netns exit method of ipip is called, thus our ipip iface is removed:
=> a netlink message is built in the netns foo to advertise this deletion => this netlink message requests an nsid for bar, thus a new nsid is
allocated for bar and never removed.
Quoted 4 lines
This patch adds a check in
peernet2id()so that an id cannot be allocated for a netns which is currently destroyed.Signed-off-by Nicolas Dichtel <[email protected]>
Applied.
Re: [PATCH net 2/2] netns: don't allocate an id for dead netns
Quoted 13 lines · David Miller
From: Nicolas Dichtel <[email protected]>
Date: Fri, 3 Apr 2015 12:02:37 +0200Quoted 10 lines
First, let's explain the problem.
Suppose you have an ipip interface that stands in the netns foo and its link part in the netns bar (so the netns bar has an nsid into the netns foo). Now, you remove the netns bar:
- the bar nsid into the netns foo is removed
- the netns exit method of ipip is called, thus our ipip iface is removed:
=> a netlink message is built in the netns foo to advertise this deletion => this netlink message requests an nsid for bar, thus a new nsid is
allocated for bar and never removed.
Quoted 7 lines
Quoted 4 lines
This patch adds a check in
peernet2id()so that an id cannot be allocated for a netns which is currently destroyed.Signed-off-by Nicolas Dichtel <[email protected]>Applied.
I don't see these patches in your tree, maybe you forget to push them on kernel.org?
My other series will conflict with these patches, is it possible to merge net into net-next after them?
David Miller
maintainerRe: [PATCH net 2/2] netns: don't allocate an id for dead netns
From: Nicolas Dichtel <[email protected]>
Date: Sun, 05 Apr 2015 10:39:53 +0200
Quoted 2 lines
I don't see these patches in your tree, maybe you forget to push them on kernel.org?
Indeed, I did, pushed out now. Sorry about that.
Re: [PATCH net 2/2] netns: don't allocate an id for dead netns
Quoted 8 lines · David Miller
From: Nicolas Dichtel <[email protected]>
Date: Sun, 05 Apr 2015 10:39:53 +0200Quoted 2 lines
I don't see these patches in your tree, maybe you forget to push them on kernel.org?
Indeed, I did, pushed out now. Sorry about that.
No problem, thank you.
Re: [PATCH net 1/2] Revert "netns: don't clear nsid too early on removal"
In reply to Nicolas DichtelFrom: Nicolas Dichtel <[email protected]>
Date: Fri, 3 Apr 2015 12:02:36 +0200
Quoted 6 lines
This reverts
commit4217291e592d("netns: don't clear nsid too early on removal").This is not the right fix, it introduces races.
Signed-off-by Nicolas Dichtel <[email protected]>
Applied.
PATCH 2/4 netns: minor cleanup in rtnl_net_getid()
In reply to Nicolas DichtelNo need to initialize err, it will be overridden by the value of nlmsg_parse().
--- net/core/net_namespace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net_namespace.c +1 -1
@@ -569,8 +569,8 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)struct net *net =sock_net (skb->sk);struct nlattr *tb[NETNSA_MAX +1 ];struct sk_buff *msg; -int err = -ENOBUFS ;struct net *peer; +int err; err =nlmsg_parse (nlh,sizeof (struct rtgenmsg ), tb,NETNSA_MAX , rtnl_net_policy);
Signature · 1 line
2.2.2
PATCH 3/4 netns: notify netns id events
In reply to Nicolas DichtelWith this patch, netns ids that are created and deleted are advertised into the group RTNLGRP_NSID.
Because callers of rtnl_net_notifyid() already know the id of the peer, there is no need to call __peernet2id() in rtnl_net_fill().
--- include/uapi/linux/rtnetlink.h | 4 ++++ net/core/net_namespace.c | 52 +++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 8 deletions(-)
include/uapi/linux/rtnetlink.h +4 -0
@@ -134,6 +134,8 @@ enum { RTM_NEWNSID = 88,#define RTM_NEWNSID RTM_NEWNSID + RTM_DELNSID = 89, +#define RTM_DELNSID RTM_DELNSID RTM_GETNSID = 90,#define RTM_GETNSID RTM_GETNSID
@@ -635,6 +637,8 @@ enum rtnetlink_groups {#define RTNLGRP_MDB RTNLGRP_MDB RTNLGRP_MPLS_ROUTE,#define RTNLGRP_MPLS_ROUTE RTNLGRP_MPLS_ROUTE + RTNLGRP_NSID, +#define RTNLGRP_NSID RTNLGRP_NSID __RTNLGRP_MAX };#define RTNLGRP_MAX (__RTNLGRP_MAX - 1)
net/core/net_namespace.c +44 -8
97 lines not on this page · raw message
Signature · 1 line
2.2.2
PATCH 4/4 netns: allow to dump netns ids
In reply to Nicolas DichtelWhich this patch, it's possible to dump the list of ids allocated for peer netns.
--- net/core/net_namespace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-)
net/core/net_namespace.c +48 -1
@@ -620,6 +620,52 @@ out:return err; } +struct rtnl_net_dump_cb { +struct net *net; +struct sk_buff *skb; +struct netlink_callback *cb; +int idx; +int s_idx; +}; + +static int rtnl_net_dumpid_one (int id,void *peer,void *data) +{ +struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data; +int ret; + +if (net_cb->idx < net_cb->s_idx) + goto cont; + + ret =rtnl_net_fill (net_cb->skb,NETLINK_CB (net_cb->cb->skb).portid, + net_cb->cb->nlh->nlmsg_seq,NLM_F_MULTI , +RTM_NEWNSID , net_cb->net, peer, id); +if (ret <0 ) +return ret; + +cont: + net_cb->idx++; +return 0 ; +} + +static int rtnl_net_dumpid (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct net *net =sock_net (skb->sk); +struct rtnl_net_dump_cb net_cb = { + .net = net, + .skb = skb, + .cb = cb, + .idx =0 , + .s_idx = cb->args[0 ], + }; + +ASSERT_RTNL (); + +idr_for_each (&net->netns_ids, rtnl_net_dumpid_one, &net_cb); + + cb->args[0 ] = net_cb.idx; +return skb->len; +} +static void rtnl_net_notifyid (struct net *net,struct net *peer,int cmd,int id) {
@@ -677,7 +723,8 @@ static int __init net_ns_init(void)register_pernet_subsys (&net_ns_ops);rtnl_register (PF_UNSPEC ,RTM_NEWNSID , rtnl_net_newid,NULL ,NULL ); -rtnl_register (PF_UNSPEC ,RTM_GETNSID , rtnl_net_getid,NULL ,NULL ); +rtnl_register (PF_UNSPEC ,RTM_GETNSID , rtnl_net_getid, rtnl_net_dumpid, +NULL );return 0 ; }
Signature · 1 line
2.2.2
Quoted 4 lines · Nicolas Dichtel
The first patch has been cherry-picked from the net tree to avoid any conflict later.
You don't need to cherry-pick, you just need to wait for net merged into net-next,
or simply tell DaveM your patches apply after the merge.
Quoted 10 lines · Cong Wang
Quoted 4 lines · Nicolas Dichtel
The first patch has been cherry-picked from the net tree to avoid any conflict later.
You don't need to cherry-pick, you just need to wait for net merged into net-next,
or simply tell DaveM your patches apply after the merge.
In fact, the goal of the cherry-pick was to inform David of that dependency. I didn't expect him to include this patch in net-next ;-)
v2 cover netns: enhance netlink interface for nsid
In reply to Nicolas DichtelThe first patch is a small cleanup. The second patch implements notifications for netns id events. And the last one allows to dump existing netns id from userland.
iproute2 patches are available, I can send them on demand.
v2: drop the first patch (the fix is now in net-next)
include/uapi/linux/rtnetlink.h | 4 ++ net/core/net_namespace.c | 103 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 10 deletions(-)
Comments are welcome.
Regards,
Nicolas
PATCH v2 1/3 netns: minor cleanup in rtnl_net_getid()
No need to initialize err, it will be overridden by the value of nlmsg_parse().
--- net/core/net_namespace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/net_namespace.c +1 -1
@@ -565,8 +565,8 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)struct net *net =sock_net (skb->sk);struct nlattr *tb[NETNSA_MAX +1 ];struct sk_buff *msg; -int err = -ENOBUFS ;struct net *peer; +int err; err =nlmsg_parse (nlh,sizeof (struct rtgenmsg ), tb,NETNSA_MAX , rtnl_net_policy);
Signature · 1 line
2.2.2
PATCH v2 2/3 netns: notify netns id events
In reply to Nicolas DichtelWith this patch, netns ids that are created and deleted are advertised into the group RTNLGRP_NSID.
Because callers of rtnl_net_notifyid() already know the id of the peer, there is no need to call __peernet2id() in rtnl_net_fill().
--- include/uapi/linux/rtnetlink.h | 4 ++++ net/core/net_namespace.c | 52 +++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 8 deletions(-)
include/uapi/linux/rtnetlink.h +4 -0
@@ -134,6 +134,8 @@ enum { RTM_NEWNSID = 88,#define RTM_NEWNSID RTM_NEWNSID + RTM_DELNSID = 89, +#define RTM_DELNSID RTM_DELNSID RTM_GETNSID = 90,#define RTM_GETNSID RTM_GETNSID
@@ -635,6 +637,8 @@ enum rtnetlink_groups {#define RTNLGRP_MDB RTNLGRP_MDB RTNLGRP_MPLS_ROUTE,#define RTNLGRP_MPLS_ROUTE RTNLGRP_MPLS_ROUTE + RTNLGRP_NSID, +#define RTNLGRP_NSID RTNLGRP_NSID __RTNLGRP_MAX };#define RTNLGRP_MAX (__RTNLGRP_MAX - 1)
net/core/net_namespace.c +44 -8
97 lines not on this page · raw message
Signature · 1 line
2.2.2
PATCH v2 3/3 netns: allow to dump netns ids
In reply to Nicolas DichtelWhich this patch, it's possible to dump the list of ids allocated for peer netns.
--- net/core/net_namespace.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-)
net/core/net_namespace.c +48 -1
@@ -616,6 +616,52 @@ out:return err; } +struct rtnl_net_dump_cb { +struct net *net; +struct sk_buff *skb; +struct netlink_callback *cb; +int idx; +int s_idx; +}; + +static int rtnl_net_dumpid_one (int id,void *peer,void *data) +{ +struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data; +int ret; + +if (net_cb->idx < net_cb->s_idx) + goto cont; + + ret =rtnl_net_fill (net_cb->skb,NETLINK_CB (net_cb->cb->skb).portid, + net_cb->cb->nlh->nlmsg_seq,NLM_F_MULTI , +RTM_NEWNSID , net_cb->net, peer, id); +if (ret <0 ) +return ret; + +cont: + net_cb->idx++; +return 0 ; +} + +static int rtnl_net_dumpid (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct net *net =sock_net (skb->sk); +struct rtnl_net_dump_cb net_cb = { + .net = net, + .skb = skb, + .cb = cb, + .idx =0 , + .s_idx = cb->args[0 ], + }; + +ASSERT_RTNL (); + +idr_for_each (&net->netns_ids, rtnl_net_dumpid_one, &net_cb); + + cb->args[0 ] = net_cb.idx; +return skb->len; +} +static void rtnl_net_notifyid (struct net *net,struct net *peer,int cmd,int id) {
@@ -673,7 +719,8 @@ static int __init net_ns_init(void)register_pernet_subsys (&net_ns_ops);rtnl_register (PF_UNSPEC ,RTM_NEWNSID , rtnl_net_newid,NULL ,NULL ); -rtnl_register (PF_UNSPEC ,RTM_GETNSID , rtnl_net_getid,NULL ,NULL ); +rtnl_register (PF_UNSPEC ,RTM_GETNSID , rtnl_net_getid, rtnl_net_dumpid, +NULL );return 0 ; }
Signature · 1 line
2.2.2
David Miller
maintainerFrom: Nicolas Dichtel <[email protected]>
Date: Tue, 7 Apr 2015 11:51:51 +0200
Quoted 7 lines
The first patch is a small cleanup. The second patch implements notifications for netns id events. And the last one allows to dump existing netns id from userland.
iproute2 patches are available, I can send them on demand.
v2: drop the first patch (the fix is now in net-next)
Series applied, thanks.