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;
50 struct nlattr *nh_encap;
54 struct nl_info nlinfo;
58 struct hlist_node dev_hash; /* entry on netns devhash */
59 struct nexthop *nh_parent;
66 struct fib_nh_common fib_nhc;
68 struct fib6_nh fib6_nh;
72 struct nh_res_bucket {
73 struct nh_grp_entry __rcu *nh_entry;
74 atomic_long_t used_time;
75 unsigned long migrated_time;
83 struct delayed_work upkeep_dw;
85 /* List of NHGEs that have too few buckets ("uw" for underweight).
86 * Reclaimed buckets will be given to entries in this list.
88 struct list_head uw_nh_entries;
89 unsigned long unbalanced_since;
95 struct nh_res_bucket nh_buckets[];
104 atomic_t upper_bound;
107 /* Member on uw_nh_entries. */
108 struct list_head uw_nh_entry;
115 struct list_head nh_list;
116 struct nexthop *nh_parent; /* nexthop of group with this entry */
120 struct nh_group *spare; /* spare group for removals */
128 struct nh_res_table __rcu *res_table;
129 struct nh_grp_entry nh_entries[];
133 struct rb_node rb_node; /* entry on netns rbtree */
134 struct list_head fi_list; /* v4 entries using nh */
135 struct list_head f6i_list; /* v6 entries using nh */
136 struct list_head fdb_list; /* fdb entries using this nh */
137 struct list_head grp_list; /* nh group entries using this nh */
142 u8 protocol; /* app managing this nh */
150 struct nh_info __rcu *nh_info;
151 struct nh_group __rcu *nh_grp;
155 enum nexthop_event_type {
157 NEXTHOP_EVENT_REPLACE,
158 NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
159 NEXTHOP_EVENT_BUCKET_REPLACE,
162 enum nh_notifier_info_type {
163 NH_NOTIFIER_INFO_TYPE_SINGLE,
164 NH_NOTIFIER_INFO_TYPE_GRP,
165 NH_NOTIFIER_INFO_TYPE_RES_TABLE,
166 NH_NOTIFIER_INFO_TYPE_RES_BUCKET,
169 struct nh_notifier_single_info {
170 struct net_device *dev;
174 struct in6_addr ipv6;
181 struct nh_notifier_grp_entry_info {
184 struct nh_notifier_single_info nh;
187 struct nh_notifier_grp_info {
190 struct nh_notifier_grp_entry_info nh_entries[];
193 struct nh_notifier_res_bucket_info {
195 unsigned int idle_timer_ms;
197 struct nh_notifier_single_info old_nh;
198 struct nh_notifier_single_info new_nh;
201 struct nh_notifier_res_table_info {
203 struct nh_notifier_single_info nhs[];
206 struct nh_notifier_info {
208 struct netlink_ext_ack *extack;
210 enum nh_notifier_info_type type;
212 struct nh_notifier_single_info *nh;
213 struct nh_notifier_grp_info *nh_grp;
214 struct nh_notifier_res_table_info *nh_res_table;
215 struct nh_notifier_res_bucket_info *nh_res_bucket;
219 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
220 struct netlink_ext_ack *extack);
221 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
222 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
223 void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
224 bool offload, bool trap);
225 void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
226 unsigned long *activity);
228 /* caller is holding rcu or rtnl; no reference taken to nexthop */
229 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
230 void nexthop_free_rcu(struct rcu_head *head);
232 static inline bool nexthop_get(struct nexthop *nh)
234 return refcount_inc_not_zero(&nh->refcnt);
237 static inline void nexthop_put(struct nexthop *nh)
239 if (refcount_dec_and_test(&nh->refcnt))
240 call_rcu(&nh->rcu, nexthop_free_rcu);
243 static inline bool nexthop_cmp(const struct nexthop *nh1,
244 const struct nexthop *nh2)
249 static inline bool nexthop_is_fdb(const struct nexthop *nh)
252 const struct nh_group *nh_grp;
254 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
255 return nh_grp->fdb_nh;
257 const struct nh_info *nhi;
259 nhi = rcu_dereference_rtnl(nh->nh_info);
264 static inline bool nexthop_has_v4(const struct nexthop *nh)
267 struct nh_group *nh_grp;
269 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
270 return nh_grp->has_v4;
275 static inline bool nexthop_is_multipath(const struct nexthop *nh)
278 struct nh_group *nh_grp;
280 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
281 return nh_grp->is_multipath;
286 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
288 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
293 struct nh_group *nh_grp;
295 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
296 if (nh_grp->is_multipath)
304 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
306 /* for_nexthops macros in fib_semantics.c grabs a pointer to
307 * the nexthop before checking nhsel
309 if (nhsel >= nhg->num_nh)
312 return nhg->nh_entries[nhsel].nh;
316 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
319 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
322 for (i = 0; i < nhg->num_nh; i++) {
323 struct nexthop *nhe = nhg->nh_entries[i].nh;
324 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
325 struct fib_nh_common *nhc = &nhi->fib_nhc;
326 int weight = nhg->nh_entries[i].weight;
328 if (fib_add_nexthop(skb, nhc, weight, rt_family, 0) < 0)
335 /* called with rcu lock */
336 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
338 const struct nh_info *nhi;
341 struct nh_group *nh_grp;
343 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
344 if (nh_grp->num_nh > 1)
347 nh = nh_grp->nh_entries[0].nh;
350 nhi = rcu_dereference_rtnl(nh->nh_info);
351 return nhi->reject_nh;
354 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
359 nh = nexthop_select_path(res->fi->nh, hash);
360 nhi = rcu_dereference(nh->nh_info);
361 res->nhc = &nhi->fib_nhc;
364 /* called with rcu read lock or rtnl held */
366 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
370 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
371 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
374 struct nh_group *nh_grp;
376 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
377 if (nh_grp->is_multipath) {
378 nh = nexthop_mpath_select(nh_grp, nhsel);
384 nhi = rcu_dereference_rtnl(nh->nh_info);
385 return &nhi->fib_nhc;
388 /* called from fib_table_lookup with rcu_lock */
390 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
392 const struct flowi4 *flp,
398 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
401 for (i = 0; i < nhg->num_nh; i++) {
402 struct nexthop *nhe = nhg->nh_entries[i].nh;
404 nhi = rcu_dereference(nhe->nh_info);
405 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
407 return &nhi->fib_nhc;
411 nhi = rcu_dereference(nh->nh_info);
412 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
414 return &nhi->fib_nhc;
421 static inline bool nexthop_uses_dev(const struct nexthop *nh,
422 const struct net_device *dev)
427 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
430 for (i = 0; i < nhg->num_nh; i++) {
431 struct nexthop *nhe = nhg->nh_entries[i].nh;
433 nhi = rcu_dereference(nhe->nh_info);
434 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
438 nhi = rcu_dereference(nh->nh_info);
439 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
446 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
448 if (unlikely(fi->nh))
449 return nexthop_num_path(fi->nh);
454 int fib_check_nexthop(struct nexthop *nh, u8 scope,
455 struct netlink_ext_ack *extack);
457 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
459 if (unlikely(fi->nh))
460 return nexthop_fib_nhc(fi->nh, nhsel);
462 return &fi->fib_nh[nhsel].nh_common;
465 /* only used when fib_nh is built into fib_info */
466 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
470 return &fi->fib_nh[nhsel];
476 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
477 struct netlink_ext_ack *extack);
479 /* Caller should either hold rcu_read_lock(), or RTNL. */
480 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
485 struct nh_group *nh_grp;
487 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
488 nh = nexthop_mpath_select(nh_grp, 0);
493 nhi = rcu_dereference_rtnl(nh->nh_info);
494 if (nhi->family == AF_INET6)
495 return &nhi->fib6_nh;
500 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
502 struct fib6_nh *fib6_nh;
504 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
505 return fib6_nh->fib_nh_dev;
508 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
510 struct nexthop *nh = res->f6i->nh;
513 nh = nexthop_select_path(nh, hash);
515 nhi = rcu_dereference_rtnl(nh->nh_info);
516 if (nhi->reject_nh) {
517 res->fib6_type = RTN_BLACKHOLE;
518 res->fib6_flags |= RTF_REJECT;
519 res->nh = nexthop_fib6_nh(nh);
521 res->nh = &nhi->fib6_nh;
525 int nexthop_for_each_fib6_nh(struct nexthop *nh,
526 int (*cb)(struct fib6_nh *nh, void *arg),
529 static inline int nexthop_get_family(struct nexthop *nh)
531 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
537 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
539 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
541 return &nhi->fib_nhc;
544 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
550 nhp = nexthop_select_path(nh, hash);
553 nhi = rcu_dereference(nhp->nh_info);
554 return &nhi->fib_nhc;