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/*---------------------------------------------------------------------------*/
114int
115orchestra_callback_packet_ready(void)
116{
117 int i;
118 /* By default, use any slotframe, any timeslot */
119 uint16_t slotframe = 0xffff;
120 uint16_t timeslot = 0xffff;
121 /* The default channel offset 0xffff means that the channel offset in the scheduled
122 * tsch_link structure is used instead. Any other value specified in the packetbuf
123 * overrides per-link value, allowing to implement multi-channel Orchestra. */
124 uint16_t channel_offset = 0xffff;
125 int matched_rule = -1;
126
127 /* Loop over all rules until finding one able to handle the packet */
128 for(i = 0; i < NUM_RULES; i++) {
129 if(all_rules[i]->select_packet != NULL) {
130 if(all_rules[i]->select_packet(&slotframe, &timeslot, &channel_offset)) {
131 matched_rule = i;
132 break;
133 }
134 }
135 }
136
137#if TSCH_WITH_LINK_SELECTOR
138 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_SLOTFRAME, slotframe);
139 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_TIMESLOT, timeslot);
140 packetbuf_set_attr(PACKETBUF_ATTR_TSCH_CHANNEL_OFFSET, channel_offset);
141#endif
142
143 return matched_rule;
144}
145/*---------------------------------------------------------------------------*/
146void
147orchestra_callback_new_time_source(const struct tsch_neighbor *old, const struct tsch_neighbor *new)
148{
149 /* Orchestra assumes that the time source is also the RPL parent.
150 * This is the case if the following is set:
151 * #define RPL_CALLBACK_PARENT_SWITCH tsch_rpl_callback_parent_switch
152 * */
153
154 int i;
155 if(new != old) {
156 orchestra_parent_knows_us = 0;
157 }
158 for(i = 0; i < NUM_RULES; i++) {
159 if(all_rules[i]->new_time_source != NULL) {
160 all_rules[i]->new_time_source(old, new);
161 }
162 }
163}
164/*---------------------------------------------------------------------------*/
165void
166orchestra_callback_root_node_updated(const linkaddr_t *root, uint8_t is_added)
167{
168 int i;
169
170 for(i = 0; i < NUM_RULES; i++) {
171 if(all_rules[i]->root_node_updated != NULL) {
172 all_rules[i]->root_node_updated(root, is_added);
173 }
174 }
175}
176/*---------------------------------------------------------------------------*/
177void
178orchestra_init(void)
179{
180 int i;
181 /* Snoop on packet transmission to know if our parent knows about us
182 * (i.e. has ACKed at one of our DAOs since we decided to use it as a parent) */
183 netstack_sniffer_add(&orchestra_sniffer);
184 linkaddr_copy(&orchestra_parent_linkaddr, &linkaddr_null);
185 /* Initialize all Orchestra rules */
186 for(i = 0; i < NUM_RULES; i++) {
187 LOG_INFO("Initializing rule %s (%u), size %d\n", all_rules[i]->name, i, all_rules[i]->slotframe_size);
188 if(all_rules[i]->init != NULL) {
189 all_rules[i]->init(i);
190 }
191 }
192 LOG_INFO("Initialization done\n");
193}
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
const linkaddr_t linkaddr_null
The null link-layer address.
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
#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