]>
Commit | Line | Data |
---|---|---|
ad28e79a SW |
1 | /* Zebra Nexthop Group header. |
2 | * Copyright (C) 2019 Cumulus Networks, Inc. | |
3 | * Donald Sharp | |
4 | * Stephen Worley | |
5 | * | |
6 | * This file is part of FRR. | |
7 | * | |
8 | * FRR is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * FRR is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with FRR; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
21 | * 02111-1307, USA. | |
22 | */ | |
23 | #ifndef __ZEBRA_NHG_H__ | |
24 | #define __ZEBRA_NHG_H__ | |
25 | ||
5463ce26 | 26 | #include "lib/nexthop.h" |
69171da2 | 27 | #include "lib/nexthop_group.h" |
ad28e79a | 28 | |
17e38209 RW |
29 | #ifdef __cplusplus |
30 | extern "C" { | |
31 | #endif | |
32 | ||
0c8215cb SW |
33 | /* This struct is used exclusively for dataplane |
34 | * interaction via a dataplane context. | |
35 | * | |
36 | * It is designed to mimic the netlink nexthop_grp | |
37 | * struct in include/linux/nexthop.h | |
38 | */ | |
e22e8001 | 39 | struct nh_grp { |
0c8215cb SW |
40 | uint32_t id; |
41 | uint8_t weight; | |
42 | }; | |
43 | ||
37c6708b | 44 | PREDECL_RBTREE_UNIQ(nhg_connected_tree); |
0c8215cb | 45 | |
53ac1fbb | 46 | /* |
c415d895 | 47 | * Hashtables containing nhg entries is in `zebra_router`. |
53ac1fbb | 48 | */ |
69171da2 | 49 | struct nhg_hash_entry { |
a95b8020 | 50 | uint32_t id; |
77b76fc9 | 51 | afi_t afi; |
69171da2 | 52 | vrf_id_t vrf_id; |
ee94437e | 53 | |
45691de9 SW |
54 | /* Time since last update */ |
55 | time_t uptime; | |
56 | ||
ee94437e | 57 | /* Source protocol - zebra or another daemon */ |
38e40db1 | 58 | int type; |
69171da2 | 59 | |
ee94437e MS |
60 | /* zapi instance and session id, for groups from other daemons */ |
61 | uint16_t zapi_instance; | |
62 | uint32_t zapi_session; | |
63 | ||
c415d895 | 64 | struct nexthop_group nhg; |
69171da2 | 65 | |
1d48702e MS |
66 | /* If supported, a mapping of backup nexthops. */ |
67 | struct nhg_backup_info *backup_info; | |
68 | ||
2614bf87 SW |
69 | /* If this is not a group, it |
70 | * will be a single nexthop | |
71 | * and must have an interface | |
72 | * associated with it. | |
73 | * Otherwise, this will be null. | |
74 | */ | |
75 | struct interface *ifp; | |
76 | ||
69171da2 DS |
77 | uint32_t refcnt; |
78 | uint32_t dplane_ref; | |
c8ee3cdb DS |
79 | |
80 | uint32_t flags; | |
3119f6a1 | 81 | |
5772319e | 82 | /* Dependency trees for other entries. |
3119f6a1 SW |
83 | * For instance a group with two |
84 | * nexthops will have two dependencies | |
85 | * pointing to those nhg_hash_entries. | |
0c8215cb SW |
86 | * |
87 | * Using a rb tree here to make lookups | |
88 | * faster with ID's. | |
5772319e DS |
89 | * |
90 | * nhg_depends the RB tree of entries that this | |
91 | * group contains. | |
92 | * | |
93 | * nhg_dependents the RB tree of entries that | |
94 | * this group is being used by | |
95 | * | |
96 | * NHG id 3 with nexthops id 1/2 | |
97 | * nhg(3)->nhg_depends has 1 and 2 in the tree | |
98 | * nhg(3)->nhg_dependents is empty | |
99 | * | |
100 | * nhg(1)->nhg_depends is empty | |
101 | * nhg(1)->nhg_dependents is 3 in the tree | |
102 | * | |
103 | * nhg(2)->nhg_depends is empty | |
104 | * nhg(3)->nhg_dependents is 3 in the tree | |
3119f6a1 | 105 | */ |
37c6708b | 106 | struct nhg_connected_tree_head nhg_depends, nhg_dependents; |
1d48702e | 107 | |
35729f38 DS |
108 | struct thread *timer; |
109 | ||
c8ee3cdb DS |
110 | /* |
111 | * Is this nexthop group valid, ie all nexthops are fully resolved. | |
112 | * What is fully resolved? It's a nexthop that is either self contained | |
113 | * and correct( ie no recursive pointer ) or a nexthop that is recursively | |
114 | * resolved and correct. | |
115 | */ | |
38e40db1 | 116 | #define NEXTHOP_GROUP_VALID (1 << 0) |
c8ee3cdb DS |
117 | /* |
118 | * Has this nexthop group been installed? At this point in time, this | |
119 | * means that the data-plane has been told about this nexthop group | |
120 | * and it's possible usage by a route entry. | |
121 | */ | |
38e40db1 | 122 | #define NEXTHOP_GROUP_INSTALLED (1 << 1) |
7f6077d0 SW |
123 | /* |
124 | * Has the nexthop group been queued to be send to the FIB? | |
125 | * The NEXTHOP_GROUP_VALID flag should also be set by this point. | |
126 | */ | |
38e40db1 | 127 | #define NEXTHOP_GROUP_QUEUED (1 << 2) |
98cda54a SW |
128 | /* |
129 | * Is this a nexthop that is recursively resolved? | |
130 | */ | |
38e40db1 | 131 | #define NEXTHOP_GROUP_RECURSIVE (1 << 3) |
1d48702e MS |
132 | |
133 | /* | |
134 | * Backup nexthop support - identify groups that are backups for | |
135 | * another group. | |
136 | */ | |
0885b1e3 | 137 | #define NEXTHOP_GROUP_BACKUP (1 << 4) |
1d48702e | 138 | |
8155e8c5 SW |
139 | /* |
140 | * The NHG has been release by an upper level protocol via the | |
141 | * `zebra_nhg_proto_del()` API. | |
142 | * | |
143 | * We use this flag to track this state in case the NHG is still being used | |
144 | * by routes therefore holding their refcnts as well. Otherwise, the NHG will | |
145 | * be removed and uninstalled. | |
146 | * | |
147 | */ | |
148 | #define NEXTHOP_GROUP_PROTO_RELEASED (1 << 5) | |
149 | ||
35729f38 DS |
150 | /* |
151 | * When deleting a NHG notice that it is still installed | |
152 | * and if it is, slightly delay the actual removal to | |
153 | * the future. So that upper level protocols might | |
154 | * be able to take advantage of some NHG's that | |
155 | * are there | |
156 | */ | |
157 | #define NEXTHOP_GROUP_KEEP_AROUND (1 << 6) | |
158 | ||
981ca597 RZ |
159 | /* |
160 | * Track FPM installation status.. | |
161 | */ | |
8155e8c5 | 162 | #define NEXTHOP_GROUP_FPM (1 << 6) |
69171da2 DS |
163 | }; |
164 | ||
4f9bb78e AK |
165 | /* Upper 4 bits of the NHG are reserved for indicating the NHG type */ |
166 | #define NHG_ID_TYPE_POS 28 | |
167 | enum nhg_type { | |
168 | NHG_TYPE_L3 = 0, | |
169 | NHG_TYPE_L2_NH, /* NHs in a L2 NHG used as a MAC/FDB dest */ | |
170 | NHG_TYPE_L2, /* L2 NHG used as a MAC/FDB dest */ | |
171 | }; | |
172 | ||
38e40db1 | 173 | /* Was this one we created, either this session or previously? */ |
0885b1e3 SW |
174 | #define ZEBRA_NHG_CREATED(NHE) \ |
175 | (((NHE->type) <= ZEBRA_ROUTE_MAX) && (NHE->type != ZEBRA_ROUTE_KERNEL)) | |
176 | ||
177 | /* Is this an NHE owned by zebra and not an upper level protocol? */ | |
178 | #define ZEBRA_OWNED(NHE) (NHE->type == ZEBRA_ROUTE_NHG) | |
38e40db1 | 179 | |
65f137fe SW |
180 | #define PROTO_OWNED(NHE) (NHE->id >= ZEBRA_NHG_PROTO_LOWER) |
181 | ||
1d48702e MS |
182 | /* |
183 | * Backup nexthops: this is a group object itself, so | |
184 | * that the backup nexthops can use the same code as a normal object. | |
185 | */ | |
186 | struct nhg_backup_info { | |
187 | struct nhg_hash_entry *nhe; | |
188 | }; | |
e22e8001 SW |
189 | |
190 | enum nhg_ctx_op_e { | |
191 | NHG_CTX_OP_NONE = 0, | |
192 | NHG_CTX_OP_NEW, | |
193 | NHG_CTX_OP_DEL, | |
194 | }; | |
195 | ||
1b366e63 | 196 | enum nhg_ctx_status { |
e22e8001 SW |
197 | NHG_CTX_NONE = 0, |
198 | NHG_CTX_QUEUED, | |
1b366e63 | 199 | NHG_CTX_REQUEUED, |
e22e8001 SW |
200 | NHG_CTX_SUCCESS, |
201 | NHG_CTX_FAILURE, | |
202 | }; | |
203 | ||
204 | /* | |
205 | * Context needed to queue nhg updates on the | |
206 | * work queue. | |
207 | */ | |
208 | struct nhg_ctx { | |
209 | ||
210 | /* Unique ID */ | |
211 | uint32_t id; | |
212 | ||
213 | vrf_id_t vrf_id; | |
214 | afi_t afi; | |
9b4ab909 | 215 | |
38e40db1 | 216 | /* |
9b4ab909 | 217 | * This should only ever be ZEBRA_ROUTE_NHG unless we get a a kernel |
38e40db1 SW |
218 | * created nexthop not made by us. |
219 | */ | |
220 | int type; | |
e22e8001 SW |
221 | |
222 | /* If its a group array, how many? */ | |
223 | uint8_t count; | |
224 | ||
225 | /* Its either a single nexthop or an array of ID's */ | |
226 | union { | |
227 | struct nexthop nh; | |
228 | struct nh_grp grp[MULTIPATH_NUM]; | |
229 | } u; | |
230 | ||
569e1411 | 231 | struct nhg_resilience resilience; |
e22e8001 | 232 | enum nhg_ctx_op_e op; |
1b366e63 | 233 | enum nhg_ctx_status status; |
e22e8001 SW |
234 | }; |
235 | ||
7c99d51b MS |
236 | /* Global control to disable use of kernel nexthops, if available. We can't |
237 | * force the kernel to support nexthop ids, of course, but we can disable | |
238 | * zebra's use of them, for testing e.g. By default, if the kernel supports | |
239 | * nexthop ids, zebra uses them. | |
240 | */ | |
241 | void zebra_nhg_enable_kernel_nexthops(bool set); | |
242 | bool zebra_nhg_kernel_nexthops_enabled(void); | |
e22e8001 | 243 | |
6c67f41f SW |
244 | /* Global control for zebra to only use proto-owned nexthops */ |
245 | void zebra_nhg_set_proto_nexthops_only(bool set); | |
246 | bool zebra_nhg_proto_nexthops_only(void); | |
247 | ||
aa458838 MS |
248 | /* Global control for use of activated backups for recursive resolution. */ |
249 | void zebra_nhg_set_recursive_use_backups(bool set); | |
250 | bool zebra_nhg_recursive_use_backups(void); | |
251 | ||
fe593b78 SW |
252 | /** |
253 | * NHE abstracted tree functions. | |
1d48702e | 254 | * Use these where possible instead of direct access. |
fe593b78 | 255 | */ |
0eb97b86 MS |
256 | struct nhg_hash_entry *zebra_nhg_alloc(void); |
257 | void zebra_nhg_free(struct nhg_hash_entry *nhe); | |
258 | /* In order to clear a generic hash, we need a generic api, sigh. */ | |
259 | void zebra_nhg_hash_free(void *p); | |
d5795103 | 260 | void zebra_nhg_hash_free_zero_id(struct hash_bucket *b, void *arg); |
0eb97b86 | 261 | |
377e29f7 MS |
262 | /* Init an nhe, for use in a hash lookup for example. There's some fuzziness |
263 | * if the nhe represents only a single nexthop, so we try to capture that | |
264 | * variant also. | |
265 | */ | |
266 | void zebra_nhe_init(struct nhg_hash_entry *nhe, afi_t afi, | |
267 | const struct nexthop *nh); | |
268 | ||
f727646a MS |
269 | /* |
270 | * Shallow copy of 'orig', into new/allocated nhe. | |
271 | */ | |
272 | struct nhg_hash_entry *zebra_nhe_copy(const struct nhg_hash_entry *orig, | |
273 | uint32_t id); | |
274 | ||
1d48702e MS |
275 | /* Allocate, free backup nexthop info objects */ |
276 | struct nhg_backup_info *zebra_nhg_backup_alloc(void); | |
277 | void zebra_nhg_backup_free(struct nhg_backup_info **p); | |
278 | ||
1d48702e MS |
279 | struct nexthop_group *zebra_nhg_get_backup_nhg(struct nhg_hash_entry *nhe); |
280 | ||
98cda54a | 281 | extern struct nhg_hash_entry *zebra_nhg_resolve(struct nhg_hash_entry *nhe); |
98cda54a | 282 | |
fe593b78 | 283 | extern unsigned int zebra_nhg_depends_count(const struct nhg_hash_entry *nhe); |
0c8215cb | 284 | extern bool zebra_nhg_depends_is_empty(const struct nhg_hash_entry *nhe); |
5948f013 | 285 | |
fe593b78 SW |
286 | extern unsigned int |
287 | zebra_nhg_dependents_count(const struct nhg_hash_entry *nhe); | |
288 | extern bool zebra_nhg_dependents_is_empty(const struct nhg_hash_entry *nhe); | |
3119f6a1 | 289 | |
5948f013 | 290 | /* Lookup ID, doesn't create */ |
d9f5b2f5 | 291 | extern struct nhg_hash_entry *zebra_nhg_lookup_id(uint32_t id); |
d9f5b2f5 | 292 | |
5948f013 | 293 | /* Hash functions */ |
69171da2 | 294 | extern uint32_t zebra_nhg_hash_key(const void *arg); |
a95b8020 | 295 | extern uint32_t zebra_nhg_id_key(const void *arg); |
69171da2 DS |
296 | |
297 | extern bool zebra_nhg_hash_equal(const void *arg1, const void *arg2); | |
d9f5b2f5 | 298 | extern bool zebra_nhg_hash_id_equal(const void *arg1, const void *arg2); |
69171da2 | 299 | |
e22e8001 SW |
300 | /* |
301 | * Process a context off of a queue. | |
302 | * Specifically this should be from | |
303 | * the rib meta queue. | |
304 | */ | |
305 | extern int nhg_ctx_process(struct nhg_ctx *ctx); | |
04bec7b2 | 306 | void nhg_ctx_free(struct nhg_ctx **ctx); |
85f5e761 | 307 | |
e22e8001 SW |
308 | /* Find via kernel nh creation */ |
309 | extern int zebra_nhg_kernel_find(uint32_t id, struct nexthop *nh, | |
310 | struct nh_grp *grp, uint8_t count, | |
38e40db1 | 311 | vrf_id_t vrf_id, afi_t afi, int type, |
569e1411 DS |
312 | int startup, |
313 | struct nhg_resilience *resilience); | |
9a1588c4 | 314 | /* Del via kernel */ |
88cafda7 | 315 | extern int zebra_nhg_kernel_del(uint32_t id, vrf_id_t vrf_id); |
e22e8001 | 316 | |
377e29f7 | 317 | /* Find an nhe based on a nexthop_group */ |
0885b1e3 SW |
318 | extern struct nhg_hash_entry *zebra_nhg_rib_find(uint32_t id, |
319 | struct nexthop_group *nhg, | |
320 | afi_t rt_afi, int type); | |
3057df51 | 321 | |
377e29f7 MS |
322 | /* Find an nhe based on a route's nhe, used during route creation */ |
323 | struct nhg_hash_entry * | |
324 | zebra_nhg_rib_find_nhe(struct nhg_hash_entry *rt_nhe, afi_t rt_afi); | |
325 | ||
0885b1e3 SW |
326 | |
327 | /** | |
328 | * Functions for Add/Del/Replace via protocol NHG creation. | |
329 | * | |
330 | * The NHEs will not be hashed. They will only be present in the | |
331 | * ID table and therefore not sharable. | |
332 | * | |
333 | * It is the owning protocols job to manage these. | |
334 | */ | |
335 | ||
336 | /* | |
2d8a9c54 | 337 | * Add NHE. If already exists, Replace. |
0885b1e3 SW |
338 | * |
339 | * Returns allocated NHE on success, otherwise NULL. | |
340 | */ | |
341 | struct nhg_hash_entry *zebra_nhg_proto_add(uint32_t id, int type, | |
ee94437e | 342 | uint16_t instance, uint32_t session, |
0885b1e3 SW |
343 | struct nexthop_group *nhg, |
344 | afi_t afi); | |
345 | ||
0885b1e3 SW |
346 | /* |
347 | * Del NHE. | |
348 | * | |
349 | * Returns deleted NHE on success, otherwise NULL. | |
350 | * | |
1f655680 | 351 | * Caller must decrement ref with zebra_nhg_decrement_ref() when done. |
0885b1e3 | 352 | */ |
aaa42e05 | 353 | struct nhg_hash_entry *zebra_nhg_proto_del(uint32_t id, int type); |
0885b1e3 | 354 | |
24db1a7b SW |
355 | /* |
356 | * Remove specific by proto NHGs. | |
357 | * | |
358 | * Called after client disconnect. | |
359 | * | |
360 | */ | |
361 | unsigned long zebra_nhg_score_proto(int type); | |
362 | ||
5948f013 SW |
363 | /* Reference counter functions */ |
364 | extern void zebra_nhg_decrement_ref(struct nhg_hash_entry *nhe); | |
365 | extern void zebra_nhg_increment_ref(struct nhg_hash_entry *nhe); | |
144a1b34 | 366 | |
5948f013 SW |
367 | /* Check validity of nhe, if invalid will update dependents as well */ |
368 | extern void zebra_nhg_check_valid(struct nhg_hash_entry *nhe); | |
369 | ||
370 | /* Convert nhe depends to a grp context that can be passed around safely */ | |
8dbc800f SW |
371 | extern uint8_t zebra_nhg_nhe2grp(struct nh_grp *grp, struct nhg_hash_entry *nhe, |
372 | int size); | |
98cda54a | 373 | |
5948f013 SW |
374 | /* Dataplane install/uninstall */ |
375 | extern void zebra_nhg_install_kernel(struct nhg_hash_entry *nhe); | |
376 | extern void zebra_nhg_uninstall_kernel(struct nhg_hash_entry *nhe); | |
147bad16 | 377 | |
0c8215cb SW |
378 | /* Forward ref of dplane update context type */ |
379 | struct zebra_dplane_ctx; | |
5948f013 SW |
380 | extern void zebra_nhg_dplane_result(struct zebra_dplane_ctx *ctx); |
381 | ||
38e40db1 | 382 | |
b1b07ef5 | 383 | /* Sweep the nhg hash tables for old entries on restart */ |
5948f013 SW |
384 | extern void zebra_nhg_sweep_table(struct hash *hash); |
385 | ||
b1b07ef5 DS |
386 | /* |
387 | * We are shutting down but the nexthops should be kept | |
388 | * as that -r has been specified and we don't want to delete | |
389 | * the routes unintentionally | |
390 | */ | |
391 | extern void zebra_nhg_mark_keep(void); | |
392 | ||
5948f013 | 393 | /* Nexthop resolution processing */ |
5463ce26 | 394 | struct route_entry; /* Forward ref to avoid circular includes */ |
5948f013 | 395 | extern int nexthop_active_update(struct route_node *rn, struct route_entry *re); |
7c99d51b | 396 | |
cc75cbea DS |
397 | #ifdef _FRR_ATTRIBUTE_PRINTFRR |
398 | #pragma FRR printfrr_ext "%pNG" (const struct nhg_hash_entry *) | |
399 | #endif | |
400 | ||
17e38209 RW |
401 | #ifdef __cplusplus |
402 | } | |
403 | #endif | |
404 | ||
7c99d51b | 405 | #endif /* __ZEBRA_NHG_H__ */ |