TiiL Tutorials
@TinhocTiiL

NS3 – Bài 3- Sử dụng NS3 Topology Generator

NS-3 topology generator cung cấp cho chúng ta cách thức nhanh chóng và dễ dàng trong tạo mã mô phỏng NS3.

Khi sử dụng công cụ này, thông qua giao diện đồ họa, chúng ta có thể thiết kế kịch bản mô phỏng của mình và tạo ra mã (sinh mã script) tương ứng chỉ thông qua tao tác trực quan với chuột. Ta có thể thêm các nodes, links, applications, và tạo ra mã mô phỏng C++/Python cho mô phỏng NS3.

Tải dự án NS3 Topology_Generator

– Link github: https://github.com/idaholab/Topology_Generator

Cài đặt thư viện yêu cầu cần thiết

  • Qt4 libraries
sudo apt update
sudo apt install qt4-default
  • Thêm module hỗ trợ:  canberra-gtk-module
sudo apt install libcanberra-gtk-module

CÀI ĐẶT

– Xả nén file github tải về vào một thưc mục;

– Vào thư mục này, mở Termimal, Chạy lệnh   qmake ,  và  make

THỰC THI

– Vào thư mục này, mở Termial, chạy lệnh:  ./ns-3-generator

THỬ NGHIỆM MỘT VÍ DỤ

– Tạo topo mạng gồm 2 node, kết nối trực tiếp với nhau bằng một liên kế có dây, sử dụng ứng dụng ping

  • + Sử dụng chuột để kéo thiết bị (2 máy tính vào, và chỉnh vị trí),
  • + Chọn liên kết có dây (wired link) sau đó nháy chuột vào lần lượt 2 máy để kết nối chúng.
  • + Chọn Application để chọn loại ứng dụng, chọn nút gửi, nút nhận
  • + Chọn thời gian bắt đầu mô phỏng và kết thúc mô phỏng

– Sinh mã

Menu  Generat C++, hoặc Python để sinh mã mô phỏng bằng ngôn ngữ tương ứng

Khi sinh mã, ta cần chọn nơi lưu tệp thuận tiện để chạy mô phỏng sau này, ví dụ: thư mục Scratch trong NS3-35

Hiệu chỉnh mã,

– Mở tệp và soạn thảo bình thường.

MÃ SINH BẰNG c++  theo topo trên

#include "ns3/core-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/bridge-module.h"
#include "ns3/csma-module.h"

using namespace ns3;

int main(int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);

  /* Configuration. */

  /* Build nodes. */
  NodeContainer term_0;
  term_0.Create (1);
  NodeContainer term_1;
  term_1.Create (1);

  /* Build link. */
  CsmaHelper csma_hub_0;
  csma_hub_0.SetChannelAttribute ("DataRate", DataRateValue (100000000));
  csma_hub_0.SetChannelAttribute ("Delay",  TimeValue (MilliSeconds (10000)));

  /* Build link net device container. */
  NodeContainer all_hub_0;
  all_hub_0.Add (term_0);
  all_hub_0.Add (term_1);
  NetDeviceContainer ndc_hub_0 = csma_hub_0.Install (all_hub_0);

  /* Install the IP stack. */
  InternetStackHelper internetStackH;
  internetStackH.Install (term_0);
  internetStackH.Install (term_1);

  /* IP assign. */
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
  Ipv4InterfaceContainer iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0);

  /* Generate Route. */
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  /* Generate Application. */

  /* Simulation. */
  /* Pcap output. */
  /* Stop the simulation after x seconds. */
  uint32_t stopTime = 1;
  Simulator::Stop (Seconds (stopTime));
  /* Start and clean simulation. */
  Simulator::Run ();
  Simulator::Destroy ();
}

MÃ SINH BẰNG python  theo topo trên

import ns3

def main(argv):

    cmd = ns3.CommandLine()
    cmd.Parse (argv)

    # Configuration.

    # Build nodes
    term_0 = ns3.NodeContainer()
    term_0.Create (1)
    term_1 = ns3.NodeContainer()
    term_1.Create (1)

    # Build link.
    csma_hub_0 = ns3.CsmaHelper()
    csma_hub_0.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(100000000)))
    csma_hub_0.SetChannelAttribute("Delay",  ns3.TimeValue(ns3.MilliSeconds(10000)))

    # Build link net device container.
    all_hub_0 = ns3.NodeContainer()
    all_hub_0.Add (term_0)
    all_hub_0.Add (term_1)
    ndc_hub_0 = csma_hub_0.Install(all_hub_0)

    # Install the IP stack.
    internetStackH = ns3.InternetStackHelper()
    internetStackH.Install (term_0)
    internetStackH.Install (term_1)

    # IP assign.
    ipv4 = ns3.Ipv4AddressHelper()
    ipv4.SetBase (ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.0"))
    iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0)

    # Generate Route.
    ns3.Ipv4GlobalRoutingHelper.PopulateRoutingTables ()

    # Generate Application.

    # Simulation.
    # Pcap output.
    # Stop the simulation after x seconds.
    stopTime = 1
    ns3.Simulator.Stop (ns3.Seconds(stopTime))
    # Start and clean simulation.
    ns3.Simulator.Run()
    ns3.Simulator.Destroy()

if __name__ == '__main__':
    import sys
    main(sys.argv)

 

Avatar
https://khoacntt.ntu.edu.vn/giang-vien/mai-cuong-tho

một GV Đại học. TiiL đã phụ trách một số môn học như: Lập trình Java, Phát triển web với Java, Lập trình thiết bị di động, Lập trình hệ thống nhúng và IoT.

Comments are closed.