2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include "opt_inet6.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/rmlock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/syslog.h>
45 #include <net/if_var.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip6.h>
50 #include <netinet6/ip6_var.h>
51 #include <netinet6/in6_fib.h>
53 #include <net/route.h>
54 #include <net/route/nhop.h>
55 #include <net/route/route_ctl.h>
56 #include <net/route/fib_algo.h>
61 #define LPM6_MIN_TBL8 8 /* 2 pages of memory */
62 #define LPM6_MAX_TBL8 65536 * 16 /* 256M */
64 struct fib_algo_calldata {
69 struct dpdk_lpm6_data {
70 struct rte_lpm6 *lpm6;
71 uint64_t routes_added;
72 uint64_t routes_failed;
73 uint32_t number_tbl8s;
79 static struct nhop_object *
80 lookup_ptr_ll(const struct rte_lpm6 *lpm6, const struct in6_addr *dst6,
83 const struct rte_lpm6_external *rte_ext;
85 rte_ext = (const struct rte_lpm6_external *)lpm6;
87 return (fib6_radix_lookup_nh(rte_ext->fibnum, dst6, scopeid));
91 * Main datapath routing
93 static struct nhop_object *
94 lookup_ptr(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid)
96 const struct rte_lpm6 *lpm6;
97 const struct rte_lpm6_external *rte_ext;
98 const struct in6_addr *addr6;
102 lpm6 = (const struct rte_lpm6 *)algo_data;
104 rte_ext = (const struct rte_lpm6_external *)lpm6;
106 if (!IN6_IS_SCOPE_LINKLOCAL(addr6)) {
107 ret = rte_lpm6_lookup(lpm6, (const uint8_t *)addr6, &nhidx);
110 return (rte_ext->nh_idx[nhidx]);
112 /* Not found. Check default route */
113 if (rte_ext->default_idx > 0)
114 return (rte_ext->nh_idx[rte_ext->default_idx]);
120 return (lookup_ptr_ll(lpm6, addr6, scopeid));
125 rte6_get_pref(const struct rib_rtable_info *rinfo)
128 if (rinfo->num_prefixes < 10)
130 else if (rinfo->num_prefixes < 1000)
131 return (rinfo->num_prefixes / 10);
132 else if (rinfo->num_prefixes < 500000)
133 return (100 + rinfo->num_prefixes / 3334);
138 static enum flm_op_result
139 handle_default_change(struct dpdk_lpm6_data *dd, struct rib_cmd_info *rc)
141 struct rte_lpm6_external *rte_ext;
142 rte_ext = (struct rte_lpm6_external *)dd->lpm6;
144 if (rc->rc_cmd != RTM_DELETE) {
146 uint32_t nhidx = fib_get_nhop_idx(dd->fd, rc->rc_nh_new);
149 return (FLM_REBUILD);
150 rte_ext->default_idx = nhidx;
152 /* No default route */
153 rte_ext->default_idx = 0;
156 return (FLM_SUCCESS);
159 static enum flm_op_result
160 handle_ll_change(struct dpdk_lpm6_data *dd, struct rib_cmd_info *rc,
161 const struct in6_addr addr6, int plen, uint32_t scopeid)
164 return (FLM_SUCCESS);
167 static struct rte_lpm6_rule *
168 pack_parent_rule(struct dpdk_lpm6_data *dd, const struct in6_addr *addr6,
171 struct rte_lpm6_rule *lsp_rule = NULL;
172 struct route_nhop_data rnd;
176 rt = fib6_lookup_rt(dd->fibnum, addr6, 0, NHR_UNLOCKED, &rnd);
177 /* plen = 0 means default route and it's out of scope */
180 struct in6_addr new_addr6;
181 rt_get_inet6_prefix_plen(rt, &new_addr6, &plen, &scopeid);
183 uint32_t nhidx = fib_get_nhop_idx(dd->fd, rnd.rnd_nhop);
186 * shouldn't happen as we already have parent route.
187 * It will trigger rebuild automatically.
191 lsp_rule = fill_rule6(buffer, (uint8_t *)&new_addr6, plen, nhidx);
198 static enum flm_op_result
199 handle_gu_change(struct dpdk_lpm6_data *dd, const struct rib_cmd_info *rc,
200 const struct in6_addr *addr6, int plen)
203 char abuf[INET6_ADDRSTRLEN];
204 inet_ntop(AF_INET6, addr6, abuf, sizeof(abuf));
206 /* So we get sin6, plen and nhidx */
207 if (rc->rc_cmd != RTM_DELETE) {
209 * Addition or change. Save nhop in the internal table
212 uint32_t nhidx = fib_get_nhop_idx(dd->fd, rc->rc_nh_new);
214 FIB_PRINTF(LOG_INFO, dd->fd, "nhop limit reached, need rebuild");
215 return (FLM_REBUILD);
218 ret = rte_lpm6_add(dd->lpm6, (const uint8_t *)addr6,
219 plen, nhidx, (rc->rc_cmd == RTM_ADD) ? 1 : 0);
220 FIB_PRINTF(LOG_DEBUG, dd->fd, "DPDK GU: %s %s/%d nhop %u = %d",
221 (rc->rc_cmd == RTM_ADD) ? "ADD" : "UPDATE",
222 abuf, plen, nhidx, ret);
225 * Need to lookup parent. Assume deletion happened already
227 char buffer[RTE_LPM6_RULE_SIZE];
228 struct rte_lpm6_rule *lsp_rule = NULL;
229 lsp_rule = pack_parent_rule(dd, addr6, buffer);
231 ret = rte_lpm6_delete(dd->lpm6, (const uint8_t *)addr6, plen, lsp_rule);
232 FIB_PRINTF(LOG_DEBUG, dd->fd, "DPDK GU: %s %s/%d nhop ? = %d",
233 "DEL", abuf, plen, ret);
237 FIB_PRINTF(LOG_INFO, dd->fd, "error: %d", ret);
239 return (FLM_REBUILD);
242 return (FLM_SUCCESS);
245 static enum flm_op_result
246 handle_any_change(struct dpdk_lpm6_data *dd, struct rib_cmd_info *rc)
248 enum flm_op_result ret;
249 struct in6_addr addr6;
253 rt_get_inet6_prefix_plen(rc->rc_rt, &addr6, &plen, &scopeid);
255 if (IN6_IS_SCOPE_LINKLOCAL(&addr6))
256 ret = handle_ll_change(dd, rc, addr6, plen, scopeid);
258 ret = handle_default_change(dd, rc);
260 ret = handle_gu_change(dd, rc, &addr6, plen);
263 FIB_PRINTF(LOG_INFO, dd->fd, "error handling route");
267 static enum flm_op_result
268 handle_rtable_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc,
271 struct dpdk_lpm6_data *dd;
273 dd = (struct dpdk_lpm6_data *)_data;
275 return (handle_any_change(dd, rc));
279 destroy_dd(struct dpdk_lpm6_data *dd)
282 FIB_PRINTF(LOG_INFO, dd->fd, "destroy dd %p", dd);
283 if (dd->lpm6 != NULL)
284 rte_lpm6_free(dd->lpm6);
289 destroy_table(void *_data)
292 destroy_dd((struct dpdk_lpm6_data *)_data);
295 static enum flm_op_result
296 add_route_cb(struct rtentry *rt, void *_data)
298 struct dpdk_lpm6_data *dd = (struct dpdk_lpm6_data *)_data;
299 struct in6_addr addr6;
300 struct nhop_object *nh;
305 rt_get_inet6_prefix_plen(rt, &addr6, &plen, &scopeid);
306 nh = rt_get_raw_nhop(rt);
308 if (IN6_IS_SCOPE_LINKLOCAL(&addr6)) {
311 * We don't operate on LL directly, however
312 * reference them to maintain guarantee on
313 * ability to refcount nhops in epoch.
315 fib_get_nhop_idx(dd->fd, nh);
316 return (FLM_SUCCESS);
319 char abuf[INET6_ADDRSTRLEN];
320 inet_ntop(AF_INET6, &addr6, abuf, sizeof(abuf));
321 FIB_PRINTF(LOG_DEBUG, dd->fd, "Operating on %s/%d", abuf, plen);
324 struct rib_cmd_info rc = {
329 FIB_PRINTF(LOG_DEBUG, dd->fd, "Adding default route");
330 return (handle_default_change(dd, &rc));
333 uint32_t nhidx = fib_get_nhop_idx(dd->fd, nh);
335 FIB_PRINTF(LOG_INFO, dd->fd, "unable to get nhop index");
336 return (FLM_REBUILD);
338 ret = rte_lpm6_add(dd->lpm6, (const uint8_t *)&addr6, plen, nhidx, 1);
339 FIB_PRINTF(LOG_DEBUG, dd->fd, "ADD %p %s/%d nh %u = %d",
340 dd->lpm6, abuf, plen, nhidx, ret);
343 FIB_PRINTF(LOG_INFO, dd->fd, "rte_lpm6_add() returned %d", ret);
344 if (ret == -ENOSPC) {
346 return (FLM_REBUILD);
353 return (FLM_SUCCESS);
356 static enum flm_op_result
357 check_dump_success(void *_data, struct fib_dp *dp)
359 struct dpdk_lpm6_data *dd;
361 dd = (struct dpdk_lpm6_data *)_data;
363 FIB_PRINTF(LOG_INFO, dd->fd, "scan completed. added: %zu failed: %zu",
364 dd->routes_added, dd->routes_failed);
365 if (dd->hit_tables || dd->routes_failed > 0)
366 return (FLM_REBUILD);
368 FIB_PRINTF(LOG_INFO, dd->fd,
369 "DPDK lookup engine synced with IPv6 RIB id %u, %zu routes",
370 dd->fibnum, dd->routes_added);
375 return (FLM_SUCCESS);
379 estimate_scale(const struct dpdk_lpm6_data *dd_src, struct dpdk_lpm6_data *dd)
382 /* XXX: update at 75% capacity */
383 if (dd_src->hit_tables)
384 dd->number_tbl8s = dd_src->number_tbl8s * 2;
386 dd->number_tbl8s = dd_src->number_tbl8s;
388 /* TODO: look into the appropriate RIB to adjust */
391 static struct dpdk_lpm6_data *
392 build_table(struct dpdk_lpm6_data *dd_prev, struct fib_data *fd)
394 struct dpdk_lpm6_data *dd;
395 struct rte_lpm6 *lpm6;
397 dd = malloc(sizeof(struct dpdk_lpm6_data), M_TEMP, M_NOWAIT | M_ZERO);
399 FIB_PRINTF(LOG_INFO, fd, "Unable to allocate base datastructure");
402 dd->fibnum = dd_prev->fibnum;
405 estimate_scale(dd_prev, dd);
407 struct rte_lpm6_config cfg = {.number_tbl8s = dd->number_tbl8s};
408 lpm6 = rte_lpm6_create("test", 0, &cfg);
410 FIB_PRINTF(LOG_INFO, fd, "unable to create lpm6");
415 struct rte_lpm6_external *ext = (struct rte_lpm6_external *)lpm6;
416 ext->nh_idx = fib_get_nhop_array(dd->fd);
418 FIB_PRINTF(LOG_INFO, fd, "allocated %u tbl8s", dd->number_tbl8s);
423 static enum flm_op_result
424 init_table(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **data)
426 struct dpdk_lpm6_data *dd, dd_base;
428 if (_old_data == NULL) {
429 bzero(&dd_base, sizeof(struct dpdk_lpm6_data));
430 dd_base.fibnum = fibnum;
431 /* TODO: get rib statistics */
432 dd_base.number_tbl8s = LPM6_MIN_TBL8;
435 FIB_PRINTF(LOG_INFO, fd, "Starting with old data");
436 dd = (struct dpdk_lpm6_data *)_old_data;
439 /* Guaranteed to be in epoch */
440 dd = build_table(dd, fd);
442 FIB_PRINTF(LOG_INFO, fd, "table creation failed");
443 return (FLM_REBUILD);
447 return (FLM_SUCCESS);
450 static struct fib_lookup_module dpdk_lpm6 = {
451 .flm_name = "dpdk_lpm6",
452 .flm_family = AF_INET6,
453 .flm_init_cb = init_table,
454 .flm_destroy_cb = destroy_table,
455 .flm_dump_rib_item_cb = add_route_cb,
456 .flm_dump_end_cb = check_dump_success,
457 .flm_change_rib_item_cb = handle_rtable_change_cb,
458 .flm_get_pref = rte6_get_pref,
462 lpm6_modevent(module_t mod, int type, void *unused)
468 fib_module_register(&dpdk_lpm6);
471 error = fib_module_unregister(&dpdk_lpm6);
480 static moduledata_t lpm6mod = {
486 DECLARE_MODULE(lpm6mod, lpm6mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
487 MODULE_VERSION(lpm6mod, 1);