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