A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
bulk-send-application.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Georgia Institute of Technology
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: George F. Riley <riley@ece.gatech.edu>
7 */
8
10
11#include "ns3/boolean.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/nstime.h"
15#include "ns3/packet.h"
16#include "ns3/simulator.h"
17#include "ns3/socket-factory.h"
18#include "ns3/socket.h"
19#include "ns3/tcp-socket-base.h"
20#include "ns3/tcp-socket-factory.h"
21#include "ns3/trace-source-accessor.h"
22#include "ns3/uinteger.h"
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("BulkSendApplication");
28
29NS_OBJECT_ENSURE_REGISTERED(BulkSendApplication);
30
31TypeId
33{
34 static TypeId tid =
35 TypeId("ns3::BulkSendApplication")
37 .SetGroupName("Applications")
38 .AddConstructor<BulkSendApplication>()
39 .AddAttribute("SendSize",
40 "The amount of data to send each time.",
41 UintegerValue(512),
44 .AddAttribute("MaxBytes",
45 "The total number of bytes to send. "
46 "Once these bytes are sent, "
47 "no data is sent again. The value zero means "
48 "that there is no limit.",
52 .AddAttribute("Protocol",
53 "The type of protocol to use.",
57 .AddAttribute("EnableSeqTsSizeHeader",
58 "Add SeqTsSizeHeader to each packet",
59 BooleanValue(false),
62 .AddTraceSource("Tx",
63 "A new packet is sent",
65 "ns3::Packet::TracedCallback")
66 .AddTraceSource("TxWithSeqTsSize",
67 "A new packet is created with SeqTsSizeHeader",
69 "ns3::PacketSink::SeqTsSizeCallback")
70 .AddTraceSource("TcpRetransmission",
71 "The TCP socket retransmitted a packet",
73 "ns3::TcpSocketBase::RetransmissionCallback");
74
75 return tid;
76}
77
79 : m_socket(nullptr),
80 m_connected(false),
81 m_totBytes(0),
82 m_unsentPacket(nullptr)
83{
84 NS_LOG_FUNCTION(this);
85}
86
91
92void
94{
95 NS_LOG_FUNCTION(this << maxBytes);
96 m_maxBytes = maxBytes;
97}
98
101{
102 NS_LOG_FUNCTION(this);
103 return m_socket;
104}
105
106void
108{
109 NS_LOG_FUNCTION(this);
110
111 m_socket = nullptr;
112 m_unsentPacket = nullptr;
113 // chain up
115}
116
117// Application Methods
118void
119BulkSendApplication::StartApplication() // Called at time specified by Start
120{
121 NS_LOG_FUNCTION(this);
122 Address from;
123
124 // Create the socket if not already
125 if (!m_socket)
126 {
128 int ret = -1;
129
130 // Fatal error if socket type is not NS3_SOCK_STREAM or NS3_SOCK_SEQPACKET
133 {
134 NS_FATAL_ERROR("Using BulkSend with an incompatible socket type. "
135 "BulkSend requires SOCK_STREAM or SOCK_SEQPACKET. "
136 "In other words, use TCP instead of UDP.");
137 }
138
139 NS_ABORT_MSG_IF(m_peer.IsInvalid(), "'Remote' attribute not properly set");
140
141 if (!m_local.IsInvalid())
142 {
147 "Incompatible peer and local address IP version");
148 ret = m_socket->Bind(m_local);
149 }
150 else
151 {
153 {
154 ret = m_socket->Bind6();
155 }
157 {
158 ret = m_socket->Bind();
159 }
160 }
161
162 if (ret == -1)
163 {
164 NS_FATAL_ERROR("Failed to bind socket");
165 }
166
168 {
169 m_socket->SetIpTos(m_tos); // Affects only IPv4 sockets.
170 }
177 if (tcpSocket)
178 {
179 tcpSocket->TraceConnectWithoutContext(
180 "Retransmission",
182 }
183 }
184 if (m_connected)
185 {
186 m_socket->GetSockName(from);
187 SendData(from, m_peer);
188 }
189}
190
191void
192BulkSendApplication::StopApplication() // Called at time specified by Stop
193{
194 NS_LOG_FUNCTION(this);
195
196 if (m_socket)
197 {
198 m_socket->Close();
199 m_connected = false;
200 }
201 else
202 {
203 NS_LOG_WARN("BulkSendApplication found null socket to close in StopApplication");
204 }
205}
206
207// Private helpers
208
209void
211{
212 NS_LOG_FUNCTION(this);
213
214 while (m_maxBytes == 0 || m_totBytes < m_maxBytes)
215 { // Time to send more
216
217 // uint64_t to allow the comparison later.
218 // the result is in a uint32_t range anyway, because
219 // m_sendSize is uint32_t.
220 uint64_t toSend = m_sendSize;
221 // Make sure we don't send too many
222 if (m_maxBytes > 0)
223 {
224 toSend = std::min(toSend, m_maxBytes - m_totBytes);
225 }
226
227 NS_LOG_LOGIC("sending packet at " << Simulator::Now());
228
229 Ptr<Packet> packet;
230 if (m_unsentPacket)
231 {
232 packet = m_unsentPacket;
233 toSend = packet->GetSize();
234 }
236 {
237 SeqTsSizeHeader header;
238 header.SetSeq(m_seq++);
239 header.SetSize(toSend);
240 NS_ABORT_IF(toSend < header.GetSerializedSize());
241 packet = Create<Packet>(toSend - header.GetSerializedSize());
242 // Trace before adding header, for consistency with PacketSink
243 m_txTraceWithSeqTsSize(packet, from, to, header);
244 packet->AddHeader(header);
245 }
246 else
247 {
248 packet = Create<Packet>(toSend);
249 }
250
251 int actual = m_socket->Send(packet);
252 if ((unsigned)actual == toSend)
253 {
254 m_totBytes += actual;
255 m_txTrace(packet);
256 m_unsentPacket = nullptr;
257 }
258 else if (actual == -1)
259 {
260 // We exit this loop when actual < toSend as the send side
261 // buffer is full. The "DataSent" callback will pop when
262 // some buffer space has freed up.
263 NS_LOG_DEBUG("Unable to send packet; caching for later attempt");
264 m_unsentPacket = packet;
265 break;
266 }
267 else if (actual > 0 && (unsigned)actual < toSend)
268 {
269 // A Linux socket (non-blocking, such as in DCE) may return
270 // a quantity less than the packet size. Split the packet
271 // into two, trace the sent packet, save the unsent packet
272 NS_LOG_DEBUG("Packet size: " << packet->GetSize() << "; sent: " << actual
273 << "; fragment saved: " << toSend - (unsigned)actual);
274 Ptr<Packet> sent = packet->CreateFragment(0, actual);
275 Ptr<Packet> unsent = packet->CreateFragment(actual, (toSend - (unsigned)actual));
276 m_totBytes += actual;
277 m_txTrace(sent);
278 m_unsentPacket = unsent;
279 break;
280 }
281 else
282 {
283 NS_FATAL_ERROR("Unexpected return value from m_socket->Send ()");
284 }
285 }
286 // Check if time to close (all sent)
288 {
289 m_socket->Close();
290 m_connected = false;
291 }
292}
293
294void
296{
297 NS_LOG_FUNCTION(this << socket);
298 NS_LOG_LOGIC("BulkSendApplication Connection succeeded");
299 m_connected = true;
300 Address from;
301 Address to;
302 socket->GetSockName(from);
303 socket->GetPeerName(to);
304 SendData(from, to);
305}
306
307void
309{
310 NS_LOG_FUNCTION(this << socket);
311 NS_LOG_LOGIC("BulkSendApplication, Connection Failed");
312}
313
314void
316{
317 NS_LOG_FUNCTION(this);
318
319 if (m_connected)
320 { // Only send new data if the connection has completed
321 Address from;
322 Address to;
323 socket->GetSockName(from);
324 socket->GetPeerName(to);
325 SendData(from, to);
326 }
327}
328
329void
331 const TcpHeader& header,
332 const Address& localAddr,
333 const Address& peerAddr,
335{
336 NS_LOG_FUNCTION(this << p << header << localAddr << peerAddr << socket);
337 m_retransmissionTrace(p, header, localAddr, peerAddr, socket);
338}
339
340} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
AttributeValue implementation for Boolean.
Definition boolean.h:26
Send as much traffic as possible, trying to fill the bandwidth.
bool m_enableSeqTsSizeHeader
Enable or disable the SeqTsSizeHeader.
void SendData(const Address &from, const Address &to)
Send data until the L4 transmission buffer is full.
Ptr< Packet > m_unsentPacket
Variable to cache unsent packet.
void PacketRetransmitted(Ptr< const Packet > p, const TcpHeader &header, const Address &localAddr, const Address &peerAddr, Ptr< const TcpSocketBase > socket)
Packet retransmitted (called by TcpSocketBase sockets via callback)
void DoDispose() override
Destructor implementation.
void ConnectionSucceeded(Ptr< Socket > socket)
Connection Succeeded (called by Socket through a callback)
static TypeId GetTypeId()
Get the type ID.
TracedCallback< Ptr< const Packet >, const TcpHeader &, const Address &, const Address &, Ptr< const TcpSocketBase > > m_retransmissionTrace
Traced Callback: retransmitted packets.
bool m_connected
True if connected.
uint32_t m_sendSize
Size of data to send each time.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: sent packets.
Ptr< Socket > GetSocket() const
Get the socket this application is attached to.
void ConnectionFailed(Ptr< Socket > socket)
Connection Failed (called by Socket through a callback)
uint64_t m_maxBytes
Limit total number of bytes sent.
TypeId m_tid
The type of protocol to use.
void StartApplication() override
Application specific startup code.
uint64_t m_totBytes
Total bytes sent so far.
Ptr< Socket > m_socket
Associated socket.
void DataSend(Ptr< Socket > socket, uint32_t unused)
Send more data as soon as some has been transmitted.
void StopApplication() override
Application specific shutdown code.
void SetMaxBytes(uint64_t maxBytes)
Set the upper bound for the total number of bytes to send.
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_txTraceWithSeqTsSize
Callback for tracing the packet Tx events, includes source, destination, the packet sent,...
static bool IsMatchingType(const Address &addr)
If the address match.
static bool IsMatchingType(const Address &address)
Smart pointer class similar to boost::intrusive_ptr.
void SetSeq(uint32_t seq)
Header with a sequence, a timestamp, and a "size" attribute.
uint32_t GetSerializedSize() const override
void SetSize(uint64_t size)
Set the size information that the header will carry.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual Socket::SocketType GetSocketType() const =0
void SetConnectCallback(Callback< void, Ptr< Socket > > connectionSucceeded, Callback< void, Ptr< Socket > > connectionFailed)
Specify callbacks to allow the caller to determine if the connection succeeds of fails.
Definition socket.cc:76
void SetIpTos(uint8_t ipTos)
Manually set IP Type of Service field.
Definition socket.cc:423
virtual int ShutdownRecv()=0
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
@ NS3_SOCK_STREAM
Definition socket.h:97
@ NS3_SOCK_SEQPACKET
Definition socket.h:98
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition socket.cc:110
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int GetSockName(Address &address) const =0
Get socket address.
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
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Base class for source applications.
Address m_local
Local address to bind to.
uint8_t m_tos
The packets Type of Service.
Address m_peer
Peer address.
Header for the Transmission Control Protocol.
Definition tcp-header.h:36
static TypeId GetTypeId()
Get the type ID.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
AttributeValue implementation for TypeId.
Definition type-id.h:617
Hold an unsigned integer type.
Definition uinteger.h:34
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition boolean.h:70
Ptr< const AttributeChecker > MakeTypeIdChecker()
Definition type-id.cc:1320
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition type-id.h:617
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition uinteger.h:35
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition abort.h:65
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580