2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
32 #include "workqueue.h"
37 #include "termtable.h"
40 #include "zebra/rib.h"
42 #include "zebra/interface.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/zebra_mpls.h"
50 #include "zebra/zebra_errors.h"
52 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
55 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
56 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
57 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
61 /* static function declarations */
63 static void fec_evaluate(struct zebra_vrf
*zvrf
);
64 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
66 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
67 struct route_node
*rn
, struct route_entry
*re
);
68 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
69 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
70 mpls_label_t old_label
);
71 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
72 static void fec_update_clients(zebra_fec_t
*fec
);
73 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
74 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
75 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
76 mpls_label_t label
, uint32_t flags
,
77 uint32_t label_index
);
78 static int fec_del(zebra_fec_t
*fec
);
80 static unsigned int label_hash(const void *p
);
81 static bool label_cmp(const void *p1
, const void *p2
);
82 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
85 struct nexthop
*nexthop
);
86 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
88 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
89 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
90 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
91 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
92 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
93 static void lsp_processq_complete(struct work_queue
*wq
);
94 static int lsp_processq_add(zebra_lsp_t
*lsp
);
95 static void *lsp_alloc(void *p
);
97 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
98 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
99 union g_addr
*gate
, ifindex_t ifindex
);
100 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
101 enum nexthop_types_t gtype
, union g_addr
*gate
,
103 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
104 enum nexthop_types_t gtype
, union g_addr
*gate
,
105 ifindex_t ifindex
, mpls_label_t out_label
);
106 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
107 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
108 struct mpls_label_stack
*nh_label
);
109 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
110 enum lsp_types_t type
);
111 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
112 mpls_label_t in_label
);
113 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
114 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
115 static void *slsp_alloc(void *p
);
116 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
117 union g_addr
*gate
, ifindex_t ifindex
);
118 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
119 enum nexthop_types_t gtype
,
120 union g_addr
*gate
, ifindex_t ifindex
);
121 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
122 enum nexthop_types_t gtype
,
123 union g_addr
*gate
, ifindex_t ifindex
,
124 mpls_label_t out_label
);
125 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
126 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
127 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
130 /* Static functions */
133 * Handle failure in LSP install, clear flags for NHLFE.
135 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
137 zebra_nhlfe_t
*nhlfe
;
138 struct nexthop
*nexthop
;
140 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
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
;
159 zebra_nhlfe_t
*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
);
179 /* For each active nexthop, create NHLFE. Note that we deliberately skip
180 * recursive nexthops right now, because intermediate hops won't
182 * the label advertised by the recursive nexthop (plus we don't have the
183 * logic yet to push multiple labels).
185 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
186 /* Skip inactive and recursive entries. */
187 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
189 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
192 nhlfe
= nhlfe_find(lsp
, lsp_type
, 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 "
207 lsp
->ile
.in_label
, lsp_type
, buf
);
210 /* Update out label, trigger processing. */
211 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
212 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
215 /* Add LSP entry to this nexthop */
216 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
217 &nexthop
->gate
, nexthop
->ifindex
,
218 nexthop
->nh_label
->label
[0]);
222 if (IS_ZEBRA_DEBUG_MPLS
) {
223 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
225 "Add LSP in-label %u type %d nexthop %s "
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
))
246 } else if (!lsp
->nhlfe_list
247 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
248 if (IS_ZEBRA_DEBUG_MPLS
)
249 zlog_debug("Free LSP in-label %u flags 0x%x",
250 lsp
->ile
.in_label
, lsp
->flags
);
252 lsp
= hash_release(lsp_table
, &lsp
->ile
);
253 XFREE(MTYPE_LSP
, lsp
);
260 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
261 * NHLFEs are removed, the entire entry is deleted.
263 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
265 struct hash
*lsp_table
;
268 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
272 lsp_table
= zvrf
->lsp_table
;
276 /* If entry is not present, exit. */
277 tmp_ile
.in_label
= label
;
278 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
279 if (!lsp
|| !lsp
->nhlfe_list
)
282 /* Mark NHLFEs for delete or directly delete, as appropriate. */
283 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
284 nhlfe_next
= nhlfe
->next
;
286 /* Skip static NHLFEs */
287 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
290 if (IS_ZEBRA_DEBUG_MPLS
) {
291 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
293 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
294 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
297 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
298 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
299 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
305 /* Queue LSP for processing, if needed, else delete. */
306 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
307 if (lsp_processq_add(lsp
))
309 } else if (!lsp
->nhlfe_list
310 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
311 if (IS_ZEBRA_DEBUG_MPLS
)
312 zlog_debug("Del LSP in-label %u flags 0x%x",
313 lsp
->ile
.in_label
, lsp
->flags
);
315 lsp
= hash_release(lsp_table
, &lsp
->ile
);
316 XFREE(MTYPE_LSP
, lsp
);
323 * This function is invoked upon change to label block configuration; it
324 * will walk all registered FECs with label-index and appropriately update
325 * their local labels and trigger client updates.
327 static void fec_evaluate(struct zebra_vrf
*zvrf
)
329 struct route_node
*rn
;
331 uint32_t old_label
, new_label
;
335 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
336 if (zvrf
->fec_table
[af
] == NULL
)
339 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
340 rn
= route_next(rn
)) {
341 if ((fec
= rn
->info
) == NULL
)
344 /* Skip configured FECs and those without a label index.
346 if (fec
->flags
& FEC_FLAG_CONFIGURED
347 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
350 if (IS_ZEBRA_DEBUG_MPLS
)
351 prefix2str(&rn
->p
, buf
, BUFSIZ
);
353 /* Save old label, determine new label. */
354 old_label
= fec
->label
;
356 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
357 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
358 new_label
= MPLS_INVALID_LABEL
;
360 /* If label has changed, update FEC and clients. */
361 if (new_label
== old_label
)
364 if (IS_ZEBRA_DEBUG_MPLS
)
366 "Update fec %s new label %u upon label block",
369 fec
->label
= new_label
;
370 fec_update_clients(fec
);
372 /* Update label forwarding entries appropriately */
373 fec_change_update_lsp(zvrf
, fec
, old_label
);
379 * Derive (if possible) and update the local label for the FEC based on
380 * its label index. The index is "acceptable" if it falls within the
381 * globally configured label block (SRGB).
383 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
388 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
389 && zvrf
->mpls_srgb
.start_label
390 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
391 < zvrf
->mpls_srgb
.end_label
))
394 fec
->label
= MPLS_INVALID_LABEL
;
400 * There is a change for this FEC. Install or uninstall label forwarding
401 * entries, as appropriate.
403 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
404 mpls_label_t old_label
)
406 struct route_table
*table
;
407 struct route_node
*rn
;
408 struct route_entry
*re
;
411 /* Uninstall label forwarding entry, if previously installed. */
412 if (old_label
!= MPLS_INVALID_LABEL
413 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
414 lsp_uninstall(zvrf
, old_label
);
416 /* Install label forwarding entry corr. to new label, if needed. */
417 if (fec
->label
== MPLS_INVALID_LABEL
418 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
421 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
422 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
426 /* See if labeled route exists. */
427 rn
= route_node_lookup(table
, &fec
->rn
->p
);
431 RNODE_FOREACH_RE (rn
, re
) {
432 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
436 if (!re
|| !zebra_rib_labeled_unicast(re
))
439 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
446 * Inform about FEC to a registered client.
448 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
451 struct route_node
*rn
;
455 /* Get output stream. */
456 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
458 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
460 stream_putw(s
, rn
->p
.family
);
461 stream_put_prefix(s
, &rn
->p
);
462 stream_putl(s
, fec
->label
);
463 stream_putw_at(s
, 0, stream_get_endp(s
));
464 return zserv_send_message(client
, s
);
468 * Update all registered clients about this FEC. Caller should've updated
469 * FEC and ensure no duplicate updates.
471 static void fec_update_clients(zebra_fec_t
*fec
)
473 struct listnode
*node
;
474 struct zserv
*client
;
476 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
477 if (IS_ZEBRA_DEBUG_MPLS
)
478 zlog_debug("Update client %s",
479 zebra_route_string(client
->proto
));
480 fec_send(fec
, client
);
486 * Print a FEC-label binding entry.
488 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
490 struct route_node
*rn
;
491 struct listnode
*node
;
492 struct zserv
*client
;
496 prefix2str(&rn
->p
, buf
, BUFSIZ
);
497 vty_out(vty
, "%s\n", buf
);
498 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
499 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
500 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
502 if (!list_isempty(fec
->client_list
)) {
503 vty_out(vty
, " Client list:");
504 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
505 vty_out(vty
, " %s(fd %d)",
506 zebra_route_string(client
->proto
),
513 * Locate FEC-label binding that matches with passed info.
515 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
517 struct route_node
*rn
;
520 rn
= route_node_lookup(table
, p
);
524 route_unlock_node(rn
);
529 * Add a FEC. This may be upon a client registering for a binding
530 * or when a binding is configured.
532 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
533 mpls_label_t label
, uint32_t flags
,
534 uint32_t label_index
)
536 struct route_node
*rn
;
541 /* Lookup (or add) route node.*/
542 rn
= route_node_get(table
, p
);
549 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
554 fec
->client_list
= list_new();
556 route_unlock_node(rn
); /* for the route_node_get */
558 fec
->label_index
= label_index
;
565 * Delete a FEC. This may be upon the last client deregistering for
566 * a FEC and no binding exists or when the binding is deleted and there
567 * are no registered clients.
569 static int fec_del(zebra_fec_t
*fec
)
571 list_delete(&fec
->client_list
);
572 fec
->rn
->info
= NULL
;
573 route_unlock_node(fec
->rn
);
574 XFREE(MTYPE_FEC
, fec
);
579 * Hash function for label.
581 static unsigned int label_hash(const void *p
)
583 const zebra_ile_t
*ile
= p
;
585 return (jhash_1word(ile
->in_label
, 0));
589 * Compare 2 LSP hash entries based on in-label.
591 static bool label_cmp(const void *p1
, const void *p2
)
593 const zebra_ile_t
*ile1
= p1
;
594 const zebra_ile_t
*ile2
= p2
;
596 return (ile1
->in_label
== ile2
->in_label
);
600 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
602 * NOTE: Looking only for connected routes right now.
604 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
605 struct nexthop
*nexthop
)
607 struct route_table
*table
;
608 struct prefix_ipv4 p
;
609 struct route_node
*rn
;
610 struct route_entry
*match
;
611 struct nexthop
*match_nh
;
613 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
617 /* Lookup nexthop in IPv4 routing table. */
618 memset(&p
, 0, sizeof(struct prefix_ipv4
));
620 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
621 p
.prefix
= nexthop
->gate
.ipv4
;
623 rn
= route_node_match(table
, (struct prefix
*)&p
);
627 route_unlock_node(rn
);
629 /* Locate a valid connected route. */
630 RNODE_FOREACH_RE (rn
, match
) {
631 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
632 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
635 for (match_nh
= match
->ng
.nexthop
; match_nh
;
636 match_nh
= match_nh
->next
) {
637 if (match
->type
== ZEBRA_ROUTE_CONNECT
638 || nexthop
->ifindex
== match_nh
->ifindex
) {
639 nexthop
->ifindex
= match_nh
->ifindex
;
650 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
652 * NOTE: Looking only for connected routes right now.
654 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
655 struct nexthop
*nexthop
)
657 struct route_table
*table
;
658 struct prefix_ipv6 p
;
659 struct route_node
*rn
;
660 struct route_entry
*match
;
662 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
666 /* Lookup nexthop in IPv6 routing table. */
667 memset(&p
, 0, sizeof(struct prefix_ipv6
));
669 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
670 p
.prefix
= nexthop
->gate
.ipv6
;
672 rn
= route_node_match(table
, (struct prefix
*)&p
);
676 route_unlock_node(rn
);
678 /* Locate a valid connected route. */
679 RNODE_FOREACH_RE (rn
, match
) {
680 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
681 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
682 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
686 if (!match
|| !match
->ng
.nexthop
)
689 nexthop
->ifindex
= match
->ng
.nexthop
->ifindex
;
695 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
697 * NOTE: Each NHLFE points to only 1 nexthop.
699 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
701 struct nexthop
*nexthop
;
702 struct interface
*ifp
;
703 struct zebra_ns
*zns
;
705 nexthop
= nhlfe
->nexthop
;
706 if (!nexthop
) // unexpected
709 /* Check on nexthop based on type. */
710 switch (nexthop
->type
) {
711 case NEXTHOP_TYPE_IFINDEX
:
713 * Lookup if this type is special. The
714 * NEXTHOP_TYPE_IFINDEX is a pop and
715 * forward into a different table for
716 * processing. As such this ifindex
717 * passed to us may be a VRF device
718 * which will not be in the default
719 * VRF. So let's look in all of them
721 zns
= zebra_ns_lookup(NS_DEFAULT
);
722 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
723 if (ifp
&& if_is_operative(ifp
))
724 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
726 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
728 case NEXTHOP_TYPE_IPV4
:
729 case NEXTHOP_TYPE_IPV4_IFINDEX
:
730 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
731 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
736 case NEXTHOP_TYPE_IPV6
:
737 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
738 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
740 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
743 case NEXTHOP_TYPE_IPV6_IFINDEX
:
744 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
745 ifp
= if_lookup_by_index(nexthop
->ifindex
,
747 if (ifp
&& if_is_operative(ifp
))
748 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
750 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
752 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
753 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
755 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
763 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
767 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
768 * reachability and select the best. Multipath entries are also
769 * marked. This is invoked when an LSP scheduled for processing (due
770 * to some change) is examined.
772 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
774 zebra_nhlfe_t
*nhlfe
;
776 struct nexthop
*nexthop
;
784 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
787 * First compute the best path, after checking nexthop status. We are
789 * concerned with non-deleted NHLFEs.
791 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
792 /* Clear selection flags. */
793 UNSET_FLAG(nhlfe
->flags
,
794 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
796 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
797 && nhlfe_nexthop_active(nhlfe
)) {
798 if (!best
|| (nhlfe
->distance
< best
->distance
))
803 lsp
->best_nhlfe
= best
;
804 if (!lsp
->best_nhlfe
)
807 /* Mark best NHLFE as selected. */
808 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
811 * If best path exists, see if there is ECMP. While doing this, note if
813 * new (uninstalled) NHLFE has been selected, an installed entry that is
814 * still selected has a change or an installed entry is to be removed.
816 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
817 int nh_chg
, nh_sel
, nh_inst
;
819 nexthop
= nhlfe
->nexthop
;
820 if (!nexthop
) // unexpected
823 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
824 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
825 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
826 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
827 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
831 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
832 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
833 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
835 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
837 if ((nh_sel
&& !nh_inst
)
838 || (nh_sel
&& nh_inst
&& nh_chg
)
839 || (nh_inst
&& !nh_sel
))
843 /* We have finished examining, clear changed flag. */
844 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
848 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
852 * Delete LSP forwarding entry from kernel, if installed. Called upon
855 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
859 lsp
= (zebra_lsp_t
*)bucket
->data
;
860 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
861 (void)dplane_lsp_delete(lsp
);
865 * Schedule LSP forwarding entry for processing. Called upon changes
866 * that may impact LSPs such as nexthop / connected route changes.
868 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
872 lsp
= (zebra_lsp_t
*)bucket
->data
;
873 (void)lsp_processq_add(lsp
);
877 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
878 * any multipaths and update or delete from the kernel, as needed.
880 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
883 zebra_nhlfe_t
*oldbest
, *newbest
;
884 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
885 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
886 enum zebra_dplane_result res
;
888 lsp
= (zebra_lsp_t
*)data
;
889 if (!lsp
) // unexpected
892 oldbest
= lsp
->best_nhlfe
;
894 /* Select best NHLFE(s) */
895 lsp_select_best_nhlfe(lsp
);
897 newbest
= lsp
->best_nhlfe
;
899 if (IS_ZEBRA_DEBUG_MPLS
) {
901 nhlfe2str(oldbest
, buf
, BUFSIZ
);
903 nhlfe2str(newbest
, buf2
, BUFSIZ
);
905 "Process LSP in-label %u oldbest %s newbest %s "
906 "flags 0x%x ecmp# %d",
907 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
908 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
911 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
912 /* Not already installed */
915 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
917 switch (dplane_lsp_add(lsp
)) {
918 case ZEBRA_DPLANE_REQUEST_QUEUED
:
919 /* Set 'installed' flag so we will know
920 * that an install is in-flight.
922 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
924 zvrf
->lsp_installs_queued
++;
926 case ZEBRA_DPLANE_REQUEST_FAILURE
:
927 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
928 "LSP Install Failure: %u",
931 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
932 zvrf
->lsp_installs
++;
937 /* Installed, may need an update and/or delete. */
939 res
= dplane_lsp_delete(lsp
);
941 /* We do some of the lsp cleanup immediately for
944 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
945 clear_nhlfe_installed(lsp
);
948 case ZEBRA_DPLANE_REQUEST_QUEUED
:
949 zvrf
->lsp_removals_queued
++;
951 case ZEBRA_DPLANE_REQUEST_FAILURE
:
952 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
953 "LSP Deletion Failure: %u",
956 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
957 zvrf
->lsp_removals
++;
960 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
961 zebra_nhlfe_t
*nhlfe
;
962 struct nexthop
*nexthop
;
964 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
966 /* We leave the INSTALLED flag set here
967 * so we know an update in in-flight.
971 * Any NHLFE that was installed but is not
972 * selected now needs to have its flags updated.
974 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
975 nhlfe
= nhlfe
->next
) {
976 nexthop
= nhlfe
->nexthop
;
980 if (CHECK_FLAG(nhlfe
->flags
,
981 NHLFE_FLAG_INSTALLED
)
982 && !CHECK_FLAG(nhlfe
->flags
,
983 NHLFE_FLAG_SELECTED
)) {
984 UNSET_FLAG(nhlfe
->flags
,
985 NHLFE_FLAG_INSTALLED
);
986 UNSET_FLAG(nexthop
->flags
,
991 switch (dplane_lsp_update(lsp
)) {
992 case ZEBRA_DPLANE_REQUEST_QUEUED
:
993 zvrf
->lsp_installs_queued
++;
995 case ZEBRA_DPLANE_REQUEST_FAILURE
:
996 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
997 "LSP Update Failure: %u",
1000 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1001 zvrf
->lsp_installs
++;
1012 * Callback upon processing completion of a LSP forwarding entry.
1014 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1016 struct zebra_vrf
*zvrf
;
1018 struct hash
*lsp_table
;
1019 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1021 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1024 lsp_table
= zvrf
->lsp_table
;
1025 if (!lsp_table
) // unexpected
1028 lsp
= (zebra_lsp_t
*)data
;
1029 if (!lsp
) // unexpected
1032 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1034 * delete LSP entry also.
1036 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1038 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1039 nhlfe_next
= nhlfe
->next
;
1040 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1044 if (!lsp
->nhlfe_list
) {
1045 if (IS_ZEBRA_DEBUG_MPLS
)
1046 zlog_debug("Free LSP in-label %u flags 0x%x",
1047 lsp
->ile
.in_label
, lsp
->flags
);
1049 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1050 XFREE(MTYPE_LSP
, lsp
);
1055 * Callback upon finishing the processing of all scheduled
1056 * LSP forwarding entries.
1058 static void lsp_processq_complete(struct work_queue
*wq
)
1060 /* Nothing to do for now. */
1064 * Add LSP forwarding entry to queue for subsequent processing.
1066 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1068 /* If already scheduled, exit. */
1069 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1072 if (zrouter
.lsp_process_q
== NULL
) {
1073 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1074 "%s: work_queue does not exist!", __func__
);
1078 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1079 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1084 * Callback to allocate LSP forwarding table entry.
1086 static void *lsp_alloc(void *p
)
1088 const zebra_ile_t
*ile
= p
;
1091 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1094 if (IS_ZEBRA_DEBUG_MPLS
)
1095 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1097 return ((void *)lsp
);
1101 * Create printable string for NHLFE entry.
1103 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1105 struct nexthop
*nexthop
;
1108 nexthop
= nhlfe
->nexthop
;
1109 switch (nexthop
->type
) {
1110 case NEXTHOP_TYPE_IPV4
:
1111 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1112 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1114 case NEXTHOP_TYPE_IPV6
:
1115 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1116 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1118 case NEXTHOP_TYPE_IFINDEX
:
1119 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1128 * Check if NHLFE matches with search info passed.
1130 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1131 union g_addr
*gate
, ifindex_t ifindex
)
1133 struct nexthop
*nhop
;
1136 nhop
= nhlfe
->nexthop
;
1140 if (nhop
->type
!= gtype
)
1143 switch (nhop
->type
) {
1144 case NEXTHOP_TYPE_IPV4
:
1145 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1146 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1147 sizeof(struct in_addr
));
1148 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1149 cmp
= !(nhop
->ifindex
== ifindex
);
1151 case NEXTHOP_TYPE_IPV6
:
1152 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1153 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1154 sizeof(struct in6_addr
));
1155 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1156 cmp
= !(nhop
->ifindex
== ifindex
);
1158 case NEXTHOP_TYPE_IFINDEX
:
1159 cmp
= !(nhop
->ifindex
== ifindex
);
1170 * Locate NHLFE that matches with passed info.
1172 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1173 enum nexthop_types_t gtype
, union g_addr
*gate
,
1176 zebra_nhlfe_t
*nhlfe
;
1181 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1182 if (nhlfe
->type
!= lsp_type
)
1184 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1192 * Add NHLFE. Base entry must have been created and duplicate
1195 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1196 enum nexthop_types_t gtype
, union g_addr
*gate
,
1197 ifindex_t ifindex
, mpls_label_t out_label
)
1199 zebra_nhlfe_t
*nhlfe
;
1200 struct nexthop
*nexthop
;
1205 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1208 nhlfe
->type
= lsp_type
;
1209 nhlfe
->distance
= lsp_distance(lsp_type
);
1211 nexthop
= nexthop_new();
1213 XFREE(MTYPE_NHLFE
, nhlfe
);
1216 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1218 nexthop
->vrf_id
= VRF_DEFAULT
;
1219 nexthop
->type
= gtype
;
1220 switch (nexthop
->type
) {
1221 case NEXTHOP_TYPE_IPV4
:
1222 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1223 nexthop
->gate
.ipv4
= gate
->ipv4
;
1225 nexthop
->ifindex
= ifindex
;
1227 case NEXTHOP_TYPE_IPV6
:
1228 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1229 nexthop
->gate
.ipv6
= gate
->ipv6
;
1231 nexthop
->ifindex
= ifindex
;
1233 case NEXTHOP_TYPE_IFINDEX
:
1234 nexthop
->ifindex
= ifindex
;
1237 nexthop_free(nexthop
);
1238 XFREE(MTYPE_NHLFE
, nhlfe
);
1243 nhlfe
->nexthop
= nexthop
;
1244 if (lsp
->nhlfe_list
)
1245 lsp
->nhlfe_list
->prev
= nhlfe
;
1246 nhlfe
->next
= lsp
->nhlfe_list
;
1247 lsp
->nhlfe_list
= nhlfe
;
1253 * Delete NHLFE. Entry must be present on list.
1255 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1268 nexthop_free(nhlfe
->nexthop
);
1270 /* Unlink from LSP */
1272 nhlfe
->next
->prev
= nhlfe
->prev
;
1274 nhlfe
->prev
->next
= nhlfe
->next
;
1276 lsp
->nhlfe_list
= nhlfe
->next
;
1278 if (nhlfe
== lsp
->best_nhlfe
)
1279 lsp
->best_nhlfe
= NULL
;
1281 XFREE(MTYPE_NHLFE
, nhlfe
);
1287 * Update label for NHLFE entry.
1289 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1290 struct mpls_label_stack
*nh_label
)
1292 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1295 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1296 enum lsp_types_t type
)
1298 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1299 int schedule_lsp
= 0;
1302 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1303 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1304 nhlfe_next
= nhlfe
->next
;
1306 /* Skip non-static NHLFEs */
1307 if (nhlfe
->type
!= type
)
1310 if (IS_ZEBRA_DEBUG_MPLS
) {
1311 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1313 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1314 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1317 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1318 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1319 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1326 /* Queue LSP for processing, if needed, else delete. */
1328 if (lsp_processq_add(lsp
))
1330 } else if (!lsp
->nhlfe_list
1331 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1332 if (IS_ZEBRA_DEBUG_MPLS
)
1333 zlog_debug("Free LSP in-label %u flags 0x%x",
1334 lsp
->ile
.in_label
, lsp
->flags
);
1336 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1337 XFREE(MTYPE_LSP
, lsp
);
1344 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1345 * If no other NHLFEs exist, the entry would be deleted.
1347 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1348 mpls_label_t in_label
)
1350 struct hash
*lsp_table
;
1351 zebra_ile_t tmp_ile
;
1355 lsp_table
= zvrf
->lsp_table
;
1359 /* If entry is not present, exit. */
1360 tmp_ile
.in_label
= in_label
;
1361 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1362 if (!lsp
|| !lsp
->nhlfe_list
)
1365 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1368 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1371 json_object
*json_nhlfe
= NULL
;
1372 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1374 json_nhlfe
= json_object_new_object();
1375 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1376 json_object_int_add(json_nhlfe
, "outLabel",
1377 nexthop
->nh_label
->label
[0]);
1378 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1380 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1381 json_object_boolean_true_add(json_nhlfe
, "installed");
1383 switch (nexthop
->type
) {
1384 case NEXTHOP_TYPE_IPV4
:
1385 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1386 json_object_string_add(json_nhlfe
, "nexthop",
1387 inet_ntoa(nexthop
->gate
.ipv4
));
1389 case NEXTHOP_TYPE_IPV6
:
1390 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1391 json_object_string_add(
1392 json_nhlfe
, "nexthop",
1393 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1395 if (nexthop
->ifindex
)
1396 json_object_string_add(json_nhlfe
, "interface",
1397 ifindex2ifname(nexthop
->ifindex
,
1407 * Print the NHLFE for a LSP forwarding entry.
1409 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1411 struct nexthop
*nexthop
;
1414 nexthop
= nhlfe
->nexthop
;
1415 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1418 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1419 nhlfe_type2str(nhlfe
->type
),
1420 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1422 switch (nexthop
->type
) {
1423 case NEXTHOP_TYPE_IPV4
:
1424 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1425 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1426 if (nexthop
->ifindex
)
1427 vty_out(vty
, " dev %s",
1428 ifindex2ifname(nexthop
->ifindex
,
1431 case NEXTHOP_TYPE_IPV6
:
1432 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1433 vty_out(vty
, " via %s",
1434 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1435 if (nexthop
->ifindex
)
1436 vty_out(vty
, " dev %s",
1437 ifindex2ifname(nexthop
->ifindex
,
1444 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1450 * Print an LSP forwarding entry.
1452 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1454 zebra_nhlfe_t
*nhlfe
;
1457 vty
= (struct vty
*)ctxt
;
1459 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1460 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1463 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1464 nhlfe_print(nhlfe
, vty
);
1468 * JSON objects for an LSP forwarding entry.
1470 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1472 zebra_nhlfe_t
*nhlfe
= NULL
;
1473 json_object
*json
= json_object_new_object();
1474 json_object
*json_nhlfe_list
= json_object_new_array();
1476 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1478 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1479 json_object_boolean_true_add(json
, "installed");
1481 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1482 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1484 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1489 /* Return a sorted linked list of the hash contents */
1490 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1493 struct hash_bucket
*hb
;
1494 struct list
*sorted_list
= list_new();
1496 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1498 for (i
= 0; i
< hash
->size
; i
++)
1499 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1500 listnode_add_sort(sorted_list
, hb
->data
);
1506 * Compare two LSPs based on their label values.
1508 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1510 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1513 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1520 * Callback to allocate static LSP.
1522 static void *slsp_alloc(void *p
)
1524 const zebra_ile_t
*ile
= p
;
1527 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1529 return ((void *)slsp
);
1533 * Compare two static LSPs based on their label values.
1535 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1537 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1540 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1547 * Check if static NHLFE matches with search info passed.
1549 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1550 union g_addr
*gate
, ifindex_t ifindex
)
1554 if (snhlfe
->gtype
!= gtype
)
1557 switch (snhlfe
->gtype
) {
1558 case NEXTHOP_TYPE_IPV4
:
1559 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1560 sizeof(struct in_addr
));
1562 case NEXTHOP_TYPE_IPV6
:
1563 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1564 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1565 sizeof(struct in6_addr
));
1566 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1567 cmp
= !(snhlfe
->ifindex
== ifindex
);
1577 * Locate static NHLFE that matches with passed info.
1579 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1580 enum nexthop_types_t gtype
,
1581 union g_addr
*gate
, ifindex_t ifindex
)
1583 zebra_snhlfe_t
*snhlfe
;
1588 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1589 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1598 * Add static NHLFE. Base LSP config entry must have been created
1599 * and duplicate check done.
1601 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1602 enum nexthop_types_t gtype
,
1603 union g_addr
*gate
, ifindex_t ifindex
,
1604 mpls_label_t out_label
)
1606 zebra_snhlfe_t
*snhlfe
;
1611 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1612 snhlfe
->slsp
= slsp
;
1613 snhlfe
->out_label
= out_label
;
1614 snhlfe
->gtype
= gtype
;
1616 case NEXTHOP_TYPE_IPV4
:
1617 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1619 case NEXTHOP_TYPE_IPV6
:
1620 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1621 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1623 snhlfe
->ifindex
= ifindex
;
1626 XFREE(MTYPE_SNHLFE
, snhlfe
);
1630 if (slsp
->snhlfe_list
)
1631 slsp
->snhlfe_list
->prev
= snhlfe
;
1632 snhlfe
->next
= slsp
->snhlfe_list
;
1633 slsp
->snhlfe_list
= snhlfe
;
1639 * Delete static NHLFE. Entry must be present on list.
1641 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1648 slsp
= snhlfe
->slsp
;
1653 snhlfe
->next
->prev
= snhlfe
->prev
;
1655 snhlfe
->prev
->next
= snhlfe
->next
;
1657 slsp
->snhlfe_list
= snhlfe
->next
;
1659 snhlfe
->prev
= snhlfe
->next
= NULL
;
1660 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1661 XFREE(MTYPE_SNHLFE
, snhlfe
);
1667 * Delete all static NHLFE entries for this LSP (in label).
1669 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1671 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1676 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1677 snhlfe_next
= snhlfe
->next
;
1685 * Create printable string for NHLFE configuration.
1687 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1690 switch (snhlfe
->gtype
) {
1691 case NEXTHOP_TYPE_IPV4
:
1692 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1694 case NEXTHOP_TYPE_IPV6
:
1695 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1696 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1697 if (snhlfe
->ifindex
)
1699 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
),
1710 * Initialize work queue for processing changed LSPs.
1712 static int mpls_processq_init(void)
1714 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1715 if (!zrouter
.lsp_process_q
) {
1716 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1717 "%s: could not initialise work queue!", __func__
);
1721 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1722 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1723 zrouter
.lsp_process_q
->spec
.errorfunc
= NULL
;
1724 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1725 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1726 zrouter
.lsp_process_q
->spec
.hold
= 10;
1732 /* Public functions */
1735 * Process LSP update results from zebra dataplane.
1737 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1739 struct zebra_vrf
*zvrf
;
1740 zebra_ile_t tmp_ile
;
1741 struct hash
*lsp_table
;
1743 zebra_nhlfe_t
*nhlfe
;
1744 struct nexthop
*nexthop
;
1745 enum dplane_op_e op
;
1746 enum zebra_dplane_result status
;
1748 op
= dplane_ctx_get_op(ctx
);
1749 status
= dplane_ctx_get_status(ctx
);
1751 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1752 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1753 ctx
, dplane_op2str(op
),
1754 dplane_ctx_get_in_label(ctx
),
1755 dplane_res2str(status
));
1758 case DPLANE_OP_LSP_INSTALL
:
1759 case DPLANE_OP_LSP_UPDATE
:
1760 /* Look for zebra LSP object */
1761 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1765 lsp_table
= zvrf
->lsp_table
;
1767 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1768 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1770 if (IS_ZEBRA_DEBUG_DPLANE
)
1771 zlog_debug("LSP ctx %p: in-label %u not found",
1772 ctx
, dplane_ctx_get_in_label(ctx
));
1776 /* TODO -- Confirm that this result is still 'current' */
1778 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1779 /* Update zebra object */
1780 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1781 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
1782 nhlfe
= nhlfe
->next
) {
1783 nexthop
= nhlfe
->nexthop
;
1787 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1788 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1791 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1792 clear_nhlfe_installed(lsp
);
1793 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1794 "LSP Install Failure: in-label %u",
1800 case DPLANE_OP_LSP_DELETE
:
1801 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1802 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1803 "LSP Deletion Failure: in-label %u",
1804 dplane_ctx_get_in_label(ctx
));
1812 dplane_ctx_fini(&ctx
);
1816 * Process async dplane notifications.
1818 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1820 struct zebra_vrf
*zvrf
;
1821 zebra_ile_t tmp_ile
;
1822 struct hash
*lsp_table
;
1824 zebra_nhlfe_t
*nhlfe
;
1825 const zebra_nhlfe_t
*ctx_nhlfe
;
1826 struct nexthop
*nexthop
;
1827 const struct nexthop
*ctx_nexthop
;
1828 int start_count
= 0, end_count
= 0; /* Installed counts */
1829 bool changed_p
= false;
1830 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1833 zlog_debug("LSP dplane notif, in-label %u",
1834 dplane_ctx_get_in_label(ctx
));
1836 /* Look for zebra LSP object */
1837 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1841 lsp_table
= zvrf
->lsp_table
;
1843 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1844 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1847 zlog_debug("dplane LSP notif: in-label %u not found",
1848 dplane_ctx_get_in_label(ctx
));
1853 * The dataplane/forwarding plane is notifying zebra about the state
1854 * of the nexthops associated with this LSP. First, we take a
1855 * pre-scan pass to determine whether the LSP has transitioned
1856 * from installed -> uninstalled. In that case, we need to have
1857 * the existing state of the LSP objects available before making
1860 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1861 char buf
[NEXTHOP_STRLEN
];
1863 nexthop
= nhlfe
->nexthop
;
1867 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1871 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1872 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1874 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1878 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1879 nexthop_same(ctx_nexthop
, nexthop
)) {
1886 nexthop2str(nexthop
, buf
, sizeof(buf
));
1888 if (ctx_nhlfe
&& ctx_nexthop
) {
1890 const char *tstr
= "";
1892 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1893 NHLFE_FLAG_INSTALLED
))
1896 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1900 /* Test zebra nhlfe install state */
1901 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1902 NHLFE_FLAG_INSTALLED
)) {
1904 if (!CHECK_FLAG(nhlfe
->flags
,
1905 NHLFE_FLAG_INSTALLED
))
1908 /* Update counter */
1912 if (CHECK_FLAG(nhlfe
->flags
,
1913 NHLFE_FLAG_INSTALLED
))
1918 /* Not mentioned in lfib set -> uninstalled */
1919 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1920 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1921 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1926 zlog_debug("LSP dplane notif: no match, nh %s",
1932 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
1933 start_count
, end_count
,
1934 changed_p
? ", changed" : "");
1937 * Has the LSP become uninstalled?
1939 if (start_count
> 0 && end_count
== 0) {
1940 /* Inform other lfibs */
1941 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
1945 * Now we take a second pass and bring the zebra
1946 * nexthop state into sync with the forwarding-plane state.
1948 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1949 char buf
[NEXTHOP_STRLEN
];
1951 nexthop
= nhlfe
->nexthop
;
1956 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1957 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1959 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1963 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1964 nexthop_same(ctx_nexthop
, nexthop
)) {
1971 nexthop2str(nexthop
, buf
, sizeof(buf
));
1973 if (ctx_nhlfe
&& ctx_nexthop
) {
1975 /* Bring zebra nhlfe install state into sync */
1976 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1977 NHLFE_FLAG_INSTALLED
)) {
1979 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1983 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1986 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
1987 NEXTHOP_FLAG_FIB
)) {
1988 SET_FLAG(nhlfe
->nexthop
->flags
,
1989 NEXTHOP_FLAG_ACTIVE
);
1990 SET_FLAG(nhlfe
->nexthop
->flags
,
1993 UNSET_FLAG(nhlfe
->nexthop
->flags
,
1994 NEXTHOP_FLAG_ACTIVE
);
1995 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2000 /* Not mentioned in lfib set -> uninstalled */
2002 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2003 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2004 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2008 if (end_count
> 0) {
2009 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2012 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2015 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2016 clear_nhlfe_installed(lsp
);
2020 dplane_ctx_fini(&ctx
);
2024 * Install dynamic LSP entry.
2026 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2027 struct route_entry
*re
)
2029 struct route_table
*table
;
2032 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2036 /* See if there is a configured label binding for this FEC. */
2037 fec
= fec_find(table
, &rn
->p
);
2038 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2041 /* We cannot install a label forwarding entry if local label is the
2042 * implicit-null label.
2044 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2047 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2054 * Uninstall dynamic LSP entry, if any.
2056 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2057 struct route_entry
*re
)
2059 struct route_table
*table
;
2062 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2066 /* See if there is a configured label binding for this FEC. */
2067 fec
= fec_find(table
, &rn
->p
);
2068 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2071 /* Uninstall always removes all dynamic NHLFEs. */
2072 return lsp_uninstall(zvrf
, fec
->label
);
2076 * Add an NHLFE to an LSP, return the newly-added object
2078 zebra_nhlfe_t
*zebra_mpls_lsp_add_nhlfe(zebra_lsp_t
*lsp
,
2079 enum lsp_types_t lsp_type
,
2080 enum nexthop_types_t gtype
,
2083 mpls_label_t out_label
)
2085 /* Just a public pass-through to the internal implementation */
2086 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, out_label
);
2090 * Free an allocated NHLFE
2092 void zebra_mpls_nhlfe_del(zebra_nhlfe_t
*nhlfe
)
2094 /* Just a pass-through to the internal implementation */
2099 * Registration from a client for the label binding for a FEC. If a binding
2100 * already exists, it is informed to the client.
2101 * NOTE: If there is a manually configured label binding, that is used.
2102 * Otherwise, if a label index is specified, it means we have to allocate the
2103 * label from a locally configured label block (SRGB), if one exists and index
2104 * is acceptable. If no label index then just register the specified label.
2105 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2106 * by the calling function. Register requests with both will be rejected.
2108 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2109 uint32_t label
, uint32_t label_index
,
2110 struct zserv
*client
)
2112 struct route_table
*table
;
2116 bool label_change
= false;
2118 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2119 bool is_configured_fec
= false; /* indicate statically configured FEC */
2121 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2125 if (IS_ZEBRA_DEBUG_MPLS
)
2126 prefix2str(p
, buf
, BUFSIZ
);
2128 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2130 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2131 "Rejecting FEC register for %s with both label %u and Label Index %u specified, client %s",
2132 buf
, label
, label_index
,
2133 zebra_route_string(client
->proto
));
2138 fec
= fec_find(table
, p
);
2140 fec
= fec_add(table
, p
, label
, 0, label_index
);
2143 EC_ZEBRA_FEC_ADD_FAILED
,
2144 "Failed to add FEC %s upon register, client %s",
2145 buf
, zebra_route_string(client
->proto
));
2149 old_label
= MPLS_INVALID_LABEL
;
2152 /* Check if the FEC has been statically defined in the config */
2153 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2154 /* Client may register same FEC with different label index. */
2156 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2157 if (!new_client
&& fec
->label_index
== label_index
2158 && fec
->label
== label
)
2159 /* Duplicate register */
2162 /* Save current label, update the FEC */
2163 old_label
= fec
->label
;
2164 fec
->label_index
= label_index
;
2168 listnode_add(fec
->client_list
, client
);
2170 if (IS_ZEBRA_DEBUG_MPLS
)
2171 zlog_debug("FEC %s label%s %u %s by client %s%s", buf
,
2172 have_label_index
? " index" : "",
2173 have_label_index
? label_index
: label
,
2174 new_client
? "registered" : "updated",
2175 zebra_route_string(client
->proto
),
2177 ? ", but using statically configured label"
2180 /* If not a statically configured FEC, derive the local label
2181 * from label index or use the provided label
2183 if (!is_configured_fec
) {
2184 if (have_label_index
)
2185 fec_derive_label_from_index(zvrf
, fec
);
2189 /* If no label change, exit. */
2190 if (fec
->label
== old_label
)
2193 label_change
= true;
2196 /* If new client or label change, update client and install or uninstall
2197 * label forwarding entry as needed.
2199 /* Inform client of label, if needed. */
2200 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2201 if (IS_ZEBRA_DEBUG_MPLS
)
2202 zlog_debug("Update client label %u", fec
->label
);
2203 fec_send(fec
, client
);
2206 if (new_client
|| label_change
)
2207 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2213 * Deregistration from a client for the label binding for a FEC. The FEC
2214 * itself is deleted if no other registered clients exist and there is no
2215 * label bound to the FEC.
2217 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2218 struct zserv
*client
)
2220 struct route_table
*table
;
2224 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2228 if (IS_ZEBRA_DEBUG_MPLS
)
2229 prefix2str(p
, buf
, BUFSIZ
);
2231 fec
= fec_find(table
, p
);
2233 prefix2str(p
, buf
, BUFSIZ
);
2234 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2235 "Failed to find FEC %s upon unregister, client %s",
2236 buf
, zebra_route_string(client
->proto
));
2240 listnode_delete(fec
->client_list
, client
);
2242 if (IS_ZEBRA_DEBUG_MPLS
)
2243 zlog_debug("FEC %s unregistered by client %s", buf
,
2244 zebra_route_string(client
->proto
));
2246 /* If not a configured entry, delete the FEC if no other clients. Before
2247 * deleting, see if any LSP needs to be uninstalled.
2249 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2250 && list_isempty(fec
->client_list
)) {
2251 mpls_label_t old_label
= fec
->label
;
2252 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2253 fec_change_update_lsp(zvrf
, fec
, old_label
);
2261 * Cleanup any FECs registered by this client.
2263 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2265 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2266 struct route_node
*rn
;
2268 struct listnode
*node
;
2269 struct zserv
*fec_client
;
2272 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2273 if (zvrf
->fec_table
[af
] == NULL
)
2276 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2277 rn
= route_next(rn
)) {
2279 if (!fec
|| list_isempty(fec
->client_list
))
2282 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2284 if (fec_client
== client
) {
2285 listnode_delete(fec
->client_list
,
2287 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2288 && list_isempty(fec
->client_list
))
2300 * Return FEC (if any) to which this label is bound.
2301 * Note: Only works for per-prefix binding and when the label is not
2303 * TODO: Currently walks entire table, can optimize later with another
2306 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2309 struct route_node
*rn
;
2313 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2314 if (zvrf
->fec_table
[af
] == NULL
)
2317 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2318 rn
= route_next(rn
)) {
2322 if (fec
->label
== label
)
2331 * Inform if specified label is currently bound to a FEC or not.
2333 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2335 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2339 * Add static FEC to label binding. If there are clients registered for this
2340 * FEC, notify them. If there are labeled routes for this FEC, install the
2341 * label forwarding entry.
2343 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2344 mpls_label_t in_label
)
2346 struct route_table
*table
;
2349 mpls_label_t old_label
;
2352 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2356 if (IS_ZEBRA_DEBUG_MPLS
)
2357 prefix2str(p
, buf
, BUFSIZ
);
2359 /* Update existing FEC or create a new one. */
2360 fec
= fec_find(table
, p
);
2362 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2363 MPLS_INVALID_LABEL_INDEX
);
2365 prefix2str(p
, buf
, BUFSIZ
);
2366 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2367 "Failed to add FEC %s upon config", buf
);
2371 if (IS_ZEBRA_DEBUG_MPLS
)
2372 zlog_debug("Add fec %s label %u", buf
, in_label
);
2374 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2375 if (fec
->label
== in_label
)
2376 /* Duplicate config */
2379 /* Label change, update clients. */
2380 old_label
= fec
->label
;
2381 if (IS_ZEBRA_DEBUG_MPLS
)
2382 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2384 fec
->label
= in_label
;
2385 fec_update_clients(fec
);
2387 /* Update label forwarding entries appropriately */
2388 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2395 * Remove static FEC to label binding. If there are no clients registered
2396 * for this FEC, delete the FEC; else notify clients
2397 * Note: Upon delete of static binding, if label index exists for this FEC,
2398 * client may need to be updated with derived label.
2400 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2402 struct route_table
*table
;
2404 mpls_label_t old_label
;
2407 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2411 fec
= fec_find(table
, p
);
2413 prefix2str(p
, buf
, BUFSIZ
);
2414 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2415 "Failed to find FEC %s upon delete", buf
);
2419 if (IS_ZEBRA_DEBUG_MPLS
) {
2420 prefix2str(p
, buf
, BUFSIZ
);
2421 zlog_debug("Delete fec %s label %u label index %u", buf
,
2422 fec
->label
, fec
->label_index
);
2425 old_label
= fec
->label
;
2426 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2427 fec
->label
= MPLS_INVALID_LABEL
;
2429 /* If no client exists, just delete the FEC. */
2430 if (list_isempty(fec
->client_list
)) {
2435 /* Derive the local label (from label index) or reset it. */
2436 fec_derive_label_from_index(zvrf
, fec
);
2438 /* If there is a label change, update clients. */
2439 if (fec
->label
== old_label
)
2441 fec_update_clients(fec
);
2443 /* Update label forwarding entries appropriately */
2444 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2448 * Display MPLS FEC to label binding configuration (VTY command handler).
2450 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2452 struct route_node
*rn
;
2458 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2459 if (zvrf
->fec_table
[af
] == NULL
)
2462 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2463 rn
= route_next(rn
)) {
2470 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2474 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2475 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2476 label2str(fec
->label
, lstr
, BUFSIZ
));
2484 * Display MPLS FEC to label binding (VTY command handler).
2486 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2488 struct route_node
*rn
;
2491 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2492 if (zvrf
->fec_table
[af
] == NULL
)
2495 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2496 rn
= route_next(rn
)) {
2499 fec_print(rn
->info
, vty
);
2505 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2507 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2510 struct route_table
*table
;
2511 struct route_node
*rn
;
2513 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2518 rn
= route_node_lookup(table
, p
);
2522 route_unlock_node(rn
);
2526 fec_print(rn
->info
, vty
);
2529 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2530 enum lsp_types_t type
, mpls_label_t label
)
2532 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2533 nexthop_add_labels(nexthop
, type
, 1, &label
);
2534 else if (!add
&& nexthop
->nh_label_type
== type
)
2535 nexthop_del_labels(nexthop
);
2543 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2545 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2546 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2547 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2548 mpls_label_t out_label
)
2550 struct route_table
*table
;
2551 struct route_node
*rn
;
2552 struct route_entry
*re
;
2553 struct nexthop
*nexthop
;
2557 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2562 /* Lookup existing route */
2563 rn
= route_node_get(table
, prefix
);
2564 RNODE_FOREACH_RE (rn
, re
) {
2565 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2567 if (re
->distance
== distance
)
2575 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2576 switch (nexthop
->type
) {
2577 case NEXTHOP_TYPE_IPV4
:
2578 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2579 if (gtype
!= NEXTHOP_TYPE_IPV4
2580 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2582 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2584 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2585 && nexthop
->ifindex
!= ifindex
)
2587 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2592 case NEXTHOP_TYPE_IPV6
:
2593 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2594 if (gtype
!= NEXTHOP_TYPE_IPV6
2595 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2597 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2599 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2600 && nexthop
->ifindex
!= ifindex
)
2602 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2615 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2616 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2623 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2624 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2625 * the out-label for an existing NHLFE (update case).
2627 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2628 mpls_label_t in_label
, mpls_label_t out_label
,
2629 enum nexthop_types_t gtype
, union g_addr
*gate
,
2632 struct hash
*lsp_table
;
2633 zebra_ile_t tmp_ile
;
2635 zebra_nhlfe_t
*nhlfe
;
2639 lsp_table
= zvrf
->lsp_table
;
2643 /* If entry is present, exit. */
2644 tmp_ile
.in_label
= in_label
;
2645 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2648 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2650 struct nexthop
*nh
= nhlfe
->nexthop
;
2653 assert(nh
->nh_label
);
2655 /* Clear deleted flag (in case it was set) */
2656 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2657 if (nh
->nh_label
->label
[0] == out_label
)
2661 if (IS_ZEBRA_DEBUG_MPLS
) {
2662 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2664 "LSP in-label %u type %d nexthop %s "
2665 "out-label changed to %u (old %u)",
2666 in_label
, type
, buf
, out_label
,
2667 nh
->nh_label
->label
[0]);
2670 /* Update out label, trigger processing. */
2671 nh
->nh_label
->label
[0] = out_label
;
2673 /* Add LSP entry to this nexthop */
2674 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2678 if (IS_ZEBRA_DEBUG_MPLS
) {
2679 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2681 "Add LSP in-label %u type %d nexthop %s "
2683 in_label
, type
, buf
, out_label
);
2686 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2689 /* Mark NHLFE, queue LSP for processing. */
2690 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2691 if (lsp_processq_add(lsp
))
2698 * Uninstall a particular NHLFE in the forwarding table. If this is
2699 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2701 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2702 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2703 union g_addr
*gate
, ifindex_t ifindex
)
2705 struct hash
*lsp_table
;
2706 zebra_ile_t tmp_ile
;
2708 zebra_nhlfe_t
*nhlfe
;
2712 lsp_table
= zvrf
->lsp_table
;
2716 /* If entry is not present, exit. */
2717 tmp_ile
.in_label
= in_label
;
2718 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2721 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2725 if (IS_ZEBRA_DEBUG_MPLS
) {
2726 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2727 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2728 in_label
, type
, buf
, nhlfe
->flags
);
2731 /* Mark NHLFE for delete or directly delete, as appropriate. */
2732 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2733 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2734 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2735 if (lsp_processq_add(lsp
))
2740 /* Free LSP entry if no other NHLFEs and not scheduled. */
2741 if (!lsp
->nhlfe_list
2742 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2743 if (IS_ZEBRA_DEBUG_MPLS
)
2744 zlog_debug("Free LSP in-label %u flags 0x%x",
2745 lsp
->ile
.in_label
, lsp
->flags
);
2747 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2748 XFREE(MTYPE_LSP
, lsp
);
2755 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2756 * If no other NHLFEs exist, the entry would be deleted.
2758 void mpls_ldp_lsp_uninstall_all(struct hash_bucket
*bucket
, void *ctxt
)
2761 struct hash
*lsp_table
;
2763 lsp
= (zebra_lsp_t
*)bucket
->data
;
2764 if (!lsp
->nhlfe_list
)
2771 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2775 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2777 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2779 struct route_table
*table
;
2780 struct route_node
*rn
;
2781 struct route_entry
*re
;
2782 struct nexthop
*nexthop
;
2785 /* Process routes of interested address-families. */
2786 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2790 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2792 RNODE_FOREACH_RE (rn
, re
) {
2793 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2794 nexthop
= nexthop
->next
) {
2795 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2798 nexthop_del_labels(nexthop
);
2799 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2800 SET_FLAG(re
->status
,
2801 ROUTE_ENTRY_LABELS_CHANGED
);
2811 #if defined(HAVE_CUMULUS)
2813 * Check that the label values used in LSP creation are consistent. The
2814 * main criteria is that if there is ECMP, the label operation must still
2815 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2816 * to current HW restrictions.
2818 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2819 mpls_label_t in_label
,
2820 mpls_label_t out_label
,
2821 enum nexthop_types_t gtype
,
2822 union g_addr
*gate
, ifindex_t ifindex
)
2824 struct hash
*slsp_table
;
2825 zebra_ile_t tmp_ile
;
2827 zebra_snhlfe_t
*snhlfe
;
2830 slsp_table
= zvrf
->slsp_table
;
2834 /* If entry is not present, exit. */
2835 tmp_ile
.in_label
= in_label
;
2836 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2840 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2842 if (snhlfe
->out_label
== out_label
)
2845 /* If not only NHLFE, cannot allow label change. */
2846 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2849 /* If other NHLFEs exist, label operation must match. */
2850 if (slsp
->snhlfe_list
) {
2853 cur_op
= (slsp
->snhlfe_list
->out_label
2854 == MPLS_LABEL_IMPLICIT_NULL
);
2855 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2856 if (cur_op
!= new_op
)
2861 /* Label values are good. */
2864 #endif /* HAVE_CUMULUS */
2867 * Add static LSP entry. This may be the first entry for this incoming label
2868 * or an additional nexthop; an existing entry may also have outgoing label
2870 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2873 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2874 mpls_label_t out_label
,
2875 enum nexthop_types_t gtype
, union g_addr
*gate
,
2878 struct hash
*slsp_table
;
2879 zebra_ile_t tmp_ile
;
2881 zebra_snhlfe_t
*snhlfe
;
2885 slsp_table
= zvrf
->slsp_table
;
2889 /* If entry is present, exit. */
2890 tmp_ile
.in_label
= in_label
;
2891 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2894 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2896 if (snhlfe
->out_label
== out_label
)
2900 if (IS_ZEBRA_DEBUG_MPLS
) {
2901 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2903 "Upd static LSP in-label %u nexthop %s "
2904 "out-label %u (old %u)",
2905 in_label
, buf
, out_label
, snhlfe
->out_label
);
2907 snhlfe
->out_label
= out_label
;
2909 /* Add static LSP entry to this nexthop */
2910 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2914 if (IS_ZEBRA_DEBUG_MPLS
) {
2915 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2917 "Add static LSP in-label %u nexthop %s out-label %u",
2918 in_label
, buf
, out_label
);
2922 /* (Re)Install LSP in the main table. */
2923 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2931 * Delete static LSP entry. This may be the delete of one particular
2932 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2934 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2935 * LSP configuration.
2937 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2938 enum nexthop_types_t gtype
, union g_addr
*gate
,
2941 struct hash
*slsp_table
;
2942 zebra_ile_t tmp_ile
;
2944 zebra_snhlfe_t
*snhlfe
;
2947 slsp_table
= zvrf
->slsp_table
;
2951 /* If entry is not present, exit. */
2952 tmp_ile
.in_label
= in_label
;
2953 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2957 /* Is it delete of entire LSP or a specific NHLFE? */
2958 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2959 if (IS_ZEBRA_DEBUG_MPLS
)
2960 zlog_debug("Del static LSP in-label %u", in_label
);
2962 /* Uninstall entire LSP from the main table. */
2963 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2965 /* Delete all static NHLFEs */
2966 snhlfe_del_all(slsp
);
2968 /* Find specific NHLFE, exit if not found. */
2969 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2973 if (IS_ZEBRA_DEBUG_MPLS
) {
2975 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2976 zlog_debug("Del static LSP in-label %u nexthop %s",
2980 /* Uninstall LSP from the main table. */
2981 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2984 /* Delete static LSP NHLFE */
2988 /* Remove entire static LSP entry if no NHLFE - valid in either case
2990 if (!slsp
->snhlfe_list
) {
2991 slsp
= hash_release(slsp_table
, &tmp_ile
);
2992 XFREE(MTYPE_SLSP
, slsp
);
2999 * Schedule all MPLS label forwarding entries for processing.
3000 * Called upon changes that may affect one or more of them such as
3001 * interface or nexthop state changes.
3003 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3007 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3011 * Display MPLS label forwarding table for a specific LSP
3012 * (VTY command handler).
3014 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3015 mpls_label_t label
, bool use_json
)
3017 struct hash
*lsp_table
;
3019 zebra_ile_t tmp_ile
;
3020 json_object
*json
= NULL
;
3023 lsp_table
= zvrf
->lsp_table
;
3027 /* If entry is not present, exit. */
3028 tmp_ile
.in_label
= label
;
3029 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3034 json
= lsp_json(lsp
);
3035 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3036 json
, JSON_C_TO_STRING_PRETTY
));
3037 json_object_free(json
);
3039 lsp_print(lsp
, (void *)vty
);
3043 * Display MPLS label forwarding table (VTY command handler).
3045 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3049 json_object
*json
= NULL
;
3050 zebra_lsp_t
*lsp
= NULL
;
3051 zebra_nhlfe_t
*nhlfe
= NULL
;
3052 struct listnode
*node
= NULL
;
3053 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3056 json
= json_object_new_object();
3058 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3059 json_object_object_add(
3060 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
3063 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3064 json
, JSON_C_TO_STRING_PRETTY
));
3065 json_object_free(json
);
3069 /* Prepare table. */
3070 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3071 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3072 tt
->style
.cell
.rpad
= 2;
3073 tt
->style
.corner
= '+';
3075 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3077 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3078 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
3079 nhlfe
= nhlfe
->next
) {
3080 struct nexthop
*nexthop
;
3081 const char *out_label_str
;
3082 char nh_buf
[NEXTHOP_STRLEN
];
3084 nexthop
= nhlfe
->nexthop
;
3086 switch (nexthop
->type
) {
3087 case NEXTHOP_TYPE_IFINDEX
: {
3088 struct zebra_ns
*zns
;
3089 struct interface
*ifp
;
3091 zns
= zebra_ns_lookup(NS_DEFAULT
);
3092 ifp
= if_lookup_by_index_per_ns(
3093 zns
, nexthop
->ifindex
);
3094 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3095 ifp
? ifp
->name
: "Null");
3098 case NEXTHOP_TYPE_IPV4
:
3099 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3100 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3101 nh_buf
, sizeof(nh_buf
));
3103 case NEXTHOP_TYPE_IPV6
:
3104 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3105 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3106 nh_buf
, sizeof(nh_buf
));
3112 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3113 out_label_str
= mpls_label2str(
3114 nexthop
->nh_label
->num_labels
,
3115 &nexthop
->nh_label
->label
[0],
3118 out_label_str
= "-";
3120 ttable_add_row(tt
, "%u|%s|%s|%s",
3122 nhlfe_type2str(nhlfe
->type
),
3123 nh_buf
, out_label_str
);
3127 /* Dump the generated table. */
3128 if (tt
->nrows
> 1) {
3129 char *table
= ttable_dump(tt
, "\n");
3130 vty_out(vty
, "%s\n", table
);
3131 XFREE(MTYPE_TMP
, table
);
3136 list_delete(&lsp_list
);
3140 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3142 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3145 zebra_snhlfe_t
*snhlfe
;
3146 struct listnode
*node
;
3147 struct list
*slsp_list
=
3148 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
3150 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
3151 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
3152 snhlfe
= snhlfe
->next
) {
3156 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
3157 switch (snhlfe
->out_label
) {
3158 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3159 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3160 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3162 case MPLS_LABEL_IMPLICIT_NULL
:
3163 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3166 sprintf(lstr
, "%u", snhlfe
->out_label
);
3170 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
3175 list_delete(&slsp_list
);
3176 return (zvrf
->slsp_table
->count
? 1 : 0);
3180 * Add/update global label block.
3182 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3185 zvrf
->mpls_srgb
.start_label
= start_label
;
3186 zvrf
->mpls_srgb
.end_label
= end_label
;
3188 /* Evaluate registered FECs to see if any get a label or not. */
3194 * Delete global label block.
3196 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3198 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3199 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3201 /* Process registered FECs to clear their local label, if needed. */
3207 * Display MPLS global label block configuration (VTY command handler).
3209 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3211 if (zvrf
->mpls_srgb
.start_label
== 0)
3214 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3215 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3216 vty_out(vty
, "mpls label global-block %u %u\n",
3217 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3224 * Called when VRF becomes inactive, cleans up information but keeps
3227 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3229 struct zebra_vrf
*def_zvrf
;
3232 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3233 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3236 * For other vrfs, we try to remove associated LSPs; we locate
3237 * the LSPs in the default vrf.
3239 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3241 /* At shutdown, the default may be gone already */
3242 if (def_zvrf
== NULL
)
3245 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3246 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3247 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3253 * Called upon process exiting, need to delete LSP forwarding
3254 * entries from the kernel.
3255 * NOTE: Currently supported only for default VRF.
3257 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
3259 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3260 hash_clean(zvrf
->lsp_table
, NULL
);
3261 hash_free(zvrf
->lsp_table
);
3262 hash_clean(zvrf
->slsp_table
, NULL
);
3263 hash_free(zvrf
->slsp_table
);
3264 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
3265 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
3269 * Allocate MPLS tables for this VRF and do other initialization.
3270 * NOTE: Currently supported only for default VRF.
3272 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
3277 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
3278 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
3279 zvrf
->fec_table
[AFI_IP
] = route_table_init();
3280 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
3281 zvrf
->mpls_flags
= 0;
3282 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3283 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3287 * Global MPLS initialization.
3289 void zebra_mpls_init(void)
3293 if (mpls_kernel_init() < 0) {
3294 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
3295 "Disabling MPLS support (no kernel support)");
3299 if (!mpls_processq_init())
3302 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);