2 Implement the socket support for the socket layer.
5 * Bound - pSocket->PortList is not NULL
6 * Listen - AcceptWait event is not NULL
8 Copyright (c) 2011, Intel Corporation
9 All rights reserved. This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 \section DataStructures Data Structures
22 +-------------+ +-------------+ +-------------+
23 Service Lists | ::ESL_SERVICE |-->| ESL_SERVICE |-->| ESL_SERVICE |--> NULL (pNext)
24 +-------------+ +-------------+ +-------------+
26 pUdp4List ^ | pTcp4List | |
31 | ::ESL_LAYER | ::mEslLayer | |
35 +-------------+ +-------------+ +-------------+
36 | ::ESL_SOCKET |-->| ::ESL_PORT |-->| ESL_PORT |--> NULL (pLinkSocket)
37 +-------------+ +-------------+ +-------------+
41 +-------------+ +-------------+
42 | ESL_SOCKET |-->| ESL_PORT |--> NULL
43 +-------------+ +-------------+
47 (pNext) | | | | (pLinkService)
48 | | | | pRxPacketListHead
49 | | | `-----------------------------------------------.
50 | | | pRxOobPacketListHead |
51 | | `--------------------------------. |
52 | | pTxPacketListHead | |
53 | `---------------. | |
54 pTxOobPacketListHead | | | |
56 +------------+ +------------+ +------------+ +------------+
57 | ::ESL_PACKET | | ESL_PACKET | | ESL_PACKET | | ESL_PACKET |
58 +------------+ +------------+ +------------+ +------------+
61 +------------+ +------------+ +------------+ +------------+
62 | ESL_PACKET | | ESL_PACKET | | ESL_PACKET | | ESL_PACKET |
63 +------------+ +------------+ +------------+ +------------+
71 ::mEslLayer is the one and only ::ESL_LAYER structure. It connects directly or
72 indirectly to the other data structures. The ESL_LAYER structure has a unique
73 service list for each of the network protocol interfaces.
75 ::ESL_SERVICE manages the network interfaces for a given transport type (IP4, TCP4, UDP4, etc.)
77 ::ESL_SOCKET manages the activity for a single socket instance. As such, it contains
78 the ::EFI_SOCKET_PROTOCOL structure which the BSD socket library uses as the object
79 reference and the API into the EFI socket library.
81 ::ESL_PORT manages the connection with a single instance of the lower layer network.
82 This structure is the socket equivalent of an IP connection or a TCP or UDP port.
84 ::ESL_PACKET buffers data for transmit and receive. There are four queues connected
85 to the ::ESL_SOCKET that manage the data:
87 <li>ESL_SOCKET::pRxPacketListHead - Normal (low) priority receive data</li>
88 <li>ESL_SOCKET::pRxOobPacketListHead - High (out-of-band or urgent) priority receive data</li>
89 <li>ESL_SOCKET::pTxPacketListHead - Normal (low) priority transmit data</li>
90 <li>ESL_SOCKET::pTxOobPacketListHead - High (out-of-band or urgent) priority transmit data</li>
92 The selection of the transmit queue is controlled by the MSG_OOB flag on the transmit
93 request as well as the socket option SO_OOBINLINE. The receive queue is selected by
94 the URGENT data flag for TCP and the setting of the socket option SO_OOBINLINE.
96 Data structure synchronization is done by raising TPL to TPL_SOCKET. Modifying
97 critical elements within the data structures must be done at this TPL. TPL is then
98 restored to the previous level. Note that the code verifies that all callbacks are
99 entering at TPL_SOCKETS for proper data structure synchronization.
101 \section PortCloseStateMachine Port Close State Machine
103 The port close state machine walks the port through the necessary
104 states to stop activity on the port and get it into a state where
105 the resources may be released. The state machine consists of the
106 following arcs and states:
110 +--------------------------+
112 +--------------------------+
114 | ::EslSocketPortCloseStart
116 +--------------------------+
117 | PORT_STATE_CLOSE_STARTED |
118 +--------------------------+
120 | ::EslSocketPortCloseTxDone
122 +--------------------------+
123 | PORT_STATE_CLOSE_TX_DONE |
124 +--------------------------+
126 | ::EslSocketPortCloseComplete
128 +--------------------------+
129 | PORT_STATE_CLOSE_DONE |
130 +--------------------------+
132 | ::EslSocketPortCloseRxDone
134 +--------------------------+
135 | PORT_STATE_CLOSE_RX_DONE |
136 +--------------------------+
138 | ::EslSocketPortClose
140 +--------------------------+
142 +--------------------------+
147 <li>Arc: ::EslSocketPortCloseStart - Marks the port as closing and
148 initiates the port close operation</li>
149 <li>State: PORT_STATE_CLOSE_STARTED</li>
150 <li>Arc: ::EslSocketPortCloseTxDone - Waits until all of the transmit
151 operations to complete. After all of the transmits are complete,
152 this routine initiates the network specific close operation by calling
153 through ESL_PROTOCOL_API::pfnPortCloseOp. One such routine is
154 ::EslTcp4PortCloseOp.
156 <li>State: PORT_STATE_CLOSE_TX_DONE</li>
157 <li>Arc: ::EslSocketPortCloseComplete - Called when the close operation is
158 complete. After the transition to PORT_STATE_CLOSE_DONE,
159 this routine calls ::EslSocketRxCancel to abort the pending receive operations.
161 <li>State: PORT_STATE_CLOSE_DONE</li>
162 <li>Arc: ::EslSocketPortCloseRxDone - Waits until all of the receive
163 operation have been cancelled. After the transition to
164 PORT_STATE_CLOSE_RX_DONE, this routine calls ::EslSocketPortClose.
166 <li>State: PORT_STATE_CLOSE_RX_DONE</li>
167 <li>Arc: ::EslSocketPortClose - This routine discards any receive buffers
168 using a network specific support routine via ESL_PROTOCOL_API::pfnPacketFree.
169 This routine then releases the port resources allocated by ::EslSocketPortAllocate
170 and calls the network specific port close routine (e.g. ::EslTcp4PortClose)
171 via ESL_PROTOCOL_API::pfnPortClose to release any network specific resources.
176 \section ReceiveEngine Receive Engine
178 The receive path accepts data from the network and queues (buffers) it for the
179 application. Flow control is applied once a maximum amount of buffering is reached
180 and is released when the buffer usage drops below that limit. Eventually the
181 application requests data from the socket which removes entries from the queue and
184 The receive engine is the state machine which reads data from the network and
185 fills the queue with received packets. The receive engine uses two data structures
186 to manage the network receive opeations and the buffers.
188 At a high level, the ::ESL_IO_MGMT structures are managing the tokens and
189 events for the interface to the UEFI network stack. The ::ESL_PACKET
190 structures are managing the receive data buffers. The receive engine
191 connects these two structures in the network specific receive completion
211 The ::ESL_IO_MGMT structures are allocated as part of the ::ESL_PORT structure in
212 ::EslSocketPortAllocate. The ESL_IO_MGMT structures are separated and placed on
213 the free list by calling ::EslSocketIoInit. The ESL_IO_MGMT structure contains
214 the network layer specific receive completion token and event. The receive engine
215 is eventually shutdown by ::EslSocketPortCloseTxDone and the resources in these
216 structures are released in ::EslSocketPortClose by a call to ::EslSocketIoFree.
223 +-------------+ +-------------+ +-------------+
224 Active | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
225 +-------------+ +-------------+ +-------------+
227 +-------------+ +-------------+ +-------------+
228 Free | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
229 +-------------+ +-------------+ +-------------+
235 The receive engine is started by calling ::EslSocketRxStart. Flow control pauses
236 the receive engine by stopping the calls to EslSocketRxStart when the amount of
237 receive data waiting for the application meets or exceeds MAX_RX_DATA. After
238 the application reads enough data that the amount of buffering drops below this
239 limit, the calls to EslSockeRxStart continue which releases the flow control.
241 Receive flow control is applied when the port is created, since no receive
242 operation are pending to the low layer network driver. The flow control gets
243 released when the low layer network port is configured or the first receive
244 operation is posted. Flow control remains in the released state until the
245 maximum buffer space is consumed. During this time, ::EslSocketRxComplete
246 calls ::EslSocketRxStart. Flow control is applied in EslSocketRxComplete
247 by skipping the call to EslSocketRxStart. Flow control is eventually
248 released in ::EslSocketReceive when the buffer space drops below the
249 maximum amount causing EslSocketReceive to call EslSocketRxStart.
253 +------------+ +------------+
254 High .----->| ESL_PACKET |-->| ESL_PACKET |--> NULL (pNext)
255 Priority | +------------+ +------------+
257 | pRxOobPacketListHead
263 Priority | +------------+ +------------+ +------------+
264 `----->| ::ESL_PACKET |-->| ESL_PACKET |-->| ESL_PACKET |--> NULL
265 +------------+ +------------+ +------------+
269 ::EslSocketRxStart connects an ::ESL_PACKET structure to the ::ESL_IO_MGMT structure
270 and then calls the network layer to start the receive operation. Upon
271 receive completion, ::EslSocketRxComplete breaks the connection between these
272 structrues and places the ESL_IO_MGMT structure onto the ESL_PORT::pRxFree list to
273 make token and event available for another receive operation. EslSocketRxComplete
274 then queues the ESL_PACKET structure (data packet) to either the
275 ESL_SOCKET::pRxOobPacketListTail or ESL_SOCKET::pRxPacketListTail depending on
276 whether urgent or normal data was received. Finally ::EslSocketRxComplete attempts
277 to start another receive operation.
281 Setup for IP4 and UDP4
283 +--------------------+
289 +----+---------------+
292 +--------------------+
297 +----+---------------+
299 Completion for IP4 and UDP4
301 +--------------------+ +----------------------+
302 | ESL_IO_MGMT | | Data Buffer |
303 | | | (Driver owned) |
304 | +---------------+ +----------------------+
307 | | | +----------------------+
308 | | RxData --> | EFI_IP4_RECEIVE_DATA |
309 +----+---------------+ | (Driver owned) |
310 | +----------------------+
312 +--------------------+ .
315 | +---------------+ .
316 | | pRxData --> NULL .......
317 +----+---------------+
320 Setup and completion for TCP4
322 +--------------------+ +--------------------------+
323 | ESL_IO_MGMT |-->| ESL_PACKET |
325 | +---------------+ +----------------------+ |
326 | | Token | | EFI_IP4_RECEIVE_DATA | |
328 | | | +----------------------+---+
329 | | Event | | Data Buffer |
330 +----+---------------+ | |
332 +--------------------------+
336 To minimize the number of buffer copies, the data is not copied until the
337 application makes a receive call. At this point socket performs a single copy
338 in the receive path to move the data from the buffer filled by the network layer
339 into the application's buffer.
341 The IP4 and UDP4 drivers go one step further to reduce buffer copies. They
342 allow the socket layer to hold on to the actual receive buffer until the
343 application has performed a receive operation or closes the socket. Both
344 of theses operations return the buffer to the lower layer network driver
345 by calling ESL_PROTOCOL_API::pfnPacketFree.
347 When a socket application wants to receive data it indirectly calls
348 ::EslSocketReceive to remove data from one of the receive data queues. This routine
349 removes the next available packet from ESL_SOCKET::pRxOobPacketListHead or
350 ESL_SOCKET::pRxPacketListHead and copies the data from the packet
351 into the application's buffer. For SOCK_STREAM sockets, if the packet
352 contains more data then the ESL_PACKET structures remains at the head of the
353 receive queue for the next application receive
354 operation. For SOCK_DGRAM, SOCK_RAW and SOCK_SEQ_PACKET sockets, the ::ESL_PACKET
355 structure is removed from the head of the receive queue and any remaining data is
356 discarded as the packet is placed on the free queue.
358 During socket layer shutdown, ::EslSocketShutdown calls ::EslSocketRxCancel to
359 cancel any pending receive operations. EslSocketRxCancel calls the network specific
360 cancel routine using ESL_PORT::pfnRxCancel.
363 \section TransmitEngine Transmit Engine
365 Application calls to ::EslSocketTransmit cause data to be copied into a buffer.
366 The buffer exists as an extension to an ESL_PACKET structure and the structure
367 is placed at the end of the transmit queue.
371 *ppQueueHead: pSocket->pRxPacketListHead or pSocket->pRxOobPacketListHead
374 +------------+ +------------+ +------------+
375 Data | ESL_PACKET |-->| ESL_PACKET |-->| ESL_PACKET |--> NULL
376 +------------+ +------------+ +------------+
379 *ppQueueTail: pSocket->pRxPacketListTail or pSocket->pRxOobPacketListTail
383 There are actually two transmit queues the normal or low priority queue which is
384 the default and the urgent or high priority queue which is addressed by specifying
385 the MSG_OOB flag during the transmit request. Associated with each queue is a
386 transmit engine which is responsible for sending the data in that queue.
388 The transmit engine is the state machine which removes entries from the head
389 of the transmit queue and causes the data to be sent over the network.
393 +--------------------+ +--------------------+
394 | ESL_IO_MGMT | | ESL_PACKET |
396 | +---------------+ +----------------+ |
397 | | Token | | Buffer Length | |
398 | | TxData --> | Buffer Address | |
399 | | | +----------------+---+
400 | | Event | | Data Buffer |
401 +----+---------------+ | |
402 +--------------------+
405 At a high level, the transmit engine uses a couple of data structures
406 to manage the data flow. The ::ESL_IO_MGMT structures manage the tokens and
407 events for the interface to the UEFI network stack. The ::ESL_PACKET
408 structures manage the data buffers that get sent. The transmit
409 engine connects these two structures prior to transmission and disconnects
410 them upon completion.
414 pPort->pTxActive or pTxOobActive
417 +-------------+ +-------------+ +-------------+
418 Active | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
419 +-------------+ +-------------+ +-------------+
421 +-------------+ +-------------+ +-------------+
422 Free | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
423 +-------------+ +-------------+ +-------------+
426 pPort->pTxFree or pTxOobFree
430 The transmit engine manages multiple transmit operations using the
431 active and free lists shown above. ::EslSocketPortAllocate allocates the
432 ::ESL_IO_MGMT structures as an extension to the ::ESL_PORT structure.
433 This routine places the ESL_IO_MGMT structures on the free list by calling
434 ::EslSocketIoInit. During their lifetime, the ESL_IO_MGMT structures
435 will move from the free list to the active list and back again. The
436 active list contains the packets that are actively being processed by
437 the UEFI network stack. Eventually the ESL_IO_MGMT structures will be
438 removed from the free list and be deallocated by the EslSocketPortClose
441 The network specific code calls the ::EslSocketTxStart routine
442 to hand a packet to the network stack. EslSocketTxStart connects
443 the transmit packet (::ESL_PACKET) to an ::ESL_IO_MGMT structure
444 and then queues the result to one of the active lists:
445 ESL_PORT::pTxActive or ESL_PORT::pTxOobActive. The routine then
446 hands the packet to the network stack.
448 Upon completion, the network specific TxComplete routine calls
449 ::EslSocketTxComplete to disconnect the transmit packet from the
450 ESL_IO_MGMT structure and frees the ::ESL_PACKET structure by calling
451 ::EslSocketPacketFree. The routine places the ::ESL_IO_MGMT structure
452 into the free list either ESL_PORT::pTxFree or ESL_PORT::pTxOobFree.
453 EslSocketTxComplete then starts the next transmit operation while
454 the socket is active or calls the ::EslSocketPortCloseTxDone routine
455 when the socket is shutting down.
463 Socket driver connection points
465 List the network stack connection points for the socket driver.
467 CONST ESL_SOCKET_BINDING cEslSocketBinding
[] = {
469 &gEfiIp4ServiceBindingProtocolGuid
,
470 &gEfiIp4ProtocolGuid
,
472 OFFSET_OF ( ESL_LAYER
, pIp4List
),
475 0 }, // TX Oob buffers
477 &gEfiTcp4ServiceBindingProtocolGuid
,
478 &gEfiTcp4ProtocolGuid
,
479 &mEslTcp4ServiceGuid
,
480 OFFSET_OF ( ESL_LAYER
, pTcp4List
),
483 4 }, // TX Oob buffers
485 &gEfiTcp6ServiceBindingProtocolGuid
,
486 &gEfiTcp6ProtocolGuid
,
487 &mEslTcp6ServiceGuid
,
488 OFFSET_OF ( ESL_LAYER
, pTcp6List
),
491 4 }, // TX Oob buffers
493 &gEfiUdp4ServiceBindingProtocolGuid
,
494 &gEfiUdp4ProtocolGuid
,
495 &mEslUdp4ServiceGuid
,
496 OFFSET_OF ( ESL_LAYER
, pUdp4List
),
499 0 }, // TX Oob buffers
501 &gEfiUdp6ServiceBindingProtocolGuid
,
502 &gEfiUdp6ProtocolGuid
,
503 &mEslUdp6ServiceGuid
,
504 OFFSET_OF ( ESL_LAYER
, pUdp6List
),
507 0 } // TX Oob buffers
510 CONST UINTN cEslSocketBindingEntries
= DIM ( cEslSocketBinding
);
513 APIs to support the various socket types for the v4 network stack.
515 CONST ESL_PROTOCOL_API
* cEslAfInetApi
[] = {
517 &cEslTcp4Api
, // SOCK_STREAM
518 &cEslUdp4Api
, // SOCK_DGRAM
519 &cEslIp4Api
, // SOCK_RAW
521 &cEslTcp4Api
// SOCK_SEQPACKET
525 Number of entries in the v4 API array ::cEslAfInetApi.
527 CONST
int cEslAfInetApiSize
= DIM ( cEslAfInetApi
);
531 APIs to support the various socket types for the v6 network stack.
533 CONST ESL_PROTOCOL_API
* cEslAfInet6Api
[] = {
535 &cEslTcp6Api
, // SOCK_STREAM
536 &cEslUdp6Api
, // SOCK_DGRAM
539 &cEslTcp6Api
// SOCK_SEQPACKET
543 Number of entries in the v6 API array ::cEslAfInet6Api.
545 CONST
int cEslAfInet6ApiSize
= DIM ( cEslAfInet6Api
);
549 Global management structure for the socket layer.
555 Initialize an endpoint for network communication.
557 This routine initializes the communication endpoint.
559 The ::socket routine calls this routine indirectly to create
560 the communication endpoint.
562 @param [in] pSocketProtocol Address of the socket protocol structure.
563 @param [in] domain Select the family of protocols for the client or server
564 application. See the ::socket documentation for values.
565 @param [in] type Specifies how to make the network connection.
566 See the ::socket documentation for values.
567 @param [in] protocol Specifies the lower layer protocol to use.
568 See the ::socket documentation for values.
569 @param [out] pErrno Address to receive the errno value upon completion.
571 @retval EFI_SUCCESS - Socket successfully created
572 @retval EFI_INVALID_PARAMETER - Invalid domain value, errno = EAFNOSUPPORT
573 @retval EFI_INVALID_PARAMETER - Invalid type value, errno = EINVAL
574 @retval EFI_INVALID_PARAMETER - Invalid protocol value, errno = EINVAL
579 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
586 CONST ESL_PROTOCOL_API
* pApi
;
587 CONST ESL_PROTOCOL_API
** ppApiArray
;
588 CONST ESL_PROTOCOL_API
** ppApiArrayEnd
;
590 ESL_SOCKET
* pSocket
;
599 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
602 // Set the default domain if necessary
604 if ( AF_UNSPEC
== domain
) {
612 Status
= EFI_SUCCESS
;
615 // Use break instead of goto
619 // Validate the domain value
621 if (( AF_INET
!= domain
)
622 && ( AF_INET6
!= domain
)
623 && ( AF_LOCAL
!= domain
)) {
624 DEBUG (( DEBUG_ERROR
| DEBUG_SOCKET
,
625 "ERROR - Invalid domain value\r\n" ));
626 Status
= EFI_INVALID_PARAMETER
;
627 errno
= EAFNOSUPPORT
;
632 // Determine the protocol APIs
636 if (( AF_INET
== domain
)
637 || ( AF_LOCAL
== domain
)) {
638 ppApiArray
= &cEslAfInetApi
[0];
639 ApiArraySize
= cEslAfInetApiSize
;
642 ppApiArray
= &cEslAfInet6Api
[0];
643 ApiArraySize
= cEslAfInet6ApiSize
;
647 // Set the default type if necessary
654 // Validate the type value
656 if (( type
>= ApiArraySize
)
657 || ( NULL
== ppApiArray
)
658 || ( NULL
== ppApiArray
[ type
])) {
659 DEBUG (( DEBUG_ERROR
| DEBUG_SOCKET
,
660 "ERROR - Invalid type value\r\n" ));
662 // The socket type is not supported
664 Status
= EFI_INVALID_PARAMETER
;
670 // Set the default protocol if necessary
672 pApi
= ppApiArray
[ type
];
673 if ( 0 == protocol
) {
674 protocol
= pApi
->DefaultProtocol
;
678 // Validate the protocol value
680 if (( pApi
->DefaultProtocol
!= protocol
)
681 && ( SOCK_RAW
!= type
)) {
682 Status
= EFI_INVALID_PARAMETER
;
685 // Assume that the driver supports this protocol
687 ppApiArray
= &cEslAfInetApi
[0];
688 ppApiArrayEnd
= &ppApiArray
[ cEslAfInetApiSize
];
689 while ( ppApiArrayEnd
> ppApiArray
) {
691 if ( protocol
== pApi
->DefaultProtocol
) {
696 if ( ppApiArrayEnd
<= ppApiArray
) {
698 // Verify against the IPv6 table
700 ppApiArray
= &cEslAfInet6Api
[0];
701 ppApiArrayEnd
= &ppApiArray
[ cEslAfInet6ApiSize
];
702 while ( ppApiArrayEnd
> ppApiArray
) {
704 if ( protocol
== pApi
->DefaultProtocol
) {
710 if ( ppApiArrayEnd
<= ppApiArray
) {
711 DEBUG (( DEBUG_ERROR
| DEBUG_SOCKET
,
712 "ERROR - The protocol is not supported!\r\n" ));
713 errno
= EPROTONOSUPPORT
;
718 // The driver does not support this protocol
720 DEBUG (( DEBUG_ERROR
| DEBUG_SOCKET
,
721 "ERROR - The protocol does not support this socket type!\r\n" ));
722 errno
= EPROTONOSUPPORT
;
728 // Save the socket attributes
730 pSocket
->pApi
= pApi
;
731 pSocket
->Domain
= domain
;
732 pSocket
->Type
= type
;
733 pSocket
->Protocol
= protocol
;
742 // Return the operation status
744 if ( NULL
!= pErrno
) {
747 DBG_EXIT_STATUS ( Status
);
753 Accept a network connection.
755 This routine calls the network specific layer to remove the next
756 connection from the FIFO.
758 The ::accept calls this routine to poll for a network
759 connection to the socket. When a connection is available
760 this routine returns the ::EFI_SOCKET_PROTOCOL structure address
761 associated with the new socket and the remote network address
764 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
766 @param [in] pSockAddr Address of a buffer to receive the remote
769 @param [in, out] pSockAddrLength Length in bytes of the address buffer.
770 On output specifies the length of the
771 remote network address.
773 @param [out] ppSocketProtocol Address of a buffer to receive the
774 ::EFI_SOCKET_PROTOCOL instance
775 associated with the new socket.
777 @param [out] pErrno Address to receive the errno value upon completion.
779 @retval EFI_SUCCESS New connection successfully created
780 @retval EFI_NOT_READY No connection is available
785 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
786 IN
struct sockaddr
* pSockAddr
,
787 IN OUT socklen_t
* pSockAddrLength
,
788 IN EFI_SOCKET_PROTOCOL
** ppSocketProtocol
,
792 ESL_SOCKET
* pNewSocket
;
793 ESL_SOCKET
* pSocket
;
802 Status
= EFI_SUCCESS
;
805 // Validate the socket
809 if ( NULL
!= pSocketProtocol
) {
810 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
815 if ( NULL
== pSocket
->pApi
->pfnAccept
) {
816 Status
= EFI_UNSUPPORTED
;
817 pSocket
->errno
= ENOTSUP
;
821 // Validate the sockaddr
823 if (( NULL
!= pSockAddr
)
824 && ( NULL
== pSockAddrLength
)) {
825 DEBUG (( DEBUG_ACCEPT
,
826 "ERROR - pSockAddr is NULL!\r\n" ));
827 Status
= EFI_INVALID_PARAMETER
;
828 pSocket
->errno
= EFAULT
;
832 // Synchronize with the socket layer
834 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
837 // Verify that the socket is in the listen state
839 if ( SOCKET_STATE_LISTENING
!= pSocket
->State
) {
840 DEBUG (( DEBUG_ACCEPT
,
841 "ERROR - Socket is not listening!\r\n" ));
842 if ( NULL
== pSocket
->pApi
->pfnAccept
) {
844 // Socket does not support listen
846 pSocket
->errno
= EOPNOTSUPP
;
847 Status
= EFI_UNSUPPORTED
;
851 // Socket supports listen, but not in listen state
853 pSocket
->errno
= EINVAL
;
854 Status
= EFI_NOT_STARTED
;
859 // Determine if a socket is available
861 if ( 0 == pSocket
->FifoDepth
) {
863 // No connections available
864 // Determine if any ports are available
866 if ( NULL
== pSocket
->pPortList
) {
868 // No ports available
870 Status
= EFI_DEVICE_ERROR
;
871 pSocket
->errno
= EINVAL
;
874 // Update the socket state
876 pSocket
->State
= SOCKET_STATE_NO_PORTS
;
880 // Ports are available
881 // No connection requests at this time
883 Status
= EFI_NOT_READY
;
884 pSocket
->errno
= EAGAIN
;
890 // Attempt to accept the connection and
891 // get the remote network address
893 pNewSocket
= pSocket
->pFifoHead
;
894 ASSERT ( NULL
!= pNewSocket
);
895 Status
= pSocket
->pApi
->pfnAccept ( pNewSocket
,
898 if ( !EFI_ERROR ( Status
)) {
900 // Remove the new socket from the list
902 pSocket
->pFifoHead
= pNewSocket
->pNextConnection
;
903 if ( NULL
== pSocket
->pFifoHead
) {
904 pSocket
->pFifoTail
= NULL
;
908 // Account for this socket
910 pSocket
->FifoDepth
-= 1;
913 // Update the new socket's state
915 pNewSocket
->State
= SOCKET_STATE_CONNECTED
;
916 pNewSocket
->bConfigured
= TRUE
;
917 DEBUG (( DEBUG_ACCEPT
,
918 "0x%08x: Socket connected\r\n",
925 // Release the socket layer synchronization
927 RESTORE_TPL ( TplPrevious
);
933 // Return the new socket
935 if (( NULL
!= ppSocketProtocol
)
936 && ( NULL
!= pNewSocket
)) {
937 *ppSocketProtocol
= &pNewSocket
->SocketProtocol
;
941 // Return the operation status
943 if ( NULL
!= pErrno
) {
944 if ( NULL
!= pSocket
) {
945 *pErrno
= pSocket
->errno
;
948 Status
= EFI_INVALID_PARAMETER
;
952 DBG_EXIT_STATUS ( Status
);
958 Allocate and initialize a ESL_SOCKET structure.
960 This support function allocates an ::ESL_SOCKET structure
961 and installs a protocol on ChildHandle. If pChildHandle is a
962 pointer to NULL, then a new handle is created and returned in
963 pChildHandle. If pChildHandle is not a pointer to NULL, then
964 the protocol installs on the existing pChildHandle.
966 @param [in, out] pChildHandle Pointer to the handle of the child to create.
967 If it is NULL, then a new handle is created.
968 If it is a pointer to an existing UEFI handle,
969 then the protocol is added to the existing UEFI
971 @param [in] DebugFlags Flags for debug messages
972 @param [in, out] ppSocket The buffer to receive an ::ESL_SOCKET structure address.
974 @retval EFI_SUCCESS The protocol was added to ChildHandle.
975 @retval EFI_INVALID_PARAMETER ChildHandle is NULL.
976 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to create
978 @retval other The child handle was not created
984 IN OUT EFI_HANDLE
* pChildHandle
,
986 IN OUT ESL_SOCKET
** ppSocket
991 ESL_SOCKET
* pSocket
;
998 // Create a socket structure
1000 LengthInBytes
= sizeof ( *pSocket
);
1001 pSocket
= (ESL_SOCKET
*) AllocateZeroPool ( LengthInBytes
);
1002 if ( NULL
!= pSocket
) {
1003 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INIT
,
1004 "0x%08x: Allocate pSocket, %d bytes\r\n",
1009 // Initialize the socket protocol
1011 pSocket
->Signature
= SOCKET_SIGNATURE
;
1012 pSocket
->SocketProtocol
.pfnAccept
= EslSocketAccept
;
1013 pSocket
->SocketProtocol
.pfnBind
= EslSocketBind
;
1014 pSocket
->SocketProtocol
.pfnClosePoll
= EslSocketClosePoll
;
1015 pSocket
->SocketProtocol
.pfnCloseStart
= EslSocketCloseStart
;
1016 pSocket
->SocketProtocol
.pfnConnect
= EslSocketConnect
;
1017 pSocket
->SocketProtocol
.pfnGetLocal
= EslSocketGetLocalAddress
;
1018 pSocket
->SocketProtocol
.pfnGetPeer
= EslSocketGetPeerAddress
;
1019 pSocket
->SocketProtocol
.pfnListen
= EslSocketListen
;
1020 pSocket
->SocketProtocol
.pfnOptionGet
= EslSocketOptionGet
;
1021 pSocket
->SocketProtocol
.pfnOptionSet
= EslSocketOptionSet
;
1022 pSocket
->SocketProtocol
.pfnPoll
= EslSocketPoll
;
1023 pSocket
->SocketProtocol
.pfnReceive
= EslSocketReceive
;
1024 pSocket
->SocketProtocol
.pfnShutdown
= EslSocketShutdown
;
1025 pSocket
->SocketProtocol
.pfnSocket
= EslSocket
;
1026 pSocket
->SocketProtocol
.pfnTransmit
= EslSocketTransmit
;
1028 pSocket
->MaxRxBuf
= MAX_RX_DATA
;
1029 pSocket
->MaxTxBuf
= MAX_TX_DATA
;
1032 // Install the socket protocol on the specified handle
1034 Status
= gBS
->InstallMultipleProtocolInterfaces (
1036 &gEfiSocketProtocolGuid
,
1037 &pSocket
->SocketProtocol
,
1040 if ( !EFI_ERROR ( Status
)) {
1041 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INIT
| DEBUG_INFO
,
1042 "Installed: gEfiSocketProtocolGuid on 0x%08x\r\n",
1044 pSocket
->SocketProtocol
.SocketHandle
= *pChildHandle
;
1047 // Synchronize with the socket layer
1049 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
1052 // Add this socket to the list
1054 pLayer
= &mEslLayer
;
1055 pSocket
->pNext
= pLayer
->pSocketList
;
1056 pLayer
->pSocketList
= pSocket
;
1059 // Release the socket layer synchronization
1061 RESTORE_TPL ( TplPrevious
);
1064 // Return the socket structure address
1066 *ppSocket
= pSocket
;
1069 DEBUG (( DEBUG_ERROR
| DebugFlags
| DEBUG_POOL
| DEBUG_INIT
,
1070 "ERROR - Failed to install gEfiSocketProtocolGuid on 0x%08x, Status: %r\r\n",
1076 // Release the socket if necessary
1078 if ( EFI_ERROR ( Status
)) {
1079 gBS
->FreePool ( pSocket
);
1080 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INIT
,
1081 "0x%08x: Free pSocket, %d bytes\r\n",
1083 sizeof ( *pSocket
)));
1088 Status
= EFI_OUT_OF_RESOURCES
;
1092 // Return the operation status
1094 DBG_EXIT_STATUS ( Status
);
1100 Bind a name to a socket.
1102 This routine calls the network specific layer to save the network
1103 address of the local connection point.
1105 The ::bind routine calls this routine to connect a name
1106 (network address and port) to a socket on the local machine.
1108 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
1110 @param [in] pSockAddr Address of a sockaddr structure that contains the
1111 connection point on the local machine. An IPv4 address
1112 of INADDR_ANY specifies that the connection is made to
1113 all of the network stacks on the platform. Specifying a
1114 specific IPv4 address restricts the connection to the
1115 network stack supporting that address. Specifying zero
1116 for the port causes the network layer to assign a port
1117 number from the dynamic range. Specifying a specific
1118 port number causes the network layer to use that port.
1120 @param [in] SockAddrLength Specifies the length in bytes of the sockaddr structure.
1122 @param [out] pErrno Address to receive the errno value upon completion.
1124 @retval EFI_SUCCESS - Socket successfully created
1129 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
1130 IN CONST
struct sockaddr
* pSockAddr
,
1131 IN socklen_t SockAddrLength
,
1135 EFI_HANDLE ChildHandle
;
1138 ESL_SERVICE
** ppServiceListHead
;
1139 ESL_SOCKET
* pSocket
;
1140 ESL_SERVICE
* pService
;
1141 EFI_SERVICE_BINDING_PROTOCOL
* pServiceBinding
;
1143 EFI_TPL TplPrevious
;
1150 Status
= EFI_SUCCESS
;
1153 // Validate the socket
1156 if ( NULL
!= pSocketProtocol
) {
1157 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
1160 // Validate the structure pointer
1163 if ( NULL
== pSockAddr
) {
1164 DEBUG (( DEBUG_BIND
,
1165 "ERROR - pSockAddr is NULL!\r\n" ));
1166 Status
= EFI_INVALID_PARAMETER
;
1167 pSocket
->errno
= EFAULT
;
1171 // Validate the local address length
1173 else if ( SockAddrLength
< pSocket
->pApi
->MinimumAddressLength
) {
1174 DEBUG (( DEBUG_BIND
,
1175 "ERROR - Invalid bind name length: %d\r\n",
1177 Status
= EFI_INVALID_PARAMETER
;
1178 pSocket
->errno
= EINVAL
;
1182 // Validate the shutdown state
1184 else if ( pSocket
->bRxDisable
|| pSocket
->bTxDisable
) {
1185 DEBUG (( DEBUG_BIND
,
1186 "ERROR - Shutdown has been called on socket 0x%08x\r\n",
1188 pSocket
->errno
= EINVAL
;
1189 Status
= EFI_INVALID_PARAMETER
;
1193 // Verify the socket state
1195 else if ( SOCKET_STATE_NOT_CONFIGURED
!= pSocket
->State
) {
1196 DEBUG (( DEBUG_BIND
,
1197 "ERROR - The socket 0x%08x is already configured!\r\n",
1199 pSocket
->errno
= EINVAL
;
1200 Status
= EFI_ALREADY_STARTED
;
1204 // Synchronize with the socket layer
1206 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
1209 // Assume no ports are available
1211 pSocket
->errno
= EADDRNOTAVAIL
;
1212 Status
= EFI_INVALID_PARAMETER
;
1215 // Walk the list of services
1217 pBuffer
= (UINT8
*)&mEslLayer
;
1218 pBuffer
= &pBuffer
[ pSocket
->pApi
->ServiceListOffset
];
1219 ppServiceListHead
= (ESL_SERVICE
**)pBuffer
;
1220 pService
= *ppServiceListHead
;
1221 while ( NULL
!= pService
) {
1225 pServiceBinding
= pService
->pServiceBinding
;
1227 Status
= pServiceBinding
->CreateChild ( pServiceBinding
,
1229 if ( !EFI_ERROR ( Status
)) {
1230 DEBUG (( DEBUG_BIND
| DEBUG_POOL
,
1231 "0x%08x: %s port handle created\r\n",
1233 pService
->pSocketBinding
->pName
));
1238 Status
= EslSocketPortAllocate ( pSocket
,
1247 DEBUG (( DEBUG_BIND
| DEBUG_POOL
,
1248 "ERROR - Failed to open %s port handle, Status: %r\r\n",
1249 pService
->pSocketBinding
->pName
,
1254 // Set the next service
1256 pService
= pService
->pNext
;
1260 // Verify that at least one network connection was found
1262 if ( NULL
!= pSocket
->pPortList
) {
1263 Status
= EFI_SUCCESS
;
1266 if ( EADDRNOTAVAIL
== pSocket
->errno
) {
1267 DEBUG (( DEBUG_BIND
| DEBUG_POOL
| DEBUG_INIT
,
1268 "ERROR - Socket address is not available!\r\n" ));
1270 if ( EADDRINUSE
== pSocket
->errno
) {
1271 DEBUG (( DEBUG_BIND
| DEBUG_POOL
| DEBUG_INIT
,
1272 "ERROR - Socket address is in use!\r\n" ));
1274 Status
= EFI_INVALID_PARAMETER
;
1278 // Mark this socket as bound if successful
1280 if ( !EFI_ERROR ( Status
)) {
1281 pSocket
->State
= SOCKET_STATE_BOUND
;
1286 // Release the socket layer synchronization
1288 RESTORE_TPL ( TplPrevious
);
1293 // Return the operation status
1295 if ( NULL
!= pErrno
) {
1296 if ( NULL
!= pSocket
) {
1297 *pErrno
= pSocket
->errno
;
1300 Status
= EFI_INVALID_PARAMETER
;
1304 DBG_EXIT_STATUS ( Status
);
1310 Test the bind configuration.
1312 @param [in] pPort Address of the ::ESL_PORT structure.
1313 @param [in] ErrnoValue errno value if test fails
1315 @retval EFI_SUCCESS The connection was successfully established.
1316 @retval Others The connection attempt failed.
1321 IN ESL_PORT
* pPort
,
1332 // Locate the configuration data
1334 pBuffer
= (UINT8
*)pPort
;
1335 pBuffer
= &pBuffer
[ pPort
->pSocket
->pApi
->ConfigDataOffset
];
1336 pConfigData
= (VOID
*)pBuffer
;
1339 // Validate that the port is connected
1341 Status
= pPort
->pSocket
->pApi
->pfnVerifyLocalIpAddress ( pPort
, pBuffer
);
1342 if ( EFI_ERROR ( Status
)) {
1343 DEBUG (( DEBUG_WARN
| DEBUG_BIND
,
1344 "WARNING - Port 0x%08x invalid IP address: %r\r\n",
1347 pPort
->pSocket
->errno
= ErrnoValue
;
1351 // Attempt to use this configuration
1353 Status
= pPort
->pfnConfigure ( pPort
->pProtocol
.v
, pConfigData
);
1354 if ( EFI_ERROR ( Status
)) {
1355 DEBUG (( DEBUG_WARN
| DEBUG_BIND
,
1356 "WARNING - Port 0x%08x failed configuration, Status: %r\r\n",
1359 pPort
->pSocket
->errno
= ErrnoValue
;
1365 Status
= pPort
->pfnConfigure ( pPort
->pProtocol
.v
, NULL
);
1366 if ( EFI_ERROR ( Status
)) {
1367 DEBUG (( DEBUG_ERROR
| DEBUG_BIND
,
1368 "ERROR - Port 0x%08x failed configuration reset, Status: %r\r\n",
1371 ASSERT ( EFI_SUCCESS
== Status
);
1377 // Return the operation status
1379 DBG_EXIT_STATUS ( Status
);
1385 Determine if the socket is closed
1387 This routine checks the state of the socket to determine if
1388 the network specific layer has completed the close operation.
1390 The ::close routine polls this routine to determine when the
1391 close operation is complete. The close operation needs to
1392 reverse the operations of the ::EslSocketAllocate routine.
1394 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
1395 @param [out] pErrno Address to receive the errno value upon completion.
1397 @retval EFI_SUCCESS Socket successfully closed
1398 @retval EFI_NOT_READY Close still in progress
1399 @retval EFI_ALREADY Close operation already in progress
1400 @retval Other Failed to close the socket
1404 EslSocketClosePoll (
1405 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
1411 ESL_SOCKET
* pNextSocket
;
1412 ESL_SOCKET
* pSocket
;
1414 EFI_TPL TplPrevious
;
1422 Status
= EFI_SUCCESS
;
1425 // Synchronize with the socket layer
1427 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
1430 // Locate the socket
1432 pLayer
= &mEslLayer
;
1433 pNextSocket
= pLayer
->pSocketList
;
1434 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
1435 while ( NULL
!= pNextSocket
) {
1436 if ( pNextSocket
== pSocket
) {
1438 // Determine if the socket is in the closing state
1440 if ( SOCKET_STATE_CLOSED
== pSocket
->State
) {
1442 // Walk the list of ports
1444 if ( NULL
== pSocket
->pPortList
) {
1446 // All the ports are closed
1447 // Close the WaitAccept event if necessary
1449 if ( NULL
!= pSocket
->WaitAccept
) {
1450 Status
= gBS
->CloseEvent ( pSocket
->WaitAccept
);
1451 if ( !EFI_ERROR ( Status
)) {
1452 DEBUG (( DEBUG_SOCKET
| DEBUG_CLOSE
| DEBUG_POOL
,
1453 "0x%08x: Closed WaitAccept event\r\n",
1454 pSocket
->WaitAccept
));
1456 // Return the transmit status
1458 Status
= pSocket
->TxError
;
1459 if ( EFI_ERROR ( Status
)) {
1460 pSocket
->errno
= EIO
;
1464 DEBUG (( DEBUG_ERROR
| DEBUG_SOCKET
| DEBUG_CLOSE
| DEBUG_POOL
,
1465 "ERROR - Failed to close the WaitAccept event, Status: %r\r\n",
1467 ASSERT ( EFI_SUCCESS
== Status
);
1473 // At least one port is still open
1475 Status
= EFI_NOT_READY
;
1481 // SocketCloseStart was not called
1483 Status
= EFI_NOT_STARTED
;
1490 // Set the next socket
1492 pNextSocket
= pNextSocket
->pNext
;
1496 // Handle the error case where the socket was already closed
1498 if ( NULL
== pSocket
) {
1502 Status
= EFI_NOT_FOUND
;
1507 // Release the socket layer synchronization
1509 RESTORE_TPL ( TplPrevious
);
1512 // Return the operation status
1514 if ( NULL
!= pErrno
) {
1517 DBG_EXIT_STATUS ( Status
);
1523 Start the close operation on the socket
1525 This routine calls the network specific layer to initiate the
1526 close state machine. This routine then calls the network
1527 specific layer to determine if the close state machine has gone
1528 to completion. The result from this poll is returned to the
1531 The ::close routine calls this routine to start the close
1532 operation which reverses the operations of the
1533 ::EslSocketAllocate routine. The close routine then polls
1534 the ::EslSocketClosePoll routine to determine when the
1537 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
1538 @param [in] bCloseNow Boolean to control close behavior
1539 @param [out] pErrno Address to receive the errno value upon completion.
1541 @retval EFI_SUCCESS Socket successfully closed
1542 @retval EFI_NOT_READY Close still in progress
1543 @retval EFI_ALREADY Close operation already in progress
1544 @retval Other Failed to close the socket
1548 EslSocketCloseStart (
1549 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
1550 IN BOOLEAN bCloseNow
,
1555 ESL_PORT
* pNextPort
;
1557 ESL_SOCKET
* pSocket
;
1559 EFI_TPL TplPrevious
;
1566 Status
= EFI_SUCCESS
;
1570 // Synchronize with the socket layer
1572 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
1575 // Determine if the socket is already closed
1577 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
1578 if ( SOCKET_STATE_CLOSED
> pSocket
->State
) {
1580 // Update the socket state
1582 pSocket
->State
= SOCKET_STATE_CLOSED
;
1585 // Walk the list of ports
1587 pPort
= pSocket
->pPortList
;
1588 while ( NULL
!= pPort
) {
1590 // Start closing the ports
1592 pNextPort
= pPort
->pLinkSocket
;
1593 Status
= EslSocketPortCloseStart ( pPort
,
1595 DEBUG_CLOSE
| DEBUG_LISTEN
| DEBUG_CONNECTION
);
1596 if (( EFI_SUCCESS
!= Status
)
1597 && ( EFI_NOT_READY
!= Status
)) {
1603 // Set the next port
1609 // Attempt to finish closing the socket
1611 if ( NULL
== pPort
) {
1612 Status
= EslSocketClosePoll ( pSocketProtocol
, &errno
);
1616 Status
= EFI_NOT_READY
;
1621 // Release the socket layer synchronization
1623 RESTORE_TPL ( TplPrevious
);
1626 // Return the operation status
1628 if ( NULL
!= pErrno
) {
1631 DBG_EXIT_STATUS ( Status
);
1637 Connect to a remote system via the network.
1639 This routine calls the network specific layer to establish
1640 the remote system address and establish the connection to
1643 The ::connect routine calls this routine to establish a
1644 connection with the specified remote system. This routine
1645 is designed to be polled by the connect routine for completion
1646 of the network connection.
1648 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
1650 @param [in] pSockAddr Network address of the remote system.
1652 @param [in] SockAddrLength Length in bytes of the network address.
1654 @param [out] pErrno Address to receive the errno value upon completion.
1656 @retval EFI_SUCCESS The connection was successfully established.
1657 @retval EFI_NOT_READY The connection is in progress, call this routine again.
1658 @retval Others The connection attempt failed.
1663 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
1664 IN
const struct sockaddr
* pSockAddr
,
1665 IN socklen_t SockAddrLength
,
1669 struct sockaddr_in6 LocalAddress
;
1671 ESL_SOCKET
* pSocket
;
1673 EFI_TPL TplPrevious
;
1675 DEBUG (( DEBUG_CONNECT
, "Entering SocketConnect\r\n" ));
1680 Status
= EFI_SUCCESS
;
1683 // Validate the socket
1686 if ( NULL
!= pSocketProtocol
) {
1687 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
1690 // Validate the name length
1692 if ( SockAddrLength
< ( sizeof ( struct sockaddr
) - sizeof ( pSockAddr
->sa_data
))) {
1693 DEBUG (( DEBUG_CONNECT
,
1694 "ERROR - Invalid bind name length: %d\r\n",
1696 Status
= EFI_INVALID_PARAMETER
;
1697 pSocket
->errno
= EINVAL
;
1706 // Synchronize with the socket layer
1708 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
1711 // Validate the socket state
1713 switch ( pSocket
->State
) {
1716 // Wrong socket state
1718 pSocket
->errno
= EIO
;
1719 Status
= EFI_DEVICE_ERROR
;
1722 case SOCKET_STATE_NOT_CONFIGURED
:
1723 case SOCKET_STATE_BOUND
:
1725 // Validate the address length
1727 if ( SockAddrLength
>= pSocket
->pApi
->MinimumAddressLength
) {
1731 if ( NULL
== pSocket
->pApi
->pfnRemoteAddrSet
) {
1733 // Already connected
1735 pSocket
->errno
= ENOTSUP
;
1736 Status
= EFI_UNSUPPORTED
;
1740 // Determine if BIND was already called
1742 if ( NULL
== pSocket
->pPortList
) {
1744 // Allow any local port
1746 ZeroMem ( &LocalAddress
, sizeof ( LocalAddress
));
1747 LocalAddress
.sin6_len
= (uint8_t)pSocket
->pApi
->MinimumAddressLength
;
1748 LocalAddress
.sin6_family
= pSocket
->pApi
->AddressFamily
;
1749 Status
= EslSocketBind ( &pSocket
->SocketProtocol
,
1750 (struct sockaddr
*)&LocalAddress
,
1751 LocalAddress
.sin6_len
,
1754 if ( NULL
!= pSocket
->pPortList
) {
1756 // Walk the list of ports
1758 pPort
= pSocket
->pPortList
;
1759 while ( NULL
!= pPort
) {
1761 // Set the remote address
1763 Status
= pSocket
->pApi
->pfnRemoteAddrSet ( pPort
,
1766 if ( EFI_ERROR ( Status
)) {
1771 // Set the next port
1773 pPort
= pPort
->pLinkSocket
;
1779 if (( !EFI_ERROR ( Status
))
1780 && ( NULL
!= pSocket
->pApi
->pfnConnectStart
)) {
1782 // Initiate the connection with the remote system
1784 Status
= pSocket
->pApi
->pfnConnectStart ( pSocket
);
1787 // Set the next state if connecting
1789 if ( EFI_NOT_READY
== Status
) {
1790 pSocket
->State
= SOCKET_STATE_CONNECTING
;
1797 DEBUG (( DEBUG_CONNECT
,
1798 "ERROR - Invalid address length: %d\r\n",
1800 Status
= EFI_INVALID_PARAMETER
;
1801 pSocket
->errno
= EINVAL
;
1805 case SOCKET_STATE_CONNECTING
:
1807 // Poll the network adapter
1809 EslSocketRxPoll ( pSocket
);
1812 // Poll for connection completion
1814 if ( NULL
== pSocket
->pApi
->pfnConnectPoll
) {
1816 // Already connected
1818 pSocket
->errno
= EISCONN
;
1819 Status
= EFI_ALREADY_STARTED
;
1822 Status
= pSocket
->pApi
->pfnConnectPoll ( pSocket
);
1825 // Set the next state if connected
1827 if ( EFI_NOT_READY
!= Status
) {
1828 if ( EFI_ERROR ( Status
)) {
1829 pSocket
->State
= SOCKET_STATE_BOUND
;
1835 case SOCKET_STATE_CONNECTED
:
1839 Status
= EFI_SUCCESS
;
1844 // Release the socket layer synchronization
1846 RESTORE_TPL ( TplPrevious
);
1851 // Return the operation status
1853 if ( NULL
!= pErrno
) {
1854 if ( NULL
!= pSocket
) {
1855 *pErrno
= pSocket
->errno
;
1859 // Bad socket protocol
1861 DEBUG (( DEBUG_ERROR
| DEBUG_CONNECT
,
1862 "ERROR - pSocketProtocol invalid!\r\n" ));
1863 Status
= EFI_INVALID_PARAMETER
;
1869 // Return the operation status
1871 DEBUG (( DEBUG_CONNECT
, "Exiting SocketConnect, Status: %r\r\n", Status
));
1877 Copy a fragmented buffer into a destination buffer.
1879 This support routine copies a fragmented buffer to the caller specified buffer.
1881 This routine is called by ::EslIp4Receive and ::EslUdp4Receive.
1883 @param [in] FragmentCount Number of fragments in the table
1885 @param [in] pFragmentTable Address of an EFI_IP4_FRAGMENT_DATA structure
1887 @param [in] BufferLength Length of the the buffer
1889 @param [in] pBuffer Address of a buffer to receive the data.
1891 @param [in] pDataLength Number of received data bytes in the buffer.
1893 @return Returns the address of the next free byte in the buffer.
1897 EslSocketCopyFragmentedBuffer (
1898 IN UINT32 FragmentCount
,
1899 IN EFI_IP4_FRAGMENT_DATA
* pFragmentTable
,
1900 IN
size_t BufferLength
,
1902 OUT
size_t * pDataLength
1913 // Validate the IP and UDP structures are identical
1915 ASSERT ( OFFSET_OF ( EFI_IP4_FRAGMENT_DATA
, FragmentLength
)
1916 == OFFSET_OF ( EFI_UDP4_FRAGMENT_DATA
, FragmentLength
));
1917 ASSERT ( OFFSET_OF ( EFI_IP4_FRAGMENT_DATA
, FragmentBuffer
)
1918 == OFFSET_OF ( EFI_UDP4_FRAGMENT_DATA
, FragmentBuffer
));
1921 // Copy the received data
1924 pBufferEnd
= &pBuffer
[ BufferLength
];
1925 while (( pBufferEnd
> pBuffer
) && ( FragmentCount
> Fragment
)) {
1927 // Determine the amount of received data
1929 pData
= pFragmentTable
[Fragment
].FragmentBuffer
;
1930 BytesToCopy
= pFragmentTable
[Fragment
].FragmentLength
;
1931 if (((size_t)( pBufferEnd
- pBuffer
)) < BytesToCopy
) {
1932 BytesToCopy
= pBufferEnd
- pBuffer
;
1936 // Move the data into the buffer
1939 "0x%08x --> 0x%08x: Copy data 0x%08x bytes\r\n",
1943 CopyMem ( pBuffer
, pData
, BytesToCopy
);
1944 pBuffer
+= BytesToCopy
;
1949 // Return the data length and the buffer address
1951 *pDataLength
= BufferLength
- ( pBufferEnd
- pBuffer
);
1952 DBG_EXIT_HEX ( pBuffer
);
1960 This routine frees the socket structure and handle resources.
1962 The ::close routine calls EslServiceFreeProtocol which then calls
1963 this routine to free the socket context structure and close the
1966 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
1968 @param [out] pErrno Address to receive the errno value upon completion.
1970 @retval EFI_SUCCESS The socket resources were returned successfully.
1975 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
1979 EFI_HANDLE ChildHandle
;
1982 ESL_SOCKET
* pSocket
;
1983 ESL_SOCKET
* pSocketPrevious
;
1985 EFI_TPL TplPrevious
;
1994 Status
= EFI_INVALID_PARAMETER
;
1997 // Validate the socket
1999 pLayer
= &mEslLayer
;
2000 if ( NULL
!= pSocketProtocol
) {
2001 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
2004 // Synchronize with the socket layer
2006 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2009 // Walk the socket list
2011 pSocketPrevious
= pLayer
->pSocketList
;
2012 if ( NULL
!= pSocketPrevious
) {
2013 if ( pSocket
== pSocketPrevious
) {
2015 // Remove the socket from the head of the list
2017 pLayer
->pSocketList
= pSocket
->pNext
;
2021 // Find the socket in the middle of the list
2023 while (( NULL
!= pSocketPrevious
)
2024 && ( pSocket
!= pSocketPrevious
->pNext
)) {
2026 // Set the next socket
2028 pSocketPrevious
= pSocketPrevious
->pNext
;
2030 if ( NULL
!= pSocketPrevious
) {
2032 // Remove the socket from the middle of the list
2034 pSocketPrevious
= pSocket
->pNext
;
2039 DEBUG (( DEBUG_ERROR
| DEBUG_POOL
,
2040 "ERROR - Socket list is empty!\r\n" ));
2044 // Release the socket layer synchronization
2046 RESTORE_TPL ( TplPrevious
);
2049 // Determine if the socket was found
2051 if ( NULL
!= pSocketPrevious
) {
2052 pSocket
->pNext
= NULL
;
2055 // Remove the socket protocol
2057 ChildHandle
= pSocket
->SocketProtocol
.SocketHandle
;
2058 Status
= gBS
->UninstallMultipleProtocolInterfaces (
2060 &gEfiSocketProtocolGuid
,
2061 &pSocket
->SocketProtocol
,
2063 if ( !EFI_ERROR ( Status
)) {
2064 DEBUG (( DEBUG_POOL
| DEBUG_INFO
,
2065 "Removed: gEfiSocketProtocolGuid from 0x%08x\r\n",
2069 // Free the socket structure
2071 Status
= gBS
->FreePool ( pSocket
);
2072 if ( !EFI_ERROR ( Status
)) {
2073 DEBUG (( DEBUG_POOL
,
2074 "0x%08x: Free pSocket, %d bytes\r\n",
2076 sizeof ( *pSocket
)));
2080 DEBUG (( DEBUG_ERROR
| DEBUG_POOL
,
2081 "ERROR - Failed to free pSocket 0x%08x, Status: %r\r\n",
2087 DEBUG (( DEBUG_ERROR
| DEBUG_POOL
| DEBUG_INFO
,
2088 "ERROR - Failed to remove gEfiSocketProtocolGuid from 0x%08x, Status: %r\r\n",
2094 DEBUG (( DEBUG_ERROR
| DEBUG_INFO
,
2095 "ERROR - The socket was not in the socket list!\r\n" ));
2096 Status
= EFI_NOT_FOUND
;
2100 DEBUG (( DEBUG_ERROR
,
2101 "ERROR - Invalid parameter pSocketProtocol is NULL\r\n" ));
2105 // Return the errno value if possible
2107 if ( NULL
!= pErrno
) {
2112 // Return the operation status
2114 DBG_EXIT_STATUS ( Status
);
2120 Get the local address.
2122 This routine calls the network specific layer to get the network
2123 address of the local host connection point.
2125 The ::getsockname routine calls this routine to obtain the network
2126 address associated with the local host connection point.
2128 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
2130 @param [out] pAddress Network address to receive the local system address
2132 @param [in,out] pAddressLength Length of the local network address structure
2134 @param [out] pErrno Address to receive the errno value upon completion.
2136 @retval EFI_SUCCESS - Local address successfully returned
2140 EslSocketGetLocalAddress (
2141 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
2142 OUT
struct sockaddr
* pAddress
,
2143 IN OUT socklen_t
* pAddressLength
,
2147 socklen_t LengthInBytes
;
2149 ESL_SOCKET
* pSocket
;
2151 EFI_TPL TplPrevious
;
2158 Status
= EFI_SUCCESS
;
2161 // Validate the socket
2164 if ( NULL
!= pSocketProtocol
) {
2165 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
2168 // Verify the socket state
2170 EslSocketIsConfigured ( pSocket
);
2171 if ( pSocket
->bAddressSet
) {
2173 // Verify the address buffer and length address
2175 if (( NULL
!= pAddress
) && ( NULL
!= pAddressLength
)) {
2179 if ( NULL
== pSocket
->pApi
->pfnLocalAddrGet
) {
2180 Status
= EFI_UNSUPPORTED
;
2181 pSocket
->errno
= ENOTSUP
;
2185 // Synchronize with the socket layer
2187 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2190 // Verify that there is just a single connection
2192 pPort
= pSocket
->pPortList
;
2193 if ( NULL
!= pPort
) {
2195 // Verify the address length
2197 LengthInBytes
= pSocket
->pApi
->AddressLength
;
2198 if (( LengthInBytes
<= *pAddressLength
)
2199 && ( 255 >= LengthInBytes
)) {
2201 // Return the local address and address length
2203 ZeroMem ( pAddress
, LengthInBytes
);
2204 pAddress
->sa_len
= (uint8_t)LengthInBytes
;
2205 *pAddressLength
= pAddress
->sa_len
;
2206 pSocket
->pApi
->pfnLocalAddrGet ( pPort
, pAddress
);
2208 Status
= EFI_SUCCESS
;
2211 pSocket
->errno
= EINVAL
;
2212 Status
= EFI_INVALID_PARAMETER
;
2216 pSocket
->errno
= ENOTCONN
;
2217 Status
= EFI_NOT_STARTED
;
2221 // Release the socket layer synchronization
2223 RESTORE_TPL ( TplPrevious
);
2227 pSocket
->errno
= EINVAL
;
2228 Status
= EFI_INVALID_PARAMETER
;
2235 Status
= EFI_NOT_STARTED
;
2236 pSocket
->errno
= EADDRNOTAVAIL
;
2241 // Return the operation status
2243 if ( NULL
!= pErrno
) {
2244 if ( NULL
!= pSocket
) {
2245 *pErrno
= pSocket
->errno
;
2248 Status
= EFI_INVALID_PARAMETER
;
2252 DBG_EXIT_STATUS ( Status
);
2258 Get the peer address.
2260 This routine calls the network specific layer to get the remote
2261 system connection point.
2263 The ::getpeername routine calls this routine to obtain the network
2264 address of the remote connection point.
2266 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
2268 @param [out] pAddress Network address to receive the remote system address
2270 @param [in,out] pAddressLength Length of the remote network address structure
2272 @param [out] pErrno Address to receive the errno value upon completion.
2274 @retval EFI_SUCCESS - Remote address successfully returned
2278 EslSocketGetPeerAddress (
2279 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
2280 OUT
struct sockaddr
* pAddress
,
2281 IN OUT socklen_t
* pAddressLength
,
2285 socklen_t LengthInBytes
;
2287 ESL_SOCKET
* pSocket
;
2289 EFI_TPL TplPrevious
;
2296 Status
= EFI_SUCCESS
;
2299 // Validate the socket
2302 if ( NULL
!= pSocketProtocol
) {
2303 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
2306 // Verify the socket state
2308 Status
= EslSocketIsConfigured ( pSocket
);
2309 if ( !EFI_ERROR ( Status
)) {
2313 if ( NULL
== pSocket
->pApi
->pfnRemoteAddrGet
) {
2314 Status
= EFI_UNSUPPORTED
;
2315 pSocket
->errno
= ENOTSUP
;
2319 // Verify the address buffer and length address
2321 if (( NULL
!= pAddress
) && ( NULL
!= pAddressLength
)) {
2323 // Verify the socket state
2325 if ( SOCKET_STATE_CONNECTED
== pSocket
->State
) {
2327 // Synchronize with the socket layer
2329 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2332 // Verify that there is just a single connection
2334 pPort
= pSocket
->pPortList
;
2335 if (( NULL
!= pPort
) && ( NULL
== pPort
->pLinkSocket
)) {
2337 // Verify the address length
2339 LengthInBytes
= pSocket
->pApi
->AddressLength
;
2340 if ( LengthInBytes
<= *pAddressLength
) {
2342 // Return the local address
2344 ZeroMem ( pAddress
, LengthInBytes
);
2345 pAddress
->sa_len
= (uint8_t)LengthInBytes
;
2346 *pAddressLength
= pAddress
->sa_len
;
2347 pSocket
->pApi
->pfnRemoteAddrGet ( pPort
, pAddress
);
2349 Status
= EFI_SUCCESS
;
2352 pSocket
->errno
= EINVAL
;
2353 Status
= EFI_INVALID_PARAMETER
;
2357 pSocket
->errno
= ENOTCONN
;
2358 Status
= EFI_NOT_STARTED
;
2362 // Release the socket layer synchronization
2364 RESTORE_TPL ( TplPrevious
);
2367 pSocket
->errno
= ENOTCONN
;
2368 Status
= EFI_NOT_STARTED
;
2372 pSocket
->errno
= EINVAL
;
2373 Status
= EFI_INVALID_PARAMETER
;
2380 // Return the operation status
2382 if ( NULL
!= pErrno
) {
2383 if ( NULL
!= pSocket
) {
2384 *pErrno
= pSocket
->errno
;
2387 Status
= EFI_INVALID_PARAMETER
;
2391 DBG_EXIT_STATUS ( Status
);
2397 Free the ESL_IO_MGMT event and structure
2399 This support routine walks the free list to close the event in
2400 the ESL_IO_MGMT structure and remove the structure from the free
2403 See the \ref TransmitEngine section.
2405 @param [in] pPort Address of an ::ESL_PORT structure
2406 @param [in] ppFreeQueue Address of the free queue head
2407 @param [in] DebugFlags Flags for debug messages
2408 @param [in] pEventName Zero terminated string containing the event name
2410 @retval EFI_SUCCESS - The structures were properly initialized
2415 IN ESL_PORT
* pPort
,
2416 IN ESL_IO_MGMT
** ppFreeQueue
,
2417 IN UINTN DebugFlags
,
2418 IN CHAR8
* pEventName
2424 ESL_SOCKET
* pSocket
;
2432 Status
= EFI_SUCCESS
;
2435 // Walk the list of IO structures
2437 pSocket
= pPort
->pSocket
;
2438 while ( *ppFreeQueue
) {
2440 // Free the event for this structure
2443 pBuffer
= (UINT8
*)pIo
;
2444 pBuffer
= &pBuffer
[ pSocket
->TxTokenEventOffset
];
2445 pEvent
= (EFI_EVENT
*)pBuffer
;
2446 Status
= gBS
->CloseEvent ( *pEvent
);
2447 if ( EFI_ERROR ( Status
)) {
2448 DEBUG (( DEBUG_ERROR
| DebugFlags
,
2449 "ERROR - Failed to close the %a event, Status: %r\r\n",
2452 pSocket
->errno
= ENOMEM
;
2455 DEBUG (( DebugFlags
,
2456 "0x%08x: Closed %a event 0x%08x\r\n",
2462 // Remove this structure from the queue
2464 *ppFreeQueue
= pIo
->pNext
;
2468 // Return the operation status
2470 DBG_EXIT_STATUS ( Status
);
2476 Initialize the ESL_IO_MGMT structures
2478 This support routine initializes the ESL_IO_MGMT structure and
2479 places them on to a free list.
2481 This routine is called by ::EslSocketPortAllocate routines to prepare
2482 the transmit engines. See the \ref TransmitEngine section.
2484 @param [in] pPort Address of an ::ESL_PORT structure
2485 @param [in, out] ppIo Address containing the first structure address. Upon
2486 return this buffer contains the next structure address.
2487 @param [in] TokenCount Number of structures to initialize
2488 @param [in] ppFreeQueue Address of the free queue head
2489 @param [in] DebugFlags Flags for debug messages
2490 @param [in] pEventName Zero terminated string containing the event name
2491 @param [in] pfnCompletion Completion routine address
2493 @retval EFI_SUCCESS - The structures were properly initialized
2498 IN ESL_PORT
* pPort
,
2499 IN ESL_IO_MGMT
** ppIo
,
2500 IN UINTN TokenCount
,
2501 IN ESL_IO_MGMT
** ppFreeQueue
,
2502 IN UINTN DebugFlags
,
2503 IN CHAR8
* pEventName
,
2504 IN PFN_API_IO_COMPLETE pfnCompletion
2510 ESL_SOCKET
* pSocket
;
2518 Status
= EFI_SUCCESS
;
2521 // Walk the list of IO structures
2523 pSocket
= pPort
->pSocket
;
2525 pEnd
= &pIo
[ TokenCount
];
2526 while ( pEnd
> pIo
) {
2528 // Initialize the IO structure
2531 pIo
->pPacket
= NULL
;
2534 // Allocate the event for this structure
2536 pEvent
= (EFI_EVENT
*)&(((UINT8
*)pIo
)[ pSocket
->TxTokenEventOffset
]);
2537 Status
= gBS
->CreateEvent ( EVT_NOTIFY_SIGNAL
,
2539 (EFI_EVENT_NOTIFY
)pfnCompletion
,
2542 if ( EFI_ERROR ( Status
)) {
2543 DEBUG (( DEBUG_ERROR
| DebugFlags
,
2544 "ERROR - Failed to create the %a event, Status: %r\r\n",
2547 pSocket
->errno
= ENOMEM
;
2550 DEBUG (( DebugFlags
,
2551 "0x%08x: Created %a event 0x%08x\r\n",
2557 // Add this structure to the queue
2559 pIo
->pNext
= *ppFreeQueue
;
2563 // Set the next structure
2569 // Save the next structure
2574 // Return the operation status
2576 DBG_EXIT_STATUS ( Status
);
2582 Determine if the socket is configured
2584 This support routine is called to determine if the socket if the
2585 configuration call was made to the network layer. The following
2586 routines call this routine to verify that they may be successful
2587 in their operations:
2589 <li>::EslSocketGetLocalAddress</li>
2590 <li>::EslSocketGetPeerAddress</li>
2591 <li>::EslSocketPoll</li>
2592 <li>::EslSocketReceive</li>
2593 <li>::EslSocketTransmit</li>
2596 @param [in] pSocket Address of an ::ESL_SOCKET structure
2598 @retval EFI_SUCCESS - The socket is configured
2602 EslSocketIsConfigured (
2603 IN ESL_SOCKET
* pSocket
2607 EFI_TPL TplPrevious
;
2612 Status
= EFI_SUCCESS
;
2615 // Verify the socket state
2617 if ( !pSocket
->bConfigured
) {
2623 if ( NULL
== pSocket
->pApi
->pfnIsConfigured
) {
2624 Status
= EFI_UNSUPPORTED
;
2625 pSocket
->errno
= ENOTSUP
;
2629 // Synchronize with the socket layer
2631 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2634 // Determine if the socket is configured
2636 Status
= pSocket
->pApi
->pfnIsConfigured ( pSocket
);
2639 // Release the socket layer synchronization
2641 RESTORE_TPL ( TplPrevious
);
2644 // Set errno if a failure occurs
2646 if ( EFI_ERROR ( Status
)) {
2647 pSocket
->errno
= EADDRNOTAVAIL
;
2651 DBG_EXIT_STATUS ( Status
);
2655 // Return the configuration status
2662 Establish the known port to listen for network connections.
2664 This routine calls into the network protocol layer to establish
2665 a handler that is called upon connection completion. The handler
2666 is responsible for inserting the connection into the FIFO.
2668 The ::listen routine indirectly calls this routine to place the
2669 socket into a state that enables connection attempts. Connections
2670 are placed in a FIFO that is serviced by the application. The
2671 application calls the ::accept (::EslSocketAccept) routine to
2672 remove the next connection from the FIFO and get the associated
2675 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
2677 @param [in] Backlog Backlog specifies the maximum FIFO depth for
2678 the connections waiting for the application
2679 to call accept. Connection attempts received
2680 while the queue is full are refused.
2682 @param [out] pErrno Address to receive the errno value upon completion.
2684 @retval EFI_SUCCESS - Socket successfully created
2685 @retval Other - Failed to enable the socket for listen
2690 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
2695 ESL_SOCKET
* pSocket
;
2697 EFI_STATUS TempStatus
;
2698 EFI_TPL TplPrevious
;
2705 Status
= EFI_SUCCESS
;
2708 // Validate the socket
2711 if ( NULL
!= pSocketProtocol
) {
2712 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
2717 if ( NULL
== pSocket
->pApi
->pfnListen
) {
2718 Status
= EFI_UNSUPPORTED
;
2719 pSocket
->errno
= ENOTSUP
;
2725 pSocket
->Status
= EFI_SUCCESS
;
2729 // Verify that the bind operation was successful
2731 if ( SOCKET_STATE_BOUND
== pSocket
->State
) {
2733 // Synchronize with the socket layer
2735 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
2738 // Create the event for SocketAccept completion
2740 Status
= gBS
->CreateEvent ( 0,
2744 &pSocket
->WaitAccept
);
2745 if ( !EFI_ERROR ( Status
)) {
2746 DEBUG (( DEBUG_POOL
,
2747 "0x%08x: Created WaitAccept event\r\n",
2748 pSocket
->WaitAccept
));
2750 // Set the maximum FIFO depth
2752 if ( 0 >= Backlog
) {
2753 Backlog
= MAX_PENDING_CONNECTIONS
;
2756 if ( SOMAXCONN
< Backlog
) {
2757 Backlog
= SOMAXCONN
;
2760 pSocket
->MaxFifoDepth
= Backlog
;
2765 // Initiate the connection attempt listen
2767 Status
= pSocket
->pApi
->pfnListen ( pSocket
);
2770 // Place the socket in the listen state if successful
2772 if ( !EFI_ERROR ( Status
)) {
2773 pSocket
->State
= SOCKET_STATE_LISTENING
;
2774 pSocket
->bListenCalled
= TRUE
;
2778 // Not waiting for SocketAccept to complete
2780 TempStatus
= gBS
->CloseEvent ( pSocket
->WaitAccept
);
2781 if ( !EFI_ERROR ( TempStatus
)) {
2782 DEBUG (( DEBUG_POOL
,
2783 "0x%08x: Closed WaitAccept event\r\n",
2784 pSocket
->WaitAccept
));
2785 pSocket
->WaitAccept
= NULL
;
2788 DEBUG (( DEBUG_ERROR
| DEBUG_POOL
,
2789 "ERROR - Failed to close WaitAccept event, Status: %r\r\n",
2791 ASSERT ( EFI_SUCCESS
== TempStatus
);
2796 DEBUG (( DEBUG_ERROR
| DEBUG_LISTEN
,
2797 "ERROR - Failed to create the WaitAccept event, Status: %r\r\n",
2799 pSocket
->errno
= ENOMEM
;
2803 // Release the socket layer synchronization
2805 RESTORE_TPL ( TplPrevious
);
2808 DEBUG (( DEBUG_ERROR
| DEBUG_LISTEN
,
2809 "ERROR - Bind operation must be performed first!\r\n" ));
2810 pSocket
->errno
= ( SOCKET_STATE_NOT_CONFIGURED
== pSocket
->State
) ? EDESTADDRREQ
2812 Status
= EFI_NO_MAPPING
;
2818 // Return the operation status
2820 if ( NULL
!= pErrno
) {
2821 if ( NULL
!= pSocket
) {
2822 *pErrno
= pSocket
->errno
;
2825 Status
= EFI_INVALID_PARAMETER
;
2829 DBG_EXIT_STATUS ( Status
);
2835 Get the socket options
2837 This routine handles the socket level options and passes the
2838 others to the network specific layer.
2840 The ::getsockopt routine calls this routine to retrieve the
2841 socket options one at a time by name.
2843 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
2844 @param [in] level Option protocol level
2845 @param [in] OptionName Name of the option
2846 @param [out] pOptionValue Buffer to receive the option value
2847 @param [in,out] pOptionLength Length of the buffer in bytes,
2848 upon return length of the option value in bytes
2849 @param [out] pErrno Address to receive the errno value upon completion.
2851 @retval EFI_SUCCESS - Socket data successfully received
2855 EslSocketOptionGet (
2856 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
2859 OUT
void * __restrict pOptionValue
,
2860 IN OUT socklen_t
* __restrict pOptionLength
,
2865 socklen_t LengthInBytes
;
2867 CONST UINT8
* pOptionData
;
2868 ESL_SOCKET
* pSocket
;
2877 Status
= EFI_INVALID_PARAMETER
;
2880 // Validate the socket
2883 if ( NULL
== pSocketProtocol
) {
2884 DEBUG (( DEBUG_OPTION
, "ERROR - pSocketProtocol is NULL!\r\n" ));
2886 else if ( NULL
== pOptionValue
) {
2887 DEBUG (( DEBUG_OPTION
, "ERROR - No option buffer specified\r\n" ));
2889 else if ( NULL
== pOptionLength
) {
2890 DEBUG (( DEBUG_OPTION
, "ERROR - Option length not specified!\r\n" ));
2893 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
2895 MaxBytes
= *pOptionLength
;
2900 // See if the protocol will handle the option
2902 if ( NULL
!= pSocket
->pApi
->pfnOptionGet
) {
2903 if ( pSocket
->pApi
->DefaultProtocol
== level
) {
2904 Status
= pSocket
->pApi
->pfnOptionGet ( pSocket
,
2906 (CONST
void ** __restrict
)&pOptionData
,
2908 errno
= pSocket
->errno
;
2913 // Protocol not supported
2915 DEBUG (( DEBUG_OPTION
,
2916 "ERROR - The socket does not support this protocol!\r\n" ));
2921 // Protocol level not supported
2923 DEBUG (( DEBUG_OPTION
,
2924 "ERROR - %a does not support any options!\r\n",
2925 pSocket
->pApi
->pName
));
2927 errno
= ENOPROTOOPT
;
2928 Status
= EFI_INVALID_PARAMETER
;
2932 switch ( OptionName
) {
2935 // Socket option not supported
2937 DEBUG (( DEBUG_INFO
| DEBUG_OPTION
, "ERROR - Invalid socket option!\r\n" ));
2939 Status
= EFI_INVALID_PARAMETER
;
2944 // Return the listen flag
2946 pOptionData
= (CONST UINT8
*)&pSocket
->bListenCalled
;
2947 LengthInBytes
= sizeof ( pSocket
->bListenCalled
);
2952 // Return the debug flags
2954 pOptionData
= (CONST UINT8
*)&pSocket
->bOobInLine
;
2955 LengthInBytes
= sizeof ( pSocket
->bOobInLine
);
2960 // Return the out-of-band inline flag
2962 pOptionData
= (CONST UINT8
*)&pSocket
->bOobInLine
;
2963 LengthInBytes
= sizeof ( pSocket
->bOobInLine
);
2968 // Return the receive timeout
2970 pOptionData
= (CONST UINT8
*)&pSocket
->RxTimeout
;
2971 LengthInBytes
= sizeof ( pSocket
->RxTimeout
);
2976 // Return the maximum receive buffer size
2978 pOptionData
= (CONST UINT8
*)&pSocket
->MaxRxBuf
;
2979 LengthInBytes
= sizeof ( pSocket
->MaxRxBuf
);
2984 // Return the address reuse flag
2986 pOptionData
= (UINT8
*)&pSocket
->bReUseAddr
;
2987 LengthInBytes
= sizeof ( pSocket
->bReUseAddr
);
2992 // Return the maximum transmit buffer size
2994 pOptionData
= (CONST UINT8
*)&pSocket
->MaxTxBuf
;
2995 LengthInBytes
= sizeof ( pSocket
->MaxTxBuf
);
3000 // Return the socket type
3002 pOptionData
= (CONST UINT8
*)&pSocket
->Type
;
3003 LengthInBytes
= sizeof ( pSocket
->Type
);
3010 // Return the option length
3012 *pOptionLength
= LengthInBytes
;
3015 // Determine if the option is present
3017 if ( 0 != LengthInBytes
) {
3019 // Silently truncate the value length
3021 if ( LengthInBytes
> MaxBytes
) {
3022 DEBUG (( DEBUG_OPTION
,
3023 "INFO - Truncating option from %d to %d bytes\r\n",
3026 LengthInBytes
= MaxBytes
;
3032 CopyMem ( pOptionValue
, pOptionData
, LengthInBytes
);
3035 // Zero fill any remaining space
3037 if ( LengthInBytes
< MaxBytes
) {
3038 ZeroMem ( &((UINT8
*)pOptionValue
)[LengthInBytes
], MaxBytes
- LengthInBytes
);
3041 Status
= EFI_SUCCESS
;
3046 // Return the operation status
3048 if ( NULL
!= pErrno
) {
3051 DBG_EXIT_STATUS ( Status
);
3057 Set the socket options
3059 This routine handles the socket level options and passes the
3060 others to the network specific layer.
3062 The ::setsockopt routine calls this routine to adjust the socket
3063 options one at a time by name.
3065 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
3066 @param [in] level Option protocol level
3067 @param [in] OptionName Name of the option
3068 @param [in] pOptionValue Buffer containing the option value
3069 @param [in] OptionLength Length of the buffer in bytes
3070 @param [out] pErrno Address to receive the errno value upon completion.
3072 @retval EFI_SUCCESS - Option successfully set
3076 EslSocketOptionSet (
3077 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
3080 IN CONST
void * pOptionValue
,
3081 IN socklen_t OptionLength
,
3087 socklen_t LengthInBytes
;
3088 UINT8
* pOptionData
;
3089 ESL_SOCKET
* pSocket
;
3098 Status
= EFI_INVALID_PARAMETER
;
3101 // Validate the socket
3104 if ( NULL
== pSocketProtocol
) {
3105 DEBUG (( DEBUG_OPTION
, "ERROR - pSocketProtocol is NULL!\r\n" ));
3107 else if ( NULL
== pOptionValue
) {
3108 DEBUG (( DEBUG_OPTION
, "ERROR - No option buffer specified\r\n" ));
3112 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
3113 if ( pSocket
->bRxDisable
|| pSocket
->bTxDisable
) {
3114 DEBUG (( DEBUG_OPTION
, "ERROR - Socket has been shutdown!\r\n" ));
3122 // See if the protocol will handle the option
3124 if ( NULL
!= pSocket
->pApi
->pfnOptionSet
) {
3125 if ( pSocket
->pApi
->DefaultProtocol
== level
) {
3126 Status
= pSocket
->pApi
->pfnOptionSet ( pSocket
,
3130 errno
= pSocket
->errno
;
3135 // Protocol not supported
3137 DEBUG (( DEBUG_OPTION
,
3138 "ERROR - The socket does not support this protocol!\r\n" ));
3143 // Protocol level not supported
3145 DEBUG (( DEBUG_OPTION
,
3146 "ERROR - %a does not support any options!\r\n",
3147 pSocket
->pApi
->pName
));
3149 errno
= ENOPROTOOPT
;
3150 Status
= EFI_INVALID_PARAMETER
;
3154 switch ( OptionName
) {
3157 // Option not supported
3159 DEBUG (( DEBUG_OPTION
,
3160 "ERROR - Sockets does not support this option!\r\n" ));
3162 Status
= EFI_INVALID_PARAMETER
;
3167 // Set the debug flags
3169 pOptionData
= (UINT8
*)&pSocket
->bOobInLine
;
3170 LengthInBytes
= sizeof ( pSocket
->bOobInLine
);
3174 pOptionData
= (UINT8
*)&pSocket
->bOobInLine
;
3175 LengthInBytes
= sizeof ( pSocket
->bOobInLine
);
3178 // Validate the option length
3180 if ( sizeof ( UINT32
) == OptionLength
) {
3182 // Restrict the input to TRUE or FALSE
3185 if ( 0 == *(UINT32
*)pOptionValue
) {
3188 pOptionValue
= &bTrueFalse
;
3192 // Force an invalid option length error
3194 OptionLength
= LengthInBytes
- 1;
3200 // Return the receive timeout
3202 pOptionData
= (UINT8
*)&pSocket
->RxTimeout
;
3203 LengthInBytes
= sizeof ( pSocket
->RxTimeout
);
3208 // Return the maximum receive buffer size
3210 pOptionData
= (UINT8
*)&pSocket
->MaxRxBuf
;
3211 LengthInBytes
= sizeof ( pSocket
->MaxRxBuf
);
3216 // Return the address reuse flag
3218 pOptionData
= (UINT8
*)&pSocket
->bReUseAddr
;
3219 LengthInBytes
= sizeof ( pSocket
->bReUseAddr
);
3227 // Return the maximum transmit buffer size
3229 pOptionData
= (UINT8
*)&pSocket
->MaxTxBuf
;
3230 LengthInBytes
= sizeof ( pSocket
->MaxTxBuf
);
3237 // Determine if an option was found
3239 if ( 0 != LengthInBytes
) {
3241 // Validate the option length
3243 if ( LengthInBytes
<= OptionLength
) {
3245 // Set the option value
3247 CopyMem ( pOptionData
, pOptionValue
, LengthInBytes
);
3249 Status
= EFI_SUCCESS
;
3252 DEBUG (( DEBUG_OPTION
,
3253 "ERROR - Buffer to small, %d bytes < %d bytes!\r\n",
3262 // Return the operation status
3264 if ( NULL
!= pErrno
) {
3267 DBG_EXIT_STATUS ( Status
);
3273 Allocate a packet for a receive or transmit operation
3275 This support routine is called by ::EslSocketRxStart and the
3276 network specific TxBuffer routines to get buffer space for the
3279 @param [in] ppPacket Address to receive the ::ESL_PACKET structure
3280 @param [in] LengthInBytes Length of the packet structure
3281 @param [in] ZeroBytes Length of packet to zero
3282 @param [in] DebugFlags Flags for debug messages
3284 @retval EFI_SUCCESS - The packet was allocated successfully
3288 EslSocketPacketAllocate (
3289 IN ESL_PACKET
** ppPacket
,
3290 IN
size_t LengthInBytes
,
3291 IN
size_t ZeroBytes
,
3295 ESL_PACKET
* pPacket
;
3301 // Allocate a packet structure
3303 LengthInBytes
+= sizeof ( *pPacket
)
3304 - sizeof ( pPacket
->Op
);
3305 Status
= gBS
->AllocatePool ( EfiRuntimeServicesData
,
3307 (VOID
**)&pPacket
);
3308 if ( !EFI_ERROR ( Status
)) {
3309 DEBUG (( DebugFlags
| DEBUG_POOL
,
3310 "0x%08x: Allocate pPacket, %d bytes\r\n",
3313 if ( 0 != ZeroBytes
) {
3314 ZeroMem ( &pPacket
->Op
, ZeroBytes
);
3316 pPacket
->PacketSize
= LengthInBytes
;
3319 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INFO
,
3320 "ERROR - Packet allocation failed for %d bytes, Status: %r\r\n",
3327 // Return the packet
3329 *ppPacket
= pPacket
;
3332 // Return the operation status
3334 DBG_EXIT_STATUS ( Status
);
3340 Free a packet used for receive or transmit operation
3342 This support routine is called by the network specific Close
3343 and TxComplete routines and during error cases in RxComplete
3344 and TxBuffer. Note that the network layers typically place
3345 receive packets on the ESL_SOCKET::pRxFree list for reuse.
3347 @param [in] pPacket Address of an ::ESL_PACKET structure
3348 @param [in] DebugFlags Flags for debug messages
3350 @retval EFI_SUCCESS - The packet was allocated successfully
3354 EslSocketPacketFree (
3355 IN ESL_PACKET
* pPacket
,
3359 UINTN LengthInBytes
;
3365 // Free a packet structure
3367 LengthInBytes
= pPacket
->PacketSize
;
3368 Status
= gBS
->FreePool ( pPacket
);
3369 if ( !EFI_ERROR ( Status
)) {
3370 DEBUG (( DebugFlags
| DEBUG_POOL
,
3371 "0x%08x: Free pPacket, %d bytes\r\n",
3376 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INFO
,
3377 "ERROR - Failed to free packet 0x%08x, Status: %r\r\n",
3383 // Return the operation status
3385 DBG_EXIT_STATUS ( Status
);
3391 Poll a socket for pending activity.
3393 This routine builds a detected event mask which is returned to
3394 the caller in the buffer provided.
3396 The ::poll routine calls this routine to determine if the socket
3397 needs to be serviced as a result of connection, error, receive or
3400 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
3402 @param [in] Events Events of interest for this socket
3404 @param [in] pEvents Address to receive the detected events
3406 @param [out] pErrno Address to receive the errno value upon completion.
3408 @retval EFI_SUCCESS - Socket successfully polled
3409 @retval EFI_INVALID_PARAMETER - When pEvents is NULL
3414 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
3420 short DetectedEvents
;
3421 ESL_SOCKET
* pSocket
;
3423 EFI_TPL TplPrevious
;
3426 DEBUG (( DEBUG_POLL
, "Entering SocketPoll\r\n" ));
3431 Status
= EFI_SUCCESS
;
3433 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
3437 // Verify the socket state
3439 Status
= EslSocketIsConfigured ( pSocket
);
3440 if ( !EFI_ERROR ( Status
)) {
3442 // Check for invalid events
3444 ValidEvents
= POLLIN
3446 | POLLOUT
| POLLWRNORM
3453 if ( 0 != ( Events
& ( ~ValidEvents
))) {
3454 DetectedEvents
|= POLLNVAL
;
3455 DEBUG (( DEBUG_INFO
| DEBUG_POLL
,
3456 "ERROR - Invalid event mask, Valid Events: 0x%04x, Invalid Events: 0x%04x\r\n",
3457 Events
& ValidEvents
,
3458 Events
& ( ~ValidEvents
)));
3462 // Synchronize with the socket layer
3464 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
3467 // Increase the network performance by extending the
3468 // polling (idle) loop down into the LAN driver
3470 EslSocketRxPoll ( pSocket
);
3473 // Release the socket layer synchronization
3475 RESTORE_TPL ( TplPrevious
);
3478 // Check for pending connections
3480 if ( 0 != pSocket
->FifoDepth
) {
3482 // A connection is waiting for an accept call
3483 // See posix connect documentation at
3484 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.htm
3486 DetectedEvents
|= POLLIN
| POLLRDNORM
;
3488 if ( pSocket
->bConnected
) {
3490 // A connection is present
3491 // See posix connect documentation at
3492 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.htm
3494 DetectedEvents
|= POLLOUT
| POLLWRNORM
;
3498 // The following bits are set based upon the POSIX poll documentation at
3499 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
3503 // Check for urgent receive data
3505 if ( 0 < pSocket
->RxOobBytes
) {
3506 DetectedEvents
|= POLLRDBAND
| POLLPRI
| POLLIN
;
3510 // Check for normal receive data
3512 if (( 0 < pSocket
->RxBytes
)
3513 || ( EFI_SUCCESS
!= pSocket
->RxError
)) {
3514 DetectedEvents
|= POLLRDNORM
| POLLIN
;
3518 // Handle the receive errors
3520 if (( EFI_SUCCESS
!= pSocket
->RxError
)
3521 && ( 0 == ( DetectedEvents
& POLLIN
))) {
3522 DetectedEvents
|= POLLERR
| POLLIN
| POLLRDNORM
| POLLRDBAND
;
3526 // Check for urgent transmit data buffer space
3528 if (( MAX_TX_DATA
> pSocket
->TxOobBytes
)
3529 || ( EFI_SUCCESS
!= pSocket
->TxError
)) {
3530 DetectedEvents
|= POLLWRBAND
;
3534 // Check for normal transmit data buffer space
3536 if (( MAX_TX_DATA
> pSocket
->TxBytes
)
3537 || ( EFI_SUCCESS
!= pSocket
->TxError
)) {
3538 DetectedEvents
|= POLLWRNORM
;
3542 // Handle the transmit error
3544 if ( EFI_ERROR ( pSocket
->TxError
)) {
3545 DetectedEvents
|= POLLERR
;
3551 // Return the detected events
3553 *pEvents
= DetectedEvents
& ( Events
3559 // Return the operation status
3561 DEBUG (( DEBUG_POLL
, "Exiting SocketPoll, Status: %r\r\n", Status
));
3567 Allocate and initialize a ESL_PORT structure.
3569 This routine initializes an ::ESL_PORT structure for use by
3570 the socket. This routine calls a routine via
3571 ESL_PROTOCOL_API::pfnPortAllocate to initialize the network
3572 specific resources. The resources are released later by the
3573 \ref PortCloseStateMachine.
3575 This support routine is called by:
3577 <li>::EslSocketBind</li>
3578 <li>::EslTcp4ListenComplete</li>
3580 to connect the socket with the underlying network adapter
3583 @param [in] pSocket Address of an ::ESL_SOCKET structure.
3584 @param [in] pService Address of an ::ESL_SERVICE structure.
3585 @param [in] ChildHandle Network protocol child handle
3586 @param [in] pSockAddr Address of a sockaddr structure that contains the
3587 connection point on the local machine. An IPv4 address
3588 of INADDR_ANY specifies that the connection is made to
3589 all of the network stacks on the platform. Specifying a
3590 specific IPv4 address restricts the connection to the
3591 network stack supporting that address. Specifying zero
3592 for the port causes the network layer to assign a port
3593 number from the dynamic range. Specifying a specific
3594 port number causes the network layer to use that port.
3595 @param [in] bBindTest TRUE if EslSocketBindTest should be called
3596 @param [in] DebugFlags Flags for debug messages
3597 @param [out] ppPort Buffer to receive new ::ESL_PORT structure address
3599 @retval EFI_SUCCESS - Socket successfully created
3603 EslSocketPortAllocate (
3604 IN ESL_SOCKET
* pSocket
,
3605 IN ESL_SERVICE
* pService
,
3606 IN EFI_HANDLE ChildHandle
,
3607 IN CONST
struct sockaddr
* pSockAddr
,
3608 IN BOOLEAN bBindTest
,
3609 IN UINTN DebugFlags
,
3610 OUT ESL_PORT
** ppPort
3613 UINTN LengthInBytes
;
3618 EFI_SERVICE_BINDING_PROTOCOL
* pServiceBinding
;
3619 CONST ESL_SOCKET_BINDING
* pSocketBinding
;
3621 EFI_STATUS TempStatus
;
3626 // Verify the socket layer synchronization
3628 VERIFY_TPL ( TPL_SOCKETS
);
3631 // Use for/break instead of goto
3632 pSocketBinding
= pService
->pSocketBinding
;
3635 // Allocate a port structure
3637 pLayer
= &mEslLayer
;
3638 LengthInBytes
= sizeof ( *pPort
)
3639 + ESL_STRUCTURE_ALIGNMENT_BYTES
3640 + (( pSocketBinding
->RxIo
3641 + pSocketBinding
->TxIoNormal
3642 + pSocketBinding
->TxIoUrgent
)
3643 * sizeof ( ESL_IO_MGMT
));
3644 pPort
= (ESL_PORT
*) AllocateZeroPool ( LengthInBytes
);
3645 if ( NULL
== pPort
) {
3646 Status
= EFI_OUT_OF_RESOURCES
;
3647 pSocket
->errno
= ENOMEM
;
3650 DEBUG (( DebugFlags
| DEBUG_POOL
| DEBUG_INIT
,
3651 "0x%08x: Allocate pPort, %d bytes\r\n",
3656 // Initialize the port
3658 pPort
->DebugFlags
= DebugFlags
;
3659 pPort
->Handle
= ChildHandle
;
3660 pPort
->pService
= pService
;
3661 pPort
->pServiceBinding
= pService
->pServiceBinding
;
3662 pPort
->pSocket
= pSocket
;
3663 pPort
->pSocketBinding
= pService
->pSocketBinding
;
3664 pPort
->Signature
= PORT_SIGNATURE
;
3667 // Open the port protocol
3669 Status
= gBS
->OpenProtocol ( pPort
->Handle
,
3670 pSocketBinding
->pNetworkProtocolGuid
,
3671 &pPort
->pProtocol
.v
,
3672 pLayer
->ImageHandle
,
3674 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
3675 if ( EFI_ERROR ( Status
)) {
3676 DEBUG (( DEBUG_ERROR
| DebugFlags
,
3677 "ERROR - Failed to open network protocol GUID on controller 0x%08x\r\n",
3679 pSocket
->errno
= EEXIST
;
3682 DEBUG (( DebugFlags
,
3683 "0x%08x: Network protocol GUID opened on controller 0x%08x\r\n",
3688 // Initialize the port specific resources
3690 Status
= pSocket
->pApi
->pfnPortAllocate ( pPort
,
3692 if ( EFI_ERROR ( Status
)) {
3697 // Set the local address
3699 Status
= pSocket
->pApi
->pfnLocalAddrSet ( pPort
, pSockAddr
, bBindTest
);
3700 if ( EFI_ERROR ( Status
)) {
3705 // Test the address/port configuration
3708 Status
= EslSocketBindTest ( pPort
, pSocket
->pApi
->BindTestErrno
);
3709 if ( EFI_ERROR ( Status
)) {
3715 // Initialize the receive structures
3717 pBuffer
= (UINT8
*)&pPort
[ 1 ];
3718 pBuffer
= &pBuffer
[ ESL_STRUCTURE_ALIGNMENT_BYTES
];
3719 pBuffer
= (UINT8
*)( ESL_STRUCTURE_ALIGNMENT_MASK
& (UINTN
)pBuffer
);
3720 pIo
= (ESL_IO_MGMT
*)pBuffer
;
3721 if (( 0 != pSocketBinding
->RxIo
)
3722 && ( NULL
!= pSocket
->pApi
->pfnRxComplete
)) {
3723 Status
= EslSocketIoInit ( pPort
,
3725 pSocketBinding
->RxIo
,
3727 DebugFlags
| DEBUG_POOL
,
3729 pSocket
->pApi
->pfnRxComplete
);
3730 if ( EFI_ERROR ( Status
)) {
3736 // Initialize the urgent transmit structures
3738 if (( 0 != pSocketBinding
->TxIoUrgent
)
3739 && ( NULL
!= pSocket
->pApi
->pfnTxOobComplete
)) {
3740 Status
= EslSocketIoInit ( pPort
,
3742 pSocketBinding
->TxIoUrgent
,
3744 DebugFlags
| DEBUG_POOL
,
3746 pSocket
->pApi
->pfnTxOobComplete
);
3747 if ( EFI_ERROR ( Status
)) {
3753 // Initialize the normal transmit structures
3755 if (( 0 != pSocketBinding
->TxIoNormal
)
3756 && ( NULL
!= pSocket
->pApi
->pfnTxComplete
)) {
3757 Status
= EslSocketIoInit ( pPort
,
3759 pSocketBinding
->TxIoNormal
,
3761 DebugFlags
| DEBUG_POOL
,
3763 pSocket
->pApi
->pfnTxComplete
);
3764 if ( EFI_ERROR ( Status
)) {
3770 // Add this port to the socket
3772 pPort
->pLinkSocket
= pSocket
->pPortList
;
3773 pSocket
->pPortList
= pPort
;
3774 DEBUG (( DebugFlags
,
3775 "0x%08x: Socket adding port: 0x%08x\r\n",
3780 // Add this port to the service
3782 pPort
->pLinkService
= pService
->pPortList
;
3783 pService
->pPortList
= pPort
;
3793 // Clean up after the error if necessary
3795 if ( EFI_ERROR ( Status
)) {
3796 if ( NULL
!= pPort
) {
3800 EslSocketPortClose ( pPort
);
3804 // Close the port if necessary
3806 pServiceBinding
= pService
->pServiceBinding
;
3807 TempStatus
= pServiceBinding
->DestroyChild ( pServiceBinding
,
3809 if ( !EFI_ERROR ( TempStatus
)) {
3810 DEBUG (( DEBUG_BIND
| DEBUG_POOL
,
3811 "0x%08x: %s port handle destroyed\r\n",
3813 pSocketBinding
->pName
));
3816 DEBUG (( DEBUG_ERROR
| DEBUG_BIND
| DEBUG_POOL
,
3817 "ERROR - Failed to destroy the %s port handle 0x%08x, Status: %r\r\n",
3818 pSocketBinding
->pName
,
3821 ASSERT ( EFI_SUCCESS
== TempStatus
);
3826 // Return the operation status
3828 DBG_EXIT_STATUS ( Status
);
3836 This routine releases the resources allocated by ::EslSocketPortAllocate.
3837 This routine calls ESL_PROTOCOL_API::pfnPortClose to release the network
3840 This routine is called by:
3842 <li>::EslSocketPortAllocate - Port initialization failure</li>
3843 <li>::EslSocketPortCloseRxDone - Last step of close processing</li>
3844 <li>::EslTcp4ConnectComplete - Connection failure and reducing the port list to a single port</li>
3846 See the \ref PortCloseStateMachine section.
3848 @param [in] pPort Address of an ::ESL_PORT structure.
3850 @retval EFI_SUCCESS The port is closed
3851 @retval other Port close error
3855 EslSocketPortClose (
3861 ESL_PACKET
* pPacket
;
3862 ESL_PORT
* pPreviousPort
;
3863 ESL_SERVICE
* pService
;
3864 EFI_SERVICE_BINDING_PROTOCOL
* pServiceBinding
;
3865 CONST ESL_SOCKET_BINDING
* pSocketBinding
;
3866 ESL_SOCKET
* pSocket
;
3872 // Verify the socket layer synchronization
3874 VERIFY_TPL ( TPL_SOCKETS
);
3877 // Locate the port in the socket list
3879 Status
= EFI_SUCCESS
;
3880 pLayer
= &mEslLayer
;
3881 DebugFlags
= pPort
->DebugFlags
;
3882 pSocket
= pPort
->pSocket
;
3883 pPreviousPort
= pSocket
->pPortList
;
3884 if ( pPreviousPort
== pPort
) {
3886 // Remove this port from the head of the socket list
3888 pSocket
->pPortList
= pPort
->pLinkSocket
;
3892 // Locate the port in the middle of the socket list
3894 while (( NULL
!= pPreviousPort
)
3895 && ( pPreviousPort
->pLinkSocket
!= pPort
)) {
3896 pPreviousPort
= pPreviousPort
->pLinkSocket
;
3898 if ( NULL
!= pPreviousPort
) {
3900 // Remove the port from the middle of the socket list
3902 pPreviousPort
->pLinkSocket
= pPort
->pLinkSocket
;
3907 // Locate the port in the service list
3908 // Note that the port may not be in the service list
3909 // if the service has been shutdown.
3911 pService
= pPort
->pService
;
3912 if ( NULL
!= pService
) {
3913 pPreviousPort
= pService
->pPortList
;
3914 if ( pPreviousPort
== pPort
) {
3916 // Remove this port from the head of the service list
3918 pService
->pPortList
= pPort
->pLinkService
;
3922 // Locate the port in the middle of the service list
3924 while (( NULL
!= pPreviousPort
)
3925 && ( pPreviousPort
->pLinkService
!= pPort
)) {
3926 pPreviousPort
= pPreviousPort
->pLinkService
;
3928 if ( NULL
!= pPreviousPort
) {
3930 // Remove the port from the middle of the service list
3932 pPreviousPort
->pLinkService
= pPort
->pLinkService
;
3938 // Empty the urgent receive queue
3940 while ( NULL
!= pSocket
->pRxOobPacketListHead
) {
3941 pPacket
= pSocket
->pRxOobPacketListHead
;
3942 pSocket
->pRxOobPacketListHead
= pPacket
->pNext
;
3943 pSocket
->pApi
->pfnPacketFree ( pPacket
, &pSocket
->RxOobBytes
);
3944 EslSocketPacketFree ( pPacket
, DEBUG_RX
);
3946 pSocket
->pRxOobPacketListTail
= NULL
;
3947 ASSERT ( 0 == pSocket
->RxOobBytes
);
3950 // Empty the receive queue
3952 while ( NULL
!= pSocket
->pRxPacketListHead
) {
3953 pPacket
= pSocket
->pRxPacketListHead
;
3954 pSocket
->pRxPacketListHead
= pPacket
->pNext
;
3955 pSocket
->pApi
->pfnPacketFree ( pPacket
, &pSocket
->RxBytes
);
3956 EslSocketPacketFree ( pPacket
, DEBUG_RX
);
3958 pSocket
->pRxPacketListTail
= NULL
;
3959 ASSERT ( 0 == pSocket
->RxBytes
);
3962 // Empty the receive free queue
3964 while ( NULL
!= pSocket
->pRxFree
) {
3965 pPacket
= pSocket
->pRxFree
;
3966 pSocket
->pRxFree
= pPacket
->pNext
;
3967 EslSocketPacketFree ( pPacket
, DEBUG_RX
);
3971 // Release the network specific resources
3973 if ( NULL
!= pSocket
->pApi
->pfnPortClose
) {
3974 Status
= pSocket
->pApi
->pfnPortClose ( pPort
);
3978 // Done with the normal transmit events
3980 Status
= EslSocketIoFree ( pPort
,
3982 DebugFlags
| DEBUG_POOL
,
3983 "normal transmit" );
3986 // Done with the urgent transmit events
3988 Status
= EslSocketIoFree ( pPort
,
3990 DebugFlags
| DEBUG_POOL
,
3991 "urgent transmit" );
3994 // Done with the receive events
3996 Status
= EslSocketIoFree ( pPort
,
3998 DebugFlags
| DEBUG_POOL
,
4002 // Done with the lower layer network protocol
4004 pSocketBinding
= pPort
->pSocketBinding
;
4005 if ( NULL
!= pPort
->pProtocol
.v
) {
4006 Status
= gBS
->CloseProtocol ( pPort
->Handle
,
4007 pSocketBinding
->pNetworkProtocolGuid
,
4008 pLayer
->ImageHandle
,
4010 if ( !EFI_ERROR ( Status
)) {
4011 DEBUG (( DebugFlags
,
4012 "0x%08x: Network protocol GUID closed on controller 0x%08x\r\n",
4017 DEBUG (( DEBUG_ERROR
| DebugFlags
,
4018 "ERROR - Failed to close network protocol GUID on controller 0x%08x, Status: %r\r\n",
4021 ASSERT ( EFI_SUCCESS
== Status
);
4026 // Done with the network port
4028 pServiceBinding
= pPort
->pServiceBinding
;
4029 if ( NULL
!= pPort
->Handle
) {
4030 Status
= pServiceBinding
->DestroyChild ( pServiceBinding
,
4032 if ( !EFI_ERROR ( Status
)) {
4033 DEBUG (( DebugFlags
| DEBUG_POOL
,
4034 "0x%08x: %s port handle destroyed\r\n",
4036 pSocketBinding
->pName
));
4039 DEBUG (( DEBUG_ERROR
| DebugFlags
| DEBUG_POOL
,
4040 "ERROR - Failed to destroy the %s port handle, Status: %r\r\n",
4041 pSocketBinding
->pName
,
4043 ASSERT ( EFI_SUCCESS
== Status
);
4048 // Release the port structure
4050 Status
= gBS
->FreePool ( pPort
);
4051 if ( !EFI_ERROR ( Status
)) {
4052 DEBUG (( DebugFlags
| DEBUG_POOL
,
4053 "0x%08x: Free pPort, %d bytes\r\n",
4055 sizeof ( *pPort
)));
4058 DEBUG (( DEBUG_ERROR
| DebugFlags
| DEBUG_POOL
,
4059 "ERROR - Failed to free pPort: 0x%08x, Status: %r\r\n",
4062 ASSERT ( EFI_SUCCESS
== Status
);
4066 // Mark the socket as closed if necessary
4068 if ( NULL
== pSocket
->pPortList
) {
4069 pSocket
->State
= SOCKET_STATE_CLOSED
;
4070 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4071 "0x%08x: Socket State: SOCKET_STATE_CLOSED\r\n",
4076 // Return the operation status
4078 DBG_EXIT_STATUS ( Status
);
4086 This routine attempts to complete the port close operation.
4088 This routine is called by the TCP layer upon completion of
4089 the close operation and by ::EslSocketPortCloseTxDone.
4090 See the \ref PortCloseStateMachine section.
4092 @param [in] Event The close completion event
4094 @param [in] pPort Address of an ::ESL_PORT structure.
4098 EslSocketPortCloseComplete (
4107 VERIFY_AT_TPL ( TPL_SOCKETS
);
4110 // Update the port state
4112 pPort
->State
= PORT_STATE_CLOSE_DONE
;
4113 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4114 "0x%08x: Port Close State: PORT_STATE_CLOSE_DONE\r\n",
4118 // Shutdown the receive operation on the port
4120 if ( NULL
!= pPort
->pfnRxCancel
) {
4121 pIo
= pPort
->pRxActive
;
4122 while ( NULL
!= pIo
) {
4123 EslSocketRxCancel ( pPort
, pIo
);
4129 // Determine if the receive operation is pending
4131 Status
= EslSocketPortCloseRxDone ( pPort
);
4132 DBG_EXIT_STATUS ( Status
);
4139 This routine determines the state of the receive operations and
4140 continues the close operation after the pending receive operations
4143 This routine is called by
4145 <li>::EslSocketPortCloseComplete</li>
4146 <li>::EslSocketPortCloseTxDone</li>
4147 <li>::EslSocketRxComplete</li>
4149 to determine the state of the receive operations.
4150 See the \ref PortCloseStateMachine section.
4152 @param [in] pPort Address of an ::ESL_PORT structure.
4154 @retval EFI_SUCCESS The port is closed
4155 @retval EFI_NOT_READY The port is still closing
4156 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
4157 most likely the routine was called already.
4161 EslSocketPortCloseRxDone (
4170 // Verify the socket layer synchronization
4172 VERIFY_TPL ( TPL_SOCKETS
);
4175 // Verify that the port is closing
4177 Status
= EFI_ALREADY_STARTED
;
4178 if ( PORT_STATE_CLOSE_DONE
== pPort
->State
) {
4180 // Determine if the receive operation is pending
4182 Status
= EFI_NOT_READY
;
4183 if ( NULL
== pPort
->pRxActive
) {
4185 // The receive operation is complete
4186 // Update the port state
4188 pPort
->State
= PORT_STATE_CLOSE_RX_DONE
;
4189 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4190 "0x%08x: Port Close State: PORT_STATE_CLOSE_RX_DONE\r\n",
4194 // Complete the port close operation
4196 Status
= EslSocketPortClose ( pPort
);
4199 DEBUG_CODE_BEGIN ();
4203 // Display the outstanding receive operations
4205 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4206 "0x%08x: Port Close: Receive still pending!\r\n",
4208 pIo
= pPort
->pRxActive
;
4209 while ( NULL
!= pIo
) {
4210 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4211 "0x%08x: Packet pending on network adapter\r\n",
4221 // Return the operation status
4223 DBG_EXIT_STATUS ( Status
);
4229 Start the close operation on a port, state 1.
4231 This routine marks the port as closed and initiates the \ref
4232 PortCloseStateMachine. The first step is to allow the \ref
4233 TransmitEngine to run down.
4235 This routine is called by ::EslSocketCloseStart to initiate the socket
4236 network specific close operation on the socket.
4238 @param [in] pPort Address of an ::ESL_PORT structure.
4239 @param [in] bCloseNow Set TRUE to abort active transfers
4240 @param [in] DebugFlags Flags for debug messages
4242 @retval EFI_SUCCESS The port is closed, not normally returned
4243 @retval EFI_NOT_READY The port has started the closing process
4244 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
4245 most likely the routine was called already.
4249 EslSocketPortCloseStart (
4250 IN ESL_PORT
* pPort
,
4251 IN BOOLEAN bCloseNow
,
4255 ESL_SOCKET
* pSocket
;
4261 // Verify the socket layer synchronization
4263 VERIFY_TPL ( TPL_SOCKETS
);
4266 // Mark the port as closing
4268 Status
= EFI_ALREADY_STARTED
;
4269 pSocket
= pPort
->pSocket
;
4270 pSocket
->errno
= EALREADY
;
4271 if ( PORT_STATE_CLOSE_STARTED
> pPort
->State
) {
4274 // Update the port state
4276 pPort
->State
= PORT_STATE_CLOSE_STARTED
;
4277 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4278 "0x%08x: Port Close State: PORT_STATE_CLOSE_STARTED\r\n",
4280 pPort
->bCloseNow
= bCloseNow
;
4281 pPort
->DebugFlags
= DebugFlags
;
4284 // Determine if transmits are complete
4286 Status
= EslSocketPortCloseTxDone ( pPort
);
4290 // Return the operation status
4292 DBG_EXIT_STATUS ( Status
);
4300 This routine determines the state of the transmit engine and
4301 continue the close operation after the transmission is complete.
4302 The next step is to stop the \ref ReceiveEngine.
4303 See the \ref PortCloseStateMachine section.
4305 This routine is called by ::EslSocketPortCloseStart to determine
4306 if the transmission is complete.
4308 @param [in] pPort Address of an ::ESL_PORT structure.
4310 @retval EFI_SUCCESS The port is closed, not normally returned
4311 @retval EFI_NOT_READY The port is still closing
4312 @retval EFI_ALREADY_STARTED Error, the port is in the wrong state,
4313 most likely the routine was called already.
4317 EslSocketPortCloseTxDone (
4322 ESL_SOCKET
* pSocket
;
4328 // Verify the socket layer synchronization
4330 VERIFY_TPL ( TPL_SOCKETS
);
4333 // All transmissions are complete or must be stopped
4334 // Mark the port as TX complete
4336 Status
= EFI_ALREADY_STARTED
;
4337 if ( PORT_STATE_CLOSE_STARTED
== pPort
->State
) {
4339 // Verify that the transmissions are complete
4341 pSocket
= pPort
->pSocket
;
4342 if ( pPort
->bCloseNow
4343 || ( EFI_SUCCESS
!= pSocket
->TxError
)
4344 || (( NULL
== pPort
->pTxActive
)
4345 && ( NULL
== pPort
->pTxOobActive
))) {
4347 // Update the port state
4349 pPort
->State
= PORT_STATE_CLOSE_TX_DONE
;
4350 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4351 "0x%08x: Port Close State: PORT_STATE_CLOSE_TX_DONE\r\n",
4356 // Skip the close operation if the port is not configured
4358 Status
= EFI_SUCCESS
;
4359 pSocket
= pPort
->pSocket
;
4360 if (( pPort
->bConfigured
)
4361 && ( NULL
!= pSocket
->pApi
->pfnPortCloseOp
)) {
4363 // Start the close operation
4365 Status
= pSocket
->pApi
->pfnPortCloseOp ( pPort
);
4366 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4367 "0x%08x: Port Close: Close operation still pending!\r\n",
4369 ASSERT ( EFI_SUCCESS
== Status
);
4373 // The receive operation is complete
4374 // Update the port state
4376 EslSocketPortCloseComplete ( NULL
, pPort
);
4381 // Transmissions are still active, exit
4383 Status
= EFI_NOT_READY
;
4384 pSocket
->errno
= EAGAIN
;
4385 DEBUG_CODE_BEGIN ( );
4387 ESL_PACKET
* pPacket
;
4389 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4390 "0x%08x: Port Close: Transmits are still pending!\r\n",
4394 // Display the pending urgent transmit packets
4396 pPacket
= pSocket
->pTxOobPacketListHead
;
4397 while ( NULL
!= pPacket
) {
4398 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4399 "0x%08x: Packet pending on urgent TX list, %d bytes\r\n",
4401 pPacket
->PacketSize
));
4402 pPacket
= pPacket
->pNext
;
4405 pIo
= pPort
->pTxOobActive
;
4406 while ( NULL
!= pIo
) {
4407 pPacket
= pIo
->pPacket
;
4408 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4409 "0x%08x: Packet active %d bytes, pIo: 0x%08x\r\n",
4411 pPacket
->PacketSize
,
4417 // Display the pending normal transmit packets
4419 pPacket
= pSocket
->pTxPacketListHead
;
4420 while ( NULL
!= pPacket
) {
4421 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4422 "0x%08x: Packet pending on normal TX list, %d bytes\r\n",
4424 pPacket
->PacketSize
));
4425 pPacket
= pPacket
->pNext
;
4428 pIo
= pPort
->pTxActive
;
4429 while ( NULL
!= pIo
) {
4430 pPacket
= pIo
->pPacket
;
4431 DEBUG (( DEBUG_CLOSE
| DEBUG_INFO
,
4432 "0x%08x: Packet active %d bytes, pIo: 0x%08x\r\n",
4434 pPacket
->PacketSize
,
4444 // Return the operation status
4446 DBG_EXIT_STATUS ( Status
);
4452 Receive data from a network connection.
4454 This routine calls the network specific routine to remove the
4455 next portion of data from the receive queue and return it to the
4458 The ::recvfrom routine calls this routine to determine if any data
4459 is received from the remote system. Note that the other routines
4460 ::recv and ::read are layered on top of ::recvfrom.
4462 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
4464 @param [in] Flags Message control flags
4466 @param [in] BufferLength Length of the the buffer
4468 @param [in] pBuffer Address of a buffer to receive the data.
4470 @param [in] pDataLength Number of received data bytes in the buffer.
4472 @param [out] pAddress Network address to receive the remote system address
4474 @param [in,out] pAddressLength Length of the remote network address structure
4476 @param [out] pErrno Address to receive the errno value upon completion.
4478 @retval EFI_SUCCESS - Socket data successfully received
4483 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
4485 IN
size_t BufferLength
,
4487 OUT
size_t * pDataLength
,
4488 OUT
struct sockaddr
* pAddress
,
4489 IN OUT socklen_t
* pAddressLength
,
4494 struct sockaddr_in v4
;
4495 struct sockaddr_in6 v6
;
4497 socklen_t AddressLength
;
4498 BOOLEAN bConsumePacket
;
4499 BOOLEAN bUrgentQueue
;
4501 ESL_PACKET
* pNextPacket
;
4502 ESL_PACKET
* pPacket
;
4504 ESL_PACKET
** ppQueueHead
;
4505 ESL_PACKET
** ppQueueTail
;
4506 struct sockaddr
* pRemoteAddress
;
4507 size_t * pRxDataBytes
;
4508 ESL_SOCKET
* pSocket
;
4511 EFI_TPL TplPrevious
;
4518 Status
= EFI_SUCCESS
;
4521 // Validate the socket
4524 if ( NULL
!= pSocketProtocol
) {
4525 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
4528 // Validate the return address parameters
4530 if (( NULL
== pAddress
) || ( NULL
!= pAddressLength
)) {
4532 // Return the transmit error if necessary
4534 if ( EFI_SUCCESS
!= pSocket
->TxError
) {
4535 pSocket
->errno
= EIO
;
4536 Status
= pSocket
->TxError
;
4537 pSocket
->TxError
= EFI_SUCCESS
;
4541 // Verify the socket state
4543 Status
= EslSocketIsConfigured ( pSocket
);
4544 if ( !EFI_ERROR ( Status
)) {
4546 // Validate the buffer length
4548 if (( NULL
== pDataLength
)
4549 || ( NULL
== pBuffer
)) {
4550 if ( NULL
== pDataLength
) {
4552 "ERROR - pDataLength is NULL!\r\n" ));
4556 "ERROR - pBuffer is NULL!\r\n" ));
4558 Status
= EFI_INVALID_PARAMETER
;
4559 pSocket
->errno
= EFAULT
;
4565 if ( NULL
== pSocket
->pApi
->pfnReceive
) {
4566 Status
= EFI_UNSUPPORTED
;
4567 pSocket
->errno
= ENOTSUP
;
4571 // Zero the receive address if being returned
4573 pRemoteAddress
= NULL
;
4574 if ( NULL
!= pAddress
) {
4575 pRemoteAddress
= (struct sockaddr
*)&Addr
;
4576 ZeroMem ( pRemoteAddress
, sizeof ( Addr
));
4577 pRemoteAddress
->sa_family
= pSocket
->pApi
->AddressFamily
;
4578 pRemoteAddress
->sa_len
= (UINT8
)pSocket
->pApi
->AddressLength
;
4582 // Synchronize with the socket layer
4584 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
4589 Status
= EFI_UNSUPPORTED
;
4590 pSocket
->errno
= ENOTCONN
;
4593 // Verify that the socket is connected
4595 if ( SOCKET_STATE_CONNECTED
== pSocket
->State
) {
4597 // Poll the network to increase performance
4599 EslSocketRxPoll ( pSocket
);
4604 pPort
= pSocket
->pPortList
;
4605 if ( NULL
!= pPort
) {
4607 // Determine the queue head
4609 bUrgentQueue
= (BOOLEAN
)( 0 != ( Flags
& MSG_OOB
));
4610 if ( bUrgentQueue
) {
4611 ppQueueHead
= &pSocket
->pRxOobPacketListHead
;
4612 ppQueueTail
= &pSocket
->pRxOobPacketListTail
;
4613 pRxDataBytes
= &pSocket
->RxOobBytes
;
4616 ppQueueHead
= &pSocket
->pRxPacketListHead
;
4617 ppQueueTail
= &pSocket
->pRxPacketListTail
;
4618 pRxDataBytes
= &pSocket
->RxBytes
;
4622 // Determine if there is any data on the queue
4625 pPacket
= *ppQueueHead
;
4626 if ( NULL
!= pPacket
) {
4628 // Copy the received data
4632 // Attempt to receive a packet
4635 bConsumePacket
= (BOOLEAN
)( 0 == ( Flags
& MSG_PEEK
));
4636 pBuffer
= pSocket
->pApi
->pfnReceive ( pPort
,
4642 (struct sockaddr
*)&Addr
,
4644 *pDataLength
+= DataLength
;
4645 BufferLength
-= DataLength
;
4648 // Determine if the data is being read
4650 pNextPacket
= pPacket
->pNext
;
4651 if ( bConsumePacket
) {
4653 // All done with this packet
4654 // Account for any discarded data
4656 pSocket
->pApi
->pfnPacketFree ( pPacket
, pRxDataBytes
);
4657 if ( 0 != SkipBytes
) {
4659 "0x%08x: Port, packet read, skipping over 0x%08x bytes\r\n",
4665 // Remove this packet from the queue
4667 *ppQueueHead
= pPacket
->pNext
;
4668 if ( NULL
== *ppQueueHead
) {
4669 *ppQueueTail
= NULL
;
4673 // Move the packet to the free queue
4675 pPacket
->pNext
= pSocket
->pRxFree
;
4676 pSocket
->pRxFree
= pPacket
;
4678 "0x%08x: Port freeing packet 0x%08x\r\n",
4683 // Restart the receive operation if necessary
4685 if (( NULL
!= pPort
->pRxFree
)
4686 && ( MAX_RX_DATA
> pSocket
->RxBytes
)) {
4687 EslSocketRxStart ( pPort
);
4692 // Get the next packet
4694 pPacket
= pNextPacket
;
4695 } while (( SOCK_STREAM
== pSocket
->Type
)
4696 && ( NULL
!= pPacket
)
4697 && ( 0 < BufferLength
));
4700 // Successful operation
4702 Status
= EFI_SUCCESS
;
4707 // The queue is empty
4708 // Determine if it is time to return the receive error
4710 if ( EFI_ERROR ( pSocket
->RxError
)
4711 && ( NULL
== pSocket
->pRxPacketListHead
)
4712 && ( NULL
== pSocket
->pRxOobPacketListHead
)) {
4713 Status
= pSocket
->RxError
;
4714 pSocket
->RxError
= EFI_SUCCESS
;
4717 pSocket
->errno
= EIO
;
4720 case EFI_CONNECTION_FIN
:
4722 // Continue to return zero bytes received when the
4723 // peer has successfully closed the connection
4725 pSocket
->RxError
= EFI_CONNECTION_FIN
;
4728 Status
= EFI_SUCCESS
;
4731 case EFI_CONNECTION_REFUSED
:
4732 pSocket
->errno
= ECONNREFUSED
;
4735 case EFI_CONNECTION_RESET
:
4736 pSocket
->errno
= ECONNRESET
;
4739 case EFI_HOST_UNREACHABLE
:
4740 pSocket
->errno
= EHOSTUNREACH
;
4743 case EFI_NETWORK_UNREACHABLE
:
4744 pSocket
->errno
= ENETUNREACH
;
4747 case EFI_PORT_UNREACHABLE
:
4748 pSocket
->errno
= EPROTONOSUPPORT
;
4751 case EFI_PROTOCOL_UNREACHABLE
:
4752 pSocket
->errno
= ENOPROTOOPT
;
4757 Status
= EFI_NOT_READY
;
4758 pSocket
->errno
= EAGAIN
;
4765 // Release the socket layer synchronization
4767 RESTORE_TPL ( TplPrevious
);
4769 if (( !EFI_ERROR ( Status
)) && ( NULL
!= pAddress
)) {
4771 // Return the remote address if requested, truncate if necessary
4773 AddressLength
= pRemoteAddress
->sa_len
;
4774 if ( AddressLength
> *pAddressLength
) {
4775 AddressLength
= *pAddressLength
;
4778 "Returning the remote address, 0x%016x bytes --> 0x%16x\r\n", *pAddressLength
, pAddress
));
4779 ZeroMem ( pAddress
, *pAddressLength
);
4780 CopyMem ( pAddress
, &Addr
, AddressLength
);
4783 // Update the address length
4785 *pAddressLength
= pRemoteAddress
->sa_len
;
4796 // Bad return address pointer and length
4798 Status
= EFI_INVALID_PARAMETER
;
4799 pSocket
->errno
= EINVAL
;
4804 // Return the operation status
4806 if ( NULL
!= pErrno
) {
4807 if ( NULL
!= pSocket
) {
4808 *pErrno
= pSocket
->errno
;
4811 Status
= EFI_INVALID_PARAMETER
;
4815 DBG_EXIT_STATUS ( Status
);
4821 Cancel the receive operations
4823 This routine cancels a pending receive operation.
4824 See the \ref ReceiveEngine section.
4826 This routine is called by ::EslSocketShutdown when the socket
4827 layer is being shutdown.
4829 @param [in] pPort Address of an ::ESL_PORT structure
4830 @param [in] pIo Address of an ::ESL_IO_MGMT structure
4835 IN ESL_PORT
* pPort
,
4836 IN ESL_IO_MGMT
* pIo
4844 // Cancel the outstanding receive
4846 Status
= pPort
->pfnRxCancel ( pPort
->pProtocol
.v
,
4848 if ( !EFI_ERROR ( Status
)) {
4849 DEBUG (( pPort
->DebugFlags
| DEBUG_CLOSE
| DEBUG_INFO
,
4850 "0x%08x: Packet receive aborted on port: 0x%08x\r\n",
4855 DEBUG (( pPort
->DebugFlags
| DEBUG_CLOSE
| DEBUG_INFO
,
4856 "0x%08x: Packet receive pending on Port 0x%08x, Status: %r\r\n",
4866 Process the receive completion
4868 This routine queues the data in FIFO order in either the urgent
4869 or normal data queues depending upon the type of data received.
4870 See the \ref ReceiveEngine section.
4872 This routine is called when some data is received by:
4874 <li>::EslIp4RxComplete</li>
4875 <li>::EslTcp4RxComplete</li>
4876 <li>::EslUdp4RxComplete</li>
4879 @param [in] pIo Address of an ::ESL_IO_MGMT structure
4880 @param [in] Status Receive status
4881 @param [in] LengthInBytes Length of the receive data
4882 @param [in] bUrgent TRUE if urgent data is received and FALSE
4887 EslSocketRxComplete (
4888 IN ESL_IO_MGMT
* pIo
,
4889 IN EFI_STATUS Status
,
4890 IN UINTN LengthInBytes
,
4894 BOOLEAN bUrgentQueue
;
4895 ESL_IO_MGMT
* pIoNext
;
4896 ESL_PACKET
* pPacket
;
4898 ESL_PACKET
* pPrevious
;
4899 ESL_PACKET
** ppQueueHead
;
4900 ESL_PACKET
** ppQueueTail
;
4902 ESL_SOCKET
* pSocket
;
4905 VERIFY_AT_TPL ( TPL_SOCKETS
);
4908 // Locate the active receive packet
4910 pPacket
= pIo
->pPacket
;
4912 pSocket
= pPort
->pSocket
;
4918 // +-------------+ +-------------+ +-------------+
4919 // Active | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
4920 // +-------------+ +-------------+ +-------------+
4922 // +-------------+ +-------------+ +-------------+
4923 // Free | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
4924 // +-------------+ +-------------+ +-------------+
4930 // Remove the IO structure from the active list
4931 // The following code searches for the entry in the list and does not
4932 // assume that the receive operations complete in the order they were
4933 // issued to the UEFI network layer.
4935 pIoNext
= pPort
->pRxActive
;
4936 while (( NULL
!= pIoNext
) && ( pIoNext
!= pIo
) && ( pIoNext
->pNext
!= pIo
))
4938 pIoNext
= pIoNext
->pNext
;
4940 ASSERT ( NULL
!= pIoNext
);
4941 if ( pIoNext
== pIo
) {
4942 pPort
->pRxActive
= pIo
->pNext
; // Beginning of list
4945 pIoNext
->pNext
= pIo
->pNext
; // Middle of list
4949 // Free the IO structure
4951 pIo
->pNext
= pPort
->pRxFree
;
4952 pPort
->pRxFree
= pIo
;
4955 // pRxOobPacketListHead pRxOobPacketListTail
4958 // +------------+ +------------+ +------------+
4959 // Urgent Data | ESL_PACKET |-->| ESL_PACKET |-->| ESL_PACKET |--> NULL
4960 // +------------+ +------------+ +------------+
4962 // +------------+ +------------+ +------------+
4963 // Normal Data | ESL_PACKET |-->| ESL_PACKET |-->| ESL_PACKET |--> NULL
4964 // +------------+ +------------+ +------------+
4967 // pRxPacketListHead pRxPacketListTail
4970 // Determine the queue to use
4972 bUrgentQueue
= (BOOLEAN
)( bUrgent
4973 && pSocket
->pApi
->bOobSupported
4974 && ( !pSocket
->bOobInLine
));
4975 if ( bUrgentQueue
) {
4976 ppQueueHead
= &pSocket
->pRxOobPacketListHead
;
4977 ppQueueTail
= &pSocket
->pRxOobPacketListTail
;
4978 pRxBytes
= &pSocket
->RxOobBytes
;
4981 ppQueueHead
= &pSocket
->pRxPacketListHead
;
4982 ppQueueTail
= &pSocket
->pRxPacketListTail
;
4983 pRxBytes
= &pSocket
->RxBytes
;
4987 // Determine if this receive was successful
4989 if (( !EFI_ERROR ( Status
))
4990 && ( PORT_STATE_CLOSE_STARTED
> pPort
->State
)
4991 && ( !pSocket
->bRxDisable
)) {
4993 // Account for the received data
4995 *pRxBytes
+= LengthInBytes
;
4998 // Log the received data
5000 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5001 "0x%08x: Packet queued on %s queue of port 0x%08x with 0x%08x bytes of %s data\r\n",
5003 bUrgentQueue
? L
"urgent" : L
"normal",
5006 bUrgent
? L
"urgent" : L
"normal" ));
5009 // Add the packet to the list tail.
5011 pPacket
->pNext
= NULL
;
5012 pPrevious
= *ppQueueTail
;
5013 if ( NULL
== pPrevious
) {
5014 *ppQueueHead
= pPacket
;
5017 pPrevious
->pNext
= pPacket
;
5019 *ppQueueTail
= pPacket
;
5022 // Attempt to restart this receive operation
5024 if ( pSocket
->MaxRxBuf
> pSocket
->RxBytes
) {
5025 EslSocketRxStart ( pPort
);
5029 "0x%08x: Port RX suspended, 0x%08x bytes queued\r\n",
5031 pSocket
->RxBytes
));
5035 if ( EFI_ERROR ( Status
)) {
5036 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5037 "ERROR - Receive error on port 0x%08x, packet 0x%08x, Status:%r\r\n",
5044 // Account for the receive bytes and release the driver's buffer
5046 if ( !EFI_ERROR ( Status
)) {
5047 *pRxBytes
+= LengthInBytes
;
5048 pSocket
->pApi
->pfnPacketFree ( pPacket
, pRxBytes
);
5052 // Receive error, free the packet save the error
5054 EslSocketPacketFree ( pPacket
, DEBUG_RX
);
5055 if ( !EFI_ERROR ( pSocket
->RxError
)) {
5056 pSocket
->RxError
= Status
;
5060 // Update the port state
5062 if ( PORT_STATE_CLOSE_STARTED
<= pPort
->State
) {
5063 if ( PORT_STATE_CLOSE_DONE
== pPort
->State
) {
5064 EslSocketPortCloseRxDone ( pPort
);
5068 if ( EFI_ERROR ( Status
)) {
5069 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5070 "0x%08x: Port state: PORT_STATE_RX_ERROR, Status: %r\r\n",
5073 pPort
->State
= PORT_STATE_RX_ERROR
;
5083 Poll a socket for pending receive activity.
5085 This routine is called at elivated TPL and extends the idle
5086 loop which polls a socket down into the LAN driver layer to
5087 determine if there is any receive activity.
5089 The ::EslSocketPoll, ::EslSocketReceive and ::EslSocketTransmit
5090 routines call this routine when there is nothing to do.
5092 @param [in] pSocket Address of an ::EFI_SOCKET structure.
5097 IN ESL_SOCKET
* pSocket
5102 DEBUG (( DEBUG_POLL
, "Entering EslSocketRxPoll\r\n" ));
5105 // Increase the network performance by extending the
5106 // polling (idle) loop down into the LAN driver
5108 pPort
= pSocket
->pPortList
;
5109 while ( NULL
!= pPort
) {
5111 // Poll the LAN adapter
5113 pPort
->pfnRxPoll ( pPort
->pProtocol
.v
);
5116 // Locate the next LAN adapter
5118 pPort
= pPort
->pLinkSocket
;
5121 DEBUG (( DEBUG_POLL
, "Exiting EslSocketRxPoll\r\n" ));
5126 Start a receive operation
5128 This routine posts a receive buffer to the network adapter.
5129 See the \ref ReceiveEngine section.
5131 This support routine is called by:
5133 <li>::EslIp4Receive to restart the receive engine to release flow control.</li>
5134 <li>::EslIp4RxComplete to continue the operation of the receive engine if flow control is not being applied.</li>
5135 <li>::EslIp4SocketIsConfigured to start the receive engine for the new socket.</li>
5136 <li>::EslTcp4ListenComplete to start the recevie engine for the new socket.</li>
5137 <li>::EslTcp4Receive to restart the receive engine to release flow control.</li>
5138 <li>::EslTcp4RxComplete to continue the operation of the receive engine if flow control is not being applied.</li>
5139 <li>::EslUdp4Receive to restart the receive engine to release flow control.</li>
5140 <li>::EslUdp4RxComplete to continue the operation of the receive engine if flow control is not being applied.</li>
5141 <li>::EslUdp4SocketIsConfigured to start the recevie engine for the new socket.</li>
5144 @param [in] pPort Address of an ::ESL_PORT structure.
5154 ESL_PACKET
* pPacket
;
5155 ESL_SOCKET
* pSocket
;
5161 // Determine if a receive is already pending
5163 Status
= EFI_SUCCESS
;
5165 pSocket
= pPort
->pSocket
;
5166 if ( !EFI_ERROR ( pPort
->pSocket
->RxError
)) {
5167 if (( NULL
!= pPort
->pRxFree
)
5168 && ( !pSocket
->bRxDisable
)
5169 && ( PORT_STATE_CLOSE_STARTED
> pPort
->State
)) {
5171 // Start all of the pending receive operations
5173 while ( NULL
!= pPort
->pRxFree
) {
5175 // Determine if there are any free packets
5177 pPacket
= pSocket
->pRxFree
;
5178 if ( NULL
!= pPacket
) {
5180 // Remove this packet from the free list
5182 pSocket
->pRxFree
= pPacket
->pNext
;
5184 "0x%08x: Port removed packet 0x%08x from free list\r\n",
5190 // Allocate a packet structure
5192 Status
= EslSocketPacketAllocate ( &pPacket
,
5193 pSocket
->pApi
->RxPacketBytes
,
5194 pSocket
->pApi
->RxZeroBytes
,
5196 if ( EFI_ERROR ( Status
)) {
5198 DEBUG (( DEBUG_ERROR
| DEBUG_RX
,
5199 "0x%08x: Port failed to allocate RX packet, Status: %r\r\n",
5207 // Connect the IO and packet structures
5209 pIo
= pPort
->pRxFree
;
5210 pIo
->pPacket
= pPacket
;
5213 // Eliminate the need for IP4 and UDP4 specific routines by
5214 // clearing the RX data pointer here.
5216 // No driver buffer for this packet
5218 // +--------------------+
5221 // | +---------------+
5223 // | | RxData --> NULL
5224 // +----+---------------+
5226 pBuffer
= (UINT8
*)pIo
;
5227 pBuffer
= &pBuffer
[ pSocket
->pApi
->RxBufferOffset
];
5228 *(VOID
**)pBuffer
= NULL
;
5231 // Network specific receive packet initialization
5233 if ( NULL
!= pSocket
->pApi
->pfnRxStart
) {
5234 pSocket
->pApi
->pfnRxStart ( pPort
, pIo
);
5238 // Start the receive on the packet
5240 Status
= pPort
->pfnRxStart ( pPort
->pProtocol
.v
, &pIo
->Token
);
5241 if ( !EFI_ERROR ( Status
)) {
5242 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5243 "0x%08x: Packet receive pending on port 0x%08x\r\n",
5247 // Allocate the receive control structure
5249 pPort
->pRxFree
= pIo
->pNext
;
5252 // Mark this receive as pending
5254 pIo
->pNext
= pPort
->pRxActive
;
5255 pPort
->pRxActive
= pIo
;
5259 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5260 "ERROR - Failed to post a receive on port 0x%08x, Status: %r\r\n",
5263 if ( !EFI_ERROR ( pSocket
->RxError
)) {
5265 // Save the error status
5267 pSocket
->RxError
= Status
;
5273 pIo
->pPacket
= NULL
;
5274 pPacket
->pNext
= pSocket
->pRxFree
;
5275 pSocket
->pRxFree
= pPacket
;
5281 if ( NULL
== pPort
->pRxFree
) {
5282 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5283 "0x%08x: Port, no available ESL_IO_MGMT structures\r\n",
5286 if ( pSocket
->bRxDisable
) {
5287 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5288 "0x%08x: Port, receive disabled!\r\n",
5291 if ( PORT_STATE_CLOSE_STARTED
<= pPort
->State
) {
5292 DEBUG (( DEBUG_RX
| DEBUG_INFO
,
5293 "0x%08x: Port, is closing!\r\n",
5299 DEBUG (( DEBUG_ERROR
| DEBUG_RX
,
5300 "ERROR - Previous receive error, Status: %r\r\n",
5301 pPort
->pSocket
->RxError
));
5309 Shutdown the socket receive and transmit operations
5311 This routine sets a flag to stop future transmissions and calls
5312 the network specific layer to cancel the pending receive operation.
5314 The ::shutdown routine calls this routine to stop receive and transmit
5315 operations on the socket.
5317 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
5319 @param [in] How Which operations to stop
5321 @param [out] pErrno Address to receive the errno value upon completion.
5323 @retval EFI_SUCCESS - Socket operations successfully shutdown
5328 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
5335 ESL_SOCKET
* pSocket
;
5337 EFI_TPL TplPrevious
;
5344 Status
= EFI_SUCCESS
;
5347 // Validate the socket
5350 if ( NULL
!= pSocketProtocol
) {
5351 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
5354 // Verify that the socket is connected
5356 if ( pSocket
->bConnected
) {
5358 // Validate the How value
5360 if (( SHUT_RD
<= How
) && ( SHUT_RDWR
>= How
)) {
5362 // Synchronize with the socket layer
5364 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
5367 // Disable the receiver if requested
5369 if (( SHUT_RD
== How
) || ( SHUT_RDWR
== How
)) {
5370 pSocket
->bRxDisable
= TRUE
;
5374 // Disable the transmitter if requested
5376 if (( SHUT_WR
== How
) || ( SHUT_RDWR
== How
)) {
5377 pSocket
->bTxDisable
= TRUE
;
5381 // Cancel the pending receive operations
5383 if ( pSocket
->bRxDisable
) {
5385 // Walk the list of ports
5387 pPort
= pSocket
->pPortList
;
5388 while ( NULL
!= pPort
) {
5390 // Walk the list of active receive operations
5392 pIo
= pPort
->pRxActive
;
5393 while ( NULL
!= pIo
) {
5394 EslSocketRxCancel ( pPort
, pIo
);
5398 // Set the next port
5400 pPort
= pPort
->pLinkSocket
;
5405 // Release the socket layer synchronization
5407 RESTORE_TPL ( TplPrevious
);
5411 // Invalid How value
5413 pSocket
->errno
= EINVAL
;
5414 Status
= EFI_INVALID_PARAMETER
;
5419 // The socket is not connected
5421 pSocket
->errno
= ENOTCONN
;
5422 Status
= EFI_NOT_STARTED
;
5427 // Return the operation status
5429 if ( NULL
!= pErrno
) {
5430 if ( NULL
!= pSocket
) {
5431 *pErrno
= pSocket
->errno
;
5434 Status
= EFI_INVALID_PARAMETER
;
5438 DBG_EXIT_STATUS ( Status
);
5444 Send data using a network connection.
5446 This routine calls the network specific layer to queue the data
5447 for transmission. Eventually the buffer will reach the head of
5448 the queue and will get transmitted over the network by the
5449 \ref TransmitEngine. For datagram
5450 sockets (SOCK_DGRAM and SOCK_RAW) there is no guarantee that
5451 the data reaches the application running on the remote system.
5453 The ::sendto routine calls this routine to send data to the remote
5454 system. Note that ::send and ::write are layered on top of ::sendto.
5456 @param [in] pSocketProtocol Address of an ::EFI_SOCKET_PROTOCOL structure.
5458 @param [in] Flags Message control flags
5460 @param [in] BufferLength Length of the the buffer
5462 @param [in] pBuffer Address of a buffer containing the data to send
5464 @param [in] pDataLength Address to receive the number of data bytes sent
5466 @param [in] pAddress Network address of the remote system address
5468 @param [in] AddressLength Length of the remote network address structure
5470 @param [out] pErrno Address to receive the errno value upon completion.
5472 @retval EFI_SUCCESS - Socket data successfully queued for transmit
5477 IN EFI_SOCKET_PROTOCOL
* pSocketProtocol
,
5479 IN
size_t BufferLength
,
5480 IN CONST UINT8
* pBuffer
,
5481 OUT
size_t * pDataLength
,
5482 IN
const struct sockaddr
* pAddress
,
5483 IN socklen_t AddressLength
,
5487 ESL_SOCKET
* pSocket
;
5489 EFI_TPL TplPrevious
;
5496 Status
= EFI_SUCCESS
;
5499 // Validate the socket
5502 if ( NULL
!= pSocketProtocol
) {
5503 pSocket
= SOCKET_FROM_PROTOCOL ( pSocketProtocol
);
5506 // Return the transmit error if necessary
5508 if ( EFI_SUCCESS
!= pSocket
->TxError
) {
5509 pSocket
->errno
= EIO
;
5510 Status
= pSocket
->TxError
;
5511 pSocket
->TxError
= EFI_SUCCESS
;
5515 // Verify the socket state
5517 Status
= EslSocketIsConfigured ( pSocket
);
5518 if ( !EFI_ERROR ( Status
)) {
5520 // Verify that transmit is still allowed
5522 if ( !pSocket
->bTxDisable
) {
5524 // Validate the buffer length
5526 if (( NULL
== pDataLength
)
5527 && ( 0 > pDataLength
)
5528 && ( NULL
== pBuffer
)) {
5529 if ( NULL
== pDataLength
) {
5531 "ERROR - pDataLength is NULL!\r\n" ));
5533 else if ( NULL
== pBuffer
) {
5535 "ERROR - pBuffer is NULL!\r\n" ));
5539 "ERROR - Data length < 0!\r\n" ));
5541 Status
= EFI_INVALID_PARAMETER
;
5542 pSocket
->errno
= EFAULT
;
5546 // Validate the remote network address
5548 if (( NULL
!= pAddress
)
5549 && ( AddressLength
< pAddress
->sa_len
)) {
5551 "ERROR - Invalid sin_len field in address\r\n" ));
5552 Status
= EFI_INVALID_PARAMETER
;
5553 pSocket
->errno
= EFAULT
;
5559 if ( NULL
== pSocket
->pApi
->pfnTransmit
) {
5560 Status
= EFI_UNSUPPORTED
;
5561 pSocket
->errno
= ENOTSUP
;
5565 // Synchronize with the socket layer
5567 RAISE_TPL ( TplPrevious
, TPL_SOCKETS
);
5570 // Poll the network to increase performance
5572 EslSocketRxPoll ( pSocket
);
5575 // Attempt to buffer the packet for transmission
5577 Status
= pSocket
->pApi
->pfnTransmit ( pSocket
,
5586 // Release the socket layer synchronization
5588 RESTORE_TPL ( TplPrevious
);
5595 // The transmitter was shutdown
5597 pSocket
->errno
= EPIPE
;
5598 Status
= EFI_NOT_STARTED
;
5605 // Return the operation status
5607 if ( NULL
!= pErrno
) {
5608 if ( NULL
!= pSocket
) {
5609 *pErrno
= pSocket
->errno
;
5612 Status
= EFI_INVALID_PARAMETER
;
5616 DBG_EXIT_STATUS ( Status
);
5622 Complete the transmit operation
5624 This support routine handles the transmit completion processing for
5625 the various network layers. It frees the ::ESL_IO_MGMT structure
5626 and and frees packet resources by calling ::EslSocketPacketFree.
5627 Transmit errors are logged in ESL_SOCKET::TxError.
5628 See the \ref TransmitEngine section.
5630 This routine is called by:
5632 <li>::EslIp4TxComplete</li>
5633 <li>::EslTcp4TxComplete</li>
5634 <li>::EslTcp4TxOobComplete</li>
5635 <li>::EslUdp4TxComplete</li>
5638 @param [in] pIo Address of an ::ESL_IO_MGMT structure
5639 @param [in] LengthInBytes Length of the data in bytes
5640 @param [in] Status Transmit operation status
5641 @param [in] pQueueType Zero terminated string describing queue type
5642 @param [in] ppQueueHead Transmit queue head address
5643 @param [in] ppQueueTail Transmit queue tail address
5644 @param [in] ppActive Active transmit queue address
5645 @param [in] ppFree Free transmit queue address
5649 EslSocketTxComplete (
5650 IN ESL_IO_MGMT
* pIo
,
5651 IN UINT32 LengthInBytes
,
5652 IN EFI_STATUS Status
,
5653 IN CONST CHAR8
* pQueueType
,
5654 IN ESL_PACKET
** ppQueueHead
,
5655 IN ESL_PACKET
** ppQueueTail
,
5656 IN ESL_IO_MGMT
** ppActive
,
5657 IN ESL_IO_MGMT
** ppFree
5660 ESL_PACKET
* pCurrentPacket
;
5661 ESL_IO_MGMT
* pIoNext
;
5662 ESL_PACKET
* pNextPacket
;
5663 ESL_PACKET
* pPacket
;
5665 ESL_SOCKET
* pSocket
;
5668 VERIFY_AT_TPL ( TPL_SOCKETS
);
5671 // Locate the active transmit packet
5673 pPacket
= pIo
->pPacket
;
5675 pSocket
= pPort
->pSocket
;
5680 pIo
->pPacket
= NULL
;
5683 // Remove the IO structure from the active list
5685 pIoNext
= *ppActive
;
5686 while (( NULL
!= pIoNext
) && ( pIoNext
!= pIo
) && ( pIoNext
->pNext
!= pIo
))
5688 pIoNext
= pIoNext
->pNext
;
5690 ASSERT ( NULL
!= pIoNext
);
5691 if ( pIoNext
== pIo
) {
5692 *ppActive
= pIo
->pNext
; // Beginning of list
5695 pIoNext
->pNext
= pIo
->pNext
; // Middle of list
5699 // Free the IO structure
5701 pIo
->pNext
= *ppFree
;
5705 // Display the results
5707 DEBUG (( DEBUG_TX
| DEBUG_INFO
,
5708 "0x%08x: pIo Released\r\n",
5712 // Save any transmit error
5714 if ( EFI_ERROR ( Status
)) {
5715 if ( !EFI_ERROR ( pSocket
->TxError
)) {
5716 pSocket
->TxError
= Status
;
5718 DEBUG (( DEBUG_TX
| DEBUG_INFO
,
5719 "ERROR - Transmit failure for %apacket 0x%08x, Status: %r\r\n",
5725 // Empty the normal transmit list
5727 pCurrentPacket
= pPacket
;
5728 pNextPacket
= *ppQueueHead
;
5729 while ( NULL
!= pNextPacket
) {
5730 pPacket
= pNextPacket
;
5731 pNextPacket
= pPacket
->pNext
;
5732 EslSocketPacketFree ( pPacket
, DEBUG_TX
);
5734 *ppQueueHead
= NULL
;
5735 *ppQueueTail
= NULL
;
5736 pPacket
= pCurrentPacket
;
5739 DEBUG (( DEBUG_TX
| DEBUG_INFO
,
5740 "0x%08x: %apacket transmitted %d bytes successfully\r\n",
5746 // Verify the transmit engine is still running
5748 if ( !pPort
->bCloseNow
) {
5750 // Start the next packet transmission
5752 EslSocketTxStart ( pPort
,
5761 // Release this packet
5763 EslSocketPacketFree ( pPacket
, DEBUG_TX
);
5766 // Finish the close operation if necessary
5768 if ( PORT_STATE_CLOSE_STARTED
<= pPort
->State
) {
5770 // Indicate that the transmit is complete
5772 EslSocketPortCloseTxDone ( pPort
);
5780 Transmit data using a network connection.
5782 This support routine starts a transmit operation on the
5783 underlying network layer.
5785 The network specific code calls this routine to start a
5786 transmit operation. See the \ref TransmitEngine section.
5788 @param [in] pPort Address of an ::ESL_PORT structure
5789 @param [in] ppQueueHead Transmit queue head address
5790 @param [in] ppQueueTail Transmit queue tail address
5791 @param [in] ppActive Active transmit queue address
5792 @param [in] ppFree Free transmit queue address
5797 IN ESL_PORT
* pPort
,
5798 IN ESL_PACKET
** ppQueueHead
,
5799 IN ESL_PACKET
** ppQueueTail
,
5800 IN ESL_IO_MGMT
** ppActive
,
5801 IN ESL_IO_MGMT
** ppFree
5806 ESL_PACKET
* pNextPacket
;
5807 ESL_PACKET
* pPacket
;
5808 VOID
** ppTokenData
;
5809 ESL_SOCKET
* pSocket
;
5817 Status
= EFI_SUCCESS
;
5820 // Get the packet from the queue head
5822 pPacket
= *ppQueueHead
;
5824 if (( NULL
!= pPacket
) && ( NULL
!= pIo
)) {
5825 pSocket
= pPort
->pSocket
;
5827 // *ppQueueHead: pSocket->pRxPacketListHead or pSocket->pRxOobPacketListHead
5830 // +------------+ +------------+ +------------+
5831 // Data | ESL_PACKET |-->| ESL_PACKET |-->| ESL_PACKET |--> NULL
5832 // +------------+ +------------+ +------------+
5835 // *ppQueueTail: pSocket->pRxPacketListTail or pSocket->pRxOobPacketListTail
5838 // Remove the packet from the queue
5840 pNextPacket
= pPacket
->pNext
;
5841 *ppQueueHead
= pNextPacket
;
5842 if ( NULL
== pNextPacket
) {
5843 *ppQueueTail
= NULL
;
5845 pPacket
->pNext
= NULL
;
5848 // Eliminate the need for IP4 and UDP4 specific routines by
5849 // connecting the token with the TX data control structure here.
5851 // +--------------------+ +--------------------+
5852 // | ESL_IO_MGMT | | ESL_PACKET |
5854 // | +---------------+ +----------------+ |
5855 // | | Token | | Buffer Length | |
5856 // | | TxData --> | Buffer Address | |
5857 // | | | +----------------+---+
5858 // | | Event | | Data Buffer |
5859 // +----+---------------+ | |
5860 // +--------------------+
5862 // Compute the address of the TxData pointer in the token
5864 pBuffer
= (UINT8
*)&pIo
->Token
;
5865 pBuffer
= &pBuffer
[ pSocket
->TxTokenOffset
];
5866 ppTokenData
= (VOID
**)pBuffer
;
5869 // Compute the address of the TX data control structure in the packet
5871 // * EFI_IP4_TRANSMIT_DATA
5872 // * EFI_TCP4_TRANSMIT_DATA
5873 // * EFI_UDP4_TRANSMIT_DATA
5875 pBuffer
= (UINT8
*)pPacket
;
5876 pBuffer
= &pBuffer
[ pSocket
->TxPacketOffset
];
5879 // Connect the token to the transmit data control structure
5881 *ppTokenData
= (VOID
**)pBuffer
;
5884 // Display the results
5886 DEBUG (( DEBUG_TX
| DEBUG_INFO
,
5887 "0x%08x: pIo allocated for pPacket: 0x%08x\r\n",
5892 // Start the transmit operation
5894 Status
= pPort
->pfnTxStart ( pPort
->pProtocol
.v
,
5896 if ( !EFI_ERROR ( Status
)) {
5898 // Connect the structures
5900 pIo
->pPacket
= pPacket
;
5903 // +-------------+ +-------------+ +-------------+
5904 // Free | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
5905 // +-------------+ +-------------+ +-------------+
5908 // *ppFree: pPort->pTxFree or pTxOobFree
5911 // Remove the IO structure from the queue
5913 *ppFree
= pIo
->pNext
;
5916 // *ppActive: pPort->pTxActive or pTxOobActive
5919 // +-------------+ +-------------+ +-------------+
5920 // Active | ESL_IO_MGMT |-->| ESL_IO_MGMT |-->| ESL_IO_MGMT |--> NULL
5921 // +-------------+ +-------------+ +-------------+
5924 // Mark this packet as active
5926 pIo
->pPacket
= pPacket
;
5927 pIo
->pNext
= *ppActive
;
5932 // Display the transmit error
5934 DEBUG (( DEBUG_TX
| DEBUG_INFO
,
5935 "0x%08x, 0x%08x: pIo, pPacket transmit failure: %r\r\n",
5939 if ( EFI_SUCCESS
== pSocket
->TxError
) {
5940 pSocket
->TxError
= Status
;
5944 // Free the IO structure
5946 pIo
->pNext
= *ppFree
;
5950 // Discard the transmit buffer
5952 EslSocketPacketFree ( pPacket
, DEBUG_TX
);