-
Notifications
You must be signed in to change notification settings - Fork 5
/
routes.c
1836 lines (1564 loc) · 80 KB
/
routes.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
/*
* =====================================================================================
*
* Filename: routes.c
*
* Description: This file implements the construction of routing table building
*
* Version: 1.0
* Created: Monday 04 September 2017 12:05:40 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* This file is part of the SPFComputation distribution (https://github.com/sachinites).
* Copyright (c) 2017 Abhishek Sagar.
* This program 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, version 3.
*
* This program 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. If not, see <http://www.gnu.org/licenses/>.
*
* =====================================================================================
*/
#include "spfutil.h"
#include "routes.h"
#include "bitsop.h"
#include "advert.h"
#include "spftrace.h"
#include "igp_sr_ext.h"
#include "sr_tlv_api.h"
#include "no_warn.h"
extern instance_t *instance;
extern int
instance_node_comparison_fn(void *_node, void *input_node_name);
static void
enhanced_start_route_installation(spf_info_t *spf_info,
LEVEL level, rtttype_t rtttype);
extern void
route_fetch_tilfa_backups(node_t *spf_root,
routes_t *route,
boolean inet3,
boolean mpls0);
static boolean
is_destination_has_multiple_primary_nxthops(spf_result_t *D_res){
if((is_internal_nh_t_empty(D_res->next_hop[IPNH][0]) &&
!is_internal_nh_t_empty(D_res->next_hop[LSPNH][0]) &&
is_internal_nh_t_empty(D_res->next_hop[LSPNH][1]))
||
(is_internal_nh_t_empty(D_res->next_hop[LSPNH][0]) &&
!is_internal_nh_t_empty(D_res->next_hop[IPNH][0]) &&
is_internal_nh_t_empty(D_res->next_hop[IPNH][1]))){
return FALSE;
}
return TRUE;
}
void
delete_route(spf_info_t *spf_info, routes_t *route,
boolean del_from_igp,
boolean del_from_rib){
singly_ll_node_t *list_node1 = NULL,
*list_node2 = NULL;
rt_key_t rt_key;
boolean is_found = FALSE;
rtttype_t rt_type = route->rt_type;
assert(del_from_igp || del_from_rib);
memset(&rt_key, 0, sizeof(rt_key_t));
strncpy((RT_ENTRY_PFX(&rt_key)), route->rt_key.u.prefix.prefix, PREFIX_LEN);
RT_ENTRY_MASK(&rt_key) = route->rt_key.u.prefix.mask;
if(del_from_igp){
ITERATE_LIST_BEGIN2(spf_info->routes_list[rt_type], list_node1, list_node2){
if(list_node1->data == route){
ITERATIVE_LIST_NODE_DELETE2(spf_info->routes_list[rt_type], list_node1, list_node2);
is_found = TRUE;
ITERATE_LIST_BREAK2(spf_info->routes_list[rt_type], list_node1, list_node2);
}
}ITERATE_LIST_END2(spf_info->routes_list[rt_type], list_node1, list_node2);
}
if(del_from_rib){
switch(rt_type){
case UNICAST_T:
{
rt_un_table_t *rib_inet_0 = spf_info->rib[INET_0];
rib_inet_0->rt_un_route_delete(rib_inet_0, &rt_key);
}
break;
case SPRING_T:
{
rt_un_table_t *rib_inet_3 = spf_info->rib[INET_3];
rt_un_table_t *rib_mpls_0 = spf_info->rib[MPLS_0];
rib_inet_3->rt_un_route_delete(rib_inet_3, &rt_key);
RT_ENTRY_LABEL(&rt_key) = route->rt_key.u.label;
rib_mpls_0->rt_un_route_delete(rib_mpls_0, &rt_key);
}
break;
default:
assert(0);
}
}
if(is_found){
free_route(route);
}
}
routes_t *
route_malloc(){
nh_type_t nh;
routes_t *route = XCALLOC(1, routes_t);
ITERATE_NH_TYPE_BEGIN(nh){
route->primary_nh_list[nh] = init_singly_ll();
singly_ll_set_comparison_fn(route->primary_nh_list[nh], instance_node_comparison_fn);
route->backup_nh_list[nh] = init_singly_ll();
singly_ll_set_comparison_fn(route->backup_nh_list[nh], instance_node_comparison_fn);
}ITERATE_NH_TYPE_END;
route->like_prefix_list = init_singly_ll();
singly_ll_set_comparison_fn(route->like_prefix_list, get_prefix_comparison_fn());
singly_ll_set_order_comparison_fn(route->like_prefix_list, get_prefix_order_comparison_fn());
return route;
}
static void
merge_route_primary_nexthops(routes_t *route, spf_result_t *result, nh_type_t nh){
unsigned int i = 0;
internal_nh_t *int_nxt_hop = NULL;
for( ; i < MAX_NXT_HOPS ; i++){
if(is_internal_nh_t_empty(result->next_hop[nh][i]))
break;
if(is_internal_nh_exist(route->primary_nh_list[nh], &result->next_hop[nh][i]))
continue;
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t(result->next_hop[nh][i], *int_nxt_hop);
singly_ll_add_node_by_val(route->primary_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u primary next hop is merged with %s's next hop node %s",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->next_hop[nh][i].node->node_name);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
}
assert(GET_NODE_COUNT_SINGLY_LL(route->primary_nh_list[nh]) <= MAX_NXT_HOPS);
}
static void
merge_route_backup_nexthops(routes_t *route,
spf_result_t *result,
nh_type_t nh){
unsigned int i = 0;
internal_nh_t *int_nxt_hop = NULL,
*backup = NULL;
boolean dont_collect_onlylink_protecting_backups =
is_destination_has_multiple_primary_nxthops(result);
for( i = 0; i < MAX_NXT_HOPS ; i++){
backup = &result->node->backup_next_hop[route->level][nh][i];
if(is_internal_nh_t_empty(*backup)) break;
if(is_internal_nh_exist(route->backup_nh_list[nh], backup))
continue;
if(dont_collect_onlylink_protecting_backups){
if(backup->lfa_type == LINK_PROTECTION_LFA ||
backup->lfa_type == LINK_PROTECTION_LFA_DOWNSTREAM ||
backup->lfa_type == LINK_PROTECTION_RLFA ||
backup->lfa_type == LINK_PROTECTION_RLFA_DOWNSTREAM ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_LFA ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_LFA_DOWNSTREAM ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_RLFA ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_RLFA_DOWNSTREAM){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "\t ECMP : only link-protecting backup dropped : %s----%s---->%-s(%s(%s)) protecting link: %s",
backup->oif->intf_name,
next_hop_type(*backup) == IPNH ? "IPNH" : "LSPNH",
next_hop_type(*backup) == IPNH ? next_hop_gateway_pfx(backup) : "",
backup->node ? backup->node->node_name : backup->rlfa->node_name,
backup->node ? backup->node->router_id : backup->rlfa->router_id,
backup->protected_link->intf_name);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
continue;
}
}
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t(result->node->backup_next_hop[route->level][nh][i], *int_nxt_hop);
singly_ll_add_node_by_val(route->backup_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u backup next hop is merged with %s's next hop node %s",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->node->backup_next_hop[route->level][nh][i].node->node_name);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
assert(GET_NODE_COUNT_SINGLY_LL(route->backup_nh_list[nh]) <= MAX_NXT_HOPS);
}
void
route_set_key(routes_t *route, char *ipv4_addr, char mask){
apply_mask(ipv4_addr, mask, route->rt_key.u.prefix.prefix);
route->rt_key.u.prefix.prefix[PREFIX_LEN] = '\0';
route->rt_key.u.prefix.mask = mask;
}
void
free_route(routes_t *route){
if(!route) return;
nh_type_t nh;
route->hosting_node = 0;
ITERATE_NH_TYPE_BEGIN(nh){
ROUTE_FLUSH_PRIMARY_NH_LIST(route, nh);
XFREE(route->primary_nh_list[nh]);
route->primary_nh_list[nh] = 0;
ROUTE_FLUSH_BACKUP_NH_LIST(route, nh);
XFREE(route->backup_nh_list[nh]);
route->backup_nh_list[nh] = 0;
} ITERATE_NH_TYPE_END;
if(route->rt_type == UNICAST_T){
delete_singly_ll(route->like_prefix_list);
}
XFREE(route->like_prefix_list);
route->like_prefix_list = NULL;
XFREE(route);
}
routes_t *
search_route_in_spf_route_list(spf_info_t *spf_info,
common_pfx_key_t *common_pfx,
rtttype_t rt_type){
routes_t *route = NULL;
singly_ll_node_t* list_node = NULL;
char prefix_with_mask[PREFIX_LEN + 1];
switch(rt_type){
case UNICAST_T:
apply_mask(common_pfx->u.prefix.prefix, common_pfx->u.prefix.mask, prefix_with_mask);
prefix_with_mask[PREFIX_LEN] = '\0';
ITERATE_LIST_BEGIN(spf_info->routes_list[rt_type], list_node){
route = list_node->data;
if(strncmp(route->rt_key.u.prefix.prefix, prefix_with_mask, PREFIX_LEN) == 0 &&
(route->rt_key.u.prefix.mask == common_pfx->u.prefix.mask))
return route;
}ITERATE_LIST_END;
break;
case SPRING_T:
ITERATE_LIST_BEGIN(spf_info->routes_list[rt_type], list_node){
route = list_node->data;
if(route->rt_key.u.label == common_pfx->u.label){
return route;
}
}ITERATE_LIST_END;
break;
default:
assert(0);
}
return NULL;
}
static unsigned int
delete_stale_routes(spf_info_t *spf_info, LEVEL level, rtttype_t rt_type){
singly_ll_node_t *list_node1 = NULL,
*list_node2 = NULL;
routes_t *route = NULL;
unsigned int i = 0;
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Deleting Stale Routes");
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
ITERATE_LIST_BEGIN2(spf_info->routes_list[rt_type], list_node1, list_node2){
route = list_node1->data;
if(route->level != level){
ITERATE_LIST_CONTINUE2(spf_info->routes_list[rt_type], list_node1, list_node2);
}
if(route->version != spf_info->spf_level_info[level].version){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u is STALE for Level%d, deleted", route->rt_key.u.prefix.prefix,
route->rt_key.u.prefix.mask, level); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
i++;
singly_ll_delete_node_by_data_ptr(spf_info->priority_routes_list[rt_type], route);
ITERATIVE_LIST_NODE_DELETE2(spf_info->priority_routes_list[rt_type], list_node1, list_node2);
free_route(route);
route = NULL;
}
}ITERATE_LIST_END2(spf_info->routes_list[rt_type], list_node1, list_node2);
return i;
}
/*Search internal route using longest prefix
* * match*/
routes_t *
search_route_in_spf_route_list_by_lpm(spf_info_t *spf_info,
char *prefix, rtttype_t rt_type){
routes_t *route = NULL,
*default_route = NULL,
*lpm_route = NULL;
char longest_mask = 0;
singly_ll_node_t* list_node = NULL;
ITERATE_LIST_BEGIN(spf_info->routes_list[rt_type], list_node){
route = list_node->data;
if(strncmp("0.0.0.0", route->rt_key.u.prefix.prefix, strlen("0.0.0.0")) == 0 &&
route->rt_key.u.prefix.mask == 0){
default_route = route;
}
else if(strncmp(prefix, route->rt_key.u.prefix.prefix, PREFIX_LEN) == 0){
if( route->rt_key.u.prefix.mask > longest_mask){
longest_mask = route->rt_key.u.prefix.mask;
lpm_route = route;
}
}
} ITERATE_LIST_END;
return lpm_route ? lpm_route : default_route;
}
static void
overwrite_route(spf_info_t *spf_info, routes_t *route,
prefix_t *prefix, spf_result_t *result, LEVEL level){
unsigned int i = 0;
nh_type_t nh = NH_MAX;
internal_nh_t *int_nxt_hop = NULL,
*backup = NULL;
/*Once we implement the proper route installation between IGP and RIB,
* we dont need to delete route from here anymore*/
if(route->level != level){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : IGP route %s/%u at %s will be transformed into %s route, hence deleting it from RIB",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix,
route->rt_key.u.prefix.mask, get_str_level(route->level), get_str_level(level));
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
delete_route(spf_info, route, FALSE, TRUE);
}
else{
delete_singly_ll(route->like_prefix_list);
}
//route_set_key(route, prefix->prefix, prefix->mask);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u being over written for %s", route->rt_key.u.prefix.prefix,
route->rt_key.u.prefix.mask, get_str_level(level));
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
route->version = spf_info->spf_level_info[level].version;
route->flags = prefix->prefix_flags;
route->rt_type = UNICAST_T;
route->level = level;
route->hosting_node = prefix->hosting_node;
if(!IS_BIT_SET(prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT)){
/* If the prefix metric type is internal*/
route->spf_metric = result->spf_metric + prefix->metric;
route->lsp_metric = result->lsp_metric + prefix->metric;;
}else{
/* If the metric type is external, we only compute the hosting node distance as spf metric*/
route->spf_metric = result->spf_metric;
route->lsp_metric = result->lsp_metric;
route->ext_metric = prefix->metric;
}
/*Check if Destination node has More than one primary next hops, then we need not copy
* only-link protecting backups*/
boolean dont_collect_onlylink_protecting_backups =
is_destination_has_multiple_primary_nxthops(result);
ITERATE_NH_TYPE_BEGIN(nh){
ROUTE_FLUSH_PRIMARY_NH_LIST(route, nh);
ROUTE_FLUSH_BACKUP_NH_LIST(route, nh);
for(i = 0 ; i < MAX_NXT_HOPS; i++){
if(!is_internal_nh_t_empty(result->next_hop[nh][i])){
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t(result->next_hop[nh][i], *int_nxt_hop);
ROUTE_ADD_NH(route->primary_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u primary next hop is merged with %s's next hop node %s",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->next_hop[nh][i].node->node_name); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else
break;
}
for(i = 0 ; i < MAX_NXT_HOPS; i++){
if(!is_internal_nh_t_empty((result->node->backup_next_hop[level][nh][i]))){
backup = &result->node->backup_next_hop[level][nh][i];
if(dont_collect_onlylink_protecting_backups){
if(backup->lfa_type == LINK_PROTECTION_LFA ||
backup->lfa_type == LINK_PROTECTION_LFA_DOWNSTREAM ||
backup->lfa_type == LINK_PROTECTION_RLFA ||
backup->lfa_type == LINK_PROTECTION_RLFA_DOWNSTREAM ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_LFA ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_LFA_DOWNSTREAM ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_RLFA ||
backup->lfa_type == BROADCAST_LINK_PROTECTION_RLFA_DOWNSTREAM){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "\t ECMP : only link-protecting backup dropped : %s----%s---->%-s(%s(%s)) protecting link: %s",
backup->oif->intf_name,
next_hop_type(*backup) == IPNH ? "IPNH" : "LSPNH",
next_hop_type(*backup) == IPNH ? next_hop_gateway_pfx(backup) : "",
backup->node ? backup->node->node_name : backup->rlfa->node_name,
backup->node ? backup->node->router_id : backup->rlfa->router_id,
backup->protected_link->intf_name);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
continue;
}
}
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t((result->node->backup_next_hop[level][nh][i]), *int_nxt_hop);
ROUTE_ADD_NH(route->backup_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u backup next hop is merged with %s's backup next hop node %s",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->node->backup_next_hop[level][nh][i].node->node_name); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else
break;
}
} ITERATE_NH_TYPE_END;
}
static void
link_prefix_to_route(routes_t *route, prefix_t *new_prefix,
unsigned int prefix_hosting_node_metric,
spf_info_t *spf_info){
singly_ll_node_t *list_node_prev = NULL,
*list_node_next = NULL,
*list_new_node = NULL;
prefix_t *list_prefix = NULL;
unsigned int new_prefix_metric = 0,
list_prefix_metric = 0;
spf_result_t *res = NULL;
prefix_pref_data_t list_prefix_pref = {ROUTE_UNKNOWN_PREFERENCE,
"ROUTE_UNKNOWN_PREFERENCE"},
new_prefix_pref = route_preference(new_prefix->prefix_flags, new_prefix->level);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "To Route : %s/%u, %s, Appending prefix : %s/%u to Route prefix list",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, get_str_level(route->level),
new_prefix->prefix, new_prefix->mask); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
if(is_singly_ll_empty(route->like_prefix_list)){
singly_ll_add_node_by_val(route->like_prefix_list, new_prefix);
return;
}
list_new_node = singly_ll_init_node(new_prefix);
ITERATE_LIST_BEGIN(route->like_prefix_list, list_node_next){
list_prefix = (prefix_t *)list_node_next->data;
list_prefix_pref = route_preference(list_prefix->prefix_flags, list_prefix->level);
if(new_prefix_pref.pref > list_prefix_pref.pref){
list_node_prev = list_node_next;
continue;
}
if(new_prefix_pref.pref < list_prefix_pref.pref){
list_new_node->next = list_node_next;
if(!list_node_prev)
route->like_prefix_list->head = list_new_node;
else
list_node_prev->next = list_new_node;
INC_NODE_COUNT_SINGLY_LL(route->like_prefix_list);
return;
}
/* Now tie based on metric*/
res = GET_SPF_RESULT(spf_info, list_prefix->hosting_node, list_prefix->level);
assert(res);
if(IS_BIT_SET(new_prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT))
new_prefix_metric = prefix_hosting_node_metric;
else
new_prefix_metric = prefix_hosting_node_metric + new_prefix->metric;
if(IS_BIT_SET(list_prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT))
list_prefix_metric = res->spf_metric;
else
list_prefix_metric = res->spf_metric + list_prefix->metric;
if(new_prefix->metric >= INFINITE_METRIC)
new_prefix_metric = INFINITE_METRIC;
/*Check with production code, comparison looks unfair to me*/
if(new_prefix_metric > list_prefix_metric){
list_node_prev = list_node_next;
continue;
}
if(new_prefix_metric < list_prefix_metric){
list_new_node->next = list_node_next;
if(!list_node_prev)
route->like_prefix_list->head = list_new_node;
else
list_node_prev->next = list_new_node;
INC_NODE_COUNT_SINGLY_LL(route->like_prefix_list);
return;
}
/* We are here because preference and metrics are same.
* now tie based on Router ID (We dont support this comparison, so
* simply insert the prefix here and exit)*/
list_new_node->next = list_node_next;
if(!list_node_prev)
route->like_prefix_list->head = list_new_node;
else
list_node_prev->next = list_new_node;
INC_NODE_COUNT_SINGLY_LL(route->like_prefix_list);
return;
}ITERATE_LIST_END;
/* Add at the end of list*/
list_new_node->next = list_node_next;
if(!list_node_prev)
route->like_prefix_list->head = list_new_node;
else
list_node_prev->next = list_new_node;
INC_NODE_COUNT_SINGLY_LL(route->like_prefix_list);
}
static void
update_route(spf_info_t *spf_info, /*spf_info of computing node*/
spf_result_t *result, /*result representing some network node*/
prefix_t *prefix, /*local prefix hosted on 'result' node*/
LEVEL level, rtttype_t rt_type,
boolean linkage){
routes_t *route = NULL;
unsigned int i = 0;
nh_type_t nh = NH_MAX;
internal_nh_t *int_nxt_hop = NULL;
common_pfx_key_t comm_pfx_key;
prefix_pref_data_t prefix_pref = {ROUTE_UNKNOWN_PREFERENCE, "ROUTE_UNKNOWN_PREFERENCE"},
route_pref = {ROUTE_UNKNOWN_PREFERENCE, "ROUTE_UNKNOWN_PREFERENCE"};
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : result node %s, topo = %s, prefix %s, level %s, prefix metric : %u",
GET_SPF_INFO_NODE(spf_info, level)->node_name, result->node->node_name, get_topology_name(rt_type),
prefix->prefix, get_str_level(level), prefix->metric); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
if(prefix->metric == INFINITE_METRIC){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "prefix : %s/%u discarded because of infinite metric",
prefix->prefix, prefix->mask); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
return;
}
init_prefix_key(&comm_pfx_key, prefix->prefix, prefix->mask);
route = search_route_in_spf_route_list(spf_info, &comm_pfx_key, rt_type);
if(!route){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "prefix : %s/%u is a New route (malloc'd) in %s, hosting_node %s",
prefix->prefix, prefix->mask, get_str_level(level), prefix->hosting_node->node_name);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
route = route_malloc();
route_set_key(route, prefix->prefix, prefix->mask);
route->version = spf_info->spf_level_info[level].version;
route->rt_type = UNICAST_T;
/*Copy the prefix flags to route flags. flags include :
* PREFIX_DOWNBIT_FLAG
* PREFIX_EXTERNABIT_FLAG
* PREFIX_METRIC_TYPE_EXT */
route->flags = prefix->prefix_flags;
route->level = level;
route->hosting_node = prefix->hosting_node;
/* Update route metric. Metric is to be filled depending on the prefix
* metric type is external or internal*/
if(!IS_BIT_SET(prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT)){
/* If the prefix metric type is internal*/
route->spf_metric = result->spf_metric + prefix->metric;
route->lsp_metric = result->lsp_metric + prefix->metric;
}else{
/* If the metric type is external, we only compute the hosting node distance as spf metric*/
route->spf_metric = result->spf_metric;
route->lsp_metric = result->lsp_metric;
route->ext_metric = prefix->metric;
}
ITERATE_NH_TYPE_BEGIN(nh){
for(i = 0; i < MAX_NXT_HOPS; i++){
if(!is_internal_nh_t_empty(result->next_hop[nh][i])){
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t(result->next_hop[nh][i], *int_nxt_hop);
ROUTE_ADD_NH(route->primary_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u Next hop added : %s|%s at %s",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask ,
result->next_hop[nh][i].node->node_name, nh == IPNH ? "IPNH":"LSPNH", get_str_level(level)); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else
break;
}
for(i = 0 ; i < MAX_NXT_HOPS; i++){
if(!is_internal_nh_t_empty((result->node->backup_next_hop[level][nh][i]))){
int_nxt_hop = XCALLOC(1, internal_nh_t);
copy_internal_nh_t((result->node->backup_next_hop[level][nh][i]), *int_nxt_hop);
ROUTE_ADD_NH(route->backup_nh_list[nh], int_nxt_hop);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "route : %s/%u backup next hop is copied with with %s's next hop node %s",
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->node->backup_next_hop[level][nh][i].node->node_name); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else
break;
}
}ITERATE_NH_TYPE_END;
/* route->backup_nh_list Not supported yet */
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
ROUTE_ADD_TO_ROUTE_LIST(spf_info, route, rt_type);
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u, spf_metric = %u, lsp_metric = %u, level = %u",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask,
route->spf_metric, route->lsp_metric, route->level); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else{
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u existing route. route verion : %u,"
"spf version : %u, route level : %s, spf level : %s",
GET_SPF_INFO_NODE(spf_info, level)->node_name, prefix->prefix, prefix->mask, route->version,
spf_info->spf_level_info[level].version, get_str_level(route->level), get_str_level(level)); trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
if((route->level == level && route->version == spf_info->spf_level_info[level].version)
|| (route->level != level)){
/* Over write based on preference now
Comparison Block Start*/
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u Trying over-writing route based on preference",
GET_SPF_INFO_NODE(spf_info, level)->node_name, prefix->prefix, prefix->mask);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
prefix_pref = route_preference(prefix->prefix_flags, prefix->level);
route_pref = route_preference(route->flags, route->level);
if(prefix_pref.pref == ROUTE_UNKNOWN_PREFERENCE){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : Prefix : %s/%u pref = %s, ignoring prefix", GET_SPF_INFO_NODE(spf_info, level)->node_name,
prefix->prefix, prefix->mask, prefix_pref.pref_str); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
return;
}
if(route_pref.pref < prefix_pref.pref){
/* if existing route is better*/
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u preference = %s, prefix pref = %s, Not overwritten",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask,
route_pref.pref_str, prefix_pref.pref_str); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
return;
}
/* If new prefix is better*/
else if(prefix_pref.pref < route_pref.pref){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u preference = %s, prefix pref = %s, will be overwritten",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask,
route_pref.pref_str, prefix_pref.pref_str); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
overwrite_route(spf_info, route, prefix, result, level);
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
return;
}
/* If prefixes are of same preference*/
else{
/* If route pref = prefix pref, then decide based on metric*/
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u preference = %s, prefix pref = %s, Same preference, Trying based on metric",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask,
route_pref.pref_str, prefix_pref.pref_str); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
/* If the prefix and route are of same pref, both will have internal metric Or both will have external metric*/
if(IS_BIT_SET(route->flags, PREFIX_METRIC_TYPE_EXT) &&
!IS_BIT_SET(prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT)){
assert(0);
}
if(!IS_BIT_SET(route->flags, PREFIX_METRIC_TYPE_EXT) &&
IS_BIT_SET(prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT)){
assert(0);
}
if(IS_BIT_SET(prefix->prefix_flags, PREFIX_METRIC_TYPE_EXT)){
/*Decide pref based on external metric*/
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u Deciding based on External metric",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
if(prefix->metric < route->ext_metric){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : prefix external metric ( = %u) is better than routes external metric( = %u), will overwrite",
GET_SPF_INFO_NODE(spf_info, level)->node_name, prefix->metric, route->ext_metric);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
overwrite_route(spf_info, route, prefix, result, level);
}
else if(prefix->metric > route->ext_metric){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : prefix external metric ( = %u) is no better than routes external metric( = %u), will not overwrite",
GET_SPF_INFO_NODE(spf_info, level)->node_name, prefix->metric, route->ext_metric);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
else{
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u hits ecmp case", GET_SPF_INFO_NODE(spf_info, level)->node_name,
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
/* Union LFA,s RLFA,s Primary nexthops*/
ITERATE_NH_TYPE_BEGIN(nh){
merge_route_primary_nexthops(route, result, nh);
merge_route_backup_nexthops(route, result, nh);
} ITERATE_NH_TYPE_END;
}
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
return;
}else{
/*Decide pref based on internal metric*/
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u Deciding based on Internal metric",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
if(result->spf_metric + prefix->metric < route->spf_metric){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u is over-written because better metric on node %s is found with metric = %u, old route metric = %u",
GET_SPF_INFO_NODE(spf_info, level)->node_name,
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->spf_metric + prefix->metric, route->spf_metric);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
overwrite_route(spf_info, route, prefix, result, level);
}
else if(result->spf_metric + prefix->metric == route->spf_metric){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u hits ecmp case", GET_SPF_INFO_NODE(spf_info, level)->node_name,
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask);
trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
/* Union LFA,s RLFA,s Primary nexthops*/
ITERATE_NH_TYPE_BEGIN(nh){
merge_route_primary_nexthops(route, result, nh);
merge_route_backup_nexthops(route, result, nh);
} ITERATE_NH_TYPE_END;
}
else{
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u is not over-written because no better metric on node %s is found with metric = %u, old route metric = %u",
GET_SPF_INFO_NODE(spf_info, level)->node_name,
route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, result->node->node_name,
result->spf_metric + prefix->metric, route->spf_metric); trace(instance->traceopts, ROUTE_CALCULATION_BIT);;
#endif
}
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
return;
}
}
/* Comparison Block Ends*/
}
else if(route->level == level && route->version != spf_info->spf_level_info[level].version){
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : route : %s/%u %s is mandatorily over-written because of version mismatch",
GET_SPF_INFO_NODE(spf_info, level)->node_name, route->rt_key.u.prefix.prefix, route->rt_key.u.prefix.mask, get_str_level(level));
trace(instance->traceopts, ROUTE_CALCULATION_BIT);
#endif
overwrite_route(spf_info, route, prefix, result, level);
/*Linkage*/
if(linkage){
link_prefix_to_route(route, prefix, result->spf_metric, spf_info);
}
return;
}
}
}
/* Efficient fn is introduced to decide if the route needs any backups at all. A route
* Does not need only-Link protecting backups if only it has more than 1 primary nexthops.
* We delete all only-link protecting backups at the stage when route was being built while
* iterating over ECMP destinations nodes. This is done in merge_route_backup_nexthops() fn.
* After the process of accumulating primary nexthops from all ECMP destinations node is complete, we need
* to analyse the nature of primary nexthops of the route. Here 'nature' means how primary nexthops
* are related to each other. We need to find out basically whether the route has independant
* set of primary nexthops. A set of nexthops of a route is said to be independant if and only if
* there exist atleast 1 nexthop in the set which could relay the traffic to atleast one ECMP destination
* of route without bypassing any other primary nexthop node in the set. The below function returns True
* if this condn is satified for the route, else false. If this fn return is TRUE, we can safely remove all
* backup nexthops of all types from the route's backup nexthop lists. This function is written for routes
* and will be used in phase2 to weed out the routes which do not need any backups. Function
* is_independant_primary_next_hop_list(node_t *node, LEVEL level) serves the same purpose but for Destination
* and in phase 1 of backup route calculation*/
/* This is very Expensive function, need optimization, will revisit . . . */
/*This fn should be used in phase 1 in backup route calculation*/
boolean
is_independant_primary_next_hop_list_for_nodes(node_t *S, node_t *dst_node, LEVEL level){
internal_nh_t *prim_next_hop1 = NULL,
*prim_next_hop2 = NULL;
nh_type_t nh;
unsigned int dist_prim_nh1_to_D = 0,
dist_prim_nh2_to_D = 0,
dist_prim_nh1_to_prim_nh2 = 0,
i = 0, j = 0,
indep_pr_nh_count = 0;
boolean check_next_outer_nh;
/*This fn assumes that all requires SPF runs has been triggered*/
//Compute_PHYSICAL_Neighbor_SPFs(S, level);
spf_result_t *D_res = GET_SPF_RESULT((&S->spf_info), dst_node, level);
D_res->backup_requirement[level] = BACKUPS_REQUIRED;
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : Testing for Independant primary nexthops at %s for Dest %s",
S->node_name, get_str_level(level), dst_node->node_name);
trace(instance->traceopts, BACKUP_COMPUTATION_BIT);
#endif
check_next_outer_nh = FALSE;
ITERATE_NH_TYPE_BEGIN(nh){
for(i = 0; i < MAX_NXT_HOPS; i++){
prim_next_hop1 = &D_res->next_hop[nh][i];
if(is_nh_list_empty2(prim_next_hop1))
break;
dist_prim_nh1_to_D = DIST_X_Y(prim_next_hop1->node, dst_node, level);
for(j = 0; j < MAX_NXT_HOPS; j++){
prim_next_hop2 = &D_res->next_hop[nh][j];
if(is_nh_list_empty2(prim_next_hop2))
break;
if(prim_next_hop1 == prim_next_hop2) continue;
dist_prim_nh2_to_D = DIST_X_Y(prim_next_hop2->node, dst_node, level);
dist_prim_nh1_to_prim_nh2 = DIST_X_Y(prim_next_hop1->node, prim_next_hop2->node, level);
if(dist_prim_nh1_to_D < dist_prim_nh1_to_prim_nh2 + dist_prim_nh2_to_D)
continue;
else{
check_next_outer_nh = TRUE;
break;
}
}
if(check_next_outer_nh){
check_next_outer_nh = FALSE;
continue;
}
else{
indep_pr_nh_count++;
if(indep_pr_nh_count == 2)
break;
}
}
} ITERATE_NH_TYPE_END;
if(indep_pr_nh_count == 2){
D_res->backup_requirement[level] = NO_BACKUP_REQUIRED;
#ifdef __ENABLE_TRACE__
sprintf(instance->traceopts->b, "Node : %s : Dest %s has independent Primary nexthops at %s",
S->node_name, dst_node->node_name, get_str_level(level));
trace(instance->traceopts, BACKUP_COMPUTATION_BIT);
#endif
return TRUE;
}
return FALSE;
}
/*This fn should be used in phase 2 in backup route calculation*/
boolean
is_independant_primary_next_hop_list(routes_t *route){
singly_ll_node_t *list_node1 = NULL,
*list_node2 = NULL,
*list_node3 = NULL;
prefix_t *prefix = NULL;
node_t *ecmp_dest_node = NULL,
*primary_nh1 = NULL,
*primary_nh2 = NULL;
internal_nh_t *next_hop = NULL;
nh_type_t nh;
LEVEL level = route->level;
unsigned int dist_prim_nh1_to_D = 0,
dist_prim_nh2_to_D = 0,
dist_prim_nh1_to_prim_nh2 = 0,
indep_prim_nh_count = 0;
boolean try_next_dest = FALSE;