ndnSIM learning -- super detailed analysis of ndn-simple.cpp of examples

preface

Since I am still a senior and a beginner of ndnSIM, I will inevitably make many mistakes in the article. I hope beginners don't believe it when they see this article. You are also welcome to correct my article!

code analysis

Firstly, start with the simplest ~ / newndnSIM/ns-3/src/ndnSIM/examples/ndn-simple.cpp to analyze the whole ndnSIM framework. In order to facilitate the reader's comparative analysis, I copy the code here into the article (delete a large section of comments).

// ndn-simple.cpp

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ndnSIM-module.h"

namespace ns3 {

int
main(int argc, char* argv[])
{
  // setting default parameters for PointToPoint links and channels
  Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
  Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
  Config::SetDefault("ns3::QueueBase::MaxSize", StringValue("20p"));

  // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
  CommandLine cmd;
  cmd.Parse(argc, argv);

  // Creating nodes
  NodeContainer nodes;
  nodes.Create(3);

  // Connecting nodes using two links
  PointToPointHelper p2p;
  p2p.Install(nodes.Get(0), nodes.Get(1));
  p2p.Install(nodes.Get(1), nodes.Get(2));

  // Install NDN stack on all nodes
  ndn::StackHelper ndnHelper;
  ndnHelper.SetDefaultRoutes(true);
  ndnHelper.InstallAll();

  // Choosing forwarding strategy
  ndn::StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/multicast");

  // Installing applications

  // Consumer
  ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
  // Consumer will request /prefix/0, /prefix/1, ...
  consumerHelper.SetPrefix("/prefix");
  consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
  auto apps = consumerHelper.Install(nodes.Get(0));                        // first node
  apps.Stop(Seconds(10.0)); // stop the consumer app at 10 seconds mark

  // Producer
  ndn::AppHelper producerHelper("ns3::ndn::Producer");
  // Producer will reply to all requests starting with /prefix
  producerHelper.SetPrefix("/prefix");
  producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
  producerHelper.Install(nodes.Get(2)); // last node

  Simulator::Stop(Seconds(20.0));

  Simulator::Run();
  Simulator::Destroy();

  return 0;
}

} // namespace ns3

int
main(int argc, char* argv[])
{
  return ns3::main(argc, argv);
}

0. Header file

effect

Import header file. The first three are ns-3 files, and the last is ndnSIM files. The source code is as follows——

#Include "NS3 / core module. H" / / core module
#Include "NS3 / network module. H" / / network module
#Include "NS3 / point to point module. H" / / point to point module
#Include "NS3 / ndnsim module. H" / / ndnsim's own module

Detailed analysis

First of all, let's look at the file of include in front. I'm in the article ndnSIM learning (II) -- configuring cross file definition of VScode As mentioned in, the addresses of these files are relative to ~ / newndnSIM/ns-3/build /
, the contents are all the same #inlude.

Among the included libraries, the first three are ns-3 software functions, and the last one is ndnSIM's own. There is only one line #include "ndn-all.hpp", which includes all header files of ndnSIM.

1. Configure network initial parameters

effect

Configure the initial parameters of the network, such as network data transmission rate, channel delay and maximum queue capacity. The source code is as follows——

  // setting default parameters for PointToPoint links and channels
  Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
  Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
  Config::SetDefault("ns3::QueueBase::MaxSize", StringValue("20p"));

Detailed analysis

We can see from the literal meaning that the Config::SetDefault function is used to configure some network initial parameters, which is equivalent to the SetAttribute function of ns-3. Specifically, configure DataRate=1Mbps, Delay=10ms and MaxSize=20p.

Note:

  1. The Config::SetDefault function is used, which is a function of ns-3. For this function, please refer to the link https://www.nsnam.org/doxygen/group__config.html#ga2e7882df849d8ba4aaad31c934c40c06
  2. For the ns3::PointToPoint class, refer to the link https://blog.csdn.net/loongkingwhat/article/details/53455691
  3. For ns3:QueueBase class, refer to the link https://www.nsnam.org/doxygen/classns3_1_1_queue_base.html.

2. Command line parameter parsing

effect

Parse the command line parameters. For example, use the -- vis parameter on the command line to start visualization. The source code is as follows——

  // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
  CommandLine cmd;
  cmd.Parse(argc, argv);

3. Create 3 network nodes

effect

Create 3 network nodes. The source code is as follows——

  // Creating nodes
  NodeContainer nodes;
  nodes.Create(3);

Detailed analysis

There seems to be nothing to say. After all, it's just two lines of code, but some ns-3 syntax needs to be pointed out.

NodeContainer is a vector container containing a smart pointer to the Node class. Specifically, its members are

class NodeContainer
{
public:
  ...  // Omit many operation functions
private:
  std::vector<Ptr<Node> > m_nodes;  // NodeContainer is a real data member
}

std::vector is very familiar to everyone. It is the vector container. It is worth mentioning that Ptr class is a smart pointer class implemented by ns-3 itself 1 , maybe the author wants to force everyone to standardize the code and hand over the memory to intelligent pointer management to prevent memory leakage.

As for the Create function, we can understand it as soon as we look at the implementation in the source code. The passed parameter n represents pushing into the vector container_ back n n n nodes.

// node-container.cc
NodeContainer::Create (uint32_t n)
{
  for (uint32_t i = 0; i < n; i++)
  {
    m_nodes.push_back (CreateObject<Node> ());
  }
}

4. Define network topology

effect

The network topology is defined as node0 - > node1 - > node2. The source code is as follows——

  // Connecting nodes using two links
  PointToPointHelper p2p;
  p2p.Install(nodes.Get(0), nodes.Get(1));
  p2p.Install(nodes.Get(1), nodes.Get(2));

Detailed analysis

A pointtopointhhelper object is created to help manage the whole network. It can be seen from the point to pin helper. H file that the install (PTR < Node > A, PTR < Node > b) function is used to connect two nodes, which is equivalent to defining the topology relationship.

The node.Get(2) operation is equivalent to taking out the second element of the node container and Analogizing it to the array. node.Get(2)==node[2].

5. Add all nodes to the ndn Stack

effect

Add all nodes to the ndn Stack. The source code is as follows——

  // Install NDN stack on all nodes
  ndn::StackHelper ndnHelper;
  ndnHelper.SetDefaultRoutes(true);
  ndnHelper.InstallAll();

Detailed analysis

Ndnhelp. Setdefaultroutes (true) represents FIB(Forwarding Information Base) to start the default route, and ndnhelp. Installall() represents adding all nodes to the ndn Stack.

6. Set Forwarding Policy

effect

Set the Consumer prefix to / prefix (this is doubtful, do not believe it easily), and set the forwarding policy of ndn to multicast. The source code is as follows——

  // Choosing forwarding strategy
  ndn::StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/multicast");

Detailed analysis

The "/ prefix" parameter seems to change the request prefix of the Consumer to / prefix. Specifically, I --vis can't find it out and can't test it. (this is doubtful. Don't believe it easily).

The focus is on the second parameter "/ localhost/nfd/strategy/multicast". After repeated testing and searching, I finally know that the corresponding files of this forwarding strategy are ~ / newndnsim / ns-3 / SRC / ndnsim / NFD / daemon / FW / multicast strategy.hpp and ~ / newndnsim / ns-3 / SRC / ndnsim / NFD / daemon / FW / multicast strategy.cpp.

The class MulticastStrategy public inherits the Strategy class and another class. From the implementation of the class MulticastStrategy constructor, the parameters passed by the inherited Strategy class are the objects of the Forwarder class. Code is better than a thousand words. It's not like a piece of code to say so much——

// multicast-strategy.hpp
class MulticastStrategy : public Strategy
                        , public ProcessNackTraits<MulticastStrategy>
{
public:
  explicit
  MulticastStrategy(Forwarder& forwarder, const Name& name = getStrategyName());
  ...  // Omit subsequent codes
};

// multicast-strategy.cpp
MulticastStrategy::MulticastStrategy(Forwarder& forwarder, const Name& name)
  : Strategy(forwarder)
  , ProcessNackTraits(this)
  , ...  // Omit the rest
{
  ...  // Omit subsequent codes
}

In other words, if we want to understand the code of forwarding policy, we should understand the Forwarder class, that is ~ / newndnSIM/ns-3/src/ndnSIM/NFD/daemon/fw/forwarder. *.

7.Consumer settings

effect

Set the information of the Consumer, including setting TypeId, Prefix, Frequency, specifying which node is the Consumer, and request duration. The source code is as follows——

  // Installing applications

  // Consumer
  ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
  // Consumer will request /prefix/0, /prefix/1, ...
  consumerHelper.SetPrefix("/prefix");
  consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
  auto apps = consumerHelper.Install(nodes.Get(0));                        // first node
  apps.Stop(Seconds(10.0)); // stop the consumer app at 10 seconds mark

Detailed analysis

ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr"); establish AppHelper object, setting TypeId is "ns3::ndn::ConsumerCbr" consumerHelper.SetPrefix("/prefix"); Set request prefix Prefix is /prefix consumerHelper.SetAttribute("Frequency", StringValue("10")); set a property "Frequency" is 10 (send 10 interest packets per second) auto apps = consumerHelper.Install(nodes.Get(0)); take Node0 is set to Consumer apps.Stop(Seconds(10.0)); The consumer continues to request for 10s before stopping

8.Producer settings

effect

Set the information of Producer, including setting TypeId, Prefix, PayloadSize, and specifying which node is the Producer. The source code is as follows——

  // Producer
  ndn::AppHelper producerHelper("ns3::ndn::Producer");
  // Producer will reply to all requests starting with /prefix
  producerHelper.SetPrefix("/prefix");
  producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
  producerHelper.Install(nodes.Get(2)); // last node

Detailed analysis

ndn::AppHelper producerHelper("ns3::ndn::Producer"); establish AppHelper object, setting TypeId is "ns3::ndn::Producer" producerHelper.SetPrefix("/prefix"); Set response prefix Prefix is /prefix producerHelper.SetAttribute("PayloadSize", StringValue("1024")); set a property PayloadSize is 1024 (packet size 1024Bytes) producerHelper.Install(nodes.Get(2)); take Node2 is set to Producer

9. Start the simulation

effect

Set the simulation duration to 20s and execute the simulation,. The source code is as follows——

  Simulator::Stop(Seconds(20.0));

  Simulator::Run();
  Simulator::Destroy();

  return 0;

epilogue

In this article, we dissect the entire ndn-simple.cpp code. We analyzed it so carefully that almost every line of code was deeply understood.

At first glance, this article is very lengthy, just a Hello world! Level programs take so much effort to analyze. However, in fact, the skeleton of ndnSIM code is composed of these simple parts, just adding some more details to the skeleton.

To be honest, it took me four hours to write this article, including consulting the source code, links and books. However, if you look at the similar ndn-grid.cpp code after writing, you can see ten lines at a glance. It only takes one minute to understand what the whole code is doing.

The so-called "sharpening a knife without mistaking firewood cutting", maybe smart readers can read my article in just a few minutes. Of course, this is also the purpose of this article. I hope readers can get started with the whole framework in less time. But I still hope that readers can spend a little more time and make the principle more thorough, which can save a lot of repetitive work.

  1. Zhou dizhi. Architecture and practice of open source network simulator ns-3 [M]. Beijing: China Machine Press, 2019: 65-70 ↩︎

Keywords: C++ Visual Studio Code

Added by -Karl- on Sun, 03 Oct 2021 23:03:40 +0300