2016-06-01 20:25:22 +08:00
|
|
|
/** @file
|
|
|
|
* @brief IPv6 neighbor management.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-06-01 20:25:22 +08:00
|
|
|
*/
|
|
|
|
#ifndef __NET_NBR_H
|
|
|
|
#define __NET_NBR_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-06-01 20:25:22 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <net/net_if.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define NET_NBR_LLADDR_UNKNOWN 0xff
|
|
|
|
|
|
|
|
/* The neighbors are tracked by link layer address. This is not part
|
|
|
|
* of struct net_nbr because this data can be shared between different
|
|
|
|
* neighboring tables.
|
|
|
|
*/
|
|
|
|
struct net_nbr_lladdr {
|
|
|
|
/** Link layer address */
|
|
|
|
struct net_linkaddr_storage lladdr;
|
|
|
|
|
|
|
|
/** Reference count. */
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t ref;
|
2016-06-01 20:25:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NET_NBR_LLADDR_INIT(_name, _count) \
|
2016-11-12 02:14:20 +08:00
|
|
|
struct net_nbr_lladdr _name[_count] = { }
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/* Alignment needed for various parts of the neighbor definition */
|
|
|
|
#define __net_nbr_align __aligned(sizeof(int))
|
|
|
|
|
|
|
|
/* The neighbor node generic data. There can be sub-system specific
|
|
|
|
* data at the end of the node.
|
|
|
|
*/
|
|
|
|
struct net_nbr {
|
|
|
|
/** Reference count. */
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t ref;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/** Link to ll address. This is the index into lladdr array.
|
|
|
|
* The value NET_NBR_LLADDR_UNKNOWN tells that this neighbor
|
|
|
|
* does not yet have lladdr linked to it.
|
|
|
|
*/
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t idx;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/** Amount of data that this neighbor buffer can store. */
|
2017-04-21 22:27:50 +08:00
|
|
|
const u16_t size;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
2016-09-20 20:30:22 +08:00
|
|
|
/** Extra data size associated with this neighbor */
|
2017-04-21 22:27:50 +08:00
|
|
|
const u16_t extra_data_size;
|
2016-09-20 20:30:22 +08:00
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
/** Interface this neighbor is found */
|
|
|
|
struct net_if *iface;
|
|
|
|
|
|
|
|
/** Pointer to the start of data in the neighbor table. */
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t *data;
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/** Function to be called when the neighbor is removed. */
|
|
|
|
void (*const remove)(struct net_nbr *nbr);
|
|
|
|
|
|
|
|
/** Start of the data storage. Not to be accessed directly
|
|
|
|
* (the data pointer should be used instead).
|
|
|
|
*/
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t __nbr[0] __net_nbr_align;
|
2016-06-01 20:25:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is an array of struct net_nbr + some additional data */
|
2016-09-20 20:30:22 +08:00
|
|
|
#define NET_NBR_POOL_INIT(_name, _count, _size, _remove, _extra_size) \
|
2016-06-01 20:25:22 +08:00
|
|
|
struct { \
|
|
|
|
struct net_nbr nbr; \
|
2017-04-21 22:27:50 +08:00
|
|
|
u8_t data[ROUND_UP(_size, 4)] __net_nbr_align; \
|
|
|
|
u8_t extra[ROUND_UP(_extra_size, 4)] __net_nbr_align;\
|
2016-06-01 20:25:22 +08:00
|
|
|
} _name[_count] = { \
|
|
|
|
[0 ... (_count - 1)] = { .nbr = { \
|
|
|
|
.idx = NET_NBR_LLADDR_UNKNOWN, \
|
|
|
|
.remove = _remove, \
|
2016-09-20 20:30:22 +08:00
|
|
|
.size = ROUND_UP(_size, 4), \
|
|
|
|
.extra_data_size = ROUND_UP(_extra_size, 4) } },\
|
2016-06-01 20:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct net_nbr_table {
|
|
|
|
/** Link to a neighbor pool */
|
|
|
|
struct net_nbr *nbr;
|
|
|
|
|
|
|
|
/** Function to be called when the table is cleared. */
|
|
|
|
void (*const clear)(struct net_nbr_table *table);
|
2017-08-10 21:51:20 +08:00
|
|
|
|
|
|
|
/** Max number of neighbors in the pool */
|
|
|
|
const u16_t nbr_count;
|
2016-06-01 20:25:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NET_NBR_LOCAL static
|
|
|
|
#define NET_NBR_GLOBAL
|
|
|
|
|
|
|
|
/* Type of the table can be NET_NBR_LOCAL or NET_NBR_GLOBAL
|
|
|
|
*/
|
|
|
|
#define NET_NBR_TABLE_INIT(_type, _name, _pool, _clear) \
|
|
|
|
_type struct net_nbr_table_##_name { \
|
|
|
|
struct net_nbr_table table; \
|
2016-09-21 20:43:37 +08:00
|
|
|
} net_##_name __used = { \
|
2016-06-01 20:25:22 +08:00
|
|
|
.table = { \
|
|
|
|
.clear = _clear, \
|
|
|
|
.nbr = (struct net_nbr *)_pool, \
|
2017-08-10 21:51:20 +08:00
|
|
|
.nbr_count = ARRAY_SIZE(_pool), \
|
2016-06-01 20:25:22 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2016-09-20 20:30:22 +08:00
|
|
|
/**
|
|
|
|
* @brief Get a pointer to the extra data of a neighbor entry.
|
|
|
|
*
|
|
|
|
* @param nbr A valid pointer to neighbor
|
|
|
|
*
|
|
|
|
* @return Pointer to the extra data of the nbr.
|
|
|
|
*/
|
|
|
|
static inline void *net_nbr_extra_data(struct net_nbr *nbr)
|
|
|
|
{
|
|
|
|
return (void *)ROUND_UP((nbr->__nbr + nbr->size), sizeof(int));
|
|
|
|
}
|
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
/**
|
|
|
|
* @brief Decrement the reference count. If count goes to 0, the neighbor
|
|
|
|
* is released and returned to free list.
|
|
|
|
* @param nbr Pointer to neighbor
|
|
|
|
*/
|
2016-09-20 18:14:00 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_IPV6_NBR_CACHE)
|
|
|
|
void net_nbr_unref_debug(struct net_nbr *nbr, const char *caller, int line);
|
|
|
|
#define net_nbr_unref(nbr) net_nbr_unref_debug(nbr, __func__, __LINE__)
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
void net_nbr_unref(struct net_nbr *nbr);
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Increment the reference count.
|
|
|
|
* @param nbr Pointer to neighbor
|
|
|
|
* @return Pointer to neighbor
|
|
|
|
*/
|
2016-09-20 18:14:00 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_IPV6_NBR_CACHE)
|
|
|
|
struct net_nbr *net_nbr_ref_debug(struct net_nbr *nbr, const char *caller,
|
|
|
|
int line);
|
|
|
|
#define net_nbr_ref(nbr) net_nbr_ref_debug(nbr, __func__, __LINE__)
|
|
|
|
#else
|
2016-06-01 20:25:22 +08:00
|
|
|
struct net_nbr *net_nbr_ref(struct net_nbr *nbr);
|
2016-09-20 18:14:00 +08:00
|
|
|
#endif
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a free neighbor from specific table.
|
|
|
|
* @param table Neighbor table
|
2017-04-20 02:05:01 +08:00
|
|
|
* @return Pointer to neighbor, NULL if no free neighbors
|
2016-06-01 20:25:22 +08:00
|
|
|
*/
|
|
|
|
struct net_nbr *net_nbr_get(struct net_nbr_table *table);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Find a neighbor from specific table.
|
|
|
|
* @param table Neighbor table
|
|
|
|
* @param iface Network interface to use
|
2016-09-16 16:17:24 +08:00
|
|
|
* @param lladdr Neighbor link layer address
|
2016-06-01 20:25:22 +08:00
|
|
|
* @return Pointer to neighbor, NULL if not found
|
|
|
|
*/
|
|
|
|
struct net_nbr *net_nbr_lookup(struct net_nbr_table *table,
|
|
|
|
struct net_if *iface,
|
|
|
|
struct net_linkaddr *lladdr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Link a neighbor to specific link layer address.
|
|
|
|
* @param table Neighbor table
|
|
|
|
* @param iface Network interface to use
|
2016-09-16 16:17:24 +08:00
|
|
|
* @param lladdr Neighbor link layer address
|
2016-06-01 20:25:22 +08:00
|
|
|
* @return 0 if ok, <0 if linking failed
|
|
|
|
*/
|
|
|
|
int net_nbr_link(struct net_nbr *nbr, struct net_if *iface,
|
|
|
|
struct net_linkaddr *lladdr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unlink a neighbor from specific link layer address.
|
|
|
|
* @param table Neighbor table
|
2016-09-16 16:17:24 +08:00
|
|
|
* @param lladdr Neighbor link layer address
|
2016-06-01 20:25:22 +08:00
|
|
|
* @return 0 if ok, <0 if unlinking failed
|
|
|
|
*/
|
|
|
|
int net_nbr_unlink(struct net_nbr *nbr, struct net_linkaddr *lladdr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return link address for a specific lladdr table index
|
|
|
|
* @param idx Link layer address index in ll table.
|
|
|
|
* @return Pointer to link layer address storage, NULL if not found
|
|
|
|
*/
|
2017-04-21 22:27:50 +08:00
|
|
|
struct net_linkaddr_storage *net_nbr_get_lladdr(u8_t idx);
|
2016-06-01 20:25:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Clear table from all neighbors. After this the linking between
|
|
|
|
* lladdr and neighbor is removed.
|
|
|
|
* @param table Neighbor table
|
|
|
|
*/
|
|
|
|
void net_nbr_clear_table(struct net_nbr_table *table);
|
|
|
|
|
2016-08-24 17:58:29 +08:00
|
|
|
#if defined(CONFIG_NET_DEBUG_IPV6_NBR_CACHE)
|
2016-07-06 17:36:53 +08:00
|
|
|
/**
|
|
|
|
* @brief Debug helper to print out the neighbor information.
|
|
|
|
* @param table Neighbor table
|
|
|
|
*/
|
|
|
|
void net_nbr_print(struct net_nbr_table *table);
|
2016-08-24 17:58:29 +08:00
|
|
|
#else
|
2016-07-06 17:36:53 +08:00
|
|
|
#define net_nbr_print(...)
|
2016-08-24 17:58:29 +08:00
|
|
|
#endif /* CONFIG_NET_DEBUG_IPV6_NBR_CACHE */
|
2016-07-06 17:36:53 +08:00
|
|
|
|
2016-06-01 20:25:22 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __NET_NBR_H */
|