How to Implement Flow Based Routing in NS2

To implement the Flow-based routing in NS2 (Network Simulator 2) that can be executed by manually set up the routing according to the traffic flows instead of using the dynamic routing protocols like AODV, DSR, or DSDV. It permits routes to be determined rely on particular flows, such as various kinds of traffic, quality of service (QoS), or other flow identifiers.

Key Steps to Implement Flow-Based Routing in NS2:

It has two key methods to simulate flow-based routing in NS2:

  1. Manual Configuration of Flows and Routes: We can manually specify the routes for various kinds of traffic (flows) in the TCL script.
  2. Custom C++ Implementation: For more developed flow-based routing we can change the NS2’s C++ code to describe how traffic flows are managed and routed.
  1. Manual Configuration of Flow-Based Routing in TCL

In this method, we can manually state that the static routing tables and stipulate how various flows like UDP, TCP traffic are routed via the network.

Example TCL Script for Flow-Based Routing:

# Create the simulator object

set ns [new Simulator]

# Open trace file to log simulation events

set tracefile [open flow_based_trace.tr w]

$ns trace-all $tracefile

# Open NAM file for visualization

set namfile [open flow_based_routing.nam w]

$ns namtrace-all $namfile

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam flow_based_routing.nam &

exit 0

}

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

# Define links between nodes

$ns duplex-link $n0 $n1 2Mb 10ms DropTail

$ns duplex-link $n1 $n2 2Mb 10ms DropTail

$ns duplex-link $n2 $n3 2Mb 10ms DropTail

$ns duplex-link $n3 $n4 2Mb 10ms DropTail

$ns duplex-link $n0 $n4 1Mb 20ms DropTail  ;# Lower bandwidth link

# Define static routes based on traffic flow

# For UDP flow from n0 to n4, take the path n0 -> n1 -> n2 -> n3 -> n4

$n0 add-route $n4 $n1

$n1 add-route $n4 $n2

$n2 add-route $n4 $n3

$n3 add-route $n4 $n4

# For TCP flow, take a different route: n0 -> n4 directly (even if it’s a slower link)

$n0 add-route $n4 $n4

# Create UDP agents for flow 1 (n0 -> n4 via intermediate nodes)

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

set null0 [new Agent/Null]

$ns attach-agent $n4 $null0

# Connect UDP agent to the null agent

$ns connect $udp0 $null0

# Create CBR traffic over UDP (flow 1)

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set interval_ 0.2

$cbr0 attach-agent $udp0

# Create TCP agents for flow 2 (direct path n0 -> n4)

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

set sink0 [new Agent/TCPSink]

$ns attach-agent $n4 $sink0

# Connect TCP agent to the sink agent

$ns connect $tcp0 $sink0

# Create FTP traffic over TCP (flow 2)

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

# Schedule traffic for the flows

$ns at 0.5 “$cbr0 start”

$ns at 1.0 “$ftp0 start”

$ns at 4.0 “$cbr0 stop”

$ns at 5.0 “$ftp0 stop”

# Schedule end of the simulation

$ns at 6.0 “finish”

# Run the simulation

$ns run

  1. Explanation of the Script:
  • Flow-based Routing:
    • UDP Flow (Flow 1): The script stipulates that the UDP traffic from n0 to n4 would get the longer however higher bandwidth path: n0 -> n1 -> n2 -> n3 -> n4. This mimics a flow-based routing decision in which the path is ascertained depends on the nature of the flow (UDP).
    • TCP Flow (Flow 2): TCP traffic from n0 to n4 takes a direct path that is lower bandwidth nevertheless it may be chosen for connection-oriented flows such as TCP.
  • Static Routing: The add-route command is used to manually describe the paths for the particular flows. UDP and TCP traffic are routed along various ways, in this situation.
  • Traffic Setup: CBR traffic is forward through the UDP in flow 1, whereas FTP traffic is sent through TCP in flow 2.
  1. Running the Simulation:
  1. We can save the script as flow_based_routing.tcl.
  2. Run the simulation using:

ns flow_based_routing.tcl

  1. It will make a trace file (flow_based_trace.tr) and a NAM file (flow_based_routing.nam) for the visualization. We can use the given command to visualize the simulation:

nam flow_based_routing.nam

  1. Custom C++ Implementation of Flow-Based Routing in NS2:

For more difficult flow-based routing, we may need to change the NS2 C++ source code to dynamically route flows according to the flow identifiers like:

  • Source and destination IP addresses
  • Protocol type like TCP, UDP
  • QoS requirements
  • Traffic type such as streaming, file transfer

Steps for Custom C++ Flow-Based Routing Implementation:

  1. Identify the Routing Layer in NS2: We can change the routing behaviour in the source code NS2 documents placed in ~/ns-2.x/queue/route.cc or ~/ns-2.x/tcl/lib/ns-lib.tcl.
  2. Implement Flow Identification: Change the routing layer to classify and find the flows depends on the packet headers such as IP, TCP/UDP, etc. Then create a routing decisions consequently.
  3. Define Flow-Based Routing Logic: Execute a custom flow-based routing procedure which allocates various routes rely on the identified flows. For sample, we can route high-priority traffic via low-delay paths and best-effort traffic over higher-delay paths.
  4. Compile NS2: Afterwards, changing the source code, recompile NS2 using the below code:

/configure

make clean

make

We had showed comprehensive details, simple approaches with examples to setup the routing and execute the Flow based routing in the NS2 environment. We will deliver extra details based on your needs. Contact us if you encounter any issues with implementing Flow Based Routing in NS2; we are here to provide you with the best solutions.