1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2013 Cumulus Networks, Inc.
14 #include "sockunion.h"
17 #include "workqueue.h"
22 #include "termtable.h"
25 #include "zebra/rib.h"
27 #include "zebra/interface.h"
28 #include "zebra/zserv.h"
29 #include "zebra/zebra_router.h"
30 #include "zebra/redistribute.h"
31 #include "zebra/debug.h"
32 #include "zebra/zebra_vrf.h"
33 #include "zebra/zebra_mpls.h"
34 #include "zebra/zebra_srte.h"
35 #include "zebra/zebra_errors.h"
37 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object");
38 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object");
39 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object");
42 bool mpls_pw_reach_strict
; /* Strict reachability checking */
44 /* static function declarations */
46 static void fec_evaluate(struct zebra_vrf
*zvrf
);
47 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
48 struct zebra_fec
*fec
);
49 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
50 struct route_node
*rn
, struct route_entry
*re
);
51 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
52 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, struct zebra_fec
*fec
,
53 mpls_label_t old_label
);
54 static int fec_send(struct zebra_fec
*fec
, struct zserv
*client
);
55 static void fec_update_clients(struct zebra_fec
*fec
);
56 static void fec_print(struct zebra_fec
*fec
, struct vty
*vty
);
57 static struct zebra_fec
*fec_find(struct route_table
*table
, struct prefix
*p
);
58 static struct zebra_fec
*fec_add(struct route_table
*table
, struct prefix
*p
,
59 mpls_label_t label
, uint32_t flags
,
60 uint32_t label_index
);
61 static int fec_del(struct zebra_fec
*fec
);
63 static unsigned int label_hash(const void *p
);
64 static bool label_cmp(const void *p1
, const void *p2
);
65 static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe
*nhlfe
,
66 struct nexthop
*nexthop
);
67 static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe
*nhlfe
,
68 struct nexthop
*nexthop
);
69 static int nhlfe_nexthop_active(struct zebra_nhlfe
*nhlfe
);
71 static void lsp_select_best_nhlfe(struct zebra_lsp
*lsp
);
72 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
73 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
74 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
75 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
76 static void lsp_processq_complete(struct work_queue
*wq
);
77 static int lsp_processq_add(struct zebra_lsp
*lsp
);
78 static void *lsp_alloc(void *p
);
80 /* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
81 static void lsp_check_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
);
83 /* Free lsp; sets caller's pointer to NULL */
84 static void lsp_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
);
86 static char *nhlfe2str(const struct zebra_nhlfe
*nhlfe
, char *buf
, int size
);
87 static char *nhlfe_config_str(const struct zebra_nhlfe
*nhlfe
, char *buf
,
89 static int nhlfe_nhop_match(struct zebra_nhlfe
*nhlfe
,
90 enum nexthop_types_t gtype
,
91 const union g_addr
*gate
, ifindex_t ifindex
);
92 static struct zebra_nhlfe
*nhlfe_find(struct nhlfe_list_head
*list
,
93 enum lsp_types_t lsp_type
,
94 enum nexthop_types_t gtype
,
95 const union g_addr
*gate
,
97 static struct zebra_nhlfe
*
98 nhlfe_add(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
99 enum nexthop_types_t gtype
, const union g_addr
*gate
,
100 ifindex_t ifindex
, vrf_id_t vrf_id
, uint8_t num_labels
,
101 const mpls_label_t
*labels
, bool is_backup
);
102 static int nhlfe_del(struct zebra_nhlfe
*nhlfe
);
103 static void nhlfe_free(struct zebra_nhlfe
*nhlfe
);
104 static void nhlfe_out_label_update(struct zebra_nhlfe
*nhlfe
,
105 struct mpls_label_stack
*nh_label
);
106 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, struct zebra_lsp
*lsp
,
107 enum lsp_types_t type
);
108 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
109 mpls_label_t in_label
);
110 static void nhlfe_print(struct zebra_nhlfe
*nhlfe
, struct vty
*vty
,
112 static void lsp_print(struct vty
*vty
, struct zebra_lsp
*lsp
);
113 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
114 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
115 int afi
, enum lsp_types_t lsp_type
);
116 static int lsp_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
117 const struct zapi_nexthop
*znh
);
118 static int lsp_backup_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
119 const struct zapi_nexthop
*znh
);
121 /* Static functions */
124 * Handle failure in LSP install, clear flags for NHLFE.
126 static void clear_nhlfe_installed(struct zebra_lsp
*lsp
)
128 struct zebra_nhlfe
*nhlfe
;
129 struct nexthop
*nexthop
;
131 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
132 nexthop
= nhlfe
->nexthop
;
136 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
137 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
140 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
141 nexthop
= nhlfe
->nexthop
;
145 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
146 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
151 * Install label forwarding entry based on labeled-route entry.
153 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
154 struct route_node
*rn
, struct route_entry
*re
)
156 struct hash
*lsp_table
;
157 struct zebra_ile tmp_ile
;
158 struct zebra_lsp
*lsp
;
159 struct zebra_nhlfe
*nhlfe
;
160 struct nexthop
*nexthop
;
161 enum lsp_types_t lsp_type
;
166 lsp_table
= zvrf
->lsp_table
;
170 lsp_type
= lsp_type_from_re_type(re
->type
);
173 /* Locate or allocate LSP entry. */
174 tmp_ile
.in_label
= label
;
175 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
177 /* For each active nexthop, create NHLFE. Note that we deliberately skip
178 * recursive nexthops right now, because intermediate hops won't
180 * the label advertised by the recursive nexthop (plus we don't have the
181 * logic yet to push multiple labels).
183 for (nexthop
= re
->nhe
->nhg
.nexthop
;
184 nexthop
; nexthop
= nexthop
->next
) {
185 /* Skip inactive and recursive entries. */
186 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
188 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
191 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, lsp_type
,
192 nexthop
->type
, &nexthop
->gate
,
195 /* Clear deleted flag (in case it was set) */
196 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
197 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
202 if (IS_ZEBRA_DEBUG_MPLS
) {
203 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
205 "LSP in-label %u type %d nexthop %s out-label changed",
206 lsp
->ile
.in_label
, lsp_type
, buf
);
209 /* Update out label, trigger processing. */
210 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
211 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
214 /* Add LSP entry to this nexthop */
216 lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
217 nexthop
->ifindex
, nexthop
->vrf_id
,
218 nexthop
->nh_label
->num_labels
,
219 nexthop
->nh_label
->label
, false /*backup*/);
223 if (IS_ZEBRA_DEBUG_MPLS
) {
224 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
226 "Add LSP in-label %u type %d nexthop %s out-label %u",
227 lsp
->ile
.in_label
, lsp_type
, buf
,
228 nexthop
->nh_label
->label
[0]);
231 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
233 /* Mark NHLFE as changed. */
234 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
239 /* Queue LSP for processing if necessary. If no NHLFE got added (special
240 * case), delete the LSP entry; this case results in somewhat ugly
243 if (added
|| changed
) {
244 if (lsp_processq_add(lsp
))
247 lsp_check_free(lsp_table
, &lsp
);
254 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
255 * NHLFEs are removed, the entire entry is deleted.
257 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
259 struct hash
*lsp_table
;
260 struct zebra_ile tmp_ile
;
261 struct zebra_lsp
*lsp
;
262 struct zebra_nhlfe
*nhlfe
;
266 lsp_table
= zvrf
->lsp_table
;
270 /* If entry is not present, exit. */
271 tmp_ile
.in_label
= label
;
272 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
273 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
276 /* Mark NHLFEs for delete or directly delete, as appropriate. */
277 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
279 /* Skip static NHLFEs */
280 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
283 if (IS_ZEBRA_DEBUG_MPLS
) {
284 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
286 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
287 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
290 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
291 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
292 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
298 /* Queue LSP for processing, if needed, else delete. */
299 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
300 if (lsp_processq_add(lsp
))
303 lsp_check_free(lsp_table
, &lsp
);
310 * This function is invoked upon change to label block configuration; it
311 * will walk all registered FECs with label-index and appropriately update
312 * their local labels and trigger client updates.
314 static void fec_evaluate(struct zebra_vrf
*zvrf
)
316 struct route_node
*rn
;
317 struct zebra_fec
*fec
;
318 uint32_t old_label
, new_label
;
321 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
322 if (zvrf
->fec_table
[af
] == NULL
)
325 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
326 rn
= route_next(rn
)) {
327 if ((fec
= rn
->info
) == NULL
)
330 /* Skip configured FECs and those without a label index.
332 if (fec
->flags
& FEC_FLAG_CONFIGURED
333 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
336 /* Save old label, determine new label. */
337 old_label
= fec
->label
;
339 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
340 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
341 new_label
= MPLS_INVALID_LABEL
;
343 /* If label has changed, update FEC and clients. */
344 if (new_label
== old_label
)
347 if (IS_ZEBRA_DEBUG_MPLS
)
349 "Update fec %pRN new label %u upon label block",
352 fec
->label
= new_label
;
353 fec_update_clients(fec
);
355 /* Update label forwarding entries appropriately */
356 fec_change_update_lsp(zvrf
, fec
, old_label
);
362 * Derive (if possible) and update the local label for the FEC based on
363 * its label index. The index is "acceptable" if it falls within the
364 * globally configured label block (SRGB).
366 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
367 struct zebra_fec
*fec
)
371 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
372 && zvrf
->mpls_srgb
.start_label
373 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
374 < zvrf
->mpls_srgb
.end_label
))
377 fec
->label
= MPLS_INVALID_LABEL
;
383 * There is a change for this FEC. Install or uninstall label forwarding
384 * entries, as appropriate.
386 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, struct zebra_fec
*fec
,
387 mpls_label_t old_label
)
389 struct route_table
*table
;
390 struct route_node
*rn
;
391 struct route_entry
*re
;
394 /* Uninstall label forwarding entry, if previously installed. */
395 if (old_label
!= MPLS_INVALID_LABEL
396 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
397 lsp_uninstall(zvrf
, old_label
);
399 /* Install label forwarding entry corr. to new label, if needed. */
400 if (fec
->label
== MPLS_INVALID_LABEL
401 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
404 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
405 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
409 /* See if labeled route exists. */
410 rn
= route_node_lookup(table
, &fec
->rn
->p
);
414 RNODE_FOREACH_RE (rn
, re
) {
415 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
419 if (!re
|| !zebra_rib_labeled_unicast(re
))
422 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
429 * Inform about FEC to a registered client.
431 static int fec_send(struct zebra_fec
*fec
, struct zserv
*client
)
434 struct route_node
*rn
;
438 /* Get output stream. */
439 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
441 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
443 stream_putw(s
, rn
->p
.family
);
444 stream_put_prefix(s
, &rn
->p
);
445 stream_putl(s
, fec
->label
);
446 stream_putw_at(s
, 0, stream_get_endp(s
));
447 return zserv_send_message(client
, s
);
451 * Update all registered clients about this FEC. Caller should've updated
452 * FEC and ensure no duplicate updates.
454 static void fec_update_clients(struct zebra_fec
*fec
)
456 struct listnode
*node
;
457 struct zserv
*client
;
459 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
460 if (IS_ZEBRA_DEBUG_MPLS
)
461 zlog_debug("Update client %s",
462 zebra_route_string(client
->proto
));
463 fec_send(fec
, client
);
469 * Print a FEC-label binding entry.
471 static void fec_print(struct zebra_fec
*fec
, struct vty
*vty
)
473 struct route_node
*rn
;
474 struct listnode
*node
;
475 struct zserv
*client
;
479 vty_out(vty
, "%pRN\n", rn
);
480 vty_out(vty
, " Label: %s", label2str(fec
->label
, 0, buf
, BUFSIZ
));
481 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
482 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
484 if (!list_isempty(fec
->client_list
)) {
485 vty_out(vty
, " Client list:");
486 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
487 vty_out(vty
, " %s(fd %d)",
488 zebra_route_string(client
->proto
),
495 * Locate FEC-label binding that matches with passed info.
497 static struct zebra_fec
*fec_find(struct route_table
*table
, struct prefix
*p
)
499 struct route_node
*rn
;
502 rn
= route_node_lookup(table
, p
);
506 route_unlock_node(rn
);
511 * Add a FEC. This may be upon a client registering for a binding
512 * or when a binding is configured.
514 static struct zebra_fec
*fec_add(struct route_table
*table
, struct prefix
*p
,
515 mpls_label_t label
, uint32_t flags
,
516 uint32_t label_index
)
518 struct route_node
*rn
;
519 struct zebra_fec
*fec
;
523 /* Lookup (or add) route node.*/
524 rn
= route_node_get(table
, p
);
531 fec
= XCALLOC(MTYPE_FEC
, sizeof(struct zebra_fec
));
536 fec
->client_list
= list_new();
538 route_unlock_node(rn
); /* for the route_node_get */
540 fec
->label_index
= label_index
;
547 * Delete a FEC. This may be upon the last client deregistering for
548 * a FEC and no binding exists or when the binding is deleted and there
549 * are no registered clients.
551 static int fec_del(struct zebra_fec
*fec
)
553 list_delete(&fec
->client_list
);
554 fec
->rn
->info
= NULL
;
555 route_unlock_node(fec
->rn
);
556 XFREE(MTYPE_FEC
, fec
);
561 * Hash function for label.
563 static unsigned int label_hash(const void *p
)
565 const struct zebra_ile
*ile
= p
;
567 return (jhash_1word(ile
->in_label
, 0));
571 * Compare 2 LSP hash entries based on in-label.
573 static bool label_cmp(const void *p1
, const void *p2
)
575 const struct zebra_ile
*ile1
= p1
;
576 const struct zebra_ile
*ile2
= p2
;
578 return (ile1
->in_label
== ile2
->in_label
);
582 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
584 * NOTE: Looking only for connected routes right now.
586 static int nhlfe_nexthop_active_ipv4(struct zebra_nhlfe
*nhlfe
,
587 struct nexthop
*nexthop
)
589 struct route_table
*table
;
590 struct prefix_ipv4 p
;
591 struct route_node
*rn
;
592 struct route_entry
*match
;
593 struct nexthop
*match_nh
;
595 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
599 /* Lookup nexthop in IPv4 routing table. */
600 memset(&p
, 0, sizeof(p
));
602 p
.prefixlen
= IPV4_MAX_BITLEN
;
603 p
.prefix
= nexthop
->gate
.ipv4
;
605 rn
= route_node_match(table
, (struct prefix
*)&p
);
609 route_unlock_node(rn
);
611 /* Locate a valid connected route. */
612 RNODE_FOREACH_RE (rn
, match
) {
613 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
614 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
617 for (match_nh
= match
->nhe
->nhg
.nexthop
; match_nh
;
618 match_nh
= match_nh
->next
) {
619 if (match
->type
== ZEBRA_ROUTE_CONNECT
620 || nexthop
->ifindex
== match_nh
->ifindex
) {
621 nexthop
->ifindex
= match_nh
->ifindex
;
632 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
634 * NOTE: Looking only for connected routes right now.
636 static int nhlfe_nexthop_active_ipv6(struct zebra_nhlfe
*nhlfe
,
637 struct nexthop
*nexthop
)
639 struct route_table
*table
;
640 struct prefix_ipv6 p
;
641 struct route_node
*rn
;
642 struct route_entry
*match
;
644 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
648 /* Lookup nexthop in IPv6 routing table. */
649 memset(&p
, 0, sizeof(p
));
651 p
.prefixlen
= IPV6_MAX_BITLEN
;
652 p
.prefix
= nexthop
->gate
.ipv6
;
654 rn
= route_node_match(table
, (struct prefix
*)&p
);
658 route_unlock_node(rn
);
660 /* Locate a valid connected route. */
661 RNODE_FOREACH_RE (rn
, match
) {
662 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
663 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
664 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
668 if (!match
|| !match
->nhe
->nhg
.nexthop
)
671 nexthop
->ifindex
= match
->nhe
->nhg
.nexthop
->ifindex
;
677 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
679 * NOTE: Each NHLFE points to only 1 nexthop.
681 static int nhlfe_nexthop_active(struct zebra_nhlfe
*nhlfe
)
683 struct nexthop
*nexthop
;
684 struct interface
*ifp
;
685 struct zebra_ns
*zns
;
687 nexthop
= nhlfe
->nexthop
;
688 if (!nexthop
) // unexpected
691 /* Check on nexthop based on type. */
692 switch (nexthop
->type
) {
693 case NEXTHOP_TYPE_IFINDEX
:
695 * Lookup if this type is special. The
696 * NEXTHOP_TYPE_IFINDEX is a pop and
697 * forward into a different table for
698 * processing. As such this ifindex
699 * passed to us may be a VRF device
700 * which will not be in the default
701 * VRF. So let's look in all of them
703 zns
= zebra_ns_lookup(NS_DEFAULT
);
704 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
705 if (ifp
&& if_is_operative(ifp
))
706 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
708 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
710 case NEXTHOP_TYPE_IPV4
:
711 case NEXTHOP_TYPE_IPV4_IFINDEX
:
712 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
713 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
715 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
718 case NEXTHOP_TYPE_IPV6
:
719 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
720 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
722 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
725 case NEXTHOP_TYPE_IPV6_IFINDEX
:
726 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
727 ifp
= if_lookup_by_index(nexthop
->ifindex
,
729 if (ifp
&& if_is_operative(ifp
))
730 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
732 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
734 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
735 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
741 case NEXTHOP_TYPE_BLACKHOLE
:
745 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
749 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
750 * reachability and select the best. Multipath entries are also
751 * marked. This is invoked when an LSP scheduled for processing (due
752 * to some change) is examined.
754 static void lsp_select_best_nhlfe(struct zebra_lsp
*lsp
)
756 struct zebra_nhlfe
*nhlfe
;
757 struct zebra_nhlfe
*best
;
758 struct nexthop
*nexthop
;
766 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
769 * First compute the best path, after checking nexthop status. We are
770 * only concerned with non-deleted NHLFEs.
772 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
773 /* Clear selection flags. */
774 UNSET_FLAG(nhlfe
->flags
,
775 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
777 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
778 && nhlfe_nexthop_active(nhlfe
)) {
779 if (!best
|| (nhlfe
->distance
< best
->distance
))
784 lsp
->best_nhlfe
= best
;
785 if (!lsp
->best_nhlfe
)
789 * Check the active status of backup nhlfes also
791 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
792 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
793 (void)nhlfe_nexthop_active(nhlfe
);
796 /* Mark best NHLFE as selected. */
797 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
800 * If best path exists, see if there is ECMP. While doing this, note if
802 * new (uninstalled) NHLFE has been selected, an installed entry that is
803 * still selected has a change or an installed entry is to be removed.
805 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
806 int nh_chg
, nh_sel
, nh_inst
;
808 nexthop
= nhlfe
->nexthop
;
809 if (!nexthop
) // unexpected
812 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
813 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
814 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
815 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
816 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
820 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
821 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
822 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
824 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
826 if ((nh_sel
&& !nh_inst
)
827 || (nh_sel
&& nh_inst
&& nh_chg
)
828 || (nh_inst
&& !nh_sel
))
832 /* We have finished examining, clear changed flag. */
833 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
837 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
841 * Delete LSP forwarding entry from kernel, if installed. Called upon
844 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
846 struct zebra_lsp
*lsp
;
848 lsp
= (struct zebra_lsp
*)bucket
->data
;
849 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
850 (void)dplane_lsp_delete(lsp
);
854 * Schedule LSP forwarding entry for processing. Called upon changes
855 * that may impact LSPs such as nexthop / connected route changes.
857 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
859 struct zebra_lsp
*lsp
;
861 lsp
= (struct zebra_lsp
*)bucket
->data
;
863 /* In the common flow, this is used when external events occur. For
864 * LSPs with backup nhlfes, we'll assume that the forwarding
865 * plane will use the backups to handle these events, until the
866 * owning protocol can react.
869 /* Skip LSPs with backups */
870 if (nhlfe_list_first(&lsp
->backup_nhlfe_list
) != NULL
) {
871 if (IS_ZEBRA_DEBUG_MPLS_DETAIL
)
872 zlog_debug("%s: skip LSP in-label %u",
873 __func__
, lsp
->ile
.in_label
);
878 (void)lsp_processq_add(lsp
);
882 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
883 * any multipaths and update or delete from the kernel, as needed.
885 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
887 struct zebra_lsp
*lsp
;
888 struct zebra_nhlfe
*oldbest
, *newbest
;
889 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
890 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
891 enum zebra_dplane_result res
;
893 lsp
= (struct zebra_lsp
*)data
;
894 if (!lsp
) // unexpected
897 oldbest
= lsp
->best_nhlfe
;
899 /* Select best NHLFE(s) */
900 lsp_select_best_nhlfe(lsp
);
902 newbest
= lsp
->best_nhlfe
;
904 if (IS_ZEBRA_DEBUG_MPLS
) {
906 nhlfe2str(oldbest
, buf
, sizeof(buf
));
908 nhlfe2str(newbest
, buf2
, sizeof(buf2
));
910 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
911 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
912 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
915 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
916 /* Not already installed */
919 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
921 switch (dplane_lsp_add(lsp
)) {
922 case ZEBRA_DPLANE_REQUEST_QUEUED
:
923 /* Set 'installed' flag so we will know
924 * that an install is in-flight.
926 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
928 zvrf
->lsp_installs_queued
++;
930 case ZEBRA_DPLANE_REQUEST_FAILURE
:
931 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
932 "LSP Install Failure: %u",
935 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
936 zvrf
->lsp_installs
++;
941 /* Installed, may need an update and/or delete. */
943 res
= dplane_lsp_delete(lsp
);
945 /* We do some of the lsp cleanup immediately for
948 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
949 clear_nhlfe_installed(lsp
);
952 case ZEBRA_DPLANE_REQUEST_QUEUED
:
953 zvrf
->lsp_removals_queued
++;
955 case ZEBRA_DPLANE_REQUEST_FAILURE
:
956 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
957 "LSP Deletion Failure: %u",
960 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
961 zvrf
->lsp_removals
++;
964 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
965 struct zebra_nhlfe
*nhlfe
;
966 struct nexthop
*nexthop
;
968 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
970 /* We leave the INSTALLED flag set here
971 * so we know an update is in-flight.
975 * Any NHLFE that was installed but is not
976 * selected now needs to have its flags updated.
978 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
979 nexthop
= nhlfe
->nexthop
;
983 if (CHECK_FLAG(nhlfe
->flags
,
984 NHLFE_FLAG_INSTALLED
)
985 && !CHECK_FLAG(nhlfe
->flags
,
986 NHLFE_FLAG_SELECTED
)) {
987 UNSET_FLAG(nhlfe
->flags
,
988 NHLFE_FLAG_INSTALLED
);
989 UNSET_FLAG(nexthop
->flags
,
994 switch (dplane_lsp_update(lsp
)) {
995 case ZEBRA_DPLANE_REQUEST_QUEUED
:
996 zvrf
->lsp_installs_queued
++;
998 case ZEBRA_DPLANE_REQUEST_FAILURE
:
999 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1000 "LSP Update Failure: %u",
1003 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1004 zvrf
->lsp_installs
++;
1015 * Callback upon processing completion of a LSP forwarding entry.
1017 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1019 struct zebra_vrf
*zvrf
;
1020 struct zebra_lsp
*lsp
;
1021 struct hash
*lsp_table
;
1022 struct zebra_nhlfe
*nhlfe
;
1024 /* If zebra is shutting down, don't delete any structs,
1025 * just ignore this callback. The LSPs will be cleaned up
1026 * during the shutdown processing.
1028 if (zebra_router_in_shutdown())
1031 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
1034 lsp_table
= zvrf
->lsp_table
;
1035 if (!lsp_table
) // unexpected
1038 lsp
= (struct zebra_lsp
*)data
;
1039 if (!lsp
) // unexpected
1042 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1044 * delete LSP entry also.
1046 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1048 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1049 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1053 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1054 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1058 lsp_check_free(lsp_table
, &lsp
);
1062 * Callback upon finishing the processing of all scheduled
1063 * LSP forwarding entries.
1065 static void lsp_processq_complete(struct work_queue
*wq
)
1067 /* Nothing to do for now. */
1071 * Add LSP forwarding entry to queue for subsequent processing.
1073 static int lsp_processq_add(struct zebra_lsp
*lsp
)
1075 /* If already scheduled, exit. */
1076 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1079 if (zrouter
.lsp_process_q
== NULL
) {
1080 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1081 "%s: work_queue does not exist!", __func__
);
1085 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1086 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1091 * Callback to allocate LSP forwarding table entry.
1093 static void *lsp_alloc(void *p
)
1095 const struct zebra_ile
*ile
= p
;
1096 struct zebra_lsp
*lsp
;
1098 lsp
= XCALLOC(MTYPE_LSP
, sizeof(struct zebra_lsp
));
1100 nhlfe_list_init(&lsp
->nhlfe_list
);
1101 nhlfe_list_init(&lsp
->backup_nhlfe_list
);
1103 if (IS_ZEBRA_DEBUG_MPLS
)
1104 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1106 return ((void *)lsp
);
1110 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1112 static void lsp_check_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
)
1114 struct zebra_lsp
*lsp
;
1116 if (plsp
== NULL
|| *plsp
== NULL
)
1121 if ((nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) &&
1122 (nhlfe_list_first(&lsp
->backup_nhlfe_list
) == NULL
) &&
1123 !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1124 lsp_free(lsp_table
, plsp
);
1127 static void lsp_free_nhlfe(struct zebra_lsp
*lsp
)
1129 struct zebra_nhlfe
*nhlfe
;
1131 while ((nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
))) {
1132 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1136 while ((nhlfe
= nhlfe_list_first(&lsp
->backup_nhlfe_list
))) {
1137 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1143 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1146 static void lsp_free(struct hash
*lsp_table
, struct zebra_lsp
**plsp
)
1148 struct zebra_lsp
*lsp
;
1150 if (plsp
== NULL
|| *plsp
== NULL
)
1155 if (IS_ZEBRA_DEBUG_MPLS
)
1156 zlog_debug("Free LSP in-label %u flags 0x%x",
1157 lsp
->ile
.in_label
, lsp
->flags
);
1159 lsp_free_nhlfe(lsp
);
1161 hash_release(lsp_table
, &lsp
->ile
);
1162 XFREE(MTYPE_LSP
, lsp
);
1168 * Create printable string for NHLFE entry.
1170 static char *nhlfe2str(const struct zebra_nhlfe
*nhlfe
, char *buf
, int size
)
1172 const struct nexthop
*nexthop
;
1175 nexthop
= nhlfe
->nexthop
;
1176 switch (nexthop
->type
) {
1177 case NEXTHOP_TYPE_IPV4
:
1178 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1179 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1181 case NEXTHOP_TYPE_IPV6
:
1182 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1183 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1185 case NEXTHOP_TYPE_IFINDEX
:
1186 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1187 case NEXTHOP_TYPE_BLACKHOLE
:
1195 * Check if NHLFE matches with search info passed.
1197 static int nhlfe_nhop_match(struct zebra_nhlfe
*nhlfe
,
1198 enum nexthop_types_t gtype
,
1199 const union g_addr
*gate
, ifindex_t ifindex
)
1201 struct nexthop
*nhop
;
1204 nhop
= nhlfe
->nexthop
;
1208 if (nhop
->type
!= gtype
)
1211 switch (nhop
->type
) {
1212 case NEXTHOP_TYPE_IPV4
:
1213 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1214 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1215 sizeof(struct in_addr
));
1216 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1217 cmp
= !(nhop
->ifindex
== ifindex
);
1219 case NEXTHOP_TYPE_IPV6
:
1220 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1221 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1222 sizeof(struct in6_addr
));
1223 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1224 cmp
= !(nhop
->ifindex
== ifindex
);
1226 case NEXTHOP_TYPE_IFINDEX
:
1227 cmp
= !(nhop
->ifindex
== ifindex
);
1229 case NEXTHOP_TYPE_BLACKHOLE
:
1238 * Locate NHLFE that matches with passed info.
1239 * TODO: handle vrf_id if vrf backend is netns based
1241 static struct zebra_nhlfe
*nhlfe_find(struct nhlfe_list_head
*list
,
1242 enum lsp_types_t lsp_type
,
1243 enum nexthop_types_t gtype
,
1244 const union g_addr
*gate
,
1247 struct zebra_nhlfe
*nhlfe
;
1249 frr_each_safe(nhlfe_list
, list
, nhlfe
) {
1250 if (nhlfe
->type
!= lsp_type
)
1252 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1260 * Allocate and init new NHLFE.
1262 static struct zebra_nhlfe
*
1263 nhlfe_alloc(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
1264 enum nexthop_types_t gtype
, const union g_addr
*gate
,
1265 ifindex_t ifindex
, vrf_id_t vrf_id
, uint8_t num_labels
,
1266 const mpls_label_t
*labels
)
1268 struct zebra_nhlfe
*nhlfe
;
1269 struct nexthop
*nexthop
;
1273 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(struct zebra_nhlfe
));
1276 nhlfe
->type
= lsp_type
;
1277 nhlfe
->distance
= lsp_distance(lsp_type
);
1279 nexthop
= nexthop_new();
1281 nexthop_add_labels(nexthop
, lsp_type
, num_labels
, labels
);
1283 nexthop
->vrf_id
= vrf_id
;
1284 nexthop
->type
= gtype
;
1285 switch (nexthop
->type
) {
1286 case NEXTHOP_TYPE_IPV4
:
1287 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1288 nexthop
->gate
.ipv4
= gate
->ipv4
;
1290 nexthop
->ifindex
= ifindex
;
1292 case NEXTHOP_TYPE_IPV6
:
1293 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1294 nexthop
->gate
.ipv6
= gate
->ipv6
;
1296 nexthop
->ifindex
= ifindex
;
1298 case NEXTHOP_TYPE_IFINDEX
:
1299 nexthop
->ifindex
= ifindex
;
1301 case NEXTHOP_TYPE_BLACKHOLE
:
1302 if (IS_ZEBRA_DEBUG_MPLS
)
1303 zlog_debug("%s: invalid: blackhole nexthop", __func__
);
1305 nexthop_free(nexthop
);
1306 XFREE(MTYPE_NHLFE
, nhlfe
);
1309 nhlfe
->nexthop
= nexthop
;
1315 * Add primary or backup NHLFE. Base entry must have been created and
1316 * duplicate check done.
1318 static struct zebra_nhlfe
*
1319 nhlfe_add(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
1320 enum nexthop_types_t gtype
, const union g_addr
*gate
,
1321 ifindex_t ifindex
, vrf_id_t vrf_id
, uint8_t num_labels
,
1322 const mpls_label_t
*labels
, bool is_backup
)
1324 struct zebra_nhlfe
*nhlfe
;
1329 /* Allocate new object */
1330 nhlfe
= nhlfe_alloc(lsp
, lsp_type
, gtype
, gate
, ifindex
, vrf_id
,
1331 num_labels
, labels
);
1336 /* Enqueue to LSP: primaries at head of list, backups at tail */
1338 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
);
1339 nhlfe_list_add_tail(&lsp
->backup_nhlfe_list
, nhlfe
);
1341 nhlfe_list_add_head(&lsp
->nhlfe_list
, nhlfe
);
1347 * Common delete for NHLFEs.
1349 static void nhlfe_free(struct zebra_nhlfe
*nhlfe
)
1356 nexthop_free(nhlfe
->nexthop
);
1358 nhlfe
->nexthop
= NULL
;
1360 XFREE(MTYPE_NHLFE
, nhlfe
);
1365 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1367 static int nhlfe_del(struct zebra_nhlfe
*nhlfe
)
1369 struct zebra_lsp
*lsp
;
1378 if (nhlfe
== lsp
->best_nhlfe
)
1379 lsp
->best_nhlfe
= NULL
;
1381 /* Unlink from LSP */
1382 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
))
1383 nhlfe_list_del(&lsp
->backup_nhlfe_list
, nhlfe
);
1385 nhlfe_list_del(&lsp
->nhlfe_list
, nhlfe
);
1395 * Update label for NHLFE entry.
1397 static void nhlfe_out_label_update(struct zebra_nhlfe
*nhlfe
,
1398 struct mpls_label_stack
*nh_label
)
1400 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1403 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, struct zebra_lsp
*lsp
,
1404 enum lsp_types_t type
)
1406 struct zebra_nhlfe
*nhlfe
;
1407 int schedule_lsp
= 0;
1410 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1413 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1414 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1415 /* Skip non-static NHLFEs */
1416 if (nhlfe
->type
!= type
)
1419 if (IS_ZEBRA_DEBUG_MPLS
) {
1420 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1422 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1423 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1426 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1427 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1428 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1435 frr_each_safe(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1436 /* Skip non-static NHLFEs */
1437 if (nhlfe
->type
!= type
)
1440 if (IS_ZEBRA_DEBUG_MPLS
) {
1441 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
1443 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1444 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1447 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1448 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1449 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1456 /* Queue LSP for processing, if needed, else delete. */
1458 if (IS_ZEBRA_DEBUG_MPLS
) {
1459 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1460 lsp
->ile
.in_label
, lsp
->flags
);
1462 if (lsp_processq_add(lsp
))
1465 lsp_check_free(lsp_table
, &lsp
);
1472 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1473 * If no other NHLFEs exist, the entry would be deleted.
1475 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1476 mpls_label_t in_label
)
1478 struct hash
*lsp_table
;
1479 struct zebra_ile tmp_ile
;
1480 struct zebra_lsp
*lsp
;
1483 lsp_table
= zvrf
->lsp_table
;
1487 /* If entry is not present, exit. */
1488 tmp_ile
.in_label
= in_label
;
1489 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1490 if (!lsp
|| (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
))
1493 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1496 static json_object
*nhlfe_json(struct zebra_nhlfe
*nhlfe
)
1498 json_object
*json_nhlfe
= NULL
;
1499 json_object
*json_backups
= NULL
;
1500 json_object
*json_label_stack
;
1501 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1504 json_nhlfe
= json_object_new_object();
1505 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1506 if (nexthop
->nh_label
) {
1507 json_object_int_add(json_nhlfe
, "outLabel",
1508 nexthop
->nh_label
->label
[0]);
1509 json_label_stack
= json_object_new_array();
1510 json_object_object_add(json_nhlfe
, "outLabelStack",
1512 for (i
= 0; i
< nexthop
->nh_label
->num_labels
; i
++)
1513 json_object_array_add(
1515 json_object_new_int(
1516 nexthop
->nh_label
->label
[i
]));
1518 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1520 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1521 json_object_boolean_true_add(json_nhlfe
, "installed");
1523 switch (nexthop
->type
) {
1524 case NEXTHOP_TYPE_IPV4
:
1525 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1526 json_object_string_addf(json_nhlfe
, "nexthop", "%pI4",
1527 &nexthop
->gate
.ipv4
);
1528 if (nexthop
->ifindex
)
1529 json_object_string_add(json_nhlfe
, "interface",
1530 ifindex2ifname(nexthop
->ifindex
,
1533 case NEXTHOP_TYPE_IPV6
:
1534 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1535 json_object_string_addf(json_nhlfe
, "nexthop", "%pI6",
1536 &nexthop
->gate
.ipv6
);
1538 if (nexthop
->ifindex
)
1539 json_object_string_add(json_nhlfe
, "interface",
1540 ifindex2ifname(nexthop
->ifindex
,
1543 case NEXTHOP_TYPE_IFINDEX
:
1544 if (nexthop
->ifindex
)
1545 json_object_string_add(json_nhlfe
, "interface",
1546 ifindex2ifname(nexthop
->ifindex
,
1549 case NEXTHOP_TYPE_BLACKHOLE
:
1553 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
1554 json_backups
= json_object_new_array();
1555 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
1556 json_object_array_add(
1558 json_object_new_int(nexthop
->backup_idx
[i
]));
1561 json_object_object_add(json_nhlfe
, "backupIndex",
1569 * Print the NHLFE for a LSP forwarding entry.
1571 static void nhlfe_print(struct zebra_nhlfe
*nhlfe
, struct vty
*vty
,
1574 struct nexthop
*nexthop
;
1575 char buf
[MPLS_LABEL_STRLEN
];
1577 nexthop
= nhlfe
->nexthop
;
1578 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1581 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1582 nhlfe_type2str(nhlfe
->type
),
1583 mpls_label2str(nexthop
->nh_label
->num_labels
,
1584 nexthop
->nh_label
->label
, buf
, sizeof(buf
),
1585 nexthop
->nh_label_type
, 0),
1589 vty_out(vty
, "%s", indent
);
1591 switch (nexthop
->type
) {
1592 case NEXTHOP_TYPE_IPV4
:
1593 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1594 vty_out(vty
, " via %pI4", &nexthop
->gate
.ipv4
);
1595 if (nexthop
->ifindex
)
1596 vty_out(vty
, " dev %s",
1597 ifindex2ifname(nexthop
->ifindex
,
1600 case NEXTHOP_TYPE_IPV6
:
1601 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1602 vty_out(vty
, " via %s",
1603 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
1605 if (nexthop
->ifindex
)
1606 vty_out(vty
, " dev %s",
1607 ifindex2ifname(nexthop
->ifindex
,
1610 case NEXTHOP_TYPE_IFINDEX
:
1611 if (nexthop
->ifindex
)
1612 vty_out(vty
, " dev %s",
1613 ifindex2ifname(nexthop
->ifindex
,
1616 case NEXTHOP_TYPE_BLACKHOLE
:
1620 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_IS_BACKUP
) ? " (backup)"
1623 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1629 * Print an LSP forwarding entry.
1631 static void lsp_print(struct vty
*vty
, struct zebra_lsp
*lsp
)
1633 struct zebra_nhlfe
*nhlfe
, *backup
;
1636 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1637 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1640 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1641 nhlfe_print(nhlfe
, vty
, NULL
);
1643 if (nhlfe
->nexthop
== NULL
||
1644 !CHECK_FLAG(nhlfe
->nexthop
->flags
,
1645 NEXTHOP_FLAG_HAS_BACKUP
))
1648 /* Backup nhlfes: find backups in backup list */
1650 for (j
= 0; j
< nhlfe
->nexthop
->backup_num
; j
++) {
1653 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, backup
) {
1654 if (i
== nhlfe
->nexthop
->backup_idx
[j
])
1660 vty_out(vty
, " [backup %d]", i
);
1661 nhlfe_print(backup
, vty
, " ");
1668 * JSON objects for an LSP forwarding entry.
1670 static json_object
*lsp_json(struct zebra_lsp
*lsp
)
1672 struct zebra_nhlfe
*nhlfe
= NULL
;
1673 json_object
*json
= json_object_new_object();
1674 json_object
*json_nhlfe_list
= json_object_new_array();
1676 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1678 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1679 json_object_boolean_true_add(json
, "installed");
1681 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
)
1682 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1684 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1685 json_nhlfe_list
= NULL
;
1688 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
1689 if (json_nhlfe_list
== NULL
)
1690 json_nhlfe_list
= json_object_new_array();
1692 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1695 if (json_nhlfe_list
)
1696 json_object_object_add(json
, "backupNexthops", json_nhlfe_list
);
1702 /* Return a sorted linked list of the hash contents */
1703 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1706 struct hash_bucket
*hb
;
1707 struct list
*sorted_list
= list_new();
1709 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1711 for (i
= 0; i
< hash
->size
; i
++)
1712 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1713 listnode_add_sort(sorted_list
, hb
->data
);
1719 * Compare two LSPs based on their label values.
1721 static int lsp_cmp(const struct zebra_lsp
*lsp1
, const struct zebra_lsp
*lsp2
)
1723 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1726 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1733 * Initialize work queue for processing changed LSPs.
1735 static void mpls_processq_init(void)
1737 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1739 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1740 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1741 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1742 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1743 zrouter
.lsp_process_q
->spec
.hold
= 10;
1748 * Process LSP update results from zebra dataplane.
1750 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1752 struct zebra_vrf
*zvrf
;
1754 struct zebra_ile tmp_ile
;
1755 struct hash
*lsp_table
;
1756 struct zebra_lsp
*lsp
;
1757 struct zebra_nhlfe
*nhlfe
;
1758 struct nexthop
*nexthop
;
1759 enum dplane_op_e op
;
1760 enum zebra_dplane_result status
;
1761 enum zebra_sr_policy_update_label_mode update_mode
;
1763 op
= dplane_ctx_get_op(ctx
);
1764 status
= dplane_ctx_get_status(ctx
);
1766 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1767 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1768 ctx
, dplane_op2str(op
),
1769 dplane_ctx_get_in_label(ctx
),
1770 dplane_res2str(status
));
1772 label
= dplane_ctx_get_in_label(ctx
);
1775 case DPLANE_OP_LSP_INSTALL
:
1776 case DPLANE_OP_LSP_UPDATE
:
1777 /* Look for zebra LSP object */
1778 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
1782 lsp_table
= zvrf
->lsp_table
;
1784 tmp_ile
.in_label
= label
;
1785 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1787 if (IS_ZEBRA_DEBUG_DPLANE
)
1788 zlog_debug("LSP ctx %p: in-label %u not found",
1789 ctx
, dplane_ctx_get_in_label(ctx
));
1793 /* TODO -- Confirm that this result is still 'current' */
1795 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1796 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1797 clear_nhlfe_installed(lsp
);
1798 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1799 "LSP Install Failure: in-label %u",
1804 /* Update zebra object */
1805 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1806 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
1807 nexthop
= nhlfe
->nexthop
;
1811 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
) &&
1812 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
1813 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1814 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1818 update_mode
= (op
== DPLANE_OP_LSP_INSTALL
)
1819 ? ZEBRA_SR_POLICY_LABEL_CREATED
1820 : ZEBRA_SR_POLICY_LABEL_UPDATED
;
1821 zebra_sr_policy_label_update(label
, update_mode
);
1824 case DPLANE_OP_LSP_DELETE
:
1825 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1826 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1827 "LSP Deletion Failure: in-label %u",
1828 dplane_ctx_get_in_label(ctx
));
1831 zebra_sr_policy_label_update(label
,
1832 ZEBRA_SR_POLICY_LABEL_REMOVED
);
1835 case DPLANE_OP_LSP_NOTIFY
:
1836 case DPLANE_OP_NONE
:
1837 case DPLANE_OP_ROUTE_INSTALL
:
1838 case DPLANE_OP_ROUTE_UPDATE
:
1839 case DPLANE_OP_ROUTE_DELETE
:
1840 case DPLANE_OP_ROUTE_NOTIFY
:
1841 case DPLANE_OP_NH_INSTALL
:
1842 case DPLANE_OP_NH_UPDATE
:
1843 case DPLANE_OP_NH_DELETE
:
1844 case DPLANE_OP_PW_INSTALL
:
1845 case DPLANE_OP_PW_UNINSTALL
:
1846 case DPLANE_OP_SYS_ROUTE_ADD
:
1847 case DPLANE_OP_SYS_ROUTE_DELETE
:
1848 case DPLANE_OP_ADDR_INSTALL
:
1849 case DPLANE_OP_ADDR_UNINSTALL
:
1850 case DPLANE_OP_MAC_INSTALL
:
1851 case DPLANE_OP_MAC_DELETE
:
1852 case DPLANE_OP_NEIGH_INSTALL
:
1853 case DPLANE_OP_NEIGH_UPDATE
:
1854 case DPLANE_OP_NEIGH_DELETE
:
1855 case DPLANE_OP_VTEP_ADD
:
1856 case DPLANE_OP_VTEP_DELETE
:
1857 case DPLANE_OP_RULE_ADD
:
1858 case DPLANE_OP_RULE_DELETE
:
1859 case DPLANE_OP_RULE_UPDATE
:
1860 case DPLANE_OP_NEIGH_DISCOVER
:
1861 case DPLANE_OP_BR_PORT_UPDATE
:
1862 case DPLANE_OP_IPTABLE_ADD
:
1863 case DPLANE_OP_IPTABLE_DELETE
:
1864 case DPLANE_OP_IPSET_ADD
:
1865 case DPLANE_OP_IPSET_DELETE
:
1866 case DPLANE_OP_IPSET_ENTRY_ADD
:
1867 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1868 case DPLANE_OP_NEIGH_IP_INSTALL
:
1869 case DPLANE_OP_NEIGH_IP_DELETE
:
1870 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1871 case DPLANE_OP_GRE_SET
:
1872 case DPLANE_OP_INTF_ADDR_ADD
:
1873 case DPLANE_OP_INTF_ADDR_DEL
:
1874 case DPLANE_OP_INTF_NETCONFIG
:
1875 case DPLANE_OP_INTF_INSTALL
:
1876 case DPLANE_OP_INTF_UPDATE
:
1877 case DPLANE_OP_INTF_DELETE
:
1878 case DPLANE_OP_TC_QDISC_INSTALL
:
1879 case DPLANE_OP_TC_QDISC_UNINSTALL
:
1880 case DPLANE_OP_TC_CLASS_ADD
:
1881 case DPLANE_OP_TC_CLASS_DELETE
:
1882 case DPLANE_OP_TC_CLASS_UPDATE
:
1883 case DPLANE_OP_TC_FILTER_ADD
:
1884 case DPLANE_OP_TC_FILTER_DELETE
:
1885 case DPLANE_OP_TC_FILTER_UPDATE
:
1892 * Process LSP installation info from two sets of nhlfes: a set from
1893 * a dplane notification, and a set from the zebra LSP object. Update
1894 * counters of installed nexthops, and return whether the LSP has changed.
1896 static bool compare_notif_nhlfes(const struct nhlfe_list_head
*ctx_head
,
1897 struct nhlfe_list_head
*nhlfe_head
,
1898 int *start_counter
, int *end_counter
)
1900 struct zebra_nhlfe
*nhlfe
;
1901 const struct zebra_nhlfe
*ctx_nhlfe
;
1902 struct nexthop
*nexthop
;
1903 const struct nexthop
*ctx_nexthop
;
1904 int start_count
= 0, end_count
= 0;
1905 bool changed_p
= false;
1906 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1908 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
1909 char buf
[NEXTHOP_STRLEN
];
1911 nexthop
= nhlfe
->nexthop
;
1915 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1920 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
1921 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1925 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1926 nexthop_same(ctx_nexthop
, nexthop
)) {
1933 nexthop2str(nexthop
, buf
, sizeof(buf
));
1935 if (ctx_nhlfe
&& ctx_nexthop
) {
1937 const char *tstr
= "";
1939 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1940 NHLFE_FLAG_INSTALLED
))
1943 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1947 /* Test zebra nhlfe install state */
1948 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1949 NHLFE_FLAG_INSTALLED
)) {
1951 if (!CHECK_FLAG(nhlfe
->flags
,
1952 NHLFE_FLAG_INSTALLED
))
1955 /* Update counter */
1959 if (CHECK_FLAG(nhlfe
->flags
,
1960 NHLFE_FLAG_INSTALLED
))
1965 /* Not mentioned in lfib set -> uninstalled */
1966 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1967 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1968 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1973 zlog_debug("LSP dplane notif: no match, nh %s",
1979 *start_counter
+= start_count
;
1981 *end_counter
+= end_count
;
1987 * Update an lsp nhlfe list from a dplane context, typically an async
1988 * notification context. Update the LSP list to match the installed
1989 * status from the context's list.
1991 static int update_nhlfes_from_ctx(struct nhlfe_list_head
*nhlfe_head
,
1992 const struct nhlfe_list_head
*ctx_head
)
1995 struct zebra_nhlfe
*nhlfe
;
1996 const struct zebra_nhlfe
*ctx_nhlfe
;
1997 struct nexthop
*nexthop
;
1998 const struct nexthop
*ctx_nexthop
;
1999 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
2001 frr_each_safe(nhlfe_list
, nhlfe_head
, nhlfe
) {
2002 char buf
[NEXTHOP_STRLEN
];
2004 nexthop
= nhlfe
->nexthop
;
2010 frr_each(nhlfe_list_const
, ctx_head
, ctx_nhlfe
) {
2011 ctx_nexthop
= ctx_nhlfe
->nexthop
;
2015 if ((ctx_nexthop
->type
== nexthop
->type
) &&
2016 nexthop_same(ctx_nexthop
, nexthop
)) {
2023 nexthop2str(nexthop
, buf
, sizeof(buf
));
2025 if (ctx_nhlfe
&& ctx_nexthop
) {
2027 /* Bring zebra nhlfe install state into sync */
2028 if (CHECK_FLAG(ctx_nhlfe
->flags
,
2029 NHLFE_FLAG_INSTALLED
)) {
2031 zlog_debug("%s: matched lsp nhlfe %s (installed)",
2034 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2035 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2039 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
2042 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2043 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2046 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
2047 NEXTHOP_FLAG_FIB
)) {
2048 SET_FLAG(nhlfe
->nexthop
->flags
,
2049 NEXTHOP_FLAG_ACTIVE
);
2050 SET_FLAG(nhlfe
->nexthop
->flags
,
2053 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2054 NEXTHOP_FLAG_ACTIVE
);
2055 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2060 /* Not mentioned in lfib set -> uninstalled */
2062 zlog_debug("%s: no match for lsp nhlfe %s",
2064 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2065 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
2066 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2067 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2075 * Process async dplane notifications.
2077 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2079 struct zebra_vrf
*zvrf
;
2080 struct zebra_ile tmp_ile
;
2081 struct hash
*lsp_table
;
2082 struct zebra_lsp
*lsp
;
2083 const struct nhlfe_list_head
*ctx_list
;
2084 int start_count
= 0, end_count
= 0; /* Installed counts */
2085 bool changed_p
= false;
2086 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
2087 enum zebra_sr_policy_update_label_mode update_mode
;
2090 zlog_debug("LSP dplane notif, in-label %u",
2091 dplane_ctx_get_in_label(ctx
));
2093 /* Look for zebra LSP object */
2094 zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2098 lsp_table
= zvrf
->lsp_table
;
2100 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
2101 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2104 zlog_debug("dplane LSP notif: in-label %u not found",
2105 dplane_ctx_get_in_label(ctx
));
2110 * The dataplane/forwarding plane is notifying zebra about the state
2111 * of the nexthops associated with this LSP. First, we take a
2112 * pre-scan pass to determine whether the LSP has transitioned
2113 * from installed -> uninstalled. In that case, we need to have
2114 * the existing state of the LSP objects available before making
2117 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2119 changed_p
= compare_notif_nhlfes(ctx_list
, &lsp
->nhlfe_list
,
2120 &start_count
, &end_count
);
2123 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2124 start_count
, end_count
,
2125 changed_p
? ", changed" : "");
2127 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2129 if (compare_notif_nhlfes(ctx_list
, &lsp
->backup_nhlfe_list
,
2130 &start_count
, &end_count
))
2131 /* Avoid accidentally setting back to 'false' */
2135 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2136 start_count
, end_count
,
2137 changed_p
? ", changed" : "");
2140 * Has the LSP become uninstalled? We need the existing state of the
2141 * nexthops/nhlfes at this point so we know what to delete.
2143 if (start_count
> 0 && end_count
== 0) {
2144 /* Inform other lfibs */
2145 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
2149 * Now we take a second pass and bring the zebra
2150 * nexthop state into sync with the forwarding-plane state.
2152 ctx_list
= dplane_ctx_get_nhlfe_list(ctx
);
2153 update_nhlfes_from_ctx(&lsp
->nhlfe_list
, ctx_list
);
2155 ctx_list
= dplane_ctx_get_backup_nhlfe_list(ctx
);
2156 update_nhlfes_from_ctx(&lsp
->backup_nhlfe_list
, ctx_list
);
2158 if (end_count
> 0) {
2159 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2161 /* SR-TE update too */
2162 if (start_count
== 0)
2163 update_mode
= ZEBRA_SR_POLICY_LABEL_CREATED
;
2165 update_mode
= ZEBRA_SR_POLICY_LABEL_UPDATED
;
2166 zebra_sr_policy_label_update(lsp
->ile
.in_label
, update_mode
);
2169 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2172 /* SR-TE update too */
2173 zebra_sr_policy_label_update(lsp
->ile
.in_label
,
2174 ZEBRA_SR_POLICY_LABEL_REMOVED
);
2176 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2177 clear_nhlfe_installed(lsp
);
2182 * Install dynamic LSP entry.
2184 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2185 struct route_entry
*re
)
2187 struct route_table
*table
;
2188 struct zebra_fec
*fec
;
2190 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2194 /* See if there is a configured label binding for this FEC. */
2195 fec
= fec_find(table
, &rn
->p
);
2196 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2199 /* We cannot install a label forwarding entry if local label is the
2200 * implicit-null label.
2202 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2205 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2212 * Uninstall dynamic LSP entry, if any.
2214 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2215 struct route_entry
*re
)
2217 struct route_table
*table
;
2218 struct zebra_fec
*fec
;
2220 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2224 /* See if there is a configured label binding for this FEC. */
2225 fec
= fec_find(table
, &rn
->p
);
2226 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2229 /* Uninstall always removes all dynamic NHLFEs. */
2230 return lsp_uninstall(zvrf
, fec
->label
);
2234 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2235 * the LSP object - nothing is scheduled for processing, for example.
2237 struct zebra_nhlfe
*
2238 zebra_mpls_lsp_add_nhlfe(struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
2239 enum nexthop_types_t gtype
, const union g_addr
*gate
,
2240 ifindex_t ifindex
, uint8_t num_labels
,
2241 const mpls_label_t
*out_labels
)
2243 /* Just a public pass-through to the internal implementation */
2244 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, VRF_DEFAULT
,
2245 num_labels
, out_labels
, false /*backup*/);
2249 * Add a backup NHLFE to an LSP, return the newly-added object.
2250 * This path only changes the LSP object - nothing is scheduled for
2251 * processing, for example.
2253 struct zebra_nhlfe
*zebra_mpls_lsp_add_backup_nhlfe(
2254 struct zebra_lsp
*lsp
, enum lsp_types_t lsp_type
,
2255 enum nexthop_types_t gtype
, const union g_addr
*gate
, ifindex_t ifindex
,
2256 uint8_t num_labels
, const mpls_label_t
*out_labels
)
2258 /* Just a public pass-through to the internal implementation */
2259 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, VRF_DEFAULT
,
2260 num_labels
, out_labels
, true);
2264 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2266 struct zebra_nhlfe
*zebra_mpls_lsp_add_nh(struct zebra_lsp
*lsp
,
2267 enum lsp_types_t lsp_type
,
2268 const struct nexthop
*nh
)
2270 struct zebra_nhlfe
*nhlfe
;
2273 lsp
, lsp_type
, nh
->type
, &nh
->gate
, nh
->ifindex
, nh
->vrf_id
,
2274 nh
->nh_label
? nh
->nh_label
->num_labels
: 0,
2275 nh
->nh_label
? nh
->nh_label
->label
: NULL
, false /*backup*/);
2281 * Add a backup NHLFE to an LSP based on a nexthop;
2282 * return the newly-added object.
2284 struct zebra_nhlfe
*zebra_mpls_lsp_add_backup_nh(struct zebra_lsp
*lsp
,
2285 enum lsp_types_t lsp_type
,
2286 const struct nexthop
*nh
)
2288 struct zebra_nhlfe
*nhlfe
;
2290 nhlfe
= nhlfe_add(lsp
, lsp_type
, nh
->type
, &nh
->gate
, nh
->ifindex
,
2292 nh
->nh_label
? nh
->nh_label
->num_labels
: 0,
2293 nh
->nh_label
? nh
->nh_label
->label
: NULL
, true);
2299 * Free an allocated NHLFE
2301 void zebra_mpls_nhlfe_free(struct zebra_nhlfe
*nhlfe
)
2303 /* Just a pass-through to the internal implementation */
2308 * Registration from a client for the label binding for a FEC. If a binding
2309 * already exists, it is informed to the client.
2310 * NOTE: If there is a manually configured label binding, that is used.
2311 * Otherwise, if a label index is specified, it means we have to allocate the
2312 * label from a locally configured label block (SRGB), if one exists and index
2313 * is acceptable. If no label index then just register the specified label.
2314 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2315 * by the calling function. Register requests with both will be rejected.
2317 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2318 uint32_t label
, uint32_t label_index
,
2319 struct zserv
*client
)
2321 struct route_table
*table
;
2322 struct zebra_fec
*fec
;
2324 bool label_change
= false;
2326 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2327 bool is_configured_fec
= false; /* indicate statically configured FEC */
2329 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2333 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2335 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2336 "Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
2337 p
, label
, label_index
,
2338 zebra_route_string(client
->proto
));
2343 fec
= fec_find(table
, p
);
2345 fec
= fec_add(table
, p
, label
, 0, label_index
);
2348 EC_ZEBRA_FEC_ADD_FAILED
,
2349 "Failed to add FEC %pFX upon register, client %s",
2350 p
, zebra_route_string(client
->proto
));
2354 old_label
= MPLS_INVALID_LABEL
;
2357 /* Check if the FEC has been statically defined in the config */
2358 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2359 /* Client may register same FEC with different label index. */
2361 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2362 if (!new_client
&& fec
->label_index
== label_index
2363 && fec
->label
== label
)
2364 /* Duplicate register */
2367 /* Save current label, update the FEC */
2368 old_label
= fec
->label
;
2369 fec
->label_index
= label_index
;
2373 listnode_add(fec
->client_list
, client
);
2375 if (IS_ZEBRA_DEBUG_MPLS
)
2376 zlog_debug("FEC %pFX label%s %u %s by client %s%s", p
,
2377 have_label_index
? " index" : "",
2378 have_label_index
? label_index
: label
,
2379 new_client
? "registered" : "updated",
2380 zebra_route_string(client
->proto
),
2382 ? ", but using statically configured label"
2385 /* If not a statically configured FEC, derive the local label
2386 * from label index or use the provided label
2388 if (!is_configured_fec
) {
2389 if (have_label_index
)
2390 fec_derive_label_from_index(zvrf
, fec
);
2394 /* If no label change, exit. */
2395 if (fec
->label
== old_label
)
2398 label_change
= true;
2401 /* If new client or label change, update client and install or uninstall
2402 * label forwarding entry as needed.
2404 /* Inform client of label, if needed. */
2405 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2406 if (IS_ZEBRA_DEBUG_MPLS
)
2407 zlog_debug("Update client label %u", fec
->label
);
2408 fec_send(fec
, client
);
2411 if (new_client
|| label_change
)
2412 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2418 * Deregistration from a client for the label binding for a FEC. The FEC
2419 * itself is deleted if no other registered clients exist and there is no
2420 * label bound to the FEC.
2422 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2423 struct zserv
*client
)
2425 struct route_table
*table
;
2426 struct zebra_fec
*fec
;
2428 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2432 fec
= fec_find(table
, p
);
2434 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2435 "Failed to find FEC %pFX upon unregister, client %s",
2436 p
, zebra_route_string(client
->proto
));
2440 listnode_delete(fec
->client_list
, client
);
2442 if (IS_ZEBRA_DEBUG_MPLS
)
2443 zlog_debug("FEC %pFX unregistered by client %s", p
,
2444 zebra_route_string(client
->proto
));
2446 /* If not a configured entry, delete the FEC if no other clients. Before
2447 * deleting, see if any LSP needs to be uninstalled.
2449 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2450 && list_isempty(fec
->client_list
)) {
2451 mpls_label_t old_label
= fec
->label
;
2452 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2453 fec_change_update_lsp(zvrf
, fec
, old_label
);
2461 * Cleanup any FECs registered by this client.
2463 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2465 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
2466 struct route_node
*rn
;
2467 struct zebra_fec
*fec
;
2468 struct listnode
*node
;
2469 struct zserv
*fec_client
;
2472 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2473 if (zvrf
->fec_table
[af
] == NULL
)
2476 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2477 rn
= route_next(rn
)) {
2479 if (!fec
|| list_isempty(fec
->client_list
))
2482 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2484 if (fec_client
== client
) {
2485 listnode_delete(fec
->client_list
,
2487 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2488 && list_isempty(fec
->client_list
))
2499 struct lsp_uninstall_args
{
2500 struct hash
*lsp_table
;
2501 enum lsp_types_t type
;
2505 * Cleanup MPLS labels registered by this client.
2507 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2510 struct zebra_vrf
*zvrf
;
2512 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2513 struct lsp_uninstall_args args
;
2520 args
.lsp_table
= zvrf
->lsp_table
;
2521 args
.type
= lsp_type_from_re_type(client
->proto
);
2522 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2526 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2527 lsp_type_from_re_type(client
->proto
));
2528 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2529 lsp_type_from_re_type(client
->proto
));
2536 * Return FEC (if any) to which this label is bound.
2537 * Note: Only works for per-prefix binding and when the label is not
2539 * TODO: Currently walks entire table, can optimize later with another
2542 struct zebra_fec
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2545 struct route_node
*rn
;
2546 struct zebra_fec
*fec
;
2549 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2550 if (zvrf
->fec_table
[af
] == NULL
)
2553 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2554 rn
= route_next(rn
)) {
2558 if (fec
->label
== label
)
2567 * Inform if specified label is currently bound to a FEC or not.
2569 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2571 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2575 * Add static FEC to label binding. If there are clients registered for this
2576 * FEC, notify them. If there are labeled routes for this FEC, install the
2577 * label forwarding entry.
2579 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2580 mpls_label_t in_label
)
2582 struct route_table
*table
;
2583 struct zebra_fec
*fec
;
2584 mpls_label_t old_label
;
2587 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2591 /* Update existing FEC or create a new one. */
2592 fec
= fec_find(table
, p
);
2594 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2595 MPLS_INVALID_LABEL_INDEX
);
2597 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2598 "Failed to add FEC %pFX upon config", p
);
2602 if (IS_ZEBRA_DEBUG_MPLS
)
2603 zlog_debug("Add fec %pFX label %u", p
, in_label
);
2605 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2606 if (fec
->label
== in_label
)
2607 /* Duplicate config */
2610 /* Label change, update clients. */
2611 old_label
= fec
->label
;
2612 if (IS_ZEBRA_DEBUG_MPLS
)
2613 zlog_debug("Update fec %pFX new label %u", p
, in_label
);
2615 fec
->label
= in_label
;
2616 fec_update_clients(fec
);
2618 /* Update label forwarding entries appropriately */
2619 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2626 * Remove static FEC to label binding. If there are no clients registered
2627 * for this FEC, delete the FEC; else notify clients
2628 * Note: Upon delete of static binding, if label index exists for this FEC,
2629 * client may need to be updated with derived label.
2631 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2633 struct route_table
*table
;
2634 struct zebra_fec
*fec
;
2635 mpls_label_t old_label
;
2637 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2641 fec
= fec_find(table
, p
);
2643 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2644 "Failed to find FEC %pFX upon delete", p
);
2648 if (IS_ZEBRA_DEBUG_MPLS
) {
2649 zlog_debug("Delete fec %pFX label %u label index %u", p
,
2650 fec
->label
, fec
->label_index
);
2653 old_label
= fec
->label
;
2654 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2655 fec
->label
= MPLS_INVALID_LABEL
;
2657 /* If no client exists, just delete the FEC. */
2658 if (list_isempty(fec
->client_list
)) {
2663 /* Derive the local label (from label index) or reset it. */
2664 fec_derive_label_from_index(zvrf
, fec
);
2666 /* If there is a label change, update clients. */
2667 if (fec
->label
== old_label
)
2669 fec_update_clients(fec
);
2671 /* Update label forwarding entries appropriately */
2672 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2676 * Display MPLS FEC to label binding configuration (VTY command handler).
2678 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2680 struct route_node
*rn
;
2682 struct zebra_fec
*fec
;
2685 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2686 if (zvrf
->fec_table
[af
] == NULL
)
2689 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2690 rn
= route_next(rn
)) {
2697 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2701 vty_out(vty
, "mpls label bind %pFX %s\n", &rn
->p
,
2702 label2str(fec
->label
, 0, lstr
, BUFSIZ
));
2710 * Display MPLS FEC to label binding (VTY command handler).
2712 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2714 struct route_node
*rn
;
2717 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2718 if (zvrf
->fec_table
[af
] == NULL
)
2721 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2722 rn
= route_next(rn
)) {
2725 fec_print(rn
->info
, vty
);
2731 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2733 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2736 struct route_table
*table
;
2737 struct route_node
*rn
;
2739 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2744 rn
= route_node_lookup(table
, p
);
2748 route_unlock_node(rn
);
2752 fec_print(rn
->info
, vty
);
2755 static void mpls_zebra_nhe_update(struct route_entry
*re
, afi_t afi
,
2756 struct nhg_hash_entry
*new_nhe
)
2758 struct nhg_hash_entry
*nhe
;
2760 nhe
= zebra_nhg_rib_find_nhe(new_nhe
, afi
);
2762 route_entry_update_nhe(re
, nhe
);
2765 static bool ftn_update_nexthop(bool add_p
, struct nexthop
*nexthop
,
2766 enum lsp_types_t type
,
2767 const struct zapi_nexthop
*znh
)
2769 if (add_p
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2770 nexthop_add_labels(nexthop
, type
, znh
->label_num
, znh
->labels
);
2771 else if (!add_p
&& nexthop
->nh_label_type
== type
)
2772 nexthop_del_labels(nexthop
);
2779 void zebra_mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2780 struct prefix
*prefix
, uint8_t route_type
,
2781 uint8_t route_instance
)
2783 struct route_table
*table
;
2784 struct route_node
*rn
;
2785 struct route_entry
*re
;
2786 struct nexthop
*nexthop
;
2787 struct nhg_hash_entry
*new_nhe
;
2788 afi_t afi
= family2afi(prefix
->family
);
2791 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2795 /* Lookup existing route */
2796 rn
= route_node_get(table
, prefix
);
2797 RNODE_FOREACH_RE (rn
, re
) {
2798 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2800 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2807 * Nexthops are now shared by multiple routes, so we have to make
2808 * a local copy, modify the copy, then update the route.
2810 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2812 for (nexthop
= new_nhe
->nhg
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2813 nexthop_del_labels(nexthop
);
2815 /* Update backup routes/nexthops also, if present. */
2816 if (zebra_nhg_get_backup_nhg(new_nhe
) != NULL
) {
2817 for (nexthop
= new_nhe
->backup_info
->nhe
->nhg
.nexthop
; nexthop
;
2818 nexthop
= nexthop
->next
)
2819 nexthop_del_labels(nexthop
);
2822 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2823 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2825 /* This will create (or ref) a new nhe, so we will discard the local
2828 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
2830 zebra_nhg_free(new_nhe
);
2836 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2837 * update its labels according to 'add_p', and return 'true' if successful.
2839 static bool ftn_update_znh(bool add_p
, enum lsp_types_t type
,
2840 struct nexthop
*head
, const struct zapi_nexthop
*znh
)
2842 bool found
= false, success
= false;
2843 struct nexthop
*nexthop
;
2845 for (nexthop
= head
; nexthop
; nexthop
= nexthop
->next
) {
2846 switch (nexthop
->type
) {
2847 case NEXTHOP_TYPE_IPV4
:
2848 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2849 if (znh
->type
!= NEXTHOP_TYPE_IPV4
2850 && znh
->type
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2852 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
,
2855 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2856 && nexthop
->ifindex
!= znh
->ifindex
)
2861 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2866 case NEXTHOP_TYPE_IPV6
:
2867 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2868 if (znh
->type
!= NEXTHOP_TYPE_IPV6
2869 && znh
->type
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2871 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
,
2874 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2875 && nexthop
->ifindex
!= znh
->ifindex
)
2880 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2884 case NEXTHOP_TYPE_IFINDEX
:
2885 if (znh
->type
!= NEXTHOP_TYPE_IFINDEX
)
2887 if (nexthop
->ifindex
!= znh
->ifindex
)
2892 if (!ftn_update_nexthop(add_p
, nexthop
, type
, znh
))
2896 case NEXTHOP_TYPE_BLACKHOLE
:
2909 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2910 * using zapi message info.
2911 * There are several changes that need to be made, in several zebra
2912 * data structures, so we want to do all the work required at once.
2914 void zebra_mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
2915 const struct zapi_labels
*zl
)
2917 int i
, counter
, ret
= 0;
2918 char buf
[NEXTHOP_STRLEN
];
2919 const struct zapi_nexthop
*znh
;
2920 struct route_table
*table
;
2921 struct route_node
*rn
= NULL
;
2922 struct route_entry
*re
= NULL
;
2923 struct nhg_hash_entry
*new_nhe
= NULL
;
2926 const struct prefix
*prefix
= NULL
;
2927 struct hash
*lsp_table
;
2928 struct zebra_ile tmp_ile
;
2929 struct zebra_lsp
*lsp
= NULL
;
2931 /* Prep LSP for add case */
2934 lsp_table
= zvrf
->lsp_table
;
2938 /* Find or create LSP object */
2939 tmp_ile
.in_label
= zl
->local_label
;
2940 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2943 /* Prep for route/FEC update if requested */
2944 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2945 prefix
= &zl
->route
.prefix
;
2947 afi
= family2afi(prefix
->family
);
2950 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2952 /* Lookup existing route */
2953 rn
= route_node_get(table
, prefix
);
2954 RNODE_FOREACH_RE(rn
, re
) {
2955 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2957 if (re
->type
== zl
->route
.type
&&
2958 re
->instance
== zl
->route
.instance
)
2965 * Copy over current nexthops into a temporary group.
2966 * We can't just change the values here since the nhgs
2967 * are shared and if the labels change, we'll need
2968 * to find or create a new nhg. We need to create
2969 * a whole temporary group, make changes to it,
2970 * then attach that to the route.
2972 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
2976 * The old version of the zapi code
2977 * attempted to manage LSPs before trying to
2978 * find a route/FEC, so we'll continue that way.
2980 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
)
2982 "%s: FTN update requested: no route for prefix %pFX",
2988 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2992 for (i
= 0; i
< zl
->nexthop_num
; i
++) {
2994 znh
= &zl
->nexthops
[i
];
2996 /* Attempt LSP update */
2998 ret
= lsp_znh_install(lsp
, zl
->type
, znh
);
3000 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
3001 zl
->local_label
, znh
->type
,
3002 &znh
->gate
, znh
->ifindex
,
3005 if (IS_ZEBRA_DEBUG_RECV
|| IS_ZEBRA_DEBUG_MPLS
) {
3006 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3007 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
3008 __func__
, (add_p
? "" : "un"),
3009 zl
->local_label
, buf
);
3014 /* Attempt route/FEC update if requested */
3018 /* Search the route's nexthops for a match, and update it. */
3019 found
= ftn_update_znh(add_p
, zl
->type
, new_nhe
->nhg
.nexthop
,
3023 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
3024 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3026 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
3027 __func__
, prefix
, zl
->local_label
, buf
);
3032 * Process backup LSPs/nexthop entries also. We associate backup
3033 * LSP info with backup nexthops.
3035 if (!CHECK_FLAG(zl
->message
, ZAPI_LABELS_HAS_BACKUPS
))
3038 for (i
= 0; i
< zl
->backup_nexthop_num
; i
++) {
3040 znh
= &zl
->backup_nexthops
[i
];
3043 ret
= lsp_backup_znh_install(lsp
, zl
->type
, znh
);
3045 ret
= mpls_lsp_uninstall(zvrf
, zl
->type
,
3047 znh
->type
, &znh
->gate
,
3048 znh
->ifindex
, true);
3051 if (IS_ZEBRA_DEBUG_RECV
||
3052 IS_ZEBRA_DEBUG_MPLS
) {
3053 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3054 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
3055 __func__
, (add_p
? "" : "un"),
3056 zl
->local_label
, buf
);
3061 /* Attempt backup nexthop/FEC update if requested */
3062 if (re
== NULL
|| zebra_nhg_get_backup_nhg(new_nhe
) == NULL
)
3065 /* Search the route's backup nexthops for a match
3068 found
= ftn_update_znh(add_p
, zl
->type
,
3069 new_nhe
->backup_info
->nhe
->nhg
.nexthop
,
3073 } else if (IS_ZEBRA_DEBUG_RECV
| IS_ZEBRA_DEBUG_MPLS
) {
3074 zapi_nexthop2str(znh
, buf
, sizeof(buf
));
3076 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
3077 __func__
, prefix
, zl
->local_label
, buf
);
3084 * If we made changes, update the route, and schedule it
3085 * for rib processing
3087 if (re
!= NULL
&& counter
> 0) {
3090 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3091 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
3093 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3099 zebra_nhg_free(new_nhe
);
3103 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3104 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3105 * the out-label for an existing NHLFE (update case).
3107 static struct zebra_nhlfe
*
3108 lsp_add_nhlfe(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3109 uint8_t num_out_labels
, const mpls_label_t
*out_labels
,
3110 enum nexthop_types_t gtype
, const union g_addr
*gate
,
3111 ifindex_t ifindex
, vrf_id_t vrf_id
, bool is_backup
)
3113 struct zebra_nhlfe
*nhlfe
;
3114 char buf
[MPLS_LABEL_STRLEN
];
3115 const char *backup_str
;
3118 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3120 backup_str
= "backup ";
3122 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3128 struct nexthop
*nh
= nhlfe
->nexthop
;
3132 /* Clear deleted flag (in case it was set) */
3133 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3135 if (!nh
->nh_label
|| num_out_labels
== 0)
3140 nh
->nh_label
->num_labels
== num_out_labels
&&
3141 !memcmp(nh
->nh_label
->label
, out_labels
,
3142 sizeof(mpls_label_t
) * num_out_labels
))
3146 if (IS_ZEBRA_DEBUG_MPLS
) {
3147 char buf2
[MPLS_LABEL_STRLEN
];
3148 char buf3
[MPLS_LABEL_STRLEN
];
3150 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3151 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3152 sizeof(buf2
), 0, 0);
3153 mpls_label2str(nh
->nh_label
->num_labels
,
3154 nh
->nh_label
->label
, buf3
, sizeof(buf3
),
3155 nh
->nh_label_type
, 0);
3157 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3158 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3162 /* Update out label(s), trigger processing. */
3163 if (nh
->nh_label
&& nh
->nh_label
->num_labels
== num_out_labels
)
3164 memcpy(nh
->nh_label
->label
, out_labels
,
3165 sizeof(mpls_label_t
) * num_out_labels
);
3167 nexthop_del_labels(nh
);
3168 nexthop_add_labels(nh
, type
, num_out_labels
,
3172 /* Add LSP entry to this nexthop */
3173 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, vrf_id
,
3174 num_out_labels
, out_labels
, is_backup
);
3178 if (IS_ZEBRA_DEBUG_MPLS
) {
3179 char buf2
[MPLS_LABEL_STRLEN
];
3181 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3183 mpls_label2str(num_out_labels
, out_labels
, buf2
,
3184 sizeof(buf2
), 0, 0);
3186 snprintf(buf2
, sizeof(buf2
), "-");
3188 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3189 lsp
->ile
.in_label
, type
, backup_str
, buf
,
3193 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
3196 /* Mark NHLFE, queue LSP for processing. */
3197 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3203 * Install an LSP and forwarding entry; used primarily
3204 * from vrf zapi message processing.
3205 * TODO: handle vrf_id parameter when mpls API extends to interface or SRTE
3208 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3209 mpls_label_t in_label
, uint8_t num_out_labels
,
3210 const mpls_label_t
*out_labels
, enum nexthop_types_t gtype
,
3211 const union g_addr
*gate
, ifindex_t ifindex
)
3213 struct hash
*lsp_table
;
3214 struct zebra_ile tmp_ile
;
3215 struct zebra_lsp
*lsp
;
3216 struct zebra_nhlfe
*nhlfe
;
3219 lsp_table
= zvrf
->lsp_table
;
3223 /* Find or create LSP object */
3224 tmp_ile
.in_label
= in_label
;
3225 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
3227 nhlfe
= lsp_add_nhlfe(lsp
, type
, num_out_labels
, out_labels
, gtype
,
3228 gate
, ifindex
, VRF_DEFAULT
, false /*backup*/);
3232 /* Queue LSP for processing. */
3233 if (lsp_processq_add(lsp
))
3240 * Install or replace NHLFE, using info from zapi nexthop
3242 static int lsp_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3243 const struct zapi_nexthop
*znh
)
3245 struct zebra_nhlfe
*nhlfe
;
3247 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
, znh
->labels
, znh
->type
,
3248 &znh
->gate
, znh
->ifindex
, znh
->vrf_id
,
3253 /* Update backup info if present */
3254 if (CHECK_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_HAS_BACKUP
)) {
3255 if (znh
->backup_num
> NEXTHOP_MAX_BACKUPS
) {
3260 nhlfe
->nexthop
->backup_num
= znh
->backup_num
;
3261 memcpy(nhlfe
->nexthop
->backup_idx
, znh
->backup_idx
,
3263 SET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3265 /* Ensure there's no stale backup info */
3266 UNSET_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
);
3267 nhlfe
->nexthop
->backup_num
= 0;
3270 /* Queue LSP for processing. */
3271 if (lsp_processq_add(lsp
))
3278 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3280 static int lsp_backup_znh_install(struct zebra_lsp
*lsp
, enum lsp_types_t type
,
3281 const struct zapi_nexthop
*znh
)
3283 struct zebra_nhlfe
*nhlfe
;
3285 nhlfe
= lsp_add_nhlfe(lsp
, type
, znh
->label_num
, znh
->labels
, znh
->type
,
3286 &znh
->gate
, znh
->ifindex
, znh
->vrf_id
,
3288 if (nhlfe
== NULL
) {
3289 if (IS_ZEBRA_DEBUG_MPLS
)
3290 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3291 __func__
, lsp
->ile
.in_label
);
3295 /* Queue LSP for processing. */
3296 if (lsp_processq_add(lsp
))
3302 struct zebra_lsp
*mpls_lsp_find(struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
3304 struct hash
*lsp_table
;
3305 struct zebra_ile tmp_ile
;
3308 lsp_table
= zvrf
->lsp_table
;
3312 /* If entry is not present, exit. */
3313 tmp_ile
.in_label
= in_label
;
3314 return hash_lookup(lsp_table
, &tmp_ile
);
3318 * Uninstall a particular NHLFE in the forwarding table. If this is
3319 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3321 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3322 mpls_label_t in_label
, enum nexthop_types_t gtype
,
3323 const union g_addr
*gate
, ifindex_t ifindex
,
3326 struct hash
*lsp_table
;
3327 struct zebra_ile tmp_ile
;
3328 struct zebra_lsp
*lsp
;
3329 struct zebra_nhlfe
*nhlfe
;
3330 char buf
[NEXTHOP_STRLEN
];
3331 bool schedule_lsp
= false;
3334 lsp_table
= zvrf
->lsp_table
;
3338 /* If entry is not present, exit. */
3339 tmp_ile
.in_label
= in_label
;
3340 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3345 nhlfe
= nhlfe_find(&lsp
->backup_nhlfe_list
, type
, gtype
,
3348 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, type
, gtype
, gate
,
3353 if (IS_ZEBRA_DEBUG_MPLS
) {
3354 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3355 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3356 in_label
, type
, buf
, nhlfe
->flags
);
3359 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ||
3360 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
3361 schedule_lsp
= true;
3363 /* Mark NHLFE for delete or directly delete, as appropriate. */
3365 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
3366 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
3368 if (IS_ZEBRA_DEBUG_MPLS
)
3369 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3370 lsp
->ile
.in_label
, lsp
->flags
);
3371 if (lsp_processq_add(lsp
))
3376 /* Free LSP entry if no other NHLFEs and not scheduled. */
3377 lsp_check_free(lsp_table
, &lsp
);
3382 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3383 mpls_label_t in_label
)
3385 struct hash
*lsp_table
;
3386 struct zebra_ile tmp_ile
;
3387 struct zebra_lsp
*lsp
;
3390 lsp_table
= zvrf
->lsp_table
;
3394 /* If entry is not present, exit. */
3395 tmp_ile
.in_label
= in_label
;
3396 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3400 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
3404 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3405 * If no other NHLFEs exist, the entry would be deleted.
3407 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
3409 struct lsp_uninstall_args
*args
= ctxt
;
3410 struct zebra_lsp
*lsp
;
3411 struct hash
*lsp_table
;
3413 lsp
= (struct zebra_lsp
*)bucket
->data
;
3414 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
)
3417 lsp_table
= args
->lsp_table
;
3421 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
3425 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3428 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
3429 int afi
, enum lsp_types_t lsp_type
)
3431 struct route_table
*table
;
3432 struct route_node
*rn
;
3433 struct route_entry
*re
;
3434 struct nexthop
*nexthop
;
3435 struct nexthop_group
*nhg
;
3438 /* Process routes of interested address-families. */
3439 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
3443 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
3446 RNODE_FOREACH_RE (rn
, re
) {
3447 struct nhg_hash_entry
*new_nhe
;
3449 new_nhe
= zebra_nhe_copy(re
->nhe
, 0);
3451 nhg
= &new_nhe
->nhg
;
3452 for (nexthop
= nhg
->nexthop
; nexthop
;
3453 nexthop
= nexthop
->next
) {
3454 if (nexthop
->nh_label_type
!= lsp_type
)
3457 nexthop_del_labels(nexthop
);
3458 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3459 SET_FLAG(re
->status
,
3460 ROUTE_ENTRY_LABELS_CHANGED
);
3464 /* Check for backup info and update that also */
3465 nhg
= zebra_nhg_get_backup_nhg(new_nhe
);
3467 for (nexthop
= nhg
->nexthop
; nexthop
;
3468 nexthop
= nexthop
->next
) {
3469 if (nexthop
->nh_label_type
!= lsp_type
)
3472 nexthop_del_labels(nexthop
);
3473 SET_FLAG(re
->status
,
3474 ROUTE_ENTRY_CHANGED
);
3475 SET_FLAG(re
->status
,
3476 ROUTE_ENTRY_LABELS_CHANGED
);
3481 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
3482 mpls_zebra_nhe_update(re
, afi
, new_nhe
);
3484 zebra_nhg_free(new_nhe
);
3492 #if defined(HAVE_CUMULUS)
3494 * Check that the label values used in LSP creation are consistent. The
3495 * main criteria is that if there is ECMP, the label operation must still
3496 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3497 * to current HW restrictions.
3499 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
3500 mpls_label_t in_label
,
3501 mpls_label_t out_label
,
3502 enum nexthop_types_t gtype
,
3503 union g_addr
*gate
, ifindex_t ifindex
)
3505 struct hash
*slsp_table
;
3506 struct zebra_ile tmp_ile
;
3507 struct zebra_lsp
*lsp
;
3508 struct zebra_nhlfe
*nhlfe
;
3509 const struct nexthop
*nh
;
3512 slsp_table
= zvrf
->slsp_table
;
3516 /* If entry is not present, exit. */
3517 tmp_ile
.in_label
= in_label
;
3518 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3522 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3523 gtype
, gate
, ifindex
);
3525 nh
= nhlfe
->nexthop
;
3527 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3530 if (nh
->nh_label
->label
[0] == out_label
)
3533 /* If not only NHLFE, cannot allow label change. */
3534 if (nhlfe
!= nhlfe_list_first(&lsp
->nhlfe_list
) ||
3535 nhlfe_list_next(&lsp
->nhlfe_list
, nhlfe
) != NULL
)
3538 /* If other NHLFEs exist, label operation must match. */
3539 nhlfe
= nhlfe_list_first(&lsp
->nhlfe_list
);
3540 if (nhlfe
!= NULL
) {
3543 nh
= nhlfe
->nexthop
;
3545 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3548 cur_op
= (nh
->nh_label
->label
[0] ==
3549 MPLS_LABEL_IMPLICIT_NULL
);
3550 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
3551 if (cur_op
!= new_op
)
3556 /* Label values are good. */
3559 #endif /* HAVE_CUMULUS */
3562 * Add static LSP entry. This may be the first entry for this incoming label
3563 * or an additional nexthop; an existing entry may also have outgoing label
3565 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3568 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3569 mpls_label_t out_label
,
3570 enum nexthop_types_t gtype
, union g_addr
*gate
,
3573 struct hash
*slsp_table
;
3574 struct zebra_ile tmp_ile
;
3575 struct zebra_lsp
*lsp
;
3576 struct zebra_nhlfe
*nhlfe
;
3580 slsp_table
= zvrf
->slsp_table
;
3584 /* Find or create LSP. */
3585 tmp_ile
.in_label
= in_label
;
3586 lsp
= hash_get(slsp_table
, &tmp_ile
, lsp_alloc
);
3588 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
, gtype
, gate
,
3591 struct nexthop
*nh
= nhlfe
->nexthop
;
3594 assert(nh
->nh_label
);
3596 /* Compare existing nexthop */
3597 if (nh
->nh_label
->num_labels
== 1 &&
3598 nh
->nh_label
->label
[0] == out_label
)
3602 if (IS_ZEBRA_DEBUG_MPLS
) {
3603 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3605 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3606 in_label
, buf
, out_label
,
3607 nh
->nh_label
->label
[0]);
3609 if (nh
->nh_label
->num_labels
== 1)
3610 nh
->nh_label
->label
[0] = out_label
;
3612 nexthop_del_labels(nh
);
3613 nexthop_add_labels(nh
, ZEBRA_LSP_STATIC
, 1, &out_label
);
3617 /* Add static LSP entry to this nexthop */
3618 nhlfe
= nhlfe_add(lsp
, ZEBRA_LSP_STATIC
, gtype
, gate
, ifindex
,
3619 VRF_DEFAULT
, 1, &out_label
, false /*backup*/);
3623 if (IS_ZEBRA_DEBUG_MPLS
) {
3624 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3626 "Add static LSP in-label %u nexthop %s out-label %u",
3627 in_label
, buf
, out_label
);
3631 /* (Re)Install LSP in the main table. */
3632 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, 1, &out_label
,
3633 gtype
, gate
, ifindex
))
3640 * Delete static LSP entry. This may be the delete of one particular
3641 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3643 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3644 * LSP configuration.
3646 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3647 enum nexthop_types_t gtype
, union g_addr
*gate
,
3650 struct hash
*slsp_table
;
3651 struct zebra_ile tmp_ile
;
3652 struct zebra_lsp
*lsp
;
3653 struct zebra_nhlfe
*nhlfe
;
3656 slsp_table
= zvrf
->slsp_table
;
3660 /* If entry is not present, exit. */
3661 tmp_ile
.in_label
= in_label
;
3662 lsp
= hash_lookup(slsp_table
, &tmp_ile
);
3666 /* Is it delete of entire LSP or a specific NHLFE? */
3667 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3668 if (IS_ZEBRA_DEBUG_MPLS
)
3669 zlog_debug("Del static LSP in-label %u", in_label
);
3671 /* Uninstall entire LSP from the main table. */
3672 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3674 /* Delete all static NHLFEs */
3675 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3679 /* Find specific NHLFE, exit if not found. */
3680 nhlfe
= nhlfe_find(&lsp
->nhlfe_list
, ZEBRA_LSP_STATIC
,
3681 gtype
, gate
, ifindex
);
3685 if (IS_ZEBRA_DEBUG_MPLS
) {
3687 nhlfe2str(nhlfe
, buf
, sizeof(buf
));
3688 zlog_debug("Del static LSP in-label %u nexthop %s",
3692 /* Uninstall LSP from the main table. */
3693 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3694 gate
, ifindex
, false);
3696 /* Delete static LSP NHLFE */
3700 /* Remove entire static LSP entry if no NHLFE - valid in either case
3703 if (nhlfe_list_first(&lsp
->nhlfe_list
) == NULL
) {
3704 lsp
= hash_release(slsp_table
, &tmp_ile
);
3705 lsp_free_nhlfe(lsp
);
3706 XFREE(MTYPE_LSP
, lsp
);
3713 * Schedule all MPLS label forwarding entries for processing.
3714 * Called upon changes that may affect one or more of them such as
3715 * interface or nexthop state changes.
3717 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3721 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3725 * Display MPLS label forwarding table for a specific LSP
3726 * (VTY command handler).
3728 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3729 mpls_label_t label
, bool use_json
)
3731 struct hash
*lsp_table
;
3732 struct zebra_lsp
*lsp
;
3733 struct zebra_ile tmp_ile
;
3734 json_object
*json
= NULL
;
3737 lsp_table
= zvrf
->lsp_table
;
3740 vty_out(vty
, "{}\n");
3744 /* If entry is not present, exit. */
3745 tmp_ile
.in_label
= label
;
3746 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3749 vty_out(vty
, "{}\n");
3754 json
= lsp_json(lsp
);
3755 vty_json(vty
, json
);
3757 lsp_print(vty
, lsp
);
3761 * Display MPLS label forwarding table (VTY command handler).
3763 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3767 json_object
*json
= NULL
;
3768 struct zebra_lsp
*lsp
= NULL
;
3769 struct zebra_nhlfe
*nhlfe
= NULL
;
3770 struct listnode
*node
= NULL
;
3771 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3774 json
= json_object_new_object();
3776 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3777 json_object_object_add(json
,
3778 label2str(lsp
->ile
.in_label
, 0,
3782 vty_json(vty
, json
);
3786 /* Prepare table. */
3787 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3788 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3789 tt
->style
.cell
.rpad
= 2;
3790 tt
->style
.corner
= '+';
3792 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3794 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3795 frr_each_safe(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3796 struct nexthop
*nexthop
;
3797 const char *out_label_str
;
3798 char nh_buf
[NEXTHOP_STRLEN
];
3800 nexthop
= nhlfe
->nexthop
;
3802 switch (nexthop
->type
) {
3803 case NEXTHOP_TYPE_IFINDEX
: {
3804 struct zebra_ns
*zns
;
3805 struct interface
*ifp
;
3807 zns
= zebra_ns_lookup(NS_DEFAULT
);
3808 ifp
= if_lookup_by_index_per_ns(
3809 zns
, nexthop
->ifindex
);
3810 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3811 ifp
? ifp
->name
: "Null");
3814 case NEXTHOP_TYPE_IPV4
:
3815 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3816 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3817 nh_buf
, sizeof(nh_buf
));
3819 case NEXTHOP_TYPE_IPV6
:
3820 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3821 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3822 nh_buf
, sizeof(nh_buf
));
3824 case NEXTHOP_TYPE_BLACKHOLE
:
3828 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
&&
3830 out_label_str
= mpls_label2str(
3831 nexthop
->nh_label
->num_labels
,
3832 &nexthop
->nh_label
->label
[0],
3834 nexthop
->nh_label_type
, 1);
3836 out_label_str
= "-";
3838 ttable_add_row(tt
, "%u|%s|%s|%s",
3840 nhlfe_type2str(nhlfe
->type
),
3841 nh_buf
, out_label_str
);
3845 /* Dump the generated table. */
3846 if (tt
->nrows
> 1) {
3847 char *table
= ttable_dump(tt
, "\n");
3848 vty_out(vty
, "%s\n", table
);
3849 XFREE(MTYPE_TMP
, table
);
3854 list_delete(&lsp_list
);
3858 * Create printable string for static LSP configuration.
3860 static char *nhlfe_config_str(const struct zebra_nhlfe
*nhlfe
, char *buf
,
3863 const struct nexthop
*nh
;
3865 nh
= nhlfe
->nexthop
;
3869 case NEXTHOP_TYPE_IPV4
:
3870 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3871 inet_ntop(AF_INET
, &nh
->gate
.ipv4
, buf
, size
);
3873 strlcat(buf
, ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3876 case NEXTHOP_TYPE_IPV6
:
3877 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3878 inet_ntop(AF_INET6
, &nh
->gate
.ipv6
, buf
, size
);
3881 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3884 case NEXTHOP_TYPE_IFINDEX
:
3887 ifindex2ifname(nh
->ifindex
, VRF_DEFAULT
),
3890 case NEXTHOP_TYPE_BLACKHOLE
:
3898 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3900 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3902 struct zebra_lsp
*lsp
;
3903 struct zebra_nhlfe
*nhlfe
;
3905 struct listnode
*node
;
3906 struct list
*slsp_list
=
3907 hash_get_sorted_list(zvrf
->slsp_table
, lsp_cmp
);
3909 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, lsp
)) {
3910 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
3914 nh
= nhlfe
->nexthop
;
3915 if (nh
== NULL
|| nh
->nh_label
== NULL
)
3918 nhlfe_config_str(nhlfe
, buf
, sizeof(buf
));
3920 switch (nh
->nh_label
->label
[0]) {
3921 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3922 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3923 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3925 case MPLS_LABEL_IMPLICIT_NULL
:
3926 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3929 snprintf(lstr
, sizeof(lstr
), "%u",
3930 nh
->nh_label
->label
[0]);
3934 vty_out(vty
, "mpls lsp %u %s %s\n", lsp
->ile
.in_label
,
3939 list_delete(&slsp_list
);
3940 return (zvrf
->slsp_table
->count
? 1 : 0);
3944 * Add/update global label block.
3946 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3949 zvrf
->mpls_srgb
.start_label
= start_label
;
3950 zvrf
->mpls_srgb
.end_label
= end_label
;
3952 /* Evaluate registered FECs to see if any get a label or not. */
3958 * Delete global label block.
3960 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3962 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3963 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3965 /* Process registered FECs to clear their local label, if needed. */
3971 * Display MPLS global label block configuration (VTY command handler).
3973 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3975 if (zvrf
->mpls_srgb
.start_label
== 0)
3978 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3979 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3980 vty_out(vty
, "mpls label global-block %u %u\n",
3981 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3988 * Called when VRF becomes inactive, cleans up information but keeps
3991 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3993 struct zebra_vrf
*def_zvrf
;
3996 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3997 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
4000 * For other vrfs, we try to remove associated LSPs; we locate
4001 * the LSPs in the default vrf.
4003 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
4005 /* At shutdown, the default may be gone already */
4006 if (def_zvrf
== NULL
)
4009 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4010 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
4011 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
4017 * When a vrf label is assigned and the client goes away
4018 * we should cleanup the vrf labels associated with
4021 void zebra_mpls_client_cleanup_vrf_label(uint8_t proto
)
4024 struct zebra_vrf
*def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
4026 if (def_zvrf
== NULL
)
4029 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4030 struct zebra_vrf
*zvrf
= vrf
->info
;
4036 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4037 if (zvrf
->label_proto
[afi
] == proto
4038 && zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
4039 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
4042 * Cleanup data structures by fiat
4044 zvrf
->label_proto
[afi
] = 0;
4045 zvrf
->label
[afi
] = MPLS_LABEL_NONE
;
4050 static void lsp_table_free(void *p
)
4052 struct zebra_lsp
*lsp
= p
;
4054 lsp_free_nhlfe(lsp
);
4056 XFREE(MTYPE_LSP
, lsp
);
4060 * Called upon process exiting, need to delete LSP forwarding
4061 * entries from the kernel.
4062 * NOTE: Currently supported only for default VRF.
4064 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
4066 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
4067 hash_clean_and_free(&zvrf
->lsp_table
, lsp_table_free
);
4068 hash_clean_and_free(&zvrf
->slsp_table
, lsp_table_free
);
4069 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
4070 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
4074 * Allocate MPLS tables for this VRF and do other initialization.
4075 * NOTE: Currently supported only for default VRF.
4077 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
4084 snprintf(buffer
, sizeof(buffer
), "ZEBRA SLSP table: %s",
4086 zvrf
->slsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
4088 snprintf(buffer
, sizeof(buffer
), "ZEBRA LSP table: %s",
4090 zvrf
->lsp_table
= hash_create_size(8, label_hash
, label_cmp
, buffer
);
4091 zvrf
->fec_table
[AFI_IP
] = route_table_init();
4092 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
4093 zvrf
->mpls_flags
= 0;
4094 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
4095 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
4098 void zebra_mpls_turned_on(void)
4100 if (!mpls_enabled
) {
4101 mpls_processq_init();
4102 mpls_enabled
= true;
4105 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
4106 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);
4110 * Global MPLS initialization.
4112 void zebra_mpls_init(void)
4114 mpls_enabled
= false;
4115 mpls_pw_reach_strict
= false;
4117 if (mpls_kernel_init() < 0) {
4118 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
4119 "Disabling MPLS support (no kernel support)");
4123 zebra_mpls_turned_on();