00001 /* 00002 * Copyright (c) 2010, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 * \file 00032 * ContikiRPL - an implementation of the routing protocol for low 00033 * power and lossy networks. See: draft-ietf-roll-rpl-17. 00034 * \author 00035 * Joakim Eriksson <joakime@sics.se> & Nicolas Tsiftes <nvt@sics.se> 00036 * 00037 */ 00038 00039 #ifndef RPL_H 00040 #define RPL_H 00041 00042 #include "lib/list.h" 00043 #include "net/uip.h" 00044 #include "net/uip-ds6.h" 00045 #include "sys/ctimer.h" 00046 00047 /* set to 1 for some statistics on trickle / DIO */ 00048 #ifndef RPL_CONF_STATS 00049 #define RPL_CONF_STATS 0 00050 #endif /* RPL_CONF_STATS */ 00051 00052 /* 00053 * Select routing metric supported at runtime. This must be a valid 00054 * DAG Metric Container Object Type (see below). Currently, we only 00055 * support RPL_DAG_MC_ETX and RPL_DAG_MC_ENERGY. 00056 */ 00057 #ifdef RPL_CONF_DAG_MC 00058 #define RPL_DAG_MC RPL_CONF_DAG_MC 00059 #else 00060 #define RPL_DAG_MC RPL_DAG_MC_ETX 00061 #endif /* RPL_CONF_DAG_MC */ 00062 00063 /* 00064 * The objective function used by RPL is configurable through the 00065 * RPL_CONF_OF parameter. This should be defined to be the name of an 00066 * rpl_of_t object linked into the system image, e.g., rpl_of0. 00067 */ 00068 #ifdef RPL_CONF_OF 00069 #define RPL_OF RPL_CONF_OF 00070 #else 00071 /* ETX is the default objective function. */ 00072 #define RPL_OF rpl_of_etx 00073 #endif /* RPL_CONF_OF */ 00074 00075 /* This value decides which DAG instance we should participate in by default. */ 00076 #define RPL_DEFAULT_INSTANCE 0 00077 00078 /* This value is used to access an arbitrary DAG. It will likely be 00079 replaced when we support multiple DAGs more. */ 00080 #define RPL_ANY_INSTANCE -1 00081 /*---------------------------------------------------------------------------*/ 00082 /* The amount of parents that this node has in a particular DAG. */ 00083 #define RPL_PARENT_COUNT(dag) list_length((dag)->parents) 00084 /*---------------------------------------------------------------------------*/ 00085 typedef uint16_t rpl_rank_t; 00086 typedef uint16_t rpl_ocp_t; 00087 00088 /*---------------------------------------------------------------------------*/ 00089 /* DAG Metric Container Object Types, to be confirmed by IANA. */ 00090 #define RPL_DAG_MC_NONE 0 /* Local identifier for empty MC */ 00091 #define RPL_DAG_MC_NSA 1 /* Node State and Attributes */ 00092 #define RPL_DAG_MC_ENERGY 2 /* Node Energy */ 00093 #define RPL_DAG_MC_HOPCOUNT 3 /* Hop Count */ 00094 #define RPL_DAG_MC_THROUGHPUT 4 /* Throughput */ 00095 #define RPL_DAG_MC_LATENCY 5 /* Latency */ 00096 #define RPL_DAG_MC_LQL 6 /* Link Quality Level */ 00097 #define RPL_DAG_MC_ETX 7 /* Expected Transmission Count 00098 */ 00099 #define RPL_DAG_MC_LC 8 /* Link Color */ 00100 00101 /* DAG Metric Container flags. */ 00102 #define RPL_DAG_MC_FLAG_P 0x8 00103 #define RPL_DAG_MC_FLAG_C 0x4 00104 #define RPL_DAG_MC_FLAG_O 0x2 00105 #define RPL_DAG_MC_FLAG_R 0x1 00106 00107 /* DAG Metric Container aggregation mode. */ 00108 #define RPL_DAG_MC_AGGR_ADDITIVE 0 00109 #define RPL_DAG_MC_AGGR_MAXIMUM 1 00110 #define RPL_DAG_MC_AGGR_MINIMUM 2 00111 #define RPL_DAG_MC_AGGR_MULTIPLICATIVE 3 00112 00113 /* The bit index within the flags field of 00114 the rpl_metric_object_energy structure. */ 00115 #define RPL_DAG_MC_ENERGY_INCLUDED 3 00116 #define RPL_DAG_MC_ENERGY_TYPE 1 00117 #define RPL_DAG_MC_ENERGY_ESTIMATION 0 00118 00119 #define RPL_DAG_MC_ENERGY_TYPE_MAINS 0 00120 #define RPL_DAG_MC_ENERGY_TYPE_BATTERY 1 00121 #define RPL_DAG_MC_ENERGY_TYPE_SCAVENGING 2 00122 00123 struct rpl_metric_object_energy { 00124 uint8_t flags; 00125 uint8_t energy_est; 00126 }; 00127 00128 /* Logical representation of a DAG Metric Container. */ 00129 struct rpl_metric_container { 00130 uint8_t type; 00131 uint8_t flags; 00132 uint8_t aggr; 00133 uint8_t prec; 00134 uint8_t length; 00135 union metric_object { 00136 struct rpl_metric_object_energy energy; 00137 uint16_t etx; 00138 } obj; 00139 }; 00140 typedef struct rpl_metric_container rpl_metric_container_t; 00141 /*---------------------------------------------------------------------------*/ 00142 struct rpl_dag; 00143 /*---------------------------------------------------------------------------*/ 00144 struct rpl_parent { 00145 struct rpl_parent *next; 00146 struct rpl_dag *dag; 00147 rpl_metric_container_t mc; 00148 uip_ipaddr_t addr; 00149 rpl_rank_t rank; 00150 uint8_t link_metric; 00151 uint8_t dtsn; 00152 uint8_t updated; 00153 }; 00154 typedef struct rpl_parent rpl_parent_t; 00155 /*---------------------------------------------------------------------------*/ 00156 /* 00157 * API for RPL objective functions (OF) 00158 * 00159 * reset(dag) 00160 * 00161 * Resets the objective function state for a specific DAG. This function is 00162 * called when doing a global repair on the DAG. 00163 * 00164 * parent_state_callback(parent, known, etx) 00165 * 00166 * Receives link-layer neighbor information. The parameter "known" is set 00167 * either to 0 or 1. The "etx" parameter specifies the current 00168 * ETX(estimated transmissions) for the neighbor. 00169 * 00170 * best_parent(parent1, parent2) 00171 * 00172 * Compares two parents and returns the best one, according to the OF. 00173 * 00174 * calculate_rank(parent, base_rank) 00175 * 00176 * Calculates a rank value using the parent rank and a base rank. 00177 * If "parent" is NULL, the objective function selects a default increment 00178 * that is adds to the "base_rank". Otherwise, the OF uses information known 00179 * about "parent" to select an increment to the "base_rank". 00180 * 00181 * update_metric_container(dag) 00182 * 00183 * Updates the metric container for outgoing DIOs in a certain DAG. 00184 * If the objective function of the DAG does not use metric containers, 00185 * the function should set the object type to RPL_DAG_MC_NONE. 00186 */ 00187 struct rpl_of { 00188 void (*reset)(struct rpl_dag *); 00189 void (*parent_state_callback)(rpl_parent_t *, int, int); 00190 rpl_parent_t *(*best_parent)(rpl_parent_t *, rpl_parent_t *); 00191 rpl_rank_t (*calculate_rank)(rpl_parent_t *, rpl_rank_t); 00192 void (*update_metric_container)(struct rpl_dag *); 00193 rpl_ocp_t ocp; 00194 }; 00195 typedef struct rpl_of rpl_of_t; 00196 /*---------------------------------------------------------------------------*/ 00197 /* RPL DIO prefix suboption */ 00198 struct rpl_prefix { 00199 uip_ipaddr_t prefix; 00200 uint32_t lifetime; 00201 uint8_t length; 00202 uint8_t flags; 00203 }; 00204 typedef struct rpl_prefix rpl_prefix_t; 00205 /*---------------------------------------------------------------------------*/ 00206 /* Directed Acyclic Graph */ 00207 struct rpl_dag { 00208 /* DAG configuration */ 00209 rpl_metric_container_t mc; 00210 rpl_of_t *of; 00211 uip_ipaddr_t dag_id; 00212 /* The current default router - used for routing "upwards" */ 00213 uip_ds6_defrt_t *def_route; 00214 rpl_rank_t rank; 00215 rpl_rank_t min_rank; /* should be reset per DODAG iteration! */ 00216 uint8_t dtsn_out; 00217 uint8_t instance_id; 00218 uint8_t version; 00219 uint8_t grounded; 00220 uint8_t mop; 00221 uint8_t preference; 00222 uint8_t dio_intdoubl; 00223 uint8_t dio_intmin; 00224 uint8_t dio_redundancy; 00225 rpl_rank_t max_rankinc; 00226 rpl_rank_t min_hoprankinc; 00227 uint8_t used; 00228 uint8_t default_lifetime; 00229 uint16_t lifetime_unit; /* lifetime in seconds = l_u * d_l */ 00230 /* live data for the DAG */ 00231 uint8_t joined; 00232 uint8_t dio_intcurrent; 00233 uint8_t dio_send; /* for keeping track of which mode the timer is in 00234 */ 00235 uint8_t dio_counter; 00236 #if RPL_CONF_STATS 00237 uint16_t dio_totint; 00238 uint16_t dio_totsend; 00239 uint16_t dio_totrecv; 00240 #endif /* RPL_CONF_STATS */ 00241 uint32_t dio_next_delay; /* delay for completion of dio interval */ 00242 struct ctimer dio_timer; 00243 struct ctimer dao_timer; 00244 rpl_parent_t *preferred_parent; 00245 void *parent_list; 00246 list_t parents; 00247 rpl_prefix_t prefix_info; 00248 }; 00249 typedef struct rpl_dag rpl_dag_t; 00250 /*---------------------------------------------------------------------------*/ 00251 /* Public RPL functions. */ 00252 void rpl_init(void); 00253 rpl_dag_t *rpl_set_root(uip_ipaddr_t *); 00254 int rpl_set_prefix(rpl_dag_t *dag, uip_ipaddr_t *prefix, int len); 00255 int rpl_repair_dag(rpl_dag_t *dag); 00256 int rpl_set_default_route(rpl_dag_t *dag, uip_ipaddr_t *from); 00257 rpl_dag_t *rpl_get_dag(int instance_id); 00258 /*---------------------------------------------------------------------------*/ 00259 #endif /* RPL_H */