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