1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <[email protected]>
3 * Copyright(c) 2019 Intel Corporation
4 */
5
6 #ifndef _RTE_RIB6_H_
7 #define _RTE_RIB6_H_
8
9 /**
10 * @file
11 *
12 * RTE rib6 library.
13 *
14 * @warning
15 * @b EXPERIMENTAL:
16 * All functions in this file may be changed or removed without prior notice.
17 *
18 * Level compressed tree implementation for IPv6 Longest Prefix Match
19 */
20
21 #include <rte_memcpy.h>
22 #include <rte_compat.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define RTE_RIB6_IPV6_ADDR_SIZE 16
29
30 /**
31 * rte_rib6_get_nxt() flags
32 */
33 enum {
34 /** flag to get all subroutes in a RIB tree */
35 RTE_RIB6_GET_NXT_ALL,
36 /** flag to get first matched subroutes in a RIB tree */
37 RTE_RIB6_GET_NXT_COVER
38 };
39
40 struct rte_rib6;
41 struct rte_rib6_node;
42
43 /** RIB configuration structure */
44 struct rte_rib6_conf {
45 /**
46 * Size of extension block inside rte_rib_node.
47 * This space could be used to store additional user
48 * defined data.
49 */
50 size_t ext_sz;
51 /* size of rte_rib_node's pool */
52 int max_nodes;
53 };
54
55 /**
56 * Copy IPv6 address from one location to another
57 *
58 * @param dst
59 * pointer to the place to copy
60 * @param src
61 * pointer from where to copy
62 */
63 static inline void
rte_rib6_copy_addr(uint8_t * dst,const uint8_t * src)64 rte_rib6_copy_addr(uint8_t *dst, const uint8_t *src)
65 {
66 if ((dst == NULL) || (src == NULL))
67 return;
68 rte_memcpy(dst, src, RTE_RIB6_IPV6_ADDR_SIZE);
69 }
70
71 /**
72 * Compare two IPv6 addresses
73 *
74 * @param ip1
75 * pointer to the first ipv6 address
76 * @param ip2
77 * pointer to the second ipv6 address
78 *
79 * @return
80 * 1 if equal
81 * 0 otherwise
82 */
83 static inline int
rte_rib6_is_equal(const uint8_t * ip1,const uint8_t * ip2)84 rte_rib6_is_equal(const uint8_t *ip1, const uint8_t *ip2) {
85 int i;
86
87 if ((ip1 == NULL) || (ip2 == NULL))
88 return 0;
89 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++) {
90 if (ip1[i] != ip2[i])
91 return 0;
92 }
93 return 1;
94 }
95
96 /**
97 * Get 8-bit part of 128-bit IPv6 mask
98 *
99 * @param depth
100 * ipv6 prefix length
101 * @param byte
102 * position of a 8-bit chunk in the 128-bit mask
103 *
104 * @return
105 * 8-bit chunk of the 128-bit IPv6 mask
106 */
107 static inline uint8_t
get_msk_part(uint8_t depth,int byte)108 get_msk_part(uint8_t depth, int byte) {
109 uint8_t part;
110
111 byte &= 0xf;
112 depth = RTE_MIN(depth, 128);
113 part = RTE_MAX((int16_t)depth - (byte * 8), 0);
114 part = (part > 8) ? 8 : part;
115 return (uint16_t)(~UINT8_MAX) >> part;
116 }
117
118 /**
119 * Lookup an IP into the RIB structure
120 *
121 * @param rib
122 * RIB object handle
123 * @param ip
124 * IP to be looked up in the RIB
125 * @return
126 * pointer to struct rte_rib6_node on success
127 * NULL otherwise
128 */
129 __rte_experimental
130 struct rte_rib6_node *
131 rte_rib6_lookup(struct rte_rib6 *rib,
132 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
133
134 /**
135 * Lookup less specific route into the RIB structure
136 *
137 * @param ent
138 * Pointer to struct rte_rib6_node that represents target route
139 * @return
140 * pointer to struct rte_rib6_node that represents
141 * less specific route on success
142 * NULL otherwise
143 */
144 __rte_experimental
145 struct rte_rib6_node *
146 rte_rib6_lookup_parent(struct rte_rib6_node *ent);
147
148 /**
149 * Provides exact mach lookup of the prefix into the RIB structure
150 *
151 * @param rib
152 * RIB object handle
153 * @param ip
154 * net to be looked up in the RIB
155 * @param depth
156 * prefix length
157 * @return
158 * pointer to struct rte_rib6_node on success
159 * NULL otherwise
160 */
161 __rte_experimental
162 struct rte_rib6_node *
163 rte_rib6_lookup_exact(struct rte_rib6 *rib,
164 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
165
166 /**
167 * Retrieve next more specific prefix from the RIB
168 * that is covered by ip/depth supernet in an ascending order
169 *
170 * @param rib
171 * RIB object handle
172 * @param ip
173 * net address of supernet prefix that covers returned more specific prefixes
174 * @param depth
175 * supernet prefix length
176 * @param last
177 * pointer to the last returned prefix to get next prefix
178 * or
179 * NULL to get first more specific prefix
180 * @param flag
181 * -RTE_RIB6_GET_NXT_ALL
182 * get all prefixes from subtrie
183 * -RTE_RIB6_GET_NXT_COVER
184 * get only first more specific prefix even if it have more specifics
185 * @return
186 * pointer to the next more specific prefix
187 * NULL if there is no prefixes left
188 */
189 __rte_experimental
190 struct rte_rib6_node *
191 rte_rib6_get_nxt(struct rte_rib6 *rib,
192 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
193 uint8_t depth, struct rte_rib6_node *last, int flag);
194
195 /**
196 * Remove prefix from the RIB
197 *
198 * @param rib
199 * RIB object handle
200 * @param ip
201 * net to be removed from the RIB
202 * @param depth
203 * prefix length
204 */
205 __rte_experimental
206 void
207 rte_rib6_remove(struct rte_rib6 *rib,
208 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
209
210 /**
211 * Insert prefix into the RIB
212 *
213 * @param rib
214 * RIB object handle
215 * @param ip
216 * net to be inserted to the RIB
217 * @param depth
218 * prefix length
219 * @return
220 * pointer to new rte_rib6_node on success
221 * NULL otherwise
222 */
223 __rte_experimental
224 struct rte_rib6_node *
225 rte_rib6_insert(struct rte_rib6 *rib,
226 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
227
228 /**
229 * Get an ip from rte_rib6_node
230 *
231 * @param node
232 * pointer to the rib6 node
233 * @param ip
234 * pointer to the ipv6 to save
235 * @return
236 * 0 on success
237 * -1 on failure with rte_errno indicating reason for failure.
238 */
239 __rte_experimental
240 int
241 rte_rib6_get_ip(const struct rte_rib6_node *node,
242 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
243
244 /**
245 * Get a depth from rte_rib6_node
246 *
247 * @param node
248 * pointer to the rib6 node
249 * @param depth
250 * pointer to the depth to save
251 * @return
252 * 0 on success
253 * -1 on failure with rte_errno indicating reason for failure.
254 */
255 __rte_experimental
256 int
257 rte_rib6_get_depth(const struct rte_rib6_node *node, uint8_t *depth);
258
259 /**
260 * Get ext field from the rte_rib6_node
261 * It is caller responsibility to make sure there are necessary space
262 * for the ext field inside rib6 node.
263 *
264 * @param node
265 * pointer to the rte_rib6_node
266 * @return
267 * pointer to the ext
268 */
269 __rte_experimental
270 void *
271 rte_rib6_get_ext(struct rte_rib6_node *node);
272
273 /**
274 * Get nexthop from the rte_rib6_node
275 *
276 * @param node
277 * pointer to the rib6 node
278 * @param nh
279 * pointer to the nexthop to save
280 * @return
281 * 0 on success
282 * -1 on failure, with rte_errno indicating reason for failure.
283 */
284 __rte_experimental
285 int
286 rte_rib6_get_nh(const struct rte_rib6_node *node, uint64_t *nh);
287
288 /**
289 * Set nexthop into the rte_rib6_node
290 *
291 * @param node
292 * pointer to the rib6 node
293 * @param nh
294 * nexthop value to set to the rib6 node
295 * @return
296 * 0 on success
297 * -1 on failure, with rte_errno indicating reason for failure.
298 */
299 __rte_experimental
300 int
301 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh);
302
303 /**
304 * Create RIB
305 *
306 * @param name
307 * RIB name
308 * @param socket_id
309 * NUMA socket ID for RIB table memory allocation
310 * @param conf
311 * Structure containing the configuration
312 * @return
313 * Pointer to RIB object on success
314 * NULL otherwise with rte_errno indicating reason for failure.
315 */
316 __rte_experimental
317 struct rte_rib6 *
318 rte_rib6_create(const char *name, int socket_id,
319 const struct rte_rib6_conf *conf);
320
321 /**
322 * Find an existing RIB object and return a pointer to it.
323 *
324 * @param name
325 * Name of the rib object as passed to rte_rib_create()
326 * @return
327 * Pointer to RIB object on success
328 * NULL otherwise with rte_errno indicating reason for failure.
329 */
330 __rte_experimental
331 struct rte_rib6 *
332 rte_rib6_find_existing(const char *name);
333
334 /**
335 * Free an RIB object.
336 *
337 * @param rib
338 * RIB object handle
339 * @return
340 * None
341 */
342 __rte_experimental
343 void
344 rte_rib6_free(struct rte_rib6 *rib);
345
346 #ifdef __cplusplus
347 }
348 #endif
349
350 #endif /* _RTE_RIB6_H_ */
351