360 likes | 555 Views
انجمن انفورماتیک ایران . شبیه سازی شبکه توسط نرم افزار NS2 ارائه دهنده : حسین معمارزاده. NS2 . NS یک شبیه ساز شبکه است NS می تواند سناریوهای متفاوت شبکه را شبیه سازی کند
E N D
انجمن انفورماتیک ایران • شبیه سازی شبکه توسط نرم افزار NS2 • ارائه دهنده : حسین معمارزاده
NS2 NSیک شبیه ساز شبکه است • NS می تواند سناریوهای متفاوت شبکه را شبیه سازی کند • NS می تواند ترافیکهای متفاوتی را شبیه سازی کند و خروجی مورد نیاز برای اندازه گیری پارامترهایی مثل تاخیر و میزان گم شدن بسته ها را فراهم کند • NS مورد نیاز است زیرا • نیاز به صحت و اندازه گیری کارآیی الگوریتهمای جدید • شبیه سازی در محیط واقعی هزینه بر و خطا پذیر است
NS • C++ برای پردازش بسته ها ، OTcl برای سناریوها • 100K خط کد C++، 70 K خط کد Otcl • http://www.isi.edu/nsnam/ns • نسخه فعلی 2.32 • قابل نصب در بیشتر UNIX ها (Linux,Solaris,Freebsd) و ویندوز (9x/NT/2000/2003)
NS Components • اجزاء NS • NS • Nam(Network animator) • خروجی NS را می توان مشاهده کرد مراحل پردازش پیش از شبیه سازی مراحل پردازش پس از شبیه سازی
شبیه سازی اتفاقات گسسته • NS شبکه را بصورت اتفاقات گسسته شبیه سازی می کند • زمانبند NS اتفاقات را پشت سرهم اجرا می کند • زمان وقوع هر اتفاق مجازی است
C++ AND Otcl Separation • C++ برای اطلاعات • پردازش هر بسته – هسته مرکزی NS • سرعت بالا ، کاملا شی گرا • OTCL برای کنترل • ایجاد سناریوهای شبیه سازی • راحتی برای کاربر • عملیات دوره ای یا تحریک شده
Tcl/Tk Download source: A modestly up to date Tcl/Tk version Tcl-8.4.14Tk-8.4.14 • web page: Tcl Developer Xchange resources. Note that while more up to date versions of Tcl/Tk are available and may work, we have only tested up through version 8.4.14. • otcl Download source: version 1.13 (released Mar 10, 2007) • daily snapshot • anonymous CVS (package "otcl") • web page: http://otcl-tclcl.sourceforge.net/otcl/. • TclCL (the package formerly known as libTcl) Download source: The latest version tclcl-1.19 (released Mar 10, 2007) • daily snapshot • anonymous CVS (package "tclcl") • web page: http://otcl-tclcl.sourceforge.net/tclcl/. • ns-2 Download source: most recent release (ns-2.34 released Jun 17 2009) • daily snapshot, • anonymous cvs (module "ns-2"). • There may be known problems with the current version of ns; please check the ns installation problems web page for patches. nam-1 (optional) Download source: most recent release (nam-1.14 released Jun 17 2009) • daily snapshot, • anonymous cvs (module "nam-1"). • web page: http://www.isi.edu/nsnam/nam/
A simple wireless example • 4گره wireless • این گره ها در یک صفحه به مساحت 400*400حرکت می کنند • پروتکل روتینگ DSR • شبیه سازی مکانیزم IEEE 802.11 PS
ATIM Window ATIM Window ATIM Window ATIM Window power saving mode active state ATIM Beacon BTA=2, BTB=5 data frame power saving mode ACK ACK Beacon IEEE 802.11 PS Target Beacon Transmission Time (TBTT) Beacon Interval Beacon Interval Host A No ATIM means no data to send or to receive Host B
Simulation in NS2 • فایلهایی که باید تغییر کنند • Mac-802_11.cc • Mac-802_11.h • Mac-timers.cc • Mac-timers.h • Mac.cc • Mac.h
Mac-timers.cc • #include <delay.h> • #include <connector.h> • #include <packet.h> • #include <random.h> • // #define DEBUG • //#include <debug.h> • #include <arp.h> • #include <ll.h> • #include <mac.h> • #include <mac-timers.h> • #include <mac-802_11.h> • /* • * Force timers to expire on slottimeboundries. • */ • // #define USE_SLOT_TIME • // change wrt Mike's code • #ifdef USE_SLOT_TIME • #error "Incorrect slot time implementation - don't use USE_SLOT_TIME..." • #endif • #define ROUND_TIME() \ • { \ • assert(slottime); \ • double rmd = remainder(s.clock() + rtime, slottime); \ • if(rmd > 0.0) \ • rtime += (slottime - rmd); \ • else \ • rtime += (-rmd); \ • }
void • MacTimer::start(double time) • { • Scheduler &s = Scheduler::instance(); • assert(busy_ == 0); • busy_ = 1; • paused_ = 0; • stime = s.clock(); • rtime = time; • assert(rtime >= 0.0); • s.schedule(this, &intr, rtime); • } • void • MacTimer::stop(void) • { • Scheduler &s = Scheduler::instance(); • assert(busy_); • if(paused_ == 0) • s.cancel(&intr); • busy_ = 0; • paused_ = 0; • stime = 0.0; • rtime = 0.0; • }
void • BeaconIntervalTimer::handle(Event *) • { • busy_ = 0; • paused_ = 0; • stime = 0.0; • rtime = 0.0; • mac->beaconIntervalHandler(); • } • void • ATIMWindowTimer::handle(Event *) • { • busy_ = 0; • paused_ = 0; • stime = 0.0; • rtime = 0.0; • mac->atimWindowHandler(); • } • void • InitialBeaconIntervalTimer::handle(Event *) • { • busy_ = 0; • paused_ = 0; • stime = 0.0; • rtime = 0.0; • mac->initialBeaconIntervalHandler(); • }
Mac802_11::beaconIntervalHandler() • { • in_atim_=true; • totalBi_++; • EnergyModel *em = netif_->node()->energy_model(); • if(em != NULL && !netif_->is_on()) • netif_->node_wake(); • atim_tx_=true; • EnergyModel *em = netif_->node()->energy_model(); • if(em != NULL && !netif_->is_on()) • netif_->node_wake(); • } • } • // Schedule the next beacon interval • double bi_time = msec2sec(macmib_.BeaconInterval); • mhBeaconInterval_.start(bi_time); • mhAtimWindow_.start(msec2sec(macmib_.ATIMWindowSize)); • got_beacon_=false; • sendBeacon(); • stay_awake_=false; • bufferEntry *tmp = NULL; • bufferEntry *cur = pktBuffer_.firstElement(); • while(cur != NULL) { • Host *h = NULL; • if(cur->addr() != MAC_BROADCAST) • h = &cache_[cur->addr()]; • if(false && cur->age_ >= MAX_PKT_BUFFER_AGE && • ( h == NULL )) {
if(!cur->sent_data_ && cur->p_ != 0) { • hdr_cmn *ch = HDR_CMN(cur->p_); • if (ch->xmit_failure_) { • //ch->size() -= ETHER_HDR_LEN11; • // ch->xmit_reason_ = XMIT_REASON_BUF; • ch->xmit_failure_(cur->p_->copy(), ch->xmit_failure_data_); • } • discard(cur->p_, DROP_MAC_RETRY_COUNT_EXCEEDED); • } • tmp = cur; • cur = pktBuffer_.nextElement(cur); • pktBuffer_.removeElement(tmp); • } • else { • // otherwise, update the age and reset interval info • //cur->age_ += static_atim_thresh_ + 1; • cur->age_++; • cur->sent_atim_ = false; • cur->recv_ack_ = false; • cur = pktBuffer_.nextElement(cur); • } • }
// Reset all of these variables at the beginning of the • // beacon epoch • rst_cw(); • if( atim_tx_) { • atim_scan(); • } • if((pktATIM_ != 0) || atim_to_send()) • if(!netif_->is_on()) • netif_->node_wake(); • if(!mhBackoff_.busy() && !mhSend_.busy() && !mhDefer_.busy() && • tx_state_ == MAC_IDLE && atim_tx_) • if(( pktATIM_ != 0)) { • // If the synch_err is true, we can't send for the first DELTA • // of last DELTA of the ATIM • mhBackoff_.start(2 * phymib_.CWMin, is_idle());}
Tcl scenario • set val(chan) Channel/WirelessChannel • set val(prop) Propagation/TwoRayGround • set val(netif) Phy/WirelessPhy • set val(mac) Mac/802_11 • set val(ifq) CMUPriQueue • set val(ll) LL • set val(ant) Antenna/OmniAntenna • set val(x) 400 ;# X dimension of the topography • set val(y) 400 ;# Y dimension of the topography • set val(ifqlen) 1000 ;# max packet in ifq • set val(seed) 1.0 • set val(adhocRouting) DSR • set val(nn) 4 ;# how many nodes are simulated • set val(cp) "../mobility/scene/cbr-4" • set val(sc) "../mobility/scene/scen-4" • set val(stop) 400 ;# simulation time • set ns_ [new Simulator] • # setup topography object • set topo [new Topography]
set tracefd [open wireless1-out.tr w] • set namtrace [open wireless1-out.nam w] • $ns_ trace-all $tracefd • $ns_ namtrace-all-wireless $namtrace $val(x) $val(y) • # define topology • $topoload_flatgrid $val(x) $val(y) • set god_ [create-god $val(nn)] • # • # define how node should be created • # • #global node setting • $ns_ node-config -adhocRouting $val(adhocRouting) \ • -llType $val(ll) \ • -macType $val(mac) \ • -ifqType $val(ifq) \ • -ifqLen $val(ifqlen) \ • -antType $val(ant) \ • -propType $val(prop) \ • -phyType $val(netif) \ • -channelType $val(chan) \ • # -energyModelEnergyModel \ • # -initialEnergy 1000 \ • # -txpower 1 \ • # -rxpower 1.2 \ • # -idlepower 1.0 \
# -sleepower 0.001 \ • -topoInstance $topo \ • -agentTrace ON \ • -routerTrace OFF \ • -macTrace ON • # • # Create the specified number of nodes [$val(nn)] and "attach" them • # to the channel. • for {set i 0} {$i < $val(nn) } {incr i} { • set node_($i) [$ns_ node] • $node_($i) random-motion 0 ;# disable random motion • } • # • # Define node movement model • # • #puts "Loading connection pattern..." • source $val(cp) • # • # Define traffic model • # • puts "Loading scenario file..." • source $val(sc) • # Define node initial position in nam
for {set i 0} {$i < $val(nn)} {incr i} { • # 20 defines the node size in nam, must adjust it according to your scenario • # The function must be called after mobility model is defined • $ns_ initial_node_pos $node_($i) 20 • } • #11 • # Tell nodes when the simulation ends • # • for {set i 0} {$i < $val(nn) } {incr i} { • $ns_ at $val(stop).0 "$node_($i) reset"; • } • $ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt" • puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)" • puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)" • puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)" • puts "Starting Simulation..." • $ns_ run
Application traffic file • # • # nodes: 4, max conn: 3, send rate: 0.33333333333333331, seed: 1 • # • # • # 1 connecting to 2 at time 2.5568388786897245 • # • set udp_(0) [new Agent/UDP] • $ns_ attach-agent $node_(1) $udp_(0) • set null_(0) [new Agent/Null] • $ns_ attach-agent $node_(2) $null_(0) • set cbr_(0) [new Application/Traffic/Pareto] • $cbr_(0) set packetSize_ 200 • $cbr_(0) set idle_time_ 50ms • $cbr_(0) set burst_time_ 500ms • $cbr_(0) set rate_ [expr 64*4]k • $cbr_(0) attach-agent $udp_(0) • $ns_ connect $udp_(0) $null_(0) • $ns_ at 2.5568388786897245 "$cbr_(0) start“
Out put format • s 3.424723606 _1_ AGT --- 0 pareto 210 [0 0 0 0] ------- [1:0 2:0 32 0] • s 3.425118606 _1_ MAC --- 0 AODV 106 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425966712 _15_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425966874 _4_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425966969 _0_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967116 _2_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967200 _10_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967376 _19_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967413 _14_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967416 _6_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • r 3.425967429 _18_ MAC --- 0 AODV 48 [0 ffffffff 1 800] ------- [1:255 -1:255 30 0] [0x2 1 1 [2 0] [1 4]] (REQUEST) • s 3.426207116 _2_ MAC --- 0 ARP 86 [0 ffffffff 2 806] ------- [REQUEST 2/2 0/1] • r 3.426895548 _5_ MAC --- 0 ARP 28 [0 ffffffff 2 806] ------- [REQUEST 2/2 0/1] • r 3.426895625 _1_ MAC --- 0 ARP 28 [0 ffffffff 2 806] ------- [REQUEST 2/2 0/1] • r 3.426895627 _15_ MAC --- 0 ARP 28 [0 ffffffff 2 806] ------- [REQUEST 2/2 0/1] • s 3.433492355 _1_ MAC --- 0 pareto 288 [13a 2 1 800] ------- [1:0 2:0 30 2] • r 3.435796864 _2_ MAC --- 0 pareto 230 [13a 2 1 800] ------- [1:0 2:0 30 2] • s 3.435806864 _2_ MAC --- 0 ACK 38 [0 1 0 0] • r 3.435821864 _2_ AGT --- 0 pareto 230 [13a 2 1 800] ------- [1:0 2:0 30 2] • r 3.436111374 _1_ MAC --- 0 ACK 38 [0 1 0 0]
A perl script to calculate packet loss • #!/usr/local/bin/perl • if (@ARGV < 2) • { • print "Usage: packetloss.pl <trace file> <cbr//tcp>\n"; • exit; • } • $infile = $ARGV[0]; • $kind = $ARGV[1]; • $sum_sent = 0; • $num_dropped = 0; • open (DATA, "<$infile") || die "Can't open $infile $!"; • while (<DATA>) { • $line = $_; • @x = split(' '); • last if ($x[4] =~ /END/); • if ($x[0] eq 's' && $x[6] =~ /$kind/) • { • $num_sent++; • #print $line; • }
if ($x[0] eq 'D' && $line =~ /IFQ/ && $x[6] =~ /$kind/ && $x[4]!=~ /END/) • { • $num_dropped++; • print $line; • } • } • $dropped_ratio = 100*($num_dropped/$num_sent); • print STDOUT "Percentage of $kind packets that were dropped: ${dropped_ratio}%\n"; • close DATA; • exit(0);