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