Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 1 | Examples |
| 2 | ======== |
| 3 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame^] | 4 | .. _simple-scenario: |
| 5 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 6 | Simple scenario |
| 7 | --------------- |
| 8 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 9 | The first example (``ndn-simple.cc``) shows very basics of ndnSIM. In the simulated |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 10 | topology there are 3 nodes, connected with point-to-point links, one |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 11 | NDN consumer, and one NDN producer: |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 13 | .. aafig:: |
| 14 | :aspect: 60 |
| 15 | :scale: 120 |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 16 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 17 | +----------+ +--------+ +----------+ |
| 18 | | | 1Mbps | | 1Mbps | | |
| 19 | | Consumer |<-------------->| Router |<-------------->| Producer | |
| 20 | | | 10ms | | 10ms | | |
| 21 | +----------+ +--------+ +----------+ |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 23 | Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 24 | with frequency of 10 Interests per second (see :doc:`applications`). |
| 25 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 26 | Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 27 | |
| 28 | FIB on every node is populated using default routes (see :doc:`helpers`). |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 29 | |
| 30 | The following code represents all that is necessary to run such a |
| 31 | simple 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 40 | using namespace ns3; |
| 41 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 42 | int |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 43 | 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame^] | 50 | // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize |
| 51 | CommandLine cmd; |
| 52 | cmd.Parse (argc, argv); |
| 53 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 54 | // Creating nodes |
| 55 | NodeContainer nodes; |
| 56 | nodes.Create (3); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 58 | // 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 63 | // Install CCNx stack on all nodes |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 64 | ndn::StackHelper ccnxHelper; |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 65 | ccnxHelper.SetDefaultRoutes (true); |
| 66 | ccnxHelper.InstallAll (); |
| 67 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 68 | // Installing applications |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 69 | |
| 70 | // Consumer |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 71 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 72 | // 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 77 | // Producer |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 78 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 79 | // 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 83 | |
| 84 | Simulator::Stop (Seconds (20.0)); |
| 85 | |
| 86 | Simulator::Run (); |
| 87 | Simulator::Destroy (); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 92 | If 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 93 | simulation using the following command (in optimized mode nothing will be printed out):: |
| 94 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 95 | NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 96 | |
| 97 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame^] | 98 | .. _9-node-grid-example: |
| 99 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 100 | 9-node grid example |
| 101 | ------------------- |
| 102 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 103 | This scenario (``ndn-grid.cc``) simulates using a grid topology build with PointToPointGrid NS-3 module |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 104 | |
| 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 Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 126 | FIB is populated using :ndnsim:`GlobalRoutingHelper` (see :doc:`helpers`). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 127 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 128 | Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 129 | with frequency of 10 Interests per second (see :doc:`applications`). |
| 130 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 131 | Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 132 | |
| 133 | |
| 134 | The following code represents all that is necessary to run such a |
| 135 | simple 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 Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame^] | 155 | // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize |
| 156 | CommandLine cmd; |
| 157 | cmd.Parse (argc, argv); |
| 158 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 159 | // 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 Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 165 | ndn::StackHelper ccnxHelper; |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 166 | ccnxHelper.InstallAll (); |
| 167 | |
| 168 | // Installing global routing interface on all nodes |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 169 | ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper; |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 170 | 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 Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 180 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 181 | consumerHelper.SetPrefix (prefix); |
| 182 | consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second |
| 183 | consumerHelper.Install (consumerNodes); |
| 184 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 185 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 186 | producerHelper.SetPrefix (prefix); |
| 187 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 188 | producerHelper.Install (producer); |
| 189 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 190 | // Add /prefix origins to ndn::GlobalRouter |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 191 | ccnxGlobalRoutingHelper.AddOrigins (prefix, producer); |
| 192 | |
| 193 | // Calculate and install FIBs |
| 194 | ccnxGlobalRoutingHelper.CalculateRoutes (); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 195 | |
| 196 | Simulator::Stop (Seconds (20.0)); |
| 197 | |
| 198 | Simulator::Run (); |
| 199 | Simulator::Destroy (); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 204 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 205 | If 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 Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 206 | simulation using the following command (in optimized mode nothing will be printed out):: |
| 207 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 208 | NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-grid |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 209 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame^] | 210 | .. _9-node-grid-example-using-topology-plugin: |
| 211 | |
| 212 | 9-node grid example using topology plugin |
| 213 | ----------------------------------------- |
| 214 | |
| 215 | Instead 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 | |
| 217 | While :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. |
| 218 | The 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 | |
| 263 | If 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 | |
| 323 | As you can see, scenario code became more compact and more readable. |
| 324 | |
| 325 | :ndnsim:`AnnotatedTopologyReader` provides two ways to access topology nodes. |
| 326 | First, you can use the method :ndnsim:`AnnotatedTopologyReader::GetNodes` which returns NodeContainer. |
| 327 | |
| 328 | Alternatively, nodes can be accessed by name using `Names::Find<Node> ("nodename")` call, as in the above example. |
| 329 | For this purpose,:ndnsim:`AnnotatedTopologyReader` automatically registers all created nodes with names specified in topology file. |
| 330 | For more information about `Names` class, please refer to `NS-3 documentation <.. http://www.nsnam.org/doxygen/classns3_1_1_names.html>`_ |
| 331 | . |