运行
# 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
基本接口
- 例如在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
- 任何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 Window
和Slow 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 <gawlowicz.p@gmail.com>
* 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 随机变量
作用:生成随机数,使仿真结果有一定的随机性。
- 播种与独立重复
SeedManager::SetSeed (1); // 设置种子,参数不变,生成的随机数也不变
SeedManager::SetRun (run); // 设置运行标识
- 随机变量
- ==随机数生成器(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);
拓展:
- 构造哑铃型拓扑 PointToPointDumbbellHelper
ns3::PointToPointDumbbellHelper::PointToPointDumbbellHelper ( uint32_t nLeftLeaf,
PointToPointHelper leftHelper,
uint32_t nRightLeaf,
PointToPointHelper rightHelper,
PointToPointHelper bottleneckHelper
)
- 构造星型拓扑 PointToPointStrarHelper
ns3::PointToPointStarHelper::PointToPointStarHelper ( uint32_t numSpokes,
PointToPointHelper p2pHelper
)
- 构造网络型拓扑 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
- 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 系统是否通过接口发现事件动态更新全局路由。
- 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
- 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
class TcpSocketDerived : public TcpSocketBase
{
public:
static TypeId GetTypeId (void);
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
- 子类继承父类的构造函数:
构造函数第一个参数都是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__ = "gawlowicz@tkn.tu-berlin.de"
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")