2015-12-25 the creation of ns2 simulated environment ( 1 ) li chengbo...

41
22/6/28 The Creation of NS2 Simulated Environment 1 LI Chengbo [email protected] .cn

Upload: barnaby-ray

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

The Creation of NS2 Simulated Environment ( 1)

LI Chengbo

[email protected]

Page 2: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

tcl Interpreter With Extensions

tcl

otcl

tclcl

U IEventScheduler

Netw

orkC

omponent

A Little Bit of Review

C++

Page 3: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Example Script

set ns [new Simulator]

set n0 [$ns node]

set n1 [$ns node]

n0 n1

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 0.2 "$ftp start"

$ns at 1.2 ”exit"

$ns run$ns duplex-link $n0 $n1

1.5Mb 10ms DropTailset tcp [$ns create-connection

TCP $n0 TCPSink $n1 0]

Page 4: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Basic ns-2: Covered

• wired & wireless

• unicast & multicast

• TCP & UDP

• errors & network dynamics

• ns & nam tracing

• application-level support

Page 5: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Outline for Today

• ns-2 Internal

• Making changes

• New components– in otcl– otcl and C++ Linkage– in C++

Page 6: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

ns-2 Internals

• Discrete Event Scheduler

• Network Topology

• Routing

• Transport

• Application

• Packet Flow

• Packet Format

Page 7: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Discrete Event Scheduler

time_, uid_, next_, handler_

handler_ -> handle()

time_, uid_, next_, handler_insert

head_ ->head_ ->

Page 8: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Network Topology - Node

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

Node entry

Unicast Node

Multicast Classifier

classifier_

dmux_

entry_

Node entry

Multicast Node

multiclassifier_

Page 9: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Network Topology - Link

n0 n1

enqT_ queue_ deqT_

drophead_ drpT_

link_ ttl_

n1 entry_head_

Page 10: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Routing

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

Node entry 0

1enqT_ queue_ deqT_

drophead_ drpT_

link_ ttl_

n1 entry_head_

Page 11: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Routing (cont.)

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0

1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

1

0Link n0-n1

Link n1-n0

Page 12: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

0

1

Transport

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0 Agent/TCP

agents_

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

agents_

dst_addr_=1dst_port_=0

dst_addr_=0dst_port_=0

Page 13: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Application

0

1

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0 Agent/TCP

agents_

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

agents_

Application/FTP

dst_addr_=1dst_port_=0

dst_addr_=0dst_port_=0

Page 14: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Packet Flow

0

1

n0 n1

Addr Classifier

Port Classifier

entry_

0 Agent/TCP Addr Classifier

Port Classifier

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

Application/FTP

dst_addr_=1dst_port_=0

dst_addr_=0dst_port_=0

Page 15: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Packet Format

header

data

ip header

tcp header

rtp header

trace header

cmn header

...

ts_

ptype_

uid_

size_

iface_

Page 16: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

NS-2 Wireless Internal

• Packet headers

• Mobile node

• Wireless channel

NS-2 Wireless Internal

Page 17: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Wireless Packet Format

header

data

ts_

ptype_

uid_

size_

iface_

IP header

......

cmn header

LL

MAC 802_11

......

ARP

wireless headers

Page 18: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Portrait of A Mobile Node

Node

ARP

Propagation and antenna models MobileNode

LL

MAC

PHY

LL

CHANNEL

LL

MAC

PHY

Classifier: Forwarding

Agent: Protocol EntityNode Entry

LL: Link layer object

IFQ: Interface queue

MAC: Mac object

PHY: Net interface

protocolagent

routingagent

addrclassifier

portclassifier

255

IFQIFQ

defaulttarget_

Page 19: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Mobile Node: Layer 2

• Link Layer– Same as LAN, but with a separate ARP module

• Interface queue– Give priority to routing protocol packets

• Mac Layer– IEEE 802.11– RTS/CTS/DATA/ACK for all unicast packets– DATA for all broadcast packets

Page 20: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Mobile Node: Layer 1

• Network interface (PHY)– Parameters based on Direct Sequence Spread

Spectrum (WaveLan)– Interface with: antenna and propagation models– Update energy: transmission, reception, and idle

• Radio Propagation Model– Friss-space attenuation(1/r2) at near distance– Two-ray Ground (1/r4) at far distance

• Antenna– Omni-directional, unity-gain

Page 21: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Wireless Channel

• Duplicate packets to all mobile nodes attached to the channel except the sender

• It is the receiver’s responsibility to decide if it will accept the packet– Whether the sender is close enough– Collision is also handled at individual

receivers– O(N2) messages grid keeper

Page 22: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Outline for Today

• ns-2 Internal

• Making changes

• New components– in otcl– otcl and C++ Linkage– in C++

• debugging

Page 23: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Making Changes

• In C++ space– Straight forward– Recompile

• In otcl space– source in the simulation scripts– Or recompile– Ex. changing the packet size

• tcl/ex/simple.tcl• CBR source packet size 210• Change to 420

Page 24: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Understanding The Makefile

• Defined variables– For example

• c++ compiler in CPP• Header files to include in INCLUDES• cc files to compiles in OBJ_CC• tcl files to merge in NS_TCL_LIB

• Commands– For example

• distclean to clean the distribution• ns to build ns binary

Page 25: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Adding New Components

• in otcl

• otcl and C++ linkage

• in C++

Page 26: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

New Component Purely in otcl

• Additional <new_stuff>.tcl file

• Adding new files– change Makefile (NS_TCL_LIB)– source in tcl/lib/ns-lib.tcl– recompile

Page 27: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Example: Agent/Message

n0 n1

n4

n5

n2

n3

128Kb, 50ms

10Mb, 1ms 10Mb, 1ms

C Ccrosstraffic

S R

msg agent

Page 28: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message

• A UDP agent (without UDP header)• Up to 64 bytes user message• Good for fast prototyping a simple idea• Usage requires extending ns functionality

SS RR

pkt: 64 bytesof arbitrary string

Receiver-sideprocessing

Page 29: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 1

• Define senderclass Sender –superclass Agent/Messageclass Sender –superclass Agent/Message

# Message format: “Addr Op SeqNo”# Message format: “Addr Op SeqNo”

Sender instproc send-next {} {Sender instproc send-next {} {

$self instvar seq_ agent_addr_$self instvar seq_ agent_addr_

$self send “$agent_addr_ send $seq_”$self send “$agent_addr_ send $seq_”

incr seq_incr seq_

global nsglobal ns

$ns at [expr [$ns now]+0.1] "$self send-next"$ns at [expr [$ns now]+0.1] "$self send-next"

}}

Page 30: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 2

• Define sender packet processing

Sender instproc Sender instproc recvrecv msg { msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts "Sender gets ack $seq from $sdr"puts "Sender gets ack $seq from $sdr"

}}

Page 31: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 3

• Define receiver packet processing

Class Receiver –superclass Agent/MessageClass Receiver –superclass Agent/Message

Receiver instproc Receiver instproc recvrecv msg { msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts “Receiver gets seq $seq from $sdr”puts “Receiver gets seq $seq from $sdr”

$self send “$agent_addr_ ack $seq”$self send “$agent_addr_ ack $seq”

}}

Page 32: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 4

• Scheduler and tracing

# Create scheduler# Create scheduler

set ns [new Simulator]set ns [new Simulator]

# Turn on Tracing# Turn on Tracing

set fd [new “message.nam” w]set fd [new “message.nam” w]

$ns namtrace-all $fd$ns namtrace-all $fd

Page 33: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 5

• Topologyfor {set i 0} {$i < 6} {incr i} {for {set i 0} {$i < 6} {incr i} {

set n($i) [$ns node]set n($i) [$ns node]}}$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail

$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(1) $n(0) 5$ns queue-limit $n(1) $n(0) 5

Page 34: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 6

• Routing

# Packet loss produced by queueing# Packet loss produced by queueing

# Routing protocol: let’s run distance vector# Routing protocol: let’s run distance vector

$ns rtproto DV$ns rtproto DV

Page 35: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 7

• Cross trafficset udp0 [new Agent/UDP]set udp0 [new Agent/UDP]$ns attach-agent $n(2) $udp0$ns attach-agent $n(2) $udp0set null0 [new Agent/NULL]set null0 [new Agent/NULL]$ns attach-agent $n(4) $null0$ns attach-agent $n(4) $null0$ns connect $udp0 $null0$ns connect $udp0 $null0

set exp0 [new Application/Traffic/Exponential]set exp0 [new Application/Traffic/Exponential]$exp0 set rate_ 128k$exp0 set rate_ 128k$exp0 attach-agent $udp0$exp0 attach-agent $udp0$ns at 1.0 “$exp0 start”$ns at 1.0 “$exp0 start”

Page 36: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 8

• Message agentsset sdr [new Sender]set sdr [new Sender]$sdr set packetSize_ 1000$sdr set packetSize_ 1000

set rcvr [new Receiver]set rcvr [new Receiver]$rcvr set packetSize_ 40$rcvr set packetSize_ 40

$ns attach $n(3) $sdr$ns attach $n(3) $sdr$ns attach $n(5) $rcvr$ns attach $n(5) $rcvr$ns connect $sdr $rcvr$ns connect $sdr $rcvr$ns connect $rcvr $sdr$ns connect $rcvr $sdr$ns at 1.1 “$sdr send-next”$ns at 1.1 “$sdr send-next”

Page 37: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Step 9

• End-of-simulation wrapper (as usual)

$ns at 2.0 finish$ns at 2.0 finish

proc finish {} {proc finish {} {

global ns fdglobal ns fd

$ns flush-trace$ns flush-trace

close $fdclose $fd

exit 0exit 0

}}

Page 38: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Agent/Message: Result

• Example output> ./ns msg.tcl> ./ns msg.tclReceiver gets seq 0 from 0Receiver gets seq 0 from 0Sender gets ack 0 from 1Sender gets ack 0 from 1Receiver gets seq 1 from 0Receiver gets seq 1 from 0Sender gets ack 1 from 1Sender gets ack 1 from 1Receiver gets seq 2 from 0Receiver gets seq 2 from 0Sender gets ack 2 from 1Sender gets ack 2 from 1Receiver gets seq 3 from 0Receiver gets seq 3 from 0Sender gets ack 3 from 1Sender gets ack 3 from 1Receiver gets seq 4 from 0Receiver gets seq 4 from 0Sender gets ack 4 from 1Sender gets ack 4 from 1Receiver gets seq 5 from 0Receiver gets seq 5 from 0

Page 39: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Add Your Changes into ns

TK8.0 OTcl tclclTcl8.0 ns-2 nam-1

tcl

ex test lib

...

...

examples validation tests

C++ code

OTcl code

ns-allinone

mcastmysrc

msg.tcl

Page 40: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Add Your Change into ns

• tcl/lib/ns-lib.tclClass SimulatorClass Simulator……source ../mysrc/msg.tclsource ../mysrc/msg.tcl

• MakefileNS_TCL_LIB = \NS_TCL_LIB = \tcl/mysrc/msg.tcl \tcl/mysrc/msg.tcl \……

– Or: change Makefile.in, make distcleanmake distclean, then ./configure./configure

Page 41: 2015-12-25 The Creation of NS2 Simulated Environment ( 1 ) LI Chengbo lichengbo518@stu.snnu.edu.cn

23/4/21

Adding New Components

• In otcl

• otcl and C++ linkage

• In C++