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 
45 // Document the available input files
67 using namespace ns3;
68 
69 NS_LOG_COMPONENT_DEFINE ("TopologyCreationExperiment");
70 
71 static std::list<unsigned int> data;
72 
73 static void SinkRx (Ptr<const Packet> p, const Address &ad)
74 {
75  Ipv4Header ipv4;
76  p->PeekHeader (ipv4);
77  std::cout << "TTL: " << (unsigned)ipv4.GetTtl () << std::endl;
78 }
79 
80 
81 // ----------------------------------------------------------------------
82 // -- main
83 // ----------------------------------------------
84 int main (int argc, char *argv[])
85 {
86 
87  std::string format ("Inet");
88  std::string input ("src/topology-read/examples/Inet_small_toposample.txt");
89 
90  // Set up command line parameters used to control the experiment.
91  CommandLine cmd (__FILE__);
92  cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
93  format);
94  cmd.AddValue ("input", "Name of the input file.",
95  input);
96  cmd.Parse (argc, argv);
97 
98 
99  // ------------------------------------------------------------
100  // -- Read topology data.
101  // --------------------------------------------
102 
103  // Pick a topology reader based in the requested format.
104  TopologyReaderHelper topoHelp;
105  topoHelp.SetFileName (input);
106  topoHelp.SetFileType (format);
107  Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader ();
108 
110 
111  if (inFile != 0)
112  {
113  nodes = inFile->Read ();
114  }
115 
116  if (inFile->LinksSize () == 0)
117  {
118  NS_LOG_ERROR ("Problems reading the topology file. Failing.");
119  return -1;
120  }
121 
122  // ------------------------------------------------------------
123  // -- Create nodes and network stacks
124  // --------------------------------------------
125  NS_LOG_INFO ("creating internet stack");
127 
128  // Setup NixVector Routing
129  Ipv4NixVectorHelper nixRouting;
130  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
131  stack.Install (nodes);
132 
133  NS_LOG_INFO ("creating ip4 addresses");
135  address.SetBase ("10.0.0.0", "255.255.255.252");
136 
137  int totlinks = inFile->LinksSize ();
138 
139  NS_LOG_INFO ("creating node containers");
140  NodeContainer* nc = new NodeContainer[totlinks];
142  int i = 0;
143  for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
144  {
145  nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
146  }
147 
148  NS_LOG_INFO ("creating net device containers");
149  NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
150  PointToPointHelper p2p;
151  for (int i = 0; i < totlinks; i++)
152  {
153  // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
154  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
155  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
156  ndc[i] = p2p.Install (nc[i]);
157  }
158 
159  // it crates little subnets, one for each couple of nodes.
160  NS_LOG_INFO ("creating ipv4 interfaces");
161  Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
162  for (int i = 0; i < totlinks; i++)
163  {
164  ipic[i] = address.Assign (ndc[i]);
165  address.NewNetwork ();
166  }
167 
168 
169  uint32_t totalNodes = nodes.GetN ();
170  Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable> ();
171  unifRandom->SetAttribute ("Min", DoubleValue (0));
172  unifRandom->SetAttribute ("Max", DoubleValue (totalNodes - 1));
173 
174  unsigned int randomServerNumber = unifRandom->GetInteger (0, totalNodes - 1);
175 
176  Ptr<Node> randomServerNode = nodes.Get (randomServerNumber);
177  Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4> ();
178  Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress (1,0);
179  Ipv4Address ipv4AddrServer = iaddrServer.GetLocal ();
180 
181  // ------------------------------------------------------------
182  // -- Send around packets to check the ttl
183  // --------------------------------------------
184  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
185  InetSocketAddress dst = InetSocketAddress ( ipv4AddrServer );
186 
187  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
188  onoff.SetConstantRate (DataRate (15000));
189  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
190 
191  NodeContainer clientNodes;
192  for ( unsigned int i = 0; i < nodes.GetN (); i++ )
193  {
194  if (i != randomServerNumber )
195  {
196  Ptr<Node> clientNode = nodes.Get (i);
197  clientNodes.Add (clientNode);
198  }
199  }
200 
201  ApplicationContainer apps = onoff.Install (clientNodes);
202  apps.Start (Seconds (1.0));
203  apps.Stop (Seconds (2.0));
204 
205  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
206  apps = sink.Install (randomServerNode);
207  apps.Start (Seconds (0.0));
208  apps.Stop (Seconds (3.0));
209 
210  // we trap the packet sink receiver to extract the TTL.
211  Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
212  MakeCallback (&SinkRx));
213 
214  // ------------------------------------------------------------
215  // -- Run the simulation
216  // --------------------------------------------
217  NS_LOG_INFO ("Run Simulation.");
218  Simulator::Run ();
220 
221  delete[] ipic;
222  delete[] ndc;
223  delete[] nc;
224 
225  NS_LOG_INFO ("Done.");
226 
227  return 0;
228 
229  // end main
230 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
an Inet address class
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
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:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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:281
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:41
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...
nodes
Definition: first.py:32
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
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:899
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:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
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:470
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:44
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:41
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:1278
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
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:257
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.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.