从ns-3.33版本开始,NS3支持CUBIC。
从ns-3.34版本开始,支持BBR v1。
呜呜呜,感谢前人!!!
我用的是ns-3.34, 代码直接改自ns-3.34/examples/tcp/tcp-linux-reno.cc, 呃。也不用改,直接拷贝到scratch文件夹下运行就行…不过运行的只有reno,可以稍微,稍微改一丢丢:
可以运行:NewReno,Reno,BBR, CUBIC,Veno等拥塞控制算法

// Network topology
//
//       n0 ---------- n1 ---------- n2 ---------- n3
//            10 Mbps       1 Mbps        10 Mbps
//             1 ms         10 ms          1 ms#include <iostream>
#include <string>
#include <fstream>
#include <sys/stat.h>
#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/traffic-control-module.h"
#include "ns3/flow-monitor-module.h"using namespace ns3;
std::string dir = "results/";
Time stopTime = Seconds (200);
uint32_t segmentSize = 524;// Calculate throughput
uint32_t prev = 0;
Time prevTime = Seconds (0);
static void
TraceThroughput (Ptr<FlowMonitor> monitor)
{FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();auto itr = stats.begin ();Time curTime = Now ();std::ofstream thr (dir + "/throughput.dat", std::ios::out | std::ios::app);thr <<  curTime << " " << 8 * (itr->second.txBytes - prev) / (1000 * 1000 * (curTime.GetSeconds () - prevTime.GetSeconds ())) << std::endl;prevTime = curTime;prev = itr->second.txBytes;Simulator::Schedule (Seconds (0.2), &TraceThroughput, monitor);
}// Function to check queue length of Router 1
void
CheckQueueSize (Ptr<QueueDisc> queue)
{uint32_t qSize = queue->GetCurrentSize ().GetValue ();// Check queue size every 1/100 of a secondSimulator::Schedule (Seconds (0.001), &CheckQueueSize, queue);std::ofstream fPlotQueue (std::stringstream (dir + "queue-size.dat").str ().c_str (), std::ios::out | std::ios::app);fPlotQueue << Simulator::Now ().GetSeconds () << " " << qSize << std::endl;fPlotQueue.close ();
}// Function to trace change in cwnd at n0
static void
CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
{std::ofstream fPlotQueue (dir + "cwndTraces/n0.dat", std::ios::out | std::ios::app);fPlotQueue << Simulator::Now ().GetSeconds () << " " << newCwnd / segmentSize << std::endl;fPlotQueue.close ();
}// Function to calculate drops in a particular Queue
static void
DropAtQueue (Ptr<OutputStreamWrapper> stream, Ptr<const QueueDiscItem> item)
{*stream->GetStream () << Simulator::Now ().GetSeconds () << " 1" << std::endl;
}// Trace Function for cwnd
void
TraceCwnd (uint32_t node, uint32_t cwndWindow,Callback <void, uint32_t, uint32_t> CwndTrace)
{Config::ConnectWithoutContext ("/NodeList/" + std::to_string (node) + "/$ns3::TcpL4Protocol/SocketList/" + std::to_string (cwndWindow) + "/CongestionWindow", CwndTrace);
}// Function to install BulkSend application
void InstallBulkSend (Ptr<Node> node, Ipv4Address address, uint16_t port, std::string socketFactory,uint32_t nodeId, uint32_t cwndWindow,Callback <void, uint32_t, uint32_t> CwndTrace)
{BulkSendHelper source (socketFactory, InetSocketAddress (address, port));source.SetAttribute ("MaxBytes", UintegerValue (0));ApplicationContainer sourceApps = source.Install (node);sourceApps.Start (Seconds (10.0));Simulator::Schedule (Seconds (10.0) + Seconds (0.001), &TraceCwnd, nodeId, cwndWindow, CwndTrace);sourceApps.Stop (stopTime);
}// Function to install sink application
void InstallPacketSink (Ptr<Node> node, uint16_t port, std::string socketFactory)
{PacketSinkHelper sink (socketFactory, InetSocketAddress (Ipv4Address::GetAny (), port));ApplicationContainer sinkApps = sink.Install (node);sinkApps.Start (Seconds (10.0));sinkApps.Stop (stopTime);
}int main (int argc, char *argv[])
{uint32_t stream = 1;std::string socketFactory = "ns3::TcpSocketFactory";//如果要用不同的拥塞控制算法改下面的就行// std::string tcpTypeId = "ns3::TcpLinuxReno";// std::string tcpTypeId = "ns3::TcpNewReno";// std::string tcpTypeId = "ns3::TcpVeno";// std::string tcpTypeId = "ns3::TcpBic";// std::string tcpTypeId = "ns3::TcpBbr";std::string tcpTypeId = "ns3::TcpCubic";std::string qdiscTypeId = "ns3::FifoQueueDisc";bool isSack = true;uint32_t delAckCount = 1;std::string recovery = "ns3::TcpClassicRecovery";CommandLine cmd;cmd.AddValue ("tcpTypeId", "TCP variant to use (e.g., ns3::TcpNewReno, ns3::TcpLinuxReno, etc.)", tcpTypeId);cmd.AddValue ("qdiscTypeId", "Queue disc for gateway (e.g., ns3::CoDelQueueDisc)", qdiscTypeId);cmd.AddValue ("segmentSize", "TCP segment size (bytes)", segmentSize);cmd.AddValue ("delAckCount", "Delayed ack count", delAckCount);cmd.AddValue ("enableSack", "Flag to enable/disable sack in TCP", isSack);cmd.AddValue ("stopTime", "Stop time for applications / simulation time will be stopTime", stopTime);cmd.AddValue ("recovery", "Recovery algorithm type to use (e.g., ns3::TcpPrrRecovery", recovery);cmd.Parse (argc, argv);TypeId qdTid;NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (qdiscTypeId, &qdTid), "TypeId " << qdiscTypeId << " not found");// Set recovery algorithm and TCP variantConfig::SetDefault ("ns3::TcpL4Protocol::RecoveryType", TypeIdValue (TypeId::LookupByName (recovery)));if (tcpTypeId.compare ("ns3::TcpWestwoodPlus") == 0){// TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood hereConfig::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));// the default protocol type in ns3::TcpWestwood is WESTWOODConfig::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));}else{TypeId tcpTid;NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (tcpTypeId, &tcpTid), "TypeId " << tcpTypeId << " not found");Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (tcpTypeId)));}// Create nodesNodeContainer leftNodes, rightNodes, routers;routers.Create (2);leftNodes.Create (1);rightNodes.Create (1);std::vector <NetDeviceContainer> leftToRouter;std::vector <NetDeviceContainer> routerToRight;// Create the point-to-point link helpers and connect two router nodesPointToPointHelper pointToPointRouter;pointToPointRouter.SetDeviceAttribute  ("DataRate", StringValue ("1Mbps"));pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("10ms"));NetDeviceContainer r1r2ND = pointToPointRouter.Install (routers.Get (0), routers.Get (1));// Create the point-to-point link helpers and connect leaf nodes to routerPointToPointHelper pointToPointLeaf;pointToPointLeaf.SetDeviceAttribute    ("DataRate", StringValue ("10Mbps"));pointToPointLeaf.SetChannelAttribute   ("Delay", StringValue ("1ms"));leftToRouter.push_back (pointToPointLeaf.Install (leftNodes.Get (0), routers.Get (0)));routerToRight.push_back (pointToPointLeaf.Install (routers.Get (1), rightNodes.Get (0)));InternetStackHelper internetStack;internetStack.Install (leftNodes);internetStack.Install (rightNodes);internetStack.Install (routers);// Assign IP addresses to all the network devicesIpv4AddressHelper ipAddresses ("10.0.0.0", "255.255.255.0");Ipv4InterfaceContainer r1r2IPAddress = ipAddresses.Assign (r1r2ND);ipAddresses.NewNetwork ();std::vector <Ipv4InterfaceContainer> leftToRouterIPAddress;leftToRouterIPAddress.push_back (ipAddresses.Assign (leftToRouter [0]));ipAddresses.NewNetwork ();std::vector <Ipv4InterfaceContainer> routerToRightIPAddress;routerToRightIPAddress.push_back (ipAddresses.Assign (routerToRight [0]));Ipv4GlobalRoutingHelper::PopulateRoutingTables ();// Set default sender and receiver buffer size as 1MBConfig::SetDefault ("ns3::TcpSocket::SndBufSize", UintegerValue (1 << 20));Config::SetDefault ("ns3::TcpSocket::RcvBufSize", UintegerValue (1 << 20));// Set default initial congestion window as 10 segmentsConfig::SetDefault ("ns3::TcpSocket::InitialCwnd", UintegerValue (10));// Set default delayed ack count to a specified valueConfig::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (delAckCount));// Set default segment size of TCP packet to a specified valueConfig::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (segmentSize));// Enable/Disable SACK in TCPConfig::SetDefault ("ns3::TcpSocketBase::Sack", BooleanValue (isSack));// Create directories to store dat filesstruct stat buffer;int retVal;if ((stat (dir.c_str (), &buffer)) == 0){std::string dirToRemove = "rm -rf " + dir;retVal = system (dirToRemove.c_str ());NS_ASSERT_MSG (retVal == 0, "Error in return value");}std::string dirToSave = "mkdir -p " + dir;retVal = system (dirToSave.c_str ());NS_ASSERT_MSG (retVal == 0, "Error in return value");retVal = system ((dirToSave + "/pcap/").c_str ());NS_ASSERT_MSG (retVal == 0, "Error in return value");retVal = system ((dirToSave + "/queueTraces/").c_str ());NS_ASSERT_MSG (retVal == 0, "Error in return value");retVal = system ((dirToSave + "/cwndTraces/").c_str ());NS_ASSERT_MSG (retVal == 0, "Error in return value");NS_UNUSED (retVal);// Set default parameters for queue disciplineConfig::SetDefault (qdiscTypeId + "::MaxSize", QueueSizeValue (QueueSize ("100p")));// Install queue discipline on routerTrafficControlHelper tch;tch.SetRootQueueDisc (qdiscTypeId);QueueDiscContainer qd;tch.Uninstall (routers.Get (0)->GetDevice (0));qd.Add (tch.Install (routers.Get (0)->GetDevice (0)).Get (0));// Enable BQLtch.SetQueueLimits ("ns3::DynamicQueueLimits");// Calls function to check queue sizeSimulator::ScheduleNow (&CheckQueueSize, qd.Get (0));AsciiTraceHelper asciiTraceHelper;Ptr<OutputStreamWrapper> streamWrapper;// Create dat to store packets dropped and marked at the routerstreamWrapper = asciiTraceHelper.CreateFileStream (dir + "/queueTraces/drop-0.dat");qd.Get (0)->TraceConnectWithoutContext ("Drop", MakeBoundCallback (&DropAtQueue, streamWrapper));// Install packet sink at receiver sideuint16_t port = 50000;InstallPacketSink (rightNodes.Get (0), port, "ns3::TcpSocketFactory");// Install BulkSend applicationInstallBulkSend (leftNodes.Get (0), routerToRightIPAddress [0].GetAddress (1), port, socketFactory, 2, 0, MakeCallback (&CwndChange));// Enable PCAP on all the point to point interfacespointToPointLeaf.EnablePcapAll (dir + "pcap/ns-3", true);FlowMonitorHelper flowmon;Ptr<FlowMonitor> monitor = flowmon.InstallAll ();Simulator::Schedule (Seconds (0 + 0.000001), &TraceThroughput, monitor);Simulator::Stop (stopTime);Simulator::Run ();// Store queue stats in a filestd::ofstream myfile;myfile.open (dir + "queueStats.txt", std::fstream::in | std::fstream::out | std::fstream::app);myfile << std::endl;myfile << "Stat for Queue 1";myfile << qd.Get (0)->GetStats ();myfile.close ();// Store configuration of the simulation in a filemyfile.open (dir + "config.txt", std::fstream::in | std::fstream::out | std::fstream::app);myfile << "qdiscTypeId " << qdiscTypeId << "\n";myfile << "stream  " << stream << "\n";myfile << "segmentSize " << segmentSize << "\n";myfile << "delAckCount " << delAckCount << "\n";myfile << "stopTime " << stopTime.As (Time::S) << "\n";myfile.close ();Simulator::Destroy ();return 0;
}

直接用trace的cwnd的结果绘图:

  • CUBIC

  • Reno

  • bbr

    咦!画的咋这么丑呢…

NS3运行Reno,BBR,CUBIC等拥塞控制算法相关推荐

  1. TCP拥塞控制算法-从BIC到CUBIC

    摘要:tcp就是乘性加,然后加性加接近最大码率.BIC优化了,变成折半加,不是加一个rtt,这样加的速度变快,同时进入下一周期做了图形对称.cubic完全根据bic的图形,将图形转成代数,带入3个关键 ...

  2. 拥塞控制算法——BBR

    拥塞控制算法--BBR 目录 BBR产生的背景 TCP算法存在的问题 BBR算法的特点及核心 BBR算法基本原理 BBR结构图 即时带宽的计算 BDP BBR状态机 BBR算法的优缺点 抗丢包能力强 ...

  3. 漫谈TCP拥塞控制算法(2)

    刚看了会儿关于流体模型和排队论的东西,觉得太高端了,但实际能做的事情又很少,有感而发,写篇随笔. TCP的拥塞控制远不止Linux内核源码树的net/ipv4目录下的那些,事实上那些算法误导了算法的实 ...

  4. Tcp拥塞控制算法入门:分类及介绍( Reno bic Cubic vegas Bbr)

    目录 一.Tcp拥塞控制算法的发展历程及种类 二.Reno算法 三.bic算法.Cubic算法 3.1bic算法 3.2Cubic算法 四.vegas算法 五.Bbr算法 本文介绍了一些基础的拥塞控制 ...

  5. BBR/Vegas/CUBIC拥塞控制算法对比

    文章目录 环境 结果统计 结果分析 总结 发生丢包时的反应 环境 基于本地通信,通过tcconfig设置丢包率,rtt和带宽上限. 测试程序阻塞发包. 结果统计 Vegas BBR CUBIC 不做任 ...

  6. Google BBR拥塞控制算法背后的数学解释 | 深度

    参加 2019 Python开发者日,请扫码咨询 ↑↑↑ 作者 | 赵亚 转载自CSDN网站 杭州待了一段时间,回到深圳过国庆假期,无奈温州皮鞋?厂老板过节要回温州和上海,不在深圳,也就没有见着,非常 ...

  7. Google's BBR TCP拥塞控制算法的四个变速引擎

    台风海马来临前的两个几乎通宵的夜晚,我用一篇关于BBR算法的文章的迎接海马!公司在昨晚响应深圳市停工,停课号召,发布了在家办公(请注意,不是放假...)通知...其实吧,我觉得停电才是王道,你觉得呢? ...

  8. Google BBR拥塞控制算法背后的数学解释

    杭州待了一段时间,回到深圳过国庆假期,无奈温州皮鞋?厂老板过节要回温州和上海,不在深圳,也就没有见着,非常遗憾! 国庆节当天,就写这个了.经理不会弹琴,但是经理会弹琴. 我原本可能会在想国庆节的凌晨到 ...

  9. Google BBR拥塞控制算法模型初探

    女主宣言 本文出自于ADDOPS团队,该文章是一篇兴趣触发的探索性文章,作者是一名刚毕业的小鲜肉,买新系统,去自己探索bbr算法,难能可贵,并且给出了详细的部署步骤,也能让大家在兴趣之余跟着步骤试一把 ...

最新文章

  1. 接口 500_python接口的自我修炼之路
  2. 关于IIS服务启动失败的问题:“IIS提示‘另一个程序正在使用此文件,进程无法访问’”,的解决方法...
  3. 2017视频监控行业应用趋势与市场发展分析
  4. azure第一个月_MLOps:两个Azure管道的故事
  5. 面向对象之编写一个完整的类
  6. t3s java_关于JAVA的this关键字
  7. [转载] Python 递归函数
  8. M文件---脚本与函数
  9. php 框架测试,PHP测试框架PHPUnit组织测试操作示例
  10. 关于图像格式jpg、gif、png、svg、bmp、WebP的区别以及应用场景
  11. 精准医学:循环肿瘤DNA在检测非小细胞肺癌患者体细胞突变及跟踪肿瘤进展中的作用|精准治疗
  12. 下载bilibili视频
  13. java识别图片文字_java 实现图片的文字识别
  14. antDesign 自定义分页样式
  15. JAVA个版本新特性
  16. java实现客户端 与服务端的对话_Socket实现单客户端与服务器对话功能
  17. 中国有多少个省,多少个直辖市,多少个特别行政区,多少个自治区
  18. 知道创宇研发技能表v3.1
  19. Vant Uploader 文件上传
  20. TX2刷机和使用常见问题

热门文章

  1. 51单片机 | 红外遥控实验
  2. 毕业设计 基于单片机的万能红外遥控器 - 物联网 嵌入式
  3. 8bit音乐的一些相关知识
  4. deel t410安装_用DEEL-LIP构建Lipschitz约束网络
  5. Sendcloud邮件发送api拼接问题
  6. 技术经理成长复盘-要懂一些项目管理的知识
  7. 对标西湖大学?中国芯片首富捐资200亿办高校!地址选在了这里
  8. 什么是阿里云服务器ECS?阿里云服务器的用途
  9. GitHub快速上手指南
  10. crt、cer类型证书转换成bks