]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_mpls.c
isisd: implement the 'lsp-too-large' notification
[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"
b78b820d 37#include "lib/json.h"
7758e3f3 38
39#include "zebra/rib.h"
40#include "zebra/rt.h"
86f07f44 41#include "zebra/interface.h"
7758e3f3 42#include "zebra/zserv.h"
43#include "zebra/redistribute.h"
44#include "zebra/debug.h"
45#include "zebra/zebra_memory.h"
46#include "zebra/zebra_vrf.h"
47#include "zebra/zebra_mpls.h"
43e52561 48#include "zebra/zebra_errors.h"
7758e3f3 49
d62a17ae 50DEFINE_MTYPE_STATIC(ZEBRA, LSP, "MPLS LSP object")
51DEFINE_MTYPE_STATIC(ZEBRA, FEC, "MPLS FEC object")
52DEFINE_MTYPE_STATIC(ZEBRA, SLSP, "MPLS static LSP config")
53DEFINE_MTYPE_STATIC(ZEBRA, NHLFE, "MPLS nexthop object")
54DEFINE_MTYPE_STATIC(ZEBRA, SNHLFE, "MPLS static nexthop object")
55DEFINE_MTYPE_STATIC(ZEBRA, SNHLFE_IFNAME, "MPLS static nexthop ifname")
7758e3f3 56
fe6c7157
RW
57int mpls_enabled;
58
7758e3f3 59/* Default rtm_table for all clients */
60extern struct zebra_t zebrad;
61
62/* static function declarations */
f31e084c 63
d62a17ae 64static void fec_evaluate(struct zebra_vrf *zvrf);
d7c0a89a
QY
65static uint32_t fec_derive_label_from_index(struct zebra_vrf *vrf,
66 zebra_fec_t *fec);
d62a17ae 67static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
68 struct route_node *rn, struct route_entry *re);
69static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label);
70static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
71 mpls_label_t old_label);
72static int fec_send(zebra_fec_t *fec, struct zserv *client);
73static void fec_update_clients(zebra_fec_t *fec);
74static void fec_print(zebra_fec_t *fec, struct vty *vty);
75static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p);
76static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
d7c0a89a
QY
77 mpls_label_t label, uint32_t flags,
78 uint32_t label_index);
d62a17ae 79static int fec_del(zebra_fec_t *fec);
80
81static unsigned int label_hash(void *p);
74df8d6d 82static bool label_cmp(const void *p1, const void *p2);
d62a17ae 83static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
84 struct nexthop *nexthop);
85static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
86 struct nexthop *nexthop);
87static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe);
88
89static void lsp_select_best_nhlfe(zebra_lsp_t *lsp);
90static void lsp_uninstall_from_kernel(struct hash_backet *backet, void *ctxt);
91static void lsp_schedule(struct hash_backet *backet, void *ctxt);
92static wq_item_status lsp_process(struct work_queue *wq, void *data);
93static void lsp_processq_del(struct work_queue *wq, void *data);
94static void lsp_processq_complete(struct work_queue *wq);
95static int lsp_processq_add(zebra_lsp_t *lsp);
96static void *lsp_alloc(void *p);
97
98static char *nhlfe2str(zebra_nhlfe_t *nhlfe, char *buf, int size);
99static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
100 union g_addr *gate, ifindex_t ifindex);
101static zebra_nhlfe_t *nhlfe_find(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
102 enum nexthop_types_t gtype, union g_addr *gate,
103 ifindex_t ifindex);
104static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
105 enum nexthop_types_t gtype, union g_addr *gate,
106 ifindex_t ifindex, mpls_label_t out_label);
107static int nhlfe_del(zebra_nhlfe_t *snhlfe);
108static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
8ecdb26e 109 struct mpls_label_stack *nh_label);
d62a17ae 110static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
111 enum lsp_types_t type);
112static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
113 mpls_label_t in_label);
114static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty);
115static void lsp_print(zebra_lsp_t *lsp, void *ctxt);
116static void *slsp_alloc(void *p);
117static int snhlfe_match(zebra_snhlfe_t *snhlfe, enum nexthop_types_t gtype,
118 union g_addr *gate, ifindex_t ifindex);
119static zebra_snhlfe_t *snhlfe_find(zebra_slsp_t *slsp,
120 enum nexthop_types_t gtype,
121 union g_addr *gate, ifindex_t ifindex);
122static zebra_snhlfe_t *snhlfe_add(zebra_slsp_t *slsp,
123 enum nexthop_types_t gtype,
124 union g_addr *gate, ifindex_t ifindex,
125 mpls_label_t out_label);
126static int snhlfe_del(zebra_snhlfe_t *snhlfe);
127static int snhlfe_del_all(zebra_slsp_t *slsp);
128static char *snhlfe2str(zebra_snhlfe_t *snhlfe, char *buf, int size);
129static int mpls_processq_init(struct zebra_t *zebra);
7758e3f3 130
131
132/* Static functions */
133
3d468f66
DS
134/*
135 * Handle failure in LSP install, clear flags for NHLFE.
136 */
137static void clear_nhlfe_installed(zebra_lsp_t *lsp)
138{
139 zebra_nhlfe_t *nhlfe;
140 struct nexthop *nexthop;
141
142 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
143 nexthop = nhlfe->nexthop;
144 if (!nexthop)
145 continue;
146
147 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
148 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
149 }
150}
151
a64448ba
DS
152/*
153 * Install label forwarding entry based on labeled-route entry.
154 */
d62a17ae 155static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
156 struct route_node *rn, struct route_entry *re)
157{
158 struct hash *lsp_table;
159 zebra_ile_t tmp_ile;
160 zebra_lsp_t *lsp;
161 zebra_nhlfe_t *nhlfe;
162 struct nexthop *nexthop;
163 enum lsp_types_t lsp_type;
164 char buf[BUFSIZ];
165 int added, changed;
166
167 /* Lookup table. */
168 lsp_table = zvrf->lsp_table;
169 if (!lsp_table)
170 return -1;
171
172 lsp_type = lsp_type_from_re_type(re->type);
173 added = changed = 0;
174
175 /* Locate or allocate LSP entry. */
176 tmp_ile.in_label = label;
177 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
178 if (!lsp)
179 return -1;
180
181 /* For each active nexthop, create NHLFE. Note that we deliberately skip
182 * recursive nexthops right now, because intermediate hops won't
183 * understand
184 * the label advertised by the recursive nexthop (plus we don't have the
185 * logic yet to push multiple labels).
186 */
7ee30f28 187 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
d62a17ae 188 /* Skip inactive and recursive entries. */
189 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
190 continue;
191 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
192 continue;
193
194 nhlfe = nhlfe_find(lsp, lsp_type, nexthop->type, &nexthop->gate,
195 nexthop->ifindex);
196 if (nhlfe) {
197 /* Clear deleted flag (in case it was set) */
198 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
199 if (nexthop_labels_match(nhlfe->nexthop, nexthop))
200 /* No change */
201 continue;
202
203
204 if (IS_ZEBRA_DEBUG_MPLS) {
205 nhlfe2str(nhlfe, buf, BUFSIZ);
206 zlog_debug(
207 "LSP in-label %u type %d nexthop %s "
208 "out-label changed",
209 lsp->ile.in_label, lsp_type, buf);
210 }
211
212 /* Update out label, trigger processing. */
213 nhlfe_out_label_update(nhlfe, nexthop->nh_label);
214 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
215 changed++;
216 } else {
217 /* Add LSP entry to this nexthop */
218 nhlfe = nhlfe_add(lsp, lsp_type, nexthop->type,
219 &nexthop->gate, nexthop->ifindex,
220 nexthop->nh_label->label[0]);
221 if (!nhlfe)
222 return -1;
223
224 if (IS_ZEBRA_DEBUG_MPLS) {
225 nhlfe2str(nhlfe, buf, BUFSIZ);
226 zlog_debug(
227 "Add LSP in-label %u type %d nexthop %s "
228 "out-label %u",
229 lsp->ile.in_label, lsp_type, buf,
230 nexthop->nh_label->label[0]);
231 }
232
233 lsp->addr_family = NHLFE_FAMILY(nhlfe);
234
235 /* Mark NHLFE as changed. */
236 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
237 added++;
238 }
239 }
240
241 /* Queue LSP for processing if necessary. If no NHLFE got added (special
242 * case), delete the LSP entry; this case results in somewhat ugly
243 * logging.
244 */
245 if (added || changed) {
246 if (lsp_processq_add(lsp))
247 return -1;
248 } else if (!lsp->nhlfe_list
249 && !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED)) {
250 if (IS_ZEBRA_DEBUG_MPLS)
251 zlog_debug("Free LSP in-label %u flags 0x%x",
252 lsp->ile.in_label, lsp->flags);
253
254 lsp = hash_release(lsp_table, &lsp->ile);
255 if (lsp)
256 XFREE(MTYPE_LSP, lsp);
257 }
258
259 return 0;
a64448ba
DS
260}
261
262/*
263 * Uninstall all non-static NHLFEs of a label forwarding entry. If all
264 * NHLFEs are removed, the entire entry is deleted.
265 */
d62a17ae 266static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label)
267{
268 struct hash *lsp_table;
269 zebra_ile_t tmp_ile;
270 zebra_lsp_t *lsp;
271 zebra_nhlfe_t *nhlfe, *nhlfe_next;
272 char buf[BUFSIZ];
273
274 /* Lookup table. */
275 lsp_table = zvrf->lsp_table;
276 if (!lsp_table)
277 return -1;
278
279 /* If entry is not present, exit. */
280 tmp_ile.in_label = label;
281 lsp = hash_lookup(lsp_table, &tmp_ile);
282 if (!lsp || !lsp->nhlfe_list)
283 return 0;
284
285 /* Mark NHLFEs for delete or directly delete, as appropriate. */
286 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe_next) {
287 nhlfe_next = nhlfe->next;
288
289 /* Skip static NHLFEs */
290 if (nhlfe->type == ZEBRA_LSP_STATIC)
291 continue;
292
293 if (IS_ZEBRA_DEBUG_MPLS) {
294 nhlfe2str(nhlfe, buf, BUFSIZ);
295 zlog_debug(
296 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
297 label, nhlfe->type, buf, nhlfe->flags);
298 }
299
300 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)) {
301 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
302 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
303 } else {
304 nhlfe_del(nhlfe);
305 }
306 }
307
308 /* Queue LSP for processing, if needed, else delete. */
309 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
310 if (lsp_processq_add(lsp))
311 return -1;
312 } else if (!lsp->nhlfe_list
313 && !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED)) {
314 if (IS_ZEBRA_DEBUG_MPLS)
315 zlog_debug("Del LSP in-label %u flags 0x%x",
316 lsp->ile.in_label, lsp->flags);
317
318 lsp = hash_release(lsp_table, &lsp->ile);
319 if (lsp)
320 XFREE(MTYPE_LSP, lsp);
321 }
322
323 return 0;
a64448ba
DS
324}
325
28d58fd7
VV
326/*
327 * This function is invoked upon change to label block configuration; it
328 * will walk all registered FECs with label-index and appropriately update
329 * their local labels and trigger client updates.
330 */
d62a17ae 331static void fec_evaluate(struct zebra_vrf *zvrf)
332{
333 struct route_node *rn;
334 zebra_fec_t *fec;
d7c0a89a 335 uint32_t old_label, new_label;
d62a17ae 336 int af;
337 char buf[BUFSIZ];
338
339 for (af = AFI_IP; af < AFI_MAX; af++) {
340 if (zvrf->fec_table[af] == NULL)
341 continue;
342
343 for (rn = route_top(zvrf->fec_table[af]); rn;
344 rn = route_next(rn)) {
345 if ((fec = rn->info) == NULL)
346 continue;
347
348 /* Skip configured FECs and those without a label index.
349 */
350 if (fec->flags & FEC_FLAG_CONFIGURED
351 || fec->label_index == MPLS_INVALID_LABEL_INDEX)
352 continue;
353
354 if (IS_ZEBRA_DEBUG_MPLS)
355 prefix2str(&rn->p, buf, BUFSIZ);
356
357 /* Save old label, determine new label. */
358 old_label = fec->label;
359 new_label =
360 zvrf->mpls_srgb.start_label + fec->label_index;
361 if (new_label >= zvrf->mpls_srgb.end_label)
362 new_label = MPLS_INVALID_LABEL;
363
364 /* If label has changed, update FEC and clients. */
365 if (new_label == old_label)
366 continue;
367
368 if (IS_ZEBRA_DEBUG_MPLS)
369 zlog_debug(
370 "Update fec %s new label %u upon label block",
371 buf, new_label);
372
373 fec->label = new_label;
374 fec_update_clients(fec);
375
376 /* Update label forwarding entries appropriately */
377 fec_change_update_lsp(zvrf, fec, old_label);
378 }
379 }
28d58fd7
VV
380}
381
382/*
383 * Derive (if possible) and update the local label for the FEC based on
384 * its label index. The index is "acceptable" if it falls within the
385 * globally configured label block (SRGB).
386 */
d7c0a89a
QY
387static uint32_t fec_derive_label_from_index(struct zebra_vrf *zvrf,
388 zebra_fec_t *fec)
28d58fd7 389{
d7c0a89a 390 uint32_t label;
28d58fd7 391
d62a17ae 392 if (fec->label_index != MPLS_INVALID_LABEL_INDEX
393 && zvrf->mpls_srgb.start_label
394 && ((label = zvrf->mpls_srgb.start_label + fec->label_index)
395 < zvrf->mpls_srgb.end_label))
396 fec->label = label;
397 else
398 fec->label = MPLS_INVALID_LABEL;
28d58fd7 399
d62a17ae 400 return fec->label;
28d58fd7
VV
401}
402
a64448ba
DS
403/*
404 * There is a change for this FEC. Install or uninstall label forwarding
405 * entries, as appropriate.
406 */
d62a17ae 407static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
408 mpls_label_t old_label)
a64448ba 409{
d62a17ae 410 struct route_table *table;
411 struct route_node *rn;
412 struct route_entry *re;
413 afi_t afi;
a64448ba 414
d62a17ae 415 /* Uninstall label forwarding entry, if previously installed. */
996c9314
LB
416 if (old_label != MPLS_INVALID_LABEL
417 && old_label != MPLS_LABEL_IMPLICIT_NULL)
d62a17ae 418 lsp_uninstall(zvrf, old_label);
a64448ba 419
d62a17ae 420 /* Install label forwarding entry corr. to new label, if needed. */
421 if (fec->label == MPLS_INVALID_LABEL
70e98a7f 422 || fec->label == MPLS_LABEL_IMPLICIT_NULL)
d62a17ae 423 return 0;
a64448ba 424
d62a17ae 425 afi = family2afi(PREFIX_FAMILY(&fec->rn->p));
426 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
427 if (!table)
428 return 0;
a64448ba 429
d62a17ae 430 /* See if labeled route exists. */
431 rn = route_node_lookup(table, &fec->rn->p);
432 if (!rn)
433 return 0;
a64448ba 434
a2addae8 435 RNODE_FOREACH_RE (rn, re) {
d62a17ae 436 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
437 break;
438 }
a64448ba 439
d62a17ae 440 if (!re || !zebra_rib_labeled_unicast(re))
441 return 0;
a64448ba 442
d62a17ae 443 if (lsp_install(zvrf, fec->label, rn, re))
444 return -1;
a64448ba 445
d62a17ae 446 return 0;
a64448ba
DS
447}
448
5aba114a
DS
449/*
450 * Inform about FEC to a registered client.
451 */
d62a17ae 452static int fec_send(zebra_fec_t *fec, struct zserv *client)
5aba114a 453{
d62a17ae 454 struct stream *s;
455 struct route_node *rn;
5aba114a 456
d62a17ae 457 rn = fec->rn;
5aba114a 458
d62a17ae 459 /* Get output stream. */
1002497a 460 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5aba114a 461
7cf15b25 462 zclient_create_header(s, ZEBRA_FEC_UPDATE, VRF_DEFAULT);
5aba114a 463
d62a17ae 464 stream_putw(s, rn->p.family);
465 stream_put_prefix(s, &rn->p);
466 stream_putl(s, fec->label);
467 stream_putw_at(s, 0, stream_get_endp(s));
21ccc0cf 468 return zserv_send_message(client, s);
5aba114a
DS
469}
470
471/*
472 * Update all registered clients about this FEC. Caller should've updated
473 * FEC and ensure no duplicate updates.
474 */
d62a17ae 475static void fec_update_clients(zebra_fec_t *fec)
5aba114a 476{
d62a17ae 477 struct listnode *node;
478 struct zserv *client;
5aba114a 479
d62a17ae 480 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client)) {
481 if (IS_ZEBRA_DEBUG_MPLS)
482 zlog_debug("Update client %s",
483 zebra_route_string(client->proto));
484 fec_send(fec, client);
485 }
5aba114a
DS
486}
487
488
f31e084c
DS
489/*
490 * Print a FEC-label binding entry.
491 */
d62a17ae 492static void fec_print(zebra_fec_t *fec, struct vty *vty)
493{
494 struct route_node *rn;
495 struct listnode *node;
496 struct zserv *client;
497 char buf[BUFSIZ];
498
499 rn = fec->rn;
500 prefix2str(&rn->p, buf, BUFSIZ);
501 vty_out(vty, "%s\n", buf);
502 vty_out(vty, " Label: %s", label2str(fec->label, buf, BUFSIZ));
503 if (fec->label_index != MPLS_INVALID_LABEL_INDEX)
504 vty_out(vty, ", Label Index: %u", fec->label_index);
505 vty_out(vty, "\n");
506 if (!list_isempty(fec->client_list)) {
507 vty_out(vty, " Client list:");
508 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client))
509 vty_out(vty, " %s(fd %d)",
510 zebra_route_string(client->proto),
511 client->sock);
512 vty_out(vty, "\n");
513 }
f31e084c
DS
514}
515
516/*
517 * Locate FEC-label binding that matches with passed info.
518 */
d62a17ae 519static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p)
f31e084c 520{
d62a17ae 521 struct route_node *rn;
f31e084c 522
d62a17ae 523 apply_mask(p);
524 rn = route_node_lookup(table, p);
525 if (!rn)
526 return NULL;
f31e084c 527
d62a17ae 528 route_unlock_node(rn);
529 return (rn->info);
f31e084c
DS
530}
531
532/*
5aba114a
DS
533 * Add a FEC. This may be upon a client registering for a binding
534 * or when a binding is configured.
f31e084c 535 */
d62a17ae 536static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
d7c0a89a
QY
537 mpls_label_t label, uint32_t flags,
538 uint32_t label_index)
f31e084c 539{
d62a17ae 540 struct route_node *rn;
541 zebra_fec_t *fec;
f31e084c 542
d62a17ae 543 apply_mask(p);
f31e084c 544
d62a17ae 545 /* Lookup (or add) route node.*/
546 rn = route_node_get(table, p);
547 if (!rn)
548 return NULL;
f31e084c 549
d62a17ae 550 fec = rn->info;
f31e084c 551
d62a17ae 552 if (!fec) {
553 fec = XCALLOC(MTYPE_FEC, sizeof(zebra_fec_t));
f31e084c 554
d62a17ae 555 rn->info = fec;
556 fec->rn = rn;
557 fec->label = label;
558 fec->client_list = list_new();
559 } else
560 route_unlock_node(rn); /* for the route_node_get */
f31e084c 561
d62a17ae 562 fec->label_index = label_index;
563 fec->flags = flags;
f31e084c 564
d62a17ae 565 return fec;
f31e084c
DS
566}
567
568/*
5aba114a
DS
569 * Delete a FEC. This may be upon the last client deregistering for
570 * a FEC and no binding exists or when the binding is deleted and there
571 * are no registered clients.
f31e084c 572 */
d62a17ae 573static int fec_del(zebra_fec_t *fec)
f31e084c 574{
6a154c88 575 list_delete(&fec->client_list);
d62a17ae 576 fec->rn->info = NULL;
577 route_unlock_node(fec->rn);
578 XFREE(MTYPE_FEC, fec);
579 return 0;
f31e084c
DS
580}
581
7758e3f3 582/*
583 * Hash function for label.
584 */
d62a17ae 585static unsigned int label_hash(void *p)
7758e3f3 586{
d62a17ae 587 const zebra_ile_t *ile = p;
7758e3f3 588
d62a17ae 589 return (jhash_1word(ile->in_label, 0));
7758e3f3 590}
591
592/*
593 * Compare 2 LSP hash entries based on in-label.
594 */
74df8d6d 595static bool label_cmp(const void *p1, const void *p2)
7758e3f3 596{
d62a17ae 597 const zebra_ile_t *ile1 = p1;
598 const zebra_ile_t *ile2 = p2;
7758e3f3 599
d62a17ae 600 return (ile1->in_label == ile2->in_label);
7758e3f3 601}
602
40c7bdb0 603/*
604 * Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
605 * the passed flag.
606 * NOTE: Looking only for connected routes right now.
607 */
d62a17ae 608static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
609 struct nexthop *nexthop)
40c7bdb0 610{
d62a17ae 611 struct route_table *table;
612 struct prefix_ipv4 p;
613 struct route_node *rn;
614 struct route_entry *match;
615 struct nexthop *match_nh;
40c7bdb0 616
4a7371e9 617 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, nexthop->vrf_id);
d62a17ae 618 if (!table)
619 return 0;
40c7bdb0 620
d62a17ae 621 /* Lookup nexthop in IPv4 routing table. */
622 memset(&p, 0, sizeof(struct prefix_ipv4));
623 p.family = AF_INET;
624 p.prefixlen = IPV4_MAX_PREFIXLEN;
625 p.prefix = nexthop->gate.ipv4;
40c7bdb0 626
d62a17ae 627 rn = route_node_match(table, (struct prefix *)&p);
628 if (!rn)
629 return 0;
40c7bdb0 630
d62a17ae 631 route_unlock_node(rn);
88d88a9c 632
d62a17ae 633 /* Locate a valid connected route. */
a2addae8 634 RNODE_FOREACH_RE (rn, match) {
d62a17ae 635 if (CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
636 || !CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
637 continue;
638
7ee30f28 639 for (match_nh = match->ng.nexthop; match_nh;
d62a17ae 640 match_nh = match_nh->next) {
641 if (match->type == ZEBRA_ROUTE_CONNECT
642 || nexthop->ifindex == match_nh->ifindex) {
643 nexthop->ifindex = match_nh->ifindex;
644 return 1;
645 }
646 }
88d88a9c 647 }
40c7bdb0 648
d62a17ae 649 return 0;
40c7bdb0 650}
651
652
653/*
654 * Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
655 * the passed flag.
656 * NOTE: Looking only for connected routes right now.
657 */
d62a17ae 658static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
659 struct nexthop *nexthop)
40c7bdb0 660{
d62a17ae 661 struct route_table *table;
662 struct prefix_ipv6 p;
663 struct route_node *rn;
664 struct route_entry *match;
40c7bdb0 665
4a7371e9 666 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, nexthop->vrf_id);
d62a17ae 667 if (!table)
668 return 0;
40c7bdb0 669
d62a17ae 670 /* Lookup nexthop in IPv6 routing table. */
671 memset(&p, 0, sizeof(struct prefix_ipv6));
672 p.family = AF_INET6;
673 p.prefixlen = IPV6_MAX_PREFIXLEN;
674 p.prefix = nexthop->gate.ipv6;
40c7bdb0 675
d62a17ae 676 rn = route_node_match(table, (struct prefix *)&p);
677 if (!rn)
678 return 0;
40c7bdb0 679
d62a17ae 680 route_unlock_node(rn);
40c7bdb0 681
d62a17ae 682 /* Locate a valid connected route. */
a2addae8 683 RNODE_FOREACH_RE (rn, match) {
d62a17ae 684 if ((match->type == ZEBRA_ROUTE_CONNECT)
685 && !CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
686 && CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
687 break;
688 }
40c7bdb0 689
7ee30f28 690 if (!match || !match->ng.nexthop)
d62a17ae 691 return 0;
40c7bdb0 692
7ee30f28 693 nexthop->ifindex = match->ng.nexthop->ifindex;
d62a17ae 694 return 1;
40c7bdb0 695}
696
697
698/*
699 * Check the nexthop reachability for a NHLFE and return if valid (reachable)
700 * or not.
701 * NOTE: Each NHLFE points to only 1 nexthop.
702 */
d62a17ae 703static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe)
704{
705 struct nexthop *nexthop;
706 struct interface *ifp;
86f07f44 707 struct zebra_ns *zns;
d62a17ae 708
709 nexthop = nhlfe->nexthop;
710 if (!nexthop) // unexpected
711 return 0;
712
713 /* Check on nexthop based on type. */
714 switch (nexthop->type) {
b9abd9ad
DS
715 case NEXTHOP_TYPE_IFINDEX:
716 /*
717 * Lookup if this type is special. The
718 * NEXTHOP_TYPE_IFINDEX is a pop and
719 * forward into a different table for
720 * processing. As such this ifindex
721 * passed to us may be a VRF device
722 * which will not be in the default
723 * VRF. So let's look in all of them
724 */
86f07f44
PG
725 zns = zebra_ns_lookup(NS_DEFAULT);
726 ifp = if_lookup_by_index_per_ns(zns, nexthop->ifindex);
b9abd9ad
DS
727 if (ifp && if_is_operative(ifp))
728 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
729 else
730 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
731 break;
d62a17ae 732 case NEXTHOP_TYPE_IPV4:
733 case NEXTHOP_TYPE_IPV4_IFINDEX:
734 if (nhlfe_nexthop_active_ipv4(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:
741 if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
742 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
743 else
744 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
745 break;
746
747 case NEXTHOP_TYPE_IPV6_IFINDEX:
748 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
4a7371e9
DS
749 ifp = if_lookup_by_index(nexthop->ifindex,
750 nexthop->vrf_id);
d62a17ae 751 if (ifp && if_is_operative(ifp))
752 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
753 else
754 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
755 } else {
756 if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
757 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
758 else
759 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
760 }
761 break;
762
763 default:
764 break;
765 }
766
767 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
40c7bdb0 768}
769
770/*
771 * Walk through NHLFEs for a LSP forwarding entry, verify nexthop
772 * reachability and select the best. Multipath entries are also
773 * marked. This is invoked when an LSP scheduled for processing (due
774 * to some change) is examined.
775 */
d62a17ae 776static void lsp_select_best_nhlfe(zebra_lsp_t *lsp)
777{
778 zebra_nhlfe_t *nhlfe;
779 zebra_nhlfe_t *best;
780 struct nexthop *nexthop;
781 int changed = 0;
782
783 if (!lsp)
784 return;
785
786 best = NULL;
787 lsp->num_ecmp = 0;
788 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
789
790 /*
791 * First compute the best path, after checking nexthop status. We are
792 * only
793 * concerned with non-deleted NHLFEs.
794 */
795 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
796 /* Clear selection flags. */
797 UNSET_FLAG(nhlfe->flags,
798 (NHLFE_FLAG_SELECTED | NHLFE_FLAG_MULTIPATH));
799
800 if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
801 && nhlfe_nexthop_active(nhlfe)) {
802 if (!best || (nhlfe->distance < best->distance))
803 best = nhlfe;
804 }
805 }
806
807 lsp->best_nhlfe = best;
808 if (!lsp->best_nhlfe)
809 return;
810
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 */
820 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
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 */
d62a17ae 859static void lsp_uninstall_from_kernel(struct hash_backet *backet, void *ctxt)
40c7bdb0 860{
d62a17ae 861 zebra_lsp_t *lsp;
40c7bdb0 862
d62a17ae 863 lsp = (zebra_lsp_t *)backet->data;
4a83e7a0 864 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
7c5d0e18 865 (void)kernel_del_lsp(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 */
d62a17ae 872static void lsp_schedule(struct hash_backet *backet, void *ctxt)
40c7bdb0 873{
d62a17ae 874 zebra_lsp_t *lsp;
40c7bdb0 875
d62a17ae 876 lsp = (zebra_lsp_t *)backet->data;
0af35d90 877 (void)lsp_processq_add(lsp);
40c7bdb0 878}
879
880/*
881 * Process a LSP entry that is in the queue. Recalculate best NHLFE and
882 * any multipaths and update or delete from the kernel, as needed.
883 */
d62a17ae 884static wq_item_status lsp_process(struct work_queue *wq, void *data)
885{
886 zebra_lsp_t *lsp;
887 zebra_nhlfe_t *oldbest, *newbest;
888 char buf[BUFSIZ], buf2[BUFSIZ];
889 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
890
891 lsp = (zebra_lsp_t *)data;
892 if (!lsp) // unexpected
893 return WQ_SUCCESS;
894
895 oldbest = lsp->best_nhlfe;
896
897 /* Select best NHLFE(s) */
898 lsp_select_best_nhlfe(lsp);
899
900 newbest = lsp->best_nhlfe;
901
902 if (IS_ZEBRA_DEBUG_MPLS) {
903 if (oldbest)
904 nhlfe2str(oldbest, buf, BUFSIZ);
905 if (newbest)
906 nhlfe2str(newbest, buf2, BUFSIZ);
907 zlog_debug(
908 "Process LSP in-label %u oldbest %s newbest %s "
909 "flags 0x%x ecmp# %d",
910 lsp->ile.in_label, oldbest ? buf : "NULL",
911 newbest ? buf2 : "NULL", lsp->flags, lsp->num_ecmp);
912 }
913
914 if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
915 /* Not already installed */
916 if (newbest) {
2b63430c
DS
917
918 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
7c5d0e18 919 switch (kernel_add_lsp(lsp)) {
ea1c14f6 920 case ZEBRA_DPLANE_REQUEST_QUEUED:
af4c2728 921 flog_err(
e914ccbe 922 EC_ZEBRA_DP_INVALID_RC,
43e52561 923 "No current DataPlane interfaces can return this, please fix");
7c5d0e18 924 break;
ea1c14f6 925 case ZEBRA_DPLANE_REQUEST_FAILURE:
7c5d0e18 926 break;
ea1c14f6 927 case ZEBRA_DPLANE_REQUEST_SUCCESS:
7c5d0e18
DS
928 zvrf->lsp_installs++;
929 break;
930 }
d62a17ae 931 }
932 } else {
933 /* Installed, may need an update and/or delete. */
934 if (!newbest) {
e6d44ec7 935
7c5d0e18 936 switch (kernel_del_lsp(lsp)) {
ea1c14f6 937 case ZEBRA_DPLANE_REQUEST_QUEUED:
af4c2728 938 flog_err(
e914ccbe 939 EC_ZEBRA_DP_INVALID_RC,
43e52561 940 "No current DataPlane interfaces can return this, please fix");
7c5d0e18 941 break;
ea1c14f6 942 case ZEBRA_DPLANE_REQUEST_FAILURE:
7c5d0e18 943 break;
ea1c14f6 944 case ZEBRA_DPLANE_REQUEST_SUCCESS:
7c5d0e18
DS
945 zvrf->lsp_removals++;
946 break;
947 }
d62a17ae 948 } else if (CHECK_FLAG(lsp->flags, LSP_FLAG_CHANGED)) {
9fa38ec6
DS
949 zebra_nhlfe_t *nhlfe;
950 struct nexthop *nexthop;
2b63430c
DS
951
952 UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
953 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
e6d44ec7 954
9fa38ec6
DS
955 /*
956 * Any NHLFE that was installed but is not
957 * selected now needs to have its flags updated.
958 */
996c9314
LB
959 for (nhlfe = lsp->nhlfe_list; nhlfe;
960 nhlfe = nhlfe->next) {
9fa38ec6
DS
961 nexthop = nhlfe->nexthop;
962 if (!nexthop)
963 continue;
964
965 if (CHECK_FLAG(nhlfe->flags,
996c9314
LB
966 NHLFE_FLAG_INSTALLED)
967 && !CHECK_FLAG(nhlfe->flags,
968 NHLFE_FLAG_SELECTED)) {
9fa38ec6
DS
969 UNSET_FLAG(nhlfe->flags,
970 NHLFE_FLAG_INSTALLED);
971 UNSET_FLAG(nexthop->flags,
972 NEXTHOP_FLAG_FIB);
973 }
974 }
975
7c5d0e18 976 switch (kernel_upd_lsp(lsp)) {
ea1c14f6 977 case ZEBRA_DPLANE_REQUEST_QUEUED:
af4c2728 978 flog_err(
e914ccbe 979 EC_ZEBRA_DP_INVALID_RC,
43e52561 980 "No current DataPlane interfaces can return this, please fix");
7c5d0e18 981 break;
ea1c14f6 982 case ZEBRA_DPLANE_REQUEST_FAILURE:
7c5d0e18 983 break;
ea1c14f6 984 case ZEBRA_DPLANE_REQUEST_SUCCESS:
7c5d0e18
DS
985 zvrf->lsp_installs++;
986 break;
987 }
d62a17ae 988 }
989 }
990
991 return WQ_SUCCESS;
40c7bdb0 992}
993
994
995/*
996 * Callback upon processing completion of a LSP forwarding entry.
997 */
d62a17ae 998static void lsp_processq_del(struct work_queue *wq, void *data)
40c7bdb0 999{
d62a17ae 1000 struct zebra_vrf *zvrf;
1001 zebra_lsp_t *lsp;
1002 struct hash *lsp_table;
1003 zebra_nhlfe_t *nhlfe, *nhlfe_next;
40c7bdb0 1004
d62a17ae 1005 zvrf = vrf_info_lookup(VRF_DEFAULT);
1006 assert(zvrf);
40c7bdb0 1007
d62a17ae 1008 lsp_table = zvrf->lsp_table;
1009 if (!lsp_table) // unexpected
1010 return;
40c7bdb0 1011
d62a17ae 1012 lsp = (zebra_lsp_t *)data;
1013 if (!lsp) // unexpected
1014 return;
40c7bdb0 1015
d62a17ae 1016 /* Clear flag, remove any NHLFEs marked for deletion. If no NHLFEs
1017 * exist,
1018 * delete LSP entry also.
1019 */
1020 UNSET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
40c7bdb0 1021
d62a17ae 1022 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe_next) {
1023 nhlfe_next = nhlfe->next;
1024 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
1025 nhlfe_del(nhlfe);
1026 }
40c7bdb0 1027
d62a17ae 1028 if (!lsp->nhlfe_list) {
1029 if (IS_ZEBRA_DEBUG_MPLS)
1030 zlog_debug("Free LSP in-label %u flags 0x%x",
1031 lsp->ile.in_label, lsp->flags);
40c7bdb0 1032
d62a17ae 1033 lsp = hash_release(lsp_table, &lsp->ile);
1034 if (lsp)
1035 XFREE(MTYPE_LSP, lsp);
1036 }
40c7bdb0 1037}
1038
1039/*
1040 * Callback upon finishing the processing of all scheduled
1041 * LSP forwarding entries.
1042 */
d62a17ae 1043static void lsp_processq_complete(struct work_queue *wq)
40c7bdb0 1044{
d62a17ae 1045 /* Nothing to do for now. */
40c7bdb0 1046}
1047
1048/*
1049 * Add LSP forwarding entry to queue for subsequent processing.
1050 */
d62a17ae 1051static int lsp_processq_add(zebra_lsp_t *lsp)
40c7bdb0 1052{
d62a17ae 1053 /* If already scheduled, exit. */
1054 if (CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED))
1055 return 0;
40c7bdb0 1056
d62a17ae 1057 if (zebrad.lsp_process_q == NULL) {
e914ccbe 1058 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 1059 "%s: work_queue does not exist!", __func__);
d62a17ae 1060 return -1;
1061 }
33c32282 1062
d62a17ae 1063 work_queue_add(zebrad.lsp_process_q, lsp);
1064 SET_FLAG(lsp->flags, LSP_FLAG_SCHEDULED);
1065 return 0;
40c7bdb0 1066}
1067
1068/*
1069 * Callback to allocate LSP forwarding table entry.
1070 */
d62a17ae 1071static void *lsp_alloc(void *p)
40c7bdb0 1072{
d62a17ae 1073 const zebra_ile_t *ile = p;
1074 zebra_lsp_t *lsp;
40c7bdb0 1075
d62a17ae 1076 lsp = XCALLOC(MTYPE_LSP, sizeof(zebra_lsp_t));
1077 lsp->ile = *ile;
40c7bdb0 1078
d62a17ae 1079 if (IS_ZEBRA_DEBUG_MPLS)
1080 zlog_debug("Alloc LSP in-label %u", lsp->ile.in_label);
40c7bdb0 1081
d62a17ae 1082 return ((void *)lsp);
40c7bdb0 1083}
1084
1085/*
1086 * Create printable string for NHLFE entry.
1087 */
d62a17ae 1088static char *nhlfe2str(zebra_nhlfe_t *nhlfe, char *buf, int size)
40c7bdb0 1089{
d62a17ae 1090 struct nexthop *nexthop;
40c7bdb0 1091
d62a17ae 1092 buf[0] = '\0';
1093 nexthop = nhlfe->nexthop;
1094 switch (nexthop->type) {
1095 case NEXTHOP_TYPE_IPV4:
1096 case NEXTHOP_TYPE_IPV4_IFINDEX:
1097 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, size);
1098 break;
1099 case NEXTHOP_TYPE_IPV6:
1100 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, size);
1101 break;
b9abd9ad
DS
1102 case NEXTHOP_TYPE_IFINDEX:
1103 snprintf(buf, size, "Ifindex: %u", nexthop->ifindex);
d62a17ae 1104 default:
1105 break;
1106 }
40c7bdb0 1107
d62a17ae 1108 return buf;
40c7bdb0 1109}
1110
1111/*
1112 * Check if NHLFE matches with search info passed.
1113 */
d62a17ae 1114static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
1115 union g_addr *gate, ifindex_t ifindex)
40c7bdb0 1116{
d62a17ae 1117 struct nexthop *nhop;
1118 int cmp = 1;
40c7bdb0 1119
d62a17ae 1120 nhop = nhlfe->nexthop;
1121 if (!nhop)
1122 return 1;
40c7bdb0 1123
d62a17ae 1124 if (nhop->type != gtype)
1125 return 1;
40c7bdb0 1126
d62a17ae 1127 switch (nhop->type) {
1128 case NEXTHOP_TYPE_IPV4:
1129 case NEXTHOP_TYPE_IPV4_IFINDEX:
1130 cmp = memcmp(&(nhop->gate.ipv4), &(gate->ipv4),
1131 sizeof(struct in_addr));
1132 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
1133 cmp = !(nhop->ifindex == ifindex);
1134 break;
1135 case NEXTHOP_TYPE_IPV6:
1136 case NEXTHOP_TYPE_IPV6_IFINDEX:
1137 cmp = memcmp(&(nhop->gate.ipv6), &(gate->ipv6),
1138 sizeof(struct in6_addr));
1139 if (!cmp && nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
1140 cmp = !(nhop->ifindex == ifindex);
1141 break;
b9abd9ad
DS
1142 case NEXTHOP_TYPE_IFINDEX:
1143 cmp = !(nhop->ifindex == ifindex);
1144 break;
d62a17ae 1145 default:
1146 break;
1147 }
40c7bdb0 1148
d62a17ae 1149 return cmp;
40c7bdb0 1150}
1151
1152
1153/*
1154 * Locate NHLFE that matches with passed info.
1155 */
d62a17ae 1156static zebra_nhlfe_t *nhlfe_find(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1157 enum nexthop_types_t gtype, union g_addr *gate,
1158 ifindex_t ifindex)
40c7bdb0 1159{
d62a17ae 1160 zebra_nhlfe_t *nhlfe;
40c7bdb0 1161
d62a17ae 1162 if (!lsp)
1163 return NULL;
40c7bdb0 1164
d62a17ae 1165 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
1166 if (nhlfe->type != lsp_type)
1167 continue;
1168 if (!nhlfe_nhop_match(nhlfe, gtype, gate, ifindex))
1169 break;
1170 }
40c7bdb0 1171
d62a17ae 1172 return nhlfe;
40c7bdb0 1173}
1174
1175/*
1176 * Add NHLFE. Base entry must have been created and duplicate
1177 * check done.
1178 */
d62a17ae 1179static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
1180 enum nexthop_types_t gtype, union g_addr *gate,
1181 ifindex_t ifindex, mpls_label_t out_label)
1182{
1183 zebra_nhlfe_t *nhlfe;
1184 struct nexthop *nexthop;
1185
1186 if (!lsp)
1187 return NULL;
1188
1189 nhlfe = XCALLOC(MTYPE_NHLFE, sizeof(zebra_nhlfe_t));
d62a17ae 1190
1191 nhlfe->lsp = lsp;
1192 nhlfe->type = lsp_type;
1193 nhlfe->distance = lsp_distance(lsp_type);
1194
1195 nexthop = nexthop_new();
1196 if (!nexthop) {
1197 XFREE(MTYPE_NHLFE, nhlfe);
1198 return NULL;
1199 }
1200 nexthop_add_labels(nexthop, lsp_type, 1, &out_label);
1201
4a7371e9 1202 nexthop->vrf_id = VRF_DEFAULT;
d62a17ae 1203 nexthop->type = gtype;
1204 switch (nexthop->type) {
1205 case NEXTHOP_TYPE_IPV4:
1206 case NEXTHOP_TYPE_IPV4_IFINDEX:
1207 nexthop->gate.ipv4 = gate->ipv4;
1208 if (ifindex)
1209 nexthop->ifindex = ifindex;
1210 break;
1211 case NEXTHOP_TYPE_IPV6:
1212 case NEXTHOP_TYPE_IPV6_IFINDEX:
1213 nexthop->gate.ipv6 = gate->ipv6;
1214 if (ifindex)
1215 nexthop->ifindex = ifindex;
1216 break;
b9abd9ad
DS
1217 case NEXTHOP_TYPE_IFINDEX:
1218 nexthop->ifindex = ifindex;
1219 break;
d62a17ae 1220 default:
1221 nexthop_free(nexthop);
1222 XFREE(MTYPE_NHLFE, nhlfe);
1223 return NULL;
1224 break;
1225 }
1226
1227 nhlfe->nexthop = nexthop;
1228 if (lsp->nhlfe_list)
1229 lsp->nhlfe_list->prev = nhlfe;
1230 nhlfe->next = lsp->nhlfe_list;
1231 lsp->nhlfe_list = nhlfe;
1232
1233 return nhlfe;
40c7bdb0 1234}
1235
1236/*
1237 * Delete NHLFE. Entry must be present on list.
1238 */
d62a17ae 1239static int nhlfe_del(zebra_nhlfe_t *nhlfe)
40c7bdb0 1240{
d62a17ae 1241 zebra_lsp_t *lsp;
40c7bdb0 1242
d62a17ae 1243 if (!nhlfe)
1244 return -1;
40c7bdb0 1245
d62a17ae 1246 lsp = nhlfe->lsp;
1247 if (!lsp)
1248 return -1;
40c7bdb0 1249
d62a17ae 1250 /* Free nexthop. */
1251 if (nhlfe->nexthop)
1252 nexthop_free(nhlfe->nexthop);
40c7bdb0 1253
d62a17ae 1254 /* Unlink from LSP */
1255 if (nhlfe->next)
1256 nhlfe->next->prev = nhlfe->prev;
1257 if (nhlfe->prev)
1258 nhlfe->prev->next = nhlfe->next;
1259 else
1260 lsp->nhlfe_list = nhlfe->next;
40c7bdb0 1261
d62a17ae 1262 if (nhlfe == lsp->best_nhlfe)
1263 lsp->best_nhlfe = NULL;
bb49a121 1264
d62a17ae 1265 XFREE(MTYPE_NHLFE, nhlfe);
40c7bdb0 1266
d62a17ae 1267 return 0;
40c7bdb0 1268}
1269
a64448ba
DS
1270/*
1271 * Update label for NHLFE entry.
1272 */
d62a17ae 1273static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
8ecdb26e 1274 struct mpls_label_stack *nh_label)
d62a17ae 1275{
1276 nhlfe->nexthop->nh_label->label[0] = nh_label->label[0];
1277}
1278
1279static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
1280 enum lsp_types_t type)
1281{
1282 zebra_nhlfe_t *nhlfe, *nhlfe_next;
1283 int schedule_lsp = 0;
1284 char buf[BUFSIZ];
1285
1286 /* Mark NHLFEs for delete or directly delete, as appropriate. */
1287 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe_next) {
1288 nhlfe_next = nhlfe->next;
1289
1290 /* Skip non-static NHLFEs */
1291 if (nhlfe->type != type)
1292 continue;
1293
1294 if (IS_ZEBRA_DEBUG_MPLS) {
1295 nhlfe2str(nhlfe, buf, BUFSIZ);
1296 zlog_debug(
1297 "Del LSP in-label %u type %d nexthop %s flags 0x%x",
1298 lsp->ile.in_label, type, buf, nhlfe->flags);
1299 }
1300
1301 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
1302 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
1303 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
1304 schedule_lsp = 1;
1305 } else {
1306 nhlfe_del(nhlfe);
1307 }
1308 }
1309
1310 /* Queue LSP for processing, if needed, else delete. */
1311 if (schedule_lsp) {
1312 if (lsp_processq_add(lsp))
1313 return -1;
1314 } else if (!lsp->nhlfe_list
1315 && !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED)) {
1316 if (IS_ZEBRA_DEBUG_MPLS)
1317 zlog_debug("Free LSP in-label %u flags 0x%x",
1318 lsp->ile.in_label, lsp->flags);
1319
1320 lsp = hash_release(lsp_table, &lsp->ile);
1321 if (lsp)
1322 XFREE(MTYPE_LSP, lsp);
1323 }
1324
1325 return 0;
40c7bdb0 1326}
1327
ce549947
RW
1328/*
1329 * Uninstall all static NHLFEs for a particular LSP forwarding entry.
1330 * If no other NHLFEs exist, the entry would be deleted.
1331 */
d62a17ae 1332static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
1333 mpls_label_t in_label)
ce549947 1334{
d62a17ae 1335 struct hash *lsp_table;
1336 zebra_ile_t tmp_ile;
1337 zebra_lsp_t *lsp;
ce549947 1338
d62a17ae 1339 /* Lookup table. */
1340 lsp_table = zvrf->lsp_table;
1341 if (!lsp_table)
1342 return -1;
ce549947 1343
d62a17ae 1344 /* If entry is not present, exit. */
1345 tmp_ile.in_label = in_label;
1346 lsp = hash_lookup(lsp_table, &tmp_ile);
1347 if (!lsp || !lsp->nhlfe_list)
1348 return 0;
ce549947 1349
d62a17ae 1350 return mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_STATIC);
ce549947
RW
1351}
1352
d62a17ae 1353static json_object *nhlfe_json(zebra_nhlfe_t *nhlfe)
b78b820d 1354{
d62a17ae 1355 char buf[BUFSIZ];
1356 json_object *json_nhlfe = NULL;
1357 struct nexthop *nexthop = nhlfe->nexthop;
b78b820d 1358
d62a17ae 1359 json_nhlfe = json_object_new_object();
1360 json_object_string_add(json_nhlfe, "type", nhlfe_type2str(nhlfe->type));
1361 json_object_int_add(json_nhlfe, "outLabel",
1362 nexthop->nh_label->label[0]);
1363 json_object_int_add(json_nhlfe, "distance", nhlfe->distance);
b78b820d 1364
d62a17ae 1365 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED))
1366 json_object_boolean_true_add(json_nhlfe, "installed");
b78b820d 1367
d62a17ae 1368 switch (nexthop->type) {
1369 case NEXTHOP_TYPE_IPV4:
1370 case NEXTHOP_TYPE_IPV4_IFINDEX:
1371 json_object_string_add(json_nhlfe, "nexthop",
1372 inet_ntoa(nexthop->gate.ipv4));
1373 break;
1374 case NEXTHOP_TYPE_IPV6:
1375 case NEXTHOP_TYPE_IPV6_IFINDEX:
1376 json_object_string_add(
1377 json_nhlfe, "nexthop",
1378 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1379
1380 if (nexthop->ifindex)
4a7371e9
DS
1381 json_object_string_add(json_nhlfe, "interface",
1382 ifindex2ifname(nexthop->ifindex,
1383 nexthop->vrf_id));
d62a17ae 1384 break;
1385 default:
1386 break;
1387 }
1388 return json_nhlfe;
b78b820d 1389}
1390
3ab18ff2 1391/*
1392 * Print the NHLFE for a LSP forwarding entry.
1393 */
d62a17ae 1394static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty)
1395{
1396 struct nexthop *nexthop;
1397 char buf[BUFSIZ];
1398
1399 nexthop = nhlfe->nexthop;
1400 if (!nexthop || !nexthop->nh_label) // unexpected
1401 return;
1402
1403 vty_out(vty, " type: %s remote label: %s distance: %d\n",
1404 nhlfe_type2str(nhlfe->type),
1405 label2str(nexthop->nh_label->label[0], buf, BUFSIZ),
1406 nhlfe->distance);
1407 switch (nexthop->type) {
1408 case NEXTHOP_TYPE_IPV4:
1409 case NEXTHOP_TYPE_IPV4_IFINDEX:
1410 vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4));
1411 if (nexthop->ifindex)
1412 vty_out(vty, " dev %s",
4a7371e9
DS
1413 ifindex2ifname(nexthop->ifindex,
1414 nexthop->vrf_id));
d62a17ae 1415 break;
1416 case NEXTHOP_TYPE_IPV6:
1417 case NEXTHOP_TYPE_IPV6_IFINDEX:
1418 vty_out(vty, " via %s",
1419 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1420 if (nexthop->ifindex)
1421 vty_out(vty, " dev %s",
4a7371e9
DS
1422 ifindex2ifname(nexthop->ifindex,
1423 nexthop->vrf_id));
d62a17ae 1424 break;
1425 default:
1426 break;
1427 }
996c9314
LB
1428 vty_out(vty, "%s",
1429 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ? " (installed)"
1430 : "");
d62a17ae 1431 vty_out(vty, "\n");
3ab18ff2 1432}
1433
1434/*
1435 * Print an LSP forwarding entry.
1436 */
d62a17ae 1437static void lsp_print(zebra_lsp_t *lsp, void *ctxt)
3ab18ff2 1438{
d62a17ae 1439 zebra_nhlfe_t *nhlfe;
1440 struct vty *vty;
3ab18ff2 1441
d62a17ae 1442 vty = (struct vty *)ctxt;
3ab18ff2 1443
d62a17ae 1444 vty_out(vty, "Local label: %u%s\n", lsp->ile.in_label,
1445 CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) ? " (installed)"
1446 : "");
3ab18ff2 1447
d62a17ae 1448 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next)
1449 nhlfe_print(nhlfe, vty);
3ab18ff2 1450}
1451
1452/*
b78b820d 1453 * JSON objects for an LSP forwarding entry.
3ab18ff2 1454 */
d62a17ae 1455static json_object *lsp_json(zebra_lsp_t *lsp)
3ab18ff2 1456{
d62a17ae 1457 zebra_nhlfe_t *nhlfe = NULL;
1458 json_object *json = json_object_new_object();
1459 json_object *json_nhlfe_list = json_object_new_array();
3ab18ff2 1460
d62a17ae 1461 json_object_int_add(json, "inLabel", lsp->ile.in_label);
b78b820d 1462
d62a17ae 1463 if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
1464 json_object_boolean_true_add(json, "installed");
3ab18ff2 1465
d62a17ae 1466 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next)
1467 json_object_array_add(json_nhlfe_list, nhlfe_json(nhlfe));
b78b820d 1468
d62a17ae 1469 json_object_object_add(json, "nexthops", json_nhlfe_list);
1470 return json;
b78b820d 1471}
1472
1473
1474/* Return a sorted linked list of the hash contents */
d62a17ae 1475static struct list *hash_get_sorted_list(struct hash *hash, void *cmp)
b78b820d 1476{
d62a17ae 1477 unsigned int i;
1478 struct hash_backet *hb;
1479 struct list *sorted_list = list_new();
b78b820d 1480
d62a17ae 1481 sorted_list->cmp = (int (*)(void *, void *))cmp;
b78b820d 1482
d62a17ae 1483 for (i = 0; i < hash->size; i++)
1484 for (hb = hash->index[i]; hb; hb = hb->next)
1485 listnode_add_sort(sorted_list, hb->data);
b78b820d 1486
d62a17ae 1487 return sorted_list;
3ab18ff2 1488}
1489
7758e3f3 1490/*
b78b820d 1491 * Compare two LSPs based on their label values.
7758e3f3 1492 */
d62a17ae 1493static int lsp_cmp(zebra_lsp_t *lsp1, zebra_lsp_t *lsp2)
7758e3f3 1494{
d62a17ae 1495 if (lsp1->ile.in_label < lsp2->ile.in_label)
1496 return -1;
7758e3f3 1497
d62a17ae 1498 if (lsp1->ile.in_label > lsp2->ile.in_label)
1499 return 1;
7758e3f3 1500
d62a17ae 1501 return 0;
7758e3f3 1502}
1503
1504/*
1505 * Callback to allocate static LSP.
1506 */
d62a17ae 1507static void *slsp_alloc(void *p)
7758e3f3 1508{
d62a17ae 1509 const zebra_ile_t *ile = p;
1510 zebra_slsp_t *slsp;
7758e3f3 1511
d62a17ae 1512 slsp = XCALLOC(MTYPE_SLSP, sizeof(zebra_slsp_t));
1513 slsp->ile = *ile;
1514 return ((void *)slsp);
7758e3f3 1515}
1516
b78b820d 1517/*
1518 * Compare two static LSPs based on their label values.
1519 */
d62a17ae 1520static int slsp_cmp(zebra_slsp_t *slsp1, zebra_slsp_t *slsp2)
b78b820d 1521{
d62a17ae 1522 if (slsp1->ile.in_label < slsp2->ile.in_label)
1523 return -1;
b78b820d 1524
d62a17ae 1525 if (slsp1->ile.in_label > slsp2->ile.in_label)
1526 return 1;
b78b820d 1527
d62a17ae 1528 return 0;
b78b820d 1529}
1530
7758e3f3 1531/*
1532 * Check if static NHLFE matches with search info passed.
1533 */
d62a17ae 1534static int snhlfe_match(zebra_snhlfe_t *snhlfe, enum nexthop_types_t gtype,
1535 union g_addr *gate, ifindex_t ifindex)
7758e3f3 1536{
d62a17ae 1537 int cmp = 1;
7758e3f3 1538
d62a17ae 1539 if (snhlfe->gtype != gtype)
1540 return 1;
7758e3f3 1541
d62a17ae 1542 switch (snhlfe->gtype) {
1543 case NEXTHOP_TYPE_IPV4:
1544 cmp = memcmp(&(snhlfe->gate.ipv4), &(gate->ipv4),
1545 sizeof(struct in_addr));
1546 break;
1547 case NEXTHOP_TYPE_IPV6:
1548 case NEXTHOP_TYPE_IPV6_IFINDEX:
1549 cmp = memcmp(&(snhlfe->gate.ipv6), &(gate->ipv6),
1550 sizeof(struct in6_addr));
1551 if (!cmp && snhlfe->gtype == NEXTHOP_TYPE_IPV6_IFINDEX)
1552 cmp = !(snhlfe->ifindex == ifindex);
1553 break;
1554 default:
1555 break;
1556 }
7758e3f3 1557
d62a17ae 1558 return cmp;
7758e3f3 1559}
1560
1561/*
1562 * Locate static NHLFE that matches with passed info.
1563 */
d62a17ae 1564static zebra_snhlfe_t *snhlfe_find(zebra_slsp_t *slsp,
1565 enum nexthop_types_t gtype,
1566 union g_addr *gate, ifindex_t ifindex)
7758e3f3 1567{
d62a17ae 1568 zebra_snhlfe_t *snhlfe;
7758e3f3 1569
d62a17ae 1570 if (!slsp)
1571 return NULL;
7758e3f3 1572
d62a17ae 1573 for (snhlfe = slsp->snhlfe_list; snhlfe; snhlfe = snhlfe->next) {
1574 if (!snhlfe_match(snhlfe, gtype, gate, ifindex))
1575 break;
1576 }
7758e3f3 1577
d62a17ae 1578 return snhlfe;
7758e3f3 1579}
1580
1581
1582/*
1583 * Add static NHLFE. Base LSP config entry must have been created
1584 * and duplicate check done.
1585 */
d62a17ae 1586static zebra_snhlfe_t *snhlfe_add(zebra_slsp_t *slsp,
1587 enum nexthop_types_t gtype,
1588 union g_addr *gate, ifindex_t ifindex,
1589 mpls_label_t out_label)
1590{
1591 zebra_snhlfe_t *snhlfe;
1592
1593 if (!slsp)
1594 return NULL;
1595
1596 snhlfe = XCALLOC(MTYPE_SNHLFE, sizeof(zebra_snhlfe_t));
1597 snhlfe->slsp = slsp;
1598 snhlfe->out_label = out_label;
1599 snhlfe->gtype = gtype;
1600 switch (gtype) {
1601 case NEXTHOP_TYPE_IPV4:
1602 snhlfe->gate.ipv4 = gate->ipv4;
1603 break;
1604 case NEXTHOP_TYPE_IPV6:
1605 case NEXTHOP_TYPE_IPV6_IFINDEX:
1606 snhlfe->gate.ipv6 = gate->ipv6;
1607 if (ifindex)
1608 snhlfe->ifindex = ifindex;
1609 break;
1610 default:
1611 XFREE(MTYPE_SNHLFE, snhlfe);
1612 return NULL;
1613 }
1614
1615 if (slsp->snhlfe_list)
1616 slsp->snhlfe_list->prev = snhlfe;
1617 snhlfe->next = slsp->snhlfe_list;
1618 slsp->snhlfe_list = snhlfe;
1619
1620 return snhlfe;
7758e3f3 1621}
1622
1623/*
1624 * Delete static NHLFE. Entry must be present on list.
1625 */
d62a17ae 1626static int snhlfe_del(zebra_snhlfe_t *snhlfe)
7758e3f3 1627{
d62a17ae 1628 zebra_slsp_t *slsp;
7758e3f3 1629
d62a17ae 1630 if (!snhlfe)
1631 return -1;
7758e3f3 1632
d62a17ae 1633 slsp = snhlfe->slsp;
1634 if (!slsp)
1635 return -1;
7758e3f3 1636
d62a17ae 1637 if (snhlfe->next)
1638 snhlfe->next->prev = snhlfe->prev;
1639 if (snhlfe->prev)
1640 snhlfe->prev->next = snhlfe->next;
1641 else
1642 slsp->snhlfe_list = snhlfe->next;
7758e3f3 1643
d62a17ae 1644 snhlfe->prev = snhlfe->next = NULL;
1645 if (snhlfe->ifname)
1646 XFREE(MTYPE_SNHLFE_IFNAME, snhlfe->ifname);
1647 XFREE(MTYPE_SNHLFE, snhlfe);
7758e3f3 1648
d62a17ae 1649 return 0;
7758e3f3 1650}
1651
1652/*
1653 * Delete all static NHLFE entries for this LSP (in label).
1654 */
d62a17ae 1655static int snhlfe_del_all(zebra_slsp_t *slsp)
7758e3f3 1656{
d62a17ae 1657 zebra_snhlfe_t *snhlfe, *snhlfe_next;
7758e3f3 1658
d62a17ae 1659 if (!slsp)
1660 return -1;
7758e3f3 1661
d62a17ae 1662 for (snhlfe = slsp->snhlfe_list; snhlfe; snhlfe = snhlfe_next) {
1663 snhlfe_next = snhlfe->next;
1664 snhlfe_del(snhlfe);
1665 }
7758e3f3 1666
d62a17ae 1667 return 0;
7758e3f3 1668}
1669
1670/*
1671 * Create printable string for NHLFE configuration.
1672 */
d62a17ae 1673static char *snhlfe2str(zebra_snhlfe_t *snhlfe, char *buf, int size)
7758e3f3 1674{
d62a17ae 1675 buf[0] = '\0';
1676 switch (snhlfe->gtype) {
1677 case NEXTHOP_TYPE_IPV4:
1678 inet_ntop(AF_INET, &snhlfe->gate.ipv4, buf, size);
1679 break;
1680 case NEXTHOP_TYPE_IPV6:
1681 case NEXTHOP_TYPE_IPV6_IFINDEX:
1682 inet_ntop(AF_INET6, &snhlfe->gate.ipv6, buf, size);
1683 if (snhlfe->ifindex)
1684 strcat(buf,
1685 ifindex2ifname(snhlfe->ifindex, VRF_DEFAULT));
1686 break;
1687 default:
1688 break;
1689 }
7758e3f3 1690
d62a17ae 1691 return buf;
7758e3f3 1692}
1693
40c7bdb0 1694/*
1695 * Initialize work queue for processing changed LSPs.
1696 */
d62a17ae 1697static int mpls_processq_init(struct zebra_t *zebra)
40c7bdb0 1698{
d62a17ae 1699 zebra->lsp_process_q = work_queue_new(zebra->master, "LSP processing");
1700 if (!zebra->lsp_process_q) {
e914ccbe 1701 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 1702 "%s: could not initialise work queue!", __func__);
d62a17ae 1703 return -1;
1704 }
40c7bdb0 1705
d62a17ae 1706 zebra->lsp_process_q->spec.workfunc = &lsp_process;
1707 zebra->lsp_process_q->spec.del_item_data = &lsp_processq_del;
1708 zebra->lsp_process_q->spec.errorfunc = NULL;
1709 zebra->lsp_process_q->spec.completion_func = &lsp_processq_complete;
1710 zebra->lsp_process_q->spec.max_retries = 0;
1711 zebra->lsp_process_q->spec.hold = 10;
33c32282 1712
d62a17ae 1713 return 0;
40c7bdb0 1714}
1715
7758e3f3 1716
7758e3f3 1717/* Public functions */
1718
ea1c14f6 1719void kernel_lsp_pass_fail(zebra_lsp_t *lsp, enum zebra_dplane_status res)
4a83e7a0
DS
1720{
1721 struct nexthop *nexthop;
1722 zebra_nhlfe_t *nhlfe;
1723
1724 if (!lsp)
1725 return;
1726
1727 switch (res) {
ea1c14f6 1728 case ZEBRA_DPLANE_INSTALL_FAILURE:
4a83e7a0
DS
1729 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1730 clear_nhlfe_installed(lsp);
e914ccbe 1731 flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
9df414fe 1732 "LSP Install Failure: %u", lsp->ile.in_label);
4a83e7a0 1733 break;
ea1c14f6 1734 case ZEBRA_DPLANE_INSTALL_SUCCESS:
4a83e7a0
DS
1735 SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1736 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
1737 nexthop = nhlfe->nexthop;
1738 if (!nexthop)
1739 continue;
1740
1741 SET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
1742 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1743 }
1744 break;
ea1c14f6 1745 case ZEBRA_DPLANE_DELETE_SUCCESS:
4a83e7a0
DS
1746 UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
1747 clear_nhlfe_installed(lsp);
1748 break;
ea1c14f6 1749 case ZEBRA_DPLANE_DELETE_FAILURE:
e914ccbe 1750 flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
9df414fe 1751 "LSP Deletion Failure: %u", lsp->ile.in_label);
4a83e7a0 1752 break;
ea1c14f6
MS
1753 case ZEBRA_DPLANE_STATUS_NONE:
1754 break;
4a83e7a0
DS
1755 }
1756}
1757
a64448ba
DS
1758/*
1759 * Install dynamic LSP entry.
1760 */
d62a17ae 1761int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
1762 struct route_entry *re)
a64448ba 1763{
d62a17ae 1764 struct route_table *table;
1765 zebra_fec_t *fec;
a64448ba 1766
d62a17ae 1767 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
1768 if (!table)
1769 return -1;
a64448ba 1770
d62a17ae 1771 /* See if there is a configured label binding for this FEC. */
1772 fec = fec_find(table, &rn->p);
1773 if (!fec || fec->label == MPLS_INVALID_LABEL)
1774 return 0;
a64448ba 1775
d62a17ae 1776 /* We cannot install a label forwarding entry if local label is the
1777 * implicit-null label.
1778 */
70e98a7f 1779 if (fec->label == MPLS_LABEL_IMPLICIT_NULL)
d62a17ae 1780 return 0;
a64448ba 1781
d62a17ae 1782 if (lsp_install(zvrf, fec->label, rn, re))
1783 return -1;
a64448ba 1784
d62a17ae 1785 return 0;
a64448ba
DS
1786}
1787
1788/*
1789 * Uninstall dynamic LSP entry, if any.
1790 */
d62a17ae 1791int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
1792 struct route_entry *re)
a64448ba 1793{
d62a17ae 1794 struct route_table *table;
1795 zebra_fec_t *fec;
a64448ba 1796
d62a17ae 1797 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
1798 if (!table)
1799 return -1;
a64448ba 1800
d62a17ae 1801 /* See if there is a configured label binding for this FEC. */
1802 fec = fec_find(table, &rn->p);
1803 if (!fec || fec->label == MPLS_INVALID_LABEL)
1804 return 0;
a64448ba 1805
d62a17ae 1806 /* Uninstall always removes all dynamic NHLFEs. */
1807 return lsp_uninstall(zvrf, fec->label);
a64448ba
DS
1808}
1809
5aba114a
DS
1810/*
1811 * Registration from a client for the label binding for a FEC. If a binding
1812 * already exists, it is informed to the client.
28d58fd7 1813 * NOTE: If there is a manually configured label binding, that is used.
9bedbb1e 1814 * Otherwise, if a label index is specified, it means we have to allocate the
28d58fd7
VV
1815 * label from a locally configured label block (SRGB), if one exists and index
1816 * is acceptable.
5aba114a 1817 */
d62a17ae 1818int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
d7c0a89a 1819 uint32_t label_index, struct zserv *client)
d62a17ae 1820{
1821 struct route_table *table;
1822 zebra_fec_t *fec;
1823 char buf[BUFSIZ];
1824 int new_client;
1825 int label_change = 0;
d7c0a89a 1826 uint32_t old_label;
d62a17ae 1827
1828 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
1829 if (!table)
1830 return -1;
1831
1832 if (IS_ZEBRA_DEBUG_MPLS)
1833 prefix2str(p, buf, BUFSIZ);
1834
1835 /* Locate FEC */
1836 fec = fec_find(table, p);
1837 if (!fec) {
1838 fec = fec_add(table, p, MPLS_INVALID_LABEL, 0, label_index);
1839 if (!fec) {
1840 prefix2str(p, buf, BUFSIZ);
af4c2728 1841 flog_err(
e914ccbe 1842 EC_ZEBRA_FEC_ADD_FAILED,
d62a17ae 1843 "Failed to add FEC %s upon register, client %s",
1844 buf, zebra_route_string(client->proto));
1845 return -1;
1846 }
1847
1848 old_label = MPLS_INVALID_LABEL;
1849 new_client = 1;
1850 } else {
1851 /* Client may register same FEC with different label index. */
1852 new_client =
1853 (listnode_lookup(fec->client_list, client) == NULL);
1854 if (!new_client && fec->label_index == label_index)
1855 /* Duplicate register */
1856 return 0;
1857
1858 /* Save current label, update label index */
1859 old_label = fec->label;
1860 fec->label_index = label_index;
1861 }
1862
1863 if (new_client)
1864 listnode_add(fec->client_list, client);
1865
1866 if (IS_ZEBRA_DEBUG_MPLS)
1867 zlog_debug("FEC %s Label Index %u %s by client %s", buf,
1868 label_index, new_client ? "registered" : "updated",
1869 zebra_route_string(client->proto));
1870
1871 /* If not a configured FEC, derive the local label (from label index)
1872 * or reset it.
1873 */
1874 if (!(fec->flags & FEC_FLAG_CONFIGURED)) {
1875 fec_derive_label_from_index(zvrf, fec);
1876
1877 /* If no label change, exit. */
1878 if (fec->label == old_label)
1879 return 0;
1880
1881 label_change = 1;
1882 }
1883
1884 /* If new client or label change, update client and install or uninstall
1885 * label forwarding entry as needed.
1886 */
1887 /* Inform client of label, if needed. */
1888 if ((new_client && fec->label != MPLS_INVALID_LABEL) || label_change) {
1889 if (IS_ZEBRA_DEBUG_MPLS)
1890 zlog_debug("Update client label %u", fec->label);
1891 fec_send(fec, client);
1892 }
1893
1894 if (new_client || label_change)
1895 return fec_change_update_lsp(zvrf, fec, old_label);
1896
1897 return 0;
5aba114a
DS
1898}
1899
1900/*
1901 * Deregistration from a client for the label binding for a FEC. The FEC
1902 * itself is deleted if no other registered clients exist and there is no
1903 * label bound to the FEC.
1904 */
d62a17ae 1905int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
1906 struct zserv *client)
5aba114a 1907{
d62a17ae 1908 struct route_table *table;
1909 zebra_fec_t *fec;
1910 char buf[BUFSIZ];
5aba114a 1911
d62a17ae 1912 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
1913 if (!table)
1914 return -1;
5aba114a 1915
d62a17ae 1916 if (IS_ZEBRA_DEBUG_MPLS)
1917 prefix2str(p, buf, BUFSIZ);
5aba114a 1918
d62a17ae 1919 fec = fec_find(table, p);
1920 if (!fec) {
1921 prefix2str(p, buf, BUFSIZ);
e914ccbe 1922 flog_err(EC_ZEBRA_FEC_RM_FAILED,
1c50c1c0
QY
1923 "Failed to find FEC %s upon unregister, client %s",
1924 buf, zebra_route_string(client->proto));
d62a17ae 1925 return -1;
1926 }
5aba114a 1927
d62a17ae 1928 listnode_delete(fec->client_list, client);
1929
1930 if (IS_ZEBRA_DEBUG_MPLS)
1931 zlog_debug("FEC %s unregistered by client %s", buf,
1932 zebra_route_string(client->proto));
1933
1934 /* If not a configured entry, delete the FEC if no other clients. Before
1935 * deleting, see if any LSP needs to be uninstalled.
1936 */
1937 if (!(fec->flags & FEC_FLAG_CONFIGURED)
1938 && list_isempty(fec->client_list)) {
1939 mpls_label_t old_label = fec->label;
1940 fec->label = MPLS_INVALID_LABEL; /* reset */
1941 fec_change_update_lsp(zvrf, fec, old_label);
1942 fec_del(fec);
1943 }
5aba114a 1944
d62a17ae 1945 return 0;
5aba114a
DS
1946}
1947
1948/*
1949 * Cleanup any FECs registered by this client.
1950 */
453844ab 1951static int zebra_mpls_cleanup_fecs_for_client(struct zserv *client)
d62a17ae 1952{
453844ab 1953 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
d62a17ae 1954 struct route_node *rn;
1955 zebra_fec_t *fec;
1956 struct listnode *node;
1957 struct zserv *fec_client;
1958 int af;
1959
1960 for (af = AFI_IP; af < AFI_MAX; af++) {
1961 if (zvrf->fec_table[af] == NULL)
1962 continue;
1963
1964 for (rn = route_top(zvrf->fec_table[af]); rn;
1965 rn = route_next(rn)) {
1966 fec = rn->info;
1967 if (!fec || list_isempty(fec->client_list))
1968 continue;
1969
1970 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node,
1971 fec_client)) {
1972 if (fec_client == client) {
1973 listnode_delete(fec->client_list,
1974 fec_client);
1975 if (!(fec->flags & FEC_FLAG_CONFIGURED)
1976 && list_isempty(fec->client_list))
1977 fec_del(fec);
1978 break;
1979 }
1980 }
1981 }
1982 }
5aba114a 1983
d62a17ae 1984 return 0;
5aba114a
DS
1985}
1986
f31e084c
DS
1987/*
1988 * Return FEC (if any) to which this label is bound.
1989 * Note: Only works for per-prefix binding and when the label is not
1990 * implicit-null.
1991 * TODO: Currently walks entire table, can optimize later with another
1992 * hash..
1993 */
d62a17ae 1994zebra_fec_t *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
1995 mpls_label_t label)
1996{
1997 struct route_node *rn;
1998 zebra_fec_t *fec;
1999 int af;
2000
2001 for (af = AFI_IP; af < AFI_MAX; af++) {
2002 if (zvrf->fec_table[af] == NULL)
2003 continue;
2004
2005 for (rn = route_top(zvrf->fec_table[af]); rn;
2006 rn = route_next(rn)) {
2007 if (!rn->info)
2008 continue;
2009 fec = rn->info;
2010 if (fec->label == label)
2011 return fec;
2012 }
2013 }
f31e084c 2014
d62a17ae 2015 return NULL;
f31e084c
DS
2016}
2017
2018/*
2019 * Inform if specified label is currently bound to a FEC or not.
2020 */
d62a17ae 2021int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label)
f31e084c 2022{
d62a17ae 2023 return (zebra_mpls_fec_for_label(zvrf, label) ? 1 : 0);
f31e084c
DS
2024}
2025
2026/*
5aba114a 2027 * Add static FEC to label binding. If there are clients registered for this
a64448ba
DS
2028 * FEC, notify them. If there are labeled routes for this FEC, install the
2029 * label forwarding entry.
9d303b37 2030*/
d62a17ae 2031int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
2032 mpls_label_t in_label)
2033{
2034 struct route_table *table;
2035 zebra_fec_t *fec;
2036 char buf[BUFSIZ];
2037 mpls_label_t old_label;
2038 int ret = 0;
2039
2040 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2041 if (!table)
2042 return -1;
2043
2044 if (IS_ZEBRA_DEBUG_MPLS)
2045 prefix2str(p, buf, BUFSIZ);
2046
2047 /* Update existing FEC or create a new one. */
2048 fec = fec_find(table, p);
2049 if (!fec) {
2050 fec = fec_add(table, p, in_label, FEC_FLAG_CONFIGURED,
2051 MPLS_INVALID_LABEL_INDEX);
2052 if (!fec) {
2053 prefix2str(p, buf, BUFSIZ);
e914ccbe 2054 flog_err(EC_ZEBRA_FEC_ADD_FAILED,
1c50c1c0 2055 "Failed to add FEC %s upon config", buf);
d62a17ae 2056 return -1;
2057 }
2058
2059 if (IS_ZEBRA_DEBUG_MPLS)
2060 zlog_debug("Add fec %s label %u", buf, in_label);
2061 } else {
2062 fec->flags |= FEC_FLAG_CONFIGURED;
2063 if (fec->label == in_label)
2064 /* Duplicate config */
2065 return 0;
2066
2067 /* Label change, update clients. */
2068 old_label = fec->label;
2069 if (IS_ZEBRA_DEBUG_MPLS)
2070 zlog_debug("Update fec %s new label %u", buf, in_label);
2071
2072 fec->label = in_label;
2073 fec_update_clients(fec);
2074
2075 /* Update label forwarding entries appropriately */
2076 ret = fec_change_update_lsp(zvrf, fec, old_label);
2077 }
2078
2079 return ret;
f31e084c
DS
2080}
2081
2082/*
5aba114a
DS
2083 * Remove static FEC to label binding. If there are no clients registered
2084 * for this FEC, delete the FEC; else notify clients
28d58fd7
VV
2085 * Note: Upon delete of static binding, if label index exists for this FEC,
2086 * client may need to be updated with derived label.
f31e084c 2087 */
d62a17ae 2088int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p)
2089{
2090 struct route_table *table;
2091 zebra_fec_t *fec;
2092 mpls_label_t old_label;
2093 char buf[BUFSIZ];
2094
2095 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2096 if (!table)
2097 return -1;
2098
2099 fec = fec_find(table, p);
2100 if (!fec) {
2101 prefix2str(p, buf, BUFSIZ);
e914ccbe 2102 flog_err(EC_ZEBRA_FEC_RM_FAILED,
1c50c1c0 2103 "Failed to find FEC %s upon delete", buf);
d62a17ae 2104 return -1;
2105 }
2106
2107 if (IS_ZEBRA_DEBUG_MPLS) {
2108 prefix2str(p, buf, BUFSIZ);
2109 zlog_debug("Delete fec %s label index %u", buf,
2110 fec->label_index);
2111 }
2112
2113 old_label = fec->label;
2114 fec->flags &= ~FEC_FLAG_CONFIGURED;
2115 fec->label = MPLS_INVALID_LABEL;
2116
2117 /* If no client exists, just delete the FEC. */
2118 if (list_isempty(fec->client_list)) {
2119 fec_del(fec);
2120 return 0;
2121 }
2122
2123 /* Derive the local label (from label index) or reset it. */
2124 fec_derive_label_from_index(zvrf, fec);
2125
2126 /* If there is a label change, update clients. */
2127 if (fec->label == old_label)
2128 return 0;
2129 fec_update_clients(fec);
2130
2131 /* Update label forwarding entries appropriately */
2132 return fec_change_update_lsp(zvrf, fec, old_label);
f31e084c
DS
2133}
2134
2135/*
2136 * Display MPLS FEC to label binding configuration (VTY command handler).
2137 */
d62a17ae 2138int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf)
f31e084c 2139{
d62a17ae 2140 struct route_node *rn;
2141 int af;
2142 zebra_fec_t *fec;
2143 char buf[BUFSIZ];
2144 int write = 0;
f31e084c 2145
d62a17ae 2146 for (af = AFI_IP; af < AFI_MAX; af++) {
2147 if (zvrf->fec_table[af] == NULL)
2148 continue;
f31e084c 2149
d62a17ae 2150 for (rn = route_top(zvrf->fec_table[af]); rn;
2151 rn = route_next(rn)) {
2152 if (!rn->info)
2153 continue;
f31e084c 2154
d62a17ae 2155 char lstr[BUFSIZ];
2156 fec = rn->info;
f31e084c 2157
d62a17ae 2158 if (!(fec->flags & FEC_FLAG_CONFIGURED))
2159 continue;
f31e084c 2160
d62a17ae 2161 write = 1;
2162 prefix2str(&rn->p, buf, BUFSIZ);
2163 vty_out(vty, "mpls label bind %s %s\n", buf,
2164 label2str(fec->label, lstr, BUFSIZ));
2165 }
2166 }
f31e084c 2167
d62a17ae 2168 return write;
f31e084c
DS
2169}
2170
2171/*
2172 * Display MPLS FEC to label binding (VTY command handler).
2173 */
d62a17ae 2174void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf)
f31e084c 2175{
d62a17ae 2176 struct route_node *rn;
2177 int af;
f31e084c 2178
d62a17ae 2179 for (af = AFI_IP; af < AFI_MAX; af++) {
2180 if (zvrf->fec_table[af] == NULL)
2181 continue;
f31e084c 2182
d62a17ae 2183 for (rn = route_top(zvrf->fec_table[af]); rn;
2184 rn = route_next(rn)) {
2185 if (!rn->info)
2186 continue;
2187 fec_print(rn->info, vty);
2188 }
2189 }
f31e084c
DS
2190}
2191
2192/*
2193 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2194 */
d62a17ae 2195void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
2196 struct prefix *p)
f31e084c 2197{
d62a17ae 2198 struct route_table *table;
2199 struct route_node *rn;
f31e084c 2200
d62a17ae 2201 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2202 if (!table)
2203 return;
f31e084c 2204
d62a17ae 2205 apply_mask(p);
2206 rn = route_node_lookup(table, p);
2207 if (!rn)
2208 return;
f31e084c 2209
d62a17ae 2210 route_unlock_node(rn);
2211 if (!rn->info)
2212 return;
f31e084c 2213
d62a17ae 2214 fec_print(rn->info, vty);
f31e084c
DS
2215}
2216
8f77d0ee
DS
2217static bool mpls_ftn_update_nexthop(int add, struct nexthop *nexthop,
2218 enum lsp_types_t type, mpls_label_t label)
2219{
2220 if (add && nexthop->nh_label_type == ZEBRA_LSP_NONE)
2221 nexthop_add_labels(nexthop, type, 1, &label);
2222 else if (!add && nexthop->nh_label_type == type)
2223 nexthop_del_labels(nexthop);
2224 else
2225 return false;
2226
2227 return true;
2228}
2229
ce549947
RW
2230/*
2231 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2232 */
d62a17ae 2233int mpls_ftn_update(int add, struct zebra_vrf *zvrf, enum lsp_types_t type,
2234 struct prefix *prefix, enum nexthop_types_t gtype,
d7c0a89a 2235 union g_addr *gate, ifindex_t ifindex, uint8_t distance,
d62a17ae 2236 mpls_label_t out_label)
2237{
2238 struct route_table *table;
2239 struct route_node *rn;
2240 struct route_entry *re;
2241 struct nexthop *nexthop;
8f77d0ee 2242 bool found;
d62a17ae 2243
2244 /* Lookup table. */
2245 table = zebra_vrf_table(family2afi(prefix->family), SAFI_UNICAST,
2246 zvrf_id(zvrf));
2247 if (!table)
2248 return -1;
2249
2250 /* Lookup existing route */
2251 rn = route_node_get(table, prefix);
a2addae8 2252 RNODE_FOREACH_RE (rn, re) {
d62a17ae 2253 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2254 continue;
2255 if (re->distance == distance)
2256 break;
88d88a9c 2257 }
ce549947 2258
d62a17ae 2259 if (re == NULL)
2260 return -1;
2261
8f77d0ee 2262 found = false;
7ee30f28 2263 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
d62a17ae 2264 switch (nexthop->type) {
2265 case NEXTHOP_TYPE_IPV4:
2266 case NEXTHOP_TYPE_IPV4_IFINDEX:
2267 if (gtype != NEXTHOP_TYPE_IPV4
2268 && gtype != NEXTHOP_TYPE_IPV4_IFINDEX)
2269 continue;
2270 if (!IPV4_ADDR_SAME(&nexthop->gate.ipv4, &gate->ipv4))
2271 continue;
2272 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
2273 && nexthop->ifindex != ifindex)
2274 continue;
8f77d0ee
DS
2275 if (!mpls_ftn_update_nexthop(add, nexthop, type,
2276 out_label))
2277 return 0;
2278 found = true;
2279 break;
d62a17ae 2280 case NEXTHOP_TYPE_IPV6:
2281 case NEXTHOP_TYPE_IPV6_IFINDEX:
2282 if (gtype != NEXTHOP_TYPE_IPV6
2283 && gtype != NEXTHOP_TYPE_IPV6_IFINDEX)
2284 continue;
2285 if (!IPV6_ADDR_SAME(&nexthop->gate.ipv6, &gate->ipv6))
2286 continue;
2287 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
2288 && nexthop->ifindex != ifindex)
2289 continue;
8f77d0ee
DS
2290 if (!mpls_ftn_update_nexthop(add, nexthop, type,
2291 out_label))
2292 return 0;
2293 found = true;
2294 break;
d62a17ae 2295 default:
2296 break;
2297 }
2298 }
d62a17ae 2299
8f77d0ee
DS
2300 if (!found)
2301 return -1;
ce549947 2302
d62a17ae 2303 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
332ad713 2304 SET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
d62a17ae 2305 rib_queue_add(rn);
ce549947 2306
d62a17ae 2307 return 0;
ce549947
RW
2308}
2309
2310/*
2311 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2312 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2313 * the out-label for an existing NHLFE (update case).
2314 */
d62a17ae 2315int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
2316 mpls_label_t in_label, mpls_label_t out_label,
2317 enum nexthop_types_t gtype, union g_addr *gate,
2318 ifindex_t ifindex)
2319{
2320 struct hash *lsp_table;
2321 zebra_ile_t tmp_ile;
2322 zebra_lsp_t *lsp;
2323 zebra_nhlfe_t *nhlfe;
2324 char buf[BUFSIZ];
2325
2326 /* Lookup table. */
2327 lsp_table = zvrf->lsp_table;
2328 if (!lsp_table)
2329 return -1;
2330
2331 /* If entry is present, exit. */
2332 tmp_ile.in_label = in_label;
2333 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
2334 if (!lsp)
2335 return -1;
2336 nhlfe = nhlfe_find(lsp, type, gtype, gate, ifindex);
2337 if (nhlfe) {
2338 struct nexthop *nh = nhlfe->nexthop;
2339
2340 assert(nh);
2341 assert(nh->nh_label);
2342
2343 /* Clear deleted flag (in case it was set) */
2344 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
2345 if (nh->nh_label->label[0] == out_label)
2346 /* No change */
2347 return 0;
2348
2349 if (IS_ZEBRA_DEBUG_MPLS) {
2350 nhlfe2str(nhlfe, buf, BUFSIZ);
2351 zlog_debug(
2352 "LSP in-label %u type %d nexthop %s "
2353 "out-label changed to %u (old %u)",
2354 in_label, type, buf, out_label,
2355 nh->nh_label->label[0]);
2356 }
2357
2358 /* Update out label, trigger processing. */
2359 nh->nh_label->label[0] = out_label;
2360 } else {
2361 /* Add LSP entry to this nexthop */
2362 nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex, out_label);
2363 if (!nhlfe)
2364 return -1;
2365
2366 if (IS_ZEBRA_DEBUG_MPLS) {
2367 nhlfe2str(nhlfe, buf, BUFSIZ);
2368 zlog_debug(
2369 "Add LSP in-label %u type %d nexthop %s "
2370 "out-label %u",
2371 in_label, type, buf, out_label);
2372 }
2373
2374 lsp->addr_family = NHLFE_FAMILY(nhlfe);
2375 }
2376
2377 /* Mark NHLFE, queue LSP for processing. */
2378 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
2379 if (lsp_processq_add(lsp))
2380 return -1;
2381
2382 return 0;
ce549947
RW
2383}
2384
2385/*
2386 * Uninstall a particular NHLFE in the forwarding table. If this is
2387 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2388 */
d62a17ae 2389int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
2390 mpls_label_t in_label, enum nexthop_types_t gtype,
2391 union g_addr *gate, ifindex_t ifindex)
2392{
2393 struct hash *lsp_table;
2394 zebra_ile_t tmp_ile;
2395 zebra_lsp_t *lsp;
2396 zebra_nhlfe_t *nhlfe;
2397 char buf[BUFSIZ];
2398
2399 /* Lookup table. */
2400 lsp_table = zvrf->lsp_table;
2401 if (!lsp_table)
2402 return -1;
2403
2404 /* If entry is not present, exit. */
2405 tmp_ile.in_label = in_label;
2406 lsp = hash_lookup(lsp_table, &tmp_ile);
2407 if (!lsp)
2408 return 0;
2409 nhlfe = nhlfe_find(lsp, type, gtype, gate, ifindex);
2410 if (!nhlfe)
2411 return 0;
2412
2413 if (IS_ZEBRA_DEBUG_MPLS) {
2414 nhlfe2str(nhlfe, buf, BUFSIZ);
2415 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2416 in_label, type, buf, nhlfe->flags);
2417 }
2418
2419 /* Mark NHLFE for delete or directly delete, as appropriate. */
2420 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
2421 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
2422 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
2423 if (lsp_processq_add(lsp))
2424 return -1;
2425 } else {
2426 nhlfe_del(nhlfe);
2427
2428 /* Free LSP entry if no other NHLFEs and not scheduled. */
2429 if (!lsp->nhlfe_list
2430 && !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED)) {
2431 if (IS_ZEBRA_DEBUG_MPLS)
2432 zlog_debug("Free LSP in-label %u flags 0x%x",
2433 lsp->ile.in_label, lsp->flags);
2434
2435 lsp = hash_release(lsp_table, &lsp->ile);
2436 if (lsp)
2437 XFREE(MTYPE_LSP, lsp);
2438 }
2439 }
2440 return 0;
ce549947
RW
2441}
2442
2443/*
2444 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2445 * If no other NHLFEs exist, the entry would be deleted.
2446 */
d62a17ae 2447void mpls_ldp_lsp_uninstall_all(struct hash_backet *backet, void *ctxt)
ce549947 2448{
d62a17ae 2449 zebra_lsp_t *lsp;
2450 struct hash *lsp_table;
ce549947 2451
d62a17ae 2452 lsp = (zebra_lsp_t *)backet->data;
9ea660be 2453 if (!lsp->nhlfe_list)
d62a17ae 2454 return;
ce549947 2455
d62a17ae 2456 lsp_table = ctxt;
2457 if (!lsp_table)
2458 return;
ce549947 2459
d62a17ae 2460 mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_LDP);
ce549947
RW
2461}
2462
2463/*
2464 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2465 */
d62a17ae 2466void mpls_ldp_ftn_uninstall_all(struct zebra_vrf *zvrf, int afi)
2467{
2468 struct route_table *table;
2469 struct route_node *rn;
2470 struct route_entry *re;
2471 struct nexthop *nexthop;
2472 int update;
2473
2474 /* Process routes of interested address-families. */
2475 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2476 if (!table)
2477 return;
2478
2479 for (rn = route_top(table); rn; rn = route_next(rn)) {
2480 update = 0;
a2addae8 2481 RNODE_FOREACH_RE (rn, re) {
7ee30f28 2482 for (nexthop = re->ng.nexthop; nexthop;
407c87a6
DS
2483 nexthop = nexthop->next) {
2484 if (nexthop->nh_label_type != ZEBRA_LSP_LDP)
2485 continue;
2486
d62a17ae 2487 nexthop_del_labels(nexthop);
2488 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2489 SET_FLAG(re->status,
332ad713 2490 ROUTE_ENTRY_LABELS_CHANGED);
d62a17ae 2491 update = 1;
2492 }
407c87a6 2493 }
d62a17ae 2494
2495 if (update)
2496 rib_queue_add(rn);
2497 }
ce549947
RW
2498}
2499
1c1cf002 2500#if defined(HAVE_CUMULUS)
7758e3f3 2501/*
2502 * Check that the label values used in LSP creation are consistent. The
2503 * main criteria is that if there is ECMP, the label operation must still
2504 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2505 * to current HW restrictions.
2506 */
d62a17ae 2507int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
2508 mpls_label_t in_label,
2509 mpls_label_t out_label,
2510 enum nexthop_types_t gtype,
2511 union g_addr *gate, ifindex_t ifindex)
2512{
2513 struct hash *slsp_table;
2514 zebra_ile_t tmp_ile;
2515 zebra_slsp_t *slsp;
2516 zebra_snhlfe_t *snhlfe;
2517
2518 /* Lookup table. */
2519 slsp_table = zvrf->slsp_table;
2520 if (!slsp_table)
2521 return 0;
2522
2523 /* If entry is not present, exit. */
2524 tmp_ile.in_label = in_label;
2525 slsp = hash_lookup(slsp_table, &tmp_ile);
2526 if (!slsp)
2527 return 1;
2528
2529 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2530 if (snhlfe) {
2531 if (snhlfe->out_label == out_label)
2532 return 1;
2533
2534 /* If not only NHLFE, cannot allow label change. */
2535 if (snhlfe != slsp->snhlfe_list || snhlfe->next)
2536 return 0;
2537 } else {
2538 /* If other NHLFEs exist, label operation must match. */
2539 if (slsp->snhlfe_list) {
2540 int cur_op, new_op;
2541
2542 cur_op = (slsp->snhlfe_list->out_label
70e98a7f
DS
2543 == MPLS_LABEL_IMPLICIT_NULL);
2544 new_op = (out_label == MPLS_LABEL_IMPLICIT_NULL);
d62a17ae 2545 if (cur_op != new_op)
2546 return 0;
2547 }
2548 }
2549
2550 /* Label values are good. */
2551 return 1;
7758e3f3 2552}
1c1cf002 2553#endif /* HAVE_CUMULUS */
7758e3f3 2554
2555/*
2556 * Add static LSP entry. This may be the first entry for this incoming label
2557 * or an additional nexthop; an existing entry may also have outgoing label
2558 * changed.
2559 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2560 * NHLFEs).
2561 */
d62a17ae 2562int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
2563 mpls_label_t out_label,
2564 enum nexthop_types_t gtype, union g_addr *gate,
2565 ifindex_t ifindex)
2566{
2567 struct hash *slsp_table;
2568 zebra_ile_t tmp_ile;
2569 zebra_slsp_t *slsp;
2570 zebra_snhlfe_t *snhlfe;
2571 char buf[BUFSIZ];
2572
2573 /* Lookup table. */
2574 slsp_table = zvrf->slsp_table;
2575 if (!slsp_table)
2576 return -1;
2577
2578 /* If entry is present, exit. */
2579 tmp_ile.in_label = in_label;
2580 slsp = hash_get(slsp_table, &tmp_ile, slsp_alloc);
2581 if (!slsp)
2582 return -1;
2583 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2584 if (snhlfe) {
2585 if (snhlfe->out_label == out_label)
2586 /* No change */
2587 return 0;
2588
2589 if (IS_ZEBRA_DEBUG_MPLS) {
2590 snhlfe2str(snhlfe, buf, BUFSIZ);
2591 zlog_debug(
2592 "Upd static LSP in-label %u nexthop %s "
2593 "out-label %u (old %u)",
2594 in_label, buf, out_label, snhlfe->out_label);
2595 }
2596 snhlfe->out_label = out_label;
2597 } else {
2598 /* Add static LSP entry to this nexthop */
2599 snhlfe = snhlfe_add(slsp, gtype, gate, ifindex, out_label);
2600 if (!snhlfe)
2601 return -1;
2602
2603 if (IS_ZEBRA_DEBUG_MPLS) {
2604 snhlfe2str(snhlfe, buf, BUFSIZ);
2605 zlog_debug(
2606 "Add static LSP in-label %u nexthop %s out-label %u",
2607 in_label, buf, out_label);
2608 }
2609 }
2610
2611 /* (Re)Install LSP in the main table. */
2612 if (mpls_lsp_install(zvrf, ZEBRA_LSP_STATIC, in_label, out_label, gtype,
2613 gate, ifindex))
2614 return -1;
2615
2616 return 0;
7758e3f3 2617}
2618
2619/*
2620 * Delete static LSP entry. This may be the delete of one particular
2621 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2622 * all NHLFEs).
2623 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2624 * LSP configuration.
2625 */
d62a17ae 2626int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
2627 enum nexthop_types_t gtype, union g_addr *gate,
2628 ifindex_t ifindex)
2629{
2630 struct hash *slsp_table;
2631 zebra_ile_t tmp_ile;
2632 zebra_slsp_t *slsp;
2633 zebra_snhlfe_t *snhlfe;
2634
2635 /* Lookup table. */
2636 slsp_table = zvrf->slsp_table;
2637 if (!slsp_table)
2638 return -1;
2639
2640 /* If entry is not present, exit. */
2641 tmp_ile.in_label = in_label;
2642 slsp = hash_lookup(slsp_table, &tmp_ile);
2643 if (!slsp)
2644 return 0;
2645
2646 /* Is it delete of entire LSP or a specific NHLFE? */
2647 if (gtype == NEXTHOP_TYPE_BLACKHOLE) {
2648 if (IS_ZEBRA_DEBUG_MPLS)
2649 zlog_debug("Del static LSP in-label %u", in_label);
2650
2651 /* Uninstall entire LSP from the main table. */
2652 mpls_static_lsp_uninstall_all(zvrf, in_label);
2653
2654 /* Delete all static NHLFEs */
2655 snhlfe_del_all(slsp);
2656 } else {
2657 /* Find specific NHLFE, exit if not found. */
2658 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2659 if (!snhlfe)
2660 return 0;
2661
2662 if (IS_ZEBRA_DEBUG_MPLS) {
2663 char buf[BUFSIZ];
2664 snhlfe2str(snhlfe, buf, BUFSIZ);
2665 zlog_debug("Del static LSP in-label %u nexthop %s",
2666 in_label, buf);
2667 }
2668
2669 /* Uninstall LSP from the main table. */
2670 mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
2671 gate, ifindex);
2672
2673 /* Delete static LSP NHLFE */
2674 snhlfe_del(snhlfe);
2675 }
2676
2677 /* Remove entire static LSP entry if no NHLFE - valid in either case
2678 * above. */
2679 if (!slsp->snhlfe_list) {
2680 slsp = hash_release(slsp_table, &tmp_ile);
2681 if (slsp)
2682 XFREE(MTYPE_SLSP, slsp);
2683 }
2684
2685 return 0;
7758e3f3 2686}
2687
40c7bdb0 2688/*
2689 * Schedule all MPLS label forwarding entries for processing.
2690 * Called upon changes that may affect one or more of them such as
2691 * interface or nexthop state changes.
2692 */
d62a17ae 2693void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf)
40c7bdb0 2694{
d62a17ae 2695 if (!zvrf)
2696 return;
2697 hash_iterate(zvrf->lsp_table, lsp_schedule, NULL);
40c7bdb0 2698}
2699
3ab18ff2 2700/*
2701 * Display MPLS label forwarding table for a specific LSP
2702 * (VTY command handler).
2703 */
d62a17ae 2704void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 2705 mpls_label_t label, bool use_json)
3ab18ff2 2706{
d62a17ae 2707 struct hash *lsp_table;
2708 zebra_lsp_t *lsp;
2709 zebra_ile_t tmp_ile;
2710 json_object *json = NULL;
3ab18ff2 2711
d62a17ae 2712 /* Lookup table. */
2713 lsp_table = zvrf->lsp_table;
2714 if (!lsp_table)
2715 return;
3ab18ff2 2716
d62a17ae 2717 /* If entry is not present, exit. */
2718 tmp_ile.in_label = label;
2719 lsp = hash_lookup(lsp_table, &tmp_ile);
2720 if (!lsp)
2721 return;
3ab18ff2 2722
d62a17ae 2723 if (use_json) {
2724 json = lsp_json(lsp);
9d303b37
DL
2725 vty_out(vty, "%s\n", json_object_to_json_string_ext(
2726 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 2727 json_object_free(json);
2728 } else
2729 lsp_print(lsp, (void *)vty);
3ab18ff2 2730}
2731
2732/*
2733 * Display MPLS label forwarding table (VTY command handler).
2734 */
d62a17ae 2735void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
9f049418 2736 bool use_json)
d62a17ae 2737{
2738 char buf[BUFSIZ];
2739 json_object *json = NULL;
2740 zebra_lsp_t *lsp = NULL;
2741 zebra_nhlfe_t *nhlfe = NULL;
2742 struct nexthop *nexthop = NULL;
2743 struct listnode *node = NULL;
2744 struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
2745
2746 if (use_json) {
2747 json = json_object_new_object();
2748
2749 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
2750 json_object_object_add(
2751 json, label2str(lsp->ile.in_label, buf, BUFSIZ),
2752 lsp_json(lsp));
2753
9d303b37
DL
2754 vty_out(vty, "%s\n", json_object_to_json_string_ext(
2755 json, JSON_C_TO_STRING_PRETTY));
d62a17ae 2756 json_object_free(json);
2757 } else {
2758 vty_out(vty, " Inbound Outbound\n");
2759 vty_out(vty, " Label Type Nexthop Label\n");
2760 vty_out(vty, "-------- ------- --------------- --------\n");
2761
2762 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
2763 for (nhlfe = lsp->nhlfe_list; nhlfe;
2764 nhlfe = nhlfe->next) {
2765 vty_out(vty, "%8d %7s ", lsp->ile.in_label,
2766 nhlfe_type2str(nhlfe->type));
2767 nexthop = nhlfe->nexthop;
2768
2769 switch (nexthop->type) {
996c9314 2770 case NEXTHOP_TYPE_IFINDEX: {
86f07f44 2771 struct zebra_ns *zns;
b9abd9ad
DS
2772 struct interface *ifp;
2773
86f07f44
PG
2774 zns = zebra_ns_lookup(NS_DEFAULT);
2775 ifp = if_lookup_by_index_per_ns(
2776 zns,
2777 nexthop->ifindex);
b9abd9ad
DS
2778 vty_out(vty, "%15s", ifp->name);
2779 break;
2780 }
d62a17ae 2781 case NEXTHOP_TYPE_IPV4:
2782 case NEXTHOP_TYPE_IPV4_IFINDEX:
2783 vty_out(vty, "%15s",
2784 inet_ntoa(nexthop->gate.ipv4));
2785 break;
2786 case NEXTHOP_TYPE_IPV6:
2787 case NEXTHOP_TYPE_IPV6_IFINDEX:
2788 vty_out(vty, "%15s",
2789 inet_ntop(AF_INET6,
2790 &nexthop->gate.ipv6,
2791 buf, BUFSIZ));
2792 break;
2793 default:
2794 break;
2795 }
2796
b9abd9ad 2797 if (nexthop->type != NEXTHOP_TYPE_IFINDEX)
fc5cca9f
DS
2798 vty_out(vty, " %8s\n",
2799 mpls_label2str(
2800 nexthop->nh_label
2801 ->num_labels,
2802 &nexthop->nh_label
2803 ->label[0],
2804 buf, BUFSIZ, 1));
b9abd9ad
DS
2805 else
2806 vty_out(vty, "\n");
d62a17ae 2807 }
2808 }
2809
2810 vty_out(vty, "\n");
2811 }
2812
6a154c88 2813 list_delete(&lsp_list);
3ab18ff2 2814}
2815
7758e3f3 2816/*
2817 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2818 */
d62a17ae 2819int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf)
2820{
2821 zebra_slsp_t *slsp;
2822 zebra_snhlfe_t *snhlfe;
2823 struct listnode *node;
2824 struct list *slsp_list =
2825 hash_get_sorted_list(zvrf->slsp_table, slsp_cmp);
2826
2827 for (ALL_LIST_ELEMENTS_RO(slsp_list, node, slsp)) {
2828 for (snhlfe = slsp->snhlfe_list; snhlfe;
2829 snhlfe = snhlfe->next) {
0af35d90 2830 char buf[BUFSIZ];
d62a17ae 2831 char lstr[30];
2832
0af35d90 2833 snhlfe2str(snhlfe, buf, sizeof(buf));
d62a17ae 2834 switch (snhlfe->out_label) {
70e98a7f
DS
2835 case MPLS_LABEL_IPV4_EXPLICIT_NULL:
2836 case MPLS_LABEL_IPV6_EXPLICIT_NULL:
d62a17ae 2837 strlcpy(lstr, "explicit-null", sizeof(lstr));
2838 break;
70e98a7f 2839 case MPLS_LABEL_IMPLICIT_NULL:
d62a17ae 2840 strlcpy(lstr, "implicit-null", sizeof(lstr));
2841 break;
2842 default:
2843 sprintf(lstr, "%u", snhlfe->out_label);
2844 break;
2845 }
2846
2847 vty_out(vty, "mpls lsp %u %s %s\n", slsp->ile.in_label,
2848 buf, lstr);
2849 }
2850 }
b78b820d 2851
6a154c88 2852 list_delete(&slsp_list);
d62a17ae 2853 return (zvrf->slsp_table->count ? 1 : 0);
7758e3f3 2854}
2855
1b6d5c7e
VV
2856/*
2857 * Add/update global label block.
2858 */
d7c0a89a
QY
2859int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, uint32_t start_label,
2860 uint32_t end_label)
1b6d5c7e 2861{
d62a17ae 2862 zvrf->mpls_srgb.start_label = start_label;
2863 zvrf->mpls_srgb.end_label = end_label;
28d58fd7 2864
d62a17ae 2865 /* Evaluate registered FECs to see if any get a label or not. */
2866 fec_evaluate(zvrf);
2867 return 0;
1b6d5c7e
VV
2868}
2869
2870/*
2871 * Delete global label block.
2872 */
d62a17ae 2873int zebra_mpls_label_block_del(struct zebra_vrf *zvrf)
1b6d5c7e 2874{
d62a17ae 2875 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
2876 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
28d58fd7 2877
d62a17ae 2878 /* Process registered FECs to clear their local label, if needed. */
2879 fec_evaluate(zvrf);
2880 return 0;
1b6d5c7e
VV
2881}
2882
2883/*
2884 * Display MPLS global label block configuration (VTY command handler).
2885 */
d62a17ae 2886int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *zvrf)
1b6d5c7e 2887{
d62a17ae 2888 if (zvrf->mpls_srgb.start_label == 0)
2889 return 0;
1b6d5c7e 2890
d62a17ae 2891 if ((zvrf->mpls_srgb.start_label != MPLS_DEFAULT_MIN_SRGB_LABEL)
2892 || (zvrf->mpls_srgb.end_label != MPLS_DEFAULT_MAX_SRGB_LABEL)) {
2893 vty_out(vty, "mpls label global-block %u %u\n",
2894 zvrf->mpls_srgb.start_label, zvrf->mpls_srgb.end_label);
2895 }
1b6d5c7e 2896
d62a17ae 2897 return 1;
1b6d5c7e
VV
2898}
2899
84915b0a 2900/*
2901 * Called when VRF becomes inactive, cleans up information but keeps
2902 * the table itself.
2903 * NOTE: Currently supported only for default VRF.
2904 */
2905void zebra_mpls_cleanup_tables(struct zebra_vrf *zvrf)
2906{
2907 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
2908}
2909
40c7bdb0 2910/*
2911 * Called upon process exiting, need to delete LSP forwarding
2912 * entries from the kernel.
2913 * NOTE: Currently supported only for default VRF.
2914 */
d62a17ae 2915void zebra_mpls_close_tables(struct zebra_vrf *zvrf)
40c7bdb0 2916{
d62a17ae 2917 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
2918 hash_clean(zvrf->lsp_table, NULL);
2919 hash_free(zvrf->lsp_table);
2920 hash_clean(zvrf->slsp_table, NULL);
2921 hash_free(zvrf->slsp_table);
9b67b514
DS
2922 route_table_finish(zvrf->fec_table[AFI_IP]);
2923 route_table_finish(zvrf->fec_table[AFI_IP6]);
40c7bdb0 2924}
2925
7758e3f3 2926/*
2927 * Allocate MPLS tables for this VRF and do other initialization.
2928 * NOTE: Currently supported only for default VRF.
2929 */
d62a17ae 2930void zebra_mpls_init_tables(struct zebra_vrf *zvrf)
7758e3f3 2931{
d62a17ae 2932 if (!zvrf)
2933 return;
996c9314
LB
2934 zvrf->slsp_table =
2935 hash_create(label_hash, label_cmp, "ZEBRA SLSP table");
2936 zvrf->lsp_table = hash_create(label_hash, label_cmp, "ZEBRA LSP table");
d62a17ae 2937 zvrf->fec_table[AFI_IP] = route_table_init();
2938 zvrf->fec_table[AFI_IP6] = route_table_init();
2939 zvrf->mpls_flags = 0;
2940 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
2941 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
7758e3f3 2942}
2943
2944/*
2945 * Global MPLS initialization.
2946 */
d62a17ae 2947void zebra_mpls_init(void)
7758e3f3 2948{
d62a17ae 2949 mpls_enabled = 0;
33c32282 2950
d62a17ae 2951 if (mpls_kernel_init() < 0) {
e914ccbe 2952 flog_warn(EC_ZEBRA_MPLS_SUPPORT_DISABLED,
9df414fe 2953 "Disabling MPLS support (no kernel support)");
d62a17ae 2954 return;
2955 }
fe6c7157 2956
d62a17ae 2957 if (!mpls_processq_init(&zebrad))
2958 mpls_enabled = 1;
453844ab 2959
21ccc0cf 2960 hook_register(zserv_client_close, zebra_mpls_cleanup_fecs_for_client);
7758e3f3 2961}