37 #include "ns3/simulation-singleton.h" |
37 #include "ns3/simulation-singleton.h" |
38 #include "ns3/simulator.h" |
38 #include "ns3/simulator.h" |
39 #include "ns3/packet.h" |
39 #include "ns3/packet.h" |
40 #include "ns3/uinteger.h" |
40 #include "ns3/uinteger.h" |
41 #include "ns3/double.h" |
41 #include "ns3/double.h" |
|
42 #include "ns3/pointer.h" |
42 #include "ns3/trace-source-accessor.h" |
43 #include "ns3/trace-source-accessor.h" |
43 #include "tcp-socket-base.h" |
44 #include "tcp-socket-base.h" |
44 #include "tcp-l4-protocol.h" |
45 #include "tcp-l4-protocol.h" |
45 #include "ipv4-end-point.h" |
46 #include "ipv4-end-point.h" |
46 #include "ipv6-end-point.h" |
47 #include "ipv6-end-point.h" |
104 "Clock Granularity used in RTO calculations", |
105 "Clock Granularity used in RTO calculations", |
105 TimeValue (MilliSeconds (1)), // RFC6298 suggest to use fine clock granularity |
106 TimeValue (MilliSeconds (1)), // RFC6298 suggest to use fine clock granularity |
106 MakeTimeAccessor (&TcpSocketBase::SetClockGranularity, |
107 MakeTimeAccessor (&TcpSocketBase::SetClockGranularity, |
107 &TcpSocketBase::GetClockGranularity), |
108 &TcpSocketBase::GetClockGranularity), |
108 MakeTimeChecker ()) |
109 MakeTimeChecker ()) |
|
110 .AddAttribute ("TxBuffer", |
|
111 "TCP Tx buffer", |
|
112 PointerValue (), |
|
113 MakePointerAccessor (&TcpSocketBase::GetTxBuffer), |
|
114 MakePointerChecker<TcpTxBuffer> ()) |
|
115 .AddAttribute ("RxBuffer", |
|
116 "TCP Rx buffer", |
|
117 PointerValue (), |
|
118 MakePointerAccessor (&TcpSocketBase::GetRxBuffer), |
|
119 MakePointerChecker<TcpRxBuffer> ()) |
109 .AddTraceSource ("RTO", |
120 .AddTraceSource ("RTO", |
110 "Retransmission timeout", |
121 "Retransmission timeout", |
111 MakeTraceSourceAccessor (&TcpSocketBase::m_rto), |
122 MakeTraceSourceAccessor (&TcpSocketBase::m_rto), |
112 "ns3::Time::TracedValueCallback") |
123 "ns3::Time::TracedValueCallback") |
113 .AddTraceSource ("RTT", |
124 .AddTraceSource ("RTT", |
162 m_timestampEnabled (true), |
173 m_timestampEnabled (true), |
163 m_timestampToEcho (0) |
174 m_timestampToEcho (0) |
164 |
175 |
165 { |
176 { |
166 NS_LOG_FUNCTION (this); |
177 NS_LOG_FUNCTION (this); |
|
178 m_rxBuffer = CreateObject<TcpRxBuffer> (); |
|
179 m_txBuffer = CreateObject<TcpTxBuffer> (); |
167 } |
180 } |
168 |
181 |
169 TcpSocketBase::TcpSocketBase (const TcpSocketBase& sock) |
182 TcpSocketBase::TcpSocketBase (const TcpSocketBase& sock) |
170 : TcpSocket (sock), |
183 : TcpSocket (sock), |
171 //copy object::m_tid and socket::callbacks |
184 //copy object::m_tid and socket::callbacks |
182 m_node (sock.m_node), |
195 m_node (sock.m_node), |
183 m_tcp (sock.m_tcp), |
196 m_tcp (sock.m_tcp), |
184 m_rtt (0), |
197 m_rtt (0), |
185 m_nextTxSequence (sock.m_nextTxSequence), |
198 m_nextTxSequence (sock.m_nextTxSequence), |
186 m_highTxMark (sock.m_highTxMark), |
199 m_highTxMark (sock.m_highTxMark), |
187 m_rxBuffer (sock.m_rxBuffer), |
|
188 m_txBuffer (sock.m_txBuffer), |
|
189 m_state (sock.m_state), |
200 m_state (sock.m_state), |
190 m_errno (sock.m_errno), |
201 m_errno (sock.m_errno), |
191 m_closeNotified (sock.m_closeNotified), |
202 m_closeNotified (sock.m_closeNotified), |
192 m_closeOnEmpty (sock.m_closeOnEmpty), |
203 m_closeOnEmpty (sock.m_closeOnEmpty), |
193 m_shutdownSend (sock.m_shutdownSend), |
204 m_shutdownSend (sock.m_shutdownSend), |
217 Callback<void, Ptr<Socket>, uint32_t> vPSUI = MakeNullCallback<void, Ptr<Socket>, uint32_t> (); |
228 Callback<void, Ptr<Socket>, uint32_t> vPSUI = MakeNullCallback<void, Ptr<Socket>, uint32_t> (); |
218 SetConnectCallback (vPS, vPS); |
229 SetConnectCallback (vPS, vPS); |
219 SetDataSentCallback (vPSUI); |
230 SetDataSentCallback (vPSUI); |
220 SetSendCallback (vPSUI); |
231 SetSendCallback (vPSUI); |
221 SetRecvCallback (vPS); |
232 SetRecvCallback (vPS); |
|
233 m_txBuffer = CopyObject (sock.m_txBuffer); |
|
234 m_rxBuffer = CopyObject (sock.m_rxBuffer); |
222 } |
235 } |
223 |
236 |
224 TcpSocketBase::~TcpSocketBase (void) |
237 TcpSocketBase::~TcpSocketBase (void) |
225 { |
238 { |
226 NS_LOG_FUNCTION (this); |
239 NS_LOG_FUNCTION (this); |
499 { |
512 { |
500 NS_LOG_FUNCTION (this); |
513 NS_LOG_FUNCTION (this); |
501 /// \internal |
514 /// \internal |
502 /// First we check to see if there is any unread rx data. |
515 /// First we check to see if there is any unread rx data. |
503 /// \bugid{426} claims we should send reset in this case. |
516 /// \bugid{426} claims we should send reset in this case. |
504 if (m_rxBuffer.Size () != 0) |
517 if (m_rxBuffer->Size () != 0) |
505 { |
518 { |
506 NS_LOG_INFO ("Socket " << this << " << unread rx data during close. Sending reset"); |
519 NS_LOG_INFO ("Socket " << this << " << unread rx data during close. Sending reset"); |
507 SendRST (); |
520 SendRST (); |
508 return 0; |
521 return 0; |
509 } |
522 } |
510 |
523 |
511 if (m_txBuffer.SizeFromSequence (m_nextTxSequence) > 0) |
524 if (m_txBuffer->SizeFromSequence (m_nextTxSequence) > 0) |
512 { // App close with pending data must wait until all data transmitted |
525 { // App close with pending data must wait until all data transmitted |
513 if (m_closeOnEmpty == false) |
526 if (m_closeOnEmpty == false) |
514 { |
527 { |
515 m_closeOnEmpty = true; |
528 m_closeOnEmpty = true; |
516 NS_LOG_INFO ("Socket " << this << " deferring close, state " << TcpStateName[m_state]); |
529 NS_LOG_INFO ("Socket " << this << " deferring close, state " << TcpStateName[m_state]); |
529 //this prevents data from being added to the buffer |
542 //this prevents data from being added to the buffer |
530 m_shutdownSend = true; |
543 m_shutdownSend = true; |
531 m_closeOnEmpty = true; |
544 m_closeOnEmpty = true; |
532 //if buffer is already empty, send a fin now |
545 //if buffer is already empty, send a fin now |
533 //otherwise fin will go when buffer empties. |
546 //otherwise fin will go when buffer empties. |
534 if (m_txBuffer.Size () == 0) |
547 if (m_txBuffer->Size () == 0) |
535 { |
548 { |
536 if (m_state == ESTABLISHED || m_state == CLOSE_WAIT) |
549 if (m_state == ESTABLISHED || m_state == CLOSE_WAIT) |
537 { |
550 { |
538 NS_LOG_INFO("Emtpy tx buffer, send fin"); |
551 NS_LOG_INFO("Emtpy tx buffer, send fin"); |
539 SendEmptyPacket (TcpHeader::FIN); |
552 SendEmptyPacket (TcpHeader::FIN); |
571 NS_LOG_FUNCTION (this << p); |
584 NS_LOG_FUNCTION (this << p); |
572 NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Send()"); |
585 NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Send()"); |
573 if (m_state == ESTABLISHED || m_state == SYN_SENT || m_state == CLOSE_WAIT) |
586 if (m_state == ESTABLISHED || m_state == SYN_SENT || m_state == CLOSE_WAIT) |
574 { |
587 { |
575 // Store the packet into Tx buffer |
588 // Store the packet into Tx buffer |
576 if (!m_txBuffer.Add (p)) |
589 if (!m_txBuffer->Add (p)) |
577 { // TxBuffer overflow, send failed |
590 { // TxBuffer overflow, send failed |
578 m_errno = ERROR_MSGSIZE; |
591 m_errno = ERROR_MSGSIZE; |
579 return -1; |
592 return -1; |
580 } |
593 } |
581 if (m_shutdownSend) |
594 if (m_shutdownSend) |
582 { |
595 { |
583 m_errno = ERROR_SHUTDOWN; |
596 m_errno = ERROR_SHUTDOWN; |
584 return -1; |
597 return -1; |
585 } |
598 } |
586 // Submit the data to lower layers |
599 // Submit the data to lower layers |
587 NS_LOG_LOGIC ("txBufSize=" << m_txBuffer.Size () << " state " << TcpStateName[m_state]); |
600 NS_LOG_LOGIC ("txBufSize=" << m_txBuffer->Size () << " state " << TcpStateName[m_state]); |
588 if (m_state == ESTABLISHED || m_state == CLOSE_WAIT) |
601 if (m_state == ESTABLISHED || m_state == CLOSE_WAIT) |
589 { // Try to send the data out |
602 { // Try to send the data out |
590 SendPendingData (m_connected); |
603 SendPendingData (m_connected); |
591 } |
604 } |
592 return p->GetSize (); |
605 return p->GetSize (); |
610 Ptr<Packet> |
623 Ptr<Packet> |
611 TcpSocketBase::Recv (uint32_t maxSize, uint32_t flags) |
624 TcpSocketBase::Recv (uint32_t maxSize, uint32_t flags) |
612 { |
625 { |
613 NS_LOG_FUNCTION (this); |
626 NS_LOG_FUNCTION (this); |
614 NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Recv()"); |
627 NS_ABORT_MSG_IF (flags, "use of flags is not supported in TcpSocketBase::Recv()"); |
615 if (m_rxBuffer.Size () == 0 && m_state == CLOSE_WAIT) |
628 if (m_rxBuffer->Size () == 0 && m_state == CLOSE_WAIT) |
616 { |
629 { |
617 return Create<Packet> (); // Send EOF on connection close |
630 return Create<Packet> (); // Send EOF on connection close |
618 } |
631 } |
619 Ptr<Packet> outPacket = m_rxBuffer.Extract (maxSize); |
632 Ptr<Packet> outPacket = m_rxBuffer->Extract (maxSize); |
620 if (outPacket != 0 && outPacket->GetSize () != 0) |
633 if (outPacket != 0 && outPacket->GetSize () != 0) |
621 { |
634 { |
622 SocketAddressTag tag; |
635 SocketAddressTag tag; |
623 if (m_endPoint != 0) |
636 if (m_endPoint != 0) |
624 { |
637 { |
661 /* Inherit from Socket class: Get the max number of bytes an app can send */ |
674 /* Inherit from Socket class: Get the max number of bytes an app can send */ |
662 uint32_t |
675 uint32_t |
663 TcpSocketBase::GetTxAvailable (void) const |
676 TcpSocketBase::GetTxAvailable (void) const |
664 { |
677 { |
665 NS_LOG_FUNCTION (this); |
678 NS_LOG_FUNCTION (this); |
666 return m_txBuffer.Available (); |
679 return m_txBuffer->Available (); |
667 } |
680 } |
668 |
681 |
669 /* Inherit from Socket class: Get the max number of bytes an app can read */ |
682 /* Inherit from Socket class: Get the max number of bytes an app can read */ |
670 uint32_t |
683 uint32_t |
671 TcpSocketBase::GetRxAvailable (void) const |
684 TcpSocketBase::GetRxAvailable (void) const |
672 { |
685 { |
673 NS_LOG_FUNCTION (this); |
686 NS_LOG_FUNCTION (this); |
674 return m_rxBuffer.Available (); |
687 return m_rxBuffer->Available (); |
675 } |
688 } |
676 |
689 |
677 /* Inherit from Socket class: Return local address:port */ |
690 /* Inherit from Socket class: Return local address:port */ |
678 int |
691 int |
679 TcpSocketBase::GetSockName (Address &address) const |
692 TcpSocketBase::GetSockName (Address &address) const |
845 { // Rx buffer in these states are not initialized. |
858 { // Rx buffer in these states are not initialized. |
846 return false; |
859 return false; |
847 } |
860 } |
848 if (m_state == LAST_ACK || m_state == CLOSING || m_state == CLOSE_WAIT) |
861 if (m_state == LAST_ACK || m_state == CLOSING || m_state == CLOSE_WAIT) |
849 { // In LAST_ACK and CLOSING states, it only wait for an ACK and the |
862 { // In LAST_ACK and CLOSING states, it only wait for an ACK and the |
850 // sequence number must equals to m_rxBuffer.NextRxSequence () |
863 // sequence number must equals to m_rxBuffer->NextRxSequence () |
851 return (m_rxBuffer.NextRxSequence () != head); |
864 return (m_rxBuffer->NextRxSequence () != head); |
852 } |
865 } |
853 |
866 |
854 // In all other cases, check if the sequence number is in range |
867 // In all other cases, check if the sequence number is in range |
855 return (tail < m_rxBuffer.NextRxSequence () || m_rxBuffer.MaxRxSequence () <= head); |
868 return (tail < m_rxBuffer->NextRxSequence () || m_rxBuffer->MaxRxSequence () <= head); |
856 } |
869 } |
857 |
870 |
858 /* Function called by the L3 protocol when it received a packet to pass on to |
871 /* Function called by the L3 protocol when it received a packet to pass on to |
859 the TCP. This function is registered as the "RxCallback" function in |
872 the TCP. This function is registered as the "RxCallback" function in |
860 SetupCallback(), which invoked by Bind(), and CompleteFork() */ |
873 SetupCallback(), which invoked by Bind(), and CompleteFork() */ |
942 && OutOfRange (tcpHeader.GetSequenceNumber (), tcpHeader.GetSequenceNumber () + packet->GetSize ())) |
955 && OutOfRange (tcpHeader.GetSequenceNumber (), tcpHeader.GetSequenceNumber () + packet->GetSize ())) |
943 { |
956 { |
944 NS_LOG_LOGIC ("At state " << TcpStateName[m_state] << |
957 NS_LOG_LOGIC ("At state " << TcpStateName[m_state] << |
945 " received packet of seq [" << tcpHeader.GetSequenceNumber () << |
958 " received packet of seq [" << tcpHeader.GetSequenceNumber () << |
946 ":" << tcpHeader.GetSequenceNumber () + packet->GetSize () << |
959 ":" << tcpHeader.GetSequenceNumber () + packet->GetSize () << |
947 ") out of range [" << m_rxBuffer.NextRxSequence () << ":" << |
960 ") out of range [" << m_rxBuffer->NextRxSequence () << ":" << |
948 m_rxBuffer.MaxRxSequence () << ")"); |
961 m_rxBuffer->MaxRxSequence () << ")"); |
949 // Acknowledgement should be sent for all unacceptable packets (RFC793, p.69) |
962 // Acknowledgement should be sent for all unacceptable packets (RFC793, p.69) |
950 if (m_state == ESTABLISHED && !(tcpHeader.GetFlags () & TcpHeader::RST)) |
963 if (m_state == ESTABLISHED && !(tcpHeader.GetFlags () & TcpHeader::RST)) |
951 { |
964 { |
952 SendEmptyPacket (TcpHeader::ACK); |
965 SendEmptyPacket (TcpHeader::ACK); |
953 } |
966 } |
972 if ((tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG)) != TcpHeader::RST) |
985 if ((tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG)) != TcpHeader::RST) |
973 { // Since m_endPoint is not configured yet, we cannot use SendRST here |
986 { // Since m_endPoint is not configured yet, we cannot use SendRST here |
974 TcpHeader h; |
987 TcpHeader h; |
975 h.SetFlags (TcpHeader::RST); |
988 h.SetFlags (TcpHeader::RST); |
976 h.SetSequenceNumber (m_nextTxSequence); |
989 h.SetSequenceNumber (m_nextTxSequence); |
977 h.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
990 h.SetAckNumber (m_rxBuffer->NextRxSequence ()); |
978 h.SetSourcePort (tcpHeader.GetDestinationPort ()); |
991 h.SetSourcePort (tcpHeader.GetDestinationPort ()); |
979 h.SetDestinationPort (tcpHeader.GetSourcePort ()); |
992 h.SetDestinationPort (tcpHeader.GetSourcePort ()); |
980 h.SetWindowSize (AdvertisedWindowSize ()); |
993 h.SetWindowSize (AdvertisedWindowSize ()); |
981 AddOptions (h); |
994 AddOptions (h); |
982 m_tcp->SendPacket (Create<Packet> (), h, header.GetDestination (), header.GetSource (), m_boundnetdevice); |
995 m_tcp->SendPacket (Create<Packet> (), h, header.GetDestination (), header.GetSource (), m_boundnetdevice); |
1046 && OutOfRange (tcpHeader.GetSequenceNumber (), tcpHeader.GetSequenceNumber () + packet->GetSize ())) |
1059 && OutOfRange (tcpHeader.GetSequenceNumber (), tcpHeader.GetSequenceNumber () + packet->GetSize ())) |
1047 { |
1060 { |
1048 NS_LOG_LOGIC ("At state " << TcpStateName[m_state] << |
1061 NS_LOG_LOGIC ("At state " << TcpStateName[m_state] << |
1049 " received packet of seq [" << tcpHeader.GetSequenceNumber () << |
1062 " received packet of seq [" << tcpHeader.GetSequenceNumber () << |
1050 ":" << tcpHeader.GetSequenceNumber () + packet->GetSize () << |
1063 ":" << tcpHeader.GetSequenceNumber () + packet->GetSize () << |
1051 ") out of range [" << m_rxBuffer.NextRxSequence () << ":" << |
1064 ") out of range [" << m_rxBuffer->NextRxSequence () << ":" << |
1052 m_rxBuffer.MaxRxSequence () << ")"); |
1065 m_rxBuffer->MaxRxSequence () << ")"); |
1053 // Acknowledgement should be sent for all unacceptable packets (RFC793, p.69) |
1066 // Acknowledgement should be sent for all unacceptable packets (RFC793, p.69) |
1054 if (m_state == ESTABLISHED && !(tcpHeader.GetFlags () & TcpHeader::RST)) |
1067 if (m_state == ESTABLISHED && !(tcpHeader.GetFlags () & TcpHeader::RST)) |
1055 { |
1068 { |
1056 SendEmptyPacket (TcpHeader::ACK); |
1069 SendEmptyPacket (TcpHeader::ACK); |
1057 } |
1070 } |
1076 if ((tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG)) != TcpHeader::RST) |
1089 if ((tcpHeader.GetFlags () & ~(TcpHeader::PSH | TcpHeader::URG)) != TcpHeader::RST) |
1077 { // Since m_endPoint is not configured yet, we cannot use SendRST here |
1090 { // Since m_endPoint is not configured yet, we cannot use SendRST here |
1078 TcpHeader h; |
1091 TcpHeader h; |
1079 h.SetFlags (TcpHeader::RST); |
1092 h.SetFlags (TcpHeader::RST); |
1080 h.SetSequenceNumber (m_nextTxSequence); |
1093 h.SetSequenceNumber (m_nextTxSequence); |
1081 h.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
1094 h.SetAckNumber (m_rxBuffer->NextRxSequence ()); |
1082 h.SetSourcePort (tcpHeader.GetDestinationPort ()); |
1095 h.SetSourcePort (tcpHeader.GetDestinationPort ()); |
1083 h.SetDestinationPort (tcpHeader.GetSourcePort ()); |
1096 h.SetDestinationPort (tcpHeader.GetSourcePort ()); |
1084 h.SetWindowSize (AdvertisedWindowSize ()); |
1097 h.SetWindowSize (AdvertisedWindowSize ()); |
1085 AddOptions (h); |
1098 AddOptions (h); |
1086 m_tcp->SendPacket (Create<Packet> (), h, header.GetDestinationAddress (), header.GetSourceAddress (), m_boundnetdevice); |
1099 m_tcp->SendPacket (Create<Packet> (), h, header.GetDestinationAddress (), header.GetSourceAddress (), m_boundnetdevice); |
1136 PeerClose (packet, tcpHeader); |
1149 PeerClose (packet, tcpHeader); |
1137 } |
1150 } |
1138 else if (tcpflags == 0) |
1151 else if (tcpflags == 0) |
1139 { // No flags means there is only data |
1152 { // No flags means there is only data |
1140 ReceivedData (packet, tcpHeader); |
1153 ReceivedData (packet, tcpHeader); |
1141 if (m_rxBuffer.Finished ()) |
1154 if (m_rxBuffer->Finished ()) |
1142 { |
1155 { |
1143 PeerClose (packet, tcpHeader); |
1156 PeerClose (packet, tcpHeader); |
1144 } |
1157 } |
1145 } |
1158 } |
1146 else |
1159 else |
1162 |
1175 |
1163 // Received ACK. Compare the ACK number against highest unacked seqno |
1176 // Received ACK. Compare the ACK number against highest unacked seqno |
1164 if (0 == (tcpHeader.GetFlags () & TcpHeader::ACK)) |
1177 if (0 == (tcpHeader.GetFlags () & TcpHeader::ACK)) |
1165 { // Ignore if no ACK flag |
1178 { // Ignore if no ACK flag |
1166 } |
1179 } |
1167 else if (tcpHeader.GetAckNumber () < m_txBuffer.HeadSequence ()) |
1180 else if (tcpHeader.GetAckNumber () < m_txBuffer->HeadSequence ()) |
1168 { // Case 1: Old ACK, ignored. |
1181 { // Case 1: Old ACK, ignored. |
1169 NS_LOG_LOGIC ("Ignored ack of " << tcpHeader.GetAckNumber ()); |
1182 NS_LOG_LOGIC ("Ignored ack of " << tcpHeader.GetAckNumber ()); |
1170 } |
1183 } |
1171 else if (tcpHeader.GetAckNumber () == m_txBuffer.HeadSequence ()) |
1184 else if (tcpHeader.GetAckNumber () == m_txBuffer->HeadSequence ()) |
1172 { // Case 2: Potentially a duplicated ACK |
1185 { // Case 2: Potentially a duplicated ACK |
1173 if (tcpHeader.GetAckNumber () < m_nextTxSequence && packet->GetSize() == 0) |
1186 if (tcpHeader.GetAckNumber () < m_nextTxSequence && packet->GetSize() == 0) |
1174 { |
1187 { |
1175 NS_LOG_LOGIC ("Dupack of " << tcpHeader.GetAckNumber ()); |
1188 NS_LOG_LOGIC ("Dupack of " << tcpHeader.GetAckNumber ()); |
1176 DupAck (tcpHeader, ++m_dupAckCount); |
1189 DupAck (tcpHeader, ++m_dupAckCount); |
1177 } |
1190 } |
1178 // otherwise, the ACK is precisely equal to the nextTxSequence |
1191 // otherwise, the ACK is precisely equal to the nextTxSequence |
1179 NS_ASSERT (tcpHeader.GetAckNumber () <= m_nextTxSequence); |
1192 NS_ASSERT (tcpHeader.GetAckNumber () <= m_nextTxSequence); |
1180 } |
1193 } |
1181 else if (tcpHeader.GetAckNumber () > m_txBuffer.HeadSequence ()) |
1194 else if (tcpHeader.GetAckNumber () > m_txBuffer->HeadSequence ()) |
1182 { // Case 3: New ACK, reset m_dupAckCount and update m_txBuffer |
1195 { // Case 3: New ACK, reset m_dupAckCount and update m_txBuffer |
1183 NS_LOG_LOGIC ("New ack of " << tcpHeader.GetAckNumber ()); |
1196 NS_LOG_LOGIC ("New ack of " << tcpHeader.GetAckNumber ()); |
1184 NewAck (tcpHeader.GetAckNumber ()); |
1197 NewAck (tcpHeader.GetAckNumber ()); |
1185 m_dupAckCount = 0; |
1198 m_dupAckCount = 0; |
1186 } |
1199 } |
1246 else if (tcpflags == TcpHeader::SYN) |
1259 else if (tcpflags == TcpHeader::SYN) |
1247 { // Received SYN, move to SYN_RCVD state and respond with SYN+ACK |
1260 { // Received SYN, move to SYN_RCVD state and respond with SYN+ACK |
1248 NS_LOG_INFO ("SYN_SENT -> SYN_RCVD"); |
1261 NS_LOG_INFO ("SYN_SENT -> SYN_RCVD"); |
1249 m_state = SYN_RCVD; |
1262 m_state = SYN_RCVD; |
1250 m_cnCount = m_cnRetries; |
1263 m_cnCount = m_cnRetries; |
1251 m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1264 m_rxBuffer->SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1252 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
1265 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
1253 } |
1266 } |
1254 else if (tcpflags == (TcpHeader::SYN | TcpHeader::ACK) |
1267 else if (tcpflags == (TcpHeader::SYN | TcpHeader::ACK) |
1255 && m_nextTxSequence + SequenceNumber32 (1) == tcpHeader.GetAckNumber ()) |
1268 && m_nextTxSequence + SequenceNumber32 (1) == tcpHeader.GetAckNumber ()) |
1256 { // Handshake completed |
1269 { // Handshake completed |
1257 NS_LOG_INFO ("SYN_SENT -> ESTABLISHED"); |
1270 NS_LOG_INFO ("SYN_SENT -> ESTABLISHED"); |
1258 m_state = ESTABLISHED; |
1271 m_state = ESTABLISHED; |
1259 m_connected = true; |
1272 m_connected = true; |
1260 m_retxEvent.Cancel (); |
1273 m_retxEvent.Cancel (); |
1261 m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1274 m_rxBuffer->SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1262 m_highTxMark = ++m_nextTxSequence; |
1275 m_highTxMark = ++m_nextTxSequence; |
1263 m_txBuffer.SetHeadSequence (m_nextTxSequence); |
1276 m_txBuffer->SetHeadSequence (m_nextTxSequence); |
1264 SendEmptyPacket (TcpHeader::ACK); |
1277 SendEmptyPacket (TcpHeader::ACK); |
1265 SendPendingData (m_connected); |
1278 SendPendingData (m_connected); |
1266 Simulator::ScheduleNow (&TcpSocketBase::ConnectionSucceeded, this); |
1279 Simulator::ScheduleNow (&TcpSocketBase::ConnectionSucceeded, this); |
1267 // Always respond to first data packet to speed up the connection. |
1280 // Always respond to first data packet to speed up the connection. |
1268 // Remove to get the behaviour of old NS-3 code. |
1281 // Remove to get the behaviour of old NS-3 code. |
1298 NS_LOG_INFO ("SYN_RCVD -> ESTABLISHED"); |
1311 NS_LOG_INFO ("SYN_RCVD -> ESTABLISHED"); |
1299 m_state = ESTABLISHED; |
1312 m_state = ESTABLISHED; |
1300 m_connected = true; |
1313 m_connected = true; |
1301 m_retxEvent.Cancel (); |
1314 m_retxEvent.Cancel (); |
1302 m_highTxMark = ++m_nextTxSequence; |
1315 m_highTxMark = ++m_nextTxSequence; |
1303 m_txBuffer.SetHeadSequence (m_nextTxSequence); |
1316 m_txBuffer->SetHeadSequence (m_nextTxSequence); |
1304 if (m_endPoint) |
1317 if (m_endPoint) |
1305 { |
1318 { |
1306 m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
1319 m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
1307 InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
1320 InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
1308 } |
1321 } |
1322 NotifySend (GetTxAvailable ()); |
1335 NotifySend (GetTxAvailable ()); |
1323 } |
1336 } |
1324 } |
1337 } |
1325 else if (tcpflags == TcpHeader::SYN) |
1338 else if (tcpflags == TcpHeader::SYN) |
1326 { // Probably the peer lost my SYN+ACK |
1339 { // Probably the peer lost my SYN+ACK |
1327 m_rxBuffer.SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1340 m_rxBuffer->SetNextRxSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (1)); |
1328 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
1341 SendEmptyPacket (TcpHeader::SYN | TcpHeader::ACK); |
1329 } |
1342 } |
1330 else if (tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
1343 else if (tcpflags == (TcpHeader::FIN | TcpHeader::ACK)) |
1331 { |
1344 { |
1332 if (tcpHeader.GetSequenceNumber () == m_rxBuffer.NextRxSequence ()) |
1345 if (tcpHeader.GetSequenceNumber () == m_rxBuffer->NextRxSequence ()) |
1333 { // In-sequence FIN before connection complete. Set up connection and close. |
1346 { // In-sequence FIN before connection complete. Set up connection and close. |
1334 m_connected = true; |
1347 m_connected = true; |
1335 m_retxEvent.Cancel (); |
1348 m_retxEvent.Cancel (); |
1336 m_highTxMark = ++m_nextTxSequence; |
1349 m_highTxMark = ++m_nextTxSequence; |
1337 m_txBuffer.SetHeadSequence (m_nextTxSequence); |
1350 m_txBuffer->SetHeadSequence (m_nextTxSequence); |
1338 if (m_endPoint) |
1351 if (m_endPoint) |
1339 { |
1352 { |
1340 m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
1353 m_endPoint->SetPeer (InetSocketAddress::ConvertFrom (fromAddress).GetIpv4 (), |
1341 InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
1354 InetSocketAddress::ConvertFrom (fromAddress).GetPort ()); |
1342 } |
1355 } |
1383 ReceivedData (packet, tcpHeader); |
1396 ReceivedData (packet, tcpHeader); |
1384 } |
1397 } |
1385 else if (tcpflags == TcpHeader::ACK) |
1398 else if (tcpflags == TcpHeader::ACK) |
1386 { // Process the ACK, and if in FIN_WAIT_1, conditionally move to FIN_WAIT_2 |
1399 { // Process the ACK, and if in FIN_WAIT_1, conditionally move to FIN_WAIT_2 |
1387 ReceivedAck (packet, tcpHeader); |
1400 ReceivedAck (packet, tcpHeader); |
1388 if (m_state == FIN_WAIT_1 && m_txBuffer.Size () == 0 |
1401 if (m_state == FIN_WAIT_1 && m_txBuffer->Size () == 0 |
1389 && tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
1402 && tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
1390 { // This ACK corresponds to the FIN sent |
1403 { // This ACK corresponds to the FIN sent |
1391 NS_LOG_INFO ("FIN_WAIT_1 -> FIN_WAIT_2"); |
1404 NS_LOG_INFO ("FIN_WAIT_1 -> FIN_WAIT_2"); |
1392 m_state = FIN_WAIT_2; |
1405 m_state = FIN_WAIT_2; |
1393 } |
1406 } |
1396 { // Got FIN, respond with ACK and move to next state |
1409 { // Got FIN, respond with ACK and move to next state |
1397 if (tcpflags & TcpHeader::ACK) |
1410 if (tcpflags & TcpHeader::ACK) |
1398 { // Process the ACK first |
1411 { // Process the ACK first |
1399 ReceivedAck (packet, tcpHeader); |
1412 ReceivedAck (packet, tcpHeader); |
1400 } |
1413 } |
1401 m_rxBuffer.SetFinSequence (tcpHeader.GetSequenceNumber ()); |
1414 m_rxBuffer->SetFinSequence (tcpHeader.GetSequenceNumber ()); |
1402 } |
1415 } |
1403 else if (tcpflags == TcpHeader::SYN || tcpflags == (TcpHeader::SYN | TcpHeader::ACK)) |
1416 else if (tcpflags == TcpHeader::SYN || tcpflags == (TcpHeader::SYN | TcpHeader::ACK)) |
1404 { // Duplicated SYN or SYN+ACK, possibly due to spurious retransmission |
1417 { // Duplicated SYN or SYN+ACK, possibly due to spurious retransmission |
1405 return; |
1418 return; |
1406 } |
1419 } |
1414 CloseAndNotify (); |
1427 CloseAndNotify (); |
1415 return; |
1428 return; |
1416 } |
1429 } |
1417 |
1430 |
1418 // Check if the close responder sent an in-sequence FIN, if so, respond ACK |
1431 // Check if the close responder sent an in-sequence FIN, if so, respond ACK |
1419 if ((m_state == FIN_WAIT_1 || m_state == FIN_WAIT_2) && m_rxBuffer.Finished ()) |
1432 if ((m_state == FIN_WAIT_1 || m_state == FIN_WAIT_2) && m_rxBuffer->Finished ()) |
1420 { |
1433 { |
1421 if (m_state == FIN_WAIT_1) |
1434 if (m_state == FIN_WAIT_1) |
1422 { |
1435 { |
1423 NS_LOG_INFO ("FIN_WAIT_1 -> CLOSING"); |
1436 NS_LOG_INFO ("FIN_WAIT_1 -> CLOSING"); |
1424 m_state = CLOSING; |
1437 m_state = CLOSING; |
1425 if (m_txBuffer.Size () == 0 |
1438 if (m_txBuffer->Size () == 0 |
1426 && tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
1439 && tcpHeader.GetAckNumber () == m_highTxMark + SequenceNumber32 (1)) |
1427 { // This ACK corresponds to the FIN sent |
1440 { // This ACK corresponds to the FIN sent |
1428 TimeWait (); |
1441 TimeWait (); |
1429 } |
1442 } |
1430 } |
1443 } |
1485 { |
1498 { |
1486 ReceivedData (packet, tcpHeader); |
1499 ReceivedData (packet, tcpHeader); |
1487 } |
1500 } |
1488 else if (tcpflags == TcpHeader::ACK) |
1501 else if (tcpflags == TcpHeader::ACK) |
1489 { |
1502 { |
1490 if (tcpHeader.GetSequenceNumber () == m_rxBuffer.NextRxSequence ()) |
1503 if (tcpHeader.GetSequenceNumber () == m_rxBuffer->NextRxSequence ()) |
1491 { // This ACK corresponds to the FIN sent. This socket closed peacefully. |
1504 { // This ACK corresponds to the FIN sent. This socket closed peacefully. |
1492 CloseAndNotify (); |
1505 CloseAndNotify (); |
1493 } |
1506 } |
1494 } |
1507 } |
1495 else if (tcpflags == TcpHeader::FIN) |
1508 else if (tcpflags == TcpHeader::FIN) |
1513 TcpSocketBase::PeerClose (Ptr<Packet> p, const TcpHeader& tcpHeader) |
1526 TcpSocketBase::PeerClose (Ptr<Packet> p, const TcpHeader& tcpHeader) |
1514 { |
1527 { |
1515 NS_LOG_FUNCTION (this << tcpHeader); |
1528 NS_LOG_FUNCTION (this << tcpHeader); |
1516 |
1529 |
1517 // Ignore all out of range packets |
1530 // Ignore all out of range packets |
1518 if (tcpHeader.GetSequenceNumber () < m_rxBuffer.NextRxSequence () |
1531 if (tcpHeader.GetSequenceNumber () < m_rxBuffer->NextRxSequence () |
1519 || tcpHeader.GetSequenceNumber () > m_rxBuffer.MaxRxSequence ()) |
1532 || tcpHeader.GetSequenceNumber () > m_rxBuffer->MaxRxSequence ()) |
1520 { |
1533 { |
1521 return; |
1534 return; |
1522 } |
1535 } |
1523 // For any case, remember the FIN position in rx buffer first |
1536 // For any case, remember the FIN position in rx buffer first |
1524 m_rxBuffer.SetFinSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
1537 m_rxBuffer->SetFinSequence (tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
1525 NS_LOG_LOGIC ("Accepted FIN at seq " << tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
1538 NS_LOG_LOGIC ("Accepted FIN at seq " << tcpHeader.GetSequenceNumber () + SequenceNumber32 (p->GetSize ())); |
1526 // If there is any piggybacked data, process it |
1539 // If there is any piggybacked data, process it |
1527 if (p->GetSize ()) |
1540 if (p->GetSize ()) |
1528 { |
1541 { |
1529 ReceivedData (p, tcpHeader); |
1542 ReceivedData (p, tcpHeader); |
1530 } |
1543 } |
1531 // Return if FIN is out of sequence, otherwise move to CLOSE_WAIT state by DoPeerClose |
1544 // Return if FIN is out of sequence, otherwise move to CLOSE_WAIT state by DoPeerClose |
1532 if (!m_rxBuffer.Finished ()) |
1545 if (!m_rxBuffer->Finished ()) |
1533 { |
1546 { |
1534 return; |
1547 return; |
1535 } |
1548 } |
1536 |
1549 |
1537 // Simultaneous close: Application invoked Close() when we are processing this FIN packet |
1550 // Simultaneous close: Application invoked Close() when we are processing this FIN packet |
1682 ++s; |
1695 ++s; |
1683 } |
1696 } |
1684 |
1697 |
1685 header.SetFlags (flags); |
1698 header.SetFlags (flags); |
1686 header.SetSequenceNumber (s); |
1699 header.SetSequenceNumber (s); |
1687 header.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
1700 header.SetAckNumber (m_rxBuffer->NextRxSequence ()); |
1688 if (m_endPoint != 0) |
1701 if (m_endPoint != 0) |
1689 { |
1702 { |
1690 header.SetSourcePort (m_endPoint->GetLocalPort ()); |
1703 header.SetSourcePort (m_endPoint->GetLocalPort ()); |
1691 header.SetDestinationPort (m_endPoint->GetPeerPort ()); |
1704 header.SetDestinationPort (m_endPoint->GetPeerPort ()); |
1692 } |
1705 } |
1904 TcpSocketBase::SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck) |
1917 TcpSocketBase::SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck) |
1905 { |
1918 { |
1906 NS_LOG_FUNCTION (this << seq << maxSize << withAck); |
1919 NS_LOG_FUNCTION (this << seq << maxSize << withAck); |
1907 |
1920 |
1908 bool isRetransmission = false; |
1921 bool isRetransmission = false; |
1909 if ( seq == m_txBuffer.HeadSequence () ) |
1922 if ( seq == m_txBuffer->HeadSequence () ) |
1910 { |
1923 { |
1911 isRetransmission = true; |
1924 isRetransmission = true; |
1912 } |
1925 } |
1913 |
1926 |
1914 Ptr<Packet> p = m_txBuffer.CopyFromSequence (maxSize, seq); |
1927 Ptr<Packet> p = m_txBuffer->CopyFromSequence (maxSize, seq); |
1915 uint32_t sz = p->GetSize (); // Size of packet |
1928 uint32_t sz = p->GetSize (); // Size of packet |
1916 uint8_t flags = withAck ? TcpHeader::ACK : 0; |
1929 uint8_t flags = withAck ? TcpHeader::ACK : 0; |
1917 uint32_t remainingData = m_txBuffer.SizeFromSequence (seq + SequenceNumber32 (sz)); |
1930 uint32_t remainingData = m_txBuffer->SizeFromSequence (seq + SequenceNumber32 (sz)); |
1918 |
1931 |
1919 /* |
1932 /* |
1920 * Add tags for each socket option. |
1933 * Add tags for each socket option. |
1921 * Note that currently the socket adds both IPv4 tag and IPv6 tag |
1934 * Note that currently the socket adds both IPv4 tag and IPv6 tag |
1922 * if both options are set. Once the packet got to layer three, only |
1935 * if both options are set. Once the packet got to layer three, only |
2039 */ |
2052 */ |
2040 bool |
2053 bool |
2041 TcpSocketBase::SendPendingData (bool withAck) |
2054 TcpSocketBase::SendPendingData (bool withAck) |
2042 { |
2055 { |
2043 NS_LOG_FUNCTION (this << withAck); |
2056 NS_LOG_FUNCTION (this << withAck); |
2044 if (m_txBuffer.Size () == 0) |
2057 if (m_txBuffer->Size () == 0) |
2045 { |
2058 { |
2046 return false; // Nothing to send |
2059 return false; // Nothing to send |
2047 |
2060 |
2048 } |
2061 } |
2049 if (m_endPoint == 0 && m_endPoint6 == 0) |
2062 if (m_endPoint == 0 && m_endPoint6 == 0) |
2050 { |
2063 { |
2051 NS_LOG_INFO ("TcpSocketBase::SendPendingData: No endpoint; m_shutdownSend=" << m_shutdownSend); |
2064 NS_LOG_INFO ("TcpSocketBase::SendPendingData: No endpoint; m_shutdownSend=" << m_shutdownSend); |
2052 return false; // Is this the right way to handle this condition? |
2065 return false; // Is this the right way to handle this condition? |
2053 } |
2066 } |
2054 uint32_t nPacketsSent = 0; |
2067 uint32_t nPacketsSent = 0; |
2055 while (m_txBuffer.SizeFromSequence (m_nextTxSequence)) |
2068 while (m_txBuffer->SizeFromSequence (m_nextTxSequence)) |
2056 { |
2069 { |
2057 uint32_t w = AvailableWindow (); // Get available window size |
2070 uint32_t w = AvailableWindow (); // Get available window size |
2058 NS_LOG_LOGIC ("TcpSocketBase " << this << " SendPendingData" << |
2071 NS_LOG_LOGIC ("TcpSocketBase " << this << " SendPendingData" << |
2059 " w " << w << |
2072 " w " << w << |
2060 " rxwin " << m_rWnd << |
2073 " rxwin " << m_rWnd << |
2061 " segsize " << m_segmentSize << |
2074 " segsize " << m_segmentSize << |
2062 " nextTxSeq " << m_nextTxSequence << |
2075 " nextTxSeq " << m_nextTxSequence << |
2063 " highestRxAck " << m_txBuffer.HeadSequence () << |
2076 " highestRxAck " << m_txBuffer->HeadSequence () << |
2064 " pd->Size " << m_txBuffer.Size () << |
2077 " pd->Size " << m_txBuffer->Size () << |
2065 " pd->SFS " << m_txBuffer.SizeFromSequence (m_nextTxSequence)); |
2078 " pd->SFS " << m_txBuffer->SizeFromSequence (m_nextTxSequence)); |
2066 // Stop sending if we need to wait for a larger Tx window (prevent silly window syndrome) |
2079 // Stop sending if we need to wait for a larger Tx window (prevent silly window syndrome) |
2067 if (w < m_segmentSize && m_txBuffer.SizeFromSequence (m_nextTxSequence) > w) |
2080 if (w < m_segmentSize && m_txBuffer->SizeFromSequence (m_nextTxSequence) > w) |
2068 { |
2081 { |
2069 break; // No more |
2082 break; // No more |
2070 } |
2083 } |
2071 // Nagle's algorithm (RFC896): Hold off sending if there is unacked data |
2084 // Nagle's algorithm (RFC896): Hold off sending if there is unacked data |
2072 // in the buffer and the amount of data to send is less than one segment |
2085 // in the buffer and the amount of data to send is less than one segment |
2073 if (!m_noDelay && UnAckDataCount () > 0 |
2086 if (!m_noDelay && UnAckDataCount () > 0 |
2074 && m_txBuffer.SizeFromSequence (m_nextTxSequence) < m_segmentSize) |
2087 && m_txBuffer->SizeFromSequence (m_nextTxSequence) < m_segmentSize) |
2075 { |
2088 { |
2076 NS_LOG_LOGIC ("Invoking Nagle's algorithm. Wait to send."); |
2089 NS_LOG_LOGIC ("Invoking Nagle's algorithm. Wait to send."); |
2077 break; |
2090 break; |
2078 } |
2091 } |
2079 uint32_t s = std::min (w, m_segmentSize); // Send no more than window |
2092 uint32_t s = std::min (w, m_segmentSize); // Send no more than window |
2087 |
2100 |
2088 uint32_t |
2101 uint32_t |
2089 TcpSocketBase::UnAckDataCount () |
2102 TcpSocketBase::UnAckDataCount () |
2090 { |
2103 { |
2091 NS_LOG_FUNCTION (this); |
2104 NS_LOG_FUNCTION (this); |
2092 return m_nextTxSequence.Get () - m_txBuffer.HeadSequence (); |
2105 return m_nextTxSequence.Get () - m_txBuffer->HeadSequence (); |
2093 } |
2106 } |
2094 |
2107 |
2095 uint32_t |
2108 uint32_t |
2096 TcpSocketBase::BytesInFlight () |
2109 TcpSocketBase::BytesInFlight () |
2097 { |
2110 { |
2098 NS_LOG_FUNCTION (this); |
2111 NS_LOG_FUNCTION (this); |
2099 return m_highTxMark.Get () - m_txBuffer.HeadSequence (); |
2112 return m_highTxMark.Get () - m_txBuffer->HeadSequence (); |
2100 } |
2113 } |
2101 |
2114 |
2102 uint32_t |
2115 uint32_t |
2103 TcpSocketBase::Window () |
2116 TcpSocketBase::Window () |
2104 { |
2117 { |
2140 NS_LOG_LOGIC ("seq " << tcpHeader.GetSequenceNumber () << |
2153 NS_LOG_LOGIC ("seq " << tcpHeader.GetSequenceNumber () << |
2141 " ack " << tcpHeader.GetAckNumber () << |
2154 " ack " << tcpHeader.GetAckNumber () << |
2142 " pkt size " << p->GetSize () ); |
2155 " pkt size " << p->GetSize () ); |
2143 |
2156 |
2144 // Put into Rx buffer |
2157 // Put into Rx buffer |
2145 SequenceNumber32 expectedSeq = m_rxBuffer.NextRxSequence (); |
2158 SequenceNumber32 expectedSeq = m_rxBuffer->NextRxSequence (); |
2146 if (!m_rxBuffer.Add (p, tcpHeader)) |
2159 if (!m_rxBuffer->Add (p, tcpHeader)) |
2147 { // Insert failed: No data or RX buffer full |
2160 { // Insert failed: No data or RX buffer full |
2148 SendEmptyPacket (TcpHeader::ACK); |
2161 SendEmptyPacket (TcpHeader::ACK); |
2149 return; |
2162 return; |
2150 } |
2163 } |
2151 // Now send a new ACK packet acknowledging all received and delivered data |
2164 // Now send a new ACK packet acknowledging all received and delivered data |
2152 if (m_rxBuffer.Size () > m_rxBuffer.Available () || m_rxBuffer.NextRxSequence () > expectedSeq + p->GetSize ()) |
2165 if (m_rxBuffer->Size () > m_rxBuffer->Available () || m_rxBuffer->NextRxSequence () > expectedSeq + p->GetSize ()) |
2153 { // A gap exists in the buffer, or we filled a gap: Always ACK |
2166 { // A gap exists in the buffer, or we filled a gap: Always ACK |
2154 SendEmptyPacket (TcpHeader::ACK); |
2167 SendEmptyPacket (TcpHeader::ACK); |
2155 } |
2168 } |
2156 else |
2169 else |
2157 { // In-sequence packet: ACK if delayed ack count allows |
2170 { // In-sequence packet: ACK if delayed ack count allows |
2167 &TcpSocketBase::DelAckTimeout, this); |
2180 &TcpSocketBase::DelAckTimeout, this); |
2168 NS_LOG_LOGIC (this << " scheduled delayed ACK at " << (Simulator::Now () + Simulator::GetDelayLeft (m_delAckEvent)).GetSeconds ()); |
2181 NS_LOG_LOGIC (this << " scheduled delayed ACK at " << (Simulator::Now () + Simulator::GetDelayLeft (m_delAckEvent)).GetSeconds ()); |
2169 } |
2182 } |
2170 } |
2183 } |
2171 // Notify app to receive if necessary |
2184 // Notify app to receive if necessary |
2172 if (expectedSeq < m_rxBuffer.NextRxSequence ()) |
2185 if (expectedSeq < m_rxBuffer->NextRxSequence ()) |
2173 { // NextRxSeq advanced, we have something to send to the app |
2186 { // NextRxSeq advanced, we have something to send to the app |
2174 if (!m_shutdownRecv) |
2187 if (!m_shutdownRecv) |
2175 { |
2188 { |
2176 NotifyDataRecv (); |
2189 NotifyDataRecv (); |
2177 } |
2190 } |
2275 m_persistEvent = Simulator::Schedule (m_persistTimeout, &TcpSocketBase::PersistTimeout, this); |
2288 m_persistEvent = Simulator::Schedule (m_persistTimeout, &TcpSocketBase::PersistTimeout, this); |
2276 NS_ASSERT (m_persistTimeout == Simulator::GetDelayLeft (m_persistEvent)); |
2289 NS_ASSERT (m_persistTimeout == Simulator::GetDelayLeft (m_persistEvent)); |
2277 } |
2290 } |
2278 // Note the highest ACK and tell app to send more |
2291 // Note the highest ACK and tell app to send more |
2279 NS_LOG_LOGIC ("TCP " << this << " NewAck " << ack << |
2292 NS_LOG_LOGIC ("TCP " << this << " NewAck " << ack << |
2280 " numberAck " << (ack - m_txBuffer.HeadSequence ())); // Number bytes ack'ed |
2293 " numberAck " << (ack - m_txBuffer->HeadSequence ())); // Number bytes ack'ed |
2281 m_txBuffer.DiscardUpTo (ack); |
2294 m_txBuffer->DiscardUpTo (ack); |
2282 if (GetTxAvailable () > 0) |
2295 if (GetTxAvailable () > 0) |
2283 { |
2296 { |
2284 NotifySend (GetTxAvailable ()); |
2297 NotifySend (GetTxAvailable ()); |
2285 } |
2298 } |
2286 if (ack > m_nextTxSequence) |
2299 if (ack > m_nextTxSequence) |
2287 { |
2300 { |
2288 m_nextTxSequence = ack; // If advanced |
2301 m_nextTxSequence = ack; // If advanced |
2289 } |
2302 } |
2290 if (m_txBuffer.Size () == 0 && m_state != FIN_WAIT_1 && m_state != CLOSING) |
2303 if (m_txBuffer->Size () == 0 && m_state != FIN_WAIT_1 && m_state != CLOSING) |
2291 { // No retransmit timer if no data to retransmit |
2304 { // No retransmit timer if no data to retransmit |
2292 NS_LOG_LOGIC (this << " Cancelled ReTxTimeout event which was set to expire at " << |
2305 NS_LOG_LOGIC (this << " Cancelled ReTxTimeout event which was set to expire at " << |
2293 (Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
2306 (Simulator::Now () + Simulator::GetDelayLeft (m_retxEvent)).GetSeconds ()); |
2294 m_retxEvent.Cancel (); |
2307 m_retxEvent.Cancel (); |
2295 } |
2308 } |
2346 void |
2359 void |
2347 TcpSocketBase::PersistTimeout () |
2360 TcpSocketBase::PersistTimeout () |
2348 { |
2361 { |
2349 NS_LOG_LOGIC ("PersistTimeout expired at " << Simulator::Now ().GetSeconds ()); |
2362 NS_LOG_LOGIC ("PersistTimeout expired at " << Simulator::Now ().GetSeconds ()); |
2350 m_persistTimeout = std::min (Seconds (60), Time (2 * m_persistTimeout)); // max persist timeout = 60s |
2363 m_persistTimeout = std::min (Seconds (60), Time (2 * m_persistTimeout)); // max persist timeout = 60s |
2351 Ptr<Packet> p = m_txBuffer.CopyFromSequence (1, m_nextTxSequence); |
2364 Ptr<Packet> p = m_txBuffer->CopyFromSequence (1, m_nextTxSequence); |
2352 TcpHeader tcpHeader; |
2365 TcpHeader tcpHeader; |
2353 tcpHeader.SetSequenceNumber (m_nextTxSequence); |
2366 tcpHeader.SetSequenceNumber (m_nextTxSequence); |
2354 tcpHeader.SetAckNumber (m_rxBuffer.NextRxSequence ()); |
2367 tcpHeader.SetAckNumber (m_rxBuffer->NextRxSequence ()); |
2355 tcpHeader.SetWindowSize (AdvertisedWindowSize ()); |
2368 tcpHeader.SetWindowSize (AdvertisedWindowSize ()); |
2356 if (m_endPoint != 0) |
2369 if (m_endPoint != 0) |
2357 { |
2370 { |
2358 tcpHeader.SetSourcePort (m_endPoint->GetLocalPort ()); |
2371 tcpHeader.SetSourcePort (m_endPoint->GetLocalPort ()); |
2359 tcpHeader.SetDestinationPort (m_endPoint->GetPeerPort ()); |
2372 tcpHeader.SetDestinationPort (m_endPoint->GetPeerPort ()); |
2405 NotifyConnectionFailed (); |
2418 NotifyConnectionFailed (); |
2406 } |
2419 } |
2407 return; |
2420 return; |
2408 } |
2421 } |
2409 // Retransmit non-data packet: Only if in FIN_WAIT_1 or CLOSING state |
2422 // Retransmit non-data packet: Only if in FIN_WAIT_1 or CLOSING state |
2410 if (m_txBuffer.Size () == 0) |
2423 if (m_txBuffer->Size () == 0) |
2411 { |
2424 { |
2412 if (m_state == FIN_WAIT_1 || m_state == CLOSING) |
2425 if (m_state == FIN_WAIT_1 || m_state == CLOSING) |
2413 { // Must have lost FIN, re-send |
2426 { // Must have lost FIN, re-send |
2414 SendEmptyPacket (TcpHeader::FIN); |
2427 SendEmptyPacket (TcpHeader::FIN); |
2415 } |
2428 } |
2416 return; |
2429 return; |
2417 } |
2430 } |
2418 // Retransmit a data packet: Call SendDataPacket |
2431 // Retransmit a data packet: Call SendDataPacket |
2419 NS_LOG_LOGIC ("TcpSocketBase " << this << " retxing seq " << m_txBuffer.HeadSequence ()); |
2432 NS_LOG_LOGIC ("TcpSocketBase " << this << " retxing seq " << m_txBuffer->HeadSequence ()); |
2420 uint32_t sz = SendDataPacket (m_txBuffer.HeadSequence (), m_segmentSize, true); |
2433 uint32_t sz = SendDataPacket (m_txBuffer->HeadSequence (), m_segmentSize, true); |
2421 // In case of RTO, advance m_nextTxSequence |
2434 // In case of RTO, advance m_nextTxSequence |
2422 m_nextTxSequence = std::max (m_nextTxSequence.Get (), m_txBuffer.HeadSequence () + sz); |
2435 m_nextTxSequence = std::max (m_nextTxSequence.Get (), m_txBuffer->HeadSequence () + sz); |
2423 |
2436 |
2424 } |
2437 } |
2425 |
2438 |
2426 void |
2439 void |
2427 TcpSocketBase::CancelAllTimers () |
2440 TcpSocketBase::CancelAllTimers () |
2449 /* Below are the attribute get/set functions */ |
2462 /* Below are the attribute get/set functions */ |
2450 |
2463 |
2451 void |
2464 void |
2452 TcpSocketBase::SetSndBufSize (uint32_t size) |
2465 TcpSocketBase::SetSndBufSize (uint32_t size) |
2453 { |
2466 { |
2454 m_txBuffer.SetMaxBufferSize (size); |
2467 m_txBuffer->SetMaxBufferSize (size); |
2455 } |
2468 } |
2456 |
2469 |
2457 uint32_t |
2470 uint32_t |
2458 TcpSocketBase::GetSndBufSize (void) const |
2471 TcpSocketBase::GetSndBufSize (void) const |
2459 { |
2472 { |
2460 return m_txBuffer.MaxBufferSize (); |
2473 return m_txBuffer->MaxBufferSize (); |
2461 } |
2474 } |
2462 |
2475 |
2463 void |
2476 void |
2464 TcpSocketBase::SetRcvBufSize (uint32_t size) |
2477 TcpSocketBase::SetRcvBufSize (uint32_t size) |
2465 { |
2478 { |
2466 m_rxBuffer.SetMaxBufferSize (size); |
2479 m_rxBuffer->SetMaxBufferSize (size); |
2467 } |
2480 } |
2468 |
2481 |
2469 uint32_t |
2482 uint32_t |
2470 TcpSocketBase::GetRcvBufSize (void) const |
2483 TcpSocketBase::GetRcvBufSize (void) const |
2471 { |
2484 { |
2472 return m_rxBuffer.MaxBufferSize (); |
2485 return m_rxBuffer->MaxBufferSize (); |
2473 } |
2486 } |
2474 |
2487 |
2475 void |
2488 void |
2476 TcpSocketBase::SetSegSize (uint32_t size) |
2489 TcpSocketBase::SetSegSize (uint32_t size) |
2477 { |
2490 { |
2731 TcpSocketBase::GetClockGranularity (void) const |
2744 TcpSocketBase::GetClockGranularity (void) const |
2732 { |
2745 { |
2733 return m_clockGranularity; |
2746 return m_clockGranularity; |
2734 } |
2747 } |
2735 |
2748 |
|
2749 Ptr<TcpTxBuffer> |
|
2750 TcpSocketBase::GetTxBuffer (void) const |
|
2751 { |
|
2752 return m_txBuffer; |
|
2753 } |
|
2754 |
|
2755 Ptr<TcpRxBuffer> |
|
2756 TcpSocketBase::GetRxBuffer (void) const |
|
2757 { |
|
2758 return m_rxBuffer; |
|
2759 } |
|
2760 |
|
2761 |
2736 //RttHistory methods |
2762 //RttHistory methods |
2737 RttHistory::RttHistory (SequenceNumber32 s, uint32_t c, Time t) |
2763 RttHistory::RttHistory (SequenceNumber32 s, uint32_t c, Time t) |
2738 : seq (s), count (c), time (t), retx (false) |
2764 : seq (s), count (c), time (t), retx (false) |
2739 { |
2765 { |
2740 } |
2766 } |