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