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