Contiki-NG
Loading...
Searching...
No Matches
rpl-mrhof.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \file
32 * The Minimum Rank with Hysteresis Objective Function (MRHOF), RFC6719
33 *
34 * This implementation uses the estimated number of
35 * transmissions (ETX) as the additive routing metric,
36 * and also provides stubs for the energy metric.
37 *
38 * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
39 */
40
41/**
42 * \addtogroup uip
43 * @{
44 */
45
46#include "net/routing/rpl-classic/rpl.h"
47#include "net/routing/rpl-classic/rpl-private.h"
48#include "net/nbr-table.h"
49#include "net/link-stats.h"
50
51#include "sys/log.h"
52
53#define LOG_MODULE "RPL"
54#define LOG_LEVEL LOG_LEVEL_RPL
55
56/*
57 * RFC6551 and RFC6719 do not mandate the use of a specific formula to
58 * compute the ETX value. This MRHOF implementation relies on the
59 * value computed by the link-stats module. It has an optional
60 * feature, RPL_MRHOF_CONF_SQUARED_ETX, that consists in squaring this
61 * value.
62 *
63 * Squaring basically penalizes bad links while preserving the
64 * semantics of ETX (1 = perfect link, more = worse link). As a
65 * result, MRHOF will favor good links over short paths. Without this
66 * feature, a hop with 50% PRR (ETX=2) is equivalent to two perfect
67 * hops with 100% PRR (ETX=1+1=2). With this feature, the former path
68 * obtains ETX=2*2=4 and the latter ETX=1*1+1*1=2.
69 *
70 * While this feature helps achieve extra reliability, it also results
71 * in added churn. In networks with high congestion or poor links,
72 * this can lead to poor connectivity due to more parent switches,
73 * loops, Trickle timer resets, etc.
74 */
75#ifdef RPL_MRHOF_CONF_SQUARED_ETX
76#define RPL_MRHOF_SQUARED_ETX RPL_MRHOF_CONF_SQUARED_ETX
77#else /* RPL_MRHOF_CONF_SQUARED_ETX */
78#define RPL_MRHOF_SQUARED_ETX 0
79#endif /* RPL_MRHOF_CONF_SQUARED_ETX */
80
81#if !RPL_MRHOF_SQUARED_ETX
82/* Configuration parameters of RFC6719. Reject parents that have a higher
83 link metric than the following. The default value is 512 but we use 1024. */
84#define MAX_LINK_METRIC 1024 /* Eq ETX of 8 */
85
86/*
87 * Hysteresis of MRHOF: the rank must differ more than
88 * PARENT_SWITCH_THRESHOLD_DIV in order to switch preferred
89 * parent. Default in RFC6719: 192, eq ETX of 1.5. We use a more
90 * aggressive setting: 96, eq ETX of 0.75.
91 */
92#define PARENT_SWITCH_THRESHOLD 96 /* Eq ETX of 0.75 */
93#else /* !RPL_MRHOF_SQUARED_ETX */
94#define MAX_LINK_METRIC 2048 /* Eq ETX of 4 */
95#define PARENT_SWITCH_THRESHOLD 160 /* Eq ETX of 1.25 (results in a churn comparable
96 to the threshold of 96 in the non-squared case) */
97#endif /* !RPL_MRHOF_SQUARED_ETX */
98
99/* Reject parents that have a higher path cost than the following. */
100#define MAX_PATH_COST 32768 /* Eq path ETX of 256 */
101
102/*---------------------------------------------------------------------------*/
103static void
104reset(rpl_dag_t *dag)
105{
106 LOG_INFO("Reset MRHOF\n");
107}
108/*---------------------------------------------------------------------------*/
109#if RPL_WITH_DAO_ACK
110static void
111dao_ack_callback(rpl_parent_t *p, int status)
112{
113 if(status == RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT) {
114 return;
115 }
116 /* Here we need to handle failed DAO's and other stuff. */
117 LOG_DBG("MRHOF - DAO ACK received with status: %d\n", status);
118 if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
119 /* punish the ETX as if this was 10 packets lost */
120 link_stats_packet_sent(rpl_get_parent_lladdr(p), MAC_TX_OK, 10);
121 } else if(status == RPL_DAO_ACK_TIMEOUT) { /* timeout = no ack */
122 /* Punish the total lack of ACK with a similar punishment. */
123 link_stats_packet_sent(rpl_get_parent_lladdr(p), MAC_TX_OK, 10);
124 }
125}
126#endif /* RPL_WITH_DAO_ACK */
127/*---------------------------------------------------------------------------*/
128static uint16_t
129parent_link_metric(rpl_parent_t *p)
130{
131 const struct link_stats *stats = rpl_get_parent_link_stats(p);
132 if(stats != NULL) {
133#if RPL_MRHOF_SQUARED_ETX
134 uint32_t squared_etx = ((uint32_t)stats->etx * stats->etx) / LINK_STATS_ETX_DIVISOR;
135 return (uint16_t)MIN(squared_etx, 0xffff);
136#else /* RPL_MRHOF_SQUARED_ETX */
137 return stats->etx;
138#endif /* RPL_MRHOF_SQUARED_ETX */
139 }
140 return 0xffff;
141}
142/*---------------------------------------------------------------------------*/
143static uint16_t
144parent_path_cost(rpl_parent_t *p)
145{
146 uint16_t base;
147
148 if(p == NULL || p->dag == NULL || p->dag->instance == NULL) {
149 return 0xffff;
150 }
151
152#if RPL_WITH_MC
153 /* Handle the different MC types */
154 switch(p->dag->instance->mc.type) {
155 case RPL_DAG_MC_ETX:
156 base = p->mc.obj.etx;
157 break;
158 case RPL_DAG_MC_ENERGY:
159 base = p->mc.obj.energy.energy_est << 8;
160 break;
161 default:
162 base = p->rank;
163 break;
164 }
165#else /* RPL_WITH_MC */
166 base = p->rank;
167#endif /* RPL_WITH_MC */
168
169 /* path cost upper bound: 0xffff */
170 return MIN((uint32_t)base + parent_link_metric(p), 0xffff);
171}
172/*---------------------------------------------------------------------------*/
173static rpl_rank_t
174rank_via_parent(rpl_parent_t *p)
175{
176 uint16_t min_hoprankinc;
177 uint16_t path_cost;
178
179 if(p == NULL || p->dag == NULL || p->dag->instance == NULL) {
180 return RPL_INFINITE_RANK;
181 }
182
183 min_hoprankinc = p->dag->instance->min_hoprankinc;
184 path_cost = parent_path_cost(p);
185
186 /* Rank lower-bound: parent rank + min_hoprankinc */
187 return MAX(MIN((uint32_t)p->rank + min_hoprankinc, 0xffff), path_cost);
188}
189/*---------------------------------------------------------------------------*/
190static int
191parent_is_acceptable(rpl_parent_t *p)
192{
193 uint16_t link_metric = parent_link_metric(p);
194 uint16_t path_cost = parent_path_cost(p);
195 /* Exclude links with too high link metrics or path cost. (RFC6719, 3.2.2) */
196 return link_metric <= MAX_LINK_METRIC && path_cost <= MAX_PATH_COST;
197}
198/*---------------------------------------------------------------------------*/
199static int
200parent_has_usable_link(rpl_parent_t *p)
201{
202 uint16_t link_metric = parent_link_metric(p);
203 /* Exclude links with too high link metrics */
204 return link_metric <= MAX_LINK_METRIC;
205}
206/*---------------------------------------------------------------------------*/
207static rpl_parent_t *
208best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
209{
210 rpl_dag_t *dag;
211 uint16_t p1_cost;
212 uint16_t p2_cost;
213 int p1_is_acceptable;
214 int p2_is_acceptable;
215
216 p1_is_acceptable = p1 != NULL && parent_is_acceptable(p1);
217 p2_is_acceptable = p2 != NULL && parent_is_acceptable(p2);
218
219 if(!p1_is_acceptable) {
220 return p2_is_acceptable ? p2 : NULL;
221 }
222 if(!p2_is_acceptable) {
223 return p1_is_acceptable ? p1 : NULL;
224 }
225
226 dag = p1->dag; /* Both parents are in the same DAG. */
227 p1_cost = parent_path_cost(p1);
228 p2_cost = parent_path_cost(p2);
229
230 /* Maintain the stability of the preferred parent in case of similar ranks. */
231 if(p1 == dag->preferred_parent || p2 == dag->preferred_parent) {
232 if(p1_cost < p2_cost + PARENT_SWITCH_THRESHOLD &&
233 p1_cost > p2_cost - PARENT_SWITCH_THRESHOLD) {
234 return dag->preferred_parent;
235 }
236 }
237
238 return p1_cost < p2_cost ? p1 : p2;
239}
240/*---------------------------------------------------------------------------*/
241static rpl_dag_t *
242best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
243{
244 if(d1->grounded != d2->grounded) {
245 return d1->grounded ? d1 : d2;
246 }
247
248 if(d1->preference != d2->preference) {
249 return d1->preference > d2->preference ? d1 : d2;
250 }
251
252 return d1->rank < d2->rank ? d1 : d2;
253}
254/*---------------------------------------------------------------------------*/
255#if !RPL_WITH_MC
256static void
257update_metric_container(rpl_instance_t *instance)
258{
259 instance->mc.type = RPL_DAG_MC_NONE;
260}
261#else /* RPL_WITH_MC */
262static void
263update_metric_container(rpl_instance_t *instance)
264{
265 rpl_dag_t *dag;
266 uint16_t path_cost;
267 uint8_t type;
268
269 dag = instance->current_dag;
270 if(dag == NULL || !dag->joined) {
271 LOG_WARN("Cannot update the metric container when not joined\n");
272 return;
273 }
274
275 if(dag->rank == ROOT_RANK(instance)) {
276 /* Configure the metric container at the root only; other nodes
277 are auto-configured when joining. */
278 instance->mc.type = RPL_DAG_MC;
279 instance->mc.flags = 0;
280 instance->mc.aggr = RPL_DAG_MC_AGGR_ADDITIVE;
281 instance->mc.prec = 0;
282 path_cost = dag->rank;
283 } else {
284 path_cost = parent_path_cost(dag->preferred_parent);
285 }
286
287 /* Handle the different MC types. */
288 switch(instance->mc.type) {
289 case RPL_DAG_MC_NONE:
290 break;
291 case RPL_DAG_MC_ETX:
292 instance->mc.length = sizeof(instance->mc.obj.etx);
293 instance->mc.obj.etx = path_cost;
294 break;
295 case RPL_DAG_MC_ENERGY:
296 instance->mc.length = sizeof(instance->mc.obj.energy);
297 if(dag->rank == ROOT_RANK(instance)) {
298 type = RPL_DAG_MC_ENERGY_TYPE_MAINS;
299 } else {
300 type = RPL_DAG_MC_ENERGY_TYPE_BATTERY;
301 }
302 instance->mc.obj.energy.flags = type << RPL_DAG_MC_ENERGY_TYPE;
303 /* Energy_est is only one byte -- use the least significant byte
304 of the path metric. */
305 instance->mc.obj.energy.energy_est = path_cost >> 8;
306 break;
307 default:
308 LOG_WARN("MRHOF, non-supported MC %u\n", instance->mc.type);
309 break;
310 }
311}
312#endif /* RPL_WITH_MC */
313/*---------------------------------------------------------------------------*/
314rpl_of_t rpl_mrhof = {
315 reset,
316#if RPL_WITH_DAO_ACK
317 dao_ack_callback,
318#endif
319 parent_link_metric,
320 parent_has_usable_link,
321 parent_path_cost,
322 rank_via_parent,
323 best_parent,
324 best_dag,
325 update_metric_container,
326 RPL_OCP_MRHOF
327};
328
329/** @}*/
Header file for the logging system.
@ MAC_TX_OK
The MAC layer transmission was OK.
Definition mac.h:93
RPL DAG structure.
Definition rpl.h:138
RPL instance structure.
Definition rpl.h:222
API for RPL objective functions (OF)
Definition rpl.h:204