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/zserv.h"
42 #include "zebra/redistribute.h"
43 #include "zebra/debug.h"
44 #include "zebra/zebra_memory.h"
45 #include "zebra/zebra_vrf.h"
46 #include "zebra/zebra_mpls.h"
48 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
49 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
50 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
51 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
52 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
57 /* Default rtm_table for all clients */
58 extern struct zebra_t zebrad
;
60 /* static function declarations */
62 static void fec_evaluate(struct zebra_vrf
*zvrf
);
63 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
65 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
66 struct route_node
*rn
, struct route_entry
*re
);
67 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
68 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
69 mpls_label_t old_label
);
70 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
71 static void fec_update_clients(zebra_fec_t
*fec
);
72 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
73 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
74 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
75 mpls_label_t label
, uint32_t flags
,
76 uint32_t label_index
);
77 static int fec_del(zebra_fec_t
*fec
);
79 static unsigned int label_hash(void *p
);
80 static int label_cmp(const void *p1
, const void *p2
);
81 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
82 struct nexthop
*nexthop
);
83 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
84 struct nexthop
*nexthop
);
85 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
87 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
88 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
);
89 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
);
90 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
91 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
92 static void lsp_processq_complete(struct work_queue
*wq
);
93 static int lsp_processq_add(zebra_lsp_t
*lsp
);
94 static void *lsp_alloc(void *p
);
96 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
97 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
98 union g_addr
*gate
, ifindex_t ifindex
);
99 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
100 enum nexthop_types_t gtype
, union g_addr
*gate
,
102 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
103 enum nexthop_types_t gtype
, union g_addr
*gate
,
104 ifindex_t ifindex
, mpls_label_t out_label
);
105 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
106 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
107 struct mpls_label_stack
*nh_label
);
108 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
109 enum lsp_types_t type
);
110 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
111 mpls_label_t in_label
);
112 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
113 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
114 static void *slsp_alloc(void *p
);
115 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
116 union g_addr
*gate
, ifindex_t ifindex
);
117 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
118 enum nexthop_types_t gtype
,
119 union g_addr
*gate
, ifindex_t ifindex
);
120 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
121 enum nexthop_types_t gtype
,
122 union g_addr
*gate
, ifindex_t ifindex
,
123 mpls_label_t out_label
);
124 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
125 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
126 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
127 static int mpls_processq_init(struct zebra_t
*zebra
);
130 /* Static functions */
133 * Handle failure in LSP install, clear flags for NHLFE.
135 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
137 zebra_nhlfe_t
*nhlfe
;
138 struct nexthop
*nexthop
;
140 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
141 nexthop
= nhlfe
->nexthop
;
145 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
146 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
151 * Install label forwarding entry based on labeled-route entry.
153 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
154 struct route_node
*rn
, struct route_entry
*re
)
156 struct hash
*lsp_table
;
159 zebra_nhlfe_t
*nhlfe
;
160 struct nexthop
*nexthop
;
161 enum lsp_types_t lsp_type
;
166 lsp_table
= zvrf
->lsp_table
;
170 lsp_type
= lsp_type_from_re_type(re
->type
);
173 /* Locate or allocate LSP entry. */
174 tmp_ile
.in_label
= label
;
175 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
179 /* For each active nexthop, create NHLFE. Note that we deliberately skip
180 * recursive nexthops right now, because intermediate hops won't
182 * the label advertised by the recursive nexthop (plus we don't have the
183 * logic yet to push multiple labels).
185 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
186 /* Skip inactive and recursive entries. */
187 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
189 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
192 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
195 /* Clear deleted flag (in case it was set) */
196 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
197 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
202 if (IS_ZEBRA_DEBUG_MPLS
) {
203 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
205 "LSP in-label %u type %d nexthop %s "
207 lsp
->ile
.in_label
, lsp_type
, buf
);
210 /* Update out label, trigger processing. */
211 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
212 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
215 /* Add LSP entry to this nexthop */
216 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
217 &nexthop
->gate
, nexthop
->ifindex
,
218 nexthop
->nh_label
->label
[0]);
222 if (IS_ZEBRA_DEBUG_MPLS
) {
223 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
225 "Add LSP in-label %u type %d nexthop %s "
227 lsp
->ile
.in_label
, lsp_type
, buf
,
228 nexthop
->nh_label
->label
[0]);
231 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
233 /* Mark NHLFE as changed. */
234 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
239 /* Queue LSP for processing if necessary. If no NHLFE got added (special
240 * case), delete the LSP entry; this case results in somewhat ugly
243 if (added
|| changed
) {
244 if (lsp_processq_add(lsp
))
246 } else if (!lsp
->nhlfe_list
247 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
248 if (IS_ZEBRA_DEBUG_MPLS
)
249 zlog_debug("Free LSP in-label %u flags 0x%x",
250 lsp
->ile
.in_label
, lsp
->flags
);
252 lsp
= hash_release(lsp_table
, &lsp
->ile
);
254 XFREE(MTYPE_LSP
, lsp
);
261 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
262 * NHLFEs are removed, the entire entry is deleted.
264 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
266 struct hash
*lsp_table
;
269 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
273 lsp_table
= zvrf
->lsp_table
;
277 /* If entry is not present, exit. */
278 tmp_ile
.in_label
= label
;
279 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
280 if (!lsp
|| !lsp
->nhlfe_list
)
283 /* Mark NHLFEs for delete or directly delete, as appropriate. */
284 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
285 nhlfe_next
= nhlfe
->next
;
287 /* Skip static NHLFEs */
288 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
291 if (IS_ZEBRA_DEBUG_MPLS
) {
292 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
294 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
295 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
298 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
299 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
300 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
306 /* Queue LSP for processing, if needed, else delete. */
307 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
308 if (lsp_processq_add(lsp
))
310 } else if (!lsp
->nhlfe_list
311 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
312 if (IS_ZEBRA_DEBUG_MPLS
)
313 zlog_debug("Del LSP in-label %u flags 0x%x",
314 lsp
->ile
.in_label
, lsp
->flags
);
316 lsp
= hash_release(lsp_table
, &lsp
->ile
);
318 XFREE(MTYPE_LSP
, lsp
);
325 * This function is invoked upon change to label block configuration; it
326 * will walk all registered FECs with label-index and appropriately update
327 * their local labels and trigger client updates.
329 static void fec_evaluate(struct zebra_vrf
*zvrf
)
331 struct route_node
*rn
;
333 uint32_t old_label
, new_label
;
337 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
338 if (zvrf
->fec_table
[af
] == NULL
)
341 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
342 rn
= route_next(rn
)) {
343 if ((fec
= rn
->info
) == NULL
)
346 /* Skip configured FECs and those without a label index.
348 if (fec
->flags
& FEC_FLAG_CONFIGURED
349 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
352 if (IS_ZEBRA_DEBUG_MPLS
)
353 prefix2str(&rn
->p
, buf
, BUFSIZ
);
355 /* Save old label, determine new label. */
356 old_label
= fec
->label
;
358 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
359 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
360 new_label
= MPLS_INVALID_LABEL
;
362 /* If label has changed, update FEC and clients. */
363 if (new_label
== old_label
)
366 if (IS_ZEBRA_DEBUG_MPLS
)
368 "Update fec %s new label %u upon label block",
371 fec
->label
= new_label
;
372 fec_update_clients(fec
);
374 /* Update label forwarding entries appropriately */
375 fec_change_update_lsp(zvrf
, fec
, old_label
);
381 * Derive (if possible) and update the local label for the FEC based on
382 * its label index. The index is "acceptable" if it falls within the
383 * globally configured label block (SRGB).
385 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
390 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
391 && zvrf
->mpls_srgb
.start_label
392 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
393 < zvrf
->mpls_srgb
.end_label
))
396 fec
->label
= MPLS_INVALID_LABEL
;
402 * There is a change for this FEC. Install or uninstall label forwarding
403 * entries, as appropriate.
405 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
406 mpls_label_t old_label
)
408 struct route_table
*table
;
409 struct route_node
*rn
;
410 struct route_entry
*re
;
413 /* Uninstall label forwarding entry, if previously installed. */
414 if (old_label
!= MPLS_INVALID_LABEL
415 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
416 lsp_uninstall(zvrf
, old_label
);
418 /* Install label forwarding entry corr. to new label, if needed. */
419 if (fec
->label
== MPLS_INVALID_LABEL
420 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
423 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
424 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
428 /* See if labeled route exists. */
429 rn
= route_node_lookup(table
, &fec
->rn
->p
);
433 RNODE_FOREACH_RE (rn
, re
) {
434 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
438 if (!re
|| !zebra_rib_labeled_unicast(re
))
441 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
448 * Inform about FEC to a registered client.
450 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
453 struct route_node
*rn
;
457 /* Get output stream. */
458 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
460 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
462 stream_putw(s
, rn
->p
.family
);
463 stream_put_prefix(s
, &rn
->p
);
464 stream_putl(s
, fec
->label
);
465 stream_putw_at(s
, 0, stream_get_endp(s
));
466 return zebra_server_send_message(client
, s
);
470 * Update all registered clients about this FEC. Caller should've updated
471 * FEC and ensure no duplicate updates.
473 static void fec_update_clients(zebra_fec_t
*fec
)
475 struct listnode
*node
;
476 struct zserv
*client
;
478 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
479 if (IS_ZEBRA_DEBUG_MPLS
)
480 zlog_debug("Update client %s",
481 zebra_route_string(client
->proto
));
482 fec_send(fec
, client
);
488 * Print a FEC-label binding entry.
490 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
492 struct route_node
*rn
;
493 struct listnode
*node
;
494 struct zserv
*client
;
498 prefix2str(&rn
->p
, buf
, BUFSIZ
);
499 vty_out(vty
, "%s\n", buf
);
500 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
501 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
502 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
504 if (!list_isempty(fec
->client_list
)) {
505 vty_out(vty
, " Client list:");
506 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
507 vty_out(vty
, " %s(fd %d)",
508 zebra_route_string(client
->proto
),
515 * Locate FEC-label binding that matches with passed info.
517 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
519 struct route_node
*rn
;
522 rn
= route_node_lookup(table
, p
);
526 route_unlock_node(rn
);
531 * Add a FEC. This may be upon a client registering for a binding
532 * or when a binding is configured.
534 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
535 mpls_label_t label
, uint32_t flags
,
536 uint32_t label_index
)
538 struct route_node
*rn
;
543 /* Lookup (or add) route node.*/
544 rn
= route_node_get(table
, p
);
551 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
558 fec
->client_list
= list_new();
560 route_unlock_node(rn
); /* for the route_node_get */
562 fec
->label_index
= label_index
;
569 * Delete a FEC. This may be upon the last client deregistering for
570 * a FEC and no binding exists or when the binding is deleted and there
571 * are no registered clients.
573 static int fec_del(zebra_fec_t
*fec
)
575 list_delete_and_null(&fec
->client_list
);
576 fec
->rn
->info
= NULL
;
577 route_unlock_node(fec
->rn
);
578 XFREE(MTYPE_FEC
, fec
);
583 * Hash function for label.
585 static unsigned int label_hash(void *p
)
587 const zebra_ile_t
*ile
= p
;
589 return (jhash_1word(ile
->in_label
, 0));
593 * Compare 2 LSP hash entries based on in-label.
595 static int label_cmp(const void *p1
, const void *p2
)
597 const zebra_ile_t
*ile1
= p1
;
598 const zebra_ile_t
*ile2
= p2
;
600 return (ile1
->in_label
== ile2
->in_label
);
604 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
606 * NOTE: Looking only for connected routes right now.
608 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
609 struct nexthop
*nexthop
)
611 struct route_table
*table
;
612 struct prefix_ipv4 p
;
613 struct route_node
*rn
;
614 struct route_entry
*match
;
615 struct nexthop
*match_nh
;
617 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
621 /* Lookup nexthop in IPv4 routing table. */
622 memset(&p
, 0, sizeof(struct prefix_ipv4
));
624 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
625 p
.prefix
= nexthop
->gate
.ipv4
;
627 rn
= route_node_match(table
, (struct prefix
*)&p
);
631 route_unlock_node(rn
);
633 /* Locate a valid connected route. */
634 RNODE_FOREACH_RE (rn
, match
) {
635 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
636 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
639 for (match_nh
= match
->ng
.nexthop
; match_nh
;
640 match_nh
= match_nh
->next
) {
641 if (match
->type
== ZEBRA_ROUTE_CONNECT
642 || nexthop
->ifindex
== match_nh
->ifindex
) {
643 nexthop
->ifindex
= match_nh
->ifindex
;
654 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
656 * NOTE: Looking only for connected routes right now.
658 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
659 struct nexthop
*nexthop
)
661 struct route_table
*table
;
662 struct prefix_ipv6 p
;
663 struct route_node
*rn
;
664 struct route_entry
*match
;
666 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
670 /* Lookup nexthop in IPv6 routing table. */
671 memset(&p
, 0, sizeof(struct prefix_ipv6
));
673 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
674 p
.prefix
= nexthop
->gate
.ipv6
;
676 rn
= route_node_match(table
, (struct prefix
*)&p
);
680 route_unlock_node(rn
);
682 /* Locate a valid connected route. */
683 RNODE_FOREACH_RE (rn
, match
) {
684 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
685 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
686 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
690 if (!match
|| !match
->ng
.nexthop
)
693 nexthop
->ifindex
= match
->ng
.nexthop
->ifindex
;
699 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
701 * NOTE: Each NHLFE points to only 1 nexthop.
703 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
705 struct nexthop
*nexthop
;
706 struct interface
*ifp
;
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 ifp
= if_lookup_by_index(nexthop
->ifindex
, VRF_UNKNOWN
);
725 if (ifp
&& if_is_operative(ifp
))
726 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
728 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
730 case NEXTHOP_TYPE_IPV4
:
731 case NEXTHOP_TYPE_IPV4_IFINDEX
:
732 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
733 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
735 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
738 case NEXTHOP_TYPE_IPV6
:
739 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
740 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
742 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
745 case NEXTHOP_TYPE_IPV6_IFINDEX
:
746 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
747 ifp
= if_lookup_by_index(nexthop
->ifindex
,
749 if (ifp
&& if_is_operative(ifp
))
750 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
752 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
754 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
755 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
757 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
765 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
769 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
770 * reachability and select the best. Multipath entries are also
771 * marked. This is invoked when an LSP scheduled for processing (due
772 * to some change) is examined.
774 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
776 zebra_nhlfe_t
*nhlfe
;
778 struct nexthop
*nexthop
;
786 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
789 * First compute the best path, after checking nexthop status. We are
791 * concerned with non-deleted NHLFEs.
793 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
794 /* Clear selection flags. */
795 UNSET_FLAG(nhlfe
->flags
,
796 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
798 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
799 && nhlfe_nexthop_active(nhlfe
)) {
800 if (!best
|| (nhlfe
->distance
< best
->distance
))
805 lsp
->best_nhlfe
= best
;
806 if (!lsp
->best_nhlfe
)
809 /* Mark best NHLFE as selected. */
810 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
813 * If best path exists, see if there is ECMP. While doing this, note if
815 * new (uninstalled) NHLFE has been selected, an installed entry that is
816 * still selected has a change or an installed entry is to be removed.
818 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
819 int nh_chg
, nh_sel
, nh_inst
;
821 nexthop
= nhlfe
->nexthop
;
822 if (!nexthop
) // unexpected
825 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
826 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
827 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
828 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
829 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
833 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
834 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
835 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
837 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
839 if ((nh_sel
&& !nh_inst
)
840 || (nh_sel
&& nh_inst
&& nh_chg
)
841 || (nh_inst
&& !nh_sel
))
845 /* We have finished examining, clear changed flag. */
846 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
850 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
854 * Delete LSP forwarding entry from kernel, if installed. Called upon
857 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
)
861 lsp
= (zebra_lsp_t
*)backet
->data
;
862 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
867 * Schedule LSP forwarding entry for processing. Called upon changes
868 * that may impact LSPs such as nexthop / connected route changes.
870 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
)
874 lsp
= (zebra_lsp_t
*)backet
->data
;
875 (void)lsp_processq_add(lsp
);
879 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
880 * any multipaths and update or delete from the kernel, as needed.
882 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
885 zebra_nhlfe_t
*oldbest
, *newbest
;
886 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
887 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
889 lsp
= (zebra_lsp_t
*)data
;
890 if (!lsp
) // unexpected
893 oldbest
= lsp
->best_nhlfe
;
895 /* Select best NHLFE(s) */
896 lsp_select_best_nhlfe(lsp
);
898 newbest
= lsp
->best_nhlfe
;
900 if (IS_ZEBRA_DEBUG_MPLS
) {
902 nhlfe2str(oldbest
, buf
, BUFSIZ
);
904 nhlfe2str(newbest
, buf2
, BUFSIZ
);
906 "Process LSP in-label %u oldbest %s newbest %s "
907 "flags 0x%x ecmp# %d",
908 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
909 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
912 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
913 /* Not already installed */
916 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
919 zvrf
->lsp_installs
++;
922 /* Installed, may need an update and/or delete. */
927 zvrf
->lsp_removals
++;
928 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
929 zebra_nhlfe_t
*nhlfe
;
930 struct nexthop
*nexthop
;
932 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
933 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
936 * Any NHLFE that was installed but is not
937 * selected now needs to have its flags updated.
939 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
940 nhlfe
= nhlfe
->next
) {
941 nexthop
= nhlfe
->nexthop
;
945 if (CHECK_FLAG(nhlfe
->flags
,
946 NHLFE_FLAG_INSTALLED
)
947 && !CHECK_FLAG(nhlfe
->flags
,
948 NHLFE_FLAG_SELECTED
)) {
949 UNSET_FLAG(nhlfe
->flags
,
950 NHLFE_FLAG_INSTALLED
);
951 UNSET_FLAG(nexthop
->flags
,
958 zvrf
->lsp_installs
++;
967 * Callback upon processing completion of a LSP forwarding entry.
969 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
971 struct zebra_vrf
*zvrf
;
973 struct hash
*lsp_table
;
974 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
976 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
979 lsp_table
= zvrf
->lsp_table
;
980 if (!lsp_table
) // unexpected
983 lsp
= (zebra_lsp_t
*)data
;
984 if (!lsp
) // unexpected
987 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
989 * delete LSP entry also.
991 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
993 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
994 nhlfe_next
= nhlfe
->next
;
995 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
999 if (!lsp
->nhlfe_list
) {
1000 if (IS_ZEBRA_DEBUG_MPLS
)
1001 zlog_debug("Free LSP in-label %u flags 0x%x",
1002 lsp
->ile
.in_label
, lsp
->flags
);
1004 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1006 XFREE(MTYPE_LSP
, lsp
);
1011 * Callback upon finishing the processing of all scheduled
1012 * LSP forwarding entries.
1014 static void lsp_processq_complete(struct work_queue
*wq
)
1016 /* Nothing to do for now. */
1020 * Add LSP forwarding entry to queue for subsequent processing.
1022 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1024 /* If already scheduled, exit. */
1025 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1028 if (zebrad
.lsp_process_q
== NULL
) {
1029 zlog_err("%s: work_queue does not exist!", __func__
);
1033 work_queue_add(zebrad
.lsp_process_q
, lsp
);
1034 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1039 * Callback to allocate LSP forwarding table entry.
1041 static void *lsp_alloc(void *p
)
1043 const zebra_ile_t
*ile
= p
;
1046 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1049 if (IS_ZEBRA_DEBUG_MPLS
)
1050 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1052 return ((void *)lsp
);
1056 * Create printable string for NHLFE entry.
1058 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1060 struct nexthop
*nexthop
;
1063 nexthop
= nhlfe
->nexthop
;
1064 switch (nexthop
->type
) {
1065 case NEXTHOP_TYPE_IPV4
:
1066 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1067 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1069 case NEXTHOP_TYPE_IPV6
:
1070 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1072 case NEXTHOP_TYPE_IFINDEX
:
1073 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1082 * Check if NHLFE matches with search info passed.
1084 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1085 union g_addr
*gate
, ifindex_t ifindex
)
1087 struct nexthop
*nhop
;
1090 nhop
= nhlfe
->nexthop
;
1094 if (nhop
->type
!= gtype
)
1097 switch (nhop
->type
) {
1098 case NEXTHOP_TYPE_IPV4
:
1099 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1100 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1101 sizeof(struct in_addr
));
1102 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1103 cmp
= !(nhop
->ifindex
== ifindex
);
1105 case NEXTHOP_TYPE_IPV6
:
1106 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1107 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1108 sizeof(struct in6_addr
));
1109 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1110 cmp
= !(nhop
->ifindex
== ifindex
);
1112 case NEXTHOP_TYPE_IFINDEX
:
1113 cmp
= !(nhop
->ifindex
== ifindex
);
1124 * Locate NHLFE that matches with passed info.
1126 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1127 enum nexthop_types_t gtype
, union g_addr
*gate
,
1130 zebra_nhlfe_t
*nhlfe
;
1135 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1136 if (nhlfe
->type
!= lsp_type
)
1138 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1146 * Add NHLFE. Base entry must have been created and duplicate
1149 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1150 enum nexthop_types_t gtype
, union g_addr
*gate
,
1151 ifindex_t ifindex
, mpls_label_t out_label
)
1153 zebra_nhlfe_t
*nhlfe
;
1154 struct nexthop
*nexthop
;
1159 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1164 nhlfe
->type
= lsp_type
;
1165 nhlfe
->distance
= lsp_distance(lsp_type
);
1167 nexthop
= nexthop_new();
1169 XFREE(MTYPE_NHLFE
, nhlfe
);
1172 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1174 nexthop
->vrf_id
= VRF_DEFAULT
;
1175 nexthop
->type
= gtype
;
1176 switch (nexthop
->type
) {
1177 case NEXTHOP_TYPE_IPV4
:
1178 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1179 nexthop
->gate
.ipv4
= gate
->ipv4
;
1181 nexthop
->ifindex
= ifindex
;
1183 case NEXTHOP_TYPE_IPV6
:
1184 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1185 nexthop
->gate
.ipv6
= gate
->ipv6
;
1187 nexthop
->ifindex
= ifindex
;
1189 case NEXTHOP_TYPE_IFINDEX
:
1190 nexthop
->ifindex
= ifindex
;
1193 nexthop_free(nexthop
);
1194 XFREE(MTYPE_NHLFE
, nhlfe
);
1199 nhlfe
->nexthop
= nexthop
;
1200 if (lsp
->nhlfe_list
)
1201 lsp
->nhlfe_list
->prev
= nhlfe
;
1202 nhlfe
->next
= lsp
->nhlfe_list
;
1203 lsp
->nhlfe_list
= nhlfe
;
1209 * Delete NHLFE. Entry must be present on list.
1211 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1224 nexthop_free(nhlfe
->nexthop
);
1226 /* Unlink from LSP */
1228 nhlfe
->next
->prev
= nhlfe
->prev
;
1230 nhlfe
->prev
->next
= nhlfe
->next
;
1232 lsp
->nhlfe_list
= nhlfe
->next
;
1234 if (nhlfe
== lsp
->best_nhlfe
)
1235 lsp
->best_nhlfe
= NULL
;
1237 XFREE(MTYPE_NHLFE
, nhlfe
);
1243 * Update label for NHLFE entry.
1245 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1246 struct mpls_label_stack
*nh_label
)
1248 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1251 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1252 enum lsp_types_t type
)
1254 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1255 int schedule_lsp
= 0;
1258 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1259 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1260 nhlfe_next
= nhlfe
->next
;
1262 /* Skip non-static NHLFEs */
1263 if (nhlfe
->type
!= type
)
1266 if (IS_ZEBRA_DEBUG_MPLS
) {
1267 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1269 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1270 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1273 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1274 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1275 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1282 /* Queue LSP for processing, if needed, else delete. */
1284 if (lsp_processq_add(lsp
))
1286 } else if (!lsp
->nhlfe_list
1287 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1288 if (IS_ZEBRA_DEBUG_MPLS
)
1289 zlog_debug("Free LSP in-label %u flags 0x%x",
1290 lsp
->ile
.in_label
, lsp
->flags
);
1292 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1294 XFREE(MTYPE_LSP
, lsp
);
1301 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1302 * If no other NHLFEs exist, the entry would be deleted.
1304 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1305 mpls_label_t in_label
)
1307 struct hash
*lsp_table
;
1308 zebra_ile_t tmp_ile
;
1312 lsp_table
= zvrf
->lsp_table
;
1316 /* If entry is not present, exit. */
1317 tmp_ile
.in_label
= in_label
;
1318 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1319 if (!lsp
|| !lsp
->nhlfe_list
)
1322 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1325 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1328 json_object
*json_nhlfe
= NULL
;
1329 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1331 json_nhlfe
= json_object_new_object();
1332 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1333 json_object_int_add(json_nhlfe
, "outLabel",
1334 nexthop
->nh_label
->label
[0]);
1335 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1337 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1338 json_object_boolean_true_add(json_nhlfe
, "installed");
1340 switch (nexthop
->type
) {
1341 case NEXTHOP_TYPE_IPV4
:
1342 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1343 json_object_string_add(json_nhlfe
, "nexthop",
1344 inet_ntoa(nexthop
->gate
.ipv4
));
1346 case NEXTHOP_TYPE_IPV6
:
1347 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1348 json_object_string_add(
1349 json_nhlfe
, "nexthop",
1350 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1352 if (nexthop
->ifindex
)
1353 json_object_string_add(json_nhlfe
, "interface",
1354 ifindex2ifname(nexthop
->ifindex
,
1364 * Print the NHLFE for a LSP forwarding entry.
1366 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1368 struct nexthop
*nexthop
;
1371 nexthop
= nhlfe
->nexthop
;
1372 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1375 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1376 nhlfe_type2str(nhlfe
->type
),
1377 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1379 switch (nexthop
->type
) {
1380 case NEXTHOP_TYPE_IPV4
:
1381 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1382 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1383 if (nexthop
->ifindex
)
1384 vty_out(vty
, " dev %s",
1385 ifindex2ifname(nexthop
->ifindex
,
1388 case NEXTHOP_TYPE_IPV6
:
1389 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1390 vty_out(vty
, " via %s",
1391 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1392 if (nexthop
->ifindex
)
1393 vty_out(vty
, " dev %s",
1394 ifindex2ifname(nexthop
->ifindex
,
1401 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1407 * Print an LSP forwarding entry.
1409 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1411 zebra_nhlfe_t
*nhlfe
;
1414 vty
= (struct vty
*)ctxt
;
1416 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1417 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1420 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1421 nhlfe_print(nhlfe
, vty
);
1425 * JSON objects for an LSP forwarding entry.
1427 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1429 zebra_nhlfe_t
*nhlfe
= NULL
;
1430 json_object
*json
= json_object_new_object();
1431 json_object
*json_nhlfe_list
= json_object_new_array();
1433 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1435 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1436 json_object_boolean_true_add(json
, "installed");
1438 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1439 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1441 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1446 /* Return a sorted linked list of the hash contents */
1447 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1450 struct hash_backet
*hb
;
1451 struct list
*sorted_list
= list_new();
1453 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1455 for (i
= 0; i
< hash
->size
; i
++)
1456 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1457 listnode_add_sort(sorted_list
, hb
->data
);
1463 * Compare two LSPs based on their label values.
1465 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1467 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1470 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1477 * Callback to allocate static LSP.
1479 static void *slsp_alloc(void *p
)
1481 const zebra_ile_t
*ile
= p
;
1484 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1486 return ((void *)slsp
);
1490 * Compare two static LSPs based on their label values.
1492 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1494 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1497 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1504 * Check if static NHLFE matches with search info passed.
1506 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1507 union g_addr
*gate
, ifindex_t ifindex
)
1511 if (snhlfe
->gtype
!= gtype
)
1514 switch (snhlfe
->gtype
) {
1515 case NEXTHOP_TYPE_IPV4
:
1516 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1517 sizeof(struct in_addr
));
1519 case NEXTHOP_TYPE_IPV6
:
1520 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1521 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1522 sizeof(struct in6_addr
));
1523 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1524 cmp
= !(snhlfe
->ifindex
== ifindex
);
1534 * Locate static NHLFE that matches with passed info.
1536 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1537 enum nexthop_types_t gtype
,
1538 union g_addr
*gate
, ifindex_t ifindex
)
1540 zebra_snhlfe_t
*snhlfe
;
1545 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1546 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1555 * Add static NHLFE. Base LSP config entry must have been created
1556 * and duplicate check done.
1558 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1559 enum nexthop_types_t gtype
,
1560 union g_addr
*gate
, ifindex_t ifindex
,
1561 mpls_label_t out_label
)
1563 zebra_snhlfe_t
*snhlfe
;
1568 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1569 snhlfe
->slsp
= slsp
;
1570 snhlfe
->out_label
= out_label
;
1571 snhlfe
->gtype
= gtype
;
1573 case NEXTHOP_TYPE_IPV4
:
1574 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1576 case NEXTHOP_TYPE_IPV6
:
1577 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1578 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1580 snhlfe
->ifindex
= ifindex
;
1583 XFREE(MTYPE_SNHLFE
, snhlfe
);
1587 if (slsp
->snhlfe_list
)
1588 slsp
->snhlfe_list
->prev
= snhlfe
;
1589 snhlfe
->next
= slsp
->snhlfe_list
;
1590 slsp
->snhlfe_list
= snhlfe
;
1596 * Delete static NHLFE. Entry must be present on list.
1598 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1605 slsp
= snhlfe
->slsp
;
1610 snhlfe
->next
->prev
= snhlfe
->prev
;
1612 snhlfe
->prev
->next
= snhlfe
->next
;
1614 slsp
->snhlfe_list
= snhlfe
->next
;
1616 snhlfe
->prev
= snhlfe
->next
= NULL
;
1618 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1619 XFREE(MTYPE_SNHLFE
, snhlfe
);
1625 * Delete all static NHLFE entries for this LSP (in label).
1627 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1629 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1634 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1635 snhlfe_next
= snhlfe
->next
;
1643 * Create printable string for NHLFE configuration.
1645 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1648 switch (snhlfe
->gtype
) {
1649 case NEXTHOP_TYPE_IPV4
:
1650 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1652 case NEXTHOP_TYPE_IPV6
:
1653 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1654 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1655 if (snhlfe
->ifindex
)
1657 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1667 * Initialize work queue for processing changed LSPs.
1669 static int mpls_processq_init(struct zebra_t
*zebra
)
1671 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1672 if (!zebra
->lsp_process_q
) {
1673 zlog_err("%s: could not initialise work queue!", __func__
);
1677 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1678 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1679 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1680 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1681 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1682 zebra
->lsp_process_q
->spec
.hold
= 10;
1688 /* Public functions */
1690 void kernel_lsp_pass_fail(zebra_lsp_t
*lsp
, enum southbound_results res
)
1692 struct nexthop
*nexthop
;
1693 zebra_nhlfe_t
*nhlfe
;
1699 case SOUTHBOUND_INSTALL_FAILURE
:
1700 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1701 clear_nhlfe_installed(lsp
);
1702 zlog_warn("LSP Install Failure: %u", lsp
->ile
.in_label
);
1704 case SOUTHBOUND_INSTALL_SUCCESS
:
1705 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1706 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1707 nexthop
= nhlfe
->nexthop
;
1711 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1712 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1715 case SOUTHBOUND_DELETE_SUCCESS
:
1716 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1717 clear_nhlfe_installed(lsp
);
1719 case SOUTHBOUND_DELETE_FAILURE
:
1720 zlog_warn("LSP Deletion Failure: %u", lsp
->ile
.in_label
);
1726 * Install dynamic LSP entry.
1728 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1729 struct route_entry
*re
)
1731 struct route_table
*table
;
1734 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1738 /* See if there is a configured label binding for this FEC. */
1739 fec
= fec_find(table
, &rn
->p
);
1740 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1743 /* We cannot install a label forwarding entry if local label is the
1744 * implicit-null label.
1746 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
1749 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1756 * Uninstall dynamic LSP entry, if any.
1758 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1759 struct route_entry
*re
)
1761 struct route_table
*table
;
1764 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1768 /* See if there is a configured label binding for this FEC. */
1769 fec
= fec_find(table
, &rn
->p
);
1770 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1773 /* Uninstall always removes all dynamic NHLFEs. */
1774 return lsp_uninstall(zvrf
, fec
->label
);
1778 * Registration from a client for the label binding for a FEC. If a binding
1779 * already exists, it is informed to the client.
1780 * NOTE: If there is a manually configured label binding, that is used.
1781 * Otherwise, if a label index is specified, it means we have to allocate the
1782 * label from a locally configured label block (SRGB), if one exists and index
1785 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1786 uint32_t label_index
, struct zserv
*client
)
1788 struct route_table
*table
;
1792 int label_change
= 0;
1795 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1799 if (IS_ZEBRA_DEBUG_MPLS
)
1800 prefix2str(p
, buf
, BUFSIZ
);
1803 fec
= fec_find(table
, p
);
1805 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1807 prefix2str(p
, buf
, BUFSIZ
);
1809 "Failed to add FEC %s upon register, client %s",
1810 buf
, zebra_route_string(client
->proto
));
1814 old_label
= MPLS_INVALID_LABEL
;
1817 /* Client may register same FEC with different label index. */
1819 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1820 if (!new_client
&& fec
->label_index
== label_index
)
1821 /* Duplicate register */
1824 /* Save current label, update label index */
1825 old_label
= fec
->label
;
1826 fec
->label_index
= label_index
;
1830 listnode_add(fec
->client_list
, client
);
1832 if (IS_ZEBRA_DEBUG_MPLS
)
1833 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1834 label_index
, new_client
? "registered" : "updated",
1835 zebra_route_string(client
->proto
));
1837 /* If not a configured FEC, derive the local label (from label index)
1840 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1841 fec_derive_label_from_index(zvrf
, fec
);
1843 /* If no label change, exit. */
1844 if (fec
->label
== old_label
)
1850 /* If new client or label change, update client and install or uninstall
1851 * label forwarding entry as needed.
1853 /* Inform client of label, if needed. */
1854 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1855 if (IS_ZEBRA_DEBUG_MPLS
)
1856 zlog_debug("Update client label %u", fec
->label
);
1857 fec_send(fec
, client
);
1860 if (new_client
|| label_change
)
1861 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1867 * Deregistration from a client for the label binding for a FEC. The FEC
1868 * itself is deleted if no other registered clients exist and there is no
1869 * label bound to the FEC.
1871 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1872 struct zserv
*client
)
1874 struct route_table
*table
;
1878 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1882 if (IS_ZEBRA_DEBUG_MPLS
)
1883 prefix2str(p
, buf
, BUFSIZ
);
1885 fec
= fec_find(table
, p
);
1887 prefix2str(p
, buf
, BUFSIZ
);
1888 zlog_err("Failed to find FEC %s upon unregister, client %s",
1889 buf
, zebra_route_string(client
->proto
));
1893 listnode_delete(fec
->client_list
, client
);
1895 if (IS_ZEBRA_DEBUG_MPLS
)
1896 zlog_debug("FEC %s unregistered by client %s", buf
,
1897 zebra_route_string(client
->proto
));
1899 /* If not a configured entry, delete the FEC if no other clients. Before
1900 * deleting, see if any LSP needs to be uninstalled.
1902 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1903 && list_isempty(fec
->client_list
)) {
1904 mpls_label_t old_label
= fec
->label
;
1905 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1906 fec_change_update_lsp(zvrf
, fec
, old_label
);
1914 * Cleanup any FECs registered by this client.
1916 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
1918 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1919 struct route_node
*rn
;
1921 struct listnode
*node
;
1922 struct zserv
*fec_client
;
1925 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1926 if (zvrf
->fec_table
[af
] == NULL
)
1929 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1930 rn
= route_next(rn
)) {
1932 if (!fec
|| list_isempty(fec
->client_list
))
1935 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1937 if (fec_client
== client
) {
1938 listnode_delete(fec
->client_list
,
1940 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1941 && list_isempty(fec
->client_list
))
1953 * Return FEC (if any) to which this label is bound.
1954 * Note: Only works for per-prefix binding and when the label is not
1956 * TODO: Currently walks entire table, can optimize later with another
1959 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1962 struct route_node
*rn
;
1966 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1967 if (zvrf
->fec_table
[af
] == NULL
)
1970 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1971 rn
= route_next(rn
)) {
1975 if (fec
->label
== label
)
1984 * Inform if specified label is currently bound to a FEC or not.
1986 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
1988 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
1992 * Add static FEC to label binding. If there are clients registered for this
1993 * FEC, notify them. If there are labeled routes for this FEC, install the
1994 * label forwarding entry.
1996 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1997 mpls_label_t in_label
)
1999 struct route_table
*table
;
2002 mpls_label_t old_label
;
2005 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2009 if (IS_ZEBRA_DEBUG_MPLS
)
2010 prefix2str(p
, buf
, BUFSIZ
);
2012 /* Update existing FEC or create a new one. */
2013 fec
= fec_find(table
, p
);
2015 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2016 MPLS_INVALID_LABEL_INDEX
);
2018 prefix2str(p
, buf
, BUFSIZ
);
2019 zlog_err("Failed to add FEC %s upon config", buf
);
2023 if (IS_ZEBRA_DEBUG_MPLS
)
2024 zlog_debug("Add fec %s label %u", buf
, in_label
);
2026 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2027 if (fec
->label
== in_label
)
2028 /* Duplicate config */
2031 /* Label change, update clients. */
2032 old_label
= fec
->label
;
2033 if (IS_ZEBRA_DEBUG_MPLS
)
2034 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2036 fec
->label
= in_label
;
2037 fec_update_clients(fec
);
2039 /* Update label forwarding entries appropriately */
2040 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2047 * Remove static FEC to label binding. If there are no clients registered
2048 * for this FEC, delete the FEC; else notify clients
2049 * Note: Upon delete of static binding, if label index exists for this FEC,
2050 * client may need to be updated with derived label.
2052 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2054 struct route_table
*table
;
2056 mpls_label_t old_label
;
2059 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2063 fec
= fec_find(table
, p
);
2065 prefix2str(p
, buf
, BUFSIZ
);
2066 zlog_err("Failed to find FEC %s upon delete", buf
);
2070 if (IS_ZEBRA_DEBUG_MPLS
) {
2071 prefix2str(p
, buf
, BUFSIZ
);
2072 zlog_debug("Delete fec %s label index %u", buf
,
2076 old_label
= fec
->label
;
2077 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2078 fec
->label
= MPLS_INVALID_LABEL
;
2080 /* If no client exists, just delete the FEC. */
2081 if (list_isempty(fec
->client_list
)) {
2086 /* Derive the local label (from label index) or reset it. */
2087 fec_derive_label_from_index(zvrf
, fec
);
2089 /* If there is a label change, update clients. */
2090 if (fec
->label
== old_label
)
2092 fec_update_clients(fec
);
2094 /* Update label forwarding entries appropriately */
2095 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2099 * Display MPLS FEC to label binding configuration (VTY command handler).
2101 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2103 struct route_node
*rn
;
2109 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2110 if (zvrf
->fec_table
[af
] == NULL
)
2113 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2114 rn
= route_next(rn
)) {
2121 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2125 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2126 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2127 label2str(fec
->label
, lstr
, BUFSIZ
));
2135 * Display MPLS FEC to label binding (VTY command handler).
2137 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2139 struct route_node
*rn
;
2142 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2143 if (zvrf
->fec_table
[af
] == NULL
)
2146 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2147 rn
= route_next(rn
)) {
2150 fec_print(rn
->info
, vty
);
2156 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2158 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2161 struct route_table
*table
;
2162 struct route_node
*rn
;
2164 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2169 rn
= route_node_lookup(table
, p
);
2173 route_unlock_node(rn
);
2177 fec_print(rn
->info
, vty
);
2180 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2181 enum lsp_types_t type
, mpls_label_t label
)
2183 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2184 nexthop_add_labels(nexthop
, type
, 1, &label
);
2185 else if (!add
&& nexthop
->nh_label_type
== type
)
2186 nexthop_del_labels(nexthop
);
2194 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2196 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2197 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2198 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2199 mpls_label_t out_label
)
2201 struct route_table
*table
;
2202 struct route_node
*rn
;
2203 struct route_entry
*re
;
2204 struct nexthop
*nexthop
;
2208 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2213 /* Lookup existing route */
2214 rn
= route_node_get(table
, prefix
);
2215 RNODE_FOREACH_RE (rn
, re
) {
2216 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2218 if (re
->distance
== distance
)
2226 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2227 switch (nexthop
->type
) {
2228 case NEXTHOP_TYPE_IPV4
:
2229 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2230 if (gtype
!= NEXTHOP_TYPE_IPV4
2231 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2233 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2235 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2236 && nexthop
->ifindex
!= ifindex
)
2238 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2243 case NEXTHOP_TYPE_IPV6
:
2244 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2245 if (gtype
!= NEXTHOP_TYPE_IPV6
2246 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2248 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2250 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2251 && nexthop
->ifindex
!= ifindex
)
2253 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2266 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2267 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2274 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2275 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2276 * the out-label for an existing NHLFE (update case).
2278 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2279 mpls_label_t in_label
, mpls_label_t out_label
,
2280 enum nexthop_types_t gtype
, union g_addr
*gate
,
2283 struct hash
*lsp_table
;
2284 zebra_ile_t tmp_ile
;
2286 zebra_nhlfe_t
*nhlfe
;
2290 lsp_table
= zvrf
->lsp_table
;
2294 /* If entry is present, exit. */
2295 tmp_ile
.in_label
= in_label
;
2296 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2299 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2301 struct nexthop
*nh
= nhlfe
->nexthop
;
2304 assert(nh
->nh_label
);
2306 /* Clear deleted flag (in case it was set) */
2307 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2308 if (nh
->nh_label
->label
[0] == out_label
)
2312 if (IS_ZEBRA_DEBUG_MPLS
) {
2313 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2315 "LSP in-label %u type %d nexthop %s "
2316 "out-label changed to %u (old %u)",
2317 in_label
, type
, buf
, out_label
,
2318 nh
->nh_label
->label
[0]);
2321 /* Update out label, trigger processing. */
2322 nh
->nh_label
->label
[0] = out_label
;
2324 /* Add LSP entry to this nexthop */
2325 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2329 if (IS_ZEBRA_DEBUG_MPLS
) {
2330 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2332 "Add LSP in-label %u type %d nexthop %s "
2334 in_label
, type
, buf
, out_label
);
2337 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2340 /* Mark NHLFE, queue LSP for processing. */
2341 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2342 if (lsp_processq_add(lsp
))
2349 * Uninstall a particular NHLFE in the forwarding table. If this is
2350 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2352 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2353 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2354 union g_addr
*gate
, ifindex_t ifindex
)
2356 struct hash
*lsp_table
;
2357 zebra_ile_t tmp_ile
;
2359 zebra_nhlfe_t
*nhlfe
;
2363 lsp_table
= zvrf
->lsp_table
;
2367 /* If entry is not present, exit. */
2368 tmp_ile
.in_label
= in_label
;
2369 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2372 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2376 if (IS_ZEBRA_DEBUG_MPLS
) {
2377 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2378 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2379 in_label
, type
, buf
, nhlfe
->flags
);
2382 /* Mark NHLFE for delete or directly delete, as appropriate. */
2383 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2384 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2385 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2386 if (lsp_processq_add(lsp
))
2391 /* Free LSP entry if no other NHLFEs and not scheduled. */
2392 if (!lsp
->nhlfe_list
2393 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2394 if (IS_ZEBRA_DEBUG_MPLS
)
2395 zlog_debug("Free LSP in-label %u flags 0x%x",
2396 lsp
->ile
.in_label
, lsp
->flags
);
2398 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2400 XFREE(MTYPE_LSP
, lsp
);
2407 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2408 * If no other NHLFEs exist, the entry would be deleted.
2410 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2413 struct hash
*lsp_table
;
2415 lsp
= (zebra_lsp_t
*)backet
->data
;
2416 if (!lsp
|| !lsp
->nhlfe_list
)
2423 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2427 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2429 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2431 struct route_table
*table
;
2432 struct route_node
*rn
;
2433 struct route_entry
*re
;
2434 struct nexthop
*nexthop
;
2437 /* Process routes of interested address-families. */
2438 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2442 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2444 RNODE_FOREACH_RE (rn
, re
) {
2445 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2446 nexthop
= nexthop
->next
) {
2447 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2450 nexthop_del_labels(nexthop
);
2451 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2452 SET_FLAG(re
->status
,
2453 ROUTE_ENTRY_LABELS_CHANGED
);
2463 #if defined(HAVE_CUMULUS)
2465 * Check that the label values used in LSP creation are consistent. The
2466 * main criteria is that if there is ECMP, the label operation must still
2467 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2468 * to current HW restrictions.
2470 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2471 mpls_label_t in_label
,
2472 mpls_label_t out_label
,
2473 enum nexthop_types_t gtype
,
2474 union g_addr
*gate
, ifindex_t ifindex
)
2476 struct hash
*slsp_table
;
2477 zebra_ile_t tmp_ile
;
2479 zebra_snhlfe_t
*snhlfe
;
2482 slsp_table
= zvrf
->slsp_table
;
2486 /* If entry is not present, exit. */
2487 tmp_ile
.in_label
= in_label
;
2488 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2492 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2494 if (snhlfe
->out_label
== out_label
)
2497 /* If not only NHLFE, cannot allow label change. */
2498 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2501 /* If other NHLFEs exist, label operation must match. */
2502 if (slsp
->snhlfe_list
) {
2505 cur_op
= (slsp
->snhlfe_list
->out_label
2506 == MPLS_LABEL_IMPLICIT_NULL
);
2507 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2508 if (cur_op
!= new_op
)
2513 /* Label values are good. */
2516 #endif /* HAVE_CUMULUS */
2519 * Add static LSP entry. This may be the first entry for this incoming label
2520 * or an additional nexthop; an existing entry may also have outgoing label
2522 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2525 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2526 mpls_label_t out_label
,
2527 enum nexthop_types_t gtype
, union g_addr
*gate
,
2530 struct hash
*slsp_table
;
2531 zebra_ile_t tmp_ile
;
2533 zebra_snhlfe_t
*snhlfe
;
2537 slsp_table
= zvrf
->slsp_table
;
2541 /* If entry is present, exit. */
2542 tmp_ile
.in_label
= in_label
;
2543 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2546 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2548 if (snhlfe
->out_label
== out_label
)
2552 if (IS_ZEBRA_DEBUG_MPLS
) {
2553 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2555 "Upd static LSP in-label %u nexthop %s "
2556 "out-label %u (old %u)",
2557 in_label
, buf
, out_label
, snhlfe
->out_label
);
2559 snhlfe
->out_label
= out_label
;
2561 /* Add static LSP entry to this nexthop */
2562 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2566 if (IS_ZEBRA_DEBUG_MPLS
) {
2567 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2569 "Add static LSP in-label %u nexthop %s out-label %u",
2570 in_label
, buf
, out_label
);
2574 /* (Re)Install LSP in the main table. */
2575 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2583 * Delete static LSP entry. This may be the delete of one particular
2584 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2586 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2587 * LSP configuration.
2589 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2590 enum nexthop_types_t gtype
, union g_addr
*gate
,
2593 struct hash
*slsp_table
;
2594 zebra_ile_t tmp_ile
;
2596 zebra_snhlfe_t
*snhlfe
;
2599 slsp_table
= zvrf
->slsp_table
;
2603 /* If entry is not present, exit. */
2604 tmp_ile
.in_label
= in_label
;
2605 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2609 /* Is it delete of entire LSP or a specific NHLFE? */
2610 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2611 if (IS_ZEBRA_DEBUG_MPLS
)
2612 zlog_debug("Del static LSP in-label %u", in_label
);
2614 /* Uninstall entire LSP from the main table. */
2615 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2617 /* Delete all static NHLFEs */
2618 snhlfe_del_all(slsp
);
2620 /* Find specific NHLFE, exit if not found. */
2621 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2625 if (IS_ZEBRA_DEBUG_MPLS
) {
2627 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2628 zlog_debug("Del static LSP in-label %u nexthop %s",
2632 /* Uninstall LSP from the main table. */
2633 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2636 /* Delete static LSP NHLFE */
2640 /* Remove entire static LSP entry if no NHLFE - valid in either case
2642 if (!slsp
->snhlfe_list
) {
2643 slsp
= hash_release(slsp_table
, &tmp_ile
);
2645 XFREE(MTYPE_SLSP
, slsp
);
2652 * Schedule all MPLS label forwarding entries for processing.
2653 * Called upon changes that may affect one or more of them such as
2654 * interface or nexthop state changes.
2656 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2660 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2664 * Display MPLS label forwarding table for a specific LSP
2665 * (VTY command handler).
2667 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2668 mpls_label_t label
, uint8_t use_json
)
2670 struct hash
*lsp_table
;
2672 zebra_ile_t tmp_ile
;
2673 json_object
*json
= NULL
;
2676 lsp_table
= zvrf
->lsp_table
;
2680 /* If entry is not present, exit. */
2681 tmp_ile
.in_label
= label
;
2682 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2687 json
= lsp_json(lsp
);
2688 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2689 json
, JSON_C_TO_STRING_PRETTY
));
2690 json_object_free(json
);
2692 lsp_print(lsp
, (void *)vty
);
2696 * Display MPLS label forwarding table (VTY command handler).
2698 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2702 json_object
*json
= NULL
;
2703 zebra_lsp_t
*lsp
= NULL
;
2704 zebra_nhlfe_t
*nhlfe
= NULL
;
2705 struct nexthop
*nexthop
= NULL
;
2706 struct listnode
*node
= NULL
;
2707 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2710 json
= json_object_new_object();
2712 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2713 json_object_object_add(
2714 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2717 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2718 json
, JSON_C_TO_STRING_PRETTY
));
2719 json_object_free(json
);
2721 vty_out(vty
, " Inbound Outbound\n");
2722 vty_out(vty
, " Label Type Nexthop Label\n");
2723 vty_out(vty
, "-------- ------- --------------- --------\n");
2725 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2726 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2727 nhlfe
= nhlfe
->next
) {
2728 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2729 nhlfe_type2str(nhlfe
->type
));
2730 nexthop
= nhlfe
->nexthop
;
2732 switch (nexthop
->type
) {
2733 case NEXTHOP_TYPE_IFINDEX
: {
2734 struct interface
*ifp
;
2736 ifp
= if_lookup_by_index(
2737 nexthop
->ifindex
, VRF_UNKNOWN
);
2738 vty_out(vty
, "%15s", ifp
->name
);
2741 case NEXTHOP_TYPE_IPV4
:
2742 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2743 vty_out(vty
, "%15s",
2744 inet_ntoa(nexthop
->gate
.ipv4
));
2746 case NEXTHOP_TYPE_IPV6
:
2747 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2748 vty_out(vty
, "%15s",
2750 &nexthop
->gate
.ipv6
,
2757 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
2758 vty_out(vty
, " %8s\n",
2773 list_delete_and_null(&lsp_list
);
2777 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2779 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2782 zebra_snhlfe_t
*snhlfe
;
2783 struct listnode
*node
;
2784 struct list
*slsp_list
=
2785 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2787 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2788 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2789 snhlfe
= snhlfe
->next
) {
2793 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2794 switch (snhlfe
->out_label
) {
2795 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2796 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
2797 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2799 case MPLS_LABEL_IMPLICIT_NULL
:
2800 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2803 sprintf(lstr
, "%u", snhlfe
->out_label
);
2807 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2812 list_delete_and_null(&slsp_list
);
2813 return (zvrf
->slsp_table
->count
? 1 : 0);
2817 * Add/update global label block.
2819 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
2822 zvrf
->mpls_srgb
.start_label
= start_label
;
2823 zvrf
->mpls_srgb
.end_label
= end_label
;
2825 /* Evaluate registered FECs to see if any get a label or not. */
2831 * Delete global label block.
2833 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2835 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2836 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2838 /* Process registered FECs to clear their local label, if needed. */
2844 * Display MPLS global label block configuration (VTY command handler).
2846 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2848 if (zvrf
->mpls_srgb
.start_label
== 0)
2851 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2852 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2853 vty_out(vty
, "mpls label global-block %u %u\n",
2854 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2861 * Called when VRF becomes inactive, cleans up information but keeps
2863 * NOTE: Currently supported only for default VRF.
2865 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
2867 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2871 * Called upon process exiting, need to delete LSP forwarding
2872 * entries from the kernel.
2873 * NOTE: Currently supported only for default VRF.
2875 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2877 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2878 hash_clean(zvrf
->lsp_table
, NULL
);
2879 hash_free(zvrf
->lsp_table
);
2880 hash_clean(zvrf
->slsp_table
, NULL
);
2881 hash_free(zvrf
->slsp_table
);
2882 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2883 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2887 * Allocate MPLS tables for this VRF and do other initialization.
2888 * NOTE: Currently supported only for default VRF.
2890 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2895 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
2896 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
2897 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2898 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2899 zvrf
->mpls_flags
= 0;
2900 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2901 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2905 * Global MPLS initialization.
2907 void zebra_mpls_init(void)
2911 if (mpls_kernel_init() < 0) {
2912 zlog_warn("Disabling MPLS support (no kernel support)");
2916 if (!mpls_processq_init(&zebrad
))
2919 hook_register(zapi_client_close
, zebra_mpls_cleanup_fecs_for_client
);