How to Implement Network Layer in NS2

To implement or customize the Network Layer within NS2 (Network Simulator 2), we require to work with the routing and packet forwarding mechanisms which work at Layer 3 that is the network layer. The simulation NS2 delivers built-in support for numerous network-layer protocols like AODV, DSDV, DSR, and TORA. However, we can make custom network-layer functionality or change the existing ones. We provide stepwise procedure to implement the network layer in NS2:

Steps to Implement or Customize the Network Layer in NS2:

  1. Understanding the Network Layer in NS2

In NS2, the network layer manages:

  • Routing Protocols: Ascertains how packets are routed from the source to destination like AODV, DSDV.
  • Packet Forwarding: Handles how packets are forwarded among the nodes.
  • Addressing: It use IP-like addressing for nodes.
  1. Basic Network Layer Configuration in NS2 (Using Built-in Protocols)

We can setup and replicate the network layer using NS2’s built-in routing protocols such as AODV, DSDV, and DSR. Given below is an instance of how to use this protocols such as AODV in a TCL script.

Example TCL Script for Using AODV (Network Layer Protocol)

# Create a new simulator instance

set ns [new Simulator]

# Open trace and nam files

set tracefile [open “network_layer_trace.tr” w]

$ns trace-all $tracefile

set namfile [open “network_layer.nam” w]

$ns namtrace-all-wireless $namfile

# Define the network topology (a flat grid of 500×500)

set topo [new Topography]

$topo load_flatgrid 500 500

# Configure the nodes with the network layer settings (AODV as the routing protocol)

$ns node-config -adhocRouting AODV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

# Set up UDP agents and traffic generators

set udp0 [new Agent/UDP]

set sink0 [new Agent/Null]

$ns attach-agent $n0 $udp0

$ns attach-agent $n2 $sink0

$ns connect $udp0 $sink0

# Create a CBR (Constant Bit Rate) application to generate traffic

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set interval_ 0.1

$cbr attach-agent $udp0

# Define a movement pattern (optional for a mobile scenario)

$ns at 0.0 “$n0 setdest 200 300 5.0”

$ns at 0.0 “$n1 setdest 250 400 5.0”

$ns at 0.0 “$n2 setdest 300 100 5.0”

# Start and stop the traffic

$ns at 1.0 “$cbr start”

$ns at 4.0 “$cbr stop”

# Define finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam network_layer.nam &

exit 0

}

# End the simulation

$ns at 5.0 “finish”

# Run the simulation

$ns run

Key Elements of the Script:

  • Network Layer Configuration: The -adhocRouting AODV option sets the network layer routing protocol to the AODV. You can replace AODV with other routing protocols such as DSDV, DSR, or TORA.
  • Traffic Setup: UDP agents and a CBR traffic generator are used to mimic communication among the nodes.
  • Movement Patterns: For mobile ad-hoc networks (MANETs), movement patterns can be described to replicate the node mobility.

This script configures a basic wireless network in which the nodes use AODV to route traffic.

  1. Customizing the Network Layer (Implementing a New Protocol)

If we need to make a custom network layer protocol or alter an existing protocol, we require to work with the C++ source code of NS2.

Steps for Customizing or Creating a New Network Layer Protocol:

  1. Choose a Protocol as a Base: We can begin changing an existing protocol such as AODV or DSDV. The related files are placed in the ns-allinone-2.35/ns-2.35/ directory.

For instance, AODV’s execution can be found in:

    • aodv.cc: Encompasses the core logic of AODV.
    • aodv.h: Header file for AODV.
  1. Modify the Protocol: We can alter how the protocol performs by editing its source files. For instance, to execute a custom routing protocol, we would:
    • Change how route requests and replies are managed.
    • Execute custom packet forwarding logic.
    • Alter how the routing table is updated.

Given below is a sample of changing the aodv.cc file to add custom logic to the route request managing:

void AODV::recvRequest(Packet *p) {

// Custom logic for handling route requests

hdr_aodv_request *rq = HDR_AODV_REQUEST(p);

if (rq->rq_src == index) {

// If the request is from the same node, drop the packet

Packet::free(p);

return;

}

// Add custom behavior here for processing route requests

// For example, modifying route selection criteria

// Call the original AODV route request handling

forwardRequest(p);

}

  1. Recompile NS2: We want to recompile NS2 for the changes to take impact, after modifying the source code,:

cd ns-allinone-2.35/ns-2.35/

./configure

make clean

make

  1. Register the New Protocol: When we have executed the new protocol, register it in NS2 thus it can use in TCL scripts. To do this, we require to change modify the tcl/lib/ns-default.tcl file and register the new protocol.

For instance, if the protocol is called MyProtocol:

Agent/MyProtocol set sport_ 0

Agent/MyProtocol set dport_ 0

  1. Create a TCL Script to Test the New Protocol: Now, make a TCL script same to the previous sample but set up it to use the new protocol:

$ns node-config -adhocRouting MyProtocol

  1. Packet Forwarding and Routing Table Management

If we are constructing a custom protocol, we will likely want to execute the following main functions:

  • Route Request Handling: Manage requests for new routes when a node doesn’t have an entry in these routing table.
  • Route Reply Handling: When a node has a valid route, it answers with route data.
  • Routing Table Management: Maintain the routing table with update the route information, using the parameters like hop count or link quality.
  1. Analysing the Network Layer Behaviour

We can evaluate the trace file (network_layer_trace.tr) to monitor the network layer’s behaviour, after running the simulation. The trace file logs events like packet forwarding, routing table updates, route requests, and replies. Also, we can visualize the simulation using NAM by opening the NAM file (network_layer.nam) to observe how packets are routed over the network.

  1. Advanced Customizations (Optional)
  • Energy-Aware Routing: If we are mimicking wireless sensor networks, we may need to change the network layer protocol to deliberate energy consumption as a metric when choosing the routes.
  • Load Balancing: We can execute load balancing mechanisms in which traffic is distributed over numerous paths according to the network conditions.
  • Multipath Routing: Execute multipath routing protocols which maintain several routes among the nodes for redundancy and fault tolerance.

Over this explanation we learned and get knowledge regarding the Network Layer that implement and evaluate in NS2 environment through the above procedure and examples. We can offer more instances about this topic as required.

Receive top-notch project support from our team. Reach out to ns2project.com for expert implementation advice in the Network Layer of NS2. We specialize in simulating and analyzing network layers. Our developers are here to assist you with network-layer protocols such as AODV, DSDV, DSR, and TORA.