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, 1);
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  if(!ORCHESTRA_UNICAST_SENDER_BASED) {
141  /* Packets to this address were marked with this slotframe and neighbor-specific timeslot;
142  * make sure they don't remain stuck in the queues after the link is removed. */
143  tsch_queue_free_packets_to(linkaddr);
144  }
145 
146  /* Does our current parent need this timeslot? */
147  if(timeslot == get_node_timeslot(&orchestra_parent_linkaddr)) {
148  /* Yes, this timeslot is being used, return */
149  return;
150  }
151  /* Does any other child need this timeslot?
152  * (lookup all route next hops) */
153  nbr_table_item_t *item = nbr_table_head(nbr_routes);
154  while(item != NULL) {
155  linkaddr_t *addr = nbr_table_get_lladdr(nbr_routes, item);
156  if(timeslot == get_node_timeslot(addr)) {
157  /* Yes, this timeslot is being used, return */
158  return;
159  }
160  item = nbr_table_next(nbr_routes, item);
161  }
162 
163  /* Do we need this timeslot? */
164  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
165  /* This is our link, keep it but update the link options */
166  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
167  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
168  timeslot, local_channel_offset, 1);
169  } else {
170  /* Remove link */
171  tsch_schedule_remove_link(sf_unicast, l);
172  }
173 }
174 /*---------------------------------------------------------------------------*/
175 static void
176 child_added(const linkaddr_t *linkaddr)
177 {
178  add_uc_link(linkaddr);
179 }
180 /*---------------------------------------------------------------------------*/
181 static void
182 child_removed(const linkaddr_t *linkaddr)
183 {
184  remove_uc_link(linkaddr);
185 }
186 /*---------------------------------------------------------------------------*/
187 static int
188 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
189 {
190  /* Select data packets we have a unicast link to */
191  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
192  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
193  && !orchestra_is_root_schedule_active(dest)
194  && neighbor_has_uc_link(dest)) {
195  if(slotframe != NULL) {
196  *slotframe = slotframe_handle;
197  }
198  if(timeslot != NULL) {
199  *timeslot = ORCHESTRA_UNICAST_SENDER_BASED ? get_node_timeslot(&linkaddr_node_addr) : get_node_timeslot(dest);
200  }
201  /* set per-packet channel offset */
202  if(channel_offset != NULL) {
203  *channel_offset = get_node_channel_offset(dest);
204  }
205  return 1;
206  }
207  return 0;
208 }
209 /*---------------------------------------------------------------------------*/
210 static void
211 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
212 {
213  if(new != old) {
214  const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
215  const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
216  if(new_addr != NULL) {
217  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
218  } else {
219  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
220  }
221  remove_uc_link(old_addr);
222  add_uc_link(new_addr);
223  }
224 }
225 /*---------------------------------------------------------------------------*/
226 static void
227 init(uint16_t sf_handle)
228 {
229  uint16_t timeslot;
230  linkaddr_t *local_addr = &linkaddr_node_addr;
231 
232  slotframe_handle = sf_handle;
233  local_channel_offset = get_node_channel_offset(local_addr);
234  /* Slotframe for unicast transmissions */
235  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
236  timeslot = get_node_timeslot(local_addr);
237  tsch_schedule_add_link(sf_unicast,
238  ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX,
239  LINK_TYPE_NORMAL, &tsch_broadcast_address,
240  timeslot, local_channel_offset, 1);
241 }
242 /*---------------------------------------------------------------------------*/
243 struct orchestra_rule unicast_per_neighbor_rpl_storing = {
244  init,
245  new_time_source,
246  select_packet,
247  child_added,
248  child_removed,
249  NULL,
250  "unicast per neighbor storing",
251  ORCHESTRA_UNICAST_PERIOD,
252 };
253 
254 #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:73
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
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, uint8_t do_remove)
Adds a link to a slotframe.
linkaddr_t * tsch_queue_get_nbr_address(const struct tsch_neighbor *n)
Get the address of a neighbor.
Definition: tsch-queue.c:135
Routing driver header file
int tsch_schedule_remove_link(struct tsch_slotframe *slotframe, struct tsch_link *l)
Removes a link.
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
void tsch_queue_free_packets_to(const linkaddr_t *addr)
Flush packets to a specific address.
Definition: tsch-queue.c:324