3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/p2p/base/pseudotcp.h"
35 #include "webrtc/base/basictypes.h"
36 #include "webrtc/base/bytebuffer.h"
37 #include "webrtc/base/byteorder.h"
38 #include "webrtc/base/common.h"
39 #include "webrtc/base/logging.h"
40 #include "webrtc/base/scoped_ptr.h"
41 #include "webrtc/base/socket.h"
42 #include "webrtc/base/stringutils.h"
43 #include "webrtc/base/timeutils.h"
45 // The following logging is for detailed (packet-level) analysis only.
48 #define _DBG_VERBOSE 2
49 #define _DEBUGMSG _DBG_NONE
53 //////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////
58 const uint16 PACKET_MAXIMUMS[] = {
59 65535, // Theoretical maximum, Hyperchannel
61 17914, // 16Mb IBM Token Ring
63 //4464, // IEEE 802.5 (4Mb max)
65 //2048, // Wideband Network
66 2002, // IEEE 802.5 (4Mb recommended)
67 //1536, // Expermental Ethernet Networks
68 //1500, // Ethernet, Point-to-Point (default)
70 1006, // SLIP, ARPANET
71 //576, // X.25 Networks
72 //544, // DEC IP Portal
74 508, // IEEE 802/Source-Rt Bridge, ARCNET
75 296, // Point-to-Point (low delay)
76 //68, // Official minimum
77 0, // End of list marker
80 const uint32 MAX_PACKET = 65535;
81 // Note: we removed lowest level because packet overhead was larger!
82 const uint32 MIN_PACKET = 296;
84 const uint32 IP_HEADER_SIZE = 20; // (+ up to 40 bytes of options?)
85 const uint32 UDP_HEADER_SIZE = 8;
86 // TODO: Make JINGLE_HEADER_SIZE transparent to this code?
87 const uint32 JINGLE_HEADER_SIZE = 64; // when relay framing is in use
89 // Default size for receive and send buffer.
90 const uint32 DEFAULT_RCV_BUF_SIZE = 60 * 1024;
91 const uint32 DEFAULT_SND_BUF_SIZE = 90 * 1024;
93 //////////////////////////////////////////////////////////////////////
94 // Global Constants and Functions
95 //////////////////////////////////////////////////////////////////////
98 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
99 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 // 0 | Conversation Number |
101 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 // 4 | Sequence Number |
103 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 // 8 | Acknowledgment Number |
105 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 // | | |U|A|P|R|S|F| |
107 // 12 | Control | |R|C|S|S|Y|I| Window |
108 // | | |G|K|H|T|N|N| |
109 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 // 16 | Timestamp sending |
111 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 // 20 | Timestamp receiving |
113 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 //////////////////////////////////////////////////////////////////////
119 #define PSEUDO_KEEPALIVE 0
121 const uint32 HEADER_SIZE = 24;
122 const uint32 PACKET_OVERHEAD = HEADER_SIZE + UDP_HEADER_SIZE + IP_HEADER_SIZE + JINGLE_HEADER_SIZE;
124 const uint32 MIN_RTO = 250; // 250 ms (RFC1122, Sec 4.2.3.1 "fractions of a second")
125 const uint32 DEF_RTO = 3000; // 3 seconds (RFC1122, Sec 4.2.3.1)
126 const uint32 MAX_RTO = 60000; // 60 seconds
127 const uint32 DEF_ACK_DELAY = 100; // 100 milliseconds
129 const uint8 FLAG_CTL = 0x02;
130 const uint8 FLAG_RST = 0x04;
132 const uint8 CTL_CONNECT = 0;
135 const uint8 TCP_OPT_EOL = 0; // End of list.
136 const uint8 TCP_OPT_NOOP = 1; // No-op.
137 const uint8 TCP_OPT_MSS = 2; // Maximum segment size.
138 const uint8 TCP_OPT_WND_SCALE = 3; // Window scale factor.
140 const long DEFAULT_TIMEOUT = 4000; // If there are no pending clocks, wake up every 4 seconds
141 const long CLOSED_TIMEOUT = 60 * 1000; // If the connection is closed, once per minute
144 // !?! Rethink these times
145 const uint32 IDLE_PING = 20 * 1000; // 20 seconds (note: WinXP SP2 firewall udp timeout is 90 seconds)
146 const uint32 IDLE_TIMEOUT = 90 * 1000; // 90 seconds;
147 #endif // PSEUDO_KEEPALIVE
149 //////////////////////////////////////////////////////////////////////
151 //////////////////////////////////////////////////////////////////////
153 inline void long_to_bytes(uint32 val, void* buf) {
154 *static_cast<uint32*>(buf) = rtc::HostToNetwork32(val);
157 inline void short_to_bytes(uint16 val, void* buf) {
158 *static_cast<uint16*>(buf) = rtc::HostToNetwork16(val);
161 inline uint32 bytes_to_long(const void* buf) {
162 return rtc::NetworkToHost32(*static_cast<const uint32*>(buf));
165 inline uint16 bytes_to_short(const void* buf) {
166 return rtc::NetworkToHost16(*static_cast<const uint16*>(buf));
169 uint32 bound(uint32 lower, uint32 middle, uint32 upper) {
170 return rtc::_min(rtc::_max(lower, middle), upper);
173 //////////////////////////////////////////////////////////////////////
174 // Debugging Statistics
175 //////////////////////////////////////////////////////////////////////
177 #if 0 // Not used yet
180 S_SENT_PACKET, // All packet sends
181 S_RESENT_PACKET, // All packet sends that are retransmits
182 S_RECV_PACKET, // All packet receives
183 S_RECV_NEW, // All packet receives that are too new
184 S_RECV_OLD, // All packet receives that are too old
188 const char* const STAT_NAMES[S_NUM_STATS] = {
196 int g_stats[S_NUM_STATS];
197 inline void Incr(Stat s) { ++g_stats[s]; }
201 for (int i = 0; i < S_NUM_STATS; ++i) {
202 len += rtc::sprintfn(buffer, ARRAY_SIZE(buffer), "%s%s:%d",
203 (i == 0) ? "" : ",", STAT_NAMES[i], g_stats[i]);
206 LOG(LS_INFO) << "Stats[" << buffer << "]";
211 //////////////////////////////////////////////////////////////////////
213 //////////////////////////////////////////////////////////////////////
215 uint32 PseudoTcp::Now() {
216 #if 0 // Use this to synchronize timers with logging timestamps (easier debug)
217 return rtc::TimeSince(StartTime());
223 PseudoTcp::PseudoTcp(IPseudoTcpNotify* notify, uint32 conv)
227 m_rbuf_len(DEFAULT_RCV_BUF_SIZE),
229 m_sbuf_len(DEFAULT_SND_BUF_SIZE),
232 // Sanity check on buffer sizes (needed for OnTcpWriteable notification logic)
233 ASSERT(m_rbuf_len + MIN_PACKET < m_sbuf_len);
237 m_state = TCP_LISTEN;
239 m_rcv_wnd = m_rbuf_len;
240 m_rwnd_scale = m_swnd_scale = 0;
243 m_snd_una = m_rcv_nxt = 0;
244 m_bReadEnable = true;
245 m_bWriteEnable = false;
250 ASSERT(MIN_PACKET > PACKET_OVERHEAD);
251 m_mss = MIN_PACKET - PACKET_OVERHEAD;
252 m_mtu_advise = MAX_PACKET;
257 m_ssthresh = m_rbuf_len;
258 m_lastrecv = m_lastsend = m_lasttraffic = now;
264 m_ts_recent = m_ts_lastack = 0;
267 m_rx_srtt = m_rx_rttvar = 0;
269 m_use_nagling = true;
270 m_ack_delay = DEF_ACK_DELAY;
271 m_support_wnd_scale = true;
274 PseudoTcp::~PseudoTcp() {
277 int PseudoTcp::Connect() {
278 if (m_state != TCP_LISTEN) {
283 m_state = TCP_SYN_SENT;
284 LOG(LS_INFO) << "State: TCP_SYN_SENT";
286 queueConnectMessage();
292 void PseudoTcp::NotifyMTU(uint16 mtu) {
294 if (m_state == TCP_ESTABLISHED) {
299 void PseudoTcp::NotifyClock(uint32 now) {
300 if (m_state == TCP_CLOSED)
303 // Check if it's time to retransmit a segment
304 if (m_rto_base && (rtc::TimeDiff(m_rto_base + m_rx_rto, now) <= 0)) {
305 if (m_slist.empty()) {
308 // Note: (m_slist.front().xmit == 0)) {
309 // retransmit segments
310 #if _DEBUGMSG >= _DBG_NORMAL
311 LOG(LS_INFO) << "timeout retransmit (rto: " << m_rx_rto
312 << ") (rto_base: " << m_rto_base
314 << ") (dup_acks: " << static_cast<unsigned>(m_dup_acks)
317 if (!transmit(m_slist.begin(), now)) {
318 closedown(ECONNABORTED);
322 uint32 nInFlight = m_snd_nxt - m_snd_una;
323 m_ssthresh = rtc::_max(nInFlight / 2, 2 * m_mss);
324 //LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss;
327 // Back off retransmit timer. Note: the limit is lower when connecting.
328 uint32 rto_limit = (m_state < TCP_ESTABLISHED) ? DEF_RTO : MAX_RTO;
329 m_rx_rto = rtc::_min(rto_limit, m_rx_rto * 2);
334 // Check if it's time to probe closed windows
336 && (rtc::TimeDiff(m_lastsend + m_rx_rto, now) <= 0)) {
337 if (rtc::TimeDiff(now, m_lastrecv) >= 15000) {
338 closedown(ECONNABORTED);
343 packet(m_snd_nxt - 1, 0, 0, 0);
346 // back off retransmit timer
347 m_rx_rto = rtc::_min(MAX_RTO, m_rx_rto * 2);
350 // Check if it's time to send delayed acks
351 if (m_t_ack && (rtc::TimeDiff(m_t_ack + m_ack_delay, now) <= 0)) {
352 packet(m_snd_nxt, 0, 0, 0);
356 // Check for idle timeout
357 if ((m_state == TCP_ESTABLISHED) && (TimeDiff(m_lastrecv + IDLE_TIMEOUT, now) <= 0)) {
358 closedown(ECONNABORTED);
362 // Check for ping timeout (to keep udp mapping open)
363 if ((m_state == TCP_ESTABLISHED) && (TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3/2 : IDLE_PING), now) <= 0)) {
364 packet(m_snd_nxt, 0, 0, 0);
366 #endif // PSEUDO_KEEPALIVE
369 bool PseudoTcp::NotifyPacket(const char* buffer, size_t len) {
370 if (len > MAX_PACKET) {
371 LOG_F(WARNING) << "packet too large";
374 return parse(reinterpret_cast<const uint8 *>(buffer), uint32(len));
377 bool PseudoTcp::GetNextClock(uint32 now, long& timeout) {
378 return clock_check(now, timeout);
381 void PseudoTcp::GetOption(Option opt, int* value) {
382 if (opt == OPT_NODELAY) {
383 *value = m_use_nagling ? 0 : 1;
384 } else if (opt == OPT_ACKDELAY) {
385 *value = m_ack_delay;
386 } else if (opt == OPT_SNDBUF) {
388 } else if (opt == OPT_RCVBUF) {
394 void PseudoTcp::SetOption(Option opt, int value) {
395 if (opt == OPT_NODELAY) {
396 m_use_nagling = value == 0;
397 } else if (opt == OPT_ACKDELAY) {
399 } else if (opt == OPT_SNDBUF) {
400 ASSERT(m_state == TCP_LISTEN);
401 resizeSendBuffer(value);
402 } else if (opt == OPT_RCVBUF) {
403 ASSERT(m_state == TCP_LISTEN);
404 resizeReceiveBuffer(value);
410 uint32 PseudoTcp::GetCongestionWindow() const {
414 uint32 PseudoTcp::GetBytesInFlight() const {
415 return m_snd_nxt - m_snd_una;
418 uint32 PseudoTcp::GetBytesBufferedNotSent() const {
419 size_t buffered_bytes = 0;
420 m_sbuf.GetBuffered(&buffered_bytes);
421 return static_cast<uint32>(m_snd_una + buffered_bytes - m_snd_nxt);
424 uint32 PseudoTcp::GetRoundTripTimeEstimateMs() const {
429 // IPStream Implementation
432 int PseudoTcp::Recv(char* buffer, size_t len) {
433 if (m_state != TCP_ESTABLISHED) {
439 rtc::StreamResult result = m_rbuf.Read(buffer, len, &read, NULL);
441 // If there's no data in |m_rbuf|.
442 if (result == rtc::SR_BLOCK) {
443 m_bReadEnable = true;
444 m_error = EWOULDBLOCK;
447 ASSERT(result == rtc::SR_SUCCESS);
449 size_t available_space = 0;
450 m_rbuf.GetWriteRemaining(&available_space);
452 if (uint32(available_space) - m_rcv_wnd >=
453 rtc::_min<uint32>(m_rbuf_len / 2, m_mss)) {
454 // TODO(jbeda): !?! Not sure about this was closed business
455 bool bWasClosed = (m_rcv_wnd == 0);
456 m_rcv_wnd = static_cast<uint32>(available_space);
459 attemptSend(sfImmediateAck);
463 return static_cast<int>(read);
466 int PseudoTcp::Send(const char* buffer, size_t len) {
467 if (m_state != TCP_ESTABLISHED) {
472 size_t available_space = 0;
473 m_sbuf.GetWriteRemaining(&available_space);
475 if (!available_space) {
476 m_bWriteEnable = true;
477 m_error = EWOULDBLOCK;
481 int written = queue(buffer, uint32(len), false);
486 void PseudoTcp::Close(bool force) {
487 LOG_F(LS_VERBOSE) << "(" << (force ? "true" : "false") << ")";
488 m_shutdown = force ? SD_FORCEFUL : SD_GRACEFUL;
491 int PseudoTcp::GetError() {
496 // Internal Implementation
499 uint32 PseudoTcp::queue(const char* data, uint32 len, bool bCtrl) {
500 size_t available_space = 0;
501 m_sbuf.GetWriteRemaining(&available_space);
503 if (len > static_cast<uint32>(available_space)) {
505 len = static_cast<uint32>(available_space);
508 // We can concatenate data if the last segment is the same type
509 // (control v. regular data), and has not been transmitted yet
510 if (!m_slist.empty() && (m_slist.back().bCtrl == bCtrl) &&
511 (m_slist.back().xmit == 0)) {
512 m_slist.back().len += len;
514 size_t snd_buffered = 0;
515 m_sbuf.GetBuffered(&snd_buffered);
516 SSegment sseg(static_cast<uint32>(m_snd_una + snd_buffered), len, bCtrl);
517 m_slist.push_back(sseg);
521 m_sbuf.Write(data, len, &written, NULL);
522 return static_cast<uint32>(written);
525 IPseudoTcpNotify::WriteResult PseudoTcp::packet(uint32 seq, uint8 flags,
526 uint32 offset, uint32 len) {
527 ASSERT(HEADER_SIZE + len <= MAX_PACKET);
531 rtc::scoped_ptr<uint8[]> buffer(new uint8[MAX_PACKET]);
532 long_to_bytes(m_conv, buffer.get());
533 long_to_bytes(seq, buffer.get() + 4);
534 long_to_bytes(m_rcv_nxt, buffer.get() + 8);
538 static_cast<uint16>(m_rcv_wnd >> m_rwnd_scale), buffer.get() + 14);
540 // Timestamp computations
541 long_to_bytes(now, buffer.get() + 16);
542 long_to_bytes(m_ts_recent, buffer.get() + 20);
543 m_ts_lastack = m_rcv_nxt;
546 size_t bytes_read = 0;
547 rtc::StreamResult result = m_sbuf.ReadOffset(
548 buffer.get() + HEADER_SIZE, len, offset, &bytes_read);
550 ASSERT(result == rtc::SR_SUCCESS);
551 ASSERT(static_cast<uint32>(bytes_read) == len);
554 #if _DEBUGMSG >= _DBG_VERBOSE
555 LOG(LS_INFO) << "<-- <CONV=" << m_conv
556 << "><FLG=" << static_cast<unsigned>(flags)
557 << "><SEQ=" << seq << ":" << seq + len
558 << "><ACK=" << m_rcv_nxt
559 << "><WND=" << m_rcv_wnd
560 << "><TS=" << (now % 10000)
561 << "><TSR=" << (m_ts_recent % 10000)
562 << "><LEN=" << len << ">";
565 IPseudoTcpNotify::WriteResult wres = m_notify->TcpWritePacket(
566 this, reinterpret_cast<char *>(buffer.get()), len + HEADER_SIZE);
567 // Note: When len is 0, this is an ACK packet. We don't read the return value for those,
568 // and thus we won't retry. So go ahead and treat the packet as a success (basically simulate
569 // as if it were dropped), which will prevent our timers from being messed up.
570 if ((wres != IPseudoTcpNotify::WR_SUCCESS) && (0 != len))
580 return IPseudoTcpNotify::WR_SUCCESS;
583 bool PseudoTcp::parse(const uint8* buffer, uint32 size) {
588 seg.conv = bytes_to_long(buffer);
589 seg.seq = bytes_to_long(buffer + 4);
590 seg.ack = bytes_to_long(buffer + 8);
591 seg.flags = buffer[13];
592 seg.wnd = bytes_to_short(buffer + 14);
594 seg.tsval = bytes_to_long(buffer + 16);
595 seg.tsecr = bytes_to_long(buffer + 20);
597 seg.data = reinterpret_cast<const char *>(buffer) + HEADER_SIZE;
598 seg.len = size - HEADER_SIZE;
600 #if _DEBUGMSG >= _DBG_VERBOSE
601 LOG(LS_INFO) << "--> <CONV=" << seg.conv
602 << "><FLG=" << static_cast<unsigned>(seg.flags)
603 << "><SEQ=" << seg.seq << ":" << seg.seq + seg.len
604 << "><ACK=" << seg.ack
605 << "><WND=" << seg.wnd
606 << "><TS=" << (seg.tsval % 10000)
607 << "><TSR=" << (seg.tsecr % 10000)
608 << "><LEN=" << seg.len << ">";
614 bool PseudoTcp::clock_check(uint32 now, long& nTimeout) {
615 if (m_shutdown == SD_FORCEFUL)
618 size_t snd_buffered = 0;
619 m_sbuf.GetBuffered(&snd_buffered);
620 if ((m_shutdown == SD_GRACEFUL)
621 && ((m_state != TCP_ESTABLISHED)
622 || ((snd_buffered == 0) && (m_t_ack == 0)))) {
626 if (m_state == TCP_CLOSED) {
627 nTimeout = CLOSED_TIMEOUT;
631 nTimeout = DEFAULT_TIMEOUT;
634 nTimeout = rtc::_min<int32>(nTimeout,
635 rtc::TimeDiff(m_t_ack + m_ack_delay, now));
638 nTimeout = rtc::_min<int32>(nTimeout,
639 rtc::TimeDiff(m_rto_base + m_rx_rto, now));
641 if (m_snd_wnd == 0) {
642 nTimeout = rtc::_min<int32>(nTimeout, rtc::TimeDiff(m_lastsend + m_rx_rto, now));
645 if (m_state == TCP_ESTABLISHED) {
646 nTimeout = rtc::_min<int32>(nTimeout,
647 rtc::TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3/2 : IDLE_PING), now));
649 #endif // PSEUDO_KEEPALIVE
653 bool PseudoTcp::process(Segment& seg) {
654 // If this is the wrong conversation, send a reset!?! (with the correct conversation?)
655 if (seg.conv != m_conv) {
656 //if ((seg.flags & FLAG_RST) == 0) {
657 // packet(tcb, seg.ack, 0, FLAG_RST, 0, 0);
659 LOG_F(LS_ERROR) << "wrong conversation";
664 m_lasttraffic = m_lastrecv = now;
667 if (m_state == TCP_CLOSED) {
669 LOG_F(LS_ERROR) << "closed";
673 // Check if this is a reset segment
674 if (seg.flags & FLAG_RST) {
675 closedown(ECONNRESET);
679 // Check for control data
680 bool bConnect = false;
681 if (seg.flags & FLAG_CTL) {
683 LOG_F(LS_ERROR) << "Missing control code";
685 } else if (seg.data[0] == CTL_CONNECT) {
688 // TCP options are in the remainder of the payload after CTL_CONNECT.
689 parseOptions(&seg.data[1], seg.len - 1);
691 if (m_state == TCP_LISTEN) {
692 m_state = TCP_SYN_RECEIVED;
693 LOG(LS_INFO) << "State: TCP_SYN_RECEIVED";
694 //m_notify->associate(addr);
695 queueConnectMessage();
696 } else if (m_state == TCP_SYN_SENT) {
697 m_state = TCP_ESTABLISHED;
698 LOG(LS_INFO) << "State: TCP_ESTABLISHED";
701 m_notify->OnTcpOpen(this);
706 LOG_F(LS_WARNING) << "Unknown control code: " << seg.data[0];
712 if ((seg.seq <= m_ts_lastack) && (m_ts_lastack < seg.seq + seg.len)) {
713 m_ts_recent = seg.tsval;
716 // Check if this is a valuable ack
717 if ((seg.ack > m_snd_una) && (seg.ack <= m_snd_nxt)) {
718 // Calculate round-trip time
720 int32 rtt = rtc::TimeDiff(now, seg.tsecr);
722 if (m_rx_srtt == 0) {
724 m_rx_rttvar = rtt / 2;
726 uint32 unsigned_rtt = static_cast<uint32>(rtt);
727 uint32 abs_err = unsigned_rtt > m_rx_srtt ? unsigned_rtt - m_rx_srtt
728 : m_rx_srtt - unsigned_rtt;
729 m_rx_rttvar = (3 * m_rx_rttvar + abs_err) / 4;
730 m_rx_srtt = (7 * m_rx_srtt + rtt) / 8;
732 m_rx_rto = bound(MIN_RTO, m_rx_srtt +
733 rtc::_max<uint32>(1, 4 * m_rx_rttvar), MAX_RTO);
734 #if _DEBUGMSG >= _DBG_VERBOSE
735 LOG(LS_INFO) << "rtt: " << rtt
736 << " srtt: " << m_rx_srtt
737 << " rto: " << m_rx_rto;
744 m_snd_wnd = static_cast<uint32>(seg.wnd) << m_swnd_scale;
746 uint32 nAcked = seg.ack - m_snd_una;
749 m_rto_base = (m_snd_una == m_snd_nxt) ? 0 : now;
751 m_sbuf.ConsumeReadData(nAcked);
753 for (uint32 nFree = nAcked; nFree > 0; ) {
754 ASSERT(!m_slist.empty());
755 if (nFree < m_slist.front().len) {
756 m_slist.front().len -= nFree;
759 if (m_slist.front().len > m_largest) {
760 m_largest = m_slist.front().len;
762 nFree -= m_slist.front().len;
767 if (m_dup_acks >= 3) {
768 if (m_snd_una >= m_recover) { // NewReno
769 uint32 nInFlight = m_snd_nxt - m_snd_una;
770 m_cwnd = rtc::_min(m_ssthresh, nInFlight + m_mss); // (Fast Retransmit)
771 #if _DEBUGMSG >= _DBG_NORMAL
772 LOG(LS_INFO) << "exit recovery";
776 #if _DEBUGMSG >= _DBG_NORMAL
777 LOG(LS_INFO) << "recovery retransmit";
779 if (!transmit(m_slist.begin(), now)) {
780 closedown(ECONNABORTED);
783 m_cwnd += m_mss - rtc::_min(nAcked, m_cwnd);
787 // Slow start, congestion avoidance
788 if (m_cwnd < m_ssthresh) {
791 m_cwnd += rtc::_max<uint32>(1, m_mss * m_mss / m_cwnd);
794 } else if (seg.ack == m_snd_una) {
795 // !?! Note, tcp says don't do this... but otherwise how does a closed window become open?
796 m_snd_wnd = static_cast<uint32>(seg.wnd) << m_swnd_scale;
798 // Check duplicate acks
800 // it's a dup ack, but with a data payload, so don't modify m_dup_acks
801 } else if (m_snd_una != m_snd_nxt) {
803 if (m_dup_acks == 3) { // (Fast Retransmit)
804 #if _DEBUGMSG >= _DBG_NORMAL
805 LOG(LS_INFO) << "enter recovery";
806 LOG(LS_INFO) << "recovery retransmit";
808 if (!transmit(m_slist.begin(), now)) {
809 closedown(ECONNABORTED);
812 m_recover = m_snd_nxt;
813 uint32 nInFlight = m_snd_nxt - m_snd_una;
814 m_ssthresh = rtc::_max(nInFlight / 2, 2 * m_mss);
815 //LOG(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss;
816 m_cwnd = m_ssthresh + 3 * m_mss;
817 } else if (m_dup_acks > 3) {
826 if ((m_state == TCP_SYN_RECEIVED) && !bConnect) {
827 m_state = TCP_ESTABLISHED;
828 LOG(LS_INFO) << "State: TCP_ESTABLISHED";
831 m_notify->OnTcpOpen(this);
836 // If we make room in the send queue, notify the user
837 // The goal it to make sure we always have at least enough data to fill the
838 // window. We'd like to notify the app when we are halfway to that point.
839 const uint32 kIdealRefillSize = (m_sbuf_len + m_rbuf_len) / 2;
840 size_t snd_buffered = 0;
841 m_sbuf.GetBuffered(&snd_buffered);
842 if (m_bWriteEnable && static_cast<uint32>(snd_buffered) < kIdealRefillSize) {
843 m_bWriteEnable = false;
845 m_notify->OnTcpWriteable(this);
850 // Conditions were acks must be sent:
851 // 1) Segment is too old (they missed an ACK) (immediately)
852 // 2) Segment is too new (we missed a segment) (immediately)
853 // 3) Segment has data (so we need to ACK!) (delayed)
854 // ... so the only time we don't need to ACK, is an empty segment that points to rcv_nxt!
856 SendFlags sflags = sfNone;
857 if (seg.seq != m_rcv_nxt) {
858 sflags = sfImmediateAck; // (Fast Recovery)
859 } else if (seg.len != 0) {
860 if (m_ack_delay == 0) {
861 sflags = sfImmediateAck;
863 sflags = sfDelayedAck;
866 #if _DEBUGMSG >= _DBG_NORMAL
867 if (sflags == sfImmediateAck) {
868 if (seg.seq > m_rcv_nxt) {
869 LOG_F(LS_INFO) << "too new";
870 } else if (seg.seq + seg.len <= m_rcv_nxt) {
871 LOG_F(LS_INFO) << "too old";
876 // Adjust the incoming segment to fit our receive buffer
877 if (seg.seq < m_rcv_nxt) {
878 uint32 nAdjust = m_rcv_nxt - seg.seq;
879 if (nAdjust < seg.len) {
888 size_t available_space = 0;
889 m_rbuf.GetWriteRemaining(&available_space);
891 if ((seg.seq + seg.len - m_rcv_nxt) > static_cast<uint32>(available_space)) {
892 uint32 nAdjust = seg.seq + seg.len - m_rcv_nxt - static_cast<uint32>(available_space);
893 if (nAdjust < seg.len) {
900 bool bIgnoreData = (seg.flags & FLAG_CTL) || (m_shutdown != SD_NONE);
901 bool bNewData = false;
905 if (seg.seq == m_rcv_nxt) {
906 m_rcv_nxt += seg.len;
909 uint32 nOffset = seg.seq - m_rcv_nxt;
911 rtc::StreamResult result = m_rbuf.WriteOffset(seg.data, seg.len,
913 ASSERT(result == rtc::SR_SUCCESS);
916 if (seg.seq == m_rcv_nxt) {
917 m_rbuf.ConsumeWriteBuffer(seg.len);
918 m_rcv_nxt += seg.len;
919 m_rcv_wnd -= seg.len;
922 RList::iterator it = m_rlist.begin();
923 while ((it != m_rlist.end()) && (it->seq <= m_rcv_nxt)) {
924 if (it->seq + it->len > m_rcv_nxt) {
925 sflags = sfImmediateAck; // (Fast Recovery)
926 uint32 nAdjust = (it->seq + it->len) - m_rcv_nxt;
927 #if _DEBUGMSG >= _DBG_NORMAL
928 LOG(LS_INFO) << "Recovered " << nAdjust << " bytes (" << m_rcv_nxt << " -> " << m_rcv_nxt + nAdjust << ")";
930 m_rbuf.ConsumeWriteBuffer(nAdjust);
931 m_rcv_nxt += nAdjust;
932 m_rcv_wnd -= nAdjust;
934 it = m_rlist.erase(it);
937 #if _DEBUGMSG >= _DBG_NORMAL
938 LOG(LS_INFO) << "Saving " << seg.len << " bytes (" << seg.seq << " -> " << seg.seq + seg.len << ")";
943 RList::iterator it = m_rlist.begin();
944 while ((it != m_rlist.end()) && (it->seq < rseg.seq)) {
947 m_rlist.insert(it, rseg);
954 // If we have new data, notify the user
955 if (bNewData && m_bReadEnable) {
956 m_bReadEnable = false;
958 m_notify->OnTcpReadable(this);
966 bool PseudoTcp::transmit(const SList::iterator& seg, uint32 now) {
967 if (seg->xmit >= ((m_state == TCP_ESTABLISHED) ? 15 : 30)) {
968 LOG_F(LS_VERBOSE) << "too many retransmits";
972 uint32 nTransmit = rtc::_min(seg->len, m_mss);
975 uint32 seq = seg->seq;
976 uint8 flags = (seg->bCtrl ? FLAG_CTL : 0);
977 IPseudoTcpNotify::WriteResult wres = packet(seq,
979 seg->seq - m_snd_una,
982 if (wres == IPseudoTcpNotify::WR_SUCCESS)
985 if (wres == IPseudoTcpNotify::WR_FAIL) {
986 LOG_F(LS_VERBOSE) << "packet failed";
990 ASSERT(wres == IPseudoTcpNotify::WR_TOO_LARGE);
993 if (PACKET_MAXIMUMS[m_msslevel + 1] == 0) {
994 LOG_F(LS_VERBOSE) << "MTU too small";
997 // !?! We need to break up all outstanding and pending packets and then retransmit!?!
999 m_mss = PACKET_MAXIMUMS[++m_msslevel] - PACKET_OVERHEAD;
1000 m_cwnd = 2 * m_mss; // I added this... haven't researched actual formula
1001 if (m_mss < nTransmit) {
1006 #if _DEBUGMSG >= _DBG_NORMAL
1007 LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes";
1011 if (nTransmit < seg->len) {
1012 LOG_F(LS_VERBOSE) << "mss reduced to " << m_mss;
1014 SSegment subseg(seg->seq + nTransmit, seg->len - nTransmit, seg->bCtrl);
1015 //subseg.tstamp = seg->tstamp;
1016 subseg.xmit = seg->xmit;
1017 seg->len = nTransmit;
1019 SList::iterator next = seg;
1020 m_slist.insert(++next, subseg);
1023 if (seg->xmit == 0) {
1024 m_snd_nxt += seg->len;
1027 //seg->tstamp = now;
1028 if (m_rto_base == 0) {
1035 void PseudoTcp::attemptSend(SendFlags sflags) {
1038 if (rtc::TimeDiff(now, m_lastsend) > static_cast<long>(m_rx_rto)) {
1048 uint32 cwnd = m_cwnd;
1049 if ((m_dup_acks == 1) || (m_dup_acks == 2)) { // Limited Transmit
1050 cwnd += m_dup_acks * m_mss;
1052 uint32 nWindow = rtc::_min(m_snd_wnd, cwnd);
1053 uint32 nInFlight = m_snd_nxt - m_snd_una;
1054 uint32 nUseable = (nInFlight < nWindow) ? (nWindow - nInFlight) : 0;
1056 size_t snd_buffered = 0;
1057 m_sbuf.GetBuffered(&snd_buffered);
1059 rtc::_min(static_cast<uint32>(snd_buffered) - nInFlight, m_mss);
1061 if (nAvailable > nUseable) {
1062 if (nUseable * 4 < nWindow) {
1063 // RFC 813 - avoid SWS
1066 nAvailable = nUseable;
1070 #if _DEBUGMSG >= _DBG_VERBOSE
1072 size_t available_space = 0;
1073 m_sbuf.GetWriteRemaining(&available_space);
1076 LOG(LS_INFO) << "[cwnd: " << m_cwnd
1077 << " nWindow: " << nWindow
1078 << " nInFlight: " << nInFlight
1079 << " nAvailable: " << nAvailable
1080 << " nQueued: " << snd_buffered
1081 << " nEmpty: " << available_space
1082 << " ssthresh: " << m_ssthresh << "]";
1086 if (nAvailable == 0) {
1087 if (sflags == sfNone)
1090 // If this is an immediate ack, or the second delayed ack
1091 if ((sflags == sfImmediateAck) || m_t_ack) {
1092 packet(m_snd_nxt, 0, 0, 0);
1099 // Nagle's algorithm.
1100 // If there is data already in-flight, and we haven't a full segment of
1101 // data ready to send then hold off until we get more to send, or the
1102 // in-flight data is acknowledged.
1103 if (m_use_nagling && (m_snd_nxt > m_snd_una) && (nAvailable < m_mss)) {
1107 // Find the next segment to transmit
1108 SList::iterator it = m_slist.begin();
1109 while (it->xmit > 0) {
1111 ASSERT(it != m_slist.end());
1113 SList::iterator seg = it;
1115 // If the segment is too large, break it into two
1116 if (seg->len > nAvailable) {
1117 SSegment subseg(seg->seq + nAvailable, seg->len - nAvailable, seg->bCtrl);
1118 seg->len = nAvailable;
1119 m_slist.insert(++it, subseg);
1122 if (!transmit(seg, now)) {
1123 LOG_F(LS_VERBOSE) << "transmit failed";
1124 // TODO: consider closing socket
1133 PseudoTcp::closedown(uint32 err) {
1134 LOG(LS_INFO) << "State: TCP_CLOSED";
1135 m_state = TCP_CLOSED;
1137 m_notify->OnTcpClosed(this, err);
1139 //notify(evClose, err);
1143 PseudoTcp::adjustMTU() {
1144 // Determine our current mss level, so that we can adjust appropriately later
1145 for (m_msslevel = 0; PACKET_MAXIMUMS[m_msslevel + 1] > 0; ++m_msslevel) {
1146 if (static_cast<uint16>(PACKET_MAXIMUMS[m_msslevel]) <= m_mtu_advise) {
1150 m_mss = m_mtu_advise - PACKET_OVERHEAD;
1151 // !?! Should we reset m_largest here?
1152 #if _DEBUGMSG >= _DBG_NORMAL
1153 LOG(LS_INFO) << "Adjusting mss to " << m_mss << " bytes";
1155 // Enforce minimums on ssthresh and cwnd
1156 m_ssthresh = rtc::_max(m_ssthresh, 2 * m_mss);
1157 m_cwnd = rtc::_max(m_cwnd, m_mss);
1161 PseudoTcp::isReceiveBufferFull() const {
1162 size_t available_space = 0;
1163 m_rbuf.GetWriteRemaining(&available_space);
1164 return !available_space;
1168 PseudoTcp::disableWindowScale() {
1169 m_support_wnd_scale = false;
1173 PseudoTcp::queueConnectMessage() {
1174 rtc::ByteBuffer buf(rtc::ByteBuffer::ORDER_NETWORK);
1176 buf.WriteUInt8(CTL_CONNECT);
1177 if (m_support_wnd_scale) {
1178 buf.WriteUInt8(TCP_OPT_WND_SCALE);
1180 buf.WriteUInt8(m_rwnd_scale);
1182 m_snd_wnd = static_cast<uint32>(buf.Length());
1183 queue(buf.Data(), static_cast<uint32>(buf.Length()), true);
1187 PseudoTcp::parseOptions(const char* data, uint32 len) {
1188 std::set<uint8> options_specified;
1190 // See http://www.freesoft.org/CIE/Course/Section4/8.htm for
1191 // parsing the options list.
1192 rtc::ByteBuffer buf(data, len);
1193 while (buf.Length()) {
1194 uint8 kind = TCP_OPT_EOL;
1195 buf.ReadUInt8(&kind);
1197 if (kind == TCP_OPT_EOL) {
1198 // End of option list.
1200 } else if (kind == TCP_OPT_NOOP) {
1205 // Length of this option.
1209 buf.ReadUInt8(&opt_len);
1211 // Content of this option.
1212 if (opt_len <= buf.Length()) {
1213 applyOption(kind, buf.Data(), opt_len);
1214 buf.Consume(opt_len);
1216 LOG(LS_ERROR) << "Invalid option length received.";
1219 options_specified.insert(kind);
1222 if (options_specified.find(TCP_OPT_WND_SCALE) == options_specified.end()) {
1223 LOG(LS_WARNING) << "Peer doesn't support window scaling";
1225 if (m_rwnd_scale > 0) {
1226 // Peer doesn't support TCP options and window scaling.
1227 // Revert receive buffer size to default value.
1228 resizeReceiveBuffer(DEFAULT_RCV_BUF_SIZE);
1235 PseudoTcp::applyOption(char kind, const char* data, uint32 len) {
1236 if (kind == TCP_OPT_MSS) {
1237 LOG(LS_WARNING) << "Peer specified MSS option which is not supported.";
1239 } else if (kind == TCP_OPT_WND_SCALE) {
1240 // Window scale factor.
1241 // http://www.ietf.org/rfc/rfc1323.txt
1243 LOG_F(WARNING) << "Invalid window scale option received.";
1246 applyWindowScaleOption(data[0]);
1251 PseudoTcp::applyWindowScaleOption(uint8 scale_factor) {
1252 m_swnd_scale = scale_factor;
1256 PseudoTcp::resizeSendBuffer(uint32 new_size) {
1257 m_sbuf_len = new_size;
1258 m_sbuf.SetCapacity(new_size);
1262 PseudoTcp::resizeReceiveBuffer(uint32 new_size) {
1263 uint8 scale_factor = 0;
1265 // Determine the scale factor such that the scaled window size can fit
1266 // in a 16-bit unsigned integer.
1267 while (new_size > 0xFFFF) {
1272 // Determine the proper size of the buffer.
1273 new_size <<= scale_factor;
1274 bool result = m_rbuf.SetCapacity(new_size);
1276 // Make sure the new buffer is large enough to contain data in the old
1277 // buffer. This should always be true because this method is called either
1278 // before connection is established or when peers are exchanging connect
1282 m_rbuf_len = new_size;
1283 m_rwnd_scale = scale_factor;
1284 m_ssthresh = new_size;
1286 size_t available_space = 0;
1287 m_rbuf.GetWriteRemaining(&available_space);
1288 m_rcv_wnd = static_cast<uint32>(available_space);
1291 } // namespace cricket