2.2 Simple Simulation Example
이 section은 간단한 NS simulation script를 보여주며, 각 line이 무엇을 하는지를 설명한다. 예제3 은 간단한 network 구성을 생성하는 OTcl script이며, 그림4.에 있는 시뮬레이션 scenario를 동작한다. 이 시뮬레이션을 동작하기 위해서 "ns-simple"를 다운받고, shell prompt에서 "ns ns-simple.tcl"을 입력하라.
Example 3. A Simple NS Simulation Script
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
다음은 위 script에 대한 설명이다. 일반적으로 NS script는 Simulator object instance를 만들어서 시작한다.
○ set ns[new Simulator]:
NS simulator object instance를 생성하며, 변수 ns로 그것을 할당한다.(italics은 이 section에서 변수와 값으로 사용된다.) 이 라인의 역할은 다음과 같다.
-. packet format를 초기화(여기서는 이것을 무시)
-. scheduler를 생성(default는 calendar scheduler임)
-. default address format를 선택(여기서는 이것을 무시)
이 "Simulator" object는 다음의 일을 하는 member 함수를 가진다.
-. node와 link같은 복합 object를 생성(desired later)
-. 생성된 network 구성 object를 연결(ex. attach-agent)
-. network 구성 parameter를 set(mostly for compound object)
-. agent 사이의 연결 생성(ex. "tcp"와 "sink"사이의 연결을 만든다)
-. NAM display option을 규정
-. Etc.
member 함수의 대부분은 simulation setup(Overview section에서 plumbing 함수와 관련된)과 scheduling을 위해서 있다. 그러나, 그들 중 대부분은 NAM display를 위해서 있다. "Simulator" object member 함수 구현은 "ns-2/tcl/lib/ns-lib.tcl" file내에 위치해 있다.
○ $ns color fid color:
flow id(fid)에 의해 규정된 flow에 대한 packet의 color를 set 하는 것. 이 "Simulator" object의 member 함수는 NAM display를 위한 것이다. 그리고 실제적인 시뮬레이션 상에 어떤 효과도 미치지 않는다.
○ $ns namtrace-all file-descriptor:
이 member 함수는 NAM input format에 있는 simulation trace를 기록하기 위한 시뮬레이터를 말한다. 그것은 또한 trace가 $ns flush-trace 명령에 의해 나중에 쓰여질 file name을 준다.
○ proc finish {}:
이 시뮬레이션 뒤에 $ns at 5.0 "finish" 명령에 의해 호출된다. 이 함수에서 post-simulation process가 규정된다.
○ set n0 [$ns node]:
member 함수 node는 node를 생성한다. NS에 있는 node는 address와 port classifier들로 만들어진 복합 object이다.(다음 section에서 기술된다) 사용자는 주소와 port classifier object를 분리하여 생성하고, 그들을 함께 연결함으로써 node를 생성할 수 있다. 그러나, 이 Simulator object의 member 함수는 job를 보다 쉽게 만든다. node가 생성되는 방법을 보기 위해 file "ns-2/tcl/libs/ns-lib.tcl" 과 "ns-2/tcl/ns-node.tcl"을 살펴보라.
○ $ns duplex-link node1 node2 bandwidth delay queue-type:
상술된 bandwidth와 delay의 두 개의 simplex link를 생성하며, 두 개의 상술된 node를 연결한다. NS에서 node의 출력 queue는 link의 부분으로 구현된다. 그러므로, user는 link를 생성할 때 queue type를 상술해야 한다. 위의 시뮬레이션 script에서, DropTail queue가 사용된다. 만일 독자가 RED queue를 사용하기를 원한다면, 간단하게 DropTail을 RED로 대치하면 된다. link의 NS 구현은 다음 section에서 보여진다. node처럼, link도 compound object이며 user는 그것의 sub-object를 생성하고 그들과 node를 연결할 수 있다. Link source code는 "ns-2/tcl/libs/ns-lib.tcl"과 "ns-2/tcl/libs/ns-link.tcl" 파일에서 찾을 수 있다. 주목해야하는 하나는 당신이 lossy link(실제 user는 어떤 network object를 만들고 삽입할 수 있다.) 를 시뮬레이트 하기 위해서 link component 안에서 error module를 삽입할 수 있다. 이것을 어떻게 찾는가 하는 것은 NS document를 참고하라.
○ $ns queue-limit node1 node2 number:
이 라인은 상술된 숫자로 node1고 node2를 연결하는 두 개의 simplex link의 queue limit를 set한다. 이 점에서, 저자는 얼마나 많은 시뮬레이터 object의 member 함수의 종류들이 유용한지, 그리고 그것들이 무엇인지를 알지 못한다. "ns-2/tcl/libs/ns-lib.tcl"과 "ns-2/tcl/libs/ns-link.tcl"을 찾아보거나, 보다 자세한 정보를 찾으려면 NS document를 참고하라.
○ $ns duplex-link-op node1 node2...:
line의 다음 couple는 NAM display를 위해서 사용된다. 이 라인의 효과를 보기 위해서 user는 이 라인을 구성하고 시뮬레이션을 시도할 수 있다.
지금, 기본적인 network setup이 행해졌다. 다음에 해야할 일은 TCP와 UDP 같은 traffic agent, FTP와 CBR같은 traffic source를 setup하는 것이다. 그리고 그것들을 각각 node와 agent로 접속하는 것이다.
○ set tcp[new Agent/TCP]:
이 라인은 TCP agent를 생성하는 방법을 보여준다. 그러나, 일반적으로 user는 이런 방법으로 어떤 agent나 traffic source를 생성할 수 있다. Agent와 traffic source는 사실상 기본적인 object(not compound object)이며, 대부분 C++에서 구현되고 OTcl로 링크된다. 그러므로, 이러한 object instance를 생성하는 특수한 Simulator object member 함수는 없다. Agentsk traffic source를 생성하기 위해서, User는 object class 이름(Agent/TCP, Agent/TCPSink, Application/FTP 등등)을 알아야 한다. 이 정보는 NS document나 이 document에서 부분적으로 찾을 수 있다. 그러나, one shortcut는 "ns-2/tcl/libs/ns-default.tcl" file을 보는 것이다. 이 file은 가용한 network object에 대한 default 구성 parameter 값 setting을 포함한다. 그러므로, 그것은 어떤 종류의 network object가 NS에 유용한가 그리고 구성 parameter가 무엇인가에 대한 훌륭한 indicator로서 동작한다.
○ $ns attach-agent node agent:
attach-agent member 함수는 node object로 생성된 agent object를 접속한다. 실제적으로, 이 함수는 규정된 node의 attach member 함수를 호출한다. 그것은 그 자신으로 주어진 agent를 접속한다. 그러므로, user는 예를 들면, $n0 attach $tcp에 의해 동일한 일을 할 수 있다. 유사하게, 각 agent object는 그 자신으로 traffic source object를 접속하는 member 함수 attach-agent를 가진다.
○ $ns connect agent1 agent2:
서로간에 통신을 할 두 개의 agent가 생성된 후에, 다음 일은 그들 사이에 logical network 연결을 설정하는 것이다. 이 라인은 각기 다른 network와 port address pair로 목적지 address를 setting함으로서 network 연결을 설정한다.
network 구성이 모두 완료되었다고 가정하면, 다음에 해야할 일은 simulation 시나리오(시뮬레이션 scheduling 등)를 쓰는 일이다. Simulator object는 많은 scheduling member 함수를 갖는다. 그러나, 대부분 사용되는 방법은 다음과 같다.
○ $ns at time "string":
Simulator object의 member 함수는 주어진 simulation time에 규정된 string의 실행을 schedule하기 위해 scheduler(scheduler_ 는 script 시작 부분에서 [net scheduler] 명령에 의해 생성된 scheduler object를 지시하는 변수이다.)를 만든다. 예를 들어, $ns at 0.1 "$cbr start"는 data 전송을 위해 CBR을 시작하는 CBR traffic source object의 start member 함수를 호출하는 call를 만들 것이다. NS에서 보통 traffic source가 실제적인 data를 전송하지는 않지만 그것은 전송해야하는 data가 많으며, 전송할 data가 얼마나 많은지 알고 있다는 것을 기초적인 Agent에게 공지하고 packet을 생성하며 그것들을 전송한다.
모든 network 구성 후에, scheduling과 post-simulation 절차 specification이 행해진다. 남아있는 유일한 일은 시뮬레이션을 작동시키는 것이다. 이것은 $ns run으로 실행된다.
이 section은 간단한 NS simulation script를 보여주며, 각 line이 무엇을 하는지를 설명한다. 예제3 은 간단한 network 구성을 생성하는 OTcl script이며, 그림4.에 있는 시뮬레이션 scenario를 동작한다. 이 시뮬레이션을 동작하기 위해서 "ns-simple"를 다운받고, shell prompt에서 "ns ns-simple.tcl"을 입력하라.
Example 3. A Simple NS Simulation Script
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
다음은 위 script에 대한 설명이다. 일반적으로 NS script는 Simulator object instance를 만들어서 시작한다.
○ set ns[new Simulator]:
NS simulator object instance를 생성하며, 변수 ns로 그것을 할당한다.(italics은 이 section에서 변수와 값으로 사용된다.) 이 라인의 역할은 다음과 같다.
-. packet format를 초기화(여기서는 이것을 무시)
-. scheduler를 생성(default는 calendar scheduler임)
-. default address format를 선택(여기서는 이것을 무시)
이 "Simulator" object는 다음의 일을 하는 member 함수를 가진다.
-. node와 link같은 복합 object를 생성(desired later)
-. 생성된 network 구성 object를 연결(ex. attach-agent)
-. network 구성 parameter를 set(mostly for compound object)
-. agent 사이의 연결 생성(ex. "tcp"와 "sink"사이의 연결을 만든다)
-. NAM display option을 규정
-. Etc.
member 함수의 대부분은 simulation setup(Overview section에서 plumbing 함수와 관련된)과 scheduling을 위해서 있다. 그러나, 그들 중 대부분은 NAM display를 위해서 있다. "Simulator" object member 함수 구현은 "ns-2/tcl/lib/ns-lib.tcl" file내에 위치해 있다.
○ $ns color fid color:
flow id(fid)에 의해 규정된 flow에 대한 packet의 color를 set 하는 것. 이 "Simulator" object의 member 함수는 NAM display를 위한 것이다. 그리고 실제적인 시뮬레이션 상에 어떤 효과도 미치지 않는다.
○ $ns namtrace-all file-descriptor:
이 member 함수는 NAM input format에 있는 simulation trace를 기록하기 위한 시뮬레이터를 말한다. 그것은 또한 trace가 $ns flush-trace 명령에 의해 나중에 쓰여질 file name을 준다.
○ proc finish {}:
이 시뮬레이션 뒤에 $ns at 5.0 "finish" 명령에 의해 호출된다. 이 함수에서 post-simulation process가 규정된다.
○ set n0 [$ns node]:
member 함수 node는 node를 생성한다. NS에 있는 node는 address와 port classifier들로 만들어진 복합 object이다.(다음 section에서 기술된다) 사용자는 주소와 port classifier object를 분리하여 생성하고, 그들을 함께 연결함으로써 node를 생성할 수 있다. 그러나, 이 Simulator object의 member 함수는 job를 보다 쉽게 만든다. node가 생성되는 방법을 보기 위해 file "ns-2/tcl/libs/ns-lib.tcl" 과 "ns-2/tcl/ns-node.tcl"을 살펴보라.
○ $ns duplex-link node1 node2 bandwidth delay queue-type:
상술된 bandwidth와 delay의 두 개의 simplex link를 생성하며, 두 개의 상술된 node를 연결한다. NS에서 node의 출력 queue는 link의 부분으로 구현된다. 그러므로, user는 link를 생성할 때 queue type를 상술해야 한다. 위의 시뮬레이션 script에서, DropTail queue가 사용된다. 만일 독자가 RED queue를 사용하기를 원한다면, 간단하게 DropTail을 RED로 대치하면 된다. link의 NS 구현은 다음 section에서 보여진다. node처럼, link도 compound object이며 user는 그것의 sub-object를 생성하고 그들과 node를 연결할 수 있다. Link source code는 "ns-2/tcl/libs/ns-lib.tcl"과 "ns-2/tcl/libs/ns-link.tcl" 파일에서 찾을 수 있다. 주목해야하는 하나는 당신이 lossy link(실제 user는 어떤 network object를 만들고 삽입할 수 있다.) 를 시뮬레이트 하기 위해서 link component 안에서 error module를 삽입할 수 있다. 이것을 어떻게 찾는가 하는 것은 NS document를 참고하라.
○ $ns queue-limit node1 node2 number:
이 라인은 상술된 숫자로 node1고 node2를 연결하는 두 개의 simplex link의 queue limit를 set한다. 이 점에서, 저자는 얼마나 많은 시뮬레이터 object의 member 함수의 종류들이 유용한지, 그리고 그것들이 무엇인지를 알지 못한다. "ns-2/tcl/libs/ns-lib.tcl"과 "ns-2/tcl/libs/ns-link.tcl"을 찾아보거나, 보다 자세한 정보를 찾으려면 NS document를 참고하라.
○ $ns duplex-link-op node1 node2...:
line의 다음 couple는 NAM display를 위해서 사용된다. 이 라인의 효과를 보기 위해서 user는 이 라인을 구성하고 시뮬레이션을 시도할 수 있다.
지금, 기본적인 network setup이 행해졌다. 다음에 해야할 일은 TCP와 UDP 같은 traffic agent, FTP와 CBR같은 traffic source를 setup하는 것이다. 그리고 그것들을 각각 node와 agent로 접속하는 것이다.
○ set tcp[new Agent/TCP]:
이 라인은 TCP agent를 생성하는 방법을 보여준다. 그러나, 일반적으로 user는 이런 방법으로 어떤 agent나 traffic source를 생성할 수 있다. Agent와 traffic source는 사실상 기본적인 object(not compound object)이며, 대부분 C++에서 구현되고 OTcl로 링크된다. 그러므로, 이러한 object instance를 생성하는 특수한 Simulator object member 함수는 없다. Agentsk traffic source를 생성하기 위해서, User는 object class 이름(Agent/TCP, Agent/TCPSink, Application/FTP 등등)을 알아야 한다. 이 정보는 NS document나 이 document에서 부분적으로 찾을 수 있다. 그러나, one shortcut는 "ns-2/tcl/libs/ns-default.tcl" file을 보는 것이다. 이 file은 가용한 network object에 대한 default 구성 parameter 값 setting을 포함한다. 그러므로, 그것은 어떤 종류의 network object가 NS에 유용한가 그리고 구성 parameter가 무엇인가에 대한 훌륭한 indicator로서 동작한다.
○ $ns attach-agent node agent:
attach-agent member 함수는 node object로 생성된 agent object를 접속한다. 실제적으로, 이 함수는 규정된 node의 attach member 함수를 호출한다. 그것은 그 자신으로 주어진 agent를 접속한다. 그러므로, user는 예를 들면, $n0 attach $tcp에 의해 동일한 일을 할 수 있다. 유사하게, 각 agent object는 그 자신으로 traffic source object를 접속하는 member 함수 attach-agent를 가진다.
○ $ns connect agent1 agent2:
서로간에 통신을 할 두 개의 agent가 생성된 후에, 다음 일은 그들 사이에 logical network 연결을 설정하는 것이다. 이 라인은 각기 다른 network와 port address pair로 목적지 address를 setting함으로서 network 연결을 설정한다.
network 구성이 모두 완료되었다고 가정하면, 다음에 해야할 일은 simulation 시나리오(시뮬레이션 scheduling 등)를 쓰는 일이다. Simulator object는 많은 scheduling member 함수를 갖는다. 그러나, 대부분 사용되는 방법은 다음과 같다.
○ $ns at time "string":
Simulator object의 member 함수는 주어진 simulation time에 규정된 string의 실행을 schedule하기 위해 scheduler(scheduler_ 는 script 시작 부분에서 [net scheduler] 명령에 의해 생성된 scheduler object를 지시하는 변수이다.)를 만든다. 예를 들어, $ns at 0.1 "$cbr start"는 data 전송을 위해 CBR을 시작하는 CBR traffic source object의 start member 함수를 호출하는 call를 만들 것이다. NS에서 보통 traffic source가 실제적인 data를 전송하지는 않지만 그것은 전송해야하는 data가 많으며, 전송할 data가 얼마나 많은지 알고 있다는 것을 기초적인 Agent에게 공지하고 packet을 생성하며 그것들을 전송한다.
모든 network 구성 후에, scheduling과 post-simulation 절차 specification이 행해진다. 남아있는 유일한 일은 시뮬레이션을 작동시키는 것이다. 이것은 $ns run으로 실행된다.
'Network > Ns2_Lecture' 카테고리의 다른 글
Implementing a New Manet Unicast Routing Protocol in NS2 Version 0.2 (0) | 2009.01.18 |
---|---|
NS2 - 무선환경 시뮬레이션 파라메터 설명 (0) | 2009.01.18 |
NS by Example - Network Components (0) | 2009.01.18 |
802.11 MAC code in NS-2 (version 2.28) (0) | 2009.01.18 |
802.11 MAC code in NS-2 (version 2.28) (0) | 2009.01.18 |