]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_mpls.c
cb1c80923d666eb10032e16fc49059549d25fca6
[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 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",
1325 CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) ? " (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 *buf_ptr = buf;
1675 buf[0] = '\0';
1676
1677 if (pretty) {
1678 if (num_labels == 1) {
1679 label2str(labels[0], buf, len);
1680 } else if (num_labels == 2) {
1681 label2str(labels[0], buf, len);
1682 buf_ptr += strlen(buf);
1683
1684 snprintf(buf_ptr, len, "/");
1685 buf_ptr++;
1686
1687 label2str(labels[1], buf_ptr, len);
1688 }
1689 } else {
1690 if (num_labels == 1)
1691 snprintf(buf, len, "%u", labels[0]);
1692 else if (num_labels == 2)
1693 snprintf(buf, len, "%u/%u", labels[0], labels[1]);
1694 }
1695 return buf;
1696 }
1697
1698 /*
1699 * Install dynamic LSP entry.
1700 */
1701 int zebra_mpls_lsp_install(struct zebra_vrf *zvrf, struct route_node *rn,
1702 struct route_entry *re)
1703 {
1704 struct route_table *table;
1705 zebra_fec_t *fec;
1706
1707 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
1708 if (!table)
1709 return -1;
1710
1711 /* See if there is a configured label binding for this FEC. */
1712 fec = fec_find(table, &rn->p);
1713 if (!fec || fec->label == MPLS_INVALID_LABEL)
1714 return 0;
1715
1716 /* We cannot install a label forwarding entry if local label is the
1717 * implicit-null label.
1718 */
1719 if (fec->label == MPLS_IMP_NULL_LABEL)
1720 return 0;
1721
1722 if (lsp_install(zvrf, fec->label, rn, re))
1723 return -1;
1724
1725 return 0;
1726 }
1727
1728 /*
1729 * Uninstall dynamic LSP entry, if any.
1730 */
1731 int zebra_mpls_lsp_uninstall(struct zebra_vrf *zvrf, struct route_node *rn,
1732 struct route_entry *re)
1733 {
1734 struct route_table *table;
1735 zebra_fec_t *fec;
1736
1737 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(&rn->p))];
1738 if (!table)
1739 return -1;
1740
1741 /* See if there is a configured label binding for this FEC. */
1742 fec = fec_find(table, &rn->p);
1743 if (!fec || fec->label == MPLS_INVALID_LABEL)
1744 return 0;
1745
1746 /* Uninstall always removes all dynamic NHLFEs. */
1747 return lsp_uninstall(zvrf, fec->label);
1748 }
1749
1750 /*
1751 * Registration from a client for the label binding for a FEC. If a binding
1752 * already exists, it is informed to the client.
1753 * NOTE: If there is a manually configured label binding, that is used.
1754 * Otherwise, if a label index is specified, it means we have to allocate the
1755 * label from a locally configured label block (SRGB), if one exists and index
1756 * is acceptable.
1757 */
1758 int zebra_mpls_fec_register(struct zebra_vrf *zvrf, struct prefix *p,
1759 u_int32_t label_index, struct zserv *client)
1760 {
1761 struct route_table *table;
1762 zebra_fec_t *fec;
1763 char buf[BUFSIZ];
1764 int new_client;
1765 int label_change = 0;
1766 u_int32_t old_label;
1767
1768 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
1769 if (!table)
1770 return -1;
1771
1772 if (IS_ZEBRA_DEBUG_MPLS)
1773 prefix2str(p, buf, BUFSIZ);
1774
1775 /* Locate FEC */
1776 fec = fec_find(table, p);
1777 if (!fec) {
1778 fec = fec_add(table, p, MPLS_INVALID_LABEL, 0, label_index);
1779 if (!fec) {
1780 prefix2str(p, buf, BUFSIZ);
1781 zlog_err(
1782 "Failed to add FEC %s upon register, client %s",
1783 buf, zebra_route_string(client->proto));
1784 return -1;
1785 }
1786
1787 old_label = MPLS_INVALID_LABEL;
1788 new_client = 1;
1789 } else {
1790 /* Client may register same FEC with different label index. */
1791 new_client =
1792 (listnode_lookup(fec->client_list, client) == NULL);
1793 if (!new_client && fec->label_index == label_index)
1794 /* Duplicate register */
1795 return 0;
1796
1797 /* Save current label, update label index */
1798 old_label = fec->label;
1799 fec->label_index = label_index;
1800 }
1801
1802 if (new_client)
1803 listnode_add(fec->client_list, client);
1804
1805 if (IS_ZEBRA_DEBUG_MPLS)
1806 zlog_debug("FEC %s Label Index %u %s by client %s", buf,
1807 label_index, new_client ? "registered" : "updated",
1808 zebra_route_string(client->proto));
1809
1810 /* If not a configured FEC, derive the local label (from label index)
1811 * or reset it.
1812 */
1813 if (!(fec->flags & FEC_FLAG_CONFIGURED)) {
1814 fec_derive_label_from_index(zvrf, fec);
1815
1816 /* If no label change, exit. */
1817 if (fec->label == old_label)
1818 return 0;
1819
1820 label_change = 1;
1821 }
1822
1823 /* If new client or label change, update client and install or uninstall
1824 * label forwarding entry as needed.
1825 */
1826 /* Inform client of label, if needed. */
1827 if ((new_client && fec->label != MPLS_INVALID_LABEL) || label_change) {
1828 if (IS_ZEBRA_DEBUG_MPLS)
1829 zlog_debug("Update client label %u", fec->label);
1830 fec_send(fec, client);
1831 }
1832
1833 if (new_client || label_change)
1834 return fec_change_update_lsp(zvrf, fec, old_label);
1835
1836 return 0;
1837 }
1838
1839 /*
1840 * Deregistration from a client for the label binding for a FEC. The FEC
1841 * itself is deleted if no other registered clients exist and there is no
1842 * label bound to the FEC.
1843 */
1844 int zebra_mpls_fec_unregister(struct zebra_vrf *zvrf, struct prefix *p,
1845 struct zserv *client)
1846 {
1847 struct route_table *table;
1848 zebra_fec_t *fec;
1849 char buf[BUFSIZ];
1850
1851 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
1852 if (!table)
1853 return -1;
1854
1855 if (IS_ZEBRA_DEBUG_MPLS)
1856 prefix2str(p, buf, BUFSIZ);
1857
1858 fec = fec_find(table, p);
1859 if (!fec) {
1860 prefix2str(p, buf, BUFSIZ);
1861 zlog_err("Failed to find FEC %s upon unregister, client %s",
1862 buf, zebra_route_string(client->proto));
1863 return -1;
1864 }
1865
1866 listnode_delete(fec->client_list, client);
1867
1868 if (IS_ZEBRA_DEBUG_MPLS)
1869 zlog_debug("FEC %s unregistered by client %s", buf,
1870 zebra_route_string(client->proto));
1871
1872 /* If not a configured entry, delete the FEC if no other clients. Before
1873 * deleting, see if any LSP needs to be uninstalled.
1874 */
1875 if (!(fec->flags & FEC_FLAG_CONFIGURED)
1876 && list_isempty(fec->client_list)) {
1877 mpls_label_t old_label = fec->label;
1878 fec->label = MPLS_INVALID_LABEL; /* reset */
1879 fec_change_update_lsp(zvrf, fec, old_label);
1880 fec_del(fec);
1881 }
1882
1883 return 0;
1884 }
1885
1886 /*
1887 * Cleanup any FECs registered by this client.
1888 */
1889 int zebra_mpls_cleanup_fecs_for_client(struct zebra_vrf *zvrf,
1890 struct zserv *client)
1891 {
1892 struct route_node *rn;
1893 zebra_fec_t *fec;
1894 struct listnode *node;
1895 struct zserv *fec_client;
1896 int af;
1897
1898 for (af = AFI_IP; af < AFI_MAX; af++) {
1899 if (zvrf->fec_table[af] == NULL)
1900 continue;
1901
1902 for (rn = route_top(zvrf->fec_table[af]); rn;
1903 rn = route_next(rn)) {
1904 fec = rn->info;
1905 if (!fec || list_isempty(fec->client_list))
1906 continue;
1907
1908 for (ALL_LIST_ELEMENTS_RO(fec->client_list, node,
1909 fec_client)) {
1910 if (fec_client == client) {
1911 listnode_delete(fec->client_list,
1912 fec_client);
1913 if (!(fec->flags & FEC_FLAG_CONFIGURED)
1914 && list_isempty(fec->client_list))
1915 fec_del(fec);
1916 break;
1917 }
1918 }
1919 }
1920 }
1921
1922 return 0;
1923 }
1924
1925 /*
1926 * Return FEC (if any) to which this label is bound.
1927 * Note: Only works for per-prefix binding and when the label is not
1928 * implicit-null.
1929 * TODO: Currently walks entire table, can optimize later with another
1930 * hash..
1931 */
1932 zebra_fec_t *zebra_mpls_fec_for_label(struct zebra_vrf *zvrf,
1933 mpls_label_t label)
1934 {
1935 struct route_node *rn;
1936 zebra_fec_t *fec;
1937 int af;
1938
1939 for (af = AFI_IP; af < AFI_MAX; af++) {
1940 if (zvrf->fec_table[af] == NULL)
1941 continue;
1942
1943 for (rn = route_top(zvrf->fec_table[af]); rn;
1944 rn = route_next(rn)) {
1945 if (!rn->info)
1946 continue;
1947 fec = rn->info;
1948 if (fec->label == label)
1949 return fec;
1950 }
1951 }
1952
1953 return NULL;
1954 }
1955
1956 /*
1957 * Inform if specified label is currently bound to a FEC or not.
1958 */
1959 int zebra_mpls_label_already_bound(struct zebra_vrf *zvrf, mpls_label_t label)
1960 {
1961 return (zebra_mpls_fec_for_label(zvrf, label) ? 1 : 0);
1962 }
1963
1964 /*
1965 * Add static FEC to label binding. If there are clients registered for this
1966 * FEC, notify them. If there are labeled routes for this FEC, install the
1967 * label forwarding entry.
1968 */
1969 int zebra_mpls_static_fec_add(struct zebra_vrf *zvrf, struct prefix *p,
1970 mpls_label_t in_label)
1971 {
1972 struct route_table *table;
1973 zebra_fec_t *fec;
1974 char buf[BUFSIZ];
1975 mpls_label_t old_label;
1976 int ret = 0;
1977
1978 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
1979 if (!table)
1980 return -1;
1981
1982 if (IS_ZEBRA_DEBUG_MPLS)
1983 prefix2str(p, buf, BUFSIZ);
1984
1985 /* Update existing FEC or create a new one. */
1986 fec = fec_find(table, p);
1987 if (!fec) {
1988 fec = fec_add(table, p, in_label, FEC_FLAG_CONFIGURED,
1989 MPLS_INVALID_LABEL_INDEX);
1990 if (!fec) {
1991 prefix2str(p, buf, BUFSIZ);
1992 zlog_err("Failed to add FEC %s upon config", buf);
1993 return -1;
1994 }
1995
1996 if (IS_ZEBRA_DEBUG_MPLS)
1997 zlog_debug("Add fec %s label %u", buf, in_label);
1998 } else {
1999 fec->flags |= FEC_FLAG_CONFIGURED;
2000 if (fec->label == in_label)
2001 /* Duplicate config */
2002 return 0;
2003
2004 /* Label change, update clients. */
2005 old_label = fec->label;
2006 if (IS_ZEBRA_DEBUG_MPLS)
2007 zlog_debug("Update fec %s new label %u", buf, in_label);
2008
2009 fec->label = in_label;
2010 fec_update_clients(fec);
2011
2012 /* Update label forwarding entries appropriately */
2013 ret = fec_change_update_lsp(zvrf, fec, old_label);
2014 }
2015
2016 return ret;
2017 }
2018
2019 /*
2020 * Remove static FEC to label binding. If there are no clients registered
2021 * for this FEC, delete the FEC; else notify clients
2022 * Note: Upon delete of static binding, if label index exists for this FEC,
2023 * client may need to be updated with derived label.
2024 */
2025 int zebra_mpls_static_fec_del(struct zebra_vrf *zvrf, struct prefix *p)
2026 {
2027 struct route_table *table;
2028 zebra_fec_t *fec;
2029 mpls_label_t old_label;
2030 char buf[BUFSIZ];
2031
2032 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2033 if (!table)
2034 return -1;
2035
2036 fec = fec_find(table, p);
2037 if (!fec) {
2038 prefix2str(p, buf, BUFSIZ);
2039 zlog_err("Failed to find FEC %s upon delete", buf);
2040 return -1;
2041 }
2042
2043 if (IS_ZEBRA_DEBUG_MPLS) {
2044 prefix2str(p, buf, BUFSIZ);
2045 zlog_debug("Delete fec %s label index %u", buf,
2046 fec->label_index);
2047 }
2048
2049 old_label = fec->label;
2050 fec->flags &= ~FEC_FLAG_CONFIGURED;
2051 fec->label = MPLS_INVALID_LABEL;
2052
2053 /* If no client exists, just delete the FEC. */
2054 if (list_isempty(fec->client_list)) {
2055 fec_del(fec);
2056 return 0;
2057 }
2058
2059 /* Derive the local label (from label index) or reset it. */
2060 fec_derive_label_from_index(zvrf, fec);
2061
2062 /* If there is a label change, update clients. */
2063 if (fec->label == old_label)
2064 return 0;
2065 fec_update_clients(fec);
2066
2067 /* Update label forwarding entries appropriately */
2068 return fec_change_update_lsp(zvrf, fec, old_label);
2069 }
2070
2071 /*
2072 * Display MPLS FEC to label binding configuration (VTY command handler).
2073 */
2074 int zebra_mpls_write_fec_config(struct vty *vty, struct zebra_vrf *zvrf)
2075 {
2076 struct route_node *rn;
2077 int af;
2078 zebra_fec_t *fec;
2079 char buf[BUFSIZ];
2080 int write = 0;
2081
2082 for (af = AFI_IP; af < AFI_MAX; af++) {
2083 if (zvrf->fec_table[af] == NULL)
2084 continue;
2085
2086 for (rn = route_top(zvrf->fec_table[af]); rn;
2087 rn = route_next(rn)) {
2088 if (!rn->info)
2089 continue;
2090
2091 char lstr[BUFSIZ];
2092 fec = rn->info;
2093
2094 if (!(fec->flags & FEC_FLAG_CONFIGURED))
2095 continue;
2096
2097 write = 1;
2098 prefix2str(&rn->p, buf, BUFSIZ);
2099 vty_out(vty, "mpls label bind %s %s\n", buf,
2100 label2str(fec->label, lstr, BUFSIZ));
2101 }
2102 }
2103
2104 return write;
2105 }
2106
2107 /*
2108 * Display MPLS FEC to label binding (VTY command handler).
2109 */
2110 void zebra_mpls_print_fec_table(struct vty *vty, struct zebra_vrf *zvrf)
2111 {
2112 struct route_node *rn;
2113 int af;
2114
2115 for (af = AFI_IP; af < AFI_MAX; af++) {
2116 if (zvrf->fec_table[af] == NULL)
2117 continue;
2118
2119 for (rn = route_top(zvrf->fec_table[af]); rn;
2120 rn = route_next(rn)) {
2121 if (!rn->info)
2122 continue;
2123 fec_print(rn->info, vty);
2124 }
2125 }
2126 }
2127
2128 /*
2129 * Display MPLS FEC to label binding for a specific FEC (VTY command handler).
2130 */
2131 void zebra_mpls_print_fec(struct vty *vty, struct zebra_vrf *zvrf,
2132 struct prefix *p)
2133 {
2134 struct route_table *table;
2135 struct route_node *rn;
2136
2137 table = zvrf->fec_table[family2afi(PREFIX_FAMILY(p))];
2138 if (!table)
2139 return;
2140
2141 apply_mask(p);
2142 rn = route_node_lookup(table, p);
2143 if (!rn)
2144 return;
2145
2146 route_unlock_node(rn);
2147 if (!rn->info)
2148 return;
2149
2150 fec_print(rn->info, vty);
2151 }
2152
2153 /*
2154 * Install/uninstall a FEC-To-NHLFE (FTN) binding.
2155 */
2156 int mpls_ftn_update(int add, struct zebra_vrf *zvrf, enum lsp_types_t type,
2157 struct prefix *prefix, enum nexthop_types_t gtype,
2158 union g_addr *gate, ifindex_t ifindex, u_int8_t distance,
2159 mpls_label_t out_label)
2160 {
2161 struct route_table *table;
2162 struct route_node *rn;
2163 struct route_entry *re;
2164 struct nexthop *nexthop;
2165
2166 /* Lookup table. */
2167 table = zebra_vrf_table(family2afi(prefix->family), SAFI_UNICAST,
2168 zvrf_id(zvrf));
2169 if (!table)
2170 return -1;
2171
2172 /* Lookup existing route */
2173 rn = route_node_get(table, prefix);
2174 RNODE_FOREACH_RE(rn, re)
2175 {
2176 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2177 continue;
2178 if (re->distance == distance)
2179 break;
2180 }
2181
2182 if (re == NULL)
2183 return -1;
2184
2185 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next) {
2186 switch (nexthop->type) {
2187 case NEXTHOP_TYPE_IPV4:
2188 case NEXTHOP_TYPE_IPV4_IFINDEX:
2189 if (gtype != NEXTHOP_TYPE_IPV4
2190 && gtype != NEXTHOP_TYPE_IPV4_IFINDEX)
2191 continue;
2192 if (!IPV4_ADDR_SAME(&nexthop->gate.ipv4, &gate->ipv4))
2193 continue;
2194 if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX
2195 && nexthop->ifindex != ifindex)
2196 continue;
2197 goto found;
2198 case NEXTHOP_TYPE_IPV6:
2199 case NEXTHOP_TYPE_IPV6_IFINDEX:
2200 if (gtype != NEXTHOP_TYPE_IPV6
2201 && gtype != NEXTHOP_TYPE_IPV6_IFINDEX)
2202 continue;
2203 if (!IPV6_ADDR_SAME(&nexthop->gate.ipv6, &gate->ipv6))
2204 continue;
2205 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
2206 && nexthop->ifindex != ifindex)
2207 continue;
2208 goto found;
2209 default:
2210 break;
2211 }
2212 }
2213 /* nexthop not found */
2214 return -1;
2215
2216 found:
2217 if (add && nexthop->nh_label_type == ZEBRA_LSP_NONE)
2218 nexthop_add_labels(nexthop, type, 1, &out_label);
2219 else if (!add && nexthop->nh_label_type == type)
2220 nexthop_del_labels(nexthop);
2221 else
2222 return 0;
2223
2224 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2225 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
2226 rib_queue_add(rn);
2227
2228 return 0;
2229 }
2230
2231 /*
2232 * Install/update a NHLFE for an LSP in the forwarding table. This may be
2233 * a new LSP entry or a new NHLFE for an existing in-label or an update of
2234 * the out-label for an existing NHLFE (update case).
2235 */
2236 int mpls_lsp_install(struct zebra_vrf *zvrf, enum lsp_types_t type,
2237 mpls_label_t in_label, mpls_label_t out_label,
2238 enum nexthop_types_t gtype, union g_addr *gate,
2239 ifindex_t ifindex)
2240 {
2241 struct hash *lsp_table;
2242 zebra_ile_t tmp_ile;
2243 zebra_lsp_t *lsp;
2244 zebra_nhlfe_t *nhlfe;
2245 char buf[BUFSIZ];
2246
2247 /* Lookup table. */
2248 lsp_table = zvrf->lsp_table;
2249 if (!lsp_table)
2250 return -1;
2251
2252 /* If entry is present, exit. */
2253 tmp_ile.in_label = in_label;
2254 lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
2255 if (!lsp)
2256 return -1;
2257 nhlfe = nhlfe_find(lsp, type, gtype, gate, ifindex);
2258 if (nhlfe) {
2259 struct nexthop *nh = nhlfe->nexthop;
2260
2261 assert(nh);
2262 assert(nh->nh_label);
2263
2264 /* Clear deleted flag (in case it was set) */
2265 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
2266 if (nh->nh_label->label[0] == out_label)
2267 /* No change */
2268 return 0;
2269
2270 if (IS_ZEBRA_DEBUG_MPLS) {
2271 nhlfe2str(nhlfe, buf, BUFSIZ);
2272 zlog_debug(
2273 "LSP in-label %u type %d nexthop %s "
2274 "out-label changed to %u (old %u)",
2275 in_label, type, buf, out_label,
2276 nh->nh_label->label[0]);
2277 }
2278
2279 /* Update out label, trigger processing. */
2280 nh->nh_label->label[0] = out_label;
2281 } else {
2282 /* Add LSP entry to this nexthop */
2283 nhlfe = nhlfe_add(lsp, type, gtype, gate, ifindex, out_label);
2284 if (!nhlfe)
2285 return -1;
2286
2287 if (IS_ZEBRA_DEBUG_MPLS) {
2288 nhlfe2str(nhlfe, buf, BUFSIZ);
2289 zlog_debug(
2290 "Add LSP in-label %u type %d nexthop %s "
2291 "out-label %u",
2292 in_label, type, buf, out_label);
2293 }
2294
2295 lsp->addr_family = NHLFE_FAMILY(nhlfe);
2296 }
2297
2298 /* Mark NHLFE, queue LSP for processing. */
2299 SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
2300 if (lsp_processq_add(lsp))
2301 return -1;
2302
2303 return 0;
2304 }
2305
2306 /*
2307 * Uninstall a particular NHLFE in the forwarding table. If this is
2308 * the only NHLFE, the entire LSP forwarding entry has to be deleted.
2309 */
2310 int mpls_lsp_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
2311 mpls_label_t in_label, enum nexthop_types_t gtype,
2312 union g_addr *gate, ifindex_t ifindex)
2313 {
2314 struct hash *lsp_table;
2315 zebra_ile_t tmp_ile;
2316 zebra_lsp_t *lsp;
2317 zebra_nhlfe_t *nhlfe;
2318 char buf[BUFSIZ];
2319
2320 /* Lookup table. */
2321 lsp_table = zvrf->lsp_table;
2322 if (!lsp_table)
2323 return -1;
2324
2325 /* If entry is not present, exit. */
2326 tmp_ile.in_label = in_label;
2327 lsp = hash_lookup(lsp_table, &tmp_ile);
2328 if (!lsp)
2329 return 0;
2330 nhlfe = nhlfe_find(lsp, type, gtype, gate, ifindex);
2331 if (!nhlfe)
2332 return 0;
2333
2334 if (IS_ZEBRA_DEBUG_MPLS) {
2335 nhlfe2str(nhlfe, buf, BUFSIZ);
2336 zlog_debug("Del LSP in-label %u type %d nexthop %s flags 0x%x",
2337 in_label, type, buf, nhlfe->flags);
2338 }
2339
2340 /* Mark NHLFE for delete or directly delete, as appropriate. */
2341 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED)) {
2342 UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
2343 SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
2344 if (lsp_processq_add(lsp))
2345 return -1;
2346 } else {
2347 nhlfe_del(nhlfe);
2348
2349 /* Free LSP entry if no other NHLFEs and not scheduled. */
2350 if (!lsp->nhlfe_list
2351 && !CHECK_FLAG(lsp->flags, LSP_FLAG_SCHEDULED)) {
2352 if (IS_ZEBRA_DEBUG_MPLS)
2353 zlog_debug("Free LSP in-label %u flags 0x%x",
2354 lsp->ile.in_label, lsp->flags);
2355
2356 lsp = hash_release(lsp_table, &lsp->ile);
2357 if (lsp)
2358 XFREE(MTYPE_LSP, lsp);
2359 }
2360 }
2361 return 0;
2362 }
2363
2364 /*
2365 * Uninstall all LDP NHLFEs for a particular LSP forwarding entry.
2366 * If no other NHLFEs exist, the entry would be deleted.
2367 */
2368 void mpls_ldp_lsp_uninstall_all(struct hash_backet *backet, void *ctxt)
2369 {
2370 zebra_lsp_t *lsp;
2371 struct hash *lsp_table;
2372
2373 lsp = (zebra_lsp_t *)backet->data;
2374 if (!lsp || !lsp->nhlfe_list)
2375 return;
2376
2377 lsp_table = ctxt;
2378 if (!lsp_table)
2379 return;
2380
2381 mpls_lsp_uninstall_all(lsp_table, lsp, ZEBRA_LSP_LDP);
2382 }
2383
2384 /*
2385 * Uninstall all LDP FEC-To-NHLFE (FTN) bindings of the given address-family.
2386 */
2387 void mpls_ldp_ftn_uninstall_all(struct zebra_vrf *zvrf, int afi)
2388 {
2389 struct route_table *table;
2390 struct route_node *rn;
2391 struct route_entry *re;
2392 struct nexthop *nexthop;
2393 int update;
2394
2395 /* Process routes of interested address-families. */
2396 table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
2397 if (!table)
2398 return;
2399
2400 for (rn = route_top(table); rn; rn = route_next(rn)) {
2401 update = 0;
2402 RNODE_FOREACH_RE(rn, re)
2403 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
2404 if (nexthop->nh_label_type == ZEBRA_LSP_LDP) {
2405 nexthop_del_labels(nexthop);
2406 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2407 SET_FLAG(re->status,
2408 ROUTE_ENTRY_NEXTHOPS_CHANGED);
2409 update = 1;
2410 }
2411
2412 if (update)
2413 rib_queue_add(rn);
2414 }
2415 }
2416
2417 #if defined(HAVE_CUMULUS)
2418 /*
2419 * Check that the label values used in LSP creation are consistent. The
2420 * main criteria is that if there is ECMP, the label operation must still
2421 * be consistent - i.e., all paths either do a swap or do PHP. This is due
2422 * to current HW restrictions.
2423 */
2424 int zebra_mpls_lsp_label_consistent(struct zebra_vrf *zvrf,
2425 mpls_label_t in_label,
2426 mpls_label_t out_label,
2427 enum nexthop_types_t gtype,
2428 union g_addr *gate, ifindex_t ifindex)
2429 {
2430 struct hash *slsp_table;
2431 zebra_ile_t tmp_ile;
2432 zebra_slsp_t *slsp;
2433 zebra_snhlfe_t *snhlfe;
2434
2435 /* Lookup table. */
2436 slsp_table = zvrf->slsp_table;
2437 if (!slsp_table)
2438 return 0;
2439
2440 /* If entry is not present, exit. */
2441 tmp_ile.in_label = in_label;
2442 slsp = hash_lookup(slsp_table, &tmp_ile);
2443 if (!slsp)
2444 return 1;
2445
2446 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2447 if (snhlfe) {
2448 if (snhlfe->out_label == out_label)
2449 return 1;
2450
2451 /* If not only NHLFE, cannot allow label change. */
2452 if (snhlfe != slsp->snhlfe_list || snhlfe->next)
2453 return 0;
2454 } else {
2455 /* If other NHLFEs exist, label operation must match. */
2456 if (slsp->snhlfe_list) {
2457 int cur_op, new_op;
2458
2459 cur_op = (slsp->snhlfe_list->out_label
2460 == MPLS_IMP_NULL_LABEL);
2461 new_op = (out_label == MPLS_IMP_NULL_LABEL);
2462 if (cur_op != new_op)
2463 return 0;
2464 }
2465 }
2466
2467 /* Label values are good. */
2468 return 1;
2469 }
2470 #endif /* HAVE_CUMULUS */
2471
2472 /*
2473 * Add static LSP entry. This may be the first entry for this incoming label
2474 * or an additional nexthop; an existing entry may also have outgoing label
2475 * changed.
2476 * Note: The label operation (swap or PHP) is common for the LSP entry (all
2477 * NHLFEs).
2478 */
2479 int zebra_mpls_static_lsp_add(struct zebra_vrf *zvrf, mpls_label_t in_label,
2480 mpls_label_t out_label,
2481 enum nexthop_types_t gtype, union g_addr *gate,
2482 ifindex_t ifindex)
2483 {
2484 struct hash *slsp_table;
2485 zebra_ile_t tmp_ile;
2486 zebra_slsp_t *slsp;
2487 zebra_snhlfe_t *snhlfe;
2488 char buf[BUFSIZ];
2489
2490 /* Lookup table. */
2491 slsp_table = zvrf->slsp_table;
2492 if (!slsp_table)
2493 return -1;
2494
2495 /* If entry is present, exit. */
2496 tmp_ile.in_label = in_label;
2497 slsp = hash_get(slsp_table, &tmp_ile, slsp_alloc);
2498 if (!slsp)
2499 return -1;
2500 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2501 if (snhlfe) {
2502 if (snhlfe->out_label == out_label)
2503 /* No change */
2504 return 0;
2505
2506 if (IS_ZEBRA_DEBUG_MPLS) {
2507 snhlfe2str(snhlfe, buf, BUFSIZ);
2508 zlog_debug(
2509 "Upd static LSP in-label %u nexthop %s "
2510 "out-label %u (old %u)",
2511 in_label, buf, out_label, snhlfe->out_label);
2512 }
2513 snhlfe->out_label = out_label;
2514 } else {
2515 /* Add static LSP entry to this nexthop */
2516 snhlfe = snhlfe_add(slsp, gtype, gate, ifindex, out_label);
2517 if (!snhlfe)
2518 return -1;
2519
2520 if (IS_ZEBRA_DEBUG_MPLS) {
2521 snhlfe2str(snhlfe, buf, BUFSIZ);
2522 zlog_debug(
2523 "Add static LSP in-label %u nexthop %s out-label %u",
2524 in_label, buf, out_label);
2525 }
2526 }
2527
2528 /* (Re)Install LSP in the main table. */
2529 if (mpls_lsp_install(zvrf, ZEBRA_LSP_STATIC, in_label, out_label, gtype,
2530 gate, ifindex))
2531 return -1;
2532
2533 return 0;
2534 }
2535
2536 /*
2537 * Delete static LSP entry. This may be the delete of one particular
2538 * NHLFE for this incoming label or the delete of the entire entry (i.e.,
2539 * all NHLFEs).
2540 * NOTE: Delete of the only NHLFE will also end up deleting the entire
2541 * LSP configuration.
2542 */
2543 int zebra_mpls_static_lsp_del(struct zebra_vrf *zvrf, mpls_label_t in_label,
2544 enum nexthop_types_t gtype, union g_addr *gate,
2545 ifindex_t ifindex)
2546 {
2547 struct hash *slsp_table;
2548 zebra_ile_t tmp_ile;
2549 zebra_slsp_t *slsp;
2550 zebra_snhlfe_t *snhlfe;
2551
2552 /* Lookup table. */
2553 slsp_table = zvrf->slsp_table;
2554 if (!slsp_table)
2555 return -1;
2556
2557 /* If entry is not present, exit. */
2558 tmp_ile.in_label = in_label;
2559 slsp = hash_lookup(slsp_table, &tmp_ile);
2560 if (!slsp)
2561 return 0;
2562
2563 /* Is it delete of entire LSP or a specific NHLFE? */
2564 if (gtype == NEXTHOP_TYPE_BLACKHOLE) {
2565 if (IS_ZEBRA_DEBUG_MPLS)
2566 zlog_debug("Del static LSP in-label %u", in_label);
2567
2568 /* Uninstall entire LSP from the main table. */
2569 mpls_static_lsp_uninstall_all(zvrf, in_label);
2570
2571 /* Delete all static NHLFEs */
2572 snhlfe_del_all(slsp);
2573 } else {
2574 /* Find specific NHLFE, exit if not found. */
2575 snhlfe = snhlfe_find(slsp, gtype, gate, ifindex);
2576 if (!snhlfe)
2577 return 0;
2578
2579 if (IS_ZEBRA_DEBUG_MPLS) {
2580 char buf[BUFSIZ];
2581 snhlfe2str(snhlfe, buf, BUFSIZ);
2582 zlog_debug("Del static LSP in-label %u nexthop %s",
2583 in_label, buf);
2584 }
2585
2586 /* Uninstall LSP from the main table. */
2587 mpls_lsp_uninstall(zvrf, ZEBRA_LSP_STATIC, in_label, gtype,
2588 gate, ifindex);
2589
2590 /* Delete static LSP NHLFE */
2591 snhlfe_del(snhlfe);
2592 }
2593
2594 /* Remove entire static LSP entry if no NHLFE - valid in either case
2595 * above. */
2596 if (!slsp->snhlfe_list) {
2597 slsp = hash_release(slsp_table, &tmp_ile);
2598 if (slsp)
2599 XFREE(MTYPE_SLSP, slsp);
2600 }
2601
2602 return 0;
2603 }
2604
2605 /*
2606 * Schedule all MPLS label forwarding entries for processing.
2607 * Called upon changes that may affect one or more of them such as
2608 * interface or nexthop state changes.
2609 */
2610 void zebra_mpls_lsp_schedule(struct zebra_vrf *zvrf)
2611 {
2612 if (!zvrf)
2613 return;
2614 hash_iterate(zvrf->lsp_table, lsp_schedule, NULL);
2615 }
2616
2617 /*
2618 * Display MPLS label forwarding table for a specific LSP
2619 * (VTY command handler).
2620 */
2621 void zebra_mpls_print_lsp(struct vty *vty, struct zebra_vrf *zvrf,
2622 mpls_label_t label, u_char use_json)
2623 {
2624 struct hash *lsp_table;
2625 zebra_lsp_t *lsp;
2626 zebra_ile_t tmp_ile;
2627 json_object *json = NULL;
2628
2629 /* Lookup table. */
2630 lsp_table = zvrf->lsp_table;
2631 if (!lsp_table)
2632 return;
2633
2634 /* If entry is not present, exit. */
2635 tmp_ile.in_label = label;
2636 lsp = hash_lookup(lsp_table, &tmp_ile);
2637 if (!lsp)
2638 return;
2639
2640 if (use_json) {
2641 json = lsp_json(lsp);
2642 vty_out(vty, "%s\n",
2643 json_object_to_json_string_ext(
2644 json, JSON_C_TO_STRING_PRETTY));
2645 json_object_free(json);
2646 } else
2647 lsp_print(lsp, (void *)vty);
2648 }
2649
2650 /*
2651 * Display MPLS label forwarding table (VTY command handler).
2652 */
2653 void zebra_mpls_print_lsp_table(struct vty *vty, struct zebra_vrf *zvrf,
2654 u_char use_json)
2655 {
2656 char buf[BUFSIZ];
2657 json_object *json = NULL;
2658 zebra_lsp_t *lsp = NULL;
2659 zebra_nhlfe_t *nhlfe = NULL;
2660 struct nexthop *nexthop = NULL;
2661 struct listnode *node = NULL;
2662 struct list *lsp_list = hash_get_sorted_list(zvrf->lsp_table, lsp_cmp);
2663
2664 if (use_json) {
2665 json = json_object_new_object();
2666
2667 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
2668 json_object_object_add(
2669 json, label2str(lsp->ile.in_label, buf, BUFSIZ),
2670 lsp_json(lsp));
2671
2672 vty_out(vty, "%s\n",
2673 json_object_to_json_string_ext(
2674 json, JSON_C_TO_STRING_PRETTY));
2675 json_object_free(json);
2676 } else {
2677 vty_out(vty, " Inbound Outbound\n");
2678 vty_out(vty, " Label Type Nexthop Label\n");
2679 vty_out(vty, "-------- ------- --------------- --------\n");
2680
2681 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
2682 for (nhlfe = lsp->nhlfe_list; nhlfe;
2683 nhlfe = nhlfe->next) {
2684 vty_out(vty, "%8d %7s ", lsp->ile.in_label,
2685 nhlfe_type2str(nhlfe->type));
2686 nexthop = nhlfe->nexthop;
2687
2688 switch (nexthop->type) {
2689 case NEXTHOP_TYPE_IPV4:
2690 case NEXTHOP_TYPE_IPV4_IFINDEX:
2691 vty_out(vty, "%15s",
2692 inet_ntoa(nexthop->gate.ipv4));
2693 break;
2694 case NEXTHOP_TYPE_IPV6:
2695 case NEXTHOP_TYPE_IPV6_IFINDEX:
2696 vty_out(vty, "%15s",
2697 inet_ntop(AF_INET6,
2698 &nexthop->gate.ipv6,
2699 buf, BUFSIZ));
2700 break;
2701 default:
2702 break;
2703 }
2704
2705 vty_out(vty, " %8d\n",
2706 nexthop->nh_label->label[0]);
2707 }
2708 }
2709
2710 vty_out(vty, "\n");
2711 }
2712
2713 list_delete(lsp_list);
2714 }
2715
2716 /*
2717 * Display MPLS LSP configuration of all static LSPs (VTY command handler).
2718 */
2719 int zebra_mpls_write_lsp_config(struct vty *vty, struct zebra_vrf *zvrf)
2720 {
2721 zebra_slsp_t *slsp;
2722 zebra_snhlfe_t *snhlfe;
2723 struct listnode *node;
2724 struct list *slsp_list =
2725 hash_get_sorted_list(zvrf->slsp_table, slsp_cmp);
2726
2727 for (ALL_LIST_ELEMENTS_RO(slsp_list, node, slsp)) {
2728 for (snhlfe = slsp->snhlfe_list; snhlfe;
2729 snhlfe = snhlfe->next) {
2730 char buf[INET6_ADDRSTRLEN];
2731 char lstr[30];
2732
2733 snhlfe2str(snhlfe, buf, BUFSIZ);
2734 switch (snhlfe->out_label) {
2735 case MPLS_V4_EXP_NULL_LABEL:
2736 case MPLS_V6_EXP_NULL_LABEL:
2737 strlcpy(lstr, "explicit-null", sizeof(lstr));
2738 break;
2739 case MPLS_IMP_NULL_LABEL:
2740 strlcpy(lstr, "implicit-null", sizeof(lstr));
2741 break;
2742 default:
2743 sprintf(lstr, "%u", snhlfe->out_label);
2744 break;
2745 }
2746
2747 vty_out(vty, "mpls lsp %u %s %s\n", slsp->ile.in_label,
2748 buf, lstr);
2749 }
2750 }
2751
2752 list_delete(slsp_list);
2753 return (zvrf->slsp_table->count ? 1 : 0);
2754 }
2755
2756 /*
2757 * Add/update global label block.
2758 */
2759 int zebra_mpls_label_block_add(struct zebra_vrf *zvrf, u_int32_t start_label,
2760 u_int32_t end_label)
2761 {
2762 zvrf->mpls_srgb.start_label = start_label;
2763 zvrf->mpls_srgb.end_label = end_label;
2764
2765 /* Evaluate registered FECs to see if any get a label or not. */
2766 fec_evaluate(zvrf);
2767 return 0;
2768 }
2769
2770 /*
2771 * Delete global label block.
2772 */
2773 int zebra_mpls_label_block_del(struct zebra_vrf *zvrf)
2774 {
2775 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
2776 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
2777
2778 /* Process registered FECs to clear their local label, if needed. */
2779 fec_evaluate(zvrf);
2780 return 0;
2781 }
2782
2783 /*
2784 * Display MPLS global label block configuration (VTY command handler).
2785 */
2786 int zebra_mpls_write_label_block_config(struct vty *vty, struct zebra_vrf *zvrf)
2787 {
2788 if (zvrf->mpls_srgb.start_label == 0)
2789 return 0;
2790
2791 if ((zvrf->mpls_srgb.start_label != MPLS_DEFAULT_MIN_SRGB_LABEL)
2792 || (zvrf->mpls_srgb.end_label != MPLS_DEFAULT_MAX_SRGB_LABEL)) {
2793 vty_out(vty, "mpls label global-block %u %u\n",
2794 zvrf->mpls_srgb.start_label, zvrf->mpls_srgb.end_label);
2795 }
2796
2797 return 1;
2798 }
2799
2800 /*
2801 * Called upon process exiting, need to delete LSP forwarding
2802 * entries from the kernel.
2803 * NOTE: Currently supported only for default VRF.
2804 */
2805 void zebra_mpls_close_tables(struct zebra_vrf *zvrf)
2806 {
2807 hash_iterate(zvrf->lsp_table, lsp_uninstall_from_kernel, NULL);
2808 hash_clean(zvrf->lsp_table, NULL);
2809 hash_free(zvrf->lsp_table);
2810 hash_clean(zvrf->slsp_table, NULL);
2811 hash_free(zvrf->slsp_table);
2812 }
2813
2814 /*
2815 * Allocate MPLS tables for this VRF and do other initialization.
2816 * NOTE: Currently supported only for default VRF.
2817 */
2818 void zebra_mpls_init_tables(struct zebra_vrf *zvrf)
2819 {
2820 if (!zvrf)
2821 return;
2822 zvrf->slsp_table = hash_create(label_hash, label_cmp, NULL);
2823 zvrf->lsp_table = hash_create(label_hash, label_cmp, NULL);
2824 zvrf->fec_table[AFI_IP] = route_table_init();
2825 zvrf->fec_table[AFI_IP6] = route_table_init();
2826 zvrf->mpls_flags = 0;
2827 zvrf->mpls_srgb.start_label = MPLS_DEFAULT_MIN_SRGB_LABEL;
2828 zvrf->mpls_srgb.end_label = MPLS_DEFAULT_MAX_SRGB_LABEL;
2829 }
2830
2831 /*
2832 * Global MPLS initialization.
2833 */
2834 void zebra_mpls_init(void)
2835 {
2836 mpls_enabled = 0;
2837
2838 if (mpls_kernel_init() < 0) {
2839 zlog_warn("Disabling MPLS support (no kernel support)");
2840 return;
2841 }
2842
2843 if (!mpls_processq_init(&zebrad))
2844 mpls_enabled = 1;
2845 }