To simulate Layer 3 routed protocols in NS3 has usually includes the routing packets among diverse network segments using routing protocols that perform at the Network Layer (Layer 3), like OSPF (Open Shortest Path First), BGP (Border Gateway Protocol), RIP (Routing Information Protocol), or static routing.
In this guide, we will cover how to simulate Layer 3 routing protocols in NS3 by using OSPF as an instance. We will generate a simple network topology with routers, setting up routing protocols to forward packets among different networks, and emulate the traffic among nodes.
The below is a procedure to simulate the Layer 3 routed protocols in ns3
Key Components for Layer 3 Routing Protocol Simulation:
- Nodes: Replicate routers and end-hosts.
- Point-to-Point (P2P) Links: Used to connect routers, simulating links among different network segments.
- Layer 3 Routing Protocols: Utilize protocols such as OSPF or static routing to route packets among different networks.
- Traffic Generators: Generate and route traffic (e.g., TCP, UDP) among nodes in diverse networks.
Steps to Simulate a Layer 3 Routed Protocol (e.g., OSPF) in NS3
- Install NS3
Make sure that NS3 is installed in the system.
- Create a Simulation Using a Layer 3 Routing Protocol (OSPF)
We will emulate a Layer 3 routed protocol using OSPF in this example. The steps can be easily modified to other Layer 3 routing protocols such as RIP or static routing.
Example Simulation Using OSPF
- Include Necessary Headers
The following headers are essential for configuring routers, Layer 3 routing (OSPF), point-to-point connections, and applications in NS3.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/ipv4-ospf-routing-helper.h” // OSPF Routing
- Create and Install Nodes (Routers and End Hosts)
Generate a set of nodes to signify routers and end hosts. For instance, we generate six nodes: four routers and two end-hosts.
NodeContainer routers;
routers.Create(4); // 4 routers
NodeContainer endHosts;
endHosts.Create(2); // 2 end-hosts
- Set Up Point-to-Point Links for Routers
Utilize PointToPointHelper to emulate WAN links among routers. We can setting up the data rate and latency for these links to emulate real-world conditions.
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”)); // Data rate for the P2P links
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”)); // Delay for the P2P links
NetDeviceContainer routerDevices1 = p2p.Install(routers.Get(0), routers.Get(1)); // Link router 0 to router 1
NetDeviceContainer routerDevices2 = p2p.Install(routers.Get(1), routers.Get(2)); // Link router 1 to router 2
NetDeviceContainer routerDevices3 = p2p.Install(routers.Get(2), routers.Get(3)); // Link router 2 to router 3
NetDeviceContainer hostRouter1 = p2p.Install(endHosts.Get(0), routers.Get(0)); // Link host 0 to router 0
NetDeviceContainer hostRouter2 = p2p.Install(endHosts.Get(1), routers.Get(3)); // Link host 1 to router 3
- Install the Internet Stack and OSPF Routing Protocol
Install the Internet stack (TCP/IP protocols) and set up OSPF as the routing protocol for the routers.
InternetStackHelper internet;
Ipv4OspfRoutingHelper ospfRouting;
internet.SetRoutingHelper(ospfRouting); // Use OSPF for Layer 3 routing
internet.Install(routers); // Install the Internet stack on the routers
internet.Install(endHosts); // Install the Internet stack on the end-hosts
- Assign IP Addresses
Allocate IP addresses to the point-to-point links between the routers and between the routers and the end-hosts.
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”); // Network 10.1.1.0/24 for router 0-1 link
Ipv4InterfaceContainer interfaces1 = address.Assign(routerDevices1);
address.SetBase(“10.1.2.0”, “255.255.255.0”); // Network 10.1.2.0/24 for router 1-2 link
Ipv4InterfaceContainer interfaces2 = address.Assign(routerDevices2);
address.SetBase(“10.1.3.0”, “255.255.255.0”); // Network 10.1.3.0/24 for router 2-3 link
Ipv4InterfaceContainer interfaces3 = address.Assign(routerDevices3);
address.SetBase(“192.168.1.0”, “255.255.255.0”); // Network 192.168.1.0/24 for host 0-router 0 link
Ipv4InterfaceContainer hostInterface1 = address.Assign(hostRouter1);
address.SetBase(“192.168.2.0”, “255.255.255.0”); // Network 192.168.2.0/24 for host 1-router 3 link
Ipv4InterfaceContainer hostInterface2 = address.Assign(hostRouter2);
- Set Up Traffic Applications
We can emulate traffic among the end hosts using TCP or UDP applications. For instance, we can utilize a UDP Echo client-server application in which one end-host behave like a server and the other as a client.
- Server (UDP Echo Server):
uint16_t port = 9; // Port for the UDP server
UdpEchoServerHelper echoServer(port);
ApplicationContainer serverApp = echoServer.Install(endHosts.Get(1)); // Server on host 1
serverApp.Start(Seconds(1.0)); // Start server at 1 second
serverApp.Stop(Seconds(20.0)); // Stop server at 20 seconds
- Client (UDP Echo Client):
UdpEchoClientHelper echoClient(hostInterface2.GetAddress(1), port); // Client sending to host 1
echoClient.SetAttribute(“MaxPackets”, UintegerValue(100)); // Send 100 packets
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0))); // Send a packet every 1 second
echoClient.SetAttribute(“PacketSize”, UintegerValue(1024)); // Packet size
ApplicationContainer clientApp = echoClient.Install(endHosts.Get(0)); // Client on host 0
clientApp.Start(Seconds(2.0)); // Start client at 2 seconds
clientApp.Stop(Seconds(20.0)); // Stop client at 20 seconds
- Enable Tracing and Logging
Allow PCAP tracing to capture packet interchange for later analysis, and utilize FlowMonitor to monitor traffic flow and network performance.
p2p.EnablePcapAll(“layer3_ospf_simulation”); // Enable packet capture (PCAP) for all links
- Run the Simulation
Set the simulation stop time and execute the simulation.
Simulator::Stop(Seconds(20.0)); // Stop the simulation after 20 seconds
Simulator::Run(); // Run the simulation
Simulator::Destroy(); // Clean up after simulation ends
- Full Example Script for Layer 3 Routed Protocol Simulation Using OSPF
Here is a complete simulation script using OSPF for Layer 3 routing in a simple network with routers and end-hosts.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/ipv4-ospf-routing-helper.h”
using namespace ns3;
int main(int argc, char *argv[]) {
// Step 1: Create routers and end hosts
NodeContainer routers;
routers.Create(4); // 4 routers
NodeContainer endHosts;
endHosts.Create(2); // 2 end hosts
// Step 2: Set up point-to-point links between routers and hosts
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer routerDevices1 = p2p.Install(routers.Get(0), routers.Get(1)); // Router 0 to router 1
NetDeviceContainer routerDevices2 = p2p.Install(routers.Get(1), routers.Get(2)); // Router 1 to router 2
NetDeviceContainer routerDevices3 = p2p.Install(routers.Get(2), routers.Get(3)); // Router 2 to router 3
NetDeviceContainer hostRouter1 = p2p.Install(endHosts.Get(0), routers.Get(0)); // Host 0 to router 0
NetDeviceContainer hostRouter2 = p2p.Install(endHosts.Get(1), routers.Get(3)); // Host 1 to router 3
// Step 3: Install Internet stack and OSPF routing on routers and hosts
InternetStackHelper internet;
Ipv4OspfRoutingHelper ospfRouting;
internet.SetRoutingHelper(ospfRouting); // Use OSPF for routing
internet.Install(routers);
internet.Install(endHosts);
// Step 4: Assign IP addresses to P2P links
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces1 = address.Assign(routerDevices1);
address.SetBase(“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces2 = address.Assign(routerDevices2);
address.SetBase(“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces3 = address.Assign(routerDevices3);
address.SetBase(“192.168.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer hostInterface1 = address.Assign(hostRouter1);
address.SetBase(“192.168.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer hostInterface2 = address.Assign(hostRouter2);
// Step 5: Set up UDP Echo server on host 1
uint16_t port = 9;
UdpEchoServerHelper echoServer(port);
ApplicationContainer serverApp = echoServer.Install(endHosts.Get(1));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(20.0));
// Step 6: Set up UDP Echo client on host 0
UdpEchoClientHelper echoClient(hostInterface2.GetAddress(1), port);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(100));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApp = echoClient.Install(endHosts.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(20.0));
// Step 7: Enable packet capture (PCAP) for all links
p2p.EnablePcapAll(“layer3_ospf_simulation”);
// Step 8: Run the simulation
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
- Running the Simulation
- Build and run the script using NS3’s waf build system:
./waf build
./waf –run layer3_ospf_simulation
- Analysing Results
- PCAP Traces: The pcap files created in the course of the simulation can be measured using tools such as Wireshark to track packet exchanges and routing behavior.
- FlowMonitor: we can combine FlowMonitor into the script to evaluate throughput, delay, packet loss, and other network parameter.
- Extending the Simulation
We can expand this simple OSPF simulation by:
- Executing more complex network topologies with additional routers and network segments.
- Replicating different routing protocols such as BGP, RIP, static routing and relating their performance.
- Modifying the link characteristics such as bandwidth, delay to replicate more realistic WAN or LAN environments.
In the above demonstration we provide the complete simulation process procedures to execute the layer 3 routed protocols that executes in ns3 tool. If you did like to know more details regarding the layer 3 routed protocol let me know! To simulate Projects Using Layer 3 Routing Protocol We have a dedicated team to assist you using NS3. With the help of our team, complete your work on time. We have all the resources necessary to provide you with the best results.