2 Implementation of the Socket.
4 Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Get the length of the data that can be retrieved from the socket
21 @param SockBuffer Pointer to the socket receive buffer.
22 @param IsUrg Pointer to a BOOLEAN variable. If TRUE the data is
24 @param BufLen The maximum length of the data buffer to store the
25 received data in socket layer.
27 @return The length of the data can be retreived.
32 IN SOCK_BUFFER
*SockBuffer
,
38 Process the send token.
40 @param Sock Pointer to the socket.
49 Supporting function for both SockImpl and SockInterface.
51 @param Event The Event this notify function registered to, ignored.
65 Get the length of the data that can be retrieved from the socket
68 @param SockBuffer Pointer to the socket receive buffer.
69 @param IsUrg Pointer to a BOOLEAN variable. If TRUE the data is
71 @param BufLen The maximum length of the data buffer to store the
72 received data in socket layer.
74 @return The length of the data can be retreived.
79 IN SOCK_BUFFER
*SockBuffer
,
86 TCP_RSV_DATA
*TcpRsvData
;
88 ASSERT ((SockBuffer
!= NULL
) && (IsUrg
!= NULL
) && (BufLen
> 0));
90 RcvBufEntry
= SockBufFirst (SockBuffer
);
91 ASSERT (RcvBufEntry
!= NULL
);
93 TcpRsvData
= (TCP_RSV_DATA
*) RcvBufEntry
->ProtoData
;
95 *IsUrg
= (BOOLEAN
) ((TcpRsvData
->UrgLen
> 0) ? TRUE
: FALSE
);
97 if (*IsUrg
&& TcpRsvData
->UrgLen
< RcvBufEntry
->TotalSize
) {
99 DataLen
= MIN (TcpRsvData
->UrgLen
, BufLen
);
101 if (DataLen
< TcpRsvData
->UrgLen
) {
102 TcpRsvData
->UrgLen
= TcpRsvData
->UrgLen
- DataLen
;
104 TcpRsvData
->UrgLen
= 0;
111 DataLen
= RcvBufEntry
->TotalSize
;
113 RcvBufEntry
= SockBufNext (SockBuffer
, RcvBufEntry
);
115 while ((BufLen
> DataLen
) && (RcvBufEntry
!= NULL
)) {
117 TcpRsvData
= (TCP_RSV_DATA
*) RcvBufEntry
->ProtoData
;
119 Urg
= (BOOLEAN
) ((TcpRsvData
->UrgLen
> 0) ? TRUE
: FALSE
);
125 if (*IsUrg
&& TcpRsvData
->UrgLen
< RcvBufEntry
->TotalSize
) {
127 if (TcpRsvData
->UrgLen
+ DataLen
< BufLen
) {
128 TcpRsvData
->UrgLen
= 0;
130 TcpRsvData
->UrgLen
= TcpRsvData
->UrgLen
- (BufLen
- DataLen
);
133 return MIN (TcpRsvData
->UrgLen
+ DataLen
, BufLen
);
137 DataLen
+= RcvBufEntry
->TotalSize
;
139 RcvBufEntry
= SockBufNext (SockBuffer
, RcvBufEntry
);
142 DataLen
= MIN (BufLen
, DataLen
);
148 Copy data from socket buffer to application provided receive buffer.
150 @param Sock Pointer to the socket.
151 @param TcpRxData Pointer to the application provided receive buffer.
152 @param RcvdBytes The maximum length of the data can be copied.
153 @param IsOOB If TRUE the data is OOB, FALSE the data is normal.
167 EFI_TCP4_RECEIVE_DATA
*RxData
;
168 EFI_TCP4_FRAGMENT_DATA
*Fragment
;
170 RxData
= (EFI_TCP4_RECEIVE_DATA
*) TcpRxData
;
174 ASSERT (RxData
->DataLength
>= RcvdBytes
);
176 RxData
->DataLength
= RcvdBytes
;
177 RxData
->UrgentFlag
= IsOOB
;
179 for (Index
= 0; (Index
< RxData
->FragmentCount
) && (RcvdBytes
> 0); Index
++) {
181 Fragment
= &RxData
->FragmentTable
[Index
];
182 CopyBytes
= MIN ((UINT32
) (Fragment
->FragmentLength
), RcvdBytes
);
185 Sock
->RcvBuffer
.DataQueue
,
188 Fragment
->FragmentBuffer
191 Fragment
->FragmentLength
= CopyBytes
;
192 RcvdBytes
-= CopyBytes
;
199 Get received data from the socket layer to the receive token.
201 @param Sock Pointer to the socket.
202 @param RcvToken Pointer to the application provided receive token.
204 @return The length of data received in this token.
208 SockProcessRcvToken (
210 IN OUT SOCK_IO_TOKEN
*RcvToken
213 UINT32 TokenRcvdBytes
;
214 EFI_TCP4_RECEIVE_DATA
*RxData
;
217 ASSERT (Sock
!= NULL
);
219 ASSERT (SockStream
== Sock
->Type
);
221 RxData
= RcvToken
->Packet
.RxData
;
223 TokenRcvdBytes
= SockTcpDataToRcv (
226 (UINT32
) RxData
->DataLength
230 // Copy data from RcvBuffer of socket to user
231 // provided RxData and set the fields in TCP RxData
233 SockSetTcpRxData (Sock
, RxData
, TokenRcvdBytes
, IsUrg
);
235 NetbufQueTrim (Sock
->RcvBuffer
.DataQueue
, TokenRcvdBytes
);
236 SIGNAL_TOKEN (&(RcvToken
->Token
), EFI_SUCCESS
);
238 return TokenRcvdBytes
;
243 Process the TCP send data, buffer the tcp txdata and append
244 the buffer to socket send buffer,then try to send it.
246 @param Sock Pointer to the socket.
247 @param TcpTxData Pointer to the application provided send buffer.
249 @retval EFI_SUCCESS The operation is completed successfully.
250 @retval EFI_OUT_OF_RESOURCES Failed due to resource limit.
254 SockProcessTcpSndData (
261 EFI_TCP4_TRANSMIT_DATA
*TxData
;
263 TxData
= (EFI_TCP4_TRANSMIT_DATA
*) TcpTxData
;
266 // transform this TxData into a NET_BUFFER
267 // and insert it into Sock->SndBuffer
269 SndData
= NetbufFromExt (
270 (NET_FRAGMENT
*) TxData
->FragmentTable
,
271 (UINT32
) TxData
->FragmentCount
,
278 if (NULL
== SndData
) {
279 DEBUG ((EFI_D_ERROR
, "SockKProcessSndData: Failed to"
280 " call NetBufferFromExt\n"));
282 return EFI_OUT_OF_RESOURCES
;
285 NetbufQueAppend (Sock
->SndBuffer
.DataQueue
, SndData
);
288 // notify the low layer protocol to handle this send token
290 if (TxData
->Urgent
) {
291 Status
= Sock
->ProtoHandler (Sock
, SOCK_SNDURG
, NULL
);
293 if (EFI_ERROR (Status
)) {
299 Status
= Sock
->ProtoHandler (Sock
, SOCK_SNDPUSH
, NULL
);
301 if (EFI_ERROR (Status
)) {
307 // low layer protocol should really handle the sending
308 // process when catching SOCK_SND request
310 Status
= Sock
->ProtoHandler (Sock
, SOCK_SND
, NULL
);
312 if (EFI_ERROR (Status
)) {
321 Flush the tokens in the specific token list.
323 @param Sock Pointer to the socket.
324 @param PendingTokenList Pointer to the token list to be flushed.
328 SockFlushPendingToken (
330 IN LIST_ENTRY
*PendingTokenList
333 SOCK_TOKEN
*SockToken
;
334 SOCK_COMPLETION_TOKEN
*Token
;
336 ASSERT ((Sock
!= NULL
) && (PendingTokenList
!= NULL
));
338 while (!IsListEmpty (PendingTokenList
)) {
339 SockToken
= NET_LIST_HEAD (
345 Token
= SockToken
->Token
;
346 SIGNAL_TOKEN (Token
, Sock
->SockError
);
348 RemoveEntryList (&(SockToken
->TokenList
));
349 FreePool (SockToken
);
355 Wake up the connection token while the connection is successfully established,
356 then try to process any pending send token.
358 @param Sock Pointer to the socket.
366 ASSERT (Sock
->ConnectionToken
!= NULL
);
368 SIGNAL_TOKEN (Sock
->ConnectionToken
, EFI_SUCCESS
);
369 Sock
->ConnectionToken
= NULL
;
372 // check to see if some pending send token existed?
374 SockProcessSndToken (Sock
);
380 Wake up the listen token while the connection is established successfully.
382 @param Sock Pointer to the socket.
386 SockWakeListenToken (
391 SOCK_TOKEN
*SockToken
;
392 EFI_TCP4_LISTEN_TOKEN
*ListenToken
;
394 Parent
= Sock
->Parent
;
396 ASSERT ((Parent
!= NULL
) && SOCK_IS_LISTENING (Parent
) && SOCK_IS_CONNECTED (Sock
));
398 if (!IsListEmpty (&Parent
->ListenTokenList
)) {
399 SockToken
= NET_LIST_HEAD (
400 &Parent
->ListenTokenList
,
405 ListenToken
= (EFI_TCP4_LISTEN_TOKEN
*) SockToken
->Token
;
406 ListenToken
->NewChildHandle
= Sock
->SockHandle
;
408 SIGNAL_TOKEN (&(ListenToken
->CompletionToken
), EFI_SUCCESS
);
410 RemoveEntryList (&SockToken
->TokenList
);
411 FreePool (SockToken
);
413 RemoveEntryList (&Sock
->ConnectionList
);
416 DEBUG ((EFI_D_NET
, "SockWakeListenToken: accept a socket, now conncnt is %d", Parent
->ConnCnt
));
424 Wake up the receive token while some data is received.
426 @param Sock Pointer to the socket.
435 UINT32 TokenRcvdBytes
;
436 SOCK_TOKEN
*SockToken
;
437 SOCK_IO_TOKEN
*RcvToken
;
439 ASSERT (Sock
->RcvBuffer
.DataQueue
!= NULL
);
441 RcvdBytes
= (Sock
->RcvBuffer
.DataQueue
)->BufSize
;
443 ASSERT (RcvdBytes
> 0);
445 while (RcvdBytes
> 0 && !IsListEmpty (&Sock
->RcvTokenList
)) {
447 SockToken
= NET_LIST_HEAD (
453 RcvToken
= (SOCK_IO_TOKEN
*) SockToken
->Token
;
454 TokenRcvdBytes
= SockProcessRcvToken (Sock
, RcvToken
);
456 if (0 == TokenRcvdBytes
) {
460 RemoveEntryList (&(SockToken
->TokenList
));
461 FreePool (SockToken
);
462 RcvdBytes
-= TokenRcvdBytes
;
468 Process the send token.
470 @param Sock Pointer to the socket.
474 SockProcessSndToken (
479 SOCK_TOKEN
*SockToken
;
481 SOCK_IO_TOKEN
*SndToken
;
482 EFI_TCP4_TRANSMIT_DATA
*TxData
;
485 ASSERT ((Sock
!= NULL
) && (SockStream
== Sock
->Type
));
487 FreeSpace
= SockGetFreeSpace (Sock
, SOCK_SND_BUF
);
490 // to determine if process a send token using
491 // socket layer flow control policy
493 while ((FreeSpace
>= Sock
->SndBuffer
.LowWater
) &&
494 !IsListEmpty (&Sock
->SndTokenList
)) {
496 SockToken
= NET_LIST_HEAD (
497 &(Sock
->SndTokenList
),
503 // process this token
505 RemoveEntryList (&(SockToken
->TokenList
));
507 &(Sock
->ProcessingSndTokenList
),
508 &(SockToken
->TokenList
)
512 // Proceess it in the light of SockType
514 SndToken
= (SOCK_IO_TOKEN
*) SockToken
->Token
;
515 TxData
= SndToken
->Packet
.TxData
;
517 DataLen
= (UINT32
) TxData
->DataLength
;
518 Status
= SockProcessTcpSndData (Sock
, TxData
);
520 if (EFI_ERROR (Status
)) {
524 if (DataLen
>= FreeSpace
) {
528 FreeSpace
-= DataLen
;
537 RemoveEntryList (&SockToken
->TokenList
);
538 SIGNAL_TOKEN (SockToken
->Token
, Status
);
539 FreePool (SockToken
);
544 Create a socket with initial data SockInitData.
546 @param SockInitData Pointer to the initial data of the socket.
548 @return Pointer to the newly created socket, return NULL when exception occured.
553 IN SOCK_INIT_DATA
*SockInitData
560 ASSERT ((SockInitData
!= NULL
) && (SockInitData
->ProtoHandler
!= NULL
));
561 ASSERT (SockInitData
->Type
== SockStream
);
562 ASSERT ((SockInitData
->ProtoData
!= NULL
) && (SockInitData
->DataSize
<= PROTO_RESERVED_LEN
));
564 Parent
= SockInitData
->Parent
;
566 if ((Parent
!= NULL
) && (Parent
->ConnCnt
== Parent
->BackLog
)) {
569 "SockCreate: Socket parent has "
570 "reached its connection limit with %d ConnCnt and %d BackLog\n",
578 Sock
= AllocateZeroPool (sizeof (SOCKET
));
581 DEBUG ((EFI_D_ERROR
, "SockCreate: No resource to create a new socket\n"));
585 InitializeListHead (&Sock
->Link
);
586 InitializeListHead (&Sock
->ConnectionList
);
587 InitializeListHead (&Sock
->ListenTokenList
);
588 InitializeListHead (&Sock
->RcvTokenList
);
589 InitializeListHead (&Sock
->SndTokenList
);
590 InitializeListHead (&Sock
->ProcessingSndTokenList
);
592 EfiInitializeLock (&(Sock
->Lock
), TPL_CALLBACK
);
594 Sock
->SndBuffer
.DataQueue
= NetbufQueAlloc ();
595 if (NULL
== Sock
->SndBuffer
.DataQueue
) {
596 DEBUG ((EFI_D_ERROR
, "SockCreate: No resource to allocate"
597 " SndBuffer for new socket\n"));
602 Sock
->RcvBuffer
.DataQueue
= NetbufQueAlloc ();
603 if (NULL
== Sock
->RcvBuffer
.DataQueue
) {
604 DEBUG ((EFI_D_ERROR
, "SockCreate: No resource to allocate "
605 "RcvBuffer for new socket\n"));
610 Sock
->Signature
= SOCK_SIGNATURE
;
612 Sock
->Parent
= Parent
;
613 Sock
->BackLog
= SockInitData
->BackLog
;
614 Sock
->ProtoHandler
= SockInitData
->ProtoHandler
;
615 Sock
->SndBuffer
.HighWater
= SockInitData
->SndBufferSize
;
616 Sock
->RcvBuffer
.HighWater
= SockInitData
->RcvBufferSize
;
617 Sock
->Type
= SockInitData
->Type
;
618 Sock
->DriverBinding
= SockInitData
->DriverBinding
;
619 Sock
->State
= SockInitData
->State
;
620 Sock
->CreateCallback
= SockInitData
->CreateCallback
;
621 Sock
->DestroyCallback
= SockInitData
->DestroyCallback
;
622 Sock
->Context
= SockInitData
->Context
;
624 Sock
->SockError
= EFI_ABORTED
;
625 Sock
->SndBuffer
.LowWater
= SOCK_BUFF_LOW_WATER
;
626 Sock
->RcvBuffer
.LowWater
= SOCK_BUFF_LOW_WATER
;
629 // Install protocol on Sock->SockHandle
632 &(Sock
->NetProtocol
.TcpProtocol
),
633 SockInitData
->Protocol
,
634 sizeof (EFI_TCP4_PROTOCOL
)
638 // copy the protodata into socket
640 CopyMem (Sock
->ProtoReserved
, SockInitData
->ProtoData
, SockInitData
->DataSize
);
642 Status
= gBS
->InstallMultipleProtocolInterfaces (
644 &gEfiTcp4ProtocolGuid
,
645 &(Sock
->NetProtocol
.TcpProtocol
),
649 if (EFI_ERROR (Status
)) {
650 DEBUG ((EFI_D_ERROR
, "SockCreate: Install TCP protocol in "
651 "socket failed with %r\n", Status
));
656 if (Parent
!= NULL
) {
657 ASSERT (Parent
->BackLog
> 0);
658 ASSERT (SOCK_IS_LISTENING (Parent
));
661 // need to add it into Parent->ConnectionList
662 // if the Parent->ConnCnt < Parent->BackLog
668 "SockCreate: Create a new socket and add to parent, now conncnt is %d\n",
672 InsertTailList (&Parent
->ConnectionList
, &Sock
->ConnectionList
);
675 if (Sock
->CreateCallback
!= NULL
) {
676 Status
= Sock
->CreateCallback (Sock
, Sock
->Context
);
677 if (EFI_ERROR (Status
)) {
686 if (Sock
->SockHandle
!= NULL
) {
687 gBS
->UninstallMultipleProtocolInterfaces (
689 &gEfiTcp4ProtocolGuid
,
690 &(Sock
->NetProtocol
.TcpProtocol
),
695 if (NULL
!= Sock
->SndBuffer
.DataQueue
) {
696 NetbufQueFree (Sock
->SndBuffer
.DataQueue
);
699 if (NULL
!= Sock
->RcvBuffer
.DataQueue
) {
700 NetbufQueFree (Sock
->RcvBuffer
.DataQueue
);
712 @param Sock Pointer to the socket.
721 EFI_GUID
*ProtocolGuid
;
724 ASSERT (SockStream
== Sock
->Type
);
726 if (Sock
->DestroyCallback
!= NULL
) {
727 Sock
->DestroyCallback (Sock
, Sock
->Context
);
731 // Flush the completion token buffered
732 // by sock and rcv, snd buffer
734 if (!SOCK_IS_UNCONFIGURED (Sock
)) {
736 SockConnFlush (Sock
);
737 SockSetState (Sock
, SO_CLOSED
);
738 Sock
->ConfigureState
= SO_UNCONFIGURED
;
742 // Destroy the RcvBuffer Queue and SendBuffer Queue
744 NetbufQueFree (Sock
->RcvBuffer
.DataQueue
);
745 NetbufQueFree (Sock
->SndBuffer
.DataQueue
);
748 // Remove it from parent connection list if needed
750 if (Sock
->Parent
!= NULL
) {
752 RemoveEntryList (&(Sock
->ConnectionList
));
753 (Sock
->Parent
->ConnCnt
)--;
757 "SockDestroy: Delete a unaccepted socket from parent"
758 "now conncnt is %d\n",
759 Sock
->Parent
->ConnCnt
)
766 // Set the protocol guid and driver binding handle
767 // in the light of Sock->SockType
769 ProtocolGuid
= &gEfiTcp4ProtocolGuid
;
772 // Retrieve the protocol installed on this sock
774 Status
= gBS
->OpenProtocol (
780 EFI_OPEN_PROTOCOL_GET_PROTOCOL
783 if (EFI_ERROR (Status
)) {
785 DEBUG ((EFI_D_ERROR
, "SockDestroy: Open protocol installed "
786 "on socket failed with %r\n", Status
));
792 // Uninstall the protocol installed on this sock
793 // in the light of Sock->SockType
795 gBS
->UninstallMultipleProtocolInterfaces (
809 Flush the sndBuffer and rcvBuffer of socket.
811 @param Sock Pointer to the socket.
821 ASSERT (Sock
!= NULL
);
824 // Clear the flag in this socket
829 // Flush the SndBuffer and RcvBuffer of Sock
831 NetbufQueFlush (Sock
->SndBuffer
.DataQueue
);
832 NetbufQueFlush (Sock
->RcvBuffer
.DataQueue
);
835 // Signal the pending token
837 if (Sock
->ConnectionToken
!= NULL
) {
838 SIGNAL_TOKEN (Sock
->ConnectionToken
, Sock
->SockError
);
839 Sock
->ConnectionToken
= NULL
;
842 if (Sock
->CloseToken
!= NULL
) {
843 SIGNAL_TOKEN (Sock
->CloseToken
, Sock
->SockError
);
844 Sock
->CloseToken
= NULL
;
847 SockFlushPendingToken (Sock
, &(Sock
->ListenTokenList
));
848 SockFlushPendingToken (Sock
, &(Sock
->RcvTokenList
));
849 SockFlushPendingToken (Sock
, &(Sock
->SndTokenList
));
850 SockFlushPendingToken (Sock
, &(Sock
->ProcessingSndTokenList
));
853 // Destroy the pending connection, if it is a listening socket
855 if (SOCK_IS_LISTENING (Sock
)) {
856 while (!IsListEmpty (&Sock
->ConnectionList
)) {
857 Child
= NET_LIST_HEAD (
858 &Sock
->ConnectionList
,
863 SockDestroyChild (Child
);
874 Set the state of the socket.
876 @param Sock Pointer to the socket.
877 @param State The new socket state to be set.
891 Clone a new socket including its associated protocol control block.
893 @param Sock Pointer to the socket to be cloned.
895 @return Pointer to the newly cloned socket. If NULL, error condition occurred.
904 SOCK_INIT_DATA InitData
;
906 InitData
.BackLog
= Sock
->BackLog
;
907 InitData
.Parent
= Sock
;
908 InitData
.State
= Sock
->State
;
909 InitData
.ProtoHandler
= Sock
->ProtoHandler
;
910 InitData
.Type
= Sock
->Type
;
911 InitData
.RcvBufferSize
= Sock
->RcvBuffer
.HighWater
;
912 InitData
.SndBufferSize
= Sock
->SndBuffer
.HighWater
;
913 InitData
.DriverBinding
= Sock
->DriverBinding
;
914 InitData
.Protocol
= &(Sock
->NetProtocol
);
915 InitData
.CreateCallback
= Sock
->CreateCallback
;
916 InitData
.DestroyCallback
= Sock
->DestroyCallback
;
917 InitData
.Context
= Sock
->Context
;
918 InitData
.ProtoData
= Sock
->ProtoReserved
;
919 InitData
.DataSize
= sizeof (Sock
->ProtoReserved
);
921 ClonedSock
= SockCreate (&InitData
);
923 if (NULL
== ClonedSock
) {
924 DEBUG ((EFI_D_ERROR
, "SockClone: no resource to create a cloned sock\n"));
928 SockSetState (ClonedSock
, SO_CONNECTING
);
929 ClonedSock
->ConfigureState
= Sock
->ConfigureState
;
936 Called by the low layer protocol to indicate the socket a connection is
939 This function just changes the socket's state to SO_CONNECTED
940 and signals the token used for connection establishment.
942 @param Sock Pointer to the socket associated with the
943 established connection.
946 SockConnEstablished (
951 ASSERT (SO_CONNECTING
== Sock
->State
);
953 SockSetState (Sock
, SO_CONNECTED
);
955 if (NULL
== Sock
->Parent
) {
956 SockWakeConnToken (Sock
);
958 SockWakeListenToken (Sock
);
966 Called by the low layer protocol to indicate the connection is closed.
968 This function flushes the socket, sets the state to SO_CLOSED and signals
971 @param Sock Pointer to the socket associated with the closed
980 if (Sock
->CloseToken
!= NULL
) {
981 SIGNAL_TOKEN (Sock
->CloseToken
, EFI_SUCCESS
);
982 Sock
->CloseToken
= NULL
;
985 SockConnFlush (Sock
);
986 SockSetState (Sock
, SO_CLOSED
);
988 if (Sock
->Parent
!= NULL
) {
989 SockDestroyChild (Sock
);
996 Called by low layer protocol to indicate that some data is sent or processed.
998 This function trims the sent data in the socket send buffer, signals the data
1001 @param Sock Pointer to the socket.
1002 @param Count The length of the data processed or sent, in bytes.
1011 SOCK_TOKEN
*SockToken
;
1012 SOCK_COMPLETION_TOKEN
*SndToken
;
1014 ASSERT (!IsListEmpty (&Sock
->ProcessingSndTokenList
));
1015 ASSERT (Count
<= (Sock
->SndBuffer
.DataQueue
)->BufSize
);
1017 NetbufQueTrim (Sock
->SndBuffer
.DataQueue
, Count
);
1020 // To check if we can signal some snd token in this socket
1023 SockToken
= NET_LIST_HEAD (
1024 &(Sock
->ProcessingSndTokenList
),
1029 SndToken
= SockToken
->Token
;
1031 if (SockToken
->RemainDataLen
<= Count
) {
1033 RemoveEntryList (&(SockToken
->TokenList
));
1034 SIGNAL_TOKEN (SndToken
, EFI_SUCCESS
);
1035 Count
-= SockToken
->RemainDataLen
;
1036 FreePool (SockToken
);
1039 SockToken
->RemainDataLen
-= Count
;
1045 // to judge if we can process some send token in
1046 // Sock->SndTokenList, if so process those send token
1048 SockProcessSndToken (Sock
);
1054 Called by the low layer protocol to copy some data in socket send
1055 buffer starting from the specific offset to a buffer provided by
1058 @param Sock Pointer to the socket.
1059 @param Offset The start point of the data to be copied.
1060 @param Len The length of the data to be copied.
1061 @param Dest Pointer to the destination to copy the data.
1063 @return The data size copied.
1074 ASSERT ((Sock
!= NULL
) && SockStream
== Sock
->Type
);
1076 return NetbufQueCopy (
1077 Sock
->SndBuffer
.DataQueue
,
1086 Called by the low layer protocol to deliver received data to socket layer.
1088 This function will append the data to the socket receive buffer, set ther
1089 urgent data length and then check if any receive token can be signaled.
1091 @param Sock Pointer to the socket.
1092 @param NetBuffer Pointer to the buffer that contains the received
1094 @param UrgLen The length of the urgent data in the received data.
1100 IN OUT NET_BUF
*NetBuffer
,
1104 ASSERT ((Sock
!= NULL
) && (Sock
->RcvBuffer
.DataQueue
!= NULL
) &&
1105 UrgLen
<= NetBuffer
->TotalSize
);
1107 NET_GET_REF (NetBuffer
);
1109 ((TCP_RSV_DATA
*) (NetBuffer
->ProtoData
))->UrgLen
= UrgLen
;
1111 NetbufQueAppend (Sock
->RcvBuffer
.DataQueue
, NetBuffer
);
1113 SockWakeRcvToken (Sock
);
1119 Get the length of the free space of the specific socket buffer.
1121 @param Sock Pointer to the socket.
1122 @param Which Flag to indicate which socket buffer to check,
1123 either send buffer or receive buffer.
1125 @return The length of the free space, in bytes.
1135 SOCK_BUFFER
*SockBuffer
;
1137 ASSERT ((Sock
!= NULL
) && ((SOCK_SND_BUF
== Which
) || (SOCK_RCV_BUF
== Which
)));
1139 if (SOCK_SND_BUF
== Which
) {
1140 SockBuffer
= &(Sock
->SndBuffer
);
1142 SockBuffer
= &(Sock
->RcvBuffer
);
1145 BufferCC
= (SockBuffer
->DataQueue
)->BufSize
;
1147 if (BufferCC
>= SockBuffer
->HighWater
) {
1152 return SockBuffer
->HighWater
- BufferCC
;
1157 Signal the receive token with the specific error or
1158 set socket error code after error is received.
1160 @param Sock Pointer to the socket.
1161 @param Error The error code received.
1166 IN OUT SOCKET
*Sock
,
1170 SOCK_TOKEN
*SockToken
;
1172 if (!IsListEmpty (&Sock
->RcvTokenList
)) {
1174 SockToken
= NET_LIST_HEAD (
1175 &Sock
->RcvTokenList
,
1180 RemoveEntryList (&SockToken
->TokenList
);
1182 SIGNAL_TOKEN (SockToken
->Token
, Error
);
1184 FreePool (SockToken
);
1187 SOCK_ERROR (Sock
, Error
);
1193 Called by the low layer protocol to indicate that there will be no more data
1194 from the communication peer.
1196 This function set the socket's state to SO_NO_MORE_DATA and signal all queued
1197 IO tokens with the error status EFI_CONNECTION_FIN.
1199 @param Sock Pointer to the socket.
1209 SOCK_NO_MORE_DATA (Sock
);
1211 if (!IsListEmpty (&Sock
->RcvTokenList
)) {
1213 ASSERT (0 == GET_RCV_DATASIZE (Sock
));
1215 Err
= Sock
->SockError
;
1217 SOCK_ERROR (Sock
, EFI_CONNECTION_FIN
);
1219 SockFlushPendingToken (Sock
, &Sock
->RcvTokenList
);
1221 SOCK_ERROR (Sock
, Err
);
1229 Get the first buffer block in the specific socket buffer.
1231 @param Sockbuf Pointer to the socket buffer.
1233 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
1238 IN SOCK_BUFFER
*Sockbuf
1241 LIST_ENTRY
*NetbufList
;
1243 NetbufList
= &(Sockbuf
->DataQueue
->BufList
);
1245 if (IsListEmpty (NetbufList
)) {
1249 return NET_LIST_HEAD (NetbufList
, NET_BUF
, List
);
1254 Get the next buffer block in the specific socket buffer.
1256 @param Sockbuf Pointer to the socket buffer.
1257 @param SockEntry Pointer to the buffer block prior to the required
1260 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
1261 the tail or head entry.
1266 IN SOCK_BUFFER
*Sockbuf
,
1267 IN NET_BUF
*SockEntry
1270 LIST_ENTRY
*NetbufList
;
1272 NetbufList
= &(Sockbuf
->DataQueue
->BufList
);
1274 if ((SockEntry
->List
.ForwardLink
== NetbufList
) ||
1275 (SockEntry
->List
.BackLink
== &SockEntry
->List
) ||
1276 (SockEntry
->List
.ForwardLink
== &SockEntry
->List
)) {
1281 return NET_LIST_USER_STRUCT (SockEntry
->List
.ForwardLink
, NET_BUF
, List
);