LEFT | RIGHT |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2010 Adrian Sai-wah Tam | 3 * Copyright (c) 2010 Adrian Sai-wah Tam |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 5 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 2 as | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include <string.h> | 23 #include <string.h> |
24 #include "ns3/packet.h" | 24 #include "ns3/packet.h" |
25 #include "ns3/fatal-error.h" | 25 #include "ns3/fatal-error.h" |
26 #include "ns3/log.h" | 26 #include "ns3/log.h" |
27 #include "tcp-tx-buffer.h" | 27 #include "tcp-tx-buffer.h" |
28 | 28 |
29 NS_LOG_COMPONENT_DEFINE ("TcpTxBuffer"); | 29 NS_LOG_COMPONENT_DEFINE ("TcpTxBuffer"); |
30 | 30 |
31 namespace ns3 { | 31 namespace ns3 { |
32 | 32 |
| 33 TypeId |
| 34 TcpTxBuffer::GetTypeId (void) |
| 35 { |
| 36 static TypeId tid = TypeId ("ns3::TcpTxBuffer") |
| 37 .SetParent<Object> () |
| 38 .AddConstructor<TcpTxBuffer> () |
| 39 .AddTraceSource ("UnackSequence", |
| 40 "First unacknowledged sequence number (SND.UNA)", |
| 41 MakeTraceSourceAccessor (&TcpTxBuffer::m_firstByteSeq)) |
| 42 ; |
| 43 return tid; |
| 44 } |
| 45 |
| 46 /* A user is supposed to create a TcpSocket through a factory. In TcpSocket, |
| 47 * there are attributes SndBufSize and RcvBufSize to control the default Tx and |
| 48 * Rx window sizes respectively, with default of 128 KiByte. The attribute |
| 49 * SndBufSize is passed to TcpTxBuffer by TcpSocketBase::SetSndBufSize() and in |
| 50 * turn, TcpTxBuffer:SetMaxBufferSize(). Therefore, the m_maxBuffer value |
| 51 * initialized below is insignificant. |
| 52 */ |
33 TcpTxBuffer::TcpTxBuffer (uint32_t n) | 53 TcpTxBuffer::TcpTxBuffer (uint32_t n) |
34 : firstByteSeq(n), size (0), maxBuffer(32768), data (0), unused(true) | 54 : m_firstByteSeq(n), m_size (0), m_maxBuffer(32768), m_data (0) |
35 { | 55 { |
36 } | 56 } |
37 | 57 |
38 TcpTxBuffer::~TcpTxBuffer (void) | 58 TcpTxBuffer::~TcpTxBuffer (void) |
39 { | 59 { |
| 60 } |
| 61 |
| 62 SequenceNumber32 |
| 63 TcpTxBuffer::HeadSequence (void) const |
| 64 { |
| 65 return m_firstByteSeq; |
| 66 } |
| 67 |
| 68 SequenceNumber32 |
| 69 TcpTxBuffer::TailSequence (void) const |
| 70 { |
| 71 return m_firstByteSeq + SequenceNumber32 (m_size); |
| 72 } |
| 73 |
| 74 uint32_t |
| 75 TcpTxBuffer::Size (void) const |
| 76 { |
| 77 return m_size; |
| 78 } |
| 79 |
| 80 uint32_t |
| 81 TcpTxBuffer::MaxBufferSize (void) const |
| 82 { |
| 83 return m_maxBuffer; |
| 84 } |
| 85 |
| 86 void |
| 87 TcpTxBuffer::SetMaxBufferSize (uint32_t n) |
| 88 { |
| 89 m_maxBuffer = n; |
| 90 } |
| 91 |
| 92 uint32_t |
| 93 TcpTxBuffer::Available (void) const |
| 94 { |
| 95 return m_maxBuffer - m_size; |
40 } | 96 } |
41 | 97 |
42 bool | 98 bool |
43 TcpTxBuffer::Add (Ptr<Packet> p) | 99 TcpTxBuffer::Add (Ptr<Packet> p) |
44 { | 100 { |
| 101 NS_LOG_FUNCTION (this << p); |
45 NS_LOG_LOGIC ("Packet of size " << p->GetSize () << " appending to window star
ting at " | 102 NS_LOG_LOGIC ("Packet of size " << p->GetSize () << " appending to window star
ting at " |
46 << firstByteSeq << ", availSize="<< Available ()); | 103 << m_firstByteSeq << ", availSize="<< Available ()); |
47 if (p->GetSize () <= Available ()) | 104 if (p->GetSize () <= Available ()) |
48 { | 105 { |
49 if (p->GetSize () > 0) | 106 if (p->GetSize () > 0) |
50 { | 107 { |
51 data.push_back (p); | 108 m_data.push_back (p); |
52 size += p->GetSize (); | 109 m_size += p->GetSize (); |
53 NS_LOG_LOGIC ("Updated size=" << size << ", lastSeq=" << firstByteSeq
+ SequenceNumber (size)); | 110 NS_LOG_LOGIC ("Updated size=" << m_size << ", lastSeq=" << m_firstByte
Seq + SequenceNumber32 (m_size)); |
54 } | 111 } |
55 return true; | 112 return true; |
56 } | 113 } |
57 NS_LOG_LOGIC ("Rejected. Not enough room to buffer packet."); | 114 NS_LOG_LOGIC ("Rejected. Not enough room to buffer packet."); |
58 return false; | 115 return false; |
59 } | 116 } |
60 | 117 |
61 uint32_t | 118 uint32_t |
62 TcpTxBuffer::SizeFromSeq (const SequenceNumber& seq) const | 119 TcpTxBuffer::SizeFromSequence (const SequenceNumber32& seq) const |
63 { | 120 { |
64 NS_LOG_FUNCTION (this << seq); | 121 NS_LOG_FUNCTION (this << seq); |
65 // Sequence of last byte in buffer | 122 // Sequence of last byte in buffer |
66 SequenceNumber lastSeq = firstByteSeq + SequenceNumber (size); | 123 SequenceNumber32 lastSeq = m_firstByteSeq + SequenceNumber32 (m_size); |
67 // Non-negative size | 124 // Non-negative size |
68 NS_LOG_LOGIC ("HeadSeq=" << firstByteSeq << ", lastSeq=" << lastSeq << ", size
=" << size << | 125 NS_LOG_LOGIC ("HeadSeq=" << m_firstByteSeq << ", lastSeq=" << lastSeq << ", si
ze=" << m_size << |
69 ", returns " << lastSeq - seq); | 126 ", returns " << lastSeq - seq); |
70 return lastSeq - seq; | 127 return lastSeq - seq; |
71 } | 128 } |
72 | 129 |
73 Ptr<Packet> | 130 Ptr<Packet> |
74 TcpTxBuffer::CopyFromSeq (uint32_t numBytes, const SequenceNumber& seq) | 131 TcpTxBuffer::CopyFromSequence (uint32_t numBytes, const SequenceNumber32& seq) |
75 { | 132 { |
76 NS_LOG_FUNCTION (this << numBytes << seq); | 133 NS_LOG_FUNCTION (this << numBytes << seq); |
77 uint32_t s = std::min (numBytes, SizeFromSeq (seq)); // Real size to extract.
Insure not beyond end of data | 134 uint32_t s = std::min (numBytes, SizeFromSequence (seq)); // Real size to extr
act. Insure not beyond end of data |
78 if (s == 0) | 135 if (s == 0) |
79 { | 136 { |
80 return Create<Packet> (); // Empty packet returned | 137 return Create<Packet> (); // Empty packet returned |
81 } | 138 } |
82 if (data.size () == 0) | 139 if (m_data.size () == 0) |
83 { // No actual data, just return dummy-data packet of correct size | 140 { // No actual data, just return dummy-data packet of correct size |
84 return Create<Packet> (s); | 141 return Create<Packet> (s); |
85 } | 142 } |
86 | 143 |
87 // Extract data from the buffer and return | 144 // Extract data from the buffer and return |
88 uint32_t offset = seq - firstByteSeq; | 145 uint32_t offset = seq - m_firstByteSeq.Get (); |
89 uint32_t count = 0; // Offset of the first byte of a packet in the buffer | 146 uint32_t count = 0; // Offset of the first byte of a packet in the buffer |
90 uint32_t pktSize = 0; | 147 uint32_t pktSize = 0; |
91 bool beginFound = false; | 148 bool beginFound = false; |
92 int pktCount = 0; | 149 int pktCount = 0; |
93 Ptr<Packet> outPacket; | 150 Ptr<Packet> outPacket; |
94 NS_LOG_LOGIC ("There are " << data.size () << " number of packets in buffer"); | 151 NS_LOG_LOGIC ("There are " << m_data.size () << " number of packets in buffer"
); |
95 for (BufIterator i = data.begin (); i != data.end (); ++i) | 152 for (BufIterator i = m_data.begin (); i != m_data.end (); ++i) |
96 { | 153 { |
97 pktCount++; | 154 pktCount++; |
98 pktSize = (*i)->GetSize (); | 155 pktSize = (*i)->GetSize (); |
99 if (!beginFound) | 156 if (!beginFound) |
100 { // Look for first fragment | 157 { // Look for first fragment |
101 if (count + pktSize > offset) | 158 if (count + pktSize > offset) |
102 { | 159 { |
103 NS_LOG_LOGIC ("First byte found in packet #" << pktCount << " at b
uffer offset " << count | 160 NS_LOG_LOGIC ("First byte found in packet #" << pktCount << " at b
uffer offset " << count |
104 << ", packet len=" << pktSize); | 161 << ", packet len=" << pktSize); |
105 beginFound = true; | 162 beginFound = true; |
106 unused = false; | |
107 uint32_t packetOffset = offset - count; | 163 uint32_t packetOffset = offset - count; |
108 uint32_t fragmentLength = count + pktSize - offset; | 164 uint32_t fragmentLength = count + pktSize - offset; |
109 if (fragmentLength >= s) | 165 if (fragmentLength >= s) |
110 { // Data to be copied falls entirely in this packet | 166 { // Data to be copied falls entirely in this packet |
111 return (*i)->CreateFragment (packetOffset, s); | 167 return (*i)->CreateFragment (packetOffset, s); |
112 } | 168 } |
113 else | 169 else |
114 { // This packet only fulfills part of the request | 170 { // This packet only fulfills part of the request |
115 outPacket = (*i)->CreateFragment (packetOffset, fragmentLength
); | 171 outPacket = (*i)->CreateFragment (packetOffset, fragmentLength
); |
116 } | 172 } |
(...skipping 15 matching lines...) Expand all Loading... |
132 NS_LOG_LOGIC ("Appending to output the packet #" << pktCount << " of o
ffset " << count << " len=" << pktSize); | 188 NS_LOG_LOGIC ("Appending to output the packet #" << pktCount << " of o
ffset " << count << " len=" << pktSize); |
133 outPacket->AddAtEnd (*i); | 189 outPacket->AddAtEnd (*i); |
134 NS_LOG_LOGIC ("Output packet is now of size " << outPacket->GetSize ()
); | 190 NS_LOG_LOGIC ("Output packet is now of size " << outPacket->GetSize ()
); |
135 } | 191 } |
136 count += pktSize; | 192 count += pktSize; |
137 } | 193 } |
138 NS_ASSERT (outPacket->GetSize () == s); | 194 NS_ASSERT (outPacket->GetSize () == s); |
139 return outPacket; | 195 return outPacket; |
140 } | 196 } |
141 | 197 |
142 bool | 198 void |
143 TcpTxBuffer::SetHeadSeq (const SequenceNumber& seq) | 199 TcpTxBuffer::SetHeadSequence (const SequenceNumber32& seq) |
144 { | 200 { |
145 NS_LOG_FUNCTION (this << seq); | 201 NS_LOG_FUNCTION (this << seq); |
146 NS_LOG_LOGIC ("current data size=" << size << ", headSeq=" << firstByteSeq <<
", maxBuffer=" << maxBuffer | 202 m_firstByteSeq = seq; |
147 << ", numPkts=" << data.size () << ", unused buffer=" << unused)
; | 203 } |
| 204 |
| 205 void |
| 206 TcpTxBuffer::DiscardUpTo (const SequenceNumber32& seq) |
| 207 { |
| 208 NS_LOG_FUNCTION (this << seq); |
| 209 NS_LOG_LOGIC ("current data size=" << m_size << ", headSeq=" << m_firstByteSeq
<< ", maxBuffer=" << m_maxBuffer |
| 210 << ", numPkts=" << m_data.size ()); |
148 // Cases do not need to scan the buffer | 211 // Cases do not need to scan the buffer |
149 if (firstByteSeq >= seq) return false; | 212 if (m_firstByteSeq >= seq) return; |
150 if (unused) | |
151 { | |
152 firstByteSeq = seq; | |
153 return false; | |
154 } | |
155 | 213 |
156 // Scan the buffer and discard packets | 214 // Scan the buffer and discard packets |
157 uint32_t offset = seq - firstByteSeq; // Number of bytes to remove | 215 uint32_t offset = seq - m_firstByteSeq.Get (); // Number of bytes to remove |
158 uint32_t pktSize; | 216 uint32_t pktSize; |
159 NS_LOG_LOGIC ("Offset=" << offset); | 217 NS_LOG_LOGIC ("Offset=" << offset); |
160 BufIterator i = data.begin (); | 218 BufIterator i = m_data.begin (); |
161 while (i != data.end ()) | 219 while (i != m_data.end ()) |
162 { | 220 { |
163 if (offset > (*i)->GetSize ()) | 221 if (offset > (*i)->GetSize ()) |
164 { // This packet is behind the seqnum. Remove this packet from the buffe
r | 222 { // This packet is behind the seqnum. Remove this packet from the buffe
r |
165 pktSize = (*i)->GetSize (); | 223 pktSize = (*i)->GetSize (); |
166 size -= pktSize; | 224 m_size -= pktSize; |
167 offset -= pktSize; | 225 offset -= pktSize; |
168 firstByteSeq += pktSize; | 226 m_firstByteSeq += pktSize; |
169 i = data.erase (i); | 227 i = m_data.erase (i); |
170 NS_LOG_LOGIC ("Removed one packet of size " << pktSize << ", offset="
<< offset); | 228 NS_LOG_LOGIC ("Removed one packet of size " << pktSize << ", offset="
<< offset); |
171 } | 229 } |
172 else if (offset > 0) | 230 else if (offset > 0) |
173 { // Part of the packet is behind the seqnum. Fragment | 231 { // Part of the packet is behind the seqnum. Fragment |
174 pktSize = (*i)->GetSize () - offset; | 232 pktSize = (*i)->GetSize () - offset; |
175 *i = (*i)->CreateFragment (offset, pktSize); | 233 *i = (*i)->CreateFragment (offset, pktSize); |
176 size -= offset; | 234 m_size -= offset; |
177 firstByteSeq += offset; | 235 m_firstByteSeq += offset; |
178 NS_LOG_LOGIC ("Fragmented one packet by size " << offset << ", new siz
e=" << pktSize); | 236 NS_LOG_LOGIC ("Fragmented one packet by size " << offset << ", new siz
e=" << pktSize); |
179 break; | 237 break; |
180 } | 238 } |
181 } | 239 } |
182 // Catching the case of ACKing a FIN | 240 // Catching the case of ACKing a FIN |
183 if (size == 0) | 241 if (m_size == 0) |
184 { | 242 { |
185 firstByteSeq = seq; | 243 m_firstByteSeq = seq; |
186 unused = true; | 244 } |
187 } | 245 NS_LOG_LOGIC ("size=" << m_size << " headSeq=" << m_firstByteSeq << " maxBuffe
r=" << m_maxBuffer |
188 NS_LOG_LOGIC ("size=" << size << " headSeq=" << firstByteSeq << " maxBuffer="
<< maxBuffer | 246 <<" numPkts="<< m_data.size ()); |
189 <<" numPkts="<< data.size ()); | 247 NS_ASSERT (m_firstByteSeq == seq); |
190 NS_ASSERT (firstByteSeq == seq); | |
191 return true; | |
192 } | 248 } |
193 | 249 |
194 } // namepsace ns3 | 250 } // namepsace ns3 |
LEFT | RIGHT |