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
41 #include "libospf.h" /* for ospf interface types */
48 #include "sockunion.h" /* for inet_aton() */
55 #include "ospf_errors.h"
57 #include "ospfd/ospfd.h"
58 #include "ospfd/ospf_interface.h"
59 #include "ospfd/ospf_ism.h"
60 #include "ospfd/ospf_asbr.h"
61 #include "ospfd/ospf_lsa.h"
62 #include "ospfd/ospf_lsdb.h"
63 #include "ospfd/ospf_neighbor.h"
64 #include "ospfd/ospf_nsm.h"
65 #include "ospfd/ospf_flood.h"
66 #include "ospfd/ospf_packet.h"
67 #include "ospfd/ospf_spf.h"
68 #include "ospfd/ospf_dump.h"
69 #include "ospfd/ospf_route.h"
70 #include "ospfd/ospf_ase.h"
71 #include "ospfd/ospf_sr.h"
72 #include "ospfd/ospf_ri.h"
73 #include "ospfd/ospf_ext.h"
74 #include "ospfd/ospf_zebra.h"
77 * Global variable to manage Segment Routing on this node.
78 * Note that all parameter values are stored in network byte order.
80 static struct ospf_sr_db OspfSR
;
81 static void ospf_sr_register_vty(void);
82 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
);
85 * Segment Routing Data Base functions
88 /* Hash function for Segment Routing entry */
89 static unsigned int sr_hash(void *p
)
91 const struct in_addr
*rid
= p
;
93 return jhash_1word(rid
->s_addr
, 0);
96 /* Compare 2 Router ID hash entries based on SR Node */
97 static int sr_cmp(const void *p1
, const void *p2
)
99 const struct sr_node
*srn
= p1
;
100 const struct in_addr
*rid
= p2
;
102 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
105 /* Functions to remove an SR Link */
106 static void del_sr_link(void *val
)
108 struct sr_link
*srl
= (struct sr_link
*)val
;
110 del_sid_nhlfe(srl
->nhlfe
[0]);
111 del_sid_nhlfe(srl
->nhlfe
[1]);
112 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
115 /* Functions to remove an SR Prefix */
116 static void del_sr_pref(void *val
)
118 struct sr_prefix
*srp
= (struct sr_prefix
*)val
;
120 del_sid_nhlfe(srp
->nhlfe
);
121 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
124 /* Allocate new Segment Routine node */
125 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
133 /* Allocate Segment Routing node memory */
134 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
136 /* Default Algorithm, SRGB and MSD */
137 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
138 new->algo
[i
] = SR_ALGORITHM_UNSET
;
140 new->srgb
.range_size
= 0;
141 new->srgb
.lower_bound
= 0;
144 /* Create Link, Prefix and Range TLVs list */
145 new->ext_link
= list_new();
146 new->ext_prefix
= list_new();
147 new->ext_link
->del
= del_sr_link
;
148 new->ext_prefix
->del
= del_sr_pref
;
150 IPV4_ADDR_COPY(&new->adv_router
, rid
);
151 new->neighbor
= NULL
;
154 if (IS_DEBUG_OSPF_SR
)
155 zlog_debug(" |- Created new SR node for %s",
156 inet_ntoa(new->adv_router
));
160 /* Delete Segment Routing node */
161 static void sr_node_del(struct sr_node
*srn
)
167 /* Clean Extended Link */
168 list_delete_and_null(&srn
->ext_link
);
170 /* Clean Prefix List */
171 list_delete_and_null(&srn
->ext_prefix
);
173 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
176 /* Get SR Node for a given nexthop */
177 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
178 struct in_addr nexthop
)
180 struct ospf_interface
*oi
= NULL
;
181 struct ospf_neighbor
*nbr
= NULL
;
182 struct listnode
*node
;
183 struct route_node
*rn
;
188 if (OspfSR
.neighbors
== NULL
)
191 if (IS_DEBUG_OSPF_SR
)
192 zlog_debug(" |- Search SR-Node for nexthop %s",
195 /* First, search neighbor Router ID for this nexthop */
197 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
198 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
200 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
212 if (IS_DEBUG_OSPF_SR
)
213 zlog_debug(" |- Found nexthop Router ID %s",
214 inet_ntoa(nbr
->router_id
));
215 /* Then, search SR Node */
216 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
222 * Segment Routing Initialization functions
225 /* Segment Routing starter function */
226 static int ospf_sr_start(struct ospf
*ospf
)
228 struct route_node
*rn
;
229 struct ospf_lsa
*lsa
;
233 if (IS_DEBUG_OSPF_SR
)
234 zlog_debug("SR (%s): Start Segment Routing", __func__
);
236 /* Initialize self SR Node */
237 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
238 (void *)sr_node_new
);
244 /* Complete & Store self SR Node */
245 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
246 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
247 srn
->algo
[0] = OspfSR
.algo
[0];
248 srn
->msd
= OspfSR
.msd
;
251 if (IS_DEBUG_OSPF_EVENT
)
252 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
254 /* Start by looking to Router Info & Extended LSA in lsdb */
255 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
256 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
) {
257 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
260 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
262 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
263 ospf_sr_ri_lsa_update(lsa
);
265 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
266 ospf_sr_ext_prefix_lsa_update(lsa
);
268 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
269 ospf_sr_ext_link_lsa_update(lsa
);
281 /* Stop Segment Routing */
282 static void ospf_sr_stop(void)
285 if (IS_DEBUG_OSPF_SR
)
286 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
289 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
290 * be remove though list_delete_and_null() call. See sr_node_del()
292 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
296 * Segment Routing initialize function
300 * @return 0 if OK, -1 otherwise
302 int ospf_sr_init(void)
306 if (IS_DEBUG_OSPF_SR
)
307 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
309 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
310 OspfSR
.enabled
= false;
311 /* Only AREA flooding is supported in this release */
312 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
314 /* Initialize SRGB, Algorithms and MSD TLVs */
315 /* Only Algorithm SPF is supported */
316 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
317 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
318 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
320 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
321 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
324 /* Initialize Hash table for neighbor SR nodes */
325 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
326 if (OspfSR
.neighbors
== NULL
)
329 /* Initialize Route Table for prefix */
330 OspfSR
.prefix
= route_table_init();
331 if (OspfSR
.prefix
== NULL
)
334 /* Register Segment Routing VTY command */
335 ospf_sr_register_vty();
342 * Segment Routing termination function
347 void ospf_sr_term(void)
350 /* Stop Segment Routing */
353 /* Clear SR Node Table */
354 if (OspfSR
.neighbors
)
355 hash_free(OspfSR
.neighbors
);
357 /* Clear Prefix Table */
359 route_table_finish(OspfSR
.prefix
);
361 OspfSR
.enabled
= false;
366 * Segment Routing finish function
371 void ospf_sr_finish(void)
373 /* Stop Segment Routing */
376 OspfSR
.enabled
= false;
380 * Following functions are used to manipulate the
381 * Next Hop Label Forwarding entry (NHLFE)
384 /* Compute label from index */
385 static mpls_label_t
index2label(uint32_t index
, struct sr_srgb srgb
)
389 label
= srgb
.lower_bound
+ index
;
390 if (label
> (srgb
.lower_bound
+ srgb
.range_size
))
391 return MPLS_INVALID_LABEL
;
396 /* Get neighbor full structure from address */
397 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
400 struct ospf_neighbor
*nbr
;
401 struct ospf_interface
*oi
;
402 struct listnode
*node
;
403 struct route_node
*rn
;
409 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
410 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
413 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
415 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
416 route_unlock_node(rn
);
423 /* Get OSPF Path from address */
424 static struct ospf_path
*get_nexthop_by_addr(struct ospf
*top
,
425 struct prefix_ipv4 p
)
427 struct ospf_route
* or ;
428 struct ospf_path
*path
;
429 struct listnode
*node
;
430 struct route_node
*rn
;
436 if (IS_DEBUG_OSPF_SR
)
437 zlog_debug(" |- Search Nexthop for prefix %s/%u",
438 inet_ntoa(p
.prefix
), p
.prefixlen
);
440 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
443 * Check if we found an OSPF route. May be NULL if SPF has not
444 * yet populate routing table for this prefix.
449 route_unlock_node(rn
);
454 /* Then search path from this route */
455 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
))
456 if (path
->nexthop
.s_addr
!= INADDR_ANY
|| path
->ifindex
!= 0)
462 /* Compute NHLFE entry for Extended Link */
463 static int compute_link_nhlfe(struct sr_link
*srl
)
465 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
466 struct ospf_neighbor
*nh
;
469 if (IS_DEBUG_OSPF_SR
)
470 zlog_debug(" |- Compute NHLFE for link %s/%u",
471 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
472 srl
->nhlfe
[0].prefv4
.prefixlen
);
474 /* First determine the OSPF Neighbor */
475 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
477 /* Neighbor could be not found when OSPF Adjacency just fire up
478 * because SPF don't yet populate routing table. This NHLFE will
479 * be fixed later when SR SPF schedule will be called.
484 if (IS_DEBUG_OSPF_SR
)
485 zlog_debug(" |- Found nexthop NHLFE %s",
486 inet_ntoa(nh
->router_id
));
488 /* Set ifindex for this neighbor */
489 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
490 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
492 /* Update neighbor address for LAN_ADJ_SID */
493 if (srl
->type
== LAN_ADJ_SID
) {
494 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
495 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
498 /* Set Input & Output Label */
499 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
500 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
502 srl
->nhlfe
[0].label_in
=
503 index2label(srl
->sid
[0], srl
->srn
->srgb
);
504 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
505 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
507 srl
->nhlfe
[1].label_in
=
508 index2label(srl
->sid
[1], srl
->srn
->srgb
);
510 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
511 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
518 * Compute NHLFE entry for Extended Prefix
520 * @param srp - Segment Routing Prefix
522 * @return -1 if next hop is not found, 0 if nexthop has not changed
525 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
527 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
528 struct ospf_path
*nh
= NULL
;
529 struct sr_node
*srnext
;
532 if (IS_DEBUG_OSPF_SR
)
533 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
534 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
535 srp
->nhlfe
.prefv4
.prefixlen
);
537 /* First determine the nexthop */
538 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
540 /* Nexthop could be not found when OSPF Adjacency just fire up
541 * because SPF don't yet populate routing table. This NHLFE will
542 * be fixed later when SR SPF schedule will be called.
547 /* Check if NextHop has changed when call after running a new SPF */
548 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
549 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
552 if (IS_DEBUG_OSPF_SR
)
553 zlog_debug(" |- Found new next hop for this NHLFE: %s",
554 inet_ntoa(nh
->nexthop
));
557 * Get SR-Node for this nexthop. Could be not yet available
558 * as Extende Link / Prefix and Router Information are flooded
559 * after LSA Type 1 & 2 which populate the OSPF Route Table
561 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
565 /* And store this information for later update if SR Node is found */
566 srnext
->neighbor
= OspfSR
.self
;
567 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
570 srp
->nexthop
= srnext
;
573 * SR Node could be known, but SRGB could be not initialize
574 * This is due to the fact that Extended Link / Prefix could
575 * be received before corresponding Router Information LSA
577 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
578 || (srnext
->srgb
.range_size
== 0))
581 if (IS_DEBUG_OSPF_SR
)
582 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
583 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
584 inet_ntoa(srnext
->adv_router
));
586 /* Set ip addr & ifindex for this neighbor */
587 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
588 srp
->nhlfe
.ifindex
= nh
->ifindex
;
590 /* Compute Input Label with self SRGB */
591 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
593 * and Output Label with Next hop SR Node SRGB or Implicit Null label
594 * if next hop is the destination and request PHP
596 if ((srp
->nexthop
== NULL
)
597 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
598 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
599 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
600 srp
->nhlfe
.label_out
= srp
->sid
;
602 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
604 if (IS_DEBUG_OSPF_SR
)
605 zlog_debug(" |- Computed new labels in: %u out: %u",
606 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
612 /* Send MPLS Label entry to Zebra for installation or deletion */
613 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
621 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
622 stream_putc(s
, ZEBRA_LSP_SR
);
623 /* OSPF Segment Routing currently support only IPv4 */
624 stream_putl(s
, nhlfe
.prefv4
.family
);
625 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
626 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
627 stream_put_in_addr(s
, &nhlfe
.nexthop
);
628 stream_putl(s
, nhlfe
.ifindex
);
629 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
630 stream_putl(s
, nhlfe
.label_in
);
631 stream_putl(s
, nhlfe
.label_out
);
633 /* Put length at the first point of the stream. */
634 stream_putw_at(s
, 0, stream_get_endp(s
));
636 if (IS_DEBUG_OSPF_SR
)
637 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
638 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
639 nhlfe
.label_in
, nhlfe
.label_out
,
640 inet_ntoa(nhlfe
.prefv4
.prefix
),
641 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
643 return zclient_send_message(zclient
);
646 /* Request zebra to install/remove FEC in FIB */
647 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
649 struct zapi_route api
;
650 struct zapi_nexthop
*api_nh
;
652 /* Support only IPv4 */
653 if (nhlfe
.prefv4
.family
!= AF_INET
)
656 memset(&api
, 0, sizeof(api
));
657 api
.vrf_id
= VRF_DEFAULT
;
658 api
.type
= ZEBRA_ROUTE_OSPF
;
659 api
.safi
= SAFI_UNICAST
;
660 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
662 if (cmd
== ZEBRA_ROUTE_ADD
) {
664 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
665 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
667 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
668 api_nh
= &api
.nexthops
[0];
669 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
670 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
671 api_nh
->ifindex
= nhlfe
.ifindex
;
673 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
674 api_nh
->labels
[0] = nhlfe
.label_out
;
675 api_nh
->label_num
= 1;
676 api_nh
->vrf_id
= VRF_DEFAULT
;
680 if (IS_DEBUG_OSPF_SR
)
681 zlog_debug(" |- %s FEC %u for %s/%u via %u",
682 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
683 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
684 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
686 return zclient_route_send(cmd
, zclient
, &api
);
689 /* Add new NHLFE entry for SID */
690 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
692 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
693 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
694 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
695 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
699 /* Remove NHLFE entry for SID */
700 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
702 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
703 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
704 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
705 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
709 /* Update NHLFE entry for SID */
710 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
718 * Functions to parse and get Extended Link / Prefix
722 /* Extended Link SubTLVs Getter */
723 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
727 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
728 struct ext_subtlv_adj_sid
*adj_sid
;
729 struct ext_subtlv_lan_adj_sid
*lan_sid
;
730 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
732 struct tlv_header
*sub_tlvh
;
733 uint16_t length
= 0, sum
= 0, i
= 0;
735 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
737 /* Initialize TLV browsing */
738 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
739 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
740 + EXT_TLV_LINK_SIZE
);
741 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
742 switch (ntohs(sub_tlvh
->type
)) {
743 case EXT_SUBTLV_ADJ_SID
:
744 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
746 i
= CHECK_FLAG(adj_sid
->flags
,
747 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
750 srl
->flags
[i
] = adj_sid
->flags
;
751 if (CHECK_FLAG(adj_sid
->flags
,
752 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
753 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
755 srl
->sid
[i
] = ntohl(adj_sid
->value
);
756 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
758 case EXT_SUBTLV_LAN_ADJ_SID
:
759 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
760 srl
->type
= LAN_ADJ_SID
;
761 i
= CHECK_FLAG(lan_sid
->flags
,
762 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
765 srl
->flags
[i
] = lan_sid
->flags
;
766 if (CHECK_FLAG(lan_sid
->flags
,
767 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
768 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
770 srl
->sid
[i
] = ntohl(lan_sid
->value
);
771 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
772 &lan_sid
->neighbor_id
);
774 case EXT_SUBTLV_RMT_ITF_ADDR
:
775 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
776 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
777 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
782 sum
+= TLV_SIZE(sub_tlvh
);
785 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
786 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
787 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
788 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
789 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
790 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
791 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
792 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
794 if (IS_DEBUG_OSPF_SR
) {
795 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
796 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
797 srl
->nhlfe
[0].prefv4
.prefixlen
);
798 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
799 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
800 srl
->nhlfe
[1].prefv4
.prefixlen
);
806 /* Extended Prefix SubTLVs Getter */
807 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
810 struct sr_prefix
*srp
;
811 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
812 struct ext_subtlv_prefix_sid
*psid
;
814 struct tlv_header
*sub_tlvh
;
815 uint16_t length
= 0, sum
= 0;
817 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
819 /* Initialize TLV browsing */
820 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
821 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
822 + EXT_TLV_PREFIX_SIZE
);
823 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
824 switch (ntohs(sub_tlvh
->type
)) {
825 case EXT_SUBTLV_PREFIX_SID
:
826 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
827 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
828 flog_err(OSPF_ERR_SR_INVALID_ALGORITHM
,
829 "SR (%s): Unsupported Algorithm",
831 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
834 srp
->type
= PREF_SID
;
835 srp
->flags
= psid
->flags
;
836 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
837 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
839 srp
->sid
= ntohl(psid
->value
);
840 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
842 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
843 srp
->nhlfe
.prefv4
.family
= AF_INET
;
844 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
849 sum
+= TLV_SIZE(sub_tlvh
);
852 if (IS_DEBUG_OSPF_SR
)
853 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
854 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
855 srp
->nhlfe
.prefv4
.prefixlen
);
860 * Functions to manipulate Segment Routing Link & Prefix structures
863 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
864 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
866 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
867 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
868 && (srl1
->flags
[1] == srl2
->flags
[1]))
874 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
875 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
877 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
883 /* Update Segment Link of given Segment Routing Node */
884 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
887 struct listnode
*node
;
892 if ((srn
== NULL
) || (srl
== NULL
))
895 if (IS_DEBUG_OSPF_SR
)
896 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
898 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
899 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
900 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
901 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
904 /* Search for existing Segment Link */
905 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
906 if (lk
->instance
== srl
->instance
) {
911 if (IS_DEBUG_OSPF_SR
)
912 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
913 found
? "Update" : "Add",
914 GET_OPAQUE_ID(srl
->instance
),
915 inet_ntoa(srn
->adv_router
));
917 /* if not found, add new Segment Link and install NHLFE */
919 /* Complete SR-Link and add it to SR-Node list */
921 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
922 listnode_add(srn
->ext_link
, srl
);
923 /* Try to set MPLS table */
924 if (compute_link_nhlfe(srl
)) {
925 add_sid_nhlfe(srl
->nhlfe
[0]);
926 add_sid_nhlfe(srl
->nhlfe
[1]);
929 if (sr_link_cmp(lk
, srl
)) {
930 if (compute_link_nhlfe(srl
)) {
931 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
932 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
933 /* Replace Segment List */
934 listnode_delete(srn
->ext_link
, lk
);
935 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
937 IPV4_ADDR_COPY(&srl
->adv_router
,
939 listnode_add(srn
->ext_link
, srl
);
941 /* New NHLFE was not found.
942 * Just free the SR Link
944 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
948 * This is just an LSA refresh.
949 * Stop processing and free SR Link
951 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
956 /* Update Segment Prefix of given Segment Routing Node */
957 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
960 struct listnode
*node
;
961 struct sr_prefix
*pref
;
965 if (srn
== NULL
|| srp
== NULL
)
968 if (IS_DEBUG_OSPF_SR
)
969 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
971 /* Process only Global Prefix SID */
972 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
975 /* Search for existing Segment Prefix */
976 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
977 if (pref
->instance
== srp
->instance
) {
982 if (IS_DEBUG_OSPF_SR
)
983 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
984 found
? "Update" : "Add",
985 GET_OPAQUE_ID(srp
->instance
),
986 inet_ntoa(srn
->adv_router
));
988 /* if not found, add new Segment Prefix and install NHLFE */
990 /* Complete SR-Prefix and add it to SR-Node list */
992 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
993 listnode_add(srn
->ext_prefix
, srp
);
994 /* Try to set MPLS table */
995 if (compute_prefix_nhlfe(srp
) == 1)
996 add_sid_nhlfe(srp
->nhlfe
);
998 if (sr_prefix_cmp(pref
, srp
)) {
999 if (compute_prefix_nhlfe(srp
) == 1) {
1000 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
1001 /* Replace Segment Prefix */
1002 listnode_delete(srn
->ext_prefix
, pref
);
1003 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1005 IPV4_ADDR_COPY(&srp
->adv_router
,
1007 listnode_add(srn
->ext_prefix
, srp
);
1009 /* New NHLFE was not found.
1010 * Just free the SR Prefix
1012 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1015 /* This is just an LSA refresh.
1016 * Stop processing and free SR Prefix
1018 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1024 * When change the FRR Self SRGB, update the NHLFE Input Label
1025 * for all Extended Prefix with SID index through hash_iterate()
1027 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1029 struct listnode
*node
;
1030 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1031 struct sr_prefix
*srp
;
1032 struct sr_nhlfe
new;
1034 /* Process Every Extended Prefix for this SR-Node */
1035 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1036 /* Process Self SRN only if NO-PHP is requested */
1037 if ((srn
== OspfSR
.self
)
1038 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1041 /* Process only SID Index */
1042 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1045 /* OK. Compute new NHLFE */
1046 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1047 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1048 /* Update MPLS LFIB */
1049 update_sid_nhlfe(srp
->nhlfe
, new);
1050 /* Finally update Input Label */
1051 srp
->nhlfe
.label_in
= new.label_in
;
1056 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1057 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1059 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1061 struct listnode
*node
;
1062 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1063 struct sr_node
*srnext
= (struct sr_node
*)args
;
1064 struct sr_prefix
*srp
;
1065 struct sr_nhlfe
new;
1067 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1068 /* Process only SID Index for next hop without PHP */
1069 if ((srp
->nexthop
== NULL
)
1070 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1072 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1073 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1074 update_sid_nhlfe(srp
->nhlfe
, new);
1075 srp
->nhlfe
.label_out
= new.label_out
;
1080 * Following functions are call when new Segment Routing LSA are received
1081 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1082 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1083 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1086 /* Update Segment Routing from Router Information LSA */
1087 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1089 struct sr_node
*srn
;
1090 struct tlv_header
*tlvh
;
1091 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1092 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1093 struct ri_sr_tlv_sr_algorithm
*algo
;
1094 struct sr_srgb srgb
;
1095 uint16_t length
= 0, sum
= 0;
1097 if (IS_DEBUG_OSPF_SR
)
1099 "SR (%s): Process Router "
1100 "Information LSA 4.0.0.%u from %s",
1101 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1102 inet_ntoa(lsah
->adv_router
));
1105 if (IS_LSA_SELF(lsa
))
1108 if (OspfSR
.neighbors
== NULL
) {
1109 flog_err(OSPF_ERR_SR_INVALID_DB
,
1110 "SR (%s): Abort! no valid SR DataBase", __func__
);
1114 /* Get SR Node in hash table from Router ID */
1115 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1116 (void *)sr_node_new
);
1120 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1121 "SR (%s): Abort! can't create SR node in hash table",
1126 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1127 flog_err(OSPF_ERR_SR_INVALID_LSA_ID
,
1128 "SR (%s): Abort! Wrong "
1129 "LSA ID 4.0.0.%u for SR node %s/%u",
1130 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1131 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1135 /* Collect Router Information Sub TLVs */
1136 /* Initialize TLV browsing */
1137 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1138 srgb
.range_size
= 0;
1139 srgb
.lower_bound
= 0;
1141 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1142 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1143 switch (ntohs(tlvh
->type
)) {
1144 case RI_SR_TLV_SR_ALGORITHM
:
1145 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1148 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1149 srn
->algo
[i
] = algo
->value
[0];
1150 for (; i
< ALGORITHM_COUNT
; i
++)
1151 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1152 sum
+= TLV_SIZE(tlvh
);
1154 case RI_SR_TLV_SID_LABEL_RANGE
:
1155 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1156 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1158 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1159 sum
+= TLV_SIZE(tlvh
);
1161 case RI_SR_TLV_NODE_MSD
:
1162 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1163 sum
+= TLV_SIZE(tlvh
);
1166 sum
+= TLV_SIZE(tlvh
);
1171 /* Check that we collect mandatory parameters */
1172 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1173 || srgb
.lower_bound
== 0) {
1174 zlog_warn("SR (%s): Missing mandatory parameters. Abort!",
1176 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1177 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1181 /* Check if it is a new SR Node or not */
1182 if (srn
->instance
== 0) {
1184 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1186 srn
->srgb
.range_size
= srgb
.range_size
;
1187 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1190 /* Check if SRGB has changed */
1191 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1192 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1193 srn
->srgb
.range_size
= srgb
.range_size
;
1194 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1195 /* Update NHLFE if it is a neighbor SR node */
1196 if (srn
->neighbor
== OspfSR
.self
)
1197 hash_iterate(OspfSR
.neighbors
,
1198 (void (*)(struct hash_backet
*,
1199 void *))update_out_nhlfe
,
1205 * Delete SR Node entry in hash table information corresponding to an expired
1206 * Router Information LSA
1208 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1210 struct sr_node
*srn
;
1211 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1213 if (IS_DEBUG_OSPF_SR
)
1214 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1215 __func__
, inet_ntoa(lsah
->adv_router
),
1216 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1219 if (OspfSR
.neighbors
== NULL
) {
1220 flog_err(OSPF_ERR_SR_INVALID_DB
,
1221 "SR (%s): Abort! no valid SR Data Base", __func__
);
1225 /* Release Router ID entry in SRDB hash table */
1226 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1230 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1231 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1232 __func__
, inet_ntoa(lsah
->adv_router
));
1236 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1238 OSPF_ERR_SR_INVALID_LSA_ID
,
1239 "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 flog_err(OSPF_ERR_SR_INVALID_DB
,
1268 "SR (%s): Abort! no valid SR DataBase", __func__
);
1272 /* Get SR Node in hash table from Router ID */
1273 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1274 (void *)&(lsah
->adv_router
),
1275 (void *)sr_node_new
);
1279 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1280 "SR (%s): Abort! can't create SR node in hash table",
1285 /* Initialize TLV browsing */
1286 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1288 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1289 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1290 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1291 /* Got Extended Link information */
1292 srl
= get_ext_link_sid(tlvh
);
1293 /* Update SID if not null */
1295 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1296 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1299 sum
+= TLV_SIZE(tlvh
);
1303 /* Delete Segment Routing from Extended Link LSA */
1304 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1306 struct listnode
*node
;
1307 struct sr_link
*srl
;
1308 struct sr_node
*srn
;
1309 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1310 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1312 if (IS_DEBUG_OSPF_SR
)
1313 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1314 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1315 inet_ntoa(lsah
->adv_router
));
1318 if (OspfSR
.neighbors
== NULL
) {
1319 flog_err(OSPF_ERR_SR_INVALID_DB
,
1320 "SR (%s): Abort! no valid SR DataBase", __func__
);
1324 /* Search SR Node in hash table from Router ID */
1325 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1326 (void *)&(lsah
->adv_router
));
1329 * SR-Node may be NULL if it has been remove previously when
1330 * processing Router Information LSA deletion
1333 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1334 __func__
, inet_ntoa(lsah
->adv_router
));
1338 /* Search for corresponding Segment Link */
1339 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1340 if (srl
->instance
== instance
)
1343 /* Remove Segment Link if found */
1344 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1345 del_sid_nhlfe(srl
->nhlfe
[0]);
1346 del_sid_nhlfe(srl
->nhlfe
[1]);
1347 listnode_delete(srn
->ext_link
, srl
);
1348 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1351 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1353 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1354 inet_ntoa(lsah
->adv_router
));
1358 /* Update Segment Routing from Extended Prefix LSA */
1359 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1361 struct sr_node
*srn
;
1362 struct tlv_header
*tlvh
;
1363 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1364 struct sr_prefix
*srp
;
1366 uint16_t length
, sum
;
1368 if (IS_DEBUG_OSPF_SR
)
1370 "SR (%s): Process Extended Prefix LSA "
1372 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1373 inet_ntoa(lsah
->adv_router
));
1376 if (OspfSR
.neighbors
== NULL
) {
1377 flog_err(OSPF_ERR_SR_INVALID_DB
,
1378 "SR (%s): Abort! no valid SR DataBase", __func__
);
1382 /* Get SR Node in hash table from Router ID */
1383 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1384 (void *)&(lsah
->adv_router
),
1385 (void *)sr_node_new
);
1389 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1390 "SR (%s): Abort! can't create SR node in hash table",
1395 /* Initialize TLV browsing */
1396 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1398 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1399 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1400 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1401 /* Got Extended Link information */
1402 srp
= get_ext_prefix_sid(tlvh
);
1403 /* Update SID if not null */
1405 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1406 update_ext_prefix_sid(srn
, srp
);
1409 sum
+= TLV_SIZE(tlvh
);
1413 /* Delete Segment Routing from Extended Prefix LSA */
1414 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1416 struct listnode
*node
;
1417 struct sr_prefix
*srp
;
1418 struct sr_node
*srn
;
1419 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1420 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1422 if (IS_DEBUG_OSPF_SR
)
1424 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1425 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1426 inet_ntoa(lsah
->adv_router
));
1429 if (OspfSR
.neighbors
== NULL
) {
1430 flog_err(OSPF_ERR_SR_INVALID_DB
,
1431 "SR (%s): Abort! no valid SR DataBase", __func__
);
1435 /* Search SR Node in hash table from Router ID */
1436 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1437 (void *)&(lsah
->adv_router
));
1440 * SR-Node may be NULL if it has been remove previously when
1441 * processing Router Information LSA deletion
1444 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1445 __func__
, inet_ntoa(lsah
->adv_router
));
1449 /* Search for corresponding Segment Link */
1450 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1451 if (srp
->instance
== instance
)
1454 /* Remove Segment Link if found */
1455 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1456 del_sid_nhlfe(srp
->nhlfe
);
1457 listnode_delete(srn
->ext_link
, srp
);
1458 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1461 "SR (%s): Didn't found corresponding SR Prefix "
1462 "7.0.0.%u for SR Node %s",
1463 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1464 inet_ntoa(lsah
->adv_router
));
1468 /* Get Label for Extended Link SID */
1469 /* TODO: To be replace by Zebra Label Manager */
1470 uint32_t get_ext_link_label_value(void)
1472 static uint32_t label
= ADJ_SID_MIN
- 1;
1474 if (label
< ADJ_SID_MAX
)
1481 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1482 * complete initial CLI command at startutp.
1484 * @param ifp - Loopback interface
1485 * @param pref - Prefix address of this interface
1489 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1491 struct listnode
*node
;
1492 struct sr_prefix
*srp
;
1495 if ((ifp
== NULL
) || (p
== NULL
))
1499 * Search if there is a Segment Prefix that correspond to this
1500 * interface or prefix, and update it if found
1502 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1503 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1504 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1506 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1508 /* Update Interface & Prefix info */
1509 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1510 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1512 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1513 srp
->nhlfe
.prefv4
.family
= p
->family
;
1514 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1516 /* OK. Let's Schedule Extended Prefix LSA */
1517 srp
->instance
= ospf_ext_schedule_prefix_index(
1518 ifp
, srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1520 /* Install NHLFE if NO-PHP is requested */
1521 if (CHECK_FLAG(srp
->flags
,
1522 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1523 srp
->nhlfe
.label_in
= index2label(
1524 srp
->sid
, OspfSR
.self
->srgb
);
1525 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1526 add_sid_nhlfe(srp
->nhlfe
);
1533 * Following functions are used to update MPLS LFIB after a SPF run
1536 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1539 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1540 struct listnode
*node
;
1541 struct sr_prefix
*srp
;
1542 struct sr_nhlfe old
;
1545 if (IS_DEBUG_OSPF_SR
)
1546 zlog_debug(" |- Update Prefix for SR Node %s",
1547 inet_ntoa(srn
->adv_router
));
1549 /* Skip Self SR Node */
1550 if (srn
== OspfSR
.self
)
1553 /* Update Extended Prefix */
1554 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1556 /* Backup current NHLFE */
1557 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1559 /* Compute the new NHLFE */
1560 rc
= compute_prefix_nhlfe(srp
);
1562 /* Check computation result */
1564 /* next hop is not know, remove old NHLFE to avoid loop */
1566 del_sid_nhlfe(srp
->nhlfe
);
1568 /* next hop has not changed, skip it */
1571 /* there is a new next hop, update NHLFE */
1573 update_sid_nhlfe(old
, srp
->nhlfe
);
1581 static int ospf_sr_update_schedule(struct thread
*t
)
1585 struct timeval start_time
, stop_time
;
1587 ospf
= THREAD_ARG(t
);
1588 ospf
->t_sr_update
= NULL
;
1593 monotime(&start_time
);
1595 if (IS_DEBUG_OSPF_SR
)
1596 zlog_debug("SR (%s): Start SPF update", __func__
);
1598 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1599 void *))ospf_sr_nhlfe_update
,
1602 monotime(&stop_time
);
1604 if (IS_DEBUG_OSPF_SR
)
1605 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld\n",
1607 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1608 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1610 OspfSR
.update
= false;
1614 #define OSPF_SR_UPDATE_INTERVAL 1
1616 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1622 /* Check if an update is not alreday engage */
1626 OspfSR
.update
= true;
1628 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1629 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1633 * --------------------------------------
1634 * Followings are vty command functions.
1635 * --------------------------------------
1639 * Segment Routing Router configuration
1641 * Must be centralize as it concerns both Extended Link/Prefix LSA
1642 * and Router Information LSA. Choose to call it from Extended Prefix
1643 * write_config() call back.
1645 * @param vty VTY output
1649 void ospf_sr_config_write_router(struct vty
*vty
)
1651 struct listnode
*node
;
1652 struct sr_prefix
*srp
;
1654 if (OspfSR
.enabled
) {
1655 vty_out(vty
, " segment-routing on\n");
1657 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1658 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1659 vty_out(vty
, " segment-routing global-block %u %u\n",
1660 OspfSR
.srgb
.lower_bound
,
1661 OspfSR
.srgb
.lower_bound
+ OspfSR
.srgb
.range_size
1664 if (OspfSR
.msd
!= 0)
1665 vty_out(vty
, " segment-routing node-msd %u\n",
1668 if (OspfSR
.self
!= NULL
) {
1669 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1672 " segment-routing prefix %s/%u "
1674 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1675 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1676 CHECK_FLAG(srp
->flags
,
1677 EXT_SUBTLV_PREFIX_SID_NPFLG
)
1685 DEFUN(ospf_sr_enable
,
1687 "segment-routing on",
1689 "Enable Segment Routing\n")
1692 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1697 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1699 "Segment Routing is only supported in default "
1701 return CMD_WARNING_CONFIG_FAILED
;
1704 if (IS_DEBUG_OSPF_EVENT
)
1705 zlog_debug("SR: Segment Routing: OFF -> ON");
1707 /* Start Segment Routing */
1708 OspfSR
.enabled
= true;
1709 if (!ospf_sr_start(ospf
)) {
1710 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1714 /* Set Router Information SR parameters */
1715 if (IS_DEBUG_OSPF_EVENT
)
1716 zlog_debug("SR: Activate SR for Router Information LSA");
1718 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1720 /* Update Ext LSA */
1721 if (IS_DEBUG_OSPF_EVENT
)
1722 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1724 ospf_ext_update_sr(true);
1729 DEFUN (no_ospf_sr_enable
,
1730 no_ospf_sr_enable_cmd
,
1731 "no segment-routing [on]",
1734 "Disable Segment Routing\n")
1737 if (!OspfSR
.enabled
)
1740 if (IS_DEBUG_OSPF_EVENT
)
1741 zlog_debug("SR: Segment Routing: ON -> OFF");
1743 /* Start by Disabling Extended Link & Prefix LSA */
1744 ospf_ext_update_sr(false);
1746 /* then, disable Router Information SR parameters */
1747 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1749 /* Finally, stop Segment Routing */
1751 OspfSR
.enabled
= false;
1756 static int ospf_sr_enabled(struct vty
*vty
)
1762 vty_out(vty
, "%% OSPF SR is not turned on\n");
1767 DEFUN (sr_sid_label_range
,
1768 sr_sid_label_range_cmd
,
1769 "segment-routing global-block (0-1048575) (0-1048575)",
1771 "Segment Routing Global Block label range\n"
1772 "Lower-bound range in decimal (0-1048575)\n"
1773 "Upper-bound range in decimal (0-1048575)\n")
1781 if (!ospf_sr_enabled(vty
))
1782 return CMD_WARNING_CONFIG_FAILED
;
1784 /* Get lower and upper bound */
1785 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1786 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1787 size
= upper
- lower
+ 1;
1789 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1791 "Range size cannot be less than 0 or more than %u\n",
1792 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1793 return CMD_WARNING_CONFIG_FAILED
;
1796 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1797 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1798 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1799 return CMD_WARNING_CONFIG_FAILED
;
1802 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1803 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1804 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1805 return CMD_WARNING_CONFIG_FAILED
;
1808 /* Check if values have changed */
1809 if ((OspfSR
.srgb
.range_size
== size
)
1810 && (OspfSR
.srgb
.lower_bound
== lower
))
1813 /* Set SID/Label range SRGB */
1814 OspfSR
.srgb
.range_size
= size
;
1815 OspfSR
.srgb
.lower_bound
= lower
;
1816 if (OspfSR
.self
!= NULL
) {
1817 OspfSR
.self
->srgb
.range_size
= size
;
1818 OspfSR
.self
->srgb
.lower_bound
= lower
;
1821 /* Set Router Information SR parameters */
1822 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1824 /* Update NHLFE entries */
1825 hash_iterate(OspfSR
.neighbors
,
1826 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1832 DEFUN (no_sr_sid_label_range
,
1833 no_sr_sid_label_range_cmd
,
1834 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1837 "Segment Routing Global Block label range\n"
1838 "Lower-bound range in decimal (0-1048575)\n"
1839 "Upper-bound range in decimal (0-1048575)\n")
1842 if (!ospf_sr_enabled(vty
))
1843 return CMD_WARNING_CONFIG_FAILED
;
1845 /* Revert to default SRGB value */
1846 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1847 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1848 if (OspfSR
.self
!= NULL
) {
1849 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1850 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1853 /* Set Router Information SR parameters */
1854 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1856 /* Update NHLFE entries */
1857 hash_iterate(OspfSR
.neighbors
,
1858 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1866 "segment-routing node-msd (1-16)",
1868 "Maximum Stack Depth for this router\n"
1869 "Maximum number of label that could be stack (1-16)\n")
1874 if (!ospf_sr_enabled(vty
))
1875 return CMD_WARNING_CONFIG_FAILED
;
1878 argv_find(argv
, argc
, "(1-16)", &idx
);
1879 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1880 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1881 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1883 return CMD_WARNING_CONFIG_FAILED
;
1886 /* Check if value has changed */
1887 if (OspfSR
.msd
== msd
)
1890 /* Set this router MSD */
1892 if (OspfSR
.self
!= NULL
)
1893 OspfSR
.self
->msd
= msd
;
1895 /* Set Router Information SR parameters */
1896 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1901 DEFUN (no_sr_node_msd
,
1903 "no segment-routing node-msd [(1-16)]",
1906 "Maximum Stack Depth for this router\n"
1907 "Maximum number of label that could be stack (1-16)\n")
1910 if (!ospf_sr_enabled(vty
))
1911 return CMD_WARNING_CONFIG_FAILED
;
1913 /* unset this router MSD */
1915 if (OspfSR
.self
!= NULL
)
1916 OspfSR
.self
->msd
= 0;
1918 /* Set Router Information SR parameters */
1919 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1924 DEFUN (sr_prefix_sid
,
1926 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1929 "IPv4 Prefix as A.B.C.D/M\n"
1930 "SID index for this prefix in decimal (0-65535)\n"
1931 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1932 "Don't request Penultimate Hop Popping (PHP)\n")
1937 struct listnode
*node
;
1938 struct sr_prefix
*srp
, *new;
1939 struct interface
*ifp
;
1941 if (!ospf_sr_enabled(vty
))
1942 return CMD_WARNING_CONFIG_FAILED
;
1944 /* Get network prefix */
1945 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1946 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1947 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
1948 return CMD_WARNING_CONFIG_FAILED
;
1951 /* Get & verify index value */
1952 argv_find(argv
, argc
, "(0-65535)", &idx
);
1953 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1954 if (index
> OspfSR
.srgb
.range_size
- 1) {
1955 vty_out(vty
, "Index %u must be lower than range size %u\n",
1956 index
, OspfSR
.srgb
.range_size
);
1957 return CMD_WARNING_CONFIG_FAILED
;
1960 /* check that the index is not already used */
1961 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1962 if (srp
->sid
== index
) {
1963 vty_out(vty
, "Index %u is already used\n", index
);
1964 return CMD_WARNING_CONFIG_FAILED
;
1968 /* Create new Extended Prefix to SRDB if not found */
1969 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1970 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1971 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1972 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1973 new->nhlfe
.prefv4
.family
= p
.family
;
1975 /* Set NO PHP flag if present and compute NHLFE */
1976 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1977 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1978 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1979 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1982 if (IS_DEBUG_OSPF_SR
)
1983 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1984 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
1985 new->nhlfe
.prefv4
.prefixlen
);
1987 /* Get Interface and check if it is a Loopback */
1988 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1991 * Interface could be not yet available i.e. when this
1992 * command is in the configuration file, OSPF is not yet
1993 * ready. In this case, store the prefix SID for latter
1994 * update of this Extended Prefix
1996 listnode_add(OspfSR
.self
->ext_prefix
, new);
1998 "Interface for prefix %s/%u not found. Deferred LSA "
2000 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2004 if (!if_is_loopback(ifp
)) {
2005 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2006 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
2007 return CMD_WARNING_CONFIG_FAILED
;
2009 new->nhlfe
.ifindex
= ifp
->ifindex
;
2011 /* Search if this prefix already exist */
2012 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2013 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2014 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2020 /* Update or Add this new SR Prefix */
2022 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2023 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2024 listnode_add(OspfSR
.self
->ext_prefix
, new);
2026 listnode_add(OspfSR
.self
->ext_prefix
, new);
2027 add_sid_nhlfe(new->nhlfe
);
2030 /* Finally, update Extended Prefix LSA */
2031 new->instance
= ospf_ext_schedule_prefix_index(
2032 ifp
, new->sid
, &new->nhlfe
.prefv4
, new->flags
);
2033 if (new->instance
== 0) {
2034 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2035 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2042 DEFUN (no_sr_prefix_sid
,
2043 no_sr_prefix_sid_cmd
,
2044 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2048 "IPv4 Prefix as A.B.C.D/M\n"
2049 "SID index for this prefix in decimal (0-65535)\n"
2050 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2051 "Don't request Penultimate Hop Popping (PHP)\n")
2055 struct listnode
*node
;
2056 struct sr_prefix
*srp
;
2057 struct interface
*ifp
;
2061 /* Get network prefix */
2062 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2063 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2065 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2066 return CMD_WARNING_CONFIG_FAILED
;
2069 /* check that the prefix is already set */
2070 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2071 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2072 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2078 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2080 return CMD_WARNING_CONFIG_FAILED
;
2084 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2086 vty_out(vty
, "interface for prefix %s not found.\n",
2088 return CMD_WARNING_CONFIG_FAILED
;
2091 /* Update Extended Prefix LSA */
2092 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2093 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2097 if (IS_DEBUG_OSPF_SR
)
2098 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2099 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2100 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2102 /* Delete NHLFE is NO-PHP is set */
2103 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2104 del_sid_nhlfe(srp
->nhlfe
);
2106 /* OK, all is clean, remove SRP from SRDB */
2107 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2108 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2114 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2115 struct sr_node
*srn
)
2118 struct listnode
*node
;
2119 struct sr_link
*srl
;
2120 struct sr_prefix
*srp
;
2121 struct interface
*itf
;
2125 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2126 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2133 json_node
= json_object_new_object();
2134 json_object_string_add(json_node
, "routerID",
2135 inet_ntoa(srn
->adv_router
));
2136 json_object_int_add(json_node
, "srgbSize",
2137 srn
->srgb
.range_size
);
2138 json_object_int_add(json_node
, "srgbLabel",
2139 srn
->srgb
.lower_bound
);
2140 json_algo
= json_object_new_array();
2141 json_object_object_add(json_node
, "algorithms", json_algo
);
2142 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2143 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2145 json_obj
= json_object_new_object();
2148 snprintf(tmp
, 2, "%u", i
);
2149 json_object_string_add(json_obj
, tmp
,
2150 srn
->algo
[i
] == SR_ALGORITHM_SPF
2153 json_object_array_add(json_algo
, json_obj
);
2156 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2158 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2159 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2160 srn
->srgb
.lower_bound
);
2161 vty_out(vty
, "\tAlgorithm(s): %s",
2162 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2163 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2164 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2167 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2171 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2176 "\n\n Prefix or Link Label In Label Out "
2177 "Node or Adj. SID Interface Nexthop\n");
2179 "------------------ -------- --------- "
2180 "--------------------- --------- ---------------\n");
2182 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2183 snprintf(pref
, 19, "%s/%u", inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2184 srp
->nhlfe
.prefv4
.prefixlen
);
2185 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2186 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2187 sprintf(label
, "pop");
2189 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2190 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2193 json_prefix
= json_object_new_array();
2194 json_object_object_add(json_node
,
2198 json_obj
= json_object_new_object();
2199 json_object_string_add(json_obj
, "prefix", pref
);
2200 json_object_int_add(json_obj
, "sid", srp
->sid
);
2201 json_object_int_add(json_obj
, "inputLabel",
2202 srp
->nhlfe
.label_in
);
2203 json_object_string_add(json_obj
, "outputLabel", label
);
2204 json_object_string_add(json_obj
, "interface",
2205 itf
? itf
->name
: "-");
2206 json_object_string_add(json_obj
, "nexthop",
2207 inet_ntoa(srp
->nhlfe
.nexthop
));
2208 json_object_array_add(json_prefix
, json_obj
);
2210 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2211 srp
->nhlfe
.label_in
, label
, sid
,
2212 itf
? itf
->name
: "-",
2213 inet_ntoa(srp
->nhlfe
.nexthop
));
2217 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2218 snprintf(pref
, 19, "%s/%u",
2219 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2220 srl
->nhlfe
[0].prefv4
.prefixlen
);
2221 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2222 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2223 sprintf(label
, "pop");
2225 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2226 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2229 json_link
= json_object_new_array();
2230 json_object_object_add(
2231 json_node
, "extendedLink", json_link
);
2234 json_obj
= json_object_new_object();
2235 json_object_string_add(json_obj
, "prefix", pref
);
2236 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2237 json_object_int_add(json_obj
, "inputLabel",
2238 srl
->nhlfe
[0].label_in
);
2239 json_object_string_add(json_obj
, "outputLabel", label
);
2240 json_object_string_add(json_obj
, "interface",
2241 itf
? itf
->name
: "-");
2242 json_object_string_add(
2243 json_obj
, "nexthop",
2244 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2245 json_object_array_add(json_link
, json_obj
);
2247 json_obj
= json_object_new_object();
2248 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2249 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2250 sprintf(label
, "pop");
2252 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2253 json_object_string_add(json_obj
, "prefix", pref
);
2254 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2255 json_object_int_add(json_obj
, "inputLabel",
2256 srl
->nhlfe
[1].label_in
);
2257 json_object_string_add(json_obj
, "outputLabel", label
);
2258 json_object_string_add(json_obj
, "interface",
2259 itf
? itf
->name
: "-");
2260 json_object_string_add(
2261 json_obj
, "nexthop",
2262 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2263 json_object_array_add(json_link
, json_obj
);
2265 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2266 srl
->nhlfe
[0].label_in
, label
, sid
,
2267 itf
? itf
->name
: "-",
2268 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2269 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2270 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2271 sprintf(label
, "pop");
2273 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2274 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2275 srl
->nhlfe
[1].label_in
, label
, sid
,
2276 itf
? itf
->name
: "-",
2277 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2281 json_object_array_add(json
, json_node
);
2286 static void show_vty_srdb(struct hash_backet
*backet
, void *args
)
2288 struct vty
*vty
= (struct vty
*)args
;
2289 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2291 show_sr_node(vty
, NULL
, srn
);
2294 static void show_json_srdb(struct hash_backet
*backet
, void *args
)
2296 struct json_object
*json
= (struct json_object
*)args
;
2297 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2299 show_sr_node(NULL
, json
, srn
);
2302 DEFUN (show_ip_opsf_srdb
,
2303 show_ip_ospf_srdb_cmd
,
2304 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2308 "Database summary\n"
2309 "Show Segment Routing Data Base\n"
2310 "Advertising SR node\n"
2311 "Advertising SR node ID (as an IP address)\n"
2312 "Self-originated SR node\n"
2317 struct sr_node
*srn
;
2318 bool uj
= use_json(argc
, argv
);
2319 json_object
*json
= NULL
, *json_node_array
= NULL
;
2321 if (!OspfSR
.enabled
) {
2322 vty_out(vty
, "Segment Routing is disabled on this router\n");
2327 json
= json_object_new_object();
2328 json_node_array
= json_object_new_array();
2329 json_object_string_add(json
, "srdbID",
2330 inet_ntoa(OspfSR
.self
->adv_router
));
2331 json_object_object_add(json
, "srNodes", json_node_array
);
2334 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2335 inet_ntoa(OspfSR
.self
->adv_router
));
2338 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2340 show_sr_node(vty
, json_node_array
, srn
);
2342 vty_out(vty
, "%s\n",
2343 json_object_to_json_string_ext(
2344 json
, JSON_C_TO_STRING_PRETTY
));
2345 json_object_free(json
);
2350 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2351 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2352 vty_out(vty
, "Specified Router ID %s is invalid\n",
2354 return CMD_WARNING_CONFIG_FAILED
;
2356 /* Get the SR Node from the SRDB */
2357 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2359 show_sr_node(vty
, json_node_array
, srn
);
2361 vty_out(vty
, "%s\n",
2362 json_object_to_json_string_ext(
2363 json
, JSON_C_TO_STRING_PRETTY
));
2364 json_object_free(json
);
2369 /* No parameters have been provided, Iterate through all the SRDB */
2371 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2372 void *))show_json_srdb
,
2373 (void *)json_node_array
);
2374 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2375 json
, JSON_C_TO_STRING_PRETTY
));
2376 json_object_free(json
);
2378 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2379 void *))show_vty_srdb
,
2385 /* Install new CLI commands */
2386 void ospf_sr_register_vty(void)
2388 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2390 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2391 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2392 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2393 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2394 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2395 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2396 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2397 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);