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