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
,
64 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
65 struct route_node
*rn
, struct route_entry
*re
);
66 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
67 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
68 mpls_label_t old_label
);
69 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
70 static void fec_update_clients(zebra_fec_t
*fec
);
71 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
72 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
73 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
74 mpls_label_t label
, uint32_t flags
,
75 uint32_t label_index
);
76 static int fec_del(zebra_fec_t
*fec
);
78 static unsigned int label_hash(const void *p
);
79 static bool label_cmp(const void *p1
, const void *p2
);
80 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
81 struct nexthop
*nexthop
);
82 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
86 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
87 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
88 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
89 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
90 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
91 static void lsp_processq_complete(struct work_queue
*wq
);
92 static int lsp_processq_add(zebra_lsp_t
*lsp
);
93 static void *lsp_alloc(void *p
);
95 /* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
96 static void lsp_check_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
);
98 /* Free lsp; sets caller's pointer to NULL */
99 static void lsp_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
);
101 static char *nhlfe2str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
102 static char *nhlfe_config_str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
103 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
104 const union g_addr
*gate
, ifindex_t ifindex
);
105 static zebra_nhlfe_t
*nhlfe_find(struct nhlfe_list_head
*list
,
106 enum lsp_types_t lsp_type
,
107 enum nexthop_types_t gtype
,
108 const union g_addr
*gate
, ifindex_t ifindex
);
109 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
110 enum nexthop_types_t gtype
,
111 const union g_addr
*gate
, ifindex_t ifindex
,
112 uint8_t num_labels
, const mpls_label_t
*labels
,
114 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
);
115 static void nhlfe_free(zebra_nhlfe_t
*nhlfe
);
116 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
117 struct mpls_label_stack
*nh_label
);
118 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
119 enum lsp_types_t type
);
120 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
121 mpls_label_t in_label
);
122 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
,
124 static void lsp_print(struct vty
*vty
, zebra_lsp_t
*lsp
);
125 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
126 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
127 int afi
, enum lsp_types_t lsp_type
);
128 static int lsp_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
129 const struct zapi_nexthop
*znh
);
130 static int lsp_backup_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
131 const struct zapi_nexthop
*znh
);
133 /* Static functions */
136 * Handle failure in LSP install, clear flags for NHLFE.
138 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
140 zebra_nhlfe_t
*nhlfe
;
141 struct nexthop
*nexthop
;
143 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
144 nexthop
= nhlfe
->nexthop
;
148 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
149 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
152 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
153 nexthop
= nhlfe
->nexthop
;
157 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
158 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
163 * Install label forwarding entry based on labeled-route entry.
165 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
166 struct route_node
*rn
, struct route_entry
*re
)
168 struct hash
*lsp_table
;
171 zebra_nhlfe_t
*nhlfe
;
172 struct nexthop
*nexthop
;
173 enum lsp_types_t lsp_type
;
178 lsp_table
= zvrf
->lsp_table
;
182 lsp_type
= lsp_type_from_re_type(re
->type
);
185 /* Locate or allocate LSP entry. */
186 tmp_ile
.in_label
= label
;
187 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
191 /* For each active nexthop, create NHLFE. Note that we deliberately skip
192 * recursive nexthops right now, because intermediate hops won't
194 * the label advertised by the recursive nexthop (plus we don't have the
195 * logic yet to push multiple labels).
197 for (nexthop
= re
->nhe
->nhg
.nexthop
;
198 nexthop
; nexthop
= nexthop
->next
) {
199 /* Skip inactive and recursive entries. */
200 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
202 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
205 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, lsp_type
,
206 nexthop
->type
, &nexthop
->gate
,
209 /* Clear deleted flag (in case it was set) */
210 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
211 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
216 if (IS_ZEBRA_DEBUG_MPLS
) {
217 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
219 "LSP in-label %u type %d nexthop %s out-label changed",
220 lsp
->ile
.in_label
, lsp_type
, buf
);
223 /* Update out label, trigger processing. */
224 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
225 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
228 /* Add LSP entry to this nexthop */
229 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
230 &nexthop
->gate
, nexthop
->ifindex
,
231 nexthop
->nh_label
->num_labels
,
232 nexthop
->nh_label
->label
,
237 if (IS_ZEBRA_DEBUG_MPLS
) {
238 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
240 "Add LSP in-label %u type %d nexthop %s out-label %u",
241 lsp
->ile
.in_label
, lsp_type
, buf
,
242 nexthop
->nh_label
->label
[0]);
245 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
247 /* Mark NHLFE as changed. */
248 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
253 /* Queue LSP for processing if necessary. If no NHLFE got added (special
254 * case), delete the LSP entry; this case results in somewhat ugly
257 if (added
|| changed
) {
258 if (lsp_processq_add(lsp
))
261 lsp_check_free(lsp_table
, &lsp
);
268 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
269 * NHLFEs are removed, the entire entry is deleted.
271 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
273 struct hash
*lsp_table
;
276 zebra_nhlfe_t
*nhlfe
;
280 lsp_table
= zvrf
->lsp_table
;
284 /* If entry is not present, exit. */
285 tmp_ile
.in_label
= label
;
286 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
287 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
290 /* Mark NHLFEs for delete or directly delete, as appropriate. */
291 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
293 /* Skip static NHLFEs */
294 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
297 if (IS_ZEBRA_DEBUG_MPLS
) {
298 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
300 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
301 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
304 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
305 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
306 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
312 /* Queue LSP for processing, if needed, else delete. */
313 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
314 if (lsp_processq_add(lsp
))
317 lsp_check_free(lsp_table
, &lsp
);
324 * This function is invoked upon change to label block configuration; it
325 * will walk all registered FECs with label-index and appropriately update
326 * their local labels and trigger client updates.
328 static void fec_evaluate(struct zebra_vrf
*zvrf
)
330 struct route_node
*rn
;
332 uint32_t old_label
, new_label
;
335 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
336 if (zvrf
->fec_table
[af
] == NULL
)
339 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
340 rn
= route_next(rn
)) {
341 if ((fec
= rn
->info
) == NULL
)
344 /* Skip configured FECs and those without a label index.
346 if (fec
->flags
& FEC_FLAG_CONFIGURED
347 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
350 /* Save old label, determine new label. */
351 old_label
= fec
->label
;
353 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
354 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
355 new_label
= MPLS_INVALID_LABEL
;
357 /* If label has changed, update FEC and clients. */
358 if (new_label
== old_label
)
361 if (IS_ZEBRA_DEBUG_MPLS
)
363 "Update fec %pRN new label %u upon label block",
366 fec
->label
= new_label
;
367 fec_update_clients(fec
);
369 /* Update label forwarding entries appropriately */
370 fec_change_update_lsp(zvrf
, fec
, old_label
);
376 * Derive (if possible) and update the local label for the FEC based on
377 * its label index. The index is "acceptable" if it falls within the
378 * globally configured label block (SRGB).
380 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
385 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
386 && zvrf
->mpls_srgb
.start_label
387 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
388 < zvrf
->mpls_srgb
.end_label
))
391 fec
->label
= MPLS_INVALID_LABEL
;
397 * There is a change for this FEC. Install or uninstall label forwarding
398 * entries, as appropriate.
400 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
401 mpls_label_t old_label
)
403 struct route_table
*table
;
404 struct route_node
*rn
;
405 struct route_entry
*re
;
408 /* Uninstall label forwarding entry, if previously installed. */
409 if (old_label
!= MPLS_INVALID_LABEL
410 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
411 lsp_uninstall(zvrf
, old_label
);
413 /* Install label forwarding entry corr. to new label, if needed. */
414 if (fec
->label
== MPLS_INVALID_LABEL
415 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
418 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
419 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
423 /* See if labeled route exists. */
424 rn
= route_node_lookup(table
, &fec
->rn
->p
);
428 RNODE_FOREACH_RE (rn
, re
) {
429 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
433 if (!re
|| !zebra_rib_labeled_unicast(re
))
436 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
443 * Inform about FEC to a registered client.
445 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
448 struct route_node
*rn
;
452 /* Get output stream. */
453 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
455 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
457 stream_putw(s
, rn
->p
.family
);
458 stream_put_prefix(s
, &rn
->p
);
459 stream_putl(s
, fec
->label
);
460 stream_putw_at(s
, 0, stream_get_endp(s
));
461 return zserv_send_message(client
, s
);
465 * Update all registered clients about this FEC. Caller should've updated
466 * FEC and ensure no duplicate updates.
468 static void fec_update_clients(zebra_fec_t
*fec
)
470 struct listnode
*node
;
471 struct zserv
*client
;
473 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
474 if (IS_ZEBRA_DEBUG_MPLS
)
475 zlog_debug("Update client %s",
476 zebra_route_string(client
->proto
));
477 fec_send(fec
, client
);
483 * Print a FEC-label binding entry.
485 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
487 struct route_node
*rn
;
488 struct listnode
*node
;
489 struct zserv
*client
;
493 vty_out(vty
, "%pRN\n", rn
);
494 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
495 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
496 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
498 if (!list_isempty(fec
->client_list
)) {
499 vty_out(vty
, " Client list:");
500 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
501 vty_out(vty
, " %s(fd %d)",
502 zebra_route_string(client
->proto
),
509 * Locate FEC-label binding that matches with passed info.
511 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
513 struct route_node
*rn
;
516 rn
= route_node_lookup(table
, p
);
520 route_unlock_node(rn
);
525 * Add a FEC. This may be upon a client registering for a binding
526 * or when a binding is configured.
528 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
529 mpls_label_t label
, uint32_t flags
,
530 uint32_t label_index
)
532 struct route_node
*rn
;
537 /* Lookup (or add) route node.*/
538 rn
= route_node_get(table
, p
);
545 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
550 fec
->client_list
= list_new();
552 route_unlock_node(rn
); /* for the route_node_get */
554 fec
->label_index
= label_index
;
561 * Delete a FEC. This may be upon the last client deregistering for
562 * a FEC and no binding exists or when the binding is deleted and there
563 * are no registered clients.
565 static int fec_del(zebra_fec_t
*fec
)
567 list_delete(&fec
->client_list
);
568 fec
->rn
->info
= NULL
;
569 route_unlock_node(fec
->rn
);
570 XFREE(MTYPE_FEC
, fec
);
575 * Hash function for label.
577 static unsigned int label_hash(const void *p
)
579 const zebra_ile_t
*ile
= p
;
581 return (jhash_1word(ile
->in_label
, 0));
585 * Compare 2 LSP hash entries based on in-label.
587 static bool label_cmp(const void *p1
, const void *p2
)
589 const zebra_ile_t
*ile1
= p1
;
590 const zebra_ile_t
*ile2
= p2
;
592 return (ile1
->in_label
== ile2
->in_label
);
596 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
598 * NOTE: Looking only for connected routes right now.
600 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
601 struct nexthop
*nexthop
)
603 struct route_table
*table
;
604 struct prefix_ipv4 p
;
605 struct route_node
*rn
;
606 struct route_entry
*match
;
607 struct nexthop
*match_nh
;
609 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
613 /* Lookup nexthop in IPv4 routing table. */
614 memset(&p
, 0, sizeof(struct prefix_ipv4
));
616 p
.prefixlen
= IPV4_MAX_BITLEN
;
617 p
.prefix
= nexthop
->gate
.ipv4
;
619 rn
= route_node_match(table
, (struct prefix
*)&p
);
623 route_unlock_node(rn
);
625 /* Locate a valid connected route. */
626 RNODE_FOREACH_RE (rn
, match
) {
627 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
628 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
631 for (match_nh
= match
->nhe
->nhg
.nexthop
; match_nh
;
632 match_nh
= match_nh
->next
) {
633 if (match
->type
== ZEBRA_ROUTE_CONNECT
634 || nexthop
->ifindex
== match_nh
->ifindex
) {
635 nexthop
->ifindex
= match_nh
->ifindex
;
646 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
648 * NOTE: Looking only for connected routes right now.
650 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
651 struct nexthop
*nexthop
)
653 struct route_table
*table
;
654 struct prefix_ipv6 p
;
655 struct route_node
*rn
;
656 struct route_entry
*match
;
658 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
662 /* Lookup nexthop in IPv6 routing table. */
663 memset(&p
, 0, sizeof(struct prefix_ipv6
));
665 p
.prefixlen
= IPV6_MAX_BITLEN
;
666 p
.prefix
= nexthop
->gate
.ipv6
;
668 rn
= route_node_match(table
, (struct prefix
*)&p
);
672 route_unlock_node(rn
);
674 /* Locate a valid connected route. */
675 RNODE_FOREACH_RE (rn
, match
) {
676 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
677 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
678 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
682 if (!match
|| !match
->nhe
->nhg
.nexthop
)
685 nexthop
->ifindex
= match
->nhe
->nhg
.nexthop
->ifindex
;
691 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
693 * NOTE: Each NHLFE points to only 1 nexthop.
695 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
697 struct nexthop
*nexthop
;
698 struct interface
*ifp
;
699 struct zebra_ns
*zns
;
701 nexthop
= nhlfe
->nexthop
;
702 if (!nexthop
) // unexpected
705 /* Check on nexthop based on type. */
706 switch (nexthop
->type
) {
707 case NEXTHOP_TYPE_IFINDEX
:
709 * Lookup if this type is special. The
710 * NEXTHOP_TYPE_IFINDEX is a pop and
711 * forward into a different table for
712 * processing. As such this ifindex
713 * passed to us may be a VRF device
714 * which will not be in the default
715 * VRF. So let's look in all of them
717 zns
= zebra_ns_lookup(NS_DEFAULT
);
718 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
719 if (ifp
&& if_is_operative(ifp
))
720 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
722 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
724 case NEXTHOP_TYPE_IPV4
:
725 case NEXTHOP_TYPE_IPV4_IFINDEX
:
726 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
727 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
729 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
732 case NEXTHOP_TYPE_IPV6
:
733 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
734 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
736 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
739 case NEXTHOP_TYPE_IPV6_IFINDEX
:
740 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
741 ifp
= if_lookup_by_index(nexthop
->ifindex
,
743 if (ifp
&& if_is_operative(ifp
))
744 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
746 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
748 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
749 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
751 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
759 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
763 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
764 * reachability and select the best. Multipath entries are also
765 * marked. This is invoked when an LSP scheduled for processing (due
766 * to some change) is examined.
768 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
770 zebra_nhlfe_t
*nhlfe
;
772 struct nexthop
*nexthop
;
780 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
783 * First compute the best path, after checking nexthop status. We are
784 * only concerned with non-deleted NHLFEs.
786 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
787 /* Clear selection flags. */
788 UNSET_FLAG(nhlfe
->flags
,
789 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
791 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
792 && nhlfe_nexthop_active(nhlfe
)) {
793 if (!best
|| (nhlfe
->distance
< best
->distance
))
798 lsp
->best_nhlfe
= best
;
799 if (!lsp
->best_nhlfe
)
803 * Check the active status of backup nhlfes also
805 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
806 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
807 (void)nhlfe_nexthop_active(nhlfe
);
810 /* Mark best NHLFE as selected. */
811 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
814 * If best path exists, see if there is ECMP. While doing this, note if
816 * new (uninstalled) NHLFE has been selected, an installed entry that is
817 * still selected has a change or an installed entry is to be removed.
819 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
820 int nh_chg
, nh_sel
, nh_inst
;
822 nexthop
= nhlfe
->nexthop
;
823 if (!nexthop
) // unexpected
826 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
827 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
828 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
829 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
830 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
834 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
835 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
836 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
838 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
840 if ((nh_sel
&& !nh_inst
)
841 || (nh_sel
&& nh_inst
&& nh_chg
)
842 || (nh_inst
&& !nh_sel
))
846 /* We have finished examining, clear changed flag. */
847 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
851 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
855 * Delete LSP forwarding entry from kernel, if installed. Called upon
858 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
862 lsp
= (zebra_lsp_t
*)bucket
->data
;
863 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
864 (void)dplane_lsp_delete(lsp
);
868 * Schedule LSP forwarding entry for processing. Called upon changes
869 * that may impact LSPs such as nexthop / connected route changes.
871 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
875 lsp
= (zebra_lsp_t
*)bucket
->data
;
877 /* In the common flow, this is used when external events occur. For
878 * LSPs with backup nhlfes, we'll assume that the forwarding
879 * plane will use the backups to handle these events, until the
880 * owning protocol can react.
883 /* Skip LSPs with backups */
884 if (nhlfe_list_first(&lsp
->backup_nhlfe_list
) != NULL
) {
885 if (IS_ZEBRA_DEBUG_MPLS_DETAIL
)
886 zlog_debug("%s: skip LSP in-label %u",
887 __func__
, lsp
->ile
.in_label
);
892 (void)lsp_processq_add(lsp
);
896 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
897 * any multipaths and update or delete from the kernel, as needed.
899 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
902 zebra_nhlfe_t
*oldbest
, *newbest
;
903 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
904 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
905 enum zebra_dplane_result res
;
907 lsp
= (zebra_lsp_t
*)data
;
908 if (!lsp
) // unexpected
911 oldbest
= lsp
->best_nhlfe
;
913 /* Select best NHLFE(s) */
914 lsp_select_best_nhlfe(lsp
);
916 newbest
= lsp
->best_nhlfe
;
918 if (IS_ZEBRA_DEBUG_MPLS
) {
920 nhlfe2str(oldbest
, buf
, sizeof(buf
));
922 nhlfe2str(newbest
, buf2
, sizeof(buf2
));
924 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
925 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
926 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
929 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
930 /* Not already installed */
933 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
935 switch (dplane_lsp_add(lsp
)) {
936 case ZEBRA_DPLANE_REQUEST_QUEUED
:
937 /* Set 'installed' flag so we will know
938 * that an install is in-flight.
940 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
942 zvrf
->lsp_installs_queued
++;
944 case ZEBRA_DPLANE_REQUEST_FAILURE
:
945 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
946 "LSP Install Failure: %u",
949 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
950 zvrf
->lsp_installs
++;
955 /* Installed, may need an update and/or delete. */
957 res
= dplane_lsp_delete(lsp
);
959 /* We do some of the lsp cleanup immediately for
962 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
963 clear_nhlfe_installed(lsp
);
966 case ZEBRA_DPLANE_REQUEST_QUEUED
:
967 zvrf
->lsp_removals_queued
++;
969 case ZEBRA_DPLANE_REQUEST_FAILURE
:
970 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
971 "LSP Deletion Failure: %u",
974 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
975 zvrf
->lsp_removals
++;
978 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
979 zebra_nhlfe_t
*nhlfe
;
980 struct nexthop
*nexthop
;
982 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
984 /* We leave the INSTALLED flag set here
985 * so we know an update is in-flight.
989 * Any NHLFE that was installed but is not
990 * selected now needs to have its flags updated.
992 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
993 nexthop
= nhlfe
->nexthop
;
997 if (CHECK_FLAG(nhlfe
->flags
,
998 NHLFE_FLAG_INSTALLED
)
999 && !CHECK_FLAG(nhlfe
->flags
,
1000 NHLFE_FLAG_SELECTED
)) {
1001 UNSET_FLAG(nhlfe
->flags
,
1002 NHLFE_FLAG_INSTALLED
);
1003 UNSET_FLAG(nexthop
->flags
,
1008 switch (dplane_lsp_update(lsp
)) {
1009 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1010 zvrf
->lsp_installs_queued
++;
1012 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1013 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1014 "LSP Update Failure: %u",
1017 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1018 zvrf
->lsp_installs
++;
1029 * Callback upon processing completion of a LSP forwarding entry.
1031 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1033 struct zebra_vrf
*zvrf
;
1035 struct hash
*lsp_table
;
1036 zebra_nhlfe_t
*nhlfe
;
1038 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1041 lsp_table
= zvrf
->lsp_table
;
1042 if (!lsp_table
) // unexpected
1045 lsp
= (zebra_lsp_t
*)data
;
1046 if (!lsp
) // unexpected
1049 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1051 * delete LSP entry also.
1053 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1055 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1056 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1060 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1061 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1065 lsp_check_free(lsp_table
, &lsp
);
1069 * Callback upon finishing the processing of all scheduled
1070 * LSP forwarding entries.
1072 static void lsp_processq_complete(struct work_queue
*wq
)
1074 /* Nothing to do for now. */
1078 * Add LSP forwarding entry to queue for subsequent processing.
1080 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1082 /* If already scheduled, exit. */
1083 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1086 if (zrouter
.lsp_process_q
== NULL
) {
1087 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1088 "%s: work_queue does not exist!", __func__
);
1092 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1093 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1098 * Callback to allocate LSP forwarding table entry.
1100 static void *lsp_alloc(void *p
)
1102 const zebra_ile_t
*ile
= p
;
1105 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1107 nhlfe_list_init(&lsp
->nhlfe_list
);
1108 nhlfe_list_init(&lsp
->backup_nhlfe_list
);
1110 if (IS_ZEBRA_DEBUG_MPLS
)
1111 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1113 return ((void *)lsp
);
1117 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1119 static void lsp_check_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
)
1123 if (plsp
== NULL
|| *plsp
== NULL
)
1128 if ((nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) &&
1129 (nhlfe_list_first(&lsp
->backup_nhlfe_list
) == NULL
) &&
1130 !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1131 lsp_free(lsp_table
, plsp
);
1135 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1138 static void lsp_free(struct hash
*lsp_table
, zebra_lsp_t
**plsp
)
1141 zebra_nhlfe_t
*nhlfe
;
1143 if (plsp
== NULL
|| *plsp
== NULL
)
1148 if (IS_ZEBRA_DEBUG_MPLS
)
1149 zlog_debug("Free LSP in-label %u flags 0x%x",
1150 lsp
->ile
.in_label
, lsp
->flags
);
1152 /* Free nhlfes, if any. */
1153 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1156 /* Free backup nhlfes, if any. */
1157 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
)
1160 hash_release(lsp_table
, &lsp
->ile
);
1161 XFREE(MTYPE_LSP
, lsp
);
1167 * Create printable string for NHLFE entry.
1169 static char *nhlfe2str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1171 const struct nexthop
*nexthop
;
1174 nexthop
= nhlfe
->nexthop
;
1175 switch (nexthop
->type
) {
1176 case NEXTHOP_TYPE_IPV4
:
1177 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1178 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1180 case NEXTHOP_TYPE_IPV6
:
1181 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1182 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1184 case NEXTHOP_TYPE_IFINDEX
:
1185 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1194 * Check if NHLFE matches with search info passed.
1196 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1197 const union g_addr
*gate
, ifindex_t ifindex
)
1199 struct nexthop
*nhop
;
1202 nhop
= nhlfe
->nexthop
;
1206 if (nhop
->type
!= gtype
)
1209 switch (nhop
->type
) {
1210 case NEXTHOP_TYPE_IPV4
:
1211 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1212 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1213 sizeof(struct in_addr
));
1214 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1215 cmp
= !(nhop
->ifindex
== ifindex
);
1217 case NEXTHOP_TYPE_IPV6
:
1218 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1219 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1220 sizeof(struct in6_addr
));
1221 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1222 cmp
= !(nhop
->ifindex
== ifindex
);
1224 case NEXTHOP_TYPE_IFINDEX
:
1225 cmp
= !(nhop
->ifindex
== ifindex
);
1236 * Locate NHLFE that matches with passed info.
1238 static zebra_nhlfe_t
*nhlfe_find(struct nhlfe_list_head
*list
,
1239 enum lsp_types_t lsp_type
,
1240 enum nexthop_types_t gtype
,
1241 const union g_addr
*gate
, ifindex_t ifindex
)
1243 zebra_nhlfe_t
*nhlfe
;
1245 frr_each_safe(nhlfe_list
, list
, nhlfe
) {
1246 if (nhlfe
->type
!= lsp_type
)
1248 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1256 * Allocate and init new NHLFE.
1258 static zebra_nhlfe_t
*nhlfe_alloc(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1259 enum nexthop_types_t gtype
,
1260 const union g_addr
*gate
, ifindex_t ifindex
,
1262 const mpls_label_t
*labels
)
1264 zebra_nhlfe_t
*nhlfe
;
1265 struct nexthop
*nexthop
;
1269 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1272 nhlfe
->type
= lsp_type
;
1273 nhlfe
->distance
= lsp_distance(lsp_type
);
1275 nexthop
= nexthop_new();
1277 nexthop_add_labels(nexthop
, lsp_type
, num_labels
, labels
);
1279 nexthop
->vrf_id
= VRF_DEFAULT
;
1280 nexthop
->type
= gtype
;
1281 switch (nexthop
->type
) {
1282 case NEXTHOP_TYPE_IPV4
:
1283 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1284 nexthop
->gate
.ipv4
= gate
->ipv4
;
1286 nexthop
->ifindex
= ifindex
;
1288 case NEXTHOP_TYPE_IPV6
:
1289 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1290 nexthop
->gate
.ipv6
= gate
->ipv6
;
1292 nexthop
->ifindex
= ifindex
;
1294 case NEXTHOP_TYPE_IFINDEX
:
1295 nexthop
->ifindex
= ifindex
;
1298 nexthop_free(nexthop
);
1299 XFREE(MTYPE_NHLFE
, nhlfe
);
1302 nhlfe
->nexthop
= nexthop
;
1308 * Add primary or backup NHLFE. Base entry must have been created and
1309 * duplicate check done.
1311 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1312 enum nexthop_types_t gtype
,
1313 const union g_addr
*gate
, ifindex_t ifindex
,
1314 uint8_t num_labels
, const mpls_label_t
*labels
,
1317 zebra_nhlfe_t
*nhlfe
;
1322 /* Allocate new object */
1323 nhlfe
= nhlfe_alloc(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
1326 /* Enqueue to LSP: primaries at head of list, backups at tail */
1328 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
);
1329 nhlfe_list_add_tail(&lsp
->backup_nhlfe_list
, nhlfe
);
1331 nhlfe_list_add_head(&lsp
->nhlfe_list
, nhlfe
);
1337 * Common delete for NHLFEs.
1339 static void nhlfe_free(zebra_nhlfe_t
*nhlfe
)
1346 nexthop_free(nhlfe
->nexthop
);
1348 nhlfe
->nexthop
= NULL
;
1350 XFREE(MTYPE_NHLFE
, nhlfe
);
1355 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1357 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1368 if (nhlfe
== lsp
->best_nhlfe
)
1369 lsp
->best_nhlfe
= NULL
;
1371 /* Unlink from LSP */
1372 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
))
1373 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1375 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1385 * Update label for NHLFE entry.
1387 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1388 struct mpls_label_stack
*nh_label
)
1390 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1393 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1394 enum lsp_types_t type
)
1396 zebra_nhlfe_t
*nhlfe
;
1397 int schedule_lsp
= 0;
1400 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1403 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1404 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1405 /* Skip non-static NHLFEs */
1406 if (nhlfe
->type
!= type
)
1409 if (IS_ZEBRA_DEBUG_MPLS
) {
1410 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1412 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1413 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1416 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1417 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1418 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1425 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1426 /* Skip non-static NHLFEs */
1427 if (nhlfe
->type
!= type
)
1430 if (IS_ZEBRA_DEBUG_MPLS
) {
1431 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1433 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1434 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1437 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1438 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1439 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1446 /* Queue LSP for processing, if needed, else delete. */
1448 if (IS_ZEBRA_DEBUG_MPLS
) {
1449 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1450 lsp
->ile
.in_label
, lsp
->flags
);
1452 if (lsp_processq_add(lsp
))
1455 lsp_check_free(lsp_table
, &lsp
);
1462 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1463 * If no other NHLFEs exist, the entry would be deleted.
1465 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1466 mpls_label_t in_label
)
1468 struct hash
*lsp_table
;
1469 zebra_ile_t tmp_ile
;
1473 lsp_table
= zvrf
->lsp_table
;
1477 /* If entry is not present, exit. */
1478 tmp_ile
.in_label
= in_label
;
1479 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1480 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
1483 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1486 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1489 json_object
*json_nhlfe
= NULL
;
1490 json_object
*json_backups
= NULL
;
1491 json_object
*json_label_stack
;
1492 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1495 json_nhlfe
= json_object_new_object();
1496 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1497 json_object_int_add(json_nhlfe
, "outLabel",
1498 nexthop
->nh_label
->label
[0]);
1500 json_label_stack
= json_object_new_array();
1501 json_object_object_add(json_nhlfe
, "outLabelStack", json_label_stack
);
1502 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++)
1503 json_object_array_add(
1505 json_object_new_int(nexthop
->nh_label
->label
[i
]));
1507 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1509 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1510 json_object_boolean_true_add(json_nhlfe
, "installed");
1512 switch (nexthop
->type
) {
1513 case NEXTHOP_TYPE_IPV4
:
1514 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1515 json_object_string_add(json_nhlfe
, "nexthop",
1516 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
1519 case NEXTHOP_TYPE_IPV6
:
1520 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1521 json_object_string_add(
1522 json_nhlfe
, "nexthop",
1523 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1525 if (nexthop
->ifindex
)
1526 json_object_string_add(json_nhlfe
, "interface",
1527 ifindex2ifname(nexthop
->ifindex
,
1534 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1535 json_backups
= json_object_new_array();
1536 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1537 json_object_array_add(
1539 json_object_new_int(nexthop
->backup_idx
[i
]));
1542 json_object_object_add(json_nhlfe
, "backupIndex",
1550 * Print the NHLFE for a LSP forwarding entry.
1552 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
,
1555 struct nexthop
*nexthop
;
1556 char buf
[MPLS_LABEL_STRLEN
];
1558 nexthop
= nhlfe
->nexthop
;
1559 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1562 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1563 nhlfe_type2str(nhlfe
->type
),
1564 mpls_label2str(nexthop
->nh_label
->num_labels
,
1565 nexthop
->nh_label
->label
,
1566 buf
, sizeof(buf
), 0),
1570 vty_out(vty
, "%s", indent
);
1572 switch (nexthop
->type
) {
1573 case NEXTHOP_TYPE_IPV4
:
1574 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1575 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1576 if (nexthop
->ifindex
)
1577 vty_out(vty
, " dev %s",
1578 ifindex2ifname(nexthop
->ifindex
,
1581 case NEXTHOP_TYPE_IPV6
:
1582 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1583 vty_out(vty
, " via %s",
1584 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
1586 if (nexthop
->ifindex
)
1587 vty_out(vty
, " dev %s",
1588 ifindex2ifname(nexthop
->ifindex
,
1595 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
) ? " (backup)"
1598 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1604 * Print an LSP forwarding entry.
1606 static void lsp_print(struct vty
*vty
, zebra_lsp_t
*lsp
)
1608 zebra_nhlfe_t
*nhlfe
, *backup
;
1611 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1612 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1615 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1616 nhlfe_print(nhlfe
, vty
, NULL
);
1618 if (nhlfe
->nexthop
== NULL
||
1619 !CHECK_FLAG(nhlfe
->nexthop
->flags
,
1620 NEXTHOP_FLAG_HAS_BACKUP
))
1623 /* Backup nhlfes: find backups in backup list */
1625 for (j
= 0; j
< nhlfe
->nexthop
->backup_num
; j
++) {
1628 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, backup
) {
1629 if (i
== nhlfe
->nexthop
->backup_idx
[j
])
1635 vty_out(vty
, " [backup %d]", i
);
1636 nhlfe_print(backup
, vty
, " ");
1643 * JSON objects for an LSP forwarding entry.
1645 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1647 zebra_nhlfe_t
*nhlfe
= NULL
;
1648 json_object
*json
= json_object_new_object();
1649 json_object
*json_nhlfe_list
= json_object_new_array();
1651 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1653 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1654 json_object_boolean_true_add(json
, "installed");
1656 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1657 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1659 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1660 json_nhlfe_list
= NULL
;
1663 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1664 if (json_nhlfe_list
== NULL
)
1665 json_nhlfe_list
= json_object_new_array();
1667 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1670 if (json_nhlfe_list
)
1671 json_object_object_add(json
, "backupNexthops", json_nhlfe_list
);
1677 /* Return a sorted linked list of the hash contents */
1678 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1681 struct hash_bucket
*hb
;
1682 struct list
*sorted_list
= list_new();
1684 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1686 for (i
= 0; i
< hash
->size
; i
++)
1687 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1688 listnode_add_sort(sorted_list
, hb
->data
);
1694 * Compare two LSPs based on their label values.
1696 static int lsp_cmp(const zebra_lsp_t
*lsp1
, const zebra_lsp_t
*lsp2
)
1698 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1701 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1708 * Initialize work queue for processing changed LSPs.
1710 static int mpls_processq_init(void)
1712 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1713 if (!zrouter
.lsp_process_q
) {
1714 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1715 "%s: could not initialise work queue!", __func__
);
1719 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1720 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1721 zrouter
.lsp_process_q
->spec
.errorfunc
= NULL
;
1722 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1723 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1724 zrouter
.lsp_process_q
->spec
.hold
= 10;
1731 * Process LSP update results from zebra dataplane.
1733 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1735 struct zebra_vrf
*zvrf
;
1737 zebra_ile_t tmp_ile
;
1738 struct hash
*lsp_table
;
1740 zebra_nhlfe_t
*nhlfe
;
1741 struct nexthop
*nexthop
;
1742 enum dplane_op_e op
;
1743 enum zebra_dplane_result status
;
1744 enum zebra_sr_policy_update_label_mode update_mode
;
1746 op
= dplane_ctx_get_op(ctx
);
1747 status
= dplane_ctx_get_status(ctx
);
1749 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1750 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1751 ctx
, dplane_op2str(op
),
1752 dplane_ctx_get_in_label(ctx
),
1753 dplane_res2str(status
));
1755 label
= dplane_ctx_get_in_label(ctx
);
1758 case DPLANE_OP_LSP_INSTALL
:
1759 case DPLANE_OP_LSP_UPDATE
:
1760 /* Look for zebra LSP object */
1761 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1765 lsp_table
= zvrf
->lsp_table
;
1767 tmp_ile
.in_label
= label
;
1768 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1770 if (IS_ZEBRA_DEBUG_DPLANE
)
1771 zlog_debug("LSP ctx %p: in-label %u not found",
1772 ctx
, dplane_ctx_get_in_label(ctx
));
1776 /* TODO -- Confirm that this result is still 'current' */
1778 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1779 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1780 clear_nhlfe_installed(lsp
);
1781 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1782 "LSP Install Failure: in-label %u",
1787 /* Update zebra object */
1788 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1789 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1790 nexthop
= nhlfe
->nexthop
;
1794 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
) &&
1795 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1796 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1797 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1801 update_mode
= (op
== DPLANE_OP_LSP_INSTALL
)
1802 ? ZEBRA_SR_POLICY_LABEL_CREATED
1803 : ZEBRA_SR_POLICY_LABEL_UPDATED
;
1804 zebra_sr_policy_label_update(label
, update_mode
);
1807 case DPLANE_OP_LSP_DELETE
:
1808 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1809 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1810 "LSP Deletion Failure: in-label %u",
1811 dplane_ctx_get_in_label(ctx
));
1814 zebra_sr_policy_label_update(label
,
1815 ZEBRA_SR_POLICY_LABEL_REMOVED
);
1823 dplane_ctx_fini(&ctx
);
1827 * Process LSP installation info from two sets of nhlfes: a set from
1828 * a dplane notification, and a set from the zebra LSP object. Update
1829 * counters of installed nexthops, and return whether the LSP has changed.
1831 static bool compare_notif_nhlfes(const struct nhlfe_list_head
*ctx_head
,
1832 struct nhlfe_list_head
*nhlfe_head
,
1833 int *start_counter
, int *end_counter
)
1835 zebra_nhlfe_t
*nhlfe
;
1836 const zebra_nhlfe_t
*ctx_nhlfe
;
1837 struct nexthop
*nexthop
;
1838 const struct nexthop
*ctx_nexthop
;
1839 int start_count
= 0, end_count
= 0;
1840 bool changed_p
= false;
1841 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1843 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1844 char buf
[NEXTHOP_STRLEN
];
1846 nexthop
= nhlfe
->nexthop
;
1850 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1855 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1856 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1860 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1861 nexthop_same(ctx_nexthop
, nexthop
)) {
1868 nexthop2str(nexthop
, buf
, sizeof(buf
));
1870 if (ctx_nhlfe
&& ctx_nexthop
) {
1872 const char *tstr
= "";
1874 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1875 NHLFE_FLAG_INSTALLED
))
1878 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1882 /* Test zebra nhlfe install state */
1883 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1884 NHLFE_FLAG_INSTALLED
)) {
1886 if (!CHECK_FLAG(nhlfe
->flags
,
1887 NHLFE_FLAG_INSTALLED
))
1890 /* Update counter */
1894 if (CHECK_FLAG(nhlfe
->flags
,
1895 NHLFE_FLAG_INSTALLED
))
1900 /* Not mentioned in lfib set -> uninstalled */
1901 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1902 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1903 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1908 zlog_debug("LSP dplane notif: no match, nh %s",
1914 *start_counter
+= start_count
;
1916 *end_counter
+= end_count
;
1922 * Update an lsp nhlfe list from a dplane context, typically an async
1923 * notification context. Update the LSP list to match the installed
1924 * status from the context's list.
1926 static int update_nhlfes_from_ctx(struct nhlfe_list_head
*nhlfe_head
,
1927 const struct nhlfe_list_head
*ctx_head
)
1930 zebra_nhlfe_t
*nhlfe
;
1931 const zebra_nhlfe_t
*ctx_nhlfe
;
1932 struct nexthop
*nexthop
;
1933 const struct nexthop
*ctx_nexthop
;
1934 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1936 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1937 char buf
[NEXTHOP_STRLEN
];
1939 nexthop
= nhlfe
->nexthop
;
1945 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1946 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1950 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1951 nexthop_same(ctx_nexthop
, nexthop
)) {
1958 nexthop2str(nexthop
, buf
, sizeof(buf
));
1960 if (ctx_nhlfe
&& ctx_nexthop
) {
1962 /* Bring zebra nhlfe install state into sync */
1963 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1964 NHLFE_FLAG_INSTALLED
)) {
1966 zlog_debug("%s: matched lsp nhlfe %s (installed)",
1969 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1970 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
1974 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
1977 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1978 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
1981 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
1982 NEXTHOP_FLAG_FIB
)) {
1983 SET_FLAG(nhlfe
->nexthop
->flags
,
1984 NEXTHOP_FLAG_ACTIVE
);
1985 SET_FLAG(nhlfe
->nexthop
->flags
,
1988 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1989 NEXTHOP_FLAG_ACTIVE
);
1990 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1995 /* Not mentioned in lfib set -> uninstalled */
1997 zlog_debug("%s: no match for lsp nhlfe %s",
1999 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2000 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2001 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2002 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2010 * Process async dplane notifications.
2012 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2014 struct zebra_vrf
*zvrf
;
2015 zebra_ile_t tmp_ile
;
2016 struct hash
*lsp_table
;
2018 const struct nhlfe_list_head
*ctx_list
;
2019 int start_count
= 0, end_count
= 0; /* Installed counts */
2020 bool changed_p
= false;
2021 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
2022 enum zebra_sr_policy_update_label_mode update_mode
;
2025 zlog_debug("LSP dplane notif, in-label %u",
2026 dplane_ctx_get_in_label(ctx
));
2028 /* Look for zebra LSP object */
2029 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2033 lsp_table
= zvrf
->lsp_table
;
2035 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
2036 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2039 zlog_debug("dplane LSP notif: in-label %u not found",
2040 dplane_ctx_get_in_label(ctx
));
2045 * The dataplane/forwarding plane is notifying zebra about the state
2046 * of the nexthops associated with this LSP. First, we take a
2047 * pre-scan pass to determine whether the LSP has transitioned
2048 * from installed -> uninstalled. In that case, we need to have
2049 * the existing state of the LSP objects available before making
2052 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2054 changed_p
= compare_notif_nhlfes(ctx_list
, &lsp
->nhlfe_list
,
2055 &start_count
, &end_count
);
2058 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2059 start_count
, end_count
,
2060 changed_p
? ", changed" : "");
2062 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2064 if (compare_notif_nhlfes(ctx_list
, &lsp
->backup_nhlfe_list
,
2065 &start_count
, &end_count
))
2066 /* Avoid accidentally setting back to 'false' */
2070 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2071 start_count
, end_count
,
2072 changed_p
? ", changed" : "");
2075 * Has the LSP become uninstalled? We need the existing state of the
2076 * nexthops/nhlfes at this point so we know what to delete.
2078 if (start_count
> 0 && end_count
== 0) {
2079 /* Inform other lfibs */
2080 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
2084 * Now we take a second pass and bring the zebra
2085 * nexthop state into sync with the forwarding-plane state.
2087 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2088 update_nhlfes_from_ctx(&lsp
->nhlfe_list
, ctx_list
);
2090 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2091 update_nhlfes_from_ctx(&lsp
->backup_nhlfe_list
, ctx_list
);
2093 if (end_count
> 0) {
2094 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2096 /* SR-TE update too */
2097 if (start_count
== 0)
2098 update_mode
= ZEBRA_SR_POLICY_LABEL_CREATED
;
2100 update_mode
= ZEBRA_SR_POLICY_LABEL_UPDATED
;
2101 zebra_sr_policy_label_update(lsp
->ile
.in_label
, update_mode
);
2104 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2107 /* SR-TE update too */
2108 zebra_sr_policy_label_update(lsp
->ile
.in_label
,
2109 ZEBRA_SR_POLICY_LABEL_REMOVED
);
2111 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2112 clear_nhlfe_installed(lsp
);
2116 dplane_ctx_fini(&ctx
);
2120 * Install dynamic LSP entry.
2122 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2123 struct route_entry
*re
)
2125 struct route_table
*table
;
2128 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2132 /* See if there is a configured label binding for this FEC. */
2133 fec
= fec_find(table
, &rn
->p
);
2134 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2137 /* We cannot install a label forwarding entry if local label is the
2138 * implicit-null label.
2140 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2143 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2150 * Uninstall dynamic LSP entry, if any.
2152 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2153 struct route_entry
*re
)
2155 struct route_table
*table
;
2158 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2162 /* See if there is a configured label binding for this FEC. */
2163 fec
= fec_find(table
, &rn
->p
);
2164 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2167 /* Uninstall always removes all dynamic NHLFEs. */
2168 return lsp_uninstall(zvrf
, fec
->label
);
2172 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2173 * the LSP object - nothing is scheduled for processing, for example.
2175 zebra_nhlfe_t
*zebra_mpls_lsp_add_nhlfe(zebra_lsp_t
*lsp
,
2176 enum lsp_types_t lsp_type
,
2177 enum nexthop_types_t gtype
,
2178 const union g_addr
*gate
,
2181 const mpls_label_t
*out_labels
)
2183 /* Just a public pass-through to the internal implementation */
2184 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2185 out_labels
, false /*backup*/);
2189 * Add a backup NHLFE to an LSP, return the newly-added object.
2190 * This path only changes the LSP object - nothing is scheduled for
2191 * processing, for example.
2193 zebra_nhlfe_t
*zebra_mpls_lsp_add_backup_nhlfe(zebra_lsp_t
*lsp
,
2194 enum lsp_types_t lsp_type
,
2195 enum nexthop_types_t gtype
,
2196 const union g_addr
*gate
,
2199 const mpls_label_t
*out_labels
)
2201 /* Just a public pass-through to the internal implementation */
2202 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2207 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2209 zebra_nhlfe_t
*zebra_mpls_lsp_add_nh(zebra_lsp_t
*lsp
,
2210 enum lsp_types_t lsp_type
,
2211 const struct nexthop
*nh
)
2213 zebra_nhlfe_t
*nhlfe
;
2215 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2218 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
, nh
->ifindex
,
2219 nh
->nh_label
->num_labels
, nh
->nh_label
->label
,
2226 * Add a backup NHLFE to an LSP based on a nexthop;
2227 * return the newly-added object.
2229 zebra_nhlfe_t
*zebra_mpls_lsp_add_backup_nh(zebra_lsp_t
*lsp
,
2230 enum lsp_types_t lsp_type
,
2231 const struct nexthop
*nh
)
2233 zebra_nhlfe_t
*nhlfe
;
2235 if (nh
->nh_label
== NULL
|| nh
->nh_label
->num_labels
== 0)
2238 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
,
2239 nh
->ifindex
, nh
->nh_label
->num_labels
,
2240 nh
->nh_label
->label
, true);
2246 * Free an allocated NHLFE
2248 void zebra_mpls_nhlfe_free(zebra_nhlfe_t
*nhlfe
)
2250 /* Just a pass-through to the internal implementation */
2255 * Registration from a client for the label binding for a FEC. If a binding
2256 * already exists, it is informed to the client.
2257 * NOTE: If there is a manually configured label binding, that is used.
2258 * Otherwise, if a label index is specified, it means we have to allocate the
2259 * label from a locally configured label block (SRGB), if one exists and index
2260 * is acceptable. If no label index then just register the specified label.
2261 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2262 * by the calling function. Register requests with both will be rejected.
2264 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2265 uint32_t label
, uint32_t label_index
,
2266 struct zserv
*client
)
2268 struct route_table
*table
;
2271 bool label_change
= false;
2273 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2274 bool is_configured_fec
= false; /* indicate statically configured FEC */
2276 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2280 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2282 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2283 "Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
2284 p
, label
, label_index
,
2285 zebra_route_string(client
->proto
));
2290 fec
= fec_find(table
, p
);
2292 fec
= fec_add(table
, p
, label
, 0, label_index
);
2295 EC_ZEBRA_FEC_ADD_FAILED
,
2296 "Failed to add FEC %pFX upon register, client %s",
2297 p
, zebra_route_string(client
->proto
));
2301 old_label
= MPLS_INVALID_LABEL
;
2304 /* Check if the FEC has been statically defined in the config */
2305 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2306 /* Client may register same FEC with different label index. */
2308 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2309 if (!new_client
&& fec
->label_index
== label_index
2310 && fec
->label
== label
)
2311 /* Duplicate register */
2314 /* Save current label, update the FEC */
2315 old_label
= fec
->label
;
2316 fec
->label_index
= label_index
;
2320 listnode_add(fec
->client_list
, client
);
2322 if (IS_ZEBRA_DEBUG_MPLS
)
2323 zlog_debug("FEC %pFX label%s %u %s by client %s%s", p
,
2324 have_label_index
? " index" : "",
2325 have_label_index
? label_index
: label
,
2326 new_client
? "registered" : "updated",
2327 zebra_route_string(client
->proto
),
2329 ? ", but using statically configured label"
2332 /* If not a statically configured FEC, derive the local label
2333 * from label index or use the provided label
2335 if (!is_configured_fec
) {
2336 if (have_label_index
)
2337 fec_derive_label_from_index(zvrf
, fec
);
2341 /* If no label change, exit. */
2342 if (fec
->label
== old_label
)
2345 label_change
= true;
2348 /* If new client or label change, update client and install or uninstall
2349 * label forwarding entry as needed.
2351 /* Inform client of label, if needed. */
2352 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2353 if (IS_ZEBRA_DEBUG_MPLS
)
2354 zlog_debug("Update client label %u", fec
->label
);
2355 fec_send(fec
, client
);
2358 if (new_client
|| label_change
)
2359 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2365 * Deregistration from a client for the label binding for a FEC. The FEC
2366 * itself is deleted if no other registered clients exist and there is no
2367 * label bound to the FEC.
2369 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2370 struct zserv
*client
)
2372 struct route_table
*table
;
2375 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2379 fec
= fec_find(table
, p
);
2381 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2382 "Failed to find FEC %pFX upon unregister, client %s",
2383 p
, zebra_route_string(client
->proto
));
2387 listnode_delete(fec
->client_list
, client
);
2389 if (IS_ZEBRA_DEBUG_MPLS
)
2390 zlog_debug("FEC %pFX unregistered by client %s", p
,
2391 zebra_route_string(client
->proto
));
2393 /* If not a configured entry, delete the FEC if no other clients. Before
2394 * deleting, see if any LSP needs to be uninstalled.
2396 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2397 && list_isempty(fec
->client_list
)) {
2398 mpls_label_t old_label
= fec
->label
;
2399 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2400 fec_change_update_lsp(zvrf
, fec
, old_label
);
2408 * Cleanup any FECs registered by this client.
2410 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2412 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2413 struct route_node
*rn
;
2415 struct listnode
*node
;
2416 struct zserv
*fec_client
;
2419 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2420 if (zvrf
->fec_table
[af
] == NULL
)
2423 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2424 rn
= route_next(rn
)) {
2426 if (!fec
|| list_isempty(fec
->client_list
))
2429 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2431 if (fec_client
== client
) {
2432 listnode_delete(fec
->client_list
,
2434 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2435 && list_isempty(fec
->client_list
))
2446 struct lsp_uninstall_args
{
2447 struct hash
*lsp_table
;
2448 enum lsp_types_t type
;
2452 * Cleanup MPLS labels registered by this client.
2454 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2457 struct zebra_vrf
*zvrf
;
2459 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2460 struct lsp_uninstall_args args
;
2467 args
.lsp_table
= zvrf
->lsp_table
;
2468 args
.type
= lsp_type_from_re_type(client
->proto
);
2469 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2473 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2474 lsp_type_from_re_type(client
->proto
));
2475 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2476 lsp_type_from_re_type(client
->proto
));
2483 * Return FEC (if any) to which this label is bound.
2484 * Note: Only works for per-prefix binding and when the label is not
2486 * TODO: Currently walks entire table, can optimize later with another
2489 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2492 struct route_node
*rn
;
2496 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2497 if (zvrf
->fec_table
[af
] == NULL
)
2500 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2501 rn
= route_next(rn
)) {
2505 if (fec
->label
== label
)
2514 * Inform if specified label is currently bound to a FEC or not.
2516 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2518 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2522 * Add static FEC to label binding. If there are clients registered for this
2523 * FEC, notify them. If there are labeled routes for this FEC, install the
2524 * label forwarding entry.
2526 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2527 mpls_label_t in_label
)
2529 struct route_table
*table
;
2531 mpls_label_t old_label
;
2534 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2538 /* Update existing FEC or create a new one. */
2539 fec
= fec_find(table
, p
);
2541 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2542 MPLS_INVALID_LABEL_INDEX
);
2544 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2545 "Failed to add FEC %pFX upon config", p
);
2549 if (IS_ZEBRA_DEBUG_MPLS
)
2550 zlog_debug("Add fec %pFX label %u", p
, in_label
);
2552 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2553 if (fec
->label
== in_label
)
2554 /* Duplicate config */
2557 /* Label change, update clients. */
2558 old_label
= fec
->label
;
2559 if (IS_ZEBRA_DEBUG_MPLS
)
2560 zlog_debug("Update fec %pFX new label %u", p
, in_label
);
2562 fec
->label
= in_label
;
2563 fec_update_clients(fec
);
2565 /* Update label forwarding entries appropriately */
2566 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2573 * Remove static FEC to label binding. If there are no clients registered
2574 * for this FEC, delete the FEC; else notify clients
2575 * Note: Upon delete of static binding, if label index exists for this FEC,
2576 * client may need to be updated with derived label.
2578 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2580 struct route_table
*table
;
2582 mpls_label_t old_label
;
2584 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2588 fec
= fec_find(table
, p
);
2590 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2591 "Failed to find FEC %pFX upon delete", p
);
2595 if (IS_ZEBRA_DEBUG_MPLS
) {
2596 zlog_debug("Delete fec %pFX label %u label index %u", p
,
2597 fec
->label
, fec
->label_index
);
2600 old_label
= fec
->label
;
2601 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2602 fec
->label
= MPLS_INVALID_LABEL
;
2604 /* If no client exists, just delete the FEC. */
2605 if (list_isempty(fec
->client_list
)) {
2610 /* Derive the local label (from label index) or reset it. */
2611 fec_derive_label_from_index(zvrf
, fec
);
2613 /* If there is a label change, update clients. */
2614 if (fec
->label
== old_label
)
2616 fec_update_clients(fec
);
2618 /* Update label forwarding entries appropriately */
2619 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2623 * Display MPLS FEC to label binding configuration (VTY command handler).
2625 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2627 struct route_node
*rn
;
2632 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2633 if (zvrf
->fec_table
[af
] == NULL
)
2636 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2637 rn
= route_next(rn
)) {
2644 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2648 vty_out(vty
, "mpls label bind %pFX %s\n", &rn
->p
,
2649 label2str(fec
->label
, lstr
, BUFSIZ
));
2657 * Display MPLS FEC to label binding (VTY command handler).
2659 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2661 struct route_node
*rn
;
2664 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2665 if (zvrf
->fec_table
[af
] == NULL
)
2668 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2669 rn
= route_next(rn
)) {
2672 fec_print(rn
->info
, vty
);
2678 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2680 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2683 struct route_table
*table
;
2684 struct route_node
*rn
;
2686 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2691 rn
= route_node_lookup(table
, p
);
2695 route_unlock_node(rn
);
2699 fec_print(rn
->info
, vty
);
2702 static void mpls_zebra_nhe_update(struct route_entry
*re
, afi_t afi
,
2703 struct nhg_hash_entry
*new_nhe
)
2705 struct nhg_hash_entry
*nhe
;
2707 nhe
= zebra_nhg_rib_find_nhe(new_nhe
, afi
);
2709 route_entry_update_nhe(re
, nhe
);
2712 static bool ftn_update_nexthop(bool add_p
, struct nexthop
*nexthop
,
2713 enum lsp_types_t type
,
2714 const struct zapi_nexthop
*znh
)
2716 if (add_p
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2717 nexthop_add_labels(nexthop
, type
, znh
->label_num
, znh
->labels
);
2718 else if (!add_p
&& nexthop
->nh_label_type
== type
)
2719 nexthop_del_labels(nexthop
);
2726 int mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2727 struct prefix
*prefix
, uint8_t route_type
,
2728 unsigned short route_instance
)
2730 struct route_table
*table
;
2731 struct route_node
*rn
;
2732 struct route_entry
*re
;
2733 struct nexthop
*nexthop
;
2734 struct nhg_hash_entry
*new_nhe
;
2735 afi_t afi
= family2afi(prefix
->family
);
2738 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2742 /* Lookup existing route */
2743 rn
= route_node_get(table
, prefix
);
2744 RNODE_FOREACH_RE (rn
, re
) {
2745 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2747 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2754 * Nexthops are now shared by multiple routes, so we have to make
2755 * a local copy, modify the copy, then update the route.
2757 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2759 for (nexthop
= new_nhe
->nhg
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2760 nexthop_del_labels(nexthop
);
2762 /* Update backup routes/nexthops also, if present. */
2763 if (zebra_nhg_get_backup_nhg(new_nhe
) != NULL
) {
2764 for (nexthop
= new_nhe
->backup_info
->nhe
->nhg
.nexthop
; nexthop
;
2765 nexthop
= nexthop
->next
)
2766 nexthop_del_labels(nexthop
);
2769 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2770 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2772 /* This will create (or ref) a new nhe, so we will discard the local
2775 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
2777 zebra_nhg_free(new_nhe
);
2785 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2786 * update its labels according to 'add_p', and return 'true' if successful.
2788 static bool ftn_update_znh(bool add_p
, enum lsp_types_t type
,
2789 struct nexthop
*head
, const struct zapi_nexthop
*znh
)
2791 bool found
= false, success
= false;
2792 struct nexthop
*nexthop
;
2794 for (nexthop
= head
; nexthop
; nexthop
= nexthop
->next
) {
2795 switch (nexthop
->type
) {
2796 case NEXTHOP_TYPE_IPV4
:
2797 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2798 if (znh
->type
!= NEXTHOP_TYPE_IPV4
2799 && znh
->type
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2801 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
,
2804 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2805 && nexthop
->ifindex
!= znh
->ifindex
)
2810 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2815 case NEXTHOP_TYPE_IPV6
:
2816 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2817 if (znh
->type
!= NEXTHOP_TYPE_IPV6
2818 && znh
->type
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2820 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
,
2823 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2824 && nexthop
->ifindex
!= znh
->ifindex
)
2829 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2845 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2846 * using zapi message info.
2847 * There are several changes that need to be made, in several zebra
2848 * data structures, so we want to do all the work required at once.
2850 int mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
2851 const struct zapi_labels
*zl
)
2853 int i
, counter
, ret
= 0;
2854 char buf
[NEXTHOP_STRLEN
];
2855 const struct zapi_nexthop
*znh
;
2856 struct route_table
*table
;
2857 struct route_node
*rn
= NULL
;
2858 struct route_entry
*re
= NULL
;
2859 struct nhg_hash_entry
*new_nhe
= NULL
;
2862 const struct prefix
*prefix
= NULL
;
2863 struct hash
*lsp_table
;
2864 zebra_ile_t tmp_ile
;
2865 zebra_lsp_t
*lsp
= NULL
;
2867 /* Prep LSP for add case */
2870 lsp_table
= zvrf
->lsp_table
;
2874 /* Find or create LSP object */
2875 tmp_ile
.in_label
= zl
->local_label
;
2876 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2881 /* Prep for route/FEC update if requested */
2882 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2883 prefix
= &zl
->route
.prefix
;
2885 afi
= family2afi(prefix
->family
);
2888 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2890 /* Lookup existing route */
2891 rn
= route_node_get(table
, prefix
);
2892 RNODE_FOREACH_RE(rn
, re
) {
2893 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2895 if (re
->type
== zl
->route
.type
&&
2896 re
->instance
== zl
->route
.instance
)
2903 * Copy over current nexthops into a temporary group.
2904 * We can't just change the values here since the nhgs
2905 * are shared and if the labels change, we'll need
2906 * to find or create a new nhg. We need to create
2907 * a whole temporary group, make changes to it,
2908 * then attach that to the route.
2910 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2914 * The old version of the zapi code
2915 * attempted to manage LSPs before trying to
2916 * find a route/FEC, so we'll continue that way.
2918 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
)
2920 "%s: FTN update requested: no route for prefix %pFX",
2926 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2930 for (i
= 0; i
< zl
->nexthop_num
; i
++) {
2932 znh
= &zl
->nexthops
[i
];
2934 /* Attempt LSP update */
2936 ret
= lsp_znh_install(lsp
, zl
->type
, znh
);
2938 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
2939 zl
->local_label
, znh
->type
,
2940 &znh
->gate
, znh
->ifindex
,
2943 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
) {
2944 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2945 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
2946 __func__
, (add_p
? "" : "un"),
2947 zl
->local_label
, buf
);
2952 /* Attempt route/FEC update if requested */
2956 /* Search the route's nexthops for a match, and update it. */
2957 found
= ftn_update_znh(add_p
, zl
->type
, new_nhe
->nhg
.nexthop
,
2961 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
2962 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2964 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
2965 __func__
, prefix
, zl
->local_label
, buf
);
2970 * Process backup LSPs/nexthop entries also. We associate backup
2971 * LSP info with backup nexthops.
2973 if (!CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
))
2976 for (i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
2978 znh
= &zl
->backup_nexthops
[i
];
2981 ret
= lsp_backup_znh_install(lsp
, zl
->type
, znh
);
2983 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
2985 znh
->type
, &znh
->gate
,
2986 znh
->ifindex
, true);
2989 if (IS_ZEBRA_DEBUG_RECV
||
2990 IS_ZEBRA_DEBUG_MPLS
) {
2991 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
2992 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
2993 __func__
, (add_p
? "" : "un"),
2994 zl
->local_label
, buf
);
2999 /* Attempt backup nexthop/FEC update if requested */
3000 if (re
== NULL
|| zebra_nhg_get_backup_nhg(new_nhe
) == NULL
)
3003 /* Search the route's backup nexthops for a match
3006 found
= ftn_update_znh(add_p
, zl
->type
,
3007 new_nhe
->backup_info
->nhe
->nhg
.nexthop
,
3011 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
3012 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3014 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
3015 __func__
, prefix
, zl
->local_label
, buf
);
3022 * If we made changes, update the route, and schedule it
3023 * for rib processing
3025 if (re
!= NULL
&& counter
> 0) {
3028 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3029 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
3031 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3037 zebra_nhg_free(new_nhe
);
3043 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3044 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3045 * the out-label for an existing NHLFE (update case).
3047 static zebra_nhlfe_t
*
3048 lsp_add_nhlfe(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3049 uint8_t num_out_labels
, const mpls_label_t
*out_labels
,
3050 enum nexthop_types_t gtype
, const union g_addr
*gate
,
3051 ifindex_t ifindex
, bool is_backup
)
3053 zebra_nhlfe_t
*nhlfe
;
3054 char buf
[MPLS_LABEL_STRLEN
];
3055 const char *backup_str
;
3058 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3060 backup_str
= "backup ";
3062 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3068 struct nexthop
*nh
= nhlfe
->nexthop
;
3071 assert(nh
->nh_label
);
3073 /* Clear deleted flag (in case it was set) */
3074 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3075 if (nh
->nh_label
->num_labels
== num_out_labels
3076 && !memcmp(nh
->nh_label
->label
, out_labels
,
3077 sizeof(mpls_label_t
) * num_out_labels
))
3081 if (IS_ZEBRA_DEBUG_MPLS
) {
3082 char buf2
[MPLS_LABEL_STRLEN
];
3083 char buf3
[MPLS_LABEL_STRLEN
];
3085 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3086 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3088 mpls_label2str(nh
->nh_label
->num_labels
,
3089 nh
->nh_label
->label
, buf3
, sizeof(buf3
),
3092 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3093 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3097 /* Update out label(s), trigger processing. */
3098 if (nh
->nh_label
->num_labels
== num_out_labels
)
3099 memcpy(nh
->nh_label
->label
, out_labels
,
3100 sizeof(mpls_label_t
) * num_out_labels
);
3102 nexthop_del_labels(nh
);
3103 nexthop_add_labels(nh
, type
, num_out_labels
,
3107 /* Add LSP entry to this nexthop */
3108 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
,
3109 num_out_labels
, out_labels
, is_backup
);
3113 if (IS_ZEBRA_DEBUG_MPLS
) {
3114 char buf2
[MPLS_LABEL_STRLEN
];
3116 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3117 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3120 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3121 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3125 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
3128 /* Mark NHLFE, queue LSP for processing. */
3129 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3135 * Install an LSP and forwarding entry; used primarily
3136 * from vrf zapi message processing.
3138 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3139 mpls_label_t in_label
, uint8_t num_out_labels
,
3140 const mpls_label_t
*out_labels
, enum nexthop_types_t gtype
,
3141 const union g_addr
*gate
, ifindex_t ifindex
)
3143 struct hash
*lsp_table
;
3144 zebra_ile_t tmp_ile
;
3146 zebra_nhlfe_t
*nhlfe
;
3149 lsp_table
= zvrf
->lsp_table
;
3153 /* Find or create LSP object */
3154 tmp_ile
.in_label
= in_label
;
3155 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
3159 nhlfe
= lsp_add_nhlfe(lsp
, type
, num_out_labels
, out_labels
, gtype
,
3160 gate
, ifindex
, false /*backup*/);
3164 /* Queue LSP for processing. */
3165 if (lsp_processq_add(lsp
))
3172 * Install or replace NHLFE, using info from zapi nexthop
3174 static int lsp_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3175 const struct zapi_nexthop
*znh
)
3177 zebra_nhlfe_t
*nhlfe
;
3179 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
, znh
->labels
,
3180 znh
->type
, &znh
->gate
, znh
->ifindex
,
3185 /* Update backup info if present */
3186 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
3187 if (znh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
3192 nhlfe
->nexthop
->backup_num
= znh
->backup_num
;
3193 memcpy(nhlfe
->nexthop
->backup_idx
, znh
->backup_idx
,
3195 SET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3197 /* Ensure there's no stale backup info */
3198 UNSET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3199 nhlfe
->nexthop
->backup_num
= 0;
3202 /* Queue LSP for processing. */
3203 if (lsp_processq_add(lsp
))
3210 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3212 static int lsp_backup_znh_install(zebra_lsp_t
*lsp
, enum lsp_types_t type
,
3213 const struct zapi_nexthop
*znh
)
3215 zebra_nhlfe_t
*nhlfe
;
3217 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
,
3218 znh
->labels
, znh
->type
, &znh
->gate
,
3219 znh
->ifindex
, true /*backup*/);
3220 if (nhlfe
== NULL
) {
3221 if (IS_ZEBRA_DEBUG_MPLS
)
3222 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3223 __func__
, lsp
->ile
.in_label
);
3227 /* Queue LSP for processing. */
3228 if (lsp_processq_add(lsp
))
3234 zebra_lsp_t
*mpls_lsp_find(struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
3236 struct hash
*lsp_table
;
3237 zebra_ile_t tmp_ile
;
3240 lsp_table
= zvrf
->lsp_table
;
3244 /* If entry is not present, exit. */
3245 tmp_ile
.in_label
= in_label
;
3246 return hash_lookup(lsp_table
, &tmp_ile
);
3250 * Uninstall a particular NHLFE in the forwarding table. If this is
3251 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3253 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3254 mpls_label_t in_label
, enum nexthop_types_t gtype
,
3255 const union g_addr
*gate
, ifindex_t ifindex
,
3258 struct hash
*lsp_table
;
3259 zebra_ile_t tmp_ile
;
3261 zebra_nhlfe_t
*nhlfe
;
3262 char buf
[NEXTHOP_STRLEN
];
3263 bool schedule_lsp
= false;
3266 lsp_table
= zvrf
->lsp_table
;
3270 /* If entry is not present, exit. */
3271 tmp_ile
.in_label
= in_label
;
3272 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3277 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3280 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3285 if (IS_ZEBRA_DEBUG_MPLS
) {
3286 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3287 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3288 in_label
, type
, buf
, nhlfe
->flags
);
3291 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ||
3292 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
3293 schedule_lsp
= true;
3295 /* Mark NHLFE for delete or directly delete, as appropriate. */
3297 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3298 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3300 if (IS_ZEBRA_DEBUG_MPLS
)
3301 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3302 lsp
->ile
.in_label
, lsp
->flags
);
3303 if (lsp_processq_add(lsp
))
3308 /* Free LSP entry if no other NHLFEs and not scheduled. */
3309 lsp_check_free(lsp_table
, &lsp
);
3314 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3315 mpls_label_t in_label
)
3317 struct hash
*lsp_table
;
3318 zebra_ile_t tmp_ile
;
3322 lsp_table
= zvrf
->lsp_table
;
3326 /* If entry is not present, exit. */
3327 tmp_ile
.in_label
= in_label
;
3328 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3332 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
3336 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3337 * If no other NHLFEs exist, the entry would be deleted.
3339 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
3341 struct lsp_uninstall_args
*args
= ctxt
;
3343 struct hash
*lsp_table
;
3345 lsp
= (zebra_lsp_t
*)bucket
->data
;
3346 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
)
3349 lsp_table
= args
->lsp_table
;
3353 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
3357 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3360 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
3361 int afi
, enum lsp_types_t lsp_type
)
3363 struct route_table
*table
;
3364 struct route_node
*rn
;
3365 struct route_entry
*re
;
3366 struct nexthop
*nexthop
;
3367 struct nexthop_group
*nhg
;
3370 /* Process routes of interested address-families. */
3371 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
3375 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
3378 RNODE_FOREACH_RE (rn
, re
) {
3379 struct nhg_hash_entry
*new_nhe
;
3381 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
3383 nhg
= &new_nhe
->nhg
;
3384 for (nexthop
= nhg
->nexthop
; nexthop
;
3385 nexthop
= nexthop
->next
) {
3386 if (nexthop
->nh_label_type
!= lsp_type
)
3389 nexthop_del_labels(nexthop
);
3390 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3391 SET_FLAG(re
->status
,
3392 ROUTE_ENTRY_LABELS_CHANGED
);
3396 /* Check for backup info and update that also */
3397 nhg
= zebra_nhg_get_backup_nhg(new_nhe
);
3399 for (nexthop
= nhg
->nexthop
; nexthop
;
3400 nexthop
= nexthop
->next
) {
3401 if (nexthop
->nh_label_type
!= lsp_type
)
3404 nexthop_del_labels(nexthop
);
3405 SET_FLAG(re
->status
,
3406 ROUTE_ENTRY_CHANGED
);
3407 SET_FLAG(re
->status
,
3408 ROUTE_ENTRY_LABELS_CHANGED
);
3413 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
3414 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3416 zebra_nhg_free(new_nhe
);
3424 #if defined(HAVE_CUMULUS)
3426 * Check that the label values used in LSP creation are consistent. The
3427 * main criteria is that if there is ECMP, the label operation must still
3428 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3429 * to current HW restrictions.
3431 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
3432 mpls_label_t in_label
,
3433 mpls_label_t out_label
,
3434 enum nexthop_types_t gtype
,
3435 union g_addr
*gate
, ifindex_t ifindex
)
3437 struct hash
*slsp_table
;
3438 zebra_ile_t tmp_ile
;
3440 zebra_nhlfe_t
*nhlfe
;
3441 const struct nexthop
*nh
;
3444 slsp_table
= zvrf
->slsp_table
;
3448 /* If entry is not present, exit. */
3449 tmp_ile
.in_label
= in_label
;
3450 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3454 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3455 gtype
, gate
, ifindex
);
3457 nh
= nhlfe
->nexthop
;
3459 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3462 if (nh
->nh_label
->label
[0] == out_label
)
3465 /* If not only NHLFE, cannot allow label change. */
3466 if (nhlfe
!= nhlfe_list_first(&lsp
->nhlfe_list
) ||
3467 nhlfe_list_next(&lsp
->nhlfe_list
, nhlfe
) != NULL
)
3470 /* If other NHLFEs exist, label operation must match. */
3471 nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
);
3472 if (nhlfe
!= NULL
) {
3475 nh
= nhlfe
->nexthop
;
3477 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3480 cur_op
= (nh
->nh_label
->label
[0] ==
3481 MPLS_LABEL_IMPLICIT_NULL
);
3482 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
3483 if (cur_op
!= new_op
)
3488 /* Label values are good. */
3491 #endif /* HAVE_CUMULUS */
3494 * Add static LSP entry. This may be the first entry for this incoming label
3495 * or an additional nexthop; an existing entry may also have outgoing label
3497 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3500 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3501 mpls_label_t out_label
,
3502 enum nexthop_types_t gtype
, union g_addr
*gate
,
3505 struct hash
*slsp_table
;
3506 zebra_ile_t tmp_ile
;
3508 zebra_nhlfe_t
*nhlfe
;
3512 slsp_table
= zvrf
->slsp_table
;
3516 /* Find or create LSP. */
3517 tmp_ile
.in_label
= in_label
;
3518 lsp
= hash_get(slsp_table
, &tmp_ile
, lsp_alloc
);
3522 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3525 struct nexthop
*nh
= nhlfe
->nexthop
;
3528 assert(nh
->nh_label
);
3530 /* Compare existing nexthop */
3531 if (nh
->nh_label
->num_labels
== 1 &&
3532 nh
->nh_label
->label
[0] == out_label
)
3536 if (IS_ZEBRA_DEBUG_MPLS
) {
3537 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3539 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3540 in_label
, buf
, out_label
,
3541 nh
->nh_label
->label
[0]);
3543 if (nh
->nh_label
->num_labels
== 1)
3544 nh
->nh_label
->label
[0] = out_label
;
3546 nexthop_del_labels(nh
);
3547 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
, 1, &out_label
);
3551 /* Add static LSP entry to this nexthop */
3552 nhlfe
= nhlfe_add(lsp
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3553 ifindex
, 1, &out_label
, false /*backup*/);
3557 if (IS_ZEBRA_DEBUG_MPLS
) {
3558 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3560 "Add static LSP in-label %u nexthop %s out-label %u",
3561 in_label
, buf
, out_label
);
3565 /* (Re)Install LSP in the main table. */
3566 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, 1, &out_label
,
3567 gtype
, gate
, ifindex
))
3574 * Delete static LSP entry. This may be the delete of one particular
3575 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3577 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3578 * LSP configuration.
3580 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3581 enum nexthop_types_t gtype
, union g_addr
*gate
,
3584 struct hash
*slsp_table
;
3585 zebra_ile_t tmp_ile
;
3587 zebra_nhlfe_t
*nhlfe
;
3590 slsp_table
= zvrf
->slsp_table
;
3594 /* If entry is not present, exit. */
3595 tmp_ile
.in_label
= in_label
;
3596 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3600 /* Is it delete of entire LSP or a specific NHLFE? */
3601 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3602 if (IS_ZEBRA_DEBUG_MPLS
)
3603 zlog_debug("Del static LSP in-label %u", in_label
);
3605 /* Uninstall entire LSP from the main table. */
3606 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3608 /* Delete all static NHLFEs */
3609 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3613 /* Find specific NHLFE, exit if not found. */
3614 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3615 gtype
, gate
, ifindex
);
3619 if (IS_ZEBRA_DEBUG_MPLS
) {
3621 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3622 zlog_debug("Del static LSP in-label %u nexthop %s",
3626 /* Uninstall LSP from the main table. */
3627 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3628 gate
, ifindex
, false);
3630 /* Delete static LSP NHLFE */
3634 /* Remove entire static LSP entry if no NHLFE - valid in either case
3637 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) {
3638 lsp
= hash_release(slsp_table
, &tmp_ile
);
3639 XFREE(MTYPE_LSP
, lsp
);
3646 * Schedule all MPLS label forwarding entries for processing.
3647 * Called upon changes that may affect one or more of them such as
3648 * interface or nexthop state changes.
3650 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3654 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3658 * Display MPLS label forwarding table for a specific LSP
3659 * (VTY command handler).
3661 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3662 mpls_label_t label
, bool use_json
)
3664 struct hash
*lsp_table
;
3666 zebra_ile_t tmp_ile
;
3667 json_object
*json
= NULL
;
3670 lsp_table
= zvrf
->lsp_table
;
3674 /* If entry is not present, exit. */
3675 tmp_ile
.in_label
= label
;
3676 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3681 json
= lsp_json(lsp
);
3682 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3683 json
, JSON_C_TO_STRING_PRETTY
));
3684 json_object_free(json
);
3686 lsp_print(vty
, lsp
);
3690 * Display MPLS label forwarding table (VTY command handler).
3692 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3696 json_object
*json
= NULL
;
3697 zebra_lsp_t
*lsp
= NULL
;
3698 zebra_nhlfe_t
*nhlfe
= NULL
;
3699 struct listnode
*node
= NULL
;
3700 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3703 json
= json_object_new_object();
3705 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3706 json_object_object_add(
3707 json
, label2str(lsp
->ile
.in_label
, buf
,
3711 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3712 json
, JSON_C_TO_STRING_PRETTY
));
3713 json_object_free(json
);
3717 /* Prepare table. */
3718 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3719 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3720 tt
->style
.cell
.rpad
= 2;
3721 tt
->style
.corner
= '+';
3723 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3725 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3726 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3727 struct nexthop
*nexthop
;
3728 const char *out_label_str
;
3729 char nh_buf
[NEXTHOP_STRLEN
];
3731 nexthop
= nhlfe
->nexthop
;
3733 switch (nexthop
->type
) {
3734 case NEXTHOP_TYPE_IFINDEX
: {
3735 struct zebra_ns
*zns
;
3736 struct interface
*ifp
;
3738 zns
= zebra_ns_lookup(NS_DEFAULT
);
3739 ifp
= if_lookup_by_index_per_ns(
3740 zns
, nexthop
->ifindex
);
3741 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3742 ifp
? ifp
->name
: "Null");
3745 case NEXTHOP_TYPE_IPV4
:
3746 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3747 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3748 nh_buf
, sizeof(nh_buf
));
3750 case NEXTHOP_TYPE_IPV6
:
3751 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3752 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3753 nh_buf
, sizeof(nh_buf
));
3759 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3760 out_label_str
= mpls_label2str(
3761 nexthop
->nh_label
->num_labels
,
3762 &nexthop
->nh_label
->label
[0],
3763 buf
, sizeof(buf
), 1);
3765 out_label_str
= "-";
3767 ttable_add_row(tt
, "%u|%s|%s|%s",
3769 nhlfe_type2str(nhlfe
->type
),
3770 nh_buf
, out_label_str
);
3774 /* Dump the generated table. */
3775 if (tt
->nrows
> 1) {
3776 char *table
= ttable_dump(tt
, "\n");
3777 vty_out(vty
, "%s\n", table
);
3778 XFREE(MTYPE_TMP
, table
);
3783 list_delete(&lsp_list
);
3787 * Create printable string for static LSP configuration.
3789 static char *nhlfe_config_str(const zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
3791 const struct nexthop
*nh
;
3793 nh
= nhlfe
->nexthop
;
3797 case NEXTHOP_TYPE_IPV4
:
3798 inet_ntop(AF_INET
, &nh
->gate
.ipv4
, buf
, size
);
3800 case NEXTHOP_TYPE_IPV6
:
3801 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3802 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, size
);
3805 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3816 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3818 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3821 zebra_nhlfe_t
*nhlfe
;
3823 struct listnode
*node
;
3824 struct list
*slsp_list
=
3825 hash_get_sorted_list(zvrf
->slsp_table
, lsp_cmp
);
3827 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, lsp
)) {
3828 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3832 nh
= nhlfe
->nexthop
;
3833 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3836 nhlfe_config_str(nhlfe
, buf
, sizeof(buf
));
3838 switch (nh
->nh_label
->label
[0]) {
3839 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3840 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3841 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3843 case MPLS_LABEL_IMPLICIT_NULL
:
3844 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3847 snprintf(lstr
, sizeof(lstr
), "%u",
3848 nh
->nh_label
->label
[0]);
3852 vty_out(vty
, "mpls lsp %u %s %s\n", lsp
->ile
.in_label
,
3857 list_delete(&slsp_list
);
3858 return (zvrf
->slsp_table
->count
? 1 : 0);
3862 * Add/update global label block.
3864 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3867 zvrf
->mpls_srgb
.start_label
= start_label
;
3868 zvrf
->mpls_srgb
.end_label
= end_label
;
3870 /* Evaluate registered FECs to see if any get a label or not. */
3876 * Delete global label block.
3878 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3880 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3881 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3883 /* Process registered FECs to clear their local label, if needed. */
3889 * Display MPLS global label block configuration (VTY command handler).
3891 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3893 if (zvrf
->mpls_srgb
.start_label
== 0)
3896 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3897 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3898 vty_out(vty
, "mpls label global-block %u %u\n",
3899 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3906 * Called when VRF becomes inactive, cleans up information but keeps
3909 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3911 struct zebra_vrf
*def_zvrf
;
3914 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3915 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3918 * For other vrfs, we try to remove associated LSPs; we locate
3919 * the LSPs in the default vrf.
3921 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3923 /* At shutdown, the default may be gone already */
3924 if (def_zvrf
== NULL
)
3927 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3928 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3929 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3935 * Called upon process exiting, need to delete LSP forwarding
3936 * entries from the kernel.
3937 * NOTE: Currently supported only for default VRF.
3939 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
3941 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3942 hash_clean(zvrf
->lsp_table
, NULL
);
3943 hash_free(zvrf
->lsp_table
);
3944 hash_clean(zvrf
->slsp_table
, NULL
);
3945 hash_free(zvrf
->slsp_table
);
3946 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
3947 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
3951 * Allocate MPLS tables for this VRF and do other initialization.
3952 * NOTE: Currently supported only for default VRF.
3954 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
3961 snprintf(buffer
, sizeof(buffer
), "ZEBRA SLSP table: %s",
3963 zvrf
->slsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
3965 snprintf(buffer
, sizeof(buffer
), "ZEBRA LSP table: %s",
3967 zvrf
->lsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
3968 zvrf
->fec_table
[AFI_IP
] = route_table_init();
3969 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
3970 zvrf
->mpls_flags
= 0;
3971 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3972 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3976 * Global MPLS initialization.
3978 void zebra_mpls_init(void)
3981 mpls_pw_reach_strict
= false;
3983 if (mpls_kernel_init() < 0) {
3984 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
3985 "Disabling MPLS support (no kernel support)");
3989 if (!mpls_processq_init())
3992 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
3993 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);