]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_table.h
Merge pull request #7274 from donaldsharp/bgp_best_is_a_path
[mirror_frr.git] / bgpd / bgp_table.h
1 /* BGP routing table
2 * Copyright (C) 1998, 2001 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _QUAGGA_BGP_TABLE_H
22 #define _QUAGGA_BGP_TABLE_H
23
24 /* XXX BEGIN TEMPORARY COMPAT */
25 #define bgp_dest bgp_node
26 /* XXX END TEMPORARY COMPAT */
27
28 #include "mpls.h"
29 #include "table.h"
30 #include "queue.h"
31 #include "linklist.h"
32 #include "bgpd.h"
33 #include "bgp_advertise.h"
34
35 struct bgp_table {
36 /* table belongs to this instance */
37 struct bgp *bgp;
38
39 /* afi/safi of this table */
40 afi_t afi;
41 safi_t safi;
42
43 int lock;
44
45 struct route_table *route_table;
46 uint64_t version;
47 };
48
49 enum bgp_path_selection_reason {
50 bgp_path_selection_none,
51 bgp_path_selection_first,
52 bgp_path_selection_evpn_sticky_mac,
53 bgp_path_selection_evpn_seq,
54 bgp_path_selection_evpn_local_path,
55 bgp_path_selection_evpn_non_proxy,
56 bgp_path_selection_evpn_lower_ip,
57 bgp_path_selection_weight,
58 bgp_path_selection_local_pref,
59 bgp_path_selection_local_route,
60 bgp_path_selection_confed_as_path,
61 bgp_path_selection_as_path,
62 bgp_path_selection_origin,
63 bgp_path_selection_med,
64 bgp_path_selection_peer,
65 bgp_path_selection_confed,
66 bgp_path_selection_igp_metric,
67 bgp_path_selection_older,
68 bgp_path_selection_router_id,
69 bgp_path_selection_cluster_length,
70 bgp_path_selection_stale,
71 bgp_path_selection_local_configured,
72 bgp_path_selection_neighbor_ip,
73 bgp_path_selection_default,
74 };
75
76 struct bgp_node {
77 /*
78 * CAUTION
79 *
80 * These fields must be the very first fields in this structure.
81 *
82 * @see bgp_node_to_rnode
83 * @see bgp_node_from_rnode
84 */
85 ROUTE_NODE_FIELDS
86
87 struct bgp_adj_out_rb adj_out;
88
89 struct bgp_adj_in *adj_in;
90
91 struct bgp_dest *pdest;
92
93 STAILQ_ENTRY(bgp_dest) pq;
94
95 uint64_t version;
96
97 mpls_label_t local_label;
98
99 uint8_t flags;
100 #define BGP_NODE_PROCESS_SCHEDULED (1 << 0)
101 #define BGP_NODE_USER_CLEAR (1 << 1)
102 #define BGP_NODE_LABEL_CHANGED (1 << 2)
103 #define BGP_NODE_REGISTERED_FOR_LABEL (1 << 3)
104 #define BGP_NODE_SELECT_DEFER (1 << 4)
105 /* list node pointer */
106 struct listnode *rt_node;
107 struct bgp_addpath_node_data tx_addpath;
108
109 enum bgp_path_selection_reason reason;
110 };
111
112 extern void bgp_delete_listnode(struct bgp_dest *dest);
113 /*
114 * bgp_table_iter_t
115 *
116 * Structure that holds state for iterating over a bgp table.
117 */
118 typedef struct bgp_table_iter_t_ {
119 struct bgp_table *table;
120 route_table_iter_t rt_iter;
121 } bgp_table_iter_t;
122
123 extern struct bgp_table *bgp_table_init(struct bgp *bgp, afi_t, safi_t);
124 extern void bgp_table_lock(struct bgp_table *);
125 extern void bgp_table_unlock(struct bgp_table *);
126 extern void bgp_table_finish(struct bgp_table **);
127
128
129 /*
130 * bgp_dest_from_rnode
131 *
132 * Returns the bgp_dest structure corresponding to a route_node.
133 */
134 static inline struct bgp_dest *bgp_dest_from_rnode(struct route_node *rnode)
135 {
136 return (struct bgp_dest *)rnode;
137 }
138
139 /*
140 * bgp_dest_to_rnode
141 *
142 * Returns the route_node structure corresponding to a bgp_dest.
143 */
144 static inline struct route_node *bgp_dest_to_rnode(const struct bgp_dest *dest)
145 {
146 return (struct route_node *)dest;
147 }
148
149 /*
150 * bgp_dest_table
151 *
152 * Returns the bgp_table that the given dest is in.
153 */
154 static inline struct bgp_table *bgp_dest_table(struct bgp_dest *dest)
155 {
156 return route_table_get_info(bgp_dest_to_rnode(dest)->table);
157 }
158
159 /*
160 * bgp_dest_parent_nolock
161 *
162 * Gets the parent dest of the given node without locking it.
163 */
164 static inline struct bgp_dest *bgp_dest_parent_nolock(struct bgp_dest *dest)
165 {
166 struct route_node *rn = bgp_dest_to_rnode(dest)->parent;
167
168 return bgp_dest_from_rnode(rn);
169 }
170
171 /*
172 * bgp_dest_unlock_node
173 */
174 static inline void bgp_dest_unlock_node(struct bgp_dest *dest)
175 {
176 bgp_delete_listnode(dest);
177 route_unlock_node(bgp_dest_to_rnode(dest));
178 }
179
180 /*
181 * bgp_table_top_nolock
182 *
183 * Gets the top dest in the table without locking it.
184 *
185 * @see bgp_table_top
186 */
187 static inline struct bgp_dest *
188 bgp_table_top_nolock(const struct bgp_table *const table)
189 {
190 return bgp_dest_from_rnode(table->route_table->top);
191 }
192
193 /*
194 * bgp_table_top
195 */
196 static inline struct bgp_dest *
197 bgp_table_top(const struct bgp_table *const table)
198 {
199 return bgp_dest_from_rnode(route_top(table->route_table));
200 }
201
202 /*
203 * bgp_route_next
204 */
205 static inline struct bgp_dest *bgp_route_next(struct bgp_dest *dest)
206 {
207 return bgp_dest_from_rnode(route_next(bgp_dest_to_rnode(dest)));
208 }
209
210 /*
211 * bgp_route_next_until
212 */
213 static inline struct bgp_dest *bgp_route_next_until(struct bgp_dest *dest,
214 struct bgp_dest *limit)
215 {
216 struct route_node *rnode;
217
218 rnode = route_next_until(bgp_dest_to_rnode(dest),
219 bgp_dest_to_rnode(limit));
220
221 return bgp_dest_from_rnode(rnode);
222 }
223
224 /*
225 * bgp_node_get
226 */
227 static inline struct bgp_dest *bgp_node_get(struct bgp_table *const table,
228 const struct prefix *p)
229 {
230 return bgp_dest_from_rnode(route_node_get(table->route_table, p));
231 }
232
233 /*
234 * bgp_node_lookup
235 */
236 static inline struct bgp_dest *
237 bgp_node_lookup(const struct bgp_table *const table, const struct prefix *p)
238 {
239 struct route_node *rn = route_node_lookup(table->route_table, p);
240
241 return bgp_dest_from_rnode(rn);
242 }
243
244 /*
245 * bgp_dest_lock_node
246 */
247 static inline struct bgp_dest *bgp_dest_lock_node(struct bgp_dest *dest)
248 {
249 struct route_node *rn = route_lock_node(bgp_dest_to_rnode(dest));
250
251 return bgp_dest_from_rnode(rn);
252 }
253
254 /*
255 * bgp_node_match
256 */
257 static inline struct bgp_dest *bgp_node_match(const struct bgp_table *table,
258 const struct prefix *p)
259 {
260 struct route_node *rn = route_node_match(table->route_table, p);
261
262 return bgp_dest_from_rnode(rn);
263 }
264
265 /*
266 * bgp_node_match_ipv4
267 */
268 static inline struct bgp_dest *
269 bgp_node_match_ipv4(const struct bgp_table *table, struct in_addr *addr)
270 {
271 struct route_node *rn = route_node_match_ipv4(table->route_table, addr);
272
273 return bgp_dest_from_rnode(rn);
274 }
275
276 /*
277 * bgp_node_match_ipv6
278 */
279 static inline struct bgp_dest *
280 bgp_node_match_ipv6(const struct bgp_table *table, struct in6_addr *addr)
281 {
282 struct route_node *rn = route_node_match_ipv6(table->route_table, addr);
283
284 return bgp_dest_from_rnode(rn);
285 }
286
287 static inline unsigned long bgp_table_count(const struct bgp_table *const table)
288 {
289 return route_table_count(table->route_table);
290 }
291
292 /*
293 * bgp_table_get_next
294 */
295 static inline struct bgp_dest *bgp_table_get_next(const struct bgp_table *table,
296 const struct prefix *p)
297 {
298 return bgp_dest_from_rnode(route_table_get_next(table->route_table, p));
299 }
300
301 /*
302 * bgp_table_iter_init
303 */
304 static inline void bgp_table_iter_init(bgp_table_iter_t *iter,
305 struct bgp_table *table)
306 {
307 bgp_table_lock(table);
308 iter->table = table;
309 route_table_iter_init(&iter->rt_iter, table->route_table);
310 }
311
312 /*
313 * bgp_table_iter_next
314 */
315 static inline struct bgp_dest *bgp_table_iter_next(bgp_table_iter_t *iter)
316 {
317 return bgp_dest_from_rnode(route_table_iter_next(&iter->rt_iter));
318 }
319
320 /*
321 * bgp_table_iter_cleanup
322 */
323 static inline void bgp_table_iter_cleanup(bgp_table_iter_t *iter)
324 {
325 route_table_iter_cleanup(&iter->rt_iter);
326 bgp_table_unlock(iter->table);
327 iter->table = NULL;
328 }
329
330 /*
331 * bgp_table_iter_pause
332 */
333 static inline void bgp_table_iter_pause(bgp_table_iter_t *iter)
334 {
335 route_table_iter_pause(&iter->rt_iter);
336 }
337
338 /*
339 * bgp_table_iter_is_done
340 */
341 static inline int bgp_table_iter_is_done(bgp_table_iter_t *iter)
342 {
343 return route_table_iter_is_done(&iter->rt_iter);
344 }
345
346 /*
347 * bgp_table_iter_started
348 */
349 static inline int bgp_table_iter_started(bgp_table_iter_t *iter)
350 {
351 return route_table_iter_started(&iter->rt_iter);
352 }
353
354 /* This would benefit from a real atomic operation...
355 * until then. */
356 static inline uint64_t bgp_table_next_version(struct bgp_table *table)
357 {
358 return ++table->version;
359 }
360
361 static inline uint64_t bgp_table_version(struct bgp_table *table)
362 {
363 return table->version;
364 }
365
366 /* Find the subtree of the prefix p
367 *
368 * This will return the first node that belongs the the subtree of p. Including
369 * p itself, if it is in the tree.
370 *
371 * If the subtree is not present in the table, NULL is returned.
372 */
373 struct bgp_dest *bgp_table_subtree_lookup(const struct bgp_table *table,
374 const struct prefix *p);
375
376 static inline struct bgp_aggregate *
377 bgp_dest_get_bgp_aggregate_info(struct bgp_dest *dest)
378 {
379 return dest ? dest->info : NULL;
380 }
381
382 static inline void
383 bgp_dest_set_bgp_aggregate_info(struct bgp_dest *dest,
384 struct bgp_aggregate *aggregate)
385 {
386 dest->info = aggregate;
387 }
388
389 static inline struct bgp_distance *
390 bgp_dest_get_bgp_distance_info(struct bgp_dest *dest)
391 {
392 return dest ? dest->info : NULL;
393 }
394
395 static inline void bgp_dest_set_bgp_distance_info(struct bgp_dest *dest,
396 struct bgp_distance *distance)
397 {
398 dest->info = distance;
399 }
400
401 static inline struct bgp_static *
402 bgp_dest_get_bgp_static_info(struct bgp_dest *dest)
403 {
404 return dest ? dest->info : NULL;
405 }
406
407 static inline void bgp_dest_set_bgp_static_info(struct bgp_dest *dest,
408 struct bgp_static *bgp_static)
409 {
410 dest->info = bgp_static;
411 }
412
413 static inline struct bgp_connected_ref *
414 bgp_dest_get_bgp_connected_ref_info(struct bgp_dest *dest)
415 {
416 return dest ? dest->info : NULL;
417 }
418
419 static inline void
420 bgp_dest_set_bgp_connected_ref_info(struct bgp_dest *dest,
421 struct bgp_connected_ref *bc)
422 {
423 dest->info = bc;
424 }
425
426 static inline struct bgp_nexthop_cache *
427 bgp_dest_get_bgp_nexthop_info(struct bgp_dest *dest)
428 {
429 return dest ? dest->info : NULL;
430 }
431
432 static inline void bgp_dest_set_bgp_nexthop_info(struct bgp_dest *dest,
433 struct bgp_nexthop_cache *bnc)
434 {
435 dest->info = bnc;
436 }
437
438 static inline struct bgp_path_info *
439 bgp_dest_get_bgp_path_info(struct bgp_dest *dest)
440 {
441 return dest ? dest->info : NULL;
442 }
443
444 static inline void bgp_dest_set_bgp_path_info(struct bgp_dest *dest,
445 struct bgp_path_info *bi)
446 {
447 dest->info = bi;
448 }
449
450 static inline struct bgp_table *
451 bgp_dest_get_bgp_table_info(struct bgp_dest *dest)
452 {
453 return dest->info;
454 }
455
456 static inline void bgp_dest_set_bgp_table_info(struct bgp_dest *dest,
457 struct bgp_table *table)
458 {
459 dest->info = table;
460 }
461
462 static inline bool bgp_dest_has_bgp_path_info_data(struct bgp_dest *dest)
463 {
464 return !!dest->info;
465 }
466
467 static inline const struct prefix *bgp_dest_get_prefix(const struct bgp_dest *dest)
468 {
469 return &dest->p;
470 }
471
472 static inline unsigned int bgp_dest_get_lock_count(const struct bgp_dest *dest)
473 {
474 return dest->lock;
475 }
476
477 #ifdef _FRR_ATTRIBUTE_PRINTFRR
478 #pragma FRR printfrr_ext "%pRN" (struct bgp_node *)
479 #pragma FRR printfrr_ext "%pBD" (struct bgp_dest *)
480 #endif
481
482 #endif /* _QUAGGA_BGP_TABLE_H */