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