blob: 6e3d0e8068b5a2afddfb69b95a21099289aff11f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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#include <vnet/ip/ip.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010041#include <vnet/fib/fib_entry.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
43static void
Dave Barachd7cb1b52016-12-09 09:52:16 -050044ply_init (ip4_fib_mtrie_ply_t * p, ip4_fib_mtrie_leaf_t init,
45 uword prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
Dave Barachd7cb1b52016-12-09 09:52:16 -050047 p->n_non_empty_leafs =
48 ip4_fib_mtrie_leaf_is_empty (init) ? 0 : ARRAY_LEN (p->leaves);
49 memset (p->dst_address_bits_of_leaves, prefix_len,
50 sizeof (p->dst_address_bits_of_leaves));
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52 /* Initialize leaves. */
53#ifdef CLIB_HAVE_VEC128
54 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050055 u32x4 *l, init_x4;
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
57#ifndef __ALTIVEC__
58 init_x4 = u32x4_splat (init);
59#else
60 {
61 u32x4_union_t y;
62 y.as_u32[0] = init;
63 y.as_u32[1] = init;
64 y.as_u32[2] = init;
65 y.as_u32[3] = init;
66 init_x4 = y.as_u32x4;
67 }
Dave Barachd7cb1b52016-12-09 09:52:16 -050068#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
Dave Barachd7cb1b52016-12-09 09:52:16 -050070 for (l = p->leaves_as_u32x4;
71 l < p->leaves_as_u32x4 + ARRAY_LEN (p->leaves_as_u32x4); l += 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 {
73 l[0] = init_x4;
74 l[1] = init_x4;
75 l[2] = init_x4;
76 l[3] = init_x4;
77 }
78 }
79#else
80 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050081 u32 *l;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 for (l = p->leaves; l < p->leaves + ARRAY_LEN (p->leaves); l += 4)
84 {
85 l[0] = init;
86 l[1] = init;
87 l[2] = init;
88 l[3] = init;
89 }
90 }
91#endif
92}
93
94static ip4_fib_mtrie_leaf_t
Dave Barachd7cb1b52016-12-09 09:52:16 -050095ply_create (ip4_fib_mtrie_t * m, ip4_fib_mtrie_leaf_t init_leaf,
96 uword prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -070097{
Dave Barachd7cb1b52016-12-09 09:52:16 -050098 ip4_fib_mtrie_ply_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 /* Get cache aligned ply. */
101 pool_get_aligned (m->ply_pool, p, sizeof (p[0]));
102
103 ply_init (p, init_leaf, prefix_len);
104 return ip4_fib_mtrie_leaf_set_next_ply_index (p - m->ply_pool);
105}
106
107always_inline ip4_fib_mtrie_ply_t *
108get_next_ply_for_leaf (ip4_fib_mtrie_t * m, ip4_fib_mtrie_leaf_t l)
109{
110 uword n = ip4_fib_mtrie_leaf_get_next_ply_index (l);
111 /* It better not be the root ply. */
112 ASSERT (n != 0);
113 return pool_elt_at_index (m->ply_pool, n);
114}
115
116static void
117ply_free (ip4_fib_mtrie_t * m, ip4_fib_mtrie_ply_t * p)
118{
119 uword i, is_root;
120
121 is_root = p - m->ply_pool == 0;
122
Dave Barachd7cb1b52016-12-09 09:52:16 -0500123 for (i = 0; i < ARRAY_LEN (p->leaves); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 {
125 ip4_fib_mtrie_leaf_t l = p->leaves[i];
126 if (ip4_fib_mtrie_leaf_is_next_ply (l))
127 ply_free (m, get_next_ply_for_leaf (m, l));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
130 if (is_root)
131 ply_init (p, IP4_FIB_MTRIE_LEAF_EMPTY, /* prefix_len */ 0);
132 else
133 pool_put (m->ply_pool, p);
134}
135
Dave Barachd7cb1b52016-12-09 09:52:16 -0500136void
137ip4_fib_free (ip4_fib_mtrie_t * m)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500139 ip4_fib_mtrie_ply_t *root_ply = pool_elt_at_index (m->ply_pool, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 ply_free (m, root_ply);
141}
142
Dave Barachd7cb1b52016-12-09 09:52:16 -0500143u32
144ip4_mtrie_lookup_address (ip4_fib_mtrie_t * m, ip4_address_t dst)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500146 ip4_fib_mtrie_ply_t *p = pool_elt_at_index (m->ply_pool, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 ip4_fib_mtrie_leaf_t l;
148
149 l = p->leaves[dst.as_u8[0]];
150 if (ip4_fib_mtrie_leaf_is_terminal (l))
151 return ip4_fib_mtrie_leaf_get_adj_index (l);
152
153 p = get_next_ply_for_leaf (m, l);
154 l = p->leaves[dst.as_u8[1]];
155 if (ip4_fib_mtrie_leaf_is_terminal (l))
156 return ip4_fib_mtrie_leaf_get_adj_index (l);
157
158 p = get_next_ply_for_leaf (m, l);
159 l = p->leaves[dst.as_u8[2]];
160 if (ip4_fib_mtrie_leaf_is_terminal (l))
161 return ip4_fib_mtrie_leaf_get_adj_index (l);
162
163 p = get_next_ply_for_leaf (m, l);
164 l = p->leaves[dst.as_u8[3]];
165
166 ASSERT (ip4_fib_mtrie_leaf_is_terminal (l));
167 return ip4_fib_mtrie_leaf_get_adj_index (l);
168}
169
Dave Barachd7cb1b52016-12-09 09:52:16 -0500170typedef struct
171{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 ip4_address_t dst_address;
173 u32 dst_address_length;
174 u32 adj_index;
175} ip4_fib_mtrie_set_unset_leaf_args_t;
176
177static void
178set_ply_with_more_specific_leaf (ip4_fib_mtrie_t * m,
179 ip4_fib_mtrie_ply_t * ply,
180 ip4_fib_mtrie_leaf_t new_leaf,
181 uword new_leaf_dst_address_bits)
182{
183 ip4_fib_mtrie_leaf_t old_leaf;
184 uword i;
185
186 ASSERT (ip4_fib_mtrie_leaf_is_terminal (new_leaf));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500187 ASSERT (!ip4_fib_mtrie_leaf_is_empty (new_leaf));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
189 for (i = 0; i < ARRAY_LEN (ply->leaves); i++)
190 {
191 old_leaf = ply->leaves[i];
192
193 /* Recurse into sub plies. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 if (!ip4_fib_mtrie_leaf_is_terminal (old_leaf))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500196 ip4_fib_mtrie_ply_t *sub_ply = get_next_ply_for_leaf (m, old_leaf);
197 set_ply_with_more_specific_leaf (m, sub_ply, new_leaf,
198 new_leaf_dst_address_bits);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 }
200
201 /* Replace less specific terminal leaves with new leaf. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500202 else if (new_leaf_dst_address_bits >=
203 ply->dst_address_bits_of_leaves[i])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500205 __sync_val_compare_and_swap (&ply->leaves[i], old_leaf, new_leaf);
206 ASSERT (ply->leaves[i] == new_leaf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 ply->dst_address_bits_of_leaves[i] = new_leaf_dst_address_bits;
208 ply->n_non_empty_leafs += ip4_fib_mtrie_leaf_is_empty (old_leaf);
209 }
210 }
211}
212
213static void
214set_leaf (ip4_fib_mtrie_t * m,
215 ip4_fib_mtrie_set_unset_leaf_args_t * a,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500216 u32 old_ply_index, u32 dst_address_byte_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217{
218 ip4_fib_mtrie_leaf_t old_leaf, new_leaf;
219 i32 n_dst_bits_next_plies;
220 u8 dst_byte;
221
222 ASSERT (a->dst_address_length > 0 && a->dst_address_length <= 32);
223 ASSERT (dst_address_byte_index < ARRAY_LEN (a->dst_address.as_u8));
224
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 n_dst_bits_next_plies =
226 a->dst_address_length - BITS (u8) * (dst_address_byte_index + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
228 dst_byte = a->dst_address.as_u8[dst_address_byte_index];
229
230 /* Number of bits next plies <= 0 => insert leaves this ply. */
231 if (n_dst_bits_next_plies <= 0)
232 {
233 uword i, n_dst_bits_this_ply, old_leaf_is_terminal;
234
235 n_dst_bits_this_ply = -n_dst_bits_next_plies;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 ASSERT ((a->dst_address.as_u8[dst_address_byte_index] &
237 pow2_mask (n_dst_bits_this_ply)) == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
239 for (i = dst_byte; i < dst_byte + (1 << n_dst_bits_this_ply); i++)
240 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500241 ip4_fib_mtrie_ply_t *old_ply, *new_ply;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
243 old_ply = pool_elt_at_index (m->ply_pool, old_ply_index);
244
245 old_leaf = old_ply->leaves[i];
246 old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf);
247
248 /* Is leaf to be inserted more specific? */
249 if (a->dst_address_length >= old_ply->dst_address_bits_of_leaves[i])
250 {
251 new_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index);
252
253 if (old_leaf_is_terminal)
254 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500255 old_ply->dst_address_bits_of_leaves[i] =
256 a->dst_address_length;
257 __sync_val_compare_and_swap (&old_ply->leaves[i], old_leaf,
258 new_leaf);
259 ASSERT (old_ply->leaves[i] == new_leaf);
260 old_ply->n_non_empty_leafs +=
261 ip4_fib_mtrie_leaf_is_empty (old_leaf);
262 ASSERT (old_ply->n_non_empty_leafs <=
263 ARRAY_LEN (old_ply->leaves));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264 }
265 else
266 {
267 /* Existing leaf points to another ply. We need to place new_leaf into all
268 more specific slots. */
269 new_ply = get_next_ply_for_leaf (m, old_leaf);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 set_ply_with_more_specific_leaf (m, new_ply, new_leaf,
271 a->dst_address_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 }
273 }
274
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 else if (!old_leaf_is_terminal)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 {
277 new_ply = get_next_ply_for_leaf (m, old_leaf);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500278 set_leaf (m, a, new_ply - m->ply_pool,
279 dst_address_byte_index + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 }
281 }
282 }
283 else
284 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 ip4_fib_mtrie_ply_t *old_ply, *new_ply;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
287 old_ply = pool_elt_at_index (m->ply_pool, old_ply_index);
288 old_leaf = old_ply->leaves[dst_byte];
289 if (ip4_fib_mtrie_leaf_is_terminal (old_leaf))
290 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 new_leaf =
292 ply_create (m, old_leaf,
293 old_ply->dst_address_bits_of_leaves[dst_byte]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 new_ply = get_next_ply_for_leaf (m, new_leaf);
295
296 /* Refetch since ply_create may move pool. */
297 old_ply = pool_elt_at_index (m->ply_pool, old_ply_index);
298
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 __sync_val_compare_and_swap (&old_ply->leaves[dst_byte], old_leaf,
300 new_leaf);
301 ASSERT (old_ply->leaves[dst_byte] == new_leaf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 old_ply->dst_address_bits_of_leaves[dst_byte] = 0;
303
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 old_ply->n_non_empty_leafs -=
305 ip4_fib_mtrie_leaf_is_non_empty (old_leaf);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 ASSERT (old_ply->n_non_empty_leafs >= 0);
307
308 /* Account for the ply we just created. */
309 old_ply->n_non_empty_leafs += 1;
310 }
311 else
312 new_ply = get_next_ply_for_leaf (m, old_leaf);
313
314 set_leaf (m, a, new_ply - m->ply_pool, dst_address_byte_index + 1);
315 }
316}
317
318static uword
319unset_leaf (ip4_fib_mtrie_t * m,
320 ip4_fib_mtrie_set_unset_leaf_args_t * a,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321 ip4_fib_mtrie_ply_t * old_ply, u32 dst_address_byte_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322{
323 ip4_fib_mtrie_leaf_t old_leaf, del_leaf;
324 i32 n_dst_bits_next_plies;
Dave Barach6f6f34f2016-08-08 13:05:31 -0400325 i32 i, n_dst_bits_this_ply, old_leaf_is_terminal;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 u8 dst_byte;
327
328 ASSERT (a->dst_address_length > 0 && a->dst_address_length <= 32);
329 ASSERT (dst_address_byte_index < ARRAY_LEN (a->dst_address.as_u8));
330
Dave Barachd7cb1b52016-12-09 09:52:16 -0500331 n_dst_bits_next_plies =
332 a->dst_address_length - BITS (u8) * (dst_address_byte_index + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
334 dst_byte = a->dst_address.as_u8[dst_address_byte_index];
335 if (n_dst_bits_next_plies < 0)
336 dst_byte &= ~pow2_mask (-n_dst_bits_next_plies);
337
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338 n_dst_bits_this_ply =
339 n_dst_bits_next_plies <= 0 ? -n_dst_bits_next_plies : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 n_dst_bits_this_ply = clib_min (8, n_dst_bits_this_ply);
341
342 del_leaf = ip4_fib_mtrie_leaf_set_adj_index (a->adj_index);
343
344 for (i = dst_byte; i < dst_byte + (1 << n_dst_bits_this_ply); i++)
345 {
346 old_leaf = old_ply->leaves[i];
347 old_leaf_is_terminal = ip4_fib_mtrie_leaf_is_terminal (old_leaf);
348
349 if (old_leaf == del_leaf
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 || (!old_leaf_is_terminal
351 && unset_leaf (m, a, get_next_ply_for_leaf (m, old_leaf),
352 dst_address_byte_index + 1)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 {
354 old_ply->leaves[i] = IP4_FIB_MTRIE_LEAF_EMPTY;
355 old_ply->dst_address_bits_of_leaves[i] = 0;
356
357 /* No matter what we just deleted a non-empty leaf. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500358 ASSERT (!ip4_fib_mtrie_leaf_is_empty (old_leaf));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 old_ply->n_non_empty_leafs -= 1;
360
361 ASSERT (old_ply->n_non_empty_leafs >= 0);
362 if (old_ply->n_non_empty_leafs == 0 && dst_address_byte_index > 0)
363 {
364 pool_put (m->ply_pool, old_ply);
365 /* Old ply was deleted. */
366 return 1;
367 }
368 }
369 }
370
371 /* Old ply was not deleted. */
372 return 0;
373}
374
Dave Barachd7cb1b52016-12-09 09:52:16 -0500375void
376ip4_mtrie_init (ip4_fib_mtrie_t * m)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377{
378 ip4_fib_mtrie_leaf_t root;
379 memset (m, 0, sizeof (m[0]));
380 m->default_leaf = IP4_FIB_MTRIE_LEAF_EMPTY;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500381 root = ply_create (m, IP4_FIB_MTRIE_LEAF_EMPTY, /* dst_address_bits_of_leaves */
382 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 ASSERT (ip4_fib_mtrie_leaf_get_next_ply_index (root) == 0);
384}
385
386void
387ip4_fib_mtrie_add_del_route (ip4_fib_t * fib,
388 ip4_address_t dst_address,
389 u32 dst_address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390 u32 adj_index, u32 is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500392 ip4_fib_mtrie_t *m = &fib->mtrie;
393 ip4_fib_mtrie_ply_t *root_ply;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 ip4_fib_mtrie_set_unset_leaf_args_t a;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500395 ip4_main_t *im = &ip4_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 ASSERT (m->ply_pool != 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
399 root_ply = pool_elt_at_index (m->ply_pool, 0);
400
401 /* Honor dst_address_length. Fib masks are in network byte order */
402 dst_address.as_u32 &= im->fib_masks[dst_address_length];
403 a.dst_address = dst_address;
404 a.dst_address_length = dst_address_length;
405 a.adj_index = adj_index;
406
Dave Barachd7cb1b52016-12-09 09:52:16 -0500407 if (!is_del)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 {
409 if (dst_address_length == 0)
410 m->default_leaf = ip4_fib_mtrie_leaf_set_adj_index (adj_index);
411 else
412 set_leaf (m, &a, /* ply_index */ 0, /* dst_address_byte_index */ 0);
413 }
414 else
415 {
416 if (dst_address_length == 0)
417 m->default_leaf = IP4_FIB_MTRIE_LEAF_EMPTY;
418
419 else
420 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500421 ip4_main_t *im = &ip4_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 uword i;
423
424 unset_leaf (m, &a, root_ply, 0);
425
426 /* Find next less specific route and insert into mtrie. */
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100427 for (i = dst_address_length - 1; i >= 1; i--)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500429 uword *p;
430 index_t lbi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 ip4_address_t key;
432
Dave Barachd7cb1b52016-12-09 09:52:16 -0500433 if (!fib->fib_entry_by_dst_address[i])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 continue;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 key.as_u32 = dst_address.as_u32 & im->fib_masks[i];
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100437 p = hash_get (fib->fib_entry_by_dst_address[i], key.as_u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 if (p)
439 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500440 lbi = fib_entry_contribute_ip_forwarding (p[0])->dpoi_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100441 if (INDEX_INVALID == lbi)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500442 continue;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100443
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 a.dst_address = key;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100445 a.adj_index = lbi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 a.dst_address_length = i;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100447
Dave Barachd7cb1b52016-12-09 09:52:16 -0500448 set_leaf (m, &a, /* ply_index */ 0,
449 /* dst_address_byte_index */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 break;
451 }
452 }
453 }
454 }
455}
456
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457/* Returns number of bytes of memory used by mtrie. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500458static uword
459mtrie_memory_usage (ip4_fib_mtrie_t * m, ip4_fib_mtrie_ply_t * p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460{
461 uword bytes, i;
462
Dave Barachd7cb1b52016-12-09 09:52:16 -0500463 if (!p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 {
465 if (pool_is_free_index (m->ply_pool, 0))
466 return 0;
467 p = pool_elt_at_index (m->ply_pool, 0);
468 }
469
470 bytes = sizeof (p[0]);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500471 for (i = 0; i < ARRAY_LEN (p->leaves); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 {
473 ip4_fib_mtrie_leaf_t l = p->leaves[i];
474 if (ip4_fib_mtrie_leaf_is_next_ply (l))
475 bytes += mtrie_memory_usage (m, get_next_ply_for_leaf (m, l));
476 }
477
478 return bytes;
479}
480
Dave Barachd7cb1b52016-12-09 09:52:16 -0500481static u8 *
482format_ip4_fib_mtrie_leaf (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483{
484 ip4_fib_mtrie_leaf_t l = va_arg (*va, ip4_fib_mtrie_leaf_t);
485
486 if (ip4_fib_mtrie_leaf_is_empty (l))
487 s = format (s, "miss");
488 else if (ip4_fib_mtrie_leaf_is_terminal (l))
489 s = format (s, "adj %d", ip4_fib_mtrie_leaf_get_adj_index (l));
490 else
491 s = format (s, "next ply %d", ip4_fib_mtrie_leaf_get_next_ply_index (l));
492 return s;
493}
494
Dave Barachd7cb1b52016-12-09 09:52:16 -0500495static u8 *
496format_ip4_fib_mtrie_ply (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498 ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 u32 base_address = va_arg (*va, u32);
500 u32 ply_index = va_arg (*va, u32);
501 u32 dst_address_byte_index = va_arg (*va, u32);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500502 ip4_fib_mtrie_ply_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 uword i, indent;
504
505 p = pool_elt_at_index (m->ply_pool, ply_index);
506 indent = format_get_indent (s);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500507 s =
508 format (s, "ply index %d, %d non-empty leaves", ply_index,
509 p->n_non_empty_leafs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 for (i = 0; i < ARRAY_LEN (p->leaves); i++)
511 {
512 ip4_fib_mtrie_leaf_t l = p->leaves[i];
513
Dave Barachd7cb1b52016-12-09 09:52:16 -0500514 if (!ip4_fib_mtrie_leaf_is_empty (l))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 {
516 u32 a, ia_length;
517 ip4_address_t ia;
518
Dave Barachd7cb1b52016-12-09 09:52:16 -0500519 a = base_address + (i << (24 - 8 * dst_address_byte_index));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 ia.as_u32 = clib_host_to_net_u32 (a);
521 if (ip4_fib_mtrie_leaf_is_terminal (l))
522 ia_length = p->dst_address_bits_of_leaves[i];
523 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500524 ia_length = 8 * (1 + dst_address_byte_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 s = format (s, "\n%U%20U %U",
526 format_white_space, indent + 2,
527 format_ip4_address_and_length, &ia, ia_length,
528 format_ip4_fib_mtrie_leaf, l);
529
530 if (ip4_fib_mtrie_leaf_is_next_ply (l))
531 s = format (s, "\n%U%U",
532 format_white_space, indent + 2,
533 format_ip4_fib_mtrie_ply, m, a,
534 ip4_fib_mtrie_leaf_get_next_ply_index (l),
535 dst_address_byte_index + 1);
536 }
537 }
538
539 return s;
540}
541
Dave Barachd7cb1b52016-12-09 09:52:16 -0500542u8 *
543format_ip4_fib_mtrie (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500545 ip4_fib_mtrie_t *m = va_arg (*va, ip4_fib_mtrie_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
547 s = format (s, "%d plies, memory usage %U",
548 pool_elts (m->ply_pool),
549 format_memory_size, mtrie_memory_usage (m, 0));
550
551 if (pool_elts (m->ply_pool) > 0)
552 {
553 ip4_address_t base_address;
554 base_address.as_u32 = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500555 s =
556 format (s, "\n %U", format_ip4_fib_mtrie_ply, m, base_address, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 }
558
559 return s;
560}
Dave Barachd7cb1b52016-12-09 09:52:16 -0500561
562/*
563 * fd.io coding-style-patch-verification: ON
564 *
565 * Local Variables:
566 * eval: (c-set-style "gnu")
567 * End:
568 */