Skip to content

Commit 6ef848e

Browse files
committed
Merge branch 'rtnetlink-add-IFA_TARGET_NETNSID-for-RTM_GETADDR'
Christian Brauner says: ==================== rtnetlink: add IFA_TARGET_NETNSID for RTM_GETADDR This iteration should mainly addresses the suggestion to use IFA_TARGET_NETNSID as the property name. Additionally, an an alias for the already existing IFLA_IF_NETNSID property is added. Note that two additional cleanup patches (8\9 and 9\9) were added to address concerns raised that passing more than 6 arguments to a function will cause additional variables to be pushed onto the stack instead of being placed into registers. The way I addressed this is by introducing two new struct inet{6}_fill_args that are used to pass common information down to inet{6}_fill_if*() functions shortening all those functions to three pointer arguments. If this is something more people than Kirill find useful they can be kept if not they can simply be dropped in later iterations of this series or when merging. Here is a short overview: 1. Rename from IFA_IF_NETNSID to IFA_TARGET_NETNSID. 2. Add IFLA_TARGET_NETNSID as an alias for IFA_IFLA_NETNSID and switch all occurrences over to the new alias. 3. Add inet4_fill_args struct to avoid passing more than 6 arguments in inet_fill_if*() functions. 4. Add inet6_fill_args struct to avoid passing more than 6 arguments in inet_fill_if*() functions. The only functional change is the export of rtnl_get_net_ns_capable() which is needed in case ipv6 is built as a module. Note, I did not change the property name to IFA_TARGET_NSID as there was no clear agreement what would be preferred. My personal preference is to keep the IFA_IF_NETNSID name because it aligns naturally with the IFLA_IF_NETNSID property for RTM_*LINK requests. Jiri seems to prefer this name too. However, if there is agreement that another property name makes more sense I'm happy to send a v2 that changes this. To test this patchset I performed 1 million getifaddrs() requests against a network namespace containing 5 interfaces (lo, eth{0-4}). The first test used a network namespace aware getifaddrs() implementation I wrote and the second test used the traditional setns() + getifaddrs() method. The results show that this patchsets allows userspace to cut retrieval time in half: 1. netns_getifaddrs(): 82 microseconds 2. setns() + getifaddrs(): 162 microseconds A while back we introduced and enabled IFLA_IF_NETNSID in RTM_{DEL,GET,NEW}LINK requests (cf. [1], [2], [3], [4], [5]). This has led to signficant performance increases since it allows userspace to avoid taking the hit of a setns(netns_fd, CLONE_NEWNET), then getting the interfaces from the netns associated with the netns_fd. Especially when a lot of network namespaces are in use, using setns() becomes increasingly problematic when performance matters. Usually, RTML_GETLINK requests are followed by RTM_GETADDR requests (cf. getifaddrs() style functions and friends). But currently, RTM_GETADDR requests do not support a similar property like IFLA_IF_NETNSID for RTM_*LINK requests. This is problematic since userspace can retrieve interfaces from another network namespace by sending a IFLA_IF_NETNSID property along but RTM_GETLINK request but is still forced to use the legacy setns() style of retrieving interfaces in RTM_GETADDR requests. The goal of this series is to make it possible to perform RTM_GETADDR requests on different network namespaces. To this end a new IFA_IF_NETNSID property for RTM_*ADDR requests is introduced. It can be used to send a network namespace identifier along in RTM_*ADDR requests. The network namespace identifier will be used to retrieve the target network namespace in which the request is supposed to be fulfilled. This aligns the behavior of RTM_*ADDR requests with the behavior of RTM_*LINK requests. - The caller must have assigned a valid network namespace identifier for the target network namespace. - The caller must have CAP_NET_ADMIN in the owning user namespace of the target network namespace. [1]: commit 7973bfd ("rtnetlink: remove check for IFLA_IF_NETNSID") [2]: commit 5bb8ed0 ("rtnetlink: enable IFLA_IF_NETNSID for RTM_NEWLINK") [3]: commit b61ad68 ("rtnetlink: enable IFLA_IF_NETNSID for RTM_DELLINK") [4]: commit c310bfc ("rtnetlink: enable IFLA_IF_NETNSID for RTM_SETLINK") [5]: commit 7c4f63b ("rtnetlink: enable IFLA_IF_NETNSID in do_setlink()") ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents d4cc597 + 203651b commit 6ef848e

7 files changed

Lines changed: 180 additions & 63 deletions

File tree

‎include/net/rtnetlink.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm);
165165

166166
int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len,
167167
struct netlink_ext_ack *exterr);
168+
struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid);
168169

169170
#define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
170171

‎include/uapi/linux/if_addr.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum {
3434
IFA_MULTICAST,
3535
IFA_FLAGS,
3636
IFA_RT_PRIORITY, /* u32, priority/metric for prefix route */
37+
IFA_TARGET_NETNSID,
3738
__IFA_MAX,
3839
};
3940

‎include/uapi/linux/if_link.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ enum {
161161
IFLA_EVENT,
162162
IFLA_NEW_NETNSID,
163163
IFLA_IF_NETNSID,
164+
IFLA_TARGET_NETNSID = IFLA_IF_NETNSID, /* new alias */
164165
IFLA_CARRIER_UP_COUNT,
165166
IFLA_CARRIER_DOWN_COUNT,
166167
IFLA_NEW_IFINDEX,

‎net/core/rtnetlink.c‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
10161016
+ nla_total_size(4) /* IFLA_NEW_NETNSID */
10171017
+ nla_total_size(4) /* IFLA_NEW_IFINDEX */
10181018
+ nla_total_size(1) /* IFLA_PROTO_DOWN */
1019-
+ nla_total_size(4) /* IFLA_IF_NETNSID */
1019+
+ nla_total_size(4) /* IFLA_TARGET_NETNSID */
10201020
+ nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
10211021
+ nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
10221022
+ nla_total_size(4) /* IFLA_MIN_MTU */
@@ -1598,7 +1598,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
15981598
ifm->ifi_flags = dev_get_flags(dev);
15991599
ifm->ifi_change = change;
16001600

1601-
if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid))
1601+
if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
16021602
goto nla_put_failure;
16031603

16041604
if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
@@ -1737,7 +1737,7 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
17371737
[IFLA_XDP] = { .type = NLA_NESTED },
17381738
[IFLA_EVENT] = { .type = NLA_U32 },
17391739
[IFLA_GROUP] = { .type = NLA_U32 },
1740-
[IFLA_IF_NETNSID] = { .type = NLA_S32 },
1740+
[IFLA_TARGET_NETNSID] = { .type = NLA_S32 },
17411741
[IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
17421742
[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
17431743
[IFLA_MIN_MTU] = { .type = NLA_U32 },
@@ -1845,7 +1845,15 @@ static bool link_dump_filtered(struct net_device *dev,
18451845
return false;
18461846
}
18471847

1848-
static struct net *get_target_net(struct sock *sk, int netnsid)
1848+
/**
1849+
* rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
1850+
* @sk: netlink socket
1851+
* @netnsid: network namespace identifier
1852+
*
1853+
* Returns the network namespace identified by netnsid on success or an error
1854+
* pointer on failure.
1855+
*/
1856+
struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
18491857
{
18501858
struct net *net;
18511859

@@ -1862,6 +1870,7 @@ static struct net *get_target_net(struct sock *sk, int netnsid)
18621870
}
18631871
return net;
18641872
}
1873+
EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
18651874

18661875
static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
18671876
{
@@ -1895,9 +1904,9 @@ static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
18951904

18961905
if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX,
18971906
ifla_policy, NULL) >= 0) {
1898-
if (tb[IFLA_IF_NETNSID]) {
1899-
netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
1900-
tgt_net = get_target_net(skb->sk, netnsid);
1907+
if (tb[IFLA_TARGET_NETNSID]) {
1908+
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
1909+
tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
19011910
if (IS_ERR(tgt_net)) {
19021911
tgt_net = net;
19031912
netnsid = -1;
@@ -1984,7 +1993,7 @@ EXPORT_SYMBOL(rtnl_link_get_net);
19841993
*
19851994
* 1. IFLA_NET_NS_PID
19861995
* 2. IFLA_NET_NS_FD
1987-
* 3. IFLA_IF_NETNSID
1996+
* 3. IFLA_TARGET_NETNSID
19881997
*/
19891998
static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
19901999
struct nlattr *tb[])
@@ -1994,10 +2003,10 @@ static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
19942003
if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
19952004
return rtnl_link_get_net(src_net, tb);
19962005

1997-
if (!tb[IFLA_IF_NETNSID])
2006+
if (!tb[IFLA_TARGET_NETNSID])
19982007
return get_net(src_net);
19992008

2000-
net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID]));
2009+
net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
20012010
if (!net)
20022011
return ERR_PTR(-EINVAL);
20032012

@@ -2038,13 +2047,13 @@ static int rtnl_ensure_unique_netns(struct nlattr *tb[],
20382047
return -EOPNOTSUPP;
20392048
}
20402049

2041-
if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2050+
if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
20422051
goto invalid_attr;
20432052

2044-
if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD]))
2053+
if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
20452054
goto invalid_attr;
20462055

2047-
if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID]))
2056+
if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
20482057
goto invalid_attr;
20492058

20502059
return 0;
@@ -2320,7 +2329,7 @@ static int do_setlink(const struct sk_buff *skb,
23202329
if (err < 0)
23212330
return err;
23222331

2323-
if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) {
2332+
if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
23242333
struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev),
23252334
tb, CAP_NET_ADMIN);
23262335
if (IS_ERR(net)) {
@@ -2763,9 +2772,9 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
27632772
if (tb[IFLA_IFNAME])
27642773
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
27652774

2766-
if (tb[IFLA_IF_NETNSID]) {
2767-
netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
2768-
tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
2775+
if (tb[IFLA_TARGET_NETNSID]) {
2776+
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
2777+
tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
27692778
if (IS_ERR(tgt_net))
27702779
return PTR_ERR(tgt_net);
27712780
}
@@ -3173,9 +3182,9 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
31733182
if (err < 0)
31743183
return err;
31753184

3176-
if (tb[IFLA_IF_NETNSID]) {
3177-
netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]);
3178-
tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid);
3185+
if (tb[IFLA_TARGET_NETNSID]) {
3186+
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3187+
tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
31793188
if (IS_ERR(tgt_net))
31803189
return PTR_ERR(tgt_net);
31813190
}
@@ -3260,13 +3269,13 @@ static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
32603269
{
32613270
int idx;
32623271
int s_idx = cb->family;
3272+
int type = cb->nlh->nlmsg_type - RTM_BASE;
32633273

32643274
if (s_idx == 0)
32653275
s_idx = 1;
32663276

32673277
for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
32683278
struct rtnl_link **tab;
3269-
int type = cb->nlh->nlmsg_type-RTM_BASE;
32703279
struct rtnl_link *link;
32713280
rtnl_dumpit_func dumpit;
32723281

‎net/ipv4/devinet.c‎

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
100100
[IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
101101
[IFA_FLAGS] = { .type = NLA_U32 },
102102
[IFA_RT_PRIORITY] = { .type = NLA_U32 },
103+
[IFA_TARGET_NETNSID] = { .type = NLA_S32 },
104+
};
105+
106+
struct inet_fill_args {
107+
u32 portid;
108+
u32 seq;
109+
int event;
110+
unsigned int flags;
111+
int netnsid;
103112
};
104113

105114
#define IN4_ADDR_HSIZE_SHIFT 8
@@ -1584,13 +1593,14 @@ static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
15841593
}
15851594

15861595
static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1587-
u32 portid, u32 seq, int event, unsigned int flags)
1596+
struct inet_fill_args *args)
15881597
{
15891598
struct ifaddrmsg *ifm;
15901599
struct nlmsghdr *nlh;
15911600
u32 preferred, valid;
15921601

1593-
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
1602+
nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm),
1603+
args->flags);
15941604
if (!nlh)
15951605
return -EMSGSIZE;
15961606

@@ -1601,6 +1611,10 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
16011611
ifm->ifa_scope = ifa->ifa_scope;
16021612
ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
16031613

1614+
if (args->netnsid >= 0 &&
1615+
nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
1616+
goto nla_put_failure;
1617+
16041618
if (!(ifm->ifa_flags & IFA_F_PERMANENT)) {
16051619
preferred = ifa->ifa_preferred_lft;
16061620
valid = ifa->ifa_valid_lft;
@@ -1647,7 +1661,16 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
16471661

16481662
static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16491663
{
1664+
struct inet_fill_args fillargs = {
1665+
.portid = NETLINK_CB(cb->skb).portid,
1666+
.seq = cb->nlh->nlmsg_seq,
1667+
.event = RTM_NEWADDR,
1668+
.flags = NLM_F_MULTI,
1669+
.netnsid = -1,
1670+
};
16501671
struct net *net = sock_net(skb->sk);
1672+
struct nlattr *tb[IFA_MAX+1];
1673+
struct net *tgt_net = net;
16511674
int h, s_h;
16521675
int idx, s_idx;
16531676
int ip_idx, s_ip_idx;
@@ -1660,12 +1683,24 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16601683
s_idx = idx = cb->args[1];
16611684
s_ip_idx = ip_idx = cb->args[2];
16621685

1686+
if (nlmsg_parse(cb->nlh, sizeof(struct ifaddrmsg), tb, IFA_MAX,
1687+
ifa_ipv4_policy, NULL) >= 0) {
1688+
if (tb[IFA_TARGET_NETNSID]) {
1689+
fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
1690+
1691+
tgt_net = rtnl_get_net_ns_capable(skb->sk,
1692+
fillargs.netnsid);
1693+
if (IS_ERR(tgt_net))
1694+
return PTR_ERR(tgt_net);
1695+
}
1696+
}
1697+
16631698
for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
16641699
idx = 0;
1665-
head = &net->dev_index_head[h];
1700+
head = &tgt_net->dev_index_head[h];
16661701
rcu_read_lock();
1667-
cb->seq = atomic_read(&net->ipv4.dev_addr_genid) ^
1668-
net->dev_base_seq;
1702+
cb->seq = atomic_read(&tgt_net->ipv4.dev_addr_genid) ^
1703+
tgt_net->dev_base_seq;
16691704
hlist_for_each_entry_rcu(dev, head, index_hlist) {
16701705
if (idx < s_idx)
16711706
goto cont;
@@ -1679,10 +1714,7 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16791714
ifa = ifa->ifa_next, ip_idx++) {
16801715
if (ip_idx < s_ip_idx)
16811716
continue;
1682-
if (inet_fill_ifaddr(skb, ifa,
1683-
NETLINK_CB(cb->skb).portid,
1684-
cb->nlh->nlmsg_seq,
1685-
RTM_NEWADDR, NLM_F_MULTI) < 0) {
1717+
if (inet_fill_ifaddr(skb, ifa, &fillargs) < 0) {
16861718
rcu_read_unlock();
16871719
goto done;
16881720
}
@@ -1698,15 +1730,23 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
16981730
cb->args[0] = h;
16991731
cb->args[1] = idx;
17001732
cb->args[2] = ip_idx;
1733+
if (fillargs.netnsid >= 0)
1734+
put_net(tgt_net);
17011735

17021736
return skb->len;
17031737
}
17041738

17051739
static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
17061740
u32 portid)
17071741
{
1742+
struct inet_fill_args fillargs = {
1743+
.portid = portid,
1744+
.seq = nlh ? nlh->nlmsg_seq : 0,
1745+
.event = event,
1746+
.flags = 0,
1747+
.netnsid = -1,
1748+
};
17081749
struct sk_buff *skb;
1709-
u32 seq = nlh ? nlh->nlmsg_seq : 0;
17101750
int err = -ENOBUFS;
17111751
struct net *net;
17121752

@@ -1715,7 +1755,7 @@ static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
17151755
if (!skb)
17161756
goto errout;
17171757

1718-
err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0);
1758+
err = inet_fill_ifaddr(skb, ifa, &fillargs);
17191759
if (err < 0) {
17201760
/* -EMSGSIZE implies BUG in inet_nlmsg_size() */
17211761
WARN_ON(err == -EMSGSIZE);

0 commit comments

Comments
 (0)