此篇为学习笔记,课程地址——>
1
2

1 引言

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不兼容!

1.1 关于ns-3

开发环境推荐:

  • OS: Linux or macOS,不推荐Windows OS (可在Windows 下使用vmware workstation或
    virtualbox虚拟化软件安装Linux)
  • Language: C++、Python
  • Command Line Terminal:虽然有动画、数据分析与可视化工具可用。绝大工作还是在终端命令行中完成。

1.2 for ns-2 users

ns-3允许在模拟最中执行现实世界中的代码。借助DCE (Direct Code Execution) 可以将整个Linux网络栈封装仅一个ns-3节点中。

1.3 为ns-3做贡献

ns-3网络模拟器提供了一个开放、可扩展的网络模拟平台,主要用于网络研究和教育目的。
简单来说:ns-3提供了很多模型(models),用于模拟分组数据网络如何工作和执行;提供了一个网络模拟引擎。
为什么要使用ns-3:

  • Include to perform studies that are more difficult or not possible to perform with real systems
  • to study system behavlor in a highly controlled
  • reproducible environment, and to learn about how networks work

大多数的ns-3模型用于建模Internet协议和网络是如何工作的。ns-3不局限于Internet系统。也有一些用户用于建模非Internet系统

现有的网络模拟/仿真工具:

  • OPNET
  • OMNET++
  • ns-2

2 资源

2.1 Web

  • Ofliclal Webste: https://www.nsnam.org
  • Officlal Documents Webpage: http://www.nsnam.org/documentation
    https://www.nsnam.org/documentation/development-tree/
  • Official Wiki: https://www.nsnam.org/wiki
  • Source Code: https://gitlab.com/nsnam

2.2 git

2018年12月ns-3的源码管理工具从Mer curial迁移到了Git

2.3 waf

ns-3源码构建系统采用waf (https://waf.io/book/) .
waf是基于Python语言的源码构建系统,与make的功能相同(make太过复杂)。
不用去深究waf的实现细节,会用即可!(用来编译运行脚本)

2.4开发环境

ns-3采用的编程语言: C++、Python

  • C++ Tutorial: http://www.cplusplus.com/doc/tutorial/
  • Python: http://python.org
    Linux中使用Gnu toolchain:
  • GNUC++编译器: gcc(g++)
  • GNU binutils
  • GNU gdb
  • ns-3没有使用GNU的构建系统,而使用waf构建系统
    macOS Toolchain:
  • Xcode

2.5 套接字编程

参考链接:

  • https://www.elsevier.com/books/tcp-ip-sockets-in-c/donahoo/978-0-12-374540-8
  • http://cs.baylor.edu/~donahoo/practical/CSockets/
  • https://www.elsevier.com/books/multicast-sockets/makofske/978-1-55860-846-7

3 ns核心概念

node类:表示主机、路由器
application类:表示网络应用程序,e.g. 客户端应用程序
channel类:表示信道
net device类:表示node上的网络通信设备及驱动程序,负责将node连接到channel。e.g. CsmaNetDevice, PointToPointNetDevice, WifiNetDevice
topology helper:把net device连接到node和channel上,并配置它们,e.g. 分配ip地址

3.1 节点 Node

  • ns3将基本的计算设备抽象为node(具体表现为node类)
  • node类提供了许多方法模拟计算设备在计算机网络中的行为
  • node的概念对应于host, end system…
  • 把node看作一台计算机(裸机),需要向计算机内添加软硬件功能部分:应用程序、协议栈、计算机外设等。

Class_Node_API

Class_NodeContainer_API

Class_NodeList_API

3.2 应用程序 Application

应用程序是运行在node内部的用户软件。
ns3中有许多application类的子类:

  • UdbEchoClientApplication
  • UdbEchoServerApplication

Class_Application_API

Class_ApplicationContainer_API

3.3 信道 Channel

信道是数据的传输通道,有线、无线…
the basic communication subnetwork abstraction is called the channel and is represented in C++ by the class Channel

  • 现实世界:网卡、双绞线
  • 虚拟世界 :node、channel

Channel类提供相应的方法来管理通信子网对象,将节点连接到通信子网中。

  • a channel specialization may model something as simple as a wire.可以模拟网线。
  • the specialized channel can also model things as complicated as a large Ethernet switch.可以模拟以太网交换机。
  • or three-dimensional space full of obstructions in the case of wireless networks.模拟无线网络中充满障碍的三维空间。

常见的channel:

  • CsmaChannel: 可以模拟实现scma(carrier sense multiple access)的通信子网
  • PointToPointChannel
  • WifiChannel

Class_Channel_API

3.4 网络设备 Net Device

(node与channel连接必须有net device,例如网卡)

ns-3中的网络设备抽象包括两部分:

  • software driver: 设备驱动程序
  • simulated hardware: 仿真硬件
    net device 被安装在node中,使当前node能够和其他node构建网络(借助channel)

Class_NetDevice_API

Class_NetDeviceContainer_API

3.5 拓扑辅助类 Topology Helpers

核心:简化网络拓扑配置,将那些机械的重复配置作用Helper实现。

Helpers示例(在官方文档的类索引中搜索Helper):

  • PointToPointHelper
  • InternetStackHelper
  • Ipv4AddressHelper
  • UdpEchoServerHelper
  • UdpEchoClientHelper

4 ns3安装

官方安装指南
视频安装指南
python版本问题
其他ns3安装教程
(tips: 如果后续要安装ns3-gym,建议此处安装n3-3.30版本)

5 第一个模拟脚本

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 outputedTime::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 nodesInternetStackHelper stack;stack.Install (nodes);//this is to set ip address: start from 10.1.1.0, subnet maskIpv4AddressHelper address;address.SetBase ("10.1.1.0", "255.255.255.0");//this is assign ip address to every devicesIpv4InterfaceContainer interfaces = address.Assign (devices);//finished setting bottom layer//------------------------------//start setting upper layer//set server's port=9UdpEchoServerHelper 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 timeApplicationContainer 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 nodeUdpEchoClientHelper 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;
}


second.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*/
// 运行指令,在ns-3.29下
// cp examples/tutorial/second.cc scratch/second.cc
// ./waf --run second
// ./waf --run second --vis # 可视化
// ./waf --run "second -- nCsma=100" --vis # 局域网中节点数3->100//1.头文件
#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"
//仿真拓扑图,有两种网络:p2p网络和CSMA网络,分别有2个和4个节点。其中n1上安装有两种NetDevice,i.e. p2p和csma。n0通过n1与n4通信。
// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4
//    point-to-point  |    |    |    |
//                    ================
//                      LAN 10.1.2.0//2.命名空间
using namespace ns3;//3.定义LOG模块
NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");//4.主函数
int
main (int argc, char *argv[])
{bool verbose = true;uint32_t nCsma = 3;//使用命令行声明nCsma变量CommandLine cmd;cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//添加命令行参数。nCsma变量表示,CSMA节点数目cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);cmd.Parse (argc,argv);//读取命令行参数if (verbose){LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);  //打印UdpEchoClientApplication组件信息LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); //打印UdpEchoServerApplication组件信息}nCsma = nCsma == 0 ? 1 : nCsma;//5.创建网络拓扑NodeContainer p2pNodes;p2pNodes.Create (2);//创建两个p2p型节点,n0----n1NodeContainer csmaNodes;csmaNodes.Add (p2pNodes.Get (1));//n1节点既是p2p节点,又是csma节点csmaNodes.Create (nCsma);//创建额外的nCsma个csma节点,n2,n3,n4PointToPointHelper pointToPoint;//p2p助手类,设置设备和信道属性pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//设置设备传输速率为5MbpspointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//设置信道延迟为2msNetDeviceContainer p2pDevices; //创建p2p网络设备p2pDevices = pointToPoint.Install (p2pNodes); //把p2p网络设备分别安装在节点n0和n1上,然后共同连接至同一信道对象CsmaHelper csma;//csma助手类,设置csma信道属性csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));//设置传输速率为100Mbpscsma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));//设置信道延迟为6560nsNetDeviceContainer csmaDevices;//创建csma网络设备csmaDevices = csma.Install (csmaNodes);//连接节点与信道//6.安装协议栈和分配ip地址InternetStackHelper stack;//为每个节点安装协议栈stack.Install (p2pNodes.Get (0));stack.Install (csmaNodes);Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0");//为p2p网络设备分配ip地址,起始地址为10.1.1.0,终止地址为10.1.1.254Ipv4InterfaceContainer p2pInterfaces;p2pInterfaces = address.Assign (p2pDevices);address.SetBase ("10.1.2.0", "255.255.255.0");//为csma网络设备分配ip地址,起始地址为10.1.2.0,终止地址为10.1.2.254Ipv4InterfaceContainer csmaInterfaces;csmaInterfaces = address.Assign (csmaDevices);//7.安装应用程序UdpEchoServerHelper echoServer (9);//监听9号端口//配置服务端属性ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));//使用Install方法将echoServer安装在节点n4(n3?)上,application在第1s开始运行并接受9号端口数据,在第10s结束。serverApps.Start (Seconds (1.0));serverApps.Stop (Seconds (10.0));//配置客户端属性UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//最大发送分组个数,1echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分组发送间隔1sechoClient.SetAttribute ("PacketSize", UintegerValue (1024));//数据包大小,1024bitApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));//使用install方法将echoClient安装在节点n0上。Application在模拟,第2s开始运行,并接受9号端口的数据,在第10s停止。clientApps.Start (Seconds (2.0));clientApps.Stop (Seconds (10.0));//8.设置全局路由Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//9.数据追踪pointToPoint.EnablePcapAll ("second");//EnablePcapAll (“second”)函数的作用是收集这个信道上所有结点链路层分组收发记录。记录文件格式是pcap,”second”是文件名前缀。csma.EnablePcap ("second", csmaDevices.Get (1), true);//记录了一个有线节点中CSMA网络设备的分组收发信息
//10.启动与结束Simulator::Run ();Simulator::Destroy ();return 0;
}

【网络仿真】ns-3安装与简介相关推荐

  1. ns2安装详细过程与网络仿真

    ns2安装详细过程与网络仿真 博客分类: Networks TclLinuxUnixGCCVC++ 简单的说,NS-2是一个网络模拟器,所以经常被用到网络课的教学中. NS-2是OpenSource的 ...

  2. OPNET网络仿真分析-1.1.1、网络仿真简介

    版权声明:本书为作者版权所有,仅用于学习,请勿商用 OPENT网络仿真分析 (作者:栾鹏.陈玓玏) OPNET网络仿真分析-目录 OPNET网络仿真分析-电子版 1.1.1.网络仿真简介 1.网络仿真 ...

  3. NS-3网络仿真平台搭建及可视化

    面向5G车联网仿真平台的搭建 本博客是基于SRTP(面向5G车联网仿真平台设计与开发)而搭建的"面向5G车联网的网络仿真平台",以供大家学习使用. 目录 前言概述 准备工作 下载n ...

  4. 网络仿真软件性能比较

    [导读]网络仿真技术是一种通过建立网络设备和网络链路的统计模型, 并模拟网络流量的传输, 从而获取网络设计或优化所需要的网络性能数据的仿真技术.由于仿真不是基于数学计算, 而是基于统计模型,因此,统计 ...

  5. Windows平台下NS2网络仿真环境的搭建

    NS2(Network Simulator 2) 是一种针对网络技术的源代码公开的.免费的软件模拟平台,研究人员使用它可以很容易的进行网络技术的开发,而且发展到今天,它所包含的模块几乎涉及到了网络技术 ...

  6. NS3网络仿真(6): 总线型网络

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载.但请保留作者信息 在NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模 ...

  7. NS3网络仿真项目(二)——入门指南

    官网给出NS3的资料主要分为以下几种: 1. NS3入手指南(Tutorial) 主要介绍如何下载.安装.以及简单的功能.实例程序讲解 2. NS3使用手册(Manual) 主要介绍NS3仿真器的结构 ...

  8. NS2网络仿真的过程

    NS2网络仿真的过程可以总结为: 1.初始化     创建ns simulator     创建.tr文件(记录仿真结果)     创建.nam文件(记录仿真过程)     设置结束函数     设置 ...

  9. gps网络对时Linux,gps网络时间对时服务器的功能简介

    gps网络时间对时服务器的功能简介 将通信局域网上各种通信设备或计算机设备的时间信息基于UCT时间偏 差限定在足够小的范围内,这种同步过程叫做网络时间同步. 有源同步和无源同步 任何时间应用系统都应该 ...

最新文章

  1. nodejs 截断字符串_node.js – nodejs:字符串操作
  2. 在 Linux 下运行 ASP.NET 2.0
  3. linux之循环执行任务
  4. dwt去噪 matlab,用matlab进行小波去噪的程序
  5. python 关键字大全_一日一技:用实例列举python中所有的关键字(01)
  6. while read line的问题
  7. java ee打印功能_Java EE:异步构造和功能
  8. 信息学奥赛一本通(1154:亲和数)
  9. argument ‘input‘ (position 1) must be Tensor, not XX
  10. 剑指offer之字符流中第一个不重复的字符
  11. Webpack 中 css import 使用 alias 相对路径
  12. magento 模板区块--商品总类别显示页
  13. 今天中午还收到了,一条诈骗短信,说是中奖了
  14. python定时任务_Python定时任务
  15. 消费贷之京东白条、蚂蚁花呗和银行信用卡的那些分期“低费率”套路
  16. MindMaster Pro 8.0.0 — 亿图思维导图
  17. CANoe.Diva之cdd文件配置
  18. Creator星球教程文章分类导航
  19. MapReduce 论文阅读笔记
  20. 假面舞会狂欢节·朗瀚威 | 艺术品化的meme:以传播促进流通

热门文章

  1. java.sql.SQLException: java.lang.RuntimeException: serious problem
  2. 智慧物流之RFID物流供应链仓库管理系统,智能化的仓库rfid管理-新导智能
  3. 软件行业进入“黑暗时代”,Bob大叔开启“整洁三部曲”,正式清理门户
  4. 【ASP.NET Web】项目实践—网上宠物店3:创建主页、主题
  5. 2020.2.15 要求:总结归纳计算机编程中的各种数据类型,其表示形式和计算方法。重点关注类型转换、数值溢出和移位等操作。
  6. java: 找不到符号 符号:类xxx位置: 程序包com.xxx.xxx.xxx.xxx
  7. STM32+LWIP服务器实现多客户端连接
  8. IF以及LIF神经分析
  9. TLinux:执行sudo apt install gcc提示“软件包gcc没有可安装候选”
  10. FLIR Systems获得美国陆军士兵随身传感器项目价值3960万美元的“黑黄蜂”个人侦察系统合同