做了两个NS3仿真实验
分别是关于csma和ripv2的
在此记录一下
读者可参考
不可照抄剽窃
如有建议
欢迎交流
原理就不做介绍了,网上搜搜就有
实验目的是
直接上代码
// Network topology
//
// Lan1
// ================
// | | | |
// n0 n1 n2 n3 n4 n5 n6
// | | | |
// ================
// Lan0
// n0是源头,在n6收数据,每个节点抓包
#include
#include
#include
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("CsmaMulticastExample");
int main(int argc, char *argv[])
{
LogComponentEnable("CsmaMulticastExample", LOG_LEVEL_INFO);
Config::SetDefault("ns3::CsmaNetDevice::EncapsulationMode", StringValue("Dix")); //封装
CommandLine cmd;
cmd.Parse(argc, argv);
// 创建节点
NS_LOG_INFO("Create nodes.");
NodeContainer c;
c.Create(7);
NodeContainer c0 = NodeContainer(c.Get (0), c.Get (1), c.Get (2), c.Get (3));
NodeContainer c1 = NodeContainer(c.Get (3), c.Get (4), c.Get (5), c.Get (6));
// 网络拓扑
NS_LOG_INFO("Build Topology.");
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
NetDeviceContainer nd0 = csma.Install(c0);
NetDeviceContainer nd1 = csma.Install(c1);
// 协议栈
NS_LOG_INFO("Add IP Stack.");
InternetStackHelper internet;
internet.Install(c);
// IP地址
NS_LOG_INFO("Assign IP Addresses.");
Ipv4AddressHelper ipv4Addr;
ipv4Addr.SetBase("10.1.1.0", "255.255.255.0");
ipv4Addr.Assign(nd0);
ipv4Addr.SetBase("10.1.2.0", "255.255.255.0");
ipv4Addr.Assign(nd1);
Ipv4Address multicastSource("10.1.1.1");
Ipv4Address multicastGroup("225.1.2.4");
// 路由
NS_LOG_INFO("Configure multicasting.");
Ipv4StaticRoutingHelper multicast;
Ptr<Node> multicastRouter = c.Get(3);
Ptr<NetDevice> inputIf = nd0.Get(3);
NetDeviceContainer outputDevices;
outputDevices.Add(nd1.Get(0));
multicast.AddMulticastRoute(multicastRouter, multicastSource, multicastGroup, inputIf, outputDevices);
Ptr<Node> sender = c.Get(0);
Ptr<NetDevice> senderIf = nd0.Get(0);
multicast.SetDefaultMulticastRoute (sender, senderIf);
//Ptr sender1 = c.Get(3);
//Ptr senderIf1 = nd1.Get(3);
//multicast.SetDefaultMulticastRoute (sender, senderIf);
// 应用
NS_LOG_INFO("Create Applications.");
uint16_t multicastPort = 9;
OnOffHelper onoff("ns3::UdpSocketFactory", Address(InetSocketAddress(multicastGroup, multicastPort)));
onoff.SetConstantRate(DataRate("255b/s"));
onoff.SetAttribute("PacketSize", UintegerValue(128));
ApplicationContainer app = onoff.Install(c0.Get(0));
app.Start(Seconds (1.));
app.Stop(Seconds (10.));
PacketSinkHelper sink("ns3::UdpSocketFactory",
InetSocketAddress(Ipv4Address::GetAny(), multicastPort));
ApplicationContainer sinkC = sink.Install(c1.Get(3));
sinkC.Start(Seconds (1.0));
sinkC.Stop(Seconds (12.0));
// 跟踪
NS_LOG_INFO("Configure Tracing.");
AsciiTraceHelper ascii;
csma.EnableAsciiAll(ascii.CreateFileStream("csma.tr"));
csma.EnablePcapAll("csma", true);
// 启动
NS_LOG_INFO("Run Simulation.");
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
}
在跟踪里设置了trace文件
通过trace文件可以计算吞吐量S
修改发包速率不断进行实验
RIP是最基本的路由协议,也不多做介绍了
实验目的是
同样直接上代码
// 网络拓扑
// SRC
// |<=== source network
// A------C
// / \ |
// / \ |
// B \ |
// \ \ |
// \ \|
// E------D
// |<=== target network
// DST
//
//
//
// BE之间 cost 10,其他 cost 1
// ABCDE是 RIP router,src 和 dst交换数据包
// 程序运行190s,100s时断开DE
#include
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-routing-table-entry.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("RipRouting");
// 打断路由连接的函数
void TearDownLink(Ptr<Node> nodeA, Ptr<Node> nodeB, uint32_t interfaceA, uint32_t interfaceB)
{
nodeA->GetObject<Ipv4>()->SetDown(interfaceA);
nodeB->GetObject<Ipv4>()->SetDown(interfaceB);
}
int main(int argc, char** argv)
{
bool verbose = false;
bool printRoutingTables = true;
bool showPings = true;
std::string SplitHorizon("PoisonReverse");
CommandLine cmd;
cmd.AddValue("verbose", "turn on log components", verbose);
cmd.AddValue("printRoutingTables", "Print routing tables at 30, 60 and 90 seconds", printRoutingTables);
cmd.AddValue("showPings", "Show Ping6 reception", showPings);
cmd.AddValue("splitHorizonStrategy", "Split Horizon strategy to use (NoSplitHorizon, SplitHorizon, PoisonReverse)", SplitHorizon);
cmd.Parse(argc, argv);
if (verbose)
{
LogComponentEnableAll(LogLevel(LOG_PREFIX_TIME | LOG_PREFIX_NODE));
LogComponentEnable("RipSimpleRouting", LOG_LEVEL_INFO);
LogComponentEnable("Rip", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
LogComponentEnable("Icmpv4L4Protocol", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
LogComponentEnable("ArpCache", LOG_LEVEL_ALL);
LogComponentEnable("V4Ping", LOG_LEVEL_ALL);
}
if (SplitHorizon == "NoSplitHorizon")
{
Config::SetDefault("ns3::Rip::SplitHorizon", EnumValue(RipNg::NO_SPLIT_HORIZON));
}
else if (SplitHorizon == "SplitHorizon")
{
Config::SetDefault("ns3::Rip::SplitHorizon", EnumValue(RipNg::SPLIT_HORIZON));
}
else
{
Config::SetDefault("ns3::Rip::SplitHorizon", EnumValue(RipNg::POISON_REVERSE));
}
// 创建节点
NS_LOG_INFO("Create nodes.");
Ptr<Node> src = CreateObject<Node>();
Names::Add("SrcNode", src);
Ptr<Node> dst = CreateObject<Node>();
Names::Add("DstNode", dst);
Ptr<Node> a = CreateObject<Node>();
Names::Add("RouterA", a);
Ptr<Node> b = CreateObject<Node>();
Names::Add("RouterB", b);
Ptr<Node> c = CreateObject<Node>();
Names::Add("RouterC", c);
Ptr<Node> d = CreateObject<Node>();
Names::Add("RouterD", d);
Ptr<Node> e = CreateObject<Node>();
Names::Add("RouterE", e);
NodeContainer net1(src, a);
NodeContainer net2(a, b);
NodeContainer net3(b, e);
NodeContainer net4(a, c);
NodeContainer net5(c, d);
NodeContainer net6(d, e);
NodeContainer net7(a, d);
NodeContainer net8(e, dst);
NodeContainer routers(a, b, c, d, e);
NodeContainer nodes(src, dst);
// 创建拓扑
NS_LOG_INFO("Create channels.");
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
NetDeviceContainer ndc1 = csma.Install(net1);
NetDeviceContainer ndc2 = csma.Install(net2);
NetDeviceContainer ndc3 = csma.Install(net3);
NetDeviceContainer ndc4 = csma.Install(net4);
NetDeviceContainer ndc5 = csma.Install(net5);
NetDeviceContainer ndc6 = csma.Install(net6);
NetDeviceContainer ndc7 = csma.Install(net7);
NetDeviceContainer ndc8 = csma.Install(net8);
// 路由与IP
NS_LOG_INFO("Create IPv4 and routing");
RipHelper ripRouting;
ripRouting.ExcludeInterface(a, 1);
ripRouting.ExcludeInterface(e, 3);
ripRouting.SetInterfaceMetric(b, 3, 10);
ripRouting.SetInterfaceMetric(e, 1, 10);
Ipv4ListRoutingHelper listRH;
listRH.Add(ripRouting, 0);
NS_LOG_INFO("Add IP Stack.");
InternetStackHelper internet;
internet.SetIpv6StackInstall(false);
internet.SetRoutingHelper(listRH);
internet.Install(routers);
InternetStackHelper internetNodes;
internetNodes.SetIpv6StackInstall(false);
internetNodes.Install(nodes);
NS_LOG_INFO("Assign IPv4 Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase(Ipv4Address("10.0.0.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic1 = ipv4.Assign(ndc1);
ipv4.SetBase(Ipv4Address("10.0.1.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic2 = ipv4.Assign(ndc2);
ipv4.SetBase(Ipv4Address("10.0.2.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic3 = ipv4.Assign(ndc3);
ipv4.SetBase(Ipv4Address("10.0.3.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic4 = ipv4.Assign(ndc4);
ipv4.SetBase(Ipv4Address("10.0.4.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic5 = ipv4.Assign(ndc5);
ipv4.SetBase(Ipv4Address("10.0.5.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic6 = ipv4.Assign(ndc6);
ipv4.SetBase(Ipv4Address("10.0.6.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic7 = ipv4.Assign(ndc7);
ipv4.SetBase(Ipv4Address("10.0.7.0"), Ipv4Mask("255.255.255.0"));
Ipv4InterfaceContainer iic8 = ipv4.Assign(ndc8);
Ptr<Ipv4StaticRouting> staticRouting;
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting>(src->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.0.2", 1);
staticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting>(dst->GetObject<Ipv4>()->GetRoutingProtocol());
staticRouting->SetDefaultRoute("10.0.7.1", 1);
// 打印routing table
if (printRoutingTables)
{
RipHelper routingHelper;
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>(&std::cout);
// 设置打印时间
routingHelper.PrintRoutingTableAt(Seconds(1.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(1.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(1.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(1.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(1.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(2.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(2.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(2.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(2.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(2.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(3.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(3.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(3.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(3.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(3.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(4.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(5.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(5.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(5.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(5.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(5.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(6.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(6.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(6.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(6.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(6.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(7.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(7.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(7.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(7.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(7.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(8.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(8.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(8.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(8.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(8.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(9.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(9.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(9.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(9.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(9.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(10.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(99.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(99.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(99.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(99.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(99.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(100.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(100.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(100.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(100.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(100.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(101.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(101.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(101.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(101.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(101.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(102.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(102.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(102.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(102.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(102.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(103.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(103.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(103.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(103.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(103.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(104.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(104.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(104.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(104.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(104.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(105.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(105.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(105.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(105.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(105.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(106.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(106.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(106.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(106.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(106.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(107.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(107.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(107.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(107.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(107.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(108.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(108.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(108.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(108.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(108.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(109.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(109.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(109.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(109.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(109.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(110.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(110.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(110.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(110.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(110.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(111.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(111.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(111.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(111.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(111.0), e, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(112.0), a, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(112.0), b, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(112.0), c, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(112.0), d, routingStream);
routingHelper.PrintRoutingTableAt(Seconds(112.0), e, routingStream);
}
// 应用
NS_LOG_INFO("Create Applications.");
uint32_t packetSize = 1024;
Time interPacketInterval = Seconds(1.0);
V4PingHelper ping("10.0.7.2");
ping.SetAttribute("Interval", TimeValue(interPacketInterval));
ping.SetAttribute("Size", UintegerValue(packetSize));
if (showPings)
{
ping.SetAttribute("Verbose", BooleanValue(true));
}
ApplicationContainer apps = ping.Install(src);
apps.Start(Seconds(1.0));
apps.Stop(Seconds(190.0)); // 应用的时间
// 跟踪
NS_LOG_INFO("Configure Tracing.");
AsciiTraceHelper ascii;
csma.EnableAsciiAll(ascii.CreateFileStream("rip.tr"));
csma.EnablePcapAll("rip", true);
// 断开AD
Simulator::Schedule(Seconds(100), &TearDownLink, d, e, 3, 2);
// 启动
NS_LOG_INFO("Run Simulation.");
Simulator::Stop(Seconds(200.0));
Simulator::Run();
Simulator::Destroy();
NS_LOG_INFO("Done.");
}
通过打印得到的路由表
可以对RIP的收敛性和恢复能力做观察
也就做了这两个NS3仿真实验
读者可参考
不可照抄剽窃
如有建议或指教
欢迎交流