Coverage Report

Created: 2026-01-13 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/ospfd/ospf_sr.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * This is an implementation of Segment Routing
4
 * as per RFC 8665 - OSPF Extensions for Segment Routing
5
 * and RFC 8476 - Signaling Maximum SID Depth (MSD) Using OSPF
6
 *
7
 * Module name: Segment Routing
8
 *
9
 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
10
 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
11
 *
12
 * Copyright (C) 2016 - 2020 Orange Labs http://www.orange.com
13
 */
14
15
#ifdef HAVE_CONFIG_H
16
#include "config.h"
17
#endif
18
19
#include <math.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <zebra.h>
23
24
#include "printfrr.h"
25
#include "command.h"
26
#include "hash.h"
27
#include "if.h"
28
#include "if.h"
29
#include "jhash.h"
30
#include "libospf.h" /* for ospf interface types */
31
#include "linklist.h"
32
#include "log.h"
33
#include "memory.h"
34
#include "monotime.h"
35
#include "network.h"
36
#include "prefix.h"
37
#include "sockunion.h" /* for inet_aton() */
38
#include "stream.h"
39
#include "table.h"
40
#include "frrevent.h"
41
#include "vty.h"
42
#include "zclient.h"
43
#include "sbuf.h"
44
#include <lib/json.h>
45
#include "ospf_errors.h"
46
47
#include "ospfd/ospfd.h"
48
#include "ospfd/ospf_interface.h"
49
#include "ospfd/ospf_ism.h"
50
#include "ospfd/ospf_asbr.h"
51
#include "ospfd/ospf_lsa.h"
52
#include "ospfd/ospf_lsdb.h"
53
#include "ospfd/ospf_neighbor.h"
54
#include "ospfd/ospf_nsm.h"
55
#include "ospfd/ospf_flood.h"
56
#include "ospfd/ospf_packet.h"
57
#include "ospfd/ospf_spf.h"
58
#include "ospfd/ospf_dump.h"
59
#include "ospfd/ospf_route.h"
60
#include "ospfd/ospf_ase.h"
61
#include "ospfd/ospf_sr.h"
62
#include "ospfd/ospf_ri.h"
63
#include "ospfd/ospf_ext.h"
64
#include "ospfd/ospf_zebra.h"
65
66
/*
67
 * Global variable to manage Segment Routing on this node.
68
 * Note that all parameter values are stored in network byte order.
69
 */
70
static struct ospf_sr_db OspfSR;
71
static void ospf_sr_register_vty(void);
72
static inline void del_adj_sid(struct sr_nhlfe nhlfe);
73
static int ospf_sr_start(struct ospf *ospf);
74
75
/*
76
 * Segment Routing Data Base functions
77
 */
78
79
/* Hash function for Segment Routing entry */
80
static unsigned int sr_hash(const void *p)
81
0
{
82
0
  const struct in_addr *rid = p;
83
84
0
  return jhash_1word(rid->s_addr, 0);
85
0
}
86
87
/* Compare 2 Router ID hash entries based on SR Node */
88
static bool sr_cmp(const void *p1, const void *p2)
89
0
{
90
0
  const struct sr_node *srn = p1;
91
0
  const struct in_addr *rid = p2;
92
93
0
  return IPV4_ADDR_SAME(&srn->adv_router, rid);
94
0
}
95
96
/* Functions to remove an SR Link */
97
static void del_sr_link(void *val)
98
0
{
99
0
  struct sr_link *srl = (struct sr_link *)val;
100
101
0
  del_adj_sid(srl->nhlfe[0]);
102
0
  del_adj_sid(srl->nhlfe[1]);
103
0
  XFREE(MTYPE_OSPF_SR_PARAMS, val);
104
0
}
105
106
/* Functions to remove an SR Prefix */
107
static void del_sr_pref(void *val)
108
0
{
109
0
  struct sr_prefix *srp = (struct sr_prefix *)val;
110
111
0
  ospf_zebra_delete_prefix_sid(srp);
112
0
  XFREE(MTYPE_OSPF_SR_PARAMS, val);
113
0
}
114
115
/* Allocate new Segment Routine node */
116
static struct sr_node *sr_node_new(struct in_addr *rid)
117
0
{
118
119
0
  if (rid == NULL)
120
0
    return NULL;
121
122
0
  struct sr_node *new;
123
124
  /* Allocate Segment Routing node memory */
125
0
  new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_node));
126
127
  /* Default Algorithm, SRGB and MSD */
128
0
  for (int i = 0; i < ALGORITHM_COUNT; i++)
129
0
    new->algo[i] = SR_ALGORITHM_UNSET;
130
131
0
  new->srgb.range_size = 0;
132
0
  new->srgb.lower_bound = 0;
133
0
  new->msd = 0;
134
135
  /* Create Link, Prefix and Range TLVs list */
136
0
  new->ext_link = list_new();
137
0
  new->ext_prefix = list_new();
138
0
  new->ext_link->del = del_sr_link;
139
0
  new->ext_prefix->del = del_sr_pref;
140
141
0
  IPV4_ADDR_COPY(&new->adv_router, rid);
142
0
  new->neighbor = NULL;
143
0
  new->instance = 0;
144
145
0
  osr_debug("  |-  Created new SR node for %pI4", &new->adv_router);
146
0
  return new;
147
0
}
148
149
/* Supposed to be used for testing */
150
struct sr_node *ospf_sr_node_create(struct in_addr *rid)
151
0
{
152
0
  struct sr_node *srn;
153
154
0
  srn = hash_get(OspfSR.neighbors, (void *)rid, (void *)sr_node_new);
155
156
0
  return srn;
157
0
}
158
159
/* Delete Segment Routing node */
160
static void sr_node_del(struct sr_node *srn)
161
0
{
162
  /* Sanity Check */
163
0
  if (srn == NULL)
164
0
    return;
165
166
0
  osr_debug("  |- Delete SR node for %pI4", &srn->adv_router);
167
168
  /* Clean Extended Link */
169
0
  list_delete(&srn->ext_link);
170
171
  /* Clean Prefix List */
172
0
  list_delete(&srn->ext_prefix);
173
174
0
  XFREE(MTYPE_OSPF_SR_PARAMS, srn);
175
0
}
176
177
/* Get SR Node for a given nexthop */
178
static struct sr_node *get_sr_node_by_nexthop(struct ospf *ospf,
179
                struct in_addr nexthop)
180
0
{
181
0
  struct ospf_interface *oi = NULL;
182
0
  struct ospf_neighbor *nbr = NULL;
183
0
  struct listnode *node;
184
0
  struct route_node *rn;
185
0
  struct sr_node *srn;
186
0
  bool found;
187
188
  /* Sanity check */
189
0
  if (OspfSR.neighbors == NULL)
190
0
    return NULL;
191
192
0
  osr_debug("      |-  Search SR-Node for nexthop %pI4", &nexthop);
193
194
  /* First, search neighbor Router ID for this nexthop */
195
0
  found = false;
196
0
  for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
197
0
    for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
198
0
      nbr = rn->info;
199
0
      if ((nbr) && (IPV4_ADDR_SAME(&nexthop, &nbr->src))) {
200
0
        found = true;
201
0
        break;
202
0
      }
203
0
    }
204
0
    if (found)
205
0
      break;
206
0
  }
207
208
0
  if (!found)
209
0
    return NULL;
210
211
0
  osr_debug("      |-  Found nexthop Router ID %pI4", &nbr->router_id);
212
213
  /* Then, search SR Node */
214
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, &nbr->router_id);
215
216
0
  return srn;
217
0
}
218
219
/*
220
 * Segment Routing Local Block management functions
221
 */
222
223
/**
224
 * It is necessary to known which label is already allocated to manage the range
225
 * of SRLB. This is particular useful when an interface flap (goes up / down
226
 * frequently). Here, SR will release and then allocate label for the Adjacency
227
 * for each concerned interface. If we don't care, there is a risk to run out of
228
 * label.
229
 *
230
 * For that purpose, a similar principle as already provided to manage chunk of
231
 * label is proposed. But, here, the label chunk has not a fix range of 64
232
 * labels that could be easily manage with a single variable of 64 bits size.
233
 * So, used_mark is used as a bit wise to mark label reserved (bit set) or not
234
 * (bit unset). Its size is equal to the number of label of the SRLB range round
235
 * up to 64 bits.
236
 *
237
 *  - sr__local_block_init() computes the number of 64 bits variables that are
238
 *    needed to manage the SRLB range and allocates this number.
239
 *  - ospf_sr_local_block_request_label() pick up the first available label and
240
 *    set corresponding bit
241
 *  - ospf_sr_local_block_release_label() release label by reseting the
242
 *    corresponding bit and set the next label to the first free position
243
 */
244
245
/**
246
 * Initialize Segment Routing Local Block from SRDB configuration and reserve
247
 * block of bits to manage label allocation.
248
 *
249
 * @param lower_bound The lower bound of the SRLB range
250
 * @param upper_bound The upper bound of the SRLB range
251
 *
252
 * @return    0 on success, -1 otherwise
253
 */
254
static int sr_local_block_init(uint32_t lower_bound, uint32_t upper_bound)
255
0
{
256
0
  struct sr_local_block *srlb = &OspfSR.srlb;
257
0
  uint32_t size;
258
259
  /* Check if SRLB is not already configured */
260
0
  if (srlb->reserved)
261
0
    return 0;
262
263
  /*
264
   * Request SRLB to the label manager. If the allocation fails, return
265
   * an error to disable SR until a new SRLB is successfully allocated.
266
   */
267
0
  size = upper_bound - lower_bound + 1;
268
0
  if (ospf_zebra_request_label_range(lower_bound, size)) {
269
0
    zlog_err("SR: Error reserving SRLB [%u/%u] %u labels",
270
0
       lower_bound, upper_bound, size);
271
0
    return -1;
272
0
  }
273
274
0
  osr_debug("SR: Got new SRLB [%u/%u], %u labels", lower_bound,
275
0
      upper_bound, size);
276
277
  /* Initialize the SRLB */
278
0
  srlb->start = lower_bound;
279
0
  srlb->end = upper_bound;
280
0
  srlb->current = 0;
281
282
  /* Compute the needed Used Mark number and allocate them */
283
0
  srlb->max_block = size / SRLB_BLOCK_SIZE;
284
0
  if ((size % SRLB_BLOCK_SIZE) != 0)
285
0
    srlb->max_block++;
286
0
  srlb->used_mark = XCALLOC(MTYPE_OSPF_SR_PARAMS,
287
0
          srlb->max_block * SRLB_BLOCK_SIZE);
288
0
  srlb->reserved = true;
289
290
0
  return 0;
291
0
}
292
293
static int sr_global_block_init(uint32_t start, uint32_t size)
294
0
{
295
0
  struct sr_global_block *srgb = &OspfSR.srgb;
296
297
  /* Check if already configured */
298
0
  if (srgb->reserved)
299
0
    return 0;
300
301
  /* request chunk */
302
0
  uint32_t end = start + size - 1;
303
0
  if (ospf_zebra_request_label_range(start, size) < 0) {
304
0
    zlog_err("SR: Error reserving SRGB [%u/%u], %u labels", start,
305
0
       end, size);
306
0
    return -1;
307
0
  }
308
309
0
  osr_debug("SR: Got new SRGB [%u/%u], %u labels", start, end, size);
310
311
  /* success */
312
0
  srgb->start = start;
313
0
  srgb->size = size;
314
0
  srgb->reserved = true;
315
0
  return 0;
316
0
}
317
318
/**
319
 * Remove Segment Routing Local Block.
320
 *
321
 */
322
static void sr_local_block_delete(void)
323
0
{
324
0
  struct sr_local_block *srlb = &OspfSR.srlb;
325
326
  /* Check if SRLB is not already delete */
327
0
  if (!srlb->reserved)
328
0
    return;
329
330
0
  osr_debug("SR (%s): Remove SRLB [%u/%u]", __func__, srlb->start,
331
0
      srlb->end);
332
333
  /* First release the label block */
334
0
  ospf_zebra_release_label_range(srlb->start, srlb->end);
335
336
  /* Then reset SRLB structure */
337
0
  if (srlb->used_mark != NULL)
338
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srlb->used_mark);
339
340
0
  srlb->reserved = false;
341
0
}
342
343
/**
344
 * Remove Segment Routing Global block
345
 */
346
static void sr_global_block_delete(void)
347
0
{
348
0
  struct sr_global_block *srgb = &OspfSR.srgb;
349
350
0
  if (!srgb->reserved)
351
0
    return;
352
353
0
  osr_debug("SR (%s): Remove SRGB [%u/%u]", __func__, srgb->start,
354
0
      srgb->start + srgb->size - 1);
355
356
0
  ospf_zebra_release_label_range(srgb->start,
357
0
               srgb->start + srgb->size - 1);
358
359
0
  srgb->reserved = false;
360
0
}
361
362
363
/**
364
 * Request a label from the Segment Routing Local Block.
365
 *
366
 * @return  First available label on success or MPLS_INVALID_LABEL if the
367
 *    block of labels is full
368
 */
369
mpls_label_t ospf_sr_local_block_request_label(void)
370
0
{
371
0
  struct sr_local_block *srlb = &OspfSR.srlb;
372
0
  mpls_label_t label;
373
0
  uint32_t index;
374
0
  uint32_t pos;
375
0
  uint32_t size = srlb->end - srlb->start + 1;
376
377
  /* Check if we ran out of available labels */
378
0
  if (srlb->current >= size)
379
0
    return MPLS_INVALID_LABEL;
380
381
  /* Get first available label and mark it used */
382
0
  label = srlb->current + srlb->start;
383
0
  index = srlb->current / SRLB_BLOCK_SIZE;
384
0
  pos = 1ULL << (srlb->current % SRLB_BLOCK_SIZE);
385
0
  srlb->used_mark[index] |= pos;
386
387
  /* Jump to the next free position */
388
0
  srlb->current++;
389
0
  pos = srlb->current % SRLB_BLOCK_SIZE;
390
0
  while (srlb->current < size) {
391
0
    if (pos == 0)
392
0
      index++;
393
0
    if (!((1ULL << pos) & srlb->used_mark[index]))
394
0
      break;
395
0
    else {
396
0
      srlb->current++;
397
0
      pos = srlb->current % SRLB_BLOCK_SIZE;
398
0
    }
399
0
  }
400
401
0
  if (srlb->current == size)
402
0
    zlog_warn(
403
0
      "SR: Warning, SRLB is depleted and next label request will fail");
404
405
0
  return label;
406
0
}
407
408
/**
409
 * Release label in the Segment Routing Local Block.
410
 *
411
 * @param label Label to be release
412
 *
413
 * @return  0 on success or -1 if label falls outside SRLB
414
 */
415
int ospf_sr_local_block_release_label(mpls_label_t label)
416
0
{
417
0
  struct sr_local_block *srlb = &OspfSR.srlb;
418
0
  uint32_t index;
419
0
  uint32_t pos;
420
421
  /* Check that label falls inside the SRLB */
422
0
  if ((label < srlb->start) || (label > srlb->end)) {
423
0
    flog_warn(EC_OSPF_SR_SID_OVERFLOW,
424
0
        "%s: Returning label %u is outside SRLB [%u/%u]",
425
0
        __func__, label, srlb->start, srlb->end);
426
0
    return -1;
427
0
  }
428
429
0
  index = (label - srlb->start) / SRLB_BLOCK_SIZE;
430
0
  pos = 1ULL << ((label - srlb->start) % SRLB_BLOCK_SIZE);
431
0
  srlb->used_mark[index] &= ~pos;
432
  /* Reset current to the first available position */
433
0
  for (index = 0; index < srlb->max_block; index++) {
434
0
    if (srlb->used_mark[index] != 0xFFFFFFFFFFFFFFFF) {
435
0
      for (pos = 0; pos < SRLB_BLOCK_SIZE; pos++)
436
0
        if (!((1ULL << pos) & srlb->used_mark[index])) {
437
0
          srlb->current =
438
0
            index * SRLB_BLOCK_SIZE + pos;
439
0
          break;
440
0
        }
441
0
      break;
442
0
    }
443
0
  }
444
445
0
  return 0;
446
0
}
447
448
/*
449
 * Segment Routing Initialization functions
450
 */
451
452
/**
453
 * Thread function to re-attempt connection to the Label Manager and thus be
454
 * able to start Segment Routing.
455
 *
456
 * @param start   Thread structure that contains area as argument
457
 *
458
 * @return    1 on success
459
 */
460
static void sr_start_label_manager(struct event *start)
461
0
{
462
0
  struct ospf *ospf;
463
0
464
0
  ospf = EVENT_ARG(start);
465
0
466
0
  /* re-attempt to start SR & Label Manager connection */
467
0
  ospf_sr_start(ospf);
468
0
}
469
470
/* Segment Routing starter function */
471
static int ospf_sr_start(struct ospf *ospf)
472
0
{
473
0
  struct route_node *rn;
474
0
  struct ospf_lsa *lsa;
475
0
  struct sr_node *srn;
476
0
  int rc = 0;
477
478
0
  osr_debug("SR (%s): Start Segment Routing", __func__);
479
480
  /* Initialize self SR Node if not already done */
481
0
  if (OspfSR.self == NULL) {
482
0
    srn = hash_get(OspfSR.neighbors, (void *)&(ospf->router_id),
483
0
             (void *)sr_node_new);
484
485
    /* Complete & Store self SR Node */
486
0
    srn->srgb.range_size = OspfSR.srgb.size;
487
0
    srn->srgb.lower_bound = OspfSR.srgb.start;
488
0
    srn->srlb.lower_bound = OspfSR.srlb.start;
489
0
    srn->srlb.range_size = OspfSR.srlb.end - OspfSR.srlb.start + 1;
490
0
    srn->algo[0] = OspfSR.algo[0];
491
0
    srn->msd = OspfSR.msd;
492
0
    OspfSR.self = srn;
493
0
  }
494
495
  /* Then, start Label Manager if not ready */
496
0
  if (!ospf_zebra_label_manager_ready())
497
0
    if (ospf_zebra_label_manager_connect() < 0) {
498
      /* Re-attempt to connect to Label Manager in 1 sec. */
499
0
      event_add_timer(master, sr_start_label_manager, ospf, 1,
500
0
          &OspfSR.t_start_lm);
501
0
      osr_debug("  |- Failed to start the Label Manager");
502
0
      return -1;
503
0
    }
504
505
  /*
506
   * Request SRLB & SGRB to the label manager if not already reserved.
507
   * If the allocation fails, return an error to disable SR until a new
508
   * SRLB and/or SRGB are successfully allocated.
509
   */
510
0
  if (sr_local_block_init(OspfSR.srlb.start, OspfSR.srlb.end) < 0)
511
0
    return -1;
512
513
0
  if (sr_global_block_init(OspfSR.srgb.start, OspfSR.srgb.size) < 0)
514
0
    return -1;
515
516
  /* SR is UP and ready to flood LSA */
517
0
  OspfSR.status = SR_UP;
518
519
  /* Set Router Information SR parameters */
520
0
  osr_debug("SR: Activate SR for Router Information LSA");
521
522
0
  ospf_router_info_update_sr(true, OspfSR.self);
523
524
  /* Update Ext LSA */
525
0
  osr_debug("SR: Activate SR for Extended Link/Prefix LSA");
526
527
0
  ospf_ext_update_sr(true);
528
529
0
  osr_debug("SR (%s): Update SR-DB from LSDB", __func__);
530
531
  /* Start by looking to Router Info & Extended LSA in lsdb */
532
0
  if ((ospf != NULL) && (ospf->backbone != NULL)) {
533
0
    LSDB_LOOP (OPAQUE_AREA_LSDB(ospf->backbone), rn, lsa) {
534
0
      if (IS_LSA_MAXAGE(lsa) || IS_LSA_SELF(lsa))
535
0
        continue;
536
0
      int lsa_id =
537
0
        GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
538
0
      switch (lsa_id) {
539
0
      case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
540
0
        ospf_sr_ri_lsa_update(lsa);
541
0
        break;
542
0
      case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
543
0
        ospf_sr_ext_prefix_lsa_update(lsa);
544
0
        break;
545
0
      case OPAQUE_TYPE_EXTENDED_LINK_LSA:
546
0
        ospf_sr_ext_link_lsa_update(lsa);
547
0
        break;
548
0
      default:
549
0
        break;
550
0
      }
551
0
    }
552
0
  }
553
554
0
  rc = 1;
555
0
  return rc;
556
0
}
557
558
/* Stop Segment Routing */
559
static void ospf_sr_stop(void)
560
0
{
561
562
0
  if (OspfSR.status == SR_OFF)
563
0
    return;
564
565
0
  osr_debug("SR (%s): Stop Segment Routing", __func__);
566
567
  /* Disable any re-attempt to connect to Label Manager */
568
0
  EVENT_OFF(OspfSR.t_start_lm);
569
570
  /* Release SRGB if active */
571
0
  sr_global_block_delete();
572
573
  /* Release SRLB if active */
574
0
  sr_local_block_delete();
575
576
  /*
577
   * Remove all SR Nodes from the Hash table. Prefix and Link SID will
578
   * be remove though list_delete() call. See sr_node_del()
579
   */
580
0
  hash_clean(OspfSR.neighbors, (void *)sr_node_del);
581
0
  OspfSR.self = NULL;
582
0
  OspfSR.status = SR_OFF;
583
0
}
584
585
/*
586
 * Segment Routing initialize function
587
 *
588
 * @param - nothing
589
 *
590
 * @return 0 if OK, -1 otherwise
591
 */
592
int ospf_sr_init(void)
593
1
{
594
1
  int rc = -1;
595
596
1
  osr_debug("SR (%s): Initialize SR Data Base", __func__);
597
598
1
  memset(&OspfSR, 0, sizeof(OspfSR));
599
1
  OspfSR.status = SR_OFF;
600
  /* Only AREA flooding is supported in this release */
601
1
  OspfSR.scope = OSPF_OPAQUE_AREA_LSA;
602
603
  /* Initialize Algorithms, SRGB, SRLB and MSD TLVs */
604
  /* Only Algorithm SPF is supported */
605
1
  OspfSR.algo[0] = SR_ALGORITHM_SPF;
606
4
  for (int i = 1; i < ALGORITHM_COUNT; i++)
607
3
    OspfSR.algo[i] = SR_ALGORITHM_UNSET;
608
609
1
  OspfSR.srgb.size = DEFAULT_SRGB_SIZE;
610
1
  OspfSR.srgb.start = DEFAULT_SRGB_LABEL;
611
1
  OspfSR.srgb.reserved = false;
612
613
1
  OspfSR.srlb.start = DEFAULT_SRLB_LABEL;
614
1
  OspfSR.srlb.end = DEFAULT_SRLB_END;
615
1
  OspfSR.srlb.reserved = false;
616
1
  OspfSR.msd = 0;
617
618
  /* Initialize Hash table for neighbor SR nodes */
619
1
  OspfSR.neighbors = hash_create(sr_hash, sr_cmp, "OSPF_SR");
620
1
  if (OspfSR.neighbors == NULL)
621
0
    return rc;
622
623
  /* Register Segment Routing VTY command */
624
1
  ospf_sr_register_vty();
625
626
1
  rc = 0;
627
1
  return rc;
628
1
}
629
630
/*
631
 * Segment Routing termination function
632
 *
633
 * @param - nothing
634
 * @return - nothing
635
 */
636
void ospf_sr_term(void)
637
0
{
638
639
  /* Stop Segment Routing */
640
0
  ospf_sr_stop();
641
642
0
  hash_clean_and_free(&OspfSR.neighbors, (void *)sr_node_del);
643
0
}
644
645
/*
646
 * Segment Routing finish function
647
 *
648
 * @param - nothing
649
 * @return - nothing
650
 */
651
void ospf_sr_finish(void)
652
0
{
653
  /* Stop Segment Routing */
654
0
  ospf_sr_stop();
655
0
}
656
657
/*
658
 * Following functions are used to manipulate the
659
 * Next Hop Label Forwarding entry (NHLFE)
660
 */
661
662
/* Compute label from index */
663
static mpls_label_t index2label(uint32_t index, struct sr_block srgb)
664
0
{
665
0
  mpls_label_t label;
666
667
0
  label = srgb.lower_bound + index;
668
0
  if (label > (srgb.lower_bound + srgb.range_size)) {
669
0
    flog_warn(EC_OSPF_SR_SID_OVERFLOW,
670
0
        "%s: SID index %u falls outside SRGB range",
671
0
        __func__, index);
672
0
    return MPLS_INVALID_LABEL;
673
0
  } else
674
0
    return label;
675
0
}
676
677
/* Get the prefix sid for a specific router id */
678
mpls_label_t ospf_sr_get_prefix_sid_by_id(struct in_addr *id)
679
0
{
680
0
  struct sr_node *srn;
681
0
  struct sr_prefix *srp;
682
0
  mpls_label_t label;
683
684
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, id);
685
686
0
  if (srn) {
687
    /*
688
     * TODO: Here we assume that the SRGBs are the same,
689
     * and that the node's prefix SID is at the head of
690
     * the list, probably needs tweaking.
691
     */
692
0
    srp = listnode_head(srn->ext_prefix);
693
0
    label = index2label(srp->sid, srn->srgb);
694
0
  } else {
695
0
    label = MPLS_INVALID_LABEL;
696
0
  }
697
698
0
  return label;
699
0
}
700
701
/* Get the adjacency sid for a specific 'root' id and 'neighbor' id */
702
mpls_label_t ospf_sr_get_adj_sid_by_id(struct in_addr *root_id,
703
               struct in_addr *neighbor_id)
704
0
{
705
0
  struct sr_node *srn;
706
0
  struct sr_link *srl;
707
0
  mpls_label_t label;
708
0
  struct listnode *node;
709
710
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, root_id);
711
712
0
  label = MPLS_INVALID_LABEL;
713
714
0
  if (srn) {
715
0
    for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) {
716
0
      if (srl->type == ADJ_SID
717
0
          && srl->remote_id.s_addr == neighbor_id->s_addr) {
718
0
        label = srl->sid[0];
719
0
        break;
720
0
      }
721
0
    }
722
0
  }
723
724
0
  return label;
725
0
}
726
727
/* Get neighbor full structure from address */
728
static struct ospf_neighbor *get_neighbor_by_addr(struct ospf *top,
729
              struct in_addr addr)
730
0
{
731
0
  struct ospf_neighbor *nbr;
732
0
  struct ospf_interface *oi;
733
0
  struct listnode *node;
734
0
  struct route_node *rn;
735
736
  /* Sanity Check */
737
0
  if (top == NULL)
738
0
    return NULL;
739
740
0
  for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi))
741
0
    for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
742
0
      nbr = rn->info;
743
0
      if (!nbr)
744
0
        continue;
745
746
0
      if (IPV4_ADDR_SAME(&nbr->address.u.prefix4, &addr) ||
747
0
          IPV4_ADDR_SAME(&nbr->router_id, &addr)) {
748
0
        route_unlock_node(rn);
749
0
        return nbr;
750
0
      }
751
0
    }
752
0
  return NULL;
753
0
}
754
755
/* Get OSPF Path from address */
756
static struct ospf_route *get_nexthop_by_addr(struct ospf *top,
757
                struct prefix_ipv4 p)
758
0
{
759
0
  struct route_node *rn;
760
761
  /* Sanity Check */
762
0
  if (top == NULL)
763
0
    return NULL;
764
765
0
  osr_debug("      |-  Search Nexthop for prefix %pFX",
766
0
      (struct prefix *)&p);
767
768
0
  rn = route_node_lookup(top->new_table, (struct prefix *)&p);
769
770
  /*
771
   * Check if we found an OSPF route. May be NULL if SPF has not
772
   * yet populate routing table for this prefix.
773
   */
774
0
  if (rn == NULL)
775
0
    return NULL;
776
777
0
  route_unlock_node(rn);
778
0
  return rn->info;
779
0
}
780
781
/* Compute NHLFE entry for Extended Link */
782
static int compute_link_nhlfe(struct sr_link *srl)
783
0
{
784
0
  struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
785
0
  struct ospf_neighbor *nh;
786
0
  int rc = 0;
787
788
0
  osr_debug("    |-  Compute NHLFE for link %pI4", &srl->itf_addr);
789
790
  /* First determine the OSPF Neighbor */
791
0
  nh = get_neighbor_by_addr(top, srl->nhlfe[0].nexthop);
792
793
  /* Neighbor could be not found when OSPF Adjacency just fire up
794
   * because SPF don't yet populate routing table. This NHLFE will
795
   * be fixed later when SR SPF schedule will be called.
796
   */
797
0
  if (nh == NULL)
798
0
    return rc;
799
800
0
  osr_debug("    |-  Found nexthop %pI4", &nh->router_id);
801
802
  /* Set ifindex for this neighbor */
803
0
  srl->nhlfe[0].ifindex = nh->oi->ifp->ifindex;
804
0
  srl->nhlfe[1].ifindex = nh->oi->ifp->ifindex;
805
806
  /* Update neighbor address for LAN_ADJ_SID */
807
0
  if (srl->type == LAN_ADJ_SID) {
808
0
    IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &nh->src);
809
0
    IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &nh->src);
810
0
  }
811
812
  /* Set Input & Output Label */
813
0
  if (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
814
0
    srl->nhlfe[0].label_in = srl->sid[0];
815
0
  else
816
0
    srl->nhlfe[0].label_in =
817
0
      index2label(srl->sid[0], srl->srn->srgb);
818
0
  if (CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
819
0
    srl->nhlfe[1].label_in = srl->sid[1];
820
0
  else
821
0
    srl->nhlfe[1].label_in =
822
0
      index2label(srl->sid[1], srl->srn->srgb);
823
824
0
  srl->nhlfe[0].label_out = MPLS_LABEL_IMPLICIT_NULL;
825
0
  srl->nhlfe[1].label_out = MPLS_LABEL_IMPLICIT_NULL;
826
827
0
  rc = 1;
828
0
  return rc;
829
0
}
830
831
/**
832
 * Compute output label for the given Prefix-SID.
833
 *
834
 * @param srp   Segment Routing Prefix
835
 * @param srnext  Segment Routing nexthop node
836
 *
837
 * @return    MPLS label or MPLS_INVALID_LABEL in case of error
838
 */
839
static mpls_label_t sr_prefix_out_label(const struct sr_prefix *srp,
840
          const struct sr_node *srnext)
841
0
{
842
  /* Check if the nexthop SR Node is the last hop? */
843
0
  if (srnext == srp->srn) {
844
    /* SR-Node doesn't request NO-PHP. Return Implicit NULL label */
845
0
    if (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
846
0
      return MPLS_LABEL_IMPLICIT_NULL;
847
848
    /* SR-Node requests Explicit NULL Label */
849
0
    if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG))
850
0
      return MPLS_LABEL_IPV4_EXPLICIT_NULL;
851
    /* Fallthrough */
852
0
  }
853
854
  /* Return SID value as MPLS label if it is an Absolute SID */
855
0
  if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG
856
0
             | EXT_SUBTLV_PREFIX_SID_LFLG)) {
857
    /*
858
     * V/L SIDs have local significance, so only adjacent routers
859
     * can use them (RFC8665 section #5)
860
     */
861
0
    if (srp->srn != srnext)
862
0
      return MPLS_INVALID_LABEL;
863
0
    return srp->sid;
864
0
  }
865
866
  /* Return MPLS label as SRGB lower bound + SID index as per RFC 8665 */
867
0
  return (index2label(srp->sid, srnext->srgb));
868
0
}
869
870
/*
871
 * Compute NHLFE entry for Extended Prefix
872
 *
873
 * @param srp - Segment Routing Prefix
874
 *
875
 * @return -1 if no route is found, 0 if there is no SR route ready
876
 *         and 1 if success or update
877
 */
878
static int compute_prefix_nhlfe(struct sr_prefix *srp)
879
0
{
880
0
  struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
881
0
  struct ospf_path *path;
882
0
  struct listnode *node;
883
0
  struct sr_node *srnext;
884
0
  int rc = -1;
885
886
0
  osr_debug("    |-  Compute NHLFE for prefix %pFX",
887
0
      (struct prefix *)&srp->prefv4);
888
889
890
  /* First determine the nexthop */
891
0
  srp->route = get_nexthop_by_addr(top, srp->prefv4);
892
893
  /* Nexthop could be not found when OSPF Adjacency just fire up
894
   * because SPF don't yet populate routing table. This NHLFE will
895
   * be fixed later when SR SPF schedule will be called.
896
   */
897
0
  if (srp->route == NULL)
898
0
    return rc;
899
900
  /* Compute Input Label with self SRGB */
901
0
  srp->label_in = index2label(srp->sid, OspfSR.self->srgb);
902
903
0
  rc = 0;
904
0
  for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
905
906
0
    osr_debug("    |-  Process new route via %pI4 for this prefix",
907
0
        &path->nexthop);
908
909
    /*
910
     * Get SR-Node for this nexthop. Could be not yet available
911
     * as Extended Link / Prefix and Router Information are flooded
912
     * after LSA Type 1 & 2 which populate the OSPF Route Table
913
     */
914
0
    srnext = get_sr_node_by_nexthop(top, path->nexthop);
915
0
    if (srnext == NULL)
916
0
      continue;
917
918
    /* And store this information for later update */
919
0
    srnext->neighbor = OspfSR.self;
920
0
    path->srni.nexthop = srnext;
921
922
    /*
923
     * SR Node could be known, but SRGB could be not initialize
924
     * This is due to the fact that Extended Link / Prefix could
925
     * be received before corresponding Router Information LSA
926
     */
927
0
    if (srnext == NULL || srnext->srgb.lower_bound == 0
928
0
        || srnext->srgb.range_size == 0) {
929
0
      osr_debug(
930
0
        "    |-  SR-Node %pI4 not ready. Stop process",
931
0
        &srnext->adv_router);
932
0
      path->srni.label_out = MPLS_INVALID_LABEL;
933
0
      continue;
934
0
    }
935
936
0
    osr_debug("    |-  Found SRGB %u/%u for next hop SR-Node %pI4",
937
0
        srnext->srgb.range_size, srnext->srgb.lower_bound,
938
0
        &srnext->adv_router);
939
940
    /* Compute Output Label with Nexthop SR Node SRGB */
941
0
    path->srni.label_out = sr_prefix_out_label(srp, srnext);
942
943
0
    osr_debug("    |-  Computed new labels in: %u out: %u",
944
0
        srp->label_in, path->srni.label_out);
945
0
    rc = 1;
946
0
  }
947
0
  return rc;
948
0
}
949
950
/* Add new NHLFE entry for Adjacency SID */
951
static inline void add_adj_sid(struct sr_nhlfe nhlfe)
952
0
{
953
0
  if (nhlfe.label_in != 0)
954
0
    ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_ADD, nhlfe);
955
0
}
956
957
/* Remove NHLFE entry for Adjacency SID */
958
static inline void del_adj_sid(struct sr_nhlfe nhlfe)
959
0
{
960
0
  if (nhlfe.label_in != 0)
961
0
    ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_DELETE, nhlfe);
962
0
}
963
964
/* Update NHLFE entry for Adjacency SID */
965
static inline void update_adj_sid(struct sr_nhlfe n1, struct sr_nhlfe n2)
966
0
{
967
0
  del_adj_sid(n1);
968
0
  add_adj_sid(n2);
969
0
}
970
971
/*
972
 * Functions to parse and get Extended Link / Prefix
973
 * TLVs and SubTLVs
974
 */
975
976
/* Extended Link SubTLVs Getter */
977
static struct sr_link *get_ext_link_sid(struct tlv_header *tlvh, size_t size)
978
0
{
979
980
0
  struct sr_link *srl;
981
0
  struct ext_tlv_link *link = (struct ext_tlv_link *)tlvh;
982
0
  struct ext_subtlv_adj_sid *adj_sid;
983
0
  struct ext_subtlv_lan_adj_sid *lan_sid;
984
0
  struct ext_subtlv_rmt_itf_addr *rmt_itf;
985
986
0
  struct tlv_header *sub_tlvh;
987
0
  uint16_t length = 0, sum = 0, i = 0;
988
989
  /* Check TLV size */
990
0
  if ((ntohs(tlvh->length) > size)
991
0
      || ntohs(tlvh->length) < EXT_TLV_LINK_SIZE) {
992
0
    zlog_warn("Wrong Extended Link TLV size. Abort!");
993
0
    return NULL;
994
0
  }
995
996
0
  srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link));
997
998
  /* Initialize TLV browsing */
999
0
  length = ntohs(tlvh->length) - EXT_TLV_LINK_SIZE;
1000
0
  sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
1001
0
           + EXT_TLV_LINK_SIZE);
1002
0
  for (; sum < length && sub_tlvh; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
1003
0
    switch (ntohs(sub_tlvh->type)) {
1004
0
    case EXT_SUBTLV_ADJ_SID:
1005
0
      adj_sid = (struct ext_subtlv_adj_sid *)sub_tlvh;
1006
0
      srl->type = ADJ_SID;
1007
0
      i = CHECK_FLAG(adj_sid->flags,
1008
0
               EXT_SUBTLV_LINK_ADJ_SID_BFLG)
1009
0
            ? 1
1010
0
            : 0;
1011
0
      srl->flags[i] = adj_sid->flags;
1012
0
      if (CHECK_FLAG(adj_sid->flags,
1013
0
               EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1014
0
        srl->sid[i] = GET_LABEL(ntohl(adj_sid->value));
1015
0
      else
1016
0
        srl->sid[i] = ntohl(adj_sid->value);
1017
0
      IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop, &link->link_id);
1018
0
      break;
1019
0
    case EXT_SUBTLV_LAN_ADJ_SID:
1020
0
      lan_sid = (struct ext_subtlv_lan_adj_sid *)sub_tlvh;
1021
0
      srl->type = LAN_ADJ_SID;
1022
0
      i = CHECK_FLAG(lan_sid->flags,
1023
0
               EXT_SUBTLV_LINK_ADJ_SID_BFLG)
1024
0
            ? 1
1025
0
            : 0;
1026
0
      srl->flags[i] = lan_sid->flags;
1027
0
      if (CHECK_FLAG(lan_sid->flags,
1028
0
               EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1029
0
        srl->sid[i] = GET_LABEL(ntohl(lan_sid->value));
1030
0
      else
1031
0
        srl->sid[i] = ntohl(lan_sid->value);
1032
0
      IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop,
1033
0
               &lan_sid->neighbor_id);
1034
0
      break;
1035
0
    case EXT_SUBTLV_RMT_ITF_ADDR:
1036
0
      rmt_itf = (struct ext_subtlv_rmt_itf_addr *)sub_tlvh;
1037
0
      IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &rmt_itf->value);
1038
0
      IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &rmt_itf->value);
1039
0
      break;
1040
0
    default:
1041
0
      break;
1042
0
    }
1043
0
    sum += TLV_SIZE(sub_tlvh);
1044
0
  }
1045
1046
0
  IPV4_ADDR_COPY(&srl->itf_addr, &link->link_data);
1047
1048
0
  osr_debug("  |-  Found primary %u and backup %u Adj/Lan Sid for %pI4",
1049
0
      srl->sid[0], srl->sid[1], &srl->itf_addr);
1050
1051
0
  return srl;
1052
0
}
1053
1054
/* Extended Prefix SubTLVs Getter */
1055
static struct sr_prefix *get_ext_prefix_sid(struct tlv_header *tlvh,
1056
              size_t size)
1057
0
{
1058
1059
0
  struct sr_prefix *srp;
1060
0
  struct ext_tlv_prefix *pref = (struct ext_tlv_prefix *)tlvh;
1061
0
  struct ext_subtlv_prefix_sid *psid;
1062
1063
0
  struct tlv_header *sub_tlvh;
1064
0
  uint16_t length = 0, sum = 0;
1065
1066
  /* Check TLV size */
1067
0
  if ((ntohs(tlvh->length) > size)
1068
0
      || ntohs(tlvh->length) < EXT_TLV_PREFIX_SIZE) {
1069
0
    zlog_warn("Wrong Extended Link TLV size. Abort!");
1070
0
    return NULL;
1071
0
  }
1072
1073
0
  srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
1074
1075
  /* Initialize TLV browsing */
1076
0
  length = ntohs(tlvh->length) - EXT_TLV_PREFIX_SIZE;
1077
0
  sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
1078
0
           + EXT_TLV_PREFIX_SIZE);
1079
0
  for (; sum < length && sub_tlvh; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
1080
0
    switch (ntohs(sub_tlvh->type)) {
1081
0
    case EXT_SUBTLV_PREFIX_SID:
1082
0
      psid = (struct ext_subtlv_prefix_sid *)sub_tlvh;
1083
0
      if (psid->algorithm != SR_ALGORITHM_SPF) {
1084
0
        flog_err(EC_OSPF_INVALID_ALGORITHM,
1085
0
           "SR (%s): Unsupported Algorithm",
1086
0
           __func__);
1087
0
        XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1088
0
        return NULL;
1089
0
      }
1090
0
      srp->type = PREF_SID;
1091
0
      srp->flags = psid->flags;
1092
0
      if (CHECK_FLAG(psid->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
1093
0
        srp->sid = GET_LABEL(ntohl(psid->value));
1094
0
      else
1095
0
        srp->sid = ntohl(psid->value);
1096
0
      IPV4_ADDR_COPY(&srp->prefv4.prefix, &pref->address);
1097
0
      srp->prefv4.prefixlen = pref->pref_length;
1098
0
      srp->prefv4.family = AF_INET;
1099
0
      apply_mask_ipv4(&srp->prefv4);
1100
0
      break;
1101
0
    default:
1102
0
      break;
1103
0
    }
1104
0
    sum += TLV_SIZE(sub_tlvh);
1105
0
  }
1106
1107
0
  osr_debug("  |-  Found SID %u for prefix %pFX", srp->sid,
1108
0
      (struct prefix *)&srp->prefv4);
1109
1110
0
  return srp;
1111
0
}
1112
1113
/*
1114
 * Functions to manipulate Segment Routing Link & Prefix structures
1115
 */
1116
1117
/* Compare two Segment Link: return 0 if equal, 1 otherwise */
1118
static inline int sr_link_cmp(struct sr_link *srl1, struct sr_link *srl2)
1119
0
{
1120
0
  if ((srl1->sid[0] == srl2->sid[0]) && (srl1->sid[1] == srl2->sid[1])
1121
0
      && (srl1->type == srl2->type) && (srl1->flags[0] == srl2->flags[0])
1122
0
      && (srl1->flags[1] == srl2->flags[1]))
1123
0
    return 0;
1124
0
  else
1125
0
    return 1;
1126
0
}
1127
1128
/* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
1129
static inline int sr_prefix_cmp(struct sr_prefix *srp1, struct sr_prefix *srp2)
1130
0
{
1131
0
  if ((srp1->sid == srp2->sid) && (srp1->flags == srp2->flags))
1132
0
    return 0;
1133
0
  else
1134
0
    return 1;
1135
0
}
1136
1137
/* Update Segment Link of given Segment Routing Node */
1138
static void update_ext_link_sid(struct sr_node *srn, struct sr_link *srl,
1139
        uint8_t lsa_flags)
1140
0
{
1141
0
  struct listnode *node;
1142
0
  struct sr_link *lk;
1143
0
  bool found = false;
1144
0
  bool config = true;
1145
1146
  /* Sanity check */
1147
0
  if ((srn == NULL) || (srl == NULL))
1148
0
    return;
1149
1150
0
  osr_debug("  |-  Process Extended Link Adj/Lan-SID");
1151
1152
  /* Detect if Adj/Lan_Adj SID must be configured */
1153
0
  if (!CHECK_FLAG(lsa_flags, OSPF_LSA_SELF)
1154
0
      && (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG)
1155
0
    || CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG)))
1156
0
    config = false;
1157
1158
  /* Search for existing Segment Link */
1159
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, lk))
1160
0
    if (lk->instance == srl->instance) {
1161
0
      found = true;
1162
0
      break;
1163
0
    }
1164
1165
0
  osr_debug("  |-  %s SR Link 8.0.0.%u for SR node %pI4",
1166
0
      found ? "Update" : "Add", GET_OPAQUE_ID(srl->instance),
1167
0
      &srn->adv_router);
1168
1169
  /* if not found, add new Segment Link and install NHLFE */
1170
0
  if (!found) {
1171
    /* Complete SR-Link and add it to SR-Node list */
1172
0
    srl->srn = srn;
1173
0
    IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router);
1174
0
    listnode_add(srn->ext_link, srl);
1175
    /* Try to set MPLS table */
1176
0
    if (config && compute_link_nhlfe(srl)) {
1177
0
      add_adj_sid(srl->nhlfe[0]);
1178
0
      add_adj_sid(srl->nhlfe[1]);
1179
0
    }
1180
0
  } else {
1181
    /* Update SR-Link if they are different */
1182
0
    if (sr_link_cmp(lk, srl)) {
1183
      /* Try to set MPLS table */
1184
0
      if (config) {
1185
0
        if (compute_link_nhlfe(srl)) {
1186
0
          update_adj_sid(lk->nhlfe[0],
1187
0
                   srl->nhlfe[0]);
1188
0
          update_adj_sid(lk->nhlfe[1],
1189
0
                   srl->nhlfe[1]);
1190
0
        } else {
1191
0
          del_adj_sid(lk->nhlfe[0]);
1192
0
          del_adj_sid(lk->nhlfe[1]);
1193
0
        }
1194
0
      }
1195
      /* Replace SR-Link in SR-Node Adjacency List */
1196
0
      listnode_delete(srn->ext_link, lk);
1197
0
      XFREE(MTYPE_OSPF_SR_PARAMS, lk);
1198
0
      srl->srn = srn;
1199
0
      IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router);
1200
0
      listnode_add(srn->ext_link, srl);
1201
0
    } else {
1202
      /*
1203
       * This is just an LSA refresh.
1204
       * Stop processing and free SR Link
1205
       */
1206
0
      XFREE(MTYPE_OSPF_SR_PARAMS, srl);
1207
0
    }
1208
0
  }
1209
0
}
1210
1211
/* Update Segment Prefix of given Segment Routing Node */
1212
static void update_ext_prefix_sid(struct sr_node *srn, struct sr_prefix *srp)
1213
0
{
1214
1215
0
  struct listnode *node;
1216
0
  struct sr_prefix *pref;
1217
0
  bool found = false;
1218
1219
  /* Sanity check */
1220
0
  if (srn == NULL || srp == NULL)
1221
0
    return;
1222
1223
0
  osr_debug("  |-  Process Extended Prefix SID %u", srp->sid);
1224
1225
  /* Process only Global Prefix SID */
1226
0
  if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_LFLG))
1227
0
    return;
1228
1229
  /* Search for existing Segment Prefix */
1230
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, pref))
1231
0
    if (pref->instance == srp->instance
1232
0
        && prefix_same((struct prefix *)&srp->prefv4,
1233
0
           &pref->prefv4)) {
1234
0
      found = true;
1235
0
      break;
1236
0
    }
1237
1238
0
  osr_debug("  |-  %s SR LSA ID 7.0.0.%u for SR node %pI4",
1239
0
      found ? "Update" : "Add", GET_OPAQUE_ID(srp->instance),
1240
0
      &srn->adv_router);
1241
1242
  /* Complete SR-Prefix */
1243
0
  srp->srn = srn;
1244
0
  IPV4_ADDR_COPY(&srp->adv_router, &srn->adv_router);
1245
1246
  /* if not found, add new Segment Prefix and install NHLFE */
1247
0
  if (!found) {
1248
    /* Add it to SR-Node list ... */
1249
0
    listnode_add(srn->ext_prefix, srp);
1250
    /* ... and try to set MPLS table */
1251
0
    if (compute_prefix_nhlfe(srp) == 1)
1252
0
      ospf_zebra_update_prefix_sid(srp);
1253
0
  } else {
1254
    /*
1255
     * An old SR prefix exist. Check if something changes or if it
1256
     * is just a refresh.
1257
     */
1258
0
    if (sr_prefix_cmp(pref, srp)) {
1259
0
      if (compute_prefix_nhlfe(srp) == 1) {
1260
0
        ospf_zebra_delete_prefix_sid(pref);
1261
        /* Replace Segment Prefix */
1262
0
        listnode_delete(srn->ext_prefix, pref);
1263
0
        XFREE(MTYPE_OSPF_SR_PARAMS, pref);
1264
0
        listnode_add(srn->ext_prefix, srp);
1265
0
        ospf_zebra_update_prefix_sid(srp);
1266
0
      } else {
1267
        /* New NHLFE was not found.
1268
         * Just free the SR Prefix
1269
         */
1270
0
        XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1271
0
      }
1272
0
    } else {
1273
      /* This is just an LSA refresh.
1274
       * Stop processing and free SR Prefix
1275
       */
1276
0
      XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1277
0
    }
1278
0
  }
1279
0
}
1280
1281
/*
1282
 * When change the FRR Self SRGB, update the NHLFE Input Label
1283
 * for all Extended Prefix with SID index through hash_iterate()
1284
 */
1285
static void update_in_nhlfe(struct hash_bucket *bucket, void *args)
1286
0
{
1287
0
  struct listnode *node;
1288
0
  struct sr_node *srn = (struct sr_node *)bucket->data;
1289
0
  struct sr_prefix *srp;
1290
1291
  /* Process Every Extended Prefix for this SR-Node */
1292
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1293
    /* Process Self SRN only if NO-PHP is requested */
1294
0
    if ((srn == OspfSR.self)
1295
0
        && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
1296
0
      continue;
1297
1298
    /* Process only SID Index */
1299
0
    if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
1300
0
      continue;
1301
1302
    /* First, remove old MPLS table entries ... */
1303
0
    ospf_zebra_delete_prefix_sid(srp);
1304
    /* ... then compute new input label ... */
1305
0
    srp->label_in = index2label(srp->sid, OspfSR.self->srgb);
1306
    /* ... and install new MPLS LFIB */
1307
0
    ospf_zebra_update_prefix_sid(srp);
1308
0
  }
1309
0
}
1310
1311
/*
1312
 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1313
 * with SID index which use the given SR-Node as nexthop through hash_iterate()
1314
 */
1315
static void update_out_nhlfe(struct hash_bucket *bucket, void *args)
1316
0
{
1317
0
  struct listnode *node, *pnode;
1318
0
  struct sr_node *srn = (struct sr_node *)bucket->data;
1319
0
  struct sr_node *srnext = (struct sr_node *)args;
1320
0
  struct sr_prefix *srp;
1321
0
  struct ospf_path *path;
1322
1323
  /* Skip Self SR-Node */
1324
0
  if (srn == OspfSR.self)
1325
0
    return;
1326
1327
0
  osr_debug("SR (%s): Update Out NHLFE for neighbor SR-Node %pI4",
1328
0
      __func__, &srn->adv_router);
1329
1330
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1331
    /* Skip Prefix that has not yet a valid route */
1332
0
    if (srp->route == NULL)
1333
0
      continue;
1334
1335
0
    for (ALL_LIST_ELEMENTS_RO(srp->route->paths, pnode, path)) {
1336
      /* Skip path that has not next SR-Node as nexthop */
1337
0
      if (path->srni.nexthop != srnext)
1338
0
        continue;
1339
1340
      /* Compute new Output Label */
1341
0
      path->srni.label_out = sr_prefix_out_label(srp, srnext);
1342
0
    }
1343
1344
    /* Finally update MPLS table */
1345
0
    ospf_zebra_update_prefix_sid(srp);
1346
0
  }
1347
0
}
1348
1349
/*
1350
 * Following functions are call when new Segment Routing LSA are received
1351
 *  - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1352
 *  - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1353
 *  - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1354
 */
1355
1356
/* Update Segment Routing from Router Information LSA */
1357
void ospf_sr_ri_lsa_update(struct ospf_lsa *lsa)
1358
0
{
1359
0
  struct sr_node *srn;
1360
0
  struct tlv_header *tlvh;
1361
0
  struct lsa_header *lsah = lsa->data;
1362
0
  struct ri_sr_tlv_sid_label_range *ri_srgb = NULL;
1363
0
  struct ri_sr_tlv_sid_label_range *ri_srlb = NULL;
1364
0
  struct ri_sr_tlv_sr_algorithm *algo = NULL;
1365
0
  struct sr_block srgb;
1366
0
  uint16_t length = 0, sum = 0;
1367
0
  uint8_t msd = 0;
1368
1369
0
  osr_debug("SR (%s): Process Router Information LSA 4.0.0.%u from %pI4",
1370
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1371
0
      &lsah->adv_router);
1372
1373
  /* Sanity check */
1374
0
  if (IS_LSA_SELF(lsa))
1375
0
    return;
1376
1377
0
  if (OspfSR.neighbors == NULL) {
1378
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1379
0
       "SR (%s): Abort! no valid SR DataBase", __func__);
1380
0
    return;
1381
0
  }
1382
1383
  /* Search SR Node in hash table from Router ID */
1384
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1385
0
              &lsah->adv_router);
1386
1387
1388
  /* Collect Router Information Sub TLVs */
1389
  /* Initialize TLV browsing */
1390
0
  length = lsa->size - OSPF_LSA_HEADER_SIZE;
1391
0
  srgb.range_size = 0;
1392
0
  srgb.lower_bound = 0;
1393
1394
0
  for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL);
1395
0
       tlvh = TLV_HDR_NEXT(tlvh)) {
1396
0
    switch (ntohs(tlvh->type)) {
1397
0
    case RI_SR_TLV_SR_ALGORITHM:
1398
0
      algo = (struct ri_sr_tlv_sr_algorithm *)tlvh;
1399
0
      break;
1400
0
    case RI_SR_TLV_SRGB_LABEL_RANGE:
1401
0
      ri_srgb = (struct ri_sr_tlv_sid_label_range *)tlvh;
1402
0
      break;
1403
0
    case RI_SR_TLV_SRLB_LABEL_RANGE:
1404
0
      ri_srlb = (struct ri_sr_tlv_sid_label_range *)tlvh;
1405
0
      break;
1406
0
    case RI_SR_TLV_NODE_MSD:
1407
0
      msd = ((struct ri_sr_tlv_node_msd *)(tlvh))->value;
1408
0
      break;
1409
0
    default:
1410
0
      break;
1411
0
    }
1412
0
    sum += TLV_SIZE(tlvh);
1413
0
  }
1414
1415
  /* Check if Segment Routing Capabilities has been found */
1416
0
  if (ri_srgb == NULL) {
1417
    /* Skip Router Information without SR capabilities
1418
     * advertise by a non SR Node */
1419
0
    if (srn == NULL) {
1420
0
      return;
1421
0
    } else {
1422
      /* Remove SR Node that advertise Router Information
1423
       * without SR capabilities. This could correspond to a
1424
       * Node stopping Segment Routing */
1425
0
      hash_release(OspfSR.neighbors, &(srn->adv_router));
1426
0
      sr_node_del(srn);
1427
0
      return;
1428
0
    }
1429
0
  }
1430
1431
  /* Check that RI LSA belongs to the correct SR Node */
1432
0
  if ((srn != NULL) && (srn->instance != 0)
1433
0
      && (srn->instance != ntohl(lsah->id.s_addr))) {
1434
0
    flog_err(EC_OSPF_SR_INVALID_LSA_ID,
1435
0
       "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4/%u",
1436
0
       __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1437
0
       &lsah->adv_router, srn->instance);
1438
0
    return;
1439
0
  }
1440
1441
  /* OK. All things look good. Get SRGB */
1442
0
  srgb.range_size = GET_RANGE_SIZE(ntohl(ri_srgb->size));
1443
0
  srgb.lower_bound = GET_LABEL(ntohl(ri_srgb->lower.value));
1444
1445
  /* Check if it is a new SR Node or not */
1446
0
  if (srn == NULL) {
1447
    /* Get a new SR Node in hash table from Router ID */
1448
0
    srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1449
0
             &lsah->adv_router,
1450
0
             (void *)sr_node_new);
1451
    /* update LSA ID */
1452
0
    srn->instance = ntohl(lsah->id.s_addr);
1453
    /* Copy SRGB */
1454
0
    srn->srgb.range_size = srgb.range_size;
1455
0
    srn->srgb.lower_bound = srgb.lower_bound;
1456
0
  }
1457
1458
  /* Update Algorithm, SRLB and MSD if present */
1459
0
  if (algo != NULL) {
1460
0
    int i;
1461
0
    for (i = 0; i < ntohs(algo->header.length); i++)
1462
0
      srn->algo[i] = algo->value[0];
1463
0
    for (; i < ALGORITHM_COUNT; i++)
1464
0
      srn->algo[i] = SR_ALGORITHM_UNSET;
1465
0
  } else {
1466
0
    srn->algo[0] = SR_ALGORITHM_SPF;
1467
0
  }
1468
0
  srn->msd = msd;
1469
0
  if (ri_srlb != NULL) {
1470
0
    srn->srlb.range_size = GET_RANGE_SIZE(ntohl(ri_srlb->size));
1471
0
    srn->srlb.lower_bound = GET_LABEL(ntohl(ri_srlb->lower.value));
1472
0
  }
1473
1474
  /* Check if SRGB has changed */
1475
0
  if ((srn->srgb.range_size == srgb.range_size)
1476
0
      && (srn->srgb.lower_bound == srgb.lower_bound))
1477
0
    return;
1478
1479
  /* Copy SRGB */
1480
0
  srn->srgb.range_size = srgb.range_size;
1481
0
  srn->srgb.lower_bound = srgb.lower_bound;
1482
1483
0
  osr_debug("  |- Update SR-Node[%pI4], SRGB[%u/%u], SRLB[%u/%u], Algo[%u], MSD[%u]",
1484
0
      &srn->adv_router, srn->srgb.lower_bound, srn->srgb.range_size,
1485
0
      srn->srlb.lower_bound, srn->srlb.range_size, srn->algo[0],
1486
0
      srn->msd);
1487
1488
  /* ... and NHLFE if it is a neighbor SR node */
1489
0
  if (srn->neighbor == OspfSR.self)
1490
0
    hash_iterate(OspfSR.neighbors, update_out_nhlfe, srn);
1491
0
}
1492
1493
/*
1494
 * Delete SR Node entry in hash table information corresponding to an expired
1495
 * Router Information LSA
1496
 */
1497
void ospf_sr_ri_lsa_delete(struct ospf_lsa *lsa)
1498
0
{
1499
0
  struct sr_node *srn;
1500
0
  struct lsa_header *lsah = lsa->data;
1501
1502
0
  osr_debug("SR (%s): Remove SR node %pI4 from lsa_id 4.0.0.%u", __func__,
1503
0
      &lsah->adv_router, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)));
1504
1505
  /* Sanity check */
1506
0
  if (OspfSR.neighbors == NULL) {
1507
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1508
0
       "SR (%s): Abort! no valid SR Data Base", __func__);
1509
0
    return;
1510
0
  }
1511
1512
  /* Release Router ID entry in SRDB hash table */
1513
0
  srn = hash_release(OspfSR.neighbors, &(lsah->adv_router));
1514
1515
  /* Sanity check */
1516
0
  if (srn == NULL) {
1517
0
    flog_err(EC_OSPF_SR_NODE_CREATE,
1518
0
       "SR (%s): Abort! no entry in SRDB for SR Node %pI4",
1519
0
       __func__, &lsah->adv_router);
1520
0
    return;
1521
0
  }
1522
1523
0
  if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) {
1524
0
    flog_err(
1525
0
      EC_OSPF_SR_INVALID_LSA_ID,
1526
0
      "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4",
1527
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1528
0
      &lsah->adv_router);
1529
0
    return;
1530
0
  }
1531
1532
  /* Remove SR node */
1533
0
  sr_node_del(srn);
1534
0
}
1535
1536
/* Update Segment Routing from Extended Link LSA */
1537
void ospf_sr_ext_link_lsa_update(struct ospf_lsa *lsa)
1538
0
{
1539
0
  struct sr_node *srn;
1540
0
  struct tlv_header *tlvh;
1541
0
  struct lsa_header *lsah = lsa->data;
1542
0
  struct sr_link *srl;
1543
1544
0
  int length;
1545
1546
0
  osr_debug("SR (%s): Process Extended Link LSA 8.0.0.%u from %pI4",
1547
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1548
0
      &lsah->adv_router);
1549
1550
  /* Sanity check */
1551
0
  if (OspfSR.neighbors == NULL) {
1552
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1553
0
       "SR (%s): Abort! no valid SR DataBase", __func__);
1554
0
    return;
1555
0
  }
1556
1557
  /* Get SR Node in hash table from Router ID */
1558
0
  srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1559
0
           (void *)&(lsah->adv_router),
1560
0
           (void *)sr_node_new);
1561
1562
  /* Initialize TLV browsing */
1563
0
  length = lsa->size - OSPF_LSA_HEADER_SIZE;
1564
0
  for (tlvh = TLV_HDR_TOP(lsah); length > 0 && tlvh;
1565
0
       tlvh = TLV_HDR_NEXT(tlvh)) {
1566
0
    if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1567
      /* Got Extended Link information */
1568
0
      srl = get_ext_link_sid(tlvh, length);
1569
      /* Update SID if not null */
1570
0
      if (srl != NULL) {
1571
0
        srl->instance = ntohl(lsah->id.s_addr);
1572
0
        update_ext_link_sid(srn, srl, lsa->flags);
1573
0
      }
1574
0
    }
1575
0
    length -= TLV_SIZE(tlvh);
1576
0
  }
1577
0
}
1578
1579
/* Delete Segment Routing from Extended Link LSA */
1580
void ospf_sr_ext_link_lsa_delete(struct ospf_lsa *lsa)
1581
0
{
1582
0
  struct listnode *node;
1583
0
  struct sr_link *srl;
1584
0
  struct sr_node *srn;
1585
0
  struct lsa_header *lsah = lsa->data;
1586
0
  uint32_t instance = ntohl(lsah->id.s_addr);
1587
1588
0
  osr_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %pI4",
1589
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1590
0
      &lsah->adv_router);
1591
1592
  /* Sanity check */
1593
0
  if (OspfSR.neighbors == NULL) {
1594
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1595
0
       "SR (%s): Abort! no valid SR DataBase", __func__);
1596
0
    return;
1597
0
  }
1598
1599
  /* Search SR Node in hash table from Router ID */
1600
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1601
0
              (void *)&(lsah->adv_router));
1602
1603
  /*
1604
   * SR-Node may be NULL if it has been remove previously when
1605
   * processing Router Information LSA deletion
1606
   */
1607
0
  if (srn == NULL) {
1608
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1609
0
       "SR (%s): Stop! no entry in SRDB for SR Node %pI4",
1610
0
       __func__, &lsah->adv_router);
1611
0
    return;
1612
0
  }
1613
1614
  /* Search for corresponding Segment Link */
1615
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl))
1616
0
    if (srl->instance == instance)
1617
0
      break;
1618
1619
  /* Remove Segment Link if found. Note that for Neighbors, only Global
1620
   * Adj/Lan-Adj SID are stored in the SR-DB */
1621
0
  if ((srl != NULL) && (srl->instance == instance)) {
1622
0
    del_adj_sid(srl->nhlfe[0]);
1623
0
    del_adj_sid(srl->nhlfe[1]);
1624
0
    listnode_delete(srn->ext_link, srl);
1625
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srl);
1626
0
  }
1627
0
}
1628
1629
/* Add (LAN)Adjacency-SID from Extended Link Information */
1630
void ospf_sr_ext_itf_add(struct ext_itf *exti)
1631
0
{
1632
0
  struct sr_node *srn = OspfSR.self;
1633
0
  struct sr_link *srl;
1634
1635
0
  osr_debug("SR (%s): Add Extended Link LSA 8.0.0.%u from self", __func__,
1636
0
      exti->instance);
1637
1638
  /* Sanity check */
1639
0
  if (srn == NULL)
1640
0
    return;
1641
1642
  /* Initialize new Segment Routing Link */
1643
0
  srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link));
1644
0
  srl->srn = srn;
1645
0
  srl->adv_router = srn->adv_router;
1646
0
  srl->itf_addr = exti->link.link_data;
1647
0
  srl->instance =
1648
0
    SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance);
1649
0
  srl->remote_id = exti->link.link_id;
1650
0
  switch (exti->stype) {
1651
0
  case ADJ_SID:
1652
0
    srl->type = ADJ_SID;
1653
    /* Primary information */
1654
0
    srl->flags[0] = exti->adj_sid[0].flags;
1655
0
    if (CHECK_FLAG(exti->adj_sid[0].flags,
1656
0
             EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1657
0
      srl->sid[0] = GET_LABEL(ntohl(exti->adj_sid[0].value));
1658
0
    else
1659
0
      srl->sid[0] = ntohl(exti->adj_sid[0].value);
1660
0
    if (exti->rmt_itf_addr.header.type == 0)
1661
0
      srl->nhlfe[0].nexthop = exti->link.link_id;
1662
0
    else
1663
0
      srl->nhlfe[0].nexthop = exti->rmt_itf_addr.value;
1664
    /* Backup Information if set */
1665
0
    if (exti->adj_sid[1].header.type == 0)
1666
0
      break;
1667
0
    srl->flags[1] = exti->adj_sid[1].flags;
1668
0
    if (CHECK_FLAG(exti->adj_sid[1].flags,
1669
0
             EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1670
0
      srl->sid[1] = GET_LABEL(ntohl(exti->adj_sid[1].value));
1671
0
    else
1672
0
      srl->sid[1] = ntohl(exti->adj_sid[1].value);
1673
0
    if (exti->rmt_itf_addr.header.type == 0)
1674
0
      srl->nhlfe[1].nexthop = exti->link.link_id;
1675
0
    else
1676
0
      srl->nhlfe[1].nexthop = exti->rmt_itf_addr.value;
1677
0
    break;
1678
0
  case LAN_ADJ_SID:
1679
0
    srl->type = LAN_ADJ_SID;
1680
    /* Primary information */
1681
0
    srl->flags[0] = exti->lan_sid[0].flags;
1682
0
    if (CHECK_FLAG(exti->lan_sid[0].flags,
1683
0
             EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1684
0
      srl->sid[0] = GET_LABEL(ntohl(exti->lan_sid[0].value));
1685
0
    else
1686
0
      srl->sid[0] = ntohl(exti->lan_sid[0].value);
1687
0
    if (exti->rmt_itf_addr.header.type == 0)
1688
0
      srl->nhlfe[0].nexthop = exti->lan_sid[0].neighbor_id;
1689
0
    else
1690
0
      srl->nhlfe[0].nexthop = exti->rmt_itf_addr.value;
1691
    /* Backup Information if set */
1692
0
    if (exti->lan_sid[1].header.type == 0)
1693
0
      break;
1694
0
    srl->flags[1] = exti->lan_sid[1].flags;
1695
0
    if (CHECK_FLAG(exti->lan_sid[1].flags,
1696
0
             EXT_SUBTLV_LINK_ADJ_SID_VFLG))
1697
0
      srl->sid[1] = GET_LABEL(ntohl(exti->lan_sid[1].value));
1698
0
    else
1699
0
      srl->sid[1] = ntohl(exti->lan_sid[1].value);
1700
0
    if (exti->rmt_itf_addr.header.type == 0)
1701
0
      srl->nhlfe[1].nexthop = exti->lan_sid[1].neighbor_id;
1702
0
    else
1703
0
      srl->nhlfe[1].nexthop = exti->rmt_itf_addr.value;
1704
0
    break;
1705
0
  case PREF_SID:
1706
0
  case LOCAL_SID:
1707
    /* Wrong SID Type. Abort! */
1708
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srl);
1709
0
    return;
1710
0
  }
1711
1712
  /* Segment Routing Link is ready, update it */
1713
0
  update_ext_link_sid(srn, srl, OSPF_LSA_SELF);
1714
0
}
1715
1716
/* Delete Prefix or (LAN)Adjacency-SID from Extended Link Information */
1717
void ospf_sr_ext_itf_delete(struct ext_itf *exti)
1718
0
{
1719
0
  struct listnode *node;
1720
0
  struct sr_node *srn = OspfSR.self;
1721
0
  struct sr_prefix *srp = NULL;
1722
0
  struct sr_link *srl = NULL;
1723
0
  uint32_t instance;
1724
1725
0
  osr_debug("SR (%s): Remove Extended LSA %u.0.0.%u from self",
1726
0
      __func__, exti->stype == PREF_SID ? 7 : 8, exti->instance);
1727
1728
  /* Sanity check: SR-Node and Extended Prefix/Link list may have been
1729
   * removed earlier when stopping OSPF or OSPF-SR */
1730
0
  if (srn == NULL || srn->ext_prefix == NULL || srn->ext_link == NULL)
1731
0
    return;
1732
1733
0
  if (exti->stype == PREF_SID) {
1734
0
    instance = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA,
1735
0
             exti->instance);
1736
0
    for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp))
1737
0
      if (srp->instance == instance)
1738
0
        break;
1739
1740
    /* Uninstall Segment Prefix SID if found */
1741
0
    if ((srp != NULL) && (srp->instance == instance))
1742
0
      ospf_zebra_delete_prefix_sid(srp);
1743
0
  } else {
1744
    /* Search for corresponding Segment Link for self SR-Node */
1745
0
    instance = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA,
1746
0
             exti->instance);
1747
0
    for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl))
1748
0
      if (srl->instance == instance)
1749
0
        break;
1750
1751
    /* Remove Segment Link if found */
1752
0
    if ((srl != NULL) && (srl->instance == instance)) {
1753
0
      del_adj_sid(srl->nhlfe[0]);
1754
0
      del_adj_sid(srl->nhlfe[1]);
1755
0
      listnode_delete(srn->ext_link, srl);
1756
0
      XFREE(MTYPE_OSPF_SR_PARAMS, srl);
1757
0
    }
1758
0
  }
1759
0
}
1760
1761
/* Update Segment Routing from Extended Prefix LSA */
1762
void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa *lsa)
1763
0
{
1764
0
  struct sr_node *srn;
1765
0
  struct tlv_header *tlvh;
1766
0
  struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1767
0
  struct sr_prefix *srp;
1768
1769
0
  int length;
1770
1771
0
  osr_debug("SR (%s): Process Extended Prefix LSA 7.0.0.%u from %pI4",
1772
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1773
0
      &lsah->adv_router);
1774
1775
  /* Sanity check */
1776
0
  if (OspfSR.neighbors == NULL) {
1777
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1778
0
       "SR (%s): Abort! no valid SR DataBase", __func__);
1779
0
    return;
1780
0
  }
1781
1782
  /* Get SR Node in hash table from Router ID */
1783
0
  srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1784
0
           (void *)&(lsah->adv_router),
1785
0
           (void *)sr_node_new);
1786
  /* Initialize TLV browsing */
1787
0
  length = lsa->size - OSPF_LSA_HEADER_SIZE;
1788
0
  for (tlvh = TLV_HDR_TOP(lsah); length > 0 && tlvh;
1789
0
       tlvh = TLV_HDR_NEXT(tlvh)) {
1790
0
    if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1791
      /* Got Extended Link information */
1792
0
      srp = get_ext_prefix_sid(tlvh, length);
1793
      /* Update SID if not null */
1794
0
      if (srp != NULL) {
1795
0
        srp->instance = ntohl(lsah->id.s_addr);
1796
0
        update_ext_prefix_sid(srn, srp);
1797
0
      }
1798
0
    }
1799
0
    length -= TLV_SIZE(tlvh);
1800
0
  }
1801
0
}
1802
1803
/* Delete Segment Routing from Extended Prefix LSA */
1804
void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa *lsa)
1805
0
{
1806
0
  struct listnode *node;
1807
0
  struct sr_prefix *srp;
1808
0
  struct sr_node *srn;
1809
0
  struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1810
0
  uint32_t instance = ntohl(lsah->id.s_addr);
1811
1812
0
  osr_debug("SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %pI4",
1813
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1814
0
      &lsah->adv_router);
1815
1816
  /* Sanity check */
1817
0
  if (OspfSR.neighbors == NULL) {
1818
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1819
0
       "SR (%s): Abort! no valid SR DataBase", __func__);
1820
0
    return;
1821
0
  }
1822
1823
  /* Search SR Node in hash table from Router ID */
1824
0
  srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1825
0
              (void *)&(lsah->adv_router));
1826
1827
  /*
1828
   * SR-Node may be NULL if it has been remove previously when
1829
   * processing Router Information LSA deletion
1830
   */
1831
0
  if (srn == NULL) {
1832
0
    flog_err(EC_OSPF_SR_INVALID_DB,
1833
0
       "SR (%s):  Stop! no entry in SRDB for SR Node %pI4",
1834
0
       __func__, &lsah->adv_router);
1835
0
    return;
1836
0
  }
1837
1838
  /* Search for corresponding Segment Prefix */
1839
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp))
1840
0
    if (srp->instance == instance)
1841
0
      break;
1842
1843
  /* Remove Prefix if found */
1844
0
  if ((srp != NULL) && (srp->instance == instance)) {
1845
0
    ospf_zebra_delete_prefix_sid(srp);
1846
0
    listnode_delete(srn->ext_prefix, srp);
1847
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1848
0
  } else {
1849
0
    flog_err(
1850
0
      EC_OSPF_SR_INVALID_DB,
1851
0
      "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %pI4",
1852
0
      __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1853
0
      &lsah->adv_router);
1854
0
  }
1855
0
}
1856
1857
/*
1858
 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1859
 * complete initial CLI command at startup.
1860
 *
1861
 * @param ifp - Loopback interface
1862
 * @param pref - Prefix address of this interface
1863
 *
1864
 * @return - void
1865
 */
1866
void ospf_sr_update_local_prefix(struct interface *ifp, struct prefix *p)
1867
0
{
1868
0
  struct listnode *node;
1869
0
  struct sr_prefix *srp;
1870
1871
  /* Sanity Check */
1872
0
  if ((ifp == NULL) || (p == NULL))
1873
0
    return;
1874
1875
  /*
1876
   * Search if there is a Segment Prefix that correspond to this
1877
   * interface or prefix, and update it if found
1878
   */
1879
0
  for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1880
0
    if ((srp->nhlfe.ifindex == ifp->ifindex)
1881
0
        || ((IPV4_ADDR_SAME(&srp->prefv4.prefix, &p->u.prefix4))
1882
0
      && (srp->prefv4.prefixlen == p->prefixlen))) {
1883
1884
      /* Update Interface & Prefix info */
1885
0
      srp->nhlfe.ifindex = ifp->ifindex;
1886
0
      IPV4_ADDR_COPY(&srp->prefv4.prefix, &p->u.prefix4);
1887
0
      srp->prefv4.prefixlen = p->prefixlen;
1888
0
      srp->prefv4.family = p->family;
1889
0
      IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &p->u.prefix4);
1890
1891
      /* OK. Let's Schedule Extended Prefix LSA */
1892
0
      srp->instance = ospf_ext_schedule_prefix_index(
1893
0
        ifp, srp->sid, &srp->prefv4, srp->flags);
1894
1895
0
      osr_debug(
1896
0
        "  |-  Update Node SID %pFX - %u for self SR Node",
1897
0
        (struct prefix *)&srp->prefv4, srp->sid);
1898
1899
      /* Install SID if NO-PHP is set and not EXPLICIT-NULL */
1900
0
      if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)
1901
0
          && !CHECK_FLAG(srp->flags,
1902
0
             EXT_SUBTLV_PREFIX_SID_EFLG)) {
1903
0
        srp->label_in = index2label(srp->sid,
1904
0
                  OspfSR.self->srgb);
1905
0
        srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
1906
0
        ospf_zebra_update_prefix_sid(srp);
1907
0
      }
1908
0
    }
1909
0
  }
1910
0
}
1911
1912
/*
1913
 * Following functions are used to update MPLS LFIB after a SPF run
1914
 */
1915
1916
static void ospf_sr_nhlfe_update(struct hash_bucket *bucket, void *args)
1917
0
{
1918
1919
0
  struct sr_node *srn = (struct sr_node *)bucket->data;
1920
0
  struct listnode *node;
1921
0
  struct sr_prefix *srp;
1922
0
  bool old;
1923
0
  int rc;
1924
1925
0
  osr_debug("  |-  Update Prefix for SR Node %pI4", &srn->adv_router);
1926
1927
  /* Skip Self SR Node */
1928
0
  if (srn == OspfSR.self)
1929
0
    return;
1930
1931
  /* Update Extended Prefix */
1932
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1933
1934
    /* Keep track of valid route */
1935
0
    old = srp->route != NULL;
1936
1937
    /* Compute the new NHLFE */
1938
0
    rc = compute_prefix_nhlfe(srp);
1939
1940
    /* Check computation result */
1941
0
    switch (rc) {
1942
    /* Routes are not know, remove old NHLFE if any to avoid loop */
1943
0
    case -1:
1944
0
      if (old)
1945
0
        ospf_zebra_delete_prefix_sid(srp);
1946
0
      break;
1947
    /* Routes exist but are not ready, skip it */
1948
0
    case 0:
1949
0
      break;
1950
    /* There is at least one route, update NHLFE */
1951
0
    case 1:
1952
0
      ospf_zebra_update_prefix_sid(srp);
1953
0
      break;
1954
0
    default:
1955
0
      break;
1956
0
    }
1957
0
  }
1958
0
}
1959
1960
void ospf_sr_update_task(struct ospf *ospf)
1961
0
{
1962
1963
0
  struct timeval start_time, stop_time;
1964
1965
  /* Check ospf and SR status */
1966
0
  if ((ospf == NULL) || (OspfSR.status != SR_UP))
1967
0
    return;
1968
1969
0
  monotime(&start_time);
1970
1971
0
  osr_debug("SR (%s): Start SPF update", __func__);
1972
1973
0
  hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
1974
0
             void *))ospf_sr_nhlfe_update,
1975
0
         NULL);
1976
1977
0
  monotime(&stop_time);
1978
1979
0
  osr_debug("SR (%s): SPF Processing Time(usecs): %lld", __func__,
1980
0
      (stop_time.tv_sec - start_time.tv_sec) * 1000000LL
1981
0
        + (stop_time.tv_usec - start_time.tv_usec));
1982
0
}
1983
1984
/*
1985
 * --------------------------------------
1986
 * Following are vty command functions.
1987
 * --------------------------------------
1988
 */
1989
1990
/*
1991
 * Segment Routing Router configuration
1992
 *
1993
 * Must be centralize as it concerns both Extended Link/Prefix LSA
1994
 * and Router Information LSA. Choose to call it from Extended Prefix
1995
 * write_config() call back.
1996
 *
1997
 * @param vty VTY output
1998
 *
1999
 * @return none
2000
 */
2001
void ospf_sr_config_write_router(struct vty *vty)
2002
0
{
2003
0
  struct listnode *node;
2004
0
  struct sr_prefix *srp;
2005
0
  uint32_t upper;
2006
2007
0
  if (OspfSR.status == SR_UP)
2008
0
    vty_out(vty, " segment-routing on\n");
2009
2010
0
  upper = OspfSR.srgb.start + OspfSR.srgb.size - 1;
2011
0
  if ((OspfSR.srgb.start != DEFAULT_SRGB_LABEL)
2012
0
      || (OspfSR.srgb.size != DEFAULT_SRGB_SIZE))
2013
0
    vty_out(vty, " segment-routing global-block %u %u",
2014
0
      OspfSR.srgb.start, upper);
2015
2016
0
  if ((OspfSR.srlb.start != DEFAULT_SRLB_LABEL) ||
2017
0
      (OspfSR.srlb.end != DEFAULT_SRLB_END)) {
2018
0
    if ((OspfSR.srgb.start == DEFAULT_SRGB_LABEL) &&
2019
0
        (OspfSR.srgb.size == DEFAULT_SRGB_SIZE))
2020
0
      vty_out(vty, " segment-routing global-block %u %u",
2021
0
        OspfSR.srgb.start, upper);
2022
0
    vty_out(vty, " local-block %u %u\n", OspfSR.srlb.start,
2023
0
      OspfSR.srlb.end);
2024
0
  } else
2025
0
    vty_out(vty, "\n");
2026
2027
0
  if (OspfSR.msd != 0)
2028
0
    vty_out(vty, " segment-routing node-msd %u\n", OspfSR.msd);
2029
2030
0
  if (OspfSR.self != NULL) {
2031
0
    for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
2032
0
      vty_out(vty, " segment-routing prefix %pFX index %u",
2033
0
        &srp->prefv4, srp->sid);
2034
0
      if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG))
2035
0
        vty_out(vty, " explicit-null\n");
2036
0
      else if (CHECK_FLAG(srp->flags,
2037
0
              EXT_SUBTLV_PREFIX_SID_NPFLG))
2038
0
        vty_out(vty, " no-php-flag\n");
2039
0
      else
2040
0
        vty_out(vty, "\n");
2041
0
    }
2042
0
  }
2043
0
}
2044
2045
DEFUN(ospf_sr_enable,
2046
       ospf_sr_enable_cmd,
2047
       "segment-routing on",
2048
       SR_STR
2049
       "Enable Segment Routing\n")
2050
0
{
2051
2052
0
  VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
2053
2054
0
  if (OspfSR.status != SR_OFF)
2055
0
    return CMD_SUCCESS;
2056
2057
0
  if (ospf->vrf_id != VRF_DEFAULT) {
2058
0
    vty_out(vty,
2059
0
      "Segment Routing is only supported in default VRF\n");
2060
0
    return CMD_WARNING_CONFIG_FAILED;
2061
0
  }
2062
2063
0
  osr_debug("SR: Segment Routing: OFF -> ON");
2064
2065
  /* Start Segment Routing */
2066
0
  OspfSR.status = SR_ON;
2067
0
  ospf_sr_start(ospf);
2068
2069
0
  return CMD_SUCCESS;
2070
0
}
2071
2072
DEFUN (no_ospf_sr_enable,
2073
       no_ospf_sr_enable_cmd,
2074
       "no segment-routing [on]",
2075
       NO_STR
2076
       SR_STR
2077
       "Disable Segment Routing\n")
2078
0
{
2079
2080
0
  if (OspfSR.status == SR_OFF)
2081
0
    return CMD_SUCCESS;
2082
2083
0
  osr_debug("SR: Segment Routing: ON -> OFF");
2084
2085
  /* Start by Disabling Extended Link & Prefix LSA */
2086
0
  ospf_ext_update_sr(false);
2087
2088
  /* then, disable Router Information SR parameters */
2089
0
  ospf_router_info_update_sr(false, OspfSR.self);
2090
2091
  /* Finally, stop Segment Routing */
2092
0
  ospf_sr_stop();
2093
2094
0
  return CMD_SUCCESS;
2095
0
}
2096
2097
static int ospf_sr_enabled(struct vty *vty)
2098
0
{
2099
0
  if (OspfSR.status != SR_OFF)
2100
0
    return 1;
2101
2102
0
  if (vty)
2103
0
    vty_out(vty, "%% OSPF SR is not turned on\n");
2104
2105
0
  return 0;
2106
0
}
2107
2108
/* tell if two ranges [r1_lower, r1_upper] and [r2_lower,r2_upper] overlap */
2109
static bool ranges_overlap(uint32_t r1_lower, uint32_t r1_upper,
2110
         uint32_t r2_lower, uint32_t r2_upper)
2111
0
{
2112
0
  return !((r1_upper < r2_lower) || (r1_lower > r2_upper));
2113
0
}
2114
2115
2116
/* tell if a range is valid */
2117
static bool sr_range_is_valid(uint32_t lower, uint32_t upper, uint32_t min_size)
2118
0
{
2119
0
  return (upper >= lower + min_size);
2120
0
}
2121
2122
/**
2123
 * Update SRGB and/or SRLB using new CLI values.
2124
 *
2125
 * @param gb_lower  Lower bound of the SRGB
2126
 * @param gb_upper  Upper bound of the SRGB
2127
 * @param lb_lower  Lower bound of the SRLB
2128
 * @param lb_upper  Upper bound of the SRLB
2129
 *
2130
 * @return          0 on success, -1 otherwise
2131
 */
2132
static int update_sr_blocks(uint32_t gb_lower, uint32_t gb_upper,
2133
          uint32_t lb_lower, uint32_t lb_upper)
2134
0
{
2135
2136
  /* Check if values have changed */
2137
0
  bool gb_changed, lb_changed;
2138
0
  uint32_t gb_size = gb_upper - gb_lower + 1;
2139
0
  uint32_t lb_size = lb_upper - lb_lower + 1;
2140
2141
0
  gb_changed =
2142
0
    (OspfSR.srgb.size != gb_size || OspfSR.srgb.start != gb_lower);
2143
0
  lb_changed =
2144
0
    (OspfSR.srlb.end != lb_upper || OspfSR.srlb.start != lb_lower);
2145
0
  if (!gb_changed && !lb_changed)
2146
0
    return 0;
2147
2148
  /* Check if SR is correctly started i.e. Label Manager connected */
2149
0
  if (OspfSR.status != SR_UP) {
2150
0
    OspfSR.srgb.size = gb_size;
2151
0
    OspfSR.srgb.start = gb_lower;
2152
0
    OspfSR.srlb.end = lb_upper;
2153
0
    OspfSR.srlb.start = lb_lower;
2154
0
    return 0;
2155
0
  }
2156
2157
  /* Release old SRGB if it has changed and is active. */
2158
0
  if (gb_changed) {
2159
2160
0
    sr_global_block_delete();
2161
2162
    /* Set new SRGB values - but do not reserve yet (we need to
2163
     * release the SRLB too) */
2164
0
    OspfSR.srgb.size = gb_size;
2165
0
    OspfSR.srgb.start = gb_lower;
2166
0
    if (OspfSR.self != NULL) {
2167
0
      OspfSR.self->srgb.range_size = gb_size;
2168
0
      OspfSR.self->srgb.lower_bound = gb_lower;
2169
0
    }
2170
0
  }
2171
  /* Release old SRLB if it has changed and reserve new block as needed.
2172
   */
2173
0
  if (lb_changed) {
2174
2175
0
    sr_local_block_delete();
2176
2177
    /* Set new SRLB values */
2178
0
    if (sr_local_block_init(lb_lower, lb_upper) < 0) {
2179
0
      ospf_sr_stop();
2180
0
      return -1;
2181
0
    }
2182
0
    if (OspfSR.self != NULL) {
2183
0
      OspfSR.self->srlb.lower_bound = lb_lower;
2184
0
      OspfSR.self->srlb.range_size = lb_size;
2185
0
    }
2186
0
  }
2187
2188
  /*
2189
   * Try to reserve the new SRGB from the Label Manger. If the
2190
   * allocation fails, disable SR until new blocks are successfully
2191
   * allocated.
2192
   */
2193
0
  if (gb_changed) {
2194
0
    if (sr_global_block_init(OspfSR.srgb.start, OspfSR.srgb.size)
2195
0
        < 0) {
2196
0
      ospf_sr_stop();
2197
0
      return -1;
2198
0
    }
2199
0
  }
2200
2201
  /* Update Self SR-Node */
2202
0
  if (OspfSR.self != NULL) {
2203
    /* SRGB is reserved, set Router Information parameters */
2204
0
    ospf_router_info_update_sr(true, OspfSR.self);
2205
2206
    /* and update NHLFE entries */
2207
0
    if (gb_changed)
2208
0
      hash_iterate(OspfSR.neighbors,
2209
0
             (void (*)(struct hash_bucket *,
2210
0
                 void *))update_in_nhlfe,
2211
0
             NULL);
2212
2213
    /* and update (LAN)-Adjacency SID */
2214
0
    if (lb_changed)
2215
0
      ospf_ext_link_srlb_update();
2216
0
  }
2217
2218
0
  return 0;
2219
0
}
2220
2221
DEFUN(sr_global_label_range, sr_global_label_range_cmd,
2222
      "segment-routing global-block (16-1048575) (16-1048575) [local-block (16-1048575) (16-1048575)]",
2223
      SR_STR
2224
      "Segment Routing Global Block label range\n"
2225
      "Lower-bound range in decimal (16-1048575)\n"
2226
      "Upper-bound range in decimal (16-1048575)\n"
2227
      "Segment Routing Local Block label range\n"
2228
      "Lower-bound range in decimal (16-1048575)\n"
2229
      "Upper-bound range in decimal (16-1048575)\n")
2230
0
{
2231
0
  uint32_t lb_upper, lb_lower;
2232
0
  uint32_t gb_upper, gb_lower;
2233
0
  int idx_gb_low = 2, idx_gb_up = 3;
2234
0
  int idx_lb_low = 5, idx_lb_up = 6;
2235
2236
  /* Get lower and upper bound for mandatory global-block */
2237
0
  gb_lower = strtoul(argv[idx_gb_low]->arg, NULL, 10);
2238
0
  gb_upper = strtoul(argv[idx_gb_up]->arg, NULL, 10);
2239
2240
  /* SRLB values are taken from vtysh if there, else use the known ones */
2241
0
  lb_upper = argc > idx_lb_up ? strtoul(argv[idx_lb_up]->arg, NULL, 10)
2242
0
            : OspfSR.srlb.end;
2243
0
  lb_lower = argc > idx_lb_low ? strtoul(argv[idx_lb_low]->arg, NULL, 10)
2244
0
             : OspfSR.srlb.start;
2245
2246
  /* check correctness of input SRGB */
2247
0
  if (!sr_range_is_valid(gb_lower, gb_upper, MIN_SRGB_SIZE)) {
2248
0
    vty_out(vty, "Invalid SRGB range\n");
2249
0
    return CMD_WARNING_CONFIG_FAILED;
2250
0
  }
2251
2252
  /* check correctness of SRLB */
2253
0
  if (!sr_range_is_valid(lb_lower, lb_upper, MIN_SRLB_SIZE)) {
2254
0
    vty_out(vty, "Invalid SRLB range\n");
2255
0
    return CMD_WARNING_CONFIG_FAILED;
2256
0
  }
2257
2258
  /* Validate SRGB against SRLB */
2259
0
  if (ranges_overlap(gb_lower, gb_upper, lb_lower, lb_upper)) {
2260
0
    vty_out(vty,
2261
0
      "New SR Global Block (%u/%u) conflicts with Local Block (%u/%u)\n",
2262
0
      gb_lower, gb_upper, lb_lower, lb_upper);
2263
0
    return CMD_WARNING_CONFIG_FAILED;
2264
0
  }
2265
2266
0
  if (update_sr_blocks(gb_lower, gb_upper, lb_lower, lb_upper) < 0)
2267
0
    return CMD_WARNING_CONFIG_FAILED;
2268
0
  else
2269
0
    return CMD_SUCCESS;
2270
0
}
2271
2272
DEFUN(no_sr_global_label_range, no_sr_global_label_range_cmd,
2273
      "no segment-routing global-block [(16-1048575) (16-1048575) local-block (16-1048575) (16-1048575)]",
2274
      NO_STR SR_STR
2275
      "Segment Routing Global Block label range\n"
2276
      "Lower-bound range in decimal (16-1048575)\n"
2277
      "Upper-bound range in decimal (16-1048575)\n"
2278
      "Segment Routing Local Block label range\n"
2279
      "Lower-bound range in decimal (16-1048575)\n"
2280
      "Upper-bound range in decimal (16-1048575)\n")
2281
0
{
2282
0
  if (update_sr_blocks(DEFAULT_SRGB_LABEL, DEFAULT_SRGB_END,
2283
0
           DEFAULT_SRLB_LABEL, DEFAULT_SRLB_END)
2284
0
      < 0)
2285
0
    return CMD_WARNING_CONFIG_FAILED;
2286
0
  else
2287
0
    return CMD_SUCCESS;
2288
0
}
2289
2290
DEFUN (sr_node_msd,
2291
       sr_node_msd_cmd,
2292
       "segment-routing node-msd (1-16)",
2293
       SR_STR
2294
       "Maximum Stack Depth for this router\n"
2295
       "Maximum number of label that could be stack (1-16)\n")
2296
0
{
2297
0
  uint32_t msd;
2298
0
  int idx = 1;
2299
2300
0
  if (!ospf_sr_enabled(vty))
2301
0
    return CMD_WARNING_CONFIG_FAILED;
2302
2303
  /* Get MSD */
2304
0
  argv_find(argv, argc, "(1-16)", &idx);
2305
0
  msd = strtoul(argv[idx]->arg, NULL, 10);
2306
0
  if (msd < 1 || msd > MPLS_MAX_LABELS) {
2307
0
    vty_out(vty, "MSD must be comprise between 1 and %u\n",
2308
0
      MPLS_MAX_LABELS);
2309
0
    return CMD_WARNING_CONFIG_FAILED;
2310
0
  }
2311
2312
  /* Check if value has changed */
2313
0
  if (OspfSR.msd == msd)
2314
0
    return CMD_SUCCESS;
2315
2316
  /* Set this router MSD */
2317
0
  OspfSR.msd = msd;
2318
0
  if (OspfSR.self != NULL) {
2319
0
    OspfSR.self->msd = msd;
2320
2321
    /* Set Router Information parameters if SR is UP */
2322
0
    if (OspfSR.status == SR_UP)
2323
0
      ospf_router_info_update_sr(true, OspfSR.self);
2324
0
  }
2325
2326
0
  return CMD_SUCCESS;
2327
0
}
2328
2329
DEFUN (no_sr_node_msd,
2330
  no_sr_node_msd_cmd,
2331
  "no segment-routing node-msd [(1-16)]",
2332
  NO_STR
2333
  SR_STR
2334
  "Maximum Stack Depth for this router\n"
2335
  "Maximum number of label that could be stack (1-16)\n")
2336
0
{
2337
2338
0
  if (!ospf_sr_enabled(vty))
2339
0
    return CMD_WARNING_CONFIG_FAILED;
2340
2341
  /* unset this router MSD */
2342
0
  OspfSR.msd = 0;
2343
0
  if (OspfSR.self != NULL) {
2344
0
    OspfSR.self->msd = 0;
2345
2346
    /* Set Router Information parameters if SR is UP */
2347
0
    if (OspfSR.status == SR_UP)
2348
0
      ospf_router_info_update_sr(true, OspfSR.self);
2349
0
  }
2350
2351
0
  return CMD_SUCCESS;
2352
0
}
2353
2354
DEFUN (sr_prefix_sid,
2355
       sr_prefix_sid_cmd,
2356
       "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag|explicit-null]",
2357
       SR_STR
2358
       "Prefix SID\n"
2359
       "IPv4 Prefix as A.B.C.D/M\n"
2360
       "SID index for this prefix in decimal (0-65535)\n"
2361
       "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2362
       "Don't request Penultimate Hop Popping (PHP)\n"
2363
       "Upstream neighbor must replace prefix-sid with explicit null label\n")
2364
0
{
2365
0
  int idx = 0;
2366
0
  struct prefix p, pexist;
2367
0
  uint32_t index;
2368
0
  struct listnode *node;
2369
0
  struct sr_prefix *srp, *exist = NULL;
2370
0
  struct interface *ifp;
2371
0
  bool no_php_flag = false;
2372
0
  bool exp_null = false;
2373
0
  bool index_in_use = false;
2374
0
  uint8_t desired_flags = 0;
2375
2376
0
  if (!ospf_sr_enabled(vty))
2377
0
    return CMD_WARNING_CONFIG_FAILED;
2378
2379
  /* Get network prefix */
2380
0
  argv_find(argv, argc, "A.B.C.D/M", &idx);
2381
0
  if (!str2prefix(argv[idx]->arg, &p)) {
2382
0
    vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg);
2383
0
    return CMD_WARNING_CONFIG_FAILED;
2384
0
  }
2385
2386
  /* Get & verify index value */
2387
0
  argv_find(argv, argc, "(0-65535)", &idx);
2388
0
  index = strtoul(argv[idx]->arg, NULL, 10);
2389
0
  if (index > OspfSR.srgb.size - 1) {
2390
0
    vty_out(vty, "Index %u must be lower than range size %u\n",
2391
0
      index, OspfSR.srgb.size);
2392
0
    return CMD_WARNING_CONFIG_FAILED;
2393
0
  }
2394
2395
  /* Get options */
2396
0
  no_php_flag = argv_find(argv, argc, "no-php-flag", &idx);
2397
0
  exp_null = argv_find(argv, argc, "explicit-null", &idx);
2398
2399
0
  desired_flags |= no_php_flag ? EXT_SUBTLV_PREFIX_SID_NPFLG : 0;
2400
0
  desired_flags |= exp_null ? EXT_SUBTLV_PREFIX_SID_NPFLG : 0;
2401
0
  desired_flags |= exp_null ? EXT_SUBTLV_PREFIX_SID_EFLG : 0;
2402
2403
  /* Search for an existing Prefix-SID */
2404
0
  for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
2405
0
    if (prefix_same((struct prefix *)&srp->prefv4, &p))
2406
0
      exist = srp;
2407
0
    if (srp->sid == index) {
2408
0
      index_in_use = true;
2409
0
      pexist = p;
2410
0
    }
2411
0
  }
2412
2413
  /* done if prefix segment already there with same index and flags */
2414
0
  if (exist && exist->sid == index && exist->flags == desired_flags)
2415
0
    return CMD_SUCCESS;
2416
2417
  /* deny if index is already in use by a distinct prefix */
2418
0
  if (!exist && index_in_use) {
2419
0
    vty_out(vty, "Index %u is already used by %pFX\n", index,
2420
0
      &pexist);
2421
0
    return CMD_WARNING_CONFIG_FAILED;
2422
0
  }
2423
2424
  /* First, remove old NHLFE if installed */
2425
0
  if (exist && CHECK_FLAG(exist->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)
2426
0
      && !CHECK_FLAG(exist->flags, EXT_SUBTLV_PREFIX_SID_EFLG))
2427
0
    ospf_zebra_delete_prefix_sid(exist);
2428
2429
  /* Create new Extended Prefix to SRDB if not found */
2430
0
  if (exist == NULL) {
2431
0
    srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
2432
0
    IPV4_ADDR_COPY(&srp->prefv4.prefix, &p.u.prefix4);
2433
0
    srp->prefv4.prefixlen = p.prefixlen;
2434
0
    srp->prefv4.family = p.family;
2435
0
    srp->sid = index;
2436
0
    srp->type = LOCAL_SID;
2437
0
  } else {
2438
    /* we work on the existing SR prefix */
2439
0
    srp = exist;
2440
0
  }
2441
2442
  /* Reset labels to handle flag update */
2443
0
  srp->label_in = 0;
2444
0
  srp->nhlfe.label_out = 0;
2445
0
  srp->sid = index;
2446
0
  srp->flags = desired_flags;
2447
2448
  /* If NO PHP flag is present, compute NHLFE and set label */
2449
0
  if (no_php_flag) {
2450
0
    srp->label_in = index2label(srp->sid, OspfSR.self->srgb);
2451
0
    srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
2452
0
  }
2453
2454
0
  osr_debug("SR (%s): Add new index %u to Prefix %pFX", __func__, index,
2455
0
      (struct prefix *)&srp->prefv4);
2456
2457
  /* Get Interface and check if it is a Loopback */
2458
0
  ifp = if_lookup_prefix(&p, VRF_DEFAULT);
2459
0
  if (ifp == NULL) {
2460
    /*
2461
     * Interface could be not yet available i.e. when this
2462
     * command is in the configuration file, OSPF is not yet
2463
     * ready. In this case, store the prefix SID for latter
2464
     * update of this Extended Prefix
2465
     */
2466
0
    if (exist == NULL)
2467
0
      listnode_add(OspfSR.self->ext_prefix, srp);
2468
0
    zlog_info(
2469
0
      "Interface for prefix %pFX not found. Deferred LSA flooding",
2470
0
      &p);
2471
0
    return CMD_SUCCESS;
2472
0
  }
2473
2474
0
  if (!if_is_loopback(ifp)) {
2475
0
    vty_out(vty, "interface %s is not a Loopback\n", ifp->name);
2476
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srp);
2477
0
    return CMD_WARNING_CONFIG_FAILED;
2478
0
  }
2479
0
  srp->nhlfe.ifindex = ifp->ifindex;
2480
2481
  /* Add SR Prefix if new */
2482
0
  if (!exist)
2483
0
    listnode_add(OspfSR.self->ext_prefix, srp);
2484
2485
  /* Update Prefix SID if SR is UP */
2486
0
  if (OspfSR.status == SR_UP) {
2487
0
    if (no_php_flag && !exp_null)
2488
0
      ospf_zebra_update_prefix_sid(srp);
2489
0
  } else
2490
0
    return CMD_SUCCESS;
2491
2492
  /* Finally, update Extended Prefix LSA id SR is UP */
2493
0
  srp->instance = ospf_ext_schedule_prefix_index(
2494
0
    ifp, srp->sid, &srp->prefv4, srp->flags);
2495
0
  if (srp->instance == 0) {
2496
0
    vty_out(vty, "Unable to set index %u for prefix %pFX\n",
2497
0
      index, &p);
2498
0
    return CMD_WARNING;
2499
0
  }
2500
2501
0
  return CMD_SUCCESS;
2502
0
}
2503
2504
DEFUN (no_sr_prefix_sid,
2505
       no_sr_prefix_sid_cmd,
2506
       "no segment-routing prefix A.B.C.D/M [index (0-65535)|no-php-flag|explicit-null]",
2507
       NO_STR
2508
       SR_STR
2509
       "Prefix SID\n"
2510
       "IPv4 Prefix as A.B.C.D/M\n"
2511
       "SID index for this prefix in decimal (0-65535)\n"
2512
       "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2513
       "Don't request Penultimate Hop Popping (PHP)\n"
2514
       "Upstream neighbor must replace prefix-sid with explicit null label\n")
2515
0
{
2516
0
  int idx = 0;
2517
0
  struct prefix p;
2518
0
  struct listnode *node;
2519
0
  struct sr_prefix *srp;
2520
0
  struct interface *ifp;
2521
0
  bool found = false;
2522
0
  int rc;
2523
2524
0
  if (!ospf_sr_enabled(vty))
2525
0
    return CMD_WARNING_CONFIG_FAILED;
2526
2527
0
  if (OspfSR.status != SR_UP)
2528
0
    return CMD_SUCCESS;
2529
2530
  /* Get network prefix */
2531
0
  argv_find(argv, argc, "A.B.C.D/M", &idx);
2532
0
  rc = str2prefix(argv[idx]->arg, &p);
2533
0
  if (!rc) {
2534
0
    vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg);
2535
0
    return CMD_WARNING_CONFIG_FAILED;
2536
0
  }
2537
2538
  /* check that the prefix is already set */
2539
0
  for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp))
2540
0
    if (IPV4_ADDR_SAME(&srp->prefv4.prefix, &p.u.prefix4)
2541
0
        && (srp->prefv4.prefixlen == p.prefixlen)) {
2542
0
      found = true;
2543
0
      break;
2544
0
    }
2545
2546
0
  if (!found) {
2547
0
    vty_out(vty, "Prefix %s is not found. Abort!\n",
2548
0
      argv[idx]->arg);
2549
0
    return CMD_WARNING_CONFIG_FAILED;
2550
0
  }
2551
2552
0
  osr_debug("SR (%s): Remove Prefix %pFX with index %u", __func__,
2553
0
      (struct prefix *)&srp->prefv4, srp->sid);
2554
2555
  /* Get Interface */
2556
0
  ifp = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT);
2557
0
  if (ifp == NULL) {
2558
0
    vty_out(vty, "interface for prefix %s not found.\n",
2559
0
      argv[idx]->arg);
2560
    /* silently remove from list */
2561
0
    listnode_delete(OspfSR.self->ext_prefix, srp);
2562
0
    XFREE(MTYPE_OSPF_SR_PARAMS, srp);
2563
0
    return CMD_SUCCESS;
2564
0
  }
2565
2566
  /* Update Extended Prefix LSA */
2567
0
  if (!ospf_ext_schedule_prefix_index(ifp, 0, NULL, 0)) {
2568
0
    vty_out(vty, "No corresponding loopback interface. Abort!\n");
2569
0
    return CMD_WARNING;
2570
0
  }
2571
2572
  /* Delete NHLFE if NO-PHP is set and EXPLICIT NULL not set */
2573
0
  if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)
2574
0
      && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG))
2575
0
    ospf_zebra_delete_prefix_sid(srp);
2576
2577
  /* OK, all is clean, remove SRP from SRDB */
2578
0
  listnode_delete(OspfSR.self->ext_prefix, srp);
2579
0
  XFREE(MTYPE_OSPF_SR_PARAMS, srp);
2580
2581
0
  return CMD_SUCCESS;
2582
0
}
2583
2584
2585
static char *sr_op2str(char *buf, size_t size, mpls_label_t label_in,
2586
           mpls_label_t label_out)
2587
0
{
2588
0
  if (size < 24)
2589
0
    return NULL;
2590
2591
0
  switch (label_out) {
2592
0
  case MPLS_LABEL_IMPLICIT_NULL:
2593
0
    snprintf(buf, size, "Pop(%u)", label_in);
2594
0
    break;
2595
0
  case MPLS_LABEL_IPV4_EXPLICIT_NULL:
2596
0
    if (label_in == MPLS_LABEL_IPV4_EXPLICIT_NULL)
2597
0
      snprintf(buf, size, "no-op.");
2598
0
    else
2599
0
      snprintf(buf, size, "Swap(%u, null)", label_in);
2600
0
    break;
2601
0
  case MPLS_INVALID_LABEL:
2602
0
    snprintf(buf, size, "no-op.");
2603
0
    break;
2604
0
  default:
2605
0
    snprintf(buf, size, "Swap(%u, %u)", label_in, label_out);
2606
0
    break;
2607
0
  }
2608
0
  return buf;
2609
0
}
2610
2611
static void show_sr_prefix(struct sbuf *sbuf, struct json_object *json,
2612
         struct sr_prefix *srp)
2613
0
{
2614
2615
0
  struct listnode *node;
2616
0
  struct ospf_path *path;
2617
0
  struct interface *itf;
2618
0
  json_object *json_route = NULL, *json_obj;
2619
0
  char pref[19];
2620
0
  char sid[22];
2621
0
  char op[32];
2622
0
  char buf[PREFIX_STRLEN];
2623
0
  int indent = 0;
2624
2625
0
  snprintfrr(pref, 19, "%pFX", (struct prefix *)&srp->prefv4);
2626
0
  snprintf(sid, 22, "SR Pfx (idx %u)", srp->sid);
2627
0
  if (json) {
2628
0
    json_object_string_add(json, "prefix", pref);
2629
0
    json_object_int_add(json, "sid", srp->sid);
2630
0
    json_object_int_add(json, "inputLabel", srp->label_in);
2631
0
  } else {
2632
0
    sbuf_push(sbuf, 0, "%18s  %21s  ", pref, sid);
2633
0
  }
2634
2635
  /* Check if it is a Local Node SID */
2636
0
  if (srp->type == LOCAL_SID) {
2637
0
    itf = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT);
2638
0
    if (json) {
2639
0
      if (!json_route) {
2640
0
        json_route = json_object_new_array();
2641
0
        json_object_object_add(json, "prefixRoute",
2642
0
                   json_route);
2643
0
      }
2644
0
      json_obj = json_object_new_object();
2645
0
      json_object_int_add(json_obj, "outputLabel",
2646
0
              srp->nhlfe.label_out);
2647
0
      json_object_string_add(json_obj, "interface",
2648
0
                 itf ? itf->name : "-");
2649
0
      json_object_string_addf(json_obj, "nexthop", "%pI4",
2650
0
            &srp->nhlfe.nexthop);
2651
0
      json_object_array_add(json_route, json_obj);
2652
0
    } else {
2653
0
      sbuf_push(sbuf, 0, "%20s  %9s  %15s\n",
2654
0
          sr_op2str(op, 32, srp->label_in,
2655
0
              srp->nhlfe.label_out),
2656
0
          itf ? itf->name : "-",
2657
0
          inet_ntop(AF_INET, &srp->nhlfe.nexthop,
2658
0
              buf, sizeof(buf)));
2659
0
    }
2660
0
    return;
2661
0
  }
2662
2663
  /* Check if we have a valid path for this prefix */
2664
0
  if (srp->route == NULL) {
2665
0
    if (!json) {
2666
0
      sbuf_push(sbuf, 0, "\n");
2667
0
    }
2668
0
    return;
2669
0
  }
2670
2671
  /* Process list of OSPF paths */
2672
0
  for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) {
2673
0
    itf = if_lookup_by_index(path->ifindex, VRF_DEFAULT);
2674
0
    if (json) {
2675
0
      if (!json_route) {
2676
0
        json_route = json_object_new_array();
2677
0
        json_object_object_add(json, "prefixRoute",
2678
0
                   json_route);
2679
0
      }
2680
0
      json_obj = json_object_new_object();
2681
0
      json_object_int_add(json_obj, "outputLabel",
2682
0
              path->srni.label_out);
2683
0
      json_object_string_add(json_obj, "interface",
2684
0
                 itf ? itf->name : "-");
2685
0
      json_object_string_addf(json_obj, "nexthop", "%pI4",
2686
0
            &path->nexthop);
2687
0
      json_object_array_add(json_route, json_obj);
2688
0
    } else {
2689
0
      sbuf_push(sbuf, indent, "%20s  %9s  %15s\n",
2690
0
          sr_op2str(op, 32, srp->label_in,
2691
0
              path->srni.label_out),
2692
0
          itf ? itf->name : "-",
2693
0
          inet_ntop(AF_INET, &path->nexthop, buf,
2694
0
              sizeof(buf)));
2695
      /* Offset to align information for ECMP */
2696
0
      indent = 43;
2697
0
    }
2698
0
  }
2699
0
}
2700
2701
static void show_sr_node(struct vty *vty, struct json_object *json,
2702
       struct sr_node *srn)
2703
0
{
2704
2705
0
  struct listnode *node;
2706
0
  struct sr_link *srl;
2707
0
  struct sr_prefix *srp;
2708
0
  struct interface *itf;
2709
0
  struct sbuf sbuf;
2710
0
  char pref[19];
2711
0
  char sid[22];
2712
0
  char op[32];
2713
0
  char buf[PREFIX_STRLEN];
2714
0
  uint32_t upper;
2715
0
  json_object *json_node = NULL, *json_algo, *json_obj;
2716
0
  json_object *json_prefix = NULL, *json_link = NULL;
2717
2718
  /* Sanity Check */
2719
0
  if (srn == NULL)
2720
0
    return;
2721
2722
0
  sbuf_init(&sbuf, NULL, 0);
2723
2724
0
  if (json) {
2725
0
    json_node = json_object_new_object();
2726
0
    json_object_string_addf(json_node, "routerID", "%pI4",
2727
0
          &srn->adv_router);
2728
0
    json_object_int_add(json_node, "srgbSize",
2729
0
            srn->srgb.range_size);
2730
0
    json_object_int_add(json_node, "srgbLabel",
2731
0
            srn->srgb.lower_bound);
2732
0
    json_object_int_add(json_node, "srlbSize",
2733
0
            srn->srlb.range_size);
2734
0
    json_object_int_add(json_node, "srlbLabel",
2735
0
            srn->srlb.lower_bound);
2736
0
    json_algo = json_object_new_array();
2737
0
    json_object_object_add(json_node, "algorithms", json_algo);
2738
0
    for (int i = 0; i < ALGORITHM_COUNT; i++) {
2739
0
      if (srn->algo[i] == SR_ALGORITHM_UNSET)
2740
0
        continue;
2741
0
      json_obj = json_object_new_object();
2742
0
      char tmp[2];
2743
2744
0
      snprintf(tmp, sizeof(tmp), "%u", i);
2745
0
      json_object_string_add(json_obj, tmp,
2746
0
                 srn->algo[i] == SR_ALGORITHM_SPF
2747
0
                   ? "SPF"
2748
0
                   : "S-SPF");
2749
0
      json_object_array_add(json_algo, json_obj);
2750
0
    }
2751
0
    if (srn->msd != 0)
2752
0
      json_object_int_add(json_node, "nodeMsd", srn->msd);
2753
0
  } else {
2754
0
    sbuf_push(&sbuf, 0, "SR-Node: %pI4", &srn->adv_router);
2755
0
    upper = srn->srgb.lower_bound + srn->srgb.range_size - 1;
2756
0
    sbuf_push(&sbuf, 0, "\tSRGB: [%u/%u]",
2757
0
        srn->srgb.lower_bound, upper);
2758
0
    upper = srn->srlb.lower_bound + srn->srlb.range_size - 1;
2759
0
    sbuf_push(&sbuf, 0, "\tSRLB: [%u/%u]",
2760
0
        srn->srlb.lower_bound, upper);
2761
0
    sbuf_push(&sbuf, 0, "\tAlgo.(s): %s",
2762
0
        srn->algo[0] == SR_ALGORITHM_SPF ? "SPF" : "S-SPF");
2763
0
    for (int i = 1; i < ALGORITHM_COUNT; i++) {
2764
0
      if (srn->algo[i] == SR_ALGORITHM_UNSET)
2765
0
        continue;
2766
0
      sbuf_push(&sbuf, 0, "/%s",
2767
0
          srn->algo[i] == SR_ALGORITHM_SPF ? "SPF"
2768
0
                   : "S-SPF");
2769
0
    }
2770
0
    if (srn->msd != 0)
2771
0
      sbuf_push(&sbuf, 0, "\tMSD: %u", srn->msd);
2772
0
  }
2773
2774
0
  if (!json) {
2775
0
    sbuf_push(&sbuf, 0,
2776
0
        "\n\n    Prefix or Link       Node or Adj. SID       Label Operation  Interface          Nexthop\n");
2777
0
    sbuf_push(&sbuf, 0,
2778
0
        "------------------  ---------------------  --------------------  ---------  ---------------\n");
2779
0
  }
2780
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
2781
0
    if (json) {
2782
0
      if (!json_prefix) {
2783
0
        json_prefix = json_object_new_array();
2784
0
        json_object_object_add(json_node,
2785
0
                   "extendedPrefix",
2786
0
                   json_prefix);
2787
0
      }
2788
0
      json_obj = json_object_new_object();
2789
0
      show_sr_prefix(NULL, json_obj, srp);
2790
0
      json_object_array_add(json_prefix, json_obj);
2791
0
    } else {
2792
0
      show_sr_prefix(&sbuf, NULL, srp);
2793
0
    }
2794
0
  }
2795
2796
0
  for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) {
2797
0
    snprintfrr(pref, 19, "%pI4/32", &srl->itf_addr);
2798
0
    snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[0]);
2799
0
    itf = if_lookup_by_index(srl->nhlfe[0].ifindex, VRF_DEFAULT);
2800
0
    if (json) {
2801
0
      if (!json_link) {
2802
0
        json_link = json_object_new_array();
2803
0
        json_object_object_add(
2804
0
          json_node, "extendedLink", json_link);
2805
0
      }
2806
      /* Primary Link */
2807
0
      json_obj = json_object_new_object();
2808
0
      json_object_string_add(json_obj, "prefix", pref);
2809
0
      json_object_int_add(json_obj, "sid", srl->sid[0]);
2810
0
      json_object_int_add(json_obj, "inputLabel",
2811
0
              srl->nhlfe[0].label_in);
2812
0
      json_object_int_add(json_obj, "outputLabel",
2813
0
              srl->nhlfe[0].label_out);
2814
0
      json_object_string_add(json_obj, "interface",
2815
0
                 itf ? itf->name : "-");
2816
0
      json_object_string_addf(json_obj, "nexthop", "%pI4",
2817
0
            &srl->nhlfe[0].nexthop);
2818
0
      json_object_array_add(json_link, json_obj);
2819
      /* Backup Link */
2820
0
      json_obj = json_object_new_object();
2821
0
      snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]);
2822
0
      json_object_string_add(json_obj, "prefix", pref);
2823
0
      json_object_int_add(json_obj, "sid", srl->sid[1]);
2824
0
      json_object_int_add(json_obj, "inputLabel",
2825
0
              srl->nhlfe[1].label_in);
2826
0
      json_object_int_add(json_obj, "outputLabel",
2827
0
              srl->nhlfe[1].label_out);
2828
0
      json_object_string_add(json_obj, "interface",
2829
0
                 itf ? itf->name : "-");
2830
0
      json_object_string_addf(json_obj, "nexthop", "%pI4",
2831
0
            &srl->nhlfe[1].nexthop);
2832
0
      json_object_array_add(json_link, json_obj);
2833
0
    } else {
2834
0
      sbuf_push(&sbuf, 0, "%18s  %21s  %20s  %9s  %15s\n",
2835
0
          pref, sid,
2836
0
          sr_op2str(op, 32, srl->nhlfe[0].label_in,
2837
0
              srl->nhlfe[0].label_out),
2838
0
          itf ? itf->name : "-",
2839
0
          inet_ntop(AF_INET, &srl->nhlfe[0].nexthop,
2840
0
              buf, sizeof(buf)));
2841
0
      snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]);
2842
0
      sbuf_push(&sbuf, 0, "%18s  %21s  %20s  %9s  %15s\n",
2843
0
          pref, sid,
2844
0
          sr_op2str(op, 32, srl->nhlfe[1].label_in,
2845
0
              srl->nhlfe[1].label_out),
2846
0
          itf ? itf->name : "-",
2847
0
          inet_ntop(AF_INET, &srl->nhlfe[1].nexthop,
2848
0
            buf, sizeof(buf)));
2849
0
    }
2850
0
  }
2851
0
  if (json)
2852
0
    json_object_array_add(json, json_node);
2853
0
  else
2854
0
    vty_out(vty, "%s\n", sbuf_buf(&sbuf));
2855
2856
0
  sbuf_free(&sbuf);
2857
0
}
2858
2859
static void show_vty_srdb(struct hash_bucket *bucket, void *args)
2860
0
{
2861
0
  struct vty *vty = (struct vty *)args;
2862
0
  struct sr_node *srn = (struct sr_node *)bucket->data;
2863
2864
0
  show_sr_node(vty, NULL, srn);
2865
0
}
2866
2867
static void show_json_srdb(struct hash_bucket *bucket, void *args)
2868
0
{
2869
0
  struct json_object *json = (struct json_object *)args;
2870
0
  struct sr_node *srn = (struct sr_node *)bucket->data;
2871
2872
0
  show_sr_node(NULL, json, srn);
2873
0
}
2874
2875
DEFUN (show_ip_opsf_srdb,
2876
       show_ip_ospf_srdb_cmd,
2877
       "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]",
2878
       SHOW_STR
2879
       IP_STR
2880
       OSPF_STR
2881
       "Database summary\n"
2882
       "Show Segment Routing Data Base\n"
2883
       "Advertising SR node\n"
2884
       "Advertising SR node ID (as an IP address)\n"
2885
       "Self-originated SR node\n"
2886
       JSON_STR)
2887
0
{
2888
0
  int idx = 0;
2889
0
  struct in_addr rid;
2890
0
  struct sr_node *srn;
2891
0
  bool uj = use_json(argc, argv);
2892
0
  json_object *json = NULL, *json_node_array = NULL;
2893
2894
0
  if (OspfSR.status == SR_OFF) {
2895
0
    vty_out(vty, "Segment Routing is disabled on this router\n");
2896
0
    return CMD_WARNING;
2897
0
  }
2898
2899
0
  if (uj) {
2900
0
    json = json_object_new_object();
2901
0
    json_node_array = json_object_new_array();
2902
0
    json_object_string_addf(json, "srdbID", "%pI4",
2903
0
          &OspfSR.self->adv_router);
2904
0
    json_object_object_add(json, "srNodes", json_node_array);
2905
0
  } else {
2906
0
    vty_out(vty,
2907
0
      "\n\t\tOSPF Segment Routing database for ID %pI4\n\n",
2908
0
      &OspfSR.self->adv_router);
2909
0
  }
2910
2911
0
  if (argv_find(argv, argc, "self-originate", &idx)) {
2912
0
    srn = OspfSR.self;
2913
0
    show_sr_node(vty, json_node_array, srn);
2914
0
    if (uj)
2915
0
      vty_json(vty, json);
2916
0
    return CMD_SUCCESS;
2917
0
  }
2918
2919
0
  if (argv_find(argv, argc, "A.B.C.D", &idx)) {
2920
0
    if (!inet_aton(argv[idx]->arg, &rid)) {
2921
0
      vty_out(vty, "Specified Router ID %s is invalid\n",
2922
0
        argv[idx]->arg);
2923
0
      return CMD_WARNING_CONFIG_FAILED;
2924
0
    }
2925
    /* Get the SR Node from the SRDB */
2926
0
    srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
2927
0
                (void *)&rid);
2928
0
    show_sr_node(vty, json_node_array, srn);
2929
0
    if (uj)
2930
0
      vty_json(vty, json);
2931
0
    return CMD_SUCCESS;
2932
0
  }
2933
2934
  /* No parameters have been provided, Iterate through all the SRDB */
2935
0
  if (uj) {
2936
0
    hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
2937
0
               void *))show_json_srdb,
2938
0
           (void *)json_node_array);
2939
0
    vty_json(vty, json);
2940
0
  } else {
2941
0
    hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *,
2942
0
               void *))show_vty_srdb,
2943
0
           (void *)vty);
2944
0
  }
2945
0
  return CMD_SUCCESS;
2946
0
}
2947
2948
/* Install new CLI commands */
2949
void ospf_sr_register_vty(void)
2950
1
{
2951
1
  install_element(VIEW_NODE, &show_ip_ospf_srdb_cmd);
2952
2953
1
  install_element(OSPF_NODE, &ospf_sr_enable_cmd);
2954
1
  install_element(OSPF_NODE, &no_ospf_sr_enable_cmd);
2955
1
  install_element(OSPF_NODE, &sr_global_label_range_cmd);
2956
1
  install_element(OSPF_NODE, &no_sr_global_label_range_cmd);
2957
1
  install_element(OSPF_NODE, &sr_node_msd_cmd);
2958
1
  install_element(OSPF_NODE, &no_sr_node_msd_cmd);
2959
1
  install_element(OSPF_NODE, &sr_prefix_sid_cmd);
2960
1
  install_element(OSPF_NODE, &no_sr_prefix_sid_cmd);
2961
1
}