Skip to main content

Wired Networks in NS2 - NS2 Tutorial 5

Wired Networks

In this post:

·       Introduction to Wired networks

·       Tcl scripts for various wired networks

Wired networks are easier to setup as they need a physical link between the nodes and hence they form a network by having a duplex or simplex link. Also we have seen an example of a two node network in Chapter 1 that informs how to connect two nodes in a network with a duplex link.

This chapter tells about the various wired network supported in ns2 and examples to showcase these networks.

This wired example sets the shape for the nodes and also sets labels for the node. Each node can be differentiated using labels like client, server. Etc. There are 3 shapes supported by ns2, they are hexagon, square and circle. Once the shape is fixed, the shapes can't be changed during the simulation.

$node shape hexagon

Also the nodes can be colored using the coloring values, there are so many colors supported by ns and some of them are blue, red, green, orange, magenta, etc.

$node color name

the following examples depicts three nodes that are either colored,shaped and labelled. The following network does not have any traffic generation or simulation. Its just informs the attributes of the node.

Sometimes the node can be marked with a color and shape with a name. The syntax is

$node add-mark name color shape

Listing 4.1 – To set the attributes of the node like shape, color, label, marking.

 

#Create global variables

set ns [new Simulator]

 

#setting nam trace

set namf [open wired1.nam w]

$ns namtrace-all $namf

 

#open the trace file

set tracef [open wired1.tr w]

$ns trace-all $tracef

 

set proto rlm

#setting the color values

$ns color 1 blue

$ns color 2 yellow

$ns color 3 red

 

#---------- creating client- router- end server node----------------#

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

 

#establish the link between the nodes

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

$ns duplex-link $n1 $n2 200Kb 100ms DropTail

 

#Label the nodes

$ns at 0.0 "$n0 label Client1"

$ns at 0.0 "$n1 label Server"

$ns at 0.0 "$n2 label Client2"

 

#setting the color for nodes

$n0 color blue

$n1 color red

$n2 add-mark pradeep green square

#Shaping the nodes for differentiation

$n1 shape hexagon

$n2 shape square

 

#finish procedure

proc finish {} {

global ns tracef namf

$ns flush-trace

close $tracef

close $namf

puts "Opening nam..."

exec nam wired1.nam &

exit 0

}

 

#Calling finish procedure

$ns at 2.0 "finish"

$ns run

The above listing just shows two clients and a server and there was no traffic and it is just to create a topology and Fig 4.1 shows this

A simple Wired network with three nodes
A simple Wired network with three nodes

Tcp Source and the Sink Agent

This example shows the simple Tcp with FTP Traffic. Since in wired networks, Tcp is a connection oriented transport protocol which needs an acknowledgment for every packet it sents. The following network shows that. There are 4 nodes, and two nodes are exchanging packets between them and an intermediate node is forwarding the packet to the destination node. The performance characteristics of this network is analysed in the next section. 

The C++ source code relevant to Tcp is available in the ~ns-2.35/tcp/tcp.cc. Also it involves various parameters during the simulation like congestion window, duplicate acknowledgement, and sequence no, etc.

Also this network used a FTP (File Transfer Protocol) for the traffic.

Listing 4.2 – Tcl Script to show the TCP with FTP Traffic

#Set the simulator

set ns [new Simulator]

 

#Opening the network animation

set namf [open wired2.nam w]

$ns namtrace-all $namf

#open the file for tracing

set tracef [open wired2.tr w]

$ns trace-all $tracef

#creation of wired nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#establish the links between the nodes with bandwidth and delay

$ns duplex-link $n0 $n1 2MB 1ms DropTail

$ns duplex-link $n1 $n2 2.5MB 1ms RED

$ns duplex-link $n2 $n3 2MB 1.5ms DropTail

$ns duplex-link $n3 $n1 12MB 10ms DropTail

 

#creating the Tcp source and sink agents

set tcp [new Agent/TCP]

set sink [new Agent/TCPSink]

 

#attach the agents to the corresponding nodes

$ns attach-agent $n0 $tcp

$ns attach-agent $n2 $sink

 

#create the FTP Traffic

set ftp [new Application/FTP]

$ftp attach-agent $tcp

 

$ns connect $tcp $sink

 

#start the traffic

$ns at 1.0 "$ftp start"

 

#end the simulation

$ns at 3.0 "finish"

 

proc finish {} {

     global ns namf tracef

     $ns flush-trace

     close $namf

     close $tracef

     exec nam wired2.nam &

     exit 0

}

$ns run

 

A Wired network with Four nodes with FTP Traffic
A Wired network with Four nodes with FTP Traffic

The Listing 4.2 shows a simple wired network with four nodes that just involves in a FTP Traffic.  The Fig 4.2 shows this.


Wired network with various traffic

NS supports various traffic that may be employed in Tcl scripts to test. Some examples are

       CBR Traffic

       FTP Traffic

       Exponential Traffic

       Telnet Traffic

       SMTP

These traffic can be simulated in TCP or UDP Agents.

 

The following Listing depicts these traffics. For example, SMTP (Simple Mail Transfer protocol) traffic is more like exponential traffic and this can be simulated using Application/Traffic/Exponential.

 

Listing 4.3 – Tcl Script to show various traffics in the network

There are totally 10 wired nodes that shares duplex links between them. Four of the links are enabled with ftp, cbr,smtp and telnet traffic. The following listing shows the simulation with the snapshot of the animation.

 

#Set the global variable

set ns [new Simulator]

#create the file for animation

set namf [open wired3.nam w]

$ns namtrace-all $namf

 

#create the file for tracing

set tracef [open wired3.tr w]

$ns trace-all $tracef

#creation of nodes using for loop

for {set i 0} {$i < 10} {incr i} {

     set node($i) [$ns node]

}

#establishing the duplex links between the nodes

$ns duplex-link $node(0) $node(1) 2mb 1ms DropTail

$ns duplex-link $node(2) $node(3) 1mb 10ms DropTail

$ns duplex-link $node(1) $node(6) 3mb 10ms DropTail

$ns duplex-link $node(4) $node(3) 2mb 50ms DropTail

$ns duplex-link $node(8) $node(1) 3mb 25ms DropTail

$ns duplex-link $node(6) $node(4) 1mb 30ms DropTail

$ns duplex-link $node(7) $node(5) 2mb 15ms DropTail

$ns duplex-link $node(8) $node(4) 20mb 4ms DropTail

$ns duplex-link $node(2) $node(5) 2mb 100ms DropTail

$ns duplex-link $node(0) $node(9) 12mb 10ms DropTail

$ns duplex-link $node(0) $node(3) 20mb 14ms DropTail

$ns duplex-link $node(9) $node(7) 20mb 4ms DropTail

#define the procedure for smtp traffic with two nodes as parameters

proc traffic_smtp {n0 n1} {

global ns

set smtpsource [new Agent/UDP]

set smtpsink [new Agent/UDP]

$ns attach-agent $n0 $smtpsource

$ns attach-agent $n1 $smtpsink

$ns connect $smtpsource $smtpsink

set smtp_traffic [new Application/Traffic/Exponential]

$smtp_traffic attach-agent $smtpsource

$smtp_traffic set burst_time_ 50ms

$smtp_traffic set idle_time_ 50ms

$smtp_traffic set rate_ 100k

$smtp_traffic set packetSize_ 100

$ns at 0.1 "$smtp_traffic start"

$ns at 1.0 "$smtp_traffic stop"

}

#call the procedure

traffic_smtp $node(0) $node(1)

#define the procedure for CBR traffic with two nodes as parameters

proc traffic_cbr {n2 n3} {

global ns

set cbrsource [new Agent/UDP]

set cbrsink [new Agent/Null]

$ns attach-agent $n2 $cbrsource

$ns attach-agent $n3 $cbrsink

$ns connect $cbrsource $cbrsink

set cbr_traffic [new Application/Traffic/CBR]

$cbr_traffic attach-agent $cbrsource

$cbr_traffic set interval_ 50

$cbr_traffic set packetSize_ 50

$ns at 1.0 "$cbr_traffic start"

$ns at 2.0 "$cbr_traffic stop"

}

#call the CBR Traffic procedure

traffic_cbr $node(2) $node(3)

#define the procedure for FTP traffic with two nodes as parameters

proc traffic_ftp {n1 n6} {

global ns

set ftpsource [new Agent/TCP]

set ftpsink [new Agent/TCPSink]

$ns attach-agent $n1 $ftpsource

$ns attach-agent $n6 $ftpsink

$ns connect $ftpsource $ftpsink

set ftp_traffic [new Application/FTP]

$ftp_traffic attach-agent $ftpsource

$ftp_traffic set interval_ 50

$ftp_traffic set packetSize_ 50

$ns at 2.0 "$ftp_traffic start"

$ns at 3.0 "$ftp_traffic stop"

}

#call the ftp traffic procedure

traffic_ftp $node(1) $node(6)

#define the procedure for Telnet traffic with two nodes as parameters

proc traffic_telnet {n8 n1} {

global ns

set telnetsource [new Agent/TCP]

set telnetsink [new Agent/TCPSink]

$ns attach-agent $n8 $telnetsource

$ns attach-agent $n1 $telnetsink

$ns connect $telnetsource $telnetsink

set telnet_traffic [new Application/Telnet]

$telnet_traffic attach-agent $telnetsource

$telnet_traffic set interval_ 0.005

$ns at 3.0 "$telnet_traffic start"

$ns at 4.0 "$telnet_traffic stop"

}

#call the telnet traffic

traffic_telnet $node(8) $node(1)

#Trace annotation during the animation. Theese string will be displayed in the NAM window

$ns at 0.1 "$ns trace-annotate \"Time: 0.1 SMTP Traffic from Node 0 to Node 1 ..\""

$ns at 1.0 "$ns trace-annotate \"Time: 1.0 CBR Traffic from Node 2 to Node 3 ..\""

$ns at 2.0 "$ns trace-annotate \"Time: 2.0 FTP Traffic from Node 1 to Node 6..\""

$ns at 3.0 "$ns trace-annotate \"Time: 3.0 Telnet Traffic from Node 8 to Node 1..\""

#call the procedure finish

$ns at 5.0 "finish"

#creation of finish procedure

proc finish {} {

     global ns namf tracef

     $ns flush-trace

     close $namf

     close $tracef

}

$ns run

 

Wired network with CBR, SMTP, Telnet and FTP Traffic
Wired network with CBR, SMTP, Telnet and FTP Traffic

The Listing 4.3 shows some new things in ns, they are

       Different applications like

       FTP Traffic - Application/FTP

       CBR Traffic - Application/Traffic/CBR

       SMTP Traffic - Application/Traffic/Exponential

       Telnet Traffic - Application/Telnet

       This script uses the trace-annotate command that uses to display string during the animation as shown in Fig 4.3

       This script also uses procedure with parameters. Four procedures were used with two parameters as the node names.

       Also node creation is done using For loop and the nodes are named as array values. Ex. node0, node1, etc.

Printing the properties of Tcp Reno Agent

The following listing prints the congestion window of the TcpReno agent and plots it using xgraph. Xgraph is third party software that comes with the ns allinone package.

 

Congestion window size is a major factor in predicting the performance of a TCP Network. NS2 handles the Congestion window using a variable cwnd_ and it can be printed to a graph and as well printed as values at various intervals of time.

The following Tcl script will let you understand the working of the network (TCP Reno Protocol) and also helps you in plotting the characteristics of Congestion window value.

Listing 4.4 – Tcl Script to print the Congestion Window of  TcpReno Agent.

#create simulator

set ns [new Simulator]       

#to create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

# to create the link between the nodes with bandwidth, delay and queue

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

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

$ns duplex-link $n2 $n3 0.3Mb 200ms DropTail

$ns duplex-link $n3 $n4 0.5Mb  40ms DropTail

$ns duplex-link $n3 $n5 0.5Mb  30ms DropTail

# Sending node is 0 with agent as Reno Agent

set tcp1 [new Agent/TCP/Reno]

$ns attach-agent $n0 $tcp1

# receiving (sink) node is n4

set sink1 [new Agent/TCPSink]

$ns attach-agent $n4 $sink1

# establish the traffic between the source and sink

$ns connect $tcp1 $sink1

# Setup a FTP traffic generator on "tcp1″

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$ftp1 set type_ FTP              

# start/stop the traffic

$ns at 0.1  "$ftp1 start"

$ns at 40.0 "$ftp1 stop"

 

# Set simulation end time

$ns at 50.0 "finish"           

# procedure to plot the congestion window

proc plotWindow {tcpSource outfile} {

   global ns

   set now [$ns now]

   set cwnd [$tcpSource set cwnd_]

# the data is recorded in a file called congestion.xg (this can be plotted # using xgraph or gnuplot. this example uses xgraph to plot the cwnd_

   puts  $outfile  "$now $cwnd"

   $ns at [expr $now+0.1] "plotWindow $tcpSource  $outfile"

}

set outfile [open  "congestion.xg"  w]

$ns  at  0.0  "plotWindow $tcp1  $outfile"

proc finish {} {

     exec xgraph congestion.xg -geometry 300×300 &

     exit 0

}

# Run simulation

$ns run

 

 


Congestion window of a Tcp Reno Agent
Congestion window of a Tcp Reno Agent

The listing 4.4 has the facility of printing the congestion window in a graph and Fig 4.4 shows this. As in Fig 4.4, the congestion window is initially at a value of 1 and increases periodically over a period of time.

 

Listing 4.5 – Wired network

# enable the multicast routing protocol in ns2

set ns [ new Simulator -multicast on]

 

# Creating network animation

set namf [open wired3.nam w]

$ns namtrace-all $namf

 

#Open the trace file

set tracef [open wired3.tr w]

$ns trace-all $tracef

 

#define the multicast routing protocol

set proto rlm

 

#------------COLOR DESCRIPTION---------------#

$ns color 1 dodgerblue

$ns color 2 red

$ns color 3 cyan

$ns color 4 green

$ns color 5 yellow

$ns color 6 black

$ns color 7 magenta

$ns color 8 gold

$ns color 9 red

 

# --------- CREATING SENDER - RECEIVER - ROUTER NODES-----------#

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set m1 [$ns node]

set m2 [$ns node]

set m3 [$ns node]

set m4 [$ns node]

set Router1 [$ns node]

set Router2 [$ns node]

set Router3 [$ns node]

 

 

$ns duplex-link $n1 $Router1 1Mb 10ms DropTail

$ns duplex-link $n2 $Router1 500Kb 10ms DropTail

$ns duplex-link $n3 $Router1 750Kb 10ms DropTail

$ns duplex-link $n4 $Router2 1Mb 10ms DropTail

$ns duplex-link $m1 $Router1 1Mb 10ms DropTail

$ns duplex-link $m2 $Router1 1Mb 10ms DropTail

$ns duplex-link $m3 $Router1 1Mb 10ms DropTail

$ns duplex-link $m4 $Router3 1Mb 10ms DropTail

$ns duplex-link $Router2 $Router1 1Mb 10ms DropTail

$ns duplex-link $Router2 $Router3 1Mb 10ms DropTail

$ns duplex-link $Router1 $Router3 1Mb 10ms DropTail

 

# queue size, beyond this size, the packets will be dropped

$ns queue-limit $Router1 $Router2 20

$ns queue-limit $Router1 $Router3 20

$ns queue-limit $Router2 $Router1 20

$ns queue-limit $Router3 $Router1 25

 

# label the nodes as per their nature of work

$ns at 0.0 "$n1 label Client1"

$ns at 0.0 "$n2 label Client2"

$ns at 0.0 "$n3 label Client3"

$ns at 0.0 "$n4 label Client4"

$ns at 0.0 "$m1 label Client5"

$ns at 0.0 "$m2 label Client6"

$ns at 0.0 "$m3 label Client7"

$ns at 0.0 "$m4 label Client8"

$ns at 0.0 "$Router1 label Router1"

$ns at 0.0 "$Router2 label Router2"

$ns at 0.0 "$Router3 label Router3"

 

# defining the shapes for the nodes

$Router1 shape square

$Router2 shape square

$Router3 shape square

 

#setting the colors for the links

$ns duplex-link-op $Router1 $Router2 color cyan

$ns duplex-link-op $Router1 $Router3 color cyan

$ns duplex-link-op $Router2 $Router3 color cyan

 

# establishing the connection and the agents

#includes maximum congestion window, the packetSize and flow id

set tcp1 [$ns create-connection TCP $n1 TCPSink $m4 1]    

$tcp1 set class_ 1    

$tcp1 set maxcwnd_ 16    

$tcp1 set packetsize_ 4000     

$tcp1 set fid_ 1    

set ftp1 [$tcp1 attach-app FTP]    

$ftp1 set interval_ .005    

$ns at 0.2 "$ftp1 start"    

$ns at 4.0 "$ftp1 stop"                            

 

set tcp2 [$ns create-connection TCP $n2 TCPSink $m3 1]    

$tcp2 set class_ 1    

$tcp2 set maxcwnd_ 16    

$tcp2 set packetsize_ 4000    

$tcp2 set fid_ 2    

set ftp2 [$tcp2 attach-app FTP]    

$ftp2 set interval_ .005    

$ns at 0.7 "$ftp2 start"    

$ns at 4.0 "$ftp2 stop"               

 

set tcp3 [$ns create-connection TCP $n3 TCPSink $m2 1]    

$tcp3 set class_ 1    

$tcp3 set maxcwnd_ 16    

$tcp3 set packetsize_ 4000    

$tcp3 set fid_ 3    

set ftp3 [$tcp3 attach-app FTP]    

$ftp3 set interval_ .005    

$ns at 1.2 "$ftp3 start"    

$ns at 4.0 "$ftp3 stop"                

 

set tcp4 [$ns create-connection TCP $n4 TCPSink $m1 1]    

$tcp4 set class_ 1    

$tcp4 set maxcwnd_ 16    

$tcp4 set packetsize_ 4000    

$tcp4 set fid_ 4    

set ftp4 [$tcp4 attach-app FTP]    

$ftp1 set interval_ .005    

$ns at 2.5 "$ftp4 start"    

$ns at 4.0 "$ftp4 stop"

 

#define the finish procedure.  

proc finish {} {            

global ns namf tracef nf1            

set NSHOME "/home/pradeepkumar/ns-allinone-2.35"                        ;#change path            

set SETFID "$NSHOME/ns-2.35/bin/set_flow_id"            

set RAW2XG_SCTP "$NSHOME/ns-2.35/bin/raw2xg-sctp"            

$ns flush-trace

close $namf             

exec perl $SETFID -s rands2.tr | \

perl $RAW2XG_SCTP -A -q > Rands2.rands            

exec xgraph -bb -tk -nl -m -x time -y packets Rands2.rands &            

$ns flush-trace            

close $tracef            

exit 0     

}

#Calling finish procedure

$ns at 20.0 "finish"

$ns run

The Listing 4.5 uses the following:

·       There are totally 11 nodes, 8 client nodes and 3 router nodes

·       The links are colored

·       All the nodes were labelled

·       They use TCP Agents with FTP Flow with altered parameters like packetSize_, maximum congestion window specification, rate of transfer and flow id.

·       The script uses the set_flow_id and raw2xg_sctp binaries provided by ns2 and it uses perl script to plot the characteristics of data sent, data drop etc.

The Network Animation window and the related the graphs are shown below.

The following figure shows the data sent, forwarded, received and dropped at all the links. The graph shows green color as dense dots as data generation is huge compared to the data forwarded, received or lost.


Network Animation
Network Animation

Packet data
Packet data

Comments

Popular posts from this blog

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of vanet-routing-compare.cc (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./build.py --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb http://in.archive.ubuntu.com/ubuntu bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/Makefile.in Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/Makefile.in ~xgraph-12.2/Makefile.in ~otcl-1.14/Makefile.in Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from nsnam.org website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q