Contiki-Inga 3.x
rpl-mrhof.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip6
3  * @{
4  */
5 /*
6  * Copyright (c) 2010, Swedish Institute of Computer Science.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  *
35  */
36 /**
37  * \file
38  * The Minimum Rank with Hysteresis Objective Function (MRHOF)
39  *
40  * This implementation uses the estimated number of
41  * transmissions (ETX) as the additive routing metric,
42  * and also provides stubs for the energy metric.
43  *
44  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
45  */
46 
47 #include "net/rpl/rpl-private.h"
48 #include "net/nbr-table.h"
49 
50 #define DEBUG DEBUG_NONE
51 #include "net/ip/uip-debug.h"
52 
53 static void reset(rpl_dag_t *);
54 static void neighbor_link_callback(rpl_parent_t *, int, int);
55 static rpl_parent_t *best_parent(rpl_parent_t *, rpl_parent_t *);
56 static rpl_dag_t *best_dag(rpl_dag_t *, rpl_dag_t *);
57 static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
58 static void update_metric_container(rpl_instance_t *);
59 
60 rpl_of_t rpl_mrhof = {
61  reset,
62  neighbor_link_callback,
63  best_parent,
64  best_dag,
65  calculate_rank,
66  update_metric_container,
67  1
68 };
69 
70 /* Constants for the ETX moving average */
71 #define ETX_SCALE 100
72 #define ETX_ALPHA 90
73 
74 /* Reject parents that have a higher link metric than the following. */
75 #define MAX_LINK_METRIC 10
76 
77 /* Reject parents that have a higher path cost than the following. */
78 #define MAX_PATH_COST 100
79 
80 /*
81  * The rank must differ more than 1/PARENT_SWITCH_THRESHOLD_DIV in order
82  * to switch preferred parent.
83  */
84 #define PARENT_SWITCH_THRESHOLD_DIV 2
85 
86 typedef uint16_t rpl_path_metric_t;
87 
88 static rpl_path_metric_t
89 calculate_path_metric(rpl_parent_t *p)
90 {
91  if(p == NULL) {
92  return MAX_PATH_COST * RPL_DAG_MC_ETX_DIVISOR;
93  }
94 
95 #if RPL_DAG_MC == RPL_DAG_MC_NONE
96  return p->rank + (uint16_t)p->link_metric;
97 #elif RPL_DAG_MC == RPL_DAG_MC_ETX
98  return p->mc.obj.etx + (uint16_t)p->link_metric;
99 #elif RPL_DAG_MC == RPL_DAG_MC_ENERGY
100  return p->mc.obj.energy.energy_est + (uint16_t)p->link_metric;
101 #else
102 #error "Unsupported RPL_DAG_MC configured. See rpl.h."
103 #endif /* RPL_DAG_MC */
104 }
105 
106 static void
107 reset(rpl_dag_t *dag)
108 {
109  PRINTF("RPL: Reset MRHOF\n");
110 }
111 
112 static void
113 neighbor_link_callback(rpl_parent_t *p, int status, int numtx)
114 {
115  uint16_t recorded_etx = p->link_metric;
116  uint16_t packet_etx = numtx * RPL_DAG_MC_ETX_DIVISOR;
117  uint16_t new_etx;
118 
119  /* Do not penalize the ETX when collisions or transmission errors occur. */
120  if(status == MAC_TX_OK || status == MAC_TX_NOACK) {
121  if(status == MAC_TX_NOACK) {
122  packet_etx = MAX_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;
123  }
124 
125  new_etx = ((uint32_t)recorded_etx * ETX_ALPHA +
126  (uint32_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
127 
128  PRINTF("RPL: ETX changed from %u to %u (packet ETX = %u)\n",
129  (unsigned)(recorded_etx / RPL_DAG_MC_ETX_DIVISOR),
130  (unsigned)(new_etx / RPL_DAG_MC_ETX_DIVISOR),
131  (unsigned)(packet_etx / RPL_DAG_MC_ETX_DIVISOR));
132  p->link_metric = new_etx;
133  }
134 }
135 
136 static rpl_rank_t
137 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
138 {
139  rpl_rank_t new_rank;
140  rpl_rank_t rank_increase;
141 
142  if(p == NULL) {
143  if(base_rank == 0) {
144  return INFINITE_RANK;
145  }
146  rank_increase = RPL_INIT_LINK_METRIC * RPL_DAG_MC_ETX_DIVISOR;
147  } else {
148  rank_increase = p->link_metric;
149  if(base_rank == 0) {
150  base_rank = p->rank;
151  }
152  }
153 
154  if(INFINITE_RANK - base_rank < rank_increase) {
155  /* Reached the maximum rank. */
156  new_rank = INFINITE_RANK;
157  } else {
158  /* Calculate the rank based on the new rank information from DIO or
159  stored otherwise. */
160  new_rank = base_rank + rank_increase;
161  }
162 
163  return new_rank;
164 }
165 
166 static rpl_dag_t *
167 best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
168 {
169  if(d1->grounded != d2->grounded) {
170  return d1->grounded ? d1 : d2;
171  }
172 
173  if(d1->preference != d2->preference) {
174  return d1->preference > d2->preference ? d1 : d2;
175  }
176 
177  return d1->rank < d2->rank ? d1 : d2;
178 }
179 
180 static rpl_parent_t *
181 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
182 {
183  rpl_dag_t *dag;
184  rpl_path_metric_t min_diff;
185  rpl_path_metric_t p1_metric;
186  rpl_path_metric_t p2_metric;
187 
188  dag = p1->dag; /* Both parents are in the same DAG. */
189 
190  min_diff = RPL_DAG_MC_ETX_DIVISOR /
191  PARENT_SWITCH_THRESHOLD_DIV;
192 
193  p1_metric = calculate_path_metric(p1);
194  p2_metric = calculate_path_metric(p2);
195 
196  /* Maintain stability of the preferred parent in case of similar ranks. */
197  if(p1 == dag->preferred_parent || p2 == dag->preferred_parent) {
198  if(p1_metric < p2_metric + min_diff &&
199  p1_metric > p2_metric - min_diff) {
200  PRINTF("RPL: MRHOF hysteresis: %u <= %u <= %u\n",
201  p2_metric - min_diff,
202  p1_metric,
203  p2_metric + min_diff);
204  return dag->preferred_parent;
205  }
206  }
207 
208  return p1_metric < p2_metric ? p1 : p2;
209 }
210 
211 #if RPL_DAG_MC == RPL_DAG_MC_NONE
212 static void
213 update_metric_container(rpl_instance_t *instance)
214 {
215  instance->mc.type = RPL_DAG_MC;
216 }
217 #else
218 static void
219 update_metric_container(rpl_instance_t *instance)
220 {
221  rpl_path_metric_t path_metric;
222  rpl_dag_t *dag;
223 #if RPL_DAG_MC == RPL_DAG_MC_ENERGY
224  uint8_t type;
225 #endif
226 
227  instance->mc.type = RPL_DAG_MC;
228  instance->mc.flags = RPL_DAG_MC_FLAG_P;
229  instance->mc.aggr = RPL_DAG_MC_AGGR_ADDITIVE;
230  instance->mc.prec = 0;
231 
232  dag = instance->current_dag;
233 
234  if (!dag->joined) {
235  PRINTF("RPL: Cannot update the metric container when not joined\n");
236  return;
237  }
238 
239  if(dag->rank == ROOT_RANK(instance)) {
240  path_metric = 0;
241  } else {
242  path_metric = calculate_path_metric(dag->preferred_parent);
243  }
244 
245 #if RPL_DAG_MC == RPL_DAG_MC_ETX
246  instance->mc.length = sizeof(instance->mc.obj.etx);
247  instance->mc.obj.etx = path_metric;
248 
249  PRINTF("RPL: My path ETX to the root is %u.%u\n",
250  instance->mc.obj.etx / RPL_DAG_MC_ETX_DIVISOR,
251  (instance->mc.obj.etx % RPL_DAG_MC_ETX_DIVISOR * 100) /
252  RPL_DAG_MC_ETX_DIVISOR);
253 #elif RPL_DAG_MC == RPL_DAG_MC_ENERGY
254  instance->mc.length = sizeof(instance->mc.obj.energy);
255 
256  if(dag->rank == ROOT_RANK(instance)) {
257  type = RPL_DAG_MC_ENERGY_TYPE_MAINS;
258  } else {
259  type = RPL_DAG_MC_ENERGY_TYPE_BATTERY;
260  }
261 
262  instance->mc.obj.energy.flags = type << RPL_DAG_MC_ENERGY_TYPE;
263  instance->mc.obj.energy.energy_est = path_metric;
264 #endif /* RPL_DAG_MC == RPL_DAG_MC_ETX */
265 }
266 #endif /* RPL_DAG_MC == RPL_DAG_MC_NONE */