How to Develop L3 Protocols Projects Using NS3

To develop a Layer 3 (L3) protocol projects in NS3, we need to offer numerous key steps that contains comprehending network stack of NS3, executing routing protocols, as well as estimating the performance with simulation. Here’s a simple procedure to develop L3 protocols projects using NS3.

Steps to Develop L3 Protocols in NS3

  1. Understanding L3 (Network Layer) in NS3
  • In NS3, L3 protocols primarily concentrate on the packets’ routing and forwarding.
  • NS3 provides inherent support for numerous L3 protocols with:
    • IPv4
    • IPv6
    • Static Routing
    • Dynamic Routing (AODV, OLSR, DSDV, DSR)
  • If require a custom routing protocol then we can prolong the existing classes or execute a new one.
  1. Setting Up NS3

Installation (Linux-based)

Now, we make sure that NS3 is installed and compiled

sudo apt update

sudo apt install git g++ python3 python3-pip cmake

git clone https://gitlab.com/nsnam/ns-3-dev.git

cd ns-3-dev

./ns3 configure –enable-examples –enable-tests

./ns3 build

  • The above coding will help you to install NS3 and compile it.
  1. Selecting or Developing an L3 Protocol

(a) Using Built-in L3 Routing Protocols

NS3 offers support for multiple L3 routing protocols that we will need to directly utilize:

#include “ns3/aodv-module.h”

#include “ns3/olsr-module.h”

#include “ns3/dsdv-module.h”

#include “ns3/dsr-module.h”

Example: Using AODV routing in NS3

AodvHelper aodv;

Ipv4ListRoutingHelper list;

list.Add(aodv, 10);

InternetStackHelper stack;

stack.SetRoutingHelper(list);

stack.Install(nodes);

(b) Developing a Custom L3 Protocol

  • We can generate a new class, which expands the Ipv4RoutingProtocol.
  • Execute:
    • Route Maintenance
    • Packet Forwarding
    • Route Lookup Mechanism
    • Neighbor Discovery

Example: Creating a Custom Routing Protocol (MyRoutingProtocol)

  1. After that, we generate a new header file as my-routing-protocol.h
  2. Describe the class:

class MyRoutingProtocol : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId(void);

MyRoutingProtocol();

virtual ~MyRoutingProtocol();

virtual Ptr<Ipv4Route> RouteOutput(Ptr<Packet> packet,

const Ipv4Header &header,

Ptr<NetDevice> oif,

Socket::SocketErrno &sockerr) override;

virtual bool RouteInput(Ptr<const Packet> packet,

const Ipv4Header &header,

Ptr<const NetDevice> idev,

UnicastForwardCallback ucb,

MulticastForwardCallback mcb,

LocalDeliverCallback lcb,

ErrorCallback ecb) override;

};

  1. Execute the my-routing-protocol.cc:

NS_LOG_COMPONENT_DEFINE(“MyRoutingProtocol”);

TypeId MyRoutingProtocol::GetTypeId(void) {

static TypeId tid = TypeId(“ns3::MyRoutingProtocol”)

.SetParent<Ipv4RoutingProtocol>()

.SetGroupName(“Internet”);

return tid;

}

MyRoutingProtocol::MyRoutingProtocol() {}

MyRoutingProtocol::~MyRoutingProtocol() {}

Ptr<Ipv4Route> MyRoutingProtocol::RouteOutput(

Ptr<Packet> packet, const Ipv4Header &header,

Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) {

NS_LOG_INFO(“RouteOutput called”);

return 0;

}

  1. Simulation Script for L3 Protocol
  • Here, we have to design a simulation script such as main.cc for L3 protocol.
  • We make nodes, set up a network stack, allocate addresses, and then execute the simulation using NS3 helpers.

Example: Running a Simulation with a Custom Routing Protocol

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “my-routing-protocol.h”

using namespace ns3;

int main() {

NodeContainer nodes;

nodes.Create(4);

PointToPointHelper p2p;

p2p.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));

p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));

NetDeviceContainer devices;

devices = p2p.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign(devices);

Simulator::Run();

Simulator::Destroy();

return 0;

}

Subsequently, we will build the script and execute the simulation using:

./waf –run “scratch/main”

  1. Performance Analysis
  • Estimate the performance of network with FlowMonitor.
  • Example:

Ptr<FlowMonitor> flowMonitor;

FlowMonitorHelper flowHelper;

flowMonitor = flowHelper.InstallAll();

Simulator::Stop(Seconds(10.0));

Simulator::Run();

flowMonitor->SerializeToXmlFile(“results.xml”, true, true);

  • Execute the simulations and then plot the performance outcomes to utilize the following code for visualization:

python3 plot_results.py

We have showcased the comprehensive implementation steps on how to develop the L3 Protocols projects and how to analyse and visualize the performance outcomes using NS3 simulator. You will get any details regarding this project in the future from us.