2007-03-31 20:31:27 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup rimeroute
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-03-15 20:52:51 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-03-31 20:31:27 +02:00
|
|
|
* Rime route table
|
2007-03-15 20:52:51 +01:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2007-03-24 14:57:04 +01:00
|
|
|
#include <stdio.h>
|
2007-03-15 20:52:51 +01:00
|
|
|
|
2007-12-17 10:14:08 +01:00
|
|
|
#include "lib/list.h"
|
|
|
|
#include "lib/memb.h"
|
2010-06-14 09:34:36 +02:00
|
|
|
#include "sys/ctimer.h"
|
2007-03-24 14:42:25 +01:00
|
|
|
#include "net/rime/route.h"
|
2007-12-17 10:20:16 +01:00
|
|
|
#include "contiki-conf.h"
|
2007-03-15 20:52:51 +01:00
|
|
|
|
2007-12-17 10:20:16 +01:00
|
|
|
#ifdef ROUTE_CONF_ENTRIES
|
|
|
|
#define NUM_RT_ENTRIES ROUTE_CONF_ENTRIES
|
|
|
|
#else /* ROUTE_CONF_ENTRIES */
|
2007-03-15 20:52:51 +01:00
|
|
|
#define NUM_RT_ENTRIES 8
|
2007-12-17 10:20:16 +01:00
|
|
|
#endif /* ROUTE_CONF_ENTRIES */
|
2007-03-15 20:52:51 +01:00
|
|
|
|
2009-05-10 23:08:01 +02:00
|
|
|
#ifdef ROUTE_CONF_DECAY_THRESHOLD
|
|
|
|
#define DECAY_THRESHOLD ROUTE_CONF_DECAY_THRESHOLD
|
|
|
|
#else /* ROUTE_CONF_DECAY_THRESHOLD */
|
|
|
|
#define DECAY_THRESHOLD 8
|
|
|
|
#endif /* ROUTE_CONF_DECAY_THRESHOLD */
|
|
|
|
|
|
|
|
#ifdef ROUTE_CONF_DEFAULT_LIFETIME
|
|
|
|
#define DEFAULT_LIFETIME ROUTE_CONF_DEFAULT_LIFETIME
|
|
|
|
#else /* ROUTE_CONF_DEFAULT_LIFETIME */
|
|
|
|
#define DEFAULT_LIFETIME 60
|
|
|
|
#endif /* ROUTE_CONF_DEFAULT_LIFETIME */
|
|
|
|
|
2007-03-15 20:52:51 +01:00
|
|
|
/*
|
2009-05-10 23:08:01 +02:00
|
|
|
* List of route entries.
|
2007-03-15 20:52:51 +01:00
|
|
|
*/
|
|
|
|
LIST(route_table);
|
|
|
|
MEMB(route_mem, struct route_entry, NUM_RT_ENTRIES);
|
|
|
|
|
2007-03-22 18:34:43 +01:00
|
|
|
static struct ctimer t;
|
|
|
|
|
2009-05-10 23:08:01 +02:00
|
|
|
static int max_time = DEFAULT_LIFETIME;
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 18:34:43 +01:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
periodic(void *ptr)
|
|
|
|
{
|
|
|
|
struct route_entry *e;
|
|
|
|
|
2010-06-15 21:22:25 +02:00
|
|
|
for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
|
2007-03-22 18:34:43 +01:00
|
|
|
e->time++;
|
2007-03-25 14:03:59 +02:00
|
|
|
if(e->time >= max_time) {
|
2009-05-10 23:08:01 +02:00
|
|
|
PRINTF("route periodic: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
2007-03-22 18:34:43 +01:00
|
|
|
list_remove(route_table, e);
|
|
|
|
memb_free(&route_mem, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-25 14:03:59 +02:00
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
2007-03-22 18:34:43 +01:00
|
|
|
}
|
2007-03-15 20:52:51 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
route_init(void)
|
|
|
|
{
|
|
|
|
list_init(route_table);
|
|
|
|
memb_init(&route_mem);
|
2007-03-22 18:34:43 +01:00
|
|
|
|
2007-03-25 14:03:59 +02:00
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
2007-03-15 20:52:51 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-23 17:22:02 +01:00
|
|
|
route_add(const rimeaddr_t *dest, const rimeaddr_t *nexthop,
|
2009-05-10 23:08:01 +02:00
|
|
|
uint8_t cost, uint8_t seqno)
|
2007-03-15 20:52:51 +01:00
|
|
|
{
|
|
|
|
struct route_entry *e;
|
|
|
|
|
|
|
|
/* Avoid inserting duplicate entries. */
|
|
|
|
e = route_lookup(dest);
|
2009-05-10 23:08:01 +02:00
|
|
|
if(e != NULL && rimeaddr_cmp(&e->nexthop, nexthop)) {
|
|
|
|
list_remove(route_table, e);
|
2007-03-15 20:52:51 +01:00
|
|
|
} else {
|
2009-05-10 23:08:01 +02:00
|
|
|
/* Allocate a new entry or reuse the oldest entry with highest cost. */
|
2007-03-15 20:52:51 +01:00
|
|
|
e = memb_alloc(&route_mem);
|
|
|
|
if(e == NULL) {
|
2009-05-10 23:08:01 +02:00
|
|
|
/* Remove oldest entry. XXX */
|
|
|
|
e = list_chop(route_table);
|
|
|
|
PRINTF("route_add: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
2007-03-15 20:52:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rimeaddr_copy(&e->dest, dest);
|
|
|
|
rimeaddr_copy(&e->nexthop, nexthop);
|
2009-05-10 23:08:01 +02:00
|
|
|
e->cost = cost;
|
2007-03-15 20:52:51 +01:00
|
|
|
e->seqno = seqno;
|
2007-03-22 18:34:43 +01:00
|
|
|
e->time = 0;
|
2009-05-10 23:08:01 +02:00
|
|
|
e->decay = 0;
|
2007-03-15 20:52:51 +01:00
|
|
|
|
|
|
|
/* New entry goes first. */
|
|
|
|
list_push(route_table, e);
|
|
|
|
|
2009-05-10 23:08:01 +02:00
|
|
|
PRINTF("route_add: new entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
|
|
|
|
2007-03-15 20:52:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct route_entry *
|
2009-03-23 17:22:02 +01:00
|
|
|
route_lookup(const rimeaddr_t *dest)
|
2007-03-15 20:52:51 +01:00
|
|
|
{
|
|
|
|
struct route_entry *e;
|
2009-05-10 23:08:01 +02:00
|
|
|
uint8_t lowest_cost;
|
|
|
|
struct route_entry *best_entry;
|
2007-03-15 20:52:51 +01:00
|
|
|
|
2009-05-10 23:08:01 +02:00
|
|
|
lowest_cost = -1;
|
|
|
|
best_entry = NULL;
|
|
|
|
|
|
|
|
/* Find the route with the lowest cost. */
|
2010-06-15 21:22:25 +02:00
|
|
|
for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
|
2007-03-15 20:52:51 +01:00
|
|
|
/* printf("route_lookup: comparing %d.%d.%d.%d with %d.%d.%d.%d\n",
|
2008-01-05 00:28:33 +01:00
|
|
|
uip_ipaddr_to_quad(dest), uip_ipaddr_to_quad(&e->dest));*/
|
2007-11-28 21:00:57 +01:00
|
|
|
|
2007-03-15 20:52:51 +01:00
|
|
|
if(rimeaddr_cmp(dest, &e->dest)) {
|
2009-05-10 23:08:01 +02:00
|
|
|
if(e->cost < lowest_cost) {
|
|
|
|
best_entry = e;
|
|
|
|
lowest_cost = e->cost;
|
|
|
|
}
|
2007-03-15 20:52:51 +01:00
|
|
|
}
|
|
|
|
}
|
2009-05-10 23:08:01 +02:00
|
|
|
return best_entry;
|
2007-03-15 20:52:51 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-05-04 13:23:30 +02:00
|
|
|
route_refresh(struct route_entry *e)
|
|
|
|
{
|
|
|
|
if(e != NULL) {
|
|
|
|
/* Refresh age of route so that used routes do not get thrown
|
|
|
|
out. */
|
|
|
|
e->time = 0;
|
2009-05-10 23:08:01 +02:00
|
|
|
e->decay = 0;
|
2010-02-03 02:18:24 +01:00
|
|
|
|
|
|
|
PRINTF("route_refresh: time %d last %d decay %d for entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->time, e->time_last_decay, e->decay,
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
|
|
|
|
2009-05-10 23:08:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
route_decay(struct route_entry *e)
|
|
|
|
{
|
|
|
|
/* If routes are not refreshed, they decay over time. This function
|
|
|
|
is called to decay a route. The route can only be decayed once
|
|
|
|
per second. */
|
|
|
|
PRINTF("route_decay: time %d last %d decay %d for entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->time, e->time_last_decay, e->decay,
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
|
|
|
|
|
|
|
if(e->time != e->time_last_decay) {
|
|
|
|
/* Do not decay a route too often - not more than once per second. */
|
|
|
|
e->time_last_decay = e->time;
|
|
|
|
e->decay++;
|
|
|
|
|
|
|
|
if(e->decay >= DECAY_THRESHOLD) {
|
|
|
|
PRINTF("route_decay: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
|
|
|
|
e->dest.u8[0], e->dest.u8[1],
|
|
|
|
e->nexthop.u8[0], e->nexthop.u8[1],
|
|
|
|
e->cost);
|
|
|
|
route_remove(e);
|
|
|
|
}
|
2009-05-04 13:23:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-15 20:52:51 +01:00
|
|
|
route_remove(struct route_entry *e)
|
|
|
|
{
|
|
|
|
list_remove(route_table, e);
|
|
|
|
memb_free(&route_mem, e);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
route_flush_all(void)
|
|
|
|
{
|
|
|
|
struct route_entry *e;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
e = list_pop(route_table);
|
|
|
|
if(e != NULL) {
|
|
|
|
memb_free(&route_mem, e);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-25 14:03:59 +02:00
|
|
|
void
|
|
|
|
route_set_lifetime(int seconds)
|
|
|
|
{
|
|
|
|
max_time = seconds;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-08 08:58:23 +01:00
|
|
|
int
|
|
|
|
route_num(void)
|
|
|
|
{
|
|
|
|
struct route_entry *e;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-06-15 21:22:25 +02:00
|
|
|
for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
|
2008-01-08 08:58:23 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct route_entry *
|
|
|
|
route_get(int num)
|
|
|
|
{
|
|
|
|
struct route_entry *e;
|
|
|
|
int i = 0;
|
|
|
|
|
2010-06-15 21:22:25 +02:00
|
|
|
for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
|
2008-01-08 08:58:23 +01:00
|
|
|
if(i == num) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:31:27 +02:00
|
|
|
/** @} */
|