]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
cf9b9f77 OD |
2 | /* |
3 | * This is an implementation of Segment Routing | |
6f751f14 OD |
4 | * as per RFC 8665 - OSPF Extensions for Segment Routing |
5 | * and RFC 8476 - Signaling Maximum SID Depth (MSD) Using OSPF | |
cf9b9f77 OD |
6 | * |
7 | * Module name: Segment Routing | |
8 | * | |
cf9b9f77 | 9 | * Author: Olivier Dugeon <olivier.dugeon@orange.com> |
7743f2f8 | 10 | * Author: Anselme Sawadogo <anselmesawadogo@gmail.com> |
cf9b9f77 | 11 | * |
6f751f14 | 12 | * Copyright (C) 2016 - 2020 Orange Labs http://www.orange.com |
cf9b9f77 OD |
13 | */ |
14 | ||
b45ac5f5 DL |
15 | #ifdef HAVE_CONFIG_H |
16 | #include "config.h" | |
17 | #endif | |
18 | ||
cf9b9f77 OD |
19 | #include <math.h> |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <zebra.h> | |
23 | ||
b61264a8 | 24 | #include "printfrr.h" |
cf9b9f77 OD |
25 | #include "command.h" |
26 | #include "hash.h" | |
27 | #include "if.h" | |
28 | #include "if.h" | |
29 | #include "jhash.h" | |
30 | #include "libospf.h" /* for ospf interface types */ | |
31 | #include "linklist.h" | |
32 | #include "log.h" | |
33 | #include "memory.h" | |
34 | #include "monotime.h" | |
35 | #include "network.h" | |
36 | #include "prefix.h" | |
37 | #include "sockunion.h" /* for inet_aton() */ | |
38 | #include "stream.h" | |
39 | #include "table.h" | |
40 | #include "thread.h" | |
41 | #include "vty.h" | |
42 | #include "zclient.h" | |
b37eb79c | 43 | #include "sbuf.h" |
76665890 | 44 | #include <lib/json.h> |
313d7993 | 45 | #include "ospf_errors.h" |
cf9b9f77 OD |
46 | |
47 | #include "ospfd/ospfd.h" | |
48 | #include "ospfd/ospf_interface.h" | |
49 | #include "ospfd/ospf_ism.h" | |
50 | #include "ospfd/ospf_asbr.h" | |
51 | #include "ospfd/ospf_lsa.h" | |
52 | #include "ospfd/ospf_lsdb.h" | |
53 | #include "ospfd/ospf_neighbor.h" | |
54 | #include "ospfd/ospf_nsm.h" | |
55 | #include "ospfd/ospf_flood.h" | |
56 | #include "ospfd/ospf_packet.h" | |
57 | #include "ospfd/ospf_spf.h" | |
58 | #include "ospfd/ospf_dump.h" | |
59 | #include "ospfd/ospf_route.h" | |
60 | #include "ospfd/ospf_ase.h" | |
61 | #include "ospfd/ospf_sr.h" | |
62 | #include "ospfd/ospf_ri.h" | |
63 | #include "ospfd/ospf_ext.h" | |
64 | #include "ospfd/ospf_zebra.h" | |
65 | ||
66 | /* | |
67 | * Global variable to manage Segment Routing on this node. | |
68 | * Note that all parameter values are stored in network byte order. | |
69 | */ | |
70 | static struct ospf_sr_db OspfSR; | |
71 | static void ospf_sr_register_vty(void); | |
b37eb79c | 72 | static inline void del_adj_sid(struct sr_nhlfe nhlfe); |
d81b8e0e | 73 | static int ospf_sr_start(struct ospf *ospf); |
cf9b9f77 OD |
74 | |
75 | /* | |
76 | * Segment Routing Data Base functions | |
77 | */ | |
78 | ||
79 | /* Hash function for Segment Routing entry */ | |
d8b87afe | 80 | static unsigned int sr_hash(const void *p) |
cf9b9f77 OD |
81 | { |
82 | const struct in_addr *rid = p; | |
83 | ||
7743f2f8 | 84 | return jhash_1word(rid->s_addr, 0); |
cf9b9f77 OD |
85 | } |
86 | ||
87 | /* Compare 2 Router ID hash entries based on SR Node */ | |
74df8d6d | 88 | static bool sr_cmp(const void *p1, const void *p2) |
cf9b9f77 OD |
89 | { |
90 | const struct sr_node *srn = p1; | |
91 | const struct in_addr *rid = p2; | |
92 | ||
7743f2f8 | 93 | return IPV4_ADDR_SAME(&srn->adv_router, rid); |
cf9b9f77 OD |
94 | } |
95 | ||
fd3b19f2 OD |
96 | /* Functions to remove an SR Link */ |
97 | static void del_sr_link(void *val) | |
cf9b9f77 | 98 | { |
fd3b19f2 OD |
99 | struct sr_link *srl = (struct sr_link *)val; |
100 | ||
b37eb79c OD |
101 | del_adj_sid(srl->nhlfe[0]); |
102 | del_adj_sid(srl->nhlfe[1]); | |
fd3b19f2 | 103 | XFREE(MTYPE_OSPF_SR_PARAMS, val); |
fd3b19f2 OD |
104 | } |
105 | ||
106 | /* Functions to remove an SR Prefix */ | |
107 | static void del_sr_pref(void *val) | |
108 | { | |
109 | struct sr_prefix *srp = (struct sr_prefix *)val; | |
110 | ||
43e7abb5 | 111 | ospf_zebra_delete_prefix_sid(srp); |
cf9b9f77 | 112 | XFREE(MTYPE_OSPF_SR_PARAMS, val); |
cf9b9f77 OD |
113 | } |
114 | ||
115 | /* Allocate new Segment Routine node */ | |
116 | static struct sr_node *sr_node_new(struct in_addr *rid) | |
117 | { | |
118 | ||
119 | if (rid == NULL) | |
120 | return NULL; | |
121 | ||
122 | struct sr_node *new; | |
123 | ||
124 | /* Allocate Segment Routing node memory */ | |
125 | new = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_node)); | |
126 | ||
cf9b9f77 OD |
127 | /* Default Algorithm, SRGB and MSD */ |
128 | for (int i = 0; i < ALGORITHM_COUNT; i++) | |
fd3b19f2 | 129 | new->algo[i] = SR_ALGORITHM_UNSET; |
cf9b9f77 OD |
130 | |
131 | new->srgb.range_size = 0; | |
132 | new->srgb.lower_bound = 0; | |
133 | new->msd = 0; | |
134 | ||
135 | /* Create Link, Prefix and Range TLVs list */ | |
136 | new->ext_link = list_new(); | |
137 | new->ext_prefix = list_new(); | |
fd3b19f2 OD |
138 | new->ext_link->del = del_sr_link; |
139 | new->ext_prefix->del = del_sr_pref; | |
cf9b9f77 | 140 | |
cf9b9f77 OD |
141 | IPV4_ADDR_COPY(&new->adv_router, rid); |
142 | new->neighbor = NULL; | |
143 | new->instance = 0; | |
144 | ||
b37eb79c | 145 | osr_debug(" |- Created new SR node for %pI4", &new->adv_router); |
cf9b9f77 OD |
146 | return new; |
147 | } | |
148 | ||
7fd0729f G |
149 | /* Supposed to be used for testing */ |
150 | struct sr_node *ospf_sr_node_create(struct in_addr *rid) | |
151 | { | |
152 | struct sr_node *srn; | |
153 | ||
154 | srn = hash_get(OspfSR.neighbors, (void *)rid, (void *)sr_node_new); | |
155 | ||
156 | return srn; | |
157 | } | |
158 | ||
cf9b9f77 OD |
159 | /* Delete Segment Routing node */ |
160 | static void sr_node_del(struct sr_node *srn) | |
161 | { | |
cf9b9f77 OD |
162 | /* Sanity Check */ |
163 | if (srn == NULL) | |
164 | return; | |
165 | ||
b61264a8 OD |
166 | osr_debug(" |- Delete SR node for %pI4", &srn->adv_router); |
167 | ||
cf9b9f77 | 168 | /* Clean Extended Link */ |
6a154c88 | 169 | list_delete(&srn->ext_link); |
cf9b9f77 OD |
170 | |
171 | /* Clean Prefix List */ | |
6a154c88 | 172 | list_delete(&srn->ext_prefix); |
cf9b9f77 OD |
173 | |
174 | XFREE(MTYPE_OSPF_SR_PARAMS, srn); | |
175 | } | |
176 | ||
177 | /* Get SR Node for a given nexthop */ | |
178 | static struct sr_node *get_sr_node_by_nexthop(struct ospf *ospf, | |
179 | struct in_addr nexthop) | |
180 | { | |
181 | struct ospf_interface *oi = NULL; | |
182 | struct ospf_neighbor *nbr = NULL; | |
183 | struct listnode *node; | |
184 | struct route_node *rn; | |
185 | struct sr_node *srn; | |
7743f2f8 | 186 | bool found; |
cf9b9f77 OD |
187 | |
188 | /* Sanity check */ | |
189 | if (OspfSR.neighbors == NULL) | |
190 | return NULL; | |
191 | ||
b37eb79c | 192 | osr_debug(" |- Search SR-Node for nexthop %pI4", &nexthop); |
cf9b9f77 OD |
193 | |
194 | /* First, search neighbor Router ID for this nexthop */ | |
7743f2f8 OD |
195 | found = false; |
196 | for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) { | |
197 | for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) { | |
198 | nbr = rn->info; | |
199 | if ((nbr) && (IPV4_ADDR_SAME(&nexthop, &nbr->src))) { | |
200 | found = true; | |
cf9b9f77 | 201 | break; |
7743f2f8 OD |
202 | } |
203 | } | |
204 | if (found) | |
205 | break; | |
206 | } | |
cf9b9f77 | 207 | |
7743f2f8 | 208 | if (!found) |
cf9b9f77 OD |
209 | return NULL; |
210 | ||
b37eb79c OD |
211 | osr_debug(" |- Found nexthop Router ID %pI4", &nbr->router_id); |
212 | ||
cf9b9f77 OD |
213 | /* Then, search SR Node */ |
214 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, &nbr->router_id); | |
215 | ||
216 | return srn; | |
217 | } | |
218 | ||
6f751f14 OD |
219 | /* |
220 | * Segment Routing Local Block management functions | |
221 | */ | |
222 | ||
223 | /** | |
224 | * It is necessary to known which label is already allocated to manage the range | |
225 | * of SRLB. This is particular useful when an interface flap (goes up / down | |
226 | * frequently). Here, SR will release and then allocate label for the Adjacency | |
227 | * for each concerned interface. If we don't care, there is a risk to run out of | |
228 | * label. | |
229 | * | |
230 | * For that purpose, a similar principle as already provided to manage chunk of | |
231 | * label is proposed. But, here, the label chunk has not a fix range of 64 | |
232 | * labels that could be easily manage with a single variable of 64 bits size. | |
233 | * So, used_mark is used as a bit wise to mark label reserved (bit set) or not | |
234 | * (bit unset). Its size is equal to the number of label of the SRLB range round | |
235 | * up to 64 bits. | |
236 | * | |
237 | * - sr__local_block_init() computes the number of 64 bits variables that are | |
238 | * needed to manage the SRLB range and allocates this number. | |
239 | * - ospf_sr_local_block_request_label() pick up the first available label and | |
240 | * set corresponding bit | |
241 | * - ospf_sr_local_block_release_label() release label by reseting the | |
242 | * corresponding bit and set the next label to the first free position | |
243 | */ | |
244 | ||
245 | /** | |
246 | * Initialize Segment Routing Local Block from SRDB configuration and reserve | |
247 | * block of bits to manage label allocation. | |
248 | * | |
249 | * @param lower_bound The lower bound of the SRLB range | |
250 | * @param upper_bound The upper bound of the SRLB range | |
251 | * | |
252 | * @return 0 on success, -1 otherwise | |
253 | */ | |
254 | static int sr_local_block_init(uint32_t lower_bound, uint32_t upper_bound) | |
255 | { | |
256 | struct sr_local_block *srlb = &OspfSR.srlb; | |
257 | uint32_t size; | |
258 | ||
259 | /* Check if SRLB is not already configured */ | |
260 | if (srlb->reserved) | |
261 | return 0; | |
262 | ||
263 | /* | |
264 | * Request SRLB to the label manager. If the allocation fails, return | |
265 | * an error to disable SR until a new SRLB is successfully allocated. | |
266 | */ | |
267 | size = upper_bound - lower_bound + 1; | |
268 | if (ospf_zebra_request_label_range(lower_bound, size)) { | |
daaebd39 FR |
269 | zlog_err("SR: Error reserving SRLB [%u/%u] %u labels", |
270 | lower_bound, upper_bound, size); | |
6f751f14 OD |
271 | return -1; |
272 | } | |
273 | ||
daaebd39 FR |
274 | osr_debug("SR: Got new SRLB [%u/%u], %u labels", lower_bound, |
275 | upper_bound, size); | |
6f751f14 OD |
276 | |
277 | /* Initialize the SRLB */ | |
278 | srlb->start = lower_bound; | |
279 | srlb->end = upper_bound; | |
280 | srlb->current = 0; | |
daaebd39 | 281 | |
6f751f14 OD |
282 | /* Compute the needed Used Mark number and allocate them */ |
283 | srlb->max_block = size / SRLB_BLOCK_SIZE; | |
284 | if ((size % SRLB_BLOCK_SIZE) != 0) | |
285 | srlb->max_block++; | |
286 | srlb->used_mark = XCALLOC(MTYPE_OSPF_SR_PARAMS, | |
287 | srlb->max_block * SRLB_BLOCK_SIZE); | |
288 | srlb->reserved = true; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
daaebd39 FR |
293 | static int sr_global_block_init(uint32_t start, uint32_t size) |
294 | { | |
295 | struct sr_global_block *srgb = &OspfSR.srgb; | |
296 | ||
297 | /* Check if already configured */ | |
298 | if (srgb->reserved) | |
299 | return 0; | |
300 | ||
301 | /* request chunk */ | |
302 | uint32_t end = start + size - 1; | |
303 | if (ospf_zebra_request_label_range(start, size) < 0) { | |
304 | zlog_err("SR: Error reserving SRGB [%u/%u], %u labels", start, | |
305 | end, size); | |
306 | return -1; | |
307 | } | |
308 | ||
309 | osr_debug("SR: Got new SRGB [%u/%u], %u labels", start, end, size); | |
310 | ||
311 | /* success */ | |
312 | srgb->start = start; | |
313 | srgb->size = size; | |
314 | srgb->reserved = true; | |
315 | return 0; | |
316 | } | |
317 | ||
6f751f14 OD |
318 | /** |
319 | * Remove Segment Routing Local Block. | |
320 | * | |
321 | */ | |
9fb83ab1 | 322 | static void sr_local_block_delete(void) |
6f751f14 OD |
323 | { |
324 | struct sr_local_block *srlb = &OspfSR.srlb; | |
325 | ||
326 | /* Check if SRLB is not already delete */ | |
327 | if (!srlb->reserved) | |
328 | return; | |
329 | ||
330 | osr_debug("SR (%s): Remove SRLB [%u/%u]", __func__, srlb->start, | |
331 | srlb->end); | |
332 | ||
333 | /* First release the label block */ | |
334 | ospf_zebra_release_label_range(srlb->start, srlb->end); | |
335 | ||
336 | /* Then reset SRLB structure */ | |
337 | if (srlb->used_mark != NULL) | |
338 | XFREE(MTYPE_OSPF_SR_PARAMS, srlb->used_mark); | |
c181efbe | 339 | |
6f751f14 OD |
340 | srlb->reserved = false; |
341 | } | |
342 | ||
c181efbe FR |
343 | /** |
344 | * Remove Segment Routing Global block | |
345 | */ | |
346 | static void sr_global_block_delete(void) | |
347 | { | |
348 | struct sr_global_block *srgb = &OspfSR.srgb; | |
349 | ||
350 | if (!srgb->reserved) | |
351 | return; | |
352 | ||
353 | osr_debug("SR (%s): Remove SRGB [%u/%u]", __func__, srgb->start, | |
354 | srgb->start + srgb->size - 1); | |
355 | ||
356 | ospf_zebra_release_label_range(srgb->start, | |
357 | srgb->start + srgb->size - 1); | |
358 | ||
359 | srgb->reserved = false; | |
360 | } | |
361 | ||
362 | ||
6f751f14 OD |
363 | /** |
364 | * Request a label from the Segment Routing Local Block. | |
365 | * | |
366 | * @return First available label on success or MPLS_INVALID_LABEL if the | |
367 | * block of labels is full | |
368 | */ | |
369 | mpls_label_t ospf_sr_local_block_request_label(void) | |
370 | { | |
371 | struct sr_local_block *srlb = &OspfSR.srlb; | |
372 | mpls_label_t label; | |
373 | uint32_t index; | |
374 | uint32_t pos; | |
37a331f8 | 375 | uint32_t size = srlb->end - srlb->start + 1; |
6f751f14 OD |
376 | |
377 | /* Check if we ran out of available labels */ | |
37a331f8 | 378 | if (srlb->current >= size) |
6f751f14 OD |
379 | return MPLS_INVALID_LABEL; |
380 | ||
381 | /* Get first available label and mark it used */ | |
382 | label = srlb->current + srlb->start; | |
383 | index = srlb->current / SRLB_BLOCK_SIZE; | |
384 | pos = 1ULL << (srlb->current % SRLB_BLOCK_SIZE); | |
385 | srlb->used_mark[index] |= pos; | |
386 | ||
387 | /* Jump to the next free position */ | |
388 | srlb->current++; | |
389 | pos = srlb->current % SRLB_BLOCK_SIZE; | |
37a331f8 | 390 | while (srlb->current < size) { |
6f751f14 OD |
391 | if (pos == 0) |
392 | index++; | |
393 | if (!((1ULL << pos) & srlb->used_mark[index])) | |
394 | break; | |
395 | else { | |
396 | srlb->current++; | |
397 | pos = srlb->current % SRLB_BLOCK_SIZE; | |
398 | } | |
399 | } | |
400 | ||
37a331f8 FR |
401 | if (srlb->current == size) |
402 | zlog_warn( | |
403 | "SR: Warning, SRLB is depleted and next label request will fail"); | |
404 | ||
6f751f14 OD |
405 | return label; |
406 | } | |
407 | ||
408 | /** | |
409 | * Release label in the Segment Routing Local Block. | |
410 | * | |
411 | * @param label Label to be release | |
412 | * | |
413 | * @return 0 on success or -1 if label falls outside SRLB | |
414 | */ | |
415 | int ospf_sr_local_block_release_label(mpls_label_t label) | |
416 | { | |
417 | struct sr_local_block *srlb = &OspfSR.srlb; | |
418 | uint32_t index; | |
419 | uint32_t pos; | |
420 | ||
421 | /* Check that label falls inside the SRLB */ | |
422 | if ((label < srlb->start) || (label > srlb->end)) { | |
423 | flog_warn(EC_OSPF_SR_SID_OVERFLOW, | |
424 | "%s: Returning label %u is outside SRLB [%u/%u]", | |
425 | __func__, label, srlb->start, srlb->end); | |
426 | return -1; | |
427 | } | |
428 | ||
429 | index = (label - srlb->start) / SRLB_BLOCK_SIZE; | |
430 | pos = 1ULL << ((label - srlb->start) % SRLB_BLOCK_SIZE); | |
431 | srlb->used_mark[index] &= ~pos; | |
432 | /* Reset current to the first available position */ | |
433 | for (index = 0; index < srlb->max_block; index++) { | |
434 | if (srlb->used_mark[index] != 0xFFFFFFFFFFFFFFFF) { | |
435 | for (pos = 0; pos < SRLB_BLOCK_SIZE; pos++) | |
436 | if (!((1ULL << pos) & srlb->used_mark[index])) { | |
437 | srlb->current = | |
438 | index * SRLB_BLOCK_SIZE + pos; | |
439 | break; | |
440 | } | |
441 | break; | |
442 | } | |
443 | } | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
cf9b9f77 OD |
448 | /* |
449 | * Segment Routing Initialization functions | |
450 | */ | |
451 | ||
d81b8e0e OD |
452 | /** |
453 | * Thread function to re-attempt connection to the Label Manager and thus be | |
454 | * able to start Segment Routing. | |
455 | * | |
456 | * @param start Thread structure that contains area as argument | |
457 | * | |
458 | * @return 1 on success | |
459 | */ | |
cc9f21da | 460 | static void sr_start_label_manager(struct thread *start) |
d81b8e0e OD |
461 | { |
462 | struct ospf *ospf; | |
463 | ||
464 | ospf = THREAD_ARG(start); | |
465 | ||
466 | /* re-attempt to start SR & Label Manager connection */ | |
467 | ospf_sr_start(ospf); | |
d81b8e0e OD |
468 | } |
469 | ||
cf9b9f77 OD |
470 | /* Segment Routing starter function */ |
471 | static int ospf_sr_start(struct ospf *ospf) | |
472 | { | |
473 | struct route_node *rn; | |
474 | struct ospf_lsa *lsa; | |
475 | struct sr_node *srn; | |
476 | int rc = 0; | |
477 | ||
b37eb79c | 478 | osr_debug("SR (%s): Start Segment Routing", __func__); |
cf9b9f77 | 479 | |
d81b8e0e OD |
480 | /* Initialize self SR Node if not already done */ |
481 | if (OspfSR.self == NULL) { | |
482 | srn = hash_get(OspfSR.neighbors, (void *)&(ospf->router_id), | |
483 | (void *)sr_node_new); | |
484 | ||
485 | /* Complete & Store self SR Node */ | |
6f751f14 OD |
486 | srn->srgb.range_size = OspfSR.srgb.size; |
487 | srn->srgb.lower_bound = OspfSR.srgb.start; | |
488 | srn->srlb.lower_bound = OspfSR.srlb.start; | |
489 | srn->srlb.range_size = OspfSR.srlb.end - OspfSR.srlb.start + 1; | |
d81b8e0e OD |
490 | srn->algo[0] = OspfSR.algo[0]; |
491 | srn->msd = OspfSR.msd; | |
492 | OspfSR.self = srn; | |
493 | } | |
494 | ||
495 | /* Then, start Label Manager if not ready */ | |
496 | if (!ospf_zebra_label_manager_ready()) | |
497 | if (ospf_zebra_label_manager_connect() < 0) { | |
498 | /* Re-attempt to connect to Label Manager in 1 sec. */ | |
499 | thread_add_timer(master, sr_start_label_manager, ospf, | |
500 | 1, &OspfSR.t_start_lm); | |
501 | osr_debug(" |- Failed to start the Label Manager"); | |
502 | return -1; | |
503 | } | |
504 | ||
505 | /* | |
6f751f14 OD |
506 | * Request SRLB & SGRB to the label manager if not already reserved. |
507 | * If the allocation fails, return an error to disable SR until a new | |
508 | * SRLB and/or SRGB are successfully allocated. | |
d81b8e0e | 509 | */ |
daaebd39 FR |
510 | if (sr_local_block_init(OspfSR.srlb.start, OspfSR.srlb.end) < 0) |
511 | return -1; | |
512 | ||
513 | if (sr_global_block_init(OspfSR.srgb.start, OspfSR.srgb.size) < 0) | |
514 | return -1; | |
cf9b9f77 | 515 | |
d81b8e0e OD |
516 | /* SR is UP and ready to flood LSA */ |
517 | OspfSR.status = SR_UP; | |
518 | ||
519 | /* Set Router Information SR parameters */ | |
520 | osr_debug("SR: Activate SR for Router Information LSA"); | |
521 | ||
6f751f14 | 522 | ospf_router_info_update_sr(true, OspfSR.self); |
d81b8e0e OD |
523 | |
524 | /* Update Ext LSA */ | |
525 | osr_debug("SR: Activate SR for Extended Link/Prefix LSA"); | |
526 | ||
527 | ospf_ext_update_sr(true); | |
cf9b9f77 | 528 | |
b37eb79c | 529 | osr_debug("SR (%s): Update SR-DB from LSDB", __func__); |
cf9b9f77 OD |
530 | |
531 | /* Start by looking to Router Info & Extended LSA in lsdb */ | |
532 | if ((ospf != NULL) && (ospf->backbone != NULL)) { | |
996c9314 | 533 | LSDB_LOOP (OPAQUE_AREA_LSDB(ospf->backbone), rn, lsa) { |
cf9b9f77 OD |
534 | if (IS_LSA_MAXAGE(lsa) || IS_LSA_SELF(lsa)) |
535 | continue; | |
536 | int lsa_id = | |
537 | GET_OPAQUE_TYPE(ntohl(lsa->data->id.s_addr)); | |
538 | switch (lsa_id) { | |
539 | case OPAQUE_TYPE_ROUTER_INFORMATION_LSA: | |
540 | ospf_sr_ri_lsa_update(lsa); | |
541 | break; | |
542 | case OPAQUE_TYPE_EXTENDED_PREFIX_LSA: | |
543 | ospf_sr_ext_prefix_lsa_update(lsa); | |
544 | break; | |
545 | case OPAQUE_TYPE_EXTENDED_LINK_LSA: | |
546 | ospf_sr_ext_link_lsa_update(lsa); | |
547 | break; | |
548 | default: | |
549 | break; | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | rc = 1; | |
555 | return rc; | |
556 | } | |
557 | ||
cf9b9f77 OD |
558 | /* Stop Segment Routing */ |
559 | static void ospf_sr_stop(void) | |
560 | { | |
561 | ||
a351b3e4 OD |
562 | if (OspfSR.status == SR_OFF) |
563 | return; | |
564 | ||
b37eb79c | 565 | osr_debug("SR (%s): Stop Segment Routing", __func__); |
cf9b9f77 | 566 | |
d81b8e0e | 567 | /* Disable any re-attempt to connect to Label Manager */ |
50478845 | 568 | THREAD_OFF(OspfSR.t_start_lm); |
d81b8e0e | 569 | |
c181efbe FR |
570 | /* Release SRGB if active */ |
571 | sr_global_block_delete(); | |
572 | ||
573 | /* Release SRLB if active */ | |
6f751f14 OD |
574 | sr_local_block_delete(); |
575 | ||
7743f2f8 OD |
576 | /* |
577 | * Remove all SR Nodes from the Hash table. Prefix and Link SID will | |
6a154c88 | 578 | * be remove though list_delete() call. See sr_node_del() |
7743f2f8 | 579 | */ |
cf9b9f77 | 580 | hash_clean(OspfSR.neighbors, (void *)sr_node_del); |
21baf89a | 581 | OspfSR.self = NULL; |
d81b8e0e | 582 | OspfSR.status = SR_OFF; |
cf9b9f77 OD |
583 | } |
584 | ||
585 | /* | |
586 | * Segment Routing initialize function | |
587 | * | |
588 | * @param - nothing | |
589 | * | |
590 | * @return 0 if OK, -1 otherwise | |
591 | */ | |
592 | int ospf_sr_init(void) | |
593 | { | |
594 | int rc = -1; | |
595 | ||
b37eb79c | 596 | osr_debug("SR (%s): Initialize SR Data Base", __func__); |
cf9b9f77 | 597 | |
6006b807 | 598 | memset(&OspfSR, 0, sizeof(OspfSR)); |
d81b8e0e | 599 | OspfSR.status = SR_OFF; |
cf9b9f77 OD |
600 | /* Only AREA flooding is supported in this release */ |
601 | OspfSR.scope = OSPF_OPAQUE_AREA_LSA; | |
602 | ||
6f751f14 | 603 | /* Initialize Algorithms, SRGB, SRLB and MSD TLVs */ |
cf9b9f77 OD |
604 | /* Only Algorithm SPF is supported */ |
605 | OspfSR.algo[0] = SR_ALGORITHM_SPF; | |
606 | for (int i = 1; i < ALGORITHM_COUNT; i++) | |
607 | OspfSR.algo[i] = SR_ALGORITHM_UNSET; | |
608 | ||
6f751f14 OD |
609 | OspfSR.srgb.size = DEFAULT_SRGB_SIZE; |
610 | OspfSR.srgb.start = DEFAULT_SRGB_LABEL; | |
611 | OspfSR.srgb.reserved = false; | |
612 | ||
613 | OspfSR.srlb.start = DEFAULT_SRLB_LABEL; | |
5bd62f05 | 614 | OspfSR.srlb.end = DEFAULT_SRLB_END; |
6f751f14 | 615 | OspfSR.srlb.reserved = false; |
7743f2f8 | 616 | OspfSR.msd = 0; |
cf9b9f77 OD |
617 | |
618 | /* Initialize Hash table for neighbor SR nodes */ | |
619 | OspfSR.neighbors = hash_create(sr_hash, sr_cmp, "OSPF_SR"); | |
620 | if (OspfSR.neighbors == NULL) | |
621 | return rc; | |
622 | ||
cf9b9f77 OD |
623 | /* Register Segment Routing VTY command */ |
624 | ospf_sr_register_vty(); | |
625 | ||
626 | rc = 0; | |
627 | return rc; | |
628 | } | |
629 | ||
630 | /* | |
631 | * Segment Routing termination function | |
632 | * | |
633 | * @param - nothing | |
cf9b9f77 OD |
634 | * @return - nothing |
635 | */ | |
636 | void ospf_sr_term(void) | |
637 | { | |
638 | ||
639 | /* Stop Segment Routing */ | |
640 | ospf_sr_stop(); | |
641 | ||
642 | /* Clear SR Node Table */ | |
643 | if (OspfSR.neighbors) | |
644 | hash_free(OspfSR.neighbors); | |
645 | ||
bcf4475e OD |
646 | } |
647 | ||
648 | /* | |
649 | * Segment Routing finish function | |
650 | * | |
651 | * @param - nothing | |
652 | * @return - nothing | |
653 | */ | |
654 | void ospf_sr_finish(void) | |
655 | { | |
656 | /* Stop Segment Routing */ | |
657 | ospf_sr_stop(); | |
cf9b9f77 OD |
658 | } |
659 | ||
660 | /* | |
661 | * Following functions are used to manipulate the | |
662 | * Next Hop Label Forwarding entry (NHLFE) | |
663 | */ | |
664 | ||
665 | /* Compute label from index */ | |
6f751f14 | 666 | static mpls_label_t index2label(uint32_t index, struct sr_block srgb) |
cf9b9f77 OD |
667 | { |
668 | mpls_label_t label; | |
669 | ||
670 | label = srgb.lower_bound + index; | |
f786c4d7 OD |
671 | if (label > (srgb.lower_bound + srgb.range_size)) { |
672 | flog_warn(EC_OSPF_SR_SID_OVERFLOW, | |
673 | "%s: SID index %u falls outside SRGB range", | |
674 | __func__, index); | |
cf9b9f77 | 675 | return MPLS_INVALID_LABEL; |
f786c4d7 | 676 | } else |
cf9b9f77 OD |
677 | return label; |
678 | } | |
679 | ||
7fd0729f G |
680 | /* Get the prefix sid for a specific router id */ |
681 | mpls_label_t ospf_sr_get_prefix_sid_by_id(struct in_addr *id) | |
682 | { | |
683 | struct sr_node *srn; | |
684 | struct sr_prefix *srp; | |
685 | mpls_label_t label; | |
686 | ||
687 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, id); | |
688 | ||
689 | if (srn) { | |
690 | /* | |
691 | * TODO: Here we assume that the SRGBs are the same, | |
692 | * and that the node's prefix SID is at the head of | |
693 | * the list, probably needs tweaking. | |
694 | */ | |
695 | srp = listnode_head(srn->ext_prefix); | |
696 | label = index2label(srp->sid, srn->srgb); | |
697 | } else { | |
698 | label = MPLS_INVALID_LABEL; | |
699 | } | |
700 | ||
701 | return label; | |
702 | } | |
703 | ||
cc1725bd G |
704 | /* Get the adjacency sid for a specific 'root' id and 'neighbor' id */ |
705 | mpls_label_t ospf_sr_get_adj_sid_by_id(struct in_addr *root_id, | |
706 | struct in_addr *neighbor_id) | |
707 | { | |
708 | struct sr_node *srn; | |
709 | struct sr_link *srl; | |
710 | mpls_label_t label; | |
711 | struct listnode *node; | |
712 | ||
713 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, root_id); | |
714 | ||
715 | label = MPLS_INVALID_LABEL; | |
716 | ||
717 | if (srn) { | |
718 | for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) { | |
719 | if (srl->type == ADJ_SID | |
720 | && srl->remote_id.s_addr == neighbor_id->s_addr) { | |
721 | label = srl->sid[0]; | |
722 | break; | |
723 | } | |
724 | } | |
725 | } | |
726 | ||
727 | return label; | |
728 | } | |
729 | ||
cf9b9f77 OD |
730 | /* Get neighbor full structure from address */ |
731 | static struct ospf_neighbor *get_neighbor_by_addr(struct ospf *top, | |
732 | struct in_addr addr) | |
733 | { | |
734 | struct ospf_neighbor *nbr; | |
735 | struct ospf_interface *oi; | |
736 | struct listnode *node; | |
737 | struct route_node *rn; | |
738 | ||
739 | /* Sanity Check */ | |
740 | if (top == NULL) | |
741 | return NULL; | |
742 | ||
743 | for (ALL_LIST_ELEMENTS_RO(top->oiflist, node, oi)) | |
7743f2f8 OD |
744 | for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) { |
745 | nbr = rn->info; | |
fe61ceae DS |
746 | if (!nbr) |
747 | continue; | |
748 | ||
749 | if (IPV4_ADDR_SAME(&nbr->address.u.prefix4, &addr) || | |
750 | IPV4_ADDR_SAME(&nbr->router_id, &addr)) { | |
751 | route_unlock_node(rn); | |
752 | return nbr; | |
753 | } | |
7743f2f8 | 754 | } |
cf9b9f77 OD |
755 | return NULL; |
756 | } | |
757 | ||
758 | /* Get OSPF Path from address */ | |
b37eb79c OD |
759 | static struct ospf_route *get_nexthop_by_addr(struct ospf *top, |
760 | struct prefix_ipv4 p) | |
cf9b9f77 | 761 | { |
cf9b9f77 OD |
762 | struct route_node *rn; |
763 | ||
764 | /* Sanity Check */ | |
9b50aa1f | 765 | if (top == NULL) |
cf9b9f77 OD |
766 | return NULL; |
767 | ||
b37eb79c OD |
768 | osr_debug(" |- Search Nexthop for prefix %pFX", |
769 | (struct prefix *)&p); | |
cf9b9f77 OD |
770 | |
771 | rn = route_node_lookup(top->new_table, (struct prefix *)&p); | |
772 | ||
7743f2f8 OD |
773 | /* |
774 | * Check if we found an OSPF route. May be NULL if SPF has not | |
775 | * yet populate routing table for this prefix. | |
776 | */ | |
cf9b9f77 OD |
777 | if (rn == NULL) |
778 | return NULL; | |
779 | ||
780 | route_unlock_node(rn); | |
b37eb79c | 781 | return rn->info; |
cf9b9f77 OD |
782 | } |
783 | ||
784 | /* Compute NHLFE entry for Extended Link */ | |
785 | static int compute_link_nhlfe(struct sr_link *srl) | |
786 | { | |
787 | struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT); | |
788 | struct ospf_neighbor *nh; | |
789 | int rc = 0; | |
790 | ||
b37eb79c | 791 | osr_debug(" |- Compute NHLFE for link %pI4", &srl->itf_addr); |
cf9b9f77 OD |
792 | |
793 | /* First determine the OSPF Neighbor */ | |
794 | nh = get_neighbor_by_addr(top, srl->nhlfe[0].nexthop); | |
795 | ||
796 | /* Neighbor could be not found when OSPF Adjacency just fire up | |
797 | * because SPF don't yet populate routing table. This NHLFE will | |
798 | * be fixed later when SR SPF schedule will be called. | |
799 | */ | |
800 | if (nh == NULL) | |
801 | return rc; | |
802 | ||
b37eb79c | 803 | osr_debug(" |- Found nexthop %pI4", &nh->router_id); |
cf9b9f77 OD |
804 | |
805 | /* Set ifindex for this neighbor */ | |
806 | srl->nhlfe[0].ifindex = nh->oi->ifp->ifindex; | |
807 | srl->nhlfe[1].ifindex = nh->oi->ifp->ifindex; | |
808 | ||
d922605d OD |
809 | /* Update neighbor address for LAN_ADJ_SID */ |
810 | if (srl->type == LAN_ADJ_SID) { | |
811 | IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &nh->src); | |
812 | IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &nh->src); | |
813 | } | |
814 | ||
cf9b9f77 OD |
815 | /* Set Input & Output Label */ |
816 | if (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
817 | srl->nhlfe[0].label_in = srl->sid[0]; | |
818 | else | |
819 | srl->nhlfe[0].label_in = | |
820 | index2label(srl->sid[0], srl->srn->srgb); | |
821 | if (CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
822 | srl->nhlfe[1].label_in = srl->sid[1]; | |
823 | else | |
824 | srl->nhlfe[1].label_in = | |
825 | index2label(srl->sid[1], srl->srn->srgb); | |
826 | ||
70e98a7f DS |
827 | srl->nhlfe[0].label_out = MPLS_LABEL_IMPLICIT_NULL; |
828 | srl->nhlfe[1].label_out = MPLS_LABEL_IMPLICIT_NULL; | |
cf9b9f77 OD |
829 | |
830 | rc = 1; | |
831 | return rc; | |
832 | } | |
833 | ||
f786c4d7 OD |
834 | /** |
835 | * Compute output label for the given Prefix-SID. | |
836 | * | |
837 | * @param srp Segment Routing Prefix | |
838 | * @param srnext Segment Routing nexthop node | |
839 | * | |
840 | * @return MPLS label or MPLS_INVALID_LABEL in case of error | |
841 | */ | |
842 | static mpls_label_t sr_prefix_out_label(const struct sr_prefix *srp, | |
843 | const struct sr_node *srnext) | |
844 | { | |
845 | /* Check if the nexthop SR Node is the last hop? */ | |
846 | if (srnext == srp->srn) { | |
847 | /* SR-Node doesn't request NO-PHP. Return Implicit NULL label */ | |
848 | if (!CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)) | |
849 | return MPLS_LABEL_IMPLICIT_NULL; | |
850 | ||
851 | /* SR-Node requests Explicit NULL Label */ | |
852 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG)) | |
853 | return MPLS_LABEL_IPV4_EXPLICIT_NULL; | |
854 | /* Fallthrough */ | |
855 | } | |
856 | ||
857 | /* Return SID value as MPLS label if it is an Absolute SID */ | |
858 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG | |
859 | | EXT_SUBTLV_PREFIX_SID_LFLG)) { | |
860 | /* | |
861 | * V/L SIDs have local significance, so only adjacent routers | |
862 | * can use them (RFC8665 section #5) | |
863 | */ | |
864 | if (srp->srn != srnext) | |
865 | return MPLS_INVALID_LABEL; | |
866 | return srp->sid; | |
867 | } | |
868 | ||
869 | /* Return MPLS label as SRGB lower bound + SID index as per RFC 8665 */ | |
870 | return (index2label(srp->sid, srnext->srgb)); | |
871 | } | |
872 | ||
cf9b9f77 OD |
873 | /* |
874 | * Compute NHLFE entry for Extended Prefix | |
875 | * | |
876 | * @param srp - Segment Routing Prefix | |
877 | * | |
b37eb79c OD |
878 | * @return -1 if no route is found, 0 if there is no SR route ready |
879 | * and 1 if success or update | |
cf9b9f77 OD |
880 | */ |
881 | static int compute_prefix_nhlfe(struct sr_prefix *srp) | |
882 | { | |
883 | struct ospf *top = ospf_lookup_by_vrf_id(VRF_DEFAULT); | |
b37eb79c OD |
884 | struct ospf_path *path; |
885 | struct listnode *node; | |
cf9b9f77 OD |
886 | struct sr_node *srnext; |
887 | int rc = -1; | |
888 | ||
b37eb79c OD |
889 | osr_debug(" |- Compute NHLFE for prefix %pFX", |
890 | (struct prefix *)&srp->prefv4); | |
891 | ||
cf9b9f77 OD |
892 | |
893 | /* First determine the nexthop */ | |
b61264a8 | 894 | srp->route = get_nexthop_by_addr(top, srp->prefv4); |
cf9b9f77 OD |
895 | |
896 | /* Nexthop could be not found when OSPF Adjacency just fire up | |
897 | * because SPF don't yet populate routing table. This NHLFE will | |
898 | * be fixed later when SR SPF schedule will be called. | |
899 | */ | |
b61264a8 | 900 | if (srp->route == NULL) |
cf9b9f77 OD |
901 | return rc; |
902 | ||
b37eb79c | 903 | /* Compute Input Label with self SRGB */ |
6f751f14 | 904 | srp->label_in = index2label(srp->sid, OspfSR.self->srgb); |
cf9b9f77 | 905 | |
b37eb79c | 906 | rc = 0; |
b61264a8 | 907 | for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) { |
7743f2f8 | 908 | |
b37eb79c OD |
909 | osr_debug(" |- Process new route via %pI4 for this prefix", |
910 | &path->nexthop); | |
cf9b9f77 | 911 | |
b37eb79c OD |
912 | /* |
913 | * Get SR-Node for this nexthop. Could be not yet available | |
914 | * as Extended Link / Prefix and Router Information are flooded | |
915 | * after LSA Type 1 & 2 which populate the OSPF Route Table | |
916 | */ | |
917 | srnext = get_sr_node_by_nexthop(top, path->nexthop); | |
918 | if (srnext == NULL) | |
919 | continue; | |
cf9b9f77 | 920 | |
b37eb79c OD |
921 | /* And store this information for later update */ |
922 | srnext->neighbor = OspfSR.self; | |
f786c4d7 | 923 | path->srni.nexthop = srnext; |
cf9b9f77 | 924 | |
b37eb79c OD |
925 | /* |
926 | * SR Node could be known, but SRGB could be not initialize | |
927 | * This is due to the fact that Extended Link / Prefix could | |
928 | * be received before corresponding Router Information LSA | |
929 | */ | |
b61264a8 | 930 | if (srnext == NULL || srnext->srgb.lower_bound == 0 |
56981b40 OD |
931 | || srnext->srgb.range_size == 0) { |
932 | osr_debug( | |
933 | " |- SR-Node %pI4 not ready. Stop process", | |
934 | &srnext->adv_router); | |
935 | path->srni.label_out = MPLS_INVALID_LABEL; | |
b37eb79c | 936 | continue; |
56981b40 | 937 | } |
cf9b9f77 | 938 | |
b37eb79c OD |
939 | osr_debug(" |- Found SRGB %u/%u for next hop SR-Node %pI4", |
940 | srnext->srgb.range_size, srnext->srgb.lower_bound, | |
941 | &srnext->adv_router); | |
cf9b9f77 | 942 | |
f786c4d7 OD |
943 | /* Compute Output Label with Nexthop SR Node SRGB */ |
944 | path->srni.label_out = sr_prefix_out_label(srp, srnext); | |
cf9b9f77 | 945 | |
b37eb79c OD |
946 | osr_debug(" |- Computed new labels in: %u out: %u", |
947 | srp->label_in, path->srni.label_out); | |
948 | rc = 1; | |
949 | } | |
cf9b9f77 OD |
950 | return rc; |
951 | } | |
952 | ||
b37eb79c OD |
953 | /* Add new NHLFE entry for Adjacency SID */ |
954 | static inline void add_adj_sid(struct sr_nhlfe nhlfe) | |
cf9b9f77 | 955 | { |
b37eb79c | 956 | if (nhlfe.label_in != 0) |
43e7abb5 | 957 | ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_ADD, nhlfe); |
cf9b9f77 OD |
958 | } |
959 | ||
b37eb79c OD |
960 | /* Remove NHLFE entry for Adjacency SID */ |
961 | static inline void del_adj_sid(struct sr_nhlfe nhlfe) | |
cf9b9f77 | 962 | { |
b37eb79c | 963 | if (nhlfe.label_in != 0) |
43e7abb5 | 964 | ospf_zebra_send_adjacency_sid(ZEBRA_MPLS_LABELS_DELETE, nhlfe); |
cf9b9f77 OD |
965 | } |
966 | ||
b37eb79c OD |
967 | /* Update NHLFE entry for Adjacency SID */ |
968 | static inline void update_adj_sid(struct sr_nhlfe n1, struct sr_nhlfe n2) | |
cf9b9f77 | 969 | { |
b37eb79c OD |
970 | del_adj_sid(n1); |
971 | add_adj_sid(n2); | |
972 | } | |
973 | ||
cf9b9f77 OD |
974 | /* |
975 | * Functions to parse and get Extended Link / Prefix | |
976 | * TLVs and SubTLVs | |
977 | */ | |
978 | ||
979 | /* Extended Link SubTLVs Getter */ | |
8db278b5 | 980 | static struct sr_link *get_ext_link_sid(struct tlv_header *tlvh, size_t size) |
cf9b9f77 OD |
981 | { |
982 | ||
983 | struct sr_link *srl; | |
984 | struct ext_tlv_link *link = (struct ext_tlv_link *)tlvh; | |
985 | struct ext_subtlv_adj_sid *adj_sid; | |
986 | struct ext_subtlv_lan_adj_sid *lan_sid; | |
987 | struct ext_subtlv_rmt_itf_addr *rmt_itf; | |
988 | ||
989 | struct tlv_header *sub_tlvh; | |
93f0a26e | 990 | uint16_t length = 0, sum = 0, i = 0; |
cf9b9f77 | 991 | |
8db278b5 OD |
992 | /* Check TLV size */ |
993 | if ((ntohs(tlvh->length) > size) | |
994 | || ntohs(tlvh->length) < EXT_TLV_LINK_SIZE) { | |
995 | zlog_warn("Wrong Extended Link TLV size. Abort!"); | |
996 | return NULL; | |
997 | } | |
998 | ||
cf9b9f77 OD |
999 | srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link)); |
1000 | ||
cf9b9f77 OD |
1001 | /* Initialize TLV browsing */ |
1002 | length = ntohs(tlvh->length) - EXT_TLV_LINK_SIZE; | |
1003 | sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE | |
1004 | + EXT_TLV_LINK_SIZE); | |
8db278b5 | 1005 | for (; sum < length && sub_tlvh; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) { |
cf9b9f77 OD |
1006 | switch (ntohs(sub_tlvh->type)) { |
1007 | case EXT_SUBTLV_ADJ_SID: | |
1008 | adj_sid = (struct ext_subtlv_adj_sid *)sub_tlvh; | |
1009 | srl->type = ADJ_SID; | |
1010 | i = CHECK_FLAG(adj_sid->flags, | |
1011 | EXT_SUBTLV_LINK_ADJ_SID_BFLG) | |
1012 | ? 1 | |
1013 | : 0; | |
1014 | srl->flags[i] = adj_sid->flags; | |
1015 | if (CHECK_FLAG(adj_sid->flags, | |
1016 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1017 | srl->sid[i] = GET_LABEL(ntohl(adj_sid->value)); | |
1018 | else | |
1019 | srl->sid[i] = ntohl(adj_sid->value); | |
1020 | IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop, &link->link_id); | |
1021 | break; | |
1022 | case EXT_SUBTLV_LAN_ADJ_SID: | |
1023 | lan_sid = (struct ext_subtlv_lan_adj_sid *)sub_tlvh; | |
1024 | srl->type = LAN_ADJ_SID; | |
1025 | i = CHECK_FLAG(lan_sid->flags, | |
1026 | EXT_SUBTLV_LINK_ADJ_SID_BFLG) | |
1027 | ? 1 | |
1028 | : 0; | |
1029 | srl->flags[i] = lan_sid->flags; | |
1030 | if (CHECK_FLAG(lan_sid->flags, | |
1031 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1032 | srl->sid[i] = GET_LABEL(ntohl(lan_sid->value)); | |
1033 | else | |
1034 | srl->sid[i] = ntohl(lan_sid->value); | |
1035 | IPV4_ADDR_COPY(&srl->nhlfe[i].nexthop, | |
1036 | &lan_sid->neighbor_id); | |
1037 | break; | |
1038 | case EXT_SUBTLV_RMT_ITF_ADDR: | |
1039 | rmt_itf = (struct ext_subtlv_rmt_itf_addr *)sub_tlvh; | |
1040 | IPV4_ADDR_COPY(&srl->nhlfe[0].nexthop, &rmt_itf->value); | |
1041 | IPV4_ADDR_COPY(&srl->nhlfe[1].nexthop, &rmt_itf->value); | |
1042 | break; | |
1043 | default: | |
1044 | break; | |
1045 | } | |
1046 | sum += TLV_SIZE(sub_tlvh); | |
1047 | } | |
1048 | ||
b37eb79c | 1049 | IPV4_ADDR_COPY(&srl->itf_addr, &link->link_data); |
cf9b9f77 | 1050 | |
b37eb79c OD |
1051 | osr_debug(" |- Found primary %u and backup %u Adj/Lan Sid for %pI4", |
1052 | srl->sid[0], srl->sid[1], &srl->itf_addr); | |
cf9b9f77 OD |
1053 | |
1054 | return srl; | |
1055 | } | |
1056 | ||
1057 | /* Extended Prefix SubTLVs Getter */ | |
8db278b5 OD |
1058 | static struct sr_prefix *get_ext_prefix_sid(struct tlv_header *tlvh, |
1059 | size_t size) | |
cf9b9f77 OD |
1060 | { |
1061 | ||
1062 | struct sr_prefix *srp; | |
1063 | struct ext_tlv_prefix *pref = (struct ext_tlv_prefix *)tlvh; | |
1064 | struct ext_subtlv_prefix_sid *psid; | |
1065 | ||
1066 | struct tlv_header *sub_tlvh; | |
93f0a26e | 1067 | uint16_t length = 0, sum = 0; |
cf9b9f77 | 1068 | |
8db278b5 OD |
1069 | /* Check TLV size */ |
1070 | if ((ntohs(tlvh->length) > size) | |
1071 | || ntohs(tlvh->length) < EXT_TLV_PREFIX_SIZE) { | |
1072 | zlog_warn("Wrong Extended Link TLV size. Abort!"); | |
1073 | return NULL; | |
1074 | } | |
1075 | ||
cf9b9f77 OD |
1076 | srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix)); |
1077 | ||
cf9b9f77 OD |
1078 | /* Initialize TLV browsing */ |
1079 | length = ntohs(tlvh->length) - EXT_TLV_PREFIX_SIZE; | |
1080 | sub_tlvh = (struct tlv_header *)((char *)(tlvh) + TLV_HDR_SIZE | |
1081 | + EXT_TLV_PREFIX_SIZE); | |
8db278b5 | 1082 | for (; sum < length && sub_tlvh; sub_tlvh = TLV_HDR_NEXT(sub_tlvh)) { |
cf9b9f77 OD |
1083 | switch (ntohs(sub_tlvh->type)) { |
1084 | case EXT_SUBTLV_PREFIX_SID: | |
1085 | psid = (struct ext_subtlv_prefix_sid *)sub_tlvh; | |
1086 | if (psid->algorithm != SR_ALGORITHM_SPF) { | |
cf444bcf | 1087 | flog_err(EC_OSPF_INVALID_ALGORITHM, |
ade6974d QY |
1088 | "SR (%s): Unsupported Algorithm", |
1089 | __func__); | |
cf9b9f77 OD |
1090 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); |
1091 | return NULL; | |
1092 | } | |
1093 | srp->type = PREF_SID; | |
1094 | srp->flags = psid->flags; | |
1095 | if (CHECK_FLAG(psid->flags, EXT_SUBTLV_PREFIX_SID_VFLG)) | |
1096 | srp->sid = GET_LABEL(ntohl(psid->value)); | |
1097 | else | |
1098 | srp->sid = ntohl(psid->value); | |
b61264a8 | 1099 | IPV4_ADDR_COPY(&srp->prefv4.prefix, &pref->address); |
b37eb79c OD |
1100 | srp->prefv4.prefixlen = pref->pref_length; |
1101 | srp->prefv4.family = AF_INET; | |
1102 | apply_mask_ipv4(&srp->prefv4); | |
cf9b9f77 OD |
1103 | break; |
1104 | default: | |
1105 | break; | |
1106 | } | |
1107 | sum += TLV_SIZE(sub_tlvh); | |
1108 | } | |
1109 | ||
b37eb79c OD |
1110 | osr_debug(" |- Found SID %u for prefix %pFX", srp->sid, |
1111 | (struct prefix *)&srp->prefv4); | |
1112 | ||
cf9b9f77 OD |
1113 | return srp; |
1114 | } | |
1115 | ||
1116 | /* | |
1117 | * Functions to manipulate Segment Routing Link & Prefix structures | |
1118 | */ | |
1119 | ||
1120 | /* Compare two Segment Link: return 0 if equal, 1 otherwise */ | |
1121 | static inline int sr_link_cmp(struct sr_link *srl1, struct sr_link *srl2) | |
1122 | { | |
1123 | if ((srl1->sid[0] == srl2->sid[0]) && (srl1->sid[1] == srl2->sid[1]) | |
1124 | && (srl1->type == srl2->type) && (srl1->flags[0] == srl2->flags[0]) | |
1125 | && (srl1->flags[1] == srl2->flags[1])) | |
1126 | return 0; | |
1127 | else | |
1128 | return 1; | |
1129 | } | |
1130 | ||
1131 | /* Compare two Segment Prefix: return 0 if equal, 1 otherwise */ | |
1132 | static inline int sr_prefix_cmp(struct sr_prefix *srp1, struct sr_prefix *srp2) | |
1133 | { | |
1134 | if ((srp1->sid == srp2->sid) && (srp1->flags == srp2->flags)) | |
1135 | return 0; | |
1136 | else | |
1137 | return 1; | |
1138 | } | |
1139 | ||
1140 | /* Update Segment Link of given Segment Routing Node */ | |
1141 | static void update_ext_link_sid(struct sr_node *srn, struct sr_link *srl, | |
d7c0a89a | 1142 | uint8_t lsa_flags) |
cf9b9f77 OD |
1143 | { |
1144 | struct listnode *node; | |
1145 | struct sr_link *lk; | |
1146 | bool found = false; | |
edb368e9 | 1147 | bool config = true; |
cf9b9f77 OD |
1148 | |
1149 | /* Sanity check */ | |
1150 | if ((srn == NULL) || (srl == NULL)) | |
1151 | return; | |
1152 | ||
b37eb79c | 1153 | osr_debug(" |- Process Extended Link Adj/Lan-SID"); |
cf9b9f77 | 1154 | |
edb368e9 | 1155 | /* Detect if Adj/Lan_Adj SID must be configured */ |
b61264a8 OD |
1156 | if (!CHECK_FLAG(lsa_flags, OSPF_LSA_SELF) |
1157 | && (CHECK_FLAG(srl->flags[0], EXT_SUBTLV_LINK_ADJ_SID_LFLG) | |
1158 | || CHECK_FLAG(srl->flags[1], EXT_SUBTLV_LINK_ADJ_SID_LFLG))) | |
edb368e9 | 1159 | config = false; |
cf9b9f77 OD |
1160 | |
1161 | /* Search for existing Segment Link */ | |
1162 | for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, lk)) | |
1163 | if (lk->instance == srl->instance) { | |
1164 | found = true; | |
1165 | break; | |
1166 | } | |
1167 | ||
b37eb79c OD |
1168 | osr_debug(" |- %s SR Link 8.0.0.%u for SR node %pI4", |
1169 | found ? "Update" : "Add", GET_OPAQUE_ID(srl->instance), | |
1170 | &srn->adv_router); | |
cf9b9f77 OD |
1171 | |
1172 | /* if not found, add new Segment Link and install NHLFE */ | |
1173 | if (!found) { | |
1174 | /* Complete SR-Link and add it to SR-Node list */ | |
1175 | srl->srn = srn; | |
1176 | IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router); | |
1177 | listnode_add(srn->ext_link, srl); | |
1178 | /* Try to set MPLS table */ | |
edb368e9 | 1179 | if (config && compute_link_nhlfe(srl)) { |
b37eb79c OD |
1180 | add_adj_sid(srl->nhlfe[0]); |
1181 | add_adj_sid(srl->nhlfe[1]); | |
cf9b9f77 OD |
1182 | } |
1183 | } else { | |
edb368e9 | 1184 | /* Update SR-Link if they are different */ |
cf9b9f77 | 1185 | if (sr_link_cmp(lk, srl)) { |
edb368e9 OD |
1186 | /* Try to set MPLS table */ |
1187 | if (config) { | |
1188 | if (compute_link_nhlfe(srl)) { | |
1189 | update_adj_sid(lk->nhlfe[0], | |
1190 | srl->nhlfe[0]); | |
1191 | update_adj_sid(lk->nhlfe[1], | |
1192 | srl->nhlfe[1]); | |
1193 | } else { | |
1194 | del_adj_sid(lk->nhlfe[0]); | |
1195 | del_adj_sid(lk->nhlfe[1]); | |
1196 | } | |
cf9b9f77 | 1197 | } |
edb368e9 OD |
1198 | /* Replace SR-Link in SR-Node Adjacency List */ |
1199 | listnode_delete(srn->ext_link, lk); | |
1200 | XFREE(MTYPE_OSPF_SR_PARAMS, lk); | |
1201 | srl->srn = srn; | |
1202 | IPV4_ADDR_COPY(&srl->adv_router, &srn->adv_router); | |
1203 | listnode_add(srn->ext_link, srl); | |
cf9b9f77 | 1204 | } else { |
7743f2f8 OD |
1205 | /* |
1206 | * This is just an LSA refresh. | |
1207 | * Stop processing and free SR Link | |
1208 | */ | |
cf9b9f77 OD |
1209 | XFREE(MTYPE_OSPF_SR_PARAMS, srl); |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | /* Update Segment Prefix of given Segment Routing Node */ | |
1215 | static void update_ext_prefix_sid(struct sr_node *srn, struct sr_prefix *srp) | |
1216 | { | |
1217 | ||
1218 | struct listnode *node; | |
1219 | struct sr_prefix *pref; | |
1220 | bool found = false; | |
1221 | ||
1222 | /* Sanity check */ | |
1223 | if (srn == NULL || srp == NULL) | |
1224 | return; | |
1225 | ||
b37eb79c | 1226 | osr_debug(" |- Process Extended Prefix SID %u", srp->sid); |
cf9b9f77 OD |
1227 | |
1228 | /* Process only Global Prefix SID */ | |
1229 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_LFLG)) | |
1230 | return; | |
1231 | ||
1232 | /* Search for existing Segment Prefix */ | |
1233 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, pref)) | |
d97e415d FR |
1234 | if (pref->instance == srp->instance |
1235 | && prefix_same((struct prefix *)&srp->prefv4, | |
1236 | &pref->prefv4)) { | |
cf9b9f77 OD |
1237 | found = true; |
1238 | break; | |
1239 | } | |
1240 | ||
b37eb79c OD |
1241 | osr_debug(" |- %s SR LSA ID 7.0.0.%u for SR node %pI4", |
1242 | found ? "Update" : "Add", GET_OPAQUE_ID(srp->instance), | |
1243 | &srn->adv_router); | |
cf9b9f77 | 1244 | |
270e66a2 OD |
1245 | /* Complete SR-Prefix */ |
1246 | srp->srn = srn; | |
1247 | IPV4_ADDR_COPY(&srp->adv_router, &srn->adv_router); | |
1248 | ||
cf9b9f77 OD |
1249 | /* if not found, add new Segment Prefix and install NHLFE */ |
1250 | if (!found) { | |
270e66a2 | 1251 | /* Add it to SR-Node list ... */ |
cf9b9f77 | 1252 | listnode_add(srn->ext_prefix, srp); |
270e66a2 | 1253 | /* ... and try to set MPLS table */ |
7743f2f8 | 1254 | if (compute_prefix_nhlfe(srp) == 1) |
43e7abb5 | 1255 | ospf_zebra_update_prefix_sid(srp); |
cf9b9f77 | 1256 | } else { |
270e66a2 OD |
1257 | /* |
1258 | * An old SR prefix exist. Check if something changes or if it | |
1259 | * is just a refresh. | |
1260 | */ | |
cf9b9f77 OD |
1261 | if (sr_prefix_cmp(pref, srp)) { |
1262 | if (compute_prefix_nhlfe(srp) == 1) { | |
43e7abb5 | 1263 | ospf_zebra_delete_prefix_sid(pref); |
cf9b9f77 OD |
1264 | /* Replace Segment Prefix */ |
1265 | listnode_delete(srn->ext_prefix, pref); | |
1266 | XFREE(MTYPE_OSPF_SR_PARAMS, pref); | |
cf9b9f77 | 1267 | listnode_add(srn->ext_prefix, srp); |
43e7abb5 | 1268 | ospf_zebra_update_prefix_sid(srp); |
cf9b9f77 OD |
1269 | } else { |
1270 | /* New NHLFE was not found. | |
7743f2f8 OD |
1271 | * Just free the SR Prefix |
1272 | */ | |
cf9b9f77 OD |
1273 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); |
1274 | } | |
1275 | } else { | |
1276 | /* This is just an LSA refresh. | |
7743f2f8 OD |
1277 | * Stop processing and free SR Prefix |
1278 | */ | |
cf9b9f77 OD |
1279 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); |
1280 | } | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /* | |
1285 | * When change the FRR Self SRGB, update the NHLFE Input Label | |
1286 | * for all Extended Prefix with SID index through hash_iterate() | |
1287 | */ | |
e3b78da8 | 1288 | static void update_in_nhlfe(struct hash_bucket *bucket, void *args) |
cf9b9f77 OD |
1289 | { |
1290 | struct listnode *node; | |
e3b78da8 | 1291 | struct sr_node *srn = (struct sr_node *)bucket->data; |
cf9b9f77 | 1292 | struct sr_prefix *srp; |
cf9b9f77 | 1293 | |
cf9b9f77 OD |
1294 | /* Process Every Extended Prefix for this SR-Node */ |
1295 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) { | |
7743f2f8 OD |
1296 | /* Process Self SRN only if NO-PHP is requested */ |
1297 | if ((srn == OspfSR.self) | |
1298 | && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG)) | |
1299 | continue; | |
1300 | ||
cf9b9f77 OD |
1301 | /* Process only SID Index */ |
1302 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_VFLG)) | |
1303 | continue; | |
7743f2f8 | 1304 | |
6f751f14 OD |
1305 | /* First, remove old MPLS table entries ... */ |
1306 | ospf_zebra_delete_prefix_sid(srp); | |
1307 | /* ... then compute new input label ... */ | |
1308 | srp->label_in = index2label(srp->sid, OspfSR.self->srgb); | |
1309 | /* ... and install new MPLS LFIB */ | |
43e7abb5 | 1310 | ospf_zebra_update_prefix_sid(srp); |
cf9b9f77 OD |
1311 | } |
1312 | } | |
1313 | ||
1314 | /* | |
1315 | * When SRGB has changed, update NHLFE Output Label for all Extended Prefix | |
56981b40 | 1316 | * with SID index which use the given SR-Node as nexthop through hash_iterate() |
cf9b9f77 | 1317 | */ |
e3b78da8 | 1318 | static void update_out_nhlfe(struct hash_bucket *bucket, void *args) |
cf9b9f77 | 1319 | { |
b37eb79c | 1320 | struct listnode *node, *pnode; |
e3b78da8 | 1321 | struct sr_node *srn = (struct sr_node *)bucket->data; |
cf9b9f77 OD |
1322 | struct sr_node *srnext = (struct sr_node *)args; |
1323 | struct sr_prefix *srp; | |
b37eb79c | 1324 | struct ospf_path *path; |
cf9b9f77 | 1325 | |
56981b40 OD |
1326 | /* Skip Self SR-Node */ |
1327 | if (srn == OspfSR.self) | |
1328 | return; | |
1329 | ||
1330 | osr_debug("SR (%s): Update Out NHLFE for neighbor SR-Node %pI4", | |
1331 | __func__, &srn->adv_router); | |
1332 | ||
cf9b9f77 | 1333 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) { |
56981b40 | 1334 | /* Skip Prefix that has not yet a valid route */ |
b61264a8 | 1335 | if (srp->route == NULL) |
cf9b9f77 | 1336 | continue; |
b37eb79c | 1337 | |
b61264a8 | 1338 | for (ALL_LIST_ELEMENTS_RO(srp->route->paths, pnode, path)) { |
56981b40 OD |
1339 | /* Skip path that has not next SR-Node as nexthop */ |
1340 | if (path->srni.nexthop != srnext) | |
b37eb79c | 1341 | continue; |
56981b40 OD |
1342 | |
1343 | /* Compute new Output Label */ | |
1344 | path->srni.label_out = sr_prefix_out_label(srp, srnext); | |
b37eb79c | 1345 | } |
56981b40 OD |
1346 | |
1347 | /* Finally update MPLS table */ | |
1348 | ospf_zebra_update_prefix_sid(srp); | |
cf9b9f77 OD |
1349 | } |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Following functions are call when new Segment Routing LSA are received | |
1354 | * - Router Information: ospf_sr_ri_lsa_update() & ospf_sr_ri_lsa_delete() | |
1355 | * - Extended Link: ospf_sr_ext_link_update() & ospf_sr_ext_link_delete() | |
1356 | * - Extended Prefix: ospf_ext_prefix_update() & ospf_sr_ext_prefix_delete() | |
1357 | */ | |
1358 | ||
1359 | /* Update Segment Routing from Router Information LSA */ | |
1360 | void ospf_sr_ri_lsa_update(struct ospf_lsa *lsa) | |
1361 | { | |
1362 | struct sr_node *srn; | |
1363 | struct tlv_header *tlvh; | |
c4efd0f4 | 1364 | struct lsa_header *lsah = lsa->data; |
b61264a8 | 1365 | struct ri_sr_tlv_sid_label_range *ri_srgb = NULL; |
6f751f14 | 1366 | struct ri_sr_tlv_sid_label_range *ri_srlb = NULL; |
b61264a8 | 1367 | struct ri_sr_tlv_sr_algorithm *algo = NULL; |
6f751f14 | 1368 | struct sr_block srgb; |
93f0a26e | 1369 | uint16_t length = 0, sum = 0; |
b61264a8 | 1370 | uint8_t msd = 0; |
cf9b9f77 | 1371 | |
b37eb79c OD |
1372 | osr_debug("SR (%s): Process Router Information LSA 4.0.0.%u from %pI4", |
1373 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1374 | &lsah->adv_router); | |
cf9b9f77 OD |
1375 | |
1376 | /* Sanity check */ | |
1377 | if (IS_LSA_SELF(lsa)) | |
1378 | return; | |
1379 | ||
1380 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1381 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1382 | "SR (%s): Abort! no valid SR DataBase", __func__); |
cf9b9f77 OD |
1383 | return; |
1384 | } | |
1385 | ||
b61264a8 OD |
1386 | /* Search SR Node in hash table from Router ID */ |
1387 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, | |
1388 | &lsah->adv_router); | |
cf9b9f77 | 1389 | |
cf9b9f77 OD |
1390 | |
1391 | /* Collect Router Information Sub TLVs */ | |
1392 | /* Initialize TLV browsing */ | |
8db278b5 | 1393 | length = lsa->size - OSPF_LSA_HEADER_SIZE; |
cf9b9f77 OD |
1394 | srgb.range_size = 0; |
1395 | srgb.lower_bound = 0; | |
1396 | ||
6aaf0fdd | 1397 | for (tlvh = TLV_HDR_TOP(lsah); (sum < length) && (tlvh != NULL); |
cf9b9f77 OD |
1398 | tlvh = TLV_HDR_NEXT(tlvh)) { |
1399 | switch (ntohs(tlvh->type)) { | |
1400 | case RI_SR_TLV_SR_ALGORITHM: | |
1401 | algo = (struct ri_sr_tlv_sr_algorithm *)tlvh; | |
cf9b9f77 | 1402 | break; |
6f751f14 | 1403 | case RI_SR_TLV_SRGB_LABEL_RANGE: |
cf9b9f77 | 1404 | ri_srgb = (struct ri_sr_tlv_sid_label_range *)tlvh; |
cf9b9f77 | 1405 | break; |
6f751f14 OD |
1406 | case RI_SR_TLV_SRLB_LABEL_RANGE: |
1407 | ri_srlb = (struct ri_sr_tlv_sid_label_range *)tlvh; | |
6f751f14 | 1408 | break; |
cf9b9f77 | 1409 | case RI_SR_TLV_NODE_MSD: |
b61264a8 | 1410 | msd = ((struct ri_sr_tlv_node_msd *)(tlvh))->value; |
cf9b9f77 OD |
1411 | break; |
1412 | default: | |
cf9b9f77 OD |
1413 | break; |
1414 | } | |
8db278b5 | 1415 | sum += TLV_SIZE(tlvh); |
cf9b9f77 OD |
1416 | } |
1417 | ||
b61264a8 OD |
1418 | /* Check if Segment Routing Capabilities has been found */ |
1419 | if (ri_srgb == NULL) { | |
1420 | /* Skip Router Information without SR capabilities | |
1421 | * advertise by a non SR Node */ | |
1422 | if (srn == NULL) { | |
1423 | return; | |
1424 | } else { | |
1425 | /* Remove SR Node that advertise Router Information | |
1426 | * without SR capabilities. This could correspond to a | |
1427 | * Node stopping Segment Routing */ | |
1428 | hash_release(OspfSR.neighbors, &(srn->adv_router)); | |
1429 | sr_node_del(srn); | |
1430 | return; | |
1431 | } | |
1432 | } | |
1433 | ||
1434 | /* Check that RI LSA belongs to the correct SR Node */ | |
1435 | if ((srn != NULL) && (srn->instance != 0) | |
1436 | && (srn->instance != ntohl(lsah->id.s_addr))) { | |
1437 | flog_err(EC_OSPF_SR_INVALID_LSA_ID, | |
3efd0893 | 1438 | "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4/%u", |
b61264a8 OD |
1439 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), |
1440 | &lsah->adv_router, srn->instance); | |
cf9b9f77 OD |
1441 | return; |
1442 | } | |
1443 | ||
b61264a8 OD |
1444 | /* OK. All things look good. Get SRGB */ |
1445 | srgb.range_size = GET_RANGE_SIZE(ntohl(ri_srgb->size)); | |
1446 | srgb.lower_bound = GET_LABEL(ntohl(ri_srgb->lower.value)); | |
1447 | ||
cf9b9f77 | 1448 | /* Check if it is a new SR Node or not */ |
b61264a8 OD |
1449 | if (srn == NULL) { |
1450 | /* Get a new SR Node in hash table from Router ID */ | |
1451 | srn = (struct sr_node *)hash_get(OspfSR.neighbors, | |
1452 | &lsah->adv_router, | |
1453 | (void *)sr_node_new); | |
cf9b9f77 OD |
1454 | /* update LSA ID */ |
1455 | srn->instance = ntohl(lsah->id.s_addr); | |
6f751f14 OD |
1456 | /* Copy SRGB */ |
1457 | srn->srgb.range_size = srgb.range_size; | |
1458 | srn->srgb.lower_bound = srgb.lower_bound; | |
cf9b9f77 OD |
1459 | } |
1460 | ||
6f751f14 | 1461 | /* Update Algorithm, SRLB and MSD if present */ |
29df0fc7 G |
1462 | if (algo != NULL) { |
1463 | int i; | |
1464 | for (i = 0; i < ntohs(algo->header.length); i++) | |
1465 | srn->algo[i] = algo->value[0]; | |
1466 | for (; i < ALGORITHM_COUNT; i++) | |
1467 | srn->algo[i] = SR_ALGORITHM_UNSET; | |
1468 | } else { | |
1469 | srn->algo[0] = SR_ALGORITHM_SPF; | |
1470 | } | |
29df0fc7 | 1471 | srn->msd = msd; |
6f751f14 OD |
1472 | if (ri_srlb != NULL) { |
1473 | srn->srlb.range_size = GET_RANGE_SIZE(ntohl(ri_srlb->size)); | |
1474 | srn->srlb.lower_bound = GET_LABEL(ntohl(ri_srlb->lower.value)); | |
1475 | } | |
1476 | ||
6f751f14 OD |
1477 | /* Check if SRGB has changed */ |
1478 | if ((srn->srgb.range_size == srgb.range_size) | |
1479 | && (srn->srgb.lower_bound == srgb.lower_bound)) | |
1480 | return; | |
29df0fc7 G |
1481 | |
1482 | /* Copy SRGB */ | |
b61264a8 OD |
1483 | srn->srgb.range_size = srgb.range_size; |
1484 | srn->srgb.lower_bound = srgb.lower_bound; | |
29df0fc7 | 1485 | |
56981b40 OD |
1486 | osr_debug(" |- Update SR-Node[%pI4], SRGB[%u/%u], SRLB[%u/%u], Algo[%u], MSD[%u]", |
1487 | &srn->adv_router, srn->srgb.lower_bound, srn->srgb.range_size, | |
1488 | srn->srlb.lower_bound, srn->srlb.range_size, srn->algo[0], | |
1489 | srn->msd); | |
1490 | ||
b61264a8 OD |
1491 | /* ... and NHLFE if it is a neighbor SR node */ |
1492 | if (srn->neighbor == OspfSR.self) | |
1493 | hash_iterate(OspfSR.neighbors, update_out_nhlfe, srn); | |
cf9b9f77 OD |
1494 | } |
1495 | ||
1496 | /* | |
1497 | * Delete SR Node entry in hash table information corresponding to an expired | |
1498 | * Router Information LSA | |
1499 | */ | |
1500 | void ospf_sr_ri_lsa_delete(struct ospf_lsa *lsa) | |
1501 | { | |
1502 | struct sr_node *srn; | |
c4efd0f4 | 1503 | struct lsa_header *lsah = lsa->data; |
cf9b9f77 | 1504 | |
b61264a8 OD |
1505 | osr_debug("SR (%s): Remove SR node %pI4 from lsa_id 4.0.0.%u", __func__, |
1506 | &lsah->adv_router, GET_OPAQUE_ID(ntohl(lsah->id.s_addr))); | |
cf9b9f77 OD |
1507 | |
1508 | /* Sanity check */ | |
1509 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1510 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1511 | "SR (%s): Abort! no valid SR Data Base", __func__); |
cf9b9f77 OD |
1512 | return; |
1513 | } | |
1514 | ||
1515 | /* Release Router ID entry in SRDB hash table */ | |
1516 | srn = hash_release(OspfSR.neighbors, &(lsah->adv_router)); | |
1517 | ||
1518 | /* Sanity check */ | |
1519 | if (srn == NULL) { | |
cf444bcf | 1520 | flog_err(EC_OSPF_SR_NODE_CREATE, |
b61264a8 OD |
1521 | "SR (%s): Abort! no entry in SRDB for SR Node %pI4", |
1522 | __func__, &lsah->adv_router); | |
cf9b9f77 OD |
1523 | return; |
1524 | } | |
1525 | ||
1526 | if ((srn->instance != 0) && (srn->instance != ntohl(lsah->id.s_addr))) { | |
b61264a8 OD |
1527 | flog_err( |
1528 | EC_OSPF_SR_INVALID_LSA_ID, | |
1529 | "SR (%s): Abort! Wrong LSA ID 4.0.0.%u for SR node %pI4", | |
1530 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1531 | &lsah->adv_router); | |
cf9b9f77 OD |
1532 | return; |
1533 | } | |
1534 | ||
1535 | /* Remove SR node */ | |
1536 | sr_node_del(srn); | |
cf9b9f77 OD |
1537 | } |
1538 | ||
1539 | /* Update Segment Routing from Extended Link LSA */ | |
1540 | void ospf_sr_ext_link_lsa_update(struct ospf_lsa *lsa) | |
1541 | { | |
1542 | struct sr_node *srn; | |
1543 | struct tlv_header *tlvh; | |
c4efd0f4 | 1544 | struct lsa_header *lsah = lsa->data; |
cf9b9f77 OD |
1545 | struct sr_link *srl; |
1546 | ||
8db278b5 | 1547 | int length; |
cf9b9f77 | 1548 | |
b37eb79c OD |
1549 | osr_debug("SR (%s): Process Extended Link LSA 8.0.0.%u from %pI4", |
1550 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1551 | &lsah->adv_router); | |
cf9b9f77 OD |
1552 | |
1553 | /* Sanity check */ | |
1554 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1555 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1556 | "SR (%s): Abort! no valid SR DataBase", __func__); |
cf9b9f77 OD |
1557 | return; |
1558 | } | |
1559 | ||
1560 | /* Get SR Node in hash table from Router ID */ | |
1561 | srn = (struct sr_node *)hash_get(OspfSR.neighbors, | |
1562 | (void *)&(lsah->adv_router), | |
1563 | (void *)sr_node_new); | |
1564 | ||
cf9b9f77 | 1565 | /* Initialize TLV browsing */ |
8db278b5 OD |
1566 | length = lsa->size - OSPF_LSA_HEADER_SIZE; |
1567 | for (tlvh = TLV_HDR_TOP(lsah); length > 0 && tlvh; | |
cf9b9f77 OD |
1568 | tlvh = TLV_HDR_NEXT(tlvh)) { |
1569 | if (ntohs(tlvh->type) == EXT_TLV_LINK) { | |
1570 | /* Got Extended Link information */ | |
8db278b5 | 1571 | srl = get_ext_link_sid(tlvh, length); |
cf9b9f77 OD |
1572 | /* Update SID if not null */ |
1573 | if (srl != NULL) { | |
1574 | srl->instance = ntohl(lsah->id.s_addr); | |
1575 | update_ext_link_sid(srn, srl, lsa->flags); | |
1576 | } | |
1577 | } | |
8db278b5 | 1578 | length -= TLV_SIZE(tlvh); |
cf9b9f77 OD |
1579 | } |
1580 | } | |
1581 | ||
1582 | /* Delete Segment Routing from Extended Link LSA */ | |
1583 | void ospf_sr_ext_link_lsa_delete(struct ospf_lsa *lsa) | |
1584 | { | |
1585 | struct listnode *node; | |
1586 | struct sr_link *srl; | |
1587 | struct sr_node *srn; | |
b61264a8 | 1588 | struct lsa_header *lsah = lsa->data; |
93f0a26e | 1589 | uint32_t instance = ntohl(lsah->id.s_addr); |
cf9b9f77 | 1590 | |
b37eb79c OD |
1591 | osr_debug("SR (%s): Remove Extended Link LSA 8.0.0.%u from %pI4", |
1592 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1593 | &lsah->adv_router); | |
cf9b9f77 OD |
1594 | |
1595 | /* Sanity check */ | |
1596 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1597 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1598 | "SR (%s): Abort! no valid SR DataBase", __func__); |
cf9b9f77 OD |
1599 | return; |
1600 | } | |
1601 | ||
1602 | /* Search SR Node in hash table from Router ID */ | |
1603 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, | |
1604 | (void *)&(lsah->adv_router)); | |
1605 | ||
7743f2f8 OD |
1606 | /* |
1607 | * SR-Node may be NULL if it has been remove previously when | |
1608 | * processing Router Information LSA deletion | |
1609 | */ | |
cf9b9f77 | 1610 | if (srn == NULL) { |
cf444bcf | 1611 | flog_err(EC_OSPF_SR_INVALID_DB, |
b61264a8 OD |
1612 | "SR (%s): Stop! no entry in SRDB for SR Node %pI4", |
1613 | __func__, &lsah->adv_router); | |
cf9b9f77 OD |
1614 | return; |
1615 | } | |
1616 | ||
1617 | /* Search for corresponding Segment Link */ | |
1618 | for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) | |
1619 | if (srl->instance == instance) | |
1620 | break; | |
1621 | ||
b61264a8 OD |
1622 | /* Remove Segment Link if found. Note that for Neighbors, only Global |
1623 | * Adj/Lan-Adj SID are stored in the SR-DB */ | |
fd3b19f2 | 1624 | if ((srl != NULL) && (srl->instance == instance)) { |
b37eb79c OD |
1625 | del_adj_sid(srl->nhlfe[0]); |
1626 | del_adj_sid(srl->nhlfe[1]); | |
cf9b9f77 OD |
1627 | listnode_delete(srn->ext_link, srl); |
1628 | XFREE(MTYPE_OSPF_SR_PARAMS, srl); | |
cf9b9f77 | 1629 | } |
cf9b9f77 OD |
1630 | } |
1631 | ||
21baf89a OD |
1632 | /* Add (LAN)Adjacency-SID from Extended Link Information */ |
1633 | void ospf_sr_ext_itf_add(struct ext_itf *exti) | |
1634 | { | |
1635 | struct sr_node *srn = OspfSR.self; | |
1636 | struct sr_link *srl; | |
1637 | ||
1638 | osr_debug("SR (%s): Add Extended Link LSA 8.0.0.%u from self", __func__, | |
1639 | exti->instance); | |
1640 | ||
1641 | /* Sanity check */ | |
1642 | if (srn == NULL) | |
1643 | return; | |
1644 | ||
1645 | /* Initialize new Segment Routing Link */ | |
1646 | srl = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_link)); | |
1647 | srl->srn = srn; | |
1648 | srl->adv_router = srn->adv_router; | |
1649 | srl->itf_addr = exti->link.link_data; | |
1650 | srl->instance = | |
1651 | SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, exti->instance); | |
cc1725bd | 1652 | srl->remote_id = exti->link.link_id; |
21baf89a OD |
1653 | switch (exti->stype) { |
1654 | case ADJ_SID: | |
1655 | srl->type = ADJ_SID; | |
1656 | /* Primary information */ | |
1657 | srl->flags[0] = exti->adj_sid[0].flags; | |
1658 | if (CHECK_FLAG(exti->adj_sid[0].flags, | |
1659 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1660 | srl->sid[0] = GET_LABEL(ntohl(exti->adj_sid[0].value)); | |
1661 | else | |
1662 | srl->sid[0] = ntohl(exti->adj_sid[0].value); | |
1663 | if (exti->rmt_itf_addr.header.type == 0) | |
1664 | srl->nhlfe[0].nexthop = exti->link.link_id; | |
1665 | else | |
1666 | srl->nhlfe[0].nexthop = exti->rmt_itf_addr.value; | |
1667 | /* Backup Information if set */ | |
1668 | if (exti->adj_sid[1].header.type == 0) | |
1669 | break; | |
1670 | srl->flags[1] = exti->adj_sid[1].flags; | |
1671 | if (CHECK_FLAG(exti->adj_sid[1].flags, | |
1672 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1673 | srl->sid[1] = GET_LABEL(ntohl(exti->adj_sid[1].value)); | |
1674 | else | |
1675 | srl->sid[1] = ntohl(exti->adj_sid[1].value); | |
1676 | if (exti->rmt_itf_addr.header.type == 0) | |
1677 | srl->nhlfe[1].nexthop = exti->link.link_id; | |
1678 | else | |
1679 | srl->nhlfe[1].nexthop = exti->rmt_itf_addr.value; | |
1680 | break; | |
1681 | case LAN_ADJ_SID: | |
1682 | srl->type = LAN_ADJ_SID; | |
1683 | /* Primary information */ | |
1684 | srl->flags[0] = exti->lan_sid[0].flags; | |
1685 | if (CHECK_FLAG(exti->lan_sid[0].flags, | |
1686 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1687 | srl->sid[0] = GET_LABEL(ntohl(exti->lan_sid[0].value)); | |
1688 | else | |
1689 | srl->sid[0] = ntohl(exti->lan_sid[0].value); | |
1690 | if (exti->rmt_itf_addr.header.type == 0) | |
1691 | srl->nhlfe[0].nexthop = exti->lan_sid[0].neighbor_id; | |
1692 | else | |
1693 | srl->nhlfe[0].nexthop = exti->rmt_itf_addr.value; | |
1694 | /* Backup Information if set */ | |
1695 | if (exti->lan_sid[1].header.type == 0) | |
1696 | break; | |
1697 | srl->flags[1] = exti->lan_sid[1].flags; | |
1698 | if (CHECK_FLAG(exti->lan_sid[1].flags, | |
1699 | EXT_SUBTLV_LINK_ADJ_SID_VFLG)) | |
1700 | srl->sid[1] = GET_LABEL(ntohl(exti->lan_sid[1].value)); | |
1701 | else | |
1702 | srl->sid[1] = ntohl(exti->lan_sid[1].value); | |
1703 | if (exti->rmt_itf_addr.header.type == 0) | |
1704 | srl->nhlfe[1].nexthop = exti->lan_sid[1].neighbor_id; | |
1705 | else | |
1706 | srl->nhlfe[1].nexthop = exti->rmt_itf_addr.value; | |
1707 | break; | |
5d8c8d0b DS |
1708 | case PREF_SID: |
1709 | case LOCAL_SID: | |
21baf89a OD |
1710 | /* Wrong SID Type. Abort! */ |
1711 | XFREE(MTYPE_OSPF_SR_PARAMS, srl); | |
1712 | return; | |
1713 | } | |
1714 | ||
1715 | /* Segment Routing Link is ready, update it */ | |
1716 | update_ext_link_sid(srn, srl, OSPF_LSA_SELF); | |
1717 | } | |
1718 | ||
1719 | /* Delete Prefix or (LAN)Adjacency-SID from Extended Link Information */ | |
1720 | void ospf_sr_ext_itf_delete(struct ext_itf *exti) | |
1721 | { | |
1722 | struct listnode *node; | |
1723 | struct sr_node *srn = OspfSR.self; | |
1724 | struct sr_prefix *srp = NULL; | |
1725 | struct sr_link *srl = NULL; | |
1726 | uint32_t instance; | |
1727 | ||
1728 | osr_debug("SR (%s): Remove Extended LSA %u.0.0.%u from self", | |
1729 | __func__, exti->stype == PREF_SID ? 7 : 8, exti->instance); | |
1730 | ||
1731 | /* Sanity check: SR-Node and Extended Prefix/Link list may have been | |
1732 | * removed earlier when stopping OSPF or OSPF-SR */ | |
1733 | if (srn == NULL || srn->ext_prefix == NULL || srn->ext_link == NULL) | |
1734 | return; | |
1735 | ||
1736 | if (exti->stype == PREF_SID) { | |
1737 | instance = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_PREFIX_LSA, | |
1738 | exti->instance); | |
1739 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) | |
1740 | if (srp->instance == instance) | |
1741 | break; | |
1742 | ||
1743 | /* Uninstall Segment Prefix SID if found */ | |
1744 | if ((srp != NULL) && (srp->instance == instance)) | |
43e7abb5 | 1745 | ospf_zebra_delete_prefix_sid(srp); |
21baf89a OD |
1746 | } else { |
1747 | /* Search for corresponding Segment Link for self SR-Node */ | |
1748 | instance = SET_OPAQUE_LSID(OPAQUE_TYPE_EXTENDED_LINK_LSA, | |
1749 | exti->instance); | |
1750 | for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) | |
1751 | if (srl->instance == instance) | |
1752 | break; | |
1753 | ||
1754 | /* Remove Segment Link if found */ | |
1755 | if ((srl != NULL) && (srl->instance == instance)) { | |
1756 | del_adj_sid(srl->nhlfe[0]); | |
1757 | del_adj_sid(srl->nhlfe[1]); | |
1758 | listnode_delete(srn->ext_link, srl); | |
1759 | XFREE(MTYPE_OSPF_SR_PARAMS, srl); | |
1760 | } | |
1761 | } | |
1762 | } | |
1763 | ||
cf9b9f77 OD |
1764 | /* Update Segment Routing from Extended Prefix LSA */ |
1765 | void ospf_sr_ext_prefix_lsa_update(struct ospf_lsa *lsa) | |
1766 | { | |
1767 | struct sr_node *srn; | |
1768 | struct tlv_header *tlvh; | |
b61264a8 | 1769 | struct lsa_header *lsah = (struct lsa_header *)lsa->data; |
cf9b9f77 OD |
1770 | struct sr_prefix *srp; |
1771 | ||
8db278b5 | 1772 | int length; |
cf9b9f77 | 1773 | |
b37eb79c OD |
1774 | osr_debug("SR (%s): Process Extended Prefix LSA 7.0.0.%u from %pI4", |
1775 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1776 | &lsah->adv_router); | |
cf9b9f77 OD |
1777 | |
1778 | /* Sanity check */ | |
1779 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1780 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1781 | "SR (%s): Abort! no valid SR DataBase", __func__); |
cf9b9f77 OD |
1782 | return; |
1783 | } | |
1784 | ||
1785 | /* Get SR Node in hash table from Router ID */ | |
1786 | srn = (struct sr_node *)hash_get(OspfSR.neighbors, | |
1787 | (void *)&(lsah->adv_router), | |
1788 | (void *)sr_node_new); | |
cf9b9f77 | 1789 | /* Initialize TLV browsing */ |
8db278b5 OD |
1790 | length = lsa->size - OSPF_LSA_HEADER_SIZE; |
1791 | for (tlvh = TLV_HDR_TOP(lsah); length > 0 && tlvh; | |
cf9b9f77 OD |
1792 | tlvh = TLV_HDR_NEXT(tlvh)) { |
1793 | if (ntohs(tlvh->type) == EXT_TLV_LINK) { | |
1794 | /* Got Extended Link information */ | |
8db278b5 | 1795 | srp = get_ext_prefix_sid(tlvh, length); |
cf9b9f77 OD |
1796 | /* Update SID if not null */ |
1797 | if (srp != NULL) { | |
1798 | srp->instance = ntohl(lsah->id.s_addr); | |
1799 | update_ext_prefix_sid(srn, srp); | |
1800 | } | |
1801 | } | |
8db278b5 | 1802 | length -= TLV_SIZE(tlvh); |
cf9b9f77 OD |
1803 | } |
1804 | } | |
1805 | ||
1806 | /* Delete Segment Routing from Extended Prefix LSA */ | |
1807 | void ospf_sr_ext_prefix_lsa_delete(struct ospf_lsa *lsa) | |
1808 | { | |
1809 | struct listnode *node; | |
1810 | struct sr_prefix *srp; | |
1811 | struct sr_node *srn; | |
1812 | struct lsa_header *lsah = (struct lsa_header *)lsa->data; | |
93f0a26e | 1813 | uint32_t instance = ntohl(lsah->id.s_addr); |
cf9b9f77 | 1814 | |
b37eb79c OD |
1815 | osr_debug("SR (%s): Remove Extended Prefix LSA 7.0.0.%u from %pI4", |
1816 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), | |
1817 | &lsah->adv_router); | |
cf9b9f77 OD |
1818 | |
1819 | /* Sanity check */ | |
1820 | if (OspfSR.neighbors == NULL) { | |
cf444bcf | 1821 | flog_err(EC_OSPF_SR_INVALID_DB, |
dbb93f1b | 1822 | "SR (%s): Abort! no valid SR DataBase", __func__); |
cf9b9f77 OD |
1823 | return; |
1824 | } | |
1825 | ||
1826 | /* Search SR Node in hash table from Router ID */ | |
1827 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, | |
1828 | (void *)&(lsah->adv_router)); | |
1829 | ||
7743f2f8 OD |
1830 | /* |
1831 | * SR-Node may be NULL if it has been remove previously when | |
1832 | * processing Router Information LSA deletion | |
1833 | */ | |
cf9b9f77 | 1834 | if (srn == NULL) { |
cf444bcf | 1835 | flog_err(EC_OSPF_SR_INVALID_DB, |
b61264a8 OD |
1836 | "SR (%s): Stop! no entry in SRDB for SR Node %pI4", |
1837 | __func__, &lsah->adv_router); | |
cf9b9f77 OD |
1838 | return; |
1839 | } | |
1840 | ||
b61264a8 | 1841 | /* Search for corresponding Segment Prefix */ |
cf9b9f77 OD |
1842 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) |
1843 | if (srp->instance == instance) | |
1844 | break; | |
1845 | ||
b37eb79c | 1846 | /* Remove Prefix if found */ |
fd3b19f2 | 1847 | if ((srp != NULL) && (srp->instance == instance)) { |
43e7abb5 | 1848 | ospf_zebra_delete_prefix_sid(srp); |
b37eb79c | 1849 | listnode_delete(srn->ext_prefix, srp); |
cf9b9f77 OD |
1850 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); |
1851 | } else { | |
dbb93f1b | 1852 | flog_err( |
cf444bcf | 1853 | EC_OSPF_SR_INVALID_DB, |
b61264a8 | 1854 | "SR (%s): Didn't found corresponding SR Prefix 7.0.0.%u for SR Node %pI4", |
996c9314 | 1855 | __func__, GET_OPAQUE_ID(ntohl(lsah->id.s_addr)), |
b61264a8 | 1856 | &lsah->adv_router); |
cf9b9f77 | 1857 | } |
cf9b9f77 OD |
1858 | } |
1859 | ||
7743f2f8 OD |
1860 | /* |
1861 | * Update Prefix SID. Call by ospf_ext_pref_ism_change to | |
b37eb79c | 1862 | * complete initial CLI command at startup. |
7743f2f8 OD |
1863 | * |
1864 | * @param ifp - Loopback interface | |
1865 | * @param pref - Prefix address of this interface | |
1866 | * | |
1867 | * @return - void | |
1868 | */ | |
b37eb79c | 1869 | void ospf_sr_update_local_prefix(struct interface *ifp, struct prefix *p) |
7743f2f8 OD |
1870 | { |
1871 | struct listnode *node; | |
1872 | struct sr_prefix *srp; | |
7743f2f8 OD |
1873 | |
1874 | /* Sanity Check */ | |
1875 | if ((ifp == NULL) || (p == NULL)) | |
1876 | return; | |
1877 | ||
1878 | /* | |
1879 | * Search if there is a Segment Prefix that correspond to this | |
1880 | * interface or prefix, and update it if found | |
1881 | */ | |
1882 | for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) { | |
1883 | if ((srp->nhlfe.ifindex == ifp->ifindex) | |
b61264a8 | 1884 | || ((IPV4_ADDR_SAME(&srp->prefv4.prefix, &p->u.prefix4)) |
b37eb79c | 1885 | && (srp->prefv4.prefixlen == p->prefixlen))) { |
7743f2f8 OD |
1886 | |
1887 | /* Update Interface & Prefix info */ | |
1888 | srp->nhlfe.ifindex = ifp->ifindex; | |
b37eb79c OD |
1889 | IPV4_ADDR_COPY(&srp->prefv4.prefix, &p->u.prefix4); |
1890 | srp->prefv4.prefixlen = p->prefixlen; | |
1891 | srp->prefv4.family = p->family; | |
7743f2f8 OD |
1892 | IPV4_ADDR_COPY(&srp->nhlfe.nexthop, &p->u.prefix4); |
1893 | ||
1894 | /* OK. Let's Schedule Extended Prefix LSA */ | |
996c9314 | 1895 | srp->instance = ospf_ext_schedule_prefix_index( |
b37eb79c OD |
1896 | ifp, srp->sid, &srp->prefv4, srp->flags); |
1897 | ||
b61264a8 OD |
1898 | osr_debug( |
1899 | " |- Update Node SID %pFX - %u for self SR Node", | |
1900 | (struct prefix *)&srp->prefv4, srp->sid); | |
7743f2f8 | 1901 | |
f786c4d7 OD |
1902 | /* Install SID if NO-PHP is set and not EXPLICIT-NULL */ |
1903 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG) | |
1904 | && !CHECK_FLAG(srp->flags, | |
1905 | EXT_SUBTLV_PREFIX_SID_EFLG)) { | |
b61264a8 OD |
1906 | srp->label_in = index2label(srp->sid, |
1907 | OspfSR.self->srgb); | |
70e98a7f | 1908 | srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL; |
43e7abb5 | 1909 | ospf_zebra_update_prefix_sid(srp); |
7743f2f8 OD |
1910 | } |
1911 | } | |
1912 | } | |
1913 | } | |
1914 | ||
cf9b9f77 OD |
1915 | /* |
1916 | * Following functions are used to update MPLS LFIB after a SPF run | |
1917 | */ | |
1918 | ||
e3b78da8 | 1919 | static void ospf_sr_nhlfe_update(struct hash_bucket *bucket, void *args) |
cf9b9f77 OD |
1920 | { |
1921 | ||
e3b78da8 | 1922 | struct sr_node *srn = (struct sr_node *)bucket->data; |
cf9b9f77 OD |
1923 | struct listnode *node; |
1924 | struct sr_prefix *srp; | |
b37eb79c | 1925 | bool old; |
cf9b9f77 OD |
1926 | int rc; |
1927 | ||
b37eb79c | 1928 | osr_debug(" |- Update Prefix for SR Node %pI4", &srn->adv_router); |
cf9b9f77 | 1929 | |
7743f2f8 OD |
1930 | /* Skip Self SR Node */ |
1931 | if (srn == OspfSR.self) | |
cf9b9f77 | 1932 | return; |
cf9b9f77 OD |
1933 | |
1934 | /* Update Extended Prefix */ | |
1935 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) { | |
1936 | ||
b37eb79c OD |
1937 | /* Keep track of valid route */ |
1938 | old = srp->route != NULL; | |
cf9b9f77 OD |
1939 | |
1940 | /* Compute the new NHLFE */ | |
1941 | rc = compute_prefix_nhlfe(srp); | |
1942 | ||
1943 | /* Check computation result */ | |
1944 | switch (rc) { | |
b37eb79c | 1945 | /* Routes are not know, remove old NHLFE if any to avoid loop */ |
cf9b9f77 | 1946 | case -1: |
b37eb79c | 1947 | if (old) |
43e7abb5 | 1948 | ospf_zebra_delete_prefix_sid(srp); |
cf9b9f77 | 1949 | break; |
b37eb79c | 1950 | /* Routes exist but are not ready, skip it */ |
cf9b9f77 OD |
1951 | case 0: |
1952 | break; | |
b37eb79c | 1953 | /* There is at least one route, update NHLFE */ |
cf9b9f77 | 1954 | case 1: |
43e7abb5 | 1955 | ospf_zebra_update_prefix_sid(srp); |
cf9b9f77 OD |
1956 | break; |
1957 | default: | |
1958 | break; | |
1959 | } | |
1960 | } | |
1961 | } | |
1962 | ||
b37eb79c | 1963 | void ospf_sr_update_task(struct ospf *ospf) |
cf9b9f77 OD |
1964 | { |
1965 | ||
cf9b9f77 OD |
1966 | struct timeval start_time, stop_time; |
1967 | ||
a351b3e4 OD |
1968 | /* Check ospf and SR status */ |
1969 | if ((ospf == NULL) || (OspfSR.status != SR_UP)) | |
b37eb79c | 1970 | return; |
cf9b9f77 OD |
1971 | |
1972 | monotime(&start_time); | |
1973 | ||
b37eb79c | 1974 | osr_debug("SR (%s): Start SPF update", __func__); |
cf9b9f77 | 1975 | |
e3b78da8 | 1976 | hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *, |
cf9b9f77 OD |
1977 | void *))ospf_sr_nhlfe_update, |
1978 | NULL); | |
1979 | ||
1980 | monotime(&stop_time); | |
1981 | ||
b37eb79c OD |
1982 | osr_debug("SR (%s): SPF Processing Time(usecs): %lld", __func__, |
1983 | (stop_time.tv_sec - start_time.tv_sec) * 1000000LL | |
b61264a8 | 1984 | + (stop_time.tv_usec - start_time.tv_usec)); |
cf9b9f77 OD |
1985 | } |
1986 | ||
7743f2f8 OD |
1987 | /* |
1988 | * -------------------------------------- | |
78dfa0c7 | 1989 | * Following are vty command functions. |
7743f2f8 OD |
1990 | * -------------------------------------- |
1991 | */ | |
cf9b9f77 OD |
1992 | |
1993 | /* | |
1994 | * Segment Routing Router configuration | |
1995 | * | |
1996 | * Must be centralize as it concerns both Extended Link/Prefix LSA | |
1997 | * and Router Information LSA. Choose to call it from Extended Prefix | |
1998 | * write_config() call back. | |
1999 | * | |
2000 | * @param vty VTY output | |
2001 | * | |
2002 | * @return none | |
2003 | */ | |
2004 | void ospf_sr_config_write_router(struct vty *vty) | |
2005 | { | |
2006 | struct listnode *node; | |
2007 | struct sr_prefix *srp; | |
6f751f14 | 2008 | uint32_t upper; |
cf9b9f77 | 2009 | |
6443a4be | 2010 | if (OspfSR.status == SR_UP) |
cf9b9f77 OD |
2011 | vty_out(vty, " segment-routing on\n"); |
2012 | ||
6443a4be EDP |
2013 | upper = OspfSR.srgb.start + OspfSR.srgb.size - 1; |
2014 | if ((OspfSR.srgb.start != DEFAULT_SRGB_LABEL) | |
2015 | || (OspfSR.srgb.size != DEFAULT_SRGB_SIZE)) | |
2016 | vty_out(vty, " segment-routing global-block %u %u", | |
2017 | OspfSR.srgb.start, upper); | |
6f751f14 | 2018 | |
7bd1ec94 PG |
2019 | if ((OspfSR.srlb.start != DEFAULT_SRLB_LABEL) || |
2020 | (OspfSR.srlb.end != DEFAULT_SRLB_END)) { | |
2021 | if ((OspfSR.srgb.start == DEFAULT_SRGB_LABEL) && | |
2022 | (OspfSR.srgb.size == DEFAULT_SRGB_SIZE)) | |
2023 | vty_out(vty, " segment-routing global-block %u %u", | |
2024 | OspfSR.srgb.start, upper); | |
6443a4be EDP |
2025 | vty_out(vty, " local-block %u %u\n", OspfSR.srlb.start, |
2026 | OspfSR.srlb.end); | |
7bd1ec94 | 2027 | } else |
6443a4be | 2028 | vty_out(vty, "\n"); |
6f751f14 | 2029 | |
6443a4be EDP |
2030 | if (OspfSR.msd != 0) |
2031 | vty_out(vty, " segment-routing node-msd %u\n", OspfSR.msd); | |
cf9b9f77 | 2032 | |
6443a4be EDP |
2033 | if (OspfSR.self != NULL) { |
2034 | for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) { | |
2035 | vty_out(vty, " segment-routing prefix %pFX index %u", | |
2036 | &srp->prefv4, srp->sid); | |
2037 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG)) | |
2038 | vty_out(vty, " explicit-null\n"); | |
2039 | else if (CHECK_FLAG(srp->flags, | |
2040 | EXT_SUBTLV_PREFIX_SID_NPFLG)) | |
2041 | vty_out(vty, " no-php-flag\n"); | |
2042 | else | |
2043 | vty_out(vty, "\n"); | |
cf9b9f77 OD |
2044 | } |
2045 | } | |
2046 | } | |
2047 | ||
2048 | DEFUN(ospf_sr_enable, | |
2049 | ospf_sr_enable_cmd, | |
2050 | "segment-routing on", | |
2051 | SR_STR | |
2052 | "Enable Segment Routing\n") | |
2053 | { | |
2054 | ||
2055 | VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); | |
2056 | ||
d81b8e0e | 2057 | if (OspfSR.status != SR_OFF) |
cf9b9f77 OD |
2058 | return CMD_SUCCESS; |
2059 | ||
7726c479 | 2060 | if (ospf->vrf_id != VRF_DEFAULT) { |
996c9314 | 2061 | vty_out(vty, |
3efd0893 | 2062 | "Segment Routing is only supported in default VRF\n"); |
7726c479 OD |
2063 | return CMD_WARNING_CONFIG_FAILED; |
2064 | } | |
2065 | ||
b37eb79c | 2066 | osr_debug("SR: Segment Routing: OFF -> ON"); |
cf9b9f77 OD |
2067 | |
2068 | /* Start Segment Routing */ | |
d81b8e0e | 2069 | OspfSR.status = SR_ON; |
266469eb | 2070 | ospf_sr_start(ospf); |
cf9b9f77 | 2071 | |
cf9b9f77 OD |
2072 | return CMD_SUCCESS; |
2073 | } | |
2074 | ||
2075 | DEFUN (no_ospf_sr_enable, | |
2076 | no_ospf_sr_enable_cmd, | |
2077 | "no segment-routing [on]", | |
2078 | NO_STR | |
2079 | SR_STR | |
2080 | "Disable Segment Routing\n") | |
2081 | { | |
2082 | ||
d81b8e0e | 2083 | if (OspfSR.status == SR_OFF) |
cf9b9f77 OD |
2084 | return CMD_SUCCESS; |
2085 | ||
b37eb79c | 2086 | osr_debug("SR: Segment Routing: ON -> OFF"); |
cf9b9f77 OD |
2087 | |
2088 | /* Start by Disabling Extended Link & Prefix LSA */ | |
2089 | ospf_ext_update_sr(false); | |
2090 | ||
2091 | /* then, disable Router Information SR parameters */ | |
6f751f14 | 2092 | ospf_router_info_update_sr(false, OspfSR.self); |
cf9b9f77 OD |
2093 | |
2094 | /* Finally, stop Segment Routing */ | |
2095 | ospf_sr_stop(); | |
cf9b9f77 OD |
2096 | |
2097 | return CMD_SUCCESS; | |
2098 | } | |
2099 | ||
2100 | static int ospf_sr_enabled(struct vty *vty) | |
2101 | { | |
d81b8e0e | 2102 | if (OspfSR.status != SR_OFF) |
cf9b9f77 OD |
2103 | return 1; |
2104 | ||
2105 | if (vty) | |
2106 | vty_out(vty, "%% OSPF SR is not turned on\n"); | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
4e10b4df FR |
2111 | /* tell if two ranges [r1_lower, r1_upper] and [r2_lower,r2_upper] overlap */ |
2112 | static bool ranges_overlap(uint32_t r1_lower, uint32_t r1_upper, | |
2113 | uint32_t r2_lower, uint32_t r2_upper) | |
2114 | { | |
2115 | return !((r1_upper < r2_lower) || (r1_lower > r2_upper)); | |
2116 | } | |
2117 | ||
6c57d010 FR |
2118 | |
2119 | /* tell if a range is valid */ | |
2120 | static bool sr_range_is_valid(uint32_t lower, uint32_t upper, uint32_t min_size) | |
2121 | { | |
2122 | return (upper >= lower + min_size); | |
2123 | } | |
2124 | ||
d81b8e0e | 2125 | /** |
6443a4be | 2126 | * Update SRGB and/or SRLB using new CLI values. |
d81b8e0e | 2127 | * |
6443a4be EDP |
2128 | * @param gb_lower Lower bound of the SRGB |
2129 | * @param gb_upper Upper bound of the SRGB | |
2130 | * @param lb_lower Lower bound of the SRLB | |
2131 | * @param lb_upper Upper bound of the SRLB | |
d81b8e0e | 2132 | * |
6443a4be | 2133 | * @return 0 on success, -1 otherwise |
d81b8e0e | 2134 | */ |
6443a4be EDP |
2135 | static int update_sr_blocks(uint32_t gb_lower, uint32_t gb_upper, |
2136 | uint32_t lb_lower, uint32_t lb_upper) | |
d81b8e0e OD |
2137 | { |
2138 | ||
2139 | /* Check if values have changed */ | |
6443a4be EDP |
2140 | bool gb_changed, lb_changed; |
2141 | uint32_t gb_size = gb_upper - gb_lower + 1; | |
2142 | uint32_t lb_size = lb_upper - lb_lower + 1; | |
2143 | ||
2144 | gb_changed = | |
2145 | (OspfSR.srgb.size != gb_size || OspfSR.srgb.start != gb_lower); | |
2146 | lb_changed = | |
2147 | (OspfSR.srlb.end != lb_upper || OspfSR.srlb.start != lb_lower); | |
2148 | if (!gb_changed && !lb_changed) | |
d81b8e0e OD |
2149 | return 0; |
2150 | ||
6443a4be EDP |
2151 | /* Check if SR is correctly started i.e. Label Manager connected */ |
2152 | if (OspfSR.status != SR_UP) { | |
2153 | OspfSR.srgb.size = gb_size; | |
2154 | OspfSR.srgb.start = gb_lower; | |
2155 | OspfSR.srlb.end = lb_upper; | |
2156 | OspfSR.srlb.start = lb_lower; | |
2157 | return 0; | |
d81b8e0e OD |
2158 | } |
2159 | ||
6443a4be EDP |
2160 | /* Release old SRGB if it has changed and is active. */ |
2161 | if (gb_changed) { | |
c181efbe FR |
2162 | |
2163 | sr_global_block_delete(); | |
6443a4be EDP |
2164 | |
2165 | /* Set new SRGB values - but do not reserve yet (we need to | |
2166 | * release the SRLB too) */ | |
2167 | OspfSR.srgb.size = gb_size; | |
2168 | OspfSR.srgb.start = gb_lower; | |
2169 | if (OspfSR.self != NULL) { | |
2170 | OspfSR.self->srgb.range_size = gb_size; | |
2171 | OspfSR.self->srgb.lower_bound = gb_lower; | |
2172 | } | |
d81b8e0e | 2173 | } |
6443a4be EDP |
2174 | /* Release old SRLB if it has changed and reserve new block as needed. |
2175 | */ | |
2176 | if (lb_changed) { | |
c181efbe FR |
2177 | |
2178 | sr_local_block_delete(); | |
d81b8e0e | 2179 | |
6443a4be EDP |
2180 | /* Set new SRLB values */ |
2181 | if (sr_local_block_init(lb_lower, lb_upper) < 0) { | |
2182 | ospf_sr_stop(); | |
2183 | return -1; | |
2184 | } | |
2185 | if (OspfSR.self != NULL) { | |
2186 | OspfSR.self->srlb.lower_bound = lb_lower; | |
2187 | OspfSR.self->srlb.range_size = lb_size; | |
2188 | } | |
2189 | } | |
d81b8e0e OD |
2190 | |
2191 | /* | |
6443a4be EDP |
2192 | * Try to reserve the new SRGB from the Label Manger. If the |
2193 | * allocation fails, disable SR until new blocks are successfully | |
2194 | * allocated. | |
d81b8e0e | 2195 | */ |
6443a4be | 2196 | if (gb_changed) { |
daaebd39 | 2197 | if (sr_global_block_init(OspfSR.srgb.start, OspfSR.srgb.size) |
6443a4be | 2198 | < 0) { |
6443a4be EDP |
2199 | ospf_sr_stop(); |
2200 | return -1; | |
daaebd39 | 2201 | } |
6443a4be | 2202 | } |
d81b8e0e | 2203 | |
83df36e8 OD |
2204 | /* Update Self SR-Node */ |
2205 | if (OspfSR.self != NULL) { | |
2206 | /* SRGB is reserved, set Router Information parameters */ | |
2207 | ospf_router_info_update_sr(true, OspfSR.self); | |
d81b8e0e | 2208 | |
83df36e8 | 2209 | /* and update NHLFE entries */ |
6443a4be EDP |
2210 | if (gb_changed) |
2211 | hash_iterate(OspfSR.neighbors, | |
2212 | (void (*)(struct hash_bucket *, | |
2213 | void *))update_in_nhlfe, | |
2214 | NULL); | |
2215 | ||
2216 | /* and update (LAN)-Adjacency SID */ | |
2217 | if (lb_changed) | |
2218 | ospf_ext_link_srlb_update(); | |
83df36e8 | 2219 | } |
d81b8e0e OD |
2220 | |
2221 | return 0; | |
2222 | } | |
2223 | ||
6443a4be EDP |
2224 | DEFUN(sr_global_label_range, sr_global_label_range_cmd, |
2225 | "segment-routing global-block (16-1048575) (16-1048575) [local-block (16-1048575) (16-1048575)]", | |
2226 | SR_STR | |
2227 | "Segment Routing Global Block label range\n" | |
2228 | "Lower-bound range in decimal (16-1048575)\n" | |
2229 | "Upper-bound range in decimal (16-1048575)\n" | |
2230 | "Segment Routing Local Block label range\n" | |
2231 | "Lower-bound range in decimal (16-1048575)\n" | |
2232 | "Upper-bound range in decimal (16-1048575)\n") | |
cf9b9f77 | 2233 | { |
6443a4be EDP |
2234 | uint32_t lb_upper, lb_lower; |
2235 | uint32_t gb_upper, gb_lower; | |
2236 | int idx_gb_low = 2, idx_gb_up = 3; | |
2237 | int idx_lb_low = 5, idx_lb_up = 6; | |
2238 | ||
2239 | /* Get lower and upper bound for mandatory global-block */ | |
2240 | gb_lower = strtoul(argv[idx_gb_low]->arg, NULL, 10); | |
2241 | gb_upper = strtoul(argv[idx_gb_up]->arg, NULL, 10); | |
6c57d010 | 2242 | |
6443a4be EDP |
2243 | /* SRLB values are taken from vtysh if there, else use the known ones */ |
2244 | lb_upper = argc > idx_lb_up ? strtoul(argv[idx_lb_up]->arg, NULL, 10) | |
2245 | : OspfSR.srlb.end; | |
2246 | lb_lower = argc > idx_lb_low ? strtoul(argv[idx_lb_low]->arg, NULL, 10) | |
2247 | : OspfSR.srlb.start; | |
cf9b9f77 | 2248 | |
6c57d010 FR |
2249 | /* check correctness of input SRGB */ |
2250 | if (!sr_range_is_valid(gb_lower, gb_upper, MIN_SRGB_SIZE)) { | |
2251 | vty_out(vty, "Invalid SRGB range\n"); | |
2252 | return CMD_WARNING_CONFIG_FAILED; | |
2253 | } | |
2254 | ||
2255 | /* check correctness of SRLB */ | |
2256 | if (!sr_range_is_valid(lb_lower, lb_upper, MIN_SRLB_SIZE)) { | |
2257 | vty_out(vty, "Invalid SRLB range\n"); | |
2258 | return CMD_WARNING_CONFIG_FAILED; | |
2259 | } | |
2260 | ||
6f751f14 | 2261 | /* Validate SRGB against SRLB */ |
4e10b4df | 2262 | if (ranges_overlap(gb_lower, gb_upper, lb_lower, lb_upper)) { |
6f751f14 | 2263 | vty_out(vty, |
4e10b4df | 2264 | "New SR Global Block (%u/%u) conflicts with Local Block (%u/%u)\n", |
6443a4be | 2265 | gb_lower, gb_upper, lb_lower, lb_upper); |
6f751f14 OD |
2266 | return CMD_WARNING_CONFIG_FAILED; |
2267 | } | |
2268 | ||
6443a4be | 2269 | if (update_sr_blocks(gb_lower, gb_upper, lb_lower, lb_upper) < 0) |
cf9b9f77 | 2270 | return CMD_WARNING_CONFIG_FAILED; |
d81b8e0e | 2271 | else |
7743f2f8 | 2272 | return CMD_SUCCESS; |
cf9b9f77 OD |
2273 | } |
2274 | ||
6443a4be EDP |
2275 | DEFUN(no_sr_global_label_range, no_sr_global_label_range_cmd, |
2276 | "no segment-routing global-block [(16-1048575) (16-1048575) local-block (16-1048575) (16-1048575)]", | |
2277 | NO_STR SR_STR | |
2278 | "Segment Routing Global Block label range\n" | |
2279 | "Lower-bound range in decimal (16-1048575)\n" | |
2280 | "Upper-bound range in decimal (16-1048575)\n" | |
2281 | "Segment Routing Local Block label range\n" | |
2282 | "Lower-bound range in decimal (16-1048575)\n" | |
2283 | "Upper-bound range in decimal (16-1048575)\n") | |
cf9b9f77 | 2284 | { |
6443a4be EDP |
2285 | if (update_sr_blocks(DEFAULT_SRGB_LABEL, DEFAULT_SRGB_END, |
2286 | DEFAULT_SRLB_LABEL, DEFAULT_SRLB_END) | |
2287 | < 0) | |
d81b8e0e OD |
2288 | return CMD_WARNING_CONFIG_FAILED; |
2289 | else | |
2290 | return CMD_SUCCESS; | |
cf9b9f77 OD |
2291 | } |
2292 | ||
2293 | DEFUN (sr_node_msd, | |
2294 | sr_node_msd_cmd, | |
2295 | "segment-routing node-msd (1-16)", | |
2296 | SR_STR | |
2297 | "Maximum Stack Depth for this router\n" | |
2298 | "Maximum number of label that could be stack (1-16)\n") | |
2299 | { | |
93f0a26e | 2300 | uint32_t msd; |
62c99790 | 2301 | int idx = 1; |
cf9b9f77 OD |
2302 | |
2303 | if (!ospf_sr_enabled(vty)) | |
2304 | return CMD_WARNING_CONFIG_FAILED; | |
2305 | ||
7743f2f8 OD |
2306 | /* Get MSD */ |
2307 | argv_find(argv, argc, "(1-16)", &idx); | |
2308 | msd = strtoul(argv[idx]->arg, NULL, 10); | |
cf9b9f77 | 2309 | if (msd < 1 || msd > MPLS_MAX_LABELS) { |
7743f2f8 | 2310 | vty_out(vty, "MSD must be comprise between 1 and %u\n", |
cf9b9f77 OD |
2311 | MPLS_MAX_LABELS); |
2312 | return CMD_WARNING_CONFIG_FAILED; | |
2313 | } | |
2314 | ||
7743f2f8 OD |
2315 | /* Check if value has changed */ |
2316 | if (OspfSR.msd == msd) | |
2317 | return CMD_SUCCESS; | |
2318 | ||
cf9b9f77 OD |
2319 | /* Set this router MSD */ |
2320 | OspfSR.msd = msd; | |
83df36e8 | 2321 | if (OspfSR.self != NULL) { |
7743f2f8 | 2322 | OspfSR.self->msd = msd; |
cf9b9f77 | 2323 | |
83df36e8 OD |
2324 | /* Set Router Information parameters if SR is UP */ |
2325 | if (OspfSR.status == SR_UP) | |
2326 | ospf_router_info_update_sr(true, OspfSR.self); | |
2327 | } | |
cf9b9f77 OD |
2328 | |
2329 | return CMD_SUCCESS; | |
2330 | } | |
2331 | ||
2332 | DEFUN (no_sr_node_msd, | |
2333 | no_sr_node_msd_cmd, | |
7743f2f8 | 2334 | "no segment-routing node-msd [(1-16)]", |
cf9b9f77 OD |
2335 | NO_STR |
2336 | SR_STR | |
7743f2f8 OD |
2337 | "Maximum Stack Depth for this router\n" |
2338 | "Maximum number of label that could be stack (1-16)\n") | |
cf9b9f77 OD |
2339 | { |
2340 | ||
2341 | if (!ospf_sr_enabled(vty)) | |
2342 | return CMD_WARNING_CONFIG_FAILED; | |
2343 | ||
2344 | /* unset this router MSD */ | |
2345 | OspfSR.msd = 0; | |
83df36e8 | 2346 | if (OspfSR.self != NULL) { |
7743f2f8 | 2347 | OspfSR.self->msd = 0; |
cf9b9f77 | 2348 | |
83df36e8 OD |
2349 | /* Set Router Information parameters if SR is UP */ |
2350 | if (OspfSR.status == SR_UP) | |
2351 | ospf_router_info_update_sr(true, OspfSR.self); | |
2352 | } | |
cf9b9f77 OD |
2353 | |
2354 | return CMD_SUCCESS; | |
2355 | } | |
2356 | ||
2357 | DEFUN (sr_prefix_sid, | |
2358 | sr_prefix_sid_cmd, | |
f786c4d7 | 2359 | "segment-routing prefix A.B.C.D/M index (0-65535) [no-php-flag|explicit-null]", |
cf9b9f77 OD |
2360 | SR_STR |
2361 | "Prefix SID\n" | |
2362 | "IPv4 Prefix as A.B.C.D/M\n" | |
2363 | "SID index for this prefix in decimal (0-65535)\n" | |
7743f2f8 | 2364 | "Index value inside SRGB (lower_bound < index < upper_bound)\n" |
f786c4d7 OD |
2365 | "Don't request Penultimate Hop Popping (PHP)\n" |
2366 | "Upstream neighbor must replace prefix-sid with explicit null label\n") | |
cf9b9f77 | 2367 | { |
7743f2f8 | 2368 | int idx = 0; |
d1db7359 | 2369 | struct prefix p, pexist; |
cf9b9f77 OD |
2370 | uint32_t index; |
2371 | struct listnode *node; | |
d1db7359 | 2372 | struct sr_prefix *srp, *exist = NULL; |
cf9b9f77 | 2373 | struct interface *ifp; |
d1db7359 FR |
2374 | bool no_php_flag = false; |
2375 | bool exp_null = false; | |
2376 | bool index_in_use = false; | |
2377 | uint8_t desired_flags = 0; | |
cf9b9f77 OD |
2378 | |
2379 | if (!ospf_sr_enabled(vty)) | |
2380 | return CMD_WARNING_CONFIG_FAILED; | |
2381 | ||
2382 | /* Get network prefix */ | |
7743f2f8 | 2383 | argv_find(argv, argc, "A.B.C.D/M", &idx); |
6aaf0fdd | 2384 | if (!str2prefix(argv[idx]->arg, &p)) { |
996c9314 | 2385 | vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg); |
7743f2f8 OD |
2386 | return CMD_WARNING_CONFIG_FAILED; |
2387 | } | |
cf9b9f77 OD |
2388 | |
2389 | /* Get & verify index value */ | |
7743f2f8 OD |
2390 | argv_find(argv, argc, "(0-65535)", &idx); |
2391 | index = strtoul(argv[idx]->arg, NULL, 10); | |
6f751f14 | 2392 | if (index > OspfSR.srgb.size - 1) { |
7743f2f8 | 2393 | vty_out(vty, "Index %u must be lower than range size %u\n", |
6f751f14 | 2394 | index, OspfSR.srgb.size); |
cf9b9f77 OD |
2395 | return CMD_WARNING_CONFIG_FAILED; |
2396 | } | |
2397 | ||
d1db7359 FR |
2398 | /* Get options */ |
2399 | no_php_flag = argv_find(argv, argc, "no-php-flag", &idx); | |
2400 | exp_null = argv_find(argv, argc, "explicit-null", &idx); | |
2401 | ||
2402 | desired_flags |= no_php_flag ? EXT_SUBTLV_PREFIX_SID_NPFLG : 0; | |
2403 | desired_flags |= exp_null ? EXT_SUBTLV_PREFIX_SID_NPFLG : 0; | |
2404 | desired_flags |= exp_null ? EXT_SUBTLV_PREFIX_SID_EFLG : 0; | |
2405 | ||
f786c4d7 | 2406 | /* Search for an existing Prefix-SID */ |
cf9b9f77 | 2407 | for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) { |
d1db7359 FR |
2408 | if (prefix_same((struct prefix *)&srp->prefv4, &p)) |
2409 | exist = srp; | |
cf9b9f77 | 2410 | if (srp->sid == index) { |
d1db7359 FR |
2411 | index_in_use = true; |
2412 | pexist = p; | |
cf9b9f77 OD |
2413 | } |
2414 | } | |
2415 | ||
d1db7359 FR |
2416 | /* done if prefix segment already there with same index and flags */ |
2417 | if (exist && exist->sid == index && exist->flags == desired_flags) | |
2418 | return CMD_SUCCESS; | |
2419 | ||
2420 | /* deny if index is already in use by a distinct prefix */ | |
2421 | if (!exist && index_in_use) { | |
2422 | vty_out(vty, "Index %u is already used by %pFX\n", index, | |
2423 | &pexist); | |
2424 | return CMD_WARNING_CONFIG_FAILED; | |
f786c4d7 OD |
2425 | } |
2426 | ||
270e66a2 | 2427 | /* First, remove old NHLFE if installed */ |
d1db7359 FR |
2428 | if (exist && CHECK_FLAG(exist->flags, EXT_SUBTLV_PREFIX_SID_NPFLG) |
2429 | && !CHECK_FLAG(exist->flags, EXT_SUBTLV_PREFIX_SID_EFLG)) | |
2430 | ospf_zebra_delete_prefix_sid(exist); | |
270e66a2 | 2431 | |
d1db7359 FR |
2432 | /* Create new Extended Prefix to SRDB if not found */ |
2433 | if (exist == NULL) { | |
2434 | srp = XCALLOC(MTYPE_OSPF_SR_PARAMS, sizeof(struct sr_prefix)); | |
2435 | IPV4_ADDR_COPY(&srp->prefv4.prefix, &p.u.prefix4); | |
2436 | srp->prefv4.prefixlen = p.prefixlen; | |
2437 | srp->prefv4.family = p.family; | |
2438 | srp->sid = index; | |
2439 | srp->type = LOCAL_SID; | |
2440 | } else { | |
2441 | /* we work on the existing SR prefix */ | |
2442 | srp = exist; | |
6aaf0fdd | 2443 | } |
d1db7359 FR |
2444 | |
2445 | /* Reset labels to handle flag update */ | |
2446 | srp->label_in = 0; | |
2447 | srp->nhlfe.label_out = 0; | |
2448 | srp->sid = index; | |
2449 | srp->flags = desired_flags; | |
2450 | ||
2451 | /* If NO PHP flag is present, compute NHLFE and set label */ | |
2452 | if (no_php_flag) { | |
2453 | srp->label_in = index2label(srp->sid, OspfSR.self->srgb); | |
2454 | srp->nhlfe.label_out = MPLS_LABEL_IMPLICIT_NULL; | |
f786c4d7 | 2455 | } |
7743f2f8 | 2456 | |
b61264a8 | 2457 | osr_debug("SR (%s): Add new index %u to Prefix %pFX", __func__, index, |
d1db7359 | 2458 | (struct prefix *)&srp->prefv4); |
7743f2f8 | 2459 | |
cf9b9f77 OD |
2460 | /* Get Interface and check if it is a Loopback */ |
2461 | ifp = if_lookup_prefix(&p, VRF_DEFAULT); | |
2462 | if (ifp == NULL) { | |
7743f2f8 OD |
2463 | /* |
2464 | * Interface could be not yet available i.e. when this | |
cf9b9f77 OD |
2465 | * command is in the configuration file, OSPF is not yet |
2466 | * ready. In this case, store the prefix SID for latter | |
7743f2f8 OD |
2467 | * update of this Extended Prefix |
2468 | */ | |
d1db7359 FR |
2469 | if (exist == NULL) |
2470 | listnode_add(OspfSR.self->ext_prefix, srp); | |
266469eb | 2471 | zlog_info( |
3efd0893 | 2472 | "Interface for prefix %pFX not found. Deferred LSA flooding", |
b61264a8 | 2473 | &p); |
cf9b9f77 OD |
2474 | return CMD_SUCCESS; |
2475 | } | |
7743f2f8 | 2476 | |
cf9b9f77 OD |
2477 | if (!if_is_loopback(ifp)) { |
2478 | vty_out(vty, "interface %s is not a Loopback\n", ifp->name); | |
d1db7359 | 2479 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); |
cf9b9f77 OD |
2480 | return CMD_WARNING_CONFIG_FAILED; |
2481 | } | |
d1db7359 | 2482 | srp->nhlfe.ifindex = ifp->ifindex; |
cf9b9f77 | 2483 | |
d1db7359 FR |
2484 | /* Add SR Prefix if new */ |
2485 | if (!exist) | |
2486 | listnode_add(OspfSR.self->ext_prefix, srp); | |
7743f2f8 | 2487 | |
270e66a2 | 2488 | /* Update Prefix SID if SR is UP */ |
f786c4d7 | 2489 | if (OspfSR.status == SR_UP) { |
d1db7359 FR |
2490 | if (no_php_flag && !exp_null) |
2491 | ospf_zebra_update_prefix_sid(srp); | |
f786c4d7 | 2492 | } else |
d81b8e0e OD |
2493 | return CMD_SUCCESS; |
2494 | ||
2495 | /* Finally, update Extended Prefix LSA id SR is UP */ | |
d1db7359 FR |
2496 | srp->instance = ospf_ext_schedule_prefix_index( |
2497 | ifp, srp->sid, &srp->prefv4, srp->flags); | |
2498 | if (srp->instance == 0) { | |
96b663a3 MS |
2499 | vty_out(vty, "Unable to set index %u for prefix %pFX\n", |
2500 | index, &p); | |
cf9b9f77 OD |
2501 | return CMD_WARNING; |
2502 | } | |
2503 | ||
2504 | return CMD_SUCCESS; | |
2505 | } | |
2506 | ||
2507 | DEFUN (no_sr_prefix_sid, | |
2508 | no_sr_prefix_sid_cmd, | |
f786c4d7 | 2509 | "no segment-routing prefix A.B.C.D/M [index (0-65535)|no-php-flag|explicit-null]", |
cf9b9f77 OD |
2510 | NO_STR |
2511 | SR_STR | |
2512 | "Prefix SID\n" | |
7743f2f8 OD |
2513 | "IPv4 Prefix as A.B.C.D/M\n" |
2514 | "SID index for this prefix in decimal (0-65535)\n" | |
2515 | "Index value inside SRGB (lower_bound < index < upper_bound)\n" | |
f786c4d7 OD |
2516 | "Don't request Penultimate Hop Popping (PHP)\n" |
2517 | "Upstream neighbor must replace prefix-sid with explicit null label\n") | |
cf9b9f77 | 2518 | { |
7743f2f8 | 2519 | int idx = 0; |
cf9b9f77 OD |
2520 | struct prefix p; |
2521 | struct listnode *node; | |
2522 | struct sr_prefix *srp; | |
2523 | struct interface *ifp; | |
2524 | bool found = false; | |
7743f2f8 | 2525 | int rc; |
cf9b9f77 | 2526 | |
98c06e86 RW |
2527 | if (!ospf_sr_enabled(vty)) |
2528 | return CMD_WARNING_CONFIG_FAILED; | |
2529 | ||
d81b8e0e OD |
2530 | if (OspfSR.status != SR_UP) |
2531 | return CMD_SUCCESS; | |
2532 | ||
cf9b9f77 | 2533 | /* Get network prefix */ |
7743f2f8 OD |
2534 | argv_find(argv, argc, "A.B.C.D/M", &idx); |
2535 | rc = str2prefix(argv[idx]->arg, &p); | |
2536 | if (!rc) { | |
996c9314 | 2537 | vty_out(vty, "Invalid prefix format %s\n", argv[idx]->arg); |
7743f2f8 OD |
2538 | return CMD_WARNING_CONFIG_FAILED; |
2539 | } | |
cf9b9f77 OD |
2540 | |
2541 | /* check that the prefix is already set */ | |
2542 | for (ALL_LIST_ELEMENTS_RO(OspfSR.self->ext_prefix, node, srp)) | |
b37eb79c OD |
2543 | if (IPV4_ADDR_SAME(&srp->prefv4.prefix, &p.u.prefix4) |
2544 | && (srp->prefv4.prefixlen == p.prefixlen)) { | |
cf9b9f77 | 2545 | found = true; |
7743f2f8 OD |
2546 | break; |
2547 | } | |
cf9b9f77 OD |
2548 | |
2549 | if (!found) { | |
2550 | vty_out(vty, "Prefix %s is not found. Abort!\n", | |
7743f2f8 | 2551 | argv[idx]->arg); |
cf9b9f77 OD |
2552 | return CMD_WARNING_CONFIG_FAILED; |
2553 | } | |
2554 | ||
052a0cd0 PG |
2555 | osr_debug("SR (%s): Remove Prefix %pFX with index %u", __func__, |
2556 | (struct prefix *)&srp->prefv4, srp->sid); | |
2557 | ||
7743f2f8 OD |
2558 | /* Get Interface */ |
2559 | ifp = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT); | |
cf9b9f77 OD |
2560 | if (ifp == NULL) { |
2561 | vty_out(vty, "interface for prefix %s not found.\n", | |
7743f2f8 | 2562 | argv[idx]->arg); |
052a0cd0 PG |
2563 | /* silently remove from list */ |
2564 | listnode_delete(OspfSR.self->ext_prefix, srp); | |
2565 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); | |
2566 | return CMD_SUCCESS; | |
cf9b9f77 | 2567 | } |
7743f2f8 | 2568 | |
cf9b9f77 | 2569 | /* Update Extended Prefix LSA */ |
7743f2f8 | 2570 | if (!ospf_ext_schedule_prefix_index(ifp, 0, NULL, 0)) { |
cf9b9f77 OD |
2571 | vty_out(vty, "No corresponding loopback interface. Abort!\n"); |
2572 | return CMD_WARNING; | |
2573 | } | |
2574 | ||
f786c4d7 OD |
2575 | /* Delete NHLFE if NO-PHP is set and EXPLICIT NULL not set */ |
2576 | if (CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_NPFLG) | |
2577 | && !CHECK_FLAG(srp->flags, EXT_SUBTLV_PREFIX_SID_EFLG)) | |
43e7abb5 | 2578 | ospf_zebra_delete_prefix_sid(srp); |
7743f2f8 OD |
2579 | |
2580 | /* OK, all is clean, remove SRP from SRDB */ | |
2581 | listnode_delete(OspfSR.self->ext_prefix, srp); | |
2582 | XFREE(MTYPE_OSPF_SR_PARAMS, srp); | |
2583 | ||
cf9b9f77 OD |
2584 | return CMD_SUCCESS; |
2585 | } | |
2586 | ||
7743f2f8 | 2587 | |
b37eb79c OD |
2588 | static char *sr_op2str(char *buf, size_t size, mpls_label_t label_in, |
2589 | mpls_label_t label_out) | |
2590 | { | |
2591 | if (size < 24) | |
2592 | return NULL; | |
2593 | ||
2594 | switch (label_out) { | |
2595 | case MPLS_LABEL_IMPLICIT_NULL: | |
2596 | snprintf(buf, size, "Pop(%u)", label_in); | |
2597 | break; | |
2598 | case MPLS_LABEL_IPV4_EXPLICIT_NULL: | |
f786c4d7 OD |
2599 | if (label_in == MPLS_LABEL_IPV4_EXPLICIT_NULL) |
2600 | snprintf(buf, size, "no-op."); | |
2601 | else | |
2602 | snprintf(buf, size, "Swap(%u, null)", label_in); | |
b37eb79c OD |
2603 | break; |
2604 | case MPLS_INVALID_LABEL: | |
2605 | snprintf(buf, size, "no-op."); | |
2606 | break; | |
2607 | default: | |
2608 | snprintf(buf, size, "Swap(%u, %u)", label_in, label_out); | |
2609 | break; | |
2610 | } | |
2611 | return buf; | |
2612 | } | |
2613 | ||
2614 | static void show_sr_prefix(struct sbuf *sbuf, struct json_object *json, | |
2615 | struct sr_prefix *srp) | |
2616 | { | |
2617 | ||
2618 | struct listnode *node; | |
2619 | struct ospf_path *path; | |
2620 | struct interface *itf; | |
2621 | json_object *json_route = NULL, *json_obj; | |
2622 | char pref[19]; | |
2623 | char sid[22]; | |
2624 | char op[32]; | |
96b663a3 | 2625 | char buf[PREFIX_STRLEN]; |
b37eb79c OD |
2626 | int indent = 0; |
2627 | ||
b61264a8 | 2628 | snprintfrr(pref, 19, "%pFX", (struct prefix *)&srp->prefv4); |
b37eb79c OD |
2629 | snprintf(sid, 22, "SR Pfx (idx %u)", srp->sid); |
2630 | if (json) { | |
2631 | json_object_string_add(json, "prefix", pref); | |
2632 | json_object_int_add(json, "sid", srp->sid); | |
2633 | json_object_int_add(json, "inputLabel", srp->label_in); | |
2634 | } else { | |
2635 | sbuf_push(sbuf, 0, "%18s %21s ", pref, sid); | |
2636 | } | |
2637 | ||
2638 | /* Check if it is a Local Node SID */ | |
2639 | if (srp->type == LOCAL_SID) { | |
2640 | itf = if_lookup_by_index(srp->nhlfe.ifindex, VRF_DEFAULT); | |
2641 | if (json) { | |
2642 | if (!json_route) { | |
2643 | json_route = json_object_new_array(); | |
2644 | json_object_object_add(json, "prefixRoute", | |
2645 | json_route); | |
2646 | } | |
2647 | json_obj = json_object_new_object(); | |
2648 | json_object_int_add(json_obj, "outputLabel", | |
2649 | srp->nhlfe.label_out); | |
2650 | json_object_string_add(json_obj, "interface", | |
2651 | itf ? itf->name : "-"); | |
20308be3 DA |
2652 | json_object_string_addf(json_obj, "nexthop", "%pI4", |
2653 | &srp->nhlfe.nexthop); | |
b37eb79c OD |
2654 | json_object_array_add(json_route, json_obj); |
2655 | } else { | |
2656 | sbuf_push(sbuf, 0, "%20s %9s %15s\n", | |
b61264a8 OD |
2657 | sr_op2str(op, 32, srp->label_in, |
2658 | srp->nhlfe.label_out), | |
2659 | itf ? itf->name : "-", | |
96b663a3 MS |
2660 | inet_ntop(AF_INET, &srp->nhlfe.nexthop, |
2661 | buf, sizeof(buf))); | |
b37eb79c OD |
2662 | } |
2663 | return; | |
2664 | } | |
2665 | ||
2666 | /* Check if we have a valid path for this prefix */ | |
b61264a8 | 2667 | if (srp->route == NULL) { |
b37eb79c OD |
2668 | if (!json) { |
2669 | sbuf_push(sbuf, 0, "\n"); | |
2670 | } | |
2671 | return; | |
2672 | } | |
2673 | ||
2674 | /* Process list of OSPF paths */ | |
b61264a8 | 2675 | for (ALL_LIST_ELEMENTS_RO(srp->route->paths, node, path)) { |
b37eb79c OD |
2676 | itf = if_lookup_by_index(path->ifindex, VRF_DEFAULT); |
2677 | if (json) { | |
2678 | if (!json_route) { | |
2679 | json_route = json_object_new_array(); | |
2680 | json_object_object_add(json, "prefixRoute", | |
b61264a8 | 2681 | json_route); |
b37eb79c OD |
2682 | } |
2683 | json_obj = json_object_new_object(); | |
2684 | json_object_int_add(json_obj, "outputLabel", | |
b61264a8 | 2685 | path->srni.label_out); |
b37eb79c OD |
2686 | json_object_string_add(json_obj, "interface", |
2687 | itf ? itf->name : "-"); | |
20308be3 DA |
2688 | json_object_string_addf(json_obj, "nexthop", "%pI4", |
2689 | &path->nexthop); | |
b37eb79c OD |
2690 | json_object_array_add(json_route, json_obj); |
2691 | } else { | |
2692 | sbuf_push(sbuf, indent, "%20s %9s %15s\n", | |
b61264a8 OD |
2693 | sr_op2str(op, 32, srp->label_in, |
2694 | path->srni.label_out), | |
2695 | itf ? itf->name : "-", | |
96b663a3 MS |
2696 | inet_ntop(AF_INET, &path->nexthop, buf, |
2697 | sizeof(buf))); | |
b37eb79c OD |
2698 | /* Offset to align information for ECMP */ |
2699 | indent = 43; | |
2700 | } | |
2701 | } | |
2702 | } | |
2703 | ||
76665890 | 2704 | static void show_sr_node(struct vty *vty, struct json_object *json, |
996c9314 | 2705 | struct sr_node *srn) |
cf9b9f77 OD |
2706 | { |
2707 | ||
2708 | struct listnode *node; | |
2709 | struct sr_link *srl; | |
2710 | struct sr_prefix *srp; | |
2711 | struct interface *itf; | |
b37eb79c | 2712 | struct sbuf sbuf; |
76665890 | 2713 | char pref[19]; |
7743f2f8 | 2714 | char sid[22]; |
b37eb79c | 2715 | char op[32]; |
96b663a3 | 2716 | char buf[PREFIX_STRLEN]; |
6f751f14 | 2717 | uint32_t upper; |
76665890 OD |
2718 | json_object *json_node = NULL, *json_algo, *json_obj; |
2719 | json_object *json_prefix = NULL, *json_link = NULL; | |
cf9b9f77 OD |
2720 | |
2721 | /* Sanity Check */ | |
2722 | if (srn == NULL) | |
2723 | return; | |
2724 | ||
b37eb79c OD |
2725 | sbuf_init(&sbuf, NULL, 0); |
2726 | ||
76665890 OD |
2727 | if (json) { |
2728 | json_node = json_object_new_object(); | |
20308be3 DA |
2729 | json_object_string_addf(json_node, "routerID", "%pI4", |
2730 | &srn->adv_router); | |
76665890 | 2731 | json_object_int_add(json_node, "srgbSize", |
996c9314 | 2732 | srn->srgb.range_size); |
76665890 | 2733 | json_object_int_add(json_node, "srgbLabel", |
996c9314 | 2734 | srn->srgb.lower_bound); |
6f751f14 OD |
2735 | json_object_int_add(json_node, "srlbSize", |
2736 | srn->srlb.range_size); | |
2737 | json_object_int_add(json_node, "srlbLabel", | |
2738 | srn->srlb.lower_bound); | |
76665890 OD |
2739 | json_algo = json_object_new_array(); |
2740 | json_object_object_add(json_node, "algorithms", json_algo); | |
2741 | for (int i = 0; i < ALGORITHM_COUNT; i++) { | |
2742 | if (srn->algo[i] == SR_ALGORITHM_UNSET) | |
2743 | continue; | |
2744 | json_obj = json_object_new_object(); | |
2745 | char tmp[2]; | |
2746 | ||
772270f3 | 2747 | snprintf(tmp, sizeof(tmp), "%u", i); |
76665890 | 2748 | json_object_string_add(json_obj, tmp, |
996c9314 LB |
2749 | srn->algo[i] == SR_ALGORITHM_SPF |
2750 | ? "SPF" | |
2751 | : "S-SPF"); | |
76665890 OD |
2752 | json_object_array_add(json_algo, json_obj); |
2753 | } | |
2754 | if (srn->msd != 0) | |
2755 | json_object_int_add(json_node, "nodeMsd", srn->msd); | |
2756 | } else { | |
96b663a3 | 2757 | sbuf_push(&sbuf, 0, "SR-Node: %pI4", &srn->adv_router); |
6f751f14 OD |
2758 | upper = srn->srgb.lower_bound + srn->srgb.range_size - 1; |
2759 | sbuf_push(&sbuf, 0, "\tSRGB: [%u/%u]", | |
2760 | srn->srgb.lower_bound, upper); | |
2761 | upper = srn->srlb.lower_bound + srn->srlb.range_size - 1; | |
2762 | sbuf_push(&sbuf, 0, "\tSRLB: [%u/%u]", | |
2763 | srn->srlb.lower_bound, upper); | |
2764 | sbuf_push(&sbuf, 0, "\tAlgo.(s): %s", | |
b61264a8 | 2765 | srn->algo[0] == SR_ALGORITHM_SPF ? "SPF" : "S-SPF"); |
76665890 OD |
2766 | for (int i = 1; i < ALGORITHM_COUNT; i++) { |
2767 | if (srn->algo[i] == SR_ALGORITHM_UNSET) | |
2768 | continue; | |
b37eb79c | 2769 | sbuf_push(&sbuf, 0, "/%s", |
b61264a8 OD |
2770 | srn->algo[i] == SR_ALGORITHM_SPF ? "SPF" |
2771 | : "S-SPF"); | |
76665890 OD |
2772 | } |
2773 | if (srn->msd != 0) | |
b37eb79c | 2774 | sbuf_push(&sbuf, 0, "\tMSD: %u", srn->msd); |
76665890 OD |
2775 | } |
2776 | ||
2777 | if (!json) { | |
b37eb79c | 2778 | sbuf_push(&sbuf, 0, |
3efd0893 | 2779 | "\n\n Prefix or Link Node or Adj. SID Label Operation Interface Nexthop\n"); |
b61264a8 | 2780 | sbuf_push(&sbuf, 0, |
3efd0893 | 2781 | "------------------ --------------------- -------------------- --------- ---------------\n"); |
76665890 | 2782 | } |
cf9b9f77 | 2783 | for (ALL_LIST_ELEMENTS_RO(srn->ext_prefix, node, srp)) { |
76665890 OD |
2784 | if (json) { |
2785 | if (!json_prefix) { | |
2786 | json_prefix = json_object_new_array(); | |
2787 | json_object_object_add(json_node, | |
996c9314 LB |
2788 | "extendedPrefix", |
2789 | json_prefix); | |
76665890 OD |
2790 | } |
2791 | json_obj = json_object_new_object(); | |
b37eb79c | 2792 | show_sr_prefix(NULL, json_obj, srp); |
76665890 OD |
2793 | json_object_array_add(json_prefix, json_obj); |
2794 | } else { | |
b37eb79c | 2795 | show_sr_prefix(&sbuf, NULL, srp); |
76665890 | 2796 | } |
cf9b9f77 OD |
2797 | } |
2798 | ||
2799 | for (ALL_LIST_ELEMENTS_RO(srn->ext_link, node, srl)) { | |
b61264a8 | 2800 | snprintfrr(pref, 19, "%pI4/32", &srl->itf_addr); |
b37eb79c | 2801 | snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[0]); |
cf9b9f77 | 2802 | itf = if_lookup_by_index(srl->nhlfe[0].ifindex, VRF_DEFAULT); |
76665890 OD |
2803 | if (json) { |
2804 | if (!json_link) { | |
2805 | json_link = json_object_new_array(); | |
996c9314 LB |
2806 | json_object_object_add( |
2807 | json_node, "extendedLink", json_link); | |
76665890 OD |
2808 | } |
2809 | /* Primary Link */ | |
2810 | json_obj = json_object_new_object(); | |
2811 | json_object_string_add(json_obj, "prefix", pref); | |
2812 | json_object_int_add(json_obj, "sid", srl->sid[0]); | |
2813 | json_object_int_add(json_obj, "inputLabel", | |
996c9314 | 2814 | srl->nhlfe[0].label_in); |
b37eb79c OD |
2815 | json_object_int_add(json_obj, "outputLabel", |
2816 | srl->nhlfe[0].label_out); | |
76665890 | 2817 | json_object_string_add(json_obj, "interface", |
996c9314 | 2818 | itf ? itf->name : "-"); |
20308be3 DA |
2819 | json_object_string_addf(json_obj, "nexthop", "%pI4", |
2820 | &srl->nhlfe[0].nexthop); | |
76665890 OD |
2821 | json_object_array_add(json_link, json_obj); |
2822 | /* Backup Link */ | |
2823 | json_obj = json_object_new_object(); | |
b37eb79c | 2824 | snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]); |
76665890 OD |
2825 | json_object_string_add(json_obj, "prefix", pref); |
2826 | json_object_int_add(json_obj, "sid", srl->sid[1]); | |
2827 | json_object_int_add(json_obj, "inputLabel", | |
996c9314 | 2828 | srl->nhlfe[1].label_in); |
b37eb79c OD |
2829 | json_object_int_add(json_obj, "outputLabel", |
2830 | srl->nhlfe[1].label_out); | |
76665890 | 2831 | json_object_string_add(json_obj, "interface", |
996c9314 | 2832 | itf ? itf->name : "-"); |
20308be3 DA |
2833 | json_object_string_addf(json_obj, "nexthop", "%pI4", |
2834 | &srl->nhlfe[1].nexthop); | |
76665890 OD |
2835 | json_object_array_add(json_link, json_obj); |
2836 | } else { | |
b37eb79c | 2837 | sbuf_push(&sbuf, 0, "%18s %21s %20s %9s %15s\n", |
b61264a8 OD |
2838 | pref, sid, |
2839 | sr_op2str(op, 32, srl->nhlfe[0].label_in, | |
2840 | srl->nhlfe[0].label_out), | |
2841 | itf ? itf->name : "-", | |
96b663a3 MS |
2842 | inet_ntop(AF_INET, &srl->nhlfe[0].nexthop, |
2843 | buf, sizeof(buf))); | |
b37eb79c OD |
2844 | snprintf(sid, 22, "SR Adj. (lbl %u)", srl->sid[1]); |
2845 | sbuf_push(&sbuf, 0, "%18s %21s %20s %9s %15s\n", | |
b61264a8 OD |
2846 | pref, sid, |
2847 | sr_op2str(op, 32, srl->nhlfe[1].label_in, | |
2848 | srl->nhlfe[1].label_out), | |
2849 | itf ? itf->name : "-", | |
96b663a3 MS |
2850 | inet_ntop(AF_INET, &srl->nhlfe[1].nexthop, |
2851 | buf, sizeof(buf))); | |
76665890 | 2852 | } |
cf9b9f77 | 2853 | } |
76665890 OD |
2854 | if (json) |
2855 | json_object_array_add(json, json_node); | |
2856 | else | |
b37eb79c OD |
2857 | vty_out(vty, "%s\n", sbuf_buf(&sbuf)); |
2858 | ||
2859 | sbuf_free(&sbuf); | |
cf9b9f77 OD |
2860 | } |
2861 | ||
e3b78da8 | 2862 | static void show_vty_srdb(struct hash_bucket *bucket, void *args) |
cf9b9f77 OD |
2863 | { |
2864 | struct vty *vty = (struct vty *)args; | |
e3b78da8 | 2865 | struct sr_node *srn = (struct sr_node *)bucket->data; |
cf9b9f77 | 2866 | |
76665890 OD |
2867 | show_sr_node(vty, NULL, srn); |
2868 | } | |
2869 | ||
e3b78da8 | 2870 | static void show_json_srdb(struct hash_bucket *bucket, void *args) |
76665890 OD |
2871 | { |
2872 | struct json_object *json = (struct json_object *)args; | |
e3b78da8 | 2873 | struct sr_node *srn = (struct sr_node *)bucket->data; |
76665890 OD |
2874 | |
2875 | show_sr_node(NULL, json, srn); | |
cf9b9f77 OD |
2876 | } |
2877 | ||
2878 | DEFUN (show_ip_opsf_srdb, | |
2879 | show_ip_ospf_srdb_cmd, | |
76665890 | 2880 | "show ip ospf database segment-routing [adv-router A.B.C.D|self-originate] [json]", |
cf9b9f77 OD |
2881 | SHOW_STR |
2882 | IP_STR | |
2883 | OSPF_STR | |
2884 | "Database summary\n" | |
2885 | "Show Segment Routing Data Base\n" | |
2886 | "Advertising SR node\n" | |
2887 | "Advertising SR node ID (as an IP address)\n" | |
76665890 OD |
2888 | "Self-originated SR node\n" |
2889 | JSON_STR) | |
cf9b9f77 | 2890 | { |
7743f2f8 | 2891 | int idx = 0; |
cf9b9f77 OD |
2892 | struct in_addr rid; |
2893 | struct sr_node *srn; | |
088f1098 | 2894 | bool uj = use_json(argc, argv); |
76665890 | 2895 | json_object *json = NULL, *json_node_array = NULL; |
cf9b9f77 | 2896 | |
d81b8e0e | 2897 | if (OspfSR.status == SR_OFF) { |
cf9b9f77 | 2898 | vty_out(vty, "Segment Routing is disabled on this router\n"); |
7726c479 | 2899 | return CMD_WARNING; |
cf9b9f77 OD |
2900 | } |
2901 | ||
76665890 OD |
2902 | if (uj) { |
2903 | json = json_object_new_object(); | |
2904 | json_node_array = json_object_new_array(); | |
20308be3 DA |
2905 | json_object_string_addf(json, "srdbID", "%pI4", |
2906 | &OspfSR.self->adv_router); | |
76665890 OD |
2907 | json_object_object_add(json, "srNodes", json_node_array); |
2908 | } else { | |
2909 | vty_out(vty, | |
96b663a3 MS |
2910 | "\n\t\tOSPF Segment Routing database for ID %pI4\n\n", |
2911 | &OspfSR.self->adv_router); | |
76665890 | 2912 | } |
cf9b9f77 | 2913 | |
7743f2f8 OD |
2914 | if (argv_find(argv, argc, "self-originate", &idx)) { |
2915 | srn = OspfSR.self; | |
76665890 | 2916 | show_sr_node(vty, json_node_array, srn); |
c48349e3 | 2917 | if (uj) |
92ef0078 | 2918 | vty_json(vty, json); |
7743f2f8 OD |
2919 | return CMD_SUCCESS; |
2920 | } | |
2921 | ||
2922 | if (argv_find(argv, argc, "A.B.C.D", &idx)) { | |
2923 | if (!inet_aton(argv[idx]->arg, &rid)) { | |
996c9314 | 2924 | vty_out(vty, "Specified Router ID %s is invalid\n", |
7743f2f8 OD |
2925 | argv[idx]->arg); |
2926 | return CMD_WARNING_CONFIG_FAILED; | |
cf9b9f77 | 2927 | } |
7743f2f8 OD |
2928 | /* Get the SR Node from the SRDB */ |
2929 | srn = (struct sr_node *)hash_lookup(OspfSR.neighbors, | |
2930 | (void *)&rid); | |
76665890 | 2931 | show_sr_node(vty, json_node_array, srn); |
c48349e3 | 2932 | if (uj) |
92ef0078 | 2933 | vty_json(vty, json); |
7743f2f8 | 2934 | return CMD_SUCCESS; |
cf9b9f77 | 2935 | } |
7743f2f8 OD |
2936 | |
2937 | /* No parameters have been provided, Iterate through all the SRDB */ | |
76665890 | 2938 | if (uj) { |
e3b78da8 | 2939 | hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *, |
996c9314 LB |
2940 | void *))show_json_srdb, |
2941 | (void *)json_node_array); | |
92ef0078 | 2942 | vty_json(vty, json); |
76665890 | 2943 | } else { |
e3b78da8 | 2944 | hash_iterate(OspfSR.neighbors, (void (*)(struct hash_bucket *, |
996c9314 LB |
2945 | void *))show_vty_srdb, |
2946 | (void *)vty); | |
76665890 | 2947 | } |
cf9b9f77 OD |
2948 | return CMD_SUCCESS; |
2949 | } | |
2950 | ||
2951 | /* Install new CLI commands */ | |
2952 | void ospf_sr_register_vty(void) | |
2953 | { | |
2954 | install_element(VIEW_NODE, &show_ip_ospf_srdb_cmd); | |
2955 | ||
2956 | install_element(OSPF_NODE, &ospf_sr_enable_cmd); | |
2957 | install_element(OSPF_NODE, &no_ospf_sr_enable_cmd); | |
6f751f14 OD |
2958 | install_element(OSPF_NODE, &sr_global_label_range_cmd); |
2959 | install_element(OSPF_NODE, &no_sr_global_label_range_cmd); | |
cf9b9f77 OD |
2960 | install_element(OSPF_NODE, &sr_node_msd_cmd); |
2961 | install_element(OSPF_NODE, &no_sr_node_msd_cmd); | |
2962 | install_element(OSPF_NODE, &sr_prefix_sid_cmd); | |
2963 | install_element(OSPF_NODE, &no_sr_prefix_sid_cmd); | |
cf9b9f77 | 2964 | } |