Extending ns Padma Haldar USC/ISI 1
Outline � Extending ns � In OTcl � In C++ � Debugging 2
ns Directory Structure ns-allinone Tcl8.3 TK8.3 OTcl tclcl ns-2 nam-1 ... tcl C+ + code ex test mcast ... lib examples validation tests OTcl code 3
Extending ns in OTcl � If you don’t want to compile � source your changes in your sim scripts � Otherwise � Modifying code; recompile � Adding new files • Change Makefile (NS_TCL_LIB), tcl/lib/ns-lib.tcl • Recompile 4
Example: Agent/Message C n2 n4 C cross 128Kb, 50ms traffic n0 n1 10Mb, 1ms 10Mb, 1ms S n3 R n5 msg agent 5
Agent/Message pkt: 64 bytes Receiver-side of arbitrary processing string S R S R � A UDP agent (without UDP header) � Up to 64 bytes user message � Good for fast prototyping a simple idea � Usage requires extending ns functionality 6
Agent/Message: Step 1 � Define sender class Sender – –superclass superclass Agent/Message Agent/Message class Sender # Message format: “Addr # Message format: “ Addr Op Op SeqNo SeqNo” Sender Sender instproc instproc send send-next {} { next {} { $self instvar $self instvar seq seq_ _ agent_addr agent_addr_ $self send “$ $self send “$agent_addr agent_addr_ send $ _ send $seq seq_” _” incr incr seq seq_ global ns global ns $ns at [ $ns at [expr expr [$ns now]+0.1] "$self send [$ns now]+0.1] "$self send-next" next" } 7
Agent/Message: Step 2 � Define sender packet processing Sender instproc Sender instproc recv recv msg msg { { $self instvar $self instvar agent_addr agent_addr_ _ set sdr set sdr [ [lindex lindex $ $msg msg 0] 0] set seq seq [ [lindex lindex $ $msg msg 2] 2] set puts "Sender gets ack ack $ $seq seq from $ from $sdr sdr" " puts "Sender gets } } 8
Agent/Message: Step 3 � Define receiver packet processing Class Receiver – Class Receiver –superclass superclass Agent/Message Agent/Message Receiver instproc Receiver instproc recv recv msg msg { { $self instvar instvar agent_addr agent_addr_ _ $self set sdr sdr [ [lindex lindex $ $msg msg 0] 0] set set seq seq [ [lindex lindex $ $msg msg 2] 2] set puts “Receiver gets seq seq $ $seq seq from $ from $sdr sdr” ” puts “Receiver gets $self send “$addr addr_ _ ack ack $ $seq seq” ” $self send “$ } } 9
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 fd [new “ [new “message.tr message.tr” w] ” w] set $ns trace- -all $ all $fd fd $ns trace 10
Agent/Message: Step 5 � Topology for {set i 0} {$i < 6} { for {set i 0} {$i < 6} {incr incr i} { i} { set set n($i n($i) [$ns node] ) [$ns node] } $ns duplex $ns duplex-link $n(0) $n(1) 128kb 50ms link $n(0) $n(1) 128kb 50ms DropTail DropTail $ns duplex-link $n(1) $n(4) 10Mb 1ms $ns duplex link $n(1) $n(4) 10Mb 1ms DropTail DropTail $ns duplex $ns duplex-link $n(1) $n(5) 10Mb 1ms link $n(1) $n(5) 10Mb 1ms DropTail DropTail $ns duplex $ns duplex-link $n(0) $n(2) 10Mb 1ms link $n(0) $n(2) 10Mb 1ms DropTail DropTail $ns duplex $ns duplex-link $n(0) $n(3) 10Mb 1ms link $n(0) $n(3) 10Mb 1ms DropTail DropTail $ns queue $ns queue-limit $n(0) $n(1) 5 limit $n(0) $n(1) 5 $ns queue-limit $n(1) $n(0) 5 $ns queue limit $n(1) $n(0) 5 11
Agent/Message: Step 6 � Routing # Packet loss produced by queueing # Packet loss produced by queueing # Routing protocol: let’s run distance # Routing protocol: let’s run distance vector vector $ns rtproto rtproto DV DV $ns 12
Agent/Message: Step 7 � Cross traffic set udp0 [new Agent/UDP] set udp0 [new Agent/UDP] $ns attach- -agent $n(2) $udp0 agent $n(2) $udp0 $ns attach set null0 [new Agent/NULL] set null0 [new Agent/NULL] $ns attach- $ns attach -agent $n(4) $null0 agent $n(4) $null0 $ns connect $udp0 $null0 $ns connect $udp0 $null0 set exp0 [new set exp0 [new Application/Traffic/Exponential] Application/Traffic/Exponential] $exp0 set rate_ 128k $exp0 set rate_ 128k $exp0 attach- -agent $udp0 agent $udp0 $exp0 attach $ns at 1.0 “$exp0 start” $ns at 1.0 “$exp0 start” 13
Agent/Message: Step 8 � Message agents set sdr sdr [new Sender] [new Sender] set $sdr sdr set set seq seq_ 0 _ 0 $ $sdr sdr set set packetSize packetSize_ 1000 _ 1000 $ set rcvr rcvr [new Receiver] [new Receiver] set $rcvr rcvr set set packetSize packetSize_ 40 _ 40 $ $ns attach- -agent $n(3) $ agent $n(3) $sdr sdr $ns attach $ns attach- $ns attach -agent $n(5) $ agent $n(5) $rcvr rcvr $ns connect $sdr sdr $ $rcvr rcvr $ns connect $ $ns at 1.1 “$sdr sdr send send- -next” next” $ns at 1.1 “$ 14
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 fd fd global ns $ns flush- -trace trace $ns flush close $fd fd close $ exit 0 exit 0 } } $ns run $ns run 15
Agent/Message: Result � Example output > ./ns > ./ns msg.tcl msg.tcl Receiver gets Receiver gets seq seq 0 from 3 0 from 3 Sender gets Sender gets ack ack 0 from 5 0 from 5 Receiver gets Receiver gets seq seq 1 from 3 1 from 3 Sender gets Sender gets ack ack 1 from 5 1 from 5 Receiver gets seq Receiver gets seq 2 from 3 2 from 3 Sender gets Sender gets ack ack 2 from 5 2 from 5 Receiver gets Receiver gets seq seq 3 from 3 3 from 3 Sender gets ack Sender gets ack 3 from 5 3 from 5 Receiver gets Receiver gets seq seq 4 from 3 4 from 3 Sender gets Sender gets ack ack 4 from 5 4 from 5 Receiver gets seq Receiver gets seq 5 from 3 5 from 3 16
Add Your Changes into ns ns-allinone Tcl8.3 TK8.3 OTcl tclcl ns-2 nam-1 ... tcl C+ + code mcast ... lib ex test mysrc examples validation tests msg.tcl OTcl code 17
Add Your Change into ns � tcl/lib/ns-lib.tcl Class Simulator Class Simulator … … source ../mysrc/msg.tcl source ../ mysrc/msg.tcl � Makefile NS_TCL_LIB = \ \ NS_TCL_LIB = tcl/mysrc/msg.tcl \ \ tcl/mysrc/msg.tcl … … � Or: change Makefile.in, make distclean , make distclean then ./configure ./configure -- --enable enable- -debug , debug , make depend and and make make make depend 18
Outline � Extending ns � In OTcl � In C++ • New components 19
Extending ns in C++ � Modifying code � make depend � Recompile � Adding code in new files � Change Makefile � make depend � recompile 20
Creating New Components � Guidelines � Two styles � New agent based on existing packet headers � Add new packet header 21
Guidelines � Decide position in class hierarchy � I.e., which class to derive from? � Create new packet header (if necessary) � Create C++ class, fill in methods � Define OTcl linkage (if any) � Write OTcl code (if any) � Build (and debug) 22
New Agent, Old Header � TCP jump start � Wide-open transmission window at the beginning � From cwnd _ += 1 To cwnd cwnd_ += 1 cwnd_ = _ = MAXWIN_ MAXWIN_ 23
TCP Jump Start – Step 1 TclObject Handler NsObject Connector Classifier Queue Delay Agent Trace AddrClassifier McastClasifier DropTail RED TCP Enq Deq Drop JS Reno SACK 24
TCP Jump Start – Step 2 � New file: tcp-js.h class JSTCPAgent class JSTCPAgent : public : public TcpAgent TcpAgent { { public: public: virtual void set_initial_window set_initial_window() { () { virtual void cwnd_ = MAXWIN_; _ = MAXWIN_; cwnd } } private: private: int MAXWIN_; MAXWIN_; int }; }; 25
TCP Jump Start – Step 3 � New file: tcp-js.cc static JSTcpClass : public TclClass { public: JSTcpClass() : TclClass("Agent/TCP/JS") {} TclObject* create(int, const char*const*) { return (new JSTcpAgent()); } }; JSTcpAgent::JSTcpAgent() { bind(“MAXWIN_”, MAXWIN_); } 26
Packet Format ts_ cmn header ptype_ header ip header uid_ data tcp header size_ rtp header iface_ trace header ... 27
New Packet Header � Create new header structure � Enable tracing support of new header � Create static class for OTcl linkage (packet.h) � Enable new header in OTcl (tcl/lib/ns- packet.tcl) � This does not apply when you add a new field into an existing header! 28
How Packet Header Works Packet PacketHeader/ Common next_ hdrlen_ size determined bits_ hdr_cmn at compile time PacketHeader/ I P size determined size determined hdr_ip at simulator at compile time PacketHeader/ TCP startup time size determined (PacketHeaderManager) hdr_tcp at compile time …… 29
Example: Agent/Message � New packet header for 64-byte message � New transport agent to process this new header 30
Recommend
More recommend