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