|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
3 |
* This program is free software; you can redistribute it and/or modify |
4 |
* it under the terms of the GNU General Public License version 2 as |
5 |
* published by the Free Software Foundation; |
6 |
* |
7 |
* This program is distributed in the hope that it will be useful, |
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 |
* GNU General Public License for more details. |
11 |
* |
12 |
* You should have received a copy of the GNU General Public License |
13 |
* along with this program; if not, write to the Free Software |
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
15 |
* |
16 |
*/ |
17 |
|
18 |
// |
19 |
// Network topology |
20 |
// |
21 |
// 10Mb/s, 10ms 10Mb/s, 10ms |
22 |
// n0-----------------n1-----------------n2 |
23 |
// |
24 |
// |
25 |
// - Tracing of queues and packet receptions to file |
26 |
// "tcp-large-transfer.tr" |
27 |
// - pcap traces also generated in the following files |
28 |
// "tcp-large-transfer-$n-$i.pcap" where n and i represent node and interface |
29 |
// numbers respectively |
30 |
// Usage (e.g.): ./waf --run tcp-finite-buffers |
31 |
|
32 |
|
33 |
#include <ctype.h> |
34 |
#include <iostream> |
35 |
#include <fstream> |
36 |
#include <string> |
37 |
#include <cassert> |
38 |
|
39 |
#include "ns3/core-module.h" |
40 |
#include "ns3/helper-module.h" |
41 |
#include "ns3/node-module.h" |
42 |
#include "ns3/global-route-manager.h" |
43 |
#include "ns3/simulator-module.h" |
44 |
#include "ns3/packet-sink.h" |
45 |
|
46 |
using namespace ns3; |
47 |
|
48 |
NS_LOG_COMPONENT_DEFINE ("TcpFinteBuffers"); |
49 |
|
50 |
// The number of bytes to send in this simulation. |
51 |
static uint32_t txBytes = 2000000; |
52 |
|
53 |
// These are for starting the writing process, and handling the sending |
54 |
// socket's notification upcalls (events). These two together more or less |
55 |
// implement a sending "Application", although not a proper ns3::Application |
56 |
// subclass. |
57 |
|
58 |
void StartFlow(Ptr<Socket>, Ipv4Address, uint16_t); |
59 |
void WriteUntilBufferFull (Ptr<Socket>, uint32_t); |
60 |
|
61 |
int main (int argc, char *argv[]) |
62 |
{ |
63 |
|
64 |
// Users may find it convenient to turn on explicit debugging |
65 |
// for selected modules; the below lines suggest how to do this |
66 |
// LogComponentEnable("TcpL4Protocol", LOG_LEVEL_ALL); |
67 |
// LogComponentEnable("TcpSocketImpl", LOG_LEVEL_ALL); |
68 |
// LogComponentEnable("PacketSink", LOG_LEVEL_ALL); |
69 |
// LogComponentEnable("TcpFiniteBuffers", LOG_LEVEL_ALL); |
70 |
|
71 |
// |
72 |
// Make the random number generators generate reproducible results. |
73 |
// |
74 |
RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8); |
75 |
|
76 |
Config::SetDefault ("ns3::TcpSocket::RcvBufSize", UintegerValue (10000)); |
77 |
|
78 |
// Allow the user to override any of the defaults and the above |
79 |
// SetDefauls()s at run-time, via command-line arguments |
80 |
CommandLine cmd; |
81 |
cmd.Parse (argc, argv); |
82 |
|
83 |
// Here, we will explicitly create three nodes. The first container contains |
84 |
// nodes 0 and 1 from the diagram above, and the second one contains nodes |
85 |
// 1 and 2. This reflects the channel connectivity, and will be used to |
86 |
// install the network interfaces and connect them with a channel. |
87 |
NodeContainer n0n1; |
88 |
n0n1.Create (2); |
89 |
|
90 |
NodeContainer n1n2; |
91 |
n1n2.Add (n0n1.Get (1)); |
92 |
n1n2.Create (1); |
93 |
|
94 |
// We create the channels first without any IP addressing information |
95 |
// First make and configure the helper, so that it will put the appropriate |
96 |
// attributes on the network interfaces and channels we are about to install. |
97 |
PointToPointHelper p2p; |
98 |
p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate(10000000))); |
99 |
p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds(10))); |
100 |
|
101 |
// And then install devices and channels connecting our topology. |
102 |
NetDeviceContainer dev0 = p2p.Install (n0n1); |
103 |
NetDeviceContainer dev1 = p2p.Install (n1n2); |
104 |
|
105 |
// Now add ip/tcp stack to all nodes. |
106 |
NodeContainer allNodes = NodeContainer (n0n1, n1n2.Get (1)); |
107 |
InternetStackHelper internet; |
108 |
internet.Install (allNodes); |
109 |
|
110 |
// Later, we add IP addresses. |
111 |
Ipv4AddressHelper ipv4; |
112 |
ipv4.SetBase ("10.1.3.0", "255.255.255.0"); |
113 |
ipv4.Assign (dev0); |
114 |
ipv4.SetBase ("10.1.2.0", "255.255.255.0"); |
115 |
Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev1); |
116 |
|
117 |
// and setup ip routing tables to get total ip-level connectivity. |
118 |
GlobalRouteManager::PopulateRoutingTables (); |
119 |
|
120 |
/////////////////////////////////////////////////////////////////////////// |
121 |
// Simulation 1 |
122 |
// |
123 |
// Send 2000000 bytes over a connection to server port 50000 at time 0 |
124 |
// Should observe SYN exchange, a lot of data segments and ACKS, and FIN |
125 |
// exchange. FIN exchange isn't quite compliant with TCP spec (see release |
126 |
// notes for more info) |
127 |
// |
128 |
/////////////////////////////////////////////////////////////////////////// |
129 |
|
130 |
uint16_t servPort = 50000; |
131 |
|
132 |
// Create a packet sink to receive these packets on n2... |
133 |
PacketSinkHelper sink ("ns3::TcpSocketFactory", |
134 |
InetSocketAddress (Ipv4Address::GetAny (), servPort)); |
135 |
|
136 |
ApplicationContainer apps = sink.Install (n1n2.Get (1)); |
137 |
apps.Start (Seconds (0.0)); |
138 |
//make the sink sleep until 300s simulation time |
139 |
Ptr<PacketSink> sinkApp = apps.Get(0)->GetObject<PacketSink>(); |
140 |
sinkApp->DelayFirstRead (Seconds(300.0)); |
141 |
|
142 |
// Create a source to send packets from n0. Instead of a full Application |
143 |
// and the helper APIs you might see in other example files, this example |
144 |
// will use sockets directly and register some socket callbacks as a sending |
145 |
// "Application". |
146 |
|
147 |
// Create and bind the socket... |
148 |
Ptr<Socket> localSocket = |
149 |
Socket::CreateSocket (n0n1.Get (0), TcpSocketFactory::GetTypeId ()); |
150 |
localSocket->Bind (); |
151 |
|
152 |
// ...and schedule the sending "Application"; This is similar to what an |
153 |
// ns3::Application subclass would do internally. |
154 |
Simulator::ScheduleNow (&StartFlow, localSocket, |
155 |
ipInterfs.GetAddress (1), servPort); |
156 |
|
157 |
// One can toggle the comment for the following line on or off to see the |
158 |
// effects of finite send buffer modelling. One can also change the size of |
159 |
// said buffer. |
160 |
|
161 |
//localSocket->SetAttribute("SndBufSize", UintegerValue(4096)); |
162 |
|
163 |
//Ask for ASCII and pcap traces of network traffic |
164 |
std::ofstream ascii; |
165 |
ascii.open ("tcp-finite-buffers.tr"); |
166 |
PointToPointHelper::EnableAsciiAll (ascii); |
167 |
|
168 |
PointToPointHelper::EnablePcapAll ("tcp-finite-buffers"); |
169 |
|
170 |
// Finally, set up the simulator to run. The 1000 second hard limit is a |
171 |
// failsafe in case some change above causes the simulation to never end |
172 |
Simulator::Stop (Seconds(1000)); |
173 |
Simulator::Run (); |
174 |
Simulator::Destroy (); |
175 |
} |
176 |
|
177 |
|
178 |
//----------------------------------------------------------------------------- |
179 |
//----------------------------------------------------------------------------- |
180 |
//----------------------------------------------------------------------------- |
181 |
//begin implementation of sending "Application" |
182 |
void StartFlow(Ptr<Socket> localSocket, |
183 |
Ipv4Address servAddress, |
184 |
uint16_t servPort) |
185 |
{ |
186 |
NS_LOG_LOGIC("Starting flow at time " << Simulator::Now ().GetSeconds ()); |
187 |
localSocket->Connect (InetSocketAddress (servAddress, servPort));//connect |
188 |
|
189 |
// tell the tcp implementation to call WriteUntilBufferFull again |
190 |
// if we blocked and new tx buffer space becomes available |
191 |
localSocket->SetSendCallback (MakeCallback (&WriteUntilBufferFull)); |
192 |
WriteUntilBufferFull (localSocket, txBytes); |
193 |
} |
194 |
|
195 |
void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace) |
196 |
{ |
197 |
// Perform series of 1040 byte writes (this is a multiple of 26 since |
198 |
// we want to detect data splicing in the output stream) |
199 |
uint32_t writeSize = 1040; |
200 |
uint8_t data[writeSize]; |
201 |
|
202 |
while (txBytes > 0) { |
203 |
uint32_t curSize= txBytes > writeSize ? writeSize : txBytes; |
204 |
if (curSize > txSpace) |
205 |
curSize = txSpace; |
206 |
for(uint32_t i = 0; i < curSize; ++i) |
207 |
{ |
208 |
char m = toascii (97 + i % 26); |
209 |
data[i] = m; |
210 |
} |
211 |
int amountSent = localSocket->Send (data, curSize, 0); |
212 |
if(amountSent < 0) |
213 |
{ |
214 |
// we will be called again when new tx space becomes available. |
215 |
std::cout << "Socket blocking, " << txBytes << " left to write, returning" << std::endl; |
216 |
return; |
217 |
} |
218 |
txBytes -= curSize; |
219 |
if (amountSent != (int)curSize) |
220 |
{ |
221 |
std::cout << "Short Write, returning" << std::endl; |
222 |
return; |
223 |
} |
224 |
} |
225 |
localSocket->Close (); |
226 |
} |