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