blob: d65f4ebc0a0e5b4817defce7f71a73e178096c3e [file] [log] [blame]
Alexander Afanasyev4d325162012-06-01 12:28:50 -07001Examples
2========
3
Alexander Afanasyev6dbacda2012-10-23 17:20:18 -07004.. _simple-scenario:
5
Alexander Afanasyev4d325162012-06-01 12:28:50 -07006Simple scenario
7---------------
8
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -07009The first example (``ndn-simple.cc``) shows very basics of ndnSIM. In the simulated
Alexander Afanasyev4d325162012-06-01 12:28:50 -070010topology there are 3 nodes, connected with point-to-point links, one
Alexander Afanasyev07b00632012-06-01 23:46:47 -070011NDN consumer, and one NDN producer:
Alexander Afanasyev4d325162012-06-01 12:28:50 -070012
Alexander Afanasyev07b00632012-06-01 23:46:47 -070013.. aafig::
14 :aspect: 60
15 :scale: 120
Alexander Afanasyev4d325162012-06-01 12:28:50 -070016
Alexander Afanasyev07b00632012-06-01 23:46:47 -070017 +----------+ +--------+ +----------+
18 | | 1Mbps | | 1Mbps | |
19 | Consumer |<-------------->| Router |<-------------->| Producer |
20 | | 10ms | | 10ms | |
21 +----------+ +--------+ +----------+
Alexander Afanasyev4d325162012-06-01 12:28:50 -070022
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070023Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer
Alexander Afanasyev07b00632012-06-01 23:46:47 -070024with frequency of 10 Interests per second (see :doc:`applications`).
25
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070026Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes).
Alexander Afanasyev07b00632012-06-01 23:46:47 -070027
28FIB on every node is populated using default routes (see :doc:`helpers`).
Alexander Afanasyev4d325162012-06-01 12:28:50 -070029
30The following code represents all that is necessary to run such a
31simple scenario
32
33.. code-block:: c++
34
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/point-to-point-module.h"
38 #include "ns3/ndnSIM-module.h"
Alexander Afanasyev07b00632012-06-01 23:46:47 -070039
Alexander Afanasyev4d325162012-06-01 12:28:50 -070040 using namespace ns3;
41
Alexander Afanasyev07b00632012-06-01 23:46:47 -070042 int
Alexander Afanasyev4d325162012-06-01 12:28:50 -070043 main (int argc, char *argv[])
44 {
45 // setting default parameters for PointToPoint links and channels
46 Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
47 Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
48 Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
Alexander Afanasyev07b00632012-06-01 23:46:47 -070049
Alexander Afanasyev6dbacda2012-10-23 17:20:18 -070050 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
51 CommandLine cmd;
52 cmd.Parse (argc, argv);
53
Alexander Afanasyev4d325162012-06-01 12:28:50 -070054 // Creating nodes
55 NodeContainer nodes;
56 nodes.Create (3);
Alexander Afanasyev07b00632012-06-01 23:46:47 -070057
Alexander Afanasyev4d325162012-06-01 12:28:50 -070058 // Connecting nodes using two links
59 PointToPointHelper p2p;
60 p2p.Install (nodes.Get (0), nodes.Get (1));
61 p2p.Install (nodes.Get (1), nodes.Get (2));
Alexander Afanasyev07b00632012-06-01 23:46:47 -070062
Alexander Afanasyev4d325162012-06-01 12:28:50 -070063 // Install CCNx stack on all nodes
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070064 ndn::StackHelper ccnxHelper;
Alexander Afanasyev4d325162012-06-01 12:28:50 -070065 ccnxHelper.SetDefaultRoutes (true);
66 ccnxHelper.InstallAll ();
67
Alexander Afanasyev07b00632012-06-01 23:46:47 -070068 // Installing applications
Alexander Afanasyev4d325162012-06-01 12:28:50 -070069
70 // Consumer
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070071 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4d325162012-06-01 12:28:50 -070072 // Consumer will request /prefix/0, /prefix/1, ...
73 consumerHelper.SetPrefix ("/prefix");
74 consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
75 consumerHelper.Install (nodes.Get (0)); // first node
Alexander Afanasyev07b00632012-06-01 23:46:47 -070076
Alexander Afanasyev4d325162012-06-01 12:28:50 -070077 // Producer
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070078 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
Alexander Afanasyev4d325162012-06-01 12:28:50 -070079 // Producer will reply to all requests starting with /prefix
80 producerHelper.SetPrefix ("/prefix");
81 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
82 producerHelper.Install (nodes.Get (2)); // last node
Alexander Afanasyev07b00632012-06-01 23:46:47 -070083
84 Simulator::Stop (Seconds (20.0));
85
86 Simulator::Run ();
87 Simulator::Destroy ();
88
89 return 0;
90 }
91
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -070092If this code is placed into ``scratch/ndn-simple.cc`` and NS-3 is compiled in debug mode, you can run and see progress of the
Alexander Afanasyev07b00632012-06-01 23:46:47 -070093simulation using the following command (in optimized mode nothing will be printed out)::
94
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070095 NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple
Alexander Afanasyev07b00632012-06-01 23:46:47 -070096
97
Alexander Afanasyev6dbacda2012-10-23 17:20:18 -070098.. _9-node-grid-example:
99
Alexander Afanasyev07b00632012-06-01 23:46:47 -07001009-node grid example
101-------------------
102
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -0700103This scenario (``ndn-grid.cc``) simulates using a grid topology build with PointToPointGrid NS-3 module
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700104
105.. aafig::
106 :aspect: 60
107 :scale: 120
108
109 /--------\ /-\ /-\
110 |Consumer|<---->| |<------->| |
111 \--------/ \-/ \-/
112 ^ ^ ^
113 | | | 1Mbps/10ms delay
114 v v v
115 /-\ /-\ /-\
116 | |<-------->| |<------->| |
117 \-/ \-/ \-/
118 ^ ^ ^
119 | | |
120 v v v
121 /-\ /-\ /--------\
122 | |<-------->| |<---->|Producer|
123 \-/ \-/ \--------/
124
125
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700126FIB is populated using :ndnsim:`GlobalRoutingHelper` (see :doc:`helpers`).
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700127
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700128Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700129with frequency of 10 Interests per second (see :doc:`applications`).
130
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700131Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes).
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700132
133
134The following code represents all that is necessary to run such a
135simple scenario
136
137.. code-block:: c++
138
139 #include "ns3/core-module.h"
140 #include "ns3/network-module.h"
141 #include "ns3/point-to-point-module.h"
142 #include "ns3/point-to-point-grid.h"
143 #include "ns3/ndnSIM-module.h"
144
145 using namespace ns3;
146
147 int
148 main (int argc, char *argv[])
149 {
150 // Setting default parameters for PointToPoint links and channels
151 Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
152 Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
153 Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
154
Alexander Afanasyev6dbacda2012-10-23 17:20:18 -0700155 // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
156 CommandLine cmd;
157 cmd.Parse (argc, argv);
158
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700159 // Creating 3x3 topology
160 PointToPointHelper p2p;
161 PointToPointGridHelper grid (3, 3, p2p);
162 grid.BoundingBox(100,100,200,200);
163
164 // Install CCNx stack on all nodes
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700165 ndn::StackHelper ccnxHelper;
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700166 ccnxHelper.InstallAll ();
167
168 // Installing global routing interface on all nodes
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700169 ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper;
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700170 ccnxGlobalRoutingHelper.InstallAll ();
171
172 // Getting containers for the consumer/producer
173 Ptr<Node> producer = grid.GetNode (nGrid-1, nGrid-1);
174 NodeContainer consumerNodes;
175 consumerNodes.Add (grid.GetNode (0,0));
176
177 // Install CCNx applications
178 std::string prefix = "/prefix";
179
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700180 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700181 consumerHelper.SetPrefix (prefix);
182 consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
183 consumerHelper.Install (consumerNodes);
184
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700185 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700186 producerHelper.SetPrefix (prefix);
187 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
188 producerHelper.Install (producer);
189
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700190 // Add /prefix origins to ndn::GlobalRouter
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700191 ccnxGlobalRoutingHelper.AddOrigins (prefix, producer);
192
193 // Calculate and install FIBs
194 ccnxGlobalRoutingHelper.CalculateRoutes ();
Alexander Afanasyev4d325162012-06-01 12:28:50 -0700195
196 Simulator::Stop (Seconds (20.0));
197
198 Simulator::Run ();
199 Simulator::Destroy ();
200
201 return 0;
202 }
203
Alexander Afanasyev4d325162012-06-01 12:28:50 -0700204
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -0700205If this code is placed into ``scratch/ndn-grid.cc`` and NS-3 is compiled in debug mode, you can run and see progress of the
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700206simulation using the following command (in optimized mode nothing will be printed out)::
207
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700208 NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-grid
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700209
Alexander Afanasyev6dbacda2012-10-23 17:20:18 -0700210.. _9-node-grid-example-using-topology-plugin:
211
2129-node grid example using topology plugin
213-----------------------------------------
214
215Instead of defining topology directly as in :ref:`simple-scenario` or using specialized helpers as in :ref:`9-node-grid-example`, ndnSIM provides experimental extended versions of TopologyReader classes: :ndnsim:`AnnotatedTopologyReader` and :ndnsim:`RocketfuelWeightsReader`.
216
217While :ndnsim:`RocketfuelWeightsReader` is a specialized version intended to be used with `Rocketfuel <http://www.cs.washington.edu/research/networking/rocketfuel/>`_ topology and link weights files (examples will be provided later), :ndnsim:`AnnotatedTopologyReader` is a general-use tool that allows creation of any custom topologies.
218The based format for the input file the :ndnsim:`AnnotatedTopologyReader` expects::
219
220 # any empty lines and lines starting with '#' symbol is ignored
221
222 # The file should contain exactly two sections: router and link, each starting with the corresponding keyword
223
224 # router section defines topology nodes and their relative positions (e.g., to use in visualizer)
225 router
226
227 # each line in this section represents one router and should have the following data
228 # node comment yPos xPos
229 Node0 NA 3 1
230 Node1 NA 3 2
231 Node2 NA 3 3
232 Node3 NA 2 1
233 Node4 NA 2 2
234 Node5 NA 2 3
235 Node6 NA 1 1
236 Node7 NA 1 2
237 Node8 NA 1 3
238 # Note that `node` can be any string. It is possible to access to the node by name using Names::Find, see examples.
239
240 # link section defines point-to-point links between nodes and characteristics of these links
241 link
242
243 # Each line should be in the following format (only first two are required, the rest can be omitted)
244 # srcNode dstNode bandwidth metric delay queue
245 # bandwidth: link bandwidth
246 # metric: routing metric
247 # delay: link delay
248 # queue: MaxPackets for transmission queue on the link (both directions)
249 Node0 Node1 1Mbps 1 10ms 10
250 Node0 Node3 1Mbps 1 10ms 10
251 Node1 Node2 1Mbps 1 10ms 10
252 Node1 Node4 1Mbps 1 10ms 10
253 Node2 Node5 1Mbps 1 10ms 10
254 Node3 Node4 1Mbps 1 10ms 10
255 Node3 Node6 1Mbps 1 10ms 10
256 Node4 Node5 1Mbps 1 10ms 10
257 Node4 Node7 1Mbps 1 10ms 10
258 Node5 Node8 1Mbps 1 10ms 10
259 Node6 Node7 1Mbps 1 10ms 10
260 Node7 Node8 1Mbps 1 10ms 10
261
262
263If you save the topology file to `topo.txt` in the current directory, then the following code will duplicate the functionality of :ref:`9-node-grid-example` but with the use of :ndnsim:`AnnotatedTopologyReader`:
264
265.. code-block:: c++
266
267 #include "ns3/core-module.h"
268 #include "ns3/network-module.h"
269 #include "ns3/ndnSIM-module.h"
270
271 using namespace ns3;
272
273 int
274 main (int argc, char *argv[])
275 {
276 CommandLine cmd;
277 cmd.Parse (argc, argv);
278
279 AnnotatedTopologyReader topologyReader ("", 25);
280 topologyReader.SetFileName ("topo.txt");
281 topologyReader.Read ();
282
283 // Install CCNx stack on all nodes
284 ndn::StackHelper ccnxHelper;
285 ccnxHelper.InstallAll ();
286
287 // Installing global routing interface on all nodes
288 ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper;
289 ccnxGlobalRoutingHelper.InstallAll ();
290
291 // Getting containers for the consumer/producer
292 Ptr<Node> producer = Names::Find<Node> ("Node8");
293 NodeContainer consumerNodes;
294 consumerNodes.Add (Names::Find<Node> ("Node0"));
295
296 // Install CCNx applications
297 std::string prefix = "/prefix";
298
299 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
300 consumerHelper.SetPrefix (prefix);
301 consumerHelper.SetAttribute ("Frequency", StringValue ("100")); // 100 interests a second
302 consumerHelper.Install (consumerNodes);
303
304 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
305 producerHelper.SetPrefix (prefix);
306 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
307 producerHelper.Install (producer);
308
309 // Add /prefix origins to ndn::GlobalRouter
310 ccnxGlobalRoutingHelper.AddOrigins (prefix, producer);
311
312 // Calculate and install FIBs
313 ccnxGlobalRoutingHelper.CalculateRoutes ();
314
315 Simulator::Stop (Seconds (20.0));
316
317 Simulator::Run ();
318 Simulator::Destroy ();
319
320 return 0;
321 }
322
323As you can see, scenario code became more compact and more readable.
324
325:ndnsim:`AnnotatedTopologyReader` provides two ways to access topology nodes.
326First, you can use the method :ndnsim:`AnnotatedTopologyReader::GetNodes` which returns NodeContainer.
327
328Alternatively, nodes can be accessed by name using `Names::Find<Node> ("nodename")` call, as in the above example.
329For this purpose,:ndnsim:`AnnotatedTopologyReader` automatically registers all created nodes with names specified in topology file.
330For more information about `Names` class, please refer to `NS-3 documentation <.. http://www.nsnam.org/doxygen/classns3_1_1_names.html>`_
331.