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 (void)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 label_buf
[BUFSIZ
];
1678 for (i
= 0; i
< num_labels
; i
++) {
1680 strlcat(buf
, "/", len
);
1682 label2str(labels
[i
], label_buf
, sizeof(label_buf
));
1684 snprintf(label_buf
, sizeof(label_buf
), "%u", labels
[i
]);
1685 strlcat(buf
, label_buf
, len
);
1692 * Install dynamic LSP entry.
1694 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1695 struct route_entry
*re
)
1697 struct route_table
*table
;
1700 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1704 /* See if there is a configured label binding for this FEC. */
1705 fec
= fec_find(table
, &rn
->p
);
1706 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1709 /* We cannot install a label forwarding entry if local label is the
1710 * implicit-null label.
1712 if (fec
->label
== MPLS_IMP_NULL_LABEL
)
1715 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
1722 * Uninstall dynamic LSP entry, if any.
1724 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1725 struct route_entry
*re
)
1727 struct route_table
*table
;
1730 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
1734 /* See if there is a configured label binding for this FEC. */
1735 fec
= fec_find(table
, &rn
->p
);
1736 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
1739 /* Uninstall always removes all dynamic NHLFEs. */
1740 return lsp_uninstall(zvrf
, fec
->label
);
1744 * Registration from a client for the label binding for a FEC. If a binding
1745 * already exists, it is informed to the client.
1746 * NOTE: If there is a manually configured label binding, that is used.
1747 * Otherwise, if a label index is specified, it means we have to allocate the
1748 * label from a locally configured label block (SRGB), if one exists and index
1751 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1752 u_int32_t label_index
, struct zserv
*client
)
1754 struct route_table
*table
;
1758 int label_change
= 0;
1759 u_int32_t old_label
;
1761 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1765 if (IS_ZEBRA_DEBUG_MPLS
)
1766 prefix2str(p
, buf
, BUFSIZ
);
1769 fec
= fec_find(table
, p
);
1771 fec
= fec_add(table
, p
, MPLS_INVALID_LABEL
, 0, label_index
);
1773 prefix2str(p
, buf
, BUFSIZ
);
1775 "Failed to add FEC %s upon register, client %s",
1776 buf
, zebra_route_string(client
->proto
));
1780 old_label
= MPLS_INVALID_LABEL
;
1783 /* Client may register same FEC with different label index. */
1785 (listnode_lookup(fec
->client_list
, client
) == NULL
);
1786 if (!new_client
&& fec
->label_index
== label_index
)
1787 /* Duplicate register */
1790 /* Save current label, update label index */
1791 old_label
= fec
->label
;
1792 fec
->label_index
= label_index
;
1796 listnode_add(fec
->client_list
, client
);
1798 if (IS_ZEBRA_DEBUG_MPLS
)
1799 zlog_debug("FEC %s Label Index %u %s by client %s", buf
,
1800 label_index
, new_client
? "registered" : "updated",
1801 zebra_route_string(client
->proto
));
1803 /* If not a configured FEC, derive the local label (from label index)
1806 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)) {
1807 fec_derive_label_from_index(zvrf
, fec
);
1809 /* If no label change, exit. */
1810 if (fec
->label
== old_label
)
1816 /* If new client or label change, update client and install or uninstall
1817 * label forwarding entry as needed.
1819 /* Inform client of label, if needed. */
1820 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
1821 if (IS_ZEBRA_DEBUG_MPLS
)
1822 zlog_debug("Update client label %u", fec
->label
);
1823 fec_send(fec
, client
);
1826 if (new_client
|| label_change
)
1827 return fec_change_update_lsp(zvrf
, fec
, old_label
);
1833 * Deregistration from a client for the label binding for a FEC. The FEC
1834 * itself is deleted if no other registered clients exist and there is no
1835 * label bound to the FEC.
1837 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1838 struct zserv
*client
)
1840 struct route_table
*table
;
1844 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1848 if (IS_ZEBRA_DEBUG_MPLS
)
1849 prefix2str(p
, buf
, BUFSIZ
);
1851 fec
= fec_find(table
, p
);
1853 prefix2str(p
, buf
, BUFSIZ
);
1854 zlog_err("Failed to find FEC %s upon unregister, client %s",
1855 buf
, zebra_route_string(client
->proto
));
1859 listnode_delete(fec
->client_list
, client
);
1861 if (IS_ZEBRA_DEBUG_MPLS
)
1862 zlog_debug("FEC %s unregistered by client %s", buf
,
1863 zebra_route_string(client
->proto
));
1865 /* If not a configured entry, delete the FEC if no other clients. Before
1866 * deleting, see if any LSP needs to be uninstalled.
1868 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1869 && list_isempty(fec
->client_list
)) {
1870 mpls_label_t old_label
= fec
->label
;
1871 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
1872 fec_change_update_lsp(zvrf
, fec
, old_label
);
1880 * Cleanup any FECs registered by this client.
1882 int zebra_mpls_cleanup_fecs_for_client(struct zebra_vrf
*zvrf
,
1883 struct zserv
*client
)
1885 struct route_node
*rn
;
1887 struct listnode
*node
;
1888 struct zserv
*fec_client
;
1891 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1892 if (zvrf
->fec_table
[af
] == NULL
)
1895 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1896 rn
= route_next(rn
)) {
1898 if (!fec
|| list_isempty(fec
->client_list
))
1901 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
1903 if (fec_client
== client
) {
1904 listnode_delete(fec
->client_list
,
1906 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
1907 && list_isempty(fec
->client_list
))
1919 * Return FEC (if any) to which this label is bound.
1920 * Note: Only works for per-prefix binding and when the label is not
1922 * TODO: Currently walks entire table, can optimize later with another
1925 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
1928 struct route_node
*rn
;
1932 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
1933 if (zvrf
->fec_table
[af
] == NULL
)
1936 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
1937 rn
= route_next(rn
)) {
1941 if (fec
->label
== label
)
1950 * Inform if specified label is currently bound to a FEC or not.
1952 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
1954 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
1958 * Add static FEC to label binding. If there are clients registered for this
1959 * FEC, notify them. If there are labeled routes for this FEC, install the
1960 * label forwarding entry.
1962 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
1963 mpls_label_t in_label
)
1965 struct route_table
*table
;
1968 mpls_label_t old_label
;
1971 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
1975 if (IS_ZEBRA_DEBUG_MPLS
)
1976 prefix2str(p
, buf
, BUFSIZ
);
1978 /* Update existing FEC or create a new one. */
1979 fec
= fec_find(table
, p
);
1981 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
1982 MPLS_INVALID_LABEL_INDEX
);
1984 prefix2str(p
, buf
, BUFSIZ
);
1985 zlog_err("Failed to add FEC %s upon config", buf
);
1989 if (IS_ZEBRA_DEBUG_MPLS
)
1990 zlog_debug("Add fec %s label %u", buf
, in_label
);
1992 fec
->flags
|= FEC_FLAG_CONFIGURED
;
1993 if (fec
->label
== in_label
)
1994 /* Duplicate config */
1997 /* Label change, update clients. */
1998 old_label
= fec
->label
;
1999 if (IS_ZEBRA_DEBUG_MPLS
)
2000 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2002 fec
->label
= in_label
;
2003 fec_update_clients(fec
);
2005 /* Update label forwarding entries appropriately */
2006 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2013 * Remove static FEC to label binding. If there are no clients registered
2014 * for this FEC, delete the FEC; else notify clients
2015 * Note: Upon delete of static binding, if label index exists for this FEC,
2016 * client may need to be updated with derived label.
2018 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2020 struct route_table
*table
;
2022 mpls_label_t old_label
;
2025 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2029 fec
= fec_find(table
, p
);
2031 prefix2str(p
, buf
, BUFSIZ
);
2032 zlog_err("Failed to find FEC %s upon delete", buf
);
2036 if (IS_ZEBRA_DEBUG_MPLS
) {
2037 prefix2str(p
, buf
, BUFSIZ
);
2038 zlog_debug("Delete fec %s label index %u", buf
,
2042 old_label
= fec
->label
;
2043 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2044 fec
->label
= MPLS_INVALID_LABEL
;
2046 /* If no client exists, just delete the FEC. */
2047 if (list_isempty(fec
->client_list
)) {
2052 /* Derive the local label (from label index) or reset it. */
2053 fec_derive_label_from_index(zvrf
, fec
);
2055 /* If there is a label change, update clients. */
2056 if (fec
->label
== old_label
)
2058 fec_update_clients(fec
);
2060 /* Update label forwarding entries appropriately */
2061 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2065 * Display MPLS FEC to label binding configuration (VTY command handler).
2067 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2069 struct route_node
*rn
;
2075 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2076 if (zvrf
->fec_table
[af
] == NULL
)
2079 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2080 rn
= route_next(rn
)) {
2087 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2091 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2092 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2093 label2str(fec
->label
, lstr
, BUFSIZ
));
2101 * Display MPLS FEC to label binding (VTY command handler).
2103 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2105 struct route_node
*rn
;
2108 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2109 if (zvrf
->fec_table
[af
] == NULL
)
2112 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2113 rn
= route_next(rn
)) {
2116 fec_print(rn
->info
, vty
);
2122 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2124 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2127 struct route_table
*table
;
2128 struct route_node
*rn
;
2130 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2135 rn
= route_node_lookup(table
, p
);
2139 route_unlock_node(rn
);
2143 fec_print(rn
->info
, vty
);
2147 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2149 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2150 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2151 union g_addr
*gate
, ifindex_t ifindex
, u_int8_t distance
,
2152 mpls_label_t out_label
)
2154 struct route_table
*table
;
2155 struct route_node
*rn
;
2156 struct route_entry
*re
;
2157 struct nexthop
*nexthop
;
2160 table
= zebra_vrf_table(family2afi(prefix
->family
), SAFI_UNICAST
,
2165 /* Lookup existing route */
2166 rn
= route_node_get(table
, prefix
);
2167 RNODE_FOREACH_RE(rn
, re
)
2169 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2171 if (re
->distance
== distance
)
2178 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2179 switch (nexthop
->type
) {
2180 case NEXTHOP_TYPE_IPV4
:
2181 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2182 if (gtype
!= NEXTHOP_TYPE_IPV4
2183 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2185 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2187 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2188 && nexthop
->ifindex
!= ifindex
)
2191 case NEXTHOP_TYPE_IPV6
:
2192 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2193 if (gtype
!= NEXTHOP_TYPE_IPV6
2194 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2196 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2198 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2199 && nexthop
->ifindex
!= ifindex
)
2206 /* nexthop not found */
2210 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2211 nexthop_add_labels(nexthop
, type
, 1, &out_label
);
2212 else if (!add
&& nexthop
->nh_label_type
== type
)
2213 nexthop_del_labels(nexthop
);
2217 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2218 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2225 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2226 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2227 * the out-label for an existing NHLFE (update case).
2229 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2230 mpls_label_t in_label
, mpls_label_t out_label
,
2231 enum nexthop_types_t gtype
, union g_addr
*gate
,
2234 struct hash
*lsp_table
;
2235 zebra_ile_t tmp_ile
;
2237 zebra_nhlfe_t
*nhlfe
;
2241 lsp_table
= zvrf
->lsp_table
;
2245 /* If entry is present, exit. */
2246 tmp_ile
.in_label
= in_label
;
2247 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2250 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2252 struct nexthop
*nh
= nhlfe
->nexthop
;
2255 assert(nh
->nh_label
);
2257 /* Clear deleted flag (in case it was set) */
2258 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2259 if (nh
->nh_label
->label
[0] == out_label
)
2263 if (IS_ZEBRA_DEBUG_MPLS
) {
2264 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2266 "LSP in-label %u type %d nexthop %s "
2267 "out-label changed to %u (old %u)",
2268 in_label
, type
, buf
, out_label
,
2269 nh
->nh_label
->label
[0]);
2272 /* Update out label, trigger processing. */
2273 nh
->nh_label
->label
[0] = out_label
;
2275 /* Add LSP entry to this nexthop */
2276 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
, out_label
);
2280 if (IS_ZEBRA_DEBUG_MPLS
) {
2281 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2283 "Add LSP in-label %u type %d nexthop %s "
2285 in_label
, type
, buf
, out_label
);
2288 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2291 /* Mark NHLFE, queue LSP for processing. */
2292 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2293 if (lsp_processq_add(lsp
))
2300 * Uninstall a particular NHLFE in the forwarding table. If this is
2301 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2303 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2304 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2305 union g_addr
*gate
, ifindex_t ifindex
)
2307 struct hash
*lsp_table
;
2308 zebra_ile_t tmp_ile
;
2310 zebra_nhlfe_t
*nhlfe
;
2314 lsp_table
= zvrf
->lsp_table
;
2318 /* If entry is not present, exit. */
2319 tmp_ile
.in_label
= in_label
;
2320 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2323 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2327 if (IS_ZEBRA_DEBUG_MPLS
) {
2328 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2329 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2330 in_label
, type
, buf
, nhlfe
->flags
);
2333 /* Mark NHLFE for delete or directly delete, as appropriate. */
2334 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2335 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2336 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2337 if (lsp_processq_add(lsp
))
2342 /* Free LSP entry if no other NHLFEs and not scheduled. */
2343 if (!lsp
->nhlfe_list
2344 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2345 if (IS_ZEBRA_DEBUG_MPLS
)
2346 zlog_debug("Free LSP in-label %u flags 0x%x",
2347 lsp
->ile
.in_label
, lsp
->flags
);
2349 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2351 XFREE(MTYPE_LSP
, lsp
);
2358 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2359 * If no other NHLFEs exist, the entry would be deleted.
2361 void mpls_ldp_lsp_uninstall_all(struct hash_backet
*backet
, void *ctxt
)
2364 struct hash
*lsp_table
;
2366 lsp
= (zebra_lsp_t
*)backet
->data
;
2367 if (!lsp
|| !lsp
->nhlfe_list
)
2374 mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_LDP
);
2378 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2380 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf
*zvrf
, int afi
)
2382 struct route_table
*table
;
2383 struct route_node
*rn
;
2384 struct route_entry
*re
;
2385 struct nexthop
*nexthop
;
2388 /* Process routes of interested address-families. */
2389 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2393 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2395 RNODE_FOREACH_RE(rn
, re
) {
2396 for (nexthop
= re
->nexthop
;
2398 nexthop
= nexthop
->next
) {
2399 if (nexthop
->nh_label_type
!= ZEBRA_LSP_LDP
)
2402 nexthop_del_labels(nexthop
);
2403 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2404 SET_FLAG(re
->status
,
2405 ROUTE_ENTRY_LABELS_CHANGED
);
2415 #if defined(HAVE_CUMULUS)
2417 * Check that the label values used in LSP creation are consistent. The
2418 * main criteria is that if there is ECMP, the label operation must still
2419 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2420 * to current HW restrictions.
2422 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2423 mpls_label_t in_label
,
2424 mpls_label_t out_label
,
2425 enum nexthop_types_t gtype
,
2426 union g_addr
*gate
, ifindex_t ifindex
)
2428 struct hash
*slsp_table
;
2429 zebra_ile_t tmp_ile
;
2431 zebra_snhlfe_t
*snhlfe
;
2434 slsp_table
= zvrf
->slsp_table
;
2438 /* If entry is not present, exit. */
2439 tmp_ile
.in_label
= in_label
;
2440 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2444 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2446 if (snhlfe
->out_label
== out_label
)
2449 /* If not only NHLFE, cannot allow label change. */
2450 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
2453 /* If other NHLFEs exist, label operation must match. */
2454 if (slsp
->snhlfe_list
) {
2457 cur_op
= (slsp
->snhlfe_list
->out_label
2458 == MPLS_IMP_NULL_LABEL
);
2459 new_op
= (out_label
== MPLS_IMP_NULL_LABEL
);
2460 if (cur_op
!= new_op
)
2465 /* Label values are good. */
2468 #endif /* HAVE_CUMULUS */
2471 * Add static LSP entry. This may be the first entry for this incoming label
2472 * or an additional nexthop; an existing entry may also have outgoing label
2474 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2477 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2478 mpls_label_t out_label
,
2479 enum nexthop_types_t gtype
, union g_addr
*gate
,
2482 struct hash
*slsp_table
;
2483 zebra_ile_t tmp_ile
;
2485 zebra_snhlfe_t
*snhlfe
;
2489 slsp_table
= zvrf
->slsp_table
;
2493 /* If entry is present, exit. */
2494 tmp_ile
.in_label
= in_label
;
2495 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
2498 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2500 if (snhlfe
->out_label
== out_label
)
2504 if (IS_ZEBRA_DEBUG_MPLS
) {
2505 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2507 "Upd static LSP in-label %u nexthop %s "
2508 "out-label %u (old %u)",
2509 in_label
, buf
, out_label
, snhlfe
->out_label
);
2511 snhlfe
->out_label
= out_label
;
2513 /* Add static LSP entry to this nexthop */
2514 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
2518 if (IS_ZEBRA_DEBUG_MPLS
) {
2519 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2521 "Add static LSP in-label %u nexthop %s out-label %u",
2522 in_label
, buf
, out_label
);
2526 /* (Re)Install LSP in the main table. */
2527 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, out_label
, gtype
,
2535 * Delete static LSP entry. This may be the delete of one particular
2536 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2538 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2539 * LSP configuration.
2541 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
2542 enum nexthop_types_t gtype
, union g_addr
*gate
,
2545 struct hash
*slsp_table
;
2546 zebra_ile_t tmp_ile
;
2548 zebra_snhlfe_t
*snhlfe
;
2551 slsp_table
= zvrf
->slsp_table
;
2555 /* If entry is not present, exit. */
2556 tmp_ile
.in_label
= in_label
;
2557 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
2561 /* Is it delete of entire LSP or a specific NHLFE? */
2562 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
2563 if (IS_ZEBRA_DEBUG_MPLS
)
2564 zlog_debug("Del static LSP in-label %u", in_label
);
2566 /* Uninstall entire LSP from the main table. */
2567 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
2569 /* Delete all static NHLFEs */
2570 snhlfe_del_all(slsp
);
2572 /* Find specific NHLFE, exit if not found. */
2573 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
2577 if (IS_ZEBRA_DEBUG_MPLS
) {
2579 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
2580 zlog_debug("Del static LSP in-label %u nexthop %s",
2584 /* Uninstall LSP from the main table. */
2585 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
2588 /* Delete static LSP NHLFE */
2592 /* Remove entire static LSP entry if no NHLFE - valid in either case
2594 if (!slsp
->snhlfe_list
) {
2595 slsp
= hash_release(slsp_table
, &tmp_ile
);
2597 XFREE(MTYPE_SLSP
, slsp
);
2604 * Schedule all MPLS label forwarding entries for processing.
2605 * Called upon changes that may affect one or more of them such as
2606 * interface or nexthop state changes.
2608 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
2612 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
2616 * Display MPLS label forwarding table for a specific LSP
2617 * (VTY command handler).
2619 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2620 mpls_label_t label
, u_char use_json
)
2622 struct hash
*lsp_table
;
2624 zebra_ile_t tmp_ile
;
2625 json_object
*json
= NULL
;
2628 lsp_table
= zvrf
->lsp_table
;
2632 /* If entry is not present, exit. */
2633 tmp_ile
.in_label
= label
;
2634 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2639 json
= lsp_json(lsp
);
2640 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2641 json
, JSON_C_TO_STRING_PRETTY
));
2642 json_object_free(json
);
2644 lsp_print(lsp
, (void *)vty
);
2648 * Display MPLS label forwarding table (VTY command handler).
2650 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2654 json_object
*json
= NULL
;
2655 zebra_lsp_t
*lsp
= NULL
;
2656 zebra_nhlfe_t
*nhlfe
= NULL
;
2657 struct nexthop
*nexthop
= NULL
;
2658 struct listnode
*node
= NULL
;
2659 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
2662 json
= json_object_new_object();
2664 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
2665 json_object_object_add(
2666 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
2669 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2670 json
, JSON_C_TO_STRING_PRETTY
));
2671 json_object_free(json
);
2673 vty_out(vty
, " Inbound Outbound\n");
2674 vty_out(vty
, " Label Type Nexthop Label\n");
2675 vty_out(vty
, "-------- ------- --------------- --------\n");
2677 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
2678 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
2679 nhlfe
= nhlfe
->next
) {
2680 vty_out(vty
, "%8d %7s ", lsp
->ile
.in_label
,
2681 nhlfe_type2str(nhlfe
->type
));
2682 nexthop
= nhlfe
->nexthop
;
2684 switch (nexthop
->type
) {
2685 case NEXTHOP_TYPE_IPV4
:
2686 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2687 vty_out(vty
, "%15s",
2688 inet_ntoa(nexthop
->gate
.ipv4
));
2690 case NEXTHOP_TYPE_IPV6
:
2691 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2692 vty_out(vty
, "%15s",
2694 &nexthop
->gate
.ipv6
,
2701 vty_out(vty
, " %8d\n",
2702 nexthop
->nh_label
->label
[0]);
2709 list_delete(lsp_list
);
2713 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2715 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2718 zebra_snhlfe_t
*snhlfe
;
2719 struct listnode
*node
;
2720 struct list
*slsp_list
=
2721 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
2723 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
2724 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
2725 snhlfe
= snhlfe
->next
) {
2729 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
2730 switch (snhlfe
->out_label
) {
2731 case MPLS_V4_EXP_NULL_LABEL
:
2732 case MPLS_V6_EXP_NULL_LABEL
:
2733 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
2735 case MPLS_IMP_NULL_LABEL
:
2736 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
2739 sprintf(lstr
, "%u", snhlfe
->out_label
);
2743 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
2748 list_delete(slsp_list
);
2749 return (zvrf
->slsp_table
->count
? 1 : 0);
2753 * Add/update global label block.
2755 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, u_int32_t start_label
,
2756 u_int32_t end_label
)
2758 zvrf
->mpls_srgb
.start_label
= start_label
;
2759 zvrf
->mpls_srgb
.end_label
= end_label
;
2761 /* Evaluate registered FECs to see if any get a label or not. */
2767 * Delete global label block.
2769 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
2771 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2772 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2774 /* Process registered FECs to clear their local label, if needed. */
2780 * Display MPLS global label block configuration (VTY command handler).
2782 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2784 if (zvrf
->mpls_srgb
.start_label
== 0)
2787 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
2788 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
2789 vty_out(vty
, "mpls label global-block %u %u\n",
2790 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
2797 * Called upon process exiting, need to delete LSP forwarding
2798 * entries from the kernel.
2799 * NOTE: Currently supported only for default VRF.
2801 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
2803 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
2804 hash_clean(zvrf
->lsp_table
, NULL
);
2805 hash_free(zvrf
->lsp_table
);
2806 hash_clean(zvrf
->slsp_table
, NULL
);
2807 hash_free(zvrf
->slsp_table
);
2808 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
2809 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
2813 * Allocate MPLS tables for this VRF and do other initialization.
2814 * NOTE: Currently supported only for default VRF.
2816 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
2820 zvrf
->slsp_table
= hash_create(label_hash
,
2822 "ZEBRA SLSP table");
2823 zvrf
->lsp_table
= hash_create(label_hash
,
2826 zvrf
->fec_table
[AFI_IP
] = route_table_init();
2827 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
2828 zvrf
->mpls_flags
= 0;
2829 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
2830 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
2834 * Global MPLS initialization.
2836 void zebra_mpls_init(void)
2840 if (mpls_kernel_init() < 0) {
2841 zlog_warn("Disabling MPLS support (no kernel support)");
2845 if (!mpls_processq_init(&zebrad
))