A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
topology-example-sim.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
5 * Author: Valerio Sartini <valesar@gmail.com>
6 *
7 * This program conducts a simple experiment: It builds up a topology based on
8 * either Inet or Orbis trace files. A random node is then chosen, and all the
9 * other nodes will send a packet to it. The TTL is measured and reported as an histogram.
10 *
11 */
12
13#include "ns3/applications-module.h"
14#include "ns3/core-module.h"
15#include "ns3/internet-module.h"
16#include "ns3/network-module.h"
17#include "ns3/nix-vector-helper.h"
18#include "ns3/point-to-point-module.h"
19#include "ns3/topology-read-module.h"
20
21#include <ctime>
22#include <list>
23#include <sstream>
24
25/**
26 * \ingroup topology
27 * Example of TopologyReader: read in a topology in a specified format.
28 *
29 * This example can be used with the following parameters:
30 * - <tt>--format=Inet --input=src/topology-read/examples/Inet_small_toposample.txt</tt>
31 * - <tt>--format=Inet --input=src/topology-read/examples/Inet_toposample.txt</tt>
32 * - <tt>--format=Orbis --input=src/topology-read/examples/Orbis_toposample.txt</tt>
33 * - <tt>--format=Rocket
34 * --input=src/topology-read/examples/RocketFuel_sample_4755.r0.cch_maps.txt</tt>
35 * - <tt>--format=Rocket
36 * --input=src/topology-read/examples/RocketFuel_toposample_1239_weights.txt</tt>
37 */
38
39using namespace ns3;
40
41NS_LOG_COMPONENT_DEFINE("TopologyCreationExperiment");
42
43/**
44 * Print the TTL of received packet
45 * \param p received packet
46 * \param ad sender address
47 */
48static void
50{
51 Ipv4Header ipv4;
52 p->PeekHeader(ipv4);
53 std::cout << "TTL: " << (unsigned)ipv4.GetTtl() << std::endl;
54}
55
56// ----------------------------------------------------------------------
57// -- main
58// ----------------------------------------------
59int
60main(int argc, char* argv[])
61{
62 std::string format("Inet");
63 std::string input("src/topology-read/examples/Inet_small_toposample.txt");
64
65 LogComponentEnable("TopologyCreationExperiment", LOG_LEVEL_INFO);
66
67 // Set up command line parameters used to control the experiment.
68 CommandLine cmd(__FILE__);
69 cmd.AddValue("format", "Format to use for data input [Orbis|Inet|Rocketfuel].", format);
70 cmd.AddValue("input", "Name of the input file.", input);
71 cmd.Parse(argc, argv);
72
73 // ------------------------------------------------------------
74 // -- Read topology data.
75 // --------------------------------------------
76
77 // Pick a topology reader based in the requested format.
78 TopologyReaderHelper topoHelp;
79 topoHelp.SetFileName(input);
80 topoHelp.SetFileType(format);
81 Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader();
82
84
85 if (inFile)
86 {
87 nodes = inFile->Read();
88 }
89
90 if (inFile->LinksSize() == 0)
91 {
92 NS_LOG_ERROR("Problems reading the topology file. Failing.");
93 return -1;
94 }
95
96 // ------------------------------------------------------------
97 // -- Create nodes and network stacks
98 // --------------------------------------------
99 NS_LOG_INFO("creating internet stack");
101
102 // Setup NixVector Routing
104 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
105 stack.Install(nodes);
106
107 NS_LOG_INFO("creating IPv4 addresses");
109 address.SetBase("10.0.0.0", "255.255.255.252");
110
111 int totlinks = inFile->LinksSize();
112
113 NS_LOG_INFO("creating node containers");
114 auto nc = new NodeContainer[totlinks];
116 int i = 0;
117 for (iter = inFile->LinksBegin(); iter != inFile->LinksEnd(); iter++, i++)
118 {
119 nc[i] = NodeContainer(iter->GetFromNode(), iter->GetToNode());
120 }
121
122 NS_LOG_INFO("creating net device containers");
123 auto ndc = new NetDeviceContainer[totlinks];
125 for (int i = 0; i < totlinks; i++)
126 {
127 // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
128 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
129 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
130 ndc[i] = p2p.Install(nc[i]);
131 }
132
133 // it crates little subnets, one for each couple of nodes.
134 NS_LOG_INFO("creating IPv4 interfaces");
135 auto ipic = new Ipv4InterfaceContainer[totlinks];
136 for (int i = 0; i < totlinks; i++)
137 {
138 ipic[i] = address.Assign(ndc[i]);
139 address.NewNetwork();
140 }
141
142 uint32_t totalNodes = nodes.GetN();
144 unifRandom->SetAttribute("Min", DoubleValue(0));
145 unifRandom->SetAttribute("Max", DoubleValue(totalNodes - 1));
146
147 unsigned int randomServerNumber = unifRandom->GetInteger(0, totalNodes - 1);
148
149 Ptr<Node> randomServerNode = nodes.Get(randomServerNumber);
150 Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4>();
151 Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress(1, 0);
152 Ipv4Address ipv4AddrServer = iaddrServer.GetLocal();
153
154 // ------------------------------------------------------------
155 // -- Send around packets to check the ttl
156 // --------------------------------------------
157 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
158 InetSocketAddress dst(ipv4AddrServer);
159
160 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
161 onoff.SetConstantRate(DataRate(15000));
162 onoff.SetAttribute("PacketSize", UintegerValue(1200));
163
164 NodeContainer clientNodes;
165 for (unsigned int i = 0; i < nodes.GetN(); i++)
166 {
167 if (i != randomServerNumber)
168 {
169 Ptr<Node> clientNode = nodes.Get(i);
170 clientNodes.Add(clientNode);
171 }
172 }
173
174 ApplicationContainer apps = onoff.Install(clientNodes);
175 apps.Start(Seconds(1.0));
176 apps.Stop(Seconds(2.0));
177
178 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
179 apps = sink.Install(randomServerNode);
180 apps.Start(Seconds(0.0));
181 apps.Stop(Seconds(3.0));
182
183 // we trap the packet sink receiver to extract the TTL.
184 Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
186
187 // ------------------------------------------------------------
188 // -- Run the simulation
189 // --------------------------------------------
190 NS_LOG_INFO("Run Simulation.");
193
194 delete[] ipic;
195 delete[] ndc;
196 delete[] nc;
197
198 NS_LOG_INFO("Done.");
199
200 return 0;
201
202 // end main
203}
a polymophic address class
Definition address.h:90
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.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Packet header for IPv4.
Definition ipv4-header.h:23
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
Helper class that adds Nix-vector routing to nodes.
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
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
Hold variables of type string.
Definition string.h:45
Helper class which makes it easier to configure and use a generic TopologyReader.
void SetFileType(const std::string fileType)
Sets the input file type.
Ptr< TopologyReader > GetTopologyReader()
Gets a Ptr<TopologyReader> to the actual TopologyReader.
void SetFileName(const std::string fileName)
Sets the input file name.
std::list< Link >::const_iterator ConstLinksIterator
Constant iterator to the list of the links.
Hold an unsigned integer type.
Definition uinteger.h:34
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#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
NodeContainer nodes
address
Definition first.py:36
stack
Definition first.py:33
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
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Print the TTL of received packet.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44