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