net: Improve netns handling in RTNL and ip_tunnel
Subsystems (25)
v3 cover net: Improve netns handling in RTNL and ip_tunnel
This patch series includes some netns-related improvements and fixes for RTNL and ip_tunnel, to make link creation more intuitive:
- Creating link in another net namespace doesn't conflict with link names in current one.
- Refector rtnetlink link creation. Create link in target namespace directly. Pass both source and link netns to drivers via newlink() callback.
So that
# ip link add netns ns1 link-netns ns2 tun0 type gre ...
will create tun0 in ns1, rather than create it in ns2 and move to ns1. And don't conflict with another interface named "tun0" in current netns.
Patch 1 from Donald is included just as a dependency.
---
v3:
- Drop "netns_atomic" flag and module parameter. Add netns parameter to newlink() instead, and convert drivers accordingly.
- Move python NetNSEnter helper to net selftest lib.
v2:
- Check NLM_F_EXCL to ensure only link creation is affected.
- Add self tests for link name/ifindex conflict and notifications in different netns.
- Changes in dummy driver and ynl in order to add the test case.
v1:
Donald Hunter (1):
Revert "tools/net/ynl: improve async notification handling"
Xiao Liang (5):
net: ip_tunnel: Build flow in underlay net namespace
rtnetlink: Lookup device in target netns when creating link
rtnetlink: Decouple net namespaces in rtnl_newlink_create()
selftests: net: Add python context manager for netns entering
selftests: net: Add two test cases for link netns
drivers/infiniband/ulp/ipoib/ipoib_netlink.c | 6 ++- drivers/net/amt.c | 6 +-- drivers/net/bareudp.c | 4 +- drivers/net/bonding/bond_netlink.c | 3 +- drivers/net/can/dev/netlink.c | 2 +- drivers/net/can/vxcan.c | 4 +- .../ethernet/qualcomm/rmnet/rmnet_config.c | 5 +- drivers/net/geneve.c | 4 +- drivers/net/gtp.c | 4 +- drivers/net/ipvlan/ipvlan.h | 2 +- drivers/net/ipvlan/ipvlan_main.c | 5 +- drivers/net/ipvlan/ipvtap.c | 4 +- drivers/net/macsec.c | 5 +- drivers/net/macvlan.c | 5 +- drivers/net/macvtap.c | 5 +- drivers/net/netkit.c | 4 +- drivers/net/pfcp.c | 4 +- drivers/net/ppp/ppp_generic.c | 4 +- drivers/net/team/team_core.c | 2 +- drivers/net/veth.c | 4 +- drivers/net/vrf.c | 2 +- drivers/net/vxlan/vxlan_core.c | 4 +- drivers/net/wireguard/device.c | 4 +- drivers/net/wireless/virtual/virt_wifi.c | 5 +- drivers/net/wwan/wwan_core.c | 6 ++- include/net/ip_tunnels.h | 5 +- include/net/rtnetlink.h | 22 ++++++++- net/8021q/vlan_netlink.c | 5 +- net/batman-adv/soft-interface.c | 5 +- net/bridge/br_netlink.c | 2 +- net/caif/chnl_net.c | 2 +- net/core/rtnetlink.c | 25 ++++++---- net/hsr/hsr_netlink.c | 8 +-- net/ieee802154/6lowpan/core.c | 5 +- net/ipv4/ip_gre.c | 13 +++-- net/ipv4/ip_tunnel.c | 16 +++--- net/ipv4/ip_vti.c | 5 +- net/ipv4/ipip.c | 5 +- net/ipv6/ip6_gre.c | 17 ++++--- net/ipv6/ip6_tunnel.c | 11 ++--- net/ipv6/ip6_vti.c | 11 ++--- net/ipv6/sit.c | 11 ++--- net/xfrm/xfrm_interface_core.c | 13 +++-- tools/net/ynl/cli.py | 10 ++-- tools/net/ynl/lib/ynl.py | 49 ++++++++----------- tools/testing/selftests/net/Makefile | 1 + .../testing/selftests/net/lib/py/__init__.py | 2 +- tools/testing/selftests/net/lib/py/netns.py | 18 +++++++ tools/testing/selftests/net/netns-name.sh | 10 ++++ tools/testing/selftests/net/netns_atomic.py | 38 ++++++++++++++ 50 files changed, 255 insertions(+), 157 deletions(-) create mode 100755 tools/testing/selftests/net/netns_atomic.py
Signature · 1 line
2.47.0
PATCH v3 1/6 Revert "tools/net/ynl: improve async notification handling"
From: Donald Hunter <[email protected]>
This reverts commit 1bf70e6c3a5346966c25e0a1ff492945b25d3f80.
This modification to check_ntf() is being reverted so that its behaviour remains equivalent to ynl_ntf_check() in the C YNL. Instead a new poll_ntf() will be added in a separate patch.
--- tools/net/ynl/cli.py | 10 +++----- tools/net/ynl/lib/ynl.py | 49 ++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 36 deletions(-)
tools/net/ynl/cli.py +3 -7
@@ -5,7 +5,6 @@ import argparse import json import pprint import time -import signal from lib import YnlFamily, Netlink, NlError
@@ -18,8 +17,6 @@ class YnlEncoder(json.JSONEncoder): return list(obj) return json.JSONEncoder.default(self, obj) -def handle_timeout(sig, frame): - exit(0) def main(): description = """
@@ -84,8 +81,7 @@ def main(): ynl.ntf_subscribe(args.ntf) if args.sleep: - signal.signal(signal.SIGALRM, handle_timeout) - signal.alarm(args.sleep) + time.sleep(args.sleep) if args.list_ops: for op_name, op in ynl.ops.items():
@@ -110,8 +106,8 @@ def main(): exit(1) if args.ntf: - for msg in ynl.check_ntf(): - output(msg) + ynl.check_ntf() + output(ynl.async_msg_queue) if __name__ == "__main__":
tools/net/ynl/lib/ynl.py +20 -29
74 lines not on this page · raw message
Signature · 1 line
2.47.0
PATCH v3 2/6 net: ip_tunnel: Build flow in underlay net namespace
In reply to Xiao LiangBuild IPv4 flow in underlay net namespace, where encapsulated packets are routed.
--- net/ipv4/ip_tunnel.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/ipv4/ip_tunnel.c +3 -3
@@ -294,7 +294,7 @@ static int ip_tunnel_bind_dev(struct net_device *dev)ip_tunnel_init_flow (&fl4, iph->protocol, iph->daddr, iph->saddr, tunnel->parms.o_key, - iph->tos &INET_DSCP_MASK ,dev_net (dev), + iph->tos &INET_DSCP_MASK , tunnel->net, tunnel->parms.link, tunnel->fwmark,0 ,0 ); rt =ip_route_output_key (tunnel->net, &fl4);
@@ -611,7 +611,7 @@ void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, }ip_tunnel_init_flow (&fl4, proto, key->u.ipv4.dst, key->u.ipv4.src,tunnel_id_to_key32 (key->tun_id), - tos &INET_DSCP_MASK ,dev_net (dev),0 , skb->mark, + tos &INET_DSCP_MASK , tunnel->net,0 , skb->mark,skb_get_hash (skb), key->flow_flags);if (!tunnel_hlen)
@@ -774,7 +774,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,ip_tunnel_init_flow (&fl4, protocol, dst, tnl_params->saddr, tunnel->parms.o_key, tos &INET_DSCP_MASK , -dev_net (dev),READ_ONCE (tunnel->parms.link), + tunnel->net,READ_ONCE (tunnel->parms.link), tunnel->fwmark,skb_get_hash (skb),0 );if (ip_tunnel_encap (skb, &tunnel->encap, &protocol, &fl4) <0 )
Signature · 1 line
2.47.0
PATCH v3 3/6 rtnetlink: Lookup device in target netns when creating link
In reply to Xiao LiangWhen creating link, lookup for existing device in target net namespace instead of current one.
For example, two links created by:
# ip link add dummy1 type dummy # ip link add netns ns1 dummy1 type dummy
should have no conflict since they are in different namespaces.
--- net/core/rtnetlink.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
net/core/rtnetlink.c +8 -2
@@ -3846,20 +3846,26 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, {struct nlattr **const tb = tbs->tb;struct net *net =sock_net (skb->sk); +struct net *device_net;struct net_device *dev;struct ifinfomsg *ifm;bool link_specified; +/* When creating, lookup for existing device in target net namespace */ + device_net = (nlh->nlmsg_flags &NLM_F_CREATE ) && + (nlh->nlmsg_flags &NLM_F_EXCL ) ? + tgt_net : net; + ifm =nlmsg_data (nlh);if (ifm->ifi_index >0 ) { link_specified = true; - dev =__dev_get_by_index (net, ifm->ifi_index); + dev =__dev_get_by_index (device_net, ifm->ifi_index); }else if (ifm->ifi_index <0 ) {NL_SET_ERR_MSG (extack,"ifindex can't be negative" );return -EINVAL ; }else if (tb[IFLA_IFNAME ] || tb[IFLA_ALT_IFNAME ]) { link_specified = true; - dev =rtnl_dev_get (net, tb); + dev =rtnl_dev_get (device_net, tb); }else { link_specified = false; dev =NULL ;
Signature · 1 line
2.47.0
PATCH v3 4/6 rtnetlink: Decouple net namespaces in rtnl_newlink_create()
In reply to Xiao LiangThere are three net namespaces involved when creating links:
- source netns - where the netlink socket resides,
- target netns - where to put the device being created,
- link netns - netns associated with the device (backend).
Currently, two nets are passed to newlink() callback - "src_net" parameter and "dev_net" (implicitly in net_device). They are set as follows, depending on whether IFLA_LINK_NETNSID is present.
+-------------------+---------+---------+ | IFLA_LINK_NETNSID | src_net | dev_net | +-------------------+---------+---------+ | absent | source | target | +-------------------+---------+---------+ | present | link | link | +-------------------+---------+---------+
When IFLA_LINK_NETNSID is present, the device is created in link netns first. This has some side effects, including extra ifindex allocation, ifname validation and link notifications. There's also an extra step to move the device to target netns. These could be avoided if we create it in target netns at the beginning.
On the other hand, the meaning of src_net is ambiguous. It should be the effective link netns by design, but some drivers ignore it and use dev_net instead.
This patch refactors netns handling. rtnl_newlink_create() now creates devices in target netns directly, so dev_net is always target netns. Source and link netns are passed to newlink() as src_net and link_net in a new struct consistently.
When determining the effective link netns, in the absence link_net, drivers should look for src_net in general. But for compatibility, drivers that use dev_net will keep current behavior.
---
There're some issues found when coverting drivers. Please check if they work as intended:
- In amt_newlink() drivers/net/amt.c:
amt->net = net;
...
amt->stream_dev = dev_get_by_index(net, ... Uses net (src_net actually), but amt_lookup_upper_dev() only searches in dev_net.
- In gtp_newlink() in drivers/net/gtp.c:
gtp->net = src_net;
...
gn = net_generic(dev_net(dev), gtp_net_id);
list_add_rcu(>p->list, &gn->gtp_dev_list); Uses src_net, but is linked to list in dev_net.
- In pfcp_newlink() in drivers/net/pfcp.c:
pfcp->net = net;
...
pn = net_generic(dev_net(dev), pfcp_net_id);
list_add_rcu(&pfcp->list, &pn->pfcp_dev_list);Same.
- In lowpan_newlink() in net/ieee802154/6lowpan/core.c:
wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
Looks for IFLA_LINK in dev_net, but in theory the ifindex is defined in link netns.
---
drivers/infiniband/ulp/ipoib/ipoib_netlink.c | 6 +++-- drivers/net/amt.c | 6 ++--- drivers/net/bareudp.c | 4 ++-- drivers/net/bonding/bond_netlink.c | 3 ++- drivers/net/can/dev/netlink.c | 2 +- drivers/net/can/vxcan.c | 4 ++-- .../ethernet/qualcomm/rmnet/rmnet_config.c | 5 +++-- drivers/net/geneve.c | 4 ++-- drivers/net/gtp.c | 4 ++-- drivers/net/ipvlan/ipvlan.h | 2 +- drivers/net/ipvlan/ipvlan_main.c | 5 +++-- drivers/net/ipvlan/ipvtap.c | 4 ++-- drivers/net/macsec.c | 5 +++-- drivers/net/macvlan.c | 5 +++-- drivers/net/macvtap.c | 5 +++-- drivers/net/netkit.c | 4 ++-- drivers/net/pfcp.c | 4 ++-- drivers/net/ppp/ppp_generic.c | 4 ++-- drivers/net/team/team_core.c | 2 +- drivers/net/veth.c | 4 ++-- drivers/net/vrf.c | 2 +- drivers/net/vxlan/vxlan_core.c | 4 ++-- drivers/net/wireguard/device.c | 4 ++-- drivers/net/wireless/virtual/virt_wifi.c | 5 +++-- drivers/net/wwan/wwan_core.c | 6 +++-- include/net/ip_tunnels.h | 5 +++-- include/net/rtnetlink.h | 22 ++++++++++++++++++- net/8021q/vlan_netlink.c | 5 +++-- net/batman-adv/soft-interface.c | 5 +++-- net/bridge/br_netlink.c | 2 +- net/caif/chnl_net.c | 2 +- net/core/rtnetlink.c | 15 ++++++------- net/hsr/hsr_netlink.c | 8 +++---- net/ieee802154/6lowpan/core.c | 5 +++-- net/ipv4/ip_gre.c | 13 ++++++----- net/ipv4/ip_tunnel.c | 10 +++++---- net/ipv4/ip_vti.c | 5 +++-- net/ipv4/ipip.c | 5 +++-- net/ipv6/ip6_gre.c | 17 +++++++------- net/ipv6/ip6_tunnel.c | 11 +++++----- net/ipv6/ip6_vti.c | 11 +++++----- net/ipv6/sit.c | 11 +++++----- net/xfrm/xfrm_interface_core.c | 13 +++++------ 43 files changed, 153 insertions(+), 115 deletions(-)
drivers/infiniband/ulp/ipoib/ipoib_netlink.c +4 -2
@@ -97,7 +97,8 @@ static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[],return ret; } -static int ipoib_new_child_link (struct net *src_net,struct net_device *dev, +static int ipoib_new_child_link (struct rtnl_link_nets *nets, +struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -109,7 +110,8 @@ static int ipoib_new_child_link(struct net *src_net, struct net_device *dev,if (!tb[IFLA_LINK ])return -EINVAL ; - pdev =__dev_get_by_index (src_net,nla_get_u32 (tb[IFLA_LINK ])); + pdev =__dev_get_by_index (rtnl_link_netns (nets), +nla_get_u32 (tb[IFLA_LINK ]));if (!pdev || pdev->type !=ARPHRD_INFINIBAND )return -ENODEV ;
drivers/net/amt.c +3 -3
@@ -3161,14 +3161,14 @@ static int amt_validate(struct nlattr *tb[], struct nlattr *data[],return 0 ; } -static int amt_newlink (struct net *net,struct net_device *dev, +static int amt_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {struct amt_dev *amt =netdev_priv (dev);int err = -EINVAL ; - amt->net = net; + amt->net =rtnl_link_netns (nets); amt->mode =nla_get_u32 (data[IFLA_AMT_MODE ]); if (data[IFLA_AMT_MAX_TUNNELS] &&
@@ -3183,7 +3183,7 @@ static int amt_newlink(struct net *net, struct net_device *dev, amt->hash_buckets =AMT_HSIZE ; amt->nr_tunnels =0 ;get_random_bytes (&amt->hash_seed,sizeof (amt->hash_seed)); - amt->stream_dev =dev_get_by_index (net, + amt->stream_dev =dev_get_by_index (rtnl_link_netns (nets),nla_get_u32 (data[IFLA_AMT_LINK ]));if (!amt->stream_dev) { NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
drivers/net/bareudp.c +2 -2
@@ -698,7 +698,7 @@ static void bareudp_dellink(struct net_device *dev, struct list_head *head)unregister_netdevice_queue (dev, head); } -static int bareudp_newlink (struct net *net,struct net_device *dev, +static int bareudp_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -709,7 +709,7 @@ static int bareudp_newlink(struct net *net, struct net_device *dev,if (err)return err; - err =bareudp_configure (net, dev, &conf, extack); + err =bareudp_configure (rtnl_link_netns (nets), dev, &conf, extack);if (err)return err;
drivers/net/bonding/bond_netlink.c +2 -1
@@ -564,7 +564,8 @@ static int bond_changelink(struct net_device *bond_dev, struct nlattr *tb[],return 0 ; } -static int bond_newlink (struct net *src_net,struct net_device *bond_dev, +static int bond_newlink (struct rtnl_link_nets *nets, +struct net_device *bond_dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
drivers/net/can/dev/netlink.c +1 -1
@@ -624,7 +624,7 @@ static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)return -EMSGSIZE ; } -static int can_newlink (struct net *src_net,struct net_device *dev, +static int can_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
drivers/net/can/vxcan.c +2 -2
@@ -172,7 +172,7 @@ static void vxcan_setup(struct net_device *dev)/* forward declaration for rtnl_create_link() */ static struct rtnl_link_ops vxcan_link_ops; -static int vxcan_newlink (struct net *net,struct net_device *dev, +static int vxcan_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -203,7 +203,7 @@ static int vxcan_newlink(struct net *net, struct net_device *dev, name_assign_type =NET_NAME_ENUM ; } - peer_net =rtnl_link_get_net (net, tbp); + peer_net =rtnl_link_get_net (rtnl_link_netns (nets), tbp); peer =rtnl_create_link (peer_net, ifname, name_assign_type, &vxcan_link_ops, tbp, extack);if (IS_ERR (peer)) {
drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c +3 -2
@@ -117,7 +117,7 @@ static void rmnet_unregister_bridge(struct rmnet_port *port)rmnet_unregister_real_device (bridge_dev); } -static int rmnet_newlink (struct net *src_net,struct net_device *dev, +static int rmnet_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -134,7 +134,8 @@ static int rmnet_newlink(struct net *src_net, struct net_device *dev,return -EINVAL ; } - real_dev =__dev_get_by_index (src_net,nla_get_u32 (tb[IFLA_LINK ])); + real_dev =__dev_get_by_index (rtnl_link_netns (nets), +nla_get_u32 (tb[IFLA_LINK ]));if (!real_dev) {NL_SET_ERR_MSG_MOD (extack,"link does not exist" );return -ENODEV ;
drivers/net/geneve.c +2 -2
@@ -1614,7 +1614,7 @@ static void geneve_link_config(struct net_device *dev,geneve_change_mtu (dev, ldev_mtu - info->options_len); } -static int geneve_newlink (struct net *net,struct net_device *dev, +static int geneve_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1631,7 +1631,7 @@ static int geneve_newlink(struct net *net, struct net_device *dev,if (err)return err; - err =geneve_configure (net, dev, extack, &cfg); + err =geneve_configure (rtnl_link_netns (nets), dev, extack, &cfg);if (err)return err;
drivers/net/gtp.c +2 -2
@@ -1460,7 +1460,7 @@ static int gtp_create_sockets(struct gtp_dev *gtp, const struct nlattr *nla,#define GTP_TH_MAXLEN (sizeof(struct udphdr) + sizeof(struct gtp0_header))#define GTP_IPV6_MAXLEN (sizeof(struct ipv6hdr) + GTP_TH_MAXLEN) -static int gtp_newlink (struct net *src_net,struct net_device *dev, +static int gtp_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1494,7 +1494,7 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev, gtp->restart_count =nla_get_u8_default (data[IFLA_GTP_RESTART_COUNT ],0 ); - gtp->net = src_net; + gtp->net =rtnl_link_netns (nets); err =gtp_hashtable_new (gtp, hashsize);if (err <0 )
drivers/net/ipvlan/ipvlan.h +1 -1
@@ -166,7 +166,7 @@ struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,void *ipvlan_get_L3_hdr (struct ipvl_port *port,struct sk_buff *skb,int *type);void ipvlan_count_rx (const struct ipvl_dev *ipvlan,unsigned int len,bool success,bool mcast); -int ipvlan_link_new (struct net *src_net,struct net_device *dev, +int ipvlan_link_new (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack);void ipvlan_link_delete (struct net_device *dev,struct list_head *head);
drivers/net/ipvlan/ipvlan_main.c +3 -2
@@ -532,7 +532,7 @@ static int ipvlan_nl_fillinfo(struct sk_buff *skb,return ret; } -int ipvlan_link_new (struct net *src_net,struct net_device *dev, +int ipvlan_link_new (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -545,7 +545,8 @@ int ipvlan_link_new(struct net *src_net, struct net_device *dev,if (!tb[IFLA_LINK ])return -EINVAL ; - phy_dev =__dev_get_by_index (src_net,nla_get_u32 (tb[IFLA_LINK ])); + phy_dev =__dev_get_by_index (rtnl_link_netns (nets), +nla_get_u32 (tb[IFLA_LINK ]));if (!phy_dev)return -ENODEV ;
drivers/net/ipvlan/ipvtap.c +2 -2
@@ -73,7 +73,7 @@ static void ipvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } -static int ipvtap_newlink (struct net *src_net,struct net_device *dev, +static int ipvtap_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -97,7 +97,7 @@ static int ipvtap_newlink(struct net *src_net, struct net_device *dev,/* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err =ipvlan_link_new (src_net, dev, tb, data, extack); + err =ipvlan_link_new (nets, dev, tb, data, extack);if (err) {netdev_rx_handler_unregister (dev);return err;
drivers/net/macsec.c +3 -2
@@ -4141,7 +4141,7 @@ static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)static struct lock_class_key macsec_netdev_addr_lock_key; -static int macsec_newlink (struct net *net,struct net_device *dev, +static int macsec_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -4154,7 +4154,8 @@ static int macsec_newlink(struct net *net, struct net_device *dev,if (!tb[IFLA_LINK ])return -EINVAL ; - real_dev =__dev_get_by_index (net,nla_get_u32 (tb[IFLA_LINK ])); + real_dev =__dev_get_by_index (rtnl_link_netns (nets), +nla_get_u32 (tb[IFLA_LINK ]));if (!real_dev)return -ENODEV ;if (real_dev->type !=ARPHRD_ETHER )
drivers/net/macvlan.c +3 -2
@@ -1565,11 +1565,12 @@ int macvlan_common_newlink(struct net *src_net, struct net_device *dev, }EXPORT_SYMBOL_GPL (macvlan_common_newlink); -static int macvlan_newlink (struct net *src_net,struct net_device *dev, +static int macvlan_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { -return macvlan_common_newlink (src_net, dev, tb, data, extack); +return macvlan_common_newlink (rtnl_link_netns (nets), dev, tb, data, + extack); }void macvlan_dellink (struct net_device *dev,struct list_head *head)
drivers/net/macvtap.c +3 -2
@@ -77,7 +77,7 @@ static void macvtap_update_features(struct tap_dev *tap,netdev_update_features (vlan->dev); } -static int macvtap_newlink (struct net *src_net,struct net_device *dev, +static int macvtap_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -105,7 +105,8 @@ static int macvtap_newlink(struct net *src_net, struct net_device *dev,/* Don't put anything that may fail after macvlan_common_newlink * because we can't undo what it does. */ - err =macvlan_common_newlink (src_net, dev, tb, data, extack); + err =macvlan_common_newlink (rtnl_link_netns (nets), dev, tb, data, + extack);if (err) {netdev_rx_handler_unregister (dev);return err;
drivers/net/netkit.c +2 -2
@@ -327,7 +327,7 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[],static struct rtnl_link_ops netkit_link_ops; -static int netkit_new_link (struct net *src_net,struct net_device *dev, +static int netkit_new_link (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -385,7 +385,7 @@ static int netkit_new_link(struct net *src_net, struct net_device *dev, (tb[IFLA_ADDRESS] || tbp[IFLA_ADDRESS]))return -EOPNOTSUPP ; - net =rtnl_link_get_net (src_net, tbp); + net =rtnl_link_get_net (rtnl_link_netns (nets), tbp); peer =rtnl_create_link (net, ifname, ifname_assign_type, &netkit_link_ops, tbp, extack);if (IS_ERR (peer)) {
drivers/net/pfcp.c +2 -2
@@ -184,7 +184,7 @@ static int pfcp_add_sock(struct pfcp_dev *pfcp)return PTR_ERR_OR_ZERO (pfcp->sock); } -static int pfcp_newlink (struct net *net,struct net_device *dev, +static int pfcp_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -192,7 +192,7 @@ static int pfcp_newlink(struct net *net, struct net_device *dev,struct pfcp_net *pn;int err; - pfcp->net = net; + pfcp->net =rtnl_link_netns (nets); err =pfcp_add_sock (pfcp);if (err) {
drivers/net/ppp/ppp_generic.c +2 -2
@@ -1303,7 +1303,7 @@ static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],return 0 ; } -static int ppp_nl_newlink (struct net *src_net,struct net_device *dev, +static int ppp_nl_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1343,7 +1343,7 @@ static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,if (!tb[IFLA_IFNAME ] || !nla_len (tb[IFLA_IFNAME ]) || !*(char *)nla_data (tb[IFLA_IFNAME ])) conf.ifname_is_set = false; - err =ppp_dev_configure (src_net, dev, &conf); + err =ppp_dev_configure (rtnl_link_netns (nets), dev, &conf); out_unlock:mutex_unlock (&ppp_mutex);
drivers/net/team/team_core.c +1 -1
@@ -2206,7 +2206,7 @@ static void team_setup(struct net_device *dev) dev->features |=NETIF_F_HW_VLAN_CTAG_TX |NETIF_F_HW_VLAN_STAG_TX ; } -static int team_newlink (struct net *src_net,struct net_device *dev, +static int team_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
drivers/net/veth.c +2 -2
@@ -1765,7 +1765,7 @@ static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])return 0 ; } -static int veth_newlink (struct net *src_net,struct net_device *dev, +static int veth_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1800,7 +1800,7 @@ static int veth_newlink(struct net *src_net, struct net_device *dev, name_assign_type =NET_NAME_ENUM ; } - net =rtnl_link_get_net (src_net, tbp); + net =rtnl_link_get_net (rtnl_link_netns (nets), tbp); peer =rtnl_create_link (net, ifname, name_assign_type, &veth_link_ops, tbp, extack);if (IS_ERR (peer)) {
drivers/net/vrf.c +1 -1
@@ -1698,7 +1698,7 @@ static void vrf_dellink(struct net_device *dev, struct list_head *head)unregister_netdevice_queue (dev, head); } -static int vrf_newlink (struct net *src_net,struct net_device *dev, +static int vrf_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
drivers/net/vxlan/vxlan_core.c +2 -2
@@ -4345,7 +4345,7 @@ static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],return 0 ; } -static int vxlan_newlink (struct net *src_net,struct net_device *dev, +static int vxlan_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -4356,7 +4356,7 @@ static int vxlan_newlink(struct net *src_net, struct net_device *dev,if (err)return err; -return __vxlan_dev_create (src_net, dev, &conf, extack); +return __vxlan_dev_create (rtnl_link_netns (nets), dev, &conf, extack); } static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
drivers/net/wireguard/device.c +2 -2
@@ -306,14 +306,14 @@ static void wg_setup(struct net_device *dev) wg->dev = dev; } -static int wg_newlink (struct net *src_net,struct net_device *dev, +static int wg_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {struct wg_device *wg =netdev_priv (dev);int ret = -ENOMEM ; -rcu_assign_pointer (wg->creating_net, src_net); +rcu_assign_pointer (wg->creating_net,rtnl_link_netns (nets));init_rwsem (&wg->static_identity.lock);mutex_init (&wg->socket_update_lock);mutex_init (&wg->device_update_lock);
drivers/net/wireless/virtual/virt_wifi.c +3 -2
@@ -519,7 +519,8 @@ static rx_handler_result_t virt_wifi_rx_handler(struct sk_buff **pskb) }/* Called with rtnl lock held. */ -static int virt_wifi_newlink (struct net *src_net,struct net_device *dev, +static int virt_wifi_newlink (struct rtnl_link_nets *nets, +struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -532,7 +533,7 @@ static int virt_wifi_newlink(struct net *src_net, struct net_device *dev,netif_carrier_off (dev); priv->upperdev = dev; - priv->lowerdev =__dev_get_by_index (src_net, + priv->lowerdev =__dev_get_by_index (rtnl_link_netns (nets),nla_get_u32 (tb[IFLA_LINK ]));if (!priv->lowerdev)
drivers/net/wwan/wwan_core.c +4 -2
@@ -967,7 +967,8 @@ static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],return dev; } -static int wwan_rtnl_newlink (struct net *src_net,struct net_device *dev, +static int wwan_rtnl_newlink (struct rtnl_link_nets *nets, +struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1064,6 +1065,7 @@ static void wwan_create_default_link(struct wwan_device *wwandev,struct net_device *dev;struct nlmsghdr *nlh;struct sk_buff *msg; +struct rtnl_link_nets nets = { .src_net = &init_net };/* Forge attributes required to create a WWAN netdev. We first * build a netlink message and then parse it. This looks
@@ -1105,7 +1107,7 @@ static void wwan_create_default_link(struct wwan_device *wwandev,if (WARN_ON (IS_ERR (dev))) goto unlock; -if (WARN_ON (wwan_rtnl_newlink (&init_net, dev, tb, data,NULL ))) { +if (WARN_ON (wwan_rtnl_newlink (&nets, dev, tb, data,NULL ))) {free_netdev (dev); goto unlock; }
include/net/ip_tunnels.h +3 -2
@@ -406,8 +406,9 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, bool log_ecn_error);int ip_tunnel_changelink (struct net_device *dev,struct nlattr *tb[],struct ip_tunnel_parm_kern *p,__u32 fwmark); -int ip_tunnel_newlink (struct net_device *dev,struct nlattr *tb[], -struct ip_tunnel_parm_kern *p,__u32 fwmark); +int ip_tunnel_newlink (struct net *net,struct net_device *dev, +struct nlattr *tb[],struct ip_tunnel_parm_kern *p, +__u32 fwmark);void ip_tunnel_setup (struct net_device *dev,unsigned int net_id); bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
include/net/rtnetlink.h +21 -1
@@ -69,6 +69,26 @@ static inline int rtnl_msg_family(const struct nlmsghdr *nlh)return AF_UNSPEC ; } +/** +* struct rtnl_link_nets - net namespace context of newlink. +* +* @src_net: Source netns of rtnetlink socket +* @link_net: Link netns by IFLA_LINK_NETNSID, NULL if not specified. +*/ +struct rtnl_link_nets { +struct net *src_net; +struct net *link_net; +}; + +/* Get effective link netns from struct rtnl_link_nets. Generally, this is +* link_net and falls back to src_net. But for compatibility, a driver may +* choose to use dev_net(dev) instead. +*/ +static inline struct net *rtnl_link_netns (struct rtnl_link_nets *nets) +{ +return nets->link_net ? : nets->src_net; +} +/** * struct rtnl_link_ops - rtnetlink link operations *
@@ -125,7 +145,7 @@ struct rtnl_link_ops {struct nlattr *data[], struct netlink_ext_ack *extack); - int (*newlink)(struct net *src_net, + int (*newlink)(struct rtnl_link_nets *nets, struct net_device *dev, struct nlattr *tb[], struct nlattr *data[],
net/8021q/vlan_netlink.c +3 -2
@@ -135,7 +135,7 @@ static int vlan_changelink(struct net_device *dev, struct nlattr *tb[],return 0 ; } -static int vlan_newlink (struct net *src_net,struct net_device *dev, +static int vlan_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -155,7 +155,8 @@ static int vlan_newlink(struct net *src_net, struct net_device *dev,return -EINVAL ; } - real_dev =__dev_get_by_index (src_net,nla_get_u32 (tb[IFLA_LINK ])); + real_dev =__dev_get_by_index (rtnl_link_netns (nets), +nla_get_u32 (tb[IFLA_LINK ]));if (!real_dev) {NL_SET_ERR_MSG_MOD (extack,"link does not exist" );return -ENODEV ;
net/batman-adv/soft-interface.c +3 -2
@@ -1063,7 +1063,7 @@ static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[],/** * batadv_softif_newlink() - pre-initialize and register new batadv link -* @src_net: the applicable net namespace +* @nets: the applicable net namespaces * @dev: network device to register * @tb: IFLA_INFO_DATA netlink attributes * @data: enum batadv_ifla_attrs attributes
@@ -1071,7 +1071,8 @@ static int batadv_softif_validate(struct nlattr *tb[], struct nlattr *data[],* * Return: 0 if successful or error otherwise. */ -static int batadv_softif_newlink (struct net *src_net,struct net_device *dev, +static int batadv_softif_newlink (struct rtnl_link_nets *nets, +struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
net/bridge/br_netlink.c +1 -1
@@ -1553,7 +1553,7 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[],return 0 ; } -static int br_dev_newlink (struct net *src_net,struct net_device *dev, +static int br_dev_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
net/caif/chnl_net.c +1 -1
@@ -438,7 +438,7 @@ static void caif_netlink_parms(struct nlattr *data[], } } -static int ipcaif_newlink (struct net *src_net,struct net_device *dev, +static int ipcaif_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
net/core/rtnetlink.c +7 -8
@@ -3750,6 +3750,10 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,struct net_device *dev;char ifname[IFNAMSIZ ];int err; +struct rtnl_link_nets nets = { + .src_net = net, + .link_net = link_net, + };if (!ops->alloc && !ops->setup)return -EOPNOTSUPP ;
@@ -3761,8 +3765,8 @@ 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 (tgt_net, ifname, name_assign_type, ops, tb, + extack);if (IS_ERR (dev)) { err =PTR_ERR (dev); goto out;
@@ -3771,7 +3775,7 @@ static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, dev->ifindex = ifm->ifi_index;if (ops->newlink) - err = ops->newlink (link_net ? : net, dev, tb, data, extack); + err = ops->newlink (&nets, dev, tb, data, extack);else err =register_netdevice (dev);if (err <0 ) {
@@ -3782,11 +3786,6 @@ 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) { - err =dev_change_net_namespace (dev, tgt_net, ifname); -if (err <0 ) - goto out_unregister; - }if (tb[IFLA_MASTER ]) { err =do_set_master (dev,nla_get_u32 (tb[IFLA_MASTER ]), extack);if (err)
net/hsr/hsr_netlink.c +4 -4
@@ -29,7 +29,7 @@ static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {/* Here, it seems a netdevice has already been allocated for us, and the * hsr_dev_setup routine has been executed. Nice! */ -static int hsr_newlink (struct net *src_net,struct net_device *dev, +static int hsr_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -46,7 +46,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev,NL_SET_ERR_MSG_MOD (extack,"Slave1 device not specified" );return -EINVAL ; } - link[0 ] =__dev_get_by_index (src_net, + link[0 ] =__dev_get_by_index (rtnl_link_netns (nets),nla_get_u32 (data[IFLA_HSR_SLAVE1 ]));if (!link[0 ]) {NL_SET_ERR_MSG_MOD (extack,"Slave1 does not exist" );
@@ -56,7 +56,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev,NL_SET_ERR_MSG_MOD (extack,"Slave2 device not specified" );return -EINVAL ; } - link[1 ] =__dev_get_by_index (src_net, + link[1 ] =__dev_get_by_index (rtnl_link_netns (nets),nla_get_u32 (data[IFLA_HSR_SLAVE2 ]));if (!link[1 ]) {NL_SET_ERR_MSG_MOD (extack,"Slave2 does not exist" );
@@ -69,7 +69,7 @@ static int hsr_newlink(struct net *src_net, struct net_device *dev, }if (data[IFLA_HSR_INTERLINK ]) - interlink =__dev_get_by_index (src_net, + interlink =__dev_get_by_index (rtnl_link_netns (nets),nla_get_u32 (data[IFLA_HSR_INTERLINK ]));if (interlink && interlink == link[0 ]) {
net/ieee802154/6lowpan/core.c +3 -2
@@ -129,7 +129,7 @@ static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[],return 0 ; } -static int lowpan_newlink (struct net *src_net,struct net_device *ldev, +static int lowpan_newlink (struct rtnl_link_nets *nets,struct net_device *ldev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -143,7 +143,8 @@ static int lowpan_newlink(struct net *src_net, struct net_device *ldev,if (!tb[IFLA_LINK ])return -EINVAL ;/* find and hold wpan device */ - wdev =dev_get_by_index (dev_net (ldev),nla_get_u32 (tb[IFLA_LINK ])); + wdev =dev_get_by_index (nets->link_net ? :dev_net (ldev), +nla_get_u32 (tb[IFLA_LINK ]));if (!wdev)return -ENODEV ;if (wdev->type !=ARPHRD_IEEE802154 ) {
net/ipv4/ip_gre.c +8 -5
@@ -1389,10 +1389,11 @@ ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])return 0 ; } -static int ipgre_newlink (struct net *src_net,struct net_device *dev, +static int ipgre_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { +struct net *net = nets->link_net ? :dev_net (dev);struct ip_tunnel_parm_kern p;__u32 fwmark =0 ;int err;
@@ -1404,13 +1405,14 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev, err =ipgre_netlink_parms (dev, data, tb, &p, &fwmark);if (err <0 )return err; -return ip_tunnel_newlink (dev, tb, &p, fwmark); +return ip_tunnel_newlink (net, dev, tb, &p, fwmark); } -static int erspan_newlink (struct net *src_net,struct net_device *dev, +static int erspan_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { +struct net *net = nets->link_net ? :dev_net (dev);struct ip_tunnel_parm_kern p;__u32 fwmark =0 ;int err;
@@ -1422,7 +1424,7 @@ static int erspan_newlink(struct net *src_net, struct net_device *dev, err =erspan_netlink_parms (dev, data, tb, &p, &fwmark);if (err)return err; -return ip_tunnel_newlink (dev, tb, &p, fwmark); +return ip_tunnel_newlink (net, dev, tb, &p, fwmark); } static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
@@ -1695,6 +1697,7 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name,LIST_HEAD (list_kill);struct ip_tunnel *t;int err; +struct rtnl_link_nets nets = { .src_net = net };memset (&tb,0 ,sizeof (tb));
@@ -1707,7 +1710,7 @@ struct net_device *gretap_fb_dev_create(struct net *net, const char *name, t =netdev_priv (dev); t->collect_md = true; - err =ipgre_newlink (net, dev, tb,NULL ,NULL ); + err =ipgre_newlink (&nets, dev, tb,NULL ,NULL );if (err <0 ) {free_netdev (dev);return ERR_PTR (err);
net/ipv4/ip_tunnel.c +6 -4
@@ -1213,11 +1213,11 @@ void ip_tunnel_delete_nets(struct list_head *net_list, unsigned int id, }EXPORT_SYMBOL_GPL (ip_tunnel_delete_nets); -int ip_tunnel_newlink (struct net_device *dev,struct nlattr *tb[], -struct ip_tunnel_parm_kern *p,__u32 fwmark) +int ip_tunnel_newlink (struct net *net,struct net_device *dev, +struct nlattr *tb[],struct ip_tunnel_parm_kern *p, +__u32 fwmark) {struct ip_tunnel *nt; -struct net *net =dev_net (dev);struct ip_tunnel_net *itn;int mtu;int err;
@@ -1326,7 +1326,9 @@ int ip_tunnel_init(struct net_device *dev) } tunnel->dev = dev; - tunnel->net =dev_net (dev); +if (!tunnel->net) + tunnel->net =dev_net (dev); +strscpy (tunnel->parms.name, dev->name); iph->version =4 ; iph->ihl =5 ;
net/ipv4/ip_vti.c +3 -2
@@ -575,7 +575,7 @@ static void vti_netlink_parms(struct nlattr *data[], *fwmark =nla_get_u32 (data[IFLA_VTI_FWMARK ]); } -static int vti_newlink (struct net *src_net,struct net_device *dev, +static int vti_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -583,7 +583,8 @@ static int vti_newlink(struct net *src_net, struct net_device *dev,__u32 fwmark =0 ;vti_netlink_parms (data, &parms, &fwmark); -return ip_tunnel_newlink (dev, tb, &parms, fwmark); +return ip_tunnel_newlink (nets->link_net ? :dev_net (dev), dev, tb, + &parms, fwmark); } static int vti_changelink(struct net_device *dev, struct nlattr *tb[],
net/ipv4/ipip.c +3 -2
@@ -436,7 +436,7 @@ static void ipip_netlink_parms(struct nlattr *data[], *fwmark =nla_get_u32 (data[IFLA_IPTUN_FWMARK ]); } -static int ipip_newlink (struct net *src_net,struct net_device *dev, +static int ipip_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -453,7 +453,8 @@ static int ipip_newlink(struct net *src_net, struct net_device *dev, }ipip_netlink_parms (data, &p, &t->collect_md, &fwmark); -return ip_tunnel_newlink (dev, tb, &p, fwmark); +return ip_tunnel_newlink (nets->link_net ? :dev_net (dev), dev, tb, &p, + fwmark); } static int ipip_changelink(struct net_device *dev, struct nlattr *tb[],
net/ipv6/ip6_gre.c +9 -8
@@ -1971,7 +1971,7 @@ static bool ip6gre_netlink_encap_parms(struct nlattr *data[],return ret; } -static int ip6gre_newlink_common (struct net *src_net,struct net_device *dev, +static int ip6gre_newlink_common (struct net *link_net,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {
@@ -1992,7 +1992,7 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,eth_hw_addr_random (dev); nt->dev = dev; - nt->net =dev_net (dev); + nt->net = link_net; err =register_netdevice (dev);if (err)
@@ -2005,12 +2005,12 @@ static int ip6gre_newlink_common(struct net *src_net, struct net_device *dev,return err; } -static int ip6gre_newlink (struct net *src_net,struct net_device *dev, +static int ip6gre_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {struct ip6_tnl *nt =netdev_priv (dev); -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct ip6gre_net *ign;int err;
@@ -2025,7 +2025,7 @@ static int ip6gre_newlink(struct net *src_net, struct net_device *dev,return -EEXIST ; } - err =ip6gre_newlink_common (src_net, dev, tb, data, extack); + err =ip6gre_newlink_common (net, dev, tb, data, extack);if (!err) {ip6gre_tnl_link_config (nt, !tb[IFLA_MTU ]);ip6gre_tunnel_link_md (ign, nt);
@@ -2241,12 +2241,13 @@ static void ip6erspan_tap_setup(struct net_device *dev)netif_keep_dst (dev); } -static int ip6erspan_newlink (struct net *src_net,struct net_device *dev, +static int ip6erspan_newlink (struct rtnl_link_nets *nets, +struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) {struct ip6_tnl *nt =netdev_priv (dev); -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct ip6gre_net *ign;int err;
@@ -2262,7 +2263,7 @@ static int ip6erspan_newlink(struct net *src_net, struct net_device *dev,return -EEXIST ; } - err =ip6gre_newlink_common (src_net, dev, tb, data, extack); + err =ip6gre_newlink_common (net, dev, tb, data, extack);if (!err) {ip6erspan_tnl_link_config (nt, !tb[IFLA_MTU ]);ip6erspan_tunnel_link_md (ign, nt);
net/ipv6/ip6_tunnel.c +5 -6
@@ -250,10 +250,9 @@ static void ip6_dev_free(struct net_device *dev)dst_cache_destroy (&t->dst_cache); } -static int ip6_tnl_create2 (struct net_device *dev) +static int ip6_tnl_create2 (struct net *net,struct net_device *dev) {struct ip6_tnl *t =netdev_priv (dev); -struct net *net =dev_net (dev);struct ip6_tnl_net *ip6n =net_generic (net, ip6_tnl_net_id);int err;
@@ -308,7 +307,7 @@ static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) t =netdev_priv (dev); t->parms = *p; t->net =dev_net (dev); - err =ip6_tnl_create2 (dev); + err =ip6_tnl_create2 (net, dev);if (err <0 ) goto failed_free;
@@ -2002,11 +2001,11 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[], parms->fwmark =nla_get_u32 (data[IFLA_IPTUN_FWMARK ]); } -static int ip6_tnl_newlink (struct net *src_net,struct net_device *dev, +static int ip6_tnl_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct ip6_tnl_net *ip6n =net_generic (net, ip6_tnl_net_id);struct ip_tunnel_encap ipencap;struct ip6_tnl *nt, *t;
@@ -2031,7 +2030,7 @@ static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,return -EEXIST ; } - err =ip6_tnl_create2 (dev); + err =ip6_tnl_create2 (net, dev);if (!err && tb[IFLA_MTU ])ip6_tnl_change_mtu (dev,nla_get_u32 (tb[IFLA_MTU ]));
net/ipv6/ip6_vti.c +5 -6
@@ -174,10 +174,9 @@ vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t) } } -static int vti6_tnl_create2 (struct net_device *dev) +static int vti6_tnl_create2 (struct net *net,struct net_device *dev) {struct ip6_tnl *t =netdev_priv (dev); -struct net *net =dev_net (dev);struct vti6_net *ip6n =net_generic (net, vti6_net_id);int err;
@@ -221,7 +220,7 @@ static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p t->parms = *p; t->net =dev_net (dev); - err =vti6_tnl_create2 (dev); + err =vti6_tnl_create2 (net, dev);if (err <0 ) goto failed_free;
@@ -997,11 +996,11 @@ static void vti6_netlink_parms(struct nlattr *data[], parms->fwmark =nla_get_u32 (data[IFLA_VTI_FWMARK ]); } -static int vti6_newlink (struct net *src_net,struct net_device *dev, +static int vti6_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct ip6_tnl *nt; nt =netdev_priv (dev);
@@ -1012,7 +1011,7 @@ static int vti6_newlink(struct net *src_net, struct net_device *dev,if (vti6_locate (net, &nt->parms,0 ))return -EEXIST ; -return vti6_tnl_create2 (dev); +return vti6_tnl_create2 (net, dev); }static void vti6_dellink (struct net_device *dev,struct list_head *head)
net/ipv6/sit.c +5 -6
@@ -198,10 +198,9 @@ static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)#endif } -static int ipip6_tunnel_create (struct net_device *dev) +static int ipip6_tunnel_create (struct net *net,struct net_device *dev) {struct ip_tunnel *t =netdev_priv (dev); -struct net *net =dev_net (dev);struct sit_net *sitn =net_generic (net, sit_net_id);int err;
@@ -270,7 +269,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net, nt =netdev_priv (dev); nt->parms = *parms; -if (ipip6_tunnel_create (dev) <0 ) +if (ipip6_tunnel_create (net, dev) <0 ) goto failed_free;if (!parms->name[0 ])
@@ -1550,11 +1549,11 @@ static bool ipip6_netlink_6rd_parms(struct nlattr *data[], }#endif -static int ipip6_newlink (struct net *src_net,struct net_device *dev, +static int ipip6_newlink (struct rtnl_link_nets *nets,struct net_device *dev,struct nlattr *tb[],struct nlattr *data[],struct netlink_ext_ack *extack) { -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct ip_tunnel *nt;struct ip_tunnel_encap ipencap;#ifdef CONFIG_IPV6_SIT_6RD
@@ -1575,7 +1574,7 @@ static int ipip6_newlink(struct net *src_net, struct net_device *dev,if (ipip6_tunnel_locate (net, &nt->parms,0 ))return -EEXIST ; - err =ipip6_tunnel_create (dev); + err =ipip6_tunnel_create (net, dev);if (err <0 )return err;
net/xfrm/xfrm_interface_core.c +6 -7
@@ -242,10 +242,9 @@ static void xfrmi_dev_free(struct net_device *dev)gro_cells_destroy (&xi->gro_cells); } -static int xfrmi_create (struct net_device *dev) +static int xfrmi_create (struct net *net,struct net_device *dev) {struct xfrm_if *xi =netdev_priv (dev); -struct net *net =dev_net (dev);struct xfrmi_net *xfrmn =net_generic (net, xfrmi_net_id);int err;
@@ -814,11 +813,11 @@ static void xfrmi_netlink_parms(struct nlattr *data[], parms->collect_md = true; } -static int xfrmi_newlink (struct net *src_net,struct net_device *dev, -struct nlattr *tb[],struct nlattr *data[], -struct netlink_ext_ack *extack) +static int xfrmi_newlink (struct rtnl_link_nets *nets,struct net_device *dev, +struct nlattr *tb[],struct nlattr *data[], +struct netlink_ext_ack *extack) { -struct net *net =dev_net (dev); +struct net *net = nets->link_net ? :dev_net (dev);struct xfrm_if_parms p = {};struct xfrm_if *xi;int err;
@@ -851,7 +850,7 @@ static int xfrmi_newlink(struct net *src_net, struct net_device *dev, xi->net = net; xi->dev = dev; - err =xfrmi_create (dev); + err =xfrmi_create (net, dev);return err; }
Signature · 1 line
2.47.0
Jakub Kicinski
maintainerRe: [PATCH net-next v3 4/6] rtnetlink: Decouple net namespaces in rtnl_newlink_create()
include/net/rtnetlink.h static inline int rtnl_msg_family(const struct nlmsghdr *nlh) · line 81
Quoted 10 lines · On Wed, 13 Nov 2024 20:57:13 +0800 Xiao Liang wrote:
include/net/rtnetlink.h +10
-0+/** +* struct rtnl_link_nets - net namespace context of newlink. +* +* @src_net: Source netns of rtnetlink socket +* @link_net: Link netns by IFLA_LINK_NETNSID, NULL if not specified. +*/ +struct rtnl_link_nets { +struct net *src_net; +struct net *link_net; +};
Let's not limit ourselves to passing just netns via this struct. Let's call it rtnl_newlink_args or params.
The first patch of the series got merged independently so you'll need to respin.
Signature · 1 line
pw-bot: cr
PATCH v3 5/6 selftests: net: Add python context manager for netns entering
In reply to Xiao LiangChange netns of current thread and switch back on context exit. For example:
with NetNSEnter("ns1"):
ip("link add dummy0 type dummy")
The command be executed in netns "ns1".
--- tools/testing/selftests/net/lib/py/__init__.py | 2 +- tools/testing/selftests/net/lib/py/netns.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
tools/testing/selftests/net/lib/py/__init__.py +1 -1
@@ -2,7 +2,7 @@ from .consts import KSRC from .ksft import * -from .netns import NetNS +from .netns import NetNS, NetNSEnter from .nsim import * from .utils import * from .ynl import NlError, YnlFamily, EthtoolFamily, NetdevFamily, RtnlFamily
tools/testing/selftests/net/lib/py/netns.py +18 -0
@@ -1,9 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 from .utils import ip +import ctypes import random import string +libc = ctypes.cdll.LoadLibrary('libc.so.6') + class NetNS: def __init__(self, name=None):
@@ -29,3 +32,18 @@ class NetNS: def __repr__(self): return f"NetNS({self.name})" + + +class NetNSEnter: + def __init__(self, ns_name): + self.ns_path = f"/run/netns/{ns_name}" + + def __enter__(self): + self.saved = open("/proc/thread-self/ns/net") + with open(self.ns_path) as ns_file: + libc.setns(ns_file.fileno(), 0) + return self + + def __exit__(self, exc_type, exc_value, traceback): + libc.setns(self.saved.fileno(), 0) + self.saved.close()
Signature · 1 line
2.47.0
PATCH v3 6/6 selftests: net: Add two test cases for link netns
In reply to Xiao Liang - Add test for creating link in another netns when a link of the same name and ifindex exists in current netns.
- Add test for link netns atomicity - create link directly in target netns, and no notifications should be generated in current netns.
--- tools/testing/selftests/net/Makefile | 1 + tools/testing/selftests/net/netns-name.sh | 10 ++++++ tools/testing/selftests/net/netns_atomic.py | 38 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100755 tools/testing/selftests/net/netns_atomic.py
tools/testing/selftests/net/Makefile +1 -0
@@ -34,6 +34,7 @@ TEST_PROGS += gre_gso.sh TEST_PROGS += cmsg_so_mark.sh TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh TEST_PROGS += netns-name.sh +TEST_PROGS += netns_atomic.py TEST_PROGS += nl_netdev.py TEST_PROGS += srv6_end_dt46_l3vpn_test.sh TEST_PROGS += srv6_end_dt4_l3vpn_test.sh
tools/testing/selftests/net/netns-name.sh +10 -0
@@ -78,6 +78,16 @@ ip -netns $NS link show dev $ALT_NAME 2> /dev/null && fail "Can still find alt-name after move" ip -netns $test_ns link del $DEV || fail +# +# Test no conflict of the same name/ifindex in different netns +# +ip -netns $NS link add name $DEV index 100 type dummy || fail +ip -netns $NS link add netns $test_ns name $DEV index 100 type dummy || + fail "Can create in netns without moving" +ip -netns $test_ns link show dev $DEV >> /dev/null || fail "Device not found" +ip -netns $NS link del $DEV || fail +ip -netns $test_ns link del $DEV || fail + echo -ne "$(basename $0) \t\t\t\t" if [ $RET_CODE -eq 0 ]; then echo "[ OK ]"
tools/testing/selftests/net/netns_atomic.py new file +38 -0
@@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +import time + +from lib.py import ksft_run, ksft_exit, ksft_true +from lib.py import ip +from lib.py import NetNS, NetNSEnter +from lib.py import RtnlFamily + + +def test_event(ns1, ns2) -> None: + with NetNSEnter(str(ns1)): + rtnl = RtnlFamily() + + rtnl.ntf_subscribe("rtnlgrp-link") + + ip(f"netns set {ns1} 0", ns=str(ns2)) + + ip(f"link add netns {ns2} link-netnsid 0 dummy1 type dummy") + ip(f"link add netns {ns2} dummy2 type dummy", ns=str(ns1)) + + ip("link del dummy1", ns=str(ns2)) + ip("link del dummy2", ns=str(ns2)) + + time.sleep(1) + rtnl.check_ntf() + ksft_true(not rtnl.async_msg_queue, "Received unexpected link notification") + + +def main() -> None: + with NetNS() as ns1, NetNS() as ns2: + ksft_run([test_event], args=(ns1, ns2)) + ksft_exit() + + +if __name__ == "__main__": + main()
Signature · 1 line
2.47.0