A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
example-sixlowpan.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Universita' di Firenze, Italy
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20// Network topology
21//
22// n0 n1
23// +---------+ +--------+
24// | UDP | r | UDP |
25// +---------+ +---------+--------+ +--------+
26// | IPv6 | | IPv6 | IPv6 | | IPv6 |
27// +---------+ +---------+ | | |
28// | 6LoWPAN | | 6LoWPAN | | | |
29// +---------+ +---------+--------+ +--------+
30// | CSMA | | CSMA | CSMA | | CSMA |
31// +---------+ +---------+--------+ +--------+
32// | | | |
33// ================ =================
34//
35// - Tracing of queues and packet receptions to file "example-sixlowpan.tr"
36// Note that the Pcap packet dissection will not be very meaningful.
37// See the module's documentation for a discussion about this.
38
39#include "ns3/core-module.h"
40#include "ns3/csma-module.h"
41#include "ns3/internet-apps-module.h"
42#include "ns3/internet-module.h"
43#include "ns3/sixlowpan-module.h"
44
45#include <fstream>
46
47using namespace ns3;
48
49NS_LOG_COMPONENT_DEFINE("ExampleSixlowpan");
50
51int
52main(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);
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 NodeContainer net1(n0, r);
75 NodeContainer net2(r, n1);
76 NodeContainer all(n0, r, n1);
77
78 NS_LOG_INFO("Create IPv6 Internet Stack");
79 InternetStackHelper internetv6;
80 internetv6.Install(all);
81
82 NS_LOG_INFO("Create channels.");
84 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
85 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
86 NetDeviceContainer d2 = csma.Install(net2);
87 csma.SetDeviceAttribute("Mtu", UintegerValue(150));
88 NetDeviceContainer d1 = csma.Install(net1);
89
90 SixLowPanHelper sixlowpan;
91 NetDeviceContainer six1 = sixlowpan.Install(d1);
92
93 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
95 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
96 Ipv6InterfaceContainer i1 = ipv6.Assign(six1);
97 i1.SetForwarding(1, true);
99
100 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
101 Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
102 i2.SetForwarding(0, true);
104
105 /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */
106 uint32_t packetSize = 200;
107 uint32_t maxPacketCount = 50;
108 Time interPacketInterval = Seconds(1.);
109 PingHelper ping(i2.GetAddress(1, 1));
110
111 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
112 ping.SetAttribute("Interval", TimeValue(interPacketInterval));
113 ping.SetAttribute("Size", UintegerValue(packetSize));
114 ApplicationContainer apps = ping.Install(net1.Get(0));
115
116 apps.Start(Seconds(5.0));
117 apps.Stop(Seconds(15.0));
118
119 AsciiTraceHelper ascii;
120 csma.EnableAsciiAll(ascii.CreateFileStream("example-sixlowpan.tr"));
121 csma.EnablePcapAll(std::string("example-sixlowpan"), true);
122
124 NS_LOG_INFO("Run Simulation.");
127 NS_LOG_INFO("Done.");
128
129 return 0;
130}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
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:174
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.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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:49
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:455
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
static void EnableChecking()
Enable packets metadata checking.
Definition: packet.cc:603
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
Create a ping application and associate it to a node.
Definition: ping-helper.h:42
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
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:105
AttributeValue implementation for Time.
Definition: nstime.h:1406
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.