I want to trace TCP congestion window in ns3.26. I configure the MaxPackets 5. But I did not see the packets dropped. So the traced cwnd was keeping increasing. This behavior confused me a lot at first and got me stuck for hours.But I did see packets dropped and decreased cwnd in ns3.13. It seems its the traffic control module that caused this problem. But at the time, I am not finding why. I disabled the TC and it works.The codes are in the following:
/* -*- 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
*
*/
//
// Network topology
//
// 10Mb/s, 10ms 10Mb/s, 10ms
// n0-----------------n1-----------------n2
//
//
// - Tracing of queues and packet receptions to file
// "tcp-large-transfer.tr"
// - pcap traces also generated in the following files
// "tcp-large-transfer-$n-$i.pcap" where n and i represent node and interface
// numbers respectively
// Usage (e.g.): ./waf --run tcp-cwnd
#include <iostream>
#include <fstream>
#include <string>
#include<stdio.h>
#include<unistd.h>
#include "ns3/core-module.h"
#include "ns3/applications-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/traffic-control-module.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("TCPCwndTrace");
// The number of bytes to send in this simulation.
static const uint32_t totalTxBytes = 2000000000;
static uint32_t currentTxBytes = 0;
// Perform series of 1040 byte writes (this is a multiple of 26 since
// we want to detect data splicing in the output stream)
static const uint32_t writeSize = 1040;
static const double simDuration = 30.0;
uint8_t data[writeSize];
static fstream mTcpWindowFile;
// These are for starting the writing process, and handling the sending
// socket's notification upcalls (events). These two together more or less
// implement a sending "Application", although not a proper ns3::Application
// subclass.
void StartFlow (Ptr<Socket>, Ipv4Address, uint16_t);
void WriteUntilBufferFull (Ptr<Socket>, uint32_t);
void OpenCwndTraceFile(std::string filename);
void CloseTcpWindowTraceFile();
void TcpWindowTrace(uint32_t oldval, uint32_t newval);
static void
CwndTracer (uint32_t oldval, uint32_t newval)
{
TcpWindowTrace(oldval,newval);
NS_LOG_INFO (Simulator::Now().GetSeconds()<<"Moving cwnd from " << oldval << " to " << newval);
}
void OpenCwndTraceFile(std::string filename)
{
char buf[FILENAME_MAX];
string path = string (getcwd(buf, FILENAME_MAX)) + "/traces/" + filename;
mTcpWindowFile.open(path.c_str(), fstream::out);
sprintf (buf, "%16s %16s",
"Time", "Window");
NS_ASSERT_MSG (mTcpWindowFile.is_open(), "No trace file to write to");
mTcpWindowFile << buf << std::endl;
}
void CloseTcpWindowTraceFile()
{
if (mTcpWindowFile.is_open())
mTcpWindowFile.close();
else
NS_LOG_ERROR("Open file not found");
}
void TcpWindowTrace(uint32_t oldval, uint32_t newval)
{
char line [255];
oldval=oldval;
sprintf (line, "%16f %16f",
Simulator::Now().GetSeconds(),(double)newval/1460.0);
NS_ASSERT_MSG (mTcpWindowFile.is_open(), "No trace file to write to");
mTcpWindowFile << line << std::endl;
}
int main (int argc, char *argv[])
{
// Users may find it convenient to turn on explicit debugging
// for selected modules; the below lines suggest how to do this
// LogComponentEnable("TcpL4Protocol", LOG_LEVEL_ALL);
// LogComponentEnable("TcpSocketImpl", LOG_LEVEL_ALL);
// LogComponentEnable("PacketSink", LOG_LEVEL_ALL);
//LogComponentEnable("TCPCwndTrace", LOG_LEVEL_ALL);
LogComponentEnable("Queue", LOG_LEVEL_ALL);
uint32_t queueSize=5;
CommandLine cmd;
cmd.Parse (argc, argv);
// initialize the tx buffer.
for(uint32_t i = 0; i < writeSize; ++i)
{
char m = toascii (97 + i % 26);
data[i] = m;
}
// Here, we will explicitly create three nodes. The first container contains
// nodes 0 and 1 from the diagram above, and the second one contains nodes
// 1 and 2. This reflects the channel connectivity, and will be used to
// install the network interfaces and connect them with a channel.
NodeContainer n0n1;
n0n1.Create (2);
NodeContainer n1n2;
n1n2.Add (n0n1.Get (1));
n1n2.Create (1);
InternetStackHelper stack;
stack.Install (n0n1);
stack.Install (n1n2.Get(1));
std::string cwndtracefile="cwnd.tr";
OpenCwndTraceFile(cwndtracefile);
// We create the channels first without any IP addressing information
// First make and configure the helper, so that it will put the appropriate
// attributes on the network interfaces and channels we are about to install.
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue("1Mbps"));
p2p.SetChannelAttribute ("Delay", StringValue("25ms"));
p2p.SetQueue ("ns3::DropTailQueue", "MaxPackets",UintegerValue(5),"Mode",EnumValue (Queue::QueueMode::QUEUE_MODE_PACKETS));
// And then install devices and channels connecting our topology.
NetDeviceContainer dev0 = p2p.Install (n0n1);
NetDeviceContainer dev1 = p2p.Install (n1n2);
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.3.0", "255.255.255.0");
ipv4.Assign (dev0);
ipv4.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev1);
TrafficControlHelper tch;
tch.Uninstall (dev0);
tch.Uninstall (dev1);
// and setup ip routing tables to get total ip-level connectivity.
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
queueSize=queueSize;
//UintegerValue queuePackets;
//PointToPointNetDevice *dptr;
// dptr=dynamic_cast<PointToPointNetDevice*>(PeekPointer(dev0.Get(0)));
// dptr->GetQueue()->GetAttribute("MaxPackets",queuePackets);
//printf("queueSize %ld\n",queuePackets.Get());
uint16_t servPort = 50000;
// Create a packet sink to receive these packets on n2...
PacketSinkHelper sink ("ns3::TcpSocketFactory",
InetSocketAddress (Ipv4Address::GetAny (), servPort));
ApplicationContainer apps = sink.Install (n1n2.Get (1));
apps.Start (Seconds (0.0));
apps.Stop (Seconds (simDuration));
Ptr<Socket> localSocket =
Socket::CreateSocket (n0n1.Get (0), TcpSocketFactory::GetTypeId ());
localSocket->Bind ();
localSocket->SetAttribute("SegmentSize",UintegerValue(1460));
// Trace changes to the congestion window
Config::ConnectWithoutContext ("/NodeList/0/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow", MakeCallback (&CwndTracer));
// ...and schedule the sending "Application"; This is similar to what an
// ns3::Application subclass would do internally.
Simulator::ScheduleNow (&StartFlow, localSocket,
ipInterfs.GetAddress (1), servPort);
// One can toggle the comment for the following line on or off to see the
// effects of finite send buffer modelling. One can also change the size of
// said buffer.
//localSocket->SetAttribute("SndBufSize", UintegerValue(4096));
//Ask for ASCII and pcap traces of network traffic
//AsciiTraceHelper ascii;
//p2p.EnableAsciiAll (ascii.CreateFileStream ("tcp-large-transfer.tr"));
//p2p.EnablePcapAll ("tcp-large-transfer");
// Finally, set up the simulator to run. The 1000 second hard limit is a
// failsafe in case some change above causes the simulation to never end
Simulator::Stop (Seconds (simDuration+10.0));
Simulator::Run ();
localSocket->Close ();
CloseTcpWindowTraceFile();
Simulator::Destroy ();
return 0;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//begin implementation of sending "Application"
void StartFlow (Ptr<Socket> localSocket,
Ipv4Address servAddress,
uint16_t servPort)
{
NS_LOG_LOGIC ("Starting flow at time " << Simulator::Now ().GetSeconds ());
localSocket->Connect (InetSocketAddress (servAddress, servPort)); //connect
// tell the tcp implementation to call WriteUntilBufferFull again
// if we blocked and new tx buffer space becomes available
localSocket->SetSendCallback (MakeCallback (&WriteUntilBufferFull));
WriteUntilBufferFull (localSocket, localSocket->GetTxAvailable ());
}
static uint32_t writecount=0;
void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace)
{
while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable () > 0)
{
uint32_t left = totalTxBytes - currentTxBytes;
uint32_t dataOffset = currentTxBytes % writeSize;
uint32_t toWrite = writeSize - dataOffset;
toWrite = std::min (toWrite, left);
toWrite = std::min (toWrite, localSocket->GetTxAvailable ());
writecount++;
int amountSent = localSocket->Send (&data[dataOffset], toWrite, 0);
if(amountSent < 0)
{
// we will be called again when new tx space becomes available.
return;
}
currentTxBytes += amountSent;
}
NS_LOG_INFO("write count "<<writecount);
}
add the the wscript code in the folder scratch
obj = bld.create_ns3_program('tcpcwnd',
['point-to-point', 'applications', 'internet','traffic-control'])
obj.source = 'tcpcwnd.cc'