henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_PSEUDOTCP_H_ |
| 12 | #define P2P_BASE_PSEUDOTCP_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/stream.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | |
| 18 | namespace cricket { |
| 19 | |
| 20 | ////////////////////////////////////////////////////////////////////// |
| 21 | // IPseudoTcpNotify |
| 22 | ////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | class PseudoTcp; |
| 25 | |
| 26 | class IPseudoTcpNotify { |
| 27 | public: |
| 28 | // Notification of tcp events |
| 29 | virtual void OnTcpOpen(PseudoTcp* tcp) = 0; |
| 30 | virtual void OnTcpReadable(PseudoTcp* tcp) = 0; |
| 31 | virtual void OnTcpWriteable(PseudoTcp* tcp) = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 32 | virtual void OnTcpClosed(PseudoTcp* tcp, uint32_t error) = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 33 | |
| 34 | // Write the packet onto the network |
| 35 | enum WriteResult { WR_SUCCESS, WR_TOO_LARGE, WR_FAIL }; |
| 36 | virtual WriteResult TcpWritePacket(PseudoTcp* tcp, |
| 37 | const char* buffer, size_t len) = 0; |
| 38 | |
| 39 | protected: |
| 40 | virtual ~IPseudoTcpNotify() {} |
| 41 | }; |
| 42 | |
| 43 | ////////////////////////////////////////////////////////////////////// |
| 44 | // PseudoTcp |
| 45 | ////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | class PseudoTcp { |
| 48 | public: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | static uint32_t Now(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 51 | PseudoTcp(IPseudoTcpNotify* notify, uint32_t conv); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 52 | virtual ~PseudoTcp(); |
| 53 | |
| 54 | int Connect(); |
| 55 | int Recv(char* buffer, size_t len); |
| 56 | int Send(const char* buffer, size_t len); |
| 57 | void Close(bool force); |
| 58 | int GetError(); |
| 59 | |
| 60 | enum TcpState { |
| 61 | TCP_LISTEN, TCP_SYN_SENT, TCP_SYN_RECEIVED, TCP_ESTABLISHED, TCP_CLOSED |
| 62 | }; |
| 63 | TcpState State() const { return m_state; } |
| 64 | |
| 65 | // Call this when the PMTU changes. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 66 | void NotifyMTU(uint16_t mtu); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | |
| 68 | // Call this based on timeout value returned from GetNextClock. |
| 69 | // It's ok to call this too frequently. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 70 | void NotifyClock(uint32_t now); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | |
| 72 | // Call this whenever a packet arrives. |
| 73 | // Returns true if the packet was processed successfully. |
| 74 | bool NotifyPacket(const char * buffer, size_t len); |
| 75 | |
| 76 | // Call this to determine the next time NotifyClock should be called. |
| 77 | // Returns false if the socket is ready to be destroyed. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 78 | bool GetNextClock(uint32_t now, long& timeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | |
| 80 | // Call these to get/set option values to tailor this PseudoTcp |
| 81 | // instance's behaviour for the kind of data it will carry. |
| 82 | // If an unrecognized option is set or got, an assertion will fire. |
| 83 | // |
| 84 | // Setting options for OPT_RCVBUF or OPT_SNDBUF after Connect() is called |
| 85 | // will result in an assertion. |
| 86 | enum Option { |
| 87 | OPT_NODELAY, // Whether to enable Nagle's algorithm (0 == off) |
| 88 | OPT_ACKDELAY, // The Delayed ACK timeout (0 == off). |
| 89 | OPT_RCVBUF, // Set the receive buffer size, in bytes. |
| 90 | OPT_SNDBUF, // Set the send buffer size, in bytes. |
| 91 | }; |
| 92 | void GetOption(Option opt, int* value); |
| 93 | void SetOption(Option opt, int value); |
| 94 | |
| 95 | // Returns current congestion window in bytes. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 96 | uint32_t GetCongestionWindow() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | |
| 98 | // Returns amount of data in bytes that has been sent, but haven't |
| 99 | // been acknowledged. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | uint32_t GetBytesInFlight() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | |
| 102 | // Returns number of bytes that were written in buffer and haven't |
| 103 | // been sent. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 104 | uint32_t GetBytesBufferedNotSent() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 105 | |
| 106 | // Returns current round-trip time estimate in milliseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 107 | uint32_t GetRoundTripTimeEstimateMs() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | enum SendFlags { sfNone, sfDelayedAck, sfImmediateAck }; |
| 111 | |
| 112 | struct Segment { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 113 | uint32_t conv, seq, ack; |
| 114 | uint8_t flags; |
| 115 | uint16_t wnd; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | const char * data; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 117 | uint32_t len; |
| 118 | uint32_t tsval, tsecr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | struct SSegment { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 122 | SSegment(uint32_t s, uint32_t l, bool c) |
| 123 | : seq(s), len(l), /*tstamp(0),*/ xmit(0), bCtrl(c) {} |
| 124 | uint32_t seq, len; |
| 125 | // uint32_t tstamp; |
| 126 | uint8_t xmit; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 127 | bool bCtrl; |
| 128 | }; |
| 129 | typedef std::list<SSegment> SList; |
| 130 | |
| 131 | struct RSegment { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 132 | uint32_t seq, len; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 135 | uint32_t queue(const char* data, uint32_t len, bool bCtrl); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 136 | |
| 137 | // Creates a packet and submits it to the network. This method can either |
| 138 | // send payload or just an ACK packet. |
| 139 | // |
| 140 | // |seq| is the sequence number of this packet. |
| 141 | // |flags| is the flags for sending this packet. |
| 142 | // |offset| is the offset to read from |m_sbuf|. |
| 143 | // |len| is the number of bytes to read from |m_sbuf| as payload. If this |
| 144 | // value is 0 then this is an ACK packet, otherwise this packet has payload. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 145 | IPseudoTcpNotify::WriteResult packet(uint32_t seq, |
| 146 | uint8_t flags, |
| 147 | uint32_t offset, |
| 148 | uint32_t len); |
| 149 | bool parse(const uint8_t* buffer, uint32_t size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 150 | |
| 151 | void attemptSend(SendFlags sflags = sfNone); |
| 152 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 153 | void closedown(uint32_t err = 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 155 | bool clock_check(uint32_t now, long& nTimeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 156 | |
| 157 | bool process(Segment& seg); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | bool transmit(const SList::iterator& seg, uint32_t now); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | |
| 160 | void adjustMTU(); |
| 161 | |
| 162 | protected: |
| 163 | // This method is used in test only to query receive buffer state. |
| 164 | bool isReceiveBufferFull() const; |
| 165 | |
| 166 | // This method is only used in tests, to disable window scaling |
| 167 | // support for testing backward compatibility. |
| 168 | void disableWindowScale(); |
| 169 | |
| 170 | private: |
| 171 | // Queue the connect message with TCP options. |
| 172 | void queueConnectMessage(); |
| 173 | |
| 174 | // Parse TCP options in the header. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 175 | void parseOptions(const char* data, uint32_t len); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | |
| 177 | // Apply a TCP option that has been read from the header. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | void applyOption(char kind, const char* data, uint32_t len); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | |
| 180 | // Apply window scale option. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 181 | void applyWindowScaleOption(uint8_t scale_factor); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | |
| 183 | // Resize the send buffer with |new_size| in bytes. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 184 | void resizeSendBuffer(uint32_t new_size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | |
| 186 | // Resize the receive buffer with |new_size| in bytes. This call adjusts |
| 187 | // window scale factor |m_swnd_scale| accordingly. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 188 | void resizeReceiveBuffer(uint32_t new_size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | |
| 190 | IPseudoTcpNotify* m_notify; |
| 191 | enum Shutdown { SD_NONE, SD_GRACEFUL, SD_FORCEFUL } m_shutdown; |
| 192 | int m_error; |
| 193 | |
| 194 | // TCB data |
| 195 | TcpState m_state; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 196 | uint32_t m_conv; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 197 | bool m_bReadEnable, m_bWriteEnable, m_bOutgoing; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 198 | uint32_t m_lasttraffic; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | |
| 200 | // Incoming data |
| 201 | typedef std::list<RSegment> RList; |
| 202 | RList m_rlist; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 203 | uint32_t m_rbuf_len, m_rcv_nxt, m_rcv_wnd, m_lastrecv; |
| 204 | uint8_t m_rwnd_scale; // Window scale factor. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 205 | rtc::FifoBuffer m_rbuf; |
| 206 | |
| 207 | // Outgoing data |
| 208 | SList m_slist; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 209 | uint32_t m_sbuf_len, m_snd_nxt, m_snd_wnd, m_lastsend, m_snd_una; |
| 210 | uint8_t m_swnd_scale; // Window scale factor. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 211 | rtc::FifoBuffer m_sbuf; |
| 212 | |
| 213 | // Maximum segment size, estimated protocol level, largest segment sent |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 214 | uint32_t m_mss, m_msslevel, m_largest, m_mtu_advise; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 215 | // Retransmit timer |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 216 | uint32_t m_rto_base; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | |
| 218 | // Timestamp tracking |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 219 | uint32_t m_ts_recent, m_ts_lastack; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 220 | |
| 221 | // Round-trip calculation |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 222 | uint32_t m_rx_rttvar, m_rx_srtt, m_rx_rto; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 223 | |
| 224 | // Congestion avoidance, Fast retransmit/recovery, Delayed ACKs |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 225 | uint32_t m_ssthresh, m_cwnd; |
| 226 | uint8_t m_dup_acks; |
| 227 | uint32_t m_recover; |
| 228 | uint32_t m_t_ack; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | |
| 230 | // Configuration options |
| 231 | bool m_use_nagling; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 232 | uint32_t m_ack_delay; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 233 | |
| 234 | // This is used by unit tests to test backward compatibility of |
| 235 | // PseudoTcp implementations that don't support window scaling. |
| 236 | bool m_support_wnd_scale; |
| 237 | }; |
| 238 | |
| 239 | } // namespace cricket |
| 240 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 241 | #endif // P2P_BASE_PSEUDOTCP_H_ |