Contiki-NG
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
51#include "sys/log.h"
52#define LOG_MODULE "Orchestra"
53#define LOG_LEVEL LOG_LEVEL_MAC
54
55/* A net-layer sniffer for packets sent and received */
56static void orchestra_packet_received(void);
57static void orchestra_packet_sent(int mac_status);
58NETSTACK_SNIFFER(orchestra_sniffer, orchestra_packet_received, orchestra_packet_sent);
59
60/* The current RPL preferred parent's link-layer address */
61linkaddr_t orchestra_parent_linkaddr;
62/* Set to one only after getting an ACK for a DAO sent to our preferred parent */
63int orchestra_parent_knows_us = 0;
64
65/* The set of Orchestra rules in use */
66const struct orchestra_rule *all_rules[] = ORCHESTRA_RULES;
67#define NUM_RULES (sizeof(all_rules) / sizeof(struct orchestra_rule *))
68
69/*---------------------------------------------------------------------------*/
70static void
71orchestra_packet_received(void)
72{
73}
74/*---------------------------------------------------------------------------*/
75static void
76orchestra_packet_sent(int mac_status)
77{
78 /* Check if our parent just ACKed a DAO */
79 if(orchestra_parent_knows_us == 0
80 && mac_status == MAC_TX_OK
81 && packetbuf_attr(PACKETBUF_ATTR_NETWORK_ID) == UIP_PROTO_ICMP6
82 && packetbuf_attr(PACKETBUF_ATTR_CHANNEL) == (ICMP6_RPL << 8 | RPL_CODE_DAO)) {
83 if(!linkaddr_cmp(&orchestra_parent_linkaddr, &linkaddr_null)
84 && linkaddr_cmp(&orchestra_parent_linkaddr, packetbuf_addr(PACKETBUF_ADDR_RECEIVER))) {
85 orchestra_parent_knows_us = 1;
86 }
87 }
88}
89/*---------------------------------------------------------------------------*/
90void
91orchestra_callback_child_added(const linkaddr_t *addr)
92{
93 /* Notify all Orchestra rules that a child was added */
94 int i;
95 for(i = 0; i < NUM_RULES; i++) {
96 if(all_rules[i]->child_added != NULL) {
97 all_rules[i]->child_added(addr);
98 }
99 }
100}
101/*---------------------------------------------------------------------------*/
102void
103orchestra_callback_child_removed(const linkaddr_t *addr)
104{
105 /* Notify all Orchestra rules that a child was removed */
106 int i;
107 for(i = 0; i < NUM_RULES; i++) {
108 if(all_rules[i]->child_removed != NULL) {
109 all_rules[i]->child_removed(addr);
110 }
111 }
112}
113/*---------------------------------------------------------------------------*/
114void
115orchestra_callback_neighbor_updated(const linkaddr_t *addr, uint8_t is_added)
116{
117 /* Notify all Orchestra rules that a neighbor was added or removed */
118 int i;
119 for(i = 0; i < NUM_RULES; i++) {
120 if(all_rules[i]->neighbor_updated != NULL) {
121 all_rules[i]->neighbor_updated(addr, is_added);
122 }
123 }
124}
125/*---------------------------------------------------------------------------*/
126int
127orchestra_callback_packet_ready(void)
128{
129 int i;
130 /* By default, use any slotframe, any timeslot */
131 uint16_t slotframe = 0xffff;
132 uint16_t timeslot = 0xffff;
133 /* The default channel offset 0xffff means that the channel offset in the scheduled
134 * tsch_link structure is used instead. Any other value specified in the packetbuf
135 * overrides per-link value, allowing to implement multi-channel Orchestra. */
136 uint16_t channel_offset = 0xffff;
137 int matched_rule = -1;
138
139 /* Loop over all rules until finding one able to handle the packet */
140 for(i = 0; i < NUM_RULES; i++) {
141 if(all_rules[i]->select_packet != NULL) {
142 if(all_rules[i]->select_packet(&slotframe, &timeslot, &channel_offset)) {
143 matched_rule = i;
144 break;
145 }
146 }
147 }
148
149#if TSCH_WITH_LINK_SELECTOR
150 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_SLOTFRAME, slotframe);
151 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_TIMESLOT, timeslot);
152 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_CHANNEL_OFFSET, channel_offset);
153#endif
154
155 return matched_rule;
156}
157/*---------------------------------------------------------------------------*/
158void
159orchestra_callback_new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
160{
161 /* Orchestra assumes that the time source is also the RPL parent.
162 * This is the case if the following is set:
163 * #define RPL_CALLBACK_PARENT_SWITCH tsch_rpl_callback_parent_switch
164 * */
165
166 int i;
167 if(new != old) {
168 orchestra_parent_knows_us = 0;
169 }
170 for(i = 0; i < NUM_RULES; i++) {
171 if(all_rules[i]->new_time_source != NULL) {
172 all_rules[i]->new_time_source(old, new);
173 }
174 }
175}
176/*---------------------------------------------------------------------------*/
177void
178orchestra_callback_root_node_updated(const linkaddr_t *root, uint8_t is_added)
179{
180 int i;
181
182 for(i = 0; i < NUM_RULES; i++) {
183 if(all_rules[i]->root_node_updated != NULL) {
184 all_rules[i]->root_node_updated(root, is_added);
185 }
186 }
187}
188/*---------------------------------------------------------------------------*/
189void
190orchestra_init(void)
191{
192 int i;
193 /* Snoop on packet transmission to know if our parent knows about us
194 * (i.e. has ACKed at one of our DAOs since we decided to use it as a parent) */
195 netstack_sniffer_add(&orchestra_sniffer);
196 linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
197 /* Initialize all Orchestra rules */
198 for(i = 0; i < NUM_RULES; i++) {
199 LOG_INFO("Initializing rule %s (%u), size %d\n", all_rules[i]->name, i, all_rules[i]->slotframe_size);
200 if(all_rules[i]->init != NULL) {
201 all_rules[i]->init(i);
202 }
203 }
204 LOG_INFO("Initialization done\n");
205}
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:87
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