C++ 如何在NS-3中使用多路径路由

C++ 如何在NS-3中使用多路径路由,c++,routing,ns-3,C++,Routing,Ns 3,我是NS-3的初学者,我想通过NS-3在我的网络中配置多路径路由,下面是代码: #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 "n

我是NS-3的初学者,我想通过NS-3在我的网络中配置多路径路由,下面是代码:

#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"

using namespace ns3;
using namespace std;

NS_LOG_COMPONENT_DEFINE ("TestRoutingExample");

int main(int argc, char *argv[]) 
{
    LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
    LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

    Ptr<Node> n0 = CreateObject<Node>();
    Ptr<Node> n1 = CreateObject<Node>();
    Ptr<Node> n2 = CreateObject<Node>();
    Ptr<Node> n3 = CreateObject<Node>();
    Ptr<Node> n4 = CreateObject<Node>();

    Names::Add("n0", n0);
    Names::Add("n1", n1);
    Names::Add("n2", n2);
    Names::Add("n3", n3);
    Names::Add("n4", n4);

    NodeContainer n0n1(n0, n1);
    NodeContainer n1n2(n1, n2);
    NodeContainer n1n3(n1, n3);
    NodeContainer n3n4(n3, n4);
    NodeContainer n2n4(n2, n4);

    NodeContainer global(n0, n1, n2, n3, n4);

    // create link
    PointToPointHelper p2p;
    p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
    p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
    NetDeviceContainer d0d1 = p2p.Install(n0n1);
    NetDeviceContainer d1d2 = p2p.Install(n1n2);
    NetDeviceContainer d1d3 = p2p.Install(n1n3);
    NetDeviceContainer d2d4 = p2p.Install(n2n4);
    NetDeviceContainer d3d4 = p2p.Install(n3n4);
    // create internet stack
    InternetStackHelper internet;
    internet.Install (global);

    Ipv4AddressHelper ipv4;

    ipv4.SetBase ("10.0.0.0", "255.255.255.0");
    Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);

    ipv4.SetBase ("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);

    ipv4.SetBase ("10.2.2.0", "255.255.255.0");
    Ipv4InterfaceContainer i1i3 = ipv4.Assign (d1d3);

    ipv4.SetBase ("10.3.3.0", "255.255.255.0");
    Ipv4InterfaceContainer i2i4 = ipv4.Assign (d2d4);

    ipv4.SetBase ("10.4.4.0", "255.255.255.0");
    Ipv4InterfaceContainer i3i4 = ipv4.Assign (d3d4);


    Config::SetDefault("ns3::Ipv4GlobalRouting::RandomEcmpRouting",     BooleanValue(true)); // enable multi-path routing
    Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

    // install application
    UdpEchoServerHelper echoServer1(9999);
    ApplicationContainer serverApps1 = echoServer1.Install (n4);
    serverApps1.Start (Seconds (1.0));
    serverApps1.Stop (Seconds (1000.0));

    // n0 -> n4
    UdpEchoClientHelper echoClient1(i2i4.GetAddress (1), 9999);
    echoClient1.SetAttribute ("MaxPackets", UintegerValue (200));
    echoClient1.SetAttribute ("Interval", TimeValue (Seconds (1)));
    echoClient1.SetAttribute ("PacketSize", UintegerValue (1024));
    ApplicationContainer clientApps1 = echoClient1.Install (n0);
    clientApps1.Start (Seconds (2.0));
    clientApps1.Stop (Seconds (10.0));

    // dump config
    p2p.EnablePcapAll ("test");

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

    return 0;
}
#包括“ns3/core module.h”
#包括“ns3/网络模块.h”
#包括“ns3/csma模块.h”
#包括“ns3/internet模块.h”
#包括“ns3/点对点模块.h”
#包括“ns3/applications module.h”
#包括“ns3/ipv4全局路由助手.h”
使用名称空间ns3;
使用名称空间std;
NS_日志_组件_定义(“测试路由示例”);
int main(int argc,char*argv[])
{
LogComponentEnable(“UdpEchoClientApplication”,日志级别信息);
LogComponentEnable(“UdpEchoServerApplication”,日志级别信息);
Ptr n0=CreateObject();
Ptr n1=CreateObject();
Ptr n2=CreateObject();
Ptr n3=CreateObject();
Ptr n4=CreateObject();
名称::添加(“n0”,n0);
名称::添加(“n1”,n1);
名称:添加(“n2”,n2);
名称:添加(“n3”,n3);
名称::添加(“n4”,n4);
节点容器n0n1(n0,n1);
节点容器n1n2(n1,n2);
节点容器n1n3(n1,n3);
节点容器n3n4(n3,n4);
节点容器n2n4(n2,n4);
节点容器全局(n0、n1、n2、n3、n4);
//创建链接
点拓扑;
p2p.SetDeviceAttribute(“数据速率”,StringValue(“5Mbps”);
p2p.SetChannelAttribute(“延迟”,StringValue(“2ms”);
NetDeviceContainer d0d1=p2p.Install(n0n1);
NetDeviceContainer d1d2=p2p.Install(n1n2);
NetDeviceContainer d1d3=p2p.Install(n1n3);
NetDeviceContainer d2d4=p2p.Install(n2n4);
NetDeviceContainer d3d4=p2p.Install(n3n4);
//创建internet堆栈
互联网;
安装(全球);
ipv4-ipv4;
ipv4.SetBase(“10.0.0.0”、“255.255.255.0”);
Ipv4InterfaceContainer i0i1=ipv4.Assign(d0d1);
ipv4.SetBase(“10.1.1.0”、“255.255.255.0”);
Ipv4InterfaceContainer i1i2=ipv4.Assign(d1d2);
ipv4.SetBase(“10.2.2.0”、“255.255.255.0”);
Ipv4InterfaceContainer i1i3=ipv4.Assign(d1d3);
ipv4.SetBase(“10.3.3.0”、“255.255.255.0”);
Ipv4InterfaceContainer i2i4=ipv4.Assign(d2d4);
ipv4.SetBase(“10.4.4.0”、“255.255.255.0”);
Ipv4InterfaceContainer i3i4=ipv4.Assign(d3d4);
Config::SetDefault(“ns3::Ipv4GlobalRouting::RandomEcmpRouting”,BooleanValue(true));//启用多路径路由
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
//安装应用程序
UdpEchoServerHelper-echoServer1(9999);
ApplicationContainer serverApps1=echoServer1.Install(n4);
serverApps1.Start(秒(1.0));
serverApps1.Stop(秒(1000.0));
//n0->n4
UdpEchoClientHelper echoClient1(i2i4.GetAddress(1),9999);
echoClient1.SetAttribute(“MaxPackets”,uintegValue(200));
echoClient1.SetAttribute(“间隔”,时间值(秒(1));
echoClient1.SetAttribute(“PacketSize”,UintegerValue(1024));
ApplicationContainerClientApps1=echoClient1.Install(n0);
clientApps1.启动(秒(2.0));
clientApps1.停止(秒(10.0));
//转储配置
p2p.EnablePcapAll(“测试”);
模拟器::Run();
模拟器::Destroy();
返回0;
}
拓扑结构如下所示:

n0将定期向n4发送UDP数据包,但我发现所有数据包都采用以下路径:n0→n1→氮气→n4通过检查.pcap文件,了解原因?

查看示例。基本上,您可以通过启用:

RespondToInterfaceEvents:如果希望在发生接口通知事件(向上/向下或添加/删除地址)时动态重新计算全局路由,请设置为true

以及关闭节点n1或n4上的一个接口(因为在您的情况下,它们都有两个接口)。所以它应该是这样的:

Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
// then the next p2p is numbered 2
uint32_t ipv4ifIndex1 = 1; // since you first installed d1d2

Simulator::Schedule (Seconds(4), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
Simulator::Schedule (Seconds(6), &Ipv4::SetUp, ipv41, ipv4ifIndex1);    
ptripv41=n1->GetObject();
//对于环回,第一个ifIndex为0,然后第一个p2p编号为1,
//然后下一个p2p编号为2
uint32\u t IPv4iFindX1=1;//自从您第一次安装d1d2以来
模拟器::调度(秒(4)和Ipv4::设置、ipv41、IPV4iIndex1);
模拟器::调度(秒(6)和Ipv4::设置、ipv41、IPV4iIndex1);
按照这种方法,您可以扩展它并随机触发设置和设置事件。我希望这有帮助



更新:如果你想使用
randomecmrouting
你必须在顶部移动
SetDefault
调用(在你创建界面之前)。

首先感谢你的建议。有没有其他不破坏接口的解决方案?为什么我无法在代码中启用ECMP路由@incBrain@knshen我不确定。我知道也可以使用界面上的
SetMetric
更改度量。但不幸的是,我只能找到
randomecmrouting
的例子。你也可以尝试在那里搜索或提问,我相信你会在那里得到答案。还有一件事,在创建接口之前,您是否尝试移动顶部的
RandomEcmpRouting
?是的,您是对的。当移动顶部的
randomecmrouting
时,它工作。
Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
// then the next p2p is numbered 2
uint32_t ipv4ifIndex1 = 1; // since you first installed d1d2

Simulator::Schedule (Seconds(4), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
Simulator::Schedule (Seconds(6), &Ipv4::SetUp, ipv41, ipv4ifIndex1);