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  * \addtogroup rpl-lite
35  * @{
36  *
37  * \file
38  * An implementation of RPL's objective function 0, RFC6552
39  *
40  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
41  * Simon Duquennoy <simon.duquennoy@inria.fr>
42  */
43 
44 #include "net/routing/rpl-lite/rpl.h"
45 #include "net/nbr-table.h"
46 #include "net/link-stats.h"
47 
48 /* Log configuration */
49 #include "sys/log.h"
50 #define LOG_MODULE "RPL"
51 #define LOG_LEVEL LOG_LEVEL_RPL
52 
53 /* Constants from RFC6552. We use the default values. */
54 #define RANK_STRETCH 0 /* Must be in the range [0;5] */
55 #define RANK_FACTOR 1 /* Must be in the range [1;4] */
56 
57 #define MIN_STEP_OF_RANK 1
58 #define MAX_STEP_OF_RANK 9
59 
60 /* OF0 computes rank increase as follows:
61  * rank_increase = (RANK_FACTOR * STEP_OF_RANK + RANK_STRETCH) * min_hop_rank_increase
62  * STEP_OF_RANK is an implementation-specific scalar value in the range [1;9].
63  * RFC6552 provides a default value of 3 but recommends to use a dynamic link metric
64  * such as ETX.
65  * */
66 
67 #define RPL_OF0_FIXED_SR 0
68 #define RPL_OF0_ETX_BASED_SR 1
69 /* Select RPL_OF0_FIXED_SR or RPL_OF0_ETX_BASED_SR */
70 #ifdef RPL_OF0_CONF_SR
71 #define RPL_OF0_SR RPL_OF0_CONF_SR
72 #else /* RPL_OF0_CONF_SR */
73 #define RPL_OF0_SR RPL_OF0_ETX_BASED_SR
74 #endif /* RPL_OF0_CONF_SR */
75 
76 #if RPL_OF0_FIXED_SR
77 #define STEP_OF_RANK(nbr) (3)
78 #endif /* RPL_OF0_FIXED_SR */
79 
80 #if RPL_OF0_ETX_BASED_SR
81 /* Numbers suggested by P. Thubert for in the 6TiSCH WG. Anything that maps ETX to
82  * a step between 1 and 9 works. */
83 #define STEP_OF_RANK(nbr) (((3 * nbr_link_metric(nbr)) / LINK_STATS_ETX_DIVISOR) - 2)
84 #endif /* RPL_OF0_ETX_BASED_SR */
85 
86 /*---------------------------------------------------------------------------*/
87 static void
88 reset(void)
89 {
90  LOG_INFO("reset OF0\n");
91 }
92 /*---------------------------------------------------------------------------*/
93 static uint16_t
94 nbr_link_metric(rpl_nbr_t *nbr)
95 {
96  /* OF0 operates without metric container; the only metric we have is ETX */
97  const struct link_stats *stats = rpl_neighbor_get_link_stats(nbr);
98  return stats != NULL ? stats->etx : 0xffff;
99 }
100 /*---------------------------------------------------------------------------*/
101 static uint16_t
102 nbr_rank_increase(rpl_nbr_t *nbr)
103 {
104  uint16_t min_hoprankinc;
105  if(nbr == NULL) {
106  return RPL_INFINITE_RANK;
107  }
108  min_hoprankinc = curr_instance.min_hoprankinc;
109  return (RANK_FACTOR * STEP_OF_RANK(nbr) + RANK_STRETCH) * min_hoprankinc;
110 }
111 /*---------------------------------------------------------------------------*/
112 static uint16_t
113 nbr_path_cost(rpl_nbr_t *nbr)
114 {
115  if(nbr == NULL) {
116  return 0xffff;
117  }
118  /* path cost upper bound: 0xffff */
119  return MIN((uint32_t)nbr->rank + nbr_link_metric(nbr), 0xffff);
120 }
121 /*---------------------------------------------------------------------------*/
122 static rpl_rank_t
123 rank_via_nbr(rpl_nbr_t *nbr)
124 {
125  if(nbr == NULL) {
126  return RPL_INFINITE_RANK;
127  } else {
128  return MIN((uint32_t)nbr->rank + nbr_rank_increase(nbr), RPL_INFINITE_RANK);
129  }
130 }
131 /*---------------------------------------------------------------------------*/
132 static int
133 nbr_has_usable_link(rpl_nbr_t *nbr)
134 {
135  return 1;
136 }
137 /*---------------------------------------------------------------------------*/
138 static int
139 nbr_is_acceptable_parent(rpl_nbr_t *nbr)
140 {
141  return STEP_OF_RANK(nbr) >= MIN_STEP_OF_RANK
142  && STEP_OF_RANK(nbr) <= MAX_STEP_OF_RANK;
143 }
144 /*---------------------------------------------------------------------------*/
145 static rpl_nbr_t *
146 best_parent(rpl_nbr_t *nbr1, rpl_nbr_t *nbr2)
147 {
148  uint16_t nbr1_cost;
149  uint16_t nbr2_cost;
150  int nbr1_is_acceptable;
151  int nbr2_is_acceptable;
152 
153  nbr1_is_acceptable = nbr1 != NULL && nbr_is_acceptable_parent(nbr1);
154  nbr2_is_acceptable = nbr2 != NULL && nbr_is_acceptable_parent(nbr2);
155 
156  if(!nbr1_is_acceptable) {
157  return nbr2_is_acceptable ? nbr2 : NULL;
158  }
159  if(!nbr2_is_acceptable) {
160  return nbr1_is_acceptable ? nbr1 : NULL;
161  }
162 
163  nbr1_cost = nbr_path_cost(nbr1);
164  nbr2_cost = nbr_path_cost(nbr2);
165 
166  /* Paths costs coarse-grained (multiple of min_hoprankinc), we operate without hysteresis */
167  if(nbr1_cost != nbr2_cost) {
168  /* Pick nbr with lowest path cost */
169  return nbr1_cost < nbr2_cost ? nbr1 : nbr2;
170  } else {
171  /* We have a tie! */
172  /* Stik to current preferred parent if possible */
173  if(nbr1 == curr_instance.dag.preferred_parent || nbr2 == curr_instance.dag.preferred_parent) {
174  return curr_instance.dag.preferred_parent;
175  }
176  /* None of the nodes is the current preferred parent,
177  * choose nbr with best link metric */
178  return nbr_link_metric(nbr1) < nbr_link_metric(nbr2) ? nbr1 : nbr2;
179  }
180 }
181 /*---------------------------------------------------------------------------*/
182 static void
183 update_metric_container(void)
184 {
185  curr_instance.mc.type = RPL_DAG_MC_NONE;
186 }
187 /*---------------------------------------------------------------------------*/
188 rpl_of_t rpl_of0 = {
189  reset,
190  nbr_link_metric,
191  nbr_has_usable_link,
192  nbr_is_acceptable_parent,
193  nbr_path_cost,
194  rank_via_nbr,
195  best_parent,
196  update_metric_container,
197  RPL_OCP_OF0
198 };
199 
200 /** @}*/
static uip_ds6_nbr_t * nbr
Pointer to llao option in uip_buf.
Definition: uip-nd6.c:106
API for RPL objective functions (OF)
Definition: rpl.h:201
const struct link_stats * rpl_neighbor_get_link_stats(rpl_nbr_t *nbr)
Returns a neighbor&#39;s link statistics.
Definition: rpl-neighbor.c:259
All information related to a RPL neighbor.
Definition: rpl-types.h:136
Header file for the logging system