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