Added basic functionality for DAO ACKs and DTSN. Randomized DAO timer.

This commit is contained in:
nvt-se 2010-06-06 21:42:50 +00:00
parent f4508cc1e2
commit 3748f782c5
3 changed files with 97 additions and 31 deletions

View file

@ -32,7 +32,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rpl-dag.c,v 1.18 2010/06/03 18:37:47 joxe Exp $
* $Id: rpl-dag.c,v 1.19 2010/06/06 21:42:50 nvt-se Exp $
*/
/**
* \file
@ -110,8 +110,6 @@ remove_parents(rpl_dag_t *dag, rpl_parent_t *exception, int poison_routes)
for(p = list_head(dag->parents); p != NULL;) {
if(p != exception) {
ANNOTATE("#L %u 0\n", p->addr.u8[sizeof(p->addr) - 1]);
if(poison_routes == POISON_ROUTES) {
/* Send no-DAOs to old parents. */
dao_output(p, ZERO_LIFETIME);
@ -126,6 +124,13 @@ remove_parents(rpl_dag_t *dag, rpl_parent_t *exception, int poison_routes)
}
}
/************************************************************************/
static int
should_send_dao(rpl_dag_t *dag, rpl_dio_t *dio, rpl_parent_t *p)
{
return dio->dst_adv_supported && dio->dst_adv_trigger &&
dio->dtsn > p->dtsn && p == dag->best_parent;
}
/************************************************************************/
rpl_dag_t *
rpl_set_root(uip_ipaddr_t *dag_id)
{
@ -152,6 +157,7 @@ rpl_set_root(uip_ipaddr_t *dag_id)
dag->rank = ROOT_RANK;
dag->of = rpl_find_of(RPL_DEFAULT_OCP);
dag->best_parent = NULL;
dag->dtsn = 1; /* Trigger DAOs from the beginning. */
memcpy(&dag->dag_id, dag_id, sizeof(dag->dag_id));
@ -260,15 +266,15 @@ rpl_add_parent(rpl_dag_t *dag, rpl_dio_t *dio, uip_ipaddr_t *addr)
}
memcpy(&p->addr, addr, sizeof(p->addr));
p->local_confidence = 0;
p->dag = dag;
p->rank = dio->dag_rank;
p->local_confidence = 0;
p->dtsn = 0;
list_add(dag->parents, p);
/* Draw a line between the node and its parent in Cooja. */
ANNOTATE("#L %u 1\n",
addr->u8[sizeof(*addr) - 1]);
ANNOTATE("#L %u 1\n", addr->u8[sizeof(*addr) - 1]);
return p;
}
@ -422,6 +428,8 @@ join_dag(uip_ipaddr_t *from, rpl_dio_t *dio)
dag->rank = dag->of->increment_rank(dio->dag_rank, p);
dag->min_rank = dag->rank; /* So far this is the lowest rank we know */
dag->version = dio->version;
dag->best_parent = p;
dag->dio_intdoubl = dio->dag_intdoubl;
dag->dio_intmin = dio->dag_intmin;
dag->dio_redundancy = dio->dag_redund;
@ -442,7 +450,7 @@ join_dag(uip_ipaddr_t *from, rpl_dio_t *dio)
rpl_reset_dio_timer(dag, 1);
rpl_set_default_route(dag, from);
if(dio->dst_adv_supported && dio->dst_adv_trigger) {
if(should_send_dao(dag, dio, p)) {
rpl_schedule_dao(dag);
} else {
PRINTF("RPL: dst_adv_trigger not set in incoming DIO!\n");
@ -456,6 +464,7 @@ global_repair(uip_ipaddr_t *from, rpl_dag_t *dag, rpl_dio_t *dio)
remove_parents(dag, NULL, !POISON_ROUTES);
dag->version = dio->version;
dag->dtsn = 0;
dag->of->reset(dag);
if((p = rpl_add_parent(dag, dio, from)) == NULL) {
PRINTF("RPL: Failed to add a parent during the global repair\n");
@ -464,6 +473,9 @@ global_repair(uip_ipaddr_t *from, rpl_dag_t *dag, rpl_dio_t *dio)
rpl_set_default_route(dag, from);
dag->rank = dag->of->increment_rank(dio->dag_rank, p);
rpl_reset_dio_timer(dag, 1);
if(should_send_dao(dag, dio, p)) {
rpl_schedule_dao(dag);
}
}
PRINTF("RPL: Participating in a global repair (version=%u, rank=%hu)\n",
dag->version, dag->rank);
@ -475,6 +487,7 @@ rpl_repair_dag(rpl_dag_t *dag)
{
if(dag->rank == ROOT_RANK) {
dag->version++;
dag->dtsn = 1;
rpl_reset_dio_timer(dag, 1);
return 1;
}
@ -531,13 +544,15 @@ rpl_process_dio(uip_ipaddr_t *from, rpl_dio_t *dio)
return;
}
dag->dtsn = dio->dtsn;
/* This DIO pertains to a DAG that we are already part of. */
p = rpl_find_parent(dag, from);
if(p != NULL) {
if(dio->dst_adv_supported && dio->dst_adv_trigger &&
p == rpl_preferred_parent(dag)) {
if(should_send_dao(dag, dio, p)) {
rpl_schedule_dao(dag);
}
p->dtsn = dio->dtsn;
if(p->rank > dio->dag_rank) {
p->rank = dio->dag_rank;

View file

@ -33,7 +33,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rpl-icmp6.c,v 1.17 2010/06/06 12:45:55 joxe Exp $
* $Id: rpl-icmp6.c,v 1.18 2010/06/06 21:42:50 nvt-se Exp $
*/
/**
* \file
@ -59,6 +59,7 @@
#include "net/uip-debug.h"
/*---------------------------------------------------------------------------*/
#define RPL_DIO_GROUNDED 0x80
#define RPL_DIO_DEST_ADV_SUPPORTED 0x40
#define RPL_DIO_DEST_ADV_TRIGGER 0x20
@ -71,11 +72,14 @@
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
#define UIP_ICMP_PAYLOAD ((unsigned char *)&uip_buf[uip_l2_l3_icmp_hdr_len])
/*---------------------------------------------------------------------------*/
static void dis_input(void);
static void dio_input(void);
static void dao_input(void);
static void dao_ack_input(void);
static uint8_t dao_sequence;
/*---------------------------------------------------------------------------*/
static int
get_global_addr(uip_ipaddr_t *addr)
{
@ -94,7 +98,6 @@ get_global_addr(uip_ipaddr_t *addr)
}
return 0;
}
/*---------------------------------------------------------------------------*/
static uint32_t
get32(uint8_t *buffer, int pos)
@ -333,8 +336,6 @@ dio_output(rpl_dag_t *dag, uip_ipaddr_t *uc_addr)
buffer[pos++] = dag->of->ocp >> 8;
buffer[pos++] = dag->of->ocp & 0xff;
/* alignment here ??? */
/* always add a sub-option for DAG configuration */
buffer[pos++] = RPL_DIO_SUBOPT_DAG_CONF;
buffer[pos++] = 8;
@ -389,6 +390,7 @@ dio_output(rpl_dag_t *dag, uip_ipaddr_t *uc_addr)
static void
dao_input(void)
{
uip_ipaddr_t dao_sender_addr;
rpl_dag_t *dag;
unsigned char *buffer;
uint16_t sequence;
@ -404,14 +406,16 @@ dao_input(void)
int pos;
int len;
int i;
int learned_from;
lifetime = 0;
prefixlen = 0;
uip_ipaddr_copy(&dao_sender_addr, &UIP_IP_BUF->srcipaddr);
/* Destination Advertisement Object */
PRINTF("RPL: Received a DAO from ");
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINT6ADDR(&dao_sender_addr);
PRINTF("\n");
buffer = UIP_ICMP_PAYLOAD;
@ -464,7 +468,7 @@ dao_input(void)
}
}
PRINTF("RPL: DAO lifetime: %lu, prefix length: %u prefix:",
PRINTF("RPL: DAO lifetime: %lu, prefix length: %u prefix: ",
(unsigned long)lifetime, (unsigned)prefixlen);
PRINT6ADDR(&prefix);
PRINTF("\n");
@ -482,24 +486,26 @@ dao_input(void)
return;
}
rep = rpl_add_route(dag, &prefix, prefixlen,
&UIP_IP_BUF->srcipaddr);
learned_from = uip_is_addr_mcast(&dao_sender_addr) ?
RPL_ROUTE_FROM_MULTICAST_DAO : RPL_ROUTE_FROM_UNICAST_DAO;
rep = rpl_add_route(dag, &prefix, prefixlen, &dao_sender_addr);
if(rep == NULL) {
PRINTF("RPL: Could not add a route while receiving a DAO\n");
PRINTF("RPL: Could not add a route after receiving a DAO\n");
return;
} else {
rep->state.lifetime = lifetime;
rep->state.learned_from = learned_from;
}
rep->state.lifetime = lifetime;
if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)) {
rep->state.learned_from = RPL_ROUTE_FROM_MULTICAST_DAO;
} else {
rep->state.learned_from = RPL_ROUTE_FROM_UNICAST_DAO;
if(learned_from == RPL_ROUTE_FROM_UNICAST_DAO) {
if((n = rpl_preferred_parent(dag)) != NULL) {
PRINTF("RPL: Forwarding DAO to parent ");
PRINT6ADDR(&n->addr);
PRINTF("\n");
uip_icmp6_send(&n->addr, ICMP6_RPL, RPL_CODE_DAO, buffer_length);
} else if(flags & RPL_DAO_K_FLAG) {
dao_ack_output(dag, &dao_sender_addr, sequence);
}
}
}
@ -509,7 +515,6 @@ dao_output(rpl_parent_t *n, uint32_t lifetime)
{
rpl_dag_t *dag;
unsigned char *buffer;
static uint16_t sequence;
uint8_t prefixlen;
uip_ipaddr_t addr;
uip_ipaddr_t prefix;
@ -533,13 +538,13 @@ dao_output(rpl_parent_t *n, uint32_t lifetime)
buffer = UIP_ICMP_PAYLOAD;
++sequence;
++dao_sequence;
pos = 0;
buffer[pos++] = dag->instance_id;
buffer[pos++] = 0; /* no ack request, no DODAGID */
buffer[pos++] = RPL_DAO_K_FLAG; /* no ack request, no DODAGID */
buffer[pos++] = 0; /* reserved */
buffer[pos++] = sequence & 0xff;
buffer[pos++] = dao_sequence & 0xff;
/* create target subopt */
prefixlen = sizeof(prefix) * CHAR_BIT;
@ -577,6 +582,47 @@ dao_output(rpl_parent_t *n, uint32_t lifetime)
uip_icmp6_send(&addr, ICMP6_RPL, RPL_CODE_DAO, pos);
}
/*---------------------------------------------------------------------------*/
static void
dao_ack_input(void)
{
unsigned char *buffer;
uint8_t buffer_length;
uint8_t instance_id;
uint8_t sequence;
uint8_t status;
buffer = UIP_ICMP_PAYLOAD;
buffer_length = uip_len - uip_l2_l3_icmp_hdr_len;
instance_id = buffer[0];
sequence = buffer[2];
status = buffer[3];
PRINTF("RPL: Received a DAO ACK with sequence number %d and status %d from ",
sequence, status);
PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
PRINTF("\n");
}
/*---------------------------------------------------------------------------*/
void
dao_ack_output(rpl_dag_t *dag, uip_ipaddr_t *dest, uint8_t sequence)
{
unsigned char *buffer;
PRINTF("RPL: Sending a DAO ACK with sequence number %d to ", sequence);
PRINT6ADDR(dest);
PRINTF("\n");
buffer = UIP_ICMP_PAYLOAD;
buffer[0] = dag->instance_id;
buffer[1] = 0;
buffer[2] = sequence;
buffer[3] = 0;
uip_icmp6_send(dest, ICMP6_RPL, RPL_CODE_DAO_ACK, 4);
}
/*---------------------------------------------------------------------------*/
void
uip_rpl_input(void)
{
@ -591,6 +637,9 @@ uip_rpl_input(void)
case RPL_CODE_DAO:
dao_input();
break;
case RPL_CODE_DAO_ACK:
dao_ack_input();
break;
default:
PRINTF("RPL: received an unknown ICMP6 code (%u)\n", UIP_ICMP_BUF->icode);
break;

View file

@ -30,7 +30,7 @@
*
* Author: Joakim Eriksson, Nicolas Tsiftes
*
* $Id: rpl.h,v 1.12 2010/06/03 15:20:56 nvt-se Exp $
* $Id: rpl.h,v 1.13 2010/06/06 21:42:50 nvt-se Exp $
*/
#ifndef RPL_H
@ -90,7 +90,7 @@
/*---------------------------------------------------------------------------*/
/* Default values for RPL constants and variables. */
#define DEFAULT_DAO_LATENCY (CLOCK_SECOND * 10)
#define DEFAULT_DAO_LATENCY (CLOCK_SECOND * (1 + (random_rand() + 0xf)))
/* Special value indicating immediate removal. */
#define ZERO_LIFETIME 0
@ -162,6 +162,7 @@ struct rpl_parent {
uip_ipaddr_t addr;
rpl_rank_t rank;
uint8_t local_confidence;
uint8_t dtsn;
};
typedef struct rpl_parent rpl_parent_t;
@ -265,6 +266,7 @@ typedef struct rpl_dag rpl_dag_t;
void dis_output(uip_ipaddr_t *addr);
void dio_output(rpl_dag_t *, uip_ipaddr_t *uc_addr);
void dao_output(rpl_parent_t *, uint32_t lifetime);
void dao_ack_output(rpl_dag_t *, uip_ipaddr_t *, uint8_t);
void uip_rpl_input(void);
/* RPL logic functions. */