]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mpls.h
lib, zebra: add new MPLS zapi message with route replace semantics
[mirror_frr.git] / zebra / zebra_mpls.h
1 /*
2 * Zebra MPLS Data structures and definitions
3 * Copyright (C) 2015 Cumulus Networks, Inc.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _ZEBRA_MPLS_H
23 #define _ZEBRA_MPLS_H
24
25 #include "prefix.h"
26 #include "table.h"
27 #include "queue.h"
28 #include "hash.h"
29 #include "jhash.h"
30 #include "nexthop.h"
31 #include "vty.h"
32 #include "memory.h"
33 #include "mpls.h"
34 #include "zebra/zserv.h"
35 #include "zebra/zebra_vrf.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /* Definitions and macros. */
42
43 #define NHLFE_FAMILY(nhlfe) \
44 (((nhlfe)->nexthop->type == NEXTHOP_TYPE_IPV6 \
45 || (nhlfe)->nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) \
46 ? AF_INET6 \
47 : AF_INET)
48
49 /* Typedefs */
50
51 typedef struct zebra_ile_t_ zebra_ile_t;
52 typedef struct zebra_snhlfe_t_ zebra_snhlfe_t;
53 typedef struct zebra_slsp_t_ zebra_slsp_t;
54 typedef struct zebra_nhlfe_t_ zebra_nhlfe_t;
55 typedef struct zebra_lsp_t_ zebra_lsp_t;
56 typedef struct zebra_fec_t_ zebra_fec_t;
57
58 /*
59 * (Outgoing) nexthop label forwarding entry configuration
60 */
61 struct zebra_snhlfe_t_ {
62 /* Nexthop information */
63 enum nexthop_types_t gtype;
64 union g_addr gate;
65 char *ifname;
66 ifindex_t ifindex;
67
68 /* Out label. */
69 mpls_label_t out_label;
70
71 /* Backpointer to base entry. */
72 zebra_slsp_t *slsp;
73
74 /* Pointers to more outgoing information for same in-label */
75 zebra_snhlfe_t *next;
76 zebra_snhlfe_t *prev;
77 };
78
79 /*
80 * (Outgoing) nexthop label forwarding entry
81 */
82 struct zebra_nhlfe_t_ {
83 /* Type of entry - static etc. */
84 enum lsp_types_t type;
85
86 /* Nexthop information (with outgoing label) */
87 struct nexthop *nexthop;
88
89 /* Backpointer to base entry. */
90 zebra_lsp_t *lsp;
91
92 /* Runtime info - flags, pointers etc. */
93 uint32_t flags;
94 #define NHLFE_FLAG_CHANGED (1 << 0)
95 #define NHLFE_FLAG_SELECTED (1 << 1)
96 #define NHLFE_FLAG_MULTIPATH (1 << 2)
97 #define NHLFE_FLAG_DELETED (1 << 3)
98 #define NHLFE_FLAG_INSTALLED (1 << 4)
99
100 zebra_nhlfe_t *next;
101 zebra_nhlfe_t *prev;
102 uint8_t distance;
103 };
104
105 /*
106 * Incoming label entry
107 */
108 struct zebra_ile_t_ {
109 mpls_label_t in_label;
110 };
111
112 /*
113 * Label swap entry static configuration.
114 */
115 struct zebra_slsp_t_ {
116 /* Incoming label */
117 zebra_ile_t ile;
118
119 /* List of outgoing nexthop static configuration */
120 zebra_snhlfe_t *snhlfe_list;
121 };
122
123 /*
124 * Label swap entry (ile -> list of nhlfes)
125 */
126 struct zebra_lsp_t_ {
127 /* Incoming label */
128 zebra_ile_t ile;
129
130 /* List of NHLFE, pointer to best and num equal-cost. */
131 zebra_nhlfe_t *nhlfe_list;
132 zebra_nhlfe_t *best_nhlfe;
133 uint32_t num_ecmp;
134
135 /* Flags */
136 uint32_t flags;
137 #define LSP_FLAG_SCHEDULED (1 << 0)
138 #define LSP_FLAG_INSTALLED (1 << 1)
139 #define LSP_FLAG_CHANGED (1 << 2)
140
141 /* Address-family of NHLFE - saved here for delete. All NHLFEs */
142 /* have to be of the same AF */
143 uint8_t addr_family;
144 };
145
146 /*
147 * FEC to label binding.
148 */
149 struct zebra_fec_t_ {
150 /* FEC (prefix) */
151 struct route_node *rn;
152
153 /* In-label - either statically bound or derived from label block. */
154 mpls_label_t label;
155
156 /* Label index (into global label block), if valid */
157 uint32_t label_index;
158
159 /* Flags. */
160 uint32_t flags;
161 #define FEC_FLAG_CONFIGURED (1 << 0)
162
163 /* Clients interested in this FEC. */
164 struct list *client_list;
165 };
166
167 /* Function declarations. */
168
169 /*
170 * Add/update global label block.
171 */
172 int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, uint32_t start_label,
173 uint32_t end_label);
174
175 /*
176 * Delete global label block.
177 */
178 int zebra_mpls_label_block_del(struct zebra_vrf *vrf);
179
180 /*
181 * Display MPLS global label block configuration (VTY command handler).
182 */
183 int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *vrf);
184
185 /*
186 * Install dynamic LSP entry.
187 */
188 int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
189 struct route_entry *re);
190
191 /*
192 * Uninstall dynamic LSP entry, if any.
193 */
194 int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
195 struct route_entry *re);
196
197 /* Add an NHLFE to an LSP, return the newly-added object */
198 zebra_nhlfe_t *zebra_mpls_lsp_add_nhlfe(zebra_lsp_t *lsp,
199 enum lsp_types_t lsp_type,
200 enum nexthop_types_t gtype,
201 union g_addr *gate,
202 ifindex_t ifindex,
203 mpls_label_t out_label);
204
205 /* Free an allocated NHLFE */
206 void zebra_mpls_nhlfe_del(zebra_nhlfe_t *nhlfe);
207
208 int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
209 uint32_t label, uint32_t label_index,
210 struct zserv *client);
211
212 /*
213 * Deregistration from a client for the label binding for a FEC. The FEC
214 * itself is deleted if no other registered clients exist and there is no
215 * label bound to the FEC.
216 */
217 int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
218 struct zserv *client);
219
220 /*
221 * Return FEC (if any) to which this label is bound.
222 * Note: Only works for per-prefix binding and when the label is not
223 * implicit-null.
224 * TODO: Currently walks entire table, can optimize later with another
225 * hash..
226 */
227 zebra_fec_t *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
228 mpls_label_t label);
229
230 /*
231 * Inform if specified label is currently bound to a FEC or not.
232 */
233 int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label);
234
235 /*
236 * Add static FEC to label binding. If there are clients registered for this
237 * FEC, notify them. If there are labeled routes for this FEC, install the
238 * label forwarding entry.
239 */
240 int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
241 mpls_label_t in_label);
242
243 /*
244 * Remove static FEC to label binding. If there are no clients registered
245 * for this FEC, delete the FEC; else notify clients.
246 * Note: Upon delete of static binding, if label index exists for this FEC,
247 * client may need to be updated with derived label.
248 */
249 int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p);
250
251 /*
252 * Display MPLS FEC to label binding configuration (VTY command handler).
253 */
254 int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf);
255
256 /*
257 * Display MPLS FEC to label binding (VTY command handler).
258 */
259 void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf);
260
261 /*
262 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
263 */
264 void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
265 struct prefix *p);
266
267 /*
268 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
269 */
270 int mpls_ftn_update(int add, struct zebra_vrf *zvrf, enum lsp_types_t type,
271 struct prefix *prefix, enum nexthop_types_t gtype,
272 union g_addr *gate, ifindex_t ifindex, uint8_t route_type,
273 unsigned short route_instance, mpls_label_t out_label);
274
275 /*
276 * Uninstall all NHLFEs bound to a single FEC.
277 */
278 int mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
279 struct prefix *prefix, uint8_t route_type,
280 unsigned short route_instance);
281
282 /*
283 * Install/update a NHLFE for an LSP in the forwarding table. This may be
284 * a new LSP entry or a new NHLFE for an existing in-label or an update of
285 * the out-label for an existing NHLFE (update case).
286 */
287 int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
288 mpls_label_t in_label, mpls_label_t out_label,
289 enum nexthop_types_t gtype, union g_addr *gate,
290 ifindex_t ifindex);
291
292 /*
293 * Uninstall a particular NHLFE in the forwarding table. If this is
294 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
295 */
296 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
297 mpls_label_t in_label, enum nexthop_types_t gtype,
298 union g_addr *gate, ifindex_t ifindex);
299
300 /*
301 * Uninstall all NHLFEs for a particular LSP forwarding entry.
302 */
303 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
304 mpls_label_t in_label);
305
306 /*
307 * Uninstall all Segment Routing NHLFEs for a particular LSP forwarding entry.
308 * If no other NHLFEs exist, the entry would be deleted.
309 */
310 void mpls_sr_lsp_uninstall_all(struct hash_bucket *bucket, void *ctxt);
311
312 #if defined(HAVE_CUMULUS)
313 /*
314 * Check that the label values used in LSP creation are consistent. The
315 * main criteria is that if there is ECMP, the label operation must still
316 * be consistent - i.e., all paths either do a swap or do PHP. This is due
317 * to current HW restrictions.
318 */
319 int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
320 mpls_label_t in_label,
321 mpls_label_t out_label,
322 enum nexthop_types_t gtype,
323 union g_addr *gate, ifindex_t ifindex);
324 #endif /* HAVE_CUMULUS */
325
326 /*
327 * Add static LSP entry. This may be the first entry for this incoming label
328 * or an additional nexthop; an existing entry may also have outgoing label
329 * changed.
330 * Note: The label operation (swap or PHP) is common for the LSP entry (all
331 * NHLFEs).
332 */
333 int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
334 mpls_label_t out_label,
335 enum nexthop_types_t gtype, union g_addr *gate,
336 ifindex_t ifindex);
337
338 /*
339 * Delete static LSP entry. This may be the delete of one particular
340 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
341 * all NHLFEs).
342 * NOTE: Delete of the only NHLFE will also end up deleting the entire
343 * LSP configuration.
344 */
345 int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
346 enum nexthop_types_t gtype, union g_addr *gate,
347 ifindex_t ifindex);
348
349 /*
350 * Process LSP update results from zebra dataplane.
351 */
352 /* Forward ref of dplane update context type */
353 struct zebra_dplane_ctx;
354
355 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx *ctx);
356
357 /* Process async dplane notifications. */
358 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx *ctx);
359
360 /*
361 * Schedule all MPLS label forwarding entries for processing.
362 * Called upon changes that may affect one or more of them such as
363 * interface or nexthop state changes.
364 */
365 void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf);
366
367 /*
368 * Display MPLS label forwarding table for a specific LSP
369 * (VTY command handler).
370 */
371 void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
372 mpls_label_t label, bool use_json);
373
374 /*
375 * Display MPLS label forwarding table (VTY command handler).
376 */
377 void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
378 bool use_json);
379
380 /*
381 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
382 */
383 int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf);
384
385 /*
386 * Called when VRF becomes inactive, cleans up information but keeps
387 * the table itself.
388 * NOTE: Currently supported only for default VRF.
389 */
390 void zebra_mpls_cleanup_tables(struct zebra_vrf *zvrf);
391
392 /*
393 * Called upon process exiting, need to delete LSP forwarding
394 * entries from the kernel.
395 * NOTE: Currently supported only for default VRF.
396 */
397 void zebra_mpls_close_tables(struct zebra_vrf *zvrf);
398
399 /*
400 * Allocate MPLS tables for this VRF.
401 * NOTE: Currently supported only for default VRF.
402 */
403 void zebra_mpls_init_tables(struct zebra_vrf *zvrf);
404
405 /*
406 * Global MPLS initialization.
407 */
408 void zebra_mpls_init(void);
409
410 /*
411 * MPLS VTY.
412 */
413 void zebra_mpls_vty_init(void);
414
415 /* Inline functions. */
416
417 /*
418 * Distance (priority) definition for LSP NHLFE.
419 */
420 static inline uint8_t lsp_distance(enum lsp_types_t type)
421 {
422 switch (type) {
423 case ZEBRA_LSP_STATIC:
424 return (route_distance(ZEBRA_ROUTE_STATIC));
425 case ZEBRA_LSP_LDP:
426 return (route_distance(ZEBRA_ROUTE_LDP));
427 case ZEBRA_LSP_BGP:
428 return (route_distance(ZEBRA_ROUTE_BGP));
429 case ZEBRA_LSP_NONE:
430 case ZEBRA_LSP_SHARP:
431 case ZEBRA_LSP_OSPF_SR:
432 return 150;
433 }
434
435 /*
436 * For some reason certain compilers do not believe
437 * that all the cases have been handled. And
438 * WTF does this work differently than when I removed
439 * the default case????
440 */
441 return 150;
442 }
443
444 /*
445 * Map RIB type to LSP type. Used when labeled-routes from BGP
446 * are converted into LSPs.
447 */
448 static inline enum lsp_types_t lsp_type_from_re_type(int re_type)
449 {
450 switch (re_type) {
451 case ZEBRA_ROUTE_STATIC:
452 return ZEBRA_LSP_STATIC;
453 case ZEBRA_ROUTE_LDP:
454 return ZEBRA_LSP_LDP;
455 case ZEBRA_ROUTE_BGP:
456 return ZEBRA_LSP_BGP;
457 case ZEBRA_ROUTE_OSPF:
458 return ZEBRA_LSP_OSPF_SR;
459 case ZEBRA_ROUTE_SHARP:
460 return ZEBRA_LSP_SHARP;
461 default:
462 return ZEBRA_LSP_NONE;
463 }
464 }
465
466 /*
467 * Map LSP type to RIB type.
468 */
469 static inline int re_type_from_lsp_type(enum lsp_types_t lsp_type)
470 {
471 switch (lsp_type) {
472 case ZEBRA_LSP_STATIC:
473 return ZEBRA_ROUTE_STATIC;
474 case ZEBRA_LSP_LDP:
475 return ZEBRA_ROUTE_LDP;
476 case ZEBRA_LSP_BGP:
477 return ZEBRA_ROUTE_BGP;
478 case ZEBRA_LSP_OSPF_SR:
479 return ZEBRA_ROUTE_OSPF;
480 case ZEBRA_LSP_NONE:
481 return ZEBRA_ROUTE_KERNEL;
482 case ZEBRA_LSP_SHARP:
483 return ZEBRA_ROUTE_SHARP;
484 }
485
486 /*
487 * For some reason certain compilers do not believe
488 * that all the cases have been handled. And
489 * WTF does this work differently than when I removed
490 * the default case????
491 */
492 return ZEBRA_ROUTE_KERNEL;
493 }
494
495 /* NHLFE type as printable string. */
496 static inline const char *nhlfe_type2str(enum lsp_types_t lsp_type)
497 {
498 switch (lsp_type) {
499 case ZEBRA_LSP_STATIC:
500 return "Static";
501 case ZEBRA_LSP_LDP:
502 return "LDP";
503 case ZEBRA_LSP_BGP:
504 return "BGP";
505 case ZEBRA_LSP_OSPF_SR:
506 return "SR (OSPF)";
507 case ZEBRA_LSP_SHARP:
508 return "SHARP";
509 case ZEBRA_LSP_NONE:
510 return "Unknown";
511 }
512
513 /*
514 * For some reason certain compilers do not believe
515 * that all the cases have been handled. And
516 * WTF does this work differently than when I removed
517 * the default case????
518 */
519 return "Unknown";
520 }
521
522 static inline void mpls_mark_lsps_for_processing(struct zebra_vrf *zvrf,
523 struct prefix *p)
524 {
525 struct route_table *table;
526 struct route_node *rn;
527 rib_dest_t *dest;
528
529 if (!zvrf)
530 return;
531
532 table = zvrf->table[family2afi(p->family)][SAFI_UNICAST];
533 if (!table)
534 return;
535
536 rn = route_node_match(table, p);
537 if (!rn)
538 return;
539
540
541 dest = rib_dest_from_rnode(rn);
542 SET_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS);
543 }
544
545 static inline void mpls_unmark_lsps_for_processing(struct route_node *rn)
546 {
547 rib_dest_t *dest = rib_dest_from_rnode(rn);
548
549 UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS);
550 }
551
552 static inline int mpls_should_lsps_be_processed(struct route_node *rn)
553 {
554 rib_dest_t *dest = rib_dest_from_rnode(rn);
555
556 return !!CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS);
557 }
558
559 /* Global variables. */
560 extern int mpls_enabled;
561
562 #ifdef __cplusplus
563 }
564 #endif
565
566 #endif /*_ZEBRA_MPLS_H */