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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9// Network topology
10//
11// n0 n1
12// +---------+ +--------+
13// | UDP | r | UDP |
14// +---------+ +---------+--------+ +--------+
15// | IPv6 | | IPv6 | IPv6 | | IPv6 |
16// +---------+ +---------+ | | |
17// | 6LoWPAN | | 6LoWPAN | | | |
18// +---------+ +---------+--------+ +--------+
19// | CSMA | | CSMA | CSMA | | CSMA |
20// +---------+ +---------+--------+ +--------+
21// | | | |
22// ================ =================
23//
24// - Tracing of queues and packet receptions to file "example-sixlowpan.tr"
25// Note that the Pcap packet dissection will not be very meaningful.
26// See the module's documentation for a discussion about this.
27
28#include "ns3/core-module.h"
29#include "ns3/csma-module.h"
30#include "ns3/internet-apps-module.h"
31#include "ns3/internet-module.h"
32#include "ns3/sixlowpan-module.h"
33
34#include <fstream>
35
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE("ExampleSixlowpan");
39
40int
41main(int argc, char** argv)
42{
43 bool verbose = false;
44
45 CommandLine cmd(__FILE__);
46 cmd.AddValue("verbose", "turn on some relevant log components", verbose);
47 cmd.Parse(argc, argv);
48
49 if (verbose)
50 {
51 LogComponentEnable("SixLowPanNetDevice", LOG_LEVEL_ALL);
53 }
54
57
58 NS_LOG_INFO("Create nodes.");
62
63 NodeContainer net1(n0, r);
64 NodeContainer net2(r, n1);
65 NodeContainer all(n0, r, n1);
66
67 NS_LOG_INFO("Create IPv6 Internet Stack");
68 InternetStackHelper internetv6;
69 internetv6.Install(all);
70
71 NS_LOG_INFO("Create channels.");
73 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
74 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
75 NetDeviceContainer d2 = csma.Install(net2);
76 csma.SetDeviceAttribute("Mtu", UintegerValue(150));
77 NetDeviceContainer d1 = csma.Install(net1);
78
79 SixLowPanHelper sixlowpan;
80 NetDeviceContainer six1 = sixlowpan.Install(d1);
81
82 NS_LOG_INFO("Create networks and assign IPv6 Addresses.");
84 ipv6.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
85 Ipv6InterfaceContainer i1 = ipv6.Assign(six1);
86 i1.SetForwarding(1, true);
88
89 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
90 Ipv6InterfaceContainer i2 = ipv6.Assign(d2);
91 i2.SetForwarding(0, true);
93
94 /* Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r */
95 uint32_t packetSize = 200;
96 uint32_t maxPacketCount = 50;
97 Time interPacketInterval = Seconds(1.);
98 PingHelper ping(i2.GetAddress(1, 1));
99
100 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
101 ping.SetAttribute("Interval", TimeValue(interPacketInterval));
102 ping.SetAttribute("Size", UintegerValue(packetSize));
103 ApplicationContainer apps = ping.Install(net1.Get(0));
104
105 apps.Start(Seconds(5.0));
106 apps.Stop(Seconds(15.0));
107
108 AsciiTraceHelper ascii;
109 csma.EnableAsciiAll(ascii.CreateFileStream("example-sixlowpan.tr"));
110 csma.EnablePcapAll(std::string("example-sixlowpan"), true);
111
113 NS_LOG_INFO("Run Simulation.");
116 NS_LOG_INFO("Done.");
117
118 return 0;
119}
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.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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.
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.
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:592
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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:94
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
bool verbose
static const uint32_t packetSize
Packet size generated at the AP.