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_vrf.h"
48 #include "zebra/zebra_mpls.h"
49 #include "zebra/zebra_srte.h"
50 #include "zebra/zebra_errors.h"
52 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object");
53 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object");
54 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object");
57 bool mpls_pw_reach_strict
; /* Strict reachability checking */
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
,
63 struct zebra_fec
*fec
);
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
, struct zebra_fec
*fec
,
68 mpls_label_t old_label
);
69 static int fec_send(struct zebra_fec
*fec
, struct zserv
*client
);
70 static void fec_update_clients(struct zebra_fec
*fec
);
71 static void fec_print(struct zebra_fec
*fec
, struct vty
*vty
);
72 static struct zebra_fec
*fec_find(struct route_table
*table
, struct prefix
*p
);
73 static struct zebra_fec
*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(struct zebra_fec
*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(struct zebra_nhlfe
*nhlfe
,
81 struct nexthop
*nexthop
);
82 static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active(struct zebra_nhlfe
*nhlfe
);
86 static void lsp_select_best_nhlfe(struct zebra_lsp
*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(struct zebra_lsp
*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
, struct zebra_lsp
**plsp
);
98 /* Free lsp; sets caller's pointer to NULL */
99 static void lsp_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
);
101 static char *nhlfe2str(const struct zebra_nhlfe
*nhlfe
, char *buf
, int size
);
102 static char *nhlfe_config_str(const struct zebra_nhlfe
*nhlfe
, char *buf
,
104 static int nhlfe_nhop_match(struct zebra_nhlfe
*nhlfe
,
105 enum nexthop_types_t gtype
,
106 const union g_addr
*gate
, ifindex_t ifindex
);
107 static struct zebra_nhlfe
*nhlfe_find(struct nhlfe_list_head
*list
,
108 enum lsp_types_t lsp_type
,
109 enum nexthop_types_t gtype
,
110 const union g_addr
*gate
,
112 static struct zebra_nhlfe
*
113 nhlfe_add(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
114 enum nexthop_types_t gtype
, const union g_addr
*gate
,
115 ifindex_t ifindex
, uint8_t num_labels
, const mpls_label_t
*labels
,
117 static int nhlfe_del(struct zebra_nhlfe
*nhlfe
);
118 static void nhlfe_free(struct zebra_nhlfe
*nhlfe
);
119 static void nhlfe_out_label_update(struct zebra_nhlfe
*nhlfe
,
120 struct mpls_label_stack
*nh_label
);
121 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, struct zebra_lsp
*lsp
,
122 enum lsp_types_t type
);
123 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
124 mpls_label_t in_label
);
125 static void nhlfe_print(struct zebra_nhlfe
*nhlfe
, struct vty
*vty
,
127 static void lsp_print(struct vty
*vty
, struct zebra_lsp
*lsp
);
128 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
129 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
130 int afi
, enum lsp_types_t lsp_type
);
131 static int lsp_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
132 const struct zapi_nexthop
*znh
);
133 static int lsp_backup_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
134 const struct zapi_nexthop
*znh
);
136 /* Static functions */
139 * Handle failure in LSP install, clear flags for NHLFE.
141 static void clear_nhlfe_installed(struct zebra_lsp
*lsp
)
143 struct zebra_nhlfe
*nhlfe
;
144 struct nexthop
*nexthop
;
146 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
147 nexthop
= nhlfe
->nexthop
;
151 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
152 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
155 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
156 nexthop
= nhlfe
->nexthop
;
160 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
161 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
166 * Install label forwarding entry based on labeled-route entry.
168 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
169 struct route_node
*rn
, struct route_entry
*re
)
171 struct hash
*lsp_table
;
172 struct zebra_ile tmp_ile
;
173 struct zebra_lsp
*lsp
;
174 struct zebra_nhlfe
*nhlfe
;
175 struct nexthop
*nexthop
;
176 enum lsp_types_t lsp_type
;
181 lsp_table
= zvrf
->lsp_table
;
185 lsp_type
= lsp_type_from_re_type(re
->type
);
188 /* Locate or allocate LSP entry. */
189 tmp_ile
.in_label
= label
;
190 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
192 /* For each active nexthop, create NHLFE. Note that we deliberately skip
193 * recursive nexthops right now, because intermediate hops won't
195 * the label advertised by the recursive nexthop (plus we don't have the
196 * logic yet to push multiple labels).
198 for (nexthop
= re
->nhe
->nhg
.nexthop
;
199 nexthop
; nexthop
= nexthop
->next
) {
200 /* Skip inactive and recursive entries. */
201 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
203 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
206 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, lsp_type
,
207 nexthop
->type
, &nexthop
->gate
,
210 /* Clear deleted flag (in case it was set) */
211 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
212 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
217 if (IS_ZEBRA_DEBUG_MPLS
) {
218 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
220 "LSP in-label %u type %d nexthop %s out-label changed",
221 lsp
->ile
.in_label
, lsp_type
, buf
);
224 /* Update out label, trigger processing. */
225 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
226 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
229 /* Add LSP entry to this nexthop */
230 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
231 &nexthop
->gate
, nexthop
->ifindex
,
232 nexthop
->nh_label
->num_labels
,
233 nexthop
->nh_label
->label
,
238 if (IS_ZEBRA_DEBUG_MPLS
) {
239 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
241 "Add LSP in-label %u type %d nexthop %s out-label %u",
242 lsp
->ile
.in_label
, lsp_type
, buf
,
243 nexthop
->nh_label
->label
[0]);
246 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
248 /* Mark NHLFE as changed. */
249 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
254 /* Queue LSP for processing if necessary. If no NHLFE got added (special
255 * case), delete the LSP entry; this case results in somewhat ugly
258 if (added
|| changed
) {
259 if (lsp_processq_add(lsp
))
262 lsp_check_free(lsp_table
, &lsp
);
269 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
270 * NHLFEs are removed, the entire entry is deleted.
272 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
274 struct hash
*lsp_table
;
275 struct zebra_ile tmp_ile
;
276 struct zebra_lsp
*lsp
;
277 struct zebra_nhlfe
*nhlfe
;
281 lsp_table
= zvrf
->lsp_table
;
285 /* If entry is not present, exit. */
286 tmp_ile
.in_label
= label
;
287 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
288 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
291 /* Mark NHLFEs for delete or directly delete, as appropriate. */
292 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
294 /* Skip static NHLFEs */
295 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
298 if (IS_ZEBRA_DEBUG_MPLS
) {
299 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
301 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
302 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
305 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
306 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
307 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
313 /* Queue LSP for processing, if needed, else delete. */
314 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
315 if (lsp_processq_add(lsp
))
318 lsp_check_free(lsp_table
, &lsp
);
325 * This function is invoked upon change to label block configuration; it
326 * will walk all registered FECs with label-index and appropriately update
327 * their local labels and trigger client updates.
329 static void fec_evaluate(struct zebra_vrf
*zvrf
)
331 struct route_node
*rn
;
332 struct zebra_fec
*fec
;
333 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 /* Save old label, determine new label. */
352 old_label
= fec
->label
;
354 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
355 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
356 new_label
= MPLS_INVALID_LABEL
;
358 /* If label has changed, update FEC and clients. */
359 if (new_label
== old_label
)
362 if (IS_ZEBRA_DEBUG_MPLS
)
364 "Update fec %pRN new label %u upon label block",
367 fec
->label
= new_label
;
368 fec_update_clients(fec
);
370 /* Update label forwarding entries appropriately */
371 fec_change_update_lsp(zvrf
, fec
, old_label
);
377 * Derive (if possible) and update the local label for the FEC based on
378 * its label index. The index is "acceptable" if it falls within the
379 * globally configured label block (SRGB).
381 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
382 struct zebra_fec
*fec
)
386 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
387 && zvrf
->mpls_srgb
.start_label
388 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
389 < zvrf
->mpls_srgb
.end_label
))
392 fec
->label
= MPLS_INVALID_LABEL
;
398 * There is a change for this FEC. Install or uninstall label forwarding
399 * entries, as appropriate.
401 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, struct zebra_fec
*fec
,
402 mpls_label_t old_label
)
404 struct route_table
*table
;
405 struct route_node
*rn
;
406 struct route_entry
*re
;
409 /* Uninstall label forwarding entry, if previously installed. */
410 if (old_label
!= MPLS_INVALID_LABEL
411 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
412 lsp_uninstall(zvrf
, old_label
);
414 /* Install label forwarding entry corr. to new label, if needed. */
415 if (fec
->label
== MPLS_INVALID_LABEL
416 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
419 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
420 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
424 /* See if labeled route exists. */
425 rn
= route_node_lookup(table
, &fec
->rn
->p
);
429 RNODE_FOREACH_RE (rn
, re
) {
430 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
434 if (!re
|| !zebra_rib_labeled_unicast(re
))
437 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
444 * Inform about FEC to a registered client.
446 static int fec_send(struct zebra_fec
*fec
, struct zserv
*client
)
449 struct route_node
*rn
;
453 /* Get output stream. */
454 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
456 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
458 stream_putw(s
, rn
->p
.family
);
459 stream_put_prefix(s
, &rn
->p
);
460 stream_putl(s
, fec
->label
);
461 stream_putw_at(s
, 0, stream_get_endp(s
));
462 return zserv_send_message(client
, s
);
466 * Update all registered clients about this FEC. Caller should've updated
467 * FEC and ensure no duplicate updates.
469 static void fec_update_clients(struct zebra_fec
*fec
)
471 struct listnode
*node
;
472 struct zserv
*client
;
474 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
475 if (IS_ZEBRA_DEBUG_MPLS
)
476 zlog_debug("Update client %s",
477 zebra_route_string(client
->proto
));
478 fec_send(fec
, client
);
484 * Print a FEC-label binding entry.
486 static void fec_print(struct zebra_fec
*fec
, struct vty
*vty
)
488 struct route_node
*rn
;
489 struct listnode
*node
;
490 struct zserv
*client
;
494 vty_out(vty
, "%pRN\n", rn
);
495 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
496 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
497 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
499 if (!list_isempty(fec
->client_list
)) {
500 vty_out(vty
, " Client list:");
501 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
502 vty_out(vty
, " %s(fd %d)",
503 zebra_route_string(client
->proto
),
510 * Locate FEC-label binding that matches with passed info.
512 static struct zebra_fec
*fec_find(struct route_table
*table
, struct prefix
*p
)
514 struct route_node
*rn
;
517 rn
= route_node_lookup(table
, p
);
521 route_unlock_node(rn
);
526 * Add a FEC. This may be upon a client registering for a binding
527 * or when a binding is configured.
529 static struct zebra_fec
*fec_add(struct route_table
*table
, struct prefix
*p
,
530 mpls_label_t label
, uint32_t flags
,
531 uint32_t label_index
)
533 struct route_node
*rn
;
534 struct zebra_fec
*fec
;
538 /* Lookup (or add) route node.*/
539 rn
= route_node_get(table
, p
);
546 fec
= XCALLOC(MTYPE_FEC
, sizeof(struct zebra_fec
));
551 fec
->client_list
= list_new();
553 route_unlock_node(rn
); /* for the route_node_get */
555 fec
->label_index
= label_index
;
562 * Delete a FEC. This may be upon the last client deregistering for
563 * a FEC and no binding exists or when the binding is deleted and there
564 * are no registered clients.
566 static int fec_del(struct zebra_fec
*fec
)
568 list_delete(&fec
->client_list
);
569 fec
->rn
->info
= NULL
;
570 route_unlock_node(fec
->rn
);
571 XFREE(MTYPE_FEC
, fec
);
576 * Hash function for label.
578 static unsigned int label_hash(const void *p
)
580 const struct zebra_ile
*ile
= p
;
582 return (jhash_1word(ile
->in_label
, 0));
586 * Compare 2 LSP hash entries based on in-label.
588 static bool label_cmp(const void *p1
, const void *p2
)
590 const struct zebra_ile
*ile1
= p1
;
591 const struct zebra_ile
*ile2
= p2
;
593 return (ile1
->in_label
== ile2
->in_label
);
597 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
599 * NOTE: Looking only for connected routes right now.
601 static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe
*nhlfe
,
602 struct nexthop
*nexthop
)
604 struct route_table
*table
;
605 struct prefix_ipv4 p
;
606 struct route_node
*rn
;
607 struct route_entry
*match
;
608 struct nexthop
*match_nh
;
610 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
614 /* Lookup nexthop in IPv4 routing table. */
615 memset(&p
, 0, sizeof(p
));
617 p
.prefixlen
= IPV4_MAX_BITLEN
;
618 p
.prefix
= nexthop
->gate
.ipv4
;
620 rn
= route_node_match(table
, (struct prefix
*)&p
);
624 route_unlock_node(rn
);
626 /* Locate a valid connected route. */
627 RNODE_FOREACH_RE (rn
, match
) {
628 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
629 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
632 for (match_nh
= match
->nhe
->nhg
.nexthop
; match_nh
;
633 match_nh
= match_nh
->next
) {
634 if (match
->type
== ZEBRA_ROUTE_CONNECT
635 || nexthop
->ifindex
== match_nh
->ifindex
) {
636 nexthop
->ifindex
= match_nh
->ifindex
;
647 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
649 * NOTE: Looking only for connected routes right now.
651 static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe
*nhlfe
,
652 struct nexthop
*nexthop
)
654 struct route_table
*table
;
655 struct prefix_ipv6 p
;
656 struct route_node
*rn
;
657 struct route_entry
*match
;
659 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
663 /* Lookup nexthop in IPv6 routing table. */
664 memset(&p
, 0, sizeof(p
));
666 p
.prefixlen
= IPV6_MAX_BITLEN
;
667 p
.prefix
= nexthop
->gate
.ipv6
;
669 rn
= route_node_match(table
, (struct prefix
*)&p
);
673 route_unlock_node(rn
);
675 /* Locate a valid connected route. */
676 RNODE_FOREACH_RE (rn
, match
) {
677 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
678 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
679 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
683 if (!match
|| !match
->nhe
->nhg
.nexthop
)
686 nexthop
->ifindex
= match
->nhe
->nhg
.nexthop
->ifindex
;
692 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
694 * NOTE: Each NHLFE points to only 1 nexthop.
696 static int nhlfe_nexthop_active(struct zebra_nhlfe
*nhlfe
)
698 struct nexthop
*nexthop
;
699 struct interface
*ifp
;
700 struct zebra_ns
*zns
;
702 nexthop
= nhlfe
->nexthop
;
703 if (!nexthop
) // unexpected
706 /* Check on nexthop based on type. */
707 switch (nexthop
->type
) {
708 case NEXTHOP_TYPE_IFINDEX
:
710 * Lookup if this type is special. The
711 * NEXTHOP_TYPE_IFINDEX is a pop and
712 * forward into a different table for
713 * processing. As such this ifindex
714 * passed to us may be a VRF device
715 * which will not be in the default
716 * VRF. So let's look in all of them
718 zns
= zebra_ns_lookup(NS_DEFAULT
);
719 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
720 if (ifp
&& if_is_operative(ifp
))
721 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
723 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
725 case NEXTHOP_TYPE_IPV4
:
726 case NEXTHOP_TYPE_IPV4_IFINDEX
:
727 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
728 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
730 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 case NEXTHOP_TYPE_IPV6
:
734 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
735 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
740 case NEXTHOP_TYPE_IPV6_IFINDEX
:
741 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
742 ifp
= if_lookup_by_index(nexthop
->ifindex
,
744 if (ifp
&& if_is_operative(ifp
))
745 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
747 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
749 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
750 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
752 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
756 case NEXTHOP_TYPE_BLACKHOLE
:
760 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
764 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
765 * reachability and select the best. Multipath entries are also
766 * marked. This is invoked when an LSP scheduled for processing (due
767 * to some change) is examined.
769 static void lsp_select_best_nhlfe(struct zebra_lsp
*lsp
)
771 struct zebra_nhlfe
*nhlfe
;
772 struct zebra_nhlfe
*best
;
773 struct nexthop
*nexthop
;
781 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
784 * First compute the best path, after checking nexthop status. We are
785 * only concerned with non-deleted NHLFEs.
787 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
788 /* Clear selection flags. */
789 UNSET_FLAG(nhlfe
->flags
,
790 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
792 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
793 && nhlfe_nexthop_active(nhlfe
)) {
794 if (!best
|| (nhlfe
->distance
< best
->distance
))
799 lsp
->best_nhlfe
= best
;
800 if (!lsp
->best_nhlfe
)
804 * Check the active status of backup nhlfes also
806 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
807 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
808 (void)nhlfe_nexthop_active(nhlfe
);
811 /* Mark best NHLFE as selected. */
812 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
815 * If best path exists, see if there is ECMP. While doing this, note if
817 * new (uninstalled) NHLFE has been selected, an installed entry that is
818 * still selected has a change or an installed entry is to be removed.
820 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
821 int nh_chg
, nh_sel
, nh_inst
;
823 nexthop
= nhlfe
->nexthop
;
824 if (!nexthop
) // unexpected
827 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
828 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
829 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
830 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
831 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
835 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
836 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
837 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
839 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
841 if ((nh_sel
&& !nh_inst
)
842 || (nh_sel
&& nh_inst
&& nh_chg
)
843 || (nh_inst
&& !nh_sel
))
847 /* We have finished examining, clear changed flag. */
848 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
852 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
856 * Delete LSP forwarding entry from kernel, if installed. Called upon
859 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
861 struct zebra_lsp
*lsp
;
863 lsp
= (struct zebra_lsp
*)bucket
->data
;
864 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
865 (void)dplane_lsp_delete(lsp
);
869 * Schedule LSP forwarding entry for processing. Called upon changes
870 * that may impact LSPs such as nexthop / connected route changes.
872 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
874 struct zebra_lsp
*lsp
;
876 lsp
= (struct zebra_lsp
*)bucket
->data
;
878 /* In the common flow, this is used when external events occur. For
879 * LSPs with backup nhlfes, we'll assume that the forwarding
880 * plane will use the backups to handle these events, until the
881 * owning protocol can react.
884 /* Skip LSPs with backups */
885 if (nhlfe_list_first(&lsp
->backup_nhlfe_list
) != NULL
) {
886 if (IS_ZEBRA_DEBUG_MPLS_DETAIL
)
887 zlog_debug("%s: skip LSP in-label %u",
888 __func__
, lsp
->ile
.in_label
);
893 (void)lsp_processq_add(lsp
);
897 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
898 * any multipaths and update or delete from the kernel, as needed.
900 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
902 struct zebra_lsp
*lsp
;
903 struct zebra_nhlfe
*oldbest
, *newbest
;
904 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
905 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
906 enum zebra_dplane_result res
;
908 lsp
= (struct zebra_lsp
*)data
;
909 if (!lsp
) // unexpected
912 oldbest
= lsp
->best_nhlfe
;
914 /* Select best NHLFE(s) */
915 lsp_select_best_nhlfe(lsp
);
917 newbest
= lsp
->best_nhlfe
;
919 if (IS_ZEBRA_DEBUG_MPLS
) {
921 nhlfe2str(oldbest
, buf
, sizeof(buf
));
923 nhlfe2str(newbest
, buf2
, sizeof(buf2
));
925 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
926 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
927 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
930 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
931 /* Not already installed */
934 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
936 switch (dplane_lsp_add(lsp
)) {
937 case ZEBRA_DPLANE_REQUEST_QUEUED
:
938 /* Set 'installed' flag so we will know
939 * that an install is in-flight.
941 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
943 zvrf
->lsp_installs_queued
++;
945 case ZEBRA_DPLANE_REQUEST_FAILURE
:
946 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
947 "LSP Install Failure: %u",
950 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
951 zvrf
->lsp_installs
++;
956 /* Installed, may need an update and/or delete. */
958 res
= dplane_lsp_delete(lsp
);
960 /* We do some of the lsp cleanup immediately for
963 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
964 clear_nhlfe_installed(lsp
);
967 case ZEBRA_DPLANE_REQUEST_QUEUED
:
968 zvrf
->lsp_removals_queued
++;
970 case ZEBRA_DPLANE_REQUEST_FAILURE
:
971 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
972 "LSP Deletion Failure: %u",
975 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
976 zvrf
->lsp_removals
++;
979 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
980 struct zebra_nhlfe
*nhlfe
;
981 struct nexthop
*nexthop
;
983 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
985 /* We leave the INSTALLED flag set here
986 * so we know an update is in-flight.
990 * Any NHLFE that was installed but is not
991 * selected now needs to have its flags updated.
993 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
994 nexthop
= nhlfe
->nexthop
;
998 if (CHECK_FLAG(nhlfe
->flags
,
999 NHLFE_FLAG_INSTALLED
)
1000 && !CHECK_FLAG(nhlfe
->flags
,
1001 NHLFE_FLAG_SELECTED
)) {
1002 UNSET_FLAG(nhlfe
->flags
,
1003 NHLFE_FLAG_INSTALLED
);
1004 UNSET_FLAG(nexthop
->flags
,
1009 switch (dplane_lsp_update(lsp
)) {
1010 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1011 zvrf
->lsp_installs_queued
++;
1013 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1014 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1015 "LSP Update Failure: %u",
1018 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1019 zvrf
->lsp_installs
++;
1030 * Callback upon processing completion of a LSP forwarding entry.
1032 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1034 struct zebra_vrf
*zvrf
;
1035 struct zebra_lsp
*lsp
;
1036 struct hash
*lsp_table
;
1037 struct zebra_nhlfe
*nhlfe
;
1039 /* If zebra is shutting down, don't delete any structs,
1040 * just ignore this callback. The LSPs will be cleaned up
1041 * during the shutdown processing.
1043 if (zebra_router_in_shutdown())
1046 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1049 lsp_table
= zvrf
->lsp_table
;
1050 if (!lsp_table
) // unexpected
1053 lsp
= (struct zebra_lsp
*)data
;
1054 if (!lsp
) // unexpected
1057 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1059 * delete LSP entry also.
1061 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1063 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1064 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1068 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1069 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1073 lsp_check_free(lsp_table
, &lsp
);
1077 * Callback upon finishing the processing of all scheduled
1078 * LSP forwarding entries.
1080 static void lsp_processq_complete(struct work_queue
*wq
)
1082 /* Nothing to do for now. */
1086 * Add LSP forwarding entry to queue for subsequent processing.
1088 static int lsp_processq_add(struct zebra_lsp
*lsp
)
1090 /* If already scheduled, exit. */
1091 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1094 if (zrouter
.lsp_process_q
== NULL
) {
1095 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1096 "%s: work_queue does not exist!", __func__
);
1100 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1101 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1106 * Callback to allocate LSP forwarding table entry.
1108 static void *lsp_alloc(void *p
)
1110 const struct zebra_ile
*ile
= p
;
1111 struct zebra_lsp
*lsp
;
1113 lsp
= XCALLOC(MTYPE_LSP
, sizeof(struct zebra_lsp
));
1115 nhlfe_list_init(&lsp
->nhlfe_list
);
1116 nhlfe_list_init(&lsp
->backup_nhlfe_list
);
1118 if (IS_ZEBRA_DEBUG_MPLS
)
1119 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1121 return ((void *)lsp
);
1125 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1127 static void lsp_check_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
)
1129 struct zebra_lsp
*lsp
;
1131 if (plsp
== NULL
|| *plsp
== NULL
)
1136 if ((nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) &&
1137 (nhlfe_list_first(&lsp
->backup_nhlfe_list
) == NULL
) &&
1138 !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1139 lsp_free(lsp_table
, plsp
);
1142 static void lsp_free_nhlfe(struct zebra_lsp
*lsp
)
1144 struct zebra_nhlfe
*nhlfe
;
1146 while ((nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
))) {
1147 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1151 while ((nhlfe
= nhlfe_list_first(&lsp
->backup_nhlfe_list
))) {
1152 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1158 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1161 static void lsp_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
)
1163 struct zebra_lsp
*lsp
;
1165 if (plsp
== NULL
|| *plsp
== NULL
)
1170 if (IS_ZEBRA_DEBUG_MPLS
)
1171 zlog_debug("Free LSP in-label %u flags 0x%x",
1172 lsp
->ile
.in_label
, lsp
->flags
);
1174 lsp_free_nhlfe(lsp
);
1176 hash_release(lsp_table
, &lsp
->ile
);
1177 XFREE(MTYPE_LSP
, lsp
);
1183 * Create printable string for NHLFE entry.
1185 static char *nhlfe2str(const struct zebra_nhlfe
*nhlfe
, char *buf
, int size
)
1187 const struct nexthop
*nexthop
;
1190 nexthop
= nhlfe
->nexthop
;
1191 switch (nexthop
->type
) {
1192 case NEXTHOP_TYPE_IPV4
:
1193 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1194 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1196 case NEXTHOP_TYPE_IPV6
:
1197 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1198 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1200 case NEXTHOP_TYPE_IFINDEX
:
1201 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1202 case NEXTHOP_TYPE_BLACKHOLE
:
1210 * Check if NHLFE matches with search info passed.
1212 static int nhlfe_nhop_match(struct zebra_nhlfe
*nhlfe
,
1213 enum nexthop_types_t gtype
,
1214 const union g_addr
*gate
, ifindex_t ifindex
)
1216 struct nexthop
*nhop
;
1219 nhop
= nhlfe
->nexthop
;
1223 if (nhop
->type
!= gtype
)
1226 switch (nhop
->type
) {
1227 case NEXTHOP_TYPE_IPV4
:
1228 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1229 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1230 sizeof(struct in_addr
));
1231 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1232 cmp
= !(nhop
->ifindex
== ifindex
);
1234 case NEXTHOP_TYPE_IPV6
:
1235 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1236 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1237 sizeof(struct in6_addr
));
1238 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1239 cmp
= !(nhop
->ifindex
== ifindex
);
1241 case NEXTHOP_TYPE_IFINDEX
:
1242 cmp
= !(nhop
->ifindex
== ifindex
);
1244 case NEXTHOP_TYPE_BLACKHOLE
:
1253 * Locate NHLFE that matches with passed info.
1255 static struct zebra_nhlfe
*nhlfe_find(struct nhlfe_list_head
*list
,
1256 enum lsp_types_t lsp_type
,
1257 enum nexthop_types_t gtype
,
1258 const union g_addr
*gate
,
1261 struct zebra_nhlfe
*nhlfe
;
1263 frr_each_safe(nhlfe_list
, list
, nhlfe
) {
1264 if (nhlfe
->type
!= lsp_type
)
1266 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1274 * Allocate and init new NHLFE.
1276 static struct zebra_nhlfe
*
1277 nhlfe_alloc(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
1278 enum nexthop_types_t gtype
, const union g_addr
*gate
,
1279 ifindex_t ifindex
, uint8_t num_labels
, const mpls_label_t
*labels
)
1281 struct zebra_nhlfe
*nhlfe
;
1282 struct nexthop
*nexthop
;
1286 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(struct zebra_nhlfe
));
1289 nhlfe
->type
= lsp_type
;
1290 nhlfe
->distance
= lsp_distance(lsp_type
);
1292 nexthop
= nexthop_new();
1294 nexthop_add_labels(nexthop
, lsp_type
, num_labels
, labels
);
1296 nexthop
->vrf_id
= VRF_DEFAULT
;
1297 nexthop
->type
= gtype
;
1298 switch (nexthop
->type
) {
1299 case NEXTHOP_TYPE_IPV4
:
1300 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1301 nexthop
->gate
.ipv4
= gate
->ipv4
;
1303 nexthop
->ifindex
= ifindex
;
1305 case NEXTHOP_TYPE_IPV6
:
1306 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1307 nexthop
->gate
.ipv6
= gate
->ipv6
;
1309 nexthop
->ifindex
= ifindex
;
1311 case NEXTHOP_TYPE_IFINDEX
:
1312 nexthop
->ifindex
= ifindex
;
1314 case NEXTHOP_TYPE_BLACKHOLE
:
1315 if (IS_ZEBRA_DEBUG_MPLS
)
1316 zlog_debug("%s: invalid: blackhole nexthop", __func__
);
1318 nexthop_free(nexthop
);
1319 XFREE(MTYPE_NHLFE
, nhlfe
);
1322 nhlfe
->nexthop
= nexthop
;
1328 * Add primary or backup NHLFE. Base entry must have been created and
1329 * duplicate check done.
1331 static struct zebra_nhlfe
*nhlfe_add(struct zebra_lsp
*lsp
,
1332 enum lsp_types_t lsp_type
,
1333 enum nexthop_types_t gtype
,
1334 const union g_addr
*gate
,
1335 ifindex_t ifindex
, uint8_t num_labels
,
1336 const mpls_label_t
*labels
, bool is_backup
)
1338 struct zebra_nhlfe
*nhlfe
;
1343 /* Must have labels */
1344 if (num_labels
== 0 || labels
== NULL
) {
1345 if (IS_ZEBRA_DEBUG_MPLS
)
1346 zlog_debug("%s: invalid nexthop: no labels", __func__
);
1351 /* Allocate new object */
1352 nhlfe
= nhlfe_alloc(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
1358 /* Enqueue to LSP: primaries at head of list, backups at tail */
1360 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
);
1361 nhlfe_list_add_tail(&lsp
->backup_nhlfe_list
, nhlfe
);
1363 nhlfe_list_add_head(&lsp
->nhlfe_list
, nhlfe
);
1369 * Common delete for NHLFEs.
1371 static void nhlfe_free(struct zebra_nhlfe
*nhlfe
)
1378 nexthop_free(nhlfe
->nexthop
);
1380 nhlfe
->nexthop
= NULL
;
1382 XFREE(MTYPE_NHLFE
, nhlfe
);
1387 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1389 static int nhlfe_del(struct zebra_nhlfe
*nhlfe
)
1391 struct zebra_lsp
*lsp
;
1400 if (nhlfe
== lsp
->best_nhlfe
)
1401 lsp
->best_nhlfe
= NULL
;
1403 /* Unlink from LSP */
1404 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
))
1405 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1407 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1417 * Update label for NHLFE entry.
1419 static void nhlfe_out_label_update(struct zebra_nhlfe
*nhlfe
,
1420 struct mpls_label_stack
*nh_label
)
1422 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1425 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, struct zebra_lsp
*lsp
,
1426 enum lsp_types_t type
)
1428 struct zebra_nhlfe
*nhlfe
;
1429 int schedule_lsp
= 0;
1432 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1435 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1436 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1437 /* Skip non-static NHLFEs */
1438 if (nhlfe
->type
!= type
)
1441 if (IS_ZEBRA_DEBUG_MPLS
) {
1442 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1444 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1445 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1448 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1449 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1450 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1457 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1458 /* Skip non-static NHLFEs */
1459 if (nhlfe
->type
!= type
)
1462 if (IS_ZEBRA_DEBUG_MPLS
) {
1463 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1465 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1466 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1469 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1470 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1471 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1478 /* Queue LSP for processing, if needed, else delete. */
1480 if (IS_ZEBRA_DEBUG_MPLS
) {
1481 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1482 lsp
->ile
.in_label
, lsp
->flags
);
1484 if (lsp_processq_add(lsp
))
1487 lsp_check_free(lsp_table
, &lsp
);
1494 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1495 * If no other NHLFEs exist, the entry would be deleted.
1497 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1498 mpls_label_t in_label
)
1500 struct hash
*lsp_table
;
1501 struct zebra_ile tmp_ile
;
1502 struct zebra_lsp
*lsp
;
1505 lsp_table
= zvrf
->lsp_table
;
1509 /* If entry is not present, exit. */
1510 tmp_ile
.in_label
= in_label
;
1511 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1512 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
1515 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1518 static json_object
*nhlfe_json(struct zebra_nhlfe
*nhlfe
)
1520 json_object
*json_nhlfe
= NULL
;
1521 json_object
*json_backups
= NULL
;
1522 json_object
*json_label_stack
;
1523 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1526 json_nhlfe
= json_object_new_object();
1527 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1528 json_object_int_add(json_nhlfe
, "outLabel",
1529 nexthop
->nh_label
->label
[0]);
1531 json_label_stack
= json_object_new_array();
1532 json_object_object_add(json_nhlfe
, "outLabelStack", json_label_stack
);
1533 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++)
1534 json_object_array_add(
1536 json_object_new_int(nexthop
->nh_label
->label
[i
]));
1538 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1540 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1541 json_object_boolean_true_add(json_nhlfe
, "installed");
1543 switch (nexthop
->type
) {
1544 case NEXTHOP_TYPE_IPV4
:
1545 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1546 json_object_string_addf(json_nhlfe
, "nexthop", "%pI4",
1547 &nexthop
->gate
.ipv4
);
1549 case NEXTHOP_TYPE_IPV6
:
1550 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1551 json_object_string_addf(json_nhlfe
, "nexthop", "%pI6",
1552 &nexthop
->gate
.ipv6
);
1554 if (nexthop
->ifindex
)
1555 json_object_string_add(json_nhlfe
, "interface",
1556 ifindex2ifname(nexthop
->ifindex
,
1559 case NEXTHOP_TYPE_IFINDEX
:
1560 if (nexthop
->ifindex
)
1561 json_object_string_add(json_nhlfe
, "interface",
1562 ifindex2ifname(nexthop
->ifindex
,
1565 case NEXTHOP_TYPE_BLACKHOLE
:
1569 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1570 json_backups
= json_object_new_array();
1571 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1572 json_object_array_add(
1574 json_object_new_int(nexthop
->backup_idx
[i
]));
1577 json_object_object_add(json_nhlfe
, "backupIndex",
1585 * Print the NHLFE for a LSP forwarding entry.
1587 static void nhlfe_print(struct zebra_nhlfe
*nhlfe
, struct vty
*vty
,
1590 struct nexthop
*nexthop
;
1591 char buf
[MPLS_LABEL_STRLEN
];
1593 nexthop
= nhlfe
->nexthop
;
1594 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1597 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1598 nhlfe_type2str(nhlfe
->type
),
1599 mpls_label2str(nexthop
->nh_label
->num_labels
,
1600 nexthop
->nh_label
->label
,
1601 buf
, sizeof(buf
), 0),
1605 vty_out(vty
, "%s", indent
);
1607 switch (nexthop
->type
) {
1608 case NEXTHOP_TYPE_IPV4
:
1609 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1610 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1611 if (nexthop
->ifindex
)
1612 vty_out(vty
, " dev %s",
1613 ifindex2ifname(nexthop
->ifindex
,
1616 case NEXTHOP_TYPE_IPV6
:
1617 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1618 vty_out(vty
, " via %s",
1619 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
1621 if (nexthop
->ifindex
)
1622 vty_out(vty
, " dev %s",
1623 ifindex2ifname(nexthop
->ifindex
,
1626 case NEXTHOP_TYPE_IFINDEX
:
1627 if (nexthop
->ifindex
)
1628 vty_out(vty
, " dev %s",
1629 ifindex2ifname(nexthop
->ifindex
,
1632 case NEXTHOP_TYPE_BLACKHOLE
:
1636 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
) ? " (backup)"
1639 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1645 * Print an LSP forwarding entry.
1647 static void lsp_print(struct vty
*vty
, struct zebra_lsp
*lsp
)
1649 struct zebra_nhlfe
*nhlfe
, *backup
;
1652 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1653 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1656 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1657 nhlfe_print(nhlfe
, vty
, NULL
);
1659 if (nhlfe
->nexthop
== NULL
||
1660 !CHECK_FLAG(nhlfe
->nexthop
->flags
,
1661 NEXTHOP_FLAG_HAS_BACKUP
))
1664 /* Backup nhlfes: find backups in backup list */
1666 for (j
= 0; j
< nhlfe
->nexthop
->backup_num
; j
++) {
1669 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, backup
) {
1670 if (i
== nhlfe
->nexthop
->backup_idx
[j
])
1676 vty_out(vty
, " [backup %d]", i
);
1677 nhlfe_print(backup
, vty
, " ");
1684 * JSON objects for an LSP forwarding entry.
1686 static json_object
*lsp_json(struct zebra_lsp
*lsp
)
1688 struct zebra_nhlfe
*nhlfe
= NULL
;
1689 json_object
*json
= json_object_new_object();
1690 json_object
*json_nhlfe_list
= json_object_new_array();
1692 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1694 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1695 json_object_boolean_true_add(json
, "installed");
1697 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1698 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1700 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1701 json_nhlfe_list
= NULL
;
1704 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1705 if (json_nhlfe_list
== NULL
)
1706 json_nhlfe_list
= json_object_new_array();
1708 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1711 if (json_nhlfe_list
)
1712 json_object_object_add(json
, "backupNexthops", json_nhlfe_list
);
1718 /* Return a sorted linked list of the hash contents */
1719 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1722 struct hash_bucket
*hb
;
1723 struct list
*sorted_list
= list_new();
1725 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1727 for (i
= 0; i
< hash
->size
; i
++)
1728 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1729 listnode_add_sort(sorted_list
, hb
->data
);
1735 * Compare two LSPs based on their label values.
1737 static int lsp_cmp(const struct zebra_lsp
*lsp1
, const struct zebra_lsp
*lsp2
)
1739 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1742 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1749 * Initialize work queue for processing changed LSPs.
1751 static void mpls_processq_init(void)
1753 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1755 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1756 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1757 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1758 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1759 zrouter
.lsp_process_q
->spec
.hold
= 10;
1764 * Process LSP update results from zebra dataplane.
1766 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1768 struct zebra_vrf
*zvrf
;
1770 struct zebra_ile tmp_ile
;
1771 struct hash
*lsp_table
;
1772 struct zebra_lsp
*lsp
;
1773 struct zebra_nhlfe
*nhlfe
;
1774 struct nexthop
*nexthop
;
1775 enum dplane_op_e op
;
1776 enum zebra_dplane_result status
;
1777 enum zebra_sr_policy_update_label_mode update_mode
;
1779 op
= dplane_ctx_get_op(ctx
);
1780 status
= dplane_ctx_get_status(ctx
);
1782 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1783 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1784 ctx
, dplane_op2str(op
),
1785 dplane_ctx_get_in_label(ctx
),
1786 dplane_res2str(status
));
1788 label
= dplane_ctx_get_in_label(ctx
);
1791 case DPLANE_OP_LSP_INSTALL
:
1792 case DPLANE_OP_LSP_UPDATE
:
1793 /* Look for zebra LSP object */
1794 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1798 lsp_table
= zvrf
->lsp_table
;
1800 tmp_ile
.in_label
= label
;
1801 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1803 if (IS_ZEBRA_DEBUG_DPLANE
)
1804 zlog_debug("LSP ctx %p: in-label %u not found",
1805 ctx
, dplane_ctx_get_in_label(ctx
));
1809 /* TODO -- Confirm that this result is still 'current' */
1811 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1812 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1813 clear_nhlfe_installed(lsp
);
1814 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1815 "LSP Install Failure: in-label %u",
1820 /* Update zebra object */
1821 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1822 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1823 nexthop
= nhlfe
->nexthop
;
1827 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
) &&
1828 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1829 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1830 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1834 update_mode
= (op
== DPLANE_OP_LSP_INSTALL
)
1835 ? ZEBRA_SR_POLICY_LABEL_CREATED
1836 : ZEBRA_SR_POLICY_LABEL_UPDATED
;
1837 zebra_sr_policy_label_update(label
, update_mode
);
1840 case DPLANE_OP_LSP_DELETE
:
1841 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1842 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1843 "LSP Deletion Failure: in-label %u",
1844 dplane_ctx_get_in_label(ctx
));
1847 zebra_sr_policy_label_update(label
,
1848 ZEBRA_SR_POLICY_LABEL_REMOVED
);
1858 * Process LSP installation info from two sets of nhlfes: a set from
1859 * a dplane notification, and a set from the zebra LSP object. Update
1860 * counters of installed nexthops, and return whether the LSP has changed.
1862 static bool compare_notif_nhlfes(const struct nhlfe_list_head
*ctx_head
,
1863 struct nhlfe_list_head
*nhlfe_head
,
1864 int *start_counter
, int *end_counter
)
1866 struct zebra_nhlfe
*nhlfe
;
1867 const struct zebra_nhlfe
*ctx_nhlfe
;
1868 struct nexthop
*nexthop
;
1869 const struct nexthop
*ctx_nexthop
;
1870 int start_count
= 0, end_count
= 0;
1871 bool changed_p
= false;
1872 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1874 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1875 char buf
[NEXTHOP_STRLEN
];
1877 nexthop
= nhlfe
->nexthop
;
1881 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1886 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1887 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1891 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1892 nexthop_same(ctx_nexthop
, nexthop
)) {
1899 nexthop2str(nexthop
, buf
, sizeof(buf
));
1901 if (ctx_nhlfe
&& ctx_nexthop
) {
1903 const char *tstr
= "";
1905 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1906 NHLFE_FLAG_INSTALLED
))
1909 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1913 /* Test zebra nhlfe install state */
1914 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1915 NHLFE_FLAG_INSTALLED
)) {
1917 if (!CHECK_FLAG(nhlfe
->flags
,
1918 NHLFE_FLAG_INSTALLED
))
1921 /* Update counter */
1925 if (CHECK_FLAG(nhlfe
->flags
,
1926 NHLFE_FLAG_INSTALLED
))
1931 /* Not mentioned in lfib set -> uninstalled */
1932 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1933 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1934 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1939 zlog_debug("LSP dplane notif: no match, nh %s",
1945 *start_counter
+= start_count
;
1947 *end_counter
+= end_count
;
1953 * Update an lsp nhlfe list from a dplane context, typically an async
1954 * notification context. Update the LSP list to match the installed
1955 * status from the context's list.
1957 static int update_nhlfes_from_ctx(struct nhlfe_list_head
*nhlfe_head
,
1958 const struct nhlfe_list_head
*ctx_head
)
1961 struct zebra_nhlfe
*nhlfe
;
1962 const struct zebra_nhlfe
*ctx_nhlfe
;
1963 struct nexthop
*nexthop
;
1964 const struct nexthop
*ctx_nexthop
;
1965 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1967 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1968 char buf
[NEXTHOP_STRLEN
];
1970 nexthop
= nhlfe
->nexthop
;
1976 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1977 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1981 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1982 nexthop_same(ctx_nexthop
, nexthop
)) {
1989 nexthop2str(nexthop
, buf
, sizeof(buf
));
1991 if (ctx_nhlfe
&& ctx_nexthop
) {
1993 /* Bring zebra nhlfe install state into sync */
1994 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1995 NHLFE_FLAG_INSTALLED
)) {
1997 zlog_debug("%s: matched lsp nhlfe %s (installed)",
2000 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2001 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2005 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
2008 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2009 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2012 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
2013 NEXTHOP_FLAG_FIB
)) {
2014 SET_FLAG(nhlfe
->nexthop
->flags
,
2015 NEXTHOP_FLAG_ACTIVE
);
2016 SET_FLAG(nhlfe
->nexthop
->flags
,
2019 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2020 NEXTHOP_FLAG_ACTIVE
);
2021 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2026 /* Not mentioned in lfib set -> uninstalled */
2028 zlog_debug("%s: no match for lsp nhlfe %s",
2030 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2031 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2032 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2033 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2041 * Process async dplane notifications.
2043 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2045 struct zebra_vrf
*zvrf
;
2046 struct zebra_ile tmp_ile
;
2047 struct hash
*lsp_table
;
2048 struct zebra_lsp
*lsp
;
2049 const struct nhlfe_list_head
*ctx_list
;
2050 int start_count
= 0, end_count
= 0; /* Installed counts */
2051 bool changed_p
= false;
2052 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
2053 enum zebra_sr_policy_update_label_mode update_mode
;
2056 zlog_debug("LSP dplane notif, in-label %u",
2057 dplane_ctx_get_in_label(ctx
));
2059 /* Look for zebra LSP object */
2060 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2064 lsp_table
= zvrf
->lsp_table
;
2066 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
2067 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2070 zlog_debug("dplane LSP notif: in-label %u not found",
2071 dplane_ctx_get_in_label(ctx
));
2076 * The dataplane/forwarding plane is notifying zebra about the state
2077 * of the nexthops associated with this LSP. First, we take a
2078 * pre-scan pass to determine whether the LSP has transitioned
2079 * from installed -> uninstalled. In that case, we need to have
2080 * the existing state of the LSP objects available before making
2083 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2085 changed_p
= compare_notif_nhlfes(ctx_list
, &lsp
->nhlfe_list
,
2086 &start_count
, &end_count
);
2089 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2090 start_count
, end_count
,
2091 changed_p
? ", changed" : "");
2093 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2095 if (compare_notif_nhlfes(ctx_list
, &lsp
->backup_nhlfe_list
,
2096 &start_count
, &end_count
))
2097 /* Avoid accidentally setting back to 'false' */
2101 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2102 start_count
, end_count
,
2103 changed_p
? ", changed" : "");
2106 * Has the LSP become uninstalled? We need the existing state of the
2107 * nexthops/nhlfes at this point so we know what to delete.
2109 if (start_count
> 0 && end_count
== 0) {
2110 /* Inform other lfibs */
2111 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
2115 * Now we take a second pass and bring the zebra
2116 * nexthop state into sync with the forwarding-plane state.
2118 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2119 update_nhlfes_from_ctx(&lsp
->nhlfe_list
, ctx_list
);
2121 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2122 update_nhlfes_from_ctx(&lsp
->backup_nhlfe_list
, ctx_list
);
2124 if (end_count
> 0) {
2125 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2127 /* SR-TE update too */
2128 if (start_count
== 0)
2129 update_mode
= ZEBRA_SR_POLICY_LABEL_CREATED
;
2131 update_mode
= ZEBRA_SR_POLICY_LABEL_UPDATED
;
2132 zebra_sr_policy_label_update(lsp
->ile
.in_label
, update_mode
);
2135 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2138 /* SR-TE update too */
2139 zebra_sr_policy_label_update(lsp
->ile
.in_label
,
2140 ZEBRA_SR_POLICY_LABEL_REMOVED
);
2142 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2143 clear_nhlfe_installed(lsp
);
2148 * Install dynamic LSP entry.
2150 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2151 struct route_entry
*re
)
2153 struct route_table
*table
;
2154 struct zebra_fec
*fec
;
2156 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2160 /* See if there is a configured label binding for this FEC. */
2161 fec
= fec_find(table
, &rn
->p
);
2162 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2165 /* We cannot install a label forwarding entry if local label is the
2166 * implicit-null label.
2168 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2171 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2178 * Uninstall dynamic LSP entry, if any.
2180 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2181 struct route_entry
*re
)
2183 struct route_table
*table
;
2184 struct zebra_fec
*fec
;
2186 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2190 /* See if there is a configured label binding for this FEC. */
2191 fec
= fec_find(table
, &rn
->p
);
2192 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2195 /* Uninstall always removes all dynamic NHLFEs. */
2196 return lsp_uninstall(zvrf
, fec
->label
);
2200 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2201 * the LSP object - nothing is scheduled for processing, for example.
2203 struct zebra_nhlfe
*
2204 zebra_mpls_lsp_add_nhlfe(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
2205 enum nexthop_types_t gtype
, const union g_addr
*gate
,
2206 ifindex_t ifindex
, uint8_t num_labels
,
2207 const mpls_label_t
*out_labels
)
2209 /* Just a public pass-through to the internal implementation */
2210 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2211 out_labels
, false /*backup*/);
2215 * Add a backup NHLFE to an LSP, return the newly-added object.
2216 * This path only changes the LSP object - nothing is scheduled for
2217 * processing, for example.
2219 struct zebra_nhlfe
*zebra_mpls_lsp_add_backup_nhlfe(
2220 struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
2221 enum nexthop_types_t gtype
, const union g_addr
*gate
, ifindex_t ifindex
,
2222 uint8_t num_labels
, const mpls_label_t
*out_labels
)
2224 /* Just a public pass-through to the internal implementation */
2225 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2230 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2232 struct zebra_nhlfe
*zebra_mpls_lsp_add_nh(struct zebra_lsp
*lsp
,
2233 enum lsp_types_t lsp_type
,
2234 const struct nexthop
*nh
)
2236 struct zebra_nhlfe
*nhlfe
;
2238 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2241 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
, nh
->ifindex
,
2242 nh
->nh_label
->num_labels
, nh
->nh_label
->label
,
2249 * Add a backup NHLFE to an LSP based on a nexthop;
2250 * return the newly-added object.
2252 struct zebra_nhlfe
*zebra_mpls_lsp_add_backup_nh(struct zebra_lsp
*lsp
,
2253 enum lsp_types_t lsp_type
,
2254 const struct nexthop
*nh
)
2256 struct zebra_nhlfe
*nhlfe
;
2258 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2261 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
,
2262 nh
->ifindex
, nh
->nh_label
->num_labels
,
2263 nh
->nh_label
->label
, true);
2269 * Free an allocated NHLFE
2271 void zebra_mpls_nhlfe_free(struct zebra_nhlfe
*nhlfe
)
2273 /* Just a pass-through to the internal implementation */
2278 * Registration from a client for the label binding for a FEC. If a binding
2279 * already exists, it is informed to the client.
2280 * NOTE: If there is a manually configured label binding, that is used.
2281 * Otherwise, if a label index is specified, it means we have to allocate the
2282 * label from a locally configured label block (SRGB), if one exists and index
2283 * is acceptable. If no label index then just register the specified label.
2284 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2285 * by the calling function. Register requests with both will be rejected.
2287 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2288 uint32_t label
, uint32_t label_index
,
2289 struct zserv
*client
)
2291 struct route_table
*table
;
2292 struct zebra_fec
*fec
;
2294 bool label_change
= false;
2296 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2297 bool is_configured_fec
= false; /* indicate statically configured FEC */
2299 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2303 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2305 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2306 "Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
2307 p
, label
, label_index
,
2308 zebra_route_string(client
->proto
));
2313 fec
= fec_find(table
, p
);
2315 fec
= fec_add(table
, p
, label
, 0, label_index
);
2318 EC_ZEBRA_FEC_ADD_FAILED
,
2319 "Failed to add FEC %pFX upon register, client %s",
2320 p
, zebra_route_string(client
->proto
));
2324 old_label
= MPLS_INVALID_LABEL
;
2327 /* Check if the FEC has been statically defined in the config */
2328 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2329 /* Client may register same FEC with different label index. */
2331 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2332 if (!new_client
&& fec
->label_index
== label_index
2333 && fec
->label
== label
)
2334 /* Duplicate register */
2337 /* Save current label, update the FEC */
2338 old_label
= fec
->label
;
2339 fec
->label_index
= label_index
;
2343 listnode_add(fec
->client_list
, client
);
2345 if (IS_ZEBRA_DEBUG_MPLS
)
2346 zlog_debug("FEC %pFX label%s %u %s by client %s%s", p
,
2347 have_label_index
? " index" : "",
2348 have_label_index
? label_index
: label
,
2349 new_client
? "registered" : "updated",
2350 zebra_route_string(client
->proto
),
2352 ? ", but using statically configured label"
2355 /* If not a statically configured FEC, derive the local label
2356 * from label index or use the provided label
2358 if (!is_configured_fec
) {
2359 if (have_label_index
)
2360 fec_derive_label_from_index(zvrf
, fec
);
2364 /* If no label change, exit. */
2365 if (fec
->label
== old_label
)
2368 label_change
= true;
2371 /* If new client or label change, update client and install or uninstall
2372 * label forwarding entry as needed.
2374 /* Inform client of label, if needed. */
2375 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2376 if (IS_ZEBRA_DEBUG_MPLS
)
2377 zlog_debug("Update client label %u", fec
->label
);
2378 fec_send(fec
, client
);
2381 if (new_client
|| label_change
)
2382 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2388 * Deregistration from a client for the label binding for a FEC. The FEC
2389 * itself is deleted if no other registered clients exist and there is no
2390 * label bound to the FEC.
2392 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2393 struct zserv
*client
)
2395 struct route_table
*table
;
2396 struct zebra_fec
*fec
;
2398 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2402 fec
= fec_find(table
, p
);
2404 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2405 "Failed to find FEC %pFX upon unregister, client %s",
2406 p
, zebra_route_string(client
->proto
));
2410 listnode_delete(fec
->client_list
, client
);
2412 if (IS_ZEBRA_DEBUG_MPLS
)
2413 zlog_debug("FEC %pFX unregistered by client %s", p
,
2414 zebra_route_string(client
->proto
));
2416 /* If not a configured entry, delete the FEC if no other clients. Before
2417 * deleting, see if any LSP needs to be uninstalled.
2419 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2420 && list_isempty(fec
->client_list
)) {
2421 mpls_label_t old_label
= fec
->label
;
2422 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2423 fec_change_update_lsp(zvrf
, fec
, old_label
);
2431 * Cleanup any FECs registered by this client.
2433 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2435 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2436 struct route_node
*rn
;
2437 struct zebra_fec
*fec
;
2438 struct listnode
*node
;
2439 struct zserv
*fec_client
;
2442 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2443 if (zvrf
->fec_table
[af
] == NULL
)
2446 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2447 rn
= route_next(rn
)) {
2449 if (!fec
|| list_isempty(fec
->client_list
))
2452 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2454 if (fec_client
== client
) {
2455 listnode_delete(fec
->client_list
,
2457 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2458 && list_isempty(fec
->client_list
))
2469 struct lsp_uninstall_args
{
2470 struct hash
*lsp_table
;
2471 enum lsp_types_t type
;
2475 * Cleanup MPLS labels registered by this client.
2477 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2480 struct zebra_vrf
*zvrf
;
2482 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2483 struct lsp_uninstall_args args
;
2490 args
.lsp_table
= zvrf
->lsp_table
;
2491 args
.type
= lsp_type_from_re_type(client
->proto
);
2492 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2496 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2497 lsp_type_from_re_type(client
->proto
));
2498 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2499 lsp_type_from_re_type(client
->proto
));
2506 * Return FEC (if any) to which this label is bound.
2507 * Note: Only works for per-prefix binding and when the label is not
2509 * TODO: Currently walks entire table, can optimize later with another
2512 struct zebra_fec
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2515 struct route_node
*rn
;
2516 struct zebra_fec
*fec
;
2519 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2520 if (zvrf
->fec_table
[af
] == NULL
)
2523 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2524 rn
= route_next(rn
)) {
2528 if (fec
->label
== label
)
2537 * Inform if specified label is currently bound to a FEC or not.
2539 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2541 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2545 * Add static FEC to label binding. If there are clients registered for this
2546 * FEC, notify them. If there are labeled routes for this FEC, install the
2547 * label forwarding entry.
2549 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2550 mpls_label_t in_label
)
2552 struct route_table
*table
;
2553 struct zebra_fec
*fec
;
2554 mpls_label_t old_label
;
2557 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2561 /* Update existing FEC or create a new one. */
2562 fec
= fec_find(table
, p
);
2564 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2565 MPLS_INVALID_LABEL_INDEX
);
2567 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2568 "Failed to add FEC %pFX upon config", p
);
2572 if (IS_ZEBRA_DEBUG_MPLS
)
2573 zlog_debug("Add fec %pFX label %u", p
, in_label
);
2575 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2576 if (fec
->label
== in_label
)
2577 /* Duplicate config */
2580 /* Label change, update clients. */
2581 old_label
= fec
->label
;
2582 if (IS_ZEBRA_DEBUG_MPLS
)
2583 zlog_debug("Update fec %pFX new label %u", p
, in_label
);
2585 fec
->label
= in_label
;
2586 fec_update_clients(fec
);
2588 /* Update label forwarding entries appropriately */
2589 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2596 * Remove static FEC to label binding. If there are no clients registered
2597 * for this FEC, delete the FEC; else notify clients
2598 * Note: Upon delete of static binding, if label index exists for this FEC,
2599 * client may need to be updated with derived label.
2601 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2603 struct route_table
*table
;
2604 struct zebra_fec
*fec
;
2605 mpls_label_t old_label
;
2607 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2611 fec
= fec_find(table
, p
);
2613 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2614 "Failed to find FEC %pFX upon delete", p
);
2618 if (IS_ZEBRA_DEBUG_MPLS
) {
2619 zlog_debug("Delete fec %pFX label %u label index %u", p
,
2620 fec
->label
, fec
->label_index
);
2623 old_label
= fec
->label
;
2624 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2625 fec
->label
= MPLS_INVALID_LABEL
;
2627 /* If no client exists, just delete the FEC. */
2628 if (list_isempty(fec
->client_list
)) {
2633 /* Derive the local label (from label index) or reset it. */
2634 fec_derive_label_from_index(zvrf
, fec
);
2636 /* If there is a label change, update clients. */
2637 if (fec
->label
== old_label
)
2639 fec_update_clients(fec
);
2641 /* Update label forwarding entries appropriately */
2642 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2646 * Display MPLS FEC to label binding configuration (VTY command handler).
2648 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2650 struct route_node
*rn
;
2652 struct zebra_fec
*fec
;
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
)) {
2667 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2671 vty_out(vty
, "mpls label bind %pFX %s\n", &rn
->p
,
2672 label2str(fec
->label
, lstr
, BUFSIZ
));
2680 * Display MPLS FEC to label binding (VTY command handler).
2682 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2684 struct route_node
*rn
;
2687 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2688 if (zvrf
->fec_table
[af
] == NULL
)
2691 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2692 rn
= route_next(rn
)) {
2695 fec_print(rn
->info
, vty
);
2701 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2703 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2706 struct route_table
*table
;
2707 struct route_node
*rn
;
2709 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2714 rn
= route_node_lookup(table
, p
);
2718 route_unlock_node(rn
);
2722 fec_print(rn
->info
, vty
);
2725 static void mpls_zebra_nhe_update(struct route_entry
*re
, afi_t afi
,
2726 struct nhg_hash_entry
*new_nhe
)
2728 struct nhg_hash_entry
*nhe
;
2730 nhe
= zebra_nhg_rib_find_nhe(new_nhe
, afi
);
2732 route_entry_update_nhe(re
, nhe
);
2735 static bool ftn_update_nexthop(bool add_p
, struct nexthop
*nexthop
,
2736 enum lsp_types_t type
,
2737 const struct zapi_nexthop
*znh
)
2739 if (add_p
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2740 nexthop_add_labels(nexthop
, type
, znh
->label_num
, znh
->labels
);
2741 else if (!add_p
&& nexthop
->nh_label_type
== type
)
2742 nexthop_del_labels(nexthop
);
2749 void zebra_mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2750 struct prefix
*prefix
, uint8_t route_type
,
2751 uint8_t route_instance
)
2753 struct route_table
*table
;
2754 struct route_node
*rn
;
2755 struct route_entry
*re
;
2756 struct nexthop
*nexthop
;
2757 struct nhg_hash_entry
*new_nhe
;
2758 afi_t afi
= family2afi(prefix
->family
);
2761 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2765 /* Lookup existing route */
2766 rn
= route_node_get(table
, prefix
);
2767 RNODE_FOREACH_RE (rn
, re
) {
2768 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2770 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2777 * Nexthops are now shared by multiple routes, so we have to make
2778 * a local copy, modify the copy, then update the route.
2780 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2782 for (nexthop
= new_nhe
->nhg
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2783 nexthop_del_labels(nexthop
);
2785 /* Update backup routes/nexthops also, if present. */
2786 if (zebra_nhg_get_backup_nhg(new_nhe
) != NULL
) {
2787 for (nexthop
= new_nhe
->backup_info
->nhe
->nhg
.nexthop
; nexthop
;
2788 nexthop
= nexthop
->next
)
2789 nexthop_del_labels(nexthop
);
2792 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2793 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2795 /* This will create (or ref) a new nhe, so we will discard the local
2798 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
2800 zebra_nhg_free(new_nhe
);
2806 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2807 * update its labels according to 'add_p', and return 'true' if successful.
2809 static bool ftn_update_znh(bool add_p
, enum lsp_types_t type
,
2810 struct nexthop
*head
, const struct zapi_nexthop
*znh
)
2812 bool found
= false, success
= false;
2813 struct nexthop
*nexthop
;
2815 for (nexthop
= head
; nexthop
; nexthop
= nexthop
->next
) {
2816 switch (nexthop
->type
) {
2817 case NEXTHOP_TYPE_IPV4
:
2818 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2819 if (znh
->type
!= NEXTHOP_TYPE_IPV4
2820 && znh
->type
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2822 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
,
2825 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2826 && nexthop
->ifindex
!= znh
->ifindex
)
2831 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2836 case NEXTHOP_TYPE_IPV6
:
2837 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2838 if (znh
->type
!= NEXTHOP_TYPE_IPV6
2839 && znh
->type
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2841 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
,
2844 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2845 && nexthop
->ifindex
!= znh
->ifindex
)
2850 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2854 case NEXTHOP_TYPE_IFINDEX
:
2855 if (znh
->type
!= NEXTHOP_TYPE_IFINDEX
)
2857 if (nexthop
->ifindex
!= znh
->ifindex
)
2862 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2866 case NEXTHOP_TYPE_BLACKHOLE
:
2879 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2880 * using zapi message info.
2881 * There are several changes that need to be made, in several zebra
2882 * data structures, so we want to do all the work required at once.
2884 void zebra_mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
2885 const struct zapi_labels
*zl
)
2887 int i
, counter
, ret
= 0;
2888 char buf
[NEXTHOP_STRLEN
];
2889 const struct zapi_nexthop
*znh
;
2890 struct route_table
*table
;
2891 struct route_node
*rn
= NULL
;
2892 struct route_entry
*re
= NULL
;
2893 struct nhg_hash_entry
*new_nhe
= NULL
;
2896 const struct prefix
*prefix
= NULL
;
2897 struct hash
*lsp_table
;
2898 struct zebra_ile tmp_ile
;
2899 struct zebra_lsp
*lsp
= NULL
;
2901 /* Prep LSP for add case */
2904 lsp_table
= zvrf
->lsp_table
;
2908 /* Find or create LSP object */
2909 tmp_ile
.in_label
= zl
->local_label
;
2910 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2913 /* Prep for route/FEC update if requested */
2914 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2915 prefix
= &zl
->route
.prefix
;
2917 afi
= family2afi(prefix
->family
);
2920 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2922 /* Lookup existing route */
2923 rn
= route_node_get(table
, prefix
);
2924 RNODE_FOREACH_RE(rn
, re
) {
2925 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2927 if (re
->type
== zl
->route
.type
&&
2928 re
->instance
== zl
->route
.instance
)
2935 * Copy over current nexthops into a temporary group.
2936 * We can't just change the values here since the nhgs
2937 * are shared and if the labels change, we'll need
2938 * to find or create a new nhg. We need to create
2939 * a whole temporary group, make changes to it,
2940 * then attach that to the route.
2942 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2946 * The old version of the zapi code
2947 * attempted to manage LSPs before trying to
2948 * find a route/FEC, so we'll continue that way.
2950 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
)
2952 "%s: FTN update requested: no route for prefix %pFX",
2958 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2962 for (i
= 0; i
< zl
->nexthop_num
; i
++) {
2964 znh
= &zl
->nexthops
[i
];
2966 /* Attempt LSP update */
2968 ret
= lsp_znh_install(lsp
, zl
->type
, znh
);
2970 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
2971 zl
->local_label
, znh
->type
,
2972 &znh
->gate
, znh
->ifindex
,
2975 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
) {
2976 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2977 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
2978 __func__
, (add_p
? "" : "un"),
2979 zl
->local_label
, buf
);
2984 /* Attempt route/FEC update if requested */
2988 /* Search the route's nexthops for a match, and update it. */
2989 found
= ftn_update_znh(add_p
, zl
->type
, new_nhe
->nhg
.nexthop
,
2993 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
2994 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2996 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
2997 __func__
, prefix
, zl
->local_label
, buf
);
3002 * Process backup LSPs/nexthop entries also. We associate backup
3003 * LSP info with backup nexthops.
3005 if (!CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
))
3008 for (i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3010 znh
= &zl
->backup_nexthops
[i
];
3013 ret
= lsp_backup_znh_install(lsp
, zl
->type
, znh
);
3015 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
3017 znh
->type
, &znh
->gate
,
3018 znh
->ifindex
, true);
3021 if (IS_ZEBRA_DEBUG_RECV
||
3022 IS_ZEBRA_DEBUG_MPLS
) {
3023 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3024 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
3025 __func__
, (add_p
? "" : "un"),
3026 zl
->local_label
, buf
);
3031 /* Attempt backup nexthop/FEC update if requested */
3032 if (re
== NULL
|| zebra_nhg_get_backup_nhg(new_nhe
) == NULL
)
3035 /* Search the route's backup nexthops for a match
3038 found
= ftn_update_znh(add_p
, zl
->type
,
3039 new_nhe
->backup_info
->nhe
->nhg
.nexthop
,
3043 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
3044 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3046 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
3047 __func__
, prefix
, zl
->local_label
, buf
);
3054 * If we made changes, update the route, and schedule it
3055 * for rib processing
3057 if (re
!= NULL
&& counter
> 0) {
3060 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3061 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
3063 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3069 zebra_nhg_free(new_nhe
);
3073 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3074 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3075 * the out-label for an existing NHLFE (update case).
3077 static struct zebra_nhlfe
*
3078 lsp_add_nhlfe(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3079 uint8_t num_out_labels
, const mpls_label_t
*out_labels
,
3080 enum nexthop_types_t gtype
, const union g_addr
*gate
,
3081 ifindex_t ifindex
, bool is_backup
)
3083 struct zebra_nhlfe
*nhlfe
;
3084 char buf
[MPLS_LABEL_STRLEN
];
3085 const char *backup_str
;
3088 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3090 backup_str
= "backup ";
3092 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3098 struct nexthop
*nh
= nhlfe
->nexthop
;
3101 assert(nh
->nh_label
);
3103 /* Clear deleted flag (in case it was set) */
3104 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3105 if (nh
->nh_label
->num_labels
== num_out_labels
3106 && !memcmp(nh
->nh_label
->label
, out_labels
,
3107 sizeof(mpls_label_t
) * num_out_labels
))
3111 if (IS_ZEBRA_DEBUG_MPLS
) {
3112 char buf2
[MPLS_LABEL_STRLEN
];
3113 char buf3
[MPLS_LABEL_STRLEN
];
3115 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3116 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3118 mpls_label2str(nh
->nh_label
->num_labels
,
3119 nh
->nh_label
->label
, buf3
, sizeof(buf3
),
3122 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3123 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3127 /* Update out label(s), trigger processing. */
3128 if (nh
->nh_label
->num_labels
== num_out_labels
)
3129 memcpy(nh
->nh_label
->label
, out_labels
,
3130 sizeof(mpls_label_t
) * num_out_labels
);
3132 nexthop_del_labels(nh
);
3133 nexthop_add_labels(nh
, type
, num_out_labels
,
3137 /* Add LSP entry to this nexthop */
3138 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
,
3139 num_out_labels
, out_labels
, is_backup
);
3143 if (IS_ZEBRA_DEBUG_MPLS
) {
3144 char buf2
[MPLS_LABEL_STRLEN
];
3146 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3147 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3150 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3151 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3155 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
3158 /* Mark NHLFE, queue LSP for processing. */
3159 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3165 * Install an LSP and forwarding entry; used primarily
3166 * from vrf zapi message processing.
3168 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3169 mpls_label_t in_label
, uint8_t num_out_labels
,
3170 const mpls_label_t
*out_labels
, enum nexthop_types_t gtype
,
3171 const union g_addr
*gate
, ifindex_t ifindex
)
3173 struct hash
*lsp_table
;
3174 struct zebra_ile tmp_ile
;
3175 struct zebra_lsp
*lsp
;
3176 struct zebra_nhlfe
*nhlfe
;
3179 lsp_table
= zvrf
->lsp_table
;
3183 /* Find or create LSP object */
3184 tmp_ile
.in_label
= in_label
;
3185 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
3187 nhlfe
= lsp_add_nhlfe(lsp
, type
, num_out_labels
, out_labels
, gtype
,
3188 gate
, ifindex
, false /*backup*/);
3192 /* Queue LSP for processing. */
3193 if (lsp_processq_add(lsp
))
3200 * Install or replace NHLFE, using info from zapi nexthop
3202 static int lsp_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3203 const struct zapi_nexthop
*znh
)
3205 struct zebra_nhlfe
*nhlfe
;
3207 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
, znh
->labels
,
3208 znh
->type
, &znh
->gate
, znh
->ifindex
,
3213 /* Update backup info if present */
3214 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
3215 if (znh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
3220 nhlfe
->nexthop
->backup_num
= znh
->backup_num
;
3221 memcpy(nhlfe
->nexthop
->backup_idx
, znh
->backup_idx
,
3223 SET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3225 /* Ensure there's no stale backup info */
3226 UNSET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3227 nhlfe
->nexthop
->backup_num
= 0;
3230 /* Queue LSP for processing. */
3231 if (lsp_processq_add(lsp
))
3238 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3240 static int lsp_backup_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3241 const struct zapi_nexthop
*znh
)
3243 struct zebra_nhlfe
*nhlfe
;
3245 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
,
3246 znh
->labels
, znh
->type
, &znh
->gate
,
3247 znh
->ifindex
, true /*backup*/);
3248 if (nhlfe
== NULL
) {
3249 if (IS_ZEBRA_DEBUG_MPLS
)
3250 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3251 __func__
, lsp
->ile
.in_label
);
3255 /* Queue LSP for processing. */
3256 if (lsp_processq_add(lsp
))
3262 struct zebra_lsp
*mpls_lsp_find(struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
3264 struct hash
*lsp_table
;
3265 struct zebra_ile tmp_ile
;
3268 lsp_table
= zvrf
->lsp_table
;
3272 /* If entry is not present, exit. */
3273 tmp_ile
.in_label
= in_label
;
3274 return hash_lookup(lsp_table
, &tmp_ile
);
3278 * Uninstall a particular NHLFE in the forwarding table. If this is
3279 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3281 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3282 mpls_label_t in_label
, enum nexthop_types_t gtype
,
3283 const union g_addr
*gate
, ifindex_t ifindex
,
3286 struct hash
*lsp_table
;
3287 struct zebra_ile tmp_ile
;
3288 struct zebra_lsp
*lsp
;
3289 struct zebra_nhlfe
*nhlfe
;
3290 char buf
[NEXTHOP_STRLEN
];
3291 bool schedule_lsp
= false;
3294 lsp_table
= zvrf
->lsp_table
;
3298 /* If entry is not present, exit. */
3299 tmp_ile
.in_label
= in_label
;
3300 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3305 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3308 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3313 if (IS_ZEBRA_DEBUG_MPLS
) {
3314 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3315 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3316 in_label
, type
, buf
, nhlfe
->flags
);
3319 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ||
3320 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
3321 schedule_lsp
= true;
3323 /* Mark NHLFE for delete or directly delete, as appropriate. */
3325 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3326 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3328 if (IS_ZEBRA_DEBUG_MPLS
)
3329 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3330 lsp
->ile
.in_label
, lsp
->flags
);
3331 if (lsp_processq_add(lsp
))
3336 /* Free LSP entry if no other NHLFEs and not scheduled. */
3337 lsp_check_free(lsp_table
, &lsp
);
3342 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3343 mpls_label_t in_label
)
3345 struct hash
*lsp_table
;
3346 struct zebra_ile tmp_ile
;
3347 struct zebra_lsp
*lsp
;
3350 lsp_table
= zvrf
->lsp_table
;
3354 /* If entry is not present, exit. */
3355 tmp_ile
.in_label
= in_label
;
3356 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3360 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
3364 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3365 * If no other NHLFEs exist, the entry would be deleted.
3367 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
3369 struct lsp_uninstall_args
*args
= ctxt
;
3370 struct zebra_lsp
*lsp
;
3371 struct hash
*lsp_table
;
3373 lsp
= (struct zebra_lsp
*)bucket
->data
;
3374 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
)
3377 lsp_table
= args
->lsp_table
;
3381 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
3385 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3388 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
3389 int afi
, enum lsp_types_t lsp_type
)
3391 struct route_table
*table
;
3392 struct route_node
*rn
;
3393 struct route_entry
*re
;
3394 struct nexthop
*nexthop
;
3395 struct nexthop_group
*nhg
;
3398 /* Process routes of interested address-families. */
3399 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
3403 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
3406 RNODE_FOREACH_RE (rn
, re
) {
3407 struct nhg_hash_entry
*new_nhe
;
3409 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
3411 nhg
= &new_nhe
->nhg
;
3412 for (nexthop
= nhg
->nexthop
; nexthop
;
3413 nexthop
= nexthop
->next
) {
3414 if (nexthop
->nh_label_type
!= lsp_type
)
3417 nexthop_del_labels(nexthop
);
3418 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3419 SET_FLAG(re
->status
,
3420 ROUTE_ENTRY_LABELS_CHANGED
);
3424 /* Check for backup info and update that also */
3425 nhg
= zebra_nhg_get_backup_nhg(new_nhe
);
3427 for (nexthop
= nhg
->nexthop
; nexthop
;
3428 nexthop
= nexthop
->next
) {
3429 if (nexthop
->nh_label_type
!= lsp_type
)
3432 nexthop_del_labels(nexthop
);
3433 SET_FLAG(re
->status
,
3434 ROUTE_ENTRY_CHANGED
);
3435 SET_FLAG(re
->status
,
3436 ROUTE_ENTRY_LABELS_CHANGED
);
3441 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
3442 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3444 zebra_nhg_free(new_nhe
);
3452 #if defined(HAVE_CUMULUS)
3454 * Check that the label values used in LSP creation are consistent. The
3455 * main criteria is that if there is ECMP, the label operation must still
3456 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3457 * to current HW restrictions.
3459 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
3460 mpls_label_t in_label
,
3461 mpls_label_t out_label
,
3462 enum nexthop_types_t gtype
,
3463 union g_addr
*gate
, ifindex_t ifindex
)
3465 struct hash
*slsp_table
;
3466 struct zebra_ile tmp_ile
;
3467 struct zebra_lsp
*lsp
;
3468 struct zebra_nhlfe
*nhlfe
;
3469 const struct nexthop
*nh
;
3472 slsp_table
= zvrf
->slsp_table
;
3476 /* If entry is not present, exit. */
3477 tmp_ile
.in_label
= in_label
;
3478 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3482 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3483 gtype
, gate
, ifindex
);
3485 nh
= nhlfe
->nexthop
;
3487 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3490 if (nh
->nh_label
->label
[0] == out_label
)
3493 /* If not only NHLFE, cannot allow label change. */
3494 if (nhlfe
!= nhlfe_list_first(&lsp
->nhlfe_list
) ||
3495 nhlfe_list_next(&lsp
->nhlfe_list
, nhlfe
) != NULL
)
3498 /* If other NHLFEs exist, label operation must match. */
3499 nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
);
3500 if (nhlfe
!= NULL
) {
3503 nh
= nhlfe
->nexthop
;
3505 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3508 cur_op
= (nh
->nh_label
->label
[0] ==
3509 MPLS_LABEL_IMPLICIT_NULL
);
3510 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
3511 if (cur_op
!= new_op
)
3516 /* Label values are good. */
3519 #endif /* HAVE_CUMULUS */
3522 * Add static LSP entry. This may be the first entry for this incoming label
3523 * or an additional nexthop; an existing entry may also have outgoing label
3525 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3528 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3529 mpls_label_t out_label
,
3530 enum nexthop_types_t gtype
, union g_addr
*gate
,
3533 struct hash
*slsp_table
;
3534 struct zebra_ile tmp_ile
;
3535 struct zebra_lsp
*lsp
;
3536 struct zebra_nhlfe
*nhlfe
;
3540 slsp_table
= zvrf
->slsp_table
;
3544 /* Find or create LSP. */
3545 tmp_ile
.in_label
= in_label
;
3546 lsp
= hash_get(slsp_table
, &tmp_ile
, lsp_alloc
);
3548 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3551 struct nexthop
*nh
= nhlfe
->nexthop
;
3554 assert(nh
->nh_label
);
3556 /* Compare existing nexthop */
3557 if (nh
->nh_label
->num_labels
== 1 &&
3558 nh
->nh_label
->label
[0] == out_label
)
3562 if (IS_ZEBRA_DEBUG_MPLS
) {
3563 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3565 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3566 in_label
, buf
, out_label
,
3567 nh
->nh_label
->label
[0]);
3569 if (nh
->nh_label
->num_labels
== 1)
3570 nh
->nh_label
->label
[0] = out_label
;
3572 nexthop_del_labels(nh
);
3573 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
, 1, &out_label
);
3577 /* Add static LSP entry to this nexthop */
3578 nhlfe
= nhlfe_add(lsp
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3579 ifindex
, 1, &out_label
, false /*backup*/);
3583 if (IS_ZEBRA_DEBUG_MPLS
) {
3584 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3586 "Add static LSP in-label %u nexthop %s out-label %u",
3587 in_label
, buf
, out_label
);
3591 /* (Re)Install LSP in the main table. */
3592 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, 1, &out_label
,
3593 gtype
, gate
, ifindex
))
3600 * Delete static LSP entry. This may be the delete of one particular
3601 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3603 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3604 * LSP configuration.
3606 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3607 enum nexthop_types_t gtype
, union g_addr
*gate
,
3610 struct hash
*slsp_table
;
3611 struct zebra_ile tmp_ile
;
3612 struct zebra_lsp
*lsp
;
3613 struct zebra_nhlfe
*nhlfe
;
3616 slsp_table
= zvrf
->slsp_table
;
3620 /* If entry is not present, exit. */
3621 tmp_ile
.in_label
= in_label
;
3622 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3626 /* Is it delete of entire LSP or a specific NHLFE? */
3627 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3628 if (IS_ZEBRA_DEBUG_MPLS
)
3629 zlog_debug("Del static LSP in-label %u", in_label
);
3631 /* Uninstall entire LSP from the main table. */
3632 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3634 /* Delete all static NHLFEs */
3635 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3639 /* Find specific NHLFE, exit if not found. */
3640 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3641 gtype
, gate
, ifindex
);
3645 if (IS_ZEBRA_DEBUG_MPLS
) {
3647 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3648 zlog_debug("Del static LSP in-label %u nexthop %s",
3652 /* Uninstall LSP from the main table. */
3653 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3654 gate
, ifindex
, false);
3656 /* Delete static LSP NHLFE */
3660 /* Remove entire static LSP entry if no NHLFE - valid in either case
3663 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) {
3664 lsp
= hash_release(slsp_table
, &tmp_ile
);
3665 lsp_free_nhlfe(lsp
);
3666 XFREE(MTYPE_LSP
, lsp
);
3673 * Schedule all MPLS label forwarding entries for processing.
3674 * Called upon changes that may affect one or more of them such as
3675 * interface or nexthop state changes.
3677 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3681 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3685 * Display MPLS label forwarding table for a specific LSP
3686 * (VTY command handler).
3688 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3689 mpls_label_t label
, bool use_json
)
3691 struct hash
*lsp_table
;
3692 struct zebra_lsp
*lsp
;
3693 struct zebra_ile tmp_ile
;
3694 json_object
*json
= NULL
;
3697 lsp_table
= zvrf
->lsp_table
;
3701 /* If entry is not present, exit. */
3702 tmp_ile
.in_label
= label
;
3703 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3708 json
= lsp_json(lsp
);
3709 vty_json(vty
, json
);
3711 lsp_print(vty
, lsp
);
3715 * Display MPLS label forwarding table (VTY command handler).
3717 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3721 json_object
*json
= NULL
;
3722 struct zebra_lsp
*lsp
= NULL
;
3723 struct zebra_nhlfe
*nhlfe
= NULL
;
3724 struct listnode
*node
= NULL
;
3725 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3728 json
= json_object_new_object();
3730 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3731 json_object_object_add(
3732 json
, label2str(lsp
->ile
.in_label
, buf
,
3736 vty_json(vty
, json
);
3740 /* Prepare table. */
3741 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3742 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3743 tt
->style
.cell
.rpad
= 2;
3744 tt
->style
.corner
= '+';
3746 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3748 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3749 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3750 struct nexthop
*nexthop
;
3751 const char *out_label_str
;
3752 char nh_buf
[NEXTHOP_STRLEN
];
3754 nexthop
= nhlfe
->nexthop
;
3756 switch (nexthop
->type
) {
3757 case NEXTHOP_TYPE_IFINDEX
: {
3758 struct zebra_ns
*zns
;
3759 struct interface
*ifp
;
3761 zns
= zebra_ns_lookup(NS_DEFAULT
);
3762 ifp
= if_lookup_by_index_per_ns(
3763 zns
, nexthop
->ifindex
);
3764 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3765 ifp
? ifp
->name
: "Null");
3768 case NEXTHOP_TYPE_IPV4
:
3769 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3770 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3771 nh_buf
, sizeof(nh_buf
));
3773 case NEXTHOP_TYPE_IPV6
:
3774 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3775 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3776 nh_buf
, sizeof(nh_buf
));
3778 case NEXTHOP_TYPE_BLACKHOLE
:
3782 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3783 out_label_str
= mpls_label2str(
3784 nexthop
->nh_label
->num_labels
,
3785 &nexthop
->nh_label
->label
[0],
3786 buf
, sizeof(buf
), 1);
3788 out_label_str
= "-";
3790 ttable_add_row(tt
, "%u|%s|%s|%s",
3792 nhlfe_type2str(nhlfe
->type
),
3793 nh_buf
, out_label_str
);
3797 /* Dump the generated table. */
3798 if (tt
->nrows
> 1) {
3799 char *table
= ttable_dump(tt
, "\n");
3800 vty_out(vty
, "%s\n", table
);
3801 XFREE(MTYPE_TMP
, table
);
3806 list_delete(&lsp_list
);
3810 * Create printable string for static LSP configuration.
3812 static char *nhlfe_config_str(const struct zebra_nhlfe
*nhlfe
, char *buf
,
3815 const struct nexthop
*nh
;
3817 nh
= nhlfe
->nexthop
;
3821 case NEXTHOP_TYPE_IPV4
:
3822 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3823 inet_ntop(AF_INET
, &nh
->gate
.ipv4
, buf
, size
);
3825 strlcat(buf
, ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3828 case NEXTHOP_TYPE_IPV6
:
3829 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3830 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, size
);
3833 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3836 case NEXTHOP_TYPE_IFINDEX
:
3839 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3842 case NEXTHOP_TYPE_BLACKHOLE
:
3850 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3852 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3854 struct zebra_lsp
*lsp
;
3855 struct zebra_nhlfe
*nhlfe
;
3857 struct listnode
*node
;
3858 struct list
*slsp_list
=
3859 hash_get_sorted_list(zvrf
->slsp_table
, lsp_cmp
);
3861 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, lsp
)) {
3862 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3866 nh
= nhlfe
->nexthop
;
3867 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3870 nhlfe_config_str(nhlfe
, buf
, sizeof(buf
));
3872 switch (nh
->nh_label
->label
[0]) {
3873 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3874 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3875 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3877 case MPLS_LABEL_IMPLICIT_NULL
:
3878 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3881 snprintf(lstr
, sizeof(lstr
), "%u",
3882 nh
->nh_label
->label
[0]);
3886 vty_out(vty
, "mpls lsp %u %s %s\n", lsp
->ile
.in_label
,
3891 list_delete(&slsp_list
);
3892 return (zvrf
->slsp_table
->count
? 1 : 0);
3896 * Add/update global label block.
3898 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3901 zvrf
->mpls_srgb
.start_label
= start_label
;
3902 zvrf
->mpls_srgb
.end_label
= end_label
;
3904 /* Evaluate registered FECs to see if any get a label or not. */
3910 * Delete global label block.
3912 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3914 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3915 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3917 /* Process registered FECs to clear their local label, if needed. */
3923 * Display MPLS global label block configuration (VTY command handler).
3925 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3927 if (zvrf
->mpls_srgb
.start_label
== 0)
3930 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3931 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3932 vty_out(vty
, "mpls label global-block %u %u\n",
3933 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3940 * Called when VRF becomes inactive, cleans up information but keeps
3943 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3945 struct zebra_vrf
*def_zvrf
;
3948 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3949 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3952 * For other vrfs, we try to remove associated LSPs; we locate
3953 * the LSPs in the default vrf.
3955 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3957 /* At shutdown, the default may be gone already */
3958 if (def_zvrf
== NULL
)
3961 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3962 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3963 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3969 * When a vrf label is assigned and the client goes away
3970 * we should cleanup the vrf labels associated with
3973 void zebra_mpls_client_cleanup_vrf_label(uint8_t proto
)
3976 struct zebra_vrf
*def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3978 if (def_zvrf
== NULL
)
3981 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3982 struct zebra_vrf
*zvrf
= vrf
->info
;
3988 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3989 if (zvrf
->label_proto
[afi
] == proto
3990 && zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3991 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3994 * Cleanup data structures by fiat
3996 zvrf
->label_proto
[afi
] = 0;
3997 zvrf
->label
[afi
] = MPLS_LABEL_NONE
;
4002 static void lsp_table_free(void *p
)
4004 struct zebra_lsp
*lsp
= p
;
4006 lsp_free_nhlfe(lsp
);
4008 XFREE(MTYPE_LSP
, lsp
);
4012 * Called upon process exiting, need to delete LSP forwarding
4013 * entries from the kernel.
4014 * NOTE: Currently supported only for default VRF.
4016 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
4018 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
4019 hash_clean(zvrf
->lsp_table
, lsp_table_free
);
4020 hash_free(zvrf
->lsp_table
);
4021 hash_clean(zvrf
->slsp_table
, lsp_table_free
);
4022 hash_free(zvrf
->slsp_table
);
4023 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
4024 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
4028 * Allocate MPLS tables for this VRF and do other initialization.
4029 * NOTE: Currently supported only for default VRF.
4031 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
4038 snprintf(buffer
, sizeof(buffer
), "ZEBRA SLSP table: %s",
4040 zvrf
->slsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
4042 snprintf(buffer
, sizeof(buffer
), "ZEBRA LSP table: %s",
4044 zvrf
->lsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
4045 zvrf
->fec_table
[AFI_IP
] = route_table_init();
4046 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
4047 zvrf
->mpls_flags
= 0;
4048 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
4049 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
4052 void zebra_mpls_turned_on(void)
4054 if (!mpls_enabled
) {
4055 mpls_processq_init();
4056 mpls_enabled
= true;
4059 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
4060 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);
4064 * Global MPLS initialization.
4066 void zebra_mpls_init(void)
4068 mpls_enabled
= false;
4069 mpls_pw_reach_strict
= false;
4071 if (mpls_kernel_init() < 0) {
4072 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
4073 "Disabling MPLS support (no kernel support)");
4077 zebra_mpls_turned_on();