Contiki-Inga 3.x
smrf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Loughborough University - 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  * \file
34  * This file implements 'Stateless Multicast RPL Forwarding' (SMRF)
35  *
36  * It will only work in RPL networks in MOP 3 "Storing with Multicast"
37  *
38  * \author
39  * George Oikonomou - <oikonomou@users.sourceforge.net>
40  */
41 
42 #include "contiki.h"
43 #include "contiki-net.h"
48 #include "net/rpl/rpl.h"
49 #include "net/netstack.h"
50 #include <string.h>
51 
52 #define DEBUG DEBUG_NONE
53 #include "net/ip/uip-debug.h"
54 
55 #if UIP_CONF_IPV6
56 /*---------------------------------------------------------------------------*/
57 /* Macros */
58 /*---------------------------------------------------------------------------*/
59 /* CCI */
60 #define SMRF_FWD_DELAY() NETSTACK_RDC.channel_check_interval()
61 /* Number of slots in the next 500ms */
62 #define SMRF_INTERVAL_COUNT ((CLOCK_SECOND >> 2) / fwd_delay)
63 /*---------------------------------------------------------------------------*/
64 /* Internal Data */
65 /*---------------------------------------------------------------------------*/
66 static struct ctimer mcast_periodic;
67 static uint8_t mcast_len;
68 static uip_buf_t mcast_buf;
69 static uint8_t fwd_delay;
70 static uint8_t fwd_spread;
71 /*---------------------------------------------------------------------------*/
72 /* uIPv6 Pointers */
73 /*---------------------------------------------------------------------------*/
74 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
75 /*---------------------------------------------------------------------------*/
76 static void
77 mcast_fwd(void *p)
78 {
79  memcpy(uip_buf, &mcast_buf, mcast_len);
80  uip_len = mcast_len;
81  UIP_IP_BUF->ttl--;
83  uip_len = 0;
84 }
85 /*---------------------------------------------------------------------------*/
86 static uint8_t
87 in()
88 {
89  rpl_dag_t *d; /* Our DODAG */
90  uip_ipaddr_t *parent_ipaddr; /* Our pref. parent's IPv6 address */
91  const uip_lladdr_t *parent_lladdr; /* Our pref. parent's LL address */
92 
93  /*
94  * Fetch a pointer to the LL address of our preferred parent
95  *
96  * ToDo: This rpl_get_any_dag() call is a dirty replacement of the previous
97  * rpl_get_dag(RPL_DEFAULT_INSTANCE);
98  * so that things can compile with the new RPL code. This needs updated to
99  * read instance ID from the RPL HBHO and use the correct parent accordingly
100  */
101  d = rpl_get_any_dag();
102  if(!d) {
103  UIP_MCAST6_STATS_ADD(mcast_dropped);
104  return UIP_MCAST6_DROP;
105  }
106 
107  /* Retrieve our preferred parent's LL address */
108  parent_ipaddr = rpl_get_parent_ipaddr(d->preferred_parent);
109  parent_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(parent_ipaddr);
110 
111  if(parent_lladdr == NULL) {
112  UIP_MCAST6_STATS_ADD(mcast_dropped);
113  return UIP_MCAST6_DROP;
114  }
115 
116  /*
117  * We accept a datagram if it arrived from our preferred parent, discard
118  * otherwise.
119  */
120  if(memcmp(parent_lladdr, packetbuf_addr(PACKETBUF_ADDR_SENDER),
121  UIP_LLADDR_LEN)) {
122  PRINTF("SMRF: Routable in but SMRF ignored it\n");
123  UIP_MCAST6_STATS_ADD(mcast_dropped);
124  return UIP_MCAST6_DROP;
125  }
126 
127  if(UIP_IP_BUF->ttl <= 1) {
128  UIP_MCAST6_STATS_ADD(mcast_dropped);
129  return UIP_MCAST6_DROP;
130  }
131 
132  UIP_MCAST6_STATS_ADD(mcast_in_all);
133  UIP_MCAST6_STATS_ADD(mcast_in_unique);
134 
135  /* If we have an entry in the mcast routing table, something with
136  * a higher RPL rank (somewhere down the tree) is a group member */
137  if(uip_mcast6_route_lookup(&UIP_IP_BUF->destipaddr)) {
138  /* If we enter here, we will definitely forward */
139  UIP_MCAST6_STATS_ADD(mcast_fwd);
140 
141  /*
142  * Add a delay (D) of at least SMRF_FWD_DELAY() to compensate for how
143  * contikimac handles broadcasts. We can't start our TX before the sender
144  * has finished its own.
145  */
146  fwd_delay = SMRF_FWD_DELAY();
147 
148  /* Finalise D: D = min(SMRF_FWD_DELAY(), SMRF_MIN_FWD_DELAY) */
149 #if SMRF_MIN_FWD_DELAY
150  if(fwd_delay < SMRF_MIN_FWD_DELAY) {
151  fwd_delay = SMRF_MIN_FWD_DELAY;
152  }
153 #endif
154 
155  if(fwd_delay == 0) {
156  /* No delay required, send it, do it now, why wait? */
157  UIP_IP_BUF->ttl--;
159  UIP_IP_BUF->ttl++; /* Restore before potential upstack delivery */
160  } else {
161  /* Randomise final delay in [D , D*Spread], step D */
162  fwd_spread = SMRF_INTERVAL_COUNT;
163  if(fwd_spread > SMRF_MAX_SPREAD) {
164  fwd_spread = SMRF_MAX_SPREAD;
165  }
166  if(fwd_spread) {
167  fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread));
168  }
169 
170  memcpy(&mcast_buf, uip_buf, uip_len);
171  mcast_len = uip_len;
172  ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL);
173  }
174  PRINTF("SMRF: %u bytes: fwd in %u [%u]\n",
175  uip_len, fwd_delay, fwd_spread);
176  }
177 
178  /* Done with this packet unless we are a member of the mcast group */
179  if(!uip_ds6_is_my_maddr(&UIP_IP_BUF->destipaddr)) {
180  PRINTF("SMRF: Not a group member. No further processing\n");
181  return UIP_MCAST6_DROP;
182  } else {
183  PRINTF("SMRF: Ours. Deliver to upper layers\n");
184  UIP_MCAST6_STATS_ADD(mcast_in_ours);
185  return UIP_MCAST6_ACCEPT;
186  }
187 }
188 /*---------------------------------------------------------------------------*/
189 static void
190 init()
191 {
192  UIP_MCAST6_STATS_INIT(NULL);
193 
195 }
196 /*---------------------------------------------------------------------------*/
197 static void
198 out()
199 {
200  return;
201 }
202 /*---------------------------------------------------------------------------*/
203 const struct uip_mcast6_driver smrf_driver = {
204  "SMRF",
205  init,
206  out,
207  in,
208 };
209 /*---------------------------------------------------------------------------*/
210 
211 #endif /* UIP_CONF_IPV6 */