Contiki-NG
orchestra-rule-unicast-per-neighbor-rpl-storing.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 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  */
30 /**
31  * \file
32  * Orchestra: a slotframe dedicated to unicast data transmission. Designed for
33  * RPL storing mode only, as this is based on the knowledge of the children (and parent).
34  * If receiver-based:
35  * Nodes listen at a timeslot defined as hash(MAC) % ORCHESTRA_SB_UNICAST_PERIOD
36  * Nodes transmit at: for each nbr in RPL children and RPL preferred parent,
37  * hash(nbr.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
38  * If sender-based: the opposite
39  *
40  * \author Simon Duquennoy <simonduq@sics.se>
41  */
42 
43 #include "contiki.h"
44 #include "orchestra.h"
45 #include "net/ipv6/uip-ds6-route.h"
46 #include "net/packetbuf.h"
47 #include "net/routing/routing.h"
48 
49 /*
50  * The body of this rule should be compiled only when "nbr_routes" is available,
51  * otherwise a link error causes build failure. "nbr_routes" is compiled if
52  * UIP_MAX_ROUTES != 0. See uip-ds6-route.c.
53  */
54 #if UIP_MAX_ROUTES != 0
55 
56 #if ORCHESTRA_UNICAST_SENDER_BASED && ORCHESTRA_COLLISION_FREE_HASH
57 #define UNICAST_SLOT_SHARED_FLAG ((ORCHESTRA_UNICAST_PERIOD < (ORCHESTRA_MAX_HASH + 1)) ? LINK_OPTION_SHARED : 0)
58 #else
59 #define UNICAST_SLOT_SHARED_FLAG LINK_OPTION_SHARED
60 #endif
61 
62 static uint16_t slotframe_handle = 0;
63 static uint16_t local_channel_offset;
64 static struct tsch_slotframe *sf_unicast;
65 
66 /*---------------------------------------------------------------------------*/
67 static uint16_t
68 get_node_timeslot(const linkaddr_t *addr)
69 {
70  if(addr != NULL && ORCHESTRA_UNICAST_PERIOD > 0) {
71  return ORCHESTRA_LINKADDR_HASH(addr) % ORCHESTRA_UNICAST_PERIOD;
72  } else {
73  return 0xffff;
74  }
75 }
76 /*---------------------------------------------------------------------------*/
77 static uint16_t
78 get_node_channel_offset(const linkaddr_t *addr)
79 {
80  if(addr != NULL && ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET >= ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET) {
81  return ORCHESTRA_LINKADDR_HASH(addr) % (ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET - ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET + 1)
82  + ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET;
83  } else {
84  return 0xffff;
85  }
86 }
87 /*---------------------------------------------------------------------------*/
88 static int
89 neighbor_has_uc_link(const linkaddr_t *linkaddr)
90 {
91  if(linkaddr != NULL && !linkaddr_cmp(linkaddr, &linkaddr_null)) {
92  if((orchestra_parent_knows_us || !ORCHESTRA_UNICAST_SENDER_BASED)
93  && linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
94  return 1;
95  }
96  if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
97  return 1;
98  }
99  }
100  return 0;
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 add_uc_link(const linkaddr_t *linkaddr)
105 {
106  if(linkaddr != NULL) {
107  uint16_t timeslot = get_node_timeslot(linkaddr);
108  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_RX : LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG;
109 
110  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
111  /* This is also our timeslot, add necessary flags */
112  link_options |= ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
113  }
114 
115  /* Add/update link.
116  * Always configure the link with the local node's channel offset.
117  * If this is an Rx link, that is what the node needs to use.
118  * If this is a Tx link, packet's channel offset will override the link's channel offset.
119  */
120  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
121  timeslot, local_channel_offset);
122  }
123 }
124 /*---------------------------------------------------------------------------*/
125 static void
126 remove_uc_link(const linkaddr_t *linkaddr)
127 {
128  uint16_t timeslot;
129  struct tsch_link *l;
130 
131  if(linkaddr == NULL) {
132  return;
133  }
134 
135  timeslot = get_node_timeslot(linkaddr);
136  l = tsch_schedule_get_link_by_timeslot(sf_unicast, timeslot, local_channel_offset);
137  if(l == NULL) {
138  return;
139  }
140  /* Does our current parent need this timeslot? */
141  if(timeslot == get_node_timeslot(&orchestra_parent_linkaddr)) {
142  /* Yes, this timeslot is being used, return */
143  return;
144  }
145  /* Does any other child need this timeslot?
146  * (lookup all route next hops) */
147  nbr_table_item_t *item = nbr_table_head(nbr_routes);
148  while(item != NULL) {
149  linkaddr_t *addr = nbr_table_get_lladdr(nbr_routes, item);
150  if(timeslot == get_node_timeslot(addr)) {
151  /* Yes, this timeslot is being used, return */
152  return;
153  }
154  item = nbr_table_next(nbr_routes, item);
155  }
156 
157  /* Do we need this timeslot? */
158  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
159  /* This is our link, keep it but update the link options */
160  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
161  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
162  timeslot, local_channel_offset);
163  } else {
164  /* Remove link */
165  tsch_schedule_remove_link(sf_unicast, l);
166  }
167 }
168 /*---------------------------------------------------------------------------*/
169 static void
170 child_added(const linkaddr_t *linkaddr)
171 {
172  add_uc_link(linkaddr);
173 }
174 /*---------------------------------------------------------------------------*/
175 static void
176 child_removed(const linkaddr_t *linkaddr)
177 {
178  remove_uc_link(linkaddr);
179 }
180 /*---------------------------------------------------------------------------*/
181 static int
182 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
183 {
184  /* Select data packets we have a unicast link to */
185  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
186  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
187  && neighbor_has_uc_link(dest)) {
188  if(slotframe != NULL) {
189  *slotframe = slotframe_handle;
190  }
191  if(timeslot != NULL) {
192  *timeslot = ORCHESTRA_UNICAST_SENDER_BASED ? get_node_timeslot(&linkaddr_node_addr) : get_node_timeslot(dest);
193  }
194  /* set per-packet channel offset */
195  if(channel_offset != NULL) {
196  *channel_offset = get_node_channel_offset(dest);
197  }
198  return 1;
199  }
200  return 0;
201 }
202 /*---------------------------------------------------------------------------*/
203 static void
204 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
205 {
206  if(new != old) {
207  const linkaddr_t *old_addr = old != NULL ? &old->addr : NULL;
208  const linkaddr_t *new_addr = new != NULL ? &new->addr : NULL;
209  if(new_addr != NULL) {
210  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
211  } else {
212  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
213  }
214  remove_uc_link(old_addr);
215  add_uc_link(new_addr);
216  }
217 }
218 /*---------------------------------------------------------------------------*/
219 static void
220 init(uint16_t sf_handle)
221 {
222  uint16_t timeslot;
223  linkaddr_t *local_addr = &linkaddr_node_addr;
224 
225  slotframe_handle = sf_handle;
226  local_channel_offset = get_node_channel_offset(local_addr);
227  /* Slotframe for unicast transmissions */
228  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
229  timeslot = get_node_timeslot(local_addr);
230  tsch_schedule_add_link(sf_unicast,
231  ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX,
232  LINK_TYPE_NORMAL, &tsch_broadcast_address,
233  timeslot, local_channel_offset);
234 }
235 /*---------------------------------------------------------------------------*/
236 struct orchestra_rule unicast_per_neighbor_rpl_storing = {
237  init,
238  new_time_source,
239  select_packet,
240  child_added,
241  child_removed,
242  "unicast per neighbor storing",
243 };
244 
245 #endif /* UIP_MAX_ROUTES */
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:107
TSCH neighbor information.
Definition: tsch-types.h:109
802.15.4e slotframe (contains links)
Definition: tsch-types.h:84
Orchestra header file
struct tsch_slotframe * tsch_schedule_add_slotframe(uint16_t handle, uint16_t size)
Creates and adds a new slotframe.
Definition: tsch-schedule.c:72
const linkaddr_t linkaddr_null
The null link-layer address.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
Routing driver header file
int tsch_schedule_remove_link(struct tsch_slotframe *slotframe, struct tsch_link *l)
Removes a link.
struct tsch_link * tsch_schedule_add_link(struct tsch_slotframe *slotframe, uint8_t link_options, enum link_type link_type, const linkaddr_t *address, uint16_t timeslot, uint16_t channel_offset)
Adds a link to a slotframe.
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
struct tsch_link * tsch_schedule_get_link_by_timeslot(struct tsch_slotframe *slotframe, uint16_t timeslot, uint16_t channel_offset)
Looks within a slotframe for a link with a given timeslot.
Header file for routing table manipulation.
Header file for the Packet buffer (packetbuf) management