此篇为学习笔记,课程地址——>
1
2
The ns-3 simulator is a discrete-event network simulator targeted primarily for research and educational use.
ns-3模拟器: ns-3是一 款用于学术研究和教育用途的开放源代码验高散 事件模拟器,始于2006年。
我们将通过几个简单的模拟示例来窥探ns- 3的关键慨念和特征。
提示: ns 3是全新设计和实现的网络横拟器,它与ns-2不兼容!
开发环境推荐:
ns-3允许在模拟最中执行现实世界中的代码。借助DCE (Direct Code Execution) 可以将整个Linux网络栈封装仅一个ns-3节点中。
ns-3网络模拟器提供了一个开放、可扩展的网络模拟平台,主要用于网络研究和教育目的。
简单来说:ns-3提供了很多模型(models),用于模拟分组数据网络如何工作和执行;提供了一个网络模拟引擎。
为什么要使用ns-3:
大多数的ns-3模型用于建模Internet协议和网络是如何工作的。ns-3不局限于Internet系统。也有一些用户用于建模非Internet系统
现有的网络模拟/仿真工具:
2018年12月ns-3的源码管理工具从Mer curial
迁移到了Git
。
ns-3源码构建系统采用waf (https://waf.io/book/) .
waf是基于Python语言的源码构建系统,与make的功能相同(make太过复杂)。
不用去深究waf的实现细节,会用即可!(用来编译运行脚本)
ns-3采用的编程语言: C++、Python
参考链接:
node类:表示主机、路由器
application类:表示网络应用程序,e.g. 客户端应用程序
channel类:表示信道
net device类:表示node上的网络通信设备及驱动程序,负责将node连接到channel。e.g. CsmaNetDevice, PointToPointNetDevice, WifiNetDevice
topology helper:把net device连接到node和channel上,并配置它们,e.g. 分配ip地址
Class_Node_API
Class_NodeContainer_API
Class_NodeList_API
应用程序是运行在node内部的用户软件。
ns3中有许多application类的子类:
Class_Application_API
Class_ApplicationContainer_API
信道是数据的传输通道,有线、无线…
the basic communication subnetwork abstraction is called the channel and is represented in C++ by the class Channel
Channel类提供相应的方法来管理通信子网对象,将节点连接到通信子网中。
常见的channel:
Class_Channel_API
(node与channel连接必须有net device,例如网卡)
ns-3中的网络设备抽象包括两部分:
Class_NetDevice_API
Class_NetDeviceContainer_API
核心:简化网络拓扑配置,将那些机械的重复配置作用Helper实现。
Helpers示例(在官方文档的类索引中搜索Helper):
官方安装指南
视频安装指南
python版本问题
其他ns3安装教程
(tips: 如果后续要安装ns3-gym,建议此处安装n3-3.29版本)
github例子分析文档
在ns-3中启动vscode:(ctrl+alt+t)
#命令行输入命令
cd /home/jnbai/桌面/ns3/ns-allinone-3.33/ns-3.33
code --user-data-dir=/root/.vscode-root/ . #root用户
code . #普通用户
#在命令行中运行:(ns-3.33目录下)
sudo ./waf --run first
在vscode中运行:.cc文件要放在ns-3.33->scratch文件夹下,且用命令行已经运行过一次。
因此,以后实验编写的脚本(.cc文件)要放在ns-3.33/scratch下面。
第一个模拟脚本(在ns-3.33->examples->tutorial->first.cc中)
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//all header file is at build/ns3
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
//if is not at namespace ns3, add xx::
//e.g. Time::NS, std::cout, std::min()
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
//you can put into parameter at commandline
//e.g. sudo ./waf --run "hello-simulator --numbers=5"
CommandLine cmd;
cmd.Parse (argc, argv);
//this is to control time resolution unit (NS is millisecond)& how many log outputed
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//------------------------------------
//below is to build network topology
//create two empty nodes by nodecontainer, and putint it.
//you can get those nodes by nodes.Get (0) and nodes.Get (1)
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//this is use PointToPointHelper, install devices onto two nodes
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
//this is to install internet protocal into the nodes
InternetStackHelper stack;
stack.Install (nodes);
//this is to set ip address: start from 10.1.1.0, subnet mask
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
//this is assign ip address to every devices
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//finished setting bottom layer
//------------------------------
//start setting upper layer
//set server's port=9
UdpEchoServerHelper echoServer (9);
//ApplicationContainer is to log every node's application
//this is to install application, echoServer, into the node 1 ; set star time and end time
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
//interfaces.GetAddress (1)=ip address of server node; 9=port of server node
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//this is set for visualization (NetAnim)
AnimationInterface anim("first.xml");
anim.SetConstantPosition(nodes.Get(0), 1.0, 2.0);
anim.SetConstantPosition(nodes.Get(1), 2.0, 3.0);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}