2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
32 #include "workqueue.h"
39 #include "zebra/rib.h"
41 #include "zebra/interface.h"
42 #include "zebra/zserv.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/zebra_memory.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/zebra_mpls.h"
48 #include "zebra/zebra_errors.h"
50 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
51 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
52 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
53 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
55 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
59 /* Default rtm_table for all clients */
60 extern struct zebra_t zebrad
;
62 /* static function declarations */
64 static void fec_evaluate(struct zebra_vrf
*zvrf
);
65 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
67 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
68 struct route_node
*rn
, struct route_entry
*re
);
69 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
70 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
71 mpls_label_t old_label
);
72 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
73 static void fec_update_clients(zebra_fec_t
*fec
);
74 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
75 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
76 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
77 mpls_label_t label
, uint32_t flags
,
78 uint32_t label_index
);
79 static int fec_del(zebra_fec_t
*fec
);
81 static unsigned int label_hash(void *p
);
82 static int label_cmp(const void *p1
, const void *p2
);
83 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
84 struct nexthop
*nexthop
);
85 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
86 struct nexthop
*nexthop
);
87 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
89 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
90 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
);
91 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
);
92 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
93 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
94 static void lsp_processq_complete(struct work_queue
*wq
);
95 static int lsp_processq_add(zebra_lsp_t
*lsp
);
96 static void *lsp_alloc(void *p
);
98 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
99 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
100 union g_addr
*gate
, ifindex_t ifindex
);
101 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
102 enum nexthop_types_t gtype
, union g_addr
*gate
,
104 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
105 enum nexthop_types_t gtype
, union g_addr
*gate
,
106 ifindex_t ifindex
, mpls_label_t out_label
);
107 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
108 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
109 struct mpls_label_stack
*nh_label
);
110 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
111 enum lsp_types_t type
);
112 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
113 mpls_label_t in_label
);
114 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
115 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
116 static void *slsp_alloc(void *p
);
117 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
118 union g_addr
*gate
, ifindex_t ifindex
);
119 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
120 enum nexthop_types_t gtype
,
121 union g_addr
*gate
, ifindex_t ifindex
);
122 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
123 enum nexthop_types_t gtype
,
124 union g_addr
*gate
, ifindex_t ifindex
,
125 mpls_label_t out_label
);
126 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
127 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
128 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
129 static int mpls_processq_init(struct zebra_t
*zebra
);
132 /* Static functions */
135 * Handle failure in LSP install, clear flags for NHLFE.
137 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
139 zebra_nhlfe_t
*nhlfe
;
140 struct nexthop
*nexthop
;
142 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
143 nexthop
= nhlfe
->nexthop
;
147 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
148 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
153 * Install label forwarding entry based on labeled-route entry.
155 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
156 struct route_node
*rn
, struct route_entry
*re
)
158 struct hash
*lsp_table
;
161 zebra_nhlfe_t
*nhlfe
;
162 struct nexthop
*nexthop
;
163 enum lsp_types_t lsp_type
;
168 lsp_table
= zvrf
->lsp_table
;
172 lsp_type
= lsp_type_from_re_type(re
->type
);
175 /* Locate or allocate LSP entry. */
176 tmp_ile
.in_label
= label
;
177 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
181 /* For each active nexthop, create NHLFE. Note that we deliberately skip
182 * recursive nexthops right now, because intermediate hops won't
184 * the label advertised by the recursive nexthop (plus we don't have the
185 * logic yet to push multiple labels).
187 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
188 /* Skip inactive and recursive entries. */
189 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
191 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
194 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
197 /* Clear deleted flag (in case it was set) */
198 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
199 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
204 if (IS_ZEBRA_DEBUG_MPLS
) {
205 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
207 "LSP in-label %u type %d nexthop %s "
209 lsp
->ile
.in_label
, lsp_type
, buf
);
212 /* Update out label, trigger processing. */
213 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
214 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
217 /* Add LSP entry to this nexthop */
218 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
219 &nexthop
->gate
, nexthop
->ifindex
,
220 nexthop
->nh_label
->label
[0]);
224 if (IS_ZEBRA_DEBUG_MPLS
) {
225 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
227 "Add LSP in-label %u type %d nexthop %s "
229 lsp
->ile
.in_label
, lsp_type
, buf
,
230 nexthop
->nh_label
->label
[0]);
233 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
235 /* Mark NHLFE as changed. */
236 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
241 /* Queue LSP for processing if necessary. If no NHLFE got added (special
242 * case), delete the LSP entry; this case results in somewhat ugly
245 if (added
|| changed
) {
246 if (lsp_processq_add(lsp
))
248 } else if (!lsp
->nhlfe_list
249 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
250 if (IS_ZEBRA_DEBUG_MPLS
)
251 zlog_debug("Free LSP in-label %u flags 0x%x",
252 lsp
->ile
.in_label
, lsp
->flags
);
254 lsp
= hash_release(lsp_table
, &lsp
->ile
);
256 XFREE(MTYPE_LSP
, lsp
);
263 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
264 * NHLFEs are removed, the entire entry is deleted.
266 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
268 struct hash
*lsp_table
;
271 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
275 lsp_table
= zvrf
->lsp_table
;
279 /* If entry is not present, exit. */
280 tmp_ile
.in_label
= label
;
281 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
282 if (!lsp
|| !lsp
->nhlfe_list
)
285 /* Mark NHLFEs for delete or directly delete, as appropriate. */
286 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
287 nhlfe_next
= nhlfe
->next
;
289 /* Skip static NHLFEs */
290 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
293 if (IS_ZEBRA_DEBUG_MPLS
) {
294 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
296 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
297 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
300 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
301 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
302 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
308 /* Queue LSP for processing, if needed, else delete. */
309 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
310 if (lsp_processq_add(lsp
))
312 } else if (!lsp
->nhlfe_list
313 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
314 if (IS_ZEBRA_DEBUG_MPLS
)
315 zlog_debug("Del LSP in-label %u flags 0x%x",
316 lsp
->ile
.in_label
, lsp
->flags
);
318 lsp
= hash_release(lsp_table
, &lsp
->ile
);
320 XFREE(MTYPE_LSP
, lsp
);
327 * This function is invoked upon change to label block configuration; it
328 * will walk all registered FECs with label-index and appropriately update
329 * their local labels and trigger client updates.
331 static void fec_evaluate(struct zebra_vrf
*zvrf
)
333 struct route_node
*rn
;
335 uint32_t old_label
, new_label
;
339 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
340 if (zvrf
->fec_table
[af
] == NULL
)
343 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
344 rn
= route_next(rn
)) {
345 if ((fec
= rn
->info
) == NULL
)
348 /* Skip configured FECs and those without a label index.
350 if (fec
->flags
& FEC_FLAG_CONFIGURED
351 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
354 if (IS_ZEBRA_DEBUG_MPLS
)
355 prefix2str(&rn
->p
, buf
, BUFSIZ
);
357 /* Save old label, determine new label. */
358 old_label
= fec
->label
;
360 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
361 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
362 new_label
= MPLS_INVALID_LABEL
;
364 /* If label has changed, update FEC and clients. */
365 if (new_label
== old_label
)
368 if (IS_ZEBRA_DEBUG_MPLS
)
370 "Update fec %s new label %u upon label block",
373 fec
->label
= new_label
;
374 fec_update_clients(fec
);
376 /* Update label forwarding entries appropriately */
377 fec_change_update_lsp(zvrf
, fec
, old_label
);
383 * Derive (if possible) and update the local label for the FEC based on
384 * its label index. The index is "acceptable" if it falls within the
385 * globally configured label block (SRGB).
387 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
392 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
393 && zvrf
->mpls_srgb
.start_label
394 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
395 < zvrf
->mpls_srgb
.end_label
))
398 fec
->label
= MPLS_INVALID_LABEL
;
404 * There is a change for this FEC. Install or uninstall label forwarding
405 * entries, as appropriate.
407 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
408 mpls_label_t old_label
)
410 struct route_table
*table
;
411 struct route_node
*rn
;
412 struct route_entry
*re
;
415 /* Uninstall label forwarding entry, if previously installed. */
416 if (old_label
!= MPLS_INVALID_LABEL
417 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
418 lsp_uninstall(zvrf
, old_label
);
420 /* Install label forwarding entry corr. to new label, if needed. */
421 if (fec
->label
== MPLS_INVALID_LABEL
422 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
425 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
426 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
430 /* See if labeled route exists. */
431 rn
= route_node_lookup(table
, &fec
->rn
->p
);
435 RNODE_FOREACH_RE (rn
, re
) {
436 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
440 if (!re
|| !zebra_rib_labeled_unicast(re
))
443 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
450 * Inform about FEC to a registered client.
452 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
455 struct route_node
*rn
;
459 /* Get output stream. */
460 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
462 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
464 stream_putw(s
, rn
->p
.family
);
465 stream_put_prefix(s
, &rn
->p
);
466 stream_putl(s
, fec
->label
);
467 stream_putw_at(s
, 0, stream_get_endp(s
));
468 return zserv_send_message(client
, s
);
472 * Update all registered clients about this FEC. Caller should've updated
473 * FEC and ensure no duplicate updates.
475 static void fec_update_clients(zebra_fec_t
*fec
)
477 struct listnode
*node
;
478 struct zserv
*client
;
480 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
481 if (IS_ZEBRA_DEBUG_MPLS
)
482 zlog_debug("Update client %s",
483 zebra_route_string(client
->proto
));
484 fec_send(fec
, client
);
490 * Print a FEC-label binding entry.
492 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
494 struct route_node
*rn
;
495 struct listnode
*node
;
496 struct zserv
*client
;
500 prefix2str(&rn
->p
, buf
, BUFSIZ
);
501 vty_out(vty
, "%s\n", buf
);
502 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
503 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
504 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
506 if (!list_isempty(fec
->client_list
)) {
507 vty_out(vty
, " Client list:");
508 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
509 vty_out(vty
, " %s(fd %d)",
510 zebra_route_string(client
->proto
),
517 * Locate FEC-label binding that matches with passed info.
519 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
521 struct route_node
*rn
;
524 rn
= route_node_lookup(table
, p
);
528 route_unlock_node(rn
);
533 * Add a FEC. This may be upon a client registering for a binding
534 * or when a binding is configured.
536 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
537 mpls_label_t label
, uint32_t flags
,
538 uint32_t label_index
)
540 struct route_node
*rn
;
545 /* Lookup (or add) route node.*/
546 rn
= route_node_get(table
, p
);
553 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
;
707 struct zebra_ns
*zns
;
709 nexthop
= nhlfe
->nexthop
;
710 if (!nexthop
) // unexpected
713 /* Check on nexthop based on type. */
714 switch (nexthop
->type
) {
715 case NEXTHOP_TYPE_IFINDEX
:
717 * Lookup if this type is special. The
718 * NEXTHOP_TYPE_IFINDEX is a pop and
719 * forward into a different table for
720 * processing. As such this ifindex
721 * passed to us may be a VRF device
722 * which will not be in the default
723 * VRF. So let's look in all of them
725 zns
= zebra_ns_lookup(NS_DEFAULT
);
726 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
727 if (ifp
&& if_is_operative(ifp
))
728 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
730 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
732 case NEXTHOP_TYPE_IPV4
:
733 case NEXTHOP_TYPE_IPV4_IFINDEX
:
734 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
735 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
740 case NEXTHOP_TYPE_IPV6
:
741 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
742 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
744 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
747 case NEXTHOP_TYPE_IPV6_IFINDEX
:
748 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
749 ifp
= if_lookup_by_index(nexthop
->ifindex
,
751 if (ifp
&& if_is_operative(ifp
))
752 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
754 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
756 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
757 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
759 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
767 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
771 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
772 * reachability and select the best. Multipath entries are also
773 * marked. This is invoked when an LSP scheduled for processing (due
774 * to some change) is examined.
776 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
778 zebra_nhlfe_t
*nhlfe
;
780 struct nexthop
*nexthop
;
788 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
791 * First compute the best path, after checking nexthop status. We are
793 * concerned with non-deleted NHLFEs.
795 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
796 /* Clear selection flags. */
797 UNSET_FLAG(nhlfe
->flags
,
798 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
800 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
801 && nhlfe_nexthop_active(nhlfe
)) {
802 if (!best
|| (nhlfe
->distance
< best
->distance
))
807 lsp
->best_nhlfe
= best
;
808 if (!lsp
->best_nhlfe
)
811 /* Mark best NHLFE as selected. */
812 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
815 * If best path exists, see if there is ECMP. While doing this, note if
817 * new (uninstalled) NHLFE has been selected, an installed entry that is
818 * still selected has a change or an installed entry is to be removed.
820 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
821 int nh_chg
, nh_sel
, nh_inst
;
823 nexthop
= nhlfe
->nexthop
;
824 if (!nexthop
) // unexpected
827 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
828 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
829 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
830 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
831 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
835 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
836 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
837 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
839 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
841 if ((nh_sel
&& !nh_inst
)
842 || (nh_sel
&& nh_inst
&& nh_chg
)
843 || (nh_inst
&& !nh_sel
))
847 /* We have finished examining, clear changed flag. */
848 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
852 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
856 * Delete LSP forwarding entry from kernel, if installed. Called upon
859 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
)
863 lsp
= (zebra_lsp_t
*)backet
->data
;
864 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
865 (void)kernel_del_lsp(lsp
);
869 * Schedule LSP forwarding entry for processing. Called upon changes
870 * that may impact LSPs such as nexthop / connected route changes.
872 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
)
876 lsp
= (zebra_lsp_t
*)backet
->data
;
877 (void)lsp_processq_add(lsp
);
881 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
882 * any multipaths and update or delete from the kernel, as needed.
884 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
887 zebra_nhlfe_t
*oldbest
, *newbest
;
888 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
889 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
891 lsp
= (zebra_lsp_t
*)data
;
892 if (!lsp
) // unexpected
895 oldbest
= lsp
->best_nhlfe
;
897 /* Select best NHLFE(s) */
898 lsp_select_best_nhlfe(lsp
);
900 newbest
= lsp
->best_nhlfe
;
902 if (IS_ZEBRA_DEBUG_MPLS
) {
904 nhlfe2str(oldbest
, buf
, BUFSIZ
);
906 nhlfe2str(newbest
, buf2
, BUFSIZ
);
908 "Process LSP in-label %u oldbest %s newbest %s "
909 "flags 0x%x ecmp# %d",
910 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
911 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
914 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
915 /* Not already installed */
918 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
919 switch (kernel_add_lsp(lsp
)) {
920 case DP_REQUEST_QUEUED
:
922 EC_ZEBRA_DP_INVALID_RC
,
923 "No current DataPlane interfaces can return this, please fix");
925 case DP_REQUEST_FAILURE
:
927 case DP_REQUEST_SUCCESS
:
928 zvrf
->lsp_installs
++;
933 /* Installed, may need an update and/or delete. */
936 switch (kernel_del_lsp(lsp
)) {
937 case DP_REQUEST_QUEUED
:
939 EC_ZEBRA_DP_INVALID_RC
,
940 "No current DataPlane interfaces can return this, please fix");
942 case DP_REQUEST_FAILURE
:
944 case DP_REQUEST_SUCCESS
:
945 zvrf
->lsp_removals
++;
948 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
949 zebra_nhlfe_t
*nhlfe
;
950 struct nexthop
*nexthop
;
952 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
953 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
956 * Any NHLFE that was installed but is not
957 * selected now needs to have its flags updated.
959 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
960 nhlfe
= nhlfe
->next
) {
961 nexthop
= nhlfe
->nexthop
;
965 if (CHECK_FLAG(nhlfe
->flags
,
966 NHLFE_FLAG_INSTALLED
)
967 && !CHECK_FLAG(nhlfe
->flags
,
968 NHLFE_FLAG_SELECTED
)) {
969 UNSET_FLAG(nhlfe
->flags
,
970 NHLFE_FLAG_INSTALLED
);
971 UNSET_FLAG(nexthop
->flags
,
976 switch (kernel_upd_lsp(lsp
)) {
977 case DP_REQUEST_QUEUED
:
979 EC_ZEBRA_DP_INVALID_RC
,
980 "No current DataPlane interfaces can return this, please fix");
982 case DP_REQUEST_FAILURE
:
984 case DP_REQUEST_SUCCESS
:
985 zvrf
->lsp_installs
++;
996 * Callback upon processing completion of a LSP forwarding entry.
998 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1000 struct zebra_vrf
*zvrf
;
1002 struct hash
*lsp_table
;
1003 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1005 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1008 lsp_table
= zvrf
->lsp_table
;
1009 if (!lsp_table
) // unexpected
1012 lsp
= (zebra_lsp_t
*)data
;
1013 if (!lsp
) // unexpected
1016 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1018 * delete LSP entry also.
1020 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1022 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1023 nhlfe_next
= nhlfe
->next
;
1024 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1028 if (!lsp
->nhlfe_list
) {
1029 if (IS_ZEBRA_DEBUG_MPLS
)
1030 zlog_debug("Free LSP in-label %u flags 0x%x",
1031 lsp
->ile
.in_label
, lsp
->flags
);
1033 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1035 XFREE(MTYPE_LSP
, lsp
);
1040 * Callback upon finishing the processing of all scheduled
1041 * LSP forwarding entries.
1043 static void lsp_processq_complete(struct work_queue
*wq
)
1045 /* Nothing to do for now. */
1049 * Add LSP forwarding entry to queue for subsequent processing.
1051 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1053 /* If already scheduled, exit. */
1054 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1057 if (zebrad
.lsp_process_q
== NULL
) {
1058 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1059 "%s: work_queue does not exist!", __func__
);
1063 work_queue_add(zebrad
.lsp_process_q
, lsp
);
1064 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1069 * Callback to allocate LSP forwarding table entry.
1071 static void *lsp_alloc(void *p
)
1073 const zebra_ile_t
*ile
= p
;
1076 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1079 if (IS_ZEBRA_DEBUG_MPLS
)
1080 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1082 return ((void *)lsp
);
1086 * Create printable string for NHLFE entry.
1088 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1090 struct nexthop
*nexthop
;
1093 nexthop
= nhlfe
->nexthop
;
1094 switch (nexthop
->type
) {
1095 case NEXTHOP_TYPE_IPV4
:
1096 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1097 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1099 case NEXTHOP_TYPE_IPV6
:
1100 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1102 case NEXTHOP_TYPE_IFINDEX
:
1103 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1112 * Check if NHLFE matches with search info passed.
1114 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1115 union g_addr
*gate
, ifindex_t ifindex
)
1117 struct nexthop
*nhop
;
1120 nhop
= nhlfe
->nexthop
;
1124 if (nhop
->type
!= gtype
)
1127 switch (nhop
->type
) {
1128 case NEXTHOP_TYPE_IPV4
:
1129 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1130 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1131 sizeof(struct in_addr
));
1132 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1133 cmp
= !(nhop
->ifindex
== ifindex
);
1135 case NEXTHOP_TYPE_IPV6
:
1136 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1137 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1138 sizeof(struct in6_addr
));
1139 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1140 cmp
= !(nhop
->ifindex
== ifindex
);
1142 case NEXTHOP_TYPE_IFINDEX
:
1143 cmp
= !(nhop
->ifindex
== ifindex
);
1154 * Locate NHLFE that matches with passed info.
1156 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1157 enum nexthop_types_t gtype
, union g_addr
*gate
,
1160 zebra_nhlfe_t
*nhlfe
;
1165 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1166 if (nhlfe
->type
!= lsp_type
)
1168 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1176 * Add NHLFE. Base entry must have been created and duplicate
1179 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1180 enum nexthop_types_t gtype
, union g_addr
*gate
,
1181 ifindex_t ifindex
, mpls_label_t out_label
)
1183 zebra_nhlfe_t
*nhlfe
;
1184 struct nexthop
*nexthop
;
1189 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1192 nhlfe
->type
= lsp_type
;
1193 nhlfe
->distance
= lsp_distance(lsp_type
);
1195 nexthop
= nexthop_new();
1197 XFREE(MTYPE_NHLFE
, nhlfe
);
1200 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1202 nexthop
->vrf_id
= VRF_DEFAULT
;
1203 nexthop
->type
= gtype
;
1204 switch (nexthop
->type
) {
1205 case NEXTHOP_TYPE_IPV4
:
1206 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1207 nexthop
->gate
.ipv4
= gate
->ipv4
;
1209 nexthop
->ifindex
= ifindex
;
1211 case NEXTHOP_TYPE_IPV6
:
1212 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1213 nexthop
->gate
.ipv6
= gate
->ipv6
;
1215 nexthop
->ifindex
= ifindex
;
1217 case NEXTHOP_TYPE_IFINDEX
:
1218 nexthop
->ifindex
= ifindex
;
1221 nexthop_free(nexthop
);
1222 XFREE(MTYPE_NHLFE
, nhlfe
);
1227 nhlfe
->nexthop
= nexthop
;
1228 if (lsp
->nhlfe_list
)
1229 lsp
->nhlfe_list
->prev
= nhlfe
;
1230 nhlfe
->next
= lsp
->nhlfe_list
;
1231 lsp
->nhlfe_list
= nhlfe
;
1237 * Delete NHLFE. Entry must be present on list.
1239 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1252 nexthop_free(nhlfe
->nexthop
);
1254 /* Unlink from LSP */
1256 nhlfe
->next
->prev
= nhlfe
->prev
;
1258 nhlfe
->prev
->next
= nhlfe
->next
;
1260 lsp
->nhlfe_list
= nhlfe
->next
;
1262 if (nhlfe
== lsp
->best_nhlfe
)
1263 lsp
->best_nhlfe
= NULL
;
1265 XFREE(MTYPE_NHLFE
, nhlfe
);
1271 * Update label for NHLFE entry.
1273 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1274 struct mpls_label_stack
*nh_label
)
1276 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1279 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1280 enum lsp_types_t type
)
1282 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1283 int schedule_lsp
= 0;
1286 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1287 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1288 nhlfe_next
= nhlfe
->next
;
1290 /* Skip non-static NHLFEs */
1291 if (nhlfe
->type
!= type
)
1294 if (IS_ZEBRA_DEBUG_MPLS
) {
1295 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1297 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1298 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1301 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1302 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1303 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1310 /* Queue LSP for processing, if needed, else delete. */
1312 if (lsp_processq_add(lsp
))
1314 } else if (!lsp
->nhlfe_list
1315 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1316 if (IS_ZEBRA_DEBUG_MPLS
)
1317 zlog_debug("Free LSP in-label %u flags 0x%x",
1318 lsp
->ile
.in_label
, lsp
->flags
);
1320 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1322 XFREE(MTYPE_LSP
, lsp
);
1329 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1330 * If no other NHLFEs exist, the entry would be deleted.
1332 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1333 mpls_label_t in_label
)
1335 struct hash
*lsp_table
;
1336 zebra_ile_t tmp_ile
;
1340 lsp_table
= zvrf
->lsp_table
;
1344 /* If entry is not present, exit. */
1345 tmp_ile
.in_label
= in_label
;
1346 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1347 if (!lsp
|| !lsp
->nhlfe_list
)
1350 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1353 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1356 json_object
*json_nhlfe
= NULL
;
1357 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1359 json_nhlfe
= json_object_new_object();
1360 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1361 json_object_int_add(json_nhlfe
, "outLabel",
1362 nexthop
->nh_label
->label
[0]);
1363 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1365 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1366 json_object_boolean_true_add(json_nhlfe
, "installed");
1368 switch (nexthop
->type
) {
1369 case NEXTHOP_TYPE_IPV4
:
1370 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1371 json_object_string_add(json_nhlfe
, "nexthop",
1372 inet_ntoa(nexthop
->gate
.ipv4
));
1374 case NEXTHOP_TYPE_IPV6
:
1375 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1376 json_object_string_add(
1377 json_nhlfe
, "nexthop",
1378 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1380 if (nexthop
->ifindex
)
1381 json_object_string_add(json_nhlfe
, "interface",
1382 ifindex2ifname(nexthop
->ifindex
,
1392 * Print the NHLFE for a LSP forwarding entry.
1394 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1396 struct nexthop
*nexthop
;
1399 nexthop
= nhlfe
->nexthop
;
1400 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1403 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1404 nhlfe_type2str(nhlfe
->type
),
1405 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1407 switch (nexthop
->type
) {
1408 case NEXTHOP_TYPE_IPV4
:
1409 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1410 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1411 if (nexthop
->ifindex
)
1412 vty_out(vty
, " dev %s",
1413 ifindex2ifname(nexthop
->ifindex
,
1416 case NEXTHOP_TYPE_IPV6
:
1417 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1418 vty_out(vty
, " via %s",
1419 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1420 if (nexthop
->ifindex
)
1421 vty_out(vty
, " dev %s",
1422 ifindex2ifname(nexthop
->ifindex
,
1429 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1435 * Print an LSP forwarding entry.
1437 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1439 zebra_nhlfe_t
*nhlfe
;
1442 vty
= (struct vty
*)ctxt
;
1444 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1445 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1448 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1449 nhlfe_print(nhlfe
, vty
);
1453 * JSON objects for an LSP forwarding entry.
1455 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1457 zebra_nhlfe_t
*nhlfe
= NULL
;
1458 json_object
*json
= json_object_new_object();
1459 json_object
*json_nhlfe_list
= json_object_new_array();
1461 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1463 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1464 json_object_boolean_true_add(json
, "installed");
1466 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1467 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1469 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1474 /* Return a sorted linked list of the hash contents */
1475 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1478 struct hash_backet
*hb
;
1479 struct list
*sorted_list
= list_new();
1481 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1483 for (i
= 0; i
< hash
->size
; i
++)
1484 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1485 listnode_add_sort(sorted_list
, hb
->data
);
1491 * Compare two LSPs based on their label values.
1493 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1495 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1498 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1505 * Callback to allocate static LSP.
1507 static void *slsp_alloc(void *p
)
1509 const zebra_ile_t
*ile
= p
;
1512 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1514 return ((void *)slsp
);
1518 * Compare two static LSPs based on their label values.
1520 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1522 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1525 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1532 * Check if static NHLFE matches with search info passed.
1534 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1535 union g_addr
*gate
, ifindex_t ifindex
)
1539 if (snhlfe
->gtype
!= gtype
)
1542 switch (snhlfe
->gtype
) {
1543 case NEXTHOP_TYPE_IPV4
:
1544 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1545 sizeof(struct in_addr
));
1547 case NEXTHOP_TYPE_IPV6
:
1548 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1549 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1550 sizeof(struct in6_addr
));
1551 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1552 cmp
= !(snhlfe
->ifindex
== ifindex
);
1562 * Locate static NHLFE that matches with passed info.
1564 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1565 enum nexthop_types_t gtype
,
1566 union g_addr
*gate
, ifindex_t ifindex
)
1568 zebra_snhlfe_t
*snhlfe
;
1573 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1574 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1583 * Add static NHLFE. Base LSP config entry must have been created
1584 * and duplicate check done.
1586 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1587 enum nexthop_types_t gtype
,
1588 union g_addr
*gate
, ifindex_t ifindex
,
1589 mpls_label_t out_label
)
1591 zebra_snhlfe_t
*snhlfe
;
1596 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1597 snhlfe
->slsp
= slsp
;
1598 snhlfe
->out_label
= out_label
;
1599 snhlfe
->gtype
= gtype
;
1601 case NEXTHOP_TYPE_IPV4
:
1602 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1604 case NEXTHOP_TYPE_IPV6
:
1605 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1606 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1608 snhlfe
->ifindex
= ifindex
;
1611 XFREE(MTYPE_SNHLFE
, snhlfe
);
1615 if (slsp
->snhlfe_list
)
1616 slsp
->snhlfe_list
->prev
= snhlfe
;
1617 snhlfe
->next
= slsp
->snhlfe_list
;
1618 slsp
->snhlfe_list
= snhlfe
;
1624 * Delete static NHLFE. Entry must be present on list.
1626 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1633 slsp
= snhlfe
->slsp
;
1638 snhlfe
->next
->prev
= snhlfe
->prev
;
1640 snhlfe
->prev
->next
= snhlfe
->next
;
1642 slsp
->snhlfe_list
= snhlfe
->next
;
1644 snhlfe
->prev
= snhlfe
->next
= NULL
;
1646 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1647 XFREE(MTYPE_SNHLFE
, snhlfe
);
1653 * Delete all static NHLFE entries for this LSP (in label).
1655 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1657 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1662 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1663 snhlfe_next
= snhlfe
->next
;
1671 * Create printable string for NHLFE configuration.
1673 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1676 switch (snhlfe
->gtype
) {
1677 case NEXTHOP_TYPE_IPV4
:
1678 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1680 case NEXTHOP_TYPE_IPV6
:
1681 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1682 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1683 if (snhlfe
->ifindex
)
1685 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1695 * Initialize work queue for processing changed LSPs.
1697 static int mpls_processq_init(struct zebra_t
*zebra
)
1699 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1700 if (!zebra
->lsp_process_q
) {
1701 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1702 "%s: could not initialise work queue!", __func__
);
1706 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1707 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1708 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1709 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1710 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1711 zebra
->lsp_process_q
->spec
.hold
= 10;
1717 /* Public functions */
1719 void kernel_lsp_pass_fail(zebra_lsp_t
*lsp
, enum dp_results res
)
1721 struct nexthop
*nexthop
;
1722 zebra_nhlfe_t
*nhlfe
;
1728 case DP_INSTALL_FAILURE
:
1729 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1730 clear_nhlfe_installed(lsp
);
1731 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1732 "LSP Install Failure: %u", lsp
->ile
.in_label
);
1734 case DP_INSTALL_SUCCESS
:
1735 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1736 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1737 nexthop
= nhlfe
->nexthop
;
1741 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1742 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1745 case DP_DELETE_SUCCESS
:
1746 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1747 clear_nhlfe_installed(lsp
);
1749 case DP_DELETE_FAILURE
:
1750 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1751 "LSP Deletion Failure: %u", lsp
->ile
.in_label
);
1757 * Install dynamic LSP entry.
1759 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1760 struct route_entry
*re
)
1762 struct route_table
*table
;
1765 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1769 /* See if there is a configured label binding for this FEC. */
1770 fec
= fec_find(table
, &rn
->p
);
1771 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1774 /* We cannot install a label forwarding entry if local label is the
1775 * implicit-null label.
1777 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
1780 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1787 * Uninstall dynamic LSP entry, if any.
1789 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1790 struct route_entry
*re
)
1792 struct route_table
*table
;
1795 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1799 /* See if there is a configured label binding for this FEC. */
1800 fec
= fec_find(table
, &rn
->p
);
1801 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1804 /* Uninstall always removes all dynamic NHLFEs. */
1805 return lsp_uninstall(zvrf
, fec
->label
);
1809 * Registration from a client for the label binding for a FEC. If a binding
1810 * already exists, it is informed to the client.
1811 * NOTE: If there is a manually configured label binding, that is used.
1812 * Otherwise, if a label index is specified, it means we have to allocate the
1813 * label from a locally configured label block (SRGB), if one exists and index
1816 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1817 uint32_t label_index
, struct zserv
*client
)
1819 struct route_table
*table
;
1823 int label_change
= 0;
1826 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1830 if (IS_ZEBRA_DEBUG_MPLS
)
1831 prefix2str(p
, buf
, BUFSIZ
);
1834 fec
= fec_find(table
, p
);
1836 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1838 prefix2str(p
, buf
, BUFSIZ
);
1840 EC_ZEBRA_FEC_ADD_FAILED
,
1841 "Failed to add FEC %s upon register, client %s",
1842 buf
, zebra_route_string(client
->proto
));
1846 old_label
= MPLS_INVALID_LABEL
;
1849 /* Client may register same FEC with different label index. */
1851 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1852 if (!new_client
&& fec
->label_index
== label_index
)
1853 /* Duplicate register */
1856 /* Save current label, update label index */
1857 old_label
= fec
->label
;
1858 fec
->label_index
= label_index
;
1862 listnode_add(fec
->client_list
, client
);
1864 if (IS_ZEBRA_DEBUG_MPLS
)
1865 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1866 label_index
, new_client
? "registered" : "updated",
1867 zebra_route_string(client
->proto
));
1869 /* If not a configured FEC, derive the local label (from label index)
1872 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1873 fec_derive_label_from_index(zvrf
, fec
);
1875 /* If no label change, exit. */
1876 if (fec
->label
== old_label
)
1882 /* If new client or label change, update client and install or uninstall
1883 * label forwarding entry as needed.
1885 /* Inform client of label, if needed. */
1886 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1887 if (IS_ZEBRA_DEBUG_MPLS
)
1888 zlog_debug("Update client label %u", fec
->label
);
1889 fec_send(fec
, client
);
1892 if (new_client
|| label_change
)
1893 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1899 * Deregistration from a client for the label binding for a FEC. The FEC
1900 * itself is deleted if no other registered clients exist and there is no
1901 * label bound to the FEC.
1903 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1904 struct zserv
*client
)
1906 struct route_table
*table
;
1910 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1914 if (IS_ZEBRA_DEBUG_MPLS
)
1915 prefix2str(p
, buf
, BUFSIZ
);
1917 fec
= fec_find(table
, p
);
1919 prefix2str(p
, buf
, BUFSIZ
);
1920 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
1921 "Failed to find FEC %s upon unregister, client %s",
1922 buf
, zebra_route_string(client
->proto
));
1926 listnode_delete(fec
->client_list
, client
);
1928 if (IS_ZEBRA_DEBUG_MPLS
)
1929 zlog_debug("FEC %s unregistered by client %s", buf
,
1930 zebra_route_string(client
->proto
));
1932 /* If not a configured entry, delete the FEC if no other clients. Before
1933 * deleting, see if any LSP needs to be uninstalled.
1935 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1936 && list_isempty(fec
->client_list
)) {
1937 mpls_label_t old_label
= fec
->label
;
1938 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1939 fec_change_update_lsp(zvrf
, fec
, old_label
);
1947 * Cleanup any FECs registered by this client.
1949 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
1951 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1952 struct route_node
*rn
;
1954 struct listnode
*node
;
1955 struct zserv
*fec_client
;
1958 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1959 if (zvrf
->fec_table
[af
] == NULL
)
1962 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1963 rn
= route_next(rn
)) {
1965 if (!fec
|| list_isempty(fec
->client_list
))
1968 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1970 if (fec_client
== client
) {
1971 listnode_delete(fec
->client_list
,
1973 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1974 && list_isempty(fec
->client_list
))
1986 * Return FEC (if any) to which this label is bound.
1987 * Note: Only works for per-prefix binding and when the label is not
1989 * TODO: Currently walks entire table, can optimize later with another
1992 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1995 struct route_node
*rn
;
1999 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2000 if (zvrf
->fec_table
[af
] == NULL
)
2003 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2004 rn
= route_next(rn
)) {
2008 if (fec
->label
== label
)
2017 * Inform if specified label is currently bound to a FEC or not.
2019 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2021 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2025 * Add static FEC to label binding. If there are clients registered for this
2026 * FEC, notify them. If there are labeled routes for this FEC, install the
2027 * label forwarding entry.
2029 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2030 mpls_label_t in_label
)
2032 struct route_table
*table
;
2035 mpls_label_t old_label
;
2038 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2042 if (IS_ZEBRA_DEBUG_MPLS
)
2043 prefix2str(p
, buf
, BUFSIZ
);
2045 /* Update existing FEC or create a new one. */
2046 fec
= fec_find(table
, p
);
2048 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2049 MPLS_INVALID_LABEL_INDEX
);
2051 prefix2str(p
, buf
, BUFSIZ
);
2052 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2053 "Failed to add FEC %s upon config", buf
);
2057 if (IS_ZEBRA_DEBUG_MPLS
)
2058 zlog_debug("Add fec %s label %u", buf
, in_label
);
2060 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2061 if (fec
->label
== in_label
)
2062 /* Duplicate config */
2065 /* Label change, update clients. */
2066 old_label
= fec
->label
;
2067 if (IS_ZEBRA_DEBUG_MPLS
)
2068 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2070 fec
->label
= in_label
;
2071 fec_update_clients(fec
);
2073 /* Update label forwarding entries appropriately */
2074 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2081 * Remove static FEC to label binding. If there are no clients registered
2082 * for this FEC, delete the FEC; else notify clients
2083 * Note: Upon delete of static binding, if label index exists for this FEC,
2084 * client may need to be updated with derived label.
2086 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2088 struct route_table
*table
;
2090 mpls_label_t old_label
;
2093 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2097 fec
= fec_find(table
, p
);
2099 prefix2str(p
, buf
, BUFSIZ
);
2100 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2101 "Failed to find FEC %s upon delete", buf
);
2105 if (IS_ZEBRA_DEBUG_MPLS
) {
2106 prefix2str(p
, buf
, BUFSIZ
);
2107 zlog_debug("Delete fec %s label index %u", buf
,
2111 old_label
= fec
->label
;
2112 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2113 fec
->label
= MPLS_INVALID_LABEL
;
2115 /* If no client exists, just delete the FEC. */
2116 if (list_isempty(fec
->client_list
)) {
2121 /* Derive the local label (from label index) or reset it. */
2122 fec_derive_label_from_index(zvrf
, fec
);
2124 /* If there is a label change, update clients. */
2125 if (fec
->label
== old_label
)
2127 fec_update_clients(fec
);
2129 /* Update label forwarding entries appropriately */
2130 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2134 * Display MPLS FEC to label binding configuration (VTY command handler).
2136 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2138 struct route_node
*rn
;
2144 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2145 if (zvrf
->fec_table
[af
] == NULL
)
2148 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2149 rn
= route_next(rn
)) {
2156 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2160 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2161 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2162 label2str(fec
->label
, lstr
, BUFSIZ
));
2170 * Display MPLS FEC to label binding (VTY command handler).
2172 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2174 struct route_node
*rn
;
2177 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2178 if (zvrf
->fec_table
[af
] == NULL
)
2181 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2182 rn
= route_next(rn
)) {
2185 fec_print(rn
->info
, vty
);
2191 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2193 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2196 struct route_table
*table
;
2197 struct route_node
*rn
;
2199 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2204 rn
= route_node_lookup(table
, p
);
2208 route_unlock_node(rn
);
2212 fec_print(rn
->info
, vty
);
2215 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2216 enum lsp_types_t type
, mpls_label_t label
)
2218 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2219 nexthop_add_labels(nexthop
, type
, 1, &label
);
2220 else if (!add
&& nexthop
->nh_label_type
== type
)
2221 nexthop_del_labels(nexthop
);
2229 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2231 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2232 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2233 union g_addr
*gate
, ifindex_t ifindex
, uint8_t distance
,
2234 mpls_label_t out_label
)
2236 struct route_table
*table
;
2237 struct route_node
*rn
;
2238 struct route_entry
*re
;
2239 struct nexthop
*nexthop
;
2243 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2248 /* Lookup existing route */
2249 rn
= route_node_get(table
, prefix
);
2250 RNODE_FOREACH_RE (rn
, re
) {
2251 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2253 if (re
->distance
== distance
)
2261 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2262 switch (nexthop
->type
) {
2263 case NEXTHOP_TYPE_IPV4
:
2264 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2265 if (gtype
!= NEXTHOP_TYPE_IPV4
2266 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2268 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2270 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2271 && nexthop
->ifindex
!= ifindex
)
2273 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2278 case NEXTHOP_TYPE_IPV6
:
2279 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2280 if (gtype
!= NEXTHOP_TYPE_IPV6
2281 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2283 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2285 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2286 && nexthop
->ifindex
!= ifindex
)
2288 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2301 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2302 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2309 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2310 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2311 * the out-label for an existing NHLFE (update case).
2313 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2314 mpls_label_t in_label
, mpls_label_t out_label
,
2315 enum nexthop_types_t gtype
, union g_addr
*gate
,
2318 struct hash
*lsp_table
;
2319 zebra_ile_t tmp_ile
;
2321 zebra_nhlfe_t
*nhlfe
;
2325 lsp_table
= zvrf
->lsp_table
;
2329 /* If entry is present, exit. */
2330 tmp_ile
.in_label
= in_label
;
2331 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2334 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2336 struct nexthop
*nh
= nhlfe
->nexthop
;
2339 assert(nh
->nh_label
);
2341 /* Clear deleted flag (in case it was set) */
2342 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2343 if (nh
->nh_label
->label
[0] == out_label
)
2347 if (IS_ZEBRA_DEBUG_MPLS
) {
2348 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2350 "LSP in-label %u type %d nexthop %s "
2351 "out-label changed to %u (old %u)",
2352 in_label
, type
, buf
, out_label
,
2353 nh
->nh_label
->label
[0]);
2356 /* Update out label, trigger processing. */
2357 nh
->nh_label
->label
[0] = out_label
;
2359 /* Add LSP entry to this nexthop */
2360 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2364 if (IS_ZEBRA_DEBUG_MPLS
) {
2365 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2367 "Add LSP in-label %u type %d nexthop %s "
2369 in_label
, type
, buf
, out_label
);
2372 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2375 /* Mark NHLFE, queue LSP for processing. */
2376 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2377 if (lsp_processq_add(lsp
))
2384 * Uninstall a particular NHLFE in the forwarding table. If this is
2385 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2387 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2388 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2389 union g_addr
*gate
, ifindex_t ifindex
)
2391 struct hash
*lsp_table
;
2392 zebra_ile_t tmp_ile
;
2394 zebra_nhlfe_t
*nhlfe
;
2398 lsp_table
= zvrf
->lsp_table
;
2402 /* If entry is not present, exit. */
2403 tmp_ile
.in_label
= in_label
;
2404 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2407 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2411 if (IS_ZEBRA_DEBUG_MPLS
) {
2412 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2413 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2414 in_label
, type
, buf
, nhlfe
->flags
);
2417 /* Mark NHLFE for delete or directly delete, as appropriate. */
2418 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2419 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2420 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2421 if (lsp_processq_add(lsp
))
2426 /* Free LSP entry if no other NHLFEs and not scheduled. */
2427 if (!lsp
->nhlfe_list
2428 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2429 if (IS_ZEBRA_DEBUG_MPLS
)
2430 zlog_debug("Free LSP in-label %u flags 0x%x",
2431 lsp
->ile
.in_label
, lsp
->flags
);
2433 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2435 XFREE(MTYPE_LSP
, lsp
);
2442 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2443 * If no other NHLFEs exist, the entry would be deleted.
2445 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2448 struct hash
*lsp_table
;
2450 lsp
= (zebra_lsp_t
*)backet
->data
;
2451 if (!lsp
->nhlfe_list
)
2458 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2462 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2464 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2466 struct route_table
*table
;
2467 struct route_node
*rn
;
2468 struct route_entry
*re
;
2469 struct nexthop
*nexthop
;
2472 /* Process routes of interested address-families. */
2473 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2477 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2479 RNODE_FOREACH_RE (rn
, re
) {
2480 for (nexthop
= re
->ng
.nexthop
; nexthop
;
2481 nexthop
= nexthop
->next
) {
2482 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2485 nexthop_del_labels(nexthop
);
2486 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2487 SET_FLAG(re
->status
,
2488 ROUTE_ENTRY_LABELS_CHANGED
);
2498 #if defined(HAVE_CUMULUS)
2500 * Check that the label values used in LSP creation are consistent. The
2501 * main criteria is that if there is ECMP, the label operation must still
2502 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2503 * to current HW restrictions.
2505 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2506 mpls_label_t in_label
,
2507 mpls_label_t out_label
,
2508 enum nexthop_types_t gtype
,
2509 union g_addr
*gate
, ifindex_t ifindex
)
2511 struct hash
*slsp_table
;
2512 zebra_ile_t tmp_ile
;
2514 zebra_snhlfe_t
*snhlfe
;
2517 slsp_table
= zvrf
->slsp_table
;
2521 /* If entry is not present, exit. */
2522 tmp_ile
.in_label
= in_label
;
2523 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2527 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2529 if (snhlfe
->out_label
== out_label
)
2532 /* If not only NHLFE, cannot allow label change. */
2533 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2536 /* If other NHLFEs exist, label operation must match. */
2537 if (slsp
->snhlfe_list
) {
2540 cur_op
= (slsp
->snhlfe_list
->out_label
2541 == MPLS_LABEL_IMPLICIT_NULL
);
2542 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
2543 if (cur_op
!= new_op
)
2548 /* Label values are good. */
2551 #endif /* HAVE_CUMULUS */
2554 * Add static LSP entry. This may be the first entry for this incoming label
2555 * or an additional nexthop; an existing entry may also have outgoing label
2557 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2560 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2561 mpls_label_t out_label
,
2562 enum nexthop_types_t gtype
, union g_addr
*gate
,
2565 struct hash
*slsp_table
;
2566 zebra_ile_t tmp_ile
;
2568 zebra_snhlfe_t
*snhlfe
;
2572 slsp_table
= zvrf
->slsp_table
;
2576 /* If entry is present, exit. */
2577 tmp_ile
.in_label
= in_label
;
2578 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2581 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2583 if (snhlfe
->out_label
== out_label
)
2587 if (IS_ZEBRA_DEBUG_MPLS
) {
2588 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2590 "Upd static LSP in-label %u nexthop %s "
2591 "out-label %u (old %u)",
2592 in_label
, buf
, out_label
, snhlfe
->out_label
);
2594 snhlfe
->out_label
= out_label
;
2596 /* Add static LSP entry to this nexthop */
2597 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2601 if (IS_ZEBRA_DEBUG_MPLS
) {
2602 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2604 "Add static LSP in-label %u nexthop %s out-label %u",
2605 in_label
, buf
, out_label
);
2609 /* (Re)Install LSP in the main table. */
2610 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2618 * Delete static LSP entry. This may be the delete of one particular
2619 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2621 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2622 * LSP configuration.
2624 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2625 enum nexthop_types_t gtype
, union g_addr
*gate
,
2628 struct hash
*slsp_table
;
2629 zebra_ile_t tmp_ile
;
2631 zebra_snhlfe_t
*snhlfe
;
2634 slsp_table
= zvrf
->slsp_table
;
2638 /* If entry is not present, exit. */
2639 tmp_ile
.in_label
= in_label
;
2640 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2644 /* Is it delete of entire LSP or a specific NHLFE? */
2645 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2646 if (IS_ZEBRA_DEBUG_MPLS
)
2647 zlog_debug("Del static LSP in-label %u", in_label
);
2649 /* Uninstall entire LSP from the main table. */
2650 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2652 /* Delete all static NHLFEs */
2653 snhlfe_del_all(slsp
);
2655 /* Find specific NHLFE, exit if not found. */
2656 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2660 if (IS_ZEBRA_DEBUG_MPLS
) {
2662 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2663 zlog_debug("Del static LSP in-label %u nexthop %s",
2667 /* Uninstall LSP from the main table. */
2668 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2671 /* Delete static LSP NHLFE */
2675 /* Remove entire static LSP entry if no NHLFE - valid in either case
2677 if (!slsp
->snhlfe_list
) {
2678 slsp
= hash_release(slsp_table
, &tmp_ile
);
2680 XFREE(MTYPE_SLSP
, slsp
);
2687 * Schedule all MPLS label forwarding entries for processing.
2688 * Called upon changes that may affect one or more of them such as
2689 * interface or nexthop state changes.
2691 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2695 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2699 * Display MPLS label forwarding table for a specific LSP
2700 * (VTY command handler).
2702 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2703 mpls_label_t label
, bool use_json
)
2705 struct hash
*lsp_table
;
2707 zebra_ile_t tmp_ile
;
2708 json_object
*json
= NULL
;
2711 lsp_table
= zvrf
->lsp_table
;
2715 /* If entry is not present, exit. */
2716 tmp_ile
.in_label
= label
;
2717 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2722 json
= lsp_json(lsp
);
2723 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2724 json
, JSON_C_TO_STRING_PRETTY
));
2725 json_object_free(json
);
2727 lsp_print(lsp
, (void *)vty
);
2731 * Display MPLS label forwarding table (VTY command handler).
2733 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2737 json_object
*json
= NULL
;
2738 zebra_lsp_t
*lsp
= NULL
;
2739 zebra_nhlfe_t
*nhlfe
= NULL
;
2740 struct nexthop
*nexthop
= NULL
;
2741 struct listnode
*node
= NULL
;
2742 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2745 json
= json_object_new_object();
2747 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2748 json_object_object_add(
2749 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2752 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2753 json
, JSON_C_TO_STRING_PRETTY
));
2754 json_object_free(json
);
2756 vty_out(vty
, " Inbound Outbound\n");
2757 vty_out(vty
, " Label Type Nexthop Label\n");
2758 vty_out(vty
, "-------- ------- --------------- --------\n");
2760 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2761 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2762 nhlfe
= nhlfe
->next
) {
2763 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2764 nhlfe_type2str(nhlfe
->type
));
2765 nexthop
= nhlfe
->nexthop
;
2767 switch (nexthop
->type
) {
2768 case NEXTHOP_TYPE_IFINDEX
: {
2769 struct zebra_ns
*zns
;
2770 struct interface
*ifp
;
2772 zns
= zebra_ns_lookup(NS_DEFAULT
);
2773 ifp
= if_lookup_by_index_per_ns(
2776 vty_out(vty
, "%15s", ifp
->name
);
2779 case NEXTHOP_TYPE_IPV4
:
2780 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2781 vty_out(vty
, "%15s",
2782 inet_ntoa(nexthop
->gate
.ipv4
));
2784 case NEXTHOP_TYPE_IPV6
:
2785 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2786 vty_out(vty
, "%15s",
2788 &nexthop
->gate
.ipv6
,
2795 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
2796 vty_out(vty
, " %8s\n",
2811 list_delete_and_null(&lsp_list
);
2815 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2817 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2820 zebra_snhlfe_t
*snhlfe
;
2821 struct listnode
*node
;
2822 struct list
*slsp_list
=
2823 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2825 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2826 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2827 snhlfe
= snhlfe
->next
) {
2831 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2832 switch (snhlfe
->out_label
) {
2833 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2834 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
2835 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2837 case MPLS_LABEL_IMPLICIT_NULL
:
2838 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2841 sprintf(lstr
, "%u", snhlfe
->out_label
);
2845 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2850 list_delete_and_null(&slsp_list
);
2851 return (zvrf
->slsp_table
->count
? 1 : 0);
2855 * Add/update global label block.
2857 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
2860 zvrf
->mpls_srgb
.start_label
= start_label
;
2861 zvrf
->mpls_srgb
.end_label
= end_label
;
2863 /* Evaluate registered FECs to see if any get a label or not. */
2869 * Delete global label block.
2871 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2873 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2874 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2876 /* Process registered FECs to clear their local label, if needed. */
2882 * Display MPLS global label block configuration (VTY command handler).
2884 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2886 if (zvrf
->mpls_srgb
.start_label
== 0)
2889 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2890 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2891 vty_out(vty
, "mpls label global-block %u %u\n",
2892 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2899 * Called when VRF becomes inactive, cleans up information but keeps
2901 * NOTE: Currently supported only for default VRF.
2903 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
2905 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2909 * Called upon process exiting, need to delete LSP forwarding
2910 * entries from the kernel.
2911 * NOTE: Currently supported only for default VRF.
2913 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2915 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2916 hash_clean(zvrf
->lsp_table
, NULL
);
2917 hash_free(zvrf
->lsp_table
);
2918 hash_clean(zvrf
->slsp_table
, NULL
);
2919 hash_free(zvrf
->slsp_table
);
2920 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2921 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2925 * Allocate MPLS tables for this VRF and do other initialization.
2926 * NOTE: Currently supported only for default VRF.
2928 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2933 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
2934 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
2935 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2936 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2937 zvrf
->mpls_flags
= 0;
2938 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2939 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2943 * Global MPLS initialization.
2945 void zebra_mpls_init(void)
2949 if (mpls_kernel_init() < 0) {
2950 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
2951 "Disabling MPLS support (no kernel support)");
2955 if (!mpls_processq_init(&zebrad
))
2958 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);