【网络仿真】ns 您所在的位置:网站首页 ns3安装卡顿错误则么办 【网络仿真】ns

【网络仿真】ns

2023-12-14 12:28| 来源: 网络整理| 查看: 265

此篇为学习笔记,课程地址——> 1 2

1 引言

The ns-3 simulator is a discrete-event network simulator targeted primarily for research and educational use.

ns-3模拟器: ns-3是一 款用于学术研究和教育用途的开放源代码验高散 事件模拟器,始于2006年。 我们将通过几个简单的模拟示例来窥探ns- 3的关键慨念和特征。 提示: ns 3是全新设计和实现的网络横拟器,它与ns-2不兼容!

1.1 关于ns-3

开发环境推荐:

OS: Linux or macOS,不推荐Windows OS (可在Windows 下使用vmware workstation或 virtualbox虚拟化软件安装Linux)Language: C++、PythonCommand Line Terminal:虽然有动画、数据分析与可视化工具可用。绝大工作还是在终端命令行中完成。 1.2 for ns-2 users

ns-3允许在模拟最中执行现实世界中的代码。借助DCE (Direct Code Execution) 可以将整个Linux网络栈封装仅一个ns-3节点中。

1.3 为ns-3做贡献

ns-3网络模拟器提供了一个开放、可扩展的网络模拟平台,主要用于网络研究和教育目的。 简单来说:ns-3提供了很多模型(models),用于模拟分组数据网络如何工作和执行;提供了一个网络模拟引擎。 为什么要使用ns-3:

Include to perform studies that are more difficult or not possible to perform with real systemsto study system behavlor in a highly controlledreproducible environment, and to learn about how networks work

大多数的ns-3模型用于建模Internet协议和网络是如何工作的。ns-3不局限于Internet系统。也有一些用户用于建模非Internet系统

现有的网络模拟/仿真工具:

OPNETOMNET++ns-2 2 资源 2.1 Web Ofliclal Webste: https://www.nsnam.org Officlal Documents Webpage: http://www.nsnam.org/documentation https://www.nsnam.org/documentation/development-tree/Official Wiki: https://www.nsnam.org/wikiSource Code: https://gitlab.com/nsnam 2.2 git

2018年12月ns-3的源码管理工具从Mer curial迁移到了Git。

2.3 waf

ns-3源码构建系统采用waf (https://waf.io/book/) . waf是基于Python语言的源码构建系统,与make的功能相同(make太过复杂)。 不用去深究waf的实现细节,会用即可!(用来编译运行脚本)

2.4开发环境

ns-3采用的编程语言: C++、Python

C++ Tutorial: http://www.cplusplus.com/doc/tutorial/Python: http://python.org Linux中使用Gnu toolchain:GNUC++编译器: gcc(g++)GNU binutilsGNU gdbns-3没有使用GNU的构建系统,而使用waf构建系统 macOS Toolchain:Xcode 2.5 套接字编程

参考链接:

https://www.elsevier.com/books/tcp-ip-sockets-in-c/donahoo/978-0-12-374540-8http://cs.baylor.edu/~donahoo/practical/CSockets/https://www.elsevier.com/books/multicast-sockets/makofske/978-1-55860-846-7 3 ns核心概念

基本模型图node类:表示主机、路由器 application类:表示网络应用程序,e.g. 客户端应用程序 channel类:表示信道 net device类:表示node上的网络通信设备及驱动程序,负责将node连接到channel。e.g. CsmaNetDevice, PointToPointNetDevice, WifiNetDevice topology helper:把net device连接到node和channel上,并配置它们,e.g. 分配ip地址

3.1 节点 Node ns3将基本的计算设备抽象为node(具体表现为node类)node类提供了许多方法模拟计算设备在计算机网络中的行为node的概念对应于host, end system…把node看作一台计算机(裸机),需要向计算机内添加软硬件功能部分:应用程序、协议栈、计算机外设等。

Class_Node_API

Class_NodeContainer_API

Class_NodeList_API

3.2 应用程序 Application

应用程序是运行在node内部的用户软件。 ns3中有许多application类的子类:

UdbEchoClientApplicationUdbEchoServerApplication

Class_Application_API

Class_ApplicationContainer_API

3.3 信道 Channel

信道是数据的传输通道,有线、无线… the basic communication subnetwork abstraction is called the channel and is represented in C++ by the class Channel

现实世界:网卡、双绞线虚拟世界 :node、channel

Channel类提供相应的方法来管理通信子网对象,将节点连接到通信子网中。

a channel specialization may model something as simple as a wire.可以模拟网线。the specialized channel can also model things as complicated as a large Ethernet switch.可以模拟以太网交换机。or three-dimensional space full of obstructions in the case of wireless networks.模拟无线网络中充满障碍的三维空间。

常见的channel:

CsmaChannel: 可以模拟实现scma(carrier sense multiple access)的通信子网PointToPointChannelWifiChannel

Class_Channel_API

3.4 网络设备 Net Device

(node与channel连接必须有net device,例如网卡)

ns-3中的网络设备抽象包括两部分:

software driver: 设备驱动程序simulated hardware: 仿真硬件 net device 被安装在node中,使当前node能够和其他node构建网络(借助channel)

Class_NetDevice_API

Class_NetDeviceContainer_API

3.5 拓扑辅助类 Topology Helpers

核心:简化网络拓扑配置,将那些机械的重复配置作用Helper实现。

Helpers示例(在官方文档的类索引中搜索Helper):

PointToPointHelperInternetStackHelperIpv4AddressHelperUdpEchoServerHelperUdpEchoClientHelper 4 ns3安装

官方安装指南 视频安装指南 python版本问题 其他ns3安装教程 (tips: 如果后续要安装ns3-gym,建议此处安装n3-3.30版本)

5 第一个模拟脚本

github例子分析文档

在ns-3中启动vscode:(ctrl+alt+t)

#命令行输入命令 cd /home/jnbai/桌面/ns3/ns-allinone-3.33/ns-3.33 code --user-data-dir=/root/.vscode-root/ . #root用户 code . #普通用户 #在命令行中运行:(ns-3.33目录下) sudo ./waf --run first

在vscode中运行:.cc文件要放在ns-3.33->scratch文件夹下,且用命令行已经运行过一次。 因此,以后实验编写的脚本(.cc文件)要放在ns-3.33/scratch下面。

第一个模拟脚本(在ns-3.33->examples->tutorial->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 */ //all header file is at build/ns3 #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" #include "ns3/netanim-module.h" // Default Network Topology // // 10.1.1.0 // n0 -------------- n1 // point-to-point // //if is not at namespace ns3, add xx:: //e.g. Time::NS, std::cout, std::min() using namespace ns3; NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); int main (int argc, char *argv[]) { //you can put into parameter at commandline //e.g. sudo ./waf --run "hello-simulator --numbers=5" CommandLine cmd; cmd.Parse (argc, argv); //this is to control time resolution unit (NS is millisecond)& how many log outputed Time::SetResolution (Time::NS); LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); //------------------------------------ //below is to build network topology //create two empty nodes by nodecontainer, and putint it. //you can get those nodes by nodes.Get (0) and nodes.Get (1) NodeContainer nodes; nodes.Create (2); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //this is use PointToPointHelper, install devices onto two nodes NetDeviceContainer devices; devices = pointToPoint.Install (nodes); //this is to install internet protocal into the nodes InternetStackHelper stack; stack.Install (nodes); //this is to set ip address: start from 10.1.1.0, subnet mask Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); //this is assign ip address to every devices Ipv4InterfaceContainer interfaces = address.Assign (devices); //finished setting bottom layer //------------------------------ //start setting upper layer //set server's port=9 UdpEchoServerHelper echoServer (9); //ApplicationContainer is to log every node's application //this is to install application, echoServer, into the node 1 ; set star time and end time ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); //interfaces.GetAddress (1)=ip address of server node; 9=port of server node UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); //this is set for visualization (NetAnim) AnimationInterface anim("first.xml"); anim.SetConstantPosition(nodes.Get(0), 1.0, 2.0); anim.SetConstantPosition(nodes.Get(1), 2.0, 3.0); Simulator::Run (); Simulator::Destroy (); return 0; }

在这里插入图片描述 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 */ // 运行指令,在ns-3.29下 // cp examples/tutorial/second.cc scratch/second.cc // ./waf --run second // ./waf --run second --vis # 可视化 // ./waf --run "second -- nCsma=100" --vis # 局域网中节点数3->100 //1.头文件 #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" //仿真拓扑图,有两种网络:p2p网络和CSMA网络,分别有2个和4个节点。其中n1上安装有两种NetDevice,i.e. p2p和csma。n0通过n1与n4通信。 // Default Network Topology // // 10.1.1.0 // n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 //2.命名空间 using namespace ns3; //3.定义LOG模块 NS_LOG_COMPONENT_DEFINE ("SecondScriptExample"); //4.主函数 int main (int argc, char *argv[]) { bool verbose = true; uint32_t nCsma = 3; //使用命令行声明nCsma变量 CommandLine cmd; cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);//添加命令行参数。nCsma变量表示,CSMA节点数目 cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.Parse (argc,argv);//读取命令行参数 if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); //打印UdpEchoClientApplication组件信息 LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); //打印UdpEchoServerApplication组件信息 } nCsma = nCsma == 0 ? 1 : nCsma; //5.创建网络拓扑 NodeContainer p2pNodes; p2pNodes.Create (2);//创建两个p2p型节点,n0----n1 NodeContainer csmaNodes; csmaNodes.Add (p2pNodes.Get (1));//n1节点既是p2p节点,又是csma节点 csmaNodes.Create (nCsma);//创建额外的nCsma个csma节点,n2,n3,n4 PointToPointHelper pointToPoint;//p2p助手类,设置设备和信道属性 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//设置设备传输速率为5Mbps pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//设置信道延迟为2ms NetDeviceContainer p2pDevices; //创建p2p网络设备 p2pDevices = pointToPoint.Install (p2pNodes); //把p2p网络设备分别安装在节点n0和n1上,然后共同连接至同一信道对象 CsmaHelper csma;//csma助手类,设置csma信道属性 csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));//设置传输速率为100Mbps csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));//设置信道延迟为6560ns NetDeviceContainer csmaDevices;//创建csma网络设备 csmaDevices = csma.Install (csmaNodes);//连接节点与信道 //6.安装协议栈和分配ip地址 InternetStackHelper stack;//为每个节点安装协议栈 stack.Install (p2pNodes.Get (0)); stack.Install (csmaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0");//为p2p网络设备分配ip地址,起始地址为10.1.1.0,终止地址为10.1.1.254 Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign (p2pDevices); address.SetBase ("10.1.2.0", "255.255.255.0");//为csma网络设备分配ip地址,起始地址为10.1.2.0,终止地址为10.1.2.254 Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); //7.安装应用程序 UdpEchoServerHelper echoServer (9);//监听9号端口 //配置服务端属性 ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));//使用Install方法将echoServer安装在节点n4(n3?)上,application在第1s开始运行并接受9号端口数据,在第10s结束。 serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); //配置客户端属性 UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//最大发送分组个数,1 echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));//分组发送间隔1s echoClient.SetAttribute ("PacketSize", UintegerValue (1024));//数据包大小,1024bit ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));//使用install方法将echoClient安装在节点n0上。Application在模拟,第2s开始运行,并接受9号端口的数据,在第10s停止。 clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); //8.设置全局路由 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); //9.数据追踪 pointToPoint.EnablePcapAll ("second");//EnablePcapAll (“second”)函数的作用是收集这个信道上所有结点链路层分组收发记录。记录文件格式是pcap,”second”是文件名前缀。 csma.EnablePcap ("second", csmaDevices.Get (1), true);//记录了一个有线节点中CSMA网络设备的分组收发信息 //10.启动与结束 Simulator::Run (); Simulator::Destroy (); return 0; }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有