2015-08-20 15:09:32 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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>,
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net/rpl/rpl-private.h"
|
|
|
|
#include "net/nbr-table.h"
|
|
|
|
#include "net/ipv6/uip-ds6-nbr.h"
|
|
|
|
#include "net/ipv6/uip-ds6-route.h"
|
|
|
|
|
2015-09-22 19:01:55 +00:00
|
|
|
#define DEBUG DEBUG_NONE
|
2015-08-20 15:09:32 +00:00
|
|
|
#include "net/ip/uip-debug.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Policy for neighbor adds
|
|
|
|
* - one node is locked (default route)
|
|
|
|
* - max X children (nexthops)
|
|
|
|
* - max Y "best parents"
|
|
|
|
* => at least MAX_NBRS - (Y + X + 1) free slots for other.
|
2015-09-20 19:02:30 +00:00
|
|
|
*
|
|
|
|
* NOTE: this policy assumes that all neighbors end up being IPv6
|
|
|
|
* neighbors and are not only MAC neighbors.
|
2015-08-20 15:09:32 +00:00
|
|
|
*/
|
2015-09-22 19:01:55 +00:00
|
|
|
#define MAX_CHILDREN (NBR_TABLE_MAX_NEIGHBORS - 2)
|
2015-09-20 19:02:30 +00:00
|
|
|
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
2015-08-20 15:09:32 +00:00
|
|
|
|
|
|
|
static int num_parents; /* any node that are possible parents */
|
|
|
|
static int num_children; /* all children that we have as nexthop */
|
|
|
|
static int num_free;
|
2015-09-20 19:02:30 +00:00
|
|
|
static linkaddr_t *worst_rank_nbr; /* the parent that has the worst rank */
|
2015-08-20 15:09:32 +00:00
|
|
|
static rpl_rank_t worst_rank;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-08-24 09:42:50 +00:00
|
|
|
#if DEBUG == DEBUG_FULL
|
2016-03-26 19:12:52 +00:00
|
|
|
/*
|
|
|
|
* This create a periodic call of the update_nbr function that will print
|
|
|
|
* useful debugging information when in DEBUG_FULL mode
|
|
|
|
*/
|
2015-08-24 09:42:50 +00:00
|
|
|
static void update_nbr(void);
|
|
|
|
static struct ctimer periodic_timer;
|
|
|
|
static int timer_init = 0;
|
|
|
|
static void
|
|
|
|
handle_periodic_timer(void *ptr)
|
|
|
|
{
|
|
|
|
update_nbr();
|
|
|
|
ctimer_restart(&periodic_timer);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG == DEBUG_FULL */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-08-20 15:09:32 +00:00
|
|
|
static void
|
|
|
|
update_nbr(void)
|
|
|
|
{
|
|
|
|
uip_ds6_nbr_t *nbr;
|
|
|
|
rpl_parent_t *parent;
|
|
|
|
int num_used;
|
|
|
|
int is_used;
|
|
|
|
rpl_rank_t rank;
|
|
|
|
|
2015-08-24 09:42:50 +00:00
|
|
|
#if DEBUG == DEBUG_FULL
|
|
|
|
if(!timer_init) {
|
|
|
|
timer_init = 1;
|
|
|
|
ctimer_set(&periodic_timer, 60 * CLOCK_SECOND,
|
|
|
|
&handle_periodic_timer, NULL);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG == DEBUG_FULL */
|
|
|
|
|
2015-08-20 15:09:32 +00:00
|
|
|
worst_rank = 0;
|
|
|
|
worst_rank_nbr = NULL;
|
|
|
|
num_used = 0;
|
|
|
|
num_parents = 0;
|
|
|
|
num_children = 0;
|
|
|
|
|
|
|
|
nbr = nbr_table_head(ds6_neighbors);
|
|
|
|
while(nbr != NULL) {
|
|
|
|
linkaddr_t *lladdr = nbr_table_get_lladdr(ds6_neighbors, nbr);
|
|
|
|
is_used = 0;
|
|
|
|
|
2016-03-26 19:12:52 +00:00
|
|
|
/*
|
|
|
|
* Check if this neighbor is used as nexthop and therefor being a
|
|
|
|
* RPL child.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(uip_ds6_route_is_nexthop(&nbr->ipaddr) != 0) {
|
2016-02-18 21:40:30 +00:00
|
|
|
is_used++;
|
|
|
|
num_children++;
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:09:32 +00:00
|
|
|
parent = rpl_get_parent((uip_lladdr_t *)lladdr);
|
|
|
|
if(parent != NULL) {
|
|
|
|
num_parents++;
|
|
|
|
|
|
|
|
if(parent->dag != NULL && parent->dag->preferred_parent == parent) {
|
2016-03-26 19:12:52 +00:00
|
|
|
/*
|
|
|
|
* This is the preferred parent for the DAG and must not be removed
|
|
|
|
* Note: this assumes that only RPL adds default routes.
|
|
|
|
*/
|
2016-02-18 21:40:30 +00:00
|
|
|
} else if(is_used == 0 && worst_rank < INFINITE_RANK &&
|
2015-08-20 15:09:32 +00:00
|
|
|
parent->rank > 0 &&
|
|
|
|
parent->dag != NULL &&
|
|
|
|
parent->dag->instance != NULL &&
|
2016-04-22 21:46:33 +00:00
|
|
|
(rank = parent->dag->instance->of->rank_via_parent(parent)) > worst_rank) {
|
2015-08-20 15:09:32 +00:00
|
|
|
/* This is the worst-rank neighbor - this is a good candidate for removal */
|
2016-02-18 21:40:30 +00:00
|
|
|
worst_rank = rank;
|
|
|
|
worst_rank_nbr = lladdr;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
2016-02-18 21:40:30 +00:00
|
|
|
/* add to is_used after evaluation of is_used above */
|
2015-08-20 15:09:32 +00:00
|
|
|
is_used++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_used == 0) {
|
|
|
|
/* This neighbor is neither parent or child and can be safely removed */
|
2015-09-20 19:02:30 +00:00
|
|
|
worst_rank_nbr = lladdr;
|
2015-08-20 15:09:32 +00:00
|
|
|
worst_rank = INFINITE_RANK;
|
|
|
|
} else if(is_used > 1) {
|
|
|
|
PRINTF("NBR-POLICY: *** Neighbor is both child and candidate parent: ");
|
|
|
|
PRINTLLADDR((uip_lladdr_t *)lladdr);
|
|
|
|
PRINTF("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
nbr = nbr_table_next(ds6_neighbors, nbr);
|
|
|
|
num_used++;
|
|
|
|
}
|
|
|
|
/* how many more IP neighbors can be have? */
|
|
|
|
num_free = NBR_TABLE_MAX_NEIGHBORS - num_used;
|
|
|
|
|
|
|
|
PRINTF("NBR-POLICY: Free: %d, Children: %d, Parents: %d Routes: %d\n",
|
|
|
|
num_free, num_children, num_parents, uip_ds6_route_num_routes());
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Called whenever we get a unicast DIS - e.g. someone that already
|
|
|
|
have this node in its table - since it is a unicast */
|
2015-09-20 19:02:30 +00:00
|
|
|
const linkaddr_t *
|
|
|
|
find_removable_dis(uip_ipaddr_t *from)
|
2015-08-20 15:09:32 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
update_nbr();
|
|
|
|
if(num_free > 0) {
|
2016-03-26 19:12:52 +00:00
|
|
|
/* there are free entries (e.g. unsused by RPL and ND6) but since it is
|
|
|
|
used by other modules we can not pick these entries for removal. */
|
|
|
|
PRINTF("Num-free > 0 = %d - Other for RPL/ND6 unused NBR entry exists .",
|
|
|
|
num_free);
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
if(num_children < MAX_CHILDREN) {
|
2015-09-20 19:02:30 +00:00
|
|
|
return worst_rank_nbr;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
2015-09-20 19:02:30 +00:00
|
|
|
return NULL;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-20 19:02:30 +00:00
|
|
|
const linkaddr_t *
|
|
|
|
find_removable_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
|
2015-08-20 15:09:32 +00:00
|
|
|
{
|
|
|
|
rpl_instance_t *instance;
|
|
|
|
|
|
|
|
update_nbr();
|
|
|
|
|
|
|
|
instance = rpl_get_instance(dio->instance_id);
|
|
|
|
if(instance == NULL || instance->current_dag == NULL) {
|
|
|
|
PRINTF("Did not find instance id: %d\n", dio->instance_id);
|
2015-09-20 19:02:30 +00:00
|
|
|
return NULL;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 19:12:52 +00:00
|
|
|
/* Add the new neighbor only if it is better than the worst parent. */
|
2016-04-22 21:46:33 +00:00
|
|
|
if(dio->rank + instance->min_hoprankinc < worst_rank - instance->min_hoprankinc / 2) {
|
2015-08-20 15:09:32 +00:00
|
|
|
/* Found *great* neighbor - add! */
|
|
|
|
PRINTF("Found better neighbor %d < %d - add to cache...\n",
|
|
|
|
rank, worst_rank);
|
|
|
|
|
2015-09-20 19:02:30 +00:00
|
|
|
return worst_rank_nbr;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF("Found worse neighbor with new %d and old %d - NOT add to cache.\n",
|
|
|
|
rank, worst_rank);
|
2015-09-20 19:02:30 +00:00
|
|
|
return NULL;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-20 19:02:30 +00:00
|
|
|
const linkaddr_t *
|
2015-09-22 20:12:06 +00:00
|
|
|
find_removable_dao(uip_ipaddr_t *from, rpl_instance_t *instance)
|
2015-08-20 15:09:32 +00:00
|
|
|
{
|
2015-09-22 20:12:06 +00:00
|
|
|
int max = MAX_CHILDREN;
|
2015-08-20 15:09:32 +00:00
|
|
|
update_nbr();
|
|
|
|
|
2015-09-22 20:12:06 +00:00
|
|
|
if(instance != NULL) {
|
|
|
|
/* No need to reserve space for parents for RPL ROOT */
|
|
|
|
if(instance->current_dag->rank == ROOT_RANK(instance)) {
|
|
|
|
max = NBR_TABLE_MAX_NEIGHBORS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:09:32 +00:00
|
|
|
/* Check if this DAO sender is not yet neighbor and there is already too
|
|
|
|
many children. */
|
2015-09-22 20:12:06 +00:00
|
|
|
if(num_children >= max) {
|
2015-08-20 15:09:32 +00:00
|
|
|
PRINTF("Can not add another child - already at max.\n");
|
2015-09-20 19:02:30 +00:00
|
|
|
return NULL;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
2015-09-20 19:02:30 +00:00
|
|
|
/* remove the worst ranked nbr */
|
|
|
|
return worst_rank_nbr;
|
2015-08-20 15:09:32 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-20 19:02:30 +00:00
|
|
|
const linkaddr_t *
|
2016-03-26 19:12:52 +00:00
|
|
|
rpl_nbr_policy_find_removable(nbr_table_reason_t reason,void * data)
|
|
|
|
{
|
2015-09-20 19:02:30 +00:00
|
|
|
/* 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_DAO:
|
2015-09-22 20:12:06 +00:00
|
|
|
return find_removable_dao(&UIP_IP_BUF->srcipaddr, data);
|
2015-09-20 19:02:30 +00:00
|
|
|
case NBR_TABLE_REASON_RPL_DIS:
|
|
|
|
return find_removable_dis(&UIP_IP_BUF->srcipaddr);
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 15:09:32 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @}*/
|