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(const 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 bool 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(&srn
->ext_link
);
170 /* Clean Prefix List */
171 list_delete(&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
);
240 /* Complete & Store self SR Node */
241 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
242 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
243 srn
->algo
[0] = OspfSR
.algo
[0];
244 srn
->msd
= OspfSR
.msd
;
247 if (IS_DEBUG_OSPF_EVENT
)
248 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
250 /* Start by looking to Router Info & Extended LSA in lsdb */
251 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
252 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
) {
253 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
256 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
258 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
259 ospf_sr_ri_lsa_update(lsa
);
261 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
262 ospf_sr_ext_prefix_lsa_update(lsa
);
264 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
265 ospf_sr_ext_link_lsa_update(lsa
);
277 /* Stop Segment Routing */
278 static void ospf_sr_stop(void)
281 if (IS_DEBUG_OSPF_SR
)
282 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
285 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
286 * be remove though list_delete() call. See sr_node_del()
288 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
292 * Segment Routing initialize function
296 * @return 0 if OK, -1 otherwise
298 int ospf_sr_init(void)
302 if (IS_DEBUG_OSPF_SR
)
303 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
305 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
306 OspfSR
.enabled
= false;
307 /* Only AREA flooding is supported in this release */
308 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
310 /* Initialize SRGB, Algorithms and MSD TLVs */
311 /* Only Algorithm SPF is supported */
312 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
313 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
314 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
316 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
317 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
320 /* Initialize Hash table for neighbor SR nodes */
321 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
322 if (OspfSR
.neighbors
== NULL
)
325 /* Initialize Route Table for prefix */
326 OspfSR
.prefix
= route_table_init();
327 if (OspfSR
.prefix
== NULL
)
330 /* Register Segment Routing VTY command */
331 ospf_sr_register_vty();
338 * Segment Routing termination function
343 void ospf_sr_term(void)
346 /* Stop Segment Routing */
349 /* Clear SR Node Table */
350 if (OspfSR
.neighbors
)
351 hash_free(OspfSR
.neighbors
);
353 /* Clear Prefix Table */
355 route_table_finish(OspfSR
.prefix
);
357 OspfSR
.enabled
= false;
362 * Segment Routing finish function
367 void ospf_sr_finish(void)
369 /* Stop Segment Routing */
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
;
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 /* Update neighbor address for LAN_ADJ_SID */
489 if (srl
->type
== LAN_ADJ_SID
) {
490 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
491 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
494 /* Set Input & Output Label */
495 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
496 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
498 srl
->nhlfe
[0].label_in
=
499 index2label(srl
->sid
[0], srl
->srn
->srgb
);
500 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
501 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
503 srl
->nhlfe
[1].label_in
=
504 index2label(srl
->sid
[1], srl
->srn
->srgb
);
506 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
507 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
514 * Compute NHLFE entry for Extended Prefix
516 * @param srp - Segment Routing Prefix
518 * @return -1 if next hop is not found, 0 if nexthop has not changed
521 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
523 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
524 struct ospf_path
*nh
= NULL
;
525 struct sr_node
*srnext
;
528 if (IS_DEBUG_OSPF_SR
)
529 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
530 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
531 srp
->nhlfe
.prefv4
.prefixlen
);
533 /* First determine the nexthop */
534 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
536 /* Nexthop could be not found when OSPF Adjacency just fire up
537 * because SPF don't yet populate routing table. This NHLFE will
538 * be fixed later when SR SPF schedule will be called.
543 /* Check if NextHop has changed when call after running a new SPF */
544 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
545 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
548 if (IS_DEBUG_OSPF_SR
)
549 zlog_debug(" |- Found new next hop for this NHLFE: %s",
550 inet_ntoa(nh
->nexthop
));
553 * Get SR-Node for this nexthop. Could be not yet available
554 * as Extende Link / Prefix and Router Information are flooded
555 * after LSA Type 1 & 2 which populate the OSPF Route Table
557 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
561 /* And store this information for later update if SR Node is found */
562 srnext
->neighbor
= OspfSR
.self
;
563 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
566 srp
->nexthop
= srnext
;
569 * SR Node could be known, but SRGB could be not initialize
570 * This is due to the fact that Extended Link / Prefix could
571 * be received before corresponding Router Information LSA
573 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
574 || (srnext
->srgb
.range_size
== 0))
577 if (IS_DEBUG_OSPF_SR
)
578 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
579 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
580 inet_ntoa(srnext
->adv_router
));
582 /* Set ip addr & ifindex for this neighbor */
583 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
584 srp
->nhlfe
.ifindex
= nh
->ifindex
;
586 /* Compute Input Label with self SRGB */
587 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
589 * and Output Label with Next hop SR Node SRGB or Implicit Null label
590 * if next hop is the destination and request PHP
592 if ((srp
->nexthop
== NULL
)
593 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
594 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
595 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
596 srp
->nhlfe
.label_out
= srp
->sid
;
598 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
600 if (IS_DEBUG_OSPF_SR
)
601 zlog_debug(" |- Computed new labels in: %u out: %u",
602 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
608 /* Send MPLS Label entry to Zebra for installation or deletion */
609 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
611 struct zapi_labels zl
= {};
613 if (IS_DEBUG_OSPF_SR
)
614 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
615 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
616 nhlfe
.label_in
, nhlfe
.label_out
,
617 inet_ntoa(nhlfe
.prefv4
.prefix
),
618 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
620 zl
.type
= ZEBRA_LSP_SR
;
621 zl
.prefix
.family
= nhlfe
.prefv4
.family
;
622 zl
.prefix
.prefixlen
= nhlfe
.prefv4
.prefixlen
;
623 zl
.prefix
.u
.prefix4
= nhlfe
.prefv4
.prefix
;
624 zl
.nexthop
.ipv4
= nhlfe
.nexthop
;
625 zl
.ifindex
= nhlfe
.ifindex
;
626 zl
.distance
= OSPF_SR_PRIORITY_DEFAULT
;
627 zl
.local_label
= nhlfe
.label_in
;
628 zl
.remote_label
= nhlfe
.label_out
;
630 return zebra_send_mpls_labels(zclient
, cmd
, &zl
);
633 /* Request zebra to install/remove FEC in FIB */
634 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
636 struct zapi_route api
;
637 struct zapi_nexthop
*api_nh
;
639 /* Support only IPv4 */
640 if (nhlfe
.prefv4
.family
!= AF_INET
)
643 memset(&api
, 0, sizeof(api
));
644 api
.vrf_id
= VRF_DEFAULT
;
645 api
.type
= ZEBRA_ROUTE_OSPF
;
646 api
.safi
= SAFI_UNICAST
;
647 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
649 if (cmd
== ZEBRA_ROUTE_ADD
) {
651 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
652 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
654 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
655 api_nh
= &api
.nexthops
[0];
656 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
657 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
658 api_nh
->ifindex
= nhlfe
.ifindex
;
660 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
661 api_nh
->labels
[0] = nhlfe
.label_out
;
662 api_nh
->label_num
= 1;
663 api_nh
->vrf_id
= VRF_DEFAULT
;
667 if (IS_DEBUG_OSPF_SR
)
668 zlog_debug(" |- %s FEC %u for %s/%u via %u",
669 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
670 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
671 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
673 return zclient_route_send(cmd
, zclient
, &api
);
676 /* Add new NHLFE entry for SID */
677 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
679 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
680 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
681 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
682 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
686 /* Remove NHLFE entry for SID */
687 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
689 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
690 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
691 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
692 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
696 /* Update NHLFE entry for SID */
697 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
705 * Functions to parse and get Extended Link / Prefix
709 /* Extended Link SubTLVs Getter */
710 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
714 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
715 struct ext_subtlv_adj_sid
*adj_sid
;
716 struct ext_subtlv_lan_adj_sid
*lan_sid
;
717 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
719 struct tlv_header
*sub_tlvh
;
720 uint16_t length
= 0, sum
= 0, i
= 0;
722 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
724 /* Initialize TLV browsing */
725 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
726 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
727 + EXT_TLV_LINK_SIZE
);
728 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
729 switch (ntohs(sub_tlvh
->type
)) {
730 case EXT_SUBTLV_ADJ_SID
:
731 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
733 i
= CHECK_FLAG(adj_sid
->flags
,
734 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
737 srl
->flags
[i
] = adj_sid
->flags
;
738 if (CHECK_FLAG(adj_sid
->flags
,
739 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
740 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
742 srl
->sid
[i
] = ntohl(adj_sid
->value
);
743 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
745 case EXT_SUBTLV_LAN_ADJ_SID
:
746 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
747 srl
->type
= LAN_ADJ_SID
;
748 i
= CHECK_FLAG(lan_sid
->flags
,
749 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
752 srl
->flags
[i
] = lan_sid
->flags
;
753 if (CHECK_FLAG(lan_sid
->flags
,
754 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
755 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
757 srl
->sid
[i
] = ntohl(lan_sid
->value
);
758 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
759 &lan_sid
->neighbor_id
);
761 case EXT_SUBTLV_RMT_ITF_ADDR
:
762 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
763 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
764 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
769 sum
+= TLV_SIZE(sub_tlvh
);
772 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
773 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
774 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
775 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
776 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
777 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
778 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
779 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
781 if (IS_DEBUG_OSPF_SR
) {
782 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
783 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
784 srl
->nhlfe
[0].prefv4
.prefixlen
);
785 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
786 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
787 srl
->nhlfe
[1].prefv4
.prefixlen
);
793 /* Extended Prefix SubTLVs Getter */
794 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
797 struct sr_prefix
*srp
;
798 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
799 struct ext_subtlv_prefix_sid
*psid
;
801 struct tlv_header
*sub_tlvh
;
802 uint16_t length
= 0, sum
= 0;
804 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
806 /* Initialize TLV browsing */
807 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
808 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
809 + EXT_TLV_PREFIX_SIZE
);
810 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
811 switch (ntohs(sub_tlvh
->type
)) {
812 case EXT_SUBTLV_PREFIX_SID
:
813 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
814 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
815 flog_err(EC_OSPF_INVALID_ALGORITHM
,
816 "SR (%s): Unsupported Algorithm",
818 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
821 srp
->type
= PREF_SID
;
822 srp
->flags
= psid
->flags
;
823 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
824 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
826 srp
->sid
= ntohl(psid
->value
);
827 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
829 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
830 srp
->nhlfe
.prefv4
.family
= AF_INET
;
831 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
836 sum
+= TLV_SIZE(sub_tlvh
);
839 if (IS_DEBUG_OSPF_SR
)
840 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
841 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
842 srp
->nhlfe
.prefv4
.prefixlen
);
847 * Functions to manipulate Segment Routing Link & Prefix structures
850 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
851 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
853 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
854 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
855 && (srl1
->flags
[1] == srl2
->flags
[1]))
861 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
862 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
864 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
870 /* Update Segment Link of given Segment Routing Node */
871 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
874 struct listnode
*node
;
879 if ((srn
== NULL
) || (srl
== NULL
))
882 if (IS_DEBUG_OSPF_SR
)
883 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
885 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
886 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
887 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
888 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
891 /* Search for existing Segment Link */
892 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
893 if (lk
->instance
== srl
->instance
) {
898 if (IS_DEBUG_OSPF_SR
)
899 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
900 found
? "Update" : "Add",
901 GET_OPAQUE_ID(srl
->instance
),
902 inet_ntoa(srn
->adv_router
));
904 /* if not found, add new Segment Link and install NHLFE */
906 /* Complete SR-Link and add it to SR-Node list */
908 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
909 listnode_add(srn
->ext_link
, srl
);
910 /* Try to set MPLS table */
911 if (compute_link_nhlfe(srl
)) {
912 add_sid_nhlfe(srl
->nhlfe
[0]);
913 add_sid_nhlfe(srl
->nhlfe
[1]);
916 if (sr_link_cmp(lk
, srl
)) {
917 if (compute_link_nhlfe(srl
)) {
918 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
919 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
920 /* Replace Segment List */
921 listnode_delete(srn
->ext_link
, lk
);
922 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
924 IPV4_ADDR_COPY(&srl
->adv_router
,
926 listnode_add(srn
->ext_link
, srl
);
928 /* New NHLFE was not found.
929 * Just free the SR Link
931 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
935 * This is just an LSA refresh.
936 * Stop processing and free SR Link
938 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
943 /* Update Segment Prefix of given Segment Routing Node */
944 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
947 struct listnode
*node
;
948 struct sr_prefix
*pref
;
952 if (srn
== NULL
|| srp
== NULL
)
955 if (IS_DEBUG_OSPF_SR
)
956 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
958 /* Process only Global Prefix SID */
959 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
962 /* Search for existing Segment Prefix */
963 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
964 if (pref
->instance
== srp
->instance
) {
969 if (IS_DEBUG_OSPF_SR
)
970 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
971 found
? "Update" : "Add",
972 GET_OPAQUE_ID(srp
->instance
),
973 inet_ntoa(srn
->adv_router
));
975 /* if not found, add new Segment Prefix and install NHLFE */
977 /* Complete SR-Prefix and add it to SR-Node list */
979 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
980 listnode_add(srn
->ext_prefix
, srp
);
981 /* Try to set MPLS table */
982 if (compute_prefix_nhlfe(srp
) == 1)
983 add_sid_nhlfe(srp
->nhlfe
);
985 if (sr_prefix_cmp(pref
, srp
)) {
986 if (compute_prefix_nhlfe(srp
) == 1) {
987 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
988 /* Replace Segment Prefix */
989 listnode_delete(srn
->ext_prefix
, pref
);
990 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
992 IPV4_ADDR_COPY(&srp
->adv_router
,
994 listnode_add(srn
->ext_prefix
, srp
);
996 /* New NHLFE was not found.
997 * Just free the SR Prefix
999 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1002 /* This is just an LSA refresh.
1003 * Stop processing and free SR Prefix
1005 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1011 * When change the FRR Self SRGB, update the NHLFE Input Label
1012 * for all Extended Prefix with SID index through hash_iterate()
1014 static void update_in_nhlfe(struct hash_bucket
*bucket
, void *args
)
1016 struct listnode
*node
;
1017 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1018 struct sr_prefix
*srp
;
1019 struct sr_nhlfe
new;
1021 /* Process Every Extended Prefix for this SR-Node */
1022 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1023 /* Process Self SRN only if NO-PHP is requested */
1024 if ((srn
== OspfSR
.self
)
1025 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1028 /* Process only SID Index */
1029 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1032 /* OK. Compute new NHLFE */
1033 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1034 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1035 /* Update MPLS LFIB */
1036 update_sid_nhlfe(srp
->nhlfe
, new);
1037 /* Finally update Input Label */
1038 srp
->nhlfe
.label_in
= new.label_in
;
1043 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1044 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1046 static void update_out_nhlfe(struct hash_bucket
*bucket
, void *args
)
1048 struct listnode
*node
;
1049 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1050 struct sr_node
*srnext
= (struct sr_node
*)args
;
1051 struct sr_prefix
*srp
;
1052 struct sr_nhlfe
new;
1054 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1055 /* Process only SID Index for next hop without PHP */
1056 if ((srp
->nexthop
== NULL
)
1057 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1059 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1060 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1061 update_sid_nhlfe(srp
->nhlfe
, new);
1062 srp
->nhlfe
.label_out
= new.label_out
;
1067 * Following functions are call when new Segment Routing LSA are received
1068 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1069 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1070 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1073 /* Update Segment Routing from Router Information LSA */
1074 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1076 struct sr_node
*srn
;
1077 struct tlv_header
*tlvh
;
1078 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1079 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1080 struct ri_sr_tlv_sr_algorithm
*algo
;
1081 struct sr_srgb srgb
;
1082 uint16_t length
= 0, sum
= 0;
1084 if (IS_DEBUG_OSPF_SR
)
1086 "SR (%s): Process Router "
1087 "Information LSA 4.0.0.%u from %s",
1088 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1089 inet_ntoa(lsah
->adv_router
));
1092 if (IS_LSA_SELF(lsa
))
1095 if (OspfSR
.neighbors
== NULL
) {
1096 flog_err(EC_OSPF_SR_INVALID_DB
,
1097 "SR (%s): Abort! no valid SR DataBase", __func__
);
1101 /* Get SR Node in hash table from Router ID */
1102 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1103 (void *)sr_node_new
);
1107 flog_err(EC_OSPF_SR_NODE_CREATE
,
1108 "SR (%s): Abort! can't create SR node in hash table",
1113 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1114 flog_err(EC_OSPF_SR_INVALID_LSA_ID
,
1115 "SR (%s): Abort! Wrong "
1116 "LSA ID 4.0.0.%u for SR node %s/%u",
1117 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1118 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1122 /* Collect Router Information Sub TLVs */
1123 /* Initialize TLV browsing */
1124 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1125 srgb
.range_size
= 0;
1126 srgb
.lower_bound
= 0;
1128 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1129 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1130 switch (ntohs(tlvh
->type
)) {
1131 case RI_SR_TLV_SR_ALGORITHM
:
1132 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1135 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1136 srn
->algo
[i
] = algo
->value
[0];
1137 for (; i
< ALGORITHM_COUNT
; i
++)
1138 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1139 sum
+= TLV_SIZE(tlvh
);
1141 case RI_SR_TLV_SID_LABEL_RANGE
:
1142 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1143 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1145 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1146 sum
+= TLV_SIZE(tlvh
);
1148 case RI_SR_TLV_NODE_MSD
:
1149 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1150 sum
+= TLV_SIZE(tlvh
);
1153 sum
+= TLV_SIZE(tlvh
);
1158 /* Check that we collect mandatory parameters */
1159 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1160 || srgb
.lower_bound
== 0) {
1161 flog_err(EC_OSPF_SR_NODE_CREATE
,
1162 "SR (%s): Missing mandatory parameters. Abort!",
1164 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1165 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1169 /* Check if it is a new SR Node or not */
1170 if (srn
->instance
== 0) {
1172 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1174 srn
->srgb
.range_size
= srgb
.range_size
;
1175 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1178 /* Check if SRGB has changed */
1179 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1180 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1181 srn
->srgb
.range_size
= srgb
.range_size
;
1182 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1183 /* Update NHLFE if it is a neighbor SR node */
1184 if (srn
->neighbor
== OspfSR
.self
)
1185 hash_iterate(OspfSR
.neighbors
,
1186 (void (*)(struct hash_bucket
*,
1187 void *))update_out_nhlfe
,
1193 * Delete SR Node entry in hash table information corresponding to an expired
1194 * Router Information LSA
1196 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1198 struct sr_node
*srn
;
1199 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1201 if (IS_DEBUG_OSPF_SR
)
1202 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1203 __func__
, inet_ntoa(lsah
->adv_router
),
1204 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1207 if (OspfSR
.neighbors
== NULL
) {
1208 flog_err(EC_OSPF_SR_INVALID_DB
,
1209 "SR (%s): Abort! no valid SR Data Base", __func__
);
1213 /* Release Router ID entry in SRDB hash table */
1214 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1218 flog_err(EC_OSPF_SR_NODE_CREATE
,
1219 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1220 __func__
, inet_ntoa(lsah
->adv_router
));
1224 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1225 flog_err(EC_OSPF_SR_INVALID_LSA_ID
,
1226 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1227 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1228 inet_ntoa(lsah
->adv_router
));
1232 /* Remove SR node */
1236 /* Update Segment Routing from Extended Link LSA */
1237 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1239 struct sr_node
*srn
;
1240 struct tlv_header
*tlvh
;
1241 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1242 struct sr_link
*srl
;
1244 uint16_t length
, sum
;
1246 if (IS_DEBUG_OSPF_SR
)
1248 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1249 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1250 inet_ntoa(lsah
->adv_router
));
1253 if (OspfSR
.neighbors
== NULL
) {
1254 flog_err(EC_OSPF_SR_INVALID_DB
,
1255 "SR (%s): Abort! no valid SR DataBase", __func__
);
1259 /* Get SR Node in hash table from Router ID */
1260 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1261 (void *)&(lsah
->adv_router
),
1262 (void *)sr_node_new
);
1266 flog_err(EC_OSPF_SR_NODE_CREATE
,
1267 "SR (%s): Abort! can't create SR node in hash table",
1272 /* Initialize TLV browsing */
1273 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1275 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1276 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1277 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1278 /* Got Extended Link information */
1279 srl
= get_ext_link_sid(tlvh
);
1280 /* Update SID if not null */
1282 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1283 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1286 sum
+= TLV_SIZE(tlvh
);
1290 /* Delete Segment Routing from Extended Link LSA */
1291 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1293 struct listnode
*node
;
1294 struct sr_link
*srl
;
1295 struct sr_node
*srn
;
1296 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1297 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1299 if (IS_DEBUG_OSPF_SR
)
1300 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1301 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1302 inet_ntoa(lsah
->adv_router
));
1305 if (OspfSR
.neighbors
== NULL
) {
1306 flog_err(EC_OSPF_SR_INVALID_DB
,
1307 "SR (%s): Abort! no valid SR DataBase", __func__
);
1311 /* Search SR Node in hash table from Router ID */
1312 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1313 (void *)&(lsah
->adv_router
));
1316 * SR-Node may be NULL if it has been remove previously when
1317 * processing Router Information LSA deletion
1320 flog_err(EC_OSPF_SR_INVALID_DB
,
1321 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1322 __func__
, inet_ntoa(lsah
->adv_router
));
1326 /* Search for corresponding Segment Link */
1327 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1328 if (srl
->instance
== instance
)
1331 /* Remove Segment Link if found */
1332 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1333 del_sid_nhlfe(srl
->nhlfe
[0]);
1334 del_sid_nhlfe(srl
->nhlfe
[1]);
1335 listnode_delete(srn
->ext_link
, srl
);
1336 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1338 flog_err(EC_OSPF_SR_INVALID_DB
,
1339 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1341 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1342 inet_ntoa(lsah
->adv_router
));
1346 /* Update Segment Routing from Extended Prefix LSA */
1347 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1349 struct sr_node
*srn
;
1350 struct tlv_header
*tlvh
;
1351 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1352 struct sr_prefix
*srp
;
1354 uint16_t length
, sum
;
1356 if (IS_DEBUG_OSPF_SR
)
1358 "SR (%s): Process Extended Prefix LSA "
1360 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1361 inet_ntoa(lsah
->adv_router
));
1364 if (OspfSR
.neighbors
== NULL
) {
1365 flog_err(EC_OSPF_SR_INVALID_DB
,
1366 "SR (%s): Abort! no valid SR DataBase", __func__
);
1370 /* Get SR Node in hash table from Router ID */
1371 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1372 (void *)&(lsah
->adv_router
),
1373 (void *)sr_node_new
);
1377 flog_err(EC_OSPF_SR_NODE_CREATE
,
1378 "SR (%s): Abort! can't create SR node in hash table",
1383 /* Initialize TLV browsing */
1384 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1386 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1387 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1388 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1389 /* Got Extended Link information */
1390 srp
= get_ext_prefix_sid(tlvh
);
1391 /* Update SID if not null */
1393 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1394 update_ext_prefix_sid(srn
, srp
);
1397 sum
+= TLV_SIZE(tlvh
);
1401 /* Delete Segment Routing from Extended Prefix LSA */
1402 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1404 struct listnode
*node
;
1405 struct sr_prefix
*srp
;
1406 struct sr_node
*srn
;
1407 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1408 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1410 if (IS_DEBUG_OSPF_SR
)
1412 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1413 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1414 inet_ntoa(lsah
->adv_router
));
1417 if (OspfSR
.neighbors
== NULL
) {
1418 flog_err(EC_OSPF_SR_INVALID_DB
,
1419 "SR (%s): Abort! no valid SR DataBase", __func__
);
1423 /* Search SR Node in hash table from Router ID */
1424 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1425 (void *)&(lsah
->adv_router
));
1428 * SR-Node may be NULL if it has been remove previously when
1429 * processing Router Information LSA deletion
1432 flog_err(EC_OSPF_SR_INVALID_DB
,
1433 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1434 __func__
, inet_ntoa(lsah
->adv_router
));
1438 /* Search for corresponding Segment Link */
1439 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1440 if (srp
->instance
== instance
)
1443 /* Remove Segment Link if found */
1444 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1445 del_sid_nhlfe(srp
->nhlfe
);
1446 listnode_delete(srn
->ext_link
, srp
);
1447 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1450 EC_OSPF_SR_INVALID_DB
,
1451 "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %s",
1452 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1453 inet_ntoa(lsah
->adv_router
));
1457 /* Get Label for Extended Link SID */
1458 /* TODO: To be replace by Zebra Label Manager */
1459 uint32_t get_ext_link_label_value(void)
1461 static uint32_t label
= ADJ_SID_MIN
- 1;
1463 if (label
< ADJ_SID_MAX
)
1470 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1471 * complete initial CLI command at startutp.
1473 * @param ifp - Loopback interface
1474 * @param pref - Prefix address of this interface
1478 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1480 struct listnode
*node
;
1481 struct sr_prefix
*srp
;
1484 if ((ifp
== NULL
) || (p
== NULL
))
1488 * Search if there is a Segment Prefix that correspond to this
1489 * interface or prefix, and update it if found
1491 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1492 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1493 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1495 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1497 /* Update Interface & Prefix info */
1498 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1499 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1501 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1502 srp
->nhlfe
.prefv4
.family
= p
->family
;
1503 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1505 /* OK. Let's Schedule Extended Prefix LSA */
1506 srp
->instance
= ospf_ext_schedule_prefix_index(
1507 ifp
, srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1509 /* Install NHLFE if NO-PHP is requested */
1510 if (CHECK_FLAG(srp
->flags
,
1511 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1512 srp
->nhlfe
.label_in
= index2label(
1513 srp
->sid
, OspfSR
.self
->srgb
);
1514 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1515 add_sid_nhlfe(srp
->nhlfe
);
1522 * Following functions are used to update MPLS LFIB after a SPF run
1525 static void ospf_sr_nhlfe_update(struct hash_bucket
*bucket
, void *args
)
1528 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1529 struct listnode
*node
;
1530 struct sr_prefix
*srp
;
1531 struct sr_nhlfe old
;
1534 if (IS_DEBUG_OSPF_SR
)
1535 zlog_debug(" |- Update Prefix for SR Node %s",
1536 inet_ntoa(srn
->adv_router
));
1538 /* Skip Self SR Node */
1539 if (srn
== OspfSR
.self
)
1542 /* Update Extended Prefix */
1543 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1545 /* Backup current NHLFE */
1546 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1548 /* Compute the new NHLFE */
1549 rc
= compute_prefix_nhlfe(srp
);
1551 /* Check computation result */
1553 /* next hop is not know, remove old NHLFE to avoid loop */
1555 del_sid_nhlfe(srp
->nhlfe
);
1557 /* next hop has not changed, skip it */
1560 /* there is a new next hop, update NHLFE */
1562 update_sid_nhlfe(old
, srp
->nhlfe
);
1570 static int ospf_sr_update_schedule(struct thread
*t
)
1574 struct timeval start_time
, stop_time
;
1576 ospf
= THREAD_ARG(t
);
1577 ospf
->t_sr_update
= NULL
;
1582 monotime(&start_time
);
1584 if (IS_DEBUG_OSPF_SR
)
1585 zlog_debug("SR (%s): Start SPF update", __func__
);
1587 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
1588 void *))ospf_sr_nhlfe_update
,
1591 monotime(&stop_time
);
1593 if (IS_DEBUG_OSPF_SR
)
1594 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld",
1596 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1597 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1599 OspfSR
.update
= false;
1603 #define OSPF_SR_UPDATE_INTERVAL 1
1605 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1611 /* Check if an update is not alreday engage */
1615 OspfSR
.update
= true;
1617 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1618 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1622 * --------------------------------------
1623 * Followings are vty command functions.
1624 * --------------------------------------
1628 * Segment Routing Router configuration
1630 * Must be centralize as it concerns both Extended Link/Prefix LSA
1631 * and Router Information LSA. Choose to call it from Extended Prefix
1632 * write_config() call back.
1634 * @param vty VTY output
1638 void ospf_sr_config_write_router(struct vty
*vty
)
1640 struct listnode
*node
;
1641 struct sr_prefix
*srp
;
1643 if (OspfSR
.enabled
) {
1644 vty_out(vty
, " segment-routing on\n");
1646 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1647 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1648 vty_out(vty
, " segment-routing global-block %u %u\n",
1649 OspfSR
.srgb
.lower_bound
,
1650 OspfSR
.srgb
.lower_bound
+ OspfSR
.srgb
.range_size
1653 if (OspfSR
.msd
!= 0)
1654 vty_out(vty
, " segment-routing node-msd %u\n",
1657 if (OspfSR
.self
!= NULL
) {
1658 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1661 " segment-routing prefix %s/%u "
1663 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1664 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1665 CHECK_FLAG(srp
->flags
,
1666 EXT_SUBTLV_PREFIX_SID_NPFLG
)
1674 DEFUN(ospf_sr_enable
,
1676 "segment-routing on",
1678 "Enable Segment Routing\n")
1681 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1686 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1688 "Segment Routing is only supported in default "
1690 return CMD_WARNING_CONFIG_FAILED
;
1693 if (IS_DEBUG_OSPF_EVENT
)
1694 zlog_debug("SR: Segment Routing: OFF -> ON");
1696 /* Start Segment Routing */
1697 OspfSR
.enabled
= true;
1698 ospf_sr_start(ospf
);
1700 /* Set Router Information SR parameters */
1701 if (IS_DEBUG_OSPF_EVENT
)
1702 zlog_debug("SR: Activate SR for Router Information LSA");
1704 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1706 /* Update Ext LSA */
1707 if (IS_DEBUG_OSPF_EVENT
)
1708 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1710 ospf_ext_update_sr(true);
1715 DEFUN (no_ospf_sr_enable
,
1716 no_ospf_sr_enable_cmd
,
1717 "no segment-routing [on]",
1720 "Disable Segment Routing\n")
1723 if (!OspfSR
.enabled
)
1726 if (IS_DEBUG_OSPF_EVENT
)
1727 zlog_debug("SR: Segment Routing: ON -> OFF");
1729 /* Start by Disabling Extended Link & Prefix LSA */
1730 ospf_ext_update_sr(false);
1732 /* then, disable Router Information SR parameters */
1733 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1735 /* Finally, stop Segment Routing */
1737 OspfSR
.enabled
= false;
1742 static int ospf_sr_enabled(struct vty
*vty
)
1748 vty_out(vty
, "%% OSPF SR is not turned on\n");
1753 DEFUN (sr_sid_label_range
,
1754 sr_sid_label_range_cmd
,
1755 "segment-routing global-block (0-1048575) (0-1048575)",
1757 "Segment Routing Global Block label range\n"
1758 "Lower-bound range in decimal (0-1048575)\n"
1759 "Upper-bound range in decimal (0-1048575)\n")
1767 if (!ospf_sr_enabled(vty
))
1768 return CMD_WARNING_CONFIG_FAILED
;
1770 /* Get lower and upper bound */
1771 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1772 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1773 size
= upper
- lower
+ 1;
1775 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1777 "Range size cannot be less than 0 or more than %u\n",
1778 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1779 return CMD_WARNING_CONFIG_FAILED
;
1782 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1783 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1784 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1785 return CMD_WARNING_CONFIG_FAILED
;
1788 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1789 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1790 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1791 return CMD_WARNING_CONFIG_FAILED
;
1794 /* Check if values have changed */
1795 if ((OspfSR
.srgb
.range_size
== size
)
1796 && (OspfSR
.srgb
.lower_bound
== lower
))
1799 /* Set SID/Label range SRGB */
1800 OspfSR
.srgb
.range_size
= size
;
1801 OspfSR
.srgb
.lower_bound
= lower
;
1802 if (OspfSR
.self
!= NULL
) {
1803 OspfSR
.self
->srgb
.range_size
= size
;
1804 OspfSR
.self
->srgb
.lower_bound
= lower
;
1807 /* Set Router Information SR parameters */
1808 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1810 /* Update NHLFE entries */
1811 hash_iterate(OspfSR
.neighbors
,
1812 (void (*)(struct hash_bucket
*, void *))update_in_nhlfe
,
1818 DEFUN (no_sr_sid_label_range
,
1819 no_sr_sid_label_range_cmd
,
1820 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1823 "Segment Routing Global Block label range\n"
1824 "Lower-bound range in decimal (0-1048575)\n"
1825 "Upper-bound range in decimal (0-1048575)\n")
1828 if (!ospf_sr_enabled(vty
))
1829 return CMD_WARNING_CONFIG_FAILED
;
1831 /* Revert to default SRGB value */
1832 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1833 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1834 if (OspfSR
.self
!= NULL
) {
1835 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1836 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1839 /* Set Router Information SR parameters */
1840 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1842 /* Update NHLFE entries */
1843 hash_iterate(OspfSR
.neighbors
,
1844 (void (*)(struct hash_bucket
*, void *))update_in_nhlfe
,
1852 "segment-routing node-msd (1-16)",
1854 "Maximum Stack Depth for this router\n"
1855 "Maximum number of label that could be stack (1-16)\n")
1860 if (!ospf_sr_enabled(vty
))
1861 return CMD_WARNING_CONFIG_FAILED
;
1864 argv_find(argv
, argc
, "(1-16)", &idx
);
1865 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1866 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1867 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1869 return CMD_WARNING_CONFIG_FAILED
;
1872 /* Check if value has changed */
1873 if (OspfSR
.msd
== msd
)
1876 /* Set this router MSD */
1878 if (OspfSR
.self
!= NULL
)
1879 OspfSR
.self
->msd
= msd
;
1881 /* Set Router Information SR parameters */
1882 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1887 DEFUN (no_sr_node_msd
,
1889 "no segment-routing node-msd [(1-16)]",
1892 "Maximum Stack Depth for this router\n"
1893 "Maximum number of label that could be stack (1-16)\n")
1896 if (!ospf_sr_enabled(vty
))
1897 return CMD_WARNING_CONFIG_FAILED
;
1899 /* unset this router MSD */
1901 if (OspfSR
.self
!= NULL
)
1902 OspfSR
.self
->msd
= 0;
1904 /* Set Router Information SR parameters */
1905 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1910 DEFUN (sr_prefix_sid
,
1912 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1915 "IPv4 Prefix as A.B.C.D/M\n"
1916 "SID index for this prefix in decimal (0-65535)\n"
1917 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1918 "Don't request Penultimate Hop Popping (PHP)\n")
1923 struct listnode
*node
;
1924 struct sr_prefix
*srp
, *new;
1925 struct interface
*ifp
;
1927 if (!ospf_sr_enabled(vty
))
1928 return CMD_WARNING_CONFIG_FAILED
;
1930 /* Get network prefix */
1931 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1932 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1933 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
1934 return CMD_WARNING_CONFIG_FAILED
;
1937 /* Get & verify index value */
1938 argv_find(argv
, argc
, "(0-65535)", &idx
);
1939 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1940 if (index
> OspfSR
.srgb
.range_size
- 1) {
1941 vty_out(vty
, "Index %u must be lower than range size %u\n",
1942 index
, OspfSR
.srgb
.range_size
);
1943 return CMD_WARNING_CONFIG_FAILED
;
1946 /* check that the index is not already used */
1947 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1948 if (srp
->sid
== index
) {
1949 vty_out(vty
, "Index %u is already used\n", index
);
1950 return CMD_WARNING_CONFIG_FAILED
;
1954 /* Create new Extended Prefix to SRDB if not found */
1955 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1956 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1957 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1958 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1959 new->nhlfe
.prefv4
.family
= p
.family
;
1961 /* Set NO PHP flag if present and compute NHLFE */
1962 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1963 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1964 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1965 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1968 if (IS_DEBUG_OSPF_SR
)
1969 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1970 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
1971 new->nhlfe
.prefv4
.prefixlen
);
1973 /* Get Interface and check if it is a Loopback */
1974 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1977 * Interface could be not yet available i.e. when this
1978 * command is in the configuration file, OSPF is not yet
1979 * ready. In this case, store the prefix SID for latter
1980 * update of this Extended Prefix
1982 listnode_add(OspfSR
.self
->ext_prefix
, new);
1984 "Interface for prefix %s/%u not found. Deferred LSA "
1986 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
1990 if (!if_is_loopback(ifp
)) {
1991 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
1992 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
1993 return CMD_WARNING_CONFIG_FAILED
;
1995 new->nhlfe
.ifindex
= ifp
->ifindex
;
1997 /* Search if this prefix already exist */
1998 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1999 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2000 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2006 /* Update or Add this new SR Prefix */
2008 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2009 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2010 listnode_add(OspfSR
.self
->ext_prefix
, new);
2012 listnode_add(OspfSR
.self
->ext_prefix
, new);
2013 add_sid_nhlfe(new->nhlfe
);
2016 /* Finally, update Extended Prefix LSA */
2017 new->instance
= ospf_ext_schedule_prefix_index(
2018 ifp
, new->sid
, &new->nhlfe
.prefv4
, new->flags
);
2019 if (new->instance
== 0) {
2020 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2021 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2028 DEFUN (no_sr_prefix_sid
,
2029 no_sr_prefix_sid_cmd
,
2030 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2034 "IPv4 Prefix as A.B.C.D/M\n"
2035 "SID index for this prefix in decimal (0-65535)\n"
2036 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2037 "Don't request Penultimate Hop Popping (PHP)\n")
2041 struct listnode
*node
;
2042 struct sr_prefix
*srp
;
2043 struct interface
*ifp
;
2047 if (!ospf_sr_enabled(vty
))
2048 return CMD_WARNING_CONFIG_FAILED
;
2050 /* Get network prefix */
2051 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2052 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2054 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2055 return CMD_WARNING_CONFIG_FAILED
;
2058 /* check that the prefix is already set */
2059 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2060 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2061 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2067 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2069 return CMD_WARNING_CONFIG_FAILED
;
2073 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2075 vty_out(vty
, "interface for prefix %s not found.\n",
2077 return CMD_WARNING_CONFIG_FAILED
;
2080 /* Update Extended Prefix LSA */
2081 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2082 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2086 if (IS_DEBUG_OSPF_SR
)
2087 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2088 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2089 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2091 /* Delete NHLFE is NO-PHP is set */
2092 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2093 del_sid_nhlfe(srp
->nhlfe
);
2095 /* OK, all is clean, remove SRP from SRDB */
2096 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2097 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2103 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2104 struct sr_node
*srn
)
2107 struct listnode
*node
;
2108 struct sr_link
*srl
;
2109 struct sr_prefix
*srp
;
2110 struct interface
*itf
;
2114 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2115 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2122 json_node
= json_object_new_object();
2123 json_object_string_add(json_node
, "routerID",
2124 inet_ntoa(srn
->adv_router
));
2125 json_object_int_add(json_node
, "srgbSize",
2126 srn
->srgb
.range_size
);
2127 json_object_int_add(json_node
, "srgbLabel",
2128 srn
->srgb
.lower_bound
);
2129 json_algo
= json_object_new_array();
2130 json_object_object_add(json_node
, "algorithms", json_algo
);
2131 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2132 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2134 json_obj
= json_object_new_object();
2137 snprintf(tmp
, 2, "%u", i
);
2138 json_object_string_add(json_obj
, tmp
,
2139 srn
->algo
[i
] == SR_ALGORITHM_SPF
2142 json_object_array_add(json_algo
, json_obj
);
2145 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2147 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2148 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2149 srn
->srgb
.lower_bound
);
2150 vty_out(vty
, "\tAlgorithm(s): %s",
2151 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2152 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2153 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2156 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2160 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2165 "\n\n Prefix or Link Label In Label Out "
2166 "Node or Adj. SID Interface Nexthop\n");
2168 "------------------ -------- --------- "
2169 "--------------------- --------- ---------------\n");
2171 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2172 snprintf(pref
, 19, "%s/%u", inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2173 srp
->nhlfe
.prefv4
.prefixlen
);
2174 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2175 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2176 sprintf(label
, "pop");
2178 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2179 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2182 json_prefix
= json_object_new_array();
2183 json_object_object_add(json_node
,
2187 json_obj
= json_object_new_object();
2188 json_object_string_add(json_obj
, "prefix", pref
);
2189 json_object_int_add(json_obj
, "sid", srp
->sid
);
2190 json_object_int_add(json_obj
, "inputLabel",
2191 srp
->nhlfe
.label_in
);
2192 json_object_string_add(json_obj
, "outputLabel", label
);
2193 json_object_string_add(json_obj
, "interface",
2194 itf
? itf
->name
: "-");
2195 json_object_string_add(json_obj
, "nexthop",
2196 inet_ntoa(srp
->nhlfe
.nexthop
));
2197 json_object_array_add(json_prefix
, json_obj
);
2199 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2200 srp
->nhlfe
.label_in
, label
, sid
,
2201 itf
? itf
->name
: "-",
2202 inet_ntoa(srp
->nhlfe
.nexthop
));
2206 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2207 snprintf(pref
, 19, "%s/%u",
2208 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2209 srl
->nhlfe
[0].prefv4
.prefixlen
);
2210 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2211 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2212 sprintf(label
, "pop");
2214 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2215 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2218 json_link
= json_object_new_array();
2219 json_object_object_add(
2220 json_node
, "extendedLink", json_link
);
2223 json_obj
= json_object_new_object();
2224 json_object_string_add(json_obj
, "prefix", pref
);
2225 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2226 json_object_int_add(json_obj
, "inputLabel",
2227 srl
->nhlfe
[0].label_in
);
2228 json_object_string_add(json_obj
, "outputLabel", label
);
2229 json_object_string_add(json_obj
, "interface",
2230 itf
? itf
->name
: "-");
2231 json_object_string_add(
2232 json_obj
, "nexthop",
2233 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2234 json_object_array_add(json_link
, json_obj
);
2236 json_obj
= json_object_new_object();
2237 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2238 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2239 sprintf(label
, "pop");
2241 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2242 json_object_string_add(json_obj
, "prefix", pref
);
2243 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2244 json_object_int_add(json_obj
, "inputLabel",
2245 srl
->nhlfe
[1].label_in
);
2246 json_object_string_add(json_obj
, "outputLabel", label
);
2247 json_object_string_add(json_obj
, "interface",
2248 itf
? itf
->name
: "-");
2249 json_object_string_add(
2250 json_obj
, "nexthop",
2251 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2252 json_object_array_add(json_link
, json_obj
);
2254 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2255 srl
->nhlfe
[0].label_in
, label
, sid
,
2256 itf
? itf
->name
: "-",
2257 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2258 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2259 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2260 sprintf(label
, "pop");
2262 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2263 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2264 srl
->nhlfe
[1].label_in
, label
, sid
,
2265 itf
? itf
->name
: "-",
2266 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2270 json_object_array_add(json
, json_node
);
2275 static void show_vty_srdb(struct hash_bucket
*bucket
, void *args
)
2277 struct vty
*vty
= (struct vty
*)args
;
2278 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
2280 show_sr_node(vty
, NULL
, srn
);
2283 static void show_json_srdb(struct hash_bucket
*bucket
, void *args
)
2285 struct json_object
*json
= (struct json_object
*)args
;
2286 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
2288 show_sr_node(NULL
, json
, srn
);
2291 DEFUN (show_ip_opsf_srdb
,
2292 show_ip_ospf_srdb_cmd
,
2293 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2297 "Database summary\n"
2298 "Show Segment Routing Data Base\n"
2299 "Advertising SR node\n"
2300 "Advertising SR node ID (as an IP address)\n"
2301 "Self-originated SR node\n"
2306 struct sr_node
*srn
;
2307 bool uj
= use_json(argc
, argv
);
2308 json_object
*json
= NULL
, *json_node_array
= NULL
;
2310 if (!OspfSR
.enabled
) {
2311 vty_out(vty
, "Segment Routing is disabled on this router\n");
2316 json
= json_object_new_object();
2317 json_node_array
= json_object_new_array();
2318 json_object_string_add(json
, "srdbID",
2319 inet_ntoa(OspfSR
.self
->adv_router
));
2320 json_object_object_add(json
, "srNodes", json_node_array
);
2323 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2324 inet_ntoa(OspfSR
.self
->adv_router
));
2327 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2329 show_sr_node(vty
, json_node_array
, srn
);
2331 vty_out(vty
, "%s\n",
2332 json_object_to_json_string_ext(
2333 json
, JSON_C_TO_STRING_PRETTY
));
2334 json_object_free(json
);
2339 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2340 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2341 vty_out(vty
, "Specified Router ID %s is invalid\n",
2343 return CMD_WARNING_CONFIG_FAILED
;
2345 /* Get the SR Node from the SRDB */
2346 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2348 show_sr_node(vty
, json_node_array
, srn
);
2350 vty_out(vty
, "%s\n",
2351 json_object_to_json_string_ext(
2352 json
, JSON_C_TO_STRING_PRETTY
));
2353 json_object_free(json
);
2358 /* No parameters have been provided, Iterate through all the SRDB */
2360 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
2361 void *))show_json_srdb
,
2362 (void *)json_node_array
);
2363 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2364 json
, JSON_C_TO_STRING_PRETTY
));
2365 json_object_free(json
);
2367 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
2368 void *))show_vty_srdb
,
2374 /* Install new CLI commands */
2375 void ospf_sr_register_vty(void)
2377 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2379 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2380 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2381 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2382 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2383 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2384 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2385 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2386 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);