3523d93a7bbb627c6c2c22db4caa912e7b19607a
[vpp.git] / vnet / vnet / fib / fib_path_list.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vppinfra/mhash.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/adj/adj.h>
19 #include <vnet/dpo/load_balance.h>
20 #include <vnet/dpo/load_balance_map.h>
21
22 #include <vnet/fib/fib_path_list.h>
23 #include <vnet/fib/fib_internal.h>
24 #include <vnet/fib/fib_node_list.h>
25 #include <vnet/fib/fib_walk.h>
26
27 /**
28  * FIB path-list
29  * A representation of the list/set of path trough which a prefix is reachable
30  */
31 typedef struct fib_path_list_t_ {
32     /**
33      * A path-list is a node in the FIB graph.
34      */
35     fib_node_t fpl_node;
36
37     /**
38      * Flags on the path-list
39      */
40     fib_path_list_flags_t fpl_flags;
41
42     /**
43      * The next-hop protocol for the paths in this path list.
44      * Note that fixing the proto here means we don't support a mix of
45      * v4 and v6 paths. ho hum.
46      */
47     fib_protocol_t fpl_nh_proto;
48
49     /**
50      * Vector of paths indecies for all configured paths.
51      * For shareable path-lists this list MUST not change.
52      */
53     fib_node_index_t *fpl_paths;
54 } fib_path_list_t;
55
56 /*
57  * Array of strings/names for the FIB sources
58  */
59 static const char *fib_path_list_attr_names[] = FIB_PATH_LIST_ATTRIBUTES;
60
61 /*
62  * The memory pool from which we allocate all the path-lists
63  */
64 static fib_path_list_t * fib_path_list_pool;
65
66 /*
67  * The data-base of shared path-lists
68  */
69 static uword *fib_path_list_db;
70
71 /*
72  * Debug macro
73  */
74 #ifdef FIB_DEBUG
75 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)            \
76 {                                                         \
77     u8 *_tmp = 0;                                         \
78     _tmp = fib_path_list_format(                          \
79         fib_path_list_get_index(_pl), _tmp);              \
80     clib_warning("pl:[%d:%p:%p:%s]:" _fmt,                \
81                  fib_path_list_get_index(_pl),            \
82                  _pl, _pl->fpl_paths, _tmp,               \
83                  ##_args);                                \
84     vec_free(_tmp);                                       \
85 }
86 #else
87 #define FIB_PATH_LIST_DBG(_pl, _fmt, _args...)
88 #endif
89
90 static fib_path_list_t *
91 fib_path_list_get (fib_node_index_t index)
92 {
93     return (pool_elt_at_index(fib_path_list_pool, index));
94 }
95
96 static fib_node_t *
97 fib_path_list_get_node (fib_node_index_t index)
98 {
99     return ((fib_node_t*)fib_path_list_get(index));
100 }
101
102 static fib_path_list_t*
103 fib_path_list_from_fib_node (fib_node_t *node)
104 {
105 #if CLIB_DEBUG > 0
106     ASSERT(FIB_NODE_TYPE_PATH_LIST == node->fn_type);
107 #endif
108     return ((fib_path_list_t*)node);
109 }
110
111 static fib_node_index_t
112 fib_path_list_get_index (fib_path_list_t *path_list)
113 {
114     return (path_list - fib_path_list_pool);
115 }
116
117 static u8 *
118 format_fib_path_list (u8 * s, va_list * args)
119 {
120     fib_path_list_attribute_t attr;
121     fib_node_index_t *path_index;
122     fib_path_list_t *path_list;
123
124     path_list = va_arg (*args, fib_path_list_t *);
125     
126     s = format (s, "    index:%u", fib_path_list_get_index(path_list));
127     s = format (s, " locks:%u", path_list->fpl_node.fn_locks);
128     s = format (s, " proto:%U", format_fib_protocol, path_list->fpl_nh_proto);
129
130     if (FIB_PATH_LIST_FLAG_NONE != path_list->fpl_flags)
131     {
132         s = format (s, " flags:");
133         FOR_EACH_PATH_LIST_ATTRIBUTE(attr)
134         {
135             if ((1<<attr) & path_list->fpl_flags)
136             {
137                 s = format (s, "%s,", fib_path_list_attr_names[attr]);
138             }
139         }
140     }
141     vec_foreach (path_index, path_list->fpl_paths)
142     {
143         s = fib_path_format(*path_index, s);
144         s = format(s, "\n");
145     }
146
147     return (s);
148 }
149
150 u8 *
151 fib_path_list_adjs_format (fib_node_index_t path_list_index,
152                            u32 indent,
153                            u8 * s)
154 {
155     fib_path_list_t *path_list;
156     u32 i;
157
158     path_list = fib_path_list_get(path_list_index);
159
160     vec_foreach_index (i, path_list->fpl_paths)
161     {
162         s = fib_path_adj_format(path_list->fpl_paths[i],
163                                 indent, s);
164     }
165
166     return (s);
167 }
168
169
170 u8 *
171 fib_path_list_format (fib_node_index_t path_list_index,
172                       u8 * s)
173 {
174     fib_path_list_t *path_list;
175
176     path_list = fib_path_list_get(path_list_index);
177
178     return (format(s, "%U", format_fib_path_list, path_list));
179 }
180
181 static uword
182 fib_path_list_hash (fib_path_list_t *path_list)
183 {
184     uword old_path_list_hash, new_path_list_hash, path_hash;
185     fib_node_index_t *path_index;
186
187     ASSERT(path_list);
188
189     new_path_list_hash = old_path_list_hash = vec_len(path_list->fpl_paths);
190
191     vec_foreach (path_index, path_list->fpl_paths)
192     {
193         path_hash = fib_path_hash(*path_index);
194 #if uword_bits == 64
195         hash_mix64(path_hash, old_path_list_hash, new_path_list_hash);
196 #else
197         hash_mix32(path_hash, old_path_list_hash, new_path_list_hash);
198 #endif
199     }
200
201     return (new_path_list_hash);
202 }
203
204 always_inline uword
205 fib_path_list_db_hash_key_from_index (uword index)
206 {
207     return 1 + 2*index;
208 }
209
210 always_inline uword
211 fib_path_list_db_hash_key_is_index (uword key)
212 {
213     return key & 1;
214 }
215
216 always_inline uword
217 fib_path_list_db_hash_key_2_index (uword key)
218 {
219     ASSERT (fib_path_list_db_hash_key_is_index (key));
220     return key / 2;
221 }
222
223 static fib_path_list_t*
224 fib_path_list_db_get_from_hash_key (uword key)
225 {
226     fib_path_list_t *path_list;
227
228     if (fib_path_list_db_hash_key_is_index (key))
229     {
230         fib_node_index_t path_list_index;
231
232         path_list_index = fib_path_list_db_hash_key_2_index(key);
233         path_list = fib_path_list_get(path_list_index);
234     }
235     else
236     {       
237         path_list = uword_to_pointer (key, fib_path_list_t *);
238     }
239
240     return (path_list);
241 }
242
243 static uword
244 fib_path_list_db_hash_key_sum (hash_t * h,
245                                uword key)
246 {
247     fib_path_list_t *path_list;
248
249     path_list = fib_path_list_db_get_from_hash_key(key);
250
251     return (fib_path_list_hash(path_list));
252 }
253
254 static uword
255 fib_path_list_db_hash_key_equal (hash_t * h,
256                                  uword key1,
257                                  uword key2)
258 {
259     fib_path_list_t *path_list1, *path_list2;
260
261     path_list1 = fib_path_list_db_get_from_hash_key(key1);
262     path_list2 = fib_path_list_db_get_from_hash_key(key2);
263
264     return (fib_path_list_hash(path_list1) ==
265             fib_path_list_hash(path_list2));
266 }
267
268 static fib_node_index_t
269 fib_path_list_db_find (fib_path_list_t *path_list)
270 {
271     uword *p;
272
273     p = hash_get(fib_path_list_db, path_list);
274
275     if (NULL != p)
276     {
277         return p[0];
278     }
279
280     return (FIB_NODE_INDEX_INVALID);
281 }
282
283 static void
284 fib_path_list_db_insert (fib_node_index_t path_list_index)
285 {
286     fib_path_list_t *path_list;
287
288     path_list = fib_path_list_get(path_list_index);
289
290     ASSERT(FIB_NODE_INDEX_INVALID == fib_path_list_db_find(path_list));
291
292     hash_set (fib_path_list_db,
293               fib_path_list_db_hash_key_from_index(path_list_index),
294               path_list_index);
295
296     FIB_PATH_LIST_DBG(path_list, "DB-inserted");
297 }
298
299 static void
300 fib_path_list_db_remove (fib_node_index_t path_list_index)
301 {
302     fib_path_list_t *path_list;
303
304     path_list = fib_path_list_get(path_list_index);
305
306     ASSERT(FIB_NODE_INDEX_INVALID != fib_path_list_db_find(path_list));
307
308     hash_unset(fib_path_list_db,
309                fib_path_list_db_hash_key_from_index(path_list_index));
310
311     FIB_PATH_LIST_DBG(path_list, "DB-removed");
312 }
313
314 static void
315 fib_path_list_destroy (fib_path_list_t *path_list)
316 {
317     fib_node_index_t *path_index;
318
319     FIB_PATH_LIST_DBG(path_list, "destroy");
320
321     vec_foreach (path_index, path_list->fpl_paths)
322     {
323         fib_path_destroy(*path_index);
324     }    
325
326     vec_free(path_list->fpl_paths);
327
328     fib_node_deinit(&path_list->fpl_node);
329     pool_put(fib_path_list_pool, path_list);
330 }
331
332 static void
333 fib_path_list_last_lock_gone (fib_node_t *node)
334 {
335     fib_path_list_t *path_list;
336
337     path_list = fib_path_list_from_fib_node(node);
338
339     FIB_PATH_LIST_DBG(path_list, "last-lock");
340
341     if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
342     {
343         fib_path_list_db_remove(fib_path_list_get_index(path_list));
344     }
345     fib_path_list_destroy(path_list);
346 }
347
348 /*
349  * fib_path_mk_lb
350  *
351  * update the multipath adj this path-list will contribute to its
352  * children's forwarding.
353  */
354 static void
355 fib_path_list_mk_lb (fib_path_list_t *path_list,
356                      fib_forward_chain_type_t fct,
357                      dpo_id_t *dpo)
358 {
359     load_balance_path_t *hash_key;
360     fib_node_index_t *path_index;
361
362     hash_key  = NULL;
363
364     if (!dpo_id_is_valid(dpo))
365     {
366         /*
367          * first time create
368          */
369         dpo_set(dpo,
370                 DPO_LOAD_BALANCE,
371                 fib_forw_chain_type_to_dpo_proto(fct),
372                 load_balance_create(0,
373                                     fib_forw_chain_type_to_dpo_proto(fct),
374                                     0 /* FIXME FLOW HASH */));
375     }
376
377     /*
378      * We gather the DPOs from resolved paths.
379      */
380     vec_foreach (path_index, path_list->fpl_paths)
381     {
382         hash_key = fib_path_append_nh_for_multipath_hash(
383                        *path_index,
384                        fct,
385                        hash_key);
386     }
387
388     /*
389      * Path-list load-balances, which if used, would be shared and hence
390      * never need a load-balance map.
391      */
392     load_balance_multipath_update(dpo, hash_key, LOAD_BALANCE_FLAG_NONE);
393
394     FIB_PATH_LIST_DBG(path_list, "mk lb: %d", dpo->dpoi_index);
395
396     vec_free(hash_key);
397 }
398
399 /*
400  * fib_path_list_back_walk
401  *
402  * Called from one of this path-list's paths to progate
403  * a back walk
404  */
405 void
406 fib_path_list_back_walk (fib_node_index_t path_list_index,
407                          fib_node_back_walk_ctx_t *ctx)
408 {
409     fib_path_list_t *path_list;
410
411     path_list = fib_path_list_get(path_list_index);
412
413     /*
414      * propagate the backwalk further
415      */
416     if (32 >= fib_node_list_get_size(path_list->fpl_node.fn_children))
417     {
418         /*
419          * only a few children. continue the walk synchronously
420          */
421         fib_walk_sync(FIB_NODE_TYPE_PATH_LIST, path_list_index, ctx);
422     }
423     else
424     {
425         /*
426          * many children. schedule a async walk
427          */
428         fib_walk_async(FIB_NODE_TYPE_PATH_LIST,
429                        path_list_index,
430                        FIB_WALK_PRIORITY_LOW,
431                        ctx);
432     }
433 }
434
435 /*
436  * fib_path_list_back_walk_notify
437  *
438  * A back walk has reach this path-list.
439  */
440 static fib_node_back_walk_rc_t
441 fib_path_list_back_walk_notify (fib_node_t *node,
442                                 fib_node_back_walk_ctx_t *ctx)
443 {
444     /*
445      * the path-list is not a direct child of any other node type
446      * paths, which do not change thier to-list-mapping, save the
447      * list they are a member of, and invoke the BW function directly.
448      */
449     ASSERT(0);
450
451     return (FIB_NODE_BACK_WALK_CONTINUE);
452 }
453
454 /*
455  * The FIB path-list's graph node virtual function table
456  */
457 static const fib_node_vft_t fib_path_list_vft = {
458     .fnv_get = fib_path_list_get_node,
459     .fnv_last_lock = fib_path_list_last_lock_gone,
460     .fnv_back_walk = fib_path_list_back_walk_notify,
461 };
462
463 static fib_path_list_t *
464 fib_path_list_alloc (fib_node_index_t *path_list_index)
465 {
466     fib_path_list_t *path_list;
467
468     pool_get(fib_path_list_pool, path_list);
469     memset(path_list, 0, sizeof(*path_list));
470
471     fib_node_init(&path_list->fpl_node,
472                   FIB_NODE_TYPE_PATH_LIST);
473
474     if (NULL != path_list_index)
475     {
476         *path_list_index = fib_path_list_get_index(path_list);
477     }
478
479     FIB_PATH_LIST_DBG(path_list, "alloc");
480
481     return (path_list);
482 }
483
484 static fib_path_list_t *
485 fib_path_list_resolve (fib_path_list_t *path_list)
486 {
487     fib_node_index_t *path_index, *paths, path_list_index;
488
489     ASSERT(!(path_list->fpl_flags & FIB_PATH_LIST_FLAG_RESOLVED));
490
491     /*
492      * resolving a path-list is a recursive action. this means more path
493      * lists can be created during this call, and hence this path-list
494      * can be realloc'd. so we work with copies.
495      * this function is called only once per-path list, so its no great overhead.
496      */
497     path_list_index = fib_path_list_get_index(path_list);
498     paths = vec_dup(path_list->fpl_paths);
499
500     vec_foreach (path_index, paths)
501     {
502         fib_path_resolve(*path_index);
503     }
504
505     vec_free(paths);
506     path_list = fib_path_list_get(path_list_index);
507
508     FIB_PATH_LIST_DBG(path_list, "resovled");
509
510     return (path_list);
511 }
512
513 u32
514 fib_path_list_get_resolving_interface (fib_node_index_t path_list_index)
515 {
516     fib_node_index_t *path_index;
517     fib_path_list_t *path_list;
518     u32 sw_if_index;
519
520     path_list = fib_path_list_get(path_list_index);
521
522     sw_if_index = ~0;
523     vec_foreach (path_index, path_list->fpl_paths)
524     {
525         sw_if_index = fib_path_get_resolving_interface(*path_index);
526         if (~0 != sw_if_index)
527         {
528             return (sw_if_index);
529         }
530     }
531
532     return (sw_if_index);
533 }
534
535 int
536 fib_path_list_is_looped (fib_node_index_t path_list_index)
537 {
538     fib_path_list_t *path_list;
539
540     path_list = fib_path_list_get(path_list_index);
541
542     return (path_list->fpl_flags & FIB_PATH_LIST_FLAG_LOOPED);
543 }
544
545 static fib_path_cfg_flags_t 
546 fib_path_list_flags_2_path_flags (fib_path_list_flags_t plf)
547 {
548     fib_path_cfg_flags_t pf = FIB_PATH_CFG_FLAG_NONE;
549
550     if (plf & FIB_PATH_LIST_FLAG_LOCAL)
551     {
552         pf |= FIB_PATH_CFG_FLAG_LOCAL;
553     }
554     if (plf & FIB_PATH_LIST_FLAG_DROP)
555     {
556         pf |= FIB_PATH_CFG_FLAG_DROP;
557     }
558     if (plf & FIB_PATH_LIST_FLAG_EXCLUSIVE)
559     {
560         pf |= FIB_PATH_CFG_FLAG_EXCLUSIVE;
561     }
562
563     return (pf);
564 }
565
566 static fib_path_list_flags_t
567 fib_path_list_flags_fixup (fib_path_list_flags_t flags)
568 {
569     /*
570      * we do no share drop nor exclusive path-lists
571      */
572     if (flags & FIB_PATH_LIST_FLAG_DROP ||
573         flags & FIB_PATH_LIST_FLAG_EXCLUSIVE)
574     {
575         flags &= ~FIB_PATH_LIST_FLAG_SHARED;
576     }
577
578     return (flags);
579 }
580
581 fib_node_index_t
582 fib_path_list_create (fib_path_list_flags_t flags,
583                       const fib_route_path_t *rpaths)
584 {
585     fib_node_index_t path_list_index, old_path_list_index;
586     fib_path_list_t *path_list;
587     int i;
588
589     flags = fib_path_list_flags_fixup(flags);
590     path_list = fib_path_list_alloc(&path_list_index);
591     path_list->fpl_flags = flags;
592     /*
593      * we'll assume for now all paths are the same next-hop protocol
594      */
595     path_list->fpl_nh_proto = rpaths[0].frp_proto;
596
597     vec_foreach_index(i, rpaths)
598     {
599         vec_add1(path_list->fpl_paths,
600                  fib_path_create(path_list_index,
601                                  path_list->fpl_nh_proto,
602                                  fib_path_list_flags_2_path_flags(flags),
603                                  &rpaths[i]));
604     }
605
606     /*
607      * If a shared path list is requested, consult the DB for a match
608      */
609     if (flags & FIB_PATH_LIST_FLAG_SHARED)
610     {
611         /*
612          * check for a matching path-list in the DB.
613          * If we find one then we can return the existing one and destroy the
614          * new one just created.
615          */
616         old_path_list_index = fib_path_list_db_find(path_list);
617         if (FIB_NODE_INDEX_INVALID != old_path_list_index)
618         {
619             fib_path_list_destroy(path_list);
620         
621             path_list_index = old_path_list_index;
622         }
623         else
624         {
625             /*
626              * if there was not a matching path-list, then this
627              * new one will need inserting into the DB and resolving.
628              */
629             fib_path_list_db_insert(path_list_index);
630             path_list = fib_path_list_resolve(path_list);
631         }
632     }
633     else
634     {
635         /*
636          * no shared path list requested. resolve and use the one
637          * just created.
638          */
639         path_list = fib_path_list_resolve(path_list);
640     }
641
642     return (path_list_index);
643 }
644
645 fib_node_index_t
646 fib_path_list_create_special (fib_protocol_t nh_proto,
647                               fib_path_list_flags_t flags,
648                               const dpo_id_t *dpo)
649 {
650     fib_node_index_t path_index, path_list_index;
651     fib_path_list_t *path_list;
652
653     path_list = fib_path_list_alloc(&path_list_index);
654     path_list->fpl_flags = flags;
655     path_list->fpl_nh_proto = nh_proto;
656
657     path_index =
658         fib_path_create_special(path_list_index,
659                                 path_list->fpl_nh_proto,
660                                 fib_path_list_flags_2_path_flags(flags),
661                                 dpo);
662     vec_add1(path_list->fpl_paths, path_index);
663
664     /*
665      * we don't share path-lists. we can do PIC on them so why bother.
666      */
667     path_list = fib_path_list_resolve(path_list);
668
669     return (path_list_index);
670 }
671
672 /*
673  * fib_path_list_copy_and_path_add
674  *
675  * Create a copy of a path-list and append one more path to it.
676  * The path-list returned could either have been newly created, or
677  * can be a shared path-list from the data-base.
678  */
679 fib_node_index_t
680 fib_path_list_copy_and_path_add (fib_node_index_t orig_path_list_index,
681                                  fib_path_list_flags_t flags,
682                                  const fib_route_path_t *rpaths)
683 {
684     fib_node_index_t path_index, path_list_index, *orig_path_index;
685     fib_path_list_t *path_list, *orig_path_list;
686     fib_node_index_t pi;
687
688     ASSERT(1 == vec_len(rpaths));
689
690     /*
691      * alloc the new list before we retrieve the old one, lest
692      * the alloc result in a realloc
693      */
694     path_list = fib_path_list_alloc(&path_list_index);
695
696     orig_path_list = fib_path_list_get(orig_path_list_index);
697
698     FIB_PATH_LIST_DBG(orig_path_list, "copy-add");
699
700     flags = fib_path_list_flags_fixup(flags);
701     path_list->fpl_flags = flags;
702     path_list->fpl_nh_proto = orig_path_list->fpl_nh_proto;
703     vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths));
704     pi = 0;
705
706     vec_foreach (orig_path_index, orig_path_list->fpl_paths)
707     {
708         path_index = fib_path_copy(*orig_path_index, path_list_index);
709         path_list->fpl_paths[pi++] = path_index;
710     }
711     path_index = fib_path_create(path_list_index,
712                                  path_list->fpl_nh_proto,
713                                  fib_path_list_flags_2_path_flags(flags),
714                                  rpaths);
715     path_list->fpl_paths[pi] = path_index;
716
717     /*
718      * we sort the paths since the key for the path-list is
719      * the description of the paths it contains. The paths need to
720      * be sorted else this description will differ.
721      */
722     vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
723
724     FIB_PATH_LIST_DBG(path_list, "path-added");
725
726     /*
727      * If a shared path list is requested, consult the DB for a match
728      */
729     if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
730     {
731         fib_node_index_t exist_path_list_index;
732         /*
733          * check for a matching path-list in the DB.
734          * If we find one then we can return the existing one and destroy the
735          * new one just created.
736          */
737         exist_path_list_index = fib_path_list_db_find(path_list);
738         if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
739         {
740             fib_path_list_destroy(path_list);
741         
742             path_list_index = exist_path_list_index;
743         }
744         else
745         {
746             /*
747              * if there was not a matching path-list, then this
748              * new one will need inserting into the DB and resolving.
749              */
750             fib_path_list_db_insert(path_list_index);
751
752             path_list = fib_path_list_resolve(path_list);
753         }
754     }
755     else
756     {
757         /*
758          * no shared path list requested. resolve and use the one
759          * just created.
760          */
761         path_list = fib_path_list_resolve(path_list);
762     }
763
764     return (path_list_index);
765 }
766
767 /*
768  * fib_path_list_copy_and_path_remove
769  *
770  * Copy the path-list excluding the path passed.
771  * If the path is the last one, then the index reurned will be invalid.
772  * i.e. the path-list is toast.
773  */
774 fib_node_index_t
775 fib_path_list_copy_and_path_remove (fib_node_index_t orig_path_list_index,
776                                     fib_path_list_flags_t flags,
777                                     const fib_route_path_t *rpaths)
778 {
779     fib_node_index_t path_index, *orig_path_index, path_list_index, tmp_path_index;
780     fib_path_list_t *path_list,  *orig_path_list;
781     fib_node_index_t pi;
782
783     ASSERT(1 == vec_len(rpaths));
784
785     path_list = fib_path_list_alloc(&path_list_index);
786
787     flags = fib_path_list_flags_fixup(flags);
788     orig_path_list = fib_path_list_get(orig_path_list_index);
789
790     FIB_PATH_LIST_DBG(orig_path_list, "copy-remove");
791
792     path_list->fpl_flags = flags;
793     path_list->fpl_nh_proto = orig_path_list->fpl_nh_proto;
794     /*
795      * allocate as many paths as we might need in one go, rather than
796      * using vec_add to do a few at a time.
797      */
798     if (vec_len(orig_path_list->fpl_paths) > 1)
799     {
800         vec_validate(path_list->fpl_paths, vec_len(orig_path_list->fpl_paths) - 2);
801     }
802     pi = 0;
803
804     /*
805      * create a representation of the path to be removed, so it
806      * can be used as a comparison object during the copy.
807      */
808     tmp_path_index = fib_path_create(path_list_index,
809                                      path_list->fpl_nh_proto,
810                                      fib_path_list_flags_2_path_flags(flags),
811                                      rpaths);
812
813     vec_foreach (orig_path_index, orig_path_list->fpl_paths)
814     {
815         if (0 != fib_path_cmp(tmp_path_index, *orig_path_index)) {
816             path_index = fib_path_copy(*orig_path_index, path_list_index);
817             if (pi < vec_len(path_list->fpl_paths))
818             {
819                 path_list->fpl_paths[pi++] = path_index;
820             }
821             else
822             {
823                 /*
824                  * this is the unlikely case that the path being
825                  * removed does not match one in the path-list, so
826                  * we end up with as many paths as we started with.
827                  * the paths vector was sized above with the expectation
828                  * that we would have 1 less.
829                  */
830                 vec_add1(path_list->fpl_paths, path_index);
831             }
832         }
833     }
834
835     /*
836      * done with the temporary now
837      */
838     fib_path_destroy(tmp_path_index);
839
840     /*
841      * if there are no paths, then the new path-list is aborted
842      */
843     if (0 == vec_len(path_list->fpl_paths)) {
844         FIB_PATH_LIST_DBG(path_list, "last-path-removed");
845
846         fib_path_list_destroy(path_list);
847
848         path_list_index = FIB_NODE_INDEX_INVALID;
849     } else {
850         /*
851          * we sort the paths since the key for the path-list is
852          * the description of the paths it contains. The paths need to
853          * be sorted else this description will differ.
854          */
855         vec_sort_with_function(path_list->fpl_paths, fib_path_cmp_for_sort);
856     
857         /*
858          * If a shared path list is requested, consult the DB for a match
859          */
860         if (path_list->fpl_flags & FIB_PATH_LIST_FLAG_SHARED)
861         {
862             fib_node_index_t exist_path_list_index;
863
864             /*
865              * check for a matching path-list in the DB.
866              * If we find one then we can return the existing one and destroy the
867              * new one just created.
868              */
869             exist_path_list_index = fib_path_list_db_find(path_list);
870             if (FIB_NODE_INDEX_INVALID != exist_path_list_index)
871             {
872                 fib_path_list_destroy(path_list);
873         
874                 path_list_index = exist_path_list_index;
875             }
876             else
877             {
878                 /*
879                  * if there was not a matching path-list, then this
880                  * new one will need inserting into the DB and resolving.
881                  */
882                 fib_path_list_db_insert(path_list_index);
883
884                 path_list = fib_path_list_resolve(path_list);
885             }
886         }
887         else
888         {
889             /*
890              * no shared path list requested. resolve and use the one
891              * just created.
892              */
893             path_list = fib_path_list_resolve(path_list);
894         }
895     }
896
897     return (path_list_index);
898 }
899
900 /*
901  * fib_path_list_contribute_forwarding
902  *
903  * Return the index of a load-balance that user of this path-list should
904  * use for forwarding
905  */
906 void
907 fib_path_list_contribute_forwarding (fib_node_index_t path_list_index,
908                                      fib_forward_chain_type_t type,
909                                      dpo_id_t *dpo)
910 {
911     fib_path_list_t *path_list;
912
913     path_list = fib_path_list_get(path_list_index);
914
915     fib_path_list_mk_lb(path_list, type, dpo);
916 }
917
918 /*
919  * fib_path_list_get_adj
920  *
921  * Return the index of a adjacency for the first path that user of this
922  * path-list should use for forwarding
923  */
924 adj_index_t
925 fib_path_list_get_adj (fib_node_index_t path_list_index,
926                        fib_forward_chain_type_t type)
927 {
928     fib_path_list_t *path_list;
929
930     path_list = fib_path_list_get(path_list_index);
931     return (fib_path_get_adj(path_list->fpl_paths[0]));
932 }
933
934 int
935 fib_path_list_recursive_loop_detect (fib_node_index_t path_list_index,
936                                      fib_node_index_t **entry_indicies)
937 {
938     fib_node_index_t *path_index;
939     int is_looped, list_looped;
940     fib_path_list_t *path_list;
941
942     list_looped = 0;
943     path_list = fib_path_list_get(path_list_index);
944
945     vec_foreach (path_index, path_list->fpl_paths)
946     {
947         fib_node_index_t *copy, **copy_ptr;
948
949         /*
950          * we need a copy of the nodes visited so that when we add entries
951          * we explore on the nth path and a looped is detected, those entries
952          * are not again searched for n+1 path and so finding a loop that does
953          * not exist.
954          */
955         copy = vec_dup(*entry_indicies);
956         copy_ptr = &copy;
957
958         is_looped  = fib_path_recursive_loop_detect(*path_index, copy_ptr);
959         list_looped += is_looped;
960     }
961
962     FIB_PATH_LIST_DBG(path_list, "loop-detect: eval:%d", eval);
963
964     if (list_looped)
965     {
966         path_list->fpl_flags |= FIB_PATH_LIST_FLAG_LOOPED;
967     }
968     else
969     {
970         path_list->fpl_flags &= ~FIB_PATH_LIST_FLAG_LOOPED;
971     }
972
973     return (list_looped);
974 }
975
976 u32
977 fib_path_list_child_add (fib_node_index_t path_list_index,
978                          fib_node_type_t child_type,
979                          fib_node_index_t child_index)
980 {
981     return (fib_node_child_add(FIB_NODE_TYPE_PATH_LIST,
982                                path_list_index,
983                                child_type,
984                                child_index));
985 }
986
987 void
988 fib_path_list_child_remove (fib_node_index_t path_list_index,
989                             u32 si)
990 {
991     fib_node_child_remove(FIB_NODE_TYPE_PATH_LIST,
992                           path_list_index,
993                           si);
994 }
995
996 void
997 fib_path_list_lock(fib_node_index_t path_list_index)
998 {
999     fib_path_list_t *path_list;
1000
1001     if (FIB_NODE_INDEX_INVALID != path_list_index)
1002     {
1003         path_list = fib_path_list_get(path_list_index);
1004
1005         fib_node_lock(&path_list->fpl_node);
1006         FIB_PATH_LIST_DBG(path_list, "lock");
1007     }
1008 }
1009
1010 void
1011 fib_path_list_unlock (fib_node_index_t path_list_index)
1012 {
1013     fib_path_list_t *path_list;
1014
1015     if (FIB_NODE_INDEX_INVALID != path_list_index)
1016     {
1017         path_list = fib_path_list_get(path_list_index);
1018         FIB_PATH_LIST_DBG(path_list, "unlock");
1019     
1020         fib_node_unlock(&path_list->fpl_node);
1021     }
1022 }
1023
1024 u32
1025 fib_path_list_pool_size (void)
1026 {
1027     return (pool_elts(fib_path_list_pool));    
1028 }
1029
1030 u32
1031 fib_path_list_db_size (void)
1032 {
1033     return (hash_elts(fib_path_list_db));
1034 }
1035
1036 void
1037 fib_path_list_walk (fib_node_index_t path_list_index,
1038                     fib_path_list_walk_fn_t func,
1039                     void *ctx)
1040 {
1041     fib_node_index_t *path_index;
1042     fib_path_list_t *path_list;
1043
1044     path_list = fib_path_list_get(path_list_index);
1045
1046     vec_foreach(path_index, path_list->fpl_paths)
1047     {
1048         if (!func(path_list_index, *path_index, ctx))
1049             break;
1050     }
1051 }
1052
1053
1054 void
1055 fib_path_list_module_init (void)
1056 {
1057     fib_node_register_type (FIB_NODE_TYPE_PATH_LIST, &fib_path_list_vft);
1058
1059     fib_path_list_db = hash_create2 (/* elts */ 0,
1060                                      /* user */ 0,
1061                                      /* value_bytes */ sizeof (fib_node_index_t),
1062                                      fib_path_list_db_hash_key_sum,
1063                                      fib_path_list_db_hash_key_equal,
1064                                      /* format pair/arg */
1065                                      0, 0);
1066 }
1067
1068 static clib_error_t *
1069 show_fib_path_list_command (vlib_main_t * vm,
1070                             unformat_input_t * input,
1071                             vlib_cli_command_t * cmd)
1072 {
1073     fib_path_list_t *path_list;
1074     fib_node_index_t pli;
1075
1076     if (unformat (input, "%d", &pli))
1077     {
1078         /*
1079          * show one in detail
1080          */
1081         if (!pool_is_free_index(fib_path_list_pool, pli))
1082         {
1083             path_list = fib_path_list_get(pli);
1084             u8 *s = fib_path_list_format(pli, NULL);
1085             s = format(s, "children:");
1086             s = fib_node_children_format(path_list->fpl_node.fn_children, s);
1087             vlib_cli_output (vm, "%s", s);
1088             vec_free(s);
1089         }
1090         else
1091         {
1092             vlib_cli_output (vm, "path list %d invalid", pli);
1093         }
1094     }
1095     else
1096     {
1097         /*
1098          * show all
1099          */
1100         vlib_cli_output (vm, "FIB Path Lists");
1101         pool_foreach(path_list, fib_path_list_pool,
1102         ({
1103             vlib_cli_output (vm, "%U", format_fib_path_list, path_list);
1104         }));
1105     }
1106     return (NULL);
1107 }
1108
1109 VLIB_CLI_COMMAND (show_fib_path_list, static) = {
1110   .path = "show fib path list",
1111   .function = show_fib_path_list_command,
1112   .short_help = "show fib path list",
1113 };