]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mpls.c
Merge pull request #8471 from idryzhov/cleanup-num-named-lists
[mirror_frr.git] / zebra / zebra_mpls.c
1 /* Zebra MPLS code
2 * Copyright (C) 2013 Cumulus Networks, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "table.h"
25 #include "memory.h"
26 #include "command.h"
27 #include "if.h"
28 #include "log.h"
29 #include "sockunion.h"
30 #include "linklist.h"
31 #include "thread.h"
32 #include "workqueue.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "stream.h"
36 #include "nexthop.h"
37 #include "termtable.h"
38 #include "lib/json.h"
39
40 #include "zebra/rib.h"
41 #include "zebra/rt.h"
42 #include "zebra/interface.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_vrf.h"
48 #include "zebra/zebra_mpls.h"
49 #include "zebra/zebra_srte.h"
50 #include "zebra/zebra_errors.h"
51
52 DEFINE_MTYPE_STATIC(ZEBRA, LSP, "MPLS LSP object");
53 DEFINE_MTYPE_STATIC(ZEBRA, FEC, "MPLS FEC object");
54 DEFINE_MTYPE_STATIC(ZEBRA, NHLFE, "MPLS nexthop object");
55
56 int mpls_enabled;
57
58 /* static function declarations */
59
60 static void fec_evaluate(struct zebra_vrf *zvrf);
61 static uint32_t fec_derive_label_from_index(struct zebra_vrf *vrf,
62 zebra_fec_t *fec);
63 static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
64 struct route_node *rn, struct route_entry *re);
65 static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label);
66 static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
67 mpls_label_t old_label);
68 static int fec_send(zebra_fec_t *fec, struct zserv *client);
69 static void fec_update_clients(zebra_fec_t *fec);
70 static void fec_print(zebra_fec_t *fec, struct vty *vty);
71 static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p);
72 static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
73 mpls_label_t label, uint32_t flags,
74 uint32_t label_index);
75 static int fec_del(zebra_fec_t *fec);
76
77 static unsigned int label_hash(const void *p);
78 static bool label_cmp(const void *p1, const void *p2);
79 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
80 struct nexthop *nexthop);
81 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
82 struct nexthop *nexthop);
83 static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe);
84
85 static void lsp_select_best_nhlfe(zebra_lsp_t *lsp);
86 static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt);
87 static void lsp_schedule(struct hash_bucket *bucket, void *ctxt);
88 static wq_item_status lsp_process(struct work_queue *wq, void *data);
89 static void lsp_processq_del(struct work_queue *wq, void *data);
90 static void lsp_processq_complete(struct work_queue *wq);
91 static int lsp_processq_add(zebra_lsp_t *lsp);
92 static void *lsp_alloc(void *p);
93
94 /* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
95 static void lsp_check_free(struct hash *lsp_table, zebra_lsp_t **plsp);
96
97 /* Free lsp; sets caller's pointer to NULL */
98 static void lsp_free(struct hash *lsp_table, zebra_lsp_t **plsp);
99
100 static char *nhlfe2str(const zebra_nhlfe_t *nhlfe, char *buf, int size);
101 static char *nhlfe_config_str(const zebra_nhlfe_t *nhlfe, char *buf, int size);
102 static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
103 const union g_addr *gate, ifindex_t ifindex);
104 static zebra_nhlfe_t *nhlfe_find(struct nhlfe_list_head *list,
105 enum lsp_types_t lsp_type,
106 enum nexthop_types_t gtype,
107 const union g_addr *gate, ifindex_t ifindex);
108 static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
109 enum nexthop_types_t gtype,
110 const union g_addr *gate, ifindex_t ifindex,
111 uint8_t num_labels, const mpls_label_t *labels,
112 bool is_backup);
113 static int nhlfe_del(zebra_nhlfe_t *nhlfe);
114 static void nhlfe_free(zebra_nhlfe_t *nhlfe);
115 static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
116 struct mpls_label_stack *nh_label);
117 static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
118 enum lsp_types_t type);
119 static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
120 mpls_label_t in_label);
121 static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty,
122 const char *indent);
123 static void lsp_print(struct vty *vty, zebra_lsp_t *lsp);
124 static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt);
125 static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
126 int afi, enum lsp_types_t lsp_type);
127 static int lsp_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
128 const struct zapi_nexthop *znh);
129 static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
130 const struct zapi_nexthop *znh);
131
132 /* Static functions */
133
134 /*
135 * Handle failure in LSP install, clear flags for NHLFE.
136 */
137 static void clear_nhlfe_installed(zebra_lsp_t *lsp)
138 {
139 zebra_nhlfe_t *nhlfe;
140 struct nexthop *nexthop;
141
142 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
143 nexthop = nhlfe->nexthop;
144 if (!nexthop)
145 continue;
146
147 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
148 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
149 }
150
151 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
152 nexthop = nhlfe->nexthop;
153 if (!nexthop)
154 continue;
155
156 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
157 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
158 }
159 }
160
161 /*
162 * Install label forwarding entry based on labeled-route entry.
163 */
164 static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
165 struct route_node *rn, struct route_entry *re)
166 {
167 struct hash *lsp_table;
168 zebra_ile_t tmp_ile;
169 zebra_lsp_t *lsp;
170 zebra_nhlfe_t *nhlfe;
171 struct nexthop *nexthop;
172 enum lsp_types_t lsp_type;
173 char buf[BUFSIZ];
174 int added, changed;
175
176 /* Lookup table. */
177 lsp_table = zvrf->lsp_table;
178 if (!lsp_table)
179 return -1;
180
181 lsp_type = lsp_type_from_re_type(re->type);
182 added = changed = 0;
183
184 /* Locate or allocate LSP entry. */
185 tmp_ile.in_label = label;
186 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
187 if (!lsp)
188 return -1;
189
190 /* For each active nexthop, create NHLFE. Note that we deliberately skip
191 * recursive nexthops right now, because intermediate hops won't
192 * understand
193 * the label advertised by the recursive nexthop (plus we don't have the
194 * logic yet to push multiple labels).
195 */
196 for (nexthop = re->nhe->nhg.nexthop;
197 nexthop; nexthop = nexthop->next) {
198 /* Skip inactive and recursive entries. */
199 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
200 continue;
201 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
202 continue;
203
204 nhlfe = nhlfe_find(&lsp->nhlfe_list, lsp_type,
205 nexthop->type, &nexthop->gate,
206 nexthop->ifindex);
207 if (nhlfe) {
208 /* Clear deleted flag (in case it was set) */
209 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
210 if (nexthop_labels_match(nhlfe->nexthop, nexthop))
211 /* No change */
212 continue;
213
214
215 if (IS_ZEBRA_DEBUG_MPLS) {
216 nhlfe2str(nhlfe, buf, BUFSIZ);
217 zlog_debug(
218 "LSP in-label %u type %d nexthop %s out-label changed",
219 lsp->ile.in_label, lsp_type, buf);
220 }
221
222 /* Update out label, trigger processing. */
223 nhlfe_out_label_update(nhlfe, nexthop->nh_label);
224 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
225 changed++;
226 } else {
227 /* Add LSP entry to this nexthop */
228 nhlfe = nhlfe_add(lsp, lsp_type, nexthop->type,
229 &nexthop->gate, nexthop->ifindex,
230 nexthop->nh_label->num_labels,
231 nexthop->nh_label->label,
232 false /*backup*/);
233 if (!nhlfe)
234 return -1;
235
236 if (IS_ZEBRA_DEBUG_MPLS) {
237 nhlfe2str(nhlfe, buf, BUFSIZ);
238 zlog_debug(
239 "Add LSP in-label %u type %d nexthop %s out-label %u",
240 lsp->ile.in_label, lsp_type, buf,
241 nexthop->nh_label->label[0]);
242 }
243
244 lsp->addr_family = NHLFE_FAMILY(nhlfe);
245
246 /* Mark NHLFE as changed. */
247 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
248 added++;
249 }
250 }
251
252 /* Queue LSP for processing if necessary. If no NHLFE got added (special
253 * case), delete the LSP entry; this case results in somewhat ugly
254 * logging.
255 */
256 if (added || changed) {
257 if (lsp_processq_add(lsp))
258 return -1;
259 } else {
260 lsp_check_free(lsp_table, &lsp);
261 }
262
263 return 0;
264 }
265
266 /*
267 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
268 * NHLFEs are removed, the entire entry is deleted.
269 */
270 static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label)
271 {
272 struct hash *lsp_table;
273 zebra_ile_t tmp_ile;
274 zebra_lsp_t *lsp;
275 zebra_nhlfe_t *nhlfe;
276 char buf[BUFSIZ];
277
278 /* Lookup table. */
279 lsp_table = zvrf->lsp_table;
280 if (!lsp_table)
281 return -1;
282
283 /* If entry is not present, exit. */
284 tmp_ile.in_label = label;
285 lsp = hash_lookup(lsp_table, &tmp_ile);
286 if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
287 return 0;
288
289 /* Mark NHLFEs for delete or directly delete, as appropriate. */
290 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
291
292 /* Skip static NHLFEs */
293 if (nhlfe->type == ZEBRA_LSP_STATIC)
294 continue;
295
296 if (IS_ZEBRA_DEBUG_MPLS) {
297 nhlfe2str(nhlfe, buf, BUFSIZ);
298 zlog_debug(
299 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
300 label, nhlfe->type, buf, nhlfe->flags);
301 }
302
303 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)) {
304 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
305 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
306 } else {
307 nhlfe_del(nhlfe);
308 }
309 }
310
311 /* Queue LSP for processing, if needed, else delete. */
312 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
313 if (lsp_processq_add(lsp))
314 return -1;
315 } else {
316 lsp_check_free(lsp_table, &lsp);
317 }
318
319 return 0;
320 }
321
322 /*
323 * This function is invoked upon change to label block configuration; it
324 * will walk all registered FECs with label-index and appropriately update
325 * their local labels and trigger client updates.
326 */
327 static void fec_evaluate(struct zebra_vrf *zvrf)
328 {
329 struct route_node *rn;
330 zebra_fec_t *fec;
331 uint32_t old_label, new_label;
332 int af;
333
334 for (af = AFI_IP; af < AFI_MAX; af++) {
335 if (zvrf->fec_table[af] == NULL)
336 continue;
337
338 for (rn = route_top(zvrf->fec_table[af]); rn;
339 rn = route_next(rn)) {
340 if ((fec = rn->info) == NULL)
341 continue;
342
343 /* Skip configured FECs and those without a label index.
344 */
345 if (fec->flags & FEC_FLAG_CONFIGURED
346 || fec->label_index == MPLS_INVALID_LABEL_INDEX)
347 continue;
348
349 /* Save old label, determine new label. */
350 old_label = fec->label;
351 new_label =
352 zvrf->mpls_srgb.start_label + fec->label_index;
353 if (new_label >= zvrf->mpls_srgb.end_label)
354 new_label = MPLS_INVALID_LABEL;
355
356 /* If label has changed, update FEC and clients. */
357 if (new_label == old_label)
358 continue;
359
360 if (IS_ZEBRA_DEBUG_MPLS)
361 zlog_debug(
362 "Update fec %pRN new label %u upon label block",
363 rn, new_label);
364
365 fec->label = new_label;
366 fec_update_clients(fec);
367
368 /* Update label forwarding entries appropriately */
369 fec_change_update_lsp(zvrf, fec, old_label);
370 }
371 }
372 }
373
374 /*
375 * Derive (if possible) and update the local label for the FEC based on
376 * its label index. The index is "acceptable" if it falls within the
377 * globally configured label block (SRGB).
378 */
379 static uint32_t fec_derive_label_from_index(struct zebra_vrf *zvrf,
380 zebra_fec_t *fec)
381 {
382 uint32_t label;
383
384 if (fec->label_index != MPLS_INVALID_LABEL_INDEX
385 && zvrf->mpls_srgb.start_label
386 && ((label = zvrf->mpls_srgb.start_label + fec->label_index)
387 < zvrf->mpls_srgb.end_label))
388 fec->label = label;
389 else
390 fec->label = MPLS_INVALID_LABEL;
391
392 return fec->label;
393 }
394
395 /*
396 * There is a change for this FEC. Install or uninstall label forwarding
397 * entries, as appropriate.
398 */
399 static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
400 mpls_label_t old_label)
401 {
402 struct route_table *table;
403 struct route_node *rn;
404 struct route_entry *re;
405 afi_t afi;
406
407 /* Uninstall label forwarding entry, if previously installed. */
408 if (old_label != MPLS_INVALID_LABEL
409 && old_label != MPLS_LABEL_IMPLICIT_NULL)
410 lsp_uninstall(zvrf, old_label);
411
412 /* Install label forwarding entry corr. to new label, if needed. */
413 if (fec->label == MPLS_INVALID_LABEL
414 || fec->label == MPLS_LABEL_IMPLICIT_NULL)
415 return 0;
416
417 afi = family2afi(PREFIX_FAMILY(&fec->rn->p));
418 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
419 if (!table)
420 return 0;
421
422 /* See if labeled route exists. */
423 rn = route_node_lookup(table, &fec->rn->p);
424 if (!rn)
425 return 0;
426
427 RNODE_FOREACH_RE (rn, re) {
428 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
429 break;
430 }
431
432 if (!re || !zebra_rib_labeled_unicast(re))
433 return 0;
434
435 if (lsp_install(zvrf, fec->label, rn, re))
436 return -1;
437
438 return 0;
439 }
440
441 /*
442 * Inform about FEC to a registered client.
443 */
444 static int fec_send(zebra_fec_t *fec, struct zserv *client)
445 {
446 struct stream *s;
447 struct route_node *rn;
448
449 rn = fec->rn;
450
451 /* Get output stream. */
452 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
453
454 zclient_create_header(s, ZEBRA_FEC_UPDATE, VRF_DEFAULT);
455
456 stream_putw(s, rn->p.family);
457 stream_put_prefix(s, &rn->p);
458 stream_putl(s, fec->label);
459 stream_putw_at(s, 0, stream_get_endp(s));
460 return zserv_send_message(client, s);
461 }
462
463 /*
464 * Update all registered clients about this FEC. Caller should've updated
465 * FEC and ensure no duplicate updates.
466 */
467 static void fec_update_clients(zebra_fec_t *fec)
468 {
469 struct listnode *node;
470 struct zserv *client;
471
472 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client)) {
473 if (IS_ZEBRA_DEBUG_MPLS)
474 zlog_debug("Update client %s",
475 zebra_route_string(client->proto));
476 fec_send(fec, client);
477 }
478 }
479
480
481 /*
482 * Print a FEC-label binding entry.
483 */
484 static void fec_print(zebra_fec_t *fec, struct vty *vty)
485 {
486 struct route_node *rn;
487 struct listnode *node;
488 struct zserv *client;
489 char buf[BUFSIZ];
490
491 rn = fec->rn;
492 vty_out(vty, "%pRN\n", rn);
493 vty_out(vty, " Label: %s", label2str(fec->label, buf, BUFSIZ));
494 if (fec->label_index != MPLS_INVALID_LABEL_INDEX)
495 vty_out(vty, ", Label Index: %u", fec->label_index);
496 vty_out(vty, "\n");
497 if (!list_isempty(fec->client_list)) {
498 vty_out(vty, " Client list:");
499 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client))
500 vty_out(vty, " %s(fd %d)",
501 zebra_route_string(client->proto),
502 client->sock);
503 vty_out(vty, "\n");
504 }
505 }
506
507 /*
508 * Locate FEC-label binding that matches with passed info.
509 */
510 static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p)
511 {
512 struct route_node *rn;
513
514 apply_mask(p);
515 rn = route_node_lookup(table, p);
516 if (!rn)
517 return NULL;
518
519 route_unlock_node(rn);
520 return (rn->info);
521 }
522
523 /*
524 * Add a FEC. This may be upon a client registering for a binding
525 * or when a binding is configured.
526 */
527 static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
528 mpls_label_t label, uint32_t flags,
529 uint32_t label_index)
530 {
531 struct route_node *rn;
532 zebra_fec_t *fec;
533
534 apply_mask(p);
535
536 /* Lookup (or add) route node.*/
537 rn = route_node_get(table, p);
538 if (!rn)
539 return NULL;
540
541 fec = rn->info;
542
543 if (!fec) {
544 fec = XCALLOC(MTYPE_FEC, sizeof(zebra_fec_t));
545
546 rn->info = fec;
547 fec->rn = rn;
548 fec->label = label;
549 fec->client_list = list_new();
550 } else
551 route_unlock_node(rn); /* for the route_node_get */
552
553 fec->label_index = label_index;
554 fec->flags = flags;
555
556 return fec;
557 }
558
559 /*
560 * Delete a FEC. This may be upon the last client deregistering for
561 * a FEC and no binding exists or when the binding is deleted and there
562 * are no registered clients.
563 */
564 static int fec_del(zebra_fec_t *fec)
565 {
566 list_delete(&fec->client_list);
567 fec->rn->info = NULL;
568 route_unlock_node(fec->rn);
569 XFREE(MTYPE_FEC, fec);
570 return 0;
571 }
572
573 /*
574 * Hash function for label.
575 */
576 static unsigned int label_hash(const void *p)
577 {
578 const zebra_ile_t *ile = p;
579
580 return (jhash_1word(ile->in_label, 0));
581 }
582
583 /*
584 * Compare 2 LSP hash entries based on in-label.
585 */
586 static bool label_cmp(const void *p1, const void *p2)
587 {
588 const zebra_ile_t *ile1 = p1;
589 const zebra_ile_t *ile2 = p2;
590
591 return (ile1->in_label == ile2->in_label);
592 }
593
594 /*
595 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
596 * the passed flag.
597 * NOTE: Looking only for connected routes right now.
598 */
599 static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
600 struct nexthop *nexthop)
601 {
602 struct route_table *table;
603 struct prefix_ipv4 p;
604 struct route_node *rn;
605 struct route_entry *match;
606 struct nexthop *match_nh;
607
608 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, nexthop->vrf_id);
609 if (!table)
610 return 0;
611
612 /* Lookup nexthop in IPv4 routing table. */
613 memset(&p, 0, sizeof(struct prefix_ipv4));
614 p.family = AF_INET;
615 p.prefixlen = IPV4_MAX_PREFIXLEN;
616 p.prefix = nexthop->gate.ipv4;
617
618 rn = route_node_match(table, (struct prefix *)&p);
619 if (!rn)
620 return 0;
621
622 route_unlock_node(rn);
623
624 /* Locate a valid connected route. */
625 RNODE_FOREACH_RE (rn, match) {
626 if (CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
627 || !CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
628 continue;
629
630 for (match_nh = match->nhe->nhg.nexthop; match_nh;
631 match_nh = match_nh->next) {
632 if (match->type == ZEBRA_ROUTE_CONNECT
633 || nexthop->ifindex == match_nh->ifindex) {
634 nexthop->ifindex = match_nh->ifindex;
635 return 1;
636 }
637 }
638 }
639
640 return 0;
641 }
642
643
644 /*
645 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
646 * the passed flag.
647 * NOTE: Looking only for connected routes right now.
648 */
649 static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
650 struct nexthop *nexthop)
651 {
652 struct route_table *table;
653 struct prefix_ipv6 p;
654 struct route_node *rn;
655 struct route_entry *match;
656
657 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, nexthop->vrf_id);
658 if (!table)
659 return 0;
660
661 /* Lookup nexthop in IPv6 routing table. */
662 memset(&p, 0, sizeof(struct prefix_ipv6));
663 p.family = AF_INET6;
664 p.prefixlen = IPV6_MAX_PREFIXLEN;
665 p.prefix = nexthop->gate.ipv6;
666
667 rn = route_node_match(table, (struct prefix *)&p);
668 if (!rn)
669 return 0;
670
671 route_unlock_node(rn);
672
673 /* Locate a valid connected route. */
674 RNODE_FOREACH_RE (rn, match) {
675 if ((match->type == ZEBRA_ROUTE_CONNECT)
676 && !CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
677 && CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
678 break;
679 }
680
681 if (!match || !match->nhe->nhg.nexthop)
682 return 0;
683
684 nexthop->ifindex = match->nhe->nhg.nexthop->ifindex;
685 return 1;
686 }
687
688
689 /*
690 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
691 * or not.
692 * NOTE: Each NHLFE points to only 1 nexthop.
693 */
694 static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe)
695 {
696 struct nexthop *nexthop;
697 struct interface *ifp;
698 struct zebra_ns *zns;
699
700 nexthop = nhlfe->nexthop;
701 if (!nexthop) // unexpected
702 return 0;
703
704 /* Check on nexthop based on type. */
705 switch (nexthop->type) {
706 case NEXTHOP_TYPE_IFINDEX:
707 /*
708 * Lookup if this type is special. The
709 * NEXTHOP_TYPE_IFINDEX is a pop and
710 * forward into a different table for
711 * processing. As such this ifindex
712 * passed to us may be a VRF device
713 * which will not be in the default
714 * VRF. So let's look in all of them
715 */
716 zns = zebra_ns_lookup(NS_DEFAULT);
717 ifp = if_lookup_by_index_per_ns(zns, nexthop->ifindex);
718 if (ifp && if_is_operative(ifp))
719 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
720 else
721 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
722 break;
723 case NEXTHOP_TYPE_IPV4:
724 case NEXTHOP_TYPE_IPV4_IFINDEX:
725 if (nhlfe_nexthop_active_ipv4(nhlfe, nexthop))
726 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
727 else
728 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
729 break;
730
731 case NEXTHOP_TYPE_IPV6:
732 if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
733 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
734 else
735 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
736 break;
737
738 case NEXTHOP_TYPE_IPV6_IFINDEX:
739 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
740 ifp = if_lookup_by_index(nexthop->ifindex,
741 nexthop->vrf_id);
742 if (ifp && if_is_operative(ifp))
743 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
744 else
745 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
746 } else {
747 if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
748 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
749 else
750 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
751 }
752 break;
753
754 default:
755 break;
756 }
757
758 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
759 }
760
761 /*
762 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
763 * reachability and select the best. Multipath entries are also
764 * marked. This is invoked when an LSP scheduled for processing (due
765 * to some change) is examined.
766 */
767 static void lsp_select_best_nhlfe(zebra_lsp_t *lsp)
768 {
769 zebra_nhlfe_t *nhlfe;
770 zebra_nhlfe_t *best;
771 struct nexthop *nexthop;
772 int changed = 0;
773
774 if (!lsp)
775 return;
776
777 best = NULL;
778 lsp->num_ecmp = 0;
779 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
780
781 /*
782 * First compute the best path, after checking nexthop status. We are
783 * only concerned with non-deleted NHLFEs.
784 */
785 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
786 /* Clear selection flags. */
787 UNSET_FLAG(nhlfe->flags,
788 (NHLFE_FLAG_SELECTED | NHLFE_FLAG_MULTIPATH));
789
790 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
791 && nhlfe_nexthop_active(nhlfe)) {
792 if (!best || (nhlfe->distance < best->distance))
793 best = nhlfe;
794 }
795 }
796
797 lsp->best_nhlfe = best;
798 if (!lsp->best_nhlfe)
799 return;
800
801 /*
802 * Check the active status of backup nhlfes also
803 */
804 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
805 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
806 (void)nhlfe_nexthop_active(nhlfe);
807 }
808
809 /* Mark best NHLFE as selected. */
810 SET_FLAG(lsp->best_nhlfe->flags, NHLFE_FLAG_SELECTED);
811
812 /*
813 * If best path exists, see if there is ECMP. While doing this, note if
814 * a
815 * new (uninstalled) NHLFE has been selected, an installed entry that is
816 * still selected has a change or an installed entry is to be removed.
817 */
818 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
819 int nh_chg, nh_sel, nh_inst;
820
821 nexthop = nhlfe->nexthop;
822 if (!nexthop) // unexpected
823 continue;
824
825 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
826 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
827 && (nhlfe->distance == lsp->best_nhlfe->distance)) {
828 SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
829 SET_FLAG(nhlfe->flags, NHLFE_FLAG_MULTIPATH);
830 lsp->num_ecmp++;
831 }
832
833 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) && !changed) {
834 nh_chg = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
835 nh_sel = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
836 nh_inst =
837 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
838
839 if ((nh_sel && !nh_inst)
840 || (nh_sel && nh_inst && nh_chg)
841 || (nh_inst && !nh_sel))
842 changed = 1;
843 }
844
845 /* We have finished examining, clear changed flag. */
846 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
847 }
848
849 if (changed)
850 SET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
851 }
852
853 /*
854 * Delete LSP forwarding entry from kernel, if installed. Called upon
855 * process exit.
856 */
857 static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt)
858 {
859 zebra_lsp_t *lsp;
860
861 lsp = (zebra_lsp_t *)bucket->data;
862 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
863 (void)dplane_lsp_delete(lsp);
864 }
865
866 /*
867 * Schedule LSP forwarding entry for processing. Called upon changes
868 * that may impact LSPs such as nexthop / connected route changes.
869 */
870 static void lsp_schedule(struct hash_bucket *bucket, void *ctxt)
871 {
872 zebra_lsp_t *lsp;
873
874 lsp = (zebra_lsp_t *)bucket->data;
875
876 /* In the common flow, this is used when external events occur. For
877 * LSPs with backup nhlfes, we'll assume that the forwarding
878 * plane will use the backups to handle these events, until the
879 * owning protocol can react.
880 */
881 if (ctxt == NULL) {
882 /* Skip LSPs with backups */
883 if (nhlfe_list_first(&lsp->backup_nhlfe_list) != NULL) {
884 if (IS_ZEBRA_DEBUG_MPLS_DETAIL)
885 zlog_debug("%s: skip LSP in-label %u",
886 __func__, lsp->ile.in_label);
887 return;
888 }
889 }
890
891 (void)lsp_processq_add(lsp);
892 }
893
894 /*
895 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
896 * any multipaths and update or delete from the kernel, as needed.
897 */
898 static wq_item_status lsp_process(struct work_queue *wq, void *data)
899 {
900 zebra_lsp_t *lsp;
901 zebra_nhlfe_t *oldbest, *newbest;
902 char buf[BUFSIZ], buf2[BUFSIZ];
903 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
904 enum zebra_dplane_result res;
905
906 lsp = (zebra_lsp_t *)data;
907 if (!lsp) // unexpected
908 return WQ_SUCCESS;
909
910 oldbest = lsp->best_nhlfe;
911
912 /* Select best NHLFE(s) */
913 lsp_select_best_nhlfe(lsp);
914
915 newbest = lsp->best_nhlfe;
916
917 if (IS_ZEBRA_DEBUG_MPLS) {
918 if (oldbest)
919 nhlfe2str(oldbest, buf, sizeof(buf));
920 if (newbest)
921 nhlfe2str(newbest, buf2, sizeof(buf2));
922 zlog_debug(
923 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
924 lsp->ile.in_label, oldbest ? buf : "NULL",
925 newbest ? buf2 : "NULL", lsp->flags, lsp->num_ecmp);
926 }
927
928 if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
929 /* Not already installed */
930 if (newbest) {
931
932 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
933
934 switch (dplane_lsp_add(lsp)) {
935 case ZEBRA_DPLANE_REQUEST_QUEUED:
936 /* Set 'installed' flag so we will know
937 * that an install is in-flight.
938 */
939 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
940
941 zvrf->lsp_installs_queued++;
942 break;
943 case ZEBRA_DPLANE_REQUEST_FAILURE:
944 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
945 "LSP Install Failure: %u",
946 lsp->ile.in_label);
947 break;
948 case ZEBRA_DPLANE_REQUEST_SUCCESS:
949 zvrf->lsp_installs++;
950 break;
951 }
952 }
953 } else {
954 /* Installed, may need an update and/or delete. */
955 if (!newbest) {
956 res = dplane_lsp_delete(lsp);
957
958 /* We do some of the lsp cleanup immediately for
959 * deletes.
960 */
961 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
962 clear_nhlfe_installed(lsp);
963
964 switch (res) {
965 case ZEBRA_DPLANE_REQUEST_QUEUED:
966 zvrf->lsp_removals_queued++;
967 break;
968 case ZEBRA_DPLANE_REQUEST_FAILURE:
969 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
970 "LSP Deletion Failure: %u",
971 lsp->ile.in_label);
972 break;
973 case ZEBRA_DPLANE_REQUEST_SUCCESS:
974 zvrf->lsp_removals++;
975 break;
976 }
977 } else if (CHECK_FLAG(lsp->flags, LSP_FLAG_CHANGED)) {
978 zebra_nhlfe_t *nhlfe;
979 struct nexthop *nexthop;
980
981 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
982
983 /* We leave the INSTALLED flag set here
984 * so we know an update is in-flight.
985 */
986
987 /*
988 * Any NHLFE that was installed but is not
989 * selected now needs to have its flags updated.
990 */
991 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
992 nexthop = nhlfe->nexthop;
993 if (!nexthop)
994 continue;
995
996 if (CHECK_FLAG(nhlfe->flags,
997 NHLFE_FLAG_INSTALLED)
998 && !CHECK_FLAG(nhlfe->flags,
999 NHLFE_FLAG_SELECTED)) {
1000 UNSET_FLAG(nhlfe->flags,
1001 NHLFE_FLAG_INSTALLED);
1002 UNSET_FLAG(nexthop->flags,
1003 NEXTHOP_FLAG_FIB);
1004 }
1005 }
1006
1007 switch (dplane_lsp_update(lsp)) {
1008 case ZEBRA_DPLANE_REQUEST_QUEUED:
1009 zvrf->lsp_installs_queued++;
1010 break;
1011 case ZEBRA_DPLANE_REQUEST_FAILURE:
1012 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
1013 "LSP Update Failure: %u",
1014 lsp->ile.in_label);
1015 break;
1016 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1017 zvrf->lsp_installs++;
1018 break;
1019 }
1020 }
1021 }
1022
1023 return WQ_SUCCESS;
1024 }
1025
1026
1027 /*
1028 * Callback upon processing completion of a LSP forwarding entry.
1029 */
1030 static void lsp_processq_del(struct work_queue *wq, void *data)
1031 {
1032 struct zebra_vrf *zvrf;
1033 zebra_lsp_t *lsp;
1034 struct hash *lsp_table;
1035 zebra_nhlfe_t *nhlfe;
1036
1037 zvrf = vrf_info_lookup(VRF_DEFAULT);
1038 assert(zvrf);
1039
1040 lsp_table = zvrf->lsp_table;
1041 if (!lsp_table) // unexpected
1042 return;
1043
1044 lsp = (zebra_lsp_t *)data;
1045 if (!lsp) // unexpected
1046 return;
1047
1048 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1049 * exist,
1050 * delete LSP entry also.
1051 */
1052 UNSET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
1053
1054 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1055 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
1056 nhlfe_del(nhlfe);
1057 }
1058
1059 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1060 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
1061 nhlfe_del(nhlfe);
1062 }
1063
1064 lsp_check_free(lsp_table, &lsp);
1065 }
1066
1067 /*
1068 * Callback upon finishing the processing of all scheduled
1069 * LSP forwarding entries.
1070 */
1071 static void lsp_processq_complete(struct work_queue *wq)
1072 {
1073 /* Nothing to do for now. */
1074 }
1075
1076 /*
1077 * Add LSP forwarding entry to queue for subsequent processing.
1078 */
1079 static int lsp_processq_add(zebra_lsp_t *lsp)
1080 {
1081 /* If already scheduled, exit. */
1082 if (CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
1083 return 0;
1084
1085 if (zrouter.lsp_process_q == NULL) {
1086 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1087 "%s: work_queue does not exist!", __func__);
1088 return -1;
1089 }
1090
1091 work_queue_add(zrouter.lsp_process_q, lsp);
1092 SET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
1093 return 0;
1094 }
1095
1096 /*
1097 * Callback to allocate LSP forwarding table entry.
1098 */
1099 static void *lsp_alloc(void *p)
1100 {
1101 const zebra_ile_t *ile = p;
1102 zebra_lsp_t *lsp;
1103
1104 lsp = XCALLOC(MTYPE_LSP, sizeof(zebra_lsp_t));
1105 lsp->ile = *ile;
1106 nhlfe_list_init(&lsp->nhlfe_list);
1107 nhlfe_list_init(&lsp->backup_nhlfe_list);
1108
1109 if (IS_ZEBRA_DEBUG_MPLS)
1110 zlog_debug("Alloc LSP in-label %u", lsp->ile.in_label);
1111
1112 return ((void *)lsp);
1113 }
1114
1115 /*
1116 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1117 */
1118 static void lsp_check_free(struct hash *lsp_table, zebra_lsp_t **plsp)
1119 {
1120 zebra_lsp_t *lsp;
1121
1122 if (plsp == NULL || *plsp == NULL)
1123 return;
1124
1125 lsp = *plsp;
1126
1127 if ((nhlfe_list_first(&lsp->nhlfe_list) == NULL) &&
1128 (nhlfe_list_first(&lsp->backup_nhlfe_list) == NULL) &&
1129 !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
1130 lsp_free(lsp_table, plsp);
1131 }
1132
1133 /*
1134 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1135 * free LSP object.
1136 */
1137 static void lsp_free(struct hash *lsp_table, zebra_lsp_t **plsp)
1138 {
1139 zebra_lsp_t *lsp;
1140 zebra_nhlfe_t *nhlfe;
1141
1142 if (plsp == NULL || *plsp == NULL)
1143 return;
1144
1145 lsp = *plsp;
1146
1147 if (IS_ZEBRA_DEBUG_MPLS)
1148 zlog_debug("Free LSP in-label %u flags 0x%x",
1149 lsp->ile.in_label, lsp->flags);
1150
1151 /* Free nhlfes, if any. */
1152 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe)
1153 nhlfe_del(nhlfe);
1154
1155 /* Free backup nhlfes, if any. */
1156 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe)
1157 nhlfe_del(nhlfe);
1158
1159 hash_release(lsp_table, &lsp->ile);
1160 XFREE(MTYPE_LSP, lsp);
1161
1162 *plsp = NULL;
1163 }
1164
1165 /*
1166 * Create printable string for NHLFE entry.
1167 */
1168 static char *nhlfe2str(const zebra_nhlfe_t *nhlfe, char *buf, int size)
1169 {
1170 const struct nexthop *nexthop;
1171
1172 buf[0] = '\0';
1173 nexthop = nhlfe->nexthop;
1174 switch (nexthop->type) {
1175 case NEXTHOP_TYPE_IPV4:
1176 case NEXTHOP_TYPE_IPV4_IFINDEX:
1177 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, size);
1178 break;
1179 case NEXTHOP_TYPE_IPV6:
1180 case NEXTHOP_TYPE_IPV6_IFINDEX:
1181 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, size);
1182 break;
1183 case NEXTHOP_TYPE_IFINDEX:
1184 snprintf(buf, size, "Ifindex: %u", nexthop->ifindex);
1185 default:
1186 break;
1187 }
1188
1189 return buf;
1190 }
1191
1192 /*
1193 * Check if NHLFE matches with search info passed.
1194 */
1195 static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
1196 const union g_addr *gate, ifindex_t ifindex)
1197 {
1198 struct nexthop *nhop;
1199 int cmp = 1;
1200
1201 nhop = nhlfe->nexthop;
1202 if (!nhop)
1203 return 1;
1204
1205 if (nhop->type != gtype)
1206 return 1;
1207
1208 switch (nhop->type) {
1209 case NEXTHOP_TYPE_IPV4:
1210 case NEXTHOP_TYPE_IPV4_IFINDEX:
1211 cmp = memcmp(&(nhop->gate.ipv4), &(gate->ipv4),
1212 sizeof(struct in_addr));
1213 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1214 cmp = !(nhop->ifindex == ifindex);
1215 break;
1216 case NEXTHOP_TYPE_IPV6:
1217 case NEXTHOP_TYPE_IPV6_IFINDEX:
1218 cmp = memcmp(&(nhop->gate.ipv6), &(gate->ipv6),
1219 sizeof(struct in6_addr));
1220 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1221 cmp = !(nhop->ifindex == ifindex);
1222 break;
1223 case NEXTHOP_TYPE_IFINDEX:
1224 cmp = !(nhop->ifindex == ifindex);
1225 break;
1226 default:
1227 break;
1228 }
1229
1230 return cmp;
1231 }
1232
1233
1234 /*
1235 * Locate NHLFE that matches with passed info.
1236 */
1237 static zebra_nhlfe_t *nhlfe_find(struct nhlfe_list_head *list,
1238 enum lsp_types_t lsp_type,
1239 enum nexthop_types_t gtype,
1240 const union g_addr *gate, ifindex_t ifindex)
1241 {
1242 zebra_nhlfe_t *nhlfe;
1243
1244 frr_each_safe(nhlfe_list, list, nhlfe) {
1245 if (nhlfe->type != lsp_type)
1246 continue;
1247 if (!nhlfe_nhop_match(nhlfe, gtype, gate, ifindex))
1248 break;
1249 }
1250
1251 return nhlfe;
1252 }
1253
1254 /*
1255 * Allocate and init new NHLFE.
1256 */
1257 static zebra_nhlfe_t *nhlfe_alloc(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1258 enum nexthop_types_t gtype,
1259 const union g_addr *gate, ifindex_t ifindex,
1260 uint8_t num_labels,
1261 const mpls_label_t *labels)
1262 {
1263 zebra_nhlfe_t *nhlfe;
1264 struct nexthop *nexthop;
1265
1266 assert(lsp);
1267
1268 nhlfe = XCALLOC(MTYPE_NHLFE, sizeof(zebra_nhlfe_t));
1269
1270 nhlfe->lsp = lsp;
1271 nhlfe->type = lsp_type;
1272 nhlfe->distance = lsp_distance(lsp_type);
1273
1274 nexthop = nexthop_new();
1275
1276 nexthop_add_labels(nexthop, lsp_type, num_labels, labels);
1277
1278 nexthop->vrf_id = VRF_DEFAULT;
1279 nexthop->type = gtype;
1280 switch (nexthop->type) {
1281 case NEXTHOP_TYPE_IPV4:
1282 case NEXTHOP_TYPE_IPV4_IFINDEX:
1283 nexthop->gate.ipv4 = gate->ipv4;
1284 if (ifindex)
1285 nexthop->ifindex = ifindex;
1286 break;
1287 case NEXTHOP_TYPE_IPV6:
1288 case NEXTHOP_TYPE_IPV6_IFINDEX:
1289 nexthop->gate.ipv6 = gate->ipv6;
1290 if (ifindex)
1291 nexthop->ifindex = ifindex;
1292 break;
1293 case NEXTHOP_TYPE_IFINDEX:
1294 nexthop->ifindex = ifindex;
1295 break;
1296 default:
1297 nexthop_free(nexthop);
1298 XFREE(MTYPE_NHLFE, nhlfe);
1299 return NULL;
1300 }
1301 nhlfe->nexthop = nexthop;
1302
1303 return nhlfe;
1304 }
1305
1306 /*
1307 * Add primary or backup NHLFE. Base entry must have been created and
1308 * duplicate check done.
1309 */
1310 static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1311 enum nexthop_types_t gtype,
1312 const union g_addr *gate, ifindex_t ifindex,
1313 uint8_t num_labels, const mpls_label_t *labels,
1314 bool is_backup)
1315 {
1316 zebra_nhlfe_t *nhlfe;
1317
1318 if (!lsp)
1319 return NULL;
1320
1321 /* Allocate new object */
1322 nhlfe = nhlfe_alloc(lsp, lsp_type, gtype, gate, ifindex, num_labels,
1323 labels);
1324
1325 /* Enqueue to LSP: primaries at head of list, backups at tail */
1326 if (is_backup) {
1327 SET_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP);
1328 nhlfe_list_add_tail(&lsp->backup_nhlfe_list, nhlfe);
1329 } else
1330 nhlfe_list_add_head(&lsp->nhlfe_list, nhlfe);
1331
1332 return nhlfe;
1333 }
1334
1335 /*
1336 * Common delete for NHLFEs.
1337 */
1338 static void nhlfe_free(zebra_nhlfe_t *nhlfe)
1339 {
1340 if (!nhlfe)
1341 return;
1342
1343 /* Free nexthop. */
1344 if (nhlfe->nexthop)
1345 nexthop_free(nhlfe->nexthop);
1346
1347 nhlfe->nexthop = NULL;
1348
1349 XFREE(MTYPE_NHLFE, nhlfe);
1350 }
1351
1352
1353 /*
1354 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1355 */
1356 static int nhlfe_del(zebra_nhlfe_t *nhlfe)
1357 {
1358 zebra_lsp_t *lsp;
1359
1360 if (!nhlfe)
1361 return -1;
1362
1363 lsp = nhlfe->lsp;
1364 if (!lsp)
1365 return -1;
1366
1367 if (nhlfe == lsp->best_nhlfe)
1368 lsp->best_nhlfe = NULL;
1369
1370 /* Unlink from LSP */
1371 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP))
1372 nhlfe_list_del(&lsp->backup_nhlfe_list, nhlfe);
1373 else
1374 nhlfe_list_del(&lsp->nhlfe_list, nhlfe);
1375
1376 nhlfe->lsp = NULL;
1377
1378 nhlfe_free(nhlfe);
1379
1380 return 0;
1381 }
1382
1383 /*
1384 * Update label for NHLFE entry.
1385 */
1386 static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
1387 struct mpls_label_stack *nh_label)
1388 {
1389 nhlfe->nexthop->nh_label->label[0] = nh_label->label[0];
1390 }
1391
1392 static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
1393 enum lsp_types_t type)
1394 {
1395 zebra_nhlfe_t *nhlfe;
1396 int schedule_lsp = 0;
1397 char buf[BUFSIZ];
1398
1399 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
1400 schedule_lsp = 1;
1401
1402 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1403 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1404 /* Skip non-static NHLFEs */
1405 if (nhlfe->type != type)
1406 continue;
1407
1408 if (IS_ZEBRA_DEBUG_MPLS) {
1409 nhlfe2str(nhlfe, buf, sizeof(buf));
1410 zlog_debug(
1411 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1412 lsp->ile.in_label, type, buf, nhlfe->flags);
1413 }
1414
1415 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
1416 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
1417 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
1418 schedule_lsp = 1;
1419 } else {
1420 nhlfe_del(nhlfe);
1421 }
1422 }
1423
1424 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1425 /* Skip non-static NHLFEs */
1426 if (nhlfe->type != type)
1427 continue;
1428
1429 if (IS_ZEBRA_DEBUG_MPLS) {
1430 nhlfe2str(nhlfe, buf, sizeof(buf));
1431 zlog_debug(
1432 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1433 lsp->ile.in_label, type, buf, nhlfe->flags);
1434 }
1435
1436 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
1437 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
1438 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
1439 schedule_lsp = 1;
1440 } else {
1441 nhlfe_del(nhlfe);
1442 }
1443 }
1444
1445 /* Queue LSP for processing, if needed, else delete. */
1446 if (schedule_lsp) {
1447 if (IS_ZEBRA_DEBUG_MPLS) {
1448 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1449 lsp->ile.in_label, lsp->flags);
1450 }
1451 if (lsp_processq_add(lsp))
1452 return -1;
1453 } else {
1454 lsp_check_free(lsp_table, &lsp);
1455 }
1456
1457 return 0;
1458 }
1459
1460 /*
1461 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1462 * If no other NHLFEs exist, the entry would be deleted.
1463 */
1464 static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
1465 mpls_label_t in_label)
1466 {
1467 struct hash *lsp_table;
1468 zebra_ile_t tmp_ile;
1469 zebra_lsp_t *lsp;
1470
1471 /* Lookup table. */
1472 lsp_table = zvrf->lsp_table;
1473 if (!lsp_table)
1474 return -1;
1475
1476 /* If entry is not present, exit. */
1477 tmp_ile.in_label = in_label;
1478 lsp = hash_lookup(lsp_table, &tmp_ile);
1479 if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
1480 return 0;
1481
1482 return mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_STATIC);
1483 }
1484
1485 static json_object *nhlfe_json(zebra_nhlfe_t *nhlfe)
1486 {
1487 char buf[BUFSIZ];
1488 json_object *json_nhlfe = NULL;
1489 json_object *json_backups = NULL;
1490 json_object *json_label_stack;
1491 struct nexthop *nexthop = nhlfe->nexthop;
1492 int i;
1493
1494 json_nhlfe = json_object_new_object();
1495 json_object_string_add(json_nhlfe, "type", nhlfe_type2str(nhlfe->type));
1496 json_object_int_add(json_nhlfe, "outLabel",
1497 nexthop->nh_label->label[0]);
1498
1499 json_label_stack = json_object_new_array();
1500 json_object_object_add(json_nhlfe, "outLabelStack", json_label_stack);
1501 for (i = 0; i < nexthop->nh_label->num_labels; i++)
1502 json_object_array_add(
1503 json_label_stack,
1504 json_object_new_int(nexthop->nh_label->label[i]));
1505
1506 json_object_int_add(json_nhlfe, "distance", nhlfe->distance);
1507
1508 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
1509 json_object_boolean_true_add(json_nhlfe, "installed");
1510
1511 switch (nexthop->type) {
1512 case NEXTHOP_TYPE_IPV4:
1513 case NEXTHOP_TYPE_IPV4_IFINDEX:
1514 json_object_string_add(json_nhlfe, "nexthop",
1515 inet_ntop(AF_INET, &nexthop->gate.ipv4,
1516 buf, sizeof(buf)));
1517 break;
1518 case NEXTHOP_TYPE_IPV6:
1519 case NEXTHOP_TYPE_IPV6_IFINDEX:
1520 json_object_string_add(
1521 json_nhlfe, "nexthop",
1522 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1523
1524 if (nexthop->ifindex)
1525 json_object_string_add(json_nhlfe, "interface",
1526 ifindex2ifname(nexthop->ifindex,
1527 nexthop->vrf_id));
1528 break;
1529 default:
1530 break;
1531 }
1532
1533 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
1534 json_backups = json_object_new_array();
1535 for (i = 0; i < nexthop->backup_num; i++) {
1536 json_object_array_add(
1537 json_backups,
1538 json_object_new_int(nexthop->backup_idx[i]));
1539 }
1540
1541 json_object_object_add(json_nhlfe, "backupIndex",
1542 json_backups);
1543 }
1544
1545 return json_nhlfe;
1546 }
1547
1548 /*
1549 * Print the NHLFE for a LSP forwarding entry.
1550 */
1551 static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty,
1552 const char *indent)
1553 {
1554 struct nexthop *nexthop;
1555 char buf[MPLS_LABEL_STRLEN];
1556
1557 nexthop = nhlfe->nexthop;
1558 if (!nexthop || !nexthop->nh_label) // unexpected
1559 return;
1560
1561 vty_out(vty, " type: %s remote label: %s distance: %d\n",
1562 nhlfe_type2str(nhlfe->type),
1563 mpls_label2str(nexthop->nh_label->num_labels,
1564 nexthop->nh_label->label,
1565 buf, sizeof(buf), 0),
1566 nhlfe->distance);
1567
1568 if (indent)
1569 vty_out(vty, "%s", indent);
1570
1571 switch (nexthop->type) {
1572 case NEXTHOP_TYPE_IPV4:
1573 case NEXTHOP_TYPE_IPV4_IFINDEX:
1574 vty_out(vty, " via %pI4", &nexthop->gate.ipv4);
1575 if (nexthop->ifindex)
1576 vty_out(vty, " dev %s",
1577 ifindex2ifname(nexthop->ifindex,
1578 nexthop->vrf_id));
1579 break;
1580 case NEXTHOP_TYPE_IPV6:
1581 case NEXTHOP_TYPE_IPV6_IFINDEX:
1582 vty_out(vty, " via %s",
1583 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
1584 sizeof(buf)));
1585 if (nexthop->ifindex)
1586 vty_out(vty, " dev %s",
1587 ifindex2ifname(nexthop->ifindex,
1588 nexthop->vrf_id));
1589 break;
1590 default:
1591 break;
1592 }
1593 vty_out(vty, "%s",
1594 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP) ? " (backup)"
1595 : "");
1596 vty_out(vty, "%s",
1597 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ? " (installed)"
1598 : "");
1599 vty_out(vty, "\n");
1600 }
1601
1602 /*
1603 * Print an LSP forwarding entry.
1604 */
1605 static void lsp_print(struct vty *vty, zebra_lsp_t *lsp)
1606 {
1607 zebra_nhlfe_t *nhlfe, *backup;
1608 int i, j;
1609
1610 vty_out(vty, "Local label: %u%s\n", lsp->ile.in_label,
1611 CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ? " (installed)"
1612 : "");
1613
1614 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1615 nhlfe_print(nhlfe, vty, NULL);
1616
1617 if (nhlfe->nexthop == NULL ||
1618 !CHECK_FLAG(nhlfe->nexthop->flags,
1619 NEXTHOP_FLAG_HAS_BACKUP))
1620 continue;
1621
1622 /* Backup nhlfes: find backups in backup list */
1623
1624 for (j = 0; j < nhlfe->nexthop->backup_num; j++) {
1625 i = 0;
1626 backup = NULL;
1627 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, backup) {
1628 if (i == nhlfe->nexthop->backup_idx[j])
1629 break;
1630 i++;
1631 }
1632
1633 if (backup) {
1634 vty_out(vty, " [backup %d]", i);
1635 nhlfe_print(backup, vty, " ");
1636 }
1637 }
1638 }
1639 }
1640
1641 /*
1642 * JSON objects for an LSP forwarding entry.
1643 */
1644 static json_object *lsp_json(zebra_lsp_t *lsp)
1645 {
1646 zebra_nhlfe_t *nhlfe = NULL;
1647 json_object *json = json_object_new_object();
1648 json_object *json_nhlfe_list = json_object_new_array();
1649
1650 json_object_int_add(json, "inLabel", lsp->ile.in_label);
1651
1652 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
1653 json_object_boolean_true_add(json, "installed");
1654
1655 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe)
1656 json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
1657
1658 json_object_object_add(json, "nexthops", json_nhlfe_list);
1659 json_nhlfe_list = NULL;
1660
1661
1662 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1663 if (json_nhlfe_list == NULL)
1664 json_nhlfe_list = json_object_new_array();
1665
1666 json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
1667 }
1668
1669 if (json_nhlfe_list)
1670 json_object_object_add(json, "backupNexthops", json_nhlfe_list);
1671
1672 return json;
1673 }
1674
1675
1676 /* Return a sorted linked list of the hash contents */
1677 static struct list *hash_get_sorted_list(struct hash *hash, void *cmp)
1678 {
1679 unsigned int i;
1680 struct hash_bucket *hb;
1681 struct list *sorted_list = list_new();
1682
1683 sorted_list->cmp = (int (*)(void *, void *))cmp;
1684
1685 for (i = 0; i < hash->size; i++)
1686 for (hb = hash->index[i]; hb; hb = hb->next)
1687 listnode_add_sort(sorted_list, hb->data);
1688
1689 return sorted_list;
1690 }
1691
1692 /*
1693 * Compare two LSPs based on their label values.
1694 */
1695 static int lsp_cmp(const zebra_lsp_t *lsp1, const zebra_lsp_t *lsp2)
1696 {
1697 if (lsp1->ile.in_label < lsp2->ile.in_label)
1698 return -1;
1699
1700 if (lsp1->ile.in_label > lsp2->ile.in_label)
1701 return 1;
1702
1703 return 0;
1704 }
1705
1706 /*
1707 * Initialize work queue for processing changed LSPs.
1708 */
1709 static int mpls_processq_init(void)
1710 {
1711 zrouter.lsp_process_q = work_queue_new(zrouter.master, "LSP processing");
1712 if (!zrouter.lsp_process_q) {
1713 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1714 "%s: could not initialise work queue!", __func__);
1715 return -1;
1716 }
1717
1718 zrouter.lsp_process_q->spec.workfunc = &lsp_process;
1719 zrouter.lsp_process_q->spec.del_item_data = &lsp_processq_del;
1720 zrouter.lsp_process_q->spec.errorfunc = NULL;
1721 zrouter.lsp_process_q->spec.completion_func = &lsp_processq_complete;
1722 zrouter.lsp_process_q->spec.max_retries = 0;
1723 zrouter.lsp_process_q->spec.hold = 10;
1724
1725 return 0;
1726 }
1727
1728
1729 /*
1730 * Process LSP update results from zebra dataplane.
1731 */
1732 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx *ctx)
1733 {
1734 struct zebra_vrf *zvrf;
1735 mpls_label_t label;
1736 zebra_ile_t tmp_ile;
1737 struct hash *lsp_table;
1738 zebra_lsp_t *lsp;
1739 zebra_nhlfe_t *nhlfe;
1740 struct nexthop *nexthop;
1741 enum dplane_op_e op;
1742 enum zebra_dplane_result status;
1743 enum zebra_sr_policy_update_label_mode update_mode;
1744
1745 op = dplane_ctx_get_op(ctx);
1746 status = dplane_ctx_get_status(ctx);
1747
1748 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1749 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1750 ctx, dplane_op2str(op),
1751 dplane_ctx_get_in_label(ctx),
1752 dplane_res2str(status));
1753
1754 label = dplane_ctx_get_in_label(ctx);
1755
1756 switch (op) {
1757 case DPLANE_OP_LSP_INSTALL:
1758 case DPLANE_OP_LSP_UPDATE:
1759 /* Look for zebra LSP object */
1760 zvrf = vrf_info_lookup(VRF_DEFAULT);
1761 if (zvrf == NULL)
1762 break;
1763
1764 lsp_table = zvrf->lsp_table;
1765
1766 tmp_ile.in_label = label;
1767 lsp = hash_lookup(lsp_table, &tmp_ile);
1768 if (lsp == NULL) {
1769 if (IS_ZEBRA_DEBUG_DPLANE)
1770 zlog_debug("LSP ctx %p: in-label %u not found",
1771 ctx, dplane_ctx_get_in_label(ctx));
1772 break;
1773 }
1774
1775 /* TODO -- Confirm that this result is still 'current' */
1776
1777 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
1778 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1779 clear_nhlfe_installed(lsp);
1780 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
1781 "LSP Install Failure: in-label %u",
1782 lsp->ile.in_label);
1783 break;
1784 }
1785
1786 /* Update zebra object */
1787 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1788 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1789 nexthop = nhlfe->nexthop;
1790 if (!nexthop)
1791 continue;
1792
1793 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) &&
1794 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
1795 SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1796 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1797 }
1798 }
1799
1800 update_mode = (op == DPLANE_OP_LSP_INSTALL)
1801 ? ZEBRA_SR_POLICY_LABEL_CREATED
1802 : ZEBRA_SR_POLICY_LABEL_UPDATED;
1803 zebra_sr_policy_label_update(label, update_mode);
1804 break;
1805
1806 case DPLANE_OP_LSP_DELETE:
1807 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
1808 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
1809 "LSP Deletion Failure: in-label %u",
1810 dplane_ctx_get_in_label(ctx));
1811 break;
1812 }
1813 zebra_sr_policy_label_update(label,
1814 ZEBRA_SR_POLICY_LABEL_REMOVED);
1815 break;
1816
1817 default:
1818 break;
1819
1820 } /* Switch */
1821
1822 dplane_ctx_fini(&ctx);
1823 }
1824
1825 /*
1826 * Process LSP installation info from two sets of nhlfes: a set from
1827 * a dplane notification, and a set from the zebra LSP object. Update
1828 * counters of installed nexthops, and return whether the LSP has changed.
1829 */
1830 static bool compare_notif_nhlfes(const struct nhlfe_list_head *ctx_head,
1831 struct nhlfe_list_head *nhlfe_head,
1832 int *start_counter, int *end_counter)
1833 {
1834 zebra_nhlfe_t *nhlfe;
1835 const zebra_nhlfe_t *ctx_nhlfe;
1836 struct nexthop *nexthop;
1837 const struct nexthop *ctx_nexthop;
1838 int start_count = 0, end_count = 0;
1839 bool changed_p = false;
1840 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
1841
1842 frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
1843 char buf[NEXTHOP_STRLEN];
1844
1845 nexthop = nhlfe->nexthop;
1846 if (!nexthop)
1847 continue;
1848
1849 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1850 start_count++;
1851
1852 ctx_nhlfe = NULL;
1853 ctx_nexthop = NULL;
1854 frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
1855 ctx_nexthop = ctx_nhlfe->nexthop;
1856 if (!ctx_nexthop)
1857 continue;
1858
1859 if ((ctx_nexthop->type == nexthop->type) &&
1860 nexthop_same(ctx_nexthop, nexthop)) {
1861 /* Matched */
1862 break;
1863 }
1864 }
1865
1866 if (is_debug)
1867 nexthop2str(nexthop, buf, sizeof(buf));
1868
1869 if (ctx_nhlfe && ctx_nexthop) {
1870 if (is_debug) {
1871 const char *tstr = "";
1872
1873 if (!CHECK_FLAG(ctx_nhlfe->flags,
1874 NHLFE_FLAG_INSTALLED))
1875 tstr = "not ";
1876
1877 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1878 buf, tstr);
1879 }
1880
1881 /* Test zebra nhlfe install state */
1882 if (CHECK_FLAG(ctx_nhlfe->flags,
1883 NHLFE_FLAG_INSTALLED)) {
1884
1885 if (!CHECK_FLAG(nhlfe->flags,
1886 NHLFE_FLAG_INSTALLED))
1887 changed_p = true;
1888
1889 /* Update counter */
1890 end_count++;
1891 } else {
1892
1893 if (CHECK_FLAG(nhlfe->flags,
1894 NHLFE_FLAG_INSTALLED))
1895 changed_p = true;
1896 }
1897
1898 } else {
1899 /* Not mentioned in lfib set -> uninstalled */
1900 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ||
1901 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) ||
1902 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1903 changed_p = true;
1904 }
1905
1906 if (is_debug)
1907 zlog_debug("LSP dplane notif: no match, nh %s",
1908 buf);
1909 }
1910 }
1911
1912 if (start_counter)
1913 *start_counter += start_count;
1914 if (end_counter)
1915 *end_counter += end_count;
1916
1917 return changed_p;
1918 }
1919
1920 /*
1921 * Update an lsp nhlfe list from a dplane context, typically an async
1922 * notification context. Update the LSP list to match the installed
1923 * status from the context's list.
1924 */
1925 static int update_nhlfes_from_ctx(struct nhlfe_list_head *nhlfe_head,
1926 const struct nhlfe_list_head *ctx_head)
1927 {
1928 int ret = 0;
1929 zebra_nhlfe_t *nhlfe;
1930 const zebra_nhlfe_t *ctx_nhlfe;
1931 struct nexthop *nexthop;
1932 const struct nexthop *ctx_nexthop;
1933 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
1934
1935 frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
1936 char buf[NEXTHOP_STRLEN];
1937
1938 nexthop = nhlfe->nexthop;
1939 if (!nexthop)
1940 continue;
1941
1942 ctx_nhlfe = NULL;
1943 ctx_nexthop = NULL;
1944 frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
1945 ctx_nexthop = ctx_nhlfe->nexthop;
1946 if (!ctx_nexthop)
1947 continue;
1948
1949 if ((ctx_nexthop->type == nexthop->type) &&
1950 nexthop_same(ctx_nexthop, nexthop)) {
1951 /* Matched */
1952 break;
1953 }
1954 }
1955
1956 if (is_debug)
1957 nexthop2str(nexthop, buf, sizeof(buf));
1958
1959 if (ctx_nhlfe && ctx_nexthop) {
1960
1961 /* Bring zebra nhlfe install state into sync */
1962 if (CHECK_FLAG(ctx_nhlfe->flags,
1963 NHLFE_FLAG_INSTALLED)) {
1964 if (is_debug)
1965 zlog_debug("%s: matched lsp nhlfe %s (installed)",
1966 __func__, buf);
1967
1968 SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1969 SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
1970
1971 } else {
1972 if (is_debug)
1973 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
1974 __func__, buf);
1975
1976 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1977 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
1978 }
1979
1980 if (CHECK_FLAG(ctx_nhlfe->nexthop->flags,
1981 NEXTHOP_FLAG_FIB)) {
1982 SET_FLAG(nhlfe->nexthop->flags,
1983 NEXTHOP_FLAG_ACTIVE);
1984 SET_FLAG(nhlfe->nexthop->flags,
1985 NEXTHOP_FLAG_FIB);
1986 } else {
1987 UNSET_FLAG(nhlfe->nexthop->flags,
1988 NEXTHOP_FLAG_ACTIVE);
1989 UNSET_FLAG(nhlfe->nexthop->flags,
1990 NEXTHOP_FLAG_FIB);
1991 }
1992
1993 } else {
1994 /* Not mentioned in lfib set -> uninstalled */
1995 if (is_debug)
1996 zlog_debug("%s: no match for lsp nhlfe %s",
1997 __func__, buf);
1998 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1999 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
2000 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2001 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
2002 }
2003 }
2004
2005 return ret;
2006 }
2007
2008 /*
2009 * Process async dplane notifications.
2010 */
2011 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2012 {
2013 struct zebra_vrf *zvrf;
2014 zebra_ile_t tmp_ile;
2015 struct hash *lsp_table;
2016 zebra_lsp_t *lsp;
2017 const struct nhlfe_list_head *ctx_list;
2018 int start_count = 0, end_count = 0; /* Installed counts */
2019 bool changed_p = false;
2020 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
2021 enum zebra_sr_policy_update_label_mode update_mode;
2022
2023 if (is_debug)
2024 zlog_debug("LSP dplane notif, in-label %u",
2025 dplane_ctx_get_in_label(ctx));
2026
2027 /* Look for zebra LSP object */
2028 zvrf = vrf_info_lookup(VRF_DEFAULT);
2029 if (zvrf == NULL)
2030 goto done;
2031
2032 lsp_table = zvrf->lsp_table;
2033
2034 tmp_ile.in_label = dplane_ctx_get_in_label(ctx);
2035 lsp = hash_lookup(lsp_table, &tmp_ile);
2036 if (lsp == NULL) {
2037 if (is_debug)
2038 zlog_debug("dplane LSP notif: in-label %u not found",
2039 dplane_ctx_get_in_label(ctx));
2040 goto done;
2041 }
2042
2043 /*
2044 * The dataplane/forwarding plane is notifying zebra about the state
2045 * of the nexthops associated with this LSP. First, we take a
2046 * pre-scan pass to determine whether the LSP has transitioned
2047 * from installed -> uninstalled. In that case, we need to have
2048 * the existing state of the LSP objects available before making
2049 * any changes.
2050 */
2051 ctx_list = dplane_ctx_get_nhlfe_list(ctx);
2052
2053 changed_p = compare_notif_nhlfes(ctx_list, &lsp->nhlfe_list,
2054 &start_count, &end_count);
2055
2056 if (is_debug)
2057 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2058 start_count, end_count,
2059 changed_p ? ", changed" : "");
2060
2061 ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
2062
2063 if (compare_notif_nhlfes(ctx_list, &lsp->backup_nhlfe_list,
2064 &start_count, &end_count))
2065 /* Avoid accidentally setting back to 'false' */
2066 changed_p = true;
2067
2068 if (is_debug)
2069 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2070 start_count, end_count,
2071 changed_p ? ", changed" : "");
2072
2073 /*
2074 * Has the LSP become uninstalled? We need the existing state of the
2075 * nexthops/nhlfes at this point so we know what to delete.
2076 */
2077 if (start_count > 0 && end_count == 0) {
2078 /* Inform other lfibs */
2079 dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_DELETE, ctx);
2080 }
2081
2082 /*
2083 * Now we take a second pass and bring the zebra
2084 * nexthop state into sync with the forwarding-plane state.
2085 */
2086 ctx_list = dplane_ctx_get_nhlfe_list(ctx);
2087 update_nhlfes_from_ctx(&lsp->nhlfe_list, ctx_list);
2088
2089 ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
2090 update_nhlfes_from_ctx(&lsp->backup_nhlfe_list, ctx_list);
2091
2092 if (end_count > 0) {
2093 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
2094
2095 /* SR-TE update too */
2096 if (start_count == 0)
2097 update_mode = ZEBRA_SR_POLICY_LABEL_CREATED;
2098 else
2099 update_mode = ZEBRA_SR_POLICY_LABEL_UPDATED;
2100 zebra_sr_policy_label_update(lsp->ile.in_label, update_mode);
2101
2102 if (changed_p)
2103 dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_UPDATE, ctx);
2104
2105 } else {
2106 /* SR-TE update too */
2107 zebra_sr_policy_label_update(lsp->ile.in_label,
2108 ZEBRA_SR_POLICY_LABEL_REMOVED);
2109
2110 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
2111 clear_nhlfe_installed(lsp);
2112 }
2113
2114 done:
2115 dplane_ctx_fini(&ctx);
2116 }
2117
2118 /*
2119 * Install dynamic LSP entry.
2120 */
2121 int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
2122 struct route_entry *re)
2123 {
2124 struct route_table *table;
2125 zebra_fec_t *fec;
2126
2127 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
2128 if (!table)
2129 return -1;
2130
2131 /* See if there is a configured label binding for this FEC. */
2132 fec = fec_find(table, &rn->p);
2133 if (!fec || fec->label == MPLS_INVALID_LABEL)
2134 return 0;
2135
2136 /* We cannot install a label forwarding entry if local label is the
2137 * implicit-null label.
2138 */
2139 if (fec->label == MPLS_LABEL_IMPLICIT_NULL)
2140 return 0;
2141
2142 if (lsp_install(zvrf, fec->label, rn, re))
2143 return -1;
2144
2145 return 0;
2146 }
2147
2148 /*
2149 * Uninstall dynamic LSP entry, if any.
2150 */
2151 int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
2152 struct route_entry *re)
2153 {
2154 struct route_table *table;
2155 zebra_fec_t *fec;
2156
2157 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
2158 if (!table)
2159 return -1;
2160
2161 /* See if there is a configured label binding for this FEC. */
2162 fec = fec_find(table, &rn->p);
2163 if (!fec || fec->label == MPLS_INVALID_LABEL)
2164 return 0;
2165
2166 /* Uninstall always removes all dynamic NHLFEs. */
2167 return lsp_uninstall(zvrf, fec->label);
2168 }
2169
2170 /*
2171 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2172 * the LSP object - nothing is scheduled for processing, for example.
2173 */
2174 zebra_nhlfe_t *zebra_mpls_lsp_add_nhlfe(zebra_lsp_t *lsp,
2175 enum lsp_types_t lsp_type,
2176 enum nexthop_types_t gtype,
2177 const union g_addr *gate,
2178 ifindex_t ifindex,
2179 uint8_t num_labels,
2180 const mpls_label_t *out_labels)
2181 {
2182 /* Just a public pass-through to the internal implementation */
2183 return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
2184 out_labels, false /*backup*/);
2185 }
2186
2187 /*
2188 * Add a backup NHLFE to an LSP, return the newly-added object.
2189 * This path only changes the LSP object - nothing is scheduled for
2190 * processing, for example.
2191 */
2192 zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nhlfe(zebra_lsp_t *lsp,
2193 enum lsp_types_t lsp_type,
2194 enum nexthop_types_t gtype,
2195 const union g_addr *gate,
2196 ifindex_t ifindex,
2197 uint8_t num_labels,
2198 const mpls_label_t *out_labels)
2199 {
2200 /* Just a public pass-through to the internal implementation */
2201 return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
2202 out_labels, true);
2203 }
2204
2205 /*
2206 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2207 */
2208 zebra_nhlfe_t *zebra_mpls_lsp_add_nh(zebra_lsp_t *lsp,
2209 enum lsp_types_t lsp_type,
2210 const struct nexthop *nh)
2211 {
2212 zebra_nhlfe_t *nhlfe;
2213
2214 if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
2215 return NULL;
2216
2217 nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate, nh->ifindex,
2218 nh->nh_label->num_labels, nh->nh_label->label,
2219 false /*backup*/);
2220
2221 return nhlfe;
2222 }
2223
2224 /*
2225 * Add a backup NHLFE to an LSP based on a nexthop;
2226 * return the newly-added object.
2227 */
2228 zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nh(zebra_lsp_t *lsp,
2229 enum lsp_types_t lsp_type,
2230 const struct nexthop *nh)
2231 {
2232 zebra_nhlfe_t *nhlfe;
2233
2234 if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
2235 return NULL;
2236
2237 nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate,
2238 nh->ifindex, nh->nh_label->num_labels,
2239 nh->nh_label->label, true);
2240
2241 return nhlfe;
2242 }
2243
2244 /*
2245 * Free an allocated NHLFE
2246 */
2247 void zebra_mpls_nhlfe_free(zebra_nhlfe_t *nhlfe)
2248 {
2249 /* Just a pass-through to the internal implementation */
2250 nhlfe_free(nhlfe);
2251 }
2252
2253 /*
2254 * Registration from a client for the label binding for a FEC. If a binding
2255 * already exists, it is informed to the client.
2256 * NOTE: If there is a manually configured label binding, that is used.
2257 * Otherwise, if a label index is specified, it means we have to allocate the
2258 * label from a locally configured label block (SRGB), if one exists and index
2259 * is acceptable. If no label index then just register the specified label.
2260 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2261 * by the calling function. Register requests with both will be rejected.
2262 */
2263 int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
2264 uint32_t label, uint32_t label_index,
2265 struct zserv *client)
2266 {
2267 struct route_table *table;
2268 zebra_fec_t *fec;
2269 bool new_client;
2270 bool label_change = false;
2271 uint32_t old_label;
2272 bool have_label_index = (label_index != MPLS_INVALID_LABEL_INDEX);
2273 bool is_configured_fec = false; /* indicate statically configured FEC */
2274
2275 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2276 if (!table)
2277 return -1;
2278
2279 if (label != MPLS_INVALID_LABEL && have_label_index) {
2280 flog_err(
2281 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT,
2282 "Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
2283 p, label, label_index,
2284 zebra_route_string(client->proto));
2285 return -1;
2286 }
2287
2288 /* Locate FEC */
2289 fec = fec_find(table, p);
2290 if (!fec) {
2291 fec = fec_add(table, p, label, 0, label_index);
2292 if (!fec) {
2293 flog_err(
2294 EC_ZEBRA_FEC_ADD_FAILED,
2295 "Failed to add FEC %pFX upon register, client %s",
2296 p, zebra_route_string(client->proto));
2297 return -1;
2298 }
2299
2300 old_label = MPLS_INVALID_LABEL;
2301 new_client = true;
2302 } else {
2303 /* Check if the FEC has been statically defined in the config */
2304 is_configured_fec = fec->flags & FEC_FLAG_CONFIGURED;
2305 /* Client may register same FEC with different label index. */
2306 new_client =
2307 (listnode_lookup(fec->client_list, client) == NULL);
2308 if (!new_client && fec->label_index == label_index
2309 && fec->label == label)
2310 /* Duplicate register */
2311 return 0;
2312
2313 /* Save current label, update the FEC */
2314 old_label = fec->label;
2315 fec->label_index = label_index;
2316 }
2317
2318 if (new_client)
2319 listnode_add(fec->client_list, client);
2320
2321 if (IS_ZEBRA_DEBUG_MPLS)
2322 zlog_debug("FEC %pFX label%s %u %s by client %s%s", p,
2323 have_label_index ? " index" : "",
2324 have_label_index ? label_index : label,
2325 new_client ? "registered" : "updated",
2326 zebra_route_string(client->proto),
2327 is_configured_fec
2328 ? ", but using statically configured label"
2329 : "");
2330
2331 /* If not a statically configured FEC, derive the local label
2332 * from label index or use the provided label
2333 */
2334 if (!is_configured_fec) {
2335 if (have_label_index)
2336 fec_derive_label_from_index(zvrf, fec);
2337 else
2338 fec->label = label;
2339
2340 /* If no label change, exit. */
2341 if (fec->label == old_label)
2342 return 0;
2343
2344 label_change = true;
2345 }
2346
2347 /* If new client or label change, update client and install or uninstall
2348 * label forwarding entry as needed.
2349 */
2350 /* Inform client of label, if needed. */
2351 if ((new_client && fec->label != MPLS_INVALID_LABEL) || label_change) {
2352 if (IS_ZEBRA_DEBUG_MPLS)
2353 zlog_debug("Update client label %u", fec->label);
2354 fec_send(fec, client);
2355 }
2356
2357 if (new_client || label_change)
2358 return fec_change_update_lsp(zvrf, fec, old_label);
2359
2360 return 0;
2361 }
2362
2363 /*
2364 * Deregistration from a client for the label binding for a FEC. The FEC
2365 * itself is deleted if no other registered clients exist and there is no
2366 * label bound to the FEC.
2367 */
2368 int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
2369 struct zserv *client)
2370 {
2371 struct route_table *table;
2372 zebra_fec_t *fec;
2373
2374 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2375 if (!table)
2376 return -1;
2377
2378 fec = fec_find(table, p);
2379 if (!fec) {
2380 flog_err(EC_ZEBRA_FEC_RM_FAILED,
2381 "Failed to find FEC %pFX upon unregister, client %s",
2382 p, zebra_route_string(client->proto));
2383 return -1;
2384 }
2385
2386 listnode_delete(fec->client_list, client);
2387
2388 if (IS_ZEBRA_DEBUG_MPLS)
2389 zlog_debug("FEC %pFX unregistered by client %s", p,
2390 zebra_route_string(client->proto));
2391
2392 /* If not a configured entry, delete the FEC if no other clients. Before
2393 * deleting, see if any LSP needs to be uninstalled.
2394 */
2395 if (!(fec->flags & FEC_FLAG_CONFIGURED)
2396 && list_isempty(fec->client_list)) {
2397 mpls_label_t old_label = fec->label;
2398 fec->label = MPLS_INVALID_LABEL; /* reset */
2399 fec_change_update_lsp(zvrf, fec, old_label);
2400 fec_del(fec);
2401 }
2402
2403 return 0;
2404 }
2405
2406 /*
2407 * Cleanup any FECs registered by this client.
2408 */
2409 static int zebra_mpls_cleanup_fecs_for_client(struct zserv *client)
2410 {
2411 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
2412 struct route_node *rn;
2413 zebra_fec_t *fec;
2414 struct listnode *node;
2415 struct zserv *fec_client;
2416 int af;
2417
2418 for (af = AFI_IP; af < AFI_MAX; af++) {
2419 if (zvrf->fec_table[af] == NULL)
2420 continue;
2421
2422 for (rn = route_top(zvrf->fec_table[af]); rn;
2423 rn = route_next(rn)) {
2424 fec = rn->info;
2425 if (!fec || list_isempty(fec->client_list))
2426 continue;
2427
2428 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node,
2429 fec_client)) {
2430 if (fec_client == client) {
2431 listnode_delete(fec->client_list,
2432 fec_client);
2433 if (!(fec->flags & FEC_FLAG_CONFIGURED)
2434 && list_isempty(fec->client_list))
2435 fec_del(fec);
2436 break;
2437 }
2438 }
2439 }
2440 }
2441
2442 return 0;
2443 }
2444
2445 struct lsp_uninstall_args {
2446 struct hash *lsp_table;
2447 enum lsp_types_t type;
2448 };
2449
2450 /*
2451 * Cleanup MPLS labels registered by this client.
2452 */
2453 static int zebra_mpls_cleanup_zclient_labels(struct zserv *client)
2454 {
2455 struct vrf *vrf;
2456 struct zebra_vrf *zvrf;
2457
2458 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2459 struct lsp_uninstall_args args;
2460
2461 zvrf = vrf->info;
2462 if (!zvrf)
2463 continue;
2464
2465 /* Cleanup LSPs. */
2466 args.lsp_table = zvrf->lsp_table;
2467 args.type = lsp_type_from_re_type(client->proto);
2468 hash_iterate(zvrf->lsp_table, mpls_lsp_uninstall_all_type,
2469 &args);
2470
2471 /* Cleanup FTNs. */
2472 mpls_ftn_uninstall_all(zvrf, AFI_IP,
2473 lsp_type_from_re_type(client->proto));
2474 mpls_ftn_uninstall_all(zvrf, AFI_IP6,
2475 lsp_type_from_re_type(client->proto));
2476 }
2477
2478 return 0;
2479 }
2480
2481 /*
2482 * Return FEC (if any) to which this label is bound.
2483 * Note: Only works for per-prefix binding and when the label is not
2484 * implicit-null.
2485 * TODO: Currently walks entire table, can optimize later with another
2486 * hash..
2487 */
2488 zebra_fec_t *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
2489 mpls_label_t label)
2490 {
2491 struct route_node *rn;
2492 zebra_fec_t *fec;
2493 int af;
2494
2495 for (af = AFI_IP; af < AFI_MAX; af++) {
2496 if (zvrf->fec_table[af] == NULL)
2497 continue;
2498
2499 for (rn = route_top(zvrf->fec_table[af]); rn;
2500 rn = route_next(rn)) {
2501 if (!rn->info)
2502 continue;
2503 fec = rn->info;
2504 if (fec->label == label)
2505 return fec;
2506 }
2507 }
2508
2509 return NULL;
2510 }
2511
2512 /*
2513 * Inform if specified label is currently bound to a FEC or not.
2514 */
2515 int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label)
2516 {
2517 return (zebra_mpls_fec_for_label(zvrf, label) ? 1 : 0);
2518 }
2519
2520 /*
2521 * Add static FEC to label binding. If there are clients registered for this
2522 * FEC, notify them. If there are labeled routes for this FEC, install the
2523 * label forwarding entry.
2524 */
2525 int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
2526 mpls_label_t in_label)
2527 {
2528 struct route_table *table;
2529 zebra_fec_t *fec;
2530 mpls_label_t old_label;
2531 int ret = 0;
2532
2533 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2534 if (!table)
2535 return -1;
2536
2537 /* Update existing FEC or create a new one. */
2538 fec = fec_find(table, p);
2539 if (!fec) {
2540 fec = fec_add(table, p, in_label, FEC_FLAG_CONFIGURED,
2541 MPLS_INVALID_LABEL_INDEX);
2542 if (!fec) {
2543 flog_err(EC_ZEBRA_FEC_ADD_FAILED,
2544 "Failed to add FEC %pFX upon config", p);
2545 return -1;
2546 }
2547
2548 if (IS_ZEBRA_DEBUG_MPLS)
2549 zlog_debug("Add fec %pFX label %u", p, in_label);
2550 } else {
2551 fec->flags |= FEC_FLAG_CONFIGURED;
2552 if (fec->label == in_label)
2553 /* Duplicate config */
2554 return 0;
2555
2556 /* Label change, update clients. */
2557 old_label = fec->label;
2558 if (IS_ZEBRA_DEBUG_MPLS)
2559 zlog_debug("Update fec %pFX new label %u", p, in_label);
2560
2561 fec->label = in_label;
2562 fec_update_clients(fec);
2563
2564 /* Update label forwarding entries appropriately */
2565 ret = fec_change_update_lsp(zvrf, fec, old_label);
2566 }
2567
2568 return ret;
2569 }
2570
2571 /*
2572 * Remove static FEC to label binding. If there are no clients registered
2573 * for this FEC, delete the FEC; else notify clients
2574 * Note: Upon delete of static binding, if label index exists for this FEC,
2575 * client may need to be updated with derived label.
2576 */
2577 int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p)
2578 {
2579 struct route_table *table;
2580 zebra_fec_t *fec;
2581 mpls_label_t old_label;
2582
2583 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2584 if (!table)
2585 return -1;
2586
2587 fec = fec_find(table, p);
2588 if (!fec) {
2589 flog_err(EC_ZEBRA_FEC_RM_FAILED,
2590 "Failed to find FEC %pFX upon delete", p);
2591 return -1;
2592 }
2593
2594 if (IS_ZEBRA_DEBUG_MPLS) {
2595 zlog_debug("Delete fec %pFX label %u label index %u", p,
2596 fec->label, fec->label_index);
2597 }
2598
2599 old_label = fec->label;
2600 fec->flags &= ~FEC_FLAG_CONFIGURED;
2601 fec->label = MPLS_INVALID_LABEL;
2602
2603 /* If no client exists, just delete the FEC. */
2604 if (list_isempty(fec->client_list)) {
2605 fec_del(fec);
2606 return 0;
2607 }
2608
2609 /* Derive the local label (from label index) or reset it. */
2610 fec_derive_label_from_index(zvrf, fec);
2611
2612 /* If there is a label change, update clients. */
2613 if (fec->label == old_label)
2614 return 0;
2615 fec_update_clients(fec);
2616
2617 /* Update label forwarding entries appropriately */
2618 return fec_change_update_lsp(zvrf, fec, old_label);
2619 }
2620
2621 /*
2622 * Display MPLS FEC to label binding configuration (VTY command handler).
2623 */
2624 int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf)
2625 {
2626 struct route_node *rn;
2627 int af;
2628 zebra_fec_t *fec;
2629 int write = 0;
2630
2631 for (af = AFI_IP; af < AFI_MAX; af++) {
2632 if (zvrf->fec_table[af] == NULL)
2633 continue;
2634
2635 for (rn = route_top(zvrf->fec_table[af]); rn;
2636 rn = route_next(rn)) {
2637 if (!rn->info)
2638 continue;
2639
2640 char lstr[BUFSIZ];
2641 fec = rn->info;
2642
2643 if (!(fec->flags & FEC_FLAG_CONFIGURED))
2644 continue;
2645
2646 write = 1;
2647 vty_out(vty, "mpls label bind %pFX %s\n", &rn->p,
2648 label2str(fec->label, lstr, BUFSIZ));
2649 }
2650 }
2651
2652 return write;
2653 }
2654
2655 /*
2656 * Display MPLS FEC to label binding (VTY command handler).
2657 */
2658 void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf)
2659 {
2660 struct route_node *rn;
2661 int af;
2662
2663 for (af = AFI_IP; af < AFI_MAX; af++) {
2664 if (zvrf->fec_table[af] == NULL)
2665 continue;
2666
2667 for (rn = route_top(zvrf->fec_table[af]); rn;
2668 rn = route_next(rn)) {
2669 if (!rn->info)
2670 continue;
2671 fec_print(rn->info, vty);
2672 }
2673 }
2674 }
2675
2676 /*
2677 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2678 */
2679 void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
2680 struct prefix *p)
2681 {
2682 struct route_table *table;
2683 struct route_node *rn;
2684
2685 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2686 if (!table)
2687 return;
2688
2689 apply_mask(p);
2690 rn = route_node_lookup(table, p);
2691 if (!rn)
2692 return;
2693
2694 route_unlock_node(rn);
2695 if (!rn->info)
2696 return;
2697
2698 fec_print(rn->info, vty);
2699 }
2700
2701 static void mpls_zebra_nhe_update(struct route_entry *re, afi_t afi,
2702 struct nhg_hash_entry *new_nhe)
2703 {
2704 struct nhg_hash_entry *nhe;
2705
2706 nhe = zebra_nhg_rib_find_nhe(new_nhe, afi);
2707
2708 route_entry_update_nhe(re, nhe);
2709 }
2710
2711 static bool ftn_update_nexthop(bool add_p, struct nexthop *nexthop,
2712 enum lsp_types_t type,
2713 const struct zapi_nexthop *znh)
2714 {
2715 if (add_p && nexthop->nh_label_type == ZEBRA_LSP_NONE)
2716 nexthop_add_labels(nexthop, type, znh->label_num, znh->labels);
2717 else if (!add_p && nexthop->nh_label_type == type)
2718 nexthop_del_labels(nexthop);
2719 else
2720 return false;
2721
2722 return true;
2723 }
2724
2725 int mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
2726 struct prefix *prefix, uint8_t route_type,
2727 unsigned short route_instance)
2728 {
2729 struct route_table *table;
2730 struct route_node *rn;
2731 struct route_entry *re;
2732 struct nexthop *nexthop;
2733 struct nhg_hash_entry *new_nhe;
2734 afi_t afi = family2afi(prefix->family);
2735
2736 /* Lookup table. */
2737 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2738 if (!table)
2739 return -1;
2740
2741 /* Lookup existing route */
2742 rn = route_node_get(table, prefix);
2743 RNODE_FOREACH_RE (rn, re) {
2744 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2745 continue;
2746 if (re->type == route_type && re->instance == route_instance)
2747 break;
2748 }
2749 if (re == NULL)
2750 return -1;
2751
2752 /*
2753 * Nexthops are now shared by multiple routes, so we have to make
2754 * a local copy, modify the copy, then update the route.
2755 */
2756 new_nhe = zebra_nhe_copy(re->nhe, 0);
2757
2758 for (nexthop = new_nhe->nhg.nexthop; nexthop; nexthop = nexthop->next)
2759 nexthop_del_labels(nexthop);
2760
2761 /* Update backup routes/nexthops also, if present. */
2762 if (zebra_nhg_get_backup_nhg(new_nhe) != NULL) {
2763 for (nexthop = new_nhe->backup_info->nhe->nhg.nexthop; nexthop;
2764 nexthop = nexthop->next)
2765 nexthop_del_labels(nexthop);
2766 }
2767
2768 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2769 SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
2770
2771 /* This will create (or ref) a new nhe, so we will discard the local
2772 * temporary nhe
2773 */
2774 mpls_zebra_nhe_update(re, afi, new_nhe);
2775
2776 zebra_nhg_free(new_nhe);
2777
2778 rib_queue_add(rn);
2779
2780 return 0;
2781 }
2782
2783 /*
2784 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2785 * update its labels according to 'add_p', and return 'true' if successful.
2786 */
2787 static bool ftn_update_znh(bool add_p, enum lsp_types_t type,
2788 struct nexthop *head, const struct zapi_nexthop *znh)
2789 {
2790 bool found = false, success = false;
2791 struct nexthop *nexthop;
2792
2793 for (nexthop = head; nexthop; nexthop = nexthop->next) {
2794 switch (nexthop->type) {
2795 case NEXTHOP_TYPE_IPV4:
2796 case NEXTHOP_TYPE_IPV4_IFINDEX:
2797 if (znh->type != NEXTHOP_TYPE_IPV4
2798 && znh->type != NEXTHOP_TYPE_IPV4_IFINDEX)
2799 continue;
2800 if (!IPV4_ADDR_SAME(&nexthop->gate.ipv4,
2801 &znh->gate.ipv4))
2802 continue;
2803 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
2804 && nexthop->ifindex != znh->ifindex)
2805 continue;
2806
2807 found = true;
2808
2809 if (!ftn_update_nexthop(add_p, nexthop, type, znh))
2810 break;
2811
2812 success = true;
2813 break;
2814 case NEXTHOP_TYPE_IPV6:
2815 case NEXTHOP_TYPE_IPV6_IFINDEX:
2816 if (znh->type != NEXTHOP_TYPE_IPV6
2817 && znh->type != NEXTHOP_TYPE_IPV6_IFINDEX)
2818 continue;
2819 if (!IPV6_ADDR_SAME(&nexthop->gate.ipv6,
2820 &znh->gate.ipv6))
2821 continue;
2822 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
2823 && nexthop->ifindex != znh->ifindex)
2824 continue;
2825
2826 found = true;
2827
2828 if (!ftn_update_nexthop(add_p, nexthop, type, znh))
2829 break;
2830 success = true;
2831 break;
2832 default:
2833 break;
2834 }
2835
2836 if (found)
2837 break;
2838 }
2839
2840 return success;
2841 }
2842
2843 /*
2844 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2845 * using zapi message info.
2846 * There are several changes that need to be made, in several zebra
2847 * data structures, so we want to do all the work required at once.
2848 */
2849 int mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
2850 const struct zapi_labels *zl)
2851 {
2852 int i, counter, ret = 0;
2853 char buf[NEXTHOP_STRLEN];
2854 const struct zapi_nexthop *znh;
2855 struct route_table *table;
2856 struct route_node *rn = NULL;
2857 struct route_entry *re = NULL;
2858 struct nhg_hash_entry *new_nhe = NULL;
2859 bool found;
2860 afi_t afi = AFI_IP;
2861 const struct prefix *prefix = NULL;
2862 struct hash *lsp_table;
2863 zebra_ile_t tmp_ile;
2864 zebra_lsp_t *lsp = NULL;
2865
2866 /* Prep LSP for add case */
2867 if (add_p) {
2868 /* Lookup table. */
2869 lsp_table = zvrf->lsp_table;
2870 if (!lsp_table)
2871 return -1;
2872
2873 /* Find or create LSP object */
2874 tmp_ile.in_label = zl->local_label;
2875 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
2876 if (!lsp)
2877 return -1;
2878 }
2879
2880 /* Prep for route/FEC update if requested */
2881 if (CHECK_FLAG(zl->message, ZAPI_LABELS_FTN)) {
2882 prefix = &zl->route.prefix;
2883
2884 afi = family2afi(prefix->family);
2885
2886 /* Lookup table. */
2887 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2888 if (table) {
2889 /* Lookup existing route */
2890 rn = route_node_get(table, prefix);
2891 RNODE_FOREACH_RE(rn, re) {
2892 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2893 continue;
2894 if (re->type == zl->route.type &&
2895 re->instance == zl->route.instance)
2896 break;
2897 }
2898 }
2899
2900 if (re) {
2901 /*
2902 * Copy over current nexthops into a temporary group.
2903 * We can't just change the values here since the nhgs
2904 * are shared and if the labels change, we'll need
2905 * to find or create a new nhg. We need to create
2906 * a whole temporary group, make changes to it,
2907 * then attach that to the route.
2908 */
2909 new_nhe = zebra_nhe_copy(re->nhe, 0);
2910
2911 } else {
2912 /*
2913 * The old version of the zapi code
2914 * attempted to manage LSPs before trying to
2915 * find a route/FEC, so we'll continue that way.
2916 */
2917 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS)
2918 zlog_debug(
2919 "%s: FTN update requested: no route for prefix %pFX",
2920 __func__, prefix);
2921 }
2922 }
2923
2924 /*
2925 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2926 */
2927
2928 counter = 0;
2929 for (i = 0; i < zl->nexthop_num; i++) {
2930
2931 znh = &zl->nexthops[i];
2932
2933 /* Attempt LSP update */
2934 if (add_p)
2935 ret = lsp_znh_install(lsp, zl->type, znh);
2936 else
2937 ret = mpls_lsp_uninstall(zvrf, zl->type,
2938 zl->local_label, znh->type,
2939 &znh->gate, znh->ifindex,
2940 false);
2941 if (ret < 0) {
2942 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS) {
2943 zapi_nexthop2str(znh, buf, sizeof(buf));
2944 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
2945 __func__, (add_p ? "" : "un"),
2946 zl->local_label, buf);
2947 }
2948 continue;
2949 }
2950
2951 /* Attempt route/FEC update if requested */
2952 if (re == NULL)
2953 continue;
2954
2955 /* Search the route's nexthops for a match, and update it. */
2956 found = ftn_update_znh(add_p, zl->type, new_nhe->nhg.nexthop,
2957 znh);
2958 if (found) {
2959 counter++;
2960 } else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
2961 zapi_nexthop2str(znh, buf, sizeof(buf));
2962 zlog_debug(
2963 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
2964 __func__, prefix, zl->local_label, buf);
2965 }
2966 }
2967
2968 /*
2969 * Process backup LSPs/nexthop entries also. We associate backup
2970 * LSP info with backup nexthops.
2971 */
2972 if (!CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS))
2973 goto znh_done;
2974
2975 for (i = 0; i < zl->backup_nexthop_num; i++) {
2976
2977 znh = &zl->backup_nexthops[i];
2978
2979 if (add_p)
2980 ret = lsp_backup_znh_install(lsp, zl->type, znh);
2981 else
2982 ret = mpls_lsp_uninstall(zvrf, zl->type,
2983 zl->local_label,
2984 znh->type, &znh->gate,
2985 znh->ifindex, true);
2986
2987 if (ret < 0) {
2988 if (IS_ZEBRA_DEBUG_RECV ||
2989 IS_ZEBRA_DEBUG_MPLS) {
2990 zapi_nexthop2str(znh, buf, sizeof(buf));
2991 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
2992 __func__, (add_p ? "" : "un"),
2993 zl->local_label, buf);
2994 }
2995 continue;
2996 }
2997
2998 /* Attempt backup nexthop/FEC update if requested */
2999 if (re == NULL || zebra_nhg_get_backup_nhg(new_nhe) == NULL)
3000 continue;
3001
3002 /* Search the route's backup nexthops for a match
3003 * and update it.
3004 */
3005 found = ftn_update_znh(add_p, zl->type,
3006 new_nhe->backup_info->nhe->nhg.nexthop,
3007 znh);
3008 if (found) {
3009 counter++;
3010 } else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
3011 zapi_nexthop2str(znh, buf, sizeof(buf));
3012 zlog_debug(
3013 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
3014 __func__, prefix, zl->local_label, buf);
3015 }
3016 }
3017
3018 znh_done:
3019
3020 /*
3021 * If we made changes, update the route, and schedule it
3022 * for rib processing
3023 */
3024 if (re != NULL && counter > 0) {
3025 assert(rn != NULL);
3026
3027 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3028 SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
3029
3030 mpls_zebra_nhe_update(re, afi, new_nhe);
3031
3032 rib_queue_add(rn);
3033 }
3034
3035 if (new_nhe)
3036 zebra_nhg_free(new_nhe);
3037
3038 return ret;
3039 }
3040
3041 /*
3042 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3043 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3044 * the out-label for an existing NHLFE (update case).
3045 */
3046 static zebra_nhlfe_t *
3047 lsp_add_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
3048 uint8_t num_out_labels, const mpls_label_t *out_labels,
3049 enum nexthop_types_t gtype, const union g_addr *gate,
3050 ifindex_t ifindex, bool is_backup)
3051 {
3052 zebra_nhlfe_t *nhlfe;
3053 char buf[MPLS_LABEL_STRLEN];
3054 const char *backup_str;
3055
3056 if (is_backup) {
3057 nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
3058 gate, ifindex);
3059 backup_str = "backup ";
3060 } else {
3061 nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
3062 ifindex);
3063 backup_str = "";
3064 }
3065
3066 if (nhlfe) {
3067 struct nexthop *nh = nhlfe->nexthop;
3068
3069 assert(nh);
3070 assert(nh->nh_label);
3071
3072 /* Clear deleted flag (in case it was set) */
3073 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
3074 if (nh->nh_label->num_labels == num_out_labels
3075 && !memcmp(nh->nh_label->label, out_labels,
3076 sizeof(mpls_label_t) * num_out_labels))
3077 /* No change */
3078 return nhlfe;
3079
3080 if (IS_ZEBRA_DEBUG_MPLS) {
3081 char buf2[MPLS_LABEL_STRLEN];
3082 char buf3[MPLS_LABEL_STRLEN];
3083
3084 nhlfe2str(nhlfe, buf, sizeof(buf));
3085 mpls_label2str(num_out_labels, out_labels, buf2,
3086 sizeof(buf2), 0);
3087 mpls_label2str(nh->nh_label->num_labels,
3088 nh->nh_label->label, buf3, sizeof(buf3),
3089 0);
3090
3091 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3092 lsp->ile.in_label, type, backup_str, buf,
3093 buf2, buf3);
3094 }
3095
3096 /* Update out label(s), trigger processing. */
3097 if (nh->nh_label->num_labels == num_out_labels)
3098 memcpy(nh->nh_label->label, out_labels,
3099 sizeof(mpls_label_t) * num_out_labels);
3100 else {
3101 nexthop_del_labels(nh);
3102 nexthop_add_labels(nh, type, num_out_labels,
3103 out_labels);
3104 }
3105 } else {
3106 /* Add LSP entry to this nexthop */
3107 nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex,
3108 num_out_labels, out_labels, is_backup);
3109 if (!nhlfe)
3110 return NULL;
3111
3112 if (IS_ZEBRA_DEBUG_MPLS) {
3113 char buf2[MPLS_LABEL_STRLEN];
3114
3115 nhlfe2str(nhlfe, buf, sizeof(buf));
3116 mpls_label2str(num_out_labels, out_labels, buf2,
3117 sizeof(buf2), 0);
3118
3119 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3120 lsp->ile.in_label, type, backup_str, buf,
3121 buf2);
3122 }
3123
3124 lsp->addr_family = NHLFE_FAMILY(nhlfe);
3125 }
3126
3127 /* Mark NHLFE, queue LSP for processing. */
3128 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
3129
3130 return nhlfe;
3131 }
3132
3133 /*
3134 * Install an LSP and forwarding entry; used primarily
3135 * from vrf zapi message processing.
3136 */
3137 int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
3138 mpls_label_t in_label, uint8_t num_out_labels,
3139 const mpls_label_t *out_labels, enum nexthop_types_t gtype,
3140 const union g_addr *gate, ifindex_t ifindex)
3141 {
3142 struct hash *lsp_table;
3143 zebra_ile_t tmp_ile;
3144 zebra_lsp_t *lsp;
3145 zebra_nhlfe_t *nhlfe;
3146
3147 /* Lookup table. */
3148 lsp_table = zvrf->lsp_table;
3149 if (!lsp_table)
3150 return -1;
3151
3152 /* Find or create LSP object */
3153 tmp_ile.in_label = in_label;
3154 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
3155 if (!lsp)
3156 return -1;
3157
3158 nhlfe = lsp_add_nhlfe(lsp, type, num_out_labels, out_labels, gtype,
3159 gate, ifindex, false /*backup*/);
3160 if (nhlfe == NULL)
3161 return -1;
3162
3163 /* Queue LSP for processing. */
3164 if (lsp_processq_add(lsp))
3165 return -1;
3166
3167 return 0;
3168 }
3169
3170 /*
3171 * Install or replace NHLFE, using info from zapi nexthop
3172 */
3173 static int lsp_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
3174 const struct zapi_nexthop *znh)
3175 {
3176 zebra_nhlfe_t *nhlfe;
3177
3178 nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num, znh->labels,
3179 znh->type, &znh->gate, znh->ifindex,
3180 false /*backup*/);
3181 if (nhlfe == NULL)
3182 return -1;
3183
3184 /* Update backup info if present */
3185 if (CHECK_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
3186 if (znh->backup_num > NEXTHOP_MAX_BACKUPS) {
3187 nhlfe_del(nhlfe);
3188 return -1;
3189 }
3190
3191 nhlfe->nexthop->backup_num = znh->backup_num;
3192 memcpy(nhlfe->nexthop->backup_idx, znh->backup_idx,
3193 znh->backup_num);
3194 SET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
3195 } else {
3196 /* Ensure there's no stale backup info */
3197 UNSET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
3198 nhlfe->nexthop->backup_num = 0;
3199 }
3200
3201 /* Queue LSP for processing. */
3202 if (lsp_processq_add(lsp))
3203 return -1;
3204
3205 return 0;
3206 }
3207
3208 /*
3209 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3210 */
3211 static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
3212 const struct zapi_nexthop *znh)
3213 {
3214 zebra_nhlfe_t *nhlfe;
3215
3216 nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num,
3217 znh->labels, znh->type, &znh->gate,
3218 znh->ifindex, true /*backup*/);
3219 if (nhlfe == NULL) {
3220 if (IS_ZEBRA_DEBUG_MPLS)
3221 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3222 __func__, lsp->ile.in_label);
3223 return -1;
3224 }
3225
3226 /* Queue LSP for processing. */
3227 if (lsp_processq_add(lsp))
3228 return -1;
3229
3230 return 0;
3231 }
3232
3233 zebra_lsp_t *mpls_lsp_find(struct zebra_vrf *zvrf, mpls_label_t in_label)
3234 {
3235 struct hash *lsp_table;
3236 zebra_ile_t tmp_ile;
3237
3238 /* Lookup table. */
3239 lsp_table = zvrf->lsp_table;
3240 if (!lsp_table)
3241 return NULL;
3242
3243 /* If entry is not present, exit. */
3244 tmp_ile.in_label = in_label;
3245 return hash_lookup(lsp_table, &tmp_ile);
3246 }
3247
3248 /*
3249 * Uninstall a particular NHLFE in the forwarding table. If this is
3250 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3251 */
3252 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3253 mpls_label_t in_label, enum nexthop_types_t gtype,
3254 const union g_addr *gate, ifindex_t ifindex,
3255 bool backup_p)
3256 {
3257 struct hash *lsp_table;
3258 zebra_ile_t tmp_ile;
3259 zebra_lsp_t *lsp;
3260 zebra_nhlfe_t *nhlfe;
3261 char buf[NEXTHOP_STRLEN];
3262 bool schedule_lsp = false;
3263
3264 /* Lookup table. */
3265 lsp_table = zvrf->lsp_table;
3266 if (!lsp_table)
3267 return -1;
3268
3269 /* If entry is not present, exit. */
3270 tmp_ile.in_label = in_label;
3271 lsp = hash_lookup(lsp_table, &tmp_ile);
3272 if (!lsp)
3273 return 0;
3274
3275 if (backup_p)
3276 nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
3277 gate, ifindex);
3278 else
3279 nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
3280 ifindex);
3281 if (!nhlfe)
3282 return 0;
3283
3284 if (IS_ZEBRA_DEBUG_MPLS) {
3285 nhlfe2str(nhlfe, buf, sizeof(buf));
3286 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3287 in_label, type, buf, nhlfe->flags);
3288 }
3289
3290 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ||
3291 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
3292 schedule_lsp = true;
3293
3294 /* Mark NHLFE for delete or directly delete, as appropriate. */
3295 if (schedule_lsp) {
3296 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
3297 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
3298
3299 if (IS_ZEBRA_DEBUG_MPLS)
3300 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3301 lsp->ile.in_label, lsp->flags);
3302 if (lsp_processq_add(lsp))
3303 return -1;
3304 } else {
3305 nhlfe_del(nhlfe);
3306
3307 /* Free LSP entry if no other NHLFEs and not scheduled. */
3308 lsp_check_free(lsp_table, &lsp);
3309 }
3310 return 0;
3311 }
3312
3313 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
3314 mpls_label_t in_label)
3315 {
3316 struct hash *lsp_table;
3317 zebra_ile_t tmp_ile;
3318 zebra_lsp_t *lsp;
3319
3320 /* Lookup table. */
3321 lsp_table = zvrf->lsp_table;
3322 if (!lsp_table)
3323 return -1;
3324
3325 /* If entry is not present, exit. */
3326 tmp_ile.in_label = in_label;
3327 lsp = hash_lookup(lsp_table, &tmp_ile);
3328 if (!lsp)
3329 return 0;
3330
3331 return mpls_lsp_uninstall_all(lsp_table, lsp, type);
3332 }
3333
3334 /*
3335 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3336 * If no other NHLFEs exist, the entry would be deleted.
3337 */
3338 static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt)
3339 {
3340 struct lsp_uninstall_args *args = ctxt;
3341 zebra_lsp_t *lsp;
3342 struct hash *lsp_table;
3343
3344 lsp = (zebra_lsp_t *)bucket->data;
3345 if (nhlfe_list_first(&lsp->nhlfe_list) == NULL)
3346 return;
3347
3348 lsp_table = args->lsp_table;
3349 if (!lsp_table)
3350 return;
3351
3352 mpls_lsp_uninstall_all(lsp_table, lsp, args->type);
3353 }
3354
3355 /*
3356 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3357 * LSP type.
3358 */
3359 static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
3360 int afi, enum lsp_types_t lsp_type)
3361 {
3362 struct route_table *table;
3363 struct route_node *rn;
3364 struct route_entry *re;
3365 struct nexthop *nexthop;
3366 struct nexthop_group *nhg;
3367 bool update;
3368
3369 /* Process routes of interested address-families. */
3370 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
3371 if (!table)
3372 return;
3373
3374 for (rn = route_top(table); rn; rn = route_next(rn)) {
3375 update = false;
3376
3377 RNODE_FOREACH_RE (rn, re) {
3378 struct nhg_hash_entry *new_nhe;
3379
3380 new_nhe = zebra_nhe_copy(re->nhe, 0);
3381
3382 nhg = &new_nhe->nhg;
3383 for (nexthop = nhg->nexthop; nexthop;
3384 nexthop = nexthop->next) {
3385 if (nexthop->nh_label_type != lsp_type)
3386 continue;
3387
3388 nexthop_del_labels(nexthop);
3389 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3390 SET_FLAG(re->status,
3391 ROUTE_ENTRY_LABELS_CHANGED);
3392 update = true;
3393 }
3394
3395 /* Check for backup info and update that also */
3396 nhg = zebra_nhg_get_backup_nhg(new_nhe);
3397 if (nhg != NULL) {
3398 for (nexthop = nhg->nexthop; nexthop;
3399 nexthop = nexthop->next) {
3400 if (nexthop->nh_label_type != lsp_type)
3401 continue;
3402
3403 nexthop_del_labels(nexthop);
3404 SET_FLAG(re->status,
3405 ROUTE_ENTRY_CHANGED);
3406 SET_FLAG(re->status,
3407 ROUTE_ENTRY_LABELS_CHANGED);
3408 update = true;
3409 }
3410 }
3411
3412 if (CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
3413 mpls_zebra_nhe_update(re, afi, new_nhe);
3414
3415 zebra_nhg_free(new_nhe);
3416 }
3417
3418 if (update)
3419 rib_queue_add(rn);
3420 }
3421 }
3422
3423 #if defined(HAVE_CUMULUS)
3424 /*
3425 * Check that the label values used in LSP creation are consistent. The
3426 * main criteria is that if there is ECMP, the label operation must still
3427 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3428 * to current HW restrictions.
3429 */
3430 int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
3431 mpls_label_t in_label,
3432 mpls_label_t out_label,
3433 enum nexthop_types_t gtype,
3434 union g_addr *gate, ifindex_t ifindex)
3435 {
3436 struct hash *slsp_table;
3437 zebra_ile_t tmp_ile;
3438 zebra_lsp_t *lsp;
3439 zebra_nhlfe_t *nhlfe;
3440 const struct nexthop *nh;
3441
3442 /* Lookup table. */
3443 slsp_table = zvrf->slsp_table;
3444 if (!slsp_table)
3445 return 0;
3446
3447 /* If entry is not present, exit. */
3448 tmp_ile.in_label = in_label;
3449 lsp = hash_lookup(slsp_table, &tmp_ile);
3450 if (!lsp)
3451 return 1;
3452
3453 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
3454 gtype, gate, ifindex);
3455 if (nhlfe) {
3456 nh = nhlfe->nexthop;
3457
3458 if (nh == NULL || nh->nh_label == NULL)
3459 return 0;
3460
3461 if (nh->nh_label->label[0] == out_label)
3462 return 1;
3463
3464 /* If not only NHLFE, cannot allow label change. */
3465 if (nhlfe != nhlfe_list_first(&lsp->nhlfe_list) ||
3466 nhlfe_list_next(&lsp->nhlfe_list, nhlfe) != NULL)
3467 return 0;
3468 } else {
3469 /* If other NHLFEs exist, label operation must match. */
3470 nhlfe = nhlfe_list_first(&lsp->nhlfe_list);
3471 if (nhlfe != NULL) {
3472 int cur_op, new_op;
3473
3474 nh = nhlfe->nexthop;
3475
3476 if (nh == NULL || nh->nh_label == NULL)
3477 return 0;
3478
3479 cur_op = (nh->nh_label->label[0] ==
3480 MPLS_LABEL_IMPLICIT_NULL);
3481 new_op = (out_label == MPLS_LABEL_IMPLICIT_NULL);
3482 if (cur_op != new_op)
3483 return 0;
3484 }
3485 }
3486
3487 /* Label values are good. */
3488 return 1;
3489 }
3490 #endif /* HAVE_CUMULUS */
3491
3492 /*
3493 * Add static LSP entry. This may be the first entry for this incoming label
3494 * or an additional nexthop; an existing entry may also have outgoing label
3495 * changed.
3496 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3497 * NHLFEs).
3498 */
3499 int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
3500 mpls_label_t out_label,
3501 enum nexthop_types_t gtype, union g_addr *gate,
3502 ifindex_t ifindex)
3503 {
3504 struct hash *slsp_table;
3505 zebra_ile_t tmp_ile;
3506 zebra_lsp_t *lsp;
3507 zebra_nhlfe_t *nhlfe;
3508 char buf[BUFSIZ];
3509
3510 /* Lookup table. */
3511 slsp_table = zvrf->slsp_table;
3512 if (!slsp_table)
3513 return -1;
3514
3515 /* Find or create LSP. */
3516 tmp_ile.in_label = in_label;
3517 lsp = hash_get(slsp_table, &tmp_ile, lsp_alloc);
3518 if (!lsp)
3519 return -1;
3520
3521 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC, gtype, gate,
3522 ifindex);
3523 if (nhlfe) {
3524 struct nexthop *nh = nhlfe->nexthop;
3525
3526 assert(nh);
3527 assert(nh->nh_label);
3528
3529 /* Compare existing nexthop */
3530 if (nh->nh_label->num_labels == 1 &&
3531 nh->nh_label->label[0] == out_label)
3532 /* No change */
3533 return 0;
3534
3535 if (IS_ZEBRA_DEBUG_MPLS) {
3536 nhlfe2str(nhlfe, buf, sizeof(buf));
3537 zlog_debug(
3538 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3539 in_label, buf, out_label,
3540 nh->nh_label->label[0]);
3541 }
3542 if (nh->nh_label->num_labels == 1)
3543 nh->nh_label->label[0] = out_label;
3544 else {
3545 nexthop_del_labels(nh);
3546 nexthop_add_labels(nh, ZEBRA_LSP_STATIC, 1, &out_label);
3547 }
3548
3549 } else {
3550 /* Add static LSP entry to this nexthop */
3551 nhlfe = nhlfe_add(lsp, ZEBRA_LSP_STATIC, gtype, gate,
3552 ifindex, 1, &out_label, false /*backup*/);
3553 if (!nhlfe)
3554 return -1;
3555
3556 if (IS_ZEBRA_DEBUG_MPLS) {
3557 nhlfe2str(nhlfe, buf, sizeof(buf));
3558 zlog_debug(
3559 "Add static LSP in-label %u nexthop %s out-label %u",
3560 in_label, buf, out_label);
3561 }
3562 }
3563
3564 /* (Re)Install LSP in the main table. */
3565 if (mpls_lsp_install(zvrf, ZEBRA_LSP_STATIC, in_label, 1, &out_label,
3566 gtype, gate, ifindex))
3567 return -1;
3568
3569 return 0;
3570 }
3571
3572 /*
3573 * Delete static LSP entry. This may be the delete of one particular
3574 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3575 * all NHLFEs).
3576 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3577 * LSP configuration.
3578 */
3579 int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
3580 enum nexthop_types_t gtype, union g_addr *gate,
3581 ifindex_t ifindex)
3582 {
3583 struct hash *slsp_table;
3584 zebra_ile_t tmp_ile;
3585 zebra_lsp_t *lsp;
3586 zebra_nhlfe_t *nhlfe;
3587
3588 /* Lookup table. */
3589 slsp_table = zvrf->slsp_table;
3590 if (!slsp_table)
3591 return -1;
3592
3593 /* If entry is not present, exit. */
3594 tmp_ile.in_label = in_label;
3595 lsp = hash_lookup(slsp_table, &tmp_ile);
3596 if (!lsp)
3597 return 0;
3598
3599 /* Is it delete of entire LSP or a specific NHLFE? */
3600 if (gtype == NEXTHOP_TYPE_BLACKHOLE) {
3601 if (IS_ZEBRA_DEBUG_MPLS)
3602 zlog_debug("Del static LSP in-label %u", in_label);
3603
3604 /* Uninstall entire LSP from the main table. */
3605 mpls_static_lsp_uninstall_all(zvrf, in_label);
3606
3607 /* Delete all static NHLFEs */
3608 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3609 nhlfe_del(nhlfe);
3610 }
3611 } else {
3612 /* Find specific NHLFE, exit if not found. */
3613 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
3614 gtype, gate, ifindex);
3615 if (!nhlfe)
3616 return 0;
3617
3618 if (IS_ZEBRA_DEBUG_MPLS) {
3619 char buf[BUFSIZ];
3620 nhlfe2str(nhlfe, buf, sizeof(buf));
3621 zlog_debug("Del static LSP in-label %u nexthop %s",
3622 in_label, buf);
3623 }
3624
3625 /* Uninstall LSP from the main table. */
3626 mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
3627 gate, ifindex, false);
3628
3629 /* Delete static LSP NHLFE */
3630 nhlfe_del(nhlfe);
3631 }
3632
3633 /* Remove entire static LSP entry if no NHLFE - valid in either case
3634 * above.
3635 */
3636 if (nhlfe_list_first(&lsp->nhlfe_list) == NULL) {
3637 lsp = hash_release(slsp_table, &tmp_ile);
3638 XFREE(MTYPE_LSP, lsp);
3639 }
3640
3641 return 0;
3642 }
3643
3644 /*
3645 * Schedule all MPLS label forwarding entries for processing.
3646 * Called upon changes that may affect one or more of them such as
3647 * interface or nexthop state changes.
3648 */
3649 void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf)
3650 {
3651 if (!zvrf)
3652 return;
3653 hash_iterate(zvrf->lsp_table, lsp_schedule, NULL);
3654 }
3655
3656 /*
3657 * Display MPLS label forwarding table for a specific LSP
3658 * (VTY command handler).
3659 */
3660 void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
3661 mpls_label_t label, bool use_json)
3662 {
3663 struct hash *lsp_table;
3664 zebra_lsp_t *lsp;
3665 zebra_ile_t tmp_ile;
3666 json_object *json = NULL;
3667
3668 /* Lookup table. */
3669 lsp_table = zvrf->lsp_table;
3670 if (!lsp_table)
3671 return;
3672
3673 /* If entry is not present, exit. */
3674 tmp_ile.in_label = label;
3675 lsp = hash_lookup(lsp_table, &tmp_ile);
3676 if (!lsp)
3677 return;
3678
3679 if (use_json) {
3680 json = lsp_json(lsp);
3681 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3682 json, JSON_C_TO_STRING_PRETTY));
3683 json_object_free(json);
3684 } else
3685 lsp_print(vty, lsp);
3686 }
3687
3688 /*
3689 * Display MPLS label forwarding table (VTY command handler).
3690 */
3691 void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
3692 bool use_json)
3693 {
3694 char buf[BUFSIZ];
3695 json_object *json = NULL;
3696 zebra_lsp_t *lsp = NULL;
3697 zebra_nhlfe_t *nhlfe = NULL;
3698 struct listnode *node = NULL;
3699 struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
3700
3701 if (use_json) {
3702 json = json_object_new_object();
3703
3704 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
3705 json_object_object_add(
3706 json, label2str(lsp->ile.in_label, buf,
3707 sizeof(buf)),
3708 lsp_json(lsp));
3709
3710 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3711 json, JSON_C_TO_STRING_PRETTY));
3712 json_object_free(json);
3713 } else {
3714 struct ttable *tt;
3715
3716 /* Prepare table. */
3717 tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
3718 ttable_add_row(tt, "Inbound Label|Type|Nexthop|Outbound Label");
3719 tt->style.cell.rpad = 2;
3720 tt->style.corner = '+';
3721 ttable_restyle(tt);
3722 ttable_rowseps(tt, 0, BOTTOM, true, '-');
3723
3724 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
3725 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3726 struct nexthop *nexthop;
3727 const char *out_label_str;
3728 char nh_buf[NEXTHOP_STRLEN];
3729
3730 nexthop = nhlfe->nexthop;
3731
3732 switch (nexthop->type) {
3733 case NEXTHOP_TYPE_IFINDEX: {
3734 struct zebra_ns *zns;
3735 struct interface *ifp;
3736
3737 zns = zebra_ns_lookup(NS_DEFAULT);
3738 ifp = if_lookup_by_index_per_ns(
3739 zns, nexthop->ifindex);
3740 snprintf(nh_buf, sizeof(nh_buf), "%s",
3741 ifp ? ifp->name : "Null");
3742 break;
3743 }
3744 case NEXTHOP_TYPE_IPV4:
3745 case NEXTHOP_TYPE_IPV4_IFINDEX:
3746 inet_ntop(AF_INET, &nexthop->gate.ipv4,
3747 nh_buf, sizeof(nh_buf));
3748 break;
3749 case NEXTHOP_TYPE_IPV6:
3750 case NEXTHOP_TYPE_IPV6_IFINDEX:
3751 inet_ntop(AF_INET6, &nexthop->gate.ipv6,
3752 nh_buf, sizeof(nh_buf));
3753 break;
3754 default:
3755 break;
3756 }
3757
3758 if (nexthop->type != NEXTHOP_TYPE_IFINDEX)
3759 out_label_str = mpls_label2str(
3760 nexthop->nh_label->num_labels,
3761 &nexthop->nh_label->label[0],
3762 buf, sizeof(buf), 1);
3763 else
3764 out_label_str = "-";
3765
3766 ttable_add_row(tt, "%u|%s|%s|%s",
3767 lsp->ile.in_label,
3768 nhlfe_type2str(nhlfe->type),
3769 nh_buf, out_label_str);
3770 }
3771 }
3772
3773 /* Dump the generated table. */
3774 if (tt->nrows > 1) {
3775 char *table = ttable_dump(tt, "\n");
3776 vty_out(vty, "%s\n", table);
3777 XFREE(MTYPE_TMP, table);
3778 }
3779 ttable_del(tt);
3780 }
3781
3782 list_delete(&lsp_list);
3783 }
3784
3785 /*
3786 * Create printable string for static LSP configuration.
3787 */
3788 static char *nhlfe_config_str(const zebra_nhlfe_t *nhlfe, char *buf, int size)
3789 {
3790 const struct nexthop *nh;
3791
3792 nh = nhlfe->nexthop;
3793
3794 buf[0] = '\0';
3795 switch (nh->type) {
3796 case NEXTHOP_TYPE_IPV4:
3797 inet_ntop(AF_INET, &nh->gate.ipv4, buf, size);
3798 break;
3799 case NEXTHOP_TYPE_IPV6:
3800 case NEXTHOP_TYPE_IPV6_IFINDEX:
3801 inet_ntop(AF_INET6, &nh->gate.ipv6, buf, size);
3802 if (nh->ifindex)
3803 strlcat(buf,
3804 ifindex2ifname(nh->ifindex, VRF_DEFAULT),
3805 size);
3806 break;
3807 default:
3808 break;
3809 }
3810
3811 return buf;
3812 }
3813
3814 /*
3815 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3816 */
3817 int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf)
3818 {
3819 zebra_lsp_t *lsp;
3820 zebra_nhlfe_t *nhlfe;
3821 struct nexthop *nh;
3822 struct listnode *node;
3823 struct list *slsp_list =
3824 hash_get_sorted_list(zvrf->slsp_table, lsp_cmp);
3825
3826 for (ALL_LIST_ELEMENTS_RO(slsp_list, node, lsp)) {
3827 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3828 char buf[BUFSIZ];
3829 char lstr[30];
3830
3831 nh = nhlfe->nexthop;
3832 if (nh == NULL || nh->nh_label == NULL)
3833 continue;
3834
3835 nhlfe_config_str(nhlfe, buf, sizeof(buf));
3836
3837 switch (nh->nh_label->label[0]) {
3838 case MPLS_LABEL_IPV4_EXPLICIT_NULL:
3839 case MPLS_LABEL_IPV6_EXPLICIT_NULL:
3840 strlcpy(lstr, "explicit-null", sizeof(lstr));
3841 break;
3842 case MPLS_LABEL_IMPLICIT_NULL:
3843 strlcpy(lstr, "implicit-null", sizeof(lstr));
3844 break;
3845 default:
3846 snprintf(lstr, sizeof(lstr), "%u",
3847 nh->nh_label->label[0]);
3848 break;
3849 }
3850
3851 vty_out(vty, "mpls lsp %u %s %s\n", lsp->ile.in_label,
3852 buf, lstr);
3853 }
3854 }
3855
3856 list_delete(&slsp_list);
3857 return (zvrf->slsp_table->count ? 1 : 0);
3858 }
3859
3860 /*
3861 * Add/update global label block.
3862 */
3863 int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, uint32_t start_label,
3864 uint32_t end_label)
3865 {
3866 zvrf->mpls_srgb.start_label = start_label;
3867 zvrf->mpls_srgb.end_label = end_label;
3868
3869 /* Evaluate registered FECs to see if any get a label or not. */
3870 fec_evaluate(zvrf);
3871 return 0;
3872 }
3873
3874 /*
3875 * Delete global label block.
3876 */
3877 int zebra_mpls_label_block_del(struct zebra_vrf *zvrf)
3878 {
3879 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
3880 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
3881
3882 /* Process registered FECs to clear their local label, if needed. */
3883 fec_evaluate(zvrf);
3884 return 0;
3885 }
3886
3887 /*
3888 * Display MPLS global label block configuration (VTY command handler).
3889 */
3890 int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *zvrf)
3891 {
3892 if (zvrf->mpls_srgb.start_label == 0)
3893 return 0;
3894
3895 if ((zvrf->mpls_srgb.start_label != MPLS_DEFAULT_MIN_SRGB_LABEL)
3896 || (zvrf->mpls_srgb.end_label != MPLS_DEFAULT_MAX_SRGB_LABEL)) {
3897 vty_out(vty, "mpls label global-block %u %u\n",
3898 zvrf->mpls_srgb.start_label, zvrf->mpls_srgb.end_label);
3899 }
3900
3901 return 1;
3902 }
3903
3904 /*
3905 * Called when VRF becomes inactive, cleans up information but keeps
3906 * the table itself.
3907 */
3908 void zebra_mpls_cleanup_tables(struct zebra_vrf *zvrf)
3909 {
3910 struct zebra_vrf *def_zvrf;
3911 afi_t afi;
3912
3913 if (zvrf_id(zvrf) == VRF_DEFAULT)
3914 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
3915 else {
3916 /*
3917 * For other vrfs, we try to remove associated LSPs; we locate
3918 * the LSPs in the default vrf.
3919 */
3920 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
3921
3922 /* At shutdown, the default may be gone already */
3923 if (def_zvrf == NULL)
3924 return;
3925
3926 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3927 if (zvrf->label[afi] != MPLS_LABEL_NONE)
3928 lsp_uninstall(def_zvrf, zvrf->label[afi]);
3929 }
3930 }
3931 }
3932
3933 /*
3934 * Called upon process exiting, need to delete LSP forwarding
3935 * entries from the kernel.
3936 * NOTE: Currently supported only for default VRF.
3937 */
3938 void zebra_mpls_close_tables(struct zebra_vrf *zvrf)
3939 {
3940 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
3941 hash_clean(zvrf->lsp_table, NULL);
3942 hash_free(zvrf->lsp_table);
3943 hash_clean(zvrf->slsp_table, NULL);
3944 hash_free(zvrf->slsp_table);
3945 route_table_finish(zvrf->fec_table[AFI_IP]);
3946 route_table_finish(zvrf->fec_table[AFI_IP6]);
3947 }
3948
3949 /*
3950 * Allocate MPLS tables for this VRF and do other initialization.
3951 * NOTE: Currently supported only for default VRF.
3952 */
3953 void zebra_mpls_init_tables(struct zebra_vrf *zvrf)
3954 {
3955 if (!zvrf)
3956 return;
3957 zvrf->slsp_table =
3958 hash_create(label_hash, label_cmp, "ZEBRA SLSP table");
3959 zvrf->lsp_table = hash_create(label_hash, label_cmp, "ZEBRA LSP table");
3960 zvrf->fec_table[AFI_IP] = route_table_init();
3961 zvrf->fec_table[AFI_IP6] = route_table_init();
3962 zvrf->mpls_flags = 0;
3963 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
3964 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
3965 }
3966
3967 /*
3968 * Global MPLS initialization.
3969 */
3970 void zebra_mpls_init(void)
3971 {
3972 mpls_enabled = 0;
3973
3974 if (mpls_kernel_init() < 0) {
3975 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED,
3976 "Disabling MPLS support (no kernel support)");
3977 return;
3978 }
3979
3980 if (!mpls_processq_init())
3981 mpls_enabled = 1;
3982
3983 hook_register(zserv_client_close, zebra_mpls_cleanup_fecs_for_client);
3984 hook_register(zserv_client_close, zebra_mpls_cleanup_zclient_labels);
3985 }