Centos7下的NS-3安装与配置总结(超详细!超完整!)

CONTENTS

1 NS-3概述

2 平台安装

2.1 安装ns-3的依赖环境

2.2 Downloading ns-3

3 Eclipse配置

3.1 安装Eclipse

3.2 安装cdt

3.3 配置

4 脚本运行

4.1 终端编译运行

4.2 Eclipse编译运行

5 可视化界面

5.1 PyViz安装

5.2 PyViz使用

6 总结

7 附录

7.1 first.cc

7.2 second.cc

8 参考资料

9 版权声明


1 NS-3概述

官网:https://www.nsnam.org/

ns-3 is a discrete-event network simulator for Internet systems, targeted primarily for research and educational use. ns-3 is free software, licensed under the GNU GPLv2 license, and is publicly available for research, development, and use.ns-3是用于因特网系统的离散事件网络模拟器,主要用于研究和教育用途。ns-3是免费软件,根据GNU GPLv2许可证授权,可公开用于研究,开发和使用。

介绍完NS-3的用途接下来就是安装教程啦~

注意:本实验所有操作通过虚拟机直接进入root用户进行操作,默认所有操作都在root权限下进行。


2 平台安装

2.1 安装ns-3的依赖环境

yum install gcc-c++ python

yum install python-devel

yum install mercurial 

yum install doxygen graphviz ImageMagick

yum install python-sphinx dia texlive texlive-latex

yum install openmpi openmpi-devel

yum install tcpdump wireshark

yum install sqlite sqlite-devel

yum install libxml2 libxml2-devel

yum install boost-devel 

yum install graphviz graphviz-devel python-setuptools-devel ipython

sudo easy_install pygraphviz

yum install goocanvas pygtk2-devel

 

Then obtain the RPM for pygoocanvas andpygoocanvas-devel from here: http://li.nux.ro/download/nux/dextop/el6/x86_64/

在上面网址下载pygoocanvas-0.14.1-3.el6.nux.x86_64.rpm和pygoocanvas-devel-0.14.1-3.el6.nux.x86_64.rpm,放入/root/packages文件夹中,用rpm进行安装时要在包前加上文件路径。

rpm -ivh /root/packages/pygoocanvas-0.14.1-3.el6.nux.x86_64.rpm --nodeps --force

rpm -ivh /root/packages/pygoocanvas-devel-0.14.1-3.el6.nux.x86_64.rpm --nodeps --force

 

yum install git

yum install gsl gsl-devel

yum install gtk2 gtk2-devel

yum install gdb valgrind

 

2.2 Downloading ns-3

创建文件夹:[root@localhost ~]# mkdir tarballs

进入tarballs:[root@localhost ~]# cd tarballs

联网下载: [root@localhost tarballs]# wget http://www.nsnam.org/releases/ns-allinone-3.21.tar.bz2

解压:[root@localhost tarballs]# tar jxvf ns-allinone-3.21.tar.bz2

进入ns-allinone-3.21[root@localhost tarballs]# cd ns-allinone-3.21

构建安装:[root@localhost ns-allinone-3.21]# ./build.py

进入ns-3.21:[root@localhost ns-allinone-3.21]# cd ns-3.21

使用waf编译安装,使用test.py测试,运行脚本测试:

[root@localhost ns-3.21]# ./test.py

[root@localhost ns-3.21]# ./waf distclean

[root@localhost ns-3.21]# ./waf --build-profile=debug --enable-examples --enable-tests configure

[root@localhost ns-3.21]# ./waf

[root@localhost ns-3.21]# ./test.py -c core

[root@localhost ns-3.21]# ./waf --hello-simulator

通过以上代码和部分压缩吧可以搭建好ns-3.21的环境,接下来是安装eclipse。


3 Eclipse配置

3.1 安装Eclipse

(1)测试一下是否已安装jdk:# java -version

         一般centos7已经安装jdk 8.0,如果没安装,在root用户下,输入命令yum install java

(2)下载eclipse:# wget http://mirrors.neusoft.edu.cn/eclipse/technology/epp/downloads/release/luna/SR2/eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz

(3)新建一个eclipse文件夹保存下载压缩包:

         # mkdir eclipse

         # mv /root/eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz /root/eclipse

(4)将eclipse压缩包解压到/opt目录下:

         # cd eclipse 

         # tar -zxvf eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz -C/opt

(5)建立软连接(等同于快捷方式):

         # ln -s /opt/eclipse/eclipse /usr/bin/eclipse

(6)为 Eclipse 设置桌面启动:

         # vim /usr/share/applications/eclipse.desktop

         并在文件中添加如下内容:

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse 4.4.1
Comment=Eclipse Luna 
Exec=/usr/bin/eclipse
con=/opt/eclipse/icon.xpm
Terminal=false
Version=1.0
Type=Application
Categories=Application;Development;Java;IDE


:wq保存退出

(7)Eclipse安装完成,可在应用程序——编程中查看

3.2 安装cdt

(1)打开 eclipse ,点击 help-Install new software

点击Add

Nmae:cdt

Location:http://download.eclipse.org/tools/cdt/releases/kepler

点击ok后,再点击右下方next按照需要选择组件继续向下安装即可。

3.3 配置

(1)新建一个C++Project,我将其name命名为homework,这个项目的路径是:/root/workspace/homework

将/root/tarballs/ns-allinone-3.21/ns-3.21目录下的所有文件,全部复制到homework目录中,在eclipse中刷新构建该项目。

(2)进入ns-3.21目录,执行waf配置:

# cd /root/tarballs/ns-allinone-3.21/ns-3.21

# ./waf --build-profile=debug --enable-examples --enable-tests configure

(3)回到eclipse,右击项目homework,选择properties,点击C/C++ build

Build command栏中改写为:${workspace_loc:/homework/waf}

Build directory栏中改写为:${workspace_loc:/homework/build}

点击上方的behaviour,将all改为build

点击apply,ok完成应用设置。

(4)右击homework项目,选择debug as --> debug configure --> 选择C/C++ application

修改C/C++ Application:build/scratch/scratch-simulator

再选Environment,添加如下信息:

Variable:LD_LIBRARY_PATH

Value:${workspace_loc:/homework/build}

(5)运行scratch文件夹下的scratch-simulator.cc,运行结果如下图所示:

至此,eclips配置成功。


4 脚本运行

4.1 终端编译运行

(1)将编写的脚本(first.cc和second.cc)复制到/root/tarballs/ns-allinone-3.21/ns-3.21/scratch目录下,可以在centos7窗口界面直接复制;然后,进入ns3目录:# cd /root/tarballs/ns-allinone-3.21/ns-3.21

(2)构建(编译):# ./waf

(3)运行脚本

运行first.cc脚本:# ./waf --run scratch/first

运行second.cc脚本:# ./waf --run scratch/second

4.2 Eclipse编译运行

(1)将编写的脚本(first.cc和second.cc)复制到/root/workspace/homework/scratch目录下,刷新构建该工程。

(2)右击homework项目,选择debug as --> debug configure --> 选择C/C++ application

修改C/C++ Application:build/scratch/first

然后点击apply应用于该项目。

(3)打开first.cc文件,运行可的如下结果:


5 可视化界面

PyViz是一个用Python开发的在线ns-3可视化工具,不需要使用trace文件

5.1 PyViz安装

(1)首先需要检查机器上是否有epel仓库,因为会用到里面的一些包。如果有结果则从(2)开始向下进行,如果无显示则表明本机无epel仓库需要继续执行后续操作。

检查epel仓库:# rpm -qa | grep epel

安装epel:# yum -y install epel-release

查看仓库信息:# yum repolist

(2)安装该模块依赖的软件包

# yum install python-devel gnome-python2 gnome-python2-gnomedesktop gnome-python2-rsvg graphviz-python pygoocanvas python-kiwi graphviz-devel

(红色字体的包我没有在yum,epel源中找到,暂时不知是否会对以后操作有影响,所以暂且搁置。做完回来发现这三个包即使没有找到未安装,最后的可视化界面也能出来,还是没有搞懂,我还是自己再研究研究吧。)

# yum install python

# yum install ipython

# yum install mock

# useradd -s /sbin/nologin mockbuild

# yum install cmake glibc-devel  glibc-devel

# easy_install pygraphviz

(3)下载pyviz

pyviz下载网址:http://code.nsnam.org/gjc/

# tar -xzvf ns-3-pyviz-d26a12d3ebb8.tar.gz

解压后将文件夹放在/root/tarballs/ns-allinone-3.21/ns-3.21/目录下,再重新编译如下命令:

# ./waf clean

# ./waf --build-profile=debug --enable-examples --enable-tests configure

(4)用如下命令可以测试是否安装成功:

# ./waf --pyrun src/flow-monitor/examples/wifi-olsr-flowmon.py --visualize

若安装成功则会出现下图:

5.2 PyViz使用

(1)修改脚本

首先,到/root/tarballs/ns-allinone-3.21/ns-3.21/scratch找到first.cc和second.cc;

然后,双击打开这两个脚本,看文件中是否有以下两句,如果没有则添加到两文件的指定位置:

CommandLine cmd;
cmd.Parse(argc,argv);

(2)运行脚本显示可视化图像

# ./waf --run scratch/first --vis

# ./waf --run scratch/second --vis


6 总结

拖拖拉拉好长时间终于将NS-3的总体框架搭建成功了~希望对大家的学习有所帮助,有问题可以评论,我会尽力帮大家解决哒~

That is all.


7 附录

7.1 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
 */

 //Include头文件
#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"

//使用ns3 namespace(无需使用ns3::)
using namespace ns3;

//定义一个log模块
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse(argc,argv);
  Time::SetResolution (Time::NS);
  //Enable两个log模块,将在收到和发出数据包后输出log消息
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  //NodeContainer:便于我们创建、管理和访问Node对象
  NodeContainer nodes;
  nodes.Create (2);

  //帮助我们配置和连接PointToPointNetDevice和PointToPointChannel
  //将Device的数据发送速率设为5Mbps
  //将Channel的时延设为2ms
  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
	
  //使用helper的Install方法在每个节点上创建一个pointToPointNetDevice,
  //保存在NetDeviceContainer中,并且还创建了一个PointToPointChannel
  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);

  //在两个node上安装协议栈(IP、TCP、UDP等)
  InternetStackHelper stack;
  stack.Install (nodes);

  //管理和分配IPv4地址
  //在Device上依次绑定IP地址,10.1.1.1开始,直到10.1.1.254,产生网络接口(interface)
  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  //UdpEchoServerApplication和UdpEchoClientApplication
  //创建一个工作在端口9的echoServer
  UdpEchoServerHelper echoServer (9);

  //使用Install方法将Application安装在#1的Node上
  //Application在第1秒开始运行,在第10秒停止
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));
  
  //指定对端的地址和端口、指定最大发包数、发包间隔、大小
  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  //使用Install方法在节点0创建echoClient
  //第2秒开始、第10秒停止
  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  //执行仿真
  //按照事件的时序,从第0到第10秒执行
  //使用Destroy清理,资源回收
  Simulator::Run ();
  Simulator::Destroy ();
  
  return 0;
}

7.2 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
 */

#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"

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1   n2   n3   n4
//    point-to-point  |    |    |    |
//                    ================
//                      LAN 10.1.2.0


using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("SecondScriptExample");

int 
main (int argc, char *argv[])
{
  bool verbose = true;
  uint32_t nCsma = 3;

  //命令行参数,允许用户指定脚本运行时的参数
  CommandLine cmd;
  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
  cmd.Parse (argc,argv);
  
  //根据用户命令行参数,启用Logging
  if (verbose)
    {
      LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
      LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
    }

  //个数为1或nCsma
  nCsma = nCsma == 0 ? 1 : nCsma;

  /**************开始定义网络拓扑***************/
  //两个p2p节点:n0和n1
  NodeContainer p2pNodes;
  p2pNodes.Create (2);

  //nCsma个CSMA节点
  NodeContainer csmaNodes;
  csmaNodes.Add (p2pNodes.Get (1));
  csmaNodes.Create (nCsma);

  //P2P链路属性
  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  NetDeviceContainer p2pDevices;
  p2pDevices = pointToPoint.Install (p2pNodes);

  //总线网络属性
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

  NetDeviceContainer csmaDevices;
  csmaDevices = csma.Install (csmaNodes);

  //安装协议栈
  InternetStackHelper stack;
  stack.Install (p2pNodes.Get (0));
  stack.Install (csmaNodes);

  //分配IP地址
  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");   //p2p网段
  Ipv4InterfaceContainer p2pInterfaces;
  p2pInterfaces = address.Assign (p2pDevices);

  address.SetBase ("10.1.2.0", "255.255.255.0");   //总线网段
  Ipv4InterfaceContainer csmaInterfaces;
  csmaInterfaces = address.Assign (csmaDevices);

  /**************网络拓扑定义结束***************/
  
  
  /**************应用程序开始***************/
  UdpEchoServerHelper echoServer (9);
  
  //服务器在最后一个节点上
  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  //客户端在n0
  ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));
  /**************应用程序结束***************/
  
  
  //根据节点产生的链路通告为每个节点建立路由信息
  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  //开启pcap跟踪,生成以"second"为前缀的文件名
  pointToPoint.EnablePcapAll ("second");
  csma.EnablePcap ("second", csmaDevices.Get (1), true);

  Simulator::Run ();
  Simulator::Destroy ();
  
  return 0;
}

8 参考资料

ns-3手册:https://www.nsnam.org/wiki/Installation#CentOS

Centos7下安装配置NS3:https://wenku.baidu.com/view/3670a2ba3086bceb19e8b8f67c1cfad6185fe970.html

NS_3--PyViz:https://blog.csdn.net/wuzhiwuweisun/article/details/79790041


9 版权声明

本文为博主原创文章,未经博主允许不得转载!

联系本博主同意后转载,引用参考请务必注明出处。

如有侵权者,本博主保留追究法律责任的权力。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章