introduction to ns2 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to ns2 PowerPoint Presentation
Download Presentation
Introduction to ns2

Loading in 2 Seconds...

play fullscreen
1 / 54

Introduction to ns2 - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

Introduction to ns2. Network Simulation and Simulator Internals 潘仁義 jypan@comm.ccu.edu.tw. Modified from Su Wen ’ s ns2 ppt suwen@dcs.uky.edu & Padmaparna Haldar ’ s haldar@isi.edu. The Network Simulator - ns-2. http://www.isi.edu/nsnam/ns/

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to ns2' - xuan


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to ns2

Introduction to ns2

Network Simulation and Simulator Internals

潘仁義

jypan@comm.ccu.edu.tw

Modified from Su Wen’s ns2 ppt suwen@dcs.uky.edu

& Padmaparna Haldar’s

haldar@isi.edu

the network simulator ns 2
The Network Simulator - ns-2
  • http://www.isi.edu/nsnam/ns/
    • The source code and documentation is currently maintained by VINT project at ISI
  • http://nsnam.isi.edu/nsnam/index.php/Main_Page
    • Sept 3, 2007: ns-2.32 released.
    • Mar 10, 2007: ns-2.31 released.
    • July 2, 2006: ns-3 project announced.
  • NS2 is a discrete event simulator targeted at networking research
  • NS2is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend

抽考: ns2安裝

ns 2 overview
ns-2 Overview
  • Collection of various protocols at multiple layers
    • TCP(reno, tahoe, vegas, sack)
    • MAC(802.11, 802.3, TDMA)
    • Ad-hoc Routing (DSDV, DSR, AODV, TORA)
    • Sensor Network (diffusion, gaf)
    • Multicast protocols, Satellite protocols, and many others
  • Codes are contributed from multiple research communities
    • Good: Large set of simulation modules
    • Bad: Level of support and documentation varies
  • The source code and documentation is currently maintained by VINT project at ISI
documentation
Documentation
  • introductory: Marc Greis's tutorial
  • reference: Ns Manual (formerly called "ns Notes and Documentation")
  • ns by Example
  • Practical Programming in Tcl and Tk (http://www.beedub.com/book/)
  • http://hpds.ee.ncku.edu.tw/~smallko/ns2/ns2.htm
current status
Current Status
  • ns-2 (2.1b6) Simulator Core
    • 100K lines of C++
    • 70K lines of OTcl
    • 30K lines of test suite
    • 20K lines of documentation
  • Other Components
    • Tcl/TK 8.x, OTcl, TclCL, nam-1
    • Tcl-debug, GT-ITM, xgraph, …
ns directory structure
ns Directory Structure

ns-allinone

Tcl8.0

TK8.0

OTcl

tclcl

ns-2

nam-1

C++ code

...

tcl

ex

test

mcast

lib

...

examples

validation tests

OTcl code

simple.tcl

running simulations with ns
Running simulations with ns
  • Compile the simulator core (“ns”)
  • Write a simulation script in Otcl
    • e.g. my-test.tcl
  • Running the simulator
    • e.g. ns my-test.tcl
hello world
Hello World

simple.tcl

set sim [new Simulator]

$sim at 1 “puts \“Hello World!\””

$sim at 1.5 “exit”

$sim run

arches 74% ns simple.tcl

Hello World!

arches 75%

discrete event simulation
Discrete Event Simulation
  • Model world as events
    • Simulator has list of events
    • Process: take next one, run it, until done
    • Each event happens in an instant of virtual (simulated) time, but takes an arbitrary amount of real time
  • Ns uses simple model: single thread of control => no locking or race conditions to worry about (very easy)
discrete event examples

A

B

Discrete Event Examples

simple

queuing

model:

Consider two nodes

on an Ethernet:

t=1, A enqueues pkt on LAN

t=1.01, LAN dequeues pkt

and triggers B

t=1.0: A sends pkt to NIC

A’s NIC starts carrier sense

t=1.005: A’s NIC concludes cs,

starts tx

t=1.006: B’s NIC begins reciving pkt

t=1.01: B’s NIC concludes pkt

B’s NIC passes pkt to app

detailed

CSMA/CD

model:

ns 2 environment

Simulation Scenario

2

C++ Implementation

1

ns-2 Environment

set ns_ [new Simulator]

set node_(0) [$ns_ node]

set node_(1) [$ns_ node]

Tcl Script

class MobileNode : public Node

{

friend class PositionHandler;

public:

MobileNode();

}

why two languages tcl c
Why two languages? (Tcl & C++)
  • C++: Detailed protocol simulations require systems programming language
    • byte manipulation, packet processing, algorithm implementation
    • Run time speed is important
    • Turn around time (run simulation, find bug, fix bug, recompile, re-run) is slower
  • Tcl: Simulation of slightly varying parameters or configurations
    • quickly exploring a number of scenarios
    • iteration time (change the model and re-run) is more important
c and otcl separation
C++ and OTcl Separation
  • “data” / control separation
    • C++ for “data”:
      • per packet processing, core of ns
      • fast to run, detailed, complete control
    • OTcl for control:
      • Simulation scenario configurations
      • Periodic or triggered action
      • Manipulating existing C++ objects
      • fast to write and change
  • running vs. writing speed
  • Learning and debugging (two languages)
using ns
Using ns

Problem

Result

analysis

Simulation

model

Modify

ns

Setup/run

simulation

with ns

summary generic script structure
Summary: Generic Script Structure

set ns [new Simulator]

# [Turn on tracing]

# Create topology

# Setup packet loss, link dynamics

# Create routing agents

# Create:

# - multicast groups

# - protocol agents

# - application and/or setup traffic sources

# Post-processing procs

# Start simulation

basic tcl
variables:

set x 10

puts “x is $x”

functions and expressions:

set y [pow x 2]

set y [expr x*x]

control flow:

if {$x > 0} { return $x } else {

return [expr -$x] }

while { $x > 0 } {

puts $x

incr x –1

}

procedures:

proc pow {x n} {

if {$n == 1} { return $x }

set part [pow x [expr $n-1]]

return [expr $x*$part]

}

Also lists, associative arrays, etc.

=> can use a real programming language to build network topologies, traffic models, etc.

Basic Tcl
basic otcl
Class Person

# constructor:

Person instproc init {age} {

$selfinstvar age_

set age_ $age

}

# method:

Person instproc greet {} {

$selfinstvar age_

puts “$age_ years old: How are you doing?”

}

# subclass:

Class Kid -superclass Person

Kid instproc greet {} {

$selfinstvar age_

puts “$age_ years old kid: What’s up, dude?”

}

set a [new Person 45]

set b [new Kid 15]

$a greet

$b greet

# “new” in ns2 only

Basic otcl

Person a 45; Kid b 15

a greet; b greet

Person info instances

a info class; a info vars

=> can easily make variations of existing things (TCP, TCP/Reno)

otcl and c the duality
Otcl and C++: The Duality
  • OTcl (object variant of Tcl) and C++ share class hierarchy
  • TclCL is glue library that makes it easy to share functions, variables, etc

C++

C++/OTcl

split objects

otcl

c and otcl linkage
C++ and OTcl Linkage
  • Class Tcl: instance of OTcl interpreter

Tcl& tcl = Tcl::instance();

tcl.evalc(“puts stdout hello world”);

tcl.result() and tcl.error()

  • Class TclObject and TclClass
    • Variable bindings

bind(“rtt_”, &t_rtt_)

    • Invoking command method in shadow class

$tcp advanceby 10

c and otcl linkage ii
C++ and Otcl linkage II
  • Some important objects:
    • NsObject: has recv() method
    • Connector: has target() and drop()
    • BiConnector: uptarget() & downtarget()
  • 目前悟性不夠
tclobject hierarchy and shadowing
TclObject: Hierarchy and Shadowing

C++ class

hierarchy

OTcl class

hierarchy

TclObject

TclObject

static JSTcpClass : public TclClass {

public:

JSTcpClass():TclClass("Agent/TCP/JS"){}

TclObject*

create(int,const char*const*)

{ return (new JSTcpAgent());}

};

Agent

Agent

JSTcpAgent

Agent/TCP/JS

*tcp

_o123

Agent/TCP/JS OTcl

shadow object

Agent/TCP/JS C++

object

tclobject creation and deletion

Agent/TCP/JS

constructor

Agent/TCP

constructor

TclObject

constructor

invoke parent

constructor

create C++

object

which C++

object to create?

– TclClass

complete

initialization

complete

initialization

create OTcl

shadow object

parent (Agent)

constructor

JSTCPAgent

constructor

TclObject (C++)

constructor

invoke parent

constructor

invoke parent

constructor

do nothing,

return

bind variables

and return

bind variables

and return

TclObject: Creation and Deletion

invoke parent

constructor

OTcl

C++

class hierarchy in ns
Class Hierarchy in ns

TclObject

NsObject

Connector

Classifier

Queue

Delay

Agent

Trace

AddrClassifier

McastClasifier

DropTail

RED

TCP

Enq

Deq

Drop

JS

Reno

SACK

tcp jump start step 1
TCP Jump Start – Step 1
  • New file: tcp-js.h

class JSTcpAgent : public TcpAgent {

public:

virtual void set_initial_window() {

cwnd_ = MAXWIN_;

}

JSTcpAgent();

private:

int MAXWIN_;

};

tcp jump start step 2
TCP Jump Start – Step 2
  • New file: tcp-js.cc

#include “tcp.h”

#include “tcp-js.h”

static class JSTcpClass : public TclClass {

public:

JSTcpClass() : TclClass("Agent/TCP/JS") {}

TclObject* create(int, const char*const*) {

return (new JSTcpAgent());

}

}class_jstcp;

JSTcpAgent::JSTcpAgent() {

bind(“MAXWIN_”, &MAXWIN_);

}

tclobject bind
TclObject::bind()
  • Link C++ member variables to OTcl object variables
  • C++

TcpAgent::TcpAgent() {

bind(“window_”, &wnd_);

… …

}

    • bind_time(), bind_bool(), bind_bw() (set with unit)
  • OTcl

set tcp [new Agent/TCP]

$tcp set window_ 200

initialization of bound variables
Initialization of Bound Variables
  • Initialization through OTcl class variables

Agent/TCP set window_ 50

  • Do all initialization of bound variables in ~ns/lib/ns-default.tcl
    • Otherwise a warning will be issued when the shadow object is created
    • After modifying, remember to make ns2
calling c functions from otcl
Calling C++ functions from Otcl
  • OTcl

set tcp [new Agent/TCP]

$tcp advance 10

  • C++

int TcpAgent::command(int argc,

const char*const* argv) {

if (argc == 3) {

if (strcmp(argv[1], “advance”) == 0) {

int newseq = atoi(argv[2]);

……

return(TCL_OK);

}

}

return (Agent::command(argc, argv);

}

tclobject command
TclObject::command()

OTcl space

no such

procedure

$tcp send

TclObject::unknown{}

$tcp cmd send

C++ space

TcpAgent::command()

match

“send”?

Yes

No

Invoke parent:

return Agent::command()

process and return

calling otcl functions from c
Calling Otcl functions from C++
  • OTcl

Agent/TCP instproc advance {num} {

set window_ [expr $window_ + $num]

return $window_

}

  • C++

Tcl& tcl = Tcl::instance();

char *result;

tcl.evalf(“%s advance %d”, name_, size);

result = tcl.result();

wnd_ = atoi(result);

embeddedtcl
EmbeddedTcl
  • How it works
    • tcl2c++: provided by TclCL, converts tcl scripts into a C++ static character array
    • Makefile.in:

tclsh8.0 bin/tcl-expand.tcl tcl/lib/ns-lib.tcl | tcl2c++ et_ns_lib > gen/ns_tcl.cc

summary
Summary
  • TclObject
    • Unified interpreted (OTcl) and compiled (C++) class hierarchies
    • Seamless access (procedure call and variable access) between OTcl and C++
  • TclClass
    • The mechanism that makes TclObject work
  • Tcl: primitives to access Tcl interpreter
event scheduler
Event Scheduler
  • Create event scheduler
    • set ns [new Simulator]
  • Schedule events
    • $ns at <time> <event>
      • <event>: any legitimate ns/tcl commands
    • $ns at 0.1 “$ftp start”
    • $ns at 4.0 “$ftp stop”
    • $ns at 5.0 “finish”
  • Start scheduler
    • $ns run
  • ns/common/scheduler.cc
    • Scheduler::command() “now”, “at”
  • ns/link/delay.cc
    • LinkDelay::recv() “s.schedule()”
extending ns in otcl
Extending ns in OTcl

ns-allinone

Tcl8.0

TK8.0

OTcl

tclcl

ns-2

nam-1

C++ code

...

tcl

mcast

lib

...

ex

test

mysrc

examples

msg.tcl

validation tests

OTcl code

add your changes into ns
Add Your Changes into ns
    • source your changes in your sim scripts
  • Or
    • add to tcl/lib/ns-lib.tcl

source ../mysrc/msg.tcl

    • Change Makefile

NS_TCL_LIB = \

tcl/mysrc/msg.tcl \

    • Recompile
scalability vs flexibility
Scalability vs Flexibility
  • It’s tempting to write all-OTcl simulation
    • Benefit: quick prototyping
    • Cost: memory + runtime
  • Solution
    • Control the granularity of your split object by migrating methods from OTcl to C++
  • Conventional Wisdom:
    • C++ for “data”
      • Per packet action
    • OTcl for control
      • Periodic or triggered action
the merit of otcl
THE Merit of OTcl
  • Smoothly adjust the granularity of scripting to balance extensibility and performance
  • With complete compatibility with existing simulation scripts

Program size, complexity

low

high

OTcl

C/C++

split objects

object granularity tips
Object Granularity Tips
  • Functionality
    • Per-packet processing  C++
    • Hooks, frequently changing code  OTcl
  • Data management
    • Complex/large data structure  C++
    • One-time configuration variables  OTcl
memory conservation tips
Memory Conservation Tips
  • Avoid trace-all
  • Use arrays for a sequence of variables
    • Instead of n$i, say n($i)
  • Avoid OTcl temporary variables
  • Use dynamic binding
    • delay_bind() instead of bind()
    • See object.{h,cc}
memory leaks
Memory Leaks
  • Purify or dmalloc, but be careful about split objects:

for {set i 0} {$i < 500} {incr i} {

set a [new RandomVariable/Constant]

}

    • It leaks memory, but can’t be detected!
  • Solution
    • Explicitly delete EVERY split object that was new-ed
otcl linkage
OTcl Linkage
  • OTcl Linkage是c++和OTcl 的一個介面。

C++ code

OLcl Linkake

OTcl code

otcl linkage2
如何使用OTcl linkage
  • Export C++ class variables to OTcl
  • - bind(): real or integer variables- bind_time(): time variable- bind_bw(): bandwidth variable- bind_bool(): boolean variable請在ns-2/tcl/lib/ns-lib.tcl設預設值,否則會有警告message

OTcl變數

C++變數

otcl linkage3
如何使用OTcl linkage
  • Export C++ Object Control Commands to OTcl
  • This is done by defining a "command" member function of your C++ object, which works as an OTcl command interpreter.
  • When OTcl object( is created and the user tries to call a member function of that object (i.e. $myagent call-my-priv-func), OTcl searches for the given member function name in the OTcl object. If the given member function name cannot be found, then it invokes the "MyAgent::command" passing the invoked OTcl member function name and arguments in argc/argv format.
otcl linkage4
如何使用OTcl linkage
  • Export C++ Object Control Commands to OTcl
  • -
otcl linkage5
如何使用OTcl linkage
  • Execute an OTcl command from C++.
  • makes an OTcl interpreter print out the value in "my_var1" and "my_var2" private member variables
  • To execute an OTcl command from C++, you should get a reference to "Tcl::instance()"
otcl linkage6
如何使用OTcl linkage
  • Compile, run and test "MyAgent“
  • put "ex-linkage.cc" file, and save it under the "ns-2" directory.
  • Open "Makefile", add "ex-linkage.o" at the end of object file list.
  • Re-compile NS using the "make" command.
  • Run the OTcl script using command "ns ex-linkage.tcl".
c guidelines
C++ 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)
where to find what1
Where to Find What?
  • ns-lib.tcl: The simulator class and most of its member function definitionsexcept for LAN, Web, and Multicast related ones are located here.
  • ns-default.tcl: The default values for configurable parameters for various network components are located here. the parameters are actually C++ variables made available to OTcl via an OTcl linkage function
  • bind(C++_variable_name, OTcl_variable_name)
where to find what2
Where to Find What?
  • ns-packet.tcl: The packet header format initialization implementation is located here. When you create a new packet header, you should register the header in this file to make the packet header initialization process and give you the offset of your header in the stack.
  • other OTcl files: Other OTcl files in this directory, The FTP application is entirely implemented in OTcl and the source code is located in "ns-source.tcl".