IP Flow Hash Config fixes
[vpp.git] / src / vnet / fib / mpls_fib.h
1 /*
2  * mpls_fib.h: The Label/MPLS FIB
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __MPLS_FIB_TABLE_H__
19 #define __MPLS_FIB_TABLE_H__
20
21 #include <vnet/vnet.h>
22 #include <vnet/mpls/mpls.h>
23 #include <vnet/fib/fib_types.h>
24 #include <vnet/dpo/dpo.h>
25 #include <vnet/mpls/mpls.h>
26 #include <vnet/fib/fib_table.h>
27
28 #define MPLS_FIB_DEFAULT_TABLE_ID 0
29
30 /**
31  * Type exposure is to allow the DP fast/inlined access
32  */
33 #define MPLS_FIB_KEY_SIZE 21
34 #define MPLS_FIB_DB_SIZE (1 << (MPLS_FIB_KEY_SIZE-1))
35
36 /**
37  * There are no options for controlling the MPLS flow hash
38  */
39 #define MPLS_FLOW_HASH_DEFAULT 0
40
41 typedef struct mpls_fib_t_
42 {
43   /**
44    * A hash table of entries. 21 bit key
45    * Hash table for reduced memory footprint
46    */
47   uword * mf_entries;
48
49   /**
50    * The load-balance indices keyed by 21 bit label+eos bit.
51    * A flat array for maximum lookup performace.
52    */
53   index_t mf_lbs[MPLS_FIB_DB_SIZE];
54 } mpls_fib_t;
55
56 static inline mpls_fib_t*
57 mpls_fib_get (fib_node_index_t index)
58 {
59     return (pool_elt_at_index(mpls_main.mpls_fibs, index));
60 }
61
62 extern u32 mpls_fib_table_find_or_create_and_lock(u32 table_id);
63 extern u32 mpls_fib_table_create_and_lock(void);
64 // extern mpls_fib_t * mpls_fib_find(u32 table_id);
65 extern u32 mpls_fib_index_from_table_id(u32 table_id);
66
67 extern u8 *format_mpls_fib_table_name(u8 * s, va_list * args);
68
69 extern fib_node_index_t mpls_fib_table_entry_add_from_ip_fib_entry (
70     u32 table_id,
71     mpls_label_t label,
72     mpls_eos_bit_t eos,
73     fib_node_index_t fib_entry_index);
74
75
76 extern fib_node_index_t mpls_fib_table_lookup(const mpls_fib_t *mf,
77                                               mpls_label_t label,
78                                               mpls_eos_bit_t eos);
79
80 extern void mpls_fib_table_entry_remove(mpls_fib_t *mf,
81                                         mpls_label_t label,
82                                         mpls_eos_bit_t eos);
83 extern void mpls_fib_table_entry_insert(mpls_fib_t *mf,
84                                         mpls_label_t label,
85                                         mpls_eos_bit_t eos,
86                                         fib_node_index_t fei);
87 extern void mpls_fib_table_destroy(u32 fib_index);
88
89
90 extern void mpls_fib_forwarding_table_update(mpls_fib_t *mf,
91                                              mpls_label_t label,
92                                              mpls_eos_bit_t eos,
93                                              const dpo_id_t *dpo);
94 extern void mpls_fib_forwarding_table_reset(mpls_fib_t *mf,
95                                             mpls_label_t label,
96                                             mpls_eos_bit_t eos);
97
98 /**
99  * @brief Walk all entries in a FIB table
100  * N.B: This is NOT safe to deletes. If you need to delete walk the whole
101  * table and store elements in a vector, then delete the elements
102  */
103 extern void mpls_fib_table_walk(mpls_fib_t *fib,
104                                 fib_table_walk_fn_t fn,
105                                 void *ctx);
106
107 /**
108  * @brief
109  *  Lookup a label and EOS bit in the MPLS_FIB table to retrieve the
110  *  load-balance index to be used for packet forwarding.
111  */
112 static inline index_t
113 mpls_fib_table_forwarding_lookup (u32 mpls_fib_index,
114                                   const mpls_unicast_header_t *hdr)
115 {
116     mpls_label_t label;
117     mpls_fib_t *mf;
118     u32 key;
119
120     label = clib_net_to_host_u32(hdr->label_exp_s_ttl);
121     key = (vnet_mpls_uc_get_label(label) << 1) | vnet_mpls_uc_get_s(label);
122
123     mf = mpls_fib_get(mpls_fib_index);
124
125     return (mf->mf_lbs[key]);
126 }
127
128 static inline u32
129 mpls_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
130 {
131     mpls_main_t *mm = &mpls_main;
132
133     ASSERT(vec_len(mm->fib_index_by_sw_if_index) > sw_if_index);
134
135     return (mm->fib_index_by_sw_if_index[sw_if_index]);
136 }
137
138 #endif