]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
fb018d25 DS |
2 | /* |
3 | * Nexthop structure definition. | |
a399694f | 4 | * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro |
fb018d25 | 5 | * Copyright (C) 2013 Cumulus Networks, Inc. |
fb018d25 DS |
6 | */ |
7 | ||
8 | #ifndef _LIB_NEXTHOP_H | |
9 | #define _LIB_NEXTHOP_H | |
10 | ||
11 | #include "prefix.h" | |
54d48ea1 | 12 | #include "mpls.h" |
f2a0ba3a | 13 | #include "vxlan.h" |
cb7775a9 | 14 | #include "srv6.h" |
fb018d25 | 15 | |
5e244469 RW |
16 | #ifdef __cplusplus |
17 | extern "C" { | |
18 | #endif | |
19 | ||
80c2442a | 20 | /* Maximum next hop string length - gateway + ifindex */ |
21 | #define NEXTHOP_STRLEN (INET6_ADDRSTRLEN + 30) | |
22 | ||
fb018d25 | 23 | union g_addr { |
d62a17ae | 24 | struct in_addr ipv4; |
25 | struct in6_addr ipv6; | |
fb018d25 DS |
26 | }; |
27 | ||
d62a17ae | 28 | enum nexthop_types_t { |
29 | NEXTHOP_TYPE_IFINDEX = 1, /* Directly connected. */ | |
30 | NEXTHOP_TYPE_IPV4, /* IPv4 nexthop. */ | |
31 | NEXTHOP_TYPE_IPV4_IFINDEX, /* IPv4 nexthop with ifindex. */ | |
32 | NEXTHOP_TYPE_IPV6, /* IPv6 nexthop. */ | |
33 | NEXTHOP_TYPE_IPV6_IFINDEX, /* IPv6 nexthop with ifindex. */ | |
34 | NEXTHOP_TYPE_BLACKHOLE, /* Null0 nexthop. */ | |
fb018d25 | 35 | }; |
09a484dd DL |
36 | |
37 | enum blackhole_type { | |
38 | BLACKHOLE_UNSPEC = 0, | |
39 | BLACKHOLE_NULL, | |
40 | BLACKHOLE_REJECT, | |
41 | BLACKHOLE_ADMINPROHIB, | |
42 | }; | |
fb018d25 | 43 | |
f2a0ba3a RZ |
44 | enum nh_encap_type { |
45 | NET_VXLAN = 100, /* value copied from FPM_NH_ENCAP_VXLAN. */ | |
46 | }; | |
47 | ||
474aebd9 MS |
48 | /* Fixed limit on the number of backup nexthops per primary nexthop */ |
49 | #define NEXTHOP_MAX_BACKUPS 8 | |
50 | ||
51 | /* Backup index value is limited */ | |
52 | #define NEXTHOP_BACKUP_IDX_MAX 255 | |
53 | ||
fb018d25 | 54 | /* Nexthop structure. */ |
d62a17ae | 55 | struct nexthop { |
56 | struct nexthop *next; | |
57 | struct nexthop *prev; | |
fb018d25 | 58 | |
4a7371e9 DS |
59 | /* |
60 | * What vrf is this nexthop associated with? | |
61 | */ | |
62 | vrf_id_t vrf_id; | |
63 | ||
d62a17ae | 64 | /* Interface index. */ |
65 | ifindex_t ifindex; | |
fb018d25 | 66 | |
d62a17ae | 67 | enum nexthop_types_t type; |
fb018d25 | 68 | |
52e8a7c4 | 69 | uint16_t flags; |
fb018d25 DS |
70 | #define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */ |
71 | #define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */ | |
72 | #define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */ | |
0641a955 MS |
73 | #define NEXTHOP_FLAG_ONLINK (1 << 3) /* Nexthop should be installed |
74 | * onlink. | |
75 | */ | |
76 | #define NEXTHOP_FLAG_DUPLICATE (1 << 4) /* nexthop duplicates another | |
77 | * active one | |
78 | */ | |
79 | #define NEXTHOP_FLAG_RNH_FILTERED (1 << 5) /* rmap filtered, used by rnh */ | |
defd2ea4 | 80 | #define NEXTHOP_FLAG_HAS_BACKUP (1 << 6) /* Backup nexthop index is set */ |
31f937fb | 81 | #define NEXTHOP_FLAG_SRTE (1 << 7) /* SR-TE color used for BGP traffic */ |
5609e70f | 82 | #define NEXTHOP_FLAG_EVPN (1 << 8) /* nexthop is EVPN */ |
c704cb44 | 83 | #define NEXTHOP_FLAG_LINKDOWN (1 << 9) /* is not removed on link down */ |
defd2ea4 | 84 | |
996c9314 LB |
85 | #define NEXTHOP_IS_ACTIVE(flags) \ |
86 | (CHECK_FLAG(flags, NEXTHOP_FLAG_ACTIVE) \ | |
87 | && !CHECK_FLAG(flags, NEXTHOP_FLAG_DUPLICATE)) | |
fb018d25 | 88 | |
d62a17ae | 89 | /* Nexthop address */ |
a8309422 DL |
90 | union { |
91 | union g_addr gate; | |
92 | enum blackhole_type bh_type; | |
93 | }; | |
d62a17ae | 94 | union g_addr src; |
95 | union g_addr rmap_src; /* Src is set via routemap */ | |
96 | ||
97 | /* Nexthops obtained by recursive resolution. | |
98 | * | |
99 | * If the nexthop struct needs to be resolved recursively, | |
100 | * NEXTHOP_FLAG_RECURSIVE will be set in flags and the nexthops | |
101 | * obtained by recursive resolution will be added to `resolved'. | |
102 | */ | |
103 | struct nexthop *resolved; | |
104 | /* Recursive parent */ | |
105 | struct nexthop *rparent; | |
106 | ||
107 | /* Type of label(s), if any */ | |
108 | enum lsp_types_t nh_label_type; | |
109 | ||
110 | /* Label(s) associated with this nexthop. */ | |
8ecdb26e | 111 | struct mpls_label_stack *nh_label; |
df7fb580 DS |
112 | |
113 | /* Weight of the nexthop ( for unequal cost ECMP ) */ | |
114 | uint8_t weight; | |
defd2ea4 | 115 | |
474aebd9 | 116 | /* Count and index of corresponding backup nexthop(s) in a backup list; |
defd2ea4 MS |
117 | * only meaningful if the HAS_BACKUP flag is set. |
118 | */ | |
474aebd9 MS |
119 | uint8_t backup_num; |
120 | uint8_t backup_idx[NEXTHOP_MAX_BACKUPS]; | |
f2a0ba3a RZ |
121 | |
122 | /* Encapsulation information. */ | |
123 | enum nh_encap_type nh_encap_type; | |
124 | union { | |
125 | vni_t vni; | |
126 | } nh_encap; | |
31f937fb SM |
127 | |
128 | /* SR-TE color used for matching SR-TE policies */ | |
129 | uint32_t srte_color; | |
cb7775a9 | 130 | |
eab0f8f0 HS |
131 | /* SRv6 information */ |
132 | struct nexthop_srv6 *nh_srv6; | |
fb018d25 DS |
133 | }; |
134 | ||
e4a1ec74 MS |
135 | /* Utility to append one nexthop to another. */ |
136 | #define NEXTHOP_APPEND(to, new) \ | |
137 | do { \ | |
138 | (to)->next = (new); \ | |
139 | (new)->prev = (to); \ | |
140 | (new)->next = NULL; \ | |
141 | } while (0) | |
142 | ||
d62a17ae | 143 | struct nexthop *nexthop_new(void); |
a399694f | 144 | |
d62a17ae | 145 | void nexthop_free(struct nexthop *nexthop); |
146 | void nexthops_free(struct nexthop *nexthop); | |
a399694f | 147 | |
e4a1ec74 MS |
148 | void nexthop_add_labels(struct nexthop *nexthop, enum lsp_types_t ltype, |
149 | uint8_t num_labels, const mpls_label_t *labels); | |
d62a17ae | 150 | void nexthop_del_labels(struct nexthop *); |
eab0f8f0 HS |
151 | void nexthop_add_srv6_seg6local(struct nexthop *nexthop, uint32_t action, |
152 | const struct seg6local_context *ctx); | |
153 | void nexthop_del_srv6_seg6local(struct nexthop *nexthop); | |
154 | void nexthop_add_srv6_seg6(struct nexthop *nexthop, | |
155 | const struct in6_addr *segs); | |
156 | void nexthop_del_srv6_seg6(struct nexthop *nexthop); | |
40c7bdb0 | 157 | |
f3323df2 MS |
158 | /* |
159 | * Allocate a new nexthop object and initialize it from various args. | |
160 | */ | |
161 | struct nexthop *nexthop_from_ifindex(ifindex_t ifindex, vrf_id_t vrf_id); | |
162 | struct nexthop *nexthop_from_ipv4(const struct in_addr *ipv4, | |
163 | const struct in_addr *src, | |
164 | vrf_id_t vrf_id); | |
165 | struct nexthop *nexthop_from_ipv4_ifindex(const struct in_addr *ipv4, | |
166 | const struct in_addr *src, | |
167 | ifindex_t ifindex, vrf_id_t vrf_id); | |
168 | struct nexthop *nexthop_from_ipv6(const struct in6_addr *ipv6, | |
169 | vrf_id_t vrf_id); | |
170 | struct nexthop *nexthop_from_ipv6_ifindex(const struct in6_addr *ipv6, | |
171 | ifindex_t ifindex, vrf_id_t vrf_id); | |
0789eb69 KM |
172 | struct nexthop *nexthop_from_blackhole(enum blackhole_type bh_type, |
173 | vrf_id_t nh_vrf_id); | |
f3323df2 | 174 | |
d36d0d57 QY |
175 | /* |
176 | * Hash a nexthop. Suitable for use with hash tables. | |
177 | * | |
178 | * This function uses the following values when computing the hash: | |
179 | * - vrf_id | |
180 | * - ifindex | |
181 | * - type | |
182 | * - gate | |
183 | * | |
184 | * nexthop | |
185 | * The nexthop to hash | |
186 | * | |
187 | * Returns: | |
188 | * 32-bit hash of nexthop | |
189 | */ | |
1b1fe1c4 | 190 | uint32_t nexthop_hash(const struct nexthop *nexthop); |
73a38187 SW |
191 | /* |
192 | * Hash a nexthop only on word-sized attributes: | |
193 | * - vrf_id | |
194 | * - ifindex | |
195 | * - type | |
196 | * - (some) flags | |
197 | */ | |
198 | uint32_t nexthop_hash_quick(const struct nexthop *nexthop); | |
d36d0d57 | 199 | |
31919191 | 200 | extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2); |
a5a2d802 SW |
201 | extern bool nexthop_same_no_labels(const struct nexthop *nh1, |
202 | const struct nexthop *nh2); | |
776c3e90 | 203 | extern int nexthop_cmp(const struct nexthop *nh1, const struct nexthop *nh2); |
3c6e0bd4 SW |
204 | extern int nexthop_g_addr_cmp(enum nexthop_types_t type, |
205 | const union g_addr *addr1, | |
206 | const union g_addr *addr2); | |
31919191 | 207 | |
338ec3b8 MS |
208 | /* More-limited comparison function used to detect duplicate nexthops. |
209 | * Returns -1, 0, 1 | |
210 | */ | |
211 | int nexthop_cmp_basic(const struct nexthop *nh1, const struct nexthop *nh2); | |
212 | ||
d62a17ae | 213 | extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type); |
89dc3160 SW |
214 | extern bool nexthop_labels_match(const struct nexthop *nh1, |
215 | const struct nexthop *nh2); | |
fb018d25 | 216 | |
55f93d4b MS |
217 | extern const char *nexthop2str(const struct nexthop *nexthop, |
218 | char *str, int size); | |
17c25e03 SW |
219 | extern struct nexthop *nexthop_next(const struct nexthop *nexthop); |
220 | extern struct nexthop * | |
221 | nexthop_next_active_resolved(const struct nexthop *nexthop); | |
850c85b9 | 222 | extern unsigned int nexthop_level(const struct nexthop *nexthop); |
504d0a40 | 223 | /* Copies to an already allocated nexthop struct */ |
e7addf02 SW |
224 | extern void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop, |
225 | struct nexthop *rparent); | |
77bf9504 SW |
226 | /* Copies to an already allocated nexthop struct, not including recurse info */ |
227 | extern void nexthop_copy_no_recurse(struct nexthop *copy, | |
228 | const struct nexthop *nexthop, | |
229 | struct nexthop *rparent); | |
504d0a40 SW |
230 | /* Duplicates a nexthop and returns the newly allocated nexthop */ |
231 | extern struct nexthop *nexthop_dup(const struct nexthop *nexthop, | |
232 | struct nexthop *rparent); | |
77bf9504 SW |
233 | /* Duplicates a nexthop and returns the newly allocated nexthop */ |
234 | extern struct nexthop *nexthop_dup_no_recurse(const struct nexthop *nexthop, | |
235 | struct nexthop *rparent); | |
5e244469 | 236 | |
5bb87732 AR |
237 | /* Check nexthop of IFINDEX type */ |
238 | extern bool nexthop_is_ifindex_type(const struct nexthop *nh); | |
239 | ||
0cac0cf4 MS |
240 | /* |
241 | * Parse one or more backup index values, as comma-separated numbers, | |
242 | * into caller's array of uint8_ts. The array must be NEXTHOP_MAX_BACKUPS | |
243 | * in size. Mails back the number of values converted, and returns 0 on | |
244 | * success, <0 if an error in parsing. | |
245 | */ | |
246 | int nexthop_str2backups(const char *str, int *num_backups, | |
247 | uint8_t *backups); | |
248 | ||
07ef3e34 DL |
249 | #ifdef _FRR_ATTRIBUTE_PRINTFRR |
250 | #pragma FRR printfrr_ext "%pNH" (struct nexthop *) | |
251 | #endif | |
252 | ||
f7a410a7 | 253 | ssize_t printfrr_nhs(struct fbuf *buf, const struct nexthop *nh); |
5e244469 RW |
254 | #ifdef __cplusplus |
255 | } | |
256 | #endif | |
257 | ||
fb018d25 | 258 | #endif /*_LIB_NEXTHOP_H */ |