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_compat.h>
19 #include <rte_common.h>
25 #define RTE_RIB6_IPV6_ADDR_SIZE 16
28 * rte_rib6_get_nxt() flags
31 /** flag to get all subroutes in a RIB tree */
33 /** flag to get first matched subroutes in a RIB tree */
34 RTE_RIB6_GET_NXT_COVER
40 /** RIB configuration structure */
41 struct rte_rib6_conf {
43 * Size of extension block inside rte_rib_node.
44 * This space could be used to store additional user
48 /* size of rte_rib_node's pool */
53 * Copy IPv6 address from one location to another
56 * pointer to the place to copy
58 * pointer from where to copy
61 rte_rib6_copy_addr(uint8_t *dst, const uint8_t *src)
63 if ((dst == NULL) || (src == NULL))
65 rte_memcpy(dst, src, RTE_RIB6_IPV6_ADDR_SIZE);
69 * Compare two IPv6 addresses
72 * pointer to the first ipv6 address
74 * pointer to the second ipv6 address
81 rte_rib6_is_equal(const uint8_t *ip1, const uint8_t *ip2) {
84 if ((ip1 == NULL) || (ip2 == NULL))
86 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++) {
94 * Get 8-bit part of 128-bit IPv6 mask
99 * position of a 8-bit chunk in the 128-bit mask
102 * 8-bit chunk of the 128-bit IPv6 mask
104 static inline uint8_t
105 get_msk_part(uint8_t depth, int byte) {
109 depth = RTE_MIN(depth, 128);
110 part = RTE_MAX((int16_t)depth - (byte * 8), 0);
111 part = (part > 8) ? 8 : part;
112 return (uint16_t)(~UINT8_MAX) >> part;
116 * Lookup an IP into the RIB structure
121 * IP to be looked up in the RIB
123 * 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
140 struct rte_rib6_node *
141 rte_rib6_lookup_parent(struct rte_rib6_node *ent);
144 * Provides exact mach lookup of the prefix into the RIB structure
149 * net to be looked up in the RIB
153 * pointer to struct rte_rib6_node on success
156 struct rte_rib6_node *
157 rte_rib6_lookup_exact(struct rte_rib6 *rib,
158 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
161 * Retrieve next more specific prefix from the RIB
162 * that is covered by ip/depth supernet in an ascending order
167 * net address of supernet prefix that covers returned more specific prefixes
169 * supernet prefix length
171 * pointer to the last returned prefix to get next prefix
173 * NULL to get first more specific prefix
175 * -RTE_RIB6_GET_NXT_ALL
176 * get all prefixes from subtrie
177 * -RTE_RIB6_GET_NXT_COVER
178 * get only first more specific prefix even if it have more specifics
180 * pointer to the next more specific prefix
181 * NULL if there is no prefixes left
183 struct rte_rib6_node *
184 rte_rib6_get_nxt(struct rte_rib6 *rib,
185 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
186 uint8_t depth, struct rte_rib6_node *last, int flag);
189 * Remove prefix from the RIB
194 * net to be removed from the RIB
199 rte_rib6_remove(struct rte_rib6 *rib,
200 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
203 * Insert prefix into the RIB
208 * net to be inserted to the RIB
212 * pointer to new rte_rib6_node on success
215 struct rte_rib6_node *
216 rte_rib6_insert(struct rte_rib6 *rib,
217 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth);
220 * Get an ip from rte_rib6_node
223 * pointer to the rib6 node
225 * pointer to the ipv6 to save
228 * -1 on failure with rte_errno indicating reason for failure.
231 rte_rib6_get_ip(const struct rte_rib6_node *node,
232 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE]);
235 * Get a depth from rte_rib6_node
238 * pointer to the rib6 node
240 * pointer to the depth to save
243 * -1 on failure with rte_errno indicating reason for failure.
246 rte_rib6_get_depth(const struct rte_rib6_node *node, uint8_t *depth);
249 * Get ext field from the rte_rib6_node
250 * It is caller responsibility to make sure there are necessary space
251 * for the ext field inside rib6 node.
254 * pointer to the rte_rib6_node
259 rte_rib6_get_ext(struct rte_rib6_node *node);
262 * Get nexthop from the rte_rib6_node
265 * pointer to the rib6 node
267 * pointer to the nexthop to save
270 * -1 on failure, with rte_errno indicating reason for failure.
273 rte_rib6_get_nh(const struct rte_rib6_node *node, uint64_t *nh);
276 * Set nexthop into the rte_rib6_node
279 * pointer to the rib6 node
281 * nexthop value to set to the rib6 node
284 * -1 on failure, with rte_errno indicating reason for failure.
287 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh);
295 * NUMA socket ID for RIB table memory allocation
297 * Structure containing the configuration
299 * Pointer to RIB object on success
300 * NULL otherwise with rte_errno indicating reason for failure.
303 rte_rib6_create(const char *name, int socket_id,
304 const struct rte_rib6_conf *conf);
307 * Find an existing RIB object and return a pointer to it.
310 * Name of the rib object as passed to rte_rib_create()
312 * Pointer to RIB object on success
313 * NULL otherwise with rte_errno indicating reason for failure.
316 rte_rib6_find_existing(const char *name);
319 * Free an RIB object.
327 rte_rib6_free(struct rte_rib6 *rib);
333 #endif /* _RTE_RIB6_H_ */