Contiki 2.5
rpl.h
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  * \file
32  * ContikiRPL - an implementation of the routing protocol for low
33  * power and lossy networks. See: draft-ietf-roll-rpl-17.
34  * \author
35  * Joakim Eriksson <joakime@sics.se> & Nicolas Tsiftes <nvt@sics.se>
36  *
37  */
38 
39 #ifndef RPL_H
40 #define RPL_H
41 
42 #include "lib/list.h"
43 #include "net/uip.h"
44 #include "net/uip-ds6.h"
45 #include "sys/ctimer.h"
46 
47 /* set to 1 for some statistics on trickle / DIO */
48 #ifndef RPL_CONF_STATS
49 #define RPL_CONF_STATS 0
50 #endif /* RPL_CONF_STATS */
51 
52 /*
53  * Select routing metric supported at runtime. This must be a valid
54  * DAG Metric Container Object Type (see below). Currently, we only
55  * support RPL_DAG_MC_ETX and RPL_DAG_MC_ENERGY.
56  */
57 #ifdef RPL_CONF_DAG_MC
58 #define RPL_DAG_MC RPL_CONF_DAG_MC
59 #else
60 #define RPL_DAG_MC RPL_DAG_MC_ETX
61 #endif /* RPL_CONF_DAG_MC */
62 
63 /*
64  * The objective function used by RPL is configurable through the
65  * RPL_CONF_OF parameter. This should be defined to be the name of an
66  * rpl_of_t object linked into the system image, e.g., rpl_of0.
67  */
68 #ifdef RPL_CONF_OF
69 #define RPL_OF RPL_CONF_OF
70 #else
71 /* ETX is the default objective function. */
72 #define RPL_OF rpl_of_etx
73 #endif /* RPL_CONF_OF */
74 
75 /* This value decides which DAG instance we should participate in by default. */
76 #define RPL_DEFAULT_INSTANCE 0
77 
78 /* This value is used to access an arbitrary DAG. It will likely be
79  replaced when we support multiple DAGs more. */
80 #define RPL_ANY_INSTANCE -1
81 /*---------------------------------------------------------------------------*/
82 /* The amount of parents that this node has in a particular DAG. */
83 #define RPL_PARENT_COUNT(dag) list_length((dag)->parents)
84 /*---------------------------------------------------------------------------*/
85 typedef uint16_t rpl_rank_t;
86 typedef uint8_t rpl_lifetime_t;
87 typedef uint16_t rpl_ocp_t;
88 
89 /*---------------------------------------------------------------------------*/
90 /* DAG Metric Container Object Types, to be confirmed by IANA. */
91 #define RPL_DAG_MC_NONE 0 /* Local identifier for empty MC */
92 #define RPL_DAG_MC_NSA 1 /* Node State and Attributes */
93 #define RPL_DAG_MC_ENERGY 2 /* Node Energy */
94 #define RPL_DAG_MC_HOPCOUNT 3 /* Hop Count */
95 #define RPL_DAG_MC_THROUGHPUT 4 /* Throughput */
96 #define RPL_DAG_MC_LATENCY 5 /* Latency */
97 #define RPL_DAG_MC_LQL 6 /* Link Quality Level */
98 #define RPL_DAG_MC_ETX 7 /* Expected Transmission Count */
99 #define RPL_DAG_MC_LC 8 /* Link Color */
100 
101 /* DAG Metric Container flags. */
102 #define RPL_DAG_MC_FLAG_P 0x8
103 #define RPL_DAG_MC_FLAG_C 0x4
104 #define RPL_DAG_MC_FLAG_O 0x2
105 #define RPL_DAG_MC_FLAG_R 0x1
106 
107 /* DAG Metric Container aggregation mode. */
108 #define RPL_DAG_MC_AGGR_ADDITIVE 0
109 #define RPL_DAG_MC_AGGR_MAXIMUM 1
110 #define RPL_DAG_MC_AGGR_MINIMUM 2
111 #define RPL_DAG_MC_AGGR_MULTIPLICATIVE 3
112 
113 /* The bit index within the flags field of
114  the rpl_metric_object_energy structure. */
115 #define RPL_DAG_MC_ENERGY_INCLUDED 3
116 #define RPL_DAG_MC_ENERGY_TYPE 1
117 #define RPL_DAG_MC_ENERGY_ESTIMATION 0
118 
119 #define RPL_DAG_MC_ENERGY_TYPE_MAINS 0
120 #define RPL_DAG_MC_ENERGY_TYPE_BATTERY 1
121 #define RPL_DAG_MC_ENERGY_TYPE_SCAVENGING 2
122 
123 struct rpl_metric_object_energy {
124  uint8_t flags;
125  uint8_t energy_est;
126 };
127 
128 /* Logical representation of a DAG Metric Container. */
129 struct rpl_metric_container {
130  uint8_t type;
131  uint8_t flags;
132  uint8_t aggr;
133  uint8_t prec;
134  uint8_t length;
135  union metric_object {
136  struct rpl_metric_object_energy energy;
137  uint16_t etx;
138  } obj;
139 };
140 typedef struct rpl_metric_container rpl_metric_container_t;
141 /*---------------------------------------------------------------------------*/
142 struct rpl_dag;
143 /*---------------------------------------------------------------------------*/
144 struct rpl_parent {
145  struct rpl_parent *next;
146  struct rpl_dag *dag;
147  rpl_metric_container_t mc;
148  uip_ipaddr_t addr;
149  rpl_rank_t rank;
150  uint8_t link_metric;
151  uint8_t dtsn;
152  uint8_t updated;
153 };
154 typedef struct rpl_parent rpl_parent_t;
155 /*---------------------------------------------------------------------------*/
156 /*
157  * API for RPL objective functions (OF)
158  *
159  * reset(dag)
160  *
161  * Resets the objective function state for a specific DAG. This function is
162  * called when doing a global repair on the DAG.
163  *
164  * parent_state_callback(parent, known, etx)
165  *
166  * Receives link-layer neighbor information. The parameter "known" is set
167  * either to 0 or 1. The "etx" parameter specifies the current
168  * ETX(estimated transmissions) for the neighbor.
169  *
170  * best_parent(parent1, parent2)
171  *
172  * Compares two parents and returns the best one, according to the OF.
173  *
174  * calculate_rank(parent, base_rank)
175  *
176  * Calculates a rank value using the parent rank and a base rank.
177  * If "parent" is NULL, the objective function selects a default increment
178  * that is adds to the "base_rank". Otherwise, the OF uses information known
179  * about "parent" to select an increment to the "base_rank".
180  *
181  * update_metric_container(dag)
182  *
183  * Updates the metric container for outgoing DIOs in a certain DAG.
184  * If the objective function of the DAG does not use metric containers,
185  * the function should set the object type to RPL_DAG_MC_NONE.
186  */
187 struct rpl_of {
188  void (*reset)(struct rpl_dag *);
189  void (*parent_state_callback)(rpl_parent_t *, int, int);
190  rpl_parent_t *(*best_parent)(rpl_parent_t *, rpl_parent_t *);
191  rpl_rank_t (*calculate_rank)(rpl_parent_t *, rpl_rank_t);
192  void (*update_metric_container)(struct rpl_dag *);
193  rpl_ocp_t ocp;
194 };
195 typedef struct rpl_of rpl_of_t;
196 /*---------------------------------------------------------------------------*/
197 /* RPL DIO prefix suboption */
198 struct rpl_prefix {
199  uip_ipaddr_t prefix;
200  uint32_t lifetime;
201  uint8_t length;
202  uint8_t flags;
203 };
204 typedef struct rpl_prefix rpl_prefix_t;
205 /*---------------------------------------------------------------------------*/
206 /* Directed Acyclic Graph */
207 struct rpl_dag {
208  /* DAG configuration */
209  rpl_metric_container_t mc;
210  rpl_of_t *of;
211  uip_ipaddr_t dag_id;
212  /* The current default router - used for routing "upwards" */
213  uip_ds6_defrt_t *def_route;
214  rpl_rank_t rank;
215  rpl_rank_t min_rank; /* should be reset per DODAG iteration! */
216  uint8_t dtsn_out;
217  uint8_t instance_id;
218  uint8_t version;
219  uint8_t grounded;
220  uint8_t mop;
221  uint8_t preference;
222  uint8_t dio_intdoubl;
223  uint8_t dio_intmin;
224  uint8_t dio_redundancy;
225  rpl_rank_t max_rankinc;
226  rpl_rank_t min_hoprankinc;
227  uint8_t used;
228  uint8_t default_lifetime;
229  uint16_t lifetime_unit; /* lifetime in seconds = l_u * d_l */
230  /* live data for the DAG */
231  uint8_t joined;
232  uint8_t dio_intcurrent;
233  uint8_t dio_send; /* for keeping track of which mode the timer is in
234 */
235  uint8_t dio_counter;
236 #if RPL_CONF_STATS
237  uint16_t dio_totint;
238  uint16_t dio_totsend;
239  uint16_t dio_totrecv;
240 #endif /* RPL_CONF_STATS */
241  uint32_t dio_next_delay; /* delay for completion of dio interval */
242  struct ctimer dio_timer;
243  struct ctimer dao_timer;
244  rpl_parent_t *preferred_parent;
245  void *parent_list;
246  list_t parents;
247  rpl_prefix_t prefix_info;
248 };
249 typedef struct rpl_dag rpl_dag_t;
250 /*---------------------------------------------------------------------------*/
251 /* Public RPL functions. */
252 void rpl_init(void);
253 rpl_dag_t *rpl_set_root(uip_ipaddr_t *);
254 int rpl_set_prefix(rpl_dag_t *dag, uip_ipaddr_t *prefix, int len);
255 int rpl_repair_dag(rpl_dag_t *dag);
256 int rpl_set_default_route(rpl_dag_t *dag, uip_ipaddr_t *from);
257 rpl_dag_t *rpl_get_dag(int instance_id);
258 /*---------------------------------------------------------------------------*/
259 #endif /* RPL_H */