1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

gnrc_ipv6_nib: provide internal implementation for off-link entries

This commit is contained in:
Martine Lenders 2017-06-20 10:23:20 +02:00
parent a6fcbcde9c
commit f249c0c2a0
No known key found for this signature in database
GPG Key ID: 8E97A9FE55F25D62
3 changed files with 160 additions and 51 deletions

View File

@ -184,6 +184,16 @@ extern "C" {
#define GNRC_IPV6_NIB_NUMOF (4)
#endif
/**
* @brief Number of off-link entries in NIB
*
* @attention This number is equal to the maximum number of forwarding table
* and prefix list entries in NIB
*/
#ifndef GNRC_IPV6_NIB_OFFL_NUMOF
#define GNRC_IPV6_NIB_OFFL_NUMOF (8)
#endif
#ifdef __cplusplus
}
#endif

View File

@ -33,6 +33,7 @@ static _nib_dr_entry_t *_prime_def_router = NULL;
static clist_node_t _next_removable = { NULL };
static _nib_onl_entry_t _nodes[GNRC_IPV6_NIB_NUMOF];
static _nib_offl_entry_t _dsts[GNRC_IPV6_NIB_OFFL_NUMOF];
static _nib_dr_entry_t _def_routers[GNRC_IPV6_NIB_DEFAULT_ROUTER_NUMOF];
static _nib_iface_t _nis[GNRC_NETIF_NUMOF];
@ -53,33 +54,41 @@ void _nib_init(void)
_next_removable.next = NULL;
memset(_nodes, 0, sizeof(_nodes));
memset(_def_routers, 0, sizeof(_def_routers));
memset(_dsts, 0, sizeof(_dsts));
memset(_nis, 0, sizeof(_nis));
#endif
/* TODO: load ABR information from persistent memory */
}
static inline bool _addr_equals(const ipv6_addr_t *addr,
const _nib_onl_entry_t *node)
{
return (addr == NULL) || ipv6_addr_is_unspecified(&node->ipv6) ||
(ipv6_addr_equal(addr, &node->ipv6));
}
_nib_onl_entry_t *_nib_onl_alloc(const ipv6_addr_t *addr, unsigned iface)
{
_nib_onl_entry_t *node = NULL;
assert(addr != NULL);
DEBUG("nib: Allocating on-link node entry (addr = %s, iface = %u)\n",
ipv6_addr_to_str(addr_str, addr, sizeof(addr_str)), iface);
(addr == NULL) ? "NULL" : ipv6_addr_to_str(addr_str, addr,
sizeof(addr_str)), iface);
for (unsigned i = 0; i < GNRC_IPV6_NIB_NUMOF; i++) {
_nib_onl_entry_t *tmp = &_nodes[i];
if ((_nib_onl_get_if(tmp) == iface) &&
(ipv6_addr_equal(addr, &tmp->ipv6))) {
if ((_nib_onl_get_if(tmp) == iface) && _addr_equals(addr, tmp)) {
/* exact match */
DEBUG(" %p is an exact match\n", (void *)tmp);
return tmp;
node = tmp;
break;
}
if ((node == NULL) && (tmp->mode == _EMPTY)) {
DEBUG(" using %p\n", (void *)node);
node = tmp;
}
}
if (node != NULL) {
DEBUG(" using %p\n", (void *)node);
_override_node(addr, iface, node);
}
#if ENABLE_DEBUG
@ -141,6 +150,7 @@ static inline _nib_onl_entry_t *_cache_out_onl_entry(const ipv6_addr_t *addr,
_nib_onl_entry_t *_nib_nc_add(const ipv6_addr_t *addr, unsigned iface,
uint16_t cstate)
{
assert(addr != NULL);
cstate &= GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK;
assert(cstate != GNRC_IPV6_NIB_NC_INFO_NUD_STATE_DELAY);
assert(cstate != GNRC_IPV6_NIB_NC_INFO_NUD_STATE_PROBE);
@ -341,6 +351,95 @@ _nib_dr_entry_t *_nib_drl_get_dr(void)
return _prime_def_router;
}
_nib_offl_entry_t *_nib_offl_alloc(const ipv6_addr_t *next_hop, unsigned iface,
const ipv6_addr_t *pfx, unsigned pfx_len)
{
_nib_offl_entry_t *dst = NULL;
assert((pfx != NULL) && (!ipv6_addr_is_unspecified(pfx)) &&
(pfx_len > 0) && (pfx_len <= 128));
DEBUG("nib: Allocating off-link-entry entry "
"(next_hop = %s, iface = %u, ",
(next_hop == NULL) ? "NULL" : ipv6_addr_to_str(addr_str, next_hop,
sizeof(addr_str)),
iface);
DEBUG("pfx = %s/%u)\n", ipv6_addr_to_str(addr_str, pfx,
sizeof(addr_str)), pfx_len);
for (unsigned i = 0; i < GNRC_IPV6_NIB_OFFL_NUMOF; i++) {
_nib_offl_entry_t *tmp = &_dsts[i];
_nib_onl_entry_t *tmp_node = tmp->next_hop;
if ((tmp->pfx_len == pfx_len) && /* prefix length matches and */
(tmp_node != NULL) && /* there is a next hop that */
(_nib_onl_get_if(tmp_node) == iface) && /* has a matching interface and */
_addr_equals(next_hop, tmp_node) && /* equal address to next_hop, also */
(ipv6_addr_match_prefix(&tmp->pfx, pfx) >= pfx_len)) { /* the prefix matches */
/* exact match (or next hop address was previously unset) */
DEBUG(" %p is an exact match\n", (void *)tmp);
if (next_hop != NULL) {
memcpy(&tmp_node->ipv6, next_hop, sizeof(tmp_node->ipv6));
}
tmp->next_hop->mode |= _DST;
return tmp;
}
if ((dst == NULL) && (tmp_node == NULL)) {
dst = tmp;
}
}
if (dst != NULL) {
DEBUG(" using %p\n", (void *)dst);
dst->next_hop = _nib_onl_alloc(next_hop, iface);
if (dst->next_hop == NULL) {
memset(dst, 0, sizeof(_nib_offl_entry_t));
return NULL;
}
_override_node(next_hop, iface, dst->next_hop);
dst->next_hop->mode |= _DST;
ipv6_addr_init_prefix(&dst->pfx, pfx, pfx_len);
dst->pfx_len = pfx_len;
}
return dst;
}
static inline bool _in_dsts(const _nib_offl_entry_t *dst)
{
return (dst < (_dsts + GNRC_IPV6_NIB_OFFL_NUMOF));
}
void _nib_offl_clear(_nib_offl_entry_t *dst)
{
if (dst->next_hop != NULL) {
_nib_offl_entry_t *ptr;
for (ptr = _dsts; _in_dsts(ptr); ptr++) {
/* there is another dst pointing to next-hop => only remove dst */
if ((dst != ptr) && (dst->next_hop == ptr->next_hop)) {
break;
}
}
/* we iterated and found no further dst pointing to next-hop */
if (!_in_dsts(ptr)) {
dst->next_hop->mode &= ~(_DST);
_nib_onl_clear(dst->next_hop);
}
memset(dst, 0, sizeof(_nib_offl_entry_t));
}
}
_nib_offl_entry_t *_nib_offl_iter(const _nib_offl_entry_t *last)
{
for (const _nib_offl_entry_t *dst = (last) ? (last + 1) : _dsts;
_in_dsts(dst);
dst++) {
if (dst->mode != _EMPTY) {
/* const modifier provided to assure internal consistency.
* Can now be discarded. */
return (_nib_offl_entry_t *)dst;
}
}
return NULL;
}
_nib_iface_t *_nib_iface_get(unsigned iface)
{
_nib_iface_t *ni = NULL;
@ -367,7 +466,9 @@ static void _override_node(const ipv6_addr_t *addr, unsigned iface,
_nib_onl_entry_t *node)
{
_nib_onl_clear(node);
memcpy(&node->ipv6, addr, sizeof(node->ipv6));
if (addr != NULL) {
memcpy(&node->ipv6, addr, sizeof(node->ipv6));
}
_nib_onl_set_if(node, iface);
}

View File

@ -19,6 +19,7 @@
#ifndef PRIV_NIB_INTERNAL_H
#define PRIV_NIB_INTERNAL_H
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
@ -48,6 +49,8 @@ extern "C" {
#define _DRL (0x08) /**< default router list */
#define _FT (0x10) /**< forwarding table */
#define _DAD (0x20) /**< 6LoWPAN duplicate address detection table */
#define _DST (0x40) /**< there is @ref _nib_offl_entry_t pointing
to this @ref _nib_onl_entry_t */
/** @} */
/**
@ -148,6 +151,8 @@ typedef struct {
ipv6_addr_t pfx; /**< prefix to the destination */
unsigned pfx_len; /**< prefix-length in bits of
* _nib_onl_entry_t::pfx */
uint8_t mode; /**< [mode](@ref net_gnrc_ipv6_nib_mode) of the
* off-link entry */
} _nib_offl_entry_t;
/**
@ -232,10 +237,8 @@ static inline void _nib_onl_set_if(_nib_onl_entry_t *node, unsigned iface)
/**
* @brief Creates or gets an existing on-link entry by address
*
* @pre `(addr != NULL)`.
*
* @param[in] addr An IPv6 address. May not be NULL.
* *May also be a global address!*
* @param[in] addr An IPv6 address. May be NULL (to be pointed to by a prefix
* list entry). *May also be a global address!*
* @param[in] iface The interface to the node.
*
* @return A new or existing on-link entry with _nib_onl_entry_t::ipv6 set to
@ -286,7 +289,6 @@ _nib_onl_entry_t *_nib_onl_get(const ipv6_addr_t *addr, unsigned iface);
/**
* @brief Creates or gets an existing node from the neighbor cache by address
*
* @pre `(addr != NULL)`
* @pre `((cstate & GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK) !=
* GNRC_IPV6_NIB_NC_INFO_NUD_STATE_DELAY)`
* @pre `((cstate & GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK) !=
@ -294,7 +296,8 @@ _nib_onl_entry_t *_nib_onl_get(const ipv6_addr_t *addr, unsigned iface);
* @pre `((cstate & GNRC_IPV6_NIB_NC_INFO_NUD_STATE_MASK) !=
* GNRC_IPV6_NIB_NC_INFO_NUD_STATE_REACHABLE)`
*
* @param[in] addr The address of a node. May not be NULL.
* @param[in] addr The address of a node. May be NULL for prefix list
* entries.
* @param[in] iface The interface to the node.
* @param[in] cstate Creation state. State of the entry *if* the entry is
* newly created.
@ -333,6 +336,7 @@ void _nib_nc_set_reachable(_nib_onl_entry_t *node);
*/
static inline _nib_onl_entry_t *_nib_dad_add(const ipv6_addr_t *addr)
{
assert(addr != NULL);
_nib_onl_entry_t *node = _nib_onl_alloc(addr, 0);
if (node != NULL) {
@ -412,11 +416,10 @@ _nib_dr_entry_t *_nib_drl_get_dr(void);
/**
* @brief Creates or gets an existing off-link entry by next hop and prefix
*
* @pre `(next_hop != NULL)`
* @pre `(pfx != NULL) && (pfx != "::") && (pfx_len != 0) && (pfx_len <= 128)`
*
* @param[in] next_hop An IPv6 address to next hop. May not be NULL.
* *May also be a global address!*
* @param[in] next_hop An IPv6 address to next hop. May be NULL (for prefix
* list). *May also be a global address!*
* @param[in] iface The interface to @p next_hop.
* @param[in] pfx The IPv6 prefix or address of the destination.
* May not be NULL or unspecified address. Use
@ -427,15 +430,15 @@ _nib_dr_entry_t *_nib_drl_get_dr(void);
* @p pfx.
* @return NULL, if no space is left.
*/
_nib_offl_entry_t *_nib_dst_alloc(const ipv6_addr_t *next_hop, unsigned iface,
const ipv6_addr_t *pfx, unsigned pfx_len);
_nib_offl_entry_t *_nib_offl_alloc(const ipv6_addr_t *next_hop, unsigned iface,
const ipv6_addr_t *pfx, unsigned pfx_len);
/**
* @brief Clears out a NIB entry (off-link version)
*
* @param[in,out] dst An entry.
*/
void _nib_dst_clear(_nib_offl_entry_t *dst);
void _nib_offl_clear(_nib_offl_entry_t *dst);
/**
* @brief Iterates over off-link entries
@ -444,16 +447,15 @@ void _nib_dst_clear(_nib_offl_entry_t *dst);
*
* @return entry after @p last.
*/
_nib_offl_entry_t *_nib_dst_iter(const _nib_offl_entry_t *last);
_nib_offl_entry_t *_nib_offl_iter(const _nib_offl_entry_t *last);
/**
* @brief Helper function for view-level add-functions below
*
* @pre `(next_hop != NULL)`
* @pre `(pfx != NULL) && (pfx != "::") && (pfx_len != 0) && (pfx_len <= 128)`
*
* @param[in] next_hop Next hop to the destination. May not be NULL.
* *May also be a global address!*
* @param[in] next_hop An IPv6 address to next hop. May be NULL (for prefix
* list). *May also be a global address!*
* @param[in] iface The interface to the destination.
* @param[in] pfx The IPv6 prefix or address of the destination.
* May not be NULL or unspecified address. Use
@ -461,19 +463,19 @@ _nib_offl_entry_t *_nib_dst_iter(const _nib_offl_entry_t *last);
* @param[in] pfx_len The length in bits of @p pfx in bits.
* @param[in] mode [NIB-mode](_nib_onl_entry_t::mode).
*
* @return A new or existing off-link entry with _nib_dr_entry_t::pfx set to
* @return A new or existing off-link entry with _nib_offl_entry_t::pfx set to
* @p pfx.
* @return NULL, if no space is left.
*/
static inline _nib_offl_entry_t *_nib_dst_add(const ipv6_addr_t *next_hop,
unsigned iface,
const ipv6_addr_t *pfx,
unsigned pfx_len, uint8_t mode)
static inline _nib_offl_entry_t *_nib_offl_add(const ipv6_addr_t *next_hop,
unsigned iface,
const ipv6_addr_t *pfx,
unsigned pfx_len, uint8_t mode)
{
_nib_offl_entry_t *nib_offl = _nib_dst_alloc(next_hop, iface, pfx, pfx_len);
_nib_offl_entry_t *nib_offl = _nib_offl_alloc(next_hop, iface, pfx, pfx_len);
if (nib_offl != NULL) {
nib_offl->next_hop->mode |= (mode);
nib_offl->mode |= mode;
}
return nib_offl;
}
@ -483,17 +485,13 @@ static inline _nib_offl_entry_t *_nib_dst_add(const ipv6_addr_t *next_hop,
*
* @param[in,out] nib_offl An entry.
*/
static inline void _nib_dst_remove(_nib_offl_entry_t *nib_offl, uint8_t mode)
static inline void _nib_offl_remove(_nib_offl_entry_t *nib_offl, uint8_t mode)
{
_nib_onl_entry_t *node = nib_offl->next_hop;
if (node != NULL) {
node->mode &= ~mode;
}
_nib_dst_clear(nib_offl);
nib_offl->mode &= ~mode;
_nib_offl_clear(nib_offl);
}
#if defined(GNRC_IPV6_NIB_CONF_DC) || DOXYGEN
#if GNRC_IPV6_NIB_CONF_DC || DOXYGEN
/**
* @brief Creates or gets an existing destination cache entry by its addresses
*
@ -506,16 +504,16 @@ static inline void _nib_dst_remove(_nib_offl_entry_t *nib_offl, uint8_t mode)
*
* @note Only available if @ref GNRC_IPV6_NIB_CONF_DC.
*
* @return A new or existing destination cache entry with
* _nib_onl_entry_t::ipv6 of _nib_dr_entry_t::next_hop set to
* @p next_hop.
* @return A new or existing off-link entry with _nib_offl_entry_t::pfx set to
* @p pfx.
* @return NULL, if no space is left.
*/
static inline _nib_offl_entry_t *_nib_dc_add(const ipv6_addr_t *next_hop,
unsigned iface,
const ipv6_addr_t *dst)
{
return _nib_dst_add(next_hop, iface, dst, IPV6_ADDR_BIT_LEN, _DC);
assert((next_hop != NULL) && (dst != NULL));
return _nib_offl_add(next_hop, iface, dst, IPV6_ADDR_BIT_LEN, _DC);
}
/**
@ -529,7 +527,7 @@ static inline _nib_offl_entry_t *_nib_dc_add(const ipv6_addr_t *next_hop,
*/
static inline void _nib_dc_remove(_nib_offl_entry_t *nib_offl)
{
_nib_dst_remove(nib_offl, _DC);
_nib_offl_remove(nib_offl, _DC);
}
#endif /* GNRC_IPV6_NIB_CONF_DC */
@ -545,7 +543,7 @@ static inline void _nib_dc_remove(_nib_offl_entry_t *nib_offl)
* @ref _nib_drl_add() for default route destinations.
* @param[in] pfx_len The length in bits of @p pfx in bits.
*
* @return A new or existing prefix list entry with _nib_dr_entry_t::pfx set to
* @return A new or existing off-link entry with _nib_offl_entry_t::pfx set to
* @p pfx.
* @return NULL, if no space is left.
*/
@ -553,7 +551,7 @@ static inline _nib_offl_entry_t *_nib_pl_add(unsigned iface,
const ipv6_addr_t *pfx,
unsigned pfx_len)
{
return _nib_dst_add(NULL, iface, pfx, pfx_len, _PL);
return _nib_offl_add(NULL, iface, pfx, pfx_len, _PL);
}
/**
@ -565,10 +563,10 @@ static inline _nib_offl_entry_t *_nib_pl_add(unsigned iface,
*/
static inline void _nib_pl_remove(_nib_offl_entry_t *nib_offl)
{
_nib_dst_remove(nib_offl, _PL);
_nib_offl_remove(nib_offl, _PL);
}
#if defined(GNRC_IPV6_NIB_CONF_ROUTER) || DOXYGEN
#if GNRC_IPV6_NIB_CONF_ROUTER || DOXYGEN
/**
* @brief Creates or gets an existing forwarding table entry by its prefix
*
@ -585,8 +583,8 @@ static inline void _nib_pl_remove(_nib_offl_entry_t *nib_offl)
*
* @note Only available if @ref GNRC_IPV6_NIB_CONF_ROUTER.
*
* @return A new or existing forwarding table entry with _nib_dr_entry_t::pfx
* set to @p pfx.
* @return A new or existing off-link entry with _nib_offl_entry_t::pfx set to
* @p pfx.
* @return NULL, if no space is left.
*/
static inline _nib_offl_entry_t *_nib_ft_add(const ipv6_addr_t *next_hop,
@ -594,7 +592,7 @@ static inline _nib_offl_entry_t *_nib_ft_add(const ipv6_addr_t *next_hop,
const ipv6_addr_t *pfx,
unsigned pfx_len)
{
return _nib_dst_add(next_hop, iface, pfx, pfx_len, _FT);
return _nib_offl_add(next_hop, iface, pfx, pfx_len, _FT);
}
/**
@ -608,7 +606,7 @@ static inline _nib_offl_entry_t *_nib_ft_add(const ipv6_addr_t *next_hop,
*/
static inline void _nib_ft_remove(_nib_offl_entry_t *nib_offl)
{
_nib_dst_remove(nib_offl, _FT);
_nib_offl_remove(nib_offl, _FT);
}
#endif /* GNRC_IPV6_NIB_CONF_ROUTER */