blob: 34b97dc1a9dacfe50ea310fe028491dfab1e4182 [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#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.
Neale Rannsa3af3372017-03-28 03:49:52 -070050 0 + 2*next_ply_index for non-terminals, i.e. PLYs
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 1 => empty (adjacency index of zero is special miss adjacency). */
52typedef u32 ip4_fib_mtrie_leaf_t;
53
Neale Ranns0bfe5d82016-08-25 15:29:12 +010054#define IP4_FIB_MTRIE_LEAF_EMPTY (1 + 2*0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Neale Ranns04a75e32017-03-23 06:46:01 -070056/**
Neale Rannsa3af3372017-03-28 03:49:52 -070057 * @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)
63typedef struct ip4_fib_mtrie_16_ply_t_
64{
65 /**
66 * The leaves/slots/buckets to be filed with leafs
67 */
68 union
69 {
70 ip4_fib_mtrie_leaf_t leaves[PLY_16_SIZE];
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 */
80 u8 dst_address_bits_of_leaves[PLY_16_SIZE];
81} ip4_fib_mtrie_16_ply_t;
82
83/**
Neale Ranns04a75e32017-03-23 06:46:01 -070084 * @brief One ply of the 4 ply mtrie fib.
85 */
Neale Rannsa3af3372017-03-28 03:49:52 -070086typedef struct ip4_fib_mtrie_8_ply_t_
Dave Barachd7cb1b52016-12-09 09:52:16 -050087{
Neale Ranns04a75e32017-03-23 06:46:01 -070088 /**
89 * The leaves/slots/buckets to be filed with leafs
90 */
91 union
92 {
93 ip4_fib_mtrie_leaf_t leaves[256];
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Neale Ranns04a75e32017-03-23 06:46:01 -070095#ifdef CLIB_HAVE_VEC128
96 u32x4 leaves_as_u32x4[256 / 4];
97#endif
98 };
99
100 /**
101 * Prefix length for leaves/ply.
102 */
103 u8 dst_address_bits_of_leaves[256];
104
105 /**
106 * Number of non-empty leafs (whether terminal or not).
107 */
108 i32 n_non_empty_leafs;
109
110 /**
111 * The length of the ply's coviering 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 */
115 i32 dst_address_bits_base;
116
117 /* Pad to cache line boundary. */
118 u8 pad[CLIB_CACHE_LINE_BYTES - 2 * sizeof (i32)];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500119}
Neale Rannsa3af3372017-03-28 03:49:52 -0700120ip4_fib_mtrie_8_ply_t;
Neale Ranns04a75e32017-03-23 06:46:01 -0700121
Neale Rannsa3af3372017-03-28 03:49:52 -0700122STATIC_ASSERT (0 == sizeof (ip4_fib_mtrie_8_ply_t) % CLIB_CACHE_LINE_BYTES,
Neale Ranns04a75e32017-03-23 06:46:01 -0700123 "IP4 Mtrie ply cache line");
124
Neale Rannsa3af3372017-03-28 03:49:52 -0700125/**
126 * @brief The mutiway-TRIE.
127 * There is no data associated with the mtrie apart from the top PLY
128 */
Neale Ranns04a75e32017-03-23 06:46:01 -0700129typedef struct
130{
Neale Rannsa3af3372017-03-28 03:49:52 -0700131 /**
132 * Embed the PLY with the mtrie struct. This means that the Data-plane
133 * 'get me the mtrie' returns the first ply, and not an indirect 'pointer'
134 * to it. therefore no cachline misses in the data-path.
135 */
136 ip4_fib_mtrie_16_ply_t root_ply;
Neale Ranns04a75e32017-03-23 06:46:01 -0700137} ip4_fib_mtrie_t;
138
Neale Rannsa3af3372017-03-28 03:49:52 -0700139/**
140 * @brief Initialise an mtrie
141 */
142void ip4_mtrie_init (ip4_fib_mtrie_t * m);
Neale Ranns04a75e32017-03-23 06:46:01 -0700143
Neale Rannsa3af3372017-03-28 03:49:52 -0700144/**
145 * @brief Free an mtrie, It must be emty when free'd
146 */
147void ip4_mtrie_free (ip4_fib_mtrie_t * m);
Neale Ranns04a75e32017-03-23 06:46:01 -0700148
Neale Rannsa3af3372017-03-28 03:49:52 -0700149/**
150 * @brief Add a route/rntry to the mtrie
151 */
152void ip4_fib_mtrie_route_add (ip4_fib_mtrie_t * m,
153 const ip4_address_t * dst_address,
154 u32 dst_address_length, u32 adj_index);
155/**
156 * @brief remove a route/rntry to the mtrie
157 */
158void ip4_fib_mtrie_route_del (ip4_fib_mtrie_t * m,
159 const ip4_address_t * dst_address,
160 u32 dst_address_length,
161 u32 adj_index,
162 u32 cover_address_length, u32 cover_adj_index);
Neale Ranns04a75e32017-03-23 06:46:01 -0700163
Neale Rannsa3af3372017-03-28 03:49:52 -0700164/**
Neale Rannsc87aafa2017-11-29 00:59:31 -0800165 * @brief return the memory used by the table
166 */
167uword ip4_fib_mtrie_memory_usage (ip4_fib_mtrie_t * m);
168
169/**
Neale Rannsa3af3372017-03-28 03:49:52 -0700170 * @brief Format/display the contents of the mtrie
171 */
Neale Ranns04a75e32017-03-23 06:46:01 -0700172format_function_t format_ip4_fib_mtrie;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Neale Rannsa3af3372017-03-28 03:49:52 -0700174/**
175 * @brief A global pool of 8bit stride plys
176 */
177extern ip4_fib_mtrie_8_ply_t *ip4_ply_pool;
178
179/**
180 * Is the leaf terminal (i.e. an LB index) or non-terminak (i.e. a PLY index)
181 */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182always_inline u32
183ip4_fib_mtrie_leaf_is_terminal (ip4_fib_mtrie_leaf_t n)
184{
185 return n & 1;
186}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Neale Rannsa3af3372017-03-28 03:49:52 -0700188/**
189 * From the stored slot value extract the LB index value
190 */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500191always_inline u32
192ip4_fib_mtrie_leaf_get_adj_index (ip4_fib_mtrie_leaf_t n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193{
194 ASSERT (ip4_fib_mtrie_leaf_is_terminal (n));
195 return n >> 1;
196}
197
Neale Rannsa3af3372017-03-28 03:49:52 -0700198/**
199 * @brief Lookup step. Processes 1 byte of 4 byte ip4 address.
200 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201always_inline ip4_fib_mtrie_leaf_t
Neale Ranns04a75e32017-03-23 06:46:01 -0700202ip4_fib_mtrie_lookup_step (const ip4_fib_mtrie_t * m,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 ip4_fib_mtrie_leaf_t current_leaf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100204 const ip4_address_t * dst_address,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 u32 dst_address_byte_index)
206{
Neale Rannsa3af3372017-03-28 03:49:52 -0700207 ip4_fib_mtrie_8_ply_t *ply;
208
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 uword current_is_terminal = ip4_fib_mtrie_leaf_is_terminal (current_leaf);
210
Neale Ranns04a75e32017-03-23 06:46:01 -0700211 if (!current_is_terminal)
212 {
Neale Rannsa3af3372017-03-28 03:49:52 -0700213 ply = ip4_ply_pool + (current_leaf >> 1);
Neale Ranns04a75e32017-03-23 06:46:01 -0700214 return (ply->leaves[dst_address->as_u8[dst_address_byte_index]]);
215 }
216
217 return current_leaf;
218}
219
Neale Rannsa3af3372017-03-28 03:49:52 -0700220/**
221 * @brief Lookup step number 1. Processes 2 bytes of 4 byte ip4 address.
222 */
Neale Ranns04a75e32017-03-23 06:46:01 -0700223always_inline ip4_fib_mtrie_leaf_t
224ip4_fib_mtrie_lookup_step_one (const ip4_fib_mtrie_t * m,
225 const ip4_address_t * dst_address)
226{
227 ip4_fib_mtrie_leaf_t next_leaf;
Neale Ranns04a75e32017-03-23 06:46:01 -0700228
Neale Rannsa3af3372017-03-28 03:49:52 -0700229 next_leaf = m->root_ply.leaves[dst_address->as_u16[0]];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231 return next_leaf;
232}
233
234#endif /* included_ip_ip4_fib_h */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500235
236/*
237 * fd.io coding-style-patch-verification: ON
238 *
239 * Local Variables:
240 * eval: (c-set-style "gnu")
241 * End:
242 */