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