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 版權聲明

本文爲博主原創文章,未經博主允許不得轉載!

聯繫本博主同意後轉載,引用參考請務必註明出處。

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

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