A Discrete-Event Network Simulator
API
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/internet-apps-module.h"
46 #include "ns3/sixlowpan-module.h"
47 
48 using namespace ns3;
49 
50 NS_LOG_COMPONENT_DEFINE ("ExampleSixlowpan");
51 
52 int main (int argc, char** argv)
53 {
54  bool verbose = false;
55 
56  CommandLine cmd (__FILE__);
57  cmd.AddValue ("verbose", "turn on some relevant log components", verbose);
58  cmd.Parse (argc, argv);
59 
60  if (verbose)
61  {
62  LogComponentEnable ("SixLowPanNetDevice", LOG_LEVEL_ALL);
63  LogComponentEnable ("Ping6Application", LOG_LEVEL_ALL);
64  }
65 
68 
69  NS_LOG_INFO ("Create nodes.");
70  Ptr<Node> n0 = CreateObject<Node> ();
71  Ptr<Node> r = CreateObject<Node> ();
72  Ptr<Node> n1 = CreateObject<Node> ();
73 
74 
75  NodeContainer net1 (n0, r);
76  NodeContainer net2 (r, n1);
77  NodeContainer all (n0, r, n1);
78 
79  NS_LOG_INFO ("Create IPv6 Internet Stack");
80  InternetStackHelper internetv6;
81  internetv6.Install (all);
82 
83  NS_LOG_INFO ("Create channels.");
85  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
86  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
87  NetDeviceContainer d2 = csma.Install (net2);
88  csma.SetDeviceAttribute ("Mtu", UintegerValue (150));
89  NetDeviceContainer d1 = csma.Install (net1);
90 
91  SixLowPanHelper sixlowpan;
92  sixlowpan.SetDeviceAttribute ("ForceEtherType", BooleanValue (true) );
93  NetDeviceContainer six1 = sixlowpan.Install (d1);
94 
95  NS_LOG_INFO ("Create networks and assign IPv6 Addresses.");
96  Ipv6AddressHelper ipv6;
97  ipv6.SetBase (Ipv6Address ("2001:1::"), Ipv6Prefix (64));
98  Ipv6InterfaceContainer i1 = ipv6.Assign (six1);
99  i1.SetForwarding (1, true);
101 
102  ipv6.SetBase (Ipv6Address ("2001:2::"), Ipv6Prefix (64));
103  Ipv6InterfaceContainer i2 = ipv6.Assign (d2);
104  i2.SetForwarding (0, true);
106 
107  /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */
108  uint32_t packetSize = 200;
109  uint32_t maxPacketCount = 50;
110  Time interPacketInterval = Seconds (1.);
111  Ping6Helper ping6;
112 
113  ping6.SetLocal (i1.GetAddress (0, 1));
114  ping6.SetRemote (i2.GetAddress (1, 1));
115 
116  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
117  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
118  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
119  ApplicationContainer apps = ping6.Install (net1.Get (0));
120 
121  apps.Start (Seconds (5.0));
122  apps.Stop (Seconds (15.0));
123 
124 
125  AsciiTraceHelper ascii;
126  csma.EnableAsciiAll (ascii.CreateFileStream ("example-sixlowpan.tr"));
127  csma.EnablePcapAll (std::string ("example-sixlowpan"), true);
128 
129  Simulator::Stop (Seconds (100));
130  NS_LOG_INFO ("Run Simulation.");
131  Simulator::Run ();
133  NS_LOG_INFO ("Done.");
134 }
135 
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:50
Keep track of a set of IPv6 interfaces.
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.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
static void EnableChecking(void)
Enable packets metadata checking.
Definition: packet.cc:579
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Ping6 application helper.
Definition: ping6-helper.h:39
void SetRemote(Ipv6Address ip)
Set the remote IPv6 address.
Definition: ping6-helper.cc:40
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
void SetLocal(Ipv6Address ip)
Set the local IPv6 address.
Definition: ping6-helper.cc:35
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Set an attribute on each ns3::SixlowpanNetDevice created by SixlowpanHelper::Install.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
csma
Definition: second.py:63
cmd
Definition: second.py:35
bool verbose
static const uint32_t packetSize