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