Files
unison/examples/tutorial/seventh.cc

201 lines
7.3 KiB
C++
Raw Normal View History

/*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
*/
2022-10-07 20:08:35 +00:00
#include "tutorial-app.h"
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
2022-10-07 20:08:35 +00:00
#include "ns3/network-module.h"
2022-01-30 11:14:47 -06:00
#include "ns3/point-to-point-module.h"
#include "ns3/stats-module.h"
2022-10-07 20:08:35 +00:00
#include <fstream>
using namespace ns3;
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("SeventhScriptExample");
// ===========================================================================
//
// node 0 node 1
// +----------------+ +----------------+
// | ns-3 TCP | | ns-3 TCP |
// +----------------+ +----------------+
// | 10.1.1.1 | | 10.1.1.2 |
// +----------------+ +----------------+
// | point-to-point | | point-to-point |
// +----------------+ +----------------+
// | |
// +---------------------+
// 5 Mbps, 2 ms
//
//
// We want to look at changes in the ns-3 TCP congestion window. We need
// to crank up a flow and hook the CongestionWindow attribute on the socket
// of the sender. Normally one would use an on-off application to generate a
// flow, but this has a couple of problems. First, the socket of the on-off
// application is not created until Application Start time, so we wouldn't be
// able to hook the socket (now) at configuration time. Second, even if we
// could arrange a call after start time, the socket is not public so we
// couldn't get at it.
//
// So, we can cook up a simple version of the on-off application that does what
// we want. On the plus side we don't need all of the complexity of the on-off
// application. On the minus side, we don't have a helper, so we have to get
// a little more involved in the details, but this is trivial.
//
// So first, we create a socket and do the trace connect on it; then we pass
// this socket into the constructor of our simple application which we then
// install in the source node.
//
// NOTE: If this example gets modified, do not forget to update the .png figure
// in src/stats/docs/seventh-packet-byte-count.png
// ===========================================================================
//
2022-08-28 13:24:25 -05:00
/**
* Congestion window change callback
*
2023-02-18 00:43:21 -03:00
* \param stream The output stream file.
2022-08-28 13:24:25 -05:00
* \param oldCwnd Old congestion window.
* \param newCwnd New congestion window.
*/
static void
2022-10-07 20:08:35 +00:00
CwndChange(Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
{
2022-10-07 20:08:35 +00:00
NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "\t" << newCwnd);
*stream->GetStream() << Simulator::Now().GetSeconds() << "\t" << oldCwnd << "\t" << newCwnd
<< std::endl;
}
2022-08-28 13:24:25 -05:00
/**
* Rx drop callback
*
2023-02-18 00:43:21 -03:00
* \param file The output PCAP file.
2022-08-28 13:24:25 -05:00
* \param p The dropped packet.
*/
static void
2022-10-07 20:08:35 +00:00
RxDrop(Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
{
2022-10-07 20:08:35 +00:00
NS_LOG_UNCOND("RxDrop at " << Simulator::Now().GetSeconds());
file->Write(Simulator::Now(), p);
}
int
2022-10-07 20:08:35 +00:00
main(int argc, char* argv[])
{
2022-10-07 20:08:35 +00:00
bool useV6 = false;
2022-10-07 20:08:35 +00:00
CommandLine cmd(__FILE__);
cmd.AddValue("useIpv6", "Use Ipv6", useV6);
cmd.Parse(argc, argv);
2022-10-07 20:08:35 +00:00
NodeContainer nodes;
nodes.Create(2);
2022-10-07 20:08:35 +00:00
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
2022-10-07 20:08:35 +00:00
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
2022-10-07 20:08:35 +00:00
Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
em->SetAttribute("ErrorRate", DoubleValue(0.00001));
devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
2022-10-07 20:08:35 +00:00
InternetStackHelper stack;
stack.Install(nodes);
2022-10-07 20:08:35 +00:00
uint16_t sinkPort = 8080;
Address sinkAddress;
Address anyAddress;
std::string probeType;
std::string tracePath;
if (!useV6)
{
2022-10-07 20:08:35 +00:00
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
sinkAddress = InetSocketAddress(interfaces.GetAddress(1), sinkPort);
anyAddress = InetSocketAddress(Ipv4Address::GetAny(), sinkPort);
probeType = "ns3::Ipv4PacketProbe";
tracePath = "/NodeList/*/$ns3::Ipv4L3Protocol/Tx";
}
2022-10-07 20:08:35 +00:00
else
{
2022-10-07 20:08:35 +00:00
Ipv6AddressHelper address;
address.SetBase("2001:0000:f00d:cafe::", Ipv6Prefix(64));
Ipv6InterfaceContainer interfaces = address.Assign(devices);
sinkAddress = Inet6SocketAddress(interfaces.GetAddress(1, 1), sinkPort);
anyAddress = Inet6SocketAddress(Ipv6Address::GetAny(), sinkPort);
probeType = "ns3::Ipv6PacketProbe";
tracePath = "/NodeList/*/$ns3::Ipv6L3Protocol/Tx";
}
2022-10-07 20:08:35 +00:00
PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", anyAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));
sinkApps.Start(Seconds(0.));
sinkApps.Stop(Seconds(20.));
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket(nodes.Get(0), TcpSocketFactory::GetTypeId());
Ptr<TutorialApp> app = CreateObject<TutorialApp>();
app->Setup(ns3TcpSocket, sinkAddress, 1040, 1000, DataRate("1Mbps"));
nodes.Get(0)->AddApplication(app);
app->SetStartTime(Seconds(1.));
app->SetStopTime(Seconds(20.));
AsciiTraceHelper asciiTraceHelper;
Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream("seventh.cwnd");
ns3TcpSocket->TraceConnectWithoutContext("CongestionWindow",
MakeBoundCallback(&CwndChange, stream));
PcapHelper pcapHelper;
Ptr<PcapFileWrapper> file =
pcapHelper.CreateFile("seventh.pcap", std::ios::out, PcapHelper::DLT_PPP);
devices.Get(1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback(&RxDrop, file));
// Use GnuplotHelper to plot the packet byte count over time
GnuplotHelper plotHelper;
// Configure the plot. The first argument is the file name prefix
// for the output files generated. The second, third, and fourth
// arguments are, respectively, the plot title, x-axis, and y-axis labels
plotHelper.ConfigurePlot("seventh-packet-byte-count",
"Packet Byte Count vs. Time",
"Time (Seconds)",
"Packet Byte Count");
// Specify the probe type, trace source path (in configuration namespace), and
// probe output trace source ("OutputBytes") to plot. The fourth argument
// specifies the name of the data series label on the plot. The last
// argument formats the plot by specifying where the key should be placed.
plotHelper.PlotProbe(probeType,
tracePath,
2022-10-07 20:08:35 +00:00
"OutputBytes",
"Packet Byte Count",
GnuplotAggregator::KEY_BELOW);
2022-10-07 20:08:35 +00:00
// Use FileHelper to write out the packet byte count over time
FileHelper fileHelper;
2022-10-07 20:08:35 +00:00
// Configure the file to be written, and the formatting of output data.
fileHelper.ConfigureFile("seventh-packet-byte-count", FileAggregator::FORMATTED);
2022-10-07 20:08:35 +00:00
// Set the labels for this formatted output file.
fileHelper.Set2dFormat("Time (Seconds) = %.3e\tPacket Byte Count = %.0f");
// Specify the probe type, trace source path (in configuration namespace), and
// probe output trace source ("OutputBytes") to write.
fileHelper.WriteProbe(probeType, tracePath, "OutputBytes");
Simulator::Stop(Seconds(20));
Simulator::Run();
Simulator::Destroy();
return 0;
}