【网络仿真】ns3-gym/rl-tcp-程序员宅基地

技术标签: python  网络仿真  

运行

# Terminal 1 
# ./waf --run "rl-tcp --transport_prot=TcpNewReno"
./waf --run "rl-tcp --transport_prot=TcpRl"

# Terminal 2
cd ./scratch/rl-tcp
./test_tcp.py --start=0

结果

# Terminal 1 
root@jnbai-ThinkPad-T460:/ns29/ns-allinone-3.29/ns-3.29# ./waf --run "rl-tcp --transport_prot=TcpRl" | more
Waf: Entering directory `/ns29/ns-allinone-3.29/ns-3.29/build'
Waf: Leaving directory `/ns29/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (3.749s)
Ns3Env parameters:
--openGymPort: 5555
--seed: 0
--Tcp version: ns3::TcpRl
Simulation process id: 14089 (parent (waf shell) id: 14081)
Waiting for Python process to connect on port: tcp://localhost:5555
Please start proper Python Gym Agent
Wait for stop message
RxPkts:
---SinkId: 0 RxPkts: 5367


# Terminal 2
Observation space:  Box(0, 1000000000, (15,), uint64) uint64
Action space:  Box(0, 65535, (2,), uint64) uint64
Start iteration:  0
Step:  0
---obs:  [1, 0, 163241, 2, 4294967295, 340, 340, 1, 458695632, 80375, 80000, 1, 0, 0, 0]
---action:  [229347816, 680]
...
Step:  2621
---obs, reward, done, info:  [1, 0, 10099379, 2, 25500, 33299, 340, 2, 51000, 154556, 80000, 1, 0, 2, 0] 2.0 False Increas
eWindow
---action:  [25500, 33302]
Step:  2622
---obs, reward, done, info:  [1, 0, 10099999, 2, 25500, 33302, 340, 2, 51000, 154556, 80000, 1, 0, 2, 0] 2.0 True Increase
Window
Done

基本接口

  1. 例如在Python脚本中,这样写。(注:gym.make('ns3-v0')启动当前工作目录中的ns-3仿真脚本)
import gym
import ns3gym
import MyAgent

env = gym.make('ns3-v0')
obs = env.reset()
agent = MyAgent.Agent()

while True:
	action = agent.get_action(obs)
	obs, reward, done, info = env.step(action)

	if done:
		break
env.close()
env = ns3env.Ns3Env(port=port, stepTime=stepTime, startSim=startSim, simSeed=seed, simArgs=simArgs, debug=debug)
# simpler:
#env = ns3env.Ns3Env()
env.reset()

ob_space = env.observation_space
ac_space = env.action_space
  1. 任何ns-3仿真脚本都可以作为Gym环境,只需要实例化OpenGymInterface (Ptr<OpenGymInterface> openGymInterface;);并实现ns3-gym C++接口,接口包括的函数有:
Ptr<OpenGymSpace> GetObservationSpace();
Ptr<OpenGymSpace> GetActionSpace();
Ptr<OpenGymDataContainer> GetObservation();
float GetReward();
bool GetGameOver();
std::string GetExtraInfo();
bool ExecuteActions(Ptr<OpenGymDataContainer> action);

此项目为两个版本(即time-based和event-based)的RL-TCP,可以控制Congestion WindowSlow Start Threshold两个版本都继承自TcpCongestionOps,因此可以被用在ns3::TcpL4Protocol::SocketType中。

此例中使用event-based接口,实现了TCP NewReno并使用ns3gym与ns-3仿真通信,这个例子可以作为RL-based TCP congestion control algorithms的开始点。

1 sim.cc

1.1 Emacs 模式行

// 解释代码的预定格式
// 描述ns-3的C++代码规范。统一风格是为了便于阅读。
// 具体规则为:1. 函数名、方法名、类名单词首字母大写,其他字母都小写;2. 变量名首字母小写,其他首字母大写,局部变量以g_开头,类的成员变量以m_开头,自定义类型以_t开头,常量全部大写
; // 确保使用Emacs编辑器的开发者可正确打开文件
/*
 * Copyright (c) 2018 Piotr Gawlowicz // 确保下面的代码在GPL版权下,即开源
 *
 * 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
 *
 * Author: Piotr Gawlowicz <[email protected]>
 * Based on script: ./examples/tcp/tcp-variants-comparison.cc
 *
 * Topology:
 *
 *   Right Leafs (Clients)                      Left Leafs (Sinks)
 *           |            \                    /        |
 *           |             \    bottleneck    /         |
 *           |              R0--------------R1          |
 *           |             /                  \         |
 *           |   access   /                    \ access |
 *           N -----------                      --------N
 */

1.2 头文件

引用的头文件中位置在build/ns3中

#include <iostream>
#include <fstream>
#include <string>

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/applications-module.h"
#include "ns3/error-model.h"
#include "ns3/tcp-header.h"
#include "ns3/enum.h"
#include "ns3/event-id.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/traffic-control-module.h"

#include "ns3/opengym-module.h"
#include "tcp-rl.h"

1.3 命名空间

如果使用的是命名空间不是ns3的,则要加 xx::yy,例如 Time::NS、std::cout、std::min()。
这是C++新引入的一个机制,主要是为了解决重名冲突的问题,名字放入多个命名空间来防止名字的冲突。
例如标准C++库提供的对象都放在std这个名字空间中。

using namespace ns3;

1.4 Logging系统

定义日志组件LOG模块,允许脚本使用log系统中的宏定义打印辅助信息。
即把相关信息打印在控制台上。

NS_LOG_COMPONENT_DEFINE ("TcpVariantsComparison"); // 向ns-3系统注册一个名为 "TcpVariantsComparison" 的记录组件。

static std::vector<uint32_t> rxPkts; 
// rePkts[i]表示第i个节点收到的数据包个数
// 定义了一个变量,类型为无符号32字节整型的静态数组,名字为rxPkts。
// static修饰的变量:静态变量,在内存中只有一份存储空间,不属于某个实例对象,被一个类的所有对象共享。
// const修饰的变量:使变量有常属性,即以后使用中其值都不能改变。

// 记录和打印每个节点收到数据包的数量
static void
CountRxPkts(uint32_t sinkId, Ptr<const Packet> packet, const Address & srcAddr)
{
    
  rxPkts[sinkId]++;
}

static void
PrintRxCount()
{
    
  uint32_t size = rxPkts.size();
  NS_LOG_UNCOND("RxPkts:");
  for (uint32_t i=0; i<size; i++){
    
    NS_LOG_UNCOND("---SinkId: "<< i << " RxPkts: " << rxPkts.at(i));
  }
}
//  NS_LOG_LOGIC ("IP Header size is: " << ip_header);

Logging系统有7个等级:

  • LOG_ERROR 有错误,打印信息
  • LOG_WARN 有警告,打印信息
  • LOG_DEBUG 有调试,打印信息
  • LOG_INFO 打印与程序相关信息
  • LOG_FUNCTION 有函数调用,打印调用相关信息
  • LOG_LOGIC 打印整体逻辑描述信息
  • LOG_ALL 打印所有信息

还有一种无条件日志级别,其日志详尽级别与组件选择无关

  • LOG_UNCOND 无条件记录信息

1.5 变量定义

int main (int argc, char *argv[])
{
    
  // 定义各种变量
  uint32_t openGymPort = 5555; // 定义open gym端口号为5555
  double tcpEnvTimeStep = 0.1;

  uint32_t nLeaf = 1;
  std::string transport_prot = "TcpRl";
  double error_p = 0.0;
  std::string bottleneck_bandwidth = "2Mbps";
  std::string bottleneck_delay = "0.01ms";
  std::string access_bandwidth = "10Mbps";
  std::string access_delay = "20ms";
  std::string prefix_file_name = "TcpVariantsComparison";
  uint64_t data_mbytes = 0;
  uint32_t mtu_bytes = 400;
  double duration = 10.0;
  uint32_t run = 0;
  bool flow_monitor = false;
  bool sack = true;
  std::string queue_disc_type = "ns3::PfifoFastQueueDisc";
  std::string recovery = "ns3::TcpClassicRecovery";

1.6 命令行参数

  CommandLine cmd;
  // required parameters for OpenGym interface
  cmd.AddValue ("openGymPort", "Port number for OpenGym env. Default: 5555", openGymPort); // 第一个参数:与命令行对应。第二个参数:说明,--printHelp时显示。第三个参数:与脚本变量对应。
  cmd.AddValue ("simSeed", "Seed for random generator. Default: 1", run);
  cmd.AddValue ("envTimeStep", "Time step interval for time-based TCP env [s]. Default: 0.1s", tcpEnvTimeStep);
  // other parameters
  cmd.AddValue ("nLeaf",     "Number of left and right side leaf nodes", nLeaf);
  cmd.AddValue ("transport_prot", "Transport protocol to use: TcpNewReno, "
                "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
                "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat, "
		            "TcpLp, TcpRl, TcpRlTimeBased", transport_prot);
  cmd.AddValue ("error_p", "Packet error rate", error_p);
  cmd.AddValue ("bottleneck_bandwidth", "Bottleneck bandwidth", bottleneck_bandwidth);
  cmd.AddValue ("bottleneck_delay", "Bottleneck delay", bottleneck_delay);
  cmd.AddValue ("access_bandwidth", "Access link bandwidth", access_bandwidth);
  cmd.AddValue ("access_delay", "Access link delay", access_delay);
  cmd.AddValue ("prefix_name", "Prefix of output trace file", prefix_file_name);
  cmd.AddValue ("data", "Number of Megabytes of data to transmit", data_mbytes);
  cmd.AddValue ("mtu", "Size of IP packets to send in bytes", mtu_bytes);
  cmd.AddValue ("duration", "Time to allow flows to run in seconds", duration);
  cmd.AddValue ("run", "Run index (for setting repeatable seeds)", run);
  cmd.AddValue ("flow_monitor", "Enable flow monitor", flow_monitor);
  cmd.AddValue ("queue_disc_type", "Queue disc type for gateway (e.g. ns3::CoDelQueueDisc)", queue_disc_type);
  cmd.AddValue ("sack", "Enable or disable SACK option", sack);
  cmd.AddValue ("recovery", "Recovery algorithm type to use (e.g., ns3::TcpPrrRecovery", recovery);
  cmd.Parse (argc, argv);

  transport_prot = std::string ("ns3::") + transport_prot;

  SeedManager::SetSeed (1);
  SeedManager::SetRun (run);

  NS_LOG_UNCOND("Ns3Env parameters:"); // 打印ns3环境参数
  if (transport_prot.compare ("ns3::TcpRl") == 0 or transport_prot.compare ("ns3::TcpRlTimeBased") == 0)
  {
    
    NS_LOG_UNCOND("--openGymPort: " << openGymPort);
  } else {
    
    NS_LOG_UNCOND("--openGymPort: No OpenGym");
  }

  NS_LOG_UNCOND("--seed: " << run);
  NS_LOG_UNCOND("--Tcp version: " << transport_prot);
# 使用命令行显示系统属性默认值
./waf --run "scratch/sim --PrintAttributes = ns3::PointToPointNetDevice"

# 使用命令行设置对应参数
./waf --run "scratch/sim --nLeaf = 3"

其他可写的命令:

--PrintHelp # 打印帮助信息
--PrintGroups # 打印组列表
--PrintTypeIds # 打印所有的TypeIds
--PrintGroup = [group] # 打印组中所有的TypeId
--PrintAttributes = [typeid] # 打印此TypeId的所有属性
--PrintGlobals # 打印globals的列表

1.7 随机变量

作用:生成随机数,使仿真结果有一定的随机性。

  1. 播种与独立重复
  SeedManager::SetSeed (1); // 设置种子,参数不变,生成的随机数也不变
  SeedManager::SetRun (run); // 设置运行标识
  1. 随机变量
  • ==随机数生成器(RNG)==通过对选定的随机种子做复杂运算,产生随机数。
  • ns-3提供了伪随机数生成器(PRNG),即以相同概率从一组有限数字中选择伪随机数。
  • 每个RNG提供一个很长的随机数序列,序列长度成为循环周期循环长度
  • 一个序列被分为几个相互没有关联的数据流。每个种子对应不同的RNG序列,每个标识对应相同序列分割的子序列

声明随机变量的类都有一个基类:RandomVariableStream
RandomVariableStream的派生类:

  • UniformRandomVariable: 可以设定最大最小值,按均匀分布方式返回一个随机数。
  • ConstantRandomVariable
  • SequentialRandomVariable
  • ExponentialRandomVariable
  • NormalRandomVariable
  // Configure the error model
  // Here we use RateErrorModel with packet error rate
  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> (); // 随机变量默认区间为[0,1) 
  // double min = 1.0; uv->SetAttribute("Min",DoubleValue(min));
  uv->SetStream (50);
  RateErrorModel error_model;
  error_model.SetRandomVariable (uv);
  error_model.SetUnit (RateErrorModel::ERROR_UNIT_PACKET);
  error_model.SetRate (error_p);

通过命令行改变运行标识:

# NS_GLOBAL_VALUE = "RngRun = 5" ./waf --run scratch/sim
./waf --run "scratch/sim --RngRun=5"

1.8 创建OpenGym环境

  // OpenGym Env --- has to be created before any other thing
  Ptr<OpenGymInterface> openGymInterface;
  if (transport_prot.compare ("ns3::TcpRl") == 0)
  {
    
    openGymInterface = OpenGymInterface::Get(openGymPort);
    // 使用Config::SetDefault设置对象的默认属性
    Config::SetDefault ("ns3::TcpRl::Reward", DoubleValue (2.0)); // Reward when increasing congestion window
    Config::SetDefault ("ns3::TcpRl::Penalty", DoubleValue (-30.0)); // Penalty when decreasing congestion window
  }

  if (transport_prot.compare ("ns3::TcpRlTimeBased") == 0)
  {
    
    openGymInterface = OpenGymInterface::Get(openGymPort);
    Config::SetDefault ("ns3::TcpRlTimeBased::StepTime", TimeValue (Seconds(tcpEnvTimeStep))); // Time step of TCP env
  }

1.9 其他配置

PDU: 协议数据单元。PUD=PCI(协议控制信息)+SDU(服务数据单元)
ADU: 应用数据单元。可以理解为网络上实际传输的数据单元。

  // Calculate the ADU size
  Header* temp_header = new Ipv4Header ();
  uint32_t ip_header = temp_header->GetSerializedSize ();
  NS_LOG_LOGIC ("IP Header size is: " << ip_header);
  delete temp_header;
  temp_header = new TcpHeader ();
  uint32_t tcp_header = temp_header->GetSerializedSize ();
  NS_LOG_LOGIC ("TCP Header size is: " << tcp_header);
  delete temp_header;
  uint32_t tcp_adu_size = mtu_bytes - 20 - (ip_header + tcp_header);
  NS_LOG_LOGIC ("TCP ADU size is: " << tcp_adu_size);

  // Set the simulation start and stop time
  double start_time = 0.1;
  double stop_time = start_time + duration;

  // 4 MB of TCP buffer
  Config::SetDefault ("ns3::TcpSocket::RcvBufSize", UintegerValue (1 << 21));
  Config::SetDefault ("ns3::TcpSocket::SndBufSize", UintegerValue (1 << 21));
  Config::SetDefault ("ns3::TcpSocketBase::Sack", BooleanValue (sack));
  Config::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (2));


  Config::SetDefault ("ns3::TcpL4Protocol::RecoveryType",
                      TypeIdValue (TypeId::LookupByName (recovery)));
                      
  // Select TCP variant
  if (transport_prot.compare ("ns3::TcpWestwoodPlus") == 0)
    {
    
      // TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
      Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
      // the default protocol type in ns3::TcpWestwood is WESTWOOD
      Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
    }
  else
    {
    
      TypeId tcpTid;
      NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (transport_prot, &tcpTid), "TypeId " << transport_prot << " not found");
      Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (transport_prot)));
    }
                     

1.10 错误模块

作用:确定与底层分布、速率和单位对应的哪些数据包出错。即产生随机的错误或丢包。

  // Configure the error model
  // Here we use RateErrorModel with packet error rate
  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> (); // 定义一个指向均匀分布随机变量生成器对象的指针uv
  uv->SetStream (50); // SetStream()为ns3::RandomVariableStream类的成员函数。输入为stream(RngStream的流号,-1表示自动分配流号),无输出。
  RateErrorModel error_model; // 实例化误差模型
  error_model.SetRandomVariable (uv); // 生成随机变量的随机变量分布。 
  error_model.SetUnit (RateErrorModel::ERROR_UNIT_PACKET); // 设置使用的错误单元,为PACET单元。 
  error_model.SetRate (error_p); // 设置模型使用的错误率,默认错误率为0。

1.11 创建网络拓扑

  // 创建中间的点到点链路
  // Create the point-to-point link helpers
  PointToPointHelper bottleNeckLink;
  bottleNeckLink.SetDeviceAttribute  ("DataRate", StringValue (bottleneck_bandwidth));
  bottleNeckLink.SetChannelAttribute ("Delay", StringValue (bottleneck_delay));
  //bottleNeckLink.SetDeviceAttribute  ("ReceiveErrorModel", PointerValue (&error_model)); // 设置错误接收属性
  
  // 创建两边的链路
  PointToPointHelper pointToPointLeaf;
  pointToPointLeaf.SetDeviceAttribute  ("DataRate", StringValue (access_bandwidth));
  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue (access_delay));
  
  // 创建哑铃型拓扑结构
  PointToPointDumbbellHelper d (nLeaf, pointToPointLeaf,
                                nLeaf, pointToPointLeaf,
                                bottleNeckLink);

拓展:

  1. 构造哑铃型拓扑 PointToPointDumbbellHelper
ns3::PointToPointDumbbellHelper::PointToPointDumbbellHelper	(	uint32_t 	nLeftLeaf,
PointToPointHelper 	leftHelper,
uint32_t 	nRightLeaf,
PointToPointHelper 	rightHelper,
PointToPointHelper 	bottleneckHelper 
)
  1. 构造星型拓扑 PointToPointStrarHelper
ns3::PointToPointStarHelper::PointToPointStarHelper	(	uint32_t 	numSpokes,
PointToPointHelper 	p2pHelper 
)	
  1. 构造网络型拓扑 PointToPointGridHelper
ns3::PointToPointGridHelper::PointToPointGridHelper	(	uint32_t 	nRows,
uint32_t 	nCols,
PointToPointHelper 	pointToPoint 
)	

1.12 安装协议栈和分配IP地址

  // Install IP stack
  InternetStackHelper stack; // 创建网络协议栈
  stack.InstallAll (); // 把协议栈安装在每个节点上
  
  // Assign IP Addresses
  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
                         Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
                         Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));

void PointToPointDumbbellHelper::AssignIpv4Addresses (
Ipv4AddressHelper leftIp,
Ipv4AddressHelper rightIp,
Ipv4AddressHelper routerIp)

1.13 流量控制

  // Traffic Control
  TrafficControlHelper tchPfifo; // 创建流量控制帮助实例,用来设置排队规则
  tchPfifo.SetRootQueueDisc ("ns3::PfifoFastQueueDisc"); // Linux系统使用的默认优先级队列为pfifo_fast

  TrafficControlHelper tchCoDel;
  tchCoDel.SetRootQueueDisc ("ns3::CoDelQueueDisc"); // 一种数据包排队规则,名为CoDel

  DataRate access_b (access_bandwidth); // DataRate类在network模块中,单位bps
  DataRate bottle_b (bottleneck_bandwidth);
  Time access_d (access_delay);
  Time bottle_d (bottleneck_delay);

  uint32_t size = static_cast<uint32_t>((std::min (access_b, bottle_b).GetBitRate () / 8) *
    ((access_d + bottle_d + access_d) * 2).GetSeconds ());

  Config::SetDefault ("ns3::PfifoFastQueueDisc::MaxSize",
                      QueueSizeValue (QueueSize (QueueSizeUnit::PACKETS, size / mtu_bytes)));
  Config::SetDefault ("ns3::CoDelQueueDisc::MaxSize",
                      QueueSizeValue (QueueSize (QueueSizeUnit::BYTES, size)));

  if (queue_disc_type.compare ("ns3::PfifoFastQueueDisc") == 0)
  {
    
    tchPfifo.Install (d.GetLeft()->GetDevice(1));
    tchPfifo.Install (d.GetRight()->GetDevice(1));
  }
  else if (queue_disc_type.compare ("ns3::CoDelQueueDisc") == 0)
  {
    
    tchCoDel.Install (d.GetLeft()->GetDevice(1));
    tchCoDel.Install (d.GetRight()->GetDevice(1));
  }
  else
  {
    
    NS_FATAL_ERROR ("Queue not recognized. Allowed values are ns3::CoDelQueueDisc or ns3::PfifoFastQueueDisc");
  }

1.14 Internet模块——路由

  NS_LOG_INFO ("Initialize Global Routing."); // 初始化全局路由
  Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // 填充路由表

路由:网络的核心部分
路由表:路由最关键的地方。
ns-3是开源软件,可以编写自己的路由。IPv4 Routing Protocol

  1. golbal centralied routing

为有线网络配置全局路由
每次都用最短路径(现实不存在),使用InternetStackHelper类创建网络协议栈时,全局路由会默认绑定到节点上。
当IP地址配置到节点时,Ipv4GlobalRoutingHelper::PopulateRoutingTables ()会使每个节点拥有一个IPv4接口接收路由表,而路由表是由GlobalRouteManager注册。
注:此协议对于无线节点没有效果(推荐用OLSR动态路由协议),可以用在Wi-Fi AP节点中。

// 实现路由表的更新
Ipv4GlobalRoutingHelper::RecomputeRoutingTables (); 

// 例如:在5s时更新路由表
Simulator::Schedule (Seconds (5), &Ipv4GlobalRoutingHelper::RecomputeRoutingTables); 

控制路由行为的属性:

  • Ipv4GlobalRouting::RandomEcmpRouting 数据分组是否会随机选择有相同代价的路由。
  • Ipv4GlobalRouting::RespondToInterfaceEvents 系统是否通过接口发现事件动态更新全局路由。
  1. unicast routing: 单播路由协议
    ns-3支持的单播路由协议:
  • IPv4 optimized link state routing (OLSR)
    源码:src/olsr
    例子:examples/simple-point-to-point-olsr.cc
NodeContainer c;
...
// Enable OLSR
NS_LOG_INFO ("Enabling OLSR");
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10); // OLSR协议会在static routing协议前使用
InternetStackHelper internet;
internet.SetRoutingHelper (list);
internet.Install (c);
  • IPv4 Static Routing (支持单播和多播)
  • IPv4 ad hoc on demand distance vector (AODV)
  • IPv4 destination sequenced distance vector (DSDV)
  • IPv4 ListRouting (保存路由协议的优先列表)
    提供了优先级列表保存多种路由并支持用户编写自己的路由,IPv4会根据这个队列调用这些路由协议。
    类 IPv4ListRouting 提供了一个纯虚函数(在基类中不实现,在继承类中实现的函数)void AddRoutingProtocol (Ptr< IPv4RoutingProtocol > routingProtocol, int16_t priority); 允许用户添加路由协议。
    例如:
Ptr<MyRoutingProtocol> myRoutingProto = CreateObject<MyRoutingProtocol> (); // 实例化自己的路由对象,保存在指针中
listRoutingPtr->AddRoutingProtocol (myRoutingProto, -10); // 根据路由优先级从高到低依次调用
  • IPv4 NixVectorRouting
  • IPv6 ListRouting
  • IPv6 Static Routing
  1. multicast routing: 多播路由协议
// 将多播路由加入到Node节点中
void 
Ipv4StaticRouting::AddMulticastRoute (Ipv4Address origin, Ipv4Address group, uint32_t inputInterface, std::vector<uint32_t> outputInterfaces);
// 参数1:一个源地址;参数2:一组目的地址;参数3:一个输入网络接口;参数4:一个输出网络接口。

1.15 应用层模块

  // Install apps in left and right nodes
  // 安装接收端应用程序
  uint16_t port = 50000;
  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress); // 数据包接收端助手类,参数1:协议;参数2:地址
  ApplicationContainer sinkApps;
  for (uint32_t i = 0; i < d.RightCount (); ++i)
  {
    
    sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
    sinkApps.Add (sinkHelper.Install (d.GetRight (i)));
  }
  sinkApps.Start (Seconds (0.0));
  sinkApps.Stop  (Seconds (stop_time));
  
  // 安装发送端应用程序
  for (uint32_t i = 0; i < d.LeftCount (); ++i)
  {
    
    // Create an on/off app sending packets to the left side
    AddressValue remoteAddress (InetSocketAddress (d.GetRightIpv4Address (i), port));
    Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (tcp_adu_size));
    BulkSendHelper ftp ("ns3::TcpSocketFactory", Address ());
    ftp.SetAttribute ("Remote", remoteAddress);
    ftp.SetAttribute ("SendSize", UintegerValue (tcp_adu_size));
    ftp.SetAttribute ("MaxBytes", UintegerValue (data_mbytes * 1000000));

    ApplicationContainer clientApp = ftp.Install (d.GetLeft (i));
    clientApp.Start (Seconds (start_time * i)); // Start after sink
    clientApp.Stop (Seconds (stop_time - 3)); // Stop before the sink
  }

1.16 Flow monitor模块

作用:收集每个流的统计信息以XML格式导出。

统计信息包括:
timeFirstTxPacket: 传输流中第一个数据包时的时间
timeLastTxPacket:传输 流中最后一个数据包时的时间
timeFirstRxPacket:端节点接收到流中的第一个数据包的时间;
timeLastRxPacket: 收到流中的最后一个数据包的时间
delaySum: 所有收到的流数据包的所有端到端延迟的总和;
jitterSum:所有接收到的流数据包的所有端到端延迟抖动(延迟变化)值的总和,参考文档:rfc:3393;
txBytes, txPackets: 流的传输字节/数据包总数;
rxBytes, rxPackets: 流的接收字节/数据包总数;
lostPackets: 假设丢失的数据包总数(未报告超过10秒);
timesForwarded: 报告转发数据包的次数;
delayHistogram, jitterHistogram, packetSizeHistogram: 延迟,抖动和数据包大小的直方图
packetsDropped, bytesDropped: 丢失的数据包和字节数,根据丢失原因代码(在探测中定义)进行划分。

  bool flow_monitor = false;
  
  cmd.AddValue ("flow_monitor", "Enable flow monitor", flow_monitor);

  // Flow monitor
  FlowMonitorHelper flowHelper; // 实例化流量监视器助手类
  if (flow_monitor)
  {
    
    flowHelper.InstallAll (); // 通过帮助程序,在节点中安装监视器(可以设置监视器属性、打印统计信息)
  }
  Simulator::Stop (Seconds (stop_time));
  Simulator::Run ();
  
  // 注:SerializeToXmlFile一定要放在Simulator::Run();之后去运行;SerializeToXmlFile的后两个参数是用来画图的,一般可以设置成false
  
  std::string prefix_file_name = "TcpVariantsComparison";
  
  cmd.AddValue ("prefix_name", "Prefix of output trace file", prefix_file_name);
  
  if (flow_monitor)
    {
    
      flowHelper.SerializeToXmlFile (prefix_file_name + ".flowmonitor", true, true);  // 序列化结果并写入文件中。参数1:文件名;参数2:若true在输出中包括Histograms直方图;参数3:是否激活Probes探针,若true在输出中包括探针-流对的统计数据。
    }

1.17 计算数据包数量

  static std::vector<uint32_t> rxPkts;
  
  // Count RX packets
  for (uint32_t i = 0; i < d.RightCount (); ++i)
  {
    
    rxPkts.push_back(0); // push_back()为在尾端加入数字。即rePkts[0]=0
    Ptr<PacketSink> pktSink = DynamicCast<PacketSink>(sinkApps.Get(i));
    pktSink->TraceConnectWithoutContext ("Rx", MakeBoundCallback (&CountRxPkts, i));
  }
  
  // 若使用的TCP协议包含了RL,关闭openGym接口
  if (transport_prot.compare ("ns3::TcpRl") == 0 or transport_prot.compare ("ns3::TcpRlTimeBased") == 0)
  {
    
    openGymInterface->NotifySimulationEnd();
  }

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

2 tcp-rl.h/cc

2.1 #ifndef

#ifndef TCP_RL_H
#define TCP_RL_H

#endif /* TCP_RL_H */

作用:防止该头文件被重复包含。

2.2 头文件

#include "ns3/tcp-congestion-ops.h"
#include "ns3/opengym-module.h"
#include "ns3/tcp-socket-base.h"

2.3 类的前置声明(Forward declaration)


namespace ns3 {
    

class TcpSocketBase;
class Time;
class TcpGymEnv;

2.4 类TcpSocketDerived

// used to get pointer to Congestion Algorithm
// 继承类TcpSocketBase,用于使用TCP实现流套接字的基类。
// TCP套接字 = IP + 端口号
class TcpSocketDerived : public TcpSocketBase
{
    
public:
  static TypeId GetTypeId (void); // TypeId: a unique identifier for an interdace, 身份证号 ; GetTypeId():Register this type.
  virtual TypeId GetInstanceTypeId () const;

  TcpSocketDerived (void);
  virtual ~TcpSocketDerived (void);

  Ptr<TcpCongestionOps> GetCongestionControlAlgorithm (); // 返回指向CC算法的指针
};

2.5 类TcpRlBase


class TcpRlBase : public TcpCongestionOps
{
    
public:
  /**
   * \brief Get the type ID.
   * \return the object TypeId
   */
  static TypeId GetTypeId (void);

  TcpRlBase ();

  /**
   * \brief Copy constructor.
   * \param sock object to copy.
   */
  TcpRlBase (const TcpRlBase& sock);

  ~TcpRlBase ();

  virtual std::string GetName () const;
  virtual uint32_t GetSsThresh (Ptr<const TcpSocketState> tcb, uint32_t bytesInFlight);
  virtual void IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked);
  virtual void PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked, const Time& rtt);
  virtual void CongestionStateSet (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCongState_t newState);
  virtual void CwndEvent (Ptr<TcpSocketState> tcb, const TcpSocketState::TcpCAEvent_t event);
  virtual Ptr<TcpCongestionOps> Fork ();

protected:
  static uint64_t GenerateUuid ();
  virtual void CreateGymEnv();
  void ConnectSocketCallbacks();

  // OpenGymEnv interface
  Ptr<TcpSocketBase> m_tcpSocket;
  Ptr<TcpGymEnv> m_tcpGymEnv;
};

2.6 类TcpRl


class TcpRl : public TcpRlBase
{
    
public:
  static TypeId GetTypeId (void);

  TcpRl ();
  TcpRl (const TcpRl& sock);
  ~TcpRl ();

  virtual std::string GetName () const;
private:
  virtual void CreateGymEnv();
  // OpenGymEnv env
  float m_reward {
    1.0};
  float m_penalty {
    -100.0};
};

2.7 类TcpRlTimeBased


class TcpRlTimeBased : public TcpRlBase
{
    
public:
  static TypeId GetTypeId (void);

  TcpRlTimeBased ();
  TcpRlTimeBased (const TcpRlTimeBased& sock);
  ~TcpRlTimeBased ();

  virtual std::string GetName () const;

private:
  virtual void CreateGymEnv();
  // OpenGymEnv env
  Time m_timeStep {
    MilliSeconds (100)};
};

} // namespace ns3

3 tcp-rl-env.h/cc

#ifndef TCP_RL_ENV_H
#define TCP_RL_ENV_H

#include "ns3/opengym-module.h"
#include "ns3/tcp-socket-base.h"
#include <vector>

namespace ns3 {
    

class Packet;
class TcpHeader;
class TcpSocketBase;
class Time;

class TcpGymEnv : public OpenGymEnv ...
class TcpEventGymEnv : public TcpGymEnv ...
class TcpTimeStepGymEnv : public TcpGymEnv ...
} // namespace ns3

#endif /* TCP_RL_ENV_H */

4 tcp_base.py

4.1 class Tcp(object)


class Tcp(object):
    """docstring for Tcp"""
    def __init__(self):
        super(Tcp, self).__init__()

    def set_spaces(self, obs, act):
        self.obsSpace = obs
        self.actSpace = act

    def get_action(self, obs, reward, done, info):
        pass

  1. 子类继承父类的构造函数:
    构造函数第一个参数都是self,若继承父类的构造函数,用super关键字:
  super(子类, self).__init__(参数1,参数2...)

4.2 class TcpEventBased(Tcp)

斜体样式

4.3 class TcpTimeBased(Tcp)

5 tcp_newreno.py

5.1 class TcpNewReno(TcpEventBased)

6 test_tcp.py

C++等作为编译型语言,要先将程序编译成二进制再运行,需要包含一个main()主类,作为程序入口。
而Python作为脚本语言,是动态的逐行解释运行,即从第一行开始运行,没有统一的入口。
使用if __name__ == '__main__':,是为了当文件作为主文件时,这部分语句就会执行;若作为被其他文件调用的模块,就不会执行。

import argparse
from ns3gym import ns3env
from tcp_base import TcpTimeBased
from tcp_newreno import TcpNewReno

__author__ = "Piotr Gawlowicz"
__copyright__ = "Copyright (c) 2018, Technische Universität Berlin"
__version__ = "0.1.0"
__email__ = "[email protected]"


parser = argparse.ArgumentParser(description='Start simulation script on/off')
parser.add_argument('--start',
                    type=int,
                    default=1,
                    help='Start ns-3 simulation script 0/1, Default: 1')
parser.add_argument('--iterations',
                    type=int,
                    default=1,
                    help='Number of iterations, Default: 1')
args = parser.parse_args()
startSim = bool(args.start)
iterationNum = int(args.iterations)

port = 5555
simTime = 10 # seconds
stepTime = 0.5  # seconds
seed = 12
simArgs = {
    "--duration": simTime,}
debug = False

env = ns3env.Ns3Env(port=port, stepTime=stepTime, startSim=startSim, simSeed=seed, simArgs=simArgs, debug=debug)
# simpler:
#env = ns3env.Ns3Env()
env.reset()

ob_space = env.observation_space
ac_space = env.action_space
print("Observation space: ", ob_space,  ob_space.dtype)
print("Action space: ", ac_space, ac_space.dtype)

stepIdx = 0
currIt = 0

def get_agent(obs):
    socketUuid = obs[0]
    tcpEnvType = obs[1]
    tcpAgent = get_agent.tcpAgents.get(socketUuid, None)
    if tcpAgent is None:
        if tcpEnvType == 0:
            # event-based = 0
            tcpAgent = TcpNewReno()
        else:
            # time-based = 1
            tcpAgent = TcpTimeBased()
        tcpAgent.set_spaces(get_agent.ob_space, get_agent.ac_space)
        get_agent.tcpAgents[socketUuid] = tcpAgent

    return tcpAgent

# initialize variable
get_agent.tcpAgents = {
    }
get_agent.ob_space = ob_space
get_agent.ac_space = ac_space

try:
    while True:
        print("Start iteration: ", currIt)
        obs = env.reset()
        reward = 0
        done = False
        info = None
        print("Step: ", stepIdx)
        print("---obs: ", obs)

        # get existing agent of create new TCP agent if needed
        tcpAgent = get_agent(obs)

        while True:
            stepIdx += 1
            action = tcpAgent.get_action(obs, reward, done, info)
            print("---action: ", action)

            print("Step: ", stepIdx)
            obs, reward, done, info = env.step(action)
            print("---obs, reward, done, info: ", obs, reward, done, info)

            # get existing agent of create new TCP agent if needed
            tcpAgent = get_agent(obs)

            if done:
                stepIdx = 0
                if currIt + 1 < iterationNum:
                    env.reset()
                break

        currIt += 1
        if currIt == iterationNum:
            break

except KeyboardInterrupt:
    print("Ctrl-C -> Exit")
finally:
    env.close()
    print("Done")
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bajiaoyu517/article/details/120273117

智能推荐

我的一个关于文件的程序 - [C语言]_fseek(fp,0l,2)-程序员宅基地

文章浏览阅读6.3k次。 2005-09-05我的一个关于文件的程序 - [C语言]#includevoid main(){char ch;FILE* fp;if((fp=fopen("test.txt","r"))==NULL){printf("error");exit(1);}fseek(fp,0L,2);while((fseek(fp,-1L,1))!=-1){ch=fgetc(fp);pu_fseek(fp,0l,2)

oracle 设置查询条数,SQL、MySQL、Oracle、 Sqlite、Informix数据库查询指定条数数据的方法...-程序员宅基地

文章浏览阅读674次。SQL查询前10条的方法为:select top X * from table_name--查询前X条记录,可以改成需要的数字,比如前10条。select top X * from table_name order by colum_name desc--按colum_name属性降序排序查询前X条记录,“order by” 后紧跟要排序的属性列名,其中desc表示降序,asc表示升序(默认也..._oracle怎么用语句设置查询结果数量

课程设计之第二次冲刺----第九天-程序员宅基地

文章浏览阅读58次。讨论成员:罗凯旋、罗林杰、吴伟锋、黎文衷讨论完善APP,调试功能。转载于:https://www.cnblogs.com/383237360q/p/5011594.html

favicon.ico 图标及时更新问题_win 软件开发 ico图标多久更新-程序员宅基地

文章浏览阅读5.4k次。首先看你 favicon.ico 图标文件引入路径是否正确然后 看ico文件能否正常打开,这两个没问题的话,在地址栏直接输入你的域名 http://xxx.com/favicon.ico 注意 此刻可能还是 之前的ico图标 不要着急 刷新一下 试试 完美解决 清除程序缓存_win 软件开发 ico图标多久更新

手工物理删除Oracle归档日志RMAN备份报错_rman 说明与资料档案库中在任何归档日志都不匹配-程序员宅基地

文章浏览阅读2.1k次。Oracle归档日志删除我们都都知道在controlfile中记录着每一个archivelog的相关信息,当然们在OS下把这些物理文件delete掉后,在我们的controlfile中仍然记录着这些archivelog的信息,在oracle的OEM管理器中有可视化的日志展现出,当我们手工清除 archive目录下的文件后,这些记录并没有被我们从controlfile中清除掉,也就是or_rman 说明与资料档案库中在任何归档日志都不匹配

命令提示符_命令提示符文件开头-程序员宅基地

文章浏览阅读706次。命令提示符:[ root@localhost桌面] #[用户名@主机名 当前所在位置] #(超级用户) KaTeX parse error: Expected 'EOF', got '#' at position 25: …用户: #̲ su 用户名 //切… su密码:[ root@cml桌面] #临时提升为root权限:# sudo 命令..._命令提示符文件开头

随便推点

android+打包+不同app,基于Gradle的Android应用打包实践-程序员宅基地

文章浏览阅读152次。0x01 基本项目结构使用Android Studio创建的Android项目会划分成三个层级:project : settings.gradle定义了构建应用时包含了哪些模块;build.gradle定义了适用于项目中所有模块的构建配置module : 可以是一个app类型的module,对应生成apk应用;也可以是一个lib类型的module,对应生成aar包. 每个module中包含的bui..._android多个应用 gradle 怎么打包指定的应用

qsort实现顺序与逆序/排整型,字符串数组,字符数组,结构体类型数组的名字排序,年龄排序等_qsort反向排序-程序员宅基地

文章浏览阅读599次,点赞12次,收藏11次。前言:通常我们排序都需要创建一个函数实现排序,但当我们排完整型数组时,想要排字符串呢?那需要重新创建一个函数,完善它的功能,进而实现排字符串,这样非常繁琐,但是有一个函数可以帮我们实现传什么,排什么;qsort的传参:(1️⃣,2️⃣,3️⃣,4️⃣) (首元素地址,排序的元素个数,每个元素的大小,指向比较两个元素的函数的指针)1️⃣2️⃣3️⃣4️⃣的传参方法,下面介绍:…整型数组:......_qsort反向排序

MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法_mvc 不验证登陆-程序员宅基地

文章浏览阅读355次。MVC绕过登陆界面验证时HttpContext.Current.User.Identity.Name取值为空问题解决方法_mvc 不验证登陆

Java中DO、DTO、BO、AO、VO、POJO、Query 命名规范_dto命名规范-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏8次。1.分层领域模型规约: • DO( Data Object):与数据库表结构一一对应,通过DAO层向上传输数据源对象。 • DTO( Data Transfer Object):数据传输对象,Service或Manager向外传输的对象。 • BO( Business Object):业务对象。 由Service层输出的封装业务逻辑的对象。 • AO( Ap..._dto命名规范

1015. Reversible Primes (20) PAT甲级刷题_pat甲级1015-程序员宅基地

文章浏览阅读91次。A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a pr..._pat甲级1015

ABAP接口之Http发送json报文_abap http 转换为json输出-程序员宅基地

文章浏览阅读1.5k次。ABAP接口之Http发送json报文abap 调用http 发送 json 测试函数SE11创建结构:zsmlscpnoticeSE37创建函数:zqb_test_http_fuc1FUNCTIONzqb_test_http_fuc1.*"----------------------------------------------------------------..._abap http 转换为json输出