250313 NS-3 安装

开始用Ubuntu 20.04 server,但是发现g++版本是9.4,不够NS-3的最低要求

根据这个回答
https://askubuntu.com/questions/1500553/how-to-install-g12-on-ubuntu-20-04-6
最好还是直接升级到ubuntu 22.04然后升级g++

因为之后可能需要可视化网络拓扑,所以server还得装上桌面,感觉不如一开始直接安装ubuntu desktop版本。看网上视频教程有推荐用server的,不过没说原因。

总之系统搞定了就要装依赖和软件了。很多NS-3安装教程都有些年头了,但是从3.36版本开始,NS-3的构建工具改成了CMake,之前的安装教程都不适用了。

不过还是找到了一个新一点的教程:
https://youtu.be/BpfF-ZaFSJ8?si=5lnVLBHkV7eYMTWS

  1. 先安装一些依赖,虽然不一定都用得上。
1
$ sudo apt install g++ python3 cmake ninja-build git gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 tcpdump wireshark sqlite sqlite3 libsqlite3-dev qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools openmpi-bin openmpi-common openmpi-doc libopenmpi-dev doxygen graphviz imagemagick python3-sphinx dia imagemagick texlive dvipng latexmk texlive-extra-utils texlive-latex-extra texlive-font-utils libeigen3-dev gsl-bin libgsl-dev libgslcblas0 libxml2 libxml2-dev libgtk-3-dev lxc-utils lxc-templates vtun uml-utilities ebtables bridge-utils libxml2 libxml2-dev libboost-all-dev ccache

这之后其实跟着官方教程走就行
https://www.nsnam.org/docs/tutorial/html/quick-start.html

  1. 下载NS-3的release版本,也就是官网上的tar包版本。

解压之后的目录下类似这样

1
2
~/ns-allinone-3.43$ ls
bake build.py constants.py netanim-3.109 ns-3.43 README.md util.py
  1. 配置构建系统

进入ns-3.43目录,可以看到类似内容

1
2
3
4
5
~/ns-allinone-3.43/ns-3.43$ ls
AUTHORS build-support CMakeLists.txt doc LICENSES pyproject.toml scratch src utils
bindings CHANGES.md contrib examples ns3 README.md setup.cfg test.py utils.py
build cmake-cache CONTRIBUTING.md LICENSE __pycache__ RELEASE_NOTES.md setup.py testpy-output VERSION

配置构建系统

1
$ ./ns3 configure --enable-examples --enable-tests
  1. build

这一步可能要花很久

1
$ ./ns3 build
  1. 运行测试

这一步也可能要花很久,这一步不是必须的,只是检查编译完的程序有没有问题

1
$ ./test.py
  1. 运行例程
1
$ ./ns3 run first

如果成功运行可以看到类似如下输出

1
2
3
4
5
6
[0/2] Re-checking globbed directories...
ninja: no work to do.
At time +2s client sent 1024 bytes to 10.1.1.2 port 9
At time +2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time +2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time +2.00737s client received 1024 bytes from 10.1.1.2 port 9

more about build

如果切换了build profile,在重新build之前最好先clean一下。但是实测完整的build需要花二三十分钟,当然这跟处理器性能有关。

1
$ ./ns3 clean

build profile

build有几种profile,debug,default(default好像就是debug),release,optimized。

Feature debug default release optimized
Enabled Features NS3_BUILD_PROFILE_DEBUG NS3_BUILD_PROFILE_DEBUG NS3_BUILD_PROFILE_RELEASE NS3_BUILD_PROFILE_OPTIMIZED
NS_LOG... NS_ASSERT... NS_LOG... NS_ASSERT...
Code Wrapper Macro NS_BUILD_DEBUG(code) NS_BUILD_DEBUG(code) NS_BUILD_RELEASE(code) NS_BUILD_OPTIMIZED(code)
Compiler Flags -Og -g -Os -g -O3 -O3 -march=native -mtune=native

官方也建议在开发的时候先用debug,之后重复跑模拟再用optimized。

Recommended practice is to develop your scenario in debug mode, then conduct repetitive runs (for statistics or changing parameters) in optimized build profile.

1
$ ./ns3 configure --build-profile=debug --enable-examples --enable-tests

可以对不同的profile设置不同的output文件夹,这样的话切换profile不用重新编译所有文件了。

1
2
3
4
5
6
$ ./ns3 configure --build-profile=debug --out=build/debug
$ ./ns3 build
...
$ ./ns3 configure --build-profile=optimized --out=build/optimized
$ ./ns3 build
...