1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Generic nexthop implementation
5 * Copyright (c) 2017-19 Cumulus Networks
6 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
9 #ifndef __LINUX_NEXTHOP_H
10 #define __LINUX_NEXTHOP_H
12 #include <linux/netdevice.h>
13 #include <linux/notifier.h>
14 #include <linux/route.h>
15 #include <linux/types.h>
16 #include <net/ip_fib.h>
17 #include <net/ip6_fib.h>
18 #include <net/netlink.h>
20 #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
34 struct net_device *dev;
41 struct nlattr *nh_grp;
43 u16 nh_grp_res_num_buckets;
44 unsigned long nh_grp_res_idle_timer;
45 unsigned long nh_grp_res_unbalanced_timer;
46 bool nh_grp_res_has_num_buckets;
47 bool nh_grp_res_has_idle_timer;
48 bool nh_grp_res_has_unbalanced_timer;
52 struct nlattr *nh_encap;
56 struct nl_info nlinfo;
60 struct hlist_node dev_hash; /* entry on netns devhash */
61 struct nexthop *nh_parent;
68 struct fib_nh_common fib_nhc;
70 struct fib6_nh fib6_nh;
74 struct nh_res_bucket {
75 struct nh_grp_entry __rcu *nh_entry;
76 atomic_long_t used_time;
77 unsigned long migrated_time;
85 struct delayed_work upkeep_dw;
87 /* List of NHGEs that have too few buckets ("uw" for underweight).
88 * Reclaimed buckets will be given to entries in this list.
90 struct list_head uw_nh_entries;
91 unsigned long unbalanced_since;
97 struct nh_res_bucket nh_buckets[] __counted_by(num_nh_buckets);
100 struct nh_grp_entry_stats {
102 struct u64_stats_sync syncp;
105 struct nh_grp_entry {
107 struct nh_grp_entry_stats __percpu *stats;
112 atomic_t upper_bound;
115 /* Member on uw_nh_entries. */
116 struct list_head uw_nh_entry;
123 struct list_head nh_list;
124 struct nexthop *nh_parent; /* nexthop of group with this entry */
129 struct nh_group *spare; /* spare group for removals */
138 struct nh_res_table __rcu *res_table;
139 struct nh_grp_entry nh_entries[] __counted_by(num_nh);
143 struct rb_node rb_node; /* entry on netns rbtree */
144 struct list_head fi_list; /* v4 entries using nh */
145 struct list_head f6i_list; /* v6 entries using nh */
146 struct list_head fdb_list; /* fdb entries using this nh */
147 struct list_head grp_list; /* nh group entries using this nh */
152 u8 protocol; /* app managing this nh */
160 struct nh_info __rcu *nh_info;
161 struct nh_group __rcu *nh_grp;
165 enum nexthop_event_type {
167 NEXTHOP_EVENT_REPLACE,
168 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
169 NEXTHOP_EVENT_BUCKET_REPLACE,
170 NEXTHOP_EVENT_HW_STATS_REPORT_DELTA,
173 enum nh_notifier_info_type {
174 NH_NOTIFIER_INFO_TYPE_SINGLE,
175 NH_NOTIFIER_INFO_TYPE_GRP,
176 NH_NOTIFIER_INFO_TYPE_RES_TABLE,
177 NH_NOTIFIER_INFO_TYPE_RES_BUCKET,
178 NH_NOTIFIER_INFO_TYPE_GRP_HW_STATS,
181 struct nh_notifier_single_info {
182 struct net_device *dev;
186 struct in6_addr ipv6;
194 struct nh_notifier_grp_entry_info {
196 struct nh_notifier_single_info nh;
199 struct nh_notifier_grp_info {
203 struct nh_notifier_grp_entry_info nh_entries[] __counted_by(num_nh);
206 struct nh_notifier_res_bucket_info {
208 unsigned int idle_timer_ms;
210 struct nh_notifier_single_info old_nh;
211 struct nh_notifier_single_info new_nh;
214 struct nh_notifier_res_table_info {
217 struct nh_notifier_single_info nhs[] __counted_by(num_nh_buckets);
220 struct nh_notifier_grp_hw_stats_entry_info {
225 struct nh_notifier_grp_hw_stats_info {
228 struct nh_notifier_grp_hw_stats_entry_info stats[] __counted_by(num_nh);
231 struct nh_notifier_info {
233 struct netlink_ext_ack *extack;
235 enum nh_notifier_info_type type;
237 struct nh_notifier_single_info *nh;
238 struct nh_notifier_grp_info *nh_grp;
239 struct nh_notifier_res_table_info *nh_res_table;
240 struct nh_notifier_res_bucket_info *nh_res_bucket;
241 struct nh_notifier_grp_hw_stats_info *nh_grp_hw_stats;
245 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
246 struct netlink_ext_ack *extack);
247 int __unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
248 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
249 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
250 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
251 bool offload, bool trap);
252 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
253 unsigned long *activity);
254 void nh_grp_hw_stats_report_delta(struct nh_notifier_grp_hw_stats_info *info,
258 /* caller is holding rcu or rtnl; no reference taken to nexthop */
259 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
260 void nexthop_free_rcu(struct rcu_head *head);
262 static inline bool nexthop_get(struct nexthop *nh)
264 return refcount_inc_not_zero(&nh->refcnt);
267 static inline void nexthop_put(struct nexthop *nh)
269 if (refcount_dec_and_test(&nh->refcnt))
270 call_rcu(&nh->rcu, nexthop_free_rcu);
273 static inline bool nexthop_cmp(const struct nexthop *nh1,
274 const struct nexthop *nh2)
279 static inline bool nexthop_is_fdb(const struct nexthop *nh)
282 const struct nh_group *nh_grp;
284 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
285 return nh_grp->fdb_nh;
287 const struct nh_info *nhi;
289 nhi = rcu_dereference_rtnl(nh->nh_info);
294 static inline bool nexthop_has_v4(const struct nexthop *nh)
297 struct nh_group *nh_grp;
299 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
300 return nh_grp->has_v4;
305 static inline bool nexthop_is_multipath(const struct nexthop *nh)
308 struct nh_group *nh_grp;
310 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
311 return nh_grp->is_multipath;
316 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
318 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
323 struct nh_group *nh_grp;
325 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
326 if (nh_grp->is_multipath)
334 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
336 /* for_nexthops macros in fib_semantics.c grabs a pointer to
337 * the nexthop before checking nhsel
339 if (nhsel >= nhg->num_nh)
342 return nhg->nh_entries[nhsel].nh;
346 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
349 struct nh_group *nhg = rcu_dereference_rtnl(nh->nh_grp);
352 for (i = 0; i < nhg->num_nh; i++) {
353 struct nexthop *nhe = nhg->nh_entries[i].nh;
354 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
355 struct fib_nh_common *nhc = &nhi->fib_nhc;
356 int weight = nhg->nh_entries[i].weight;
358 if (fib_add_nexthop(skb, nhc, weight, rt_family, 0) < 0)
365 /* called with rcu lock */
366 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
368 const struct nh_info *nhi;
371 struct nh_group *nh_grp;
373 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
374 if (nh_grp->num_nh > 1)
377 nh = nh_grp->nh_entries[0].nh;
380 nhi = rcu_dereference_rtnl(nh->nh_info);
381 return nhi->reject_nh;
384 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
389 nh = nexthop_select_path(res->fi->nh, hash);
390 nhi = rcu_dereference(nh->nh_info);
391 res->nhc = &nhi->fib_nhc;
394 /* called with rcu read lock or rtnl held */
396 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
400 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
401 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
404 struct nh_group *nh_grp;
406 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
407 if (nh_grp->is_multipath) {
408 nh = nexthop_mpath_select(nh_grp, nhsel);
414 nhi = rcu_dereference_rtnl(nh->nh_info);
415 return &nhi->fib_nhc;
418 /* called from fib_table_lookup with rcu_lock */
420 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
422 const struct flowi4 *flp,
428 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
431 for (i = 0; i < nhg->num_nh; i++) {
432 struct nexthop *nhe = nhg->nh_entries[i].nh;
434 nhi = rcu_dereference(nhe->nh_info);
435 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
437 return &nhi->fib_nhc;
441 nhi = rcu_dereference(nh->nh_info);
442 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
444 return &nhi->fib_nhc;
451 static inline bool nexthop_uses_dev(const struct nexthop *nh,
452 const struct net_device *dev)
457 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
460 for (i = 0; i < nhg->num_nh; i++) {
461 struct nexthop *nhe = nhg->nh_entries[i].nh;
463 nhi = rcu_dereference(nhe->nh_info);
464 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
468 nhi = rcu_dereference(nh->nh_info);
469 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
476 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
478 if (unlikely(fi->nh))
479 return nexthop_num_path(fi->nh);
484 int fib_check_nexthop(struct nexthop *nh, u8 scope,
485 struct netlink_ext_ack *extack);
487 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
489 if (unlikely(fi->nh))
490 return nexthop_fib_nhc(fi->nh, nhsel);
492 return &fi->fib_nh[nhsel].nh_common;
495 /* only used when fib_nh is built into fib_info */
496 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
500 return &fi->fib_nh[nhsel];
506 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
507 struct netlink_ext_ack *extack);
509 /* Caller should either hold rcu_read_lock(), or RTNL. */
510 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
515 struct nh_group *nh_grp;
517 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
518 nh = nexthop_mpath_select(nh_grp, 0);
523 nhi = rcu_dereference_rtnl(nh->nh_info);
524 if (nhi->family == AF_INET6)
525 return &nhi->fib6_nh;
530 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
532 struct fib6_nh *fib6_nh;
534 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
535 return fib6_nh->fib_nh_dev;
538 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
540 struct nexthop *nh = res->f6i->nh;
543 nh = nexthop_select_path(nh, hash);
545 nhi = rcu_dereference_rtnl(nh->nh_info);
546 if (nhi->reject_nh) {
547 res->fib6_type = RTN_BLACKHOLE;
548 res->fib6_flags |= RTF_REJECT;
549 res->nh = nexthop_fib6_nh(nh);
551 res->nh = &nhi->fib6_nh;
555 int nexthop_for_each_fib6_nh(struct nexthop *nh,
556 int (*cb)(struct fib6_nh *nh, void *arg),
559 static inline int nexthop_get_family(struct nexthop *nh)
561 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
567 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
569 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
571 return &nhi->fib_nhc;
574 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
580 nhp = nexthop_select_path(nh, hash);
583 nhi = rcu_dereference(nhp->nh_info);
584 return &nhi->fib_nhc;