diff mbox series

[net-next,2/5] rtnetlink: Add netns_atomic flag in rtnl_link_ops

Message ID 20241023023146.372653-3-shaw.leon@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: Improve netns handling in RTNL and ip_tunnel | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 18 this patch: 18
netdev/build_tools success Errors and warnings before: 157 (+1) this patch: 157 (+1)
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 40 this patch: 40
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 2874 this patch: 2874
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 34 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 14 this patch: 14
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-23--12-00 (tests: 777)

Commit Message

Xiao Liang Oct. 23, 2024, 2:31 a.m. UTC
Currently these two steps are needed to create a net device with
IFLA_LINK_NETNSID attr:

 1. create and setup the netdev in the link netns with
    rtnl_create_link()
 2. move it to the target netns with dev_change_net_namespace()

This has some side effects, including extra ifindex allocation, ifname
validation and link notifications in link netns.

Add a netns_atomic flag, that if set to true, devices will be created in
the target netns directly.

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
---
 include/net/rtnetlink.h | 3 +++
 net/core/rtnetlink.c    | 7 ++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

Comments

Kuniyuki Iwashima Oct. 23, 2024, 4:03 a.m. UTC | #1
From: Xiao Liang <shaw.leon@gmail.com>
Date: Wed, 23 Oct 2024 10:31:43 +0800
> Currently these two steps are needed to create a net device with
> IFLA_LINK_NETNSID attr:
> 
>  1. create and setup the netdev in the link netns with
>     rtnl_create_link()
>  2. move it to the target netns with dev_change_net_namespace()

IIRC, this is to send the notification in the link netns.


> 
> This has some side effects, including extra ifindex allocation, ifname
> validation and link notifications in link netns.
> 
> Add a netns_atomic flag, that if set to true, devices will be created in
> the target netns directly.
> 
> Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
> ---
>  include/net/rtnetlink.h | 3 +++
>  net/core/rtnetlink.c    | 7 ++++---
>  2 files changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h
> index e0d9a8eae6b6..59594cef2272 100644
> --- a/include/net/rtnetlink.h
> +++ b/include/net/rtnetlink.h
> @@ -74,6 +74,8 @@ static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
>   *	@srcu: Used internally
>   *	@kind: Identifier
>   *	@netns_refund: Physical device, move to init_net on netns exit
> + *	@netns_atomic: Device can be created in target netns even when
> + *		       link-netns is different, avoiding netns change.
>   *	@maxtype: Highest device specific netlink attribute number
>   *	@policy: Netlink policy for device specific attribute validation
>   *	@validate: Optional validation function for netlink/changelink parameters
> @@ -115,6 +117,7 @@ struct rtnl_link_ops {
>  	void			(*setup)(struct net_device *dev);
>  
>  	bool			netns_refund;
> +	bool			netns_atomic;
>  	unsigned int		maxtype;
>  	const struct nla_policy	*policy;
>  	int			(*validate)(struct nlattr *tb[],
> diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
> index ff8d25acfc00..99250779d8ba 100644
> --- a/net/core/rtnetlink.c
> +++ b/net/core/rtnetlink.c
> @@ -3679,8 +3679,9 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
>  		name_assign_type = NET_NAME_ENUM;
>  	}
>  
> -	dev = rtnl_create_link(link_net ? : tgt_net, ifname,
> -			       name_assign_type, ops, tb, extack);
> +	dev = rtnl_create_link(!link_net || ops->netns_atomic ?
> +			       tgt_net : link_net, ifname, name_assign_type,
> +			       ops, tb, extack);
>  	if (IS_ERR(dev)) {
>  		err = PTR_ERR(dev);
>  		goto out;
> @@ -3700,7 +3701,7 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
>  	err = rtnl_configure_link(dev, ifm, portid, nlh);
>  	if (err < 0)
>  		goto out_unregister;
> -	if (link_net) {
> +	if (link_net && !ops->netns_atomic) {
>  		err = dev_change_net_namespace(dev, tgt_net, ifname);
>  		if (err < 0)
>  			goto out_unregister;
> -- 
> 2.47.0
>
Xiao Liang Oct. 23, 2024, 4:36 a.m. UTC | #2
On Wed, Oct 23, 2024 at 12:03 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> From: Xiao Liang <shaw.leon@gmail.com>
> Date: Wed, 23 Oct 2024 10:31:43 +0800
> > Currently these two steps are needed to create a net device with
> > IFLA_LINK_NETNSID attr:
> >
> >  1. create and setup the netdev in the link netns with
> >     rtnl_create_link()
> >  2. move it to the target netns with dev_change_net_namespace()
>
> IIRC, this is to send the notification in the link netns.
>

Yes. This patch changes this behavior only when the new flag is set.

I doubt if it's really necessary to send link create/delete notifications
to link-netns. Also the current behavior is somewhat inconsistent, say

    1) ip link add netns n1 link-netns n2 link eth0 mac0 type macvlan
    2) ip -n n2 link add netns n1 link eth0 mac0 type macvlan

Intuitively, the two commands are equivalent. But notification is sent
only for 1.
diff mbox series

Patch

diff --git a/include/net/rtnetlink.h b/include/net/rtnetlink.h
index e0d9a8eae6b6..59594cef2272 100644
--- a/include/net/rtnetlink.h
+++ b/include/net/rtnetlink.h
@@ -74,6 +74,8 @@  static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
  *	@srcu: Used internally
  *	@kind: Identifier
  *	@netns_refund: Physical device, move to init_net on netns exit
+ *	@netns_atomic: Device can be created in target netns even when
+ *		       link-netns is different, avoiding netns change.
  *	@maxtype: Highest device specific netlink attribute number
  *	@policy: Netlink policy for device specific attribute validation
  *	@validate: Optional validation function for netlink/changelink parameters
@@ -115,6 +117,7 @@  struct rtnl_link_ops {
 	void			(*setup)(struct net_device *dev);
 
 	bool			netns_refund;
+	bool			netns_atomic;
 	unsigned int		maxtype;
 	const struct nla_policy	*policy;
 	int			(*validate)(struct nlattr *tb[],
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index ff8d25acfc00..99250779d8ba 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -3679,8 +3679,9 @@  static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
 		name_assign_type = NET_NAME_ENUM;
 	}
 
-	dev = rtnl_create_link(link_net ? : tgt_net, ifname,
-			       name_assign_type, ops, tb, extack);
+	dev = rtnl_create_link(!link_net || ops->netns_atomic ?
+			       tgt_net : link_net, ifname, name_assign_type,
+			       ops, tb, extack);
 	if (IS_ERR(dev)) {
 		err = PTR_ERR(dev);
 		goto out;
@@ -3700,7 +3701,7 @@  static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
 	err = rtnl_configure_link(dev, ifm, portid, nlh);
 	if (err < 0)
 		goto out_unregister;
-	if (link_net) {
+	if (link_net && !ops->netns_atomic) {
 		err = dev_change_net_namespace(dev, tgt_net, ifname);
 		if (err < 0)
 			goto out_unregister;