Contiki 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  * \addtogroup smrf-multicast
34  * @{
35  */
36 /**
37  * \file
38  * This file implements 'Stateless Multicast RPL Forwarding' (SMRF)
39  *
40  * \author
41  * George Oikonomou - <oikonomou@users.sourceforge.net>
42  */
43 
44 #include "contiki.h"
45 #include "contiki-net.h"
50 #include "net/rpl/rpl.h"
51 #include "net/netstack.h"
52 #include <string.h>
53 
54 #define DEBUG DEBUG_NONE
55 #include "net/ip/uip-debug.h"
56 
57 /*---------------------------------------------------------------------------*/
58 /* Macros */
59 /*---------------------------------------------------------------------------*/
60 /* CCI */
61 #define SMRF_FWD_DELAY() NETSTACK_RDC.channel_check_interval()
62 /* Number of slots in the next 500ms */
63 #define SMRF_INTERVAL_COUNT ((CLOCK_SECOND >> 2) / fwd_delay)
64 /*---------------------------------------------------------------------------*/
65 /* Internal Data */
66 /*---------------------------------------------------------------------------*/
67 static struct ctimer mcast_periodic;
68 static uint8_t mcast_len;
69 static uip_buf_t mcast_buf;
70 static uint8_t fwd_delay;
71 static uint8_t fwd_spread;
72 /*---------------------------------------------------------------------------*/
73 /* uIPv6 Pointers */
74 /*---------------------------------------------------------------------------*/
75 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
76 /*---------------------------------------------------------------------------*/
77 static void
78 mcast_fwd(void *p)
79 {
80  memcpy(uip_buf, &mcast_buf, mcast_len);
81  uip_len = mcast_len;
82  UIP_IP_BUF->ttl--;
84  uip_clear_buf();
85 }
86 /*---------------------------------------------------------------------------*/
87 static uint8_t
88 in()
89 {
90  rpl_dag_t *d; /* Our DODAG */
91  uip_ipaddr_t *parent_ipaddr; /* Our pref. parent's IPv6 address */
92  const uip_lladdr_t *parent_lladdr; /* Our pref. parent's LL address */
93 
94  /*
95  * Fetch a pointer to the LL address of our preferred parent
96  *
97  * ToDo: This rpl_get_any_dag() call is a dirty replacement of the previous
98  * rpl_get_dag(RPL_DEFAULT_INSTANCE);
99  * so that things can compile with the new RPL code. This needs updated to
100  * read instance ID from the RPL HBHO and use the correct parent accordingly
101  */
102  d = rpl_get_any_dag();
103  if(!d) {
104  UIP_MCAST6_STATS_ADD(mcast_dropped);
105  return UIP_MCAST6_DROP;
106  }
107 
108  /* Retrieve our preferred parent's LL address */
109  parent_ipaddr = rpl_get_parent_ipaddr(d->preferred_parent);
110  parent_lladdr = uip_ds6_nbr_lladdr_from_ipaddr(parent_ipaddr);
111 
112  if(parent_lladdr == NULL) {
113  UIP_MCAST6_STATS_ADD(mcast_dropped);
114  return UIP_MCAST6_DROP;
115  }
116 
117  /*
118  * We accept a datagram if it arrived from our preferred parent, discard
119  * otherwise.
120  */
121  if(memcmp(parent_lladdr, packetbuf_addr(PACKETBUF_ADDR_SENDER),
122  UIP_LLADDR_LEN)) {
123  PRINTF("SMRF: Routable in but SMRF ignored it\n");
124  UIP_MCAST6_STATS_ADD(mcast_dropped);
125  return UIP_MCAST6_DROP;
126  }
127 
128  if(UIP_IP_BUF->ttl <= 1) {
129  UIP_MCAST6_STATS_ADD(mcast_dropped);
130  return UIP_MCAST6_DROP;
131  }
132 
133  UIP_MCAST6_STATS_ADD(mcast_in_all);
134  UIP_MCAST6_STATS_ADD(mcast_in_unique);
135 
136  /* If we have an entry in the mcast routing table, something with
137  * a higher RPL rank (somewhere down the tree) is a group member */
138  if(uip_mcast6_route_lookup(&UIP_IP_BUF->destipaddr)) {
139  /* If we enter here, we will definitely forward */
140  UIP_MCAST6_STATS_ADD(mcast_fwd);
141 
142  /*
143  * Add a delay (D) of at least SMRF_FWD_DELAY() to compensate for how
144  * contikimac handles broadcasts. We can't start our TX before the sender
145  * has finished its own.
146  */
147  fwd_delay = SMRF_FWD_DELAY();
148 
149  /* Finalise D: D = min(SMRF_FWD_DELAY(), SMRF_MIN_FWD_DELAY) */
150 #if SMRF_MIN_FWD_DELAY
151  if(fwd_delay < SMRF_MIN_FWD_DELAY) {
152  fwd_delay = SMRF_MIN_FWD_DELAY;
153  }
154 #endif
155 
156  if(fwd_delay == 0) {
157  /* No delay required, send it, do it now, why wait? */
158  UIP_IP_BUF->ttl--;
160  UIP_IP_BUF->ttl++; /* Restore before potential upstack delivery */
161  } else {
162  /* Randomise final delay in [D , D*Spread], step D */
163  fwd_spread = SMRF_INTERVAL_COUNT;
164  if(fwd_spread > SMRF_MAX_SPREAD) {
165  fwd_spread = SMRF_MAX_SPREAD;
166  }
167  if(fwd_spread) {
168  fwd_delay = fwd_delay * (1 + ((random_rand() >> 11) % fwd_spread));
169  }
170 
171  memcpy(&mcast_buf, uip_buf, uip_len);
172  mcast_len = uip_len;
173  ctimer_set(&mcast_periodic, fwd_delay, mcast_fwd, NULL);
174  }
175  PRINTF("SMRF: %u bytes: fwd in %u [%u]\n",
176  uip_len, fwd_delay, fwd_spread);
177  }
178 
179  /* Done with this packet unless we are a member of the mcast group */
180  if(!uip_ds6_is_my_maddr(&UIP_IP_BUF->destipaddr)) {
181  PRINTF("SMRF: Not a group member. No further processing\n");
182  return UIP_MCAST6_DROP;
183  } else {
184  PRINTF("SMRF: Ours. Deliver to upper layers\n");
185  UIP_MCAST6_STATS_ADD(mcast_in_ours);
186  return UIP_MCAST6_ACCEPT;
187  }
188 }
189 /*---------------------------------------------------------------------------*/
190 static void
191 init()
192 {
193  UIP_MCAST6_STATS_INIT(NULL);
194 
196 }
197 /*---------------------------------------------------------------------------*/
198 static void
199 out()
200 {
201  return;
202 }
203 /*---------------------------------------------------------------------------*/
204 /**
205  * \brief The SMRF engine driver
206  */
208  "SMRF",
209  init,
210  out,
211  in,
212 };
213 /*---------------------------------------------------------------------------*/
214 /** @} */
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
The data structure used to represent a multicast engine.
Definition: uip-mcast6.h:99
uint8_t(* in)(void)
Process an incoming multicast datagram and determine whether it should be delivered up the stack or n...
Definition: uip-mcast6.h:137
void(* out)(void)
Process an outgoing datagram with a multicast IPv6 destination address.
Definition: uip-mcast6.h:119
#define UIP_LLADDR_LEN
Link layer address length.
Definition: uip.h:151
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:523
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
void uip_mcast6_route_init()
Multicast routing table init routine.
This header file contains configuration directives for uIPv6 multicast support.
#define NULL
The null pointer.
802.3 address
Definition: uip.h:129
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
uint8_t tcpip_output(const uip_lladdr_t *a)
Output packet to layer 2 The eventual parameter is the MAC address of the destination.
Definition: tcpip.c:120
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
A set of debugging macros for the IP stack
Header file for the SMRF forwarding engine.
uip_mcast6_route_t * uip_mcast6_route_lookup(uip_ipaddr_t *group)
Lookup a multicast route.
Header file for IPv6 multicast forwarding stats maintenance
The uIP packet buffer.
Definition: uip.h:515
Header file for multicast routing table manipulation.
const struct uip_mcast6_driver smrf_driver
The SMRF engine driver.
Definition: smrf.c:207
Include file for the Contiki low-layer network stack (NETSTACK)
void(* init)(void)
Initialize the multicast engine.
Definition: uip-mcast6.h:104