Contiki 3.x
route.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 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  * \file
35  * Rime route table
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimeroute
42  * @{
43  */
44 
45 #include <stdio.h>
46 
47 #include "lib/list.h"
48 #include "lib/memb.h"
49 #include "sys/ctimer.h"
50 #include "net/rime/route.h"
51 #include "contiki-conf.h"
52 
53 #ifdef ROUTE_CONF_ENTRIES
54 #define NUM_RT_ENTRIES ROUTE_CONF_ENTRIES
55 #else /* ROUTE_CONF_ENTRIES */
56 #define NUM_RT_ENTRIES 8
57 #endif /* ROUTE_CONF_ENTRIES */
58 
59 #ifdef ROUTE_CONF_DECAY_THRESHOLD
60 #define DECAY_THRESHOLD ROUTE_CONF_DECAY_THRESHOLD
61 #else /* ROUTE_CONF_DECAY_THRESHOLD */
62 #define DECAY_THRESHOLD 8
63 #endif /* ROUTE_CONF_DECAY_THRESHOLD */
64 
65 #ifdef ROUTE_CONF_DEFAULT_LIFETIME
66 #define DEFAULT_LIFETIME ROUTE_CONF_DEFAULT_LIFETIME
67 #else /* ROUTE_CONF_DEFAULT_LIFETIME */
68 #define DEFAULT_LIFETIME 60
69 #endif /* ROUTE_CONF_DEFAULT_LIFETIME */
70 
71 /*
72  * List of route entries.
73  */
74 LIST(route_table);
75 MEMB(route_mem, struct route_entry, NUM_RT_ENTRIES);
76 
77 static struct ctimer t;
78 
79 static int max_time = DEFAULT_LIFETIME;
80 
81 #define DEBUG 0
82 #if DEBUG
83 #include <stdio.h>
84 #define PRINTF(...) printf(__VA_ARGS__)
85 #else
86 #define PRINTF(...)
87 #endif
88 
89 
90 /*---------------------------------------------------------------------------*/
91 static void
92 periodic(void *ptr)
93 {
94  struct route_entry *e;
95 
96  for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
97  e->time++;
98  if(e->time >= max_time) {
99  PRINTF("route periodic: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
100  e->dest.u8[0], e->dest.u8[1],
101  e->nexthop.u8[0], e->nexthop.u8[1],
102  e->cost);
103  list_remove(route_table, e);
104  memb_free(&route_mem, e);
105  }
106  }
107 
108  ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
109 }
110 /*---------------------------------------------------------------------------*/
111 void
112 route_init(void)
113 {
114  list_init(route_table);
115  memb_init(&route_mem);
116 
117  ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
118 }
119 /*---------------------------------------------------------------------------*/
120 int
121 route_add(const linkaddr_t *dest, const linkaddr_t *nexthop,
122  uint8_t cost, uint8_t seqno)
123 {
124  struct route_entry *e;
125 
126  /* Avoid inserting duplicate entries. */
127  e = route_lookup(dest);
128  if(e != NULL && linkaddr_cmp(&e->nexthop, nexthop)) {
129  list_remove(route_table, e);
130  } else {
131  /* Allocate a new entry or reuse the oldest entry with highest cost. */
132  e = memb_alloc(&route_mem);
133  if(e == NULL) {
134  /* Remove oldest entry. XXX */
135  e = list_chop(route_table);
136  PRINTF("route_add: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
137  e->dest.u8[0], e->dest.u8[1],
138  e->nexthop.u8[0], e->nexthop.u8[1],
139  e->cost);
140  }
141  }
142 
143  linkaddr_copy(&e->dest, dest);
144  linkaddr_copy(&e->nexthop, nexthop);
145  e->cost = cost;
146  e->seqno = seqno;
147  e->time = 0;
148  e->decay = 0;
149 
150  /* New entry goes first. */
151  list_push(route_table, e);
152 
153  PRINTF("route_add: new entry to %d.%d with nexthop %d.%d and cost %d\n",
154  e->dest.u8[0], e->dest.u8[1],
155  e->nexthop.u8[0], e->nexthop.u8[1],
156  e->cost);
157 
158  return 0;
159 }
160 /*---------------------------------------------------------------------------*/
161 struct route_entry *
162 route_lookup(const linkaddr_t *dest)
163 {
164  struct route_entry *e;
165  uint8_t lowest_cost;
166  struct route_entry *best_entry;
167 
168  lowest_cost = -1;
169  best_entry = NULL;
170 
171  /* Find the route with the lowest cost. */
172  for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
173  /* printf("route_lookup: comparing %d.%d.%d.%d with %d.%d.%d.%d\n",
174  uip_ipaddr_to_quad(dest), uip_ipaddr_to_quad(&e->dest));*/
175 
176  if(linkaddr_cmp(dest, &e->dest)) {
177  if(e->cost < lowest_cost) {
178  best_entry = e;
179  lowest_cost = e->cost;
180  }
181  }
182  }
183  return best_entry;
184 }
185 /*---------------------------------------------------------------------------*/
186 void
187 route_refresh(struct route_entry *e)
188 {
189  if(e != NULL) {
190  /* Refresh age of route so that used routes do not get thrown
191  out. */
192  e->time = 0;
193  e->decay = 0;
194 
195  PRINTF("route_refresh: time %d last %d decay %d for entry to %d.%d with nexthop %d.%d and cost %d\n",
196  e->time, e->time_last_decay, e->decay,
197  e->dest.u8[0], e->dest.u8[1],
198  e->nexthop.u8[0], e->nexthop.u8[1],
199  e->cost);
200 
201  }
202 }
203 /*---------------------------------------------------------------------------*/
204 void
205 route_decay(struct route_entry *e)
206 {
207  /* If routes are not refreshed, they decay over time. This function
208  is called to decay a route. The route can only be decayed once
209  per second. */
210  PRINTF("route_decay: time %d last %d decay %d for entry to %d.%d with nexthop %d.%d and cost %d\n",
211  e->time, e->time_last_decay, e->decay,
212  e->dest.u8[0], e->dest.u8[1],
213  e->nexthop.u8[0], e->nexthop.u8[1],
214  e->cost);
215 
216  if(e->time != e->time_last_decay) {
217  /* Do not decay a route too often - not more than once per second. */
218  e->time_last_decay = e->time;
219  e->decay++;
220 
221  if(e->decay >= DECAY_THRESHOLD) {
222  PRINTF("route_decay: removing entry to %d.%d with nexthop %d.%d and cost %d\n",
223  e->dest.u8[0], e->dest.u8[1],
224  e->nexthop.u8[0], e->nexthop.u8[1],
225  e->cost);
226  route_remove(e);
227  }
228  }
229 }
230 /*---------------------------------------------------------------------------*/
231 void
232 route_remove(struct route_entry *e)
233 {
234  list_remove(route_table, e);
235  memb_free(&route_mem, e);
236 }
237 /*---------------------------------------------------------------------------*/
238 void
239 route_flush_all(void)
240 {
241  struct route_entry *e;
242 
243  while(1) {
244  e = list_pop(route_table);
245  if(e != NULL) {
246  memb_free(&route_mem, e);
247  } else {
248  break;
249  }
250  }
251 }
252 /*---------------------------------------------------------------------------*/
253 void
254 route_set_lifetime(int seconds)
255 {
256  max_time = seconds;
257 }
258 /*---------------------------------------------------------------------------*/
259 int
260 route_num(void)
261 {
262  struct route_entry *e;
263  int i = 0;
264 
265  for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
266  i++;
267  }
268  return i;
269 }
270 /*---------------------------------------------------------------------------*/
271 struct route_entry *
272 route_get(int num)
273 {
274  struct route_entry *e;
275  int i = 0;
276 
277  for(e = list_head(route_table); e != NULL; e = list_item_next(e)) {
278  if(i == num) {
279  return e;
280  }
281  i++;
282  }
283  return NULL;
284 }
285 /*---------------------------------------------------------------------------*/
286 /** @} */
void * list_pop(list_t list)
Remove the first object on a list.
Definition: list.c:218
Linked list manipulation routines.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
void list_push(list_t list, void *item)
Add an item to the start of the list.
Definition: list.c:165
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
#define LIST(name)
Declare a linked list.
Definition: list.h:86
Header file for the callback timer
void * list_chop(list_t list)
Remove the last object on the list.
Definition: list.c:186
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
Memory block allocation routines.
Header file for the Rime route table
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82