To implement IP protocols in ns-3, we carry it by ns3 environment that is suitable for configuring and simulating different types of IP-based networks for the implementation process.
Here we have provided the steps to implement and simulate both IPv4 and IPv6 protocols in ns-3.
Step-by-Step Guide to Implement IP Protocols in ns-3
- Setting Up Your Environment
Ensure that ns3 is installed on the system.
- Creating a New ns-3 Script
Create a new simulation script in the scratch directory of your ns3 installation. For example, we can create a file named ip-simulation.cc.
cd ns-3.xx
cd scratch
touch ip-simulation.cc
3. Including Necessary Headers
In the ip-simulation.cc file, include the necessary headers for simulation.
#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”
using namespace 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”
using namespace ns3;
4. Setting Up IPv4 Network Topology
Define the network topology for IPv4. Let’s create a simple topology with three nodes connected in a line.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get (0));
interfaces.Add (address.Assign (devices.Get (1)));
address.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get (2)));
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (2), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
5. Setting Up IPv6 Network Topology
To set up an IPv6 network topology, we can modify the script to configure IPv6 addresses and interfaces. Let’s use the same topology with IPv6 addresses.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));
InternetStackHelper stack;
stack.Install (nodes);
Ipv6AddressHelper address;
address.SetBase (Ipv6Address (“2001:db8::”), Ipv6Prefix (64));
Ipv6InterfaceContainer interfaces = address.Assign (devices.Get (0));
interfaces.Add (address.Assign (devices.Get (1)));
address.SetBase (Ipv6Address (“2001:db8:1::”), Ipv6Prefix (64));
interfaces.Add (address.Assign (devices.Get (2)));
// Enable IPv6 on all interfaces
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
Ptr<Ipv6> ipv6 = nodes.Get (i)->GetObject<Ipv6> ();
ipv6->SetForwarding (0, true);
ipv6->SetDefaultRoute (interfaces.GetAddress (0, 1), 1);
}
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (2, 1), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
6. Build and Run the Simulation
After writing the script, we need to build and run it.
./waf build
./waf –run scratch/ip-simulation
7. Analyze the Results
After running the simulation, we can analyze the results based on needs. We have to add logging or tracing to observe the behavior of the IP protocols.
Complete Example Script
Here’s a complete example script for both IPv4 and IPv6 setups:
#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”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));
InternetStackHelper stack;
stack.Install (nodes);
// IPv4 setup
Ipv4AddressHelper address4;
address4.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces4 = address4.Assign (devices.Get (0));
interfaces4.Add (address4.Assign (devices.Get (1)));
address4.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces4.Add (address4.Assign (devices.Get (2)));
// IPv6 setup
Ipv6AddressHelper address6;
address6.SetBase (Ipv6Address (“2001:db8::”), Ipv6Prefix (64));
Ipv6InterfaceContainer interfaces6 = address6.Assign (devices.Get (0));
interfaces6.Add (address6.Assign (devices.Get (1)));
address6.SetBase (Ipv6Address (“2001:db8:1::”), Ipv6Prefix (64));
interfaces6.Add (address6.Assign (devices.Get (2)));
// Enable IPv6 on all interfaces
for (uint32_t i = 0; i < nodes.GetN (); ++i)
{
Ptr<Ipv6> ipv6 = nodes.Get (i)->GetObject<Ipv6> ();
ipv6->SetForwarding (0, true);
ipv6->SetDefaultRoute (interfaces6.GetAddress (0, 1), 1);
}
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// IPv4 client
UdpEchoClientHelper echoClient4 (interfaces4.GetAddress (2), 9);
echoClient4.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient4.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient4.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps4 = echoClient4.Install (nodes.Get (0));
clientApps4.Start (Seconds (2.0));
clientApps4.Stop (Seconds (10.0));
// IPv6 client
UdpEchoClientHelper echoClient6 (interfaces6.GetAddress (2, 1), 9);
echoClient6.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient6.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient6.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps6 = echoClient6.Install (nodes.Get (1));
clientApps6.Start (Seconds (2.0));
clientApps6.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Finally, IPv4 and IPv6 protocols is implemented successfully by creating a new ns3 script for setting up simulation scripts were build and run to analyse the results.
We work on IPv4 and IPv6 protocols in ns-3 for best results enquire us for more support.