2 * Copyright (C) 2013 Cumulus Networks, Inc.
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
32 #include "workqueue.h"
39 #include "zebra/rib.h"
41 #include "zebra/zserv.h"
42 #include "zebra/redistribute.h"
43 #include "zebra/debug.h"
44 #include "zebra/zebra_memory.h"
45 #include "zebra/zebra_vrf.h"
46 #include "zebra/zebra_mpls.h"
48 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
49 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
50 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
51 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
52 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
57 /* Default rtm_table for all clients */
58 extern struct zebra_t zebrad
;
60 /* static function declarations */
62 static void fec_evaluate(struct zebra_vrf
*zvrf
);
63 static u_int32_t
fec_derive_label_from_index(struct zebra_vrf
*vrf
,
65 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
66 struct route_node
*rn
, struct route_entry
*re
);
67 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
68 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
69 mpls_label_t old_label
);
70 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
71 static void fec_update_clients(zebra_fec_t
*fec
);
72 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
73 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
74 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
75 mpls_label_t label
, u_int32_t flags
,
76 u_int32_t label_index
);
77 static int fec_del(zebra_fec_t
*fec
);
79 static unsigned int label_hash(void *p
);
80 static int label_cmp(const void *p1
, const void *p2
);
81 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
82 struct nexthop
*nexthop
);
83 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
84 struct nexthop
*nexthop
);
85 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
87 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
88 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
);
89 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
);
90 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
91 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
92 static void lsp_processq_complete(struct work_queue
*wq
);
93 static int lsp_processq_add(zebra_lsp_t
*lsp
);
94 static void *lsp_alloc(void *p
);
96 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
97 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
98 union g_addr
*gate
, ifindex_t ifindex
);
99 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
100 enum nexthop_types_t gtype
, union g_addr
*gate
,
102 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
103 enum nexthop_types_t gtype
, union g_addr
*gate
,
104 ifindex_t ifindex
, mpls_label_t out_label
);
105 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
106 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
107 struct nexthop_label
*nh_label
);
108 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
109 enum lsp_types_t type
);
110 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
111 mpls_label_t in_label
);
112 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
113 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
114 static void *slsp_alloc(void *p
);
115 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
116 union g_addr
*gate
, ifindex_t ifindex
);
117 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
118 enum nexthop_types_t gtype
,
119 union g_addr
*gate
, ifindex_t ifindex
);
120 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
121 enum nexthop_types_t gtype
,
122 union g_addr
*gate
, ifindex_t ifindex
,
123 mpls_label_t out_label
);
124 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
125 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
126 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
127 static int mpls_processq_init(struct zebra_t
*zebra
);
130 /* Static functions */
133 * Install label forwarding entry based on labeled-route entry.
135 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
136 struct route_node
*rn
, struct route_entry
*re
)
138 struct hash
*lsp_table
;
141 zebra_nhlfe_t
*nhlfe
;
142 struct nexthop
*nexthop
;
143 enum lsp_types_t lsp_type
;
148 lsp_table
= zvrf
->lsp_table
;
152 lsp_type
= lsp_type_from_re_type(re
->type
);
155 /* Locate or allocate LSP entry. */
156 tmp_ile
.in_label
= label
;
157 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
161 /* For each active nexthop, create NHLFE. Note that we deliberately skip
162 * recursive nexthops right now, because intermediate hops won't
164 * the label advertised by the recursive nexthop (plus we don't have the
165 * logic yet to push multiple labels).
167 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
168 /* Skip inactive and recursive entries. */
169 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
171 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
174 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
177 /* Clear deleted flag (in case it was set) */
178 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
179 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
184 if (IS_ZEBRA_DEBUG_MPLS
) {
185 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
187 "LSP in-label %u type %d nexthop %s "
189 lsp
->ile
.in_label
, lsp_type
, buf
);
192 /* Update out label, trigger processing. */
193 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
194 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
197 /* Add LSP entry to this nexthop */
198 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
199 &nexthop
->gate
, nexthop
->ifindex
,
200 nexthop
->nh_label
->label
[0]);
204 if (IS_ZEBRA_DEBUG_MPLS
) {
205 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
207 "Add LSP in-label %u type %d nexthop %s "
209 lsp
->ile
.in_label
, lsp_type
, buf
,
210 nexthop
->nh_label
->label
[0]);
213 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
215 /* Mark NHLFE as changed. */
216 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
221 /* Queue LSP for processing if necessary. If no NHLFE got added (special
222 * case), delete the LSP entry; this case results in somewhat ugly
225 if (added
|| changed
) {
226 if (lsp_processq_add(lsp
))
228 } else if (!lsp
->nhlfe_list
229 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
230 if (IS_ZEBRA_DEBUG_MPLS
)
231 zlog_debug("Free LSP in-label %u flags 0x%x",
232 lsp
->ile
.in_label
, lsp
->flags
);
234 lsp
= hash_release(lsp_table
, &lsp
->ile
);
236 XFREE(MTYPE_LSP
, lsp
);
243 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
244 * NHLFEs are removed, the entire entry is deleted.
246 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
248 struct hash
*lsp_table
;
251 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
255 lsp_table
= zvrf
->lsp_table
;
259 /* If entry is not present, exit. */
260 tmp_ile
.in_label
= label
;
261 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
262 if (!lsp
|| !lsp
->nhlfe_list
)
265 /* Mark NHLFEs for delete or directly delete, as appropriate. */
266 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
267 nhlfe_next
= nhlfe
->next
;
269 /* Skip static NHLFEs */
270 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
273 if (IS_ZEBRA_DEBUG_MPLS
) {
274 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
276 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
277 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
280 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
281 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
282 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
288 /* Queue LSP for processing, if needed, else delete. */
289 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
290 if (lsp_processq_add(lsp
))
292 } else if (!lsp
->nhlfe_list
293 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
294 if (IS_ZEBRA_DEBUG_MPLS
)
295 zlog_debug("Del LSP in-label %u flags 0x%x",
296 lsp
->ile
.in_label
, lsp
->flags
);
298 lsp
= hash_release(lsp_table
, &lsp
->ile
);
300 XFREE(MTYPE_LSP
, lsp
);
307 * This function is invoked upon change to label block configuration; it
308 * will walk all registered FECs with label-index and appropriately update
309 * their local labels and trigger client updates.
311 static void fec_evaluate(struct zebra_vrf
*zvrf
)
313 struct route_node
*rn
;
315 u_int32_t old_label
, new_label
;
319 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
320 if (zvrf
->fec_table
[af
] == NULL
)
323 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
324 rn
= route_next(rn
)) {
325 if ((fec
= rn
->info
) == NULL
)
328 /* Skip configured FECs and those without a label index.
330 if (fec
->flags
& FEC_FLAG_CONFIGURED
331 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
334 if (IS_ZEBRA_DEBUG_MPLS
)
335 prefix2str(&rn
->p
, buf
, BUFSIZ
);
337 /* Save old label, determine new label. */
338 old_label
= fec
->label
;
340 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
341 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
342 new_label
= MPLS_INVALID_LABEL
;
344 /* If label has changed, update FEC and clients. */
345 if (new_label
== old_label
)
348 if (IS_ZEBRA_DEBUG_MPLS
)
350 "Update fec %s new label %u upon label block",
353 fec
->label
= new_label
;
354 fec_update_clients(fec
);
356 /* Update label forwarding entries appropriately */
357 fec_change_update_lsp(zvrf
, fec
, old_label
);
363 * Derive (if possible) and update the local label for the FEC based on
364 * its label index. The index is "acceptable" if it falls within the
365 * globally configured label block (SRGB).
367 static u_int32_t
fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
372 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
373 && zvrf
->mpls_srgb
.start_label
374 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
375 < zvrf
->mpls_srgb
.end_label
))
378 fec
->label
= MPLS_INVALID_LABEL
;
384 * There is a change for this FEC. Install or uninstall label forwarding
385 * entries, as appropriate.
387 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
388 mpls_label_t old_label
)
390 struct route_table
*table
;
391 struct route_node
*rn
;
392 struct route_entry
*re
;
395 /* Uninstall label forwarding entry, if previously installed. */
396 if (old_label
!= MPLS_INVALID_LABEL
&& old_label
!= MPLS_IMP_NULL_LABEL
)
397 lsp_uninstall(zvrf
, old_label
);
399 /* Install label forwarding entry corr. to new label, if needed. */
400 if (fec
->label
== MPLS_INVALID_LABEL
401 || fec
->label
== MPLS_IMP_NULL_LABEL
)
404 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
405 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
409 /* See if labeled route exists. */
410 rn
= route_node_lookup(table
, &fec
->rn
->p
);
414 RNODE_FOREACH_RE(rn
, re
)
416 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
420 if (!re
|| !zebra_rib_labeled_unicast(re
))
423 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
430 * Inform about FEC to a registered client.
432 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
435 struct route_node
*rn
;
439 /* Get output stream. */
443 zserv_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
445 stream_putw(s
, rn
->p
.family
);
446 stream_put_prefix(s
, &rn
->p
);
447 stream_putl(s
, fec
->label
);
448 stream_putw_at(s
, 0, stream_get_endp(s
));
449 return zebra_server_send_message(client
);
453 * Update all registered clients about this FEC. Caller should've updated
454 * FEC and ensure no duplicate updates.
456 static void fec_update_clients(zebra_fec_t
*fec
)
458 struct listnode
*node
;
459 struct zserv
*client
;
461 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
462 if (IS_ZEBRA_DEBUG_MPLS
)
463 zlog_debug("Update client %s",
464 zebra_route_string(client
->proto
));
465 fec_send(fec
, client
);
471 * Print a FEC-label binding entry.
473 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
475 struct route_node
*rn
;
476 struct listnode
*node
;
477 struct zserv
*client
;
481 prefix2str(&rn
->p
, buf
, BUFSIZ
);
482 vty_out(vty
, "%s\n", buf
);
483 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
484 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
485 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
487 if (!list_isempty(fec
->client_list
)) {
488 vty_out(vty
, " Client list:");
489 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
490 vty_out(vty
, " %s(fd %d)",
491 zebra_route_string(client
->proto
),
498 * Locate FEC-label binding that matches with passed info.
500 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
502 struct route_node
*rn
;
505 rn
= route_node_lookup(table
, p
);
509 route_unlock_node(rn
);
514 * Add a FEC. This may be upon a client registering for a binding
515 * or when a binding is configured.
517 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
518 mpls_label_t label
, u_int32_t flags
,
519 u_int32_t label_index
)
521 struct route_node
*rn
;
526 /* Lookup (or add) route node.*/
527 rn
= route_node_get(table
, p
);
534 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
541 fec
->client_list
= list_new();
543 route_unlock_node(rn
); /* for the route_node_get */
545 fec
->label_index
= label_index
;
552 * Delete a FEC. This may be upon the last client deregistering for
553 * a FEC and no binding exists or when the binding is deleted and there
554 * are no registered clients.
556 static int fec_del(zebra_fec_t
*fec
)
558 list_free(fec
->client_list
);
559 fec
->rn
->info
= NULL
;
560 route_unlock_node(fec
->rn
);
561 XFREE(MTYPE_FEC
, fec
);
566 * Hash function for label.
568 static unsigned int label_hash(void *p
)
570 const zebra_ile_t
*ile
= p
;
572 return (jhash_1word(ile
->in_label
, 0));
576 * Compare 2 LSP hash entries based on in-label.
578 static int label_cmp(const void *p1
, const void *p2
)
580 const zebra_ile_t
*ile1
= p1
;
581 const zebra_ile_t
*ile2
= p2
;
583 return (ile1
->in_label
== ile2
->in_label
);
587 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
589 * NOTE: Looking only for connected routes right now.
591 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
592 struct nexthop
*nexthop
)
594 struct route_table
*table
;
595 struct prefix_ipv4 p
;
596 struct route_node
*rn
;
597 struct route_entry
*match
;
598 struct nexthop
*match_nh
;
600 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
);
604 /* Lookup nexthop in IPv4 routing table. */
605 memset(&p
, 0, sizeof(struct prefix_ipv4
));
607 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
608 p
.prefix
= nexthop
->gate
.ipv4
;
610 rn
= route_node_match(table
, (struct prefix
*)&p
);
614 route_unlock_node(rn
);
616 /* Locate a valid connected route. */
617 RNODE_FOREACH_RE(rn
, match
)
619 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
620 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
623 for (match_nh
= match
->nexthop
; match_nh
;
624 match_nh
= match_nh
->next
) {
625 if (match
->type
== ZEBRA_ROUTE_CONNECT
626 || nexthop
->ifindex
== match_nh
->ifindex
) {
627 nexthop
->ifindex
= match_nh
->ifindex
;
638 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
640 * NOTE: Looking only for connected routes right now.
642 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
643 struct nexthop
*nexthop
)
645 struct route_table
*table
;
646 struct prefix_ipv6 p
;
647 struct route_node
*rn
;
648 struct route_entry
*match
;
650 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, VRF_DEFAULT
);
654 /* Lookup nexthop in IPv6 routing table. */
655 memset(&p
, 0, sizeof(struct prefix_ipv6
));
657 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
658 p
.prefix
= nexthop
->gate
.ipv6
;
660 rn
= route_node_match(table
, (struct prefix
*)&p
);
664 route_unlock_node(rn
);
666 /* Locate a valid connected route. */
667 RNODE_FOREACH_RE(rn
, match
)
669 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
670 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
671 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
675 if (!match
|| !match
->nexthop
)
678 nexthop
->ifindex
= match
->nexthop
->ifindex
;
684 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
686 * NOTE: Each NHLFE points to only 1 nexthop.
688 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
690 struct nexthop
*nexthop
;
691 struct interface
*ifp
;
693 nexthop
= nhlfe
->nexthop
;
694 if (!nexthop
) // unexpected
697 /* Check on nexthop based on type. */
698 switch (nexthop
->type
) {
699 case NEXTHOP_TYPE_IPV4
:
700 case NEXTHOP_TYPE_IPV4_IFINDEX
:
701 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
702 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
704 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
707 case NEXTHOP_TYPE_IPV6
:
708 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
709 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
711 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
714 case NEXTHOP_TYPE_IPV6_IFINDEX
:
715 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
716 ifp
= if_lookup_by_index(nexthop
->ifindex
, VRF_DEFAULT
);
717 if (ifp
&& if_is_operative(ifp
))
718 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
720 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
722 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
723 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
725 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
738 * reachability and select the best. Multipath entries are also
739 * marked. This is invoked when an LSP scheduled for processing (due
740 * to some change) is examined.
742 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
744 zebra_nhlfe_t
*nhlfe
;
746 struct nexthop
*nexthop
;
754 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
757 * First compute the best path, after checking nexthop status. We are
759 * concerned with non-deleted NHLFEs.
761 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
762 /* Clear selection flags. */
763 UNSET_FLAG(nhlfe
->flags
,
764 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
766 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
767 && nhlfe_nexthop_active(nhlfe
)) {
768 if (!best
|| (nhlfe
->distance
< best
->distance
))
773 lsp
->best_nhlfe
= best
;
774 if (!lsp
->best_nhlfe
)
777 /* Mark best NHLFE as selected. */
778 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
781 * If best path exists, see if there is ECMP. While doing this, note if
783 * new (uninstalled) NHLFE has been selected, an installed entry that is
784 * still selected has a change or an installed entry is to be removed.
786 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
787 int nh_chg
, nh_sel
, nh_inst
;
789 nexthop
= nhlfe
->nexthop
;
790 if (!nexthop
) // unexpected
793 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
794 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
795 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
796 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
797 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
801 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
802 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
803 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
805 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
807 if ((nh_sel
&& !nh_inst
)
808 || (nh_sel
&& nh_inst
&& nh_chg
)
809 || (nh_inst
&& !nh_sel
))
813 /* We have finished examining, clear changed flag. */
814 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
818 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
822 * Delete LSP forwarding entry from kernel, if installed. Called upon
825 static void lsp_uninstall_from_kernel(struct hash_backet
*backet
, void *ctxt
)
829 lsp
= (zebra_lsp_t
*)backet
->data
;
830 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
835 * Schedule LSP forwarding entry for processing. Called upon changes
836 * that may impact LSPs such as nexthop / connected route changes.
838 static void lsp_schedule(struct hash_backet
*backet
, void *ctxt
)
842 lsp
= (zebra_lsp_t
*)backet
->data
;
843 lsp_processq_add(lsp
);
847 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
848 * any multipaths and update or delete from the kernel, as needed.
850 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
853 zebra_nhlfe_t
*oldbest
, *newbest
;
854 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
855 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
857 lsp
= (zebra_lsp_t
*)data
;
858 if (!lsp
) // unexpected
861 oldbest
= lsp
->best_nhlfe
;
863 /* Select best NHLFE(s) */
864 lsp_select_best_nhlfe(lsp
);
866 newbest
= lsp
->best_nhlfe
;
868 if (IS_ZEBRA_DEBUG_MPLS
) {
870 nhlfe2str(oldbest
, buf
, BUFSIZ
);
872 nhlfe2str(newbest
, buf2
, BUFSIZ
);
874 "Process LSP in-label %u oldbest %s newbest %s "
875 "flags 0x%x ecmp# %d",
876 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
877 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
880 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
881 /* Not already installed */
884 zvrf
->lsp_installs
++;
887 /* Installed, may need an update and/or delete. */
890 zvrf
->lsp_removals
++;
891 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
893 zvrf
->lsp_installs
++;
902 * Callback upon processing completion of a LSP forwarding entry.
904 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
906 struct zebra_vrf
*zvrf
;
908 struct hash
*lsp_table
;
909 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
911 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
914 lsp_table
= zvrf
->lsp_table
;
915 if (!lsp_table
) // unexpected
918 lsp
= (zebra_lsp_t
*)data
;
919 if (!lsp
) // unexpected
922 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
924 * delete LSP entry also.
926 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
928 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
929 nhlfe_next
= nhlfe
->next
;
930 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
934 if (!lsp
->nhlfe_list
) {
935 if (IS_ZEBRA_DEBUG_MPLS
)
936 zlog_debug("Free LSP in-label %u flags 0x%x",
937 lsp
->ile
.in_label
, lsp
->flags
);
939 lsp
= hash_release(lsp_table
, &lsp
->ile
);
941 XFREE(MTYPE_LSP
, lsp
);
946 * Callback upon finishing the processing of all scheduled
947 * LSP forwarding entries.
949 static void lsp_processq_complete(struct work_queue
*wq
)
951 /* Nothing to do for now. */
955 * Add LSP forwarding entry to queue for subsequent processing.
957 static int lsp_processq_add(zebra_lsp_t
*lsp
)
959 /* If already scheduled, exit. */
960 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
963 if (zebrad
.lsp_process_q
== NULL
) {
964 zlog_err("%s: work_queue does not exist!", __func__
);
968 work_queue_add(zebrad
.lsp_process_q
, lsp
);
969 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
974 * Callback to allocate LSP forwarding table entry.
976 static void *lsp_alloc(void *p
)
978 const zebra_ile_t
*ile
= p
;
981 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
984 if (IS_ZEBRA_DEBUG_MPLS
)
985 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
987 return ((void *)lsp
);
991 * Create printable string for NHLFE entry.
993 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
995 struct nexthop
*nexthop
;
998 nexthop
= nhlfe
->nexthop
;
999 switch (nexthop
->type
) {
1000 case NEXTHOP_TYPE_IPV4
:
1001 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1002 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1004 case NEXTHOP_TYPE_IPV6
:
1005 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1015 * Check if NHLFE matches with search info passed.
1017 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1018 union g_addr
*gate
, ifindex_t ifindex
)
1020 struct nexthop
*nhop
;
1023 nhop
= nhlfe
->nexthop
;
1027 if (nhop
->type
!= gtype
)
1030 switch (nhop
->type
) {
1031 case NEXTHOP_TYPE_IPV4
:
1032 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1033 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1034 sizeof(struct in_addr
));
1035 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1036 cmp
= !(nhop
->ifindex
== ifindex
);
1038 case NEXTHOP_TYPE_IPV6
:
1039 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1040 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1041 sizeof(struct in6_addr
));
1042 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1043 cmp
= !(nhop
->ifindex
== ifindex
);
1054 * Locate NHLFE that matches with passed info.
1056 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1057 enum nexthop_types_t gtype
, union g_addr
*gate
,
1060 zebra_nhlfe_t
*nhlfe
;
1065 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1066 if (nhlfe
->type
!= lsp_type
)
1068 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1076 * Add NHLFE. Base entry must have been created and duplicate
1079 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1080 enum nexthop_types_t gtype
, union g_addr
*gate
,
1081 ifindex_t ifindex
, mpls_label_t out_label
)
1083 zebra_nhlfe_t
*nhlfe
;
1084 struct nexthop
*nexthop
;
1089 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1094 nhlfe
->type
= lsp_type
;
1095 nhlfe
->distance
= lsp_distance(lsp_type
);
1097 nexthop
= nexthop_new();
1099 XFREE(MTYPE_NHLFE
, nhlfe
);
1102 nexthop_add_labels(nexthop
, lsp_type
, 1, &out_label
);
1104 nexthop
->type
= gtype
;
1105 switch (nexthop
->type
) {
1106 case NEXTHOP_TYPE_IPV4
:
1107 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1108 nexthop
->gate
.ipv4
= gate
->ipv4
;
1110 nexthop
->ifindex
= ifindex
;
1112 case NEXTHOP_TYPE_IPV6
:
1113 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1114 nexthop
->gate
.ipv6
= gate
->ipv6
;
1116 nexthop
->ifindex
= ifindex
;
1119 nexthop_free(nexthop
);
1120 XFREE(MTYPE_NHLFE
, nhlfe
);
1125 nhlfe
->nexthop
= nexthop
;
1126 if (lsp
->nhlfe_list
)
1127 lsp
->nhlfe_list
->prev
= nhlfe
;
1128 nhlfe
->next
= lsp
->nhlfe_list
;
1129 lsp
->nhlfe_list
= nhlfe
;
1135 * Delete NHLFE. Entry must be present on list.
1137 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1150 nexthop_free(nhlfe
->nexthop
);
1152 /* Unlink from LSP */
1154 nhlfe
->next
->prev
= nhlfe
->prev
;
1156 nhlfe
->prev
->next
= nhlfe
->next
;
1158 lsp
->nhlfe_list
= nhlfe
->next
;
1160 if (nhlfe
== lsp
->best_nhlfe
)
1161 lsp
->best_nhlfe
= NULL
;
1163 XFREE(MTYPE_NHLFE
, nhlfe
);
1169 * Update label for NHLFE entry.
1171 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1172 struct nexthop_label
*nh_label
)
1174 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1177 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1178 enum lsp_types_t type
)
1180 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1181 int schedule_lsp
= 0;
1184 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1185 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1186 nhlfe_next
= nhlfe
->next
;
1188 /* Skip non-static NHLFEs */
1189 if (nhlfe
->type
!= type
)
1192 if (IS_ZEBRA_DEBUG_MPLS
) {
1193 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1195 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1196 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1199 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1200 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1201 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1208 /* Queue LSP for processing, if needed, else delete. */
1210 if (lsp_processq_add(lsp
))
1212 } else if (!lsp
->nhlfe_list
1213 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1214 if (IS_ZEBRA_DEBUG_MPLS
)
1215 zlog_debug("Free LSP in-label %u flags 0x%x",
1216 lsp
->ile
.in_label
, lsp
->flags
);
1218 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1220 XFREE(MTYPE_LSP
, lsp
);
1227 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1228 * If no other NHLFEs exist, the entry would be deleted.
1230 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1231 mpls_label_t in_label
)
1233 struct hash
*lsp_table
;
1234 zebra_ile_t tmp_ile
;
1238 lsp_table
= zvrf
->lsp_table
;
1242 /* If entry is not present, exit. */
1243 tmp_ile
.in_label
= in_label
;
1244 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1245 if (!lsp
|| !lsp
->nhlfe_list
)
1248 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1251 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1254 json_object
*json_nhlfe
= NULL
;
1255 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1257 json_nhlfe
= json_object_new_object();
1258 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1259 json_object_int_add(json_nhlfe
, "outLabel",
1260 nexthop
->nh_label
->label
[0]);
1261 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1263 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1264 json_object_boolean_true_add(json_nhlfe
, "installed");
1266 switch (nexthop
->type
) {
1267 case NEXTHOP_TYPE_IPV4
:
1268 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1269 json_object_string_add(json_nhlfe
, "nexthop",
1270 inet_ntoa(nexthop
->gate
.ipv4
));
1272 case NEXTHOP_TYPE_IPV6
:
1273 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1274 json_object_string_add(
1275 json_nhlfe
, "nexthop",
1276 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1278 if (nexthop
->ifindex
)
1279 json_object_string_add(
1280 json_nhlfe
, "interface",
1281 ifindex2ifname(nexthop
->ifindex
, VRF_DEFAULT
));
1290 * Print the NHLFE for a LSP forwarding entry.
1292 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1294 struct nexthop
*nexthop
;
1297 nexthop
= nhlfe
->nexthop
;
1298 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1301 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1302 nhlfe_type2str(nhlfe
->type
),
1303 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1305 switch (nexthop
->type
) {
1306 case NEXTHOP_TYPE_IPV4
:
1307 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1308 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1309 if (nexthop
->ifindex
)
1310 vty_out(vty
, " dev %s",
1311 ifindex2ifname(nexthop
->ifindex
, VRF_DEFAULT
));
1313 case NEXTHOP_TYPE_IPV6
:
1314 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1315 vty_out(vty
, " via %s",
1316 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1317 if (nexthop
->ifindex
)
1318 vty_out(vty
, " dev %s",
1319 ifindex2ifname(nexthop
->ifindex
, VRF_DEFAULT
));
1324 vty_out(vty
, "%s", CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)
1331 * Print an LSP forwarding entry.
1333 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1335 zebra_nhlfe_t
*nhlfe
;
1338 vty
= (struct vty
*)ctxt
;
1340 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1341 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1344 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1345 nhlfe_print(nhlfe
, vty
);
1349 * JSON objects for an LSP forwarding entry.
1351 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1353 zebra_nhlfe_t
*nhlfe
= NULL
;
1354 json_object
*json
= json_object_new_object();
1355 json_object
*json_nhlfe_list
= json_object_new_array();
1357 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1359 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1360 json_object_boolean_true_add(json
, "installed");
1362 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1363 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1365 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1370 /* Return a sorted linked list of the hash contents */
1371 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1374 struct hash_backet
*hb
;
1375 struct list
*sorted_list
= list_new();
1377 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1379 for (i
= 0; i
< hash
->size
; i
++)
1380 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1381 listnode_add_sort(sorted_list
, hb
->data
);
1387 * Compare two LSPs based on their label values.
1389 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1391 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1394 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1401 * Callback to allocate static LSP.
1403 static void *slsp_alloc(void *p
)
1405 const zebra_ile_t
*ile
= p
;
1408 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1410 return ((void *)slsp
);
1414 * Compare two static LSPs based on their label values.
1416 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1418 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1421 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1428 * Check if static NHLFE matches with search info passed.
1430 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1431 union g_addr
*gate
, ifindex_t ifindex
)
1435 if (snhlfe
->gtype
!= gtype
)
1438 switch (snhlfe
->gtype
) {
1439 case NEXTHOP_TYPE_IPV4
:
1440 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1441 sizeof(struct in_addr
));
1443 case NEXTHOP_TYPE_IPV6
:
1444 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1445 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1446 sizeof(struct in6_addr
));
1447 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1448 cmp
= !(snhlfe
->ifindex
== ifindex
);
1458 * Locate static NHLFE that matches with passed info.
1460 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1461 enum nexthop_types_t gtype
,
1462 union g_addr
*gate
, ifindex_t ifindex
)
1464 zebra_snhlfe_t
*snhlfe
;
1469 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1470 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1479 * Add static NHLFE. Base LSP config entry must have been created
1480 * and duplicate check done.
1482 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1483 enum nexthop_types_t gtype
,
1484 union g_addr
*gate
, ifindex_t ifindex
,
1485 mpls_label_t out_label
)
1487 zebra_snhlfe_t
*snhlfe
;
1492 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1493 snhlfe
->slsp
= slsp
;
1494 snhlfe
->out_label
= out_label
;
1495 snhlfe
->gtype
= gtype
;
1497 case NEXTHOP_TYPE_IPV4
:
1498 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1500 case NEXTHOP_TYPE_IPV6
:
1501 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1502 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1504 snhlfe
->ifindex
= ifindex
;
1507 XFREE(MTYPE_SNHLFE
, snhlfe
);
1511 if (slsp
->snhlfe_list
)
1512 slsp
->snhlfe_list
->prev
= snhlfe
;
1513 snhlfe
->next
= slsp
->snhlfe_list
;
1514 slsp
->snhlfe_list
= snhlfe
;
1520 * Delete static NHLFE. Entry must be present on list.
1522 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1529 slsp
= snhlfe
->slsp
;
1534 snhlfe
->next
->prev
= snhlfe
->prev
;
1536 snhlfe
->prev
->next
= snhlfe
->next
;
1538 slsp
->snhlfe_list
= snhlfe
->next
;
1540 snhlfe
->prev
= snhlfe
->next
= NULL
;
1542 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1543 XFREE(MTYPE_SNHLFE
, snhlfe
);
1549 * Delete all static NHLFE entries for this LSP (in label).
1551 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1553 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1558 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1559 snhlfe_next
= snhlfe
->next
;
1567 * Create printable string for NHLFE configuration.
1569 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1572 switch (snhlfe
->gtype
) {
1573 case NEXTHOP_TYPE_IPV4
:
1574 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1576 case NEXTHOP_TYPE_IPV6
:
1577 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1578 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1579 if (snhlfe
->ifindex
)
1581 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
));
1591 * Initialize work queue for processing changed LSPs.
1593 static int mpls_processq_init(struct zebra_t
*zebra
)
1595 zebra
->lsp_process_q
= work_queue_new(zebra
->master
, "LSP processing");
1596 if (!zebra
->lsp_process_q
) {
1597 zlog_err("%s: could not initialise work queue!", __func__
);
1601 zebra
->lsp_process_q
->spec
.workfunc
= &lsp_process
;
1602 zebra
->lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1603 zebra
->lsp_process_q
->spec
.errorfunc
= NULL
;
1604 zebra
->lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1605 zebra
->lsp_process_q
->spec
.max_retries
= 0;
1606 zebra
->lsp_process_q
->spec
.hold
= 10;
1612 /* Public functions */
1615 * String to label conversion, labels separated by '/'.
1617 * @param label_str labels separated by /
1618 * @param num_labels number of labels; zero if conversion was unsuccessful
1619 * @param labels preallocated mpls_label_t array of size MPLS_MAX_LABELS; only
1620 * modified if the conversion succeeded
1621 * @return 0 on success
1622 * -1 if the string could not be parsed as integers
1623 * -2 if a label was inside the reserved range (0-15)
1624 * -3 if the number of labels given exceeds MPLS_MAX_LABELS
1626 int mpls_str2label(const char *label_str
, u_int8_t
*num_labels
,
1627 mpls_label_t
*labels
)
1629 char *ostr
; // copy of label string (start)
1630 char *lstr
; // copy of label string
1631 char *nump
; // pointer to next segment
1632 char *endp
; // end pointer
1633 int i
; // for iterating label_str
1634 int rc
; // return code
1635 mpls_label_t pl
[MPLS_MAX_LABELS
]; // parsed labels
1637 /* labels to zero until we have a successful parse */
1638 ostr
= lstr
= XSTRDUP(MTYPE_TMP
, label_str
);
1642 for (i
= 0; i
< MPLS_MAX_LABELS
&& lstr
&& !rc
; i
++) {
1643 nump
= strsep(&lstr
, "/");
1644 pl
[i
] = strtoul(nump
, &endp
, 10);
1649 /* validity check */
1650 else if (!IS_MPLS_UNRESERVED_LABEL(pl
[i
]))
1655 if (!rc
&& i
== MPLS_MAX_LABELS
&& lstr
)
1660 memcpy(labels
, pl
, *num_labels
* sizeof(mpls_label_t
));
1663 XFREE(MTYPE_TMP
, ostr
);
1669 * Label to string conversion, labels in string separated by '/'.
1671 char *mpls_label2str(u_int8_t num_labels
, mpls_label_t
*labels
, char *buf
,
1672 int len
, int pretty
)
1674 char *buf_ptr
= buf
;
1678 if (num_labels
== 1) {
1679 label2str(labels
[0], buf
, len
);
1680 } else if (num_labels
== 2) {
1681 label2str(labels
[0], buf
, len
);
1682 buf_ptr
+= strlen(buf
);
1684 snprintf(buf_ptr
, len
, "/");
1687 label2str(labels
[1], buf_ptr
, len
);
1690 if (num_labels
== 1)
1691 snprintf(buf
, len
, "%u", labels
[0]);
1692 else if (num_labels
== 2)
1693 snprintf(buf
, len
, "%u/%u", labels
[0], labels
[1]);
1699 * Install dynamic LSP entry.
1701 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1702 struct route_entry
*re
)
1704 struct route_table
*table
;
1707 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1711 /* See if there is a configured label binding for this FEC. */
1712 fec
= fec_find(table
, &rn
->p
);
1713 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1716 /* We cannot install a label forwarding entry if local label is the
1717 * implicit-null label.
1719 if (fec
->label
== MPLS_IMP_NULL_LABEL
)
1722 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1729 * Uninstall dynamic LSP entry, if any.
1731 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1732 struct route_entry
*re
)
1734 struct route_table
*table
;
1737 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1741 /* See if there is a configured label binding for this FEC. */
1742 fec
= fec_find(table
, &rn
->p
);
1743 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1746 /* Uninstall always removes all dynamic NHLFEs. */
1747 return lsp_uninstall(zvrf
, fec
->label
);
1751 * Registration from a client for the label binding for a FEC. If a binding
1752 * already exists, it is informed to the client.
1753 * NOTE: If there is a manually configured label binding, that is used.
1754 * Otherwise, if a label index is specified, it means we have to allocate the
1755 * label from a locally configured label block (SRGB), if one exists and index
1758 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1759 u_int32_t label_index
, struct zserv
*client
)
1761 struct route_table
*table
;
1765 int label_change
= 0;
1766 u_int32_t old_label
;
1768 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1772 if (IS_ZEBRA_DEBUG_MPLS
)
1773 prefix2str(p
, buf
, BUFSIZ
);
1776 fec
= fec_find(table
, p
);
1778 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1780 prefix2str(p
, buf
, BUFSIZ
);
1782 "Failed to add FEC %s upon register, client %s",
1783 buf
, zebra_route_string(client
->proto
));
1787 old_label
= MPLS_INVALID_LABEL
;
1790 /* Client may register same FEC with different label index. */
1792 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1793 if (!new_client
&& fec
->label_index
== label_index
)
1794 /* Duplicate register */
1797 /* Save current label, update label index */
1798 old_label
= fec
->label
;
1799 fec
->label_index
= label_index
;
1803 listnode_add(fec
->client_list
, client
);
1805 if (IS_ZEBRA_DEBUG_MPLS
)
1806 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1807 label_index
, new_client
? "registered" : "updated",
1808 zebra_route_string(client
->proto
));
1810 /* If not a configured FEC, derive the local label (from label index)
1813 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1814 fec_derive_label_from_index(zvrf
, fec
);
1816 /* If no label change, exit. */
1817 if (fec
->label
== old_label
)
1823 /* If new client or label change, update client and install or uninstall
1824 * label forwarding entry as needed.
1826 /* Inform client of label, if needed. */
1827 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1828 if (IS_ZEBRA_DEBUG_MPLS
)
1829 zlog_debug("Update client label %u", fec
->label
);
1830 fec_send(fec
, client
);
1833 if (new_client
|| label_change
)
1834 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1840 * Deregistration from a client for the label binding for a FEC. The FEC
1841 * itself is deleted if no other registered clients exist and there is no
1842 * label bound to the FEC.
1844 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1845 struct zserv
*client
)
1847 struct route_table
*table
;
1851 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1855 if (IS_ZEBRA_DEBUG_MPLS
)
1856 prefix2str(p
, buf
, BUFSIZ
);
1858 fec
= fec_find(table
, p
);
1860 prefix2str(p
, buf
, BUFSIZ
);
1861 zlog_err("Failed to find FEC %s upon unregister, client %s",
1862 buf
, zebra_route_string(client
->proto
));
1866 listnode_delete(fec
->client_list
, client
);
1868 if (IS_ZEBRA_DEBUG_MPLS
)
1869 zlog_debug("FEC %s unregistered by client %s", buf
,
1870 zebra_route_string(client
->proto
));
1872 /* If not a configured entry, delete the FEC if no other clients. Before
1873 * deleting, see if any LSP needs to be uninstalled.
1875 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1876 && list_isempty(fec
->client_list
)) {
1877 mpls_label_t old_label
= fec
->label
;
1878 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1879 fec_change_update_lsp(zvrf
, fec
, old_label
);
1887 * Cleanup any FECs registered by this client.
1889 int zebra_mpls_cleanup_fecs_for_client(struct zebra_vrf
*zvrf
,
1890 struct zserv
*client
)
1892 struct route_node
*rn
;
1894 struct listnode
*node
;
1895 struct zserv
*fec_client
;
1898 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1899 if (zvrf
->fec_table
[af
] == NULL
)
1902 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1903 rn
= route_next(rn
)) {
1905 if (!fec
|| list_isempty(fec
->client_list
))
1908 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1910 if (fec_client
== client
) {
1911 listnode_delete(fec
->client_list
,
1913 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1914 && list_isempty(fec
->client_list
))
1926 * Return FEC (if any) to which this label is bound.
1927 * Note: Only works for per-prefix binding and when the label is not
1929 * TODO: Currently walks entire table, can optimize later with another
1932 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1935 struct route_node
*rn
;
1939 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1940 if (zvrf
->fec_table
[af
] == NULL
)
1943 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1944 rn
= route_next(rn
)) {
1948 if (fec
->label
== label
)
1957 * Inform if specified label is currently bound to a FEC or not.
1959 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
1961 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
1965 * Add static FEC to label binding. If there are clients registered for this
1966 * FEC, notify them. If there are labeled routes for this FEC, install the
1967 * label forwarding entry.
1969 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1970 mpls_label_t in_label
)
1972 struct route_table
*table
;
1975 mpls_label_t old_label
;
1978 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1982 if (IS_ZEBRA_DEBUG_MPLS
)
1983 prefix2str(p
, buf
, BUFSIZ
);
1985 /* Update existing FEC or create a new one. */
1986 fec
= fec_find(table
, p
);
1988 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
1989 MPLS_INVALID_LABEL_INDEX
);
1991 prefix2str(p
, buf
, BUFSIZ
);
1992 zlog_err("Failed to add FEC %s upon config", buf
);
1996 if (IS_ZEBRA_DEBUG_MPLS
)
1997 zlog_debug("Add fec %s label %u", buf
, in_label
);
1999 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2000 if (fec
->label
== in_label
)
2001 /* Duplicate config */
2004 /* Label change, update clients. */
2005 old_label
= fec
->label
;
2006 if (IS_ZEBRA_DEBUG_MPLS
)
2007 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2009 fec
->label
= in_label
;
2010 fec_update_clients(fec
);
2012 /* Update label forwarding entries appropriately */
2013 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2020 * Remove static FEC to label binding. If there are no clients registered
2021 * for this FEC, delete the FEC; else notify clients
2022 * Note: Upon delete of static binding, if label index exists for this FEC,
2023 * client may need to be updated with derived label.
2025 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2027 struct route_table
*table
;
2029 mpls_label_t old_label
;
2032 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2036 fec
= fec_find(table
, p
);
2038 prefix2str(p
, buf
, BUFSIZ
);
2039 zlog_err("Failed to find FEC %s upon delete", buf
);
2043 if (IS_ZEBRA_DEBUG_MPLS
) {
2044 prefix2str(p
, buf
, BUFSIZ
);
2045 zlog_debug("Delete fec %s label index %u", buf
,
2049 old_label
= fec
->label
;
2050 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2051 fec
->label
= MPLS_INVALID_LABEL
;
2053 /* If no client exists, just delete the FEC. */
2054 if (list_isempty(fec
->client_list
)) {
2059 /* Derive the local label (from label index) or reset it. */
2060 fec_derive_label_from_index(zvrf
, fec
);
2062 /* If there is a label change, update clients. */
2063 if (fec
->label
== old_label
)
2065 fec_update_clients(fec
);
2067 /* Update label forwarding entries appropriately */
2068 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2072 * Display MPLS FEC to label binding configuration (VTY command handler).
2074 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2076 struct route_node
*rn
;
2082 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2083 if (zvrf
->fec_table
[af
] == NULL
)
2086 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2087 rn
= route_next(rn
)) {
2094 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2098 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2099 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2100 label2str(fec
->label
, lstr
, BUFSIZ
));
2108 * Display MPLS FEC to label binding (VTY command handler).
2110 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2112 struct route_node
*rn
;
2115 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2116 if (zvrf
->fec_table
[af
] == NULL
)
2119 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2120 rn
= route_next(rn
)) {
2123 fec_print(rn
->info
, vty
);
2129 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2131 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2134 struct route_table
*table
;
2135 struct route_node
*rn
;
2137 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2142 rn
= route_node_lookup(table
, p
);
2146 route_unlock_node(rn
);
2150 fec_print(rn
->info
, vty
);
2154 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2156 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2157 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2158 union g_addr
*gate
, ifindex_t ifindex
, u_int8_t distance
,
2159 mpls_label_t out_label
)
2161 struct route_table
*table
;
2162 struct route_node
*rn
;
2163 struct route_entry
*re
;
2164 struct nexthop
*nexthop
;
2167 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2172 /* Lookup existing route */
2173 rn
= route_node_get(table
, prefix
);
2174 RNODE_FOREACH_RE(rn
, re
)
2176 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2178 if (re
->distance
== distance
)
2185 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2186 switch (nexthop
->type
) {
2187 case NEXTHOP_TYPE_IPV4
:
2188 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2189 if (gtype
!= NEXTHOP_TYPE_IPV4
2190 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2192 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2194 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2195 && nexthop
->ifindex
!= ifindex
)
2198 case NEXTHOP_TYPE_IPV6
:
2199 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2200 if (gtype
!= NEXTHOP_TYPE_IPV6
2201 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2203 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2205 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2206 && nexthop
->ifindex
!= ifindex
)
2213 /* nexthop not found */
2217 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2218 nexthop_add_labels(nexthop
, type
, 1, &out_label
);
2219 else if (!add
&& nexthop
->nh_label_type
== type
)
2220 nexthop_del_labels(nexthop
);
2224 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2225 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
2232 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2233 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2234 * the out-label for an existing NHLFE (update case).
2236 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2237 mpls_label_t in_label
, mpls_label_t out_label
,
2238 enum nexthop_types_t gtype
, union g_addr
*gate
,
2241 struct hash
*lsp_table
;
2242 zebra_ile_t tmp_ile
;
2244 zebra_nhlfe_t
*nhlfe
;
2248 lsp_table
= zvrf
->lsp_table
;
2252 /* If entry is present, exit. */
2253 tmp_ile
.in_label
= in_label
;
2254 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2257 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2259 struct nexthop
*nh
= nhlfe
->nexthop
;
2262 assert(nh
->nh_label
);
2264 /* Clear deleted flag (in case it was set) */
2265 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2266 if (nh
->nh_label
->label
[0] == out_label
)
2270 if (IS_ZEBRA_DEBUG_MPLS
) {
2271 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2273 "LSP in-label %u type %d nexthop %s "
2274 "out-label changed to %u (old %u)",
2275 in_label
, type
, buf
, out_label
,
2276 nh
->nh_label
->label
[0]);
2279 /* Update out label, trigger processing. */
2280 nh
->nh_label
->label
[0] = out_label
;
2282 /* Add LSP entry to this nexthop */
2283 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2287 if (IS_ZEBRA_DEBUG_MPLS
) {
2288 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2290 "Add LSP in-label %u type %d nexthop %s "
2292 in_label
, type
, buf
, out_label
);
2295 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2298 /* Mark NHLFE, queue LSP for processing. */
2299 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2300 if (lsp_processq_add(lsp
))
2307 * Uninstall a particular NHLFE in the forwarding table. If this is
2308 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2310 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2311 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2312 union g_addr
*gate
, ifindex_t ifindex
)
2314 struct hash
*lsp_table
;
2315 zebra_ile_t tmp_ile
;
2317 zebra_nhlfe_t
*nhlfe
;
2321 lsp_table
= zvrf
->lsp_table
;
2325 /* If entry is not present, exit. */
2326 tmp_ile
.in_label
= in_label
;
2327 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2330 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2334 if (IS_ZEBRA_DEBUG_MPLS
) {
2335 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2336 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2337 in_label
, type
, buf
, nhlfe
->flags
);
2340 /* Mark NHLFE for delete or directly delete, as appropriate. */
2341 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2342 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2343 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2344 if (lsp_processq_add(lsp
))
2349 /* Free LSP entry if no other NHLFEs and not scheduled. */
2350 if (!lsp
->nhlfe_list
2351 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2352 if (IS_ZEBRA_DEBUG_MPLS
)
2353 zlog_debug("Free LSP in-label %u flags 0x%x",
2354 lsp
->ile
.in_label
, lsp
->flags
);
2356 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2358 XFREE(MTYPE_LSP
, lsp
);
2365 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2366 * If no other NHLFEs exist, the entry would be deleted.
2368 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2371 struct hash
*lsp_table
;
2373 lsp
= (zebra_lsp_t
*)backet
->data
;
2374 if (!lsp
|| !lsp
->nhlfe_list
)
2381 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2385 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2387 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2389 struct route_table
*table
;
2390 struct route_node
*rn
;
2391 struct route_entry
*re
;
2392 struct nexthop
*nexthop
;
2395 /* Process routes of interested address-families. */
2396 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2400 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2402 RNODE_FOREACH_RE(rn
, re
)
2403 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2404 if (nexthop
->nh_label_type
== ZEBRA_LSP_LDP
) {
2405 nexthop_del_labels(nexthop
);
2406 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2407 SET_FLAG(re
->status
,
2408 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
2417 #if defined(HAVE_CUMULUS)
2419 * Check that the label values used in LSP creation are consistent. The
2420 * main criteria is that if there is ECMP, the label operation must still
2421 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2422 * to current HW restrictions.
2424 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2425 mpls_label_t in_label
,
2426 mpls_label_t out_label
,
2427 enum nexthop_types_t gtype
,
2428 union g_addr
*gate
, ifindex_t ifindex
)
2430 struct hash
*slsp_table
;
2431 zebra_ile_t tmp_ile
;
2433 zebra_snhlfe_t
*snhlfe
;
2436 slsp_table
= zvrf
->slsp_table
;
2440 /* If entry is not present, exit. */
2441 tmp_ile
.in_label
= in_label
;
2442 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2446 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2448 if (snhlfe
->out_label
== out_label
)
2451 /* If not only NHLFE, cannot allow label change. */
2452 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2455 /* If other NHLFEs exist, label operation must match. */
2456 if (slsp
->snhlfe_list
) {
2459 cur_op
= (slsp
->snhlfe_list
->out_label
2460 == MPLS_IMP_NULL_LABEL
);
2461 new_op
= (out_label
== MPLS_IMP_NULL_LABEL
);
2462 if (cur_op
!= new_op
)
2467 /* Label values are good. */
2470 #endif /* HAVE_CUMULUS */
2473 * Add static LSP entry. This may be the first entry for this incoming label
2474 * or an additional nexthop; an existing entry may also have outgoing label
2476 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2479 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2480 mpls_label_t out_label
,
2481 enum nexthop_types_t gtype
, union g_addr
*gate
,
2484 struct hash
*slsp_table
;
2485 zebra_ile_t tmp_ile
;
2487 zebra_snhlfe_t
*snhlfe
;
2491 slsp_table
= zvrf
->slsp_table
;
2495 /* If entry is present, exit. */
2496 tmp_ile
.in_label
= in_label
;
2497 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2500 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2502 if (snhlfe
->out_label
== out_label
)
2506 if (IS_ZEBRA_DEBUG_MPLS
) {
2507 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2509 "Upd static LSP in-label %u nexthop %s "
2510 "out-label %u (old %u)",
2511 in_label
, buf
, out_label
, snhlfe
->out_label
);
2513 snhlfe
->out_label
= out_label
;
2515 /* Add static LSP entry to this nexthop */
2516 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2520 if (IS_ZEBRA_DEBUG_MPLS
) {
2521 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2523 "Add static LSP in-label %u nexthop %s out-label %u",
2524 in_label
, buf
, out_label
);
2528 /* (Re)Install LSP in the main table. */
2529 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2537 * Delete static LSP entry. This may be the delete of one particular
2538 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2540 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2541 * LSP configuration.
2543 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2544 enum nexthop_types_t gtype
, union g_addr
*gate
,
2547 struct hash
*slsp_table
;
2548 zebra_ile_t tmp_ile
;
2550 zebra_snhlfe_t
*snhlfe
;
2553 slsp_table
= zvrf
->slsp_table
;
2557 /* If entry is not present, exit. */
2558 tmp_ile
.in_label
= in_label
;
2559 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2563 /* Is it delete of entire LSP or a specific NHLFE? */
2564 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2565 if (IS_ZEBRA_DEBUG_MPLS
)
2566 zlog_debug("Del static LSP in-label %u", in_label
);
2568 /* Uninstall entire LSP from the main table. */
2569 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2571 /* Delete all static NHLFEs */
2572 snhlfe_del_all(slsp
);
2574 /* Find specific NHLFE, exit if not found. */
2575 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2579 if (IS_ZEBRA_DEBUG_MPLS
) {
2581 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2582 zlog_debug("Del static LSP in-label %u nexthop %s",
2586 /* Uninstall LSP from the main table. */
2587 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2590 /* Delete static LSP NHLFE */
2594 /* Remove entire static LSP entry if no NHLFE - valid in either case
2596 if (!slsp
->snhlfe_list
) {
2597 slsp
= hash_release(slsp_table
, &tmp_ile
);
2599 XFREE(MTYPE_SLSP
, slsp
);
2606 * Schedule all MPLS label forwarding entries for processing.
2607 * Called upon changes that may affect one or more of them such as
2608 * interface or nexthop state changes.
2610 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2614 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2618 * Display MPLS label forwarding table for a specific LSP
2619 * (VTY command handler).
2621 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2622 mpls_label_t label
, u_char use_json
)
2624 struct hash
*lsp_table
;
2626 zebra_ile_t tmp_ile
;
2627 json_object
*json
= NULL
;
2630 lsp_table
= zvrf
->lsp_table
;
2634 /* If entry is not present, exit. */
2635 tmp_ile
.in_label
= label
;
2636 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2641 json
= lsp_json(lsp
);
2642 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2643 json
, JSON_C_TO_STRING_PRETTY
));
2644 json_object_free(json
);
2646 lsp_print(lsp
, (void *)vty
);
2650 * Display MPLS label forwarding table (VTY command handler).
2652 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2656 json_object
*json
= NULL
;
2657 zebra_lsp_t
*lsp
= NULL
;
2658 zebra_nhlfe_t
*nhlfe
= NULL
;
2659 struct nexthop
*nexthop
= NULL
;
2660 struct listnode
*node
= NULL
;
2661 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2664 json
= json_object_new_object();
2666 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2667 json_object_object_add(
2668 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2671 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2672 json
, JSON_C_TO_STRING_PRETTY
));
2673 json_object_free(json
);
2675 vty_out(vty
, " Inbound Outbound\n");
2676 vty_out(vty
, " Label Type Nexthop Label\n");
2677 vty_out(vty
, "-------- ------- --------------- --------\n");
2679 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2680 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2681 nhlfe
= nhlfe
->next
) {
2682 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2683 nhlfe_type2str(nhlfe
->type
));
2684 nexthop
= nhlfe
->nexthop
;
2686 switch (nexthop
->type
) {
2687 case NEXTHOP_TYPE_IPV4
:
2688 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2689 vty_out(vty
, "%15s",
2690 inet_ntoa(nexthop
->gate
.ipv4
));
2692 case NEXTHOP_TYPE_IPV6
:
2693 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2694 vty_out(vty
, "%15s",
2696 &nexthop
->gate
.ipv6
,
2703 vty_out(vty
, " %8d\n",
2704 nexthop
->nh_label
->label
[0]);
2711 list_delete(lsp_list
);
2715 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2717 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2720 zebra_snhlfe_t
*snhlfe
;
2721 struct listnode
*node
;
2722 struct list
*slsp_list
=
2723 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2725 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2726 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2727 snhlfe
= snhlfe
->next
) {
2728 char buf
[INET6_ADDRSTRLEN
];
2731 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2732 switch (snhlfe
->out_label
) {
2733 case MPLS_V4_EXP_NULL_LABEL
:
2734 case MPLS_V6_EXP_NULL_LABEL
:
2735 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2737 case MPLS_IMP_NULL_LABEL
:
2738 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2741 sprintf(lstr
, "%u", snhlfe
->out_label
);
2745 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2750 list_delete(slsp_list
);
2751 return (zvrf
->slsp_table
->count
? 1 : 0);
2755 * Add/update global label block.
2757 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, u_int32_t start_label
,
2758 u_int32_t end_label
)
2760 zvrf
->mpls_srgb
.start_label
= start_label
;
2761 zvrf
->mpls_srgb
.end_label
= end_label
;
2763 /* Evaluate registered FECs to see if any get a label or not. */
2769 * Delete global label block.
2771 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2773 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2774 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2776 /* Process registered FECs to clear their local label, if needed. */
2782 * Display MPLS global label block configuration (VTY command handler).
2784 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2786 if (zvrf
->mpls_srgb
.start_label
== 0)
2789 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2790 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2791 vty_out(vty
, "mpls label global-block %u %u\n",
2792 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2799 * Called upon process exiting, need to delete LSP forwarding
2800 * entries from the kernel.
2801 * NOTE: Currently supported only for default VRF.
2803 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2805 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2806 hash_clean(zvrf
->lsp_table
, NULL
);
2807 hash_free(zvrf
->lsp_table
);
2808 hash_clean(zvrf
->slsp_table
, NULL
);
2809 hash_free(zvrf
->slsp_table
);
2810 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2811 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2815 * Allocate MPLS tables for this VRF and do other initialization.
2816 * NOTE: Currently supported only for default VRF.
2818 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2822 zvrf
->slsp_table
= hash_create(label_hash
, label_cmp
, NULL
);
2823 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, NULL
);
2824 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2825 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2826 zvrf
->mpls_flags
= 0;
2827 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2828 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2832 * Global MPLS initialization.
2834 void zebra_mpls_init(void)
2838 if (mpls_kernel_init() < 0) {
2839 zlog_warn("Disabling MPLS support (no kernel support)");
2843 if (!mpls_processq_init(&zebrad
))