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 channel_offset = 0;
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 int
78 neighbor_has_uc_link(const linkaddr_t *linkaddr)
79 {
80  if(linkaddr != NULL && !linkaddr_cmp(linkaddr, &linkaddr_null)) {
81  if((orchestra_parent_knows_us || !ORCHESTRA_UNICAST_SENDER_BASED)
82  && linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
83  return 1;
84  }
85  if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
86  return 1;
87  }
88  }
89  return 0;
90 }
91 /*---------------------------------------------------------------------------*/
92 static void
93 add_uc_link(const linkaddr_t *linkaddr)
94 {
95  if(linkaddr != NULL) {
96  uint16_t timeslot = get_node_timeslot(linkaddr);
97  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_RX : LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG;
98 
99  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
100  /* This is also our timeslot, add necessary flags */
101  link_options |= ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
102  }
103 
104  /* Add/update link */
105  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
106  timeslot, channel_offset);
107  }
108 }
109 /*---------------------------------------------------------------------------*/
110 static void
111 remove_uc_link(const linkaddr_t *linkaddr)
112 {
113  uint16_t timeslot;
114  struct tsch_link *l;
115 
116  if(linkaddr == NULL) {
117  return;
118  }
119 
120  timeslot = get_node_timeslot(linkaddr);
121  l = tsch_schedule_get_link_by_timeslot(sf_unicast, timeslot);
122  if(l == NULL) {
123  return;
124  }
125  /* Does our current parent need this timeslot? */
126  if(timeslot == get_node_timeslot(&orchestra_parent_linkaddr)) {
127  /* Yes, this timeslot is being used, return */
128  return;
129  }
130  /* Does any other child need this timeslot?
131  * (lookup all route next hops) */
132  nbr_table_item_t *item = nbr_table_head(nbr_routes);
133  while(item != NULL) {
134  linkaddr_t *addr = nbr_table_get_lladdr(nbr_routes, item);
135  if(timeslot == get_node_timeslot(addr)) {
136  /* Yes, this timeslot is being used, return */
137  return;
138  }
139  item = nbr_table_next(nbr_routes, item);
140  }
141 
142  /* Do we need this timeslot? */
143  if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
144  /* This is our link, keep it but update the link options */
145  uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
146  tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
147  timeslot, channel_offset);
148  } else {
149  /* Remove link */
150  tsch_schedule_remove_link(sf_unicast, l);
151  }
152 }
153 /*---------------------------------------------------------------------------*/
154 static void
155 child_added(const linkaddr_t *linkaddr)
156 {
157  add_uc_link(linkaddr);
158 }
159 /*---------------------------------------------------------------------------*/
160 static void
161 child_removed(const linkaddr_t *linkaddr)
162 {
163  remove_uc_link(linkaddr);
164 }
165 /*---------------------------------------------------------------------------*/
166 static int
167 select_packet(uint16_t *slotframe, uint16_t *timeslot)
168 {
169  /* Select data packets we have a unicast link to */
170  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
171  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
172  && neighbor_has_uc_link(dest)) {
173  if(slotframe != NULL) {
174  *slotframe = slotframe_handle;
175  }
176  if(timeslot != NULL) {
177  *timeslot = ORCHESTRA_UNICAST_SENDER_BASED ? get_node_timeslot(&linkaddr_node_addr) : get_node_timeslot(dest);
178  }
179  return 1;
180  }
181  return 0;
182 }
183 /*---------------------------------------------------------------------------*/
184 static void
185 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
186 {
187  if(new != old) {
188  const linkaddr_t *old_addr = old != NULL ? &old->addr : NULL;
189  const linkaddr_t *new_addr = new != NULL ? &new->addr : NULL;
190  if(new_addr != NULL) {
191  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
192  } else {
193  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
194  }
195  remove_uc_link(old_addr);
196  add_uc_link(new_addr);
197  }
198 }
199 /*---------------------------------------------------------------------------*/
200 static void
201 init(uint16_t sf_handle)
202 {
203  slotframe_handle = sf_handle;
204  channel_offset = sf_handle;
205  /* Slotframe for unicast transmissions */
206  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
207  uint16_t timeslot = get_node_timeslot(&linkaddr_node_addr);
208  tsch_schedule_add_link(sf_unicast,
209  ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX,
210  LINK_TYPE_NORMAL, &tsch_broadcast_address,
211  timeslot, channel_offset);
212 }
213 /*---------------------------------------------------------------------------*/
214 struct orchestra_rule unicast_per_neighbor_rpl_storing = {
215  init,
216  new_time_source,
217  select_packet,
218  child_added,
219  child_removed,
220 };
221 
222 #endif /* UIP_MAX_ROUTES */
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
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
struct tsch_link * tsch_schedule_get_link_by_timeslot(struct tsch_slotframe *slotframe, uint16_t timeslot)
Looks within a slotframe for a link with a given timeslot.
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
Header file for routing table manipulation.
Header file for the Packet buffer (packetbuf) management