2 * This is an implementation of Segment Routing
3 * as per draft draft-ietf-ospf-segment-routing-extensions-24
5 * Module name: Segment Routing
7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "libospf.h" /* for ospf interface types */
44 #include "sockunion.h" /* for inet_aton() */
52 #include "ospfd/ospfd.h"
53 #include "ospfd/ospf_interface.h"
54 #include "ospfd/ospf_ism.h"
55 #include "ospfd/ospf_asbr.h"
56 #include "ospfd/ospf_lsa.h"
57 #include "ospfd/ospf_lsdb.h"
58 #include "ospfd/ospf_neighbor.h"
59 #include "ospfd/ospf_nsm.h"
60 #include "ospfd/ospf_flood.h"
61 #include "ospfd/ospf_packet.h"
62 #include "ospfd/ospf_spf.h"
63 #include "ospfd/ospf_dump.h"
64 #include "ospfd/ospf_route.h"
65 #include "ospfd/ospf_ase.h"
66 #include "ospfd/ospf_sr.h"
67 #include "ospfd/ospf_ri.h"
68 #include "ospfd/ospf_ext.h"
69 #include "ospfd/ospf_zebra.h"
72 * Global variable to manage Segment Routing on this node.
73 * Note that all parameter values are stored in network byte order.
75 static struct ospf_sr_db OspfSR
;
76 static void ospf_sr_register_vty(void);
77 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
);
80 * Segment Routing Data Base functions
83 /* Hash function for Segment Routing entry */
84 static unsigned int sr_hash(void *p
)
86 const struct in_addr
*rid
= p
;
88 return jhash_1word(rid
->s_addr
, 0);
91 /* Compare 2 Router ID hash entries based on SR Node */
92 static int sr_cmp(const void *p1
, const void *p2
)
94 const struct sr_node
*srn
= p1
;
95 const struct in_addr
*rid
= p2
;
97 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
100 /* Functions to remove an SR Link */
101 static void del_sr_link(void *val
)
103 struct sr_link
*srl
= (struct sr_link
*)val
;
105 del_sid_nhlfe(srl
->nhlfe
[0]);
106 del_sid_nhlfe(srl
->nhlfe
[1]);
107 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
111 /* Functions to remove an SR Prefix */
112 static void del_sr_pref(void *val
)
114 struct sr_prefix
*srp
= (struct sr_prefix
*)val
;
116 del_sid_nhlfe(srp
->nhlfe
);
117 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
121 /* Allocate new Segment Routine node */
122 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
130 /* Allocate Segment Routing node memory */
131 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
135 zlog_err("SR (%s): Abort! can't create new SR node", __func__
);
139 /* Default Algorithm, SRGB and MSD */
140 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
141 new->algo
[i
] = SR_ALGORITHM_UNSET
;
143 new->srgb
.range_size
= 0;
144 new->srgb
.lower_bound
= 0;
147 /* Create Link, Prefix and Range TLVs list */
148 new->ext_link
= list_new();
149 new->ext_prefix
= list_new();
150 new->ext_link
->del
= del_sr_link
;
151 new->ext_prefix
->del
= del_sr_pref
;
153 IPV4_ADDR_COPY(&new->adv_router
, rid
);
154 new->neighbor
= NULL
;
157 if (IS_DEBUG_OSPF_SR
)
158 zlog_debug(" |- Created new SR node for %s",
159 inet_ntoa(new->adv_router
));
163 /* Delete Segment Routing node */
164 static void sr_node_del(struct sr_node
*srn
)
170 /* Clean Extended Link */
171 list_delete_and_null(&srn
->ext_link
);
173 /* Clean Prefix List */
174 list_delete_and_null(&srn
->ext_prefix
);
176 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
179 /* Get SR Node for a given nexthop */
180 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
181 struct in_addr nexthop
)
183 struct ospf_interface
*oi
= NULL
;
184 struct ospf_neighbor
*nbr
= NULL
;
185 struct listnode
*node
;
186 struct route_node
*rn
;
191 if (OspfSR
.neighbors
== NULL
)
194 if (IS_DEBUG_OSPF_SR
)
195 zlog_debug(" |- Search SR-Node for nexthop %s",
198 /* First, search neighbor Router ID for this nexthop */
200 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
201 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
203 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
215 if (IS_DEBUG_OSPF_SR
)
216 zlog_debug(" |- Found nexthop Router ID %s",
217 inet_ntoa(nbr
->router_id
));
218 /* Then, search SR Node */
219 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
225 * Segment Routing Initialization functions
228 /* Segment Routing starter function */
229 static int ospf_sr_start(struct ospf
*ospf
)
231 struct route_node
*rn
;
232 struct ospf_lsa
*lsa
;
236 if (IS_DEBUG_OSPF_SR
)
237 zlog_debug("SR (%s): Start Segment Routing", __func__
);
239 /* Initialize self SR Node */
240 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
241 (void *)sr_node_new
);
247 /* Complete & Store self SR Node */
248 srn
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
249 srn
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
250 srn
->algo
[0] = OspfSR
.algo
[0];
251 srn
->msd
= OspfSR
.msd
;
254 if (IS_DEBUG_OSPF_EVENT
)
255 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__
);
257 /* Start by looking to Router Info & Extended LSA in lsdb */
258 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
259 LSDB_LOOP(OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
)
261 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
264 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
266 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
267 ospf_sr_ri_lsa_update(lsa
);
269 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
270 ospf_sr_ext_prefix_lsa_update(lsa
);
272 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
273 ospf_sr_ext_link_lsa_update(lsa
);
285 /* Stop Segment Routing */
286 static void ospf_sr_stop(void)
289 if (IS_DEBUG_OSPF_SR
)
290 zlog_debug("SR (%s): Stop Segment Routing", __func__
);
293 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
294 * be remove though list_delete_and_null() call. See sr_node_del()
296 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
300 * Segment Routing initialize function
304 * @return 0 if OK, -1 otherwise
306 int ospf_sr_init(void)
310 if (IS_DEBUG_OSPF_SR
)
311 zlog_info("SR (%s): Initialize SR Data Base", __func__
);
313 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
314 OspfSR
.enabled
= false;
315 /* Only AREA flooding is supported in this release */
316 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
318 /* Initialize SRGB, Algorithms and MSD TLVs */
319 /* Only Algorithm SPF is supported */
320 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
321 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
322 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
324 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MAX_SRGB_SIZE
;
325 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
328 /* Initialize Hash table for neighbor SR nodes */
329 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
330 if (OspfSR
.neighbors
== NULL
)
333 /* Initialize Route Table for prefix */
334 OspfSR
.prefix
= route_table_init();
335 if (OspfSR
.prefix
== NULL
)
338 /* Register Segment Routing VTY command */
339 ospf_sr_register_vty();
346 * Segment Routing termination function
351 void ospf_sr_term(void)
354 /* Stop Segment Routing */
357 /* Clear SR Node Table */
358 if (OspfSR
.neighbors
)
359 hash_free(OspfSR
.neighbors
);
361 /* Clear Prefix Table */
363 route_table_finish(OspfSR
.prefix
);
365 OspfSR
.enabled
= false;
370 * Segment Routing finish function
375 void ospf_sr_finish(void)
377 /* Stop Segment Routing */
380 OspfSR
.enabled
= false;
384 * Following functions are used to manipulate the
385 * Next Hop Label Forwarding entry (NHLFE)
388 /* Compute label from index */
389 static mpls_label_t
index2label(uint32_t index
, struct sr_srgb srgb
)
393 label
= srgb
.lower_bound
+ index
;
394 if (label
> (srgb
.lower_bound
+ srgb
.range_size
))
395 return MPLS_INVALID_LABEL
;
400 /* Get neighbor full structure from address */
401 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
404 struct ospf_neighbor
*nbr
;
405 struct ospf_interface
*oi
;
406 struct listnode
*node
;
407 struct route_node
*rn
;
413 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
414 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
417 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
419 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
420 route_unlock_node(rn
);
427 /* Get OSPF Path from address */
428 static struct ospf_path
*get_nexthop_by_addr(struct ospf
*top
,
429 struct prefix_ipv4 p
)
431 struct ospf_route
*or;
432 struct ospf_path
*path
;
433 struct listnode
*node
;
434 struct route_node
*rn
;
440 if (IS_DEBUG_OSPF_SR
)
441 zlog_debug(" |- Search Nexthop for prefix %s/%u",
442 inet_ntoa(p
.prefix
), p
.prefixlen
);
444 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
447 * Check if we found an OSPF route. May be NULL if SPF has not
448 * yet populate routing table for this prefix.
453 route_unlock_node(rn
);
458 /* Then search path from this route */
459 for (ALL_LIST_ELEMENTS_RO(or->paths
, node
, path
))
460 if (path
->nexthop
.s_addr
!= INADDR_ANY
|| path
->ifindex
!= 0)
466 /* Compute NHLFE entry for Extended Link */
467 static int compute_link_nhlfe(struct sr_link
*srl
)
469 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
470 struct ospf_neighbor
*nh
;
473 if (IS_DEBUG_OSPF_SR
)
474 zlog_debug(" |- Compute NHLFE for link %s/%u",
475 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
476 srl
->nhlfe
[0].prefv4
.prefixlen
);
478 /* First determine the OSPF Neighbor */
479 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
481 /* Neighbor could be not found when OSPF Adjacency just fire up
482 * because SPF don't yet populate routing table. This NHLFE will
483 * be fixed later when SR SPF schedule will be called.
488 if (IS_DEBUG_OSPF_SR
)
489 zlog_debug(" |- Found nexthop NHLFE %s",
490 inet_ntoa(nh
->router_id
));
492 /* Set ifindex for this neighbor */
493 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
494 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
496 /* Update neighbor address for LAN_ADJ_SID */
497 if (srl
->type
== LAN_ADJ_SID
) {
498 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
499 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
502 /* Set Input & Output Label */
503 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
504 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
506 srl
->nhlfe
[0].label_in
=
507 index2label(srl
->sid
[0], srl
->srn
->srgb
);
508 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
509 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
511 srl
->nhlfe
[1].label_in
=
512 index2label(srl
->sid
[1], srl
->srn
->srgb
);
514 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
515 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
522 * Compute NHLFE entry for Extended Prefix
524 * @param srp - Segment Routing Prefix
526 * @return -1 if next hop is not found, 0 if nexthop has not changed
529 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
531 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
532 struct ospf_path
*nh
= NULL
;
533 struct sr_node
*srnext
;
536 if (IS_DEBUG_OSPF_SR
)
537 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
538 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
539 srp
->nhlfe
.prefv4
.prefixlen
);
541 /* First determine the nexthop */
542 nh
= get_nexthop_by_addr(top
, srp
->nhlfe
.prefv4
);
544 /* Nexthop could be not found when OSPF Adjacency just fire up
545 * because SPF don't yet populate routing table. This NHLFE will
546 * be fixed later when SR SPF schedule will be called.
551 /* Check if NextHop has changed when call after running a new SPF */
552 if (IPV4_ADDR_SAME(&nh
->nexthop
, &srp
->nhlfe
.nexthop
)
553 && (nh
->ifindex
== srp
->nhlfe
.ifindex
))
556 if (IS_DEBUG_OSPF_SR
)
557 zlog_debug(" |- Found new next hop for this NHLFE: %s",
558 inet_ntoa(nh
->nexthop
));
561 * Get SR-Node for this nexthop. Could be not yet available
562 * as Extende Link / Prefix and Router Information are flooded
563 * after LSA Type 1 & 2 which populate the OSPF Route Table
565 srnext
= get_sr_node_by_nexthop(top
, nh
->nexthop
);
569 /* And store this information for later update if SR Node is found */
570 srnext
->neighbor
= OspfSR
.self
;
571 if (IPV4_ADDR_SAME(&srnext
->adv_router
, &srp
->adv_router
))
574 srp
->nexthop
= srnext
;
577 * SR Node could be known, but SRGB could be not initialize
578 * This is due to the fact that Extended Link / Prefix could
579 * be received before corresponding Router Information LSA
581 if ((srnext
== NULL
) || (srnext
->srgb
.lower_bound
== 0)
582 || (srnext
->srgb
.range_size
== 0))
585 if (IS_DEBUG_OSPF_SR
)
586 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
587 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
588 inet_ntoa(srnext
->adv_router
));
590 /* Set ip addr & ifindex for this neighbor */
591 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &nh
->nexthop
);
592 srp
->nhlfe
.ifindex
= nh
->ifindex
;
594 /* Compute Input Label with self SRGB */
595 srp
->nhlfe
.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
597 * and Output Label with Next hop SR Node SRGB or Implicit Null label
598 * if next hop is the destination and request PHP
600 if ((srp
->nexthop
== NULL
)
601 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
602 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
603 else if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
604 srp
->nhlfe
.label_out
= srp
->sid
;
606 srp
->nhlfe
.label_out
= index2label(srp
->sid
, srnext
->srgb
);
608 if (IS_DEBUG_OSPF_SR
)
609 zlog_debug(" |- Computed new labels in: %u out: %u",
610 srp
->nhlfe
.label_in
, srp
->nhlfe
.label_out
);
616 /* Send MPLS Label entry to Zebra for installation or deletion */
617 static int ospf_zebra_send_mpls_labels(int cmd
, struct sr_nhlfe nhlfe
)
625 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
626 stream_putc(s
, ZEBRA_LSP_SR
);
627 /* OSPF Segment Routing currently support only IPv4 */
628 stream_putl(s
, nhlfe
.prefv4
.family
);
629 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
630 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
631 stream_put_in_addr(s
, &nhlfe
.nexthop
);
632 stream_putl(s
, nhlfe
.ifindex
);
633 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
634 stream_putl(s
, nhlfe
.label_in
);
635 stream_putl(s
, nhlfe
.label_out
);
637 /* Put length at the first point of the stream. */
638 stream_putw_at(s
, 0, stream_get_endp(s
));
640 if (IS_DEBUG_OSPF_SR
)
641 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
642 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
643 nhlfe
.label_in
, nhlfe
.label_out
,
644 inet_ntoa(nhlfe
.prefv4
.prefix
),
645 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
647 return zclient_send_message(zclient
);
650 /* Request zebra to install/remove FEC in FIB */
651 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
653 struct zapi_route api
;
654 struct zapi_nexthop
*api_nh
;
656 /* Support only IPv4 */
657 if (nhlfe
.prefv4
.family
!= AF_INET
)
660 memset(&api
, 0, sizeof(api
));
661 api
.vrf_id
= VRF_DEFAULT
;
662 api
.type
= ZEBRA_ROUTE_OSPF
;
663 api
.safi
= SAFI_UNICAST
;
664 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
666 if (cmd
== ZEBRA_ROUTE_ADD
) {
668 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
669 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
671 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
672 api_nh
= &api
.nexthops
[0];
673 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
674 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
675 api_nh
->ifindex
= nhlfe
.ifindex
;
677 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
678 api_nh
->labels
[0] = nhlfe
.label_out
;
679 api_nh
->label_num
= 1;
680 api_nh
->vrf_id
= VRF_DEFAULT
;
684 if (IS_DEBUG_OSPF_SR
)
685 zlog_debug(" |- %s FEC %u for %s/%u via %u",
686 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
687 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
688 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
690 return zclient_route_send(cmd
, zclient
, &api
);
693 /* Add new NHLFE entry for SID */
694 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
696 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
697 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
698 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
699 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
703 /* Remove NHLFE entry for SID */
704 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
706 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
707 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
708 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
709 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
713 /* Update NHLFE entry for SID */
714 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
722 * Functions to parse and get Extended Link / Prefix
726 /* Extended Link SubTLVs Getter */
727 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
731 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
732 struct ext_subtlv_adj_sid
*adj_sid
;
733 struct ext_subtlv_lan_adj_sid
*lan_sid
;
734 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
736 struct tlv_header
*sub_tlvh
;
737 uint16_t length
= 0, sum
= 0, i
= 0;
739 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
744 /* Initialize TLV browsing */
745 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
746 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
747 + EXT_TLV_LINK_SIZE
);
748 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
749 switch (ntohs(sub_tlvh
->type
)) {
750 case EXT_SUBTLV_ADJ_SID
:
751 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
753 i
= CHECK_FLAG(adj_sid
->flags
,
754 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
757 srl
->flags
[i
] = adj_sid
->flags
;
758 if (CHECK_FLAG(adj_sid
->flags
,
759 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
760 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
762 srl
->sid
[i
] = ntohl(adj_sid
->value
);
763 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
765 case EXT_SUBTLV_LAN_ADJ_SID
:
766 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
767 srl
->type
= LAN_ADJ_SID
;
768 i
= CHECK_FLAG(lan_sid
->flags
,
769 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
772 srl
->flags
[i
] = lan_sid
->flags
;
773 if (CHECK_FLAG(lan_sid
->flags
,
774 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
775 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
777 srl
->sid
[i
] = ntohl(lan_sid
->value
);
778 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
779 &lan_sid
->neighbor_id
);
781 case EXT_SUBTLV_RMT_ITF_ADDR
:
782 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
783 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
784 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
789 sum
+= TLV_SIZE(sub_tlvh
);
792 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
793 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
794 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
795 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
796 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
797 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
798 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
799 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
801 if (IS_DEBUG_OSPF_SR
) {
802 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
803 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
804 srl
->nhlfe
[0].prefv4
.prefixlen
);
805 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
806 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
807 srl
->nhlfe
[1].prefv4
.prefixlen
);
813 /* Extended Prefix SubTLVs Getter */
814 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
817 struct sr_prefix
*srp
;
818 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
819 struct ext_subtlv_prefix_sid
*psid
;
821 struct tlv_header
*sub_tlvh
;
822 uint16_t length
= 0, sum
= 0;
824 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
829 /* Initialize TLV browsing */
830 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
831 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
832 + EXT_TLV_PREFIX_SIZE
);
833 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
834 switch (ntohs(sub_tlvh
->type
)) {
835 case EXT_SUBTLV_PREFIX_SID
:
836 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
837 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
839 "SR (%s): Unsupported Algorithm",
841 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
844 srp
->type
= PREF_SID
;
845 srp
->flags
= psid
->flags
;
846 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
847 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
849 srp
->sid
= ntohl(psid
->value
);
850 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
852 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
853 srp
->nhlfe
.prefv4
.family
= AF_INET
;
854 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
859 sum
+= TLV_SIZE(sub_tlvh
);
862 if (IS_DEBUG_OSPF_SR
)
863 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
864 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
865 srp
->nhlfe
.prefv4
.prefixlen
);
870 * Functions to manipulate Segment Routing Link & Prefix structures
873 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
874 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
876 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
877 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
878 && (srl1
->flags
[1] == srl2
->flags
[1]))
884 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
885 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
887 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
893 /* Update Segment Link of given Segment Routing Node */
894 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
897 struct listnode
*node
;
902 if ((srn
== NULL
) || (srl
== NULL
))
905 if (IS_DEBUG_OSPF_SR
)
906 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
908 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
909 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
910 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
911 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
914 /* Search for existing Segment Link */
915 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
916 if (lk
->instance
== srl
->instance
) {
921 if (IS_DEBUG_OSPF_SR
)
922 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
923 found
? "Update" : "Add",
924 GET_OPAQUE_ID(srl
->instance
),
925 inet_ntoa(srn
->adv_router
));
927 /* if not found, add new Segment Link and install NHLFE */
929 /* Complete SR-Link and add it to SR-Node list */
931 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
932 listnode_add(srn
->ext_link
, srl
);
933 /* Try to set MPLS table */
934 if (compute_link_nhlfe(srl
)) {
935 add_sid_nhlfe(srl
->nhlfe
[0]);
936 add_sid_nhlfe(srl
->nhlfe
[1]);
939 if (sr_link_cmp(lk
, srl
)) {
940 if (compute_link_nhlfe(srl
)) {
941 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
942 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
943 /* Replace Segment List */
944 listnode_delete(srn
->ext_link
, lk
);
945 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
947 IPV4_ADDR_COPY(&srl
->adv_router
,
949 listnode_add(srn
->ext_link
, srl
);
951 /* New NHLFE was not found.
952 * Just free the SR Link
954 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
958 * This is just an LSA refresh.
959 * Stop processing and free SR Link
961 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
966 /* Update Segment Prefix of given Segment Routing Node */
967 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
970 struct listnode
*node
;
971 struct sr_prefix
*pref
;
975 if (srn
== NULL
|| srp
== NULL
)
978 if (IS_DEBUG_OSPF_SR
)
979 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
981 /* Process only Global Prefix SID */
982 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
985 /* Search for existing Segment Prefix */
986 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
987 if (pref
->instance
== srp
->instance
) {
992 if (IS_DEBUG_OSPF_SR
)
993 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
994 found
? "Update" : "Add",
995 GET_OPAQUE_ID(srp
->instance
),
996 inet_ntoa(srn
->adv_router
));
998 /* if not found, add new Segment Prefix and install NHLFE */
1000 /* Complete SR-Prefix and add it to SR-Node list */
1002 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
1003 listnode_add(srn
->ext_prefix
, srp
);
1004 /* Try to set MPLS table */
1005 if (compute_prefix_nhlfe(srp
) == 1)
1006 add_sid_nhlfe(srp
->nhlfe
);
1008 if (sr_prefix_cmp(pref
, srp
)) {
1009 if (compute_prefix_nhlfe(srp
) == 1) {
1010 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
1011 /* Replace Segment Prefix */
1012 listnode_delete(srn
->ext_prefix
, pref
);
1013 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1015 IPV4_ADDR_COPY(&srp
->adv_router
,
1017 listnode_add(srn
->ext_prefix
, srp
);
1019 /* New NHLFE was not found.
1020 * Just free the SR Prefix
1022 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1025 /* This is just an LSA refresh.
1026 * Stop processing and free SR Prefix
1028 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1034 * When change the FRR Self SRGB, update the NHLFE Input Label
1035 * for all Extended Prefix with SID index through hash_iterate()
1037 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1039 struct listnode
*node
;
1040 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1041 struct sr_prefix
*srp
;
1042 struct sr_nhlfe
new;
1044 /* Process Every Extended Prefix for this SR-Node */
1045 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1046 /* Process Self SRN only if NO-PHP is requested */
1047 if ((srn
== OspfSR
.self
)
1048 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1051 /* Process only SID Index */
1052 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1055 /* OK. Compute new NHLFE */
1056 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1057 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1058 /* Update MPLS LFIB */
1059 update_sid_nhlfe(srp
->nhlfe
, new);
1060 /* Finally update Input Label */
1061 srp
->nhlfe
.label_in
= new.label_in
;
1066 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1067 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1069 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1071 struct listnode
*node
;
1072 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1073 struct sr_node
*srnext
= (struct sr_node
*)args
;
1074 struct sr_prefix
*srp
;
1075 struct sr_nhlfe
new;
1077 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1078 /* Process only SID Index for next hop without PHP */
1079 if ((srp
->nexthop
== NULL
)
1080 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1082 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1083 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1084 update_sid_nhlfe(srp
->nhlfe
, new);
1085 srp
->nhlfe
.label_out
= new.label_out
;
1090 * Following functions are call when new Segment Routing LSA are received
1091 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1092 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1093 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1096 /* Update Segment Routing from Router Information LSA */
1097 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1099 struct sr_node
*srn
;
1100 struct tlv_header
*tlvh
;
1101 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1102 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1103 struct ri_sr_tlv_sr_algorithm
*algo
;
1104 struct sr_srgb srgb
;
1105 uint16_t length
= 0, sum
= 0;
1107 if (IS_DEBUG_OSPF_SR
)
1109 "SR (%s): Process Router "
1110 "Information LSA 4.0.0.%u from %s",
1112 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1113 inet_ntoa(lsah
->adv_router
));
1116 if (IS_LSA_SELF(lsa
))
1119 if (OspfSR
.neighbors
== NULL
) {
1120 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1124 /* Get SR Node in hash table from Router ID */
1125 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1126 (void *)sr_node_new
);
1131 "SR (%s): Abort! can't create SR node in hash table",
1136 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1138 "SR (%s): Abort! Wrong "
1139 "LSA ID 4.0.0.%u for SR node %s/%u",
1141 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1142 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1146 /* Collect Router Information Sub TLVs */
1147 /* Initialize TLV browsing */
1148 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1149 srgb
.range_size
= 0;
1150 srgb
.lower_bound
= 0;
1152 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1153 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1154 switch (ntohs(tlvh
->type
)) {
1155 case RI_SR_TLV_SR_ALGORITHM
:
1156 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1159 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1160 srn
->algo
[i
] = algo
->value
[0];
1161 for (; i
< ALGORITHM_COUNT
; i
++)
1162 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1163 sum
+= TLV_SIZE(tlvh
);
1165 case RI_SR_TLV_SID_LABEL_RANGE
:
1166 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1167 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1169 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1170 sum
+= TLV_SIZE(tlvh
);
1172 case RI_SR_TLV_NODE_MSD
:
1173 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1174 sum
+= TLV_SIZE(tlvh
);
1177 sum
+= TLV_SIZE(tlvh
);
1182 /* Check that we collect mandatory parameters */
1183 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1184 || srgb
.lower_bound
== 0) {
1186 "SR (%s): Missing mandatory parameters. Abort!",
1188 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1189 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1193 /* Check if it is a new SR Node or not */
1194 if (srn
->instance
== 0) {
1196 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1198 srn
->srgb
.range_size
= srgb
.range_size
;
1199 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1202 /* Check if SRGB has changed */
1203 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1204 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1205 srn
->srgb
.range_size
= srgb
.range_size
;
1206 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1207 /* Update NHLFE if it is a neighbor SR node */
1208 if (srn
->neighbor
== OspfSR
.self
)
1209 hash_iterate(OspfSR
.neighbors
,
1210 (void (*)(struct hash_backet
*,
1211 void *))update_out_nhlfe
,
1218 * Delete SR Node entry in hash table information corresponding to an expired
1219 * Router Information LSA
1221 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1223 struct sr_node
*srn
;
1224 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1226 if (IS_DEBUG_OSPF_SR
)
1228 "SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1229 __func__
, inet_ntoa(lsah
->adv_router
),
1230 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1233 if (OspfSR
.neighbors
== NULL
) {
1234 zlog_err("SR (%s): Abort! no valid SR Data Base", __func__
);
1238 /* Release Router ID entry in SRDB hash table */
1239 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1244 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1245 __func__
, inet_ntoa(lsah
->adv_router
));
1249 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1251 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1252 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1253 inet_ntoa(lsah
->adv_router
));
1257 /* Remove SR node */
1262 /* Update Segment Routing from Extended Link LSA */
1263 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1265 struct sr_node
*srn
;
1266 struct tlv_header
*tlvh
;
1267 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1268 struct sr_link
*srl
;
1270 uint16_t length
, sum
;
1272 if (IS_DEBUG_OSPF_SR
)
1274 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1275 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1276 inet_ntoa(lsah
->adv_router
));
1279 if (OspfSR
.neighbors
== NULL
) {
1280 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1284 /* Get SR Node in hash table from Router ID */
1285 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1286 (void *)&(lsah
->adv_router
),
1287 (void *)sr_node_new
);
1292 "SR (%s): Abort! can't create SR node in hash table",
1297 /* Initialize TLV browsing */
1298 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1300 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1301 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1302 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1303 /* Got Extended Link information */
1304 srl
= get_ext_link_sid(tlvh
);
1305 /* Update SID if not null */
1307 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1308 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1311 sum
+= TLV_SIZE(tlvh
);
1315 /* Delete Segment Routing from Extended Link LSA */
1316 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1318 struct listnode
*node
;
1319 struct sr_link
*srl
;
1320 struct sr_node
*srn
;
1321 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1322 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1324 if (IS_DEBUG_OSPF_SR
)
1326 "SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1327 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1328 inet_ntoa(lsah
->adv_router
));
1331 if (OspfSR
.neighbors
== NULL
) {
1332 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1336 /* Search SR Node in hash table from Router ID */
1337 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1338 (void *)&(lsah
->adv_router
));
1341 * SR-Node may be NULL if it has been remove previously when
1342 * processing Router Information LSA deletion
1346 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1347 __func__
, inet_ntoa(lsah
->adv_router
));
1351 /* Search for corresponding Segment Link */
1352 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1353 if (srl
->instance
== instance
)
1356 /* Remove Segment Link if found */
1357 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1358 del_sid_nhlfe(srl
->nhlfe
[0]);
1359 del_sid_nhlfe(srl
->nhlfe
[1]);
1360 listnode_delete(srn
->ext_link
, srl
);
1361 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1364 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1365 "for SR Node %s", __func__
,
1366 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1367 inet_ntoa(lsah
->adv_router
));
1372 /* Update Segment Routing from Extended Prefix LSA */
1373 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1375 struct sr_node
*srn
;
1376 struct tlv_header
*tlvh
;
1377 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1378 struct sr_prefix
*srp
;
1380 uint16_t length
, sum
;
1382 if (IS_DEBUG_OSPF_SR
)
1384 "SR (%s): Process Extended Prefix LSA "
1385 "7.0.0.%u from %s", __func__
,
1386 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1387 inet_ntoa(lsah
->adv_router
));
1390 if (OspfSR
.neighbors
== NULL
) {
1391 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1395 /* Get SR Node in hash table from Router ID */
1396 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1397 (void *)&(lsah
->adv_router
),
1398 (void *)sr_node_new
);
1403 "SR (%s): Abort! can't create SR node in hash table",
1408 /* Initialize TLV browsing */
1409 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1411 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1412 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1413 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1414 /* Got Extended Link information */
1415 srp
= get_ext_prefix_sid(tlvh
);
1416 /* Update SID if not null */
1418 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1419 update_ext_prefix_sid(srn
, srp
);
1422 sum
+= TLV_SIZE(tlvh
);
1426 /* Delete Segment Routing from Extended Prefix LSA */
1427 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1429 struct listnode
*node
;
1430 struct sr_prefix
*srp
;
1431 struct sr_node
*srn
;
1432 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1433 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1435 if (IS_DEBUG_OSPF_SR
)
1437 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1438 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1439 inet_ntoa(lsah
->adv_router
));
1442 if (OspfSR
.neighbors
== NULL
) {
1443 zlog_err("SR (%s): Abort! no valid SR DataBase", __func__
);
1447 /* Search SR Node in hash table from Router ID */
1448 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1449 (void *)&(lsah
->adv_router
));
1452 * SR-Node may be NULL if it has been remove previously when
1453 * processing Router Information LSA deletion
1457 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1458 __func__
, inet_ntoa(lsah
->adv_router
));
1462 /* Search for corresponding Segment Link */
1463 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1464 if (srp
->instance
== instance
)
1467 /* Remove Segment Link if found */
1468 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1469 del_sid_nhlfe(srp
->nhlfe
);
1470 listnode_delete(srn
->ext_link
, srp
);
1471 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1474 "SR (%s): Didn't found corresponding SR Prefix "
1475 "7.0.0.%u for SR Node %s", __func__
,
1476 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1477 inet_ntoa(lsah
->adv_router
));
1482 /* Get Label for Extended Link SID */
1483 /* TODO: To be replace by Zebra Label Manager */
1484 uint32_t get_ext_link_label_value(void)
1486 static uint32_t label
= ADJ_SID_MIN
- 1;
1488 if (label
< ADJ_SID_MAX
)
1495 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1496 * complete initial CLI command at startutp.
1498 * @param ifp - Loopback interface
1499 * @param pref - Prefix address of this interface
1503 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1505 struct listnode
*node
;
1506 struct sr_prefix
*srp
;
1509 if ((ifp
== NULL
) || (p
== NULL
))
1513 * Search if there is a Segment Prefix that correspond to this
1514 * interface or prefix, and update it if found
1516 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1517 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1518 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1520 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1522 /* Update Interface & Prefix info */
1523 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1524 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1526 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1527 srp
->nhlfe
.prefv4
.family
= p
->family
;
1528 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1530 /* OK. Let's Schedule Extended Prefix LSA */
1531 srp
->instance
= ospf_ext_schedule_prefix_index(ifp
,
1532 srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1534 /* Install NHLFE if NO-PHP is requested */
1535 if (CHECK_FLAG(srp
->flags
,
1536 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1537 srp
->nhlfe
.label_in
= index2label(srp
->sid
,
1539 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1540 add_sid_nhlfe(srp
->nhlfe
);
1547 * Following functions are used to update MPLS LFIB after a SPF run
1550 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1553 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1554 struct listnode
*node
;
1555 struct sr_prefix
*srp
;
1556 struct sr_nhlfe old
;
1563 if (IS_DEBUG_OSPF_SR
)
1564 zlog_debug(" |- Update Prefix for SR Node %s",
1565 inet_ntoa(srn
->adv_router
));
1567 /* Skip Self SR Node */
1568 if (srn
== OspfSR
.self
)
1571 /* Update Extended Prefix */
1572 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1574 /* Backup current NHLFE */
1575 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1577 /* Compute the new NHLFE */
1578 rc
= compute_prefix_nhlfe(srp
);
1580 /* Check computation result */
1582 /* next hop is not know, remove old NHLFE to avoid loop */
1584 del_sid_nhlfe(srp
->nhlfe
);
1586 /* next hop has not changed, skip it */
1589 /* there is a new next hop, update NHLFE */
1591 update_sid_nhlfe(old
, srp
->nhlfe
);
1599 static int ospf_sr_update_schedule(struct thread
*t
)
1603 struct timeval start_time
, stop_time
;
1605 ospf
= THREAD_ARG(t
);
1606 ospf
->t_sr_update
= NULL
;
1611 monotime(&start_time
);
1613 if (IS_DEBUG_OSPF_SR
)
1614 zlog_debug("SR (%s): Start SPF update", __func__
);
1616 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1617 void *))ospf_sr_nhlfe_update
,
1620 monotime(&stop_time
);
1622 if (IS_DEBUG_OSPF_SR
)
1623 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld\n",
1625 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1626 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1628 OspfSR
.update
= false;
1632 #define OSPF_SR_UPDATE_INTERVAL 1
1634 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1640 /* Check if an update is not alreday engage */
1644 OspfSR
.update
= true;
1646 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1647 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1651 * --------------------------------------
1652 * Followings are vty command functions.
1653 * --------------------------------------
1657 * Segment Routing Router configuration
1659 * Must be centralize as it concerns both Extended Link/Prefix LSA
1660 * and Router Information LSA. Choose to call it from Extended Prefix
1661 * write_config() call back.
1663 * @param vty VTY output
1667 void ospf_sr_config_write_router(struct vty
*vty
)
1669 struct listnode
*node
;
1670 struct sr_prefix
*srp
;
1672 if (OspfSR
.enabled
) {
1673 vty_out(vty
, " segment-routing on\n");
1675 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1676 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1677 vty_out(vty
, " segment-routing global-block %u %u\n",
1678 OspfSR
.srgb
.lower_bound
,
1679 OspfSR
.srgb
.lower_bound
+
1680 OspfSR
.srgb
.range_size
- 1);
1682 if (OspfSR
.msd
!= 0)
1683 vty_out(vty
, " segment-routing node-msd %u\n",
1686 if (OspfSR
.self
!= NULL
) {
1687 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1690 " segment-routing prefix %s/%u "
1692 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1693 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1694 CHECK_FLAG(srp
->flags
,
1695 EXT_SUBTLV_PREFIX_SID_NPFLG
) ?
1696 " no-php-flag" : "");
1702 DEFUN(ospf_sr_enable
,
1704 "segment-routing on",
1706 "Enable Segment Routing\n")
1709 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1714 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1715 vty_out(vty
, "Segment Routing is only supported in default "
1717 return CMD_WARNING_CONFIG_FAILED
;
1720 if (IS_DEBUG_OSPF_EVENT
)
1721 zlog_debug("SR: Segment Routing: OFF -> ON");
1723 /* Start Segment Routing */
1724 OspfSR
.enabled
= true;
1725 if (!ospf_sr_start(ospf
)) {
1726 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1730 /* Set Router Information SR parameters */
1731 if (IS_DEBUG_OSPF_EVENT
)
1732 zlog_debug("SR: Activate SR for Router Information LSA");
1734 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1736 /* Update Ext LSA */
1737 if (IS_DEBUG_OSPF_EVENT
)
1738 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1740 ospf_ext_update_sr(true);
1745 DEFUN (no_ospf_sr_enable
,
1746 no_ospf_sr_enable_cmd
,
1747 "no segment-routing [on]",
1750 "Disable Segment Routing\n")
1753 if (!OspfSR
.enabled
)
1756 if (IS_DEBUG_OSPF_EVENT
)
1757 zlog_debug("SR: Segment Routing: ON -> OFF");
1759 /* Start by Disabling Extended Link & Prefix LSA */
1760 ospf_ext_update_sr(false);
1762 /* then, disable Router Information SR parameters */
1763 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1765 /* Finally, stop Segment Routing */
1767 OspfSR
.enabled
= false;
1772 static int ospf_sr_enabled(struct vty
*vty
)
1778 vty_out(vty
, "%% OSPF SR is not turned on\n");
1783 DEFUN (sr_sid_label_range
,
1784 sr_sid_label_range_cmd
,
1785 "segment-routing global-block (0-1048575) (0-1048575)",
1787 "Segment Routing Global Block label range\n"
1788 "Lower-bound range in decimal (0-1048575)\n"
1789 "Upper-bound range in decimal (0-1048575)\n")
1797 if (!ospf_sr_enabled(vty
))
1798 return CMD_WARNING_CONFIG_FAILED
;
1800 /* Get lower and upper bound */
1801 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1802 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1803 size
= upper
- lower
+ 1;
1805 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1807 "Range size cannot be less than 0 or more than %u\n",
1808 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1809 return CMD_WARNING_CONFIG_FAILED
;
1812 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1813 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1814 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1815 return CMD_WARNING_CONFIG_FAILED
;
1818 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1819 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1820 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1821 return CMD_WARNING_CONFIG_FAILED
;
1824 /* Check if values have changed */
1825 if ((OspfSR
.srgb
.range_size
== size
)
1826 && (OspfSR
.srgb
.lower_bound
== lower
))
1829 /* Set SID/Label range SRGB */
1830 OspfSR
.srgb
.range_size
= size
;
1831 OspfSR
.srgb
.lower_bound
= lower
;
1832 if (OspfSR
.self
!= NULL
) {
1833 OspfSR
.self
->srgb
.range_size
= size
;
1834 OspfSR
.self
->srgb
.lower_bound
= lower
;
1837 /* Set Router Information SR parameters */
1838 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1840 /* Update NHLFE entries */
1841 hash_iterate(OspfSR
.neighbors
,
1842 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1848 DEFUN (no_sr_sid_label_range
,
1849 no_sr_sid_label_range_cmd
,
1850 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1853 "Segment Routing Global Block label range\n"
1854 "Lower-bound range in decimal (0-1048575)\n"
1855 "Upper-bound range in decimal (0-1048575)\n")
1858 if (!ospf_sr_enabled(vty
))
1859 return CMD_WARNING_CONFIG_FAILED
;
1861 /* Revert to default SRGB value */
1862 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1863 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1864 if (OspfSR
.self
!= NULL
) {
1865 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1866 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1869 /* Set Router Information SR parameters */
1870 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1872 /* Update NHLFE entries */
1873 hash_iterate(OspfSR
.neighbors
,
1874 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1882 "segment-routing node-msd (1-16)",
1884 "Maximum Stack Depth for this router\n"
1885 "Maximum number of label that could be stack (1-16)\n")
1890 if (!ospf_sr_enabled(vty
))
1891 return CMD_WARNING_CONFIG_FAILED
;
1894 argv_find(argv
, argc
, "(1-16)", &idx
);
1895 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1896 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1897 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1899 return CMD_WARNING_CONFIG_FAILED
;
1902 /* Check if value has changed */
1903 if (OspfSR
.msd
== msd
)
1906 /* Set this router MSD */
1908 if (OspfSR
.self
!= NULL
)
1909 OspfSR
.self
->msd
= msd
;
1911 /* Set Router Information SR parameters */
1912 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1917 DEFUN (no_sr_node_msd
,
1919 "no segment-routing node-msd [(1-16)]",
1922 "Maximum Stack Depth for this router\n"
1923 "Maximum number of label that could be stack (1-16)\n")
1926 if (!ospf_sr_enabled(vty
))
1927 return CMD_WARNING_CONFIG_FAILED
;
1929 /* unset this router MSD */
1931 if (OspfSR
.self
!= NULL
)
1932 OspfSR
.self
->msd
= 0;
1934 /* Set Router Information SR parameters */
1935 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1940 DEFUN (sr_prefix_sid
,
1942 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1945 "IPv4 Prefix as A.B.C.D/M\n"
1946 "SID index for this prefix in decimal (0-65535)\n"
1947 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1948 "Don't request Penultimate Hop Popping (PHP)\n")
1953 struct listnode
*node
;
1954 struct sr_prefix
*srp
, *new;
1955 struct interface
*ifp
;
1957 if (!ospf_sr_enabled(vty
))
1958 return CMD_WARNING_CONFIG_FAILED
;
1960 /* Get network prefix */
1961 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1962 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1963 vty_out(vty
, "Invalid prefix format %s\n",
1965 return CMD_WARNING_CONFIG_FAILED
;
1968 /* Get & verify index value */
1969 argv_find(argv
, argc
, "(0-65535)", &idx
);
1970 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1971 if (index
> OspfSR
.srgb
.range_size
- 1) {
1972 vty_out(vty
, "Index %u must be lower than range size %u\n",
1973 index
, OspfSR
.srgb
.range_size
);
1974 return CMD_WARNING_CONFIG_FAILED
;
1977 /* check that the index is not already used */
1978 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1979 if (srp
->sid
== index
) {
1980 vty_out(vty
, "Index %u is already used\n", index
);
1981 return CMD_WARNING_CONFIG_FAILED
;
1985 /* Create new Extended Prefix to SRDB if not found */
1986 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1987 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1988 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1989 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1990 new->nhlfe
.prefv4
.family
= p
.family
;
1992 /* Set NO PHP flag if present and compute NHLFE */
1993 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1994 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1995 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1996 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1999 if (IS_DEBUG_OSPF_SR
)
2001 "SR (%s): Add new index %u to Prefix %s/%u",
2002 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
2003 new->nhlfe
.prefv4
.prefixlen
);
2005 /* Get Interface and check if it is a Loopback */
2006 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
2009 * Interface could be not yet available i.e. when this
2010 * command is in the configuration file, OSPF is not yet
2011 * ready. In this case, store the prefix SID for latter
2012 * update of this Extended Prefix
2014 listnode_add(OspfSR
.self
->ext_prefix
, new);
2016 "Interface for prefix %s/%u not found. Deferred LSA "
2017 "flooding", inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2021 if (!if_is_loopback(ifp
)) {
2022 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2023 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
2024 return CMD_WARNING_CONFIG_FAILED
;
2026 new->nhlfe
.ifindex
= ifp
->ifindex
;
2028 /* Search if this prefix already exist */
2029 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2030 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2031 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2037 /* Update or Add this new SR Prefix */
2039 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2040 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2041 listnode_add(OspfSR
.self
->ext_prefix
, new);
2043 listnode_add(OspfSR
.self
->ext_prefix
, new);
2044 add_sid_nhlfe(new->nhlfe
);
2047 /* Finally, update Extended Prefix LSA */
2048 new->instance
= ospf_ext_schedule_prefix_index(ifp
, new->sid
,
2049 &new->nhlfe
.prefv4
, new->flags
);
2050 if (new->instance
== 0) {
2051 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2052 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2059 DEFUN (no_sr_prefix_sid
,
2060 no_sr_prefix_sid_cmd
,
2061 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2065 "IPv4 Prefix as A.B.C.D/M\n"
2066 "SID index for this prefix in decimal (0-65535)\n"
2067 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2068 "Don't request Penultimate Hop Popping (PHP)\n")
2072 struct listnode
*node
;
2073 struct sr_prefix
*srp
;
2074 struct interface
*ifp
;
2078 /* Get network prefix */
2079 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2080 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2082 vty_out(vty
, "Invalid prefix format %s\n",
2084 return CMD_WARNING_CONFIG_FAILED
;
2087 /* check that the prefix is already set */
2088 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2089 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2090 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2096 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2098 return CMD_WARNING_CONFIG_FAILED
;
2102 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2104 vty_out(vty
, "interface for prefix %s not found.\n",
2106 return CMD_WARNING_CONFIG_FAILED
;
2109 /* Update Extended Prefix LSA */
2110 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2111 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2115 if (IS_DEBUG_OSPF_SR
)
2117 "SR (%s): Remove Prefix %s/%u with index %u",
2118 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2119 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2121 /* Delete NHLFE is NO-PHP is set */
2122 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2123 del_sid_nhlfe(srp
->nhlfe
);
2125 /* OK, all is clean, remove SRP from SRDB */
2126 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2127 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2134 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2135 struct sr_node
*srn
)
2138 struct listnode
*node
;
2139 struct sr_link
*srl
;
2140 struct sr_prefix
*srp
;
2141 struct interface
*itf
;
2145 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2146 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2153 json_node
= json_object_new_object();
2154 json_object_string_add(json_node
, "routerID",
2155 inet_ntoa(srn
->adv_router
));
2156 json_object_int_add(json_node
, "srgbSize",
2157 srn
->srgb
.range_size
);
2158 json_object_int_add(json_node
, "srgbLabel",
2159 srn
->srgb
.lower_bound
);
2160 json_algo
= json_object_new_array();
2161 json_object_object_add(json_node
, "algorithms", json_algo
);
2162 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2163 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2165 json_obj
= json_object_new_object();
2168 snprintf(tmp
, 2, "%u", i
);
2169 json_object_string_add(json_obj
, tmp
,
2170 srn
->algo
[i
] == SR_ALGORITHM_SPF
?
2172 json_object_array_add(json_algo
, json_obj
);
2175 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2177 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2178 vty_out(vty
, "\tSRGB (Size/Label): %u/%u",
2179 srn
->srgb
.range_size
, srn
->srgb
.lower_bound
);
2180 vty_out(vty
, "\tAlgorithm(s): %s",
2181 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2182 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2183 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2186 srn
->algo
[i
] == SR_ALGORITHM_SPF
?
2190 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2195 "\n\n Prefix or Link Label In Label Out "
2196 "Node or Adj. SID Interface Nexthop\n");
2198 "------------------ -------- --------- "
2199 "--------------------- --------- ---------------\n");
2201 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2202 snprintf(pref
, 19, "%s/%u",
2203 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2204 srp
->nhlfe
.prefv4
.prefixlen
);
2205 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2206 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2207 sprintf(label
, "pop");
2209 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2210 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2213 json_prefix
= json_object_new_array();
2214 json_object_object_add(json_node
,
2215 "extendedPrefix", json_prefix
);
2217 json_obj
= json_object_new_object();
2218 json_object_string_add(json_obj
, "prefix", pref
);
2219 json_object_int_add(json_obj
, "sid", srp
->sid
);
2220 json_object_int_add(json_obj
, "inputLabel",
2221 srp
->nhlfe
.label_in
);
2222 json_object_string_add(json_obj
, "outputLabel",
2224 json_object_string_add(json_obj
, "interface",
2225 itf
? itf
->name
: "-");
2226 json_object_string_add(json_obj
, "nexthop",
2227 inet_ntoa(srp
->nhlfe
.nexthop
));
2228 json_object_array_add(json_prefix
, json_obj
);
2230 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n",
2231 pref
, srp
->nhlfe
.label_in
, label
,
2232 sid
, itf
? itf
->name
: "-",
2233 inet_ntoa(srp
->nhlfe
.nexthop
));
2237 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2238 snprintf(pref
, 19, "%s/%u",
2239 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2240 srl
->nhlfe
[0].prefv4
.prefixlen
);
2241 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2242 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2243 sprintf(label
, "pop");
2245 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2246 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2249 json_link
= json_object_new_array();
2250 json_object_object_add(json_node
,
2251 "extendedLink", json_link
);
2254 json_obj
= json_object_new_object();
2255 json_object_string_add(json_obj
, "prefix", pref
);
2256 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2257 json_object_int_add(json_obj
, "inputLabel",
2258 srl
->nhlfe
[0].label_in
);
2259 json_object_string_add(json_obj
, "outputLabel",
2261 json_object_string_add(json_obj
, "interface",
2262 itf
? itf
->name
: "-");
2263 json_object_string_add(json_obj
, "nexthop",
2264 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2265 json_object_array_add(json_link
, json_obj
);
2267 json_obj
= json_object_new_object();
2268 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2269 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2270 sprintf(label
, "pop");
2272 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2273 json_object_string_add(json_obj
, "prefix", pref
);
2274 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2275 json_object_int_add(json_obj
, "inputLabel",
2276 srl
->nhlfe
[1].label_in
);
2277 json_object_string_add(json_obj
, "outputLabel",
2279 json_object_string_add(json_obj
, "interface",
2280 itf
? itf
->name
: "-");
2281 json_object_string_add(json_obj
, "nexthop",
2282 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2283 json_object_array_add(json_link
, json_obj
);
2285 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n",
2286 pref
, srl
->nhlfe
[0].label_in
,
2287 label
, sid
, itf
? itf
->name
: "-",
2288 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2289 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2290 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2291 sprintf(label
, "pop");
2293 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2294 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n",
2295 pref
, srl
->nhlfe
[1].label_in
,
2296 label
, sid
, itf
? itf
->name
: "-",
2297 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2301 json_object_array_add(json
, json_node
);
2306 static void show_vty_srdb(struct hash_backet
*backet
, void *args
)
2308 struct vty
*vty
= (struct vty
*)args
;
2309 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2311 show_sr_node(vty
, NULL
, srn
);
2314 static void show_json_srdb(struct hash_backet
*backet
, void *args
)
2316 struct json_object
*json
= (struct json_object
*)args
;
2317 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2319 show_sr_node(NULL
, json
, srn
);
2322 DEFUN (show_ip_opsf_srdb
,
2323 show_ip_ospf_srdb_cmd
,
2324 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2328 "Database summary\n"
2329 "Show Segment Routing Data Base\n"
2330 "Advertising SR node\n"
2331 "Advertising SR node ID (as an IP address)\n"
2332 "Self-originated SR node\n"
2337 struct sr_node
*srn
;
2338 u_char uj
= use_json(argc
, argv
);
2339 json_object
*json
= NULL
, *json_node_array
= NULL
;
2341 if (!OspfSR
.enabled
) {
2342 vty_out(vty
, "Segment Routing is disabled on this router\n");
2347 json
= json_object_new_object();
2348 json_node_array
= json_object_new_array();
2349 json_object_string_add(json
, "srdbID",
2350 inet_ntoa(OspfSR
.self
->adv_router
));
2351 json_object_object_add(json
, "srNodes", json_node_array
);
2354 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2355 inet_ntoa(OspfSR
.self
->adv_router
));
2358 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2360 show_sr_node(vty
, json_node_array
, srn
);
2362 vty_out(vty
, "%s\n",
2363 json_object_to_json_string_ext(json
,
2364 JSON_C_TO_STRING_PRETTY
));
2365 json_object_free(json
);
2370 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2371 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2373 "Specified Router ID %s is invalid\n",
2375 return CMD_WARNING_CONFIG_FAILED
;
2377 /* Get the SR Node from the SRDB */
2378 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2380 show_sr_node(vty
, json_node_array
, srn
);
2382 vty_out(vty
, "%s\n",
2383 json_object_to_json_string_ext(json
,
2384 JSON_C_TO_STRING_PRETTY
));
2385 json_object_free(json
);
2390 /* No parameters have been provided, Iterate through all the SRDB */
2394 (void (*)(struct hash_backet
*, void *))show_json_srdb
,
2395 (void *)json_node_array
);
2396 vty_out(vty
, "%s\n",
2397 json_object_to_json_string_ext(json
,
2398 JSON_C_TO_STRING_PRETTY
));
2399 json_object_free(json
);
2403 (void (*)(struct hash_backet
*, void *))show_vty_srdb
,
2409 /* Install new CLI commands */
2410 void ospf_sr_register_vty(void)
2412 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2414 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2415 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2416 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2417 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2418 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2419 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2420 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2421 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);