# SDN Architecture for 6 LoWPAN Wireless Sensor Networks
## 4 Implementation of SD6 WSN on the Contiki Platform
### Software Architecture  
Contiki [31] is an open source operating system specially developed for networked embedded systems, which provides an implementation of the IEEE802.15.4/6 LoWPAN/RPL/CoAP stack for use in lossy networks with devices with little processing capacity and memory. Integration between the SD6 WSN agent and Contiki takes place through the API provided by the operating system. The Contiki code is organized in a directory tree as shown in Figure 7. The \"/core/net/\" directory contains the link layer handling functions, including link quality metrics such as ETX calculation and RSSI measurements. The \"/core/net/rpl\" directory contains the code that implements RPL and \"/core/net/ipv6\" the code for IPv6 and 6 LoWPAN. The directory ER-CoAP contains the default implementation of the ER-CoAP server. The \"Platform\" and \"CPU\" directories contain the specific functions for building firmware for the supported notes. Device drivers, such as radios and Ethernet adapters, are stored in the \"Dev\" directory. The agent is implemented by the program \"SD6 WSN-Agent.c\", which integrates with the ER-CoAP server through function calls. It implements the functions that handle SD6 WSNP messages  
Figure 6: Packet forwarding procedure. Figure 7: Directory tree of Contiki O.S. (called resources). SD6 WSNP messages are addressed to specific resources according to their URIs. In addition to implementing control plane messaging, the ER-CoAP server is responsible for handling the notifications. The time between two consecutive notifications is parameterizable and must be set so as to avoid a significant impact on the total traffic of the WSN. Figure 8 illustrates the integration between agent, ER-CoAP, resources and Contiki core libraries. One can also observe the ping probe component, whose function is to make Contiki calculate the ETX metric for every link between neighbor nodes. There is a mechanism for active link quality testing, called \"RPL Probing\", which generates RPL control messages, encapsulated in ICMPv6 and transmitted in unicast mode, causing the update of ETX values in the corresponding links. However, this mechanism only sends messages to the preferred parents, leaving the other links without an update. The ping probe mechanism of the SD6 WSN agent periodically sends ICMPv6 echo-request messages (the same used by ping) causing Contiki to return echo-reply messages and the consequent update of ETX in all links involved. As mentioned before, the routing of SD6 WSNP messages inside the 6 LoWPAN-WSN is performed by RPL, but the routing of IPv6 packets to the controller and other network elements outside 6 LoWPAN-WSN is performed by the standard TCP/IP stack. RPL routing on SD6 WSN nodes is performed by the native Contiki RPL libraries, located in /core/net/rpl, and by the \"border-router.c\" application within 6 LBR, which also uses the Contiki libraries to implement the 6 LBR functions. Packet forwarding logic is in the \"core/net/ip\" directory tree, especially in the \"tcpip.c\" program. We changed the packet processing flow by inserting the code snippet shown in Figure 9, which inspects the header fields of the incoming packet. If it is a UDP packet (Line 3), the source and destination ports are identified (Lines 4 and 5), and the get-next-hop-by-flow (Line 6) function (implemented in the agent) is called with the data of the header fields as arguments. The agent returns the IPv6 address of the next-hop. The function get-next-hop-by-flow implements the forwarding logic. First, it looks up the entry that best matches the packet header in the flow-table. If no entry matches the packet-header, a packet-in notification is sent. Then, the function returns the IPv6 address depending on the action present in the matching flow table entry. It returns NULL when the packet is a control plane message or when it matches an entry with a Type 3 action (see Table 2), forcing RPL to compute the next-hop address.