NS2 Tutorial for CS740

Jianming Wu

*modified from Aga Zhang

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

NS2 • NS version2 is a discrete-event driven and object-oriented network simulator – Type of simulation: continuous. and combined • Events  Packet and Timer Retransmit timer start R1 TCP Packet Ack Retransmit R2 . discrete event.Introduction • NS2 history – Modified from REAL network simulator – Developed through VINT project at UC-Berkeley – NS1 vs.

Fundamental Skills • 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 .

a b source file include foreach info exists varNam info tclversion add2 55  57 linsert lreplace .b .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” } set ll [list a b c] lappend $ll d  a b c d lindex $ll 0  a llength $ll  4 lsearch split a.

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 .

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

return (TCL_OK). } } return (Agent::command(argc. } . argv).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]).

&wnd_). } // bind_time().OTcl Linkage (III) bind(): link C++ member variables to Otcl object variables • C++ TcpAgent::TcpAgent() { bind(“window_”.tcl . bind_bool(). bind_bw() • Otcl $tcp set window_ 200 You must setting the initial values of variants in ~ns/tcl/lib/nsdefault.

evalf("%s Lookup_CIP_RT %d". name().OTcl Linkage (IV) • Invoking Otcl procedure and obtaining its results Tcl::instance().result(“………”) . iph->dst().addr_). Classifier/Addr/Cip instproc Lookup_CIP_RT { m_addr } { return … } Buffer • Passing a results string to Otcl Tcl::instance(). nextHop = Tcl::instance().result().

remember to modify makefile (.cc or *.cc) or ~ns/tcl/lib/ns-lib.tcl(.bashrc • Modifying *..tcl) • make depend make Or make clean configure –enable--debug make . e.g.tcl file – if add new one.31 – Add the path of ns-2 to your profile.edu/nsnam/ns/ – Install ns-allinone-2. .Network Simulator .isi.ns-2 • Homepage: http://www.

tcl .tcl Run your program  % ns Simple.Simple code (I) Simple.

Simple code (II) .

Simple code (III) .

Simple code (IV) .

Simple code (V) .

HA. and FA • New packet header • Setting timer for advertisement and registration • Processing handoff .Study Project .Mobile IP • Configuring mobile node.

Configure mobile node. and FA routingAgent_ • Wired node – set node(0) [$ns_ node] • Wireless & wired node OFFMN . HA.

00 "$MH setdest 640.0] $HA random-motion 0 $FA random-motion 0 Close random motion # create a mobilenode $ns_ node-config -wiredRouting OFF set MH [$ns_ node 1.0] set FA [$ns_ node 2.0.000000000000 # MH starts to move towards FA $ns_ at 100.0 20.0 610.000000000000 $FA set X_ 650.000000000000 $MH set X_ 2.000000000000 $MH set Y_ 2.000000000000 $HA set Z_ 0.0.0" # Position for base-station nodes (HA & FA).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.Motion # Create HA and FA set HA [$ns_ node 1.00 "$MH setdest 2. $HA set X_ 1.0 2.0" # goes back to HA speed $ns_ at 200.000000000000 destination Initial position .000000000000 $FA set Y_ 600.000000000000 $FA set Z_ 0.0.000000000000 $HA set Y_ 2.0 20.

Mobile Node 0 1 Agent Agent Node .

Base-Station .

hdr_cmn *ch = hdr_cmn::access(p). // required by PacketHeaderManager inline static hdr_ip* access(Packet* p) { return (hdr_ip*) p->access(offset_). } .Packet header offset_ struct hdr_ip { …………………. static int offset_. •Access packet header: hdr_ip *iph = hdr_ip::access(p). …………………… iph->dst() = iph->src().

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

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

} } • Receiving reply void MIPMHAgent::recv(Packet* p. hdr_mip *miph = hdr_mip::access(p). default: break.evalf("%s update-reg %d".5). reg(). coa_). default: Packet::free(p). } } . break. case MIPT_ADS: reg(). switch (miph->type_) { case MIPT_REG_REPLY: tcl.New Agent (II) • Time-out void MIPMHAgent::timeout(int tno){ switch (tno) { case MIP_TIMER_SIMPLE: reg().cancel(). case MIP_TIMER_REG: seqno_++. name_.resched(miph->lifetime_-0. break. break. if (rtx_timer_. Handler *){ Tcl& tcl = Tcl::instance(). break.status() == TIMER_PENDING) rtx_timer_. reg_timer_. break.

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

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_ .

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

3 .0 .0/0503/ 2.431:70 03.0. W 2.38 - 9.0. 9.0 72.-0 /0-: 2.

4/0  $250 9.2 38$250 9.$250. #:34:75747. .

4/0  .$250.

4/0  .$250.

$250.4/0 ' .

4/0 ' .$250.

$9:/!740.3/411 ./07 W $09939207147.090.079802039..3/ W 05.0883.943 W !74./.9 4-0! W 431:7324-034/0  .3/ 70897.

3/  74:93039* W 70/34/0 80934/0 38*34/0( W 70088 70/34/0   .431:7024-034/0  .

7989424.//7(( 80970.548943 .094..70.90.4943 70.9 809/089    !48943147-.3/42 24943 480 7.943 39.80 89.3/ 80938*34/0 ( 80938*34/0 ( 7.039*(809420*.//7088//7!.9 809/089    408-.//7/34/0 .90.28.//7088 24.7/8 38*.431 70/#4:93  80938*34/0 ( 80934/0*  809.020394190 809* 809* 809* 89.039*.94334/08    809* 809* 809* 809* 809* 809* /0893.94 8500/ 38*.3/42 24943 7.24-034/0 38*34/0 .7.3/42 24943 .

4-04/0  039 039 4/0 .

.80 $9.943 .

9..9/7*5  89.!.39411809*.090./07 411809* 897:.

.

088 !...088 5  5 /89 5 87.07 33089..088 5  /7*./07 /7*5 5/7*5..23 .090.706:70/-!..23./7*5 ./07./7*..3.090.9..088 411809*  < W...  ..09 5  709:73 /7*5 5 .0885...

.9.9/7*25 !.090. 897:. !0.0!5../07.090./07.88!0..88 !..88 W .090./07 89.090../07./07.885:-.9:70 5:-.9.88  897:.!./07..

! 8041 /7*25  39.88*25/7 5#0%50950* ..* -3/*411809 /7*25411809* < 39.4.* <.//7* 39.

.

4389!..09 5  =38..39411809* 33089.9.088 .9./7*25 ..!%*#*#"&$% !%*#*#! !%*$ !%*$  /4:-010920* 3980634* W$0993 89.

422..

.3/ 709:73 /7*25 5 .09 .088 411809*  =38.5...

9..

-.

. < < W.088 5 .088 /7*25 25/7*25..38 5.09 9...

.3/07 W .88$250%2075:-..9.9:70 .4/70  39.4/70.. !039  .*.%207..095074/.0039 W 0039147803/370897.. .4/9204:9 39  57490..09 .9435. 897:.88!039 5:-.039 5:-.3/07  . !.90/ .

039.//7088 . 420.

*.4. 39.

//7088 . ..70 41 .

/4:-070*79* .

7097.382993920 .

$250%20779*9207* #0%20770*9207* /4:-070192* .

94310920 . 70897.

/192*. /4:-0.

./810920 .:77039.

039  .*.3/07 5:-.90/ 330.4/0570 .90/ 330.< 57490.*. $250%207 039 .%207. %207.< 57490. < 5:-.*< . #0%207 !039 .* 9204:9 !*%#*# < !039 ..3/07 .*< .88#0%2075:-.*9204:9 !*%#*$! < 039 .4/0570 . %207.039  .3/07 .

 9.9:8 %#*! 79*9207* ..088 5  89.09 5 . 0.20* .4/!03970.3/07  %. /01. /01. 25 950*  .0039  W %20 4:9 .*  1 79*9207* 89..0.3. !.4/!0399204:9 39934  89. 934  ...:9-70.389.4.091700 5 -70.0  70*9207* 708.80!*%#*# 80634*  70 -70.. < < ..90 70/ 3. .80!%*#*#! 9.:9!.0  /7*25 25/7*25.80!%*$70 -70.1 8:5/.3705 ..%.0/ 25 10920*   -70. ....3.80!*%#*$! 70 -70. < < W #0..

885:-. !039  .88!039 5:-.039 5:-....88 %.4/70..9.88 !039..0039  ..88!039..3/07  $250%20779*9207* #0%20770*9207* < 89.09 .88 039. !.%.

 .9 .20* . /01.90 39 ... 25 950*  ....*  -70. !.09 5 .3/07  /7*25 25/7*25.7 .80!%*$70 -70.. -0..4./$0*  80*  < .1 8:5/.. 0.091700 5 -70.4389  709:73 30!039  < <.70.039 !039!039  -3/ .88*252.90 70/ 3.4/!03970.088 5  89..:9!.! < %. < < =38..80!%*#*#! 9.4389.

9..

-.

38 /01. .:9 9.

943.3/411 38*34/0 ..0883.!74.431 5745%50!745.

74:3/ 5%50!.%4#.

330%50.330..70088! .

74:3/ 7010..330  W70085.024/0 W%4 7.94324/0 W$./4324/0 =38.70088.

2.

. #%708* .70088 5 .

08 W W W W W %.#010703..

% 995.

.

 9. 9.

8419.70.

9.9.

08 995. 83.20-5.

.

 8 0/:.

383.2.

$-0.250 995.

.

30 5 0/:.

$.

 8. .708 89:947.70 995.9438419.7.7090 0. %:947.03982:.

.

 94544 47.

8419.

82 92 .

Sign up to vote on this title
UsefulNot useful