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