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 }
141 /*---------------------------------------------------------------------------*/
142 static void
143 child_added(const linkaddr_t *linkaddr)
144 {
145  add_uc_links(linkaddr);
146 }
147 /*---------------------------------------------------------------------------*/
148 static void
149 child_removed(const linkaddr_t *linkaddr)
150 {
151  remove_uc_links(linkaddr);
152 }
153 /*---------------------------------------------------------------------------*/
154 static int
155 select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
156 {
157  /* Select data packets we have a unicast link to */
158  const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
159  if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
160  && neighbor_has_uc_link(dest)) {
161  if(slotframe != NULL) {
162  *slotframe = slotframe_handle;
163  }
164  if(timeslot != NULL) {
165  *timeslot = get_node_pair_timeslot(&linkaddr_node_addr, dest);
166  }
167  /* set per-packet channel offset */
168  if(channel_offset != NULL) {
169  *channel_offset = get_node_channel_offset(dest);
170  }
171  return 1;
172  }
173  return 0;
174 }
175 /*---------------------------------------------------------------------------*/
176 static void
177 new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
178 {
179  if(new != old) {
180  const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
181  const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
182  if(new_addr != NULL) {
183  linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
184  } else {
185  linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
186  }
187  remove_uc_links(old_addr);
188  add_uc_links(new_addr);
189  }
190 }
191 /*---------------------------------------------------------------------------*/
192 static void
193 init(uint16_t sf_handle)
194 {
195  slotframe_handle = sf_handle;
196  local_channel_offset = get_node_channel_offset(&linkaddr_node_addr);
197  /* Slotframe for unicast transmissions */
198  sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
199 }
200 /*---------------------------------------------------------------------------*/
201 struct orchestra_rule unicast_per_neighbor_link_based = {
202  init,
203  new_time_source,
204  select_packet,
205  child_added,
206  child_removed,
207  "unicast per neighbor link based",
208  ORCHESTRA_UNICAST_PERIOD,
209 };
210 
211 #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