A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-rx-buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Adrian Sai-wah Tam
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
18 */
19
20#ifndef TCP_RX_BUFFER_H
21#define TCP_RX_BUFFER_H
22
23#include "tcp-header.h"
24#include "tcp-option-sack.h"
25
26#include "ns3/ptr.h"
27#include "ns3/sequence-number.h"
28#include "ns3/trace-source-accessor.h"
29#include "ns3/traced-value.h"
30
31#include <map>
32
33namespace ns3
34{
35class Packet;
36
37/**
38 * \ingroup tcp
39 *
40 * \brief Rx reordering buffer for TCP
41 *
42 * The class is responsible to safely store the segments, and then
43 * returning them in-order to the application, where "in-order" does not means
44 * "network-order", but "sender-order" : the bytes should be returned in the
45 * same order that the sender application used to push them down on wire.
46 *
47 * The first useful sequence that this class is waiting is returned by the method
48 * NextRxSequence, and could be set at the beginning through MaxRxSequence.
49 *
50 * The max. size of this buffer is managed through SetMaxBufferSize, and could be
51 * retrieved using MaxBufferSize. The current size instead is returned by
52 * Size, while the amount of in-order data that could be extracted is returned
53 * by the method Available.
54 *
55 * To store data, use Add; for retrieving a certain amount of ordered data, use
56 * the method Extract.
57 *
58 * SACK list
59 * ---------
60 *
61 * An interesting feature of this class is the ability to maintain an ordered
62 * SACK list, under the definition of RFC 2018. When a out-of-order segment
63 * reaches this buffer, an ACK will be sent out, and the SACK list is
64 * generated or updated. From RFC 2018:
65 *
66 * > If sent at all, SACK options SHOULD be included in all ACKs which do
67 * > not ACK the highest sequence number in the data receiver's queue.
68 *
69 * For more information about the SACK list, please check the documentation of
70 * the method GetSackList.
71 *
72 * \see GetSackList
73 * \see UpdateSackList
74 */
75class TcpRxBuffer : public Object
76{
77 public:
78 /**
79 * \brief Get the type ID.
80 * \return the object TypeId
81 */
82 static TypeId GetTypeId();
83 /**
84 * \brief Constructor
85 * \param n initial Sequence number to be received
86 */
87 TcpRxBuffer(uint32_t n = 0);
88 ~TcpRxBuffer() override;
89
90 // Accessors
91 /**
92 * \brief Get Next Rx Sequence number
93 * \returns Next Rx Sequence number
94 */
96 /**
97 * \brief Get the lowest sequence number that this TcpRxBuffer cannot accept
98 * \returns the lowest sequence number that this TcpRxBuffer cannot accept
99 */
101 /**
102 * \brief Increment the Next Sequence number
103 */
104 void IncNextRxSequence();
105 /**
106 * \brief Set the Next Sequence number
107 * \param s the Sequence number
108 */
109 void SetNextRxSequence(const SequenceNumber32& s);
110 /**
111 * \brief Set the FIN Sequence number (i.e., the one closing the connection)
112 * \param s the Sequence number
113 */
114 void SetFinSequence(const SequenceNumber32& s);
115 /**
116 * \brief Get the Maximum buffer size
117 * \returns the Maximum buffer size
118 */
119 uint32_t MaxBufferSize() const;
120 /**
121 * \brief Set the Maximum buffer size
122 * \param s the Maximum buffer size
123 */
125 /**
126 * \brief Get the actual buffer occupancy
127 * \returns buffer occupancy (in bytes)
128 */
129 uint32_t Size() const;
130 /**
131 * \brief Get the actual number of bytes available to be read
132 * \returns size of available data (in bytes)
133 */
134 uint32_t Available() const;
135 /**
136 * \brief Check if the buffer did receive all the data (and the connection is closed)
137 * \returns true if all data have been received
138 */
139 bool Finished();
140
141 /**
142 * Insert a packet into the buffer and update the availBytes counter to
143 * reflect the number of bytes ready to send to the application. This
144 * function handles overlap by trimming the head of the inputted packet and
145 * removing data from the buffer that overlaps the tail of the inputted
146 * packet
147 *
148 * \param p packet
149 * \param tcph packet's TCP header
150 * \return True when success, false otherwise.
151 */
152 bool Add(Ptr<Packet> p, const TcpHeader& tcph);
153
154 /**
155 * Extract data from the head of the buffer as indicated by nextRxSeq.
156 * The extracted data is going to be forwarded to the application.
157 *
158 * \param maxSize maximum number of bytes to extract
159 * \returns a packet
160 */
162
163 /**
164 * \brief Get the sack list
165 *
166 * The sack list can be empty, and it is updated each time Add or Extract
167 * are called through the private method UpdateSackList.
168 *
169 * \return a list of isolated blocks
170 */
172
173 /**
174 * \brief Get the size of Sack list
175 *
176 * \return the size of the sack block list; can be empty
177 */
179
180 /**
181 * \brief Says if a FIN bit has been received
182 * \return true if we received a FIN bit
183 */
184 bool GotFin() const
185 {
186 return m_gotFin;
187 }
188
189 private:
190 /**
191 * \brief Update the sack list, with the block seq starting at the beginning
192 *
193 * Note: the maximum size of the block list is 4. Caller is free to
194 * drop blocks at the end to accommodate header size; from RFC 2018:
195 *
196 * > The data receiver SHOULD include as many distinct SACK blocks as
197 * > possible in the SACK option. Note that the maximum available
198 * > option space may not be sufficient to report all blocks present in
199 * > the receiver's queue.
200 *
201 * In fact, the maximum amount of blocks is 4, and if we consider the timestamp
202 * (or other) options, it is even less. For more detail about this function,
203 * please see the source code and in-line comments.
204 *
205 * \param head sequence number of the block at the beginning
206 * \param tail sequence number of the block at the end
207 */
208 void UpdateSackList(const SequenceNumber32& head, const SequenceNumber32& tail);
209
210 /**
211 * \brief Remove old blocks from the sack list
212 *
213 * Used to remove blocks already delivered to the application.
214 *
215 * After this call, in the SACK list there will be only blocks with
216 * sequence numbers greater than seq; it is perfectly safe to call this
217 * function with an empty sack list.
218 *
219 * \param seq Last sequence to remove
220 */
221 void ClearSackList(const SequenceNumber32& seq);
222
223 TcpOptionSack::SackList m_sackList; //!< Sack list (updated constantly)
224
225 /// container for data stored in the buffer
226 typedef std::map<SequenceNumber32, Ptr<Packet>>::iterator BufIterator;
228 m_nextRxSeq; //!< Seqnum of the first missing byte in data (RCV.NXT)
229 SequenceNumber32 m_finSeq; //!< Seqnum of the FIN packet
230 bool m_gotFin; //!< Did I received FIN packet?
231 uint32_t m_size; //!< Number of total data bytes in the buffer, not necessarily contiguous
232 uint32_t m_maxBuffer; //!< Upper bound of the number of data bytes in buffer (RCV.WND)
233 uint32_t m_availBytes; //!< Number of bytes available to read, i.e. contiguous block at head
234 std::map<SequenceNumber32, Ptr<Packet>> m_data; //!< Corresponding data (may be null)
235};
236
237} // namespace ns3
238
239#endif /* TCP_RX_BUFFER_H */
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Header for the Transmission Control Protocol.
Definition: tcp-header.h:47
std::list< SackBlock > SackList
SACK list definition.
Rx reordering buffer for TCP.
Definition: tcp-rx-buffer.h:76
~TcpRxBuffer() override
uint32_t GetSackListSize() const
Get the size of Sack list.
Ptr< Packet > Extract(uint32_t maxSize)
Extract data from the head of the buffer as indicated by nextRxSeq.
bool Finished()
Check if the buffer did receive all the data (and the connection is closed)
static TypeId GetTypeId()
Get the type ID.
void SetFinSequence(const SequenceNumber32 &s)
Set the FIN Sequence number (i.e., the one closing the connection)
void SetMaxBufferSize(uint32_t s)
Set the Maximum buffer size.
SequenceNumber32 NextRxSequence() const
Get Next Rx Sequence number.
std::map< SequenceNumber32, Ptr< Packet > > m_data
Corresponding data (may be null)
void SetNextRxSequence(const SequenceNumber32 &s)
Set the Next Sequence number.
SequenceNumber32 m_finSeq
Seqnum of the FIN packet.
uint32_t m_availBytes
Number of bytes available to read, i.e.
void IncNextRxSequence()
Increment the Next Sequence number.
uint32_t MaxBufferSize() const
Get the Maximum buffer size.
uint32_t Size() const
Get the actual buffer occupancy.
bool Add(Ptr< Packet > p, const TcpHeader &tcph)
Insert a packet into the buffer and update the availBytes counter to reflect the number of bytes read...
uint32_t m_maxBuffer
Upper bound of the number of data bytes in buffer (RCV.WND)
std::map< SequenceNumber32, Ptr< Packet > >::iterator BufIterator
container for data stored in the buffer
SequenceNumber32 MaxRxSequence() const
Get the lowest sequence number that this TcpRxBuffer cannot accept.
bool GotFin() const
Says if a FIN bit has been received.
TracedValue< SequenceNumber32 > m_nextRxSeq
Seqnum of the first missing byte in data (RCV.NXT)
void UpdateSackList(const SequenceNumber32 &head, const SequenceNumber32 &tail)
Update the sack list, with the block seq starting at the beginning.
uint32_t Available() const
Get the actual number of bytes available to be read.
TcpOptionSack::SackList GetSackList() const
Get the sack list.
uint32_t m_size
Number of total data bytes in the buffer, not necessarily contiguous.
void ClearSackList(const SequenceNumber32 &seq)
Remove old blocks from the sack list.
bool m_gotFin
Did I received FIN packet?
TcpOptionSack::SackList m_sackList
Sack list (updated constantly)
Trace classes with value semantics.
Definition: traced-value.h:116
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.