How to Develop OSPF Algorithm Projects Using NS3

To develop the OSPF Algorithm has includes the NS3. The Open Shortest Path First (OSPF) protocol is link-state routing protocol used for IP networks. NS3 does not natively encourage for OSPF, nevertheless we can execute an OSPF-like routing using NS3’s Global Routing or incorporate the Quagga (an OSPF-supported routing daemon) in NS3.

Steps to Develop OSPF Algorithm Projects Using NS3

  1. Install NS3

First, install and download the NS3 on your system:

sudo apt update

sudo apt install git g++ python3 cmake make

git clone

cd ns-3-dev

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

./ns3 build

  1. Create Network Topology for OSPF

Since OSPF is link-state protocol, it needs the several routers by active connection updates.

Example: Creating a Simple Network

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

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

#include “ns3/ipv4-global-routing-helper.h”

using namespace ns3;

int main()

{

// Create 5 routers (nodes)

NodeContainer routers;

routers.Create(5);

PointToPointHelper p2p;

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

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

NetDeviceContainer devices;

for (uint32_t i = 0; i < routers.GetN() – 1; ++i)

{

devices = p2p.Install(routers.Get(i), routers.Get(i + 1));

}

// Install Internet Stack

InternetStackHelper stack;

stack.Install(routers);

// Assign IP Addresses

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Enable OSPF-like Global Routing

Ipv4GlobalRoutingHelper::PopulateRoutingTables();

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Implement OSPF Algorithm (Dijkstra’s Algorithm)

OSPF uses Dijkstra’s Algorithm for every router to calculate the shortest path.

Dijkstra’s Algorithm Implementation

#include <iostream>

#include <vector>

#include <limits>

#define INF std::numeric_limits<int>::max()

void dijkstra(int graph[5][5], int src, int numNodes)

{

std::vector<int> dist(numNodes, INF);

std::vector<bool> visited(numNodes, false);

dist[src] = 0;

for (int i = 0; i < numNodes – 1; i++)

{

int min = INF, u;

for (int j = 0; j < numNodes; j++)

{

if (!visited[j] && dist[j] < min)

{

min = dist[j], u = j;

}

}

visited[u] = true;

for (int v = 0; v < numNodes; v++)

{

if (!visited[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v])

{

dist[v] = dist[u] + graph[u][v];

}

}

}

std::cout << “Node\tDistance from Source\n”;

for (int i = 0; i < numNodes; i++)

{

std::cout << i << “\t” << dist[i] << std::endl;

}

}

int main()

{

int graph[5][5] = {

{0, 10, 20, 0, 0},

{10, 0, 5, 15, 0},

{20, 5, 0, 30, 20},

{0, 15, 30, 0, 10},

{0, 0, 20, 10, 0}};

int source = 0;

dijkstra(graph, source, 5);

return 0;

}

  1. Use Quagga to Implement OSPF in NS3

Quagga is routing software suite which contains an OSPFv2 and OSPFv3 execution.

Step 1: Install Quagga

sudo apt install quagga

Step 2: Configure OSPF in NS3

NS3 can be incorporated by Quagga using Direct Code Execution (DCE).

Example:

#include “ns3/dce-module.h”

DceManagerHelper dce;

dce.SetTaskManagerAttribute(“FiberManagerType”, StringValue(“UcontextFiberManager”));

stack.Install(routers);

dce.Install(routers);

Step 3: We setting the OSPF Daemon

echo “router ospf

network 10.1.1.0/24 area 0

” > /etc/quagga/ospfd.conf

It processes the Quagga daemon:

sudo systemctl start quagga

  1. Run the Simulation

Compile and implement the process for replication of OSPF Algorithms:

./ns3 run ospf-routing

  1. Analyze Network Performance

FlowMonitor used to calculate the performance of parameter metrices such as Packet Delivery Ratio, Throughput, and Delay:

#include “ns3/flow-monitor-module.h”

FlowMonitorHelper flowmon;

Ptr<FlowMonitor> monitor = flowmon.InstallAll();

Simulator::Stop(Seconds(10.0));

Simulator::Run();

monitor->SerializeToXmlFile(“ospf.xml”, true, true);

Simulator::Destroy();

As the above following instruction we shown on how to simulate and extend the implementation for Open Shortest Path First in ns3 tool and any concerns regarding this Open Shortest Path First will be resolved in an extra manual