]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mpls.c
zebra: kill zebra_memory.h, use MTYPE_STATIC
[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 (void)lsp_processq_add(lsp);
876 }
877
878 /*
879 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
880 * any multipaths and update or delete from the kernel, as needed.
881 */
882 static wq_item_status lsp_process(struct work_queue *wq, void *data)
883 {
884 zebra_lsp_t *lsp;
885 zebra_nhlfe_t *oldbest, *newbest;
886 char buf[BUFSIZ], buf2[BUFSIZ];
887 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
888 enum zebra_dplane_result res;
889
890 lsp = (zebra_lsp_t *)data;
891 if (!lsp) // unexpected
892 return WQ_SUCCESS;
893
894 oldbest = lsp->best_nhlfe;
895
896 /* Select best NHLFE(s) */
897 lsp_select_best_nhlfe(lsp);
898
899 newbest = lsp->best_nhlfe;
900
901 if (IS_ZEBRA_DEBUG_MPLS) {
902 if (oldbest)
903 nhlfe2str(oldbest, buf, sizeof(buf));
904 if (newbest)
905 nhlfe2str(newbest, buf2, sizeof(buf2));
906 zlog_debug(
907 "Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
908 lsp->ile.in_label, oldbest ? buf : "NULL",
909 newbest ? buf2 : "NULL", lsp->flags, lsp->num_ecmp);
910 }
911
912 if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
913 /* Not already installed */
914 if (newbest) {
915
916 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
917
918 switch (dplane_lsp_add(lsp)) {
919 case ZEBRA_DPLANE_REQUEST_QUEUED:
920 /* Set 'installed' flag so we will know
921 * that an install is in-flight.
922 */
923 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
924
925 zvrf->lsp_installs_queued++;
926 break;
927 case ZEBRA_DPLANE_REQUEST_FAILURE:
928 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
929 "LSP Install Failure: %u",
930 lsp->ile.in_label);
931 break;
932 case ZEBRA_DPLANE_REQUEST_SUCCESS:
933 zvrf->lsp_installs++;
934 break;
935 }
936 }
937 } else {
938 /* Installed, may need an update and/or delete. */
939 if (!newbest) {
940 res = dplane_lsp_delete(lsp);
941
942 /* We do some of the lsp cleanup immediately for
943 * deletes.
944 */
945 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
946 clear_nhlfe_installed(lsp);
947
948 switch (res) {
949 case ZEBRA_DPLANE_REQUEST_QUEUED:
950 zvrf->lsp_removals_queued++;
951 break;
952 case ZEBRA_DPLANE_REQUEST_FAILURE:
953 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
954 "LSP Deletion Failure: %u",
955 lsp->ile.in_label);
956 break;
957 case ZEBRA_DPLANE_REQUEST_SUCCESS:
958 zvrf->lsp_removals++;
959 break;
960 }
961 } else if (CHECK_FLAG(lsp->flags, LSP_FLAG_CHANGED)) {
962 zebra_nhlfe_t *nhlfe;
963 struct nexthop *nexthop;
964
965 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
966
967 /* We leave the INSTALLED flag set here
968 * so we know an update is in-flight.
969 */
970
971 /*
972 * Any NHLFE that was installed but is not
973 * selected now needs to have its flags updated.
974 */
975 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
976 nexthop = nhlfe->nexthop;
977 if (!nexthop)
978 continue;
979
980 if (CHECK_FLAG(nhlfe->flags,
981 NHLFE_FLAG_INSTALLED)
982 && !CHECK_FLAG(nhlfe->flags,
983 NHLFE_FLAG_SELECTED)) {
984 UNSET_FLAG(nhlfe->flags,
985 NHLFE_FLAG_INSTALLED);
986 UNSET_FLAG(nexthop->flags,
987 NEXTHOP_FLAG_FIB);
988 }
989 }
990
991 switch (dplane_lsp_update(lsp)) {
992 case ZEBRA_DPLANE_REQUEST_QUEUED:
993 zvrf->lsp_installs_queued++;
994 break;
995 case ZEBRA_DPLANE_REQUEST_FAILURE:
996 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
997 "LSP Update Failure: %u",
998 lsp->ile.in_label);
999 break;
1000 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1001 zvrf->lsp_installs++;
1002 break;
1003 }
1004 }
1005 }
1006
1007 return WQ_SUCCESS;
1008 }
1009
1010
1011 /*
1012 * Callback upon processing completion of a LSP forwarding entry.
1013 */
1014 static void lsp_processq_del(struct work_queue *wq, void *data)
1015 {
1016 struct zebra_vrf *zvrf;
1017 zebra_lsp_t *lsp;
1018 struct hash *lsp_table;
1019 zebra_nhlfe_t *nhlfe;
1020
1021 zvrf = vrf_info_lookup(VRF_DEFAULT);
1022 assert(zvrf);
1023
1024 lsp_table = zvrf->lsp_table;
1025 if (!lsp_table) // unexpected
1026 return;
1027
1028 lsp = (zebra_lsp_t *)data;
1029 if (!lsp) // unexpected
1030 return;
1031
1032 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1033 * exist,
1034 * delete LSP entry also.
1035 */
1036 UNSET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
1037
1038 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1039 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
1040 nhlfe_del(nhlfe);
1041 }
1042
1043 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1044 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
1045 nhlfe_del(nhlfe);
1046 }
1047
1048 lsp_check_free(lsp_table, &lsp);
1049 }
1050
1051 /*
1052 * Callback upon finishing the processing of all scheduled
1053 * LSP forwarding entries.
1054 */
1055 static void lsp_processq_complete(struct work_queue *wq)
1056 {
1057 /* Nothing to do for now. */
1058 }
1059
1060 /*
1061 * Add LSP forwarding entry to queue for subsequent processing.
1062 */
1063 static int lsp_processq_add(zebra_lsp_t *lsp)
1064 {
1065 /* If already scheduled, exit. */
1066 if (CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
1067 return 0;
1068
1069 if (zrouter.lsp_process_q == NULL) {
1070 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1071 "%s: work_queue does not exist!", __func__);
1072 return -1;
1073 }
1074
1075 work_queue_add(zrouter.lsp_process_q, lsp);
1076 SET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
1077 return 0;
1078 }
1079
1080 /*
1081 * Callback to allocate LSP forwarding table entry.
1082 */
1083 static void *lsp_alloc(void *p)
1084 {
1085 const zebra_ile_t *ile = p;
1086 zebra_lsp_t *lsp;
1087
1088 lsp = XCALLOC(MTYPE_LSP, sizeof(zebra_lsp_t));
1089 lsp->ile = *ile;
1090 nhlfe_list_init(&lsp->nhlfe_list);
1091 nhlfe_list_init(&lsp->backup_nhlfe_list);
1092
1093 if (IS_ZEBRA_DEBUG_MPLS)
1094 zlog_debug("Alloc LSP in-label %u", lsp->ile.in_label);
1095
1096 return ((void *)lsp);
1097 }
1098
1099 /*
1100 * Check whether lsp can be freed - no nhlfes, e.g., and call free api
1101 */
1102 static void lsp_check_free(struct hash *lsp_table, zebra_lsp_t **plsp)
1103 {
1104 zebra_lsp_t *lsp;
1105
1106 if (plsp == NULL || *plsp == NULL)
1107 return;
1108
1109 lsp = *plsp;
1110
1111 if ((nhlfe_list_first(&lsp->nhlfe_list) == NULL) &&
1112 (nhlfe_list_first(&lsp->backup_nhlfe_list) == NULL) &&
1113 !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
1114 lsp_free(lsp_table, plsp);
1115 }
1116
1117 /*
1118 * Dtor for an LSP: remove from ile hash, release any internal allocations,
1119 * free LSP object.
1120 */
1121 static void lsp_free(struct hash *lsp_table, zebra_lsp_t **plsp)
1122 {
1123 zebra_lsp_t *lsp;
1124 zebra_nhlfe_t *nhlfe;
1125
1126 if (plsp == NULL || *plsp == NULL)
1127 return;
1128
1129 lsp = *plsp;
1130
1131 if (IS_ZEBRA_DEBUG_MPLS)
1132 zlog_debug("Free LSP in-label %u flags 0x%x",
1133 lsp->ile.in_label, lsp->flags);
1134
1135 /* Free nhlfes, if any. */
1136 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe)
1137 nhlfe_del(nhlfe);
1138
1139 /* Free backup nhlfes, if any. */
1140 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe)
1141 nhlfe_del(nhlfe);
1142
1143 hash_release(lsp_table, &lsp->ile);
1144 XFREE(MTYPE_LSP, lsp);
1145
1146 *plsp = NULL;
1147 }
1148
1149 /*
1150 * Create printable string for NHLFE entry.
1151 */
1152 static char *nhlfe2str(const zebra_nhlfe_t *nhlfe, char *buf, int size)
1153 {
1154 const struct nexthop *nexthop;
1155
1156 buf[0] = '\0';
1157 nexthop = nhlfe->nexthop;
1158 switch (nexthop->type) {
1159 case NEXTHOP_TYPE_IPV4:
1160 case NEXTHOP_TYPE_IPV4_IFINDEX:
1161 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, size);
1162 break;
1163 case NEXTHOP_TYPE_IPV6:
1164 case NEXTHOP_TYPE_IPV6_IFINDEX:
1165 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, size);
1166 break;
1167 case NEXTHOP_TYPE_IFINDEX:
1168 snprintf(buf, size, "Ifindex: %u", nexthop->ifindex);
1169 default:
1170 break;
1171 }
1172
1173 return buf;
1174 }
1175
1176 /*
1177 * Check if NHLFE matches with search info passed.
1178 */
1179 static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
1180 const union g_addr *gate, ifindex_t ifindex)
1181 {
1182 struct nexthop *nhop;
1183 int cmp = 1;
1184
1185 nhop = nhlfe->nexthop;
1186 if (!nhop)
1187 return 1;
1188
1189 if (nhop->type != gtype)
1190 return 1;
1191
1192 switch (nhop->type) {
1193 case NEXTHOP_TYPE_IPV4:
1194 case NEXTHOP_TYPE_IPV4_IFINDEX:
1195 cmp = memcmp(&(nhop->gate.ipv4), &(gate->ipv4),
1196 sizeof(struct in_addr));
1197 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1198 cmp = !(nhop->ifindex == ifindex);
1199 break;
1200 case NEXTHOP_TYPE_IPV6:
1201 case NEXTHOP_TYPE_IPV6_IFINDEX:
1202 cmp = memcmp(&(nhop->gate.ipv6), &(gate->ipv6),
1203 sizeof(struct in6_addr));
1204 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1205 cmp = !(nhop->ifindex == ifindex);
1206 break;
1207 case NEXTHOP_TYPE_IFINDEX:
1208 cmp = !(nhop->ifindex == ifindex);
1209 break;
1210 default:
1211 break;
1212 }
1213
1214 return cmp;
1215 }
1216
1217
1218 /*
1219 * Locate NHLFE that matches with passed info.
1220 */
1221 static zebra_nhlfe_t *nhlfe_find(struct nhlfe_list_head *list,
1222 enum lsp_types_t lsp_type,
1223 enum nexthop_types_t gtype,
1224 const union g_addr *gate, ifindex_t ifindex)
1225 {
1226 zebra_nhlfe_t *nhlfe;
1227
1228 frr_each_safe(nhlfe_list, list, nhlfe) {
1229 if (nhlfe->type != lsp_type)
1230 continue;
1231 if (!nhlfe_nhop_match(nhlfe, gtype, gate, ifindex))
1232 break;
1233 }
1234
1235 return nhlfe;
1236 }
1237
1238 /*
1239 * Allocate and init new NHLFE.
1240 */
1241 static zebra_nhlfe_t *nhlfe_alloc(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1242 enum nexthop_types_t gtype,
1243 const union g_addr *gate, ifindex_t ifindex,
1244 uint8_t num_labels,
1245 const mpls_label_t *labels)
1246 {
1247 zebra_nhlfe_t *nhlfe;
1248 struct nexthop *nexthop;
1249
1250 assert(lsp);
1251
1252 nhlfe = XCALLOC(MTYPE_NHLFE, sizeof(zebra_nhlfe_t));
1253
1254 nhlfe->lsp = lsp;
1255 nhlfe->type = lsp_type;
1256 nhlfe->distance = lsp_distance(lsp_type);
1257
1258 nexthop = nexthop_new();
1259
1260 nexthop_add_labels(nexthop, lsp_type, num_labels, labels);
1261
1262 nexthop->vrf_id = VRF_DEFAULT;
1263 nexthop->type = gtype;
1264 switch (nexthop->type) {
1265 case NEXTHOP_TYPE_IPV4:
1266 case NEXTHOP_TYPE_IPV4_IFINDEX:
1267 nexthop->gate.ipv4 = gate->ipv4;
1268 if (ifindex)
1269 nexthop->ifindex = ifindex;
1270 break;
1271 case NEXTHOP_TYPE_IPV6:
1272 case NEXTHOP_TYPE_IPV6_IFINDEX:
1273 nexthop->gate.ipv6 = gate->ipv6;
1274 if (ifindex)
1275 nexthop->ifindex = ifindex;
1276 break;
1277 case NEXTHOP_TYPE_IFINDEX:
1278 nexthop->ifindex = ifindex;
1279 break;
1280 default:
1281 nexthop_free(nexthop);
1282 XFREE(MTYPE_NHLFE, nhlfe);
1283 return NULL;
1284 }
1285 nhlfe->nexthop = nexthop;
1286
1287 return nhlfe;
1288 }
1289
1290 /*
1291 * Add primary or backup NHLFE. Base entry must have been created and
1292 * duplicate check done.
1293 */
1294 static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1295 enum nexthop_types_t gtype,
1296 const union g_addr *gate, ifindex_t ifindex,
1297 uint8_t num_labels, const mpls_label_t *labels,
1298 bool is_backup)
1299 {
1300 zebra_nhlfe_t *nhlfe;
1301
1302 if (!lsp)
1303 return NULL;
1304
1305 /* Allocate new object */
1306 nhlfe = nhlfe_alloc(lsp, lsp_type, gtype, gate, ifindex, num_labels,
1307 labels);
1308
1309 /* Enqueue to LSP: primaries at head of list, backups at tail */
1310 if (is_backup) {
1311 SET_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP);
1312 nhlfe_list_add_tail(&lsp->backup_nhlfe_list, nhlfe);
1313 } else
1314 nhlfe_list_add_head(&lsp->nhlfe_list, nhlfe);
1315
1316 return nhlfe;
1317 }
1318
1319 /*
1320 * Common delete for NHLFEs.
1321 */
1322 static void nhlfe_free(zebra_nhlfe_t *nhlfe)
1323 {
1324 if (!nhlfe)
1325 return;
1326
1327 /* Free nexthop. */
1328 if (nhlfe->nexthop)
1329 nexthop_free(nhlfe->nexthop);
1330
1331 nhlfe->nexthop = NULL;
1332
1333 XFREE(MTYPE_NHLFE, nhlfe);
1334 }
1335
1336
1337 /*
1338 * Disconnect NHLFE from LSP, and free. Entry must be present on LSP's list.
1339 */
1340 static int nhlfe_del(zebra_nhlfe_t *nhlfe)
1341 {
1342 zebra_lsp_t *lsp;
1343
1344 if (!nhlfe)
1345 return -1;
1346
1347 lsp = nhlfe->lsp;
1348 if (!lsp)
1349 return -1;
1350
1351 if (nhlfe == lsp->best_nhlfe)
1352 lsp->best_nhlfe = NULL;
1353
1354 /* Unlink from LSP */
1355 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP))
1356 nhlfe_list_del(&lsp->backup_nhlfe_list, nhlfe);
1357 else
1358 nhlfe_list_del(&lsp->nhlfe_list, nhlfe);
1359
1360 nhlfe->lsp = NULL;
1361
1362 nhlfe_free(nhlfe);
1363
1364 return 0;
1365 }
1366
1367 /*
1368 * Update label for NHLFE entry.
1369 */
1370 static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
1371 struct mpls_label_stack *nh_label)
1372 {
1373 nhlfe->nexthop->nh_label->label[0] = nh_label->label[0];
1374 }
1375
1376 static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
1377 enum lsp_types_t type)
1378 {
1379 zebra_nhlfe_t *nhlfe;
1380 int schedule_lsp = 0;
1381 char buf[BUFSIZ];
1382
1383 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
1384 schedule_lsp = 1;
1385
1386 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1387 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1388 /* Skip non-static NHLFEs */
1389 if (nhlfe->type != type)
1390 continue;
1391
1392 if (IS_ZEBRA_DEBUG_MPLS) {
1393 nhlfe2str(nhlfe, buf, sizeof(buf));
1394 zlog_debug(
1395 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1396 lsp->ile.in_label, type, buf, nhlfe->flags);
1397 }
1398
1399 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
1400 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
1401 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
1402 schedule_lsp = 1;
1403 } else {
1404 nhlfe_del(nhlfe);
1405 }
1406 }
1407
1408 frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1409 /* Skip non-static NHLFEs */
1410 if (nhlfe->type != type)
1411 continue;
1412
1413 if (IS_ZEBRA_DEBUG_MPLS) {
1414 nhlfe2str(nhlfe, buf, sizeof(buf));
1415 zlog_debug(
1416 "Del backup LSP in-label %u type %d nexthop %s flags 0x%x",
1417 lsp->ile.in_label, type, buf, nhlfe->flags);
1418 }
1419
1420 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
1421 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
1422 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
1423 schedule_lsp = 1;
1424 } else {
1425 nhlfe_del(nhlfe);
1426 }
1427 }
1428
1429 /* Queue LSP for processing, if needed, else delete. */
1430 if (schedule_lsp) {
1431 if (IS_ZEBRA_DEBUG_MPLS) {
1432 zlog_debug("Schedule LSP in-label %u flags 0x%x",
1433 lsp->ile.in_label, lsp->flags);
1434 }
1435 if (lsp_processq_add(lsp))
1436 return -1;
1437 } else {
1438 lsp_check_free(lsp_table, &lsp);
1439 }
1440
1441 return 0;
1442 }
1443
1444 /*
1445 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1446 * If no other NHLFEs exist, the entry would be deleted.
1447 */
1448 static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
1449 mpls_label_t in_label)
1450 {
1451 struct hash *lsp_table;
1452 zebra_ile_t tmp_ile;
1453 zebra_lsp_t *lsp;
1454
1455 /* Lookup table. */
1456 lsp_table = zvrf->lsp_table;
1457 if (!lsp_table)
1458 return -1;
1459
1460 /* If entry is not present, exit. */
1461 tmp_ile.in_label = in_label;
1462 lsp = hash_lookup(lsp_table, &tmp_ile);
1463 if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
1464 return 0;
1465
1466 return mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_STATIC);
1467 }
1468
1469 static json_object *nhlfe_json(zebra_nhlfe_t *nhlfe)
1470 {
1471 char buf[BUFSIZ];
1472 json_object *json_nhlfe = NULL;
1473 json_object *json_backups = NULL;
1474 json_object *json_label_stack;
1475 struct nexthop *nexthop = nhlfe->nexthop;
1476 int i;
1477
1478 json_nhlfe = json_object_new_object();
1479 json_object_string_add(json_nhlfe, "type", nhlfe_type2str(nhlfe->type));
1480 json_object_int_add(json_nhlfe, "outLabel",
1481 nexthop->nh_label->label[0]);
1482
1483 json_label_stack = json_object_new_array();
1484 json_object_object_add(json_nhlfe, "outLabelStack", json_label_stack);
1485 for (i = 0; i < nexthop->nh_label->num_labels; i++)
1486 json_object_array_add(
1487 json_label_stack,
1488 json_object_new_int(nexthop->nh_label->label[i]));
1489
1490 json_object_int_add(json_nhlfe, "distance", nhlfe->distance);
1491
1492 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
1493 json_object_boolean_true_add(json_nhlfe, "installed");
1494
1495 switch (nexthop->type) {
1496 case NEXTHOP_TYPE_IPV4:
1497 case NEXTHOP_TYPE_IPV4_IFINDEX:
1498 json_object_string_add(json_nhlfe, "nexthop",
1499 inet_ntop(AF_INET, &nexthop->gate.ipv4,
1500 buf, sizeof(buf)));
1501 break;
1502 case NEXTHOP_TYPE_IPV6:
1503 case NEXTHOP_TYPE_IPV6_IFINDEX:
1504 json_object_string_add(
1505 json_nhlfe, "nexthop",
1506 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1507
1508 if (nexthop->ifindex)
1509 json_object_string_add(json_nhlfe, "interface",
1510 ifindex2ifname(nexthop->ifindex,
1511 nexthop->vrf_id));
1512 break;
1513 default:
1514 break;
1515 }
1516
1517 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
1518 json_backups = json_object_new_array();
1519 for (i = 0; i < nexthop->backup_num; i++) {
1520 json_object_array_add(
1521 json_backups,
1522 json_object_new_int(nexthop->backup_idx[i]));
1523 }
1524
1525 json_object_object_add(json_nhlfe, "backupIndex",
1526 json_backups);
1527 }
1528
1529 return json_nhlfe;
1530 }
1531
1532 /*
1533 * Print the NHLFE for a LSP forwarding entry.
1534 */
1535 static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty,
1536 const char *indent)
1537 {
1538 struct nexthop *nexthop;
1539 char buf[MPLS_LABEL_STRLEN];
1540
1541 nexthop = nhlfe->nexthop;
1542 if (!nexthop || !nexthop->nh_label) // unexpected
1543 return;
1544
1545 vty_out(vty, " type: %s remote label: %s distance: %d\n",
1546 nhlfe_type2str(nhlfe->type),
1547 mpls_label2str(nexthop->nh_label->num_labels,
1548 nexthop->nh_label->label,
1549 buf, sizeof(buf), 0),
1550 nhlfe->distance);
1551
1552 if (indent)
1553 vty_out(vty, "%s", indent);
1554
1555 switch (nexthop->type) {
1556 case NEXTHOP_TYPE_IPV4:
1557 case NEXTHOP_TYPE_IPV4_IFINDEX:
1558 vty_out(vty, " via %pI4", &nexthop->gate.ipv4);
1559 if (nexthop->ifindex)
1560 vty_out(vty, " dev %s",
1561 ifindex2ifname(nexthop->ifindex,
1562 nexthop->vrf_id));
1563 break;
1564 case NEXTHOP_TYPE_IPV6:
1565 case NEXTHOP_TYPE_IPV6_IFINDEX:
1566 vty_out(vty, " via %s",
1567 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
1568 sizeof(buf)));
1569 if (nexthop->ifindex)
1570 vty_out(vty, " dev %s",
1571 ifindex2ifname(nexthop->ifindex,
1572 nexthop->vrf_id));
1573 break;
1574 default:
1575 break;
1576 }
1577 vty_out(vty, "%s",
1578 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_IS_BACKUP) ? " (backup)"
1579 : "");
1580 vty_out(vty, "%s",
1581 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ? " (installed)"
1582 : "");
1583 vty_out(vty, "\n");
1584 }
1585
1586 /*
1587 * Print an LSP forwarding entry.
1588 */
1589 static void lsp_print(struct vty *vty, zebra_lsp_t *lsp)
1590 {
1591 zebra_nhlfe_t *nhlfe, *backup;
1592 int i, j;
1593
1594 vty_out(vty, "Local label: %u%s\n", lsp->ile.in_label,
1595 CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ? " (installed)"
1596 : "");
1597
1598 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1599 nhlfe_print(nhlfe, vty, NULL);
1600
1601 if (nhlfe->nexthop == NULL ||
1602 !CHECK_FLAG(nhlfe->nexthop->flags,
1603 NEXTHOP_FLAG_HAS_BACKUP))
1604 continue;
1605
1606 /* Backup nhlfes: find backups in backup list */
1607
1608 for (j = 0; j < nhlfe->nexthop->backup_num; j++) {
1609 i = 0;
1610 backup = NULL;
1611 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, backup) {
1612 if (i == nhlfe->nexthop->backup_idx[j])
1613 break;
1614 i++;
1615 }
1616
1617 if (backup) {
1618 vty_out(vty, " [backup %d]", i);
1619 nhlfe_print(backup, vty, " ");
1620 }
1621 }
1622 }
1623 }
1624
1625 /*
1626 * JSON objects for an LSP forwarding entry.
1627 */
1628 static json_object *lsp_json(zebra_lsp_t *lsp)
1629 {
1630 zebra_nhlfe_t *nhlfe = NULL;
1631 json_object *json = json_object_new_object();
1632 json_object *json_nhlfe_list = json_object_new_array();
1633
1634 json_object_int_add(json, "inLabel", lsp->ile.in_label);
1635
1636 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
1637 json_object_boolean_true_add(json, "installed");
1638
1639 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe)
1640 json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
1641
1642 json_object_object_add(json, "nexthops", json_nhlfe_list);
1643 json_nhlfe_list = NULL;
1644
1645
1646 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
1647 if (json_nhlfe_list == NULL)
1648 json_nhlfe_list = json_object_new_array();
1649
1650 json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
1651 }
1652
1653 if (json_nhlfe_list)
1654 json_object_object_add(json, "backupNexthops", json_nhlfe_list);
1655
1656 return json;
1657 }
1658
1659
1660 /* Return a sorted linked list of the hash contents */
1661 static struct list *hash_get_sorted_list(struct hash *hash, void *cmp)
1662 {
1663 unsigned int i;
1664 struct hash_bucket *hb;
1665 struct list *sorted_list = list_new();
1666
1667 sorted_list->cmp = (int (*)(void *, void *))cmp;
1668
1669 for (i = 0; i < hash->size; i++)
1670 for (hb = hash->index[i]; hb; hb = hb->next)
1671 listnode_add_sort(sorted_list, hb->data);
1672
1673 return sorted_list;
1674 }
1675
1676 /*
1677 * Compare two LSPs based on their label values.
1678 */
1679 static int lsp_cmp(const zebra_lsp_t *lsp1, const zebra_lsp_t *lsp2)
1680 {
1681 if (lsp1->ile.in_label < lsp2->ile.in_label)
1682 return -1;
1683
1684 if (lsp1->ile.in_label > lsp2->ile.in_label)
1685 return 1;
1686
1687 return 0;
1688 }
1689
1690 /*
1691 * Initialize work queue for processing changed LSPs.
1692 */
1693 static int mpls_processq_init(void)
1694 {
1695 zrouter.lsp_process_q = work_queue_new(zrouter.master, "LSP processing");
1696 if (!zrouter.lsp_process_q) {
1697 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1698 "%s: could not initialise work queue!", __func__);
1699 return -1;
1700 }
1701
1702 zrouter.lsp_process_q->spec.workfunc = &lsp_process;
1703 zrouter.lsp_process_q->spec.del_item_data = &lsp_processq_del;
1704 zrouter.lsp_process_q->spec.errorfunc = NULL;
1705 zrouter.lsp_process_q->spec.completion_func = &lsp_processq_complete;
1706 zrouter.lsp_process_q->spec.max_retries = 0;
1707 zrouter.lsp_process_q->spec.hold = 10;
1708
1709 return 0;
1710 }
1711
1712
1713 /*
1714 * Process LSP update results from zebra dataplane.
1715 */
1716 void zebra_mpls_lsp_dplane_result(struct zebra_dplane_ctx *ctx)
1717 {
1718 struct zebra_vrf *zvrf;
1719 mpls_label_t label;
1720 zebra_ile_t tmp_ile;
1721 struct hash *lsp_table;
1722 zebra_lsp_t *lsp;
1723 zebra_nhlfe_t *nhlfe;
1724 struct nexthop *nexthop;
1725 enum dplane_op_e op;
1726 enum zebra_dplane_result status;
1727 enum zebra_sr_policy_update_label_mode update_mode;
1728
1729 op = dplane_ctx_get_op(ctx);
1730 status = dplane_ctx_get_status(ctx);
1731
1732 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1733 zlog_debug("LSP dplane ctx %p, op %s, in-label %u, result %s",
1734 ctx, dplane_op2str(op),
1735 dplane_ctx_get_in_label(ctx),
1736 dplane_res2str(status));
1737
1738 label = dplane_ctx_get_in_label(ctx);
1739
1740 switch (op) {
1741 case DPLANE_OP_LSP_INSTALL:
1742 case DPLANE_OP_LSP_UPDATE:
1743 /* Look for zebra LSP object */
1744 zvrf = vrf_info_lookup(VRF_DEFAULT);
1745 if (zvrf == NULL)
1746 break;
1747
1748 lsp_table = zvrf->lsp_table;
1749
1750 tmp_ile.in_label = label;
1751 lsp = hash_lookup(lsp_table, &tmp_ile);
1752 if (lsp == NULL) {
1753 if (IS_ZEBRA_DEBUG_DPLANE)
1754 zlog_debug("LSP ctx %p: in-label %u not found",
1755 ctx, dplane_ctx_get_in_label(ctx));
1756 break;
1757 }
1758
1759 /* TODO -- Confirm that this result is still 'current' */
1760
1761 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
1762 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1763 clear_nhlfe_installed(lsp);
1764 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
1765 "LSP Install Failure: in-label %u",
1766 lsp->ile.in_label);
1767 break;
1768 }
1769
1770 /* Update zebra object */
1771 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1772 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
1773 nexthop = nhlfe->nexthop;
1774 if (!nexthop)
1775 continue;
1776
1777 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED) &&
1778 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
1779 SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1780 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1781 }
1782 }
1783
1784 update_mode = (op == DPLANE_OP_LSP_INSTALL)
1785 ? ZEBRA_SR_POLICY_LABEL_CREATED
1786 : ZEBRA_SR_POLICY_LABEL_UPDATED;
1787 zebra_sr_policy_label_update(label, update_mode);
1788 break;
1789
1790 case DPLANE_OP_LSP_DELETE:
1791 if (status != ZEBRA_DPLANE_REQUEST_SUCCESS) {
1792 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
1793 "LSP Deletion Failure: in-label %u",
1794 dplane_ctx_get_in_label(ctx));
1795 break;
1796 }
1797 zebra_sr_policy_label_update(label,
1798 ZEBRA_SR_POLICY_LABEL_REMOVED);
1799 break;
1800
1801 default:
1802 break;
1803
1804 } /* Switch */
1805
1806 dplane_ctx_fini(&ctx);
1807 }
1808
1809 /*
1810 * Process LSP installation info from two sets of nhlfes: a set from
1811 * a dplane notification, and a set from the zebra LSP object. Update
1812 * counters of installed nexthops, and return whether the LSP has changed.
1813 */
1814 static bool compare_notif_nhlfes(const struct nhlfe_list_head *ctx_head,
1815 struct nhlfe_list_head *nhlfe_head,
1816 int *start_counter, int *end_counter)
1817 {
1818 zebra_nhlfe_t *nhlfe;
1819 const zebra_nhlfe_t *ctx_nhlfe;
1820 struct nexthop *nexthop;
1821 const struct nexthop *ctx_nexthop;
1822 int start_count = 0, end_count = 0;
1823 bool changed_p = false;
1824 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
1825
1826 frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
1827 char buf[NEXTHOP_STRLEN];
1828
1829 nexthop = nhlfe->nexthop;
1830 if (!nexthop)
1831 continue;
1832
1833 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1834 start_count++;
1835
1836 ctx_nhlfe = NULL;
1837 ctx_nexthop = NULL;
1838 frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
1839 ctx_nexthop = ctx_nhlfe->nexthop;
1840 if (!ctx_nexthop)
1841 continue;
1842
1843 if ((ctx_nexthop->type == nexthop->type) &&
1844 nexthop_same(ctx_nexthop, nexthop)) {
1845 /* Matched */
1846 break;
1847 }
1848 }
1849
1850 if (is_debug)
1851 nexthop2str(nexthop, buf, sizeof(buf));
1852
1853 if (ctx_nhlfe && ctx_nexthop) {
1854 if (is_debug) {
1855 const char *tstr = "";
1856
1857 if (!CHECK_FLAG(ctx_nhlfe->flags,
1858 NHLFE_FLAG_INSTALLED))
1859 tstr = "not ";
1860
1861 zlog_debug("LSP dplane notif: matched nh %s (%sinstalled)",
1862 buf, tstr);
1863 }
1864
1865 /* Test zebra nhlfe install state */
1866 if (CHECK_FLAG(ctx_nhlfe->flags,
1867 NHLFE_FLAG_INSTALLED)) {
1868
1869 if (!CHECK_FLAG(nhlfe->flags,
1870 NHLFE_FLAG_INSTALLED))
1871 changed_p = true;
1872
1873 /* Update counter */
1874 end_count++;
1875 } else {
1876
1877 if (CHECK_FLAG(nhlfe->flags,
1878 NHLFE_FLAG_INSTALLED))
1879 changed_p = true;
1880 }
1881
1882 } else {
1883 /* Not mentioned in lfib set -> uninstalled */
1884 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ||
1885 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE) ||
1886 CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1887 changed_p = true;
1888 }
1889
1890 if (is_debug)
1891 zlog_debug("LSP dplane notif: no match, nh %s",
1892 buf);
1893 }
1894 }
1895
1896 if (start_counter)
1897 *start_counter += start_count;
1898 if (end_counter)
1899 *end_counter += end_count;
1900
1901 return changed_p;
1902 }
1903
1904 /*
1905 * Update an lsp nhlfe list from a dplane context, typically an async
1906 * notification context. Update the LSP list to match the installed
1907 * status from the context's list.
1908 */
1909 static int update_nhlfes_from_ctx(struct nhlfe_list_head *nhlfe_head,
1910 const struct nhlfe_list_head *ctx_head)
1911 {
1912 int ret = 0;
1913 zebra_nhlfe_t *nhlfe;
1914 const zebra_nhlfe_t *ctx_nhlfe;
1915 struct nexthop *nexthop;
1916 const struct nexthop *ctx_nexthop;
1917 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
1918
1919 frr_each_safe(nhlfe_list, nhlfe_head, nhlfe) {
1920 char buf[NEXTHOP_STRLEN];
1921
1922 nexthop = nhlfe->nexthop;
1923 if (!nexthop)
1924 continue;
1925
1926 ctx_nhlfe = NULL;
1927 ctx_nexthop = NULL;
1928 frr_each(nhlfe_list_const, ctx_head, ctx_nhlfe) {
1929 ctx_nexthop = ctx_nhlfe->nexthop;
1930 if (!ctx_nexthop)
1931 continue;
1932
1933 if ((ctx_nexthop->type == nexthop->type) &&
1934 nexthop_same(ctx_nexthop, nexthop)) {
1935 /* Matched */
1936 break;
1937 }
1938 }
1939
1940 if (is_debug)
1941 nexthop2str(nexthop, buf, sizeof(buf));
1942
1943 if (ctx_nhlfe && ctx_nexthop) {
1944
1945 /* Bring zebra nhlfe install state into sync */
1946 if (CHECK_FLAG(ctx_nhlfe->flags,
1947 NHLFE_FLAG_INSTALLED)) {
1948 if (is_debug)
1949 zlog_debug("%s: matched lsp nhlfe %s (installed)",
1950 __func__, buf);
1951
1952 SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1953 SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
1954
1955 } else {
1956 if (is_debug)
1957 zlog_debug("%s: matched lsp nhlfe %s (not installed)",
1958 __func__, buf);
1959
1960 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1961 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
1962 }
1963
1964 if (CHECK_FLAG(ctx_nhlfe->nexthop->flags,
1965 NEXTHOP_FLAG_FIB)) {
1966 SET_FLAG(nhlfe->nexthop->flags,
1967 NEXTHOP_FLAG_ACTIVE);
1968 SET_FLAG(nhlfe->nexthop->flags,
1969 NEXTHOP_FLAG_FIB);
1970 } else {
1971 UNSET_FLAG(nhlfe->nexthop->flags,
1972 NEXTHOP_FLAG_ACTIVE);
1973 UNSET_FLAG(nhlfe->nexthop->flags,
1974 NEXTHOP_FLAG_FIB);
1975 }
1976
1977 } else {
1978 /* Not mentioned in lfib set -> uninstalled */
1979 if (is_debug)
1980 zlog_debug("%s: no match for lsp nhlfe %s",
1981 __func__, buf);
1982 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1983 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
1984 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1985 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1986 }
1987 }
1988
1989 return ret;
1990 }
1991
1992 /*
1993 * Process async dplane notifications.
1994 */
1995 void zebra_mpls_process_dplane_notify(struct zebra_dplane_ctx *ctx)
1996 {
1997 struct zebra_vrf *zvrf;
1998 zebra_ile_t tmp_ile;
1999 struct hash *lsp_table;
2000 zebra_lsp_t *lsp;
2001 const struct nhlfe_list_head *ctx_list;
2002 int start_count = 0, end_count = 0; /* Installed counts */
2003 bool changed_p = false;
2004 bool is_debug = (IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_MPLS);
2005
2006 if (is_debug)
2007 zlog_debug("LSP dplane notif, in-label %u",
2008 dplane_ctx_get_in_label(ctx));
2009
2010 /* Look for zebra LSP object */
2011 zvrf = vrf_info_lookup(VRF_DEFAULT);
2012 if (zvrf == NULL)
2013 goto done;
2014
2015 lsp_table = zvrf->lsp_table;
2016
2017 tmp_ile.in_label = dplane_ctx_get_in_label(ctx);
2018 lsp = hash_lookup(lsp_table, &tmp_ile);
2019 if (lsp == NULL) {
2020 if (is_debug)
2021 zlog_debug("dplane LSP notif: in-label %u not found",
2022 dplane_ctx_get_in_label(ctx));
2023 goto done;
2024 }
2025
2026 /*
2027 * The dataplane/forwarding plane is notifying zebra about the state
2028 * of the nexthops associated with this LSP. First, we take a
2029 * pre-scan pass to determine whether the LSP has transitioned
2030 * from installed -> uninstalled. In that case, we need to have
2031 * the existing state of the LSP objects available before making
2032 * any changes.
2033 */
2034 ctx_list = dplane_ctx_get_nhlfe_list(ctx);
2035
2036 changed_p = compare_notif_nhlfes(ctx_list, &lsp->nhlfe_list,
2037 &start_count, &end_count);
2038
2039 if (is_debug)
2040 zlog_debug("LSP dplane notif: lfib start_count %d, end_count %d%s",
2041 start_count, end_count,
2042 changed_p ? ", changed" : "");
2043
2044 ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
2045
2046 if (compare_notif_nhlfes(ctx_list, &lsp->backup_nhlfe_list,
2047 &start_count, &end_count))
2048 /* Avoid accidentally setting back to 'false' */
2049 changed_p = true;
2050
2051 if (is_debug)
2052 zlog_debug("LSP dplane notif: lfib backups, start_count %d, end_count %d%s",
2053 start_count, end_count,
2054 changed_p ? ", changed" : "");
2055
2056 /*
2057 * Has the LSP become uninstalled? We need the existing state of the
2058 * nexthops/nhlfes at this point so we know what to delete.
2059 */
2060 if (start_count > 0 && end_count == 0) {
2061 /* Inform other lfibs */
2062 dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_DELETE, ctx);
2063 }
2064
2065 /*
2066 * Now we take a second pass and bring the zebra
2067 * nexthop state into sync with the forwarding-plane state.
2068 */
2069 ctx_list = dplane_ctx_get_nhlfe_list(ctx);
2070 update_nhlfes_from_ctx(&lsp->nhlfe_list, ctx_list);
2071
2072 ctx_list = dplane_ctx_get_backup_nhlfe_list(ctx);
2073 update_nhlfes_from_ctx(&lsp->backup_nhlfe_list, ctx_list);
2074
2075 if (end_count > 0) {
2076 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
2077
2078 if (changed_p)
2079 dplane_lsp_notif_update(lsp, DPLANE_OP_LSP_UPDATE, ctx);
2080
2081 } else {
2082 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
2083 clear_nhlfe_installed(lsp);
2084 }
2085
2086 done:
2087 dplane_ctx_fini(&ctx);
2088 }
2089
2090 /*
2091 * Install dynamic LSP entry.
2092 */
2093 int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
2094 struct route_entry *re)
2095 {
2096 struct route_table *table;
2097 zebra_fec_t *fec;
2098
2099 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
2100 if (!table)
2101 return -1;
2102
2103 /* See if there is a configured label binding for this FEC. */
2104 fec = fec_find(table, &rn->p);
2105 if (!fec || fec->label == MPLS_INVALID_LABEL)
2106 return 0;
2107
2108 /* We cannot install a label forwarding entry if local label is the
2109 * implicit-null label.
2110 */
2111 if (fec->label == MPLS_LABEL_IMPLICIT_NULL)
2112 return 0;
2113
2114 if (lsp_install(zvrf, fec->label, rn, re))
2115 return -1;
2116
2117 return 0;
2118 }
2119
2120 /*
2121 * Uninstall dynamic LSP entry, if any.
2122 */
2123 int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
2124 struct route_entry *re)
2125 {
2126 struct route_table *table;
2127 zebra_fec_t *fec;
2128
2129 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
2130 if (!table)
2131 return -1;
2132
2133 /* See if there is a configured label binding for this FEC. */
2134 fec = fec_find(table, &rn->p);
2135 if (!fec || fec->label == MPLS_INVALID_LABEL)
2136 return 0;
2137
2138 /* Uninstall always removes all dynamic NHLFEs. */
2139 return lsp_uninstall(zvrf, fec->label);
2140 }
2141
2142 /*
2143 * Add an NHLFE to an LSP, return the newly-added object. This path only changes
2144 * the LSP object - nothing is scheduled for processing, for example.
2145 */
2146 zebra_nhlfe_t *zebra_mpls_lsp_add_nhlfe(zebra_lsp_t *lsp,
2147 enum lsp_types_t lsp_type,
2148 enum nexthop_types_t gtype,
2149 const union g_addr *gate,
2150 ifindex_t ifindex,
2151 uint8_t num_labels,
2152 const mpls_label_t *out_labels)
2153 {
2154 /* Just a public pass-through to the internal implementation */
2155 return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
2156 out_labels, false /*backup*/);
2157 }
2158
2159 /*
2160 * Add a backup NHLFE to an LSP, return the newly-added object.
2161 * This path only changes the LSP object - nothing is scheduled for
2162 * processing, for example.
2163 */
2164 zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nhlfe(zebra_lsp_t *lsp,
2165 enum lsp_types_t lsp_type,
2166 enum nexthop_types_t gtype,
2167 const union g_addr *gate,
2168 ifindex_t ifindex,
2169 uint8_t num_labels,
2170 const mpls_label_t *out_labels)
2171 {
2172 /* Just a public pass-through to the internal implementation */
2173 return nhlfe_add(lsp, lsp_type, gtype, gate, ifindex, num_labels,
2174 out_labels, true);
2175 }
2176
2177 /*
2178 * Add an NHLFE to an LSP based on a nexthop; return the newly-added object
2179 */
2180 zebra_nhlfe_t *zebra_mpls_lsp_add_nh(zebra_lsp_t *lsp,
2181 enum lsp_types_t lsp_type,
2182 const struct nexthop *nh)
2183 {
2184 zebra_nhlfe_t *nhlfe;
2185
2186 if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
2187 return NULL;
2188
2189 nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate, nh->ifindex,
2190 nh->nh_label->num_labels, nh->nh_label->label,
2191 false /*backup*/);
2192
2193 return nhlfe;
2194 }
2195
2196 /*
2197 * Add a backup NHLFE to an LSP based on a nexthop;
2198 * return the newly-added object.
2199 */
2200 zebra_nhlfe_t *zebra_mpls_lsp_add_backup_nh(zebra_lsp_t *lsp,
2201 enum lsp_types_t lsp_type,
2202 const struct nexthop *nh)
2203 {
2204 zebra_nhlfe_t *nhlfe;
2205
2206 if (nh->nh_label == NULL || nh->nh_label->num_labels == 0)
2207 return NULL;
2208
2209 nhlfe = nhlfe_add(lsp, lsp_type, nh->type, &nh->gate,
2210 nh->ifindex, nh->nh_label->num_labels,
2211 nh->nh_label->label, true);
2212
2213 return nhlfe;
2214 }
2215
2216 /*
2217 * Free an allocated NHLFE
2218 */
2219 void zebra_mpls_nhlfe_free(zebra_nhlfe_t *nhlfe)
2220 {
2221 /* Just a pass-through to the internal implementation */
2222 nhlfe_free(nhlfe);
2223 }
2224
2225 /*
2226 * Registration from a client for the label binding for a FEC. If a binding
2227 * already exists, it is informed to the client.
2228 * NOTE: If there is a manually configured label binding, that is used.
2229 * Otherwise, if a label index is specified, it means we have to allocate the
2230 * label from a locally configured label block (SRGB), if one exists and index
2231 * is acceptable. If no label index then just register the specified label.
2232 * NOTE2: Either label or label_index is expected to be set to MPLS_INVALID_*
2233 * by the calling function. Register requests with both will be rejected.
2234 */
2235 int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
2236 uint32_t label, uint32_t label_index,
2237 struct zserv *client)
2238 {
2239 struct route_table *table;
2240 zebra_fec_t *fec;
2241 bool new_client;
2242 bool label_change = false;
2243 uint32_t old_label;
2244 bool have_label_index = (label_index != MPLS_INVALID_LABEL_INDEX);
2245 bool is_configured_fec = false; /* indicate statically configured FEC */
2246
2247 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2248 if (!table)
2249 return -1;
2250
2251 if (label != MPLS_INVALID_LABEL && have_label_index) {
2252 flog_err(
2253 EC_ZEBRA_FEC_LABEL_INDEX_LABEL_CONFLICT,
2254 "Rejecting FEC register for %pFX with both label %u and Label Index %u specified, client %s",
2255 p, label, label_index,
2256 zebra_route_string(client->proto));
2257 return -1;
2258 }
2259
2260 /* Locate FEC */
2261 fec = fec_find(table, p);
2262 if (!fec) {
2263 fec = fec_add(table, p, label, 0, label_index);
2264 if (!fec) {
2265 flog_err(
2266 EC_ZEBRA_FEC_ADD_FAILED,
2267 "Failed to add FEC %pFX upon register, client %s",
2268 p, zebra_route_string(client->proto));
2269 return -1;
2270 }
2271
2272 old_label = MPLS_INVALID_LABEL;
2273 new_client = true;
2274 } else {
2275 /* Check if the FEC has been statically defined in the config */
2276 is_configured_fec = fec->flags & FEC_FLAG_CONFIGURED;
2277 /* Client may register same FEC with different label index. */
2278 new_client =
2279 (listnode_lookup(fec->client_list, client) == NULL);
2280 if (!new_client && fec->label_index == label_index
2281 && fec->label == label)
2282 /* Duplicate register */
2283 return 0;
2284
2285 /* Save current label, update the FEC */
2286 old_label = fec->label;
2287 fec->label_index = label_index;
2288 }
2289
2290 if (new_client)
2291 listnode_add(fec->client_list, client);
2292
2293 if (IS_ZEBRA_DEBUG_MPLS)
2294 zlog_debug("FEC %pFX label%s %u %s by client %s%s", p,
2295 have_label_index ? " index" : "",
2296 have_label_index ? label_index : label,
2297 new_client ? "registered" : "updated",
2298 zebra_route_string(client->proto),
2299 is_configured_fec
2300 ? ", but using statically configured label"
2301 : "");
2302
2303 /* If not a statically configured FEC, derive the local label
2304 * from label index or use the provided label
2305 */
2306 if (!is_configured_fec) {
2307 if (have_label_index)
2308 fec_derive_label_from_index(zvrf, fec);
2309 else
2310 fec->label = label;
2311
2312 /* If no label change, exit. */
2313 if (fec->label == old_label)
2314 return 0;
2315
2316 label_change = true;
2317 }
2318
2319 /* If new client or label change, update client and install or uninstall
2320 * label forwarding entry as needed.
2321 */
2322 /* Inform client of label, if needed. */
2323 if ((new_client && fec->label != MPLS_INVALID_LABEL) || label_change) {
2324 if (IS_ZEBRA_DEBUG_MPLS)
2325 zlog_debug("Update client label %u", fec->label);
2326 fec_send(fec, client);
2327 }
2328
2329 if (new_client || label_change)
2330 return fec_change_update_lsp(zvrf, fec, old_label);
2331
2332 return 0;
2333 }
2334
2335 /*
2336 * Deregistration from a client for the label binding for a FEC. The FEC
2337 * itself is deleted if no other registered clients exist and there is no
2338 * label bound to the FEC.
2339 */
2340 int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
2341 struct zserv *client)
2342 {
2343 struct route_table *table;
2344 zebra_fec_t *fec;
2345
2346 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2347 if (!table)
2348 return -1;
2349
2350 fec = fec_find(table, p);
2351 if (!fec) {
2352 flog_err(EC_ZEBRA_FEC_RM_FAILED,
2353 "Failed to find FEC %pFX upon unregister, client %s",
2354 p, zebra_route_string(client->proto));
2355 return -1;
2356 }
2357
2358 listnode_delete(fec->client_list, client);
2359
2360 if (IS_ZEBRA_DEBUG_MPLS)
2361 zlog_debug("FEC %pFX unregistered by client %s", p,
2362 zebra_route_string(client->proto));
2363
2364 /* If not a configured entry, delete the FEC if no other clients. Before
2365 * deleting, see if any LSP needs to be uninstalled.
2366 */
2367 if (!(fec->flags & FEC_FLAG_CONFIGURED)
2368 && list_isempty(fec->client_list)) {
2369 mpls_label_t old_label = fec->label;
2370 fec->label = MPLS_INVALID_LABEL; /* reset */
2371 fec_change_update_lsp(zvrf, fec, old_label);
2372 fec_del(fec);
2373 }
2374
2375 return 0;
2376 }
2377
2378 /*
2379 * Cleanup any FECs registered by this client.
2380 */
2381 static int zebra_mpls_cleanup_fecs_for_client(struct zserv *client)
2382 {
2383 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
2384 struct route_node *rn;
2385 zebra_fec_t *fec;
2386 struct listnode *node;
2387 struct zserv *fec_client;
2388 int af;
2389
2390 for (af = AFI_IP; af < AFI_MAX; af++) {
2391 if (zvrf->fec_table[af] == NULL)
2392 continue;
2393
2394 for (rn = route_top(zvrf->fec_table[af]); rn;
2395 rn = route_next(rn)) {
2396 fec = rn->info;
2397 if (!fec || list_isempty(fec->client_list))
2398 continue;
2399
2400 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node,
2401 fec_client)) {
2402 if (fec_client == client) {
2403 listnode_delete(fec->client_list,
2404 fec_client);
2405 if (!(fec->flags & FEC_FLAG_CONFIGURED)
2406 && list_isempty(fec->client_list))
2407 fec_del(fec);
2408 break;
2409 }
2410 }
2411 }
2412 }
2413
2414 return 0;
2415 }
2416
2417 struct lsp_uninstall_args {
2418 struct hash *lsp_table;
2419 enum lsp_types_t type;
2420 };
2421
2422 /*
2423 * Cleanup MPLS labels registered by this client.
2424 */
2425 static int zebra_mpls_cleanup_zclient_labels(struct zserv *client)
2426 {
2427 struct vrf *vrf;
2428 struct zebra_vrf *zvrf;
2429
2430 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2431 struct lsp_uninstall_args args;
2432
2433 zvrf = vrf->info;
2434 if (!zvrf)
2435 continue;
2436
2437 /* Cleanup LSPs. */
2438 args.lsp_table = zvrf->lsp_table;
2439 args.type = lsp_type_from_re_type(client->proto);
2440 hash_iterate(zvrf->lsp_table, mpls_lsp_uninstall_all_type,
2441 &args);
2442
2443 /* Cleanup FTNs. */
2444 mpls_ftn_uninstall_all(zvrf, AFI_IP,
2445 lsp_type_from_re_type(client->proto));
2446 mpls_ftn_uninstall_all(zvrf, AFI_IP6,
2447 lsp_type_from_re_type(client->proto));
2448 }
2449
2450 return 0;
2451 }
2452
2453 /*
2454 * Return FEC (if any) to which this label is bound.
2455 * Note: Only works for per-prefix binding and when the label is not
2456 * implicit-null.
2457 * TODO: Currently walks entire table, can optimize later with another
2458 * hash..
2459 */
2460 zebra_fec_t *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
2461 mpls_label_t label)
2462 {
2463 struct route_node *rn;
2464 zebra_fec_t *fec;
2465 int af;
2466
2467 for (af = AFI_IP; af < AFI_MAX; af++) {
2468 if (zvrf->fec_table[af] == NULL)
2469 continue;
2470
2471 for (rn = route_top(zvrf->fec_table[af]); rn;
2472 rn = route_next(rn)) {
2473 if (!rn->info)
2474 continue;
2475 fec = rn->info;
2476 if (fec->label == label)
2477 return fec;
2478 }
2479 }
2480
2481 return NULL;
2482 }
2483
2484 /*
2485 * Inform if specified label is currently bound to a FEC or not.
2486 */
2487 int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label)
2488 {
2489 return (zebra_mpls_fec_for_label(zvrf, label) ? 1 : 0);
2490 }
2491
2492 /*
2493 * Add static FEC to label binding. If there are clients registered for this
2494 * FEC, notify them. If there are labeled routes for this FEC, install the
2495 * label forwarding entry.
2496 */
2497 int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
2498 mpls_label_t in_label)
2499 {
2500 struct route_table *table;
2501 zebra_fec_t *fec;
2502 mpls_label_t old_label;
2503 int ret = 0;
2504
2505 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2506 if (!table)
2507 return -1;
2508
2509 /* Update existing FEC or create a new one. */
2510 fec = fec_find(table, p);
2511 if (!fec) {
2512 fec = fec_add(table, p, in_label, FEC_FLAG_CONFIGURED,
2513 MPLS_INVALID_LABEL_INDEX);
2514 if (!fec) {
2515 flog_err(EC_ZEBRA_FEC_ADD_FAILED,
2516 "Failed to add FEC %pFX upon config", p);
2517 return -1;
2518 }
2519
2520 if (IS_ZEBRA_DEBUG_MPLS)
2521 zlog_debug("Add fec %pFX label %u", p, in_label);
2522 } else {
2523 fec->flags |= FEC_FLAG_CONFIGURED;
2524 if (fec->label == in_label)
2525 /* Duplicate config */
2526 return 0;
2527
2528 /* Label change, update clients. */
2529 old_label = fec->label;
2530 if (IS_ZEBRA_DEBUG_MPLS)
2531 zlog_debug("Update fec %pFX new label %u", p, in_label);
2532
2533 fec->label = in_label;
2534 fec_update_clients(fec);
2535
2536 /* Update label forwarding entries appropriately */
2537 ret = fec_change_update_lsp(zvrf, fec, old_label);
2538 }
2539
2540 return ret;
2541 }
2542
2543 /*
2544 * Remove static FEC to label binding. If there are no clients registered
2545 * for this FEC, delete the FEC; else notify clients
2546 * Note: Upon delete of static binding, if label index exists for this FEC,
2547 * client may need to be updated with derived label.
2548 */
2549 int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p)
2550 {
2551 struct route_table *table;
2552 zebra_fec_t *fec;
2553 mpls_label_t old_label;
2554
2555 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2556 if (!table)
2557 return -1;
2558
2559 fec = fec_find(table, p);
2560 if (!fec) {
2561 flog_err(EC_ZEBRA_FEC_RM_FAILED,
2562 "Failed to find FEC %pFX upon delete", p);
2563 return -1;
2564 }
2565
2566 if (IS_ZEBRA_DEBUG_MPLS) {
2567 zlog_debug("Delete fec %pFX label %u label index %u", p,
2568 fec->label, fec->label_index);
2569 }
2570
2571 old_label = fec->label;
2572 fec->flags &= ~FEC_FLAG_CONFIGURED;
2573 fec->label = MPLS_INVALID_LABEL;
2574
2575 /* If no client exists, just delete the FEC. */
2576 if (list_isempty(fec->client_list)) {
2577 fec_del(fec);
2578 return 0;
2579 }
2580
2581 /* Derive the local label (from label index) or reset it. */
2582 fec_derive_label_from_index(zvrf, fec);
2583
2584 /* If there is a label change, update clients. */
2585 if (fec->label == old_label)
2586 return 0;
2587 fec_update_clients(fec);
2588
2589 /* Update label forwarding entries appropriately */
2590 return fec_change_update_lsp(zvrf, fec, old_label);
2591 }
2592
2593 /*
2594 * Display MPLS FEC to label binding configuration (VTY command handler).
2595 */
2596 int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf)
2597 {
2598 struct route_node *rn;
2599 int af;
2600 zebra_fec_t *fec;
2601 int write = 0;
2602
2603 for (af = AFI_IP; af < AFI_MAX; af++) {
2604 if (zvrf->fec_table[af] == NULL)
2605 continue;
2606
2607 for (rn = route_top(zvrf->fec_table[af]); rn;
2608 rn = route_next(rn)) {
2609 if (!rn->info)
2610 continue;
2611
2612 char lstr[BUFSIZ];
2613 fec = rn->info;
2614
2615 if (!(fec->flags & FEC_FLAG_CONFIGURED))
2616 continue;
2617
2618 write = 1;
2619 vty_out(vty, "mpls label bind %pFX %s\n", &rn->p,
2620 label2str(fec->label, lstr, BUFSIZ));
2621 }
2622 }
2623
2624 return write;
2625 }
2626
2627 /*
2628 * Display MPLS FEC to label binding (VTY command handler).
2629 */
2630 void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf)
2631 {
2632 struct route_node *rn;
2633 int af;
2634
2635 for (af = AFI_IP; af < AFI_MAX; af++) {
2636 if (zvrf->fec_table[af] == NULL)
2637 continue;
2638
2639 for (rn = route_top(zvrf->fec_table[af]); rn;
2640 rn = route_next(rn)) {
2641 if (!rn->info)
2642 continue;
2643 fec_print(rn->info, vty);
2644 }
2645 }
2646 }
2647
2648 /*
2649 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2650 */
2651 void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
2652 struct prefix *p)
2653 {
2654 struct route_table *table;
2655 struct route_node *rn;
2656
2657 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2658 if (!table)
2659 return;
2660
2661 apply_mask(p);
2662 rn = route_node_lookup(table, p);
2663 if (!rn)
2664 return;
2665
2666 route_unlock_node(rn);
2667 if (!rn->info)
2668 return;
2669
2670 fec_print(rn->info, vty);
2671 }
2672
2673 static void mpls_zebra_nhe_update(struct route_entry *re, afi_t afi,
2674 struct nhg_hash_entry *new_nhe)
2675 {
2676 struct nhg_hash_entry *nhe;
2677
2678 nhe = zebra_nhg_rib_find_nhe(new_nhe, afi);
2679
2680 route_entry_update_nhe(re, nhe);
2681 }
2682
2683 static bool ftn_update_nexthop(bool add_p, struct nexthop *nexthop,
2684 enum lsp_types_t type,
2685 const struct zapi_nexthop *znh)
2686 {
2687 if (add_p && nexthop->nh_label_type == ZEBRA_LSP_NONE)
2688 nexthop_add_labels(nexthop, type, znh->label_num, znh->labels);
2689 else if (!add_p && nexthop->nh_label_type == type)
2690 nexthop_del_labels(nexthop);
2691 else
2692 return false;
2693
2694 return true;
2695 }
2696
2697 int mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
2698 struct prefix *prefix, uint8_t route_type,
2699 unsigned short route_instance)
2700 {
2701 struct route_table *table;
2702 struct route_node *rn;
2703 struct route_entry *re;
2704 struct nexthop *nexthop;
2705 struct nhg_hash_entry *new_nhe;
2706 afi_t afi = family2afi(prefix->family);
2707
2708 /* Lookup table. */
2709 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2710 if (!table)
2711 return -1;
2712
2713 /* Lookup existing route */
2714 rn = route_node_get(table, prefix);
2715 RNODE_FOREACH_RE (rn, re) {
2716 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2717 continue;
2718 if (re->type == route_type && re->instance == route_instance)
2719 break;
2720 }
2721 if (re == NULL)
2722 return -1;
2723
2724 /*
2725 * Nexthops are now shared by multiple routes, so we have to make
2726 * a local copy, modify the copy, then update the route.
2727 */
2728 new_nhe = zebra_nhe_copy(re->nhe, 0);
2729
2730 for (nexthop = new_nhe->nhg.nexthop; nexthop; nexthop = nexthop->next)
2731 nexthop_del_labels(nexthop);
2732
2733 /* Update backup routes/nexthops also, if present. */
2734 if (zebra_nhg_get_backup_nhg(new_nhe) != NULL) {
2735 for (nexthop = new_nhe->backup_info->nhe->nhg.nexthop; nexthop;
2736 nexthop = nexthop->next)
2737 nexthop_del_labels(nexthop);
2738 }
2739
2740 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2741 SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
2742
2743 /* This will create (or ref) a new nhe, so we will discard the local
2744 * temporary nhe
2745 */
2746 mpls_zebra_nhe_update(re, afi, new_nhe);
2747
2748 zebra_nhg_free(new_nhe);
2749
2750 rib_queue_add(rn);
2751
2752 return 0;
2753 }
2754
2755 /*
2756 * Iterate through a list of nexthops, for a match for 'znh'. If found,
2757 * update its labels according to 'add_p', and return 'true' if successful.
2758 */
2759 static bool ftn_update_znh(bool add_p, enum lsp_types_t type,
2760 struct nexthop *head, const struct zapi_nexthop *znh)
2761 {
2762 bool found = false, success = false;
2763 struct nexthop *nexthop;
2764
2765 for (nexthop = head; nexthop; nexthop = nexthop->next) {
2766 switch (nexthop->type) {
2767 case NEXTHOP_TYPE_IPV4:
2768 case NEXTHOP_TYPE_IPV4_IFINDEX:
2769 if (znh->type != NEXTHOP_TYPE_IPV4
2770 && znh->type != NEXTHOP_TYPE_IPV4_IFINDEX)
2771 continue;
2772 if (!IPV4_ADDR_SAME(&nexthop->gate.ipv4,
2773 &znh->gate.ipv4))
2774 continue;
2775 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
2776 && nexthop->ifindex != znh->ifindex)
2777 continue;
2778
2779 found = true;
2780
2781 if (!ftn_update_nexthop(add_p, nexthop, type, znh))
2782 break;
2783
2784 success = true;
2785 break;
2786 case NEXTHOP_TYPE_IPV6:
2787 case NEXTHOP_TYPE_IPV6_IFINDEX:
2788 if (znh->type != NEXTHOP_TYPE_IPV6
2789 && znh->type != NEXTHOP_TYPE_IPV6_IFINDEX)
2790 continue;
2791 if (!IPV6_ADDR_SAME(&nexthop->gate.ipv6,
2792 &znh->gate.ipv6))
2793 continue;
2794 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
2795 && nexthop->ifindex != znh->ifindex)
2796 continue;
2797
2798 found = true;
2799
2800 if (!ftn_update_nexthop(add_p, nexthop, type, znh))
2801 break;
2802 success = true;
2803 break;
2804 default:
2805 break;
2806 }
2807
2808 if (found)
2809 break;
2810 }
2811
2812 return success;
2813 }
2814
2815 /*
2816 * Install/uninstall LSP and (optionally) FEC-To-NHLFE (FTN) bindings,
2817 * using zapi message info.
2818 * There are several changes that need to be made, in several zebra
2819 * data structures, so we want to do all the work required at once.
2820 */
2821 int mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
2822 const struct zapi_labels *zl)
2823 {
2824 int i, counter, ret = 0;
2825 char buf[NEXTHOP_STRLEN];
2826 const struct zapi_nexthop *znh;
2827 struct route_table *table;
2828 struct route_node *rn = NULL;
2829 struct route_entry *re = NULL;
2830 struct nhg_hash_entry *new_nhe = NULL;
2831 bool found;
2832 afi_t afi = AFI_IP;
2833 const struct prefix *prefix = NULL;
2834 struct hash *lsp_table;
2835 zebra_ile_t tmp_ile;
2836 zebra_lsp_t *lsp = NULL;
2837
2838 /* Prep LSP for add case */
2839 if (add_p) {
2840 /* Lookup table. */
2841 lsp_table = zvrf->lsp_table;
2842 if (!lsp_table)
2843 return -1;
2844
2845 /* Find or create LSP object */
2846 tmp_ile.in_label = zl->local_label;
2847 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
2848 if (!lsp)
2849 return -1;
2850 }
2851
2852 /* Prep for route/FEC update if requested */
2853 if (CHECK_FLAG(zl->message, ZAPI_LABELS_FTN)) {
2854 prefix = &zl->route.prefix;
2855
2856 afi = family2afi(prefix->family);
2857
2858 /* Lookup table. */
2859 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2860 if (table) {
2861 /* Lookup existing route */
2862 rn = route_node_get(table, prefix);
2863 RNODE_FOREACH_RE(rn, re) {
2864 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2865 continue;
2866 if (re->type == zl->route.type &&
2867 re->instance == zl->route.instance)
2868 break;
2869 }
2870 }
2871
2872 if (re) {
2873 /*
2874 * Copy over current nexthops into a temporary group.
2875 * We can't just change the values here since the nhgs
2876 * are shared and if the labels change, we'll need
2877 * to find or create a new nhg. We need to create
2878 * a whole temporary group, make changes to it,
2879 * then attach that to the route.
2880 */
2881 new_nhe = zebra_nhe_copy(re->nhe, 0);
2882
2883 } else {
2884 /*
2885 * The old version of the zapi code
2886 * attempted to manage LSPs before trying to
2887 * find a route/FEC, so we'll continue that way.
2888 */
2889 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS)
2890 zlog_debug(
2891 "%s: FTN update requested: no route for prefix %pFX",
2892 __func__, prefix);
2893 }
2894 }
2895
2896 /*
2897 * Use info from the zapi nexthops to add/replace/remove LSP/FECs
2898 */
2899
2900 counter = 0;
2901 for (i = 0; i < zl->nexthop_num; i++) {
2902
2903 znh = &zl->nexthops[i];
2904
2905 /* Attempt LSP update */
2906 if (add_p)
2907 ret = lsp_znh_install(lsp, zl->type, znh);
2908 else
2909 ret = mpls_lsp_uninstall(zvrf, zl->type,
2910 zl->local_label, znh->type,
2911 &znh->gate, znh->ifindex,
2912 false);
2913 if (ret < 0) {
2914 if (IS_ZEBRA_DEBUG_RECV || IS_ZEBRA_DEBUG_MPLS) {
2915 zapi_nexthop2str(znh, buf, sizeof(buf));
2916 zlog_debug("%s: Unable to %sinstall LSP: label %u, znh %s",
2917 __func__, (add_p ? "" : "un"),
2918 zl->local_label, buf);
2919 }
2920 continue;
2921 }
2922
2923 /* Attempt route/FEC update if requested */
2924 if (re == NULL)
2925 continue;
2926
2927 /* Search the route's nexthops for a match, and update it. */
2928 found = ftn_update_znh(add_p, zl->type, new_nhe->nhg.nexthop,
2929 znh);
2930 if (found) {
2931 counter++;
2932 } else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
2933 zapi_nexthop2str(znh, buf, sizeof(buf));
2934 zlog_debug(
2935 "%s: Unable to update FEC: prefix %pFX, label %u, znh %s",
2936 __func__, prefix, zl->local_label, buf);
2937 }
2938 }
2939
2940 /*
2941 * Process backup LSPs/nexthop entries also. We associate backup
2942 * LSP info with backup nexthops.
2943 */
2944 if (!CHECK_FLAG(zl->message, ZAPI_LABELS_HAS_BACKUPS))
2945 goto znh_done;
2946
2947 for (i = 0; i < zl->backup_nexthop_num; i++) {
2948
2949 znh = &zl->backup_nexthops[i];
2950
2951 if (add_p)
2952 ret = lsp_backup_znh_install(lsp, zl->type, znh);
2953 else
2954 ret = mpls_lsp_uninstall(zvrf, zl->type,
2955 zl->local_label,
2956 znh->type, &znh->gate,
2957 znh->ifindex, true);
2958
2959 if (ret < 0) {
2960 if (IS_ZEBRA_DEBUG_RECV ||
2961 IS_ZEBRA_DEBUG_MPLS) {
2962 zapi_nexthop2str(znh, buf, sizeof(buf));
2963 zlog_debug("%s: Unable to %sinstall backup LSP: label %u, znh %s",
2964 __func__, (add_p ? "" : "un"),
2965 zl->local_label, buf);
2966 }
2967 continue;
2968 }
2969
2970 /* Attempt backup nexthop/FEC update if requested */
2971 if (re == NULL || zebra_nhg_get_backup_nhg(new_nhe) == NULL)
2972 continue;
2973
2974 /* Search the route's backup nexthops for a match
2975 * and update it.
2976 */
2977 found = ftn_update_znh(add_p, zl->type,
2978 new_nhe->backup_info->nhe->nhg.nexthop,
2979 znh);
2980 if (found) {
2981 counter++;
2982 } else if (IS_ZEBRA_DEBUG_RECV | IS_ZEBRA_DEBUG_MPLS) {
2983 zapi_nexthop2str(znh, buf, sizeof(buf));
2984 zlog_debug(
2985 "%s: Unable to update backup FEC: prefix %pFX, label %u, znh %s",
2986 __func__, prefix, zl->local_label, buf);
2987 }
2988 }
2989
2990 znh_done:
2991
2992 /*
2993 * If we made changes, update the route, and schedule it
2994 * for rib processing
2995 */
2996 if (re != NULL && counter > 0) {
2997 assert(rn != NULL);
2998
2999 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3000 SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
3001
3002 mpls_zebra_nhe_update(re, afi, new_nhe);
3003
3004 rib_queue_add(rn);
3005 }
3006
3007 if (new_nhe)
3008 zebra_nhg_free(new_nhe);
3009
3010 return ret;
3011 }
3012
3013 /*
3014 * Install/update a NHLFE for an LSP in the forwarding table. This may be
3015 * a new LSP entry or a new NHLFE for an existing in-label or an update of
3016 * the out-label for an existing NHLFE (update case).
3017 */
3018 static zebra_nhlfe_t *
3019 lsp_add_nhlfe(zebra_lsp_t *lsp, enum lsp_types_t type,
3020 uint8_t num_out_labels, const mpls_label_t *out_labels,
3021 enum nexthop_types_t gtype, const union g_addr *gate,
3022 ifindex_t ifindex, bool is_backup)
3023 {
3024 zebra_nhlfe_t *nhlfe;
3025 char buf[MPLS_LABEL_STRLEN];
3026 const char *backup_str;
3027
3028 if (is_backup) {
3029 nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
3030 gate, ifindex);
3031 backup_str = "backup ";
3032 } else {
3033 nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
3034 ifindex);
3035 backup_str = "";
3036 }
3037
3038 if (nhlfe) {
3039 struct nexthop *nh = nhlfe->nexthop;
3040
3041 assert(nh);
3042 assert(nh->nh_label);
3043
3044 /* Clear deleted flag (in case it was set) */
3045 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
3046 if (nh->nh_label->num_labels == num_out_labels
3047 && !memcmp(nh->nh_label->label, out_labels,
3048 sizeof(mpls_label_t) * num_out_labels))
3049 /* No change */
3050 return nhlfe;
3051
3052 if (IS_ZEBRA_DEBUG_MPLS) {
3053 char buf2[MPLS_LABEL_STRLEN];
3054 char buf3[MPLS_LABEL_STRLEN];
3055
3056 nhlfe2str(nhlfe, buf, sizeof(buf));
3057 mpls_label2str(num_out_labels, out_labels, buf2,
3058 sizeof(buf2), 0);
3059 mpls_label2str(nh->nh_label->num_labels,
3060 nh->nh_label->label, buf3, sizeof(buf3),
3061 0);
3062
3063 zlog_debug("LSP in-label %u type %d %snexthop %s out-label(s) changed to %s (old %s)",
3064 lsp->ile.in_label, type, backup_str, buf,
3065 buf2, buf3);
3066 }
3067
3068 /* Update out label(s), trigger processing. */
3069 if (nh->nh_label->num_labels == num_out_labels)
3070 memcpy(nh->nh_label->label, out_labels,
3071 sizeof(mpls_label_t) * num_out_labels);
3072 else {
3073 nexthop_del_labels(nh);
3074 nexthop_add_labels(nh, type, num_out_labels,
3075 out_labels);
3076 }
3077 } else {
3078 /* Add LSP entry to this nexthop */
3079 nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex,
3080 num_out_labels, out_labels, is_backup);
3081 if (!nhlfe)
3082 return NULL;
3083
3084 if (IS_ZEBRA_DEBUG_MPLS) {
3085 char buf2[MPLS_LABEL_STRLEN];
3086
3087 nhlfe2str(nhlfe, buf, sizeof(buf));
3088 mpls_label2str(num_out_labels, out_labels, buf2,
3089 sizeof(buf2), 0);
3090
3091 zlog_debug("Add LSP in-label %u type %d %snexthop %s out-label(s) %s",
3092 lsp->ile.in_label, type, backup_str, buf,
3093 buf2);
3094 }
3095
3096 lsp->addr_family = NHLFE_FAMILY(nhlfe);
3097 }
3098
3099 /* Mark NHLFE, queue LSP for processing. */
3100 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
3101
3102 return nhlfe;
3103 }
3104
3105 /*
3106 * Install an LSP and forwarding entry; used primarily
3107 * from vrf zapi message processing.
3108 */
3109 int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
3110 mpls_label_t in_label, uint8_t num_out_labels,
3111 const mpls_label_t *out_labels, enum nexthop_types_t gtype,
3112 const union g_addr *gate, ifindex_t ifindex)
3113 {
3114 struct hash *lsp_table;
3115 zebra_ile_t tmp_ile;
3116 zebra_lsp_t *lsp;
3117 zebra_nhlfe_t *nhlfe;
3118
3119 /* Lookup table. */
3120 lsp_table = zvrf->lsp_table;
3121 if (!lsp_table)
3122 return -1;
3123
3124 /* Find or create LSP object */
3125 tmp_ile.in_label = in_label;
3126 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
3127 if (!lsp)
3128 return -1;
3129
3130 nhlfe = lsp_add_nhlfe(lsp, type, num_out_labels, out_labels, gtype,
3131 gate, ifindex, false /*backup*/);
3132 if (nhlfe == NULL)
3133 return -1;
3134
3135 /* Queue LSP for processing. */
3136 if (lsp_processq_add(lsp))
3137 return -1;
3138
3139 return 0;
3140 }
3141
3142 /*
3143 * Install or replace NHLFE, using info from zapi nexthop
3144 */
3145 static int lsp_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
3146 const struct zapi_nexthop *znh)
3147 {
3148 zebra_nhlfe_t *nhlfe;
3149
3150 nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num, znh->labels,
3151 znh->type, &znh->gate, znh->ifindex,
3152 false /*backup*/);
3153 if (nhlfe == NULL)
3154 return -1;
3155
3156 /* Update backup info if present */
3157 if (CHECK_FLAG(znh->flags, ZAPI_NEXTHOP_FLAG_HAS_BACKUP)) {
3158 if (znh->backup_num > NEXTHOP_MAX_BACKUPS) {
3159 nhlfe_del(nhlfe);
3160 return -1;
3161 }
3162
3163 nhlfe->nexthop->backup_num = znh->backup_num;
3164 memcpy(nhlfe->nexthop->backup_idx, znh->backup_idx,
3165 znh->backup_num);
3166 SET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
3167 } else {
3168 /* Ensure there's no stale backup info */
3169 UNSET_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP);
3170 nhlfe->nexthop->backup_num = 0;
3171 }
3172
3173 /* Queue LSP for processing. */
3174 if (lsp_processq_add(lsp))
3175 return -1;
3176
3177 return 0;
3178 }
3179
3180 /*
3181 * Install/update backup NHLFE for an LSP, using info from a zapi message.
3182 */
3183 static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
3184 const struct zapi_nexthop *znh)
3185 {
3186 zebra_nhlfe_t *nhlfe;
3187
3188 nhlfe = lsp_add_nhlfe(lsp, type, znh->label_num,
3189 znh->labels, znh->type, &znh->gate,
3190 znh->ifindex, true /*backup*/);
3191 if (nhlfe == NULL) {
3192 if (IS_ZEBRA_DEBUG_MPLS)
3193 zlog_debug("%s: unable to add backup nhlfe, label: %u",
3194 __func__, lsp->ile.in_label);
3195 return -1;
3196 }
3197
3198 /* Queue LSP for processing. */
3199 if (lsp_processq_add(lsp))
3200 return -1;
3201
3202 return 0;
3203 }
3204
3205 zebra_lsp_t *mpls_lsp_find(struct zebra_vrf *zvrf, mpls_label_t in_label)
3206 {
3207 struct hash *lsp_table;
3208 zebra_ile_t tmp_ile;
3209
3210 /* Lookup table. */
3211 lsp_table = zvrf->lsp_table;
3212 if (!lsp_table)
3213 return NULL;
3214
3215 /* If entry is not present, exit. */
3216 tmp_ile.in_label = in_label;
3217 return hash_lookup(lsp_table, &tmp_ile);
3218 }
3219
3220 /*
3221 * Uninstall a particular NHLFE in the forwarding table. If this is
3222 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
3223 */
3224 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3225 mpls_label_t in_label, enum nexthop_types_t gtype,
3226 const union g_addr *gate, ifindex_t ifindex,
3227 bool backup_p)
3228 {
3229 struct hash *lsp_table;
3230 zebra_ile_t tmp_ile;
3231 zebra_lsp_t *lsp;
3232 zebra_nhlfe_t *nhlfe;
3233 char buf[NEXTHOP_STRLEN];
3234 bool schedule_lsp = false;
3235
3236 /* Lookup table. */
3237 lsp_table = zvrf->lsp_table;
3238 if (!lsp_table)
3239 return -1;
3240
3241 /* If entry is not present, exit. */
3242 tmp_ile.in_label = in_label;
3243 lsp = hash_lookup(lsp_table, &tmp_ile);
3244 if (!lsp)
3245 return 0;
3246
3247 if (backup_p)
3248 nhlfe = nhlfe_find(&lsp->backup_nhlfe_list, type, gtype,
3249 gate, ifindex);
3250 else
3251 nhlfe = nhlfe_find(&lsp->nhlfe_list, type, gtype, gate,
3252 ifindex);
3253 if (!nhlfe)
3254 return 0;
3255
3256 if (IS_ZEBRA_DEBUG_MPLS) {
3257 nhlfe2str(nhlfe, buf, sizeof(buf));
3258 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
3259 in_label, type, buf, nhlfe->flags);
3260 }
3261
3262 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ||
3263 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
3264 schedule_lsp = true;
3265
3266 /* Mark NHLFE for delete or directly delete, as appropriate. */
3267 if (schedule_lsp) {
3268 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
3269 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
3270
3271 if (IS_ZEBRA_DEBUG_MPLS)
3272 zlog_debug("Schedule LSP in-label %u flags 0x%x",
3273 lsp->ile.in_label, lsp->flags);
3274 if (lsp_processq_add(lsp))
3275 return -1;
3276 } else {
3277 nhlfe_del(nhlfe);
3278
3279 /* Free LSP entry if no other NHLFEs and not scheduled. */
3280 lsp_check_free(lsp_table, &lsp);
3281 }
3282 return 0;
3283 }
3284
3285 int mpls_lsp_uninstall_all_vrf(struct zebra_vrf *zvrf, enum lsp_types_t type,
3286 mpls_label_t in_label)
3287 {
3288 struct hash *lsp_table;
3289 zebra_ile_t tmp_ile;
3290 zebra_lsp_t *lsp;
3291
3292 /* Lookup table. */
3293 lsp_table = zvrf->lsp_table;
3294 if (!lsp_table)
3295 return -1;
3296
3297 /* If entry is not present, exit. */
3298 tmp_ile.in_label = in_label;
3299 lsp = hash_lookup(lsp_table, &tmp_ile);
3300 if (!lsp)
3301 return 0;
3302
3303 return mpls_lsp_uninstall_all(lsp_table, lsp, type);
3304 }
3305
3306 /*
3307 * Uninstall all NHLFEs for a particular LSP forwarding entry.
3308 * If no other NHLFEs exist, the entry would be deleted.
3309 */
3310 static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt)
3311 {
3312 struct lsp_uninstall_args *args = ctxt;
3313 zebra_lsp_t *lsp;
3314 struct hash *lsp_table;
3315
3316 lsp = (zebra_lsp_t *)bucket->data;
3317 if (nhlfe_list_first(&lsp->nhlfe_list) == NULL)
3318 return;
3319
3320 lsp_table = args->lsp_table;
3321 if (!lsp_table)
3322 return;
3323
3324 mpls_lsp_uninstall_all(lsp_table, lsp, args->type);
3325 }
3326
3327 /*
3328 * Uninstall all FEC-To-NHLFE (FTN) bindings of the given address-family and
3329 * LSP type.
3330 */
3331 static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
3332 int afi, enum lsp_types_t lsp_type)
3333 {
3334 struct route_table *table;
3335 struct route_node *rn;
3336 struct route_entry *re;
3337 struct nexthop *nexthop;
3338 struct nexthop_group *nhg;
3339 bool update;
3340
3341 /* Process routes of interested address-families. */
3342 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
3343 if (!table)
3344 return;
3345
3346 for (rn = route_top(table); rn; rn = route_next(rn)) {
3347 update = false;
3348
3349 RNODE_FOREACH_RE (rn, re) {
3350 struct nhg_hash_entry *new_nhe;
3351
3352 new_nhe = zebra_nhe_copy(re->nhe, 0);
3353
3354 nhg = &new_nhe->nhg;
3355 for (nexthop = nhg->nexthop; nexthop;
3356 nexthop = nexthop->next) {
3357 if (nexthop->nh_label_type != lsp_type)
3358 continue;
3359
3360 nexthop_del_labels(nexthop);
3361 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
3362 SET_FLAG(re->status,
3363 ROUTE_ENTRY_LABELS_CHANGED);
3364 update = true;
3365 }
3366
3367 /* Check for backup info and update that also */
3368 nhg = zebra_nhg_get_backup_nhg(new_nhe);
3369 if (nhg != NULL) {
3370 for (nexthop = nhg->nexthop; nexthop;
3371 nexthop = nexthop->next) {
3372 if (nexthop->nh_label_type != lsp_type)
3373 continue;
3374
3375 nexthop_del_labels(nexthop);
3376 SET_FLAG(re->status,
3377 ROUTE_ENTRY_CHANGED);
3378 SET_FLAG(re->status,
3379 ROUTE_ENTRY_LABELS_CHANGED);
3380 update = true;
3381 }
3382 }
3383
3384 if (CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED))
3385 mpls_zebra_nhe_update(re, afi, new_nhe);
3386
3387 zebra_nhg_free(new_nhe);
3388 }
3389
3390 if (update)
3391 rib_queue_add(rn);
3392 }
3393 }
3394
3395 #if defined(HAVE_CUMULUS)
3396 /*
3397 * Check that the label values used in LSP creation are consistent. The
3398 * main criteria is that if there is ECMP, the label operation must still
3399 * be consistent - i.e., all paths either do a swap or do PHP. This is due
3400 * to current HW restrictions.
3401 */
3402 int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
3403 mpls_label_t in_label,
3404 mpls_label_t out_label,
3405 enum nexthop_types_t gtype,
3406 union g_addr *gate, ifindex_t ifindex)
3407 {
3408 struct hash *slsp_table;
3409 zebra_ile_t tmp_ile;
3410 zebra_lsp_t *lsp;
3411 zebra_nhlfe_t *nhlfe;
3412 const struct nexthop *nh;
3413
3414 /* Lookup table. */
3415 slsp_table = zvrf->slsp_table;
3416 if (!slsp_table)
3417 return 0;
3418
3419 /* If entry is not present, exit. */
3420 tmp_ile.in_label = in_label;
3421 lsp = hash_lookup(slsp_table, &tmp_ile);
3422 if (!lsp)
3423 return 1;
3424
3425 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
3426 gtype, gate, ifindex);
3427 if (nhlfe) {
3428 nh = nhlfe->nexthop;
3429
3430 if (nh == NULL || nh->nh_label == NULL)
3431 return 0;
3432
3433 if (nh->nh_label->label[0] == out_label)
3434 return 1;
3435
3436 /* If not only NHLFE, cannot allow label change. */
3437 if (nhlfe != nhlfe_list_first(&lsp->nhlfe_list) ||
3438 nhlfe_list_next(&lsp->nhlfe_list, nhlfe) != NULL)
3439 return 0;
3440 } else {
3441 /* If other NHLFEs exist, label operation must match. */
3442 nhlfe = nhlfe_list_first(&lsp->nhlfe_list);
3443 if (nhlfe != NULL) {
3444 int cur_op, new_op;
3445
3446 nh = nhlfe->nexthop;
3447
3448 if (nh == NULL || nh->nh_label == NULL)
3449 return 0;
3450
3451 cur_op = (nh->nh_label->label[0] ==
3452 MPLS_LABEL_IMPLICIT_NULL);
3453 new_op = (out_label == MPLS_LABEL_IMPLICIT_NULL);
3454 if (cur_op != new_op)
3455 return 0;
3456 }
3457 }
3458
3459 /* Label values are good. */
3460 return 1;
3461 }
3462 #endif /* HAVE_CUMULUS */
3463
3464 /*
3465 * Add static LSP entry. This may be the first entry for this incoming label
3466 * or an additional nexthop; an existing entry may also have outgoing label
3467 * changed.
3468 * Note: The label operation (swap or PHP) is common for the LSP entry (all
3469 * NHLFEs).
3470 */
3471 int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
3472 mpls_label_t out_label,
3473 enum nexthop_types_t gtype, union g_addr *gate,
3474 ifindex_t ifindex)
3475 {
3476 struct hash *slsp_table;
3477 zebra_ile_t tmp_ile;
3478 zebra_lsp_t *lsp;
3479 zebra_nhlfe_t *nhlfe;
3480 char buf[BUFSIZ];
3481
3482 /* Lookup table. */
3483 slsp_table = zvrf->slsp_table;
3484 if (!slsp_table)
3485 return -1;
3486
3487 /* Find or create LSP. */
3488 tmp_ile.in_label = in_label;
3489 lsp = hash_get(slsp_table, &tmp_ile, lsp_alloc);
3490 if (!lsp)
3491 return -1;
3492
3493 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC, gtype, gate,
3494 ifindex);
3495 if (nhlfe) {
3496 struct nexthop *nh = nhlfe->nexthop;
3497
3498 assert(nh);
3499 assert(nh->nh_label);
3500
3501 /* Compare existing nexthop */
3502 if (nh->nh_label->num_labels == 1 &&
3503 nh->nh_label->label[0] == out_label)
3504 /* No change */
3505 return 0;
3506
3507 if (IS_ZEBRA_DEBUG_MPLS) {
3508 nhlfe2str(nhlfe, buf, sizeof(buf));
3509 zlog_debug(
3510 "Upd static LSP in-label %u nexthop %s out-label %u (old %u)",
3511 in_label, buf, out_label,
3512 nh->nh_label->label[0]);
3513 }
3514 if (nh->nh_label->num_labels == 1)
3515 nh->nh_label->label[0] = out_label;
3516 else {
3517 nexthop_del_labels(nh);
3518 nexthop_add_labels(nh, ZEBRA_LSP_STATIC, 1, &out_label);
3519 }
3520
3521 } else {
3522 /* Add static LSP entry to this nexthop */
3523 nhlfe = nhlfe_add(lsp, ZEBRA_LSP_STATIC, gtype, gate,
3524 ifindex, 1, &out_label, false /*backup*/);
3525 if (!nhlfe)
3526 return -1;
3527
3528 if (IS_ZEBRA_DEBUG_MPLS) {
3529 nhlfe2str(nhlfe, buf, sizeof(buf));
3530 zlog_debug(
3531 "Add static LSP in-label %u nexthop %s out-label %u",
3532 in_label, buf, out_label);
3533 }
3534 }
3535
3536 /* (Re)Install LSP in the main table. */
3537 if (mpls_lsp_install(zvrf, ZEBRA_LSP_STATIC, in_label, 1, &out_label,
3538 gtype, gate, ifindex))
3539 return -1;
3540
3541 return 0;
3542 }
3543
3544 /*
3545 * Delete static LSP entry. This may be the delete of one particular
3546 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
3547 * all NHLFEs).
3548 * NOTE: Delete of the only NHLFE will also end up deleting the entire
3549 * LSP configuration.
3550 */
3551 int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
3552 enum nexthop_types_t gtype, union g_addr *gate,
3553 ifindex_t ifindex)
3554 {
3555 struct hash *slsp_table;
3556 zebra_ile_t tmp_ile;
3557 zebra_lsp_t *lsp;
3558 zebra_nhlfe_t *nhlfe;
3559
3560 /* Lookup table. */
3561 slsp_table = zvrf->slsp_table;
3562 if (!slsp_table)
3563 return -1;
3564
3565 /* If entry is not present, exit. */
3566 tmp_ile.in_label = in_label;
3567 lsp = hash_lookup(slsp_table, &tmp_ile);
3568 if (!lsp)
3569 return 0;
3570
3571 /* Is it delete of entire LSP or a specific NHLFE? */
3572 if (gtype == NEXTHOP_TYPE_BLACKHOLE) {
3573 if (IS_ZEBRA_DEBUG_MPLS)
3574 zlog_debug("Del static LSP in-label %u", in_label);
3575
3576 /* Uninstall entire LSP from the main table. */
3577 mpls_static_lsp_uninstall_all(zvrf, in_label);
3578
3579 /* Delete all static NHLFEs */
3580 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3581 nhlfe_del(nhlfe);
3582 }
3583 } else {
3584 /* Find specific NHLFE, exit if not found. */
3585 nhlfe = nhlfe_find(&lsp->nhlfe_list, ZEBRA_LSP_STATIC,
3586 gtype, gate, ifindex);
3587 if (!nhlfe)
3588 return 0;
3589
3590 if (IS_ZEBRA_DEBUG_MPLS) {
3591 char buf[BUFSIZ];
3592 nhlfe2str(nhlfe, buf, sizeof(buf));
3593 zlog_debug("Del static LSP in-label %u nexthop %s",
3594 in_label, buf);
3595 }
3596
3597 /* Uninstall LSP from the main table. */
3598 mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
3599 gate, ifindex, false);
3600
3601 /* Delete static LSP NHLFE */
3602 nhlfe_del(nhlfe);
3603 }
3604
3605 /* Remove entire static LSP entry if no NHLFE - valid in either case
3606 * above.
3607 */
3608 if (nhlfe_list_first(&lsp->nhlfe_list) == NULL) {
3609 lsp = hash_release(slsp_table, &tmp_ile);
3610 XFREE(MTYPE_LSP, lsp);
3611 }
3612
3613 return 0;
3614 }
3615
3616 /*
3617 * Schedule all MPLS label forwarding entries for processing.
3618 * Called upon changes that may affect one or more of them such as
3619 * interface or nexthop state changes.
3620 */
3621 void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf)
3622 {
3623 if (!zvrf)
3624 return;
3625 hash_iterate(zvrf->lsp_table, lsp_schedule, NULL);
3626 }
3627
3628 /*
3629 * Display MPLS label forwarding table for a specific LSP
3630 * (VTY command handler).
3631 */
3632 void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
3633 mpls_label_t label, bool use_json)
3634 {
3635 struct hash *lsp_table;
3636 zebra_lsp_t *lsp;
3637 zebra_ile_t tmp_ile;
3638 json_object *json = NULL;
3639
3640 /* Lookup table. */
3641 lsp_table = zvrf->lsp_table;
3642 if (!lsp_table)
3643 return;
3644
3645 /* If entry is not present, exit. */
3646 tmp_ile.in_label = label;
3647 lsp = hash_lookup(lsp_table, &tmp_ile);
3648 if (!lsp)
3649 return;
3650
3651 if (use_json) {
3652 json = lsp_json(lsp);
3653 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3654 json, JSON_C_TO_STRING_PRETTY));
3655 json_object_free(json);
3656 } else
3657 lsp_print(vty, lsp);
3658 }
3659
3660 /*
3661 * Display MPLS label forwarding table (VTY command handler).
3662 */
3663 void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
3664 bool use_json)
3665 {
3666 char buf[BUFSIZ];
3667 json_object *json = NULL;
3668 zebra_lsp_t *lsp = NULL;
3669 zebra_nhlfe_t *nhlfe = NULL;
3670 struct listnode *node = NULL;
3671 struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
3672
3673 if (use_json) {
3674 json = json_object_new_object();
3675
3676 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
3677 json_object_object_add(
3678 json, label2str(lsp->ile.in_label, buf,
3679 sizeof(buf)),
3680 lsp_json(lsp));
3681
3682 vty_out(vty, "%s\n", json_object_to_json_string_ext(
3683 json, JSON_C_TO_STRING_PRETTY));
3684 json_object_free(json);
3685 } else {
3686 struct ttable *tt;
3687
3688 /* Prepare table. */
3689 tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]);
3690 ttable_add_row(tt, "Inbound Label|Type|Nexthop|Outbound Label");
3691 tt->style.cell.rpad = 2;
3692 tt->style.corner = '+';
3693 ttable_restyle(tt);
3694 ttable_rowseps(tt, 0, BOTTOM, true, '-');
3695
3696 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
3697 frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3698 struct nexthop *nexthop;
3699 const char *out_label_str;
3700 char nh_buf[NEXTHOP_STRLEN];
3701
3702 nexthop = nhlfe->nexthop;
3703
3704 switch (nexthop->type) {
3705 case NEXTHOP_TYPE_IFINDEX: {
3706 struct zebra_ns *zns;
3707 struct interface *ifp;
3708
3709 zns = zebra_ns_lookup(NS_DEFAULT);
3710 ifp = if_lookup_by_index_per_ns(
3711 zns, nexthop->ifindex);
3712 snprintf(nh_buf, sizeof(nh_buf), "%s",
3713 ifp ? ifp->name : "Null");
3714 break;
3715 }
3716 case NEXTHOP_TYPE_IPV4:
3717 case NEXTHOP_TYPE_IPV4_IFINDEX:
3718 inet_ntop(AF_INET, &nexthop->gate.ipv4,
3719 nh_buf, sizeof(nh_buf));
3720 break;
3721 case NEXTHOP_TYPE_IPV6:
3722 case NEXTHOP_TYPE_IPV6_IFINDEX:
3723 inet_ntop(AF_INET6, &nexthop->gate.ipv6,
3724 nh_buf, sizeof(nh_buf));
3725 break;
3726 default:
3727 break;
3728 }
3729
3730 if (nexthop->type != NEXTHOP_TYPE_IFINDEX)
3731 out_label_str = mpls_label2str(
3732 nexthop->nh_label->num_labels,
3733 &nexthop->nh_label->label[0],
3734 buf, sizeof(buf), 1);
3735 else
3736 out_label_str = "-";
3737
3738 ttable_add_row(tt, "%u|%s|%s|%s",
3739 lsp->ile.in_label,
3740 nhlfe_type2str(nhlfe->type),
3741 nh_buf, out_label_str);
3742 }
3743 }
3744
3745 /* Dump the generated table. */
3746 if (tt->nrows > 1) {
3747 char *table = ttable_dump(tt, "\n");
3748 vty_out(vty, "%s\n", table);
3749 XFREE(MTYPE_TMP, table);
3750 }
3751 ttable_del(tt);
3752 }
3753
3754 list_delete(&lsp_list);
3755 }
3756
3757 /*
3758 * Create printable string for static LSP configuration.
3759 */
3760 static char *nhlfe_config_str(const zebra_nhlfe_t *nhlfe, char *buf, int size)
3761 {
3762 const struct nexthop *nh;
3763
3764 nh = nhlfe->nexthop;
3765
3766 buf[0] = '\0';
3767 switch (nh->type) {
3768 case NEXTHOP_TYPE_IPV4:
3769 inet_ntop(AF_INET, &nh->gate.ipv4, buf, size);
3770 break;
3771 case NEXTHOP_TYPE_IPV6:
3772 case NEXTHOP_TYPE_IPV6_IFINDEX:
3773 inet_ntop(AF_INET6, &nh->gate.ipv6, buf, size);
3774 if (nh->ifindex)
3775 strlcat(buf,
3776 ifindex2ifname(nh->ifindex, VRF_DEFAULT),
3777 size);
3778 break;
3779 default:
3780 break;
3781 }
3782
3783 return buf;
3784 }
3785
3786 /*
3787 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
3788 */
3789 int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf)
3790 {
3791 zebra_lsp_t *lsp;
3792 zebra_nhlfe_t *nhlfe;
3793 struct nexthop *nh;
3794 struct listnode *node;
3795 struct list *slsp_list =
3796 hash_get_sorted_list(zvrf->slsp_table, lsp_cmp);
3797
3798 for (ALL_LIST_ELEMENTS_RO(slsp_list, node, lsp)) {
3799 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
3800 char buf[BUFSIZ];
3801 char lstr[30];
3802
3803 nh = nhlfe->nexthop;
3804 if (nh == NULL || nh->nh_label == NULL)
3805 continue;
3806
3807 nhlfe_config_str(nhlfe, buf, sizeof(buf));
3808
3809 switch (nh->nh_label->label[0]) {
3810 case MPLS_LABEL_IPV4_EXPLICIT_NULL:
3811 case MPLS_LABEL_IPV6_EXPLICIT_NULL:
3812 strlcpy(lstr, "explicit-null", sizeof(lstr));
3813 break;
3814 case MPLS_LABEL_IMPLICIT_NULL:
3815 strlcpy(lstr, "implicit-null", sizeof(lstr));
3816 break;
3817 default:
3818 snprintf(lstr, sizeof(lstr), "%u",
3819 nh->nh_label->label[0]);
3820 break;
3821 }
3822
3823 vty_out(vty, "mpls lsp %u %s %s\n", lsp->ile.in_label,
3824 buf, lstr);
3825 }
3826 }
3827
3828 list_delete(&slsp_list);
3829 return (zvrf->slsp_table->count ? 1 : 0);
3830 }
3831
3832 /*
3833 * Add/update global label block.
3834 */
3835 int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, uint32_t start_label,
3836 uint32_t end_label)
3837 {
3838 zvrf->mpls_srgb.start_label = start_label;
3839 zvrf->mpls_srgb.end_label = end_label;
3840
3841 /* Evaluate registered FECs to see if any get a label or not. */
3842 fec_evaluate(zvrf);
3843 return 0;
3844 }
3845
3846 /*
3847 * Delete global label block.
3848 */
3849 int zebra_mpls_label_block_del(struct zebra_vrf *zvrf)
3850 {
3851 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
3852 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
3853
3854 /* Process registered FECs to clear their local label, if needed. */
3855 fec_evaluate(zvrf);
3856 return 0;
3857 }
3858
3859 /*
3860 * Display MPLS global label block configuration (VTY command handler).
3861 */
3862 int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *zvrf)
3863 {
3864 if (zvrf->mpls_srgb.start_label == 0)
3865 return 0;
3866
3867 if ((zvrf->mpls_srgb.start_label != MPLS_DEFAULT_MIN_SRGB_LABEL)
3868 || (zvrf->mpls_srgb.end_label != MPLS_DEFAULT_MAX_SRGB_LABEL)) {
3869 vty_out(vty, "mpls label global-block %u %u\n",
3870 zvrf->mpls_srgb.start_label, zvrf->mpls_srgb.end_label);
3871 }
3872
3873 return 1;
3874 }
3875
3876 /*
3877 * Called when VRF becomes inactive, cleans up information but keeps
3878 * the table itself.
3879 */
3880 void zebra_mpls_cleanup_tables(struct zebra_vrf *zvrf)
3881 {
3882 struct zebra_vrf *def_zvrf;
3883 afi_t afi;
3884
3885 if (zvrf_id(zvrf) == VRF_DEFAULT)
3886 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
3887 else {
3888 /*
3889 * For other vrfs, we try to remove associated LSPs; we locate
3890 * the LSPs in the default vrf.
3891 */
3892 def_zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
3893
3894 /* At shutdown, the default may be gone already */
3895 if (def_zvrf == NULL)
3896 return;
3897
3898 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
3899 if (zvrf->label[afi] != MPLS_LABEL_NONE)
3900 lsp_uninstall(def_zvrf, zvrf->label[afi]);
3901 }
3902 }
3903 }
3904
3905 /*
3906 * Called upon process exiting, need to delete LSP forwarding
3907 * entries from the kernel.
3908 * NOTE: Currently supported only for default VRF.
3909 */
3910 void zebra_mpls_close_tables(struct zebra_vrf *zvrf)
3911 {
3912 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
3913 hash_clean(zvrf->lsp_table, NULL);
3914 hash_free(zvrf->lsp_table);
3915 hash_clean(zvrf->slsp_table, NULL);
3916 hash_free(zvrf->slsp_table);
3917 route_table_finish(zvrf->fec_table[AFI_IP]);
3918 route_table_finish(zvrf->fec_table[AFI_IP6]);
3919 }
3920
3921 /*
3922 * Allocate MPLS tables for this VRF and do other initialization.
3923 * NOTE: Currently supported only for default VRF.
3924 */
3925 void zebra_mpls_init_tables(struct zebra_vrf *zvrf)
3926 {
3927 if (!zvrf)
3928 return;
3929 zvrf->slsp_table =
3930 hash_create(label_hash, label_cmp, "ZEBRA SLSP table");
3931 zvrf->lsp_table = hash_create(label_hash, label_cmp, "ZEBRA LSP table");
3932 zvrf->fec_table[AFI_IP] = route_table_init();
3933 zvrf->fec_table[AFI_IP6] = route_table_init();
3934 zvrf->mpls_flags = 0;
3935 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
3936 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
3937 }
3938
3939 /*
3940 * Global MPLS initialization.
3941 */
3942 void zebra_mpls_init(void)
3943 {
3944 mpls_enabled = 0;
3945
3946 if (mpls_kernel_init() < 0) {
3947 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED,
3948 "Disabling MPLS support (no kernel support)");
3949 return;
3950 }
3951
3952 if (!mpls_processq_init())
3953 mpls_enabled = 1;
3954
3955 hook_register(zserv_client_close, zebra_mpls_cleanup_fecs_for_client);
3956 hook_register(zserv_client_close, zebra_mpls_cleanup_zclient_labels);
3957 }