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"
39 #include "zebra/rib.h"
41 #include "zebra/interface.h"
42 #include "zebra/zserv.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/zebra_memory.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/zebra_mpls.h"
49 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
50 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
51 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
52 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
58 /* Default rtm_table for all clients */
59 extern struct zebra_t zebrad
;
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(void *p
);
81 static int 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_backet
*backet
, void *ctxt
);
90 static void lsp_schedule(struct hash_backet
*backet
, 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
);
128 static int mpls_processq_init(struct zebra_t
*zebra
);
131 /* Static functions */
134 * Handle failure in LSP install, clear flags for NHLFE.
136 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
138 zebra_nhlfe_t
*nhlfe
;
139 struct nexthop
*nexthop
;
141 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
142 nexthop
= nhlfe
->nexthop
;
146 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
147 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
152 * Install label forwarding entry based on labeled-route entry.
154 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
155 struct route_node
*rn
, struct route_entry
*re
)
157 struct hash
*lsp_table
;
160 zebra_nhlfe_t
*nhlfe
;
161 struct nexthop
*nexthop
;
162 enum lsp_types_t lsp_type
;
167 lsp_table
= zvrf
->lsp_table
;
171 lsp_type
= lsp_type_from_re_type(re
->type
);
174 /* Locate or allocate LSP entry. */
175 tmp_ile
.in_label
= label
;
176 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
180 /* For each active nexthop, create NHLFE. Note that we deliberately skip
181 * recursive nexthops right now, because intermediate hops won't
183 * the label advertised by the recursive nexthop (plus we don't have the
184 * logic yet to push multiple labels).
186 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
187 /* Skip inactive and recursive entries. */
188 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
190 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
193 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
196 /* Clear deleted flag (in case it was set) */
197 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
198 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
203 if (IS_ZEBRA_DEBUG_MPLS
) {
204 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
206 "LSP in-label %u type %d nexthop %s "
208 lsp
->ile
.in_label
, lsp_type
, buf
);
211 /* Update out label, trigger processing. */
212 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
213 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
216 /* Add LSP entry to this nexthop */
217 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
218 &nexthop
->gate
, nexthop
->ifindex
,
219 nexthop
->nh_label
->label
[0]);
223 if (IS_ZEBRA_DEBUG_MPLS
) {
224 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
226 "Add LSP in-label %u type %d nexthop %s "
228 lsp
->ile
.in_label
, lsp_type
, buf
,
229 nexthop
->nh_label
->label
[0]);
232 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
234 /* Mark NHLFE as changed. */
235 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
240 /* Queue LSP for processing if necessary. If no NHLFE got added (special
241 * case), delete the LSP entry; this case results in somewhat ugly
244 if (added
|| changed
) {
245 if (lsp_processq_add(lsp
))
247 } else if (!lsp
->nhlfe_list
248 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
249 if (IS_ZEBRA_DEBUG_MPLS
)
250 zlog_debug("Free LSP in-label %u flags 0x%x",
251 lsp
->ile
.in_label
, lsp
->flags
);
253 lsp
= hash_release(lsp_table
, &lsp
->ile
);
255 XFREE(MTYPE_LSP
, lsp
);
262 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
263 * NHLFEs are removed, the entire entry is deleted.
265 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
267 struct hash
*lsp_table
;
270 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
274 lsp_table
= zvrf
->lsp_table
;
278 /* If entry is not present, exit. */
279 tmp_ile
.in_label
= label
;
280 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
281 if (!lsp
|| !lsp
->nhlfe_list
)
284 /* Mark NHLFEs for delete or directly delete, as appropriate. */
285 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
286 nhlfe_next
= nhlfe
->next
;
288 /* Skip static NHLFEs */
289 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
292 if (IS_ZEBRA_DEBUG_MPLS
) {
293 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
295 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
296 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
299 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
300 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
301 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
307 /* Queue LSP for processing, if needed, else delete. */
308 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
309 if (lsp_processq_add(lsp
))
311 } else if (!lsp
->nhlfe_list
312 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
313 if (IS_ZEBRA_DEBUG_MPLS
)
314 zlog_debug("Del LSP in-label %u flags 0x%x",
315 lsp
->ile
.in_label
, lsp
->flags
);
317 lsp
= hash_release(lsp_table
, &lsp
->ile
);
319 XFREE(MTYPE_LSP
, lsp
);
326 * This function is invoked upon change to label block configuration; it
327 * will walk all registered FECs with label-index and appropriately update
328 * their local labels and trigger client updates.
330 static void fec_evaluate(struct zebra_vrf
*zvrf
)
332 struct route_node
*rn
;
334 uint32_t old_label
, new_label
;
338 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
339 if (zvrf
->fec_table
[af
] == NULL
)
342 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
343 rn
= route_next(rn
)) {
344 if ((fec
= rn
->info
) == NULL
)
347 /* Skip configured FECs and those without a label index.
349 if (fec
->flags
& FEC_FLAG_CONFIGURED
350 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
353 if (IS_ZEBRA_DEBUG_MPLS
)
354 prefix2str(&rn
->p
, buf
, BUFSIZ
);
356 /* Save old label, determine new label. */
357 old_label
= fec
->label
;
359 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
360 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
361 new_label
= MPLS_INVALID_LABEL
;
363 /* If label has changed, update FEC and clients. */
364 if (new_label
== old_label
)
367 if (IS_ZEBRA_DEBUG_MPLS
)
369 "Update fec %s new label %u upon label block",
372 fec
->label
= new_label
;
373 fec_update_clients(fec
);
375 /* Update label forwarding entries appropriately */
376 fec_change_update_lsp(zvrf
, fec
, old_label
);
382 * Derive (if possible) and update the local label for the FEC based on
383 * its label index. The index is "acceptable" if it falls within the
384 * globally configured label block (SRGB).
386 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
391 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
392 && zvrf
->mpls_srgb
.start_label
393 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
394 < zvrf
->mpls_srgb
.end_label
))
397 fec
->label
= MPLS_INVALID_LABEL
;
403 * There is a change for this FEC. Install or uninstall label forwarding
404 * entries, as appropriate.
406 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
407 mpls_label_t old_label
)
409 struct route_table
*table
;
410 struct route_node
*rn
;
411 struct route_entry
*re
;
414 /* Uninstall label forwarding entry, if previously installed. */
415 if (old_label
!= MPLS_INVALID_LABEL
416 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
417 lsp_uninstall(zvrf
, old_label
);
419 /* Install label forwarding entry corr. to new label, if needed. */
420 if (fec
->label
== MPLS_INVALID_LABEL
421 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
424 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
425 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
429 /* See if labeled route exists. */
430 rn
= route_node_lookup(table
, &fec
->rn
->p
);
434 RNODE_FOREACH_RE (rn
, re
) {
435 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
439 if (!re
|| !zebra_rib_labeled_unicast(re
))
442 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
449 * Inform about FEC to a registered client.
451 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
454 struct route_node
*rn
;
458 /* Get output stream. */
459 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
461 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
463 stream_putw(s
, rn
->p
.family
);
464 stream_put_prefix(s
, &rn
->p
);
465 stream_putl(s
, fec
->label
);
466 stream_putw_at(s
, 0, stream_get_endp(s
));
467 return zserv_send_message(client
, s
);
471 * Update all registered clients about this FEC. Caller should've updated
472 * FEC and ensure no duplicate updates.
474 static void fec_update_clients(zebra_fec_t
*fec
)
476 struct listnode
*node
;
477 struct zserv
*client
;
479 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
480 if (IS_ZEBRA_DEBUG_MPLS
)
481 zlog_debug("Update client %s",
482 zebra_route_string(client
->proto
));
483 fec_send(fec
, client
);
489 * Print a FEC-label binding entry.
491 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
493 struct route_node
*rn
;
494 struct listnode
*node
;
495 struct zserv
*client
;
499 prefix2str(&rn
->p
, buf
, BUFSIZ
);
500 vty_out(vty
, "%s\n", buf
);
501 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
502 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
503 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
505 if (!list_isempty(fec
->client_list
)) {
506 vty_out(vty
, " Client list:");
507 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
508 vty_out(vty
, " %s(fd %d)",
509 zebra_route_string(client
->proto
),
516 * Locate FEC-label binding that matches with passed info.
518 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
520 struct route_node
*rn
;
523 rn
= route_node_lookup(table
, p
);
527 route_unlock_node(rn
);
532 * Add a FEC. This may be upon a client registering for a binding
533 * or when a binding is configured.
535 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
536 mpls_label_t label
, uint32_t flags
,
537 uint32_t label_index
)
539 struct route_node
*rn
;
544 /* Lookup (or add) route node.*/
545 rn
= route_node_get(table
, p
);
552 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
559 fec
->client_list
= list_new();
561 route_unlock_node(rn
); /* for the route_node_get */
563 fec
->label_index
= label_index
;
570 * Delete a FEC. This may be upon the last client deregistering for
571 * a FEC and no binding exists or when the binding is deleted and there
572 * are no registered clients.
574 static int fec_del(zebra_fec_t
*fec
)
576 list_delete_and_null(&fec
->client_list
);
577 fec
->rn
->info
= NULL
;
578 route_unlock_node(fec
->rn
);
579 XFREE(MTYPE_FEC
, fec
);
584 * Hash function for label.
586 static unsigned int label_hash(void *p
)
588 const zebra_ile_t
*ile
= p
;
590 return (jhash_1word(ile
->in_label
, 0));
594 * Compare 2 LSP hash entries based on in-label.
596 static int label_cmp(const void *p1
, const void *p2
)
598 const zebra_ile_t
*ile1
= p1
;
599 const zebra_ile_t
*ile2
= p2
;
601 return (ile1
->in_label
== ile2
->in_label
);
605 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
607 * NOTE: Looking only for connected routes right now.
609 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
610 struct nexthop
*nexthop
)
612 struct route_table
*table
;
613 struct prefix_ipv4 p
;
614 struct route_node
*rn
;
615 struct route_entry
*match
;
616 struct nexthop
*match_nh
;
618 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
622 /* Lookup nexthop in IPv4 routing table. */
623 memset(&p
, 0, sizeof(struct prefix_ipv4
));
625 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
626 p
.prefix
= nexthop
->gate
.ipv4
;
628 rn
= route_node_match(table
, (struct prefix
*)&p
);
632 route_unlock_node(rn
);
634 /* Locate a valid connected route. */
635 RNODE_FOREACH_RE (rn
, match
) {
636 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
637 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
640 for (match_nh
= match
->ng
.nexthop
; match_nh
;
641 match_nh
= match_nh
->next
) {
642 if (match
->type
== ZEBRA_ROUTE_CONNECT
643 || nexthop
->ifindex
== match_nh
->ifindex
) {
644 nexthop
->ifindex
= match_nh
->ifindex
;
655 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
657 * NOTE: Looking only for connected routes right now.
659 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
660 struct nexthop
*nexthop
)
662 struct route_table
*table
;
663 struct prefix_ipv6 p
;
664 struct route_node
*rn
;
665 struct route_entry
*match
;
667 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
671 /* Lookup nexthop in IPv6 routing table. */
672 memset(&p
, 0, sizeof(struct prefix_ipv6
));
674 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
675 p
.prefix
= nexthop
->gate
.ipv6
;
677 rn
= route_node_match(table
, (struct prefix
*)&p
);
681 route_unlock_node(rn
);
683 /* Locate a valid connected route. */
684 RNODE_FOREACH_RE (rn
, match
) {
685 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
686 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
687 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
691 if (!match
|| !match
->ng
.nexthop
)
694 nexthop
->ifindex
= match
->ng
.nexthop
->ifindex
;
700 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
702 * NOTE: Each NHLFE points to only 1 nexthop.
704 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
706 struct nexthop
*nexthop
;
707 struct interface
*ifp
;
708 struct zebra_ns
*zns
;
710 nexthop
= nhlfe
->nexthop
;
711 if (!nexthop
) // unexpected
714 /* Check on nexthop based on type. */
715 switch (nexthop
->type
) {
716 case NEXTHOP_TYPE_IFINDEX
:
718 * Lookup if this type is special. The
719 * NEXTHOP_TYPE_IFINDEX is a pop and
720 * forward into a different table for
721 * processing. As such this ifindex
722 * passed to us may be a VRF device
723 * which will not be in the default
724 * VRF. So let's look in all of them
726 zns
= zebra_ns_lookup(NS_DEFAULT
);
727 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
728 if (ifp
&& if_is_operative(ifp
))
729 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 case NEXTHOP_TYPE_IPV4
:
734 case NEXTHOP_TYPE_IPV4_IFINDEX
:
735 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
736 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
738 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
741 case NEXTHOP_TYPE_IPV6
:
742 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
743 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
745 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
748 case NEXTHOP_TYPE_IPV6_IFINDEX
:
749 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
750 ifp
= if_lookup_by_index(nexthop
->ifindex
,
752 if (ifp
&& if_is_operative(ifp
))
753 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
755 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
757 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
758 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
760 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
768 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
772 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
773 * reachability and select the best. Multipath entries are also
774 * marked. This is invoked when an LSP scheduled for processing (due
775 * to some change) is examined.
777 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
779 zebra_nhlfe_t
*nhlfe
;
781 struct nexthop
*nexthop
;
789 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
792 * First compute the best path, after checking nexthop status. We are
794 * concerned with non-deleted NHLFEs.
796 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
797 /* Clear selection flags. */
798 UNSET_FLAG(nhlfe
->flags
,
799 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
801 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
802 && nhlfe_nexthop_active(nhlfe
)) {
803 if (!best
|| (nhlfe
->distance
< best
->distance
))
808 lsp
->best_nhlfe
= best
;
809 if (!lsp
->best_nhlfe
)
812 /* Mark best NHLFE as selected. */
813 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
816 * If best path exists, see if there is ECMP. While doing this, note if
818 * new (uninstalled) NHLFE has been selected, an installed entry that is
819 * still selected has a change or an installed entry is to be removed.
821 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
822 int nh_chg
, nh_sel
, nh_inst
;
824 nexthop
= nhlfe
->nexthop
;
825 if (!nexthop
) // unexpected
828 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
829 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
830 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
831 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
832 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
836 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
837 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
838 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
840 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
842 if ((nh_sel
&& !nh_inst
)
843 || (nh_sel
&& nh_inst
&& nh_chg
)
844 || (nh_inst
&& !nh_sel
))
848 /* We have finished examining, clear changed flag. */
849 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
853 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
857 * Delete LSP forwarding entry from kernel, if installed. Called upon
860 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
)
864 lsp
= (zebra_lsp_t
*)backet
->data
;
865 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
866 (void)kernel_del_lsp(lsp
);
870 * Schedule LSP forwarding entry for processing. Called upon changes
871 * that may impact LSPs such as nexthop / connected route changes.
873 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
)
877 lsp
= (zebra_lsp_t
*)backet
->data
;
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
)
888 zebra_nhlfe_t
*oldbest
, *newbest
;
889 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
890 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
892 lsp
= (zebra_lsp_t
*)data
;
893 if (!lsp
) // unexpected
896 oldbest
= lsp
->best_nhlfe
;
898 /* Select best NHLFE(s) */
899 lsp_select_best_nhlfe(lsp
);
901 newbest
= lsp
->best_nhlfe
;
903 if (IS_ZEBRA_DEBUG_MPLS
) {
905 nhlfe2str(oldbest
, buf
, BUFSIZ
);
907 nhlfe2str(newbest
, buf2
, BUFSIZ
);
909 "Process LSP in-label %u oldbest %s newbest %s "
910 "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
);
920 switch (kernel_add_lsp(lsp
)) {
921 case DP_REQUEST_QUEUED
:
922 zlog_err("No current DataPlane interfaces can return this, please fix");
924 case DP_REQUEST_FAILURE
:
926 case DP_REQUEST_SUCCESS
:
927 zvrf
->lsp_installs
++;
932 /* Installed, may need an update and/or delete. */
935 switch (kernel_del_lsp(lsp
)) {
936 case DP_REQUEST_QUEUED
:
937 zlog_err("No current DataPlane interfaces can return this, please fix");
939 case DP_REQUEST_FAILURE
:
941 case DP_REQUEST_SUCCESS
:
942 zvrf
->lsp_removals
++;
945 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
946 zebra_nhlfe_t
*nhlfe
;
947 struct nexthop
*nexthop
;
949 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
950 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
953 * Any NHLFE that was installed but is not
954 * selected now needs to have its flags updated.
956 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
957 nhlfe
= nhlfe
->next
) {
958 nexthop
= nhlfe
->nexthop
;
962 if (CHECK_FLAG(nhlfe
->flags
,
963 NHLFE_FLAG_INSTALLED
)
964 && !CHECK_FLAG(nhlfe
->flags
,
965 NHLFE_FLAG_SELECTED
)) {
966 UNSET_FLAG(nhlfe
->flags
,
967 NHLFE_FLAG_INSTALLED
);
968 UNSET_FLAG(nexthop
->flags
,
973 switch (kernel_upd_lsp(lsp
)) {
974 case DP_REQUEST_QUEUED
:
975 zlog_err("No current DataPlane interfaces can return this, please fix");
977 case DP_REQUEST_FAILURE
:
979 case DP_REQUEST_SUCCESS
:
980 zvrf
->lsp_installs
++;
991 * Callback upon processing completion of a LSP forwarding entry.
993 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
995 struct zebra_vrf
*zvrf
;
997 struct hash
*lsp_table
;
998 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1000 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1003 lsp_table
= zvrf
->lsp_table
;
1004 if (!lsp_table
) // unexpected
1007 lsp
= (zebra_lsp_t
*)data
;
1008 if (!lsp
) // unexpected
1011 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1013 * delete LSP entry also.
1015 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1017 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1018 nhlfe_next
= nhlfe
->next
;
1019 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1023 if (!lsp
->nhlfe_list
) {
1024 if (IS_ZEBRA_DEBUG_MPLS
)
1025 zlog_debug("Free LSP in-label %u flags 0x%x",
1026 lsp
->ile
.in_label
, lsp
->flags
);
1028 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1030 XFREE(MTYPE_LSP
, lsp
);
1035 * Callback upon finishing the processing of all scheduled
1036 * LSP forwarding entries.
1038 static void lsp_processq_complete(struct work_queue
*wq
)
1040 /* Nothing to do for now. */
1044 * Add LSP forwarding entry to queue for subsequent processing.
1046 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1048 /* If already scheduled, exit. */
1049 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1052 if (zebrad
.lsp_process_q
== NULL
) {
1053 zlog_err("%s: work_queue does not exist!", __func__
);
1057 work_queue_add(zebrad
.lsp_process_q
, lsp
);
1058 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1063 * Callback to allocate LSP forwarding table entry.
1065 static void *lsp_alloc(void *p
)
1067 const zebra_ile_t
*ile
= p
;
1070 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1073 if (IS_ZEBRA_DEBUG_MPLS
)
1074 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1076 return ((void *)lsp
);
1080 * Create printable string for NHLFE entry.
1082 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1084 struct nexthop
*nexthop
;
1087 nexthop
= nhlfe
->nexthop
;
1088 switch (nexthop
->type
) {
1089 case NEXTHOP_TYPE_IPV4
:
1090 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1091 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1093 case NEXTHOP_TYPE_IPV6
:
1094 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1096 case NEXTHOP_TYPE_IFINDEX
:
1097 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1106 * Check if NHLFE matches with search info passed.
1108 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1109 union g_addr
*gate
, ifindex_t ifindex
)
1111 struct nexthop
*nhop
;
1114 nhop
= nhlfe
->nexthop
;
1118 if (nhop
->type
!= gtype
)
1121 switch (nhop
->type
) {
1122 case NEXTHOP_TYPE_IPV4
:
1123 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1124 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1125 sizeof(struct in_addr
));
1126 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1127 cmp
= !(nhop
->ifindex
== ifindex
);
1129 case NEXTHOP_TYPE_IPV6
:
1130 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1131 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1132 sizeof(struct in6_addr
));
1133 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1134 cmp
= !(nhop
->ifindex
== ifindex
);
1136 case NEXTHOP_TYPE_IFINDEX
:
1137 cmp
= !(nhop
->ifindex
== ifindex
);
1148 * Locate NHLFE that matches with passed info.
1150 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1151 enum nexthop_types_t gtype
, union g_addr
*gate
,
1154 zebra_nhlfe_t
*nhlfe
;
1159 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1160 if (nhlfe
->type
!= lsp_type
)
1162 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1170 * Add NHLFE. Base entry must have been created and duplicate
1173 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1174 enum nexthop_types_t gtype
, union g_addr
*gate
,
1175 ifindex_t ifindex
, mpls_label_t out_label
)
1177 zebra_nhlfe_t
*nhlfe
;
1178 struct nexthop
*nexthop
;
1183 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1188 nhlfe
->type
= lsp_type
;
1189 nhlfe
->distance
= lsp_distance(lsp_type
);
1191 nexthop
= nexthop_new();
1193 XFREE(MTYPE_NHLFE
, nhlfe
);
1196 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1198 nexthop
->vrf_id
= VRF_DEFAULT
;
1199 nexthop
->type
= gtype
;
1200 switch (nexthop
->type
) {
1201 case NEXTHOP_TYPE_IPV4
:
1202 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1203 nexthop
->gate
.ipv4
= gate
->ipv4
;
1205 nexthop
->ifindex
= ifindex
;
1207 case NEXTHOP_TYPE_IPV6
:
1208 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1209 nexthop
->gate
.ipv6
= gate
->ipv6
;
1211 nexthop
->ifindex
= ifindex
;
1213 case NEXTHOP_TYPE_IFINDEX
:
1214 nexthop
->ifindex
= ifindex
;
1217 nexthop_free(nexthop
);
1218 XFREE(MTYPE_NHLFE
, nhlfe
);
1223 nhlfe
->nexthop
= nexthop
;
1224 if (lsp
->nhlfe_list
)
1225 lsp
->nhlfe_list
->prev
= nhlfe
;
1226 nhlfe
->next
= lsp
->nhlfe_list
;
1227 lsp
->nhlfe_list
= nhlfe
;
1233 * Delete NHLFE. Entry must be present on list.
1235 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1248 nexthop_free(nhlfe
->nexthop
);
1250 /* Unlink from LSP */
1252 nhlfe
->next
->prev
= nhlfe
->prev
;
1254 nhlfe
->prev
->next
= nhlfe
->next
;
1256 lsp
->nhlfe_list
= nhlfe
->next
;
1258 if (nhlfe
== lsp
->best_nhlfe
)
1259 lsp
->best_nhlfe
= NULL
;
1261 XFREE(MTYPE_NHLFE
, nhlfe
);
1267 * Update label for NHLFE entry.
1269 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1270 struct mpls_label_stack
*nh_label
)
1272 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1275 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1276 enum lsp_types_t type
)
1278 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1279 int schedule_lsp
= 0;
1282 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1283 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1284 nhlfe_next
= nhlfe
->next
;
1286 /* Skip non-static NHLFEs */
1287 if (nhlfe
->type
!= type
)
1290 if (IS_ZEBRA_DEBUG_MPLS
) {
1291 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1293 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1294 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1297 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1298 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1299 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1306 /* Queue LSP for processing, if needed, else delete. */
1308 if (lsp_processq_add(lsp
))
1310 } else if (!lsp
->nhlfe_list
1311 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1312 if (IS_ZEBRA_DEBUG_MPLS
)
1313 zlog_debug("Free LSP in-label %u flags 0x%x",
1314 lsp
->ile
.in_label
, lsp
->flags
);
1316 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1318 XFREE(MTYPE_LSP
, lsp
);
1325 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1326 * If no other NHLFEs exist, the entry would be deleted.
1328 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1329 mpls_label_t in_label
)
1331 struct hash
*lsp_table
;
1332 zebra_ile_t tmp_ile
;
1336 lsp_table
= zvrf
->lsp_table
;
1340 /* If entry is not present, exit. */
1341 tmp_ile
.in_label
= in_label
;
1342 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1343 if (!lsp
|| !lsp
->nhlfe_list
)
1346 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1349 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1352 json_object
*json_nhlfe
= NULL
;
1353 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1355 json_nhlfe
= json_object_new_object();
1356 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1357 json_object_int_add(json_nhlfe
, "outLabel",
1358 nexthop
->nh_label
->label
[0]);
1359 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1361 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1362 json_object_boolean_true_add(json_nhlfe
, "installed");
1364 switch (nexthop
->type
) {
1365 case NEXTHOP_TYPE_IPV4
:
1366 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1367 json_object_string_add(json_nhlfe
, "nexthop",
1368 inet_ntoa(nexthop
->gate
.ipv4
));
1370 case NEXTHOP_TYPE_IPV6
:
1371 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1372 json_object_string_add(
1373 json_nhlfe
, "nexthop",
1374 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1376 if (nexthop
->ifindex
)
1377 json_object_string_add(json_nhlfe
, "interface",
1378 ifindex2ifname(nexthop
->ifindex
,
1388 * Print the NHLFE for a LSP forwarding entry.
1390 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1392 struct nexthop
*nexthop
;
1395 nexthop
= nhlfe
->nexthop
;
1396 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1399 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1400 nhlfe_type2str(nhlfe
->type
),
1401 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1403 switch (nexthop
->type
) {
1404 case NEXTHOP_TYPE_IPV4
:
1405 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1406 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1407 if (nexthop
->ifindex
)
1408 vty_out(vty
, " dev %s",
1409 ifindex2ifname(nexthop
->ifindex
,
1412 case NEXTHOP_TYPE_IPV6
:
1413 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1414 vty_out(vty
, " via %s",
1415 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1416 if (nexthop
->ifindex
)
1417 vty_out(vty
, " dev %s",
1418 ifindex2ifname(nexthop
->ifindex
,
1425 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1431 * Print an LSP forwarding entry.
1433 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1435 zebra_nhlfe_t
*nhlfe
;
1438 vty
= (struct vty
*)ctxt
;
1440 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1441 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1444 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1445 nhlfe_print(nhlfe
, vty
);
1449 * JSON objects for an LSP forwarding entry.
1451 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1453 zebra_nhlfe_t
*nhlfe
= NULL
;
1454 json_object
*json
= json_object_new_object();
1455 json_object
*json_nhlfe_list
= json_object_new_array();
1457 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1459 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1460 json_object_boolean_true_add(json
, "installed");
1462 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1463 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1465 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1470 /* Return a sorted linked list of the hash contents */
1471 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1474 struct hash_backet
*hb
;
1475 struct list
*sorted_list
= list_new();
1477 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1479 for (i
= 0; i
< hash
->size
; i
++)
1480 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1481 listnode_add_sort(sorted_list
, hb
->data
);
1487 * Compare two LSPs based on their label values.
1489 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1491 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1494 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1501 * Callback to allocate static LSP.
1503 static void *slsp_alloc(void *p
)
1505 const zebra_ile_t
*ile
= p
;
1508 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1510 return ((void *)slsp
);
1514 * Compare two static LSPs based on their label values.
1516 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1518 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1521 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1528 * Check if static NHLFE matches with search info passed.
1530 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1531 union g_addr
*gate
, ifindex_t ifindex
)
1535 if (snhlfe
->gtype
!= gtype
)
1538 switch (snhlfe
->gtype
) {
1539 case NEXTHOP_TYPE_IPV4
:
1540 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1541 sizeof(struct in_addr
));
1543 case NEXTHOP_TYPE_IPV6
:
1544 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1545 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1546 sizeof(struct in6_addr
));
1547 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1548 cmp
= !(snhlfe
->ifindex
== ifindex
);
1558 * Locate static NHLFE that matches with passed info.
1560 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1561 enum nexthop_types_t gtype
,
1562 union g_addr
*gate
, ifindex_t ifindex
)
1564 zebra_snhlfe_t
*snhlfe
;
1569 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1570 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1579 * Add static NHLFE. Base LSP config entry must have been created
1580 * and duplicate check done.
1582 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1583 enum nexthop_types_t gtype
,
1584 union g_addr
*gate
, ifindex_t ifindex
,
1585 mpls_label_t out_label
)
1587 zebra_snhlfe_t
*snhlfe
;
1592 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1593 snhlfe
->slsp
= slsp
;
1594 snhlfe
->out_label
= out_label
;
1595 snhlfe
->gtype
= gtype
;
1597 case NEXTHOP_TYPE_IPV4
:
1598 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1600 case NEXTHOP_TYPE_IPV6
:
1601 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1602 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1604 snhlfe
->ifindex
= ifindex
;
1607 XFREE(MTYPE_SNHLFE
, snhlfe
);
1611 if (slsp
->snhlfe_list
)
1612 slsp
->snhlfe_list
->prev
= snhlfe
;
1613 snhlfe
->next
= slsp
->snhlfe_list
;
1614 slsp
->snhlfe_list
= snhlfe
;
1620 * Delete static NHLFE. Entry must be present on list.
1622 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1629 slsp
= snhlfe
->slsp
;
1634 snhlfe
->next
->prev
= snhlfe
->prev
;
1636 snhlfe
->prev
->next
= snhlfe
->next
;
1638 slsp
->snhlfe_list
= snhlfe
->next
;
1640 snhlfe
->prev
= snhlfe
->next
= NULL
;
1642 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1643 XFREE(MTYPE_SNHLFE
, snhlfe
);
1649 * Delete all static NHLFE entries for this LSP (in label).
1651 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1653 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1658 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1659 snhlfe_next
= snhlfe
->next
;
1667 * Create printable string for NHLFE configuration.
1669 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1672 switch (snhlfe
->gtype
) {
1673 case NEXTHOP_TYPE_IPV4
:
1674 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1676 case NEXTHOP_TYPE_IPV6
:
1677 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1678 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1679 if (snhlfe
->ifindex
)
1681 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1691 * Initialize work queue for processing changed LSPs.
1693 static int mpls_processq_init(struct zebra_t
*zebra
)
1695 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1696 if (!zebra
->lsp_process_q
) {
1697 zlog_err("%s: could not initialise work queue!", __func__
);
1701 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1702 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1703 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1704 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1705 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1706 zebra
->lsp_process_q
->spec
.hold
= 10;
1712 /* Public functions */
1714 void kernel_lsp_pass_fail(zebra_lsp_t
*lsp
, enum dp_results res
)
1716 struct nexthop
*nexthop
;
1717 zebra_nhlfe_t
*nhlfe
;
1723 case DP_INSTALL_FAILURE
:
1724 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1725 clear_nhlfe_installed(lsp
);
1726 zlog_warn("LSP Install Failure: %u", lsp
->ile
.in_label
);
1728 case DP_INSTALL_SUCCESS
:
1729 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1730 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1731 nexthop
= nhlfe
->nexthop
;
1735 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1736 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1739 case DP_DELETE_SUCCESS
:
1740 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1741 clear_nhlfe_installed(lsp
);
1743 case DP_DELETE_FAILURE
:
1744 zlog_warn("LSP Deletion Failure: %u", lsp
->ile
.in_label
);
1750 * Install dynamic LSP entry.
1752 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1753 struct route_entry
*re
)
1755 struct route_table
*table
;
1758 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1762 /* See if there is a configured label binding for this FEC. */
1763 fec
= fec_find(table
, &rn
->p
);
1764 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1767 /* We cannot install a label forwarding entry if local label is the
1768 * implicit-null label.
1770 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
1773 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1780 * Uninstall dynamic LSP entry, if any.
1782 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1783 struct route_entry
*re
)
1785 struct route_table
*table
;
1788 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1792 /* See if there is a configured label binding for this FEC. */
1793 fec
= fec_find(table
, &rn
->p
);
1794 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1797 /* Uninstall always removes all dynamic NHLFEs. */
1798 return lsp_uninstall(zvrf
, fec
->label
);
1802 * Registration from a client for the label binding for a FEC. If a binding
1803 * already exists, it is informed to the client.
1804 * NOTE: If there is a manually configured label binding, that is used.
1805 * Otherwise, if a label index is specified, it means we have to allocate the
1806 * label from a locally configured label block (SRGB), if one exists and index
1809 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1810 uint32_t label_index
, struct zserv
*client
)
1812 struct route_table
*table
;
1816 int label_change
= 0;
1819 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1823 if (IS_ZEBRA_DEBUG_MPLS
)
1824 prefix2str(p
, buf
, BUFSIZ
);
1827 fec
= fec_find(table
, p
);
1829 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1831 prefix2str(p
, buf
, BUFSIZ
);
1833 "Failed to add FEC %s upon register, client %s",
1834 buf
, zebra_route_string(client
->proto
));
1838 old_label
= MPLS_INVALID_LABEL
;
1841 /* Client may register same FEC with different label index. */
1843 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1844 if (!new_client
&& fec
->label_index
== label_index
)
1845 /* Duplicate register */
1848 /* Save current label, update label index */
1849 old_label
= fec
->label
;
1850 fec
->label_index
= label_index
;
1854 listnode_add(fec
->client_list
, client
);
1856 if (IS_ZEBRA_DEBUG_MPLS
)
1857 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1858 label_index
, new_client
? "registered" : "updated",
1859 zebra_route_string(client
->proto
));
1861 /* If not a configured FEC, derive the local label (from label index)
1864 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1865 fec_derive_label_from_index(zvrf
, fec
);
1867 /* If no label change, exit. */
1868 if (fec
->label
== old_label
)
1874 /* If new client or label change, update client and install or uninstall
1875 * label forwarding entry as needed.
1877 /* Inform client of label, if needed. */
1878 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1879 if (IS_ZEBRA_DEBUG_MPLS
)
1880 zlog_debug("Update client label %u", fec
->label
);
1881 fec_send(fec
, client
);
1884 if (new_client
|| label_change
)
1885 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1891 * Deregistration from a client for the label binding for a FEC. The FEC
1892 * itself is deleted if no other registered clients exist and there is no
1893 * label bound to the FEC.
1895 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1896 struct zserv
*client
)
1898 struct route_table
*table
;
1902 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1906 if (IS_ZEBRA_DEBUG_MPLS
)
1907 prefix2str(p
, buf
, BUFSIZ
);
1909 fec
= fec_find(table
, p
);
1911 prefix2str(p
, buf
, BUFSIZ
);
1912 zlog_err("Failed to find FEC %s upon unregister, client %s",
1913 buf
, zebra_route_string(client
->proto
));
1917 listnode_delete(fec
->client_list
, client
);
1919 if (IS_ZEBRA_DEBUG_MPLS
)
1920 zlog_debug("FEC %s unregistered by client %s", buf
,
1921 zebra_route_string(client
->proto
));
1923 /* If not a configured entry, delete the FEC if no other clients. Before
1924 * deleting, see if any LSP needs to be uninstalled.
1926 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1927 && list_isempty(fec
->client_list
)) {
1928 mpls_label_t old_label
= fec
->label
;
1929 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1930 fec_change_update_lsp(zvrf
, fec
, old_label
);
1938 * Cleanup any FECs registered by this client.
1940 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
1942 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1943 struct route_node
*rn
;
1945 struct listnode
*node
;
1946 struct zserv
*fec_client
;
1949 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1950 if (zvrf
->fec_table
[af
] == NULL
)
1953 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1954 rn
= route_next(rn
)) {
1956 if (!fec
|| list_isempty(fec
->client_list
))
1959 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1961 if (fec_client
== client
) {
1962 listnode_delete(fec
->client_list
,
1964 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1965 && list_isempty(fec
->client_list
))
1977 * Return FEC (if any) to which this label is bound.
1978 * Note: Only works for per-prefix binding and when the label is not
1980 * TODO: Currently walks entire table, can optimize later with another
1983 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1986 struct route_node
*rn
;
1990 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1991 if (zvrf
->fec_table
[af
] == NULL
)
1994 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1995 rn
= route_next(rn
)) {
1999 if (fec
->label
== label
)
2008 * Inform if specified label is currently bound to a FEC or not.
2010 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2012 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2016 * Add static FEC to label binding. If there are clients registered for this
2017 * FEC, notify them. If there are labeled routes for this FEC, install the
2018 * label forwarding entry.
2020 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2021 mpls_label_t in_label
)
2023 struct route_table
*table
;
2026 mpls_label_t old_label
;
2029 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2033 if (IS_ZEBRA_DEBUG_MPLS
)
2034 prefix2str(p
, buf
, BUFSIZ
);
2036 /* Update existing FEC or create a new one. */
2037 fec
= fec_find(table
, p
);
2039 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2040 MPLS_INVALID_LABEL_INDEX
);
2042 prefix2str(p
, buf
, BUFSIZ
);
2043 zlog_err("Failed to add FEC %s upon config", buf
);
2047 if (IS_ZEBRA_DEBUG_MPLS
)
2048 zlog_debug("Add fec %s label %u", buf
, in_label
);
2050 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2051 if (fec
->label
== in_label
)
2052 /* Duplicate config */
2055 /* Label change, update clients. */
2056 old_label
= fec
->label
;
2057 if (IS_ZEBRA_DEBUG_MPLS
)
2058 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2060 fec
->label
= in_label
;
2061 fec_update_clients(fec
);
2063 /* Update label forwarding entries appropriately */
2064 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2071 * Remove static FEC to label binding. If there are no clients registered
2072 * for this FEC, delete the FEC; else notify clients
2073 * Note: Upon delete of static binding, if label index exists for this FEC,
2074 * client may need to be updated with derived label.
2076 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2078 struct route_table
*table
;
2080 mpls_label_t old_label
;
2083 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2087 fec
= fec_find(table
, p
);
2089 prefix2str(p
, buf
, BUFSIZ
);
2090 zlog_err("Failed to find FEC %s upon delete", buf
);
2094 if (IS_ZEBRA_DEBUG_MPLS
) {
2095 prefix2str(p
, buf
, BUFSIZ
);
2096 zlog_debug("Delete fec %s label index %u", buf
,
2100 old_label
= fec
->label
;
2101 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2102 fec
->label
= MPLS_INVALID_LABEL
;
2104 /* If no client exists, just delete the FEC. */
2105 if (list_isempty(fec
->client_list
)) {
2110 /* Derive the local label (from label index) or reset it. */
2111 fec_derive_label_from_index(zvrf
, fec
);
2113 /* If there is a label change, update clients. */
2114 if (fec
->label
== old_label
)
2116 fec_update_clients(fec
);
2118 /* Update label forwarding entries appropriately */
2119 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2123 * Display MPLS FEC to label binding configuration (VTY command handler).
2125 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2127 struct route_node
*rn
;
2133 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2134 if (zvrf
->fec_table
[af
] == NULL
)
2137 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2138 rn
= route_next(rn
)) {
2145 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2149 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2150 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2151 label2str(fec
->label
, lstr
, BUFSIZ
));
2159 * Display MPLS FEC to label binding (VTY command handler).
2161 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2163 struct route_node
*rn
;
2166 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2167 if (zvrf
->fec_table
[af
] == NULL
)
2170 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2171 rn
= route_next(rn
)) {
2174 fec_print(rn
->info
, vty
);
2180 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2182 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2185 struct route_table
*table
;
2186 struct route_node
*rn
;
2188 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2193 rn
= route_node_lookup(table
, p
);
2197 route_unlock_node(rn
);
2201 fec_print(rn
->info
, vty
);
2204 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2205 enum lsp_types_t type
, mpls_label_t label
)
2207 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2208 nexthop_add_labels(nexthop
, type
, 1, &label
);
2209 else if (!add
&& nexthop
->nh_label_type
== type
)
2210 nexthop_del_labels(nexthop
);
2218 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2220 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2221 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2222 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2223 mpls_label_t out_label
)
2225 struct route_table
*table
;
2226 struct route_node
*rn
;
2227 struct route_entry
*re
;
2228 struct nexthop
*nexthop
;
2232 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2237 /* Lookup existing route */
2238 rn
= route_node_get(table
, prefix
);
2239 RNODE_FOREACH_RE (rn
, re
) {
2240 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2242 if (re
->distance
== distance
)
2250 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2251 switch (nexthop
->type
) {
2252 case NEXTHOP_TYPE_IPV4
:
2253 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2254 if (gtype
!= NEXTHOP_TYPE_IPV4
2255 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2257 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2259 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2260 && nexthop
->ifindex
!= ifindex
)
2262 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2267 case NEXTHOP_TYPE_IPV6
:
2268 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2269 if (gtype
!= NEXTHOP_TYPE_IPV6
2270 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2272 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2274 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2275 && nexthop
->ifindex
!= ifindex
)
2277 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2290 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2291 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2298 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2299 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2300 * the out-label for an existing NHLFE (update case).
2302 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2303 mpls_label_t in_label
, mpls_label_t out_label
,
2304 enum nexthop_types_t gtype
, union g_addr
*gate
,
2307 struct hash
*lsp_table
;
2308 zebra_ile_t tmp_ile
;
2310 zebra_nhlfe_t
*nhlfe
;
2314 lsp_table
= zvrf
->lsp_table
;
2318 /* If entry is present, exit. */
2319 tmp_ile
.in_label
= in_label
;
2320 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2323 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2325 struct nexthop
*nh
= nhlfe
->nexthop
;
2328 assert(nh
->nh_label
);
2330 /* Clear deleted flag (in case it was set) */
2331 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2332 if (nh
->nh_label
->label
[0] == out_label
)
2336 if (IS_ZEBRA_DEBUG_MPLS
) {
2337 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2339 "LSP in-label %u type %d nexthop %s "
2340 "out-label changed to %u (old %u)",
2341 in_label
, type
, buf
, out_label
,
2342 nh
->nh_label
->label
[0]);
2345 /* Update out label, trigger processing. */
2346 nh
->nh_label
->label
[0] = out_label
;
2348 /* Add LSP entry to this nexthop */
2349 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2353 if (IS_ZEBRA_DEBUG_MPLS
) {
2354 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2356 "Add LSP in-label %u type %d nexthop %s "
2358 in_label
, type
, buf
, out_label
);
2361 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2364 /* Mark NHLFE, queue LSP for processing. */
2365 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2366 if (lsp_processq_add(lsp
))
2373 * Uninstall a particular NHLFE in the forwarding table. If this is
2374 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2376 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2377 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2378 union g_addr
*gate
, ifindex_t ifindex
)
2380 struct hash
*lsp_table
;
2381 zebra_ile_t tmp_ile
;
2383 zebra_nhlfe_t
*nhlfe
;
2387 lsp_table
= zvrf
->lsp_table
;
2391 /* If entry is not present, exit. */
2392 tmp_ile
.in_label
= in_label
;
2393 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2396 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2400 if (IS_ZEBRA_DEBUG_MPLS
) {
2401 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2402 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2403 in_label
, type
, buf
, nhlfe
->flags
);
2406 /* Mark NHLFE for delete or directly delete, as appropriate. */
2407 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2408 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2409 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2410 if (lsp_processq_add(lsp
))
2415 /* Free LSP entry if no other NHLFEs and not scheduled. */
2416 if (!lsp
->nhlfe_list
2417 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2418 if (IS_ZEBRA_DEBUG_MPLS
)
2419 zlog_debug("Free LSP in-label %u flags 0x%x",
2420 lsp
->ile
.in_label
, lsp
->flags
);
2422 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2424 XFREE(MTYPE_LSP
, lsp
);
2431 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2432 * If no other NHLFEs exist, the entry would be deleted.
2434 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2437 struct hash
*lsp_table
;
2439 lsp
= (zebra_lsp_t
*)backet
->data
;
2440 if (!lsp
|| !lsp
->nhlfe_list
)
2447 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2451 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2453 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2455 struct route_table
*table
;
2456 struct route_node
*rn
;
2457 struct route_entry
*re
;
2458 struct nexthop
*nexthop
;
2461 /* Process routes of interested address-families. */
2462 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2466 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2468 RNODE_FOREACH_RE (rn
, re
) {
2469 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2470 nexthop
= nexthop
->next
) {
2471 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2474 nexthop_del_labels(nexthop
);
2475 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2476 SET_FLAG(re
->status
,
2477 ROUTE_ENTRY_LABELS_CHANGED
);
2487 #if defined(HAVE_CUMULUS)
2489 * Check that the label values used in LSP creation are consistent. The
2490 * main criteria is that if there is ECMP, the label operation must still
2491 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2492 * to current HW restrictions.
2494 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2495 mpls_label_t in_label
,
2496 mpls_label_t out_label
,
2497 enum nexthop_types_t gtype
,
2498 union g_addr
*gate
, ifindex_t ifindex
)
2500 struct hash
*slsp_table
;
2501 zebra_ile_t tmp_ile
;
2503 zebra_snhlfe_t
*snhlfe
;
2506 slsp_table
= zvrf
->slsp_table
;
2510 /* If entry is not present, exit. */
2511 tmp_ile
.in_label
= in_label
;
2512 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2516 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2518 if (snhlfe
->out_label
== out_label
)
2521 /* If not only NHLFE, cannot allow label change. */
2522 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2525 /* If other NHLFEs exist, label operation must match. */
2526 if (slsp
->snhlfe_list
) {
2529 cur_op
= (slsp
->snhlfe_list
->out_label
2530 == MPLS_LABEL_IMPLICIT_NULL
);
2531 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2532 if (cur_op
!= new_op
)
2537 /* Label values are good. */
2540 #endif /* HAVE_CUMULUS */
2543 * Add static LSP entry. This may be the first entry for this incoming label
2544 * or an additional nexthop; an existing entry may also have outgoing label
2546 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2549 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2550 mpls_label_t out_label
,
2551 enum nexthop_types_t gtype
, union g_addr
*gate
,
2554 struct hash
*slsp_table
;
2555 zebra_ile_t tmp_ile
;
2557 zebra_snhlfe_t
*snhlfe
;
2561 slsp_table
= zvrf
->slsp_table
;
2565 /* If entry is present, exit. */
2566 tmp_ile
.in_label
= in_label
;
2567 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2570 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2572 if (snhlfe
->out_label
== out_label
)
2576 if (IS_ZEBRA_DEBUG_MPLS
) {
2577 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2579 "Upd static LSP in-label %u nexthop %s "
2580 "out-label %u (old %u)",
2581 in_label
, buf
, out_label
, snhlfe
->out_label
);
2583 snhlfe
->out_label
= out_label
;
2585 /* Add static LSP entry to this nexthop */
2586 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2590 if (IS_ZEBRA_DEBUG_MPLS
) {
2591 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2593 "Add static LSP in-label %u nexthop %s out-label %u",
2594 in_label
, buf
, out_label
);
2598 /* (Re)Install LSP in the main table. */
2599 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2607 * Delete static LSP entry. This may be the delete of one particular
2608 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2610 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2611 * LSP configuration.
2613 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2614 enum nexthop_types_t gtype
, union g_addr
*gate
,
2617 struct hash
*slsp_table
;
2618 zebra_ile_t tmp_ile
;
2620 zebra_snhlfe_t
*snhlfe
;
2623 slsp_table
= zvrf
->slsp_table
;
2627 /* If entry is not present, exit. */
2628 tmp_ile
.in_label
= in_label
;
2629 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2633 /* Is it delete of entire LSP or a specific NHLFE? */
2634 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2635 if (IS_ZEBRA_DEBUG_MPLS
)
2636 zlog_debug("Del static LSP in-label %u", in_label
);
2638 /* Uninstall entire LSP from the main table. */
2639 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2641 /* Delete all static NHLFEs */
2642 snhlfe_del_all(slsp
);
2644 /* Find specific NHLFE, exit if not found. */
2645 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2649 if (IS_ZEBRA_DEBUG_MPLS
) {
2651 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2652 zlog_debug("Del static LSP in-label %u nexthop %s",
2656 /* Uninstall LSP from the main table. */
2657 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2660 /* Delete static LSP NHLFE */
2664 /* Remove entire static LSP entry if no NHLFE - valid in either case
2666 if (!slsp
->snhlfe_list
) {
2667 slsp
= hash_release(slsp_table
, &tmp_ile
);
2669 XFREE(MTYPE_SLSP
, slsp
);
2676 * Schedule all MPLS label forwarding entries for processing.
2677 * Called upon changes that may affect one or more of them such as
2678 * interface or nexthop state changes.
2680 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2684 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2688 * Display MPLS label forwarding table for a specific LSP
2689 * (VTY command handler).
2691 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2692 mpls_label_t label
, uint8_t use_json
)
2694 struct hash
*lsp_table
;
2696 zebra_ile_t tmp_ile
;
2697 json_object
*json
= NULL
;
2700 lsp_table
= zvrf
->lsp_table
;
2704 /* If entry is not present, exit. */
2705 tmp_ile
.in_label
= label
;
2706 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2711 json
= lsp_json(lsp
);
2712 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2713 json
, JSON_C_TO_STRING_PRETTY
));
2714 json_object_free(json
);
2716 lsp_print(lsp
, (void *)vty
);
2720 * Display MPLS label forwarding table (VTY command handler).
2722 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2726 json_object
*json
= NULL
;
2727 zebra_lsp_t
*lsp
= NULL
;
2728 zebra_nhlfe_t
*nhlfe
= NULL
;
2729 struct nexthop
*nexthop
= NULL
;
2730 struct listnode
*node
= NULL
;
2731 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2734 json
= json_object_new_object();
2736 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2737 json_object_object_add(
2738 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2741 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2742 json
, JSON_C_TO_STRING_PRETTY
));
2743 json_object_free(json
);
2745 vty_out(vty
, " Inbound Outbound\n");
2746 vty_out(vty
, " Label Type Nexthop Label\n");
2747 vty_out(vty
, "-------- ------- --------------- --------\n");
2749 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2750 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2751 nhlfe
= nhlfe
->next
) {
2752 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2753 nhlfe_type2str(nhlfe
->type
));
2754 nexthop
= nhlfe
->nexthop
;
2756 switch (nexthop
->type
) {
2757 case NEXTHOP_TYPE_IFINDEX
: {
2758 struct zebra_ns
*zns
;
2759 struct interface
*ifp
;
2761 zns
= zebra_ns_lookup(NS_DEFAULT
);
2762 ifp
= if_lookup_by_index_per_ns(
2765 vty_out(vty
, "%15s", ifp
->name
);
2768 case NEXTHOP_TYPE_IPV4
:
2769 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2770 vty_out(vty
, "%15s",
2771 inet_ntoa(nexthop
->gate
.ipv4
));
2773 case NEXTHOP_TYPE_IPV6
:
2774 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2775 vty_out(vty
, "%15s",
2777 &nexthop
->gate
.ipv6
,
2784 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
2785 vty_out(vty
, " %8s\n",
2800 list_delete_and_null(&lsp_list
);
2804 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2806 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2809 zebra_snhlfe_t
*snhlfe
;
2810 struct listnode
*node
;
2811 struct list
*slsp_list
=
2812 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2814 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2815 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2816 snhlfe
= snhlfe
->next
) {
2820 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2821 switch (snhlfe
->out_label
) {
2822 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2823 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
2824 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2826 case MPLS_LABEL_IMPLICIT_NULL
:
2827 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2830 sprintf(lstr
, "%u", snhlfe
->out_label
);
2834 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2839 list_delete_and_null(&slsp_list
);
2840 return (zvrf
->slsp_table
->count
? 1 : 0);
2844 * Add/update global label block.
2846 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
2849 zvrf
->mpls_srgb
.start_label
= start_label
;
2850 zvrf
->mpls_srgb
.end_label
= end_label
;
2852 /* Evaluate registered FECs to see if any get a label or not. */
2858 * Delete global label block.
2860 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2862 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2863 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2865 /* Process registered FECs to clear their local label, if needed. */
2871 * Display MPLS global label block configuration (VTY command handler).
2873 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2875 if (zvrf
->mpls_srgb
.start_label
== 0)
2878 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2879 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2880 vty_out(vty
, "mpls label global-block %u %u\n",
2881 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2888 * Called when VRF becomes inactive, cleans up information but keeps
2890 * NOTE: Currently supported only for default VRF.
2892 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
2894 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2898 * Called upon process exiting, need to delete LSP forwarding
2899 * entries from the kernel.
2900 * NOTE: Currently supported only for default VRF.
2902 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2904 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2905 hash_clean(zvrf
->lsp_table
, NULL
);
2906 hash_free(zvrf
->lsp_table
);
2907 hash_clean(zvrf
->slsp_table
, NULL
);
2908 hash_free(zvrf
->slsp_table
);
2909 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2910 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2914 * Allocate MPLS tables for this VRF and do other initialization.
2915 * NOTE: Currently supported only for default VRF.
2917 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2922 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
2923 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
2924 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2925 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2926 zvrf
->mpls_flags
= 0;
2927 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2928 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2932 * Global MPLS initialization.
2934 void zebra_mpls_init(void)
2938 if (mpls_kernel_init() < 0) {
2939 zlog_warn("Disabling MPLS support (no kernel support)");
2943 if (!mpls_processq_init(&zebrad
))
2946 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);