本文主要参考NS-3的官方参考教程搭建NS-3网络仿真工具。

主要包括两个方面:

  1. 下载源码
  2. 编译并执行示例和测试程序。

依赖

用途工具最小版本号版本检查方式
下载gittarbunzip2无版本要求git --version
编译g++clang++g++>=10clang++11g++ --version
配置python3python3>=3.8python3 -V
构建cmakemakeninjacmake>=3.13cmake --verison

下载

wget源码下载:

wget https://www.nsnam.org/releases/ns-allinone-3.44.tar.bz2
tar xfj ns-allinone-3.44.tar.bz2
cd ns-allinone-3.44/ns-3.44

git源码下载

git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
git checkout -b ns-3.44-release ns-3.44

ns-3.44目录如下:

$ tree -L 1
.
├── AUTHORS
├── CHANGES.md
├── CMakeLists.txt
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── LICENSES
├── README.md
├── RELEASE_NOTES.md
├── VERSION
├── bindings
├── build-support
├── contrib
├── doc
├── examples
├── ns3
├── pyproject.toml
├── scratch
├── setup.cfg
├── setup.py
├── src
├── test.py
├── utils
└── utils.py

10 directories, 15 files

其中ns3是包装了cmake的python3脚本,用来进行编译。

构建和测试

下载完源码之后,使用ns3进行构建,ns3是对cmake进行封装的python3脚本。

配置模块:

./ns3 configure --enable-examples --enable-tests --prefix=~/usr

下面可以看到已经配置的模块:

-- ---- Summary of ns-3 settings:
Build profile                 : default
Build directory               : /home/blduan/open_source/ns-3/ns-allinone-3.44/ns-3.44/build
Build with runtime asserts    : ON
Build with runtime logging    : ON
Build version embedding       : OFF (not requested)
BRITE Integration             : OFF (Missing headers: "Brite.h" and missing libraries: "brite")
DES Metrics event collection  : OFF (not requested)
DPDK NetDevice                : OFF (not requested)
Emulation FdNetDevice         : ON
Examples                      : ON
File descriptor NetDevice     : ON
GNU Scientific Library (GSL)  : OFF (GSL was not found)
GtkConfigStore                : OFF (Harfbuzz is required by GTK3 and was not found)
LibXml2 support               : ON
MPI Support                   : OFF (not requested)
ns-3 Click Integration        : OFF (Missing headers: "simclick.h" and missing libraries: "nsclick click")
ns-3 OpenFlow Integration     : OFF (Missing headers: "openflow.h" and missing libraries: "openflow")
Netmap emulation FdNetDevice  : OFF (missing dependency)
PyViz visualizer              : OFF (Python Bindings are disabled)
Python Bindings               : OFF (not requested)
SQLite support                : OFF (Missing headers: "sqlite3.h" and missing libraries: "sqlite3")
Eigen3 support                : OFF (Eigen was not found)
Tap Bridge                    : ON
Tap FdNetDevice               : ON
Tests                         : ON

Modules configured to be built:
antenna                   aodv                      applications
bridge                    buildings                 config-store
core                      csma                      csma-layout
dsdv                      dsr                       energy
fd-net-device             flow-monitor              internet
internet-apps             lr-wpan                   lte
mesh                      mobility                  netanim
network                   nix-vector-routing        olsr
point-to-point            point-to-point-layout     propagation
sixlowpan                 spectrum                  stats
tap-bridge                test                      topology-read
traffic-control           uan                       virtual-net-device
wifi                      wimax                     zigbee


Modules that cannot be built:
brite                     click                     mpi
openflow                  visualizer

其中不能构建的模块是可选模块,需要外部依赖,暂时不影响使用所以可以不管它。

对已经配置的模块进行构建:

./ns3 build

构建时间依赖于CPU、内存以及构建模式(debug或默认优化模式等)。

构建完成之后就要对模块进行测试,ns-3.44版本目前具有790个测试项。

测试命令如下:

./test.py

模块测试完成之后会有如下日志:

# ....
790 of 790 tests passed (790 passed, 0 skipped, 0 failed, 0 crashed, 0 valgrind errors)

安装

可以通过在ns3 configure中设置--prefix=$PREFIX来指定安装目录,默认安装目录为/usr/local

$PREFIX/include为头文件目录、$PREFIX/lib为库文件目录、$PREFIX/libexec为可执行二进制文件目录。

注意:ns3脚本阻止以root权限运行,即使sudo也不行。因此想要安装到默认目录有两个选项:

  1. 注释掉ns3脚本中的refuse_run_as_root()语句
  2. 当前用户自身具备写入/usr/local目录的权限。

安装NS-3到指定目录:

./ns3 install