nexthop: Expand nexthop_is_multipath in a few places
[linux-block.git] / include / net / nexthop.h
CommitLineData
ab84be7e
DA
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Generic nexthop implementation
4 *
5 * Copyright (c) 2017-19 Cumulus Networks
6 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
7 */
8
9#ifndef __LINUX_NEXTHOP_H
10#define __LINUX_NEXTHOP_H
11
12#include <linux/netdevice.h>
f88d8ea6 13#include <linux/route.h>
ab84be7e
DA
14#include <linux/types.h>
15#include <net/ip_fib.h>
53010f99 16#include <net/ip6_fib.h>
ab84be7e
DA
17#include <net/netlink.h>
18
19#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
20
21struct nexthop;
22
23struct nh_config {
24 u32 nh_id;
25
26 u8 nh_family;
27 u8 nh_protocol;
28 u8 nh_blackhole;
29 u32 nh_flags;
30
31 int nh_ifindex;
32 struct net_device *dev;
33
597cfe4f
DA
34 union {
35 __be32 ipv4;
53010f99 36 struct in6_addr ipv6;
597cfe4f
DA
37 } gw;
38
430a0491
DA
39 struct nlattr *nh_grp;
40 u16 nh_grp_type;
41
b513bd03
DA
42 struct nlattr *nh_encap;
43 u16 nh_encap_type;
44
ab84be7e
DA
45 u32 nlflags;
46 struct nl_info nlinfo;
47};
48
49struct nh_info {
50 struct hlist_node dev_hash; /* entry on netns devhash */
51 struct nexthop *nh_parent;
52
53 u8 family;
54 bool reject_nh;
55
56 union {
57 struct fib_nh_common fib_nhc;
597cfe4f 58 struct fib_nh fib_nh;
53010f99 59 struct fib6_nh fib6_nh;
ab84be7e
DA
60 };
61};
62
430a0491
DA
63struct nh_grp_entry {
64 struct nexthop *nh;
65 u8 weight;
66 atomic_t upper_bound;
67
68 struct list_head nh_list;
69 struct nexthop *nh_parent; /* nexthop of group with this entry */
70};
71
72struct nh_group {
90f33bff 73 struct nh_group *spare; /* spare group for removals */
430a0491
DA
74 u16 num_nh;
75 bool mpath;
76 bool has_v4;
97a888c2 77 struct nh_grp_entry nh_entries[];
430a0491
DA
78};
79
ab84be7e
DA
80struct nexthop {
81 struct rb_node rb_node; /* entry on netns rbtree */
4c7e8084 82 struct list_head fi_list; /* v4 entries using nh */
f88d8ea6 83 struct list_head f6i_list; /* v6 entries using nh */
430a0491 84 struct list_head grp_list; /* nh group entries using this nh */
ab84be7e
DA
85 struct net *net;
86
87 u32 id;
88
89 u8 protocol; /* app managing this nh */
90 u8 nh_flags;
430a0491 91 bool is_group;
ab84be7e
DA
92
93 refcount_t refcnt;
94 struct rcu_head rcu;
95
96 union {
97 struct nh_info __rcu *nh_info;
430a0491 98 struct nh_group __rcu *nh_grp;
ab84be7e
DA
99 };
100};
101
102/* caller is holding rcu or rtnl; no reference taken to nexthop */
103struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
104void nexthop_free_rcu(struct rcu_head *head);
105
106static inline bool nexthop_get(struct nexthop *nh)
107{
108 return refcount_inc_not_zero(&nh->refcnt);
109}
110
111static inline void nexthop_put(struct nexthop *nh)
112{
113 if (refcount_dec_and_test(&nh->refcnt))
114 call_rcu(&nh->rcu, nexthop_free_rcu);
115}
116
4c7e8084
DA
117static inline bool nexthop_cmp(const struct nexthop *nh1,
118 const struct nexthop *nh2)
119{
120 return nh1 == nh2;
121}
122
430a0491
DA
123static inline bool nexthop_is_multipath(const struct nexthop *nh)
124{
125 if (nh->is_group) {
126 struct nh_group *nh_grp;
127
128 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
129 return nh_grp->mpath;
130 }
131 return false;
132}
133
134struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
135
136static inline unsigned int nexthop_num_path(const struct nexthop *nh)
137{
138 unsigned int rc = 1;
139
0b5e2e39 140 if (nh->is_group) {
430a0491
DA
141 struct nh_group *nh_grp;
142
143 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0b5e2e39
DA
144 if (nh_grp->mpath)
145 rc = nh_grp->num_nh;
430a0491
DA
146 }
147
148 return rc;
149}
150
151static inline
0b5e2e39 152struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
430a0491 153{
430a0491
DA
154 /* for_nexthops macros in fib_semantics.c grabs a pointer to
155 * the nexthop before checking nhsel
156 */
5270041d 157 if (nhsel >= nhg->num_nh)
430a0491
DA
158 return NULL;
159
160 return nhg->nh_entries[nhsel].nh;
161}
162
163static inline
7bdf4de1
DS
164int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
165 u8 rt_family)
430a0491
DA
166{
167 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
168 int i;
169
170 for (i = 0; i < nhg->num_nh; i++) {
171 struct nexthop *nhe = nhg->nh_entries[i].nh;
172 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
173 struct fib_nh_common *nhc = &nhi->fib_nhc;
174 int weight = nhg->nh_entries[i].weight;
175
7bdf4de1 176 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
430a0491
DA
177 return -EMSGSIZE;
178 }
179
180 return 0;
181}
182
ab84be7e
DA
183/* called with rcu lock */
184static inline bool nexthop_is_blackhole(const struct nexthop *nh)
185{
186 const struct nh_info *nhi;
187
0b5e2e39
DA
188 if (nh->is_group) {
189 struct nh_group *nh_grp;
190
191 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
192 if (nh_grp->num_nh > 1)
430a0491 193 return false;
0b5e2e39
DA
194
195 nh = nh_grp->nh_entries[0].nh;
430a0491
DA
196 }
197
198 nhi = rcu_dereference_rtnl(nh->nh_info);
ab84be7e
DA
199 return nhi->reject_nh;
200}
5481d73f 201
4c7e8084
DA
202static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
203{
204 struct nh_info *nhi;
205 struct nexthop *nh;
206
207 nh = nexthop_select_path(res->fi->nh, hash);
208 nhi = rcu_dereference(nh->nh_info);
209 res->nhc = &nhi->fib_nhc;
210}
211
212/* called with rcu read lock or rtnl held */
213static inline
214struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
215{
216 struct nh_info *nhi;
217
218 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
219 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
220
0b5e2e39
DA
221 if (nh->is_group) {
222 struct nh_group *nh_grp;
223
224 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
225 if (nh_grp->mpath) {
226 nh = nexthop_mpath_select(nh_grp, nhsel);
227 if (!nh)
228 return NULL;
229 }
4c7e8084
DA
230 }
231
232 nhi = rcu_dereference_rtnl(nh->nh_info);
233 return &nhi->fib_nhc;
234}
235
5481d73f
DA
236static inline unsigned int fib_info_num_path(const struct fib_info *fi)
237{
4c7e8084
DA
238 if (unlikely(fi->nh))
239 return nexthop_num_path(fi->nh);
240
5481d73f
DA
241 return fi->fib_nhs;
242}
243
4c7e8084
DA
244int fib_check_nexthop(struct nexthop *nh, u8 scope,
245 struct netlink_ext_ack *extack);
246
5481d73f
DA
247static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
248{
4c7e8084
DA
249 if (unlikely(fi->nh))
250 return nexthop_fib_nhc(fi->nh, nhsel);
251
5481d73f
DA
252 return &fi->fib_nh[nhsel].nh_common;
253}
254
4c7e8084 255/* only used when fib_nh is built into fib_info */
5481d73f
DA
256static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
257{
4c7e8084
DA
258 WARN_ON(fi->nh);
259
5481d73f
DA
260 return &fi->fib_nh[nhsel];
261}
f88d8ea6
DA
262
263/*
264 * IPv6 variants
265 */
266int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
267 struct netlink_ext_ack *extack);
268
269static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
270{
271 struct nh_info *nhi;
272
0b5e2e39
DA
273 if (nh->is_group) {
274 struct nh_group *nh_grp;
275
276 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
277 nh = nexthop_mpath_select(nh_grp, 0);
f88d8ea6
DA
278 if (!nh)
279 return NULL;
280 }
281
282 nhi = rcu_dereference_rtnl(nh->nh_info);
283 if (nhi->family == AF_INET6)
284 return &nhi->fib6_nh;
285
286 return NULL;
287}
288
289static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
290{
291 struct fib6_nh *fib6_nh;
292
293 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
294 return fib6_nh->fib_nh_dev;
295}
296
297static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
298{
299 struct nexthop *nh = res->f6i->nh;
300 struct nh_info *nhi;
301
302 nh = nexthop_select_path(nh, hash);
303
304 nhi = rcu_dereference_rtnl(nh->nh_info);
305 if (nhi->reject_nh) {
306 res->fib6_type = RTN_BLACKHOLE;
307 res->fib6_flags |= RTF_REJECT;
308 res->nh = nexthop_fib6_nh(nh);
309 } else {
310 res->nh = &nhi->fib6_nh;
311 }
312}
f88c9aa1
DA
313
314int nexthop_for_each_fib6_nh(struct nexthop *nh,
315 int (*cb)(struct fib6_nh *nh, void *arg),
316 void *arg);
ab84be7e 317#endif