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