Contiki-NG
Loading...
Searching...
No Matches
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"
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
62static uint16_t slotframe_handle = 0;
63static uint16_t local_channel_offset;
64static struct tsch_slotframe *sf_unicast;
65
66/*---------------------------------------------------------------------------*/
67static uint16_t
68get_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/*---------------------------------------------------------------------------*/
77static uint16_t
78get_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/*---------------------------------------------------------------------------*/
88static int
89neighbor_has_uc_link(const linkaddr_t *linkaddr)
90{
91 if(linkaddr == NULL || linkaddr_cmp(linkaddr, &linkaddr_null)) {
92 return 0;
93 }
94
95 if(!ORCHESTRA_UNICAST_SENDER_BASED) {
96 /* With the receiver-based Orchestra,
97 * all nodes have a link installed at their own timeslot */
98 return 1;
99 }
100
101 if(linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
102 /* The node is our parent */
103 return orchestra_parent_knows_us ? 1 : 0;
104 }
105
106 if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
107 /* We have a route to this node;
108 * it should have selected us as its parent and installed a link */
109 return 1;
110 }
111
112 return 0;
113}
114/*---------------------------------------------------------------------------*/
115static void
116add_uc_link(const linkaddr_t *linkaddr)
117{
118 if(linkaddr != NULL) {
119 uint16_t timeslot = get_node_timeslot(linkaddr);
120 uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_RX : LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG;
121
122 if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
123 /* This is also our timeslot, add necessary flags */
124 link_options |= ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
125 }
126
127 /* Add/update link.
128 * Always configure the link with the local node's channel offset.
129 * If this is an Rx link, that is what the node needs to use.
130 * If this is a Tx link, packet's channel offset will override the link's channel offset.
131 */
132 tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
133 timeslot, local_channel_offset, 1);
134 }
135}
136/*---------------------------------------------------------------------------*/
137static void
138remove_uc_link(const linkaddr_t *linkaddr)
139{
140 uint16_t timeslot;
141 struct tsch_link *l;
142
143 if(linkaddr == NULL) {
144 return;
145 }
146
147 timeslot = get_node_timeslot(linkaddr);
148 l = tsch_schedule_get_link_by_offsets(sf_unicast, timeslot, local_channel_offset);
149 if(l == NULL) {
150 return;
151 }
152 if(!ORCHESTRA_UNICAST_SENDER_BASED) {
153 /* Packets to this address were marked with this slotframe and neighbor-specific timeslot;
154 * make sure they don't remain stuck in the queues after the link is removed. */
156 }
157
158 /* Does our current parent need this timeslot? */
159 if(timeslot == get_node_timeslot(&orchestra_parent_linkaddr)) {
160 /* Yes, this timeslot is being used, return */
161 return;
162 }
163 /* Does any other child need this timeslot?
164 * (lookup all route next hops) */
165 nbr_table_item_t *item = nbr_table_head(nbr_routes);
166 while(item != NULL) {
167 linkaddr_t *addr = nbr_table_get_lladdr(nbr_routes, item);
168 if(timeslot == get_node_timeslot(addr)) {
169 /* Yes, this timeslot is being used, return */
170 return;
171 }
172 item = nbr_table_next(nbr_routes, item);
173 }
174
175 /* Do we need this timeslot? */
176 if(timeslot == get_node_timeslot(&linkaddr_node_addr)) {
177 /* This is our link, keep it but update the link options */
178 uint8_t link_options = ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX;
179 tsch_schedule_add_link(sf_unicast, link_options, LINK_TYPE_NORMAL, &tsch_broadcast_address,
180 timeslot, local_channel_offset, 1);
181 } else {
182 /* Remove link */
183 tsch_schedule_remove_link(sf_unicast, l);
184 }
185}
186/*---------------------------------------------------------------------------*/
187static void
188child_added(const linkaddr_t *linkaddr)
189{
190 add_uc_link(linkaddr);
191}
192/*---------------------------------------------------------------------------*/
193static void
194child_removed(const linkaddr_t *linkaddr)
195{
196 remove_uc_link(linkaddr);
197}
198/*---------------------------------------------------------------------------*/
199static int
200select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
201{
202 /* Select data packets we have a unicast link to */
203 const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
204 if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
205 && !orchestra_is_root_schedule_active(dest)
206 && neighbor_has_uc_link(dest)) {
207 if(slotframe != NULL) {
208 *slotframe = slotframe_handle;
209 }
210 if(timeslot != NULL) {
211 *timeslot = ORCHESTRA_UNICAST_SENDER_BASED ? get_node_timeslot(&linkaddr_node_addr) : get_node_timeslot(dest);
212 }
213 /* set per-packet channel offset */
214 if(channel_offset != NULL) {
215 *channel_offset = get_node_channel_offset(dest);
216 }
217 return 1;
218 }
219 return 0;
220}
221/*---------------------------------------------------------------------------*/
222static void
223new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
224{
225 if(new != old) {
226 const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
227 const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
228 if(new_addr != NULL) {
229 linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
230 } else {
231 linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
232 }
233 remove_uc_link(old_addr);
234 add_uc_link(new_addr);
235 }
236}
237/*---------------------------------------------------------------------------*/
238static void
239init(uint16_t sf_handle)
240{
241 uint16_t timeslot;
242 linkaddr_t *local_addr = &linkaddr_node_addr;
243
244 slotframe_handle = sf_handle;
245 local_channel_offset = get_node_channel_offset(local_addr);
246 /* Slotframe for unicast transmissions */
247 sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
248 timeslot = get_node_timeslot(local_addr);
249 tsch_schedule_add_link(sf_unicast,
250 ORCHESTRA_UNICAST_SENDER_BASED ? LINK_OPTION_TX | UNICAST_SLOT_SHARED_FLAG: LINK_OPTION_RX,
251 LINK_TYPE_NORMAL, &tsch_broadcast_address,
252 timeslot, local_channel_offset, 1);
253}
254/*---------------------------------------------------------------------------*/
255struct orchestra_rule unicast_per_neighbor_rpl_storing = {
256 init,
257 new_time_source,
258 select_packet,
259 child_added,
260 child_removed,
261 NULL,
262 NULL,
263 "unicast per neighbor storing",
264 ORCHESTRA_UNICAST_PERIOD,
265};
266
267#endif /* UIP_MAX_ROUTES */
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition linkaddr.c:63
bool linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition linkaddr.c:69
const linkaddr_t linkaddr_null
The null link-layer address.
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.
struct tsch_link * tsch_schedule_get_link_by_offsets(struct tsch_slotframe *slotframe, uint16_t timeslot, uint16_t channel_offset)
Looks within a slotframe for a link with a given timeslot and channel offset.
void tsch_queue_free_packets_to(const linkaddr_t *addr)
Flush packets to a specific address.
Definition tsch-queue.c:324
struct tsch_slotframe * tsch_schedule_add_slotframe(uint16_t handle, uint16_t size)
Creates and adds a new slotframe.
linkaddr_t * tsch_queue_get_nbr_address(const struct tsch_neighbor *n)
Get the address of a neighbor.
Definition tsch-queue.c:135
int tsch_schedule_remove_link(struct tsch_slotframe *slotframe, struct tsch_link *l)
Removes a link.
Orchestra header file.
Header file for the Packet buffer (packetbuf) management.
Routing driver header file.
TSCH neighbor information.
Definition tsch-types.h:109
802.15.4e slotframe (contains links)
Definition tsch-types.h:84
Header file for routing table manipulation.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107