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