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