Contiki-NG
Loading...
Searching...
No Matches
rpl-timers.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 * RPL timer management.
33 *
34 * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
35 */
36
37/**
38 * \addtogroup uip
39 * @{
40 */
41
42#include "contiki.h"
43#include "net/routing/rpl-classic/rpl-private.h"
44#include "net/link-stats.h"
46#include "net/ipv6/uip-sr.h"
47#include "lib/random.h"
48#include "sys/ctimer.h"
49#include "sys/log.h"
50
51#define LOG_MODULE "RPL"
52#define LOG_LEVEL LOG_LEVEL_RPL
53
54/* A configurable function called after update of the RPL DIO interval. */
55#ifdef RPL_CALLBACK_NEW_DIO_INTERVAL
56void RPL_CALLBACK_NEW_DIO_INTERVAL(clock_time_t dio_interval);
57#endif /* RPL_CALLBACK_NEW_DIO_INTERVAL */
58
59#ifdef RPL_PROBING_SELECT_FUNC
60rpl_parent_t *RPL_PROBING_SELECT_FUNC(rpl_dag_t *dag);
61#endif /* RPL_PROBING_SELECT_FUNC */
62
63#ifdef RPL_PROBING_DELAY_FUNC
64clock_time_t RPL_PROBING_DELAY_FUNC(rpl_dag_t *dag);
65#endif /* RPL_PROBING_DELAY_FUNC */
66
67/*---------------------------------------------------------------------------*/
68static struct ctimer periodic_timer;
69
70static void handle_periodic_timer(void *ptr);
71static void new_dio_interval(rpl_instance_t *instance);
72static void handle_dio_timer(void *ptr);
73
74static uint16_t next_dis;
75
76/* dio_send_ok is true if the node is ready to send DIOs. */
77static uint8_t dio_send_ok;
78
79/*---------------------------------------------------------------------------*/
80static void
81handle_periodic_timer(void *ptr)
82{
83 rpl_dag_t *dag = rpl_get_any_dag();
84
85 rpl_purge_dags();
86 if(dag != NULL) {
87 if(RPL_IS_STORING(dag->instance)) {
88 rpl_purge_routes();
89 }
90 if(RPL_IS_NON_STORING(dag->instance)) {
92 }
93 }
94 rpl_recalculate_ranks();
95
96 /* Handle DIS. */
97#if RPL_DIS_SEND
98 next_dis++;
99 if((dag == NULL || dag->instance->current_dag->rank == RPL_INFINITE_RANK)
100 && next_dis >= RPL_DIS_INTERVAL) {
101 next_dis = 0;
102 dis_output(NULL);
103 }
104#endif
105 ctimer_reset(&periodic_timer);
106}
107/*---------------------------------------------------------------------------*/
108static void
109new_dio_interval(rpl_instance_t *instance)
110{
111 uint32_t time;
112 clock_time_t ticks;
113
114 /* TODO: too small timer intervals for many cases. */
115 time = 1UL << instance->dio_intcurrent;
116
117 /* Convert from milliseconds to CLOCK_TICKS. */
118 ticks = (time * CLOCK_SECOND) / 1000;
119 instance->dio_next_delay = ticks;
120
121 /* Random number between I/2 and I. */
122 ticks = ticks / 2 + (ticks / 2 * (uint32_t)random_rand()) / RANDOM_RAND_MAX;
123
124 /*
125 * The intervals must be equally long among the nodes for Trickle to
126 * operate efficiently. Therefore we need to calculate the delay between
127 * the randomized time and the start time of the next interval.
128 */
129 instance->dio_next_delay -= ticks;
130 instance->dio_send = 1;
131
132#if RPL_CONF_STATS
133 /* Keep some stats. */
134 instance->dio_totint++;
135 instance->dio_totrecv += instance->dio_counter;
136 LOG_ANNOTATE("#A rank=%u.%u(%u),stats=%d %d %d %d,color=%s\n",
137 DAG_RANK(instance->current_dag->rank, instance),
138 (10 * (instance->current_dag->rank % instance->min_hoprankinc)) / instance->min_hoprankinc,
139 instance->current_dag->version,
140 instance->dio_totint, instance->dio_totsend,
141 instance->dio_totrecv, instance->dio_intcurrent,
142 instance->current_dag->rank == ROOT_RANK(instance) ? "BLUE" : "ORANGE");
143#endif /* RPL_CONF_STATS */
144
145 /* Reset the redundancy counter. */
146 instance->dio_counter = 0;
147
148 /* Schedule the timer. */
149 LOG_INFO("Scheduling DIO timer %lu ticks in future (Interval)\n",
150 (unsigned long)ticks);
151 ctimer_set(&instance->dio_timer, ticks, &handle_dio_timer, instance);
152
153#ifdef RPL_CALLBACK_NEW_DIO_INTERVAL
154 RPL_CALLBACK_NEW_DIO_INTERVAL((CLOCK_SECOND * 1UL << instance->dio_intcurrent) / 1000);
155#endif /* RPL_CALLBACK_NEW_DIO_INTERVAL */
156}
157/*---------------------------------------------------------------------------*/
158static void
159handle_dio_timer(void *ptr)
160{
161 rpl_instance_t *instance = (rpl_instance_t *)ptr;
162
163 LOG_DBG("DIO Timer triggered\n");
164 if(!dio_send_ok) {
165 if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) {
166 dio_send_ok = 1;
167 } else {
168 LOG_WARN("Postponing DIO transmission since link local address is not ok\n");
169 ctimer_set(&instance->dio_timer, CLOCK_SECOND, &handle_dio_timer, instance);
170 return;
171 }
172 }
173
174 if(instance->dio_send) {
175 /* Send DIO if counter is less than desired redundancy. */
176 if(instance->dio_redundancy == 0 || instance->dio_counter < instance->dio_redundancy) {
177#if RPL_CONF_STATS
178 instance->dio_totsend++;
179#endif /* RPL_CONF_STATS */
180 dio_output(instance, NULL);
181 } else {
182 LOG_DBG("Suppressing DIO transmission (%d >= %d)\n",
183 instance->dio_counter, instance->dio_redundancy);
184 }
185 instance->dio_send = 0;
186 LOG_DBG("Scheduling DIO timer %lu ticks in future (sent)\n",
187 (unsigned long)instance->dio_next_delay);
188 ctimer_set(&instance->dio_timer, instance->dio_next_delay,
189 handle_dio_timer, instance);
190 } else {
191 /* Check if we need to double interval. */
192 if(instance->dio_intcurrent <
193 instance->dio_intmin + instance->dio_intdoubl) {
194 instance->dio_intcurrent++;
195 LOG_DBG("DIO Timer interval doubled %d\n", instance->dio_intcurrent);
196 }
197 new_dio_interval(instance);
198 }
199
200 if(LOG_DBG_ENABLED) {
201 rpl_print_neighbor_list();
202 }
203}
204/*---------------------------------------------------------------------------*/
205void
206rpl_reset_periodic_timer(void)
207{
208 next_dis = RPL_DIS_INTERVAL / 2 +
209 ((uint32_t)RPL_DIS_INTERVAL * (uint32_t)random_rand()) / RANDOM_RAND_MAX -
210 RPL_DIS_START_DELAY;
211 ctimer_set(&periodic_timer, CLOCK_SECOND, handle_periodic_timer, NULL);
212}
213/*---------------------------------------------------------------------------*/
214/* Resets the DIO timer in the instance to its minimal interval. */
215void
216rpl_reset_dio_timer(rpl_instance_t *instance)
217{
218#if !RPL_LEAF_ONLY
219 /* Do not reset if we are already on the minimum interval,
220 unless forced to do so. */
221 if(instance->dio_intcurrent > instance->dio_intmin) {
222 instance->dio_counter = 0;
223 instance->dio_intcurrent = instance->dio_intmin;
224 new_dio_interval(instance);
225 }
226#if RPL_CONF_STATS
227 rpl_stats.resets++;
228#endif /* RPL_CONF_STATS */
229#endif /* RPL_LEAF_ONLY */
230}
231/*---------------------------------------------------------------------------*/
232static void handle_dao_timer(void *ptr);
233static void
234set_dao_lifetime_timer(rpl_instance_t *instance)
235{
236 if(rpl_get_mode() == RPL_MODE_FEATHER) {
237 return;
238 }
239
240 /* Set up another DAO within half the expiration time, if such a
241 time has been configured */
242 if(instance->default_lifetime != RPL_INFINITE_LIFETIME) {
243 clock_time_t expiration_time;
244
245 /*
246 * If the lifetime is 0, we simply set the expiration time to
247 * half a second to get an interval that corresponds to a
248 * lifetime of 1 and a lifetime unit of 1.
249 */
250 if(instance->default_lifetime == 0 || instance->lifetime_unit == 0) {
251 expiration_time = CLOCK_SECOND / 2;
252 } else {
253 expiration_time = (clock_time_t)instance->default_lifetime *
254 (clock_time_t)instance->lifetime_unit * CLOCK_SECOND / 2;
255 }
256
257 /* Make the time for the re-registration to be between 1/2 and 3/4 of
258 the lifetime. */
259 expiration_time = expiration_time + (random_rand() % (expiration_time / 2));
260 LOG_DBG("Scheduling DAO lifetime timer %u ticks in the future\n",
261 (unsigned)expiration_time);
262 ctimer_set(&instance->dao_lifetime_timer, expiration_time,
263 handle_dao_timer, instance);
264 }
265}
266/*---------------------------------------------------------------------------*/
267static void
268handle_dao_timer(void *ptr)
269{
270 rpl_instance_t *instance = (rpl_instance_t *)ptr;
271
272#if RPL_WITH_MULTICAST
273 uip_mcast6_route_t *mcast_route;
274 uint8_t i;
275#endif
276
277 if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) {
278 LOG_INFO("Postpone DAO transmission\n");
279 ctimer_set(&instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance);
280 return;
281 }
282
283 /* Send the DAO to the DAO parent set -- the preferred parent in our case. */
284 if(instance->current_dag->preferred_parent != NULL) {
285 LOG_INFO("handle_dao_timer - sending DAO\n");
286 /* Set the route lifetime to the default value. */
287 dao_output(instance->current_dag->preferred_parent,
288 instance->default_lifetime);
289
290#if RPL_WITH_MULTICAST
291 /* Send DAOs for multicast prefixes only if the instance is in MOP 3. */
292 if(instance->mop == RPL_MOP_STORING_MULTICAST) {
293 /* Send a DAO for own multicast addresses. */
294 for(i = 0; i < UIP_DS6_MADDR_NB; i++) {
295 if(uip_ds6_if.maddr_list[i].isused
296 && uip_is_addr_mcast_global(&uip_ds6_if.maddr_list[i].ipaddr)) {
297 dao_output_target(instance->current_dag->preferred_parent,
298 &uip_ds6_if.maddr_list[i].ipaddr, instance->default_lifetime);
299 }
300 }
301
302 /* Iterate over multicast routes and send DAOs */
303 mcast_route = uip_mcast6_route_list_head();
304 while(mcast_route != NULL) {
305 /* Don't send if it's also our own address, done that already. */
306 if(uip_ds6_maddr_lookup(&mcast_route->group) == NULL) {
307 dao_output_target(instance->current_dag->preferred_parent,
308 &mcast_route->group, instance->default_lifetime);
309 }
310 mcast_route = list_item_next(mcast_route);
311 }
312 }
313#endif
314 } else {
315 LOG_INFO("No suitable DAO parent\n");
316 }
317
318 ctimer_stop(&instance->dao_timer);
319
320 if(etimer_expired(&instance->dao_lifetime_timer.etimer)) {
321 set_dao_lifetime_timer(instance);
322 }
323}
324/*---------------------------------------------------------------------------*/
325static void
326schedule_dao(rpl_instance_t *instance, clock_time_t latency)
327{
328 clock_time_t expiration_time;
329
330 if(rpl_get_mode() == RPL_MODE_FEATHER) {
331 return;
332 }
333
334 if(!etimer_expired(&instance->dao_timer.etimer)) {
335 LOG_DBG("DAO timer already scheduled\n");
336 } else {
337 if(latency != 0) {
338 expiration_time = latency / 2 +
339 (random_rand() % (latency));
340 } else {
341 expiration_time = 0;
342 }
343 LOG_DBG("Scheduling DAO timer %u ticks in the future\n",
344 (unsigned)expiration_time);
345 ctimer_set(&instance->dao_timer, expiration_time,
346 handle_dao_timer, instance);
347
348 set_dao_lifetime_timer(instance);
349 }
350}
351/*---------------------------------------------------------------------------*/
352void
353rpl_schedule_dao(rpl_instance_t *instance)
354{
355 schedule_dao(instance, RPL_DAO_DELAY);
356}
357/*---------------------------------------------------------------------------*/
358void
359rpl_schedule_dao_immediately(rpl_instance_t *instance)
360{
361 schedule_dao(instance, 0);
362}
363/*---------------------------------------------------------------------------*/
364void
365rpl_cancel_dao(rpl_instance_t *instance)
366{
367 ctimer_stop(&instance->dao_timer);
368 ctimer_stop(&instance->dao_lifetime_timer);
369}
370/*---------------------------------------------------------------------------*/
371static void
372handle_unicast_dio_timer(void *ptr)
373{
374 rpl_instance_t *instance = (rpl_instance_t *)ptr;
375 uip_ipaddr_t *target_ipaddr = rpl_parent_get_ipaddr(instance->unicast_dio_target);
376
377 if(target_ipaddr != NULL) {
378 dio_output(instance, target_ipaddr);
379 }
380}
381/*---------------------------------------------------------------------------*/
382void
383rpl_schedule_unicast_dio_immediately(rpl_instance_t *instance)
384{
385 ctimer_set(&instance->unicast_dio_timer, 0,
386 handle_unicast_dio_timer, instance);
387}
388/*---------------------------------------------------------------------------*/
389#if RPL_WITH_PROBING
390clock_time_t
391get_probing_delay(rpl_dag_t *dag)
392{
393 return ((RPL_PROBING_INTERVAL) / 2) + random_rand() % (RPL_PROBING_INTERVAL);
394}
395/*---------------------------------------------------------------------------*/
396rpl_parent_t *
397get_probing_target(rpl_dag_t *dag)
398{
399 /*
400 * Returns the next probing target. The current implementation
401 * probes the urgent probing target if any, or the preferred parent
402 * if its link statistics need refresh.
403 *
404 * Otherwise, it picks at random between:
405 * (1) selecting the best parent with non-fresh link statistics.
406 * (2) selecting the least recently updated parent.
407 */
408
409 rpl_parent_t *p;
410 rpl_parent_t *probing_target = NULL;
411 rpl_rank_t probing_target_rank = RPL_INFINITE_RANK;
412 clock_time_t probing_target_age = 0;
413 clock_time_t clock_now = clock_time();
414
415 if(dag == NULL || dag->instance == NULL) {
416 return NULL;
417 }
418
419 /* There is an urgent probing target. */
420 if(dag->instance->urgent_probing_target != NULL) {
421 return dag->instance->urgent_probing_target;
422 }
423
424 /* The preferred parent needs probing. */
425 if(dag->preferred_parent != NULL
426 && !rpl_parent_is_fresh(dag->preferred_parent)) {
427 return dag->preferred_parent;
428 }
429
430 /* With 50% probability: probe best non-fresh parent. */
431 if(random_rand() % 2 == 0) {
432 p = nbr_table_head(rpl_parents);
433 while(p != NULL) {
434 if(p->dag == dag && !rpl_parent_is_fresh(p)) {
435 /* p is in our DAG and needs probing. */
436 rpl_rank_t p_rank = rpl_rank_via_parent(p);
437 if(probing_target == NULL || p_rank < probing_target_rank) {
438 probing_target = p;
439 probing_target_rank = p_rank;
440 }
441 }
442 p = nbr_table_next(rpl_parents, p);
443 }
444 }
445
446 /* If we still do not have a probing target:
447 pick the least recently updated parent. */
448 if(probing_target == NULL) {
449 p = nbr_table_head(rpl_parents);
450 while(p != NULL) {
451 const struct link_stats *stats = rpl_get_parent_link_stats(p);
452 if(p->dag == dag && stats != NULL) {
453 if(probing_target == NULL
454 || clock_now - stats->last_tx_time > probing_target_age) {
455 probing_target = p;
456 probing_target_age = clock_now - stats->last_tx_time;
457 }
458 }
459 p = nbr_table_next(rpl_parents, p);
460 }
461 }
462
463 return probing_target;
464}
465/*---------------------------------------------------------------------------*/
466static rpl_dag_t *
467get_next_dag(rpl_instance_t *instance)
468{
469 rpl_dag_t *dag = NULL;
470 int new_dag = instance->last_dag;
471
472 do {
473 new_dag++;
474 if(new_dag >= RPL_MAX_DAG_PER_INSTANCE) {
475 new_dag = 0;
476 }
477 if(instance->dag_table[new_dag].used) {
478 dag = &instance->dag_table[new_dag];
479 }
480 } while(new_dag != instance->last_dag && dag == NULL);
481 instance->last_dag = new_dag;
482 return dag;
483}
484/*---------------------------------------------------------------------------*/
485static void
486handle_probing_timer(void *ptr)
487{
488 rpl_instance_t *instance = (rpl_instance_t *)ptr;
489 rpl_parent_t *probing_target = RPL_PROBING_SELECT_FUNC(get_next_dag(instance));
490 uip_ipaddr_t *target_ipaddr = rpl_parent_get_ipaddr(probing_target);
491
492 /* Perform probing. */
493 if(target_ipaddr != NULL) {
494 const struct link_stats *stats = rpl_get_parent_link_stats(probing_target);
495 const linkaddr_t *lladdr = rpl_get_parent_lladdr(probing_target);
496 LOG_INFO("probing %u %s last tx %u min ago\n",
497 lladdr != NULL ? lladdr->u8[7] : 0x0,
498 instance->urgent_probing_target != NULL ? "(urgent)" : "",
499 probing_target != NULL && stats != NULL ?
500 (unsigned)((clock_time() - stats->last_tx_time) / (60 * CLOCK_SECOND)) : 0
501 );
502
503 /* Send probe, e.g., a unicast DIO or DIS. */
504 RPL_PROBING_SEND_FUNC(instance, target_ipaddr);
505 }
506
507 /* Schedule next probing. */
508 rpl_schedule_probing(instance);
509
510 if(LOG_DBG_ENABLED) {
511 rpl_print_neighbor_list();
512 }
513}
514/*---------------------------------------------------------------------------*/
515void
517{
518 ctimer_set(&instance->probing_timer,
519 RPL_PROBING_DELAY_FUNC(instance->current_dag),
520 handle_probing_timer, instance);
521}
522/*---------------------------------------------------------------------------*/
523void
525{
526 ctimer_set(&instance->probing_timer, random_rand() % (CLOCK_SECOND * 4),
527 handle_probing_timer, instance);
528}
529#endif /* RPL_WITH_PROBING */
530
531/** @}*/
Header file for the callback timer.
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition random.c:58
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:103
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition ctimer.c:138
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition ctimer.c:114
static void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition ctimer.h:137
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
static void * list_item_next(const void *item)
Get the next item following this item.
Definition list.h:294
void rpl_schedule_probing(void)
Schedule probing with delay RPL_PROBING_DELAY_FUNC()
void rpl_schedule_probing_now(void)
Schedule probing within a few seconds.
uip_mcast6_route_t * uip_mcast6_route_list_head(void)
Retrieve a pointer to the start of the multicast routes list.
enum rpl_mode rpl_get_mode(void)
Get the RPL mode.
Definition rpl.c:68
void uip_sr_periodic(unsigned seconds)
A function called periodically.
Definition uip-sr.c:211
#define uip_is_addr_mcast_global(a)
is address a global multicast address (FFxE::/16), a is of type uip_ip6addr_t*
Definition uip.h:1867
uip_ds6_netif_t uip_ds6_if
The single interface.
Definition uip-ds6.c:75
Header file for the logging system.
RPL DAG structure.
Definition rpl.h:138
RPL instance structure.
Definition rpl.h:222
An entry in the multicast routing table.
uip_ipaddr_t group
The multicast group.
This header file contains configuration directives for uIPv6 multicast support.
Source routing support.