TCPTransmitImpl.mesa
Last Edited by: Nichols, August 25, 1983 4:37 pm
Last Edited by: Taft, January 4, 1984 12:01 pm
Last Edited by: HGM, April 18, 1985 11:07:00 pm PST
Hal Murray June 3, 1985 10:05:37 pm PDT
John Larson, April 14, 1986 11:30:03 pm PST
DIRECTORY
IPDefs USING [DByte, Datagram, DatagramRec, Address, SendSpecific, TCPProtocol],
List USING [Nconc1],
TCP USING [Error],
TCPLogging USING [PrintTCPPacket],
TCPOps USING [Flip, Flop, maxTCPDataLength, ourLocalAddress, pktsRexmitted, pktsSent, sendBufferLength, SetTimeout, TCPChecksum, TCPHandle, tcpHdrByteLength, tcpHdrWordLength, TCPHeaderP, tcpSegmentLife, TCPSendBuffer],
TCPTransmit;
TCPTransmitImpl:
CEDAR
PROGRAM
IMPORTS IPDefs, List, TCP, TCPLogging, TCPOps
EXPORTS TCPTransmit =
BEGIN
OPEN TCPOps, TCPTransmit;
TCPSend:
PROC [handle: TCPHandle, data: IPDefs.Datagram] ~ {
Fill in the header and miscellaneous stuff in the datagram and send it. This uses IPDefs.SendSpecific since the destination depends on the handle. When packets are given to TCPSend, the dataLength should be the length of the TCP data, not the total data length.
data.inHdr.timeToLive ← tcpSegmentLife;
data.inHdr.protocol ← IPDefs.TCPProtocol;
data.inHdr.source ← ourLocalAddress;
IF handle # NIL THEN data.inHdr.destination ← handle.foreignAddr;
data.optionLength ← 0;
data.dataLength ← data.dataLength + tcpHdrByteLength;
TRUSTED {
tcpHdrPtr: TCPHeaderP ← LOOPHOLE[@data.data];
IF handle #
NIL
THEN {
tcpHdrPtr.sourcePort ← handle.localPort;
tcpHdrPtr.dstnPort ← handle.foreignPort; };
tcpHdrPtr.unused ← 0;
tcpHdrPtr.dataWordOffset ← tcpHdrWordLength;
tcpHdrPtr.checksum ← TCPChecksum[data]; };
pktsSent ← pktsSent + 1;
IPDefs.SendSpecific[data];
data.dataLength ← data.dataLength - tcpHdrByteLength; };
SendSegmentToNet:
PROC [handle: TCPHandle, sendDatagram: IPDefs.Datagram, dataSegment:
BOOL] ~
TRUSTED {
Procedure to send data, control or ack only segments to network. The dataSegment argument should be set if there is control or data in the segment which should be acked, i.e. if it is not an ack only segment. If the dataSegment argument is set, the packet is put on the rexmit queue for the connection.
tcpHdrPtr: TCPHeaderP; -- TCP header to send
tcpSendBufferPtr: REF TCPSendBuffer; -- information about sent segment
common code for sending data or ack only segments
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.seqNumber ← Flop[handle.sndNxt]; -- set sequence number
tcpHdrPtr.window ← handle.rcvWnd; -- send latest window
IF ~(handle.state = listen
OR handle.state = synSent)
THEN {
-- if in synced state
tcpHdrPtr.ack ← TRUE; -- send ack
tcpHdrPtr.ackNumber ← Flop[handle.rcvNxt]; }
ELSE {
tcpHdrPtr.ack ← FALSE;
tcpHdrPtr.ackNumber ← Flop[0]; };
update next seq number to send
handle.sndNxt ← handle.sndNxt + sendDatagram.dataLength;
IF tcpHdrPtr.syn
THEN
-- adjust seq number for SYN and FIN
handle.sndNxt ← handle.sndNxt + 1;
IF tcpHdrPtr.fin
THEN {
handle.sndNxt ← handle.sndNxt + 1;
handle.finSequence ← Flip[tcpHdrPtr.seqNumber]; };
IF handle.sndUrgent
THEN {
tcpHdrPtr.urg ← TRUE;
tcpHdrPtr.urgentPtr ← handle.sndUp - handle.sndNxt; }
ELSE {
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.urgentPtr ← 0; };
TCPSend[handle, sendDatagram];
TCPLogging.PrintTCPPacket[handle, sendDatagram, toNet];
IF dataSegment
THEN {
-- if didn't send ack alone
then put entry on rexmit queue
tcpSendBufferPtr ← NEW[TCPSendBuffer];
tcpSendBufferPtr.dataByteCount ← sendDatagram.dataLength;
tcpSendBufferPtr.datagram ← sendDatagram;
tcpSendBufferPtr.rexmitTime ← SetTimeout[rexmitInterval];
tcpSendBufferPtr.timeoutTime ← SetTimeout[rexmitTimeout];
IF tcpHdrPtr.syn THEN tcpSendBufferPtr.timeoutTime ← SetTimeout[initialTimeout];
handle.rexmitQueue ← List.Nconc1[handle.rexmitQueue, tcpSendBufferPtr]; };
}; -- SendSegmentToNet
TryToSend:
PUBLIC
PROC [handle: TCPHandle] = {
Called to send as much data as possible from the ToNet queue. Checks window to determine if data can be sent; if data can not be sent, sends an ack only segment. Calls SendSegmentToNet to actually send the data.
sendDatagram: IPDefs.Datagram; -- datagram to send
sendData: BOOL; -- set if sending data, not ack only
IF handle.state = listen
OR handle.state = synSent
THEN
-- if in unsynced state
RETURN WITH ERROR TCP.Error[handle.reason]; -- Trying to send data in unsynced state
IF handle.sndWnd = 0
THEN
-- in synced state, if send window is zero
IF handle.rexmitQueue = NIL THEN sendData ← TRUE -- if no pkts sent, send pkt to probe window
ELSE sendData ← FALSE -- if pkts sent and unacked, send ack
ELSE
IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN sendData ← FALSE -- if non-zero window is full, send ack
ELSE sendData ← TRUE; -- else send data
IF
-- if can send data, but no data to send
sendData AND handle.toNetQueue = NIL THEN
sendData ← FALSE; -- just send an ack
IF
NOT sendData
THEN
TRUSTED {
-- if sending only an ack
allocate new buffer for ack and fill in header
tcpHdrPtr: TCPHeaderP; -- TCP header to send
sendDatagram ← NEW[IPDefs.DatagramRec];
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.psh ← FALSE;
tcpHdrPtr.syn ← FALSE;
tcpHdrPtr.fin ← FALSE;
tcpHdrPtr.rst ← FALSE;
IF handle.sndUrgent
THEN {
tcpHdrPtr.urg ← TRUE;
tcpHdrPtr.urgentPtr ← handle.sndUp - handle.sndNxt; }
ELSE {
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.urgentPtr ← 0; };
sendDatagram.dataLength ← 0;
SendSegmentToNet[handle, sendDatagram, FALSE]; -- send to net
RETURN; -- finished trying to send
};
DO
-- send data until send window is full or no more data to send
IF handle.toNetQueue = NIL THEN RETURN; -- out of data
sendDatagram ← NARROW[handle.toNetQueue.first];
handle.toNetQueue ← handle.toNetQueue.rest;
SendSegmentToNet[handle, sendDatagram, TRUE]; -- send segment to net
IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN EXIT
ENDLOOP
}; -- TryToSend
TryToSendData:
PUBLIC
PROC [handle: TCPHandle] = {
Called to determine if data can be sent; if so, it calls TryToSend. This routine is called when you want to send a segment if and only if data exists; thus it is called when an empty ack packet has been received and more data may be sent or when the user supplies more data to send via the TCPSend routine. TryToSend can't be called directly because it always generates an ack and we don't need acks for the acks.
sendData: BOOL;
IF handle.state = listen
OR handle.state = synSent
THEN
-- if in unsynced state
RETURN WITH ERROR TCP.Error[handle.reason]; -- Trying to send data in unsynced state
IF handle.sndWnd = 0
THEN
-- if send window is zero
IF handle.rexmitQueue = NIL THEN sendData ← TRUE -- if no pkts sent, send pkt to probe window
ELSE sendData ← FALSE -- if pkts sent and unacked, send ack
ELSE
IF handle.sndNxt >= handle.sndUna + handle.sndWnd THEN sendData ← FALSE -- if non-zero window is full, send ack
ELSE sendData ← TRUE; -- else send data
IF sendData
AND handle.toNetQueue #
NIL
THEN
-- if allowed to send data and data to send
TryToSend[handle] -- then send it
}; -- TryToSendData
SendSYN:
PUBLIC
PROC [handle: TCPHandle] =
TRUSTED {
This procedure allocates a segment to send a SYN. It calls SendSegmentToNet to send the SYN segment.
sendDatagram: IPDefs.Datagram ← NEW[IPDefs.DatagramRec];
tcpHdrPtr: TCPHeaderP ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.urgentPtr ← 0;
tcpHdrPtr.psh ← FALSE;
tcpHdrPtr.rst ← FALSE;
tcpHdrPtr.syn ← FALSE;
tcpHdrPtr.fin ← FALSE;
tcpHdrPtr.ack ← FALSE;
sendDatagram.dataLength ← 0; -- set data length
tcpHdrPtr.syn ← TRUE;
tcpHdrPtr.seqNumber ← Flop[handle.iss];
handle.sndNxt ← handle.iss;
SendSegmentToNet[handle, sendDatagram, TRUE]; };
SendFIN:
PUBLIC
PROC [handle: TCPHandle] =
TRUSTED {
This procedure allocates a segment to send a FIN, queues it on the end of the ToNet queue and then calls TryToSend to send any data on the ToNet queue and the FIN.
sendDatagram: IPDefs.Datagram ← NEW[IPDefs.DatagramRec];
tcpHdrPtr: TCPHeaderP ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.urgentPtr ← 0;
tcpHdrPtr.psh ← FALSE;
tcpHdrPtr.rst ← FALSE;
tcpHdrPtr.syn ← FALSE;
tcpHdrPtr.fin ← TRUE;
tcpHdrPtr.ack ← FALSE;
sendDatagram.dataLength ← 0;
handle.toNetQueue ← List.Nconc1[handle.toNetQueue, sendDatagram];
TryToSend[handle] -- try to send data and fin on to net queue
}; -- SendFIN
SendReset:
PUBLIC
PROC [handle: TCPHandle, sourcePort, dstnPort: IPDefs.DByte, Dstn: IPDefs.Address, seq, ack:
INT] =
TRUSTED {
This procedure is called to send a reset; it sends directly to the network by calling the TCPSend routine; it does not use the TryToSend or SendSegmentToNet routines because resets can be sent when no TCB exists.
sendDatagram: IPDefs.Datagram; -- segment to send
tcpHdrPtr: TCPHeaderP; -- header to send
sendDatagram ← NEW[IPDefs.DatagramRec];
sendDatagram.dataLength ← 0;
sendDatagram.inHdr.destination ← Dstn;
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.sourcePort ← sourcePort;
tcpHdrPtr.dstnPort ← dstnPort;
tcpHdrPtr.seqNumber ← Flop[seq];
tcpHdrPtr.ackNumber ← Flop[ack];
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.psh ← FALSE;
tcpHdrPtr.syn ← FALSE;
tcpHdrPtr.fin ← FALSE;
tcpHdrPtr.rst ← TRUE;
IF ack # 0 THEN tcpHdrPtr.ack ← TRUE
ELSE tcpHdrPtr.ack ← FALSE;
tcpHdrPtr.window ← 0;
tcpHdrPtr.checksum ← 0;
tcpHdrPtr.urgentPtr ← 0;
TCPSend[handle, sendDatagram];
TCPLogging.PrintTCPPacket[handle, sendDatagram, toNet];
}; -- SendReset
RemoveAckedSegments:
PUBLIC
PROC [handle: TCPHandle] ~
TRUSTED {
IF handle.sndUna >= handle.sndUp
THEN {
handle.sndUrgent ← FALSE;
handle.sndUp ← 0; };
WHILE handle.rexmitQueue #
NIL
DO
tcpSendBufferPtr: REF TCPSendBuffer ← NARROW[handle.rexmitQueue.first];
tcpHdrPtr: TCPHeaderP ← LOOPHOLE[@tcpSendBufferPtr.datagram.data];
lastSeqNumber: INT ← Flip[tcpHdrPtr.seqNumber] + tcpSendBufferPtr.dataByteCount - 1;
IF tcpHdrPtr.syn THEN lastSeqNumber ← lastSeqNumber + 1;
IF tcpHdrPtr.fin THEN lastSeqNumber ← lastSeqNumber + 1;
IF handle.sndUna > lastSeqNumber
THEN {
beNiceToGC: LIST OF REF ANY ← handle.rexmitQueue;
handle.rexmitQueue ← handle.rexmitQueue.rest;
beNiceToGC.first ← NIL;
beNiceToGC.rest ← NIL; }
ELSE RETURN; -- segment at head of queue not acked
ENDLOOP;
}; -- RemoveAckedSegments
RepacketizeandRexmit:
PUBLIC
PROC [handle: TCPHandle, tcpSendBufferPtr:
REF TCPSendBuffer] ~
TRUSTED {
This procedure is called to repacketize and retransmit data on the retransmit queue. It is called from CheckRexmitQueues when repacketizing is enabled.
sendDatagram: IPDefs.Datagram;
tcpHdrPtr: TCPHeaderP;
i, j: INT;
bytesToSend: INT;
bytesRexmitted: INT;
tcpSendBufferPtr.rexmitTime ← SetTimeout[rexmitInterval];
IF handle.state = synSent
OR handle.state = synRcvd
THEN {
if syn unacked, just rexmit users's packet
sendDatagram ← tcpSendBufferPtr.datagram;
pktsRexmitted ← pktsRexmitted + 1;
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
IF
NOT (handle.state = listen
OR handle.state = synSent)
THEN
IF
NOT tcpHdrPtr.ack
OR Flip[tcpHdrPtr.ackNumber] # handle.rcvNxt
THEN {
tcpHdrPtr.ack ← TRUE;
tcpHdrPtr.ackNumber ← Flop[handle.rcvNxt]; };
TCPSend[handle, sendDatagram];
TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet];
RETURN };
IF handle.nBytesToSend = 0
AND
NOT (
SELECT handle.state
FROM
finWait1, closing, lastAck => TRUE
ENDCASE => FALSE) THEN
RETURN; -- if no data or fin to send, then exit
bytesToSend ← handle.nBytesToSend;
j ← handle.sendSlot;
bytesRexmitted ← 0;
DO
-- allocate buffers to send and fill them
sendDatagram ← NEW[IPDefs.DatagramRec];
i ← 0;
fill send buffer until all bytes are sent or send buffer is full
WHILE i # bytesToSend
AND i # maxTCPDataLength
DO
sendDatagram.data[i+tcpHdrByteLength] ← handle.sendBuffer[j];
j ← (j + 1) MOD sendBufferLength;
i ← i + 1
ENDLOOP;
bytesToSend ← bytesToSend - i;
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
tcpHdrPtr.urg ← FALSE;
tcpHdrPtr.urgentPtr ← 0;
tcpHdrPtr.psh ← FALSE;
tcpHdrPtr.rst ← FALSE;
tcpHdrPtr.syn ← FALSE;
IF handle.sndUna + i = handle.finSequence THEN tcpHdrPtr.fin ← TRUE
ELSE tcpHdrPtr.fin ← FALSE;
tcpHdrPtr.checksum ← 0;
sendDatagram^.dataLength ← i; -- set data length
tcpHdrPtr.seqNumber ← Flop[handle.sndUna + bytesRexmitted];
bytesRexmitted ← bytesRexmitted + i; -- to calculate sequence numbers
tcpHdrPtr.window ← handle.rcvWnd; -- send latest window
tcpHdrPtr.ack ← TRUE;
tcpHdrPtr.ackNumber ← Flop[handle.rcvNxt];
send the segment to the internet
pktsRexmitted ← pktsRexmitted + 1;
TCPSend[handle, sendDatagram];
TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet];
IF bytesToSend = 0 THEN EXIT
ENDLOOP -- end filling send buffers
}; -- RepacketizeandRexmit
Rexmit:
PUBLIC
PROC [handle: TCPHandle, tcpSendBufferPtr:
REF TCPSendBuffer] ~
TRUSTED {
This procedure is called to retransmit a segment on the retransmit queue when Repacketizing is not enabled. It is called from CheckRexmitQueues.
sendDatagram: IPDefs.Datagram;
tcpHdrPtr: TCPHeaderP;
tcpSendBufferPtr.rexmitTime ← SetTimeout[rexmitInterval];
sendDatagram ← tcpSendBufferPtr.datagram;
pktsRexmitted ← pktsRexmitted + 1;
tcpHdrPtr ← LOOPHOLE[@sendDatagram.data];
IF
NOT (handle.state = listen
OR handle.state = synSent)
THEN
-- update ack field
IF
NOT tcpHdrPtr.ack
OR Flip[tcpHdrPtr.ackNumber] # handle.rcvNxt
THEN {
tcpHdrPtr.ack ← TRUE;
tcpHdrPtr.ackNumber ← Flop[handle.rcvNxt]; };
TCPSend[handle, sendDatagram];
TCPLogging.PrintTCPPacket[handle, sendDatagram, rexmitToNet]; };
END.