개인 공부

NS-3 독학 - 2. Second.cc

Beige00 2024. 1. 11. 17:04

NS-3 example 코드들의 2번째, Second.cc를 분석해보겠다.

 

 

Second.cc Topology

토폴로지를 살펴보면, n0와 n1은 first.cc와 같이 구성되어있다.

다만, n1~n4는 10.1.2.0의 IP를 가진 LAN에 있다.

int
main(int argc, char* argv[])
{
    bool verbose = true;
    uint32_t nCsma = 3;

    CommandLine cmd(__FILE__);
    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;

    NodeContainer p2pNodes;
    p2pNodes.Create(2);

    NodeContainer csmaNodes;
    csmaNodes.Add(p2pNodes.Get(1));
    csmaNodes.Create(nCsma);

    PointToPointHelper pointToPoint;
    pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
    pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));

    NetDeviceContainer p2pDevices;
    p2pDevices = pointToPoint.Install(p2pNodes);

    CsmaHelper csma;
    csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
    csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));

    NetDeviceContainer csmaDevices;
    csmaDevices = csma.Install(csmaNodes);

    InternetStackHelper stack;
    stack.Install(p2pNodes.Get(0));
    stack.Install(csmaNodes);

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

    address.SetBase("10.1.2.0", "255.255.255.0");
    Ipv4InterfaceContainer csmaInterfaces;
    csmaInterfaces = address.Assign(csmaDevices);

    UdpEchoServerHelper echoServer(9);

    ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma));
    serverApps.Start(Seconds(1.0));
    serverApps.Stop(Seconds(10.0));

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

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

    Ipv4GlobalRoutingHelper::PopulateRoutingTables();

    pointToPoint.EnablePcapAll("second");
    csma.EnablePcap("second", csmaDevices.Get(1), true);

    Simulator::Run();
    Simulator::Destroy();
    return 0;
}

 

이는 Second.cc의 전문이다.

1. bool verbose = true; uint32_t nCsma = 3; :

아직은 어디에 쓰일지 모르겠지만, 변수를 만들어준다.

 

2. CommandLine cmd(__FILE__); :

cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);

cmd.AddValue("verbose", "Tell echo applications to log if true", verbose); :

현재 변수 상태를 CommandLine에 출력한다.

 

3. cmd.Parse(argc, argv); :

커맨드 라인에서 전달된 인자들을 파싱하여 프로그램에 적용한다.

 

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

verbose가 true일 시, 각 Udp model의 Log 레벨을 INFO로 설정해주어 Log를 기록한다.

 

5. nCsma = nCsma == 0 ? 1 : nCsma; :

nCsma가 0일 시 1을, 아닐 시 nCsma를 저장해준다.(여기서 미루어보아 nCsma는 LAN에 존재할 node의 갯수인 것 같다.)

 

6. NodeContainer p2pNodes; p2pNodes.Create(2); :토폴로지에 따라 point to point node들을 만들어준다. (n0, n1)

 

7. NodeContainer csmaNodes; csmaNodes.Add(p2pNodes.Get(1)); csmaNodes.Create(nCsma); :

LAN에 들어갈 노드들을 만들어준다. 단, 여기서 n0은 이미 p2pNode로 만들어줬기에, 단순히 NodeContainer에 추가만 해준다.

 

8. PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));

pointToPoint.SetChannelAttribute("Delay", StringValue("2ms")); :

p2p link의 attribute를 설정해준다.

 

9. NetDeviceContainer p2pDevices;

p2pDevices = pointToPoint.Install(p2pNodes); :

first.cc 에서 했던 접근과 같이 p2p 연결이 된 노드들에게 helper로 만들어둔 p2p link를 설치한다.

 

10. CsmaHelper csma;

csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));

c

sma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560))); :

LAN 환경을 구성해준다. DataRate은 100 Mbps, Delay는 6560 nano second로 초기화해줬다.

 

11. NetDeviceContainer csmaDevices;

csmaDevices = csma.Install(csmaNodes); :

설정해준 channel을 csmaNodes들의 node들에게 설치해준다.

 

12. InternetStackHelper stack;

stack.Install(p2pNodes.Get(0));

stack.Install(csmaNodes); :

first.cc에서와 같이 p2p node들에게 internet 프로토콜을 설치한다. (UDP client, UDP server가 될 것임.)

 

13. Ipv4AddressHelper address;

address.SetBase("10.1.1.0", "255.255.255.0");

Ipv4InterfaceContainer p2pInterfaces;

p2pInterfaces = address.Assign(p2pDevices); :

이 또한 first.cc에서와 같이 Ipv4 address setting을 해주고, p2p 연결에 적용시켜준다.

 

14. address.SetBase("10.1.2.0", "255.255.255.0");

Ipv4InterfaceContainer csmaInterfaces;

csmaInterfaces = address.Assign(csmaDevices); :

Topology 구성 예제에 따라 LAN IP는 10.1.2.0 으로 준다. 

(csmaDevice들은 10.1.2.1, 10.1.2.2, 10.1.2.3, 10.1.2.4 가 될 것이다.)

 

15. UdpEchoServerHelper echoServer(9);

ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(10.0)); :

UdpEchoServer를 Port 9에서 돌아가도록 만들어주고, LAN상 의 마지막 node에 설치해준다.  (1~10초간 존재)

더보기
Second.cc Topology

이 시점에서 해당 토폴로지가 구현이 완료되었으며, n4가 UdpEchoServer의 역할을 해줄 것이다.

16. UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);

echoClient.SetAttribute("MaxPackets", UintegerValue(1));

echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));

echoClient.SetAttribute("PacketSize", UintegerValue(1024)); :

Server의 구성이 끝났으니 Client를 구성해주어야한다. UdpEchoClient 들은 LAN의 IP와 Port num 9를 향해 통신할 것이다. (n4)각각 패킷은 1번씩만 보낼 것이며 간격은 1초, PacketSize는 1024 바이트이다.

 

17. ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0));

clientApps.Start(Seconds(2.0));

clientApps.Stop(Seconds(10.0)); :

구성된 UDPEchoClient를 p2p node 연결의 첫번째 노드(n0)에 설치한다. (2~10초간 존재)

 

18. Ipv4GlobalRoutingHelper::PopulateRoutingTables(); :

Ipv4GlobalRoutingHelper 클래스에서 제공하는 함수로써, 시뮬레이션 환경에서 각 노드의 라우팅 테이블을 초기화하고 구성한다. (노드 초기 설정)

=> 라우팅이 가능해진다. First.cc와 다르게 라우터에 연결된 LAN 연결을 가정하므로 필요해진 함수이다.

 

19. pointToPoint.EnablePcapAll("second");

csma.EnablePcap("second", csmaDevices.Get(1), true); :

P2P network의 모든 노드(n0, n1)에 대해 second라는 캡처 파일을 생성한다.

또한 second에는 LAN에 존재하는 node 중 2번째 노드에 수신된 모든 패킷을 캡처한다.

 


* 실행 결과

cline가 LAN에 존재하는 server node 4(10.1.2.4)에게 패킷을 보낸다.

-> 서버는 client로 지정된 n0(10.1.1.1)에게 패킷을 받았다.

-> server는 다시 n0에게 reply packet을 보낸다.

-> clinet는 server로부터 packet을 받았다.

 

그럼 이제 패킷 캡처를 봐보자.

저장 이름 규칙은 <name>-<node>-<device>.pcap 이다.

pcap 생성

 

더 자세히 알아보기 위해 tcpdump -nn -tt -r 로 뜯어보겠다.

0-0, 1-0의 경우, 2초에 자기가 node 4 echo server로 송신, reply를 수신한 기록이 남아있다.

(p2p link)

second-2-0

반면, second-2-0의 경우 LAN link이다. (라우터의 개입)

그래서 ARP를 사용하는 모습을 볼 수 있다.

n0은 10.1.2.4로 패킷을 쏘기 전에,  broadcast로 (ff:ff:ff:ff...) 10.1.2.4 정보를 아는 노드를 찾는다.

(여기선 연결되어있는 노드가 p2p link n1 밖에 없기 때문에 사실상 n1에게만 정보가 간다.)

n1은 n4가 속해있는 LAN에 연결이 되어있고, 라우팅 테이블에 해당 정보가 있다.

따라서 n1은 00:00:00:00:00:06(MAC 주소) 에 10.1.2.4가 있다고 Reply가 온다.

그 후 n0은 10.1.2.4.9로 패킷을 쏜다.

(반대로 서버에서 10.1.1.1로 갈 때도 똑같다.)

따라서 n0 to n4 는 직접 연결이 되어있지 않아 ARP 를 통해 n1에게 n4의 MAC 주소를 받고 Packet을 주고받는 것이다.

 

더보기

! 햇갈렸던 점

패킷캡처를 하면 자신이 존재하는 네트워크 세그먼트에서 발생하는 모든 패킷이 캡처 된다.

따라서 second-0-0, second-1-0은 node 0의 device 0을 조회하고 node 1의 device 0을 조회한 것이다.

이는 p2p link를 의미하고, 여기에는 당연히 'node0이 node4 에게 데이터를 보냄, node 0이 node 4에게 데이터를 받음.'이 찍혀있을 수 밖에 없다.

 

여기서 만약에 다음과 같이 입력하고 수행하면 0-0, 1-0, 2-0외에 어떤 pcap 파일이 추가로 생성될까?


    pointToPoint.EnablePcapAll("second");
    csma.EnablePcap("second", csmaDevices.Get(0), true);

 (Get(1)로 n2의 패킷을 캡처하던게 Get(0)으로 n1의 패킷을 캡처하게 되었다.)

 

=> 정답은 second-1-1.pcap이 생성된다. 왜냐하면 Pcap을 csma에서 찍었기 때문이다.

n1은 p2p, csma 2가지 device이다.

따라서 1-0은 p2p로써 n1을, 1-1은 csma로써 n1을 가리키는 것이다.

이 말이 맞다면, second-1-0을 깟을때는 p2p link의 패킷들이 캡처되었으므로 second-1-1은 csma(LAN)의 패킷들이 캡처가 되어야할 것이다.

이해한 내용이 맞은 것을 확인할 수 있다.

여기서 추가로 하나를 더 검증해보겠다.

만약 csma.EnablePcap("second",csmaDevices.Get(3), true); 로 패킷캡처를 찍게되면,

second-4-0 이 생기고 여기에는 LAN의 패킷들이 찍혀있어야한다.

'개인 공부' 카테고리의 다른 글

NS-3 독학 - 5.fifth.cc  (0) 2024.01.15
NS-3 독학 - 4.fourth.cc(Tracing)  (0) 2024.01.13
NS-3 독학 - 3.Third.cc  (0) 2024.01.12
NS-3 독학 - 1. First.cc  (0) 2024.01.11
NS-3 독학 - 0. 설치하기  (0) 2024.01.10