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"
37 #include "termtable.h"
40 #include "zebra/rib.h"
42 #include "zebra/interface.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_memory.h"
48 #include "zebra/zebra_vrf.h"
49 #include "zebra/zebra_mpls.h"
50 #include "zebra/zebra_errors.h"
52 DEFINE_MTYPE_STATIC(ZEBRA
, LSP
, "MPLS LSP object")
53 DEFINE_MTYPE_STATIC(ZEBRA
, FEC
, "MPLS FEC object")
54 DEFINE_MTYPE_STATIC(ZEBRA
, SLSP
, "MPLS static LSP config")
55 DEFINE_MTYPE_STATIC(ZEBRA
, NHLFE
, "MPLS nexthop object")
56 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE
, "MPLS static nexthop object")
57 DEFINE_MTYPE_STATIC(ZEBRA
, SNHLFE_IFNAME
, "MPLS static nexthop ifname")
61 /* static function declarations */
63 static void fec_evaluate(struct zebra_vrf
*zvrf
);
64 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*vrf
,
66 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
67 struct route_node
*rn
, struct route_entry
*re
);
68 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
);
69 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
70 mpls_label_t old_label
);
71 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
);
72 static void fec_update_clients(zebra_fec_t
*fec
);
73 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
);
74 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
);
75 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
76 mpls_label_t label
, uint32_t flags
,
77 uint32_t label_index
);
78 static int fec_del(zebra_fec_t
*fec
);
80 static unsigned int label_hash(const void *p
);
81 static bool label_cmp(const void *p1
, const void *p2
);
82 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
83 struct nexthop
*nexthop
);
84 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
85 struct nexthop
*nexthop
);
86 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
);
88 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
);
89 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
);
90 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
);
91 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
);
92 static void lsp_processq_del(struct work_queue
*wq
, void *data
);
93 static void lsp_processq_complete(struct work_queue
*wq
);
94 static int lsp_processq_add(zebra_lsp_t
*lsp
);
95 static void *lsp_alloc(void *p
);
97 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
);
98 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
99 union g_addr
*gate
, ifindex_t ifindex
);
100 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
101 enum nexthop_types_t gtype
, union g_addr
*gate
,
103 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
104 enum nexthop_types_t gtype
, union g_addr
*gate
,
105 ifindex_t ifindex
, uint8_t num_labels
,
106 mpls_label_t
*labels
);
107 static int nhlfe_del(zebra_nhlfe_t
*snhlfe
);
108 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
109 struct mpls_label_stack
*nh_label
);
110 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
111 enum lsp_types_t type
);
112 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
113 mpls_label_t in_label
);
114 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
);
115 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
);
116 static void *slsp_alloc(void *p
);
117 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
118 union g_addr
*gate
, ifindex_t ifindex
);
119 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
120 enum nexthop_types_t gtype
,
121 union g_addr
*gate
, ifindex_t ifindex
);
122 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
123 enum nexthop_types_t gtype
,
124 union g_addr
*gate
, ifindex_t ifindex
,
125 mpls_label_t out_label
);
126 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
);
127 static int snhlfe_del_all(zebra_slsp_t
*slsp
);
128 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
);
129 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
);
130 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
131 int afi
, enum lsp_types_t lsp_type
);
134 /* Static functions */
137 * Handle failure in LSP install, clear flags for NHLFE.
139 static void clear_nhlfe_installed(zebra_lsp_t
*lsp
)
141 zebra_nhlfe_t
*nhlfe
;
142 struct nexthop
*nexthop
;
144 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
145 nexthop
= nhlfe
->nexthop
;
149 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
150 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
155 * Install label forwarding entry based on labeled-route entry.
157 static int lsp_install(struct zebra_vrf
*zvrf
, mpls_label_t label
,
158 struct route_node
*rn
, struct route_entry
*re
)
160 struct hash
*lsp_table
;
163 zebra_nhlfe_t
*nhlfe
;
164 struct nexthop
*nexthop
;
165 enum lsp_types_t lsp_type
;
170 lsp_table
= zvrf
->lsp_table
;
174 lsp_type
= lsp_type_from_re_type(re
->type
);
177 /* Locate or allocate LSP entry. */
178 tmp_ile
.in_label
= label
;
179 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
183 /* For each active nexthop, create NHLFE. Note that we deliberately skip
184 * recursive nexthops right now, because intermediate hops won't
186 * the label advertised by the recursive nexthop (plus we don't have the
187 * logic yet to push multiple labels).
189 for (nexthop
= re
->ng
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
190 /* Skip inactive and recursive entries. */
191 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
193 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
196 nhlfe
= nhlfe_find(lsp
, lsp_type
, nexthop
->type
, &nexthop
->gate
,
199 /* Clear deleted flag (in case it was set) */
200 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
201 if (nexthop_labels_match(nhlfe
->nexthop
, nexthop
))
206 if (IS_ZEBRA_DEBUG_MPLS
) {
207 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
209 "LSP in-label %u type %d nexthop %s "
211 lsp
->ile
.in_label
, lsp_type
, buf
);
214 /* Update out label, trigger processing. */
215 nhlfe_out_label_update(nhlfe
, nexthop
->nh_label
);
216 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
219 /* Add LSP entry to this nexthop */
220 nhlfe
= nhlfe_add(lsp
, lsp_type
, nexthop
->type
,
221 &nexthop
->gate
, nexthop
->ifindex
,
222 nexthop
->nh_label
->num_labels
,
223 nexthop
->nh_label
->label
);
227 if (IS_ZEBRA_DEBUG_MPLS
) {
228 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
230 "Add LSP in-label %u type %d nexthop %s "
232 lsp
->ile
.in_label
, lsp_type
, buf
,
233 nexthop
->nh_label
->label
[0]);
236 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
238 /* Mark NHLFE as changed. */
239 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
244 /* Queue LSP for processing if necessary. If no NHLFE got added (special
245 * case), delete the LSP entry; this case results in somewhat ugly
248 if (added
|| changed
) {
249 if (lsp_processq_add(lsp
))
251 } else if (!lsp
->nhlfe_list
252 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
253 if (IS_ZEBRA_DEBUG_MPLS
)
254 zlog_debug("Free LSP in-label %u flags 0x%x",
255 lsp
->ile
.in_label
, lsp
->flags
);
257 lsp
= hash_release(lsp_table
, &lsp
->ile
);
258 XFREE(MTYPE_LSP
, lsp
);
265 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
266 * NHLFEs are removed, the entire entry is deleted.
268 static int lsp_uninstall(struct zebra_vrf
*zvrf
, mpls_label_t label
)
270 struct hash
*lsp_table
;
273 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
277 lsp_table
= zvrf
->lsp_table
;
281 /* If entry is not present, exit. */
282 tmp_ile
.in_label
= label
;
283 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
284 if (!lsp
|| !lsp
->nhlfe_list
)
287 /* Mark NHLFEs for delete or directly delete, as appropriate. */
288 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
289 nhlfe_next
= nhlfe
->next
;
291 /* Skip static NHLFEs */
292 if (nhlfe
->type
== ZEBRA_LSP_STATIC
)
295 if (IS_ZEBRA_DEBUG_MPLS
) {
296 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
298 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
299 label
, nhlfe
->type
, buf
, nhlfe
->flags
);
302 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
)) {
303 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
304 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
310 /* Queue LSP for processing, if needed, else delete. */
311 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
312 if (lsp_processq_add(lsp
))
314 } else if (!lsp
->nhlfe_list
315 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
316 if (IS_ZEBRA_DEBUG_MPLS
)
317 zlog_debug("Del LSP in-label %u flags 0x%x",
318 lsp
->ile
.in_label
, lsp
->flags
);
320 lsp
= hash_release(lsp_table
, &lsp
->ile
);
321 XFREE(MTYPE_LSP
, lsp
);
328 * This function is invoked upon change to label block configuration; it
329 * will walk all registered FECs with label-index and appropriately update
330 * their local labels and trigger client updates.
332 static void fec_evaluate(struct zebra_vrf
*zvrf
)
334 struct route_node
*rn
;
336 uint32_t old_label
, new_label
;
340 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
341 if (zvrf
->fec_table
[af
] == NULL
)
344 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
345 rn
= route_next(rn
)) {
346 if ((fec
= rn
->info
) == NULL
)
349 /* Skip configured FECs and those without a label index.
351 if (fec
->flags
& FEC_FLAG_CONFIGURED
352 || fec
->label_index
== MPLS_INVALID_LABEL_INDEX
)
355 if (IS_ZEBRA_DEBUG_MPLS
)
356 prefix2str(&rn
->p
, buf
, BUFSIZ
);
358 /* Save old label, determine new label. */
359 old_label
= fec
->label
;
361 zvrf
->mpls_srgb
.start_label
+ fec
->label_index
;
362 if (new_label
>= zvrf
->mpls_srgb
.end_label
)
363 new_label
= MPLS_INVALID_LABEL
;
365 /* If label has changed, update FEC and clients. */
366 if (new_label
== old_label
)
369 if (IS_ZEBRA_DEBUG_MPLS
)
371 "Update fec %s new label %u upon label block",
374 fec
->label
= new_label
;
375 fec_update_clients(fec
);
377 /* Update label forwarding entries appropriately */
378 fec_change_update_lsp(zvrf
, fec
, old_label
);
384 * Derive (if possible) and update the local label for the FEC based on
385 * its label index. The index is "acceptable" if it falls within the
386 * globally configured label block (SRGB).
388 static uint32_t fec_derive_label_from_index(struct zebra_vrf
*zvrf
,
393 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
394 && zvrf
->mpls_srgb
.start_label
395 && ((label
= zvrf
->mpls_srgb
.start_label
+ fec
->label_index
)
396 < zvrf
->mpls_srgb
.end_label
))
399 fec
->label
= MPLS_INVALID_LABEL
;
405 * There is a change for this FEC. Install or uninstall label forwarding
406 * entries, as appropriate.
408 static int fec_change_update_lsp(struct zebra_vrf
*zvrf
, zebra_fec_t
*fec
,
409 mpls_label_t old_label
)
411 struct route_table
*table
;
412 struct route_node
*rn
;
413 struct route_entry
*re
;
416 /* Uninstall label forwarding entry, if previously installed. */
417 if (old_label
!= MPLS_INVALID_LABEL
418 && old_label
!= MPLS_LABEL_IMPLICIT_NULL
)
419 lsp_uninstall(zvrf
, old_label
);
421 /* Install label forwarding entry corr. to new label, if needed. */
422 if (fec
->label
== MPLS_INVALID_LABEL
423 || fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
426 afi
= family2afi(PREFIX_FAMILY(&fec
->rn
->p
));
427 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
431 /* See if labeled route exists. */
432 rn
= route_node_lookup(table
, &fec
->rn
->p
);
436 RNODE_FOREACH_RE (rn
, re
) {
437 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
441 if (!re
|| !zebra_rib_labeled_unicast(re
))
444 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
451 * Inform about FEC to a registered client.
453 static int fec_send(zebra_fec_t
*fec
, struct zserv
*client
)
456 struct route_node
*rn
;
460 /* Get output stream. */
461 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
463 zclient_create_header(s
, ZEBRA_FEC_UPDATE
, VRF_DEFAULT
);
465 stream_putw(s
, rn
->p
.family
);
466 stream_put_prefix(s
, &rn
->p
);
467 stream_putl(s
, fec
->label
);
468 stream_putw_at(s
, 0, stream_get_endp(s
));
469 return zserv_send_message(client
, s
);
473 * Update all registered clients about this FEC. Caller should've updated
474 * FEC and ensure no duplicate updates.
476 static void fec_update_clients(zebra_fec_t
*fec
)
478 struct listnode
*node
;
479 struct zserv
*client
;
481 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
)) {
482 if (IS_ZEBRA_DEBUG_MPLS
)
483 zlog_debug("Update client %s",
484 zebra_route_string(client
->proto
));
485 fec_send(fec
, client
);
491 * Print a FEC-label binding entry.
493 static void fec_print(zebra_fec_t
*fec
, struct vty
*vty
)
495 struct route_node
*rn
;
496 struct listnode
*node
;
497 struct zserv
*client
;
501 prefix2str(&rn
->p
, buf
, BUFSIZ
);
502 vty_out(vty
, "%s\n", buf
);
503 vty_out(vty
, " Label: %s", label2str(fec
->label
, buf
, BUFSIZ
));
504 if (fec
->label_index
!= MPLS_INVALID_LABEL_INDEX
)
505 vty_out(vty
, ", Label Index: %u", fec
->label_index
);
507 if (!list_isempty(fec
->client_list
)) {
508 vty_out(vty
, " Client list:");
509 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
, client
))
510 vty_out(vty
, " %s(fd %d)",
511 zebra_route_string(client
->proto
),
518 * Locate FEC-label binding that matches with passed info.
520 static zebra_fec_t
*fec_find(struct route_table
*table
, struct prefix
*p
)
522 struct route_node
*rn
;
525 rn
= route_node_lookup(table
, p
);
529 route_unlock_node(rn
);
534 * Add a FEC. This may be upon a client registering for a binding
535 * or when a binding is configured.
537 static zebra_fec_t
*fec_add(struct route_table
*table
, struct prefix
*p
,
538 mpls_label_t label
, uint32_t flags
,
539 uint32_t label_index
)
541 struct route_node
*rn
;
546 /* Lookup (or add) route node.*/
547 rn
= route_node_get(table
, p
);
554 fec
= XCALLOC(MTYPE_FEC
, sizeof(zebra_fec_t
));
559 fec
->client_list
= list_new();
561 route_unlock_node(rn
); /* for the route_node_get */
563 fec
->label_index
= label_index
;
570 * Delete a FEC. This may be upon the last client deregistering for
571 * a FEC and no binding exists or when the binding is deleted and there
572 * are no registered clients.
574 static int fec_del(zebra_fec_t
*fec
)
576 list_delete(&fec
->client_list
);
577 fec
->rn
->info
= NULL
;
578 route_unlock_node(fec
->rn
);
579 XFREE(MTYPE_FEC
, fec
);
584 * Hash function for label.
586 static unsigned int label_hash(const void *p
)
588 const zebra_ile_t
*ile
= p
;
590 return (jhash_1word(ile
->in_label
, 0));
594 * Compare 2 LSP hash entries based on in-label.
596 static bool label_cmp(const void *p1
, const void *p2
)
598 const zebra_ile_t
*ile1
= p1
;
599 const zebra_ile_t
*ile2
= p2
;
601 return (ile1
->in_label
== ile2
->in_label
);
605 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
607 * NOTE: Looking only for connected routes right now.
609 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t
*nhlfe
,
610 struct nexthop
*nexthop
)
612 struct route_table
*table
;
613 struct prefix_ipv4 p
;
614 struct route_node
*rn
;
615 struct route_entry
*match
;
616 struct nexthop
*match_nh
;
618 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, nexthop
->vrf_id
);
622 /* Lookup nexthop in IPv4 routing table. */
623 memset(&p
, 0, sizeof(struct prefix_ipv4
));
625 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
626 p
.prefix
= nexthop
->gate
.ipv4
;
628 rn
= route_node_match(table
, (struct prefix
*)&p
);
632 route_unlock_node(rn
);
634 /* Locate a valid connected route. */
635 RNODE_FOREACH_RE (rn
, match
) {
636 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
637 || !CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
640 for (match_nh
= match
->ng
->nexthop
; match_nh
;
641 match_nh
= match_nh
->next
) {
642 if (match
->type
== ZEBRA_ROUTE_CONNECT
643 || nexthop
->ifindex
== match_nh
->ifindex
) {
644 nexthop
->ifindex
= match_nh
->ifindex
;
655 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
657 * NOTE: Looking only for connected routes right now.
659 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t
*nhlfe
,
660 struct nexthop
*nexthop
)
662 struct route_table
*table
;
663 struct prefix_ipv6 p
;
664 struct route_node
*rn
;
665 struct route_entry
*match
;
667 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, nexthop
->vrf_id
);
671 /* Lookup nexthop in IPv6 routing table. */
672 memset(&p
, 0, sizeof(struct prefix_ipv6
));
674 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
675 p
.prefix
= nexthop
->gate
.ipv6
;
677 rn
= route_node_match(table
, (struct prefix
*)&p
);
681 route_unlock_node(rn
);
683 /* Locate a valid connected route. */
684 RNODE_FOREACH_RE (rn
, match
) {
685 if ((match
->type
== ZEBRA_ROUTE_CONNECT
)
686 && !CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
)
687 && CHECK_FLAG(match
->flags
, ZEBRA_FLAG_SELECTED
))
691 if (!match
|| !match
->ng
->nexthop
)
694 nexthop
->ifindex
= match
->ng
->nexthop
->ifindex
;
700 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
702 * NOTE: Each NHLFE points to only 1 nexthop.
704 static int nhlfe_nexthop_active(zebra_nhlfe_t
*nhlfe
)
706 struct nexthop
*nexthop
;
707 struct interface
*ifp
;
708 struct zebra_ns
*zns
;
710 nexthop
= nhlfe
->nexthop
;
711 if (!nexthop
) // unexpected
714 /* Check on nexthop based on type. */
715 switch (nexthop
->type
) {
716 case NEXTHOP_TYPE_IFINDEX
:
718 * Lookup if this type is special. The
719 * NEXTHOP_TYPE_IFINDEX is a pop and
720 * forward into a different table for
721 * processing. As such this ifindex
722 * passed to us may be a VRF device
723 * which will not be in the default
724 * VRF. So let's look in all of them
726 zns
= zebra_ns_lookup(NS_DEFAULT
);
727 ifp
= if_lookup_by_index_per_ns(zns
, nexthop
->ifindex
);
728 if (ifp
&& if_is_operative(ifp
))
729 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 case NEXTHOP_TYPE_IPV4
:
734 case NEXTHOP_TYPE_IPV4_IFINDEX
:
735 if (nhlfe_nexthop_active_ipv4(nhlfe
, nexthop
))
736 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
738 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
741 case NEXTHOP_TYPE_IPV6
:
742 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
743 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
745 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
748 case NEXTHOP_TYPE_IPV6_IFINDEX
:
749 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
750 ifp
= if_lookup_by_index(nexthop
->ifindex
,
752 if (ifp
&& if_is_operative(ifp
))
753 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
755 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
757 if (nhlfe_nexthop_active_ipv6(nhlfe
, nexthop
))
758 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
760 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
768 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
772 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
773 * reachability and select the best. Multipath entries are also
774 * marked. This is invoked when an LSP scheduled for processing (due
775 * to some change) is examined.
777 static void lsp_select_best_nhlfe(zebra_lsp_t
*lsp
)
779 zebra_nhlfe_t
*nhlfe
;
781 struct nexthop
*nexthop
;
789 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
792 * First compute the best path, after checking nexthop status. We are
794 * concerned with non-deleted NHLFEs.
796 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
797 /* Clear selection flags. */
798 UNSET_FLAG(nhlfe
->flags
,
799 (NHLFE_FLAG_SELECTED
| NHLFE_FLAG_MULTIPATH
));
801 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
802 && nhlfe_nexthop_active(nhlfe
)) {
803 if (!best
|| (nhlfe
->distance
< best
->distance
))
808 lsp
->best_nhlfe
= best
;
809 if (!lsp
->best_nhlfe
)
812 /* Mark best NHLFE as selected. */
813 SET_FLAG(lsp
->best_nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
816 * If best path exists, see if there is ECMP. While doing this, note if
818 * new (uninstalled) NHLFE has been selected, an installed entry that is
819 * still selected has a change or an installed entry is to be removed.
821 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
822 int nh_chg
, nh_sel
, nh_inst
;
824 nexthop
= nhlfe
->nexthop
;
825 if (!nexthop
) // unexpected
828 if (!CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
)
829 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
830 && (nhlfe
->distance
== lsp
->best_nhlfe
->distance
)) {
831 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
832 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_MULTIPATH
);
836 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) && !changed
) {
837 nh_chg
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
838 nh_sel
= CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_SELECTED
);
840 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
842 if ((nh_sel
&& !nh_inst
)
843 || (nh_sel
&& nh_inst
&& nh_chg
)
844 || (nh_inst
&& !nh_sel
))
848 /* We have finished examining, clear changed flag. */
849 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
853 SET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
857 * Delete LSP forwarding entry from kernel, if installed. Called upon
860 static void lsp_uninstall_from_kernel(struct hash_bucket
*bucket
, void *ctxt
)
864 lsp
= (zebra_lsp_t
*)bucket
->data
;
865 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
866 (void)dplane_lsp_delete(lsp
);
870 * Schedule LSP forwarding entry for processing. Called upon changes
871 * that may impact LSPs such as nexthop / connected route changes.
873 static void lsp_schedule(struct hash_bucket
*bucket
, void *ctxt
)
877 lsp
= (zebra_lsp_t
*)bucket
->data
;
878 (void)lsp_processq_add(lsp
);
882 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
883 * any multipaths and update or delete from the kernel, as needed.
885 static wq_item_status
lsp_process(struct work_queue
*wq
, void *data
)
888 zebra_nhlfe_t
*oldbest
, *newbest
;
889 char buf
[BUFSIZ
], buf2
[BUFSIZ
];
890 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
891 enum zebra_dplane_result res
;
893 lsp
= (zebra_lsp_t
*)data
;
894 if (!lsp
) // unexpected
897 oldbest
= lsp
->best_nhlfe
;
899 /* Select best NHLFE(s) */
900 lsp_select_best_nhlfe(lsp
);
902 newbest
= lsp
->best_nhlfe
;
904 if (IS_ZEBRA_DEBUG_MPLS
) {
906 nhlfe2str(oldbest
, buf
, BUFSIZ
);
908 nhlfe2str(newbest
, buf2
, BUFSIZ
);
910 "Process LSP in-label %u oldbest %s newbest %s "
911 "flags 0x%x ecmp# %d",
912 lsp
->ile
.in_label
, oldbest
? buf
: "NULL",
913 newbest
? buf2
: "NULL", lsp
->flags
, lsp
->num_ecmp
);
916 if (!CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
)) {
917 /* Not already installed */
920 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
922 switch (dplane_lsp_add(lsp
)) {
923 case ZEBRA_DPLANE_REQUEST_QUEUED
:
924 /* Set 'installed' flag so we will know
925 * that an install is in-flight.
927 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
929 zvrf
->lsp_installs_queued
++;
931 case ZEBRA_DPLANE_REQUEST_FAILURE
:
932 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
933 "LSP Install Failure: %u",
936 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
937 zvrf
->lsp_installs
++;
942 /* Installed, may need an update and/or delete. */
944 res
= dplane_lsp_delete(lsp
);
946 /* We do some of the lsp cleanup immediately for
949 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
950 clear_nhlfe_installed(lsp
);
953 case ZEBRA_DPLANE_REQUEST_QUEUED
:
954 zvrf
->lsp_removals_queued
++;
956 case ZEBRA_DPLANE_REQUEST_FAILURE
:
957 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
958 "LSP Deletion Failure: %u",
961 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
962 zvrf
->lsp_removals
++;
965 } else if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
)) {
966 zebra_nhlfe_t
*nhlfe
;
967 struct nexthop
*nexthop
;
969 UNSET_FLAG(lsp
->flags
, LSP_FLAG_CHANGED
);
971 /* We leave the INSTALLED flag set here
972 * so we know an update in in-flight.
976 * Any NHLFE that was installed but is not
977 * selected now needs to have its flags updated.
979 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
980 nhlfe
= nhlfe
->next
) {
981 nexthop
= nhlfe
->nexthop
;
985 if (CHECK_FLAG(nhlfe
->flags
,
986 NHLFE_FLAG_INSTALLED
)
987 && !CHECK_FLAG(nhlfe
->flags
,
988 NHLFE_FLAG_SELECTED
)) {
989 UNSET_FLAG(nhlfe
->flags
,
990 NHLFE_FLAG_INSTALLED
);
991 UNSET_FLAG(nexthop
->flags
,
996 switch (dplane_lsp_update(lsp
)) {
997 case ZEBRA_DPLANE_REQUEST_QUEUED
:
998 zvrf
->lsp_installs_queued
++;
1000 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1001 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1002 "LSP Update Failure: %u",
1005 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1006 zvrf
->lsp_installs
++;
1017 * Callback upon processing completion of a LSP forwarding entry.
1019 static void lsp_processq_del(struct work_queue
*wq
, void *data
)
1021 struct zebra_vrf
*zvrf
;
1023 struct hash
*lsp_table
;
1024 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1026 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1029 lsp_table
= zvrf
->lsp_table
;
1030 if (!lsp_table
) // unexpected
1033 lsp
= (zebra_lsp_t
*)data
;
1034 if (!lsp
) // unexpected
1037 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1039 * delete LSP entry also.
1041 UNSET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1043 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1044 nhlfe_next
= nhlfe
->next
;
1045 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
))
1049 if (!lsp
->nhlfe_list
) {
1050 if (IS_ZEBRA_DEBUG_MPLS
)
1051 zlog_debug("Free LSP in-label %u flags 0x%x",
1052 lsp
->ile
.in_label
, lsp
->flags
);
1054 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1055 XFREE(MTYPE_LSP
, lsp
);
1060 * Callback upon finishing the processing of all scheduled
1061 * LSP forwarding entries.
1063 static void lsp_processq_complete(struct work_queue
*wq
)
1065 /* Nothing to do for now. */
1069 * Add LSP forwarding entry to queue for subsequent processing.
1071 static int lsp_processq_add(zebra_lsp_t
*lsp
)
1073 /* If already scheduled, exit. */
1074 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
))
1077 if (zrouter
.lsp_process_q
== NULL
) {
1078 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1079 "%s: work_queue does not exist!", __func__
);
1083 work_queue_add(zrouter
.lsp_process_q
, lsp
);
1084 SET_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
);
1089 * Callback to allocate LSP forwarding table entry.
1091 static void *lsp_alloc(void *p
)
1093 const zebra_ile_t
*ile
= p
;
1096 lsp
= XCALLOC(MTYPE_LSP
, sizeof(zebra_lsp_t
));
1099 if (IS_ZEBRA_DEBUG_MPLS
)
1100 zlog_debug("Alloc LSP in-label %u", lsp
->ile
.in_label
);
1102 return ((void *)lsp
);
1106 * Create printable string for NHLFE entry.
1108 static char *nhlfe2str(zebra_nhlfe_t
*nhlfe
, char *buf
, int size
)
1110 struct nexthop
*nexthop
;
1113 nexthop
= nhlfe
->nexthop
;
1114 switch (nexthop
->type
) {
1115 case NEXTHOP_TYPE_IPV4
:
1116 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1117 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
, size
);
1119 case NEXTHOP_TYPE_IPV6
:
1120 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1121 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, size
);
1123 case NEXTHOP_TYPE_IFINDEX
:
1124 snprintf(buf
, size
, "Ifindex: %u", nexthop
->ifindex
);
1133 * Check if NHLFE matches with search info passed.
1135 static int nhlfe_nhop_match(zebra_nhlfe_t
*nhlfe
, enum nexthop_types_t gtype
,
1136 union g_addr
*gate
, ifindex_t ifindex
)
1138 struct nexthop
*nhop
;
1141 nhop
= nhlfe
->nexthop
;
1145 if (nhop
->type
!= gtype
)
1148 switch (nhop
->type
) {
1149 case NEXTHOP_TYPE_IPV4
:
1150 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1151 cmp
= memcmp(&(nhop
->gate
.ipv4
), &(gate
->ipv4
),
1152 sizeof(struct in_addr
));
1153 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1154 cmp
= !(nhop
->ifindex
== ifindex
);
1156 case NEXTHOP_TYPE_IPV6
:
1157 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1158 cmp
= memcmp(&(nhop
->gate
.ipv6
), &(gate
->ipv6
),
1159 sizeof(struct in6_addr
));
1160 if (!cmp
&& nhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1161 cmp
= !(nhop
->ifindex
== ifindex
);
1163 case NEXTHOP_TYPE_IFINDEX
:
1164 cmp
= !(nhop
->ifindex
== ifindex
);
1175 * Locate NHLFE that matches with passed info.
1177 static zebra_nhlfe_t
*nhlfe_find(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1178 enum nexthop_types_t gtype
, union g_addr
*gate
,
1181 zebra_nhlfe_t
*nhlfe
;
1186 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1187 if (nhlfe
->type
!= lsp_type
)
1189 if (!nhlfe_nhop_match(nhlfe
, gtype
, gate
, ifindex
))
1197 * Add NHLFE. Base entry must have been created and duplicate
1200 static zebra_nhlfe_t
*nhlfe_add(zebra_lsp_t
*lsp
, enum lsp_types_t lsp_type
,
1201 enum nexthop_types_t gtype
, union g_addr
*gate
,
1202 ifindex_t ifindex
, uint8_t num_labels
,
1203 mpls_label_t labels
[])
1205 zebra_nhlfe_t
*nhlfe
;
1206 struct nexthop
*nexthop
;
1211 nhlfe
= XCALLOC(MTYPE_NHLFE
, sizeof(zebra_nhlfe_t
));
1214 nhlfe
->type
= lsp_type
;
1215 nhlfe
->distance
= lsp_distance(lsp_type
);
1217 nexthop
= nexthop_new();
1219 XFREE(MTYPE_NHLFE
, nhlfe
);
1222 nexthop_add_labels(nexthop
, lsp_type
, num_labels
, labels
);
1224 nexthop
->vrf_id
= VRF_DEFAULT
;
1225 nexthop
->type
= gtype
;
1226 switch (nexthop
->type
) {
1227 case NEXTHOP_TYPE_IPV4
:
1228 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1229 nexthop
->gate
.ipv4
= gate
->ipv4
;
1231 nexthop
->ifindex
= ifindex
;
1233 case NEXTHOP_TYPE_IPV6
:
1234 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1235 nexthop
->gate
.ipv6
= gate
->ipv6
;
1237 nexthop
->ifindex
= ifindex
;
1239 case NEXTHOP_TYPE_IFINDEX
:
1240 nexthop
->ifindex
= ifindex
;
1243 nexthop_free(nexthop
);
1244 XFREE(MTYPE_NHLFE
, nhlfe
);
1249 nhlfe
->nexthop
= nexthop
;
1250 if (lsp
->nhlfe_list
)
1251 lsp
->nhlfe_list
->prev
= nhlfe
;
1252 nhlfe
->next
= lsp
->nhlfe_list
;
1253 lsp
->nhlfe_list
= nhlfe
;
1259 * Delete NHLFE. Entry must be present on list.
1261 static int nhlfe_del(zebra_nhlfe_t
*nhlfe
)
1274 nexthop_free(nhlfe
->nexthop
);
1276 /* Unlink from LSP */
1278 nhlfe
->next
->prev
= nhlfe
->prev
;
1280 nhlfe
->prev
->next
= nhlfe
->next
;
1282 lsp
->nhlfe_list
= nhlfe
->next
;
1284 if (nhlfe
== lsp
->best_nhlfe
)
1285 lsp
->best_nhlfe
= NULL
;
1287 XFREE(MTYPE_NHLFE
, nhlfe
);
1293 * Update label for NHLFE entry.
1295 static void nhlfe_out_label_update(zebra_nhlfe_t
*nhlfe
,
1296 struct mpls_label_stack
*nh_label
)
1298 nhlfe
->nexthop
->nh_label
->label
[0] = nh_label
->label
[0];
1301 static int mpls_lsp_uninstall_all(struct hash
*lsp_table
, zebra_lsp_t
*lsp
,
1302 enum lsp_types_t type
)
1304 zebra_nhlfe_t
*nhlfe
, *nhlfe_next
;
1305 int schedule_lsp
= 0;
1308 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1309 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe_next
) {
1310 nhlfe_next
= nhlfe
->next
;
1312 /* Skip non-static NHLFEs */
1313 if (nhlfe
->type
!= type
)
1316 if (IS_ZEBRA_DEBUG_MPLS
) {
1317 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
1319 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1320 lsp
->ile
.in_label
, type
, buf
, nhlfe
->flags
);
1323 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
1324 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
1325 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
1332 /* Queue LSP for processing, if needed, else delete. */
1334 if (lsp_processq_add(lsp
))
1336 } else if (!lsp
->nhlfe_list
1337 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
1338 if (IS_ZEBRA_DEBUG_MPLS
)
1339 zlog_debug("Free LSP in-label %u flags 0x%x",
1340 lsp
->ile
.in_label
, lsp
->flags
);
1342 lsp
= hash_release(lsp_table
, &lsp
->ile
);
1343 XFREE(MTYPE_LSP
, lsp
);
1350 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1351 * If no other NHLFEs exist, the entry would be deleted.
1353 static int mpls_static_lsp_uninstall_all(struct zebra_vrf
*zvrf
,
1354 mpls_label_t in_label
)
1356 struct hash
*lsp_table
;
1357 zebra_ile_t tmp_ile
;
1361 lsp_table
= zvrf
->lsp_table
;
1365 /* If entry is not present, exit. */
1366 tmp_ile
.in_label
= in_label
;
1367 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1368 if (!lsp
|| !lsp
->nhlfe_list
)
1371 return mpls_lsp_uninstall_all(lsp_table
, lsp
, ZEBRA_LSP_STATIC
);
1374 static json_object
*nhlfe_json(zebra_nhlfe_t
*nhlfe
)
1377 json_object
*json_nhlfe
= NULL
;
1378 struct nexthop
*nexthop
= nhlfe
->nexthop
;
1380 json_nhlfe
= json_object_new_object();
1381 json_object_string_add(json_nhlfe
, "type", nhlfe_type2str(nhlfe
->type
));
1382 json_object_int_add(json_nhlfe
, "outLabel",
1383 nexthop
->nh_label
->label
[0]);
1384 json_object_int_add(json_nhlfe
, "distance", nhlfe
->distance
);
1386 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
))
1387 json_object_boolean_true_add(json_nhlfe
, "installed");
1389 switch (nexthop
->type
) {
1390 case NEXTHOP_TYPE_IPV4
:
1391 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1392 json_object_string_add(json_nhlfe
, "nexthop",
1393 inet_ntoa(nexthop
->gate
.ipv4
));
1395 case NEXTHOP_TYPE_IPV6
:
1396 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1397 json_object_string_add(
1398 json_nhlfe
, "nexthop",
1399 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1401 if (nexthop
->ifindex
)
1402 json_object_string_add(json_nhlfe
, "interface",
1403 ifindex2ifname(nexthop
->ifindex
,
1413 * Print the NHLFE for a LSP forwarding entry.
1415 static void nhlfe_print(zebra_nhlfe_t
*nhlfe
, struct vty
*vty
)
1417 struct nexthop
*nexthop
;
1420 nexthop
= nhlfe
->nexthop
;
1421 if (!nexthop
|| !nexthop
->nh_label
) // unexpected
1424 vty_out(vty
, " type: %s remote label: %s distance: %d\n",
1425 nhlfe_type2str(nhlfe
->type
),
1426 label2str(nexthop
->nh_label
->label
[0], buf
, BUFSIZ
),
1428 switch (nexthop
->type
) {
1429 case NEXTHOP_TYPE_IPV4
:
1430 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1431 vty_out(vty
, " via %s", inet_ntoa(nexthop
->gate
.ipv4
));
1432 if (nexthop
->ifindex
)
1433 vty_out(vty
, " dev %s",
1434 ifindex2ifname(nexthop
->ifindex
,
1437 case NEXTHOP_TYPE_IPV6
:
1438 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1439 vty_out(vty
, " via %s",
1440 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
, BUFSIZ
));
1441 if (nexthop
->ifindex
)
1442 vty_out(vty
, " dev %s",
1443 ifindex2ifname(nexthop
->ifindex
,
1450 CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ? " (installed)"
1456 * Print an LSP forwarding entry.
1458 static void lsp_print(zebra_lsp_t
*lsp
, void *ctxt
)
1460 zebra_nhlfe_t
*nhlfe
;
1463 vty
= (struct vty
*)ctxt
;
1465 vty_out(vty
, "Local label: %u%s\n", lsp
->ile
.in_label
,
1466 CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
) ? " (installed)"
1469 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1470 nhlfe_print(nhlfe
, vty
);
1474 * JSON objects for an LSP forwarding entry.
1476 static json_object
*lsp_json(zebra_lsp_t
*lsp
)
1478 zebra_nhlfe_t
*nhlfe
= NULL
;
1479 json_object
*json
= json_object_new_object();
1480 json_object
*json_nhlfe_list
= json_object_new_array();
1482 json_object_int_add(json
, "inLabel", lsp
->ile
.in_label
);
1484 if (CHECK_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
))
1485 json_object_boolean_true_add(json
, "installed");
1487 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
)
1488 json_object_array_add(json_nhlfe_list
, nhlfe_json(nhlfe
));
1490 json_object_object_add(json
, "nexthops", json_nhlfe_list
);
1495 /* Return a sorted linked list of the hash contents */
1496 static struct list
*hash_get_sorted_list(struct hash
*hash
, void *cmp
)
1499 struct hash_bucket
*hb
;
1500 struct list
*sorted_list
= list_new();
1502 sorted_list
->cmp
= (int (*)(void *, void *))cmp
;
1504 for (i
= 0; i
< hash
->size
; i
++)
1505 for (hb
= hash
->index
[i
]; hb
; hb
= hb
->next
)
1506 listnode_add_sort(sorted_list
, hb
->data
);
1512 * Compare two LSPs based on their label values.
1514 static int lsp_cmp(zebra_lsp_t
*lsp1
, zebra_lsp_t
*lsp2
)
1516 if (lsp1
->ile
.in_label
< lsp2
->ile
.in_label
)
1519 if (lsp1
->ile
.in_label
> lsp2
->ile
.in_label
)
1526 * Callback to allocate static LSP.
1528 static void *slsp_alloc(void *p
)
1530 const zebra_ile_t
*ile
= p
;
1533 slsp
= XCALLOC(MTYPE_SLSP
, sizeof(zebra_slsp_t
));
1535 return ((void *)slsp
);
1539 * Compare two static LSPs based on their label values.
1541 static int slsp_cmp(zebra_slsp_t
*slsp1
, zebra_slsp_t
*slsp2
)
1543 if (slsp1
->ile
.in_label
< slsp2
->ile
.in_label
)
1546 if (slsp1
->ile
.in_label
> slsp2
->ile
.in_label
)
1553 * Check if static NHLFE matches with search info passed.
1555 static int snhlfe_match(zebra_snhlfe_t
*snhlfe
, enum nexthop_types_t gtype
,
1556 union g_addr
*gate
, ifindex_t ifindex
)
1560 if (snhlfe
->gtype
!= gtype
)
1563 switch (snhlfe
->gtype
) {
1564 case NEXTHOP_TYPE_IPV4
:
1565 cmp
= memcmp(&(snhlfe
->gate
.ipv4
), &(gate
->ipv4
),
1566 sizeof(struct in_addr
));
1568 case NEXTHOP_TYPE_IPV6
:
1569 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1570 cmp
= memcmp(&(snhlfe
->gate
.ipv6
), &(gate
->ipv6
),
1571 sizeof(struct in6_addr
));
1572 if (!cmp
&& snhlfe
->gtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1573 cmp
= !(snhlfe
->ifindex
== ifindex
);
1583 * Locate static NHLFE that matches with passed info.
1585 static zebra_snhlfe_t
*snhlfe_find(zebra_slsp_t
*slsp
,
1586 enum nexthop_types_t gtype
,
1587 union g_addr
*gate
, ifindex_t ifindex
)
1589 zebra_snhlfe_t
*snhlfe
;
1594 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe
->next
) {
1595 if (!snhlfe_match(snhlfe
, gtype
, gate
, ifindex
))
1604 * Add static NHLFE. Base LSP config entry must have been created
1605 * and duplicate check done.
1607 static zebra_snhlfe_t
*snhlfe_add(zebra_slsp_t
*slsp
,
1608 enum nexthop_types_t gtype
,
1609 union g_addr
*gate
, ifindex_t ifindex
,
1610 mpls_label_t out_label
)
1612 zebra_snhlfe_t
*snhlfe
;
1617 snhlfe
= XCALLOC(MTYPE_SNHLFE
, sizeof(zebra_snhlfe_t
));
1618 snhlfe
->slsp
= slsp
;
1619 snhlfe
->out_label
= out_label
;
1620 snhlfe
->gtype
= gtype
;
1622 case NEXTHOP_TYPE_IPV4
:
1623 snhlfe
->gate
.ipv4
= gate
->ipv4
;
1625 case NEXTHOP_TYPE_IPV6
:
1626 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1627 snhlfe
->gate
.ipv6
= gate
->ipv6
;
1629 snhlfe
->ifindex
= ifindex
;
1632 XFREE(MTYPE_SNHLFE
, snhlfe
);
1636 if (slsp
->snhlfe_list
)
1637 slsp
->snhlfe_list
->prev
= snhlfe
;
1638 snhlfe
->next
= slsp
->snhlfe_list
;
1639 slsp
->snhlfe_list
= snhlfe
;
1645 * Delete static NHLFE. Entry must be present on list.
1647 static int snhlfe_del(zebra_snhlfe_t
*snhlfe
)
1654 slsp
= snhlfe
->slsp
;
1659 snhlfe
->next
->prev
= snhlfe
->prev
;
1661 snhlfe
->prev
->next
= snhlfe
->next
;
1663 slsp
->snhlfe_list
= snhlfe
->next
;
1665 snhlfe
->prev
= snhlfe
->next
= NULL
;
1666 XFREE(MTYPE_SNHLFE_IFNAME
, snhlfe
->ifname
);
1667 XFREE(MTYPE_SNHLFE
, snhlfe
);
1673 * Delete all static NHLFE entries for this LSP (in label).
1675 static int snhlfe_del_all(zebra_slsp_t
*slsp
)
1677 zebra_snhlfe_t
*snhlfe
, *snhlfe_next
;
1682 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
; snhlfe
= snhlfe_next
) {
1683 snhlfe_next
= snhlfe
->next
;
1691 * Create printable string for NHLFE configuration.
1693 static char *snhlfe2str(zebra_snhlfe_t
*snhlfe
, char *buf
, int size
)
1696 switch (snhlfe
->gtype
) {
1697 case NEXTHOP_TYPE_IPV4
:
1698 inet_ntop(AF_INET
, &snhlfe
->gate
.ipv4
, buf
, size
);
1700 case NEXTHOP_TYPE_IPV6
:
1701 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1702 inet_ntop(AF_INET6
, &snhlfe
->gate
.ipv6
, buf
, size
);
1703 if (snhlfe
->ifindex
)
1705 ifindex2ifname(snhlfe
->ifindex
, VRF_DEFAULT
),
1716 * Initialize work queue for processing changed LSPs.
1718 static int mpls_processq_init(void)
1720 zrouter
.lsp_process_q
= work_queue_new(zrouter
.master
, "LSP processing");
1721 if (!zrouter
.lsp_process_q
) {
1722 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
1723 "%s: could not initialise work queue!", __func__
);
1727 zrouter
.lsp_process_q
->spec
.workfunc
= &lsp_process
;
1728 zrouter
.lsp_process_q
->spec
.del_item_data
= &lsp_processq_del
;
1729 zrouter
.lsp_process_q
->spec
.errorfunc
= NULL
;
1730 zrouter
.lsp_process_q
->spec
.completion_func
= &lsp_processq_complete
;
1731 zrouter
.lsp_process_q
->spec
.max_retries
= 0;
1732 zrouter
.lsp_process_q
->spec
.hold
= 10;
1738 /* Public functions */
1741 * Process LSP update results from zebra dataplane.
1743 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx
*ctx
)
1745 struct zebra_vrf
*zvrf
;
1746 zebra_ile_t tmp_ile
;
1747 struct hash
*lsp_table
;
1749 zebra_nhlfe_t
*nhlfe
;
1750 struct nexthop
*nexthop
;
1751 enum dplane_op_e op
;
1752 enum zebra_dplane_result status
;
1754 op
= dplane_ctx_get_op(ctx
);
1755 status
= dplane_ctx_get_status(ctx
);
1757 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1758 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1759 ctx
, dplane_op2str(op
),
1760 dplane_ctx_get_in_label(ctx
),
1761 dplane_res2str(status
));
1764 case DPLANE_OP_LSP_INSTALL
:
1765 case DPLANE_OP_LSP_UPDATE
:
1766 /* Look for zebra LSP object */
1767 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1771 lsp_table
= zvrf
->lsp_table
;
1773 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1774 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1776 if (IS_ZEBRA_DEBUG_DPLANE
)
1777 zlog_debug("LSP ctx %p: in-label %u not found",
1778 ctx
, dplane_ctx_get_in_label(ctx
));
1782 /* TODO -- Confirm that this result is still 'current' */
1784 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1785 /* Update zebra object */
1786 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1787 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
1788 nhlfe
= nhlfe
->next
) {
1789 nexthop
= nhlfe
->nexthop
;
1793 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1794 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1797 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
1798 clear_nhlfe_installed(lsp
);
1799 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE
,
1800 "LSP Install Failure: in-label %u",
1806 case DPLANE_OP_LSP_DELETE
:
1807 if (status
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1808 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE
,
1809 "LSP Deletion Failure: in-label %u",
1810 dplane_ctx_get_in_label(ctx
));
1818 dplane_ctx_fini(&ctx
);
1822 * Process async dplane notifications.
1824 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1826 struct zebra_vrf
*zvrf
;
1827 zebra_ile_t tmp_ile
;
1828 struct hash
*lsp_table
;
1830 zebra_nhlfe_t
*nhlfe
;
1831 const zebra_nhlfe_t
*ctx_nhlfe
;
1832 struct nexthop
*nexthop
;
1833 const struct nexthop
*ctx_nexthop
;
1834 int start_count
= 0, end_count
= 0; /* Installed counts */
1835 bool changed_p
= false;
1836 bool is_debug
= (IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_MPLS
);
1839 zlog_debug("LSP dplane notif, in-label %u",
1840 dplane_ctx_get_in_label(ctx
));
1842 /* Look for zebra LSP object */
1843 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1847 lsp_table
= zvrf
->lsp_table
;
1849 tmp_ile
.in_label
= dplane_ctx_get_in_label(ctx
);
1850 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
1853 zlog_debug("dplane LSP notif: in-label %u not found",
1854 dplane_ctx_get_in_label(ctx
));
1859 * The dataplane/forwarding plane is notifying zebra about the state
1860 * of the nexthops associated with this LSP. First, we take a
1861 * pre-scan pass to determine whether the LSP has transitioned
1862 * from installed -> uninstalled. In that case, we need to have
1863 * the existing state of the LSP objects available before making
1866 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1867 char buf
[NEXTHOP_STRLEN
];
1869 nexthop
= nhlfe
->nexthop
;
1873 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1877 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1878 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1880 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1884 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1885 nexthop_same(ctx_nexthop
, nexthop
)) {
1892 nexthop2str(nexthop
, buf
, sizeof(buf
));
1894 if (ctx_nhlfe
&& ctx_nexthop
) {
1896 const char *tstr
= "";
1898 if (!CHECK_FLAG(ctx_nhlfe
->flags
,
1899 NHLFE_FLAG_INSTALLED
))
1902 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1906 /* Test zebra nhlfe install state */
1907 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1908 NHLFE_FLAG_INSTALLED
)) {
1910 if (!CHECK_FLAG(nhlfe
->flags
,
1911 NHLFE_FLAG_INSTALLED
))
1914 /* Update counter */
1918 if (CHECK_FLAG(nhlfe
->flags
,
1919 NHLFE_FLAG_INSTALLED
))
1924 /* Not mentioned in lfib set -> uninstalled */
1925 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) ||
1926 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ||
1927 CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1932 zlog_debug("LSP dplane notif: no match, nh %s",
1938 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
1939 start_count
, end_count
,
1940 changed_p
? ", changed" : "");
1943 * Has the LSP become uninstalled?
1945 if (start_count
> 0 && end_count
== 0) {
1946 /* Inform other lfibs */
1947 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_DELETE
, ctx
);
1951 * Now we take a second pass and bring the zebra
1952 * nexthop state into sync with the forwarding-plane state.
1954 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1955 char buf
[NEXTHOP_STRLEN
];
1957 nexthop
= nhlfe
->nexthop
;
1962 for (ctx_nhlfe
= dplane_ctx_get_nhlfe(ctx
);
1963 ctx_nhlfe
; ctx_nhlfe
= ctx_nhlfe
->next
) {
1965 ctx_nexthop
= ctx_nhlfe
->nexthop
;
1969 if ((ctx_nexthop
->type
== nexthop
->type
) &&
1970 nexthop_same(ctx_nexthop
, nexthop
)) {
1977 nexthop2str(nexthop
, buf
, sizeof(buf
));
1979 if (ctx_nhlfe
&& ctx_nexthop
) {
1981 /* Bring zebra nhlfe install state into sync */
1982 if (CHECK_FLAG(ctx_nhlfe
->flags
,
1983 NHLFE_FLAG_INSTALLED
)) {
1985 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1989 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
1992 if (CHECK_FLAG(ctx_nhlfe
->nexthop
->flags
,
1993 NEXTHOP_FLAG_FIB
)) {
1994 SET_FLAG(nhlfe
->nexthop
->flags
,
1995 NEXTHOP_FLAG_ACTIVE
);
1996 SET_FLAG(nhlfe
->nexthop
->flags
,
1999 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2000 NEXTHOP_FLAG_ACTIVE
);
2001 UNSET_FLAG(nhlfe
->nexthop
->flags
,
2006 /* Not mentioned in lfib set -> uninstalled */
2008 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
);
2009 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2010 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
2014 if (end_count
> 0) {
2015 SET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2018 dplane_lsp_notif_update(lsp
, DPLANE_OP_LSP_UPDATE
, ctx
);
2021 UNSET_FLAG(lsp
->flags
, LSP_FLAG_INSTALLED
);
2022 clear_nhlfe_installed(lsp
);
2026 dplane_ctx_fini(&ctx
);
2030 * Install dynamic LSP entry.
2032 int zebra_mpls_lsp_install(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2033 struct route_entry
*re
)
2035 struct route_table
*table
;
2038 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2042 /* See if there is a configured label binding for this FEC. */
2043 fec
= fec_find(table
, &rn
->p
);
2044 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2047 /* We cannot install a label forwarding entry if local label is the
2048 * implicit-null label.
2050 if (fec
->label
== MPLS_LABEL_IMPLICIT_NULL
)
2053 if (lsp_install(zvrf
, fec
->label
, rn
, re
))
2060 * Uninstall dynamic LSP entry, if any.
2062 int zebra_mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
2063 struct route_entry
*re
)
2065 struct route_table
*table
;
2068 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(&rn
->p
))];
2072 /* See if there is a configured label binding for this FEC. */
2073 fec
= fec_find(table
, &rn
->p
);
2074 if (!fec
|| fec
->label
== MPLS_INVALID_LABEL
)
2077 /* Uninstall always removes all dynamic NHLFEs. */
2078 return lsp_uninstall(zvrf
, fec
->label
);
2082 * Add an NHLFE to an LSP, return the newly-added object
2084 zebra_nhlfe_t
*zebra_mpls_lsp_add_nhlfe(zebra_lsp_t
*lsp
,
2085 enum lsp_types_t lsp_type
,
2086 enum nexthop_types_t gtype
,
2090 mpls_label_t out_labels
[])
2092 /* Just a public pass-through to the internal implementation */
2093 return nhlfe_add(lsp
, lsp_type
, gtype
, gate
, ifindex
, num_labels
,
2098 * Free an allocated NHLFE
2100 void zebra_mpls_nhlfe_del(zebra_nhlfe_t
*nhlfe
)
2102 /* Just a pass-through to the internal implementation */
2107 * Registration from a client for the label binding for a FEC. If a binding
2108 * already exists, it is informed to the client.
2109 * NOTE: If there is a manually configured label binding, that is used.
2110 * Otherwise, if a label index is specified, it means we have to allocate the
2111 * label from a locally configured label block (SRGB), if one exists and index
2112 * is acceptable. If no label index then just register the specified label.
2113 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2114 * by the calling function. Register requests with both will be rejected.
2116 int zebra_mpls_fec_register(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2117 uint32_t label
, uint32_t label_index
,
2118 struct zserv
*client
)
2120 struct route_table
*table
;
2124 bool label_change
= false;
2126 bool have_label_index
= (label_index
!= MPLS_INVALID_LABEL_INDEX
);
2127 bool is_configured_fec
= false; /* indicate statically configured FEC */
2129 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2133 if (IS_ZEBRA_DEBUG_MPLS
)
2134 prefix2str(p
, buf
, BUFSIZ
);
2136 if (label
!= MPLS_INVALID_LABEL
&& have_label_index
) {
2138 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT
,
2139 "Rejecting FEC register for %s with both label %u and Label Index %u specified, client %s",
2140 buf
, label
, label_index
,
2141 zebra_route_string(client
->proto
));
2146 fec
= fec_find(table
, p
);
2148 fec
= fec_add(table
, p
, label
, 0, label_index
);
2151 EC_ZEBRA_FEC_ADD_FAILED
,
2152 "Failed to add FEC %s upon register, client %s",
2153 buf
, zebra_route_string(client
->proto
));
2157 old_label
= MPLS_INVALID_LABEL
;
2160 /* Check if the FEC has been statically defined in the config */
2161 is_configured_fec
= fec
->flags
& FEC_FLAG_CONFIGURED
;
2162 /* Client may register same FEC with different label index. */
2164 (listnode_lookup(fec
->client_list
, client
) == NULL
);
2165 if (!new_client
&& fec
->label_index
== label_index
2166 && fec
->label
== label
)
2167 /* Duplicate register */
2170 /* Save current label, update the FEC */
2171 old_label
= fec
->label
;
2172 fec
->label_index
= label_index
;
2176 listnode_add(fec
->client_list
, client
);
2178 if (IS_ZEBRA_DEBUG_MPLS
)
2179 zlog_debug("FEC %s label%s %u %s by client %s%s", buf
,
2180 have_label_index
? " index" : "",
2181 have_label_index
? label_index
: label
,
2182 new_client
? "registered" : "updated",
2183 zebra_route_string(client
->proto
),
2185 ? ", but using statically configured label"
2188 /* If not a statically configured FEC, derive the local label
2189 * from label index or use the provided label
2191 if (!is_configured_fec
) {
2192 if (have_label_index
)
2193 fec_derive_label_from_index(zvrf
, fec
);
2197 /* If no label change, exit. */
2198 if (fec
->label
== old_label
)
2201 label_change
= true;
2204 /* If new client or label change, update client and install or uninstall
2205 * label forwarding entry as needed.
2207 /* Inform client of label, if needed. */
2208 if ((new_client
&& fec
->label
!= MPLS_INVALID_LABEL
) || label_change
) {
2209 if (IS_ZEBRA_DEBUG_MPLS
)
2210 zlog_debug("Update client label %u", fec
->label
);
2211 fec_send(fec
, client
);
2214 if (new_client
|| label_change
)
2215 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2221 * Deregistration from a client for the label binding for a FEC. The FEC
2222 * itself is deleted if no other registered clients exist and there is no
2223 * label bound to the FEC.
2225 int zebra_mpls_fec_unregister(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2226 struct zserv
*client
)
2228 struct route_table
*table
;
2232 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2236 if (IS_ZEBRA_DEBUG_MPLS
)
2237 prefix2str(p
, buf
, BUFSIZ
);
2239 fec
= fec_find(table
, p
);
2241 prefix2str(p
, buf
, BUFSIZ
);
2242 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2243 "Failed to find FEC %s upon unregister, client %s",
2244 buf
, zebra_route_string(client
->proto
));
2248 listnode_delete(fec
->client_list
, client
);
2250 if (IS_ZEBRA_DEBUG_MPLS
)
2251 zlog_debug("FEC %s unregistered by client %s", buf
,
2252 zebra_route_string(client
->proto
));
2254 /* If not a configured entry, delete the FEC if no other clients. Before
2255 * deleting, see if any LSP needs to be uninstalled.
2257 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2258 && list_isempty(fec
->client_list
)) {
2259 mpls_label_t old_label
= fec
->label
;
2260 fec
->label
= MPLS_INVALID_LABEL
; /* reset */
2261 fec_change_update_lsp(zvrf
, fec
, old_label
);
2269 * Cleanup any FECs registered by this client.
2271 static int zebra_mpls_cleanup_fecs_for_client(struct zserv
*client
)
2273 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2274 struct route_node
*rn
;
2276 struct listnode
*node
;
2277 struct zserv
*fec_client
;
2280 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2281 if (zvrf
->fec_table
[af
] == NULL
)
2284 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2285 rn
= route_next(rn
)) {
2287 if (!fec
|| list_isempty(fec
->client_list
))
2290 for (ALL_LIST_ELEMENTS_RO(fec
->client_list
, node
,
2292 if (fec_client
== client
) {
2293 listnode_delete(fec
->client_list
,
2295 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
)
2296 && list_isempty(fec
->client_list
))
2307 struct lsp_uninstall_args
{
2308 struct hash
*lsp_table
;
2309 enum lsp_types_t type
;
2313 * Cleanup MPLS labels registered by this client.
2315 static int zebra_mpls_cleanup_zclient_labels(struct zserv
*client
)
2318 struct zebra_vrf
*zvrf
;
2320 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2321 struct lsp_uninstall_args args
;
2328 args
.lsp_table
= zvrf
->lsp_table
;
2329 args
.type
= lsp_type_from_re_type(client
->proto
);
2330 hash_iterate(zvrf
->lsp_table
, mpls_lsp_uninstall_all_type
,
2334 mpls_ftn_uninstall_all(zvrf
, AFI_IP
,
2335 lsp_type_from_re_type(client
->proto
));
2336 mpls_ftn_uninstall_all(zvrf
, AFI_IP6
,
2337 lsp_type_from_re_type(client
->proto
));
2344 * Return FEC (if any) to which this label is bound.
2345 * Note: Only works for per-prefix binding and when the label is not
2347 * TODO: Currently walks entire table, can optimize later with another
2350 zebra_fec_t
*zebra_mpls_fec_for_label(struct zebra_vrf
*zvrf
,
2353 struct route_node
*rn
;
2357 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2358 if (zvrf
->fec_table
[af
] == NULL
)
2361 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2362 rn
= route_next(rn
)) {
2366 if (fec
->label
== label
)
2375 * Inform if specified label is currently bound to a FEC or not.
2377 int zebra_mpls_label_already_bound(struct zebra_vrf
*zvrf
, mpls_label_t label
)
2379 return (zebra_mpls_fec_for_label(zvrf
, label
) ? 1 : 0);
2383 * Add static FEC to label binding. If there are clients registered for this
2384 * FEC, notify them. If there are labeled routes for this FEC, install the
2385 * label forwarding entry.
2387 int zebra_mpls_static_fec_add(struct zebra_vrf
*zvrf
, struct prefix
*p
,
2388 mpls_label_t in_label
)
2390 struct route_table
*table
;
2393 mpls_label_t old_label
;
2396 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2400 if (IS_ZEBRA_DEBUG_MPLS
)
2401 prefix2str(p
, buf
, BUFSIZ
);
2403 /* Update existing FEC or create a new one. */
2404 fec
= fec_find(table
, p
);
2406 fec
= fec_add(table
, p
, in_label
, FEC_FLAG_CONFIGURED
,
2407 MPLS_INVALID_LABEL_INDEX
);
2409 prefix2str(p
, buf
, BUFSIZ
);
2410 flog_err(EC_ZEBRA_FEC_ADD_FAILED
,
2411 "Failed to add FEC %s upon config", buf
);
2415 if (IS_ZEBRA_DEBUG_MPLS
)
2416 zlog_debug("Add fec %s label %u", buf
, in_label
);
2418 fec
->flags
|= FEC_FLAG_CONFIGURED
;
2419 if (fec
->label
== in_label
)
2420 /* Duplicate config */
2423 /* Label change, update clients. */
2424 old_label
= fec
->label
;
2425 if (IS_ZEBRA_DEBUG_MPLS
)
2426 zlog_debug("Update fec %s new label %u", buf
, in_label
);
2428 fec
->label
= in_label
;
2429 fec_update_clients(fec
);
2431 /* Update label forwarding entries appropriately */
2432 ret
= fec_change_update_lsp(zvrf
, fec
, old_label
);
2439 * Remove static FEC to label binding. If there are no clients registered
2440 * for this FEC, delete the FEC; else notify clients
2441 * Note: Upon delete of static binding, if label index exists for this FEC,
2442 * client may need to be updated with derived label.
2444 int zebra_mpls_static_fec_del(struct zebra_vrf
*zvrf
, struct prefix
*p
)
2446 struct route_table
*table
;
2448 mpls_label_t old_label
;
2451 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2455 fec
= fec_find(table
, p
);
2457 prefix2str(p
, buf
, BUFSIZ
);
2458 flog_err(EC_ZEBRA_FEC_RM_FAILED
,
2459 "Failed to find FEC %s upon delete", buf
);
2463 if (IS_ZEBRA_DEBUG_MPLS
) {
2464 prefix2str(p
, buf
, BUFSIZ
);
2465 zlog_debug("Delete fec %s label %u label index %u", buf
,
2466 fec
->label
, fec
->label_index
);
2469 old_label
= fec
->label
;
2470 fec
->flags
&= ~FEC_FLAG_CONFIGURED
;
2471 fec
->label
= MPLS_INVALID_LABEL
;
2473 /* If no client exists, just delete the FEC. */
2474 if (list_isempty(fec
->client_list
)) {
2479 /* Derive the local label (from label index) or reset it. */
2480 fec_derive_label_from_index(zvrf
, fec
);
2482 /* If there is a label change, update clients. */
2483 if (fec
->label
== old_label
)
2485 fec_update_clients(fec
);
2487 /* Update label forwarding entries appropriately */
2488 return fec_change_update_lsp(zvrf
, fec
, old_label
);
2492 * Display MPLS FEC to label binding configuration (VTY command handler).
2494 int zebra_mpls_write_fec_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2496 struct route_node
*rn
;
2502 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2503 if (zvrf
->fec_table
[af
] == NULL
)
2506 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2507 rn
= route_next(rn
)) {
2514 if (!(fec
->flags
& FEC_FLAG_CONFIGURED
))
2518 prefix2str(&rn
->p
, buf
, BUFSIZ
);
2519 vty_out(vty
, "mpls label bind %s %s\n", buf
,
2520 label2str(fec
->label
, lstr
, BUFSIZ
));
2528 * Display MPLS FEC to label binding (VTY command handler).
2530 void zebra_mpls_print_fec_table(struct vty
*vty
, struct zebra_vrf
*zvrf
)
2532 struct route_node
*rn
;
2535 for (af
= AFI_IP
; af
< AFI_MAX
; af
++) {
2536 if (zvrf
->fec_table
[af
] == NULL
)
2539 for (rn
= route_top(zvrf
->fec_table
[af
]); rn
;
2540 rn
= route_next(rn
)) {
2543 fec_print(rn
->info
, vty
);
2549 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2551 void zebra_mpls_print_fec(struct vty
*vty
, struct zebra_vrf
*zvrf
,
2554 struct route_table
*table
;
2555 struct route_node
*rn
;
2557 table
= zvrf
->fec_table
[family2afi(PREFIX_FAMILY(p
))];
2562 rn
= route_node_lookup(table
, p
);
2566 route_unlock_node(rn
);
2570 fec_print(rn
->info
, vty
);
2573 static void mpls_zebra_nhg_update(struct route_entry
*re
, afi_t afi
,
2574 struct nexthop_group
*new_grp
)
2576 struct nhg_hash_entry
*nhe
;
2578 nhe
= zebra_nhg_rib_find(0, new_grp
, afi
);
2580 zebra_nhg_re_update_ref(re
, nhe
);
2583 static bool mpls_ftn_update_nexthop(int add
, struct nexthop
*nexthop
,
2584 enum lsp_types_t type
, mpls_label_t label
)
2586 if (add
&& nexthop
->nh_label_type
== ZEBRA_LSP_NONE
)
2587 nexthop_add_labels(nexthop
, type
, 1, &label
);
2588 else if (!add
&& nexthop
->nh_label_type
== type
)
2589 nexthop_del_labels(nexthop
);
2597 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2599 int mpls_ftn_update(int add
, struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2600 struct prefix
*prefix
, enum nexthop_types_t gtype
,
2601 union g_addr
*gate
, ifindex_t ifindex
, uint8_t route_type
,
2602 unsigned short route_instance
, mpls_label_t out_label
)
2604 struct route_table
*table
;
2605 struct route_node
*rn
;
2606 struct route_entry
*re
;
2607 struct nexthop
*nexthop
;
2608 struct nexthop_group new_grp
= {};
2610 afi_t afi
= family2afi(prefix
->family
);
2613 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2617 /* Lookup existing route */
2618 rn
= route_node_get(table
, prefix
);
2619 RNODE_FOREACH_RE (rn
, re
) {
2620 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2622 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2630 * Copy over current nexthops into a temporary group.
2631 * We can't just change the values here since we are hashing
2632 * on labels. We need to create a whole new group
2634 nexthop_group_copy(&new_grp
, re
->ng
);
2637 for (nexthop
= new_grp
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
2638 switch (nexthop
->type
) {
2639 case NEXTHOP_TYPE_IPV4
:
2640 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2641 if (gtype
!= NEXTHOP_TYPE_IPV4
2642 && gtype
!= NEXTHOP_TYPE_IPV4_IFINDEX
)
2644 if (!IPV4_ADDR_SAME(&nexthop
->gate
.ipv4
, &gate
->ipv4
))
2646 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
2647 && nexthop
->ifindex
!= ifindex
)
2649 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2654 case NEXTHOP_TYPE_IPV6
:
2655 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2656 if (gtype
!= NEXTHOP_TYPE_IPV6
2657 && gtype
!= NEXTHOP_TYPE_IPV6_IFINDEX
)
2659 if (!IPV6_ADDR_SAME(&nexthop
->gate
.ipv6
, &gate
->ipv6
))
2661 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
2662 && nexthop
->ifindex
!= ifindex
)
2664 if (!mpls_ftn_update_nexthop(add
, nexthop
, type
,
2675 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2676 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2678 mpls_zebra_nhg_update(re
, afi
, &new_grp
);
2683 nexthops_free(new_grp
.nexthop
);
2685 return found
? 0 : -1;
2688 int mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2689 struct prefix
*prefix
, uint8_t route_type
,
2690 unsigned short route_instance
)
2692 struct route_table
*table
;
2693 struct route_node
*rn
;
2694 struct route_entry
*re
;
2695 struct nexthop
*nexthop
;
2696 struct nexthop_group new_grp
= {};
2697 afi_t afi
= family2afi(prefix
->family
);
2700 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2704 /* Lookup existing route */
2705 rn
= route_node_get(table
, prefix
);
2706 RNODE_FOREACH_RE (rn
, re
) {
2707 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2709 if (re
->type
== route_type
&& re
->instance
== route_instance
)
2715 nexthop_group_copy(&new_grp
, re
->ng
);
2717 for (nexthop
= new_grp
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2718 nexthop_del_labels(nexthop
);
2720 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2721 SET_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
);
2723 mpls_zebra_nhg_update(re
, afi
, &new_grp
);
2725 nexthops_free(new_grp
.nexthop
);
2733 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2734 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2735 * the out-label for an existing NHLFE (update case).
2737 int mpls_lsp_install(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2738 mpls_label_t in_label
, uint8_t num_out_labels
,
2739 mpls_label_t out_labels
[], enum nexthop_types_t gtype
,
2740 union g_addr
*gate
, ifindex_t ifindex
)
2742 struct hash
*lsp_table
;
2743 zebra_ile_t tmp_ile
;
2745 zebra_nhlfe_t
*nhlfe
;
2749 lsp_table
= zvrf
->lsp_table
;
2753 /* If entry is present, exit. */
2754 tmp_ile
.in_label
= in_label
;
2755 lsp
= hash_get(lsp_table
, &tmp_ile
, lsp_alloc
);
2758 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2760 struct nexthop
*nh
= nhlfe
->nexthop
;
2763 assert(nh
->nh_label
);
2765 /* Clear deleted flag (in case it was set) */
2766 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2767 if (nh
->nh_label
->num_labels
== num_out_labels
2768 && !memcmp(nh
->nh_label
->label
, out_labels
,
2769 sizeof(mpls_label_t
) * num_out_labels
))
2773 if (IS_ZEBRA_DEBUG_MPLS
) {
2777 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2778 mpls_label2str(num_out_labels
, out_labels
, buf2
,
2780 mpls_label2str(nh
->nh_label
->num_labels
,
2781 nh
->nh_label
->label
, buf3
, sizeof(buf3
),
2785 "LSP in-label %u type %d nexthop %s "
2786 "out-label(s) changed to %s (old %s)",
2787 in_label
, type
, buf
, buf2
, buf3
);
2790 /* Update out label(s), trigger processing. */
2791 if (nh
->nh_label
->num_labels
== num_out_labels
)
2792 memcpy(nh
->nh_label
->label
, out_labels
,
2793 sizeof(mpls_label_t
) * num_out_labels
);
2795 nexthop_del_labels(nh
);
2796 nexthop_add_labels(nh
, type
, num_out_labels
,
2800 /* Add LSP entry to this nexthop */
2801 nhlfe
= nhlfe_add(lsp
, type
, gtype
, gate
, ifindex
,
2802 num_out_labels
, out_labels
);
2806 if (IS_ZEBRA_DEBUG_MPLS
) {
2809 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2810 mpls_label2str(num_out_labels
, out_labels
, buf2
,
2814 "Add LSP in-label %u type %d nexthop %s "
2816 in_label
, type
, buf
, buf2
);
2819 lsp
->addr_family
= NHLFE_FAMILY(nhlfe
);
2822 /* Mark NHLFE, queue LSP for processing. */
2823 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2824 if (lsp_processq_add(lsp
))
2831 * Uninstall a particular NHLFE in the forwarding table. If this is
2832 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2834 int mpls_lsp_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2835 mpls_label_t in_label
, enum nexthop_types_t gtype
,
2836 union g_addr
*gate
, ifindex_t ifindex
)
2838 struct hash
*lsp_table
;
2839 zebra_ile_t tmp_ile
;
2841 zebra_nhlfe_t
*nhlfe
;
2845 lsp_table
= zvrf
->lsp_table
;
2849 /* If entry is not present, exit. */
2850 tmp_ile
.in_label
= in_label
;
2851 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2854 nhlfe
= nhlfe_find(lsp
, type
, gtype
, gate
, ifindex
);
2858 if (IS_ZEBRA_DEBUG_MPLS
) {
2859 nhlfe2str(nhlfe
, buf
, BUFSIZ
);
2860 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2861 in_label
, type
, buf
, nhlfe
->flags
);
2864 /* Mark NHLFE for delete or directly delete, as appropriate. */
2865 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
)) {
2866 UNSET_FLAG(nhlfe
->flags
, NHLFE_FLAG_CHANGED
);
2867 SET_FLAG(nhlfe
->flags
, NHLFE_FLAG_DELETED
);
2868 if (lsp_processq_add(lsp
))
2873 /* Free LSP entry if no other NHLFEs and not scheduled. */
2874 if (!lsp
->nhlfe_list
2875 && !CHECK_FLAG(lsp
->flags
, LSP_FLAG_SCHEDULED
)) {
2876 if (IS_ZEBRA_DEBUG_MPLS
)
2877 zlog_debug("Free LSP in-label %u flags 0x%x",
2878 lsp
->ile
.in_label
, lsp
->flags
);
2880 lsp
= hash_release(lsp_table
, &lsp
->ile
);
2881 XFREE(MTYPE_LSP
, lsp
);
2887 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
2888 mpls_label_t in_label
)
2890 struct hash
*lsp_table
;
2891 zebra_ile_t tmp_ile
;
2895 lsp_table
= zvrf
->lsp_table
;
2899 /* If entry is not present, exit. */
2900 tmp_ile
.in_label
= in_label
;
2901 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
2905 return mpls_lsp_uninstall_all(lsp_table
, lsp
, type
);
2909 * Uninstall all NHLFEs for a particular LSP forwarding entry.
2910 * If no other NHLFEs exist, the entry would be deleted.
2912 static void mpls_lsp_uninstall_all_type(struct hash_bucket
*bucket
, void *ctxt
)
2914 struct lsp_uninstall_args
*args
= ctxt
;
2916 struct hash
*lsp_table
;
2918 lsp
= (zebra_lsp_t
*)bucket
->data
;
2919 if (!lsp
->nhlfe_list
)
2922 lsp_table
= args
->lsp_table
;
2926 mpls_lsp_uninstall_all(lsp_table
, lsp
, args
->type
);
2930 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
2933 static void mpls_ftn_uninstall_all(struct zebra_vrf
*zvrf
,
2934 int afi
, enum lsp_types_t lsp_type
)
2936 struct route_table
*table
;
2937 struct route_node
*rn
;
2938 struct route_entry
*re
;
2939 struct nexthop
*nexthop
;
2942 /* Process routes of interested address-families. */
2943 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, zvrf_id(zvrf
));
2947 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
2949 RNODE_FOREACH_RE (rn
, re
) {
2950 struct nexthop_group new_grp
= {};
2952 nexthop_group_copy(&new_grp
, re
->ng
);
2954 for (nexthop
= new_grp
.nexthop
; nexthop
;
2955 nexthop
= nexthop
->next
) {
2956 if (nexthop
->nh_label_type
!= lsp_type
)
2959 nexthop_del_labels(nexthop
);
2960 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2961 SET_FLAG(re
->status
,
2962 ROUTE_ENTRY_LABELS_CHANGED
);
2966 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
))
2967 mpls_zebra_nhg_update(re
, afi
, &new_grp
);
2969 nexthops_free(new_grp
.nexthop
);
2977 #if defined(HAVE_CUMULUS)
2979 * Check that the label values used in LSP creation are consistent. The
2980 * main criteria is that if there is ECMP, the label operation must still
2981 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2982 * to current HW restrictions.
2984 int zebra_mpls_lsp_label_consistent(struct zebra_vrf
*zvrf
,
2985 mpls_label_t in_label
,
2986 mpls_label_t out_label
,
2987 enum nexthop_types_t gtype
,
2988 union g_addr
*gate
, ifindex_t ifindex
)
2990 struct hash
*slsp_table
;
2991 zebra_ile_t tmp_ile
;
2993 zebra_snhlfe_t
*snhlfe
;
2996 slsp_table
= zvrf
->slsp_table
;
3000 /* If entry is not present, exit. */
3001 tmp_ile
.in_label
= in_label
;
3002 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
3006 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
3008 if (snhlfe
->out_label
== out_label
)
3011 /* If not only NHLFE, cannot allow label change. */
3012 if (snhlfe
!= slsp
->snhlfe_list
|| snhlfe
->next
)
3015 /* If other NHLFEs exist, label operation must match. */
3016 if (slsp
->snhlfe_list
) {
3019 cur_op
= (slsp
->snhlfe_list
->out_label
3020 == MPLS_LABEL_IMPLICIT_NULL
);
3021 new_op
= (out_label
== MPLS_LABEL_IMPLICIT_NULL
);
3022 if (cur_op
!= new_op
)
3027 /* Label values are good. */
3030 #endif /* HAVE_CUMULUS */
3033 * Add static LSP entry. This may be the first entry for this incoming label
3034 * or an additional nexthop; an existing entry may also have outgoing label
3036 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3039 int zebra_mpls_static_lsp_add(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3040 mpls_label_t out_label
,
3041 enum nexthop_types_t gtype
, union g_addr
*gate
,
3044 struct hash
*slsp_table
;
3045 zebra_ile_t tmp_ile
;
3047 zebra_snhlfe_t
*snhlfe
;
3051 slsp_table
= zvrf
->slsp_table
;
3055 /* If entry is present, exit. */
3056 tmp_ile
.in_label
= in_label
;
3057 slsp
= hash_get(slsp_table
, &tmp_ile
, slsp_alloc
);
3060 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
3062 if (snhlfe
->out_label
== out_label
)
3066 if (IS_ZEBRA_DEBUG_MPLS
) {
3067 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3069 "Upd static LSP in-label %u nexthop %s "
3070 "out-label %u (old %u)",
3071 in_label
, buf
, out_label
, snhlfe
->out_label
);
3073 snhlfe
->out_label
= out_label
;
3075 /* Add static LSP entry to this nexthop */
3076 snhlfe
= snhlfe_add(slsp
, gtype
, gate
, ifindex
, out_label
);
3080 if (IS_ZEBRA_DEBUG_MPLS
) {
3081 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3083 "Add static LSP in-label %u nexthop %s out-label %u",
3084 in_label
, buf
, out_label
);
3088 /* (Re)Install LSP in the main table. */
3089 if (mpls_lsp_install(zvrf
, ZEBRA_LSP_STATIC
, in_label
, 1, &out_label
,
3090 gtype
, gate
, ifindex
))
3097 * Delete static LSP entry. This may be the delete of one particular
3098 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3100 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3101 * LSP configuration.
3103 int zebra_mpls_static_lsp_del(struct zebra_vrf
*zvrf
, mpls_label_t in_label
,
3104 enum nexthop_types_t gtype
, union g_addr
*gate
,
3107 struct hash
*slsp_table
;
3108 zebra_ile_t tmp_ile
;
3110 zebra_snhlfe_t
*snhlfe
;
3113 slsp_table
= zvrf
->slsp_table
;
3117 /* If entry is not present, exit. */
3118 tmp_ile
.in_label
= in_label
;
3119 slsp
= hash_lookup(slsp_table
, &tmp_ile
);
3123 /* Is it delete of entire LSP or a specific NHLFE? */
3124 if (gtype
== NEXTHOP_TYPE_BLACKHOLE
) {
3125 if (IS_ZEBRA_DEBUG_MPLS
)
3126 zlog_debug("Del static LSP in-label %u", in_label
);
3128 /* Uninstall entire LSP from the main table. */
3129 mpls_static_lsp_uninstall_all(zvrf
, in_label
);
3131 /* Delete all static NHLFEs */
3132 snhlfe_del_all(slsp
);
3134 /* Find specific NHLFE, exit if not found. */
3135 snhlfe
= snhlfe_find(slsp
, gtype
, gate
, ifindex
);
3139 if (IS_ZEBRA_DEBUG_MPLS
) {
3141 snhlfe2str(snhlfe
, buf
, BUFSIZ
);
3142 zlog_debug("Del static LSP in-label %u nexthop %s",
3146 /* Uninstall LSP from the main table. */
3147 mpls_lsp_uninstall(zvrf
, ZEBRA_LSP_STATIC
, in_label
, gtype
,
3150 /* Delete static LSP NHLFE */
3154 /* Remove entire static LSP entry if no NHLFE - valid in either case
3156 if (!slsp
->snhlfe_list
) {
3157 slsp
= hash_release(slsp_table
, &tmp_ile
);
3158 XFREE(MTYPE_SLSP
, slsp
);
3165 * Schedule all MPLS label forwarding entries for processing.
3166 * Called upon changes that may affect one or more of them such as
3167 * interface or nexthop state changes.
3169 void zebra_mpls_lsp_schedule(struct zebra_vrf
*zvrf
)
3173 hash_iterate(zvrf
->lsp_table
, lsp_schedule
, NULL
);
3177 * Display MPLS label forwarding table for a specific LSP
3178 * (VTY command handler).
3180 void zebra_mpls_print_lsp(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3181 mpls_label_t label
, bool use_json
)
3183 struct hash
*lsp_table
;
3185 zebra_ile_t tmp_ile
;
3186 json_object
*json
= NULL
;
3189 lsp_table
= zvrf
->lsp_table
;
3193 /* If entry is not present, exit. */
3194 tmp_ile
.in_label
= label
;
3195 lsp
= hash_lookup(lsp_table
, &tmp_ile
);
3200 json
= lsp_json(lsp
);
3201 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3202 json
, JSON_C_TO_STRING_PRETTY
));
3203 json_object_free(json
);
3205 lsp_print(lsp
, (void *)vty
);
3209 * Display MPLS label forwarding table (VTY command handler).
3211 void zebra_mpls_print_lsp_table(struct vty
*vty
, struct zebra_vrf
*zvrf
,
3215 json_object
*json
= NULL
;
3216 zebra_lsp_t
*lsp
= NULL
;
3217 zebra_nhlfe_t
*nhlfe
= NULL
;
3218 struct listnode
*node
= NULL
;
3219 struct list
*lsp_list
= hash_get_sorted_list(zvrf
->lsp_table
, lsp_cmp
);
3222 json
= json_object_new_object();
3224 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
))
3225 json_object_object_add(
3226 json
, label2str(lsp
->ile
.in_label
, buf
, BUFSIZ
),
3229 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
3230 json
, JSON_C_TO_STRING_PRETTY
));
3231 json_object_free(json
);
3235 /* Prepare table. */
3236 tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
3237 ttable_add_row(tt
, "Inbound Label|Type|Nexthop|Outbound Label");
3238 tt
->style
.cell
.rpad
= 2;
3239 tt
->style
.corner
= '+';
3241 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
3243 for (ALL_LIST_ELEMENTS_RO(lsp_list
, node
, lsp
)) {
3244 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
;
3245 nhlfe
= nhlfe
->next
) {
3246 struct nexthop
*nexthop
;
3247 const char *out_label_str
;
3248 char nh_buf
[NEXTHOP_STRLEN
];
3250 nexthop
= nhlfe
->nexthop
;
3252 switch (nexthop
->type
) {
3253 case NEXTHOP_TYPE_IFINDEX
: {
3254 struct zebra_ns
*zns
;
3255 struct interface
*ifp
;
3257 zns
= zebra_ns_lookup(NS_DEFAULT
);
3258 ifp
= if_lookup_by_index_per_ns(
3259 zns
, nexthop
->ifindex
);
3260 snprintf(nh_buf
, sizeof(nh_buf
), "%s",
3261 ifp
? ifp
->name
: "Null");
3264 case NEXTHOP_TYPE_IPV4
:
3265 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3266 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
,
3267 nh_buf
, sizeof(nh_buf
));
3269 case NEXTHOP_TYPE_IPV6
:
3270 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3271 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
,
3272 nh_buf
, sizeof(nh_buf
));
3278 if (nexthop
->type
!= NEXTHOP_TYPE_IFINDEX
)
3279 out_label_str
= mpls_label2str(
3280 nexthop
->nh_label
->num_labels
,
3281 &nexthop
->nh_label
->label
[0],
3284 out_label_str
= "-";
3286 ttable_add_row(tt
, "%u|%s|%s|%s",
3288 nhlfe_type2str(nhlfe
->type
),
3289 nh_buf
, out_label_str
);
3293 /* Dump the generated table. */
3294 if (tt
->nrows
> 1) {
3295 char *table
= ttable_dump(tt
, "\n");
3296 vty_out(vty
, "%s\n", table
);
3297 XFREE(MTYPE_TMP
, table
);
3302 list_delete(&lsp_list
);
3306 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3308 int zebra_mpls_write_lsp_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3311 zebra_snhlfe_t
*snhlfe
;
3312 struct listnode
*node
;
3313 struct list
*slsp_list
=
3314 hash_get_sorted_list(zvrf
->slsp_table
, slsp_cmp
);
3316 for (ALL_LIST_ELEMENTS_RO(slsp_list
, node
, slsp
)) {
3317 for (snhlfe
= slsp
->snhlfe_list
; snhlfe
;
3318 snhlfe
= snhlfe
->next
) {
3322 snhlfe2str(snhlfe
, buf
, sizeof(buf
));
3323 switch (snhlfe
->out_label
) {
3324 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
3325 case MPLS_LABEL_IPV6_EXPLICIT_NULL
:
3326 strlcpy(lstr
, "explicit-null", sizeof(lstr
));
3328 case MPLS_LABEL_IMPLICIT_NULL
:
3329 strlcpy(lstr
, "implicit-null", sizeof(lstr
));
3332 sprintf(lstr
, "%u", snhlfe
->out_label
);
3336 vty_out(vty
, "mpls lsp %u %s %s\n", slsp
->ile
.in_label
,
3341 list_delete(&slsp_list
);
3342 return (zvrf
->slsp_table
->count
? 1 : 0);
3346 * Add/update global label block.
3348 int zebra_mpls_label_block_add(struct zebra_vrf
*zvrf
, uint32_t start_label
,
3351 zvrf
->mpls_srgb
.start_label
= start_label
;
3352 zvrf
->mpls_srgb
.end_label
= end_label
;
3354 /* Evaluate registered FECs to see if any get a label or not. */
3360 * Delete global label block.
3362 int zebra_mpls_label_block_del(struct zebra_vrf
*zvrf
)
3364 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3365 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3367 /* Process registered FECs to clear their local label, if needed. */
3373 * Display MPLS global label block configuration (VTY command handler).
3375 int zebra_mpls_write_label_block_config(struct vty
*vty
, struct zebra_vrf
*zvrf
)
3377 if (zvrf
->mpls_srgb
.start_label
== 0)
3380 if ((zvrf
->mpls_srgb
.start_label
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
3381 || (zvrf
->mpls_srgb
.end_label
!= MPLS_DEFAULT_MAX_SRGB_LABEL
)) {
3382 vty_out(vty
, "mpls label global-block %u %u\n",
3383 zvrf
->mpls_srgb
.start_label
, zvrf
->mpls_srgb
.end_label
);
3390 * Called when VRF becomes inactive, cleans up information but keeps
3393 void zebra_mpls_cleanup_tables(struct zebra_vrf
*zvrf
)
3395 struct zebra_vrf
*def_zvrf
;
3398 if (zvrf_id(zvrf
) == VRF_DEFAULT
)
3399 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3402 * For other vrfs, we try to remove associated LSPs; we locate
3403 * the LSPs in the default vrf.
3405 def_zvrf
= zebra_vrf_lookup_by_id(VRF_DEFAULT
);
3407 /* At shutdown, the default may be gone already */
3408 if (def_zvrf
== NULL
)
3411 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
3412 if (zvrf
->label
[afi
] != MPLS_LABEL_NONE
)
3413 lsp_uninstall(def_zvrf
, zvrf
->label
[afi
]);
3419 * Called upon process exiting, need to delete LSP forwarding
3420 * entries from the kernel.
3421 * NOTE: Currently supported only for default VRF.
3423 void zebra_mpls_close_tables(struct zebra_vrf
*zvrf
)
3425 hash_iterate(zvrf
->lsp_table
, lsp_uninstall_from_kernel
, NULL
);
3426 hash_clean(zvrf
->lsp_table
, NULL
);
3427 hash_free(zvrf
->lsp_table
);
3428 hash_clean(zvrf
->slsp_table
, NULL
);
3429 hash_free(zvrf
->slsp_table
);
3430 route_table_finish(zvrf
->fec_table
[AFI_IP
]);
3431 route_table_finish(zvrf
->fec_table
[AFI_IP6
]);
3435 * Allocate MPLS tables for this VRF and do other initialization.
3436 * NOTE: Currently supported only for default VRF.
3438 void zebra_mpls_init_tables(struct zebra_vrf
*zvrf
)
3443 hash_create(label_hash
, label_cmp
, "ZEBRA SLSP table");
3444 zvrf
->lsp_table
= hash_create(label_hash
, label_cmp
, "ZEBRA LSP table");
3445 zvrf
->fec_table
[AFI_IP
] = route_table_init();
3446 zvrf
->fec_table
[AFI_IP6
] = route_table_init();
3447 zvrf
->mpls_flags
= 0;
3448 zvrf
->mpls_srgb
.start_label
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
3449 zvrf
->mpls_srgb
.end_label
= MPLS_DEFAULT_MAX_SRGB_LABEL
;
3453 * Global MPLS initialization.
3455 void zebra_mpls_init(void)
3459 if (mpls_kernel_init() < 0) {
3460 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED
,
3461 "Disabling MPLS support (no kernel support)");
3465 if (!mpls_processq_init())
3468 hook_register(zserv_client_close
, zebra_mpls_cleanup_fecs_for_client
);
3469 hook_register(zserv_client_close
, zebra_mpls_cleanup_zclient_labels
);