author | Josh Pelkey <jpelkey@gatech.edu> |
Fri, 13 May 2011 14:55:24 -0400 | |
changeset 7176 | 9f2663992e99 |
parent 6834 | 036f9a0b9899 |
child 7385 | 10beb0e53130 |
permissions | -rw-r--r-- |
6694 | 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
|
3 |
* Copyright (c) 2010 Adrian Sai-wah Tam |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License version 2 as |
|
7 |
* published by the Free Software Foundation; |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
* |
|
18 |
* Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com> |
|
19 |
*/ |
|
20 |
#ifndef TCP_SOCKET_BASE_H |
|
21 |
#define TCP_SOCKET_BASE_H |
|
22 |
||
23 |
#include <stdint.h> |
|
24 |
#include <queue> |
|
25 |
#include "ns3/callback.h" |
|
26 |
#include "ns3/traced-value.h" |
|
27 |
#include "ns3/tcp-socket.h" |
|
28 |
#include "ns3/ptr.h" |
|
29 |
#include "ns3/ipv4-address.h" |
|
30 |
#include "ns3/ipv4-header.h" |
|
31 |
#include "ns3/ipv4-interface.h" |
|
32 |
#include "ns3/event-id.h" |
|
33 |
#include "tcp-tx-buffer.h" |
|
34 |
#include "tcp-rx-buffer.h" |
|
35 |
#include "rtt-estimator.h" |
|
36 |
||
37 |
namespace ns3 { |
|
38 |
||
39 |
class Ipv4EndPoint; |
|
40 |
class Node; |
|
41 |
class Packet; |
|
42 |
class TcpL4Protocol; |
|
43 |
class TcpHeader; |
|
44 |
||
45 |
/** |
|
46 |
* \ingroup socket |
|
47 |
* \ingroup tcp |
|
48 |
* |
|
49 |
* \brief A base class for implementation of a stream socket using TCP. |
|
50 |
* |
|
51 |
* This class contains the essential components of TCP, as well as a sockets |
|
52 |
* interface for upper layers to call. This serves as a base for other TCP |
|
53 |
* functions where the sliding window mechanism is handled here. This class |
|
54 |
* provides connection orientation and sliding window flow control. Part of |
|
55 |
* this class is modified from the original NS-3 TCP socket implementation |
|
56 |
* (TcpSocketImpl) by Raj Bhattacharjea. |
|
57 |
*/ |
|
58 |
class TcpSocketBase : public TcpSocket |
|
59 |
{ |
|
60 |
public: |
|
61 |
static TypeId GetTypeId (void); |
|
62 |
/** |
|
63 |
* Create an unbound TCP socket |
|
64 |
*/ |
|
65 |
TcpSocketBase (void); |
|
66 |
||
67 |
/** |
|
68 |
* Clone a TCP socket, for use upon receiving a connection request in LISTEN state |
|
69 |
*/ |
|
70 |
TcpSocketBase (const TcpSocketBase& sock); |
|
71 |
virtual ~TcpSocketBase (void); |
|
72 |
||
73 |
// Set associated Node, TcpL4Protocol, RttEstimator to this socket |
|
74 |
virtual void SetNode (Ptr<Node> node); |
|
75 |
virtual void SetTcp (Ptr<TcpL4Protocol> tcp); |
|
76 |
virtual void SetRtt (Ptr<RttEstimator> rtt); |
|
77 |
||
78 |
// Necessary implementations of null functions from ns3::Socket |
|
79 |
virtual enum SocketErrno GetErrno (void) const; // returns m_errno |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
80 |
virtual enum SocketType GetSocketType (void) const; // returns socket type |
6694 | 81 |
virtual Ptr<Node> GetNode (void) const; // returns m_node |
82 |
virtual int Bind (void); // Bind a socket by setting up endpoint in TcpL4Protocol |
|
83 |
virtual int Bind (const Address &address); // ... endpoint of specific addr or port |
|
84 |
virtual int Connect (const Address &address); // Setup endpoint and call ProcessAction() to connect |
|
85 |
virtual int Listen (void); // Verify the socket is in a correct state and call ProcessAction() to listen |
|
86 |
virtual int Close (void); // Close by app: Kill socket upon tx buffer emptied |
|
87 |
virtual int ShutdownSend (void); // Assert the m_shutdownSend flag to prevent send to network |
|
88 |
virtual int ShutdownRecv (void); // Assert the m_shutdownRecv flag to prevent forward to app |
|
89 |
virtual int Send (Ptr<Packet> p, uint32_t flags); // Call by app to send data to network |
|
90 |
virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress); // Same as Send(), toAddress is insignificant |
|
91 |
virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags); // Return a packet to be forwarded to app |
|
92 |
virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, Address &fromAddress); // ... and write the remote address at fromAddress |
|
93 |
virtual uint32_t GetTxAvailable (void) const; // Available Tx buffer size |
|
94 |
virtual uint32_t GetRxAvailable (void) const; // Available-to-read data size, i.e. value of m_rxAvailable |
|
95 |
virtual int GetSockName (Address &address) const; // Return local addr:port in address |
|
96 |
virtual void BindToNetDevice (Ptr<NetDevice> netdevice); // NetDevice with my m_endPoint |
|
97 |
||
98 |
protected: |
|
99 |
// Implementing ns3::TcpSocket -- Attribute get/set |
|
100 |
virtual void SetSndBufSize (uint32_t size); |
|
101 |
virtual uint32_t GetSndBufSize (void) const; |
|
102 |
virtual void SetRcvBufSize (uint32_t size); |
|
103 |
virtual uint32_t GetRcvBufSize (void) const; |
|
104 |
virtual void SetSegSize (uint32_t size); |
|
105 |
virtual uint32_t GetSegSize (void) const; |
|
106 |
virtual void SetSSThresh (uint32_t threshold) = 0; |
|
107 |
virtual uint32_t GetSSThresh (void) const = 0; |
|
108 |
virtual void SetInitialCwnd (uint32_t cwnd) = 0; |
|
109 |
virtual uint32_t GetInitialCwnd (void) const = 0; |
|
110 |
virtual void SetConnTimeout (Time timeout); |
|
111 |
virtual Time GetConnTimeout (void) const; |
|
112 |
virtual void SetConnCount (uint32_t count); |
|
113 |
virtual uint32_t GetConnCount (void) const; |
|
114 |
virtual void SetDelAckTimeout (Time timeout); |
|
115 |
virtual Time GetDelAckTimeout (void) const; |
|
116 |
virtual void SetDelAckMaxCount (uint32_t count); |
|
117 |
virtual uint32_t GetDelAckMaxCount (void) const; |
|
118 |
virtual void SetPersistTimeout (Time timeout); |
|
119 |
virtual Time GetPersistTimeout (void) const; |
|
120 |
virtual bool SetAllowBroadcast (bool allowBroadcast); |
|
121 |
virtual bool GetAllowBroadcast () const; |
|
122 |
||
123 |
// Helper functions: Connection set up |
|
124 |
int SetupCallback (void); // Common part of the two Bind(), i.e. set callback and remembering local addr:port |
|
125 |
int DoConnect (void); // Sending a SYN packet to make a connection if the state allows |
|
126 |
void ConnectionSucceeded (void); // Schedule-friendly wrapper for Socket::NotifyConnectionSucceeded() |
|
127 |
int SetupEndpoint (void); // Configure m_endpoint for local addr for given remote addr |
|
128 |
void CompleteFork (Ptr<Packet>, const TcpHeader&, const Address& fromAddress, const Address& toAdress); |
|
129 |
||
130 |
// Helper functions: Transfer operation |
|
131 |
void ForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface); //Get a pkt from L3 |
|
132 |
bool SendPendingData (bool withAck = false); // Send as much as the window allows |
|
133 |
void SendEmptyPacket (uint8_t flags); // Send a empty packet that carries a flag, e.g. ACK |
|
134 |
void SendRST (void); // Send reset and tear down this socket |
|
135 |
bool OutOfRange (SequenceNumber32 s) const; // Check if a sequence number is within rx window |
|
136 |
||
137 |
// Helper functions: Connection close |
|
138 |
int DoClose (void); // Close a socket by sending RST, FIN, or FIN+ACK, depend on the current state |
|
139 |
void CloseAndNotify (void); // To CLOSED state, notify upper layer, and deallocate end point |
|
140 |
void Destroy (void); // Kill this socket by zeroing its attributes |
|
141 |
void DeallocateEndPoint (void); // Deallocate m_endPoint |
|
142 |
void PeerClose (Ptr<Packet>, const TcpHeader&); // Received a FIN from peer, notify rx buffer |
|
143 |
void DoPeerClose (void); // FIN is in sequence, notify app and respond with a FIN |
|
144 |
void CancelAllTimers (void); // Cancel all timer when endpoint is deleted |
|
145 |
||
146 |
// State transition functions |
|
147 |
void ProcessEstablished (Ptr<Packet>, const TcpHeader&); // Received a packet upon ESTABLISHED state |
|
148 |
void ProcessListen (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Process the newly received ACK |
|
149 |
void ProcessSynSent (Ptr<Packet>, const TcpHeader&); // Received a packet upon SYN_SENT |
|
150 |
void ProcessSynRcvd (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Received a packet upon SYN_RCVD |
|
151 |
void ProcessWait (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSE_WAIT, FIN_WAIT_1, FIN_WAIT_2 |
|
152 |
void ProcessClosing (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSING |
|
153 |
void ProcessLastAck (Ptr<Packet>, const TcpHeader&); // Received a packet upon LAST_ACK |
|
154 |
||
155 |
// Window management |
|
156 |
virtual uint32_t UnAckDataCount (void); // Return count of number of unacked bytes |
|
157 |
virtual uint32_t BytesInFlight (void); // Return total bytes in flight |
|
158 |
virtual uint32_t Window (void); // Return the max possible number of unacked bytes |
|
159 |
virtual uint32_t AvailableWindow (void); // Return unfilled portion of window |
|
160 |
virtual uint16_t AdvertisedWindowSize (void); // The amount of Rx window announced to the peer |
|
161 |
||
162 |
// Manage data tx/rx |
|
163 |
virtual Ptr<TcpSocketBase> Fork (void) = 0; // Call CopyObject<> to clone me |
|
164 |
virtual void ReceivedAck (Ptr<Packet>, const TcpHeader&); // Received an ACK packet |
|
165 |
virtual void ReceivedData (Ptr<Packet>, const TcpHeader&); // Recv of a data, put into buffer, call L7 to get it if necessary |
|
166 |
virtual void EstimateRtt (const TcpHeader&); // RTT accounting |
|
167 |
virtual void NewAck (SequenceNumber32 const& seq); // Update buffers w.r.t. ACK |
|
168 |
virtual void DupAck (const TcpHeader& t, uint32_t count) = 0; // Received dupack |
|
169 |
virtual void ReTxTimeout (void); // Call Retransmit() upon RTO event |
|
170 |
virtual void Retransmit (void); // Halving cwnd and call DoRetransmit() |
|
171 |
virtual void DelAckTimeout (void); // Action upon delay ACK timeout, i.e. send an ACK |
|
172 |
virtual void LastAckTimeout (void); // Timeout at LAST_ACK, close the connection |
|
173 |
virtual void PersistTimeout (void); // Send 1 byte probe to get an updated window size |
|
174 |
virtual void DoRetransmit (void); // Retransmit the oldest packet |
|
175 |
||
176 |
protected: |
|
177 |
// Counters and events |
|
178 |
EventId m_retxEvent; //< Retransmission event |
|
179 |
EventId m_lastAckEvent; //< Last ACK timeout event |
|
180 |
EventId m_delAckEvent; //< Delayed ACK timeout event |
|
181 |
EventId m_persistEvent; //< Persist event: Send 1 byte to probe for a non-zero Rx window |
|
182 |
uint32_t m_dupAckCount; //< Dupack counter |
|
183 |
uint32_t m_delAckCount; //< Delayed ACK counter |
|
184 |
uint32_t m_delAckMaxCount; //< Number of packet to fire an ACK before delay timeout |
|
185 |
uint32_t m_cnCount; //< Count of remaining connection retries |
|
186 |
TracedValue<Time> m_rto; //< Retransmit timeout |
|
187 |
TracedValue<Time> m_lastRtt; //< Last RTT sample collected |
|
188 |
Time m_delAckTimeout; //< Time to delay an ACK |
|
189 |
Time m_persistTimeout; //< Time between sending 1-byte probes |
|
190 |
Time m_cnTimeout; //< Timeout for connection retry |
|
191 |
||
192 |
// Connections to other layers of TCP/IP |
|
193 |
Ipv4EndPoint* m_endPoint; |
|
194 |
Ptr<Node> m_node; |
|
195 |
Ptr<TcpL4Protocol> m_tcp; |
|
196 |
||
197 |
// Round trip time estimation |
|
198 |
Ptr<RttEstimator> m_rtt; |
|
199 |
||
200 |
// Rx and Tx buffer management |
|
201 |
TracedValue<SequenceNumber32> m_nextTxSequence; //< Next seqnum to be sent (SND.NXT), ReTx pushes it back |
|
202 |
TracedValue<SequenceNumber32> m_highTxMark; //< Highest seqno ever sent, regardless of ReTx |
|
203 |
TcpRxBuffer m_rxBuffer; //< Rx buffer (reordering buffer) |
|
204 |
TcpTxBuffer m_txBuffer; //< Tx buffer |
|
205 |
||
206 |
// State-related attributes |
|
207 |
TracedValue<TcpStates_t> m_state; //< TCP state |
|
208 |
enum SocketErrno m_errno; //< Socket error code |
|
209 |
bool m_closeNotified; //< Told app to close socket |
|
210 |
bool m_closeOnEmpty; //< Close socket upon tx buffer emptied |
|
211 |
bool m_shutdownSend; //< Send no longer allowed |
|
212 |
bool m_shutdownRecv; //< Receive no longer allowed |
|
213 |
bool m_connected; //< Connection established |
|
214 |
||
215 |
// Window management |
|
216 |
uint32_t m_segmentSize; //< Segment size |
|
217 |
TracedValue<uint32_t> m_rWnd; //< Flow control window at remote side |
|
218 |
}; |
|
219 |
||
220 |
} // namespace ns3 |
|
221 |
||
222 |
#endif /* TCP_SOCKET_BASE_H */ |