How to Implement Flooding Routing in NS2

To implement the flooding routing is a simple however valuable technique of the routing where each node in the network that send every incoming packet to all of its neighbours excepting the one it received from the packet. It continues until the packet is reaches its end or until it has been transferred to every nodes in the network. This routing is generally used in scenarios like finding paths in the ad-hoc networks and the broadcasting data.

The simulator NS2 does not deliver a built-in flooding protocol. However, we can execute it by tailoring the performance in the network of packet forward. We provide the simple procedure to execute it in ns2 using OTcL.

Steps to Implement Flooding Routing in NS2

  1. Set Up NS2 Environment
    • Make sure that NS2 is installed and set up on the computer.
  2. Understand Flooding Routing
    • In the flooding, each node forwards the packet to all its neighbours. To avoid the endless looping, we can want the mechanisms such as hop count, sequence numbers, or time-to-live (TTL) to limit how long a packet is flooded in the network.
  3. Design the Network Topology
    • Create a network with several nodes in which the packets can be forwarded to all neighbours.
  1. Implement Flooding in NS2

Now, we will execute a simple flooding routing protocol by creating the custom agents to manage packet forwarding, mimic flooding behaviour and the packet will be forwarded by all node to its neighbours except the packet has previously processed or its TTL has expired.

Example OTcl Script for Flooding Routing

# Define the simulation environment

set ns [new Simulator]

# Open the trace file for recording data

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Define the finish procedure

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exec nam out.nam &

exit 0

}

# Create network nodes

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

set node4 [$ns node]

set node5 [$ns node]

# Create links between nodes

$ns duplex-link $node1 $node2 10Mb 10ms DropTail

$ns duplex-link $node2 $node3 10Mb 10ms DropTail

$ns duplex-link $node3 $node4 10Mb 10ms DropTail

$ns duplex-link $node4 $node5 10Mb 10ms DropTail

$ns duplex-link $node1 $node5 5Mb 50ms DropTail

$ns duplex-link $node2 $node4 5Mb 30ms DropTail

# Create Flooding Agent

Agent/Flood set packetSize_ 512   ;# Set the size of packets to be flooded

# Create a custom Flooding agent

Class Agent/Flood -superclass Agent

Agent/Flood instproc init {} {

# Initialization

$self next

$self set seqno_ 0  ;# Sequence number to prevent duplicate forwarding

$self set seen_ {}  ;# Set to track seen packets

}

Agent/Flood instproc recv {pkt} {

# Process received packets

global ns

set node [$self node]

set seqno [$pkt set seqno_]

# Check if the packet has already been processed (to prevent loops)

if {[$self seen? $seqno]} {

return  ;# Do not forward the packet again

}

# Mark the packet as seen

$self add-seen $seqno

# Forward the packet to all neighbors except the sender

foreach neighbor [$node neighbors] {

if {[$neighbor id] != [$pkt set src_]} {

set fwd_pkt [$pkt copy]

$ns sendto-neighbor $neighbor $fwd_pkt

}

}

}

# Function to check if a packet has been seen

Agent/Flood instproc seen? {seqno} {

return [info exists seen_($seqno)]

}

# Function to add packet to the seen list

Agent/Flood instproc add-seen {seqno} {

set seen_($seqno) 1

}

# Attach the Flooding agent to the nodes

set flood1 [new Agent/Flood]

$ns attach-agent $node1 $flood1

set flood2 [new Agent/Flood]

$ns attach-agent $node2 $flood2

set flood3 [new Agent/Flood]

$ns attach-agent $node3 $flood3

set flood4 [new Agent/Flood]

$ns attach-agent $node4 $flood4

set flood5 [new Agent/Flood]

$ns attach-agent $node5 $flood5

# Start flooding by sending a packet from node1

$ns at 1.0 “$flood1 send”

# Stop the simulation at 10.0 seconds

$ns at 10.0 “finish”

# Run the simulation

$ns run

Explanation of the Script

  1. Network Topology: We can make a basic five-node network with duplex links among the nodes, each with various latency, and bandwidths. It is to replicate the connected network in which packets can forward among the nodes.
  2. Flooding Agent: We describe a custom flooding agent like Agent/Flood, where responsible for the forwarding packets. This agent uses a sequence number that is seqno_ to the track packets. Make sure that packets are not forwarded more than once. The seen_ variable maintain track of packets where already been processed to prevent the loops.
  3. Packet Forwarding: Once a packet is received by a node that verifies if it has already processed the packet according to the sequence number. Unless, the packet is sent to every node’s neighbours excepting the one from that the packet was received.
  4. Traffic Generation: The replication begins with node1 forwarding a packet that will be flooded via the network by the other nodes.
  5. Simulation Time: The simulation runs for the 10 seconds.

Running the Simulation

  1. We can save the script to a file like flooding_routing.tcl.
  2. Run the script using the simulator NS2:

ns flooding_routing.tcl

  1. A trace file (out.tr) and Network Animator (NAM) file (out.nam) are generated by the simulation. We can visualize the simulation using NAM to monitor how packets are forwarded by every node and flooded via the network.

Analyse the Results

  • Trace File: Investigate the trace file (out.tr) to check that packets are flooded via the network and received by every nodes.
  • NAM Visualization: We can use the NAM to visualize the network topology and monitor how the packet is forwarded via the network from the source to every neighbours.

Enhancements

  1. Time-to-Live (TTL): We can append the TTL field to limit how many hops a packet can be forwarded and it avoids endless flooding loops and decreases the redundant transmissions.
  2. Sequence Numbers: The present execution uses sequence numbers that to prevent the loops. We can expand this numbers by appending the timestamps or other identifiers for more furthered control.
  3. Network Size: Investigate the flooding procedure including a larger network to test how well the flooding propagates through the network.
  4. Reliability: Execute acknowledgment-based flooding in which the nodes acknowledge receipt of a flooded packet to maximize reliability in noisy or lossy networks.

In the end, you get more information and clearly know how to execute and analyse the Flooding Routing using the ns2 simulation with the help of the above procedure. Additional procedures and concepts will be offered in another manual, if required.

Any challenges with implementing Flooding Routing in NS2 are tackled by us, feel free to reach out to us, and we will provide you with the best solutions.