nexthop: Use enum to encode notification type
[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>
8590ceed 13#include <linux/notifier.h>
f88d8ea6 14#include <linux/route.h>
ab84be7e
DA
15#include <linux/types.h>
16#include <net/ip_fib.h>
53010f99 17#include <net/ip6_fib.h>
ab84be7e
DA
18#include <net/netlink.h>
19
20#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
21
22struct nexthop;
23
24struct nh_config {
25 u32 nh_id;
26
27 u8 nh_family;
28 u8 nh_protocol;
29 u8 nh_blackhole;
38428d68 30 u8 nh_fdb;
ab84be7e
DA
31 u32 nh_flags;
32
33 int nh_ifindex;
34 struct net_device *dev;
35
597cfe4f
DA
36 union {
37 __be32 ipv4;
53010f99 38 struct in6_addr ipv6;
597cfe4f
DA
39 } gw;
40
430a0491
DA
41 struct nlattr *nh_grp;
42 u16 nh_grp_type;
43
b513bd03
DA
44 struct nlattr *nh_encap;
45 u16 nh_encap_type;
46
ab84be7e
DA
47 u32 nlflags;
48 struct nl_info nlinfo;
49};
50
51struct nh_info {
52 struct hlist_node dev_hash; /* entry on netns devhash */
53 struct nexthop *nh_parent;
54
55 u8 family;
56 bool reject_nh;
38428d68 57 bool fdb_nh;
ab84be7e
DA
58
59 union {
60 struct fib_nh_common fib_nhc;
597cfe4f 61 struct fib_nh fib_nh;
53010f99 62 struct fib6_nh fib6_nh;
ab84be7e
DA
63 };
64};
65
430a0491
DA
66struct nh_grp_entry {
67 struct nexthop *nh;
68 u8 weight;
b9bae61b
PM
69
70 union {
71 struct {
72 atomic_t upper_bound;
73 } mpath;
74 };
430a0491
DA
75
76 struct list_head nh_list;
77 struct nexthop *nh_parent; /* nexthop of group with this entry */
78};
79
80struct nh_group {
90f33bff 81 struct nh_group *spare; /* spare group for removals */
430a0491
DA
82 u16 num_nh;
83 bool mpath;
ce9ac056 84 bool fdb_nh;
430a0491 85 bool has_v4;
97a888c2 86 struct nh_grp_entry nh_entries[];
430a0491
DA
87};
88
ab84be7e
DA
89struct nexthop {
90 struct rb_node rb_node; /* entry on netns rbtree */
4c7e8084 91 struct list_head fi_list; /* v4 entries using nh */
f88d8ea6 92 struct list_head f6i_list; /* v6 entries using nh */
38428d68 93 struct list_head fdb_list; /* fdb entries using this nh */
430a0491 94 struct list_head grp_list; /* nh group entries using this nh */
ab84be7e
DA
95 struct net *net;
96
97 u32 id;
98
99 u8 protocol; /* app managing this nh */
100 u8 nh_flags;
430a0491 101 bool is_group;
ab84be7e
DA
102
103 refcount_t refcnt;
104 struct rcu_head rcu;
105
106 union {
107 struct nh_info __rcu *nh_info;
430a0491 108 struct nh_group __rcu *nh_grp;
ab84be7e
DA
109 };
110};
111
8590ceed 112enum nexthop_event_type {
732d167b
IS
113 NEXTHOP_EVENT_DEL,
114 NEXTHOP_EVENT_REPLACE,
8590ceed
RP
115};
116
09ad6bec
IS
117enum nh_notifier_info_type {
118 NH_NOTIFIER_INFO_TYPE_SINGLE,
119 NH_NOTIFIER_INFO_TYPE_GRP,
120};
121
1c9cac65
IS
122struct nh_notifier_single_info {
123 struct net_device *dev;
124 u8 gw_family;
125 union {
126 __be32 ipv4;
127 struct in6_addr ipv6;
128 };
129 u8 is_reject:1,
130 is_fdb:1,
131 has_encap:1;
132};
133
134struct nh_notifier_grp_entry_info {
135 u8 weight;
136 u32 id;
137 struct nh_notifier_single_info nh;
138};
139
140struct nh_notifier_grp_info {
141 u16 num_nh;
142 bool is_fdb;
143 struct nh_notifier_grp_entry_info nh_entries[];
144};
145
146struct nh_notifier_info {
147 struct net *net;
148 struct netlink_ext_ack *extack;
149 u32 id;
09ad6bec 150 enum nh_notifier_info_type type;
1c9cac65
IS
151 union {
152 struct nh_notifier_single_info *nh;
153 struct nh_notifier_grp_info *nh_grp;
154 };
155};
156
ce7e9c8a
IS
157int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
158 struct netlink_ext_ack *extack);
8590ceed 159int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
e95f2592 160void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
8590ceed 161
ab84be7e
DA
162/* caller is holding rcu or rtnl; no reference taken to nexthop */
163struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
164void nexthop_free_rcu(struct rcu_head *head);
165
166static inline bool nexthop_get(struct nexthop *nh)
167{
168 return refcount_inc_not_zero(&nh->refcnt);
169}
170
171static inline void nexthop_put(struct nexthop *nh)
172{
173 if (refcount_dec_and_test(&nh->refcnt))
174 call_rcu(&nh->rcu, nexthop_free_rcu);
175}
176
4c7e8084
DA
177static inline bool nexthop_cmp(const struct nexthop *nh1,
178 const struct nexthop *nh2)
179{
180 return nh1 == nh2;
ce9ac056
DA
181}
182
183static inline bool nexthop_is_fdb(const struct nexthop *nh)
184{
185 if (nh->is_group) {
186 const struct nh_group *nh_grp;
187
188 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
189 return nh_grp->fdb_nh;
190 } else {
191 const struct nh_info *nhi;
192
193 nhi = rcu_dereference_rtnl(nh->nh_info);
194 return nhi->fdb_nh;
195 }
50cb8769
DA
196}
197
198static inline bool nexthop_has_v4(const struct nexthop *nh)
199{
200 if (nh->is_group) {
201 struct nh_group *nh_grp;
202
203 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
204 return nh_grp->has_v4;
205 }
206 return false;
4c7e8084
DA
207}
208
430a0491
DA
209static inline bool nexthop_is_multipath(const struct nexthop *nh)
210{
211 if (nh->is_group) {
212 struct nh_group *nh_grp;
213
214 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
215 return nh_grp->mpath;
216 }
217 return false;
218}
219
220struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);
221
222static inline unsigned int nexthop_num_path(const struct nexthop *nh)
223{
224 unsigned int rc = 1;
225
0b5e2e39 226 if (nh->is_group) {
430a0491
DA
227 struct nh_group *nh_grp;
228
229 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
0b5e2e39
DA
230 if (nh_grp->mpath)
231 rc = nh_grp->num_nh;
430a0491
DA
232 }
233
234 return rc;
235}
236
237static inline
0b5e2e39 238struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
430a0491 239{
430a0491
DA
240 /* for_nexthops macros in fib_semantics.c grabs a pointer to
241 * the nexthop before checking nhsel
242 */
5270041d 243 if (nhsel >= nhg->num_nh)
430a0491
DA
244 return NULL;
245
246 return nhg->nh_entries[nhsel].nh;
247}
248
249static inline
7bdf4de1
DS
250int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
251 u8 rt_family)
430a0491
DA
252{
253 struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
254 int i;
255
256 for (i = 0; i < nhg->num_nh; i++) {
257 struct nexthop *nhe = nhg->nh_entries[i].nh;
258 struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
259 struct fib_nh_common *nhc = &nhi->fib_nhc;
260 int weight = nhg->nh_entries[i].weight;
261
7bdf4de1 262 if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
430a0491
DA
263 return -EMSGSIZE;
264 }
265
266 return 0;
267}
268
ab84be7e
DA
269/* called with rcu lock */
270static inline bool nexthop_is_blackhole(const struct nexthop *nh)
271{
272 const struct nh_info *nhi;
273
0b5e2e39
DA
274 if (nh->is_group) {
275 struct nh_group *nh_grp;
276
277 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
278 if (nh_grp->num_nh > 1)
430a0491 279 return false;
0b5e2e39
DA
280
281 nh = nh_grp->nh_entries[0].nh;
430a0491
DA
282 }
283
284 nhi = rcu_dereference_rtnl(nh->nh_info);
ab84be7e
DA
285 return nhi->reject_nh;
286}
5481d73f 287
4c7e8084
DA
288static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
289{
290 struct nh_info *nhi;
291 struct nexthop *nh;
292
293 nh = nexthop_select_path(res->fi->nh, hash);
294 nhi = rcu_dereference(nh->nh_info);
295 res->nhc = &nhi->fib_nhc;
296}
297
298/* called with rcu read lock or rtnl held */
299static inline
300struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
301{
302 struct nh_info *nhi;
303
304 BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
305 BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);
306
0b5e2e39
DA
307 if (nh->is_group) {
308 struct nh_group *nh_grp;
309
310 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
311 if (nh_grp->mpath) {
312 nh = nexthop_mpath_select(nh_grp, nhsel);
313 if (!nh)
314 return NULL;
315 }
4c7e8084
DA
316 }
317
318 nhi = rcu_dereference_rtnl(nh->nh_info);
319 return &nhi->fib_nhc;
320}
321
af7888ad
DA
322/* called from fib_table_lookup with rcu_lock */
323static inline
324struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
325 int fib_flags,
326 const struct flowi4 *flp,
327 int *nhsel)
328{
329 struct nh_info *nhi;
330
331 if (nh->is_group) {
332 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
333 int i;
334
335 for (i = 0; i < nhg->num_nh; i++) {
336 struct nexthop *nhe = nhg->nh_entries[i].nh;
337
338 nhi = rcu_dereference(nhe->nh_info);
339 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
340 *nhsel = i;
341 return &nhi->fib_nhc;
342 }
343 }
344 } else {
345 nhi = rcu_dereference(nh->nh_info);
346 if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
347 *nhsel = 0;
348 return &nhi->fib_nhc;
349 }
350 }
351
352 return NULL;
353}
354
1fd1c768
DA
355static inline bool nexthop_uses_dev(const struct nexthop *nh,
356 const struct net_device *dev)
357{
358 struct nh_info *nhi;
359
360 if (nh->is_group) {
361 struct nh_group *nhg = rcu_dereference(nh->nh_grp);
362 int i;
363
364 for (i = 0; i < nhg->num_nh; i++) {
365 struct nexthop *nhe = nhg->nh_entries[i].nh;
366
367 nhi = rcu_dereference(nhe->nh_info);
368 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
369 return true;
370 }
371 } else {
372 nhi = rcu_dereference(nh->nh_info);
373 if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
374 return true;
375 }
376
377 return false;
378}
379
5481d73f
DA
380static inline unsigned int fib_info_num_path(const struct fib_info *fi)
381{
4c7e8084
DA
382 if (unlikely(fi->nh))
383 return nexthop_num_path(fi->nh);
384
5481d73f
DA
385 return fi->fib_nhs;
386}
387
4c7e8084
DA
388int fib_check_nexthop(struct nexthop *nh, u8 scope,
389 struct netlink_ext_ack *extack);
390
5481d73f
DA
391static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
392{
4c7e8084
DA
393 if (unlikely(fi->nh))
394 return nexthop_fib_nhc(fi->nh, nhsel);
395
5481d73f
DA
396 return &fi->fib_nh[nhsel].nh_common;
397}
398
4c7e8084 399/* only used when fib_nh is built into fib_info */
5481d73f
DA
400static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
401{
4c7e8084
DA
402 WARN_ON(fi->nh);
403
5481d73f
DA
404 return &fi->fib_nh[nhsel];
405}
f88d8ea6
DA
406
407/*
408 * IPv6 variants
409 */
410int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
411 struct netlink_ext_ack *extack);
412
413static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
414{
415 struct nh_info *nhi;
416
0b5e2e39
DA
417 if (nh->is_group) {
418 struct nh_group *nh_grp;
419
420 nh_grp = rcu_dereference_rtnl(nh->nh_grp);
421 nh = nexthop_mpath_select(nh_grp, 0);
f88d8ea6
DA
422 if (!nh)
423 return NULL;
424 }
425
426 nhi = rcu_dereference_rtnl(nh->nh_info);
427 if (nhi->family == AF_INET6)
428 return &nhi->fib6_nh;
429
430 return NULL;
431}
432
433static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
434{
435 struct fib6_nh *fib6_nh;
436
437 fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
438 return fib6_nh->fib_nh_dev;
439}
440
441static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
442{
443 struct nexthop *nh = res->f6i->nh;
444 struct nh_info *nhi;
445
446 nh = nexthop_select_path(nh, hash);
447
448 nhi = rcu_dereference_rtnl(nh->nh_info);
449 if (nhi->reject_nh) {
450 res->fib6_type = RTN_BLACKHOLE;
451 res->fib6_flags |= RTF_REJECT;
452 res->nh = nexthop_fib6_nh(nh);
453 } else {
454 res->nh = &nhi->fib6_nh;
455 }
456}
f88c9aa1
DA
457
458int nexthop_for_each_fib6_nh(struct nexthop *nh,
459 int (*cb)(struct fib6_nh *nh, void *arg),
460 void *arg);
38428d68
RP
461
462static inline int nexthop_get_family(struct nexthop *nh)
463{
464 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
465
466 return nhi->family;
467}
468
469static inline
470struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
471{
472 struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);
473
474 return &nhi->fib_nhc;
475}
476
477static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
478 int hash)
479{
480 struct nh_info *nhi;
481 struct nexthop *nhp;
482
483 nhp = nexthop_select_path(nh, hash);
484 if (unlikely(!nhp))
485 return NULL;
486 nhi = rcu_dereference(nhp->nh_info);
487 return &nhi->fib_nhc;
488}
ab84be7e 489#endif