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