2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
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
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.
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
29 #include "sockunion.h"
32 #include "workqueue.h"
37 #include "termtable.h"
40 #include "zebra/rib.h"
42 #include "zebra/interface.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/zebra_mpls.h"
50 #include "zebra/zebra_errors.h"
52 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
55 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
56 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
57 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
61 /* static function declarations */
63 static void fec_evaluate(struct zebra_vrf
*zvrf
);
64 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
66 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
67 struct route_node
*rn
, struct route_entry
*re
);
68 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
69 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
70 mpls_label_t old_label
);
71 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
72 static void fec_update_clients(zebra_fec_t
*fec
);
73 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
74 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
75 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
76 mpls_label_t label
, uint32_t flags
,
77 uint32_t label_index
);
78 static int fec_del(zebra_fec_t
*fec
);
80 static unsigned int label_hash(const void *p
);
81 static bool label_cmp(const void *p1
, const void *p2
);
82 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
85 struct nexthop
*nexthop
);
86 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
88 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
89 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
90 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
91 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
92 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
93 static void lsp_processq_complete(struct work_queue
*wq
);
94 static int lsp_processq_add(zebra_lsp_t
*lsp
);
95 static void *lsp_alloc(void *p
);
97 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
98 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
99 union g_addr
*gate
, ifindex_t ifindex
);
100 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
101 enum nexthop_types_t gtype
, union g_addr
*gate
,
103 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
104 enum nexthop_types_t gtype
, union g_addr
*gate
,
105 ifindex_t ifindex
, mpls_label_t out_label
);
106 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
107 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
108 struct mpls_label_stack
*nh_label
);
109 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
110 enum lsp_types_t type
);
111 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
112 mpls_label_t in_label
);
113 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
114 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
115 static void *slsp_alloc(void *p
);
116 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
117 union g_addr
*gate
, ifindex_t ifindex
);
118 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
119 enum nexthop_types_t gtype
,
120 union g_addr
*gate
, ifindex_t ifindex
);
121 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
122 enum nexthop_types_t gtype
,
123 union g_addr
*gate
, ifindex_t ifindex
,
124 mpls_label_t out_label
);
125 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
126 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
127 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
128 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
129 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
130 int afi
, enum lsp_types_t lsp_type
);
133 /* Static functions */
136 * Handle failure in LSP install, clear flags for NHLFE.
138 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
140 zebra_nhlfe_t
*nhlfe
;
141 struct nexthop
*nexthop
;
143 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
144 nexthop
= nhlfe
->nexthop
;
148 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
149 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
154 * Install label forwarding entry based on labeled-route entry.
156 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
157 struct route_node
*rn
, struct route_entry
*re
)
159 struct hash
*lsp_table
;
162 zebra_nhlfe_t
*nhlfe
;
163 struct nexthop
*nexthop
;
164 enum lsp_types_t lsp_type
;
169 lsp_table
= zvrf
->lsp_table
;
173 lsp_type
= lsp_type_from_re_type(re
->type
);
176 /* Locate or allocate LSP entry. */
177 tmp_ile
.in_label
= label
;
178 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
182 /* For each active nexthop, create NHLFE. Note that we deliberately skip
183 * recursive nexthops right now, because intermediate hops won't
185 * the label advertised by the recursive nexthop (plus we don't have the
186 * logic yet to push multiple labels).
188 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
189 /* Skip inactive and recursive entries. */
190 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
192 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
195 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
198 /* Clear deleted flag (in case it was set) */
199 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
200 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
205 if (IS_ZEBRA_DEBUG_MPLS
) {
206 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
208 "LSP in-label %u type %d nexthop %s "
210 lsp
->ile
.in_label
, lsp_type
, buf
);
213 /* Update out label, trigger processing. */
214 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
215 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
218 /* Add LSP entry to this nexthop */
219 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
220 &nexthop
->gate
, nexthop
->ifindex
,
221 nexthop
->nh_label
->label
[0]);
225 if (IS_ZEBRA_DEBUG_MPLS
) {
226 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
228 "Add LSP in-label %u type %d nexthop %s "
230 lsp
->ile
.in_label
, lsp_type
, buf
,
231 nexthop
->nh_label
->label
[0]);
234 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
236 /* Mark NHLFE as changed. */
237 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
242 /* Queue LSP for processing if necessary. If no NHLFE got added (special
243 * case), delete the LSP entry; this case results in somewhat ugly
246 if (added
|| changed
) {
247 if (lsp_processq_add(lsp
))
249 } else if (!lsp
->nhlfe_list
250 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
251 if (IS_ZEBRA_DEBUG_MPLS
)
252 zlog_debug("Free LSP in-label %u flags 0x%x",
253 lsp
->ile
.in_label
, lsp
->flags
);
255 lsp
= hash_release(lsp_table
, &lsp
->ile
);
256 XFREE(MTYPE_LSP
, lsp
);
263 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
264 * NHLFEs are removed, the entire entry is deleted.
266 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
268 struct hash
*lsp_table
;
271 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
275 lsp_table
= zvrf
->lsp_table
;
279 /* If entry is not present, exit. */
280 tmp_ile
.in_label
= label
;
281 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
282 if (!lsp
|| !lsp
->nhlfe_list
)
285 /* Mark NHLFEs for delete or directly delete, as appropriate. */
286 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
287 nhlfe_next
= nhlfe
->next
;
289 /* Skip static NHLFEs */
290 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
293 if (IS_ZEBRA_DEBUG_MPLS
) {
294 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
296 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
297 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
300 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
301 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
302 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
308 /* Queue LSP for processing, if needed, else delete. */
309 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
310 if (lsp_processq_add(lsp
))
312 } else if (!lsp
->nhlfe_list
313 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
314 if (IS_ZEBRA_DEBUG_MPLS
)
315 zlog_debug("Del LSP in-label %u flags 0x%x",
316 lsp
->ile
.in_label
, lsp
->flags
);
318 lsp
= hash_release(lsp_table
, &lsp
->ile
);
319 XFREE(MTYPE_LSP
, lsp
);
326 * This function is invoked upon change to label block configuration; it
327 * will walk all registered FECs with label-index and appropriately update
328 * their local labels and trigger client updates.
330 static void fec_evaluate(struct zebra_vrf
*zvrf
)
332 struct route_node
*rn
;
334 uint32_t old_label
, new_label
;
338 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
339 if (zvrf
->fec_table
[af
] == NULL
)
342 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
343 rn
= route_next(rn
)) {
344 if ((fec
= rn
->info
) == NULL
)
347 /* Skip configured FECs and those without a label index.
349 if (fec
->flags
& FEC_FLAG_CONFIGURED
350 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
353 if (IS_ZEBRA_DEBUG_MPLS
)
354 prefix2str(&rn
->p
, buf
, BUFSIZ
);
356 /* Save old label, determine new label. */
357 old_label
= fec
->label
;
359 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
360 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
361 new_label
= MPLS_INVALID_LABEL
;
363 /* If label has changed, update FEC and clients. */
364 if (new_label
== old_label
)
367 if (IS_ZEBRA_DEBUG_MPLS
)
369 "Update fec %s new label %u upon label block",
372 fec
->label
= new_label
;
373 fec_update_clients(fec
);
375 /* Update label forwarding entries appropriately */
376 fec_change_update_lsp(zvrf
, fec
, old_label
);
382 * Derive (if possible) and update the local label for the FEC based on
383 * its label index. The index is "acceptable" if it falls within the
384 * globally configured label block (SRGB).
386 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
391 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
392 && zvrf
->mpls_srgb
.start_label
393 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
394 < zvrf
->mpls_srgb
.end_label
))
397 fec
->label
= MPLS_INVALID_LABEL
;
403 * There is a change for this FEC. Install or uninstall label forwarding
404 * entries, as appropriate.
406 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
407 mpls_label_t old_label
)
409 struct route_table
*table
;
410 struct route_node
*rn
;
411 struct route_entry
*re
;
414 /* Uninstall label forwarding entry, if previously installed. */
415 if (old_label
!= MPLS_INVALID_LABEL
416 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
417 lsp_uninstall(zvrf
, old_label
);
419 /* Install label forwarding entry corr. to new label, if needed. */
420 if (fec
->label
== MPLS_INVALID_LABEL
421 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
424 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
425 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
429 /* See if labeled route exists. */
430 rn
= route_node_lookup(table
, &fec
->rn
->p
);
434 RNODE_FOREACH_RE (rn
, re
) {
435 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
439 if (!re
|| !zebra_rib_labeled_unicast(re
))
442 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
449 * Inform about FEC to a registered client.
451 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
454 struct route_node
*rn
;
458 /* Get output stream. */
459 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
461 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
463 stream_putw(s
, rn
->p
.family
);
464 stream_put_prefix(s
, &rn
->p
);
465 stream_putl(s
, fec
->label
);
466 stream_putw_at(s
, 0, stream_get_endp(s
));
467 return zserv_send_message(client
, s
);
471 * Update all registered clients about this FEC. Caller should've updated
472 * FEC and ensure no duplicate updates.
474 static void fec_update_clients(zebra_fec_t
*fec
)
476 struct listnode
*node
;
477 struct zserv
*client
;
479 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
480 if (IS_ZEBRA_DEBUG_MPLS
)
481 zlog_debug("Update client %s",
482 zebra_route_string(client
->proto
));
483 fec_send(fec
, client
);
489 * Print a FEC-label binding entry.
491 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
493 struct route_node
*rn
;
494 struct listnode
*node
;
495 struct zserv
*client
;
499 prefix2str(&rn
->p
, buf
, BUFSIZ
);
500 vty_out(vty
, "%s\n", buf
);
501 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
502 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
503 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
505 if (!list_isempty(fec
->client_list
)) {
506 vty_out(vty
, " Client list:");
507 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
508 vty_out(vty
, " %s(fd %d)",
509 zebra_route_string(client
->proto
),
516 * Locate FEC-label binding that matches with passed info.
518 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
520 struct route_node
*rn
;
523 rn
= route_node_lookup(table
, p
);
527 route_unlock_node(rn
);
532 * Add a FEC. This may be upon a client registering for a binding
533 * or when a binding is configured.
535 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
536 mpls_label_t label
, uint32_t flags
,
537 uint32_t label_index
)
539 struct route_node
*rn
;
544 /* Lookup (or add) route node.*/
545 rn
= route_node_get(table
, p
);
552 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
557 fec
->client_list
= list_new();
559 route_unlock_node(rn
); /* for the route_node_get */
561 fec
->label_index
= label_index
;
568 * Delete a FEC. This may be upon the last client deregistering for
569 * a FEC and no binding exists or when the binding is deleted and there
570 * are no registered clients.
572 static int fec_del(zebra_fec_t
*fec
)
574 list_delete(&fec
->client_list
);
575 fec
->rn
->info
= NULL
;
576 route_unlock_node(fec
->rn
);
577 XFREE(MTYPE_FEC
, fec
);
582 * Hash function for label.
584 static unsigned int label_hash(const void *p
)
586 const zebra_ile_t
*ile
= p
;
588 return (jhash_1word(ile
->in_label
, 0));
592 * Compare 2 LSP hash entries based on in-label.
594 static bool label_cmp(const void *p1
, const void *p2
)
596 const zebra_ile_t
*ile1
= p1
;
597 const zebra_ile_t
*ile2
= p2
;
599 return (ile1
->in_label
== ile2
->in_label
);
603 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
605 * NOTE: Looking only for connected routes right now.
607 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
608 struct nexthop
*nexthop
)
610 struct route_table
*table
;
611 struct prefix_ipv4 p
;
612 struct route_node
*rn
;
613 struct route_entry
*match
;
614 struct nexthop
*match_nh
;
616 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
620 /* Lookup nexthop in IPv4 routing table. */
621 memset(&p
, 0, sizeof(struct prefix_ipv4
));
623 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
624 p
.prefix
= nexthop
->gate
.ipv4
;
626 rn
= route_node_match(table
, (struct prefix
*)&p
);
630 route_unlock_node(rn
);
632 /* Locate a valid connected route. */
633 RNODE_FOREACH_RE (rn
, match
) {
634 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
635 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
638 for (match_nh
= match
->ng
.nexthop
; match_nh
;
639 match_nh
= match_nh
->next
) {
640 if (match
->type
== ZEBRA_ROUTE_CONNECT
641 || nexthop
->ifindex
== match_nh
->ifindex
) {
642 nexthop
->ifindex
= match_nh
->ifindex
;
653 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
655 * NOTE: Looking only for connected routes right now.
657 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
658 struct nexthop
*nexthop
)
660 struct route_table
*table
;
661 struct prefix_ipv6 p
;
662 struct route_node
*rn
;
663 struct route_entry
*match
;
665 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
669 /* Lookup nexthop in IPv6 routing table. */
670 memset(&p
, 0, sizeof(struct prefix_ipv6
));
672 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
673 p
.prefix
= nexthop
->gate
.ipv6
;
675 rn
= route_node_match(table
, (struct prefix
*)&p
);
679 route_unlock_node(rn
);
681 /* Locate a valid connected route. */
682 RNODE_FOREACH_RE (rn
, match
) {
683 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
684 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
685 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
689 if (!match
|| !match
->ng
.nexthop
)
692 nexthop
->ifindex
= match
->ng
.nexthop
->ifindex
;
698 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
700 * NOTE: Each NHLFE points to only 1 nexthop.
702 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
704 struct nexthop
*nexthop
;
705 struct interface
*ifp
;
706 struct zebra_ns
*zns
;
708 nexthop
= nhlfe
->nexthop
;
709 if (!nexthop
) // unexpected
712 /* Check on nexthop based on type. */
713 switch (nexthop
->type
) {
714 case NEXTHOP_TYPE_IFINDEX
:
716 * Lookup if this type is special. The
717 * NEXTHOP_TYPE_IFINDEX is a pop and
718 * forward into a different table for
719 * processing. As such this ifindex
720 * passed to us may be a VRF device
721 * which will not be in the default
722 * VRF. So let's look in all of them
724 zns
= zebra_ns_lookup(NS_DEFAULT
);
725 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
726 if (ifp
&& if_is_operative(ifp
))
727 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
729 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 case NEXTHOP_TYPE_IPV4
:
732 case NEXTHOP_TYPE_IPV4_IFINDEX
:
733 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
734 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
736 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
739 case NEXTHOP_TYPE_IPV6
:
740 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
741 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
743 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
746 case NEXTHOP_TYPE_IPV6_IFINDEX
:
747 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
748 ifp
= if_lookup_by_index(nexthop
->ifindex
,
750 if (ifp
&& if_is_operative(ifp
))
751 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
753 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
755 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
756 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
758 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
766 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
770 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
771 * reachability and select the best. Multipath entries are also
772 * marked. This is invoked when an LSP scheduled for processing (due
773 * to some change) is examined.
775 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
777 zebra_nhlfe_t
*nhlfe
;
779 struct nexthop
*nexthop
;
787 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
790 * First compute the best path, after checking nexthop status. We are
792 * concerned with non-deleted NHLFEs.
794 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
795 /* Clear selection flags. */
796 UNSET_FLAG(nhlfe
->flags
,
797 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
799 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
800 && nhlfe_nexthop_active(nhlfe
)) {
801 if (!best
|| (nhlfe
->distance
< best
->distance
))
806 lsp
->best_nhlfe
= best
;
807 if (!lsp
->best_nhlfe
)
810 /* Mark best NHLFE as selected. */
811 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
814 * If best path exists, see if there is ECMP. While doing this, note if
816 * new (uninstalled) NHLFE has been selected, an installed entry that is
817 * still selected has a change or an installed entry is to be removed.
819 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
820 int nh_chg
, nh_sel
, nh_inst
;
822 nexthop
= nhlfe
->nexthop
;
823 if (!nexthop
) // unexpected
826 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
827 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
828 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
829 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
830 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
834 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
835 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
836 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
838 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
840 if ((nh_sel
&& !nh_inst
)
841 || (nh_sel
&& nh_inst
&& nh_chg
)
842 || (nh_inst
&& !nh_sel
))
846 /* We have finished examining, clear changed flag. */
847 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
851 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
855 * Delete LSP forwarding entry from kernel, if installed. Called upon
858 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
862 lsp
= (zebra_lsp_t
*)bucket
->data
;
863 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
864 (void)dplane_lsp_delete(lsp
);
868 * Schedule LSP forwarding entry for processing. Called upon changes
869 * that may impact LSPs such as nexthop / connected route changes.
871 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
875 lsp
= (zebra_lsp_t
*)bucket
->data
;
876 (void)lsp_processq_add(lsp
);
880 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
881 * any multipaths and update or delete from the kernel, as needed.
883 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
886 zebra_nhlfe_t
*oldbest
, *newbest
;
887 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
888 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
889 enum zebra_dplane_result res
;
891 lsp
= (zebra_lsp_t
*)data
;
892 if (!lsp
) // unexpected
895 oldbest
= lsp
->best_nhlfe
;
897 /* Select best NHLFE(s) */
898 lsp_select_best_nhlfe(lsp
);
900 newbest
= lsp
->best_nhlfe
;
902 if (IS_ZEBRA_DEBUG_MPLS
) {
904 nhlfe2str(oldbest
, buf
, BUFSIZ
);
906 nhlfe2str(newbest
, buf2
, BUFSIZ
);
908 "Process LSP in-label %u oldbest %s newbest %s "
909 "flags 0x%x ecmp# %d",
910 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
911 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
914 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
915 /* Not already installed */
918 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
920 switch (dplane_lsp_add(lsp
)) {
921 case ZEBRA_DPLANE_REQUEST_QUEUED
:
922 /* Set 'installed' flag so we will know
923 * that an install is in-flight.
925 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
927 zvrf
->lsp_installs_queued
++;
929 case ZEBRA_DPLANE_REQUEST_FAILURE
:
930 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
931 "LSP Install Failure: %u",
934 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
935 zvrf
->lsp_installs
++;
940 /* Installed, may need an update and/or delete. */
942 res
= dplane_lsp_delete(lsp
);
944 /* We do some of the lsp cleanup immediately for
947 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
948 clear_nhlfe_installed(lsp
);
951 case ZEBRA_DPLANE_REQUEST_QUEUED
:
952 zvrf
->lsp_removals_queued
++;
954 case ZEBRA_DPLANE_REQUEST_FAILURE
:
955 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
956 "LSP Deletion Failure: %u",
959 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
960 zvrf
->lsp_removals
++;
963 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
964 zebra_nhlfe_t
*nhlfe
;
965 struct nexthop
*nexthop
;
967 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
969 /* We leave the INSTALLED flag set here
970 * so we know an update in in-flight.
974 * Any NHLFE that was installed but is not
975 * selected now needs to have its flags updated.
977 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
978 nhlfe
= nhlfe
->next
) {
979 nexthop
= nhlfe
->nexthop
;
983 if (CHECK_FLAG(nhlfe
->flags
,
984 NHLFE_FLAG_INSTALLED
)
985 && !CHECK_FLAG(nhlfe
->flags
,
986 NHLFE_FLAG_SELECTED
)) {
987 UNSET_FLAG(nhlfe
->flags
,
988 NHLFE_FLAG_INSTALLED
);
989 UNSET_FLAG(nexthop
->flags
,
994 switch (dplane_lsp_update(lsp
)) {
995 case ZEBRA_DPLANE_REQUEST_QUEUED
:
996 zvrf
->lsp_installs_queued
++;
998 case ZEBRA_DPLANE_REQUEST_FAILURE
:
999 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1000 "LSP Update Failure: %u",
1003 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1004 zvrf
->lsp_installs
++;
1015 * Callback upon processing completion of a LSP forwarding entry.
1017 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1019 struct zebra_vrf
*zvrf
;
1021 struct hash
*lsp_table
;
1022 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1024 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1027 lsp_table
= zvrf
->lsp_table
;
1028 if (!lsp_table
) // unexpected
1031 lsp
= (zebra_lsp_t
*)data
;
1032 if (!lsp
) // unexpected
1035 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1037 * delete LSP entry also.
1039 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1041 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1042 nhlfe_next
= nhlfe
->next
;
1043 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1047 if (!lsp
->nhlfe_list
) {
1048 if (IS_ZEBRA_DEBUG_MPLS
)
1049 zlog_debug("Free LSP in-label %u flags 0x%x",
1050 lsp
->ile
.in_label
, lsp
->flags
);
1052 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1053 XFREE(MTYPE_LSP
, lsp
);
1058 * Callback upon finishing the processing of all scheduled
1059 * LSP forwarding entries.
1061 static void lsp_processq_complete(struct work_queue
*wq
)
1063 /* Nothing to do for now. */
1067 * Add LSP forwarding entry to queue for subsequent processing.
1069 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1071 /* If already scheduled, exit. */
1072 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1075 if (zrouter
.lsp_process_q
== NULL
) {
1076 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1077 "%s: work_queue does not exist!", __func__
);
1081 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1082 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1087 * Callback to allocate LSP forwarding table entry.
1089 static void *lsp_alloc(void *p
)
1091 const zebra_ile_t
*ile
= p
;
1094 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1097 if (IS_ZEBRA_DEBUG_MPLS
)
1098 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1100 return ((void *)lsp
);
1104 * Create printable string for NHLFE entry.
1106 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1108 struct nexthop
*nexthop
;
1111 nexthop
= nhlfe
->nexthop
;
1112 switch (nexthop
->type
) {
1113 case NEXTHOP_TYPE_IPV4
:
1114 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1115 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1117 case NEXTHOP_TYPE_IPV6
:
1118 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1119 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1121 case NEXTHOP_TYPE_IFINDEX
:
1122 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1131 * Check if NHLFE matches with search info passed.
1133 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1134 union g_addr
*gate
, ifindex_t ifindex
)
1136 struct nexthop
*nhop
;
1139 nhop
= nhlfe
->nexthop
;
1143 if (nhop
->type
!= gtype
)
1146 switch (nhop
->type
) {
1147 case NEXTHOP_TYPE_IPV4
:
1148 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1149 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1150 sizeof(struct in_addr
));
1151 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1152 cmp
= !(nhop
->ifindex
== ifindex
);
1154 case NEXTHOP_TYPE_IPV6
:
1155 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1156 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1157 sizeof(struct in6_addr
));
1158 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1159 cmp
= !(nhop
->ifindex
== ifindex
);
1161 case NEXTHOP_TYPE_IFINDEX
:
1162 cmp
= !(nhop
->ifindex
== ifindex
);
1173 * Locate NHLFE that matches with passed info.
1175 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1176 enum nexthop_types_t gtype
, union g_addr
*gate
,
1179 zebra_nhlfe_t
*nhlfe
;
1184 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1185 if (nhlfe
->type
!= lsp_type
)
1187 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1195 * Add NHLFE. Base entry must have been created and duplicate
1198 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1199 enum nexthop_types_t gtype
, union g_addr
*gate
,
1200 ifindex_t ifindex
, mpls_label_t out_label
)
1202 zebra_nhlfe_t
*nhlfe
;
1203 struct nexthop
*nexthop
;
1208 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1211 nhlfe
->type
= lsp_type
;
1212 nhlfe
->distance
= lsp_distance(lsp_type
);
1214 nexthop
= nexthop_new();
1216 XFREE(MTYPE_NHLFE
, nhlfe
);
1219 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1221 nexthop
->vrf_id
= VRF_DEFAULT
;
1222 nexthop
->type
= gtype
;
1223 switch (nexthop
->type
) {
1224 case NEXTHOP_TYPE_IPV4
:
1225 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1226 nexthop
->gate
.ipv4
= gate
->ipv4
;
1228 nexthop
->ifindex
= ifindex
;
1230 case NEXTHOP_TYPE_IPV6
:
1231 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1232 nexthop
->gate
.ipv6
= gate
->ipv6
;
1234 nexthop
->ifindex
= ifindex
;
1236 case NEXTHOP_TYPE_IFINDEX
:
1237 nexthop
->ifindex
= ifindex
;
1240 nexthop_free(nexthop
);
1241 XFREE(MTYPE_NHLFE
, nhlfe
);
1246 nhlfe
->nexthop
= nexthop
;
1247 if (lsp
->nhlfe_list
)
1248 lsp
->nhlfe_list
->prev
= nhlfe
;
1249 nhlfe
->next
= lsp
->nhlfe_list
;
1250 lsp
->nhlfe_list
= nhlfe
;
1256 * Delete NHLFE. Entry must be present on list.
1258 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1271 nexthop_free(nhlfe
->nexthop
);
1273 /* Unlink from LSP */
1275 nhlfe
->next
->prev
= nhlfe
->prev
;
1277 nhlfe
->prev
->next
= nhlfe
->next
;
1279 lsp
->nhlfe_list
= nhlfe
->next
;
1281 if (nhlfe
== lsp
->best_nhlfe
)
1282 lsp
->best_nhlfe
= NULL
;
1284 XFREE(MTYPE_NHLFE
, nhlfe
);
1290 * Update label for NHLFE entry.
1292 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1293 struct mpls_label_stack
*nh_label
)
1295 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1298 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1299 enum lsp_types_t type
)
1301 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1302 int schedule_lsp
= 0;
1305 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1306 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1307 nhlfe_next
= nhlfe
->next
;
1309 /* Skip non-static NHLFEs */
1310 if (nhlfe
->type
!= type
)
1313 if (IS_ZEBRA_DEBUG_MPLS
) {
1314 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1316 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1317 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1320 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1321 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1322 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1329 /* Queue LSP for processing, if needed, else delete. */
1331 if (lsp_processq_add(lsp
))
1333 } else if (!lsp
->nhlfe_list
1334 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1335 if (IS_ZEBRA_DEBUG_MPLS
)
1336 zlog_debug("Free LSP in-label %u flags 0x%x",
1337 lsp
->ile
.in_label
, lsp
->flags
);
1339 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1340 XFREE(MTYPE_LSP
, lsp
);
1347 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1348 * If no other NHLFEs exist, the entry would be deleted.
1350 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1351 mpls_label_t in_label
)
1353 struct hash
*lsp_table
;
1354 zebra_ile_t tmp_ile
;
1358 lsp_table
= zvrf
->lsp_table
;
1362 /* If entry is not present, exit. */
1363 tmp_ile
.in_label
= in_label
;
1364 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1365 if (!lsp
|| !lsp
->nhlfe_list
)
1368 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1371 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1374 json_object
*json_nhlfe
= NULL
;
1375 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1377 json_nhlfe
= json_object_new_object();
1378 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1379 json_object_int_add(json_nhlfe
, "outLabel",
1380 nexthop
->nh_label
->label
[0]);
1381 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1383 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1384 json_object_boolean_true_add(json_nhlfe
, "installed");
1386 switch (nexthop
->type
) {
1387 case NEXTHOP_TYPE_IPV4
:
1388 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1389 json_object_string_add(json_nhlfe
, "nexthop",
1390 inet_ntoa(nexthop
->gate
.ipv4
));
1392 case NEXTHOP_TYPE_IPV6
:
1393 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1394 json_object_string_add(
1395 json_nhlfe
, "nexthop",
1396 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1398 if (nexthop
->ifindex
)
1399 json_object_string_add(json_nhlfe
, "interface",
1400 ifindex2ifname(nexthop
->ifindex
,
1410 * Print the NHLFE for a LSP forwarding entry.
1412 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1414 struct nexthop
*nexthop
;
1417 nexthop
= nhlfe
->nexthop
;
1418 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1421 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1422 nhlfe_type2str(nhlfe
->type
),
1423 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1425 switch (nexthop
->type
) {
1426 case NEXTHOP_TYPE_IPV4
:
1427 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1428 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1429 if (nexthop
->ifindex
)
1430 vty_out(vty
, " dev %s",
1431 ifindex2ifname(nexthop
->ifindex
,
1434 case NEXTHOP_TYPE_IPV6
:
1435 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1436 vty_out(vty
, " via %s",
1437 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1438 if (nexthop
->ifindex
)
1439 vty_out(vty
, " dev %s",
1440 ifindex2ifname(nexthop
->ifindex
,
1447 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1453 * Print an LSP forwarding entry.
1455 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1457 zebra_nhlfe_t
*nhlfe
;
1460 vty
= (struct vty
*)ctxt
;
1462 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1463 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1466 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1467 nhlfe_print(nhlfe
, vty
);
1471 * JSON objects for an LSP forwarding entry.
1473 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1475 zebra_nhlfe_t
*nhlfe
= NULL
;
1476 json_object
*json
= json_object_new_object();
1477 json_object
*json_nhlfe_list
= json_object_new_array();
1479 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1481 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1482 json_object_boolean_true_add(json
, "installed");
1484 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1485 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1487 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1492 /* Return a sorted linked list of the hash contents */
1493 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1496 struct hash_bucket
*hb
;
1497 struct list
*sorted_list
= list_new();
1499 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1501 for (i
= 0; i
< hash
->size
; i
++)
1502 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1503 listnode_add_sort(sorted_list
, hb
->data
);
1509 * Compare two LSPs based on their label values.
1511 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1513 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1516 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1523 * Callback to allocate static LSP.
1525 static void *slsp_alloc(void *p
)
1527 const zebra_ile_t
*ile
= p
;
1530 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1532 return ((void *)slsp
);
1536 * Compare two static LSPs based on their label values.
1538 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1540 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1543 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1550 * Check if static NHLFE matches with search info passed.
1552 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1553 union g_addr
*gate
, ifindex_t ifindex
)
1557 if (snhlfe
->gtype
!= gtype
)
1560 switch (snhlfe
->gtype
) {
1561 case NEXTHOP_TYPE_IPV4
:
1562 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1563 sizeof(struct in_addr
));
1565 case NEXTHOP_TYPE_IPV6
:
1566 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1567 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1568 sizeof(struct in6_addr
));
1569 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1570 cmp
= !(snhlfe
->ifindex
== ifindex
);
1580 * Locate static NHLFE that matches with passed info.
1582 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1583 enum nexthop_types_t gtype
,
1584 union g_addr
*gate
, ifindex_t ifindex
)
1586 zebra_snhlfe_t
*snhlfe
;
1591 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1592 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1601 * Add static NHLFE. Base LSP config entry must have been created
1602 * and duplicate check done.
1604 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1605 enum nexthop_types_t gtype
,
1606 union g_addr
*gate
, ifindex_t ifindex
,
1607 mpls_label_t out_label
)
1609 zebra_snhlfe_t
*snhlfe
;
1614 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1615 snhlfe
->slsp
= slsp
;
1616 snhlfe
->out_label
= out_label
;
1617 snhlfe
->gtype
= gtype
;
1619 case NEXTHOP_TYPE_IPV4
:
1620 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1622 case NEXTHOP_TYPE_IPV6
:
1623 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1624 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1626 snhlfe
->ifindex
= ifindex
;
1629 XFREE(MTYPE_SNHLFE
, snhlfe
);
1633 if (slsp
->snhlfe_list
)
1634 slsp
->snhlfe_list
->prev
= snhlfe
;
1635 snhlfe
->next
= slsp
->snhlfe_list
;
1636 slsp
->snhlfe_list
= snhlfe
;
1642 * Delete static NHLFE. Entry must be present on list.
1644 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1651 slsp
= snhlfe
->slsp
;
1656 snhlfe
->next
->prev
= snhlfe
->prev
;
1658 snhlfe
->prev
->next
= snhlfe
->next
;
1660 slsp
->snhlfe_list
= snhlfe
->next
;
1662 snhlfe
->prev
= snhlfe
->next
= NULL
;
1663 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1664 XFREE(MTYPE_SNHLFE
, snhlfe
);
1670 * Delete all static NHLFE entries for this LSP (in label).
1672 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1674 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1679 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1680 snhlfe_next
= snhlfe
->next
;
1688 * Create printable string for NHLFE configuration.
1690 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1693 switch (snhlfe
->gtype
) {
1694 case NEXTHOP_TYPE_IPV4
:
1695 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1697 case NEXTHOP_TYPE_IPV6
:
1698 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1699 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1700 if (snhlfe
->ifindex
)
1702 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
),
1713 * Initialize work queue for processing changed LSPs.
1715 static int mpls_processq_init(void)
1717 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1718 if (!zrouter
.lsp_process_q
) {
1719 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1720 "%s: could not initialise work queue!", __func__
);
1724 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1725 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1726 zrouter
.lsp_process_q
->spec
.errorfunc
= NULL
;
1727 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1728 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1729 zrouter
.lsp_process_q
->spec
.hold
= 10;
1735 /* Public functions */
1738 * Process LSP update results from zebra dataplane.
1740 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1742 struct zebra_vrf
*zvrf
;
1743 zebra_ile_t tmp_ile
;
1744 struct hash
*lsp_table
;
1746 zebra_nhlfe_t
*nhlfe
;
1747 struct nexthop
*nexthop
;
1748 enum dplane_op_e op
;
1749 enum zebra_dplane_result status
;
1751 op
= dplane_ctx_get_op(ctx
);
1752 status
= dplane_ctx_get_status(ctx
);
1754 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1755 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1756 ctx
, dplane_op2str(op
),
1757 dplane_ctx_get_in_label(ctx
),
1758 dplane_res2str(status
));
1761 case DPLANE_OP_LSP_INSTALL
:
1762 case DPLANE_OP_LSP_UPDATE
:
1763 /* Look for zebra LSP object */
1764 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1768 lsp_table
= zvrf
->lsp_table
;
1770 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1771 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1773 if (IS_ZEBRA_DEBUG_DPLANE
)
1774 zlog_debug("LSP ctx %p: in-label %u not found",
1775 ctx
, dplane_ctx_get_in_label(ctx
));
1779 /* TODO -- Confirm that this result is still 'current' */
1781 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1782 /* Update zebra object */
1783 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1784 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
1785 nhlfe
= nhlfe
->next
) {
1786 nexthop
= nhlfe
->nexthop
;
1790 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1791 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1794 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1795 clear_nhlfe_installed(lsp
);
1796 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1797 "LSP Install Failure: in-label %u",
1803 case DPLANE_OP_LSP_DELETE
:
1804 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1805 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1806 "LSP Deletion Failure: in-label %u",
1807 dplane_ctx_get_in_label(ctx
));
1815 dplane_ctx_fini(&ctx
);
1819 * Process async dplane notifications.
1821 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1823 struct zebra_vrf
*zvrf
;
1824 zebra_ile_t tmp_ile
;
1825 struct hash
*lsp_table
;
1827 zebra_nhlfe_t
*nhlfe
;
1828 const zebra_nhlfe_t
*ctx_nhlfe
;
1829 struct nexthop
*nexthop
;
1830 const struct nexthop
*ctx_nexthop
;
1831 int start_count
= 0, end_count
= 0; /* Installed counts */
1832 bool changed_p
= false;
1833 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1836 zlog_debug("LSP dplane notif, in-label %u",
1837 dplane_ctx_get_in_label(ctx
));
1839 /* Look for zebra LSP object */
1840 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1844 lsp_table
= zvrf
->lsp_table
;
1846 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1847 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1850 zlog_debug("dplane LSP notif: in-label %u not found",
1851 dplane_ctx_get_in_label(ctx
));
1856 * The dataplane/forwarding plane is notifying zebra about the state
1857 * of the nexthops associated with this LSP. First, we take a
1858 * pre-scan pass to determine whether the LSP has transitioned
1859 * from installed -> uninstalled. In that case, we need to have
1860 * the existing state of the LSP objects available before making
1863 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1864 char buf
[NEXTHOP_STRLEN
];
1866 nexthop
= nhlfe
->nexthop
;
1870 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1874 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1875 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1877 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1881 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1882 nexthop_same(ctx_nexthop
, nexthop
)) {
1889 nexthop2str(nexthop
, buf
, sizeof(buf
));
1891 if (ctx_nhlfe
&& ctx_nexthop
) {
1893 const char *tstr
= "";
1895 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1896 NHLFE_FLAG_INSTALLED
))
1899 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1903 /* Test zebra nhlfe install state */
1904 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1905 NHLFE_FLAG_INSTALLED
)) {
1907 if (!CHECK_FLAG(nhlfe
->flags
,
1908 NHLFE_FLAG_INSTALLED
))
1911 /* Update counter */
1915 if (CHECK_FLAG(nhlfe
->flags
,
1916 NHLFE_FLAG_INSTALLED
))
1921 /* Not mentioned in lfib set -> uninstalled */
1922 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1923 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1924 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1929 zlog_debug("LSP dplane notif: no match, nh %s",
1935 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
1936 start_count
, end_count
,
1937 changed_p
? ", changed" : "");
1940 * Has the LSP become uninstalled?
1942 if (start_count
> 0 && end_count
== 0) {
1943 /* Inform other lfibs */
1944 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
1948 * Now we take a second pass and bring the zebra
1949 * nexthop state into sync with the forwarding-plane state.
1951 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1952 char buf
[NEXTHOP_STRLEN
];
1954 nexthop
= nhlfe
->nexthop
;
1959 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1960 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1962 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1966 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1967 nexthop_same(ctx_nexthop
, nexthop
)) {
1974 nexthop2str(nexthop
, buf
, sizeof(buf
));
1976 if (ctx_nhlfe
&& ctx_nexthop
) {
1978 /* Bring zebra nhlfe install state into sync */
1979 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1980 NHLFE_FLAG_INSTALLED
)) {
1982 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1986 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1989 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
1990 NEXTHOP_FLAG_FIB
)) {
1991 SET_FLAG(nhlfe
->nexthop
->flags
,
1992 NEXTHOP_FLAG_ACTIVE
);
1993 SET_FLAG(nhlfe
->nexthop
->flags
,
1996 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1997 NEXTHOP_FLAG_ACTIVE
);
1998 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2003 /* Not mentioned in lfib set -> uninstalled */
2005 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2006 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2007 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2011 if (end_count
> 0) {
2012 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2015 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2018 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2019 clear_nhlfe_installed(lsp
);
2023 dplane_ctx_fini(&ctx
);
2027 * Install dynamic LSP entry.
2029 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2030 struct route_entry
*re
)
2032 struct route_table
*table
;
2035 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2039 /* See if there is a configured label binding for this FEC. */
2040 fec
= fec_find(table
, &rn
->p
);
2041 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2044 /* We cannot install a label forwarding entry if local label is the
2045 * implicit-null label.
2047 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2050 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2057 * Uninstall dynamic LSP entry, if any.
2059 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2060 struct route_entry
*re
)
2062 struct route_table
*table
;
2065 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2069 /* See if there is a configured label binding for this FEC. */
2070 fec
= fec_find(table
, &rn
->p
);
2071 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2074 /* Uninstall always removes all dynamic NHLFEs. */
2075 return lsp_uninstall(zvrf
, fec
->label
);
2079 * Add an NHLFE to an LSP, return the newly-added object
2081 zebra_nhlfe_t
*zebra_mpls_lsp_add_nhlfe(zebra_lsp_t
*lsp
,
2082 enum lsp_types_t lsp_type
,
2083 enum nexthop_types_t gtype
,
2086 mpls_label_t out_label
)
2088 /* Just a public pass-through to the internal implementation */
2089 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, out_label
);
2093 * Free an allocated NHLFE
2095 void zebra_mpls_nhlfe_del(zebra_nhlfe_t
*nhlfe
)
2097 /* Just a pass-through to the internal implementation */
2102 * Registration from a client for the label binding for a FEC. If a binding
2103 * already exists, it is informed to the client.
2104 * NOTE: If there is a manually configured label binding, that is used.
2105 * Otherwise, if a label index is specified, it means we have to allocate the
2106 * label from a locally configured label block (SRGB), if one exists and index
2107 * is acceptable. If no label index then just register the specified label.
2108 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2109 * by the calling function. Register requests with both will be rejected.
2111 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2112 uint32_t label
, uint32_t label_index
,
2113 struct zserv
*client
)
2115 struct route_table
*table
;
2119 bool label_change
= false;
2121 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2122 bool is_configured_fec
= false; /* indicate statically configured FEC */
2124 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2128 if (IS_ZEBRA_DEBUG_MPLS
)
2129 prefix2str(p
, buf
, BUFSIZ
);
2131 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2133 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2134 "Rejecting FEC register for %s with both label %u and Label Index %u specified, client %s",
2135 buf
, label
, label_index
,
2136 zebra_route_string(client
->proto
));
2141 fec
= fec_find(table
, p
);
2143 fec
= fec_add(table
, p
, label
, 0, label_index
);
2146 EC_ZEBRA_FEC_ADD_FAILED
,
2147 "Failed to add FEC %s upon register, client %s",
2148 buf
, zebra_route_string(client
->proto
));
2152 old_label
= MPLS_INVALID_LABEL
;
2155 /* Check if the FEC has been statically defined in the config */
2156 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2157 /* Client may register same FEC with different label index. */
2159 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2160 if (!new_client
&& fec
->label_index
== label_index
2161 && fec
->label
== label
)
2162 /* Duplicate register */
2165 /* Save current label, update the FEC */
2166 old_label
= fec
->label
;
2167 fec
->label_index
= label_index
;
2171 listnode_add(fec
->client_list
, client
);
2173 if (IS_ZEBRA_DEBUG_MPLS
)
2174 zlog_debug("FEC %s label%s %u %s by client %s%s", buf
,
2175 have_label_index
? " index" : "",
2176 have_label_index
? label_index
: label
,
2177 new_client
? "registered" : "updated",
2178 zebra_route_string(client
->proto
),
2180 ? ", but using statically configured label"
2183 /* If not a statically configured FEC, derive the local label
2184 * from label index or use the provided label
2186 if (!is_configured_fec
) {
2187 if (have_label_index
)
2188 fec_derive_label_from_index(zvrf
, fec
);
2192 /* If no label change, exit. */
2193 if (fec
->label
== old_label
)
2196 label_change
= true;
2199 /* If new client or label change, update client and install or uninstall
2200 * label forwarding entry as needed.
2202 /* Inform client of label, if needed. */
2203 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2204 if (IS_ZEBRA_DEBUG_MPLS
)
2205 zlog_debug("Update client label %u", fec
->label
);
2206 fec_send(fec
, client
);
2209 if (new_client
|| label_change
)
2210 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2216 * Deregistration from a client for the label binding for a FEC. The FEC
2217 * itself is deleted if no other registered clients exist and there is no
2218 * label bound to the FEC.
2220 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2221 struct zserv
*client
)
2223 struct route_table
*table
;
2227 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2231 if (IS_ZEBRA_DEBUG_MPLS
)
2232 prefix2str(p
, buf
, BUFSIZ
);
2234 fec
= fec_find(table
, p
);
2236 prefix2str(p
, buf
, BUFSIZ
);
2237 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2238 "Failed to find FEC %s upon unregister, client %s",
2239 buf
, zebra_route_string(client
->proto
));
2243 listnode_delete(fec
->client_list
, client
);
2245 if (IS_ZEBRA_DEBUG_MPLS
)
2246 zlog_debug("FEC %s unregistered by client %s", buf
,
2247 zebra_route_string(client
->proto
));
2249 /* If not a configured entry, delete the FEC if no other clients. Before
2250 * deleting, see if any LSP needs to be uninstalled.
2252 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2253 && list_isempty(fec
->client_list
)) {
2254 mpls_label_t old_label
= fec
->label
;
2255 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2256 fec_change_update_lsp(zvrf
, fec
, old_label
);
2264 * Cleanup any FECs registered by this client.
2266 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2268 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2269 struct route_node
*rn
;
2271 struct listnode
*node
;
2272 struct zserv
*fec_client
;
2275 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2276 if (zvrf
->fec_table
[af
] == NULL
)
2279 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2280 rn
= route_next(rn
)) {
2282 if (!fec
|| list_isempty(fec
->client_list
))
2285 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2287 if (fec_client
== client
) {
2288 listnode_delete(fec
->client_list
,
2290 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2291 && list_isempty(fec
->client_list
))
2302 struct lsp_uninstall_args
{
2303 struct hash
*lsp_table
;
2304 enum lsp_types_t type
;
2308 * Cleanup MPLS labels registered by this client.
2310 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2313 struct zebra_vrf
*zvrf
;
2315 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2316 struct lsp_uninstall_args args
;
2323 args
.lsp_table
= zvrf
->lsp_table
;
2324 args
.type
= lsp_type_from_re_type(client
->proto
);
2325 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2329 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2330 lsp_type_from_re_type(client
->proto
));
2331 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2332 lsp_type_from_re_type(client
->proto
));
2339 * Return FEC (if any) to which this label is bound.
2340 * Note: Only works for per-prefix binding and when the label is not
2342 * TODO: Currently walks entire table, can optimize later with another
2345 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2348 struct route_node
*rn
;
2352 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2353 if (zvrf
->fec_table
[af
] == NULL
)
2356 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2357 rn
= route_next(rn
)) {
2361 if (fec
->label
== label
)
2370 * Inform if specified label is currently bound to a FEC or not.
2372 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2374 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2378 * Add static FEC to label binding. If there are clients registered for this
2379 * FEC, notify them. If there are labeled routes for this FEC, install the
2380 * label forwarding entry.
2382 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2383 mpls_label_t in_label
)
2385 struct route_table
*table
;
2388 mpls_label_t old_label
;
2391 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2395 if (IS_ZEBRA_DEBUG_MPLS
)
2396 prefix2str(p
, buf
, BUFSIZ
);
2398 /* Update existing FEC or create a new one. */
2399 fec
= fec_find(table
, p
);
2401 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2402 MPLS_INVALID_LABEL_INDEX
);
2404 prefix2str(p
, buf
, BUFSIZ
);
2405 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2406 "Failed to add FEC %s upon config", buf
);
2410 if (IS_ZEBRA_DEBUG_MPLS
)
2411 zlog_debug("Add fec %s label %u", buf
, in_label
);
2413 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2414 if (fec
->label
== in_label
)
2415 /* Duplicate config */
2418 /* Label change, update clients. */
2419 old_label
= fec
->label
;
2420 if (IS_ZEBRA_DEBUG_MPLS
)
2421 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2423 fec
->label
= in_label
;
2424 fec_update_clients(fec
);
2426 /* Update label forwarding entries appropriately */
2427 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2434 * Remove static FEC to label binding. If there are no clients registered
2435 * for this FEC, delete the FEC; else notify clients
2436 * Note: Upon delete of static binding, if label index exists for this FEC,
2437 * client may need to be updated with derived label.
2439 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2441 struct route_table
*table
;
2443 mpls_label_t old_label
;
2446 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2450 fec
= fec_find(table
, p
);
2452 prefix2str(p
, buf
, BUFSIZ
);
2453 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2454 "Failed to find FEC %s upon delete", buf
);
2458 if (IS_ZEBRA_DEBUG_MPLS
) {
2459 prefix2str(p
, buf
, BUFSIZ
);
2460 zlog_debug("Delete fec %s label %u label index %u", buf
,
2461 fec
->label
, fec
->label_index
);
2464 old_label
= fec
->label
;
2465 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2466 fec
->label
= MPLS_INVALID_LABEL
;
2468 /* If no client exists, just delete the FEC. */
2469 if (list_isempty(fec
->client_list
)) {
2474 /* Derive the local label (from label index) or reset it. */
2475 fec_derive_label_from_index(zvrf
, fec
);
2477 /* If there is a label change, update clients. */
2478 if (fec
->label
== old_label
)
2480 fec_update_clients(fec
);
2482 /* Update label forwarding entries appropriately */
2483 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2487 * Display MPLS FEC to label binding configuration (VTY command handler).
2489 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2491 struct route_node
*rn
;
2497 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2498 if (zvrf
->fec_table
[af
] == NULL
)
2501 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2502 rn
= route_next(rn
)) {
2509 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2513 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2514 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2515 label2str(fec
->label
, lstr
, BUFSIZ
));
2523 * Display MPLS FEC to label binding (VTY command handler).
2525 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2527 struct route_node
*rn
;
2530 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2531 if (zvrf
->fec_table
[af
] == NULL
)
2534 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2535 rn
= route_next(rn
)) {
2538 fec_print(rn
->info
, vty
);
2544 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2546 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2549 struct route_table
*table
;
2550 struct route_node
*rn
;
2552 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2557 rn
= route_node_lookup(table
, p
);
2561 route_unlock_node(rn
);
2565 fec_print(rn
->info
, vty
);
2568 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2569 enum lsp_types_t type
, mpls_label_t label
)
2571 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2572 nexthop_add_labels(nexthop
, type
, 1, &label
);
2573 else if (!add
&& nexthop
->nh_label_type
== type
)
2574 nexthop_del_labels(nexthop
);
2582 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2584 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2585 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2586 union g_addr
*gate
, ifindex_t ifindex
, uint8_t route_type
,
2587 unsigned short route_instance
, mpls_label_t out_label
)
2589 struct route_table
*table
;
2590 struct route_node
*rn
;
2591 struct route_entry
*re
;
2592 struct nexthop
*nexthop
;
2596 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2601 /* Lookup existing route */
2602 rn
= route_node_get(table
, prefix
);
2603 RNODE_FOREACH_RE (rn
, re
) {
2604 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2606 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2614 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2615 switch (nexthop
->type
) {
2616 case NEXTHOP_TYPE_IPV4
:
2617 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2618 if (gtype
!= NEXTHOP_TYPE_IPV4
2619 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2621 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2623 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2624 && nexthop
->ifindex
!= ifindex
)
2626 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2631 case NEXTHOP_TYPE_IPV6
:
2632 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2633 if (gtype
!= NEXTHOP_TYPE_IPV6
2634 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2636 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2638 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2639 && nexthop
->ifindex
!= ifindex
)
2641 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2654 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2655 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2661 int mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2662 struct prefix
*prefix
, uint8_t route_type
,
2663 unsigned short route_instance
)
2665 struct route_table
*table
;
2666 struct route_node
*rn
;
2667 struct route_entry
*re
;
2668 struct nexthop
*nexthop
;
2671 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2676 /* Lookup existing route */
2677 rn
= route_node_get(table
, prefix
);
2678 RNODE_FOREACH_RE (rn
, re
) {
2679 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2681 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2687 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2688 nexthop_del_labels(nexthop
);
2690 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2691 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2698 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2699 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2700 * the out-label for an existing NHLFE (update case).
2702 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2703 mpls_label_t in_label
, mpls_label_t out_label
,
2704 enum nexthop_types_t gtype
, union g_addr
*gate
,
2707 struct hash
*lsp_table
;
2708 zebra_ile_t tmp_ile
;
2710 zebra_nhlfe_t
*nhlfe
;
2714 lsp_table
= zvrf
->lsp_table
;
2718 /* If entry is present, exit. */
2719 tmp_ile
.in_label
= in_label
;
2720 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2723 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2725 struct nexthop
*nh
= nhlfe
->nexthop
;
2728 assert(nh
->nh_label
);
2730 /* Clear deleted flag (in case it was set) */
2731 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2732 if (nh
->nh_label
->label
[0] == out_label
)
2736 if (IS_ZEBRA_DEBUG_MPLS
) {
2737 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2739 "LSP in-label %u type %d nexthop %s "
2740 "out-label changed to %u (old %u)",
2741 in_label
, type
, buf
, out_label
,
2742 nh
->nh_label
->label
[0]);
2745 /* Update out label, trigger processing. */
2746 nh
->nh_label
->label
[0] = out_label
;
2748 /* Add LSP entry to this nexthop */
2749 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2753 if (IS_ZEBRA_DEBUG_MPLS
) {
2754 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2756 "Add LSP in-label %u type %d nexthop %s "
2758 in_label
, type
, buf
, out_label
);
2761 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2764 /* Mark NHLFE, queue LSP for processing. */
2765 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2766 if (lsp_processq_add(lsp
))
2773 * Uninstall a particular NHLFE in the forwarding table. If this is
2774 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2776 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2777 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2778 union g_addr
*gate
, ifindex_t ifindex
)
2780 struct hash
*lsp_table
;
2781 zebra_ile_t tmp_ile
;
2783 zebra_nhlfe_t
*nhlfe
;
2787 lsp_table
= zvrf
->lsp_table
;
2791 /* If entry is not present, exit. */
2792 tmp_ile
.in_label
= in_label
;
2793 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2796 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2800 if (IS_ZEBRA_DEBUG_MPLS
) {
2801 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2802 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2803 in_label
, type
, buf
, nhlfe
->flags
);
2806 /* Mark NHLFE for delete or directly delete, as appropriate. */
2807 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2808 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2809 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2810 if (lsp_processq_add(lsp
))
2815 /* Free LSP entry if no other NHLFEs and not scheduled. */
2816 if (!lsp
->nhlfe_list
2817 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2818 if (IS_ZEBRA_DEBUG_MPLS
)
2819 zlog_debug("Free LSP in-label %u flags 0x%x",
2820 lsp
->ile
.in_label
, lsp
->flags
);
2822 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2823 XFREE(MTYPE_LSP
, lsp
);
2829 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2830 mpls_label_t in_label
)
2832 struct hash
*lsp_table
;
2833 zebra_ile_t tmp_ile
;
2837 lsp_table
= zvrf
->lsp_table
;
2841 /* If entry is not present, exit. */
2842 tmp_ile
.in_label
= in_label
;
2843 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2847 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
2851 * Uninstall all NHLFEs for a particular LSP forwarding entry.
2852 * If no other NHLFEs exist, the entry would be deleted.
2854 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
2856 struct lsp_uninstall_args
*args
= ctxt
;
2858 struct hash
*lsp_table
;
2860 lsp
= (zebra_lsp_t
*)bucket
->data
;
2861 if (!lsp
->nhlfe_list
)
2864 lsp_table
= args
->lsp_table
;
2868 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
2872 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
2875 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
2876 int afi
, enum lsp_types_t lsp_type
)
2878 struct route_table
*table
;
2879 struct route_node
*rn
;
2880 struct route_entry
*re
;
2881 struct nexthop
*nexthop
;
2884 /* Process routes of interested address-families. */
2885 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2889 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2891 RNODE_FOREACH_RE (rn
, re
) {
2892 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2893 nexthop
= nexthop
->next
) {
2894 if (nexthop
->nh_label_type
!= lsp_type
)
2897 nexthop_del_labels(nexthop
);
2898 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2899 SET_FLAG(re
->status
,
2900 ROUTE_ENTRY_LABELS_CHANGED
);
2910 #if defined(HAVE_CUMULUS)
2912 * Check that the label values used in LSP creation are consistent. The
2913 * main criteria is that if there is ECMP, the label operation must still
2914 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2915 * to current HW restrictions.
2917 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2918 mpls_label_t in_label
,
2919 mpls_label_t out_label
,
2920 enum nexthop_types_t gtype
,
2921 union g_addr
*gate
, ifindex_t ifindex
)
2923 struct hash
*slsp_table
;
2924 zebra_ile_t tmp_ile
;
2926 zebra_snhlfe_t
*snhlfe
;
2929 slsp_table
= zvrf
->slsp_table
;
2933 /* If entry is not present, exit. */
2934 tmp_ile
.in_label
= in_label
;
2935 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2939 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2941 if (snhlfe
->out_label
== out_label
)
2944 /* If not only NHLFE, cannot allow label change. */
2945 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2948 /* If other NHLFEs exist, label operation must match. */
2949 if (slsp
->snhlfe_list
) {
2952 cur_op
= (slsp
->snhlfe_list
->out_label
2953 == MPLS_LABEL_IMPLICIT_NULL
);
2954 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2955 if (cur_op
!= new_op
)
2960 /* Label values are good. */
2963 #endif /* HAVE_CUMULUS */
2966 * Add static LSP entry. This may be the first entry for this incoming label
2967 * or an additional nexthop; an existing entry may also have outgoing label
2969 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2972 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2973 mpls_label_t out_label
,
2974 enum nexthop_types_t gtype
, union g_addr
*gate
,
2977 struct hash
*slsp_table
;
2978 zebra_ile_t tmp_ile
;
2980 zebra_snhlfe_t
*snhlfe
;
2984 slsp_table
= zvrf
->slsp_table
;
2988 /* If entry is present, exit. */
2989 tmp_ile
.in_label
= in_label
;
2990 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2993 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2995 if (snhlfe
->out_label
== out_label
)
2999 if (IS_ZEBRA_DEBUG_MPLS
) {
3000 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3002 "Upd static LSP in-label %u nexthop %s "
3003 "out-label %u (old %u)",
3004 in_label
, buf
, out_label
, snhlfe
->out_label
);
3006 snhlfe
->out_label
= out_label
;
3008 /* Add static LSP entry to this nexthop */
3009 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
3013 if (IS_ZEBRA_DEBUG_MPLS
) {
3014 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3016 "Add static LSP in-label %u nexthop %s out-label %u",
3017 in_label
, buf
, out_label
);
3021 /* (Re)Install LSP in the main table. */
3022 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
3030 * Delete static LSP entry. This may be the delete of one particular
3031 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3033 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3034 * LSP configuration.
3036 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3037 enum nexthop_types_t gtype
, union g_addr
*gate
,
3040 struct hash
*slsp_table
;
3041 zebra_ile_t tmp_ile
;
3043 zebra_snhlfe_t
*snhlfe
;
3046 slsp_table
= zvrf
->slsp_table
;
3050 /* If entry is not present, exit. */
3051 tmp_ile
.in_label
= in_label
;
3052 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
3056 /* Is it delete of entire LSP or a specific NHLFE? */
3057 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3058 if (IS_ZEBRA_DEBUG_MPLS
)
3059 zlog_debug("Del static LSP in-label %u", in_label
);
3061 /* Uninstall entire LSP from the main table. */
3062 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3064 /* Delete all static NHLFEs */
3065 snhlfe_del_all(slsp
);
3067 /* Find specific NHLFE, exit if not found. */
3068 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
3072 if (IS_ZEBRA_DEBUG_MPLS
) {
3074 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3075 zlog_debug("Del static LSP in-label %u nexthop %s",
3079 /* Uninstall LSP from the main table. */
3080 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3083 /* Delete static LSP NHLFE */
3087 /* Remove entire static LSP entry if no NHLFE - valid in either case
3089 if (!slsp
->snhlfe_list
) {
3090 slsp
= hash_release(slsp_table
, &tmp_ile
);
3091 XFREE(MTYPE_SLSP
, slsp
);
3098 * Schedule all MPLS label forwarding entries for processing.
3099 * Called upon changes that may affect one or more of them such as
3100 * interface or nexthop state changes.
3102 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3106 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3110 * Display MPLS label forwarding table for a specific LSP
3111 * (VTY command handler).
3113 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3114 mpls_label_t label
, bool use_json
)
3116 struct hash
*lsp_table
;
3118 zebra_ile_t tmp_ile
;
3119 json_object
*json
= NULL
;
3122 lsp_table
= zvrf
->lsp_table
;
3126 /* If entry is not present, exit. */
3127 tmp_ile
.in_label
= label
;
3128 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3133 json
= lsp_json(lsp
);
3134 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3135 json
, JSON_C_TO_STRING_PRETTY
));
3136 json_object_free(json
);
3138 lsp_print(lsp
, (void *)vty
);
3142 * Display MPLS label forwarding table (VTY command handler).
3144 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3148 json_object
*json
= NULL
;
3149 zebra_lsp_t
*lsp
= NULL
;
3150 zebra_nhlfe_t
*nhlfe
= NULL
;
3151 struct listnode
*node
= NULL
;
3152 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3155 json
= json_object_new_object();
3157 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3158 json_object_object_add(
3159 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
3162 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3163 json
, JSON_C_TO_STRING_PRETTY
));
3164 json_object_free(json
);
3168 /* Prepare table. */
3169 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3170 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3171 tt
->style
.cell
.rpad
= 2;
3172 tt
->style
.corner
= '+';
3174 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3176 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3177 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
3178 nhlfe
= nhlfe
->next
) {
3179 struct nexthop
*nexthop
;
3180 const char *out_label_str
;
3181 char nh_buf
[NEXTHOP_STRLEN
];
3183 nexthop
= nhlfe
->nexthop
;
3185 switch (nexthop
->type
) {
3186 case NEXTHOP_TYPE_IFINDEX
: {
3187 struct zebra_ns
*zns
;
3188 struct interface
*ifp
;
3190 zns
= zebra_ns_lookup(NS_DEFAULT
);
3191 ifp
= if_lookup_by_index_per_ns(
3192 zns
, nexthop
->ifindex
);
3193 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3194 ifp
? ifp
->name
: "Null");
3197 case NEXTHOP_TYPE_IPV4
:
3198 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3199 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3200 nh_buf
, sizeof(nh_buf
));
3202 case NEXTHOP_TYPE_IPV6
:
3203 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3204 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3205 nh_buf
, sizeof(nh_buf
));
3211 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3212 out_label_str
= mpls_label2str(
3213 nexthop
->nh_label
->num_labels
,
3214 &nexthop
->nh_label
->label
[0],
3217 out_label_str
= "-";
3219 ttable_add_row(tt
, "%u|%s|%s|%s",
3221 nhlfe_type2str(nhlfe
->type
),
3222 nh_buf
, out_label_str
);
3226 /* Dump the generated table. */
3227 if (tt
->nrows
> 1) {
3228 char *table
= ttable_dump(tt
, "\n");
3229 vty_out(vty
, "%s\n", table
);
3230 XFREE(MTYPE_TMP
, table
);
3235 list_delete(&lsp_list
);
3239 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3241 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3244 zebra_snhlfe_t
*snhlfe
;
3245 struct listnode
*node
;
3246 struct list
*slsp_list
=
3247 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
3249 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
3250 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
3251 snhlfe
= snhlfe
->next
) {
3255 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
3256 switch (snhlfe
->out_label
) {
3257 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3258 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3259 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3261 case MPLS_LABEL_IMPLICIT_NULL
:
3262 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3265 sprintf(lstr
, "%u", snhlfe
->out_label
);
3269 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
3274 list_delete(&slsp_list
);
3275 return (zvrf
->slsp_table
->count
? 1 : 0);
3279 * Add/update global label block.
3281 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3284 zvrf
->mpls_srgb
.start_label
= start_label
;
3285 zvrf
->mpls_srgb
.end_label
= end_label
;
3287 /* Evaluate registered FECs to see if any get a label or not. */
3293 * Delete global label block.
3295 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3297 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3298 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3300 /* Process registered FECs to clear their local label, if needed. */
3306 * Display MPLS global label block configuration (VTY command handler).
3308 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3310 if (zvrf
->mpls_srgb
.start_label
== 0)
3313 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3314 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3315 vty_out(vty
, "mpls label global-block %u %u\n",
3316 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3323 * Called when VRF becomes inactive, cleans up information but keeps
3326 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3328 struct zebra_vrf
*def_zvrf
;
3331 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3332 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3335 * For other vrfs, we try to remove associated LSPs; we locate
3336 * the LSPs in the default vrf.
3338 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3340 /* At shutdown, the default may be gone already */
3341 if (def_zvrf
== NULL
)
3344 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3345 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3346 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3352 * Called upon process exiting, need to delete LSP forwarding
3353 * entries from the kernel.
3354 * NOTE: Currently supported only for default VRF.
3356 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
3358 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3359 hash_clean(zvrf
->lsp_table
, NULL
);
3360 hash_free(zvrf
->lsp_table
);
3361 hash_clean(zvrf
->slsp_table
, NULL
);
3362 hash_free(zvrf
->slsp_table
);
3363 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
3364 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
3368 * Allocate MPLS tables for this VRF and do other initialization.
3369 * NOTE: Currently supported only for default VRF.
3371 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
3376 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
3377 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
3378 zvrf
->fec_table
[AFI_IP
] = route_table_init();
3379 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
3380 zvrf
->mpls_flags
= 0;
3381 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3382 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3386 * Global MPLS initialization.
3388 void zebra_mpls_init(void)
3392 if (mpls_kernel_init() < 0) {
3393 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
3394 "Disabling MPLS support (no kernel support)");
3398 if (!mpls_processq_init())
3401 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
3402 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);