tcp4output.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,320 行 · 第 1/2 页
C
1,320 行
/*++
Copyright (c) 2005 - 2006, Intel Corporation
All rights reserved. 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
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
Tcp4Output.c
Abstract:
TCP output process routines.
--*/
#include "Tcp4Main.h"
STATIC UINT8 mTcpOutFlag[] = {
0, // TCP_CLOSED
0, // TCP_LISTEN
TCP_FLG_SYN, // TCP_SYN_SENT
TCP_FLG_SYN | TCP_FLG_ACK, // TCP_SYN_RCVD
TCP_FLG_ACK, // TCP_ESTABLISHED
TCP_FLG_FIN | TCP_FLG_ACK, // TCP_FIN_WAIT_1
TCP_FLG_ACK, // TCP_FIN_WAIT_2
TCP_FLG_ACK | TCP_FLG_FIN, // TCP_CLOSING
TCP_FLG_ACK, // TCP_TIME_WAIT
TCP_FLG_ACK, // TCP_CLOSE_WAIT
TCP_FLG_FIN | TCP_FLG_ACK // TCP_LAST_ACK
};
UINT32
TcpRcvWinOld (
IN TCP_CB *Tcb
)
/*++
Routine Description:
Compute the sequence space left in the old receive window.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Returns:
The sequence space left in the old receive window.
--*/
{
UINT32 OldWin;
OldWin = 0;
if (TCP_SEQ_GT (Tcb->RcvWl2 + Tcb->RcvWnd, Tcb->RcvNxt)) {
OldWin = TCP_SUB_SEQ (
Tcb->RcvWl2 + Tcb->RcvWnd,
Tcb->RcvNxt
);
}
return OldWin;
}
UINT32
TcpRcvWinNow (
IN TCP_CB *Tcb
)
/*++
Routine Description:
Compute the current receive window.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Returns:
The size of the current receive window, in bytes.
--*/
{
SOCKET *Sk;
UINT32 Win;
UINT32 Increase;
UINT32 OldWin;
Sk = Tcb->Sk;
ASSERT (Sk);
OldWin = TcpRcvWinOld (Tcb);
Win = SockGetFreeSpace (Sk, SOCK_RCV_BUF);
Increase = 0;
if (Win > OldWin) {
Increase = Win - OldWin;
}
//
// 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.
//
if ((Increase > Tcb->SndMss) ||
(2 * Increase >= GET_RCV_BUFFSIZE (Sk))) {
return Win;
}
return OldWin;
}
STATIC
UINT16
TcpComputeWnd (
IN TCP_CB *Tcb,
IN BOOLEAN Syn
)
/*++
Routine Description:
Compute the value to fill in the window size field
of the outgoing segment.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Syn - The flag to indicate whether the outgoing
segment is a SYN segment.
Returns:
The value of the local receive window size used to fill the outing segment.
--*/
{
UINT32 Wnd;
//
// RFC requires that initial window not be scaled
//
if (Syn) {
Wnd = GET_RCV_BUFFSIZE (Tcb->Sk);
} else {
Wnd = TcpRcvWinNow (Tcb);
Tcb->RcvWnd = Wnd;
}
Wnd = NET_MIN (Wnd >> Tcb->RcvWndScale, 0xffff);
return NTOHS ((UINT16) Wnd);
}
TCP_SEQNO
TcpGetMaxSndNxt (
IN TCP_CB *Tcb
)
/*++
Routine Description:
Get the maximum SndNxt.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Returns:
The sequence number of the maximum SndNxt.
--*/
{
NET_LIST_ENTRY *Entry;
NET_BUF *Nbuf;
if (NetListIsEmpty (&Tcb->SndQue)) {
return Tcb->SndNxt;
}
Entry = Tcb->SndQue.BackLink;
Nbuf = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);
ASSERT (TCP_SEQ_GEQ (TCPSEG_NETBUF (Nbuf)->End, Tcb->SndNxt));
return TCPSEG_NETBUF (Nbuf)->End;
}
UINT32
TcpDataToSend (
IN TCP_CB *Tcb,
IN INTN Force
)
/*++
Routine Description:
Compute how much data to send.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Force - Whether to ignore the sender's SWS avoidance algorithm
and send out data by force.
Returns:
The length of the data can be sent, if 0, no data can be sent.
--*/
{
SOCKET *Sk;
UINT32 Win;
UINT32 Len;
UINT32 Left;
UINT32 Limit;
Sk = Tcb->Sk;
ASSERT (Sk);
//
// 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;
Limit = Tcb->SndWl2 + Tcb->SndWnd;
if (TCP_SEQ_GT (Limit, Tcb->SndUna + Tcb->CWnd)) {
Limit = Tcb->SndUna + Tcb->CWnd;
}
if (TCP_SEQ_GT (Limit, Tcb->SndNxt)) {
Win = TCP_SUB_SEQ (Limit, Tcb->SndNxt);
}
//
// 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.
//
Left = GET_SND_DATASIZE (Sk) +
TCP_SUB_SEQ (TcpGetMaxSndNxt (Tcb), Tcb->SndNxt);
Len = NET_MIN (Win, Left);
if (Len > Tcb->SndMss) {
Len = Tcb->SndMss;
}
if (Force || (Len == 0 && Left == 0)) {
return Len;
}
if (Len == 0 && Left != 0) {
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.
//
if ((Len == Tcb->SndMss) || (2 * Len >= Tcb->SndWndMax)) {
return Len;
}
if ((Len == Left) &&
((Tcb->SndNxt == Tcb->SndUna) ||
TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_NO_NAGLE))) {
return Len;
}
//
// RFC1122 suggests to set a timer when SWSA forbids TCP
// sending more data, and combine it with probe timer.
//
SetPersistTimer:
if (!TCP_TIMER_ON (Tcb->EnabledTimer, TCP_TIMER_REXMIT)) {
TCP4_DEBUG_WARN (
("TcpDataToSend: enter persistent state for TCB %x\n",
Tcb)
);
TcpSetProbeTimer (Tcb);
}
return 0;
}
STATIC
INTN
TcpTransmitSegment (
IN TCP_CB *Tcb,
IN NET_BUF *Nbuf
)
/*++
Routine Description:
Build the TCP header of the TCP segment and transmit the
segment by IP.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Nbuf - Pointer to the buffer containing the segment to be sent out.
Returns:
0 - The segment is sent out successfully.
other - Error condition occurred.
--*/
{
UINT16 Len;
TCP_HEAD *Head;
TCP_SEG *Seg;
BOOLEAN Syn;
UINT32 DataLen;
ASSERT (Nbuf && (Nbuf->Tcp == NULL) && TcpVerifySegment (Nbuf));
DataLen = Nbuf->TotalSize;
Seg = TCPSEG_NETBUF (Nbuf);
Syn = TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN);
if (Syn) {
Len = TcpSynBuildOption (Tcb, Nbuf);
} else {
Len = TcpBuildOption (Tcb, Nbuf);
}
ASSERT ((Len % 4 == 0) && (Len <= 40));
Len += sizeof (TCP_HEAD);
Head = (TCP_HEAD *) NetbufAllocSpace (
Nbuf,
sizeof (TCP_HEAD),
NET_BUF_HEAD
);
ASSERT (Head != NULL);
Nbuf->Tcp = Head;
Head->SrcPort = Tcb->LocalEnd.Port;
Head->DstPort = Tcb->RemoteEnd.Port;
Head->Seq = NTOHL (Seg->Seq);
Head->Ack = NTOHL (Tcb->RcvNxt);
Head->HeadLen = (UINT8) (Len >> 2);
Head->Res = 0;
Head->Wnd = TcpComputeWnd (Tcb, Syn);
Head->Checksum = 0;
//
// Check whether to set the PSH flag.
//
TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_PSH);
if (DataLen != 0) {
if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_PSH) &&
TCP_SEQ_BETWEEN (Seg->Seq, Tcb->SndPsh, Seg->End)) {
TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH);
TCP_CLEAR_FLG (Tcb->CtrlFlag, TCP_CTRL_SND_PSH);
} else if ((Seg->End == Tcb->SndNxt) &&
(GET_SND_DATASIZE (Tcb->Sk) == 0)) {
TCP_SET_FLG (Seg->Flag, TCP_FLG_PSH);
}
}
//
// Check whether to set the URG flag and the urgent pointer.
//
TCP_CLEAR_FLG (Seg->Flag, TCP_FLG_URG);
if (TCP_FLG_ON (Tcb->CtrlFlag, TCP_CTRL_SND_URG) &&
TCP_SEQ_LEQ (Seg->Seq, Tcb->SndUp)) {
TCP_SET_FLG (Seg->Flag, TCP_FLG_URG);
if (TCP_SEQ_LT (Tcb->SndUp, Seg->End)) {
Seg->Urg = (UINT16) TCP_SUB_SEQ (Tcb->SndUp, Seg->Seq);
} else {
Seg->Urg = (UINT16) NET_MIN (
TCP_SUB_SEQ (Tcb->SndUp,
Seg->Seq),
0xffff
);
}
}
Head->Flag = Seg->Flag;
Head->Urg = NTOHS (Seg->Urg);
Head->Checksum = TcpChecksum (Nbuf, Tcb->HeadSum);
//
// update the TCP session's control information
//
Tcb->RcvWl2 = Tcb->RcvNxt;
if (Syn) {
Tcb->RcvWnd = NTOHS (Head->Wnd);
}
//
// clear delayedack flag
//
Tcb->DelayedAck = 0;
return TcpSendIpPacket (Tcb, Nbuf, Tcb->LocalEnd.Ip, Tcb->RemoteEnd.Ip);
}
NET_BUF *
TcpGetSegmentSndQue (
IN TCP_CB *Tcb,
IN TCP_SEQNO Seq,
IN UINT32 Len
)
/*++
Routine Description:
Get a segment from the Tcb's SndQue.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Seq - The sequence number of the segment.
Len - The maximum length of the segment.
Returns:
Pointer to the segment, if NULL some error occurred.
--*/
{
NET_LIST_ENTRY *Head;
NET_LIST_ENTRY *Cur;
NET_BUF *Node;
TCP_SEG *Seg;
NET_BUF *Nbuf;
TCP_SEQNO End;
UINT8 *Data;
UINT8 Flag;
INT32 Offset;
INT32 CopyLen;
ASSERT (Tcb && TCP_SEQ_LEQ (Seq, Tcb->SndNxt) && (Len > 0));
//
// Find the segment that contains the Seq.
//
Head = &Tcb->SndQue;
Node = NULL;
Seg = NULL;
NET_LIST_FOR_EACH (Cur, Head) {
Node = NET_LIST_USER_STRUCT (Cur, NET_BUF, List);
Seg = TCPSEG_NETBUF (Node);
if (TCP_SEQ_LT (Seq, Seg->End) && TCP_SEQ_LEQ (Seg->Seq, Seq)) {
break;
}
}
ASSERT (Cur != Head);
//
// Return the buffer if it can be returned without
// adjustment:
//
if ((Seg->Seq == Seq) &&
TCP_SEQ_LEQ (Seg->End, Seg->Seq + Len) &&
!NET_BUF_SHARED (Node)) {
NET_GET_REF (Node);
return Node;
}
//
// Create a new buffer and copy data there.
//
Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD);
if (Nbuf == NULL) {
return NULL;
}
NetbufReserve (Nbuf, TCP_MAX_HEAD);
Flag = Seg->Flag;
End = Seg->End;
if (TCP_SEQ_LT (Seq + Len, Seg->End)) {
End = Seq + Len;
}
CopyLen = TCP_SUB_SEQ (End, Seq);
Offset = TCP_SUB_SEQ (Seq, Seg->Seq);
//
// 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.
//
if (TCP_FLG_ON (Seg->Flag, TCP_FLG_SYN)) {
if (TCP_SEQ_LT (Seg->Seq, Seq)) {
TCP_CLEAR_FLG (Flag, TCP_FLG_SYN);
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.
//
if (TCP_FLG_ON (Seg->Flag, TCP_FLG_FIN)) {
if (Seg->End == End) {
CopyLen--;
} else {
TCP_CLEAR_FLG (Flag, TCP_FLG_FIN);
}
}
ASSERT (CopyLen >= 0);
//
// copy data to the segment
//
if (CopyLen) {
Data = NetbufAllocSpace (Nbuf, CopyLen, NET_BUF_TAIL);
ASSERT (Data);
if ((INT32) NetbufCopy (Node, Offset, CopyLen, Data) != CopyLen) {
goto OnError;
}
}
NetCopyMem (TCPSEG_NETBUF (Nbuf), Seg, sizeof (TCP_SEG));
TCPSEG_NETBUF (Nbuf)->Seq = Seq;
TCPSEG_NETBUF (Nbuf)->End = End;
TCPSEG_NETBUF (Nbuf)->Flag = Flag;
return Nbuf;
OnError:
NetbufFree (Nbuf);
return NULL;
}
NET_BUF *
TcpGetSegmentSock (
IN TCP_CB *Tcb,
IN TCP_SEQNO Seq,
IN UINT32 Len
)
/*++
Routine Description:
Get a segment from the Tcb's socket buffer.
Arguments:
Tcb - Pointer to the TCP_CB of this TCP instance.
Seq - The sequence number of the segment.
Len - The maximum length of the segment.
Returns:
Pointer to the segment, if NULL some error occurred.
--*/
{
NET_BUF *Nbuf;
UINT8 *Data;
UINT32 DataGet;
ASSERT (Tcb && Tcb->Sk);
Nbuf = NetbufAlloc (Len + TCP_MAX_HEAD);
if (Nbuf == NULL) {
TCP4_DEBUG_ERROR (("TcpGetSegmentSock: failed to allocate "
"a netbuf for TCB %x\n",Tcb));
return NULL;
}
NetbufReserve (Nbuf, TCP_MAX_HEAD);
DataGet = 0;
if (Len) {
//
// copy data to the segment.
//
Data = NetbufAllocSpace (Nbuf, Len, NET_BUF_TAIL);
ASSERT (Data);
DataGet = SockGetDataToSend (Tcb->Sk, 0, Len, Data);
}
NET_GET_REF (Nbuf);
TCPSEG_NETBUF (Nbuf)->Seq = Seq;
TCPSEG_NETBUF (Nbuf)->End = Seq + Len;
NetListInsertTail (&(Tcb->SndQue), &(Nbuf->List));
if (DataGet != 0) {
SockDataSent (Tcb->Sk, DataGet);
}
return Nbuf;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?