2017-06-10 12:35:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014-2015, Yanzi Networks AB.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name of the copyright holders nor the
|
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
|
|
|
* COPYRIGHT HOLDERS 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-09-29 20:18:48 +00:00
|
|
|
* \addtogroup rpl-lite
|
|
|
|
* @{
|
|
|
|
*
|
2017-06-10 12:35:39 +00:00
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* Default RPL NBR policy
|
|
|
|
* decides when to add a new discovered node to the nbr table from RPL.
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Contributors: Niclas Finne <nfi@sics.se>, Oriol Piñol <oriol@yanzi.se>,
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-06-22 07:49:01 +00:00
|
|
|
#include "net/rpl-lite/rpl.h"
|
2017-06-10 12:35:39 +00:00
|
|
|
#include "net/nbr-table.h"
|
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "RPL"
|
2017-07-15 13:19:09 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_RPL
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
/*
|
2017-06-29 14:44:56 +00:00
|
|
|
* Policy for neighbor addition
|
2017-06-10 12:35:39 +00:00
|
|
|
* - one node is locked (default route)
|
2017-06-29 14:44:56 +00:00
|
|
|
* - max X "best parents"
|
|
|
|
* => at least MAX_NBRS - (X + 1) free slots for other.
|
2017-06-10 12:35:39 +00:00
|
|
|
*
|
|
|
|
* NOTE: this policy assumes that all neighbors end up being IPv6
|
|
|
|
* neighbors and are not only MAC neighbors.
|
|
|
|
*/
|
|
|
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
|
|
|
|
|
|
|
static int num_parents; /* all nodes that are possible parents */
|
|
|
|
static int num_free;
|
2017-06-29 14:44:56 +00:00
|
|
|
static linkaddr_t *worst_rank_nbr_lladdr; /* lladdr of the the neighbor with the worst rank */
|
2017-06-10 12:35:39 +00:00
|
|
|
static rpl_rank_t worst_rank;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-06-29 14:44:56 +00:00
|
|
|
#if LOG_DBG_ENABLED
|
|
|
|
/* Print out state periodically */
|
|
|
|
static void update_state(void);
|
2017-06-10 12:35:39 +00:00
|
|
|
static struct ctimer periodic_timer;
|
|
|
|
static int timer_init = 0;
|
|
|
|
static void
|
|
|
|
handle_periodic_timer(void *ptr)
|
|
|
|
{
|
2017-06-29 14:44:56 +00:00
|
|
|
update_state();
|
2017-06-10 12:35:39 +00:00
|
|
|
ctimer_restart(&periodic_timer);
|
|
|
|
}
|
2017-06-29 14:44:56 +00:00
|
|
|
#endif /* LOG_DBG_ENABLED */
|
2017-06-10 12:35:39 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2017-06-29 14:44:56 +00:00
|
|
|
update_state(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-19 09:35:02 +00:00
|
|
|
uip_ds6_nbr_t *ds6_nbr;
|
|
|
|
rpl_nbr_t *rpl_nbr;
|
2017-06-29 14:44:56 +00:00
|
|
|
rpl_rank_t nbr_rank;
|
|
|
|
int num_used = 0;
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-29 14:44:56 +00:00
|
|
|
#if LOG_DBG_ENABLED
|
2017-06-10 12:35:39 +00:00
|
|
|
if(!timer_init) {
|
|
|
|
timer_init = 1;
|
|
|
|
ctimer_set(&periodic_timer, 60 * CLOCK_SECOND,
|
|
|
|
&handle_periodic_timer, NULL);
|
|
|
|
}
|
2017-06-29 14:44:56 +00:00
|
|
|
#endif /* LOG_DBG_ENABLED */
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
worst_rank = 0;
|
2017-06-29 14:44:56 +00:00
|
|
|
worst_rank_nbr_lladdr = NULL;
|
2017-06-10 12:35:39 +00:00
|
|
|
num_parents = 0;
|
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
ds6_nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
while(ds6_nbr != NULL) {
|
2017-06-10 12:35:39 +00:00
|
|
|
|
2017-06-29 14:44:56 +00:00
|
|
|
linkaddr_t *nbr_lladdr = nbr_table_get_lladdr(ds6_neighbors, ds6_nbr);
|
|
|
|
rpl_nbr = rpl_neighbor_get_from_lladdr((uip_lladdr_t *)nbr_lladdr);
|
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
if(rpl_nbr != NULL && rpl_neighbor_is_parent(rpl_nbr)) {
|
2017-06-10 12:35:39 +00:00
|
|
|
num_parents++;
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:44:56 +00:00
|
|
|
nbr_rank = rpl_neighbor_rank_via_nbr(rpl_nbr);
|
|
|
|
/* Select worst-rank neighbor */
|
|
|
|
if(rpl_nbr != curr_instance.dag.preferred_parent
|
|
|
|
&& nbr_rank > worst_rank) {
|
|
|
|
/* This is the worst-rank neighbor - this is a good candidate for removal */
|
|
|
|
worst_rank = nbr_rank;
|
|
|
|
worst_rank_nbr_lladdr = nbr_lladdr;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
ds6_nbr = nbr_table_next(ds6_neighbors, ds6_nbr);
|
2017-06-10 12:35:39 +00:00
|
|
|
num_used++;
|
|
|
|
}
|
|
|
|
/* how many more IP neighbors can be have? */
|
|
|
|
num_free = NBR_TABLE_MAX_NEIGHBORS - num_used;
|
|
|
|
|
2017-06-29 14:44:56 +00:00
|
|
|
LOG_INFO("nbr-policy: free: %d, parents: %d\n", num_free, num_parents);
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static const linkaddr_t *
|
2017-06-29 14:44:56 +00:00
|
|
|
find_worst_rank_nbr_lladdr(void)
|
2017-06-10 12:35:39 +00:00
|
|
|
{
|
2017-06-29 14:44:56 +00:00
|
|
|
update_state();
|
|
|
|
return worst_rank_nbr_lladdr;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static const linkaddr_t *
|
|
|
|
find_removable_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
|
|
|
|
{
|
2017-06-29 14:44:56 +00:00
|
|
|
update_state();
|
2017-06-10 12:35:39 +00:00
|
|
|
|
|
|
|
if(!curr_instance.used || curr_instance.instance_id != dio->instance_id) {
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_WARN("nbr-policy: did not find instance id: %d\n", dio->instance_id);
|
2017-06-10 12:35:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:35:02 +00:00
|
|
|
/* Add the new neighbor only if it is better than the current worst. */
|
2017-06-10 12:35:39 +00:00
|
|
|
if(dio->rank + curr_instance.min_hoprankinc < worst_rank - curr_instance.min_hoprankinc / 2) {
|
|
|
|
/* Found *great* neighbor - add! */
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("nbr-policy: DIO rank %u, worse_rank %u -- add to cache\n",
|
2017-06-10 12:35:39 +00:00
|
|
|
dio->rank, worst_rank);
|
2017-06-29 14:44:56 +00:00
|
|
|
return worst_rank_nbr_lladdr;
|
2017-06-10 12:35:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:19:36 +00:00
|
|
|
LOG_INFO("nbr-policy: DIO rank %u, worse_rank %u -- do not add to cache\n",
|
2017-06-10 12:35:39 +00:00
|
|
|
dio->rank, worst_rank);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const linkaddr_t *
|
|
|
|
rpl_nbr_policy_find_removable(nbr_table_reason_t reason, void *data)
|
|
|
|
{
|
|
|
|
/* When we get the DIO/DAO/DIS we know that UIP contains the
|
|
|
|
incoming packet */
|
|
|
|
switch(reason) {
|
|
|
|
case NBR_TABLE_REASON_RPL_DIO:
|
|
|
|
return find_removable_dio(&UIP_IP_BUF->srcipaddr, data);
|
|
|
|
case NBR_TABLE_REASON_RPL_DIS:
|
2017-06-29 14:44:56 +00:00
|
|
|
return find_worst_rank_nbr_lladdr();
|
|
|
|
case NBR_TABLE_REASON_IPV6_ND_AUTOFILL:
|
|
|
|
return find_worst_rank_nbr_lladdr();
|
2017-06-10 12:35:39 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @}*/
|