Files
unison/examples/tcp/tcp-large-transfer.cc

233 lines
8.0 KiB
C++
Raw Permalink Normal View History

2008-01-25 13:57:38 -05:00
/*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
2008-01-25 13:57:38 -05:00
*
*/
//
// Network topology
//
// 10Mb/s, 10ms 10Mb/s, 10ms
// n0-----------------n1-----------------n2
//
//
// - Tracing of queues and packet receptions to file
2008-01-25 13:57:38 -05:00
// "tcp-large-transfer.tr"
// - pcap traces also generated in the following files
2008-06-19 14:58:46 -07:00
// "tcp-large-transfer-$n-$i.pcap" where n and i represent node and interface
// numbers respectively
2022-01-13 23:59:59 -03:00
// Usage (e.g.): ./ns3 run tcp-large-transfer
2008-01-25 13:57:38 -05:00
2011-03-02 13:42:28 -08:00
#include "ns3/applications-module.h"
2022-10-07 20:08:35 +00:00
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-global-routing-helper.h"
2022-10-07 20:08:35 +00:00
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include <fstream>
#include <iostream>
#include <string>
2008-01-25 13:57:38 -05:00
using namespace ns3;
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("TcpLargeTransfer");
2008-01-25 13:57:38 -05:00
2022-08-28 13:24:25 -05:00
/// The number of bytes to send in this simulation.
static const uint32_t totalTxBytes = 2000000;
2022-08-28 13:24:25 -05:00
/// The actual number of sent bytes.
static uint32_t currentTxBytes = 0;
2022-08-28 13:24:25 -05:00
// Perform series of 1040 byte writes (this is a multiple of 26 since
// we want to detect data splicing in the output stream)
2022-08-28 13:24:25 -05:00
/// Write size.
static const uint32_t writeSize = 1040;
2022-08-28 13:24:25 -05:00
/// Data to be written.
uint8_t data[writeSize];
2008-01-25 13:57:38 -05:00
// These are for starting the writing process, and handling the sending
2008-06-19 14:58:46 -07:00
// socket's notification upcalls (events). These two together more or less
// implement a sending "Application", although not a proper ns3::Application
// subclass.
2022-08-28 13:24:25 -05:00
/**
* Start a flow.
*
2024-11-08 18:05:46 +00:00
* @param localSocket The local (sending) socket.
* @param servAddress The server address.
* @param servPort The server port.
2022-08-28 13:24:25 -05:00
*/
2022-10-07 20:08:35 +00:00
void StartFlow(Ptr<Socket> localSocket, Ipv4Address servAddress, uint16_t servPort);
2022-08-28 13:24:25 -05:00
/**
* Write to the buffer, filling it.
*
2024-11-08 18:05:46 +00:00
* @param localSocket The socket.
* @param txSpace The number of bytes to write.
2022-08-28 13:24:25 -05:00
*/
2022-10-07 20:08:35 +00:00
void WriteUntilBufferFull(Ptr<Socket> localSocket, uint32_t txSpace);
2008-01-25 13:57:38 -05:00
2022-08-28 13:24:25 -05:00
/**
* Congestion window tracker function.
*
2024-11-08 18:05:46 +00:00
* @param oldval Old value.
* @param newval New value.
2022-08-28 13:24:25 -05:00
*/
static void
2022-10-07 20:08:35 +00:00
CwndTracer(uint32_t oldval, uint32_t newval)
2008-01-25 13:57:38 -05:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_INFO("Moving cwnd from " << oldval << " to " << newval);
2009-03-23 11:26:39 -07:00
}
2008-01-25 13:57:38 -05:00
2022-10-07 20:08:35 +00:00
int
main(int argc, char* argv[])
2009-03-23 11:26:39 -07:00
{
2022-10-07 20:08:35 +00:00
// 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("TcpLargeTransfer", LOG_LEVEL_ALL);
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
// initialize the tx buffer.
for (uint32_t i = 0; i < writeSize; ++i)
{
2022-10-07 20:08:35 +00:00
char m = toascii(97 + i % 26);
data[i] = m;
}
2022-10-07 20:08:35 +00:00
// 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);
// 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", DataRateValue(DataRate(10000000)));
p2p.SetChannelAttribute("Delay", TimeValue(MilliSeconds(10)));
// And then install devices and channels connecting our topology.
NetDeviceContainer dev0 = p2p.Install(n0n1);
NetDeviceContainer dev1 = p2p.Install(n1n2);
// Now add ip/tcp stack to all nodes.
InternetStackHelper internet;
internet.InstallAll();
// Later, we add IP addresses.
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);
// and setup ip routing tables to get total ip-level connectivity.
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
///////////////////////////////////////////////////////////////////////////
// Simulation 1
//
// Send 2000000 bytes over a connection to server port 50000 at time 0
// Should observe SYN exchange, a lot of data segments and ACKS, and FIN
// exchange. FIN exchange isn't quite compliant with TCP spec (see release
// notes for more info)
//
///////////////////////////////////////////////////////////////////////////
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));
apps.Stop(Seconds(3));
2022-10-07 20:08:35 +00:00
// Create a source to send packets from n0. Instead of a full Application
// and the helper APIs you might see in other example files, this example
// will use sockets directly and register some socket callbacks as a sending
// "Application".
// Create and bind the socket...
Ptr<Socket> localSocket = Socket::CreateSocket(n0n1.Get(0), TcpSocketFactory::GetTypeId());
localSocket->Bind();
// 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(1000));
Simulator::Run();
Simulator::Destroy();
return 0;
2008-01-25 13:57:38 -05:00
}
2008-06-19 14:58:46 -07:00
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
2022-10-07 20:08:35 +00:00
// begin implementation of sending "Application"
void
StartFlow(Ptr<Socket> localSocket, Ipv4Address servAddress, uint16_t servPort)
{
2022-10-07 20:08:35 +00:00
NS_LOG_LOGIC("Starting flow at time " << Simulator::Now().GetSeconds());
localSocket->Connect(InetSocketAddress(servAddress, servPort)); // connect
2008-06-19 14:58:46 -07:00
2022-10-07 20:08:35 +00:00
// 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());
}
2022-10-07 20:08:35 +00:00
void
WriteUntilBufferFull(Ptr<Socket> localSocket, uint32_t txSpace)
{
2022-10-07 20:08:35 +00:00
while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable() > 0)
{
2022-10-07 20:08:35 +00:00
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());
int amountSent = localSocket->Send(&data[dataOffset], toWrite, 0);
if (amountSent < 0)
{
2022-10-07 20:08:35 +00:00
// we will be called again when new tx space becomes available.
return;
}
2022-10-07 20:08:35 +00:00
currentTxBytes += amountSent;
}
2022-10-07 20:08:35 +00:00
if (currentTxBytes >= totalTxBytes)
{
2022-10-07 20:08:35 +00:00
localSocket->Close();
}
}