Contiki-NG
rpl-of0.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  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * An implementation of RPL's objective function 0, RFC6552
36  *
37  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
38  */
39 
40 /**
41  * \addtogroup uip
42  * @{
43  */
44 
45 #include "net/routing/rpl-classic/rpl.h"
46 #include "net/routing/rpl-classic/rpl-private.h"
47 #include "net/nbr-table.h"
48 #include "net/link-stats.h"
49 
50 #include "sys/log.h"
51 
52 #define LOG_MODULE "RPL"
53 #define LOG_LEVEL LOG_LEVEL_RPL
54 
55 /* Constants from RFC6552. We use the default values. */
56 #define RANK_STRETCH 0 /* Must be in the range [0;5] */
57 #define RANK_FACTOR 1 /* Must be in the range [1;4] */
58 
59 #define MIN_STEP_OF_RANK 1
60 #define MAX_STEP_OF_RANK 9
61 
62 /* OF0 computes rank increase as follows:
63  * rank_increase = (RANK_FACTOR * STEP_OF_RANK + RANK_STRETCH) * min_hop_rank_increase
64  * STEP_OF_RANK is an implementation-specific scalar value in the range [1;9].
65  * RFC6552 provides a default value of 3 but recommends to use a dynamic link metric
66  * such as ETX.
67  * */
68 
69 #define RPL_OF0_FIXED_SR 0
70 #define RPL_OF0_ETX_BASED_SR 1
71 /* Select RPL_OF0_FIXED_SR or RPL_OF0_ETX_BASED_SR */
72 #ifdef RPL_OF0_CONF_SR
73 #define RPL_OF0_SR RPL_OF0_CONF_SR
74 #else /* RPL_OF0_CONF_SR */
75 #define RPL_OF0_SR RPL_OF0_ETX_BASED_SR
76 #endif /* RPL_OF0_CONF_SR */
77 
78 #if RPL_OF0_FIXED_SR
79 #define STEP_OF_RANK(p) (3)
80 #endif /* RPL_OF0_FIXED_SR */
81 
82 #if RPL_OF0_ETX_BASED_SR
83 /* Numbers suggested by P. Thubert for in the 6TiSCH WG. Anything that maps ETX to
84  * a step between 1 and 9 works. */
85 #define STEP_OF_RANK(p) (((3 * parent_link_metric(p)) / LINK_STATS_ETX_DIVISOR) - 2)
86 #endif /* RPL_OF0_ETX_BASED_SR */
87 
88 /*---------------------------------------------------------------------------*/
89 static void
90 reset(rpl_dag_t *dag)
91 {
92  LOG_INFO("Reset OF0\n");
93 }
94 /*---------------------------------------------------------------------------*/
95 #if RPL_WITH_DAO_ACK
96 static void
97 dao_ack_callback(rpl_parent_t *p, int status)
98 {
99  if(status == RPL_DAO_ACK_UNABLE_TO_ADD_ROUTE_AT_ROOT) {
100  return;
101  }
102  /* here we need to handle failed DAO's and other stuff */
103  LOG_DBG("OF0 - DAO ACK received with status: %d\n", status);
104  if(status >= RPL_DAO_ACK_UNABLE_TO_ACCEPT) {
105  /* punish the ETX as if this was 10 packets lost */
106  link_stats_packet_sent(rpl_get_parent_lladdr(p), MAC_TX_OK, 10);
107  } else if(status == RPL_DAO_ACK_TIMEOUT) { /* timeout = no ack */
108  /* punish the total lack of ACK with a similar punishment */
109  link_stats_packet_sent(rpl_get_parent_lladdr(p), MAC_TX_OK, 10);
110  }
111 }
112 #endif /* RPL_WITH_DAO_ACK */
113 /*---------------------------------------------------------------------------*/
114 static uint16_t
115 parent_link_metric(rpl_parent_t *p)
116 {
117  /* OF0 operates without metric container; the only metric we have is ETX */
118  const struct link_stats *stats = rpl_get_parent_link_stats(p);
119  return stats != NULL ? stats->etx : 0xffff;
120 }
121 /*---------------------------------------------------------------------------*/
122 static uint16_t
123 parent_rank_increase(rpl_parent_t *p)
124 {
125  uint16_t min_hoprankinc;
126  if(p == NULL || p->dag == NULL || p->dag->instance == NULL) {
127  return RPL_INFINITE_RANK;
128  }
129  min_hoprankinc = p->dag->instance->min_hoprankinc;
130  return (RANK_FACTOR * STEP_OF_RANK(p) + RANK_STRETCH) * min_hoprankinc;
131 }
132 /*---------------------------------------------------------------------------*/
133 static uint16_t
134 parent_path_cost(rpl_parent_t *p)
135 {
136  if(p == NULL) {
137  return 0xffff;
138  }
139  /* path cost upper bound: 0xffff */
140  return MIN((uint32_t)p->rank + parent_link_metric(p), 0xffff);
141 }
142 /*---------------------------------------------------------------------------*/
143 static rpl_rank_t
144 rank_via_parent(rpl_parent_t *p)
145 {
146  if(p == NULL) {
147  return RPL_INFINITE_RANK;
148  } else {
149  return MIN((uint32_t)p->rank + parent_rank_increase(p), RPL_INFINITE_RANK);
150  }
151 }
152 /*---------------------------------------------------------------------------*/
153 static int
154 parent_is_acceptable(rpl_parent_t *p)
155 {
156  return STEP_OF_RANK(p) >= MIN_STEP_OF_RANK
157  && STEP_OF_RANK(p) <= MAX_STEP_OF_RANK;
158 }
159 /*---------------------------------------------------------------------------*/
160 static int
161 parent_has_usable_link(rpl_parent_t *p)
162 {
163  return parent_is_acceptable(p);
164 }
165 /*---------------------------------------------------------------------------*/
166 static rpl_parent_t *
167 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
168 {
169  rpl_dag_t *dag;
170  uint16_t p1_cost;
171  uint16_t p2_cost;
172  int p1_is_acceptable;
173  int p2_is_acceptable;
174 
175  p1_is_acceptable = p1 != NULL && parent_is_acceptable(p1);
176  p2_is_acceptable = p2 != NULL && parent_is_acceptable(p2);
177 
178  if(!p1_is_acceptable) {
179  return p2_is_acceptable ? p2 : NULL;
180  }
181  if(!p2_is_acceptable) {
182  return p1_is_acceptable ? p1 : NULL;
183  }
184 
185  dag = p1->dag; /* Both parents are in the same DAG. */
186  p1_cost = parent_path_cost(p1);
187  p2_cost = parent_path_cost(p2);
188 
189  /* Paths costs coarse-grained (multiple of min_hoprankinc), we operate without hysteresis */
190  if(p1_cost != p2_cost) {
191  /* Pick parent with lowest path cost */
192  return p1_cost < p2_cost ? p1 : p2;
193  } else {
194  /* We have a tie! */
195  /* Stik to current preferred parent if possible */
196  if(p1 == dag->preferred_parent || p2 == dag->preferred_parent) {
197  return dag->preferred_parent;
198  }
199  /* None of the nodes is the current preferred parent,
200  * choose parent with best link metric */
201  return parent_link_metric(p1) < parent_link_metric(p2) ? p1 : p2;
202  }
203 }
204 /*---------------------------------------------------------------------------*/
205 static rpl_dag_t *
206 best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
207 {
208  if(d1->grounded != d2->grounded) {
209  return d1->grounded ? d1 : d2;
210  }
211 
212  if(d1->preference != d2->preference) {
213  return d1->preference > d2->preference ? d1 : d2;
214  }
215 
216  return d1->rank < d2->rank ? d1 : d2;
217 }
218 /*---------------------------------------------------------------------------*/
219 static void
220 update_metric_container(rpl_instance_t *instance)
221 {
222  instance->mc.type = RPL_DAG_MC_NONE;
223 }
224 /*---------------------------------------------------------------------------*/
225 rpl_of_t rpl_of0 = {
226  reset,
227 #if RPL_WITH_DAO_ACK
228  dao_ack_callback,
229 #endif
230  parent_link_metric,
231  parent_has_usable_link,
232  parent_path_cost,
233  rank_via_parent,
234  best_parent,
235  best_dag,
236  update_metric_container,
237  RPL_OCP_OF0
238 };
239 
240 /** @}*/
RPL DAG structure.
Definition: rpl.h:135
RPL instance structure.
Definition: rpl.h:219
The MAC layer transmission was OK.
Definition: mac.h:87
API for RPL objective functions (OF)
Definition: rpl.h:201
Header file for the logging system