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