Contiki-NG
Loading...
Searching...
No Matches
orchestra.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/**
32 * \file
33 * Orchestra: an autonomous scheduler for TSCH exploiting RPL state.
34 * See "Orchestra: Robust Mesh Networks Through Autonomously Scheduled TSCH", ACM SenSys'15
35 *
36 * \author Simon Duquennoy <simonduq@sics.se>
37 */
38
39#include "contiki.h"
40#include "orchestra.h"
41#include "net/packetbuf.h"
42#include "net/ipv6/uip-icmp6.h"
43#include "net/routing/routing.h"
44#if ROUTING_CONF_RPL_LITE
45#include "net/routing/rpl-lite/rpl.h"
46#elif ROUTING_CONF_RPL_CLASSIC
47#include "net/routing/rpl-classic/rpl.h"
48#include "net/routing/rpl-classic/rpl-private.h"
49#endif
50#include "sys/cc.h"
51
52#include "sys/log.h"
53#define LOG_MODULE "Orchestra"
54#define LOG_LEVEL LOG_LEVEL_MAC
55
56/* A net-layer sniffer for packets sent and received */
57static void orchestra_packet_received(void);
58static void orchestra_packet_sent(int mac_status);
59NETSTACK_SNIFFER(orchestra_sniffer, orchestra_packet_received, orchestra_packet_sent);
60
61/* The current RPL preferred parent's link-layer address */
62linkaddr_t orchestra_parent_linkaddr;
63/* Set to one only after getting an ACK for a DAO sent to our preferred parent */
64int orchestra_parent_knows_us = 0;
65
66/* The set of Orchestra rules in use */
67const struct orchestra_rule *all_rules[] = ORCHESTRA_RULES;
68#define NUM_RULES CC_ARRAY_LENGTH(all_rules)
69
70/*---------------------------------------------------------------------------*/
71static void
72orchestra_packet_received(void)
73{
74}
75/*---------------------------------------------------------------------------*/
76static void
77orchestra_packet_sent(int mac_status)
78{
79 /* Check if our parent just ACKed a DAO */
80 if(orchestra_parent_knows_us == 0
81 && mac_status == MAC_TX_OK
82 && packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID) == UIP_PROTO_ICMP6
83 && packetbuf_attr(PACKETBUF_ATTR_CHANNEL) == (ICMP6_RPL << 8 | RPL_CODE_DAO)) {
84 if(!linkaddr_cmp(&orchestra_parent_linkaddr, &linkaddr_null)
85 && linkaddr_cmp(&orchestra_parent_linkaddr, packetbuf_addr(PACKETBUF_ADDR_RECEIVER))) {
86 orchestra_parent_knows_us = 1;
87 }
88 }
89}
90/*---------------------------------------------------------------------------*/
91void
92orchestra_callback_child_added(const linkaddr_t *addr)
93{
94 /* Notify all Orchestra rules that a child was added */
95 int i;
96 for(i = 0; i < NUM_RULES; i++) {
97 if(all_rules[i]->child_added != NULL) {
98 all_rules[i]->child_added(addr);
99 }
100 }
101}
102/*---------------------------------------------------------------------------*/
103void
104orchestra_callback_child_removed(const linkaddr_t *addr)
105{
106 /* Notify all Orchestra rules that a child was removed */
107 int i;
108 for(i = 0; i < NUM_RULES; i++) {
109 if(all_rules[i]->child_removed != NULL) {
110 all_rules[i]->child_removed(addr);
111 }
112 }
113}
114/*---------------------------------------------------------------------------*/
115void
116orchestra_callback_neighbor_updated(const linkaddr_t *addr, uint8_t is_added)
117{
118 /* Notify all Orchestra rules that a neighbor was added or removed */
119 int i;
120 for(i = 0; i < NUM_RULES; i++) {
121 if(all_rules[i]->neighbor_updated != NULL) {
122 all_rules[i]->neighbor_updated(addr, is_added);
123 }
124 }
125}
126/*---------------------------------------------------------------------------*/
127int
128orchestra_callback_packet_ready(void)
129{
130 int i;
131 /* By default, use any slotframe, any timeslot */
132 uint16_t slotframe = 0xffff;
133 uint16_t timeslot = 0xffff;
134 /* The default channel offset 0xffff means that the channel offset in the scheduled
135 * tsch_link structure is used instead. Any other value specified in the packetbuf
136 * overrides per-link value, allowing to implement multi-channel Orchestra. */
137 uint16_t channel_offset = 0xffff;
138 int matched_rule = -1;
139
140 /* Loop over all rules until finding one able to handle the packet */
141 for(i = 0; i < NUM_RULES; i++) {
142 if(all_rules[i]->select_packet != NULL) {
143 if(all_rules[i]->select_packet(&slotframe, &timeslot, &channel_offset)) {
144 matched_rule = i;
145 break;
146 }
147 }
148 }
149
150#if TSCH_WITH_LINK_SELECTOR
151 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_SLOTFRAME, slotframe);
152 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_TIMESLOT, timeslot);
153 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_CHANNEL_OFFSET, channel_offset);
154#endif
155
156 return matched_rule;
157}
158/*---------------------------------------------------------------------------*/
159void
160orchestra_callback_new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
161{
162 /* Orchestra assumes that the time source is also the RPL parent.
163 * This is the case if the following is set:
164 * #define RPL_CALLBACK_PARENT_SWITCH tsch_rpl_callback_parent_switch
165 * */
166
167 int i;
168 if(new != old) {
169 orchestra_parent_knows_us = 0;
170 }
171 for(i = 0; i < NUM_RULES; i++) {
172 if(all_rules[i]->new_time_source != NULL) {
173 all_rules[i]->new_time_source(old, new);
174 }
175 }
176}
177/*---------------------------------------------------------------------------*/
178void
179orchestra_callback_root_node_updated(const linkaddr_t *root, uint8_t is_added)
180{
181 int i;
182
183 for(i = 0; i < NUM_RULES; i++) {
184 if(all_rules[i]->root_node_updated != NULL) {
185 all_rules[i]->root_node_updated(root, is_added);
186 }
187 }
188}
189/*---------------------------------------------------------------------------*/
190void
191orchestra_init(void)
192{
193 int i;
194 /* Snoop on packet transmission to know if our parent knows about us
195 * (i.e. has ACKed at one of our DAOs since we decided to use it as a parent) */
196 netstack_sniffer_add(&orchestra_sniffer);
197 linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
198 /* Initialize all Orchestra rules */
199 for(i = 0; i < NUM_RULES; i++) {
200 LOG_INFO("Initializing rule %s (%u), size %d\n", all_rules[i]->name, i, all_rules[i]->slotframe_size);
201 if(all_rules[i]->init != NULL) {
202 all_rules[i]->init(i);
203 }
204 }
205 LOG_INFO("Initialization done\n");
206}
Default definitions of C compiler quirk work-arounds.
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.
#define ICMP6_RPL
RPL.
Definition uip-icmp6.h:66
Header file for the logging system.
@ MAC_TX_OK
The MAC layer transmission was OK.
Definition mac.h:93
Orchestra header file.
Header file for the Packet buffer (packetbuf) management.
Routing driver header file.
TSCH neighbor information.
Definition tsch-types.h:109
Header file for ICMPv6 message and error handing (RFC 4443).
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition uip-nd6.c:107