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