author | Josh Pelkey <jpelkey@gatech.edu> |
Mon, 12 Apr 2010 20:50:04 -0400 | |
changeset 6192 | cf8ef89e65c4 |
parent 6191 | 5cde5bcbc902 |
child 6279 | 006a660a021a |
permissions | -rw-r--r-- |
6191 | 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright (c) 2010 University of Washington |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License version 2 as |
|
7 |
* published by the Free Software Foundation; |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
*/ |
|
18 |
||
6192
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
19 |
#include "receive-list-error-model.h" |
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
20 |
|
6191 | 21 |
#include "ns3/log.h" |
22 |
#include "ns3/abort.h" |
|
23 |
#include "ns3/test.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 |
||
42 |
using namespace ns3; |
|
43 |
||
44 |
NS_LOG_COMPONENT_DEFINE ("Ns3TcpLossTest"); |
|
45 |
||
46 |
// =========================================================================== |
|
47 |
// Tests of TCP implementation loss behavior |
|
48 |
// =========================================================================== |
|
49 |
// |
|
50 |
||
51 |
class Ns3TcpLossTestCase1 : public TestCase |
|
52 |
{ |
|
53 |
public: |
|
54 |
Ns3TcpLossTestCase1 (); |
|
55 |
virtual ~Ns3TcpLossTestCase1 () {} |
|
56 |
||
57 |
private: |
|
58 |
virtual bool DoRun (void); |
|
59 |
bool m_writeResults; |
|
60 |
||
61 |
void SinkRx (std::string path, Ptr<const Packet> p, const Address &address); |
|
62 |
||
63 |
TestVectors<uint32_t> m_inputs; |
|
64 |
TestVectors<uint32_t> m_responses; |
|
65 |
}; |
|
66 |
||
67 |
Ns3TcpLossTestCase1::Ns3TcpLossTestCase1 () |
|
68 |
: TestCase ("Check that ns-3 TCP survives loss of first two SYNs"), |
|
69 |
m_writeResults (false) |
|
70 |
{ |
|
71 |
} |
|
72 |
||
73 |
void |
|
74 |
Ns3TcpLossTestCase1::SinkRx (std::string path, Ptr<const Packet> p, const Address &address) |
|
75 |
{ |
|
76 |
m_responses.Add (p->GetSize ()); |
|
77 |
} |
|
78 |
||
79 |
bool |
|
80 |
Ns3TcpLossTestCase1::DoRun (void) |
|
81 |
{ |
|
82 |
uint16_t sinkPort = 50000; |
|
83 |
double sinkStopTime = 40; // sec; will trigger Socket::Close |
|
84 |
double writerStopTime = 30; // sec; will trigger Socket::Close |
|
85 |
double simStopTime = 60; // sec |
|
86 |
Time sinkStopTimeObj = Seconds (sinkStopTime); |
|
87 |
Time writerStopTimeObj = Seconds (writerStopTime); |
|
88 |
Time simStopTimeObj= Seconds (simStopTime); |
|
89 |
||
90 |
Ptr<Node> n0 = CreateObject<Node> (); |
|
91 |
Ptr<Node> n1 = CreateObject<Node> (); |
|
92 |
||
93 |
PointToPointHelper pointToPoint; |
|
94 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
95 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("200ms")); |
|
96 |
||
97 |
NetDeviceContainer devices; |
|
98 |
devices = pointToPoint.Install (n0, n1); |
|
99 |
||
100 |
InternetStackHelper internet; |
|
101 |
internet.InstallAll (); |
|
102 |
||
103 |
Ipv4AddressHelper address; |
|
104 |
address.SetBase ("10.1.1.0", "255.255.255.252"); |
|
105 |
Ipv4InterfaceContainer ifContainer = address.Assign (devices); |
|
106 |
||
107 |
Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> (); |
|
108 |
Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort)); |
|
109 |
socketWriter->Setup (n0, sinkAddress); |
|
110 |
n0->AddApplication (socketWriter); |
|
111 |
socketWriter->SetStartTime (Seconds (0.)); |
|
112 |
socketWriter->SetStopTime (writerStopTimeObj); |
|
113 |
||
114 |
PacketSinkHelper sink ("ns3::TcpSocketFactory", |
|
115 |
InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); |
|
116 |
ApplicationContainer apps = sink.Install (n1); |
|
117 |
// Start the sink application at time zero, and stop it at sinkStopTime |
|
118 |
apps.Start (Seconds (0.0)); |
|
119 |
apps.Stop (sinkStopTimeObj); |
|
120 |
||
121 |
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", |
|
122 |
MakeCallback (&Ns3TcpLossTestCase1::SinkRx, this)); |
|
123 |
||
124 |
Simulator::Schedule(Seconds (2), &SocketWriter::Connect, socketWriter); |
|
125 |
Simulator::Schedule(Seconds (10), &SocketWriter::Write, socketWriter, 500); |
|
126 |
m_inputs.Add (500); |
|
127 |
Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter); |
|
128 |
||
129 |
std::list<uint32_t> sampleList; |
|
130 |
// Lose first two SYNs |
|
131 |
sampleList.push_back (0); |
|
132 |
sampleList.push_back (1); |
|
133 |
// This time, we'll explicitly create the error model we want |
|
6192
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
134 |
Ptr<ReceiveListErrorModel> pem = CreateObject<ReceiveListErrorModel> (); |
6191 | 135 |
pem->SetList (sampleList); |
136 |
devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem)); |
|
137 |
||
138 |
if (m_writeResults) |
|
139 |
{ |
|
140 |
pointToPoint.EnablePcapAll ("tcp-loss-test-case-1"); |
|
141 |
pointToPoint.EnableAsciiAll ("tcp-loss-test-case-1"); |
|
142 |
} |
|
143 |
||
144 |
Simulator::Stop (simStopTimeObj); |
|
145 |
Simulator::Run (); |
|
146 |
Simulator::Destroy (); |
|
147 |
||
148 |
// Compare inputs and outputs |
|
149 |
NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events"); |
|
150 |
for (uint32_t i = 0; i < m_responses.GetN (); i++) |
|
151 |
{ |
|
152 |
uint32_t in = m_inputs.Get (i); |
|
153 |
uint32_t out = m_responses.Get (i); |
|
154 |
NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes"); |
|
155 |
} |
|
156 |
||
157 |
return GetErrorStatus (); |
|
158 |
} |
|
159 |
||
160 |
class Ns3TcpLossTestCase2 : public TestCase |
|
161 |
{ |
|
162 |
public: |
|
163 |
Ns3TcpLossTestCase2 (); |
|
164 |
virtual ~Ns3TcpLossTestCase2 () {} |
|
165 |
||
166 |
private: |
|
167 |
virtual bool DoRun (void); |
|
168 |
bool m_writeResults; |
|
169 |
||
170 |
void SinkRx (std::string path, Ptr<const Packet> p, const Address &address); |
|
171 |
||
172 |
TestVectors<uint32_t> m_inputs; |
|
173 |
TestVectors<uint32_t> m_responses; |
|
174 |
}; |
|
175 |
||
176 |
Ns3TcpLossTestCase2::Ns3TcpLossTestCase2 () |
|
177 |
: TestCase ("Check that ns-3 TCP survives loss of first data packet"), |
|
178 |
m_writeResults (false) |
|
179 |
{ |
|
180 |
} |
|
181 |
||
182 |
void |
|
183 |
Ns3TcpLossTestCase2::SinkRx (std::string path, Ptr<const Packet> p, const Address &address) |
|
184 |
{ |
|
185 |
m_responses.Add (p->GetSize ()); |
|
186 |
} |
|
187 |
||
188 |
bool |
|
189 |
Ns3TcpLossTestCase2::DoRun (void) |
|
190 |
{ |
|
191 |
uint16_t sinkPort = 50000; |
|
192 |
double sinkStopTime = 40; // sec; will trigger Socket::Close |
|
193 |
double writerStopTime = 12; // sec; will trigger Socket::Close |
|
194 |
double simStopTime = 60; // sec |
|
195 |
Time sinkStopTimeObj = Seconds (sinkStopTime); |
|
196 |
Time writerStopTimeObj = Seconds (writerStopTime); |
|
197 |
Time simStopTimeObj= Seconds (simStopTime); |
|
198 |
||
199 |
Ptr<Node> n0 = CreateObject<Node> (); |
|
200 |
Ptr<Node> n1 = CreateObject<Node> (); |
|
201 |
||
202 |
PointToPointHelper pointToPoint; |
|
203 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
204 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("200ms")); |
|
205 |
||
206 |
NetDeviceContainer devices; |
|
207 |
devices = pointToPoint.Install (n0, n1); |
|
208 |
||
209 |
InternetStackHelper internet; |
|
210 |
internet.InstallAll (); |
|
211 |
||
212 |
Ipv4AddressHelper address; |
|
213 |
address.SetBase ("10.1.1.0", "255.255.255.252"); |
|
214 |
Ipv4InterfaceContainer ifContainer = address.Assign (devices); |
|
215 |
||
216 |
Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> (); |
|
217 |
Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort)); |
|
218 |
socketWriter->Setup (n0, sinkAddress); |
|
219 |
n0->AddApplication (socketWriter); |
|
220 |
socketWriter->SetStartTime (Seconds (0.)); |
|
221 |
socketWriter->SetStopTime (writerStopTimeObj); |
|
222 |
||
223 |
PacketSinkHelper sink ("ns3::TcpSocketFactory", |
|
224 |
InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); |
|
225 |
ApplicationContainer apps = sink.Install (n1); |
|
226 |
// Start the sink application at time zero, and stop it at sinkStopTime |
|
227 |
apps.Start (Seconds (0.0)); |
|
228 |
apps.Stop (sinkStopTimeObj); |
|
229 |
||
230 |
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", |
|
231 |
MakeCallback (&Ns3TcpLossTestCase2::SinkRx, this)); |
|
232 |
||
233 |
Simulator::Schedule(Seconds (2), &SocketWriter::Connect, socketWriter); |
|
234 |
Simulator::Schedule(Seconds (10), &SocketWriter::Write, socketWriter, 500); |
|
235 |
m_inputs.Add (500); |
|
236 |
Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter); |
|
237 |
||
238 |
std::list<uint32_t> sampleList; |
|
239 |
// Lose first data segment |
|
6192
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
240 |
sampleList.push_back (2); |
6191 | 241 |
// This time, we'll explicitly create the error model we want |
6192
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
242 |
Ptr<ReceiveListErrorModel> pem = CreateObject<ReceiveListErrorModel> (); |
6191 | 243 |
pem->SetList (sampleList); |
244 |
devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem)); |
|
245 |
||
246 |
if (m_writeResults) |
|
247 |
{ |
|
248 |
pointToPoint.EnablePcapAll ("tcp-loss-test-case-2"); |
|
249 |
pointToPoint.EnableAsciiAll ("tcp-loss-test-case-2"); |
|
250 |
} |
|
251 |
||
252 |
Simulator::Stop (simStopTimeObj); |
|
253 |
Simulator::Run (); |
|
254 |
Simulator::Destroy (); |
|
255 |
||
256 |
// Compare inputs and outputs |
|
257 |
NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events"); |
|
258 |
for (uint32_t i = 0; i < m_responses.GetN (); i++) |
|
259 |
{ |
|
260 |
uint32_t in = m_inputs.Get (i); |
|
261 |
uint32_t out = m_responses.Get (i); |
|
262 |
NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes"); |
|
263 |
} |
|
264 |
||
265 |
return GetErrorStatus (); |
|
266 |
} |
|
267 |
||
268 |
class Ns3TcpLossTestSuite : public TestSuite |
|
269 |
{ |
|
270 |
public: |
|
271 |
Ns3TcpLossTestSuite (); |
|
272 |
}; |
|
273 |
||
274 |
Ns3TcpLossTestSuite::Ns3TcpLossTestSuite () |
|
275 |
: TestSuite ("ns3-tcp-loss", SYSTEM) |
|
276 |
{ |
|
277 |
AddTestCase (new Ns3TcpLossTestCase1); |
|
6192
cf8ef89e65c4
Modifiy tcp loss test to help with bug 818
Josh Pelkey <jpelkey@gatech.edu>
parents:
6191
diff
changeset
|
278 |
AddTestCase (new Ns3TcpLossTestCase2); |
6191 | 279 |
} |
280 |
||
281 |
Ns3TcpLossTestSuite ns3TcpLossTestSuite; |