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