Contiki-NG
orchestra-rule-unicast-link-based.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Institute of Electronics and Computer Science (EDI)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the Institute nor the names of its contributors
13  * may be used to endorse or promote products derived from this software
14  * without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 /**
30  * \file
31  * Orchestra: a slotframe dedicated to unicast data transmission. Designed for
32  * RPL storing mode only, as this is based on the knowledge of the children (and parent).
33  * This is the link-based mode:
34  * For each nbr in RPL children and RPL preferred parent,
35  * nodes listen at: hash(nbr.MAC, local.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
36  * nodes transmit at: hash(local.MAC, nbr.MAC) % ORCHESTRA_SB_UNICAST_PERIOD
37  * For receiver-based and sender-based modes, see orchestra-rule-unicast-per-neighbor-rpl-storing.c
38  * The Orchestra link-based rule has been designed based on the insights from:
39  * 1) An Empirical Survey of Autonomous Scheduling Methods for TSCH,
40  * Atis Elsts, Seohyang Kim, Hyung-Sin Kim, Chongkwon Kim, IEEE Access, 2020
41  * 2) ALICE: autonomous link-based cell scheduling for TSCH,
42  * Seohyang Kim, Hyung-Sin Kim, Chongkwon Kim, IEEE Access, ACM/IEEE IPSN, 2019.
43  *
44  * \author Atis Elsts <atis.elsts@edi.lv>
45  */
46 
47 #include "contiki.h"
48 #include "orchestra.h"
49 #include "net/packetbuf.h"
50 
51 /*
52  * The body of this rule should be compiled only when "nbr_routes" is available,
53  * otherwise a link error causes build failure. "nbr_routes" is compiled if
54  * UIP_MAX_ROUTES != 0. See uip-ds6-route.c.
55  */
56 #if UIP_MAX_ROUTES != 0
57 
58 static uint16_t slotframe_handle = 0;
59 static uint16_t local_channel_offset;
60 static struct tsch_slotframe *sf_unicast;
61 
62 /*---------------------------------------------------------------------------*/
63 static uint16_t
64 get_node_pair_timeslot(const linkaddr_t *from, const linkaddr_t *to)
65 {
66  if(from != NULL && to != NULL && ORCHESTRA_UNICAST_PERIOD > 0) {
67  return ORCHESTRA_LINKADDR_HASH2(from, to) % ORCHESTRA_UNICAST_PERIOD;
68  } else {
69  return 0xffff;
70  }
71 }
72 /*---------------------------------------------------------------------------*/
73 static uint16_t
74 get_node_channel_offset(const linkaddr_t *addr)
75 {
76  if(addr != NULL && ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET >= ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET) {
77  return ORCHESTRA_LINKADDR_HASH(addr) % (ORCHESTRA_UNICAST_MAX_CHANNEL_OFFSET - ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET + 1)
78  + ORCHESTRA_UNICAST_MIN_CHANNEL_OFFSET;
79  } else {
80  return 0xffff;
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 static int
85 neighbor_has_uc_link(const linkaddr_t *linkaddr)
86 {
87  if(linkaddr != NULL && !linkaddr_cmp(linkaddr, &linkaddr_null)) {
88  if(orchestra_parent_knows_us && linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
89  return 1;
90  }
91  if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
92  return 1;
93  }
94  }
95  return 0;
96 }
97 /*---------------------------------------------------------------------------*/
98 static void
99 add_uc_links(const linkaddr_t *linkaddr)
100 {
101  if(linkaddr != NULL) {
102  uint16_t timeslot_rx = get_node_pair_timeslot(linkaddr, &linkaddr_node_addr);
103  uint16_t timeslot_tx = get_node_pair_timeslot(&linkaddr_node_addr, linkaddr);
104 
105  /* Add Tx link */
106  tsch_schedule_add_link(sf_unicast, LINK_OPTION_TX | LINK_OPTION_SHARED, LINK_TYPE_NORMAL, &tsch_broadcast_address,
107  timeslot_tx, local_channel_offset, 0);
108  /* Add Rx link */
109  tsch_schedule_add_link(sf_unicast, LINK_OPTION_RX, LINK_TYPE_NORMAL, &tsch_broadcast_address,
110  timeslot_rx, local_channel_offset, 0);
111 
112  }
113 }
114 /*---------------------------------------------------------------------------*/
115 static void
116 remove_unicast_link(uint16_t timeslot, uint16_t options)
117 {
118  struct tsch_link *l = list_head(sf_unicast->links_list);
119  while(l != NULL) {
120  if(l->timeslot == timeslot
121  && l->channel_offset == local_channel_offset
122  && l->link_options == options) {
123  tsch_schedule_remove_link(sf_unicast, l);
124  break;
125  }
126  l = list_item_next(l);
127  }
128 }
129 /*---------------------------------------------------------------------------*/
130 static void
131 remove_uc_links(const linkaddr_t *linkaddr)
132 {
133  if(linkaddr != NULL) {
134  uint16_t timeslot_rx = get_node_pair_timeslot(linkaddr, &linkaddr_node_addr);
135  uint16_t timeslot_tx = get_node_pair_timeslot(&linkaddr_node_addr, linkaddr);
136 
137  remove_unicast_link(timeslot_rx, LINK_OPTION_RX);
138  remove_unicast_link(timeslot_tx, LINK_OPTION_TX | LINK_OPTION_SHARED);
139 
140  /* Packets to this address were marked with this slotframe and neighbor-specific timeslot;
141  * make sure they don't remain stuck in the queues after the link is removed. */
142  tsch_queue_free_packets_to(linkaddr);
143  }
144 }
145 /*---------------------------------------------------------------------------*/
146 static void
147 child_added(const linkaddr_t *linkaddr)
148 {
149  add_uc_links(linkaddr);
150 }
151 /*---------------------------------------------------------------------------*/
152 static void
153 child_removed(const linkaddr_t *linkaddr)
154 {
155  remove_uc_links(linkaddr);
156 }
157 /*---------------------------------------------------------------------------*/
158 static int
159 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
160 {
161  /* Select data packets we have a unicast link to */
162  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
163  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
164  && !orchestra_is_root_schedule_active(dest)
165  && neighbor_has_uc_link(dest)) {
166  if(slotframe != NULL) {
167  *slotframe = slotframe_handle;
168  }
169  if(timeslot != NULL) {
170  *timeslot = get_node_pair_timeslot(&linkaddr_node_addr, dest);
171  }
172  /* set per-packet channel offset */
173  if(channel_offset != NULL) {
174  *channel_offset = get_node_channel_offset(dest);
175  }
176  return 1;
177  }
178  return 0;
179 }
180 /*---------------------------------------------------------------------------*/
181 static void
182 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
183 {
184  if(new != old) {
185  const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
186  const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
187  if(new_addr != NULL) {
188  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
189  } else {
190  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
191  }
192  remove_uc_links(old_addr);
193  add_uc_links(new_addr);
194  }
195 }
196 /*---------------------------------------------------------------------------*/
197 static void
198 init(uint16_t sf_handle)
199 {
200  slotframe_handle = sf_handle;
201  local_channel_offset = get_node_channel_offset(&linkaddr_node_addr);
202  /* Slotframe for unicast transmissions */
203  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
204 }
205 /*---------------------------------------------------------------------------*/
206 struct orchestra_rule unicast_per_neighbor_link_based = {
207  init,
208  new_time_source,
209  select_packet,
210  child_added,
211  child_removed,
212  NULL,
213  "unicast per neighbor link based",
214  ORCHESTRA_UNICAST_PERIOD,
215 };
216 
217 #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
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
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
Header file for the Packet buffer (packetbuf) management
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:322
void tsch_queue_free_packets_to(const linkaddr_t *addr)
Flush packets to a specific address.
Definition: tsch-queue.c:324