0% found this document useful (0 votes)
81 views32 pages

NS2 Tutorial: Aga Zhang Dependable Computing Lab

This document provides an overview and introduction to NS2, a discrete-event network simulator. It outlines the fundamentals of NS2 including using Tcl and OTcl for scripting. It also describes using NS2 to simulate networks and conduct studies such as on mobile IP. The document concludes with references for further information on NS2.

Uploaded by

Sadry Saputra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views32 pages

NS2 Tutorial: Aga Zhang Dependable Computing Lab

This document provides an overview and introduction to NS2, a discrete-event network simulator. It outlines the fundamentals of NS2 including using Tcl and OTcl for scripting. It also describes using NS2 to simulate networks and conduct studies such as on mobile IP. The document concludes with references for further information on NS2.

Uploaded by

Sadry Saputra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

NS2 Tutorial

Aga Zhang Dependable Computing Lab

Outline
Introduction Fundamental Skills - Tcl and OTcl Network Simulator - ns-2 Study Project - Mobile IP Conclusions

Introduction
NS2 history
Modified from REAL network simulator Developed through VINT project at UCB NS1 vs. NS2

NS version2 is a discrete-event driven and object-oriented network simulator


Type of simulation: continuous, discrete event, and combined

Events Packet and Timer


Retransmit timer start

R1

TCP Packet Ack Retransmit

R2

Fundamental Skills (I)


Scripting language is used to describe a high-level programming language with relatively transparent syntax
button .b text Hello! command {puts hello} ---- tcl command

Scripting languageUnix shells(sh, csh, ), Tcl, Perl,VB, and JavaScript All major computing platforms provide both system programming languages such as C or Java and scripting languages such as Tcl

Fundamental Skills (II)


NS2 is written in C++ and OTcl
OTcl = Tcl + OO C++ implements the code that executed frequently OTcl configures the system
set ns [new Simulator] set n1 [new Node] set n2 [new Node] $ns duplex-link $n1 $n2 5Mb 2ms DropTail

n1

n2

Tcl Tool Command Language


Start: Typing tclsh in Unix shell Instructions using in ns2:
proc add2 {a} { set b [expr $a+1] incr b puts a+2=$b } add2 55 57 set ll [list a b c] lappend $ll d a b c d lindex $ll 0 a llength $ll 4 lsearch linsert lreplace split a.b . a b source file include foreach info exists varNam info tclversion

OTcl MIT Object Tcl


Class son superclass father Class father father instproc init {args} { $self set var_ 0 puts **var_=$var_ eval $self next $args } father ff **var_=0 ff info vars var_ ff set var_ 0 ff info class father father info instances ff son instproc init {args} { puts no var_ } son ss no var_ Class son2 superclass father son instproc init {args} { puts no var_ $self next $args } son2 ss no var_ **var_=0

OTcl Linkage
set tcp [new Agent/TCP]
Agent/TCP init Agent init TclObject
Create C++ object Create Otcl shadow object
static class TcpClass : public TclClass { public: TcpClass() : TclClass("Agent/TCP") {} TclObject* create(int, const char*const*) { return (new TcpAgent);} } class_tcp;

OTcl TclObject (C++)


constructor

parent
constructor

TcpAgent
constructor

C++

OTcl Linkage (II)


Command()
Otcl
$tcp advance 10

C++
int Agent::command(int argc, const char*const* argv) { if (argc == 3) { if (strcmp(argv[1], advance") == 0) { int newswq = atoi(argv[2]); return (TCL_OK); } } return (Agent::command(argc, argv); }

OTcl Linkage (III)


bind(): link C++ member variables to Otcl object variables C++
TcpAgent::TcpAgent() { bind(window_, &wnd_); } // bind_time(), bind_bool(), bind_bw()

Otcl
$tcp set window_ 200

You must setting the initial values of variants in ~ns/tcl/lib/nsdefault.tcl

OTcl Linkage (IV)


Invoking Otcl procedure and obtaining its results
Tcl::instance().evalf("%s Lookup_CIP_RT %d", name(), iph>dst().addr_); nextHop = Tcl::instance().result(); Classifier/Addr/Cip instproc Lookup_CIP_RT { m_addr } { return }

Buffer

Passing a results string to Otcl


Tcl::instance().result()

Network Simulator - ns-2


After installing: Add the path of ns2 to your profile, and validate your ns2 Modifying *.cc or *.tcl file if add new one adding its path to makefile(.cc) or ~ns/tcl/lib/ns-lib.tcl(.tcl) Type: make depend make Or make clean configure enable--debug make

The directory of ns2

Simple code (I)

Simple.tcl

Run your program % ns Simple.tcl

Simple code (II)

Simple code (III)

Simple code (IV)

Simple code (V)

Study Project - Mobile IP


Configuring mobile node, HA, and FA New packet header Setting timer for advertisement and registration Processing handoff

Configure mobile node, HA, and FA routingAgent_


Wired node set node(0) [$ns_ node] Wireless&wired node

OFFMN

Motion
# Create HA and FA set HA [$ns_ node 1.0.0] set FA [$ns_ node 2.0.0] $HA random-motion 0 $FA random-motion 0

Close random motion

# Position for base-station nodes (HA & FA). $HA set X_ 1.000000000000 $HA set Y_ 2.000000000000 $HA set Z_ 0.000000000000 $FA set X_ 650.000000000000 $FA set Y_ 600.000000000000 $FA set Z_ 0.000000000000

# create a mobilenode $ns_ node-config -wiredRouting OFF set MH [$ns_ node 1.0.1] set node_(0) $MH set HAaddress [AddrParams addr2id [$HA node-addr]] [$MH set regagent_] set home_agent_ $HAaddress # movement of the MH $MH set Z_ 0.000000000000 $MH set Y_ 2.000000000000 $MH set X_ 2.000000000000 # MH starts to move towards FA $ns_ at 100.00 "$MH setdest 640.0 610.0 20.0" # goes back to HA speed $ns_ at 200.00 "$MH setdest 2.0 2.0 20.0"

destination

Initial position

Mobile Node
0 1 Agent Agent

Node

Base-Station

Packet header
offset_

struct hdr_ip { . static int offset_; // required by PacketHeaderManager inline static hdr_ip* access(Packet* p) { return (hdr_ip*) p->access(offset_); }

Access packet header:


hdr_ip *iph = hdr_ip::access(p); hdr_cmn *ch = hdr_cmn::access(p); iph->dst() = iph->src();

New MIP packet header


static class MIPHeaderClass : public PacketHeaderClass {

Data-structure

public:

MIPHeaderClass() : struct hdr_mip { PacketHeaderClass("PacketHeader/MIP", sizeof(hdr_mip)) { int haddr_; int ha_; bind_offset(&hdr_mip::offset_);} int coa_; } class_miphdr; MipRegType type_; //MIPT_REG_REQUEST, MIPT_REG_REPLY, MIPT_ADS, MIPT_SOL double lifetime_; int seqno_; Setting: static int offset_; inline static hdr_mip* access(const Packet* p) { ~ns/comm/packet.h and return (hdr_mip*) p->access(offset_); ~ns/tcl/lib/ns-packet.tcl } };

Access:

hdr_mip *miph = hdr_mip::access(p)

New Agent
New Agent for sending registration packet periodically class SimpleTimer : public TimerHandler { Data-structure
class MIPMHAgent : public Agent { public: MIPMHAgent(); void recv(Packet *, Handler *); void timeout(int); protected: void reg(); int ha_; /* home agent address */ int coa_; /* care-of-address */ double reg_rtx_; /* retransmitting time */ SimpleTimer rtx_timer_; RegTimer reg_timer_; double reglftm_; /* registration lifetime */ double adlftm_; /* current ads lifetime */ };
public: SimpleTimer(Agent *a) : TimerHandler() { a_ = a; } protected: inline void expire(Event*){ a_>timeout(MIP_TIMER_SIMPLE); } Agent *a_; }; class RegTimer : public TimerHandler { public: RegTimer(MIPMHAgent *a) : TimerHandler() { a_ = a; } protected: Inline void expire(Event *) { a_->timeout(MIP_TIMER_REG);} MIPMHAgent *a_;};

New Agent (II)


Time-out
void MIPMHAgent::timeout(int tno){ switch (tno) { case MIP_TIMER_SIMPLE: reg(); break; case MIP_TIMER_REG: seqno_++; reg(); break; default: break; } }

Receiving reply
void MIPMHAgent::recv(Packet* p, Handler *){ Tcl& tcl = Tcl::instance(); hdr_mip *miph = hdr_mip::access(p); switch (miph->type_) { case MIPT_REG_REPLY: tcl.evalf("%s update-reg %d", name_, coa_); if (rtx_timer_.status() == TIMER_PENDING) rtx_timer_.cancel(); reg_timer_.resched(miph->lifetime_-0.5); break; case MIPT_ADS: reg(); break; default: Packet::free(p); break; } }

New Agent (III)


class MIPMHAgent : public Agent { public: MIPMHAgent(); void recv(Packet *, Handler *); .. SimpleTimer rtx_timer_; RegTimer reg_timer_; }; static class MIPMHAgentClass : public TclClass { MIPMHAgentClass() : TclClass("Agent/MIPMH") {} TclObject* create(int, const char*const*) { return (new MIPMHAgent()); } } class_mipmhagent; MIPMHAgent::MIPMHAgent() { bind("adSize_", &size_); } void MIPMHAgent::recv(Packet* p, Handler *){ hdr_mip *miph = hdr_mip::access(p); switch (miph->type_) { case MIPT_REG_REPLY: tcl.evalf("%s update-reg %d", name_, coa_); break; case MIPT_ADS: reg(); break; default: Packet::free(p); break; } }

~ns/tcl/lib/ns-default.tcl

recv()

Class ..
recv(Packet* p, Handler *)

Class ..
recv(Packet* p, Handler *)

Class ..
recv(Packet* p, Handler *)

Processing handoff
$ns_ node-config -propType Propagation/TwoRayGround \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \

Free space model Two-ray ground reflection model Shadowing model


~ns/mac/wireless-phy.cc

RXThresh_

Conclusions
Tcl/Tk NAM Xgraph New node
Node instproc init args { eval $self next $args .. $self mk-default-classifier .. } foreach modname [Node set module_list_] { $self register-module [new RtModule/$modname] } Simulator instproc set-address-format {opt args} {}

References
Tcl/Tk http://www.tcl.tk/software/tcltk/ Nsnam web pages http://www.isi.edu/nsnam/ NS by example http://nile.wpi.edu/NS/ Tutorial Marc Greis's tutorial Discrete-event simulation software
http://www.topology.org/soft/sim.html

You might also like