SlideShare una empresa de Scribd logo
1 de 72
Descargar para leer sin conexión
Network Simulator (NS2)




                                    โดย
                 นางสาวจิตติมา นิตยวรรณ Nectec/NTL
               นายโสฬส ชคัตตรยาพงษ มหาวิทยาลัยบูรพา
           นางสาวอุไรรัตน พึ่งสุนทรบัตร มหาวิทยาลัยธรรมศาสตร




                                    ปฏิบัติงาน ณ
               ศูนยเทคโนโลยีอิเล็กทรอนิกสและคอมพิวเตอรแหงชาติ
(National Electronics and Computer Technology Center :
                                  NECTEC)
 112 อุทยานวิทยาศาสตรประเทศไทย ถนนพหลโยธิน ตําบลคลองหนึ่ง อําเภอคลองหลวง
                                  จังหวัดปทุมธานี




                                    1
สารบัญ
สารบัญ                                                2

บทที่ 1 บทนํา                                         3

บทที่ 2 ทฤษฎีที่เกี่ยวของ
        2.1 Literature Reviews                        6
        2.2 การติดตั้งโปรแกรม NS2                     7

บทที่ 3 หลักการทํางานของ NS2
        3.1 การทํางานของ NS2                          13
        3.2 NS Script Structure                       14
        3.3 การจัดการไฟลขอมูลเกี่ยวกับ Trace file   20
        3.4 NS process                                23

บทที่ 4 Visualization Tools
        4.1 Nam (Network AniMator Version1)           32
        4.2 Xgraph                                    34

บทที่ 5 ตัวอยางการใชงาน NS2                         42

ภาคผนวก
    Tcl และ OTcl Programming                          56
    อธิบายตัวอยาง ns-simlple.tcl                     68

บรรณานุกรม                                            72




                                             2
Chapter 1
                                          Introduction
         ตั้งแตอดีตจนถึงปจจุบันนี้ไดมีการพัฒนาระบบเครือขายมาตลอด ซึ่งการพัฒนาระบบเครือนั้น
จะตองมีการวางโครงสรางและระบบผานอุปกรณตางๆที่ใชในการเชื่อมตอเครือขายมากมาย โดยการที่จะ
ติดตั้งระบบเครือขายสําหรับการใชงานนั้นจะตองมีการทดสอบการทํางานของระบบเครือขายจึงเปนการยาก
ที่จะทดสอบการทํางานของระบบเครือขายโดยการทดสอบผานอุปกรณตางๆ ที่ไดทําการวางระบบไวแลว
เนื่องจากการวางระบบเครือขายนั้นเปนสิ่งที่ใชงบประมาณและเวลามากจึงไมคุมที่จะทดสอบการทํางาน
ของระบบเครือขายโดยการทดสอบผานอุปกรณที่ใชในความเปนจริง จึงเกิดความจําเปนที่จะตองมีการ
จําลองการทํางานของระบบเครือขายเพื่อที่จะไดทําการศึกษาการทํางานของระบบเครือขายในดานตางๆ ทั้ง
พฤติกรรมของการขนสง packet, การศึกษาการทํางานของ Traffic บนระบบเครือขาย, การศึกษาการทํางาน
ของ Algorithm ที่ใชในการสงผาน packet เปนตน ซึ่งในปจจุบันไดมีโปรแกรมที่ชวยในการจําลองการ
ทํางานของระบบเครือขายมากมาย มีทั้งที่เปน Freeware และ commercial เชน NS2, OMNet++, OPNet,
CSim, Swans, NCTUns เปนตน เมื่อมีโปรแกรมที่ชวยในการจําลองการทํางานของระบบเครือขายจึงเปน
การดีตอการศึกษาและงานวิจัยทางดานระบบเครือขาย เนื่องจากการคนควาวิจัยในดานระบบเครือขายนั้น
จะต อ งใช ง บประมาณในการทดสอบการทํ า งานของระบบเครื อ ข า ย รวมทั้ ง โปรแกรมเหล า นี้ ยั ง เป น
ประโยชนตอการศึกษาคนควาเปนอยางยิ่งตอผูที่ไมมีทุนทรัพยในการที่ไดเห็นการทํางานจริงของระบบ
เครือขายผานการจําลองโดยใชโปรแกรม เมื่อไมตองคํานึงถึงคาใชจายที่จะตองเสียจากการทดสอบการ
ทํางานของระบบเครือขาย จึงสามารถที่จะพัฒนาความรูในดานนี้ไดอยางรวดเร็ว
         ในบทความนี้จะขอกลาวถึ งโปรแกรมการจําลองการทํางานของระบบเครื อข ายที่มีชื่อวา NS2
(Network Simulator version2)เปน discrete event simulator NS2 ไดสนับสนุนการทําแบบจําลองการ
ทํางานของTCP, routing, multicast protocols over wired และ wireless (local และ satellite) networks จาก
คุณสมบัติเหลานี้เราสามารถจําลองการทํางานของ network ได




                                                 3
ภาพที่ 1.1 แสดง Network Model

                                                                  App1            App2
           App1           App2

          Agent1          Agent2                                  Agent1      Agent

                   Node                                                  Node


                          Link                                      Link



                                 Node                      Node


                          Link                                    Link



            App1            App2                                   App1           App2

           Agent1          Agent2                                 Agent1      Agent2

                    Node                                                   Node




                                        ภาพที่ 1.2 แสดงการจําลอง Network

       จากภาพที่ 1.1 นั้นเปนการออกแบบ Network ปจจุบันซึ่งเมื่อเรานํา Network ที่ใชกันอยูในปจจุบัน
มาทําการจําลองเพื่อที่จะใชในการตรวจสอบการทํางานทางดานตางๆ เราจึงตองใชโปรแกรม NS2 มาชวย
ในการออกแบบการจําลอง Network ดังนั้นภาพที่ 1.2 จึงเปนการจําลอง Network ที่เทียบมาจากความเปน
จริง
       NS2 มีภาษาที่ใชอยู 2 ภาษาดังคือ ภาษา C++ และ OTcl (Appendix A) ในสวนของขั้นตอนในการ
ลงโปรแกรม NS2 พรอมทั้งรายละเอียดของการใชงาน OTcl และรายละเอียดของโปรแกรมที่ใชในการ

                                                       4
จําลองการทํางานของ Network พรอมทั้งยังมีการอธิบายโครงสรางและการทํางานของ NS2 ที่จะกลาวไวใน
Chapter 2 หลังจากนั้นจะกลาวถึงหลักการทํางานและโครงสรางสวนประกอบทั้งหมดของ NS2 ใน chapter
3 ซึ่งใน NS2 ยังมี Visualization tool ซึ่งเปน tool ที่ใชในการแสดงผลการทํางานของ Otcl script ออกมาใน
รูปของกราฟฟกซึ่งมีทั้งกราฟและการจําลองการทํางานของ Network ซึ่งกราฟนั้นจะมี Xgraph ซึ่งเปน Tool
ที่มีอยูแลวใน NS2 เปนตัวแสดงผล สวนในเรื่องของการจําลองการทํางานของ Network จะมี NAM
(Network Animator) เปนตัวแสดงผลซึ่งรายละเอียดจะมีการกลาวเพิ่มเติมใน Chapter 4 และใน Chapter 5
จะมีการแสดงตัวอยางการทํางาน




                                                 5
Chapter 2
                                       Literature Reviews
2.1 Literature Reviews
         โปรแกรมที่ใชในการจําลองการทํางานของ Network มีมากมายหลายโปรแกรมมากในปจจุบัน ซึ่ง
นอกจาก NS2 ที่ไดแนะนําไปบางแลวก็ยังมีโปรแกรมอื่นๆอีกมากมาย ซึ่งแตละโปรแกรมก็มีความสามารถ
ในแตละดานที่เพิ่มขึ้นมาแตกตางกัน ซึ่งการที่จะนําแตละโปรแกรมมาใชงานนั้นก็ขึ้นอยูกับความถนัดของ
ผูใชและความเหมาะสมของงานซึ่งในที่นี้จะขอแนะนําโปรแกรมที่ใชในการจําลองงานทํางานของ Network
ดังตอไปนี้
      1. NS2
                  NS2 เปน open-source และสามารถที่จะ run ไดทั้งบน Linux , FreeBSD, SunOS, Solaris,
         Window ถูกพัฒนาขึ้นโดย ISI (Information Sciences Institute) NS2 เปนโปรแกรมที่ใชในการ
         จําลองการทํางานของ network ในแบบที่เปน discrete event simulator ซึ่งสนับสนุนการจําลองการ
         เลือกเสนทางในการขนสง packet,จําลองการทํางานของ multicast protocol และ IP protocol เชน
         UDP, TCP, RTP, SRM ที่อยูบนเครือขายประเภทที่เปน wire และ wireless (local และ satellite) ซึ่ง
         NS2 เปน tool ที่มีประโยชนมากทั้งยังสนับสนุน multiple protocol และยังมีความสามารถในการ
         แสดงรายละเอียดของ network traffic ออกมาในรูปแบบของกราฟฟก รวมทั้งยังสนับสนุน
         algorithm ในการ routing และ queuing เชน FIFO, round-robin เปนตน
      2. OPNET
                  OPNET เปนโปรแกรมที่เปนผลิตภัณฑทางพาณิชยซึ่งไดรวบรวม Model ตางๆและ
         ครอบคลุมถึงเทคโนโลยีทางดาน Network เพื่อรองรับใหผูใชสามารถที่จะออกแบบ และจัดการกับ
         ระบบเครื อขา ยขั้น พื้ นฐานพร อมทั้งยั งมี การจําลองอุปกรณทางดาน network และnetwork
         application เพื่อใหผูใชนั้นไดพัฒนาระบบเครือขายเสมือนกับสภาพแวดลอมในความเปนจริง
      3. CSim
                  CSim เปน Simulation Environment ที่มีพื้นฐานมาจากภาษา C ใน CSim จะมีการทําโมเดล
         ที่มีความเปนอิสระและมีความสัมพันธระหวาง entity มีลักษณะการเก็บขอมูลแบบเปนลําดับชั้นจึง
         เปนระบบที่ชวยทําใหจัดเก็บแฟมขอมูลเปนกลุม สามารถเพิ่มประสิทธิภาพในการทํางานในเวลา
         เดียวกันภายในระบบได พรอมทั้งยังสนับสนุนระบบการทํางานประเภท rates, data, bandwidth,
         latency, traffic เปนตน ใน CSim นั้นจะมีแบบจําลองของซอฟตแวรเปนเชิงกลศาสตร (concurrent
         process oriented) พรอมทั้งยังมี Methods ในการชวยพัฒนาการทํางานทั้งแบบที่เปน Graphical
         และ Textual ในเรื่องของ Graph file นั้นจะมี Graph file เปน XML ascii-text เพื่อชวยเพิ่มความ
         สะดวกในเรื่องการทํางานขาม platform โปรแกรม CSim นั้นเปน commercial software

                                                   6
4. SWANS (Scalable Wireless As hoc Network Simulator)
               SWANS เปน scalable wireless network simulator ซึ่งสรางอยูบน JiST platform (Java in
      Simulation Time) SWANS มีการจัดการระบบในรูปแบบของ independent software component ทํา
      ใหสามารถที่เปลี่ยนรูปแบบเปน complete wireless network หรือ sensor network configurations
      ความสามารถของ SWANS นั้นเหมือนกับ NS2 และ GloMoSim แตกตางกันที่ SWANS นั้นจะ
      รองรับการจําลองNetwork ขนาดใหญๆไดซึ่ง SWANS ยังสามารถแสดงผลลัพธของการจําลองใน
      ระดับสูง พรอมทั้งยังชวยประหยัดหนวยความจําและ สามารถ run Standard Java network
      application ไดบน simulate network
   5. OMNet++
               OMNet++ เปน discrete event simulation environment ซึ่ง OMNet++ นั้นสามารถที่จะ
      จําลองการสื่อสารของNetwork protocol,computer network,traffic modeling, multi-processor และ
      distributed system เปนตน OMNet++นั้นไมไดเปน Network simulator ซึ่งตัว OMNet++ เปนแค
      network simulation platform ซึ่ง Modelsตางๆของ OMNet++ นั้นจะเปน Component architecture
      ซึ่งคอมโพเนนทตางๆนั้นถูกพัฒนาดวยภาษา C++ ซึ่งการสรางคอมโพเนนทขนาดใหญและโมเดล
      นั้นจะใชภาษาระดับสูง (high-level language) ในการเขียนซึ่งภาษาระดับสูงที่ใชคือ ภาษา NED
      (Network Description)

   6. NCTUns3.0
              NCTUns เปน open-source โปรแกรม NCTUns นั้นพัฒนาขึ้นมาเพื่อใชเปน network
      simulation และ emulation สําหรับ wireless และ mobile network ซึ่ง NCTUns จะสรางการจําลอง
      สําหรับ wireless ad hoc, sensor, inter-vehicle communication network, GPRS cellular network
      และ wireless mesh network ซึ่งความสามารถอีกอยางของ NCTUns คือ สามารถที่จะแบงแยก
      network device และ network protocol ออกเปนหมวดหมูได

2.2 การติดตั้งโปรแกรม Network Simulator (NS) บน Linux
         Network Simulator คือ โปรแกรมที่ชวยในการฝกการออกแบบและติดตั้งระบบเครือขาย
คอมพิวเตอร ซึ่งโปรแกรมนี้สามารถใชงานไดทั้งบน Window และ Unix (FreeBSD, Linux, SunOS,
Solaris) สําหรับการลงโปรแกรมนี้มี 2 วิธีในการลงโปรแกรม คือ สามารถลงแบบที่ละ Component หรือ ลง
แบบ all at one ซึ่งในที่นี้จะเลือกลงโปรแกรมแบบ all at once ซึ่งคือ NS-allinone-2.31 เปนโปรแกรม NS
allinone Version 2.31 ซึ่งการที่จะลงโปรแกรมแบบ allinone นั้นตองใชเนื้อที่อยางนอย 320 MB



                                                7
1.วิธีการดาวนโหลด (Download) Network Simulator
  1.1 ดาวนโหลด Network Simulator (NS-allinone Version 2.31)จาก
       http://sourceforge.net/project/showfiles.php?group_id=149743&package_id=169689&release_id=4
       92770

2.การเตรียมอุปกรณเบื้องตน
  2.1 เครื่องคอมพิวเตอรที่มี รายละเอียดดังตอไปนี้
      OS : Linux2.6.18.2-34-default i686
      System : openSUSE10.2(i586)
      Download OpenSUSE : http://es.opensuse.org/Versi%C3%B3n_publicada
  2.2 หนวยความจําขนาด 320 MB

3.การติดตั้ง (install)
  3.1 นํา File ns-allinone-2.31.tar มาไวใน Directory homeและสราง Directory ที่ตองการเพื่อวางns-
      allinone- 2.31โดยในที่นี้จะไวที่ /home/user/NS
  3.2 ทําการแตก file ns-allinone-2.31.tar โดยใชคําสั่ง tar -xvzf ns-allinone- 2.31.tar.gz
  3.3 เมื่อแตก file ns-allinone-2.31.tar เรียบรอย ใหไปยัง Directory ที่เก็บ file ns-allinone-2.31 ที่แตก
      เรียบรอยพิมพ ./install ดังภาพที่ 2.1




                        ภาพที่ 2.1 แสดงคําสั่งที่ใชในการ install ns-allinone-2.31


                                                    8
3.4 Set Environment variables
     ซึ่ง Environment variables ที่ตอง set มีดังนี้ PATH, LD_LIBRARY_PATH, TCL_LIBRARY
     เพื่อที่จะได Run file(.tcl) ณ ตําแหนงตางๆ ที่มี file(.tcl)
  3.4.1 set path โดยการเพิ่มคําสั่งลงใน /etc/bash.bashrc โดย vi bash.bashrc ดังภาพที่ 2.2 เพื่อที่จะ
              พิมพคําสั่ง set path




                          ภาพที่ 2.2 แสดงวิธีในการเขาถึง file bash.bashrc

3.4.2     เมี่อเขามาใน bash.bashrc ใหพิมพคําสั่ง Set Path ดังภาพที่ 2.3
          PATH=$PATH:$HOME/bin
          PATH=$PATH:/home/user/NS/ns-allinone-2.31/bin
          PATH=$PATH:/home/user/NS/ns-allinone-2.31/tcl8.4.14/unix
          PATH=$PATH:/home/user/NS/ns-allinone-2.31/tk8.4.14/unix

          LD_LIBRARY_PATH=/home/user/NS/ns-allinone-2.31/otcl-1.13
          LD_LIBRARY_PATH=$LD_LIBRARY:/home/user/NS/ns-allinone-2.31/lib,

          TCL_LIBRARY==/home/user/NS/ns-allinone-2.31/tcl8.4.14/library
          export PATH LD_LIBRARY_PATH TCL_LIBRARY

          หมายเหตุ /home/user/NS/ns-allinone-2.31 เปนpath ที่เก็บ ns-allinone-2.31

                                                  9
ภาพที่ 2.3 แสดงคําสั่งที่ใชในการ Set path

4. วิธีแกปญหา
  4.1 เมื่อ install และ set Path แลวแตไมสามารถที่จะ run file (.tcl) ไดใหเขาไปยัง Directory ที่เก็บ Program
        ns-allinone-2.31 แลวเขาไปยัง Directory bin แลวพิมพ cp * /usr/local/bin

5. การ run ns
  5.1 เมื่อลงโปรแกรมเรียบรอยแลวเราจะทําการทดสอบการทํางานของโปรแกรมโดยจะนํา file (.tcl) มาทํา
       การประมวลผลโดยผูใชจะตอง cd เขาไปยัง Directory ที่เก็บ file(.tcl) แลวพิมพคําสั่ง ns ตามดวยชื่อ
       file(.tcl) ที่ตองการจะประมวลผล ดังภาพที่ 2.4
      ns ns_simple.tcl




                                                      10
ภาพที่ 2.4 แสดงการใชคําสัง ns เพื่อประมวลผล
                                                     ่

5.2 กรณีที่คําสั่ง ns ประมวลผล file (.tcl) ไดถูกตองจะปรากฏผลลัพธที่ไดจากการประมวลผลดังภาพที่ 2.5
และถือวาการ install ns-allinone-2.31 ถูกตอง




                         ภาพที่ 2.5 แสดงผลของการประมวลผล ns-simple.tcl

                                                11
6. NS Simulation Script
  เมื่อ run ตัวอยางนี้เรียบรอยแลวจะปรากฏผลลัพธดังภาพที่ 2.6




                          ภาพที่ 2.6 แสดงผลของการประมวลผล ns-simple.tcl

  สามารถดูตัวอยาง Script ไดที่ http://nile.wpi.edu/NS/simple_ns.html




                                                   12
Chapter 3
                                                    หลักการทํางานของ NS2

       3.1 การทํางานของ NS2

                      nam                        Nam trace file


                  Otcl Script                    NS trace file                Visualization tool               diagram

User


             C++ network elements                Perl script                 results



                                                                                                   Generates

                                                                                                   Uses




              User จะสามารถสราง Otcl Script, C++ Network elements และ Perl Script เพื่อที่จะนํามาใชในการ
       ออกแบบเพื่อจําลองการทํางานของ Network ซึ่ง User สราง C++ network ขึ้นมาเพื่อใหเปน object ที่อยูใน
       library ที่สามารถจะเรียกใชไดโดยเรียกใชผาน Command ของ Otcl Script ซึ่งจะมี Otcl linkage เปนตัวที่จะ
       เรียก Object ใน library ขึ้นมาใชงาน เมื่อ Otcl script ถูกประมวลผล Otcl script จะสราง nam trace file และ
       ns trace file ซึ่งตัว nam trace file ที่ไดนั้นจะถูกเรียกใชโดย nam เพื่อที่จะนําไปประมวลผลแลวแสดงผล
       ออกมาในรูปกราฟฟก ในสวนของ perl script นั้นจะเรียกใช ns trace file เพื่อนํา ns trace file มา filter ใหได
       ผลลัพธออกมาเปน Result ที่จะถูกเรียกใชโดย Visualization tool เชน Xgraph เพื่อที่จะนํามาใชสรางกราฟที่
       แสดงผลการทํางานของการจําลอง Network
            ภาษาทีใชใน NS2 มีอยู 2 ภาษา เนื่องจาก Simulator มีลักษณะการทํางาน 2 อยางที่แตกตางกัน ซึ่งงาน
                    ่
       สวนแรกจะเปนการจําลองรายละเอียดตางๆเกี่ยวกับ Protocol จึงจําเปนตองใช System programming
       language ซึ่งมีประสิทธิภาพในการจัดการขอมูลที่มีหนวยของขนาดขอมูลเปน byte , packet header และ
       สราง algorithm ที่สามารถประมวลผลขอมูลที่มีขนาดใหญได ซึ่งงานในสวนนี้จะเนน และใหความสําคัญ
       ในเรื่องของ run-time speed สวนในเรื่องของ turn-around time (คนหา bug ,re-run, recompile) นั้น จะมี
       ความสําคัญนอยกวา สวนงานในสวนที่ 2 สวนใหญจะเปนเรื่องของการทําวิจัยเกี่ยวกับ network ซึ่งจะตอง


                                                           13
ยุงเกียวกับพวก Parameters หรือ การทํา Configuration ซึ่ง iteration time (change the model and re-run) นั้น
       ่
เปนสิ่งสําคัญ เพราะฉะนันจึงจําเปนที่ NS2 จะใช 2 ภาษา คือ
                        ้

    1. C++
      - เอาไวใชในการสราง Protocol และ Application Agents
      - ใชกําหนดคุณสมบัติของกระบวนการทํางานของแตละ packet ตามที่ผูใชตองการ
      - ใชเปลี่ยนการทํางานของ object ใน C++ เพี่อเอาไวปรับใชกับ non-standard policies
        ขอดี - ประมวลผลเร็ว
        ขอเสีย – จะทํางานชาเมื่อมีการ run simulation, คนหา bug, fix bug, recompile, re-run
    2. Otcl (Object Tool Command Language)
      - เอาไวใชในการเขียน Simulation script , Configuration และ setup
      - ทดสอบผลของ network parameter โดยทําการเปลี่ยนแปลงคาใน object ของ C++
      - วิเคราะหผลของการเปลี่ยนแปลง parameter โดยใชการ trace และแสดงผลทางหนาจอ
        ขอดี – สามารถทํางานไดอยางรวดเร็วเมื่อมีการเปลี่ยนแปลง
        ขอเสีย – ประมวลผลชา

NS Feature

      - จําลองความแตกตางของ protocol (TCP/UDP)
      - Wired Routing protocols, Ad-Hoc Routing protocol
      - วิธีการที่ใชในการจัด packet เพื่อเรียงเขา Queue เชน Drop Tail, RED, WFQ, DRR, LQD เปนตน
      - คุณลักษณะของ Traffic มีดังนี้ Poisson, Exponential, Pareto เปนตน สําหรับ Traffic ของจุดเริ่มตน
        คือ CBR หรือ VBR
      - ปรับเปลี่ยน NS2 เพื่อสราง protocol ใหม หรือพัฒนา protocol เดิมใหเปน Version ใหม
      - การวัดทางสถิติ เชน Throughput, Delat, Jitter, Queue Monitoring, Drops at Queues
      - Graphic visualization ใช NAM (Network Animator) ในการจําลอง visualize wired และใช Ad-
        Hockey ในการจําลอง visualize wireless

3.2 NS Script Structure

    1. Create Simulator Object
       set ns [new Simulator] #เปนการสราง Object ของ Simulator
    2. Tracing

                                                    14
การเปด nam trace file
                  Nam trace file มีเอาไวสําหรับเก็บขอมูลที่จะนํามาใชในการประมวลผลของ NAM
       เพื่อที่จะแสดงผลการจําลอง Network ออกมาในรูปของกราฟฟก
       set <variable name> [open <ชื่อ file.nam> w]
       $ns namtrace-all file-descriptor : เปนการบอกให simulator บันทึก simulation trace ลงใน file-
       descriptor ตามรูปแบบของ NAM
       w คือ การบอกวาเปนการเขียนขอมูลลงใน file
       set nf [open out.nam w]
       $ns namtrace-all $nf
       ซึ่งผูใชสามารถที่จะ trace file ของ nam เฉพาะแตละชวงของ event ไดโดยใชคําสั่ง
       $ns namtrace-queue $<source_node> $<destination_node> $<fileName>
       การเปด trace file
       set <variable name>[open <ชื่อ file.tr> w]
       $ns trace-all $<variable name>
       w คือ การบอกวาเปนการเขียนขอมูลลงใน file
       set tf [open out.tr w]
       $ns trace-all $tf
       $ns flush-trace : เปน member function ของ trace-all ซึ่งเปนคําสั่งที่ใชในการบันทึก simulation
       trace ไวในรูปแบบที่กําหนด

รายละเอียดของ Trace file




                             ภาพที่ 2.7 แสดงรายละเอียดของ Trace file




                                                 15
1. Event คือ Event type ซึ่งมีสัญลักษณอยู 4 ชนิด คือ r, +, -, d
       r หมายถึง receive
       + หมายถึง enqueue
       - หมายถึง dequeue
       d หมายถึง Drop
   2. Time คือ เวลาขณะที่เกิด event
   3. From node คือ input node ของ link ที่เกิด event
   4. To node คือ output node ของ link ที่เกิด event
   5. Pkt type คือ Packet type หรือชื่อของตัวแปรที่ถูกกําหนดคาใหเปน Application
   6. Pkt size คือ Packet Size
   7. Flags
   8. Fid คือ Flow id (ตัวแปรที่เก็บคาสี)
   9. Src addr คือ source address
   10. Dst addr คือ destination address
   11. Seq num คือ Sequence number ของ packet ใน Network Layer
   12. Pkt id คือ unique id ของ packet

3. Create Nodes ( physical layer)
                                                                               Node
   set <variable name> [$ns node]
                                             n0                     n1
   set n0 [$ns node]
   set n1 [$ns node]                                   Link


4. Links and queuing ( Data-link layer )
   $ns <link> node1 node2 <bandwidth> <delay> <queue_type>
   <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR etc.
   <link> : Simplex-link คือ การเชื่อมตอแบบทางเดียว
             Duplex-link คือ การเชื่อมตอแบบ 2 ทาง
   $ns duplex-link $n0 $n1 1Mb 10ms RED
   $ns queue-limit node1 node2 number : เปนการกําหนดขนาดของ queue ที่เชื่อมระหวาง node1
   และ node2
   $ns duplex-link-op node1 node2… : เอาไวใชสาหรับการแสดงผลของ NAM
                                               ํ

                                            16
5. Creating UDP connection ( transport layer)
                                                                                                       udp
   การสรางตัวแปรที่เปน Agent
   set udp [new Agent/UDP]                                                                             n0


   set null [new Agent/NULL]
   การกําหนดให Agent อยูบน Node                                                                      n1


   $ns attach-agent node agent :                                                                       null


   $ns attach-agent $n0 $udp                                                                    Create UDP
   $ns attach-agent $n1 $null
   การสรางการติดตอสื่อสารระหวาง agent
   $ns connect agent1 agent2
   $ns connect $udp $null

           การสราง Agent ขึ้นมา 2 ตัวโดยใหตัวหนึ่งมีคุณสมบัติของ UDP นั่นก็คือ udp
           และอีกตัวใหเปน null แลวกําหนดให Agent แตละตัวอยูบน node คนละตัวกัน
           เมื่ อ กํ าหนดให มี ก ารติ ด ตอ สื่ อ สารกั น จะมี ตั วหนึ่ ง เป น source อี กตั ว เป น
           destination


6. Creating TCP connection ( transport layer )                                                 tcp
   การสรางตัวแปรที่เปน Agent
   set tcp [new Agent/TCP]                                                                      n0


   set tcpsink [new Agent/TCPSink]
   การกําหนดให Agent อยูบน Node
                                                                                                n1



   $ns attach-agent $n0 $tcp                                                                   sink

                                                                                          Create TCP
   $ns attach-agent $n1 $tcpsink
   การสรางการติดตอสื่อสารระหวาง Source และ destination
   $ns connect $tcp $tcpsink

   การสราง Agent ขึ้นมา 2 ตัวโดยใหตัวหนึ่งมีคุณสมบัติของ TCP นั่นก็คือ tcp
   และอีกตัวใหเปน tcpsink แลวกําหนดให Agent แตละตัวอยูบน node คนละ
   ตั ว กั น เมื่ อ ให มี ก ารติ ด ต อ สื่ อ สารกั น จะมี ตั ว หนึ่ ง เป น source อี ก ตั ว เป น
   destination




                                                             17
7. Creating Traffic ( application layer)                                  cbr
    (On Top of UDP) เชน CBR
   set cbr [new Application/Traffic/CBR]                                  udp


   $cbr set packetSize_ 500
   $cbr set interval_ 0.005
                                                                           n0


   $cbr attach-agent $udp
                                                                           n1

   สราง Traffic ที่ชื่อ cbr แลวกําหนดคุณสมบัติของ cbr ทั้งขนาดของ
   packet และชวงเวลาที่ packet ใช รวมทั้งกําหนดใหcbr อยูบน Agent      null
   UDP
                                                                       Create CBR

   การสราง Application
                                                                           ftp
   (On Top of TCP) เชน FTP, Telnet
   FTP                                                                    tcp


       set ftp [new Application/FTP]                                       n0

       $ftp attach-agent $tcp
   Telnet
       set telnet [new Application/Telnet]                                 n1


       $telnet attach-agent $tcp                                          sink

                                                                       Create FTP
  สราง Application ที่ชื่อ ftp แลวกําหนดคุณสมบัติของ FTP รวมทั้ง
  กําหนดให ftp อยูบน Agent tcp

8. การเขียน Procedure
   proc ชื่อprocedure {}
   { body }
   proc finish {} {
     global ns nf
     $ns flush-trace
     close $nf
     exec nam out.nam &
     exit 0
   }
                                             18
9. Call Procedure
        $ns at 5.0 quot;finishquot;
    10. Schedule event
        $ns at <time> <event> : เปนคําสั่งที่จะทําการประมวลผล event ตามเวลาที่กําหนด
        <event>: คําสั่งที่ไดกําหนดไวใน ns/tcl
        $ns at 1.0 “Start”
    11. Start NS (เริ่มการทํางานของ Script)
        $ns run
    12. Stop NS
        Exit 0
การ Run NS Program
การ Run แบบ interactive mode โดยการพิมพ ns แลวจึงพิมพ command ตางๆ ทีละบรรทัด
      bash-shell$ ns
      % set ns [new Simulator]
      % $ns at 1 “puts “Hello World!””
      % $ns at 1.5 “exit”
      % $ns run
      Output :
      Hello World!

การ Run แบบ Batch mode เปนการที่ run จาก file ที่มีการกําหนด Script ไวเรียบรอยแลว
     - ใชคําสั่ง ns <Otcl script file>

กระบวนการของการประมวลผลของ NS
    - อาน tcl file
    - Run simulation program
    - สราง trace file
    - แสดงผลลัพธโดยใช NAM (network animator)
    - ผลทางสถิติ




                                                 19
3.3 การจัดการขอมูลเกี่ยวกับ Trace file
           ใน NS simulator นั้นสามารถจัดเตรียมรายละเอียดของขอมูลในแตละเหตุการณบน Network ได
ซึ่งถาผูใชตองการที่จะวิเคราะหขอมูลตางๆ ผูใชจะตองทําการ trace ขอมูลตางมาไวใน file แลวทําการ
วิเคราะหจัดการขอมูลตางๆ เองซึ่งอาจจะวิเคราะหขอมูลตางๆเหลานี้โดยการใชภาษาที่ใชในการเขียน
โปรแกรมตาง ๆ ในการจัดการกับไฟล ซึ่งสวนใหญจะจัดการกับไฟลตางๆโดยใชภาษา script เนื่องจาก
ภาษาเหลานี้จะทํางานโดยการแปลคําสั่งและประมวลผลไดโดยที่ไมตองทําการคอมไพล
การจัดการไฟลขอมูลดวย awk
           Awk เปนภาษาที่สามารถจะนํามาใชในการจัดการกับไฟลขอมูลไดดวยคําสั่งงายๆ ที่มีใน awk ซึ่ง
Awk มีความสามารถดังตอไปนี้
      - จัดการกับ database เล็กๆ
      - สราง report
      - ตรวจสอบความถูกตองของขอมูล
      - สราง index และแสดงเอกสารของงานตางๆ
      - ใชทดสอบ algolithm กอนที่จะมีการนําไปประยุกตใชดวยภาษาอื่นๆ
เชน การแสดงคาในแตละ column ในไฟล, การคํานวณหาผลรวม, การหาผลหาร เปนตน
Example : ตัวอยาง awk script ที่คํานวณคาเฉลี่ยในคอลัมนที่ 4 จากไฟล
           BEGIN {FS = “t”} {nl++} {s=s+$4} END
           {print “average:” s/n1}
ตัวอยางขางตนมีชื่อไฟลวา Average.awk ซึ่งถาตองการนํา code ดังกลาวไปใชงานกับ file ที่ชื่อ out.ns ใหใช
คําสั่งในการประมวลผล file ดังนี้ awk –f Average.awk out.ns ซึ่งผลลัพธก็คือการหาคาเฉลี่ยในคอลัมนที่ 4
ของไฟล out.ns
วิธีการประมวลผล awk
กรณีที่ awk อยูใน OTcl script
           exec awk $<function> <fileName>
           เชน exec awk $awkCode all.q
กรณีที่ awk อยูใน file
           awk –f <program> <input-file>
           เชน awk –f advice
คําสั่ง –f มีเพื่อที่จะนําโปรแกรม awk มาจากไฟล source file




                                                     20
การจัดการไฟลขอมูลดวย perl
          Perl ยอมาจาก Practical Extraction and Report Language ซึ่ง perl สามารถที่จะทําการประมวลผล
ไฟลขอมูลประเภท ASCII ใน unix ซึ่งภาษานี้ถูกสรางมาเพื่อชวยใหงานของผูดูแลระบบนั้นงายขึ้น
ขอดีของการใช perl
     - งายตอการเขียนโปรแกรมเล็กที่ใชในการกรองขอมูลจากไฟลตางๆ
     - สามารถใชไดในหลายๆ OS โดยที่ไมตองทําการเปลี่ยนแปลง code
     - การดูแลและการตรวจสอบ Bug ใน perl script นั้นทําไดงาย
     - ภาษา perl เปนที่นิยมกันมาก
ซึ่งใน NS simulator สามารถที่จะนําขอดีของ perl script มาชวยในการวิเคราะหขอมูลที่มีอยูใน trace file
ตางๆ เพื่อที่จะไดนําผลที่ไดจากการวิเคราะหไปใชในการสรางกราฟ หรือไปใชในการตรวจสอบความ
ถูกตองของขอมูลที่ไดจากการทํางานของ ns simulator ดังตัวอยางตอไปนี้ที่จะแสดงการนํา perl script มา
ชวยในการจัดการประมวลผลขอมูลที่อยูใน trace file ซึ่งตัวอยางนี้จะคํานวณคา throughput ของการติดตอ
กับ TCP ซึ่งตัวโปรแกรมจะทําการหาคาเฉลี่ยของ throughput ที่อยูในชวงตามคาที่ไดรับจาก parameter ซึ่ง
เรียกวา “granularity” ซึ่ง input ที่รับมานั้นมี 3 คา คือ ชื่อของ trace file (ในตัวอยางนี้คือ out.tr), nodeที่
ตองการตรวจสอบคา throughput, คาgranularity


Example : Perl script
# type: perl throughput.pl <trace file> <required node> <granlarity> > file
$infile=$ARGV[0];
$tonode=$ARGV[1];                                     รั บ ค า จากพารามิ เ ตอร 3 ค า คื อ ชื่ อ trace
                                                      file, nodeที่ตองการ, granularity แลว
$granularity=$ARGV[2];
                                                             นํามาเก็บไวในตัวแปรตางๆ

#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum=0;
$clock=0;
        open (DATA,quot;<$infilequot;) #เปนคําสั่งเปด file out.tr แลวเอาคาใน file มาไวที่ DATA
        || die quot;Can't open $infile $!quot;;
        while (<DATA>){
                 @x= split(' '); # นําคาที่อยูใน DATA มาไวใน array x
#column 1 is time

                                                          21
if ($x[1]-$clock<=$granularity)
{
# ตรวจสอบวาคอลัมนที่ 0 นั้นมีคาเทากับ reception หรือไม
if ($x[0] eq 'r')
{
# ตรวจสอบวาคาในคอลัมนที่ 3 นั้นมีคาเทากับ node ที่ใสมาในพารามิเตอรหรือไม
if ($x[3] eq $tonode)
{
# ตรวจสอบวา packet เปนประเภท TCP หรือไม
if ($x[4] eq 'tcp')
{
         $sum=$sum+$x[5];
}
}
}
}
else
{
         $throughput=$sum/$granularity;                          เปนการคํานวณหาคา throughput
         print STDOUT quot;$x[1] $throughputnquot;;                     และทํ า การพิ ม พ ค า ที่ ไ ด จ ากการ
         $clock=$clock+$granularity;                             คํานวณออกมา
         $sum=0;
}
}
         $throughput=$sum/$granularity;
         print STDOUT quot;$x[1] $throughputnquot;;
         $clock=$clock+$granularity;
         $sum=0;
         close DATA;
exit(0);



                                                       22
ตัวอยางขอมูลใน out.tr Trace file




                                     ภาพที่ 3.1 แสดงขอมูลใน file out.tr
วิธีการประมวลผล perl script
perl <PerlFile> <trace file>

เมื่อตองการทดสอบการทํางานของไฟลตัวอยางขางตนใหใชคําสัง  ่
perl throughput.pl <trace file> <required node> <granlarity> > Resultfile

3.4 NS Process
      NS Architecture ในมุมมองของผูใชทั่วไป




                You are here

                                     ภาพที่ 3.2 Architecture View of NS

       ภาพที่ 2.9 นี้จะแสดงถึงสถาปตยกรรมทั่วๆไปของ NS ซึ่งเปนสถาปตยกรรมของผูใชทั่วไปที่ไมใช
NS developer โดยผูใชจะใชภาษา Otcl script ในการออกแบบและ คําสั่งในการประมวลผลรูปแบบของการ

                                                     23
จําลองใน Tcl ซึ่ง Tcl จะมีการเรียกใช simulator object ใน Otcl library ในสวนของ event scheduleและ
network component นั้นไดมีการพัฒนาไวเรียบรอยแลวซึ่ง Souce code ภายในนั้นถูกพัฒนาดวยภาษา C++
ดังนั้น Otcl สามารถที่จะเรียกใชไดตลอดการทํางาน ซึ่งจะมี Otcl linkage เปนตัวเรียกใชงานโดยเรียกผาน
tclcl ซึ่งทั้งหมดจะเปนการทํางานของ NS (NS คือ OO extended Tcl interpreter รวมกับ network simulator
library )




                          ภาพที่ 3.3 แสดงกระบวนการประมวลผลของ NS2

        จากภาพที่ 2.10 จะเห็นวา Otcl script ที่เราจําลองขึ้นมานั้นจะแสดงผลออกมาในรูปแบบของภาพ
กราฟฟกโดย nam ซึ่งการที่จะไดแบบจําลองที่แสดงผลตาม Script นั้นตัว NS2 จะตองประมวลผลจาก
Script แลวเชื่อมโยงคําสั่งจาก Otcl linkage แลวเรียก Object ที่ไดกําหนดไวแลวในภาษา C++ มาทําการ
ประมวลผลและแสดงผลตามที่กําหนดไวใน Script

ผูใชทั่วไป
                                                                            Analysis
                               NS-2
    Simulation-                                       Simulation
    Otcl Script          Otcl Interperter               Results
                                                                              NAM

                           C++ Library



                            ภาพที่ 3.4 แสดงกระบวนการทํางานของ NS-2




                                                 24
เมื่อผูใชตองการที่จะใช NS2 ผูใชจะกําหนด Otcl Script แลวทําการ run Script แลว NS2 จะสราง
Output file ใหแลวจะทําใหไดผลลัพธที่แสดงผลการจําลองตาม Script ออกมาในรูปแบบของกราฟฟก
(NAM)

NS2 Developer

    1. พัฒนา NS โดยใชภาษา C++ สราง object ขึ้นมา
    2. ทดสอบ object ที่พัฒนาขึ้นมาโดยใช Otcl script โดยจะออกแบบการจําลอง Network ขึ้นมาเพื่อ
       ทดสอบการทํางานของ Object ที่สรางขึ้น
    3. ทดสอบการทํางานโดยดูผลจากการ run script ผาน NAM และ XGraph

Extending NS2 มีอยู 2 แบบดังนี้คือ Extending NS in Otcl และ Extending NS in C++

                                     ns-allinone


      Tcl8.0         TK8.0           OTcl           tclcl         ns-2         nam-1


                                            tcl             ...          C++ code


                ex            test                lib          mcas              ...

     examples           validation
                                                                  OTcl code

                         ภาพที่ 3.5 แผนภาพโครงสรางของ File ใน NS-allinone

1. Extending NS in Otcl
  การเพิ่ม file.tcl
    1.1 นําไฟล (.tcl)ไปไวใน /ns2/tcl/lib
    1.2 เขาไปใน Makefile ที่อยูใน /ns2 แลวเพิ่มชื่อของไฟล (.tcl) นั้นไวบริเวณ NS_TCL_LIB
    1.3 เขาไปใน tcl/lib/ แลวเพิ่มชื่อไฟล (.tcl) เขาไป
    1.4 Recompile




                                                  25
2. Extending NS in C++
  การแกไข Code C++
    2.1 ใชคําสั่ง “make clean”
    2.2 ใชคําสั่ง “make” เพื่อที่จะทําการ compile .o ทั้งหมดอีกครั้ง

การสราง Network Object ดวย C++ เขาไปใน NS
        การสราง network object ใหมโดยใชภาษา C++ นั้นจะตองมีการสรางการเชื่อมโยงระหวางตัวแปร
ใน Otcl และตัวแปรในภาษา C++ เพื่อที่เวลาใช Otcl script แลว NS2 จะสามารถที่จะคนหาตัวแปรแลว
match กับคําสั่งที่มีแลวทําการแสดงผลได ซึ่งจะตองนํา file ที่เปน code ภาษา C++ เขาไปไวในโปรแกรม
NS2 ขั้นตอนของการสราง Network object มีดังนี้
   1. Export C++ class to OTcl
   2. Export C++ class variables to OTcl
   3. Export C++ Object Control Commands to OTcl
   4. Execute an OTcl command from C++
   5. Compile and Run

ตัวอยางของการสราง Network object (MyAgent)
Step1. Export C++ class to OTcl

        เราสราง network object class ชื่อวา MyAgent ซึ่ง derive มาจาก Class Agent ใน C++ โดย Class
MyAgent นี้จะทําการสรางตัวแทนของ Object นี้ไวใน Otcl ซึ่งการที่จะทําเชนนี้ไดจะตองนิยาม linkage
object เปนตัวกลางในการเชื่อมตอ โดยสราง Class MyAgentClass ขึ้นมา ซึ่ง Class MyAgentClass นั้น
derive มาจาก Class Tclclass ซึ่ง linkage object นี้จะทําการสราง OTcl object ที่มีชื่อวา
“Agent/MyAgentOtcl” (ในตัวอยางนี้) และสราง Linkageระหวาง OTcl object และ C++ object ที่เปนตัว
สราง object ซึ่งในที่นี้ คือ MyAgent




                                                     26
เมื่อ NS เริ่มทํางานจะมีการเรียก constructor ของตัวแปร static ที่ชื่อวา class_my_agent แลวจึงสราง
ตัวแทนของ “MyAgentClass” ขึ้น Class Agent/MyAgentOtcl และ method ตางๆจะถูกสรางไวใน OTcl
space เมื่อใดที่ User ใน Otcl พยายามที่จะการสรางตัวแทนของ Object จะทําไดโดยใชคําสั่ง “new
Agent/MyAgentOtcl” ซึ่งจะมีการเรียก Method “MyAgentClass::create” เพื่อสรางตัวแทนของ “MyAgent”
และ สงคา address กลับไป ในการสราง Object ใน C++ จาก OTcl นั้นจะไมไดคุณสมบัติ function หรือ
member variable ของ C++ จาก OTcl

Step 2. Export C++ class variables to OTcl
        สมมติวา C ++ Object “MyAgent” ที่เราสรางใหมขึ้นมา มี parameter 2 ตัว คือ “my_var1” และ
“my_var2” ซึ่งเราตองการที่จะ configure จาก OTcl โดยใช input simulation script เราจะใช binding
function ทําการ binding แตละตัวแปรของ ClassในC++ ที่เราตองการที่จะสงคาออกไป ซึ่ง binding function
นั้น จะทําการสรางตัวแปรขึ้นมา (ชื่อตาม my_var1) โดยจะนําไป match กับ OTcl object ของ class
(“Agent/MyAgentOtcl”) และทําการ binding แบบ bi-directional ระหวางตัวแปรของ OTcl class กับตัวแปร
ของ C++ ที่มี address ตรงกับ my_var1 และ my_var2 ตามลําดับ




                         ภาพที่ 3.6 แสดงการทํางานแบบ Bidirectional binding

                                                    27
Binding function จะอยูใน constructor function เพื่อใชในการ binding ตอนที่ instance ของ object นี้ถูก
                          
สรางขึ้น ซึ่ง N S สนับสนุนฟงกชั่น B i n d i n g 4 แบบที่เหมาะสําหรับ D a t a t y p e 5 แบบดังนี้

                                    - bind(): real or integer variables
                                    - bind_time(): time variable
                                    - bind_bw(): bandwidth variable
                                    - bind_bool(): boolean variable

         ดังนั้น User designing และ running simulation ที่ใช OTcl Script จะสามารถ configure parameter
    ที่สรางใน C++ ได

Step 3. Export C++ Object Control Commands to OTcl
          การ export C++ object variable ที่ User สรางนั้น เราจึงตองกําหนดให control ของ C++ Object ไป
อยูใน OTcl ซึ่งจะทําได โดยใหเรา define “command” member function ของ C++ object (“MyAgent”)
เพื่อที่จะทําหนาที่เปนตัว interpreter ของ OTcl ซึ่ง “command” member function จะทําการคนหา Member
function ที่ Match กับ OTcl object ใหกับ User




        เมื่อ instance ของ the shadow OTcl นั้น ทําการ Match “MyAgent” object ที่ถูกสรางใน Otcl space
(set myagent [new Agent/MyAgentOtcl]) แลว และ User ทําการเรียก Member function ของ object
($myagent call-my-priv-func), OTcl จะคนหา Member function ที่ User ตองการใน OTcl object ถาชื่อ
Member function ที่ใหมาหาไมเจอมันจะเรียก “MyAgent :: command” แลวสงคาชื่อของ OTcl member
function ที่เรียก และ argument ไปในรูปแบบของ argc/argv แตถาหาเจอ และมี          action มารองรับ มัน
จะทํา action และทําการ return ผลลัพธกลับไปและเรียก function ถาไมมี “command” function ของ object
บรรพบุรุษมันจะทํา recursive หาจนกวาจะพบ แตถาหาชื่อนั้นไมพบในบรรพบุรุษไหนเลย ก็จะมี error
message สงไปให OTcl object และ OTcl object จะแสดง error message ใหกับ User




                                                   28
Step 4. Execute an OTcl command from C++
         เมื่อเราตองการที่จะสราง network object ใหม เราจึงจําเปนที่จะตองมีการ Execute คําสั่งของ OTcl
    จาก C++ object จากภาพดานลางเปนตัวอยางของการ implement “MyPrivFunc” ซึ่งเปน member
    function ของ “MyAgent” ซึ่งเปนการแสดงผลลัพธของคาของ my_var1 และ my_var2 ออกทางหนาจอ
    ซึ่งการที่จะ Execute คําสั่งของ OTcl จาก C++ นั้นเราตองมีการอางถึง “Tcl::instance()” ซึ่งถูกประกาศ
    เปน static member variable ซึ่งในตัวอยางนี้ไดแสดงการสงคําสั่ง OTcl ใหกับ interpreter ซึ่งมี 2 ทาง
    คือ MyPrivFunc และ Tcl::instance()




Step 5. Compile and Run and Test “MyAgent”
    1. บันทึก ชื่อfile.cc ที่ตองการจะสราง Network Object เชน ex-linkage.cc แลวบันทึกไวใน NS-2
        directory
    2. เปด “Makefile” แลวทําการเพิ่ม ชื่อfile.o (เชน ex-linkage.o) ไวในบรรทัดสุดทายของObject file
        list
        OBJ_CC = 
                 …
                 ./ex-linkage.o
                 $(OBJ_STL)
    3. Re-compile NS โดยใชคําสั่ง “make”
    4. สราง file.tcl ขึ้นมาเพื่อทดสอบการทํางานของ Network Object ที่สรางขึ้น
    5. run Otcl script โดยใชคําสั่ง ns ชื่อfile.tcl

Download ex-linkage.cc : http://nile.wpi.edu/NS/Example/ex-linkage.cc
Download ex-linkage.tcl : http://nile.wpi.edu/NS/linkage.html




                                                   29
ภาพที่ 3.7 แสดง Otcl script ที่จะใชทดสอบการทํางานของ Network Object




                    ภาพที่ 3.8 แสดงผลลัพธของการทดสอบการทํางานของ Network Object



                                         Static class MyAgentClass:public TclClass{
                                         Public:
                                         MyAgentClass (): TclClass (“Agent/MyAgentOtcl”){}
                                         TclObject* create(int,const char*const*) {
                                         Return(new MyAgent()); }
                                         } class my agent;


Agent/MyAgentOtcl
                                                  TclObject
                             Agent
                                                    Create C++
                                                      object
     init                      init
                                                   Create Otcl
                                                  shadow object
                                                                  OTcl

 TclObject                                                        C++
 (C++)                        parent             MyAgent
  constructor              constructor            constructor


                               ภาพที่ 3.9 แสดงการเชื่อมตอของ OTcl กับ C++




                                                    30
เมื่อผูใชมีการเรียกใช network object ที่สรางขึ้นโดยใชคําสั่ง set myagent [new
Agent/MyAgentOtcl] แลว TclObject จะมีการ create C++ object ที่ชื่อ MyAgent แลวจะมีการเรียก
constructor ของ parent และ TclObject ใน code C++ จะถูกประมวลผลการทํางาน แลวสงผลลัพธกลับไป
โดยใน TclObject จะมีการสราง OTcl shadow object ขึ้นมาเพื่อที่จะมาทําการสงคาไปใหกับ Agent เพื่อ
เชื่อมตอและแสดงผลลัพธที่ไดจากการทํางาน




                                               31
Chapter 4
                                      Visualization Tools

4.1 nam-1 (Network AniMator Version 1)
        Nam เปน Animation tool ที่มีพื้นฐานมาจาก Tcl/TK มีไวสําหรับแสดงการจําลองการทํางาน ของ
network และ packet ซึ่งแสดงผลออกมาในรูปของภาพกราฟฟกโดยไปดึงขอมูลที่ใชในการจําลอง Network
จาก nam teace file ที่ไดสรางไวจากใน Tcl Script ซึ่ง Nam ไดสนับสนุน topology latout, packet level
animation และ เครื่องมือในการตรวจสอบขอมูล
Run Nam
nam <nam-file>
nam out.nam
Run Nam in Ns2 script
exec nam <nam-file>
exec nam out.nam
Nam Interface
    1. Color mapping
        $ns color fid <color> : เปนการกําหนดสีใหกับ packet ตามเสนทางการเดินทางโดยใช flow id
        (fid) เปนตัวบงบอก
        COLOR : red, blue เปนตน
        $ns color 40 red
        $ns color 41 blue
    2. Color flow id association
        $<variable of agent> set fid_ <variable of color>
        $tcp0 set fid_ 40 # red packets แสดงวา 40 = สีแดง
    3. Node Color เปนการกําหนดสีใหกับ node
        $<variable of node> color <COLOR>
        COLOR : red, blue เปนตน
        $node color red #nodeนี้จะมีสีเปนสีแดง




                                                32
4. Node Shape เปนการกําหนดรูปรางใหกับ node
   $<variable of node> shape <SHAPE>
   SHAPE : circle, box, hexagon เปนตน
   $node shape box
5. Node Marks เปนการเพิ่มและลบ node ที่มีการ mark ไวตามเวลาที่กําหนด
   $ns at 1.0 “$n0 add-mark m0 blue box”
   $ns at 2.0 “$n0 delete-mark m0”
   เมื่อกําหนดเชนนี้เวลาแสดงผลจะปรากฎ Mask ตามที่ระบุภายในชวงเวลา 1-2 วินาที
6. Node Lable เปนการกําหนดขอความกํากับที่ node เริ่มตั้งแตชวงเวลาที่กําหนด
   $ns at 1.1 “$n0 label ”web cache 0””
7. Link Color เปนการกําหนดสีของlink จาก Node หนึ่งไปยังอีก Node
   $ns duplex-link-op $n0 $n1 color quot;greenquot;
8. Link Label เปนการกํากับขอความบน link ที่อยูระหวาง node 2 node ที่กาหนด
                                                                           ํ
   $ns duplex-link-op $n0 $n1 label “backbonequot;




                        ภาพที่ 4.1 แสดงหนาจอที่มีการเรียกใช Nam


                                           33
4.2 Xgraph
       Xgraph คือ Plotting Program ซึ่ง Plotting Program นี้มีความสามารถที่จะสรางการแสดงผลออกมา
ในรูปของภาพกราฟฟกจากการแสดงผลลัพธจาก ns ได และภายในหัวขอนี้จะแสดงวิธีการสราง output file
จาก Tcl scripts ซึ่งเปนตัวที่จะมากําหนดขอมูลสําคัญที่ใชในการ plot graph และจะแสดงออกมาดวย
โปรแกรม Xgraph
       ชื่อกราฟ : -t “title”
       ขนาดของกราฟ : -geometry xsize * ysize
       ชื่อกํากับแตละแกน : -x “xtitle” (แกน x) และ –y “ytitle” (แกน y)
       สีของตัวอักษรและgrid : -v
       Run Xgraph
       xgraph <ชื่อ file> -geometry xsize*ysize –t “title” –x “xtitle” –y “ytitle”

       Run Xgraph (in NS2 Script)
       Exec xgraph <ชื่อ file> -geometry xsize*ysize –t “title” –x “xtitle” –y “ytitle”
       Exec xgraph out.tr –geometry 800* 400




                            ภาพที่ 4.2 แสดงหนาจอที่มีการเรียกใช Xgraph




                                                  34
Creating Output Files for Xgraph
        อีกสวนหนึ่งของโปรแกรมภายใน ns-allinone package คือ 'Xgraph' , Plotting Program นี้มี
ความสามารถที่จะสรางการแสดงผลออกมาในรูปของภาพกราฟฟกจากการแสดงผลลัพธจาก ns ได และ
ภายในหัวขอนี้จะแสดงวิธีการสราง output file จาก Tcl scripts ซึ่งเปนตัวที่จะมากําหนดขอมูลที่สําคัญใน
การ plot graph และจะแสดงออกมาดวยโปรแกรม Xgraph

1.Topology and Traffic Sources
ในขั้นตอนแรกเราจะสราง Topology ขึ้นมาตามแบบในรูปตอไปนี้




         ในสวนของ code ขางตนนี้จะมีความคลายคลึงกับตัวอยางโปรแกรมในขางตน โดยความหมายจาก
สวนนี้คือการสราง node ใหมเปนจํานวน 5 node
 set   n0   [$ns   node]
 set   n1   [$ns   node]
 set   n2   [$ns   node]
 set   n3   [$ns   node]
 set   n4   [$ns   node]

 $ns   duplex-link    $n0   $n3   1Mb   100ms   DropTail
 $ns   duplex-link    $n1   $n3   1Mb   100ms   DropTail
 $ns   duplex-link    $n2   $n3   1Mb   100ms   DropTail
 $ns   duplex-link    $n3   $n4   1Mb   100ms   DropTail




                                                           35
จากนั้นทําการสราง traffic ขึ้นมาเพื่อเพิ่มเติมรายละเอียดใหกับ node ดังกลาว โดยในสวนนี้จะเขียน
อยูในรูปของฟงกชันซึ่งมีชื่อวา attach-expoo-traffic และมีการรับคาจาก parameter 6 คาซึ่งคือ
node,sink,size,burst,idle,rate เพื่อความสะดวกในการกําหนดรายละเอียดของ node นั้น ๆ

 proc attach-expoo-traffic { node sink size burst idle rate } {
           #Get an instance of the simulator
           set ns [Simulator instance]

            #Create a UDP agent and attach it to the node
            set source [new Agent/UDP]
            $ns attach-agent $node $source

            #Create an Expoo traffic agent and set its
 configuration parameters
            set traffic [new Application/Traffic/Exponential]
            $traffic set packetSize_ $size
            $traffic set burst_time_ $burst
            $traffic set idle_time_ $idle
            $traffic set rate_ $rate

       # Attach traffic source to the traffic generator
       $traffic attach-agent $source
            #Connect the source and the sink
            $ns connect $source $sink
            return $traffic
 }



จากตัวอยางฟงกชันดังกลาวนี้ จะมีอยูทั้งหมด 6 arguments
1. node คือ node ที่มีการสรางไวขางตน
2. sink คือ ตัวแปรของ traffic sink ที่สรางขึ้น
3. size คือ ขนาดของ packet ของ traffic source
4. burst คือ burst time
5. idle คือ idle time
6. rate คือ burst peak rate

        จากรายละเอียดของฟงกชันนี้จะสราง traffic ขึ้นมาและทําการ attaches เขาไปภายใน node และมี
การสราง traffic object ซึ่งจากการสราง Traffic/Expoo object จะสามารถระบุการตั้งคาตาง ๆ ใหกับ traffic
object ไดโดยการรับคามาจาก parameters ซึ่งคาที่จะสงมากับ parameter นั้นมาจากการสรางขึ้นมากับ traffic
source และมีการ attach traffic เขาไปใน source รวมทั้งยังมีการ connect ระหวาง source และ sink ดวย

       สวนตอไปนี้คือการสราง traffic เพื่อสรางความแตกตางกันของคา peak rates ภายใน node n0 , n1
และ n2 และจากนั้นจะเชื่อมโยง 3 traffic sink ไปยัง node n4
                                                    36
set sink0 [new Agent/LossMonitor]                                  * ในสวนนี้ใช Agent/LossMonitor
   set sink1 [new Agent/LossMonitor]
   set sink2 [new Agent/LossMonitor]                                  objects ในการสราง traffic sinks เพื่อ
   $ns attach-agent $n4 $sink0
   $ns attach-agent $n4 $sink1                                        นํามาชวยในการเก็บจํานวน bytes ที่ได
   $ns attach-agent $n4 $sink2
                                                                      มีการรับเขามา ซึ่งจะมีสวนชวยในการ
   set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
   set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]       คํานวณหา bandwidth
   set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]



2. Recording Data in Output Files
ในสวนแรกนีจะทําการเปด output files ขึ้นมา 3 files จาก Tcl script ตามตัวอยางตอไปนี้
            ้
 set f0 [open out0.tr w]
 set f1 [open out1.tr w]
 set f2 [open out2.tr w]




สรางฟงกชันเพื่อจบการทํางานของโปรแกรมดังนี้

 proc finish {} {
       global f0 f1 f2
       #Close the output files
       close $f0
       close $f1
       close $f2
       #Call xgraph to display the results
       exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
       exit 0
 }




                                                   37
จากฟงกชันนี้ไมเพียงแตปดการทํางานของ output files แตจะเปนการเรียกการทํางานของ Xgraph
ขึ้นมาเพื่อแสดงผลลัพธดวยตามขนาดของ size window ที่ 800*400 ที่หนาจอ screen
 proc record {} {
       global sink0 sink1 sink2 f0 f1 f2
       #Get an instance of the simulator
       set ns [Simulator instance]
       #Set the time after which the procedure should be called again
       set time 0.5
       #How many bytes have been received by the traffic sinks?
       set bw0 [$sink0 set bytes_]
       set bw1 [$sink1 set bytes_]
       set bw2 [$sink2 set bytes_]
       #Get the current time
       set now [$ns now]
       #Calculate the bandwidth (in MBit/s) and write it to the files
       puts $f0 quot;$now [expr $bw0/$time*8/1000000]quot;
       puts $f1 quot;$now [expr $bw1/$time*8/1000000]quot;
       puts $f2 quot;$now [expr $bw2/$time*8/1000000]quot;
       #Reset the bytes_ values on the traffic sinks
       $sink0 set bytes_ 0
       $sink1 set bytes_ 0
       $sink2 set bytes_ 0
       #Re-schedule the procedure
       $ns at [expr $now+$time] quot;recordquot;
 }



          จากฟงกชันกอนหนานี้จะทําการอานจํานวนของ Bytes จากสาม traffic sinks ที่ถูกเก็บไวจาก object
ของ Agent/LossMonitor และจากนั้นจะทําการคํานวณ Bandwidth ในหนวยของ MBits/sec และจะเขียน
ขอมูลที่เปนเวลาปจจุบันและคา Bandwidth ที่คํานวณไดลงในแตละ output files ไปดวยกันกอนที่จะทําการ
reset คาของ bytes_value ที่ traffic sinks

3. Running the Simulation
       มาถึงในสวนนี้ จะได schedule ขึ้นมาจากกลุมคําสั่งตอไปนี้ และทําการรันโปรแกรมขึ้นมาตาม
ตารางเวลาดังกลาวนี้
 $ns at   0.0 quot;recordquot;
 $ns at   10.0 quot;$source0   startquot;
Result
 $ns at   10.0 quot;$source1   startquot;
 $ns at   10.0 quot;$source2   startquot;
 $ns at   50.0 quot;$source0   stopquot;
 $ns at   50.0 quot;$source1   stopquot;
 $ns at   50.0 quot;$source2   stopquot;
 $ns at   60.0 quot;finishquot;

 $ns run




                                                  38
ภาพที่ 4.3 แสดงผลการทํางานของ Xgraph
Source Code

#Create a simulator object
set ns [new Simulator]

#Open the output files
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]

#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

#Connect the nodes
$ns duplex-link $n0   $n3   1Mb   100ms   DropTail
$ns duplex-link $n1   $n3   1Mb   100ms   DropTail
$ns duplex-link $n2   $n3   1Mb   100ms   DropTail
$ns duplex-link $n3   $n4   1Mb   100ms   DropTail

#Define a 'finish' procedure
proc finish {} {
      global f0 f1 f2
      #Close the output files
      close $f0
      close $f1
      close $f2
      #Call xgraph to display the results
      exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
        exit 0
}

                                            39
#Define a procedure that attaches a UDP agent to a previously created node
#'node' and attaches an Expoo traffic generator to the agent with the
#characteristic values 'size' for packet size 'burst' for burst time,
#'idle' for idle time and 'rate' for burst peak rate. The procedure
connects
#the source with the previously defined traffic sink 'sink' and returns the
#source object.
proc attach-expoo-traffic { node sink size burst idle rate } {
      #Get an instance of the simulator
      set ns [Simulator instance]

      #Create a UDP agent and attach it to the node
      set source [new Agent/UDP]
      $ns attach-agent $node $source

      #Create an Expoo traffic agent and set its configuration parameters
      set traffic [new Application/Traffic/Exponential]
      $traffic set packetSize_ $size
      $traffic set burst_time_ $burst
      $traffic set idle_time_ $idle
      $traffic set rate_ $rate

        # Attach traffic source to the traffic generator
        $traffic attach-agent $source
      #Connect the source and the sink
      $ns connect $source $sink
      return $traffic
}

#Define a procedure which periodically records the bandwidth received by
the
#three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.
proc record {} {
        global sink0 sink1 sink2 f0 f1 f2
      #Get an instance of the simulator
      set ns [Simulator instance]
      #Set the time after which the procedure should be called again
        set time 0.5
      #How many bytes have been received by the traffic sinks?
        set bw0 [$sink0 set bytes_]
        set bw1 [$sink1 set bytes_]
        set bw2 [$sink2 set bytes_]
      #Get the current time
        set now [$ns now]
      #Calculate the bandwidth (in MBit/s) and write it to the files
        puts $f0 quot;$now [expr $bw0/$time*8/1000000]quot;
        puts $f1 quot;$now [expr $bw1/$time*8/1000000]quot;
        puts $f2 quot;$now [expr $bw2/$time*8/1000000]quot;
      #Reset the bytes_ values on the traffic sinks
        $sink0 set bytes_ 0
        $sink1 set bytes_ 0
        $sink2 set bytes_ 0
      #Re-schedule the procedure
        $ns at [expr $now+$time] quot;recordquot;
}
#Create three traffic sinks and attach them to the node n4
set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1

                                    40
$ns attach-agent $n4 $sink2

#Create three traffic sources
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]

#Start logging the received bandwidth
$ns at 0.0 quot;recordquot;
#Start the traffic sources
$ns at 10.0 quot;$source0 startquot;
$ns at 10.0 quot;$source1 startquot;
$ns at 10.0 quot;$source2 startquot;
#Stop the traffic sources
$ns at 50.0 quot;$source0 stopquot;
$ns at 50.0 quot;$source1 stopquot;
$ns at 50.0 quot;$source2 stopquot;
#Call the finish procedure after 60 seconds simulation time
$ns at 60.0 quot;finishquot;

#Run the simulation
$ns run




                                    41
Chapter 5
                                         ตัวอยางการใชงาน NS2
NS Script
          ตัวอยางนี้เปนการจําลองการทํางานของระบบเครือขายที่มี Node ภายในเครือขาย 5 node ซึ่งมี
source อยู 2 node คือ n0, n1 และ destination อยู 2 node คือ n3, n4 ซึ่ง node n0 มีการสรางการเชื่อมตอโดย
ใช TCP พรอมทั้งกําหนดใหใช Application เปน FTP โดยกําหนดให node n3 เปน destination ของการ
เชื่อมตอ สวน node n1 มีการสรางการเชื่อมตอโดยใช UDP พรอมทั้งกําหนดใหใช Application เปน CBR
โดยกําหนดให node n4 เปน destination ของการเชื่อมตอ พรอมทั้งยังเก็บ tracefile ไวในไฟลช่อ traceout.tr
                                                                                               ื
เพื่อที่จะไฟลดงกลาวมาใชในการสรางกราฟเพื่อวิเคราะหการทํางานของ TCP และ UDP
                ั

set ns [new Simulator]
set tf [open traceout.tr w]
$ns trace-all $tf
set nf [open out2.nam w]
$ns namtrace-all $nf
$ns color 1 RED
$ns color 2 BLUE

set n0 [$ns node]
set n1 [$ns node]
set r1 [$ns node]
set r2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns duplex-link $n0 $r1 2Mb 2ms DropTail
$ns duplex-link $n1 $r1 2Mb 2ms DropTail
$ns duplex-link $r1 $r2 1Mb 2ms RED
$ns duplex-link $n3 $r2 2Mb 2ms DropTail
$ns duplex-link $n4 $r2 2Mb 2ms DropTail



                                                    42
$ns duplex-link-op $n0 $r1 orient right-down
$ns duplex-link-op $n1 $r1 orient right-up
$ns duplex-link-op $r1 $r2 orient right
$ns duplex-link-op $r2 $n3 orient right-up
$ns duplex-link-op $r2 $n4 orient right-down

$ns queue-limit $r1 $r2 10
$ns queue-limit $r2 $r1 10

set tcp_src [new Agent/TCP]
$ns attach-agent $n0 $tcp_src
set tcp_des [new Agent/TCPSink]
$ns attach-agent $n3 $tcp_des
$ns connect $tcp_src $tcp_des
$tcp_src set fid_ 1
$tcp_src set packetSize_ 552

set ftp [new Application/FTP]
$ftp attach-agent $tcp_src
$ftp set type_ FTP

#Setup a UDP connection
set udp_src [new Agent/UDP]
$ns attach-agent $n1 $udp_src
set udp_des [new Agent/Null]
$ns attach-agent $n4 $udp_des
$ns connect $udp_src $udp_des
$udp_src set fid_ 2

#Setup CBR
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp_src

                                               43
$cbr set packetSize_ 1000
#$cbr set rate_ 0.01Mb
$cbr set random_ false

$ns at 0.1 quot;$ftp startquot;
$ns at 1.0 quot;$cbr startquot;
$ns at 120.0 quot;$ftp stopquot;
$ns at 120.5 quot;$cbr stopquot;
$ns at 121.0 quot;finishquot;

#Tracing a queue
set redq [[$ns link $r1 $r2] queue]
 set tchan_ [open all.q w]
 $redq trace curq_
 $redq trace ave_
 $redq attach $tchan_

proc finish {} {

     global ns nf tchan_ tf
     set awkCode {
      {
           if($1 ==quot;Qquot; && NF>2) {
                 print $2, $3 >> quot;temp.qquot;;
                 set end $2
           }
           else if($1 ==quot;aquot; && NF>2)
                 print $2, $3 >> quot;temp.aquot;;
      }
     }
     set f [open temp.queue w]
     if { [info exists tchan_] } {

                                             44
close $tchan_
    }
    exec rm -f temp.q temp.a
    exec touch temp.q temp.a
    exec awk $awkCode all.q
    puts $f quot;queue
    exec cat temp.q >@ $f
    puts $f nquot;avg_queue
    exec cat temp.a >@ $f
    close $f

   $ns flush-trace
         close $tf
    close $nf

    exec xgraph -x time -y queue temp.queue &
    exec nam out2.nam &
    exit 0
}
$ns run




                                                45
ภาพที่ 5.1 แสดง Topology ที่จําลองการทํางานของระบบเครือขาย

         Xgraph ที่ไดเปนกราฟที่แสดงความสัมพันธระหวางเวลาและqueue length ซึ่งจากกราฟจะแสดง
เสนกราฟที่เปรียบเทียบระหวาง Current queue length และ Average queue length
    เมื่อตองการที่จะสรางกราฟที่แสดงความสัมพันธระหวาง throughput และ เวลาจะตองมีการคํานวณคา
Average Throughput ซึ่งคาดังกลาวไดมาจากการคํานวณ packet size โดยคาของ packet size ถูกนํามาจาก
tracefile ซึ่งจากการทดลองดังกลาวใช tracefile ที่มีชื่อวา traceout.tr ซึ่งการที่จะนําคา packet size ใน
tracefile มาคํานวณในทีนี้ใชการเขียน script เพื่อที่จะดึงคาใน tracefile มาคํานวณซึ่งภาษาทีใชเขียน script
                         ่                                                                   ่
คือภาษา perl ในที่นี้จะวิเคราะหการทํางานของ throughput ซึ่งจะวิเคราะหทั้ง Average throughput ของทั้ง
Received และ Dropped โดยมองทั้งรูปแบบการทํางานของ TCP และ UDP ซึ่งการวิเคราะหที่ไดแบง
ออกเปน 4 รูปแบบดังนี้
    1. Averaged Received Throughput of TCP
    2. Averaged Received Throughput of CBR
    3. Averaged Dropped Throughput of TCP
    4. Averaged Dropped Throughput of CBR




                                                   46
ซึ่งเมื่อมีการกําหนดเงื่อนไขที่ใชใน perl script ตรงตามที่ตองการแลวก็ทําการสราง output file ที่ไดจาก perl
script นี้เพื่อที่จะนํา output file ที่ไดมาแสดงเปนกราฟโดยใช gnuplot ซึ่งรูปแบบคําสั่งที่ใชรัน perl script มี
ดังนี้
perl <perl script> <trace file> <required node> <granularity> > Output file
เมื่อตองการที่จะรัน perl script โดยตองการใหโหนดที่ 2 เปน required node และมี perl script ชื่อ
throughput.pl พรอมทั้งมี tracefile ชื่อ traceout.tr และไดกําหนดให granularity มีคาเปน 1 รวมทั้งตั้งชื่อ
output file วา TCP จะตองใชรูปแบบคําสั่งดังนี้
perl throughput.pl traceout.tr 2 1 > TCP
เมื่อได output file ที่ชื่อวา TCP ใหนํา output file ดังกลาวมาเขียนกราฟโดยใช gnuplot เชน

gnuplot
set size 1,1
set key 60,15000
plot ‘TCP’ w lines 1
1. Averaged Received Throughput of TCP
คําสั่งรัน perl script ที่วิเคราะห Averaged Received Throughput ของ TCP โดยพิจารณา throughput ที่ออก
จากโหนดที่ 4
perl throughput.pl traceout.tr 4 1 > TCP
Perl Script : throughput.pl
$infile=$ARGV[0]; #Argument0
$tonode=$ARGV[1]; #Argument1
$granularity=$ARGV[2]; #Argument2
#we compute how many bytes were transmitted during time interval specified
#by granularity parameter in seconds
$sum =0;
$clock=0;
          open (DATA,quot;<$infilequot;)||die quot;Can't open $infile $!quot;;
while (<DATA>){
          @x=split(' ');
          #column1 is time



                                                      47
if ($x[1]-$clock<=$granularity)
         {
         #checking if the event corresponds to a reception
                  if($x[0] eq 'r')
                  {
         #checking if the destination corresponds to 1st argument
                           if($x[3] eq $tonode)
                           {
         #checking if the packet type is TCP
                                    if($x[4] eq 'tcp')
                                    {
                                             $sum=$sum+$x[5];
                                    }
                           }
                  }
         }
         else
         {
                  $throughput=$sum/$granularity;
                  print STDOUT quot;$x[1] $throughputnquot;;
                  $clock=$clock+$granularity;
                  $sum=0;
         }
}
         $throughput=$sum/$granularity;
         print STDOUT quot;$x[1] $throughputnquot;;
         $clock=$clock+$granularity;
         $sum=0;
         close DATA;
         exit(0);



                                                   48
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)
Ns2 Tutorial by NECTEC (Thai)

Más contenido relacionado

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Ns2 Tutorial by NECTEC (Thai)

  • 1. Network Simulator (NS2) โดย นางสาวจิตติมา นิตยวรรณ Nectec/NTL นายโสฬส ชคัตตรยาพงษ มหาวิทยาลัยบูรพา นางสาวอุไรรัตน พึ่งสุนทรบัตร มหาวิทยาลัยธรรมศาสตร ปฏิบัติงาน ณ ศูนยเทคโนโลยีอิเล็กทรอนิกสและคอมพิวเตอรแหงชาติ (National Electronics and Computer Technology Center : NECTEC) 112 อุทยานวิทยาศาสตรประเทศไทย ถนนพหลโยธิน ตําบลคลองหนึ่ง อําเภอคลองหลวง จังหวัดปทุมธานี 1
  • 2. สารบัญ สารบัญ 2 บทที่ 1 บทนํา 3 บทที่ 2 ทฤษฎีที่เกี่ยวของ 2.1 Literature Reviews 6 2.2 การติดตั้งโปรแกรม NS2 7 บทที่ 3 หลักการทํางานของ NS2 3.1 การทํางานของ NS2 13 3.2 NS Script Structure 14 3.3 การจัดการไฟลขอมูลเกี่ยวกับ Trace file 20 3.4 NS process 23 บทที่ 4 Visualization Tools 4.1 Nam (Network AniMator Version1) 32 4.2 Xgraph 34 บทที่ 5 ตัวอยางการใชงาน NS2 42 ภาคผนวก Tcl และ OTcl Programming 56 อธิบายตัวอยาง ns-simlple.tcl 68 บรรณานุกรม 72 2
  • 3. Chapter 1 Introduction ตั้งแตอดีตจนถึงปจจุบันนี้ไดมีการพัฒนาระบบเครือขายมาตลอด ซึ่งการพัฒนาระบบเครือนั้น จะตองมีการวางโครงสรางและระบบผานอุปกรณตางๆที่ใชในการเชื่อมตอเครือขายมากมาย โดยการที่จะ ติดตั้งระบบเครือขายสําหรับการใชงานนั้นจะตองมีการทดสอบการทํางานของระบบเครือขายจึงเปนการยาก ที่จะทดสอบการทํางานของระบบเครือขายโดยการทดสอบผานอุปกรณตางๆ ที่ไดทําการวางระบบไวแลว เนื่องจากการวางระบบเครือขายนั้นเปนสิ่งที่ใชงบประมาณและเวลามากจึงไมคุมที่จะทดสอบการทํางาน ของระบบเครือขายโดยการทดสอบผานอุปกรณที่ใชในความเปนจริง จึงเกิดความจําเปนที่จะตองมีการ จําลองการทํางานของระบบเครือขายเพื่อที่จะไดทําการศึกษาการทํางานของระบบเครือขายในดานตางๆ ทั้ง พฤติกรรมของการขนสง packet, การศึกษาการทํางานของ Traffic บนระบบเครือขาย, การศึกษาการทํางาน ของ Algorithm ที่ใชในการสงผาน packet เปนตน ซึ่งในปจจุบันไดมีโปรแกรมที่ชวยในการจําลองการ ทํางานของระบบเครือขายมากมาย มีทั้งที่เปน Freeware และ commercial เชน NS2, OMNet++, OPNet, CSim, Swans, NCTUns เปนตน เมื่อมีโปรแกรมที่ชวยในการจําลองการทํางานของระบบเครือขายจึงเปน การดีตอการศึกษาและงานวิจัยทางดานระบบเครือขาย เนื่องจากการคนควาวิจัยในดานระบบเครือขายนั้น จะต อ งใช ง บประมาณในการทดสอบการทํ า งานของระบบเครื อ ข า ย รวมทั้ ง โปรแกรมเหล า นี้ ยั ง เป น ประโยชนตอการศึกษาคนควาเปนอยางยิ่งตอผูที่ไมมีทุนทรัพยในการที่ไดเห็นการทํางานจริงของระบบ เครือขายผานการจําลองโดยใชโปรแกรม เมื่อไมตองคํานึงถึงคาใชจายที่จะตองเสียจากการทดสอบการ ทํางานของระบบเครือขาย จึงสามารถที่จะพัฒนาความรูในดานนี้ไดอยางรวดเร็ว ในบทความนี้จะขอกลาวถึ งโปรแกรมการจําลองการทํางานของระบบเครื อข ายที่มีชื่อวา NS2 (Network Simulator version2)เปน discrete event simulator NS2 ไดสนับสนุนการทําแบบจําลองการ ทํางานของTCP, routing, multicast protocols over wired และ wireless (local และ satellite) networks จาก คุณสมบัติเหลานี้เราสามารถจําลองการทํางานของ network ได 3
  • 4. ภาพที่ 1.1 แสดง Network Model App1 App2 App1 App2 Agent1 Agent2 Agent1 Agent Node Node Link Link Node Node Link Link App1 App2 App1 App2 Agent1 Agent2 Agent1 Agent2 Node Node ภาพที่ 1.2 แสดงการจําลอง Network จากภาพที่ 1.1 นั้นเปนการออกแบบ Network ปจจุบันซึ่งเมื่อเรานํา Network ที่ใชกันอยูในปจจุบัน มาทําการจําลองเพื่อที่จะใชในการตรวจสอบการทํางานทางดานตางๆ เราจึงตองใชโปรแกรม NS2 มาชวย ในการออกแบบการจําลอง Network ดังนั้นภาพที่ 1.2 จึงเปนการจําลอง Network ที่เทียบมาจากความเปน จริง NS2 มีภาษาที่ใชอยู 2 ภาษาดังคือ ภาษา C++ และ OTcl (Appendix A) ในสวนของขั้นตอนในการ ลงโปรแกรม NS2 พรอมทั้งรายละเอียดของการใชงาน OTcl และรายละเอียดของโปรแกรมที่ใชในการ 4
  • 5. จําลองการทํางานของ Network พรอมทั้งยังมีการอธิบายโครงสรางและการทํางานของ NS2 ที่จะกลาวไวใน Chapter 2 หลังจากนั้นจะกลาวถึงหลักการทํางานและโครงสรางสวนประกอบทั้งหมดของ NS2 ใน chapter 3 ซึ่งใน NS2 ยังมี Visualization tool ซึ่งเปน tool ที่ใชในการแสดงผลการทํางานของ Otcl script ออกมาใน รูปของกราฟฟกซึ่งมีทั้งกราฟและการจําลองการทํางานของ Network ซึ่งกราฟนั้นจะมี Xgraph ซึ่งเปน Tool ที่มีอยูแลวใน NS2 เปนตัวแสดงผล สวนในเรื่องของการจําลองการทํางานของ Network จะมี NAM (Network Animator) เปนตัวแสดงผลซึ่งรายละเอียดจะมีการกลาวเพิ่มเติมใน Chapter 4 และใน Chapter 5 จะมีการแสดงตัวอยางการทํางาน 5
  • 6. Chapter 2 Literature Reviews 2.1 Literature Reviews โปรแกรมที่ใชในการจําลองการทํางานของ Network มีมากมายหลายโปรแกรมมากในปจจุบัน ซึ่ง นอกจาก NS2 ที่ไดแนะนําไปบางแลวก็ยังมีโปรแกรมอื่นๆอีกมากมาย ซึ่งแตละโปรแกรมก็มีความสามารถ ในแตละดานที่เพิ่มขึ้นมาแตกตางกัน ซึ่งการที่จะนําแตละโปรแกรมมาใชงานนั้นก็ขึ้นอยูกับความถนัดของ ผูใชและความเหมาะสมของงานซึ่งในที่นี้จะขอแนะนําโปรแกรมที่ใชในการจําลองงานทํางานของ Network ดังตอไปนี้ 1. NS2 NS2 เปน open-source และสามารถที่จะ run ไดทั้งบน Linux , FreeBSD, SunOS, Solaris, Window ถูกพัฒนาขึ้นโดย ISI (Information Sciences Institute) NS2 เปนโปรแกรมที่ใชในการ จําลองการทํางานของ network ในแบบที่เปน discrete event simulator ซึ่งสนับสนุนการจําลองการ เลือกเสนทางในการขนสง packet,จําลองการทํางานของ multicast protocol และ IP protocol เชน UDP, TCP, RTP, SRM ที่อยูบนเครือขายประเภทที่เปน wire และ wireless (local และ satellite) ซึ่ง NS2 เปน tool ที่มีประโยชนมากทั้งยังสนับสนุน multiple protocol และยังมีความสามารถในการ แสดงรายละเอียดของ network traffic ออกมาในรูปแบบของกราฟฟก รวมทั้งยังสนับสนุน algorithm ในการ routing และ queuing เชน FIFO, round-robin เปนตน 2. OPNET OPNET เปนโปรแกรมที่เปนผลิตภัณฑทางพาณิชยซึ่งไดรวบรวม Model ตางๆและ ครอบคลุมถึงเทคโนโลยีทางดาน Network เพื่อรองรับใหผูใชสามารถที่จะออกแบบ และจัดการกับ ระบบเครื อขา ยขั้น พื้ นฐานพร อมทั้งยั งมี การจําลองอุปกรณทางดาน network และnetwork application เพื่อใหผูใชนั้นไดพัฒนาระบบเครือขายเสมือนกับสภาพแวดลอมในความเปนจริง 3. CSim CSim เปน Simulation Environment ที่มีพื้นฐานมาจากภาษา C ใน CSim จะมีการทําโมเดล ที่มีความเปนอิสระและมีความสัมพันธระหวาง entity มีลักษณะการเก็บขอมูลแบบเปนลําดับชั้นจึง เปนระบบที่ชวยทําใหจัดเก็บแฟมขอมูลเปนกลุม สามารถเพิ่มประสิทธิภาพในการทํางานในเวลา เดียวกันภายในระบบได พรอมทั้งยังสนับสนุนระบบการทํางานประเภท rates, data, bandwidth, latency, traffic เปนตน ใน CSim นั้นจะมีแบบจําลองของซอฟตแวรเปนเชิงกลศาสตร (concurrent process oriented) พรอมทั้งยังมี Methods ในการชวยพัฒนาการทํางานทั้งแบบที่เปน Graphical และ Textual ในเรื่องของ Graph file นั้นจะมี Graph file เปน XML ascii-text เพื่อชวยเพิ่มความ สะดวกในเรื่องการทํางานขาม platform โปรแกรม CSim นั้นเปน commercial software 6
  • 7. 4. SWANS (Scalable Wireless As hoc Network Simulator) SWANS เปน scalable wireless network simulator ซึ่งสรางอยูบน JiST platform (Java in Simulation Time) SWANS มีการจัดการระบบในรูปแบบของ independent software component ทํา ใหสามารถที่เปลี่ยนรูปแบบเปน complete wireless network หรือ sensor network configurations ความสามารถของ SWANS นั้นเหมือนกับ NS2 และ GloMoSim แตกตางกันที่ SWANS นั้นจะ รองรับการจําลองNetwork ขนาดใหญๆไดซึ่ง SWANS ยังสามารถแสดงผลลัพธของการจําลองใน ระดับสูง พรอมทั้งยังชวยประหยัดหนวยความจําและ สามารถ run Standard Java network application ไดบน simulate network 5. OMNet++ OMNet++ เปน discrete event simulation environment ซึ่ง OMNet++ นั้นสามารถที่จะ จําลองการสื่อสารของNetwork protocol,computer network,traffic modeling, multi-processor และ distributed system เปนตน OMNet++นั้นไมไดเปน Network simulator ซึ่งตัว OMNet++ เปนแค network simulation platform ซึ่ง Modelsตางๆของ OMNet++ นั้นจะเปน Component architecture ซึ่งคอมโพเนนทตางๆนั้นถูกพัฒนาดวยภาษา C++ ซึ่งการสรางคอมโพเนนทขนาดใหญและโมเดล นั้นจะใชภาษาระดับสูง (high-level language) ในการเขียนซึ่งภาษาระดับสูงที่ใชคือ ภาษา NED (Network Description) 6. NCTUns3.0 NCTUns เปน open-source โปรแกรม NCTUns นั้นพัฒนาขึ้นมาเพื่อใชเปน network simulation และ emulation สําหรับ wireless และ mobile network ซึ่ง NCTUns จะสรางการจําลอง สําหรับ wireless ad hoc, sensor, inter-vehicle communication network, GPRS cellular network และ wireless mesh network ซึ่งความสามารถอีกอยางของ NCTUns คือ สามารถที่จะแบงแยก network device และ network protocol ออกเปนหมวดหมูได 2.2 การติดตั้งโปรแกรม Network Simulator (NS) บน Linux Network Simulator คือ โปรแกรมที่ชวยในการฝกการออกแบบและติดตั้งระบบเครือขาย คอมพิวเตอร ซึ่งโปรแกรมนี้สามารถใชงานไดทั้งบน Window และ Unix (FreeBSD, Linux, SunOS, Solaris) สําหรับการลงโปรแกรมนี้มี 2 วิธีในการลงโปรแกรม คือ สามารถลงแบบที่ละ Component หรือ ลง แบบ all at one ซึ่งในที่นี้จะเลือกลงโปรแกรมแบบ all at once ซึ่งคือ NS-allinone-2.31 เปนโปรแกรม NS allinone Version 2.31 ซึ่งการที่จะลงโปรแกรมแบบ allinone นั้นตองใชเนื้อที่อยางนอย 320 MB 7
  • 8. 1.วิธีการดาวนโหลด (Download) Network Simulator 1.1 ดาวนโหลด Network Simulator (NS-allinone Version 2.31)จาก http://sourceforge.net/project/showfiles.php?group_id=149743&package_id=169689&release_id=4 92770 2.การเตรียมอุปกรณเบื้องตน 2.1 เครื่องคอมพิวเตอรที่มี รายละเอียดดังตอไปนี้ OS : Linux2.6.18.2-34-default i686 System : openSUSE10.2(i586) Download OpenSUSE : http://es.opensuse.org/Versi%C3%B3n_publicada 2.2 หนวยความจําขนาด 320 MB 3.การติดตั้ง (install) 3.1 นํา File ns-allinone-2.31.tar มาไวใน Directory homeและสราง Directory ที่ตองการเพื่อวางns- allinone- 2.31โดยในที่นี้จะไวที่ /home/user/NS 3.2 ทําการแตก file ns-allinone-2.31.tar โดยใชคําสั่ง tar -xvzf ns-allinone- 2.31.tar.gz 3.3 เมื่อแตก file ns-allinone-2.31.tar เรียบรอย ใหไปยัง Directory ที่เก็บ file ns-allinone-2.31 ที่แตก เรียบรอยพิมพ ./install ดังภาพที่ 2.1 ภาพที่ 2.1 แสดงคําสั่งที่ใชในการ install ns-allinone-2.31 8
  • 9. 3.4 Set Environment variables ซึ่ง Environment variables ที่ตอง set มีดังนี้ PATH, LD_LIBRARY_PATH, TCL_LIBRARY เพื่อที่จะได Run file(.tcl) ณ ตําแหนงตางๆ ที่มี file(.tcl) 3.4.1 set path โดยการเพิ่มคําสั่งลงใน /etc/bash.bashrc โดย vi bash.bashrc ดังภาพที่ 2.2 เพื่อที่จะ พิมพคําสั่ง set path ภาพที่ 2.2 แสดงวิธีในการเขาถึง file bash.bashrc 3.4.2 เมี่อเขามาใน bash.bashrc ใหพิมพคําสั่ง Set Path ดังภาพที่ 2.3 PATH=$PATH:$HOME/bin PATH=$PATH:/home/user/NS/ns-allinone-2.31/bin PATH=$PATH:/home/user/NS/ns-allinone-2.31/tcl8.4.14/unix PATH=$PATH:/home/user/NS/ns-allinone-2.31/tk8.4.14/unix LD_LIBRARY_PATH=/home/user/NS/ns-allinone-2.31/otcl-1.13 LD_LIBRARY_PATH=$LD_LIBRARY:/home/user/NS/ns-allinone-2.31/lib, TCL_LIBRARY==/home/user/NS/ns-allinone-2.31/tcl8.4.14/library export PATH LD_LIBRARY_PATH TCL_LIBRARY หมายเหตุ /home/user/NS/ns-allinone-2.31 เปนpath ที่เก็บ ns-allinone-2.31 9
  • 10. ภาพที่ 2.3 แสดงคําสั่งที่ใชในการ Set path 4. วิธีแกปญหา 4.1 เมื่อ install และ set Path แลวแตไมสามารถที่จะ run file (.tcl) ไดใหเขาไปยัง Directory ที่เก็บ Program ns-allinone-2.31 แลวเขาไปยัง Directory bin แลวพิมพ cp * /usr/local/bin 5. การ run ns 5.1 เมื่อลงโปรแกรมเรียบรอยแลวเราจะทําการทดสอบการทํางานของโปรแกรมโดยจะนํา file (.tcl) มาทํา การประมวลผลโดยผูใชจะตอง cd เขาไปยัง Directory ที่เก็บ file(.tcl) แลวพิมพคําสั่ง ns ตามดวยชื่อ file(.tcl) ที่ตองการจะประมวลผล ดังภาพที่ 2.4 ns ns_simple.tcl 10
  • 11. ภาพที่ 2.4 แสดงการใชคําสัง ns เพื่อประมวลผล ่ 5.2 กรณีที่คําสั่ง ns ประมวลผล file (.tcl) ไดถูกตองจะปรากฏผลลัพธที่ไดจากการประมวลผลดังภาพที่ 2.5 และถือวาการ install ns-allinone-2.31 ถูกตอง ภาพที่ 2.5 แสดงผลของการประมวลผล ns-simple.tcl 11
  • 12. 6. NS Simulation Script เมื่อ run ตัวอยางนี้เรียบรอยแลวจะปรากฏผลลัพธดังภาพที่ 2.6 ภาพที่ 2.6 แสดงผลของการประมวลผล ns-simple.tcl สามารถดูตัวอยาง Script ไดที่ http://nile.wpi.edu/NS/simple_ns.html 12
  • 13. Chapter 3 หลักการทํางานของ NS2 3.1 การทํางานของ NS2 nam Nam trace file Otcl Script NS trace file Visualization tool diagram User C++ network elements Perl script results Generates Uses User จะสามารถสราง Otcl Script, C++ Network elements และ Perl Script เพื่อที่จะนํามาใชในการ ออกแบบเพื่อจําลองการทํางานของ Network ซึ่ง User สราง C++ network ขึ้นมาเพื่อใหเปน object ที่อยูใน library ที่สามารถจะเรียกใชไดโดยเรียกใชผาน Command ของ Otcl Script ซึ่งจะมี Otcl linkage เปนตัวที่จะ เรียก Object ใน library ขึ้นมาใชงาน เมื่อ Otcl script ถูกประมวลผล Otcl script จะสราง nam trace file และ ns trace file ซึ่งตัว nam trace file ที่ไดนั้นจะถูกเรียกใชโดย nam เพื่อที่จะนําไปประมวลผลแลวแสดงผล ออกมาในรูปกราฟฟก ในสวนของ perl script นั้นจะเรียกใช ns trace file เพื่อนํา ns trace file มา filter ใหได ผลลัพธออกมาเปน Result ที่จะถูกเรียกใชโดย Visualization tool เชน Xgraph เพื่อที่จะนํามาใชสรางกราฟที่ แสดงผลการทํางานของการจําลอง Network ภาษาทีใชใน NS2 มีอยู 2 ภาษา เนื่องจาก Simulator มีลักษณะการทํางาน 2 อยางที่แตกตางกัน ซึ่งงาน ่ สวนแรกจะเปนการจําลองรายละเอียดตางๆเกี่ยวกับ Protocol จึงจําเปนตองใช System programming language ซึ่งมีประสิทธิภาพในการจัดการขอมูลที่มีหนวยของขนาดขอมูลเปน byte , packet header และ สราง algorithm ที่สามารถประมวลผลขอมูลที่มีขนาดใหญได ซึ่งงานในสวนนี้จะเนน และใหความสําคัญ ในเรื่องของ run-time speed สวนในเรื่องของ turn-around time (คนหา bug ,re-run, recompile) นั้น จะมี ความสําคัญนอยกวา สวนงานในสวนที่ 2 สวนใหญจะเปนเรื่องของการทําวิจัยเกี่ยวกับ network ซึ่งจะตอง 13
  • 14. ยุงเกียวกับพวก Parameters หรือ การทํา Configuration ซึ่ง iteration time (change the model and re-run) นั้น ่ เปนสิ่งสําคัญ เพราะฉะนันจึงจําเปนที่ NS2 จะใช 2 ภาษา คือ ้ 1. C++ - เอาไวใชในการสราง Protocol และ Application Agents - ใชกําหนดคุณสมบัติของกระบวนการทํางานของแตละ packet ตามที่ผูใชตองการ - ใชเปลี่ยนการทํางานของ object ใน C++ เพี่อเอาไวปรับใชกับ non-standard policies ขอดี - ประมวลผลเร็ว ขอเสีย – จะทํางานชาเมื่อมีการ run simulation, คนหา bug, fix bug, recompile, re-run 2. Otcl (Object Tool Command Language) - เอาไวใชในการเขียน Simulation script , Configuration และ setup - ทดสอบผลของ network parameter โดยทําการเปลี่ยนแปลงคาใน object ของ C++ - วิเคราะหผลของการเปลี่ยนแปลง parameter โดยใชการ trace และแสดงผลทางหนาจอ ขอดี – สามารถทํางานไดอยางรวดเร็วเมื่อมีการเปลี่ยนแปลง ขอเสีย – ประมวลผลชา NS Feature - จําลองความแตกตางของ protocol (TCP/UDP) - Wired Routing protocols, Ad-Hoc Routing protocol - วิธีการที่ใชในการจัด packet เพื่อเรียงเขา Queue เชน Drop Tail, RED, WFQ, DRR, LQD เปนตน - คุณลักษณะของ Traffic มีดังนี้ Poisson, Exponential, Pareto เปนตน สําหรับ Traffic ของจุดเริ่มตน คือ CBR หรือ VBR - ปรับเปลี่ยน NS2 เพื่อสราง protocol ใหม หรือพัฒนา protocol เดิมใหเปน Version ใหม - การวัดทางสถิติ เชน Throughput, Delat, Jitter, Queue Monitoring, Drops at Queues - Graphic visualization ใช NAM (Network Animator) ในการจําลอง visualize wired และใช Ad- Hockey ในการจําลอง visualize wireless 3.2 NS Script Structure 1. Create Simulator Object set ns [new Simulator] #เปนการสราง Object ของ Simulator 2. Tracing 14
  • 15. การเปด nam trace file Nam trace file มีเอาไวสําหรับเก็บขอมูลที่จะนํามาใชในการประมวลผลของ NAM เพื่อที่จะแสดงผลการจําลอง Network ออกมาในรูปของกราฟฟก set <variable name> [open <ชื่อ file.nam> w] $ns namtrace-all file-descriptor : เปนการบอกให simulator บันทึก simulation trace ลงใน file- descriptor ตามรูปแบบของ NAM w คือ การบอกวาเปนการเขียนขอมูลลงใน file set nf [open out.nam w] $ns namtrace-all $nf ซึ่งผูใชสามารถที่จะ trace file ของ nam เฉพาะแตละชวงของ event ไดโดยใชคําสั่ง $ns namtrace-queue $<source_node> $<destination_node> $<fileName> การเปด trace file set <variable name>[open <ชื่อ file.tr> w] $ns trace-all $<variable name> w คือ การบอกวาเปนการเขียนขอมูลลงใน file set tf [open out.tr w] $ns trace-all $tf $ns flush-trace : เปน member function ของ trace-all ซึ่งเปนคําสั่งที่ใชในการบันทึก simulation trace ไวในรูปแบบที่กําหนด รายละเอียดของ Trace file ภาพที่ 2.7 แสดงรายละเอียดของ Trace file 15
  • 16. 1. Event คือ Event type ซึ่งมีสัญลักษณอยู 4 ชนิด คือ r, +, -, d r หมายถึง receive + หมายถึง enqueue - หมายถึง dequeue d หมายถึง Drop 2. Time คือ เวลาขณะที่เกิด event 3. From node คือ input node ของ link ที่เกิด event 4. To node คือ output node ของ link ที่เกิด event 5. Pkt type คือ Packet type หรือชื่อของตัวแปรที่ถูกกําหนดคาใหเปน Application 6. Pkt size คือ Packet Size 7. Flags 8. Fid คือ Flow id (ตัวแปรที่เก็บคาสี) 9. Src addr คือ source address 10. Dst addr คือ destination address 11. Seq num คือ Sequence number ของ packet ใน Network Layer 12. Pkt id คือ unique id ของ packet 3. Create Nodes ( physical layer) Node set <variable name> [$ns node] n0 n1 set n0 [$ns node] set n1 [$ns node] Link 4. Links and queuing ( Data-link layer ) $ns <link> node1 node2 <bandwidth> <delay> <queue_type> <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR etc. <link> : Simplex-link คือ การเชื่อมตอแบบทางเดียว Duplex-link คือ การเชื่อมตอแบบ 2 ทาง $ns duplex-link $n0 $n1 1Mb 10ms RED $ns queue-limit node1 node2 number : เปนการกําหนดขนาดของ queue ที่เชื่อมระหวาง node1 และ node2 $ns duplex-link-op node1 node2… : เอาไวใชสาหรับการแสดงผลของ NAM ํ 16
  • 17. 5. Creating UDP connection ( transport layer) udp การสรางตัวแปรที่เปน Agent set udp [new Agent/UDP] n0 set null [new Agent/NULL] การกําหนดให Agent อยูบน Node n1 $ns attach-agent node agent : null $ns attach-agent $n0 $udp Create UDP $ns attach-agent $n1 $null การสรางการติดตอสื่อสารระหวาง agent $ns connect agent1 agent2 $ns connect $udp $null การสราง Agent ขึ้นมา 2 ตัวโดยใหตัวหนึ่งมีคุณสมบัติของ UDP นั่นก็คือ udp และอีกตัวใหเปน null แลวกําหนดให Agent แตละตัวอยูบน node คนละตัวกัน เมื่ อ กํ าหนดให มี ก ารติ ด ตอ สื่ อ สารกั น จะมี ตั วหนึ่ ง เป น source อี กตั ว เป น destination 6. Creating TCP connection ( transport layer ) tcp การสรางตัวแปรที่เปน Agent set tcp [new Agent/TCP] n0 set tcpsink [new Agent/TCPSink] การกําหนดให Agent อยูบน Node n1 $ns attach-agent $n0 $tcp sink Create TCP $ns attach-agent $n1 $tcpsink การสรางการติดตอสื่อสารระหวาง Source และ destination $ns connect $tcp $tcpsink การสราง Agent ขึ้นมา 2 ตัวโดยใหตัวหนึ่งมีคุณสมบัติของ TCP นั่นก็คือ tcp และอีกตัวใหเปน tcpsink แลวกําหนดให Agent แตละตัวอยูบน node คนละ ตั ว กั น เมื่ อ ให มี ก ารติ ด ต อ สื่ อ สารกั น จะมี ตั ว หนึ่ ง เป น source อี ก ตั ว เป น destination 17
  • 18. 7. Creating Traffic ( application layer) cbr (On Top of UDP) เชน CBR set cbr [new Application/Traffic/CBR] udp $cbr set packetSize_ 500 $cbr set interval_ 0.005 n0 $cbr attach-agent $udp n1 สราง Traffic ที่ชื่อ cbr แลวกําหนดคุณสมบัติของ cbr ทั้งขนาดของ packet และชวงเวลาที่ packet ใช รวมทั้งกําหนดใหcbr อยูบน Agent null UDP Create CBR การสราง Application ftp (On Top of TCP) เชน FTP, Telnet FTP tcp set ftp [new Application/FTP] n0 $ftp attach-agent $tcp Telnet set telnet [new Application/Telnet] n1 $telnet attach-agent $tcp sink Create FTP สราง Application ที่ชื่อ ftp แลวกําหนดคุณสมบัติของ FTP รวมทั้ง กําหนดให ftp อยูบน Agent tcp 8. การเขียน Procedure proc ชื่อprocedure {} { body } proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0 } 18
  • 19. 9. Call Procedure $ns at 5.0 quot;finishquot; 10. Schedule event $ns at <time> <event> : เปนคําสั่งที่จะทําการประมวลผล event ตามเวลาที่กําหนด <event>: คําสั่งที่ไดกําหนดไวใน ns/tcl $ns at 1.0 “Start” 11. Start NS (เริ่มการทํางานของ Script) $ns run 12. Stop NS Exit 0 การ Run NS Program การ Run แบบ interactive mode โดยการพิมพ ns แลวจึงพิมพ command ตางๆ ทีละบรรทัด bash-shell$ ns % set ns [new Simulator] % $ns at 1 “puts “Hello World!”” % $ns at 1.5 “exit” % $ns run Output : Hello World! การ Run แบบ Batch mode เปนการที่ run จาก file ที่มีการกําหนด Script ไวเรียบรอยแลว - ใชคําสั่ง ns <Otcl script file> กระบวนการของการประมวลผลของ NS - อาน tcl file - Run simulation program - สราง trace file - แสดงผลลัพธโดยใช NAM (network animator) - ผลทางสถิติ 19
  • 20. 3.3 การจัดการขอมูลเกี่ยวกับ Trace file ใน NS simulator นั้นสามารถจัดเตรียมรายละเอียดของขอมูลในแตละเหตุการณบน Network ได ซึ่งถาผูใชตองการที่จะวิเคราะหขอมูลตางๆ ผูใชจะตองทําการ trace ขอมูลตางมาไวใน file แลวทําการ วิเคราะหจัดการขอมูลตางๆ เองซึ่งอาจจะวิเคราะหขอมูลตางๆเหลานี้โดยการใชภาษาที่ใชในการเขียน โปรแกรมตาง ๆ ในการจัดการกับไฟล ซึ่งสวนใหญจะจัดการกับไฟลตางๆโดยใชภาษา script เนื่องจาก ภาษาเหลานี้จะทํางานโดยการแปลคําสั่งและประมวลผลไดโดยที่ไมตองทําการคอมไพล การจัดการไฟลขอมูลดวย awk Awk เปนภาษาที่สามารถจะนํามาใชในการจัดการกับไฟลขอมูลไดดวยคําสั่งงายๆ ที่มีใน awk ซึ่ง Awk มีความสามารถดังตอไปนี้ - จัดการกับ database เล็กๆ - สราง report - ตรวจสอบความถูกตองของขอมูล - สราง index และแสดงเอกสารของงานตางๆ - ใชทดสอบ algolithm กอนที่จะมีการนําไปประยุกตใชดวยภาษาอื่นๆ เชน การแสดงคาในแตละ column ในไฟล, การคํานวณหาผลรวม, การหาผลหาร เปนตน Example : ตัวอยาง awk script ที่คํานวณคาเฉลี่ยในคอลัมนที่ 4 จากไฟล BEGIN {FS = “t”} {nl++} {s=s+$4} END {print “average:” s/n1} ตัวอยางขางตนมีชื่อไฟลวา Average.awk ซึ่งถาตองการนํา code ดังกลาวไปใชงานกับ file ที่ชื่อ out.ns ใหใช คําสั่งในการประมวลผล file ดังนี้ awk –f Average.awk out.ns ซึ่งผลลัพธก็คือการหาคาเฉลี่ยในคอลัมนที่ 4 ของไฟล out.ns วิธีการประมวลผล awk กรณีที่ awk อยูใน OTcl script exec awk $<function> <fileName> เชน exec awk $awkCode all.q กรณีที่ awk อยูใน file awk –f <program> <input-file> เชน awk –f advice คําสั่ง –f มีเพื่อที่จะนําโปรแกรม awk มาจากไฟล source file 20
  • 21. การจัดการไฟลขอมูลดวย perl Perl ยอมาจาก Practical Extraction and Report Language ซึ่ง perl สามารถที่จะทําการประมวลผล ไฟลขอมูลประเภท ASCII ใน unix ซึ่งภาษานี้ถูกสรางมาเพื่อชวยใหงานของผูดูแลระบบนั้นงายขึ้น ขอดีของการใช perl - งายตอการเขียนโปรแกรมเล็กที่ใชในการกรองขอมูลจากไฟลตางๆ - สามารถใชไดในหลายๆ OS โดยที่ไมตองทําการเปลี่ยนแปลง code - การดูแลและการตรวจสอบ Bug ใน perl script นั้นทําไดงาย - ภาษา perl เปนที่นิยมกันมาก ซึ่งใน NS simulator สามารถที่จะนําขอดีของ perl script มาชวยในการวิเคราะหขอมูลที่มีอยูใน trace file ตางๆ เพื่อที่จะไดนําผลที่ไดจากการวิเคราะหไปใชในการสรางกราฟ หรือไปใชในการตรวจสอบความ ถูกตองของขอมูลที่ไดจากการทํางานของ ns simulator ดังตัวอยางตอไปนี้ที่จะแสดงการนํา perl script มา ชวยในการจัดการประมวลผลขอมูลที่อยูใน trace file ซึ่งตัวอยางนี้จะคํานวณคา throughput ของการติดตอ กับ TCP ซึ่งตัวโปรแกรมจะทําการหาคาเฉลี่ยของ throughput ที่อยูในชวงตามคาที่ไดรับจาก parameter ซึ่ง เรียกวา “granularity” ซึ่ง input ที่รับมานั้นมี 3 คา คือ ชื่อของ trace file (ในตัวอยางนี้คือ out.tr), nodeที่ ตองการตรวจสอบคา throughput, คาgranularity Example : Perl script # type: perl throughput.pl <trace file> <required node> <granlarity> > file $infile=$ARGV[0]; $tonode=$ARGV[1]; รั บ ค า จากพารามิ เ ตอร 3 ค า คื อ ชื่ อ trace file, nodeที่ตองการ, granularity แลว $granularity=$ARGV[2]; นํามาเก็บไวในตัวแปรตางๆ #we compute how many bytes were transmitted during time interval specified #by granularity parameter in seconds $sum=0; $clock=0; open (DATA,quot;<$infilequot;) #เปนคําสั่งเปด file out.tr แลวเอาคาใน file มาไวที่ DATA || die quot;Can't open $infile $!quot;; while (<DATA>){ @x= split(' '); # นําคาที่อยูใน DATA มาไวใน array x #column 1 is time 21
  • 22. if ($x[1]-$clock<=$granularity) { # ตรวจสอบวาคอลัมนที่ 0 นั้นมีคาเทากับ reception หรือไม if ($x[0] eq 'r') { # ตรวจสอบวาคาในคอลัมนที่ 3 นั้นมีคาเทากับ node ที่ใสมาในพารามิเตอรหรือไม if ($x[3] eq $tonode) { # ตรวจสอบวา packet เปนประเภท TCP หรือไม if ($x[4] eq 'tcp') { $sum=$sum+$x[5]; } } } } else { $throughput=$sum/$granularity; เปนการคํานวณหาคา throughput print STDOUT quot;$x[1] $throughputnquot;; และทํ า การพิ ม พ ค า ที่ ไ ด จ ากการ $clock=$clock+$granularity; คํานวณออกมา $sum=0; } } $throughput=$sum/$granularity; print STDOUT quot;$x[1] $throughputnquot;; $clock=$clock+$granularity; $sum=0; close DATA; exit(0); 22
  • 23. ตัวอยางขอมูลใน out.tr Trace file ภาพที่ 3.1 แสดงขอมูลใน file out.tr วิธีการประมวลผล perl script perl <PerlFile> <trace file> เมื่อตองการทดสอบการทํางานของไฟลตัวอยางขางตนใหใชคําสัง ่ perl throughput.pl <trace file> <required node> <granlarity> > Resultfile 3.4 NS Process NS Architecture ในมุมมองของผูใชทั่วไป You are here ภาพที่ 3.2 Architecture View of NS ภาพที่ 2.9 นี้จะแสดงถึงสถาปตยกรรมทั่วๆไปของ NS ซึ่งเปนสถาปตยกรรมของผูใชทั่วไปที่ไมใช NS developer โดยผูใชจะใชภาษา Otcl script ในการออกแบบและ คําสั่งในการประมวลผลรูปแบบของการ 23
  • 24. จําลองใน Tcl ซึ่ง Tcl จะมีการเรียกใช simulator object ใน Otcl library ในสวนของ event scheduleและ network component นั้นไดมีการพัฒนาไวเรียบรอยแลวซึ่ง Souce code ภายในนั้นถูกพัฒนาดวยภาษา C++ ดังนั้น Otcl สามารถที่จะเรียกใชไดตลอดการทํางาน ซึ่งจะมี Otcl linkage เปนตัวเรียกใชงานโดยเรียกผาน tclcl ซึ่งทั้งหมดจะเปนการทํางานของ NS (NS คือ OO extended Tcl interpreter รวมกับ network simulator library ) ภาพที่ 3.3 แสดงกระบวนการประมวลผลของ NS2 จากภาพที่ 2.10 จะเห็นวา Otcl script ที่เราจําลองขึ้นมานั้นจะแสดงผลออกมาในรูปแบบของภาพ กราฟฟกโดย nam ซึ่งการที่จะไดแบบจําลองที่แสดงผลตาม Script นั้นตัว NS2 จะตองประมวลผลจาก Script แลวเชื่อมโยงคําสั่งจาก Otcl linkage แลวเรียก Object ที่ไดกําหนดไวแลวในภาษา C++ มาทําการ ประมวลผลและแสดงผลตามที่กําหนดไวใน Script ผูใชทั่วไป Analysis NS-2 Simulation- Simulation Otcl Script Otcl Interperter Results NAM C++ Library ภาพที่ 3.4 แสดงกระบวนการทํางานของ NS-2 24
  • 25. เมื่อผูใชตองการที่จะใช NS2 ผูใชจะกําหนด Otcl Script แลวทําการ run Script แลว NS2 จะสราง Output file ใหแลวจะทําใหไดผลลัพธที่แสดงผลการจําลองตาม Script ออกมาในรูปแบบของกราฟฟก (NAM) NS2 Developer 1. พัฒนา NS โดยใชภาษา C++ สราง object ขึ้นมา 2. ทดสอบ object ที่พัฒนาขึ้นมาโดยใช Otcl script โดยจะออกแบบการจําลอง Network ขึ้นมาเพื่อ ทดสอบการทํางานของ Object ที่สรางขึ้น 3. ทดสอบการทํางานโดยดูผลจากการ run script ผาน NAM และ XGraph Extending NS2 มีอยู 2 แบบดังนี้คือ Extending NS in Otcl และ Extending NS in C++ ns-allinone Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1 tcl ... C++ code ex test lib mcas ... examples validation OTcl code ภาพที่ 3.5 แผนภาพโครงสรางของ File ใน NS-allinone 1. Extending NS in Otcl การเพิ่ม file.tcl 1.1 นําไฟล (.tcl)ไปไวใน /ns2/tcl/lib 1.2 เขาไปใน Makefile ที่อยูใน /ns2 แลวเพิ่มชื่อของไฟล (.tcl) นั้นไวบริเวณ NS_TCL_LIB 1.3 เขาไปใน tcl/lib/ แลวเพิ่มชื่อไฟล (.tcl) เขาไป 1.4 Recompile 25
  • 26. 2. Extending NS in C++ การแกไข Code C++ 2.1 ใชคําสั่ง “make clean” 2.2 ใชคําสั่ง “make” เพื่อที่จะทําการ compile .o ทั้งหมดอีกครั้ง การสราง Network Object ดวย C++ เขาไปใน NS การสราง network object ใหมโดยใชภาษา C++ นั้นจะตองมีการสรางการเชื่อมโยงระหวางตัวแปร ใน Otcl และตัวแปรในภาษา C++ เพื่อที่เวลาใช Otcl script แลว NS2 จะสามารถที่จะคนหาตัวแปรแลว match กับคําสั่งที่มีแลวทําการแสดงผลได ซึ่งจะตองนํา file ที่เปน code ภาษา C++ เขาไปไวในโปรแกรม NS2 ขั้นตอนของการสราง Network object มีดังนี้ 1. Export C++ class to OTcl 2. Export C++ class variables to OTcl 3. Export C++ Object Control Commands to OTcl 4. Execute an OTcl command from C++ 5. Compile and Run ตัวอยางของการสราง Network object (MyAgent) Step1. Export C++ class to OTcl เราสราง network object class ชื่อวา MyAgent ซึ่ง derive มาจาก Class Agent ใน C++ โดย Class MyAgent นี้จะทําการสรางตัวแทนของ Object นี้ไวใน Otcl ซึ่งการที่จะทําเชนนี้ไดจะตองนิยาม linkage object เปนตัวกลางในการเชื่อมตอ โดยสราง Class MyAgentClass ขึ้นมา ซึ่ง Class MyAgentClass นั้น derive มาจาก Class Tclclass ซึ่ง linkage object นี้จะทําการสราง OTcl object ที่มีชื่อวา “Agent/MyAgentOtcl” (ในตัวอยางนี้) และสราง Linkageระหวาง OTcl object และ C++ object ที่เปนตัว สราง object ซึ่งในที่นี้ คือ MyAgent 26
  • 27. เมื่อ NS เริ่มทํางานจะมีการเรียก constructor ของตัวแปร static ที่ชื่อวา class_my_agent แลวจึงสราง ตัวแทนของ “MyAgentClass” ขึ้น Class Agent/MyAgentOtcl และ method ตางๆจะถูกสรางไวใน OTcl space เมื่อใดที่ User ใน Otcl พยายามที่จะการสรางตัวแทนของ Object จะทําไดโดยใชคําสั่ง “new Agent/MyAgentOtcl” ซึ่งจะมีการเรียก Method “MyAgentClass::create” เพื่อสรางตัวแทนของ “MyAgent” และ สงคา address กลับไป ในการสราง Object ใน C++ จาก OTcl นั้นจะไมไดคุณสมบัติ function หรือ member variable ของ C++ จาก OTcl Step 2. Export C++ class variables to OTcl สมมติวา C ++ Object “MyAgent” ที่เราสรางใหมขึ้นมา มี parameter 2 ตัว คือ “my_var1” และ “my_var2” ซึ่งเราตองการที่จะ configure จาก OTcl โดยใช input simulation script เราจะใช binding function ทําการ binding แตละตัวแปรของ ClassในC++ ที่เราตองการที่จะสงคาออกไป ซึ่ง binding function นั้น จะทําการสรางตัวแปรขึ้นมา (ชื่อตาม my_var1) โดยจะนําไป match กับ OTcl object ของ class (“Agent/MyAgentOtcl”) และทําการ binding แบบ bi-directional ระหวางตัวแปรของ OTcl class กับตัวแปร ของ C++ ที่มี address ตรงกับ my_var1 และ my_var2 ตามลําดับ ภาพที่ 3.6 แสดงการทํางานแบบ Bidirectional binding 27
  • 28. Binding function จะอยูใน constructor function เพื่อใชในการ binding ตอนที่ instance ของ object นี้ถูก  สรางขึ้น ซึ่ง N S สนับสนุนฟงกชั่น B i n d i n g 4 แบบที่เหมาะสําหรับ D a t a t y p e 5 แบบดังนี้ - bind(): real or integer variables - bind_time(): time variable - bind_bw(): bandwidth variable - bind_bool(): boolean variable ดังนั้น User designing และ running simulation ที่ใช OTcl Script จะสามารถ configure parameter ที่สรางใน C++ ได Step 3. Export C++ Object Control Commands to OTcl การ export C++ object variable ที่ User สรางนั้น เราจึงตองกําหนดให control ของ C++ Object ไป อยูใน OTcl ซึ่งจะทําได โดยใหเรา define “command” member function ของ C++ object (“MyAgent”) เพื่อที่จะทําหนาที่เปนตัว interpreter ของ OTcl ซึ่ง “command” member function จะทําการคนหา Member function ที่ Match กับ OTcl object ใหกับ User เมื่อ instance ของ the shadow OTcl นั้น ทําการ Match “MyAgent” object ที่ถูกสรางใน Otcl space (set myagent [new Agent/MyAgentOtcl]) แลว และ User ทําการเรียก Member function ของ object ($myagent call-my-priv-func), OTcl จะคนหา Member function ที่ User ตองการใน OTcl object ถาชื่อ Member function ที่ใหมาหาไมเจอมันจะเรียก “MyAgent :: command” แลวสงคาชื่อของ OTcl member function ที่เรียก และ argument ไปในรูปแบบของ argc/argv แตถาหาเจอ และมี action มารองรับ มัน จะทํา action และทําการ return ผลลัพธกลับไปและเรียก function ถาไมมี “command” function ของ object บรรพบุรุษมันจะทํา recursive หาจนกวาจะพบ แตถาหาชื่อนั้นไมพบในบรรพบุรุษไหนเลย ก็จะมี error message สงไปให OTcl object และ OTcl object จะแสดง error message ใหกับ User 28
  • 29. Step 4. Execute an OTcl command from C++ เมื่อเราตองการที่จะสราง network object ใหม เราจึงจําเปนที่จะตองมีการ Execute คําสั่งของ OTcl จาก C++ object จากภาพดานลางเปนตัวอยางของการ implement “MyPrivFunc” ซึ่งเปน member function ของ “MyAgent” ซึ่งเปนการแสดงผลลัพธของคาของ my_var1 และ my_var2 ออกทางหนาจอ ซึ่งการที่จะ Execute คําสั่งของ OTcl จาก C++ นั้นเราตองมีการอางถึง “Tcl::instance()” ซึ่งถูกประกาศ เปน static member variable ซึ่งในตัวอยางนี้ไดแสดงการสงคําสั่ง OTcl ใหกับ interpreter ซึ่งมี 2 ทาง คือ MyPrivFunc และ Tcl::instance() Step 5. Compile and Run and Test “MyAgent” 1. บันทึก ชื่อfile.cc ที่ตองการจะสราง Network Object เชน ex-linkage.cc แลวบันทึกไวใน NS-2 directory 2. เปด “Makefile” แลวทําการเพิ่ม ชื่อfile.o (เชน ex-linkage.o) ไวในบรรทัดสุดทายของObject file list OBJ_CC = … ./ex-linkage.o $(OBJ_STL) 3. Re-compile NS โดยใชคําสั่ง “make” 4. สราง file.tcl ขึ้นมาเพื่อทดสอบการทํางานของ Network Object ที่สรางขึ้น 5. run Otcl script โดยใชคําสั่ง ns ชื่อfile.tcl Download ex-linkage.cc : http://nile.wpi.edu/NS/Example/ex-linkage.cc Download ex-linkage.tcl : http://nile.wpi.edu/NS/linkage.html 29
  • 30. ภาพที่ 3.7 แสดง Otcl script ที่จะใชทดสอบการทํางานของ Network Object ภาพที่ 3.8 แสดงผลลัพธของการทดสอบการทํางานของ Network Object Static class MyAgentClass:public TclClass{ Public: MyAgentClass (): TclClass (“Agent/MyAgentOtcl”){} TclObject* create(int,const char*const*) { Return(new MyAgent()); } } class my agent; Agent/MyAgentOtcl TclObject Agent Create C++ object init init Create Otcl shadow object OTcl TclObject C++ (C++) parent MyAgent constructor constructor constructor ภาพที่ 3.9 แสดงการเชื่อมตอของ OTcl กับ C++ 30
  • 31. เมื่อผูใชมีการเรียกใช network object ที่สรางขึ้นโดยใชคําสั่ง set myagent [new Agent/MyAgentOtcl] แลว TclObject จะมีการ create C++ object ที่ชื่อ MyAgent แลวจะมีการเรียก constructor ของ parent และ TclObject ใน code C++ จะถูกประมวลผลการทํางาน แลวสงผลลัพธกลับไป โดยใน TclObject จะมีการสราง OTcl shadow object ขึ้นมาเพื่อที่จะมาทําการสงคาไปใหกับ Agent เพื่อ เชื่อมตอและแสดงผลลัพธที่ไดจากการทํางาน 31
  • 32. Chapter 4 Visualization Tools 4.1 nam-1 (Network AniMator Version 1) Nam เปน Animation tool ที่มีพื้นฐานมาจาก Tcl/TK มีไวสําหรับแสดงการจําลองการทํางาน ของ network และ packet ซึ่งแสดงผลออกมาในรูปของภาพกราฟฟกโดยไปดึงขอมูลที่ใชในการจําลอง Network จาก nam teace file ที่ไดสรางไวจากใน Tcl Script ซึ่ง Nam ไดสนับสนุน topology latout, packet level animation และ เครื่องมือในการตรวจสอบขอมูล Run Nam nam <nam-file> nam out.nam Run Nam in Ns2 script exec nam <nam-file> exec nam out.nam Nam Interface 1. Color mapping $ns color fid <color> : เปนการกําหนดสีใหกับ packet ตามเสนทางการเดินทางโดยใช flow id (fid) เปนตัวบงบอก COLOR : red, blue เปนตน $ns color 40 red $ns color 41 blue 2. Color flow id association $<variable of agent> set fid_ <variable of color> $tcp0 set fid_ 40 # red packets แสดงวา 40 = สีแดง 3. Node Color เปนการกําหนดสีใหกับ node $<variable of node> color <COLOR> COLOR : red, blue เปนตน $node color red #nodeนี้จะมีสีเปนสีแดง 32
  • 33. 4. Node Shape เปนการกําหนดรูปรางใหกับ node $<variable of node> shape <SHAPE> SHAPE : circle, box, hexagon เปนตน $node shape box 5. Node Marks เปนการเพิ่มและลบ node ที่มีการ mark ไวตามเวลาที่กําหนด $ns at 1.0 “$n0 add-mark m0 blue box” $ns at 2.0 “$n0 delete-mark m0” เมื่อกําหนดเชนนี้เวลาแสดงผลจะปรากฎ Mask ตามที่ระบุภายในชวงเวลา 1-2 วินาที 6. Node Lable เปนการกําหนดขอความกํากับที่ node เริ่มตั้งแตชวงเวลาที่กําหนด $ns at 1.1 “$n0 label ”web cache 0”” 7. Link Color เปนการกําหนดสีของlink จาก Node หนึ่งไปยังอีก Node $ns duplex-link-op $n0 $n1 color quot;greenquot; 8. Link Label เปนการกํากับขอความบน link ที่อยูระหวาง node 2 node ที่กาหนด ํ $ns duplex-link-op $n0 $n1 label “backbonequot; ภาพที่ 4.1 แสดงหนาจอที่มีการเรียกใช Nam 33
  • 34. 4.2 Xgraph Xgraph คือ Plotting Program ซึ่ง Plotting Program นี้มีความสามารถที่จะสรางการแสดงผลออกมา ในรูปของภาพกราฟฟกจากการแสดงผลลัพธจาก ns ได และภายในหัวขอนี้จะแสดงวิธีการสราง output file จาก Tcl scripts ซึ่งเปนตัวที่จะมากําหนดขอมูลสําคัญที่ใชในการ plot graph และจะแสดงออกมาดวย โปรแกรม Xgraph ชื่อกราฟ : -t “title” ขนาดของกราฟ : -geometry xsize * ysize ชื่อกํากับแตละแกน : -x “xtitle” (แกน x) และ –y “ytitle” (แกน y) สีของตัวอักษรและgrid : -v Run Xgraph xgraph <ชื่อ file> -geometry xsize*ysize –t “title” –x “xtitle” –y “ytitle” Run Xgraph (in NS2 Script) Exec xgraph <ชื่อ file> -geometry xsize*ysize –t “title” –x “xtitle” –y “ytitle” Exec xgraph out.tr –geometry 800* 400 ภาพที่ 4.2 แสดงหนาจอที่มีการเรียกใช Xgraph 34
  • 35. Creating Output Files for Xgraph อีกสวนหนึ่งของโปรแกรมภายใน ns-allinone package คือ 'Xgraph' , Plotting Program นี้มี ความสามารถที่จะสรางการแสดงผลออกมาในรูปของภาพกราฟฟกจากการแสดงผลลัพธจาก ns ได และ ภายในหัวขอนี้จะแสดงวิธีการสราง output file จาก Tcl scripts ซึ่งเปนตัวที่จะมากําหนดขอมูลที่สําคัญใน การ plot graph และจะแสดงออกมาดวยโปรแกรม Xgraph 1.Topology and Traffic Sources ในขั้นตอนแรกเราจะสราง Topology ขึ้นมาตามแบบในรูปตอไปนี้ ในสวนของ code ขางตนนี้จะมีความคลายคลึงกับตัวอยางโปรแกรมในขางตน โดยความหมายจาก สวนนี้คือการสราง node ใหมเปนจํานวน 5 node set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] $ns duplex-link $n0 $n3 1Mb 100ms DropTail $ns duplex-link $n1 $n3 1Mb 100ms DropTail $ns duplex-link $n2 $n3 1Mb 100ms DropTail $ns duplex-link $n3 $n4 1Mb 100ms DropTail 35
  • 36. จากนั้นทําการสราง traffic ขึ้นมาเพื่อเพิ่มเติมรายละเอียดใหกับ node ดังกลาว โดยในสวนนี้จะเขียน อยูในรูปของฟงกชันซึ่งมีชื่อวา attach-expoo-traffic และมีการรับคาจาก parameter 6 คาซึ่งคือ node,sink,size,burst,idle,rate เพื่อความสะดวกในการกําหนดรายละเอียดของ node นั้น ๆ proc attach-expoo-traffic { node sink size burst idle rate } { #Get an instance of the simulator set ns [Simulator instance] #Create a UDP agent and attach it to the node set source [new Agent/UDP] $ns attach-agent $node $source #Create an Expoo traffic agent and set its configuration parameters set traffic [new Application/Traffic/Exponential] $traffic set packetSize_ $size $traffic set burst_time_ $burst $traffic set idle_time_ $idle $traffic set rate_ $rate # Attach traffic source to the traffic generator $traffic attach-agent $source #Connect the source and the sink $ns connect $source $sink return $traffic } จากตัวอยางฟงกชันดังกลาวนี้ จะมีอยูทั้งหมด 6 arguments 1. node คือ node ที่มีการสรางไวขางตน 2. sink คือ ตัวแปรของ traffic sink ที่สรางขึ้น 3. size คือ ขนาดของ packet ของ traffic source 4. burst คือ burst time 5. idle คือ idle time 6. rate คือ burst peak rate จากรายละเอียดของฟงกชันนี้จะสราง traffic ขึ้นมาและทําการ attaches เขาไปภายใน node และมี การสราง traffic object ซึ่งจากการสราง Traffic/Expoo object จะสามารถระบุการตั้งคาตาง ๆ ใหกับ traffic object ไดโดยการรับคามาจาก parameters ซึ่งคาที่จะสงมากับ parameter นั้นมาจากการสรางขึ้นมากับ traffic source และมีการ attach traffic เขาไปใน source รวมทั้งยังมีการ connect ระหวาง source และ sink ดวย สวนตอไปนี้คือการสราง traffic เพื่อสรางความแตกตางกันของคา peak rates ภายใน node n0 , n1 และ n2 และจากนั้นจะเชื่อมโยง 3 traffic sink ไปยัง node n4 36
  • 37. set sink0 [new Agent/LossMonitor] * ในสวนนี้ใช Agent/LossMonitor set sink1 [new Agent/LossMonitor] set sink2 [new Agent/LossMonitor] objects ในการสราง traffic sinks เพื่อ $ns attach-agent $n4 $sink0 $ns attach-agent $n4 $sink1 นํามาชวยในการเก็บจํานวน bytes ที่ได $ns attach-agent $n4 $sink2 มีการรับเขามา ซึ่งจะมีสวนชวยในการ set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k] set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k] คํานวณหา bandwidth set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k] 2. Recording Data in Output Files ในสวนแรกนีจะทําการเปด output files ขึ้นมา 3 files จาก Tcl script ตามตัวอยางตอไปนี้ ้ set f0 [open out0.tr w] set f1 [open out1.tr w] set f2 [open out2.tr w] สรางฟงกชันเพื่อจบการทํางานของโปรแกรมดังนี้ proc finish {} { global f0 f1 f2 #Close the output files close $f0 close $f1 close $f2 #Call xgraph to display the results exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 & exit 0 } 37
  • 38. จากฟงกชันนี้ไมเพียงแตปดการทํางานของ output files แตจะเปนการเรียกการทํางานของ Xgraph ขึ้นมาเพื่อแสดงผลลัพธดวยตามขนาดของ size window ที่ 800*400 ที่หนาจอ screen proc record {} { global sink0 sink1 sink2 f0 f1 f2 #Get an instance of the simulator set ns [Simulator instance] #Set the time after which the procedure should be called again set time 0.5 #How many bytes have been received by the traffic sinks? set bw0 [$sink0 set bytes_] set bw1 [$sink1 set bytes_] set bw2 [$sink2 set bytes_] #Get the current time set now [$ns now] #Calculate the bandwidth (in MBit/s) and write it to the files puts $f0 quot;$now [expr $bw0/$time*8/1000000]quot; puts $f1 quot;$now [expr $bw1/$time*8/1000000]quot; puts $f2 quot;$now [expr $bw2/$time*8/1000000]quot; #Reset the bytes_ values on the traffic sinks $sink0 set bytes_ 0 $sink1 set bytes_ 0 $sink2 set bytes_ 0 #Re-schedule the procedure $ns at [expr $now+$time] quot;recordquot; } จากฟงกชันกอนหนานี้จะทําการอานจํานวนของ Bytes จากสาม traffic sinks ที่ถูกเก็บไวจาก object ของ Agent/LossMonitor และจากนั้นจะทําการคํานวณ Bandwidth ในหนวยของ MBits/sec และจะเขียน ขอมูลที่เปนเวลาปจจุบันและคา Bandwidth ที่คํานวณไดลงในแตละ output files ไปดวยกันกอนที่จะทําการ reset คาของ bytes_value ที่ traffic sinks 3. Running the Simulation มาถึงในสวนนี้ จะได schedule ขึ้นมาจากกลุมคําสั่งตอไปนี้ และทําการรันโปรแกรมขึ้นมาตาม ตารางเวลาดังกลาวนี้ $ns at 0.0 quot;recordquot; $ns at 10.0 quot;$source0 startquot; Result $ns at 10.0 quot;$source1 startquot; $ns at 10.0 quot;$source2 startquot; $ns at 50.0 quot;$source0 stopquot; $ns at 50.0 quot;$source1 stopquot; $ns at 50.0 quot;$source2 stopquot; $ns at 60.0 quot;finishquot; $ns run 38
  • 39. ภาพที่ 4.3 แสดงผลการทํางานของ Xgraph Source Code #Create a simulator object set ns [new Simulator] #Open the output files set f0 [open out0.tr w] set f1 [open out1.tr w] set f2 [open out2.tr w] #Create 5 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] #Connect the nodes $ns duplex-link $n0 $n3 1Mb 100ms DropTail $ns duplex-link $n1 $n3 1Mb 100ms DropTail $ns duplex-link $n2 $n3 1Mb 100ms DropTail $ns duplex-link $n3 $n4 1Mb 100ms DropTail #Define a 'finish' procedure proc finish {} { global f0 f1 f2 #Close the output files close $f0 close $f1 close $f2 #Call xgraph to display the results exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 & exit 0 } 39
  • 40. #Define a procedure that attaches a UDP agent to a previously created node #'node' and attaches an Expoo traffic generator to the agent with the #characteristic values 'size' for packet size 'burst' for burst time, #'idle' for idle time and 'rate' for burst peak rate. The procedure connects #the source with the previously defined traffic sink 'sink' and returns the #source object. proc attach-expoo-traffic { node sink size burst idle rate } { #Get an instance of the simulator set ns [Simulator instance] #Create a UDP agent and attach it to the node set source [new Agent/UDP] $ns attach-agent $node $source #Create an Expoo traffic agent and set its configuration parameters set traffic [new Application/Traffic/Exponential] $traffic set packetSize_ $size $traffic set burst_time_ $burst $traffic set idle_time_ $idle $traffic set rate_ $rate # Attach traffic source to the traffic generator $traffic attach-agent $source #Connect the source and the sink $ns connect $source $sink return $traffic } #Define a procedure which periodically records the bandwidth received by the #three traffic sinks sink0/1/2 and writes it to the three files f0/1/2. proc record {} { global sink0 sink1 sink2 f0 f1 f2 #Get an instance of the simulator set ns [Simulator instance] #Set the time after which the procedure should be called again set time 0.5 #How many bytes have been received by the traffic sinks? set bw0 [$sink0 set bytes_] set bw1 [$sink1 set bytes_] set bw2 [$sink2 set bytes_] #Get the current time set now [$ns now] #Calculate the bandwidth (in MBit/s) and write it to the files puts $f0 quot;$now [expr $bw0/$time*8/1000000]quot; puts $f1 quot;$now [expr $bw1/$time*8/1000000]quot; puts $f2 quot;$now [expr $bw2/$time*8/1000000]quot; #Reset the bytes_ values on the traffic sinks $sink0 set bytes_ 0 $sink1 set bytes_ 0 $sink2 set bytes_ 0 #Re-schedule the procedure $ns at [expr $now+$time] quot;recordquot; } #Create three traffic sinks and attach them to the node n4 set sink0 [new Agent/LossMonitor] set sink1 [new Agent/LossMonitor] set sink2 [new Agent/LossMonitor] $ns attach-agent $n4 $sink0 $ns attach-agent $n4 $sink1 40
  • 41. $ns attach-agent $n4 $sink2 #Create three traffic sources set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k] set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k] set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k] #Start logging the received bandwidth $ns at 0.0 quot;recordquot; #Start the traffic sources $ns at 10.0 quot;$source0 startquot; $ns at 10.0 quot;$source1 startquot; $ns at 10.0 quot;$source2 startquot; #Stop the traffic sources $ns at 50.0 quot;$source0 stopquot; $ns at 50.0 quot;$source1 stopquot; $ns at 50.0 quot;$source2 stopquot; #Call the finish procedure after 60 seconds simulation time $ns at 60.0 quot;finishquot; #Run the simulation $ns run 41
  • 42. Chapter 5 ตัวอยางการใชงาน NS2 NS Script ตัวอยางนี้เปนการจําลองการทํางานของระบบเครือขายที่มี Node ภายในเครือขาย 5 node ซึ่งมี source อยู 2 node คือ n0, n1 และ destination อยู 2 node คือ n3, n4 ซึ่ง node n0 มีการสรางการเชื่อมตอโดย ใช TCP พรอมทั้งกําหนดใหใช Application เปน FTP โดยกําหนดให node n3 เปน destination ของการ เชื่อมตอ สวน node n1 มีการสรางการเชื่อมตอโดยใช UDP พรอมทั้งกําหนดใหใช Application เปน CBR โดยกําหนดให node n4 เปน destination ของการเชื่อมตอ พรอมทั้งยังเก็บ tracefile ไวในไฟลช่อ traceout.tr ื เพื่อที่จะไฟลดงกลาวมาใชในการสรางกราฟเพื่อวิเคราะหการทํางานของ TCP และ UDP ั set ns [new Simulator] set tf [open traceout.tr w] $ns trace-all $tf set nf [open out2.nam w] $ns namtrace-all $nf $ns color 1 RED $ns color 2 BLUE set n0 [$ns node] set n1 [$ns node] set r1 [$ns node] set r2 [$ns node] set n3 [$ns node] set n4 [$ns node] $ns duplex-link $n0 $r1 2Mb 2ms DropTail $ns duplex-link $n1 $r1 2Mb 2ms DropTail $ns duplex-link $r1 $r2 1Mb 2ms RED $ns duplex-link $n3 $r2 2Mb 2ms DropTail $ns duplex-link $n4 $r2 2Mb 2ms DropTail 42
  • 43. $ns duplex-link-op $n0 $r1 orient right-down $ns duplex-link-op $n1 $r1 orient right-up $ns duplex-link-op $r1 $r2 orient right $ns duplex-link-op $r2 $n3 orient right-up $ns duplex-link-op $r2 $n4 orient right-down $ns queue-limit $r1 $r2 10 $ns queue-limit $r2 $r1 10 set tcp_src [new Agent/TCP] $ns attach-agent $n0 $tcp_src set tcp_des [new Agent/TCPSink] $ns attach-agent $n3 $tcp_des $ns connect $tcp_src $tcp_des $tcp_src set fid_ 1 $tcp_src set packetSize_ 552 set ftp [new Application/FTP] $ftp attach-agent $tcp_src $ftp set type_ FTP #Setup a UDP connection set udp_src [new Agent/UDP] $ns attach-agent $n1 $udp_src set udp_des [new Agent/Null] $ns attach-agent $n4 $udp_des $ns connect $udp_src $udp_des $udp_src set fid_ 2 #Setup CBR set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp_src 43
  • 44. $cbr set packetSize_ 1000 #$cbr set rate_ 0.01Mb $cbr set random_ false $ns at 0.1 quot;$ftp startquot; $ns at 1.0 quot;$cbr startquot; $ns at 120.0 quot;$ftp stopquot; $ns at 120.5 quot;$cbr stopquot; $ns at 121.0 quot;finishquot; #Tracing a queue set redq [[$ns link $r1 $r2] queue] set tchan_ [open all.q w] $redq trace curq_ $redq trace ave_ $redq attach $tchan_ proc finish {} { global ns nf tchan_ tf set awkCode { { if($1 ==quot;Qquot; && NF>2) { print $2, $3 >> quot;temp.qquot;; set end $2 } else if($1 ==quot;aquot; && NF>2) print $2, $3 >> quot;temp.aquot;; } } set f [open temp.queue w] if { [info exists tchan_] } { 44
  • 45. close $tchan_ } exec rm -f temp.q temp.a exec touch temp.q temp.a exec awk $awkCode all.q puts $f quot;queue exec cat temp.q >@ $f puts $f nquot;avg_queue exec cat temp.a >@ $f close $f $ns flush-trace close $tf close $nf exec xgraph -x time -y queue temp.queue & exec nam out2.nam & exit 0 } $ns run 45
  • 46. ภาพที่ 5.1 แสดง Topology ที่จําลองการทํางานของระบบเครือขาย Xgraph ที่ไดเปนกราฟที่แสดงความสัมพันธระหวางเวลาและqueue length ซึ่งจากกราฟจะแสดง เสนกราฟที่เปรียบเทียบระหวาง Current queue length และ Average queue length เมื่อตองการที่จะสรางกราฟที่แสดงความสัมพันธระหวาง throughput และ เวลาจะตองมีการคํานวณคา Average Throughput ซึ่งคาดังกลาวไดมาจากการคํานวณ packet size โดยคาของ packet size ถูกนํามาจาก tracefile ซึ่งจากการทดลองดังกลาวใช tracefile ที่มีชื่อวา traceout.tr ซึ่งการที่จะนําคา packet size ใน tracefile มาคํานวณในทีนี้ใชการเขียน script เพื่อที่จะดึงคาใน tracefile มาคํานวณซึ่งภาษาทีใชเขียน script ่ ่ คือภาษา perl ในที่นี้จะวิเคราะหการทํางานของ throughput ซึ่งจะวิเคราะหทั้ง Average throughput ของทั้ง Received และ Dropped โดยมองทั้งรูปแบบการทํางานของ TCP และ UDP ซึ่งการวิเคราะหที่ไดแบง ออกเปน 4 รูปแบบดังนี้ 1. Averaged Received Throughput of TCP 2. Averaged Received Throughput of CBR 3. Averaged Dropped Throughput of TCP 4. Averaged Dropped Throughput of CBR 46
  • 47. ซึ่งเมื่อมีการกําหนดเงื่อนไขที่ใชใน perl script ตรงตามที่ตองการแลวก็ทําการสราง output file ที่ไดจาก perl script นี้เพื่อที่จะนํา output file ที่ไดมาแสดงเปนกราฟโดยใช gnuplot ซึ่งรูปแบบคําสั่งที่ใชรัน perl script มี ดังนี้ perl <perl script> <trace file> <required node> <granularity> > Output file เมื่อตองการที่จะรัน perl script โดยตองการใหโหนดที่ 2 เปน required node และมี perl script ชื่อ throughput.pl พรอมทั้งมี tracefile ชื่อ traceout.tr และไดกําหนดให granularity มีคาเปน 1 รวมทั้งตั้งชื่อ output file วา TCP จะตองใชรูปแบบคําสั่งดังนี้ perl throughput.pl traceout.tr 2 1 > TCP เมื่อได output file ที่ชื่อวา TCP ใหนํา output file ดังกลาวมาเขียนกราฟโดยใช gnuplot เชน gnuplot set size 1,1 set key 60,15000 plot ‘TCP’ w lines 1 1. Averaged Received Throughput of TCP คําสั่งรัน perl script ที่วิเคราะห Averaged Received Throughput ของ TCP โดยพิจารณา throughput ที่ออก จากโหนดที่ 4 perl throughput.pl traceout.tr 4 1 > TCP Perl Script : throughput.pl $infile=$ARGV[0]; #Argument0 $tonode=$ARGV[1]; #Argument1 $granularity=$ARGV[2]; #Argument2 #we compute how many bytes were transmitted during time interval specified #by granularity parameter in seconds $sum =0; $clock=0; open (DATA,quot;<$infilequot;)||die quot;Can't open $infile $!quot;; while (<DATA>){ @x=split(' '); #column1 is time 47
  • 48. if ($x[1]-$clock<=$granularity) { #checking if the event corresponds to a reception if($x[0] eq 'r') { #checking if the destination corresponds to 1st argument if($x[3] eq $tonode) { #checking if the packet type is TCP if($x[4] eq 'tcp') { $sum=$sum+$x[5]; } } } } else { $throughput=$sum/$granularity; print STDOUT quot;$x[1] $throughputnquot;; $clock=$clock+$granularity; $sum=0; } } $throughput=$sum/$granularity; print STDOUT quot;$x[1] $throughputnquot;; $clock=$clock+$granularity; $sum=0; close DATA; exit(0); 48