summaryrefslogtreecommitdiffstats
path: root/src/vnet/fib/fib_path_list.c
blob: 5b35e9b87e71b44610055d8c9dd618dceeb3a2f8 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/mhash.h>
#include <vnet/ip/ip.h>
#include <vnet/adj/adj.h>
#include <vnet/dpo/load_balance.h>
#include <vnet/dpo/load_balance_map.h>

#include <vnet/fib/fib_path_list.h>
#include <vnet/fib/fib_internal.h>
#include <vnet/fib/fib_node_list.h>
#include <vnet/fib/fib_walk.h>
#include <vnet/fib/fib_urpf_list.h>

/**
 * FIB path-list
 * A representation of the list/set of path trough which a prefix is reachable
 */
typedef struct fib_path_list_t_ {
    /**
     * A path-list is a node in the FIB graph.
     */
    fib_node_t fpl_node;

    /**
     * Flags on the path-list
     */
    fib_path_list_flags_t fpl_flags;

    /**
     * The next-hop protocol for the paths in this path list.
     * Note that fixing the proto here means we don't support a mix of
     * v4 and v6 paths. ho hum.
     */
    fib_protocol_t fpl_nh_proto;

    /**
     * Vector of paths indicies for all configured paths.
     * For shareable path-lists this list MUST not change.
     */
    fib_node_index_t *fpl_paths;

    /**
     * the RPF list calculated for this path list
     */
    fib_node_index_t fpl_urpf;
} fib_path_list_t;

/*
 * Array of strings/names for the FIB sources
 */
static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;

/*
 * The memory pool from which we allocate all the path-lists
 */
static fib_path_list_t * fib_path_list_pool;

/*
 * The data-base of shared path-lists
 */
static uword *fib_path_list_db;

/*
 * Debug macro
 */
#ifdef FIB_DEBUG
#define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)		  \
{   		            				  \
    u8 *_tmp = 0;					  \
    _tmp = fib_path_list_format(			  \
	fib_path_list_get_index(_pl), _tmp);		  \
    clib_warning("pl:[%d:%p:%p:%s]:" _fmt,		  \
		 fib_path_list_get_index(_pl),		  \
		 _pl, _pl->fpl_paths, _tmp,		  \
		 ##_args);				  \
    vec_free(_tmp);					  \
}
#else
#define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)
#endif

static fib_path_list_t *
fib_path_list_get (fib_node_index_t index)
{
    return (pool_elt_at_index(fib_path_list_pool, index));
}

static fib_node_t *
fib_path_list_get_node (fib_node_index_t index)
{
    return ((fib_node_t*)fib_path_list_get(index));
}

static fib_path_list_t*
fib_path_list_from_fib_node (fib_node_t *node)
{
#if CLIB_DEBUG > 0
    ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
#endif
    return ((fib_path_list_t*)node);
}

static fib_node_index_t
fib_path_list_get_index (fib_path_list_t *path_list)
{
    return (path_list - fib_path_list_pool);
}

static u8 *
format_fib_path_list (u8 * s, va_list * args)
{
    fib_path_list_attribute_t attr;
    fib_node_index_t *path_index;
    fib_path_list_t *path_list;

    path_list = va_arg (*args, fib_path_list_t *);
    
    s = format (s, "    index:%u", fib_path_list_get_index(path_list));
    s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
    s = format (s, " proto:%U", format_fib_protocol, path_list->fpl_nh_proto);

    if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
    {
	s = format (s, " flags:");
	FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
        {
	    if ((1<<attr) & path_list->fpl_flags)
            {
		s = format (s, "%s,", fib_path_list_attr_names[attr]);
	    }
	}
    }
    s = format (s, " %U\n", format_fib_urpf_list, path_list->fpl_urpf);

    vec_foreach (path_index, path_list->fpl_paths)
    {
	s = fib_path_format(*path_index, s);
	s = format(s, "\n");
    }

    return (s);
}

u8 *
fib_path_list_adjs_format (fib_node_index_t path_list_index,
			   u32 indent,
			   u8 * s)
{
    fib_path_list_t *path_list;
    u32 i;

    path_list = fib_path_list_get(path_list_index);

    vec_foreach_index (i, path_list->fpl_paths)
    {
	s = fib_path_adj_format(path_list->fpl_paths[i],
				indent, s);
    }

    return (s);
}


u8 *
fib_path_list_format (fib_node_index_t path_list_index,
		      u8 * s)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    return (format(s, "%U", format_fib_path_list, path_list));
}

static uword
fib_path_list_hash (fib_path_list_t *path_list)
{
    uword old_path_list_hash, new_path_list_hash, path_hash;
    fib_node_index_t *path_index;

    ASSERT(path_list);

    new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths);

    vec_foreach (path_index, path_list->fpl_paths)
    {
	path_hash = fib_path_hash(*path_index);
#if uword_bits == 64
	hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
#else
	hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
#endif
    }

    return (new_path_list_hash);
}

always_inline uword
fib_path_list_db_hash_key_from_index (uword index)
{
    return 1 + 2*index;
}

always_inline uword
fib_path_list_db_hash_key_is_index (uword key)
{
    return key & 1;
}

always_inline uword
fib_path_list_db_hash_key_2_index (uword key)
{
    ASSERT (fib_path_list_db_hash_key_is_index (key));
    return key / 2;
}

static fib_path_list_t*
fib_path_list_db_get_from_hash_key (uword key)
{
    fib_path_list_t *path_list;

    if (fib_path_list_db_hash_key_is_index (key))
    {
	fib_node_index_t path_list_index;

	path_list_index = fib_path_list_db_hash_key_2_index(key);
	path_list = fib_path_list_get(path_list_index);
    }
    else
    {       
	path_list = uword_to_pointer (key, fib_path_list_t *);
    }

    return (path_list);
}

static uword
fib_path_list_db_hash_key_sum (hash_t * h,
			       uword key)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_db_get_from_hash_key(key);

    return (fib_path_list_hash(path_list));
}

static uword
fib_path_list_db_hash_key_equal (hash_t * h,
				 uword key1,
				 uword key2)
{
    fib_path_list_t *path_list1, *path_list2;

    path_list1 = fib_path_list_db_get_from_hash_key(key1);
    path_list2 = fib_path_list_db_get_from_hash_key(key2);

    return (fib_path_list_hash(path_list1) ==
	    fib_path_list_hash(path_list2));
}

static fib_node_index_t
fib_path_list_db_find (fib_path_list_t *path_list)
{
    uword *p;

    p = hash_get(fib_path_list_db, path_list);

    if (NULL != p)
    {
	return p[0];
    }

    return (FIB_NODE_INDEX_INVALID);
}

static void
fib_path_list_db_insert (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));

    hash_set (fib_path_list_db,
	      fib_path_list_db_hash_key_from_index(path_list_index),
	      path_list_index);

    FIB_PATH_LIST_DBG(path_list, "DB-inserted");
}

static void
fib_path_list_db_remove (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));

    hash_unset(fib_path_list_db,
	       fib_path_list_db_hash_key_from_index(path_list_index));

    FIB_PATH_LIST_DBG(path_list, "DB-removed");
}

static void
fib_path_list_destroy (fib_path_list_t *path_list)
{
    fib_node_index_t *path_index;

    FIB_PATH_LIST_DBG(path_list, "destroy");

    vec_foreach (path_index, path_list->fpl_paths)
    {
	fib_path_destroy(*path_index);
    }

    vec_free(path_list->fpl_paths);
    fib_urpf_list_unlock(path_list->fpl_urpf);

    fib_node_deinit(&path_list->fpl_node);
    pool_put(fib_path_list_pool, path_list);
}

static void
fib_path_list_last_lock_gone (fib_node_t *node)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_from_fib_node(node);

    FIB_PATH_LIST_DBG(path_list, "last-lock");

    if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
    {
	fib_path_list_db_remove(fib_path_list_get_index(path_list));
    }
    fib_path_list_destroy(path_list);
}

/*
 * fib_path_mk_lb
 *
 * update the multipath adj this path-list will contribute to its
 * children's forwarding.
 */
static void
fib_path_list_mk_lb (fib_path_list_t *path_list,
		     fib_forward_chain_type_t fct,
		     dpo_id_t *dpo)
{
    load_balance_path_t *hash_key;
    fib_node_index_t *path_index;

    hash_key  = NULL;

    if (!dpo_id_is_valid(dpo))
    {
        /*
         * first time create
         */
        dpo_set(dpo,
                DPO_LOAD_BALANCE,
                fib_forw_chain_type_to_dpo_proto(fct),
                load_balance_create(0,
				    fib_forw_chain_type_to_dpo_proto(fct),
				    0 /* FIXME FLOW HASH */));
    }

    /*
     * We gather the DPOs from resolved paths.
     */
    vec_foreach (path_index, path_list->fpl_paths)
    {
	hash_key = fib_path_append_nh_for_multipath_hash(
	               *path_index,
		       fct,
		       hash_key);
    }

    /*
     * Path-list load-balances, which if used, would be shared and hence
     * never need a load-balance map.
     */
    load_balance_multipath_update(dpo, hash_key, LOAD_BALANCE_FLAG_NONE);

    FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);

    vec_free(hash_key);
}

/**
 * @brief [re]build the path list's uRPF list
 */
static void
fib_path_list_mk_urpf (fib_path_list_t *path_list)
{
    fib_node_index_t *path_index;

    /*
     * ditch the old one. by iterating through all paths we are going
     * to re-find all the adjs that were in the old one anyway. If we
     * keep the old one, then the |sort|uniq requires more work.
     * All users of the RPF list have their own lock, so we can release
     * immediately.
     */
    fib_urpf_list_unlock(path_list->fpl_urpf);
    path_list->fpl_urpf = fib_urpf_list_alloc_and_lock();

    vec_foreach (path_index, path_list->fpl_paths)
    {
	fib_path_contribute_urpf(*path_index, path_list->fpl_urpf);
    }

    fib_urpf_list_bake(path_list->fpl_urpf);
}

/**
 * @brief Contribute (add) this path list's uRPF list. This allows the child
 * to construct an aggregate list.
 */
void
fib_path_list_contribute_urpf (fib_node_index_t path_list_index,
			       index_t urpf)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    fib_urpf_list_combine(urpf, path_list->fpl_urpf);
}

/**
 * @brief Return the the child the RPF list pre-built for this path list
 */
index_t
fib_path_list_get_urpf (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    return (path_list->fpl_urpf);
}

/*
 * fib_path_list_back_walk
 *
 * Called from one of this path-list's paths to progate
 * a back walk
 */
void
fib_path_list_back_walk (fib_node_index_t path_list_index,
			 fib_node_back_walk_ctx_t *ctx)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    fib_path_list_mk_urpf(path_list);

    /*
     * propagate the backwalk further
     */
    if (32 >= fib_node_list_get_size(path_list->fpl_node.fn_children))
    {
        /*
         * only a few children. continue the walk synchronously
         */
	fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
    }
    else
    {
        /*
         * many children. schedule a async walk
         */
        fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
                       path_list_index,
                       FIB_WALK_PRIORITY_LOW,
                       ctx);
    }
}

/*
 * fib_path_list_back_walk_notify
 *
 * A back walk has reach this path-list.
 */
static fib_node_back_walk_rc_t
fib_path_list_back_walk_notify (fib_node_t *node,
				fib_node_back_walk_ctx_t *ctx)
{
    /*
     * the path-list is not a direct child of any other node type
     * paths, which do not change thier to-list-mapping, save the
     * list they are a member of, and invoke the BW function directly.
     */
    ASSERT(0);

    return (FIB_NODE_BACK_WALK_CONTINUE);
}

/*
 * Display the path-list memory usage
 */
static void
fib_path_list_memory_show (void)
{
    fib_show_memory_usage("Path-list",
			  pool_elts(fib_path_list_pool),
			  pool_len(fib_path_list_pool),
			  sizeof(fib_path_list_t));
    fib_urpf_list_show_mem();
}

/*
 * The FIB path-list's graph node virtual function table
 */
static const fib_node_vft_t fib_path_list_vft = {
    .fnv_get = fib_path_list_get_node,
    .fnv_last_lock = fib_path_list_last_lock_gone,
    .fnv_back_walk = fib_path_list_back_walk_notify,
    .fnv_mem_show = fib_path_list_memory_show,
};

static fib_path_list_t *
fib_path_list_alloc (fib_node_index_t *path_list_index)
{
    fib_path_list_t *path_list;

    pool_get(fib_path_list_pool, path_list);
    memset(path_list, 0, sizeof(*path_list));

    fib_node_init(&path_list->fpl_node,
		  FIB_NODE_TYPE_PATH_LIST);
    path_list->fpl_urpf = INDEX_INVALID;

    if (NULL != path_list_index)
    {
	*path_list_index = fib_path_list_get_index(path_list);
    }

    FIB_PATH_LIST_DBG(path_list, "alloc");

    return (path_list);
}

static fib_path_list_t *
fib_path_list_resolve (fib_path_list_t *path_list)
{
    fib_node_index_t *path_index, *paths, path_list_index;

    ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));

    /*
     * resolving a path-list is a recursive action. this means more path
     * lists can be created during this call, and hence this path-list
     * can be realloc'd. so we work with copies.
     * this function is called only once per-path list, so its no great overhead.
     */
    path_list_index = fib_path_list_get_index(path_list);
    paths = vec_dup(path_list->fpl_paths);

    vec_foreach (path_index, paths)
    {
	fib_path_resolve(*path_index);
    }

    vec_free(paths);
    path_list = fib_path_list_get(path_list_index);

    FIB_PATH_LIST_DBG(path_list, "resovled");
    fib_path_list_mk_urpf(path_list);

    return (path_list);
}

u32
fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
{
    fib_node_index_t *path_index;
    fib_path_list_t *path_list;
    u32 sw_if_index;

    path_list = fib_path_list_get(path_list_index);

    sw_if_index = ~0;
    vec_foreach (path_index, path_list->fpl_paths)
    {
	sw_if_index = fib_path_get_resolving_interface(*path_index);
	if (~0 != sw_if_index)
	{
	    return (sw_if_index);
	}
    }

    return (sw_if_index);
}

fib_protocol_t
fib_path_list_get_proto (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    /*
     * we don't support a mix of path protocols, so we can return the proto
     * of the first
     */
    return (fib_path_get_proto(path_list->fpl_paths[0]));
}

int
fib_path_list_is_looped (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
}

static fib_path_cfg_flags_t 
fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
{
    fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;

    if (plf & FIB_PATH_LIST_FLAG_LOCAL)
    {
	pf |= FIB_PATH_CFG_FLAG_LOCAL;
    }
    if (plf & FIB_PATH_LIST_FLAG_DROP)
    {
	pf |= FIB_PATH_CFG_FLAG_DROP;
    }
    if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
    {
	pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
    }

    return (pf);
}

static fib_path_list_flags_t
fib_path_list_flags_fixup (fib_path_list_flags_t flags)
{
    /*
     * we do no share drop nor exclusive path-lists
     */
    if (flags & FIB_PATH_LIST_FLAG_DROP ||
	flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
    {
	flags &= ~FIB_PATH_LIST_FLAG_SHARED;
    }

    return (flags);
}

fib_node_index_t
fib_path_list_create (fib_path_list_flags_t flags,
		      const fib_route_path_t *rpaths)
{
    fib_node_index_t path_list_index, old_path_list_index;
    fib_path_list_t *path_list;
    int i;

    flags = fib_path_list_flags_fixup(flags);
    path_list = fib_path_list_alloc(&path_list_index);
    path_list->fpl_flags = flags;
    /*
     * we'll assume for now all paths are the same next-hop protocol
     */
    path_list->fpl_nh_proto = rpaths[0].frp_proto;

    vec_foreach_index(i, rpaths)
    {
	vec_add1(path_list->fpl_paths,
		 fib_path_create(path_list_index,
				 path_list->fpl_nh_proto,
				 fib_path_list_flags_2_path_flags(flags),
				 &rpaths[i]));
    }

    /*
     * If a shared path list is requested, consult the DB for a match
     */
    if (flags & FIB_PATH_LIST_FLAG_SHARED)
    {
	/*
	 * check for a matching path-list in the DB.
	 * If we find one then we can return the existing one and destroy the
	 * new one just created.
	 */
	old_path_list_index = fib_path_list_db_find(path_list);
	if (FIB_NODE_INDEX_INVALID != old_path_list_index)
	{
	    fib_path_list_destroy(path_list);
	
	    path_list_index = old_path_list_index;
	}
	else
	{
	    /*
	     * if there was not a matching path-list, then this
	     * new one will need inserting into the DB and resolving.
	     */
	    fib_path_list_db_insert(path_list_index);
	    path_list = fib_path_list_resolve(path_list);
	}
    }
    else
    {
	/*
	 * no shared path list requested. resolve and use the one
	 * just created.
	 */
	path_list = fib_path_list_resolve(path_list);
    }

    return (path_list_index);
}

fib_node_index_t
fib_path_list_create_special (fib_protocol_t nh_proto,
			      fib_path_list_flags_t flags,
			      const dpo_id_t *dpo)
{
    fib_node_index_t path_index, path_list_index;
    fib_path_list_t *path_list;

    path_list = fib_path_list_alloc(&path_list_index);
    path_list->fpl_flags = flags;
    path_list->fpl_nh_proto = nh_proto;

    path_index =
	fib_path_create_special(path_list_index,
				path_list->fpl_nh_proto,
				fib_path_list_flags_2_path_flags(flags),
				dpo);
    vec_add1(path_list->fpl_paths, path_index);

    /*
     * we don't share path-lists. we can do PIC on them so why bother.
     */
    path_list = fib_path_list_resolve(path_list);

    return (path_list_index);
}

/*
 * fib_path_list_copy_and_path_add
 *
 * Create a copy of a path-list and append one more path to it.
 * The path-list returned could either have been newly created, or
 * can be a shared path-list from the data-base.
 */
fib_node_index_t
fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
				 fib_path_list_flags_t flags,
				 const fib_route_path_t *rpaths)
{
    fib_node_index_t path_index, new_path_index, *orig_path_index;
    fib_path_list_t *path_list, *orig_path_list;
    fib_node_index_t path_list_index;
    fib_node_index_t pi;

    ASSERT(1 == vec_len(rpaths));

    /*
     * alloc the new list before we retrieve the old one, lest
     * the alloc result in a realloc
     */
    path_list = fib_path_list_alloc(&path_list_index);

    orig_path_list = fib_path_list_get(orig_path_list_index);

    FIB_PATH_LIST_DBG(orig_path_list, "copy-add");

    flags = fib_path_list_flags_fixup(flags);
    path_list->fpl_flags = flags;
    path_list->fpl_nh_proto = orig_path_list->fpl_nh_proto;
    vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
    pi = 0;

    new_path_index = fib_path_create(path_list_index,
                                     path_list->fpl_nh_proto,
                                     fib_path_list_flags_2_path_flags(flags),
                                     rpaths);

    vec_foreach (orig_path_index, orig_path_list->fpl_paths)
    {
        /*
         * don't add duplicate paths
         * In the unlikely event the path is a duplicate, then we'll
         * find a matching path-list later and this one will be toast.
         */
	if (0 != fib_path_cmp(new_path_index, *orig_path_index))
        {
            path_index = fib_path_copy(*orig_path_index, path_list_index);
            path_list->fpl_paths[pi++] = path_index;
        }
        else
        {
            _vec_len(path_list->fpl_paths) = vec_len(orig_path_list->fpl_paths);
        }
    }

    path_list->fpl_paths[pi] = new_path_index;

    /*
     * we sort the paths since the key for the path-list is
     * the description of the paths it contains. The paths need to
     * be sorted else this description will differ.
     */
    vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);

    FIB_PATH_LIST_DBG(path_list, "path-added");

    /*
     * If a shared path list is requested, consult the DB for a match
     */
    if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
    {
	fib_node_index_t exist_path_list_index;
	/*
	 * check for a matching path-list in the DB.
	 * If we find one then we can return the existing one and destroy the
	 * new one just created.
	 */
	exist_path_list_index = fib_path_list_db_find(path_list);
	if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
	{
	    fib_path_list_destroy(path_list);
	
	    path_list_index = exist_path_list_index;
	}
	else
	{
	    /*
	     * if there was not a matching path-list, then this
	     * new one will need inserting into the DB and resolving.
	     */
	    fib_path_list_db_insert(path_list_index);

	    path_list = fib_path_list_resolve(path_list);
	}
    }
    else
    {
	/*
	 * no shared path list requested. resolve and use the one
	 * just created.
	 */
	path_list = fib_path_list_resolve(path_list);
    }

    return (path_list_index);
}

/*
 * fib_path_list_copy_and_path_remove
 *
 * Copy the path-list excluding the path passed.
 * If the path is the last one, then the index reurned will be invalid.
 * i.e. the path-list is toast.
 */
fib_node_index_t
fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
				    fib_path_list_flags_t flags,
				    const fib_route_path_t *rpaths)
{
    fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
    fib_path_list_t *path_list,  *orig_path_list;
    fib_node_index_t pi;

    ASSERT(1 == vec_len(rpaths));

    path_list = fib_path_list_alloc(&path_list_index);

    flags = fib_path_list_flags_fixup(flags);
    orig_path_list = fib_path_list_get(orig_path_list_index);

    FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");

    path_list->fpl_flags = flags;
    path_list->fpl_nh_proto = orig_path_list->fpl_nh_proto;
    /*
     * allocate as many paths as we might need in one go, rather than
     * using vec_add to do a few at a time.
     */
    if (vec_len(orig_path_list->fpl_paths) > 1)
    {
	vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
    }
    pi = 0;

    /*
     * create a representation of the path to be removed, so it
     * can be used as a comparison object during the copy.
     */
    tmp_path_index = fib_path_create(path_list_index,
				     path_list->fpl_nh_proto,
				     fib_path_list_flags_2_path_flags(flags),
				     rpaths);

    vec_foreach (orig_path_index, orig_path_list->fpl_paths)
    {
	if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
	    path_index = fib_path_copy(*orig_path_index, path_list_index);
	    if (pi < vec_len(path_list->fpl_paths))
	    {
		path_list->fpl_paths[pi++] = path_index;
	    }
	    else
	    {
		/*
		 * this is the unlikely case that the path being
		 * removed does not match one in the path-list, so
		 * we end up with as many paths as we started with.
		 * the paths vector was sized above with the expectation
		 * that we would have 1 less.
		 */
		vec_add1(path_list->fpl_paths, path_index);
	    }
	}
    }

    /*
     * done with the temporary now
     */
    fib_path_destroy(tmp_path_index);

    /*
     * if there are no paths, then the new path-list is aborted
     */
    if (0 == vec_len(path_list->fpl_paths)) {
	FIB_PATH_LIST_DBG(path_list, "last-path-removed");

	fib_path_list_destroy(path_list);

	path_list_index = FIB_NODE_INDEX_INVALID;
    } else {
	/*
	 * we sort the paths since the key for the path-list is
	 * the description of the paths it contains. The paths need to
	 * be sorted else this description will differ.
	 */
	vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
    
	/*
	 * If a shared path list is requested, consult the DB for a match
	 */
	if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
	{
	    fib_node_index_t exist_path_list_index;

            /*
	     * check for a matching path-list in the DB.
	     * If we find one then we can return the existing one and destroy the
	     * new one just created.
	     */
	    exist_path_list_index = fib_path_list_db_find(path_list);
	    if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
	    {
		fib_path_list_destroy(path_list);
	
		path_list_index = exist_path_list_index;
	    }
	    else
	    {
		/*
		 * if there was not a matching path-list, then this
		 * new one will need inserting into the DB and resolving.
		 */
		fib_path_list_db_insert(path_list_index);

		path_list = fib_path_list_resolve(path_list);
	    }
	}
	else
	{
	    /*
	     * no shared path list requested. resolve and use the one
	     * just created.
	     */
	    path_list = fib_path_list_resolve(path_list);
	}
    }

    return (path_list_index);
}

/*
 * fib_path_list_contribute_forwarding
 *
 * Return the index of a load-balance that user of this path-list should
 * use for forwarding
 */
void
fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
				     fib_forward_chain_type_t type,
				     dpo_id_t *dpo)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    fib_path_list_mk_lb(path_list, type, dpo);
}

/*
 * fib_path_list_get_adj
 *
 * Return the index of a adjacency for the first path that user of this
 * path-list should use for forwarding
 */
adj_index_t
fib_path_list_get_adj (fib_node_index_t path_list_index,
		       fib_forward_chain_type_t type)
{
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);
    return (fib_path_get_adj(path_list->fpl_paths[0]));
}

int
fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
				     fib_node_index_t **entry_indicies)
{
    fib_node_index_t *path_index;
    int is_looped, list_looped;
    fib_path_list_t *path_list;

    list_looped = 0;
    path_list = fib_path_list_get(path_list_index);

    vec_foreach (path_index, path_list->fpl_paths)
    {
	fib_node_index_t *copy, **copy_ptr;

	/*
	 * we need a copy of the nodes visited so that when we add entries
	 * we explore on the nth path and a looped is detected, those entries
	 * are not again searched for n+1 path and so finding a loop that does
	 * not exist.
	 */
	copy = vec_dup(*entry_indicies);
	copy_ptr = &copy;

	is_looped  = fib_path_recursive_loop_detect(*path_index, copy_ptr);
	list_looped += is_looped;
    }

    FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", eval);

    if (list_looped)
    {
	path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
    }
    else
    {
	path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
    }

    return (list_looped);
}

u32
fib_path_list_child_add (fib_node_index_t path_list_index,
			 fib_node_type_t child_type,
			 fib_node_index_t child_index)
{
    return (fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
                               path_list_index,
                               child_type,
                               child_index));
}

void
fib_path_list_child_remove (fib_node_index_t path_list_index,
			    u32 si)
{
    fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
                          path_list_index,
                          si);
}

void
fib_path_list_lock(fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    if (FIB_NODE_INDEX_INVALID != path_list_index)
    {
	path_list = fib_path_list_get(path_list_index);

	fib_node_lock(&path_list->fpl_node);
	FIB_PATH_LIST_DBG(path_list, "lock");
    }
}

void
fib_path_list_unlock (fib_node_index_t path_list_index)
{
    fib_path_list_t *path_list;

    if (FIB_NODE_INDEX_INVALID != path_list_index)
    {
	path_list = fib_path_list_get(path_list_index);
	FIB_PATH_LIST_DBG(path_list, "unlock");
    
	fib_node_unlock(&path_list->fpl_node);
    }
}

u32
fib_path_list_pool_size (void)
{
    return (pool_elts(fib_path_list_pool));    
}

u32
fib_path_list_db_size (void)
{
    return (hash_elts(fib_path_list_db));
}

void
fib_path_list_walk (fib_node_index_t path_list_index,
		    fib_path_list_walk_fn_t func,
		    void *ctx)
{
    fib_node_index_t *path_index;
    fib_path_list_t *path_list;

    path_list = fib_path_list_get(path_list_index);

    vec_foreach(path_index, path_list->fpl_paths)
    {
	if (!func(path_list_index, *path_index, ctx))
	    break;
    }
}


void
fib_path_list_module_init (void)
{
    fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);

    fib_path_list_db = hash_create2 (/* elts */ 0,
    				     /* user */ 0,
    				     /* value_bytes */ sizeof (fib_node_index_t),
    				     fib_path_list_db_hash_key_sum,
    				     fib_path_list_db_hash_key_equal,
    				     /* format pair/arg */
    				     0, 0);
}

static clib_error_t *
show_fib_path_list_command (vlib_main_t * vm,
			    unformat_input_t * input,
			    vlib_cli_command_t * cmd)
{
    fib_path_list_t *path_list;
    fib_node_index_t pli;

    if (unformat (input, "%d", &pli))
    {
	/*
	 * show one in detail
	 */
	if (!pool_is_free_index(fib_path_list_pool, pli))
	{
	    path_list = fib_path_list_get(pli);
	    u8 *s = fib_path_list_format(pli, NULL);
	    s = format(s, "children:");
	    s = fib_node_children_format(path_list->fpl_node.fn_children, s);
	    vlib_cli_output (vm, "%s", s);
	    vec_free(s);
	}
	else
	{
	    vlib_cli_output (vm, "path list %d invalid", pli);
	}
    }
    else
    {
	/*
	 * show all
	 */
	vlib_cli_output (vm, "FIB Path Lists");
	pool_foreach(path_list, fib_path_list_pool,
	({
	    vlib_cli_output (vm, "%U", format_fib_path_list, path_list);
	}));
    }
    return (NULL);
}

VLIB_CLI_COMMAND (show_fib_path_list, static) = {
  .path = "show fib path-lists",
  .function = show_fib_path_list_command,
  .short_help = "show fib path-lists",
};