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