nexthops: Add ipv6 helper to walk all fib6_nh in a nexthop struct
authorDavid Ahern <dsahern@gmail.com>
Sat, 8 Jun 2019 21:53:22 +0000 (14:53 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 10 Jun 2019 17:44:56 +0000 (10:44 -0700)
IPv6 has traditionally had a single fib6_nh per fib6_info. With
nexthops we can have multiple fib6_nh associated with a fib6_info.
Add a nexthop helper to invoke a callback for each fib6_nh in a
'struct nexthop'. If the callback returns non-0, the loop is
stopped and the return value passed to the caller.

Signed-off-by: David Ahern <dsahern@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/net/nexthop.h
net/ipv4/nexthop.c

index e019ed9b3dc3e76ede4e7081ffe7e9d5e54647b5..25f1f9a8419b88eba011f1d14c6e1987146b2b26 100644 (file)
@@ -305,4 +305,8 @@ static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
                res->nh = &nhi->fib6_nh;
        }
 }
+
+int nexthop_for_each_fib6_nh(struct nexthop *nh,
+                            int (*cb)(struct fib6_nh *nh, void *arg),
+                            void *arg);
 #endif
index 5e48762b6b5f7988318fe1607baa2a561ed60ea3..49e8adce5b965a30eeff3c19f9116ae5020a75c4 100644 (file)
@@ -517,6 +517,37 @@ struct nexthop *nexthop_select_path(struct nexthop *nh, int hash)
 }
 EXPORT_SYMBOL_GPL(nexthop_select_path);
 
+int nexthop_for_each_fib6_nh(struct nexthop *nh,
+                            int (*cb)(struct fib6_nh *nh, void *arg),
+                            void *arg)
+{
+       struct nh_info *nhi;
+       int err;
+
+       if (nh->is_group) {
+               struct nh_group *nhg;
+               int i;
+
+               nhg = rcu_dereference_rtnl(nh->nh_grp);
+               for (i = 0; i < nhg->num_nh; i++) {
+                       struct nh_grp_entry *nhge = &nhg->nh_entries[i];
+
+                       nhi = rcu_dereference_rtnl(nhge->nh->nh_info);
+                       err = cb(&nhi->fib6_nh, arg);
+                       if (err)
+                               return err;
+               }
+       } else {
+               nhi = rcu_dereference_rtnl(nh->nh_info);
+               err = cb(&nhi->fib6_nh, arg);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(nexthop_for_each_fib6_nh);
+
 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
                       struct netlink_ext_ack *extack)
 {