A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fifth.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "tutorial-app.h"
6
7#include "ns3/applications-module.h"
8#include "ns3/core-module.h"
9#include "ns3/internet-module.h"
10#include "ns3/network-module.h"
11#include "ns3/point-to-point-module.h"
12
13#include <fstream>
14
15using namespace ns3;
16
17NS_LOG_COMPONENT_DEFINE("FifthScriptExample");
18
19// ===========================================================================
20//
21// node 0 node 1
22// +----------------+ +----------------+
23// | ns-3 TCP | | ns-3 TCP |
24// +----------------+ +----------------+
25// | 10.1.1.1 | | 10.1.1.2 |
26// +----------------+ +----------------+
27// | point-to-point | | point-to-point |
28// +----------------+ +----------------+
29// | |
30// +---------------------+
31// 5 Mbps, 2 ms
32//
33//
34// We want to look at changes in the ns-3 TCP congestion window. We need
35// to crank up a flow and hook the CongestionWindow attribute on the socket
36// of the sender. Normally one would use an on-off application to generate a
37// flow, but this has a couple of problems. First, the socket of the on-off
38// application is not created until Application Start time, so we wouldn't be
39// able to hook the socket (now) at configuration time. Second, even if we
40// could arrange a call after start time, the socket is not public so we
41// couldn't get at it.
42//
43// So, we can cook up a simple version of the on-off application that does what
44// we want. On the plus side we don't need all of the complexity of the on-off
45// application. On the minus side, we don't have a helper, so we have to get
46// a little more involved in the details, but this is trivial.
47//
48// So first, we create a socket and do the trace connect on it; then we pass
49// this socket into the constructor of our simple application which we then
50// install in the source node.
51// ===========================================================================
52//
53
54/**
55 * Congestion window change callback
56 *
57 * \param oldCwnd Old congestion window.
58 * \param newCwnd New congestion window.
59 */
60static void
61CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
62{
63 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "\t" << newCwnd);
64}
65
66/**
67 * Rx drop callback
68 *
69 * \param p The dropped packet.
70 */
71static void
73{
74 NS_LOG_UNCOND("RxDrop at " << Simulator::Now().GetSeconds());
75}
76
77int
78main(int argc, char* argv[])
79{
80 CommandLine cmd(__FILE__);
81 cmd.Parse(argc, argv);
82
83 // In the following three lines, TCP NewReno is used as the congestion
84 // control algorithm, the initial congestion window of a TCP connection is
85 // set to 1 packet, and the classic fast recovery algorithm is used. Note
86 // that this configuration is used only to demonstrate how TCP parameters
87 // can be configured in ns-3. Otherwise, it is recommended to use the default
88 // settings of TCP in ns-3.
89 Config::SetDefault("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
90 Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(1));
91 Config::SetDefault("ns3::TcpL4Protocol::RecoveryType",
92 TypeIdValue(TypeId::LookupByName("ns3::TcpClassicRecovery")));
93
95 nodes.Create(2);
96
98 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
99 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
100
102 devices = pointToPoint.Install(nodes);
103
105 em->SetAttribute("ErrorRate", DoubleValue(0.00001));
106 devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
107
109 stack.Install(nodes);
110
112 address.SetBase("10.1.1.0", "255.255.255.252");
113 Ipv4InterfaceContainer interfaces = address.Assign(devices);
114
115 uint16_t sinkPort = 8080;
116 Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), sinkPort));
117 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory",
119 ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));
120 sinkApps.Start(Seconds(0.));
121 sinkApps.Stop(Seconds(20.));
122
124 ns3TcpSocket->TraceConnectWithoutContext("CongestionWindow", MakeCallback(&CwndChange));
125
127 app->Setup(ns3TcpSocket, sinkAddress, 1040, 1000, DataRate("1Mbps"));
128 nodes.Get(0)->AddApplication(app);
129 app->SetStartTime(Seconds(1.));
130 app->SetStopTime(Seconds(20.));
131
132 devices.Get(1)->TraceConnectWithoutContext("PhyRxDrop", MakeCallback(&RxDrop));
133
137
138 return 0;
139}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
static TypeId GetTypeId()
Get the type ID.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Hold an unsigned integer type.
Definition uinteger.h:34
static void RxDrop(Ptr< const Packet > p)
Rx drop callback.
Definition fifth.cc:72
static void CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
Congestion window change callback.
Definition fifth.cc:61
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
address
Definition first.py:36
pointToPoint
Definition first.py:27
devices
Definition first.py:31
stack
Definition first.py:33
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684