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