Ns is a discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks.
- 本文也可以在脱缰的哈士奇's 博客阅读
- 本文主要对NS2学习和使用过程中遇到的问题进行记录
- 本文主要参考了柯志亨的《NS2仿真实验》书籍
- The Network Simulator - ns-2
- 柯志亨《NS2仿真实验》
- 柯志亨《NS2仿真实验》书籍的源代码和NS2模块配置与修改可以参见Github-lbs0912
sudo apt-get update #更新源列表
sudo apt-get upgrade #更新已安装的包
sudo apt-get dist-upgrade #更新软件,升级系统
sudo apt-get install build-essential
sudo apt-get install tcl8.5 tcl8.5-dev tk8.5 tk8.5-dev #for tcl and tk
sudo apt-get install libxmu-dev libxmu-headers #for nam
Download NS-allinone and install it.
Ns-allinone is a package which contains equired components and some optional components used in running ns. The package contains an "install" script to automatically configure, compile and install these components. After downloading, run the install script. If you haven't installed ns before and want to quickly try ns out, ns-allinone may be easier than getting all the pieces by hand.
After downloading NS2 package, run the following code in terminal to unzip the package.
tar xvfz ns-allinone-2.35.tar.gz
Then copy these files to "Home/lbs/ProgramFiles" directory and run the following code to install NS2.
cd ns-allinone-2.35
./install #进行安装
Usually, you'll encounter some problems. Don't be afraid and just fix them. For example, I come across the following problem:
linkstate/ls.h:137:58: note: declarations in dependent base ‘std::map<int, LsIdSeq, std::less<int>, std::allocator<std::pair<const int, LsIdSeq> > >’ are not found by unqualified lookup
linkstate/ls.h:137:58: note: use ‘this->erase’ instead
make: *** [linkstate/ls.o] Error 1
Ns make failed!
See http://www.isi.edu/nsnam/ns/ns-problems.html for problems
If you encounter this problem, you just need to modify the ls.h
file in ns-2.35/linkstate
directory according to the error information.
Open ls.h
and find line 137(according to the error information), then modify
void eraseAll() { erase(baseMap::begin(), baseMap::end()); }
to
void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); }
After that, just run./install
again to install NS2. You can also use Google to help you solve problems.
If you install NS2 successfully, the terminal will appear the information:
Ns-allinone package has been installed successfully.
Here are the installation places:
tcl8.5.10: /home/lbs/ProgramFiles/ns-allinone-2.35/{bin,include,lib}
tk8.5.10: /home/lbs/ProgramFiles/ns-allinone-2.35/{bin,include,lib}
otcl: /home/lbs/ProgramFiles/ns-allinone-2.35/otcl-1.14
tclcl: /home/lbs/ProgramFiles/ns-allinone-2.35/tclcl-1.20
ns: /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/ns
nam: /home/lbs/ProgramFiles/ns-allinone-2.35/nam-1.15/nam
xgraph: /home/lbs/ProgramFiles/ns-allinone-2.35/xgraph-12.2
gt-itm: /home/lbs/ProgramFiles/ns-allinone-2.35/itm, edriver, sgb2alt, sgb2ns, sgb2comns, sgb2hierns
There are also some tips:
Ns-allinone package has been installed successfully.
Here are the installation places:
tcl8.5.10: /home/lbs/ProgramFiles/ns-allinone-2.35/{bin,include,lib}
tk8.5.10: /home/lbs/ProgramFiles/ns-allinone-2.35/{bin,include,lib}
otcl: /home/lbs/ProgramFiles/ns-allinone-2.35/otcl-1.14
tclcl: /home/lbs/ProgramFiles/ns-allinone-2.35/tclcl-1.20
ns: /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/ns
nam: /home/lbs/ProgramFiles/ns-allinone-2.35/nam-1.15/nam
xgraph: /home/lbs/ProgramFiles/ns-allinone-2.35/xgraph-12.2
gt-itm: /home/lbs/ProgramFiles/ns-allinone-2.35/itm, edriver, sgb2alt, sgb2ns, sgb2comns, sgb2hierns
We will use the tips in Step 4 to modify path environment.
Modify PATH
,LD_LIBRARY_PATH
,TCL_LIBRARY
path environment according to the terminal tips.
Open a new terminal window (or you need to change the directory to the initial directory in terminal) and run the following code.
sudo gedit .bashrc
Open .bashrc
file and add this setting to the end of the file.
export PATH=$PATH:/home/lbs/ProgramFiles/ns-allinone-2.35/bin:/home/lbs/ProgramFiles/ns-allinone-2.35/tcl8.5.10/unix:/home/lbs/ProgramFiles/ns-allinone-2.35/tk8.5.10/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lbs/ProgramFiles/ns-allinone-2.35/otcl-1.14:/home/lbs/ProgramFiles/ns-allinone-2.35/lib
export TCL_LIBRARY=$TCL_LIBRARY:/home/lbs/ProgramFiles/ns-allinone-2.35/tcl8.5.10/library
# 注意两个环境变量路径之间要用冒号:隔开
# Modify-lbs-20161103
At last, you should run
source .bashrc
to finish the modification of path environment.
To testify the installment, we can input ns
in terminal, and if the terminal return back %
, it shows that you have successfully installed NS2.
You can also use the following ways to testify the installment.
cd ns-allinone-2.35/ns-2.35/tcl/ex
ns example.tcl
If you run the code, the terminal will show you some data. Then, you can run the following code, the console window of nam and the help window of name will be opened.
ns simple.tcl
Open Cygwin website to install Cygwin.Note that by default Cygwin does not install all packages neccessary to run NS2. A user needs to manually install the addition packages shown in Table 1-1.
Table 1-1 Additional Cygwin packages required to run NS2
Category | Package |
---|---|
Development | gcc, gcc-objc, gcc-g++, make |
Utils | patch |
X11 | xorg-x11-base, xorg-x11-devel |
Others | gzip, tar, gnuplot, gawk,XFree86-base, XFree86-bin,XFree86-prog, XFree86-lib, XFree86-etc, perl |
You should also install packages whose prefix are diff in case you encounter some errors. Go to Reference 3 for more detail.
After the installment of Cygwin, please open Cygwin to generate a folder(D:\cygwin64\home\lbs
,You can use pwd
command in Cygwin to query the folder) which contains .bashrc
, .bashrc_profile
, .profile
and .inputrc
. These files are about path environment.
You can use cygcheck -c -d
command to show the installed packages. Command cygcheck -c -d | grep regexp
can alos be used to show the version of choosen and installed packages, regexp
is used to choose packages.
Open Cygwin and run the following command.
tar xvfz ns-allinone-2.35.tar.gz
cd ns-allinone-2.35
./install #进行安装
You may come across the following error: Checking for gcc4....No!
GCC-4
has been already obsoleted in Cygwin. You can install gcc5
in the items list of Cygwin. To install gcc4
, you can first download gcc4
at here.I choose to download gcc-4.8.0.tar.gz here.
Second, please put the file to directory "D:\cygwin64\home\lbs"
and unzip the file.
tar xvfz gcc-4.8.0.tar.gz
Third, please input the following command.
cd gcc-4.8.0
./contrib/download_prerequisites
./configure
cd ..
mkdir build-gcc
cd build-gcc
$PWD/../gcc-4.8.0/configure --prefix=$HOME/gcc-4.8.0 --enable-languages=c,c++,fortran,go
make
make install
The make
step takes a long time. If your computer has multiple processors or cores you can speed it up by building in parallel using make -j 2
(or a higher number for more parallelism).
[1] Ubuntu 14.04下安装ns2.35 [2] ubuntu 12.04 安装 ns2.35 [3] windows下安装NS2 [4] Windows下安装NS2详细步骤 [5] Installing GCC _ GCC Wiki [6] linux 安装问题make: 没有指明目标并且找不到makefile [7] How to Install the Latest GCC on Windows
##2. Add mUDP & mUdpSink & mTcpSink modules
You can downlaod mUdp.cc
,mUdp.h
,mUdpSink.cc
,mUdpSink.h
,mTcpSink.cc
and mTcpSink.h
files from Baidu Pan at here. You can also download these modules from CSDN
at []here](http://download.csdn.net/download/joanna_yan/8232917).
###2.2 Create a folder
Create a new folder whose name is measure
. The measure
folder should be existed in/home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/measure
. Then, put these module files into the folder.
###2.3 Modify packet.h
file
Open packet.h
file which exists in/home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/common/
, and add the following code into struct hdr_cmn{}
(in line 599).
//------------------------
//add by lbs 2016-11-18
int frametype_;
double sendtime_;
unsigned int pkt_id_;
unsigned int frame_pkt_id_;
//------------------------
Add the following code to Makefile
& Makefile.in
files which lie in /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/
. Remember to use TAB
key instead of SPACE
key.
measure/mudp.o measure/mudpsink.o \
measure/mtcpsink.o \
Add the following code to ns-default.tcl
file which lies in /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/tcl/lib
. The following code should be placed after line 830 : Agent/Ping set packetSize_ 64
.
#------------------------------
# add by lbs 2016-11-17
Agent/mUDP set packetSize_ 1000
#------------------------------
Change you directory to /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/
in the terminal, and run the following command.
./configure --with-tcl-ver=8.5
make clean
make
You may encounter the following error: cannot call constructor 'mUdpAgent::UdpAgent' directly [-fpermissive] UdpAgent::UdpAgent
.
To solve the problem, you can delete UdpAgent::UdpAgent
in your mUdp.cc
file.
mUdpAgent::mUdpAgent() : id_(0), openfile(0)
{
bind("packetSize_", &size_);
//UdpAgent::UdpAgent(); modify by lbs 2016-11-17 line 19
}
[1] mUDP,mUdpSink,mTcpsink的添加 [2] mUDP,mUdpSink,mTcpsink 下载
Open /home/ProgramFiles/ns-allinone-2.35/ns-2.35/queue
directory. Copy drop-tail.cc
and drop-tail.h
files and then, rename the copied files with myfifo.cc
and myfifo.h
.
- Modify
myfifo.h
Usemyfifo
to replace allDropTail
anddrop_tail
. - Modify
myfifo.cc
Usemyfifo
to replace allDropTail
,drop-tail
anddrop_tail
.
Open /home/ProgramFiles/ns-allinone-2.35/ns-2.35/tcl/lib
directory. Then open ns-default.tcl
file and find setting of DropTail
.
Queue/DropTail set drop_front_ false
Queue/DropTail set summarystats_ false
Queue/DropTail set queue_in_bytes_ false
Queue/DropTail set mean_pktsize_ 500
Copy the four lines command and paste behind them. Last, change DropTail
to myfifo
.
Queue/myfifo set drop_front_ false
Queue/myfifo set summarystats_ false
Queue/myfifo set queue_in_bytes_ false
Queue/myfifo set mean_pktsize_ 500
Open /home/ProgramFiles/ns-allinone-2.35/ns-2.35
directory. Then open Makefile
and Makefile.in
file.
Change
queue/queue.o queue/drop-tail.o \
to
queue/queue.o queue/drop-tail.o queue/myfifo.o \
Change you directory to /home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/
in the terminal, and run the following command.
./configure --with-tcl-ver=8.5
make clean
make
Please refer 2.Add mUDP & mUdpSink & mTcpSink modules
in the notes.
- Change
set namfd [open nam-expr.tr w]
toset namfd [open nam-expr.nam w]
Please refer 3.Add myfifo module
in the notes.
- Change
set thgpt($i) [expr $ackno_($i) * 1000.0 * 8.0 / ($time - $start($i))]
to
set thgpt($i) [expr $ackno_($i) * 1000.0 * 8.0 / ($time - $startT($i))]`
- Change
#$q_ attach $queuechan
to
$ns monitor-queue $r1 $r2 [open q-$par1-$par2.tr w] 0.3
[$ns link $r1 $r2] queue-sample-timeout
- The whole code of Tcl
if {$argc != 2} {
puts "Usage: ns 9-1.tcl queuetype_noflows_"
puts "Examples: ns 9-1.tcl myfifp 10"
puts "queuetype_: myfifo or RED"
exit
}
set par1 [lindex $argv 0]
set par2 [lindex $argv 1]
#create a simulator
set ns [new Simulator]
# open a trace file
set nd [open out-$par1-$par2.tr w]
$ns trace-all $nd
# define a finish process
proc finish {} {
global ns nd par2 tcp startT
$ns flush-trace
close $nd
set time [$ns now]
set sum_thgpt 0
#throughput = Ack * PacketSize(bit)/deliver time
# Ack = Received Packet
for {set i 0} {$i < $par2} {incr i} {
set ackno_($i) [$tcp($i) set ack_ ]
set thgpt($i) [expr $ackno_($i) * 1000.0 * 8.0 / ($time - $startT($i))]
puts $thgpt($i)
set sum_thgpt [ expr $sum_thgpt+$thgpt($i) ]
}
set avgthgpt [expr $sum_thgpt/$par2]
puts "average throughput:$avgthgpt (bps)"
exit 0
}
#create 20 nodes
for {set i 0} {$i < $par2} {incr i} {
set src($i) [$ns node]
set dst($i) [$ns node]
}
#create two router
set r1 [$ns node]
set r2 [$ns node]
#link node to router
for {set i 0} {$i < $par2} {incr i} {
$ns duplex-link $src($i) $r1 100Mb [expr ($i*10)]ms DropTail
$ns duplex-link $r2 $dst($i) 100Mb [expr ($i*10)]ms DropTail
}
$ns duplex-link $r1 $r2 56k 10ms $par1
#set Queue Size between routers
$ns queue-limit $r1 $r2 50
#trace queue length
set q_ [[$ns link $r1 $r2] queue]
set queuechan [open q-$par1-$par2.tr w]
$q_ trace curq_
if {$par1 == "RED"} {
# use packet mode
$q_ set bytes_ false
$q_ set queue_in_bytes_ false
}
#$q_ attach $queuechan
$ns monitor-queue $r1 $r2 [open q-$par1-$par2.tr w] 0.3
[$ns link $r1 $r2] queue-sample-timeout
for {set i 0} {$i < $par2} {incr i} {
set tcp($i) [$ns create-connection TCP/Reno $src($i) TCPSink $dst($i) 0]
$tcp($i) set fid_ $i
}
# start data transfer randomly between 0s and 1s
set rng [new RNG]
$rng seed 1
set RVStart [new RandomVariable/Uniform]
$RVStart set min_ 0
$RVStart set max_ 1
$RVStart use-rng $rng
# set data transfer starting time
for {set i 0} {$i < $par2} {incr i} {
set startT($i) [expr [$RVStart value]]
#puts "startT($i) $startT($i) sec"
}
# data transfer
for {set i 0} {$i < $par2} {incr i} {
set ftp($i) [$tcp($i) attach-app FTP]
$ns at startT($i) "$ftp($i) start"
}
$ns at 50.0 "finish"
$ns run
- Changes when using gnuplot Instead of
plot "q-myfifo-10.tr" using 2:3 with linespoints,"q-RED-10.tr" using 2:3 with linespoints
You should use the following command
plot "q-myfifo-10.tr" using 1:5 with linespoints,"q-RED-10.tr" using 1:5 with linespoints
using 1:5
means data of list 1 is used for x-axis and data of list 5 is used for y-axis.
[1] NS2队列管理机制
###4.18 Lab18 无线网络封包传输遗失模型
在原本的NS2无线网络结构中,封包从传送段送出,若是没有发生碰撞且接收端在接收信号允许的范围内,则接收端一定可以成功地接收到封包。在此假设下,是没有考虑传输过程中封包发生错误的情形。
为此,可以修改ns2安装目录下的mac文件夹下的wireless-phy.cc
,添加随机统一模型与连续性遗失模型,用来仿真封包从传送端到接收端的传输过程中可能发生的错误情形。
这里需要特别注意的是,由于传输遗失模型只在接收端作用,所以若是属于双向传输(如TCP
)的话,也要在传送端加上无线遗失模型。
- Download wireless-error-model.rar
- Copy
noah
directory inwireless-error-model.rar
to/home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35
- Add
noah.tcl
to.../ns-allinone-2.35/ns-2.35/tcl/mobility
- Modify
Makefile
&Makefile.in
Addnoah/noah.o \
to OBJ_CC andtcl/mobility/noah.tcl \
to NS_TCL_LIB.
OBJ_CC = \
...
noah/noah.o \
...
NS_TCL_LIB = \
...
tcl/mobility/noah.tcl \
...
- Modify
wireless_phy.cc
&wireless_phy.h
files Usewireless_phy.cc
&wireless_phy.h
files inwireless-error-model.rar
to replace these files ofNS2
(/home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35/mac
) - Add
forwarder.cc
&forwarder.h
files to../ns-allinone-2.35/ns-2.35/mac
. And change#include <iostream.h>
to#include <iostream>
inforwarder.cc
andforwarder.h
- Run
configure
&make
Change the directory to/home/lbs/ProgramFiles/ns-allinone-2.35/ns-2.35
and runconfigure
&make
command
./configure --with-tcl-ver=8.5
sudo make clean
sudo make
[1] NS2无线网络遗失模型 [2] 在ns2.35下完成柯老师lab18实验- Mr.Sting ###4.19 myEvalvid ###4.20 myEvalvid-NT ###4.21 图像传输效果分析与评估 ###4.22 无线网络效果分析(一):隐藏结点与暴露结点 ###4.23 无线网络效果分析(二):AdHoc网络路由协议效果分析 ###4.24 无线网络效果分析(三):802.11b DCF与802.11e EDCA的比较 ###4.25 无线网络效果分析(四):IEEE802.11b的吞吐量 ###4.26 无线网络效果分析(五):效果异常
- 邮箱:[email protected]
- 微信:@脱缰的哈士奇(ab1203940926)
- 微博:@脱缰的哈士