forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzebra_mpls.c
3992 lines (3357 loc) · 99.2 KB
/
zebra_mpls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Zebra MPLS code
* Copyright (C) 2013 Cumulus Networks, Inc.
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "command.h"
#include "if.h"
#include "log.h"
#include "sockunion.h"
#include "linklist.h"
#include "thread.h"
#include "workqueue.h"
#include "prefix.h"
#include "routemap.h"
#include "stream.h"
#include "nexthop.h"
#include "termtable.h"
#include "lib/json.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/interface.h"
#include "zebra/zserv.h"
#include "zebra/zebra_router.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_mpls.h"
#include "zebra/zebra_srte.h"
#include "zebra/zebra_errors.h"
DEFINE_MTYPE_STATIC(ZEBRA, LSP, "MPLS LSP object");
DEFINE_MTYPE_STATIC(ZEBRA, FEC, "MPLS FEC object");
DEFINE_MTYPE_STATIC(ZEBRA, NHLFE, "MPLS nexthop object");
int mpls_enabled;
/* static function declarations */
static void fec_evaluate(struct zebra_vrf *zvrf);
static uint32_t fec_derive_label_from_index(struct zebra_vrf *vrf,
zebra_fec_t *fec);
static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
struct route_node *rn, struct route_entry *re);
static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label);
static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
mpls_label_t old_label);
static int fec_send(zebra_fec_t *fec, struct zserv *client);
static void fec_update_clients(zebra_fec_t *fec);
static void fec_print(zebra_fec_t *fec, struct vty *vty);
static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p);
static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
mpls_label_t label, uint32_t flags,
uint32_t label_index);
static int fec_del(zebra_fec_t *fec);
static unsigned int label_hash(const void *p);
static bool label_cmp(const void *p1, const void *p2);
static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
struct nexthop *nexthop);
static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
struct nexthop *nexthop);
static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe);
static void lsp_select_best_nhlfe(zebra_lsp_t *lsp);
static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt);
static void lsp_schedule(struct hash_bucket *bucket, void *ctxt);
static wq_item_status lsp_process(struct work_queue *wq, void *data);
static void lsp_processq_del(struct work_queue *wq, void *data);
static void lsp_processq_complete(struct work_queue *wq);
static int lsp_processq_add(zebra_lsp_t *lsp);
static void *lsp_alloc(void *p);
/* Check whether lsp can be freed - no nhlfes, e.g., and call free api */
static void lsp_check_free(struct hash *lsp_table, zebra_lsp_t **plsp);
/* Free lsp; sets caller's pointer to NULL */
static void lsp_free(struct hash *lsp_table, zebra_lsp_t **plsp);
static char *nhlfe2str(const zebra_nhlfe_t *nhlfe, char *buf, int size);
static char *nhlfe_config_str(const zebra_nhlfe_t *nhlfe, char *buf, int size);
static int nhlfe_nhop_match(zebra_nhlfe_t *nhlfe, enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex);
static zebra_nhlfe_t *nhlfe_find(struct nhlfe_list_head *list,
enum lsp_types_t lsp_type,
enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex);
static zebra_nhlfe_t *nhlfe_add(zebra_lsp_t *lsp, enum lsp_types_t lsp_type,
enum nexthop_types_t gtype,
const union g_addr *gate, ifindex_t ifindex,
uint8_t num_labels, const mpls_label_t *labels,
bool is_backup);
static int nhlfe_del(zebra_nhlfe_t *nhlfe);
static void nhlfe_free(zebra_nhlfe_t *nhlfe);
static void nhlfe_out_label_update(zebra_nhlfe_t *nhlfe,
struct mpls_label_stack *nh_label);
static int mpls_lsp_uninstall_all(struct hash *lsp_table, zebra_lsp_t *lsp,
enum lsp_types_t type);
static int mpls_static_lsp_uninstall_all(struct zebra_vrf *zvrf,
mpls_label_t in_label);
static void nhlfe_print(zebra_nhlfe_t *nhlfe, struct vty *vty,
const char *indent);
static void lsp_print(struct vty *vty, zebra_lsp_t *lsp);
static void mpls_lsp_uninstall_all_type(struct hash_bucket *bucket, void *ctxt);
static void mpls_ftn_uninstall_all(struct zebra_vrf *zvrf,
int afi, enum lsp_types_t lsp_type);
static int lsp_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh);
static int lsp_backup_znh_install(zebra_lsp_t *lsp, enum lsp_types_t type,
const struct zapi_nexthop *znh);
/* Static functions */
/*
* Handle failure in LSP install, clear flags for NHLFE.
*/
static void clear_nhlfe_installed(zebra_lsp_t *lsp)
{
zebra_nhlfe_t *nhlfe;
struct nexthop *nexthop;
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
}
}
/*
* Install label forwarding entry based on labeled-route entry.
*/
static int lsp_install(struct zebra_vrf *zvrf, mpls_label_t label,
struct route_node *rn, struct route_entry *re)
{
struct hash *lsp_table;
zebra_ile_t tmp_ile;
zebra_lsp_t *lsp;
zebra_nhlfe_t *nhlfe;
struct nexthop *nexthop;
enum lsp_types_t lsp_type;
char buf[BUFSIZ];
int added, changed;
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
lsp_type = lsp_type_from_re_type(re->type);
added = changed = 0;
/* Locate or allocate LSP entry. */
tmp_ile.in_label = label;
lsp = hash_get(lsp_table, &tmp_ile, lsp_alloc);
if (!lsp)
return -1;
/* For each active nexthop, create NHLFE. Note that we deliberately skip
* recursive nexthops right now, because intermediate hops won't
* understand
* the label advertised by the recursive nexthop (plus we don't have the
* logic yet to push multiple labels).
*/
for (nexthop = re->nhe->nhg.nexthop;
nexthop; nexthop = nexthop->next) {
/* Skip inactive and recursive entries. */
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
continue;
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
continue;
nhlfe = nhlfe_find(&lsp->nhlfe_list, lsp_type,
nexthop->type, &nexthop->gate,
nexthop->ifindex);
if (nhlfe) {
/* Clear deleted flag (in case it was set) */
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
if (nexthop_labels_match(nhlfe->nexthop, nexthop))
/* No change */
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"LSP in-label %u type %d nexthop %s out-label changed",
lsp->ile.in_label, lsp_type, buf);
}
/* Update out label, trigger processing. */
nhlfe_out_label_update(nhlfe, nexthop->nh_label);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
changed++;
} else {
/* Add LSP entry to this nexthop */
nhlfe = nhlfe_add(lsp, lsp_type, nexthop->type,
&nexthop->gate, nexthop->ifindex,
nexthop->nh_label->num_labels,
nexthop->nh_label->label,
false /*backup*/);
if (!nhlfe)
return -1;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"Add LSP in-label %u type %d nexthop %s out-label %u",
lsp->ile.in_label, lsp_type, buf,
nexthop->nh_label->label[0]);
}
lsp->addr_family = NHLFE_FAMILY(nhlfe);
/* Mark NHLFE as changed. */
SET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
added++;
}
}
/* Queue LSP for processing if necessary. If no NHLFE got added (special
* case), delete the LSP entry; this case results in somewhat ugly
* logging.
*/
if (added || changed) {
if (lsp_processq_add(lsp))
return -1;
} else {
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
/*
* Uninstall all non-static NHLFEs of a label forwarding entry. If all
* NHLFEs are removed, the entire entry is deleted.
*/
static int lsp_uninstall(struct zebra_vrf *zvrf, mpls_label_t label)
{
struct hash *lsp_table;
zebra_ile_t tmp_ile;
zebra_lsp_t *lsp;
zebra_nhlfe_t *nhlfe;
char buf[BUFSIZ];
/* Lookup table. */
lsp_table = zvrf->lsp_table;
if (!lsp_table)
return -1;
/* If entry is not present, exit. */
tmp_ile.in_label = label;
lsp = hash_lookup(lsp_table, &tmp_ile);
if (!lsp || (nhlfe_list_first(&lsp->nhlfe_list) == NULL))
return 0;
/* Mark NHLFEs for delete or directly delete, as appropriate. */
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
/* Skip static NHLFEs */
if (nhlfe->type == ZEBRA_LSP_STATIC)
continue;
if (IS_ZEBRA_DEBUG_MPLS) {
nhlfe2str(nhlfe, buf, BUFSIZ);
zlog_debug(
"Del LSP in-label %u type %d nexthop %s flags 0x%x",
label, nhlfe->type, buf, nhlfe->flags);
}
if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED)) {
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED);
} else {
nhlfe_del(nhlfe);
}
}
/* Queue LSP for processing, if needed, else delete. */
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
if (lsp_processq_add(lsp))
return -1;
} else {
lsp_check_free(lsp_table, &lsp);
}
return 0;
}
/*
* This function is invoked upon change to label block configuration; it
* will walk all registered FECs with label-index and appropriately update
* their local labels and trigger client updates.
*/
static void fec_evaluate(struct zebra_vrf *zvrf)
{
struct route_node *rn;
zebra_fec_t *fec;
uint32_t old_label, new_label;
int af;
for (af = AFI_IP; af < AFI_MAX; af++) {
if (zvrf->fec_table[af] == NULL)
continue;
for (rn = route_top(zvrf->fec_table[af]); rn;
rn = route_next(rn)) {
if ((fec = rn->info) == NULL)
continue;
/* Skip configured FECs and those without a label index.
*/
if (fec->flags & FEC_FLAG_CONFIGURED
|| fec->label_index == MPLS_INVALID_LABEL_INDEX)
continue;
/* Save old label, determine new label. */
old_label = fec->label;
new_label =
zvrf->mpls_srgb.start_label + fec->label_index;
if (new_label >= zvrf->mpls_srgb.end_label)
new_label = MPLS_INVALID_LABEL;
/* If label has changed, update FEC and clients. */
if (new_label == old_label)
continue;
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug(
"Update fec %pRN new label %u upon label block",
rn, new_label);
fec->label = new_label;
fec_update_clients(fec);
/* Update label forwarding entries appropriately */
fec_change_update_lsp(zvrf, fec, old_label);
}
}
}
/*
* Derive (if possible) and update the local label for the FEC based on
* its label index. The index is "acceptable" if it falls within the
* globally configured label block (SRGB).
*/
static uint32_t fec_derive_label_from_index(struct zebra_vrf *zvrf,
zebra_fec_t *fec)
{
uint32_t label;
if (fec->label_index != MPLS_INVALID_LABEL_INDEX
&& zvrf->mpls_srgb.start_label
&& ((label = zvrf->mpls_srgb.start_label + fec->label_index)
< zvrf->mpls_srgb.end_label))
fec->label = label;
else
fec->label = MPLS_INVALID_LABEL;
return fec->label;
}
/*
* There is a change for this FEC. Install or uninstall label forwarding
* entries, as appropriate.
*/
static int fec_change_update_lsp(struct zebra_vrf *zvrf, zebra_fec_t *fec,
mpls_label_t old_label)
{
struct route_table *table;
struct route_node *rn;
struct route_entry *re;
afi_t afi;
/* Uninstall label forwarding entry, if previously installed. */
if (old_label != MPLS_INVALID_LABEL
&& old_label != MPLS_LABEL_IMPLICIT_NULL)
lsp_uninstall(zvrf, old_label);
/* Install label forwarding entry corr. to new label, if needed. */
if (fec->label == MPLS_INVALID_LABEL
|| fec->label == MPLS_LABEL_IMPLICIT_NULL)
return 0;
afi = family2afi(PREFIX_FAMILY(&fec->rn->p));
table = zebra_vrf_table(afi, SAFI_UNICAST, zvrf_id(zvrf));
if (!table)
return 0;
/* See if labeled route exists. */
rn = route_node_lookup(table, &fec->rn->p);
if (!rn)
return 0;
RNODE_FOREACH_RE (rn, re) {
if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
break;
}
if (!re || !zebra_rib_labeled_unicast(re))
return 0;
if (lsp_install(zvrf, fec->label, rn, re))
return -1;
return 0;
}
/*
* Inform about FEC to a registered client.
*/
static int fec_send(zebra_fec_t *fec, struct zserv *client)
{
struct stream *s;
struct route_node *rn;
rn = fec->rn;
/* Get output stream. */
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_FEC_UPDATE, VRF_DEFAULT);
stream_putw(s, rn->p.family);
stream_put_prefix(s, &rn->p);
stream_putl(s, fec->label);
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/*
* Update all registered clients about this FEC. Caller should've updated
* FEC and ensure no duplicate updates.
*/
static void fec_update_clients(zebra_fec_t *fec)
{
struct listnode *node;
struct zserv *client;
for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client)) {
if (IS_ZEBRA_DEBUG_MPLS)
zlog_debug("Update client %s",
zebra_route_string(client->proto));
fec_send(fec, client);
}
}
/*
* Print a FEC-label binding entry.
*/
static void fec_print(zebra_fec_t *fec, struct vty *vty)
{
struct route_node *rn;
struct listnode *node;
struct zserv *client;
char buf[BUFSIZ];
rn = fec->rn;
vty_out(vty, "%pRN\n", rn);
vty_out(vty, " Label: %s", label2str(fec->label, buf, BUFSIZ));
if (fec->label_index != MPLS_INVALID_LABEL_INDEX)
vty_out(vty, ", Label Index: %u", fec->label_index);
vty_out(vty, "\n");
if (!list_isempty(fec->client_list)) {
vty_out(vty, " Client list:");
for (ALL_LIST_ELEMENTS_RO(fec->client_list, node, client))
vty_out(vty, " %s(fd %d)",
zebra_route_string(client->proto),
client->sock);
vty_out(vty, "\n");
}
}
/*
* Locate FEC-label binding that matches with passed info.
*/
static zebra_fec_t *fec_find(struct route_table *table, struct prefix *p)
{
struct route_node *rn;
apply_mask(p);
rn = route_node_lookup(table, p);
if (!rn)
return NULL;
route_unlock_node(rn);
return (rn->info);
}
/*
* Add a FEC. This may be upon a client registering for a binding
* or when a binding is configured.
*/
static zebra_fec_t *fec_add(struct route_table *table, struct prefix *p,
mpls_label_t label, uint32_t flags,
uint32_t label_index)
{
struct route_node *rn;
zebra_fec_t *fec;
apply_mask(p);
/* Lookup (or add) route node.*/
rn = route_node_get(table, p);
if (!rn)
return NULL;
fec = rn->info;
if (!fec) {
fec = XCALLOC(MTYPE_FEC, sizeof(zebra_fec_t));
rn->info = fec;
fec->rn = rn;
fec->label = label;
fec->client_list = list_new();
} else
route_unlock_node(rn); /* for the route_node_get */
fec->label_index = label_index;
fec->flags = flags;
return fec;
}
/*
* Delete a FEC. This may be upon the last client deregistering for
* a FEC and no binding exists or when the binding is deleted and there
* are no registered clients.
*/
static int fec_del(zebra_fec_t *fec)
{
list_delete(&fec->client_list);
fec->rn->info = NULL;
route_unlock_node(fec->rn);
XFREE(MTYPE_FEC, fec);
return 0;
}
/*
* Hash function for label.
*/
static unsigned int label_hash(const void *p)
{
const zebra_ile_t *ile = p;
return (jhash_1word(ile->in_label, 0));
}
/*
* Compare 2 LSP hash entries based on in-label.
*/
static bool label_cmp(const void *p1, const void *p2)
{
const zebra_ile_t *ile1 = p1;
const zebra_ile_t *ile2 = p2;
return (ile1->in_label == ile2->in_label);
}
/*
* Check if an IPv4 nexthop for a NHLFE is active. Update nexthop based on
* the passed flag.
* NOTE: Looking only for connected routes right now.
*/
static int nhlfe_nexthop_active_ipv4(zebra_nhlfe_t *nhlfe,
struct nexthop *nexthop)
{
struct route_table *table;
struct prefix_ipv4 p;
struct route_node *rn;
struct route_entry *match;
struct nexthop *match_nh;
table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, nexthop->vrf_id);
if (!table)
return 0;
/* Lookup nexthop in IPv4 routing table. */
memset(&p, 0, sizeof(struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = nexthop->gate.ipv4;
rn = route_node_match(table, (struct prefix *)&p);
if (!rn)
return 0;
route_unlock_node(rn);
/* Locate a valid connected route. */
RNODE_FOREACH_RE (rn, match) {
if (CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
|| !CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
continue;
for (match_nh = match->nhe->nhg.nexthop; match_nh;
match_nh = match_nh->next) {
if (match->type == ZEBRA_ROUTE_CONNECT
|| nexthop->ifindex == match_nh->ifindex) {
nexthop->ifindex = match_nh->ifindex;
return 1;
}
}
}
return 0;
}
/*
* Check if an IPv6 nexthop for a NHLFE is active. Update nexthop based on
* the passed flag.
* NOTE: Looking only for connected routes right now.
*/
static int nhlfe_nexthop_active_ipv6(zebra_nhlfe_t *nhlfe,
struct nexthop *nexthop)
{
struct route_table *table;
struct prefix_ipv6 p;
struct route_node *rn;
struct route_entry *match;
table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, nexthop->vrf_id);
if (!table)
return 0;
/* Lookup nexthop in IPv6 routing table. */
memset(&p, 0, sizeof(struct prefix_ipv6));
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_PREFIXLEN;
p.prefix = nexthop->gate.ipv6;
rn = route_node_match(table, (struct prefix *)&p);
if (!rn)
return 0;
route_unlock_node(rn);
/* Locate a valid connected route. */
RNODE_FOREACH_RE (rn, match) {
if ((match->type == ZEBRA_ROUTE_CONNECT)
&& !CHECK_FLAG(match->status, ROUTE_ENTRY_REMOVED)
&& CHECK_FLAG(match->flags, ZEBRA_FLAG_SELECTED))
break;
}
if (!match || !match->nhe->nhg.nexthop)
return 0;
nexthop->ifindex = match->nhe->nhg.nexthop->ifindex;
return 1;
}
/*
* Check the nexthop reachability for a NHLFE and return if valid (reachable)
* or not.
* NOTE: Each NHLFE points to only 1 nexthop.
*/
static int nhlfe_nexthop_active(zebra_nhlfe_t *nhlfe)
{
struct nexthop *nexthop;
struct interface *ifp;
struct zebra_ns *zns;
nexthop = nhlfe->nexthop;
if (!nexthop) // unexpected
return 0;
/* Check on nexthop based on type. */
switch (nexthop->type) {
case NEXTHOP_TYPE_IFINDEX:
/*
* Lookup if this type is special. The
* NEXTHOP_TYPE_IFINDEX is a pop and
* forward into a different table for
* processing. As such this ifindex
* passed to us may be a VRF device
* which will not be in the default
* VRF. So let's look in all of them
*/
zns = zebra_ns_lookup(NS_DEFAULT);
ifp = if_lookup_by_index_per_ns(zns, nexthop->ifindex);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
if (nhlfe_nexthop_active_ipv4(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6:
if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
ifp = if_lookup_by_index(nexthop->ifindex,
nexthop->vrf_id);
if (ifp && if_is_operative(ifp))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
} else {
if (nhlfe_nexthop_active_ipv6(nhlfe, nexthop))
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
else
UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
break;
default:
break;
}
return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
}
/*
* Walk through NHLFEs for a LSP forwarding entry, verify nexthop
* reachability and select the best. Multipath entries are also
* marked. This is invoked when an LSP scheduled for processing (due
* to some change) is examined.
*/
static void lsp_select_best_nhlfe(zebra_lsp_t *lsp)
{
zebra_nhlfe_t *nhlfe;
zebra_nhlfe_t *best;
struct nexthop *nexthop;
int changed = 0;
if (!lsp)
return;
best = NULL;
lsp->num_ecmp = 0;
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
/*
* First compute the best path, after checking nexthop status. We are
* only concerned with non-deleted NHLFEs.
*/
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
/* Clear selection flags. */
UNSET_FLAG(nhlfe->flags,
(NHLFE_FLAG_SELECTED | NHLFE_FLAG_MULTIPATH));
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
&& nhlfe_nexthop_active(nhlfe)) {
if (!best || (nhlfe->distance < best->distance))
best = nhlfe;
}
}
lsp->best_nhlfe = best;
if (!lsp->best_nhlfe)
return;
/*
* Check the active status of backup nhlfes also
*/
frr_each_safe(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED))
(void)nhlfe_nexthop_active(nhlfe);
}
/* Mark best NHLFE as selected. */
SET_FLAG(lsp->best_nhlfe->flags, NHLFE_FLAG_SELECTED);
/*
* If best path exists, see if there is ECMP. While doing this, note if
* a
* new (uninstalled) NHLFE has been selected, an installed entry that is
* still selected has a change or an installed entry is to be removed.
*/
frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
int nh_chg, nh_sel, nh_inst;
nexthop = nhlfe->nexthop;
if (!nexthop) // unexpected
continue;
if (!CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_DELETED)
&& CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
&& (nhlfe->distance == lsp->best_nhlfe->distance)) {
SET_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
SET_FLAG(nhlfe->flags, NHLFE_FLAG_MULTIPATH);
lsp->num_ecmp++;
}
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED) && !changed) {
nh_chg = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
nh_sel = CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_SELECTED);
nh_inst =
CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED);
if ((nh_sel && !nh_inst)
|| (nh_sel && nh_inst && nh_chg)
|| (nh_inst && !nh_sel))
changed = 1;
}
/* We have finished examining, clear changed flag. */
UNSET_FLAG(nhlfe->flags, NHLFE_FLAG_CHANGED);
}
if (changed)
SET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
}
/*
* Delete LSP forwarding entry from kernel, if installed. Called upon
* process exit.
*/
static void lsp_uninstall_from_kernel(struct hash_bucket *bucket, void *ctxt)
{
zebra_lsp_t *lsp;
lsp = (zebra_lsp_t *)bucket->data;
if (CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED))
(void)dplane_lsp_delete(lsp);
}
/*
* Schedule LSP forwarding entry for processing. Called upon changes
* that may impact LSPs such as nexthop / connected route changes.
*/
static void lsp_schedule(struct hash_bucket *bucket, void *ctxt)
{
zebra_lsp_t *lsp;
lsp = (zebra_lsp_t *)bucket->data;
/* In the common flow, this is used when external events occur. For
* LSPs with backup nhlfes, we'll assume that the forwarding
* plane will use the backups to handle these events, until the
* owning protocol can react.
*/
if (ctxt == NULL) {
/* Skip LSPs with backups */
if (nhlfe_list_first(&lsp->backup_nhlfe_list) != NULL) {
if (IS_ZEBRA_DEBUG_MPLS_DETAIL)
zlog_debug("%s: skip LSP in-label %u",
__func__, lsp->ile.in_label);
return;
}
}
(void)lsp_processq_add(lsp);
}
/*
* Process a LSP entry that is in the queue. Recalculate best NHLFE and
* any multipaths and update or delete from the kernel, as needed.
*/
static wq_item_status lsp_process(struct work_queue *wq, void *data)
{
zebra_lsp_t *lsp;
zebra_nhlfe_t *oldbest, *newbest;
char buf[BUFSIZ], buf2[BUFSIZ];
struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
enum zebra_dplane_result res;
lsp = (zebra_lsp_t *)data;
if (!lsp) // unexpected
return WQ_SUCCESS;
oldbest = lsp->best_nhlfe;
/* Select best NHLFE(s) */
lsp_select_best_nhlfe(lsp);
newbest = lsp->best_nhlfe;
if (IS_ZEBRA_DEBUG_MPLS) {
if (oldbest)
nhlfe2str(oldbest, buf, sizeof(buf));
if (newbest)
nhlfe2str(newbest, buf2, sizeof(buf2));
zlog_debug(
"Process LSP in-label %u oldbest %s newbest %s flags 0x%x ecmp# %d",
lsp->ile.in_label, oldbest ? buf : "NULL",
newbest ? buf2 : "NULL", lsp->flags, lsp->num_ecmp);
}
if (!CHECK_FLAG(lsp->flags, LSP_FLAG_INSTALLED)) {
/* Not already installed */
if (newbest) {
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
switch (dplane_lsp_add(lsp)) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
/* Set 'installed' flag so we will know
* that an install is in-flight.
*/
SET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
zvrf->lsp_installs_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_warn(EC_ZEBRA_LSP_INSTALL_FAILURE,
"LSP Install Failure: %u",
lsp->ile.in_label);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->lsp_installs++;
break;
}
}
} else {
/* Installed, may need an update and/or delete. */
if (!newbest) {
res = dplane_lsp_delete(lsp);
/* We do some of the lsp cleanup immediately for
* deletes.
*/
UNSET_FLAG(lsp->flags, LSP_FLAG_INSTALLED);
clear_nhlfe_installed(lsp);
switch (res) {
case ZEBRA_DPLANE_REQUEST_QUEUED:
zvrf->lsp_removals_queued++;
break;
case ZEBRA_DPLANE_REQUEST_FAILURE:
flog_warn(EC_ZEBRA_LSP_DELETE_FAILURE,
"LSP Deletion Failure: %u",
lsp->ile.in_label);
break;
case ZEBRA_DPLANE_REQUEST_SUCCESS:
zvrf->lsp_removals++;
break;
}
} else if (CHECK_FLAG(lsp->flags, LSP_FLAG_CHANGED)) {
zebra_nhlfe_t *nhlfe;
struct nexthop *nexthop;
UNSET_FLAG(lsp->flags, LSP_FLAG_CHANGED);
/* We leave the INSTALLED flag set here
* so we know an update is in-flight.
*/
/*
* Any NHLFE that was installed but is not
* selected now needs to have its flags updated.
*/
frr_each_safe(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
nexthop = nhlfe->nexthop;
if (!nexthop)
continue;
if (CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_INSTALLED)
&& !CHECK_FLAG(nhlfe->flags,
NHLFE_FLAG_SELECTED)) {
UNSET_FLAG(nhlfe->flags,