How to Begin Implement Network Gateways in NS3

To implement the network a gateway in ns-3 has includes the set-up a node to perform as a gateway, assigning the communication among various networks or subnets. A gateway works at the network layer and typically needs the correct routing and interfaces settings.

Below is a structured guide to implementing network gateways in ns-3:

Steps to Begin Implement Network Gateways in NS3

  1. Understand the Role of a Gateway
  • An interconnection point gateway serves as among two or additional networks or subnets.
  • It executes the packet forwarding according to their routing tables and can be apply the NAT (Network Address Translation) if needed.
  1. Set up the ns-3 Environment
  • Enable the ns-3 is installed and functional:

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

cd ns-3-dev

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

./ns3 build

  • Validate the configuration by:

./ns3 run hello-simulator

  1. Design the Network Topology
  • Describe the two or many subnets linked by a gateway node.
  • Example:
    • Subnet 1: Node1 <-> Gateway.
    • Subnet 2: Node2 <-> Gateway.
  1. Steps to Implement a Gateway

(a) Create Nodes

  • Make a nodes for the subnets and the gateway.

NodeContainer nodes;

nodes.Create(3); // Node 0, Node 2, and Node 1 (Gateway)

(b) Install Network Devices

  • Use PointToPointHelper to linked the nodes and gateway.

PointToPointHelper p2p;

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

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

NetDeviceContainer devices1 = p2p.Install(NodeContainer(nodes.Get(0), nodes.Get(1)));

NetDeviceContainer devices2 = p2p.Install(NodeContainer(nodes.Get(1), nodes.Get(2)));

(c) Install Internet Stack

  • Install the all nodes for Internet stack.

InternetStackHelper stack;

stack.Install(nodes);

(d) Assign IP Addresses

  • Allot the IP addresses for devices on every subnet.

Ipv4AddressHelper address1;

address1.SetBase(“192.168.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);

Ipv4AddressHelper address2;

address2.SetBase(“192.168.2.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);

(e) Set Up Routing

  • Setting the gateway for route packets among the subnets.
    • Ensure the forwarding on the gateway:

Ptr<Ipv4> ipv4 = nodes.Get(1)->GetObject<Ipv4>();

ipv4->SetAttribute(“IpForward”, BooleanValue(true));

    • Use static or dynamic routing:
      • Static Routing:

Ipv4StaticRoutingHelper staticRouting;

Ptr<Ipv4StaticRouting> routingNode0 = staticRouting.GetStaticRouting(nodes.Get(0)->GetObject<Ipv4>());

routingNode0->AddNetworkRouteTo(Ipv4Address(“192.168.2.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“192.168.1.1”), 1);

Ptr<Ipv4StaticRouting> routingNode2 = staticRouting.GetStaticRouting(nodes.Get(2)->GetObject<Ipv4>());

routingNode2->AddNetworkRouteTo(Ipv4Address(“192.168.1.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“192.168.2.1”), 1);

      • Dynamic Routing (Optional): Used for dynamic routing such as RipHelper or OspfHelper.

(f) Install Applications

  • It replicates the congestion with the gateway using UdpEchoServer and UdpEchoClient:

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApp = echoServer.Install(nodes.Get(2));

serverApp.Start(Seconds(1.0));

serverApp.Stop(Seconds(10.0));

UdpEchoClientHelper echoClient(Ipv4Address(“192.168.2.2”), 9);

echoClient.SetAttribute(“MaxPackets”, UintegerValue(10));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApp = echoClient.Install(nodes.Get(0));

clientApp.Start(Seconds(2.0));

clientApp.Stop(Seconds(10.0));

  1. Example Code for Gateway Implementation

Below is a whole sample signifying the network gateways in ns-3:

#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);

// Create nodes

NodeContainer nodes;

nodes.Create(3); // Node 0, Node 2, and Node 1 (Gateway)

// Create Point-to-Point links

PointToPointHelper p2p;

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

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

NetDeviceContainer devices1 = p2p.Install(NodeContainer(nodes.Get(0), nodes.Get(1)));

NetDeviceContainer devices2 = p2p.Install(NodeContainer(nodes.Get(1), nodes.Get(2)));

// Install Internet stack

InternetStackHelper stack;

stack.Install(nodes);

// Assign IP addresses

Ipv4AddressHelper address1;

address1.SetBase(“192.168.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);

Ipv4AddressHelper address2;

address2.SetBase(“192.168.2.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);

// Enable IP forwarding on the gateway node

Ptr<Ipv4> ipv4 = nodes.Get(1)->GetObject<Ipv4>();

ipv4->SetAttribute(“IpForward”, BooleanValue(true));

// Set up static routing

Ipv4StaticRoutingHelper staticRouting;

Ptr<Ipv4StaticRouting> routingNode0 = staticRouting.GetStaticRouting(nodes.Get(0)->GetObject<Ipv4>());

routingNode0->AddNetworkRouteTo(Ipv4Address(“192.168.2.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“192.168.1.1”), 1);

 

Ptr<Ipv4StaticRouting> routingNode2 = staticRouting.GetStaticRouting(nodes.Get(2)->GetObject<Ipv4>());

routingNode2->AddNetworkRouteTo(Ipv4Address(“192.168.1.0”), Ipv4Mask(“255.255.255.0”), Ipv4Address(“192.168.2.1”), 1);

// Install applications

UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApp = echoServer.Install(nodes.Get(2));

serverApp.Start(Seconds(1.0));

serverApp.Stop(Seconds(10.0));

UdpEchoClientHelper echoClient(Ipv4Address(“192.168.2.2”), 9);

echoClient.SetAttribute(“MaxPackets”, UintegerValue(10));

echoClient.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));

ApplicationContainer clientApp = echoClient.Install(nodes.Get(0));

clientApp.Start(Seconds(2.0));

clientApp.Stop(Seconds(10.0));

// Run simulation

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Validate the Gateway Functionality
  • Use FlowMonitor or packet traces for validate which congestion is correctly routed through the gateway.
  • Examine the performance of metrics such as latency, throughput, and packet delivery ratio.
  1. Extend Functionality
  • Enhance the many subnets and gateways.
  • Apply the NAT for inter-network transmission.
  • Use dynamic routing protocols such as RIP or OSPF for scalable routing.

We explored the numerous concepts regarding the simulation process, installation procedures and their configuration setup for gateway protocol project that will executed using the tool of ns3 analysis tool. Any queries related to this project will be clarified in a different manual.