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() */
51 #include "ospfd/ospfd.h"
52 #include "ospfd/ospf_interface.h"
53 #include "ospfd/ospf_ism.h"
54 #include "ospfd/ospf_asbr.h"
55 #include "ospfd/ospf_lsa.h"
56 #include "ospfd/ospf_lsdb.h"
57 #include "ospfd/ospf_neighbor.h"
58 #include "ospfd/ospf_nsm.h"
59 #include "ospfd/ospf_flood.h"
60 #include "ospfd/ospf_packet.h"
61 #include "ospfd/ospf_spf.h"
62 #include "ospfd/ospf_dump.h"
63 #include "ospfd/ospf_route.h"
64 #include "ospfd/ospf_ase.h"
65 #include "ospfd/ospf_sr.h"
66 #include "ospfd/ospf_ri.h"
67 #include "ospfd/ospf_ext.h"
68 #include "ospfd/ospf_zebra.h"
71 * Global variable to manage Segment Routing on this node.
72 * Note that all parameter values are stored in network byte order.
74 static struct ospf_sr_db OspfSR
;
75 static void ospf_sr_register_vty(void);
76 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
);
79 * Segment Routing Data Base functions
82 /* Hash function for Segment Routing entry */
83 static unsigned int sr_hash(void *p
)
85 const struct in_addr
*rid
= p
;
87 return jhash_1word(rid
->s_addr
, 0);
90 /* Compare 2 Router ID hash entries based on SR Node */
91 static int sr_cmp(const void *p1
, const void *p2
)
93 const struct sr_node
*srn
= p1
;
94 const struct in_addr
*rid
= p2
;
96 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
99 /* Functions to remove an SR Link */
100 static void del_sr_link(void *val
)
102 struct sr_link
*srl
= (struct sr_link
*)val
;
104 del_sid_nhlfe(srl
->nhlfe
[0]);
105 del_sid_nhlfe(srl
->nhlfe
[1]);
106 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
);
120 /* Allocate new Segment Routine node */
121 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
129 /* Allocate Segment Routing node memory */
130 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
134 zlog_err("SR (%s): Abort! can't create new SR node", __func__
);
138 /* Default Algorithm, SRGB and MSD */
139 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
140 new->algo
[i
] = SR_ALGORITHM_UNSET
;
142 new->srgb
.range_size
= 0;
143 new->srgb
.lower_bound
= 0;
146 /* Create Link, Prefix and Range TLVs list */
147 new->ext_link
= list_new();
148 new->ext_prefix
= list_new();
149 new->ext_link
->del
= del_sr_link
;
150 new->ext_prefix
->del
= del_sr_pref
;
152 /* Check if list are correctly created */
153 if (new->ext_link
== NULL
|| new->ext_prefix
== NULL
) {
154 list_delete_original(new->ext_link
);
155 list_delete_original(new->ext_prefix
);
156 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
160 IPV4_ADDR_COPY(&new->adv_router
, rid
);
161 new->neighbor
= NULL
;
164 if (IS_DEBUG_OSPF_SR
)
165 zlog_debug(" |- Created new SR node for %s",
166 inet_ntoa(new->adv_router
));
170 /* Delete Segment Routing node */
171 static void sr_node_del(struct sr_node
*srn
)
177 /* Clean Extended Link */
178 list_delete_and_null(&srn
->ext_link
);
180 /* Clean Prefix List */
181 list_delete_and_null(&srn
->ext_prefix
);
183 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
186 /* Get SR Node for a given nexthop */
187 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
188 struct in_addr nexthop
)
190 struct ospf_interface
*oi
= NULL
;
191 struct ospf_neighbor
*nbr
= NULL
;
192 struct listnode
*node
;
193 struct route_node
*rn
;
198 if (OspfSR
.neighbors
== NULL
)
201 if (IS_DEBUG_OSPF_SR
)
202 zlog_debug(" |- Search SR-Node for nexthop %s",
205 /* First, search neighbor Router ID for this nexthop */
207 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
208 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
210 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
222 if (IS_DEBUG_OSPF_SR
)
223 zlog_debug(" |- Found nexthop Router ID %s",
224 inet_ntoa(nbr
->router_id
));
225 /* Then, search SR Node */
226 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
232 * Segment Routing Initialization functions
235 /* Segment Routing starter function */
236 static int ospf_sr_start(struct ospf
*ospf
)
238 struct route_node
*rn
;
239 struct ospf_lsa
*lsa
;
243 if (IS_DEBUG_OSPF_SR
)
244 zlog_debug("SR (%s): Start Segment Routing", __func__
);
246 /* Initialize self SR Node */
247 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
248 (void *)sr_node_new
);
254 /* Complete & Store self SR Node */
255 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
256 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
257 srn
->algo
[0] = OspfSR
.algo
[0];
258 srn
->msd
= OspfSR
.msd
;
261 if (IS_DEBUG_OSPF_EVENT
)
262 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
264 /* Start by looking to Router Info & Extended LSA in lsdb */
265 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
266 LSDB_LOOP(OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
)
268 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
271 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
273 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
274 ospf_sr_ri_lsa_update(lsa
);
276 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
277 ospf_sr_ext_prefix_lsa_update(lsa
);
279 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
280 ospf_sr_ext_link_lsa_update(lsa
);
292 /* Stop Segment Routing */
293 static void ospf_sr_stop(void)
296 if (IS_DEBUG_OSPF_SR
)
297 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
300 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
301 * be remove though list_delete_and_null() call. See sr_node_del()
303 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
307 * Segment Routing initialize function
311 * @return 0 if OK, -1 otherwise
313 int ospf_sr_init(void)
317 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
319 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
320 OspfSR
.enabled
= false;
321 /* Only AREA flooding is supported in this release */
322 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
324 /* Initialize SRGB, Algorithms and MSD TLVs */
325 /* Only Algorithm SPF is supported */
326 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
327 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
328 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
330 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
331 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
334 /* Initialize Hash table for neighbor SR nodes */
335 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
336 if (OspfSR
.neighbors
== NULL
)
339 /* Initialize Route Table for prefix */
340 OspfSR
.prefix
= route_table_init();
341 if (OspfSR
.prefix
== NULL
)
344 /* Register Segment Routing VTY command */
345 ospf_sr_register_vty();
352 * Segment Routing termination function
358 void ospf_sr_term(void)
361 /* Stop Segment Routing */
364 /* Clear SR Node Table */
365 if (OspfSR
.neighbors
)
366 hash_free(OspfSR
.neighbors
);
368 /* Clear Prefix Table */
370 route_table_finish(OspfSR
.prefix
);
372 OspfSR
.enabled
= false;
376 * Following functions are used to manipulate the
377 * Next Hop Label Forwarding entry (NHLFE)
380 /* Compute label from index */
381 static mpls_label_t
index2label(uint32_t index
, struct sr_srgb srgb
)
385 label
= srgb
.lower_bound
+ index
;
386 if (label
> (srgb
.lower_bound
+ srgb
.range_size
))
387 return MPLS_INVALID_LABEL
;
392 /* Get neighbor full structure from address */
393 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
396 struct ospf_neighbor
*nbr
;
397 struct ospf_interface
*oi
;
398 struct listnode
*node
;
399 struct route_node
*rn
;
405 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
406 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
409 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
411 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
412 route_unlock_node(rn
);
419 /* Get OSPF Path from address */
420 static struct ospf_path
*get_nexthop_by_addr(struct ospf
*top
,
421 struct prefix_ipv4 p
)
423 struct ospf_route
*or;
424 struct ospf_path
*path
;
425 struct listnode
*node
;
426 struct route_node
*rn
;
429 if ((top
== NULL
) && (top
->new_table
))
432 if (IS_DEBUG_OSPF_SR
)
433 zlog_debug(" |- Search Nexthop for prefix %s/%u",
434 inet_ntoa(p
.prefix
), p
.prefixlen
);
436 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
439 * Check if we found an OSPF route. May be NULL if SPF has not
440 * yet populate routing table for this prefix.
445 route_unlock_node(rn
);
450 /* Then search path from this route */
451 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
))
452 if (path
->nexthop
.s_addr
!= INADDR_ANY
|| path
->ifindex
!= 0)
458 /* Compute NHLFE entry for Extended Link */
459 static int compute_link_nhlfe(struct sr_link
*srl
)
461 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
462 struct ospf_neighbor
*nh
;
465 if (IS_DEBUG_OSPF_SR
)
466 zlog_debug(" |- Compute NHLFE for link %s/%u",
467 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
468 srl
->nhlfe
[0].prefv4
.prefixlen
);
470 /* First determine the OSPF Neighbor */
471 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
473 /* Neighbor could be not found when OSPF Adjacency just fire up
474 * because SPF don't yet populate routing table. This NHLFE will
475 * be fixed later when SR SPF schedule will be called.
480 if (IS_DEBUG_OSPF_SR
)
481 zlog_debug(" |- Found nexthop NHLFE %s",
482 inet_ntoa(nh
->router_id
));
484 /* Set ifindex for this neighbor */
485 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
486 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
488 /* Set Input & Output Label */
489 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
490 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
492 srl
->nhlfe
[0].label_in
=
493 index2label(srl
->sid
[0], srl
->srn
->srgb
);
494 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
495 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
497 srl
->nhlfe
[1].label_in
=
498 index2label(srl
->sid
[1], srl
->srn
->srgb
);
500 srl
->nhlfe
[0].label_out
= MPLS_IMP_NULL_LABEL
;
501 srl
->nhlfe
[1].label_out
= MPLS_IMP_NULL_LABEL
;
508 * Compute NHLFE entry for Extended Prefix
510 * @param srp - Segment Routing Prefix
512 * @return -1 if next hop is not found, 0 if nexthop has not changed
515 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
517 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
518 struct ospf_path
*nh
= NULL
;
519 struct sr_node
*srnext
;
522 if (IS_DEBUG_OSPF_SR
)
523 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
524 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
525 srp
->nhlfe
.prefv4
.prefixlen
);
527 /* First determine the nexthop */
528 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
530 /* Nexthop could be not found when OSPF Adjacency just fire up
531 * because SPF don't yet populate routing table. This NHLFE will
532 * be fixed later when SR SPF schedule will be called.
537 /* Check if NextHop has changed when call after running a new SPF */
538 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
539 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
542 if (IS_DEBUG_OSPF_SR
)
543 zlog_debug(" |- Found new next hop for this NHLFE: %s",
544 inet_ntoa(nh
->nexthop
));
547 * Get SR-Node for this nexthop. Could be not yet available
548 * as Extende Link / Prefix and Router Information are flooded
549 * after LSA Type 1 & 2 which populate the OSPF Route Table
551 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
555 /* And store this information for later update if SR Node is found */
556 srnext
->neighbor
= OspfSR
.self
;
557 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
560 srp
->nexthop
= srnext
;
563 * SR Node could be known, but SRGB could be not initialize
564 * This is due to the fact that Extended Link / Prefix could
565 * be received before corresponding Router Information LSA
567 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
568 || (srnext
->srgb
.range_size
== 0))
571 if (IS_DEBUG_OSPF_SR
)
572 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
573 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
574 inet_ntoa(srnext
->adv_router
));
576 /* Set ip addr & ifindex for this neighbor */
577 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
578 srp
->nhlfe
.ifindex
= nh
->ifindex
;
580 /* Compute Input Label with self SRGB */
581 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
583 * and Output Label with Next hop SR Node SRGB or Implicit Null label
584 * if next hop is the destination and request PHP
586 if ((srp
->nexthop
== NULL
)
587 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
588 srp
->nhlfe
.label_out
= MPLS_IMP_NULL_LABEL
;
589 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
590 srp
->nhlfe
.label_out
= srp
->sid
;
592 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
594 if (IS_DEBUG_OSPF_SR
)
595 zlog_debug(" |- Computed new labels in: %u out: %u",
596 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
602 /* Send MPLS Label entry to Zebra for installation or deletion */
603 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
611 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
612 stream_putc(s
, ZEBRA_LSP_SR
);
613 /* OSPF Segment Routing currently support only IPv4 */
614 stream_putl(s
, nhlfe
.prefv4
.family
);
615 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
616 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
617 stream_put_in_addr(s
, &nhlfe
.nexthop
);
618 stream_putl(s
, nhlfe
.ifindex
);
619 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
620 stream_putl(s
, nhlfe
.label_in
);
621 stream_putl(s
, nhlfe
.label_out
);
623 /* Put length at the first point of the stream. */
624 stream_putw_at(s
, 0, stream_get_endp(s
));
626 if (IS_DEBUG_OSPF_SR
)
627 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
628 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
629 nhlfe
.label_in
, nhlfe
.label_out
,
630 inet_ntoa(nhlfe
.prefv4
.prefix
),
631 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
633 return zclient_send_message(zclient
);
636 /* Request zebra to install/remove FEC in FIB */
637 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
639 struct zapi_route api
;
640 struct zapi_nexthop
*api_nh
;
642 /* Support only IPv4 */
643 if (nhlfe
.prefv4
.family
!= AF_INET
)
646 memset(&api
, 0, sizeof(api
));
647 api
.vrf_id
= VRF_DEFAULT
;
648 api
.type
= ZEBRA_ROUTE_OSPF
;
649 api
.safi
= SAFI_UNICAST
;
650 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
652 if (cmd
== ZEBRA_ROUTE_ADD
) {
654 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
655 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
657 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
658 api_nh
= &api
.nexthops
[0];
659 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
660 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
661 api_nh
->ifindex
= nhlfe
.ifindex
;
663 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
664 api_nh
->labels
[0] = nhlfe
.label_out
;
665 api_nh
->label_num
= 1;
669 if (IS_DEBUG_OSPF_SR
)
670 zlog_debug(" |- %s FEC %u for %s/%u via %u",
671 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
672 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
673 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
675 return zclient_route_send(cmd
, zclient
, &api
);
678 /* Add new NHLFE entry for SID */
679 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
681 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
682 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
683 if (nhlfe
.label_out
!= MPLS_IMP_NULL_LABEL
)
684 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
688 /* Remove NHLFE entry for SID */
689 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
691 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
692 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
693 if (nhlfe
.label_out
!= MPLS_IMP_NULL_LABEL
)
694 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
698 /* Update NHLFE entry for SID */
699 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
707 * Functions to parse and get Extended Link / Prefix
711 /* Extended Link SubTLVs Getter */
712 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
716 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
717 struct ext_subtlv_adj_sid
*adj_sid
;
718 struct ext_subtlv_lan_adj_sid
*lan_sid
;
719 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
721 struct tlv_header
*sub_tlvh
;
722 uint16_t length
= 0, sum
= 0, i
= 0;
724 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
729 /* Initialize TLV browsing */
730 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
731 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
732 + EXT_TLV_LINK_SIZE
);
733 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
734 switch (ntohs(sub_tlvh
->type
)) {
735 case EXT_SUBTLV_ADJ_SID
:
736 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
738 i
= CHECK_FLAG(adj_sid
->flags
,
739 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
742 srl
->flags
[i
] = adj_sid
->flags
;
743 if (CHECK_FLAG(adj_sid
->flags
,
744 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
745 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
747 srl
->sid
[i
] = ntohl(adj_sid
->value
);
748 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
750 case EXT_SUBTLV_LAN_ADJ_SID
:
751 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
752 srl
->type
= LAN_ADJ_SID
;
753 i
= CHECK_FLAG(lan_sid
->flags
,
754 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
757 srl
->flags
[i
] = lan_sid
->flags
;
758 if (CHECK_FLAG(lan_sid
->flags
,
759 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
760 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
762 srl
->sid
[i
] = ntohl(lan_sid
->value
);
763 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
764 &lan_sid
->neighbor_id
);
766 case EXT_SUBTLV_RMT_ITF_ADDR
:
767 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
768 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
769 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
774 sum
+= TLV_SIZE(sub_tlvh
);
777 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
778 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
779 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
780 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
781 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
782 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
783 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
784 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
786 if (IS_DEBUG_OSPF_SR
) {
787 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
788 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
789 srl
->nhlfe
[0].prefv4
.prefixlen
);
790 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
791 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
792 srl
->nhlfe
[1].prefv4
.prefixlen
);
798 /* Extended Prefix SubTLVs Getter */
799 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
802 struct sr_prefix
*srp
;
803 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
804 struct ext_subtlv_prefix_sid
*psid
;
806 struct tlv_header
*sub_tlvh
;
807 uint16_t length
= 0, sum
= 0;
809 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
814 /* Initialize TLV browsing */
815 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
816 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
817 + EXT_TLV_PREFIX_SIZE
);
818 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
819 switch (ntohs(sub_tlvh
->type
)) {
820 case EXT_SUBTLV_PREFIX_SID
:
821 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
822 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
824 "SR (%s): Unsupported Algorithm",
826 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
829 srp
->type
= PREF_SID
;
830 srp
->flags
= psid
->flags
;
831 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
832 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
834 srp
->sid
= ntohl(psid
->value
);
835 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
837 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
838 srp
->nhlfe
.prefv4
.family
= AF_INET
;
839 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
844 sum
+= TLV_SIZE(sub_tlvh
);
847 if (IS_DEBUG_OSPF_SR
)
848 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
849 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
850 srp
->nhlfe
.prefv4
.prefixlen
);
855 * Functions to manipulate Segment Routing Link & Prefix structures
858 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
859 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
861 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
862 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
863 && (srl1
->flags
[1] == srl2
->flags
[1]))
869 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
870 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
872 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
878 /* Update Segment Link of given Segment Routing Node */
879 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
882 struct listnode
*node
;
887 if ((srn
== NULL
) || (srl
== NULL
))
890 if (IS_DEBUG_OSPF_SR
)
891 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
893 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
894 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
895 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
896 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
899 /* Search for existing Segment Link */
900 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
901 if (lk
->instance
== srl
->instance
) {
906 if (IS_DEBUG_OSPF_SR
)
907 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
908 found
? "Update" : "Add",
909 GET_OPAQUE_ID(srl
->instance
),
910 inet_ntoa(srn
->adv_router
));
912 /* if not found, add new Segment Link and install NHLFE */
914 /* Complete SR-Link and add it to SR-Node list */
916 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
917 listnode_add(srn
->ext_link
, srl
);
918 /* Try to set MPLS table */
919 if (compute_link_nhlfe(srl
)) {
920 add_sid_nhlfe(srl
->nhlfe
[0]);
921 add_sid_nhlfe(srl
->nhlfe
[1]);
924 if (sr_link_cmp(lk
, srl
)) {
925 if (compute_link_nhlfe(srl
)) {
926 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
927 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
928 /* Replace Segment List */
929 listnode_delete(srn
->ext_link
, lk
);
930 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
932 IPV4_ADDR_COPY(&srl
->adv_router
,
934 listnode_add(srn
->ext_link
, srl
);
936 /* New NHLFE was not found.
937 * Just free the SR Link
939 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
943 * This is just an LSA refresh.
944 * Stop processing and free SR Link
946 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
951 /* Update Segment Prefix of given Segment Routing Node */
952 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
955 struct listnode
*node
;
956 struct sr_prefix
*pref
;
960 if (srn
== NULL
|| srp
== NULL
)
963 if (IS_DEBUG_OSPF_SR
)
964 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
966 /* Process only Global Prefix SID */
967 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
970 /* Search for existing Segment Prefix */
971 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
972 if (pref
->instance
== srp
->instance
) {
977 if (IS_DEBUG_OSPF_SR
)
978 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
979 found
? "Update" : "Add",
980 GET_OPAQUE_ID(srp
->instance
),
981 inet_ntoa(srn
->adv_router
));
983 /* if not found, add new Segment Prefix and install NHLFE */
985 /* Complete SR-Prefix and add it to SR-Node list */
987 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
988 listnode_add(srn
->ext_prefix
, srp
);
989 /* Try to set MPLS table */
990 if (compute_prefix_nhlfe(srp
) == 1)
991 add_sid_nhlfe(srp
->nhlfe
);
993 if (sr_prefix_cmp(pref
, srp
)) {
994 if (compute_prefix_nhlfe(srp
) == 1) {
995 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
996 /* Replace Segment Prefix */
997 listnode_delete(srn
->ext_prefix
, pref
);
998 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1000 IPV4_ADDR_COPY(&srp
->adv_router
,
1002 listnode_add(srn
->ext_prefix
, srp
);
1004 /* New NHLFE was not found.
1005 * Just free the SR Prefix
1007 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1010 /* This is just an LSA refresh.
1011 * Stop processing and free SR Prefix
1013 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1019 * When change the FRR Self SRGB, update the NHLFE Input Label
1020 * for all Extended Prefix with SID index through hash_iterate()
1022 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1024 struct listnode
*node
;
1025 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1026 struct sr_prefix
*srp
;
1027 struct sr_nhlfe
new;
1029 /* Process Every Extended Prefix for this SR-Node */
1030 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1031 /* Process Self SRN only if NO-PHP is requested */
1032 if ((srn
== OspfSR
.self
)
1033 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1036 /* Process only SID Index */
1037 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1040 /* OK. Compute new NHLFE */
1041 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1042 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1043 /* Update MPLS LFIB */
1044 update_sid_nhlfe(srp
->nhlfe
, new);
1045 /* Finally update Input Label */
1046 srp
->nhlfe
.label_in
= new.label_in
;
1051 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1052 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1054 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1056 struct listnode
*node
;
1057 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1058 struct sr_node
*srnext
= (struct sr_node
*)args
;
1059 struct sr_prefix
*srp
;
1060 struct sr_nhlfe
new;
1062 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1063 /* Process only SID Index for next hop without PHP */
1064 if ((srp
->nexthop
== NULL
)
1065 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1067 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1068 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1069 update_sid_nhlfe(srp
->nhlfe
, new);
1070 srp
->nhlfe
.label_out
= new.label_out
;
1075 * Following functions are call when new Segment Routing LSA are received
1076 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1077 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1078 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1081 /* Update Segment Routing from Router Information LSA */
1082 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1084 struct sr_node
*srn
;
1085 struct tlv_header
*tlvh
;
1086 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1087 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1088 struct ri_sr_tlv_sr_algorithm
*algo
;
1089 struct sr_srgb srgb
;
1090 uint16_t length
= 0, sum
= 0;
1092 if (IS_DEBUG_OSPF_SR
)
1094 "SR (%s): Process Router "
1095 "Information LSA 4.0.0.%u from %s",
1097 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1098 inet_ntoa(lsah
->adv_router
));
1101 if (IS_LSA_SELF(lsa
))
1104 if (OspfSR
.neighbors
== NULL
) {
1105 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1109 /* Get SR Node in hash table from Router ID */
1110 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1111 (void *)sr_node_new
);
1116 "SR (%s): Abort! can't create SR node in hash table",
1121 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1123 "SR (%s): Abort! Wrong "
1124 "LSA ID 4.0.0.%u for SR node %s/%u",
1126 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1127 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1131 /* Collect Router Information Sub TLVs */
1132 /* Initialize TLV browsing */
1133 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1134 srgb
.range_size
= 0;
1135 srgb
.lower_bound
= 0;
1137 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1138 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1139 switch (ntohs(tlvh
->type
)) {
1140 case RI_SR_TLV_SR_ALGORITHM
:
1141 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1144 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1145 srn
->algo
[i
] = algo
->value
[0];
1146 for (; i
< ALGORITHM_COUNT
; i
++)
1147 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1148 sum
+= TLV_SIZE(tlvh
);
1150 case RI_SR_TLV_SID_LABEL_RANGE
:
1151 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1152 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1154 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1155 sum
+= TLV_SIZE(tlvh
);
1157 case RI_SR_TLV_NODE_MSD
:
1158 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1159 sum
+= TLV_SIZE(tlvh
);
1162 sum
+= TLV_SIZE(tlvh
);
1167 /* Check that we collect mandatory parameters */
1168 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1169 || srgb
.lower_bound
== 0) {
1171 "SR (%s): Missing mandatory parameters. Abort!",
1173 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1174 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1178 /* Check if it is a new SR Node or not */
1179 if (srn
->instance
== 0) {
1181 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1183 srn
->srgb
.range_size
= srgb
.range_size
;
1184 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1187 /* Check if SRGB has changed */
1188 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1189 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1190 srn
->srgb
.range_size
= srgb
.range_size
;
1191 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1192 /* Update NHLFE if it is a neighbor SR node */
1193 if (srn
->neighbor
== OspfSR
.self
)
1194 hash_iterate(OspfSR
.neighbors
,
1195 (void (*)(struct hash_backet
*,
1196 void *))update_out_nhlfe
,
1203 * Delete SR Node entry in hash table information corresponding to an expired
1204 * Router Information LSA
1206 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1208 struct sr_node
*srn
;
1209 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1211 if (IS_DEBUG_OSPF_SR
)
1213 "SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1214 __func__
, inet_ntoa(lsah
->adv_router
),
1215 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1218 if (OspfSR
.neighbors
== NULL
) {
1219 zlog_err("SR (%s): Abort! no valid SR Data Base", __func__
);
1223 /* Release Router ID entry in SRDB hash table */
1224 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1229 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1230 __func__
, inet_ntoa(lsah
->adv_router
));
1234 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1236 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1237 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1238 inet_ntoa(lsah
->adv_router
));
1242 /* Remove SR node */
1247 /* Update Segment Routing from Extended Link LSA */
1248 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1250 struct sr_node
*srn
;
1251 struct tlv_header
*tlvh
;
1252 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1253 struct sr_link
*srl
;
1255 uint16_t length
, sum
;
1257 if (IS_DEBUG_OSPF_SR
)
1259 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1260 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1261 inet_ntoa(lsah
->adv_router
));
1264 if (OspfSR
.neighbors
== NULL
) {
1265 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1269 /* Get SR Node in hash table from Router ID */
1270 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1271 (void *)&(lsah
->adv_router
),
1272 (void *)sr_node_new
);
1277 "SR (%s): Abort! can't create SR node in hash table",
1282 /* Initialize TLV browsing */
1283 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1285 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1286 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1287 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1288 /* Got Extended Link information */
1289 srl
= get_ext_link_sid(tlvh
);
1290 /* Update SID if not null */
1292 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1293 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1296 sum
+= TLV_SIZE(tlvh
);
1300 /* Delete Segment Routing from Extended Link LSA */
1301 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1303 struct listnode
*node
;
1304 struct sr_link
*srl
;
1305 struct sr_node
*srn
;
1306 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1307 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1309 if (IS_DEBUG_OSPF_SR
)
1311 "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
1331 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1332 __func__
, inet_ntoa(lsah
->adv_router
));
1336 /* Search for corresponding Segment Link */
1337 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1338 if (srl
->instance
== instance
)
1341 /* Remove Segment Link if found */
1342 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1343 del_sid_nhlfe(srl
->nhlfe
[0]);
1344 del_sid_nhlfe(srl
->nhlfe
[1]);
1345 listnode_delete(srn
->ext_link
, srl
);
1346 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1349 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1350 "for SR Node %s", __func__
,
1351 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1352 inet_ntoa(lsah
->adv_router
));
1357 /* Update Segment Routing from Extended Prefix LSA */
1358 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1360 struct sr_node
*srn
;
1361 struct tlv_header
*tlvh
;
1362 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1363 struct sr_prefix
*srp
;
1365 uint16_t length
, sum
;
1367 if (IS_DEBUG_OSPF_SR
)
1369 "SR (%s): Process Extended Prefix LSA "
1370 "7.0.0.%u from %s", __func__
,
1371 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1372 inet_ntoa(lsah
->adv_router
));
1375 if (OspfSR
.neighbors
== NULL
) {
1376 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1380 /* Get SR Node in hash table from Router ID */
1381 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1382 (void *)&(lsah
->adv_router
),
1383 (void *)sr_node_new
);
1388 "SR (%s): Abort! can't create SR node in hash table",
1393 /* Initialize TLV browsing */
1394 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1396 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1397 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1398 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1399 /* Got Extended Link information */
1400 srp
= get_ext_prefix_sid(tlvh
);
1401 /* Update SID if not null */
1403 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1404 update_ext_prefix_sid(srn
, srp
);
1407 sum
+= TLV_SIZE(tlvh
);
1411 /* Delete Segment Routing from Extended Prefix LSA */
1412 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1414 struct listnode
*node
;
1415 struct sr_prefix
*srp
;
1416 struct sr_node
*srn
;
1417 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1418 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1420 if (IS_DEBUG_OSPF_SR
)
1422 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1423 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1424 inet_ntoa(lsah
->adv_router
));
1427 if (OspfSR
.neighbors
== NULL
) {
1428 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1432 /* Search SR Node in hash table from Router ID */
1433 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1434 (void *)&(lsah
->adv_router
));
1437 * SR-Node may be NULL if it has been remove previously when
1438 * processing Router Information LSA deletion
1442 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1443 __func__
, inet_ntoa(lsah
->adv_router
));
1447 /* Search for corresponding Segment Link */
1448 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1449 if (srp
->instance
== instance
)
1452 /* Remove Segment Link if found */
1453 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1454 del_sid_nhlfe(srp
->nhlfe
);
1455 listnode_delete(srn
->ext_link
, srp
);
1456 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1459 "SR (%s): Didn't found corresponding SR Prefix "
1460 "7.0.0.%u for SR Node %s", __func__
,
1461 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1462 inet_ntoa(lsah
->adv_router
));
1467 /* Get Label for Extended Link SID */
1468 /* TODO: To be replace by Zebra Label Manager */
1469 uint32_t get_ext_link_label_value(void)
1471 static uint32_t label
= ADJ_SID_MIN
- 1;
1473 if (label
< ADJ_SID_MAX
)
1480 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1481 * complete initial CLI command at startutp.
1483 * @param ifp - Loopback interface
1484 * @param pref - Prefix address of this interface
1488 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1490 struct listnode
*node
;
1491 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 rc
= ospf_ext_schedule_prefix_index(ifp
, srp
->sid
,
1518 &srp
->nhlfe
.prefv4
, srp
->flags
);
1519 srp
->instance
= SET_OPAQUE_LSID(
1520 OPAQUE_TYPE_EXTENDED_PREFIX_LSA
, rc
);
1522 /* Install NHLFE if NO-PHP is requested */
1523 if (CHECK_FLAG(srp
->flags
,
1524 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1525 srp
->nhlfe
.label_in
= index2label(srp
->sid
,
1527 srp
->nhlfe
.label_out
= MPLS_IMP_NULL_LABEL
;
1528 add_sid_nhlfe(srp
->nhlfe
);
1535 * Following functions are used to update MPLS LFIB after a SPF run
1538 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1541 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1542 struct listnode
*node
;
1543 struct sr_prefix
*srp
;
1544 struct sr_nhlfe old
;
1551 if (IS_DEBUG_OSPF_SR
)
1552 zlog_debug(" |- Update Prefix for SR Node %s",
1553 inet_ntoa(srn
->adv_router
));
1555 /* Skip Self SR Node */
1556 if (srn
== OspfSR
.self
)
1559 /* Update Extended Prefix */
1560 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1562 /* Backup current NHLFE */
1563 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1565 /* Compute the new NHLFE */
1566 rc
= compute_prefix_nhlfe(srp
);
1568 /* Check computation result */
1570 /* next hop is not know, remove old NHLFE to avoid loop */
1572 del_sid_nhlfe(srp
->nhlfe
);
1574 /* next hop has not changed, skip it */
1577 /* there is a new next hop, update NHLFE */
1579 update_sid_nhlfe(old
, srp
->nhlfe
);
1587 static int ospf_sr_update_schedule(struct thread
*t
)
1591 struct timeval start_time
, stop_time
;
1593 ospf
= THREAD_ARG(t
);
1594 ospf
->t_sr_update
= NULL
;
1599 monotime(&start_time
);
1601 if (IS_DEBUG_OSPF_SR
)
1602 zlog_debug("SR (%s): Start SPF update", __func__
);
1604 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1605 void *))ospf_sr_nhlfe_update
,
1608 monotime(&stop_time
);
1611 "SR (%s): SPF Processing Time(usecs): %lld\n",
1613 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1614 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1616 OspfSR
.update
= false;
1620 #define OSPF_SR_UPDATE_INTERVAL 1
1622 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1628 /* Check if an update is not alreday engage */
1632 OspfSR
.update
= true;
1634 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1635 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1639 * --------------------------------------
1640 * Followings are vty command functions.
1641 * --------------------------------------
1645 * Segment Routing Router configuration
1647 * Must be centralize as it concerns both Extended Link/Prefix LSA
1648 * and Router Information LSA. Choose to call it from Extended Prefix
1649 * write_config() call back.
1651 * @param vty VTY output
1655 void ospf_sr_config_write_router(struct vty
*vty
)
1657 struct listnode
*node
;
1658 struct sr_prefix
*srp
;
1660 if (OspfSR
.enabled
) {
1661 vty_out(vty
, " segment-routing on\n");
1663 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1664 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1665 vty_out(vty
, " segment-routing global-block %u %u\n",
1666 OspfSR
.srgb
.lower_bound
,
1667 OspfSR
.srgb
.lower_bound
+
1668 OspfSR
.srgb
.range_size
- 1);
1670 if (OspfSR
.msd
!= 0)
1671 vty_out(vty
, " segment-routing node-msd %u\n",
1674 if (OspfSR
.self
!= NULL
) {
1675 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1678 " segment-routing prefix %s/%u "
1680 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1681 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1682 CHECK_FLAG(srp
->flags
,
1683 EXT_SUBTLV_PREFIX_SID_NPFLG
) ?
1684 " no-php-flag" : "");
1690 DEFUN(ospf_sr_enable
,
1692 "segment-routing on",
1694 "Enable Segment Routing\n")
1697 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1702 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1703 vty_out(vty
, "Segment Routing is only supported in default "
1705 return CMD_WARNING_CONFIG_FAILED
;
1708 if (IS_DEBUG_OSPF_EVENT
)
1709 zlog_debug("SR: Segment Routing: OFF -> ON");
1711 /* Start Segment Routing */
1712 OspfSR
.enabled
= true;
1713 if (!ospf_sr_start(ospf
)) {
1714 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1718 /* Set Router Information SR parameters */
1719 if (IS_DEBUG_OSPF_EVENT
)
1720 zlog_debug("SR: Activate SR for Router Information LSA");
1722 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1724 /* Update Ext LSA */
1725 if (IS_DEBUG_OSPF_EVENT
)
1726 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1728 ospf_ext_update_sr(true);
1733 DEFUN (no_ospf_sr_enable
,
1734 no_ospf_sr_enable_cmd
,
1735 "no segment-routing [on]",
1738 "Disable Segment Routing\n")
1741 if (!OspfSR
.enabled
)
1744 if (IS_DEBUG_OSPF_EVENT
)
1745 zlog_debug("SR: Segment Routing: ON -> OFF");
1747 /* Start by Disabling Extended Link & Prefix LSA */
1748 ospf_ext_update_sr(false);
1750 /* then, disable Router Information SR parameters */
1751 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1753 /* Finally, stop Segment Routing */
1755 OspfSR
.enabled
= false;
1760 static int ospf_sr_enabled(struct vty
*vty
)
1766 vty_out(vty
, "%% OSPF SR is not turned on\n");
1771 DEFUN (sr_sid_label_range
,
1772 sr_sid_label_range_cmd
,
1773 "segment-routing global-block (0-1048575) (0-1048575)",
1775 "Segment Routing Global Block label range\n"
1776 "Lower-bound range in decimal (0-1048575)\n"
1777 "Upper-bound range in decimal (0-1048575)\n")
1785 if (!ospf_sr_enabled(vty
))
1786 return CMD_WARNING_CONFIG_FAILED
;
1788 /* Get lower and upper bound */
1789 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1790 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1791 size
= upper
- lower
+ 1;
1793 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1795 "Range size cannot be less than 0 or more than %u\n",
1796 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1797 return CMD_WARNING_CONFIG_FAILED
;
1800 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1801 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1802 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1803 return CMD_WARNING_CONFIG_FAILED
;
1806 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1807 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1808 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1809 return CMD_WARNING_CONFIG_FAILED
;
1812 /* Check if values have changed */
1813 if ((OspfSR
.srgb
.range_size
== size
)
1814 && (OspfSR
.srgb
.lower_bound
== lower
))
1817 /* Set SID/Label range SRGB */
1818 OspfSR
.srgb
.range_size
= size
;
1819 OspfSR
.srgb
.lower_bound
= lower
;
1820 if (OspfSR
.self
!= NULL
) {
1821 OspfSR
.self
->srgb
.range_size
= size
;
1822 OspfSR
.self
->srgb
.lower_bound
= lower
;
1825 /* Set Router Information SR parameters */
1826 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1828 /* Update NHLFE entries */
1829 hash_iterate(OspfSR
.neighbors
,
1830 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1836 DEFUN (no_sr_sid_label_range
,
1837 no_sr_sid_label_range_cmd
,
1838 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1841 "Segment Routing Global Block label range\n"
1842 "Lower-bound range in decimal (0-1048575)\n"
1843 "Upper-bound range in decimal (0-1048575)\n")
1846 if (!ospf_sr_enabled(vty
))
1847 return CMD_WARNING_CONFIG_FAILED
;
1849 /* Revert to default SRGB value */
1850 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1851 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1852 if (OspfSR
.self
!= NULL
) {
1853 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1854 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1857 /* Set Router Information SR parameters */
1858 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1860 /* Update NHLFE entries */
1861 hash_iterate(OspfSR
.neighbors
,
1862 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1870 "segment-routing node-msd (1-16)",
1872 "Maximum Stack Depth for this router\n"
1873 "Maximum number of label that could be stack (1-16)\n")
1878 if (!ospf_sr_enabled(vty
))
1879 return CMD_WARNING_CONFIG_FAILED
;
1882 argv_find(argv
, argc
, "(1-16)", &idx
);
1883 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1884 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1885 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1887 return CMD_WARNING_CONFIG_FAILED
;
1890 /* Check if value has changed */
1891 if (OspfSR
.msd
== msd
)
1894 /* Set this router MSD */
1896 if (OspfSR
.self
!= NULL
)
1897 OspfSR
.self
->msd
= msd
;
1899 /* Set Router Information SR parameters */
1900 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1905 DEFUN (no_sr_node_msd
,
1907 "no segment-routing node-msd [(1-16)]",
1910 "Maximum Stack Depth for this router\n"
1911 "Maximum number of label that could be stack (1-16)\n")
1914 if (!ospf_sr_enabled(vty
))
1915 return CMD_WARNING_CONFIG_FAILED
;
1917 /* unset this router MSD */
1919 if (OspfSR
.self
!= NULL
)
1920 OspfSR
.self
->msd
= 0;
1922 /* Set Router Information SR parameters */
1923 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1928 DEFUN (sr_prefix_sid
,
1930 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1933 "IPv4 Prefix as A.B.C.D/M\n"
1934 "SID index for this prefix in decimal (0-65535)\n"
1935 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1936 "Don't request Penultimate Hop Popping (PHP)\n")
1941 struct listnode
*node
;
1942 struct sr_prefix
*srp
;
1943 struct interface
*ifp
;
1946 if (!ospf_sr_enabled(vty
))
1947 return CMD_WARNING_CONFIG_FAILED
;
1949 /* Get network prefix */
1950 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1951 rc
= str2prefix(argv
[idx
]->arg
, &p
);
1953 vty_out(vty
, "Invalid prefix format %s\n",
1955 return CMD_WARNING_CONFIG_FAILED
;
1958 /* Get & verify index value */
1959 argv_find(argv
, argc
, "(0-65535)", &idx
);
1960 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1961 if (index
> OspfSR
.srgb
.range_size
- 1) {
1962 vty_out(vty
, "Index %u must be lower than range size %u\n",
1963 index
, OspfSR
.srgb
.range_size
);
1964 return CMD_WARNING_CONFIG_FAILED
;
1967 /* check that the index is not already used */
1968 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1969 if (srp
->sid
== index
) {
1970 vty_out(vty
, "Index %u is already used\n", index
);
1971 return CMD_WARNING_CONFIG_FAILED
;
1975 /* Add new Extended Prefix to SRDB */
1976 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1977 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1978 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
.u
.prefix4
);
1979 srp
->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1980 srp
->nhlfe
.prefv4
.family
= p
.family
;
1982 /* Set NO PHP flag if present */
1983 if (argv_find(argv
, argc
, "no-php-flag", &idx
))
1984 SET_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1985 listnode_add(OspfSR
.self
->ext_prefix
, srp
);
1987 if (IS_DEBUG_OSPF_SR
)
1989 "SR (%s): Add new Prefix %s/%u with index %u",
1990 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1991 srp
->nhlfe
.prefv4
.prefixlen
, index
);
1993 /* Get Interface and check if it is a Loopback */
1994 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1997 * Interface could be not yet available i.e. when this
1998 * command is in the configuration file, OSPF is not yet
1999 * ready. In this case, store the prefix SID for latter
2000 * update of this Extended Prefix
2003 "Interface for prefix %s/%u not found. Deferred LSA "
2004 "flooding", inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2008 if (!if_is_loopback(ifp
)) {
2009 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2010 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2011 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2012 return CMD_WARNING_CONFIG_FAILED
;
2014 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
2016 /* Install NHLFE if NO-PHP is requested */
2017 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
2018 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.self
->srgb
);
2019 srp
->nhlfe
.label_out
= MPLS_IMP_NULL_LABEL
;
2020 add_sid_nhlfe(srp
->nhlfe
);
2023 /* Finally, update Extended Prefix LSA */
2024 rc
= ospf_ext_schedule_prefix_index(ifp
, srp
->sid
,
2025 &srp
->nhlfe
.prefv4
, srp
->flags
);
2027 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2028 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2031 srp
->instance
= SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA
, rc
);
2036 DEFUN (no_sr_prefix_sid
,
2037 no_sr_prefix_sid_cmd
,
2038 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2042 "IPv4 Prefix as A.B.C.D/M\n"
2043 "SID index for this prefix in decimal (0-65535)\n"
2044 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2045 "Don't request Penultimate Hop Popping (PHP)\n")
2049 struct listnode
*node
;
2050 struct sr_prefix
*srp
;
2051 struct interface
*ifp
;
2055 /* Get network prefix */
2056 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2057 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2059 vty_out(vty
, "Invalid prefix format %s\n",
2061 return CMD_WARNING_CONFIG_FAILED
;
2064 /* check that the prefix is already set */
2065 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2066 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2067 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2073 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2075 return CMD_WARNING_CONFIG_FAILED
;
2079 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2081 vty_out(vty
, "interface for prefix %s not found.\n",
2083 return CMD_WARNING_CONFIG_FAILED
;
2086 /* Update Extended Prefix LSA */
2087 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2088 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2092 if (IS_DEBUG_OSPF_SR
)
2094 "SR (%s): Remove Prefix %s/%u with index %u",
2095 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2096 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2098 /* Delete NHLFE is NO-PHP is set */
2099 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2100 del_sid_nhlfe(srp
->nhlfe
);
2102 /* OK, all is clean, remove SRP from SRDB */
2103 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2104 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2111 static void show_vty_sr_node(struct vty
*vty
, struct sr_node
*srn
)
2114 struct listnode
*node
;
2115 struct sr_link
*srl
;
2116 struct sr_prefix
*srp
;
2117 struct interface
*itf
;
2126 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2127 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2128 srn
->srgb
.lower_bound
);
2129 vty_out(vty
, "\tAlgorithm(s): %s",
2130 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2131 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2132 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2135 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2138 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2141 "\n\n Prefix or Link Label In Label Out "
2142 "Node or Adj. SID Interface Nexthop\n");
2144 "------------------ -------- --------- "
2145 "--------------------- --------- ---------------\n");
2146 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2147 strncpy(pref
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
), 16);
2148 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2149 if (srp
->nhlfe
.label_out
== MPLS_IMP_NULL_LABEL
)
2150 sprintf(label
, "pop");
2152 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2153 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2154 vty_out(vty
, "%15s/%u %8u %9s %21s %9s %15s\n", pref
,
2155 srp
->nhlfe
.prefv4
.prefixlen
, srp
->nhlfe
.label_in
, label
,
2156 sid
, itf
? itf
->name
: "-",
2157 inet_ntoa(srp
->nhlfe
.nexthop
));
2160 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2161 strncpy(pref
, inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
), 16);
2162 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2163 if (srl
->nhlfe
[0].label_out
== MPLS_IMP_NULL_LABEL
)
2164 sprintf(label
, "pop");
2166 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2167 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2168 vty_out(vty
, "%15s/%u %8u %9s %21s %9s %15s\n", pref
,
2169 srl
->nhlfe
[0].prefv4
.prefixlen
, srl
->nhlfe
[0].label_in
,
2170 label
, sid
, itf
? itf
->name
: "-",
2171 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2172 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2173 if (srl
->nhlfe
[1].label_out
== MPLS_IMP_NULL_LABEL
)
2174 sprintf(label
, "pop");
2176 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2177 vty_out(vty
, "%15s/%u %8u %9s %21s %9s %15s\n", pref
,
2178 srl
->nhlfe
[1].prefv4
.prefixlen
, srl
->nhlfe
[1].label_in
,
2179 label
, sid
, itf
? itf
->name
: "-",
2180 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2185 static void show_srdb_entry(struct hash_backet
*backet
, void *args
)
2187 struct vty
*vty
= (struct vty
*)args
;
2188 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2190 show_vty_sr_node(vty
, srn
);
2193 DEFUN (show_ip_opsf_srdb
,
2194 show_ip_ospf_srdb_cmd
,
2195 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate]",
2199 "Database summary\n"
2200 "Show Segment Routing Data Base\n"
2201 "Advertising SR node\n"
2202 "Advertising SR node ID (as an IP address)\n"
2203 "Self-originated SR node\n")
2207 struct sr_node
*srn
;
2209 if (!OspfSR
.enabled
) {
2210 vty_out(vty
, "Segment Routing is disabled on this router\n");
2214 vty_out(vty
, "\n OSPF Segment Routing database for ID %s\n\n",
2215 inet_ntoa(OspfSR
.self
->adv_router
));
2217 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2219 show_vty_sr_node(vty
, srn
);
2223 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2224 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2226 "Specified Router ID %s is invalid\n",
2228 return CMD_WARNING_CONFIG_FAILED
;
2230 /* Get the SR Node from the SRDB */
2231 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2233 show_vty_sr_node(vty
, srn
);
2237 /* No parameters have been provided, Iterate through all the SRDB */
2240 (void (*)(struct hash_backet
*, void *))show_srdb_entry
,
2245 /* Install new CLI commands */
2246 void ospf_sr_register_vty(void)
2248 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2250 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2251 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2252 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2253 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2254 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2255 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2256 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2257 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);