2 Implementation of the Socket.
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Get the first buffer block in the specific socket buffer.
21 @param[in] Sockbuf Pointer to the socket buffer.
23 @return Pointer to the first buffer in the queue. NULL if the queue is empty.
28 IN SOCK_BUFFER
*Sockbuf
31 LIST_ENTRY
*NetbufList
;
33 NetbufList
= &(Sockbuf
->DataQueue
->BufList
);
35 if (IsListEmpty (NetbufList
)) {
39 return NET_LIST_HEAD (NetbufList
, NET_BUF
, List
);
43 Get the next buffer block in the specific socket buffer.
45 @param[in] Sockbuf Pointer to the socket buffer.
46 @param[in] SockEntry Pointer to the buffer block prior to the required one.
48 @return Pointer to the buffer block next to SockEntry. NULL if SockEntry is
49 the tail or head entry.
54 IN SOCK_BUFFER
*Sockbuf
,
58 LIST_ENTRY
*NetbufList
;
60 NetbufList
= &(Sockbuf
->DataQueue
->BufList
);
62 if ((SockEntry
->List
.ForwardLink
== NetbufList
) ||
63 (SockEntry
->List
.BackLink
== &SockEntry
->List
) ||
64 (SockEntry
->List
.ForwardLink
== &SockEntry
->List
)
70 return NET_LIST_USER_STRUCT (SockEntry
->List
.ForwardLink
, NET_BUF
, List
);
74 User provided callback function for NetbufFromExt.
76 @param[in] Event The Event this notify function registered to, ignored.
89 Get the length of the data that can be retrieved from the socket
92 @param[in] SockBuffer Pointer to the socket receive buffer.
93 @param[out] IsUrg Pointer to a BOOLEAN variable.
94 If TRUE the data is OOB.
95 @param[in] BufLen The maximum length of the data buffer to
96 store the received data in the socket layer.
98 @return The length of the data can be retreived.
103 IN SOCK_BUFFER
*SockBuffer
,
108 NET_BUF
*RcvBufEntry
;
110 TCP_RSV_DATA
*TcpRsvData
;
113 ASSERT ((SockBuffer
!= NULL
) && (IsUrg
!= NULL
) && (BufLen
> 0));
116 // Get the first socket receive buffer
118 RcvBufEntry
= SockBufFirst (SockBuffer
);
119 ASSERT (RcvBufEntry
!= NULL
);
121 TcpRsvData
= (TCP_RSV_DATA
*) RcvBufEntry
->ProtoData
;
124 // Check whether the receive data is out of bound. If yes, calculate the maximum
125 // allowed length of the urgent data and output it.
127 *IsUrg
= (BOOLEAN
) ((TcpRsvData
->UrgLen
> 0) ? TRUE
: FALSE
);
129 if (*IsUrg
&& (TcpRsvData
->UrgLen
< RcvBufEntry
->TotalSize
)) {
131 DataLen
= MIN (TcpRsvData
->UrgLen
, BufLen
);
133 if (DataLen
< TcpRsvData
->UrgLen
) {
134 TcpRsvData
->UrgLen
= TcpRsvData
->UrgLen
- DataLen
;
136 TcpRsvData
->UrgLen
= 0;
144 // Process the next socket receive buffer to get the maximum allowed length
145 // of the received data.
147 DataLen
= RcvBufEntry
->TotalSize
;
149 RcvBufEntry
= SockBufNext (SockBuffer
, RcvBufEntry
);
151 while ((BufLen
> DataLen
) && (RcvBufEntry
!= NULL
)) {
153 TcpRsvData
= (TCP_RSV_DATA
*) RcvBufEntry
->ProtoData
;
155 Urg
= (BOOLEAN
) ((TcpRsvData
->UrgLen
> 0) ? TRUE
: FALSE
);
161 if (*IsUrg
&& TcpRsvData
->UrgLen
< RcvBufEntry
->TotalSize
) {
163 if (TcpRsvData
->UrgLen
+ DataLen
< BufLen
) {
164 TcpRsvData
->UrgLen
= 0;
166 TcpRsvData
->UrgLen
= TcpRsvData
->UrgLen
- (BufLen
- DataLen
);
169 return MIN (TcpRsvData
->UrgLen
+ DataLen
, BufLen
);
173 DataLen
+= RcvBufEntry
->TotalSize
;
175 RcvBufEntry
= SockBufNext (SockBuffer
, RcvBufEntry
);
178 DataLen
= MIN (BufLen
, DataLen
);
183 Copy data from socket buffer to an application provided receive buffer.
185 @param[in] Sock Pointer to the socket.
186 @param[in] TcpRxData Pointer to the application provided receive buffer.
187 @param[in] RcvdBytes The maximum length of the data can be copied.
188 @param[in] IsUrg If TRUE the data is Out of Bound, FALSE the data is normal.
202 EFI_TCP4_RECEIVE_DATA
*RxData
;
203 EFI_TCP4_FRAGMENT_DATA
*Fragment
;
205 RxData
= (EFI_TCP4_RECEIVE_DATA
*) TcpRxData
;
209 ASSERT (RxData
->DataLength
>= RcvdBytes
);
211 RxData
->DataLength
= RcvdBytes
;
212 RxData
->UrgentFlag
= IsUrg
;
215 // Copy the CopyBytes data from socket receive buffer to RxData.
217 for (Index
= 0; (Index
< RxData
->FragmentCount
) && (RcvdBytes
> 0); Index
++) {
219 Fragment
= &RxData
->FragmentTable
[Index
];
220 CopyBytes
= MIN ((UINT32
) (Fragment
->FragmentLength
), RcvdBytes
);
223 Sock
->RcvBuffer
.DataQueue
,
226 Fragment
->FragmentBuffer
229 Fragment
->FragmentLength
= CopyBytes
;
230 RcvdBytes
-= CopyBytes
;
236 Process the send token.
238 @param[in, out] Sock Pointer to the socket.
242 SockProcessSndToken (
247 SOCK_TOKEN
*SockToken
;
249 SOCK_IO_TOKEN
*SndToken
;
250 EFI_TCP4_TRANSMIT_DATA
*TxData
;
253 ASSERT ((Sock
!= NULL
) && (SockStream
== Sock
->Type
));
255 FreeSpace
= SockGetFreeSpace (Sock
, SOCK_SND_BUF
);
258 // to determine if process a send token using
259 // socket layer flow control policy
261 while ((FreeSpace
>= Sock
->SndBuffer
.LowWater
) && !IsListEmpty (&Sock
->SndTokenList
)) {
263 SockToken
= NET_LIST_HEAD (
264 &(Sock
->SndTokenList
),
270 // process this token
272 RemoveEntryList (&(SockToken
->TokenList
));
274 &(Sock
->ProcessingSndTokenList
),
275 &(SockToken
->TokenList
)
279 // Proceess it in the light of SockType
281 SndToken
= (SOCK_IO_TOKEN
*) SockToken
->Token
;
282 TxData
= SndToken
->Packet
.TxData
;
284 DataLen
= TxData
->DataLength
;
285 Status
= SockProcessTcpSndData (Sock
, TxData
);
287 if (EFI_ERROR (Status
)) {
291 if (DataLen
>= FreeSpace
) {
295 FreeSpace
-= DataLen
;
304 RemoveEntryList (&SockToken
->TokenList
);
305 SIGNAL_TOKEN (SockToken
->Token
, Status
);
306 FreePool (SockToken
);
310 Get received data from the socket layer to the receive token.
312 @param[in, out] Sock Pointer to the socket.
313 @param[in, out] RcvToken Pointer to the application provided receive token.
315 @return The length of data received in this token.
319 SockProcessRcvToken (
321 IN OUT SOCK_IO_TOKEN
*RcvToken
324 UINT32 TokenRcvdBytes
;
325 EFI_TCP4_RECEIVE_DATA
*RxData
;
328 ASSERT (Sock
!= NULL
);
330 ASSERT (SockStream
== Sock
->Type
);
332 RxData
= RcvToken
->Packet
.RxData
;
334 TokenRcvdBytes
= SockTcpDataToRcv (
341 // Copy data from RcvBuffer of socket to user
342 // provided RxData and set the fields in TCP RxData
344 SockSetTcpRxData (Sock
, RxData
, TokenRcvdBytes
, IsUrg
);
346 NetbufQueTrim (Sock
->RcvBuffer
.DataQueue
, TokenRcvdBytes
);
347 SIGNAL_TOKEN (&(RcvToken
->Token
), EFI_SUCCESS
);
349 return TokenRcvdBytes
;
353 Process the TCP send data, buffer the tcp txdata, and append
354 the buffer to socket send buffer, then try to send it.
356 @param[in] Sock Pointer to the socket.
357 @param[in] TcpTxData Pointer to the application provided send buffer.
359 @retval EFI_SUCCESS The operation completed successfully.
360 @retval EFI_OUT_OF_RESOURCES Failed due to resource limits.
364 SockProcessTcpSndData (
371 EFI_TCP4_TRANSMIT_DATA
*TxData
;
373 TxData
= (EFI_TCP4_TRANSMIT_DATA
*) TcpTxData
;
376 // transform this TxData into a NET_BUFFER
377 // and insert it into Sock->SndBuffer
379 SndData
= NetbufFromExt (
380 (NET_FRAGMENT
*) TxData
->FragmentTable
,
381 TxData
->FragmentCount
,
388 if (NULL
== SndData
) {
391 "SockKProcessSndData: Failed to call NetBufferFromExt\n")
394 return EFI_OUT_OF_RESOURCES
;
397 NetbufQueAppend (Sock
->SndBuffer
.DataQueue
, SndData
);
400 // notify the low layer protocol to handle this send token
402 if (TxData
->Urgent
) {
403 Status
= Sock
->ProtoHandler (Sock
, SOCK_SNDURG
, NULL
);
405 if (EFI_ERROR (Status
)) {
411 Status
= Sock
->ProtoHandler (Sock
, SOCK_SNDPUSH
, NULL
);
413 if (EFI_ERROR (Status
)) {
419 // low layer protocol should really handle the sending
420 // process when catching SOCK_SND request
422 Status
= Sock
->ProtoHandler (Sock
, SOCK_SND
, NULL
);
424 if (EFI_ERROR (Status
)) {
432 Flush the tokens in the specific token list.
434 @param[in] Sock Pointer to the socket.
435 @param[in, out] PendingTokenList Pointer to the token list to be flushed.
439 SockFlushPendingToken (
441 IN OUT LIST_ENTRY
*PendingTokenList
444 SOCK_TOKEN
*SockToken
;
445 SOCK_COMPLETION_TOKEN
*Token
;
447 ASSERT ((Sock
!= NULL
) && (PendingTokenList
!= NULL
));
449 while (!IsListEmpty (PendingTokenList
)) {
450 SockToken
= NET_LIST_HEAD (
456 Token
= SockToken
->Token
;
457 SIGNAL_TOKEN (Token
, Sock
->SockError
);
459 RemoveEntryList (&(SockToken
->TokenList
));
460 FreePool (SockToken
);
465 Wake up the connection token while the connection is successfully established,
466 then try to process any pending send token.
468 @param[in, out] Sock Pointer to the socket.
476 ASSERT (Sock
->ConnectionToken
!= NULL
);
478 SIGNAL_TOKEN (Sock
->ConnectionToken
, EFI_SUCCESS
);
479 Sock
->ConnectionToken
= NULL
;
482 // check to see if some pending send token existed?
484 SockProcessSndToken (Sock
);
488 Wake up the listen token while the connection is established successfully.
490 @param[in, out] Sock Pointer to the socket.
494 SockWakeListenToken (
499 SOCK_TOKEN
*SockToken
;
500 EFI_TCP4_LISTEN_TOKEN
*ListenToken
;
502 Parent
= Sock
->Parent
;
504 ASSERT ((Parent
!= NULL
) && SOCK_IS_LISTENING (Parent
) && SOCK_IS_CONNECTED (Sock
));
506 if (!IsListEmpty (&Parent
->ListenTokenList
)) {
507 SockToken
= NET_LIST_HEAD (
508 &Parent
->ListenTokenList
,
513 ListenToken
= (EFI_TCP4_LISTEN_TOKEN
*) SockToken
->Token
;
514 ListenToken
->NewChildHandle
= Sock
->SockHandle
;
516 SIGNAL_TOKEN (&(ListenToken
->CompletionToken
), EFI_SUCCESS
);
518 RemoveEntryList (&SockToken
->TokenList
);
519 FreePool (SockToken
);
521 RemoveEntryList (&Sock
->ConnectionList
);
526 "SockWakeListenToken: accept a socket, now conncnt is %d",
535 Wake up the receive token while some data is received.
537 @param[in, out] Sock Pointer to the socket.
546 UINT32 TokenRcvdBytes
;
547 SOCK_TOKEN
*SockToken
;
548 SOCK_IO_TOKEN
*RcvToken
;
550 ASSERT (Sock
->RcvBuffer
.DataQueue
!= NULL
);
552 RcvdBytes
= (Sock
->RcvBuffer
.DataQueue
)->BufSize
;
554 ASSERT (RcvdBytes
> 0);
556 while (RcvdBytes
> 0 && !IsListEmpty (&Sock
->RcvTokenList
)) {
558 SockToken
= NET_LIST_HEAD (
564 RcvToken
= (SOCK_IO_TOKEN
*) SockToken
->Token
;
565 TokenRcvdBytes
= SockProcessRcvToken (Sock
, RcvToken
);
567 if (0 == TokenRcvdBytes
) {
571 RemoveEntryList (&(SockToken
->TokenList
));
572 FreePool (SockToken
);
573 RcvdBytes
-= TokenRcvdBytes
;
578 Cancel the tokens in the specific token list.
580 @param[in] Token Pointer to the Token. If NULL, all tokens
581 in SpecifiedTokenList will be canceled.
582 @param[in, out] SpecifiedTokenList Pointer to the token list to be checked.
584 @retval EFI_SUCCESS Cancel the tokens in the specific token listsuccessfully.
585 @retval EFI_NOT_FOUND The Token is not found in SpecifiedTokenList.
590 IN SOCK_COMPLETION_TOKEN
*Token
,
591 IN OUT LIST_ENTRY
*SpecifiedTokenList
597 SOCK_TOKEN
*SockToken
;
599 Status
= EFI_SUCCESS
;
604 if (IsListEmpty (SpecifiedTokenList
) && Token
!= NULL
) {
605 return EFI_NOT_FOUND
;
609 // Iterate through the SpecifiedTokenList.
611 Entry
= SpecifiedTokenList
->ForwardLink
;
612 while (Entry
!= SpecifiedTokenList
) {
613 SockToken
= NET_LIST_USER_STRUCT (Entry
, SOCK_TOKEN
, TokenList
);
616 SIGNAL_TOKEN (SockToken
->Token
, EFI_ABORTED
);
617 RemoveEntryList (&SockToken
->TokenList
);
618 FreePool (SockToken
);
620 Entry
= SpecifiedTokenList
->ForwardLink
;
621 Status
= EFI_SUCCESS
;
623 if (Token
== (VOID
*) SockToken
->Token
) {
624 SIGNAL_TOKEN (Token
, EFI_ABORTED
);
625 RemoveEntryList (&(SockToken
->TokenList
));
626 FreePool (SockToken
);
631 Status
= EFI_NOT_FOUND
;
633 Entry
= Entry
->ForwardLink
;
637 ASSERT (IsListEmpty (SpecifiedTokenList
) || Token
!= NULL
);
643 Create a socket with initial data SockInitData.
645 @param[in] SockInitData Pointer to the initial data of the socket.
647 @return Pointer to the newly created socket, return NULL when an exception occurs.
652 IN SOCK_INIT_DATA
*SockInitData
658 EFI_GUID
*TcpProtocolGuid
;
659 UINTN ProtocolLength
;
661 ASSERT ((SockInitData
!= NULL
) && (SockInitData
->ProtoHandler
!= NULL
));
662 ASSERT (SockInitData
->Type
== SockStream
);
663 ASSERT ((SockInitData
->ProtoData
!= NULL
) && (SockInitData
->DataSize
<= PROTO_RESERVED_LEN
));
665 if (SockInitData
->IpVersion
== IP_VERSION_4
) {
666 TcpProtocolGuid
= &gEfiTcp4ProtocolGuid
;
667 ProtocolLength
= sizeof (EFI_TCP4_PROTOCOL
);
669 TcpProtocolGuid
= &gEfiTcp6ProtocolGuid
;
670 ProtocolLength
= sizeof (EFI_TCP6_PROTOCOL
);
674 Parent
= SockInitData
->Parent
;
676 if ((Parent
!= NULL
) && (Parent
->ConnCnt
== Parent
->BackLog
)) {
679 "SockCreate: Socket parent has reached its connection limit with %d ConnCnt and %d BackLog\n",
687 Sock
= AllocateZeroPool (sizeof (SOCKET
));
690 DEBUG ((EFI_D_ERROR
, "SockCreate: No resource to create a new socket\n"));
694 InitializeListHead (&Sock
->Link
);
695 InitializeListHead (&Sock
->ConnectionList
);
696 InitializeListHead (&Sock
->ListenTokenList
);
697 InitializeListHead (&Sock
->RcvTokenList
);
698 InitializeListHead (&Sock
->SndTokenList
);
699 InitializeListHead (&Sock
->ProcessingSndTokenList
);
701 EfiInitializeLock (&(Sock
->Lock
), TPL_CALLBACK
);
703 Sock
->SndBuffer
.DataQueue
= NetbufQueAlloc ();
704 if (NULL
== Sock
->SndBuffer
.DataQueue
) {
707 "SockCreate: No resource to allocate SndBuffer for new socket\n")
713 Sock
->RcvBuffer
.DataQueue
= NetbufQueAlloc ();
714 if (NULL
== Sock
->RcvBuffer
.DataQueue
) {
717 "SockCreate: No resource to allocate RcvBuffer for new socket\n")
723 Sock
->Signature
= SOCK_SIGNATURE
;
725 Sock
->Parent
= Parent
;
726 Sock
->BackLog
= SockInitData
->BackLog
;
727 Sock
->ProtoHandler
= SockInitData
->ProtoHandler
;
728 Sock
->SndBuffer
.HighWater
= SockInitData
->SndBufferSize
;
729 Sock
->RcvBuffer
.HighWater
= SockInitData
->RcvBufferSize
;
730 Sock
->Type
= SockInitData
->Type
;
731 Sock
->DriverBinding
= SockInitData
->DriverBinding
;
732 Sock
->State
= SockInitData
->State
;
733 Sock
->CreateCallback
= SockInitData
->CreateCallback
;
734 Sock
->DestroyCallback
= SockInitData
->DestroyCallback
;
735 Sock
->Context
= SockInitData
->Context
;
737 Sock
->SockError
= EFI_ABORTED
;
738 Sock
->SndBuffer
.LowWater
= SOCK_BUFF_LOW_WATER
;
739 Sock
->RcvBuffer
.LowWater
= SOCK_BUFF_LOW_WATER
;
741 Sock
->IpVersion
= SockInitData
->IpVersion
;
744 // Install protocol on Sock->SockHandle
746 CopyMem (&Sock
->NetProtocol
, SockInitData
->Protocol
, ProtocolLength
);
749 // copy the protodata into socket
751 CopyMem (Sock
->ProtoReserved
, SockInitData
->ProtoData
, SockInitData
->DataSize
);
753 Status
= gBS
->InstallMultipleProtocolInterfaces (
760 if (EFI_ERROR (Status
)) {
763 "SockCreate: Install TCP protocol in socket failed with %r\n",
770 if (Parent
!= NULL
) {
771 ASSERT (Parent
->BackLog
> 0);
772 ASSERT (SOCK_IS_LISTENING (Parent
));
775 // need to add it into Parent->ConnectionList
776 // if the Parent->ConnCnt < Parent->BackLog
782 "SockCreate: Create a new socket and add to parent, now conncnt is %d\n",
786 InsertTailList (&Parent
->ConnectionList
, &Sock
->ConnectionList
);
789 if (Sock
->CreateCallback
!= NULL
) {
790 Status
= Sock
->CreateCallback (Sock
, Sock
->Context
);
791 if (EFI_ERROR (Status
)) {
800 if (Sock
->SockHandle
!= NULL
) {
801 gBS
->UninstallMultipleProtocolInterfaces (
809 if (NULL
!= Sock
->SndBuffer
.DataQueue
) {
810 NetbufQueFree (Sock
->SndBuffer
.DataQueue
);
813 if (NULL
!= Sock
->RcvBuffer
.DataQueue
) {
814 NetbufQueFree (Sock
->RcvBuffer
.DataQueue
);
825 @param[in, out] Sock Pointer to the socket.
834 EFI_GUID
*TcpProtocolGuid
;
837 ASSERT (SockStream
== Sock
->Type
);
839 if (Sock
->DestroyCallback
!= NULL
) {
840 Sock
->DestroyCallback (Sock
, Sock
->Context
);
844 // Flush the completion token buffered
845 // by sock and rcv, snd buffer
847 if (!SOCK_IS_UNCONFIGURED (Sock
)) {
849 SockConnFlush (Sock
);
850 SockSetState (Sock
, SO_CLOSED
);
851 Sock
->ConfigureState
= SO_UNCONFIGURED
;
855 // Destroy the RcvBuffer Queue and SendBuffer Queue
857 NetbufQueFree (Sock
->RcvBuffer
.DataQueue
);
858 NetbufQueFree (Sock
->SndBuffer
.DataQueue
);
861 // Remove it from parent connection list if needed
863 if (Sock
->Parent
!= NULL
) {
865 RemoveEntryList (&(Sock
->ConnectionList
));
866 (Sock
->Parent
->ConnCnt
)--;
870 "SockDestroy: Delete a unaccepted socket from parent now conncnt is %d\n",
871 Sock
->Parent
->ConnCnt
)
878 // Set the protocol guid and driver binding handle
879 // in the light of Sock->SockType
881 if (Sock
->IpVersion
== IP_VERSION_4
) {
882 TcpProtocolGuid
= &gEfiTcp4ProtocolGuid
;
884 TcpProtocolGuid
= &gEfiTcp6ProtocolGuid
;
888 // Retrieve the protocol installed on this sock
890 Status
= gBS
->OpenProtocol (
896 EFI_OPEN_PROTOCOL_GET_PROTOCOL
899 if (EFI_ERROR (Status
)) {
903 "SockDestroy: Open protocol installed on socket failed with %r\n",
911 // Uninstall the protocol installed on this sock
912 // in the light of Sock->SockType
914 gBS
->UninstallMultipleProtocolInterfaces (
927 Flush the sndBuffer and rcvBuffer of socket.
929 @param[in, out] Sock Pointer to the socket.
939 ASSERT (Sock
!= NULL
);
942 // Clear the flag in this socket
947 // Flush the SndBuffer and RcvBuffer of Sock
949 NetbufQueFlush (Sock
->SndBuffer
.DataQueue
);
950 NetbufQueFlush (Sock
->RcvBuffer
.DataQueue
);
953 // Signal the pending token
955 if (Sock
->ConnectionToken
!= NULL
) {
956 SIGNAL_TOKEN (Sock
->ConnectionToken
, Sock
->SockError
);
957 Sock
->ConnectionToken
= NULL
;
960 if (Sock
->CloseToken
!= NULL
) {
961 SIGNAL_TOKEN (Sock
->CloseToken
, Sock
->SockError
);
962 Sock
->CloseToken
= NULL
;
965 SockFlushPendingToken (Sock
, &(Sock
->ListenTokenList
));
966 SockFlushPendingToken (Sock
, &(Sock
->RcvTokenList
));
967 SockFlushPendingToken (Sock
, &(Sock
->SndTokenList
));
968 SockFlushPendingToken (Sock
, &(Sock
->ProcessingSndTokenList
));
971 // Destroy the pending connection, if it is a listening socket
973 if (SOCK_IS_LISTENING (Sock
)) {
974 while (!IsListEmpty (&Sock
->ConnectionList
)) {
975 Child
= NET_LIST_HEAD (
976 &Sock
->ConnectionList
,
981 SockDestroyChild (Child
);
990 Set the state of the socket.
992 @param[in, out] Sock Pointer to the socket.
993 @param[in] State The new socket state to be set.
1002 Sock
->State
= State
;
1006 Clone a new socket, including its associated protocol control block.
1008 @param[in] Sock Pointer to the socket to be cloned.
1010 @return Pointer to the newly cloned socket. If NULL, an error condition occurred.
1019 SOCK_INIT_DATA InitData
;
1021 InitData
.BackLog
= Sock
->BackLog
;
1022 InitData
.Parent
= Sock
;
1023 InitData
.State
= Sock
->State
;
1024 InitData
.ProtoHandler
= Sock
->ProtoHandler
;
1025 InitData
.Type
= Sock
->Type
;
1026 InitData
.RcvBufferSize
= Sock
->RcvBuffer
.HighWater
;
1027 InitData
.SndBufferSize
= Sock
->SndBuffer
.HighWater
;
1028 InitData
.DriverBinding
= Sock
->DriverBinding
;
1029 InitData
.IpVersion
= Sock
->IpVersion
;
1030 InitData
.Protocol
= &(Sock
->NetProtocol
);
1031 InitData
.CreateCallback
= Sock
->CreateCallback
;
1032 InitData
.DestroyCallback
= Sock
->DestroyCallback
;
1033 InitData
.Context
= Sock
->Context
;
1034 InitData
.ProtoData
= Sock
->ProtoReserved
;
1035 InitData
.DataSize
= sizeof (Sock
->ProtoReserved
);
1037 ClonedSock
= SockCreate (&InitData
);
1039 if (NULL
== ClonedSock
) {
1040 DEBUG ((EFI_D_ERROR
, "SockClone: no resource to create a cloned sock\n"));
1044 SockSetState (ClonedSock
, SO_CONNECTING
);
1045 ClonedSock
->ConfigureState
= Sock
->ConfigureState
;
1051 Called by the low layer protocol to indicate the socket a connection is
1054 This function just changes the socket's state to SO_CONNECTED
1055 and signals the token used for connection establishment.
1057 @param[in, out] Sock Pointer to the socket associated with the
1058 established connection.
1062 SockConnEstablished (
1067 ASSERT (SO_CONNECTING
== Sock
->State
);
1069 SockSetState (Sock
, SO_CONNECTED
);
1071 if (NULL
== Sock
->Parent
) {
1072 SockWakeConnToken (Sock
);
1074 SockWakeListenToken (Sock
);
1080 Called by the low layer protocol to indicate the connection is closed.
1082 This function flushes the socket, sets the state to SO_CLOSED, and signals
1085 @param[in, out] Sock Pointer to the socket associated with the closed
1094 if (Sock
->CloseToken
!= NULL
) {
1095 SIGNAL_TOKEN (Sock
->CloseToken
, EFI_SUCCESS
);
1096 Sock
->CloseToken
= NULL
;
1099 SockConnFlush (Sock
);
1100 SockSetState (Sock
, SO_CLOSED
);
1102 if (Sock
->Parent
!= NULL
) {
1103 SockDestroyChild (Sock
);
1109 Called by low layer protocol to indicate that some data was sent or processed.
1111 This function trims the sent data in the socket send buffer, and signals the data
1114 @param[in, out] Sock Pointer to the socket.
1115 @param[in] Count The length of the data processed or sent, in bytes.
1120 IN OUT SOCKET
*Sock
,
1124 SOCK_TOKEN
*SockToken
;
1125 SOCK_COMPLETION_TOKEN
*SndToken
;
1127 ASSERT (!IsListEmpty (&Sock
->ProcessingSndTokenList
));
1128 ASSERT (Count
<= (Sock
->SndBuffer
.DataQueue
)->BufSize
);
1130 NetbufQueTrim (Sock
->SndBuffer
.DataQueue
, Count
);
1133 // To check if we can signal some snd token in this socket
1136 SockToken
= NET_LIST_HEAD (
1137 &(Sock
->ProcessingSndTokenList
),
1142 SndToken
= SockToken
->Token
;
1144 if (SockToken
->RemainDataLen
<= Count
) {
1146 RemoveEntryList (&(SockToken
->TokenList
));
1147 SIGNAL_TOKEN (SndToken
, EFI_SUCCESS
);
1148 Count
-= SockToken
->RemainDataLen
;
1149 FreePool (SockToken
);
1152 SockToken
->RemainDataLen
-= Count
;
1158 // to judge if we can process some send token in
1159 // Sock->SndTokenList, if so process those send token
1161 SockProcessSndToken (Sock
);
1165 Called by the low layer protocol to copy some data in the socket send
1166 buffer starting from the specific offset to a buffer provided by
1169 @param[in] Sock Pointer to the socket.
1170 @param[in] Offset The start point of the data to be copied.
1171 @param[in] Len The length of the data to be copied.
1172 @param[out] Dest Pointer to the destination to copy the data.
1174 @return The data size copied.
1185 ASSERT ((Sock
!= NULL
) && SockStream
== Sock
->Type
);
1187 return NetbufQueCopy (
1188 Sock
->SndBuffer
.DataQueue
,
1196 Called by the low layer protocol to deliver received data to socket layer.
1198 This function will append the data to the socket receive buffer, set the
1199 urgent data length, and then check if any receive token can be signaled.
1201 @param[in, out] Sock Pointer to the socket.
1202 @param[in, out] NetBuffer Pointer to the buffer that contains the received data.
1203 @param[in] UrgLen The length of the urgent data in the received data.
1208 IN OUT SOCKET
*Sock
,
1209 IN OUT NET_BUF
*NetBuffer
,
1213 ASSERT ((Sock
!= NULL
) && (Sock
->RcvBuffer
.DataQueue
!= NULL
) &&
1214 UrgLen
<= NetBuffer
->TotalSize
);
1216 NET_GET_REF (NetBuffer
);
1218 ((TCP_RSV_DATA
*) (NetBuffer
->ProtoData
))->UrgLen
= UrgLen
;
1220 NetbufQueAppend (Sock
->RcvBuffer
.DataQueue
, NetBuffer
);
1222 SockWakeRcvToken (Sock
);
1226 Get the length of the free space of the specific socket buffer.
1228 @param[in] Sock Pointer to the socket.
1229 @param[in] Which Flag to indicate which socket buffer to check:
1230 either send buffer or receive buffer.
1232 @return The length of the free space, in bytes.
1242 SOCK_BUFFER
*SockBuffer
;
1244 ASSERT (Sock
!= NULL
&& ((SOCK_SND_BUF
== Which
) || (SOCK_RCV_BUF
== Which
)));
1246 if (SOCK_SND_BUF
== Which
) {
1247 SockBuffer
= &(Sock
->SndBuffer
);
1249 SockBuffer
= &(Sock
->RcvBuffer
);
1252 BufferCC
= (SockBuffer
->DataQueue
)->BufSize
;
1254 if (BufferCC
>= SockBuffer
->HighWater
) {
1259 return SockBuffer
->HighWater
- BufferCC
;
1263 Called by the low layer protocol to indicate that there will be no more data
1264 from the communication peer.
1266 This function sets the socket's state to SO_NO_MORE_DATA and signals all queued
1267 IO tokens with the error status EFI_CONNECTION_FIN.
1269 @param[in, out] Sock Pointer to the socket.
1279 SOCK_NO_MORE_DATA (Sock
);
1281 if (!IsListEmpty (&Sock
->RcvTokenList
)) {
1283 ASSERT (0 == GET_RCV_DATASIZE (Sock
));
1285 Err
= Sock
->SockError
;
1287 SOCK_ERROR (Sock
, EFI_CONNECTION_FIN
);
1289 SockFlushPendingToken (Sock
, &Sock
->RcvTokenList
);
1291 SOCK_ERROR (Sock
, Err
);