How to Implement Network Architecture in NS2

To implement the Network Architecture within the NS2 (Network Simulator 2) that has includes creating and replicating performance and also structure of a network with its nodes, protocols, links, and traffic patterns. This approach permits to evaluate and assess the various network designs, measure the performance metrics, and experiment with numerous sets up without the requirement for physical hardware. The followings are complete procedure to executing it in NS2 that finish with instance OTcl (Object Tcl) scripts to show every step.

Step-by-Step Implementation:

  1. Understanding Network Architecture in NS2

Before split into the execution these critical to know what constitutes a network architecture in NS2:

  • Nodes: These devices are in the network (e.g., computers, routers, servers).
  • Links: Connections among the nodes (wired or wireless).
  • Protocols: Rules are major communication (e.g., TCP, UDP, routing protocols).
  • Traffic Patterns: Data flow among the nodes (e.g., CBR, FTP, VoIP).
  • Applications: Services running on nodes (e.g., HTTP servers, FTP clients).

The simulation environment NS2 uses OTcl scripts to describe and set up these components. Comprehending how to script in OTcl is vital for efficient simulation.

  1. Prerequisites
  • NS2 Installation: Make sure NS2 is installed on the machine and we can download it from NS2 Official Website.
  • Basic Knowledge of OTcl: Familiarity with OTcl scripting language used by the simulation platform NS2.
  • Understanding of Networking Concepts: Understanding of network topologies, protocols, and traffic patterns.
  1. Step 1: Setting Up the Basic Network Topology

The initial stage is to describe the links and nodes, which constitute the network.

Example Scenario:

  • Nodes: 4 Nodes are Node0, Node1, Node2, Node3
  • Topology: Completely connected mesh in which each node is connected to every other node.

OTcl Script:

# Initialize the simulator

set ns [new Simulator]

# Open the trace file

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Open the NAM trace file for visualization

set namfile [open out.nam w]

$ns namtrace-all $namfile

# Create nodes

set node0 [$ns node]

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

# Define the positions of nodes for visualization (optional)

$node0 set X_ 100

$node0 set Y_ 100

$node1 set X_ 200

$node1 set Y_ 100

$node2 set X_ 100

$node2 set Y_ 200

$node3 set X_ 200

$node3 set Y_ 200

# Create duplex links between all pairs of nodes (fully connected mesh)

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

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

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

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

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

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

Explanation:

  • Simulator Initialization: Creates a new NS2 simulator instance.
  • Trace Files: Opens files to record simulation events (out.tr) and visualization data (out.nam).
  • Node Creation: Defines four nodes.
  • Link Creation: Establishes duplex (two-way) links between each pair of nodes with a bandwidth of 10 Mbps and a delay of 10 ms using the DropTail queue management algorithm.
  1. Step 2: Configuring Node Properties

Nodes can tailor with numerous properties like queue sizes, buffer types, and link-layer protocols.

Example: Configuring Queue Sizes

# Set queue sizes for all links

$ns queue-limit $node0 $node1 50    ;# 50 packets buffer

$ns queue-limit $node0 $node2 50

$ns queue-limit $node0 $node3 50

$ns queue-limit $node1 $node2 50

$ns queue-limit $node1 $node3 50

$ns queue-limit $node2 $node3 50

Explanation:

  • queue-limit: Sets the large number of packets, which can queue on a link before packets are fell.
  1. Step 3: Defining Link Properties

Link properties like bandwidth, delay, and queue type significantly influence the network performance.

Example: Different Link Configurations

# Link with higher bandwidth and lower delay

$ns duplex-link $node0 $node1 100Mb 5ms DropTail

# Link with lower bandwidth and higher delay

$ns duplex-link $node1 $node2 1Mb 20ms DropTail

Explanation:

  • Bandwidth: Ascertains the data rate of the link (e.g., 100 Mbps, 1 Mbps).
  • Delay: The time it takes for a packet to traverse the link (e.g., 5 ms, 20 ms).
  • Queue Type: The queue management algorithm (e.g., DropTail, RED).
  1. Step 4: Implementing Routing Protocols

Routing protocols are establish how data packets are traverse the network from origin to end. NS2 supports numerous routing protocols, with AODV, DSDV, OLSR, and more.

Example: Implementing the AODV Routing Protocol

# Set the routing protocol to AODV

set val(rp) AODV

$ns node-config -adhocRouting $val(rp)

Explanation:

  • set val(rp) AODV: Allocates the AODV routing protocol.
  • node-config: Applies the routing protocol configuration to every nodes.

Alternative Routing Protocols:

  • DSDV: Distance Vector Routing
  • OLSR: Optimized Link State Routing
  • DHRP: Dynamic Host Routing Protocol

Example: Using DSDV Routing Protocol

set val(rp) DSDV

$ns node-config -adhocRouting $val(rp)

  1. Step 5: Setting Up Traffic Patterns

Traffic patterns are describe how data are drops among the nodes and the simulation platform NS2 delivers the numerous traffic generators like CBR (Constant Bit Rate), FTP, Telnet, and more.

Example Scenario:

  • Node0 transfers the CBR traffic to Node3.
  • Node1 sends FTP traffic to Node2.

OTcl Script:

# Create TCP and UDP agents

set tcp [new Agent/TCP]

set sink [new Agent/TCPSink]

$ns attach-agent $node0 $tcp

$ns attach-agent $node3 $sink

$ns connect $tcp $sink

set udp [new Agent/UDP]

set null [new Agent/Null]

$ns attach-agent $node1 $udp

$ns attach-agent $node2 $null

$ns connect $udp $null

# Create CBR traffic from Node0 to Node3

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $tcp

$cbr set packetSize_ 500

$cbr set interval_ 0.005   ;# 200 packets per second (1/0.005)

# Create FTP traffic from Node1 to Node2

set ftp [new Application/FTP]

$ftp attach-agent $tcp ;# Note: FTP uses TCP

# Start CBR traffic at 1.0 seconds

$ns at 1.0 “$cbr start”

$ns at 5.0 “$cbr stop”

# Start FTP traffic at 2.0 seconds

$ns at 2.0 “$ftp start”

$ns at 10.0 “$ftp stop”

Explanation:

  • Agents:
    • TCP Agent: It manages the TCP connections.
    • UDP Agent: Handles UDP connections.
    • TCPSink: Receives TCP traffic.
    • Null Agent: Removes the received packets (useful for simulating packet sinks).
  • Applications:
    • CBR: Makes a constant bit rate traffic flow.
    • FTP: Replicates the FTP file transfers using TCP.
  • Traffic Configuration:
    • Packet Size: The size of every packet in the bytes.
    • Interval: Time among the consecutive packets.
  1. Step 6: Applying Application Layer Protocols

Application layer protocols are replicate the performance of the applications running on network nodes, like web browsing, file transfers, or voice calls.

Example: Simulating HTTP Traffic

# Create a TCP agent for HTTP

set tcp_http [new Agent/TCP]

set sink_http [new Agent/TCPSink]

$ns attach-agent $node0 $tcp_http

$ns attach-agent $node2 $sink_http

$ns connect $tcp_http $sink_http

# Create an HTTP application

set http [new Application/FTP]

$http attach-agent $tcp_http

# Start HTTP traffic at 3.0 seconds

$ns at 3.0 “$http start”

$ns at 8.0 “$http stop”

Explanation:

  • FTP Application: In the platform NS2, the FTP application can repurpose to mimic the HTTP traffic by connecting it to a TCP agent.
  1. Step 7: Running the Simulation and Collecting Results

When the network architecture, traffic patterns and protocols are described then we can run the simulation and gather the outcomes for analysis.

Completing the OTcl Script:

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}

# Schedule the finish procedure

$ns at 20.0 “finish”

# Run the simulation

$ns run

Explanation:

  • finish Procedure: Closes the trace files and introduces the NAM visualization tool after the simulation completes.
  • Simulation Duration: Set to 20 seconds in this sample.

Running the Simulation:

We can save the OTcl script (e.g., network_simulation.tcl) then implement it using the NS2 simulator:

ns network_simulation.tcl

  1. Step 8: Visualizing the Network with NAM

The NAM (Network Animator) delivers a graphical interface to envision the simulation and envision the node movements, packet transmissions, and protocol performance.

Launching NAM:

The finish process in the OTcl script automatically introduces the NAM at the destination of the simulation. If we want to see NAM separately, use:

nam out.nam

NAM Features:

  • Node Representation: Visualizes these nodes with their positions.
  • Link Representation: Displays active links and their properties.
  • Packet Flow: Animates packet movements among the nodes.
  • Protocol Visualization: Shows the protocol actions (e.g., routing updates).

Example NAM Visualization:

Note: Substitute the above link with real image if required.

  1. Advanced Configurations

For more difficult network architectures, consider executing the following:

  1. Hierarchical Network Topology

Hierarchical topologies are contain various layers, like core, aggregation, and access layers, are usually used in large-scale networks.

OTcl Example:

# Create core nodes

set core0 [$ns node]

set core1 [$ns node]

# Create aggregation nodes

set agg0 [$ns node]

set agg1 [$ns node]

set agg2 [$ns node]

set agg3 [$ns node]

# Create access nodes

set access0 [$ns node]

set access1 [$ns node]

set access2 [$ns node]

set access3 [$ns node]

# Connect core to aggregation

$ns duplex-link $core0 $agg0 100Mb 5ms DropTail

$ns duplex-link $core1 $agg1 100Mb 5ms DropTail

# Connect aggregation to access

$ns duplex-link $agg0 $access0 10Mb 10ms DropTail

$ns duplex-link $agg0 $access1 10Mb 10ms DropTail

$ns duplex-link $agg1 $access2 10Mb 10ms DropTail

$ns duplex-link $agg1 $access3 10Mb 10ms DropTail

  1. Wireless Network Topology

Executing wireless nodes with mobility models.

OTcl Example:

# Define a wireless channel

set channel [new Channel/WirelessChannel]

set phy [new Phy/WirelessPhy]

set mac [new Mac/802_11]

set ifq [new Queue/DropTail/PriQueue]

set ll [new LL]

# Create wireless nodes

set wnode0 [$ns node]

set wnode1 [$ns node]

set wnode2 [$ns node]

# Configure wireless node properties

$wnode0 set X_ 100

$wnode0 set Y_ 100

$wnode1 set X_ 200

$wnode1 set Y_ 100

$wnode2 set X_ 150

$wnode2 set Y_ 200

# Create wireless links

$ns duplex-link $wnode0 $wnode1 2Mb 10ms Wireless

$ns duplex-link $wnode1 $wnode2 2Mb 10ms Wireless

$ns duplex-link $wnode0 $wnode2 2Mb 10ms Wireless

  1. Implementing Mobility Models

Replicating node movements using models such as Random Waypoint, Gauss-Markov, and so on.

Example: Random Waypoint Mobility

# Define the random waypoint procedure

proc random_waypoint {node max_x max_y} {

set x [expr rand() * $max_x]

set y [expr rand() * $max_y]

set speed [expr 5 + rand() * 15]   ;# Speed between 5 and 20 m/s

set pause [expr rand() * 5]       ;# Pause between 0 and 5 seconds

$node setdest $x $y $speed

$ns at [expr $pause + $ns now] “random_waypoint $node $max_x $max_y”

}

# Initialize mobility for nodes

$ns at 1.0 “random_waypoint $node0 500 500”

$ns at 1.0 “random_waypoint $node1 500 500”

$ns at 1.0 “random_waypoint $node2 500 500”

$ns at 1.0 “random_waypoint $node3 500 500”

  1. Incorporating Quality of Service (QoS)

Highlighting the traffic types using various queue management algorithms such as RED, WFQ, etc.

Example: Implementing RED Queue Management

# Define RED queue with specific parameters

set red_queue [new Queue/RED]

$red_queue set link_ [ $node0 $node1 ]

$red_queue set queueSize_ 100

$red_queue set minthresh_ 20

$red_queue set maxthresh_ 50

$red_queue set maxpmark_ 1

# Apply RED queue to a link

$ns attach-queue $node0 $node1 $red_queue

In this framework, we declared that how to execute and setup the Network Architecture using the above implementation procedure in NS2 simulation platform. We will provide further details as required.

ns2project.com provides amazing project ideas that fit your research interests. We’re ready to assist you with quick help for putting Network Architecture into practice using NS2 tool.