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