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
));
133 zlog_err("SR (%s): Abort! can't create new SR node", __func__
);
137 /* Default Algorithm, SRGB and MSD */
138 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
139 new->algo
[i
] = SR_ALGORITHM_UNSET
;
141 new->srgb
.range_size
= 0;
142 new->srgb
.lower_bound
= 0;
145 /* Create Link, Prefix and Range TLVs list */
146 new->ext_link
= list_new();
147 new->ext_prefix
= list_new();
148 new->ext_link
->del
= del_sr_link
;
149 new->ext_prefix
->del
= del_sr_pref
;
151 IPV4_ADDR_COPY(&new->adv_router
, rid
);
152 new->neighbor
= NULL
;
155 if (IS_DEBUG_OSPF_SR
)
156 zlog_debug(" |- Created new SR node for %s",
157 inet_ntoa(new->adv_router
));
161 /* Delete Segment Routing node */
162 static void sr_node_del(struct sr_node
*srn
)
168 /* Clean Extended Link */
169 list_delete_and_null(&srn
->ext_link
);
171 /* Clean Prefix List */
172 list_delete_and_null(&srn
->ext_prefix
);
174 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
177 /* Get SR Node for a given nexthop */
178 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
179 struct in_addr nexthop
)
181 struct ospf_interface
*oi
= NULL
;
182 struct ospf_neighbor
*nbr
= NULL
;
183 struct listnode
*node
;
184 struct route_node
*rn
;
189 if (OspfSR
.neighbors
== NULL
)
192 if (IS_DEBUG_OSPF_SR
)
193 zlog_debug(" |- Search SR-Node for nexthop %s",
196 /* First, search neighbor Router ID for this nexthop */
198 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
199 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
201 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
213 if (IS_DEBUG_OSPF_SR
)
214 zlog_debug(" |- Found nexthop Router ID %s",
215 inet_ntoa(nbr
->router_id
));
216 /* Then, search SR Node */
217 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
223 * Segment Routing Initialization functions
226 /* Segment Routing starter function */
227 static int ospf_sr_start(struct ospf
*ospf
)
229 struct route_node
*rn
;
230 struct ospf_lsa
*lsa
;
234 if (IS_DEBUG_OSPF_SR
)
235 zlog_debug("SR (%s): Start Segment Routing", __func__
);
237 /* Initialize self SR Node */
238 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
239 (void *)sr_node_new
);
245 /* Complete & Store self SR Node */
246 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
247 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
248 srn
->algo
[0] = OspfSR
.algo
[0];
249 srn
->msd
= OspfSR
.msd
;
252 if (IS_DEBUG_OSPF_EVENT
)
253 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
255 /* Start by looking to Router Info & Extended LSA in lsdb */
256 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
257 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
) {
258 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
261 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
263 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
264 ospf_sr_ri_lsa_update(lsa
);
266 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
267 ospf_sr_ext_prefix_lsa_update(lsa
);
269 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
270 ospf_sr_ext_link_lsa_update(lsa
);
282 /* Stop Segment Routing */
283 static void ospf_sr_stop(void)
286 if (IS_DEBUG_OSPF_SR
)
287 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
290 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
291 * be remove though list_delete_and_null() call. See sr_node_del()
293 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
297 * Segment Routing initialize function
301 * @return 0 if OK, -1 otherwise
303 int ospf_sr_init(void)
307 if (IS_DEBUG_OSPF_SR
)
308 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
310 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
311 OspfSR
.enabled
= false;
312 /* Only AREA flooding is supported in this release */
313 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
315 /* Initialize SRGB, Algorithms and MSD TLVs */
316 /* Only Algorithm SPF is supported */
317 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
318 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
319 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
321 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
322 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
325 /* Initialize Hash table for neighbor SR nodes */
326 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
327 if (OspfSR
.neighbors
== NULL
)
330 /* Initialize Route Table for prefix */
331 OspfSR
.prefix
= route_table_init();
332 if (OspfSR
.prefix
== NULL
)
335 /* Register Segment Routing VTY command */
336 ospf_sr_register_vty();
343 * Segment Routing termination function
348 void ospf_sr_term(void)
351 /* Stop Segment Routing */
354 /* Clear SR Node Table */
355 if (OspfSR
.neighbors
)
356 hash_free(OspfSR
.neighbors
);
358 /* Clear Prefix Table */
360 route_table_finish(OspfSR
.prefix
);
362 OspfSR
.enabled
= false;
367 * Segment Routing finish function
372 void ospf_sr_finish(void)
374 /* Stop Segment Routing */
377 OspfSR
.enabled
= false;
381 * Following functions are used to manipulate the
382 * Next Hop Label Forwarding entry (NHLFE)
385 /* Compute label from index */
386 static mpls_label_t
index2label(uint32_t index
, struct sr_srgb srgb
)
390 label
= srgb
.lower_bound
+ index
;
391 if (label
> (srgb
.lower_bound
+ srgb
.range_size
))
392 return MPLS_INVALID_LABEL
;
397 /* Get neighbor full structure from address */
398 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
401 struct ospf_neighbor
*nbr
;
402 struct ospf_interface
*oi
;
403 struct listnode
*node
;
404 struct route_node
*rn
;
410 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
411 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
414 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
416 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
417 route_unlock_node(rn
);
424 /* Get OSPF Path from address */
425 static struct ospf_path
*get_nexthop_by_addr(struct ospf
*top
,
426 struct prefix_ipv4 p
)
428 struct ospf_route
* or ;
429 struct ospf_path
*path
;
430 struct listnode
*node
;
431 struct route_node
*rn
;
437 if (IS_DEBUG_OSPF_SR
)
438 zlog_debug(" |- Search Nexthop for prefix %s/%u",
439 inet_ntoa(p
.prefix
), p
.prefixlen
);
441 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
444 * Check if we found an OSPF route. May be NULL if SPF has not
445 * yet populate routing table for this prefix.
450 route_unlock_node(rn
);
455 /* Then search path from this route */
456 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
))
457 if (path
->nexthop
.s_addr
!= INADDR_ANY
|| path
->ifindex
!= 0)
463 /* Compute NHLFE entry for Extended Link */
464 static int compute_link_nhlfe(struct sr_link
*srl
)
466 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
467 struct ospf_neighbor
*nh
;
470 if (IS_DEBUG_OSPF_SR
)
471 zlog_debug(" |- Compute NHLFE for link %s/%u",
472 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
473 srl
->nhlfe
[0].prefv4
.prefixlen
);
475 /* First determine the OSPF Neighbor */
476 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
478 /* Neighbor could be not found when OSPF Adjacency just fire up
479 * because SPF don't yet populate routing table. This NHLFE will
480 * be fixed later when SR SPF schedule will be called.
485 if (IS_DEBUG_OSPF_SR
)
486 zlog_debug(" |- Found nexthop NHLFE %s",
487 inet_ntoa(nh
->router_id
));
489 /* Set ifindex for this neighbor */
490 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
491 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
493 /* Update neighbor address for LAN_ADJ_SID */
494 if (srl
->type
== LAN_ADJ_SID
) {
495 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
496 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
499 /* Set Input & Output Label */
500 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
501 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
503 srl
->nhlfe
[0].label_in
=
504 index2label(srl
->sid
[0], srl
->srn
->srgb
);
505 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
506 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
508 srl
->nhlfe
[1].label_in
=
509 index2label(srl
->sid
[1], srl
->srn
->srgb
);
511 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
512 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
519 * Compute NHLFE entry for Extended Prefix
521 * @param srp - Segment Routing Prefix
523 * @return -1 if next hop is not found, 0 if nexthop has not changed
526 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
528 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
529 struct ospf_path
*nh
= NULL
;
530 struct sr_node
*srnext
;
533 if (IS_DEBUG_OSPF_SR
)
534 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
535 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
536 srp
->nhlfe
.prefv4
.prefixlen
);
538 /* First determine the nexthop */
539 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
541 /* Nexthop could be not found when OSPF Adjacency just fire up
542 * because SPF don't yet populate routing table. This NHLFE will
543 * be fixed later when SR SPF schedule will be called.
548 /* Check if NextHop has changed when call after running a new SPF */
549 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
550 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
553 if (IS_DEBUG_OSPF_SR
)
554 zlog_debug(" |- Found new next hop for this NHLFE: %s",
555 inet_ntoa(nh
->nexthop
));
558 * Get SR-Node for this nexthop. Could be not yet available
559 * as Extende Link / Prefix and Router Information are flooded
560 * after LSA Type 1 & 2 which populate the OSPF Route Table
562 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
566 /* And store this information for later update if SR Node is found */
567 srnext
->neighbor
= OspfSR
.self
;
568 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
571 srp
->nexthop
= srnext
;
574 * SR Node could be known, but SRGB could be not initialize
575 * This is due to the fact that Extended Link / Prefix could
576 * be received before corresponding Router Information LSA
578 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
579 || (srnext
->srgb
.range_size
== 0))
582 if (IS_DEBUG_OSPF_SR
)
583 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
584 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
585 inet_ntoa(srnext
->adv_router
));
587 /* Set ip addr & ifindex for this neighbor */
588 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
589 srp
->nhlfe
.ifindex
= nh
->ifindex
;
591 /* Compute Input Label with self SRGB */
592 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
594 * and Output Label with Next hop SR Node SRGB or Implicit Null label
595 * if next hop is the destination and request PHP
597 if ((srp
->nexthop
== NULL
)
598 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
599 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
600 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
601 srp
->nhlfe
.label_out
= srp
->sid
;
603 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
605 if (IS_DEBUG_OSPF_SR
)
606 zlog_debug(" |- Computed new labels in: %u out: %u",
607 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
613 /* Send MPLS Label entry to Zebra for installation or deletion */
614 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
622 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
623 stream_putc(s
, ZEBRA_LSP_SR
);
624 /* OSPF Segment Routing currently support only IPv4 */
625 stream_putl(s
, nhlfe
.prefv4
.family
);
626 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
627 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
628 stream_put_in_addr(s
, &nhlfe
.nexthop
);
629 stream_putl(s
, nhlfe
.ifindex
);
630 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
631 stream_putl(s
, nhlfe
.label_in
);
632 stream_putl(s
, nhlfe
.label_out
);
634 /* Put length at the first point of the stream. */
635 stream_putw_at(s
, 0, stream_get_endp(s
));
637 if (IS_DEBUG_OSPF_SR
)
638 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
639 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
640 nhlfe
.label_in
, nhlfe
.label_out
,
641 inet_ntoa(nhlfe
.prefv4
.prefix
),
642 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
644 return zclient_send_message(zclient
);
647 /* Request zebra to install/remove FEC in FIB */
648 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
650 struct zapi_route api
;
651 struct zapi_nexthop
*api_nh
;
653 /* Support only IPv4 */
654 if (nhlfe
.prefv4
.family
!= AF_INET
)
657 memset(&api
, 0, sizeof(api
));
658 api
.vrf_id
= VRF_DEFAULT
;
659 api
.type
= ZEBRA_ROUTE_OSPF
;
660 api
.safi
= SAFI_UNICAST
;
661 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
663 if (cmd
== ZEBRA_ROUTE_ADD
) {
665 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
666 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
668 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
669 api_nh
= &api
.nexthops
[0];
670 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
671 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
672 api_nh
->ifindex
= nhlfe
.ifindex
;
674 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
675 api_nh
->labels
[0] = nhlfe
.label_out
;
676 api_nh
->label_num
= 1;
677 api_nh
->vrf_id
= VRF_DEFAULT
;
681 if (IS_DEBUG_OSPF_SR
)
682 zlog_debug(" |- %s FEC %u for %s/%u via %u",
683 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
684 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
685 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
687 return zclient_route_send(cmd
, zclient
, &api
);
690 /* Add new NHLFE entry for SID */
691 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
693 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
694 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
695 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
696 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
700 /* Remove NHLFE entry for SID */
701 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
703 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
704 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
705 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
706 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
710 /* Update NHLFE entry for SID */
711 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
719 * Functions to parse and get Extended Link / Prefix
723 /* Extended Link SubTLVs Getter */
724 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
728 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
729 struct ext_subtlv_adj_sid
*adj_sid
;
730 struct ext_subtlv_lan_adj_sid
*lan_sid
;
731 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
733 struct tlv_header
*sub_tlvh
;
734 uint16_t length
= 0, sum
= 0, i
= 0;
736 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
741 /* Initialize TLV browsing */
742 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
743 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
744 + EXT_TLV_LINK_SIZE
);
745 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
746 switch (ntohs(sub_tlvh
->type
)) {
747 case EXT_SUBTLV_ADJ_SID
:
748 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
750 i
= CHECK_FLAG(adj_sid
->flags
,
751 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
754 srl
->flags
[i
] = adj_sid
->flags
;
755 if (CHECK_FLAG(adj_sid
->flags
,
756 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
757 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
759 srl
->sid
[i
] = ntohl(adj_sid
->value
);
760 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
762 case EXT_SUBTLV_LAN_ADJ_SID
:
763 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
764 srl
->type
= LAN_ADJ_SID
;
765 i
= CHECK_FLAG(lan_sid
->flags
,
766 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
769 srl
->flags
[i
] = lan_sid
->flags
;
770 if (CHECK_FLAG(lan_sid
->flags
,
771 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
772 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
774 srl
->sid
[i
] = ntohl(lan_sid
->value
);
775 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
776 &lan_sid
->neighbor_id
);
778 case EXT_SUBTLV_RMT_ITF_ADDR
:
779 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
780 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
781 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
786 sum
+= TLV_SIZE(sub_tlvh
);
789 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
790 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
791 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
792 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
793 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
794 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
795 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
796 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
798 if (IS_DEBUG_OSPF_SR
) {
799 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
800 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
801 srl
->nhlfe
[0].prefv4
.prefixlen
);
802 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
803 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
804 srl
->nhlfe
[1].prefv4
.prefixlen
);
810 /* Extended Prefix SubTLVs Getter */
811 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
814 struct sr_prefix
*srp
;
815 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
816 struct ext_subtlv_prefix_sid
*psid
;
818 struct tlv_header
*sub_tlvh
;
819 uint16_t length
= 0, sum
= 0;
821 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
826 /* Initialize TLV browsing */
827 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
828 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
829 + EXT_TLV_PREFIX_SIZE
);
830 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
831 switch (ntohs(sub_tlvh
->type
)) {
832 case EXT_SUBTLV_PREFIX_SID
:
833 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
834 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
835 zlog_err("SR (%s): Unsupported Algorithm",
837 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
840 srp
->type
= PREF_SID
;
841 srp
->flags
= psid
->flags
;
842 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
843 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
845 srp
->sid
= ntohl(psid
->value
);
846 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
848 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
849 srp
->nhlfe
.prefv4
.family
= AF_INET
;
850 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
855 sum
+= TLV_SIZE(sub_tlvh
);
858 if (IS_DEBUG_OSPF_SR
)
859 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
860 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
861 srp
->nhlfe
.prefv4
.prefixlen
);
866 * Functions to manipulate Segment Routing Link & Prefix structures
869 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
870 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
872 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
873 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
874 && (srl1
->flags
[1] == srl2
->flags
[1]))
880 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
881 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
883 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
889 /* Update Segment Link of given Segment Routing Node */
890 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
893 struct listnode
*node
;
898 if ((srn
== NULL
) || (srl
== NULL
))
901 if (IS_DEBUG_OSPF_SR
)
902 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
904 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
905 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
906 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
907 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
910 /* Search for existing Segment Link */
911 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
912 if (lk
->instance
== srl
->instance
) {
917 if (IS_DEBUG_OSPF_SR
)
918 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
919 found
? "Update" : "Add",
920 GET_OPAQUE_ID(srl
->instance
),
921 inet_ntoa(srn
->adv_router
));
923 /* if not found, add new Segment Link and install NHLFE */
925 /* Complete SR-Link and add it to SR-Node list */
927 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
928 listnode_add(srn
->ext_link
, srl
);
929 /* Try to set MPLS table */
930 if (compute_link_nhlfe(srl
)) {
931 add_sid_nhlfe(srl
->nhlfe
[0]);
932 add_sid_nhlfe(srl
->nhlfe
[1]);
935 if (sr_link_cmp(lk
, srl
)) {
936 if (compute_link_nhlfe(srl
)) {
937 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
938 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
939 /* Replace Segment List */
940 listnode_delete(srn
->ext_link
, lk
);
941 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
943 IPV4_ADDR_COPY(&srl
->adv_router
,
945 listnode_add(srn
->ext_link
, srl
);
947 /* New NHLFE was not found.
948 * Just free the SR Link
950 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
954 * This is just an LSA refresh.
955 * Stop processing and free SR Link
957 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
962 /* Update Segment Prefix of given Segment Routing Node */
963 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
966 struct listnode
*node
;
967 struct sr_prefix
*pref
;
971 if (srn
== NULL
|| srp
== NULL
)
974 if (IS_DEBUG_OSPF_SR
)
975 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
977 /* Process only Global Prefix SID */
978 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
981 /* Search for existing Segment Prefix */
982 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
983 if (pref
->instance
== srp
->instance
) {
988 if (IS_DEBUG_OSPF_SR
)
989 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
990 found
? "Update" : "Add",
991 GET_OPAQUE_ID(srp
->instance
),
992 inet_ntoa(srn
->adv_router
));
994 /* if not found, add new Segment Prefix and install NHLFE */
996 /* Complete SR-Prefix and add it to SR-Node list */
998 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
999 listnode_add(srn
->ext_prefix
, srp
);
1000 /* Try to set MPLS table */
1001 if (compute_prefix_nhlfe(srp
) == 1)
1002 add_sid_nhlfe(srp
->nhlfe
);
1004 if (sr_prefix_cmp(pref
, srp
)) {
1005 if (compute_prefix_nhlfe(srp
) == 1) {
1006 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
1007 /* Replace Segment Prefix */
1008 listnode_delete(srn
->ext_prefix
, pref
);
1009 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1011 IPV4_ADDR_COPY(&srp
->adv_router
,
1013 listnode_add(srn
->ext_prefix
, srp
);
1015 /* New NHLFE was not found.
1016 * Just free the SR Prefix
1018 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1021 /* This is just an LSA refresh.
1022 * Stop processing and free SR Prefix
1024 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1030 * When change the FRR Self SRGB, update the NHLFE Input Label
1031 * for all Extended Prefix with SID index through hash_iterate()
1033 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1035 struct listnode
*node
;
1036 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1037 struct sr_prefix
*srp
;
1038 struct sr_nhlfe
new;
1040 /* Process Every Extended Prefix for this SR-Node */
1041 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1042 /* Process Self SRN only if NO-PHP is requested */
1043 if ((srn
== OspfSR
.self
)
1044 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1047 /* Process only SID Index */
1048 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1051 /* OK. Compute new NHLFE */
1052 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1053 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1054 /* Update MPLS LFIB */
1055 update_sid_nhlfe(srp
->nhlfe
, new);
1056 /* Finally update Input Label */
1057 srp
->nhlfe
.label_in
= new.label_in
;
1062 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1063 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1065 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1067 struct listnode
*node
;
1068 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1069 struct sr_node
*srnext
= (struct sr_node
*)args
;
1070 struct sr_prefix
*srp
;
1071 struct sr_nhlfe
new;
1073 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1074 /* Process only SID Index for next hop without PHP */
1075 if ((srp
->nexthop
== NULL
)
1076 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1078 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1079 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1080 update_sid_nhlfe(srp
->nhlfe
, new);
1081 srp
->nhlfe
.label_out
= new.label_out
;
1086 * Following functions are call when new Segment Routing LSA are received
1087 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1088 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1089 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1092 /* Update Segment Routing from Router Information LSA */
1093 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1095 struct sr_node
*srn
;
1096 struct tlv_header
*tlvh
;
1097 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1098 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1099 struct ri_sr_tlv_sr_algorithm
*algo
;
1100 struct sr_srgb srgb
;
1101 uint16_t length
= 0, sum
= 0;
1103 if (IS_DEBUG_OSPF_SR
)
1105 "SR (%s): Process Router "
1106 "Information LSA 4.0.0.%u from %s",
1107 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1108 inet_ntoa(lsah
->adv_router
));
1111 if (IS_LSA_SELF(lsa
))
1114 if (OspfSR
.neighbors
== NULL
) {
1115 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1119 /* Get SR Node in hash table from Router ID */
1120 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1121 (void *)sr_node_new
);
1125 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1130 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1132 "SR (%s): Abort! Wrong "
1133 "LSA ID 4.0.0.%u for SR node %s/%u",
1134 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1135 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1139 /* Collect Router Information Sub TLVs */
1140 /* Initialize TLV browsing */
1141 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1142 srgb
.range_size
= 0;
1143 srgb
.lower_bound
= 0;
1145 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1146 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1147 switch (ntohs(tlvh
->type
)) {
1148 case RI_SR_TLV_SR_ALGORITHM
:
1149 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1152 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1153 srn
->algo
[i
] = algo
->value
[0];
1154 for (; i
< ALGORITHM_COUNT
; i
++)
1155 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1156 sum
+= TLV_SIZE(tlvh
);
1158 case RI_SR_TLV_SID_LABEL_RANGE
:
1159 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1160 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1162 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1163 sum
+= TLV_SIZE(tlvh
);
1165 case RI_SR_TLV_NODE_MSD
:
1166 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1167 sum
+= TLV_SIZE(tlvh
);
1170 sum
+= TLV_SIZE(tlvh
);
1175 /* Check that we collect mandatory parameters */
1176 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1177 || srgb
.lower_bound
== 0) {
1178 zlog_warn("SR (%s): Missing mandatory parameters. Abort!",
1180 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1181 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1185 /* Check if it is a new SR Node or not */
1186 if (srn
->instance
== 0) {
1188 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1190 srn
->srgb
.range_size
= srgb
.range_size
;
1191 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1194 /* Check if SRGB has changed */
1195 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1196 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1197 srn
->srgb
.range_size
= srgb
.range_size
;
1198 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1199 /* Update NHLFE if it is a neighbor SR node */
1200 if (srn
->neighbor
== OspfSR
.self
)
1201 hash_iterate(OspfSR
.neighbors
,
1202 (void (*)(struct hash_backet
*,
1203 void *))update_out_nhlfe
,
1209 * Delete SR Node entry in hash table information corresponding to an expired
1210 * Router Information LSA
1212 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1214 struct sr_node
*srn
;
1215 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1217 if (IS_DEBUG_OSPF_SR
)
1218 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1219 __func__
, inet_ntoa(lsah
->adv_router
),
1220 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1223 if (OspfSR
.neighbors
== NULL
) {
1224 zlog_err("SR (%s): Abort! no valid SR Data Base", __func__
);
1228 /* Release Router ID entry in SRDB hash table */
1229 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1233 zlog_err("SR (%s): Abort! no entry in SRDB for SR Node %s",
1234 __func__
, inet_ntoa(lsah
->adv_router
));
1238 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1239 zlog_err("SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1240 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1241 inet_ntoa(lsah
->adv_router
));
1245 /* Remove SR node */
1249 /* Update Segment Routing from Extended Link LSA */
1250 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1252 struct sr_node
*srn
;
1253 struct tlv_header
*tlvh
;
1254 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1255 struct sr_link
*srl
;
1257 uint16_t length
, sum
;
1259 if (IS_DEBUG_OSPF_SR
)
1261 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1262 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1263 inet_ntoa(lsah
->adv_router
));
1266 if (OspfSR
.neighbors
== NULL
) {
1267 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1271 /* Get SR Node in hash table from Router ID */
1272 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1273 (void *)&(lsah
->adv_router
),
1274 (void *)sr_node_new
);
1278 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1283 /* Initialize TLV browsing */
1284 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1286 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1287 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1288 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1289 /* Got Extended Link information */
1290 srl
= get_ext_link_sid(tlvh
);
1291 /* Update SID if not null */
1293 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1294 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1297 sum
+= TLV_SIZE(tlvh
);
1301 /* Delete Segment Routing from Extended Link LSA */
1302 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1304 struct listnode
*node
;
1305 struct sr_link
*srl
;
1306 struct sr_node
*srn
;
1307 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1308 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1310 if (IS_DEBUG_OSPF_SR
)
1311 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1312 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1313 inet_ntoa(lsah
->adv_router
));
1316 if (OspfSR
.neighbors
== NULL
) {
1317 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1321 /* Search SR Node in hash table from Router ID */
1322 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1323 (void *)&(lsah
->adv_router
));
1326 * SR-Node may be NULL if it has been remove previously when
1327 * processing Router Information LSA deletion
1330 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1331 __func__
, inet_ntoa(lsah
->adv_router
));
1335 /* Search for corresponding Segment Link */
1336 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1337 if (srl
->instance
== instance
)
1340 /* Remove Segment Link if found */
1341 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1342 del_sid_nhlfe(srl
->nhlfe
[0]);
1343 del_sid_nhlfe(srl
->nhlfe
[1]);
1344 listnode_delete(srn
->ext_link
, srl
);
1345 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1348 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1350 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1351 inet_ntoa(lsah
->adv_router
));
1355 /* Update Segment Routing from Extended Prefix LSA */
1356 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1358 struct sr_node
*srn
;
1359 struct tlv_header
*tlvh
;
1360 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1361 struct sr_prefix
*srp
;
1363 uint16_t length
, sum
;
1365 if (IS_DEBUG_OSPF_SR
)
1367 "SR (%s): Process Extended Prefix LSA "
1369 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1370 inet_ntoa(lsah
->adv_router
));
1373 if (OspfSR
.neighbors
== NULL
) {
1374 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1378 /* Get SR Node in hash table from Router ID */
1379 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1380 (void *)&(lsah
->adv_router
),
1381 (void *)sr_node_new
);
1385 zlog_err("SR (%s): Abort! can't create SR node in hash table",
1390 /* Initialize TLV browsing */
1391 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1393 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1394 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1395 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1396 /* Got Extended Link information */
1397 srp
= get_ext_prefix_sid(tlvh
);
1398 /* Update SID if not null */
1400 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1401 update_ext_prefix_sid(srn
, srp
);
1404 sum
+= TLV_SIZE(tlvh
);
1408 /* Delete Segment Routing from Extended Prefix LSA */
1409 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1411 struct listnode
*node
;
1412 struct sr_prefix
*srp
;
1413 struct sr_node
*srn
;
1414 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1415 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1417 if (IS_DEBUG_OSPF_SR
)
1419 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1420 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1421 inet_ntoa(lsah
->adv_router
));
1424 if (OspfSR
.neighbors
== NULL
) {
1425 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1429 /* Search SR Node in hash table from Router ID */
1430 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1431 (void *)&(lsah
->adv_router
));
1434 * SR-Node may be NULL if it has been remove previously when
1435 * processing Router Information LSA deletion
1438 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1439 __func__
, inet_ntoa(lsah
->adv_router
));
1443 /* Search for corresponding Segment Link */
1444 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1445 if (srp
->instance
== instance
)
1448 /* Remove Segment Link if found */
1449 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1450 del_sid_nhlfe(srp
->nhlfe
);
1451 listnode_delete(srn
->ext_link
, srp
);
1452 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1455 "SR (%s): Didn't found corresponding SR Prefix "
1456 "7.0.0.%u for SR Node %s",
1457 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1458 inet_ntoa(lsah
->adv_router
));
1462 /* Get Label for Extended Link SID */
1463 /* TODO: To be replace by Zebra Label Manager */
1464 uint32_t get_ext_link_label_value(void)
1466 static uint32_t label
= ADJ_SID_MIN
- 1;
1468 if (label
< ADJ_SID_MAX
)
1475 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1476 * complete initial CLI command at startutp.
1478 * @param ifp - Loopback interface
1479 * @param pref - Prefix address of this interface
1483 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1485 struct listnode
*node
;
1486 struct sr_prefix
*srp
;
1489 if ((ifp
== NULL
) || (p
== NULL
))
1493 * Search if there is a Segment Prefix that correspond to this
1494 * interface or prefix, and update it if found
1496 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1497 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1498 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1500 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1502 /* Update Interface & Prefix info */
1503 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1504 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1506 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1507 srp
->nhlfe
.prefv4
.family
= p
->family
;
1508 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1510 /* OK. Let's Schedule Extended Prefix LSA */
1511 srp
->instance
= ospf_ext_schedule_prefix_index(
1512 ifp
, srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1514 /* Install NHLFE if NO-PHP is requested */
1515 if (CHECK_FLAG(srp
->flags
,
1516 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1517 srp
->nhlfe
.label_in
= index2label(
1518 srp
->sid
, OspfSR
.self
->srgb
);
1519 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1520 add_sid_nhlfe(srp
->nhlfe
);
1527 * Following functions are used to update MPLS LFIB after a SPF run
1530 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1533 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1534 struct listnode
*node
;
1535 struct sr_prefix
*srp
;
1536 struct sr_nhlfe old
;
1543 if (IS_DEBUG_OSPF_SR
)
1544 zlog_debug(" |- Update Prefix for SR Node %s",
1545 inet_ntoa(srn
->adv_router
));
1547 /* Skip Self SR Node */
1548 if (srn
== OspfSR
.self
)
1551 /* Update Extended Prefix */
1552 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1554 /* Backup current NHLFE */
1555 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1557 /* Compute the new NHLFE */
1558 rc
= compute_prefix_nhlfe(srp
);
1560 /* Check computation result */
1562 /* next hop is not know, remove old NHLFE to avoid loop */
1564 del_sid_nhlfe(srp
->nhlfe
);
1566 /* next hop has not changed, skip it */
1569 /* there is a new next hop, update NHLFE */
1571 update_sid_nhlfe(old
, srp
->nhlfe
);
1579 static int ospf_sr_update_schedule(struct thread
*t
)
1583 struct timeval start_time
, stop_time
;
1585 ospf
= THREAD_ARG(t
);
1586 ospf
->t_sr_update
= NULL
;
1591 monotime(&start_time
);
1593 if (IS_DEBUG_OSPF_SR
)
1594 zlog_debug("SR (%s): Start SPF update", __func__
);
1596 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1597 void *))ospf_sr_nhlfe_update
,
1600 monotime(&stop_time
);
1602 if (IS_DEBUG_OSPF_SR
)
1603 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld\n",
1605 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1606 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1608 OspfSR
.update
= false;
1612 #define OSPF_SR_UPDATE_INTERVAL 1
1614 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1620 /* Check if an update is not alreday engage */
1624 OspfSR
.update
= true;
1626 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1627 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1631 * --------------------------------------
1632 * Followings are vty command functions.
1633 * --------------------------------------
1637 * Segment Routing Router configuration
1639 * Must be centralize as it concerns both Extended Link/Prefix LSA
1640 * and Router Information LSA. Choose to call it from Extended Prefix
1641 * write_config() call back.
1643 * @param vty VTY output
1647 void ospf_sr_config_write_router(struct vty
*vty
)
1649 struct listnode
*node
;
1650 struct sr_prefix
*srp
;
1652 if (OspfSR
.enabled
) {
1653 vty_out(vty
, " segment-routing on\n");
1655 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1656 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1657 vty_out(vty
, " segment-routing global-block %u %u\n",
1658 OspfSR
.srgb
.lower_bound
,
1659 OspfSR
.srgb
.lower_bound
+ OspfSR
.srgb
.range_size
1662 if (OspfSR
.msd
!= 0)
1663 vty_out(vty
, " segment-routing node-msd %u\n",
1666 if (OspfSR
.self
!= NULL
) {
1667 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1670 " segment-routing prefix %s/%u "
1672 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1673 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1674 CHECK_FLAG(srp
->flags
,
1675 EXT_SUBTLV_PREFIX_SID_NPFLG
)
1683 DEFUN(ospf_sr_enable
,
1685 "segment-routing on",
1687 "Enable Segment Routing\n")
1690 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1695 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1697 "Segment Routing is only supported in default "
1699 return CMD_WARNING_CONFIG_FAILED
;
1702 if (IS_DEBUG_OSPF_EVENT
)
1703 zlog_debug("SR: Segment Routing: OFF -> ON");
1705 /* Start Segment Routing */
1706 OspfSR
.enabled
= true;
1707 if (!ospf_sr_start(ospf
)) {
1708 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1712 /* Set Router Information SR parameters */
1713 if (IS_DEBUG_OSPF_EVENT
)
1714 zlog_debug("SR: Activate SR for Router Information LSA");
1716 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1718 /* Update Ext LSA */
1719 if (IS_DEBUG_OSPF_EVENT
)
1720 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1722 ospf_ext_update_sr(true);
1727 DEFUN (no_ospf_sr_enable
,
1728 no_ospf_sr_enable_cmd
,
1729 "no segment-routing [on]",
1732 "Disable Segment Routing\n")
1735 if (!OspfSR
.enabled
)
1738 if (IS_DEBUG_OSPF_EVENT
)
1739 zlog_debug("SR: Segment Routing: ON -> OFF");
1741 /* Start by Disabling Extended Link & Prefix LSA */
1742 ospf_ext_update_sr(false);
1744 /* then, disable Router Information SR parameters */
1745 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1747 /* Finally, stop Segment Routing */
1749 OspfSR
.enabled
= false;
1754 static int ospf_sr_enabled(struct vty
*vty
)
1760 vty_out(vty
, "%% OSPF SR is not turned on\n");
1765 DEFUN (sr_sid_label_range
,
1766 sr_sid_label_range_cmd
,
1767 "segment-routing global-block (0-1048575) (0-1048575)",
1769 "Segment Routing Global Block label range\n"
1770 "Lower-bound range in decimal (0-1048575)\n"
1771 "Upper-bound range in decimal (0-1048575)\n")
1779 if (!ospf_sr_enabled(vty
))
1780 return CMD_WARNING_CONFIG_FAILED
;
1782 /* Get lower and upper bound */
1783 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1784 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1785 size
= upper
- lower
+ 1;
1787 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1789 "Range size cannot be less than 0 or more than %u\n",
1790 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1791 return CMD_WARNING_CONFIG_FAILED
;
1794 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1795 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1796 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1797 return CMD_WARNING_CONFIG_FAILED
;
1800 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1801 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1802 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1803 return CMD_WARNING_CONFIG_FAILED
;
1806 /* Check if values have changed */
1807 if ((OspfSR
.srgb
.range_size
== size
)
1808 && (OspfSR
.srgb
.lower_bound
== lower
))
1811 /* Set SID/Label range SRGB */
1812 OspfSR
.srgb
.range_size
= size
;
1813 OspfSR
.srgb
.lower_bound
= lower
;
1814 if (OspfSR
.self
!= NULL
) {
1815 OspfSR
.self
->srgb
.range_size
= size
;
1816 OspfSR
.self
->srgb
.lower_bound
= lower
;
1819 /* Set Router Information SR parameters */
1820 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1822 /* Update NHLFE entries */
1823 hash_iterate(OspfSR
.neighbors
,
1824 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1830 DEFUN (no_sr_sid_label_range
,
1831 no_sr_sid_label_range_cmd
,
1832 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1835 "Segment Routing Global Block label range\n"
1836 "Lower-bound range in decimal (0-1048575)\n"
1837 "Upper-bound range in decimal (0-1048575)\n")
1840 if (!ospf_sr_enabled(vty
))
1841 return CMD_WARNING_CONFIG_FAILED
;
1843 /* Revert to default SRGB value */
1844 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1845 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1846 if (OspfSR
.self
!= NULL
) {
1847 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1848 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1851 /* Set Router Information SR parameters */
1852 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1854 /* Update NHLFE entries */
1855 hash_iterate(OspfSR
.neighbors
,
1856 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1864 "segment-routing node-msd (1-16)",
1866 "Maximum Stack Depth for this router\n"
1867 "Maximum number of label that could be stack (1-16)\n")
1872 if (!ospf_sr_enabled(vty
))
1873 return CMD_WARNING_CONFIG_FAILED
;
1876 argv_find(argv
, argc
, "(1-16)", &idx
);
1877 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1878 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1879 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1881 return CMD_WARNING_CONFIG_FAILED
;
1884 /* Check if value has changed */
1885 if (OspfSR
.msd
== msd
)
1888 /* Set this router MSD */
1890 if (OspfSR
.self
!= NULL
)
1891 OspfSR
.self
->msd
= msd
;
1893 /* Set Router Information SR parameters */
1894 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1899 DEFUN (no_sr_node_msd
,
1901 "no segment-routing node-msd [(1-16)]",
1904 "Maximum Stack Depth for this router\n"
1905 "Maximum number of label that could be stack (1-16)\n")
1908 if (!ospf_sr_enabled(vty
))
1909 return CMD_WARNING_CONFIG_FAILED
;
1911 /* unset this router MSD */
1913 if (OspfSR
.self
!= NULL
)
1914 OspfSR
.self
->msd
= 0;
1916 /* Set Router Information SR parameters */
1917 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1922 DEFUN (sr_prefix_sid
,
1924 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1927 "IPv4 Prefix as A.B.C.D/M\n"
1928 "SID index for this prefix in decimal (0-65535)\n"
1929 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1930 "Don't request Penultimate Hop Popping (PHP)\n")
1935 struct listnode
*node
;
1936 struct sr_prefix
*srp
, *new;
1937 struct interface
*ifp
;
1939 if (!ospf_sr_enabled(vty
))
1940 return CMD_WARNING_CONFIG_FAILED
;
1942 /* Get network prefix */
1943 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1944 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1945 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
1946 return CMD_WARNING_CONFIG_FAILED
;
1949 /* Get & verify index value */
1950 argv_find(argv
, argc
, "(0-65535)", &idx
);
1951 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1952 if (index
> OspfSR
.srgb
.range_size
- 1) {
1953 vty_out(vty
, "Index %u must be lower than range size %u\n",
1954 index
, OspfSR
.srgb
.range_size
);
1955 return CMD_WARNING_CONFIG_FAILED
;
1958 /* check that the index is not already used */
1959 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1960 if (srp
->sid
== index
) {
1961 vty_out(vty
, "Index %u is already used\n", index
);
1962 return CMD_WARNING_CONFIG_FAILED
;
1966 /* Create new Extended Prefix to SRDB if not found */
1967 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1968 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1969 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1970 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1971 new->nhlfe
.prefv4
.family
= p
.family
;
1973 /* Set NO PHP flag if present and compute NHLFE */
1974 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1975 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1976 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1977 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1980 if (IS_DEBUG_OSPF_SR
)
1981 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1982 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
1983 new->nhlfe
.prefv4
.prefixlen
);
1985 /* Get Interface and check if it is a Loopback */
1986 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1989 * Interface could be not yet available i.e. when this
1990 * command is in the configuration file, OSPF is not yet
1991 * ready. In this case, store the prefix SID for latter
1992 * update of this Extended Prefix
1994 listnode_add(OspfSR
.self
->ext_prefix
, new);
1996 "Interface for prefix %s/%u not found. Deferred LSA "
1998 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2002 if (!if_is_loopback(ifp
)) {
2003 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2004 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
2005 return CMD_WARNING_CONFIG_FAILED
;
2007 new->nhlfe
.ifindex
= ifp
->ifindex
;
2009 /* Search if this prefix already exist */
2010 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2011 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2012 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2018 /* Update or Add this new SR Prefix */
2020 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2021 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2022 listnode_add(OspfSR
.self
->ext_prefix
, new);
2024 listnode_add(OspfSR
.self
->ext_prefix
, new);
2025 add_sid_nhlfe(new->nhlfe
);
2028 /* Finally, update Extended Prefix LSA */
2029 new->instance
= ospf_ext_schedule_prefix_index(
2030 ifp
, new->sid
, &new->nhlfe
.prefv4
, new->flags
);
2031 if (new->instance
== 0) {
2032 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2033 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2040 DEFUN (no_sr_prefix_sid
,
2041 no_sr_prefix_sid_cmd
,
2042 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2046 "IPv4 Prefix as A.B.C.D/M\n"
2047 "SID index for this prefix in decimal (0-65535)\n"
2048 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2049 "Don't request Penultimate Hop Popping (PHP)\n")
2053 struct listnode
*node
;
2054 struct sr_prefix
*srp
;
2055 struct interface
*ifp
;
2059 /* Get network prefix */
2060 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2061 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2063 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2064 return CMD_WARNING_CONFIG_FAILED
;
2067 /* check that the prefix is already set */
2068 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2069 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2070 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2076 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2078 return CMD_WARNING_CONFIG_FAILED
;
2082 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2084 vty_out(vty
, "interface for prefix %s not found.\n",
2086 return CMD_WARNING_CONFIG_FAILED
;
2089 /* Update Extended Prefix LSA */
2090 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2091 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2095 if (IS_DEBUG_OSPF_SR
)
2096 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2097 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2098 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2100 /* Delete NHLFE is NO-PHP is set */
2101 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2102 del_sid_nhlfe(srp
->nhlfe
);
2104 /* OK, all is clean, remove SRP from SRDB */
2105 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2106 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2112 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2113 struct sr_node
*srn
)
2116 struct listnode
*node
;
2117 struct sr_link
*srl
;
2118 struct sr_prefix
*srp
;
2119 struct interface
*itf
;
2123 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2124 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2131 json_node
= json_object_new_object();
2132 json_object_string_add(json_node
, "routerID",
2133 inet_ntoa(srn
->adv_router
));
2134 json_object_int_add(json_node
, "srgbSize",
2135 srn
->srgb
.range_size
);
2136 json_object_int_add(json_node
, "srgbLabel",
2137 srn
->srgb
.lower_bound
);
2138 json_algo
= json_object_new_array();
2139 json_object_object_add(json_node
, "algorithms", json_algo
);
2140 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2141 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2143 json_obj
= json_object_new_object();
2146 snprintf(tmp
, 2, "%u", i
);
2147 json_object_string_add(json_obj
, tmp
,
2148 srn
->algo
[i
] == SR_ALGORITHM_SPF
2151 json_object_array_add(json_algo
, json_obj
);
2154 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2156 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2157 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2158 srn
->srgb
.lower_bound
);
2159 vty_out(vty
, "\tAlgorithm(s): %s",
2160 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2161 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2162 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2165 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2169 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2174 "\n\n Prefix or Link Label In Label Out "
2175 "Node or Adj. SID Interface Nexthop\n");
2177 "------------------ -------- --------- "
2178 "--------------------- --------- ---------------\n");
2180 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2181 snprintf(pref
, 19, "%s/%u", inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2182 srp
->nhlfe
.prefv4
.prefixlen
);
2183 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2184 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2185 sprintf(label
, "pop");
2187 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2188 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2191 json_prefix
= json_object_new_array();
2192 json_object_object_add(json_node
,
2196 json_obj
= json_object_new_object();
2197 json_object_string_add(json_obj
, "prefix", pref
);
2198 json_object_int_add(json_obj
, "sid", srp
->sid
);
2199 json_object_int_add(json_obj
, "inputLabel",
2200 srp
->nhlfe
.label_in
);
2201 json_object_string_add(json_obj
, "outputLabel", label
);
2202 json_object_string_add(json_obj
, "interface",
2203 itf
? itf
->name
: "-");
2204 json_object_string_add(json_obj
, "nexthop",
2205 inet_ntoa(srp
->nhlfe
.nexthop
));
2206 json_object_array_add(json_prefix
, json_obj
);
2208 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2209 srp
->nhlfe
.label_in
, label
, sid
,
2210 itf
? itf
->name
: "-",
2211 inet_ntoa(srp
->nhlfe
.nexthop
));
2215 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2216 snprintf(pref
, 19, "%s/%u",
2217 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2218 srl
->nhlfe
[0].prefv4
.prefixlen
);
2219 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2220 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2221 sprintf(label
, "pop");
2223 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2224 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2227 json_link
= json_object_new_array();
2228 json_object_object_add(
2229 json_node
, "extendedLink", json_link
);
2232 json_obj
= json_object_new_object();
2233 json_object_string_add(json_obj
, "prefix", pref
);
2234 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2235 json_object_int_add(json_obj
, "inputLabel",
2236 srl
->nhlfe
[0].label_in
);
2237 json_object_string_add(json_obj
, "outputLabel", label
);
2238 json_object_string_add(json_obj
, "interface",
2239 itf
? itf
->name
: "-");
2240 json_object_string_add(
2241 json_obj
, "nexthop",
2242 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2243 json_object_array_add(json_link
, json_obj
);
2245 json_obj
= json_object_new_object();
2246 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2247 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2248 sprintf(label
, "pop");
2250 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2251 json_object_string_add(json_obj
, "prefix", pref
);
2252 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2253 json_object_int_add(json_obj
, "inputLabel",
2254 srl
->nhlfe
[1].label_in
);
2255 json_object_string_add(json_obj
, "outputLabel", label
);
2256 json_object_string_add(json_obj
, "interface",
2257 itf
? itf
->name
: "-");
2258 json_object_string_add(
2259 json_obj
, "nexthop",
2260 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2261 json_object_array_add(json_link
, json_obj
);
2263 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2264 srl
->nhlfe
[0].label_in
, label
, sid
,
2265 itf
? itf
->name
: "-",
2266 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2267 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2268 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2269 sprintf(label
, "pop");
2271 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2272 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2273 srl
->nhlfe
[1].label_in
, label
, sid
,
2274 itf
? itf
->name
: "-",
2275 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2279 json_object_array_add(json
, json_node
);
2284 static void show_vty_srdb(struct hash_backet
*backet
, void *args
)
2286 struct vty
*vty
= (struct vty
*)args
;
2287 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2289 show_sr_node(vty
, NULL
, srn
);
2292 static void show_json_srdb(struct hash_backet
*backet
, void *args
)
2294 struct json_object
*json
= (struct json_object
*)args
;
2295 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2297 show_sr_node(NULL
, json
, srn
);
2300 DEFUN (show_ip_opsf_srdb
,
2301 show_ip_ospf_srdb_cmd
,
2302 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2306 "Database summary\n"
2307 "Show Segment Routing Data Base\n"
2308 "Advertising SR node\n"
2309 "Advertising SR node ID (as an IP address)\n"
2310 "Self-originated SR node\n"
2315 struct sr_node
*srn
;
2316 u_char uj
= use_json(argc
, argv
);
2317 json_object
*json
= NULL
, *json_node_array
= NULL
;
2319 if (!OspfSR
.enabled
) {
2320 vty_out(vty
, "Segment Routing is disabled on this router\n");
2325 json
= json_object_new_object();
2326 json_node_array
= json_object_new_array();
2327 json_object_string_add(json
, "srdbID",
2328 inet_ntoa(OspfSR
.self
->adv_router
));
2329 json_object_object_add(json
, "srNodes", json_node_array
);
2332 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2333 inet_ntoa(OspfSR
.self
->adv_router
));
2336 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2338 show_sr_node(vty
, json_node_array
, srn
);
2340 vty_out(vty
, "%s\n",
2341 json_object_to_json_string_ext(
2342 json
, JSON_C_TO_STRING_PRETTY
));
2343 json_object_free(json
);
2348 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2349 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2350 vty_out(vty
, "Specified Router ID %s is invalid\n",
2352 return CMD_WARNING_CONFIG_FAILED
;
2354 /* Get the SR Node from the SRDB */
2355 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2357 show_sr_node(vty
, json_node_array
, srn
);
2359 vty_out(vty
, "%s\n",
2360 json_object_to_json_string_ext(
2361 json
, JSON_C_TO_STRING_PRETTY
));
2362 json_object_free(json
);
2367 /* No parameters have been provided, Iterate through all the SRDB */
2369 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2370 void *))show_json_srdb
,
2371 (void *)json_node_array
);
2372 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2373 json
, JSON_C_TO_STRING_PRETTY
));
2374 json_object_free(json
);
2376 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2377 void *))show_vty_srdb
,
2383 /* Install new CLI commands */
2384 void ospf_sr_register_vty(void)
2386 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2388 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2389 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2390 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2391 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2392 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2393 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2394 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2395 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);