티스토리 뷰

Development/ETC

[NS3] CSMA example

KimDoubleB 2021. 1. 23. 00:20
반응형

Network Simulator 3

네트워크 시뮬레이터(Network Simulator)는 가상의 네트워크 상황을 만들어 네트워크의 동작과 성능 및 영향을 측정하고 분석하는 장치 및 소프트웨어를 의미한다. 다양한 소프트웨어들이 있으나, 대표적으로 NS-2, NS-3가 있다. 이 글에서는 NS-3의 한 예제에 다루고자 한다.

 

만약 NS-3에 대해 더 공부해보고 싶다면, 아래 문서들을 참조하자.

 

 

Problem

NS3를 사용해 다음 그림의 Topology를 구현해보자.

  • LAN은 CSMA를 사용할 것
  • UDP echo로 n0 -> n4

 

Logging

NS_LOG_COMPONENT_DEFINE("s5_ex1");

...

if(verbose){
    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
        LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
  • UdpEchoClientApplication/UdpEchoServerApplication Logging 활성화

 

Create Nodes

NodeContainer p2pNodes;
p2pNodes.Create(2);

NodeContainer csmaNodes;
csmaNodes.Add(p2pNodes.Get(1)); // Connect one nodes which is connected by p2p link
csmaNodes.Create(nCsma);
  • p2p로 연결되는 노드, csma(LAN)으로 연결되는 노드를 따로 정의한다.
  • p2p 노드에서 하나의 노드를 csmaNode에 추가. topology에 맞게 구현한다.

 

p2p link

PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
p2p.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer p2pDevices;
p2pDevices = p2p.Install(p2pNodes);
  • DataRate: 5Mbps, Delay: 2ms
  • 바로 노드들을 연결하여 NetDevice (NIC)를 가져온다.

 

CSMA(LAN) link

CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
  • DataRate: 100Mbps, Delay: 6560ns
  • p2p link와 마찬가지로 노드들을 연결하고 NetDevice를 가져온다

 

Internet Stack

InternetStackHelper stack;
stack.Install(p2pNodes.Get(0));
stack.Install(csmaNodes);
  • 각 노드에 Internet Stack 설치
  • p2pNodes(1)은 csmaNodes에 있으므로, p2pNodes(0)만 따로 설치한다.

 

Set IP

// set p2p IP address
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign(p2pDevices);

// set csma IP address
address.SetBase("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign(csmaDevices);
  • IPv4 할당
  • SetBase(IP address, subnet mask)를 통해 IP주소 생성
  • Assign(NetDevice)를 통해 IP주소 할당
  • Topology에 맞게 IP 분리하여 생성 !

Application

UdpEchoServer

UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
  • UDP Echo Server 정의
    • Port = 9
    • 받은 모든 패킷에 대해 똑같이 응답
  • Csma node에 할당한다.
  • 1초부터 10초까지 작동한다.

 

UdpEchoClient

UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));

ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
  • UDP Echo Client 정의
    • UDP Echo Server와 연결
    • Max Packet = 1
    • Interval = 1 sec
    • Packet Size = 1024
  • p2p node(0)에 할당한다.
  • 2초부터 10초까지 작동한다.
  • Attribute list는 여기를 참조

 

Routing

Ipv4GlobalRoutingHelper::PopulateRoutingTables();
  • IPv4에서 수행되는 Routing protocol
  • Simulation의 routing database를 구성하고, 노드들의 routing table을 초기화한다.

 

Pcap

p2p.EnablePcapAll("s5_ex1");
csma.EnablePcap("s5_ex1", csmaDevices.Get(1), true);
  • pcap분석을 위해 pcap 생성 활성화

 

Simulator Run & Destory

Simulator::Run();
Simulator::Destroy();
  • Simulation을 수행
  • Simulation이 끝나면, 생성된 객체 제거

 


 

All code


#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4
//    point-to-point  |    |    |    |
//                    ================
//                      LAN 10.1.2.0

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("s5_ex1");

int
main(int argc, char* argv[]){

    bool verbose = true;
    uint32_t nCsma = 3;

    // Define commandLine input
    CommandLine cmd;
    cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
    cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
    cmd.Parse(argc, argv);

    if(verbose){
        LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
        LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

    nCsma = nCsma == 0 ? 1 : nCsma;

    // Create nodes (p2p/csma)
    // p2p - client // csma - Server
    NodeContainer p2pNodes;
    p2pNodes.Create(2);

    NodeContainer csmaNodes;
    csmaNodes.Add(p2pNodes.Get(1)); // Connect one nodes which is connected by p2p link
    csmaNodes.Create(nCsma);

    // p2p link
    PointToPointHelper p2p;
    p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
    p2p.SetChannelAttribute("Delay", StringValue("2ms"));
    NetDeviceContainer p2pDevices;
    p2pDevices = p2p.Install(p2pNodes);

    // csma link
    CsmaHelper csma;
    csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
    csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
    NetDeviceContainer csmaDevices;
    csmaDevices = csma.Install(csmaNodes);

    // Install internet stack and assign IP address
    InternetStackHelper stack;
    stack.Install(p2pNodes.Get(0));
    stack.Install(csmaNodes);

    // set p2p IP address
    Ipv4AddressHelper address;
    address.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer p2pInterfaces;
    p2pInterfaces = address.Assign(p2pDevices);

    // set csma IP address
    address.SetBase("10.1.2.0", "255.255.255.0");
    Ipv4InterfaceContainer csmaInterfaces;
    csmaInterfaces = address.Assign(csmaDevices);


    // UDP Server - csmaNodes(nCsma)
    UdpEchoServerHelper echoServer(9);
    ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma));
    serverApps.Start(Seconds(1.0));
    serverApps.Stop(Seconds(10.0));


    // UDP client - p2pNodes(0)
    UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
    echoClient.SetAttribute("MaxPackets", UintegerValue(1));
    echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
    echoClient.SetAttribute("PacketSize", UintegerValue(1024));

    ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0));
    clientApps.Start(Seconds(2.0));
    clientApps.Stop(Seconds(10.0));


    // activate Routing table
    Ipv4GlobalRoutingHelper::PopulateRoutingTables();


    // Enable Pcap (p2p/csma)
    p2p.EnablePcapAll("s5_ex1");
    csma.EnablePcap("s5_ex1", csmaDevices.Get(1), true);

    Simulator::Run();
    Simulator::Destroy();

    return 0;
}

320x100
반응형
댓글
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함