Commit 16748707 authored by Eric Dumazet's avatar Eric Dumazet Committed by Jakub Kicinski

inet: use xa_array iterator to implement inet_netconf_dump_devconf()

1) inet_netconf_dump_devconf() can run under RCU protection
   instead of RTNL.

2) properly return 0 at the end of a dump, avoiding an
   an extra recvmsg() system call.

3) Do not use inet_base_seq() anymore, for_each_netdev_dump()
   has nice properties. Restarting a GETDEVCONF dump if a device has
   been added/removed or if net->ipv4.dev_addr_genid has changed is moot.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarJiri Pirko <jiri@nvidia.com>
Link: https://lore.kernel.org/r/20240227092411.2315725-4-edumazet@google.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent bbcf9105
...@@ -2267,11 +2267,13 @@ static int inet_netconf_dump_devconf(struct sk_buff *skb, ...@@ -2267,11 +2267,13 @@ static int inet_netconf_dump_devconf(struct sk_buff *skb,
{ {
const struct nlmsghdr *nlh = cb->nlh; const struct nlmsghdr *nlh = cb->nlh;
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
int h, s_h; struct {
int idx, s_idx; unsigned long ifindex;
unsigned int all_default;
} *ctx = (void *)cb->ctx;
const struct in_device *in_dev;
struct net_device *dev; struct net_device *dev;
struct in_device *in_dev; int err = 0;
struct hlist_head *head;
if (cb->strict_check) { if (cb->strict_check) {
struct netlink_ext_ack *extack = cb->extack; struct netlink_ext_ack *extack = cb->extack;
...@@ -2288,64 +2290,47 @@ static int inet_netconf_dump_devconf(struct sk_buff *skb, ...@@ -2288,64 +2290,47 @@ static int inet_netconf_dump_devconf(struct sk_buff *skb,
} }
} }
s_h = cb->args[0];
s_idx = idx = cb->args[1];
for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
idx = 0;
head = &net->dev_index_head[h];
rcu_read_lock(); rcu_read_lock();
cb->seq = inet_base_seq(net); for_each_netdev_dump(net, dev, ctx->ifindex) {
hlist_for_each_entry_rcu(dev, head, index_hlist) {
if (idx < s_idx)
goto cont;
in_dev = __in_dev_get_rcu(dev); in_dev = __in_dev_get_rcu(dev);
if (!in_dev) if (!in_dev)
goto cont; continue;
err = inet_netconf_fill_devconf(skb, dev->ifindex,
if (inet_netconf_fill_devconf(skb, dev->ifindex,
&in_dev->cnf, &in_dev->cnf,
NETLINK_CB(cb->skb).portid, NETLINK_CB(cb->skb).portid,
nlh->nlmsg_seq, nlh->nlmsg_seq,
RTM_NEWNETCONF, RTM_NEWNETCONF, NLM_F_MULTI,
NLM_F_MULTI, NETCONFA_ALL);
NETCONFA_ALL) < 0) { if (err < 0)
rcu_read_unlock();
goto done; goto done;
} }
nl_dump_check_consistent(cb, nlmsg_hdr(skb)); if (ctx->all_default == 0) {
cont: err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
idx++;
}
rcu_read_unlock();
}
if (h == NETDEV_HASHENTRIES) {
if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
net->ipv4.devconf_all, net->ipv4.devconf_all,
NETLINK_CB(cb->skb).portid, NETLINK_CB(cb->skb).portid,
nlh->nlmsg_seq, nlh->nlmsg_seq,
RTM_NEWNETCONF, NLM_F_MULTI, RTM_NEWNETCONF, NLM_F_MULTI,
NETCONFA_ALL) < 0) NETCONFA_ALL);
if (err < 0)
goto done; goto done;
else ctx->all_default++;
h++;
} }
if (h == NETDEV_HASHENTRIES + 1) { if (ctx->all_default == 1) {
if (inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
net->ipv4.devconf_dflt, net->ipv4.devconf_dflt,
NETLINK_CB(cb->skb).portid, NETLINK_CB(cb->skb).portid,
nlh->nlmsg_seq, nlh->nlmsg_seq,
RTM_NEWNETCONF, NLM_F_MULTI, RTM_NEWNETCONF, NLM_F_MULTI,
NETCONFA_ALL) < 0) NETCONFA_ALL);
if (err < 0)
goto done; goto done;
else ctx->all_default++;
h++;
} }
done: done:
cb->args[0] = h; if (err < 0 && likely(skb->len))
cb->args[1] = idx; err = skb->len;
rcu_read_unlock();
return skb->len; return err;
} }
#ifdef CONFIG_SYSCTL #ifdef CONFIG_SYSCTL
...@@ -2829,5 +2814,5 @@ void __init devinet_init(void) ...@@ -2829,5 +2814,5 @@ void __init devinet_init(void)
rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0); rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0);
rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf, rtnl_register(PF_INET, RTM_GETNETCONF, inet_netconf_get_devconf,
inet_netconf_dump_devconf, inet_netconf_dump_devconf,
RTNL_FLAG_DOIT_UNLOCKED); RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment