/** @file
TCP output process routines.
Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "TcpMain.h"
0, // TCP_CLOSED
0, // TCP_LISTEN
TCP_FLG_SYN, // TCP_SYN_SENT
TCP_FLG_ACK, // TCP_ESTABLISHED
TCP_FLG_ACK, // TCP_FIN_WAIT_2
TCP_FLG_ACK, // TCP_TIME_WAIT
TCP_FLG_ACK, // TCP_CLOSE_WAIT
};
/**
Compute the sequence space left in the old receive window.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@return The sequence space left in the old receive window.
**/
)
{
OldWin = 0;
OldWin = TCP_SUB_SEQ (
);
}
return OldWin;
}
/**
Compute the current receive window.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@return The size of the current receive window, in bytes.
**/
)
{
Increase = 0;
}
//
// Receiver's SWS: don't advertise a bigger window
// unless it can be increased by at least one Mss or
// half of the receive buffer.
//
return Win;
}
return OldWin;
}
/**
Compute the value to fill in the window size field of the outgoing segment.
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Syn The flag to indicate whether the outgoing segment
is a SYN segment.
@return The value of the local receive window size used to fill the outgoing segment.
**/
)
{
//
// RFC requires that initial window not be scaled
//
if (Syn) {
} else {
}
}
/**
Get the maximum SndNxt.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@return The sequence number of the maximum SndNxt.
**/
)
{
}
}
/**
Compute how much data to send.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Force If TRUE, to ignore the sender's SWS avoidance algorithm and send
out data by force.
@return The length of the data can be sent. If 0, no data can be sent.
**/
)
{
//
// TCP should NOT send data beyond the send window
// and congestion window. The right edge of send
// window is defined as SND.WL2 + SND.WND. The right
// edge of congestion window is defined as SND.UNA +
// CWND.
//
Win = 0;
}
}
//
// The data to send contains two parts: the data on the
// socket send queue, and the data on the TCB's send
// buffer. The later can be non-zero if the peer shrinks
// its advertised window.
//
}
return Len;
}
goto SetPersistTimer;
}
//
// Sender's SWS avoidance: Don't send a small segment unless
// a)A full-sized segment can be sent,
// b)At least one-half of the maximum sized windows that
// the other end has ever advertised.
// c)It can send everything it has, and either it isn't
// expecting an ACK, or the Nagle algorithm is disabled.
//
return Len;
}
) {
return Len;
}
//
// RFC1122 suggests to set a timer when SWSA forbids TCP
// sending more data, and combines it with a probe timer.
//
DEBUG (
"TcpDataToSend: enter persistent state for TCB %p\n",
Tcb)
);
if (!Tcb->ProbeTimerOn) {
}
}
return 0;
}
/**
Build the TCP header of the TCP segment and transmit the segment by IP.
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Nbuf Pointer to the buffer containing the segment to be
sent out.
@retval 0 The segment was sent out successfully.
@retval -1 An error condition occurred.
**/
)
{
if (Syn) {
} else {
}
Nbuf,
sizeof (TCP_HEAD),
);
//
// Check whether to set the PSH flag.
//
if (DataLen != 0) {
) {
}
}
//
// Check whether to set the URG flag and the urgent pointer.
//
} else {
0xffff
);
}
}
//
// Update the TCP session's control information.
//
if (Syn) {
}
//
// Clear the delayedack flag.
//
Tcb->DelayedAck = 0;
}
/**
Get a segment from the Tcb's SndQue.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Seq The sequence number of the segment.
@param[in] Len The maximum length of the segment.
@return Pointer to the segment. If NULL, some error occurred.
**/
NET_BUF *
)
{
//
// Find the segment that contains the Seq.
//
break;
}
}
return NULL;
}
//
// Return the buffer if it can be returned without
// adjustment:
//
) {
NET_GET_REF (Node);
return Node;
}
//
// Create a new buffer and copy data there.
//
return NULL;
}
}
//
// If SYN is set and out of the range, clear the flag.
// Becuase the sequence of the first byte is SEG.SEQ+1,
// adjust Offset by -1. If SYN is in the range, copy
// one byte less.
//
Offset--;
} else {
CopyLen--;
}
}
//
// If FIN is set and in the range, copy one byte less,
// and if it is out of the range, clear the flag.
//
CopyLen--;
} else {
}
}
//
// Copy data to the segment
//
if (CopyLen != 0) {
goto OnError;
}
}
return Nbuf;
NetbufFree (Nbuf);
return NULL;
}
/**
Get a segment from the Tcb's socket buffer.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Seq The sequence number of the segment.
@param[in] Len The maximum length of the segment.
@return Pointer to the segment. If NULL, some error occurred.
**/
NET_BUF *
)
{
DEBUG (
"TcpGetSegmentSock: failed to allocate a netbuf for TCB %p\n",
Tcb)
);
return NULL;
}
DataGet = 0;
if (Len != 0) {
//
// copy data to the segment.
//
}
NET_GET_REF (Nbuf);
if (DataGet != 0) {
}
return Nbuf;
}
/**
Get a segment starting from sequence Seq of a maximum
length of Len.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Seq The sequence number of the segment.
@param[in] Len The maximum length of the segment.
@return Pointer to the segment. If NULL, some error occurred.
**/
NET_BUF *
)
{
//
// Compare the SndNxt with the max sequence number sent.
//
} else {
}
return Nbuf;
}
/**
Retransmit the segment from sequence Seq.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Seq The sequence number of the segment to be retransmitted.
@retval 0 Retransmission succeeded.
@retval -1 Error condition occurred.
**/
)
{
//
// Compute the maxium length of retransmission. It is
// limited by three factors:
// 1. Less than SndMss
// 2. Must in the current send window
// 3. Will not change the boundaries of queued segments.
//
DEBUG (
"TcpRetransmit: retransmission cancelled because send window too small for TCB %p\n",
Tcb)
);
return 0;
}
return -1;
}
goto OnError;
}
//
// The retransmitted buffer may be on the SndQue,
// trim TCP head because all the buffers on SndQue
// are headless.
//
NetbufFree (Nbuf);
return 0;
NetbufFree (Nbuf);
}
return -1;
}
/**
Verify that all the segments in SndQue are in good shape.
@param[in] Head Pointer to the head node of the SndQue.
@retval 0 At least one segment is broken.
@retval 1 All segments in the specific queue are in good shape.
**/
)
{
if (IsListEmpty (Head)) {
return 1;
}
//
// Initialize the Seq.
//
if (TcpVerifySegment (Nbuf) == 0) {
return 0;
}
//
// All the node in the SndQue should has:
// SEG.SEQ = LAST_SEG.END
//
return 0;
}
}
return 1;
}
/**
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
@param[in] Force If TRUE, ignore the sender's SWS avoidance algorithm
and send out data by force.
@return The number of bytes sent.
**/
)
{
Sent = 0;
return 0;
}
do {
//
// Compute how much data can be sent
//
if ((Flag & TCP_FLG_SYN) != 0) {
Len = 0;
}
//
// Only send a segment without data if SYN or
// FIN is set.
//
return Sent;
}
DEBUG (
"TcpToSendData: failed to get a segment for TCB %p\n",
Tcb)
);
goto OnError;
}
//
// Set the TcpSeg in Nbuf.
//
End++;
}
if ((Flag & TCP_FLG_FIN) != 0) {
//
// Send FIN if all data is sent, and FIN is
// in the window
//
) {
DEBUG (
"TcpToSendData: send FIN to peer for TCB %p in state %s\n",
Tcb,
);
End++;
} else {
}
}
//
// Don't send an empty segment here.
//
DEBUG (
"TcpToSendData: created a empty segment for TCB %p, free it now\n",
Tcb)
);
NetbufFree (Nbuf);
return Sent;
}
if ((Flag & TCP_FLG_FIN) != 0) {
}
goto OnError;
}
//
// All the buffers in the SndQue are headless.
//
NetbufFree (Nbuf);
//
// Update the status in TCB.
//
Tcb->DelayedAck = 0;
if ((Flag & TCP_FLG_FIN) != 0) {
}
}
}
//
// Enable RTT measurement only if not in retransmit.
// Karn's algorithm requires not to update RTT when in loss.
//
DEBUG (
"TcpToSendData: set RTT measure sequence %d for TCB %p\n",
Seq,
Tcb)
);
Tcb->RttMeasure = 0;
}
return Sent;
NetbufFree (Nbuf);
}
return Sent;
}
/**
Send an ACK immediately.
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
**/
)
{
return;
}
Tcb->DelayedAck = 0;
}
NetbufFree (Nbuf);
}
/**
Send a zero probe segment. It can be used by keepalive and zero window probe.
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
@retval 0 The zero probe segment was sent out successfully.
@retval other An error condition occurred.
**/
)
{
return -1;
}
//
// SndNxt-1 is out of window. The peer should respond
// with an ACK.
//
NetbufFree (Nbuf);
return Result;
}
/**
Check whether to send an ACK or delayed ACK.
@param[in, out] Tcb Pointer to the TCP_CB of this TCP instance.
**/
)
{
//
// Generally, TCP should send a delayed ACK unless:
// 1. ACK at least every other FULL sized segment received.
// 2. Packets received out of order.
// 3. Receiving window is open.
//
TcpSendAck (Tcb);
return;
}
TcpSendAck (Tcb);
return;
}
DEBUG (
"TcpToSendAck: scheduled a delayed ACK for TCB %p\n",
Tcb)
);
//
// Schedule a delayed ACK.
//
Tcb->DelayedAck++;
}
/**
Send a RESET segment in response to the segment received.
@param[in] Tcb Pointer to the TCP_CB of this TCP instance. May be NULL.
@param[in] Head TCP header of the segment that triggers the reset.
@param[in] Len Length of the segment that triggers the reset.
@param[in] Local Local IP address.
@param[in] Remote Remote peer's IP address.
@param[in] Version IP_VERSION_4 indicates TCP is running on IP4 stack,
IP_VERSION_6 indicates TCP is running on IP6 stack.
@retval 0 A reset was sent or there is no need to send it.
@retval -1 No reset is sent.
**/
)
{
//
// Don't respond to a Reset with reset.
//
return 0;
}
return -1;
}
Nbuf,
sizeof (TCP_HEAD),
);
//
// is associated with it, otherwise derive from the Tcb.
//
} else {
}
} else {
}
if (Version == IP_VERSION_4) {
} else {
}
NetbufFree (Nbuf);
return 0;
}
/**
Verify that the segment is in good shape.
@param[in] Nbuf The buffer that contains the segment to be checked.
@retval 0 The segment is broken.
@retval 1 The segment is in good shape.
**/
)
{
return 1;
}
return 0;
}
}
Len++;
}
Len++;
}
return 0;
}
return 1;
}