2 * This is an implementation of Segment Routing
3 * as per RFC 8665 - OSPF Extensions for Segment Routing
4 * and RFC 8476 - Signaling Maximum SID Depth (MSD) Using OSPF
6 * Module name: Segment Routing
8 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
9 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
11 * Copyright (C) 2016 - 2020 Orange Labs http://www.orange.com
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
43 #include "libospf.h" /* for ospf interface types */
50 #include "sockunion.h" /* for inet_aton() */
58 #include "ospf_errors.h"
60 #include "ospfd/ospfd.h"
61 #include "ospfd/ospf_interface.h"
62 #include "ospfd/ospf_ism.h"
63 #include "ospfd/ospf_asbr.h"
64 #include "ospfd/ospf_lsa.h"
65 #include "ospfd/ospf_lsdb.h"
66 #include "ospfd/ospf_neighbor.h"
67 #include "ospfd/ospf_nsm.h"
68 #include "ospfd/ospf_flood.h"
69 #include "ospfd/ospf_packet.h"
70 #include "ospfd/ospf_spf.h"
71 #include "ospfd/ospf_dump.h"
72 #include "ospfd/ospf_route.h"
73 #include "ospfd/ospf_ase.h"
74 #include "ospfd/ospf_sr.h"
75 #include "ospfd/ospf_ri.h"
76 #include "ospfd/ospf_ext.h"
77 #include "ospfd/ospf_zebra.h"
80 * Global variable to manage Segment Routing on this node.
81 * Note that all parameter values are stored in network byte order.
83 static struct ospf_sr_db OspfSR
;
84 static void ospf_sr_register_vty(void);
85 static inline void del_adj_sid(struct sr_nhlfe nhlfe
);
86 static int ospf_sr_start(struct ospf
*ospf
);
89 * Segment Routing Data Base functions
92 /* Hash function for Segment Routing entry */
93 static unsigned int sr_hash(const void *p
)
95 const struct in_addr
*rid
= p
;
97 return jhash_1word(rid
->s_addr
, 0);
100 /* Compare 2 Router ID hash entries based on SR Node */
101 static bool sr_cmp(const void *p1
, const void *p2
)
103 const struct sr_node
*srn
= p1
;
104 const struct in_addr
*rid
= p2
;
106 return IPV4_ADDR_SAME(&srn
->adv_router
, rid
);
109 /* Functions to remove an SR Link */
110 static void del_sr_link(void *val
)
112 struct sr_link
*srl
= (struct sr_link
*)val
;
114 del_adj_sid(srl
->nhlfe
[0]);
115 del_adj_sid(srl
->nhlfe
[1]);
116 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
119 /* Functions to remove an SR Prefix */
120 static void del_sr_pref(void *val
)
122 struct sr_prefix
*srp
= (struct sr_prefix
*)val
;
124 ospf_zebra_delete_prefix_sid(srp
);
125 XFREE(MTYPE_OSPF_SR_PARAMS
, val
);
128 /* Allocate new Segment Routine node */
129 static struct sr_node
*sr_node_new(struct in_addr
*rid
)
137 /* Allocate Segment Routing node memory */
138 new = XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_node
));
140 /* Default Algorithm, SRGB and MSD */
141 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++)
142 new->algo
[i
] = SR_ALGORITHM_UNSET
;
144 new->srgb
.range_size
= 0;
145 new->srgb
.lower_bound
= 0;
148 /* Create Link, Prefix and Range TLVs list */
149 new->ext_link
= list_new();
150 new->ext_prefix
= list_new();
151 new->ext_link
->del
= del_sr_link
;
152 new->ext_prefix
->del
= del_sr_pref
;
154 IPV4_ADDR_COPY(&new->adv_router
, rid
);
155 new->neighbor
= NULL
;
158 osr_debug(" |- Created new SR node for %pI4", &new->adv_router
);
162 /* Supposed to be used for testing */
163 struct sr_node
*ospf_sr_node_create(struct in_addr
*rid
)
167 srn
= hash_get(OspfSR
.neighbors
, (void *)rid
, (void *)sr_node_new
);
172 /* Delete Segment Routing node */
173 static void sr_node_del(struct sr_node
*srn
)
179 osr_debug(" |- Delete SR node for %pI4", &srn
->adv_router
);
181 /* Clean Extended Link */
182 list_delete(&srn
->ext_link
);
184 /* Clean Prefix List */
185 list_delete(&srn
->ext_prefix
);
187 XFREE(MTYPE_OSPF_SR_PARAMS
, srn
);
190 /* Get SR Node for a given nexthop */
191 static struct sr_node
*get_sr_node_by_nexthop(struct ospf
*ospf
,
192 struct in_addr nexthop
)
194 struct ospf_interface
*oi
= NULL
;
195 struct ospf_neighbor
*nbr
= NULL
;
196 struct listnode
*node
;
197 struct route_node
*rn
;
202 if (OspfSR
.neighbors
== NULL
)
205 osr_debug(" |- Search SR-Node for nexthop %pI4", &nexthop
);
207 /* First, search neighbor Router ID for this nexthop */
209 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
210 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
212 if ((nbr
) && (IPV4_ADDR_SAME(&nexthop
, &nbr
->src
))) {
224 osr_debug(" |- Found nexthop Router ID %pI4", &nbr
->router_id
);
226 /* Then, search SR Node */
227 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, &nbr
->router_id
);
233 * Segment Routing Local Block management functions
237 * It is necessary to known which label is already allocated to manage the range
238 * of SRLB. This is particular useful when an interface flap (goes up / down
239 * frequently). Here, SR will release and then allocate label for the Adjacency
240 * for each concerned interface. If we don't care, there is a risk to run out of
243 * For that purpose, a similar principle as already provided to manage chunk of
244 * label is proposed. But, here, the label chunk has not a fix range of 64
245 * labels that could be easily manage with a single variable of 64 bits size.
246 * So, used_mark is used as a bit wise to mark label reserved (bit set) or not
247 * (bit unset). Its size is equal to the number of label of the SRLB range round
250 * - sr__local_block_init() computes the number of 64 bits variables that are
251 * needed to manage the SRLB range and allocates this number.
252 * - ospf_sr_local_block_request_label() pick up the first available label and
253 * set corresponding bit
254 * - ospf_sr_local_block_release_label() release label by reseting the
255 * corresponding bit and set the next label to the first free position
259 * Initialize Segment Routing Local Block from SRDB configuration and reserve
260 * block of bits to manage label allocation.
262 * @param lower_bound The lower bound of the SRLB range
263 * @param upper_bound The upper bound of the SRLB range
265 * @return 0 on success, -1 otherwise
267 static int sr_local_block_init(uint32_t lower_bound
, uint32_t upper_bound
)
269 struct sr_local_block
*srlb
= &OspfSR
.srlb
;
272 /* Check if SRLB is not already configured */
277 * Request SRLB to the label manager. If the allocation fails, return
278 * an error to disable SR until a new SRLB is successfully allocated.
280 size
= upper_bound
- lower_bound
+ 1;
281 if (ospf_zebra_request_label_range(lower_bound
, size
)) {
282 zlog_err("SR: Error reserving SRLB [%u/%u] %u labels",
283 lower_bound
, upper_bound
, size
);
287 osr_debug("SR: Got new SRLB [%u/%u], %u labels", lower_bound
,
290 /* Initialize the SRLB */
291 srlb
->start
= lower_bound
;
292 srlb
->end
= upper_bound
;
295 /* Compute the needed Used Mark number and allocate them */
296 srlb
->max_block
= size
/ SRLB_BLOCK_SIZE
;
297 if ((size
% SRLB_BLOCK_SIZE
) != 0)
299 srlb
->used_mark
= XCALLOC(MTYPE_OSPF_SR_PARAMS
,
300 srlb
->max_block
* SRLB_BLOCK_SIZE
);
301 srlb
->reserved
= true;
306 static int sr_global_block_init(uint32_t start
, uint32_t size
)
308 struct sr_global_block
*srgb
= &OspfSR
.srgb
;
310 /* Check if already configured */
315 uint32_t end
= start
+ size
- 1;
316 if (ospf_zebra_request_label_range(start
, size
) < 0) {
317 zlog_err("SR: Error reserving SRGB [%u/%u], %u labels", start
,
322 osr_debug("SR: Got new SRGB [%u/%u], %u labels", start
, end
, size
);
327 srgb
->reserved
= true;
332 * Remove Segment Routing Local Block.
335 static void sr_local_block_delete(void)
337 struct sr_local_block
*srlb
= &OspfSR
.srlb
;
339 /* Check if SRLB is not already delete */
343 osr_debug("SR (%s): Remove SRLB [%u/%u]", __func__
, srlb
->start
,
346 /* First release the label block */
347 ospf_zebra_release_label_range(srlb
->start
, srlb
->end
);
349 /* Then reset SRLB structure */
350 if (srlb
->used_mark
!= NULL
)
351 XFREE(MTYPE_OSPF_SR_PARAMS
, srlb
->used_mark
);
353 srlb
->reserved
= false;
357 * Remove Segment Routing Global block
359 static void sr_global_block_delete(void)
361 struct sr_global_block
*srgb
= &OspfSR
.srgb
;
366 osr_debug("SR (%s): Remove SRGB [%u/%u]", __func__
, srgb
->start
,
367 srgb
->start
+ srgb
->size
- 1);
369 ospf_zebra_release_label_range(srgb
->start
,
370 srgb
->start
+ srgb
->size
- 1);
372 srgb
->reserved
= false;
377 * Request a label from the Segment Routing Local Block.
379 * @return First available label on success or MPLS_INVALID_LABEL if the
380 * block of labels is full
382 mpls_label_t
ospf_sr_local_block_request_label(void)
384 struct sr_local_block
*srlb
= &OspfSR
.srlb
;
388 uint32_t size
= srlb
->end
- srlb
->start
+ 1;
390 /* Check if we ran out of available labels */
391 if (srlb
->current
>= size
)
392 return MPLS_INVALID_LABEL
;
394 /* Get first available label and mark it used */
395 label
= srlb
->current
+ srlb
->start
;
396 index
= srlb
->current
/ SRLB_BLOCK_SIZE
;
397 pos
= 1ULL << (srlb
->current
% SRLB_BLOCK_SIZE
);
398 srlb
->used_mark
[index
] |= pos
;
400 /* Jump to the next free position */
402 pos
= srlb
->current
% SRLB_BLOCK_SIZE
;
403 while (srlb
->current
< size
) {
406 if (!((1ULL << pos
) & srlb
->used_mark
[index
]))
410 pos
= srlb
->current
% SRLB_BLOCK_SIZE
;
414 if (srlb
->current
== size
)
416 "SR: Warning, SRLB is depleted and next label request will fail");
422 * Release label in the Segment Routing Local Block.
424 * @param label Label to be release
426 * @return 0 on success or -1 if label falls outside SRLB
428 int ospf_sr_local_block_release_label(mpls_label_t label
)
430 struct sr_local_block
*srlb
= &OspfSR
.srlb
;
434 /* Check that label falls inside the SRLB */
435 if ((label
< srlb
->start
) || (label
> srlb
->end
)) {
436 flog_warn(EC_OSPF_SR_SID_OVERFLOW
,
437 "%s: Returning label %u is outside SRLB [%u/%u]",
438 __func__
, label
, srlb
->start
, srlb
->end
);
442 index
= (label
- srlb
->start
) / SRLB_BLOCK_SIZE
;
443 pos
= 1ULL << ((label
- srlb
->start
) % SRLB_BLOCK_SIZE
);
444 srlb
->used_mark
[index
] &= ~pos
;
445 /* Reset current to the first available position */
446 for (index
= 0; index
< srlb
->max_block
; index
++) {
447 if (srlb
->used_mark
[index
] != 0xFFFFFFFFFFFFFFFF) {
448 for (pos
= 0; pos
< SRLB_BLOCK_SIZE
; pos
++)
449 if (!((1ULL << pos
) & srlb
->used_mark
[index
])) {
451 index
* SRLB_BLOCK_SIZE
+ pos
;
462 * Segment Routing Initialization functions
466 * Thread function to re-attempt connection to the Label Manager and thus be
467 * able to start Segment Routing.
469 * @param start Thread structure that contains area as argument
471 * @return 1 on success
473 static int sr_start_label_manager(struct thread
*start
)
477 ospf
= THREAD_ARG(start
);
479 /* re-attempt to start SR & Label Manager connection */
485 /* Segment Routing starter function */
486 static int ospf_sr_start(struct ospf
*ospf
)
488 struct route_node
*rn
;
489 struct ospf_lsa
*lsa
;
493 osr_debug("SR (%s): Start Segment Routing", __func__
);
495 /* Initialize self SR Node if not already done */
496 if (OspfSR
.self
== NULL
) {
497 srn
= hash_get(OspfSR
.neighbors
, (void *)&(ospf
->router_id
),
498 (void *)sr_node_new
);
500 /* Complete & Store self SR Node */
501 srn
->srgb
.range_size
= OspfSR
.srgb
.size
;
502 srn
->srgb
.lower_bound
= OspfSR
.srgb
.start
;
503 srn
->srlb
.lower_bound
= OspfSR
.srlb
.start
;
504 srn
->srlb
.range_size
= OspfSR
.srlb
.end
- OspfSR
.srlb
.start
+ 1;
505 srn
->algo
[0] = OspfSR
.algo
[0];
506 srn
->msd
= OspfSR
.msd
;
510 /* Then, start Label Manager if not ready */
511 if (!ospf_zebra_label_manager_ready())
512 if (ospf_zebra_label_manager_connect() < 0) {
513 /* Re-attempt to connect to Label Manager in 1 sec. */
514 thread_add_timer(master
, sr_start_label_manager
, ospf
,
515 1, &OspfSR
.t_start_lm
);
516 osr_debug(" |- Failed to start the Label Manager");
521 * Request SRLB & SGRB to the label manager if not already reserved.
522 * If the allocation fails, return an error to disable SR until a new
523 * SRLB and/or SRGB are successfully allocated.
525 if (sr_local_block_init(OspfSR
.srlb
.start
, OspfSR
.srlb
.end
) < 0)
528 if (sr_global_block_init(OspfSR
.srgb
.start
, OspfSR
.srgb
.size
) < 0)
531 /* SR is UP and ready to flood LSA */
532 OspfSR
.status
= SR_UP
;
534 /* Set Router Information SR parameters */
535 osr_debug("SR: Activate SR for Router Information LSA");
537 ospf_router_info_update_sr(true, OspfSR
.self
);
540 osr_debug("SR: Activate SR for Extended Link/Prefix LSA");
542 ospf_ext_update_sr(true);
544 osr_debug("SR (%s): Update SR-DB from LSDB", __func__
);
546 /* Start by looking to Router Info & Extended LSA in lsdb */
547 if ((ospf
!= NULL
) && (ospf
->backbone
!= NULL
)) {
548 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf
->backbone
), rn
, lsa
) {
549 if (IS_LSA_MAXAGE(lsa
) || IS_LSA_SELF(lsa
))
552 GET_OPAQUE_TYPE(ntohl(lsa
->data
->id
.s_addr
));
554 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA
:
555 ospf_sr_ri_lsa_update(lsa
);
557 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA
:
558 ospf_sr_ext_prefix_lsa_update(lsa
);
560 case OPAQUE_TYPE_EXTENDED_LINK_LSA
:
561 ospf_sr_ext_link_lsa_update(lsa
);
573 /* Stop Segment Routing */
574 static void ospf_sr_stop(void)
577 if (OspfSR
.status
== SR_OFF
)
580 osr_debug("SR (%s): Stop Segment Routing", __func__
);
582 /* Disable any re-attempt to connect to Label Manager */
583 THREAD_OFF(OspfSR
.t_start_lm
);
585 /* Release SRGB if active */
586 sr_global_block_delete();
588 /* Release SRLB if active */
589 sr_local_block_delete();
592 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
593 * be remove though list_delete() call. See sr_node_del()
595 hash_clean(OspfSR
.neighbors
, (void *)sr_node_del
);
597 OspfSR
.status
= SR_OFF
;
601 * Segment Routing initialize function
605 * @return 0 if OK, -1 otherwise
607 int ospf_sr_init(void)
611 osr_debug("SR (%s): Initialize SR Data Base", __func__
);
613 memset(&OspfSR
, 0, sizeof(struct ospf_sr_db
));
614 OspfSR
.status
= SR_OFF
;
615 /* Only AREA flooding is supported in this release */
616 OspfSR
.scope
= OSPF_OPAQUE_AREA_LSA
;
618 /* Initialize Algorithms, SRGB, SRLB and MSD TLVs */
619 /* Only Algorithm SPF is supported */
620 OspfSR
.algo
[0] = SR_ALGORITHM_SPF
;
621 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++)
622 OspfSR
.algo
[i
] = SR_ALGORITHM_UNSET
;
624 OspfSR
.srgb
.size
= DEFAULT_SRGB_SIZE
;
625 OspfSR
.srgb
.start
= DEFAULT_SRGB_LABEL
;
626 OspfSR
.srgb
.reserved
= false;
628 OspfSR
.srlb
.start
= DEFAULT_SRLB_LABEL
;
629 OspfSR
.srlb
.end
= DEFAULT_SRLB_END
;
630 OspfSR
.srlb
.reserved
= false;
633 /* Initialize Hash table for neighbor SR nodes */
634 OspfSR
.neighbors
= hash_create(sr_hash
, sr_cmp
, "OSPF_SR");
635 if (OspfSR
.neighbors
== NULL
)
638 /* Register Segment Routing VTY command */
639 ospf_sr_register_vty();
646 * Segment Routing termination function
651 void ospf_sr_term(void)
654 /* Stop Segment Routing */
657 /* Clear SR Node Table */
658 if (OspfSR
.neighbors
)
659 hash_free(OspfSR
.neighbors
);
664 * Segment Routing finish function
669 void ospf_sr_finish(void)
671 /* Stop Segment Routing */
676 * Following functions are used to manipulate the
677 * Next Hop Label Forwarding entry (NHLFE)
680 /* Compute label from index */
681 static mpls_label_t
index2label(uint32_t index
, struct sr_block srgb
)
685 label
= srgb
.lower_bound
+ index
;
686 if (label
> (srgb
.lower_bound
+ srgb
.range_size
)) {
687 flog_warn(EC_OSPF_SR_SID_OVERFLOW
,
688 "%s: SID index %u falls outside SRGB range",
690 return MPLS_INVALID_LABEL
;
695 /* Get the prefix sid for a specific router id */
696 mpls_label_t
ospf_sr_get_prefix_sid_by_id(struct in_addr
*id
)
699 struct sr_prefix
*srp
;
702 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, id
);
706 * TODO: Here we assume that the SRGBs are the same,
707 * and that the node's prefix SID is at the head of
708 * the list, probably needs tweaking.
710 srp
= listnode_head(srn
->ext_prefix
);
711 label
= index2label(srp
->sid
, srn
->srgb
);
713 label
= MPLS_INVALID_LABEL
;
719 /* Get the adjacency sid for a specific 'root' id and 'neighbor' id */
720 mpls_label_t
ospf_sr_get_adj_sid_by_id(struct in_addr
*root_id
,
721 struct in_addr
*neighbor_id
)
726 struct listnode
*node
;
728 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
, root_id
);
730 label
= MPLS_INVALID_LABEL
;
733 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
734 if (srl
->type
== ADJ_SID
735 && srl
->remote_id
.s_addr
== neighbor_id
->s_addr
) {
745 /* Get neighbor full structure from address */
746 static struct ospf_neighbor
*get_neighbor_by_addr(struct ospf
*top
,
749 struct ospf_neighbor
*nbr
;
750 struct ospf_interface
*oi
;
751 struct listnode
*node
;
752 struct route_node
*rn
;
758 for (ALL_LIST_ELEMENTS_RO(top
->oiflist
, node
, oi
))
759 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
762 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
,
764 || IPV4_ADDR_SAME(&nbr
->router_id
, &addr
)) {
765 route_unlock_node(rn
);
772 /* Get OSPF Path from address */
773 static struct ospf_route
*get_nexthop_by_addr(struct ospf
*top
,
774 struct prefix_ipv4 p
)
776 struct route_node
*rn
;
782 osr_debug(" |- Search Nexthop for prefix %pFX",
783 (struct prefix
*)&p
);
785 rn
= route_node_lookup(top
->new_table
, (struct prefix
*)&p
);
788 * Check if we found an OSPF route. May be NULL if SPF has not
789 * yet populate routing table for this prefix.
794 route_unlock_node(rn
);
798 /* Compute NHLFE entry for Extended Link */
799 static int compute_link_nhlfe(struct sr_link
*srl
)
801 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
802 struct ospf_neighbor
*nh
;
805 osr_debug(" |- Compute NHLFE for link %pI4", &srl
->itf_addr
);
807 /* First determine the OSPF Neighbor */
808 nh
= get_neighbor_by_addr(top
, srl
->nhlfe
[0].nexthop
);
810 /* Neighbor could be not found when OSPF Adjacency just fire up
811 * because SPF don't yet populate routing table. This NHLFE will
812 * be fixed later when SR SPF schedule will be called.
817 osr_debug(" |- Found nexthop %pI4", &nh
->router_id
);
819 /* Set ifindex for this neighbor */
820 srl
->nhlfe
[0].ifindex
= nh
->oi
->ifp
->ifindex
;
821 srl
->nhlfe
[1].ifindex
= nh
->oi
->ifp
->ifindex
;
823 /* Update neighbor address for LAN_ADJ_SID */
824 if (srl
->type
== LAN_ADJ_SID
) {
825 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &nh
->src
);
826 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &nh
->src
);
829 /* Set Input & Output Label */
830 if (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
831 srl
->nhlfe
[0].label_in
= srl
->sid
[0];
833 srl
->nhlfe
[0].label_in
=
834 index2label(srl
->sid
[0], srl
->srn
->srgb
);
835 if (CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
836 srl
->nhlfe
[1].label_in
= srl
->sid
[1];
838 srl
->nhlfe
[1].label_in
=
839 index2label(srl
->sid
[1], srl
->srn
->srgb
);
841 srl
->nhlfe
[0].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
842 srl
->nhlfe
[1].label_out
= MPLS_LABEL_IMPLICIT_NULL
;
849 * Compute output label for the given Prefix-SID.
851 * @param srp Segment Routing Prefix
852 * @param srnext Segment Routing nexthop node
854 * @return MPLS label or MPLS_INVALID_LABEL in case of error
856 static mpls_label_t
sr_prefix_out_label(const struct sr_prefix
*srp
,
857 const struct sr_node
*srnext
)
859 /* Check if the nexthop SR Node is the last hop? */
860 if (srnext
== srp
->srn
) {
861 /* SR-Node doesn't request NO-PHP. Return Implicit NULL label */
862 if (!CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
863 return MPLS_LABEL_IMPLICIT_NULL
;
865 /* SR-Node requests Explicit NULL Label */
866 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_EFLG
))
867 return MPLS_LABEL_IPV4_EXPLICIT_NULL
;
871 /* Return SID value as MPLS label if it is an Absolute SID */
872 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
873 | EXT_SUBTLV_PREFIX_SID_LFLG
)) {
875 * V/L SIDs have local significance, so only adjacent routers
876 * can use them (RFC8665 section #5)
878 if (srp
->srn
!= srnext
)
879 return MPLS_INVALID_LABEL
;
883 /* Return MPLS label as SRGB lower bound + SID index as per RFC 8665 */
884 return (index2label(srp
->sid
, srnext
->srgb
));
888 * Compute NHLFE entry for Extended Prefix
890 * @param srp - Segment Routing Prefix
892 * @return -1 if no route is found, 0 if there is no SR route ready
893 * and 1 if success or update
895 static int compute_prefix_nhlfe(struct sr_prefix
*srp
)
897 struct ospf
*top
= ospf_lookup_by_vrf_id(VRF_DEFAULT
);
898 struct ospf_path
*path
;
899 struct listnode
*node
;
900 struct sr_node
*srnext
;
903 osr_debug(" |- Compute NHLFE for prefix %pFX",
904 (struct prefix
*)&srp
->prefv4
);
907 /* First determine the nexthop */
908 srp
->route
= get_nexthop_by_addr(top
, srp
->prefv4
);
910 /* Nexthop could be not found when OSPF Adjacency just fire up
911 * because SPF don't yet populate routing table. This NHLFE will
912 * be fixed later when SR SPF schedule will be called.
914 if (srp
->route
== NULL
)
917 /* Compute Input Label with self SRGB */
918 srp
->label_in
= index2label(srp
->sid
, OspfSR
.self
->srgb
);
921 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, node
, path
)) {
923 osr_debug(" |- Process new route via %pI4 for this prefix",
927 * Get SR-Node for this nexthop. Could be not yet available
928 * as Extended Link / Prefix and Router Information are flooded
929 * after LSA Type 1 & 2 which populate the OSPF Route Table
931 srnext
= get_sr_node_by_nexthop(top
, path
->nexthop
);
935 /* And store this information for later update */
936 srnext
->neighbor
= OspfSR
.self
;
937 path
->srni
.nexthop
= srnext
;
940 * SR Node could be known, but SRGB could be not initialize
941 * This is due to the fact that Extended Link / Prefix could
942 * be received before corresponding Router Information LSA
944 if (srnext
== NULL
|| srnext
->srgb
.lower_bound
== 0
945 || srnext
->srgb
.range_size
== 0) {
947 " |- SR-Node %pI4 not ready. Stop process",
948 &srnext
->adv_router
);
949 path
->srni
.label_out
= MPLS_INVALID_LABEL
;
953 osr_debug(" |- Found SRGB %u/%u for next hop SR-Node %pI4",
954 srnext
->srgb
.range_size
, srnext
->srgb
.lower_bound
,
955 &srnext
->adv_router
);
957 /* Compute Output Label with Nexthop SR Node SRGB */
958 path
->srni
.label_out
= sr_prefix_out_label(srp
, srnext
);
960 osr_debug(" |- Computed new labels in: %u out: %u",
961 srp
->label_in
, path
->srni
.label_out
);
967 /* Add new NHLFE entry for Adjacency SID */
968 static inline void add_adj_sid(struct sr_nhlfe nhlfe
)
970 if (nhlfe
.label_in
!= 0)
971 ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_ADD
, nhlfe
);
974 /* Remove NHLFE entry for Adjacency SID */
975 static inline void del_adj_sid(struct sr_nhlfe nhlfe
)
977 if (nhlfe
.label_in
!= 0)
978 ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_DELETE
, nhlfe
);
981 /* Update NHLFE entry for Adjacency SID */
982 static inline void update_adj_sid(struct sr_nhlfe n1
, struct sr_nhlfe n2
)
989 * Functions to parse and get Extended Link / Prefix
993 /* Extended Link SubTLVs Getter */
994 static struct sr_link
*get_ext_link_sid(struct tlv_header
*tlvh
, size_t size
)
998 struct ext_tlv_link
*link
= (struct ext_tlv_link
*)tlvh
;
999 struct ext_subtlv_adj_sid
*adj_sid
;
1000 struct ext_subtlv_lan_adj_sid
*lan_sid
;
1001 struct ext_subtlv_rmt_itf_addr
*rmt_itf
;
1003 struct tlv_header
*sub_tlvh
;
1004 uint16_t length
= 0, sum
= 0, i
= 0;
1006 /* Check TLV size */
1007 if ((ntohs(tlvh
->length
) > size
)
1008 || ntohs(tlvh
->length
) < EXT_TLV_LINK_SIZE
) {
1009 zlog_warn("Wrong Extended Link TLV size. Abort!");
1013 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
1015 /* Initialize TLV browsing */
1016 length
= ntohs(tlvh
->length
) - EXT_TLV_LINK_SIZE
;
1017 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
1018 + EXT_TLV_LINK_SIZE
);
1019 for (; sum
< length
&& sub_tlvh
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
1020 switch (ntohs(sub_tlvh
->type
)) {
1021 case EXT_SUBTLV_ADJ_SID
:
1022 adj_sid
= (struct ext_subtlv_adj_sid
*)sub_tlvh
;
1023 srl
->type
= ADJ_SID
;
1024 i
= CHECK_FLAG(adj_sid
->flags
,
1025 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
1028 srl
->flags
[i
] = adj_sid
->flags
;
1029 if (CHECK_FLAG(adj_sid
->flags
,
1030 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1031 srl
->sid
[i
] = GET_LABEL(ntohl(adj_sid
->value
));
1033 srl
->sid
[i
] = ntohl(adj_sid
->value
);
1034 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
, &link
->link_id
);
1036 case EXT_SUBTLV_LAN_ADJ_SID
:
1037 lan_sid
= (struct ext_subtlv_lan_adj_sid
*)sub_tlvh
;
1038 srl
->type
= LAN_ADJ_SID
;
1039 i
= CHECK_FLAG(lan_sid
->flags
,
1040 EXT_SUBTLV_LINK_ADJ_SID_BFLG
)
1043 srl
->flags
[i
] = lan_sid
->flags
;
1044 if (CHECK_FLAG(lan_sid
->flags
,
1045 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1046 srl
->sid
[i
] = GET_LABEL(ntohl(lan_sid
->value
));
1048 srl
->sid
[i
] = ntohl(lan_sid
->value
);
1049 IPV4_ADDR_COPY(&srl
->nhlfe
[i
].nexthop
,
1050 &lan_sid
->neighbor_id
);
1052 case EXT_SUBTLV_RMT_ITF_ADDR
:
1053 rmt_itf
= (struct ext_subtlv_rmt_itf_addr
*)sub_tlvh
;
1054 IPV4_ADDR_COPY(&srl
->nhlfe
[0].nexthop
, &rmt_itf
->value
);
1055 IPV4_ADDR_COPY(&srl
->nhlfe
[1].nexthop
, &rmt_itf
->value
);
1060 sum
+= TLV_SIZE(sub_tlvh
);
1063 IPV4_ADDR_COPY(&srl
->itf_addr
, &link
->link_data
);
1065 osr_debug(" |- Found primary %u and backup %u Adj/Lan Sid for %pI4",
1066 srl
->sid
[0], srl
->sid
[1], &srl
->itf_addr
);
1071 /* Extended Prefix SubTLVs Getter */
1072 static struct sr_prefix
*get_ext_prefix_sid(struct tlv_header
*tlvh
,
1076 struct sr_prefix
*srp
;
1077 struct ext_tlv_prefix
*pref
= (struct ext_tlv_prefix
*)tlvh
;
1078 struct ext_subtlv_prefix_sid
*psid
;
1080 struct tlv_header
*sub_tlvh
;
1081 uint16_t length
= 0, sum
= 0;
1083 /* Check TLV size */
1084 if ((ntohs(tlvh
->length
) > size
)
1085 || ntohs(tlvh
->length
) < EXT_TLV_PREFIX_SIZE
) {
1086 zlog_warn("Wrong Extended Link TLV size. Abort!");
1090 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
1092 /* Initialize TLV browsing */
1093 length
= ntohs(tlvh
->length
) - EXT_TLV_PREFIX_SIZE
;
1094 sub_tlvh
= (struct tlv_header
*)((char *)(tlvh
) + TLV_HDR_SIZE
1095 + EXT_TLV_PREFIX_SIZE
);
1096 for (; sum
< length
&& sub_tlvh
; sub_tlvh
= TLV_HDR_NEXT(sub_tlvh
)) {
1097 switch (ntohs(sub_tlvh
->type
)) {
1098 case EXT_SUBTLV_PREFIX_SID
:
1099 psid
= (struct ext_subtlv_prefix_sid
*)sub_tlvh
;
1100 if (psid
->algorithm
!= SR_ALGORITHM_SPF
) {
1101 flog_err(EC_OSPF_INVALID_ALGORITHM
,
1102 "SR (%s): Unsupported Algorithm",
1104 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1107 srp
->type
= PREF_SID
;
1108 srp
->flags
= psid
->flags
;
1109 if (CHECK_FLAG(psid
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1110 srp
->sid
= GET_LABEL(ntohl(psid
->value
));
1112 srp
->sid
= ntohl(psid
->value
);
1113 IPV4_ADDR_COPY(&srp
->prefv4
.prefix
, &pref
->address
);
1114 srp
->prefv4
.prefixlen
= pref
->pref_length
;
1115 srp
->prefv4
.family
= AF_INET
;
1116 apply_mask_ipv4(&srp
->prefv4
);
1121 sum
+= TLV_SIZE(sub_tlvh
);
1124 osr_debug(" |- Found SID %u for prefix %pFX", srp
->sid
,
1125 (struct prefix
*)&srp
->prefv4
);
1131 * Functions to manipulate Segment Routing Link & Prefix structures
1134 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
1135 static inline int sr_link_cmp(struct sr_link
*srl1
, struct sr_link
*srl2
)
1137 if ((srl1
->sid
[0] == srl2
->sid
[0]) && (srl1
->sid
[1] == srl2
->sid
[1])
1138 && (srl1
->type
== srl2
->type
) && (srl1
->flags
[0] == srl2
->flags
[0])
1139 && (srl1
->flags
[1] == srl2
->flags
[1]))
1145 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
1146 static inline int sr_prefix_cmp(struct sr_prefix
*srp1
, struct sr_prefix
*srp2
)
1148 if ((srp1
->sid
== srp2
->sid
) && (srp1
->flags
== srp2
->flags
))
1154 /* Update Segment Link of given Segment Routing Node */
1155 static void update_ext_link_sid(struct sr_node
*srn
, struct sr_link
*srl
,
1158 struct listnode
*node
;
1164 if ((srn
== NULL
) || (srl
== NULL
))
1167 osr_debug(" |- Process Extended Link Adj/Lan-SID");
1169 /* Detect if Adj/Lan_Adj SID must be configured */
1170 if (!CHECK_FLAG(lsa_flags
, OSPF_LSA_SELF
)
1171 && (CHECK_FLAG(srl
->flags
[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)
1172 || CHECK_FLAG(srl
->flags
[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG
)))
1175 /* Search for existing Segment Link */
1176 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, lk
))
1177 if (lk
->instance
== srl
->instance
) {
1182 osr_debug(" |- %s SR Link 8.0.0.%u for SR node %pI4",
1183 found
? "Update" : "Add", GET_OPAQUE_ID(srl
->instance
),
1186 /* if not found, add new Segment Link and install NHLFE */
1188 /* Complete SR-Link and add it to SR-Node list */
1190 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
1191 listnode_add(srn
->ext_link
, srl
);
1192 /* Try to set MPLS table */
1193 if (config
&& compute_link_nhlfe(srl
)) {
1194 add_adj_sid(srl
->nhlfe
[0]);
1195 add_adj_sid(srl
->nhlfe
[1]);
1198 /* Update SR-Link if they are different */
1199 if (sr_link_cmp(lk
, srl
)) {
1200 /* Try to set MPLS table */
1202 if (compute_link_nhlfe(srl
)) {
1203 update_adj_sid(lk
->nhlfe
[0],
1205 update_adj_sid(lk
->nhlfe
[1],
1208 del_adj_sid(lk
->nhlfe
[0]);
1209 del_adj_sid(lk
->nhlfe
[1]);
1212 /* Replace SR-Link in SR-Node Adjacency List */
1213 listnode_delete(srn
->ext_link
, lk
);
1214 XFREE(MTYPE_OSPF_SR_PARAMS
, lk
);
1216 IPV4_ADDR_COPY(&srl
->adv_router
, &srn
->adv_router
);
1217 listnode_add(srn
->ext_link
, srl
);
1220 * This is just an LSA refresh.
1221 * Stop processing and free SR Link
1223 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1228 /* Update Segment Prefix of given Segment Routing Node */
1229 static void update_ext_prefix_sid(struct sr_node
*srn
, struct sr_prefix
*srp
)
1232 struct listnode
*node
;
1233 struct sr_prefix
*pref
;
1237 if (srn
== NULL
|| srp
== NULL
)
1240 osr_debug(" |- Process Extended Prefix SID %u", srp
->sid
);
1242 /* Process only Global Prefix SID */
1243 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_LFLG
))
1246 /* Search for existing Segment Prefix */
1247 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, pref
))
1248 if (pref
->instance
== srp
->instance
1249 && prefix_same((struct prefix
*)&srp
->prefv4
,
1255 osr_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %pI4",
1256 found
? "Update" : "Add", GET_OPAQUE_ID(srp
->instance
),
1259 /* Complete SR-Prefix */
1261 IPV4_ADDR_COPY(&srp
->adv_router
, &srn
->adv_router
);
1263 /* if not found, add new Segment Prefix and install NHLFE */
1265 /* Add it to SR-Node list ... */
1266 listnode_add(srn
->ext_prefix
, srp
);
1267 /* ... and try to set MPLS table */
1268 if (compute_prefix_nhlfe(srp
) == 1)
1269 ospf_zebra_update_prefix_sid(srp
);
1272 * An old SR prefix exist. Check if something changes or if it
1273 * is just a refresh.
1275 if (sr_prefix_cmp(pref
, srp
)) {
1276 if (compute_prefix_nhlfe(srp
) == 1) {
1277 ospf_zebra_delete_prefix_sid(pref
);
1278 /* Replace Segment Prefix */
1279 listnode_delete(srn
->ext_prefix
, pref
);
1280 XFREE(MTYPE_OSPF_SR_PARAMS
, pref
);
1281 listnode_add(srn
->ext_prefix
, srp
);
1282 ospf_zebra_update_prefix_sid(srp
);
1284 /* New NHLFE was not found.
1285 * Just free the SR Prefix
1287 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1290 /* This is just an LSA refresh.
1291 * Stop processing and free SR Prefix
1293 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1299 * When change the FRR Self SRGB, update the NHLFE Input Label
1300 * for all Extended Prefix with SID index through hash_iterate()
1302 static void update_in_nhlfe(struct hash_bucket
*bucket
, void *args
)
1304 struct listnode
*node
;
1305 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1306 struct sr_prefix
*srp
;
1308 /* Process Every Extended Prefix for this SR-Node */
1309 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1310 /* Process Self SRN only if NO-PHP is requested */
1311 if ((srn
== OspfSR
.self
)
1312 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
))
1315 /* Process only SID Index */
1316 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_VFLG
))
1319 /* First, remove old MPLS table entries ... */
1320 ospf_zebra_delete_prefix_sid(srp
);
1321 /* ... then compute new input label ... */
1322 srp
->label_in
= index2label(srp
->sid
, OspfSR
.self
->srgb
);
1323 /* ... and install new MPLS LFIB */
1324 ospf_zebra_update_prefix_sid(srp
);
1329 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1330 * with SID index which use the given SR-Node as nexthop through hash_iterate()
1332 static void update_out_nhlfe(struct hash_bucket
*bucket
, void *args
)
1334 struct listnode
*node
, *pnode
;
1335 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1336 struct sr_node
*srnext
= (struct sr_node
*)args
;
1337 struct sr_prefix
*srp
;
1338 struct ospf_path
*path
;
1340 /* Skip Self SR-Node */
1341 if (srn
== OspfSR
.self
)
1344 osr_debug("SR (%s): Update Out NHLFE for neighbor SR-Node %pI4",
1345 __func__
, &srn
->adv_router
);
1347 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1348 /* Skip Prefix that has not yet a valid route */
1349 if (srp
->route
== NULL
)
1352 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, pnode
, path
)) {
1353 /* Skip path that has not next SR-Node as nexthop */
1354 if (path
->srni
.nexthop
!= srnext
)
1357 /* Compute new Output Label */
1358 path
->srni
.label_out
= sr_prefix_out_label(srp
, srnext
);
1361 /* Finally update MPLS table */
1362 ospf_zebra_update_prefix_sid(srp
);
1367 * Following functions are call when new Segment Routing LSA are received
1368 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1369 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1370 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1373 /* Update Segment Routing from Router Information LSA */
1374 void ospf_sr_ri_lsa_update(struct ospf_lsa
*lsa
)
1376 struct sr_node
*srn
;
1377 struct tlv_header
*tlvh
;
1378 struct lsa_header
*lsah
= lsa
->data
;
1379 struct ri_sr_tlv_sid_label_range
*ri_srgb
= NULL
;
1380 struct ri_sr_tlv_sid_label_range
*ri_srlb
= NULL
;
1381 struct ri_sr_tlv_sr_algorithm
*algo
= NULL
;
1382 struct sr_block srgb
;
1383 uint16_t length
= 0, sum
= 0;
1386 osr_debug("SR (%s): Process Router Information LSA 4.0.0.%u from %pI4",
1387 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1391 if (IS_LSA_SELF(lsa
))
1394 if (OspfSR
.neighbors
== NULL
) {
1395 flog_err(EC_OSPF_SR_INVALID_DB
,
1396 "SR (%s): Abort! no valid SR DataBase", __func__
);
1400 /* Search SR Node in hash table from Router ID */
1401 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1405 /* Collect Router Information Sub TLVs */
1406 /* Initialize TLV browsing */
1407 length
= lsa
->size
- OSPF_LSA_HEADER_SIZE
;
1408 srgb
.range_size
= 0;
1409 srgb
.lower_bound
= 0;
1411 for (tlvh
= TLV_HDR_TOP(lsah
); (sum
< length
) && (tlvh
!= NULL
);
1412 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1413 switch (ntohs(tlvh
->type
)) {
1414 case RI_SR_TLV_SR_ALGORITHM
:
1415 algo
= (struct ri_sr_tlv_sr_algorithm
*)tlvh
;
1417 case RI_SR_TLV_SRGB_LABEL_RANGE
:
1418 ri_srgb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1420 case RI_SR_TLV_SRLB_LABEL_RANGE
:
1421 ri_srlb
= (struct ri_sr_tlv_sid_label_range
*)tlvh
;
1423 case RI_SR_TLV_NODE_MSD
:
1424 msd
= ((struct ri_sr_tlv_node_msd
*)(tlvh
))->value
;
1429 sum
+= TLV_SIZE(tlvh
);
1432 /* Check if Segment Routing Capabilities has been found */
1433 if (ri_srgb
== NULL
) {
1434 /* Skip Router Information without SR capabilities
1435 * advertise by a non SR Node */
1439 /* Remove SR Node that advertise Router Information
1440 * without SR capabilities. This could correspond to a
1441 * Node stopping Segment Routing */
1442 hash_release(OspfSR
.neighbors
, &(srn
->adv_router
));
1448 /* Check that RI LSA belongs to the correct SR Node */
1449 if ((srn
!= NULL
) && (srn
->instance
!= 0)
1450 && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1451 flog_err(EC_OSPF_SR_INVALID_LSA_ID
,
1452 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4/%u",
1453 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1454 &lsah
->adv_router
, srn
->instance
);
1458 /* OK. All things look good. Get SRGB */
1459 srgb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srgb
->size
));
1460 srgb
.lower_bound
= GET_LABEL(ntohl(ri_srgb
->lower
.value
));
1462 /* Check if it is a new SR Node or not */
1464 /* Get a new SR Node in hash table from Router ID */
1465 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1467 (void *)sr_node_new
);
1471 EC_OSPF_SR_NODE_CREATE
,
1472 "SR (%s): Abort! can't create SR node in hash table",
1477 srn
->instance
= ntohl(lsah
->id
.s_addr
);
1479 srn
->srgb
.range_size
= srgb
.range_size
;
1480 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1483 /* Update Algorithm, SRLB and MSD if present */
1486 for (i
= 0; i
< ntohs(algo
->header
.length
); i
++)
1487 srn
->algo
[i
] = algo
->value
[0];
1488 for (; i
< ALGORITHM_COUNT
; i
++)
1489 srn
->algo
[i
] = SR_ALGORITHM_UNSET
;
1491 srn
->algo
[0] = SR_ALGORITHM_SPF
;
1494 if (ri_srlb
!= NULL
) {
1495 srn
->srlb
.range_size
= GET_RANGE_SIZE(ntohl(ri_srlb
->size
));
1496 srn
->srlb
.lower_bound
= GET_LABEL(ntohl(ri_srlb
->lower
.value
));
1499 /* Check if SRGB has changed */
1500 if ((srn
->srgb
.range_size
== srgb
.range_size
)
1501 && (srn
->srgb
.lower_bound
== srgb
.lower_bound
))
1505 srn
->srgb
.range_size
= srgb
.range_size
;
1506 srn
->srgb
.lower_bound
= srgb
.lower_bound
;
1508 osr_debug(" |- Update SR-Node[%pI4], SRGB[%u/%u], SRLB[%u/%u], Algo[%u], MSD[%u]",
1509 &srn
->adv_router
, srn
->srgb
.lower_bound
, srn
->srgb
.range_size
,
1510 srn
->srlb
.lower_bound
, srn
->srlb
.range_size
, srn
->algo
[0],
1513 /* ... and NHLFE if it is a neighbor SR node */
1514 if (srn
->neighbor
== OspfSR
.self
)
1515 hash_iterate(OspfSR
.neighbors
, update_out_nhlfe
, srn
);
1519 * Delete SR Node entry in hash table information corresponding to an expired
1520 * Router Information LSA
1522 void ospf_sr_ri_lsa_delete(struct ospf_lsa
*lsa
)
1524 struct sr_node
*srn
;
1525 struct lsa_header
*lsah
= lsa
->data
;
1527 osr_debug("SR (%s): Remove SR node %pI4 from lsa_id 4.0.0.%u", __func__
,
1528 &lsah
->adv_router
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)));
1531 if (OspfSR
.neighbors
== NULL
) {
1532 flog_err(EC_OSPF_SR_INVALID_DB
,
1533 "SR (%s): Abort! no valid SR Data Base", __func__
);
1537 /* Release Router ID entry in SRDB hash table */
1538 srn
= hash_release(OspfSR
.neighbors
, &(lsah
->adv_router
));
1542 flog_err(EC_OSPF_SR_NODE_CREATE
,
1543 "SR (%s): Abort! no entry in SRDB for SR Node %pI4",
1544 __func__
, &lsah
->adv_router
);
1548 if ((srn
->instance
!= 0) && (srn
->instance
!= ntohl(lsah
->id
.s_addr
))) {
1550 EC_OSPF_SR_INVALID_LSA_ID
,
1551 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4",
1552 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1557 /* Remove SR node */
1561 /* Update Segment Routing from Extended Link LSA */
1562 void ospf_sr_ext_link_lsa_update(struct ospf_lsa
*lsa
)
1564 struct sr_node
*srn
;
1565 struct tlv_header
*tlvh
;
1566 struct lsa_header
*lsah
= lsa
->data
;
1567 struct sr_link
*srl
;
1571 osr_debug("SR (%s): Process Extended Link LSA 8.0.0.%u from %pI4",
1572 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1576 if (OspfSR
.neighbors
== NULL
) {
1577 flog_err(EC_OSPF_SR_INVALID_DB
,
1578 "SR (%s): Abort! no valid SR DataBase", __func__
);
1582 /* Get SR Node in hash table from Router ID */
1583 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1584 (void *)&(lsah
->adv_router
),
1585 (void *)sr_node_new
);
1589 flog_err(EC_OSPF_SR_NODE_CREATE
,
1590 "SR (%s): Abort! can't create SR node in hash table",
1595 /* Initialize TLV browsing */
1596 length
= lsa
->size
- OSPF_LSA_HEADER_SIZE
;
1597 for (tlvh
= TLV_HDR_TOP(lsah
); length
> 0 && tlvh
;
1598 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1599 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1600 /* Got Extended Link information */
1601 srl
= get_ext_link_sid(tlvh
, length
);
1602 /* Update SID if not null */
1604 srl
->instance
= ntohl(lsah
->id
.s_addr
);
1605 update_ext_link_sid(srn
, srl
, lsa
->flags
);
1608 length
-= TLV_SIZE(tlvh
);
1612 /* Delete Segment Routing from Extended Link LSA */
1613 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa
*lsa
)
1615 struct listnode
*node
;
1616 struct sr_link
*srl
;
1617 struct sr_node
*srn
;
1618 struct lsa_header
*lsah
= lsa
->data
;
1619 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1621 osr_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %pI4",
1622 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1626 if (OspfSR
.neighbors
== NULL
) {
1627 flog_err(EC_OSPF_SR_INVALID_DB
,
1628 "SR (%s): Abort! no valid SR DataBase", __func__
);
1632 /* Search SR Node in hash table from Router ID */
1633 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1634 (void *)&(lsah
->adv_router
));
1637 * SR-Node may be NULL if it has been remove previously when
1638 * processing Router Information LSA deletion
1641 flog_err(EC_OSPF_SR_INVALID_DB
,
1642 "SR (%s): Stop! no entry in SRDB for SR Node %pI4",
1643 __func__
, &lsah
->adv_router
);
1647 /* Search for corresponding Segment Link */
1648 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1649 if (srl
->instance
== instance
)
1652 /* Remove Segment Link if found. Note that for Neighbors, only Global
1653 * Adj/Lan-Adj SID are stored in the SR-DB */
1654 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1655 del_adj_sid(srl
->nhlfe
[0]);
1656 del_adj_sid(srl
->nhlfe
[1]);
1657 listnode_delete(srn
->ext_link
, srl
);
1658 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1662 /* Add (LAN)Adjacency-SID from Extended Link Information */
1663 void ospf_sr_ext_itf_add(struct ext_itf
*exti
)
1665 struct sr_node
*srn
= OspfSR
.self
;
1666 struct sr_link
*srl
;
1668 osr_debug("SR (%s): Add Extended Link LSA 8.0.0.%u from self", __func__
,
1675 /* Initialize new Segment Routing Link */
1676 srl
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_link
));
1678 srl
->adv_router
= srn
->adv_router
;
1679 srl
->itf_addr
= exti
->link
.link_data
;
1681 SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA
, exti
->instance
);
1682 srl
->remote_id
= exti
->link
.link_id
;
1683 switch (exti
->stype
) {
1685 srl
->type
= ADJ_SID
;
1686 /* Primary information */
1687 srl
->flags
[0] = exti
->adj_sid
[0].flags
;
1688 if (CHECK_FLAG(exti
->adj_sid
[0].flags
,
1689 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1690 srl
->sid
[0] = GET_LABEL(ntohl(exti
->adj_sid
[0].value
));
1692 srl
->sid
[0] = ntohl(exti
->adj_sid
[0].value
);
1693 if (exti
->rmt_itf_addr
.header
.type
== 0)
1694 srl
->nhlfe
[0].nexthop
= exti
->link
.link_id
;
1696 srl
->nhlfe
[0].nexthop
= exti
->rmt_itf_addr
.value
;
1697 /* Backup Information if set */
1698 if (exti
->adj_sid
[1].header
.type
== 0)
1700 srl
->flags
[1] = exti
->adj_sid
[1].flags
;
1701 if (CHECK_FLAG(exti
->adj_sid
[1].flags
,
1702 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1703 srl
->sid
[1] = GET_LABEL(ntohl(exti
->adj_sid
[1].value
));
1705 srl
->sid
[1] = ntohl(exti
->adj_sid
[1].value
);
1706 if (exti
->rmt_itf_addr
.header
.type
== 0)
1707 srl
->nhlfe
[1].nexthop
= exti
->link
.link_id
;
1709 srl
->nhlfe
[1].nexthop
= exti
->rmt_itf_addr
.value
;
1712 srl
->type
= LAN_ADJ_SID
;
1713 /* Primary information */
1714 srl
->flags
[0] = exti
->lan_sid
[0].flags
;
1715 if (CHECK_FLAG(exti
->lan_sid
[0].flags
,
1716 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1717 srl
->sid
[0] = GET_LABEL(ntohl(exti
->lan_sid
[0].value
));
1719 srl
->sid
[0] = ntohl(exti
->lan_sid
[0].value
);
1720 if (exti
->rmt_itf_addr
.header
.type
== 0)
1721 srl
->nhlfe
[0].nexthop
= exti
->lan_sid
[0].neighbor_id
;
1723 srl
->nhlfe
[0].nexthop
= exti
->rmt_itf_addr
.value
;
1724 /* Backup Information if set */
1725 if (exti
->lan_sid
[1].header
.type
== 0)
1727 srl
->flags
[1] = exti
->lan_sid
[1].flags
;
1728 if (CHECK_FLAG(exti
->lan_sid
[1].flags
,
1729 EXT_SUBTLV_LINK_ADJ_SID_VFLG
))
1730 srl
->sid
[1] = GET_LABEL(ntohl(exti
->lan_sid
[1].value
));
1732 srl
->sid
[1] = ntohl(exti
->lan_sid
[1].value
);
1733 if (exti
->rmt_itf_addr
.header
.type
== 0)
1734 srl
->nhlfe
[1].nexthop
= exti
->lan_sid
[1].neighbor_id
;
1736 srl
->nhlfe
[1].nexthop
= exti
->rmt_itf_addr
.value
;
1740 /* Wrong SID Type. Abort! */
1741 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1745 /* Segment Routing Link is ready, update it */
1746 update_ext_link_sid(srn
, srl
, OSPF_LSA_SELF
);
1749 /* Delete Prefix or (LAN)Adjacency-SID from Extended Link Information */
1750 void ospf_sr_ext_itf_delete(struct ext_itf
*exti
)
1752 struct listnode
*node
;
1753 struct sr_node
*srn
= OspfSR
.self
;
1754 struct sr_prefix
*srp
= NULL
;
1755 struct sr_link
*srl
= NULL
;
1758 osr_debug("SR (%s): Remove Extended LSA %u.0.0.%u from self",
1759 __func__
, exti
->stype
== PREF_SID
? 7 : 8, exti
->instance
);
1761 /* Sanity check: SR-Node and Extended Prefix/Link list may have been
1762 * removed earlier when stopping OSPF or OSPF-SR */
1763 if (srn
== NULL
|| srn
->ext_prefix
== NULL
|| srn
->ext_link
== NULL
)
1766 if (exti
->stype
== PREF_SID
) {
1767 instance
= SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA
,
1769 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1770 if (srp
->instance
== instance
)
1773 /* Uninstall Segment Prefix SID if found */
1774 if ((srp
!= NULL
) && (srp
->instance
== instance
))
1775 ospf_zebra_delete_prefix_sid(srp
);
1777 /* Search for corresponding Segment Link for self SR-Node */
1778 instance
= SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA
,
1780 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
))
1781 if (srl
->instance
== instance
)
1784 /* Remove Segment Link if found */
1785 if ((srl
!= NULL
) && (srl
->instance
== instance
)) {
1786 del_adj_sid(srl
->nhlfe
[0]);
1787 del_adj_sid(srl
->nhlfe
[1]);
1788 listnode_delete(srn
->ext_link
, srl
);
1789 XFREE(MTYPE_OSPF_SR_PARAMS
, srl
);
1794 /* Update Segment Routing from Extended Prefix LSA */
1795 void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa
*lsa
)
1797 struct sr_node
*srn
;
1798 struct tlv_header
*tlvh
;
1799 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1800 struct sr_prefix
*srp
;
1804 osr_debug("SR (%s): Process Extended Prefix LSA 7.0.0.%u from %pI4",
1805 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1809 if (OspfSR
.neighbors
== NULL
) {
1810 flog_err(EC_OSPF_SR_INVALID_DB
,
1811 "SR (%s): Abort! no valid SR DataBase", __func__
);
1815 /* Get SR Node in hash table from Router ID */
1816 srn
= (struct sr_node
*)hash_get(OspfSR
.neighbors
,
1817 (void *)&(lsah
->adv_router
),
1818 (void *)sr_node_new
);
1822 flog_err(EC_OSPF_SR_NODE_CREATE
,
1823 "SR (%s): Abort! can't create SR node in hash table",
1828 /* Initialize TLV browsing */
1829 length
= lsa
->size
- OSPF_LSA_HEADER_SIZE
;
1830 for (tlvh
= TLV_HDR_TOP(lsah
); length
> 0 && tlvh
;
1831 tlvh
= TLV_HDR_NEXT(tlvh
)) {
1832 if (ntohs(tlvh
->type
) == EXT_TLV_LINK
) {
1833 /* Got Extended Link information */
1834 srp
= get_ext_prefix_sid(tlvh
, length
);
1835 /* Update SID if not null */
1837 srp
->instance
= ntohl(lsah
->id
.s_addr
);
1838 update_ext_prefix_sid(srn
, srp
);
1841 length
-= TLV_SIZE(tlvh
);
1845 /* Delete Segment Routing from Extended Prefix LSA */
1846 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa
*lsa
)
1848 struct listnode
*node
;
1849 struct sr_prefix
*srp
;
1850 struct sr_node
*srn
;
1851 struct lsa_header
*lsah
= (struct lsa_header
*)lsa
->data
;
1852 uint32_t instance
= ntohl(lsah
->id
.s_addr
);
1854 osr_debug("SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %pI4",
1855 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1859 if (OspfSR
.neighbors
== NULL
) {
1860 flog_err(EC_OSPF_SR_INVALID_DB
,
1861 "SR (%s): Abort! no valid SR DataBase", __func__
);
1865 /* Search SR Node in hash table from Router ID */
1866 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
1867 (void *)&(lsah
->adv_router
));
1870 * SR-Node may be NULL if it has been remove previously when
1871 * processing Router Information LSA deletion
1874 flog_err(EC_OSPF_SR_INVALID_DB
,
1875 "SR (%s): Stop! no entry in SRDB for SR Node %pI4",
1876 __func__
, &lsah
->adv_router
);
1880 /* Search for corresponding Segment Prefix */
1881 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
))
1882 if (srp
->instance
== instance
)
1885 /* Remove Prefix if found */
1886 if ((srp
!= NULL
) && (srp
->instance
== instance
)) {
1887 ospf_zebra_delete_prefix_sid(srp
);
1888 listnode_delete(srn
->ext_prefix
, srp
);
1889 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
1892 EC_OSPF_SR_INVALID_DB
,
1893 "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %pI4",
1894 __func__
, GET_OPAQUE_ID(ntohl(lsah
->id
.s_addr
)),
1900 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1901 * complete initial CLI command at startup.
1903 * @param ifp - Loopback interface
1904 * @param pref - Prefix address of this interface
1908 void ospf_sr_update_local_prefix(struct interface
*ifp
, struct prefix
*p
)
1910 struct listnode
*node
;
1911 struct sr_prefix
*srp
;
1914 if ((ifp
== NULL
) || (p
== NULL
))
1918 * Search if there is a Segment Prefix that correspond to this
1919 * interface or prefix, and update it if found
1921 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
1922 if ((srp
->nhlfe
.ifindex
== ifp
->ifindex
)
1923 || ((IPV4_ADDR_SAME(&srp
->prefv4
.prefix
, &p
->u
.prefix4
))
1924 && (srp
->prefv4
.prefixlen
== p
->prefixlen
))) {
1926 /* Update Interface & Prefix info */
1927 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
1928 IPV4_ADDR_COPY(&srp
->prefv4
.prefix
, &p
->u
.prefix4
);
1929 srp
->prefv4
.prefixlen
= p
->prefixlen
;
1930 srp
->prefv4
.family
= p
->family
;
1931 IPV4_ADDR_COPY(&srp
->nhlfe
.nexthop
, &p
->u
.prefix4
);
1933 /* OK. Let's Schedule Extended Prefix LSA */
1934 srp
->instance
= ospf_ext_schedule_prefix_index(
1935 ifp
, srp
->sid
, &srp
->prefv4
, srp
->flags
);
1938 " |- Update Node SID %pFX - %u for self SR Node",
1939 (struct prefix
*)&srp
->prefv4
, srp
->sid
);
1941 /* Install SID if NO-PHP is set and not EXPLICIT-NULL */
1942 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)
1943 && !CHECK_FLAG(srp
->flags
,
1944 EXT_SUBTLV_PREFIX_SID_EFLG
)) {
1945 srp
->label_in
= index2label(srp
->sid
,
1947 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
1948 ospf_zebra_update_prefix_sid(srp
);
1955 * Following functions are used to update MPLS LFIB after a SPF run
1958 static void ospf_sr_nhlfe_update(struct hash_bucket
*bucket
, void *args
)
1961 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
1962 struct listnode
*node
;
1963 struct sr_prefix
*srp
;
1967 osr_debug(" |- Update Prefix for SR Node %pI4", &srn
->adv_router
);
1969 /* Skip Self SR Node */
1970 if (srn
== OspfSR
.self
)
1973 /* Update Extended Prefix */
1974 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
1976 /* Keep track of valid route */
1977 old
= srp
->route
!= NULL
;
1979 /* Compute the new NHLFE */
1980 rc
= compute_prefix_nhlfe(srp
);
1982 /* Check computation result */
1984 /* Routes are not know, remove old NHLFE if any to avoid loop */
1987 ospf_zebra_delete_prefix_sid(srp
);
1989 /* Routes exist but are not ready, skip it */
1992 /* There is at least one route, update NHLFE */
1994 ospf_zebra_update_prefix_sid(srp
);
2002 void ospf_sr_update_task(struct ospf
*ospf
)
2005 struct timeval start_time
, stop_time
;
2007 /* Check ospf and SR status */
2008 if ((ospf
== NULL
) || (OspfSR
.status
!= SR_UP
))
2011 monotime(&start_time
);
2013 osr_debug("SR (%s): Start SPF update", __func__
);
2015 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
2016 void *))ospf_sr_nhlfe_update
,
2019 monotime(&stop_time
);
2021 osr_debug("SR (%s): SPF Processing Time(usecs): %lld", __func__
,
2022 (stop_time
.tv_sec
- start_time
.tv_sec
) * 1000000LL
2023 + (stop_time
.tv_usec
- start_time
.tv_usec
));
2027 * --------------------------------------
2028 * Followings are vty command functions.
2029 * --------------------------------------
2033 * Segment Routing Router configuration
2035 * Must be centralize as it concerns both Extended Link/Prefix LSA
2036 * and Router Information LSA. Choose to call it from Extended Prefix
2037 * write_config() call back.
2039 * @param vty VTY output
2043 void ospf_sr_config_write_router(struct vty
*vty
)
2045 struct listnode
*node
;
2046 struct sr_prefix
*srp
;
2049 if (OspfSR
.status
== SR_UP
)
2050 vty_out(vty
, " segment-routing on\n");
2052 upper
= OspfSR
.srgb
.start
+ OspfSR
.srgb
.size
- 1;
2053 if ((OspfSR
.srgb
.start
!= DEFAULT_SRGB_LABEL
)
2054 || (OspfSR
.srgb
.size
!= DEFAULT_SRGB_SIZE
))
2055 vty_out(vty
, " segment-routing global-block %u %u",
2056 OspfSR
.srgb
.start
, upper
);
2058 if ((OspfSR
.srlb
.start
!= DEFAULT_SRLB_LABEL
)
2059 || (OspfSR
.srlb
.end
!= DEFAULT_SRLB_END
))
2060 vty_out(vty
, " local-block %u %u\n", OspfSR
.srlb
.start
,
2065 if (OspfSR
.msd
!= 0)
2066 vty_out(vty
, " segment-routing node-msd %u\n", OspfSR
.msd
);
2068 if (OspfSR
.self
!= NULL
) {
2069 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2070 vty_out(vty
, " segment-routing prefix %pFX index %u",
2071 &srp
->prefv4
, srp
->sid
);
2072 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_EFLG
))
2073 vty_out(vty
, " explicit-null\n");
2074 else if (CHECK_FLAG(srp
->flags
,
2075 EXT_SUBTLV_PREFIX_SID_NPFLG
))
2076 vty_out(vty
, " no-php-flag\n");
2083 DEFUN(ospf_sr_enable
,
2085 "segment-routing on",
2087 "Enable Segment Routing\n")
2090 VTY_DECLVAR_INSTANCE_CONTEXT(ospf
, ospf
);
2092 if (OspfSR
.status
!= SR_OFF
)
2095 if (ospf
->vrf_id
!= VRF_DEFAULT
) {
2097 "Segment Routing is only supported in default VRF\n");
2098 return CMD_WARNING_CONFIG_FAILED
;
2101 osr_debug("SR: Segment Routing: OFF -> ON");
2103 /* Start Segment Routing */
2104 OspfSR
.status
= SR_ON
;
2105 ospf_sr_start(ospf
);
2110 DEFUN (no_ospf_sr_enable
,
2111 no_ospf_sr_enable_cmd
,
2112 "no segment-routing [on]",
2115 "Disable Segment Routing\n")
2118 if (OspfSR
.status
== SR_OFF
)
2121 osr_debug("SR: Segment Routing: ON -> OFF");
2123 /* Start by Disabling Extended Link & Prefix LSA */
2124 ospf_ext_update_sr(false);
2126 /* then, disable Router Information SR parameters */
2127 ospf_router_info_update_sr(false, OspfSR
.self
);
2129 /* Finally, stop Segment Routing */
2135 static int ospf_sr_enabled(struct vty
*vty
)
2137 if (OspfSR
.status
!= SR_OFF
)
2141 vty_out(vty
, "%% OSPF SR is not turned on\n");
2146 /* tell if two ranges [r1_lower, r1_upper] and [r2_lower,r2_upper] overlap */
2147 static bool ranges_overlap(uint32_t r1_lower
, uint32_t r1_upper
,
2148 uint32_t r2_lower
, uint32_t r2_upper
)
2150 return !((r1_upper
< r2_lower
) || (r1_lower
> r2_upper
));
2154 /* tell if a range is valid */
2155 static bool sr_range_is_valid(uint32_t lower
, uint32_t upper
, uint32_t min_size
)
2157 return (upper
>= lower
+ min_size
);
2161 * Update SRGB and/or SRLB using new CLI values.
2163 * @param gb_lower Lower bound of the SRGB
2164 * @param gb_upper Upper bound of the SRGB
2165 * @param lb_lower Lower bound of the SRLB
2166 * @param lb_upper Upper bound of the SRLB
2168 * @return 0 on success, -1 otherwise
2170 static int update_sr_blocks(uint32_t gb_lower
, uint32_t gb_upper
,
2171 uint32_t lb_lower
, uint32_t lb_upper
)
2174 /* Check if values have changed */
2175 bool gb_changed
, lb_changed
;
2176 uint32_t gb_size
= gb_upper
- gb_lower
+ 1;
2177 uint32_t lb_size
= lb_upper
- lb_lower
+ 1;
2180 (OspfSR
.srgb
.size
!= gb_size
|| OspfSR
.srgb
.start
!= gb_lower
);
2182 (OspfSR
.srlb
.end
!= lb_upper
|| OspfSR
.srlb
.start
!= lb_lower
);
2183 if (!gb_changed
&& !lb_changed
)
2186 /* Check if SR is correctly started i.e. Label Manager connected */
2187 if (OspfSR
.status
!= SR_UP
) {
2188 OspfSR
.srgb
.size
= gb_size
;
2189 OspfSR
.srgb
.start
= gb_lower
;
2190 OspfSR
.srlb
.end
= lb_upper
;
2191 OspfSR
.srlb
.start
= lb_lower
;
2195 /* Release old SRGB if it has changed and is active. */
2198 sr_global_block_delete();
2200 /* Set new SRGB values - but do not reserve yet (we need to
2201 * release the SRLB too) */
2202 OspfSR
.srgb
.size
= gb_size
;
2203 OspfSR
.srgb
.start
= gb_lower
;
2204 if (OspfSR
.self
!= NULL
) {
2205 OspfSR
.self
->srgb
.range_size
= gb_size
;
2206 OspfSR
.self
->srgb
.lower_bound
= gb_lower
;
2209 /* Release old SRLB if it has changed and reserve new block as needed.
2213 sr_local_block_delete();
2215 /* Set new SRLB values */
2216 if (sr_local_block_init(lb_lower
, lb_upper
) < 0) {
2220 if (OspfSR
.self
!= NULL
) {
2221 OspfSR
.self
->srlb
.lower_bound
= lb_lower
;
2222 OspfSR
.self
->srlb
.range_size
= lb_size
;
2227 * Try to reserve the new SRGB from the Label Manger. If the
2228 * allocation fails, disable SR until new blocks are successfully
2232 if (sr_global_block_init(OspfSR
.srgb
.start
, OspfSR
.srgb
.size
)
2239 /* Update Self SR-Node */
2240 if (OspfSR
.self
!= NULL
) {
2241 /* SRGB is reserved, set Router Information parameters */
2242 ospf_router_info_update_sr(true, OspfSR
.self
);
2244 /* and update NHLFE entries */
2246 hash_iterate(OspfSR
.neighbors
,
2247 (void (*)(struct hash_bucket
*,
2248 void *))update_in_nhlfe
,
2251 /* and update (LAN)-Adjacency SID */
2253 ospf_ext_link_srlb_update();
2259 DEFUN(sr_global_label_range
, sr_global_label_range_cmd
,
2260 "segment-routing global-block (16-1048575) (16-1048575) [local-block (16-1048575) (16-1048575)]",
2262 "Segment Routing Global Block label range\n"
2263 "Lower-bound range in decimal (16-1048575)\n"
2264 "Upper-bound range in decimal (16-1048575)\n"
2265 "Segment Routing Local Block label range\n"
2266 "Lower-bound range in decimal (16-1048575)\n"
2267 "Upper-bound range in decimal (16-1048575)\n")
2269 uint32_t lb_upper
, lb_lower
;
2270 uint32_t gb_upper
, gb_lower
;
2271 int idx_gb_low
= 2, idx_gb_up
= 3;
2272 int idx_lb_low
= 5, idx_lb_up
= 6;
2274 /* Get lower and upper bound for mandatory global-block */
2275 gb_lower
= strtoul(argv
[idx_gb_low
]->arg
, NULL
, 10);
2276 gb_upper
= strtoul(argv
[idx_gb_up
]->arg
, NULL
, 10);
2278 /* SRLB values are taken from vtysh if there, else use the known ones */
2279 lb_upper
= argc
> idx_lb_up
? strtoul(argv
[idx_lb_up
]->arg
, NULL
, 10)
2281 lb_lower
= argc
> idx_lb_low
? strtoul(argv
[idx_lb_low
]->arg
, NULL
, 10)
2282 : OspfSR
.srlb
.start
;
2284 /* check correctness of input SRGB */
2285 if (!sr_range_is_valid(gb_lower
, gb_upper
, MIN_SRGB_SIZE
)) {
2286 vty_out(vty
, "Invalid SRGB range\n");
2287 return CMD_WARNING_CONFIG_FAILED
;
2290 /* check correctness of SRLB */
2291 if (!sr_range_is_valid(lb_lower
, lb_upper
, MIN_SRLB_SIZE
)) {
2292 vty_out(vty
, "Invalid SRLB range\n");
2293 return CMD_WARNING_CONFIG_FAILED
;
2296 /* Validate SRGB against SRLB */
2297 if (ranges_overlap(gb_lower
, gb_upper
, lb_lower
, lb_upper
)) {
2299 "New SR Global Block (%u/%u) conflicts with Local Block (%u/%u)\n",
2300 gb_lower
, gb_upper
, lb_lower
, lb_upper
);
2301 return CMD_WARNING_CONFIG_FAILED
;
2304 if (update_sr_blocks(gb_lower
, gb_upper
, lb_lower
, lb_upper
) < 0)
2305 return CMD_WARNING_CONFIG_FAILED
;
2310 DEFUN(no_sr_global_label_range
, no_sr_global_label_range_cmd
,
2311 "no segment-routing global-block [(16-1048575) (16-1048575) local-block (16-1048575) (16-1048575)]",
2313 "Segment Routing Global Block label range\n"
2314 "Lower-bound range in decimal (16-1048575)\n"
2315 "Upper-bound range in decimal (16-1048575)\n"
2316 "Segment Routing Local Block label range\n"
2317 "Lower-bound range in decimal (16-1048575)\n"
2318 "Upper-bound range in decimal (16-1048575)\n")
2320 if (update_sr_blocks(DEFAULT_SRGB_LABEL
, DEFAULT_SRGB_END
,
2321 DEFAULT_SRLB_LABEL
, DEFAULT_SRLB_END
)
2323 return CMD_WARNING_CONFIG_FAILED
;
2328 #if CONFDATE > 20220528
2330 "Use of the segment-routing local-block command is deprecated, use the combined global-block command instead")
2333 DEFUN_HIDDEN(sr_local_label_range
, sr_local_label_range_cmd
,
2334 "segment-routing local-block (16-1048575) (16-1048575)",
2336 "Segment Routing Local Block label range\n"
2337 "Lower-bound range in decimal (16-1048575)\n"
2338 "Upper-bound range in decimal (16-1048575)\n")
2342 uint32_t srgb_upper
;
2346 /* Get lower and upper bound */
2347 lower
= strtoul(argv
[idx_low
]->arg
, NULL
, 10);
2348 upper
= strtoul(argv
[idx_up
]->arg
, NULL
, 10);
2350 /* check correctness of SRLB */
2351 if (!sr_range_is_valid(lower
, upper
, MIN_SRLB_SIZE
)) {
2352 vty_out(vty
, "Invalid SRLB range\n");
2353 return CMD_WARNING_CONFIG_FAILED
;
2356 /* Check if values have changed */
2357 if ((OspfSR
.srlb
.start
== lower
)
2358 && (OspfSR
.srlb
.end
== upper
))
2361 /* Validate SRLB against SRGB */
2362 srgb_upper
= OspfSR
.srgb
.start
+ OspfSR
.srgb
.size
- 1;
2364 if (ranges_overlap(OspfSR
.srgb
.start
, srgb_upper
, lower
, upper
)) {
2366 "New SR Local Block (%u/%u) conflicts with Global Block (%u/%u)\n",
2367 lower
, upper
, OspfSR
.srgb
.start
, srgb_upper
);
2368 return CMD_WARNING_CONFIG_FAILED
;
2371 if (update_sr_blocks(OspfSR
.srgb
.start
, srgb_upper
, lower
, upper
) < 0)
2372 return CMD_WARNING_CONFIG_FAILED
;
2377 DEFUN_HIDDEN(no_sr_local_label_range
, no_sr_local_label_range_cmd
,
2378 "no segment-routing local-block [(16-1048575) (16-1048575)]",
2380 "Segment Routing Local Block label range\n"
2381 "Lower-bound range in decimal (16-1048575)\n"
2382 "Upper-bound range in decimal (16-1048575)\n")
2387 /* Validate SRLB against SRGB */
2388 srgb_end
= OspfSR
.srgb
.start
+ OspfSR
.srgb
.size
- 1;
2389 if (ranges_overlap(OspfSR
.srgb
.start
, srgb_end
, DEFAULT_SRLB_LABEL
,
2390 DEFAULT_SRLB_END
)) {
2392 "New SR Local Block (%u/%u) conflicts with Global Block (%u/%u)\n",
2393 DEFAULT_SRLB_LABEL
, DEFAULT_SRLB_END
, OspfSR
.srgb
.start
,
2395 return CMD_WARNING_CONFIG_FAILED
;
2398 if (update_sr_blocks(OspfSR
.srgb
.start
, srgb_end
, DEFAULT_SRLB_LABEL
,
2401 return CMD_WARNING_CONFIG_FAILED
;
2408 "segment-routing node-msd (1-16)",
2410 "Maximum Stack Depth for this router\n"
2411 "Maximum number of label that could be stack (1-16)\n")
2416 if (!ospf_sr_enabled(vty
))
2417 return CMD_WARNING_CONFIG_FAILED
;
2420 argv_find(argv
, argc
, "(1-16)", &idx
);
2421 msd
= strtoul(argv
[idx
]->arg
, NULL
, 10);
2422 if (msd
< 1 || msd
> MPLS_MAX_LABELS
) {
2423 vty_out(vty
, "MSD must be comprise between 1 and %u\n",
2425 return CMD_WARNING_CONFIG_FAILED
;
2428 /* Check if value has changed */
2429 if (OspfSR
.msd
== msd
)
2432 /* Set this router MSD */
2434 if (OspfSR
.self
!= NULL
) {
2435 OspfSR
.self
->msd
= msd
;
2437 /* Set Router Information parameters if SR is UP */
2438 if (OspfSR
.status
== SR_UP
)
2439 ospf_router_info_update_sr(true, OspfSR
.self
);
2445 DEFUN (no_sr_node_msd
,
2447 "no segment-routing node-msd [(1-16)]",
2450 "Maximum Stack Depth for this router\n"
2451 "Maximum number of label that could be stack (1-16)\n")
2454 if (!ospf_sr_enabled(vty
))
2455 return CMD_WARNING_CONFIG_FAILED
;
2457 /* unset this router MSD */
2459 if (OspfSR
.self
!= NULL
) {
2460 OspfSR
.self
->msd
= 0;
2462 /* Set Router Information parameters if SR is UP */
2463 if (OspfSR
.status
== SR_UP
)
2464 ospf_router_info_update_sr(true, OspfSR
.self
);
2470 DEFUN (sr_prefix_sid
,
2472 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag|explicit-null]",
2475 "IPv4 Prefix as A.B.C.D/M\n"
2476 "SID index for this prefix in decimal (0-65535)\n"
2477 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2478 "Don't request Penultimate Hop Popping (PHP)\n"
2479 "Upstream neighbor must replace prefix-sid with explicit null label\n")
2482 struct prefix p
, pexist
;
2484 struct listnode
*node
;
2485 struct sr_prefix
*srp
, *exist
= NULL
;
2486 struct interface
*ifp
;
2487 bool no_php_flag
= false;
2488 bool exp_null
= false;
2489 bool index_in_use
= false;
2490 uint8_t desired_flags
= 0;
2492 if (!ospf_sr_enabled(vty
))
2493 return CMD_WARNING_CONFIG_FAILED
;
2495 /* Get network prefix */
2496 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2497 if (!str2prefix(argv
[idx
]->arg
, &p
)) {
2498 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2499 return CMD_WARNING_CONFIG_FAILED
;
2502 /* Get & verify index value */
2503 argv_find(argv
, argc
, "(0-65535)", &idx
);
2504 index
= strtoul(argv
[idx
]->arg
, NULL
, 10);
2505 if (index
> OspfSR
.srgb
.size
- 1) {
2506 vty_out(vty
, "Index %u must be lower than range size %u\n",
2507 index
, OspfSR
.srgb
.size
);
2508 return CMD_WARNING_CONFIG_FAILED
;
2512 no_php_flag
= argv_find(argv
, argc
, "no-php-flag", &idx
);
2513 exp_null
= argv_find(argv
, argc
, "explicit-null", &idx
);
2515 desired_flags
|= no_php_flag
? EXT_SUBTLV_PREFIX_SID_NPFLG
: 0;
2516 desired_flags
|= exp_null
? EXT_SUBTLV_PREFIX_SID_NPFLG
: 0;
2517 desired_flags
|= exp_null
? EXT_SUBTLV_PREFIX_SID_EFLG
: 0;
2519 /* Search for an existing Prefix-SID */
2520 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
)) {
2521 if (prefix_same((struct prefix
*)&srp
->prefv4
, &p
))
2523 if (srp
->sid
== index
) {
2524 index_in_use
= true;
2529 /* done if prefix segment already there with same index and flags */
2530 if (exist
&& exist
->sid
== index
&& exist
->flags
== desired_flags
)
2533 /* deny if index is already in use by a distinct prefix */
2534 if (!exist
&& index_in_use
) {
2535 vty_out(vty
, "Index %u is already used by %pFX\n", index
,
2537 return CMD_WARNING_CONFIG_FAILED
;
2540 /* First, remove old NHLFE if installed */
2541 if (exist
&& CHECK_FLAG(exist
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)
2542 && !CHECK_FLAG(exist
->flags
, EXT_SUBTLV_PREFIX_SID_EFLG
))
2543 ospf_zebra_delete_prefix_sid(exist
);
2545 /* Create new Extended Prefix to SRDB if not found */
2546 if (exist
== NULL
) {
2547 srp
= XCALLOC(MTYPE_OSPF_SR_PARAMS
, sizeof(struct sr_prefix
));
2548 IPV4_ADDR_COPY(&srp
->prefv4
.prefix
, &p
.u
.prefix4
);
2549 srp
->prefv4
.prefixlen
= p
.prefixlen
;
2550 srp
->prefv4
.family
= p
.family
;
2552 srp
->type
= LOCAL_SID
;
2554 /* we work on the existing SR prefix */
2558 /* Reset labels to handle flag update */
2560 srp
->nhlfe
.label_out
= 0;
2562 srp
->flags
= desired_flags
;
2564 /* If NO PHP flag is present, compute NHLFE and set label */
2566 srp
->label_in
= index2label(srp
->sid
, OspfSR
.self
->srgb
);
2567 srp
->nhlfe
.label_out
= MPLS_LABEL_IMPLICIT_NULL
;
2570 osr_debug("SR (%s): Add new index %u to Prefix %pFX", __func__
, index
,
2571 (struct prefix
*)&srp
->prefv4
);
2573 /* Get Interface and check if it is a Loopback */
2574 ifp
= if_lookup_prefix(&p
, VRF_DEFAULT
);
2577 * Interface could be not yet available i.e. when this
2578 * command is in the configuration file, OSPF is not yet
2579 * ready. In this case, store the prefix SID for latter
2580 * update of this Extended Prefix
2583 listnode_add(OspfSR
.self
->ext_prefix
, srp
);
2585 "Interface for prefix %pFX not found. Deferred LSA flooding",
2590 if (!if_is_loopback(ifp
)) {
2591 vty_out(vty
, "interface %s is not a Loopback\n", ifp
->name
);
2592 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2593 return CMD_WARNING_CONFIG_FAILED
;
2595 srp
->nhlfe
.ifindex
= ifp
->ifindex
;
2597 /* Add SR Prefix if new */
2599 listnode_add(OspfSR
.self
->ext_prefix
, srp
);
2601 /* Update Prefix SID if SR is UP */
2602 if (OspfSR
.status
== SR_UP
) {
2603 if (no_php_flag
&& !exp_null
)
2604 ospf_zebra_update_prefix_sid(srp
);
2608 /* Finally, update Extended Prefix LSA id SR is UP */
2609 srp
->instance
= ospf_ext_schedule_prefix_index(
2610 ifp
, srp
->sid
, &srp
->prefv4
, srp
->flags
);
2611 if (srp
->instance
== 0) {
2612 vty_out(vty
, "Unable to set index %u for prefix %pFX\n",
2620 DEFUN (no_sr_prefix_sid
,
2621 no_sr_prefix_sid_cmd
,
2622 "no segment-routing prefix A.B.C.D/M [index (0-65535)|no-php-flag|explicit-null]",
2626 "IPv4 Prefix as A.B.C.D/M\n"
2627 "SID index for this prefix in decimal (0-65535)\n"
2628 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2629 "Don't request Penultimate Hop Popping (PHP)\n"
2630 "Upstream neighbor must replace prefix-sid with explicit null label\n")
2634 struct listnode
*node
;
2635 struct sr_prefix
*srp
;
2636 struct interface
*ifp
;
2640 if (!ospf_sr_enabled(vty
))
2641 return CMD_WARNING_CONFIG_FAILED
;
2643 if (OspfSR
.status
!= SR_UP
)
2646 /* Get network prefix */
2647 argv_find(argv
, argc
, "A.B.C.D/M", &idx
);
2648 rc
= str2prefix(argv
[idx
]->arg
, &p
);
2650 vty_out(vty
, "Invalid prefix format %s\n", argv
[idx
]->arg
);
2651 return CMD_WARNING_CONFIG_FAILED
;
2654 /* check that the prefix is already set */
2655 for (ALL_LIST_ELEMENTS_RO(OspfSR
.self
->ext_prefix
, node
, srp
))
2656 if (IPV4_ADDR_SAME(&srp
->prefv4
.prefix
, &p
.u
.prefix4
)
2657 && (srp
->prefv4
.prefixlen
== p
.prefixlen
)) {
2663 vty_out(vty
, "Prefix %s is not found. Abort!\n",
2665 return CMD_WARNING_CONFIG_FAILED
;
2669 ifp
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2671 vty_out(vty
, "interface for prefix %s not found.\n",
2673 return CMD_WARNING_CONFIG_FAILED
;
2676 /* Update Extended Prefix LSA */
2677 if (!ospf_ext_schedule_prefix_index(ifp
, 0, NULL
, 0)) {
2678 vty_out(vty
, "No corresponding loopback interface. Abort!\n");
2682 osr_debug("SR (%s): Remove Prefix %pFX with index %u", __func__
,
2683 (struct prefix
*)&srp
->prefv4
, srp
->sid
);
2685 /* Delete NHLFE if NO-PHP is set and EXPLICIT NULL not set */
2686 if (CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_NPFLG
)
2687 && !CHECK_FLAG(srp
->flags
, EXT_SUBTLV_PREFIX_SID_EFLG
))
2688 ospf_zebra_delete_prefix_sid(srp
);
2690 /* OK, all is clean, remove SRP from SRDB */
2691 listnode_delete(OspfSR
.self
->ext_prefix
, srp
);
2692 XFREE(MTYPE_OSPF_SR_PARAMS
, srp
);
2698 static char *sr_op2str(char *buf
, size_t size
, mpls_label_t label_in
,
2699 mpls_label_t label_out
)
2704 switch (label_out
) {
2705 case MPLS_LABEL_IMPLICIT_NULL
:
2706 snprintf(buf
, size
, "Pop(%u)", label_in
);
2708 case MPLS_LABEL_IPV4_EXPLICIT_NULL
:
2709 if (label_in
== MPLS_LABEL_IPV4_EXPLICIT_NULL
)
2710 snprintf(buf
, size
, "no-op.");
2712 snprintf(buf
, size
, "Swap(%u, null)", label_in
);
2714 case MPLS_INVALID_LABEL
:
2715 snprintf(buf
, size
, "no-op.");
2718 snprintf(buf
, size
, "Swap(%u, %u)", label_in
, label_out
);
2724 static void show_sr_prefix(struct sbuf
*sbuf
, struct json_object
*json
,
2725 struct sr_prefix
*srp
)
2728 struct listnode
*node
;
2729 struct ospf_path
*path
;
2730 struct interface
*itf
;
2731 json_object
*json_route
= NULL
, *json_obj
;
2735 char buf
[PREFIX_STRLEN
];
2738 snprintfrr(pref
, 19, "%pFX", (struct prefix
*)&srp
->prefv4
);
2739 snprintf(sid
, 22, "SR Pfx (idx %u)", srp
->sid
);
2741 json_object_string_add(json
, "prefix", pref
);
2742 json_object_int_add(json
, "sid", srp
->sid
);
2743 json_object_int_add(json
, "inputLabel", srp
->label_in
);
2745 sbuf_push(sbuf
, 0, "%18s %21s ", pref
, sid
);
2748 /* Check if it is a Local Node SID */
2749 if (srp
->type
== LOCAL_SID
) {
2750 itf
= if_lookup_by_index(srp
->nhlfe
.ifindex
, VRF_DEFAULT
);
2753 json_route
= json_object_new_array();
2754 json_object_object_add(json
, "prefixRoute",
2757 json_obj
= json_object_new_object();
2758 json_object_int_add(json_obj
, "outputLabel",
2759 srp
->nhlfe
.label_out
);
2760 json_object_string_add(json_obj
, "interface",
2761 itf
? itf
->name
: "-");
2762 json_object_string_addf(json_obj
, "nexthop", "%pI4",
2763 &srp
->nhlfe
.nexthop
);
2764 json_object_array_add(json_route
, json_obj
);
2766 sbuf_push(sbuf
, 0, "%20s %9s %15s\n",
2767 sr_op2str(op
, 32, srp
->label_in
,
2768 srp
->nhlfe
.label_out
),
2769 itf
? itf
->name
: "-",
2770 inet_ntop(AF_INET
, &srp
->nhlfe
.nexthop
,
2776 /* Check if we have a valid path for this prefix */
2777 if (srp
->route
== NULL
) {
2779 sbuf_push(sbuf
, 0, "\n");
2784 /* Process list of OSPF paths */
2785 for (ALL_LIST_ELEMENTS_RO(srp
->route
->paths
, node
, path
)) {
2786 itf
= if_lookup_by_index(path
->ifindex
, VRF_DEFAULT
);
2789 json_route
= json_object_new_array();
2790 json_object_object_add(json
, "prefixRoute",
2793 json_obj
= json_object_new_object();
2794 json_object_int_add(json_obj
, "outputLabel",
2795 path
->srni
.label_out
);
2796 json_object_string_add(json_obj
, "interface",
2797 itf
? itf
->name
: "-");
2798 json_object_string_addf(json_obj
, "nexthop", "%pI4",
2800 json_object_array_add(json_route
, json_obj
);
2802 sbuf_push(sbuf
, indent
, "%20s %9s %15s\n",
2803 sr_op2str(op
, 32, srp
->label_in
,
2804 path
->srni
.label_out
),
2805 itf
? itf
->name
: "-",
2806 inet_ntop(AF_INET
, &path
->nexthop
, buf
,
2808 /* Offset to align information for ECMP */
2814 static void show_sr_node(struct vty
*vty
, struct json_object
*json
,
2815 struct sr_node
*srn
)
2818 struct listnode
*node
;
2819 struct sr_link
*srl
;
2820 struct sr_prefix
*srp
;
2821 struct interface
*itf
;
2826 char buf
[PREFIX_STRLEN
];
2828 json_object
*json_node
= NULL
, *json_algo
, *json_obj
;
2829 json_object
*json_prefix
= NULL
, *json_link
= NULL
;
2835 sbuf_init(&sbuf
, NULL
, 0);
2838 json_node
= json_object_new_object();
2839 json_object_string_addf(json_node
, "routerID", "%pI4",
2841 json_object_int_add(json_node
, "srgbSize",
2842 srn
->srgb
.range_size
);
2843 json_object_int_add(json_node
, "srgbLabel",
2844 srn
->srgb
.lower_bound
);
2845 json_object_int_add(json_node
, "srlbSize",
2846 srn
->srlb
.range_size
);
2847 json_object_int_add(json_node
, "srlbLabel",
2848 srn
->srlb
.lower_bound
);
2849 json_algo
= json_object_new_array();
2850 json_object_object_add(json_node
, "algorithms", json_algo
);
2851 for (int i
= 0; i
< ALGORITHM_COUNT
; i
++) {
2852 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2854 json_obj
= json_object_new_object();
2857 snprintf(tmp
, sizeof(tmp
), "%u", i
);
2858 json_object_string_add(json_obj
, tmp
,
2859 srn
->algo
[i
] == SR_ALGORITHM_SPF
2862 json_object_array_add(json_algo
, json_obj
);
2865 json_object_int_add(json_node
, "nodeMsd", srn
->msd
);
2867 sbuf_push(&sbuf
, 0, "SR-Node: %pI4", &srn
->adv_router
);
2868 upper
= srn
->srgb
.lower_bound
+ srn
->srgb
.range_size
- 1;
2869 sbuf_push(&sbuf
, 0, "\tSRGB: [%u/%u]",
2870 srn
->srgb
.lower_bound
, upper
);
2871 upper
= srn
->srlb
.lower_bound
+ srn
->srlb
.range_size
- 1;
2872 sbuf_push(&sbuf
, 0, "\tSRLB: [%u/%u]",
2873 srn
->srlb
.lower_bound
, upper
);
2874 sbuf_push(&sbuf
, 0, "\tAlgo.(s): %s",
2875 srn
->algo
[0] == SR_ALGORITHM_SPF
? "SPF" : "S-SPF");
2876 for (int i
= 1; i
< ALGORITHM_COUNT
; i
++) {
2877 if (srn
->algo
[i
] == SR_ALGORITHM_UNSET
)
2879 sbuf_push(&sbuf
, 0, "/%s",
2880 srn
->algo
[i
] == SR_ALGORITHM_SPF
? "SPF"
2884 sbuf_push(&sbuf
, 0, "\tMSD: %u", srn
->msd
);
2889 "\n\n Prefix or Link Node or Adj. SID Label Operation Interface Nexthop\n");
2891 "------------------ --------------------- -------------------- --------- ---------------\n");
2893 for (ALL_LIST_ELEMENTS_RO(srn
->ext_prefix
, node
, srp
)) {
2896 json_prefix
= json_object_new_array();
2897 json_object_object_add(json_node
,
2901 json_obj
= json_object_new_object();
2902 show_sr_prefix(NULL
, json_obj
, srp
);
2903 json_object_array_add(json_prefix
, json_obj
);
2905 show_sr_prefix(&sbuf
, NULL
, srp
);
2909 for (ALL_LIST_ELEMENTS_RO(srn
->ext_link
, node
, srl
)) {
2910 snprintfrr(pref
, 19, "%pI4/32", &srl
->itf_addr
);
2911 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[0]);
2912 itf
= if_lookup_by_index(srl
->nhlfe
[0].ifindex
, VRF_DEFAULT
);
2915 json_link
= json_object_new_array();
2916 json_object_object_add(
2917 json_node
, "extendedLink", json_link
);
2920 json_obj
= json_object_new_object();
2921 json_object_string_add(json_obj
, "prefix", pref
);
2922 json_object_int_add(json_obj
, "sid", srl
->sid
[0]);
2923 json_object_int_add(json_obj
, "inputLabel",
2924 srl
->nhlfe
[0].label_in
);
2925 json_object_int_add(json_obj
, "outputLabel",
2926 srl
->nhlfe
[0].label_out
);
2927 json_object_string_add(json_obj
, "interface",
2928 itf
? itf
->name
: "-");
2929 json_object_string_addf(json_obj
, "nexthop", "%pI4",
2930 &srl
->nhlfe
[0].nexthop
);
2931 json_object_array_add(json_link
, json_obj
);
2933 json_obj
= json_object_new_object();
2934 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2935 json_object_string_add(json_obj
, "prefix", pref
);
2936 json_object_int_add(json_obj
, "sid", srl
->sid
[1]);
2937 json_object_int_add(json_obj
, "inputLabel",
2938 srl
->nhlfe
[1].label_in
);
2939 json_object_int_add(json_obj
, "outputLabel",
2940 srl
->nhlfe
[1].label_out
);
2941 json_object_string_add(json_obj
, "interface",
2942 itf
? itf
->name
: "-");
2943 json_object_string_addf(json_obj
, "nexthop", "%pI4",
2944 &srl
->nhlfe
[1].nexthop
);
2945 json_object_array_add(json_link
, json_obj
);
2947 sbuf_push(&sbuf
, 0, "%18s %21s %20s %9s %15s\n",
2949 sr_op2str(op
, 32, srl
->nhlfe
[0].label_in
,
2950 srl
->nhlfe
[0].label_out
),
2951 itf
? itf
->name
: "-",
2952 inet_ntop(AF_INET
, &srl
->nhlfe
[0].nexthop
,
2954 snprintf(sid
, 22, "SR Adj. (lbl %u)", srl
->sid
[1]);
2955 sbuf_push(&sbuf
, 0, "%18s %21s %20s %9s %15s\n",
2957 sr_op2str(op
, 32, srl
->nhlfe
[1].label_in
,
2958 srl
->nhlfe
[1].label_out
),
2959 itf
? itf
->name
: "-",
2960 inet_ntop(AF_INET
, &srl
->nhlfe
[1].nexthop
,
2965 json_object_array_add(json
, json_node
);
2967 vty_out(vty
, "%s\n", sbuf_buf(&sbuf
));
2972 static void show_vty_srdb(struct hash_bucket
*bucket
, void *args
)
2974 struct vty
*vty
= (struct vty
*)args
;
2975 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
2977 show_sr_node(vty
, NULL
, srn
);
2980 static void show_json_srdb(struct hash_bucket
*bucket
, void *args
)
2982 struct json_object
*json
= (struct json_object
*)args
;
2983 struct sr_node
*srn
= (struct sr_node
*)bucket
->data
;
2985 show_sr_node(NULL
, json
, srn
);
2988 DEFUN (show_ip_opsf_srdb
,
2989 show_ip_ospf_srdb_cmd
,
2990 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2994 "Database summary\n"
2995 "Show Segment Routing Data Base\n"
2996 "Advertising SR node\n"
2997 "Advertising SR node ID (as an IP address)\n"
2998 "Self-originated SR node\n"
3003 struct sr_node
*srn
;
3004 bool uj
= use_json(argc
, argv
);
3005 json_object
*json
= NULL
, *json_node_array
= NULL
;
3007 if (OspfSR
.status
== SR_OFF
) {
3008 vty_out(vty
, "Segment Routing is disabled on this router\n");
3013 json
= json_object_new_object();
3014 json_node_array
= json_object_new_array();
3015 json_object_string_addf(json
, "srdbID", "%pI4",
3016 &OspfSR
.self
->adv_router
);
3017 json_object_object_add(json
, "srNodes", json_node_array
);
3020 "\n\t\tOSPF Segment Routing database for ID %pI4\n\n",
3021 &OspfSR
.self
->adv_router
);
3024 if (argv_find(argv
, argc
, "self-originate", &idx
)) {
3026 show_sr_node(vty
, json_node_array
, srn
);
3028 vty_json(vty
, json
);
3032 if (argv_find(argv
, argc
, "A.B.C.D", &idx
)) {
3033 if (!inet_aton(argv
[idx
]->arg
, &rid
)) {
3034 vty_out(vty
, "Specified Router ID %s is invalid\n",
3036 return CMD_WARNING_CONFIG_FAILED
;
3038 /* Get the SR Node from the SRDB */
3039 srn
= (struct sr_node
*)hash_lookup(OspfSR
.neighbors
,
3041 show_sr_node(vty
, json_node_array
, srn
);
3043 vty_json(vty
, json
);
3047 /* No parameters have been provided, Iterate through all the SRDB */
3049 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
3050 void *))show_json_srdb
,
3051 (void *)json_node_array
);
3052 vty_json(vty
, json
);
3054 hash_iterate(OspfSR
.neighbors
, (void (*)(struct hash_bucket
*,
3055 void *))show_vty_srdb
,
3061 /* Install new CLI commands */
3062 void ospf_sr_register_vty(void)
3064 install_element(VIEW_NODE
, &show_ip_ospf_srdb_cmd
);
3066 install_element(OSPF_NODE
, &ospf_sr_enable_cmd
);
3067 install_element(OSPF_NODE
, &no_ospf_sr_enable_cmd
);
3068 install_element(OSPF_NODE
, &sr_global_label_range_cmd
);
3069 install_element(OSPF_NODE
, &no_sr_global_label_range_cmd
);
3070 install_element(OSPF_NODE
, &sr_local_label_range_cmd
);
3071 install_element(OSPF_NODE
, &no_sr_local_label_range_cmd
);
3072 install_element(OSPF_NODE
, &sr_node_msd_cmd
);
3073 install_element(OSPF_NODE
, &no_sr_node_msd_cmd
);
3074 install_element(OSPF_NODE
, &sr_prefix_sid_cmd
);
3075 install_element(OSPF_NODE
, &no_sr_prefix_sid_cmd
);