You are on page 1of 5

Università degli Studi di Verona, Facoltà di Scienze MM. FF. NN.

Ca' Vignal, Strada Le Grazie 15


37134 Verona, Italia
Dipartimento di Informatica Tel. +39 045 8027069
Fax +39 045 8027068
EDALab: Embedded System Design Center

Verona, 12/09/2005

Network advanced modeling in NS-2


Giovanni Perbellini

1 REQUIRED BACKGROUND 2

2 GOAL 2

3 WHAT IS NS-2? 2

4 NS-2 EMULATION 2
4.1 NETWORK OBJECTS ................................................................................................................... 2
4.2 TAP AGENTS ............................................................................................................................... 3

5 CASE STUDY 3
5.1 TCL CODE ................................................................................................................................... 4
5.2 SIMULATING THE NS-2 NETWORK ............................................................................................ 5

6 REFERENCES 5

1
Università degli Studi di Verona, Facoltà di Scienze MM. FF. NN. Ca' Vignal, Strada Le Grazie 15
37134 Verona, Italia
Dipartimento di Informatica Tel. +39 045 8027069
Fax +39 045 8027068
EDALab: Embedded System Design Center

1 Required Background
Students interested in learning NS-2 are required to know the fundamentals of C/C++
language.

2 Goal
In this lecture are introduced advanced concepts of NS-2 network modeling. Students will
learn to:
• to introduce live traffic into the simulator and injecting traffic from the simulator into
the live network;

3 What is NS-2?
NS (version 2) is an object-oriented, discrete event driven network simulator developed at
UC Berkely written in C++ and OTcl (Tcl script language with Object-oriented extensions). It
implements network protocols such as TCP and UPD, traffic source behavior such as FTP,
Telnet, Web, CBR and VBR, router queue management mechanism such as Drop Tail, RED
and CBQ, routing algorithms such as Dijkstra, and more. NS also implements multicasting
and some of the MAC layer protocols for LAN simulations.
More information in [1] and [2].

4 NS-2 Emulation
This Section describes the emulation facility of NS-2. Emulation refers to the ability to
introduce the simulator into a live network. Special objects within the simulator are capable of
introducing live traffic into the simulator and injecting traffic from the simulator into the live
network.
The interface between the simulator and live network is provided by a collection of
objects including Tap Agents and Network Objects. Tap agents embed live network data into
simulated packets and vice-versa. Network objects are installed in tap agents and provide an
entrypoint for the sending and receipt of live data. Both objects are described in the following
sections.
When using the emulation mode, a special version of the system scheduler is used: the
RealTime Scheduler. This scheduler uses the same underlying structure as the standard
calendar-queue based scheduler, but ties the execution of events to realtime. More
information in [2].

4.1 Network Objects


Network objects provide access to a live network. There are several forms of network
objects, depending on the protocol layer specified for access to the underlying network, in
addition to the facilities provided by the host operating system. Use of some network objects
requires special access privileges where noted. Generally, network objects provide an
entrypoint into the live network at a particular protocol layer (e.g. link, raw IP, UDP, etc) and
with a particular access mode (read-only, write-only, or read-write). The C++ class Network

2
Università degli Studi di Verona, Facoltà di Scienze MM. FF. NN. Ca' Vignal, Strada Le Grazie 15
37134 Verona, Italia
Dipartimento di Informatica Tel. +39 045 8027069
Fax +39 045 8027068
EDALab: Embedded System Design Center

is provided as a base class from which specific network objects are derived. Three network
objects are currently supported: pcap/bpf, raw IP, and UDP/IP.
Configuration: Network UDP/IP.
Injecting UDP traffic from the simulator into the live network:
set ipnet [new Network/IP/UDP]
$ipnet open writeonly
$ipnet connect 127.0.0.1 4460
Introducing live UDP traffic into the simulator:
set ipnet [new Network/IP/UDP]
$ipnet open readonly
$ipnet bind 127.0.0.1 4460
4.2 Tap Agents
The class TapAgent is a simple class derived from the base Agent class. As such, it is able
to generate simulator packets containing arbitrarily-assigned values within the ns common
header. The tap agent handles the setting of the common header packet size field and the type
field. Each tap agent can have at most one associated network object, although more than one
tap agent may be instantiated on a single simulator node.
Configuration: Tap agents are able to send and receive packets to/from an associated
Network object. Assuming a network object $netobj refers to a network object, a tap agent
is configured using the network method:
set a0 [new Agent/Tap]
$a0 network $ipnet

5 Case Study
The case study used to describe the NS-2 Emulation feature is an UDP Network simulated
in NS-2, composed by three nodes, where the traffic generator is a real UDP client that injects
UDP traffic into NS-2 network. Once crossed the NS-2 network, the UDP packets exit from
the simulator and are received by a real UDP server. This scenario is shown in the following
figure:

Tap Tap
Network IP-UDP Network IP-UDP

0 1 2
UDP UDP
Client Server

NS-2

3
Università degli Studi di Verona, Facoltà di Scienze MM. FF. NN. Ca' Vignal, Strada Le Grazie 15
37134 Verona, Italia
Dipartimento di Informatica Tel. +39 045 8027069
Fax +39 045 8027068
EDALab: Embedded System Design Center

The tcl code to implement the Network described above, is reported in the next section.
5.1 Tcl Code
First of all, you need to create a RealTime simulator object. This is done with the
command:
set ns [new Simulator]
$ns use-scheduler RealTime
Now we open a file for writing that is going to be used for the nam trace data.
set traceFile [open emulationInOut.tr w]
$ns trace-all $traceFile
set namFile [open emulationInOut.nam w]
$ns namtrace-all $namFile
proc finish {} {
global ns
global namFile
global traceFile
$ns flush-trace
close $namFile
close $traceFile
$ns close-channel
exit 0
}
The following lines define the three network nodes.
set node0 [$ns node]
set node1 [$ns node]
set node2 [$ns node]

The next lines allow to introduce live UDP traffic into the simulator from a UDP client
through 4460 port:
set ipnetIN [new Network/IP/UDP]
$ipnetIN open readeonly
$ipnetIN bind 127.0.0.1 4460

The Network ipnetIN object is connected to a Tap Agent called aIn.


set aIn [new Agent/Tap]
$aIn network $ipnetIN

The next step is to define the IP-UDP Network Object to inject UDP traffic from the
simulator into the live network.
set ipnetOUT [new Network/IP/UDP]
$ipnetOUT open writeonly
$ipnetOUT connect 127.0.0.1 4470

The Network ipnetOUT object is connected to a Tap Agent called aOut.

4
Università degli Studi di Verona, Facoltà di Scienze MM. FF. NN. Ca' Vignal, Strada Le Grazie 15
37134 Verona, Italia
Dipartimento di Informatica Tel. +39 045 8027069
Fax +39 045 8027068
EDALab: Embedded System Design Center

set aOut [new Agent/Tap]


$aOut network $ipnetOUT

The next lines connect the nodes.


$ns duplex-link $node0 $node1 30kb 5ms DropTail
$ns duplex-link $node1 $node2 0.1kb 5ms DropTail
$ns queue-limit $node1 $node2 10

The next line attaches the aIn Tap Agent to node node0.
$ns attach-agent $node0 $aIn
The next line attaches the aOut Tap Agent to node node2.
$ns attach-agent $node2 $aOut

Now the two agents have to be connected with each other.


$ns connect $aIn $aOut

The last line finally starts the simulation.


$ns run
Now it is possible to save the file (e.g. emulationInOut.tcl) and start the
simulation.
5.2 Simulating the NS-2 network
After the emulationInOut.tcl simulation script is built, you run the simulation by
executing the simulation script created in the previous section. To run the simulation, type the
following command:
nse emulationInOut.tcl

6 References
[1] Giovanni Perbellini, “An Introduction to NS-2”, 2005.
[2] US Berkley, “The NS Manual”, Kevin Fall Editor, Kannan Varadhan Editor.
[3] “NS Simulator for beginners”, Eitan Altman, Tania Jimenez.