2 * This is an implementation of Segment Routing
3 * as per draft draft-ietf-ospf-segment-routing-extensions-24
5 * Module name: Segment Routing
7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "libospf.h" /* for ospf interface types */
44 #include "sockunion.h" /* for inet_aton() */
51 #include "ospf_errors.h"
53 #include "ospfd/ospfd.h"
54 #include "ospfd/ospf_interface.h"
55 #include "ospfd/ospf_ism.h"
56 #include "ospfd/ospf_asbr.h"
57 #include "ospfd/ospf_lsa.h"
58 #include "ospfd/ospf_lsdb.h"
59 #include "ospfd/ospf_neighbor.h"
60 #include "ospfd/ospf_nsm.h"
61 #include "ospfd/ospf_flood.h"
62 #include "ospfd/ospf_packet.h"
63 #include "ospfd/ospf_spf.h"
64 #include "ospfd/ospf_dump.h"
65 #include "ospfd/ospf_route.h"
66 #include "ospfd/ospf_ase.h"
67 #include "ospfd/ospf_sr.h"
68 #include "ospfd/ospf_ri.h"
69 #include "ospfd/ospf_ext.h"
70 #include "ospfd/ospf_zebra.h"
73 * Global variable to manage Segment Routing on this node.
74 * Note that all parameter values are stored in network byte order.
76 static struct ospf_sr_db OspfSR
;
77 static void ospf_sr_register_vty(void);
78 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
);
81 * Segment Routing Data Base functions
84 /* Hash function for Segment Routing entry */
85 static unsigned int sr_hash(void *p
)
87 const struct in_addr
*rid
= p
;
89 return jhash_1word(rid
->s_addr
, 0);
92 /* Compare 2 Router ID hash entries based on SR Node */
93 static int sr_cmp(const void *p1
, const void *p2
)
95 const struct sr_node
*srn
= p1
;
96 const struct in_addr
*rid
= p2
;
98 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
101 /* Functions to remove an SR Link */
102 static void del_sr_link(void *val
)
104 struct sr_link
*srl
= (struct sr_link
*)val
;
106 del_sid_nhlfe(srl
->nhlfe
[0]);
107 del_sid_nhlfe(srl
->nhlfe
[1]);
108 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
);
120 /* Allocate new Segment Routine node */
121 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
129 /* Allocate Segment Routing node memory */
130 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
132 /* Default Algorithm, SRGB and MSD */
133 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
134 new->algo
[i
] = SR_ALGORITHM_UNSET
;
136 new->srgb
.range_size
= 0;
137 new->srgb
.lower_bound
= 0;
140 /* Create Link, Prefix and Range TLVs list */
141 new->ext_link
= list_new();
142 new->ext_prefix
= list_new();
143 new->ext_link
->del
= del_sr_link
;
144 new->ext_prefix
->del
= del_sr_pref
;
146 IPV4_ADDR_COPY(&new->adv_router
, rid
);
147 new->neighbor
= NULL
;
150 if (IS_DEBUG_OSPF_SR
)
151 zlog_debug(" |- Created new SR node for %s",
152 inet_ntoa(new->adv_router
));
156 /* Delete Segment Routing node */
157 static void sr_node_del(struct sr_node
*srn
)
163 /* Clean Extended Link */
164 list_delete_and_null(&srn
->ext_link
);
166 /* Clean Prefix List */
167 list_delete_and_null(&srn
->ext_prefix
);
169 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
172 /* Get SR Node for a given nexthop */
173 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
174 struct in_addr nexthop
)
176 struct ospf_interface
*oi
= NULL
;
177 struct ospf_neighbor
*nbr
= NULL
;
178 struct listnode
*node
;
179 struct route_node
*rn
;
184 if (OspfSR
.neighbors
== NULL
)
187 if (IS_DEBUG_OSPF_SR
)
188 zlog_debug(" |- Search SR-Node for nexthop %s",
191 /* First, search neighbor Router ID for this nexthop */
193 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
194 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
196 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
208 if (IS_DEBUG_OSPF_SR
)
209 zlog_debug(" |- Found nexthop Router ID %s",
210 inet_ntoa(nbr
->router_id
));
211 /* Then, search SR Node */
212 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
218 * Segment Routing Initialization functions
221 /* Segment Routing starter function */
222 static int ospf_sr_start(struct ospf
*ospf
)
224 struct route_node
*rn
;
225 struct ospf_lsa
*lsa
;
229 if (IS_DEBUG_OSPF_SR
)
230 zlog_debug("SR (%s): Start Segment Routing", __func__
);
232 /* Initialize self SR Node */
233 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
234 (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_and_null() 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
)
617 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
618 stream_putc(s
, ZEBRA_LSP_SR
);
619 /* OSPF Segment Routing currently support only IPv4 */
620 stream_putl(s
, nhlfe
.prefv4
.family
);
621 stream_put_in_addr(s
, &nhlfe
.prefv4
.prefix
);
622 stream_putc(s
, nhlfe
.prefv4
.prefixlen
);
623 stream_put_in_addr(s
, &nhlfe
.nexthop
);
624 stream_putl(s
, nhlfe
.ifindex
);
625 stream_putc(s
, OSPF_SR_PRIORITY_DEFAULT
);
626 stream_putl(s
, nhlfe
.label_in
);
627 stream_putl(s
, nhlfe
.label_out
);
629 /* Put length at the first point of the stream. */
630 stream_putw_at(s
, 0, stream_get_endp(s
));
632 if (IS_DEBUG_OSPF_SR
)
633 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
634 cmd
== ZEBRA_MPLS_LABELS_ADD
? "Add" : "Delete",
635 nhlfe
.label_in
, nhlfe
.label_out
,
636 inet_ntoa(nhlfe
.prefv4
.prefix
),
637 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
639 return zclient_send_message(zclient
);
642 /* Request zebra to install/remove FEC in FIB */
643 static int ospf_zebra_send_mpls_ftn(int cmd
, struct sr_nhlfe nhlfe
)
645 struct zapi_route api
;
646 struct zapi_nexthop
*api_nh
;
648 /* Support only IPv4 */
649 if (nhlfe
.prefv4
.family
!= AF_INET
)
652 memset(&api
, 0, sizeof(api
));
653 api
.vrf_id
= VRF_DEFAULT
;
654 api
.type
= ZEBRA_ROUTE_OSPF
;
655 api
.safi
= SAFI_UNICAST
;
656 memcpy(&api
.prefix
, &nhlfe
.prefv4
, sizeof(struct prefix_ipv4
));
658 if (cmd
== ZEBRA_ROUTE_ADD
) {
660 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
661 api
.metric
= OSPF_SR_DEFAULT_METRIC
;
663 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
664 api_nh
= &api
.nexthops
[0];
665 IPV4_ADDR_COPY(&api_nh
->gate
.ipv4
, &nhlfe
.nexthop
);
666 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
667 api_nh
->ifindex
= nhlfe
.ifindex
;
669 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
670 api_nh
->labels
[0] = nhlfe
.label_out
;
671 api_nh
->label_num
= 1;
672 api_nh
->vrf_id
= VRF_DEFAULT
;
676 if (IS_DEBUG_OSPF_SR
)
677 zlog_debug(" |- %s FEC %u for %s/%u via %u",
678 cmd
== ZEBRA_ROUTE_ADD
? "Add" : "Delete",
679 nhlfe
.label_out
, inet_ntoa(nhlfe
.prefv4
.prefix
),
680 nhlfe
.prefv4
.prefixlen
, nhlfe
.ifindex
);
682 return zclient_route_send(cmd
, zclient
, &api
);
685 /* Add new NHLFE entry for SID */
686 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe
)
688 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
689 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
690 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
691 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD
, nhlfe
);
695 /* Remove NHLFE entry for SID */
696 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe
)
698 if ((nhlfe
.label_in
!= 0) && (nhlfe
.label_out
!= 0)) {
699 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
700 if (nhlfe
.label_out
!= MPLS_LABEL_IMPLICIT_NULL
)
701 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE
, nhlfe
);
705 /* Update NHLFE entry for SID */
706 static inline void update_sid_nhlfe(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
714 * Functions to parse and get Extended Link / Prefix
718 /* Extended Link SubTLVs Getter */
719 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
)
723 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
724 struct ext_subtlv_adj_sid
*adj_sid
;
725 struct ext_subtlv_lan_adj_sid
*lan_sid
;
726 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
728 struct tlv_header
*sub_tlvh
;
729 uint16_t length
= 0, sum
= 0, i
= 0;
731 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
733 /* Initialize TLV browsing */
734 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
735 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
736 + EXT_TLV_LINK_SIZE
);
737 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
738 switch (ntohs(sub_tlvh
->type
)) {
739 case EXT_SUBTLV_ADJ_SID
:
740 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
742 i
= CHECK_FLAG(adj_sid
->flags
,
743 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
746 srl
->flags
[i
] = adj_sid
->flags
;
747 if (CHECK_FLAG(adj_sid
->flags
,
748 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
749 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
751 srl
->sid
[i
] = ntohl(adj_sid
->value
);
752 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
754 case EXT_SUBTLV_LAN_ADJ_SID
:
755 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
756 srl
->type
= LAN_ADJ_SID
;
757 i
= CHECK_FLAG(lan_sid
->flags
,
758 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
761 srl
->flags
[i
] = lan_sid
->flags
;
762 if (CHECK_FLAG(lan_sid
->flags
,
763 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
764 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
766 srl
->sid
[i
] = ntohl(lan_sid
->value
);
767 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
768 &lan_sid
->neighbor_id
);
770 case EXT_SUBTLV_RMT_ITF_ADDR
:
771 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
772 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
773 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
778 sum
+= TLV_SIZE(sub_tlvh
);
781 IPV4_ADDR_COPY(&srl
->nhlfe
[0].prefv4
.prefix
, &link
->link_data
);
782 srl
->nhlfe
[0].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
783 srl
->nhlfe
[0].prefv4
.family
= AF_INET
;
784 apply_mask_ipv4(&srl
->nhlfe
[0].prefv4
);
785 IPV4_ADDR_COPY(&srl
->nhlfe
[1].prefv4
.prefix
, &link
->link_data
);
786 srl
->nhlfe
[1].prefv4
.prefixlen
= IPV4_MAX_PREFIXLEN
;
787 srl
->nhlfe
[1].prefv4
.family
= AF_INET
;
788 apply_mask_ipv4(&srl
->nhlfe
[1].prefv4
);
790 if (IS_DEBUG_OSPF_SR
) {
791 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
792 srl
->sid
[0], inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
793 srl
->nhlfe
[0].prefv4
.prefixlen
);
794 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
795 srl
->sid
[1], inet_ntoa(srl
->nhlfe
[1].prefv4
.prefix
),
796 srl
->nhlfe
[1].prefv4
.prefixlen
);
802 /* Extended Prefix SubTLVs Getter */
803 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
)
806 struct sr_prefix
*srp
;
807 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
808 struct ext_subtlv_prefix_sid
*psid
;
810 struct tlv_header
*sub_tlvh
;
811 uint16_t length
= 0, sum
= 0;
813 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
815 /* Initialize TLV browsing */
816 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
817 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
818 + EXT_TLV_PREFIX_SIZE
);
819 for (; sum
< length
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
820 switch (ntohs(sub_tlvh
->type
)) {
821 case EXT_SUBTLV_PREFIX_SID
:
822 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
823 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
824 flog_err(OSPF_ERR_SR_INVALID_ALGORITHM
,
825 "SR (%s): Unsupported Algorithm",
827 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
830 srp
->type
= PREF_SID
;
831 srp
->flags
= psid
->flags
;
832 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
833 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
835 srp
->sid
= ntohl(psid
->value
);
836 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
838 srp
->nhlfe
.prefv4
.prefixlen
= pref
->pref_length
;
839 srp
->nhlfe
.prefv4
.family
= AF_INET
;
840 apply_mask_ipv4(&srp
->nhlfe
.prefv4
);
845 sum
+= TLV_SIZE(sub_tlvh
);
848 if (IS_DEBUG_OSPF_SR
)
849 zlog_debug(" |- Found SID %u for prefix %s/%u", srp
->sid
,
850 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
851 srp
->nhlfe
.prefv4
.prefixlen
);
856 * Functions to manipulate Segment Routing Link & Prefix structures
859 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
860 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
862 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
863 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
864 && (srl1
->flags
[1] == srl2
->flags
[1]))
870 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
871 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
873 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
879 /* Update Segment Link of given Segment Routing Node */
880 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
883 struct listnode
*node
;
888 if ((srn
== NULL
) || (srl
== NULL
))
891 if (IS_DEBUG_OSPF_SR
)
892 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
894 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
895 if (!CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
896 || !CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
897 || !CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
))
900 /* Search for existing Segment Link */
901 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
902 if (lk
->instance
== srl
->instance
) {
907 if (IS_DEBUG_OSPF_SR
)
908 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
909 found
? "Update" : "Add",
910 GET_OPAQUE_ID(srl
->instance
),
911 inet_ntoa(srn
->adv_router
));
913 /* if not found, add new Segment Link and install NHLFE */
915 /* Complete SR-Link and add it to SR-Node list */
917 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
918 listnode_add(srn
->ext_link
, srl
);
919 /* Try to set MPLS table */
920 if (compute_link_nhlfe(srl
)) {
921 add_sid_nhlfe(srl
->nhlfe
[0]);
922 add_sid_nhlfe(srl
->nhlfe
[1]);
925 if (sr_link_cmp(lk
, srl
)) {
926 if (compute_link_nhlfe(srl
)) {
927 update_sid_nhlfe(lk
->nhlfe
[0], srl
->nhlfe
[0]);
928 update_sid_nhlfe(lk
->nhlfe
[1], srl
->nhlfe
[1]);
929 /* Replace Segment List */
930 listnode_delete(srn
->ext_link
, lk
);
931 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
933 IPV4_ADDR_COPY(&srl
->adv_router
,
935 listnode_add(srn
->ext_link
, srl
);
937 /* New NHLFE was not found.
938 * Just free the SR Link
940 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
944 * This is just an LSA refresh.
945 * Stop processing and free SR Link
947 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
952 /* Update Segment Prefix of given Segment Routing Node */
953 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
956 struct listnode
*node
;
957 struct sr_prefix
*pref
;
961 if (srn
== NULL
|| srp
== NULL
)
964 if (IS_DEBUG_OSPF_SR
)
965 zlog_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
967 /* Process only Global Prefix SID */
968 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
971 /* Search for existing Segment Prefix */
972 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
973 if (pref
->instance
== srp
->instance
) {
978 if (IS_DEBUG_OSPF_SR
)
979 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
980 found
? "Update" : "Add",
981 GET_OPAQUE_ID(srp
->instance
),
982 inet_ntoa(srn
->adv_router
));
984 /* if not found, add new Segment Prefix and install NHLFE */
986 /* Complete SR-Prefix and add it to SR-Node list */
988 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
989 listnode_add(srn
->ext_prefix
, srp
);
990 /* Try to set MPLS table */
991 if (compute_prefix_nhlfe(srp
) == 1)
992 add_sid_nhlfe(srp
->nhlfe
);
994 if (sr_prefix_cmp(pref
, srp
)) {
995 if (compute_prefix_nhlfe(srp
) == 1) {
996 update_sid_nhlfe(pref
->nhlfe
, srp
->nhlfe
);
997 /* Replace Segment Prefix */
998 listnode_delete(srn
->ext_prefix
, pref
);
999 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1001 IPV4_ADDR_COPY(&srp
->adv_router
,
1003 listnode_add(srn
->ext_prefix
, srp
);
1005 /* New NHLFE was not found.
1006 * Just free the SR Prefix
1008 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1011 /* This is just an LSA refresh.
1012 * Stop processing and free SR Prefix
1014 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1020 * When change the FRR Self SRGB, update the NHLFE Input Label
1021 * for all Extended Prefix with SID index through hash_iterate()
1023 static void update_in_nhlfe(struct hash_backet
*backet
, void *args
)
1025 struct listnode
*node
;
1026 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1027 struct sr_prefix
*srp
;
1028 struct sr_nhlfe
new;
1030 /* Process Every Extended Prefix for this SR-Node */
1031 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1032 /* Process Self SRN only if NO-PHP is requested */
1033 if ((srn
== OspfSR
.self
)
1034 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1037 /* Process only SID Index */
1038 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1041 /* OK. Compute new NHLFE */
1042 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1043 new.label_in
= index2label(srp
->sid
, OspfSR
.srgb
);
1044 /* Update MPLS LFIB */
1045 update_sid_nhlfe(srp
->nhlfe
, new);
1046 /* Finally update Input Label */
1047 srp
->nhlfe
.label_in
= new.label_in
;
1052 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1053 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1055 static void update_out_nhlfe(struct hash_backet
*backet
, void *args
)
1057 struct listnode
*node
;
1058 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1059 struct sr_node
*srnext
= (struct sr_node
*)args
;
1060 struct sr_prefix
*srp
;
1061 struct sr_nhlfe
new;
1063 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1064 /* Process only SID Index for next hop without PHP */
1065 if ((srp
->nexthop
== NULL
)
1066 && (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)))
1068 memcpy(&new, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1069 new.label_out
= index2label(srp
->sid
, srnext
->srgb
);
1070 update_sid_nhlfe(srp
->nhlfe
, new);
1071 srp
->nhlfe
.label_out
= new.label_out
;
1076 * Following functions are call when new Segment Routing LSA are received
1077 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1078 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1079 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1082 /* Update Segment Routing from Router Information LSA */
1083 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1085 struct sr_node
*srn
;
1086 struct tlv_header
*tlvh
;
1087 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1088 struct ri_sr_tlv_sid_label_range
*ri_srgb
;
1089 struct ri_sr_tlv_sr_algorithm
*algo
;
1090 struct sr_srgb srgb
;
1091 uint16_t length
= 0, sum
= 0;
1093 if (IS_DEBUG_OSPF_SR
)
1095 "SR (%s): Process Router "
1096 "Information LSA 4.0.0.%u from %s",
1097 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1098 inet_ntoa(lsah
->adv_router
));
1101 if (IS_LSA_SELF(lsa
))
1104 if (OspfSR
.neighbors
== NULL
) {
1105 flog_err(OSPF_ERR_SR_INVALID_DB
,
1106 "SR (%s): Abort! no valid SR DataBase", __func__
);
1110 /* Get SR Node in hash table from Router ID */
1111 srn
= hash_get(OspfSR
.neighbors
, (void *)&(lsah
->adv_router
),
1112 (void *)sr_node_new
);
1116 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1117 "SR (%s): Abort! can't create SR node in hash table",
1122 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1123 flog_err(OSPF_ERR_SR_INVALID_LSA_ID
,
1124 "SR (%s): Abort! Wrong "
1125 "LSA ID 4.0.0.%u for SR node %s/%u",
1126 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1127 inet_ntoa(lsah
->adv_router
), srn
->instance
);
1131 /* Collect Router Information Sub TLVs */
1132 /* Initialize TLV browsing */
1133 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1134 srgb
.range_size
= 0;
1135 srgb
.lower_bound
= 0;
1137 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1138 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1139 switch (ntohs(tlvh
->type
)) {
1140 case RI_SR_TLV_SR_ALGORITHM
:
1141 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1144 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1145 srn
->algo
[i
] = algo
->value
[0];
1146 for (; i
< ALGORITHM_COUNT
; i
++)
1147 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1148 sum
+= TLV_SIZE(tlvh
);
1150 case RI_SR_TLV_SID_LABEL_RANGE
:
1151 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1152 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1154 GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1155 sum
+= TLV_SIZE(tlvh
);
1157 case RI_SR_TLV_NODE_MSD
:
1158 srn
->msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1159 sum
+= TLV_SIZE(tlvh
);
1162 sum
+= TLV_SIZE(tlvh
);
1167 /* Check that we collect mandatory parameters */
1168 if (srn
->algo
[0] == SR_ALGORITHM_UNSET
|| srgb
.range_size
== 0
1169 || srgb
.lower_bound
== 0) {
1170 zlog_warn("SR (%s): Missing mandatory parameters. Abort!",
1172 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1173 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
1177 /* Check if it is a new SR Node or not */
1178 if (srn
->instance
== 0) {
1180 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1182 srn
->srgb
.range_size
= srgb
.range_size
;
1183 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1186 /* Check if SRGB has changed */
1187 if ((srn
->srgb
.range_size
!= srgb
.range_size
)
1188 || (srn
->srgb
.lower_bound
!= srgb
.lower_bound
)) {
1189 srn
->srgb
.range_size
= srgb
.range_size
;
1190 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1191 /* Update NHLFE if it is a neighbor SR node */
1192 if (srn
->neighbor
== OspfSR
.self
)
1193 hash_iterate(OspfSR
.neighbors
,
1194 (void (*)(struct hash_backet
*,
1195 void *))update_out_nhlfe
,
1201 * Delete SR Node entry in hash table information corresponding to an expired
1202 * Router Information LSA
1204 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1206 struct sr_node
*srn
;
1207 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1209 if (IS_DEBUG_OSPF_SR
)
1210 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1211 __func__
, inet_ntoa(lsah
->adv_router
),
1212 GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1215 if (OspfSR
.neighbors
== NULL
) {
1216 flog_err(OSPF_ERR_SR_INVALID_DB
,
1217 "SR (%s): Abort! no valid SR Data Base", __func__
);
1221 /* Release Router ID entry in SRDB hash table */
1222 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1226 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1227 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1228 __func__
, inet_ntoa(lsah
->adv_router
));
1232 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1234 OSPF_ERR_SR_INVALID_LSA_ID
,
1235 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1236 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1237 inet_ntoa(lsah
->adv_router
));
1241 /* Remove SR node */
1245 /* Update Segment Routing from Extended Link LSA */
1246 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1248 struct sr_node
*srn
;
1249 struct tlv_header
*tlvh
;
1250 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1251 struct sr_link
*srl
;
1253 uint16_t length
, sum
;
1255 if (IS_DEBUG_OSPF_SR
)
1257 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1258 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1259 inet_ntoa(lsah
->adv_router
));
1262 if (OspfSR
.neighbors
== NULL
) {
1263 flog_err(OSPF_ERR_SR_INVALID_DB
,
1264 "SR (%s): Abort! no valid SR DataBase", __func__
);
1268 /* Get SR Node in hash table from Router ID */
1269 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1270 (void *)&(lsah
->adv_router
),
1271 (void *)sr_node_new
);
1275 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1276 "SR (%s): Abort! can't create SR node in hash table",
1281 /* Initialize TLV browsing */
1282 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1284 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1285 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1286 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1287 /* Got Extended Link information */
1288 srl
= get_ext_link_sid(tlvh
);
1289 /* Update SID if not null */
1291 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1292 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1295 sum
+= TLV_SIZE(tlvh
);
1299 /* Delete Segment Routing from Extended Link LSA */
1300 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1302 struct listnode
*node
;
1303 struct sr_link
*srl
;
1304 struct sr_node
*srn
;
1305 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1306 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1308 if (IS_DEBUG_OSPF_SR
)
1309 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1310 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1311 inet_ntoa(lsah
->adv_router
));
1314 if (OspfSR
.neighbors
== NULL
) {
1315 flog_err(OSPF_ERR_SR_INVALID_DB
,
1316 "SR (%s): Abort! no valid SR DataBase", __func__
);
1320 /* Search SR Node in hash table from Router ID */
1321 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1322 (void *)&(lsah
->adv_router
));
1325 * SR-Node may be NULL if it has been remove previously when
1326 * processing Router Information LSA deletion
1329 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1330 __func__
, inet_ntoa(lsah
->adv_router
));
1334 /* Search for corresponding Segment Link */
1335 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1336 if (srl
->instance
== instance
)
1339 /* Remove Segment Link if found */
1340 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1341 del_sid_nhlfe(srl
->nhlfe
[0]);
1342 del_sid_nhlfe(srl
->nhlfe
[1]);
1343 listnode_delete(srn
->ext_link
, srl
);
1344 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1347 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1349 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1350 inet_ntoa(lsah
->adv_router
));
1354 /* Update Segment Routing from Extended Prefix LSA */
1355 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1357 struct sr_node
*srn
;
1358 struct tlv_header
*tlvh
;
1359 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1360 struct sr_prefix
*srp
;
1362 uint16_t length
, sum
;
1364 if (IS_DEBUG_OSPF_SR
)
1366 "SR (%s): Process Extended Prefix LSA "
1368 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1369 inet_ntoa(lsah
->adv_router
));
1372 if (OspfSR
.neighbors
== NULL
) {
1373 flog_err(OSPF_ERR_SR_INVALID_DB
,
1374 "SR (%s): Abort! no valid SR DataBase", __func__
);
1378 /* Get SR Node in hash table from Router ID */
1379 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1380 (void *)&(lsah
->adv_router
),
1381 (void *)sr_node_new
);
1385 flog_err(OSPF_ERR_SR_NODE_CREATE
,
1386 "SR (%s): Abort! can't create SR node in hash table",
1391 /* Initialize TLV browsing */
1392 length
= ntohs(lsah
->length
) - OSPF_LSA_HEADER_SIZE
;
1394 for (tlvh
= TLV_HDR_TOP(lsah
); sum
< length
;
1395 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1396 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1397 /* Got Extended Link information */
1398 srp
= get_ext_prefix_sid(tlvh
);
1399 /* Update SID if not null */
1401 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1402 update_ext_prefix_sid(srn
, srp
);
1405 sum
+= TLV_SIZE(tlvh
);
1409 /* Delete Segment Routing from Extended Prefix LSA */
1410 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1412 struct listnode
*node
;
1413 struct sr_prefix
*srp
;
1414 struct sr_node
*srn
;
1415 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1416 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1418 if (IS_DEBUG_OSPF_SR
)
1420 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1421 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1422 inet_ntoa(lsah
->adv_router
));
1425 if (OspfSR
.neighbors
== NULL
) {
1426 flog_err(OSPF_ERR_SR_INVALID_DB
,
1427 "SR (%s): Abort! no valid SR DataBase", __func__
);
1431 /* Search SR Node in hash table from Router ID */
1432 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1433 (void *)&(lsah
->adv_router
));
1436 * SR-Node may be NULL if it has been remove previously when
1437 * processing Router Information LSA deletion
1440 zlog_warn("SR (%s): Stop! no entry in SRDB for SR Node %s",
1441 __func__
, inet_ntoa(lsah
->adv_router
));
1445 /* Search for corresponding Segment Link */
1446 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1447 if (srp
->instance
== instance
)
1450 /* Remove Segment Link if found */
1451 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1452 del_sid_nhlfe(srp
->nhlfe
);
1453 listnode_delete(srn
->ext_link
, srp
);
1454 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1457 "SR (%s): Didn't found corresponding SR Prefix "
1458 "7.0.0.%u for SR Node %s",
1459 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1460 inet_ntoa(lsah
->adv_router
));
1464 /* Get Label for Extended Link SID */
1465 /* TODO: To be replace by Zebra Label Manager */
1466 uint32_t get_ext_link_label_value(void)
1468 static uint32_t label
= ADJ_SID_MIN
- 1;
1470 if (label
< ADJ_SID_MAX
)
1477 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1478 * complete initial CLI command at startutp.
1480 * @param ifp - Loopback interface
1481 * @param pref - Prefix address of this interface
1485 void ospf_sr_update_prefix(struct interface
*ifp
, struct prefix
*p
)
1487 struct listnode
*node
;
1488 struct sr_prefix
*srp
;
1491 if ((ifp
== NULL
) || (p
== NULL
))
1495 * Search if there is a Segment Prefix that correspond to this
1496 * interface or prefix, and update it if found
1498 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1499 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1500 || ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
,
1502 && (srp
->nhlfe
.prefv4
.prefixlen
== p
->prefixlen
))) {
1504 /* Update Interface & Prefix info */
1505 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1506 IPV4_ADDR_COPY(&srp
->nhlfe
.prefv4
.prefix
,
1508 srp
->nhlfe
.prefv4
.prefixlen
= p
->prefixlen
;
1509 srp
->nhlfe
.prefv4
.family
= p
->family
;
1510 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1512 /* OK. Let's Schedule Extended Prefix LSA */
1513 srp
->instance
= ospf_ext_schedule_prefix_index(
1514 ifp
, srp
->sid
, &srp
->nhlfe
.prefv4
, srp
->flags
);
1516 /* Install NHLFE if NO-PHP is requested */
1517 if (CHECK_FLAG(srp
->flags
,
1518 EXT_SUBTLV_PREFIX_SID_NPFLG
)) {
1519 srp
->nhlfe
.label_in
= index2label(
1520 srp
->sid
, OspfSR
.self
->srgb
);
1521 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1522 add_sid_nhlfe(srp
->nhlfe
);
1529 * Following functions are used to update MPLS LFIB after a SPF run
1532 static void ospf_sr_nhlfe_update(struct hash_backet
*backet
, void *args
)
1535 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
1536 struct listnode
*node
;
1537 struct sr_prefix
*srp
;
1538 struct sr_nhlfe old
;
1541 if (IS_DEBUG_OSPF_SR
)
1542 zlog_debug(" |- Update Prefix for SR Node %s",
1543 inet_ntoa(srn
->adv_router
));
1545 /* Skip Self SR Node */
1546 if (srn
== OspfSR
.self
)
1549 /* Update Extended Prefix */
1550 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1552 /* Backup current NHLFE */
1553 memcpy(&old
, &srp
->nhlfe
, sizeof(struct sr_nhlfe
));
1555 /* Compute the new NHLFE */
1556 rc
= compute_prefix_nhlfe(srp
);
1558 /* Check computation result */
1560 /* next hop is not know, remove old NHLFE to avoid loop */
1562 del_sid_nhlfe(srp
->nhlfe
);
1564 /* next hop has not changed, skip it */
1567 /* there is a new next hop, update NHLFE */
1569 update_sid_nhlfe(old
, srp
->nhlfe
);
1577 static int ospf_sr_update_schedule(struct thread
*t
)
1581 struct timeval start_time
, stop_time
;
1583 ospf
= THREAD_ARG(t
);
1584 ospf
->t_sr_update
= NULL
;
1589 monotime(&start_time
);
1591 if (IS_DEBUG_OSPF_SR
)
1592 zlog_debug("SR (%s): Start SPF update", __func__
);
1594 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
1595 void *))ospf_sr_nhlfe_update
,
1598 monotime(&stop_time
);
1600 if (IS_DEBUG_OSPF_SR
)
1601 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld\n",
1603 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
1604 + (stop_time
.tv_usec
- start_time
.tv_usec
));
1606 OspfSR
.update
= false;
1610 #define OSPF_SR_UPDATE_INTERVAL 1
1612 void ospf_sr_update_timer_add(struct ospf
*ospf
)
1618 /* Check if an update is not alreday engage */
1622 OspfSR
.update
= true;
1624 thread_add_timer(master
, ospf_sr_update_schedule
, ospf
,
1625 OSPF_SR_UPDATE_INTERVAL
, &ospf
->t_sr_update
);
1629 * --------------------------------------
1630 * Followings are vty command functions.
1631 * --------------------------------------
1635 * Segment Routing Router configuration
1637 * Must be centralize as it concerns both Extended Link/Prefix LSA
1638 * and Router Information LSA. Choose to call it from Extended Prefix
1639 * write_config() call back.
1641 * @param vty VTY output
1645 void ospf_sr_config_write_router(struct vty
*vty
)
1647 struct listnode
*node
;
1648 struct sr_prefix
*srp
;
1650 if (OspfSR
.enabled
) {
1651 vty_out(vty
, " segment-routing on\n");
1653 if ((OspfSR
.srgb
.lower_bound
!= MPLS_DEFAULT_MIN_SRGB_LABEL
)
1654 || (OspfSR
.srgb
.range_size
!= MPLS_DEFAULT_MAX_SRGB_SIZE
)) {
1655 vty_out(vty
, " segment-routing global-block %u %u\n",
1656 OspfSR
.srgb
.lower_bound
,
1657 OspfSR
.srgb
.lower_bound
+ OspfSR
.srgb
.range_size
1660 if (OspfSR
.msd
!= 0)
1661 vty_out(vty
, " segment-routing node-msd %u\n",
1664 if (OspfSR
.self
!= NULL
) {
1665 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
,
1668 " segment-routing prefix %s/%u "
1670 inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
1671 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
,
1672 CHECK_FLAG(srp
->flags
,
1673 EXT_SUBTLV_PREFIX_SID_NPFLG
)
1681 DEFUN(ospf_sr_enable
,
1683 "segment-routing on",
1685 "Enable Segment Routing\n")
1688 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
1693 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
1695 "Segment Routing is only supported in default "
1697 return CMD_WARNING_CONFIG_FAILED
;
1700 if (IS_DEBUG_OSPF_EVENT
)
1701 zlog_debug("SR: Segment Routing: OFF -> ON");
1703 /* Start Segment Routing */
1704 OspfSR
.enabled
= true;
1705 if (!ospf_sr_start(ospf
)) {
1706 zlog_warn("SR: Unable to start Segment Routing. Abort!");
1710 /* Set Router Information SR parameters */
1711 if (IS_DEBUG_OSPF_EVENT
)
1712 zlog_debug("SR: Activate SR for Router Information LSA");
1714 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1716 /* Update Ext LSA */
1717 if (IS_DEBUG_OSPF_EVENT
)
1718 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1720 ospf_ext_update_sr(true);
1725 DEFUN (no_ospf_sr_enable
,
1726 no_ospf_sr_enable_cmd
,
1727 "no segment-routing [on]",
1730 "Disable Segment Routing\n")
1733 if (!OspfSR
.enabled
)
1736 if (IS_DEBUG_OSPF_EVENT
)
1737 zlog_debug("SR: Segment Routing: ON -> OFF");
1739 /* Start by Disabling Extended Link & Prefix LSA */
1740 ospf_ext_update_sr(false);
1742 /* then, disable Router Information SR parameters */
1743 ospf_router_info_update_sr(false, OspfSR
.srgb
, OspfSR
.msd
);
1745 /* Finally, stop Segment Routing */
1747 OspfSR
.enabled
= false;
1752 static int ospf_sr_enabled(struct vty
*vty
)
1758 vty_out(vty
, "%% OSPF SR is not turned on\n");
1763 DEFUN (sr_sid_label_range
,
1764 sr_sid_label_range_cmd
,
1765 "segment-routing global-block (0-1048575) (0-1048575)",
1767 "Segment Routing Global Block label range\n"
1768 "Lower-bound range in decimal (0-1048575)\n"
1769 "Upper-bound range in decimal (0-1048575)\n")
1777 if (!ospf_sr_enabled(vty
))
1778 return CMD_WARNING_CONFIG_FAILED
;
1780 /* Get lower and upper bound */
1781 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
1782 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
1783 size
= upper
- lower
+ 1;
1785 if (size
> MPLS_DEFAULT_MAX_SRGB_SIZE
|| size
<= 0) {
1787 "Range size cannot be less than 0 or more than %u\n",
1788 MPLS_DEFAULT_MAX_SRGB_SIZE
);
1789 return CMD_WARNING_CONFIG_FAILED
;
1792 if (upper
> MPLS_DEFAULT_MAX_SRGB_LABEL
) {
1793 vty_out(vty
, "Upper-bound cannot exceed %u\n",
1794 MPLS_DEFAULT_MAX_SRGB_LABEL
);
1795 return CMD_WARNING_CONFIG_FAILED
;
1798 if (upper
< MPLS_DEFAULT_MIN_SRGB_LABEL
) {
1799 vty_out(vty
, "Upper-bound cannot be lower than %u\n",
1800 MPLS_DEFAULT_MIN_SRGB_LABEL
);
1801 return CMD_WARNING_CONFIG_FAILED
;
1804 /* Check if values have changed */
1805 if ((OspfSR
.srgb
.range_size
== size
)
1806 && (OspfSR
.srgb
.lower_bound
== lower
))
1809 /* Set SID/Label range SRGB */
1810 OspfSR
.srgb
.range_size
= size
;
1811 OspfSR
.srgb
.lower_bound
= lower
;
1812 if (OspfSR
.self
!= NULL
) {
1813 OspfSR
.self
->srgb
.range_size
= size
;
1814 OspfSR
.self
->srgb
.lower_bound
= lower
;
1817 /* Set Router Information SR parameters */
1818 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1820 /* Update NHLFE entries */
1821 hash_iterate(OspfSR
.neighbors
,
1822 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1828 DEFUN (no_sr_sid_label_range
,
1829 no_sr_sid_label_range_cmd
,
1830 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1833 "Segment Routing Global Block label range\n"
1834 "Lower-bound range in decimal (0-1048575)\n"
1835 "Upper-bound range in decimal (0-1048575)\n")
1838 if (!ospf_sr_enabled(vty
))
1839 return CMD_WARNING_CONFIG_FAILED
;
1841 /* Revert to default SRGB value */
1842 OspfSR
.srgb
.range_size
= MPLS_DEFAULT_MIN_SRGB_SIZE
;
1843 OspfSR
.srgb
.lower_bound
= MPLS_DEFAULT_MIN_SRGB_LABEL
;
1844 if (OspfSR
.self
!= NULL
) {
1845 OspfSR
.self
->srgb
.range_size
= OspfSR
.srgb
.range_size
;
1846 OspfSR
.self
->srgb
.lower_bound
= OspfSR
.srgb
.lower_bound
;
1849 /* Set Router Information SR parameters */
1850 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1852 /* Update NHLFE entries */
1853 hash_iterate(OspfSR
.neighbors
,
1854 (void (*)(struct hash_backet
*, void *))update_in_nhlfe
,
1862 "segment-routing node-msd (1-16)",
1864 "Maximum Stack Depth for this router\n"
1865 "Maximum number of label that could be stack (1-16)\n")
1870 if (!ospf_sr_enabled(vty
))
1871 return CMD_WARNING_CONFIG_FAILED
;
1874 argv_find(argv
, argc
, "(1-16)", &idx
);
1875 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1876 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
1877 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
1879 return CMD_WARNING_CONFIG_FAILED
;
1882 /* Check if value has changed */
1883 if (OspfSR
.msd
== msd
)
1886 /* Set this router MSD */
1888 if (OspfSR
.self
!= NULL
)
1889 OspfSR
.self
->msd
= msd
;
1891 /* Set Router Information SR parameters */
1892 ospf_router_info_update_sr(true, OspfSR
.srgb
, OspfSR
.msd
);
1897 DEFUN (no_sr_node_msd
,
1899 "no segment-routing node-msd [(1-16)]",
1902 "Maximum Stack Depth for this router\n"
1903 "Maximum number of label that could be stack (1-16)\n")
1906 if (!ospf_sr_enabled(vty
))
1907 return CMD_WARNING_CONFIG_FAILED
;
1909 /* unset this router MSD */
1911 if (OspfSR
.self
!= NULL
)
1912 OspfSR
.self
->msd
= 0;
1914 /* Set Router Information SR parameters */
1915 ospf_router_info_update_sr(true, OspfSR
.srgb
, 0);
1920 DEFUN (sr_prefix_sid
,
1922 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1925 "IPv4 Prefix as A.B.C.D/M\n"
1926 "SID index for this prefix in decimal (0-65535)\n"
1927 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1928 "Don't request Penultimate Hop Popping (PHP)\n")
1933 struct listnode
*node
;
1934 struct sr_prefix
*srp
, *new;
1935 struct interface
*ifp
;
1937 if (!ospf_sr_enabled(vty
))
1938 return CMD_WARNING_CONFIG_FAILED
;
1940 /* Get network prefix */
1941 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
1942 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
1943 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
1944 return CMD_WARNING_CONFIG_FAILED
;
1947 /* Get & verify index value */
1948 argv_find(argv
, argc
, "(0-65535)", &idx
);
1949 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
1950 if (index
> OspfSR
.srgb
.range_size
- 1) {
1951 vty_out(vty
, "Index %u must be lower than range size %u\n",
1952 index
, OspfSR
.srgb
.range_size
);
1953 return CMD_WARNING_CONFIG_FAILED
;
1956 /* check that the index is not already used */
1957 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1958 if (srp
->sid
== index
) {
1959 vty_out(vty
, "Index %u is already used\n", index
);
1960 return CMD_WARNING_CONFIG_FAILED
;
1964 /* Create new Extended Prefix to SRDB if not found */
1965 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1966 IPV4_ADDR_COPY(&new->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
);
1967 IPV4_ADDR_COPY(&new->nhlfe
.nexthop
, &p
.u
.prefix4
);
1968 new->nhlfe
.prefv4
.prefixlen
= p
.prefixlen
;
1969 new->nhlfe
.prefv4
.family
= p
.family
;
1971 /* Set NO PHP flag if present and compute NHLFE */
1972 if (argv_find(argv
, argc
, "no-php-flag", &idx
)) {
1973 SET_FLAG(new->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
);
1974 new->nhlfe
.label_in
= index2label(new->sid
, OspfSR
.self
->srgb
);
1975 new->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1978 if (IS_DEBUG_OSPF_SR
)
1979 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1980 __func__
, index
, inet_ntoa(new->nhlfe
.prefv4
.prefix
),
1981 new->nhlfe
.prefv4
.prefixlen
);
1983 /* Get Interface and check if it is a Loopback */
1984 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
1987 * Interface could be not yet available i.e. when this
1988 * command is in the configuration file, OSPF is not yet
1989 * ready. In this case, store the prefix SID for latter
1990 * update of this Extended Prefix
1992 listnode_add(OspfSR
.self
->ext_prefix
, new);
1994 "Interface for prefix %s/%u not found. Deferred LSA "
1996 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2000 if (!if_is_loopback(ifp
)) {
2001 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2002 XFREE(MTYPE_OSPF_SR_PARAMS
, new);
2003 return CMD_WARNING_CONFIG_FAILED
;
2005 new->nhlfe
.ifindex
= ifp
->ifindex
;
2007 /* Search if this prefix already exist */
2008 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2009 if ((IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2010 && srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
))
2016 /* Update or Add this new SR Prefix */
2018 update_sid_nhlfe(srp
->nhlfe
, new->nhlfe
);
2019 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2020 listnode_add(OspfSR
.self
->ext_prefix
, new);
2022 listnode_add(OspfSR
.self
->ext_prefix
, new);
2023 add_sid_nhlfe(new->nhlfe
);
2026 /* Finally, update Extended Prefix LSA */
2027 new->instance
= ospf_ext_schedule_prefix_index(
2028 ifp
, new->sid
, &new->nhlfe
.prefv4
, new->flags
);
2029 if (new->instance
== 0) {
2030 vty_out(vty
, "Unable to set index %u for prefix %s/%u\n", index
,
2031 inet_ntoa(p
.u
.prefix4
), p
.prefixlen
);
2038 DEFUN (no_sr_prefix_sid
,
2039 no_sr_prefix_sid_cmd
,
2040 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2044 "IPv4 Prefix as A.B.C.D/M\n"
2045 "SID index for this prefix in decimal (0-65535)\n"
2046 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2047 "Don't request Penultimate Hop Popping (PHP)\n")
2051 struct listnode
*node
;
2052 struct sr_prefix
*srp
;
2053 struct interface
*ifp
;
2057 /* Get network prefix */
2058 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2059 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2061 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2062 return CMD_WARNING_CONFIG_FAILED
;
2065 /* check that the prefix is already set */
2066 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2067 if (IPV4_ADDR_SAME(&srp
->nhlfe
.prefv4
.prefix
, &p
.u
.prefix4
)
2068 && (srp
->nhlfe
.prefv4
.prefixlen
== p
.prefixlen
)) {
2074 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2076 return CMD_WARNING_CONFIG_FAILED
;
2080 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2082 vty_out(vty
, "interface for prefix %s not found.\n",
2084 return CMD_WARNING_CONFIG_FAILED
;
2087 /* Update Extended Prefix LSA */
2088 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2089 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2093 if (IS_DEBUG_OSPF_SR
)
2094 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2095 __func__
, inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2096 srp
->nhlfe
.prefv4
.prefixlen
, srp
->sid
);
2098 /* Delete NHLFE is NO-PHP is set */
2099 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
2100 del_sid_nhlfe(srp
->nhlfe
);
2102 /* OK, all is clean, remove SRP from SRDB */
2103 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2104 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2110 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2111 struct sr_node
*srn
)
2114 struct listnode
*node
;
2115 struct sr_link
*srl
;
2116 struct sr_prefix
*srp
;
2117 struct interface
*itf
;
2121 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2122 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2129 json_node
= json_object_new_object();
2130 json_object_string_add(json_node
, "routerID",
2131 inet_ntoa(srn
->adv_router
));
2132 json_object_int_add(json_node
, "srgbSize",
2133 srn
->srgb
.range_size
);
2134 json_object_int_add(json_node
, "srgbLabel",
2135 srn
->srgb
.lower_bound
);
2136 json_algo
= json_object_new_array();
2137 json_object_object_add(json_node
, "algorithms", json_algo
);
2138 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2139 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2141 json_obj
= json_object_new_object();
2144 snprintf(tmp
, 2, "%u", i
);
2145 json_object_string_add(json_obj
, tmp
,
2146 srn
->algo
[i
] == SR_ALGORITHM_SPF
2149 json_object_array_add(json_algo
, json_obj
);
2152 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2154 vty_out(vty
, "SR-Node: %s", inet_ntoa(srn
->adv_router
));
2155 vty_out(vty
, "\tSRGB (Size/Label): %u/%u", srn
->srgb
.range_size
,
2156 srn
->srgb
.lower_bound
);
2157 vty_out(vty
, "\tAlgorithm(s): %s",
2158 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2159 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2160 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2163 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2167 vty_out(vty
, "\tMSD: %u", srn
->msd
);
2172 "\n\n Prefix or Link Label In Label Out "
2173 "Node or Adj. SID Interface Nexthop\n");
2175 "------------------ -------- --------- "
2176 "--------------------- --------- ---------------\n");
2178 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2179 snprintf(pref
, 19, "%s/%u", inet_ntoa(srp
->nhlfe
.prefv4
.prefix
),
2180 srp
->nhlfe
.prefv4
.prefixlen
);
2181 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2182 if (srp
->nhlfe
.label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2183 sprintf(label
, "pop");
2185 sprintf(label
, "%u", srp
->nhlfe
.label_out
);
2186 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2189 json_prefix
= json_object_new_array();
2190 json_object_object_add(json_node
,
2194 json_obj
= json_object_new_object();
2195 json_object_string_add(json_obj
, "prefix", pref
);
2196 json_object_int_add(json_obj
, "sid", srp
->sid
);
2197 json_object_int_add(json_obj
, "inputLabel",
2198 srp
->nhlfe
.label_in
);
2199 json_object_string_add(json_obj
, "outputLabel", label
);
2200 json_object_string_add(json_obj
, "interface",
2201 itf
? itf
->name
: "-");
2202 json_object_string_add(json_obj
, "nexthop",
2203 inet_ntoa(srp
->nhlfe
.nexthop
));
2204 json_object_array_add(json_prefix
, json_obj
);
2206 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2207 srp
->nhlfe
.label_in
, label
, sid
,
2208 itf
? itf
->name
: "-",
2209 inet_ntoa(srp
->nhlfe
.nexthop
));
2213 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2214 snprintf(pref
, 19, "%s/%u",
2215 inet_ntoa(srl
->nhlfe
[0].prefv4
.prefix
),
2216 srl
->nhlfe
[0].prefv4
.prefixlen
);
2217 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2218 if (srl
->nhlfe
[0].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2219 sprintf(label
, "pop");
2221 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2222 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2225 json_link
= json_object_new_array();
2226 json_object_object_add(
2227 json_node
, "extendedLink", json_link
);
2230 json_obj
= json_object_new_object();
2231 json_object_string_add(json_obj
, "prefix", pref
);
2232 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2233 json_object_int_add(json_obj
, "inputLabel",
2234 srl
->nhlfe
[0].label_in
);
2235 json_object_string_add(json_obj
, "outputLabel", label
);
2236 json_object_string_add(json_obj
, "interface",
2237 itf
? itf
->name
: "-");
2238 json_object_string_add(
2239 json_obj
, "nexthop",
2240 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2241 json_object_array_add(json_link
, json_obj
);
2243 json_obj
= json_object_new_object();
2244 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2245 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2246 sprintf(label
, "pop");
2248 sprintf(label
, "%u", srl
->nhlfe
[0].label_out
);
2249 json_object_string_add(json_obj
, "prefix", pref
);
2250 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2251 json_object_int_add(json_obj
, "inputLabel",
2252 srl
->nhlfe
[1].label_in
);
2253 json_object_string_add(json_obj
, "outputLabel", label
);
2254 json_object_string_add(json_obj
, "interface",
2255 itf
? itf
->name
: "-");
2256 json_object_string_add(
2257 json_obj
, "nexthop",
2258 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2259 json_object_array_add(json_link
, json_obj
);
2261 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2262 srl
->nhlfe
[0].label_in
, label
, sid
,
2263 itf
? itf
->name
: "-",
2264 inet_ntoa(srl
->nhlfe
[0].nexthop
));
2265 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2266 if (srl
->nhlfe
[1].label_out
== MPLS_LABEL_IMPLICIT_NULL
)
2267 sprintf(label
, "pop");
2269 sprintf(label
, "%u", srl
->nhlfe
[1].label_out
);
2270 vty_out(vty
, "%18s %8u %9s %21s %9s %15s\n", pref
,
2271 srl
->nhlfe
[1].label_in
, label
, sid
,
2272 itf
? itf
->name
: "-",
2273 inet_ntoa(srl
->nhlfe
[1].nexthop
));
2277 json_object_array_add(json
, json_node
);
2282 static void show_vty_srdb(struct hash_backet
*backet
, void *args
)
2284 struct vty
*vty
= (struct vty
*)args
;
2285 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2287 show_sr_node(vty
, NULL
, srn
);
2290 static void show_json_srdb(struct hash_backet
*backet
, void *args
)
2292 struct json_object
*json
= (struct json_object
*)args
;
2293 struct sr_node
*srn
= (struct sr_node
*)backet
->data
;
2295 show_sr_node(NULL
, json
, srn
);
2298 DEFUN (show_ip_opsf_srdb
,
2299 show_ip_ospf_srdb_cmd
,
2300 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2304 "Database summary\n"
2305 "Show Segment Routing Data Base\n"
2306 "Advertising SR node\n"
2307 "Advertising SR node ID (as an IP address)\n"
2308 "Self-originated SR node\n"
2313 struct sr_node
*srn
;
2314 uint8_t uj
= use_json(argc
, argv
);
2315 json_object
*json
= NULL
, *json_node_array
= NULL
;
2317 if (!OspfSR
.enabled
) {
2318 vty_out(vty
, "Segment Routing is disabled on this router\n");
2323 json
= json_object_new_object();
2324 json_node_array
= json_object_new_array();
2325 json_object_string_add(json
, "srdbID",
2326 inet_ntoa(OspfSR
.self
->adv_router
));
2327 json_object_object_add(json
, "srNodes", json_node_array
);
2330 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2331 inet_ntoa(OspfSR
.self
->adv_router
));
2334 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
2336 show_sr_node(vty
, json_node_array
, srn
);
2338 vty_out(vty
, "%s\n",
2339 json_object_to_json_string_ext(
2340 json
, JSON_C_TO_STRING_PRETTY
));
2341 json_object_free(json
);
2346 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
2347 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
2348 vty_out(vty
, "Specified Router ID %s is invalid\n",
2350 return CMD_WARNING_CONFIG_FAILED
;
2352 /* Get the SR Node from the SRDB */
2353 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
2355 show_sr_node(vty
, json_node_array
, srn
);
2357 vty_out(vty
, "%s\n",
2358 json_object_to_json_string_ext(
2359 json
, JSON_C_TO_STRING_PRETTY
));
2360 json_object_free(json
);
2365 /* No parameters have been provided, Iterate through all the SRDB */
2367 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2368 void *))show_json_srdb
,
2369 (void *)json_node_array
);
2370 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
2371 json
, JSON_C_TO_STRING_PRETTY
));
2372 json_object_free(json
);
2374 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_backet
*,
2375 void *))show_vty_srdb
,
2381 /* Install new CLI commands */
2382 void ospf_sr_register_vty(void)
2384 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
2386 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
2387 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
2388 install_element(OSPF_NODE
, &sr_sid_label_range_cmd
);
2389 install_element(OSPF_NODE
, &no_sr_sid_label_range_cmd
);
2390 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
2391 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
2392 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
2393 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);