2010-05-30 00:23:21 +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-05-30 00:23:21 +02:00
|
|
|
/**
|
|
|
|
* \file
|
2013-07-01 18:00:21 +02:00
|
|
|
* The Minimum Rank with Hysteresis Objective Function (MRHOF)
|
2011-02-04 15:45:24 +01:00
|
|
|
*
|
2014-05-30 11:01:20 +02:00
|
|
|
* This implementation uses the estimated number of
|
2013-07-01 18:00:21 +02:00
|
|
|
* transmissions (ETX) as the additive routing metric,
|
|
|
|
* and also provides stubs for the energy metric.
|
2010-05-30 00:23:21 +02:00
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-02-11 16:21:17 +01:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2013-07-29 18:49:21 +02:00
|
|
|
#include "net/nbr-table.h"
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2011-09-22 17:22:59 +02:00
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2011-02-11 16:21:17 +01:00
|
|
|
static void reset(rpl_dag_t *);
|
2013-07-29 21:50:33 +02:00
|
|
|
static void neighbor_link_callback(rpl_parent_t *, int, int);
|
2010-05-30 00:23:21 +02:00
|
|
|
static rpl_parent_t *best_parent(rpl_parent_t *, rpl_parent_t *);
|
2011-07-11 15:50:51 +02:00
|
|
|
static rpl_dag_t *best_dag(rpl_dag_t *, rpl_dag_t *);
|
2010-06-14 14:44:37 +02:00
|
|
|
static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
|
2011-07-11 15:50:51 +02:00
|
|
|
static void update_metric_container(rpl_instance_t *);
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2013-07-01 18:00:21 +02:00
|
|
|
rpl_of_t rpl_mrhof = {
|
2010-05-31 16:22:00 +02:00
|
|
|
reset,
|
2013-07-29 21:50:33 +02:00
|
|
|
neighbor_link_callback,
|
2010-05-30 00:23:21 +02:00
|
|
|
best_parent,
|
2011-07-11 15:50:51 +02:00
|
|
|
best_dag,
|
2010-06-14 14:44:37 +02:00
|
|
|
calculate_rank,
|
2011-02-15 01:13:30 +01:00
|
|
|
update_metric_container,
|
2010-05-30 00:23:21 +02:00
|
|
|
1
|
|
|
|
};
|
|
|
|
|
2013-07-29 21:50:33 +02:00
|
|
|
/* Constants for the ETX moving average */
|
|
|
|
#define ETX_SCALE 100
|
|
|
|
#define ETX_ALPHA 90
|
|
|
|
|
2011-02-04 15:45:24 +01:00
|
|
|
/* Reject parents that have a higher link metric than the following. */
|
|
|
|
#define MAX_LINK_METRIC 10
|
|
|
|
|
|
|
|
/* Reject parents that have a higher path cost than the following. */
|
|
|
|
#define MAX_PATH_COST 100
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rank must differ more than 1/PARENT_SWITCH_THRESHOLD_DIV in order
|
|
|
|
* to switch preferred parent.
|
|
|
|
*/
|
|
|
|
#define PARENT_SWITCH_THRESHOLD_DIV 2
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2011-03-15 01:16:20 +01:00
|
|
|
typedef uint16_t rpl_path_metric_t;
|
2011-02-15 01:13:30 +01:00
|
|
|
|
2011-04-25 22:37:25 +02:00
|
|
|
static rpl_path_metric_t
|
2011-03-15 01:16:20 +01:00
|
|
|
calculate_path_metric(rpl_parent_t *p)
|
2011-02-15 01:13:30 +01:00
|
|
|
{
|
2014-10-20 16:15:20 +02:00
|
|
|
uip_ds6_nbr_t *nbr;
|
2013-07-01 18:00:21 +02:00
|
|
|
if(p == NULL) {
|
2011-03-16 13:29:01 +01:00
|
|
|
return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;
|
2013-09-20 15:44:18 +02:00
|
|
|
}
|
2014-10-20 16:15:20 +02:00
|
|
|
nbr = rpl_get_nbr(p);
|
|
|
|
if(nbr == NULL) {
|
|
|
|
return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;
|
|
|
|
}
|
2013-07-01 18:00:21 +02:00
|
|
|
#if RPL_DAG_MC == RPL_DAG_MC_NONE
|
2014-10-20 16:15:20 +02:00
|
|
|
{
|
|
|
|
return p->rank + (uint16_t)nbr->link_metric;
|
|
|
|
}
|
2013-07-01 18:00:21 +02:00
|
|
|
#elif RPL_DAG_MC == RPL_DAG_MC_ETX
|
2014-10-20 16:15:20 +02:00
|
|
|
return p->mc.obj.etx + (uint16_t)nbr->link_metric;
|
2013-07-01 18:00:21 +02:00
|
|
|
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY
|
2014-10-20 16:15:20 +02:00
|
|
|
return p->mc.obj.energy.energy_est + (uint16_t)nbr->link_metric;
|
2013-09-20 15:44:18 +02:00
|
|
|
#else
|
|
|
|
#error "Unsupported RPL_DAG_MC configured. See rpl.h."
|
2013-07-01 18:00:21 +02:00
|
|
|
#endif /* RPL_DAG_MC */
|
2011-02-15 01:13:30 +01:00
|
|
|
}
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2010-05-31 16:22:00 +02:00
|
|
|
static void
|
2014-03-27 16:17:47 +01:00
|
|
|
reset(rpl_dag_t *dag)
|
2010-05-31 16:22:00 +02:00
|
|
|
{
|
2013-09-20 15:44:18 +02:00
|
|
|
PRINTF("RPL: Reset MRHOF\n");
|
2010-05-31 16:22:00 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 00:23:21 +02:00
|
|
|
static void
|
2013-07-29 21:50:33 +02:00
|
|
|
neighbor_link_callback(rpl_parent_t *p, int status, int numtx)
|
2010-05-30 00:23:21 +02:00
|
|
|
{
|
2014-10-20 16:15:20 +02:00
|
|
|
uint16_t recorded_etx = 0;
|
2013-09-20 15:44:18 +02:00
|
|
|
uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;
|
|
|
|
uint16_t new_etx;
|
2014-10-20 16:15:20 +02:00
|
|
|
uip_ds6_nbr_t *nbr = NULL;
|
|
|
|
|
|
|
|
nbr = rpl_get_nbr(p);
|
|
|
|
if(nbr == NULL) {
|
2015-02-06 09:22:16 +01:00
|
|
|
/* No neighbor for this parent - something bad has occurred */
|
2014-10-20 16:15:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-12-10 09:07:20 +01:00
|
|
|
|
2014-10-20 16:15:20 +02:00
|
|
|
recorded_etx = nbr->link_metric;
|
2013-09-20 15:44:18 +02:00
|
|
|
|
2013-07-29 21:50:33 +02:00
|
|
|
/* Do not penalize the ETX when collisions or transmission errors occur. */
|
|
|
|
if(status == MAC_TX_OK || status == MAC_TX_NOACK) {
|
|
|
|
if(status == MAC_TX_NOACK) {
|
2013-08-12 00:12:29 +02:00
|
|
|
packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;
|
2013-07-29 21:50:33 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 16:25:16 +02:00
|
|
|
if(p->flags & RPL_PARENT_FLAG_LINK_METRIC_VALID) {
|
|
|
|
/* We already have a valid link metric, use weighted moving average to update it */
|
|
|
|
new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +
|
|
|
|
(uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
|
|
|
|
} else {
|
|
|
|
/* We don't have a valid link metric, set it to the current packet's ETX */
|
|
|
|
new_etx = packet_etx;
|
|
|
|
/* Set link metric as valid */
|
|
|
|
p->flags |= RPL_PARENT_FLAG_LINK_METRIC_VALID;
|
|
|
|
}
|
2013-07-29 21:50:33 +02:00
|
|
|
|
2013-09-20 15:44:18 +02:00
|
|
|
PRINTF("RPL: ETX changed from %u to %u (packet ETX = %u)\n",
|
|
|
|
(unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),
|
|
|
|
(unsigned)(new_etx / RPL_DAG_MC_ETX_DIVISOR),
|
|
|
|
(unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));
|
2014-10-20 16:15:20 +02:00
|
|
|
/* update the link metric for this nbr */
|
|
|
|
nbr->link_metric = new_etx;
|
2013-07-29 21:50:33 +02:00
|
|
|
}
|
2010-05-30 00:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static rpl_rank_t
|
2010-06-14 14:44:37 +02:00
|
|
|
calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
|
2010-05-30 00:23:21 +02:00
|
|
|
{
|
|
|
|
rpl_rank_t new_rank;
|
2010-06-14 14:44:37 +02:00
|
|
|
rpl_rank_t rank_increase;
|
2014-10-20 16:15:20 +02:00
|
|
|
uip_ds6_nbr_t *nbr;
|
2010-06-14 14:44:37 +02:00
|
|
|
|
2014-10-20 16:15:20 +02:00
|
|
|
if(p == NULL || (nbr = rpl_get_nbr(p)) == NULL) {
|
2010-09-15 15:22:22 +02:00
|
|
|
if(base_rank == 0) {
|
2010-06-14 14:44:37 +02:00
|
|
|
return INFINITE_RANK;
|
|
|
|
}
|
2013-09-20 15:44:18 +02:00
|
|
|
rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;
|
2010-09-15 15:22:22 +02:00
|
|
|
} else {
|
2014-10-20 16:15:20 +02:00
|
|
|
rank_increase = nbr->link_metric;
|
2010-09-15 15:22:22 +02:00
|
|
|
if(base_rank == 0) {
|
|
|
|
base_rank = p->rank;
|
|
|
|
}
|
2010-06-14 14:44:37 +02:00
|
|
|
}
|
2010-05-30 00:23:21 +02:00
|
|
|
|
2010-06-14 14:44:37 +02:00
|
|
|
if(INFINITE_RANK - base_rank < rank_increase) {
|
2010-06-03 14:12:20 +02:00
|
|
|
/* Reached the maximum rank. */
|
|
|
|
new_rank = INFINITE_RANK;
|
|
|
|
} else {
|
|
|
|
/* Calculate the rank based on the new rank information from DIO or
|
|
|
|
stored otherwise. */
|
2010-06-14 14:44:37 +02:00
|
|
|
new_rank = base_rank + rank_increase;
|
2010-06-03 14:12:20 +02:00
|
|
|
}
|
2010-05-30 00:23:21 +02:00
|
|
|
|
|
|
|
return new_rank;
|
|
|
|
}
|
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
static rpl_dag_t *
|
|
|
|
best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
|
|
|
|
{
|
2012-01-04 14:13:54 +01:00
|
|
|
if(d1->grounded != d2->grounded) {
|
|
|
|
return d1->grounded ? d1 : d2;
|
2011-07-11 15:50:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 14:13:54 +01:00
|
|
|
if(d1->preference != d2->preference) {
|
|
|
|
return d1->preference > d2->preference ? d1 : d2;
|
2011-07-11 15:50:51 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 14:13:54 +01:00
|
|
|
return d1->rank < d2->rank ? d1 : d2;
|
2011-07-11 15:50:51 +02:00
|
|
|
}
|
|
|
|
|
2010-05-30 00:23:21 +02:00
|
|
|
static rpl_parent_t *
|
|
|
|
best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
|
|
|
|
{
|
2010-06-14 14:44:37 +02:00
|
|
|
rpl_dag_t *dag;
|
2011-03-15 01:16:20 +01:00
|
|
|
rpl_path_metric_t min_diff;
|
|
|
|
rpl_path_metric_t p1_metric;
|
|
|
|
rpl_path_metric_t p2_metric;
|
2010-06-14 14:44:37 +02:00
|
|
|
|
2013-09-20 15:44:18 +02:00
|
|
|
dag = p1->dag; /* Both parents are in the same DAG. */
|
2010-09-15 15:22:22 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
min_diff = RPL_DAG_MC_ETX_DIVISOR /
|
2011-02-20 20:15:40 +01:00
|
|
|
PARENT_SWITCH_THRESHOLD_DIV;
|
2011-02-15 01:13:30 +01:00
|
|
|
|
2011-03-15 01:16:20 +01:00
|
|
|
p1_metric = calculate_path_metric(p1);
|
|
|
|
p2_metric = calculate_path_metric(p2);
|
2010-06-14 14:44:37 +02:00
|
|
|
|
|
|
|
/* Maintain stability of the preferred parent in case of similar ranks. */
|
2011-03-04 16:40:40 +01:00
|
|
|
if(p1 == dag->preferred_parent || p2 == dag->preferred_parent) {
|
2011-03-15 01:16:20 +01:00
|
|
|
if(p1_metric < p2_metric + min_diff &&
|
|
|
|
p1_metric > p2_metric - min_diff) {
|
2011-03-04 16:40:40 +01:00
|
|
|
PRINTF("RPL: MRHOF hysteresis: %u <= %u <= %u\n",
|
2011-03-15 01:16:20 +01:00
|
|
|
p2_metric - min_diff,
|
|
|
|
p1_metric,
|
|
|
|
p2_metric + min_diff);
|
2011-03-04 16:40:40 +01:00
|
|
|
return dag->preferred_parent;
|
|
|
|
}
|
2010-06-14 14:44:37 +02:00
|
|
|
}
|
|
|
|
|
2011-03-15 01:16:20 +01:00
|
|
|
return p1_metric < p2_metric ? p1 : p2;
|
2010-05-30 00:23:21 +02:00
|
|
|
}
|
2011-02-15 01:13:30 +01:00
|
|
|
|
2013-09-20 15:44:18 +02:00
|
|
|
#if RPL_DAG_MC == RPL_DAG_MC_NONE
|
2011-02-15 01:13:30 +01:00
|
|
|
static void
|
2011-07-11 15:50:51 +02:00
|
|
|
update_metric_container(rpl_instance_t *instance)
|
2011-02-15 01:13:30 +01:00
|
|
|
{
|
2013-07-01 18:00:21 +02:00
|
|
|
instance->mc.type = RPL_DAG_MC;
|
2013-09-20 15:44:18 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void
|
|
|
|
update_metric_container(rpl_instance_t *instance)
|
|
|
|
{
|
2011-04-25 22:37:25 +02:00
|
|
|
rpl_path_metric_t path_metric;
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_dag_t *dag;
|
2011-04-25 22:37:25 +02:00
|
|
|
#if RPL_DAG_MC == RPL_DAG_MC_ENERGY
|
|
|
|
uint8_t type;
|
|
|
|
#endif
|
|
|
|
|
2013-07-01 18:00:21 +02:00
|
|
|
instance->mc.type = RPL_DAG_MC;
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->mc.flags = RPL_DAG_MC_FLAG_P;
|
|
|
|
instance->mc.aggr = RPL_DAG_MC_AGGR_ADDITIVE;
|
|
|
|
instance->mc.prec = 0;
|
|
|
|
|
|
|
|
dag = instance->current_dag;
|
|
|
|
|
|
|
|
if (!dag->joined) {
|
2013-09-20 15:44:18 +02:00
|
|
|
PRINTF("RPL: Cannot update the metric container when not joined\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-04-25 22:37:25 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(dag->rank == ROOT_RANK(instance)) {
|
2011-04-25 22:37:25 +02:00
|
|
|
path_metric = 0;
|
2011-02-15 01:13:30 +01:00
|
|
|
} else {
|
2011-04-25 22:37:25 +02:00
|
|
|
path_metric = calculate_path_metric(dag->preferred_parent);
|
2011-02-15 01:13:30 +01:00
|
|
|
}
|
2011-03-16 13:29:01 +01:00
|
|
|
|
2013-09-20 15:44:18 +02:00
|
|
|
#if RPL_DAG_MC == RPL_DAG_MC_ETX
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->mc.length = sizeof(instance->mc.obj.etx);
|
|
|
|
instance->mc.obj.etx = path_metric;
|
2011-04-25 22:37:25 +02:00
|
|
|
|
2011-03-16 13:29:01 +01:00
|
|
|
PRINTF("RPL: My path ETX to the root is %u.%u\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->mc.obj.etx / RPL_DAG_MC_ETX_DIVISOR,
|
2013-09-20 15:44:18 +02:00
|
|
|
(instance->mc.obj.etx % RPL_DAG_MC_ETX_DIVISOR * 100) /
|
|
|
|
RPL_DAG_MC_ETX_DIVISOR);
|
2011-03-15 01:16:20 +01:00
|
|
|
#elif RPL_DAG_MC == RPL_DAG_MC_ENERGY
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->mc.length = sizeof(instance->mc.obj.energy);
|
2011-04-25 22:37:25 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(dag->rank == ROOT_RANK(instance)) {
|
2011-04-25 22:37:25 +02:00
|
|
|
type = RPL_DAG_MC_ENERGY_TYPE_MAINS;
|
2011-03-15 01:16:20 +01:00
|
|
|
} else {
|
2011-04-25 22:37:25 +02:00
|
|
|
type = RPL_DAG_MC_ENERGY_TYPE_BATTERY;
|
2011-03-15 01:16:20 +01:00
|
|
|
}
|
2011-04-25 22:37:25 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->mc.obj.energy.flags = type << RPL_DAG_MC_ENERGY_TYPE;
|
|
|
|
instance->mc.obj.energy.energy_est = path_metric;
|
2013-09-20 15:44:18 +02:00
|
|
|
#endif /* RPL_DAG_MC == RPL_DAG_MC_ETX */
|
2011-02-15 01:13:30 +01:00
|
|
|
}
|
2013-09-20 15:44:18 +02:00
|
|
|
#endif /* RPL_DAG_MC == RPL_DAG_MC_NONE */
|
2014-05-30 11:01:20 +02:00
|
|
|
|
|
|
|
/** @}*/
|