]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_sr.c
Merge pull request #5468 from qlyoung/bgpd-remove-bgp-attr-dup
[mirror_frr.git] / ospfd / ospf_sr.c
CommitLineData
cf9b9f77
OD
1/*
2 * This is an implementation of Segment Routing
7743f2f8 3 * as per draft draft-ietf-ospf-segment-routing-extensions-24
cf9b9f77
OD
4 *
5 * Module name: Segment Routing
6 *
cf9b9f77 7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
7743f2f8 8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
cf9b9f77 9 *
7743f2f8 10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
cf9b9f77 11 *
7743f2f8
OD
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)
15 * any later version.
cf9b9f77 16 *
7743f2f8
OD
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
20 * more details.
cf9b9f77 21 *
7743f2f8
OD
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
cf9b9f77
OD
25 */
26
b45ac5f5
DL
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
cf9b9f77
OD
31#include <math.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <zebra.h>
35
36#include "command.h"
37#include "hash.h"
38#include "if.h"
39#include "if.h"
40#include "jhash.h"
41#include "libospf.h" /* for ospf interface types */
42#include "linklist.h"
43#include "log.h"
44#include "memory.h"
45#include "monotime.h"
46#include "network.h"
47#include "prefix.h"
48#include "sockunion.h" /* for inet_aton() */
49#include "stream.h"
50#include "table.h"
51#include "thread.h"
52#include "vty.h"
53#include "zclient.h"
76665890 54#include <lib/json.h>
313d7993 55#include "ospf_errors.h"
cf9b9f77
OD
56
57#include "ospfd/ospfd.h"
58#include "ospfd/ospf_interface.h"
59#include "ospfd/ospf_ism.h"
60#include "ospfd/ospf_asbr.h"
61#include "ospfd/ospf_lsa.h"
62#include "ospfd/ospf_lsdb.h"
63#include "ospfd/ospf_neighbor.h"
64#include "ospfd/ospf_nsm.h"
65#include "ospfd/ospf_flood.h"
66#include "ospfd/ospf_packet.h"
67#include "ospfd/ospf_spf.h"
68#include "ospfd/ospf_dump.h"
69#include "ospfd/ospf_route.h"
70#include "ospfd/ospf_ase.h"
71#include "ospfd/ospf_sr.h"
72#include "ospfd/ospf_ri.h"
73#include "ospfd/ospf_ext.h"
74#include "ospfd/ospf_zebra.h"
75
76/*
77 * Global variable to manage Segment Routing on this node.
78 * Note that all parameter values are stored in network byte order.
79 */
80static struct ospf_sr_db OspfSR;
81static void ospf_sr_register_vty(void);
82static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe);
83
84/*
85 * Segment Routing Data Base functions
86 */
87
88/* Hash function for Segment Routing entry */
d8b87afe 89static unsigned int sr_hash(const void *p)
cf9b9f77
OD
90{
91 const struct in_addr *rid = p;
92
7743f2f8 93 return jhash_1word(rid->s_addr, 0);
cf9b9f77
OD
94}
95
96/* Compare 2 Router ID hash entries based on SR Node */
74df8d6d 97static bool sr_cmp(const void *p1, const void *p2)
cf9b9f77
OD
98{
99 const struct sr_node *srn = p1;
100 const struct in_addr *rid = p2;
101
7743f2f8 102 return IPV4_ADDR_SAME(&srn->adv_router, rid);
cf9b9f77
OD
103}
104
fd3b19f2
OD
105/* Functions to remove an SR Link */
106static void del_sr_link(void *val)
cf9b9f77 107{
fd3b19f2
OD
108 struct sr_link *srl = (struct sr_link *)val;
109
110 del_sid_nhlfe(srl->nhlfe[0]);
111 del_sid_nhlfe(srl->nhlfe[1]);
112 XFREE(MTYPE_OSPF_SR_PARAMS, val);
fd3b19f2
OD
113}
114
115/* Functions to remove an SR Prefix */
116static void del_sr_pref(void *val)
117{
118 struct sr_prefix *srp = (struct sr_prefix *)val;
119
120 del_sid_nhlfe(srp->nhlfe);
cf9b9f77 121 XFREE(MTYPE_OSPF_SR_PARAMS, val);
cf9b9f77
OD
122}
123
124/* Allocate new Segment Routine node */
125static struct sr_node *sr_node_new(struct in_addr *rid)
126{
127
128 if (rid == NULL)
129 return NULL;
130
131 struct sr_node *new;
132
133 /* Allocate Segment Routing node memory */
134 new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_node));
135
cf9b9f77
OD
136 /* Default Algorithm, SRGB and MSD */
137 for (int i = 0; i < ALGORITHM_COUNT; i++)
fd3b19f2 138 new->algo[i] = SR_ALGORITHM_UNSET;
cf9b9f77
OD
139
140 new->srgb.range_size = 0;
141 new->srgb.lower_bound = 0;
142 new->msd = 0;
143
144 /* Create Link, Prefix and Range TLVs list */
145 new->ext_link = list_new();
146 new->ext_prefix = list_new();
fd3b19f2
OD
147 new->ext_link->del = del_sr_link;
148 new->ext_prefix->del = del_sr_pref;
cf9b9f77 149
cf9b9f77
OD
150 IPV4_ADDR_COPY(&new->adv_router, rid);
151 new->neighbor = NULL;
152 new->instance = 0;
153
154 if (IS_DEBUG_OSPF_SR)
155 zlog_debug(" |- Created new SR node for %s",
156 inet_ntoa(new->adv_router));
157 return new;
158}
159
160/* Delete Segment Routing node */
161static void sr_node_del(struct sr_node *srn)
162{
cf9b9f77
OD
163 /* Sanity Check */
164 if (srn == NULL)
165 return;
166
167 /* Clean Extended Link */
6a154c88 168 list_delete(&srn->ext_link);
cf9b9f77
OD
169
170 /* Clean Prefix List */
6a154c88 171 list_delete(&srn->ext_prefix);
cf9b9f77
OD
172
173 XFREE(MTYPE_OSPF_SR_PARAMS, srn);
174}
175
176/* Get SR Node for a given nexthop */
177static struct sr_node *get_sr_node_by_nexthop(struct ospf *ospf,
178 struct in_addr nexthop)
179{
180 struct ospf_interface *oi = NULL;
181 struct ospf_neighbor *nbr = NULL;
182 struct listnode *node;
183 struct route_node *rn;
184 struct sr_node *srn;
7743f2f8 185 bool found;
cf9b9f77
OD
186
187 /* Sanity check */
188 if (OspfSR.neighbors == NULL)
189 return NULL;
190
191 if (IS_DEBUG_OSPF_SR)
192 zlog_debug(" |- Search SR-Node for nexthop %s",
193 inet_ntoa(nexthop));
194
195 /* First, search neighbor Router ID for this nexthop */
7743f2f8
OD
196 found = false;
197 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
198 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
199 nbr = rn->info;
200 if ((nbr) && (IPV4_ADDR_SAME(&nexthop, &nbr->src))) {
201 found = true;
cf9b9f77 202 break;
7743f2f8
OD
203 }
204 }
205 if (found)
206 break;
207 }
cf9b9f77 208
7743f2f8 209 if (!found)
cf9b9f77
OD
210 return NULL;
211
212 if (IS_DEBUG_OSPF_SR)
213 zlog_debug(" |- Found nexthop Router ID %s",
214 inet_ntoa(nbr->router_id));
215 /* Then, search SR Node */
216 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, &nbr->router_id);
217
218 return srn;
219}
220
221/*
222 * Segment Routing Initialization functions
223 */
224
225/* Segment Routing starter function */
226static int ospf_sr_start(struct ospf *ospf)
227{
228 struct route_node *rn;
229 struct ospf_lsa *lsa;
230 struct sr_node *srn;
231 int rc = 0;
232
233 if (IS_DEBUG_OSPF_SR)
7743f2f8 234 zlog_debug("SR (%s): Start Segment Routing", __func__);
cf9b9f77
OD
235
236 /* Initialize self SR Node */
237 srn = hash_get(OspfSR.neighbors, (void *)&(ospf->router_id),
238 (void *)sr_node_new);
239
cf9b9f77
OD
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;
245 OspfSR.self = srn;
246
247 if (IS_DEBUG_OSPF_EVENT)
7743f2f8 248 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__);
cf9b9f77
OD
249
250 /* Start by looking to Router Info & Extended LSA in lsdb */
251 if ((ospf != NULL) && (ospf->backbone != NULL)) {
996c9314 252 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf->backbone), rn, lsa) {
cf9b9f77
OD
253 if (IS_LSA_MAXAGE(lsa) || IS_LSA_SELF(lsa))
254 continue;
255 int lsa_id =
256 GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
257 switch (lsa_id) {
258 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
259 ospf_sr_ri_lsa_update(lsa);
260 break;
261 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
262 ospf_sr_ext_prefix_lsa_update(lsa);
263 break;
264 case OPAQUE_TYPE_EXTENDED_LINK_LSA:
265 ospf_sr_ext_link_lsa_update(lsa);
266 break;
267 default:
268 break;
269 }
270 }
271 }
272
273 rc = 1;
274 return rc;
275}
276
cf9b9f77
OD
277/* Stop Segment Routing */
278static void ospf_sr_stop(void)
279{
280
281 if (IS_DEBUG_OSPF_SR)
7743f2f8 282 zlog_debug("SR (%s): Stop Segment Routing", __func__);
cf9b9f77 283
7743f2f8
OD
284 /*
285 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
6a154c88 286 * be remove though list_delete() call. See sr_node_del()
7743f2f8 287 */
cf9b9f77
OD
288 hash_clean(OspfSR.neighbors, (void *)sr_node_del);
289}
290
291/*
292 * Segment Routing initialize function
293 *
294 * @param - nothing
295 *
296 * @return 0 if OK, -1 otherwise
297 */
298int ospf_sr_init(void)
299{
300 int rc = -1;
301
df25d292
DS
302 if (IS_DEBUG_OSPF_SR)
303 zlog_info("SR (%s): Initialize SR Data Base", __func__);
cf9b9f77
OD
304
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;
309
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;
315
316 OspfSR.srgb.range_size = MPLS_DEFAULT_MAX_SRGB_SIZE;
317 OspfSR.srgb.lower_bound = MPLS_DEFAULT_MIN_SRGB_LABEL;
7743f2f8 318 OspfSR.msd = 0;
cf9b9f77
OD
319
320 /* Initialize Hash table for neighbor SR nodes */
321 OspfSR.neighbors = hash_create(sr_hash, sr_cmp, "OSPF_SR");
322 if (OspfSR.neighbors == NULL)
323 return rc;
324
325 /* Initialize Route Table for prefix */
326 OspfSR.prefix = route_table_init();
327 if (OspfSR.prefix == NULL)
328 return rc;
329
330 /* Register Segment Routing VTY command */
331 ospf_sr_register_vty();
332
333 rc = 0;
334 return rc;
335}
336
337/*
338 * Segment Routing termination function
339 *
340 * @param - nothing
cf9b9f77
OD
341 * @return - nothing
342 */
343void ospf_sr_term(void)
344{
345
346 /* Stop Segment Routing */
347 ospf_sr_stop();
348
349 /* Clear SR Node Table */
350 if (OspfSR.neighbors)
351 hash_free(OspfSR.neighbors);
352
353 /* Clear Prefix Table */
354 if (OspfSR.prefix)
355 route_table_finish(OspfSR.prefix);
356
bcf4475e
OD
357 OspfSR.enabled = false;
358 OspfSR.self = NULL;
359}
360
361/*
362 * Segment Routing finish function
363 *
364 * @param - nothing
365 * @return - nothing
366 */
367void ospf_sr_finish(void)
368{
369 /* Stop Segment Routing */
370 ospf_sr_stop();
371
cf9b9f77
OD
372 OspfSR.enabled = false;
373}
374
375/*
376 * Following functions are used to manipulate the
377 * Next Hop Label Forwarding entry (NHLFE)
378 */
379
380/* Compute label from index */
93f0a26e 381static mpls_label_t index2label(uint32_t index, struct sr_srgb srgb)
cf9b9f77
OD
382{
383 mpls_label_t label;
384
385 label = srgb.lower_bound + index;
386 if (label > (srgb.lower_bound + srgb.range_size))
387 return MPLS_INVALID_LABEL;
388 else
389 return label;
390}
391
392/* Get neighbor full structure from address */
393static struct ospf_neighbor *get_neighbor_by_addr(struct ospf *top,
394 struct in_addr addr)
395{
396 struct ospf_neighbor *nbr;
397 struct ospf_interface *oi;
398 struct listnode *node;
399 struct route_node *rn;
400
401 /* Sanity Check */
402 if (top == NULL)
403 return NULL;
404
405 for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi))
7743f2f8
OD
406 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
407 nbr = rn->info;
408 if (nbr)
cf9b9f77
OD
409 if (IPV4_ADDR_SAME(&nbr->address.u.prefix4,
410 &addr)
411 || IPV4_ADDR_SAME(&nbr->router_id, &addr)) {
412 route_unlock_node(rn);
413 return nbr;
414 }
7743f2f8 415 }
cf9b9f77
OD
416 return NULL;
417}
418
419/* Get OSPF Path from address */
420static struct ospf_path *get_nexthop_by_addr(struct ospf *top,
421 struct prefix_ipv4 p)
422{
996c9314 423 struct ospf_route * or ;
cf9b9f77
OD
424 struct ospf_path *path;
425 struct listnode *node;
426 struct route_node *rn;
427
428 /* Sanity Check */
9b50aa1f 429 if (top == NULL)
cf9b9f77
OD
430 return NULL;
431
432 if (IS_DEBUG_OSPF_SR)
7743f2f8 433 zlog_debug(" |- Search Nexthop for prefix %s/%u",
cf9b9f77
OD
434 inet_ntoa(p.prefix), p.prefixlen);
435
436 rn = route_node_lookup(top->new_table, (struct prefix *)&p);
437
7743f2f8
OD
438 /*
439 * Check if we found an OSPF route. May be NULL if SPF has not
440 * yet populate routing table for this prefix.
441 */
cf9b9f77
OD
442 if (rn == NULL)
443 return NULL;
444
445 route_unlock_node(rn);
7743f2f8
OD
446 or = rn->info;
447 if (or == NULL)
cf9b9f77
OD
448 return NULL;
449
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)
453 return path;
454
455 return NULL;
456}
457
458/* Compute NHLFE entry for Extended Link */
459static int compute_link_nhlfe(struct sr_link *srl)
460{
461 struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
462 struct ospf_neighbor *nh;
463 int rc = 0;
464
465 if (IS_DEBUG_OSPF_SR)
7743f2f8 466 zlog_debug(" |- Compute NHLFE for link %s/%u",
cf9b9f77
OD
467 inet_ntoa(srl->nhlfe[0].prefv4.prefix),
468 srl->nhlfe[0].prefv4.prefixlen);
469
470 /* First determine the OSPF Neighbor */
471 nh = get_neighbor_by_addr(top, srl->nhlfe[0].nexthop);
472
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.
476 */
477 if (nh == NULL)
478 return rc;
479
480 if (IS_DEBUG_OSPF_SR)
481 zlog_debug(" |- Found nexthop NHLFE %s",
482 inet_ntoa(nh->router_id));
483
484 /* Set ifindex for this neighbor */
485 srl->nhlfe[0].ifindex = nh->oi->ifp->ifindex;
486 srl->nhlfe[1].ifindex = nh->oi->ifp->ifindex;
487
d922605d
OD
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);
492 }
493
cf9b9f77
OD
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];
497 else
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];
502 else
503 srl->nhlfe[1].label_in =
504 index2label(srl->sid[1], srl->srn->srgb);
505
70e98a7f
DS
506 srl->nhlfe[0].label_out = MPLS_LABEL_IMPLICIT_NULL;
507 srl->nhlfe[1].label_out = MPLS_LABEL_IMPLICIT_NULL;
cf9b9f77
OD
508
509 rc = 1;
510 return rc;
511}
512
513/*
514 * Compute NHLFE entry for Extended Prefix
515 *
516 * @param srp - Segment Routing Prefix
517 *
518 * @return -1 if next hop is not found, 0 if nexthop has not changed
519 * and 1 if success
520 */
521static int compute_prefix_nhlfe(struct sr_prefix *srp)
522{
523 struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
524 struct ospf_path *nh = NULL;
525 struct sr_node *srnext;
526 int rc = -1;
527
528 if (IS_DEBUG_OSPF_SR)
7743f2f8 529 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
cf9b9f77
OD
530 inet_ntoa(srp->nhlfe.prefv4.prefix),
531 srp->nhlfe.prefv4.prefixlen);
532
533 /* First determine the nexthop */
534 nh = get_nexthop_by_addr(top, srp->nhlfe.prefv4);
535
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.
539 */
540 if (nh == NULL)
541 return rc;
542
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))
546 return 0;
547
548 if (IS_DEBUG_OSPF_SR)
549 zlog_debug(" |- Found new next hop for this NHLFE: %s",
550 inet_ntoa(nh->nexthop));
551
7743f2f8
OD
552 /*
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
556 */
cf9b9f77 557 srnext = get_sr_node_by_nexthop(top, nh->nexthop);
7743f2f8
OD
558 if (srnext == NULL)
559 return rc;
560
561 /* And store this information for later update if SR Node is found */
cf9b9f77
OD
562 srnext->neighbor = OspfSR.self;
563 if (IPV4_ADDR_SAME(&srnext->adv_router, &srp->adv_router))
564 srp->nexthop = NULL;
565 else
566 srp->nexthop = srnext;
567
568 /*
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
572 */
573 if ((srnext == NULL) || (srnext->srgb.lower_bound == 0)
574 || (srnext->srgb.range_size == 0))
575 return rc;
576
577 if (IS_DEBUG_OSPF_SR)
7743f2f8 578 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
cf9b9f77
OD
579 srnext->srgb.range_size, srnext->srgb.lower_bound,
580 inet_ntoa(srnext->adv_router));
581
582 /* Set ip addr & ifindex for this neighbor */
583 IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &nh->nexthop);
584 srp->nhlfe.ifindex = nh->ifindex;
585
586 /* Compute Input Label with self SRGB */
587 srp->nhlfe.label_in = index2label(srp->sid, OspfSR.srgb);
7743f2f8
OD
588 /*
589 * and Output Label with Next hop SR Node SRGB or Implicit Null label
590 * if next hop is the destination and request PHP
591 */
cf9b9f77
OD
592 if ((srp->nexthop == NULL)
593 && (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)))
70e98a7f 594 srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
cf9b9f77
OD
595 else if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
596 srp->nhlfe.label_out = srp->sid;
597 else
598 srp->nhlfe.label_out = index2label(srp->sid, srnext->srgb);
599
600 if (IS_DEBUG_OSPF_SR)
7743f2f8 601 zlog_debug(" |- Computed new labels in: %u out: %u",
cf9b9f77
OD
602 srp->nhlfe.label_in, srp->nhlfe.label_out);
603
604 rc = 1;
605 return rc;
606}
607
608/* Send MPLS Label entry to Zebra for installation or deletion */
609static int ospf_zebra_send_mpls_labels(int cmd, struct sr_nhlfe nhlfe)
610{
bad6b0e7 611 struct zapi_labels zl = {};
ea6b290b 612 struct zapi_nexthop_label *znh;
cf9b9f77
OD
613
614 if (IS_DEBUG_OSPF_SR)
7743f2f8 615 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
cf9b9f77
OD
616 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
617 nhlfe.label_in, nhlfe.label_out,
618 inet_ntoa(nhlfe.prefv4.prefix),
619 nhlfe.prefv4.prefixlen, nhlfe.ifindex);
620
635a039e 621 zl.type = ZEBRA_LSP_OSPF_SR;
bad6b0e7 622 zl.local_label = nhlfe.label_in;
b3c49d0e
RW
623
624 SET_FLAG(zl.message, ZAPI_LABELS_FTN);
625 zl.route.prefix.family = nhlfe.prefv4.family;
626 zl.route.prefix.prefixlen = nhlfe.prefv4.prefixlen;
627 zl.route.prefix.u.prefix4 = nhlfe.prefv4.prefix;
628 zl.route.type = ZEBRA_ROUTE_OSPF;
629 zl.route.instance = 0;
630
ea6b290b
RW
631 zl.nexthop_num = 1;
632 znh = &zl.nexthops[0];
633 znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
634 znh->family = AF_INET;
635 znh->address.ipv4 = nhlfe.nexthop;
636 znh->ifindex = nhlfe.ifindex;
637 znh->label = nhlfe.label_out;
bad6b0e7
RW
638
639 return zebra_send_mpls_labels(zclient, cmd, &zl);
cf9b9f77
OD
640}
641
cf9b9f77
OD
642/* Add new NHLFE entry for SID */
643static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe)
644{
bf54ca82 645 if ((nhlfe.label_in != 0) && (nhlfe.label_out != 0))
cf9b9f77 646 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, nhlfe);
cf9b9f77
OD
647}
648
649/* Remove NHLFE entry for SID */
650static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe)
651{
bf54ca82 652 if ((nhlfe.label_in != 0) && (nhlfe.label_out != 0))
cf9b9f77 653 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, nhlfe);
cf9b9f77
OD
654}
655
656/* Update NHLFE entry for SID */
657static inline void update_sid_nhlfe(struct sr_nhlfe n1, struct sr_nhlfe n2)
658{
659
660 del_sid_nhlfe(n1);
661 add_sid_nhlfe(n2);
662}
663
664/*
665 * Functions to parse and get Extended Link / Prefix
666 * TLVs and SubTLVs
667 */
668
669/* Extended Link SubTLVs Getter */
670static struct sr_link *get_ext_link_sid(struct tlv_header *tlvh)
671{
672
673 struct sr_link *srl;
674 struct ext_tlv_link *link = (struct ext_tlv_link *)tlvh;
675 struct ext_subtlv_adj_sid *adj_sid;
676 struct ext_subtlv_lan_adj_sid *lan_sid;
677 struct ext_subtlv_rmt_itf_addr *rmt_itf;
678
679 struct tlv_header *sub_tlvh;
93f0a26e 680 uint16_t length = 0, sum = 0, i = 0;
cf9b9f77
OD
681
682 srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link));
683
cf9b9f77
OD
684 /* Initialize TLV browsing */
685 length = ntohs(tlvh->length) - EXT_TLV_LINK_SIZE;
686 sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
687 + EXT_TLV_LINK_SIZE);
688 for (; sum < length; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
689 switch (ntohs(sub_tlvh->type)) {
690 case EXT_SUBTLV_ADJ_SID:
691 adj_sid = (struct ext_subtlv_adj_sid *)sub_tlvh;
692 srl->type = ADJ_SID;
693 i = CHECK_FLAG(adj_sid->flags,
694 EXT_SUBTLV_LINK_ADJ_SID_BFLG)
695 ? 1
696 : 0;
697 srl->flags[i] = adj_sid->flags;
698 if (CHECK_FLAG(adj_sid->flags,
699 EXT_SUBTLV_LINK_ADJ_SID_VFLG))
700 srl->sid[i] = GET_LABEL(ntohl(adj_sid->value));
701 else
702 srl->sid[i] = ntohl(adj_sid->value);
703 IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop, &link->link_id);
704 break;
705 case EXT_SUBTLV_LAN_ADJ_SID:
706 lan_sid = (struct ext_subtlv_lan_adj_sid *)sub_tlvh;
707 srl->type = LAN_ADJ_SID;
708 i = CHECK_FLAG(lan_sid->flags,
709 EXT_SUBTLV_LINK_ADJ_SID_BFLG)
710 ? 1
711 : 0;
712 srl->flags[i] = lan_sid->flags;
713 if (CHECK_FLAG(lan_sid->flags,
714 EXT_SUBTLV_LINK_ADJ_SID_VFLG))
715 srl->sid[i] = GET_LABEL(ntohl(lan_sid->value));
716 else
717 srl->sid[i] = ntohl(lan_sid->value);
718 IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop,
719 &lan_sid->neighbor_id);
720 break;
721 case EXT_SUBTLV_RMT_ITF_ADDR:
722 rmt_itf = (struct ext_subtlv_rmt_itf_addr *)sub_tlvh;
723 IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &rmt_itf->value);
724 IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &rmt_itf->value);
725 break;
726 default:
727 break;
728 }
729 sum += TLV_SIZE(sub_tlvh);
730 }
731
732 IPV4_ADDR_COPY(&srl->nhlfe[0].prefv4.prefix, &link->link_data);
733 srl->nhlfe[0].prefv4.prefixlen = IPV4_MAX_PREFIXLEN;
734 srl->nhlfe[0].prefv4.family = AF_INET;
735 apply_mask_ipv4(&srl->nhlfe[0].prefv4);
736 IPV4_ADDR_COPY(&srl->nhlfe[1].prefv4.prefix, &link->link_data);
737 srl->nhlfe[1].prefv4.prefixlen = IPV4_MAX_PREFIXLEN;
738 srl->nhlfe[1].prefv4.family = AF_INET;
739 apply_mask_ipv4(&srl->nhlfe[1].prefv4);
740
741 if (IS_DEBUG_OSPF_SR) {
7743f2f8 742 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
cf9b9f77
OD
743 srl->sid[0], inet_ntoa(srl->nhlfe[0].prefv4.prefix),
744 srl->nhlfe[0].prefv4.prefixlen);
7743f2f8 745 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
cf9b9f77
OD
746 srl->sid[1], inet_ntoa(srl->nhlfe[1].prefv4.prefix),
747 srl->nhlfe[1].prefv4.prefixlen);
748 }
749
750 return srl;
751}
752
753/* Extended Prefix SubTLVs Getter */
754static struct sr_prefix *get_ext_prefix_sid(struct tlv_header *tlvh)
755{
756
757 struct sr_prefix *srp;
758 struct ext_tlv_prefix *pref = (struct ext_tlv_prefix *)tlvh;
759 struct ext_subtlv_prefix_sid *psid;
760
761 struct tlv_header *sub_tlvh;
93f0a26e 762 uint16_t length = 0, sum = 0;
cf9b9f77
OD
763
764 srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
765
cf9b9f77
OD
766 /* Initialize TLV browsing */
767 length = ntohs(tlvh->length) - EXT_TLV_PREFIX_SIZE;
768 sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
769 + EXT_TLV_PREFIX_SIZE);
770 for (; sum < length; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
771 switch (ntohs(sub_tlvh->type)) {
772 case EXT_SUBTLV_PREFIX_SID:
773 psid = (struct ext_subtlv_prefix_sid *)sub_tlvh;
774 if (psid->algorithm != SR_ALGORITHM_SPF) {
cf444bcf 775 flog_err(EC_OSPF_INVALID_ALGORITHM,
ade6974d
QY
776 "SR (%s): Unsupported Algorithm",
777 __func__);
cf9b9f77
OD
778 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
779 return NULL;
780 }
781 srp->type = PREF_SID;
782 srp->flags = psid->flags;
783 if (CHECK_FLAG(psid->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
784 srp->sid = GET_LABEL(ntohl(psid->value));
785 else
786 srp->sid = ntohl(psid->value);
787 IPV4_ADDR_COPY(&srp->nhlfe.prefv4.prefix,
788 &pref->address);
789 srp->nhlfe.prefv4.prefixlen = pref->pref_length;
790 srp->nhlfe.prefv4.family = AF_INET;
791 apply_mask_ipv4(&srp->nhlfe.prefv4);
792 break;
793 default:
794 break;
795 }
796 sum += TLV_SIZE(sub_tlvh);
797 }
798
799 if (IS_DEBUG_OSPF_SR)
7743f2f8 800 zlog_debug(" |- Found SID %u for prefix %s/%u", srp->sid,
cf9b9f77
OD
801 inet_ntoa(srp->nhlfe.prefv4.prefix),
802 srp->nhlfe.prefv4.prefixlen);
803 return srp;
804}
805
806/*
807 * Functions to manipulate Segment Routing Link & Prefix structures
808 */
809
810/* Compare two Segment Link: return 0 if equal, 1 otherwise */
811static inline int sr_link_cmp(struct sr_link *srl1, struct sr_link *srl2)
812{
813 if ((srl1->sid[0] == srl2->sid[0]) && (srl1->sid[1] == srl2->sid[1])
814 && (srl1->type == srl2->type) && (srl1->flags[0] == srl2->flags[0])
815 && (srl1->flags[1] == srl2->flags[1]))
816 return 0;
817 else
818 return 1;
819}
820
821/* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
822static inline int sr_prefix_cmp(struct sr_prefix *srp1, struct sr_prefix *srp2)
823{
824 if ((srp1->sid == srp2->sid) && (srp1->flags == srp2->flags))
825 return 0;
826 else
827 return 1;
828}
829
830/* Update Segment Link of given Segment Routing Node */
831static void update_ext_link_sid(struct sr_node *srn, struct sr_link *srl,
d7c0a89a 832 uint8_t lsa_flags)
cf9b9f77
OD
833{
834 struct listnode *node;
835 struct sr_link *lk;
836 bool found = false;
837
838 /* Sanity check */
839 if ((srn == NULL) || (srl == NULL))
840 return;
841
842 if (IS_DEBUG_OSPF_SR)
843 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
844
845 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
846 if (!CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG)
847 || !CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG)
848 || !CHECK_FLAG(lsa_flags, OSPF_LSA_SELF))
849 return;
850
851 /* Search for existing Segment Link */
852 for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, lk))
853 if (lk->instance == srl->instance) {
854 found = true;
855 break;
856 }
857
858 if (IS_DEBUG_OSPF_SR)
7743f2f8 859 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
cf9b9f77
OD
860 found ? "Update" : "Add",
861 GET_OPAQUE_ID(srl->instance),
862 inet_ntoa(srn->adv_router));
863
864 /* if not found, add new Segment Link and install NHLFE */
865 if (!found) {
866 /* Complete SR-Link and add it to SR-Node list */
867 srl->srn = srn;
868 IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router);
869 listnode_add(srn->ext_link, srl);
870 /* Try to set MPLS table */
871 if (compute_link_nhlfe(srl)) {
872 add_sid_nhlfe(srl->nhlfe[0]);
873 add_sid_nhlfe(srl->nhlfe[1]);
874 }
875 } else {
876 if (sr_link_cmp(lk, srl)) {
877 if (compute_link_nhlfe(srl)) {
878 update_sid_nhlfe(lk->nhlfe[0], srl->nhlfe[0]);
879 update_sid_nhlfe(lk->nhlfe[1], srl->nhlfe[1]);
880 /* Replace Segment List */
881 listnode_delete(srn->ext_link, lk);
882 XFREE(MTYPE_OSPF_SR_PARAMS, lk);
883 srl->srn = srn;
884 IPV4_ADDR_COPY(&srl->adv_router,
885 &srn->adv_router);
886 listnode_add(srn->ext_link, srl);
887 } else {
7743f2f8
OD
888 /* New NHLFE was not found.
889 * Just free the SR Link
890 */
cf9b9f77
OD
891 XFREE(MTYPE_OSPF_SR_PARAMS, srl);
892 }
893 } else {
7743f2f8
OD
894 /*
895 * This is just an LSA refresh.
896 * Stop processing and free SR Link
897 */
cf9b9f77
OD
898 XFREE(MTYPE_OSPF_SR_PARAMS, srl);
899 }
900 }
901}
902
903/* Update Segment Prefix of given Segment Routing Node */
904static void update_ext_prefix_sid(struct sr_node *srn, struct sr_prefix *srp)
905{
906
907 struct listnode *node;
908 struct sr_prefix *pref;
909 bool found = false;
910
911 /* Sanity check */
912 if (srn == NULL || srp == NULL)
913 return;
914
915 if (IS_DEBUG_OSPF_SR)
7743f2f8 916 zlog_debug(" |- Process Extended Prefix SID %u", srp->sid);
cf9b9f77
OD
917
918 /* Process only Global Prefix SID */
919 if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_LFLG))
920 return;
921
922 /* Search for existing Segment Prefix */
923 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, pref))
924 if (pref->instance == srp->instance) {
925 found = true;
926 break;
927 }
928
929 if (IS_DEBUG_OSPF_SR)
7743f2f8 930 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
cf9b9f77
OD
931 found ? "Update" : "Add",
932 GET_OPAQUE_ID(srp->instance),
933 inet_ntoa(srn->adv_router));
934
935 /* if not found, add new Segment Prefix and install NHLFE */
936 if (!found) {
937 /* Complete SR-Prefix and add it to SR-Node list */
938 srp->srn = srn;
939 IPV4_ADDR_COPY(&srp->adv_router, &srn->adv_router);
940 listnode_add(srn->ext_prefix, srp);
941 /* Try to set MPLS table */
7743f2f8 942 if (compute_prefix_nhlfe(srp) == 1)
cf9b9f77 943 add_sid_nhlfe(srp->nhlfe);
cf9b9f77
OD
944 } else {
945 if (sr_prefix_cmp(pref, srp)) {
946 if (compute_prefix_nhlfe(srp) == 1) {
947 update_sid_nhlfe(pref->nhlfe, srp->nhlfe);
948 /* Replace Segment Prefix */
949 listnode_delete(srn->ext_prefix, pref);
950 XFREE(MTYPE_OSPF_SR_PARAMS, pref);
951 srp->srn = srn;
952 IPV4_ADDR_COPY(&srp->adv_router,
953 &srn->adv_router);
954 listnode_add(srn->ext_prefix, srp);
955 } else {
956 /* New NHLFE was not found.
7743f2f8
OD
957 * Just free the SR Prefix
958 */
cf9b9f77
OD
959 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
960 }
961 } else {
962 /* This is just an LSA refresh.
7743f2f8
OD
963 * Stop processing and free SR Prefix
964 */
cf9b9f77
OD
965 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
966 }
967 }
968}
969
970/*
971 * When change the FRR Self SRGB, update the NHLFE Input Label
972 * for all Extended Prefix with SID index through hash_iterate()
973 */
e3b78da8 974static void update_in_nhlfe(struct hash_bucket *bucket, void *args)
cf9b9f77
OD
975{
976 struct listnode *node;
e3b78da8 977 struct sr_node *srn = (struct sr_node *)bucket->data;
cf9b9f77
OD
978 struct sr_prefix *srp;
979 struct sr_nhlfe new;
980
cf9b9f77
OD
981 /* Process Every Extended Prefix for this SR-Node */
982 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
7743f2f8
OD
983 /* Process Self SRN only if NO-PHP is requested */
984 if ((srn == OspfSR.self)
985 && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
986 continue;
987
cf9b9f77
OD
988 /* Process only SID Index */
989 if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
990 continue;
7743f2f8
OD
991
992 /* OK. Compute new NHLFE */
cf9b9f77
OD
993 memcpy(&new, &srp->nhlfe, sizeof(struct sr_nhlfe));
994 new.label_in = index2label(srp->sid, OspfSR.srgb);
995 /* Update MPLS LFIB */
996 update_sid_nhlfe(srp->nhlfe, new);
997 /* Finally update Input Label */
998 srp->nhlfe.label_in = new.label_in;
999 }
1000}
1001
1002/*
1003 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1004 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1005 */
e3b78da8 1006static void update_out_nhlfe(struct hash_bucket *bucket, void *args)
cf9b9f77
OD
1007{
1008 struct listnode *node;
e3b78da8 1009 struct sr_node *srn = (struct sr_node *)bucket->data;
cf9b9f77
OD
1010 struct sr_node *srnext = (struct sr_node *)args;
1011 struct sr_prefix *srp;
1012 struct sr_nhlfe new;
1013
1014 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1015 /* Process only SID Index for next hop without PHP */
1016 if ((srp->nexthop == NULL)
1017 && (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)))
1018 continue;
1019 memcpy(&new, &srp->nhlfe, sizeof(struct sr_nhlfe));
1020 new.label_out = index2label(srp->sid, srnext->srgb);
1021 update_sid_nhlfe(srp->nhlfe, new);
1022 srp->nhlfe.label_out = new.label_out;
1023 }
1024}
1025
1026/*
1027 * Following functions are call when new Segment Routing LSA are received
1028 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1029 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1030 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1031 */
1032
1033/* Update Segment Routing from Router Information LSA */
1034void ospf_sr_ri_lsa_update(struct ospf_lsa *lsa)
1035{
1036 struct sr_node *srn;
1037 struct tlv_header *tlvh;
1038 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1039 struct ri_sr_tlv_sid_label_range *ri_srgb;
1040 struct ri_sr_tlv_sr_algorithm *algo;
1041 struct sr_srgb srgb;
93f0a26e 1042 uint16_t length = 0, sum = 0;
cf9b9f77
OD
1043
1044 if (IS_DEBUG_OSPF_SR)
1045 zlog_debug(
7743f2f8
OD
1046 "SR (%s): Process Router "
1047 "Information LSA 4.0.0.%u from %s",
996c9314 1048 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
cf9b9f77
OD
1049 inet_ntoa(lsah->adv_router));
1050
1051 /* Sanity check */
1052 if (IS_LSA_SELF(lsa))
1053 return;
1054
1055 if (OspfSR.neighbors == NULL) {
cf444bcf 1056 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1057 "SR (%s): Abort! no valid SR DataBase", __func__);
cf9b9f77
OD
1058 return;
1059 }
1060
1061 /* Get SR Node in hash table from Router ID */
1062 srn = hash_get(OspfSR.neighbors, (void *)&(lsah->adv_router),
1063 (void *)sr_node_new);
1064
1065 /* Sanity check */
1066 if (srn == NULL) {
cf444bcf 1067 flog_err(EC_OSPF_SR_NODE_CREATE,
1c50c1c0
QY
1068 "SR (%s): Abort! can't create SR node in hash table",
1069 __func__);
cf9b9f77
OD
1070 return;
1071 }
1072
1073 if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) {
cf444bcf 1074 flog_err(EC_OSPF_SR_INVALID_LSA_ID,
1c50c1c0
QY
1075 "SR (%s): Abort! Wrong "
1076 "LSA ID 4.0.0.%u for SR node %s/%u",
1077 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1078 inet_ntoa(lsah->adv_router), srn->instance);
cf9b9f77
OD
1079 return;
1080 }
1081
1082 /* Collect Router Information Sub TLVs */
1083 /* Initialize TLV browsing */
1084 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1085 srgb.range_size = 0;
1086 srgb.lower_bound = 0;
1087
6aaf0fdd 1088 for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL);
cf9b9f77
OD
1089 tlvh = TLV_HDR_NEXT(tlvh)) {
1090 switch (ntohs(tlvh->type)) {
1091 case RI_SR_TLV_SR_ALGORITHM:
1092 algo = (struct ri_sr_tlv_sr_algorithm *)tlvh;
1093 int i;
7743f2f8 1094
cf9b9f77
OD
1095 for (i = 0; i < ntohs(algo->header.length); i++)
1096 srn->algo[i] = algo->value[0];
1097 for (; i < ALGORITHM_COUNT; i++)
1098 srn->algo[i] = SR_ALGORITHM_UNSET;
1099 sum += TLV_SIZE(tlvh);
1100 break;
1101 case RI_SR_TLV_SID_LABEL_RANGE:
1102 ri_srgb = (struct ri_sr_tlv_sid_label_range *)tlvh;
1103 srgb.range_size = GET_RANGE_SIZE(ntohl(ri_srgb->size));
1104 srgb.lower_bound =
1105 GET_LABEL(ntohl(ri_srgb->lower.value));
1106 sum += TLV_SIZE(tlvh);
1107 break;
1108 case RI_SR_TLV_NODE_MSD:
1109 srn->msd = ((struct ri_sr_tlv_node_msd *)(tlvh))->value;
1110 sum += TLV_SIZE(tlvh);
1111 break;
1112 default:
1113 sum += TLV_SIZE(tlvh);
1114 break;
1115 }
1116 }
1117
1118 /* Check that we collect mandatory parameters */
1119 if (srn->algo[0] == SR_ALGORITHM_UNSET || srgb.range_size == 0
1120 || srgb.lower_bound == 0) {
cf444bcf 1121 flog_err(EC_OSPF_SR_NODE_CREATE,
dbb93f1b
DS
1122 "SR (%s): Missing mandatory parameters. Abort!",
1123 __func__);
cf9b9f77
OD
1124 hash_release(OspfSR.neighbors, &(srn->adv_router));
1125 XFREE(MTYPE_OSPF_SR_PARAMS, srn);
1126 return;
1127 }
1128
1129 /* Check if it is a new SR Node or not */
1130 if (srn->instance == 0) {
1131 /* update LSA ID */
1132 srn->instance = ntohl(lsah->id.s_addr);
1133 /* Copy SRGB */
1134 srn->srgb.range_size = srgb.range_size;
1135 srn->srgb.lower_bound = srgb.lower_bound;
1136 }
1137
1138 /* Check if SRGB has changed */
1139 if ((srn->srgb.range_size != srgb.range_size)
1140 || (srn->srgb.lower_bound != srgb.lower_bound)) {
1141 srn->srgb.range_size = srgb.range_size;
1142 srn->srgb.lower_bound = srgb.lower_bound;
1143 /* Update NHLFE if it is a neighbor SR node */
1144 if (srn->neighbor == OspfSR.self)
1145 hash_iterate(OspfSR.neighbors,
e3b78da8 1146 (void (*)(struct hash_bucket *,
cf9b9f77
OD
1147 void *))update_out_nhlfe,
1148 (void *)srn);
1149 }
cf9b9f77
OD
1150}
1151
1152/*
1153 * Delete SR Node entry in hash table information corresponding to an expired
1154 * Router Information LSA
1155 */
1156void ospf_sr_ri_lsa_delete(struct ospf_lsa *lsa)
1157{
1158 struct sr_node *srn;
1159 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1160
1161 if (IS_DEBUG_OSPF_SR)
996c9314
LB
1162 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1163 __func__, inet_ntoa(lsah->adv_router),
1164 GET_OPAQUE_ID(ntohl(lsah->id.s_addr)));
cf9b9f77
OD
1165
1166 /* Sanity check */
1167 if (OspfSR.neighbors == NULL) {
cf444bcf 1168 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1169 "SR (%s): Abort! no valid SR Data Base", __func__);
cf9b9f77
OD
1170 return;
1171 }
1172
1173 /* Release Router ID entry in SRDB hash table */
1174 srn = hash_release(OspfSR.neighbors, &(lsah->adv_router));
1175
1176 /* Sanity check */
1177 if (srn == NULL) {
cf444bcf 1178 flog_err(EC_OSPF_SR_NODE_CREATE,
1c50c1c0
QY
1179 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1180 __func__, inet_ntoa(lsah->adv_router));
cf9b9f77
OD
1181 return;
1182 }
1183
1184 if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) {
1c50c1c0
QY
1185 flog_err(EC_OSPF_SR_INVALID_LSA_ID,
1186 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1187 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1188 inet_ntoa(lsah->adv_router));
cf9b9f77
OD
1189 return;
1190 }
1191
1192 /* Remove SR node */
1193 sr_node_del(srn);
cf9b9f77
OD
1194}
1195
1196/* Update Segment Routing from Extended Link LSA */
1197void ospf_sr_ext_link_lsa_update(struct ospf_lsa *lsa)
1198{
1199 struct sr_node *srn;
1200 struct tlv_header *tlvh;
1201 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1202 struct sr_link *srl;
1203
93f0a26e 1204 uint16_t length, sum;
cf9b9f77
OD
1205
1206 if (IS_DEBUG_OSPF_SR)
1207 zlog_debug(
7743f2f8
OD
1208 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1209 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
cf9b9f77
OD
1210 inet_ntoa(lsah->adv_router));
1211
1212 /* Sanity check */
1213 if (OspfSR.neighbors == NULL) {
cf444bcf 1214 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1215 "SR (%s): Abort! no valid SR DataBase", __func__);
cf9b9f77
OD
1216 return;
1217 }
1218
1219 /* Get SR Node in hash table from Router ID */
1220 srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1221 (void *)&(lsah->adv_router),
1222 (void *)sr_node_new);
1223
1224 /* Sanity check */
1225 if (srn == NULL) {
cf444bcf 1226 flog_err(EC_OSPF_SR_NODE_CREATE,
1c50c1c0
QY
1227 "SR (%s): Abort! can't create SR node in hash table",
1228 __func__);
cf9b9f77
OD
1229 return;
1230 }
1231
1232 /* Initialize TLV browsing */
1233 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1234 sum = 0;
6aaf0fdd 1235 for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL);
cf9b9f77
OD
1236 tlvh = TLV_HDR_NEXT(tlvh)) {
1237 if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1238 /* Got Extended Link information */
1239 srl = get_ext_link_sid(tlvh);
1240 /* Update SID if not null */
1241 if (srl != NULL) {
1242 srl->instance = ntohl(lsah->id.s_addr);
1243 update_ext_link_sid(srn, srl, lsa->flags);
1244 }
1245 }
1246 sum += TLV_SIZE(tlvh);
1247 }
1248}
1249
1250/* Delete Segment Routing from Extended Link LSA */
1251void ospf_sr_ext_link_lsa_delete(struct ospf_lsa *lsa)
1252{
1253 struct listnode *node;
1254 struct sr_link *srl;
1255 struct sr_node *srn;
1256 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
93f0a26e 1257 uint32_t instance = ntohl(lsah->id.s_addr);
cf9b9f77
OD
1258
1259 if (IS_DEBUG_OSPF_SR)
996c9314
LB
1260 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1261 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1262 inet_ntoa(lsah->adv_router));
cf9b9f77
OD
1263
1264 /* Sanity check */
1265 if (OspfSR.neighbors == NULL) {
cf444bcf 1266 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1267 "SR (%s): Abort! no valid SR DataBase", __func__);
cf9b9f77
OD
1268 return;
1269 }
1270
1271 /* Search SR Node in hash table from Router ID */
1272 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1273 (void *)&(lsah->adv_router));
1274
7743f2f8
OD
1275 /*
1276 * SR-Node may be NULL if it has been remove previously when
1277 * processing Router Information LSA deletion
1278 */
cf9b9f77 1279 if (srn == NULL) {
cf444bcf 1280 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b
DS
1281 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1282 __func__, inet_ntoa(lsah->adv_router));
cf9b9f77
OD
1283 return;
1284 }
1285
1286 /* Search for corresponding Segment Link */
1287 for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl))
1288 if (srl->instance == instance)
1289 break;
1290
1291 /* Remove Segment Link if found */
fd3b19f2 1292 if ((srl != NULL) && (srl->instance == instance)) {
cf9b9f77
OD
1293 del_sid_nhlfe(srl->nhlfe[0]);
1294 del_sid_nhlfe(srl->nhlfe[1]);
1295 listnode_delete(srn->ext_link, srl);
1296 XFREE(MTYPE_OSPF_SR_PARAMS, srl);
1297 } else {
cf444bcf 1298 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b
DS
1299 "SR (%s): Didn't found corresponding SR Link 8.0.0.%u "
1300 "for SR Node %s",
1301 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1302 inet_ntoa(lsah->adv_router));
cf9b9f77 1303 }
cf9b9f77
OD
1304}
1305
1306/* Update Segment Routing from Extended Prefix LSA */
1307void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa *lsa)
1308{
1309 struct sr_node *srn;
1310 struct tlv_header *tlvh;
1311 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1312 struct sr_prefix *srp;
1313
93f0a26e 1314 uint16_t length, sum;
cf9b9f77
OD
1315
1316 if (IS_DEBUG_OSPF_SR)
1317 zlog_debug(
7743f2f8 1318 "SR (%s): Process Extended Prefix LSA "
996c9314
LB
1319 "7.0.0.%u from %s",
1320 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
cf9b9f77
OD
1321 inet_ntoa(lsah->adv_router));
1322
1323 /* Sanity check */
1324 if (OspfSR.neighbors == NULL) {
cf444bcf 1325 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1326 "SR (%s): Abort! no valid SR DataBase", __func__);
cf9b9f77
OD
1327 return;
1328 }
1329
1330 /* Get SR Node in hash table from Router ID */
1331 srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1332 (void *)&(lsah->adv_router),
1333 (void *)sr_node_new);
1334
1335 /* Sanity check */
1336 if (srn == NULL) {
cf444bcf 1337 flog_err(EC_OSPF_SR_NODE_CREATE,
dbb93f1b
DS
1338 "SR (%s): Abort! can't create SR node in hash table",
1339 __func__);
cf9b9f77
OD
1340 return;
1341 }
1342
1343 /* Initialize TLV browsing */
1344 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1345 sum = 0;
1346 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1347 tlvh = TLV_HDR_NEXT(tlvh)) {
1348 if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1349 /* Got Extended Link information */
1350 srp = get_ext_prefix_sid(tlvh);
1351 /* Update SID if not null */
1352 if (srp != NULL) {
1353 srp->instance = ntohl(lsah->id.s_addr);
1354 update_ext_prefix_sid(srn, srp);
1355 }
1356 }
1357 sum += TLV_SIZE(tlvh);
1358 }
1359}
1360
1361/* Delete Segment Routing from Extended Prefix LSA */
1362void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa *lsa)
1363{
1364 struct listnode *node;
1365 struct sr_prefix *srp;
1366 struct sr_node *srn;
1367 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
93f0a26e 1368 uint32_t instance = ntohl(lsah->id.s_addr);
cf9b9f77
OD
1369
1370 if (IS_DEBUG_OSPF_SR)
1371 zlog_debug(
7743f2f8
OD
1372 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1373 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
cf9b9f77
OD
1374 inet_ntoa(lsah->adv_router));
1375
1376 /* Sanity check */
1377 if (OspfSR.neighbors == NULL) {
cf444bcf 1378 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b 1379 "SR (%s): Abort! no valid SR DataBase", __func__);
cf9b9f77
OD
1380 return;
1381 }
1382
1383 /* Search SR Node in hash table from Router ID */
1384 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1385 (void *)&(lsah->adv_router));
1386
7743f2f8
OD
1387 /*
1388 * SR-Node may be NULL if it has been remove previously when
1389 * processing Router Information LSA deletion
1390 */
cf9b9f77 1391 if (srn == NULL) {
cf444bcf 1392 flog_err(EC_OSPF_SR_INVALID_DB,
dbb93f1b
DS
1393 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1394 __func__, inet_ntoa(lsah->adv_router));
cf9b9f77
OD
1395 return;
1396 }
1397
1398 /* Search for corresponding Segment Link */
1399 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp))
1400 if (srp->instance == instance)
1401 break;
1402
1403 /* Remove Segment Link if found */
fd3b19f2 1404 if ((srp != NULL) && (srp->instance == instance)) {
cf9b9f77
OD
1405 del_sid_nhlfe(srp->nhlfe);
1406 listnode_delete(srn->ext_link, srp);
1407 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1408 } else {
dbb93f1b 1409 flog_err(
cf444bcf 1410 EC_OSPF_SR_INVALID_DB,
dbb93f1b 1411 "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %s",
996c9314 1412 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
cf9b9f77
OD
1413 inet_ntoa(lsah->adv_router));
1414 }
cf9b9f77
OD
1415}
1416
1417/* Get Label for Extended Link SID */
1418/* TODO: To be replace by Zebra Label Manager */
93f0a26e 1419uint32_t get_ext_link_label_value(void)
cf9b9f77 1420{
93f0a26e 1421 static uint32_t label = ADJ_SID_MIN - 1;
cf9b9f77
OD
1422
1423 if (label < ADJ_SID_MAX)
1424 label += 1;
1425
1426 return label;
1427}
1428
7743f2f8
OD
1429/*
1430 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1431 * complete initial CLI command at startutp.
1432 *
1433 * @param ifp - Loopback interface
1434 * @param pref - Prefix address of this interface
1435 *
1436 * @return - void
1437 */
1438void ospf_sr_update_prefix(struct interface *ifp, struct prefix *p)
1439{
1440 struct listnode *node;
1441 struct sr_prefix *srp;
7743f2f8
OD
1442
1443 /* Sanity Check */
1444 if ((ifp == NULL) || (p == NULL))
1445 return;
1446
1447 /*
1448 * Search if there is a Segment Prefix that correspond to this
1449 * interface or prefix, and update it if found
1450 */
1451 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1452 if ((srp->nhlfe.ifindex == ifp->ifindex)
1453 || ((IPV4_ADDR_SAME(&srp->nhlfe.prefv4.prefix,
996c9314
LB
1454 &p->u.prefix4))
1455 && (srp->nhlfe.prefv4.prefixlen == p->prefixlen))) {
7743f2f8
OD
1456
1457 /* Update Interface & Prefix info */
1458 srp->nhlfe.ifindex = ifp->ifindex;
1459 IPV4_ADDR_COPY(&srp->nhlfe.prefv4.prefix,
996c9314 1460 &p->u.prefix4);
7743f2f8
OD
1461 srp->nhlfe.prefv4.prefixlen = p->prefixlen;
1462 srp->nhlfe.prefv4.family = p->family;
1463 IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &p->u.prefix4);
1464
1465 /* OK. Let's Schedule Extended Prefix LSA */
996c9314
LB
1466 srp->instance = ospf_ext_schedule_prefix_index(
1467 ifp, srp->sid, &srp->nhlfe.prefv4, srp->flags);
7743f2f8
OD
1468
1469 /* Install NHLFE if NO-PHP is requested */
1470 if (CHECK_FLAG(srp->flags,
996c9314
LB
1471 EXT_SUBTLV_PREFIX_SID_NPFLG)) {
1472 srp->nhlfe.label_in = index2label(
1473 srp->sid, OspfSR.self->srgb);
70e98a7f 1474 srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
7743f2f8
OD
1475 add_sid_nhlfe(srp->nhlfe);
1476 }
1477 }
1478 }
1479}
1480
cf9b9f77
OD
1481/*
1482 * Following functions are used to update MPLS LFIB after a SPF run
1483 */
1484
e3b78da8 1485static void ospf_sr_nhlfe_update(struct hash_bucket *bucket, void *args)
cf9b9f77
OD
1486{
1487
e3b78da8 1488 struct sr_node *srn = (struct sr_node *)bucket->data;
cf9b9f77
OD
1489 struct listnode *node;
1490 struct sr_prefix *srp;
1491 struct sr_nhlfe old;
cf9b9f77
OD
1492 int rc;
1493
cf9b9f77
OD
1494 if (IS_DEBUG_OSPF_SR)
1495 zlog_debug(" |- Update Prefix for SR Node %s",
1496 inet_ntoa(srn->adv_router));
1497
7743f2f8
OD
1498 /* Skip Self SR Node */
1499 if (srn == OspfSR.self)
cf9b9f77 1500 return;
cf9b9f77
OD
1501
1502 /* Update Extended Prefix */
1503 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1504
1505 /* Backup current NHLFE */
1506 memcpy(&old, &srp->nhlfe, sizeof(struct sr_nhlfe));
1507
1508 /* Compute the new NHLFE */
1509 rc = compute_prefix_nhlfe(srp);
1510
1511 /* Check computation result */
1512 switch (rc) {
1513 /* next hop is not know, remove old NHLFE to avoid loop */
1514 case -1:
1515 del_sid_nhlfe(srp->nhlfe);
1516 break;
1517 /* next hop has not changed, skip it */
1518 case 0:
1519 break;
1520 /* there is a new next hop, update NHLFE */
1521 case 1:
1522 update_sid_nhlfe(old, srp->nhlfe);
1523 break;
1524 default:
1525 break;
1526 }
1527 }
1528}
1529
1530static int ospf_sr_update_schedule(struct thread *t)
1531{
1532
1533 struct ospf *ospf;
1534 struct timeval start_time, stop_time;
1535
1536 ospf = THREAD_ARG(t);
1537 ospf->t_sr_update = NULL;
1538
1539 if (!OspfSR.update)
1540 return 0;
1541
1542 monotime(&start_time);
1543
1544 if (IS_DEBUG_OSPF_SR)
7743f2f8 1545 zlog_debug("SR (%s): Start SPF update", __func__);
cf9b9f77 1546
e3b78da8 1547 hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
cf9b9f77
OD
1548 void *))ospf_sr_nhlfe_update,
1549 NULL);
1550
1551 monotime(&stop_time);
1552
df25d292 1553 if (IS_DEBUG_OSPF_SR)
9165c5f5 1554 zlog_debug("SR (%s): SPF Processing Time(usecs): %lld",
df25d292
DS
1555 __func__,
1556 (stop_time.tv_sec - start_time.tv_sec) * 1000000LL
996c9314 1557 + (stop_time.tv_usec - start_time.tv_usec));
cf9b9f77
OD
1558
1559 OspfSR.update = false;
1560 return 1;
1561}
1562
1563#define OSPF_SR_UPDATE_INTERVAL 1
1564
1565void ospf_sr_update_timer_add(struct ospf *ospf)
1566{
1567
1568 if (ospf == NULL)
1569 return;
1570
1571 /* Check if an update is not alreday engage */
1572 if (OspfSR.update)
1573 return;
1574
1575 OspfSR.update = true;
1576
1577 thread_add_timer(master, ospf_sr_update_schedule, ospf,
1578 OSPF_SR_UPDATE_INTERVAL, &ospf->t_sr_update);
1579}
1580
7743f2f8
OD
1581/*
1582 * --------------------------------------
cf9b9f77 1583 * Followings are vty command functions.
7743f2f8
OD
1584 * --------------------------------------
1585 */
cf9b9f77
OD
1586
1587/*
1588 * Segment Routing Router configuration
1589 *
1590 * Must be centralize as it concerns both Extended Link/Prefix LSA
1591 * and Router Information LSA. Choose to call it from Extended Prefix
1592 * write_config() call back.
1593 *
1594 * @param vty VTY output
1595 *
1596 * @return none
1597 */
1598void ospf_sr_config_write_router(struct vty *vty)
1599{
1600 struct listnode *node;
1601 struct sr_prefix *srp;
1602
1603 if (OspfSR.enabled) {
1604 vty_out(vty, " segment-routing on\n");
1605
7743f2f8
OD
1606 if ((OspfSR.srgb.lower_bound != MPLS_DEFAULT_MIN_SRGB_LABEL)
1607 || (OspfSR.srgb.range_size != MPLS_DEFAULT_MAX_SRGB_SIZE)) {
1608 vty_out(vty, " segment-routing global-block %u %u\n",
996c9314
LB
1609 OspfSR.srgb.lower_bound,
1610 OspfSR.srgb.lower_bound + OspfSR.srgb.range_size
1611 - 1);
7743f2f8 1612 }
cf9b9f77 1613 if (OspfSR.msd != 0)
7743f2f8 1614 vty_out(vty, " segment-routing node-msd %u\n",
cf9b9f77
OD
1615 OspfSR.msd);
1616
1617 if (OspfSR.self != NULL) {
1618 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node,
1619 srp)) {
1620 vty_out(vty,
7743f2f8
OD
1621 " segment-routing prefix %s/%u "
1622 "index %u%s\n",
cf9b9f77 1623 inet_ntoa(srp->nhlfe.prefv4.prefix),
7743f2f8
OD
1624 srp->nhlfe.prefv4.prefixlen, srp->sid,
1625 CHECK_FLAG(srp->flags,
996c9314
LB
1626 EXT_SUBTLV_PREFIX_SID_NPFLG)
1627 ? " no-php-flag"
1628 : "");
cf9b9f77
OD
1629 }
1630 }
1631 }
1632}
1633
1634DEFUN(ospf_sr_enable,
1635 ospf_sr_enable_cmd,
1636 "segment-routing on",
1637 SR_STR
1638 "Enable Segment Routing\n")
1639{
1640
1641 VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
1642
1643 if (OspfSR.enabled)
1644 return CMD_SUCCESS;
1645
7726c479 1646 if (ospf->vrf_id != VRF_DEFAULT) {
996c9314
LB
1647 vty_out(vty,
1648 "Segment Routing is only supported in default "
1649 "VRF\n");
7726c479
OD
1650 return CMD_WARNING_CONFIG_FAILED;
1651 }
1652
cf9b9f77
OD
1653 if (IS_DEBUG_OSPF_EVENT)
1654 zlog_debug("SR: Segment Routing: OFF -> ON");
1655
1656 /* Start Segment Routing */
1657 OspfSR.enabled = true;
266469eb 1658 ospf_sr_start(ospf);
cf9b9f77
OD
1659
1660 /* Set Router Information SR parameters */
1661 if (IS_DEBUG_OSPF_EVENT)
1662 zlog_debug("SR: Activate SR for Router Information LSA");
1663
1664 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1665
1666 /* Update Ext LSA */
1667 if (IS_DEBUG_OSPF_EVENT)
1668 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1669
1670 ospf_ext_update_sr(true);
1671
1672 return CMD_SUCCESS;
1673}
1674
1675DEFUN (no_ospf_sr_enable,
1676 no_ospf_sr_enable_cmd,
1677 "no segment-routing [on]",
1678 NO_STR
1679 SR_STR
1680 "Disable Segment Routing\n")
1681{
1682
1683 if (!OspfSR.enabled)
1684 return CMD_SUCCESS;
1685
1686 if (IS_DEBUG_OSPF_EVENT)
1687 zlog_debug("SR: Segment Routing: ON -> OFF");
1688
1689 /* Start by Disabling Extended Link & Prefix LSA */
1690 ospf_ext_update_sr(false);
1691
1692 /* then, disable Router Information SR parameters */
1693 ospf_router_info_update_sr(false, OspfSR.srgb, OspfSR.msd);
1694
1695 /* Finally, stop Segment Routing */
1696 ospf_sr_stop();
1697 OspfSR.enabled = false;
1698
1699 return CMD_SUCCESS;
1700}
1701
1702static int ospf_sr_enabled(struct vty *vty)
1703{
1704 if (OspfSR.enabled)
1705 return 1;
1706
1707 if (vty)
1708 vty_out(vty, "%% OSPF SR is not turned on\n");
1709
1710 return 0;
1711}
1712
1713DEFUN (sr_sid_label_range,
1714 sr_sid_label_range_cmd,
1715 "segment-routing global-block (0-1048575) (0-1048575)",
1716 SR_STR
1717 "Segment Routing Global Block label range\n"
1718 "Lower-bound range in decimal (0-1048575)\n"
1719 "Upper-bound range in decimal (0-1048575)\n")
1720{
93f0a26e
OD
1721 uint32_t upper;
1722 uint32_t lower;
1723 uint32_t size;
cf9b9f77
OD
1724 int idx_low = 2;
1725 int idx_up = 3;
1726
1727 if (!ospf_sr_enabled(vty))
1728 return CMD_WARNING_CONFIG_FAILED;
1729
7743f2f8
OD
1730 /* Get lower and upper bound */
1731 lower = strtoul(argv[idx_low]->arg, NULL, 10);
1732 upper = strtoul(argv[idx_up]->arg, NULL, 10);
cf9b9f77
OD
1733 size = upper - lower + 1;
1734
1735 if (size > MPLS_DEFAULT_MAX_SRGB_SIZE || size <= 0) {
1736 vty_out(vty,
7743f2f8 1737 "Range size cannot be less than 0 or more than %u\n",
cf9b9f77
OD
1738 MPLS_DEFAULT_MAX_SRGB_SIZE);
1739 return CMD_WARNING_CONFIG_FAILED;
1740 }
1741
1742 if (upper > MPLS_DEFAULT_MAX_SRGB_LABEL) {
7743f2f8 1743 vty_out(vty, "Upper-bound cannot exceed %u\n",
cf9b9f77
OD
1744 MPLS_DEFAULT_MAX_SRGB_LABEL);
1745 return CMD_WARNING_CONFIG_FAILED;
1746 }
1747
1748 if (upper < MPLS_DEFAULT_MIN_SRGB_LABEL) {
7743f2f8 1749 vty_out(vty, "Upper-bound cannot be lower than %u\n",
cf9b9f77
OD
1750 MPLS_DEFAULT_MIN_SRGB_LABEL);
1751 return CMD_WARNING_CONFIG_FAILED;
1752 }
1753
7743f2f8
OD
1754 /* Check if values have changed */
1755 if ((OspfSR.srgb.range_size == size)
1756 && (OspfSR.srgb.lower_bound == lower))
1757 return CMD_SUCCESS;
1758
cf9b9f77
OD
1759 /* Set SID/Label range SRGB */
1760 OspfSR.srgb.range_size = size;
1761 OspfSR.srgb.lower_bound = lower;
7743f2f8
OD
1762 if (OspfSR.self != NULL) {
1763 OspfSR.self->srgb.range_size = size;
1764 OspfSR.self->srgb.lower_bound = lower;
1765 }
cf9b9f77
OD
1766
1767 /* Set Router Information SR parameters */
1768 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1769
1770 /* Update NHLFE entries */
1771 hash_iterate(OspfSR.neighbors,
e3b78da8 1772 (void (*)(struct hash_bucket *, void *))update_in_nhlfe,
cf9b9f77
OD
1773 NULL);
1774
1775 return CMD_SUCCESS;
1776}
1777
1778DEFUN (no_sr_sid_label_range,
1779 no_sr_sid_label_range_cmd,
7743f2f8 1780 "no segment-routing global-block [(0-1048575) (0-1048575)]",
cf9b9f77
OD
1781 NO_STR
1782 SR_STR
7743f2f8
OD
1783 "Segment Routing Global Block label range\n"
1784 "Lower-bound range in decimal (0-1048575)\n"
1785 "Upper-bound range in decimal (0-1048575)\n")
cf9b9f77
OD
1786{
1787
1788 if (!ospf_sr_enabled(vty))
1789 return CMD_WARNING_CONFIG_FAILED;
1790
1791 /* Revert to default SRGB value */
1792 OspfSR.srgb.range_size = MPLS_DEFAULT_MIN_SRGB_SIZE;
1793 OspfSR.srgb.lower_bound = MPLS_DEFAULT_MIN_SRGB_LABEL;
7743f2f8
OD
1794 if (OspfSR.self != NULL) {
1795 OspfSR.self->srgb.range_size = OspfSR.srgb.range_size;
1796 OspfSR.self->srgb.lower_bound = OspfSR.srgb.lower_bound;
1797 }
cf9b9f77
OD
1798
1799 /* Set Router Information SR parameters */
1800 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1801
1802 /* Update NHLFE entries */
1803 hash_iterate(OspfSR.neighbors,
e3b78da8 1804 (void (*)(struct hash_bucket *, void *))update_in_nhlfe,
cf9b9f77
OD
1805 NULL);
1806
1807 return CMD_SUCCESS;
1808}
1809
1810DEFUN (sr_node_msd,
1811 sr_node_msd_cmd,
1812 "segment-routing node-msd (1-16)",
1813 SR_STR
1814 "Maximum Stack Depth for this router\n"
1815 "Maximum number of label that could be stack (1-16)\n")
1816{
93f0a26e 1817 uint32_t msd;
62c99790 1818 int idx = 1;
cf9b9f77
OD
1819
1820 if (!ospf_sr_enabled(vty))
1821 return CMD_WARNING_CONFIG_FAILED;
1822
7743f2f8
OD
1823 /* Get MSD */
1824 argv_find(argv, argc, "(1-16)", &idx);
1825 msd = strtoul(argv[idx]->arg, NULL, 10);
cf9b9f77 1826 if (msd < 1 || msd > MPLS_MAX_LABELS) {
7743f2f8 1827 vty_out(vty, "MSD must be comprise between 1 and %u\n",
cf9b9f77
OD
1828 MPLS_MAX_LABELS);
1829 return CMD_WARNING_CONFIG_FAILED;
1830 }
1831
7743f2f8
OD
1832 /* Check if value has changed */
1833 if (OspfSR.msd == msd)
1834 return CMD_SUCCESS;
1835
cf9b9f77
OD
1836 /* Set this router MSD */
1837 OspfSR.msd = msd;
7743f2f8
OD
1838 if (OspfSR.self != NULL)
1839 OspfSR.self->msd = msd;
cf9b9f77
OD
1840
1841 /* Set Router Information SR parameters */
1842 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1843
1844 return CMD_SUCCESS;
1845}
1846
1847DEFUN (no_sr_node_msd,
1848 no_sr_node_msd_cmd,
7743f2f8 1849 "no segment-routing node-msd [(1-16)]",
cf9b9f77
OD
1850 NO_STR
1851 SR_STR
7743f2f8
OD
1852 "Maximum Stack Depth for this router\n"
1853 "Maximum number of label that could be stack (1-16)\n")
cf9b9f77
OD
1854{
1855
1856 if (!ospf_sr_enabled(vty))
1857 return CMD_WARNING_CONFIG_FAILED;
1858
1859 /* unset this router MSD */
1860 OspfSR.msd = 0;
7743f2f8
OD
1861 if (OspfSR.self != NULL)
1862 OspfSR.self->msd = 0;
cf9b9f77
OD
1863
1864 /* Set Router Information SR parameters */
1865 ospf_router_info_update_sr(true, OspfSR.srgb, 0);
1866
1867 return CMD_SUCCESS;
1868}
1869
1870DEFUN (sr_prefix_sid,
1871 sr_prefix_sid_cmd,
7743f2f8 1872 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
cf9b9f77
OD
1873 SR_STR
1874 "Prefix SID\n"
1875 "IPv4 Prefix as A.B.C.D/M\n"
1876 "SID index for this prefix in decimal (0-65535)\n"
7743f2f8
OD
1877 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1878 "Don't request Penultimate Hop Popping (PHP)\n")
cf9b9f77 1879{
7743f2f8 1880 int idx = 0;
cf9b9f77
OD
1881 struct prefix p;
1882 uint32_t index;
1883 struct listnode *node;
6aaf0fdd 1884 struct sr_prefix *srp, *new;
cf9b9f77
OD
1885 struct interface *ifp;
1886
1887 if (!ospf_sr_enabled(vty))
1888 return CMD_WARNING_CONFIG_FAILED;
1889
1890 /* Get network prefix */
7743f2f8 1891 argv_find(argv, argc, "A.B.C.D/M", &idx);
6aaf0fdd 1892 if (!str2prefix(argv[idx]->arg, &p)) {
996c9314 1893 vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg);
7743f2f8
OD
1894 return CMD_WARNING_CONFIG_FAILED;
1895 }
cf9b9f77
OD
1896
1897 /* Get & verify index value */
7743f2f8
OD
1898 argv_find(argv, argc, "(0-65535)", &idx);
1899 index = strtoul(argv[idx]->arg, NULL, 10);
cf9b9f77 1900 if (index > OspfSR.srgb.range_size - 1) {
7743f2f8 1901 vty_out(vty, "Index %u must be lower than range size %u\n",
cf9b9f77
OD
1902 index, OspfSR.srgb.range_size);
1903 return CMD_WARNING_CONFIG_FAILED;
1904 }
1905
1906 /* check that the index is not already used */
1907 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1908 if (srp->sid == index) {
7743f2f8 1909 vty_out(vty, "Index %u is already used\n", index);
cf9b9f77
OD
1910 return CMD_WARNING_CONFIG_FAILED;
1911 }
1912 }
1913
6aaf0fdd
OD
1914 /* Create new Extended Prefix to SRDB if not found */
1915 new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
1916 IPV4_ADDR_COPY(&new->nhlfe.prefv4.prefix, &p.u.prefix4);
1917 IPV4_ADDR_COPY(&new->nhlfe.nexthop, &p.u.prefix4);
1918 new->nhlfe.prefv4.prefixlen = p.prefixlen;
1919 new->nhlfe.prefv4.family = p.family;
1920 new->sid = index;
1921 /* Set NO PHP flag if present and compute NHLFE */
1922 if (argv_find(argv, argc, "no-php-flag", &idx)) {
1923 SET_FLAG(new->flags, EXT_SUBTLV_PREFIX_SID_NPFLG);
1924 new->nhlfe.label_in = index2label(new->sid, OspfSR.self->srgb);
70e98a7f 1925 new->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
6aaf0fdd 1926 }
7743f2f8
OD
1927
1928 if (IS_DEBUG_OSPF_SR)
996c9314
LB
1929 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1930 __func__, index, inet_ntoa(new->nhlfe.prefv4.prefix),
1931 new->nhlfe.prefv4.prefixlen);
7743f2f8 1932
cf9b9f77
OD
1933 /* Get Interface and check if it is a Loopback */
1934 ifp = if_lookup_prefix(&p, VRF_DEFAULT);
1935 if (ifp == NULL) {
7743f2f8
OD
1936 /*
1937 * Interface could be not yet available i.e. when this
cf9b9f77
OD
1938 * command is in the configuration file, OSPF is not yet
1939 * ready. In this case, store the prefix SID for latter
7743f2f8
OD
1940 * update of this Extended Prefix
1941 */
6aaf0fdd 1942 listnode_add(OspfSR.self->ext_prefix, new);
266469eb 1943 zlog_info(
7743f2f8 1944 "Interface for prefix %s/%u not found. Deferred LSA "
996c9314
LB
1945 "flooding",
1946 inet_ntoa(p.u.prefix4), p.prefixlen);
cf9b9f77
OD
1947 return CMD_SUCCESS;
1948 }
7743f2f8 1949
cf9b9f77
OD
1950 if (!if_is_loopback(ifp)) {
1951 vty_out(vty, "interface %s is not a Loopback\n", ifp->name);
6aaf0fdd 1952 XFREE(MTYPE_OSPF_SR_PARAMS, new);
cf9b9f77
OD
1953 return CMD_WARNING_CONFIG_FAILED;
1954 }
6aaf0fdd 1955 new->nhlfe.ifindex = ifp->ifindex;
cf9b9f77 1956
6aaf0fdd
OD
1957 /* Search if this prefix already exist */
1958 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1959 if ((IPV4_ADDR_SAME(&srp->nhlfe.prefv4.prefix, &p.u.prefix4)
996c9314 1960 && srp->nhlfe.prefv4.prefixlen == p.prefixlen))
6aaf0fdd
OD
1961 break;
1962 else
1963 srp = NULL;
1964 }
1965
1966 /* Update or Add this new SR Prefix */
1967 if (srp) {
1968 update_sid_nhlfe(srp->nhlfe, new->nhlfe);
1969 listnode_delete(OspfSR.self->ext_prefix, srp);
1970 listnode_add(OspfSR.self->ext_prefix, new);
1971 } else {
1972 listnode_add(OspfSR.self->ext_prefix, new);
1973 add_sid_nhlfe(new->nhlfe);
7743f2f8
OD
1974 }
1975
1976 /* Finally, update Extended Prefix LSA */
996c9314
LB
1977 new->instance = ospf_ext_schedule_prefix_index(
1978 ifp, new->sid, &new->nhlfe.prefv4, new->flags);
6aaf0fdd 1979 if (new->instance == 0) {
7743f2f8
OD
1980 vty_out(vty, "Unable to set index %u for prefix %s/%u\n", index,
1981 inet_ntoa(p.u.prefix4), p.prefixlen);
cf9b9f77
OD
1982 return CMD_WARNING;
1983 }
1984
1985 return CMD_SUCCESS;
1986}
1987
1988DEFUN (no_sr_prefix_sid,
1989 no_sr_prefix_sid_cmd,
7743f2f8 1990 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
cf9b9f77
OD
1991 NO_STR
1992 SR_STR
1993 "Prefix SID\n"
7743f2f8
OD
1994 "IPv4 Prefix as A.B.C.D/M\n"
1995 "SID index for this prefix in decimal (0-65535)\n"
1996 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1997 "Don't request Penultimate Hop Popping (PHP)\n")
cf9b9f77 1998{
7743f2f8 1999 int idx = 0;
cf9b9f77
OD
2000 struct prefix p;
2001 struct listnode *node;
2002 struct sr_prefix *srp;
2003 struct interface *ifp;
2004 bool found = false;
7743f2f8 2005 int rc;
cf9b9f77 2006
98c06e86
RW
2007 if (!ospf_sr_enabled(vty))
2008 return CMD_WARNING_CONFIG_FAILED;
2009
cf9b9f77 2010 /* Get network prefix */
7743f2f8
OD
2011 argv_find(argv, argc, "A.B.C.D/M", &idx);
2012 rc = str2prefix(argv[idx]->arg, &p);
2013 if (!rc) {
996c9314 2014 vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg);
7743f2f8
OD
2015 return CMD_WARNING_CONFIG_FAILED;
2016 }
cf9b9f77
OD
2017
2018 /* check that the prefix is already set */
2019 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp))
2020 if (IPV4_ADDR_SAME(&srp->nhlfe.prefv4.prefix, &p.u.prefix4)
7743f2f8 2021 && (srp->nhlfe.prefv4.prefixlen == p.prefixlen)) {
cf9b9f77 2022 found = true;
7743f2f8
OD
2023 break;
2024 }
cf9b9f77
OD
2025
2026 if (!found) {
2027 vty_out(vty, "Prefix %s is not found. Abort!\n",
7743f2f8 2028 argv[idx]->arg);
cf9b9f77
OD
2029 return CMD_WARNING_CONFIG_FAILED;
2030 }
2031
7743f2f8
OD
2032 /* Get Interface */
2033 ifp = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT);
cf9b9f77
OD
2034 if (ifp == NULL) {
2035 vty_out(vty, "interface for prefix %s not found.\n",
7743f2f8 2036 argv[idx]->arg);
cf9b9f77
OD
2037 return CMD_WARNING_CONFIG_FAILED;
2038 }
7743f2f8 2039
cf9b9f77 2040 /* Update Extended Prefix LSA */
7743f2f8 2041 if (!ospf_ext_schedule_prefix_index(ifp, 0, NULL, 0)) {
cf9b9f77
OD
2042 vty_out(vty, "No corresponding loopback interface. Abort!\n");
2043 return CMD_WARNING;
2044 }
2045
7743f2f8 2046 if (IS_DEBUG_OSPF_SR)
996c9314
LB
2047 zlog_debug("SR (%s): Remove Prefix %s/%u with index %u",
2048 __func__, inet_ntoa(srp->nhlfe.prefv4.prefix),
2049 srp->nhlfe.prefv4.prefixlen, srp->sid);
7743f2f8
OD
2050
2051 /* Delete NHLFE is NO-PHP is set */
2052 if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
2053 del_sid_nhlfe(srp->nhlfe);
2054
2055 /* OK, all is clean, remove SRP from SRDB */
2056 listnode_delete(OspfSR.self->ext_prefix, srp);
2057 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
2058
cf9b9f77
OD
2059 return CMD_SUCCESS;
2060}
2061
7743f2f8 2062
76665890 2063static void show_sr_node(struct vty *vty, struct json_object *json,
996c9314 2064 struct sr_node *srn)
cf9b9f77
OD
2065{
2066
2067 struct listnode *node;
2068 struct sr_link *srl;
2069 struct sr_prefix *srp;
2070 struct interface *itf;
76665890 2071 char pref[19];
7743f2f8 2072 char sid[22];
cf9b9f77 2073 char label[8];
76665890
OD
2074 json_object *json_node = NULL, *json_algo, *json_obj;
2075 json_object *json_prefix = NULL, *json_link = NULL;
cf9b9f77
OD
2076
2077 /* Sanity Check */
2078 if (srn == NULL)
2079 return;
2080
76665890
OD
2081 if (json) {
2082 json_node = json_object_new_object();
2083 json_object_string_add(json_node, "routerID",
996c9314 2084 inet_ntoa(srn->adv_router));
76665890 2085 json_object_int_add(json_node, "srgbSize",
996c9314 2086 srn->srgb.range_size);
76665890 2087 json_object_int_add(json_node, "srgbLabel",
996c9314 2088 srn->srgb.lower_bound);
76665890
OD
2089 json_algo = json_object_new_array();
2090 json_object_object_add(json_node, "algorithms", json_algo);
2091 for (int i = 0; i < ALGORITHM_COUNT; i++) {
2092 if (srn->algo[i] == SR_ALGORITHM_UNSET)
2093 continue;
2094 json_obj = json_object_new_object();
2095 char tmp[2];
2096
2097 snprintf(tmp, 2, "%u", i);
2098 json_object_string_add(json_obj, tmp,
996c9314
LB
2099 srn->algo[i] == SR_ALGORITHM_SPF
2100 ? "SPF"
2101 : "S-SPF");
76665890
OD
2102 json_object_array_add(json_algo, json_obj);
2103 }
2104 if (srn->msd != 0)
2105 json_object_int_add(json_node, "nodeMsd", srn->msd);
2106 } else {
2107 vty_out(vty, "SR-Node: %s", inet_ntoa(srn->adv_router));
996c9314
LB
2108 vty_out(vty, "\tSRGB (Size/Label): %u/%u", srn->srgb.range_size,
2109 srn->srgb.lower_bound);
76665890
OD
2110 vty_out(vty, "\tAlgorithm(s): %s",
2111 srn->algo[0] == SR_ALGORITHM_SPF ? "SPF" : "S-SPF");
2112 for (int i = 1; i < ALGORITHM_COUNT; i++) {
2113 if (srn->algo[i] == SR_ALGORITHM_UNSET)
2114 continue;
2115 vty_out(vty, "/%s",
996c9314
LB
2116 srn->algo[i] == SR_ALGORITHM_SPF ? "SPF"
2117 : "S-SPF");
76665890
OD
2118 }
2119 if (srn->msd != 0)
2120 vty_out(vty, "\tMSD: %u", srn->msd);
2121 }
2122
2123 if (!json) {
2124 vty_out(vty,
2125 "\n\n Prefix or Link Label In Label Out "
2126 "Node or Adj. SID Interface Nexthop\n");
2127 vty_out(vty,
2128 "------------------ -------- --------- "
2129 "--------------------- --------- ---------------\n");
2130 }
cf9b9f77 2131 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
996c9314
LB
2132 snprintf(pref, 19, "%s/%u", inet_ntoa(srp->nhlfe.prefv4.prefix),
2133 srp->nhlfe.prefv4.prefixlen);
7743f2f8 2134 snprintf(sid, 22, "SR Pfx (idx %u)", srp->sid);
70e98a7f 2135 if (srp->nhlfe.label_out == MPLS_LABEL_IMPLICIT_NULL)
cf9b9f77
OD
2136 sprintf(label, "pop");
2137 else
7743f2f8 2138 sprintf(label, "%u", srp->nhlfe.label_out);
cf9b9f77 2139 itf = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT);
76665890
OD
2140 if (json) {
2141 if (!json_prefix) {
2142 json_prefix = json_object_new_array();
2143 json_object_object_add(json_node,
996c9314
LB
2144 "extendedPrefix",
2145 json_prefix);
76665890
OD
2146 }
2147 json_obj = json_object_new_object();
2148 json_object_string_add(json_obj, "prefix", pref);
2149 json_object_int_add(json_obj, "sid", srp->sid);
2150 json_object_int_add(json_obj, "inputLabel",
996c9314
LB
2151 srp->nhlfe.label_in);
2152 json_object_string_add(json_obj, "outputLabel", label);
76665890 2153 json_object_string_add(json_obj, "interface",
996c9314 2154 itf ? itf->name : "-");
76665890 2155 json_object_string_add(json_obj, "nexthop",
996c9314 2156 inet_ntoa(srp->nhlfe.nexthop));
76665890
OD
2157 json_object_array_add(json_prefix, json_obj);
2158 } else {
996c9314
LB
2159 vty_out(vty, "%18s %8u %9s %21s %9s %15s\n", pref,
2160 srp->nhlfe.label_in, label, sid,
2161 itf ? itf->name : "-",
76665890
OD
2162 inet_ntoa(srp->nhlfe.nexthop));
2163 }
cf9b9f77
OD
2164 }
2165
2166 for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) {
76665890 2167 snprintf(pref, 19, "%s/%u",
996c9314
LB
2168 inet_ntoa(srl->nhlfe[0].prefv4.prefix),
2169 srl->nhlfe[0].prefv4.prefixlen);
7743f2f8 2170 snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[0]);
70e98a7f 2171 if (srl->nhlfe[0].label_out == MPLS_LABEL_IMPLICIT_NULL)
cf9b9f77
OD
2172 sprintf(label, "pop");
2173 else
7743f2f8 2174 sprintf(label, "%u", srl->nhlfe[0].label_out);
cf9b9f77 2175 itf = if_lookup_by_index(srl->nhlfe[0].ifindex, VRF_DEFAULT);
76665890
OD
2176 if (json) {
2177 if (!json_link) {
2178 json_link = json_object_new_array();
996c9314
LB
2179 json_object_object_add(
2180 json_node, "extendedLink", json_link);
76665890
OD
2181 }
2182 /* Primary Link */
2183 json_obj = json_object_new_object();
2184 json_object_string_add(json_obj, "prefix", pref);
2185 json_object_int_add(json_obj, "sid", srl->sid[0]);
2186 json_object_int_add(json_obj, "inputLabel",
996c9314
LB
2187 srl->nhlfe[0].label_in);
2188 json_object_string_add(json_obj, "outputLabel", label);
76665890 2189 json_object_string_add(json_obj, "interface",
996c9314
LB
2190 itf ? itf->name : "-");
2191 json_object_string_add(
2192 json_obj, "nexthop",
76665890
OD
2193 inet_ntoa(srl->nhlfe[0].nexthop));
2194 json_object_array_add(json_link, json_obj);
2195 /* Backup Link */
2196 json_obj = json_object_new_object();
2197 snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]);
2198 if (srl->nhlfe[1].label_out == MPLS_LABEL_IMPLICIT_NULL)
2199 sprintf(label, "pop");
2200 else
2201 sprintf(label, "%u", srl->nhlfe[0].label_out);
2202 json_object_string_add(json_obj, "prefix", pref);
2203 json_object_int_add(json_obj, "sid", srl->sid[1]);
2204 json_object_int_add(json_obj, "inputLabel",
996c9314
LB
2205 srl->nhlfe[1].label_in);
2206 json_object_string_add(json_obj, "outputLabel", label);
76665890 2207 json_object_string_add(json_obj, "interface",
996c9314
LB
2208 itf ? itf->name : "-");
2209 json_object_string_add(
2210 json_obj, "nexthop",
76665890
OD
2211 inet_ntoa(srl->nhlfe[1].nexthop));
2212 json_object_array_add(json_link, json_obj);
2213 } else {
996c9314
LB
2214 vty_out(vty, "%18s %8u %9s %21s %9s %15s\n", pref,
2215 srl->nhlfe[0].label_in, label, sid,
2216 itf ? itf->name : "-",
76665890
OD
2217 inet_ntoa(srl->nhlfe[0].nexthop));
2218 snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]);
2219 if (srl->nhlfe[1].label_out == MPLS_LABEL_IMPLICIT_NULL)
2220 sprintf(label, "pop");
2221 else
2222 sprintf(label, "%u", srl->nhlfe[1].label_out);
996c9314
LB
2223 vty_out(vty, "%18s %8u %9s %21s %9s %15s\n", pref,
2224 srl->nhlfe[1].label_in, label, sid,
2225 itf ? itf->name : "-",
76665890
OD
2226 inet_ntoa(srl->nhlfe[1].nexthop));
2227 }
cf9b9f77 2228 }
76665890
OD
2229 if (json)
2230 json_object_array_add(json, json_node);
2231 else
2232 vty_out(vty, "\n");
cf9b9f77
OD
2233}
2234
e3b78da8 2235static void show_vty_srdb(struct hash_bucket *bucket, void *args)
cf9b9f77
OD
2236{
2237 struct vty *vty = (struct vty *)args;
e3b78da8 2238 struct sr_node *srn = (struct sr_node *)bucket->data;
cf9b9f77 2239
76665890
OD
2240 show_sr_node(vty, NULL, srn);
2241}
2242
e3b78da8 2243static void show_json_srdb(struct hash_bucket *bucket, void *args)
76665890
OD
2244{
2245 struct json_object *json = (struct json_object *)args;
e3b78da8 2246 struct sr_node *srn = (struct sr_node *)bucket->data;
76665890
OD
2247
2248 show_sr_node(NULL, json, srn);
cf9b9f77
OD
2249}
2250
2251DEFUN (show_ip_opsf_srdb,
2252 show_ip_ospf_srdb_cmd,
76665890 2253 "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
cf9b9f77
OD
2254 SHOW_STR
2255 IP_STR
2256 OSPF_STR
2257 "Database summary\n"
2258 "Show Segment Routing Data Base\n"
2259 "Advertising SR node\n"
2260 "Advertising SR node ID (as an IP address)\n"
76665890
OD
2261 "Self-originated SR node\n"
2262 JSON_STR)
cf9b9f77 2263{
7743f2f8 2264 int idx = 0;
cf9b9f77
OD
2265 struct in_addr rid;
2266 struct sr_node *srn;
088f1098 2267 bool uj = use_json(argc, argv);
76665890 2268 json_object *json = NULL, *json_node_array = NULL;
cf9b9f77
OD
2269
2270 if (!OspfSR.enabled) {
2271 vty_out(vty, "Segment Routing is disabled on this router\n");
7726c479 2272 return CMD_WARNING;
cf9b9f77
OD
2273 }
2274
76665890
OD
2275 if (uj) {
2276 json = json_object_new_object();
2277 json_node_array = json_object_new_array();
2278 json_object_string_add(json, "srdbID",
996c9314 2279 inet_ntoa(OspfSR.self->adv_router));
76665890
OD
2280 json_object_object_add(json, "srNodes", json_node_array);
2281 } else {
2282 vty_out(vty,
2283 "\n\t\tOSPF Segment Routing database for ID %s\n\n",
2284 inet_ntoa(OspfSR.self->adv_router));
2285 }
cf9b9f77 2286
7743f2f8
OD
2287 if (argv_find(argv, argc, "self-originate", &idx)) {
2288 srn = OspfSR.self;
76665890
OD
2289 show_sr_node(vty, json_node_array, srn);
2290 if (uj) {
2291 vty_out(vty, "%s\n",
996c9314
LB
2292 json_object_to_json_string_ext(
2293 json, JSON_C_TO_STRING_PRETTY));
76665890
OD
2294 json_object_free(json);
2295 }
7743f2f8
OD
2296 return CMD_SUCCESS;
2297 }
2298
2299 if (argv_find(argv, argc, "A.B.C.D", &idx)) {
2300 if (!inet_aton(argv[idx]->arg, &rid)) {
996c9314 2301 vty_out(vty, "Specified Router ID %s is invalid\n",
7743f2f8
OD
2302 argv[idx]->arg);
2303 return CMD_WARNING_CONFIG_FAILED;
cf9b9f77 2304 }
7743f2f8
OD
2305 /* Get the SR Node from the SRDB */
2306 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
2307 (void *)&rid);
76665890
OD
2308 show_sr_node(vty, json_node_array, srn);
2309 if (uj) {
2310 vty_out(vty, "%s\n",
996c9314
LB
2311 json_object_to_json_string_ext(
2312 json, JSON_C_TO_STRING_PRETTY));
76665890
OD
2313 json_object_free(json);
2314 }
7743f2f8 2315 return CMD_SUCCESS;
cf9b9f77 2316 }
7743f2f8
OD
2317
2318 /* No parameters have been provided, Iterate through all the SRDB */
76665890 2319 if (uj) {
e3b78da8 2320 hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
996c9314
LB
2321 void *))show_json_srdb,
2322 (void *)json_node_array);
2323 vty_out(vty, "%s\n", json_object_to_json_string_ext(
2324 json, JSON_C_TO_STRING_PRETTY));
76665890
OD
2325 json_object_free(json);
2326 } else {
e3b78da8 2327 hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
996c9314
LB
2328 void *))show_vty_srdb,
2329 (void *)vty);
76665890 2330 }
cf9b9f77
OD
2331 return CMD_SUCCESS;
2332}
2333
2334/* Install new CLI commands */
2335void ospf_sr_register_vty(void)
2336{
2337 install_element(VIEW_NODE, &show_ip_ospf_srdb_cmd);
2338
2339 install_element(OSPF_NODE, &ospf_sr_enable_cmd);
2340 install_element(OSPF_NODE, &no_ospf_sr_enable_cmd);
2341 install_element(OSPF_NODE, &sr_sid_label_range_cmd);
2342 install_element(OSPF_NODE, &no_sr_sid_label_range_cmd);
2343 install_element(OSPF_NODE, &sr_node_msd_cmd);
2344 install_element(OSPF_NODE, &no_sr_node_msd_cmd);
2345 install_element(OSPF_NODE, &sr_prefix_sid_cmd);
2346 install_element(OSPF_NODE, &no_sr_prefix_sid_cmd);
cf9b9f77 2347}