FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
ip4_mtrie.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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  * ip/ip4_fib.h: ip4 mtrie fib
17  *
18  * Copyright (c) 2012 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #ifndef included_ip_ip4_fib_h
41 #define included_ip_ip4_fib_h
42 
43 #include <vppinfra/cache.h>
44 #include <vppinfra/vector.h>
45 #include <vnet/ip/lookup.h>
46 #include <vnet/ip/ip4_packet.h> /* for ip4_address_t */
47 
48 /* ip4 fib leafs: 4 ply 8-8-8-8 mtrie.
49  1 + 2*adj_index for terminal leaves.
50  0 + 2*next_ply_index for non-terminals, i.e. PLYs
51  1 => empty (adjacency index of zero is special miss adjacency). */
53 
54 #define IP4_MTRIE_LEAF_EMPTY (1 + 2 * 0)
55 
56 /**
57  * @brief the 16 way stride that is the top PLY of the mtrie
58  * We do not maintain the count of 'real' leaves in this PLY, since
59  * it is never removed. The FIB will destroy the mtrie and the ply once
60  * the FIB is destroyed.
61  */
62 #define PLY_16_SIZE (1<<16)
63 typedef struct ip4_mtrie_16_ply_t_
64 {
65  /**
66  * The leaves/slots/buckets to be filed with leafs
67  */
68  union
69  {
71 
72 #ifdef CLIB_HAVE_VEC128
73  u32x4 leaves_as_u32x4[PLY_16_SIZE / 4];
74 #endif
75  };
76 
77  /**
78  * Prefix length for terminal leaves.
79  */
82 
83 /**
84  * @brief One ply of the 4 ply mtrie fib.
85  */
86 typedef struct ip4_mtrie_8_ply_t_
87 {
88  /**
89  * The leaves/slots/buckets to be filed with leafs
90  */
91  union
92  {
94 
95 #ifdef CLIB_HAVE_VEC128
96  u32x4 leaves_as_u32x4[256 / 4];
97 #endif
98  };
99 
100  /**
101  * Prefix length for leaves/ply.
102  */
104 
105  /**
106  * Number of non-empty leafs (whether terminal or not).
107  */
109 
110  /**
111  * The length of the ply's covering prefix. Also a measure of its depth
112  * If a leaf in a slot has a mask length longer than this then it is
113  * 'non-empty'. Otherwise it is the value of the cover.
114  */
116 
117  /* Pad to cache line boundary. */
118  u8 pad[CLIB_CACHE_LINE_BYTES - 2 * sizeof (i32)];
120 
122  "IP4 Mtrie ply cache line");
123 
124 /**
125  * @brief The mutiway-TRIE with a 16-8-8 stride.
126  * There is no data associated with the mtrie apart from the top PLY
127  */
128 typedef struct
129 {
130  /**
131  * Embed the PLY with the mtrie struct. This means that the Data-plane
132  * 'get me the mtrie' returns the first ply, and not an indirect 'pointer'
133  * to it. therefore no cacheline misses in the data-path.
134  */
137 
138 /**
139  * @brief The mutiway-TRIE with a 8-8-8-8 stride.
140  * There is no data associated with the mtrie apart from the top PLY
141  */
142 typedef struct
143 {
144  /* pool index of the root ply */
146 } ip4_mtrie_8_t;
147 
148 /**
149  * @brief Initialise an mtrie
150  */
153 
154 /**
155  * @brief Free an mtrie, It must be empty when free'd
156  */
159 
160 /**
161  * @brief Add a route/entry to the mtrie
162  */
164  const ip4_address_t *dst_address,
165  u32 dst_address_length, u32 adj_index);
166 void ip4_mtrie_8_route_add (ip4_mtrie_8_t *m, const ip4_address_t *dst_address,
167  u32 dst_address_length, u32 adj_index);
168 
169 /**
170  * @brief remove a route/entry to the mtrie
171  */
173  const ip4_address_t *dst_address,
174  u32 dst_address_length, u32 adj_index,
175  u32 cover_address_length, u32 cover_adj_index);
176 void ip4_mtrie_8_route_del (ip4_mtrie_8_t *m, const ip4_address_t *dst_address,
177  u32 dst_address_length, u32 adj_index,
178  u32 cover_address_length, u32 cover_adj_index);
179 
180 /**
181  * @brief return the memory used by the table
182  */
185 
186 /**
187  * @brief Format/display the contents of the mtrie
188  */
191 
192 /**
193  * @brief A global pool of 8bit stride plys
194  */
196 
197 /**
198  * Is the leaf terminal (i.e. an LB index) or non-terminal (i.e. a PLY index)
199  */
202 {
203  return n & 1;
204 }
205 
206 /**
207  * From the stored slot value extract the LB index value
208  */
211 {
213  return n >> 1;
214 }
215 
216 /**
217  * @brief Lookup step. Processes 1 byte of 4 byte ip4 address.
218  */
221  const ip4_address_t *dst_address,
222  u32 dst_address_byte_index)
223 {
224  ip4_mtrie_8_ply_t *ply;
225 
226  uword current_is_terminal = ip4_mtrie_leaf_is_terminal (current_leaf);
227 
228  if (!current_is_terminal)
229  {
230  ply = ip4_ply_pool + (current_leaf >> 1);
231  return (ply->leaves[dst_address->as_u8[dst_address_byte_index]]);
232  }
233 
234  return current_leaf;
235 }
236 
237 /**
238  * @brief Lookup step number 1. Processes 2 bytes of 4 byte ip4 address.
239  */
242  const ip4_address_t *dst_address)
243 {
244  ip4_mtrie_leaf_t next_leaf;
245 
246  next_leaf = m->root_ply.leaves[dst_address->as_u16[0]];
247 
248  return next_leaf;
249 }
250 
253  const ip4_address_t *dst_address,
254  u32 dst_address_byte_index)
255 {
256  ip4_mtrie_8_ply_t *ply;
257 
258  uword current_is_terminal = ip4_mtrie_leaf_is_terminal (current_leaf);
259 
260  if (!current_is_terminal)
261  {
262  ply = ip4_ply_pool + (current_leaf >> 1);
263  return (ply->leaves[dst_address->as_u8[dst_address_byte_index]]);
264  }
265 
266  return current_leaf;
267 }
268 
271  const ip4_address_t *dst_address)
272 {
273  ip4_mtrie_leaf_t next_leaf;
274  ip4_mtrie_8_ply_t *ply;
275 
277  next_leaf = ply->leaves[dst_address->as_u8[0]];
278 
279  return next_leaf;
280 }
281 
282 #endif /* included_ip_ip4_fib_h */
283 
284 /*
285  * fd.io coding-style-patch-verification: ON
286  *
287  * Local Variables:
288  * eval: (c-set-style "gnu")
289  * End:
290  */
ip4_mtrie_16_free
void ip4_mtrie_16_free(ip4_mtrie_16_t *m)
Free an mtrie, It must be empty when free'd.
Definition: ip4_mtrie.c:189
ip4_mtrie_8_ply_t_::leaves
ip4_mtrie_leaf_t leaves[256]
Definition: ip4_mtrie.h:93
ip4_mtrie_8_route_del
void ip4_mtrie_8_route_del(ip4_mtrie_8_t *m, const ip4_address_t *dst_address, u32 dst_address_length, u32 adj_index, u32 cover_address_length, u32 cover_adj_index)
Definition: ip4_mtrie.c:690
ip4_mtrie_leaf_t
u32 ip4_mtrie_leaf_t
Definition: ip4_mtrie.h:52
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
ip4_mtrie_16_memory_usage
uword ip4_mtrie_16_memory_usage(ip4_mtrie_16_t *m)
return the memory used by the table
Definition: ip4_mtrie.c:731
ip4_mtrie_16_ply_t_
Definition: ip4_mtrie.h:63
ip4_ply_pool
ip4_mtrie_8_ply_t * ip4_ply_pool
A global pool of 8bit stride plys.
Definition: ip4_mtrie.c:48
ip4_mtrie_8_t
The mutiway-TRIE with a 8-8-8-8 stride.
Definition: ip4_mtrie.h:142
ip4_mtrie_leaf_get_adj_index
static u32 ip4_mtrie_leaf_get_adj_index(ip4_mtrie_leaf_t n)
From the stored slot value extract the LB index value.
Definition: ip4_mtrie.h:210
ip4_mtrie_8_ply_t_::n_non_empty_leafs
i32 n_non_empty_leafs
Number of non-empty leafs (whether terminal or not).
Definition: ip4_mtrie.h:108
ip4_mtrie_16_init
void ip4_mtrie_16_init(ip4_mtrie_16_t *m)
Initialise an mtrie.
Definition: ip4_mtrie.c:205
ip4_mtrie_8_t::root_ply
u32 root_ply
Definition: ip4_mtrie.h:145
i32
signed int i32
Definition: types.h:77
ip4_mtrie_16_lookup_step_one
static ip4_mtrie_leaf_t ip4_mtrie_16_lookup_step_one(const ip4_mtrie_16_t *m, const ip4_address_t *dst_address)
Lookup step number 1.
Definition: ip4_mtrie.h:241
ip4_mtrie_8_init
void ip4_mtrie_8_init(ip4_mtrie_8_t *m)
Definition: ip4_mtrie.c:231
ip4_mtrie_8_ply_t_
One ply of the 4 ply mtrie fib.
Definition: ip4_mtrie.h:86
format_ip4_mtrie_8
format_function_t format_ip4_mtrie_8
Definition: ip4_mtrie.h:190
ip4_mtrie_8_ply_t
struct ip4_mtrie_8_ply_t_ ip4_mtrie_8_ply_t
One ply of the 4 ply mtrie fib.
format_ip4_mtrie_16
format_function_t format_ip4_mtrie_16
Format/display the contents of the mtrie.
Definition: ip4_mtrie.h:189
PLY_16_SIZE
#define PLY_16_SIZE
the 16 way stride that is the top PLY of the mtrie We do not maintain the count of 'real' leaves in t...
Definition: ip4_mtrie.h:62
ip4_address_t::as_u16
u16 as_u16[2]
Definition: ip4_packet.h:56
ip4_mtrie_16_route_del
void ip4_mtrie_16_route_del(ip4_mtrie_16_t *m, const ip4_address_t *dst_address, u32 dst_address_length, u32 adj_index, u32 cover_address_length, u32 cover_adj_index)
remove a route/entry to the mtrie
Definition: ip4_mtrie.c:670
ip4_mtrie_8_route_add
void ip4_mtrie_8_route_add(ip4_mtrie_8_t *m, const ip4_address_t *dst_address, u32 dst_address_length, u32 adj_index)
Definition: ip4_mtrie.c:652
uword
u64 uword
Definition: types.h:112
ip4_address_t
Definition: ip4_packet.h:50
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
ip4_mtrie_16_ply_t_::dst_address_bits_of_leaves
u8 dst_address_bits_of_leaves[PLY_16_SIZE]
Prefix length for terminal leaves.
Definition: ip4_mtrie.h:80
ip4_mtrie_8_lookup_step
static ip4_mtrie_leaf_t ip4_mtrie_8_lookup_step(ip4_mtrie_leaf_t current_leaf, const ip4_address_t *dst_address, u32 dst_address_byte_index)
Definition: ip4_mtrie.h:252
ip4_mtrie_8_free
void ip4_mtrie_8_free(ip4_mtrie_8_t *m)
Definition: ip4_mtrie.c:211
ip4_mtrie_8_ply_t_::dst_address_bits_base
i32 dst_address_bits_base
The length of the ply's covering prefix.
Definition: ip4_mtrie.h:115
ip4_packet.h
ip4_mtrie_16_route_add
void ip4_mtrie_16_route_add(ip4_mtrie_16_t *m, const ip4_address_t *dst_address, u32 dst_address_length, u32 adj_index)
Add a route/entry to the mtrie.
Definition: ip4_mtrie.c:636
ip4_address_t::as_u8
u8 as_u8[4]
Definition: ip4_packet.h:55
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
ip4_mtrie_16_ply_t_::leaves
ip4_mtrie_leaf_t leaves[PLY_16_SIZE]
Definition: ip4_mtrie.h:70
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip4_mtrie_8_memory_usage
uword ip4_mtrie_8_memory_usage(ip4_mtrie_8_t *m)
Definition: ip4_mtrie.c:746
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
ip4_mtrie_16_t::root_ply
ip4_mtrie_16_ply_t root_ply
Embed the PLY with the mtrie struct.
Definition: ip4_mtrie.h:135
cache.h
u32
unsigned int u32
Definition: types.h:88
ip4_mtrie_8_ply_t_::pad
u8 pad[CLIB_CACHE_LINE_BYTES - 2 *sizeof(i32)]
Definition: ip4_mtrie.h:118
vector.h
u32x4
unsigned long long u32x4
Definition: ixge.c:28
ip4_mtrie_16_lookup_step
static ip4_mtrie_leaf_t ip4_mtrie_16_lookup_step(ip4_mtrie_leaf_t current_leaf, const ip4_address_t *dst_address, u32 dst_address_byte_index)
Lookup step.
Definition: ip4_mtrie.h:220
ip4_mtrie_8_lookup_step_one
static ip4_mtrie_leaf_t ip4_mtrie_8_lookup_step_one(const ip4_mtrie_8_t *m, const ip4_address_t *dst_address)
Definition: ip4_mtrie.h:270
u8
unsigned char u8
Definition: types.h:56
lookup.h
ip4_mtrie_16_t
The mutiway-TRIE with a 16-8-8 stride.
Definition: ip4_mtrie.h:128
STATIC_ASSERT
STATIC_ASSERT(0==sizeof(ip4_mtrie_8_ply_t) % CLIB_CACHE_LINE_BYTES, "IP4 Mtrie ply cache line")
ip4_mtrie_8_ply_t_::dst_address_bits_of_leaves
u8 dst_address_bits_of_leaves[256]
Prefix length for leaves/ply.
Definition: ip4_mtrie.h:103
ip4_mtrie_leaf_is_terminal
static u32 ip4_mtrie_leaf_is_terminal(ip4_mtrie_leaf_t n)
Is the leaf terminal (i.e.
Definition: ip4_mtrie.h:201
ip4_mtrie_16_ply_t
struct ip4_mtrie_16_ply_t_ ip4_mtrie_16_ply_t