1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2007 Georgia Tech Research Corporation |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation; |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 * |
|
18 * Author: Raj Bhattacharjea <raj.b@gatech.edu> |
|
19 */ |
|
20 |
|
21 |
|
22 #include "ns3/node.h" |
|
23 #include "ns3/inet-socket-address.h" |
|
24 #include "ns3/log.h" |
|
25 #include "ns3/ipv4.h" |
|
26 #include "tcp-socket-impl.h" |
|
27 #include "tcp-l4-protocol.h" |
|
28 #include "ipv4-end-point.h" |
|
29 #include "ipv4-l4-demux.h" |
|
30 #include "ns3/simulation-singleton.h" |
|
31 #include "tcp-typedefs.h" |
|
32 #include "ns3/simulator.h" |
|
33 #include "ns3/packet.h" |
|
34 #include "ns3/uinteger.h" |
|
35 #include "ns3/trace-source-accessor.h" |
|
36 |
|
37 #include <algorithm> |
|
38 |
|
39 NS_LOG_COMPONENT_DEFINE ("TcpSocketImpl"); |
|
40 |
|
41 using namespace std; |
|
42 |
|
43 namespace ns3 { |
|
44 |
|
45 NS_OBJECT_ENSURE_REGISTERED (TcpSocketImpl); |
|
46 |
|
47 TypeId |
|
48 TcpSocketImpl::GetTypeId () |
|
49 { |
|
50 static TypeId tid = TypeId("ns3::TcpSocketImpl") |
|
51 .SetParent<TcpSocket> () |
|
52 .AddTraceSource ("CongestionWindow", |
|
53 "The TCP connection's congestion window", |
|
54 MakeTraceSourceAccessor (&TcpSocketImpl::m_cWnd)) |
|
55 ; |
|
56 return tid; |
|
57 } |
|
58 |
|
59 TcpSocketImpl::TcpSocketImpl () |
|
60 : m_skipRetxResched (false), |
|
61 m_dupAckCount (0), |
|
62 m_delAckCount (0), |
|
63 m_endPoint (0), |
|
64 m_node (0), |
|
65 m_tcp (0), |
|
66 m_errno (ERROR_NOTERROR), |
|
67 m_shutdownSend (false), |
|
68 m_shutdownRecv (false), |
|
69 m_connected (false), |
|
70 m_state (CLOSED), |
|
71 m_closeNotified (false), |
|
72 m_closeRequestNotified (false), |
|
73 m_closeOnEmpty (false), |
|
74 m_pendingClose (false), |
|
75 m_nextTxSequence (0), |
|
76 m_highTxMark (0), |
|
77 m_highestRxAck (0), |
|
78 m_lastRxAck (0), |
|
79 m_nextRxSequence (0), |
|
80 m_pendingData (0), |
|
81 m_rtt (0), |
|
82 m_lastMeasuredRtt (Seconds(0.0)), |
|
83 m_rxAvailable (0), |
|
84 m_wouldBlock (false) |
|
85 { |
|
86 NS_LOG_FUNCTION (this); |
|
87 } |
|
88 |
|
89 TcpSocketImpl::TcpSocketImpl(const TcpSocketImpl& sock) |
|
90 : TcpSocket(sock), //copy the base class callbacks |
|
91 m_skipRetxResched (sock.m_skipRetxResched), |
|
92 m_dupAckCount (sock.m_dupAckCount), |
|
93 m_delAckCount (0), |
|
94 m_delAckMaxCount (sock.m_delAckMaxCount), |
|
95 m_delAckTimeout (sock.m_delAckTimeout), |
|
96 m_endPoint (0), |
|
97 m_node (sock.m_node), |
|
98 m_tcp (sock.m_tcp), |
|
99 m_remoteAddress (sock.m_remoteAddress), |
|
100 m_remotePort (sock.m_remotePort), |
|
101 m_localAddress (sock.m_localAddress), |
|
102 m_localPort (sock.m_localPort), |
|
103 m_errno (sock.m_errno), |
|
104 m_shutdownSend (sock.m_shutdownSend), |
|
105 m_shutdownRecv (sock.m_shutdownRecv), |
|
106 m_connected (sock.m_connected), |
|
107 m_state (sock.m_state), |
|
108 m_closeNotified (sock.m_closeNotified), |
|
109 m_closeRequestNotified (sock.m_closeRequestNotified), |
|
110 m_closeOnEmpty (sock.m_closeOnEmpty), |
|
111 m_pendingClose (sock.m_pendingClose), |
|
112 m_nextTxSequence (sock.m_nextTxSequence), |
|
113 m_highTxMark (sock.m_highTxMark), |
|
114 m_highestRxAck (sock.m_highestRxAck), |
|
115 m_lastRxAck (sock.m_lastRxAck), |
|
116 m_nextRxSequence (sock.m_nextRxSequence), |
|
117 m_pendingData (0), |
|
118 m_segmentSize (sock.m_segmentSize), |
|
119 m_rxWindowSize (sock.m_rxWindowSize), |
|
120 m_advertisedWindowSize (sock.m_advertisedWindowSize), |
|
121 m_cWnd (sock.m_cWnd), |
|
122 m_ssThresh (sock.m_ssThresh), |
|
123 m_initialCWnd (sock.m_initialCWnd), |
|
124 m_rtt (0), |
|
125 m_lastMeasuredRtt (Seconds(0.0)), |
|
126 m_cnTimeout (sock.m_cnTimeout), |
|
127 m_cnCount (sock.m_cnCount), |
|
128 m_rxAvailable (0), |
|
129 m_wouldBlock (false), |
|
130 m_sndBufSize (sock.m_sndBufSize), |
|
131 m_rcvBufSize(sock.m_rcvBufSize) |
|
132 { |
|
133 NS_LOG_FUNCTION_NOARGS (); |
|
134 NS_LOG_LOGIC("Invoked the copy constructor"); |
|
135 //copy the pending data if necessary |
|
136 if(sock.m_pendingData) |
|
137 { |
|
138 m_pendingData = sock.m_pendingData->Copy(); |
|
139 } |
|
140 //copy the rtt if necessary |
|
141 if (sock.m_rtt) |
|
142 { |
|
143 m_rtt = sock.m_rtt->Copy(); |
|
144 } |
|
145 //can't "copy" the endpoint just yes, must do this when we know the peer info |
|
146 //too; this is in SYN_ACK_TX |
|
147 } |
|
148 |
|
149 TcpSocketImpl::~TcpSocketImpl () |
|
150 { |
|
151 NS_LOG_FUNCTION(this); |
|
152 m_node = 0; |
|
153 if (m_endPoint != 0) |
|
154 { |
|
155 NS_ASSERT (m_tcp != 0); |
|
156 /** |
|
157 * Note that this piece of code is a bit tricky: |
|
158 * when DeAllocate is called, it will call into |
|
159 * Ipv4EndPointDemux::Deallocate which triggers |
|
160 * a delete of the associated endPoint which triggers |
|
161 * in turn a call to the method ::Destroy below |
|
162 * will will zero the m_endPoint field. |
|
163 */ |
|
164 NS_ASSERT (m_endPoint != 0); |
|
165 m_tcp->DeAllocate (m_endPoint); |
|
166 NS_ASSERT (m_endPoint == 0); |
|
167 } |
|
168 m_tcp = 0; |
|
169 delete m_pendingData; //prevents leak |
|
170 m_pendingData = 0; |
|
171 } |
|
172 |
|
173 void |
|
174 TcpSocketImpl::SetNode (Ptr<Node> node) |
|
175 { |
|
176 m_node = node; |
|
177 // Initialize some variables |
|
178 m_cWnd = m_initialCWnd * m_segmentSize; |
|
179 m_rxWindowSize = m_advertisedWindowSize; |
|
180 } |
|
181 |
|
182 void |
|
183 TcpSocketImpl::SetTcp (Ptr<TcpL4Protocol> tcp) |
|
184 { |
|
185 m_tcp = tcp; |
|
186 } |
|
187 void |
|
188 TcpSocketImpl::SetRtt (Ptr<RttEstimator> rtt) |
|
189 { |
|
190 m_rtt = rtt; |
|
191 } |
|
192 |
|
193 |
|
194 enum Socket::SocketErrno |
|
195 TcpSocketImpl::GetErrno (void) const |
|
196 { |
|
197 NS_LOG_FUNCTION_NOARGS (); |
|
198 return m_errno; |
|
199 } |
|
200 |
|
201 Ptr<Node> |
|
202 TcpSocketImpl::GetNode (void) const |
|
203 { |
|
204 NS_LOG_FUNCTION_NOARGS (); |
|
205 return m_node; |
|
206 } |
|
207 |
|
208 void |
|
209 TcpSocketImpl::Destroy (void) |
|
210 { |
|
211 NS_LOG_FUNCTION_NOARGS (); |
|
212 m_node = 0; |
|
213 m_endPoint = 0; |
|
214 m_tcp = 0; |
|
215 m_retxEvent.Cancel (); |
|
216 } |
|
217 int |
|
218 TcpSocketImpl::FinishBind (void) |
|
219 { |
|
220 NS_LOG_FUNCTION_NOARGS (); |
|
221 if (m_endPoint == 0) |
|
222 { |
|
223 return -1; |
|
224 } |
|
225 m_endPoint->SetRxCallback (MakeCallback (&TcpSocketImpl::ForwardUp, Ptr<TcpSocketImpl>(this))); |
|
226 m_endPoint->SetDestroyCallback (MakeCallback (&TcpSocketImpl::Destroy, Ptr<TcpSocketImpl>(this))); |
|
227 m_localAddress = m_endPoint->GetLocalAddress (); |
|
228 m_localPort = m_endPoint->GetLocalPort (); |
|
229 return 0; |
|
230 } |
|
231 |
|
232 int |
|
233 TcpSocketImpl::Bind (void) |
|
234 { |
|
235 NS_LOG_FUNCTION_NOARGS (); |
|
236 m_endPoint = m_tcp->Allocate (); |
|
237 return FinishBind (); |
|
238 } |
|
239 int |
|
240 TcpSocketImpl::Bind (const Address &address) |
|
241 { |
|
242 NS_LOG_FUNCTION (this<<address); |
|
243 if (!InetSocketAddress::IsMatchingType (address)) |
|
244 { |
|
245 return ERROR_INVAL; |
|
246 } |
|
247 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); |
|
248 Ipv4Address ipv4 = transport.GetIpv4 (); |
|
249 uint16_t port = transport.GetPort (); |
|
250 if (ipv4 == Ipv4Address::GetAny () && port == 0) |
|
251 { |
|
252 m_endPoint = m_tcp->Allocate (); |
|
253 NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint); |
|
254 } |
|
255 else if (ipv4 == Ipv4Address::GetAny () && port != 0) |
|
256 { |
|
257 m_endPoint = m_tcp->Allocate (port); |
|
258 NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint); |
|
259 } |
|
260 else if (ipv4 != Ipv4Address::GetAny () && port == 0) |
|
261 { |
|
262 m_endPoint = m_tcp->Allocate (ipv4); |
|
263 NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint); |
|
264 } |
|
265 else if (ipv4 != Ipv4Address::GetAny () && port != 0) |
|
266 { |
|
267 m_endPoint = m_tcp->Allocate (ipv4, port); |
|
268 NS_LOG_LOGIC ("TcpSocketImpl "<<this<<" got an endpoint: "<<m_endPoint); |
|
269 } |
|
270 |
|
271 return FinishBind (); |
|
272 } |
|
273 |
|
274 int |
|
275 TcpSocketImpl::ShutdownSend (void) |
|
276 { |
|
277 NS_LOG_FUNCTION_NOARGS (); |
|
278 m_shutdownSend = true; |
|
279 return 0; |
|
280 } |
|
281 int |
|
282 TcpSocketImpl::ShutdownRecv (void) |
|
283 { |
|
284 NS_LOG_FUNCTION_NOARGS (); |
|
285 m_shutdownRecv = false; |
|
286 return 0; |
|
287 } |
|
288 |
|
289 int |
|
290 TcpSocketImpl::Close (void) |
|
291 { |
|
292 NS_LOG_FUNCTION_NOARGS (); |
|
293 if (m_state == CLOSED) |
|
294 { |
|
295 return -1; |
|
296 } |
|
297 if (m_pendingData && m_pendingData->Size() != 0) |
|
298 { // App close with pending data must wait until all data transmitted |
|
299 m_closeOnEmpty = true; |
|
300 NS_LOG_LOGIC("Socket " << this << |
|
301 " deferring close, state " << m_state); |
|
302 return 0; |
|
303 } |
|
304 |
|
305 Actions_t action = ProcessEvent (APP_CLOSE); |
|
306 ProcessAction (action); |
|
307 ShutdownSend (); |
|
308 return 0; |
|
309 } |
|
310 |
|
311 int |
|
312 TcpSocketImpl::Connect (const Address & address) |
|
313 { |
|
314 NS_LOG_FUNCTION (this << address); |
|
315 if (m_endPoint == 0) |
|
316 { |
|
317 if (Bind () == -1) |
|
318 { |
|
319 NS_ASSERT (m_endPoint == 0); |
|
320 return -1; |
|
321 } |
|
322 NS_ASSERT (m_endPoint != 0); |
|
323 } |
|
324 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); |
|
325 m_remoteAddress = transport.GetIpv4 (); |
|
326 m_remotePort = transport.GetPort (); |
|
327 |
|
328 uint32_t localIfIndex; |
|
329 Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> (); |
|
330 |
|
331 if (ipv4->GetIfIndexForDestination (m_remoteAddress, localIfIndex)) |
|
332 { |
|
333 m_endPoint->SetLocalAddress (ipv4->GetAddress (localIfIndex)); |
|
334 } |
|
335 else |
|
336 { |
|
337 m_errno = ERROR_NOROUTETOHOST; |
|
338 return -1; |
|
339 } |
|
340 |
|
341 Actions_t action = ProcessEvent (APP_CONNECT); |
|
342 bool success = ProcessAction (action); |
|
343 if (success) |
|
344 { |
|
345 return 0; |
|
346 } |
|
347 return -1; |
|
348 } |
|
349 int |
|
350 TcpSocketImpl::Send (const Ptr<Packet> p) //p here is just data, no headers |
|
351 { |
|
352 NS_LOG_FUNCTION (this << p); |
|
353 if (m_state == ESTABLISHED || m_state == SYN_SENT || m_state == CLOSE_WAIT) |
|
354 { |
|
355 if (p->GetSize() > GetTxAvailable ()) |
|
356 { |
|
357 m_wouldBlock = true; |
|
358 m_errno = ERROR_MSGSIZE; |
|
359 return -1; |
|
360 } |
|
361 if (!m_pendingData) |
|
362 { |
|
363 m_pendingData = new PendingData (); // Create if non-existent |
|
364 m_firstPendingSequence = m_nextTxSequence; // Note seq of first |
|
365 } |
|
366 //PendingData::Add stores a copy of the Ptr p |
|
367 m_pendingData->Add (p); |
|
368 NS_LOG_DEBUG("TcpSock::Send, pdsize " << m_pendingData->Size() << |
|
369 " state " << m_state); |
|
370 Actions_t action = ProcessEvent (APP_SEND); |
|
371 NS_LOG_DEBUG(" action " << action); |
|
372 if (!ProcessAction (action)) |
|
373 { |
|
374 return -1; // Failed, return zero |
|
375 } |
|
376 return p->GetSize(); |
|
377 } |
|
378 else |
|
379 { |
|
380 m_errno = ERROR_NOTCONN; |
|
381 return -1; |
|
382 } |
|
383 } |
|
384 |
|
385 int TcpSocketImpl::Send (const uint8_t* buf, uint32_t size) |
|
386 { |
|
387 return Send (Create<Packet> (buf, size)); |
|
388 } |
|
389 |
|
390 int TcpSocketImpl::DoSendTo (Ptr<Packet> p, const Address &address) |
|
391 { |
|
392 NS_LOG_FUNCTION (this << p << address); |
|
393 InetSocketAddress transport = InetSocketAddress::ConvertFrom (address); |
|
394 Ipv4Address ipv4 = transport.GetIpv4 (); |
|
395 uint16_t port = transport.GetPort (); |
|
396 return DoSendTo (p, ipv4, port); |
|
397 } |
|
398 |
|
399 int TcpSocketImpl::DoSendTo (Ptr<Packet> p, Ipv4Address ipv4, uint16_t port) |
|
400 { |
|
401 NS_LOG_FUNCTION (this << p << ipv4 << port); |
|
402 if (m_endPoint == 0) |
|
403 { |
|
404 if (Bind () == -1) |
|
405 { |
|
406 NS_ASSERT (m_endPoint == 0); |
|
407 return -1; |
|
408 } |
|
409 NS_ASSERT (m_endPoint != 0); |
|
410 } |
|
411 if (m_shutdownSend) |
|
412 { |
|
413 m_errno = ERROR_SHUTDOWN; |
|
414 return -1; |
|
415 } |
|
416 m_tcp->Send (p, m_endPoint->GetLocalAddress (), ipv4, |
|
417 m_endPoint->GetLocalPort (), port); |
|
418 NotifyDataSent (p->GetSize ()); |
|
419 return 0; |
|
420 } |
|
421 |
|
422 int |
|
423 TcpSocketImpl::SendTo (Ptr<Packet> p, const Address &address) |
|
424 { |
|
425 NS_LOG_FUNCTION (this << address << p); |
|
426 if (!m_connected) |
|
427 { |
|
428 m_errno = ERROR_NOTCONN; |
|
429 return -1; |
|
430 } |
|
431 else |
|
432 { |
|
433 return Send (p); //drop the address according to BSD manpages |
|
434 } |
|
435 } |
|
436 |
|
437 uint32_t |
|
438 TcpSocketImpl::GetTxAvailable (void) const |
|
439 { |
|
440 NS_LOG_FUNCTION_NOARGS (); |
|
441 if (m_pendingData != 0) |
|
442 { |
|
443 uint32_t unAckedDataSize = |
|
444 m_pendingData->SizeFromSeq (m_firstPendingSequence, m_highestRxAck); |
|
445 NS_ASSERT (m_sndBufSize >= unAckedDataSize); //else a logical error |
|
446 return m_sndBufSize-unAckedDataSize; |
|
447 } |
|
448 else |
|
449 { |
|
450 return m_sndBufSize; |
|
451 } |
|
452 } |
|
453 |
|
454 int |
|
455 TcpSocketImpl::Listen (uint32_t q) |
|
456 { |
|
457 NS_LOG_FUNCTION (this << q); |
|
458 Actions_t action = ProcessEvent (APP_LISTEN); |
|
459 ProcessAction (action); |
|
460 return 0; |
|
461 } |
|
462 |
|
463 Ptr<Packet> |
|
464 TcpSocketImpl::Recv (uint32_t maxSize, uint32_t flags) |
|
465 { |
|
466 NS_LOG_FUNCTION_NOARGS (); |
|
467 if(m_bufferedData.empty()) |
|
468 { |
|
469 return 0; |
|
470 } |
|
471 UnAckData_t out; //serves as buffer to return up to the user |
|
472 UnAckData_t::iterator i; |
|
473 while (!m_bufferedData.empty ()) |
|
474 { // Check the buffered data for delivery |
|
475 NS_LOG_LOGIC("TCP " << this << " bufferedData.size() " |
|
476 << m_bufferedData.size () |
|
477 << " time " << Simulator::Now ()); |
|
478 i = m_bufferedData.begin (); |
|
479 SequenceNumber s1 = 0; |
|
480 if (i->first > m_nextRxSequence) |
|
481 { |
|
482 break; // we're done, no more in-sequence data exits |
|
483 } |
|
484 else // (i->first <= m_nextRxSequence) |
|
485 { // Two cases here. |
|
486 // 1) seq + length > nextRxSeq, can deliver partial |
|
487 // 2) seq + length <= nextRxSeq, deliver whole |
|
488 s1 = i->second->GetSize (); |
|
489 if (i->first + s1 > m_nextRxSequence) |
|
490 { // Remove partial data to prepare for delivery |
|
491 uint32_t avail = s1 + i->first - m_nextRxSequence; |
|
492 i->second = i->second->CreateFragment (0, avail); |
|
493 } |
|
494 // else this packet is okay to deliver whole |
|
495 // so don't do anything else and output it |
|
496 out[i->first] = i->second; |
|
497 } |
|
498 m_rxAvailable -= i->second->GetSize (); |
|
499 m_bufferedData.erase (i); // Remove from list |
|
500 } |
|
501 if (out.size() == 0) |
|
502 { |
|
503 return 0; |
|
504 } |
|
505 Ptr<Packet> outPacket = Create<Packet>(); |
|
506 for(i = out.begin(); i!=out.end(); ++i) |
|
507 { |
|
508 if (outPacket->GetSize() + i->second->GetSize() <= maxSize ) |
|
509 { |
|
510 outPacket->AddAtEnd(i->second); |
|
511 } |
|
512 else |
|
513 { |
|
514 //only append as much as will fit |
|
515 uint32_t avail = maxSize - outPacket->GetSize(); |
|
516 outPacket->AddAtEnd(i->second->CreateFragment(0,avail)); |
|
517 //put the rest back into the buffer |
|
518 m_bufferedData[i->first+SequenceNumber(avail)] |
|
519 = i->second->CreateFragment(avail,i->second->GetSize()-avail); |
|
520 m_rxAvailable += i->second->GetSize()-avail; |
|
521 } |
|
522 } |
|
523 return outPacket; |
|
524 } |
|
525 |
|
526 uint32_t |
|
527 TcpSocketImpl::GetRxAvailable (void) const |
|
528 { |
|
529 NS_LOG_FUNCTION_NOARGS (); |
|
530 // We separately maintain this state to avoid walking the queue |
|
531 // every time this might be called |
|
532 return m_rxAvailable; |
|
533 } |
|
534 |
|
535 void |
|
536 TcpSocketImpl::ForwardUp (Ptr<Packet> packet, Ipv4Address ipv4, uint16_t port) |
|
537 { |
|
538 NS_LOG_DEBUG("Socket " << this << " got forward up" << |
|
539 " dport " << m_endPoint->GetLocalPort() << |
|
540 " daddr " << m_endPoint->GetLocalAddress() << |
|
541 " sport " << m_endPoint->GetPeerPort() << |
|
542 " saddr " << m_endPoint->GetPeerAddress()); |
|
543 |
|
544 NS_LOG_FUNCTION (this << packet << ipv4 << port); |
|
545 if (m_shutdownRecv) |
|
546 { |
|
547 return; |
|
548 } |
|
549 TcpHeader tcpHeader; |
|
550 packet->RemoveHeader (tcpHeader); |
|
551 |
|
552 if (tcpHeader.GetFlags () & TcpHeader::ACK) |
|
553 { |
|
554 Time m = m_rtt->AckSeq (tcpHeader.GetAckNumber () ); |
|
555 if (m != Seconds (0.0)) |
|
556 { |
|
557 m_lastMeasuredRtt = m; |
|
558 } |
|
559 } |
|
560 |
|
561 Events_t event = SimulationSingleton<TcpStateMachine>::Get ()->FlagsEvent (tcpHeader.GetFlags () ); |
|
562 Actions_t action = ProcessEvent (event); //updates the state |
|
563 Address address = InetSocketAddress (ipv4, port); |
|
564 NS_LOG_DEBUG("Socket " << this << |
|
565 " processing pkt action, " << action << |
|
566 " current state " << m_state); |
|
567 ProcessPacketAction (action, packet, tcpHeader, address); |
|
568 } |
|
569 |
|
570 Actions_t TcpSocketImpl::ProcessEvent (Events_t e) |
|
571 { |
|
572 NS_LOG_FUNCTION (this << e); |
|
573 States_t saveState = m_state; |
|
574 NS_LOG_LOGIC ("TcpSocketImpl " << this << " processing event " << e); |
|
575 // simulation singleton is a way to get a single global static instance of a |
|
576 // class intended to be a singleton; see simulation-singleton.h |
|
577 SA stateAction = SimulationSingleton<TcpStateMachine>::Get ()->Lookup (m_state,e); |
|
578 // debug |
|
579 if (stateAction.action == RST_TX) |
|
580 { |
|
581 NS_LOG_LOGIC ("TcpSocketImpl " << this << " sending RST from state " |
|
582 << saveState << " event " << e); |
|
583 } |
|
584 bool needCloseNotify = (stateAction.state == CLOSED && m_state != CLOSED |
|
585 && e != TIMEOUT); |
|
586 m_state = stateAction.state; |
|
587 NS_LOG_LOGIC ("TcpSocketImpl " << this << " moved from state " << saveState |
|
588 << " to state " <<m_state); |
|
589 NS_LOG_LOGIC ("TcpSocketImpl " << this << " pendingData " << m_pendingData); |
|
590 |
|
591 //extra event logic is here for RX events |
|
592 //e = SYN_ACK_RX |
|
593 if (saveState == SYN_SENT && m_state == ESTABLISHED) |
|
594 // this means the application side has completed its portion of |
|
595 // the handshaking |
|
596 { |
|
597 Simulator::ScheduleNow(&TcpSocketImpl::ConnectionSucceeded, this); |
|
598 //NotifyConnectionSucceeded (); |
|
599 m_connected = true; |
|
600 m_endPoint->SetPeer (m_remoteAddress, m_remotePort); |
|
601 NS_LOG_LOGIC ("TcpSocketImpl " << this << " Connected!"); |
|
602 } |
|
603 |
|
604 if (needCloseNotify && !m_closeNotified) |
|
605 { |
|
606 NS_LOG_LOGIC ("TcpSocketImpl " << this << " transition to CLOSED from " |
|
607 << m_state << " event " << e << " closeNot " << m_closeNotified |
|
608 << " action " << stateAction.action); |
|
609 NotifyCloseCompleted (); |
|
610 m_closeNotified = true; |
|
611 NS_LOG_LOGIC ("TcpSocketImpl " << this << " calling Closed from PE" |
|
612 << " origState " << saveState |
|
613 << " event " << e); |
|
614 NS_LOG_LOGIC ("TcpSocketImpl " << this << " transition to CLOSED from " |
|
615 << m_state << " event " << e |
|
616 << " set CloseNotif "); |
|
617 } |
|
618 return stateAction.action; |
|
619 } |
|
620 |
|
621 void TcpSocketImpl::SendEmptyPacket (uint8_t flags) |
|
622 { |
|
623 NS_LOG_FUNCTION (this << flags); |
|
624 Ptr<Packet> p = Create<Packet> (); |
|
625 TcpHeader header; |
|
626 |
|
627 header.SetFlags (flags); |
|
628 header.SetSequenceNumber (m_nextTxSequence); |
|
629 header.SetAckNumber (m_nextRxSequence); |
|
630 header.SetSourcePort (m_endPoint->GetLocalPort ()); |
|
631 header.SetDestinationPort (m_remotePort); |
|
632 header.SetWindowSize (m_advertisedWindowSize); |
|
633 m_tcp->SendPacket (p, header, m_endPoint->GetLocalAddress (), |
|
634 m_remoteAddress); |
|
635 Time rto = m_rtt->RetransmitTimeout (); |
|
636 if (flags & TcpHeader::SYN) |
|
637 { |
|
638 rto = m_cnTimeout; |
|
639 m_cnTimeout = m_cnTimeout + m_cnTimeout; |
|
640 m_cnCount--; |
|
641 } |
|
642 if (m_retxEvent.IsExpired () ) //no outstanding timer |
|
643 { |
|
644 NS_LOG_LOGIC ("Schedule retransmission timeout at time " |
|
645 << Simulator::Now ().GetSeconds () << " to expire at time " |
|
646 << (Simulator::Now () + rto).GetSeconds ()); |
|
647 m_retxEvent = Simulator::Schedule (rto, &TcpSocketImpl::ReTxTimeout, this); |
|
648 } |
|
649 } |
|
650 |
|
651 bool TcpSocketImpl::ProcessAction (Actions_t a) |
|
652 { // These actions do not require a packet or any TCP Headers |
|
653 NS_LOG_FUNCTION (this << a); |
|
654 switch (a) |
|
655 { |
|
656 case NO_ACT: |
|
657 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action: NO_ACT"); |
|
658 break; |
|
659 case ACK_TX: |
|
660 SendEmptyPacket (TcpHeader::ACK); |
|
661 break; |
|
662 case ACK_TX_1: |
|
663 NS_ASSERT (false); // This should be processed in ProcessPacketAction |
|
664 break; |
|
665 case RST_TX: |
|
666 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action RST_TX"); |
|
667 SendEmptyPacket (TcpHeader::RST); |
|
668 break; |
|
669 case SYN_TX: |
|
670 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action SYN_TX"); |
|
671 // TCP SYN Flag consumes one byte |
|
672 // is the above correct? we're SENDING a syn, not acking back -- Raj |
|
673 // commented out for now |
|
674 // m_nextTxSequence+= 1; |
|
675 SendEmptyPacket (TcpHeader::SYN); |
|
676 break; |
|
677 case SYN_ACK_TX: |
|
678 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action SYN_ACK_TX"); |
|
679 // TCP SYN Flag consumes one byte |
|
680 ++m_nextRxSequence; |
|
681 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
|
682 break; |
|
683 case FIN_TX: |
|
684 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action FIN_TX"); |
|
685 SendEmptyPacket (TcpHeader::FIN); |
|
686 break; |
|
687 case FIN_ACK_TX: |
|
688 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action FIN_ACK_TX"); |
|
689 SendEmptyPacket (TcpHeader::FIN | TcpHeader::ACK); |
|
690 break; |
|
691 case NEW_ACK: |
|
692 NS_ASSERT (false); // This should be processed in ProcessPacketAction |
|
693 break; |
|
694 case NEW_SEQ_RX: |
|
695 NS_ASSERT (false); // This should be processed in ProcessPacketAction |
|
696 break; |
|
697 case RETX: |
|
698 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action RETX"); |
|
699 break; |
|
700 case TX_DATA: |
|
701 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action TX_DATA"); |
|
702 SendPendingData (); |
|
703 break; |
|
704 case PEER_CLOSE: |
|
705 NS_ASSERT (false); // This should be processed in ProcessPacketAction |
|
706 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action PEER_CLOSE"); |
|
707 break; |
|
708 case APP_CLOSED: |
|
709 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action APP_CLOSED"); |
|
710 break; |
|
711 case CANCEL_TM: |
|
712 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action CANCEL_TM"); |
|
713 break; |
|
714 case APP_NOTIFY: |
|
715 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action APP_NOTIFY"); |
|
716 break; |
|
717 case SERV_NOTIFY: |
|
718 NS_ASSERT (false); // This should be processed in ProcessPacketAction |
|
719 break; |
|
720 case LAST_ACTION: |
|
721 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action LAST_ACTION"); |
|
722 break; |
|
723 } |
|
724 return true; |
|
725 } |
|
726 |
|
727 bool TcpSocketImpl::ProcessPacketAction (Actions_t a, Ptr<Packet> p, |
|
728 const TcpHeader& tcpHeader, |
|
729 const Address& fromAddress) |
|
730 { |
|
731 NS_LOG_FUNCTION (this << a << p << fromAddress); |
|
732 uint32_t localIfIndex; |
|
733 Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> (); |
|
734 switch (a) |
|
735 { |
|
736 case SYN_ACK_TX: |
|
737 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action SYN_ACK_TX"); |
|
738 // m_remotePort = InetSocketAddress::ConvertFrom (fromAddress).GetPort (); |
|
739 // m_remoteAddress = InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (); |
|
740 // if (ipv4->GetIfIndexForDestination (m_remoteAddress, localIfIndex)) |
|
741 // { |
|
742 // m_localAddress = ipv4->GetAddress (localIfIndex); |
|
743 // } |
|
744 if (m_state == LISTEN) //this means we should fork a new TcpSocketImpl |
|
745 { |
|
746 NS_LOG_DEBUG("In SYN_ACK_TX, m_state is LISTEN, this " << this); |
|
747 //notify the server that we got a SYN |
|
748 // If server refuses connection do nothing |
|
749 if (!NotifyConnectionRequest(fromAddress)) return true; |
|
750 // Clone the socket |
|
751 Ptr<TcpSocketImpl> newSock = Copy (); |
|
752 NS_LOG_LOGIC ("Cloned a TcpSocketImpl " << newSock); |
|
753 //this listening socket should do nothing more |
|
754 Simulator::ScheduleNow (&TcpSocketImpl::CompleteFork, newSock, |
|
755 p, tcpHeader,fromAddress); |
|
756 return true; |
|
757 } |
|
758 // This is the cloned endpoint |
|
759 m_endPoint->SetPeer (m_remoteAddress, m_remotePort); |
|
760 if (ipv4->GetIfIndexForDestination (m_remoteAddress, localIfIndex)) |
|
761 { |
|
762 m_localAddress = ipv4->GetAddress (localIfIndex); |
|
763 m_endPoint->SetLocalAddress (m_localAddress); |
|
764 // Leave local addr in the portmap to any, as the path from |
|
765 // remote can change and packets can arrive on different interfaces |
|
766 //m_endPoint->SetLocalAddress (Ipv4Address::GetAny()); |
|
767 } |
|
768 // TCP SYN consumes one byte |
|
769 m_nextRxSequence = tcpHeader.GetSequenceNumber() + SequenceNumber(1); |
|
770 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
|
771 break; |
|
772 case ACK_TX_1: |
|
773 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action ACK_TX_1"); |
|
774 // TCP SYN consumes one byte |
|
775 m_nextRxSequence = tcpHeader.GetSequenceNumber() + SequenceNumber(1); |
|
776 m_nextTxSequence = tcpHeader.GetAckNumber (); |
|
777 m_firstPendingSequence = m_nextTxSequence; //bug 166 |
|
778 NS_LOG_DEBUG ("TcpSocketImpl " << this << " ACK_TX_1" << |
|
779 " nextRxSeq " << m_nextRxSequence); |
|
780 SendEmptyPacket (TcpHeader::ACK); |
|
781 m_rxWindowSize = tcpHeader.GetWindowSize (); |
|
782 if (tcpHeader.GetAckNumber () > m_highestRxAck) |
|
783 { |
|
784 m_highestRxAck = tcpHeader.GetAckNumber (); |
|
785 // Data freed from the send buffer; notify any blocked sender |
|
786 if (m_wouldBlock) |
|
787 { |
|
788 NotifySend (GetTxAvailable ()); |
|
789 m_wouldBlock = false; |
|
790 } |
|
791 } |
|
792 SendPendingData (); |
|
793 break; |
|
794 case NEW_ACK: |
|
795 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action NEW_ACK_TX"); |
|
796 if (tcpHeader.GetAckNumber () < m_highestRxAck) //old ack, do nothing |
|
797 { |
|
798 break; |
|
799 } |
|
800 if (tcpHeader.GetAckNumber () == m_highestRxAck && |
|
801 tcpHeader.GetAckNumber () < m_nextTxSequence) |
|
802 { |
|
803 DupAck (tcpHeader, ++m_dupAckCount); |
|
804 break; |
|
805 } |
|
806 if (tcpHeader.GetAckNumber () > m_highestRxAck) |
|
807 { |
|
808 m_dupAckCount = 0; |
|
809 } |
|
810 NewAck (tcpHeader.GetAckNumber ()); |
|
811 break; |
|
812 case NEW_SEQ_RX: |
|
813 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action NEW_SEQ_RX"); |
|
814 NewRx (p, tcpHeader, fromAddress); // Process new data received |
|
815 break; |
|
816 case PEER_CLOSE: |
|
817 { |
|
818 // First we have to be sure the FIN packet was not received |
|
819 // out of sequence. If so, note pending close and process |
|
820 // new sequence rx |
|
821 if (tcpHeader.GetSequenceNumber () != m_nextRxSequence) |
|
822 { // process close later |
|
823 m_pendingClose = true; |
|
824 NS_LOG_LOGIC ("TcpSocketImpl " << this << " setting pendingClose" |
|
825 << " rxseq " << tcpHeader.GetSequenceNumber () |
|
826 << " nextRxSeq " << m_nextRxSequence); |
|
827 NewRx (p, tcpHeader, fromAddress); |
|
828 return true; |
|
829 } |
|
830 // Now we need to see if any data came with the FIN |
|
831 // if so, call NewRx |
|
832 if (p->GetSize () != 0) |
|
833 { |
|
834 NewRx (p, tcpHeader, fromAddress); |
|
835 } |
|
836 States_t saveState = m_state; // Used to see if app responds |
|
837 NS_LOG_LOGIC ("TcpSocketImpl " << this |
|
838 << " peer close, state " << m_state); |
|
839 if (!m_closeRequestNotified) |
|
840 { |
|
841 NS_LOG_LOGIC ("TCP " << this |
|
842 << " calling AppCloseRequest"); |
|
843 NotifyCloseRequested(); |
|
844 m_closeRequestNotified = true; |
|
845 } |
|
846 NS_LOG_LOGIC ("TcpSocketImpl " << this |
|
847 << " peer close, state after " << m_state); |
|
848 if (m_state == saveState) |
|
849 { // Need to ack, the application will close later |
|
850 SendEmptyPacket (TcpHeader::ACK); |
|
851 // // Also need to re-tx the ack if we |
|
852 } |
|
853 if (m_state == LAST_ACK) |
|
854 { |
|
855 NS_LOG_LOGIC ("TcpSocketImpl " << this << " scheduling LATO1"); |
|
856 m_lastAckEvent = Simulator::Schedule (m_rtt->RetransmitTimeout (), |
|
857 &TcpSocketImpl::LastAckTimeout,this); |
|
858 } |
|
859 break; |
|
860 } |
|
861 case SERV_NOTIFY: |
|
862 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action SERV_NOTIFY"); |
|
863 NS_LOG_LOGIC ("TcpSocketImpl " << this << " Connected!"); |
|
864 NotifyNewConnectionCreated (this, fromAddress); |
|
865 m_connected = true; // ! This is bogus; fix when we clone the tcp |
|
866 m_endPoint->SetPeer (m_remoteAddress, m_remotePort); |
|
867 //treat the connection orientation final ack as a newack |
|
868 CommonNewAck (tcpHeader.GetAckNumber (), true); |
|
869 break; |
|
870 default: |
|
871 break; |
|
872 } |
|
873 return true; |
|
874 } |
|
875 |
|
876 void TcpSocketImpl::CompleteFork(Ptr<Packet> p, const TcpHeader& h, const Address& fromAddress) |
|
877 { |
|
878 // Get port and address from peer (connecting host) |
|
879 m_remotePort = InetSocketAddress::ConvertFrom (fromAddress).GetPort (); |
|
880 m_remoteAddress = InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (); |
|
881 m_endPoint = m_tcp->Allocate (m_localAddress, |
|
882 m_localPort, |
|
883 m_remoteAddress, |
|
884 m_remotePort); |
|
885 //the cloned socket with be in listen state, so manually change state |
|
886 m_state = SYN_RCVD; |
|
887 //equivalent to FinishBind |
|
888 m_endPoint->SetRxCallback (MakeCallback (&TcpSocketImpl::ForwardUp, Ptr<TcpSocketImpl>(this))); |
|
889 m_endPoint->SetDestroyCallback (MakeCallback (&TcpSocketImpl::Destroy, Ptr<TcpSocketImpl>(this))); |
|
890 ProcessPacketAction(SYN_ACK_TX, p, h, fromAddress); |
|
891 } |
|
892 |
|
893 void TcpSocketImpl::ConnectionSucceeded() |
|
894 { // We would preferred to have scheduled an event directly to |
|
895 // NotifyConnectionSucceeded, but (sigh) these are protected |
|
896 // and we can get the address of it :( |
|
897 NotifyConnectionSucceeded(); |
|
898 } |
|
899 |
|
900 bool TcpSocketImpl::SendPendingData (bool withAck) |
|
901 { |
|
902 NS_LOG_FUNCTION (this << withAck); |
|
903 NS_LOG_LOGIC ("ENTERING SendPendingData"); |
|
904 if (!m_pendingData) |
|
905 { |
|
906 return false; // No data exists |
|
907 } |
|
908 uint32_t nPacketsSent = 0; |
|
909 while (m_pendingData->SizeFromSeq (m_firstPendingSequence, m_nextTxSequence)) |
|
910 { |
|
911 uint32_t w = AvailableWindow ();// Get available window size |
|
912 NS_LOG_LOGIC ("TcpSocketImpl " << this << " SendPendingData" |
|
913 << " w " << w |
|
914 << " rxwin " << m_rxWindowSize |
|
915 << " cWnd " << m_cWnd |
|
916 << " segsize " << m_segmentSize |
|
917 << " nextTxSeq " << m_nextTxSequence |
|
918 << " highestRxAck " << m_highestRxAck |
|
919 << " pd->Size " << m_pendingData->Size () |
|
920 << " pd->SFS " << m_pendingData->SizeFromSeq (m_firstPendingSequence, m_nextTxSequence)); |
|
921 |
|
922 if (w < m_segmentSize && m_pendingData->Size () > w) |
|
923 { |
|
924 break; // No more |
|
925 } |
|
926 uint32_t s = std::min (w, m_segmentSize); // Send no more than window |
|
927 Ptr<Packet> p = m_pendingData->CopyFromSeq (s, m_firstPendingSequence, |
|
928 m_nextTxSequence); |
|
929 NS_LOG_LOGIC("TcpSocketImpl " << this << " SendPendingData" |
|
930 << " txseq " << m_nextTxSequence |
|
931 << " s " << s |
|
932 << " datasize " << p->GetSize() ); |
|
933 uint8_t flags = 0; |
|
934 if (withAck) |
|
935 { |
|
936 flags |= TcpHeader::ACK; |
|
937 } |
|
938 uint32_t sz = p->GetSize (); // Size of packet |
|
939 uint32_t remainingData = m_pendingData->SizeFromSeq( |
|
940 m_firstPendingSequence, |
|
941 m_nextTxSequence + SequenceNumber (sz)); |
|
942 if (m_closeOnEmpty && (remainingData == 0)) |
|
943 { |
|
944 flags = TcpHeader::FIN; |
|
945 m_state = FIN_WAIT_1; |
|
946 } |
|
947 |
|
948 TcpHeader header; |
|
949 header.SetFlags (flags); |
|
950 header.SetSequenceNumber (m_nextTxSequence); |
|
951 header.SetAckNumber (m_nextRxSequence); |
|
952 header.SetSourcePort (m_endPoint->GetLocalPort()); |
|
953 header.SetDestinationPort (m_remotePort); |
|
954 if (m_shutdownSend) |
|
955 { |
|
956 m_errno = ERROR_SHUTDOWN; |
|
957 return -1; |
|
958 } |
|
959 |
|
960 |
|
961 if (m_retxEvent.IsExpired () ) //go ahead and schedule the retransmit |
|
962 { |
|
963 Time rto = m_rtt->RetransmitTimeout (); |
|
964 NS_LOG_LOGIC ("SendPendingData Schedule retransmission timeout at time " << |
|
965 Simulator::Now ().GetSeconds () << " to expire at time " << |
|
966 (Simulator::Now () + rto).GetSeconds () ); |
|
967 m_retxEvent = Simulator::Schedule (rto,&TcpSocketImpl::ReTxTimeout,this); |
|
968 } |
|
969 NS_LOG_LOGIC ("About to send a packet with flags: " << flags); |
|
970 m_tcp->SendPacket (p, header, |
|
971 m_endPoint->GetLocalAddress (), |
|
972 m_remoteAddress); |
|
973 m_rtt->SentSeq(m_nextTxSequence, sz); // notify the RTT |
|
974 // Notify the application |
|
975 Simulator::ScheduleNow(&TcpSocketImpl::NotifyDataSent, this, p->GetSize ()); |
|
976 nPacketsSent++; // Count sent this loop |
|
977 m_nextTxSequence += sz; // Advance next tx sequence |
|
978 // Note the high water mark |
|
979 m_highTxMark = std::max (m_nextTxSequence, m_highTxMark); |
|
980 } |
|
981 NS_LOG_LOGIC ("SendPendingData Sent "<<nPacketsSent<<" packets"); |
|
982 NS_LOG_LOGIC("RETURN SendPendingData"); |
|
983 return (nPacketsSent>0); |
|
984 } |
|
985 |
|
986 uint32_t TcpSocketImpl::UnAckDataCount () |
|
987 { |
|
988 NS_LOG_FUNCTION_NOARGS (); |
|
989 return m_nextTxSequence - m_highestRxAck; |
|
990 } |
|
991 |
|
992 uint32_t TcpSocketImpl::BytesInFlight () |
|
993 { |
|
994 NS_LOG_FUNCTION_NOARGS (); |
|
995 return m_highTxMark - m_highestRxAck; |
|
996 } |
|
997 |
|
998 uint32_t TcpSocketImpl::Window () |
|
999 { |
|
1000 NS_LOG_FUNCTION_NOARGS (); |
|
1001 NS_LOG_LOGIC ("TcpSocketImpl::Window() "<<this); |
|
1002 return std::min (m_rxWindowSize, m_cWnd.Get()); |
|
1003 } |
|
1004 |
|
1005 uint32_t TcpSocketImpl::AvailableWindow () |
|
1006 { |
|
1007 NS_LOG_FUNCTION_NOARGS (); |
|
1008 uint32_t unack = UnAckDataCount (); // Number of outstanding bytes |
|
1009 uint32_t win = Window (); |
|
1010 if (win < unack) |
|
1011 { |
|
1012 return 0; // No space available |
|
1013 } |
|
1014 return (win - unack); // Amount of window space available |
|
1015 } |
|
1016 |
|
1017 void TcpSocketImpl::NewRx (Ptr<Packet> p, |
|
1018 const TcpHeader& tcpHeader, |
|
1019 const Address& fromAddress) |
|
1020 { |
|
1021 NS_LOG_FUNCTION (this << p << "tcpHeader " << fromAddress); |
|
1022 NS_LOG_LOGIC ("TcpSocketImpl " << this << " NewRx," |
|
1023 << " seq " << tcpHeader.GetSequenceNumber() |
|
1024 << " ack " << tcpHeader.GetAckNumber() |
|
1025 << " p.size is " << p->GetSize () ); |
|
1026 NS_LOG_DEBUG ("TcpSocketImpl " << this << |
|
1027 " NewRx," << |
|
1028 " seq " << tcpHeader.GetSequenceNumber() << |
|
1029 " ack " << tcpHeader.GetAckNumber() << |
|
1030 " p.size is " << p->GetSize()); |
|
1031 States_t origState = m_state; |
|
1032 uint32_t s = p->GetSize (); // Size of associated data |
|
1033 if (s == 0) |
|
1034 {// Nothing to do if no associated data |
|
1035 return; |
|
1036 } |
|
1037 // Log sequence received if enabled |
|
1038 // NoteTimeSeq(LOG_SEQ_RX, h->sequenceNumber); |
|
1039 // Three possibilities |
|
1040 // 1) Received seq is expected, buffer this, update rxAvailable, and ack |
|
1041 // 2) Received seq is < expected, just re-ack previous |
|
1042 // 3) Received seq is > expected, just re-ack previous and buffer data |
|
1043 if (tcpHeader.GetSequenceNumber () == m_nextRxSequence) |
|
1044 { // If seq is expected seq |
|
1045 // 1) Update nextRxSeq |
|
1046 // 2) Buffer this packet so Recv can read it |
|
1047 // 3) Send the ack |
|
1048 m_nextRxSequence += s; // Advance next expected sequence |
|
1049 //bytesReceived += s; // Statistics |
|
1050 NS_LOG_LOGIC("Case 1, advanced nrxs to " << m_nextRxSequence ); |
|
1051 SocketRxAddressTag tag; |
|
1052 tag.SetAddress (fromAddress); |
|
1053 p->AddTag (tag); |
|
1054 //buffer this, it'll be read by call to Recv |
|
1055 UnAckData_t::iterator i = |
|
1056 m_bufferedData.find (tcpHeader.GetSequenceNumber () ); |
|
1057 if (i != m_bufferedData.end () ) //we found it already in the buffer |
|
1058 { |
|
1059 i->second = 0; // relase reference to already buffered |
|
1060 } |
|
1061 // Save for later delivery |
|
1062 m_bufferedData[tcpHeader.GetSequenceNumber () ] = p; |
|
1063 m_rxAvailable += p->GetSize (); |
|
1064 //putting this into the buffer might have filled in a sequence gap |
|
1065 //so we have to iterate through the list to find the largest contiguous |
|
1066 //sequenced chunk, and update m_rxAvailable appropriately |
|
1067 i = m_bufferedData.find (tcpHeader.GetSequenceNumber () ); |
|
1068 UnAckData_t::iterator next = i; |
|
1069 next++; |
|
1070 while(next != m_bufferedData.end()) |
|
1071 { |
|
1072 if(i->first + SequenceNumber(i->second->GetSize ()) == next->first) |
|
1073 { |
|
1074 //next packet is in sequence, count it |
|
1075 m_rxAvailable += next->second->GetSize(); |
|
1076 m_nextRxSequence += next->second->GetSize(); |
|
1077 } |
|
1078 else |
|
1079 { |
|
1080 break; //no more in this contiguous chunk |
|
1081 } |
|
1082 ++i; |
|
1083 ++next; |
|
1084 } |
|
1085 NotifyDataRecv (); |
|
1086 if (m_closeNotified) |
|
1087 { |
|
1088 NS_LOG_LOGIC ("Tcp " << this << " HuH? Got data after closeNotif"); |
|
1089 } |
|
1090 NS_LOG_LOGIC ("TcpSocketImpl " << this << " adv rxseq by " << s); |
|
1091 if (m_pendingClose || (origState > ESTABLISHED)) |
|
1092 { // See if we can close now |
|
1093 if (m_bufferedData.empty()) |
|
1094 { |
|
1095 ProcessPacketAction (PEER_CLOSE, p, tcpHeader, fromAddress); |
|
1096 } |
|
1097 } |
|
1098 } |
|
1099 else if (SequenceNumber (tcpHeader.GetSequenceNumber ()) >= m_nextRxSequence) |
|
1100 { // Need to buffer this one |
|
1101 NS_LOG_LOGIC ("Case 2, buffering " << tcpHeader.GetSequenceNumber () ); |
|
1102 UnAckData_t::iterator i = |
|
1103 m_bufferedData.find (tcpHeader.GetSequenceNumber () ); |
|
1104 if (i != m_bufferedData.end () ) |
|
1105 { |
|
1106 i->second = 0; // relase reference to already buffered |
|
1107 } |
|
1108 // Save for later delivery |
|
1109 SocketRxAddressTag tag; |
|
1110 tag.SetAddress (fromAddress); |
|
1111 p->AddTag (tag); |
|
1112 m_bufferedData[tcpHeader.GetSequenceNumber () ] = p; |
|
1113 } |
|
1114 else |
|
1115 { // debug |
|
1116 NS_LOG_LOGIC("TCP " << this |
|
1117 << " got seq " << tcpHeader.GetSequenceNumber () |
|
1118 << " expected " << m_nextRxSequence |
|
1119 << " flags " << tcpHeader.GetFlags ()); |
|
1120 } |
|
1121 // Now send a new ack packet acknowledging all received and delivered data |
|
1122 if(++m_delAckCount >= m_delAckMaxCount) |
|
1123 { |
|
1124 m_delAckEvent.Cancel(); |
|
1125 m_delAckCount = 0; |
|
1126 SendEmptyPacket (TcpHeader::ACK); |
|
1127 } |
|
1128 else |
|
1129 { |
|
1130 m_delAckEvent = Simulator::Schedule (m_delAckTimeout, &TcpSocketImpl::DelAckTimeout, this); |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 void TcpSocketImpl::DelAckTimeout () |
|
1135 { |
|
1136 m_delAckCount = 0; |
|
1137 SendEmptyPacket (TcpHeader::ACK); |
|
1138 } |
|
1139 |
|
1140 void TcpSocketImpl::CommonNewAck (SequenceNumber ack, bool skipTimer) |
|
1141 { // CommonNewAck is called only for "New" (non-duplicate) acks |
|
1142 // and MUST be called by any subclass, from the NewAck function |
|
1143 // Always cancel any pending re-tx timer on new acknowledgement |
|
1144 NS_LOG_FUNCTION (this << ack << skipTimer); |
|
1145 //DEBUG(1,(cout << "TCP " << this << "Cancelling retx timer " << endl)); |
|
1146 if (!skipTimer) |
|
1147 { |
|
1148 m_retxEvent.Cancel (); |
|
1149 //On recieving a "New" ack we restart retransmission timer .. RFC 2988 |
|
1150 Time rto = m_rtt->RetransmitTimeout (); |
|
1151 NS_LOG_LOGIC ("Schedule retransmission timeout at time " |
|
1152 << Simulator::Now ().GetSeconds () << " to expire at time " |
|
1153 << (Simulator::Now () + rto).GetSeconds ()); |
|
1154 m_retxEvent = Simulator::Schedule (rto, &TcpSocketImpl::ReTxTimeout, this); |
|
1155 } |
|
1156 NS_LOG_LOGIC ("TCP " << this << " NewAck " << ack |
|
1157 << " numberAck " << (ack - m_highestRxAck)); // Number bytes ack'ed |
|
1158 m_highestRxAck = ack; // Note the highest recieved Ack |
|
1159 if (m_wouldBlock) |
|
1160 { |
|
1161 // m_highestRxAck advancing means some data was acked, and the size |
|
1162 // of free space in the buffer has increased |
|
1163 NotifySend (GetTxAvailable ()); |
|
1164 m_wouldBlock = false; |
|
1165 } |
|
1166 if (ack > m_nextTxSequence) |
|
1167 { |
|
1168 m_nextTxSequence = ack; // If advanced |
|
1169 } |
|
1170 // See if all pending ack'ed; if so we can delete the data |
|
1171 if (m_pendingData) |
|
1172 { // Data exists, see if can be deleted |
|
1173 if (m_pendingData->SizeFromSeq (m_firstPendingSequence, m_highestRxAck) == 0) |
|
1174 { // All pending acked, can be deleted |
|
1175 m_pendingData->Clear (); |
|
1176 delete m_pendingData; |
|
1177 m_pendingData = 0; |
|
1178 // Insure no re-tx timer |
|
1179 m_retxEvent.Cancel (); |
|
1180 } |
|
1181 } |
|
1182 // Try to send more data |
|
1183 SendPendingData(); |
|
1184 } |
|
1185 |
|
1186 Ptr<TcpSocketImpl> TcpSocketImpl::Copy () |
|
1187 { |
|
1188 return CopyObject<TcpSocketImpl> (this); |
|
1189 } |
|
1190 |
|
1191 void TcpSocketImpl::NewAck (SequenceNumber seq) |
|
1192 { // New acknowledgement up to sequence number "seq" |
|
1193 // Adjust congestion window in response to new ack's received |
|
1194 NS_LOG_FUNCTION (this << seq); |
|
1195 NS_LOG_LOGIC ("TcpSocketImpl " << this << " NewAck " |
|
1196 << " seq " << seq |
|
1197 << " cWnd " << m_cWnd |
|
1198 << " ssThresh " << m_ssThresh); |
|
1199 if (m_cWnd < m_ssThresh) |
|
1200 { // Slow start mode, add one segSize to cWnd |
|
1201 m_cWnd += m_segmentSize; |
|
1202 NS_LOG_LOGIC ("TcpSocketImpl " << this << " NewCWnd SlowStart, cWnd " << m_cWnd |
|
1203 << " sst " << m_ssThresh); |
|
1204 } |
|
1205 else |
|
1206 { // Congestion avoidance mode, adjust by (ackBytes*segSize) / cWnd |
|
1207 double adder = ((double) m_segmentSize * m_segmentSize) / m_cWnd.Get(); |
|
1208 if (adder < 1.0) |
|
1209 { |
|
1210 adder = 1.0; |
|
1211 } |
|
1212 m_cWnd += (uint32_t) adder; |
|
1213 NS_LOG_LOGIC ("NewCWnd CongAvoid, cWnd " << m_cWnd |
|
1214 << " sst " << m_ssThresh); |
|
1215 } |
|
1216 CommonNewAck (seq, false); // Complete newAck processing |
|
1217 } |
|
1218 |
|
1219 void TcpSocketImpl::DupAck (const TcpHeader& t, uint32_t count) |
|
1220 { |
|
1221 NS_LOG_FUNCTION (this << "t " << count); |
|
1222 NS_LOG_LOGIC ("TcpSocketImpl " << this << " DupAck " << t.GetAckNumber () |
|
1223 << ", count " << count |
|
1224 << ", time " << Simulator::Now ()); |
|
1225 if (count == 3) |
|
1226 { // Count of three indicates triple duplicate ack |
|
1227 m_ssThresh = Window () / 2; // Per RFC2581 |
|
1228 m_ssThresh = std::max (m_ssThresh, 2 * m_segmentSize); |
|
1229 NS_LOG_LOGIC("TcpSocketImpl " << this << "Tahoe TDA, time " << Simulator::Now () |
|
1230 << " seq " << t.GetAckNumber () |
|
1231 << " in flight " << BytesInFlight () |
|
1232 << " new ssthresh " << m_ssThresh); |
|
1233 |
|
1234 m_cWnd = m_segmentSize; // Collapse cwnd (re-enter slowstart) |
|
1235 // For Tahoe, we also reset nextTxSeq |
|
1236 m_nextTxSequence = m_highestRxAck; |
|
1237 SendPendingData (); |
|
1238 } |
|
1239 } |
|
1240 |
|
1241 void TcpSocketImpl::ReTxTimeout () |
|
1242 { // Retransmit timeout |
|
1243 NS_LOG_FUNCTION (this); |
|
1244 m_ssThresh = Window () / 2; // Per RFC2581 |
|
1245 m_ssThresh = std::max (m_ssThresh, 2 * m_segmentSize); |
|
1246 // Set cWnd to segSize on timeout, per rfc2581 |
|
1247 // Collapse congestion window (re-enter slowstart) |
|
1248 m_cWnd = m_segmentSize; |
|
1249 m_nextTxSequence = m_highestRxAck; // Start from highest Ack |
|
1250 m_rtt->IncreaseMultiplier (); // DoubleValue timeout value for next retx timer |
|
1251 Retransmit (); // Retransmit the packet |
|
1252 } |
|
1253 |
|
1254 void TcpSocketImpl::LastAckTimeout () |
|
1255 { |
|
1256 m_lastAckEvent.Cancel (); |
|
1257 if (m_state == LAST_ACK) |
|
1258 { |
|
1259 Actions_t action = ProcessEvent (TIMEOUT); |
|
1260 ProcessAction (action); |
|
1261 } |
|
1262 if (!m_closeNotified) |
|
1263 { |
|
1264 m_closeNotified = true; |
|
1265 } |
|
1266 } |
|
1267 |
|
1268 void TcpSocketImpl::Retransmit () |
|
1269 { |
|
1270 NS_LOG_FUNCTION (this); |
|
1271 uint8_t flags = TcpHeader::NONE; |
|
1272 if (m_state == SYN_SENT) |
|
1273 { |
|
1274 if (m_cnCount > 0) |
|
1275 { |
|
1276 SendEmptyPacket (TcpHeader::SYN); |
|
1277 return; |
|
1278 } |
|
1279 else |
|
1280 { |
|
1281 NotifyConnectionFailed (); |
|
1282 return; |
|
1283 } |
|
1284 } |
|
1285 if (!m_pendingData) |
|
1286 { |
|
1287 if (m_state == FIN_WAIT_1 || m_state == FIN_WAIT_2) |
|
1288 { // Must have lost FIN, re-send |
|
1289 SendEmptyPacket (TcpHeader::FIN); |
|
1290 } |
|
1291 return; |
|
1292 } |
|
1293 Ptr<Packet> p = m_pendingData->CopyFromSeq (m_segmentSize, |
|
1294 m_firstPendingSequence, |
|
1295 m_highestRxAck); |
|
1296 // Calculate remaining data for COE check |
|
1297 uint32_t remainingData = m_pendingData->SizeFromSeq ( |
|
1298 m_firstPendingSequence, |
|
1299 m_nextTxSequence + SequenceNumber(p->GetSize ())); |
|
1300 if (m_closeOnEmpty && remainingData == 0) |
|
1301 { // Add the FIN flag |
|
1302 flags = flags | TcpHeader::FIN; |
|
1303 } |
|
1304 |
|
1305 NS_LOG_LOGIC ("TcpSocketImpl " << this << " retxing seq " << m_highestRxAck); |
|
1306 if (m_retxEvent.IsExpired () ) |
|
1307 { |
|
1308 Time rto = m_rtt->RetransmitTimeout (); |
|
1309 NS_LOG_LOGIC ("Schedule retransmission timeout at time " |
|
1310 << Simulator::Now ().GetSeconds () << " to expire at time " |
|
1311 << (Simulator::Now () + rto).GetSeconds ()); |
|
1312 m_retxEvent = Simulator::Schedule (rto,&TcpSocketImpl::ReTxTimeout,this); |
|
1313 } |
|
1314 m_rtt->SentSeq (m_highestRxAck,p->GetSize ()); |
|
1315 // And send the packet |
|
1316 TcpHeader tcpHeader; |
|
1317 tcpHeader.SetSequenceNumber (m_nextTxSequence); |
|
1318 tcpHeader.SetAckNumber (m_nextRxSequence); |
|
1319 tcpHeader.SetSourcePort (m_endPoint->GetLocalPort()); |
|
1320 tcpHeader.SetDestinationPort (m_remotePort); |
|
1321 tcpHeader.SetFlags (flags); |
|
1322 tcpHeader.SetWindowSize (m_advertisedWindowSize); |
|
1323 |
|
1324 m_tcp->SendPacket (p, tcpHeader, m_endPoint->GetLocalAddress (), |
|
1325 m_remoteAddress); |
|
1326 } |
|
1327 |
|
1328 void |
|
1329 TcpSocketImpl::SetSndBufSize (uint32_t size) |
|
1330 { |
|
1331 m_sndBufSize = size; |
|
1332 } |
|
1333 |
|
1334 uint32_t |
|
1335 TcpSocketImpl::GetSndBufSize (void) const |
|
1336 { |
|
1337 return m_sndBufSize; |
|
1338 } |
|
1339 |
|
1340 void |
|
1341 TcpSocketImpl::SetRcvBufSize (uint32_t size) |
|
1342 { |
|
1343 m_rcvBufSize = size; |
|
1344 } |
|
1345 |
|
1346 uint32_t |
|
1347 TcpSocketImpl::GetRcvBufSize (void) const |
|
1348 { |
|
1349 return m_rcvBufSize; |
|
1350 } |
|
1351 |
|
1352 void |
|
1353 TcpSocketImpl::SetSegSize (uint32_t size) |
|
1354 { |
|
1355 m_segmentSize = size; |
|
1356 } |
|
1357 |
|
1358 uint32_t |
|
1359 TcpSocketImpl::GetSegSize (void) const |
|
1360 { |
|
1361 return m_segmentSize; |
|
1362 } |
|
1363 |
|
1364 void |
|
1365 TcpSocketImpl::SetAdvWin (uint32_t window) |
|
1366 { |
|
1367 m_advertisedWindowSize = window; |
|
1368 } |
|
1369 |
|
1370 uint32_t |
|
1371 TcpSocketImpl::GetAdvWin (void) const |
|
1372 { |
|
1373 return m_advertisedWindowSize; |
|
1374 } |
|
1375 |
|
1376 void |
|
1377 TcpSocketImpl::SetSSThresh (uint32_t threshold) |
|
1378 { |
|
1379 m_ssThresh = threshold; |
|
1380 } |
|
1381 |
|
1382 uint32_t |
|
1383 TcpSocketImpl::GetSSThresh (void) const |
|
1384 { |
|
1385 return m_ssThresh; |
|
1386 } |
|
1387 |
|
1388 void |
|
1389 TcpSocketImpl::SetInitialCwnd (uint32_t cwnd) |
|
1390 { |
|
1391 m_initialCWnd = cwnd; |
|
1392 } |
|
1393 |
|
1394 uint32_t |
|
1395 TcpSocketImpl::GetInitialCwnd (void) const |
|
1396 { |
|
1397 return m_initialCWnd; |
|
1398 } |
|
1399 |
|
1400 void |
|
1401 TcpSocketImpl::SetConnTimeout (Time timeout) |
|
1402 { |
|
1403 m_cnTimeout = timeout; |
|
1404 } |
|
1405 |
|
1406 Time |
|
1407 TcpSocketImpl::GetConnTimeout (void) const |
|
1408 { |
|
1409 return m_cnTimeout; |
|
1410 } |
|
1411 |
|
1412 void |
|
1413 TcpSocketImpl::SetConnCount (uint32_t count) |
|
1414 { |
|
1415 m_cnCount = count; |
|
1416 } |
|
1417 |
|
1418 uint32_t |
|
1419 TcpSocketImpl::GetConnCount (void) const |
|
1420 { |
|
1421 return m_cnCount; |
|
1422 } |
|
1423 |
|
1424 void |
|
1425 TcpSocketImpl::SetDelAckTimeout (Time timeout) |
|
1426 { |
|
1427 m_delAckTimeout = timeout; |
|
1428 } |
|
1429 |
|
1430 Time |
|
1431 TcpSocketImpl::GetDelAckTimeout (void) const |
|
1432 { |
|
1433 return m_delAckTimeout; |
|
1434 } |
|
1435 |
|
1436 void |
|
1437 TcpSocketImpl::SetDelAckMaxCount (uint32_t count) |
|
1438 { |
|
1439 m_delAckMaxCount = count; |
|
1440 } |
|
1441 |
|
1442 uint32_t |
|
1443 TcpSocketImpl::GetDelAckMaxCount (void) const |
|
1444 { |
|
1445 return m_delAckMaxCount; |
|
1446 } |
|
1447 |
|
1448 }//namespace ns3 |
|