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
));
557 fec
->client_list
= list_new();
559 route_unlock_node(rn
); /* for the route_node_get */
561 fec
->label_index
= label_index
;
568 * Delete a FEC. This may be upon the last client deregistering for
569 * a FEC and no binding exists or when the binding is deleted and there
570 * are no registered clients.
572 static int fec_del(zebra_fec_t
*fec
)
574 list_delete_and_null(&fec
->client_list
);
575 fec
->rn
->info
= NULL
;
576 route_unlock_node(fec
->rn
);
577 XFREE(MTYPE_FEC
, fec
);
582 * Hash function for label.
584 static unsigned int label_hash(void *p
)
586 const zebra_ile_t
*ile
= p
;
588 return (jhash_1word(ile
->in_label
, 0));
592 * Compare 2 LSP hash entries based on in-label.
594 static int label_cmp(const void *p1
, const void *p2
)
596 const zebra_ile_t
*ile1
= p1
;
597 const zebra_ile_t
*ile2
= p2
;
599 return (ile1
->in_label
== ile2
->in_label
);
603 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
605 * NOTE: Looking only for connected routes right now.
607 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
608 struct nexthop
*nexthop
)
610 struct route_table
*table
;
611 struct prefix_ipv4 p
;
612 struct route_node
*rn
;
613 struct route_entry
*match
;
614 struct nexthop
*match_nh
;
616 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
620 /* Lookup nexthop in IPv4 routing table. */
621 memset(&p
, 0, sizeof(struct prefix_ipv4
));
623 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
624 p
.prefix
= nexthop
->gate
.ipv4
;
626 rn
= route_node_match(table
, (struct prefix
*)&p
);
630 route_unlock_node(rn
);
632 /* Locate a valid connected route. */
633 RNODE_FOREACH_RE (rn
, match
) {
634 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
635 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
638 for (match_nh
= match
->ng
.nexthop
; match_nh
;
639 match_nh
= match_nh
->next
) {
640 if (match
->type
== ZEBRA_ROUTE_CONNECT
641 || nexthop
->ifindex
== match_nh
->ifindex
) {
642 nexthop
->ifindex
= match_nh
->ifindex
;
653 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
655 * NOTE: Looking only for connected routes right now.
657 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
658 struct nexthop
*nexthop
)
660 struct route_table
*table
;
661 struct prefix_ipv6 p
;
662 struct route_node
*rn
;
663 struct route_entry
*match
;
665 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
669 /* Lookup nexthop in IPv6 routing table. */
670 memset(&p
, 0, sizeof(struct prefix_ipv6
));
672 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
673 p
.prefix
= nexthop
->gate
.ipv6
;
675 rn
= route_node_match(table
, (struct prefix
*)&p
);
679 route_unlock_node(rn
);
681 /* Locate a valid connected route. */
682 RNODE_FOREACH_RE (rn
, match
) {
683 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
684 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
685 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
689 if (!match
|| !match
->ng
.nexthop
)
692 nexthop
->ifindex
= match
->ng
.nexthop
->ifindex
;
698 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
700 * NOTE: Each NHLFE points to only 1 nexthop.
702 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
704 struct nexthop
*nexthop
;
705 struct interface
*ifp
;
706 struct zebra_ns
*zns
;
708 nexthop
= nhlfe
->nexthop
;
709 if (!nexthop
) // unexpected
712 /* Check on nexthop based on type. */
713 switch (nexthop
->type
) {
714 case NEXTHOP_TYPE_IFINDEX
:
716 * Lookup if this type is special. The
717 * NEXTHOP_TYPE_IFINDEX is a pop and
718 * forward into a different table for
719 * processing. As such this ifindex
720 * passed to us may be a VRF device
721 * which will not be in the default
722 * VRF. So let's look in all of them
724 zns
= zebra_ns_lookup(NS_DEFAULT
);
725 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
726 if (ifp
&& if_is_operative(ifp
))
727 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
729 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 case NEXTHOP_TYPE_IPV4
:
732 case NEXTHOP_TYPE_IPV4_IFINDEX
:
733 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
734 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
736 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
739 case NEXTHOP_TYPE_IPV6
:
740 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
741 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
743 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
746 case NEXTHOP_TYPE_IPV6_IFINDEX
:
747 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
748 ifp
= if_lookup_by_index(nexthop
->ifindex
,
750 if (ifp
&& if_is_operative(ifp
))
751 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
753 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
755 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
756 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
758 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
766 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
770 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
771 * reachability and select the best. Multipath entries are also
772 * marked. This is invoked when an LSP scheduled for processing (due
773 * to some change) is examined.
775 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
777 zebra_nhlfe_t
*nhlfe
;
779 struct nexthop
*nexthop
;
787 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
790 * First compute the best path, after checking nexthop status. We are
792 * concerned with non-deleted NHLFEs.
794 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
795 /* Clear selection flags. */
796 UNSET_FLAG(nhlfe
->flags
,
797 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
799 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
800 && nhlfe_nexthop_active(nhlfe
)) {
801 if (!best
|| (nhlfe
->distance
< best
->distance
))
806 lsp
->best_nhlfe
= best
;
807 if (!lsp
->best_nhlfe
)
810 /* Mark best NHLFE as selected. */
811 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
814 * If best path exists, see if there is ECMP. While doing this, note if
816 * new (uninstalled) NHLFE has been selected, an installed entry that is
817 * still selected has a change or an installed entry is to be removed.
819 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
820 int nh_chg
, nh_sel
, nh_inst
;
822 nexthop
= nhlfe
->nexthop
;
823 if (!nexthop
) // unexpected
826 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
827 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
828 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
829 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
830 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
834 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
835 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
836 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
838 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
840 if ((nh_sel
&& !nh_inst
)
841 || (nh_sel
&& nh_inst
&& nh_chg
)
842 || (nh_inst
&& !nh_sel
))
846 /* We have finished examining, clear changed flag. */
847 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
851 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
855 * Delete LSP forwarding entry from kernel, if installed. Called upon
858 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
)
862 lsp
= (zebra_lsp_t
*)backet
->data
;
863 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
864 (void)kernel_del_lsp(lsp
);
868 * Schedule LSP forwarding entry for processing. Called upon changes
869 * that may impact LSPs such as nexthop / connected route changes.
871 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
)
875 lsp
= (zebra_lsp_t
*)backet
->data
;
876 (void)lsp_processq_add(lsp
);
880 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
881 * any multipaths and update or delete from the kernel, as needed.
883 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
886 zebra_nhlfe_t
*oldbest
, *newbest
;
887 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
888 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
890 lsp
= (zebra_lsp_t
*)data
;
891 if (!lsp
) // unexpected
894 oldbest
= lsp
->best_nhlfe
;
896 /* Select best NHLFE(s) */
897 lsp_select_best_nhlfe(lsp
);
899 newbest
= lsp
->best_nhlfe
;
901 if (IS_ZEBRA_DEBUG_MPLS
) {
903 nhlfe2str(oldbest
, buf
, BUFSIZ
);
905 nhlfe2str(newbest
, buf2
, BUFSIZ
);
907 "Process LSP in-label %u oldbest %s newbest %s "
908 "flags 0x%x ecmp# %d",
909 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
910 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
913 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
914 /* Not already installed */
917 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
918 switch (kernel_add_lsp(lsp
)) {
919 case DP_REQUEST_QUEUED
:
920 zlog_err("No current DataPlane interfaces can return this, please fix");
922 case DP_REQUEST_FAILURE
:
924 case DP_REQUEST_SUCCESS
:
925 zvrf
->lsp_installs
++;
930 /* Installed, may need an update and/or delete. */
933 switch (kernel_del_lsp(lsp
)) {
934 case DP_REQUEST_QUEUED
:
935 zlog_err("No current DataPlane interfaces can return this, please fix");
937 case DP_REQUEST_FAILURE
:
939 case DP_REQUEST_SUCCESS
:
940 zvrf
->lsp_removals
++;
943 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
944 zebra_nhlfe_t
*nhlfe
;
945 struct nexthop
*nexthop
;
947 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
948 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
951 * Any NHLFE that was installed but is not
952 * selected now needs to have its flags updated.
954 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
955 nhlfe
= nhlfe
->next
) {
956 nexthop
= nhlfe
->nexthop
;
960 if (CHECK_FLAG(nhlfe
->flags
,
961 NHLFE_FLAG_INSTALLED
)
962 && !CHECK_FLAG(nhlfe
->flags
,
963 NHLFE_FLAG_SELECTED
)) {
964 UNSET_FLAG(nhlfe
->flags
,
965 NHLFE_FLAG_INSTALLED
);
966 UNSET_FLAG(nexthop
->flags
,
971 switch (kernel_upd_lsp(lsp
)) {
972 case DP_REQUEST_QUEUED
:
973 zlog_err("No current DataPlane interfaces can return this, please fix");
975 case DP_REQUEST_FAILURE
:
977 case DP_REQUEST_SUCCESS
:
978 zvrf
->lsp_installs
++;
989 * Callback upon processing completion of a LSP forwarding entry.
991 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
993 struct zebra_vrf
*zvrf
;
995 struct hash
*lsp_table
;
996 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
998 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1001 lsp_table
= zvrf
->lsp_table
;
1002 if (!lsp_table
) // unexpected
1005 lsp
= (zebra_lsp_t
*)data
;
1006 if (!lsp
) // unexpected
1009 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1011 * delete LSP entry also.
1013 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1015 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1016 nhlfe_next
= nhlfe
->next
;
1017 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1021 if (!lsp
->nhlfe_list
) {
1022 if (IS_ZEBRA_DEBUG_MPLS
)
1023 zlog_debug("Free LSP in-label %u flags 0x%x",
1024 lsp
->ile
.in_label
, lsp
->flags
);
1026 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1028 XFREE(MTYPE_LSP
, lsp
);
1033 * Callback upon finishing the processing of all scheduled
1034 * LSP forwarding entries.
1036 static void lsp_processq_complete(struct work_queue
*wq
)
1038 /* Nothing to do for now. */
1042 * Add LSP forwarding entry to queue for subsequent processing.
1044 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1046 /* If already scheduled, exit. */
1047 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1050 if (zebrad
.lsp_process_q
== NULL
) {
1051 zlog_err("%s: work_queue does not exist!", __func__
);
1055 work_queue_add(zebrad
.lsp_process_q
, lsp
);
1056 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1061 * Callback to allocate LSP forwarding table entry.
1063 static void *lsp_alloc(void *p
)
1065 const zebra_ile_t
*ile
= p
;
1068 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1071 if (IS_ZEBRA_DEBUG_MPLS
)
1072 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1074 return ((void *)lsp
);
1078 * Create printable string for NHLFE entry.
1080 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1082 struct nexthop
*nexthop
;
1085 nexthop
= nhlfe
->nexthop
;
1086 switch (nexthop
->type
) {
1087 case NEXTHOP_TYPE_IPV4
:
1088 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1089 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1091 case NEXTHOP_TYPE_IPV6
:
1092 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1094 case NEXTHOP_TYPE_IFINDEX
:
1095 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1104 * Check if NHLFE matches with search info passed.
1106 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1107 union g_addr
*gate
, ifindex_t ifindex
)
1109 struct nexthop
*nhop
;
1112 nhop
= nhlfe
->nexthop
;
1116 if (nhop
->type
!= gtype
)
1119 switch (nhop
->type
) {
1120 case NEXTHOP_TYPE_IPV4
:
1121 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1122 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1123 sizeof(struct in_addr
));
1124 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1125 cmp
= !(nhop
->ifindex
== ifindex
);
1127 case NEXTHOP_TYPE_IPV6
:
1128 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1129 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1130 sizeof(struct in6_addr
));
1131 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1132 cmp
= !(nhop
->ifindex
== ifindex
);
1134 case NEXTHOP_TYPE_IFINDEX
:
1135 cmp
= !(nhop
->ifindex
== ifindex
);
1146 * Locate NHLFE that matches with passed info.
1148 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1149 enum nexthop_types_t gtype
, union g_addr
*gate
,
1152 zebra_nhlfe_t
*nhlfe
;
1157 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1158 if (nhlfe
->type
!= lsp_type
)
1160 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1168 * Add NHLFE. Base entry must have been created and duplicate
1171 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1172 enum nexthop_types_t gtype
, union g_addr
*gate
,
1173 ifindex_t ifindex
, mpls_label_t out_label
)
1175 zebra_nhlfe_t
*nhlfe
;
1176 struct nexthop
*nexthop
;
1181 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1184 nhlfe
->type
= lsp_type
;
1185 nhlfe
->distance
= lsp_distance(lsp_type
);
1187 nexthop
= nexthop_new();
1189 XFREE(MTYPE_NHLFE
, nhlfe
);
1192 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1194 nexthop
->vrf_id
= VRF_DEFAULT
;
1195 nexthop
->type
= gtype
;
1196 switch (nexthop
->type
) {
1197 case NEXTHOP_TYPE_IPV4
:
1198 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1199 nexthop
->gate
.ipv4
= gate
->ipv4
;
1201 nexthop
->ifindex
= ifindex
;
1203 case NEXTHOP_TYPE_IPV6
:
1204 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1205 nexthop
->gate
.ipv6
= gate
->ipv6
;
1207 nexthop
->ifindex
= ifindex
;
1209 case NEXTHOP_TYPE_IFINDEX
:
1210 nexthop
->ifindex
= ifindex
;
1213 nexthop_free(nexthop
);
1214 XFREE(MTYPE_NHLFE
, nhlfe
);
1219 nhlfe
->nexthop
= nexthop
;
1220 if (lsp
->nhlfe_list
)
1221 lsp
->nhlfe_list
->prev
= nhlfe
;
1222 nhlfe
->next
= lsp
->nhlfe_list
;
1223 lsp
->nhlfe_list
= nhlfe
;
1229 * Delete NHLFE. Entry must be present on list.
1231 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1244 nexthop_free(nhlfe
->nexthop
);
1246 /* Unlink from LSP */
1248 nhlfe
->next
->prev
= nhlfe
->prev
;
1250 nhlfe
->prev
->next
= nhlfe
->next
;
1252 lsp
->nhlfe_list
= nhlfe
->next
;
1254 if (nhlfe
== lsp
->best_nhlfe
)
1255 lsp
->best_nhlfe
= NULL
;
1257 XFREE(MTYPE_NHLFE
, nhlfe
);
1263 * Update label for NHLFE entry.
1265 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1266 struct mpls_label_stack
*nh_label
)
1268 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1271 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1272 enum lsp_types_t type
)
1274 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1275 int schedule_lsp
= 0;
1278 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1279 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1280 nhlfe_next
= nhlfe
->next
;
1282 /* Skip non-static NHLFEs */
1283 if (nhlfe
->type
!= type
)
1286 if (IS_ZEBRA_DEBUG_MPLS
) {
1287 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1289 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1290 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1293 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1294 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1295 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1302 /* Queue LSP for processing, if needed, else delete. */
1304 if (lsp_processq_add(lsp
))
1306 } else if (!lsp
->nhlfe_list
1307 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1308 if (IS_ZEBRA_DEBUG_MPLS
)
1309 zlog_debug("Free LSP in-label %u flags 0x%x",
1310 lsp
->ile
.in_label
, lsp
->flags
);
1312 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1314 XFREE(MTYPE_LSP
, lsp
);
1321 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1322 * If no other NHLFEs exist, the entry would be deleted.
1324 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1325 mpls_label_t in_label
)
1327 struct hash
*lsp_table
;
1328 zebra_ile_t tmp_ile
;
1332 lsp_table
= zvrf
->lsp_table
;
1336 /* If entry is not present, exit. */
1337 tmp_ile
.in_label
= in_label
;
1338 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1339 if (!lsp
|| !lsp
->nhlfe_list
)
1342 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1345 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1348 json_object
*json_nhlfe
= NULL
;
1349 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1351 json_nhlfe
= json_object_new_object();
1352 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1353 json_object_int_add(json_nhlfe
, "outLabel",
1354 nexthop
->nh_label
->label
[0]);
1355 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1357 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1358 json_object_boolean_true_add(json_nhlfe
, "installed");
1360 switch (nexthop
->type
) {
1361 case NEXTHOP_TYPE_IPV4
:
1362 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1363 json_object_string_add(json_nhlfe
, "nexthop",
1364 inet_ntoa(nexthop
->gate
.ipv4
));
1366 case NEXTHOP_TYPE_IPV6
:
1367 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1368 json_object_string_add(
1369 json_nhlfe
, "nexthop",
1370 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1372 if (nexthop
->ifindex
)
1373 json_object_string_add(json_nhlfe
, "interface",
1374 ifindex2ifname(nexthop
->ifindex
,
1384 * Print the NHLFE for a LSP forwarding entry.
1386 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1388 struct nexthop
*nexthop
;
1391 nexthop
= nhlfe
->nexthop
;
1392 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1395 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1396 nhlfe_type2str(nhlfe
->type
),
1397 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1399 switch (nexthop
->type
) {
1400 case NEXTHOP_TYPE_IPV4
:
1401 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1402 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1403 if (nexthop
->ifindex
)
1404 vty_out(vty
, " dev %s",
1405 ifindex2ifname(nexthop
->ifindex
,
1408 case NEXTHOP_TYPE_IPV6
:
1409 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1410 vty_out(vty
, " via %s",
1411 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1412 if (nexthop
->ifindex
)
1413 vty_out(vty
, " dev %s",
1414 ifindex2ifname(nexthop
->ifindex
,
1421 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1427 * Print an LSP forwarding entry.
1429 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1431 zebra_nhlfe_t
*nhlfe
;
1434 vty
= (struct vty
*)ctxt
;
1436 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1437 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1440 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1441 nhlfe_print(nhlfe
, vty
);
1445 * JSON objects for an LSP forwarding entry.
1447 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1449 zebra_nhlfe_t
*nhlfe
= NULL
;
1450 json_object
*json
= json_object_new_object();
1451 json_object
*json_nhlfe_list
= json_object_new_array();
1453 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1455 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1456 json_object_boolean_true_add(json
, "installed");
1458 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1459 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1461 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1466 /* Return a sorted linked list of the hash contents */
1467 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1470 struct hash_backet
*hb
;
1471 struct list
*sorted_list
= list_new();
1473 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1475 for (i
= 0; i
< hash
->size
; i
++)
1476 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1477 listnode_add_sort(sorted_list
, hb
->data
);
1483 * Compare two LSPs based on their label values.
1485 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1487 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1490 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1497 * Callback to allocate static LSP.
1499 static void *slsp_alloc(void *p
)
1501 const zebra_ile_t
*ile
= p
;
1504 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1506 return ((void *)slsp
);
1510 * Compare two static LSPs based on their label values.
1512 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1514 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1517 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1524 * Check if static NHLFE matches with search info passed.
1526 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1527 union g_addr
*gate
, ifindex_t ifindex
)
1531 if (snhlfe
->gtype
!= gtype
)
1534 switch (snhlfe
->gtype
) {
1535 case NEXTHOP_TYPE_IPV4
:
1536 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1537 sizeof(struct in_addr
));
1539 case NEXTHOP_TYPE_IPV6
:
1540 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1541 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1542 sizeof(struct in6_addr
));
1543 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1544 cmp
= !(snhlfe
->ifindex
== ifindex
);
1554 * Locate static NHLFE that matches with passed info.
1556 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1557 enum nexthop_types_t gtype
,
1558 union g_addr
*gate
, ifindex_t ifindex
)
1560 zebra_snhlfe_t
*snhlfe
;
1565 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1566 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1575 * Add static NHLFE. Base LSP config entry must have been created
1576 * and duplicate check done.
1578 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1579 enum nexthop_types_t gtype
,
1580 union g_addr
*gate
, ifindex_t ifindex
,
1581 mpls_label_t out_label
)
1583 zebra_snhlfe_t
*snhlfe
;
1588 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1589 snhlfe
->slsp
= slsp
;
1590 snhlfe
->out_label
= out_label
;
1591 snhlfe
->gtype
= gtype
;
1593 case NEXTHOP_TYPE_IPV4
:
1594 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1596 case NEXTHOP_TYPE_IPV6
:
1597 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1598 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1600 snhlfe
->ifindex
= ifindex
;
1603 XFREE(MTYPE_SNHLFE
, snhlfe
);
1607 if (slsp
->snhlfe_list
)
1608 slsp
->snhlfe_list
->prev
= snhlfe
;
1609 snhlfe
->next
= slsp
->snhlfe_list
;
1610 slsp
->snhlfe_list
= snhlfe
;
1616 * Delete static NHLFE. Entry must be present on list.
1618 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1625 slsp
= snhlfe
->slsp
;
1630 snhlfe
->next
->prev
= snhlfe
->prev
;
1632 snhlfe
->prev
->next
= snhlfe
->next
;
1634 slsp
->snhlfe_list
= snhlfe
->next
;
1636 snhlfe
->prev
= snhlfe
->next
= NULL
;
1638 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1639 XFREE(MTYPE_SNHLFE
, snhlfe
);
1645 * Delete all static NHLFE entries for this LSP (in label).
1647 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1649 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1654 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1655 snhlfe_next
= snhlfe
->next
;
1663 * Create printable string for NHLFE configuration.
1665 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1668 switch (snhlfe
->gtype
) {
1669 case NEXTHOP_TYPE_IPV4
:
1670 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1672 case NEXTHOP_TYPE_IPV6
:
1673 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1674 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1675 if (snhlfe
->ifindex
)
1677 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1687 * Initialize work queue for processing changed LSPs.
1689 static int mpls_processq_init(struct zebra_t
*zebra
)
1691 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1692 if (!zebra
->lsp_process_q
) {
1693 zlog_err("%s: could not initialise work queue!", __func__
);
1697 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1698 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1699 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1700 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1701 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1702 zebra
->lsp_process_q
->spec
.hold
= 10;
1708 /* Public functions */
1710 void kernel_lsp_pass_fail(zebra_lsp_t
*lsp
, enum dp_results res
)
1712 struct nexthop
*nexthop
;
1713 zebra_nhlfe_t
*nhlfe
;
1719 case DP_INSTALL_FAILURE
:
1720 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1721 clear_nhlfe_installed(lsp
);
1722 zlog_warn("LSP Install Failure: %u", lsp
->ile
.in_label
);
1724 case DP_INSTALL_SUCCESS
:
1725 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1726 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1727 nexthop
= nhlfe
->nexthop
;
1731 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1732 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1735 case DP_DELETE_SUCCESS
:
1736 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1737 clear_nhlfe_installed(lsp
);
1739 case DP_DELETE_FAILURE
:
1740 zlog_warn("LSP Deletion Failure: %u", lsp
->ile
.in_label
);
1746 * Install dynamic LSP entry.
1748 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1749 struct route_entry
*re
)
1751 struct route_table
*table
;
1754 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1758 /* See if there is a configured label binding for this FEC. */
1759 fec
= fec_find(table
, &rn
->p
);
1760 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1763 /* We cannot install a label forwarding entry if local label is the
1764 * implicit-null label.
1766 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
1769 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1776 * Uninstall dynamic LSP entry, if any.
1778 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1779 struct route_entry
*re
)
1781 struct route_table
*table
;
1784 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1788 /* See if there is a configured label binding for this FEC. */
1789 fec
= fec_find(table
, &rn
->p
);
1790 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1793 /* Uninstall always removes all dynamic NHLFEs. */
1794 return lsp_uninstall(zvrf
, fec
->label
);
1798 * Registration from a client for the label binding for a FEC. If a binding
1799 * already exists, it is informed to the client.
1800 * NOTE: If there is a manually configured label binding, that is used.
1801 * Otherwise, if a label index is specified, it means we have to allocate the
1802 * label from a locally configured label block (SRGB), if one exists and index
1805 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1806 uint32_t label_index
, struct zserv
*client
)
1808 struct route_table
*table
;
1812 int label_change
= 0;
1815 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1819 if (IS_ZEBRA_DEBUG_MPLS
)
1820 prefix2str(p
, buf
, BUFSIZ
);
1823 fec
= fec_find(table
, p
);
1825 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1827 prefix2str(p
, buf
, BUFSIZ
);
1829 "Failed to add FEC %s upon register, client %s",
1830 buf
, zebra_route_string(client
->proto
));
1834 old_label
= MPLS_INVALID_LABEL
;
1837 /* Client may register same FEC with different label index. */
1839 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1840 if (!new_client
&& fec
->label_index
== label_index
)
1841 /* Duplicate register */
1844 /* Save current label, update label index */
1845 old_label
= fec
->label
;
1846 fec
->label_index
= label_index
;
1850 listnode_add(fec
->client_list
, client
);
1852 if (IS_ZEBRA_DEBUG_MPLS
)
1853 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1854 label_index
, new_client
? "registered" : "updated",
1855 zebra_route_string(client
->proto
));
1857 /* If not a configured FEC, derive the local label (from label index)
1860 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1861 fec_derive_label_from_index(zvrf
, fec
);
1863 /* If no label change, exit. */
1864 if (fec
->label
== old_label
)
1870 /* If new client or label change, update client and install or uninstall
1871 * label forwarding entry as needed.
1873 /* Inform client of label, if needed. */
1874 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1875 if (IS_ZEBRA_DEBUG_MPLS
)
1876 zlog_debug("Update client label %u", fec
->label
);
1877 fec_send(fec
, client
);
1880 if (new_client
|| label_change
)
1881 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1887 * Deregistration from a client for the label binding for a FEC. The FEC
1888 * itself is deleted if no other registered clients exist and there is no
1889 * label bound to the FEC.
1891 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1892 struct zserv
*client
)
1894 struct route_table
*table
;
1898 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1902 if (IS_ZEBRA_DEBUG_MPLS
)
1903 prefix2str(p
, buf
, BUFSIZ
);
1905 fec
= fec_find(table
, p
);
1907 prefix2str(p
, buf
, BUFSIZ
);
1908 zlog_err("Failed to find FEC %s upon unregister, client %s",
1909 buf
, zebra_route_string(client
->proto
));
1913 listnode_delete(fec
->client_list
, client
);
1915 if (IS_ZEBRA_DEBUG_MPLS
)
1916 zlog_debug("FEC %s unregistered by client %s", buf
,
1917 zebra_route_string(client
->proto
));
1919 /* If not a configured entry, delete the FEC if no other clients. Before
1920 * deleting, see if any LSP needs to be uninstalled.
1922 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1923 && list_isempty(fec
->client_list
)) {
1924 mpls_label_t old_label
= fec
->label
;
1925 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1926 fec_change_update_lsp(zvrf
, fec
, old_label
);
1934 * Cleanup any FECs registered by this client.
1936 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
1938 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1939 struct route_node
*rn
;
1941 struct listnode
*node
;
1942 struct zserv
*fec_client
;
1945 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1946 if (zvrf
->fec_table
[af
] == NULL
)
1949 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1950 rn
= route_next(rn
)) {
1952 if (!fec
|| list_isempty(fec
->client_list
))
1955 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1957 if (fec_client
== client
) {
1958 listnode_delete(fec
->client_list
,
1960 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1961 && list_isempty(fec
->client_list
))
1973 * Return FEC (if any) to which this label is bound.
1974 * Note: Only works for per-prefix binding and when the label is not
1976 * TODO: Currently walks entire table, can optimize later with another
1979 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1982 struct route_node
*rn
;
1986 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1987 if (zvrf
->fec_table
[af
] == NULL
)
1990 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1991 rn
= route_next(rn
)) {
1995 if (fec
->label
== label
)
2004 * Inform if specified label is currently bound to a FEC or not.
2006 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2008 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2012 * Add static FEC to label binding. If there are clients registered for this
2013 * FEC, notify them. If there are labeled routes for this FEC, install the
2014 * label forwarding entry.
2016 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2017 mpls_label_t in_label
)
2019 struct route_table
*table
;
2022 mpls_label_t old_label
;
2025 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2029 if (IS_ZEBRA_DEBUG_MPLS
)
2030 prefix2str(p
, buf
, BUFSIZ
);
2032 /* Update existing FEC or create a new one. */
2033 fec
= fec_find(table
, p
);
2035 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2036 MPLS_INVALID_LABEL_INDEX
);
2038 prefix2str(p
, buf
, BUFSIZ
);
2039 zlog_err("Failed to add FEC %s upon config", buf
);
2043 if (IS_ZEBRA_DEBUG_MPLS
)
2044 zlog_debug("Add fec %s label %u", buf
, in_label
);
2046 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2047 if (fec
->label
== in_label
)
2048 /* Duplicate config */
2051 /* Label change, update clients. */
2052 old_label
= fec
->label
;
2053 if (IS_ZEBRA_DEBUG_MPLS
)
2054 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2056 fec
->label
= in_label
;
2057 fec_update_clients(fec
);
2059 /* Update label forwarding entries appropriately */
2060 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2067 * Remove static FEC to label binding. If there are no clients registered
2068 * for this FEC, delete the FEC; else notify clients
2069 * Note: Upon delete of static binding, if label index exists for this FEC,
2070 * client may need to be updated with derived label.
2072 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2074 struct route_table
*table
;
2076 mpls_label_t old_label
;
2079 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2083 fec
= fec_find(table
, p
);
2085 prefix2str(p
, buf
, BUFSIZ
);
2086 zlog_err("Failed to find FEC %s upon delete", buf
);
2090 if (IS_ZEBRA_DEBUG_MPLS
) {
2091 prefix2str(p
, buf
, BUFSIZ
);
2092 zlog_debug("Delete fec %s label index %u", buf
,
2096 old_label
= fec
->label
;
2097 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2098 fec
->label
= MPLS_INVALID_LABEL
;
2100 /* If no client exists, just delete the FEC. */
2101 if (list_isempty(fec
->client_list
)) {
2106 /* Derive the local label (from label index) or reset it. */
2107 fec_derive_label_from_index(zvrf
, fec
);
2109 /* If there is a label change, update clients. */
2110 if (fec
->label
== old_label
)
2112 fec_update_clients(fec
);
2114 /* Update label forwarding entries appropriately */
2115 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2119 * Display MPLS FEC to label binding configuration (VTY command handler).
2121 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2123 struct route_node
*rn
;
2129 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2130 if (zvrf
->fec_table
[af
] == NULL
)
2133 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2134 rn
= route_next(rn
)) {
2141 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2145 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2146 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2147 label2str(fec
->label
, lstr
, BUFSIZ
));
2155 * Display MPLS FEC to label binding (VTY command handler).
2157 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2159 struct route_node
*rn
;
2162 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2163 if (zvrf
->fec_table
[af
] == NULL
)
2166 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2167 rn
= route_next(rn
)) {
2170 fec_print(rn
->info
, vty
);
2176 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2178 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2181 struct route_table
*table
;
2182 struct route_node
*rn
;
2184 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2189 rn
= route_node_lookup(table
, p
);
2193 route_unlock_node(rn
);
2197 fec_print(rn
->info
, vty
);
2200 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2201 enum lsp_types_t type
, mpls_label_t label
)
2203 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2204 nexthop_add_labels(nexthop
, type
, 1, &label
);
2205 else if (!add
&& nexthop
->nh_label_type
== type
)
2206 nexthop_del_labels(nexthop
);
2214 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2216 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2217 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2218 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2219 mpls_label_t out_label
)
2221 struct route_table
*table
;
2222 struct route_node
*rn
;
2223 struct route_entry
*re
;
2224 struct nexthop
*nexthop
;
2228 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2233 /* Lookup existing route */
2234 rn
= route_node_get(table
, prefix
);
2235 RNODE_FOREACH_RE (rn
, re
) {
2236 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2238 if (re
->distance
== distance
)
2246 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2247 switch (nexthop
->type
) {
2248 case NEXTHOP_TYPE_IPV4
:
2249 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2250 if (gtype
!= NEXTHOP_TYPE_IPV4
2251 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2253 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2255 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2256 && nexthop
->ifindex
!= ifindex
)
2258 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2263 case NEXTHOP_TYPE_IPV6
:
2264 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2265 if (gtype
!= NEXTHOP_TYPE_IPV6
2266 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2268 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2270 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2271 && nexthop
->ifindex
!= ifindex
)
2273 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2286 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2287 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2294 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2295 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2296 * the out-label for an existing NHLFE (update case).
2298 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2299 mpls_label_t in_label
, mpls_label_t out_label
,
2300 enum nexthop_types_t gtype
, union g_addr
*gate
,
2303 struct hash
*lsp_table
;
2304 zebra_ile_t tmp_ile
;
2306 zebra_nhlfe_t
*nhlfe
;
2310 lsp_table
= zvrf
->lsp_table
;
2314 /* If entry is present, exit. */
2315 tmp_ile
.in_label
= in_label
;
2316 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2319 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2321 struct nexthop
*nh
= nhlfe
->nexthop
;
2324 assert(nh
->nh_label
);
2326 /* Clear deleted flag (in case it was set) */
2327 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2328 if (nh
->nh_label
->label
[0] == out_label
)
2332 if (IS_ZEBRA_DEBUG_MPLS
) {
2333 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2335 "LSP in-label %u type %d nexthop %s "
2336 "out-label changed to %u (old %u)",
2337 in_label
, type
, buf
, out_label
,
2338 nh
->nh_label
->label
[0]);
2341 /* Update out label, trigger processing. */
2342 nh
->nh_label
->label
[0] = out_label
;
2344 /* Add LSP entry to this nexthop */
2345 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2349 if (IS_ZEBRA_DEBUG_MPLS
) {
2350 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2352 "Add LSP in-label %u type %d nexthop %s "
2354 in_label
, type
, buf
, out_label
);
2357 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2360 /* Mark NHLFE, queue LSP for processing. */
2361 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2362 if (lsp_processq_add(lsp
))
2369 * Uninstall a particular NHLFE in the forwarding table. If this is
2370 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2372 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2373 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2374 union g_addr
*gate
, ifindex_t ifindex
)
2376 struct hash
*lsp_table
;
2377 zebra_ile_t tmp_ile
;
2379 zebra_nhlfe_t
*nhlfe
;
2383 lsp_table
= zvrf
->lsp_table
;
2387 /* If entry is not present, exit. */
2388 tmp_ile
.in_label
= in_label
;
2389 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2392 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2396 if (IS_ZEBRA_DEBUG_MPLS
) {
2397 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2398 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2399 in_label
, type
, buf
, nhlfe
->flags
);
2402 /* Mark NHLFE for delete or directly delete, as appropriate. */
2403 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2404 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2405 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2406 if (lsp_processq_add(lsp
))
2411 /* Free LSP entry if no other NHLFEs and not scheduled. */
2412 if (!lsp
->nhlfe_list
2413 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2414 if (IS_ZEBRA_DEBUG_MPLS
)
2415 zlog_debug("Free LSP in-label %u flags 0x%x",
2416 lsp
->ile
.in_label
, lsp
->flags
);
2418 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2420 XFREE(MTYPE_LSP
, lsp
);
2427 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2428 * If no other NHLFEs exist, the entry would be deleted.
2430 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2433 struct hash
*lsp_table
;
2435 lsp
= (zebra_lsp_t
*)backet
->data
;
2436 if (!lsp
|| !lsp
->nhlfe_list
)
2443 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2447 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2449 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2451 struct route_table
*table
;
2452 struct route_node
*rn
;
2453 struct route_entry
*re
;
2454 struct nexthop
*nexthop
;
2457 /* Process routes of interested address-families. */
2458 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2462 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2464 RNODE_FOREACH_RE (rn
, re
) {
2465 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2466 nexthop
= nexthop
->next
) {
2467 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2470 nexthop_del_labels(nexthop
);
2471 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2472 SET_FLAG(re
->status
,
2473 ROUTE_ENTRY_LABELS_CHANGED
);
2483 #if defined(HAVE_CUMULUS)
2485 * Check that the label values used in LSP creation are consistent. The
2486 * main criteria is that if there is ECMP, the label operation must still
2487 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2488 * to current HW restrictions.
2490 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2491 mpls_label_t in_label
,
2492 mpls_label_t out_label
,
2493 enum nexthop_types_t gtype
,
2494 union g_addr
*gate
, ifindex_t ifindex
)
2496 struct hash
*slsp_table
;
2497 zebra_ile_t tmp_ile
;
2499 zebra_snhlfe_t
*snhlfe
;
2502 slsp_table
= zvrf
->slsp_table
;
2506 /* If entry is not present, exit. */
2507 tmp_ile
.in_label
= in_label
;
2508 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2512 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2514 if (snhlfe
->out_label
== out_label
)
2517 /* If not only NHLFE, cannot allow label change. */
2518 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2521 /* If other NHLFEs exist, label operation must match. */
2522 if (slsp
->snhlfe_list
) {
2525 cur_op
= (slsp
->snhlfe_list
->out_label
2526 == MPLS_LABEL_IMPLICIT_NULL
);
2527 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2528 if (cur_op
!= new_op
)
2533 /* Label values are good. */
2536 #endif /* HAVE_CUMULUS */
2539 * Add static LSP entry. This may be the first entry for this incoming label
2540 * or an additional nexthop; an existing entry may also have outgoing label
2542 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2545 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2546 mpls_label_t out_label
,
2547 enum nexthop_types_t gtype
, union g_addr
*gate
,
2550 struct hash
*slsp_table
;
2551 zebra_ile_t tmp_ile
;
2553 zebra_snhlfe_t
*snhlfe
;
2557 slsp_table
= zvrf
->slsp_table
;
2561 /* If entry is present, exit. */
2562 tmp_ile
.in_label
= in_label
;
2563 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2566 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2568 if (snhlfe
->out_label
== out_label
)
2572 if (IS_ZEBRA_DEBUG_MPLS
) {
2573 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2575 "Upd static LSP in-label %u nexthop %s "
2576 "out-label %u (old %u)",
2577 in_label
, buf
, out_label
, snhlfe
->out_label
);
2579 snhlfe
->out_label
= out_label
;
2581 /* Add static LSP entry to this nexthop */
2582 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2586 if (IS_ZEBRA_DEBUG_MPLS
) {
2587 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2589 "Add static LSP in-label %u nexthop %s out-label %u",
2590 in_label
, buf
, out_label
);
2594 /* (Re)Install LSP in the main table. */
2595 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2603 * Delete static LSP entry. This may be the delete of one particular
2604 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2606 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2607 * LSP configuration.
2609 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2610 enum nexthop_types_t gtype
, union g_addr
*gate
,
2613 struct hash
*slsp_table
;
2614 zebra_ile_t tmp_ile
;
2616 zebra_snhlfe_t
*snhlfe
;
2619 slsp_table
= zvrf
->slsp_table
;
2623 /* If entry is not present, exit. */
2624 tmp_ile
.in_label
= in_label
;
2625 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2629 /* Is it delete of entire LSP or a specific NHLFE? */
2630 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2631 if (IS_ZEBRA_DEBUG_MPLS
)
2632 zlog_debug("Del static LSP in-label %u", in_label
);
2634 /* Uninstall entire LSP from the main table. */
2635 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2637 /* Delete all static NHLFEs */
2638 snhlfe_del_all(slsp
);
2640 /* Find specific NHLFE, exit if not found. */
2641 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2645 if (IS_ZEBRA_DEBUG_MPLS
) {
2647 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2648 zlog_debug("Del static LSP in-label %u nexthop %s",
2652 /* Uninstall LSP from the main table. */
2653 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2656 /* Delete static LSP NHLFE */
2660 /* Remove entire static LSP entry if no NHLFE - valid in either case
2662 if (!slsp
->snhlfe_list
) {
2663 slsp
= hash_release(slsp_table
, &tmp_ile
);
2665 XFREE(MTYPE_SLSP
, slsp
);
2672 * Schedule all MPLS label forwarding entries for processing.
2673 * Called upon changes that may affect one or more of them such as
2674 * interface or nexthop state changes.
2676 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2680 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2684 * Display MPLS label forwarding table for a specific LSP
2685 * (VTY command handler).
2687 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2688 mpls_label_t label
, uint8_t use_json
)
2690 struct hash
*lsp_table
;
2692 zebra_ile_t tmp_ile
;
2693 json_object
*json
= NULL
;
2696 lsp_table
= zvrf
->lsp_table
;
2700 /* If entry is not present, exit. */
2701 tmp_ile
.in_label
= label
;
2702 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2707 json
= lsp_json(lsp
);
2708 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2709 json
, JSON_C_TO_STRING_PRETTY
));
2710 json_object_free(json
);
2712 lsp_print(lsp
, (void *)vty
);
2716 * Display MPLS label forwarding table (VTY command handler).
2718 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2722 json_object
*json
= NULL
;
2723 zebra_lsp_t
*lsp
= NULL
;
2724 zebra_nhlfe_t
*nhlfe
= NULL
;
2725 struct nexthop
*nexthop
= NULL
;
2726 struct listnode
*node
= NULL
;
2727 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2730 json
= json_object_new_object();
2732 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2733 json_object_object_add(
2734 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2737 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2738 json
, JSON_C_TO_STRING_PRETTY
));
2739 json_object_free(json
);
2741 vty_out(vty
, " Inbound Outbound\n");
2742 vty_out(vty
, " Label Type Nexthop Label\n");
2743 vty_out(vty
, "-------- ------- --------------- --------\n");
2745 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2746 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2747 nhlfe
= nhlfe
->next
) {
2748 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2749 nhlfe_type2str(nhlfe
->type
));
2750 nexthop
= nhlfe
->nexthop
;
2752 switch (nexthop
->type
) {
2753 case NEXTHOP_TYPE_IFINDEX
: {
2754 struct zebra_ns
*zns
;
2755 struct interface
*ifp
;
2757 zns
= zebra_ns_lookup(NS_DEFAULT
);
2758 ifp
= if_lookup_by_index_per_ns(
2761 vty_out(vty
, "%15s", ifp
->name
);
2764 case NEXTHOP_TYPE_IPV4
:
2765 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2766 vty_out(vty
, "%15s",
2767 inet_ntoa(nexthop
->gate
.ipv4
));
2769 case NEXTHOP_TYPE_IPV6
:
2770 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2771 vty_out(vty
, "%15s",
2773 &nexthop
->gate
.ipv6
,
2780 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
2781 vty_out(vty
, " %8s\n",
2796 list_delete_and_null(&lsp_list
);
2800 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2802 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2805 zebra_snhlfe_t
*snhlfe
;
2806 struct listnode
*node
;
2807 struct list
*slsp_list
=
2808 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2810 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2811 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2812 snhlfe
= snhlfe
->next
) {
2816 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2817 switch (snhlfe
->out_label
) {
2818 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2819 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
2820 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2822 case MPLS_LABEL_IMPLICIT_NULL
:
2823 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2826 sprintf(lstr
, "%u", snhlfe
->out_label
);
2830 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2835 list_delete_and_null(&slsp_list
);
2836 return (zvrf
->slsp_table
->count
? 1 : 0);
2840 * Add/update global label block.
2842 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
2845 zvrf
->mpls_srgb
.start_label
= start_label
;
2846 zvrf
->mpls_srgb
.end_label
= end_label
;
2848 /* Evaluate registered FECs to see if any get a label or not. */
2854 * Delete global label block.
2856 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2858 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2859 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2861 /* Process registered FECs to clear their local label, if needed. */
2867 * Display MPLS global label block configuration (VTY command handler).
2869 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2871 if (zvrf
->mpls_srgb
.start_label
== 0)
2874 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2875 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2876 vty_out(vty
, "mpls label global-block %u %u\n",
2877 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2884 * Called when VRF becomes inactive, cleans up information but keeps
2886 * NOTE: Currently supported only for default VRF.
2888 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
2890 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2894 * Called upon process exiting, need to delete LSP forwarding
2895 * entries from the kernel.
2896 * NOTE: Currently supported only for default VRF.
2898 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2900 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2901 hash_clean(zvrf
->lsp_table
, NULL
);
2902 hash_free(zvrf
->lsp_table
);
2903 hash_clean(zvrf
->slsp_table
, NULL
);
2904 hash_free(zvrf
->slsp_table
);
2905 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2906 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2910 * Allocate MPLS tables for this VRF and do other initialization.
2911 * NOTE: Currently supported only for default VRF.
2913 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2918 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
2919 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
2920 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2921 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2922 zvrf
->mpls_flags
= 0;
2923 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2924 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2928 * Global MPLS initialization.
2930 void zebra_mpls_init(void)
2934 if (mpls_kernel_init() < 0) {
2935 zlog_warn("Disabling MPLS support (no kernel support)");
2939 if (!mpls_processq_init(&zebrad
))
2942 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);