1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
10 #include <rte_eal_memconfig.h>
11 #include <rte_tailq.h>
12 #include <rte_errno.h>
13 #include <rte_rwlock.h>
14 #include <rte_malloc.h>
15 #include <rte_string_fns.h>
22 TAILQ_HEAD(rte_fib6_list, rte_tailq_entry);
23 static struct rte_tailq_elem rte_fib6_tailq = {
26 EAL_REGISTER_TAILQ(rte_fib6_tailq)
28 /* Maximum length of a FIB name. */
29 #define FIB6_NAMESIZE 64
31 #if defined(RTE_LIBRTE_FIB_DEBUG)
32 #define FIB6_RETURN_IF_TRUE(cond, retval) do { \
37 #define FIB6_RETURN_IF_TRUE(cond, retval)
41 char name[FIB6_NAMESIZE];
42 enum rte_fib6_type type; /**< Type of FIB struct */
43 struct rte_rib6 *rib; /**< RIB helper datastruct */
44 void *dp; /**< pointer to the dataplane struct*/
45 rte_fib6_lookup_fn_t lookup; /**< fib lookup function */
46 rte_fib6_modify_fn_t modify; /**< modify fib datastruct */
51 dummy_lookup(void *fib_p, uint8_t ips[][RTE_FIB6_IPV6_ADDR_SIZE],
52 uint64_t *next_hops, const unsigned int n)
55 struct rte_fib6 *fib = fib_p;
56 struct rte_rib6_node *node;
58 for (i = 0; i < n; i++) {
59 node = rte_rib6_lookup(fib->rib, ips[i]);
61 rte_rib6_get_nh(node, &next_hops[i]);
63 next_hops[i] = fib->def_nh;
68 dummy_modify(struct rte_fib6 *fib, const uint8_t ip[RTE_FIB6_IPV6_ADDR_SIZE],
69 uint8_t depth, uint64_t next_hop, int op)
71 struct rte_rib6_node *node;
72 if ((fib == NULL) || (depth > RTE_FIB6_MAXDEPTH))
75 node = rte_rib6_lookup_exact(fib->rib, ip, depth);
80 node = rte_rib6_insert(fib->rib, ip, depth);
83 return rte_rib6_set_nh(node, next_hop);
87 rte_rib6_remove(fib->rib, ip, depth);
94 init_dataplane(struct rte_fib6 *fib, __rte_unused int socket_id,
95 struct rte_fib6_conf *conf)
97 char dp_name[sizeof(void *)];
99 snprintf(dp_name, sizeof(dp_name), "%p", fib);
100 switch (conf->type) {
103 fib->lookup = dummy_lookup;
104 fib->modify = dummy_modify;
107 fib->dp = trie_create(dp_name, socket_id, conf);
110 fib->lookup = rte_trie_get_lookup_fn(conf);
111 fib->modify = trie_modify;
120 rte_fib6_add(struct rte_fib6 *fib, const uint8_t ip[RTE_FIB6_IPV6_ADDR_SIZE],
121 uint8_t depth, uint64_t next_hop)
123 if ((fib == NULL) || (ip == NULL) || (fib->modify == NULL) ||
124 (depth > RTE_FIB6_MAXDEPTH))
126 return fib->modify(fib, ip, depth, next_hop, RTE_FIB6_ADD);
130 rte_fib6_delete(struct rte_fib6 *fib, const uint8_t ip[RTE_FIB6_IPV6_ADDR_SIZE],
133 if ((fib == NULL) || (ip == NULL) || (fib->modify == NULL) ||
134 (depth > RTE_FIB6_MAXDEPTH))
136 return fib->modify(fib, ip, depth, 0, RTE_FIB6_DEL);
140 rte_fib6_lookup_bulk(struct rte_fib6 *fib,
141 uint8_t ips[][RTE_FIB6_IPV6_ADDR_SIZE],
142 uint64_t *next_hops, int n)
144 FIB6_RETURN_IF_TRUE((fib == NULL) || (ips == NULL) ||
145 (next_hops == NULL) || (fib->lookup == NULL), -EINVAL);
146 fib->lookup(fib->dp, ips, next_hops, n);
151 rte_fib6_create(const char *name, int socket_id, struct rte_fib6_conf *conf)
153 char mem_name[FIB6_NAMESIZE];
155 struct rte_fib6 *fib = NULL;
156 struct rte_rib6 *rib = NULL;
157 struct rte_tailq_entry *te;
158 struct rte_fib6_list *fib_list;
159 struct rte_rib6_conf rib_conf;
161 /* Check user arguments. */
162 if ((name == NULL) || (conf == NULL) || (conf->max_routes < 0) ||
163 (conf->type > RTE_FIB6_TRIE)) {
169 rib_conf.max_nodes = conf->max_routes * 2;
171 rib = rte_rib6_create(name, socket_id, &rib_conf);
174 "Can not allocate RIB %s\n", name);
178 snprintf(mem_name, sizeof(mem_name), "FIB6_%s", name);
179 fib_list = RTE_TAILQ_CAST(rte_fib6_tailq.head, rte_fib6_list);
181 rte_mcfg_tailq_write_lock();
183 /* guarantee there's no existing */
184 TAILQ_FOREACH(te, fib_list, next) {
185 fib = (struct rte_fib6 *)te->data;
186 if (strncmp(name, fib->name, FIB6_NAMESIZE) == 0)
195 /* allocate tailq entry */
196 te = rte_zmalloc("FIB_TAILQ_ENTRY", sizeof(*te), 0);
199 "Can not allocate tailq entry for FIB %s\n", name);
204 /* Allocate memory to store the FIB data structures. */
205 fib = rte_zmalloc_socket(mem_name,
206 sizeof(struct rte_fib6), RTE_CACHE_LINE_SIZE, socket_id);
208 RTE_LOG(ERR, LPM, "FIB %s memory allocation failed\n", name);
213 rte_strlcpy(fib->name, name, sizeof(fib->name));
215 fib->type = conf->type;
216 fib->def_nh = conf->default_nh;
217 ret = init_dataplane(fib, socket_id, conf);
220 "FIB dataplane struct %s memory allocation failed\n",
226 te->data = (void *)fib;
227 TAILQ_INSERT_TAIL(fib_list, te, next);
229 rte_mcfg_tailq_write_unlock();
238 rte_mcfg_tailq_write_unlock();
245 rte_fib6_find_existing(const char *name)
247 struct rte_fib6 *fib = NULL;
248 struct rte_tailq_entry *te;
249 struct rte_fib6_list *fib_list;
251 fib_list = RTE_TAILQ_CAST(rte_fib6_tailq.head, rte_fib6_list);
253 rte_mcfg_tailq_read_lock();
254 TAILQ_FOREACH(te, fib_list, next) {
255 fib = (struct rte_fib6 *) te->data;
256 if (strncmp(name, fib->name, FIB6_NAMESIZE) == 0)
259 rte_mcfg_tailq_read_unlock();
270 free_dataplane(struct rte_fib6 *fib)
283 rte_fib6_free(struct rte_fib6 *fib)
285 struct rte_tailq_entry *te;
286 struct rte_fib6_list *fib_list;
291 fib_list = RTE_TAILQ_CAST(rte_fib6_tailq.head, rte_fib6_list);
293 rte_mcfg_tailq_write_lock();
295 /* find our tailq entry */
296 TAILQ_FOREACH(te, fib_list, next) {
297 if (te->data == (void *)fib)
301 TAILQ_REMOVE(fib_list, te, next);
303 rte_mcfg_tailq_write_unlock();
306 rte_rib6_free(fib->rib);
312 rte_fib6_get_dp(struct rte_fib6 *fib)
314 return (fib == NULL) ? NULL : fib->dp;
318 rte_fib6_get_rib(struct rte_fib6 *fib)
320 return (fib == NULL) ? NULL : fib->rib;