ipv6: use xarray iterator to implement inet6_dump_ifinfo()
authorEric Dumazet <edumazet@google.com>
Thu, 22 Feb 2024 10:50:11 +0000 (10:50 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 26 Feb 2024 11:46:12 +0000 (11:46 +0000)
Prepare inet6_dump_ifinfo() to run with RCU protection
instead of RTNL and use for_each_netdev_dump() interface.

Also properly return 0 at the end of a dump, avoiding
an extra recvmsg() system call and RTNL acquisition.

Note that RTNL-less dumps need core changes, coming later
in the series.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/ipv6/addrconf.c

index daa81556d1180b8bfd51d9b259c069b549883901..6556136d2b8dd1f3237a6b692580ed479c8c9a13 100644 (file)
@@ -6132,50 +6132,42 @@ static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 {
        struct net *net = sock_net(skb->sk);
-       int h, s_h;
-       int idx = 0, s_idx;
+       struct {
+               unsigned long ifindex;
+       } *ctx = (void *)cb->ctx;
        struct net_device *dev;
        struct inet6_dev *idev;
-       struct hlist_head *head;
+       int err;
 
        /* only requests using strict checking can pass data to
         * influence the dump
         */
        if (cb->strict_check) {
-               int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
+               err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
 
                if (err < 0)
                        return err;
        }
 
-       s_h = cb->args[0];
-       s_idx = cb->args[1];
-
+       err = 0;
        rcu_read_lock();
-       for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
-               idx = 0;
-               head = &net->dev_index_head[h];
-               hlist_for_each_entry_rcu(dev, head, index_hlist) {
-                       if (idx < s_idx)
-                               goto cont;
-                       idev = __in6_dev_get(dev);
-                       if (!idev)
-                               goto cont;
-                       if (inet6_fill_ifinfo(skb, idev,
-                                             NETLINK_CB(cb->skb).portid,
-                                             cb->nlh->nlmsg_seq,
-                                             RTM_NEWLINK, NLM_F_MULTI) < 0)
-                               goto out;
-cont:
-                       idx++;
+       for_each_netdev_dump(net, dev, ctx->ifindex) {
+               idev = __in6_dev_get(dev);
+               if (!idev)
+                       continue;
+               err = inet6_fill_ifinfo(skb, idev,
+                                       NETLINK_CB(cb->skb).portid,
+                                       cb->nlh->nlmsg_seq,
+                                       RTM_NEWLINK, NLM_F_MULTI);
+               if (err < 0) {
+                       if (likely(skb->len))
+                               err = skb->len;
+                       break;
                }
        }
-out:
        rcu_read_unlock();
-       cb->args[1] = idx;
-       cb->args[0] = h;
 
-       return skb->len;
+       return err;
 }
 
 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)