NS3搭建网络拓扑图

//
// Network topology
//
//  n0
//     \ p-p
//      \          (shared csma/cd)      p-p
//       n2 -------------------------n3--------n7
//      /            |        | 
//     / p-p        n4--n8    n5 ---------- n6
//   n1                              p-p
//
// - CBR/UDP flows from n0 to n6
// - Tracing of queues and packet receptions to file "mixed-global-routing.tr"

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

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

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("MixedGlobalRoutingExample");

int 
main (int argc, char *argv[])
{
  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("448kb/s"));

  // Allow the user to override any of the defaults and the above
  // Bind ()s at run-time, via command-line arguments
  CommandLine cmd;
  cmd.Parse (argc, argv);

  NS_LOG_INFO ("Create nodes.");
  NodeContainer c;
  c.Create (9);
  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
  NodeContainer n3n7 = NodeContainer (c.Get (3), c.Get (7));
  NodeContainer n4n8 = NodeContainer (c.Get (4), c.Get (8));
  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));

  InternetStackHelper internet;
  internet.Install (c);

  // We create the channels first without any IP addressing information
  NS_LOG_INFO ("Create channels.");
  PointToPointHelper p2p;
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
  NetDeviceContainer d0d2 = p2p.Install (n0n2);

  NetDeviceContainer d1d2 = p2p.Install (n1n2);

  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
  NetDeviceContainer d5d6 = p2p.Install (n5n6);
  
  NetDeviceContainer d3d7 = p2p.Install (n3n7);
  
  NetDeviceContainer d4d8 = p2p.Install (n4n8);

  // We create the channels first without any IP addressing information
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
  NetDeviceContainer d2345 = csma.Install (n2345);

  // Later, we add IP addresses.
  NS_LOG_INFO ("Assign IP Addresses.");
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  ipv4.Assign (d0d2);

  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
  ipv4.Assign (d1d2);

  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);

  ipv4.SetBase ("10.1.4.0", "255.255.255.0");
  Ipv4InterfaceContainer i3i7 = ipv4.Assign (d3d7);

  ipv4.SetBase ("10.1.5.0", "255.255.255.0");
  Ipv4InterfaceContainer i4i8 = ipv4.Assign (d4d8);

  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
  ipv4.Assign (d2345);

  // Create router nodes, initialize routing database and set up the routing
  // tables in the nodes.
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  // Create the OnOff application to send UDP datagrams of size
  // 210 bytes at a rate of 448 Kb/s
  NS_LOG_INFO ("Create Applications.");
  uint16_t port = 9;   // Discard port (RFC 863)
  OnOffHelper onoff ("ns3::UdpSocketFactory",
                     InetSocketAddress (i5i6.GetAddress (1), port));
  onoff.SetConstantRate (DataRate ("300bps"));
  onoff.SetAttribute ("PacketSize", UintegerValue (50));

  ApplicationContainer apps = onoff.Install (c.Get (0));
  apps.Start (Seconds (1.0));
  apps.Stop (Seconds (10.0));

  AsciiTraceHelper ascii;
  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("mixed-global-routing.tr");
  p2p.EnableAsciiAll (stream);
  csma.EnableAsciiAll (stream);

  p2p.EnablePcapAll ("mixed-global-routing");
  csma.EnablePcapAll ("mixed-global-routing", false);

  NS_LOG_INFO ("Run Simulation.");
  Simulator::Run ();
  Simulator::Destroy ();
  NS_LOG_INFO ("Done.");
}

参考NS3源码,初步搭建了一个网络拓扑结构,可以添加子网。

执行  ./waf --run test  --vis

将输出网络拓扑结构
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
NS-3是一个网络仿真器,可以通过编写程序来创建拓扑图。以下是一个简单的NS-3程序,用于创建一个包含3个节点和2个链路的拓扑图: ``` #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("MyTopology"); int main (int argc, char *argv[]) { LogComponentEnable ("MyTopology", LOG_LEVEL_INFO); NodeContainer nodes; nodes.Create (3); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer devices; devices = csma.Install (nodes); Ptr<CsmaNetDevice> dev0 = DynamicCast<CsmaNetDevice> (devices.Get (0)); Ptr<CsmaNetDevice> dev1 = DynamicCast<CsmaNetDevice> (devices.Get (1)); Ptr<CsmaNetDevice> dev2 = DynamicCast<CsmaNetDevice> (devices.Get (2)); dev0->SetAddress (Mac48Address::Allocate ()); dev1->SetAddress (Mac48Address::Allocate ()); dev2->SetAddress (Mac48Address::Allocate ()); Simulator::Run (); Simulator::Destroy (); return 0; } ``` 在这个程序中,我们首先定义了三个节点,然后使用`CsmaHelper`类创建了一个包含3个节点的CSMA网络。我们为链路设置了数据速率和延迟,并将设备分配给节点。最后,我们为每个设备分配了一个唯一的MAC地址,并启动了仿真器。 要编译和运行这个程序,需要安装NS-3,并使用以下命令: ``` $] ./waf --run "my-topology" ``` 这将编译并运行我们创建的`my-topology.cc`文件。在NS-3仿真器中,您可以使用类似Wireshark的工具来查看网络流量和分组传输情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值