A Discrete-Event Network Simulator
API
topology-example-sim.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
17  * Author: Valerio Sartini <valesar@gmail.com>
18  *
19  * This program conducts a simple experiment: It builds up a topology based on
20  * either Inet or Orbis trace files. A random node is then chosen, and all the
21  * other nodes will send a packet to it. The TTL is measured and reported as an histogram.
22  *
23  */
24 
25 #include <ctime>
26 
27 #include <sstream>
28 
29 #include "ns3/core-module.h"
30 #include "ns3/network-module.h"
31 #include "ns3/internet-module.h"
32 #include "ns3/point-to-point-module.h"
33 #include "ns3/applications-module.h"
34 #include "ns3/ipv4-nix-vector-helper.h"
35 
36 #include "ns3/topology-read-module.h"
37 #include <list>
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE ("TopologyCreationExperiment");
42 
43 static std::list<unsigned int> data;
44 
45 static void SinkRx (Ptr<const Packet> p, const Address &ad)
46 {
47  Ipv4Header ipv4;
48  p->PeekHeader (ipv4);
49  std::cout << "TTL: " << (unsigned)ipv4.GetTtl () << std::endl;
50 }
51 
52 
53 // ----------------------------------------------------------------------
54 // -- main
55 // ----------------------------------------------
56 int main (int argc, char *argv[])
57 {
58 
59  std::string format ("Inet");
60  std::string input ("src/topology-read/examples/Inet_small_toposample.txt");
61 
62  // Set up command line parameters used to control the experiment.
64  cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
65  format);
66  cmd.AddValue ("input", "Name of the input file.",
67  input);
68  cmd.Parse (argc, argv);
69 
70 
71  // ------------------------------------------------------------
72  // -- Read topology data.
73  // --------------------------------------------
74 
75  // Pick a topology reader based in the requested format.
76  TopologyReaderHelper topoHelp;
77  topoHelp.SetFileName (input);
78  topoHelp.SetFileType (format);
79  Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader ();
80 
82 
83  if (inFile != 0)
84  {
85  nodes = inFile->Read ();
86  }
87 
88  if (inFile->LinksSize () == 0)
89  {
90  NS_LOG_ERROR ("Problems reading the topology file. Failing.");
91  return -1;
92  }
93 
94  // ------------------------------------------------------------
95  // -- Create nodes and network stacks
96  // --------------------------------------------
97  NS_LOG_INFO ("creating internet stack");
99 
100  // Setup NixVector Routing
101  Ipv4NixVectorHelper nixRouting;
102  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
103  stack.Install (nodes);
104 
105  NS_LOG_INFO ("creating ip4 addresses");
107  address.SetBase ("10.0.0.0", "255.255.255.252");
108 
109  int totlinks = inFile->LinksSize ();
110 
111  NS_LOG_INFO ("creating node containers");
112  NodeContainer* nc = new NodeContainer[totlinks];
114  int i = 0;
115  for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
116  {
117  nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
118  }
119 
120  NS_LOG_INFO ("creating net device containers");
121  NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
122  PointToPointHelper p2p;
123  for (int i = 0; i < totlinks; i++)
124  {
125  // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
126  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
127  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
128  ndc[i] = p2p.Install (nc[i]);
129  }
130 
131  // it crates little subnets, one for each couple of nodes.
132  NS_LOG_INFO ("creating ipv4 interfaces");
133  Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
134  for (int i = 0; i < totlinks; i++)
135  {
136  ipic[i] = address.Assign (ndc[i]);
137  address.NewNetwork ();
138  }
139 
140 
141  uint32_t totalNodes = nodes.GetN ();
142  Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable> ();
143  unifRandom->SetAttribute ("Min", DoubleValue (0));
144  unifRandom->SetAttribute ("Max", DoubleValue (totalNodes - 1));
145 
146  unsigned int randomServerNumber = unifRandom->GetInteger (0, totalNodes - 1);
147 
148  Ptr<Node> randomServerNode = nodes.Get (randomServerNumber);
149  Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4> ();
150  Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress (1,0);
151  Ipv4Address ipv4AddrServer = iaddrServer.GetLocal ();
152 
153  // ------------------------------------------------------------
154  // -- Send around packets to check the ttl
155  // --------------------------------------------
156  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
157  InetSocketAddress dst = InetSocketAddress ( ipv4AddrServer );
158 
159  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
160  onoff.SetConstantRate (DataRate (15000));
161  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
162 
163  NodeContainer clientNodes;
164  for ( unsigned int i = 0; i < nodes.GetN (); i++ )
165  {
166  if (i != randomServerNumber )
167  {
168  Ptr<Node> clientNode = nodes.Get (i);
169  clientNodes.Add (clientNode);
170  }
171  }
172 
173  ApplicationContainer apps = onoff.Install (clientNodes);
174  apps.Start (Seconds (1.0));
175  apps.Stop (Seconds (2.0));
176 
177  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
178  apps = sink.Install (randomServerNode);
179  apps.Start (Seconds (0.0));
180  apps.Stop (Seconds (3.0));
181 
182  // we trap the packet sink receiver to extract the TTL.
183  Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
184  MakeCallback (&SinkRx));
185 
186  // ------------------------------------------------------------
187  // -- Run the simulation
188  // --------------------------------------------
189  NS_LOG_INFO ("Run Simulation.");
190  Simulator::Run ();
192 
193  delete[] ipic;
194  delete[] ndc;
195  delete[] nc;
196 
197  NS_LOG_INFO ("Done.");
198 
199  return 0;
200 
201  // end main
202 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
an Inet address class
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Helper class that adds Nix-vector routing to nodes.
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Build a set of PointToPointNetDevice objects.
void SetFileName(const std::string fileName)
Sets the input file name.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:34
a polymophic address class
Definition: address.h:90
Ptr< TopologyReader > GetTopologyReader()
Gets a Ptr<TopologyReader> to the actual TopologyReader.
Class for representing data rates.
Definition: data-rate.h:88
Packet header for IPv4.
Definition: ipv4-header.h:33
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
nodes
Definition: first.py:25
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Hold an unsigned integer type.
Definition: uinteger.h:44
ConstLinksIterator LinksEnd(void) const
Returns an iterator to the the last link in this block.
holds a vector of ns3::NetDevice pointers
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:832
void SetFileType(const std::string fileType)
Sets the input file type.
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:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
int LinksSize(void) const
Returns the number of links in this block.
std::list< Link >::const_iterator ConstLinksIterator
Constant iterator to the list of the links.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
address
Definition: first.py:37
static std::list< unsigned int > data
uint8_t GetTtl(void) const
Definition: ipv4-header.cc:265
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Helper class which makes it easier to configure and use a generic TopologyReader. ...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
a class to store IPv4 address information on an interface
virtual Ipv4InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
virtual NodeContainer Read(void)=0
Main topology reading function.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
ConstLinksIterator LinksBegin(void) const
Returns an iterator to the the first link in this block.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.