Commit cdb2f80f authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

inet: use xa_array iterator to implement inet_dump_ifaddr()

1) inet_dump_ifaddr() can 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.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 590e92cd
...@@ -1676,7 +1676,7 @@ static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, ...@@ -1676,7 +1676,7 @@ static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
} }
static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa, static int inet_fill_ifaddr(struct sk_buff *skb, const struct in_ifaddr *ifa,
struct inet_fill_args *args) struct inet_fill_args *args)
{ {
struct ifaddrmsg *ifm; struct ifaddrmsg *ifm;
...@@ -1805,15 +1805,15 @@ static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, ...@@ -1805,15 +1805,15 @@ static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
} }
static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
struct netlink_callback *cb, int s_ip_idx, struct netlink_callback *cb, int *s_ip_idx,
struct inet_fill_args *fillargs) struct inet_fill_args *fillargs)
{ {
struct in_ifaddr *ifa; struct in_ifaddr *ifa;
int ip_idx = 0; int ip_idx = 0;
int err; int err;
in_dev_for_each_ifa_rtnl(ifa, in_dev) { in_dev_for_each_ifa_rcu(ifa, in_dev) {
if (ip_idx < s_ip_idx) { if (ip_idx < *s_ip_idx) {
ip_idx++; ip_idx++;
continue; continue;
} }
...@@ -1825,9 +1825,9 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, ...@@ -1825,9 +1825,9 @@ static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb,
ip_idx++; ip_idx++;
} }
err = 0; err = 0;
ip_idx = 0;
done: done:
cb->args[2] = ip_idx; *s_ip_idx = ip_idx;
return err; return err;
} }
...@@ -1859,75 +1859,53 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) ...@@ -1859,75 +1859,53 @@ static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
}; };
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct net *tgt_net = net; struct net *tgt_net = net;
int h, s_h; struct {
int idx, s_idx; unsigned long ifindex;
int s_ip_idx; int ip_idx;
struct net_device *dev; } *ctx = (void *)cb->ctx;
struct in_device *in_dev; struct in_device *in_dev;
struct hlist_head *head; struct net_device *dev;
int err = 0; int err = 0;
s_h = cb->args[0]; rcu_read_lock();
s_idx = idx = cb->args[1];
s_ip_idx = cb->args[2];
if (cb->strict_check) { if (cb->strict_check) {
err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
skb->sk, cb); skb->sk, cb);
if (err < 0) if (err < 0)
goto put_tgt_net; goto done;
err = 0;
if (fillargs.ifindex) { if (fillargs.ifindex) {
dev = __dev_get_by_index(tgt_net, fillargs.ifindex); err = -ENODEV;
if (!dev) { dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
err = -ENODEV; if (!dev)
goto put_tgt_net; goto done;
}
in_dev = __in_dev_get_rtnl(dev);
if (in_dev) {
err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
&fillargs);
}
goto put_tgt_net;
}
}
for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
idx = 0;
head = &tgt_net->dev_index_head[h];
rcu_read_lock();
cb->seq = inet_base_seq(tgt_net);
hlist_for_each_entry_rcu(dev, head, index_hlist) {
if (idx < s_idx)
goto cont;
if (h > s_h || idx > s_idx)
s_ip_idx = 0;
in_dev = __in_dev_get_rcu(dev); in_dev = __in_dev_get_rcu(dev);
if (!in_dev) if (!in_dev)
goto cont;
err = in_dev_dump_addr(in_dev, skb, cb, s_ip_idx,
&fillargs);
if (err < 0) {
rcu_read_unlock();
goto done; goto done;
} err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx,
cont: &fillargs);
idx++; goto done;
} }
rcu_read_unlock();
} }
cb->seq = inet_base_seq(tgt_net);
for_each_netdev_dump(net, dev, ctx->ifindex) {
in_dev = __in_dev_get_rcu(dev);
if (!in_dev)
continue;
err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx,
&fillargs);
if (err < 0)
goto done;
}
done: done:
cb->args[0] = h; if (err < 0 && likely(skb->len))
cb->args[1] = idx; err = skb->len;
put_tgt_net:
if (fillargs.netnsid >= 0) if (fillargs.netnsid >= 0)
put_net(tgt_net); put_net(tgt_net);
rcu_read_unlock();
return skb->len ? : err; return err;
} }
static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
...@@ -2818,7 +2796,8 @@ void __init devinet_init(void) ...@@ -2818,7 +2796,8 @@ void __init devinet_init(void)
rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0); rtnl_register(PF_INET, RTM_NEWADDR, inet_rtm_newaddr, NULL, 0);
rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0); rtnl_register(PF_INET, RTM_DELADDR, inet_rtm_deladdr, NULL, 0);
rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr, 0); rtnl_register(PF_INET, RTM_GETADDR, NULL, inet_dump_ifaddr,
RTNL_FLAG_DUMP_UNLOCKED);
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_DUMP_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