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