A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
example-sixlowpan.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 
22 // Network topology
23 //
24 // n0 n1
25 // +---------+ +--------+
26 // | UDP | r | UDP |
27 // +---------+ +---------+--------+ +--------+
28 // | IPv6 | | IPv6 | IPv6 | | IPv6 |
29 // +---------+ +---------+ | | |
30 // | 6LoWPAN | | 6LoWPAN | | | |
31 // +---------+ +---------+--------+ +--------+
32 // | CSMA | | CSMA | CSMA | | CSMA |
33 // +---------+ +---------+--------+ +--------+
34 // | | | |
35 // ================ =================
36 //
37 // - Tracing of queues and packet receptions to file "example-sixlowpan.tr"
38 // Note that the Pcap packet dissection will not be very meaningful.
39 // See the module's documentation for a discussion about this.
40 
41 #include <fstream>
42 #include "ns3/core-module.h"
43 #include "ns3/internet-module.h"
44 #include "ns3/csma-module.h"
45 #include "ns3/applications-module.h"
46 #include "ns3/ipv6-static-routing-helper.h"
47 
48 #include "ns3/ipv6-routing-table-entry.h"
49 #include "ns3/sixlowpan-module.h"
50 
51 using namespace ns3;
52 
53 NS_LOG_COMPONENT_DEFINE ("ExampleSixlowpan")
54  ;
55 
56 int main (int argc, char** argv)
57 {
58  bool verbose = false;
59 
60  CommandLine cmd;
61  cmd.AddValue ("verbose", "turn on some relevant log components", verbose);
62  cmd.Parse (argc, argv);
63 
64  if (verbose)
65  {
66  LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL);
67  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
68  }
69 
72 
73  NS_LOG_INFO ("Create nodes.");
74  Ptr<Node> n0 = CreateObject<Node> ();
75  Ptr<Node> r = CreateObject<Node> ();
76  Ptr<Node> n1 = CreateObject<Node> ();
77 
78 
79  NodeContainer net1 (n0, r);
80  NodeContainer net2 (r, n1);
81  NodeContainer all (n0, r, n1);
82 
83  NS_LOG_INFO ("Create IPv6 Internet Stack");
84  InternetStackHelper internetv6;
85  internetv6.Install (all);
86 
87  NS_LOG_INFO ("Create channels.");
88  CsmaHelper csma;
89  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
90  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
91  NetDeviceContainer d2 = csma.Install (net2);
92  csma.SetDeviceAttribute ("Mtu", UintegerValue (150));
93  NetDeviceContainer d1 = csma.Install (net1);
94 
95  SixLowPanHelper sixlowpan;
96  sixlowpan.SetDeviceAttribute ("ForceEtherType", BooleanValue (true) );
97  NetDeviceContainer six1 = sixlowpan.Install (d1);
98 
99  NS_LOG_INFO ("Create networks and assign IPv6 Addresses.");
100  Ipv6AddressHelper ipv6;
101  ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
102  Ipv6InterfaceContainer i1 = ipv6.Assign (six1);
103  i1.SetForwarding (1, true);
105 
106  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
107  Ipv6InterfaceContainer i2 = ipv6.Assign (d2);
108  i2.SetForwarding (0, true);
110 
111  /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */
112  uint32_t packetSize = 200;
113  uint32_t maxPacketCount = 50;
114  Time interPacketInterval = Seconds (1.);
115  Ping6Helper ping6;
116 
117  ping6.SetLocal (i1.GetAddress (0, 1));
118  ping6.SetRemote (i2.GetAddress (1, 1));
119 
120  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
121  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
122  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
123  ApplicationContainer apps = ping6.Install (net1.Get (0));
124 
125  apps.Start (Seconds (5.0));
126  apps.Stop (Seconds (15.0));
127 
128 
129  AsciiTraceHelper ascii;
130  csma.EnableAsciiAll (ascii.CreateFileStream ("example-sixlowpan.tr"));
131  csma.EnablePcapAll (std::string ("example-sixlowpan"), true);
132 
133  Simulator::Stop (Seconds (100));
134  NS_LOG_INFO ("Run Simulation.");
135  Simulator::Run ();
137  NS_LOG_INFO ("Done.");
138 }
139 
holds a vector of ns3::Application pointers.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
Manage ASCII trace files for device models.
Definition: trace-helper.h:128
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Hold a bool native type.
Definition: boolean.h:38
Keep track of a set of IPv6 interfaces.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Definition: log.h:298
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
static void EnablePrinting(void)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition: packet.cc:552
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
hold objects of type ns3::Time
Definition: nstime.h:961
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
static void EnableChecking(void)
The packet metadata is also used to perform extensive sanity checks at runtime when performing operat...
Definition: packet.cc:559
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:46
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Set an attribute on each ns3::SixlowpanNetDevice created by SixlowpanHelper::Install.
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
void Parse(int argc, char *argv[])
Parse the program arguments.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
Ping6 application helper.
Definition: ping6-helper.h:39
int main(int argc, char **argv)
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
bool verbose