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