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_srte.h"
51 #include "zebra/zebra_errors.h"
53 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
55 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
59 /* static function declarations */
61 static void fec_evaluate(struct zebra_vrf
*zvrf
);
62 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
64 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
65 struct route_node
*rn
, struct route_entry
*re
);
66 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
67 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
68 mpls_label_t old_label
);
69 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
70 static void fec_update_clients(zebra_fec_t
*fec
);
71 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
72 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
73 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
74 mpls_label_t label
, uint32_t flags
,
75 uint32_t label_index
);
76 static int fec_del(zebra_fec_t
*fec
);
78 static unsigned int label_hash(const void *p
);
79 static bool label_cmp(const void *p1
, const void *p2
);
80 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
81 struct nexthop
*nexthop
);
82 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
86 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
87 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
88 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
89 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
90 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
91 static void lsp_processq_complete(struct work_queue
*wq
);
92 static int lsp_processq_add(zebra_lsp_t
*lsp
);
93 static void *lsp_alloc(void *p
);
95 /* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
96 static void lsp_check_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
);
98 /* Free lsp; sets caller's pointer to NULL */
99 static void lsp_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
);
101 static char *nhlfe2str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
102 static char *nhlfe_config_str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
103 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
104 const union g_addr
*gate
, ifindex_t ifindex
);
105 static zebra_nhlfe_t
*nhlfe_find(struct nhlfe_list_head
*list
,
106 enum lsp_types_t lsp_type
,
107 enum nexthop_types_t gtype
,
108 const union g_addr
*gate
, ifindex_t ifindex
);
109 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
110 enum nexthop_types_t gtype
,
111 const union g_addr
*gate
, ifindex_t ifindex
,
112 uint8_t num_labels
, const mpls_label_t
*labels
,
114 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
);
115 static void nhlfe_free(zebra_nhlfe_t
*nhlfe
);
116 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
117 struct mpls_label_stack
*nh_label
);
118 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
119 enum lsp_types_t type
);
120 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
121 mpls_label_t in_label
);
122 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
,
124 static void lsp_print(struct vty
*vty
, zebra_lsp_t
*lsp
);
125 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
126 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
127 int afi
, enum lsp_types_t lsp_type
);
128 static int lsp_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
129 const struct zapi_nexthop
*znh
);
130 static int lsp_backup_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
131 const struct zapi_nexthop
*znh
);
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 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
144 nexthop
= nhlfe
->nexthop
;
148 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
149 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
152 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
153 nexthop
= nhlfe
->nexthop
;
157 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
158 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
163 * Install label forwarding entry based on labeled-route entry.
165 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
166 struct route_node
*rn
, struct route_entry
*re
)
168 struct hash
*lsp_table
;
171 zebra_nhlfe_t
*nhlfe
;
172 struct nexthop
*nexthop
;
173 enum lsp_types_t lsp_type
;
178 lsp_table
= zvrf
->lsp_table
;
182 lsp_type
= lsp_type_from_re_type(re
->type
);
185 /* Locate or allocate LSP entry. */
186 tmp_ile
.in_label
= label
;
187 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
191 /* For each active nexthop, create NHLFE. Note that we deliberately skip
192 * recursive nexthops right now, because intermediate hops won't
194 * the label advertised by the recursive nexthop (plus we don't have the
195 * logic yet to push multiple labels).
197 for (nexthop
= re
->nhe
->nhg
.nexthop
;
198 nexthop
; nexthop
= nexthop
->next
) {
199 /* Skip inactive and recursive entries. */
200 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
202 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
205 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, lsp_type
,
206 nexthop
->type
, &nexthop
->gate
,
209 /* Clear deleted flag (in case it was set) */
210 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
211 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
216 if (IS_ZEBRA_DEBUG_MPLS
) {
217 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
219 "LSP in-label %u type %d nexthop %s out-label changed",
220 lsp
->ile
.in_label
, lsp_type
, buf
);
223 /* Update out label, trigger processing. */
224 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
225 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
228 /* Add LSP entry to this nexthop */
229 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
230 &nexthop
->gate
, nexthop
->ifindex
,
231 nexthop
->nh_label
->num_labels
,
232 nexthop
->nh_label
->label
,
237 if (IS_ZEBRA_DEBUG_MPLS
) {
238 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
240 "Add LSP in-label %u type %d nexthop %s out-label %u",
241 lsp
->ile
.in_label
, lsp_type
, buf
,
242 nexthop
->nh_label
->label
[0]);
245 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
247 /* Mark NHLFE as changed. */
248 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
253 /* Queue LSP for processing if necessary. If no NHLFE got added (special
254 * case), delete the LSP entry; this case results in somewhat ugly
257 if (added
|| changed
) {
258 if (lsp_processq_add(lsp
))
261 lsp_check_free(lsp_table
, &lsp
);
268 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
269 * NHLFEs are removed, the entire entry is deleted.
271 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
273 struct hash
*lsp_table
;
276 zebra_nhlfe_t
*nhlfe
;
280 lsp_table
= zvrf
->lsp_table
;
284 /* If entry is not present, exit. */
285 tmp_ile
.in_label
= label
;
286 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
287 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
290 /* Mark NHLFEs for delete or directly delete, as appropriate. */
291 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
293 /* Skip static NHLFEs */
294 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
297 if (IS_ZEBRA_DEBUG_MPLS
) {
298 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
300 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
301 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
304 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
305 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
306 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
312 /* Queue LSP for processing, if needed, else delete. */
313 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
314 if (lsp_processq_add(lsp
))
317 lsp_check_free(lsp_table
, &lsp
);
324 * This function is invoked upon change to label block configuration; it
325 * will walk all registered FECs with label-index and appropriately update
326 * their local labels and trigger client updates.
328 static void fec_evaluate(struct zebra_vrf
*zvrf
)
330 struct route_node
*rn
;
332 uint32_t old_label
, new_label
;
336 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
337 if (zvrf
->fec_table
[af
] == NULL
)
340 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
341 rn
= route_next(rn
)) {
342 if ((fec
= rn
->info
) == NULL
)
345 /* Skip configured FECs and those without a label index.
347 if (fec
->flags
& FEC_FLAG_CONFIGURED
348 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
351 if (IS_ZEBRA_DEBUG_MPLS
)
352 prefix2str(&rn
->p
, buf
, BUFSIZ
);
354 /* Save old label, determine new label. */
355 old_label
= fec
->label
;
357 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
358 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
359 new_label
= MPLS_INVALID_LABEL
;
361 /* If label has changed, update FEC and clients. */
362 if (new_label
== old_label
)
365 if (IS_ZEBRA_DEBUG_MPLS
)
367 "Update fec %s new label %u upon label block",
370 fec
->label
= new_label
;
371 fec_update_clients(fec
);
373 /* Update label forwarding entries appropriately */
374 fec_change_update_lsp(zvrf
, fec
, old_label
);
380 * Derive (if possible) and update the local label for the FEC based on
381 * its label index. The index is "acceptable" if it falls within the
382 * globally configured label block (SRGB).
384 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
389 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
390 && zvrf
->mpls_srgb
.start_label
391 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
392 < zvrf
->mpls_srgb
.end_label
))
395 fec
->label
= MPLS_INVALID_LABEL
;
401 * There is a change for this FEC. Install or uninstall label forwarding
402 * entries, as appropriate.
404 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
405 mpls_label_t old_label
)
407 struct route_table
*table
;
408 struct route_node
*rn
;
409 struct route_entry
*re
;
412 /* Uninstall label forwarding entry, if previously installed. */
413 if (old_label
!= MPLS_INVALID_LABEL
414 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
415 lsp_uninstall(zvrf
, old_label
);
417 /* Install label forwarding entry corr. to new label, if needed. */
418 if (fec
->label
== MPLS_INVALID_LABEL
419 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
422 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
423 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
427 /* See if labeled route exists. */
428 rn
= route_node_lookup(table
, &fec
->rn
->p
);
432 RNODE_FOREACH_RE (rn
, re
) {
433 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
437 if (!re
|| !zebra_rib_labeled_unicast(re
))
440 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
447 * Inform about FEC to a registered client.
449 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
452 struct route_node
*rn
;
456 /* Get output stream. */
457 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
459 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
461 stream_putw(s
, rn
->p
.family
);
462 stream_put_prefix(s
, &rn
->p
);
463 stream_putl(s
, fec
->label
);
464 stream_putw_at(s
, 0, stream_get_endp(s
));
465 return zserv_send_message(client
, s
);
469 * Update all registered clients about this FEC. Caller should've updated
470 * FEC and ensure no duplicate updates.
472 static void fec_update_clients(zebra_fec_t
*fec
)
474 struct listnode
*node
;
475 struct zserv
*client
;
477 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
478 if (IS_ZEBRA_DEBUG_MPLS
)
479 zlog_debug("Update client %s",
480 zebra_route_string(client
->proto
));
481 fec_send(fec
, client
);
487 * Print a FEC-label binding entry.
489 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
491 struct route_node
*rn
;
492 struct listnode
*node
;
493 struct zserv
*client
;
497 prefix2str(&rn
->p
, buf
, BUFSIZ
);
498 vty_out(vty
, "%s\n", buf
);
499 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
500 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
501 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
503 if (!list_isempty(fec
->client_list
)) {
504 vty_out(vty
, " Client list:");
505 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
506 vty_out(vty
, " %s(fd %d)",
507 zebra_route_string(client
->proto
),
514 * Locate FEC-label binding that matches with passed info.
516 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
518 struct route_node
*rn
;
521 rn
= route_node_lookup(table
, p
);
525 route_unlock_node(rn
);
530 * Add a FEC. This may be upon a client registering for a binding
531 * or when a binding is configured.
533 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
534 mpls_label_t label
, uint32_t flags
,
535 uint32_t label_index
)
537 struct route_node
*rn
;
542 /* Lookup (or add) route node.*/
543 rn
= route_node_get(table
, p
);
550 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
555 fec
->client_list
= list_new();
557 route_unlock_node(rn
); /* for the route_node_get */
559 fec
->label_index
= label_index
;
566 * Delete a FEC. This may be upon the last client deregistering for
567 * a FEC and no binding exists or when the binding is deleted and there
568 * are no registered clients.
570 static int fec_del(zebra_fec_t
*fec
)
572 list_delete(&fec
->client_list
);
573 fec
->rn
->info
= NULL
;
574 route_unlock_node(fec
->rn
);
575 XFREE(MTYPE_FEC
, fec
);
580 * Hash function for label.
582 static unsigned int label_hash(const void *p
)
584 const zebra_ile_t
*ile
= p
;
586 return (jhash_1word(ile
->in_label
, 0));
590 * Compare 2 LSP hash entries based on in-label.
592 static bool label_cmp(const void *p1
, const void *p2
)
594 const zebra_ile_t
*ile1
= p1
;
595 const zebra_ile_t
*ile2
= p2
;
597 return (ile1
->in_label
== ile2
->in_label
);
601 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
603 * NOTE: Looking only for connected routes right now.
605 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
606 struct nexthop
*nexthop
)
608 struct route_table
*table
;
609 struct prefix_ipv4 p
;
610 struct route_node
*rn
;
611 struct route_entry
*match
;
612 struct nexthop
*match_nh
;
614 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
618 /* Lookup nexthop in IPv4 routing table. */
619 memset(&p
, 0, sizeof(struct prefix_ipv4
));
621 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
622 p
.prefix
= nexthop
->gate
.ipv4
;
624 rn
= route_node_match(table
, (struct prefix
*)&p
);
628 route_unlock_node(rn
);
630 /* Locate a valid connected route. */
631 RNODE_FOREACH_RE (rn
, match
) {
632 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
633 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
636 for (match_nh
= match
->nhe
->nhg
.nexthop
; match_nh
;
637 match_nh
= match_nh
->next
) {
638 if (match
->type
== ZEBRA_ROUTE_CONNECT
639 || nexthop
->ifindex
== match_nh
->ifindex
) {
640 nexthop
->ifindex
= match_nh
->ifindex
;
651 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
653 * NOTE: Looking only for connected routes right now.
655 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
656 struct nexthop
*nexthop
)
658 struct route_table
*table
;
659 struct prefix_ipv6 p
;
660 struct route_node
*rn
;
661 struct route_entry
*match
;
663 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
667 /* Lookup nexthop in IPv6 routing table. */
668 memset(&p
, 0, sizeof(struct prefix_ipv6
));
670 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
671 p
.prefix
= nexthop
->gate
.ipv6
;
673 rn
= route_node_match(table
, (struct prefix
*)&p
);
677 route_unlock_node(rn
);
679 /* Locate a valid connected route. */
680 RNODE_FOREACH_RE (rn
, match
) {
681 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
682 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
683 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
687 if (!match
|| !match
->nhe
->nhg
.nexthop
)
690 nexthop
->ifindex
= match
->nhe
->nhg
.nexthop
->ifindex
;
696 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
698 * NOTE: Each NHLFE points to only 1 nexthop.
700 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
702 struct nexthop
*nexthop
;
703 struct interface
*ifp
;
704 struct zebra_ns
*zns
;
706 nexthop
= nhlfe
->nexthop
;
707 if (!nexthop
) // unexpected
710 /* Check on nexthop based on type. */
711 switch (nexthop
->type
) {
712 case NEXTHOP_TYPE_IFINDEX
:
714 * Lookup if this type is special. The
715 * NEXTHOP_TYPE_IFINDEX is a pop and
716 * forward into a different table for
717 * processing. As such this ifindex
718 * passed to us may be a VRF device
719 * which will not be in the default
720 * VRF. So let's look in all of them
722 zns
= zebra_ns_lookup(NS_DEFAULT
);
723 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
724 if (ifp
&& if_is_operative(ifp
))
725 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
727 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
729 case NEXTHOP_TYPE_IPV4
:
730 case NEXTHOP_TYPE_IPV4_IFINDEX
:
731 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
732 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
734 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 case NEXTHOP_TYPE_IPV6
:
738 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
739 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
741 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
744 case NEXTHOP_TYPE_IPV6_IFINDEX
:
745 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
746 ifp
= if_lookup_by_index(nexthop
->ifindex
,
748 if (ifp
&& if_is_operative(ifp
))
749 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
751 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
753 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
754 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
756 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
764 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
768 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
769 * reachability and select the best. Multipath entries are also
770 * marked. This is invoked when an LSP scheduled for processing (due
771 * to some change) is examined.
773 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
775 zebra_nhlfe_t
*nhlfe
;
777 struct nexthop
*nexthop
;
785 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
788 * First compute the best path, after checking nexthop status. We are
789 * only concerned with non-deleted NHLFEs.
791 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
792 /* Clear selection flags. */
793 UNSET_FLAG(nhlfe
->flags
,
794 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
796 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
797 && nhlfe_nexthop_active(nhlfe
)) {
798 if (!best
|| (nhlfe
->distance
< best
->distance
))
803 lsp
->best_nhlfe
= best
;
804 if (!lsp
->best_nhlfe
)
808 * Check the active status of backup nhlfes also
810 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
811 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
812 (void)nhlfe_nexthop_active(nhlfe
);
815 /* Mark best NHLFE as selected. */
816 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
819 * If best path exists, see if there is ECMP. While doing this, note if
821 * new (uninstalled) NHLFE has been selected, an installed entry that is
822 * still selected has a change or an installed entry is to be removed.
824 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
825 int nh_chg
, nh_sel
, nh_inst
;
827 nexthop
= nhlfe
->nexthop
;
828 if (!nexthop
) // unexpected
831 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
832 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
833 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
834 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
835 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
839 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
840 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
841 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
843 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
845 if ((nh_sel
&& !nh_inst
)
846 || (nh_sel
&& nh_inst
&& nh_chg
)
847 || (nh_inst
&& !nh_sel
))
851 /* We have finished examining, clear changed flag. */
852 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
856 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
860 * Delete LSP forwarding entry from kernel, if installed. Called upon
863 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
867 lsp
= (zebra_lsp_t
*)bucket
->data
;
868 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
869 (void)dplane_lsp_delete(lsp
);
873 * Schedule LSP forwarding entry for processing. Called upon changes
874 * that may impact LSPs such as nexthop / connected route changes.
876 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
880 lsp
= (zebra_lsp_t
*)bucket
->data
;
881 (void)lsp_processq_add(lsp
);
885 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
886 * any multipaths and update or delete from the kernel, as needed.
888 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
891 zebra_nhlfe_t
*oldbest
, *newbest
;
892 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
893 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
894 enum zebra_dplane_result res
;
896 lsp
= (zebra_lsp_t
*)data
;
897 if (!lsp
) // unexpected
900 oldbest
= lsp
->best_nhlfe
;
902 /* Select best NHLFE(s) */
903 lsp_select_best_nhlfe(lsp
);
905 newbest
= lsp
->best_nhlfe
;
907 if (IS_ZEBRA_DEBUG_MPLS
) {
909 nhlfe2str(oldbest
, buf
, sizeof(buf
));
911 nhlfe2str(newbest
, buf2
, sizeof(buf2
));
913 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
914 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
915 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
918 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
919 /* Not already installed */
922 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
924 switch (dplane_lsp_add(lsp
)) {
925 case ZEBRA_DPLANE_REQUEST_QUEUED
:
926 /* Set 'installed' flag so we will know
927 * that an install is in-flight.
929 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
931 zvrf
->lsp_installs_queued
++;
933 case ZEBRA_DPLANE_REQUEST_FAILURE
:
934 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
935 "LSP Install Failure: %u",
938 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
939 zvrf
->lsp_installs
++;
944 /* Installed, may need an update and/or delete. */
946 res
= dplane_lsp_delete(lsp
);
948 /* We do some of the lsp cleanup immediately for
951 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
952 clear_nhlfe_installed(lsp
);
955 case ZEBRA_DPLANE_REQUEST_QUEUED
:
956 zvrf
->lsp_removals_queued
++;
958 case ZEBRA_DPLANE_REQUEST_FAILURE
:
959 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
960 "LSP Deletion Failure: %u",
963 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
964 zvrf
->lsp_removals
++;
967 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
968 zebra_nhlfe_t
*nhlfe
;
969 struct nexthop
*nexthop
;
971 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
973 /* We leave the INSTALLED flag set here
974 * so we know an update is in-flight.
978 * Any NHLFE that was installed but is not
979 * selected now needs to have its flags updated.
981 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
982 nexthop
= nhlfe
->nexthop
;
986 if (CHECK_FLAG(nhlfe
->flags
,
987 NHLFE_FLAG_INSTALLED
)
988 && !CHECK_FLAG(nhlfe
->flags
,
989 NHLFE_FLAG_SELECTED
)) {
990 UNSET_FLAG(nhlfe
->flags
,
991 NHLFE_FLAG_INSTALLED
);
992 UNSET_FLAG(nexthop
->flags
,
997 switch (dplane_lsp_update(lsp
)) {
998 case ZEBRA_DPLANE_REQUEST_QUEUED
:
999 zvrf
->lsp_installs_queued
++;
1001 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1002 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1003 "LSP Update Failure: %u",
1006 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1007 zvrf
->lsp_installs
++;
1018 * Callback upon processing completion of a LSP forwarding entry.
1020 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1022 struct zebra_vrf
*zvrf
;
1024 struct hash
*lsp_table
;
1025 zebra_nhlfe_t
*nhlfe
;
1027 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1030 lsp_table
= zvrf
->lsp_table
;
1031 if (!lsp_table
) // unexpected
1034 lsp
= (zebra_lsp_t
*)data
;
1035 if (!lsp
) // unexpected
1038 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1040 * delete LSP entry also.
1042 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1044 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1045 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1049 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1050 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1054 lsp_check_free(lsp_table
, &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
));
1096 nhlfe_list_init(&lsp
->nhlfe_list
);
1097 nhlfe_list_init(&lsp
->backup_nhlfe_list
);
1099 if (IS_ZEBRA_DEBUG_MPLS
)
1100 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1102 return ((void *)lsp
);
1106 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1108 static void lsp_check_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
)
1112 if (plsp
== NULL
|| *plsp
== NULL
)
1117 if ((nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) &&
1118 (nhlfe_list_first(&lsp
->backup_nhlfe_list
) == NULL
) &&
1119 !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1120 lsp_free(lsp_table
, plsp
);
1124 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1127 static void lsp_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
)
1130 zebra_nhlfe_t
*nhlfe
;
1132 if (plsp
== NULL
|| *plsp
== NULL
)
1137 if (IS_ZEBRA_DEBUG_MPLS
)
1138 zlog_debug("Free LSP in-label %u flags 0x%x",
1139 lsp
->ile
.in_label
, lsp
->flags
);
1141 /* Free nhlfes, if any. */
1142 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1145 /* Free backup nhlfes, if any. */
1146 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
)
1149 hash_release(lsp_table
, &lsp
->ile
);
1150 XFREE(MTYPE_LSP
, lsp
);
1156 * Create printable string for NHLFE entry.
1158 static char *nhlfe2str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1160 const struct nexthop
*nexthop
;
1163 nexthop
= nhlfe
->nexthop
;
1164 switch (nexthop
->type
) {
1165 case NEXTHOP_TYPE_IPV4
:
1166 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1167 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1169 case NEXTHOP_TYPE_IPV6
:
1170 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1171 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1173 case NEXTHOP_TYPE_IFINDEX
:
1174 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1183 * Check if NHLFE matches with search info passed.
1185 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1186 const union g_addr
*gate
, ifindex_t ifindex
)
1188 struct nexthop
*nhop
;
1191 nhop
= nhlfe
->nexthop
;
1195 if (nhop
->type
!= gtype
)
1198 switch (nhop
->type
) {
1199 case NEXTHOP_TYPE_IPV4
:
1200 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1201 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1202 sizeof(struct in_addr
));
1203 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1204 cmp
= !(nhop
->ifindex
== ifindex
);
1206 case NEXTHOP_TYPE_IPV6
:
1207 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1208 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1209 sizeof(struct in6_addr
));
1210 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1211 cmp
= !(nhop
->ifindex
== ifindex
);
1213 case NEXTHOP_TYPE_IFINDEX
:
1214 cmp
= !(nhop
->ifindex
== ifindex
);
1225 * Locate NHLFE that matches with passed info.
1227 static zebra_nhlfe_t
*nhlfe_find(struct nhlfe_list_head
*list
,
1228 enum lsp_types_t lsp_type
,
1229 enum nexthop_types_t gtype
,
1230 const union g_addr
*gate
, ifindex_t ifindex
)
1232 zebra_nhlfe_t
*nhlfe
;
1234 frr_each_safe(nhlfe_list
, list
, nhlfe
) {
1235 if (nhlfe
->type
!= lsp_type
)
1237 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1245 * Allocate and init new NHLFE.
1247 static zebra_nhlfe_t
*nhlfe_alloc(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1248 enum nexthop_types_t gtype
,
1249 const union g_addr
*gate
, ifindex_t ifindex
,
1251 const mpls_label_t
*labels
)
1253 zebra_nhlfe_t
*nhlfe
;
1254 struct nexthop
*nexthop
;
1258 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1261 nhlfe
->type
= lsp_type
;
1262 nhlfe
->distance
= lsp_distance(lsp_type
);
1264 nexthop
= nexthop_new();
1266 nexthop_add_labels(nexthop
, lsp_type
, num_labels
, labels
);
1268 nexthop
->vrf_id
= VRF_DEFAULT
;
1269 nexthop
->type
= gtype
;
1270 switch (nexthop
->type
) {
1271 case NEXTHOP_TYPE_IPV4
:
1272 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1273 nexthop
->gate
.ipv4
= gate
->ipv4
;
1275 nexthop
->ifindex
= ifindex
;
1277 case NEXTHOP_TYPE_IPV6
:
1278 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1279 nexthop
->gate
.ipv6
= gate
->ipv6
;
1281 nexthop
->ifindex
= ifindex
;
1283 case NEXTHOP_TYPE_IFINDEX
:
1284 nexthop
->ifindex
= ifindex
;
1287 nexthop_free(nexthop
);
1288 XFREE(MTYPE_NHLFE
, nhlfe
);
1291 nhlfe
->nexthop
= nexthop
;
1297 * Add primary or backup NHLFE. Base entry must have been created and
1298 * duplicate check done.
1300 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1301 enum nexthop_types_t gtype
,
1302 const union g_addr
*gate
, ifindex_t ifindex
,
1303 uint8_t num_labels
, const mpls_label_t
*labels
,
1306 zebra_nhlfe_t
*nhlfe
;
1311 /* Allocate new object */
1312 nhlfe
= nhlfe_alloc(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
1315 /* Enqueue to LSP: primaries at head of list, backups at tail */
1317 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
);
1318 nhlfe_list_add_tail(&lsp
->backup_nhlfe_list
, nhlfe
);
1320 nhlfe_list_add_head(&lsp
->nhlfe_list
, nhlfe
);
1326 * Common delete for NHLFEs.
1328 static void nhlfe_free(zebra_nhlfe_t
*nhlfe
)
1335 nexthop_free(nhlfe
->nexthop
);
1337 nhlfe
->nexthop
= NULL
;
1339 XFREE(MTYPE_NHLFE
, nhlfe
);
1344 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1346 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1357 if (nhlfe
== lsp
->best_nhlfe
)
1358 lsp
->best_nhlfe
= NULL
;
1360 /* Unlink from LSP */
1361 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
))
1362 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1364 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1374 * Update label for NHLFE entry.
1376 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1377 struct mpls_label_stack
*nh_label
)
1379 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1382 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1383 enum lsp_types_t type
)
1385 zebra_nhlfe_t
*nhlfe
;
1386 int schedule_lsp
= 0;
1389 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1392 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1393 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1394 /* Skip non-static NHLFEs */
1395 if (nhlfe
->type
!= type
)
1398 if (IS_ZEBRA_DEBUG_MPLS
) {
1399 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1401 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1402 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1405 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1406 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1407 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1414 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1415 /* Skip non-static NHLFEs */
1416 if (nhlfe
->type
!= type
)
1419 if (IS_ZEBRA_DEBUG_MPLS
) {
1420 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1422 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1423 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1426 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1427 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1428 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1435 /* Queue LSP for processing, if needed, else delete. */
1437 if (IS_ZEBRA_DEBUG_MPLS
) {
1438 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1439 lsp
->ile
.in_label
, lsp
->flags
);
1441 if (lsp_processq_add(lsp
))
1444 lsp_check_free(lsp_table
, &lsp
);
1451 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1452 * If no other NHLFEs exist, the entry would be deleted.
1454 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1455 mpls_label_t in_label
)
1457 struct hash
*lsp_table
;
1458 zebra_ile_t tmp_ile
;
1462 lsp_table
= zvrf
->lsp_table
;
1466 /* If entry is not present, exit. */
1467 tmp_ile
.in_label
= in_label
;
1468 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1469 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
1472 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1475 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1478 json_object
*json_nhlfe
= NULL
;
1479 json_object
*json_backups
= NULL
;
1480 json_object
*json_label_stack
;
1481 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1484 json_nhlfe
= json_object_new_object();
1485 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1486 json_object_int_add(json_nhlfe
, "outLabel",
1487 nexthop
->nh_label
->label
[0]);
1489 json_label_stack
= json_object_new_array();
1490 json_object_object_add(json_nhlfe
, "outLabelStack", json_label_stack
);
1491 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++)
1492 json_object_array_add(
1494 json_object_new_int(nexthop
->nh_label
->label
[i
]));
1496 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1498 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1499 json_object_boolean_true_add(json_nhlfe
, "installed");
1501 switch (nexthop
->type
) {
1502 case NEXTHOP_TYPE_IPV4
:
1503 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1504 json_object_string_add(json_nhlfe
, "nexthop",
1505 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
1508 case NEXTHOP_TYPE_IPV6
:
1509 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1510 json_object_string_add(
1511 json_nhlfe
, "nexthop",
1512 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1514 if (nexthop
->ifindex
)
1515 json_object_string_add(json_nhlfe
, "interface",
1516 ifindex2ifname(nexthop
->ifindex
,
1523 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1524 json_backups
= json_object_new_array();
1525 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1526 json_object_array_add(
1528 json_object_new_int(nexthop
->backup_idx
[i
]));
1531 json_object_object_add(json_nhlfe
, "backupIndex",
1539 * Print the NHLFE for a LSP forwarding entry.
1541 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
,
1544 struct nexthop
*nexthop
;
1545 char buf
[MPLS_LABEL_STRLEN
];
1547 nexthop
= nhlfe
->nexthop
;
1548 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1551 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1552 nhlfe_type2str(nhlfe
->type
),
1553 mpls_label2str(nexthop
->nh_label
->num_labels
,
1554 nexthop
->nh_label
->label
,
1555 buf
, sizeof(buf
), 0),
1559 vty_out(vty
, "%s", indent
);
1561 switch (nexthop
->type
) {
1562 case NEXTHOP_TYPE_IPV4
:
1563 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1564 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1565 if (nexthop
->ifindex
)
1566 vty_out(vty
, " dev %s",
1567 ifindex2ifname(nexthop
->ifindex
,
1570 case NEXTHOP_TYPE_IPV6
:
1571 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1572 vty_out(vty
, " via %s",
1573 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
1575 if (nexthop
->ifindex
)
1576 vty_out(vty
, " dev %s",
1577 ifindex2ifname(nexthop
->ifindex
,
1584 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
) ? " (backup)"
1587 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1593 * Print an LSP forwarding entry.
1595 static void lsp_print(struct vty
*vty
, zebra_lsp_t
*lsp
)
1597 zebra_nhlfe_t
*nhlfe
, *backup
;
1600 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1601 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1604 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1605 nhlfe_print(nhlfe
, vty
, NULL
);
1607 if (nhlfe
->nexthop
== NULL
||
1608 !CHECK_FLAG(nhlfe
->nexthop
->flags
,
1609 NEXTHOP_FLAG_HAS_BACKUP
))
1612 /* Backup nhlfes: find backups in backup list */
1614 for (j
= 0; j
< nhlfe
->nexthop
->backup_num
; j
++) {
1617 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, backup
) {
1618 if (i
== nhlfe
->nexthop
->backup_idx
[j
])
1624 vty_out(vty
, " [backup %d]", i
);
1625 nhlfe_print(backup
, vty
, " ");
1632 * JSON objects for an LSP forwarding entry.
1634 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1636 zebra_nhlfe_t
*nhlfe
= NULL
;
1637 json_object
*json
= json_object_new_object();
1638 json_object
*json_nhlfe_list
= json_object_new_array();
1640 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1642 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1643 json_object_boolean_true_add(json
, "installed");
1645 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1646 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1648 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1649 json_nhlfe_list
= NULL
;
1652 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1653 if (json_nhlfe_list
== NULL
)
1654 json_nhlfe_list
= json_object_new_array();
1656 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1659 if (json_nhlfe_list
)
1660 json_object_object_add(json
, "backupNexthops", json_nhlfe_list
);
1666 /* Return a sorted linked list of the hash contents */
1667 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1670 struct hash_bucket
*hb
;
1671 struct list
*sorted_list
= list_new();
1673 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1675 for (i
= 0; i
< hash
->size
; i
++)
1676 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1677 listnode_add_sort(sorted_list
, hb
->data
);
1683 * Compare two LSPs based on their label values.
1685 static int lsp_cmp(const zebra_lsp_t
*lsp1
, const zebra_lsp_t
*lsp2
)
1687 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1690 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1697 * Initialize work queue for processing changed LSPs.
1699 static int mpls_processq_init(void)
1701 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1702 if (!zrouter
.lsp_process_q
) {
1703 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1704 "%s: could not initialise work queue!", __func__
);
1708 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1709 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1710 zrouter
.lsp_process_q
->spec
.errorfunc
= NULL
;
1711 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1712 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1713 zrouter
.lsp_process_q
->spec
.hold
= 10;
1720 * Process LSP update results from zebra dataplane.
1722 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1724 struct zebra_vrf
*zvrf
;
1726 zebra_ile_t tmp_ile
;
1727 struct hash
*lsp_table
;
1729 zebra_nhlfe_t
*nhlfe
;
1730 struct nexthop
*nexthop
;
1731 enum dplane_op_e op
;
1732 enum zebra_dplane_result status
;
1733 enum zebra_sr_policy_update_label_mode update_mode
;
1735 op
= dplane_ctx_get_op(ctx
);
1736 status
= dplane_ctx_get_status(ctx
);
1738 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1739 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1740 ctx
, dplane_op2str(op
),
1741 dplane_ctx_get_in_label(ctx
),
1742 dplane_res2str(status
));
1744 label
= dplane_ctx_get_in_label(ctx
);
1747 case DPLANE_OP_LSP_INSTALL
:
1748 case DPLANE_OP_LSP_UPDATE
:
1749 /* Look for zebra LSP object */
1750 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1754 lsp_table
= zvrf
->lsp_table
;
1756 tmp_ile
.in_label
= label
;
1757 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1759 if (IS_ZEBRA_DEBUG_DPLANE
)
1760 zlog_debug("LSP ctx %p: in-label %u not found",
1761 ctx
, dplane_ctx_get_in_label(ctx
));
1765 /* TODO -- Confirm that this result is still 'current' */
1767 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1768 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1769 clear_nhlfe_installed(lsp
);
1770 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1771 "LSP Install Failure: in-label %u",
1776 /* Update zebra object */
1777 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1778 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1779 nexthop
= nhlfe
->nexthop
;
1783 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
) &&
1784 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1785 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1786 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1790 update_mode
= (op
== DPLANE_OP_LSP_INSTALL
)
1791 ? ZEBRA_SR_POLICY_LABEL_CREATED
1792 : ZEBRA_SR_POLICY_LABEL_UPDATED
;
1793 zebra_sr_policy_label_update(label
, update_mode
);
1796 case DPLANE_OP_LSP_DELETE
:
1797 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1798 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1799 "LSP Deletion Failure: in-label %u",
1800 dplane_ctx_get_in_label(ctx
));
1803 zebra_sr_policy_label_update(label
,
1804 ZEBRA_SR_POLICY_LABEL_REMOVED
);
1812 dplane_ctx_fini(&ctx
);
1816 * Process LSP installation info from two sets of nhlfes: a set from
1817 * a dplane notification, and a set from the zebra LSP object. Update
1818 * counters of installed nexthops, and return whether the LSP has changed.
1820 static bool compare_notif_nhlfes(const struct nhlfe_list_head
*ctx_head
,
1821 struct nhlfe_list_head
*nhlfe_head
,
1822 int *start_counter
, int *end_counter
)
1824 zebra_nhlfe_t
*nhlfe
;
1825 const zebra_nhlfe_t
*ctx_nhlfe
;
1826 struct nexthop
*nexthop
;
1827 const struct nexthop
*ctx_nexthop
;
1828 int start_count
= 0, end_count
= 0;
1829 bool changed_p
= false;
1830 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1832 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1833 char buf
[NEXTHOP_STRLEN
];
1835 nexthop
= nhlfe
->nexthop
;
1839 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1844 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1845 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1849 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1850 nexthop_same(ctx_nexthop
, nexthop
)) {
1857 nexthop2str(nexthop
, buf
, sizeof(buf
));
1859 if (ctx_nhlfe
&& ctx_nexthop
) {
1861 const char *tstr
= "";
1863 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1864 NHLFE_FLAG_INSTALLED
))
1867 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1871 /* Test zebra nhlfe install state */
1872 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1873 NHLFE_FLAG_INSTALLED
)) {
1875 if (!CHECK_FLAG(nhlfe
->flags
,
1876 NHLFE_FLAG_INSTALLED
))
1879 /* Update counter */
1883 if (CHECK_FLAG(nhlfe
->flags
,
1884 NHLFE_FLAG_INSTALLED
))
1889 /* Not mentioned in lfib set -> uninstalled */
1890 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1891 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1892 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1897 zlog_debug("LSP dplane notif: no match, nh %s",
1903 *start_counter
+= start_count
;
1905 *end_counter
+= end_count
;
1911 * Update an lsp nhlfe list from a dplane context, typically an async
1912 * notification context. Update the LSP list to match the installed
1913 * status from the context's list.
1915 static int update_nhlfes_from_ctx(struct nhlfe_list_head
*nhlfe_head
,
1916 const struct nhlfe_list_head
*ctx_head
)
1919 zebra_nhlfe_t
*nhlfe
;
1920 const zebra_nhlfe_t
*ctx_nhlfe
;
1921 struct nexthop
*nexthop
;
1922 const struct nexthop
*ctx_nexthop
;
1923 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1925 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1926 char buf
[NEXTHOP_STRLEN
];
1928 nexthop
= nhlfe
->nexthop
;
1934 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1935 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1939 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1940 nexthop_same(ctx_nexthop
, nexthop
)) {
1947 nexthop2str(nexthop
, buf
, sizeof(buf
));
1949 if (ctx_nhlfe
&& ctx_nexthop
) {
1951 /* Bring zebra nhlfe install state into sync */
1952 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1953 NHLFE_FLAG_INSTALLED
)) {
1955 zlog_debug("%s: matched lsp nhlfe %s (installed)",
1958 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1959 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
1963 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
1966 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1967 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
1970 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
1971 NEXTHOP_FLAG_FIB
)) {
1972 SET_FLAG(nhlfe
->nexthop
->flags
,
1973 NEXTHOP_FLAG_ACTIVE
);
1974 SET_FLAG(nhlfe
->nexthop
->flags
,
1977 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1978 NEXTHOP_FLAG_ACTIVE
);
1979 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1984 /* Not mentioned in lfib set -> uninstalled */
1986 zlog_debug("%s: no match for lsp nhlfe %s",
1988 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1989 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
1990 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1991 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1999 * Process async dplane notifications.
2001 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2003 struct zebra_vrf
*zvrf
;
2004 zebra_ile_t tmp_ile
;
2005 struct hash
*lsp_table
;
2007 const struct nhlfe_list_head
*ctx_list
;
2008 int start_count
= 0, end_count
= 0; /* Installed counts */
2009 bool changed_p
= false;
2010 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
2013 zlog_debug("LSP dplane notif, in-label %u",
2014 dplane_ctx_get_in_label(ctx
));
2016 /* Look for zebra LSP object */
2017 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2021 lsp_table
= zvrf
->lsp_table
;
2023 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
2024 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2027 zlog_debug("dplane LSP notif: in-label %u not found",
2028 dplane_ctx_get_in_label(ctx
));
2033 * The dataplane/forwarding plane is notifying zebra about the state
2034 * of the nexthops associated with this LSP. First, we take a
2035 * pre-scan pass to determine whether the LSP has transitioned
2036 * from installed -> uninstalled. In that case, we need to have
2037 * the existing state of the LSP objects available before making
2040 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2042 changed_p
= compare_notif_nhlfes(ctx_list
, &lsp
->nhlfe_list
,
2043 &start_count
, &end_count
);
2046 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2047 start_count
, end_count
,
2048 changed_p
? ", changed" : "");
2050 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2052 if (compare_notif_nhlfes(ctx_list
, &lsp
->backup_nhlfe_list
,
2053 &start_count
, &end_count
))
2054 /* Avoid accidentally setting back to 'false' */
2058 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2059 start_count
, end_count
,
2060 changed_p
? ", changed" : "");
2063 * Has the LSP become uninstalled? We need the existing state of the
2064 * nexthops/nhlfes at this point so we know what to delete.
2066 if (start_count
> 0 && end_count
== 0) {
2067 /* Inform other lfibs */
2068 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
2072 * Now we take a second pass and bring the zebra
2073 * nexthop state into sync with the forwarding-plane state.
2075 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2076 update_nhlfes_from_ctx(&lsp
->nhlfe_list
, ctx_list
);
2078 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2079 update_nhlfes_from_ctx(&lsp
->backup_nhlfe_list
, ctx_list
);
2081 if (end_count
> 0) {
2082 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2085 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2088 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2089 clear_nhlfe_installed(lsp
);
2093 dplane_ctx_fini(&ctx
);
2097 * Install dynamic LSP entry.
2099 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2100 struct route_entry
*re
)
2102 struct route_table
*table
;
2105 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2109 /* See if there is a configured label binding for this FEC. */
2110 fec
= fec_find(table
, &rn
->p
);
2111 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2114 /* We cannot install a label forwarding entry if local label is the
2115 * implicit-null label.
2117 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2120 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2127 * Uninstall dynamic LSP entry, if any.
2129 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2130 struct route_entry
*re
)
2132 struct route_table
*table
;
2135 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2139 /* See if there is a configured label binding for this FEC. */
2140 fec
= fec_find(table
, &rn
->p
);
2141 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2144 /* Uninstall always removes all dynamic NHLFEs. */
2145 return lsp_uninstall(zvrf
, fec
->label
);
2149 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2150 * the LSP object - nothing is scheduled for processing, for example.
2152 zebra_nhlfe_t
*zebra_mpls_lsp_add_nhlfe(zebra_lsp_t
*lsp
,
2153 enum lsp_types_t lsp_type
,
2154 enum nexthop_types_t gtype
,
2155 const union g_addr
*gate
,
2158 const mpls_label_t
*out_labels
)
2160 /* Just a public pass-through to the internal implementation */
2161 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2162 out_labels
, false /*backup*/);
2166 * Add a backup NHLFE to an LSP, return the newly-added object.
2167 * This path only changes the LSP object - nothing is scheduled for
2168 * processing, for example.
2170 zebra_nhlfe_t
*zebra_mpls_lsp_add_backup_nhlfe(zebra_lsp_t
*lsp
,
2171 enum lsp_types_t lsp_type
,
2172 enum nexthop_types_t gtype
,
2173 const union g_addr
*gate
,
2176 const mpls_label_t
*out_labels
)
2178 /* Just a public pass-through to the internal implementation */
2179 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2184 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2186 zebra_nhlfe_t
*zebra_mpls_lsp_add_nh(zebra_lsp_t
*lsp
,
2187 enum lsp_types_t lsp_type
,
2188 const struct nexthop
*nh
)
2190 zebra_nhlfe_t
*nhlfe
;
2192 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2195 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
, nh
->ifindex
,
2196 nh
->nh_label
->num_labels
, nh
->nh_label
->label
,
2203 * Add a backup NHLFE to an LSP based on a nexthop;
2204 * return the newly-added object.
2206 zebra_nhlfe_t
*zebra_mpls_lsp_add_backup_nh(zebra_lsp_t
*lsp
,
2207 enum lsp_types_t lsp_type
,
2208 const struct nexthop
*nh
)
2210 zebra_nhlfe_t
*nhlfe
;
2212 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2215 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
,
2216 nh
->ifindex
, nh
->nh_label
->num_labels
,
2217 nh
->nh_label
->label
, true);
2223 * Free an allocated NHLFE
2225 void zebra_mpls_nhlfe_free(zebra_nhlfe_t
*nhlfe
)
2227 /* Just a pass-through to the internal implementation */
2232 * Registration from a client for the label binding for a FEC. If a binding
2233 * already exists, it is informed to the client.
2234 * NOTE: If there is a manually configured label binding, that is used.
2235 * Otherwise, if a label index is specified, it means we have to allocate the
2236 * label from a locally configured label block (SRGB), if one exists and index
2237 * is acceptable. If no label index then just register the specified label.
2238 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2239 * by the calling function. Register requests with both will be rejected.
2241 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2242 uint32_t label
, uint32_t label_index
,
2243 struct zserv
*client
)
2245 struct route_table
*table
;
2249 bool label_change
= false;
2251 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2252 bool is_configured_fec
= false; /* indicate statically configured FEC */
2254 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2258 if (IS_ZEBRA_DEBUG_MPLS
)
2259 prefix2str(p
, buf
, BUFSIZ
);
2261 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2263 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2264 "Rejecting FEC register for %s with both label %u and Label Index %u specified, client %s",
2265 buf
, label
, label_index
,
2266 zebra_route_string(client
->proto
));
2271 fec
= fec_find(table
, p
);
2273 fec
= fec_add(table
, p
, label
, 0, label_index
);
2276 EC_ZEBRA_FEC_ADD_FAILED
,
2277 "Failed to add FEC %s upon register, client %s",
2278 buf
, zebra_route_string(client
->proto
));
2282 old_label
= MPLS_INVALID_LABEL
;
2285 /* Check if the FEC has been statically defined in the config */
2286 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2287 /* Client may register same FEC with different label index. */
2289 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2290 if (!new_client
&& fec
->label_index
== label_index
2291 && fec
->label
== label
)
2292 /* Duplicate register */
2295 /* Save current label, update the FEC */
2296 old_label
= fec
->label
;
2297 fec
->label_index
= label_index
;
2301 listnode_add(fec
->client_list
, client
);
2303 if (IS_ZEBRA_DEBUG_MPLS
)
2304 zlog_debug("FEC %s label%s %u %s by client %s%s", buf
,
2305 have_label_index
? " index" : "",
2306 have_label_index
? label_index
: label
,
2307 new_client
? "registered" : "updated",
2308 zebra_route_string(client
->proto
),
2310 ? ", but using statically configured label"
2313 /* If not a statically configured FEC, derive the local label
2314 * from label index or use the provided label
2316 if (!is_configured_fec
) {
2317 if (have_label_index
)
2318 fec_derive_label_from_index(zvrf
, fec
);
2322 /* If no label change, exit. */
2323 if (fec
->label
== old_label
)
2326 label_change
= true;
2329 /* If new client or label change, update client and install or uninstall
2330 * label forwarding entry as needed.
2332 /* Inform client of label, if needed. */
2333 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2334 if (IS_ZEBRA_DEBUG_MPLS
)
2335 zlog_debug("Update client label %u", fec
->label
);
2336 fec_send(fec
, client
);
2339 if (new_client
|| label_change
)
2340 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2346 * Deregistration from a client for the label binding for a FEC. The FEC
2347 * itself is deleted if no other registered clients exist and there is no
2348 * label bound to the FEC.
2350 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2351 struct zserv
*client
)
2353 struct route_table
*table
;
2357 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2361 if (IS_ZEBRA_DEBUG_MPLS
)
2362 prefix2str(p
, buf
, BUFSIZ
);
2364 fec
= fec_find(table
, p
);
2366 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2367 "Failed to find FEC %pFX upon unregister, client %s",
2368 p
, zebra_route_string(client
->proto
));
2372 listnode_delete(fec
->client_list
, client
);
2374 if (IS_ZEBRA_DEBUG_MPLS
)
2375 zlog_debug("FEC %s unregistered by client %s", buf
,
2376 zebra_route_string(client
->proto
));
2378 /* If not a configured entry, delete the FEC if no other clients. Before
2379 * deleting, see if any LSP needs to be uninstalled.
2381 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2382 && list_isempty(fec
->client_list
)) {
2383 mpls_label_t old_label
= fec
->label
;
2384 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2385 fec_change_update_lsp(zvrf
, fec
, old_label
);
2393 * Cleanup any FECs registered by this client.
2395 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2397 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2398 struct route_node
*rn
;
2400 struct listnode
*node
;
2401 struct zserv
*fec_client
;
2404 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2405 if (zvrf
->fec_table
[af
] == NULL
)
2408 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2409 rn
= route_next(rn
)) {
2411 if (!fec
|| list_isempty(fec
->client_list
))
2414 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2416 if (fec_client
== client
) {
2417 listnode_delete(fec
->client_list
,
2419 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2420 && list_isempty(fec
->client_list
))
2431 struct lsp_uninstall_args
{
2432 struct hash
*lsp_table
;
2433 enum lsp_types_t type
;
2437 * Cleanup MPLS labels registered by this client.
2439 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2442 struct zebra_vrf
*zvrf
;
2444 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2445 struct lsp_uninstall_args args
;
2452 args
.lsp_table
= zvrf
->lsp_table
;
2453 args
.type
= lsp_type_from_re_type(client
->proto
);
2454 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2458 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2459 lsp_type_from_re_type(client
->proto
));
2460 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2461 lsp_type_from_re_type(client
->proto
));
2468 * Return FEC (if any) to which this label is bound.
2469 * Note: Only works for per-prefix binding and when the label is not
2471 * TODO: Currently walks entire table, can optimize later with another
2474 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2477 struct route_node
*rn
;
2481 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2482 if (zvrf
->fec_table
[af
] == NULL
)
2485 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2486 rn
= route_next(rn
)) {
2490 if (fec
->label
== label
)
2499 * Inform if specified label is currently bound to a FEC or not.
2501 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2503 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2507 * Add static FEC to label binding. If there are clients registered for this
2508 * FEC, notify them. If there are labeled routes for this FEC, install the
2509 * label forwarding entry.
2511 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2512 mpls_label_t in_label
)
2514 struct route_table
*table
;
2517 mpls_label_t old_label
;
2520 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2524 if (IS_ZEBRA_DEBUG_MPLS
)
2525 prefix2str(p
, buf
, BUFSIZ
);
2527 /* Update existing FEC or create a new one. */
2528 fec
= fec_find(table
, p
);
2530 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2531 MPLS_INVALID_LABEL_INDEX
);
2533 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2534 "Failed to add FEC %pFX upon config", p
);
2538 if (IS_ZEBRA_DEBUG_MPLS
)
2539 zlog_debug("Add fec %s label %u", buf
, in_label
);
2541 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2542 if (fec
->label
== in_label
)
2543 /* Duplicate config */
2546 /* Label change, update clients. */
2547 old_label
= fec
->label
;
2548 if (IS_ZEBRA_DEBUG_MPLS
)
2549 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2551 fec
->label
= in_label
;
2552 fec_update_clients(fec
);
2554 /* Update label forwarding entries appropriately */
2555 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2562 * Remove static FEC to label binding. If there are no clients registered
2563 * for this FEC, delete the FEC; else notify clients
2564 * Note: Upon delete of static binding, if label index exists for this FEC,
2565 * client may need to be updated with derived label.
2567 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2569 struct route_table
*table
;
2571 mpls_label_t old_label
;
2574 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2578 fec
= fec_find(table
, p
);
2580 prefix2str(p
, buf
, BUFSIZ
);
2581 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2582 "Failed to find FEC %s upon delete", buf
);
2586 if (IS_ZEBRA_DEBUG_MPLS
) {
2587 zlog_debug("Delete fec %pFX label %u label index %u", p
,
2588 fec
->label
, fec
->label_index
);
2591 old_label
= fec
->label
;
2592 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2593 fec
->label
= MPLS_INVALID_LABEL
;
2595 /* If no client exists, just delete the FEC. */
2596 if (list_isempty(fec
->client_list
)) {
2601 /* Derive the local label (from label index) or reset it. */
2602 fec_derive_label_from_index(zvrf
, fec
);
2604 /* If there is a label change, update clients. */
2605 if (fec
->label
== old_label
)
2607 fec_update_clients(fec
);
2609 /* Update label forwarding entries appropriately */
2610 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2614 * Display MPLS FEC to label binding configuration (VTY command handler).
2616 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2618 struct route_node
*rn
;
2623 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2624 if (zvrf
->fec_table
[af
] == NULL
)
2627 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2628 rn
= route_next(rn
)) {
2635 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2639 vty_out(vty
, "mpls label bind %pFX %s\n", &rn
->p
,
2640 label2str(fec
->label
, lstr
, BUFSIZ
));
2648 * Display MPLS FEC to label binding (VTY command handler).
2650 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2652 struct route_node
*rn
;
2655 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2656 if (zvrf
->fec_table
[af
] == NULL
)
2659 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2660 rn
= route_next(rn
)) {
2663 fec_print(rn
->info
, vty
);
2669 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2671 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2674 struct route_table
*table
;
2675 struct route_node
*rn
;
2677 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2682 rn
= route_node_lookup(table
, p
);
2686 route_unlock_node(rn
);
2690 fec_print(rn
->info
, vty
);
2693 static void mpls_zebra_nhe_update(struct route_entry
*re
, afi_t afi
,
2694 struct nhg_hash_entry
*new_nhe
)
2696 struct nhg_hash_entry
*nhe
;
2698 nhe
= zebra_nhg_rib_find_nhe(new_nhe
, afi
);
2700 route_entry_update_nhe(re
, nhe
);
2703 static bool ftn_update_nexthop(bool add_p
, struct nexthop
*nexthop
,
2704 enum lsp_types_t type
,
2705 const struct zapi_nexthop
*znh
)
2707 if (add_p
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2708 nexthop_add_labels(nexthop
, type
, znh
->label_num
, znh
->labels
);
2709 else if (!add_p
&& nexthop
->nh_label_type
== type
)
2710 nexthop_del_labels(nexthop
);
2717 int mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2718 struct prefix
*prefix
, uint8_t route_type
,
2719 unsigned short route_instance
)
2721 struct route_table
*table
;
2722 struct route_node
*rn
;
2723 struct route_entry
*re
;
2724 struct nexthop
*nexthop
;
2725 struct nhg_hash_entry
*new_nhe
;
2726 afi_t afi
= family2afi(prefix
->family
);
2729 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2733 /* Lookup existing route */
2734 rn
= route_node_get(table
, prefix
);
2735 RNODE_FOREACH_RE (rn
, re
) {
2736 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2738 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2745 * Nexthops are now shared by multiple routes, so we have to make
2746 * a local copy, modify the copy, then update the route.
2748 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2750 for (nexthop
= new_nhe
->nhg
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2751 nexthop_del_labels(nexthop
);
2753 /* Update backup routes/nexthops also, if present. */
2754 if (zebra_nhg_get_backup_nhg(new_nhe
) != NULL
) {
2755 for (nexthop
= new_nhe
->backup_info
->nhe
->nhg
.nexthop
; nexthop
;
2756 nexthop
= nexthop
->next
)
2757 nexthop_del_labels(nexthop
);
2760 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2761 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2763 /* This will create (or ref) a new nhe, so we will discard the local
2766 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
2768 zebra_nhg_free(new_nhe
);
2776 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2777 * update its labels according to 'add_p', and return 'true' if successful.
2779 static bool ftn_update_znh(bool add_p
, enum lsp_types_t type
,
2780 struct nexthop
*head
, const struct zapi_nexthop
*znh
)
2782 bool found
= false, success
= false;
2783 struct nexthop
*nexthop
;
2785 for (nexthop
= head
; nexthop
; nexthop
= nexthop
->next
) {
2786 switch (nexthop
->type
) {
2787 case NEXTHOP_TYPE_IPV4
:
2788 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2789 if (znh
->type
!= NEXTHOP_TYPE_IPV4
2790 && znh
->type
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2792 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
,
2795 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2796 && nexthop
->ifindex
!= znh
->ifindex
)
2801 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2806 case NEXTHOP_TYPE_IPV6
:
2807 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2808 if (znh
->type
!= NEXTHOP_TYPE_IPV6
2809 && znh
->type
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2811 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
,
2814 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2815 && nexthop
->ifindex
!= znh
->ifindex
)
2820 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2836 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2837 * using zapi message info.
2838 * There are several changes that need to be made, in several zebra
2839 * data structures, so we want to do all the work required at once.
2841 int mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
2842 const struct zapi_labels
*zl
)
2844 int i
, counter
, ret
= 0;
2845 char buf
[NEXTHOP_STRLEN
];
2846 const struct zapi_nexthop
*znh
;
2847 struct route_table
*table
;
2848 struct route_node
*rn
= NULL
;
2849 struct route_entry
*re
= NULL
;
2850 struct nhg_hash_entry
*new_nhe
= NULL
;
2853 const struct prefix
*prefix
= NULL
;
2854 struct hash
*lsp_table
;
2855 zebra_ile_t tmp_ile
;
2856 zebra_lsp_t
*lsp
= NULL
;
2858 /* Prep LSP for add case */
2861 lsp_table
= zvrf
->lsp_table
;
2865 /* Find or create LSP object */
2866 tmp_ile
.in_label
= zl
->local_label
;
2867 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2872 /* Prep for route/FEC update if requested */
2873 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2874 prefix
= &zl
->route
.prefix
;
2876 afi
= family2afi(prefix
->family
);
2879 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2881 /* Lookup existing route */
2882 rn
= route_node_get(table
, prefix
);
2883 RNODE_FOREACH_RE(rn
, re
) {
2884 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2886 if (re
->type
== zl
->route
.type
&&
2887 re
->instance
== zl
->route
.instance
)
2894 * Copy over current nexthops into a temporary group.
2895 * We can't just change the values here since the nhgs
2896 * are shared and if the labels change, we'll need
2897 * to find or create a new nhg. We need to create
2898 * a whole temporary group, make changes to it,
2899 * then attach that to the route.
2901 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2905 * The old version of the zapi code
2906 * attempted to manage LSPs before trying to
2907 * find a route/FEC, so we'll continue that way.
2909 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
)
2911 "%s: FTN update requested: no route for prefix %pFX",
2917 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2921 for (i
= 0; i
< zl
->nexthop_num
; i
++) {
2923 znh
= &zl
->nexthops
[i
];
2925 /* Attempt LSP update */
2927 ret
= lsp_znh_install(lsp
, zl
->type
, znh
);
2929 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
2930 zl
->local_label
, znh
->type
,
2931 &znh
->gate
, znh
->ifindex
,
2934 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
) {
2935 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2936 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
2937 __func__
, (add_p
? "" : "un"),
2938 zl
->local_label
, buf
);
2943 /* Attempt route/FEC update if requested */
2947 /* Search the route's nexthops for a match, and update it. */
2948 found
= ftn_update_znh(add_p
, zl
->type
, new_nhe
->nhg
.nexthop
,
2952 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
2953 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2955 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
2956 __func__
, prefix
, zl
->local_label
, buf
);
2961 * Process backup LSPs/nexthop entries also. We associate backup
2962 * LSP info with backup nexthops.
2964 if (!CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
))
2967 for (i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
2969 znh
= &zl
->backup_nexthops
[i
];
2972 ret
= lsp_backup_znh_install(lsp
, zl
->type
, znh
);
2974 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
2976 znh
->type
, &znh
->gate
,
2977 znh
->ifindex
, true);
2980 if (IS_ZEBRA_DEBUG_RECV
||
2981 IS_ZEBRA_DEBUG_MPLS
) {
2982 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2983 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
2984 __func__
, (add_p
? "" : "un"),
2985 zl
->local_label
, buf
);
2990 /* Attempt backup nexthop/FEC update if requested */
2991 if (re
== NULL
|| zebra_nhg_get_backup_nhg(new_nhe
) == NULL
)
2994 /* Search the route's backup nexthops for a match
2997 found
= ftn_update_znh(add_p
, zl
->type
,
2998 new_nhe
->backup_info
->nhe
->nhg
.nexthop
,
3002 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
3003 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3005 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
3006 __func__
, prefix
, zl
->local_label
, buf
);
3013 * If we made changes, update the route, and schedule it
3014 * for rib processing
3016 if (re
!= NULL
&& counter
> 0) {
3019 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3020 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
3022 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3028 zebra_nhg_free(new_nhe
);
3034 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3035 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3036 * the out-label for an existing NHLFE (update case).
3038 static zebra_nhlfe_t
*
3039 lsp_add_nhlfe(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3040 uint8_t num_out_labels
, const mpls_label_t
*out_labels
,
3041 enum nexthop_types_t gtype
, const union g_addr
*gate
,
3042 ifindex_t ifindex
, bool is_backup
)
3044 zebra_nhlfe_t
*nhlfe
;
3045 char buf
[MPLS_LABEL_STRLEN
];
3046 const char *backup_str
;
3049 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3051 backup_str
= "backup ";
3053 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3059 struct nexthop
*nh
= nhlfe
->nexthop
;
3062 assert(nh
->nh_label
);
3064 /* Clear deleted flag (in case it was set) */
3065 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3066 if (nh
->nh_label
->num_labels
== num_out_labels
3067 && !memcmp(nh
->nh_label
->label
, out_labels
,
3068 sizeof(mpls_label_t
) * num_out_labels
))
3072 if (IS_ZEBRA_DEBUG_MPLS
) {
3073 char buf2
[MPLS_LABEL_STRLEN
];
3074 char buf3
[MPLS_LABEL_STRLEN
];
3076 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3077 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3079 mpls_label2str(nh
->nh_label
->num_labels
,
3080 nh
->nh_label
->label
, buf3
, sizeof(buf3
),
3083 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3084 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3088 /* Update out label(s), trigger processing. */
3089 if (nh
->nh_label
->num_labels
== num_out_labels
)
3090 memcpy(nh
->nh_label
->label
, out_labels
,
3091 sizeof(mpls_label_t
) * num_out_labels
);
3093 nexthop_del_labels(nh
);
3094 nexthop_add_labels(nh
, type
, num_out_labels
,
3098 /* Add LSP entry to this nexthop */
3099 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
,
3100 num_out_labels
, out_labels
, is_backup
);
3104 if (IS_ZEBRA_DEBUG_MPLS
) {
3105 char buf2
[MPLS_LABEL_STRLEN
];
3107 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3108 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3111 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3112 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3116 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
3119 /* Mark NHLFE, queue LSP for processing. */
3120 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3126 * Install an LSP and forwarding entry; used primarily
3127 * from vrf zapi message processing.
3129 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3130 mpls_label_t in_label
, uint8_t num_out_labels
,
3131 const mpls_label_t
*out_labels
, enum nexthop_types_t gtype
,
3132 const union g_addr
*gate
, ifindex_t ifindex
)
3134 struct hash
*lsp_table
;
3135 zebra_ile_t tmp_ile
;
3137 zebra_nhlfe_t
*nhlfe
;
3140 lsp_table
= zvrf
->lsp_table
;
3144 /* Find or create LSP object */
3145 tmp_ile
.in_label
= in_label
;
3146 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
3150 nhlfe
= lsp_add_nhlfe(lsp
, type
, num_out_labels
, out_labels
, gtype
,
3151 gate
, ifindex
, false /*backup*/);
3155 /* Queue LSP for processing. */
3156 if (lsp_processq_add(lsp
))
3163 * Install or replace NHLFE, using info from zapi nexthop
3165 static int lsp_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3166 const struct zapi_nexthop
*znh
)
3168 zebra_nhlfe_t
*nhlfe
;
3170 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
, znh
->labels
,
3171 znh
->type
, &znh
->gate
, znh
->ifindex
,
3176 /* Update backup info if present */
3177 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
3178 if (znh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
3183 nhlfe
->nexthop
->backup_num
= znh
->backup_num
;
3184 memcpy(nhlfe
->nexthop
->backup_idx
, znh
->backup_idx
,
3186 SET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3188 /* Ensure there's no stale backup info */
3189 UNSET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3190 nhlfe
->nexthop
->backup_num
= 0;
3193 /* Queue LSP for processing. */
3194 if (lsp_processq_add(lsp
))
3201 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3203 static int lsp_backup_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3204 const struct zapi_nexthop
*znh
)
3206 zebra_nhlfe_t
*nhlfe
;
3208 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
,
3209 znh
->labels
, znh
->type
, &znh
->gate
,
3210 znh
->ifindex
, true /*backup*/);
3211 if (nhlfe
== NULL
) {
3212 if (IS_ZEBRA_DEBUG_MPLS
)
3213 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3214 __func__
, lsp
->ile
.in_label
);
3218 /* Queue LSP for processing. */
3219 if (lsp_processq_add(lsp
))
3225 zebra_lsp_t
*mpls_lsp_find(struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
3227 struct hash
*lsp_table
;
3228 zebra_ile_t tmp_ile
;
3231 lsp_table
= zvrf
->lsp_table
;
3235 /* If entry is not present, exit. */
3236 tmp_ile
.in_label
= in_label
;
3237 return hash_lookup(lsp_table
, &tmp_ile
);
3241 * Uninstall a particular NHLFE in the forwarding table. If this is
3242 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3244 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3245 mpls_label_t in_label
, enum nexthop_types_t gtype
,
3246 const union g_addr
*gate
, ifindex_t ifindex
,
3249 struct hash
*lsp_table
;
3250 zebra_ile_t tmp_ile
;
3252 zebra_nhlfe_t
*nhlfe
;
3253 char buf
[NEXTHOP_STRLEN
];
3254 bool schedule_lsp
= false;
3257 lsp_table
= zvrf
->lsp_table
;
3261 /* If entry is not present, exit. */
3262 tmp_ile
.in_label
= in_label
;
3263 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3268 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3271 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3276 if (IS_ZEBRA_DEBUG_MPLS
) {
3277 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3278 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3279 in_label
, type
, buf
, nhlfe
->flags
);
3282 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ||
3283 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
3284 schedule_lsp
= true;
3286 /* Mark NHLFE for delete or directly delete, as appropriate. */
3288 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3289 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3291 if (IS_ZEBRA_DEBUG_MPLS
)
3292 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3293 lsp
->ile
.in_label
, lsp
->flags
);
3294 if (lsp_processq_add(lsp
))
3299 /* Free LSP entry if no other NHLFEs and not scheduled. */
3300 lsp_check_free(lsp_table
, &lsp
);
3305 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3306 mpls_label_t in_label
)
3308 struct hash
*lsp_table
;
3309 zebra_ile_t tmp_ile
;
3313 lsp_table
= zvrf
->lsp_table
;
3317 /* If entry is not present, exit. */
3318 tmp_ile
.in_label
= in_label
;
3319 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3323 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
3327 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3328 * If no other NHLFEs exist, the entry would be deleted.
3330 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
3332 struct lsp_uninstall_args
*args
= ctxt
;
3334 struct hash
*lsp_table
;
3336 lsp
= (zebra_lsp_t
*)bucket
->data
;
3337 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
)
3340 lsp_table
= args
->lsp_table
;
3344 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
3348 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3351 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
3352 int afi
, enum lsp_types_t lsp_type
)
3354 struct route_table
*table
;
3355 struct route_node
*rn
;
3356 struct route_entry
*re
;
3357 struct nexthop
*nexthop
;
3358 struct nexthop_group
*nhg
;
3361 /* Process routes of interested address-families. */
3362 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
3366 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
3369 RNODE_FOREACH_RE (rn
, re
) {
3370 struct nhg_hash_entry
*new_nhe
;
3372 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
3374 nhg
= &new_nhe
->nhg
;
3375 for (nexthop
= nhg
->nexthop
; nexthop
;
3376 nexthop
= nexthop
->next
) {
3377 if (nexthop
->nh_label_type
!= lsp_type
)
3380 nexthop_del_labels(nexthop
);
3381 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3382 SET_FLAG(re
->status
,
3383 ROUTE_ENTRY_LABELS_CHANGED
);
3387 /* Check for backup info and update that also */
3388 nhg
= zebra_nhg_get_backup_nhg(new_nhe
);
3390 for (nexthop
= nhg
->nexthop
; nexthop
;
3391 nexthop
= nexthop
->next
) {
3392 if (nexthop
->nh_label_type
!= lsp_type
)
3395 nexthop_del_labels(nexthop
);
3396 SET_FLAG(re
->status
,
3397 ROUTE_ENTRY_CHANGED
);
3398 SET_FLAG(re
->status
,
3399 ROUTE_ENTRY_LABELS_CHANGED
);
3404 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
3405 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3407 zebra_nhg_free(new_nhe
);
3415 #if defined(HAVE_CUMULUS)
3417 * Check that the label values used in LSP creation are consistent. The
3418 * main criteria is that if there is ECMP, the label operation must still
3419 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3420 * to current HW restrictions.
3422 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
3423 mpls_label_t in_label
,
3424 mpls_label_t out_label
,
3425 enum nexthop_types_t gtype
,
3426 union g_addr
*gate
, ifindex_t ifindex
)
3428 struct hash
*slsp_table
;
3429 zebra_ile_t tmp_ile
;
3431 zebra_nhlfe_t
*nhlfe
;
3432 const struct nexthop
*nh
;
3435 slsp_table
= zvrf
->slsp_table
;
3439 /* If entry is not present, exit. */
3440 tmp_ile
.in_label
= in_label
;
3441 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3445 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3446 gtype
, gate
, ifindex
);
3448 nh
= nhlfe
->nexthop
;
3450 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3453 if (nh
->nh_label
->label
[0] == out_label
)
3456 /* If not only NHLFE, cannot allow label change. */
3457 if (nhlfe
!= nhlfe_list_first(&lsp
->nhlfe_list
) ||
3458 nhlfe_list_next(&lsp
->nhlfe_list
, nhlfe
) != NULL
)
3461 /* If other NHLFEs exist, label operation must match. */
3462 nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
);
3463 if (nhlfe
!= NULL
) {
3466 nh
= nhlfe
->nexthop
;
3468 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3471 cur_op
= (nh
->nh_label
->label
[0] ==
3472 MPLS_LABEL_IMPLICIT_NULL
);
3473 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
3474 if (cur_op
!= new_op
)
3479 /* Label values are good. */
3482 #endif /* HAVE_CUMULUS */
3485 * Add static LSP entry. This may be the first entry for this incoming label
3486 * or an additional nexthop; an existing entry may also have outgoing label
3488 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3491 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3492 mpls_label_t out_label
,
3493 enum nexthop_types_t gtype
, union g_addr
*gate
,
3496 struct hash
*slsp_table
;
3497 zebra_ile_t tmp_ile
;
3499 zebra_nhlfe_t
*nhlfe
;
3503 slsp_table
= zvrf
->slsp_table
;
3507 /* Find or create LSP. */
3508 tmp_ile
.in_label
= in_label
;
3509 lsp
= hash_get(slsp_table
, &tmp_ile
, lsp_alloc
);
3513 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3516 struct nexthop
*nh
= nhlfe
->nexthop
;
3519 assert(nh
->nh_label
);
3521 /* Compare existing nexthop */
3522 if (nh
->nh_label
->num_labels
== 1 &&
3523 nh
->nh_label
->label
[0] == out_label
)
3527 if (IS_ZEBRA_DEBUG_MPLS
) {
3528 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3530 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3531 in_label
, buf
, out_label
,
3532 nh
->nh_label
->label
[0]);
3534 if (nh
->nh_label
->num_labels
== 1)
3535 nh
->nh_label
->label
[0] = out_label
;
3537 nexthop_del_labels(nh
);
3538 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
, 1, &out_label
);
3542 /* Add static LSP entry to this nexthop */
3543 nhlfe
= nhlfe_add(lsp
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3544 ifindex
, 1, &out_label
, false /*backup*/);
3548 if (IS_ZEBRA_DEBUG_MPLS
) {
3549 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3551 "Add static LSP in-label %u nexthop %s out-label %u",
3552 in_label
, buf
, out_label
);
3556 /* (Re)Install LSP in the main table. */
3557 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, 1, &out_label
,
3558 gtype
, gate
, ifindex
))
3565 * Delete static LSP entry. This may be the delete of one particular
3566 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3568 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3569 * LSP configuration.
3571 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3572 enum nexthop_types_t gtype
, union g_addr
*gate
,
3575 struct hash
*slsp_table
;
3576 zebra_ile_t tmp_ile
;
3578 zebra_nhlfe_t
*nhlfe
;
3581 slsp_table
= zvrf
->slsp_table
;
3585 /* If entry is not present, exit. */
3586 tmp_ile
.in_label
= in_label
;
3587 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3591 /* Is it delete of entire LSP or a specific NHLFE? */
3592 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3593 if (IS_ZEBRA_DEBUG_MPLS
)
3594 zlog_debug("Del static LSP in-label %u", in_label
);
3596 /* Uninstall entire LSP from the main table. */
3597 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3599 /* Delete all static NHLFEs */
3600 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3604 /* Find specific NHLFE, exit if not found. */
3605 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3606 gtype
, gate
, ifindex
);
3610 if (IS_ZEBRA_DEBUG_MPLS
) {
3612 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3613 zlog_debug("Del static LSP in-label %u nexthop %s",
3617 /* Uninstall LSP from the main table. */
3618 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3619 gate
, ifindex
, false);
3621 /* Delete static LSP NHLFE */
3625 /* Remove entire static LSP entry if no NHLFE - valid in either case
3628 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) {
3629 lsp
= hash_release(slsp_table
, &tmp_ile
);
3630 XFREE(MTYPE_LSP
, lsp
);
3637 * Schedule all MPLS label forwarding entries for processing.
3638 * Called upon changes that may affect one or more of them such as
3639 * interface or nexthop state changes.
3641 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3645 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3649 * Display MPLS label forwarding table for a specific LSP
3650 * (VTY command handler).
3652 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3653 mpls_label_t label
, bool use_json
)
3655 struct hash
*lsp_table
;
3657 zebra_ile_t tmp_ile
;
3658 json_object
*json
= NULL
;
3661 lsp_table
= zvrf
->lsp_table
;
3665 /* If entry is not present, exit. */
3666 tmp_ile
.in_label
= label
;
3667 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3672 json
= lsp_json(lsp
);
3673 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3674 json
, JSON_C_TO_STRING_PRETTY
));
3675 json_object_free(json
);
3677 lsp_print(vty
, lsp
);
3681 * Display MPLS label forwarding table (VTY command handler).
3683 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3687 json_object
*json
= NULL
;
3688 zebra_lsp_t
*lsp
= NULL
;
3689 zebra_nhlfe_t
*nhlfe
= NULL
;
3690 struct listnode
*node
= NULL
;
3691 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3694 json
= json_object_new_object();
3696 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3697 json_object_object_add(
3698 json
, label2str(lsp
->ile
.in_label
, buf
,
3702 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3703 json
, JSON_C_TO_STRING_PRETTY
));
3704 json_object_free(json
);
3708 /* Prepare table. */
3709 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3710 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3711 tt
->style
.cell
.rpad
= 2;
3712 tt
->style
.corner
= '+';
3714 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3716 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3717 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3718 struct nexthop
*nexthop
;
3719 const char *out_label_str
;
3720 char nh_buf
[NEXTHOP_STRLEN
];
3722 nexthop
= nhlfe
->nexthop
;
3724 switch (nexthop
->type
) {
3725 case NEXTHOP_TYPE_IFINDEX
: {
3726 struct zebra_ns
*zns
;
3727 struct interface
*ifp
;
3729 zns
= zebra_ns_lookup(NS_DEFAULT
);
3730 ifp
= if_lookup_by_index_per_ns(
3731 zns
, nexthop
->ifindex
);
3732 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3733 ifp
? ifp
->name
: "Null");
3736 case NEXTHOP_TYPE_IPV4
:
3737 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3738 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3739 nh_buf
, sizeof(nh_buf
));
3741 case NEXTHOP_TYPE_IPV6
:
3742 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3743 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3744 nh_buf
, sizeof(nh_buf
));
3750 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3751 out_label_str
= mpls_label2str(
3752 nexthop
->nh_label
->num_labels
,
3753 &nexthop
->nh_label
->label
[0],
3754 buf
, sizeof(buf
), 1);
3756 out_label_str
= "-";
3758 ttable_add_row(tt
, "%u|%s|%s|%s",
3760 nhlfe_type2str(nhlfe
->type
),
3761 nh_buf
, out_label_str
);
3765 /* Dump the generated table. */
3766 if (tt
->nrows
> 1) {
3767 char *table
= ttable_dump(tt
, "\n");
3768 vty_out(vty
, "%s\n", table
);
3769 XFREE(MTYPE_TMP
, table
);
3774 list_delete(&lsp_list
);
3778 * Create printable string for static LSP configuration.
3780 static char *nhlfe_config_str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
3782 const struct nexthop
*nh
;
3784 nh
= nhlfe
->nexthop
;
3788 case NEXTHOP_TYPE_IPV4
:
3789 inet_ntop(AF_INET
, &nh
->gate
.ipv4
, buf
, size
);
3791 case NEXTHOP_TYPE_IPV6
:
3792 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3793 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, size
);
3796 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3807 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3809 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3812 zebra_nhlfe_t
*nhlfe
;
3814 struct listnode
*node
;
3815 struct list
*slsp_list
=
3816 hash_get_sorted_list(zvrf
->slsp_table
, lsp_cmp
);
3818 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, lsp
)) {
3819 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3823 nh
= nhlfe
->nexthop
;
3824 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3827 nhlfe_config_str(nhlfe
, buf
, sizeof(buf
));
3829 switch (nh
->nh_label
->label
[0]) {
3830 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3831 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3832 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3834 case MPLS_LABEL_IMPLICIT_NULL
:
3835 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3838 snprintf(lstr
, sizeof(lstr
), "%u",
3839 nh
->nh_label
->label
[0]);
3843 vty_out(vty
, "mpls lsp %u %s %s\n", lsp
->ile
.in_label
,
3848 list_delete(&slsp_list
);
3849 return (zvrf
->slsp_table
->count
? 1 : 0);
3853 * Add/update global label block.
3855 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3858 zvrf
->mpls_srgb
.start_label
= start_label
;
3859 zvrf
->mpls_srgb
.end_label
= end_label
;
3861 /* Evaluate registered FECs to see if any get a label or not. */
3867 * Delete global label block.
3869 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3871 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3872 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3874 /* Process registered FECs to clear their local label, if needed. */
3880 * Display MPLS global label block configuration (VTY command handler).
3882 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3884 if (zvrf
->mpls_srgb
.start_label
== 0)
3887 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3888 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3889 vty_out(vty
, "mpls label global-block %u %u\n",
3890 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3897 * Called when VRF becomes inactive, cleans up information but keeps
3900 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3902 struct zebra_vrf
*def_zvrf
;
3905 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3906 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3909 * For other vrfs, we try to remove associated LSPs; we locate
3910 * the LSPs in the default vrf.
3912 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3914 /* At shutdown, the default may be gone already */
3915 if (def_zvrf
== NULL
)
3918 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3919 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3920 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3926 * Called upon process exiting, need to delete LSP forwarding
3927 * entries from the kernel.
3928 * NOTE: Currently supported only for default VRF.
3930 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
3932 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3933 hash_clean(zvrf
->lsp_table
, NULL
);
3934 hash_free(zvrf
->lsp_table
);
3935 hash_clean(zvrf
->slsp_table
, NULL
);
3936 hash_free(zvrf
->slsp_table
);
3937 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
3938 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
3942 * Allocate MPLS tables for this VRF and do other initialization.
3943 * NOTE: Currently supported only for default VRF.
3945 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
3950 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
3951 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
3952 zvrf
->fec_table
[AFI_IP
] = route_table_init();
3953 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
3954 zvrf
->mpls_flags
= 0;
3955 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3956 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3960 * Global MPLS initialization.
3962 void zebra_mpls_init(void)
3966 if (mpls_kernel_init() < 0) {
3967 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
3968 "Disabling MPLS support (no kernel support)");
3972 if (!mpls_processq_init())
3975 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
3976 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);