]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_sr.c
Merge pull request #3204 from ton31337/fix/make_vrf_import_default_selectable
[mirror_frr.git] / ospfd / ospf_sr.c
1 /*
2 * This is an implementation of Segment Routing
3 * as per draft draft-ietf-ospf-segment-routing-extensions-24
4 *
5 * Module name: Segment Routing
6 *
7 * Author: Olivier Dugeon <olivier.dugeon@orange.com>
8 * Author: Anselme Sawadogo <anselmesawadogo@gmail.com>
9 *
10 * Copyright (C) 2016 - 2018 Orange Labs http://www.orange.com
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <math.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <zebra.h>
35
36 #include "command.h"
37 #include "hash.h"
38 #include "if.h"
39 #include "if.h"
40 #include "jhash.h"
41 #include "libospf.h" /* for ospf interface types */
42 #include "linklist.h"
43 #include "log.h"
44 #include "memory.h"
45 #include "monotime.h"
46 #include "network.h"
47 #include "prefix.h"
48 #include "sockunion.h" /* for inet_aton() */
49 #include "stream.h"
50 #include "table.h"
51 #include "thread.h"
52 #include "vty.h"
53 #include "zclient.h"
54 #include <lib/json.h>
55 #include "ospf_errors.h"
56
57 #include "ospfd/ospfd.h"
58 #include "ospfd/ospf_interface.h"
59 #include "ospfd/ospf_ism.h"
60 #include "ospfd/ospf_asbr.h"
61 #include "ospfd/ospf_lsa.h"
62 #include "ospfd/ospf_lsdb.h"
63 #include "ospfd/ospf_neighbor.h"
64 #include "ospfd/ospf_nsm.h"
65 #include "ospfd/ospf_flood.h"
66 #include "ospfd/ospf_packet.h"
67 #include "ospfd/ospf_spf.h"
68 #include "ospfd/ospf_dump.h"
69 #include "ospfd/ospf_route.h"
70 #include "ospfd/ospf_ase.h"
71 #include "ospfd/ospf_sr.h"
72 #include "ospfd/ospf_ri.h"
73 #include "ospfd/ospf_ext.h"
74 #include "ospfd/ospf_zebra.h"
75
76 /*
77 * Global variable to manage Segment Routing on this node.
78 * Note that all parameter values are stored in network byte order.
79 */
80 static struct ospf_sr_db OspfSR;
81 static void ospf_sr_register_vty(void);
82 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe);
83
84 /*
85 * Segment Routing Data Base functions
86 */
87
88 /* Hash function for Segment Routing entry */
89 static unsigned int sr_hash(void *p)
90 {
91 const struct in_addr *rid = p;
92
93 return jhash_1word(rid->s_addr, 0);
94 }
95
96 /* Compare 2 Router ID hash entries based on SR Node */
97 static bool sr_cmp(const void *p1, const void *p2)
98 {
99 const struct sr_node *srn = p1;
100 const struct in_addr *rid = p2;
101
102 return IPV4_ADDR_SAME(&srn->adv_router, rid);
103 }
104
105 /* Functions to remove an SR Link */
106 static void del_sr_link(void *val)
107 {
108 struct sr_link *srl = (struct sr_link *)val;
109
110 del_sid_nhlfe(srl->nhlfe[0]);
111 del_sid_nhlfe(srl->nhlfe[1]);
112 XFREE(MTYPE_OSPF_SR_PARAMS, val);
113 }
114
115 /* Functions to remove an SR Prefix */
116 static void del_sr_pref(void *val)
117 {
118 struct sr_prefix *srp = (struct sr_prefix *)val;
119
120 del_sid_nhlfe(srp->nhlfe);
121 XFREE(MTYPE_OSPF_SR_PARAMS, val);
122 }
123
124 /* Allocate new Segment Routine node */
125 static struct sr_node *sr_node_new(struct in_addr *rid)
126 {
127
128 if (rid == NULL)
129 return NULL;
130
131 struct sr_node *new;
132
133 /* Allocate Segment Routing node memory */
134 new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_node));
135
136 /* Default Algorithm, SRGB and MSD */
137 for (int i = 0; i < ALGORITHM_COUNT; i++)
138 new->algo[i] = SR_ALGORITHM_UNSET;
139
140 new->srgb.range_size = 0;
141 new->srgb.lower_bound = 0;
142 new->msd = 0;
143
144 /* Create Link, Prefix and Range TLVs list */
145 new->ext_link = list_new();
146 new->ext_prefix = list_new();
147 new->ext_link->del = del_sr_link;
148 new->ext_prefix->del = del_sr_pref;
149
150 IPV4_ADDR_COPY(&new->adv_router, rid);
151 new->neighbor = NULL;
152 new->instance = 0;
153
154 if (IS_DEBUG_OSPF_SR)
155 zlog_debug(" |- Created new SR node for %s",
156 inet_ntoa(new->adv_router));
157 return new;
158 }
159
160 /* Delete Segment Routing node */
161 static void sr_node_del(struct sr_node *srn)
162 {
163 /* Sanity Check */
164 if (srn == NULL)
165 return;
166
167 /* Clean Extended Link */
168 list_delete(&srn->ext_link);
169
170 /* Clean Prefix List */
171 list_delete(&srn->ext_prefix);
172
173 XFREE(MTYPE_OSPF_SR_PARAMS, srn);
174 }
175
176 /* Get SR Node for a given nexthop */
177 static struct sr_node *get_sr_node_by_nexthop(struct ospf *ospf,
178 struct in_addr nexthop)
179 {
180 struct ospf_interface *oi = NULL;
181 struct ospf_neighbor *nbr = NULL;
182 struct listnode *node;
183 struct route_node *rn;
184 struct sr_node *srn;
185 bool found;
186
187 /* Sanity check */
188 if (OspfSR.neighbors == NULL)
189 return NULL;
190
191 if (IS_DEBUG_OSPF_SR)
192 zlog_debug(" |- Search SR-Node for nexthop %s",
193 inet_ntoa(nexthop));
194
195 /* First, search neighbor Router ID for this nexthop */
196 found = false;
197 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
198 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
199 nbr = rn->info;
200 if ((nbr) && (IPV4_ADDR_SAME(&nexthop, &nbr->src))) {
201 found = true;
202 break;
203 }
204 }
205 if (found)
206 break;
207 }
208
209 if (!found)
210 return NULL;
211
212 if (IS_DEBUG_OSPF_SR)
213 zlog_debug(" |- Found nexthop Router ID %s",
214 inet_ntoa(nbr->router_id));
215 /* Then, search SR Node */
216 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, &nbr->router_id);
217
218 return srn;
219 }
220
221 /*
222 * Segment Routing Initialization functions
223 */
224
225 /* Segment Routing starter function */
226 static int ospf_sr_start(struct ospf *ospf)
227 {
228 struct route_node *rn;
229 struct ospf_lsa *lsa;
230 struct sr_node *srn;
231 int rc = 0;
232
233 if (IS_DEBUG_OSPF_SR)
234 zlog_debug("SR (%s): Start Segment Routing", __func__);
235
236 /* Initialize self SR Node */
237 srn = hash_get(OspfSR.neighbors, (void *)&(ospf->router_id),
238 (void *)sr_node_new);
239
240 /* Complete & Store self SR Node */
241 srn->srgb.range_size = OspfSR.srgb.range_size;
242 srn->srgb.lower_bound = OspfSR.srgb.lower_bound;
243 srn->algo[0] = OspfSR.algo[0];
244 srn->msd = OspfSR.msd;
245 OspfSR.self = srn;
246
247 if (IS_DEBUG_OSPF_EVENT)
248 zlog_debug("SR (%s): Update SR-DB from LSDB", __func__);
249
250 /* Start by looking to Router Info & Extended LSA in lsdb */
251 if ((ospf != NULL) && (ospf->backbone != NULL)) {
252 LSDB_LOOP (OPAQUE_AREA_LSDB(ospf->backbone), rn, lsa) {
253 if (IS_LSA_MAXAGE(lsa) || IS_LSA_SELF(lsa))
254 continue;
255 int lsa_id =
256 GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr));
257 switch (lsa_id) {
258 case OPAQUE_TYPE_ROUTER_INFORMATION_LSA:
259 ospf_sr_ri_lsa_update(lsa);
260 break;
261 case OPAQUE_TYPE_EXTENDED_PREFIX_LSA:
262 ospf_sr_ext_prefix_lsa_update(lsa);
263 break;
264 case OPAQUE_TYPE_EXTENDED_LINK_LSA:
265 ospf_sr_ext_link_lsa_update(lsa);
266 break;
267 default:
268 break;
269 }
270 }
271 }
272
273 rc = 1;
274 return rc;
275 }
276
277 /* Stop Segment Routing */
278 static void ospf_sr_stop(void)
279 {
280
281 if (IS_DEBUG_OSPF_SR)
282 zlog_debug("SR (%s): Stop Segment Routing", __func__);
283
284 /*
285 * Remove all SR Nodes from the Hash table. Prefix and Link SID will
286 * be remove though list_delete() call. See sr_node_del()
287 */
288 hash_clean(OspfSR.neighbors, (void *)sr_node_del);
289 }
290
291 /*
292 * Segment Routing initialize function
293 *
294 * @param - nothing
295 *
296 * @return 0 if OK, -1 otherwise
297 */
298 int ospf_sr_init(void)
299 {
300 int rc = -1;
301
302 if (IS_DEBUG_OSPF_SR)
303 zlog_info("SR (%s): Initialize SR Data Base", __func__);
304
305 memset(&OspfSR, 0, sizeof(struct ospf_sr_db));
306 OspfSR.enabled = false;
307 /* Only AREA flooding is supported in this release */
308 OspfSR.scope = OSPF_OPAQUE_AREA_LSA;
309
310 /* Initialize SRGB, Algorithms and MSD TLVs */
311 /* Only Algorithm SPF is supported */
312 OspfSR.algo[0] = SR_ALGORITHM_SPF;
313 for (int i = 1; i < ALGORITHM_COUNT; i++)
314 OspfSR.algo[i] = SR_ALGORITHM_UNSET;
315
316 OspfSR.srgb.range_size = MPLS_DEFAULT_MAX_SRGB_SIZE;
317 OspfSR.srgb.lower_bound = MPLS_DEFAULT_MIN_SRGB_LABEL;
318 OspfSR.msd = 0;
319
320 /* Initialize Hash table for neighbor SR nodes */
321 OspfSR.neighbors = hash_create(sr_hash, sr_cmp, "OSPF_SR");
322 if (OspfSR.neighbors == NULL)
323 return rc;
324
325 /* Initialize Route Table for prefix */
326 OspfSR.prefix = route_table_init();
327 if (OspfSR.prefix == NULL)
328 return rc;
329
330 /* Register Segment Routing VTY command */
331 ospf_sr_register_vty();
332
333 rc = 0;
334 return rc;
335 }
336
337 /*
338 * Segment Routing termination function
339 *
340 * @param - nothing
341 * @return - nothing
342 */
343 void ospf_sr_term(void)
344 {
345
346 /* Stop Segment Routing */
347 ospf_sr_stop();
348
349 /* Clear SR Node Table */
350 if (OspfSR.neighbors)
351 hash_free(OspfSR.neighbors);
352
353 /* Clear Prefix Table */
354 if (OspfSR.prefix)
355 route_table_finish(OspfSR.prefix);
356
357 OspfSR.enabled = false;
358 OspfSR.self = NULL;
359 }
360
361 /*
362 * Segment Routing finish function
363 *
364 * @param - nothing
365 * @return - nothing
366 */
367 void ospf_sr_finish(void)
368 {
369 /* Stop Segment Routing */
370 ospf_sr_stop();
371
372 OspfSR.enabled = false;
373 }
374
375 /*
376 * Following functions are used to manipulate the
377 * Next Hop Label Forwarding entry (NHLFE)
378 */
379
380 /* Compute label from index */
381 static mpls_label_t index2label(uint32_t index, struct sr_srgb srgb)
382 {
383 mpls_label_t label;
384
385 label = srgb.lower_bound + index;
386 if (label > (srgb.lower_bound + srgb.range_size))
387 return MPLS_INVALID_LABEL;
388 else
389 return label;
390 }
391
392 /* Get neighbor full structure from address */
393 static struct ospf_neighbor *get_neighbor_by_addr(struct ospf *top,
394 struct in_addr addr)
395 {
396 struct ospf_neighbor *nbr;
397 struct ospf_interface *oi;
398 struct listnode *node;
399 struct route_node *rn;
400
401 /* Sanity Check */
402 if (top == NULL)
403 return NULL;
404
405 for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi))
406 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
407 nbr = rn->info;
408 if (nbr)
409 if (IPV4_ADDR_SAME(&nbr->address.u.prefix4,
410 &addr)
411 || IPV4_ADDR_SAME(&nbr->router_id, &addr)) {
412 route_unlock_node(rn);
413 return nbr;
414 }
415 }
416 return NULL;
417 }
418
419 /* Get OSPF Path from address */
420 static struct ospf_path *get_nexthop_by_addr(struct ospf *top,
421 struct prefix_ipv4 p)
422 {
423 struct ospf_route * or ;
424 struct ospf_path *path;
425 struct listnode *node;
426 struct route_node *rn;
427
428 /* Sanity Check */
429 if (top == NULL)
430 return NULL;
431
432 if (IS_DEBUG_OSPF_SR)
433 zlog_debug(" |- Search Nexthop for prefix %s/%u",
434 inet_ntoa(p.prefix), p.prefixlen);
435
436 rn = route_node_lookup(top->new_table, (struct prefix *)&p);
437
438 /*
439 * Check if we found an OSPF route. May be NULL if SPF has not
440 * yet populate routing table for this prefix.
441 */
442 if (rn == NULL)
443 return NULL;
444
445 route_unlock_node(rn);
446 or = rn->info;
447 if (or == NULL)
448 return NULL;
449
450 /* Then search path from this route */
451 for (ALL_LIST_ELEMENTS_RO(or->paths, node, path))
452 if (path->nexthop.s_addr != INADDR_ANY || path->ifindex != 0)
453 return path;
454
455 return NULL;
456 }
457
458 /* Compute NHLFE entry for Extended Link */
459 static int compute_link_nhlfe(struct sr_link *srl)
460 {
461 struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
462 struct ospf_neighbor *nh;
463 int rc = 0;
464
465 if (IS_DEBUG_OSPF_SR)
466 zlog_debug(" |- Compute NHLFE for link %s/%u",
467 inet_ntoa(srl->nhlfe[0].prefv4.prefix),
468 srl->nhlfe[0].prefv4.prefixlen);
469
470 /* First determine the OSPF Neighbor */
471 nh = get_neighbor_by_addr(top, srl->nhlfe[0].nexthop);
472
473 /* Neighbor could be not found when OSPF Adjacency just fire up
474 * because SPF don't yet populate routing table. This NHLFE will
475 * be fixed later when SR SPF schedule will be called.
476 */
477 if (nh == NULL)
478 return rc;
479
480 if (IS_DEBUG_OSPF_SR)
481 zlog_debug(" |- Found nexthop NHLFE %s",
482 inet_ntoa(nh->router_id));
483
484 /* Set ifindex for this neighbor */
485 srl->nhlfe[0].ifindex = nh->oi->ifp->ifindex;
486 srl->nhlfe[1].ifindex = nh->oi->ifp->ifindex;
487
488 /* Update neighbor address for LAN_ADJ_SID */
489 if (srl->type == LAN_ADJ_SID) {
490 IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &nh->src);
491 IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &nh->src);
492 }
493
494 /* Set Input & Output Label */
495 if (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
496 srl->nhlfe[0].label_in = srl->sid[0];
497 else
498 srl->nhlfe[0].label_in =
499 index2label(srl->sid[0], srl->srn->srgb);
500 if (CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG))
501 srl->nhlfe[1].label_in = srl->sid[1];
502 else
503 srl->nhlfe[1].label_in =
504 index2label(srl->sid[1], srl->srn->srgb);
505
506 srl->nhlfe[0].label_out = MPLS_LABEL_IMPLICIT_NULL;
507 srl->nhlfe[1].label_out = MPLS_LABEL_IMPLICIT_NULL;
508
509 rc = 1;
510 return rc;
511 }
512
513 /*
514 * Compute NHLFE entry for Extended Prefix
515 *
516 * @param srp - Segment Routing Prefix
517 *
518 * @return -1 if next hop is not found, 0 if nexthop has not changed
519 * and 1 if success
520 */
521 static int compute_prefix_nhlfe(struct sr_prefix *srp)
522 {
523 struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT);
524 struct ospf_path *nh = NULL;
525 struct sr_node *srnext;
526 int rc = -1;
527
528 if (IS_DEBUG_OSPF_SR)
529 zlog_debug(" |- Compute NHLFE for prefix %s/%u",
530 inet_ntoa(srp->nhlfe.prefv4.prefix),
531 srp->nhlfe.prefv4.prefixlen);
532
533 /* First determine the nexthop */
534 nh = get_nexthop_by_addr(top, srp->nhlfe.prefv4);
535
536 /* Nexthop could be not found when OSPF Adjacency just fire up
537 * because SPF don't yet populate routing table. This NHLFE will
538 * be fixed later when SR SPF schedule will be called.
539 */
540 if (nh == NULL)
541 return rc;
542
543 /* Check if NextHop has changed when call after running a new SPF */
544 if (IPV4_ADDR_SAME(&nh->nexthop, &srp->nhlfe.nexthop)
545 && (nh->ifindex == srp->nhlfe.ifindex))
546 return 0;
547
548 if (IS_DEBUG_OSPF_SR)
549 zlog_debug(" |- Found new next hop for this NHLFE: %s",
550 inet_ntoa(nh->nexthop));
551
552 /*
553 * Get SR-Node for this nexthop. Could be not yet available
554 * as Extende Link / Prefix and Router Information are flooded
555 * after LSA Type 1 & 2 which populate the OSPF Route Table
556 */
557 srnext = get_sr_node_by_nexthop(top, nh->nexthop);
558 if (srnext == NULL)
559 return rc;
560
561 /* And store this information for later update if SR Node is found */
562 srnext->neighbor = OspfSR.self;
563 if (IPV4_ADDR_SAME(&srnext->adv_router, &srp->adv_router))
564 srp->nexthop = NULL;
565 else
566 srp->nexthop = srnext;
567
568 /*
569 * SR Node could be known, but SRGB could be not initialize
570 * This is due to the fact that Extended Link / Prefix could
571 * be received before corresponding Router Information LSA
572 */
573 if ((srnext == NULL) || (srnext->srgb.lower_bound == 0)
574 || (srnext->srgb.range_size == 0))
575 return rc;
576
577 if (IS_DEBUG_OSPF_SR)
578 zlog_debug(" |- Found SRGB %u/%u for next hop SR-Node %s",
579 srnext->srgb.range_size, srnext->srgb.lower_bound,
580 inet_ntoa(srnext->adv_router));
581
582 /* Set ip addr & ifindex for this neighbor */
583 IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &nh->nexthop);
584 srp->nhlfe.ifindex = nh->ifindex;
585
586 /* Compute Input Label with self SRGB */
587 srp->nhlfe.label_in = index2label(srp->sid, OspfSR.srgb);
588 /*
589 * and Output Label with Next hop SR Node SRGB or Implicit Null label
590 * if next hop is the destination and request PHP
591 */
592 if ((srp->nexthop == NULL)
593 && (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)))
594 srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
595 else if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
596 srp->nhlfe.label_out = srp->sid;
597 else
598 srp->nhlfe.label_out = index2label(srp->sid, srnext->srgb);
599
600 if (IS_DEBUG_OSPF_SR)
601 zlog_debug(" |- Computed new labels in: %u out: %u",
602 srp->nhlfe.label_in, srp->nhlfe.label_out);
603
604 rc = 1;
605 return rc;
606 }
607
608 /* Send MPLS Label entry to Zebra for installation or deletion */
609 static int ospf_zebra_send_mpls_labels(int cmd, struct sr_nhlfe nhlfe)
610 {
611 struct stream *s;
612
613 /* Reset stream. */
614 s = zclient->obuf;
615 stream_reset(s);
616
617 zclient_create_header(s, cmd, VRF_DEFAULT);
618 stream_putc(s, ZEBRA_LSP_SR);
619 /* OSPF Segment Routing currently support only IPv4 */
620 stream_putl(s, nhlfe.prefv4.family);
621 stream_put_in_addr(s, &nhlfe.prefv4.prefix);
622 stream_putc(s, nhlfe.prefv4.prefixlen);
623 stream_put_in_addr(s, &nhlfe.nexthop);
624 stream_putl(s, nhlfe.ifindex);
625 stream_putc(s, OSPF_SR_PRIORITY_DEFAULT);
626 stream_putl(s, nhlfe.label_in);
627 stream_putl(s, nhlfe.label_out);
628
629 /* Put length at the first point of the stream. */
630 stream_putw_at(s, 0, stream_get_endp(s));
631
632 if (IS_DEBUG_OSPF_SR)
633 zlog_debug(" |- %s LSP %u/%u for %s/%u via %u",
634 cmd == ZEBRA_MPLS_LABELS_ADD ? "Add" : "Delete",
635 nhlfe.label_in, nhlfe.label_out,
636 inet_ntoa(nhlfe.prefv4.prefix),
637 nhlfe.prefv4.prefixlen, nhlfe.ifindex);
638
639 return zclient_send_message(zclient);
640 }
641
642 /* Request zebra to install/remove FEC in FIB */
643 static int ospf_zebra_send_mpls_ftn(int cmd, struct sr_nhlfe nhlfe)
644 {
645 struct zapi_route api;
646 struct zapi_nexthop *api_nh;
647
648 /* Support only IPv4 */
649 if (nhlfe.prefv4.family != AF_INET)
650 return -1;
651
652 memset(&api, 0, sizeof(api));
653 api.vrf_id = VRF_DEFAULT;
654 api.type = ZEBRA_ROUTE_OSPF;
655 api.safi = SAFI_UNICAST;
656 memcpy(&api.prefix, &nhlfe.prefv4, sizeof(struct prefix_ipv4));
657
658 if (cmd == ZEBRA_ROUTE_ADD) {
659 /* Metric value. */
660 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
661 api.metric = OSPF_SR_DEFAULT_METRIC;
662 /* Nexthop */
663 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
664 api_nh = &api.nexthops[0];
665 IPV4_ADDR_COPY(&api_nh->gate.ipv4, &nhlfe.nexthop);
666 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
667 api_nh->ifindex = nhlfe.ifindex;
668 /* MPLS labels */
669 SET_FLAG(api.message, ZAPI_MESSAGE_LABEL);
670 api_nh->labels[0] = nhlfe.label_out;
671 api_nh->label_num = 1;
672 api_nh->vrf_id = VRF_DEFAULT;
673 api.nexthop_num = 1;
674 }
675
676 if (IS_DEBUG_OSPF_SR)
677 zlog_debug(" |- %s FEC %u for %s/%u via %u",
678 cmd == ZEBRA_ROUTE_ADD ? "Add" : "Delete",
679 nhlfe.label_out, inet_ntoa(nhlfe.prefv4.prefix),
680 nhlfe.prefv4.prefixlen, nhlfe.ifindex);
681
682 return zclient_route_send(cmd, zclient, &api);
683 }
684
685 /* Add new NHLFE entry for SID */
686 static inline void add_sid_nhlfe(struct sr_nhlfe nhlfe)
687 {
688 if ((nhlfe.label_in != 0) && (nhlfe.label_out != 0)) {
689 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, nhlfe);
690 if (nhlfe.label_out != MPLS_LABEL_IMPLICIT_NULL)
691 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_ADD, nhlfe);
692 }
693 }
694
695 /* Remove NHLFE entry for SID */
696 static inline void del_sid_nhlfe(struct sr_nhlfe nhlfe)
697 {
698 if ((nhlfe.label_in != 0) && (nhlfe.label_out != 0)) {
699 ospf_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, nhlfe);
700 if (nhlfe.label_out != MPLS_LABEL_IMPLICIT_NULL)
701 ospf_zebra_send_mpls_ftn(ZEBRA_ROUTE_DELETE, nhlfe);
702 }
703 }
704
705 /* Update NHLFE entry for SID */
706 static inline void update_sid_nhlfe(struct sr_nhlfe n1, struct sr_nhlfe n2)
707 {
708
709 del_sid_nhlfe(n1);
710 add_sid_nhlfe(n2);
711 }
712
713 /*
714 * Functions to parse and get Extended Link / Prefix
715 * TLVs and SubTLVs
716 */
717
718 /* Extended Link SubTLVs Getter */
719 static struct sr_link *get_ext_link_sid(struct tlv_header *tlvh)
720 {
721
722 struct sr_link *srl;
723 struct ext_tlv_link *link = (struct ext_tlv_link *)tlvh;
724 struct ext_subtlv_adj_sid *adj_sid;
725 struct ext_subtlv_lan_adj_sid *lan_sid;
726 struct ext_subtlv_rmt_itf_addr *rmt_itf;
727
728 struct tlv_header *sub_tlvh;
729 uint16_t length = 0, sum = 0, i = 0;
730
731 srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link));
732
733 /* Initialize TLV browsing */
734 length = ntohs(tlvh->length) - EXT_TLV_LINK_SIZE;
735 sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
736 + EXT_TLV_LINK_SIZE);
737 for (; sum < length; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
738 switch (ntohs(sub_tlvh->type)) {
739 case EXT_SUBTLV_ADJ_SID:
740 adj_sid = (struct ext_subtlv_adj_sid *)sub_tlvh;
741 srl->type = ADJ_SID;
742 i = CHECK_FLAG(adj_sid->flags,
743 EXT_SUBTLV_LINK_ADJ_SID_BFLG)
744 ? 1
745 : 0;
746 srl->flags[i] = adj_sid->flags;
747 if (CHECK_FLAG(adj_sid->flags,
748 EXT_SUBTLV_LINK_ADJ_SID_VFLG))
749 srl->sid[i] = GET_LABEL(ntohl(adj_sid->value));
750 else
751 srl->sid[i] = ntohl(adj_sid->value);
752 IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop, &link->link_id);
753 break;
754 case EXT_SUBTLV_LAN_ADJ_SID:
755 lan_sid = (struct ext_subtlv_lan_adj_sid *)sub_tlvh;
756 srl->type = LAN_ADJ_SID;
757 i = CHECK_FLAG(lan_sid->flags,
758 EXT_SUBTLV_LINK_ADJ_SID_BFLG)
759 ? 1
760 : 0;
761 srl->flags[i] = lan_sid->flags;
762 if (CHECK_FLAG(lan_sid->flags,
763 EXT_SUBTLV_LINK_ADJ_SID_VFLG))
764 srl->sid[i] = GET_LABEL(ntohl(lan_sid->value));
765 else
766 srl->sid[i] = ntohl(lan_sid->value);
767 IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop,
768 &lan_sid->neighbor_id);
769 break;
770 case EXT_SUBTLV_RMT_ITF_ADDR:
771 rmt_itf = (struct ext_subtlv_rmt_itf_addr *)sub_tlvh;
772 IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &rmt_itf->value);
773 IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &rmt_itf->value);
774 break;
775 default:
776 break;
777 }
778 sum += TLV_SIZE(sub_tlvh);
779 }
780
781 IPV4_ADDR_COPY(&srl->nhlfe[0].prefv4.prefix, &link->link_data);
782 srl->nhlfe[0].prefv4.prefixlen = IPV4_MAX_PREFIXLEN;
783 srl->nhlfe[0].prefv4.family = AF_INET;
784 apply_mask_ipv4(&srl->nhlfe[0].prefv4);
785 IPV4_ADDR_COPY(&srl->nhlfe[1].prefv4.prefix, &link->link_data);
786 srl->nhlfe[1].prefv4.prefixlen = IPV4_MAX_PREFIXLEN;
787 srl->nhlfe[1].prefv4.family = AF_INET;
788 apply_mask_ipv4(&srl->nhlfe[1].prefv4);
789
790 if (IS_DEBUG_OSPF_SR) {
791 zlog_debug(" |- Found primary Adj/Lan Sid %u for %s/%u",
792 srl->sid[0], inet_ntoa(srl->nhlfe[0].prefv4.prefix),
793 srl->nhlfe[0].prefv4.prefixlen);
794 zlog_debug(" |- Found backup Adj/Lan Sid %u for %s/%u",
795 srl->sid[1], inet_ntoa(srl->nhlfe[1].prefv4.prefix),
796 srl->nhlfe[1].prefv4.prefixlen);
797 }
798
799 return srl;
800 }
801
802 /* Extended Prefix SubTLVs Getter */
803 static struct sr_prefix *get_ext_prefix_sid(struct tlv_header *tlvh)
804 {
805
806 struct sr_prefix *srp;
807 struct ext_tlv_prefix *pref = (struct ext_tlv_prefix *)tlvh;
808 struct ext_subtlv_prefix_sid *psid;
809
810 struct tlv_header *sub_tlvh;
811 uint16_t length = 0, sum = 0;
812
813 srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
814
815 /* Initialize TLV browsing */
816 length = ntohs(tlvh->length) - EXT_TLV_PREFIX_SIZE;
817 sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE
818 + EXT_TLV_PREFIX_SIZE);
819 for (; sum < length; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) {
820 switch (ntohs(sub_tlvh->type)) {
821 case EXT_SUBTLV_PREFIX_SID:
822 psid = (struct ext_subtlv_prefix_sid *)sub_tlvh;
823 if (psid->algorithm != SR_ALGORITHM_SPF) {
824 flog_err(EC_OSPF_INVALID_ALGORITHM,
825 "SR (%s): Unsupported Algorithm",
826 __func__);
827 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
828 return NULL;
829 }
830 srp->type = PREF_SID;
831 srp->flags = psid->flags;
832 if (CHECK_FLAG(psid->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
833 srp->sid = GET_LABEL(ntohl(psid->value));
834 else
835 srp->sid = ntohl(psid->value);
836 IPV4_ADDR_COPY(&srp->nhlfe.prefv4.prefix,
837 &pref->address);
838 srp->nhlfe.prefv4.prefixlen = pref->pref_length;
839 srp->nhlfe.prefv4.family = AF_INET;
840 apply_mask_ipv4(&srp->nhlfe.prefv4);
841 break;
842 default:
843 break;
844 }
845 sum += TLV_SIZE(sub_tlvh);
846 }
847
848 if (IS_DEBUG_OSPF_SR)
849 zlog_debug(" |- Found SID %u for prefix %s/%u", srp->sid,
850 inet_ntoa(srp->nhlfe.prefv4.prefix),
851 srp->nhlfe.prefv4.prefixlen);
852 return srp;
853 }
854
855 /*
856 * Functions to manipulate Segment Routing Link & Prefix structures
857 */
858
859 /* Compare two Segment Link: return 0 if equal, 1 otherwise */
860 static inline int sr_link_cmp(struct sr_link *srl1, struct sr_link *srl2)
861 {
862 if ((srl1->sid[0] == srl2->sid[0]) && (srl1->sid[1] == srl2->sid[1])
863 && (srl1->type == srl2->type) && (srl1->flags[0] == srl2->flags[0])
864 && (srl1->flags[1] == srl2->flags[1]))
865 return 0;
866 else
867 return 1;
868 }
869
870 /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */
871 static inline int sr_prefix_cmp(struct sr_prefix *srp1, struct sr_prefix *srp2)
872 {
873 if ((srp1->sid == srp2->sid) && (srp1->flags == srp2->flags))
874 return 0;
875 else
876 return 1;
877 }
878
879 /* Update Segment Link of given Segment Routing Node */
880 static void update_ext_link_sid(struct sr_node *srn, struct sr_link *srl,
881 uint8_t lsa_flags)
882 {
883 struct listnode *node;
884 struct sr_link *lk;
885 bool found = false;
886
887 /* Sanity check */
888 if ((srn == NULL) || (srl == NULL))
889 return;
890
891 if (IS_DEBUG_OSPF_SR)
892 zlog_debug(" |- Process Extended Link Adj/Lan-SID");
893
894 /* Process only Local Adj/Lan_Adj SID coming from LSA SELF */
895 if (!CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG)
896 || !CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG)
897 || !CHECK_FLAG(lsa_flags, OSPF_LSA_SELF))
898 return;
899
900 /* Search for existing Segment Link */
901 for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, lk))
902 if (lk->instance == srl->instance) {
903 found = true;
904 break;
905 }
906
907 if (IS_DEBUG_OSPF_SR)
908 zlog_debug(" |- %s SR Link 8.0.0.%u for SR node %s",
909 found ? "Update" : "Add",
910 GET_OPAQUE_ID(srl->instance),
911 inet_ntoa(srn->adv_router));
912
913 /* if not found, add new Segment Link and install NHLFE */
914 if (!found) {
915 /* Complete SR-Link and add it to SR-Node list */
916 srl->srn = srn;
917 IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router);
918 listnode_add(srn->ext_link, srl);
919 /* Try to set MPLS table */
920 if (compute_link_nhlfe(srl)) {
921 add_sid_nhlfe(srl->nhlfe[0]);
922 add_sid_nhlfe(srl->nhlfe[1]);
923 }
924 } else {
925 if (sr_link_cmp(lk, srl)) {
926 if (compute_link_nhlfe(srl)) {
927 update_sid_nhlfe(lk->nhlfe[0], srl->nhlfe[0]);
928 update_sid_nhlfe(lk->nhlfe[1], srl->nhlfe[1]);
929 /* Replace Segment List */
930 listnode_delete(srn->ext_link, lk);
931 XFREE(MTYPE_OSPF_SR_PARAMS, lk);
932 srl->srn = srn;
933 IPV4_ADDR_COPY(&srl->adv_router,
934 &srn->adv_router);
935 listnode_add(srn->ext_link, srl);
936 } else {
937 /* New NHLFE was not found.
938 * Just free the SR Link
939 */
940 XFREE(MTYPE_OSPF_SR_PARAMS, srl);
941 }
942 } else {
943 /*
944 * This is just an LSA refresh.
945 * Stop processing and free SR Link
946 */
947 XFREE(MTYPE_OSPF_SR_PARAMS, srl);
948 }
949 }
950 }
951
952 /* Update Segment Prefix of given Segment Routing Node */
953 static void update_ext_prefix_sid(struct sr_node *srn, struct sr_prefix *srp)
954 {
955
956 struct listnode *node;
957 struct sr_prefix *pref;
958 bool found = false;
959
960 /* Sanity check */
961 if (srn == NULL || srp == NULL)
962 return;
963
964 if (IS_DEBUG_OSPF_SR)
965 zlog_debug(" |- Process Extended Prefix SID %u", srp->sid);
966
967 /* Process only Global Prefix SID */
968 if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_LFLG))
969 return;
970
971 /* Search for existing Segment Prefix */
972 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, pref))
973 if (pref->instance == srp->instance) {
974 found = true;
975 break;
976 }
977
978 if (IS_DEBUG_OSPF_SR)
979 zlog_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %s",
980 found ? "Update" : "Add",
981 GET_OPAQUE_ID(srp->instance),
982 inet_ntoa(srn->adv_router));
983
984 /* if not found, add new Segment Prefix and install NHLFE */
985 if (!found) {
986 /* Complete SR-Prefix and add it to SR-Node list */
987 srp->srn = srn;
988 IPV4_ADDR_COPY(&srp->adv_router, &srn->adv_router);
989 listnode_add(srn->ext_prefix, srp);
990 /* Try to set MPLS table */
991 if (compute_prefix_nhlfe(srp) == 1)
992 add_sid_nhlfe(srp->nhlfe);
993 } else {
994 if (sr_prefix_cmp(pref, srp)) {
995 if (compute_prefix_nhlfe(srp) == 1) {
996 update_sid_nhlfe(pref->nhlfe, srp->nhlfe);
997 /* Replace Segment Prefix */
998 listnode_delete(srn->ext_prefix, pref);
999 XFREE(MTYPE_OSPF_SR_PARAMS, pref);
1000 srp->srn = srn;
1001 IPV4_ADDR_COPY(&srp->adv_router,
1002 &srn->adv_router);
1003 listnode_add(srn->ext_prefix, srp);
1004 } else {
1005 /* New NHLFE was not found.
1006 * Just free the SR Prefix
1007 */
1008 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1009 }
1010 } else {
1011 /* This is just an LSA refresh.
1012 * Stop processing and free SR Prefix
1013 */
1014 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1015 }
1016 }
1017 }
1018
1019 /*
1020 * When change the FRR Self SRGB, update the NHLFE Input Label
1021 * for all Extended Prefix with SID index through hash_iterate()
1022 */
1023 static void update_in_nhlfe(struct hash_backet *backet, void *args)
1024 {
1025 struct listnode *node;
1026 struct sr_node *srn = (struct sr_node *)backet->data;
1027 struct sr_prefix *srp;
1028 struct sr_nhlfe new;
1029
1030 /* Process Every Extended Prefix for this SR-Node */
1031 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1032 /* Process Self SRN only if NO-PHP is requested */
1033 if ((srn == OspfSR.self)
1034 && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG))
1035 continue;
1036
1037 /* Process only SID Index */
1038 if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG))
1039 continue;
1040
1041 /* OK. Compute new NHLFE */
1042 memcpy(&new, &srp->nhlfe, sizeof(struct sr_nhlfe));
1043 new.label_in = index2label(srp->sid, OspfSR.srgb);
1044 /* Update MPLS LFIB */
1045 update_sid_nhlfe(srp->nhlfe, new);
1046 /* Finally update Input Label */
1047 srp->nhlfe.label_in = new.label_in;
1048 }
1049 }
1050
1051 /*
1052 * When SRGB has changed, update NHLFE Output Label for all Extended Prefix
1053 * with SID index which use the given SR-Node as nexthop though hash_iterate()
1054 */
1055 static void update_out_nhlfe(struct hash_backet *backet, void *args)
1056 {
1057 struct listnode *node;
1058 struct sr_node *srn = (struct sr_node *)backet->data;
1059 struct sr_node *srnext = (struct sr_node *)args;
1060 struct sr_prefix *srp;
1061 struct sr_nhlfe new;
1062
1063 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) {
1064 /* Process only SID Index for next hop without PHP */
1065 if ((srp->nexthop == NULL)
1066 && (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)))
1067 continue;
1068 memcpy(&new, &srp->nhlfe, sizeof(struct sr_nhlfe));
1069 new.label_out = index2label(srp->sid, srnext->srgb);
1070 update_sid_nhlfe(srp->nhlfe, new);
1071 srp->nhlfe.label_out = new.label_out;
1072 }
1073 }
1074
1075 /*
1076 * Following functions are call when new Segment Routing LSA are received
1077 * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete()
1078 * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete()
1079 * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete()
1080 */
1081
1082 /* Update Segment Routing from Router Information LSA */
1083 void ospf_sr_ri_lsa_update(struct ospf_lsa *lsa)
1084 {
1085 struct sr_node *srn;
1086 struct tlv_header *tlvh;
1087 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1088 struct ri_sr_tlv_sid_label_range *ri_srgb;
1089 struct ri_sr_tlv_sr_algorithm *algo;
1090 struct sr_srgb srgb;
1091 uint16_t length = 0, sum = 0;
1092
1093 if (IS_DEBUG_OSPF_SR)
1094 zlog_debug(
1095 "SR (%s): Process Router "
1096 "Information LSA 4.0.0.%u from %s",
1097 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1098 inet_ntoa(lsah->adv_router));
1099
1100 /* Sanity check */
1101 if (IS_LSA_SELF(lsa))
1102 return;
1103
1104 if (OspfSR.neighbors == NULL) {
1105 flog_err(EC_OSPF_SR_INVALID_DB,
1106 "SR (%s): Abort! no valid SR DataBase", __func__);
1107 return;
1108 }
1109
1110 /* Get SR Node in hash table from Router ID */
1111 srn = hash_get(OspfSR.neighbors, (void *)&(lsah->adv_router),
1112 (void *)sr_node_new);
1113
1114 /* Sanity check */
1115 if (srn == NULL) {
1116 flog_err(EC_OSPF_SR_NODE_CREATE,
1117 "SR (%s): Abort! can't create SR node in hash table",
1118 __func__);
1119 return;
1120 }
1121
1122 if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) {
1123 flog_err(EC_OSPF_SR_INVALID_LSA_ID,
1124 "SR (%s): Abort! Wrong "
1125 "LSA ID 4.0.0.%u for SR node %s/%u",
1126 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1127 inet_ntoa(lsah->adv_router), srn->instance);
1128 return;
1129 }
1130
1131 /* Collect Router Information Sub TLVs */
1132 /* Initialize TLV browsing */
1133 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1134 srgb.range_size = 0;
1135 srgb.lower_bound = 0;
1136
1137 for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL);
1138 tlvh = TLV_HDR_NEXT(tlvh)) {
1139 switch (ntohs(tlvh->type)) {
1140 case RI_SR_TLV_SR_ALGORITHM:
1141 algo = (struct ri_sr_tlv_sr_algorithm *)tlvh;
1142 int i;
1143
1144 for (i = 0; i < ntohs(algo->header.length); i++)
1145 srn->algo[i] = algo->value[0];
1146 for (; i < ALGORITHM_COUNT; i++)
1147 srn->algo[i] = SR_ALGORITHM_UNSET;
1148 sum += TLV_SIZE(tlvh);
1149 break;
1150 case RI_SR_TLV_SID_LABEL_RANGE:
1151 ri_srgb = (struct ri_sr_tlv_sid_label_range *)tlvh;
1152 srgb.range_size = GET_RANGE_SIZE(ntohl(ri_srgb->size));
1153 srgb.lower_bound =
1154 GET_LABEL(ntohl(ri_srgb->lower.value));
1155 sum += TLV_SIZE(tlvh);
1156 break;
1157 case RI_SR_TLV_NODE_MSD:
1158 srn->msd = ((struct ri_sr_tlv_node_msd *)(tlvh))->value;
1159 sum += TLV_SIZE(tlvh);
1160 break;
1161 default:
1162 sum += TLV_SIZE(tlvh);
1163 break;
1164 }
1165 }
1166
1167 /* Check that we collect mandatory parameters */
1168 if (srn->algo[0] == SR_ALGORITHM_UNSET || srgb.range_size == 0
1169 || srgb.lower_bound == 0) {
1170 flog_err(EC_OSPF_SR_NODE_CREATE,
1171 "SR (%s): Missing mandatory parameters. Abort!",
1172 __func__);
1173 hash_release(OspfSR.neighbors, &(srn->adv_router));
1174 XFREE(MTYPE_OSPF_SR_PARAMS, srn);
1175 return;
1176 }
1177
1178 /* Check if it is a new SR Node or not */
1179 if (srn->instance == 0) {
1180 /* update LSA ID */
1181 srn->instance = ntohl(lsah->id.s_addr);
1182 /* Copy SRGB */
1183 srn->srgb.range_size = srgb.range_size;
1184 srn->srgb.lower_bound = srgb.lower_bound;
1185 }
1186
1187 /* Check if SRGB has changed */
1188 if ((srn->srgb.range_size != srgb.range_size)
1189 || (srn->srgb.lower_bound != srgb.lower_bound)) {
1190 srn->srgb.range_size = srgb.range_size;
1191 srn->srgb.lower_bound = srgb.lower_bound;
1192 /* Update NHLFE if it is a neighbor SR node */
1193 if (srn->neighbor == OspfSR.self)
1194 hash_iterate(OspfSR.neighbors,
1195 (void (*)(struct hash_backet *,
1196 void *))update_out_nhlfe,
1197 (void *)srn);
1198 }
1199 }
1200
1201 /*
1202 * Delete SR Node entry in hash table information corresponding to an expired
1203 * Router Information LSA
1204 */
1205 void ospf_sr_ri_lsa_delete(struct ospf_lsa *lsa)
1206 {
1207 struct sr_node *srn;
1208 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1209
1210 if (IS_DEBUG_OSPF_SR)
1211 zlog_debug("SR (%s): Remove SR node %s from lsa_id 4.0.0.%u",
1212 __func__, inet_ntoa(lsah->adv_router),
1213 GET_OPAQUE_ID(ntohl(lsah->id.s_addr)));
1214
1215 /* Sanity check */
1216 if (OspfSR.neighbors == NULL) {
1217 flog_err(EC_OSPF_SR_INVALID_DB,
1218 "SR (%s): Abort! no valid SR Data Base", __func__);
1219 return;
1220 }
1221
1222 /* Release Router ID entry in SRDB hash table */
1223 srn = hash_release(OspfSR.neighbors, &(lsah->adv_router));
1224
1225 /* Sanity check */
1226 if (srn == NULL) {
1227 flog_err(EC_OSPF_SR_NODE_CREATE,
1228 "SR (%s): Abort! no entry in SRDB for SR Node %s",
1229 __func__, inet_ntoa(lsah->adv_router));
1230 return;
1231 }
1232
1233 if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) {
1234 flog_err(EC_OSPF_SR_INVALID_LSA_ID,
1235 "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %s",
1236 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1237 inet_ntoa(lsah->adv_router));
1238 return;
1239 }
1240
1241 /* Remove SR node */
1242 sr_node_del(srn);
1243 }
1244
1245 /* Update Segment Routing from Extended Link LSA */
1246 void ospf_sr_ext_link_lsa_update(struct ospf_lsa *lsa)
1247 {
1248 struct sr_node *srn;
1249 struct tlv_header *tlvh;
1250 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1251 struct sr_link *srl;
1252
1253 uint16_t length, sum;
1254
1255 if (IS_DEBUG_OSPF_SR)
1256 zlog_debug(
1257 "SR (%s): Process Extended Link LSA 8.0.0.%u from %s",
1258 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1259 inet_ntoa(lsah->adv_router));
1260
1261 /* Sanity check */
1262 if (OspfSR.neighbors == NULL) {
1263 flog_err(EC_OSPF_SR_INVALID_DB,
1264 "SR (%s): Abort! no valid SR DataBase", __func__);
1265 return;
1266 }
1267
1268 /* Get SR Node in hash table from Router ID */
1269 srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1270 (void *)&(lsah->adv_router),
1271 (void *)sr_node_new);
1272
1273 /* Sanity check */
1274 if (srn == NULL) {
1275 flog_err(EC_OSPF_SR_NODE_CREATE,
1276 "SR (%s): Abort! can't create SR node in hash table",
1277 __func__);
1278 return;
1279 }
1280
1281 /* Initialize TLV browsing */
1282 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1283 sum = 0;
1284 for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL);
1285 tlvh = TLV_HDR_NEXT(tlvh)) {
1286 if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1287 /* Got Extended Link information */
1288 srl = get_ext_link_sid(tlvh);
1289 /* Update SID if not null */
1290 if (srl != NULL) {
1291 srl->instance = ntohl(lsah->id.s_addr);
1292 update_ext_link_sid(srn, srl, lsa->flags);
1293 }
1294 }
1295 sum += TLV_SIZE(tlvh);
1296 }
1297 }
1298
1299 /* Delete Segment Routing from Extended Link LSA */
1300 void ospf_sr_ext_link_lsa_delete(struct ospf_lsa *lsa)
1301 {
1302 struct listnode *node;
1303 struct sr_link *srl;
1304 struct sr_node *srn;
1305 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1306 uint32_t instance = ntohl(lsah->id.s_addr);
1307
1308 if (IS_DEBUG_OSPF_SR)
1309 zlog_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %s",
1310 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1311 inet_ntoa(lsah->adv_router));
1312
1313 /* Sanity check */
1314 if (OspfSR.neighbors == NULL) {
1315 flog_err(EC_OSPF_SR_INVALID_DB,
1316 "SR (%s): Abort! no valid SR DataBase", __func__);
1317 return;
1318 }
1319
1320 /* Search SR Node in hash table from Router ID */
1321 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1322 (void *)&(lsah->adv_router));
1323
1324 /*
1325 * SR-Node may be NULL if it has been remove previously when
1326 * processing Router Information LSA deletion
1327 */
1328 if (srn == NULL) {
1329 flog_err(EC_OSPF_SR_INVALID_DB,
1330 "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 flog_err(EC_OSPF_SR_INVALID_DB,
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 flog_err(EC_OSPF_SR_INVALID_DB,
1375 "SR (%s): Abort! no valid SR DataBase", __func__);
1376 return;
1377 }
1378
1379 /* Get SR Node in hash table from Router ID */
1380 srn = (struct sr_node *)hash_get(OspfSR.neighbors,
1381 (void *)&(lsah->adv_router),
1382 (void *)sr_node_new);
1383
1384 /* Sanity check */
1385 if (srn == NULL) {
1386 flog_err(EC_OSPF_SR_NODE_CREATE,
1387 "SR (%s): Abort! can't create SR node in hash table",
1388 __func__);
1389 return;
1390 }
1391
1392 /* Initialize TLV browsing */
1393 length = ntohs(lsah->length) - OSPF_LSA_HEADER_SIZE;
1394 sum = 0;
1395 for (tlvh = TLV_HDR_TOP(lsah); sum < length;
1396 tlvh = TLV_HDR_NEXT(tlvh)) {
1397 if (ntohs(tlvh->type) == EXT_TLV_LINK) {
1398 /* Got Extended Link information */
1399 srp = get_ext_prefix_sid(tlvh);
1400 /* Update SID if not null */
1401 if (srp != NULL) {
1402 srp->instance = ntohl(lsah->id.s_addr);
1403 update_ext_prefix_sid(srn, srp);
1404 }
1405 }
1406 sum += TLV_SIZE(tlvh);
1407 }
1408 }
1409
1410 /* Delete Segment Routing from Extended Prefix LSA */
1411 void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa *lsa)
1412 {
1413 struct listnode *node;
1414 struct sr_prefix *srp;
1415 struct sr_node *srn;
1416 struct lsa_header *lsah = (struct lsa_header *)lsa->data;
1417 uint32_t instance = ntohl(lsah->id.s_addr);
1418
1419 if (IS_DEBUG_OSPF_SR)
1420 zlog_debug(
1421 "SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %s",
1422 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1423 inet_ntoa(lsah->adv_router));
1424
1425 /* Sanity check */
1426 if (OspfSR.neighbors == NULL) {
1427 flog_err(EC_OSPF_SR_INVALID_DB,
1428 "SR (%s): Abort! no valid SR DataBase", __func__);
1429 return;
1430 }
1431
1432 /* Search SR Node in hash table from Router ID */
1433 srn = (struct sr_node *)hash_lookup(OspfSR.neighbors,
1434 (void *)&(lsah->adv_router));
1435
1436 /*
1437 * SR-Node may be NULL if it has been remove previously when
1438 * processing Router Information LSA deletion
1439 */
1440 if (srn == NULL) {
1441 flog_err(EC_OSPF_SR_INVALID_DB,
1442 "SR (%s): Stop! no entry in SRDB for SR Node %s",
1443 __func__, inet_ntoa(lsah->adv_router));
1444 return;
1445 }
1446
1447 /* Search for corresponding Segment Link */
1448 for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp))
1449 if (srp->instance == instance)
1450 break;
1451
1452 /* Remove Segment Link if found */
1453 if ((srp != NULL) && (srp->instance == instance)) {
1454 del_sid_nhlfe(srp->nhlfe);
1455 listnode_delete(srn->ext_link, srp);
1456 XFREE(MTYPE_OSPF_SR_PARAMS, srp);
1457 } else {
1458 flog_err(
1459 EC_OSPF_SR_INVALID_DB,
1460 "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %s",
1461 __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)),
1462 inet_ntoa(lsah->adv_router));
1463 }
1464 }
1465
1466 /* Get Label for Extended Link SID */
1467 /* TODO: To be replace by Zebra Label Manager */
1468 uint32_t get_ext_link_label_value(void)
1469 {
1470 static uint32_t label = ADJ_SID_MIN - 1;
1471
1472 if (label < ADJ_SID_MAX)
1473 label += 1;
1474
1475 return label;
1476 }
1477
1478 /*
1479 * Update Prefix SID. Call by ospf_ext_pref_ism_change to
1480 * complete initial CLI command at startutp.
1481 *
1482 * @param ifp - Loopback interface
1483 * @param pref - Prefix address of this interface
1484 *
1485 * @return - void
1486 */
1487 void ospf_sr_update_prefix(struct interface *ifp, struct prefix *p)
1488 {
1489 struct listnode *node;
1490 struct sr_prefix *srp;
1491
1492 /* Sanity Check */
1493 if ((ifp == NULL) || (p == NULL))
1494 return;
1495
1496 /*
1497 * Search if there is a Segment Prefix that correspond to this
1498 * interface or prefix, and update it if found
1499 */
1500 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1501 if ((srp->nhlfe.ifindex == ifp->ifindex)
1502 || ((IPV4_ADDR_SAME(&srp->nhlfe.prefv4.prefix,
1503 &p->u.prefix4))
1504 && (srp->nhlfe.prefv4.prefixlen == p->prefixlen))) {
1505
1506 /* Update Interface & Prefix info */
1507 srp->nhlfe.ifindex = ifp->ifindex;
1508 IPV4_ADDR_COPY(&srp->nhlfe.prefv4.prefix,
1509 &p->u.prefix4);
1510 srp->nhlfe.prefv4.prefixlen = p->prefixlen;
1511 srp->nhlfe.prefv4.family = p->family;
1512 IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &p->u.prefix4);
1513
1514 /* OK. Let's Schedule Extended Prefix LSA */
1515 srp->instance = ospf_ext_schedule_prefix_index(
1516 ifp, srp->sid, &srp->nhlfe.prefv4, srp->flags);
1517
1518 /* Install NHLFE if NO-PHP is requested */
1519 if (CHECK_FLAG(srp->flags,
1520 EXT_SUBTLV_PREFIX_SID_NPFLG)) {
1521 srp->nhlfe.label_in = index2label(
1522 srp->sid, OspfSR.self->srgb);
1523 srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
1524 add_sid_nhlfe(srp->nhlfe);
1525 }
1526 }
1527 }
1528 }
1529
1530 /*
1531 * Following functions are used to update MPLS LFIB after a SPF run
1532 */
1533
1534 static void ospf_sr_nhlfe_update(struct hash_backet *backet, void *args)
1535 {
1536
1537 struct sr_node *srn = (struct sr_node *)backet->data;
1538 struct listnode *node;
1539 struct sr_prefix *srp;
1540 struct sr_nhlfe old;
1541 int rc;
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 ospf_sr_start(ospf);
1708
1709 /* Set Router Information SR parameters */
1710 if (IS_DEBUG_OSPF_EVENT)
1711 zlog_debug("SR: Activate SR for Router Information LSA");
1712
1713 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1714
1715 /* Update Ext LSA */
1716 if (IS_DEBUG_OSPF_EVENT)
1717 zlog_debug("SR: Activate SR for Extended Link/Prefix LSA");
1718
1719 ospf_ext_update_sr(true);
1720
1721 return CMD_SUCCESS;
1722 }
1723
1724 DEFUN (no_ospf_sr_enable,
1725 no_ospf_sr_enable_cmd,
1726 "no segment-routing [on]",
1727 NO_STR
1728 SR_STR
1729 "Disable Segment Routing\n")
1730 {
1731
1732 if (!OspfSR.enabled)
1733 return CMD_SUCCESS;
1734
1735 if (IS_DEBUG_OSPF_EVENT)
1736 zlog_debug("SR: Segment Routing: ON -> OFF");
1737
1738 /* Start by Disabling Extended Link & Prefix LSA */
1739 ospf_ext_update_sr(false);
1740
1741 /* then, disable Router Information SR parameters */
1742 ospf_router_info_update_sr(false, OspfSR.srgb, OspfSR.msd);
1743
1744 /* Finally, stop Segment Routing */
1745 ospf_sr_stop();
1746 OspfSR.enabled = false;
1747
1748 return CMD_SUCCESS;
1749 }
1750
1751 static int ospf_sr_enabled(struct vty *vty)
1752 {
1753 if (OspfSR.enabled)
1754 return 1;
1755
1756 if (vty)
1757 vty_out(vty, "%% OSPF SR is not turned on\n");
1758
1759 return 0;
1760 }
1761
1762 DEFUN (sr_sid_label_range,
1763 sr_sid_label_range_cmd,
1764 "segment-routing global-block (0-1048575) (0-1048575)",
1765 SR_STR
1766 "Segment Routing Global Block label range\n"
1767 "Lower-bound range in decimal (0-1048575)\n"
1768 "Upper-bound range in decimal (0-1048575)\n")
1769 {
1770 uint32_t upper;
1771 uint32_t lower;
1772 uint32_t size;
1773 int idx_low = 2;
1774 int idx_up = 3;
1775
1776 if (!ospf_sr_enabled(vty))
1777 return CMD_WARNING_CONFIG_FAILED;
1778
1779 /* Get lower and upper bound */
1780 lower = strtoul(argv[idx_low]->arg, NULL, 10);
1781 upper = strtoul(argv[idx_up]->arg, NULL, 10);
1782 size = upper - lower + 1;
1783
1784 if (size > MPLS_DEFAULT_MAX_SRGB_SIZE || size <= 0) {
1785 vty_out(vty,
1786 "Range size cannot be less than 0 or more than %u\n",
1787 MPLS_DEFAULT_MAX_SRGB_SIZE);
1788 return CMD_WARNING_CONFIG_FAILED;
1789 }
1790
1791 if (upper > MPLS_DEFAULT_MAX_SRGB_LABEL) {
1792 vty_out(vty, "Upper-bound cannot exceed %u\n",
1793 MPLS_DEFAULT_MAX_SRGB_LABEL);
1794 return CMD_WARNING_CONFIG_FAILED;
1795 }
1796
1797 if (upper < MPLS_DEFAULT_MIN_SRGB_LABEL) {
1798 vty_out(vty, "Upper-bound cannot be lower than %u\n",
1799 MPLS_DEFAULT_MIN_SRGB_LABEL);
1800 return CMD_WARNING_CONFIG_FAILED;
1801 }
1802
1803 /* Check if values have changed */
1804 if ((OspfSR.srgb.range_size == size)
1805 && (OspfSR.srgb.lower_bound == lower))
1806 return CMD_SUCCESS;
1807
1808 /* Set SID/Label range SRGB */
1809 OspfSR.srgb.range_size = size;
1810 OspfSR.srgb.lower_bound = lower;
1811 if (OspfSR.self != NULL) {
1812 OspfSR.self->srgb.range_size = size;
1813 OspfSR.self->srgb.lower_bound = lower;
1814 }
1815
1816 /* Set Router Information SR parameters */
1817 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1818
1819 /* Update NHLFE entries */
1820 hash_iterate(OspfSR.neighbors,
1821 (void (*)(struct hash_backet *, void *))update_in_nhlfe,
1822 NULL);
1823
1824 return CMD_SUCCESS;
1825 }
1826
1827 DEFUN (no_sr_sid_label_range,
1828 no_sr_sid_label_range_cmd,
1829 "no segment-routing global-block [(0-1048575) (0-1048575)]",
1830 NO_STR
1831 SR_STR
1832 "Segment Routing Global Block label range\n"
1833 "Lower-bound range in decimal (0-1048575)\n"
1834 "Upper-bound range in decimal (0-1048575)\n")
1835 {
1836
1837 if (!ospf_sr_enabled(vty))
1838 return CMD_WARNING_CONFIG_FAILED;
1839
1840 /* Revert to default SRGB value */
1841 OspfSR.srgb.range_size = MPLS_DEFAULT_MIN_SRGB_SIZE;
1842 OspfSR.srgb.lower_bound = MPLS_DEFAULT_MIN_SRGB_LABEL;
1843 if (OspfSR.self != NULL) {
1844 OspfSR.self->srgb.range_size = OspfSR.srgb.range_size;
1845 OspfSR.self->srgb.lower_bound = OspfSR.srgb.lower_bound;
1846 }
1847
1848 /* Set Router Information SR parameters */
1849 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1850
1851 /* Update NHLFE entries */
1852 hash_iterate(OspfSR.neighbors,
1853 (void (*)(struct hash_backet *, void *))update_in_nhlfe,
1854 NULL);
1855
1856 return CMD_SUCCESS;
1857 }
1858
1859 DEFUN (sr_node_msd,
1860 sr_node_msd_cmd,
1861 "segment-routing node-msd (1-16)",
1862 SR_STR
1863 "Maximum Stack Depth for this router\n"
1864 "Maximum number of label that could be stack (1-16)\n")
1865 {
1866 uint32_t msd;
1867 int idx = 1;
1868
1869 if (!ospf_sr_enabled(vty))
1870 return CMD_WARNING_CONFIG_FAILED;
1871
1872 /* Get MSD */
1873 argv_find(argv, argc, "(1-16)", &idx);
1874 msd = strtoul(argv[idx]->arg, NULL, 10);
1875 if (msd < 1 || msd > MPLS_MAX_LABELS) {
1876 vty_out(vty, "MSD must be comprise between 1 and %u\n",
1877 MPLS_MAX_LABELS);
1878 return CMD_WARNING_CONFIG_FAILED;
1879 }
1880
1881 /* Check if value has changed */
1882 if (OspfSR.msd == msd)
1883 return CMD_SUCCESS;
1884
1885 /* Set this router MSD */
1886 OspfSR.msd = msd;
1887 if (OspfSR.self != NULL)
1888 OspfSR.self->msd = msd;
1889
1890 /* Set Router Information SR parameters */
1891 ospf_router_info_update_sr(true, OspfSR.srgb, OspfSR.msd);
1892
1893 return CMD_SUCCESS;
1894 }
1895
1896 DEFUN (no_sr_node_msd,
1897 no_sr_node_msd_cmd,
1898 "no segment-routing node-msd [(1-16)]",
1899 NO_STR
1900 SR_STR
1901 "Maximum Stack Depth for this router\n"
1902 "Maximum number of label that could be stack (1-16)\n")
1903 {
1904
1905 if (!ospf_sr_enabled(vty))
1906 return CMD_WARNING_CONFIG_FAILED;
1907
1908 /* unset this router MSD */
1909 OspfSR.msd = 0;
1910 if (OspfSR.self != NULL)
1911 OspfSR.self->msd = 0;
1912
1913 /* Set Router Information SR parameters */
1914 ospf_router_info_update_sr(true, OspfSR.srgb, 0);
1915
1916 return CMD_SUCCESS;
1917 }
1918
1919 DEFUN (sr_prefix_sid,
1920 sr_prefix_sid_cmd,
1921 "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag]",
1922 SR_STR
1923 "Prefix SID\n"
1924 "IPv4 Prefix as A.B.C.D/M\n"
1925 "SID index for this prefix in decimal (0-65535)\n"
1926 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
1927 "Don't request Penultimate Hop Popping (PHP)\n")
1928 {
1929 int idx = 0;
1930 struct prefix p;
1931 uint32_t index;
1932 struct listnode *node;
1933 struct sr_prefix *srp, *new;
1934 struct interface *ifp;
1935
1936 if (!ospf_sr_enabled(vty))
1937 return CMD_WARNING_CONFIG_FAILED;
1938
1939 /* Get network prefix */
1940 argv_find(argv, argc, "A.B.C.D/M", &idx);
1941 if (!str2prefix(argv[idx]->arg, &p)) {
1942 vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg);
1943 return CMD_WARNING_CONFIG_FAILED;
1944 }
1945
1946 /* Get & verify index value */
1947 argv_find(argv, argc, "(0-65535)", &idx);
1948 index = strtoul(argv[idx]->arg, NULL, 10);
1949 if (index > OspfSR.srgb.range_size - 1) {
1950 vty_out(vty, "Index %u must be lower than range size %u\n",
1951 index, OspfSR.srgb.range_size);
1952 return CMD_WARNING_CONFIG_FAILED;
1953 }
1954
1955 /* check that the index is not already used */
1956 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
1957 if (srp->sid == index) {
1958 vty_out(vty, "Index %u is already used\n", index);
1959 return CMD_WARNING_CONFIG_FAILED;
1960 }
1961 }
1962
1963 /* Create new Extended Prefix to SRDB if not found */
1964 new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix));
1965 IPV4_ADDR_COPY(&new->nhlfe.prefv4.prefix, &p.u.prefix4);
1966 IPV4_ADDR_COPY(&new->nhlfe.nexthop, &p.u.prefix4);
1967 new->nhlfe.prefv4.prefixlen = p.prefixlen;
1968 new->nhlfe.prefv4.family = p.family;
1969 new->sid = index;
1970 /* Set NO PHP flag if present and compute NHLFE */
1971 if (argv_find(argv, argc, "no-php-flag", &idx)) {
1972 SET_FLAG(new->flags, EXT_SUBTLV_PREFIX_SID_NPFLG);
1973 new->nhlfe.label_in = index2label(new->sid, OspfSR.self->srgb);
1974 new->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL;
1975 }
1976
1977 if (IS_DEBUG_OSPF_SR)
1978 zlog_debug("SR (%s): Add new index %u to Prefix %s/%u",
1979 __func__, index, inet_ntoa(new->nhlfe.prefv4.prefix),
1980 new->nhlfe.prefv4.prefixlen);
1981
1982 /* Get Interface and check if it is a Loopback */
1983 ifp = if_lookup_prefix(&p, VRF_DEFAULT);
1984 if (ifp == NULL) {
1985 /*
1986 * Interface could be not yet available i.e. when this
1987 * command is in the configuration file, OSPF is not yet
1988 * ready. In this case, store the prefix SID for latter
1989 * update of this Extended Prefix
1990 */
1991 listnode_add(OspfSR.self->ext_prefix, new);
1992 zlog_info(
1993 "Interface for prefix %s/%u not found. Deferred LSA "
1994 "flooding",
1995 inet_ntoa(p.u.prefix4), p.prefixlen);
1996 return CMD_SUCCESS;
1997 }
1998
1999 if (!if_is_loopback(ifp)) {
2000 vty_out(vty, "interface %s is not a Loopback\n", ifp->name);
2001 XFREE(MTYPE_OSPF_SR_PARAMS, new);
2002 return CMD_WARNING_CONFIG_FAILED;
2003 }
2004 new->nhlfe.ifindex = ifp->ifindex;
2005
2006 /* Search if this prefix already exist */
2007 for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) {
2008 if ((IPV4_ADDR_SAME(&srp->nhlfe.prefv4.prefix, &p.u.prefix4)
2009 && srp->nhlfe.prefv4.prefixlen == p.prefixlen))
2010 break;
2011 else
2012 srp = NULL;
2013 }
2014
2015 /* Update or Add this new SR Prefix */
2016 if (srp) {
2017 update_sid_nhlfe(srp->nhlfe, new->nhlfe);
2018 listnode_delete(OspfSR.self->ext_prefix, srp);
2019 listnode_add(OspfSR.self->ext_prefix, new);
2020 } else {
2021 listnode_add(OspfSR.self->ext_prefix, new);
2022 add_sid_nhlfe(new->nhlfe);
2023 }
2024
2025 /* Finally, update Extended Prefix LSA */
2026 new->instance = ospf_ext_schedule_prefix_index(
2027 ifp, new->sid, &new->nhlfe.prefv4, new->flags);
2028 if (new->instance == 0) {
2029 vty_out(vty, "Unable to set index %u for prefix %s/%u\n", index,
2030 inet_ntoa(p.u.prefix4), p.prefixlen);
2031 return CMD_WARNING;
2032 }
2033
2034 return CMD_SUCCESS;
2035 }
2036
2037 DEFUN (no_sr_prefix_sid,
2038 no_sr_prefix_sid_cmd,
2039 "no segment-routing prefix A.B.C.D/M [index (0-65535) no-php-flag]",
2040 NO_STR
2041 SR_STR
2042 "Prefix SID\n"
2043 "IPv4 Prefix as A.B.C.D/M\n"
2044 "SID index for this prefix in decimal (0-65535)\n"
2045 "Index value inside SRGB (lower_bound < index < upper_bound)\n"
2046 "Don't request Penultimate Hop Popping (PHP)\n")
2047 {
2048 int idx = 0;
2049 struct prefix p;
2050 struct listnode *node;
2051 struct sr_prefix *srp;
2052 struct interface *ifp;
2053 bool found = false;
2054 int rc;
2055
2056 if (!ospf_sr_enabled(vty))
2057 return CMD_WARNING_CONFIG_FAILED;
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 bool 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 }