Contiki-NG
Loading...
Searching...
No Matches
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
58static uint16_t slotframe_handle = 0;
59static uint16_t local_channel_offset;
60static struct tsch_slotframe *sf_unicast;
61
62/*---------------------------------------------------------------------------*/
63static uint16_t
64get_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/*---------------------------------------------------------------------------*/
73static uint16_t
74get_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/*---------------------------------------------------------------------------*/
84static int
85neighbor_has_uc_link(const linkaddr_t *linkaddr)
86{
87 if(linkaddr == NULL || linkaddr_cmp(linkaddr, &linkaddr_null)) {
88 return 0;
89 }
90
91 if(linkaddr_cmp(&orchestra_parent_linkaddr, linkaddr)) {
92 /* The node is our parent */
93 return orchestra_parent_knows_us ? 1 : 0;
94 }
95
96 if(nbr_table_get_from_lladdr(nbr_routes, (linkaddr_t *)linkaddr) != NULL) {
97 /* We have a route to this node;
98 * it should have selected us as its parent and installed a link */
99 return 1;
100 }
101
102 return 0;
103}
104/*---------------------------------------------------------------------------*/
105static void
106add_uc_links(const linkaddr_t *linkaddr)
107{
108 if(linkaddr != NULL) {
109 uint16_t timeslot_rx = get_node_pair_timeslot(linkaddr, &linkaddr_node_addr);
110 uint16_t timeslot_tx = get_node_pair_timeslot(&linkaddr_node_addr, linkaddr);
111
112 /* Add Tx link */
113 tsch_schedule_add_link(sf_unicast, LINK_OPTION_TX | LINK_OPTION_SHARED, LINK_TYPE_NORMAL, &tsch_broadcast_address,
114 timeslot_tx, local_channel_offset, 0);
115 /* Add Rx link */
116 tsch_schedule_add_link(sf_unicast, LINK_OPTION_RX, LINK_TYPE_NORMAL, &tsch_broadcast_address,
117 timeslot_rx, local_channel_offset, 0);
118
119 }
120}
121/*---------------------------------------------------------------------------*/
122static void
123remove_unicast_link(uint16_t timeslot, uint16_t options)
124{
125 struct tsch_link *l = list_head(sf_unicast->links_list);
126 while(l != NULL) {
127 if(l->timeslot == timeslot
128 && l->channel_offset == local_channel_offset
129 && l->link_options == options) {
130 tsch_schedule_remove_link(sf_unicast, l);
131 break;
132 }
133 l = list_item_next(l);
134 }
135}
136/*---------------------------------------------------------------------------*/
137static void
138remove_uc_links(const linkaddr_t *linkaddr)
139{
140 if(linkaddr != NULL) {
141 uint16_t timeslot_rx = get_node_pair_timeslot(linkaddr, &linkaddr_node_addr);
142 uint16_t timeslot_tx = get_node_pair_timeslot(&linkaddr_node_addr, linkaddr);
143
144 remove_unicast_link(timeslot_rx, LINK_OPTION_RX);
145 remove_unicast_link(timeslot_tx, LINK_OPTION_TX | LINK_OPTION_SHARED);
146
147 /* Packets to this address were marked with this slotframe and neighbor-specific timeslot;
148 * make sure they don't remain stuck in the queues after the link is removed. */
150 }
151}
152/*---------------------------------------------------------------------------*/
153static void
154child_added(const linkaddr_t *linkaddr)
155{
156 add_uc_links(linkaddr);
157}
158/*---------------------------------------------------------------------------*/
159static void
160child_removed(const linkaddr_t *linkaddr)
161{
162 remove_uc_links(linkaddr);
163}
164/*---------------------------------------------------------------------------*/
165static int
166select_packet(uint16_t *slotframe, uint16_t *timeslot, uint16_t *channel_offset)
167{
168 /* Select data packets we have a unicast link to */
169 const linkaddr_t *dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
170 if(packetbuf_attr(PACKETBUF_ATTR_FRAME_TYPE) == FRAME802154_DATAFRAME
171 && !orchestra_is_root_schedule_active(dest)
172 && neighbor_has_uc_link(dest)) {
173 if(slotframe != NULL) {
174 *slotframe = slotframe_handle;
175 }
176 if(timeslot != NULL) {
177 *timeslot = get_node_pair_timeslot(&linkaddr_node_addr, dest);
178 }
179 /* set per-packet channel offset */
180 if(channel_offset != NULL) {
181 *channel_offset = get_node_channel_offset(dest);
182 }
183 return 1;
184 }
185 return 0;
186}
187/*---------------------------------------------------------------------------*/
188static void
189new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
190{
191 if(new != old) {
192 const linkaddr_t *old_addr = tsch_queue_get_nbr_address(old);
193 const linkaddr_t *new_addr = tsch_queue_get_nbr_address(new);
194 if(new_addr != NULL) {
195 linkaddr_copy(&orchestra_parent_linkaddr, new_addr);
196 } else {
197 linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
198 }
199 remove_uc_links(old_addr);
200 add_uc_links(new_addr);
201 }
202}
203/*---------------------------------------------------------------------------*/
204static void
205init(uint16_t sf_handle)
206{
207 slotframe_handle = sf_handle;
208 local_channel_offset = get_node_channel_offset(&linkaddr_node_addr);
209 /* Slotframe for unicast transmissions */
210 sf_unicast = tsch_schedule_add_slotframe(slotframe_handle, ORCHESTRA_UNICAST_PERIOD);
211}
212/*---------------------------------------------------------------------------*/
213struct orchestra_rule unicast_per_neighbor_link_based = {
214 init,
215 new_time_source,
216 select_packet,
217 child_added,
218 child_removed,
219 NULL,
220 NULL,
221 "unicast per neighbor link based",
222 ORCHESTRA_UNICAST_PERIOD,
223};
224
225#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.
static void * list_item_next(const void *item)
Get the next item following this item.
Definition list.h:294
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition list.h:169
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.
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.
TSCH neighbor information.
Definition tsch-types.h:109
802.15.4e slotframe (contains links)
Definition tsch-types.h:84
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107