1ab84be7eSDavid Ahern /* SPDX-License-Identifier: GPL-2.0 */
2ab84be7eSDavid Ahern /*
3ab84be7eSDavid Ahern * Generic nexthop implementation
4ab84be7eSDavid Ahern *
5ab84be7eSDavid Ahern * Copyright (c) 2017-19 Cumulus Networks
6ab84be7eSDavid Ahern * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
7ab84be7eSDavid Ahern */
8ab84be7eSDavid Ahern
9ab84be7eSDavid Ahern #ifndef __LINUX_NEXTHOP_H
10ab84be7eSDavid Ahern #define __LINUX_NEXTHOP_H
11ab84be7eSDavid Ahern
12ab84be7eSDavid Ahern #include <linux/netdevice.h>
138590ceedSRoopa Prabhu #include <linux/notifier.h>
14f88d8ea6SDavid Ahern #include <linux/route.h>
15ab84be7eSDavid Ahern #include <linux/types.h>
16ab84be7eSDavid Ahern #include <net/ip_fib.h>
1753010f99SDavid Ahern #include <net/ip6_fib.h>
18ab84be7eSDavid Ahern #include <net/netlink.h>
19ab84be7eSDavid Ahern
20ab84be7eSDavid Ahern #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
21ab84be7eSDavid Ahern
22ab84be7eSDavid Ahern struct nexthop;
23ab84be7eSDavid Ahern
24ab84be7eSDavid Ahern struct nh_config {
25ab84be7eSDavid Ahern u32 nh_id;
26ab84be7eSDavid Ahern
27ab84be7eSDavid Ahern u8 nh_family;
28ab84be7eSDavid Ahern u8 nh_protocol;
29ab84be7eSDavid Ahern u8 nh_blackhole;
3038428d68SRoopa Prabhu u8 nh_fdb;
31ab84be7eSDavid Ahern u32 nh_flags;
32ab84be7eSDavid Ahern
33ab84be7eSDavid Ahern int nh_ifindex;
34ab84be7eSDavid Ahern struct net_device *dev;
35ab84be7eSDavid Ahern
36597cfe4fSDavid Ahern union {
37597cfe4fSDavid Ahern __be32 ipv4;
3853010f99SDavid Ahern struct in6_addr ipv6;
39597cfe4fSDavid Ahern } gw;
40597cfe4fSDavid Ahern
41430a0491SDavid Ahern struct nlattr *nh_grp;
42430a0491SDavid Ahern u16 nh_grp_type;
43283a72a5SPetr Machata u16 nh_grp_res_num_buckets;
44283a72a5SPetr Machata unsigned long nh_grp_res_idle_timer;
45283a72a5SPetr Machata unsigned long nh_grp_res_unbalanced_timer;
46283a72a5SPetr Machata bool nh_grp_res_has_num_buckets;
47283a72a5SPetr Machata bool nh_grp_res_has_idle_timer;
48283a72a5SPetr Machata bool nh_grp_res_has_unbalanced_timer;
49430a0491SDavid Ahern
50b513bd03SDavid Ahern struct nlattr *nh_encap;
51b513bd03SDavid Ahern u16 nh_encap_type;
52b513bd03SDavid Ahern
53ab84be7eSDavid Ahern u32 nlflags;
54ab84be7eSDavid Ahern struct nl_info nlinfo;
55ab84be7eSDavid Ahern };
56ab84be7eSDavid Ahern
57ab84be7eSDavid Ahern struct nh_info {
58ab84be7eSDavid Ahern struct hlist_node dev_hash; /* entry on netns devhash */
59ab84be7eSDavid Ahern struct nexthop *nh_parent;
60ab84be7eSDavid Ahern
61ab84be7eSDavid Ahern u8 family;
62ab84be7eSDavid Ahern bool reject_nh;
6338428d68SRoopa Prabhu bool fdb_nh;
64ab84be7eSDavid Ahern
65ab84be7eSDavid Ahern union {
66ab84be7eSDavid Ahern struct fib_nh_common fib_nhc;
67597cfe4fSDavid Ahern struct fib_nh fib_nh;
6853010f99SDavid Ahern struct fib6_nh fib6_nh;
69ab84be7eSDavid Ahern };
70ab84be7eSDavid Ahern };
71ab84be7eSDavid Ahern
72283a72a5SPetr Machata struct nh_res_bucket {
73283a72a5SPetr Machata struct nh_grp_entry __rcu *nh_entry;
74283a72a5SPetr Machata atomic_long_t used_time;
75283a72a5SPetr Machata unsigned long migrated_time;
76283a72a5SPetr Machata bool occupied;
77283a72a5SPetr Machata u8 nh_flags;
78283a72a5SPetr Machata };
79283a72a5SPetr Machata
80283a72a5SPetr Machata struct nh_res_table {
81283a72a5SPetr Machata struct net *net;
82283a72a5SPetr Machata u32 nhg_id;
83283a72a5SPetr Machata struct delayed_work upkeep_dw;
84283a72a5SPetr Machata
85283a72a5SPetr Machata /* List of NHGEs that have too few buckets ("uw" for underweight).
86283a72a5SPetr Machata * Reclaimed buckets will be given to entries in this list.
87283a72a5SPetr Machata */
88283a72a5SPetr Machata struct list_head uw_nh_entries;
89283a72a5SPetr Machata unsigned long unbalanced_since;
90283a72a5SPetr Machata
91283a72a5SPetr Machata u32 idle_timer;
92283a72a5SPetr Machata u32 unbalanced_timer;
93283a72a5SPetr Machata
94283a72a5SPetr Machata u16 num_nh_buckets;
95283a72a5SPetr Machata struct nh_res_bucket nh_buckets[];
96283a72a5SPetr Machata };
97283a72a5SPetr Machata
98430a0491SDavid Ahern struct nh_grp_entry {
99430a0491SDavid Ahern struct nexthop *nh;
100430a0491SDavid Ahern u8 weight;
101b9bae61bSPetr Machata
102b9bae61bSPetr Machata union {
103b9bae61bSPetr Machata struct {
104430a0491SDavid Ahern atomic_t upper_bound;
105de1d1ee3SPetr Machata } hthr;
106283a72a5SPetr Machata struct {
107283a72a5SPetr Machata /* Member on uw_nh_entries. */
108283a72a5SPetr Machata struct list_head uw_nh_entry;
109283a72a5SPetr Machata
110283a72a5SPetr Machata u16 count_buckets;
111283a72a5SPetr Machata u16 wants_buckets;
112283a72a5SPetr Machata } res;
113b9bae61bSPetr Machata };
114430a0491SDavid Ahern
115430a0491SDavid Ahern struct list_head nh_list;
116430a0491SDavid Ahern struct nexthop *nh_parent; /* nexthop of group with this entry */
117430a0491SDavid Ahern };
118430a0491SDavid Ahern
119430a0491SDavid Ahern struct nh_group {
12090f33bffSNikolay Aleksandrov struct nh_group *spare; /* spare group for removals */
121430a0491SDavid Ahern u16 num_nh;
12290e1a9e2SPetr Machata bool is_multipath;
123de1d1ee3SPetr Machata bool hash_threshold;
124283a72a5SPetr Machata bool resilient;
125ce9ac056SDavid Ahern bool fdb_nh;
126430a0491SDavid Ahern bool has_v4;
127283a72a5SPetr Machata
128283a72a5SPetr Machata struct nh_res_table __rcu *res_table;
12997a888c2SGustavo A. R. Silva struct nh_grp_entry nh_entries[];
130430a0491SDavid Ahern };
131430a0491SDavid Ahern
132ab84be7eSDavid Ahern struct nexthop {
133ab84be7eSDavid Ahern struct rb_node rb_node; /* entry on netns rbtree */
1344c7e8084SDavid Ahern struct list_head fi_list; /* v4 entries using nh */
135f88d8ea6SDavid Ahern struct list_head f6i_list; /* v6 entries using nh */
13638428d68SRoopa Prabhu struct list_head fdb_list; /* fdb entries using this nh */
137430a0491SDavid Ahern struct list_head grp_list; /* nh group entries using this nh */
138ab84be7eSDavid Ahern struct net *net;
139ab84be7eSDavid Ahern
140ab84be7eSDavid Ahern u32 id;
141ab84be7eSDavid Ahern
142ab84be7eSDavid Ahern u8 protocol; /* app managing this nh */
143ab84be7eSDavid Ahern u8 nh_flags;
144430a0491SDavid Ahern bool is_group;
145ab84be7eSDavid Ahern
146ab84be7eSDavid Ahern refcount_t refcnt;
147ab84be7eSDavid Ahern struct rcu_head rcu;
148ab84be7eSDavid Ahern
149ab84be7eSDavid Ahern union {
150ab84be7eSDavid Ahern struct nh_info __rcu *nh_info;
151430a0491SDavid Ahern struct nh_group __rcu *nh_grp;
152ab84be7eSDavid Ahern };
153ab84be7eSDavid Ahern };
154ab84be7eSDavid Ahern
1558590ceedSRoopa Prabhu enum nexthop_event_type {
156732d167bSIdo Schimmel NEXTHOP_EVENT_DEL,
157732d167bSIdo Schimmel NEXTHOP_EVENT_REPLACE,
158b8f090d0SIdo Schimmel NEXTHOP_EVENT_RES_TABLE_PRE_REPLACE,
159b8f090d0SIdo Schimmel NEXTHOP_EVENT_BUCKET_REPLACE,
1608590ceedSRoopa Prabhu };
1618590ceedSRoopa Prabhu
16209ad6becSIdo Schimmel enum nh_notifier_info_type {
16309ad6becSIdo Schimmel NH_NOTIFIER_INFO_TYPE_SINGLE,
16409ad6becSIdo Schimmel NH_NOTIFIER_INFO_TYPE_GRP,
165b8f090d0SIdo Schimmel NH_NOTIFIER_INFO_TYPE_RES_TABLE,
166b8f090d0SIdo Schimmel NH_NOTIFIER_INFO_TYPE_RES_BUCKET,
16709ad6becSIdo Schimmel };
16809ad6becSIdo Schimmel
1691c9cac65SIdo Schimmel struct nh_notifier_single_info {
1701c9cac65SIdo Schimmel struct net_device *dev;
1711c9cac65SIdo Schimmel u8 gw_family;
1721c9cac65SIdo Schimmel union {
1731c9cac65SIdo Schimmel __be32 ipv4;
1741c9cac65SIdo Schimmel struct in6_addr ipv6;
1751c9cac65SIdo Schimmel };
1761c9cac65SIdo Schimmel u8 is_reject:1,
1771c9cac65SIdo Schimmel is_fdb:1,
1781c9cac65SIdo Schimmel has_encap:1;
1791c9cac65SIdo Schimmel };
1801c9cac65SIdo Schimmel
1811c9cac65SIdo Schimmel struct nh_notifier_grp_entry_info {
1821c9cac65SIdo Schimmel u8 weight;
1831c9cac65SIdo Schimmel u32 id;
1841c9cac65SIdo Schimmel struct nh_notifier_single_info nh;
1851c9cac65SIdo Schimmel };
1861c9cac65SIdo Schimmel
1871c9cac65SIdo Schimmel struct nh_notifier_grp_info {
1881c9cac65SIdo Schimmel u16 num_nh;
1891c9cac65SIdo Schimmel bool is_fdb;
1901c9cac65SIdo Schimmel struct nh_notifier_grp_entry_info nh_entries[];
1911c9cac65SIdo Schimmel };
1921c9cac65SIdo Schimmel
193b8f090d0SIdo Schimmel struct nh_notifier_res_bucket_info {
194b8f090d0SIdo Schimmel u16 bucket_index;
195b8f090d0SIdo Schimmel unsigned int idle_timer_ms;
196b8f090d0SIdo Schimmel bool force;
197b8f090d0SIdo Schimmel struct nh_notifier_single_info old_nh;
198b8f090d0SIdo Schimmel struct nh_notifier_single_info new_nh;
199b8f090d0SIdo Schimmel };
200b8f090d0SIdo Schimmel
201b8f090d0SIdo Schimmel struct nh_notifier_res_table_info {
202b8f090d0SIdo Schimmel u16 num_nh_buckets;
203b8f090d0SIdo Schimmel struct nh_notifier_single_info nhs[];
204b8f090d0SIdo Schimmel };
205b8f090d0SIdo Schimmel
2061c9cac65SIdo Schimmel struct nh_notifier_info {
2071c9cac65SIdo Schimmel struct net *net;
2081c9cac65SIdo Schimmel struct netlink_ext_ack *extack;
2091c9cac65SIdo Schimmel u32 id;
21009ad6becSIdo Schimmel enum nh_notifier_info_type type;
2111c9cac65SIdo Schimmel union {
2121c9cac65SIdo Schimmel struct nh_notifier_single_info *nh;
2131c9cac65SIdo Schimmel struct nh_notifier_grp_info *nh_grp;
214b8f090d0SIdo Schimmel struct nh_notifier_res_table_info *nh_res_table;
215b8f090d0SIdo Schimmel struct nh_notifier_res_bucket_info *nh_res_bucket;
2161c9cac65SIdo Schimmel };
2171c9cac65SIdo Schimmel };
2181c9cac65SIdo Schimmel
219ce7e9c8aSIdo Schimmel int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
220ce7e9c8aSIdo Schimmel struct netlink_ext_ack *extack);
2218590ceedSRoopa Prabhu int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
222e95f2592SIdo Schimmel void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
22356ad5ba3SIdo Schimmel void nexthop_bucket_set_hw_flags(struct net *net, u32 id, u16 bucket_index,
22456ad5ba3SIdo Schimmel bool offload, bool trap);
225cfc15c1dSIdo Schimmel void nexthop_res_grp_activity_update(struct net *net, u32 id, u16 num_buckets,
226cfc15c1dSIdo Schimmel unsigned long *activity);
2278590ceedSRoopa Prabhu
228ab84be7eSDavid Ahern /* caller is holding rcu or rtnl; no reference taken to nexthop */
229ab84be7eSDavid Ahern struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
230ab84be7eSDavid Ahern void nexthop_free_rcu(struct rcu_head *head);
231ab84be7eSDavid Ahern
nexthop_get(struct nexthop * nh)232ab84be7eSDavid Ahern static inline bool nexthop_get(struct nexthop *nh)
233ab84be7eSDavid Ahern {
234ab84be7eSDavid Ahern return refcount_inc_not_zero(&nh->refcnt);
235ab84be7eSDavid Ahern }
236ab84be7eSDavid Ahern
nexthop_put(struct nexthop * nh)237ab84be7eSDavid Ahern static inline void nexthop_put(struct nexthop *nh)
238ab84be7eSDavid Ahern {
239ab84be7eSDavid Ahern if (refcount_dec_and_test(&nh->refcnt))
240ab84be7eSDavid Ahern call_rcu(&nh->rcu, nexthop_free_rcu);
241ab84be7eSDavid Ahern }
242ab84be7eSDavid Ahern
nexthop_cmp(const struct nexthop * nh1,const struct nexthop * nh2)2434c7e8084SDavid Ahern static inline bool nexthop_cmp(const struct nexthop *nh1,
2444c7e8084SDavid Ahern const struct nexthop *nh2)
2454c7e8084SDavid Ahern {
2464c7e8084SDavid Ahern return nh1 == nh2;
2474c7e8084SDavid Ahern }
2484c7e8084SDavid Ahern
nexthop_is_fdb(const struct nexthop * nh)249ce9ac056SDavid Ahern static inline bool nexthop_is_fdb(const struct nexthop *nh)
250ce9ac056SDavid Ahern {
251ce9ac056SDavid Ahern if (nh->is_group) {
252ce9ac056SDavid Ahern const struct nh_group *nh_grp;
253ce9ac056SDavid Ahern
254ce9ac056SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
255ce9ac056SDavid Ahern return nh_grp->fdb_nh;
256ce9ac056SDavid Ahern } else {
257ce9ac056SDavid Ahern const struct nh_info *nhi;
258ce9ac056SDavid Ahern
259ce9ac056SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info);
260ce9ac056SDavid Ahern return nhi->fdb_nh;
261ce9ac056SDavid Ahern }
262ce9ac056SDavid Ahern }
263ce9ac056SDavid Ahern
nexthop_has_v4(const struct nexthop * nh)26450cb8769SDavid Ahern static inline bool nexthop_has_v4(const struct nexthop *nh)
26550cb8769SDavid Ahern {
26650cb8769SDavid Ahern if (nh->is_group) {
26750cb8769SDavid Ahern struct nh_group *nh_grp;
26850cb8769SDavid Ahern
26950cb8769SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
27050cb8769SDavid Ahern return nh_grp->has_v4;
27150cb8769SDavid Ahern }
27250cb8769SDavid Ahern return false;
27350cb8769SDavid Ahern }
27450cb8769SDavid Ahern
nexthop_is_multipath(const struct nexthop * nh)275430a0491SDavid Ahern static inline bool nexthop_is_multipath(const struct nexthop *nh)
276430a0491SDavid Ahern {
277430a0491SDavid Ahern if (nh->is_group) {
278430a0491SDavid Ahern struct nh_group *nh_grp;
279430a0491SDavid Ahern
280430a0491SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
28190e1a9e2SPetr Machata return nh_grp->is_multipath;
282430a0491SDavid Ahern }
283430a0491SDavid Ahern return false;
284430a0491SDavid Ahern }
285430a0491SDavid Ahern
286430a0491SDavid Ahern struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
287430a0491SDavid Ahern
nexthop_num_path(const struct nexthop * nh)288430a0491SDavid Ahern static inline unsigned int nexthop_num_path(const struct nexthop *nh)
289430a0491SDavid Ahern {
290430a0491SDavid Ahern unsigned int rc = 1;
291430a0491SDavid Ahern
2920b5e2e39SDavid Ahern if (nh->is_group) {
293430a0491SDavid Ahern struct nh_group *nh_grp;
294430a0491SDavid Ahern
295430a0491SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
29690e1a9e2SPetr Machata if (nh_grp->is_multipath)
297430a0491SDavid Ahern rc = nh_grp->num_nh;
298430a0491SDavid Ahern }
299430a0491SDavid Ahern
300430a0491SDavid Ahern return rc;
301430a0491SDavid Ahern }
302430a0491SDavid Ahern
303430a0491SDavid Ahern static inline
nexthop_mpath_select(const struct nh_group * nhg,int nhsel)3040b5e2e39SDavid Ahern struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
305430a0491SDavid Ahern {
306430a0491SDavid Ahern /* for_nexthops macros in fib_semantics.c grabs a pointer to
307430a0491SDavid Ahern * the nexthop before checking nhsel
308430a0491SDavid Ahern */
3095270041dSDan Carpenter if (nhsel >= nhg->num_nh)
310430a0491SDavid Ahern return NULL;
311430a0491SDavid Ahern
312430a0491SDavid Ahern return nhg->nh_entries[nhsel].nh;
313430a0491SDavid Ahern }
314430a0491SDavid Ahern
315430a0491SDavid Ahern static inline
nexthop_mpath_fill_node(struct sk_buff * skb,struct nexthop * nh,u8 rt_family)3167bdf4de1SDonald Sharp int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
3177bdf4de1SDonald Sharp u8 rt_family)
318430a0491SDavid Ahern {
319430a0491SDavid Ahern struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
320430a0491SDavid Ahern int i;
321430a0491SDavid Ahern
322430a0491SDavid Ahern for (i = 0; i < nhg->num_nh; i++) {
323430a0491SDavid Ahern struct nexthop *nhe = nhg->nh_entries[i].nh;
324430a0491SDavid Ahern struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
325430a0491SDavid Ahern struct fib_nh_common *nhc = &nhi->fib_nhc;
326430a0491SDavid Ahern int weight = nhg->nh_entries[i].weight;
327430a0491SDavid Ahern
328*597aa16cSXiao Liang if (fib_add_nexthop(skb, nhc, weight, rt_family, 0) < 0)
329430a0491SDavid Ahern return -EMSGSIZE;
330430a0491SDavid Ahern }
331430a0491SDavid Ahern
332430a0491SDavid Ahern return 0;
333430a0491SDavid Ahern }
334430a0491SDavid Ahern
335ab84be7eSDavid Ahern /* called with rcu lock */
nexthop_is_blackhole(const struct nexthop * nh)336ab84be7eSDavid Ahern static inline bool nexthop_is_blackhole(const struct nexthop *nh)
337ab84be7eSDavid Ahern {
338ab84be7eSDavid Ahern const struct nh_info *nhi;
339ab84be7eSDavid Ahern
3400b5e2e39SDavid Ahern if (nh->is_group) {
3410b5e2e39SDavid Ahern struct nh_group *nh_grp;
3420b5e2e39SDavid Ahern
3430b5e2e39SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
3440b5e2e39SDavid Ahern if (nh_grp->num_nh > 1)
345430a0491SDavid Ahern return false;
3460b5e2e39SDavid Ahern
3470b5e2e39SDavid Ahern nh = nh_grp->nh_entries[0].nh;
348430a0491SDavid Ahern }
349430a0491SDavid Ahern
350430a0491SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info);
351ab84be7eSDavid Ahern return nhi->reject_nh;
352ab84be7eSDavid Ahern }
3535481d73fSDavid Ahern
nexthop_path_fib_result(struct fib_result * res,int hash)3544c7e8084SDavid Ahern static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
3554c7e8084SDavid Ahern {
3564c7e8084SDavid Ahern struct nh_info *nhi;
3574c7e8084SDavid Ahern struct nexthop *nh;
3584c7e8084SDavid Ahern
3594c7e8084SDavid Ahern nh = nexthop_select_path(res->fi->nh, hash);
3604c7e8084SDavid Ahern nhi = rcu_dereference(nh->nh_info);
3614c7e8084SDavid Ahern res->nhc = &nhi->fib_nhc;
3624c7e8084SDavid Ahern }
3634c7e8084SDavid Ahern
3644c7e8084SDavid Ahern /* called with rcu read lock or rtnl held */
3654c7e8084SDavid Ahern static inline
nexthop_fib_nhc(struct nexthop * nh,int nhsel)3664c7e8084SDavid Ahern struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
3674c7e8084SDavid Ahern {
3684c7e8084SDavid Ahern struct nh_info *nhi;
3694c7e8084SDavid Ahern
3704c7e8084SDavid Ahern BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
3714c7e8084SDavid Ahern BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
3724c7e8084SDavid Ahern
3730b5e2e39SDavid Ahern if (nh->is_group) {
3740b5e2e39SDavid Ahern struct nh_group *nh_grp;
3750b5e2e39SDavid Ahern
3760b5e2e39SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
37790e1a9e2SPetr Machata if (nh_grp->is_multipath) {
3780b5e2e39SDavid Ahern nh = nexthop_mpath_select(nh_grp, nhsel);
3794c7e8084SDavid Ahern if (!nh)
3804c7e8084SDavid Ahern return NULL;
3814c7e8084SDavid Ahern }
3820b5e2e39SDavid Ahern }
3834c7e8084SDavid Ahern
3844c7e8084SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info);
3854c7e8084SDavid Ahern return &nhi->fib_nhc;
3864c7e8084SDavid Ahern }
3874c7e8084SDavid Ahern
388af7888adSDavid Ahern /* called from fib_table_lookup with rcu_lock */
389af7888adSDavid Ahern static inline
nexthop_get_nhc_lookup(const struct nexthop * nh,int fib_flags,const struct flowi4 * flp,int * nhsel)390af7888adSDavid Ahern struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
391af7888adSDavid Ahern int fib_flags,
392af7888adSDavid Ahern const struct flowi4 *flp,
393af7888adSDavid Ahern int *nhsel)
394af7888adSDavid Ahern {
395af7888adSDavid Ahern struct nh_info *nhi;
396af7888adSDavid Ahern
397af7888adSDavid Ahern if (nh->is_group) {
398af7888adSDavid Ahern struct nh_group *nhg = rcu_dereference(nh->nh_grp);
399af7888adSDavid Ahern int i;
400af7888adSDavid Ahern
401af7888adSDavid Ahern for (i = 0; i < nhg->num_nh; i++) {
402af7888adSDavid Ahern struct nexthop *nhe = nhg->nh_entries[i].nh;
403af7888adSDavid Ahern
404af7888adSDavid Ahern nhi = rcu_dereference(nhe->nh_info);
405af7888adSDavid Ahern if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
406af7888adSDavid Ahern *nhsel = i;
407af7888adSDavid Ahern return &nhi->fib_nhc;
408af7888adSDavid Ahern }
409af7888adSDavid Ahern }
410af7888adSDavid Ahern } else {
411af7888adSDavid Ahern nhi = rcu_dereference(nh->nh_info);
412af7888adSDavid Ahern if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
413af7888adSDavid Ahern *nhsel = 0;
414af7888adSDavid Ahern return &nhi->fib_nhc;
415af7888adSDavid Ahern }
416af7888adSDavid Ahern }
417af7888adSDavid Ahern
418af7888adSDavid Ahern return NULL;
419af7888adSDavid Ahern }
420af7888adSDavid Ahern
nexthop_uses_dev(const struct nexthop * nh,const struct net_device * dev)4211fd1c768SDavid Ahern static inline bool nexthop_uses_dev(const struct nexthop *nh,
4221fd1c768SDavid Ahern const struct net_device *dev)
4231fd1c768SDavid Ahern {
4241fd1c768SDavid Ahern struct nh_info *nhi;
4251fd1c768SDavid Ahern
4261fd1c768SDavid Ahern if (nh->is_group) {
4271fd1c768SDavid Ahern struct nh_group *nhg = rcu_dereference(nh->nh_grp);
4281fd1c768SDavid Ahern int i;
4291fd1c768SDavid Ahern
4301fd1c768SDavid Ahern for (i = 0; i < nhg->num_nh; i++) {
4311fd1c768SDavid Ahern struct nexthop *nhe = nhg->nh_entries[i].nh;
4321fd1c768SDavid Ahern
4331fd1c768SDavid Ahern nhi = rcu_dereference(nhe->nh_info);
4341fd1c768SDavid Ahern if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
4351fd1c768SDavid Ahern return true;
4361fd1c768SDavid Ahern }
4371fd1c768SDavid Ahern } else {
4381fd1c768SDavid Ahern nhi = rcu_dereference(nh->nh_info);
4391fd1c768SDavid Ahern if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
4401fd1c768SDavid Ahern return true;
4411fd1c768SDavid Ahern }
4421fd1c768SDavid Ahern
4431fd1c768SDavid Ahern return false;
4441fd1c768SDavid Ahern }
4451fd1c768SDavid Ahern
fib_info_num_path(const struct fib_info * fi)4465481d73fSDavid Ahern static inline unsigned int fib_info_num_path(const struct fib_info *fi)
4475481d73fSDavid Ahern {
4484c7e8084SDavid Ahern if (unlikely(fi->nh))
4494c7e8084SDavid Ahern return nexthop_num_path(fi->nh);
4504c7e8084SDavid Ahern
4515481d73fSDavid Ahern return fi->fib_nhs;
4525481d73fSDavid Ahern }
4535481d73fSDavid Ahern
4544c7e8084SDavid Ahern int fib_check_nexthop(struct nexthop *nh, u8 scope,
4554c7e8084SDavid Ahern struct netlink_ext_ack *extack);
4564c7e8084SDavid Ahern
fib_info_nhc(struct fib_info * fi,int nhsel)4575481d73fSDavid Ahern static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
4585481d73fSDavid Ahern {
4594c7e8084SDavid Ahern if (unlikely(fi->nh))
4604c7e8084SDavid Ahern return nexthop_fib_nhc(fi->nh, nhsel);
4614c7e8084SDavid Ahern
4625481d73fSDavid Ahern return &fi->fib_nh[nhsel].nh_common;
4635481d73fSDavid Ahern }
4645481d73fSDavid Ahern
4654c7e8084SDavid Ahern /* only used when fib_nh is built into fib_info */
fib_info_nh(struct fib_info * fi,int nhsel)4665481d73fSDavid Ahern static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
4675481d73fSDavid Ahern {
4684c7e8084SDavid Ahern WARN_ON(fi->nh);
4694c7e8084SDavid Ahern
4705481d73fSDavid Ahern return &fi->fib_nh[nhsel];
4715481d73fSDavid Ahern }
472f88d8ea6SDavid Ahern
473f88d8ea6SDavid Ahern /*
474f88d8ea6SDavid Ahern * IPv6 variants
475f88d8ea6SDavid Ahern */
476f88d8ea6SDavid Ahern int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
477f88d8ea6SDavid Ahern struct netlink_ext_ack *extack);
478f88d8ea6SDavid Ahern
47928259bacSWei Wang /* Caller should either hold rcu_read_lock(), or RTNL. */
nexthop_fib6_nh(struct nexthop * nh)480f88d8ea6SDavid Ahern static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
481f88d8ea6SDavid Ahern {
482f88d8ea6SDavid Ahern struct nh_info *nhi;
483f88d8ea6SDavid Ahern
4840b5e2e39SDavid Ahern if (nh->is_group) {
4850b5e2e39SDavid Ahern struct nh_group *nh_grp;
4860b5e2e39SDavid Ahern
4870b5e2e39SDavid Ahern nh_grp = rcu_dereference_rtnl(nh->nh_grp);
4880b5e2e39SDavid Ahern nh = nexthop_mpath_select(nh_grp, 0);
489f88d8ea6SDavid Ahern if (!nh)
490f88d8ea6SDavid Ahern return NULL;
491f88d8ea6SDavid Ahern }
492f88d8ea6SDavid Ahern
493f88d8ea6SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info);
494f88d8ea6SDavid Ahern if (nhi->family == AF_INET6)
495f88d8ea6SDavid Ahern return &nhi->fib6_nh;
496f88d8ea6SDavid Ahern
497f88d8ea6SDavid Ahern return NULL;
498f88d8ea6SDavid Ahern }
499f88d8ea6SDavid Ahern
fib6_info_nh_dev(struct fib6_info * f6i)500f88d8ea6SDavid Ahern static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
501f88d8ea6SDavid Ahern {
502f88d8ea6SDavid Ahern struct fib6_nh *fib6_nh;
503f88d8ea6SDavid Ahern
504f88d8ea6SDavid Ahern fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
505f88d8ea6SDavid Ahern return fib6_nh->fib_nh_dev;
506f88d8ea6SDavid Ahern }
507f88d8ea6SDavid Ahern
nexthop_path_fib6_result(struct fib6_result * res,int hash)508f88d8ea6SDavid Ahern static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
509f88d8ea6SDavid Ahern {
510f88d8ea6SDavid Ahern struct nexthop *nh = res->f6i->nh;
511f88d8ea6SDavid Ahern struct nh_info *nhi;
512f88d8ea6SDavid Ahern
513f88d8ea6SDavid Ahern nh = nexthop_select_path(nh, hash);
514f88d8ea6SDavid Ahern
515f88d8ea6SDavid Ahern nhi = rcu_dereference_rtnl(nh->nh_info);
516f88d8ea6SDavid Ahern if (nhi->reject_nh) {
517f88d8ea6SDavid Ahern res->fib6_type = RTN_BLACKHOLE;
518f88d8ea6SDavid Ahern res->fib6_flags |= RTF_REJECT;
519f88d8ea6SDavid Ahern res->nh = nexthop_fib6_nh(nh);
520f88d8ea6SDavid Ahern } else {
521f88d8ea6SDavid Ahern res->nh = &nhi->fib6_nh;
522f88d8ea6SDavid Ahern }
523f88d8ea6SDavid Ahern }
524f88c9aa1SDavid Ahern
525f88c9aa1SDavid Ahern int nexthop_for_each_fib6_nh(struct nexthop *nh,
526f88c9aa1SDavid Ahern int (*cb)(struct fib6_nh *nh, void *arg),
527f88c9aa1SDavid Ahern void *arg);
52838428d68SRoopa Prabhu
nexthop_get_family(struct nexthop * nh)52938428d68SRoopa Prabhu static inline int nexthop_get_family(struct nexthop *nh)
53038428d68SRoopa Prabhu {
53138428d68SRoopa Prabhu struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
53238428d68SRoopa Prabhu
53338428d68SRoopa Prabhu return nhi->family;
53438428d68SRoopa Prabhu }
53538428d68SRoopa Prabhu
53638428d68SRoopa Prabhu static inline
nexthop_fdb_nhc(struct nexthop * nh)53738428d68SRoopa Prabhu struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
53838428d68SRoopa Prabhu {
53938428d68SRoopa Prabhu struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
54038428d68SRoopa Prabhu
54138428d68SRoopa Prabhu return &nhi->fib_nhc;
54238428d68SRoopa Prabhu }
54338428d68SRoopa Prabhu
nexthop_path_fdb_result(struct nexthop * nh,int hash)54438428d68SRoopa Prabhu static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
54538428d68SRoopa Prabhu int hash)
54638428d68SRoopa Prabhu {
54738428d68SRoopa Prabhu struct nh_info *nhi;
54838428d68SRoopa Prabhu struct nexthop *nhp;
54938428d68SRoopa Prabhu
55038428d68SRoopa Prabhu nhp = nexthop_select_path(nh, hash);
55138428d68SRoopa Prabhu if (unlikely(!nhp))
55238428d68SRoopa Prabhu return NULL;
55338428d68SRoopa Prabhu nhi = rcu_dereference(nhp->nh_info);
55438428d68SRoopa Prabhu return &nhi->fib_nhc;
55538428d68SRoopa Prabhu }
556ab84be7eSDavid Ahern #endif
557