refactored the rpl-nbr-policy to be called from nbr-table module

This commit is contained in:
Joakim Eriksson 2015-09-20 21:02:30 +02:00
parent be81d1d2c6
commit 444015df67
13 changed files with 198 additions and 161 deletions

View file

@ -49,7 +49,7 @@
#include "net/ipv6/uip-ds6-nbr.h"
#include "net/ipv6/uip-ds6-route.h"
#define DEBUG DEBUG_NONE
#define DEBUG DEBUG_FULL
#include "net/ip/uip-debug.h"
/*
@ -58,14 +58,18 @@
* - max X children (nexthops)
* - max Y "best parents"
* => at least MAX_NBRS - (Y + X + 1) free slots for other.
*
* NOTE: this policy assumes that all neighbors end up being IPv6
* neighbors and are not only MAC neighbors.
*/
#define MAX_CHILDREN (NBR_TABLE_MAX_NEIGHBORS - 3)
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
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;
static uip_ds6_nbr_t *worst_rank_nbr; /* the parent that has the worst rank */
static linkaddr_t *worst_rank_nbr; /* the parent that has the worst rank */
static rpl_rank_t worst_rank;
/*---------------------------------------------------------------------------*/
#if DEBUG == DEBUG_FULL
@ -124,8 +128,12 @@ update_nbr(void)
parent->dag->instance != NULL &&
(rank = parent->dag->instance->of->calculate_rank(parent, 0)) > worst_rank) {
/* This is the worst-rank neighbor - this is a good candidate for removal */
worst_rank = rank;
worst_rank_nbr = nbr;
if(uip_ds6_route_is_nexthop((uip_lladdr_t *)lladdr) == 0) {
worst_rank = rank;
worst_rank_nbr = lladdr;
} else {
printf("*** Can not use this as worst rank as it is a next hop\n");
}
}
}
@ -138,10 +146,9 @@ update_nbr(void)
if(is_used == 0) {
/* This neighbor is neither parent or child and can be safely removed */
worst_rank_nbr = nbr;
worst_rank_nbr = lladdr;
worst_rank = INFINITE_RANK;
} else if(is_used > 1) {
/* Both parent and child - this should never happen! */
PRINTF("NBR-POLICY: *** Neighbor is both child and candidate parent: ");
PRINTLLADDR((uip_lladdr_t *)lladdr);
PRINTF("\n");
@ -157,67 +164,36 @@ update_nbr(void)
num_free, num_children, num_parents, uip_ds6_route_num_routes());
}
/*---------------------------------------------------------------------------*/
static int
remove_worst_nbr(void)
{
/* we assume that it is possible to remove the worst parent at the moment */
if(worst_rank_nbr != NULL) {
PRINTF("Removing worst ranked nbr ");
PRINTLLADDR((uip_lladdr_t*)nbr_table_get_lladdr(ds6_neighbors, worst_rank_nbr));
PRINTF(" with rank %d\n", worst_rank);
if(uip_ds6_nbr_rm(worst_rank_nbr)) {
worst_rank_nbr = NULL;
return 1;
}
PRINTF("FAILED to remove worst ranked nbr!\n");
return 0;
}
PRINTF("FAILED to remove worst rank nbr - no found\n");
return 0;
}
/*---------------------------------------------------------------------------*/
/* Called whenever we get a unicast DIS - e.g. someone that already
have this node in its table - since it is a unicast */
static int
check_add_from_dis(uip_ipaddr_t *from)
const linkaddr_t *
find_removable_dis(uip_ipaddr_t *from)
{
/* do a lookup to see if it is alread there - then allow add/update */
if(uip_ds6_nbr_lookup(from)) {
return 1;
}
update_nbr();
if(num_free > 0) {
return 1;
printf("num-free > 0 = %d", num_free);
printf("**** Should remove unused elements but can not... \n");
/* return 1; */
}
if(num_children < MAX_CHILDREN) {
return remove_worst_nbr();
return worst_rank_nbr;
}
return 0;
return NULL;
}
/*---------------------------------------------------------------------------*/
static int
check_add_from_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
const linkaddr_t *
find_removable_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
{
rpl_instance_t *instance;
rpl_rank_t rank;
/* Do a lookup to see if it is already there - then allow add/update. */
if(uip_ds6_nbr_lookup(from)) {
return 1;
}
update_nbr();
/* If there is room for this neighbor just add it. */
if(num_free > 0) {
return 1;
}
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);
return 0;
return NULL;
}
/* Add the new neighbor only if it is better than the preferred parent. */
@ -227,38 +203,43 @@ check_add_from_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
PRINTF("Found better neighbor %d < %d - add to cache...\n",
rank, worst_rank);
return remove_worst_nbr();
return worst_rank_nbr;
}
PRINTF("Found worse neighbor with new %d and old %d - NOT add to cache.\n",
rank, worst_rank);
return 0;
return NULL;
}
/*---------------------------------------------------------------------------*/
static int
check_add_from_dao(uip_ipaddr_t *from)
const linkaddr_t *
find_removable_dao(uip_ipaddr_t *from)
{
/* Do a lookup to see if it is alread there - then allow add/update. */
if(uip_ds6_nbr_lookup(from)) {
return 1;
}
update_nbr();
/* Check if this DAO sender is not yet neighbor and there is already too
many children. */
if(num_children >= MAX_CHILDREN) {
PRINTF("Can not add another child - already at max.\n");
return 0;
return NULL;
}
return 1;
/* remove the worst ranked nbr */
return worst_rank_nbr;
}
/*---------------------------------------------------------------------------*/
const struct nbr_policy rpl_nbr_policy = {
check_add_from_dis,
check_add_from_dio,
check_add_from_dao
};
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_DAO:
return find_removable_dao(&UIP_IP_BUF->srcipaddr);
case NBR_TABLE_REASON_RPL_DIS:
return find_removable_dis(&UIP_IP_BUF->srcipaddr);
default:
return NULL;
}
}
/*---------------------------------------------------------------------------*/
/** @}*/