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 if (zebrad
.lsp_process_q
== NULL
)
1042 zlog_err ("%s: work_queue does not exist!", __func__
);
1046 work_queue_add (zebrad
.lsp_process_q
, lsp
);
1047 SET_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
);
1052 * Callback to allocate LSP forwarding table entry.
1057 const zebra_ile_t
*ile
= p
;
1060 lsp
= XCALLOC (MTYPE_LSP
, sizeof(zebra_lsp_t
));
1063 if (IS_ZEBRA_DEBUG_MPLS
)
1064 zlog_debug ("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1066 return ((void *)lsp
);
1070 * Create printable string for NHLFE entry.
1073 nhlfe2str (zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1075 struct nexthop
*nexthop
;
1078 nexthop
= nhlfe
->nexthop
;
1079 switch (nexthop
->type
)
1081 case NEXTHOP_TYPE_IPV4
:
1082 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1083 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1085 case NEXTHOP_TYPE_IPV6
:
1086 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1096 * Check if NHLFE matches with search info passed.
1099 nhlfe_nhop_match (zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1100 union g_addr
*gate
, ifindex_t ifindex
)
1102 struct nexthop
*nhop
;
1105 nhop
= nhlfe
->nexthop
;
1109 if (nhop
->type
!= gtype
)
1114 case NEXTHOP_TYPE_IPV4
:
1115 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1116 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1117 sizeof(struct in_addr
));
1118 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1119 cmp
= !(nhop
->ifindex
== ifindex
);
1121 case NEXTHOP_TYPE_IPV6
:
1122 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1123 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1124 sizeof(struct in6_addr
));
1125 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1126 cmp
= !(nhop
->ifindex
== ifindex
);
1137 * Locate NHLFE that matches with passed info.
1139 static zebra_nhlfe_t
*
1140 nhlfe_find (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1141 enum nexthop_types_t gtype
, union g_addr
*gate
,
1144 zebra_nhlfe_t
*nhlfe
;
1149 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1151 if (nhlfe
->type
!= lsp_type
)
1153 if (!nhlfe_nhop_match (nhlfe
, gtype
, gate
, ifindex
))
1161 * Add NHLFE. Base entry must have been created and duplicate
1164 static zebra_nhlfe_t
*
1165 nhlfe_add (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1166 enum nexthop_types_t gtype
, union g_addr
*gate
,
1167 ifindex_t ifindex
, mpls_label_t out_label
)
1169 zebra_nhlfe_t
*nhlfe
;
1170 struct nexthop
*nexthop
;
1175 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1180 nhlfe
->type
= lsp_type
;
1181 nhlfe
->distance
= lsp_distance (lsp_type
);
1183 nexthop
= nexthop_new();
1186 XFREE (MTYPE_NHLFE
, nhlfe
);
1189 nexthop_add_labels (nexthop
, lsp_type
, 1, &out_label
);
1191 nexthop
->type
= gtype
;
1192 switch (nexthop
->type
)
1194 case NEXTHOP_TYPE_IPV4
:
1195 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1196 nexthop
->gate
.ipv4
= gate
->ipv4
;
1198 nexthop
->ifindex
= ifindex
;
1200 case NEXTHOP_TYPE_IPV6
:
1201 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1202 nexthop
->gate
.ipv6
= gate
->ipv6
;
1204 nexthop
->ifindex
= ifindex
;
1207 nexthop_free(nexthop
);
1208 XFREE (MTYPE_NHLFE
, nhlfe
);
1213 nhlfe
->nexthop
= nexthop
;
1214 if (lsp
->nhlfe_list
)
1215 lsp
->nhlfe_list
->prev
= nhlfe
;
1216 nhlfe
->next
= lsp
->nhlfe_list
;
1217 lsp
->nhlfe_list
= nhlfe
;
1223 * Delete NHLFE. Entry must be present on list.
1226 nhlfe_del (zebra_nhlfe_t
*nhlfe
)
1239 nexthop_free(nhlfe
->nexthop
);
1241 /* Unlink from LSP */
1243 nhlfe
->next
->prev
= nhlfe
->prev
;
1245 nhlfe
->prev
->next
= nhlfe
->next
;
1247 lsp
->nhlfe_list
= nhlfe
->next
;
1249 if (nhlfe
== lsp
->best_nhlfe
)
1250 lsp
->best_nhlfe
= NULL
;
1252 XFREE (MTYPE_NHLFE
, nhlfe
);
1258 * Update label for NHLFE entry.
1261 nhlfe_out_label_update (zebra_nhlfe_t
*nhlfe
, struct nexthop_label
*nh_label
)
1263 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1267 mpls_lsp_uninstall_all (struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1268 enum lsp_types_t type
)
1270 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1271 int schedule_lsp
= 0;
1274 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1275 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
1277 nhlfe_next
= nhlfe
->next
;
1279 /* Skip non-static NHLFEs */
1280 if (nhlfe
->type
!= type
)
1283 if (IS_ZEBRA_DEBUG_MPLS
)
1285 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
1286 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
1287 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1290 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1292 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1293 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1302 /* Queue LSP for processing, if needed, else delete. */
1305 if (lsp_processq_add (lsp
))
1308 else if (!lsp
->nhlfe_list
&&
1309 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
1311 if (IS_ZEBRA_DEBUG_MPLS
)
1312 zlog_debug ("Free LSP in-label %u flags 0x%x",
1313 lsp
->ile
.in_label
, lsp
->flags
);
1315 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1317 XFREE(MTYPE_LSP
, lsp
);
1324 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1325 * If no other NHLFEs exist, the entry would be deleted.
1328 mpls_static_lsp_uninstall_all (struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
1330 struct hash
*lsp_table
;
1331 zebra_ile_t tmp_ile
;
1335 lsp_table
= zvrf
->lsp_table
;
1339 /* If entry is not present, exit. */
1340 tmp_ile
.in_label
= in_label
;
1341 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
1342 if (!lsp
|| !lsp
->nhlfe_list
)
1345 return mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1348 static json_object
*
1349 nhlfe_json (zebra_nhlfe_t
*nhlfe
)
1352 json_object
*json_nhlfe
= NULL
;
1353 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1355 json_nhlfe
= json_object_new_object();
1356 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1357 json_object_int_add(json_nhlfe
, "outLabel", nexthop
->nh_label
->label
[0]);
1358 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1360 if (CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1361 json_object_boolean_true_add(json_nhlfe
, "installed");
1363 switch (nexthop
->type
)
1365 case NEXTHOP_TYPE_IPV4
:
1366 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1367 json_object_string_add(json_nhlfe
, "nexthop",
1368 inet_ntoa (nexthop
->gate
.ipv4
));
1370 case NEXTHOP_TYPE_IPV6
:
1371 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1372 json_object_string_add(json_nhlfe
, "nexthop",
1373 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1375 if (nexthop
->ifindex
)
1376 json_object_string_add(json_nhlfe
, "interface", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1385 * Print the NHLFE for a LSP forwarding entry.
1388 nhlfe_print (zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1390 struct nexthop
*nexthop
;
1393 nexthop
= nhlfe
->nexthop
;
1394 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1397 vty_out(vty
, " type: %s remote label: %s distance: %d%s",
1398 nhlfe_type2str(nhlfe
->type
),
1399 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1400 nhlfe
->distance
, VTY_NEWLINE
);
1401 switch (nexthop
->type
)
1403 case NEXTHOP_TYPE_IPV4
:
1404 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1405 vty_out (vty
, " via %s", inet_ntoa (nexthop
->gate
.ipv4
));
1406 if (nexthop
->ifindex
)
1407 vty_out (vty
, " dev %s", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1409 case NEXTHOP_TYPE_IPV6
:
1410 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1411 vty_out (vty
, " via %s",
1412 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1413 if (nexthop
->ifindex
)
1414 vty_out (vty
, " dev %s", ifindex2ifname (nexthop
->ifindex
, VRF_DEFAULT
));
1419 vty_out(vty
, "%s", CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ?
1420 " (installed)" : "");
1421 vty_out(vty
, "%s", VTY_NEWLINE
);
1425 * Print an LSP forwarding entry.
1428 lsp_print (zebra_lsp_t
*lsp
, void *ctxt
)
1430 zebra_nhlfe_t
*nhlfe
;
1433 vty
= (struct vty
*) ctxt
;
1435 vty_out(vty
, "Local label: %u%s%s",
1437 CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)" : "",
1440 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1441 nhlfe_print (nhlfe
, vty
);
1445 * JSON objects for an LSP forwarding entry.
1447 static json_object
*
1448 lsp_json (zebra_lsp_t
*lsp
)
1450 zebra_nhlfe_t
*nhlfe
= NULL
;
1451 json_object
*json
= json_object_new_object();
1452 json_object
*json_nhlfe_list
= json_object_new_array();
1454 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1456 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
))
1457 json_object_boolean_true_add(json
, "installed");
1459 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1460 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1462 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1467 /* Return a sorted linked list of the hash contents */
1468 static struct list
*
1469 hash_get_sorted_list (struct hash
*hash
, void *cmp
)
1472 struct hash_backet
*hb
;
1473 struct list
*sorted_list
= list_new();
1475 sorted_list
->cmp
= (int (*)(void *, void *)) cmp
;
1477 for (i
= 0; i
< hash
->size
; i
++)
1478 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1479 listnode_add_sort(sorted_list
, hb
->data
);
1485 * Compare two LSPs based on their label values.
1488 lsp_cmp (zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1490 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1493 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1500 * Callback to allocate static LSP.
1503 slsp_alloc (void *p
)
1505 const zebra_ile_t
*ile
= p
;
1508 slsp
= XCALLOC (MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1510 return ((void *)slsp
);
1514 * Compare two static LSPs based on their label values.
1517 slsp_cmp (zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1519 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1522 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1529 * Check if static NHLFE matches with search info passed.
1532 snhlfe_match (zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1533 union g_addr
*gate
, ifindex_t ifindex
)
1537 if (snhlfe
->gtype
!= gtype
)
1540 switch (snhlfe
->gtype
)
1542 case NEXTHOP_TYPE_IPV4
:
1543 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1544 sizeof(struct in_addr
));
1546 case NEXTHOP_TYPE_IPV6
:
1547 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1548 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1549 sizeof(struct in6_addr
));
1550 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1551 cmp
= !(snhlfe
->ifindex
== ifindex
);
1561 * Locate static NHLFE that matches with passed info.
1563 static zebra_snhlfe_t
*
1564 snhlfe_find (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
1565 union g_addr
*gate
, ifindex_t ifindex
)
1567 zebra_snhlfe_t
*snhlfe
;
1572 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
*
1587 snhlfe_add (zebra_slsp_t
*slsp
, 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
;
1602 case NEXTHOP_TYPE_IPV4
:
1603 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1605 case NEXTHOP_TYPE_IPV6
:
1606 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1607 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1609 snhlfe
->ifindex
= ifindex
;
1612 XFREE (MTYPE_SNHLFE
, snhlfe
);
1616 if (slsp
->snhlfe_list
)
1617 slsp
->snhlfe_list
->prev
= snhlfe
;
1618 snhlfe
->next
= slsp
->snhlfe_list
;
1619 slsp
->snhlfe_list
= snhlfe
;
1625 * Delete static NHLFE. Entry must be present on list.
1628 snhlfe_del (zebra_snhlfe_t
*snhlfe
)
1635 slsp
= snhlfe
->slsp
;
1640 snhlfe
->next
->prev
= snhlfe
->prev
;
1642 snhlfe
->prev
->next
= snhlfe
->next
;
1644 slsp
->snhlfe_list
= snhlfe
->next
;
1646 snhlfe
->prev
= snhlfe
->next
= NULL
;
1648 XFREE (MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1649 XFREE (MTYPE_SNHLFE
, snhlfe
);
1655 * Delete all static NHLFE entries for this LSP (in label).
1658 snhlfe_del_all (zebra_slsp_t
*slsp
)
1660 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1665 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
)
1667 snhlfe_next
= snhlfe
->next
;
1668 snhlfe_del (snhlfe
);
1675 * Create printable string for NHLFE configuration.
1678 snhlfe2str (zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1681 switch (snhlfe
->gtype
)
1683 case NEXTHOP_TYPE_IPV4
:
1684 inet_ntop (AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1686 case NEXTHOP_TYPE_IPV6
:
1687 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1688 inet_ntop (AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1689 if (snhlfe
->ifindex
)
1690 strcat (buf
, ifindex2ifname (snhlfe
->ifindex
, VRF_DEFAULT
));
1700 * Initialize work queue for processing changed LSPs.
1703 mpls_processq_init (struct zebra_t
*zebra
)
1705 zebra
->lsp_process_q
= work_queue_new (zebra
->master
, "LSP processing");
1706 if (!zebra
->lsp_process_q
)
1708 zlog_err ("%s: could not initialise work queue!", __func__
);
1712 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1713 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1714 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1715 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1716 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1717 zebra
->lsp_process_q
->spec
.hold
= 10;
1724 /* Public functions */
1727 * String to label conversion, labels separated by '/'.
1730 mpls_str2label (const char *label_str
, u_int8_t
*num_labels
,
1731 mpls_label_t
*labels
)
1737 for (i
= 0; i
< MPLS_MAX_LABELS
; i
++)
1741 label
= strtoul(label_str
, &endp
, 0);
1743 /* validity checks */
1744 if (endp
== label_str
)
1747 if (!IS_MPLS_UNRESERVED_LABEL(label
))
1753 *num_labels
= i
+ 1;
1757 /* Check separator. */
1761 label_str
= endp
+ 1;
1764 /* Too many labels. */
1769 * Label to string conversion, labels in string separated by '/'.
1772 mpls_label2str (u_int8_t num_labels
, mpls_label_t
*labels
,
1773 char *buf
, int len
, int pretty
)
1775 char *buf_ptr
= buf
;
1779 if (num_labels
== 1) {
1780 label2str(labels
[0], buf
, len
);
1781 } else if (num_labels
== 2) {
1782 label2str(labels
[0], buf
, len
);
1783 buf_ptr
+= strlen(buf
);
1785 snprintf (buf_ptr
, len
, "/");
1788 label2str(labels
[1], buf_ptr
, len
);
1791 if (num_labels
== 1)
1792 snprintf (buf
, len
, "%u", labels
[0]);
1793 else if (num_labels
== 2)
1794 snprintf (buf
, len
, "%u/%u", labels
[0], labels
[1]);
1800 * Install dynamic LSP entry.
1803 zebra_mpls_lsp_install (struct zebra_vrf
*zvrf
, struct route_node
*rn
, struct rib
*rib
)
1805 struct route_table
*table
;
1808 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1812 /* See if there is a configured label binding for this FEC. */
1813 fec
= fec_find (table
, &rn
->p
);
1814 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1817 /* We cannot install a label forwarding entry if local label is the
1818 * implicit-null label.
1820 if (fec
->label
== MPLS_IMP_NULL_LABEL
)
1823 if (lsp_install (zvrf
, fec
->label
, rn
, rib
))
1830 * Uninstall dynamic LSP entry, if any.
1833 zebra_mpls_lsp_uninstall (struct zebra_vrf
*zvrf
, struct route_node
*rn
, struct rib
*rib
)
1835 struct route_table
*table
;
1838 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1842 /* See if there is a configured label binding for this FEC. */
1843 fec
= fec_find (table
, &rn
->p
);
1844 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1847 /* Uninstall always removes all dynamic NHLFEs. */
1848 return lsp_uninstall (zvrf
, fec
->label
);
1852 * Registration from a client for the label binding for a FEC. If a binding
1853 * already exists, it is informed to the client.
1854 * NOTE: If there is a manually configured label binding, that is used.
1855 * Otherwise, if aa label index is specified, it means we have to allocate the
1856 * label from a locally configured label block (SRGB), if one exists and index
1860 zebra_mpls_fec_register (struct zebra_vrf
*zvrf
, struct prefix
*p
,
1861 u_int32_t label_index
, struct zserv
*client
)
1863 struct route_table
*table
;
1867 int label_change
= 0;
1868 u_int32_t old_label
;
1870 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1874 if (IS_ZEBRA_DEBUG_MPLS
)
1875 prefix2str(p
, buf
, BUFSIZ
);
1878 fec
= fec_find (table
, p
);
1881 fec
= fec_add (table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1884 prefix2str(p
, buf
, BUFSIZ
);
1885 zlog_err("Failed to add FEC %s upon register, client %s",
1886 buf
, zebra_route_string(client
->proto
));
1890 old_label
= MPLS_INVALID_LABEL
;
1895 /* Client may register same FEC with different label index. */
1896 new_client
= (listnode_lookup(fec
->client_list
, client
) == NULL
);
1897 if (!new_client
&& fec
->label_index
== label_index
)
1898 /* Duplicate register */
1901 /* Save current label, update label index */
1902 old_label
= fec
->label
;
1903 fec
->label_index
= label_index
;
1907 listnode_add (fec
->client_list
, client
);
1909 if (IS_ZEBRA_DEBUG_MPLS
)
1910 zlog_debug("FEC %s Label Index %u %s by client %s",
1911 buf
, label_index
, new_client
? "registered" : "updated",
1912 zebra_route_string(client
->proto
));
1914 /* If not a configured FEC, derive the local label (from label index)
1917 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
1919 fec_derive_label_from_index (zvrf
, fec
);
1921 /* If no label change, exit. */
1922 if (fec
->label
== old_label
)
1928 /* If new client or label change, update client and install or uninstall
1929 * label forwarding entry as needed.
1931 /* Inform client of label, if needed. */
1932 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) ||
1935 if (IS_ZEBRA_DEBUG_MPLS
)
1936 zlog_debug ("Update client label %u", fec
->label
);
1937 fec_send (fec
, client
);
1940 if (new_client
|| label_change
)
1941 return fec_change_update_lsp (zvrf
, fec
, old_label
);
1947 * Deregistration from a client for the label binding for a FEC. The FEC
1948 * itself is deleted if no other registered clients exist and there is no
1949 * label bound to the FEC.
1952 zebra_mpls_fec_unregister (struct zebra_vrf
*zvrf
, struct prefix
*p
,
1953 struct zserv
*client
)
1955 struct route_table
*table
;
1959 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1963 if (IS_ZEBRA_DEBUG_MPLS
)
1964 prefix2str(p
, buf
, BUFSIZ
);
1966 fec
= fec_find (table
, p
);
1969 prefix2str(p
, buf
, BUFSIZ
);
1970 zlog_err("Failed to find FEC %s upon unregister, client %s",
1971 buf
, zebra_route_string(client
->proto
));
1975 listnode_delete(fec
->client_list
, client
);
1977 if (IS_ZEBRA_DEBUG_MPLS
)
1978 zlog_debug("FEC %s unregistered by client %s",
1979 buf
, zebra_route_string(client
->proto
));
1981 /* If not a configured entry, delete the FEC if no other clients. Before
1982 * deleting, see if any LSP needs to be uninstalled.
1984 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
) &&
1985 list_isempty(fec
->client_list
))
1987 mpls_label_t old_label
= fec
->label
;
1988 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1989 fec_change_update_lsp (zvrf
, fec
, old_label
);
1997 * Cleanup any FECs registered by this client.
2000 zebra_mpls_cleanup_fecs_for_client (struct zebra_vrf
*zvrf
, struct zserv
*client
)
2002 struct route_node
*rn
;
2004 struct listnode
*node
;
2005 struct zserv
*fec_client
;
2008 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2010 if (zvrf
->fec_table
[af
] == NULL
)
2013 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2016 if (!fec
|| list_isempty(fec
->client_list
))
2019 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, fec_client
))
2021 if (fec_client
== client
)
2023 listnode_delete(fec
->client_list
, fec_client
);
2024 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
) &&
2025 list_isempty(fec
->client_list
))
2037 * Return FEC (if any) to which this label is bound.
2038 * Note: Only works for per-prefix binding and when the label is not
2040 * TODO: Currently walks entire table, can optimize later with another
2044 zebra_mpls_fec_for_label (struct zebra_vrf
*zvrf
, mpls_label_t label
)
2046 struct route_node
*rn
;
2050 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2052 if (zvrf
->fec_table
[af
] == NULL
)
2055 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2060 if (fec
->label
== label
)
2069 * Inform if specified label is currently bound to a FEC or not.
2072 zebra_mpls_label_already_bound (struct zebra_vrf
*zvrf
, mpls_label_t label
)
2074 return (zebra_mpls_fec_for_label (zvrf
, label
) ? 1 : 0);
2078 * Add static FEC to label binding. If there are clients registered for this
2079 * FEC, notify them. If there are labeled routes for this FEC, install the
2080 * label forwarding entry.
2083 zebra_mpls_static_fec_add (struct zebra_vrf
*zvrf
, struct prefix
*p
,
2084 mpls_label_t in_label
)
2086 struct route_table
*table
;
2089 mpls_label_t old_label
;
2092 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2096 if (IS_ZEBRA_DEBUG_MPLS
)
2097 prefix2str(p
, buf
, BUFSIZ
);
2099 /* Update existing FEC or create a new one. */
2100 fec
= fec_find (table
, p
);
2103 fec
= fec_add (table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2104 MPLS_INVALID_LABEL_INDEX
);
2107 prefix2str(p
, buf
, BUFSIZ
);
2108 zlog_err ("Failed to add FEC %s upon config", buf
);
2112 if (IS_ZEBRA_DEBUG_MPLS
)
2113 zlog_debug ("Add fec %s label %u", buf
, in_label
);
2117 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2118 if (fec
->label
== in_label
)
2119 /* Duplicate config */
2122 /* Label change, update clients. */
2123 old_label
= fec
->label
;
2124 if (IS_ZEBRA_DEBUG_MPLS
)
2125 zlog_debug ("Update fec %s new label %u", buf
, in_label
);
2127 fec
->label
= in_label
;
2128 fec_update_clients (fec
);
2130 /* Update label forwarding entries appropriately */
2131 ret
= fec_change_update_lsp (zvrf
, fec
, old_label
);
2138 * Remove static FEC to label binding. If there are no clients registered
2139 * for this FEC, delete the FEC; else notify clients
2140 * Note: Upon delete of static binding, if label index exists for this FEC,
2141 * client may need to be updated with derived label.
2144 zebra_mpls_static_fec_del (struct zebra_vrf
*zvrf
, struct prefix
*p
)
2146 struct route_table
*table
;
2148 mpls_label_t old_label
;
2151 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2155 fec
= fec_find (table
, p
);
2158 prefix2str(p
, buf
, BUFSIZ
);
2159 zlog_err("Failed to find FEC %s upon delete", buf
);
2163 if (IS_ZEBRA_DEBUG_MPLS
)
2165 prefix2str(p
, buf
, BUFSIZ
);
2166 zlog_debug ("Delete fec %s label index %u",
2167 buf
, fec
->label_index
);
2170 old_label
= fec
->label
;
2171 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2172 fec
->label
= MPLS_INVALID_LABEL
;
2174 /* If no client exists, just delete the FEC. */
2175 if (list_isempty(fec
->client_list
))
2181 /* Derive the local label (from label index) or reset it. */
2182 fec_derive_label_from_index (zvrf
, fec
);
2184 /* If there is a label change, update clients. */
2185 if (fec
->label
== old_label
)
2187 fec_update_clients (fec
);
2189 /* Update label forwarding entries appropriately */
2190 return fec_change_update_lsp (zvrf
, fec
, old_label
);
2194 * Display MPLS FEC to label binding configuration (VTY command handler).
2197 zebra_mpls_write_fec_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2199 struct route_node
*rn
;
2205 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2207 if (zvrf
->fec_table
[af
] == NULL
)
2210 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2218 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2222 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2223 vty_out(vty
, "mpls label bind %s %s%s", buf
,
2224 label2str(fec
->label
, lstr
, BUFSIZ
), VTY_NEWLINE
);
2232 * Display MPLS FEC to label binding (VTY command handler).
2235 zebra_mpls_print_fec_table (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2237 struct route_node
*rn
;
2240 for (af
= AFI_IP
; af
< AFI_MAX
; af
++)
2242 if (zvrf
->fec_table
[af
] == NULL
)
2245 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
; rn
= route_next(rn
))
2249 fec_print (rn
->info
, vty
);
2255 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2258 zebra_mpls_print_fec (struct vty
*vty
, struct zebra_vrf
*zvrf
, struct prefix
*p
)
2260 struct route_table
*table
;
2261 struct route_node
*rn
;
2263 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2268 rn
= route_node_lookup(table
, p
);
2272 route_unlock_node(rn
);
2276 fec_print (rn
->info
, vty
);
2280 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2283 mpls_ftn_update (int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2284 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2285 union g_addr
*gate
, ifindex_t ifindex
, u_int8_t distance
,
2286 mpls_label_t out_label
)
2288 struct route_table
*table
;
2289 struct route_node
*rn
;
2291 struct nexthop
*nexthop
;
2294 table
= zebra_vrf_table (family2afi(prefix
->family
), SAFI_UNICAST
, zvrf_id (zvrf
));
2298 /* Lookup existing route */
2299 rn
= route_node_get (table
, prefix
);
2300 RNODE_FOREACH_RIB (rn
, rib
)
2302 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2304 if (rib
->distance
== distance
)
2311 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2313 switch (nexthop
->type
)
2315 case NEXTHOP_TYPE_IPV4
:
2316 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2317 if (gtype
!= NEXTHOP_TYPE_IPV4
&& gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2319 if (! IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2321 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
&&
2322 nexthop
->ifindex
!= ifindex
)
2325 case NEXTHOP_TYPE_IPV6
:
2326 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2327 if (gtype
!= NEXTHOP_TYPE_IPV6
&& gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2329 if (! IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2331 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
&&
2332 nexthop
->ifindex
!= ifindex
)
2339 /* nexthop not found */
2343 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2344 nexthop_add_labels (nexthop
, type
, 1, &out_label
);
2345 else if (!add
&& nexthop
->nh_label_type
== type
)
2346 nexthop_del_labels (nexthop
);
2350 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
2351 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
2358 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2359 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2360 * the out-label for an existing NHLFE (update case).
2363 mpls_lsp_install (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2364 mpls_label_t in_label
, mpls_label_t out_label
,
2365 enum nexthop_types_t gtype
, union g_addr
*gate
,
2368 struct hash
*lsp_table
;
2369 zebra_ile_t tmp_ile
;
2371 zebra_nhlfe_t
*nhlfe
;
2375 lsp_table
= zvrf
->lsp_table
;
2379 /* If entry is present, exit. */
2380 tmp_ile
.in_label
= in_label
;
2381 lsp
= hash_get (lsp_table
, &tmp_ile
, lsp_alloc
);
2384 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifindex
);
2387 struct nexthop
*nh
= nhlfe
->nexthop
;
2390 assert (nh
->nh_label
);
2392 /* Clear deleted flag (in case it was set) */
2393 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2394 if (nh
->nh_label
->label
[0] == out_label
)
2398 if (IS_ZEBRA_DEBUG_MPLS
)
2400 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2401 zlog_debug ("LSP in-label %u type %d nexthop %s "
2402 "out-label changed to %u (old %u)",
2403 in_label
, type
, buf
,
2404 out_label
, nh
->nh_label
->label
[0]);
2407 /* Update out label, trigger processing. */
2408 nh
->nh_label
->label
[0] = out_label
;
2412 /* Add LSP entry to this nexthop */
2413 nhlfe
= nhlfe_add (lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2417 if (IS_ZEBRA_DEBUG_MPLS
)
2419 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2420 zlog_debug ("Add LSP in-label %u type %d nexthop %s "
2421 "out-label %u", in_label
, type
, buf
, out_label
);
2424 lsp
->addr_family
= NHLFE_FAMILY (nhlfe
);
2427 /* Mark NHLFE, queue LSP for processing. */
2428 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2429 if (lsp_processq_add (lsp
))
2436 * Uninstall a particular NHLFE in the forwarding table. If this is
2437 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2440 mpls_lsp_uninstall (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2441 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2442 union g_addr
*gate
, ifindex_t ifindex
)
2444 struct hash
*lsp_table
;
2445 zebra_ile_t tmp_ile
;
2447 zebra_nhlfe_t
*nhlfe
;
2451 lsp_table
= zvrf
->lsp_table
;
2455 /* If entry is not present, exit. */
2456 tmp_ile
.in_label
= in_label
;
2457 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
2460 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifindex
);
2464 if (IS_ZEBRA_DEBUG_MPLS
)
2466 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
2467 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2468 in_label
, type
, buf
, nhlfe
->flags
);
2471 /* Mark NHLFE for delete or directly delete, as appropriate. */
2472 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
2474 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2475 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2476 if (lsp_processq_add (lsp
))
2483 /* Free LSP entry if no other NHLFEs and not scheduled. */
2484 if (!lsp
->nhlfe_list
&&
2485 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
2487 if (IS_ZEBRA_DEBUG_MPLS
)
2488 zlog_debug ("Free LSP in-label %u flags 0x%x",
2489 lsp
->ile
.in_label
, lsp
->flags
);
2491 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2493 XFREE(MTYPE_LSP
, lsp
);
2500 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2501 * If no other NHLFEs exist, the entry would be deleted.
2504 mpls_ldp_lsp_uninstall_all (struct hash_backet
*backet
, void *ctxt
)
2507 struct hash
*lsp_table
;
2509 lsp
= (zebra_lsp_t
*) backet
->data
;
2510 if (!lsp
|| !lsp
->nhlfe_list
)
2517 mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2521 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2524 mpls_ldp_ftn_uninstall_all (struct zebra_vrf
*zvrf
, int afi
)
2526 struct route_table
*table
;
2527 struct route_node
*rn
;
2529 struct nexthop
*nexthop
;
2532 /* Process routes of interested address-families. */
2533 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, zvrf_id (zvrf
));
2537 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2540 RNODE_FOREACH_RIB (rn
, rib
)
2541 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2542 if (nexthop
->nh_label_type
== ZEBRA_LSP_LDP
)
2544 nexthop_del_labels (nexthop
);
2545 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
2546 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
2555 #if defined(HAVE_CUMULUS)
2557 * Check that the label values used in LSP creation are consistent. The
2558 * main criteria is that if there is ECMP, the label operation must still
2559 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2560 * to current HW restrictions.
2563 zebra_mpls_lsp_label_consistent (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2564 mpls_label_t out_label
, enum nexthop_types_t gtype
,
2565 union g_addr
*gate
, ifindex_t ifindex
)
2567 struct hash
*slsp_table
;
2568 zebra_ile_t tmp_ile
;
2570 zebra_snhlfe_t
*snhlfe
;
2573 slsp_table
= zvrf
->slsp_table
;
2577 /* If entry is not present, exit. */
2578 tmp_ile
.in_label
= in_label
;
2579 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
2583 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2586 if (snhlfe
->out_label
== out_label
)
2589 /* If not only NHLFE, cannot allow label change. */
2590 if (snhlfe
!= slsp
->snhlfe_list
||
2596 /* If other NHLFEs exist, label operation must match. */
2597 if (slsp
->snhlfe_list
)
2601 cur_op
= (slsp
->snhlfe_list
->out_label
== MPLS_IMP_NULL_LABEL
);
2602 new_op
= (out_label
== MPLS_IMP_NULL_LABEL
);
2603 if (cur_op
!= new_op
)
2608 /* Label values are good. */
2611 #endif /* HAVE_CUMULUS */
2614 * Add static LSP entry. This may be the first entry for this incoming label
2615 * or an additional nexthop; an existing entry may also have outgoing label
2617 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2621 zebra_mpls_static_lsp_add (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2622 mpls_label_t out_label
, enum nexthop_types_t gtype
,
2623 union g_addr
*gate
, ifindex_t ifindex
)
2625 struct hash
*slsp_table
;
2626 zebra_ile_t tmp_ile
;
2628 zebra_snhlfe_t
*snhlfe
;
2632 slsp_table
= zvrf
->slsp_table
;
2636 /* If entry is present, exit. */
2637 tmp_ile
.in_label
= in_label
;
2638 slsp
= hash_get (slsp_table
, &tmp_ile
, slsp_alloc
);
2641 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2644 if (snhlfe
->out_label
== out_label
)
2648 if (IS_ZEBRA_DEBUG_MPLS
)
2650 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2651 zlog_debug ("Upd static LSP in-label %u nexthop %s "
2652 "out-label %u (old %u)",
2653 in_label
, buf
, out_label
, snhlfe
->out_label
);
2655 snhlfe
->out_label
= out_label
;
2659 /* Add static LSP entry to this nexthop */
2660 snhlfe
= snhlfe_add (slsp
, gtype
, gate
, ifindex
, out_label
);
2664 if (IS_ZEBRA_DEBUG_MPLS
)
2666 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2667 zlog_debug ("Add static LSP in-label %u nexthop %s out-label %u",
2668 in_label
, buf
, out_label
);
2672 /* (Re)Install LSP in the main table. */
2673 if (mpls_lsp_install (zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2681 * Delete static LSP entry. This may be the delete of one particular
2682 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2684 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2685 * LSP configuration.
2688 zebra_mpls_static_lsp_del (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2689 enum nexthop_types_t gtype
, union g_addr
*gate
,
2692 struct hash
*slsp_table
;
2693 zebra_ile_t tmp_ile
;
2695 zebra_snhlfe_t
*snhlfe
;
2698 slsp_table
= zvrf
->slsp_table
;
2702 /* If entry is not present, exit. */
2703 tmp_ile
.in_label
= in_label
;
2704 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
2708 /* Is it delete of entire LSP or a specific NHLFE? */
2709 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
)
2711 if (IS_ZEBRA_DEBUG_MPLS
)
2712 zlog_debug ("Del static LSP in-label %u", in_label
);
2714 /* Uninstall entire LSP from the main table. */
2715 mpls_static_lsp_uninstall_all (zvrf
, in_label
);
2717 /* Delete all static NHLFEs */
2718 snhlfe_del_all (slsp
);
2722 /* Find specific NHLFE, exit if not found. */
2723 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifindex
);
2727 if (IS_ZEBRA_DEBUG_MPLS
)
2730 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2731 zlog_debug ("Del static LSP in-label %u nexthop %s",
2735 /* Uninstall LSP from the main table. */
2736 mpls_lsp_uninstall (zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
, gate
,
2739 /* Delete static LSP NHLFE */
2740 snhlfe_del (snhlfe
);
2743 /* Remove entire static LSP entry if no NHLFE - valid in either case above. */
2744 if (!slsp
->snhlfe_list
)
2746 slsp
= hash_release(slsp_table
, &tmp_ile
);
2748 XFREE(MTYPE_SLSP
, slsp
);
2755 * Schedule all MPLS label forwarding entries for processing.
2756 * Called upon changes that may affect one or more of them such as
2757 * interface or nexthop state changes.
2760 zebra_mpls_lsp_schedule (struct zebra_vrf
*zvrf
)
2764 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2768 * Display MPLS label forwarding table for a specific LSP
2769 * (VTY command handler).
2772 zebra_mpls_print_lsp (struct vty
*vty
, struct zebra_vrf
*zvrf
, mpls_label_t label
,
2775 struct hash
*lsp_table
;
2777 zebra_ile_t tmp_ile
;
2778 json_object
*json
= NULL
;
2781 lsp_table
= zvrf
->lsp_table
;
2785 /* If entry is not present, exit. */
2786 tmp_ile
.in_label
= label
;
2787 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
2793 json
= lsp_json(lsp
);
2794 vty_out (vty
, "%s%s", json_object_to_json_string_ext(json
, JSON_C_TO_STRING_PRETTY
), VTY_NEWLINE
);
2795 json_object_free(json
);
2798 lsp_print (lsp
, (void *)vty
);
2802 * Display MPLS label forwarding table (VTY command handler).
2805 zebra_mpls_print_lsp_table (struct vty
*vty
, struct zebra_vrf
*zvrf
,
2809 json_object
*json
= NULL
;
2810 zebra_lsp_t
*lsp
= NULL
;
2811 zebra_nhlfe_t
*nhlfe
= NULL
;
2812 struct nexthop
*nexthop
= NULL
;
2813 struct listnode
*node
= NULL
;
2814 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2818 json
= json_object_new_object();
2820 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2821 json_object_object_add(json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2824 vty_out (vty
, "%s%s", json_object_to_json_string_ext(json
, JSON_C_TO_STRING_PRETTY
), VTY_NEWLINE
);
2825 json_object_free(json
);
2829 vty_out (vty
, " Inbound Outbound%s", VTY_NEWLINE
);
2830 vty_out (vty
, " Label Type Nexthop Label%s", VTY_NEWLINE
);
2831 vty_out (vty
, "-------- ------- --------------- --------%s", VTY_NEWLINE
);
2833 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2835 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
2837 vty_out (vty
, "%8d %7s ", lsp
->ile
.in_label
, nhlfe_type2str(nhlfe
->type
));
2838 nexthop
= nhlfe
->nexthop
;
2840 switch (nexthop
->type
)
2842 case NEXTHOP_TYPE_IPV4
:
2843 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2844 vty_out (vty
, "%15s", inet_ntoa (nexthop
->gate
.ipv4
));
2846 case NEXTHOP_TYPE_IPV6
:
2847 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2848 vty_out (vty
, "%15s", inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
2854 vty_out (vty
, " %8d%s", nexthop
->nh_label
->label
[0], VTY_NEWLINE
);
2858 vty_out (vty
, "%s", VTY_NEWLINE
);
2861 list_delete (lsp_list
);
2865 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2868 zebra_mpls_write_lsp_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2871 zebra_snhlfe_t
*snhlfe
;
2872 struct listnode
*node
;
2873 struct list
*slsp_list
= hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2875 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
))
2877 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
)
2879 char buf
[INET6_ADDRSTRLEN
];
2882 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
2883 switch (snhlfe
->out_label
) {
2884 case MPLS_V4_EXP_NULL_LABEL
:
2885 case MPLS_V6_EXP_NULL_LABEL
:
2886 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2888 case MPLS_IMP_NULL_LABEL
:
2889 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2892 sprintf(lstr
, "%u", snhlfe
->out_label
);
2896 vty_out (vty
, "mpls lsp %u %s %s%s",
2897 slsp
->ile
.in_label
, buf
, lstr
, VTY_NEWLINE
);
2901 list_delete (slsp_list
);
2902 return (zvrf
->slsp_table
->count
? 1 : 0);
2906 * Add/update global label block.
2909 zebra_mpls_label_block_add (struct zebra_vrf
*zvrf
, u_int32_t start_label
,
2910 u_int32_t end_label
)
2912 zvrf
->mpls_srgb
.start_label
= start_label
;
2913 zvrf
->mpls_srgb
.end_label
= end_label
;
2915 /* Evaluate registered FECs to see if any get a label or not. */
2916 fec_evaluate (zvrf
, 1);
2921 * Delete global label block.
2924 zebra_mpls_label_block_del (struct zebra_vrf
*zvrf
)
2926 zvrf
->mpls_srgb
.start_label
= 0;
2927 zvrf
->mpls_srgb
.end_label
= 0;
2929 /* Process registered FECs to clear their local label, if needed. */
2930 fec_evaluate (zvrf
, 0);
2935 * Display MPLS global label block configuration (VTY command handler).
2938 zebra_mpls_write_label_block_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
2940 if (zvrf
->mpls_srgb
.start_label
== 0)
2943 vty_out(vty
, "mpls label global-block %u %u%s",
2944 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
,
2951 * Called upon process exiting, need to delete LSP forwarding
2952 * entries from the kernel.
2953 * NOTE: Currently supported only for default VRF.
2956 zebra_mpls_close_tables (struct zebra_vrf
*zvrf
)
2958 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2959 hash_clean(zvrf
->lsp_table
, NULL
);
2960 hash_free(zvrf
->lsp_table
);
2961 hash_clean(zvrf
->slsp_table
, NULL
);
2962 hash_free(zvrf
->slsp_table
);
2966 * Allocate MPLS tables for this VRF and do other initialization.
2967 * NOTE: Currently supported only for default VRF.
2970 zebra_mpls_init_tables (struct zebra_vrf
*zvrf
)
2974 zvrf
->slsp_table
= hash_create(label_hash
, label_cmp
);
2975 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
);
2976 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2977 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2978 zvrf
->mpls_flags
= 0;
2979 zvrf
->mpls_srgb
.start_label
= 0;
2980 zvrf
->mpls_srgb
.end_label
= 0;
2984 * Global MPLS initialization.
2987 zebra_mpls_init (void)
2991 if (mpls_kernel_init () < 0)
2993 zlog_warn ("Disabling MPLS support (no kernel support)");
2997 if (! mpls_processq_init (&zebrad
))