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
31 #include "sockunion.h"
34 #include "workqueue.h"
41 #include "zebra/rib.h"
43 #include "zebra/zserv.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_memory.h"
47 #include "zebra/zebra_vrf.h"
48 #include "zebra/zebra_mpls.h"
50 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP 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")
56 /* Default rtm_table for all clients */
57 extern struct zebra_t zebrad
;
59 /* static function declarations */
63 label_cmp (const void *p1
, const void *p2
);
65 nhlfe_nexthop_active_ipv4 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
);
67 nhlfe_nexthop_active_ipv6 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
);
69 nhlfe_nexthop_active (zebra_nhlfe_t
*nhlfe
);
71 lsp_select_best_nhlfe (zebra_lsp_t
*lsp
);
73 lsp_uninstall_from_kernel (struct hash_backet
*backet
, void *ctxt
);
75 lsp_schedule (struct hash_backet
*backet
, void *ctxt
);
77 lsp_process (struct work_queue
*wq
, void *data
);
79 lsp_processq_del (struct work_queue
*wq
, void *data
);
81 lsp_processq_complete (struct work_queue
*wq
);
83 lsp_processq_add (zebra_lsp_t
*lsp
);
87 nhlfe2str (zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
89 nhlfe_nhop_match (zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
90 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
);
91 static zebra_nhlfe_t
*
92 nhlfe_find (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
93 enum nexthop_types_t gtype
, union g_addr
*gate
,
94 char *ifname
, ifindex_t ifindex
);
95 static zebra_nhlfe_t
*
96 nhlfe_add (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
97 enum nexthop_types_t gtype
, union g_addr
*gate
,
98 char *ifname
, ifindex_t ifindex
, mpls_label_t out_label
);
100 nhlfe_del (zebra_nhlfe_t
*snhlfe
);
102 mpls_lsp_uninstall_all (struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
103 enum lsp_types_t type
);
105 mpls_static_lsp_uninstall_all (struct zebra_vrf
*zvrf
, mpls_label_t in_label
);
107 nhlfe_print (zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
109 lsp_print (zebra_lsp_t
*lsp
, void *ctxt
);
111 slsp_alloc (void *p
);
113 snhlfe_match (zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
114 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
);
115 static zebra_snhlfe_t
*
116 snhlfe_find (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
117 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
);
118 static zebra_snhlfe_t
*
119 snhlfe_add (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
120 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
,
121 mpls_label_t out_label
);
123 snhlfe_del (zebra_snhlfe_t
*snhlfe
);
125 snhlfe_del_all (zebra_slsp_t
*slsp
);
127 snhlfe2str (zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
129 mpls_processq_init (struct zebra_t
*zebra
);
134 /* Static functions */
137 * Hash function for label.
142 const zebra_ile_t
*ile
= p
;
144 return (jhash_1word(ile
->in_label
, 0));
148 * Compare 2 LSP hash entries based on in-label.
151 label_cmp (const void *p1
, const void *p2
)
153 const zebra_ile_t
*ile1
= p1
;
154 const zebra_ile_t
*ile2
= p2
;
156 return (ile1
->in_label
== ile2
->in_label
);
160 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
162 * NOTE: Looking only for connected routes right now.
165 nhlfe_nexthop_active_ipv4 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
)
167 struct route_table
*table
;
168 struct prefix_ipv4 p
;
169 struct route_node
*rn
;
172 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
);
176 /* Lookup nexthop in IPv4 routing table. */
177 memset (&p
, 0, sizeof (struct prefix_ipv4
));
179 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
180 p
.prefix
= nexthop
->gate
.ipv4
;
182 rn
= route_node_match (table
, (struct prefix
*) &p
);
186 route_unlock_node (rn
);
188 /* Locate a valid connected route. */
189 RNODE_FOREACH_RIB (rn
, match
)
191 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) &&
192 !CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
) &&
193 CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
197 if (!match
|| !match
->nexthop
)
200 nexthop
->ifindex
= match
->nexthop
->ifindex
;
206 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
208 * NOTE: Looking only for connected routes right now.
211 nhlfe_nexthop_active_ipv6 (zebra_nhlfe_t
*nhlfe
, struct nexthop
*nexthop
)
213 struct route_table
*table
;
214 struct prefix_ipv6 p
;
215 struct route_node
*rn
;
218 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, VRF_DEFAULT
);
222 /* Lookup nexthop in IPv6 routing table. */
223 memset (&p
, 0, sizeof (struct prefix_ipv6
));
225 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
226 p
.prefix
= nexthop
->gate
.ipv6
;
228 rn
= route_node_match (table
, (struct prefix
*) &p
);
232 route_unlock_node (rn
);
234 /* Locate a valid connected route. */
235 RNODE_FOREACH_RIB (rn
, match
)
237 if ((match
->type
== ZEBRA_ROUTE_CONNECT
) &&
238 !CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
) &&
239 CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
243 if (!match
|| !match
->nexthop
)
246 nexthop
->ifindex
= match
->nexthop
->ifindex
;
252 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
254 * NOTE: Each NHLFE points to only 1 nexthop.
257 nhlfe_nexthop_active (zebra_nhlfe_t
*nhlfe
)
259 struct nexthop
*nexthop
;
260 struct interface
*ifp
;
262 nexthop
= nhlfe
->nexthop
;
263 if (!nexthop
) // unexpected
266 /* Check on nexthop based on type. */
267 switch (nexthop
->type
)
269 case NEXTHOP_TYPE_IPV4
:
270 if (nhlfe_nexthop_active_ipv4 (nhlfe
, nexthop
))
271 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
273 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
276 case NEXTHOP_TYPE_IPV6
:
277 if (nhlfe_nexthop_active_ipv6 (nhlfe
, nexthop
))
278 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
280 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
283 case NEXTHOP_TYPE_IPV6_IFINDEX
:
284 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
286 ifp
= if_lookup_by_index (nexthop
->ifindex
);
287 if (ifp
&& if_is_operative(ifp
))
288 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
290 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
294 if (nhlfe_nexthop_active_ipv6 (nhlfe
, nexthop
))
295 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
297 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
305 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
309 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
310 * reachability and select the best. Multipath entries are also
311 * marked. This is invoked when an LSP scheduled for processing (due
312 * to some change) is examined.
315 lsp_select_best_nhlfe (zebra_lsp_t
*lsp
)
317 zebra_nhlfe_t
*nhlfe
;
319 struct nexthop
*nexthop
;
327 UNSET_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
);
330 * First compute the best path, after checking nexthop status. We are only
331 * concerned with non-deleted NHLFEs.
333 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
335 /* Clear selection flags. */
336 UNSET_FLAG (nhlfe
->flags
,
337 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
339 if (!CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
) &&
340 nhlfe_nexthop_active (nhlfe
))
342 if (!best
|| (nhlfe
->distance
< best
->distance
))
347 lsp
->best_nhlfe
= best
;
348 if (!lsp
->best_nhlfe
)
351 /* Mark best NHLFE as selected. */
352 SET_FLAG (lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
355 * If best path exists, see if there is ECMP. While doing this, note if a
356 * new (uninstalled) NHLFE has been selected, an installed entry that is
357 * still selected has a change or an installed entry is to be removed.
359 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
361 int nh_chg
, nh_sel
, nh_inst
;
363 nexthop
= nhlfe
->nexthop
;
364 if (!nexthop
) // unexpected
367 if (!CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
) &&
368 CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) &&
369 (nhlfe
->distance
== lsp
->best_nhlfe
->distance
))
371 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
372 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
376 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
) &&
379 nh_chg
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
380 nh_sel
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
381 nh_inst
= CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
383 if ((nh_sel
&& !nh_inst
) ||
384 (nh_sel
&& nh_inst
&& nh_chg
) ||
385 (nh_inst
&& !nh_sel
))
389 /* We have finished examining, clear changed flag. */
390 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
394 SET_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
);
398 * Delete LSP forwarding entry from kernel, if installed. Called upon
402 lsp_uninstall_from_kernel (struct hash_backet
*backet
, void *ctxt
)
406 lsp
= (zebra_lsp_t
*) backet
->data
;
407 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
408 kernel_del_lsp (lsp
);
412 * Schedule LSP forwarding entry for processing. Called upon changes
413 * that may impact LSPs such as nexthop / connected route changes.
416 lsp_schedule (struct hash_backet
*backet
, void *ctxt
)
420 lsp
= (zebra_lsp_t
*) backet
->data
;
421 lsp_processq_add (lsp
);
425 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
426 * any multipaths and update or delete from the kernel, as needed.
428 static wq_item_status
429 lsp_process (struct work_queue
*wq
, void *data
)
432 zebra_nhlfe_t
*oldbest
, *newbest
;
433 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
435 lsp
= (zebra_lsp_t
*)data
;
436 if (!lsp
) // unexpected
439 oldbest
= lsp
->best_nhlfe
;
441 /* Select best NHLFE(s) */
442 lsp_select_best_nhlfe (lsp
);
444 newbest
= lsp
->best_nhlfe
;
446 if (IS_ZEBRA_DEBUG_MPLS
)
449 nhlfe2str (oldbest
, buf
, BUFSIZ
);
451 nhlfe2str (newbest
, buf2
, BUFSIZ
);
452 zlog_debug ("Process LSP in-label %u oldbest %s newbest %s "
453 "flags 0x%x ecmp# %d",
454 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
455 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
458 if (!CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
))
460 /* Not already installed */
462 kernel_add_lsp (lsp
);
466 /* Installed, may need an update and/or delete. */
468 kernel_del_lsp (lsp
);
469 else if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_CHANGED
))
470 kernel_upd_lsp (lsp
);
478 * Callback upon processing completion of a LSP forwarding entry.
481 lsp_processq_del (struct work_queue
*wq
, void *data
)
483 struct zebra_vrf
*zvrf
;
485 struct hash
*lsp_table
;
486 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
488 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
491 lsp_table
= zvrf
->lsp_table
;
492 if (!lsp_table
) // unexpected
495 lsp
= (zebra_lsp_t
*)data
;
496 if (!lsp
) // unexpected
499 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs exist,
500 * delete LSP entry also.
502 UNSET_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
);
504 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
506 nhlfe_next
= nhlfe
->next
;
507 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
511 if (!lsp
->nhlfe_list
)
513 if (IS_ZEBRA_DEBUG_MPLS
)
514 zlog_debug ("Free LSP in-label %u flags 0x%x",
515 lsp
->ile
.in_label
, lsp
->flags
);
517 lsp
= hash_release(lsp_table
, &lsp
->ile
);
519 XFREE(MTYPE_LSP
, lsp
);
524 * Callback upon finishing the processing of all scheduled
525 * LSP forwarding entries.
528 lsp_processq_complete (struct work_queue
*wq
)
530 /* Nothing to do for now. */
534 * Add LSP forwarding entry to queue for subsequent processing.
537 lsp_processq_add (zebra_lsp_t
*lsp
)
539 /* If already scheduled, exit. */
540 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
543 work_queue_add (zebrad
.lsp_process_q
, lsp
);
544 SET_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
);
549 * Callback to allocate LSP forwarding table entry.
554 const zebra_ile_t
*ile
= p
;
557 lsp
= XCALLOC (MTYPE_LSP
, sizeof(zebra_lsp_t
));
560 if (IS_ZEBRA_DEBUG_MPLS
)
561 zlog_debug ("Alloc LSP in-label %u", lsp
->ile
.in_label
);
563 return ((void *)lsp
);
567 * Create printable string for NHLFE entry.
570 nhlfe2str (zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
572 struct nexthop
*nexthop
;
575 nexthop
= nhlfe
->nexthop
;
576 switch (nexthop
->type
)
578 case NEXTHOP_TYPE_IPV4
:
579 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
581 case NEXTHOP_TYPE_IPV6
:
582 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
592 * Check if NHLFE matches with search info passed.
595 nhlfe_nhop_match (zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
596 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
598 struct nexthop
*nhop
;
601 nhop
= nhlfe
->nexthop
;
605 if (nhop
->type
!= gtype
)
610 case NEXTHOP_TYPE_IPV4
:
611 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
612 sizeof(struct in_addr
));
614 case NEXTHOP_TYPE_IPV6
:
615 case NEXTHOP_TYPE_IPV6_IFINDEX
:
616 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
617 sizeof(struct in6_addr
));
618 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
619 cmp
= !(nhop
->ifindex
== ifindex
);
630 * Locate NHLFE that matches with passed info.
632 static zebra_nhlfe_t
*
633 nhlfe_find (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
634 enum nexthop_types_t gtype
, union g_addr
*gate
,
635 char *ifname
, ifindex_t ifindex
)
637 zebra_nhlfe_t
*nhlfe
;
642 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
644 if (nhlfe
->type
!= lsp_type
)
646 if (!nhlfe_nhop_match (nhlfe
, gtype
, gate
, ifname
, ifindex
))
654 * Add NHLFE. Base entry must have been created and duplicate
657 static zebra_nhlfe_t
*
658 nhlfe_add (zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
659 enum nexthop_types_t gtype
, union g_addr
*gate
,
660 char *ifname
, ifindex_t ifindex
, mpls_label_t out_label
)
662 zebra_nhlfe_t
*nhlfe
;
663 struct nexthop
*nexthop
;
668 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
673 nhlfe
->type
= lsp_type
;
674 nhlfe
->distance
= lsp_distance (lsp_type
);
676 nexthop
= nexthop_new();
679 XFREE (MTYPE_NHLFE
, nhlfe
);
682 nexthop_add_labels (nexthop
, lsp_type
, 1, &out_label
);
684 nexthop
->type
= gtype
;
685 switch (nexthop
->type
)
687 case NEXTHOP_TYPE_IPV4
:
688 nexthop
->gate
.ipv4
= gate
->ipv4
;
690 case NEXTHOP_TYPE_IPV6
:
691 case NEXTHOP_TYPE_IPV6_IFINDEX
:
692 nexthop
->gate
.ipv6
= gate
->ipv6
;
694 nexthop
->ifindex
= ifindex
;
697 nexthop_free(nexthop
);
698 XFREE (MTYPE_NHLFE
, nhlfe
);
703 nhlfe
->nexthop
= nexthop
;
705 lsp
->nhlfe_list
->prev
= nhlfe
;
706 nhlfe
->next
= lsp
->nhlfe_list
;
707 lsp
->nhlfe_list
= nhlfe
;
713 * Delete NHLFE. Entry must be present on list.
716 nhlfe_del (zebra_nhlfe_t
*nhlfe
)
729 nexthop_free(nhlfe
->nexthop
);
731 /* Unlink from LSP */
733 nhlfe
->next
->prev
= nhlfe
->prev
;
735 nhlfe
->prev
->next
= nhlfe
->next
;
737 lsp
->nhlfe_list
= nhlfe
->next
;
739 XFREE (MTYPE_NHLFE
, nhlfe
);
745 mpls_lsp_uninstall_all (struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
746 enum lsp_types_t type
)
748 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
749 int schedule_lsp
= 0;
752 /* Mark NHLFEs for delete or directly delete, as appropriate. */
753 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
)
755 nhlfe_next
= nhlfe
->next
;
757 /* Skip non-static NHLFEs */
758 if (nhlfe
->type
!= type
)
761 if (IS_ZEBRA_DEBUG_MPLS
)
763 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
764 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
765 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
768 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
770 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
771 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
780 /* Queue LSP for processing, if needed, else delete. */
783 if (lsp_processq_add (lsp
))
786 else if (!lsp
->nhlfe_list
&&
787 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
789 if (IS_ZEBRA_DEBUG_MPLS
)
790 zlog_debug ("Free LSP in-label %u flags 0x%x",
791 lsp
->ile
.in_label
, lsp
->flags
);
793 lsp
= hash_release(lsp_table
, &lsp
->ile
);
795 XFREE(MTYPE_LSP
, lsp
);
802 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
803 * If no other NHLFEs exist, the entry would be deleted.
806 mpls_static_lsp_uninstall_all (struct zebra_vrf
*zvrf
, mpls_label_t in_label
)
808 struct hash
*lsp_table
;
813 lsp_table
= zvrf
->lsp_table
;
817 /* If entry is not present, exit. */
818 tmp_ile
.in_label
= in_label
;
819 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
820 if (!lsp
|| !lsp
->nhlfe_list
)
823 return mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
827 nhlfe_json (zebra_nhlfe_t
*nhlfe
)
830 json_object
*json_nhlfe
= NULL
;
831 struct nexthop
*nexthop
= nhlfe
->nexthop
;
833 json_nhlfe
= json_object_new_object();
834 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
835 json_object_int_add(json_nhlfe
, "outLabel", nexthop
->nh_label
->label
[0]);
836 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
838 if (CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
839 json_object_boolean_true_add(json_nhlfe
, "installed");
841 switch (nexthop
->type
)
843 case NEXTHOP_TYPE_IPV4
:
844 json_object_string_add(json_nhlfe
, "nexthop",
845 inet_ntoa (nexthop
->gate
.ipv4
));
847 case NEXTHOP_TYPE_IPV6
:
848 case NEXTHOP_TYPE_IPV6_IFINDEX
:
849 json_object_string_add(json_nhlfe
, "nexthop",
850 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
852 if (nexthop
->ifindex
)
853 json_object_string_add(json_nhlfe
, "interface", ifindex2ifname (nexthop
->ifindex
));
862 * Print the NHLFE for a LSP forwarding entry.
865 nhlfe_print (zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
867 struct nexthop
*nexthop
;
870 nexthop
= nhlfe
->nexthop
;
871 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
874 vty_out(vty
, " type: %s remote label: %s distance: %d%s",
875 nhlfe_type2str(nhlfe
->type
),
876 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
877 nhlfe
->distance
, VTY_NEWLINE
);
878 switch (nexthop
->type
)
880 case NEXTHOP_TYPE_IPV4
:
881 vty_out (vty
, " via %s", inet_ntoa (nexthop
->gate
.ipv4
));
883 case NEXTHOP_TYPE_IPV6
:
884 case NEXTHOP_TYPE_IPV6_IFINDEX
:
885 vty_out (vty
, " via %s",
886 inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
887 if (nexthop
->ifindex
)
888 vty_out (vty
, " dev %s", ifindex2ifname (nexthop
->ifindex
));
893 vty_out(vty
, "%s", CHECK_FLAG (nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ?
894 " (installed)" : "");
895 vty_out(vty
, "%s", VTY_NEWLINE
);
899 * Print an LSP forwarding entry.
902 lsp_print (zebra_lsp_t
*lsp
, void *ctxt
)
904 zebra_nhlfe_t
*nhlfe
;
907 vty
= (struct vty
*) ctxt
;
909 vty_out(vty
, "Local label: %u%s%s",
911 CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)" : "",
914 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
915 nhlfe_print (nhlfe
, vty
);
919 * JSON objects for an LSP forwarding entry.
922 lsp_json (zebra_lsp_t
*lsp
)
924 zebra_nhlfe_t
*nhlfe
= NULL
;
925 json_object
*json
= json_object_new_object();
926 json_object
*json_nhlfe_list
= json_object_new_array();
928 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
930 if (CHECK_FLAG (lsp
->flags
, LSP_FLAG_INSTALLED
))
931 json_object_boolean_true_add(json
, "installed");
933 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
934 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
936 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
941 /* Return a sorted linked list of the hash contents */
943 hash_get_sorted_list (struct hash
*hash
, void *cmp
)
946 struct hash_backet
*hb
;
947 struct list
*sorted_list
= list_new();
949 sorted_list
->cmp
= (int (*)(void *, void *)) cmp
;
951 for (i
= 0; i
< hash
->size
; i
++)
952 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
953 listnode_add_sort(sorted_list
, hb
->data
);
959 * Compare two LSPs based on their label values.
962 lsp_cmp (zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
964 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
967 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
974 * Callback to allocate static LSP.
979 const zebra_ile_t
*ile
= p
;
982 slsp
= XCALLOC (MTYPE_SLSP
, sizeof(zebra_slsp_t
));
984 return ((void *)slsp
);
988 * Compare two static LSPs based on their label values.
991 slsp_cmp (zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
993 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
996 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1003 * Check if static NHLFE matches with search info passed.
1006 snhlfe_match (zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1007 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
1011 if (snhlfe
->gtype
!= gtype
)
1014 switch (snhlfe
->gtype
)
1016 case NEXTHOP_TYPE_IPV4
:
1017 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1018 sizeof(struct in_addr
));
1020 case NEXTHOP_TYPE_IPV6
:
1021 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1022 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1023 sizeof(struct in6_addr
));
1024 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1025 cmp
= !(snhlfe
->ifindex
== ifindex
);
1035 * Locate static NHLFE that matches with passed info.
1037 static zebra_snhlfe_t
*
1038 snhlfe_find (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
1039 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
1041 zebra_snhlfe_t
*snhlfe
;
1046 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
)
1048 if (!snhlfe_match (snhlfe
, gtype
, gate
, ifname
, ifindex
))
1057 * Add static NHLFE. Base LSP config entry must have been created
1058 * and duplicate check done.
1060 static zebra_snhlfe_t
*
1061 snhlfe_add (zebra_slsp_t
*slsp
, enum nexthop_types_t gtype
,
1062 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
,
1063 mpls_label_t out_label
)
1065 zebra_snhlfe_t
*snhlfe
;
1070 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1071 snhlfe
->slsp
= slsp
;
1072 snhlfe
->out_label
= out_label
;
1073 snhlfe
->gtype
= gtype
;
1076 case NEXTHOP_TYPE_IPV4
:
1077 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1079 case NEXTHOP_TYPE_IPV6
:
1080 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1081 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1083 snhlfe
->ifindex
= ifindex
;
1086 XFREE (MTYPE_SNHLFE
, snhlfe
);
1090 if (slsp
->snhlfe_list
)
1091 slsp
->snhlfe_list
->prev
= snhlfe
;
1092 snhlfe
->next
= slsp
->snhlfe_list
;
1093 slsp
->snhlfe_list
= snhlfe
;
1099 * Delete static NHLFE. Entry must be present on list.
1102 snhlfe_del (zebra_snhlfe_t
*snhlfe
)
1109 slsp
= snhlfe
->slsp
;
1114 snhlfe
->next
->prev
= snhlfe
->prev
;
1116 snhlfe
->prev
->next
= snhlfe
->next
;
1118 slsp
->snhlfe_list
= snhlfe
->next
;
1120 snhlfe
->prev
= snhlfe
->next
= NULL
;
1122 XFREE (MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1123 XFREE (MTYPE_SNHLFE
, snhlfe
);
1129 * Delete all static NHLFE entries for this LSP (in label).
1132 snhlfe_del_all (zebra_slsp_t
*slsp
)
1134 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1139 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
)
1141 snhlfe_next
= snhlfe
->next
;
1142 snhlfe_del (snhlfe
);
1149 * Create printable string for NHLFE configuration.
1152 snhlfe2str (zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1155 switch (snhlfe
->gtype
)
1157 case NEXTHOP_TYPE_IPV4
:
1158 inet_ntop (AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1160 case NEXTHOP_TYPE_IPV6
:
1161 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1162 inet_ntop (AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1163 if (snhlfe
->ifindex
)
1164 strcat (buf
, ifindex2ifname (snhlfe
->ifindex
));
1174 * Initialize work queue for processing changed LSPs.
1177 mpls_processq_init (struct zebra_t
*zebra
)
1179 zebra
->lsp_process_q
= work_queue_new (zebra
->master
, "LSP processing");
1180 if (!zebra
->lsp_process_q
)
1182 zlog_err ("%s: could not initialise work queue!", __func__
);
1186 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1187 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1188 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1189 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1190 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1191 zebra
->lsp_process_q
->spec
.hold
= 10;
1196 /* Public functions */
1199 * String to label conversion, labels separated by '/'.
1202 mpls_str2label (const char *label_str
, u_int8_t
*num_labels
,
1203 mpls_label_t
*labels
)
1209 for (i
= 0; i
< MPLS_MAX_LABELS
; i
++)
1213 label
= strtoul(label_str
, &endp
, 0);
1215 /* validity checks */
1216 if (endp
== label_str
)
1219 if (!IS_MPLS_UNRESERVED_LABEL(label
))
1225 *num_labels
= i
+ 1;
1229 /* Check separator. */
1233 label_str
= endp
+ 1;
1236 /* Too many labels. */
1241 * Label to string conversion, labels in string separated by '/'.
1244 mpls_label2str (u_int8_t num_labels
, mpls_label_t
*labels
,
1248 if (num_labels
== 1)
1249 snprintf (buf
, len
, "%u", labels
[0]);
1250 else if (num_labels
== 2)
1251 snprintf (buf
, len
, "%u/%u", labels
[0], labels
[1]);
1256 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
1259 mpls_ftn_update (int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
1260 struct prefix
*prefix
, union g_addr
*gate
, u_int8_t distance
,
1261 mpls_label_t out_label
)
1263 struct route_table
*table
;
1264 struct route_node
*rn
;
1266 struct nexthop
*nexthop
;
1269 table
= zebra_vrf_table (family2afi(prefix
->family
), SAFI_UNICAST
, zvrf
->vrf_id
);
1273 /* Lookup existing route */
1274 rn
= route_node_get (table
, prefix
);
1275 RNODE_FOREACH_RIB (rn
, rib
)
1277 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1279 if (rib
->distance
== distance
)
1286 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1287 switch (prefix
->family
)
1290 if (nexthop
->type
!= NEXTHOP_TYPE_IPV4
&&
1291 nexthop
->type
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
1293 if (! IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &gate
->ipv4
))
1298 if (nexthop
->type
!= NEXTHOP_TYPE_IPV6
&&
1299 nexthop
->type
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
1301 if (! IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &gate
->ipv6
))
1308 /* nexthop not found */
1313 nexthop_add_labels (nexthop
, type
, 1, &out_label
);
1315 nexthop_del_labels (nexthop
);
1317 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1318 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1325 * Install/update a NHLFE for an LSP in the forwarding table. This may be
1326 * a new LSP entry or a new NHLFE for an existing in-label or an update of
1327 * the out-label for an existing NHLFE (update case).
1330 mpls_lsp_install (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
1331 mpls_label_t in_label
, mpls_label_t out_label
,
1332 enum nexthop_types_t gtype
, union g_addr
*gate
,
1333 char *ifname
, ifindex_t ifindex
)
1335 struct hash
*lsp_table
;
1336 zebra_ile_t tmp_ile
;
1338 zebra_nhlfe_t
*nhlfe
;
1342 lsp_table
= zvrf
->lsp_table
;
1346 /* If entry is present, exit. */
1347 tmp_ile
.in_label
= in_label
;
1348 lsp
= hash_get (lsp_table
, &tmp_ile
, lsp_alloc
);
1351 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifname
, ifindex
);
1354 struct nexthop
*nh
= nhlfe
->nexthop
;
1357 assert (nh
->nh_label
);
1359 /* Clear deleted flag (in case it was set) */
1360 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1361 if (nh
->nh_label
->label
[0] == out_label
)
1365 if (IS_ZEBRA_DEBUG_MPLS
)
1367 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
1368 zlog_debug ("LSP in-label %u type %d nexthop %s "
1369 "out-label changed to %u (old %u)",
1370 in_label
, type
, buf
,
1371 out_label
, nh
->nh_label
->label
[0]);
1374 /* Update out label, trigger processing. */
1375 nh
->nh_label
->label
[0] = out_label
;
1379 /* Add LSP entry to this nexthop */
1380 nhlfe
= nhlfe_add (lsp
, type
, gtype
, gate
,
1381 ifname
, ifindex
, out_label
);
1385 if (IS_ZEBRA_DEBUG_MPLS
)
1387 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
1388 zlog_debug ("Add LSP in-label %u type %d nexthop %s "
1389 "out-label %u", in_label
, type
, buf
, out_label
);
1392 lsp
->addr_family
= NHLFE_FAMILY (nhlfe
);
1395 /* Mark NHLFE, queue LSP for processing. */
1396 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1397 if (lsp_processq_add (lsp
))
1404 * Uninstall a particular NHLFE in the forwarding table. If this is
1405 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
1408 mpls_lsp_uninstall (struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
1409 mpls_label_t in_label
, enum nexthop_types_t gtype
,
1410 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
1412 struct hash
*lsp_table
;
1413 zebra_ile_t tmp_ile
;
1415 zebra_nhlfe_t
*nhlfe
;
1419 lsp_table
= zvrf
->lsp_table
;
1423 /* If entry is not present, exit. */
1424 tmp_ile
.in_label
= in_label
;
1425 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
1428 nhlfe
= nhlfe_find (lsp
, type
, gtype
, gate
, ifname
, ifindex
);
1432 if (IS_ZEBRA_DEBUG_MPLS
)
1434 nhlfe2str (nhlfe
, buf
, BUFSIZ
);
1435 zlog_debug ("Del LSP in-label %u type %d nexthop %s flags 0x%x",
1436 in_label
, type
, buf
, nhlfe
->flags
);
1439 /* Mark NHLFE for delete or directly delete, as appropriate. */
1440 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1442 UNSET_FLAG (nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1443 SET_FLAG (nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1444 if (lsp_processq_add (lsp
))
1451 /* Free LSP entry if no other NHLFEs and not scheduled. */
1452 if (!lsp
->nhlfe_list
&&
1453 !CHECK_FLAG (lsp
->flags
, LSP_FLAG_SCHEDULED
))
1455 if (IS_ZEBRA_DEBUG_MPLS
)
1456 zlog_debug ("Free LSP in-label %u flags 0x%x",
1457 lsp
->ile
.in_label
, lsp
->flags
);
1459 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1461 XFREE(MTYPE_LSP
, lsp
);
1468 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
1469 * If no other NHLFEs exist, the entry would be deleted.
1472 mpls_ldp_lsp_uninstall_all (struct hash_backet
*backet
, void *ctxt
)
1475 struct hash
*lsp_table
;
1477 lsp
= (zebra_lsp_t
*) backet
->data
;
1478 if (!lsp
|| !lsp
->nhlfe_list
)
1485 mpls_lsp_uninstall_all (lsp_table
, lsp
, ZEBRA_LSP_LDP
);
1489 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
1492 mpls_ldp_ftn_uninstall_all (struct zebra_vrf
*zvrf
, int afi
)
1494 struct route_table
*table
;
1495 struct route_node
*rn
;
1497 struct nexthop
*nexthop
;
1500 /* Process routes of interested address-families. */
1501 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, zvrf
->vrf_id
);
1505 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
1508 RNODE_FOREACH_RIB (rn
, rib
)
1509 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1510 if (nexthop
->nh_label_type
== ZEBRA_LSP_LDP
)
1512 nexthop_del_labels (nexthop
);
1513 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1514 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1524 * Check that the label values used in LSP creation are consistent. The
1525 * main criteria is that if there is ECMP, the label operation must still
1526 * be consistent - i.e., all paths either do a swap or do PHP. This is due
1527 * to current HW restrictions.
1530 zebra_mpls_lsp_label_consistent (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
1531 mpls_label_t out_label
, enum nexthop_types_t gtype
,
1532 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
1534 struct hash
*slsp_table
;
1535 zebra_ile_t tmp_ile
;
1537 zebra_snhlfe_t
*snhlfe
;
1540 slsp_table
= zvrf
->slsp_table
;
1544 /* If entry is not present, exit. */
1545 tmp_ile
.in_label
= in_label
;
1546 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
1550 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifname
, ifindex
);
1553 if (snhlfe
->out_label
== out_label
)
1556 /* If not only NHLFE, cannot allow label change. */
1557 if (snhlfe
!= slsp
->snhlfe_list
||
1563 /* If other NHLFEs exist, label operation must match. */
1564 if (slsp
->snhlfe_list
)
1568 cur_op
= (slsp
->snhlfe_list
->out_label
== MPLS_IMP_NULL_LABEL
);
1569 new_op
= (out_label
== MPLS_IMP_NULL_LABEL
);
1570 if (cur_op
!= new_op
)
1575 /* Label values are good. */
1581 * Add static LSP entry. This may be the first entry for this incoming label
1582 * or an additional nexthop; an existing entry may also have outgoing label
1584 * Note: The label operation (swap or PHP) is common for the LSP entry (all
1588 zebra_mpls_static_lsp_add (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
1589 mpls_label_t out_label
, enum nexthop_types_t gtype
,
1590 union g_addr
*gate
, char *ifname
, ifindex_t ifindex
)
1592 struct hash
*slsp_table
;
1593 zebra_ile_t tmp_ile
;
1595 zebra_snhlfe_t
*snhlfe
;
1599 slsp_table
= zvrf
->slsp_table
;
1603 /* If entry is present, exit. */
1604 tmp_ile
.in_label
= in_label
;
1605 slsp
= hash_get (slsp_table
, &tmp_ile
, slsp_alloc
);
1608 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifname
, ifindex
);
1611 if (snhlfe
->out_label
== out_label
)
1615 if (IS_ZEBRA_DEBUG_MPLS
)
1617 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
1618 zlog_debug ("Upd static LSP in-label %u nexthop %s "
1619 "out-label %u (old %u)",
1620 in_label
, buf
, out_label
, snhlfe
->out_label
);
1622 snhlfe
->out_label
= out_label
;
1626 /* Add static LSP entry to this nexthop */
1627 snhlfe
= snhlfe_add (slsp
, gtype
, gate
, ifname
, ifindex
, out_label
);
1631 if (IS_ZEBRA_DEBUG_MPLS
)
1633 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
1634 zlog_debug ("Add static LSP in-label %u nexthop %s out-label %u",
1635 in_label
, buf
, out_label
);
1639 /* (Re)Install LSP in the main table. */
1640 if (mpls_lsp_install (zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
1641 gate
, ifname
, ifindex
))
1648 * Delete static LSP entry. This may be the delete of one particular
1649 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
1651 * NOTE: Delete of the only NHLFE will also end up deleting the entire
1652 * LSP configuration.
1655 zebra_mpls_static_lsp_del (struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
1656 enum nexthop_types_t gtype
, union g_addr
*gate
,
1657 char *ifname
, ifindex_t ifindex
)
1659 struct hash
*slsp_table
;
1660 zebra_ile_t tmp_ile
;
1662 zebra_snhlfe_t
*snhlfe
;
1665 slsp_table
= zvrf
->slsp_table
;
1669 /* If entry is not present, exit. */
1670 tmp_ile
.in_label
= in_label
;
1671 slsp
= hash_lookup (slsp_table
, &tmp_ile
);
1675 /* Is it delete of entire LSP or a specific NHLFE? */
1676 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
)
1678 if (IS_ZEBRA_DEBUG_MPLS
)
1679 zlog_debug ("Del static LSP in-label %u", in_label
);
1681 /* Uninstall entire LSP from the main table. */
1682 mpls_static_lsp_uninstall_all (zvrf
, in_label
);
1684 /* Delete all static NHLFEs */
1685 snhlfe_del_all (slsp
);
1689 /* Find specific NHLFE, exit if not found. */
1690 snhlfe
= snhlfe_find (slsp
, gtype
, gate
, ifname
, ifindex
);
1694 if (IS_ZEBRA_DEBUG_MPLS
)
1697 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
1698 zlog_debug ("Del static LSP in-label %u nexthop %s",
1702 /* Uninstall LSP from the main table. */
1703 mpls_lsp_uninstall (zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
, gate
,
1706 /* Delete static LSP NHLFE */
1707 snhlfe_del (snhlfe
);
1710 /* Remove entire static LSP entry if no NHLFE - valid in either case above. */
1711 if (!slsp
->snhlfe_list
)
1713 slsp
= hash_release(slsp_table
, &tmp_ile
);
1715 XFREE(MTYPE_SLSP
, slsp
);
1722 * Schedule all MPLS label forwarding entries for processing.
1723 * Called upon changes that may affect one or more of them such as
1724 * interface or nexthop state changes.
1727 zebra_mpls_lsp_schedule (struct zebra_vrf
*zvrf
)
1731 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
1735 * Display MPLS label forwarding table for a specific LSP
1736 * (VTY command handler).
1739 zebra_mpls_print_lsp (struct vty
*vty
, struct zebra_vrf
*zvrf
, mpls_label_t label
,
1742 struct hash
*lsp_table
;
1744 zebra_ile_t tmp_ile
;
1745 json_object
*json
= NULL
;
1748 lsp_table
= zvrf
->lsp_table
;
1752 /* If entry is not present, exit. */
1753 tmp_ile
.in_label
= label
;
1754 lsp
= hash_lookup (lsp_table
, &tmp_ile
);
1760 json
= lsp_json(lsp
);
1761 vty_out (vty
, "%s%s", json_object_to_json_string(json
), VTY_NEWLINE
);
1762 json_object_free(json
);
1765 lsp_print (lsp
, (void *)vty
);
1769 * Display MPLS label forwarding table (VTY command handler).
1772 zebra_mpls_print_lsp_table (struct vty
*vty
, struct zebra_vrf
*zvrf
,
1776 json_object
*json
= NULL
;
1777 zebra_lsp_t
*lsp
= NULL
;
1778 zebra_nhlfe_t
*nhlfe
= NULL
;
1779 struct nexthop
*nexthop
= NULL
;
1780 struct listnode
*node
= NULL
;
1781 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
1785 json
= json_object_new_object();
1787 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
1788 json_object_object_add(json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
1791 vty_out (vty
, "%s%s", json_object_to_json_string(json
), VTY_NEWLINE
);
1792 json_object_free(json
);
1796 vty_out (vty
, " Inbound Outbound%s", VTY_NEWLINE
);
1797 vty_out (vty
, " Label Type Nexthop Label%s", VTY_NEWLINE
);
1798 vty_out (vty
, "-------- ------- --------------- --------%s", VTY_NEWLINE
);
1800 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
1802 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1804 vty_out (vty
, "%8d %7s ", lsp
->ile
.in_label
, nhlfe_type2str(nhlfe
->type
));
1805 nexthop
= nhlfe
->nexthop
;
1807 switch (nexthop
->type
)
1809 case NEXTHOP_TYPE_IPV4
:
1810 vty_out (vty
, "%15s", inet_ntoa (nexthop
->gate
.ipv4
));
1812 case NEXTHOP_TYPE_IPV6
:
1813 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1814 vty_out (vty
, "%15s", inet_ntop (AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1820 vty_out (vty
, " %8d%s", nexthop
->nh_label
->label
[0], VTY_NEWLINE
);
1824 vty_out (vty
, "%s", VTY_NEWLINE
);
1827 list_delete_all_node(lsp_list
);
1831 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
1834 zebra_mpls_write_lsp_config (struct vty
*vty
, struct zebra_vrf
*zvrf
)
1837 zebra_snhlfe_t
*snhlfe
;
1838 struct listnode
*node
;
1839 char buf
[INET6_ADDRSTRLEN
];
1840 struct list
*slsp_list
= hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
1842 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
))
1844 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
)
1847 snhlfe2str (snhlfe
, buf
, BUFSIZ
);
1848 vty_out (vty
, "mpls lsp %u %s %s%s",
1849 slsp
->ile
.in_label
, buf
,
1850 label2str(snhlfe
->out_label
, lstr
, 30), VTY_NEWLINE
);
1854 list_delete_all_node(slsp_list
);
1855 return (zvrf
->slsp_table
->count
? 1 : 0);
1859 * Called upon process exiting, need to delete LSP forwarding
1860 * entries from the kernel.
1861 * NOTE: Currently supported only for default VRF.
1864 zebra_mpls_close_tables (struct zebra_vrf
*zvrf
)
1868 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
1872 * Allocate MPLS tables for this VRF and do other initialization.
1873 * NOTE: Currently supported only for default VRF.
1876 zebra_mpls_init_tables (struct zebra_vrf
*zvrf
)
1880 zvrf
->slsp_table
= hash_create(label_hash
, label_cmp
);
1881 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
);
1882 zvrf
->mpls_flags
= 0;
1886 * Global MPLS initialization.
1889 zebra_mpls_init (void)
1891 mpls_kernel_init ();
1892 mpls_processq_init (&zebrad
);