Để làm quen với NS3 chúng ta sẽ thử nghiệm mô phỏng mô hình p2p với 2 node.
Cấu hình cài đặt
#include "ns3/applications-module.h" #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/network-module.h" #include "ns3/point-to-point-module.h" #include "ns3/netanim-module.h" #include "ns3/flow-monitor-module.h" using namespace ns3;
2. Tạo 2 nút với cấu hình speed 1Gbps ; delay 1ms
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("1Gbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("1ms"));
3. Cài đặt phương thức P2P cho 2 nodes
NetDeviceContainer devices; devices = pointToPoint.Install(nodes);
4. Cài đặt giao thức TCP/IP để cấu hình địa chỉ Ip cho 2 nodes
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
5. Cài đặt Application
Cài đặt Udp server cho node 1
UdpEchoServerHelper echoServer(9); ApplicationContainer serverApps = echoServer.Install(nodes.Get(1)); serverApps.Start(Seconds(1.0)); serverApps.Stop(Seconds(10.0));
Cài đặt Udp Client cho Node 0; cấu hình số packet gửi và kích thước packet
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(10));
echoClient.SetAttribute("Interval", TimeValue(Seconds(0.1)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
6. Cài đặt Flow Monitor để thu thập thông tin trên các node
Ptr flowmon; FlowMonitorHelper flowmonHelper; flowmon = flowmonHelper.InstallAll ();
7. Ghi nhận lại thông tin các gói và lưu thành file pcap
pointToPoint.EnablePcapAll("lab_1");
8. Tạo file mô phỏng XML để hiển thị trên NetAnim
AnimationInterface anim ("lab_1.xml");
anim.EnablePacketMetadata();
9. Chạy mô phỏng
Simulator::Stop(Seconds(11)); Simulator::Run();
10. Chuyển file flow monitor sang dạng XML
flowmon->SerializeToXmlFile ("lab1_1_flow.xml", true, true);
11. Giải phóng mô phỏng
Simulator::Destroy();
Full code
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("FirstScriptExample");
int
main(int argc, char* argv[])
{
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
Time::SetResolution(Time::NS);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("1Gbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("0.1ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(60.0));
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1000));
echoClient.SetAttribute("Interval", TimeValue(Seconds(0.1)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(60.0));
Ptr<FlowMonitor> flowmon;
FlowMonitorHelper flowmonHelper;
flowmon = flowmonHelper.InstallAll ();
pointToPoint.EnablePcapAll("lab_1");
AnimationInterface anim ("lab_1.xml");
anim.EnablePacketMetadata();
Simulator::Stop(Seconds(60));
Simulator::Run();
flowmon->SerializeToXmlFile ("lab1_1_flow.xml", true, true);
Simulator::Destroy();
return 0;
}
Chạy thử mô phỏng

Tại thư mục build sẽ sinh ra các file lưu thông tin mô phỏng

Chạy thử file lab_1-0-0.pcap trên Wireshark






