1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
14 * Level compressed tree implementation for IPv6 Longest Prefix Match
17 #include <rte_memcpy.h>
18 #include <rte_common.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(const uint8_t *ip1, const 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
125 struct rte_rib6_node *
126 rte_rib6_lookup(struct rte_rib6 *rib,
127 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
130 * Lookup less specific route into the RIB structure
133 * Pointer to struct rte_rib6_node that represents target route
135 * pointer to struct rte_rib6_node that represents
136 * less specific route on success
139 struct rte_rib6_node *
140 rte_rib6_lookup_parent(struct rte_rib6_node *ent);
143 * Provides exact mach lookup of the prefix into the RIB structure
148 * net to be looked up in the RIB
152 * pointer to struct rte_rib6_node on success
155 struct rte_rib6_node *
156 rte_rib6_lookup_exact(struct rte_rib6 *rib,
157 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
160 * Retrieve next more specific prefix from the RIB
161 * that is covered by ip/depth supernet in an ascending order
166 * net address of supernet prefix that covers returned more specific prefixes
168 * supernet prefix length
170 * pointer to the last returned prefix to get next prefix
172 * NULL to get first more specific prefix
174 * -RTE_RIB6_GET_NXT_ALL
175 * get all prefixes from subtrie
176 * -RTE_RIB6_GET_NXT_COVER
177 * get only first more specific prefix even if it have more specifics
179 * pointer to the next more specific prefix
180 * NULL if there is no prefixes left
182 struct rte_rib6_node *
183 rte_rib6_get_nxt(struct rte_rib6 *rib,
184 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
185 uint8_t depth, struct rte_rib6_node *last, int flag);
188 * Remove prefix from the RIB
193 * net to be removed from the RIB
198 rte_rib6_remove(struct rte_rib6 *rib,
199 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
202 * Insert prefix into the RIB
207 * net to be inserted to the RIB
211 * pointer to new rte_rib6_node on success
214 struct rte_rib6_node *
215 rte_rib6_insert(struct rte_rib6 *rib,
216 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
219 * Get an ip from rte_rib6_node
222 * pointer to the rib6 node
224 * pointer to the ipv6 to save
227 * -1 on failure with rte_errno indicating reason for failure.
230 rte_rib6_get_ip(const struct rte_rib6_node *node,
231 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
234 * Get a depth from rte_rib6_node
237 * pointer to the rib6 node
239 * pointer to the depth to save
242 * -1 on failure with rte_errno indicating reason for failure.
245 rte_rib6_get_depth(const struct rte_rib6_node *node, uint8_t *depth);
248 * Get ext field from the rte_rib6_node
249 * It is caller responsibility to make sure there are necessary space
250 * for the ext field inside rib6 node.
253 * pointer to the rte_rib6_node
258 rte_rib6_get_ext(struct rte_rib6_node *node);
261 * Get nexthop from the rte_rib6_node
264 * pointer to the rib6 node
266 * pointer to the nexthop to save
269 * -1 on failure, with rte_errno indicating reason for failure.
272 rte_rib6_get_nh(const struct rte_rib6_node *node, uint64_t *nh);
275 * Set nexthop into the rte_rib6_node
278 * pointer to the rib6 node
280 * nexthop value to set to the rib6 node
283 * -1 on failure, with rte_errno indicating reason for failure.
286 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh);
294 * NUMA socket ID for RIB table memory allocation
296 * Structure containing the configuration
298 * Pointer to RIB object on success
299 * NULL otherwise with rte_errno indicating reason for failure.
302 rte_rib6_create(const char *name, int socket_id,
303 const struct rte_rib6_conf *conf);
306 * Find an existing RIB object and return a pointer to it.
309 * Name of the rib object as passed to rte_rib_create()
311 * Pointer to RIB object on success
312 * NULL otherwise with rte_errno indicating reason for failure.
315 rte_rib6_find_existing(const char *name);
318 * Free an RIB object.
326 rte_rib6_free(struct rte_rib6 *rib);
332 #endif /* _RTE_RIB6_H_ */