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