MTRIE Optimisations 2
[vpp.git] / src / vnet / fib / fib_table.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 <vlib/vlib.h>
17 #include <vnet/dpo/drop_dpo.h>
18
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/fib_entry_cover.h>
21 #include <vnet/fib/fib_internal.h>
22 #include <vnet/fib/ip4_fib.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/fib/mpls_fib.h>
25
26 fib_table_t *
27 fib_table_get (fib_node_index_t index,
28                fib_protocol_t proto)
29 {
30     switch (proto)
31     {
32     case FIB_PROTOCOL_IP4:
33         return (pool_elt_at_index(ip4_main.fibs, index));
34     case FIB_PROTOCOL_IP6:
35         return (pool_elt_at_index(ip6_main.fibs, index));
36     case FIB_PROTOCOL_MPLS:
37         return (pool_elt_at_index(mpls_main.fibs, index));
38     }
39     ASSERT(0);
40     return (NULL);
41 }
42
43 static inline fib_node_index_t
44 fib_table_lookup_i (fib_table_t *fib_table,
45                     const fib_prefix_t *prefix)
46 {
47     switch (prefix->fp_proto)
48     {
49     case FIB_PROTOCOL_IP4:
50         return (ip4_fib_table_lookup(ip4_fib_get(fib_table->ft_index),
51                                      &prefix->fp_addr.ip4,
52                                      prefix->fp_len));
53     case FIB_PROTOCOL_IP6:
54         return (ip6_fib_table_lookup(fib_table->ft_index,
55                                      &prefix->fp_addr.ip6,
56                                      prefix->fp_len));
57     case FIB_PROTOCOL_MPLS:
58         return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index),
59                                       prefix->fp_label,
60                                       prefix->fp_eos));
61     }
62     return (FIB_NODE_INDEX_INVALID);
63 }
64
65 fib_node_index_t
66 fib_table_lookup (u32 fib_index,
67                   const fib_prefix_t *prefix)
68 {
69     return (fib_table_lookup_i(fib_table_get(fib_index, prefix->fp_proto), prefix));
70 }
71
72 static inline fib_node_index_t
73 fib_table_lookup_exact_match_i (const fib_table_t *fib_table,
74                                 const fib_prefix_t *prefix)
75 {
76     switch (prefix->fp_proto)
77     {
78     case FIB_PROTOCOL_IP4:
79         return (ip4_fib_table_lookup_exact_match(ip4_fib_get(fib_table->ft_index),
80                                                  &prefix->fp_addr.ip4,
81                                                  prefix->fp_len));
82     case FIB_PROTOCOL_IP6:
83         return (ip6_fib_table_lookup_exact_match(fib_table->ft_index,
84                                                  &prefix->fp_addr.ip6,
85                                                  prefix->fp_len));
86     case FIB_PROTOCOL_MPLS:
87         return (mpls_fib_table_lookup(mpls_fib_get(fib_table->ft_index),
88                                       prefix->fp_label,
89                                       prefix->fp_eos));
90     }
91     return (FIB_NODE_INDEX_INVALID);
92 }
93
94 fib_node_index_t
95 fib_table_lookup_exact_match (u32 fib_index,
96                               const fib_prefix_t *prefix)
97 {
98     return (fib_table_lookup_exact_match_i(fib_table_get(fib_index,
99                                                          prefix->fp_proto),
100                                            prefix));
101 }
102
103 static fib_node_index_t
104 fib_table_get_less_specific_i (fib_table_t *fib_table,
105                                const fib_prefix_t *prefix)
106 {
107     fib_prefix_t pfx;
108
109     pfx = *prefix;
110
111     if (FIB_PROTOCOL_MPLS == pfx.fp_proto)
112     {
113         return (FIB_NODE_INDEX_INVALID);
114     }
115
116     /*
117      * in the absence of a tree structure for the table that allows for an O(1)
118      * parent get, a cheeky way to find the cover is to LPM for the prefix with
119      * mask-1.
120      * there should always be a cover, though it may be the default route. the
121      * default route's cover is the default route.
122      */
123     if (pfx.fp_len != 0) {
124         pfx.fp_len -= 1;
125     }
126
127     return (fib_table_lookup_i(fib_table, &pfx));    
128 }
129
130 fib_node_index_t
131 fib_table_get_less_specific (u32 fib_index,
132                              const fib_prefix_t *prefix)
133 {
134     return (fib_table_get_less_specific_i(fib_table_get(fib_index,
135                                                         prefix->fp_proto),
136                                           prefix));
137 }
138
139 static void
140 fib_table_entry_remove (fib_table_t *fib_table,
141                         const fib_prefix_t *prefix,
142                         fib_node_index_t fib_entry_index)
143 {
144     vlib_smp_unsafe_warning();
145
146     fib_table->ft_total_route_counts--;
147
148     switch (prefix->fp_proto)
149     {
150     case FIB_PROTOCOL_IP4:
151         ip4_fib_table_entry_remove(ip4_fib_get(fib_table->ft_index),
152                                    &prefix->fp_addr.ip4,
153                                    prefix->fp_len);
154         break;
155     case FIB_PROTOCOL_IP6:
156         ip6_fib_table_entry_remove(fib_table->ft_index,
157                                    &prefix->fp_addr.ip6,
158                                    prefix->fp_len);
159         break;
160     case FIB_PROTOCOL_MPLS:
161         mpls_fib_table_entry_remove(mpls_fib_get(fib_table->ft_index),
162                                     prefix->fp_label,
163                                     prefix->fp_eos);
164         break;
165     }
166
167     fib_entry_unlock(fib_entry_index);
168 }
169
170 static void
171 fib_table_post_insert_actions (fib_table_t *fib_table,
172                                const fib_prefix_t *prefix,
173                                fib_node_index_t fib_entry_index)
174 {
175     fib_node_index_t fib_entry_cover_index;
176
177     /*
178      * no cover relationships in the MPLS FIB
179      */
180     if (FIB_PROTOCOL_MPLS == prefix->fp_proto)
181         return;
182
183     /*
184      * find and inform the covering entry that a new more specific
185      * has been inserted beneath it
186      */
187     fib_entry_cover_index = fib_table_get_less_specific_i(fib_table, prefix);
188     /*
189      * the indicies are the same when the default route is first added
190      */
191     if (fib_entry_cover_index != fib_entry_index)
192     {
193         fib_entry_cover_change_notify(fib_entry_cover_index,
194                                       fib_entry_index);
195     }
196 }
197
198 static void
199 fib_table_entry_insert (fib_table_t *fib_table,
200                         const fib_prefix_t *prefix,
201                         fib_node_index_t fib_entry_index)
202 {
203     vlib_smp_unsafe_warning();
204
205     fib_entry_lock(fib_entry_index);
206     fib_table->ft_total_route_counts++;
207
208     switch (prefix->fp_proto)
209     {
210     case FIB_PROTOCOL_IP4:
211         ip4_fib_table_entry_insert(ip4_fib_get(fib_table->ft_index),
212                                    &prefix->fp_addr.ip4,
213                                    prefix->fp_len,
214                                    fib_entry_index);
215         break;
216     case FIB_PROTOCOL_IP6:
217         ip6_fib_table_entry_insert(fib_table->ft_index,
218                                    &prefix->fp_addr.ip6,
219                                    prefix->fp_len,
220                                    fib_entry_index);
221         break;
222     case FIB_PROTOCOL_MPLS:
223         mpls_fib_table_entry_insert(mpls_fib_get(fib_table->ft_index),
224                                     prefix->fp_label,
225                                     prefix->fp_eos,
226                                     fib_entry_index);
227         break;
228     }
229
230     fib_table_post_insert_actions(fib_table, prefix, fib_entry_index);
231 }
232
233 void
234 fib_table_fwding_dpo_update (u32 fib_index,
235                              const fib_prefix_t *prefix,
236                              const dpo_id_t *dpo)
237 {
238     vlib_smp_unsafe_warning();
239
240     switch (prefix->fp_proto)
241     {
242     case FIB_PROTOCOL_IP4:
243         return (ip4_fib_table_fwding_dpo_update(ip4_fib_get(fib_index),
244                                                 &prefix->fp_addr.ip4,
245                                                 prefix->fp_len,
246                                                 dpo));
247     case FIB_PROTOCOL_IP6:
248         return (ip6_fib_table_fwding_dpo_update(fib_index,
249                                                 &prefix->fp_addr.ip6,
250                                                 prefix->fp_len,
251                                                 dpo));
252     case FIB_PROTOCOL_MPLS:
253         return (mpls_fib_forwarding_table_update(mpls_fib_get(fib_index),
254                                                  prefix->fp_label,
255                                                  prefix->fp_eos,
256                                                  dpo));
257     }
258 }
259
260 void
261 fib_table_fwding_dpo_remove (u32 fib_index,
262                              const fib_prefix_t *prefix,
263                              const dpo_id_t *dpo)
264 {
265     vlib_smp_unsafe_warning();
266
267     switch (prefix->fp_proto)
268     {
269     case FIB_PROTOCOL_IP4:
270         return (ip4_fib_table_fwding_dpo_remove(ip4_fib_get(fib_index),
271                                                 &prefix->fp_addr.ip4,
272                                                 prefix->fp_len,
273                                                 dpo,
274                                                 fib_table_get_less_specific(fib_index,
275                                                                             prefix)));
276     case FIB_PROTOCOL_IP6:
277         return (ip6_fib_table_fwding_dpo_remove(fib_index,
278                                                 &prefix->fp_addr.ip6,
279                                                 prefix->fp_len,
280                                                 dpo));
281     case FIB_PROTOCOL_MPLS:
282         return (mpls_fib_forwarding_table_reset(mpls_fib_get(fib_index),
283                                                 prefix->fp_label,
284                                                 prefix->fp_eos));
285     }
286 }
287
288
289 fib_node_index_t
290 fib_table_entry_special_dpo_add (u32 fib_index,
291                                  const fib_prefix_t *prefix,
292                                  fib_source_t source,
293                                  fib_entry_flag_t flags,
294                                  const dpo_id_t *dpo)
295 {
296     fib_node_index_t fib_entry_index;
297     fib_table_t *fib_table;
298
299     fib_table = fib_table_get(fib_index, prefix->fp_proto);
300     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
301
302     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
303     {
304         fib_entry_index = fib_entry_create_special(fib_index, prefix,
305                                                    source, flags,
306                                                    dpo);
307
308         fib_table_entry_insert(fib_table, prefix, fib_entry_index);
309         fib_table->ft_src_route_counts[source]++;
310     }
311     else
312     {
313         int was_sourced;
314
315         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
316         fib_entry_special_add(fib_entry_index, source, flags, dpo);
317
318         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
319         {
320             fib_table->ft_src_route_counts[source]++;
321         }
322     }
323
324
325     return (fib_entry_index);
326 }
327
328 fib_node_index_t
329 fib_table_entry_special_dpo_update (u32 fib_index,
330                                     const fib_prefix_t *prefix,
331                                     fib_source_t source,
332                                     fib_entry_flag_t flags,
333                                     const dpo_id_t *dpo)
334 {
335     fib_node_index_t fib_entry_index;
336     fib_table_t *fib_table;
337
338     fib_table = fib_table_get(fib_index, prefix->fp_proto);
339     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
340
341     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
342     {
343         fib_entry_index = fib_entry_create_special(fib_index, prefix,
344                                                    source, flags,
345                                                    dpo);
346
347         fib_table_entry_insert(fib_table, prefix, fib_entry_index);
348         fib_table->ft_src_route_counts[source]++;
349     }
350     else
351     {
352         int was_sourced;
353
354         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
355
356         if (was_sourced)
357             fib_entry_special_update(fib_entry_index, source, flags, dpo);
358         else
359             fib_entry_special_add(fib_entry_index, source, flags, dpo);
360
361         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
362         {
363             fib_table->ft_src_route_counts[source]++;
364         }
365     }
366
367     return (fib_entry_index);
368 }
369
370 fib_node_index_t
371 fib_table_entry_special_add (u32 fib_index,
372                              const fib_prefix_t *prefix,
373                              fib_source_t source,
374                              fib_entry_flag_t flags,
375                              adj_index_t adj_index)
376 {
377     fib_node_index_t fib_entry_index;
378     dpo_id_t tmp_dpo = DPO_INVALID;
379
380     if (ADJ_INDEX_INVALID != adj_index)
381     {
382         dpo_set(&tmp_dpo,
383                 DPO_ADJACENCY,
384                 FIB_PROTOCOL_MAX,
385                 adj_index);
386     }
387     else
388     {
389         dpo_copy(&tmp_dpo, drop_dpo_get(fib_proto_to_dpo(prefix->fp_proto)));
390     }
391  
392     fib_entry_index = fib_table_entry_special_dpo_add(fib_index, prefix, source,
393                                                       flags, &tmp_dpo);
394
395     dpo_unlock(&tmp_dpo);
396
397     return (fib_entry_index);
398 }
399
400 void
401 fib_table_entry_special_remove (u32 fib_index,
402                                 const fib_prefix_t *prefix,
403                                 fib_source_t source)
404 {
405     /*
406      * 1 is it present
407      *   yes => remove source
408      *    2 - is it still sourced?
409      *      no => cover walk
410      */
411     fib_node_index_t fib_entry_index;
412     fib_table_t *fib_table;
413
414     fib_table = fib_table_get(fib_index, prefix->fp_proto);
415     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
416
417     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
418     {
419         /*
420          * removing an etry that does not exist. i'll allow it.
421          */
422     }
423     else
424     {
425         fib_entry_src_flag_t src_flag;
426         int was_sourced;
427
428         /*
429          * don't nobody go nowhere
430          */
431         fib_entry_lock(fib_entry_index);
432         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
433
434         src_flag = fib_entry_special_remove(fib_entry_index, source);
435
436         if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
437         {
438             /*
439              * last source gone. remove from the table
440              */
441             fib_table_entry_remove(fib_table, prefix, fib_entry_index);
442
443             /*
444              * now the entry is no longer in the table, we can
445              * inform the entries that it covers to re-calculate their cover
446              */
447             fib_entry_cover_change_notify(fib_entry_index,
448                                           FIB_NODE_INDEX_INVALID);
449         }
450         /*
451          * else
452          *   still has sources, leave it be.
453          */
454         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
455         {
456             fib_table->ft_src_route_counts[source]--;
457         }
458
459         fib_entry_unlock(fib_entry_index);
460     }
461 }
462
463 /**
464  * fib_table_route_path_fixup
465  *
466  * Convert attached hosts to attached next-hops.
467  * 
468  * This special case is required because an attached path will link to a
469  * glean, and the FIB entry will have the interface or API/CLI source. When
470  * the ARP/ND process is completes then that source (which will provide a
471  * complete adjacency) will be lower priority and so the FIB entry will
472  * remain linked to a glean and traffic will never reach the hosts. For
473  * an ATTAHCED_HOST path we can link the path directly to the [incomplete]
474  * adjacency.
475  */
476 static void
477 fib_table_route_path_fixup (const fib_prefix_t *prefix,
478                             fib_route_path_t *path)
479 {
480     if (fib_prefix_is_host(prefix) &&
481         ip46_address_is_zero(&path->frp_addr) &&
482         path->frp_sw_if_index != ~0)
483     {
484         path->frp_addr = prefix->fp_addr;
485         path->frp_flags |= FIB_ROUTE_PATH_ATTACHED;
486     }
487 }                 
488
489 fib_node_index_t
490 fib_table_entry_path_add (u32 fib_index,
491                           const fib_prefix_t *prefix,
492                           fib_source_t source,
493                           fib_entry_flag_t flags,
494                           fib_protocol_t next_hop_proto,
495                           const ip46_address_t *next_hop,
496                           u32 next_hop_sw_if_index,
497                           u32 next_hop_fib_index,
498                           u32 next_hop_weight,
499                           mpls_label_t *next_hop_labels,
500                           fib_route_path_flags_t path_flags)
501 {
502     fib_route_path_t path = {
503         .frp_proto = next_hop_proto,
504         .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
505         .frp_sw_if_index = next_hop_sw_if_index,
506         .frp_fib_index = next_hop_fib_index,
507         .frp_weight = next_hop_weight,
508         .frp_flags = path_flags,
509         .frp_label_stack = next_hop_labels,
510     };
511     fib_node_index_t fib_entry_index;
512     fib_route_path_t *paths = NULL;
513
514     vec_add1(paths, path);
515
516     fib_entry_index = fib_table_entry_path_add2(fib_index, prefix,
517                                                 source, flags, paths);
518
519     vec_free(paths);
520     return (fib_entry_index);
521 }
522
523 fib_node_index_t
524 fib_table_entry_path_add2 (u32 fib_index,
525                            const fib_prefix_t *prefix,
526                            fib_source_t source,
527                            fib_entry_flag_t flags,
528                            fib_route_path_t *rpath)
529 {
530     fib_node_index_t fib_entry_index;
531     fib_table_t *fib_table;
532     u32 ii;
533
534     fib_table = fib_table_get(fib_index, prefix->fp_proto);
535     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
536
537     for (ii = 0; ii < vec_len(rpath); ii++)
538     {
539         fib_table_route_path_fixup(prefix, &rpath[ii]);
540     }
541
542     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
543     {
544         fib_entry_index = fib_entry_create(fib_index, prefix,
545                                            source, flags,
546                                            rpath);
547
548         fib_table_entry_insert(fib_table, prefix, fib_entry_index);
549         fib_table->ft_src_route_counts[source]++;
550     }
551     else
552     {
553         int was_sourced;
554
555         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
556         fib_entry_path_add(fib_entry_index, source, flags, rpath);;
557
558         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
559         {
560             fib_table->ft_src_route_counts[source]++;
561         }
562     }
563
564     return (fib_entry_index);
565 }
566
567 void
568 fib_table_entry_path_remove2 (u32 fib_index,
569                               const fib_prefix_t *prefix,
570                               fib_source_t source,
571                               fib_route_path_t *rpath)
572 {
573     /*
574      * 1 is it present
575      *   yes => remove source
576      *    2 - is it still sourced?
577      *      no => cover walk
578      */
579     fib_node_index_t fib_entry_index;
580     fib_table_t *fib_table;
581     u32 ii;
582
583     fib_table = fib_table_get(fib_index, prefix->fp_proto);
584     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
585
586     for (ii = 0; ii < vec_len(rpath); ii++)
587     {
588         fib_table_route_path_fixup(prefix, &rpath[ii]);
589     }
590
591     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
592     {
593         /*
594          * removing an etry that does not exist. i'll allow it.
595          */
596     }
597     else
598     {
599         fib_entry_src_flag_t src_flag;
600         int was_sourced;
601
602         /*
603          * don't nobody go nowhere
604          */
605         fib_entry_lock(fib_entry_index);
606         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
607
608         src_flag = fib_entry_path_remove(fib_entry_index, source, rpath);
609
610         if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
611         {
612             /*
613              * last source gone. remove from the table
614              */
615             fib_table_entry_remove(fib_table, prefix, fib_entry_index);
616
617             /*
618              * now the entry is no longer in the table, we can
619              * inform the entries that it covers to re-calculate their cover
620              */
621             fib_entry_cover_change_notify(fib_entry_index,
622                                           FIB_NODE_INDEX_INVALID);
623         }
624         /*
625          * else
626          *   still has sources, leave it be.
627          */
628         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
629         {
630             fib_table->ft_src_route_counts[source]--;
631         }
632
633         fib_entry_unlock(fib_entry_index);
634     }
635 }
636
637 void
638 fib_table_entry_path_remove (u32 fib_index,
639                              const fib_prefix_t *prefix,
640                              fib_source_t source,
641                              fib_protocol_t next_hop_proto,
642                              const ip46_address_t *next_hop,
643                              u32 next_hop_sw_if_index,
644                              u32 next_hop_fib_index,
645                              u32 next_hop_weight,
646                              fib_route_path_flags_t path_flags)
647 {
648     /*
649      * 1 is it present
650      *   yes => remove source
651      *    2 - is it still sourced?
652      *      no => cover walk
653      */
654     fib_route_path_t path = {
655         .frp_proto = next_hop_proto,
656         .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
657         .frp_sw_if_index = next_hop_sw_if_index,
658         .frp_fib_index = next_hop_fib_index,
659         .frp_weight = next_hop_weight,
660         .frp_flags = path_flags,
661     };
662     fib_route_path_t *paths = NULL;
663
664     fib_table_route_path_fixup(prefix, &path);
665     vec_add1(paths, path);
666
667     fib_table_entry_path_remove2(fib_index, prefix, source, paths);
668
669     vec_free(paths);
670 }
671
672 static int
673 fib_route_path_cmp_for_sort (void * v1,
674                              void * v2)
675 {
676     return (fib_route_path_cmp(v1, v2));
677 }
678
679 fib_node_index_t
680 fib_table_entry_update (u32 fib_index,
681                         const fib_prefix_t *prefix,
682                         fib_source_t source,
683                         fib_entry_flag_t flags,
684                         fib_route_path_t *paths)
685 {
686     fib_node_index_t fib_entry_index;
687     fib_table_t *fib_table;
688     u32 ii;
689
690     fib_table = fib_table_get(fib_index, prefix->fp_proto);
691     fib_entry_index = fib_table_lookup_exact_match_i(fib_table, prefix);
692
693     for (ii = 0; ii < vec_len(paths); ii++)
694     {
695         fib_table_route_path_fixup(prefix, &paths[ii]);
696     }
697     /*
698      * sort the paths provided by the control plane. this means
699      * the paths and the extension on the entry will be sorted.
700      */
701     vec_sort_with_function(paths, fib_route_path_cmp_for_sort);
702
703     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
704     {
705         fib_entry_index = fib_entry_create(fib_index, prefix,
706                                            source, flags,
707                                            paths);
708
709         fib_table_entry_insert(fib_table, prefix, fib_entry_index);
710         fib_table->ft_src_route_counts[source]++;
711     }
712     else
713     {
714         int was_sourced;
715
716         was_sourced = fib_entry_is_sourced(fib_entry_index, source);
717         fib_entry_update(fib_entry_index, source, flags, paths);
718
719         if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
720         {
721             fib_table->ft_src_route_counts[source]++;
722         }
723     }
724
725     return (fib_entry_index);
726 }
727
728 fib_node_index_t
729 fib_table_entry_update_one_path (u32 fib_index,
730                                  const fib_prefix_t *prefix,
731                                  fib_source_t source,
732                                  fib_entry_flag_t flags,
733                                  fib_protocol_t next_hop_proto,
734                                  const ip46_address_t *next_hop,
735                                  u32 next_hop_sw_if_index,
736                                  u32 next_hop_fib_index,
737                                  u32 next_hop_weight,
738                                  mpls_label_t *next_hop_labels,
739                                  fib_route_path_flags_t path_flags)
740 {
741     fib_node_index_t fib_entry_index;
742     fib_route_path_t path = {
743         .frp_proto = next_hop_proto,
744         .frp_addr = (NULL == next_hop? zero_addr : *next_hop),
745         .frp_sw_if_index = next_hop_sw_if_index,
746         .frp_fib_index = next_hop_fib_index,
747         .frp_weight = next_hop_weight,
748         .frp_flags = path_flags,
749         .frp_label_stack = next_hop_labels,
750     };
751     fib_route_path_t *paths = NULL;
752
753     fib_table_route_path_fixup(prefix, &path);
754     vec_add1(paths, path);
755
756     fib_entry_index = 
757         fib_table_entry_update(fib_index, prefix, source, flags, paths);
758
759     vec_free(paths);
760
761     return (fib_entry_index);
762 }
763
764 static void
765 fib_table_entry_delete_i (u32 fib_index,
766                           fib_node_index_t fib_entry_index,
767                           const fib_prefix_t *prefix,
768                           fib_source_t source)
769 {
770     fib_entry_src_flag_t src_flag;
771     fib_table_t *fib_table;
772     int was_sourced;
773
774     fib_table = fib_table_get(fib_index, prefix->fp_proto);
775     was_sourced = fib_entry_is_sourced(fib_entry_index, source);
776
777     /*
778      * don't nobody go nowhere
779      */
780     fib_entry_lock(fib_entry_index);
781
782     src_flag = fib_entry_delete(fib_entry_index, source);
783
784     if (!(FIB_ENTRY_SRC_FLAG_ADDED & src_flag))
785     {
786         /*
787          * last source gone. remove from the table
788          */
789         fib_table_entry_remove(fib_table, prefix, fib_entry_index);
790
791         /*
792          * now the entry is no longer in the table, we can
793          * inform the entries that it covers to re-calculate their cover
794          */
795         fib_entry_cover_change_notify(fib_entry_index,
796                                       FIB_NODE_INDEX_INVALID);
797     }
798     /*
799      * else
800      *   still has sources, leave it be.
801      */
802     if (was_sourced != fib_entry_is_sourced(fib_entry_index, source))
803     {
804         fib_table->ft_src_route_counts[source]--;
805     }
806
807     fib_entry_unlock(fib_entry_index);
808 }
809
810 void
811 fib_table_entry_delete (u32 fib_index,
812                         const fib_prefix_t *prefix,
813                         fib_source_t source)
814 {
815     fib_node_index_t fib_entry_index;
816
817     fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
818
819     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
820     {
821         /*
822          * removing an etry that does not exist.
823          * i'll allow it, but i won't like it.
824          */
825         clib_warning("%U not in FIB", format_fib_prefix, prefix);
826     }
827     else
828     {
829         fib_table_entry_delete_i(fib_index, fib_entry_index, prefix, source);
830     }
831 }
832
833 void
834 fib_table_entry_delete_index (fib_node_index_t fib_entry_index,
835                               fib_source_t source)
836 {
837     fib_prefix_t prefix;
838
839     fib_entry_get_prefix(fib_entry_index, &prefix);
840
841     fib_table_entry_delete_i(fib_entry_get_fib_index(fib_entry_index),
842                              fib_entry_index, &prefix, source);
843 }
844
845 fib_node_index_t
846 fib_table_entry_local_label_add (u32 fib_index,
847                                  const fib_prefix_t *prefix,
848                                  mpls_label_t label)
849 {
850     fib_node_index_t fib_entry_index;
851  
852     fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
853
854     if (FIB_NODE_INDEX_INVALID == fib_entry_index ||
855         !fib_entry_is_sourced(fib_entry_index, FIB_SOURCE_MPLS))
856     {
857         /*
858          * only source the prefix once. this allows the label change
859          * operation to work
860          */
861         fib_entry_index = fib_table_entry_special_dpo_add(fib_index, prefix,
862                                                           FIB_SOURCE_MPLS,
863                                                           FIB_ENTRY_FLAG_NONE,
864                                                           NULL);
865     }
866
867     fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &label);
868
869     return (fib_entry_index);
870 }
871
872 void
873 fib_table_entry_local_label_remove (u32 fib_index,
874                                     const fib_prefix_t *prefix,
875                                     mpls_label_t label)
876 {
877     fib_node_index_t fib_entry_index;
878     const void *data;
879     mpls_label_t pl;
880
881     fib_entry_index = fib_table_lookup_exact_match(fib_index, prefix);
882
883     if (FIB_NODE_INDEX_INVALID == fib_entry_index)
884         return;
885
886     data = fib_entry_get_source_data(fib_entry_index, FIB_SOURCE_MPLS);
887
888     if (NULL == data)
889         return;
890
891     pl = *(mpls_label_t*)data;
892
893     if (pl != label)
894         return;
895
896     pl = MPLS_LABEL_INVALID;
897
898     fib_entry_set_source_data(fib_entry_index, FIB_SOURCE_MPLS, &pl);
899     fib_table_entry_special_remove(fib_index,
900                                    prefix,
901                                    FIB_SOURCE_MPLS);
902 }
903
904 u32
905 fib_table_get_index_for_sw_if_index (fib_protocol_t proto,
906                                      u32 sw_if_index)
907 {
908     switch (proto)
909     {
910     case FIB_PROTOCOL_IP4:
911         return (ip4_fib_table_get_index_for_sw_if_index(sw_if_index));
912     case FIB_PROTOCOL_IP6:
913         return (ip6_fib_table_get_index_for_sw_if_index(sw_if_index));
914     case FIB_PROTOCOL_MPLS:
915         return (mpls_fib_table_get_index_for_sw_if_index(sw_if_index));
916     }
917     return (~0);
918 }
919
920 flow_hash_config_t
921 fib_table_get_flow_hash_config (u32 fib_index,
922                                 fib_protocol_t proto)
923 {
924     switch (proto)
925     {
926     case FIB_PROTOCOL_IP4:
927         return (ip4_fib_table_get_flow_hash_config(fib_index));
928     case FIB_PROTOCOL_IP6:
929         return (ip6_fib_table_get_flow_hash_config(fib_index));
930     case FIB_PROTOCOL_MPLS:
931         return (mpls_fib_table_get_flow_hash_config(fib_index));
932     }
933     return (0);
934 }
935
936
937 u32
938 fib_table_get_table_id_for_sw_if_index (fib_protocol_t proto,
939                                         u32 sw_if_index)
940 {
941     fib_table_t *fib_table;
942
943     fib_table = fib_table_get(fib_table_get_index_for_sw_if_index(
944                                   proto, sw_if_index),
945                               proto);
946
947     return ((NULL != fib_table ? fib_table->ft_table_id : ~0));
948 }
949
950 u32
951 fib_table_find (fib_protocol_t proto,
952                 u32 table_id)
953 {
954     switch (proto)
955     {
956     case FIB_PROTOCOL_IP4:
957         return (ip4_fib_index_from_table_id(table_id));
958     case FIB_PROTOCOL_IP6:
959         return (ip6_fib_index_from_table_id(table_id));
960     case FIB_PROTOCOL_MPLS:
961         return (mpls_fib_index_from_table_id(table_id));
962     }
963     return (~0);
964 }
965
966 u32
967 fib_table_find_or_create_and_lock (fib_protocol_t proto,
968                                    u32 table_id)
969 {
970     fib_table_t *fib_table;
971     fib_node_index_t fi;
972
973     switch (proto)
974     {
975     case FIB_PROTOCOL_IP4:
976         fi = ip4_fib_table_find_or_create_and_lock(table_id);
977         break;
978     case FIB_PROTOCOL_IP6:
979         fi = ip6_fib_table_find_or_create_and_lock(table_id);
980         break;
981     case FIB_PROTOCOL_MPLS:
982         fi = mpls_fib_table_find_or_create_and_lock(table_id);
983         break;
984     default:
985         return (~0);        
986     }
987
988     fib_table = fib_table_get(fi, proto);
989
990     fib_table->ft_desc = format(NULL, "%U-VRF:%d",
991                                 format_fib_protocol, proto,
992                                 table_id);
993
994     return (fi);
995 }
996
997 u32
998 fib_table_create_and_lock (fib_protocol_t proto,
999                            const char *const fmt,
1000                            ...)
1001 {
1002     fib_table_t *fib_table;
1003     fib_node_index_t fi;
1004     va_list ap;
1005
1006     va_start(ap, fmt);
1007
1008     switch (proto)
1009     {
1010     case FIB_PROTOCOL_IP4:
1011         fi = ip4_fib_table_create_and_lock();
1012         break;
1013     case FIB_PROTOCOL_IP6:
1014         fi = ip6_fib_table_create_and_lock();
1015         break;
1016      case FIB_PROTOCOL_MPLS:
1017         fi = mpls_fib_table_create_and_lock();
1018         break;
1019    default:
1020         return (~0);        
1021     }
1022
1023     fib_table = fib_table_get(fi, proto);
1024
1025     fib_table->ft_desc = va_format(fib_table->ft_desc, fmt, &ap);
1026
1027     va_end(ap);
1028     return (fi);
1029 }
1030
1031 static void
1032 fib_table_destroy (fib_table_t *fib_table)
1033 {
1034     vec_free(fib_table->ft_desc);
1035
1036     switch (fib_table->ft_proto)
1037     {
1038     case FIB_PROTOCOL_IP4:
1039         ip4_fib_table_destroy(fib_table->ft_index);
1040         break;
1041     case FIB_PROTOCOL_IP6:
1042         ip6_fib_table_destroy(fib_table->ft_index);
1043         break;
1044     case FIB_PROTOCOL_MPLS:
1045         mpls_fib_table_destroy(fib_table->ft_index);
1046         break;
1047     }
1048 }
1049
1050 void
1051 fib_table_walk (u32 fib_index,
1052                 fib_protocol_t proto,
1053                 fib_table_walk_fn_t fn,
1054                 void *ctx)
1055 {
1056     switch (proto)
1057     {
1058     case FIB_PROTOCOL_IP4:
1059         ip4_fib_table_walk(ip4_fib_get(fib_index), fn, ctx);
1060         break;
1061     case FIB_PROTOCOL_IP6:
1062         ip6_fib_table_walk(fib_index, fn, ctx);
1063         break;
1064     case FIB_PROTOCOL_MPLS:
1065         mpls_fib_table_walk(mpls_fib_get(fib_index), fn, ctx);
1066         break;
1067     }
1068 }
1069
1070 void
1071 fib_table_unlock (u32 fib_index,
1072                   fib_protocol_t proto)
1073 {
1074     fib_table_t *fib_table;
1075
1076     fib_table = fib_table_get(fib_index, proto);
1077     fib_table->ft_locks--;
1078
1079     if (0 == fib_table->ft_locks)
1080     {
1081         fib_table_destroy(fib_table);
1082     }
1083 }
1084 void
1085 fib_table_lock (u32 fib_index,
1086                 fib_protocol_t proto)
1087 {
1088     fib_table_t *fib_table;
1089
1090     fib_table = fib_table_get(fib_index, proto);
1091     fib_table->ft_locks++;
1092 }
1093
1094 u32
1095 fib_table_get_num_entries (u32 fib_index,
1096                            fib_protocol_t proto,
1097                            fib_source_t source)
1098 {
1099     fib_table_t *fib_table;
1100
1101     fib_table = fib_table_get(fib_index, proto);
1102
1103     return (fib_table->ft_src_route_counts[source]);
1104 }
1105
1106 u8*
1107 format_fib_table_name (u8* s, va_list ap)
1108 {
1109     fib_node_index_t fib_index = va_arg(ap, fib_node_index_t);
1110     fib_protocol_t proto = va_arg(ap, int); // int promotion
1111     fib_table_t *fib_table;
1112
1113     fib_table = fib_table_get(fib_index, proto);
1114
1115     s = format(s, "%v", fib_table->ft_desc);
1116
1117     return (s);
1118 }
1119
1120 /**
1121  * @brief Table flush context. Store the indicies of matching FIB entries
1122  * that need to be removed.
1123  */
1124 typedef struct fib_table_flush_ctx_t_
1125 {
1126     /**
1127      * The list of entries to flush
1128      */
1129     fib_node_index_t *ftf_entries;
1130
1131     /**
1132      * The source we are flushing
1133      */
1134     fib_source_t ftf_source;
1135 } fib_table_flush_ctx_t;
1136
1137 static int
1138 fib_table_flush_cb (fib_node_index_t fib_entry_index,
1139                     void *arg)
1140 {
1141     fib_table_flush_ctx_t *ctx = arg;
1142
1143     if (fib_entry_is_sourced(fib_entry_index, ctx->ftf_source))
1144     {
1145         vec_add1(ctx->ftf_entries, fib_entry_index);
1146     }
1147     return (1);
1148 }
1149
1150
1151 void
1152 fib_table_flush (u32 fib_index,
1153                  fib_protocol_t proto,
1154                  fib_source_t source)
1155 {
1156     fib_node_index_t *fib_entry_index;
1157     fib_table_flush_ctx_t ctx = {
1158         .ftf_entries = NULL,
1159         .ftf_source = source,
1160     };
1161
1162     fib_table_walk(fib_index, proto,
1163                    fib_table_flush_cb,
1164                    &ctx);
1165
1166     vec_foreach(fib_entry_index, ctx.ftf_entries)
1167     {
1168         fib_table_entry_delete_index(*fib_entry_index, source);
1169     }
1170
1171     vec_free(ctx.ftf_entries);
1172 }