To develop Dijkstra’s Link State Routing Algorithm in NS3 which is a broadly utilised shortest-path algorithm within network routing protocols such as OSPF (Open Shortest Path First). It is depends on the link-state data that can be accumulated from every node to allow efficient path computation with the support of Dijkstra’s Algorithm.
We will be implemented a custom Link-State Routing (LSR) protocol or adjusted an existing OSPF-like implementation as NS3 doesn’t have an inherent support for execution of OSPF or Dijkstra’s Algorithm.
Steps to Develop a Dijkstra’s Link State Routing Project in NS3
- Install and Set Up NS3
Initially, we make sure that NS3 is installed on the system:
git clone
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Define a Network Topology
We will design a graph-based network topology that can be needed for Link-State Routing.
Example: Create a 6-Node Network
NodeContainer nodes;
nodes.Create(6); // Create 6 nodes
Connect Nodes Using Point-to-Point Links
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“1Gbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devicesAB = p2p.Install(nodes.Get(0), nodes.Get(1));
NetDeviceContainer devicesAC = p2p.Install(nodes.Get(0), nodes.Get(2));
NetDeviceContainer devicesBD = p2p.Install(nodes.Get(1), nodes.Get(3));
NetDeviceContainer devicesCD = p2p.Install(nodes.Get(2), nodes.Get(3));
NetDeviceContainer devicesCE = p2p.Install(nodes.Get(2), nodes.Get(4));
NetDeviceContainer devicesDF = p2p.Install(nodes.Get(3), nodes.Get(5));
NetDeviceContainer devicesEF = p2p.Install(nodes.Get(4), nodes.Get(5));
- Assign IP Addresses
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesAB = address.Assign(devicesAB);
address.SetBase(“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesAC = address.Assign(devicesAC);
address.SetBase(“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesBD = address.Assign(devicesBD);
address.SetBase(“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesCD = address.Assign(devicesCD);
address.SetBase(“10.1.5.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesCE = address.Assign(devicesCE);
address.SetBase(“10.1.6.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesDF = address.Assign(devicesDF);
address.SetBase(“10.1.7.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfacesEF = address.Assign(devicesEF);
- Implement Dijkstra’s Link-State Routing
We execute the custom Link-State Routing Protocol because NS3 doesn’t have inherent support for OSPF that utilizes Dijkstra’s Algorithm.
(A) Create a Custom LSR Class
class LinkStateRouting : public Ipv4RoutingProtocol {
public:
void ComputeDijkstra(NodeContainer nodes, uint32_t source);
};
(B) Implement Dijkstra’s Algorithm
void LinkStateRouting::ComputeDijkstra(NodeContainer nodes, uint32_t source) {
uint32_t numNodes = nodes.GetN();
std::vector<uint32_t> distance(numNodes, INT_MAX);
std::vector<bool> visited(numNodes, false);
distance[source] = 0;
for (uint32_t i = 0; i < numNodes; i++) {
uint32_t minDist = INT_MAX, minNode = -1;
for (uint32_t j = 0; j < numNodes; j++) {
if (!visited[j] && distance[j] < minDist) {
minDist = distance[j];
minNode = j;
}
}
visited[minNode] = true;
for (uint32_t neighbor = 0; neighbor < numNodes; neighbor++) {
if (!visited[neighbor]) {
uint32_t linkCost = Simulator::Now().GetNanoSeconds() % 10 + 1;
if (distance[minNode] + linkCost < distance[neighbor]) {
distance[neighbor] = distance[minNode] + linkCost;
}
}
}
}
for (uint32_t i = 0; i < numNodes; i++) {
NS_LOG_UNCOND(“Shortest path from node ” << source << ” to node ” << i << ” is ” << distance[i]);
}
}
(C) Use the Custom LSR Protocol
Ptr<LinkStateRouting> lsrRouting = CreateObject<LinkStateRouting>();
nodes.Get(0)->AggregateObject(lsrRouting);
lsrRouting->ComputeDijkstra(nodes, 0);
- Setup Traffic Applications
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(nodes.Get(5));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(Ipv4Address(“10.1.7.2”), 9);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(5));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(512));
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
- Enable Tracing for Debugging
Now, allow tracing which is used for debugging
AsciiTraceHelper ascii;
p2p.EnableAsciiAll(ascii.CreateFileStream(“dijkstra-routing.tr”));
p2p.EnablePcapAll(“dijkstra-routing”);
- Run the Simulation
After that, we execute the simulation using
Simulator::Run();
Simulator::Destroy();
- Analyze Results
When execution is done, envision the performance outcomes using NetAnim:
./waf –run dijkstra-routing
netanim
- Extend with Advanced Features
- Energy-aware Routing (battery level consideration)
- Security-enhanced OSPF (link-state verification)
- QoS-aware Dijkstra Routing (delay and bandwidth-based routing)
At the end of this approach, you can understand the Dijkstra’s Link State Projects and how to implement and analyse it using NS3 environment. Also, we will offer detailed insights on this project, if needed.