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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "sockunion.h"
33 #include "workqueue.h"
40 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/zebra_memory.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/zebra_mpls.h"
49 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
50 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
51 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
52 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
58 /* Default rtm_table for all clients */
59 extern struct zebra_t zebrad
;
61 /* static function declarations */
64 fec_evaluate (struct zebra_vrf
*zvrf
, int add
);
66 fec_derive_label_from_index (struct zebra_vrf
*vrf
, zebra_fec_t
*fec
);
68 lsp_install (struct zebra_vrf
*zvrf
, mpls_label_t label
,
69 struct route_node
*rn
, struct rib
*rib
);
71 lsp_uninstall (struct zebra_vrf
*zvrf
, mpls_label_t label
);
73 fec_change_update_lsp (struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
, mpls_label_t old_label
);
75 fec_send (zebra_fec_t
*fec
, struct zserv
*client
);
77 fec_update_clients (zebra_fec_t
*fec
);
79 fec_print (zebra_fec_t
*fec
, struct vty
*vty
);
81 fec_find (struct route_table
*table
, struct prefix
*p
);
83 fec_add (struct route_table
*table
, struct prefix
*p
, mpls_label_t label
,
84 u_int32_t flags
, u_int32_t label_index
);
86 fec_del (zebra_fec_t
*fec
);
91 label_cmp (const void *p1
, const void *p2
);
93 nhlfe_nexthop_active_ipv4 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
);
95 nhlfe_nexthop_active_ipv6 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
);
97 nhlfe_nexthop_active (zebra_nhlfe_t
*nhlfe
);
100 lsp_select_best_nhlfe (zebra_lsp_t
*lsp
);
102 lsp_uninstall_from_kernel (struct hash_backet
*backet
, void *ctxt
);
104 lsp_schedule (struct hash_backet
*backet
, void *ctxt
);
105 static wq_item_status
106 lsp_process (struct work_queue
*wq
, void *data
);
108 lsp_processq_del (struct work_queue
*wq
, void *data
);
110 lsp_processq_complete (struct work_queue
*wq
);
112 lsp_processq_add (zebra_lsp_t
*lsp
);
117 nhlfe2str (zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
119 nhlfe_nhop_match (zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
120 union g_addr
*gate
, ifindex_t ifindex
);
121 static zebra_nhlfe_t
*
122 nhlfe_find (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
123 enum nexthop_types_t gtype
, union g_addr
*gate
,
125 static zebra_nhlfe_t
*
126 nhlfe_add (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
127 enum nexthop_types_t gtype
, union g_addr
*gate
,
128 ifindex_t ifindex
, mpls_label_t out_label
);
130 nhlfe_del (zebra_nhlfe_t
*snhlfe
);
132 nhlfe_out_label_update (zebra_nhlfe_t
*nhlfe
, struct nexthop_label
*nh_label
);
134 mpls_lsp_uninstall_all (struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
135 enum lsp_types_t type
);
137 mpls_static_lsp_uninstall_all (struct zebra_vrf
*zvrf
, mpls_label_t in_label
);
139 nhlfe_print (zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
141 lsp_print (zebra_lsp_t
*lsp
, void *ctxt
);
143 slsp_alloc (void *p
);
145 snhlfe_match (zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
146 union g_addr
*gate
, ifindex_t ifindex
);
147 static zebra_snhlfe_t
*
148 snhlfe_find (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
149 union g_addr
*gate
, ifindex_t ifindex
);
150 static zebra_snhlfe_t
*
151 snhlfe_add (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
152 union g_addr
*gate
, ifindex_t ifindex
, mpls_label_t out_label
);
154 snhlfe_del (zebra_snhlfe_t
*snhlfe
);
156 snhlfe_del_all (zebra_slsp_t
*slsp
);
158 snhlfe2str (zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
160 mpls_processq_init (struct zebra_t
*zebra
);
165 /* Static functions */
168 * Install label forwarding entry based on labeled-route entry.
171 lsp_install (struct zebra_vrf
*zvrf
, mpls_label_t label
,
172 struct route_node
*rn
, struct rib
*rib
)
174 struct hash
*lsp_table
;
177 zebra_nhlfe_t
*nhlfe
;
178 struct nexthop
*nexthop
;
179 enum lsp_types_t lsp_type
;
184 lsp_table
= zvrf
->lsp_table
;
188 lsp_type
= lsp_type_from_rib_type (rib
->type
);
191 /* Locate or allocate LSP entry. */
192 tmp_ile
.in_label
= label
;
193 lsp
= hash_get (lsp_table
, &tmp_ile
, lsp_alloc
);
197 /* For each active nexthop, create NHLFE. Note that we deliberately skip
198 * recursive nexthops right now, because intermediate hops won't understand
199 * the label advertised by the recursive nexthop (plus we don't have the
200 * logic yet to push multiple labels).
202 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
204 /* Skip inactive and recursive entries. */
205 if (!CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
207 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
210 nhlfe
= nhlfe_find (lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
214 /* Clear deleted flag (in case it was set) */
215 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
216 if (nexthop_labels_match (nhlfe
->nexthop
, nexthop
))
221 if (IS_ZEBRA_DEBUG_MPLS
)
223 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
224 zlog_debug ("LSP in-label %u type %d nexthop %s "
226 lsp
->ile
.in_label
, lsp_type
, buf
);
229 /* Update out label, trigger processing. */
230 nhlfe_out_label_update (nhlfe
, nexthop
->nh_label
);
231 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
236 /* Add LSP entry to this nexthop */
237 nhlfe
= nhlfe_add (lsp
, lsp_type
, nexthop
->type
,
238 &nexthop
->gate
, nexthop
->ifindex
,
239 nexthop
->nh_label
->label
[0]);
243 if (IS_ZEBRA_DEBUG_MPLS
)
245 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
246 zlog_debug ("Add LSP in-label %u type %d nexthop %s "
248 lsp
->ile
.in_label
, lsp_type
, buf
,
249 nexthop
->nh_label
->label
[0]);
252 lsp
->addr_family
= NHLFE_FAMILY (nhlfe
);
254 /* Mark NHLFE as changed. */
255 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
260 /* Queue LSP for processing if necessary. If no NHLFE got added (special
261 * case), delete the LSP entry; this case results in somewhat ugly logging.
263 if (added
|| changed
)
265 if (lsp_processq_add (lsp
))
268 else if (!lsp
->nhlfe_list
&&
269 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
271 if (IS_ZEBRA_DEBUG_MPLS
)
272 zlog_debug ("Free LSP in-label %u flags 0x%x",
273 lsp
->ile
.in_label
, lsp
->flags
);
275 lsp
= hash_release(lsp_table
, &lsp
->ile
);
277 XFREE(MTYPE_LSP
, lsp
);
284 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
285 * NHLFEs are removed, the entire entry is deleted.
288 lsp_uninstall (struct zebra_vrf
*zvrf
, mpls_label_t label
)
290 struct hash
*lsp_table
;
293 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
297 lsp_table
= zvrf
->lsp_table
;
301 /* If entry is not present, exit. */
302 tmp_ile
.in_label
= label
;
303 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
304 if (!lsp
|| !lsp
->nhlfe_list
)
307 /* Mark NHLFEs for delete or directly delete, as appropriate. */
308 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
310 nhlfe_next
= nhlfe
->next
;
312 /* Skip static NHLFEs */
313 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
316 if (IS_ZEBRA_DEBUG_MPLS
)
318 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
319 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
320 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
323 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
))
325 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
326 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
334 /* Queue LSP for processing, if needed, else delete. */
335 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
337 if (lsp_processq_add (lsp
))
340 else if (!lsp
->nhlfe_list
&&
341 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
343 if (IS_ZEBRA_DEBUG_MPLS
)
344 zlog_debug ("Del LSP in-label %u flags 0x%x",
345 lsp
->ile
.in_label
, lsp
->flags
);
347 lsp
= hash_release(lsp_table
, &lsp
->ile
);
349 XFREE(MTYPE_LSP
, lsp
);
356 * This function is invoked upon change to label block configuration; it
357 * will walk all registered FECs with label-index and appropriately update
358 * their local labels and trigger client updates.
361 fec_evaluate (struct zebra_vrf
*zvrf
, int add
)
363 struct route_node
*rn
;
365 u_int32_t old_label
, new_label
;
369 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
371 if (zvrf
->fec_table
[af
] == NULL
)
374 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
376 if ((fec
= rn
->info
) == NULL
)
379 /* Skip configured FECs and those without a label index. */
380 if (fec
->flags
& FEC_FLAG_CONFIGURED
||
381 fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
384 if (IS_ZEBRA_DEBUG_MPLS
)
385 prefix2str(&rn
->p
, buf
, BUFSIZ
);
387 /* Save old label, determine new label. */
388 old_label
= fec
->label
;
391 new_label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
392 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
393 new_label
= MPLS_INVALID_LABEL
;
396 new_label
= MPLS_INVALID_LABEL
;
398 /* If label has changed, update FEC and clients. */
399 if (new_label
== old_label
)
402 if (IS_ZEBRA_DEBUG_MPLS
)
403 zlog_debug ("Update fec %s new label %u upon label block %s",
404 buf
, new_label
, add
? "ADD" : "DEL");
406 fec
->label
= new_label
;
407 fec_update_clients (fec
);
409 /* Update label forwarding entries appropriately */
410 fec_change_update_lsp (zvrf
, fec
, old_label
);
416 * Derive (if possible) and update the local label for the FEC based on
417 * its label index. The index is "acceptable" if it falls within the
418 * globally configured label block (SRGB).
421 fec_derive_label_from_index (struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
)
425 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
&&
426 zvrf
->mpls_srgb
.start_label
&&
427 ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
) <
428 zvrf
->mpls_srgb
.end_label
))
431 fec
->label
= MPLS_INVALID_LABEL
;
437 * There is a change for this FEC. Install or uninstall label forwarding
438 * entries, as appropriate.
441 fec_change_update_lsp (struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
, mpls_label_t old_label
)
443 struct route_table
*table
;
444 struct route_node
*rn
;
448 /* Uninstall label forwarding entry, if previously installed. */
449 if (old_label
!= MPLS_INVALID_LABEL
&&
450 old_label
!= MPLS_IMP_NULL_LABEL
)
451 lsp_uninstall (zvrf
, old_label
);
453 /* Install label forwarding entry corr. to new label, if needed. */
454 if (fec
->label
== MPLS_INVALID_LABEL
||
455 fec
->label
== MPLS_IMP_NULL_LABEL
)
458 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
459 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, zvrf_id (zvrf
));
463 /* See if labeled route exists. */
464 rn
= route_node_lookup (table
, &fec
->rn
->p
);
468 RNODE_FOREACH_RIB (rn
, rib
)
470 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
474 if (!rib
|| !zebra_rib_labeled_unicast (rib
))
477 if (lsp_install (zvrf
, fec
->label
, rn
, rib
))
484 * Inform about FEC to a registered client.
487 fec_send (zebra_fec_t
*fec
, struct zserv
*client
)
490 struct route_node
*rn
;
494 /* Get output stream. */
498 zserv_create_header (s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
500 stream_putw(s
, rn
->p
.family
);
501 stream_put_prefix (s
, &rn
->p
);
502 stream_putl(s
, fec
->label
);
503 stream_putw_at(s
, 0, stream_get_endp(s
));
504 return zebra_server_send_message(client
);
508 * Update all registered clients about this FEC. Caller should've updated
509 * FEC and ensure no duplicate updates.
512 fec_update_clients (zebra_fec_t
*fec
)
514 struct listnode
*node
;
515 struct zserv
*client
;
517 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
519 if (IS_ZEBRA_DEBUG_MPLS
)
520 zlog_debug ("Update client %s", zebra_route_string(client
->proto
));
521 fec_send(fec
, client
);
527 * Print a FEC-label binding entry.
530 fec_print (zebra_fec_t
*fec
, struct vty
*vty
)
532 struct route_node
*rn
;
533 struct listnode
*node
;
534 struct zserv
*client
;
538 prefix2str(&rn
->p
, buf
, BUFSIZ
);
539 vty_out(vty
, "%s%s", buf
, VTY_NEWLINE
);
540 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
541 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
542 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
543 vty_out(vty
, "%s", VTY_NEWLINE
);
544 if (!list_isempty(fec
->client_list
))
546 vty_out(vty
, " Client list:");
547 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
548 vty_out(vty
, " %s(fd %d)",
549 zebra_route_string(client
->proto
), client
->sock
);
550 vty_out(vty
, "%s", VTY_NEWLINE
);
555 * Locate FEC-label binding that matches with passed info.
558 fec_find (struct route_table
*table
, struct prefix
*p
)
560 struct route_node
*rn
;
563 rn
= route_node_lookup(table
, p
);
567 route_unlock_node(rn
);
572 * Add a FEC. This may be upon a client registering for a binding
573 * or when a binding is configured.
576 fec_add (struct route_table
*table
, struct prefix
*p
,
577 mpls_label_t label
, u_int32_t flags
, u_int32_t label_index
)
579 struct route_node
*rn
;
584 /* Lookup (or add) route node.*/
585 rn
= route_node_get (table
, p
);
593 fec
= XCALLOC (MTYPE_FEC
, sizeof(zebra_fec_t
));
600 fec
->client_list
= list_new();
603 route_unlock_node (rn
); /* for the route_node_get */
605 fec
->label_index
= label_index
;
612 * Delete a FEC. This may be upon the last client deregistering for
613 * a FEC and no binding exists or when the binding is deleted and there
614 * are no registered clients.
617 fec_del (zebra_fec_t
*fec
)
619 list_free (fec
->client_list
);
620 fec
->rn
->info
= NULL
;
621 route_unlock_node (fec
->rn
);
622 XFREE (MTYPE_FEC
, fec
);
627 * Hash function for label.
632 const zebra_ile_t
*ile
= p
;
634 return (jhash_1word(ile
->in_label
, 0));
638 * Compare 2 LSP hash entries based on in-label.
641 label_cmp (const void *p1
, const void *p2
)
643 const zebra_ile_t
*ile1
= p1
;
644 const zebra_ile_t
*ile2
= p2
;
646 return (ile1
->in_label
== ile2
->in_label
);
650 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
652 * NOTE: Looking only for connected routes right now.
655 nhlfe_nexthop_active_ipv4 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
)
657 struct route_table
*table
;
658 struct prefix_ipv4 p
;
659 struct route_node
*rn
;
661 struct nexthop
*match_nh
;
663 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
);
667 /* Lookup nexthop in IPv4 routing table. */
668 memset (&p
, 0, sizeof (struct prefix_ipv4
));
670 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
671 p
.prefix
= nexthop
->gate
.ipv4
;
673 rn
= route_node_match (table
, (struct prefix
*) &p
);
677 route_unlock_node (rn
);
679 /* Locate a valid connected route. */
680 RNODE_FOREACH_RIB (rn
, match
)
682 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
) ||
683 !CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
686 for (match_nh
= match
->nexthop
; match_nh
; match_nh
= match_nh
->next
)
688 if (match
->type
== ZEBRA_ROUTE_CONNECT
||
689 nexthop
->ifindex
== match_nh
->ifindex
)
691 nexthop
->ifindex
= match_nh
->ifindex
;
702 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
704 * NOTE: Looking only for connected routes right now.
707 nhlfe_nexthop_active_ipv6 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
)
709 struct route_table
*table
;
710 struct prefix_ipv6 p
;
711 struct route_node
*rn
;
714 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, VRF_DEFAULT
);
718 /* Lookup nexthop in IPv6 routing table. */
719 memset (&p
, 0, sizeof (struct prefix_ipv6
));
721 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
722 p
.prefix
= nexthop
->gate
.ipv6
;
724 rn
= route_node_match (table
, (struct prefix
*) &p
);
728 route_unlock_node (rn
);
730 /* Locate a valid connected route. */
731 RNODE_FOREACH_RIB (rn
, match
)
733 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) &&
734 !CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
) &&
735 CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
739 if (!match
|| !match
->nexthop
)
742 nexthop
->ifindex
= match
->nexthop
->ifindex
;
748 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
750 * NOTE: Each NHLFE points to only 1 nexthop.
753 nhlfe_nexthop_active (zebra_nhlfe_t
*nhlfe
)
755 struct nexthop
*nexthop
;
756 struct interface
*ifp
;
758 nexthop
= nhlfe
->nexthop
;
759 if (!nexthop
) // unexpected
762 /* Check on nexthop based on type. */
763 switch (nexthop
->type
)
765 case NEXTHOP_TYPE_IPV4
:
766 case NEXTHOP_TYPE_IPV4_IFINDEX
:
767 if (nhlfe_nexthop_active_ipv4 (nhlfe
, nexthop
))
768 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
770 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
773 case NEXTHOP_TYPE_IPV6
:
774 if (nhlfe_nexthop_active_ipv6 (nhlfe
, nexthop
))
775 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
777 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
780 case NEXTHOP_TYPE_IPV6_IFINDEX
:
781 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
783 ifp
= if_lookup_by_index (nexthop
->ifindex
, VRF_DEFAULT
);
784 if (ifp
&& if_is_operative(ifp
))
785 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
787 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
791 if (nhlfe_nexthop_active_ipv6 (nhlfe
, nexthop
))
792 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
794 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
802 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
806 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
807 * reachability and select the best. Multipath entries are also
808 * marked. This is invoked when an LSP scheduled for processing (due
809 * to some change) is examined.
812 lsp_select_best_nhlfe (zebra_lsp_t
*lsp
)
814 zebra_nhlfe_t
*nhlfe
;
816 struct nexthop
*nexthop
;
824 UNSET_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
);
827 * First compute the best path, after checking nexthop status. We are only
828 * concerned with non-deleted NHLFEs.
830 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
832 /* Clear selection flags. */
833 UNSET_FLAG (nhlfe
->flags
,
834 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
836 if (!CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
) &&
837 nhlfe_nexthop_active (nhlfe
))
839 if (!best
|| (nhlfe
->distance
< best
->distance
))
844 lsp
->best_nhlfe
= best
;
845 if (!lsp
->best_nhlfe
)
848 /* Mark best NHLFE as selected. */
849 SET_FLAG (lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
852 * If best path exists, see if there is ECMP. While doing this, note if a
853 * new (uninstalled) NHLFE has been selected, an installed entry that is
854 * still selected has a change or an installed entry is to be removed.
856 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
858 int nh_chg
, nh_sel
, nh_inst
;
860 nexthop
= nhlfe
->nexthop
;
861 if (!nexthop
) // unexpected
864 if (!CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
) &&
865 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) &&
866 (nhlfe
->distance
== lsp
->best_nhlfe
->distance
))
868 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
869 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
873 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
) &&
876 nh_chg
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
877 nh_sel
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
878 nh_inst
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
880 if ((nh_sel
&& !nh_inst
) ||
881 (nh_sel
&& nh_inst
&& nh_chg
) ||
882 (nh_inst
&& !nh_sel
))
886 /* We have finished examining, clear changed flag. */
887 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
891 SET_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
);
895 * Delete LSP forwarding entry from kernel, if installed. Called upon
899 lsp_uninstall_from_kernel (struct hash_backet
*backet
, void *ctxt
)
903 lsp
= (zebra_lsp_t
*) backet
->data
;
904 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
905 kernel_del_lsp (lsp
);
909 * Schedule LSP forwarding entry for processing. Called upon changes
910 * that may impact LSPs such as nexthop / connected route changes.
913 lsp_schedule (struct hash_backet
*backet
, void *ctxt
)
917 lsp
= (zebra_lsp_t
*) backet
->data
;
918 lsp_processq_add (lsp
);
922 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
923 * any multipaths and update or delete from the kernel, as needed.
925 static wq_item_status
926 lsp_process (struct work_queue
*wq
, void *data
)
929 zebra_nhlfe_t
*oldbest
, *newbest
;
930 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
932 lsp
= (zebra_lsp_t
*)data
;
933 if (!lsp
) // unexpected
936 oldbest
= lsp
->best_nhlfe
;
938 /* Select best NHLFE(s) */
939 lsp_select_best_nhlfe (lsp
);
941 newbest
= lsp
->best_nhlfe
;
943 if (IS_ZEBRA_DEBUG_MPLS
)
946 nhlfe2str (oldbest
, buf
, BUFSIZ
);
948 nhlfe2str (newbest
, buf2
, BUFSIZ
);
949 zlog_debug ("Process LSP in-label %u oldbest %s newbest %s "
950 "flags 0x%x ecmp# %d",
951 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
952 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
955 if (!CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
))
957 /* Not already installed */
959 kernel_add_lsp (lsp
);
963 /* Installed, may need an update and/or delete. */
965 kernel_del_lsp (lsp
);
966 else if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
))
967 kernel_upd_lsp (lsp
);
975 * Callback upon processing completion of a LSP forwarding entry.
978 lsp_processq_del (struct work_queue
*wq
, void *data
)
980 struct zebra_vrf
*zvrf
;
982 struct hash
*lsp_table
;
983 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
985 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
988 lsp_table
= zvrf
->lsp_table
;
989 if (!lsp_table
) // unexpected
992 lsp
= (zebra_lsp_t
*)data
;
993 if (!lsp
) // unexpected
996 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs exist,
997 * delete LSP entry also.
999 UNSET_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
);
1001 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
1003 nhlfe_next
= nhlfe
->next
;
1004 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1008 if (!lsp
->nhlfe_list
)
1010 if (IS_ZEBRA_DEBUG_MPLS
)
1011 zlog_debug ("Free LSP in-label %u flags 0x%x",
1012 lsp
->ile
.in_label
, lsp
->flags
);
1014 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1016 XFREE(MTYPE_LSP
, lsp
);
1021 * Callback upon finishing the processing of all scheduled
1022 * LSP forwarding entries.
1025 lsp_processq_complete (struct work_queue
*wq
)
1027 /* Nothing to do for now. */
1031 * Add LSP forwarding entry to queue for subsequent processing.
1034 lsp_processq_add (zebra_lsp_t
*lsp
)
1036 /* If already scheduled, exit. */
1037 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
1040 work_queue_add (zebrad
.lsp_process_q
, lsp
);
1041 SET_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
);
1046 * Callback to allocate LSP forwarding table entry.
1051 const zebra_ile_t
*ile
= p
;
1054 lsp
= XCALLOC (MTYPE_LSP
, sizeof(zebra_lsp_t
));
1057 if (IS_ZEBRA_DEBUG_MPLS
)
1058 zlog_debug ("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1060 return ((void *)lsp
);
1064 * Create printable string for NHLFE entry.
1067 nhlfe2str (zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1069 struct nexthop
*nexthop
;
1072 nexthop
= nhlfe
->nexthop
;
1073 switch (nexthop
->type
)
1075 case NEXTHOP_TYPE_IPV4
:
1076 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1077 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1079 case NEXTHOP_TYPE_IPV6
:
1080 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1090 * Check if NHLFE matches with search info passed.
1093 nhlfe_nhop_match (zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1094 union g_addr
*gate
, ifindex_t ifindex
)
1096 struct nexthop
*nhop
;
1099 nhop
= nhlfe
->nexthop
;
1103 if (nhop
->type
!= gtype
)
1108 case NEXTHOP_TYPE_IPV4
:
1109 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1110 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1111 sizeof(struct in_addr
));
1112 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1113 cmp
= !(nhop
->ifindex
== ifindex
);
1115 case NEXTHOP_TYPE_IPV6
:
1116 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1117 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1118 sizeof(struct in6_addr
));
1119 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1120 cmp
= !(nhop
->ifindex
== ifindex
);
1131 * Locate NHLFE that matches with passed info.
1133 static zebra_nhlfe_t
*
1134 nhlfe_find (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1135 enum nexthop_types_t gtype
, union g_addr
*gate
,
1138 zebra_nhlfe_t
*nhlfe
;
1143 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1145 if (nhlfe
->type
!= lsp_type
)
1147 if (!nhlfe_nhop_match (nhlfe
, gtype
, gate
, ifindex
))
1155 * Add NHLFE. Base entry must have been created and duplicate
1158 static zebra_nhlfe_t
*
1159 nhlfe_add (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1160 enum nexthop_types_t gtype
, union g_addr
*gate
,
1161 ifindex_t ifindex
, mpls_label_t out_label
)
1163 zebra_nhlfe_t
*nhlfe
;
1164 struct nexthop
*nexthop
;
1169 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1174 nhlfe
->type
= lsp_type
;
1175 nhlfe
->distance
= lsp_distance (lsp_type
);
1177 nexthop
= nexthop_new();
1180 XFREE (MTYPE_NHLFE
, nhlfe
);
1183 nexthop_add_labels (nexthop
, lsp_type
, 1, &out_label
);
1185 nexthop
->type
= gtype
;
1186 switch (nexthop
->type
)
1188 case NEXTHOP_TYPE_IPV4
:
1189 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1190 nexthop
->gate
.ipv4
= gate
->ipv4
;
1192 nexthop
->ifindex
= ifindex
;
1194 case NEXTHOP_TYPE_IPV6
:
1195 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1196 nexthop
->gate
.ipv6
= gate
->ipv6
;
1198 nexthop
->ifindex
= ifindex
;
1201 nexthop_free(nexthop
);
1202 XFREE (MTYPE_NHLFE
, nhlfe
);
1207 nhlfe
->nexthop
= nexthop
;
1208 if (lsp
->nhlfe_list
)
1209 lsp
->nhlfe_list
->prev
= nhlfe
;
1210 nhlfe
->next
= lsp
->nhlfe_list
;
1211 lsp
->nhlfe_list
= nhlfe
;
1217 * Delete NHLFE. Entry must be present on list.
1220 nhlfe_del (zebra_nhlfe_t
*nhlfe
)
1233 nexthop_free(nhlfe
->nexthop
);
1235 /* Unlink from LSP */
1237 nhlfe
->next
->prev
= nhlfe
->prev
;
1239 nhlfe
->prev
->next
= nhlfe
->next
;
1241 lsp
->nhlfe_list
= nhlfe
->next
;
1243 if (nhlfe
== lsp
->best_nhlfe
)
1244 lsp
->best_nhlfe
= NULL
;
1246 XFREE (MTYPE_NHLFE
, nhlfe
);
1252 * Update label for NHLFE entry.
1255 nhlfe_out_label_update (zebra_nhlfe_t
*nhlfe
, struct nexthop_label
*nh_label
)
1257 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1261 mpls_lsp_uninstall_all (struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1262 enum lsp_types_t type
)
1264 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1265 int schedule_lsp
= 0;
1268 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1269 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
1271 nhlfe_next
= nhlfe
->next
;
1273 /* Skip non-static NHLFEs */
1274 if (nhlfe
->type
!= type
)
1277 if (IS_ZEBRA_DEBUG_MPLS
)
1279 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
1280 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
1281 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1284 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1286 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1287 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1296 /* Queue LSP for processing, if needed, else delete. */
1299 if (lsp_processq_add (lsp
))
1302 else if (!lsp
->nhlfe_list
&&
1303 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
1305 if (IS_ZEBRA_DEBUG_MPLS
)
1306 zlog_debug ("Free LSP in-label %u flags 0x%x",
1307 lsp
->ile
.in_label
, lsp
->flags
);
1309 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1311 XFREE(MTYPE_LSP
, lsp
);
1318 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1319 * If no other NHLFEs exist, the entry would be deleted.
1322 mpls_static_lsp_uninstall_all (struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
1324 struct hash
*lsp_table
;
1325 zebra_ile_t tmp_ile
;
1329 lsp_table
= zvrf
->lsp_table
;
1333 /* If entry is not present, exit. */
1334 tmp_ile
.in_label
= in_label
;
1335 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
1336 if (!lsp
|| !lsp
->nhlfe_list
)
1339 return mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1342 static json_object
*
1343 nhlfe_json (zebra_nhlfe_t
*nhlfe
)
1346 json_object
*json_nhlfe
= NULL
;
1347 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1349 json_nhlfe
= json_object_new_object();
1350 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1351 json_object_int_add(json_nhlfe
, "outLabel", nexthop
->nh_label
->label
[0]);
1352 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1354 if (CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1355 json_object_boolean_true_add(json_nhlfe
, "installed");
1357 switch (nexthop
->type
)
1359 case NEXTHOP_TYPE_IPV4
:
1360 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1361 json_object_string_add(json_nhlfe
, "nexthop",
1362 inet_ntoa (nexthop
->gate
.ipv4
));
1364 case NEXTHOP_TYPE_IPV6
:
1365 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1366 json_object_string_add(json_nhlfe
, "nexthop",
1367 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1369 if (nexthop
->ifindex
)
1370 json_object_string_add(json_nhlfe
, "interface", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1379 * Print the NHLFE for a LSP forwarding entry.
1382 nhlfe_print (zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1384 struct nexthop
*nexthop
;
1387 nexthop
= nhlfe
->nexthop
;
1388 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1391 vty_out(vty
, " type: %s remote label: %s distance: %d%s",
1392 nhlfe_type2str(nhlfe
->type
),
1393 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1394 nhlfe
->distance
, VTY_NEWLINE
);
1395 switch (nexthop
->type
)
1397 case NEXTHOP_TYPE_IPV4
:
1398 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1399 vty_out (vty
, " via %s", inet_ntoa (nexthop
->gate
.ipv4
));
1400 if (nexthop
->ifindex
)
1401 vty_out (vty
, " dev %s", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1403 case NEXTHOP_TYPE_IPV6
:
1404 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1405 vty_out (vty
, " via %s",
1406 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1407 if (nexthop
->ifindex
)
1408 vty_out (vty
, " dev %s", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1413 vty_out(vty
, "%s", CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ?
1414 " (installed)" : "");
1415 vty_out(vty
, "%s", VTY_NEWLINE
);
1419 * Print an LSP forwarding entry.
1422 lsp_print (zebra_lsp_t
*lsp
, void *ctxt
)
1424 zebra_nhlfe_t
*nhlfe
;
1427 vty
= (struct vty
*) ctxt
;
1429 vty_out(vty
, "Local label: %u%s%s",
1431 CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)" : "",
1434 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1435 nhlfe_print (nhlfe
, vty
);
1439 * JSON objects for an LSP forwarding entry.
1441 static json_object
*
1442 lsp_json (zebra_lsp_t
*lsp
)
1444 zebra_nhlfe_t
*nhlfe
= NULL
;
1445 json_object
*json
= json_object_new_object();
1446 json_object
*json_nhlfe_list
= json_object_new_array();
1448 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1450 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
))
1451 json_object_boolean_true_add(json
, "installed");
1453 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1454 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1456 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1461 /* Return a sorted linked list of the hash contents */
1462 static struct list
*
1463 hash_get_sorted_list (struct hash
*hash
, void *cmp
)
1466 struct hash_backet
*hb
;
1467 struct list
*sorted_list
= list_new();
1469 sorted_list
->cmp
= (int (*)(void *, void *)) cmp
;
1471 for (i
= 0; i
< hash
->size
; i
++)
1472 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1473 listnode_add_sort(sorted_list
, hb
->data
);
1479 * Compare two LSPs based on their label values.
1482 lsp_cmp (zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1484 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1487 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1494 * Callback to allocate static LSP.
1497 slsp_alloc (void *p
)
1499 const zebra_ile_t
*ile
= p
;
1502 slsp
= XCALLOC (MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1504 return ((void *)slsp
);
1508 * Compare two static LSPs based on their label values.
1511 slsp_cmp (zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1513 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1516 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1523 * Check if static NHLFE matches with search info passed.
1526 snhlfe_match (zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1527 union g_addr
*gate
, ifindex_t ifindex
)
1531 if (snhlfe
->gtype
!= gtype
)
1534 switch (snhlfe
->gtype
)
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
*
1558 snhlfe_find (zebra_slsp_t
*slsp
, 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
)
1568 if (!snhlfe_match (snhlfe
, gtype
, gate
, ifindex
))
1577 * Add static NHLFE. Base LSP config entry must have been created
1578 * and duplicate check done.
1580 static zebra_snhlfe_t
*
1581 snhlfe_add (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
1582 union g_addr
*gate
, ifindex_t ifindex
,
1583 mpls_label_t out_label
)
1585 zebra_snhlfe_t
*snhlfe
;
1590 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1591 snhlfe
->slsp
= slsp
;
1592 snhlfe
->out_label
= out_label
;
1593 snhlfe
->gtype
= gtype
;
1596 case NEXTHOP_TYPE_IPV4
:
1597 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1599 case NEXTHOP_TYPE_IPV6
:
1600 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1601 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1603 snhlfe
->ifindex
= ifindex
;
1606 XFREE (MTYPE_SNHLFE
, snhlfe
);
1610 if (slsp
->snhlfe_list
)
1611 slsp
->snhlfe_list
->prev
= snhlfe
;
1612 snhlfe
->next
= slsp
->snhlfe_list
;
1613 slsp
->snhlfe_list
= snhlfe
;
1619 * Delete static NHLFE. Entry must be present on list.
1622 snhlfe_del (zebra_snhlfe_t
*snhlfe
)
1629 slsp
= snhlfe
->slsp
;
1634 snhlfe
->next
->prev
= snhlfe
->prev
;
1636 snhlfe
->prev
->next
= snhlfe
->next
;
1638 slsp
->snhlfe_list
= snhlfe
->next
;
1640 snhlfe
->prev
= snhlfe
->next
= NULL
;
1642 XFREE (MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1643 XFREE (MTYPE_SNHLFE
, snhlfe
);
1649 * Delete all static NHLFE entries for this LSP (in label).
1652 snhlfe_del_all (zebra_slsp_t
*slsp
)
1654 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1659 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
)
1661 snhlfe_next
= snhlfe
->next
;
1662 snhlfe_del (snhlfe
);
1669 * Create printable string for NHLFE configuration.
1672 snhlfe2str (zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1675 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
)
1684 strcat (buf
, ifindex2ifname (snhlfe
->ifindex
, VRF_DEFAULT
));
1694 * Initialize work queue for processing changed LSPs.
1697 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
)
1702 zlog_err ("%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;
1716 /* Public functions */
1719 * String to label conversion, labels separated by '/'.
1722 mpls_str2label (const char *label_str
, u_int8_t
*num_labels
,
1723 mpls_label_t
*labels
)
1729 for (i
= 0; i
< MPLS_MAX_LABELS
; i
++)
1733 label
= strtoul(label_str
, &endp
, 0);
1735 /* validity checks */
1736 if (endp
== label_str
)
1739 if (!IS_MPLS_UNRESERVED_LABEL(label
))
1745 *num_labels
= i
+ 1;
1749 /* Check separator. */
1753 label_str
= endp
+ 1;
1756 /* Too many labels. */
1761 * Label to string conversion, labels in string separated by '/'.
1764 mpls_label2str (u_int8_t num_labels
, mpls_label_t
*labels
,
1765 char *buf
, int len
, int pretty
)
1767 char *buf_ptr
= buf
;
1771 if (num_labels
== 1) {
1772 label2str(labels
[0], buf
, len
);
1773 } else if (num_labels
== 2) {
1774 label2str(labels
[0], buf
, len
);
1775 buf_ptr
+= strlen(buf
);
1777 snprintf (buf_ptr
, len
, "/");
1780 label2str(labels
[1], buf_ptr
, len
);
1783 if (num_labels
== 1)
1784 snprintf (buf
, len
, "%u", labels
[0]);
1785 else if (num_labels
== 2)
1786 snprintf (buf
, len
, "%u/%u", labels
[0], labels
[1]);
1792 * Install dynamic LSP entry.
1795 zebra_mpls_lsp_install (struct zebra_vrf
*zvrf
, struct route_node
*rn
, struct rib
*rib
)
1797 struct route_table
*table
;
1800 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1804 /* See if there is a configured label binding for this FEC. */
1805 fec
= fec_find (table
, &rn
->p
);
1806 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1809 /* We cannot install a label forwarding entry if local label is the
1810 * implicit-null label.
1812 if (fec
->label
== MPLS_IMP_NULL_LABEL
)
1815 if (lsp_install (zvrf
, fec
->label
, rn
, rib
))
1822 * Uninstall dynamic LSP entry, if any.
1825 zebra_mpls_lsp_uninstall (struct zebra_vrf
*zvrf
, struct route_node
*rn
, struct rib
*rib
)
1827 struct route_table
*table
;
1830 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1834 /* See if there is a configured label binding for this FEC. */
1835 fec
= fec_find (table
, &rn
->p
);
1836 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1839 /* Uninstall always removes all dynamic NHLFEs. */
1840 return lsp_uninstall (zvrf
, fec
->label
);
1844 * Registration from a client for the label binding for a FEC. If a binding
1845 * already exists, it is informed to the client.
1846 * NOTE: If there is a manually configured label binding, that is used.
1847 * Otherwise, if aa label index is specified, it means we have to allocate the
1848 * label from a locally configured label block (SRGB), if one exists and index
1852 zebra_mpls_fec_register (struct zebra_vrf
*zvrf
, struct prefix
*p
,
1853 u_int32_t label_index
, struct zserv
*client
)
1855 struct route_table
*table
;
1859 int label_change
= 0;
1860 u_int32_t old_label
;
1862 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1866 if (IS_ZEBRA_DEBUG_MPLS
)
1867 prefix2str(p
, buf
, BUFSIZ
);
1870 fec
= fec_find (table
, p
);
1873 fec
= fec_add (table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1876 prefix2str(p
, buf
, BUFSIZ
);
1877 zlog_err("Failed to add FEC %s upon register, client %s",
1878 buf
, zebra_route_string(client
->proto
));
1882 old_label
= MPLS_INVALID_LABEL
;
1887 /* Client may register same FEC with different label index. */
1888 new_client
= (listnode_lookup(fec
->client_list
, client
) == NULL
);
1889 if (!new_client
&& fec
->label_index
== label_index
)
1890 /* Duplicate register */
1893 /* Save current label, update label index */
1894 old_label
= fec
->label
;
1895 fec
->label_index
= label_index
;
1899 listnode_add (fec
->client_list
, client
);
1901 if (IS_ZEBRA_DEBUG_MPLS
)
1902 zlog_debug("FEC %s Label Index %u %s by client %s",
1903 buf
, label_index
, new_client
? "registered" : "updated",
1904 zebra_route_string(client
->proto
));
1906 /* If not a configured FEC, derive the local label (from label index)
1909 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
1911 fec_derive_label_from_index (zvrf
, fec
);
1913 /* If no label change, exit. */
1914 if (fec
->label
== old_label
)
1920 /* If new client or label change, update client and install or uninstall
1921 * label forwarding entry as needed.
1923 /* Inform client of label, if needed. */
1924 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) ||
1927 if (IS_ZEBRA_DEBUG_MPLS
)
1928 zlog_debug ("Update client label %u", fec
->label
);
1929 fec_send (fec
, client
);
1932 if (new_client
|| label_change
)
1933 return fec_change_update_lsp (zvrf
, fec
, old_label
);
1939 * Deregistration from a client for the label binding for a FEC. The FEC
1940 * itself is deleted if no other registered clients exist and there is no
1941 * label bound to the FEC.
1944 zebra_mpls_fec_unregister (struct zebra_vrf
*zvrf
, struct prefix
*p
,
1945 struct zserv
*client
)
1947 struct route_table
*table
;
1951 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1955 if (IS_ZEBRA_DEBUG_MPLS
)
1956 prefix2str(p
, buf
, BUFSIZ
);
1958 fec
= fec_find (table
, p
);
1961 prefix2str(p
, buf
, BUFSIZ
);
1962 zlog_err("Failed to find FEC %s upon unregister, client %s",
1963 buf
, zebra_route_string(client
->proto
));
1967 listnode_delete(fec
->client_list
, client
);
1969 if (IS_ZEBRA_DEBUG_MPLS
)
1970 zlog_debug("FEC %s unregistered by client %s",
1971 buf
, zebra_route_string(client
->proto
));
1973 /* If not a configured entry, delete the FEC if no other clients. Before
1974 * deleting, see if any LSP needs to be uninstalled.
1976 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
) &&
1977 list_isempty(fec
->client_list
))
1979 mpls_label_t old_label
= fec
->label
;
1980 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1981 fec_change_update_lsp (zvrf
, fec
, old_label
);
1989 * Cleanup any FECs registered by this client.
1992 zebra_mpls_cleanup_fecs_for_client (struct zebra_vrf
*zvrf
, struct zserv
*client
)
1994 struct route_node
*rn
;
1996 struct listnode
*node
;
1997 struct zserv
*fec_client
;
2000 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2002 if (zvrf
->fec_table
[af
] == NULL
)
2005 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2008 if (!fec
|| list_isempty(fec
->client_list
))
2011 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, fec_client
))
2013 if (fec_client
== client
)
2015 listnode_delete(fec
->client_list
, fec_client
);
2016 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
) &&
2017 list_isempty(fec
->client_list
))
2029 * Return FEC (if any) to which this label is bound.
2030 * Note: Only works for per-prefix binding and when the label is not
2032 * TODO: Currently walks entire table, can optimize later with another
2036 zebra_mpls_fec_for_label (struct zebra_vrf
*zvrf
, mpls_label_t label
)
2038 struct route_node
*rn
;
2042 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2044 if (zvrf
->fec_table
[af
] == NULL
)
2047 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2052 if (fec
->label
== label
)
2061 * Inform if specified label is currently bound to a FEC or not.
2064 zebra_mpls_label_already_bound (struct zebra_vrf
*zvrf
, mpls_label_t label
)
2066 return (zebra_mpls_fec_for_label (zvrf
, label
) ? 1 : 0);
2070 * Add static FEC to label binding. If there are clients registered for this
2071 * FEC, notify them. If there are labeled routes for this FEC, install the
2072 * label forwarding entry.
2075 zebra_mpls_static_fec_add (struct zebra_vrf
*zvrf
, struct prefix
*p
,
2076 mpls_label_t in_label
)
2078 struct route_table
*table
;
2081 mpls_label_t old_label
;
2084 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2088 if (IS_ZEBRA_DEBUG_MPLS
)
2089 prefix2str(p
, buf
, BUFSIZ
);
2091 /* Update existing FEC or create a new one. */
2092 fec
= fec_find (table
, p
);
2095 fec
= fec_add (table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2096 MPLS_INVALID_LABEL_INDEX
);
2099 prefix2str(p
, buf
, BUFSIZ
);
2100 zlog_err ("Failed to add FEC %s upon config", buf
);
2104 if (IS_ZEBRA_DEBUG_MPLS
)
2105 zlog_debug ("Add fec %s label %u", buf
, in_label
);
2109 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2110 if (fec
->label
== in_label
)
2111 /* Duplicate config */
2114 /* Label change, update clients. */
2115 old_label
= fec
->label
;
2116 if (IS_ZEBRA_DEBUG_MPLS
)
2117 zlog_debug ("Update fec %s new label %u", buf
, in_label
);
2119 fec
->label
= in_label
;
2120 fec_update_clients (fec
);
2122 /* Update label forwarding entries appropriately */
2123 ret
= fec_change_update_lsp (zvrf
, fec
, old_label
);
2130 * Remove static FEC to label binding. If there are no clients registered
2131 * for this FEC, delete the FEC; else notify clients
2132 * Note: Upon delete of static binding, if label index exists for this FEC,
2133 * client may need to be updated with derived label.
2136 zebra_mpls_static_fec_del (struct zebra_vrf
*zvrf
, struct prefix
*p
)
2138 struct route_table
*table
;
2140 mpls_label_t old_label
;
2143 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2147 fec
= fec_find (table
, p
);
2150 prefix2str(p
, buf
, BUFSIZ
);
2151 zlog_err("Failed to find FEC %s upon delete", buf
);
2155 if (IS_ZEBRA_DEBUG_MPLS
)
2157 prefix2str(p
, buf
, BUFSIZ
);
2158 zlog_debug ("Delete fec %s label index %u",
2159 buf
, fec
->label_index
);
2162 old_label
= fec
->label
;
2163 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2164 fec
->label
= MPLS_INVALID_LABEL
;
2166 /* If no client exists, just delete the FEC. */
2167 if (list_isempty(fec
->client_list
))
2173 /* Derive the local label (from label index) or reset it. */
2174 fec_derive_label_from_index (zvrf
, fec
);
2176 /* If there is a label change, update clients. */
2177 if (fec
->label
== old_label
)
2179 fec_update_clients (fec
);
2181 /* Update label forwarding entries appropriately */
2182 return fec_change_update_lsp (zvrf
, fec
, old_label
);
2186 * Display MPLS FEC to label binding configuration (VTY command handler).
2189 zebra_mpls_write_fec_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2191 struct route_node
*rn
;
2197 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2199 if (zvrf
->fec_table
[af
] == NULL
)
2202 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2210 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2214 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2215 vty_out(vty
, "mpls label bind %s %s%s", buf
,
2216 label2str(fec
->label
, lstr
, BUFSIZ
), VTY_NEWLINE
);
2224 * Display MPLS FEC to label binding (VTY command handler).
2227 zebra_mpls_print_fec_table (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2229 struct route_node
*rn
;
2232 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2234 if (zvrf
->fec_table
[af
] == NULL
)
2237 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2241 fec_print (rn
->info
, vty
);
2247 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2250 zebra_mpls_print_fec (struct vty
*vty
, struct zebra_vrf
*zvrf
, struct prefix
*p
)
2252 struct route_table
*table
;
2253 struct route_node
*rn
;
2255 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2260 rn
= route_node_lookup(table
, p
);
2264 route_unlock_node(rn
);
2268 fec_print (rn
->info
, vty
);
2272 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2275 mpls_ftn_update (int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2276 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2277 union g_addr
*gate
, ifindex_t ifindex
, u_int8_t distance
,
2278 mpls_label_t out_label
)
2280 struct route_table
*table
;
2281 struct route_node
*rn
;
2283 struct nexthop
*nexthop
;
2286 table
= zebra_vrf_table (family2afi(prefix
->family
), SAFI_UNICAST
, zvrf_id (zvrf
));
2290 /* Lookup existing route */
2291 rn
= route_node_get (table
, prefix
);
2292 RNODE_FOREACH_RIB (rn
, rib
)
2294 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2296 if (rib
->distance
== distance
)
2303 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2305 switch (nexthop
->type
)
2307 case NEXTHOP_TYPE_IPV4
:
2308 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2309 if (gtype
!= NEXTHOP_TYPE_IPV4
&& gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2311 if (! IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2313 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
&&
2314 nexthop
->ifindex
!= ifindex
)
2317 case NEXTHOP_TYPE_IPV6
:
2318 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2319 if (gtype
!= NEXTHOP_TYPE_IPV6
&& gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2321 if (! IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2323 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
&&
2324 nexthop
->ifindex
!= ifindex
)
2331 /* nexthop not found */
2335 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2336 nexthop_add_labels (nexthop
, type
, 1, &out_label
);
2337 else if (!add
&& nexthop
->nh_label_type
== type
)
2338 nexthop_del_labels (nexthop
);
2342 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
2343 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
2350 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2351 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2352 * the out-label for an existing NHLFE (update case).
2355 mpls_lsp_install (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2356 mpls_label_t in_label
, mpls_label_t out_label
,
2357 enum nexthop_types_t gtype
, union g_addr
*gate
,
2360 struct hash
*lsp_table
;
2361 zebra_ile_t tmp_ile
;
2363 zebra_nhlfe_t
*nhlfe
;
2367 lsp_table
= zvrf
->lsp_table
;
2371 /* If entry is present, exit. */
2372 tmp_ile
.in_label
= in_label
;
2373 lsp
= hash_get (lsp_table
, &tmp_ile
, lsp_alloc
);
2376 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifindex
);
2379 struct nexthop
*nh
= nhlfe
->nexthop
;
2382 assert (nh
->nh_label
);
2384 /* Clear deleted flag (in case it was set) */
2385 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2386 if (nh
->nh_label
->label
[0] == out_label
)
2390 if (IS_ZEBRA_DEBUG_MPLS
)
2392 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2393 zlog_debug ("LSP in-label %u type %d nexthop %s "
2394 "out-label changed to %u (old %u)",
2395 in_label
, type
, buf
,
2396 out_label
, nh
->nh_label
->label
[0]);
2399 /* Update out label, trigger processing. */
2400 nh
->nh_label
->label
[0] = out_label
;
2404 /* Add LSP entry to this nexthop */
2405 nhlfe
= nhlfe_add (lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2409 if (IS_ZEBRA_DEBUG_MPLS
)
2411 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2412 zlog_debug ("Add LSP in-label %u type %d nexthop %s "
2413 "out-label %u", in_label
, type
, buf
, out_label
);
2416 lsp
->addr_family
= NHLFE_FAMILY (nhlfe
);
2419 /* Mark NHLFE, queue LSP for processing. */
2420 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2421 if (lsp_processq_add (lsp
))
2428 * Uninstall a particular NHLFE in the forwarding table. If this is
2429 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2432 mpls_lsp_uninstall (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2433 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2434 union g_addr
*gate
, ifindex_t ifindex
)
2436 struct hash
*lsp_table
;
2437 zebra_ile_t tmp_ile
;
2439 zebra_nhlfe_t
*nhlfe
;
2443 lsp_table
= zvrf
->lsp_table
;
2447 /* If entry is not present, exit. */
2448 tmp_ile
.in_label
= in_label
;
2449 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
2452 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifindex
);
2456 if (IS_ZEBRA_DEBUG_MPLS
)
2458 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2459 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2460 in_label
, type
, buf
, nhlfe
->flags
);
2463 /* Mark NHLFE for delete or directly delete, as appropriate. */
2464 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
2466 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2467 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2468 if (lsp_processq_add (lsp
))
2475 /* Free LSP entry if no other NHLFEs and not scheduled. */
2476 if (!lsp
->nhlfe_list
&&
2477 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
2479 if (IS_ZEBRA_DEBUG_MPLS
)
2480 zlog_debug ("Free LSP in-label %u flags 0x%x",
2481 lsp
->ile
.in_label
, lsp
->flags
);
2483 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2485 XFREE(MTYPE_LSP
, lsp
);
2492 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2493 * If no other NHLFEs exist, the entry would be deleted.
2496 mpls_ldp_lsp_uninstall_all (struct hash_backet
*backet
, void *ctxt
)
2499 struct hash
*lsp_table
;
2501 lsp
= (zebra_lsp_t
*) backet
->data
;
2502 if (!lsp
|| !lsp
->nhlfe_list
)
2509 mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2513 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2516 mpls_ldp_ftn_uninstall_all (struct zebra_vrf
*zvrf
, int afi
)
2518 struct route_table
*table
;
2519 struct route_node
*rn
;
2521 struct nexthop
*nexthop
;
2524 /* Process routes of interested address-families. */
2525 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, zvrf_id (zvrf
));
2529 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2532 RNODE_FOREACH_RIB (rn
, rib
)
2533 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2534 if (nexthop
->nh_label_type
== ZEBRA_LSP_LDP
)
2536 nexthop_del_labels (nexthop
);
2537 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
2538 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
2547 #if defined(HAVE_CUMULUS)
2549 * Check that the label values used in LSP creation are consistent. The
2550 * main criteria is that if there is ECMP, the label operation must still
2551 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2552 * to current HW restrictions.
2555 zebra_mpls_lsp_label_consistent (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2556 mpls_label_t out_label
, enum nexthop_types_t gtype
,
2557 union g_addr
*gate
, ifindex_t ifindex
)
2559 struct hash
*slsp_table
;
2560 zebra_ile_t tmp_ile
;
2562 zebra_snhlfe_t
*snhlfe
;
2565 slsp_table
= zvrf
->slsp_table
;
2569 /* If entry is not present, exit. */
2570 tmp_ile
.in_label
= in_label
;
2571 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
2575 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2578 if (snhlfe
->out_label
== out_label
)
2581 /* If not only NHLFE, cannot allow label change. */
2582 if (snhlfe
!= slsp
->snhlfe_list
||
2588 /* If other NHLFEs exist, label operation must match. */
2589 if (slsp
->snhlfe_list
)
2593 cur_op
= (slsp
->snhlfe_list
->out_label
== MPLS_IMP_NULL_LABEL
);
2594 new_op
= (out_label
== MPLS_IMP_NULL_LABEL
);
2595 if (cur_op
!= new_op
)
2600 /* Label values are good. */
2603 #endif /* HAVE_CUMULUS */
2606 * Add static LSP entry. This may be the first entry for this incoming label
2607 * or an additional nexthop; an existing entry may also have outgoing label
2609 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2613 zebra_mpls_static_lsp_add (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2614 mpls_label_t out_label
, enum nexthop_types_t gtype
,
2615 union g_addr
*gate
, ifindex_t ifindex
)
2617 struct hash
*slsp_table
;
2618 zebra_ile_t tmp_ile
;
2620 zebra_snhlfe_t
*snhlfe
;
2624 slsp_table
= zvrf
->slsp_table
;
2628 /* If entry is present, exit. */
2629 tmp_ile
.in_label
= in_label
;
2630 slsp
= hash_get (slsp_table
, &tmp_ile
, slsp_alloc
);
2633 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2636 if (snhlfe
->out_label
== out_label
)
2640 if (IS_ZEBRA_DEBUG_MPLS
)
2642 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2643 zlog_debug ("Upd static LSP in-label %u nexthop %s "
2644 "out-label %u (old %u)",
2645 in_label
, buf
, out_label
, snhlfe
->out_label
);
2647 snhlfe
->out_label
= out_label
;
2651 /* Add static LSP entry to this nexthop */
2652 snhlfe
= snhlfe_add (slsp
, gtype
, gate
, ifindex
, out_label
);
2656 if (IS_ZEBRA_DEBUG_MPLS
)
2658 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2659 zlog_debug ("Add static LSP in-label %u nexthop %s out-label %u",
2660 in_label
, buf
, out_label
);
2664 /* (Re)Install LSP in the main table. */
2665 if (mpls_lsp_install (zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2673 * Delete static LSP entry. This may be the delete of one particular
2674 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2676 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2677 * LSP configuration.
2680 zebra_mpls_static_lsp_del (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2681 enum nexthop_types_t gtype
, union g_addr
*gate
,
2684 struct hash
*slsp_table
;
2685 zebra_ile_t tmp_ile
;
2687 zebra_snhlfe_t
*snhlfe
;
2690 slsp_table
= zvrf
->slsp_table
;
2694 /* If entry is not present, exit. */
2695 tmp_ile
.in_label
= in_label
;
2696 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
2700 /* Is it delete of entire LSP or a specific NHLFE? */
2701 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
)
2703 if (IS_ZEBRA_DEBUG_MPLS
)
2704 zlog_debug ("Del static LSP in-label %u", in_label
);
2706 /* Uninstall entire LSP from the main table. */
2707 mpls_static_lsp_uninstall_all (zvrf
, in_label
);
2709 /* Delete all static NHLFEs */
2710 snhlfe_del_all (slsp
);
2714 /* Find specific NHLFE, exit if not found. */
2715 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2719 if (IS_ZEBRA_DEBUG_MPLS
)
2722 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2723 zlog_debug ("Del static LSP in-label %u nexthop %s",
2727 /* Uninstall LSP from the main table. */
2728 mpls_lsp_uninstall (zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
, gate
,
2731 /* Delete static LSP NHLFE */
2732 snhlfe_del (snhlfe
);
2735 /* Remove entire static LSP entry if no NHLFE - valid in either case above. */
2736 if (!slsp
->snhlfe_list
)
2738 slsp
= hash_release(slsp_table
, &tmp_ile
);
2740 XFREE(MTYPE_SLSP
, slsp
);
2747 * Schedule all MPLS label forwarding entries for processing.
2748 * Called upon changes that may affect one or more of them such as
2749 * interface or nexthop state changes.
2752 zebra_mpls_lsp_schedule (struct zebra_vrf
*zvrf
)
2756 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2760 * Display MPLS label forwarding table for a specific LSP
2761 * (VTY command handler).
2764 zebra_mpls_print_lsp (struct vty
*vty
, struct zebra_vrf
*zvrf
, mpls_label_t label
,
2767 struct hash
*lsp_table
;
2769 zebra_ile_t tmp_ile
;
2770 json_object
*json
= NULL
;
2773 lsp_table
= zvrf
->lsp_table
;
2777 /* If entry is not present, exit. */
2778 tmp_ile
.in_label
= label
;
2779 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
2785 json
= lsp_json(lsp
);
2786 vty_out (vty
, "%s%s", json_object_to_json_string_ext(json
, JSON_C_TO_STRING_PRETTY
), VTY_NEWLINE
);
2787 json_object_free(json
);
2790 lsp_print (lsp
, (void *)vty
);
2794 * Display MPLS label forwarding table (VTY command handler).
2797 zebra_mpls_print_lsp_table (struct vty
*vty
, struct zebra_vrf
*zvrf
,
2801 json_object
*json
= NULL
;
2802 zebra_lsp_t
*lsp
= NULL
;
2803 zebra_nhlfe_t
*nhlfe
= NULL
;
2804 struct nexthop
*nexthop
= NULL
;
2805 struct listnode
*node
= NULL
;
2806 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2810 json
= json_object_new_object();
2812 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2813 json_object_object_add(json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2816 vty_out (vty
, "%s%s", json_object_to_json_string_ext(json
, JSON_C_TO_STRING_PRETTY
), VTY_NEWLINE
);
2817 json_object_free(json
);
2821 vty_out (vty
, " Inbound Outbound%s", VTY_NEWLINE
);
2822 vty_out (vty
, " Label Type Nexthop Label%s", VTY_NEWLINE
);
2823 vty_out (vty
, "-------- ------- --------------- --------%s", VTY_NEWLINE
);
2825 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2827 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
2829 vty_out (vty
, "%8d %7s ", lsp
->ile
.in_label
, nhlfe_type2str(nhlfe
->type
));
2830 nexthop
= nhlfe
->nexthop
;
2832 switch (nexthop
->type
)
2834 case NEXTHOP_TYPE_IPV4
:
2835 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2836 vty_out (vty
, "%15s", inet_ntoa (nexthop
->gate
.ipv4
));
2838 case NEXTHOP_TYPE_IPV6
:
2839 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2840 vty_out (vty
, "%15s", inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
2846 vty_out (vty
, " %8d%s", nexthop
->nh_label
->label
[0], VTY_NEWLINE
);
2850 vty_out (vty
, "%s", VTY_NEWLINE
);
2853 list_delete (lsp_list
);
2857 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2860 zebra_mpls_write_lsp_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2863 zebra_snhlfe_t
*snhlfe
;
2864 struct listnode
*node
;
2865 struct list
*slsp_list
= hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2867 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
))
2869 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
)
2871 char buf
[INET6_ADDRSTRLEN
];
2874 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2875 switch (snhlfe
->out_label
) {
2876 case MPLS_V4_EXP_NULL_LABEL
:
2877 case MPLS_V6_EXP_NULL_LABEL
:
2878 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2880 case MPLS_IMP_NULL_LABEL
:
2881 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2884 sprintf(lstr
, "%u", snhlfe
->out_label
);
2888 vty_out (vty
, "mpls lsp %u %s %s%s",
2889 slsp
->ile
.in_label
, buf
, lstr
, VTY_NEWLINE
);
2893 list_delete (slsp_list
);
2894 return (zvrf
->slsp_table
->count
? 1 : 0);
2898 * Add/update global label block.
2901 zebra_mpls_label_block_add (struct zebra_vrf
*zvrf
, u_int32_t start_label
,
2902 u_int32_t end_label
)
2904 zvrf
->mpls_srgb
.start_label
= start_label
;
2905 zvrf
->mpls_srgb
.end_label
= end_label
;
2907 /* Evaluate registered FECs to see if any get a label or not. */
2908 fec_evaluate (zvrf
, 1);
2913 * Delete global label block.
2916 zebra_mpls_label_block_del (struct zebra_vrf
*zvrf
)
2918 zvrf
->mpls_srgb
.start_label
= 0;
2919 zvrf
->mpls_srgb
.end_label
= 0;
2921 /* Process registered FECs to clear their local label, if needed. */
2922 fec_evaluate (zvrf
, 0);
2927 * Display MPLS global label block configuration (VTY command handler).
2930 zebra_mpls_write_label_block_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2932 if (zvrf
->mpls_srgb
.start_label
== 0)
2935 vty_out(vty
, "mpls label global-block %u %u%s",
2936 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
,
2943 * Called upon process exiting, need to delete LSP forwarding
2944 * entries from the kernel.
2945 * NOTE: Currently supported only for default VRF.
2948 zebra_mpls_close_tables (struct zebra_vrf
*zvrf
)
2950 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2951 hash_clean(zvrf
->lsp_table
, NULL
);
2952 hash_free(zvrf
->lsp_table
);
2953 hash_clean(zvrf
->slsp_table
, NULL
);
2954 hash_free(zvrf
->slsp_table
);
2958 * Allocate MPLS tables for this VRF and do other initialization.
2959 * NOTE: Currently supported only for default VRF.
2962 zebra_mpls_init_tables (struct zebra_vrf
*zvrf
)
2966 zvrf
->slsp_table
= hash_create(label_hash
, label_cmp
);
2967 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
);
2968 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2969 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2970 zvrf
->mpls_flags
= 0;
2971 zvrf
->mpls_srgb
.start_label
= 0;
2972 zvrf
->mpls_srgb
.end_label
= 0;
2976 * Global MPLS initialization.
2979 zebra_mpls_init (void)
2981 if (mpls_kernel_init () < 0)
2983 zlog_warn ("Disabling MPLS support (no kernel support)");
2988 mpls_processq_init (&zebrad
);