Changeset View
Changeset View
Standalone View
Standalone View
sys/netinet/in_fib_algo.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2020 Alexander V. Chernikov | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <sys/param.h> | |||||
#include <sys/kernel.h> | |||||
#include <sys/lock.h> | |||||
#include <sys/rmlock.h> | |||||
#include <sys/malloc.h> | |||||
#include <sys/kernel.h> | |||||
#include <sys/priv.h> | |||||
#include <sys/socket.h> | |||||
#include <sys/sysctl.h> | |||||
#include <net/vnet.h> | |||||
#include <net/if.h> | |||||
#include <netinet/in.h> | |||||
#include <net/route.h> | |||||
#include <net/route/nhop.h> | |||||
#include <net/route/route_ctl.h> | |||||
#include <net/route/route_var.h> | |||||
#include <net/route/route_algo.h> | |||||
#define KEY_LEN_INET (offsetof(struct sockaddr_in, sin_addr) + sizeof(in_addr_t)) | |||||
#define OFF_LEN_INET (8 * offsetof(struct sockaddr_in, sin_addr)) | |||||
struct radix4_addr_entry { | |||||
struct radix_node rn[2]; | |||||
struct sockaddr_in addr; | |||||
struct nhop_object *nhop; | |||||
}; | |||||
#define LRADIX4_ITEM_SZ roundup2(sizeof(struct radix4_addr_entry), 64) | |||||
struct lradix4_data { | |||||
struct radix_node_head *rnh; | |||||
struct fib_data *fd; | |||||
void *mem; | |||||
uint32_t alloc_items; | |||||
uint32_t num_items; | |||||
}; | |||||
static struct nhop_object * | |||||
lradix4_lookup(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid) | |||||
{ | |||||
struct radix_node_head *rnh = (struct radix_node_head *)algo_data; | |||||
struct radix4_addr_entry *ent; | |||||
struct sockaddr_in addr4 = { | |||||
.sin_len = KEY_LEN_INET, | |||||
.sin_addr = key.addr4, | |||||
}; | |||||
ent = (struct radix4_addr_entry *)(rnh->rnh_matchaddr(&addr4, &rnh->rh)); | |||||
if (ent != NULL) | |||||
return (ent->nhop); | |||||
return (NULL); | |||||
} | |||||
static uint8_t | |||||
lradix4_get_pref(const struct rib_rtable_info *rinfo) | |||||
{ | |||||
if (rinfo->num_prefixes < 10) | |||||
return (255); | |||||
else if (rinfo->num_prefixes < 100000) | |||||
return (255 - rinfo->num_prefixes / 394); | |||||
else | |||||
return (1); | |||||
} | |||||
static enum flm_op_result | |||||
lradix4_init(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **_data) | |||||
{ | |||||
struct lradix4_data *lr; | |||||
struct rib_rtable_info rinfo; | |||||
uint32_t count; | |||||
lr = malloc(sizeof(struct lradix4_data), M_RTABLE, M_NOWAIT | M_ZERO); | |||||
if (lr == NULL || !rn_inithead((void **)&lr->rnh, OFF_LEN_INET)) | |||||
return (FLM_REBUILD); | |||||
fib_get_rtable_info(fib_get_rh(fd), &rinfo); | |||||
count = rinfo.num_prefixes * 11 / 10; | |||||
// XXX: alignment! | |||||
lr->mem = malloc(count * LRADIX4_ITEM_SZ, M_RTABLE, M_NOWAIT | M_ZERO); | |||||
if (lr->mem == NULL) | |||||
return (FLM_REBUILD); | |||||
lr->alloc_items = count; | |||||
lr->fd = fd; | |||||
*_data = lr; | |||||
return (FLM_SUCCESS); | |||||
} | |||||
static void | |||||
lradix4_destroy(void *_data) | |||||
{ | |||||
struct lradix4_data *lr = (struct lradix4_data *)_data; | |||||
if (lr->rnh != NULL) | |||||
rn_detachhead((void **)&lr->rnh); | |||||
if (lr->mem != NULL) | |||||
free(lr->mem, M_RTABLE); | |||||
free(lr, M_RTABLE); | |||||
} | |||||
static enum flm_op_result | |||||
lradix4_add_route_cb(struct rtentry *rt, void *_data) | |||||
{ | |||||
struct lradix4_data *lr = (struct lradix4_data *)_data; | |||||
struct radix4_addr_entry *ae; | |||||
struct sockaddr_in *rt_dst, *rt_mask, mask; | |||||
struct radix_node *rn; | |||||
if (fib_get_nhop_idx(lr->fd, rt->rt_nhop) == 0) | |||||
return (FLM_REBUILD); | |||||
if (lr->num_items >= lr->alloc_items) | |||||
return (FLM_REBUILD); | |||||
ae = (struct radix4_addr_entry *)((char *)lr->mem + lr->num_items * LRADIX4_ITEM_SZ); | |||||
lr->num_items++; | |||||
ae->nhop = rt->rt_nhop; | |||||
rt_dst = (struct sockaddr_in *)rt_key(rt); | |||||
rt_mask = (struct sockaddr_in *)rt_mask(rt); | |||||
ae->addr.sin_len = KEY_LEN_INET; | |||||
ae->addr.sin_addr = rt_dst->sin_addr; | |||||
if (rt_mask != NULL) { | |||||
bzero(&mask, sizeof(mask)); | |||||
mask.sin_len = KEY_LEN_INET; | |||||
mask.sin_addr = rt_mask->sin_addr; | |||||
rt_mask = &mask; | |||||
} | |||||
rn = lr->rnh->rnh_addaddr((struct sockaddr *)&ae->addr, | |||||
olivier: Where rt_get_raw_nhop() is defined ?
```
--- in_fib_algo.o… | |||||
Done Inline ActionsAdded in r 368317 melifaro: Added in r 368317 | |||||
(struct sockaddr *)rt_mask, &lr->rnh->rh, ae->rn); | |||||
if (rn == NULL) | |||||
return (FLM_REBUILD); | |||||
return (FLM_SUCCESS); | |||||
} | |||||
static enum flm_op_result | |||||
lradix4_end_dump(void *_data, struct fib_dp *dp) | |||||
{ | |||||
Not Done Inline ActionsSame here: /usr/src/sys/netinet/in_fib_algo.c:174:2: error: implicit declaration of function 'rt_get_inet_prefix_pmask' is invalid in C99 [-Werror,-Wimplicit-function-declaration] rt_get_inet_prefix_pmask(rt, &addr4, &mask4, &scopeid); olivier: Same here:
```
/usr/src/sys/netinet/in_fib_algo.c:174:2: error: implicit declaration of… | |||||
Done Inline ActionsSorry, should have stated that rebase to recent head is required :-( melifaro: Sorry, should have stated that rebase to recent head is required :-(
Added in r368317 | |||||
struct lradix4_data *lr = (struct lradix4_data *)_data; | |||||
dp->f = lradix4_lookup; | |||||
dp->arg = lr->rnh; | |||||
return (FLM_SUCCESS); | |||||
} | |||||
static enum flm_op_result | |||||
lradix4_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc, | |||||
void *_data) | |||||
{ | |||||
return (FLM_REBUILD); | |||||
} | |||||
struct fib_lookup_module flm_radix4_lockless = { | |||||
.flm_name = "radix4_lockless", | |||||
.flm_family = AF_INET, | |||||
.flm_init_cb = lradix4_init, | |||||
.flm_destroy_cb = lradix4_destroy, | |||||
.flm_dump_rib_item_cb = lradix4_add_route_cb, | |||||
.flm_dump_end_cb = lradix4_end_dump, | |||||
.flm_change_rib_item_cb = lradix4_change_cb, | |||||
.flm_get_pref = lradix4_get_pref, | |||||
}; | |||||
struct radix4_data { | |||||
struct fib_data *fd; | |||||
struct rib_head *rh; | |||||
}; | |||||
static struct nhop_object * | |||||
radix4_lookup(void *algo_data, const struct flm_lookup_key key, uint32_t scopeid) | |||||
{ | |||||
RIB_RLOCK_TRACKER; | |||||
struct rib_head *rh = (struct rib_head *)algo_data; | |||||
struct radix_node *rn; | |||||
struct nhop_object *nh; | |||||
/* Prepare lookup key */ | |||||
struct sockaddr_in sin4 = { | |||||
.sin_family = AF_INET, | |||||
.sin_len = sizeof(struct sockaddr_in), | |||||
.sin_addr = key.addr4, | |||||
}; | |||||
nh = NULL; | |||||
RIB_RLOCK(rh); | |||||
rn = rh->rnh_matchaddr((void *)&sin4, &rh->head); | |||||
if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) | |||||
nh = (RNTORT(rn))->rt_nhop; | |||||
RIB_RUNLOCK(rh); | |||||
return (nh); | |||||
} | |||||
static uint8_t | |||||
radix4_get_pref(const struct rib_rtable_info *rinfo) | |||||
{ | |||||
return (50); | |||||
} | |||||
static enum flm_op_result | |||||
radix4_init(uint32_t fibnum, struct fib_data *fd, void *_old_data, void **_data) | |||||
{ | |||||
struct radix4_data *r4; | |||||
r4 = malloc(sizeof(struct radix4_data), M_RTABLE, M_NOWAIT | M_ZERO); | |||||
if (r4 == NULL) | |||||
return (FLM_REBUILD); | |||||
r4->fd = fd; | |||||
r4->rh = fib_get_rh(fd); | |||||
if (r4->rh == NULL) | |||||
return (FLM_ERROR); | |||||
*_data = r4; | |||||
return (FLM_SUCCESS); | |||||
} | |||||
static void | |||||
radix4_destroy(void *_data) | |||||
{ | |||||
free(_data, M_RTABLE); | |||||
} | |||||
static enum flm_op_result | |||||
radix4_end_dump(void *_data, struct fib_dp *dp) | |||||
{ | |||||
struct radix4_data *r4 = (struct radix4_data *)_data; | |||||
dp->f = radix4_lookup; | |||||
dp->arg = r4->rh; | |||||
return (FLM_SUCCESS); | |||||
} | |||||
static enum flm_op_result | |||||
radix4_change_cb(struct rib_head *rnh, struct rib_cmd_info *rc, | |||||
void *_data) | |||||
{ | |||||
struct radix4_data *r4 = (struct radix4_data *)_data; | |||||
/* | |||||
* Grab additional reference for each nexthop to maintain guarantee | |||||
* that we have non-zero # of reference for each nexthop in radix in | |||||
* the epoch. | |||||
*/ | |||||
if (rc->rc_nh_new != NULL) { | |||||
if (fib_get_nhop_idx(r4->fd, rc->rc_nh_new) == 0) | |||||
return (FLM_REBUILD); | |||||
} | |||||
if (rc->rc_nh_old != NULL) | |||||
fib_free_nhop(r4->fd, rc->rc_nh_old); | |||||
return (FLM_SUCCESS); | |||||
} | |||||
struct fib_lookup_module flm_radix4 = { | |||||
.flm_name = "radix4", | |||||
.flm_family = AF_INET, | |||||
.flm_init_cb = radix4_init, | |||||
.flm_destroy_cb = radix4_destroy, | |||||
.flm_dump_end_cb = radix4_end_dump, | |||||
.flm_change_rib_item_cb = radix4_change_cb, | |||||
.flm_get_pref = radix4_get_pref, | |||||
}; | |||||
static void | |||||
fib4_algo_init(void) | |||||
{ | |||||
fib_module_register(&flm_radix4_lockless); | |||||
fib_module_register(&flm_radix4); | |||||
} | |||||
SYSINIT(fib4_algo_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, fib4_algo_init, NULL); |
Where rt_get_raw_nhop() is defined ?