WIP FPC-III support
[linux/fpc-iii.git] / include / net / nexthop.h
blob226930d66b637d1ed2e30bc765d5c69d8f8b398b
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Generic nexthop implementation
5 * Copyright (c) 2017-19 Cumulus Networks
6 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
7 */
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
22 struct nexthop;
24 struct nh_config {
25 u32 nh_id;
27 u8 nh_family;
28 u8 nh_protocol;
29 u8 nh_blackhole;
30 u8 nh_fdb;
31 u32 nh_flags;
33 int nh_ifindex;
34 struct net_device *dev;
36 union {
37 __be32 ipv4;
38 struct in6_addr ipv6;
39 } gw;
41 struct nlattr *nh_grp;
42 u16 nh_grp_type;
44 struct nlattr *nh_encap;
45 u16 nh_encap_type;
47 u32 nlflags;
48 struct nl_info nlinfo;
51 struct nh_info {
52 struct hlist_node dev_hash; /* entry on netns devhash */
53 struct nexthop *nh_parent;
55 u8 family;
56 bool reject_nh;
57 bool fdb_nh;
59 union {
60 struct fib_nh_common fib_nhc;
61 struct fib_nh fib_nh;
62 struct fib6_nh fib6_nh;
66 struct nh_grp_entry {
67 struct nexthop *nh;
68 u8 weight;
69 atomic_t upper_bound;
71 struct list_head nh_list;
72 struct nexthop *nh_parent; /* nexthop of group with this entry */
75 struct nh_group {
76 struct nh_group *spare; /* spare group for removals */
77 u16 num_nh;
78 bool mpath;
79 bool fdb_nh;
80 bool has_v4;
81 struct nh_grp_entry nh_entries[];
84 struct nexthop {
85 struct rb_node rb_node; /* entry on netns rbtree */
86 struct list_head fi_list; /* v4 entries using nh */
87 struct list_head f6i_list; /* v6 entries using nh */
88 struct list_head fdb_list; /* fdb entries using this nh */
89 struct list_head grp_list; /* nh group entries using this nh */
90 struct net *net;
92 u32 id;
94 u8 protocol; /* app managing this nh */
95 u8 nh_flags;
96 bool is_group;
98 refcount_t refcnt;
99 struct rcu_head rcu;
101 union {
102 struct nh_info __rcu *nh_info;
103 struct nh_group __rcu *nh_grp;
107 enum nexthop_event_type {
108 NEXTHOP_EVENT_DEL,
109 NEXTHOP_EVENT_REPLACE,
112 struct nh_notifier_single_info {
113 struct net_device *dev;
114 u8 gw_family;
115 union {
116 __be32 ipv4;
117 struct in6_addr ipv6;
119 u8 is_reject:1,
120 is_fdb:1,
121 has_encap:1;
124 struct nh_notifier_grp_entry_info {
125 u8 weight;
126 u32 id;
127 struct nh_notifier_single_info nh;
130 struct nh_notifier_grp_info {
131 u16 num_nh;
132 bool is_fdb;
133 struct nh_notifier_grp_entry_info nh_entries[];
136 struct nh_notifier_info {
137 struct net *net;
138 struct netlink_ext_ack *extack;
139 u32 id;
140 bool is_grp;
141 union {
142 struct nh_notifier_single_info *nh;
143 struct nh_notifier_grp_info *nh_grp;
147 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
148 struct netlink_ext_ack *extack);
149 int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
150 void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
152 /* caller is holding rcu or rtnl; no reference taken to nexthop */
153 struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
154 void nexthop_free_rcu(struct rcu_head *head);
156 static inline bool nexthop_get(struct nexthop *nh)
158 return refcount_inc_not_zero(&nh->refcnt);
161 static inline void nexthop_put(struct nexthop *nh)
163 if (refcount_dec_and_test(&nh->refcnt))
164 call_rcu(&nh->rcu, nexthop_free_rcu);
167 static inline bool nexthop_cmp(const struct nexthop *nh1,
168 const struct nexthop *nh2)
170 return nh1 == nh2;
173 static inline bool nexthop_is_fdb(const struct nexthop *nh)
175 if (nh->is_group) {
176 const struct nh_group *nh_grp;
178 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
179 return nh_grp->fdb_nh;
180 } else {
181 const struct nh_info *nhi;
183 nhi = rcu_dereference_rtnl(nh->nh_info);
184 return nhi->fdb_nh;
188 static inline bool nexthop_has_v4(const struct nexthop *nh)
190 if (nh->is_group) {
191 struct nh_group *nh_grp;
193 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
194 return nh_grp->has_v4;
196 return false;
199 static inline bool nexthop_is_multipath(const struct nexthop *nh)
201 if (nh->is_group) {
202 struct nh_group *nh_grp;
204 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
205 return nh_grp->mpath;
207 return false;
210 struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
212 static inline unsigned int nexthop_num_path(const struct nexthop *nh)
214 unsigned int rc = 1;
216 if (nh->is_group) {
217 struct nh_group *nh_grp;
219 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
220 if (nh_grp->mpath)
221 rc = nh_grp->num_nh;
224 return rc;
227 static inline
228 struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
230 /* for_nexthops macros in fib_semantics.c grabs a pointer to
231 * the nexthop before checking nhsel
233 if (nhsel >= nhg->num_nh)
234 return NULL;
236 return nhg->nh_entries[nhsel].nh;
239 static inline
240 int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
241 u8 rt_family)
243 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
244 int i;
246 for (i = 0; i < nhg->num_nh; i++) {
247 struct nexthop *nhe = nhg->nh_entries[i].nh;
248 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
249 struct fib_nh_common *nhc = &nhi->fib_nhc;
250 int weight = nhg->nh_entries[i].weight;
252 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
253 return -EMSGSIZE;
256 return 0;
259 /* called with rcu lock */
260 static inline bool nexthop_is_blackhole(const struct nexthop *nh)
262 const struct nh_info *nhi;
264 if (nh->is_group) {
265 struct nh_group *nh_grp;
267 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
268 if (nh_grp->num_nh > 1)
269 return false;
271 nh = nh_grp->nh_entries[0].nh;
274 nhi = rcu_dereference_rtnl(nh->nh_info);
275 return nhi->reject_nh;
278 static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
280 struct nh_info *nhi;
281 struct nexthop *nh;
283 nh = nexthop_select_path(res->fi->nh, hash);
284 nhi = rcu_dereference(nh->nh_info);
285 res->nhc = &nhi->fib_nhc;
288 /* called with rcu read lock or rtnl held */
289 static inline
290 struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
292 struct nh_info *nhi;
294 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
295 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
297 if (nh->is_group) {
298 struct nh_group *nh_grp;
300 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
301 if (nh_grp->mpath) {
302 nh = nexthop_mpath_select(nh_grp, nhsel);
303 if (!nh)
304 return NULL;
308 nhi = rcu_dereference_rtnl(nh->nh_info);
309 return &nhi->fib_nhc;
312 /* called from fib_table_lookup with rcu_lock */
313 static inline
314 struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
315 int fib_flags,
316 const struct flowi4 *flp,
317 int *nhsel)
319 struct nh_info *nhi;
321 if (nh->is_group) {
322 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
323 int i;
325 for (i = 0; i < nhg->num_nh; i++) {
326 struct nexthop *nhe = nhg->nh_entries[i].nh;
328 nhi = rcu_dereference(nhe->nh_info);
329 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
330 *nhsel = i;
331 return &nhi->fib_nhc;
334 } else {
335 nhi = rcu_dereference(nh->nh_info);
336 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
337 *nhsel = 0;
338 return &nhi->fib_nhc;
342 return NULL;
345 static inline bool nexthop_uses_dev(const struct nexthop *nh,
346 const struct net_device *dev)
348 struct nh_info *nhi;
350 if (nh->is_group) {
351 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
352 int i;
354 for (i = 0; i < nhg->num_nh; i++) {
355 struct nexthop *nhe = nhg->nh_entries[i].nh;
357 nhi = rcu_dereference(nhe->nh_info);
358 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
359 return true;
361 } else {
362 nhi = rcu_dereference(nh->nh_info);
363 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
364 return true;
367 return false;
370 static inline unsigned int fib_info_num_path(const struct fib_info *fi)
372 if (unlikely(fi->nh))
373 return nexthop_num_path(fi->nh);
375 return fi->fib_nhs;
378 int fib_check_nexthop(struct nexthop *nh, u8 scope,
379 struct netlink_ext_ack *extack);
381 static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
383 if (unlikely(fi->nh))
384 return nexthop_fib_nhc(fi->nh, nhsel);
386 return &fi->fib_nh[nhsel].nh_common;
389 /* only used when fib_nh is built into fib_info */
390 static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
392 WARN_ON(fi->nh);
394 return &fi->fib_nh[nhsel];
398 * IPv6 variants
400 int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
401 struct netlink_ext_ack *extack);
403 static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
405 struct nh_info *nhi;
407 if (nh->is_group) {
408 struct nh_group *nh_grp;
410 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
411 nh = nexthop_mpath_select(nh_grp, 0);
412 if (!nh)
413 return NULL;
416 nhi = rcu_dereference_rtnl(nh->nh_info);
417 if (nhi->family == AF_INET6)
418 return &nhi->fib6_nh;
420 return NULL;
423 static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
425 struct fib6_nh *fib6_nh;
427 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
428 return fib6_nh->fib_nh_dev;
431 static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
433 struct nexthop *nh = res->f6i->nh;
434 struct nh_info *nhi;
436 nh = nexthop_select_path(nh, hash);
438 nhi = rcu_dereference_rtnl(nh->nh_info);
439 if (nhi->reject_nh) {
440 res->fib6_type = RTN_BLACKHOLE;
441 res->fib6_flags |= RTF_REJECT;
442 res->nh = nexthop_fib6_nh(nh);
443 } else {
444 res->nh = &nhi->fib6_nh;
448 int nexthop_for_each_fib6_nh(struct nexthop *nh,
449 int (*cb)(struct fib6_nh *nh, void *arg),
450 void *arg);
452 static inline int nexthop_get_family(struct nexthop *nh)
454 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
456 return nhi->family;
459 static inline
460 struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
462 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
464 return &nhi->fib_nhc;
467 static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
468 int hash)
470 struct nh_info *nhi;
471 struct nexthop *nhp;
473 nhp = nexthop_select_path(nh, hash);
474 if (unlikely(!nhp))
475 return NULL;
476 nhi = rcu_dereference(nhp->nh_info);
477 return &nhi->fib_nhc;
479 #endif