1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
16 * All functions in this file may be changed or removed without prior notice.
18 * Level compressed tree implementation for IPv6 Longest Prefix Match
21 #include <rte_memcpy.h>
22 #include <rte_compat.h>
24 #define RTE_RIB6_IPV6_ADDR_SIZE 16
27 * rte_rib6_get_nxt() flags
30 /** flag to get all subroutes in a RIB tree */
32 /** flag to get first matched subroutes in a RIB tree */
33 RTE_RIB6_GET_NXT_COVER
39 /** RIB configuration structure */
40 struct rte_rib6_conf {
42 * Size of extension block inside rte_rib_node.
43 * This space could be used to store additional user
47 /* size of rte_rib_node's pool */
52 * Copy IPv6 address from one location to another
55 * pointer to the place to copy
57 * pointer from where to copy
60 rte_rib6_copy_addr(uint8_t *dst, const uint8_t *src)
62 if ((dst == NULL) || (src == NULL))
64 rte_memcpy(dst, src, RTE_RIB6_IPV6_ADDR_SIZE);
68 * Compare two IPv6 addresses
71 * pointer to the first ipv6 address
73 * pointer to the second ipv6 address
80 rte_rib6_is_equal(uint8_t *ip1, uint8_t *ip2) {
83 if ((ip1 == NULL) || (ip2 == NULL))
85 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++) {
93 * Get 8-bit part of 128-bit IPv6 mask
98 * position of a 8-bit chunk in the 128-bit mask
101 * 8-bit chunk of the 128-bit IPv6 mask
103 static inline uint8_t
104 get_msk_part(uint8_t depth, int byte) {
108 depth = RTE_MIN(depth, 128);
109 part = RTE_MAX((int16_t)depth - (byte * 8), 0);
110 part = (part > 8) ? 8 : part;
111 return (uint16_t)(~UINT8_MAX) >> part;
115 * Lookup an IP into the RIB structure
120 * IP to be looked up in the RIB
122 * pointer to struct rte_rib6_node on success
126 struct rte_rib6_node *
127 rte_rib6_lookup(struct rte_rib6 *rib,
128 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
131 * Lookup less specific route into the RIB structure
134 * Pointer to struct rte_rib6_node that represents target route
136 * pointer to struct rte_rib6_node that represents
137 * less specific route on success
141 struct rte_rib6_node *
142 rte_rib6_lookup_parent(struct rte_rib6_node *ent);
145 * Provides exact mach lookup of the prefix into the RIB structure
150 * net to be looked up in the RIB
154 * pointer to struct rte_rib6_node on success
158 struct rte_rib6_node *
159 rte_rib6_lookup_exact(struct rte_rib6 *rib,
160 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
163 * Retrieve next more specific prefix from the RIB
164 * that is covered by ip/depth supernet in an ascending order
169 * net address of supernet prefix that covers returned more specific prefixes
171 * supernet prefix length
173 * pointer to the last returned prefix to get next prefix
175 * NULL to get first more specific prefix
177 * -RTE_RIB6_GET_NXT_ALL
178 * get all prefixes from subtrie
179 * -RTE_RIB6_GET_NXT_COVER
180 * get only first more specific prefix even if it have more specifics
182 * pointer to the next more specific prefix
183 * NULL if there is no prefixes left
186 struct rte_rib6_node *
187 rte_rib6_get_nxt(struct rte_rib6 *rib,
188 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
189 uint8_t depth, struct rte_rib6_node *last, int flag);
192 * Remove prefix from the RIB
197 * net to be removed from the RIB
203 rte_rib6_remove(struct rte_rib6 *rib,
204 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
207 * Insert prefix into the RIB
212 * net to be inserted to the RIB
216 * pointer to new rte_rib6_node on success
220 struct rte_rib6_node *
221 rte_rib6_insert(struct rte_rib6 *rib,
222 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
225 * Get an ip from rte_rib6_node
228 * pointer to the rib6 node
230 * pointer to the ipv6 to save
233 * -1 on failure with rte_errno indicating reason for failure.
237 rte_rib6_get_ip(struct rte_rib6_node *node,
238 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
241 * Get a depth from rte_rib6_node
244 * pointer to the rib6 node
246 * pointer to the depth to save
249 * -1 on failure with rte_errno indicating reason for failure.
253 rte_rib6_get_depth(struct rte_rib6_node *node, uint8_t *depth);
256 * Get ext field from the rte_rib6_node
257 * It is caller responsibility to make sure there are necessary space
258 * for the ext field inside rib6 node.
261 * pointer to the rte_rib6_node
267 rte_rib6_get_ext(struct rte_rib6_node *node);
270 * Get nexthop from the rte_rib6_node
273 * pointer to the rib6 node
275 * pointer to the nexthop to save
278 * -1 on failure, with rte_errno indicating reason for failure.
282 rte_rib6_get_nh(struct rte_rib6_node *node, uint64_t *nh);
285 * Set nexthop into the rte_rib6_node
288 * pointer to the rib6 node
290 * nexthop value to set to the rib6 node
293 * -1 on failure, with rte_errno indicating reason for failure.
297 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh);
305 * NUMA socket ID for RIB table memory allocation
307 * Structure containing the configuration
309 * Pointer to RIB object on success
310 * NULL otherwise with rte_errno indicating reason for failure.
314 rte_rib6_create(const char *name, int socket_id, struct rte_rib6_conf *conf);
317 * Find an existing RIB object and return a pointer to it.
320 * Name of the rib object as passed to rte_rib_create()
322 * Pointer to RIB object on success
323 * NULL otherwise with rte_errno indicating reason for failure.
327 rte_rib6_find_existing(const char *name);
330 * Free an RIB object.
339 rte_rib6_free(struct rte_rib6 *rib);
341 #endif /* _RTE_RIB_H_ */