A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-static-routing-helper.h"
35 #include "ns3/ipv4-list-routing-helper.h"
36 #include "ns3/ipv4-nix-vector-helper.h"
37 
38 #include "ns3/topology-read-module.h"
39 #include <list>
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("TopologyCreationExperiment");
44 
45 
46 static std::list<unsigned int> data;
47 
48 static void SinkRx (Ptr<const Packet> p, const Address &ad)
49 {
50  Ipv4Header ipv4;
51  p->PeekHeader (ipv4);
52  std::cout << "TTL: " << (unsigned)ipv4.GetTtl () << std::endl;
53 }
54 
55 
56 // ----------------------------------------------------------------------
57 // -- main
58 // ----------------------------------------------
59 int main (int argc, char *argv[])
60 {
61 
62  std::string format ("Inet");
63  std::string input ("src/topology-read/examples/Inet_small_toposample.txt");
64 
65  // Set up command line parameters used to control the experiment.
66  CommandLine cmd;
67  cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
68  format);
69  cmd.AddValue ("input", "Name of the input file.",
70  input);
71  cmd.Parse (argc, argv);
72 
73 
74  // ------------------------------------------------------------
75  // -- Read topology data.
76  // --------------------------------------------
77 
78  // Pick a topology reader based in the requested format.
79 
80  Ptr<TopologyReader> inFile = 0;
81  TopologyReaderHelper topoHelp;
82 
83  NodeContainer nodes;
84 
85  topoHelp.SetFileName (input);
86  topoHelp.SetFileType (format);
87  inFile = topoHelp.GetTopologyReader ();
88 
89  if (inFile != 0)
90  {
91  nodes = inFile->Read ();
92  }
93 
94  if (inFile->LinksSize () == 0)
95  {
96  NS_LOG_ERROR ("Problems reading the topology file. Failing.");
97  return -1;
98  }
99 
100  // ------------------------------------------------------------
101  // -- Create nodes and network stacks
102  // --------------------------------------------
103  NS_LOG_INFO ("creating internet stack");
104  InternetStackHelper stack;
105 
106  // Setup NixVector Routing
107  Ipv4NixVectorHelper nixRouting;
108  Ipv4StaticRoutingHelper staticRouting;
109 
110  Ipv4ListRoutingHelper listRH;
111  listRH.Add (staticRouting, 0);
112  listRH.Add (nixRouting, 10);
113 
114  stack.SetRoutingHelper (listRH); // has effect on the next Install ()
115  stack.Install (nodes);
116 
117  NS_LOG_INFO ("creating ip4 addresses");
118  Ipv4AddressHelper address;
119  address.SetBase ("10.0.0.0", "255.255.255.252");
120 
121  int totlinks = inFile->LinksSize ();
122 
123  NS_LOG_INFO ("creating node containers");
124  NodeContainer* nc = new NodeContainer[totlinks];
126  int i = 0;
127  for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
128  {
129  nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
130  }
131 
132  NS_LOG_INFO ("creating net device containers");
133  NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
134  PointToPointHelper p2p;
135  for (int i = 0; i < totlinks; i++)
136  {
137  // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
138  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
139  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
140  ndc[i] = p2p.Install (nc[i]);
141  }
142 
143  // it crates little subnets, one for each couple of nodes.
144  NS_LOG_INFO ("creating ipv4 interfaces");
145  Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
146  for (int i = 0; i < totlinks; i++)
147  {
148  ipic[i] = address.Assign (ndc[i]);
149  address.NewNetwork ();
150  }
151 
152 
153  uint32_t totalNodes = nodes.GetN ();
154  Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable> ();
155  unifRandom->SetAttribute ("Min", DoubleValue (0));
156  unifRandom->SetAttribute ("Max", DoubleValue (totalNodes - 1));
157 
158  unsigned int randomServerNumber = unifRandom->GetInteger (0, totalNodes - 1);
159 
160  Ptr<Node> randomServerNode = nodes.Get (randomServerNumber);
161  Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4> ();
162  Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress (1,0);
163  Ipv4Address ipv4AddrServer = iaddrServer.GetLocal ();
164 
165  // ------------------------------------------------------------
166  // -- Send around packets to check the ttl
167  // --------------------------------------------
168  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
169  InetSocketAddress dst = InetSocketAddress ( ipv4AddrServer );
170 
171  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
172  onoff.SetConstantRate (DataRate (15000));
173  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
174 
175  NodeContainer clientNodes;
176  for ( unsigned int i = 0; i < nodes.GetN (); i++ )
177  {
178  if (i != randomServerNumber )
179  {
180  Ptr<Node> clientNode = nodes.Get (i);
181  clientNodes.Add (clientNode);
182  }
183  }
184 
185  ApplicationContainer apps = onoff.Install (clientNodes);
186  apps.Start (Seconds (1.0));
187  apps.Stop (Seconds (2.0));
188 
189  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
190  apps = sink.Install (randomServerNode);
191  apps.Start (Seconds (0.0));
192  apps.Stop (Seconds (3.0));
193 
194  // we trap the packet sink receiver to extract the TTL.
195  Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
196  MakeCallback (&SinkRx));
197 
198  // ------------------------------------------------------------
199  // -- Run the simulation
200  // --------------------------------------------
201  NS_LOG_INFO ("Run Simulation.");
202  Simulator::Run ();
204 
205  delete[] ipic;
206  delete[] ndc;
207  delete[] nc;
208 
209  NS_LOG_INFO ("Done.");
210 
211  return 0;
212 
213  // end main
214 }