22 #include "ns3/abort.h"
24 #include "ns3/pcap-file.h"
25 #include "ns3/config.h"
26 #include "ns3/string.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/data-rate.h"
29 #include "ns3/inet-socket-address.h"
30 #include "ns3/point-to-point-helper.h"
31 #include "ns3/internet-stack-helper.h"
32 #include "ns3/ipv4-global-routing-helper.h"
33 #include "ns3/ipv4-address-helper.h"
34 #include "ns3/packet-sink-helper.h"
35 #include "ns3/tcp-socket-factory.h"
36 #include "ns3/node-container.h"
37 #include "ns3/simulator.h"
38 #include "ns3/error-model.h"
39 #include "ns3/pointer.h"
40 #include "ns3tcp-socket-writer.h"
41 #include "ns3/tcp-westwood.h"
47 const bool WRITE_VECTORS =
false;
48 const bool WRITE_LOGGING =
false;
49 const uint32_t PCAP_LINK_TYPE = 1187373557;
50 const uint32_t PCAP_SNAPLEN = 64;
65 virtual void DoSetup (
void);
66 virtual void DoRun (
void);
67 virtual void DoTeardown (
void);
70 std::string m_pcapFilename;
73 uint32_t m_totalTxBytes;
74 uint32_t m_currentTxBytes;
79 std::string m_tcpModel;
82 void CwndTracer (uint32_t oldval, uint32_t newval);
83 void WriteUntilBufferFull (
Ptr<Socket> localSocket, uint32_t txSpace);
90 Ns3TcpLossTestCase::Ns3TcpLossTestCase ()
91 :
TestCase (
"Check the operation of the TCP state machine for several cases"),
93 m_totalTxBytes (200000),
95 m_writeVectors (WRITE_VECTORS),
96 m_writeResults (false),
97 m_writeLogging (WRITE_LOGGING),
99 m_tcpModel (
"ns3::TcpWestwood")
103 Ns3TcpLossTestCase::Ns3TcpLossTestCase (std::string tcpModel, uint32_t testCase)
104 :
TestCase (
"Check the behaviour of TCP upon packet losses"),
105 m_testCase (testCase),
106 m_totalTxBytes (200000),
107 m_currentTxBytes (0),
108 m_writeVectors (WRITE_VECTORS),
109 m_writeResults (false),
110 m_writeLogging (WRITE_LOGGING),
111 m_needToClose (true),
112 m_tcpModel (tcpModel)
123 std::ostringstream oss;
124 oss <<
"/response-vectors/ns3tcp-loss-" << m_tcpModel << m_testCase <<
"-response-vectors.pcap";
125 m_pcapFilename = CreateDataDirFilename(oss.str ());
129 m_pcapFile.
Open (m_pcapFilename, std::ios::out|std::ios::binary);
130 m_pcapFile.
Init (PCAP_LINK_TYPE, PCAP_SNAPLEN);
134 m_pcapFile.
Open (m_pcapFilename, std::ios::in|std::ios::binary);
135 NS_ABORT_MSG_UNLESS (m_pcapFile.GetDataLinkType () == PCAP_LINK_TYPE,
"Wrong response vectors in directory");
166 Time tNow = Simulator::Now ();
170 uint8_t *buf =
new uint8_t[size];
173 m_pcapFile.
Write (uint32_t (tMicroSeconds / 1000000),
174 uint32_t (tMicroSeconds % 1000000),
185 uint8_t expected[PCAP_SNAPLEN];
186 uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
187 m_pcapFile.
Read (expected,
sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen);
191 uint8_t *actual =
new uint8_t[readLen];
194 uint32_t result = memcmp (actual, expected, readLen);
203 NS_TEST_EXPECT_MSG_EQ (result, 0,
"Expected data comparison error");
209 Ns3TcpLossTestCase::CwndTracer (uint32_t oldval, uint32_t newval)
213 *(m_osw->
GetStream ()) <<
"Moving cwnd from " << oldval <<
" to " << newval
214 <<
" at time " << Simulator::Now ().GetSeconds ()
215 <<
" seconds" << std::endl;
222 Ns3TcpLossTestCase::WriteUntilBufferFull (
Ptr<Socket> localSocket, uint32_t txSpace)
224 while (m_currentTxBytes < m_totalTxBytes)
226 uint32_t left = m_totalTxBytes - m_currentTxBytes;
227 uint32_t dataOffset = m_currentTxBytes % 1040;
228 uint32_t toWrite = 1040 - dataOffset;
230 toWrite = std::min (toWrite, left);
231 toWrite = std::min (toWrite, txAvail);
238 std::clog <<
"Submitting " << toWrite
239 <<
" bytes to TCP socket" << std::endl;
241 int amountSent = localSocket->
Send (0, toWrite, 0);
243 m_currentTxBytes += amountSent;
249 std::clog <<
"Close socket at "
250 << Simulator::Now ().GetSeconds () << std::endl;
252 localSocket->
Close ();
253 m_needToClose =
false;
258 Ns3TcpLossTestCase::StartFlow (
Ptr<Socket> localSocket,
264 std::clog <<
"Starting flow at time "
265 << Simulator::Now ().GetSeconds () << std::endl;
272 (&Ns3TcpLossTestCase::WriteUntilBufferFull,
274 WriteUntilBufferFull (localSocket, localSocket->
GetTxAvailable ());
288 std::ostringstream tcpModel;
289 tcpModel <<
"ns3::Tcp" << m_tcpModel;
290 if (m_tcpModel.compare(
"WestwoodPlus") == 0)
292 Config::SetDefault(
"ns3::TcpL4Protocol::SocketType",
294 Config::SetDefault(
"ns3::TcpWestwood::ProtocolType",
299 Config::SetDefault (
"ns3::TcpL4Protocol::SocketType",
303 Config::SetDefault (
"ns3::TcpSocket::SegmentSize",
UintegerValue (1000));
304 Config::SetDefault (
"ns3::TcpSocket::DelAckCount",
UintegerValue (1));
346 ipv4.
SetBase (
"10.1.3.0",
"255.255.255.0");
348 ipv4.
SetBase (
"10.1.2.0",
"255.255.255.0");
352 Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
359 uint16_t servPort = 50000;
369 Ptr<Socket> localSocket = Socket::CreateSocket (s1r1.
Get (0), TcpSocketFactory::GetTypeId ());
370 localSocket->
Bind ();
371 Simulator::ScheduleNow (&Ns3TcpLossTestCase::StartFlow,
377 Config::Connect (
"/NodeList/0/$ns3::Ipv4L3Protocol/Tx",
380 Config::ConnectWithoutContext
381 (
"/NodeList/0/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
387 std::list<uint32_t> sampleList;
395 sampleList.push_back (16);
398 sampleList.push_back (16);
399 sampleList.push_back (17);
402 sampleList.push_back (16);
403 sampleList.push_back (17);
404 sampleList.push_back (18);
407 sampleList.push_back (16);
408 sampleList.push_back (17);
409 sampleList.push_back (18);
410 sampleList.push_back (19);
413 NS_FATAL_ERROR (
"Program fatal error: loss value " << m_testCase <<
" not supported.");
426 std::ostringstream oss;
427 oss <<
"tcp-loss-" << m_tcpModel << m_testCase <<
"-test-case";
434 std::ostringstream oss2;
435 oss2 <<
"src/test/ns3tcp/Tcp" << m_tcpModel <<
"." << m_testCase <<
".log";
440 *(m_osw->
GetStream ()) << std::setprecision (9) << std::fixed;
446 Simulator::Stop (
Seconds (1000));
448 Simulator::Destroy ();
457 Ns3TcpLossTestSuite::Ns3TcpLossTestSuite ()
461 Packet::EnablePrinting ();
uint32_t RemoveHeader(Header &header)
holds a vector of ns3::Application pointers.
void LogComponentEnableAll(enum LogLevel level)
Manage ASCII trace files for device models.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
hold variables of type string
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
#define NS_ASSERT(condition)
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
#define NS_LOG_COMPONENT_DEFINE(name)
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
#define NS_FATAL_ERROR(msg)
fatal error handling
Class for representing data rates.
virtual void DoRun(void)
Implementation to actually run this test case.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
hold variables of type 'enum'
int64_t GetMicroSeconds(void) const
void InstallAll(void) const
hold objects of type ns3::Time
void Read(uint8_t *const data, uint32_t maxBytes, uint32_t &tsSec, uint32_t &tsUsec, uint32_t &inclLen, uint32_t &origLen, uint32_t &readLen)
Read next packet from file.
Hold an unsigned integer type.
holds a vector of ns3::NetDevice pointers
hold objects of type ns3::TypeId
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Ptr< Packet > Copy(void) const
virtual void DoSetup(void)
Implementation to do any local setup required for this test case.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
keep track of a set of node pointers.
hold objects of type Ptr<T>
void SetList(const std::list< uint32_t > &packetlist)
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
bool IsStatusSuccess(void) const
void Open(std::string const &filename, std::ios::openmode mode)
void SetChannelAttribute(std::string name, const AttributeValue &value)
Ipv4 addresses are stored in host order in this class.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
hold objects of type ns3::DataRate
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
void SetDataDir(std::string directory)
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
ApplicationContainer Install(NodeContainer c) const
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
std::ostream * GetStream(void)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const