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