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