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