ns3 网络模拟器

ns-3是一个用于 Internet 系统的离散事件网络模拟器,主要用于研究和教育用途。ns-3 是免费的开源软件,在 GNU GPLv2 许可下获得许可,并由全球社区维护。

bilibili视频

第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 behavior 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. ns3 学习资源

2.1 Web

  • Official Website: https://www.nsnam.org
  • Official Documents Webpage: http://www.nsnam.org/documentation
  • Official Wiki: https://www.nsnam.org/wiki
  • Source Code: https://gitlab.com/nsnam

2.2 git

2018年12月ns-3的源码管理工具从Mercurial迁移到了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://pythor.org

Linux中使用Gnu toolchain:

  • GNU C++编译器: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. ns3 入门

官方文档:https://www.nsnam.org/docs/release/3.35/tutorial/html/getting-started.html

3.1 ns3 的安装(Ubuntu20.04)

3.1.1 先决条件

安装 ns3 之前,必须先安装部分软件包:
包括但不限于 c++、Python、git等,详见https://www.nsnam.org/wiki/Installation#Ubuntu.2FDebian.2FMint

进入链接后,按照步骤一步一步安装依赖包即可。

3.1.2 下载 ns3

  1. 下载 Allinone 源码包
    # wget http://www.nsnam.org/release/ns-allinone-3.33.tar.bz2
    # tar xjf ns-allinone-3.33.tar.bz2
    # cd ns-allinone-3.33
    bake      constants.py   ns-3.30           README
    build.py  netanim-3.108  pybindgen-0.20.0  util.py
    
    • 构建 ns3

      第一次构建 ns-3 项目时,您应该使用 allinone 环境进行构建。这将以最常用的方式为您配置项目。
      进入目录中找到 build.py 文件,使用以下命令构建 ns3

      ./build.py
      
  2. 使用 Git 下载源码
  3. 使用 Bake 下载源码

3.2 测试 ns3 安装

cd ns-allinone-3.33/ns-3.33
./test.py

3.3 运行网络模拟脚本

root@qiao-virtual-machine:/home/qiao/桌面/ns-allinone-3.35/ns-3.35# ./waf --run hello-simulator
Waf: Entering directory `/home/qiao/桌面/ns-allinone-3.35/ns-3.35/build'
Waf: Leaving directory `/home/qiao/桌面/ns-allinone-3.35/ns-3.35/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (0.849s)
Hello Simulator

不编译直接运行脚本:

root@qiao-virtual-machine:/home/qiao/桌面/ns-allinone-3.35/ns-3.35# ./waf --run hello-simulator
Waf: Entering directory `/home/qiao/桌面/ns-allinone-3.35/ns-3.35/build'
Hello Simulator

向模拟脚本中传递命令行参数

./waf --run <ns 3-program> --command-template="%s <args>"

./waf --run 'test-runner --suite=mytest --verbose'
./waf --run test-runner --command-template="%s --help"

4. ns3 对网络组件的抽象

节点:Node

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

应用程序:Application

  • 应用程序 Application 是运行在 Node 内部的用户软件
  • ns3 中有很多 Application 类的子类
    • UdbEchoClientApplication
    • UdbEchoServerApplication

信道:Channel

  • 信道Channel:数据传输的通道,有线、无线...

    • 现实世界:网卡 <-------------> 双绞线
    • 模拟世界:Node <--------------> Channel
  • channel 类提供相应的方法来管理通信子网对象,将节点连接到通信子网中。

    • A Channel specialization may model somethings as simple as a wire.有线线路
    • The specialized Channel can also model things as complicated as a large Etherent switch.以太网交换机
    • or three-dimensional space full obstruction in the case of wireless nwetworks.无线网络中充满障碍的三维空间
  • 常见的 Channel:

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

网络设备:Net Device

  • ns3 中 net device 抽象包括两个部分:
    • software driver:设备驱动程序
    • simulated hardware
  • net device 被安装到 Node 中,使当前 Node 能够和其他 Node 构建网络(Channel)
  • Just as in a real computer ,a Node may be connected to more than one Channel via multiple NetDevices.(就像在真实的计算机中一样,一个节点可以通过多个网络设备连接到多个通道。)

拓扑辅助类:Topology Helpers

为什么需要 Helper 类 ?
核心:简化网络拓扑配置,将那些机械的重复性的配置操作用 Helper 实现 !

In a large simulated network you will need to arrange many connections between Nodes, NetDevices and Channels.

Since connecting NetDevices to Nodes, NetDevices to Channels, assigning IP addresses, etc., are such common tasks in ns-3, we provide what we call topology helpers to make this as easy as possible.

For example, it may take many distinct ns-3 core operations to create a NetDevice, add a MAC address, install that net device on a Node, configure the node’s protocol stack, and then connect the NetDevice to a Channel. Even more operations would be required to connect multiple devices onto multipoint channels and then to connect individual networks together into internetworks.

We provide topology helper objects that combine those many distinct operations into an easy to use model for your convenience.

在大型模拟网络中,您需要在节点、网络设备和通道之间安排许多连接。

由于将 NetDevice 连接到节点、NetDevice 连接到通道、分配 IP 地址等是 ns-3 中的常见任务,因此我们提供了所谓的拓扑帮助器来使这一切尽可能简单。

例如,可能需要许多不同的 ns-3 核心操作来创建 NetDevice、添加 MAC 地址、在节点上安装该网络设备、配置节点的协议栈,然后将 NetDevice 连接到通道。将多个设备连接到多点通道,然后将各个网络连接到互联网络中,还需要更多的操作。

为了您的方便,我们提供了拓扑帮助对象,这些对象将许多不同的操作组合成一个易于使用的模型。