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