2010-04-30 15:43:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* 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 Institute 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 INSTITUTE 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 INSTITUTE OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
2014-11-08 01:15:42 +01:00
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* RPL timer management.
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "contiki-conf.h"
|
2011-02-11 16:21:17 +01:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2011-12-09 13:15:04 +01:00
|
|
|
#include "net/ipv6/multicast/uip-mcast6.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "lib/random.h"
|
2010-06-14 14:44:37 +02:00
|
|
|
#include "sys/ctimer.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2010-11-03 16:41:23 +01:00
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static struct ctimer periodic_timer;
|
|
|
|
|
|
|
|
static void handle_periodic_timer(void *ptr);
|
2011-07-11 15:50:51 +02:00
|
|
|
static void new_dio_interval(rpl_instance_t *instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
static void handle_dio_timer(void *ptr);
|
|
|
|
|
|
|
|
static uint16_t next_dis;
|
2010-05-09 19:52:37 +02:00
|
|
|
|
|
|
|
/* dio_send_ok is true if the node is ready to send DIOs */
|
|
|
|
static uint8_t dio_send_ok;
|
|
|
|
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_periodic_timer(void *ptr)
|
|
|
|
{
|
|
|
|
rpl_purge_routes();
|
2010-06-14 14:44:37 +02:00
|
|
|
rpl_recalculate_ranks();
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
/* handle DIS */
|
2014-03-18 08:11:34 +01:00
|
|
|
#if RPL_DIS_SEND
|
2010-04-30 15:43:53 +02:00
|
|
|
next_dis++;
|
2011-07-11 15:50:51 +02:00
|
|
|
if(rpl_get_any_dag() == NULL && next_dis >= RPL_DIS_INTERVAL) {
|
2010-05-05 00:55:32 +02:00
|
|
|
next_dis = 0;
|
|
|
|
dis_output(NULL);
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif
|
|
|
|
ctimer_reset(&periodic_timer);
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
2011-07-11 15:50:51 +02:00
|
|
|
new_dio_interval(rpl_instance_t *instance)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
2010-12-15 13:12:27 +01:00
|
|
|
uint32_t time;
|
2012-11-26 19:35:39 +01:00
|
|
|
clock_time_t ticks;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2010-05-05 00:55:32 +02:00
|
|
|
/* TODO: too small timer intervals for many cases */
|
2011-07-11 15:50:51 +02:00
|
|
|
time = 1UL << instance->dio_intcurrent;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-12-15 13:12:27 +01:00
|
|
|
/* Convert from milliseconds to CLOCK_TICKS. */
|
2012-11-26 19:35:39 +01:00
|
|
|
ticks = (time * CLOCK_SECOND) / 1000;
|
|
|
|
instance->dio_next_delay = ticks;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
/* random number between I/2 and I */
|
2012-11-26 19:35:39 +01:00
|
|
|
ticks = ticks / 2 + (ticks / 2 * (uint32_t)random_rand()) / RANDOM_RAND_MAX;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-12-15 13:12:27 +01:00
|
|
|
/*
|
|
|
|
* The intervals must be equally long among the nodes for Trickle to
|
|
|
|
* operate efficiently. Therefore we need to calculate the delay between
|
|
|
|
* the randomized time and the start time of the next interval.
|
|
|
|
*/
|
2012-11-26 19:35:39 +01:00
|
|
|
instance->dio_next_delay -= ticks;
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_send = 1;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
#if RPL_CONF_STATS
|
|
|
|
/* keep some stats */
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_totint++;
|
|
|
|
instance->dio_totrecv += instance->dio_counter;
|
2010-10-28 22:39:06 +02:00
|
|
|
ANNOTATE("#A rank=%u.%u(%u),stats=%d %d %d %d,color=%s\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
DAG_RANK(instance->current_dag->rank, instance),
|
|
|
|
(10 * (instance->current_dag->rank % instance->min_hoprankinc)) / instance->min_hoprankinc,
|
|
|
|
instance->current_dag->version,
|
|
|
|
instance->dio_totint, instance->dio_totsend,
|
|
|
|
instance->dio_totrecv,instance->dio_intcurrent,
|
|
|
|
instance->current_dag->rank == ROOT_RANK(instance) ? "BLUE" : "ORANGE");
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
|
|
|
|
|
|
|
/* reset the redundancy counter */
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter = 0;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
/* schedule the timer */
|
2012-11-26 19:35:39 +01:00
|
|
|
PRINTF("RPL: Scheduling DIO timer %lu ticks in future (Interval)\n", ticks);
|
|
|
|
ctimer_set(&instance->dio_timer, ticks, &handle_dio_timer, instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_dio_timer(void *ptr)
|
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_instance_t *instance;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
instance = (rpl_instance_t *)ptr;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
PRINTF("RPL: DIO Timer triggered\n");
|
2010-05-09 19:52:37 +02:00
|
|
|
if(!dio_send_ok) {
|
|
|
|
if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) {
|
|
|
|
dio_send_ok = 1;
|
|
|
|
} else {
|
2010-06-14 14:44:37 +02:00
|
|
|
PRINTF("RPL: Postponing DIO transmission since link local address is not ok\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dio_timer, CLOCK_SECOND, &handle_dio_timer, instance);
|
2010-05-09 19:52:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->dio_send) {
|
2010-04-30 15:43:53 +02:00
|
|
|
/* send DIO if counter is less than desired redundancy */
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->dio_counter < instance->dio_redundancy) {
|
2010-04-30 15:43:53 +02:00
|
|
|
#if RPL_CONF_STATS
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_totsend++;
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
2011-07-11 15:50:51 +02:00
|
|
|
dio_output(instance, NULL);
|
2010-04-30 15:43:53 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("RPL: Supressing DIO transmission (%d >= %d)\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter, instance->dio_redundancy);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_send = 0;
|
2012-01-04 11:22:11 +01:00
|
|
|
PRINTF("RPL: Scheduling DIO timer %lu ticks in future (sent)\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_next_delay);
|
|
|
|
ctimer_set(&instance->dio_timer, instance->dio_next_delay, handle_dio_timer, instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
} else {
|
|
|
|
/* check if we need to double interval */
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->dio_intcurrent < instance->dio_intmin + instance->dio_intdoubl) {
|
|
|
|
instance->dio_intcurrent++;
|
|
|
|
PRINTF("RPL: DIO Timer interval doubled %d\n", instance->dio_intcurrent);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2011-07-11 15:50:51 +02:00
|
|
|
new_dio_interval(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
void
|
|
|
|
rpl_reset_periodic_timer(void)
|
|
|
|
{
|
2012-11-26 19:36:51 +01:00
|
|
|
next_dis = RPL_DIS_INTERVAL / 2 +
|
|
|
|
((uint32_t)RPL_DIS_INTERVAL * (uint32_t)random_rand()) / RANDOM_RAND_MAX -
|
|
|
|
RPL_DIS_START_DELAY;
|
2010-04-30 15:43:53 +02:00
|
|
|
ctimer_set(&periodic_timer, CLOCK_SECOND, handle_periodic_timer, NULL);
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-07-11 15:50:51 +02:00
|
|
|
/* Resets the DIO timer in the instance to its minimal interval. */
|
2010-04-30 15:43:53 +02:00
|
|
|
void
|
2012-01-04 11:22:11 +01:00
|
|
|
rpl_reset_dio_timer(rpl_instance_t *instance)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
#if !RPL_LEAF_ONLY
|
2011-11-16 09:49:22 +01:00
|
|
|
/* Do not reset if we are already on the minimum interval,
|
|
|
|
unless forced to do so. */
|
2012-01-04 11:22:11 +01:00
|
|
|
if(instance->dio_intcurrent > instance->dio_intmin) {
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter = 0;
|
|
|
|
instance->dio_intcurrent = instance->dio_intmin;
|
|
|
|
new_dio_interval(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2010-10-28 22:39:06 +02:00
|
|
|
#if RPL_CONF_STATS
|
|
|
|
rpl_stats.resets++;
|
2011-07-11 15:50:51 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
|
|
|
#endif /* RPL_LEAF_ONLY */
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
static void handle_dao_timer(void *ptr);
|
|
|
|
static void
|
|
|
|
set_dao_lifetime_timer(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
if(rpl_get_mode() == RPL_MODE_FEATHER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up another DAO within half the expiration time, if such a
|
|
|
|
time has been configured */
|
|
|
|
if(instance->lifetime_unit != 0xffff && instance->default_lifetime != 0xff) {
|
|
|
|
clock_time_t expiration_time;
|
|
|
|
expiration_time = (clock_time_t)instance->default_lifetime *
|
|
|
|
(clock_time_t)instance->lifetime_unit *
|
|
|
|
CLOCK_SECOND / 2;
|
|
|
|
PRINTF("RPL: Scheduling DAO lifetime timer %u ticks in the future\n",
|
|
|
|
(unsigned)expiration_time);
|
|
|
|
ctimer_set(&instance->dao_lifetime_timer, expiration_time,
|
|
|
|
handle_dao_timer, instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_dao_timer(void *ptr)
|
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_instance_t *instance;
|
2011-12-09 13:15:04 +01:00
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
uip_mcast6_route_t *mcast_route;
|
|
|
|
uint8_t i;
|
|
|
|
#endif
|
2010-05-25 23:58:54 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
instance = (rpl_instance_t *)ptr;
|
2010-05-09 21:12:47 +02:00
|
|
|
|
2011-11-16 09:49:22 +01:00
|
|
|
if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) {
|
|
|
|
PRINTF("RPL: Postpone DAO transmission\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance);
|
2010-05-09 21:12:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-28 17:08:44 +02:00
|
|
|
/* Send the DAO to the DAO parent set -- the preferred parent in our case. */
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->current_dag->preferred_parent != NULL) {
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL: handle_dao_timer - sending DAO\n");
|
2011-06-28 17:08:44 +02:00
|
|
|
/* Set the route lifetime to the default value. */
|
2011-07-11 15:50:51 +02:00
|
|
|
dao_output(instance->current_dag->preferred_parent, instance->default_lifetime);
|
2011-12-09 13:15:04 +01:00
|
|
|
|
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
/* Send DAOs for multicast prefixes only if the instance is in MOP 3 */
|
|
|
|
if(instance->mop == RPL_MOP_STORING_MULTICAST) {
|
|
|
|
/* Send a DAO for own multicast addresses */
|
|
|
|
for(i = 0; i < UIP_DS6_MADDR_NB; i++) {
|
|
|
|
if(uip_ds6_if.maddr_list[i].isused
|
|
|
|
&& uip_is_addr_mcast_global(&uip_ds6_if.maddr_list[i].ipaddr)) {
|
|
|
|
dao_output_target(instance->current_dag->preferred_parent,
|
|
|
|
&uip_ds6_if.maddr_list[i].ipaddr, RPL_MCAST_LIFETIME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over multicast routes and send DAOs */
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
while(mcast_route != NULL) {
|
|
|
|
/* Don't send if it's also our own address, done that already */
|
|
|
|
if(uip_ds6_maddr_lookup(&mcast_route->group) == NULL) {
|
|
|
|
dao_output_target(instance->current_dag->preferred_parent,
|
|
|
|
&mcast_route->group, RPL_MCAST_LIFETIME);
|
|
|
|
}
|
|
|
|
mcast_route = list_item_next(mcast_route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-09 21:12:47 +02:00
|
|
|
} else {
|
2011-06-28 17:08:44 +02:00
|
|
|
PRINTF("RPL: No suitable DAO parent\n");
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2013-11-20 14:02:38 +01:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_stop(&instance->dao_timer);
|
2013-11-20 14:02:38 +01:00
|
|
|
|
|
|
|
if(etimer_expired(&instance->dao_lifetime_timer.etimer)) {
|
|
|
|
set_dao_lifetime_timer(instance);
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
static void
|
|
|
|
schedule_dao(rpl_instance_t *instance, clock_time_t latency)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
|
|
|
clock_time_t expiration_time;
|
2010-05-24 18:38:56 +02:00
|
|
|
|
2013-11-20 14:10:39 +01:00
|
|
|
if(rpl_get_mode() == RPL_MODE_FEATHER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
expiration_time = etimer_expiration_time(&instance->dao_timer.etimer);
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(!etimer_expired(&instance->dao_timer.etimer)) {
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL: DAO timer already scheduled\n");
|
|
|
|
} else {
|
2013-11-20 14:02:38 +01:00
|
|
|
if(latency != 0) {
|
|
|
|
expiration_time = latency / 2 +
|
|
|
|
(random_rand() % (latency));
|
|
|
|
} else {
|
|
|
|
expiration_time = 0;
|
|
|
|
}
|
2010-12-17 16:24:25 +01:00
|
|
|
PRINTF("RPL: Scheduling DAO timer %u ticks in the future\n",
|
|
|
|
(unsigned)expiration_time);
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dao_timer, expiration_time,
|
|
|
|
handle_dao_timer, instance);
|
2013-11-20 14:02:38 +01:00
|
|
|
|
|
|
|
set_dao_lifetime_timer(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
void
|
|
|
|
rpl_schedule_dao(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
schedule_dao(instance, RPL_DAO_LATENCY);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_schedule_dao_immediately(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
schedule_dao(instance, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_cancel_dao(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
ctimer_stop(&instance->dao_timer);
|
|
|
|
ctimer_stop(&instance->dao_lifetime_timer);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-05-30 11:01:20 +02:00
|
|
|
|
|
|
|
/** @}*/
|