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