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 zserv_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
))
863 (void)kernel_del_lsp(lsp
);
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
);
917 switch (kernel_add_lsp(lsp
)) {
918 case DP_REQUEST_QUEUED
:
919 zlog_err("No current DataPlane interfaces can return this, please fix");
921 case DP_REQUEST_FAILURE
:
923 case DP_REQUEST_SUCCESS
:
924 zvrf
->lsp_installs
++;
929 /* Installed, may need an update and/or delete. */
932 switch (kernel_del_lsp(lsp
)) {
933 case DP_REQUEST_QUEUED
:
934 zlog_err("No current DataPlane interfaces can return this, please fix");
936 case DP_REQUEST_FAILURE
:
938 case DP_REQUEST_SUCCESS
:
939 zvrf
->lsp_removals
++;
942 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
943 zebra_nhlfe_t
*nhlfe
;
944 struct nexthop
*nexthop
;
946 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
947 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
950 * Any NHLFE that was installed but is not
951 * selected now needs to have its flags updated.
953 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
954 nhlfe
= nhlfe
->next
) {
955 nexthop
= nhlfe
->nexthop
;
959 if (CHECK_FLAG(nhlfe
->flags
,
960 NHLFE_FLAG_INSTALLED
)
961 && !CHECK_FLAG(nhlfe
->flags
,
962 NHLFE_FLAG_SELECTED
)) {
963 UNSET_FLAG(nhlfe
->flags
,
964 NHLFE_FLAG_INSTALLED
);
965 UNSET_FLAG(nexthop
->flags
,
970 switch (kernel_upd_lsp(lsp
)) {
971 case DP_REQUEST_QUEUED
:
972 zlog_err("No current DataPlane interfaces can return this, please fix");
974 case DP_REQUEST_FAILURE
:
976 case DP_REQUEST_SUCCESS
:
977 zvrf
->lsp_installs
++;
988 * Callback upon processing completion of a LSP forwarding entry.
990 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
992 struct zebra_vrf
*zvrf
;
994 struct hash
*lsp_table
;
995 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
997 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1000 lsp_table
= zvrf
->lsp_table
;
1001 if (!lsp_table
) // unexpected
1004 lsp
= (zebra_lsp_t
*)data
;
1005 if (!lsp
) // unexpected
1008 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1010 * delete LSP entry also.
1012 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1014 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1015 nhlfe_next
= nhlfe
->next
;
1016 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1020 if (!lsp
->nhlfe_list
) {
1021 if (IS_ZEBRA_DEBUG_MPLS
)
1022 zlog_debug("Free LSP in-label %u flags 0x%x",
1023 lsp
->ile
.in_label
, lsp
->flags
);
1025 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1027 XFREE(MTYPE_LSP
, lsp
);
1032 * Callback upon finishing the processing of all scheduled
1033 * LSP forwarding entries.
1035 static void lsp_processq_complete(struct work_queue
*wq
)
1037 /* Nothing to do for now. */
1041 * Add LSP forwarding entry to queue for subsequent processing.
1043 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1045 /* If already scheduled, exit. */
1046 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1049 if (zebrad
.lsp_process_q
== NULL
) {
1050 zlog_err("%s: work_queue does not exist!", __func__
);
1054 work_queue_add(zebrad
.lsp_process_q
, lsp
);
1055 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1060 * Callback to allocate LSP forwarding table entry.
1062 static void *lsp_alloc(void *p
)
1064 const zebra_ile_t
*ile
= p
;
1067 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1070 if (IS_ZEBRA_DEBUG_MPLS
)
1071 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1073 return ((void *)lsp
);
1077 * Create printable string for NHLFE entry.
1079 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1081 struct nexthop
*nexthop
;
1084 nexthop
= nhlfe
->nexthop
;
1085 switch (nexthop
->type
) {
1086 case NEXTHOP_TYPE_IPV4
:
1087 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1088 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1090 case NEXTHOP_TYPE_IPV6
:
1091 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1093 case NEXTHOP_TYPE_IFINDEX
:
1094 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1103 * Check if NHLFE matches with search info passed.
1105 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1106 union g_addr
*gate
, ifindex_t ifindex
)
1108 struct nexthop
*nhop
;
1111 nhop
= nhlfe
->nexthop
;
1115 if (nhop
->type
!= gtype
)
1118 switch (nhop
->type
) {
1119 case NEXTHOP_TYPE_IPV4
:
1120 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1121 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1122 sizeof(struct in_addr
));
1123 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1124 cmp
= !(nhop
->ifindex
== ifindex
);
1126 case NEXTHOP_TYPE_IPV6
:
1127 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1128 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1129 sizeof(struct in6_addr
));
1130 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1131 cmp
= !(nhop
->ifindex
== ifindex
);
1133 case NEXTHOP_TYPE_IFINDEX
:
1134 cmp
= !(nhop
->ifindex
== ifindex
);
1145 * Locate NHLFE that matches with passed info.
1147 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1148 enum nexthop_types_t gtype
, union g_addr
*gate
,
1151 zebra_nhlfe_t
*nhlfe
;
1156 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1157 if (nhlfe
->type
!= lsp_type
)
1159 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1167 * Add NHLFE. Base entry must have been created and duplicate
1170 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1171 enum nexthop_types_t gtype
, union g_addr
*gate
,
1172 ifindex_t ifindex
, mpls_label_t out_label
)
1174 zebra_nhlfe_t
*nhlfe
;
1175 struct nexthop
*nexthop
;
1180 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1185 nhlfe
->type
= lsp_type
;
1186 nhlfe
->distance
= lsp_distance(lsp_type
);
1188 nexthop
= nexthop_new();
1190 XFREE(MTYPE_NHLFE
, nhlfe
);
1193 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1195 nexthop
->vrf_id
= VRF_DEFAULT
;
1196 nexthop
->type
= gtype
;
1197 switch (nexthop
->type
) {
1198 case NEXTHOP_TYPE_IPV4
:
1199 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1200 nexthop
->gate
.ipv4
= gate
->ipv4
;
1202 nexthop
->ifindex
= ifindex
;
1204 case NEXTHOP_TYPE_IPV6
:
1205 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1206 nexthop
->gate
.ipv6
= gate
->ipv6
;
1208 nexthop
->ifindex
= ifindex
;
1210 case NEXTHOP_TYPE_IFINDEX
:
1211 nexthop
->ifindex
= ifindex
;
1214 nexthop_free(nexthop
);
1215 XFREE(MTYPE_NHLFE
, nhlfe
);
1220 nhlfe
->nexthop
= nexthop
;
1221 if (lsp
->nhlfe_list
)
1222 lsp
->nhlfe_list
->prev
= nhlfe
;
1223 nhlfe
->next
= lsp
->nhlfe_list
;
1224 lsp
->nhlfe_list
= nhlfe
;
1230 * Delete NHLFE. Entry must be present on list.
1232 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1245 nexthop_free(nhlfe
->nexthop
);
1247 /* Unlink from LSP */
1249 nhlfe
->next
->prev
= nhlfe
->prev
;
1251 nhlfe
->prev
->next
= nhlfe
->next
;
1253 lsp
->nhlfe_list
= nhlfe
->next
;
1255 if (nhlfe
== lsp
->best_nhlfe
)
1256 lsp
->best_nhlfe
= NULL
;
1258 XFREE(MTYPE_NHLFE
, nhlfe
);
1264 * Update label for NHLFE entry.
1266 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1267 struct mpls_label_stack
*nh_label
)
1269 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1272 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1273 enum lsp_types_t type
)
1275 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1276 int schedule_lsp
= 0;
1279 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1280 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1281 nhlfe_next
= nhlfe
->next
;
1283 /* Skip non-static NHLFEs */
1284 if (nhlfe
->type
!= type
)
1287 if (IS_ZEBRA_DEBUG_MPLS
) {
1288 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1290 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1291 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1294 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1295 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1296 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1303 /* Queue LSP for processing, if needed, else delete. */
1305 if (lsp_processq_add(lsp
))
1307 } else if (!lsp
->nhlfe_list
1308 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1309 if (IS_ZEBRA_DEBUG_MPLS
)
1310 zlog_debug("Free LSP in-label %u flags 0x%x",
1311 lsp
->ile
.in_label
, lsp
->flags
);
1313 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1315 XFREE(MTYPE_LSP
, lsp
);
1322 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1323 * If no other NHLFEs exist, the entry would be deleted.
1325 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1326 mpls_label_t in_label
)
1328 struct hash
*lsp_table
;
1329 zebra_ile_t tmp_ile
;
1333 lsp_table
= zvrf
->lsp_table
;
1337 /* If entry is not present, exit. */
1338 tmp_ile
.in_label
= in_label
;
1339 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1340 if (!lsp
|| !lsp
->nhlfe_list
)
1343 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1346 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1349 json_object
*json_nhlfe
= NULL
;
1350 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1352 json_nhlfe
= json_object_new_object();
1353 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1354 json_object_int_add(json_nhlfe
, "outLabel",
1355 nexthop
->nh_label
->label
[0]);
1356 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1358 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1359 json_object_boolean_true_add(json_nhlfe
, "installed");
1361 switch (nexthop
->type
) {
1362 case NEXTHOP_TYPE_IPV4
:
1363 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1364 json_object_string_add(json_nhlfe
, "nexthop",
1365 inet_ntoa(nexthop
->gate
.ipv4
));
1367 case NEXTHOP_TYPE_IPV6
:
1368 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1369 json_object_string_add(
1370 json_nhlfe
, "nexthop",
1371 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1373 if (nexthop
->ifindex
)
1374 json_object_string_add(json_nhlfe
, "interface",
1375 ifindex2ifname(nexthop
->ifindex
,
1385 * Print the NHLFE for a LSP forwarding entry.
1387 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1389 struct nexthop
*nexthop
;
1392 nexthop
= nhlfe
->nexthop
;
1393 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1396 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1397 nhlfe_type2str(nhlfe
->type
),
1398 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1400 switch (nexthop
->type
) {
1401 case NEXTHOP_TYPE_IPV4
:
1402 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1403 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1404 if (nexthop
->ifindex
)
1405 vty_out(vty
, " dev %s",
1406 ifindex2ifname(nexthop
->ifindex
,
1409 case NEXTHOP_TYPE_IPV6
:
1410 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1411 vty_out(vty
, " via %s",
1412 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1413 if (nexthop
->ifindex
)
1414 vty_out(vty
, " dev %s",
1415 ifindex2ifname(nexthop
->ifindex
,
1422 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1428 * Print an LSP forwarding entry.
1430 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1432 zebra_nhlfe_t
*nhlfe
;
1435 vty
= (struct vty
*)ctxt
;
1437 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1438 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1441 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1442 nhlfe_print(nhlfe
, vty
);
1446 * JSON objects for an LSP forwarding entry.
1448 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1450 zebra_nhlfe_t
*nhlfe
= NULL
;
1451 json_object
*json
= json_object_new_object();
1452 json_object
*json_nhlfe_list
= json_object_new_array();
1454 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1456 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1457 json_object_boolean_true_add(json
, "installed");
1459 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1460 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1462 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1467 /* Return a sorted linked list of the hash contents */
1468 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1471 struct hash_backet
*hb
;
1472 struct list
*sorted_list
= list_new();
1474 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1476 for (i
= 0; i
< hash
->size
; i
++)
1477 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1478 listnode_add_sort(sorted_list
, hb
->data
);
1484 * Compare two LSPs based on their label values.
1486 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1488 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1491 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1498 * Callback to allocate static LSP.
1500 static void *slsp_alloc(void *p
)
1502 const zebra_ile_t
*ile
= p
;
1505 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1507 return ((void *)slsp
);
1511 * Compare two static LSPs based on their label values.
1513 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1515 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1518 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1525 * Check if static NHLFE matches with search info passed.
1527 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1528 union g_addr
*gate
, ifindex_t ifindex
)
1532 if (snhlfe
->gtype
!= gtype
)
1535 switch (snhlfe
->gtype
) {
1536 case NEXTHOP_TYPE_IPV4
:
1537 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1538 sizeof(struct in_addr
));
1540 case NEXTHOP_TYPE_IPV6
:
1541 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1542 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1543 sizeof(struct in6_addr
));
1544 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1545 cmp
= !(snhlfe
->ifindex
== ifindex
);
1555 * Locate static NHLFE that matches with passed info.
1557 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1558 enum nexthop_types_t gtype
,
1559 union g_addr
*gate
, ifindex_t ifindex
)
1561 zebra_snhlfe_t
*snhlfe
;
1566 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1567 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1576 * Add static NHLFE. Base LSP config entry must have been created
1577 * and duplicate check done.
1579 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1580 enum nexthop_types_t gtype
,
1581 union g_addr
*gate
, ifindex_t ifindex
,
1582 mpls_label_t out_label
)
1584 zebra_snhlfe_t
*snhlfe
;
1589 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1590 snhlfe
->slsp
= slsp
;
1591 snhlfe
->out_label
= out_label
;
1592 snhlfe
->gtype
= gtype
;
1594 case NEXTHOP_TYPE_IPV4
:
1595 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1597 case NEXTHOP_TYPE_IPV6
:
1598 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1599 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1601 snhlfe
->ifindex
= ifindex
;
1604 XFREE(MTYPE_SNHLFE
, snhlfe
);
1608 if (slsp
->snhlfe_list
)
1609 slsp
->snhlfe_list
->prev
= snhlfe
;
1610 snhlfe
->next
= slsp
->snhlfe_list
;
1611 slsp
->snhlfe_list
= snhlfe
;
1617 * Delete static NHLFE. Entry must be present on list.
1619 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1626 slsp
= snhlfe
->slsp
;
1631 snhlfe
->next
->prev
= snhlfe
->prev
;
1633 snhlfe
->prev
->next
= snhlfe
->next
;
1635 slsp
->snhlfe_list
= snhlfe
->next
;
1637 snhlfe
->prev
= snhlfe
->next
= NULL
;
1639 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1640 XFREE(MTYPE_SNHLFE
, snhlfe
);
1646 * Delete all static NHLFE entries for this LSP (in label).
1648 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1650 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1655 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1656 snhlfe_next
= snhlfe
->next
;
1664 * Create printable string for NHLFE configuration.
1666 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1669 switch (snhlfe
->gtype
) {
1670 case NEXTHOP_TYPE_IPV4
:
1671 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1673 case NEXTHOP_TYPE_IPV6
:
1674 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1675 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1676 if (snhlfe
->ifindex
)
1678 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1688 * Initialize work queue for processing changed LSPs.
1690 static int mpls_processq_init(struct zebra_t
*zebra
)
1692 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1693 if (!zebra
->lsp_process_q
) {
1694 zlog_err("%s: could not initialise work queue!", __func__
);
1698 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1699 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1700 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1701 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1702 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1703 zebra
->lsp_process_q
->spec
.hold
= 10;
1709 /* Public functions */
1711 void kernel_lsp_pass_fail(zebra_lsp_t
*lsp
, enum dp_results res
)
1713 struct nexthop
*nexthop
;
1714 zebra_nhlfe_t
*nhlfe
;
1720 case DP_INSTALL_FAILURE
:
1721 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1722 clear_nhlfe_installed(lsp
);
1723 zlog_warn("LSP Install Failure: %u", lsp
->ile
.in_label
);
1725 case DP_INSTALL_SUCCESS
:
1726 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1727 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1728 nexthop
= nhlfe
->nexthop
;
1732 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1733 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1736 case DP_DELETE_SUCCESS
:
1737 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1738 clear_nhlfe_installed(lsp
);
1740 case DP_DELETE_FAILURE
:
1741 zlog_warn("LSP Deletion Failure: %u", lsp
->ile
.in_label
);
1747 * Install dynamic LSP entry.
1749 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1750 struct route_entry
*re
)
1752 struct route_table
*table
;
1755 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1759 /* See if there is a configured label binding for this FEC. */
1760 fec
= fec_find(table
, &rn
->p
);
1761 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1764 /* We cannot install a label forwarding entry if local label is the
1765 * implicit-null label.
1767 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
1770 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1777 * Uninstall dynamic LSP entry, if any.
1779 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1780 struct route_entry
*re
)
1782 struct route_table
*table
;
1785 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1789 /* See if there is a configured label binding for this FEC. */
1790 fec
= fec_find(table
, &rn
->p
);
1791 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1794 /* Uninstall always removes all dynamic NHLFEs. */
1795 return lsp_uninstall(zvrf
, fec
->label
);
1799 * Registration from a client for the label binding for a FEC. If a binding
1800 * already exists, it is informed to the client.
1801 * NOTE: If there is a manually configured label binding, that is used.
1802 * Otherwise, if a label index is specified, it means we have to allocate the
1803 * label from a locally configured label block (SRGB), if one exists and index
1806 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1807 uint32_t label_index
, struct zserv
*client
)
1809 struct route_table
*table
;
1813 int label_change
= 0;
1816 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1820 if (IS_ZEBRA_DEBUG_MPLS
)
1821 prefix2str(p
, buf
, BUFSIZ
);
1824 fec
= fec_find(table
, p
);
1826 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1828 prefix2str(p
, buf
, BUFSIZ
);
1830 "Failed to add FEC %s upon register, client %s",
1831 buf
, zebra_route_string(client
->proto
));
1835 old_label
= MPLS_INVALID_LABEL
;
1838 /* Client may register same FEC with different label index. */
1840 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1841 if (!new_client
&& fec
->label_index
== label_index
)
1842 /* Duplicate register */
1845 /* Save current label, update label index */
1846 old_label
= fec
->label
;
1847 fec
->label_index
= label_index
;
1851 listnode_add(fec
->client_list
, client
);
1853 if (IS_ZEBRA_DEBUG_MPLS
)
1854 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1855 label_index
, new_client
? "registered" : "updated",
1856 zebra_route_string(client
->proto
));
1858 /* If not a configured FEC, derive the local label (from label index)
1861 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1862 fec_derive_label_from_index(zvrf
, fec
);
1864 /* If no label change, exit. */
1865 if (fec
->label
== old_label
)
1871 /* If new client or label change, update client and install or uninstall
1872 * label forwarding entry as needed.
1874 /* Inform client of label, if needed. */
1875 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1876 if (IS_ZEBRA_DEBUG_MPLS
)
1877 zlog_debug("Update client label %u", fec
->label
);
1878 fec_send(fec
, client
);
1881 if (new_client
|| label_change
)
1882 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1888 * Deregistration from a client for the label binding for a FEC. The FEC
1889 * itself is deleted if no other registered clients exist and there is no
1890 * label bound to the FEC.
1892 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1893 struct zserv
*client
)
1895 struct route_table
*table
;
1899 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1903 if (IS_ZEBRA_DEBUG_MPLS
)
1904 prefix2str(p
, buf
, BUFSIZ
);
1906 fec
= fec_find(table
, p
);
1908 prefix2str(p
, buf
, BUFSIZ
);
1909 zlog_err("Failed to find FEC %s upon unregister, client %s",
1910 buf
, zebra_route_string(client
->proto
));
1914 listnode_delete(fec
->client_list
, client
);
1916 if (IS_ZEBRA_DEBUG_MPLS
)
1917 zlog_debug("FEC %s unregistered by client %s", buf
,
1918 zebra_route_string(client
->proto
));
1920 /* If not a configured entry, delete the FEC if no other clients. Before
1921 * deleting, see if any LSP needs to be uninstalled.
1923 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1924 && list_isempty(fec
->client_list
)) {
1925 mpls_label_t old_label
= fec
->label
;
1926 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1927 fec_change_update_lsp(zvrf
, fec
, old_label
);
1935 * Cleanup any FECs registered by this client.
1937 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
1939 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1940 struct route_node
*rn
;
1942 struct listnode
*node
;
1943 struct zserv
*fec_client
;
1946 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1947 if (zvrf
->fec_table
[af
] == NULL
)
1950 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1951 rn
= route_next(rn
)) {
1953 if (!fec
|| list_isempty(fec
->client_list
))
1956 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1958 if (fec_client
== client
) {
1959 listnode_delete(fec
->client_list
,
1961 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1962 && list_isempty(fec
->client_list
))
1974 * Return FEC (if any) to which this label is bound.
1975 * Note: Only works for per-prefix binding and when the label is not
1977 * TODO: Currently walks entire table, can optimize later with another
1980 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1983 struct route_node
*rn
;
1987 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1988 if (zvrf
->fec_table
[af
] == NULL
)
1991 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1992 rn
= route_next(rn
)) {
1996 if (fec
->label
== label
)
2005 * Inform if specified label is currently bound to a FEC or not.
2007 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2009 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2013 * Add static FEC to label binding. If there are clients registered for this
2014 * FEC, notify them. If there are labeled routes for this FEC, install the
2015 * label forwarding entry.
2017 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2018 mpls_label_t in_label
)
2020 struct route_table
*table
;
2023 mpls_label_t old_label
;
2026 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2030 if (IS_ZEBRA_DEBUG_MPLS
)
2031 prefix2str(p
, buf
, BUFSIZ
);
2033 /* Update existing FEC or create a new one. */
2034 fec
= fec_find(table
, p
);
2036 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2037 MPLS_INVALID_LABEL_INDEX
);
2039 prefix2str(p
, buf
, BUFSIZ
);
2040 zlog_err("Failed to add FEC %s upon config", buf
);
2044 if (IS_ZEBRA_DEBUG_MPLS
)
2045 zlog_debug("Add fec %s label %u", buf
, in_label
);
2047 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2048 if (fec
->label
== in_label
)
2049 /* Duplicate config */
2052 /* Label change, update clients. */
2053 old_label
= fec
->label
;
2054 if (IS_ZEBRA_DEBUG_MPLS
)
2055 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2057 fec
->label
= in_label
;
2058 fec_update_clients(fec
);
2060 /* Update label forwarding entries appropriately */
2061 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2068 * Remove static FEC to label binding. If there are no clients registered
2069 * for this FEC, delete the FEC; else notify clients
2070 * Note: Upon delete of static binding, if label index exists for this FEC,
2071 * client may need to be updated with derived label.
2073 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2075 struct route_table
*table
;
2077 mpls_label_t old_label
;
2080 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2084 fec
= fec_find(table
, p
);
2086 prefix2str(p
, buf
, BUFSIZ
);
2087 zlog_err("Failed to find FEC %s upon delete", buf
);
2091 if (IS_ZEBRA_DEBUG_MPLS
) {
2092 prefix2str(p
, buf
, BUFSIZ
);
2093 zlog_debug("Delete fec %s label index %u", buf
,
2097 old_label
= fec
->label
;
2098 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2099 fec
->label
= MPLS_INVALID_LABEL
;
2101 /* If no client exists, just delete the FEC. */
2102 if (list_isempty(fec
->client_list
)) {
2107 /* Derive the local label (from label index) or reset it. */
2108 fec_derive_label_from_index(zvrf
, fec
);
2110 /* If there is a label change, update clients. */
2111 if (fec
->label
== old_label
)
2113 fec_update_clients(fec
);
2115 /* Update label forwarding entries appropriately */
2116 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2120 * Display MPLS FEC to label binding configuration (VTY command handler).
2122 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2124 struct route_node
*rn
;
2130 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2131 if (zvrf
->fec_table
[af
] == NULL
)
2134 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2135 rn
= route_next(rn
)) {
2142 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2146 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2147 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2148 label2str(fec
->label
, lstr
, BUFSIZ
));
2156 * Display MPLS FEC to label binding (VTY command handler).
2158 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2160 struct route_node
*rn
;
2163 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2164 if (zvrf
->fec_table
[af
] == NULL
)
2167 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2168 rn
= route_next(rn
)) {
2171 fec_print(rn
->info
, vty
);
2177 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2179 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2182 struct route_table
*table
;
2183 struct route_node
*rn
;
2185 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2190 rn
= route_node_lookup(table
, p
);
2194 route_unlock_node(rn
);
2198 fec_print(rn
->info
, vty
);
2201 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2202 enum lsp_types_t type
, mpls_label_t label
)
2204 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2205 nexthop_add_labels(nexthop
, type
, 1, &label
);
2206 else if (!add
&& nexthop
->nh_label_type
== type
)
2207 nexthop_del_labels(nexthop
);
2215 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2217 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2218 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2219 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2220 mpls_label_t out_label
)
2222 struct route_table
*table
;
2223 struct route_node
*rn
;
2224 struct route_entry
*re
;
2225 struct nexthop
*nexthop
;
2229 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2234 /* Lookup existing route */
2235 rn
= route_node_get(table
, prefix
);
2236 RNODE_FOREACH_RE (rn
, re
) {
2237 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2239 if (re
->distance
== distance
)
2247 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2248 switch (nexthop
->type
) {
2249 case NEXTHOP_TYPE_IPV4
:
2250 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2251 if (gtype
!= NEXTHOP_TYPE_IPV4
2252 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2254 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2256 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2257 && nexthop
->ifindex
!= ifindex
)
2259 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2264 case NEXTHOP_TYPE_IPV6
:
2265 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2266 if (gtype
!= NEXTHOP_TYPE_IPV6
2267 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2269 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2271 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2272 && nexthop
->ifindex
!= ifindex
)
2274 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2287 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2288 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2295 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2296 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2297 * the out-label for an existing NHLFE (update case).
2299 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2300 mpls_label_t in_label
, mpls_label_t out_label
,
2301 enum nexthop_types_t gtype
, union g_addr
*gate
,
2304 struct hash
*lsp_table
;
2305 zebra_ile_t tmp_ile
;
2307 zebra_nhlfe_t
*nhlfe
;
2311 lsp_table
= zvrf
->lsp_table
;
2315 /* If entry is present, exit. */
2316 tmp_ile
.in_label
= in_label
;
2317 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2320 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2322 struct nexthop
*nh
= nhlfe
->nexthop
;
2325 assert(nh
->nh_label
);
2327 /* Clear deleted flag (in case it was set) */
2328 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2329 if (nh
->nh_label
->label
[0] == out_label
)
2333 if (IS_ZEBRA_DEBUG_MPLS
) {
2334 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2336 "LSP in-label %u type %d nexthop %s "
2337 "out-label changed to %u (old %u)",
2338 in_label
, type
, buf
, out_label
,
2339 nh
->nh_label
->label
[0]);
2342 /* Update out label, trigger processing. */
2343 nh
->nh_label
->label
[0] = out_label
;
2345 /* Add LSP entry to this nexthop */
2346 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2350 if (IS_ZEBRA_DEBUG_MPLS
) {
2351 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2353 "Add LSP in-label %u type %d nexthop %s "
2355 in_label
, type
, buf
, out_label
);
2358 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2361 /* Mark NHLFE, queue LSP for processing. */
2362 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2363 if (lsp_processq_add(lsp
))
2370 * Uninstall a particular NHLFE in the forwarding table. If this is
2371 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2373 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2374 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2375 union g_addr
*gate
, ifindex_t ifindex
)
2377 struct hash
*lsp_table
;
2378 zebra_ile_t tmp_ile
;
2380 zebra_nhlfe_t
*nhlfe
;
2384 lsp_table
= zvrf
->lsp_table
;
2388 /* If entry is not present, exit. */
2389 tmp_ile
.in_label
= in_label
;
2390 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2393 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2397 if (IS_ZEBRA_DEBUG_MPLS
) {
2398 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2399 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2400 in_label
, type
, buf
, nhlfe
->flags
);
2403 /* Mark NHLFE for delete or directly delete, as appropriate. */
2404 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2405 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2406 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2407 if (lsp_processq_add(lsp
))
2412 /* Free LSP entry if no other NHLFEs and not scheduled. */
2413 if (!lsp
->nhlfe_list
2414 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2415 if (IS_ZEBRA_DEBUG_MPLS
)
2416 zlog_debug("Free LSP in-label %u flags 0x%x",
2417 lsp
->ile
.in_label
, lsp
->flags
);
2419 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2421 XFREE(MTYPE_LSP
, lsp
);
2428 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2429 * If no other NHLFEs exist, the entry would be deleted.
2431 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2434 struct hash
*lsp_table
;
2436 lsp
= (zebra_lsp_t
*)backet
->data
;
2437 if (!lsp
|| !lsp
->nhlfe_list
)
2444 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2448 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2450 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2452 struct route_table
*table
;
2453 struct route_node
*rn
;
2454 struct route_entry
*re
;
2455 struct nexthop
*nexthop
;
2458 /* Process routes of interested address-families. */
2459 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2463 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2465 RNODE_FOREACH_RE (rn
, re
) {
2466 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2467 nexthop
= nexthop
->next
) {
2468 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2471 nexthop_del_labels(nexthop
);
2472 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2473 SET_FLAG(re
->status
,
2474 ROUTE_ENTRY_LABELS_CHANGED
);
2484 #if defined(HAVE_CUMULUS)
2486 * Check that the label values used in LSP creation are consistent. The
2487 * main criteria is that if there is ECMP, the label operation must still
2488 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2489 * to current HW restrictions.
2491 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2492 mpls_label_t in_label
,
2493 mpls_label_t out_label
,
2494 enum nexthop_types_t gtype
,
2495 union g_addr
*gate
, ifindex_t ifindex
)
2497 struct hash
*slsp_table
;
2498 zebra_ile_t tmp_ile
;
2500 zebra_snhlfe_t
*snhlfe
;
2503 slsp_table
= zvrf
->slsp_table
;
2507 /* If entry is not present, exit. */
2508 tmp_ile
.in_label
= in_label
;
2509 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2513 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2515 if (snhlfe
->out_label
== out_label
)
2518 /* If not only NHLFE, cannot allow label change. */
2519 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2522 /* If other NHLFEs exist, label operation must match. */
2523 if (slsp
->snhlfe_list
) {
2526 cur_op
= (slsp
->snhlfe_list
->out_label
2527 == MPLS_LABEL_IMPLICIT_NULL
);
2528 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2529 if (cur_op
!= new_op
)
2534 /* Label values are good. */
2537 #endif /* HAVE_CUMULUS */
2540 * Add static LSP entry. This may be the first entry for this incoming label
2541 * or an additional nexthop; an existing entry may also have outgoing label
2543 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2546 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2547 mpls_label_t out_label
,
2548 enum nexthop_types_t gtype
, union g_addr
*gate
,
2551 struct hash
*slsp_table
;
2552 zebra_ile_t tmp_ile
;
2554 zebra_snhlfe_t
*snhlfe
;
2558 slsp_table
= zvrf
->slsp_table
;
2562 /* If entry is present, exit. */
2563 tmp_ile
.in_label
= in_label
;
2564 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2567 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2569 if (snhlfe
->out_label
== out_label
)
2573 if (IS_ZEBRA_DEBUG_MPLS
) {
2574 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2576 "Upd static LSP in-label %u nexthop %s "
2577 "out-label %u (old %u)",
2578 in_label
, buf
, out_label
, snhlfe
->out_label
);
2580 snhlfe
->out_label
= out_label
;
2582 /* Add static LSP entry to this nexthop */
2583 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2587 if (IS_ZEBRA_DEBUG_MPLS
) {
2588 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2590 "Add static LSP in-label %u nexthop %s out-label %u",
2591 in_label
, buf
, out_label
);
2595 /* (Re)Install LSP in the main table. */
2596 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2604 * Delete static LSP entry. This may be the delete of one particular
2605 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2607 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2608 * LSP configuration.
2610 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2611 enum nexthop_types_t gtype
, union g_addr
*gate
,
2614 struct hash
*slsp_table
;
2615 zebra_ile_t tmp_ile
;
2617 zebra_snhlfe_t
*snhlfe
;
2620 slsp_table
= zvrf
->slsp_table
;
2624 /* If entry is not present, exit. */
2625 tmp_ile
.in_label
= in_label
;
2626 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2630 /* Is it delete of entire LSP or a specific NHLFE? */
2631 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2632 if (IS_ZEBRA_DEBUG_MPLS
)
2633 zlog_debug("Del static LSP in-label %u", in_label
);
2635 /* Uninstall entire LSP from the main table. */
2636 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2638 /* Delete all static NHLFEs */
2639 snhlfe_del_all(slsp
);
2641 /* Find specific NHLFE, exit if not found. */
2642 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2646 if (IS_ZEBRA_DEBUG_MPLS
) {
2648 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2649 zlog_debug("Del static LSP in-label %u nexthop %s",
2653 /* Uninstall LSP from the main table. */
2654 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2657 /* Delete static LSP NHLFE */
2661 /* Remove entire static LSP entry if no NHLFE - valid in either case
2663 if (!slsp
->snhlfe_list
) {
2664 slsp
= hash_release(slsp_table
, &tmp_ile
);
2666 XFREE(MTYPE_SLSP
, slsp
);
2673 * Schedule all MPLS label forwarding entries for processing.
2674 * Called upon changes that may affect one or more of them such as
2675 * interface or nexthop state changes.
2677 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2681 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2685 * Display MPLS label forwarding table for a specific LSP
2686 * (VTY command handler).
2688 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2689 mpls_label_t label
, uint8_t use_json
)
2691 struct hash
*lsp_table
;
2693 zebra_ile_t tmp_ile
;
2694 json_object
*json
= NULL
;
2697 lsp_table
= zvrf
->lsp_table
;
2701 /* If entry is not present, exit. */
2702 tmp_ile
.in_label
= label
;
2703 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2708 json
= lsp_json(lsp
);
2709 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2710 json
, JSON_C_TO_STRING_PRETTY
));
2711 json_object_free(json
);
2713 lsp_print(lsp
, (void *)vty
);
2717 * Display MPLS label forwarding table (VTY command handler).
2719 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2723 json_object
*json
= NULL
;
2724 zebra_lsp_t
*lsp
= NULL
;
2725 zebra_nhlfe_t
*nhlfe
= NULL
;
2726 struct nexthop
*nexthop
= NULL
;
2727 struct listnode
*node
= NULL
;
2728 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2731 json
= json_object_new_object();
2733 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2734 json_object_object_add(
2735 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2738 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2739 json
, JSON_C_TO_STRING_PRETTY
));
2740 json_object_free(json
);
2742 vty_out(vty
, " Inbound Outbound\n");
2743 vty_out(vty
, " Label Type Nexthop Label\n");
2744 vty_out(vty
, "-------- ------- --------------- --------\n");
2746 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2747 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2748 nhlfe
= nhlfe
->next
) {
2749 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2750 nhlfe_type2str(nhlfe
->type
));
2751 nexthop
= nhlfe
->nexthop
;
2753 switch (nexthop
->type
) {
2754 case NEXTHOP_TYPE_IFINDEX
: {
2755 struct interface
*ifp
;
2757 ifp
= if_lookup_by_index(
2758 nexthop
->ifindex
, VRF_UNKNOWN
);
2759 vty_out(vty
, "%15s", ifp
->name
);
2762 case NEXTHOP_TYPE_IPV4
:
2763 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2764 vty_out(vty
, "%15s",
2765 inet_ntoa(nexthop
->gate
.ipv4
));
2767 case NEXTHOP_TYPE_IPV6
:
2768 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2769 vty_out(vty
, "%15s",
2771 &nexthop
->gate
.ipv6
,
2778 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
2779 vty_out(vty
, " %8s\n",
2794 list_delete_and_null(&lsp_list
);
2798 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2800 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2803 zebra_snhlfe_t
*snhlfe
;
2804 struct listnode
*node
;
2805 struct list
*slsp_list
=
2806 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2808 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2809 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2810 snhlfe
= snhlfe
->next
) {
2814 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2815 switch (snhlfe
->out_label
) {
2816 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2817 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
2818 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2820 case MPLS_LABEL_IMPLICIT_NULL
:
2821 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2824 sprintf(lstr
, "%u", snhlfe
->out_label
);
2828 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2833 list_delete_and_null(&slsp_list
);
2834 return (zvrf
->slsp_table
->count
? 1 : 0);
2838 * Add/update global label block.
2840 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
2843 zvrf
->mpls_srgb
.start_label
= start_label
;
2844 zvrf
->mpls_srgb
.end_label
= end_label
;
2846 /* Evaluate registered FECs to see if any get a label or not. */
2852 * Delete global label block.
2854 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2856 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2857 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2859 /* Process registered FECs to clear their local label, if needed. */
2865 * Display MPLS global label block configuration (VTY command handler).
2867 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2869 if (zvrf
->mpls_srgb
.start_label
== 0)
2872 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2873 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2874 vty_out(vty
, "mpls label global-block %u %u\n",
2875 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2882 * Called when VRF becomes inactive, cleans up information but keeps
2884 * NOTE: Currently supported only for default VRF.
2886 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
2888 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2892 * Called upon process exiting, need to delete LSP forwarding
2893 * entries from the kernel.
2894 * NOTE: Currently supported only for default VRF.
2896 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2898 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2899 hash_clean(zvrf
->lsp_table
, NULL
);
2900 hash_free(zvrf
->lsp_table
);
2901 hash_clean(zvrf
->slsp_table
, NULL
);
2902 hash_free(zvrf
->slsp_table
);
2903 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2904 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2908 * Allocate MPLS tables for this VRF and do other initialization.
2909 * NOTE: Currently supported only for default VRF.
2911 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2916 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
2917 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
2918 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2919 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2920 zvrf
->mpls_flags
= 0;
2921 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2922 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2926 * Global MPLS initialization.
2928 void zebra_mpls_init(void)
2932 if (mpls_kernel_init() < 0) {
2933 zlog_warn("Disabling MPLS support (no kernel support)");
2937 if (!mpls_processq_init(&zebrad
))
2940 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);