2 * This is an implementation of Segment Routing
3 * as per draft draft-ietf-ospf-segment-routing-extensions-24
5 * Module name: Segment Routing
7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "libospf.h" /* for ospf interface types */
44 #include "sockunion.h" /* for inet_aton() */
52 #include "ospfd/ospfd.h"
53 #include "ospfd/ospf_interface.h"
54 #include "ospfd/ospf_ism.h"
55 #include "ospfd/ospf_asbr.h"
56 #include "ospfd/ospf_lsa.h"
57 #include "ospfd/ospf_lsdb.h"
58 #include "ospfd/ospf_neighbor.h"
59 #include "ospfd/ospf_nsm.h"
60 #include "ospfd/ospf_flood.h"
61 #include "ospfd/ospf_packet.h"
62 #include "ospfd/ospf_spf.h"
63 #include "ospfd/ospf_dump.h"
64 #include "ospfd/ospf_route.h"
65 #include "ospfd/ospf_ase.h"
66 #include "ospfd/ospf_sr.h"
67 #include "ospfd/ospf_ri.h"
68 #include "ospfd/ospf_ext.h"
69 #include "ospfd/ospf_zebra.h"
72 * Global variable to manage Segment Routing on this node.
73 * Note that all parameter values are stored in network byte order.
75 static struct ospf_sr_db OspfSR
;
76 static void ospf_sr_register_vty(void);
77 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
);
80 * Segment Routing Data Base functions
83 /* Hash function for Segment Routing entry */
84 static unsigned int sr_hash(void *p
)
86 const struct in_addr
*rid
= p
;
88 return jhash_1word(rid
->s_addr
, 0);
91 /* Compare 2 Router ID hash entries based on SR Node */
92 static int sr_cmp(const void *p1
, const void *p2
)
94 const struct sr_node
*srn
= p1
;
95 const struct in_addr
*rid
= p2
;
97 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
100 /* Functions to remove an SR Link */
101 static void del_sr_link(void *val
)
103 struct sr_link
*srl
= (struct sr_link
*)val
;
105 del_sid_nhlfe(srl
->nhlfe
[0]);
106 del_sid_nhlfe(srl
->nhlfe
[1]);
107 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
110 /* Functions to remove an SR Prefix */
111 static void del_sr_pref(void *val
)
113 struct sr_prefix
*srp
= (struct sr_prefix
*)val
;
115 del_sid_nhlfe(srp
->nhlfe
);
116 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
119 /* Allocate new Segment Routine node */
120 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
128 /* Allocate Segment Routing node memory */
129 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
131 /* Default Algorithm, SRGB and MSD */
132 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
133 new->algo
[i
] = SR_ALGORITHM_UNSET
;
135 new->srgb
.range_size
= 0;
136 new->srgb
.lower_bound
= 0;
139 /* Create Link, Prefix and Range TLVs list */
140 new->ext_link
= list_new();
141 new->ext_prefix
= list_new();
142 new->ext_link
->del
= del_sr_link
;
143 new->ext_prefix
->del
= del_sr_pref
;
145 IPV4_ADDR_COPY(&new->adv_router
, rid
);
146 new->neighbor
= NULL
;
149 if (IS_DEBUG_OSPF_SR
)
150 zlog_debug(" |- Created new SR node for %s",
151 inet_ntoa(new->adv_router
));
155 /* Delete Segment Routing node */
156 static void sr_node_del(struct sr_node
*srn
)
162 /* Clean Extended Link */
163 list_delete_and_null(&srn
->ext_link
);
165 /* Clean Prefix List */
166 list_delete_and_null(&srn
->ext_prefix
);
168 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
171 /* Get SR Node for a given nexthop */
172 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
173 struct in_addr nexthop
)
175 struct ospf_interface
*oi
= NULL
;
176 struct ospf_neighbor
*nbr
= NULL
;
177 struct listnode
*node
;
178 struct route_node
*rn
;
183 if (OspfSR
.neighbors
== NULL
)
186 if (IS_DEBUG_OSPF_SR
)
187 zlog_debug(" |- Search SR-Node for nexthop %s",
190 /* First, search neighbor Router ID for this nexthop */
192 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
193 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
195 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
207 if (IS_DEBUG_OSPF_SR
)
208 zlog_debug(" |- Found nexthop Router ID %s",
209 inet_ntoa(nbr
->router_id
));
210 /* Then, search SR Node */
211 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
217 * Segment Routing Initialization functions
220 /* Segment Routing starter function */
221 static int ospf_sr_start(struct ospf
*ospf
)
223 struct route_node
*rn
;
224 struct ospf_lsa
*lsa
;
228 if (IS_DEBUG_OSPF_SR
)
229 zlog_debug("SR (%s): Start Segment Routing", __func__
);
231 /* Initialize self SR Node */
232 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
233 (void *)sr_node_new
);
239 /* Complete & Store self SR Node */
240 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
241 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
242 srn
->algo
[0] = OspfSR
.algo
[0];
243 srn
->msd
= OspfSR
.msd
;
246 if (IS_DEBUG_OSPF_EVENT
)
247 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
249 /* Start by looking to Router Info & Extended LSA in lsdb */
250 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
251 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
) {
252 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
255 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
257 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
258 ospf_sr_ri_lsa_update(lsa
);
260 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
261 ospf_sr_ext_prefix_lsa_update(lsa
);
263 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
264 ospf_sr_ext_link_lsa_update(lsa
);
276 /* Stop Segment Routing */
277 static void ospf_sr_stop(void)
280 if (IS_DEBUG_OSPF_SR
)
281 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
284 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
285 * be remove though list_delete_and_null() call. See sr_node_del()
287 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
291 * Segment Routing initialize function
295 * @return 0 if OK, -1 otherwise
297 int ospf_sr_init(void)
301 if (IS_DEBUG_OSPF_SR
)
302 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
304 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
305 OspfSR
.enabled
= false;
306 /* Only AREA flooding is supported in this release */
307 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
309 /* Initialize SRGB, Algorithms and MSD TLVs */
310 /* Only Algorithm SPF is supported */
311 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
312 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
313 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
315 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
316 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
319 /* Initialize Hash table for neighbor SR nodes */
320 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
321 if (OspfSR
.neighbors
== NULL
)
324 /* Initialize Route Table for prefix */
325 OspfSR
.prefix
= route_table_init();
326 if (OspfSR
.prefix
== NULL
)
329 /* Register Segment Routing VTY command */
330 ospf_sr_register_vty();
337 * Segment Routing termination function
342 void ospf_sr_term(void)
345 /* Stop Segment Routing */
348 /* Clear SR Node Table */
349 if (OspfSR
.neighbors
)
350 hash_free(OspfSR
.neighbors
);
352 /* Clear Prefix Table */
354 route_table_finish(OspfSR
.prefix
);
356 OspfSR
.enabled
= false;
361 * Segment Routing finish function
366 void ospf_sr_finish(void)
368 /* Stop Segment Routing */
371 OspfSR
.enabled
= false;
375 * Following functions are used to manipulate the
376 * Next Hop Label Forwarding entry (NHLFE)
379 /* Compute label from index */
380 static mpls_label_t
index2label(uint32_t index
, struct sr_srgb srgb
)
384 label
= srgb
.lower_bound
+ index
;
385 if (label
> (srgb
.lower_bound
+ srgb
.range_size
))
386 return MPLS_INVALID_LABEL
;
391 /* Get neighbor full structure from address */
392 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
395 struct ospf_neighbor
*nbr
;
396 struct ospf_interface
*oi
;
397 struct listnode
*node
;
398 struct route_node
*rn
;
404 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
405 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
408 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
410 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
411 route_unlock_node(rn
);
418 /* Get OSPF Path from address */
419 static struct ospf_path
*get_nexthop_by_addr(struct ospf
*top
,
420 struct prefix_ipv4 p
)
422 struct ospf_route
* or ;
423 struct ospf_path
*path
;
424 struct listnode
*node
;
425 struct route_node
*rn
;
431 if (IS_DEBUG_OSPF_SR
)
432 zlog_debug(" |- Search Nexthop for prefix %s/%u",
433 inet_ntoa(p
.prefix
), p
.prefixlen
);
435 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
438 * Check if we found an OSPF route. May be NULL if SPF has not
439 * yet populate routing table for this prefix.
444 route_unlock_node(rn
);
449 /* Then search path from this route */
450 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
))
451 if (path
->nexthop
.s_addr
!= INADDR_ANY
|| path
->ifindex
!= 0)
457 /* Compute NHLFE entry for Extended Link */
458 static int compute_link_nhlfe(struct sr_link
*srl
)
460 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
461 struct ospf_neighbor
*nh
;
464 if (IS_DEBUG_OSPF_SR
)
465 zlog_debug(" |- Compute NHLFE for link %s/%u",
466 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
467 srl
->nhlfe
[0].prefv4
.prefixlen
);
469 /* First determine the OSPF Neighbor */
470 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
472 /* Neighbor could be not found when OSPF Adjacency just fire up
473 * because SPF don't yet populate routing table. This NHLFE will
474 * be fixed later when SR SPF schedule will be called.
479 if (IS_DEBUG_OSPF_SR
)
480 zlog_debug(" |- Found nexthop NHLFE %s",
481 inet_ntoa(nh
->router_id
));
483 /* Set ifindex for this neighbor */
484 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
485 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
487 /* Update neighbor address for LAN_ADJ_SID */
488 if (srl
->type
== LAN_ADJ_SID
) {
489 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
490 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
493 /* Set Input & Output Label */
494 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
495 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
497 srl
->nhlfe
[0].label_in
=
498 index2label(srl
->sid
[0], srl
->srn
->srgb
);
499 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
500 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
502 srl
->nhlfe
[1].label_in
=
503 index2label(srl
->sid
[1], srl
->srn
->srgb
);
505 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
506 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
513 * Compute NHLFE entry for Extended Prefix
515 * @param srp - Segment Routing Prefix
517 * @return -1 if next hop is not found, 0 if nexthop has not changed
520 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
522 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
523 struct ospf_path
*nh
= NULL
;
524 struct sr_node
*srnext
;
527 if (IS_DEBUG_OSPF_SR
)
528 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
529 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
530 srp
->nhlfe
.prefv4
.prefixlen
);
532 /* First determine the nexthop */
533 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
535 /* Nexthop could be not found when OSPF Adjacency just fire up
536 * because SPF don't yet populate routing table. This NHLFE will
537 * be fixed later when SR SPF schedule will be called.
542 /* Check if NextHop has changed when call after running a new SPF */
543 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
544 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
547 if (IS_DEBUG_OSPF_SR
)
548 zlog_debug(" |- Found new next hop for this NHLFE: %s",
549 inet_ntoa(nh
->nexthop
));
552 * Get SR-Node for this nexthop. Could be not yet available
553 * as Extende Link / Prefix and Router Information are flooded
554 * after LSA Type 1 & 2 which populate the OSPF Route Table
556 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
560 /* And store this information for later update if SR Node is found */
561 srnext
->neighbor
= OspfSR
.self
;
562 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
565 srp
->nexthop
= srnext
;
568 * SR Node could be known, but SRGB could be not initialize
569 * This is due to the fact that Extended Link / Prefix could
570 * be received before corresponding Router Information LSA
572 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
573 || (srnext
->srgb
.range_size
== 0))
576 if (IS_DEBUG_OSPF_SR
)
577 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
578 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
579 inet_ntoa(srnext
->adv_router
));
581 /* Set ip addr & ifindex for this neighbor */
582 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
583 srp
->nhlfe
.ifindex
= nh
->ifindex
;
585 /* Compute Input Label with self SRGB */
586 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
588 * and Output Label with Next hop SR Node SRGB or Implicit Null label
589 * if next hop is the destination and request PHP
591 if ((srp
->nexthop
== NULL
)
592 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
593 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
594 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
595 srp
->nhlfe
.label_out
= srp
->sid
;
597 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
599 if (IS_DEBUG_OSPF_SR
)
600 zlog_debug(" |- Computed new labels in: %u out: %u",
601 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
607 /* Send MPLS Label entry to Zebra for installation or deletion */
608 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
616 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
617 stream_putc(s
, ZEBRA_LSP_SR
);
618 /* OSPF Segment Routing currently support only IPv4 */
619 stream_putl(s
, nhlfe
.prefv4
.family
);
620 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
621 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
622 stream_put_in_addr(s
, &nhlfe
.nexthop
);
623 stream_putl(s
, nhlfe
.ifindex
);
624 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
625 stream_putl(s
, nhlfe
.label_in
);
626 stream_putl(s
, nhlfe
.label_out
);
628 /* Put length at the first point of the stream. */
629 stream_putw_at(s
, 0, stream_get_endp(s
));
631 if (IS_DEBUG_OSPF_SR
)
632 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
633 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
634 nhlfe
.label_in
, nhlfe
.label_out
,
635 inet_ntoa(nhlfe
.prefv4
.prefix
),
636 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
638 return zclient_send_message(zclient
);
641 /* Request zebra to install/remove FEC in FIB */
642 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
644 struct zapi_route api
;
645 struct zapi_nexthop
*api_nh
;
647 /* Support only IPv4 */
648 if (nhlfe
.prefv4
.family
!= AF_INET
)
651 memset(&api
, 0, sizeof(api
));
652 api
.vrf_id
= VRF_DEFAULT
;
653 api
.type
= ZEBRA_ROUTE_OSPF
;
654 api
.safi
= SAFI_UNICAST
;
655 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
657 if (cmd
== ZEBRA_ROUTE_ADD
) {
659 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
660 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
662 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
663 api_nh
= &api
.nexthops
[0];
664 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
665 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
666 api_nh
->ifindex
= nhlfe
.ifindex
;
668 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
669 api_nh
->labels
[0] = nhlfe
.label_out
;
670 api_nh
->label_num
= 1;
671 api_nh
->vrf_id
= VRF_DEFAULT
;
675 if (IS_DEBUG_OSPF_SR
)
676 zlog_debug(" |- %s FEC %u for %s/%u via %u",
677 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
678 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
679 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
681 return zclient_route_send(cmd
, zclient
, &api
);
684 /* Add new NHLFE entry for SID */
685 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
687 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
688 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
689 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
690 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
694 /* Remove NHLFE entry for SID */
695 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
697 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
698 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
699 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
700 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
704 /* Update NHLFE entry for SID */
705 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
713 * Functions to parse and get Extended Link / Prefix
717 /* Extended Link SubTLVs Getter */
718 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
722 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
723 struct ext_subtlv_adj_sid
*adj_sid
;
724 struct ext_subtlv_lan_adj_sid
*lan_sid
;
725 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
727 struct tlv_header
*sub_tlvh
;
728 uint16_t length
= 0, sum
= 0, i
= 0;
730 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
732 /* Initialize TLV browsing */
733 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
734 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
735 + EXT_TLV_LINK_SIZE
);
736 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
737 switch (ntohs(sub_tlvh
->type
)) {
738 case EXT_SUBTLV_ADJ_SID
:
739 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
741 i
= CHECK_FLAG(adj_sid
->flags
,
742 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
745 srl
->flags
[i
] = adj_sid
->flags
;
746 if (CHECK_FLAG(adj_sid
->flags
,
747 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
748 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
750 srl
->sid
[i
] = ntohl(adj_sid
->value
);
751 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
753 case EXT_SUBTLV_LAN_ADJ_SID
:
754 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
755 srl
->type
= LAN_ADJ_SID
;
756 i
= CHECK_FLAG(lan_sid
->flags
,
757 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
760 srl
->flags
[i
] = lan_sid
->flags
;
761 if (CHECK_FLAG(lan_sid
->flags
,
762 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
763 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
765 srl
->sid
[i
] = ntohl(lan_sid
->value
);
766 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
767 &lan_sid
->neighbor_id
);
769 case EXT_SUBTLV_RMT_ITF_ADDR
:
770 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
771 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
772 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
777 sum
+= TLV_SIZE(sub_tlvh
);
780 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
781 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
782 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
783 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
784 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
785 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
786 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
787 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
789 if (IS_DEBUG_OSPF_SR
) {
790 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
791 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
792 srl
->nhlfe
[0].prefv4
.prefixlen
);
793 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
794 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
795 srl
->nhlfe
[1].prefv4
.prefixlen
);
801 /* Extended Prefix SubTLVs Getter */
802 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
805 struct sr_prefix
*srp
;
806 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
807 struct ext_subtlv_prefix_sid
*psid
;
809 struct tlv_header
*sub_tlvh
;
810 uint16_t length
= 0, sum
= 0;
812 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
814 /* Initialize TLV browsing */
815 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
816 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
817 + EXT_TLV_PREFIX_SIZE
);
818 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
819 switch (ntohs(sub_tlvh
->type
)) {
820 case EXT_SUBTLV_PREFIX_SID
:
821 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
822 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
823 zlog_err("SR (%s): Unsupported Algorithm",
825 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
828 srp
->type
= PREF_SID
;
829 srp
->flags
= psid
->flags
;
830 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
831 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
833 srp
->sid
= ntohl(psid
->value
);
834 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
836 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
837 srp
->nhlfe
.prefv4
.family
= AF_INET
;
838 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
843 sum
+= TLV_SIZE(sub_tlvh
);
846 if (IS_DEBUG_OSPF_SR
)
847 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
848 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
849 srp
->nhlfe
.prefv4
.prefixlen
);
854 * Functions to manipulate Segment Routing Link & Prefix structures
857 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
858 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
860 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
861 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
862 && (srl1
->flags
[1] == srl2
->flags
[1]))
868 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
869 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
871 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
877 /* Update Segment Link of given Segment Routing Node */
878 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
881 struct listnode
*node
;
886 if ((srn
== NULL
) || (srl
== NULL
))
889 if (IS_DEBUG_OSPF_SR
)
890 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
892 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
893 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
894 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
895 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
898 /* Search for existing Segment Link */
899 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
900 if (lk
->instance
== srl
->instance
) {
905 if (IS_DEBUG_OSPF_SR
)
906 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
907 found
? "Update" : "Add",
908 GET_OPAQUE_ID(srl
->instance
),
909 inet_ntoa(srn
->adv_router
));
911 /* if not found, add new Segment Link and install NHLFE */
913 /* Complete SR-Link and add it to SR-Node list */
915 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
916 listnode_add(srn
->ext_link
, srl
);
917 /* Try to set MPLS table */
918 if (compute_link_nhlfe(srl
)) {
919 add_sid_nhlfe(srl
->nhlfe
[0]);
920 add_sid_nhlfe(srl
->nhlfe
[1]);
923 if (sr_link_cmp(lk
, srl
)) {
924 if (compute_link_nhlfe(srl
)) {
925 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
926 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
927 /* Replace Segment List */
928 listnode_delete(srn
->ext_link
, lk
);
929 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
931 IPV4_ADDR_COPY(&srl
->adv_router
,
933 listnode_add(srn
->ext_link
, srl
);
935 /* New NHLFE was not found.
936 * Just free the SR Link
938 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
942 * This is just an LSA refresh.
943 * Stop processing and free SR Link
945 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
950 /* Update Segment Prefix of given Segment Routing Node */
951 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
954 struct listnode
*node
;
955 struct sr_prefix
*pref
;
959 if (srn
== NULL
|| srp
== NULL
)
962 if (IS_DEBUG_OSPF_SR
)
963 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
965 /* Process only Global Prefix SID */
966 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
969 /* Search for existing Segment Prefix */
970 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
971 if (pref
->instance
== srp
->instance
) {
976 if (IS_DEBUG_OSPF_SR
)
977 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
978 found
? "Update" : "Add",
979 GET_OPAQUE_ID(srp
->instance
),
980 inet_ntoa(srn
->adv_router
));
982 /* if not found, add new Segment Prefix and install NHLFE */
984 /* Complete SR-Prefix and add it to SR-Node list */
986 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
987 listnode_add(srn
->ext_prefix
, srp
);
988 /* Try to set MPLS table */
989 if (compute_prefix_nhlfe(srp
) == 1)
990 add_sid_nhlfe(srp
->nhlfe
);
992 if (sr_prefix_cmp(pref
, srp
)) {
993 if (compute_prefix_nhlfe(srp
) == 1) {
994 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
995 /* Replace Segment Prefix */
996 listnode_delete(srn
->ext_prefix
, pref
);
997 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
999 IPV4_ADDR_COPY(&srp
->adv_router
,
1001 listnode_add(srn
->ext_prefix
, srp
);
1003 /* New NHLFE was not found.
1004 * Just free the SR Prefix
1006 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1009 /* This is just an LSA refresh.
1010 * Stop processing and free SR Prefix
1012 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1018 * When change the FRR Self SRGB, update the NHLFE Input Label
1019 * for all Extended Prefix with SID index through hash_iterate()
1021 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1023 struct listnode
*node
;
1024 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1025 struct sr_prefix
*srp
;
1026 struct sr_nhlfe
new;
1028 /* Process Every Extended Prefix for this SR-Node */
1029 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1030 /* Process Self SRN only if NO-PHP is requested */
1031 if ((srn
== OspfSR
.self
)
1032 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1035 /* Process only SID Index */
1036 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1039 /* OK. Compute new NHLFE */
1040 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1041 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1042 /* Update MPLS LFIB */
1043 update_sid_nhlfe(srp
->nhlfe
, new);
1044 /* Finally update Input Label */
1045 srp
->nhlfe
.label_in
= new.label_in
;
1050 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1051 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1053 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1055 struct listnode
*node
;
1056 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1057 struct sr_node
*srnext
= (struct sr_node
*)args
;
1058 struct sr_prefix
*srp
;
1059 struct sr_nhlfe
new;
1061 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1062 /* Process only SID Index for next hop without PHP */
1063 if ((srp
->nexthop
== NULL
)
1064 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1066 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1067 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1068 update_sid_nhlfe(srp
->nhlfe
, new);
1069 srp
->nhlfe
.label_out
= new.label_out
;
1074 * Following functions are call when new Segment Routing LSA are received
1075 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1076 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1077 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1080 /* Update Segment Routing from Router Information LSA */
1081 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1083 struct sr_node
*srn
;
1084 struct tlv_header
*tlvh
;
1085 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1086 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1087 struct ri_sr_tlv_sr_algorithm
*algo
;
1088 struct sr_srgb srgb
;
1089 uint16_t length
= 0, sum
= 0;
1091 if (IS_DEBUG_OSPF_SR
)
1093 "SR (%s): Process Router "
1094 "Information LSA 4.0.0.%u from %s",
1095 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1096 inet_ntoa(lsah
->adv_router
));
1099 if (IS_LSA_SELF(lsa
))
1102 if (OspfSR
.neighbors
== NULL
) {
1103 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1107 /* Get SR Node in hash table from Router ID */
1108 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1109 (void *)sr_node_new
);
1113 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1118 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1120 "SR (%s): Abort! Wrong "
1121 "LSA ID 4.0.0.%u for SR node %s/%u",
1122 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1123 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1127 /* Collect Router Information Sub TLVs */
1128 /* Initialize TLV browsing */
1129 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1130 srgb
.range_size
= 0;
1131 srgb
.lower_bound
= 0;
1133 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1134 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1135 switch (ntohs(tlvh
->type
)) {
1136 case RI_SR_TLV_SR_ALGORITHM
:
1137 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1140 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1141 srn
->algo
[i
] = algo
->value
[0];
1142 for (; i
< ALGORITHM_COUNT
; i
++)
1143 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1144 sum
+= TLV_SIZE(tlvh
);
1146 case RI_SR_TLV_SID_LABEL_RANGE
:
1147 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1148 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1150 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1151 sum
+= TLV_SIZE(tlvh
);
1153 case RI_SR_TLV_NODE_MSD
:
1154 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1155 sum
+= TLV_SIZE(tlvh
);
1158 sum
+= TLV_SIZE(tlvh
);
1163 /* Check that we collect mandatory parameters */
1164 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1165 || srgb
.lower_bound
== 0) {
1166 zlog_warn("SR (%s): Missing mandatory parameters. Abort!",
1168 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1169 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1173 /* Check if it is a new SR Node or not */
1174 if (srn
->instance
== 0) {
1176 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1178 srn
->srgb
.range_size
= srgb
.range_size
;
1179 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1182 /* Check if SRGB has changed */
1183 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1184 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1185 srn
->srgb
.range_size
= srgb
.range_size
;
1186 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1187 /* Update NHLFE if it is a neighbor SR node */
1188 if (srn
->neighbor
== OspfSR
.self
)
1189 hash_iterate(OspfSR
.neighbors
,
1190 (void (*)(struct hash_backet
*,
1191 void *))update_out_nhlfe
,
1197 * Delete SR Node entry in hash table information corresponding to an expired
1198 * Router Information LSA
1200 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1202 struct sr_node
*srn
;
1203 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1205 if (IS_DEBUG_OSPF_SR
)
1206 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1207 __func__
, inet_ntoa(lsah
->adv_router
),
1208 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1211 if (OspfSR
.neighbors
== NULL
) {
1212 zlog_err("SR (%s): Abort! no valid SR Data Base", __func__
);
1216 /* Release Router ID entry in SRDB hash table */
1217 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1221 zlog_err("SR (%s): Abort! no entry in SRDB for SR Node %s",
1222 __func__
, inet_ntoa(lsah
->adv_router
));
1226 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1227 zlog_err("SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1228 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1229 inet_ntoa(lsah
->adv_router
));
1233 /* Remove SR node */
1237 /* Update Segment Routing from Extended Link LSA */
1238 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1240 struct sr_node
*srn
;
1241 struct tlv_header
*tlvh
;
1242 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1243 struct sr_link
*srl
;
1245 uint16_t length
, sum
;
1247 if (IS_DEBUG_OSPF_SR
)
1249 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1250 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1251 inet_ntoa(lsah
->adv_router
));
1254 if (OspfSR
.neighbors
== NULL
) {
1255 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1259 /* Get SR Node in hash table from Router ID */
1260 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1261 (void *)&(lsah
->adv_router
),
1262 (void *)sr_node_new
);
1266 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1271 /* Initialize TLV browsing */
1272 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1274 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1275 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1276 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1277 /* Got Extended Link information */
1278 srl
= get_ext_link_sid(tlvh
);
1279 /* Update SID if not null */
1281 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1282 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1285 sum
+= TLV_SIZE(tlvh
);
1289 /* Delete Segment Routing from Extended Link LSA */
1290 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1292 struct listnode
*node
;
1293 struct sr_link
*srl
;
1294 struct sr_node
*srn
;
1295 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1296 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1298 if (IS_DEBUG_OSPF_SR
)
1299 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1300 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1301 inet_ntoa(lsah
->adv_router
));
1304 if (OspfSR
.neighbors
== NULL
) {
1305 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1309 /* Search SR Node in hash table from Router ID */
1310 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1311 (void *)&(lsah
->adv_router
));
1314 * SR-Node may be NULL if it has been remove previously when
1315 * processing Router Information LSA deletion
1318 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1319 __func__
, inet_ntoa(lsah
->adv_router
));
1323 /* Search for corresponding Segment Link */
1324 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1325 if (srl
->instance
== instance
)
1328 /* Remove Segment Link if found */
1329 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1330 del_sid_nhlfe(srl
->nhlfe
[0]);
1331 del_sid_nhlfe(srl
->nhlfe
[1]);
1332 listnode_delete(srn
->ext_link
, srl
);
1333 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1336 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1338 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1339 inet_ntoa(lsah
->adv_router
));
1343 /* Update Segment Routing from Extended Prefix LSA */
1344 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1346 struct sr_node
*srn
;
1347 struct tlv_header
*tlvh
;
1348 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1349 struct sr_prefix
*srp
;
1351 uint16_t length
, sum
;
1353 if (IS_DEBUG_OSPF_SR
)
1355 "SR (%s): Process Extended Prefix LSA "
1357 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1358 inet_ntoa(lsah
->adv_router
));
1361 if (OspfSR
.neighbors
== NULL
) {
1362 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1366 /* Get SR Node in hash table from Router ID */
1367 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1368 (void *)&(lsah
->adv_router
),
1369 (void *)sr_node_new
);
1373 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1378 /* Initialize TLV browsing */
1379 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1381 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1382 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1383 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1384 /* Got Extended Link information */
1385 srp
= get_ext_prefix_sid(tlvh
);
1386 /* Update SID if not null */
1388 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1389 update_ext_prefix_sid(srn
, srp
);
1392 sum
+= TLV_SIZE(tlvh
);
1396 /* Delete Segment Routing from Extended Prefix LSA */
1397 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1399 struct listnode
*node
;
1400 struct sr_prefix
*srp
;
1401 struct sr_node
*srn
;
1402 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1403 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1405 if (IS_DEBUG_OSPF_SR
)
1407 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1408 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1409 inet_ntoa(lsah
->adv_router
));
1412 if (OspfSR
.neighbors
== NULL
) {
1413 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1417 /* Search SR Node in hash table from Router ID */
1418 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1419 (void *)&(lsah
->adv_router
));
1422 * SR-Node may be NULL if it has been remove previously when
1423 * processing Router Information LSA deletion
1426 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1427 __func__
, inet_ntoa(lsah
->adv_router
));
1431 /* Search for corresponding Segment Link */
1432 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1433 if (srp
->instance
== instance
)
1436 /* Remove Segment Link if found */
1437 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1438 del_sid_nhlfe(srp
->nhlfe
);
1439 listnode_delete(srn
->ext_link
, srp
);
1440 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1443 "SR (%s): Didn't found corresponding SR Prefix "
1444 "7.0.0.%u for SR Node %s",
1445 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1446 inet_ntoa(lsah
->adv_router
));
1450 /* Get Label for Extended Link SID */
1451 /* TODO: To be replace by Zebra Label Manager */
1452 uint32_t get_ext_link_label_value(void)
1454 static uint32_t label
= ADJ_SID_MIN
- 1;
1456 if (label
< ADJ_SID_MAX
)
1463 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1464 * complete initial CLI command at startutp.
1466 * @param ifp - Loopback interface
1467 * @param pref - Prefix address of this interface
1471 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1473 struct listnode
*node
;
1474 struct sr_prefix
*srp
;
1477 if ((ifp
== NULL
) || (p
== NULL
))
1481 * Search if there is a Segment Prefix that correspond to this
1482 * interface or prefix, and update it if found
1484 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1485 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1486 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1488 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1490 /* Update Interface & Prefix info */
1491 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1492 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1494 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1495 srp
->nhlfe
.prefv4
.family
= p
->family
;
1496 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1498 /* OK. Let's Schedule Extended Prefix LSA */
1499 srp
->instance
= ospf_ext_schedule_prefix_index(
1500 ifp
, srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1502 /* Install NHLFE if NO-PHP is requested */
1503 if (CHECK_FLAG(srp
->flags
,
1504 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1505 srp
->nhlfe
.label_in
= index2label(
1506 srp
->sid
, OspfSR
.self
->srgb
);
1507 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1508 add_sid_nhlfe(srp
->nhlfe
);
1515 * Following functions are used to update MPLS LFIB after a SPF run
1518 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1521 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1522 struct listnode
*node
;
1523 struct sr_prefix
*srp
;
1524 struct sr_nhlfe old
;
1531 if (IS_DEBUG_OSPF_SR
)
1532 zlog_debug(" |- Update Prefix for SR Node %s",
1533 inet_ntoa(srn
->adv_router
));
1535 /* Skip Self SR Node */
1536 if (srn
== OspfSR
.self
)
1539 /* Update Extended Prefix */
1540 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1542 /* Backup current NHLFE */
1543 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1545 /* Compute the new NHLFE */
1546 rc
= compute_prefix_nhlfe(srp
);
1548 /* Check computation result */
1550 /* next hop is not know, remove old NHLFE to avoid loop */
1552 del_sid_nhlfe(srp
->nhlfe
);
1554 /* next hop has not changed, skip it */
1557 /* there is a new next hop, update NHLFE */
1559 update_sid_nhlfe(old
, srp
->nhlfe
);
1567 static int ospf_sr_update_schedule(struct thread
*t
)
1571 struct timeval start_time
, stop_time
;
1573 ospf
= THREAD_ARG(t
);
1574 ospf
->t_sr_update
= NULL
;
1579 monotime(&start_time
);
1581 if (IS_DEBUG_OSPF_SR
)
1582 zlog_debug("SR (%s): Start SPF update", __func__
);
1584 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1585 void *))ospf_sr_nhlfe_update
,
1588 monotime(&stop_time
);
1590 if (IS_DEBUG_OSPF_SR
)
1591 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld\n",
1593 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1594 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1596 OspfSR
.update
= false;
1600 #define OSPF_SR_UPDATE_INTERVAL 1
1602 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1608 /* Check if an update is not alreday engage */
1612 OspfSR
.update
= true;
1614 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1615 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1619 * --------------------------------------
1620 * Followings are vty command functions.
1621 * --------------------------------------
1625 * Segment Routing Router configuration
1627 * Must be centralize as it concerns both Extended Link/Prefix LSA
1628 * and Router Information LSA. Choose to call it from Extended Prefix
1629 * write_config() call back.
1631 * @param vty VTY output
1635 void ospf_sr_config_write_router(struct vty
*vty
)
1637 struct listnode
*node
;
1638 struct sr_prefix
*srp
;
1640 if (OspfSR
.enabled
) {
1641 vty_out(vty
, " segment-routing on\n");
1643 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1644 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1645 vty_out(vty
, " segment-routing global-block %u %u\n",
1646 OspfSR
.srgb
.lower_bound
,
1647 OspfSR
.srgb
.lower_bound
+ OspfSR
.srgb
.range_size
1650 if (OspfSR
.msd
!= 0)
1651 vty_out(vty
, " segment-routing node-msd %u\n",
1654 if (OspfSR
.self
!= NULL
) {
1655 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1658 " segment-routing prefix %s/%u "
1660 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1661 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1662 CHECK_FLAG(srp
->flags
,
1663 EXT_SUBTLV_PREFIX_SID_NPFLG
)
1671 DEFUN(ospf_sr_enable
,
1673 "segment-routing on",
1675 "Enable Segment Routing\n")
1678 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1683 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1685 "Segment Routing is only supported in default "
1687 return CMD_WARNING_CONFIG_FAILED
;
1690 if (IS_DEBUG_OSPF_EVENT
)
1691 zlog_debug("SR: Segment Routing: OFF -> ON");
1693 /* Start Segment Routing */
1694 OspfSR
.enabled
= true;
1695 if (!ospf_sr_start(ospf
)) {
1696 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1700 /* Set Router Information SR parameters */
1701 if (IS_DEBUG_OSPF_EVENT
)
1702 zlog_debug("SR: Activate SR for Router Information LSA");
1704 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1706 /* Update Ext LSA */
1707 if (IS_DEBUG_OSPF_EVENT
)
1708 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1710 ospf_ext_update_sr(true);
1715 DEFUN (no_ospf_sr_enable
,
1716 no_ospf_sr_enable_cmd
,
1717 "no segment-routing [on]",
1720 "Disable Segment Routing\n")
1723 if (!OspfSR
.enabled
)
1726 if (IS_DEBUG_OSPF_EVENT
)
1727 zlog_debug("SR: Segment Routing: ON -> OFF");
1729 /* Start by Disabling Extended Link & Prefix LSA */
1730 ospf_ext_update_sr(false);
1732 /* then, disable Router Information SR parameters */
1733 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1735 /* Finally, stop Segment Routing */
1737 OspfSR
.enabled
= false;
1742 static int ospf_sr_enabled(struct vty
*vty
)
1748 vty_out(vty
, "%% OSPF SR is not turned on\n");
1753 DEFUN (sr_sid_label_range
,
1754 sr_sid_label_range_cmd
,
1755 "segment-routing global-block (0-1048575) (0-1048575)",
1757 "Segment Routing Global Block label range\n"
1758 "Lower-bound range in decimal (0-1048575)\n"
1759 "Upper-bound range in decimal (0-1048575)\n")
1767 if (!ospf_sr_enabled(vty
))
1768 return CMD_WARNING_CONFIG_FAILED
;
1770 /* Get lower and upper bound */
1771 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1772 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1773 size
= upper
- lower
+ 1;
1775 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1777 "Range size cannot be less than 0 or more than %u\n",
1778 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1779 return CMD_WARNING_CONFIG_FAILED
;
1782 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1783 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1784 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1785 return CMD_WARNING_CONFIG_FAILED
;
1788 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1789 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1790 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1791 return CMD_WARNING_CONFIG_FAILED
;
1794 /* Check if values have changed */
1795 if ((OspfSR
.srgb
.range_size
== size
)
1796 && (OspfSR
.srgb
.lower_bound
== lower
))
1799 /* Set SID/Label range SRGB */
1800 OspfSR
.srgb
.range_size
= size
;
1801 OspfSR
.srgb
.lower_bound
= lower
;
1802 if (OspfSR
.self
!= NULL
) {
1803 OspfSR
.self
->srgb
.range_size
= size
;
1804 OspfSR
.self
->srgb
.lower_bound
= lower
;
1807 /* Set Router Information SR parameters */
1808 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1810 /* Update NHLFE entries */
1811 hash_iterate(OspfSR
.neighbors
,
1812 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1818 DEFUN (no_sr_sid_label_range
,
1819 no_sr_sid_label_range_cmd
,
1820 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1823 "Segment Routing Global Block label range\n"
1824 "Lower-bound range in decimal (0-1048575)\n"
1825 "Upper-bound range in decimal (0-1048575)\n")
1828 if (!ospf_sr_enabled(vty
))
1829 return CMD_WARNING_CONFIG_FAILED
;
1831 /* Revert to default SRGB value */
1832 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1833 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1834 if (OspfSR
.self
!= NULL
) {
1835 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1836 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1839 /* Set Router Information SR parameters */
1840 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1842 /* Update NHLFE entries */
1843 hash_iterate(OspfSR
.neighbors
,
1844 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1852 "segment-routing node-msd (1-16)",
1854 "Maximum Stack Depth for this router\n"
1855 "Maximum number of label that could be stack (1-16)\n")
1860 if (!ospf_sr_enabled(vty
))
1861 return CMD_WARNING_CONFIG_FAILED
;
1864 argv_find(argv
, argc
, "(1-16)", &idx
);
1865 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1866 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1867 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1869 return CMD_WARNING_CONFIG_FAILED
;
1872 /* Check if value has changed */
1873 if (OspfSR
.msd
== msd
)
1876 /* Set this router MSD */
1878 if (OspfSR
.self
!= NULL
)
1879 OspfSR
.self
->msd
= msd
;
1881 /* Set Router Information SR parameters */
1882 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1887 DEFUN (no_sr_node_msd
,
1889 "no segment-routing node-msd [(1-16)]",
1892 "Maximum Stack Depth for this router\n"
1893 "Maximum number of label that could be stack (1-16)\n")
1896 if (!ospf_sr_enabled(vty
))
1897 return CMD_WARNING_CONFIG_FAILED
;
1899 /* unset this router MSD */
1901 if (OspfSR
.self
!= NULL
)
1902 OspfSR
.self
->msd
= 0;
1904 /* Set Router Information SR parameters */
1905 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1910 DEFUN (sr_prefix_sid
,
1912 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1915 "IPv4 Prefix as A.B.C.D/M\n"
1916 "SID index for this prefix in decimal (0-65535)\n"
1917 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1918 "Don't request Penultimate Hop Popping (PHP)\n")
1923 struct listnode
*node
;
1924 struct sr_prefix
*srp
, *new;
1925 struct interface
*ifp
;
1927 if (!ospf_sr_enabled(vty
))
1928 return CMD_WARNING_CONFIG_FAILED
;
1930 /* Get network prefix */
1931 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1932 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1933 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
1934 return CMD_WARNING_CONFIG_FAILED
;
1937 /* Get & verify index value */
1938 argv_find(argv
, argc
, "(0-65535)", &idx
);
1939 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1940 if (index
> OspfSR
.srgb
.range_size
- 1) {
1941 vty_out(vty
, "Index %u must be lower than range size %u\n",
1942 index
, OspfSR
.srgb
.range_size
);
1943 return CMD_WARNING_CONFIG_FAILED
;
1946 /* check that the index is not already used */
1947 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1948 if (srp
->sid
== index
) {
1949 vty_out(vty
, "Index %u is already used\n", index
);
1950 return CMD_WARNING_CONFIG_FAILED
;
1954 /* Create new Extended Prefix to SRDB if not found */
1955 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1956 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1957 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1958 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1959 new->nhlfe
.prefv4
.family
= p
.family
;
1961 /* Set NO PHP flag if present and compute NHLFE */
1962 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1963 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1964 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1965 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1968 if (IS_DEBUG_OSPF_SR
)
1969 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1970 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
1971 new->nhlfe
.prefv4
.prefixlen
);
1973 /* Get Interface and check if it is a Loopback */
1974 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1977 * Interface could be not yet available i.e. when this
1978 * command is in the configuration file, OSPF is not yet
1979 * ready. In this case, store the prefix SID for latter
1980 * update of this Extended Prefix
1982 listnode_add(OspfSR
.self
->ext_prefix
, new);
1984 "Interface for prefix %s/%u not found. Deferred LSA "
1986 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
1990 if (!if_is_loopback(ifp
)) {
1991 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
1992 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
1993 return CMD_WARNING_CONFIG_FAILED
;
1995 new->nhlfe
.ifindex
= ifp
->ifindex
;
1997 /* Search if this prefix already exist */
1998 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1999 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2000 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2006 /* Update or Add this new SR Prefix */
2008 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2009 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2010 listnode_add(OspfSR
.self
->ext_prefix
, new);
2012 listnode_add(OspfSR
.self
->ext_prefix
, new);
2013 add_sid_nhlfe(new->nhlfe
);
2016 /* Finally, update Extended Prefix LSA */
2017 new->instance
= ospf_ext_schedule_prefix_index(
2018 ifp
, new->sid
, &new->nhlfe
.prefv4
, new->flags
);
2019 if (new->instance
== 0) {
2020 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2021 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2028 DEFUN (no_sr_prefix_sid
,
2029 no_sr_prefix_sid_cmd
,
2030 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2034 "IPv4 Prefix as A.B.C.D/M\n"
2035 "SID index for this prefix in decimal (0-65535)\n"
2036 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2037 "Don't request Penultimate Hop Popping (PHP)\n")
2041 struct listnode
*node
;
2042 struct sr_prefix
*srp
;
2043 struct interface
*ifp
;
2047 /* Get network prefix */
2048 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2049 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2051 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2052 return CMD_WARNING_CONFIG_FAILED
;
2055 /* check that the prefix is already set */
2056 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2057 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2058 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2064 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2066 return CMD_WARNING_CONFIG_FAILED
;
2070 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2072 vty_out(vty
, "interface for prefix %s not found.\n",
2074 return CMD_WARNING_CONFIG_FAILED
;
2077 /* Update Extended Prefix LSA */
2078 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2079 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2083 if (IS_DEBUG_OSPF_SR
)
2084 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2085 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2086 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2088 /* Delete NHLFE is NO-PHP is set */
2089 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2090 del_sid_nhlfe(srp
->nhlfe
);
2092 /* OK, all is clean, remove SRP from SRDB */
2093 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2094 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2100 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2101 struct sr_node
*srn
)
2104 struct listnode
*node
;
2105 struct sr_link
*srl
;
2106 struct sr_prefix
*srp
;
2107 struct interface
*itf
;
2111 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2112 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2119 json_node
= json_object_new_object();
2120 json_object_string_add(json_node
, "routerID",
2121 inet_ntoa(srn
->adv_router
));
2122 json_object_int_add(json_node
, "srgbSize",
2123 srn
->srgb
.range_size
);
2124 json_object_int_add(json_node
, "srgbLabel",
2125 srn
->srgb
.lower_bound
);
2126 json_algo
= json_object_new_array();
2127 json_object_object_add(json_node
, "algorithms", json_algo
);
2128 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2129 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2131 json_obj
= json_object_new_object();
2134 snprintf(tmp
, 2, "%u", i
);
2135 json_object_string_add(json_obj
, tmp
,
2136 srn
->algo
[i
] == SR_ALGORITHM_SPF
2139 json_object_array_add(json_algo
, json_obj
);
2142 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2144 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2145 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2146 srn
->srgb
.lower_bound
);
2147 vty_out(vty
, "\tAlgorithm(s): %s",
2148 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2149 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2150 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2153 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2157 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2162 "\n\n Prefix or Link Label In Label Out "
2163 "Node or Adj. SID Interface Nexthop\n");
2165 "------------------ -------- --------- "
2166 "--------------------- --------- ---------------\n");
2168 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2169 snprintf(pref
, 19, "%s/%u", inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2170 srp
->nhlfe
.prefv4
.prefixlen
);
2171 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2172 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2173 sprintf(label
, "pop");
2175 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2176 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2179 json_prefix
= json_object_new_array();
2180 json_object_object_add(json_node
,
2184 json_obj
= json_object_new_object();
2185 json_object_string_add(json_obj
, "prefix", pref
);
2186 json_object_int_add(json_obj
, "sid", srp
->sid
);
2187 json_object_int_add(json_obj
, "inputLabel",
2188 srp
->nhlfe
.label_in
);
2189 json_object_string_add(json_obj
, "outputLabel", label
);
2190 json_object_string_add(json_obj
, "interface",
2191 itf
? itf
->name
: "-");
2192 json_object_string_add(json_obj
, "nexthop",
2193 inet_ntoa(srp
->nhlfe
.nexthop
));
2194 json_object_array_add(json_prefix
, json_obj
);
2196 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2197 srp
->nhlfe
.label_in
, label
, sid
,
2198 itf
? itf
->name
: "-",
2199 inet_ntoa(srp
->nhlfe
.nexthop
));
2203 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2204 snprintf(pref
, 19, "%s/%u",
2205 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2206 srl
->nhlfe
[0].prefv4
.prefixlen
);
2207 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2208 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2209 sprintf(label
, "pop");
2211 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2212 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2215 json_link
= json_object_new_array();
2216 json_object_object_add(
2217 json_node
, "extendedLink", json_link
);
2220 json_obj
= json_object_new_object();
2221 json_object_string_add(json_obj
, "prefix", pref
);
2222 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2223 json_object_int_add(json_obj
, "inputLabel",
2224 srl
->nhlfe
[0].label_in
);
2225 json_object_string_add(json_obj
, "outputLabel", label
);
2226 json_object_string_add(json_obj
, "interface",
2227 itf
? itf
->name
: "-");
2228 json_object_string_add(
2229 json_obj
, "nexthop",
2230 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2231 json_object_array_add(json_link
, json_obj
);
2233 json_obj
= json_object_new_object();
2234 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2235 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2236 sprintf(label
, "pop");
2238 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2239 json_object_string_add(json_obj
, "prefix", pref
);
2240 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2241 json_object_int_add(json_obj
, "inputLabel",
2242 srl
->nhlfe
[1].label_in
);
2243 json_object_string_add(json_obj
, "outputLabel", label
);
2244 json_object_string_add(json_obj
, "interface",
2245 itf
? itf
->name
: "-");
2246 json_object_string_add(
2247 json_obj
, "nexthop",
2248 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2249 json_object_array_add(json_link
, json_obj
);
2251 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2252 srl
->nhlfe
[0].label_in
, label
, sid
,
2253 itf
? itf
->name
: "-",
2254 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2255 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2256 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2257 sprintf(label
, "pop");
2259 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2260 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2261 srl
->nhlfe
[1].label_in
, label
, sid
,
2262 itf
? itf
->name
: "-",
2263 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2267 json_object_array_add(json
, json_node
);
2272 static void show_vty_srdb(struct hash_backet
*backet
, void *args
)
2274 struct vty
*vty
= (struct vty
*)args
;
2275 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2277 show_sr_node(vty
, NULL
, srn
);
2280 static void show_json_srdb(struct hash_backet
*backet
, void *args
)
2282 struct json_object
*json
= (struct json_object
*)args
;
2283 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2285 show_sr_node(NULL
, json
, srn
);
2288 DEFUN (show_ip_opsf_srdb
,
2289 show_ip_ospf_srdb_cmd
,
2290 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2294 "Database summary\n"
2295 "Show Segment Routing Data Base\n"
2296 "Advertising SR node\n"
2297 "Advertising SR node ID (as an IP address)\n"
2298 "Self-originated SR node\n"
2303 struct sr_node
*srn
;
2304 uint8_t uj
= use_json(argc
, argv
);
2305 json_object
*json
= NULL
, *json_node_array
= NULL
;
2307 if (!OspfSR
.enabled
) {
2308 vty_out(vty
, "Segment Routing is disabled on this router\n");
2313 json
= json_object_new_object();
2314 json_node_array
= json_object_new_array();
2315 json_object_string_add(json
, "srdbID",
2316 inet_ntoa(OspfSR
.self
->adv_router
));
2317 json_object_object_add(json
, "srNodes", json_node_array
);
2320 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2321 inet_ntoa(OspfSR
.self
->adv_router
));
2324 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2326 show_sr_node(vty
, json_node_array
, srn
);
2328 vty_out(vty
, "%s\n",
2329 json_object_to_json_string_ext(
2330 json
, JSON_C_TO_STRING_PRETTY
));
2331 json_object_free(json
);
2336 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2337 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2338 vty_out(vty
, "Specified Router ID %s is invalid\n",
2340 return CMD_WARNING_CONFIG_FAILED
;
2342 /* Get the SR Node from the SRDB */
2343 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2345 show_sr_node(vty
, json_node_array
, srn
);
2347 vty_out(vty
, "%s\n",
2348 json_object_to_json_string_ext(
2349 json
, JSON_C_TO_STRING_PRETTY
));
2350 json_object_free(json
);
2355 /* No parameters have been provided, Iterate through all the SRDB */
2357 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2358 void *))show_json_srdb
,
2359 (void *)json_node_array
);
2360 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2361 json
, JSON_C_TO_STRING_PRETTY
));
2362 json_object_free(json
);
2364 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2365 void *))show_vty_srdb
,
2371 /* Install new CLI commands */
2372 void ospf_sr_register_vty(void)
2374 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2376 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2377 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2378 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2379 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2380 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2381 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2382 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2383 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);