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