2 Help functions to access UDP service, it is used by both the DHCP and MTFTP.
4 Copyright (c) 2005 - 2007, Intel Corporation.<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<BR>
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Protocol/Udp4.h>
18 #include <Library/UdpIoLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/BaseMemoryLib.h>
27 Free a UDP_TX_TOKEN. The TX event is closed.
29 @param Token The UDP_TX_TOKEN to release.
34 IN UDP_TX_TOKEN
*Token
37 gBS
->CloseEvent (Token
->UdpToken
.Event
);
38 gBS
->FreePool (Token
);
42 Free a UDP_RX_TOKEN. The RX event is closed.
44 @param Token The UDP_RX_TOKEN to release.
49 IN UDP_RX_TOKEN
*Token
52 gBS
->CloseEvent (Token
->UdpToken
.Event
);
53 gBS
->FreePool (Token
);
57 The callback function when the packet is sent by UDP.
59 It will remove the packet from the local list then call
60 the packet owner's callback function set by UdpIoSendDatagram.
62 @param Context The UDP TX Token.
73 Token
= (UDP_TX_TOKEN
*) Context
;
74 ASSERT (Token
->Signature
== UDP_IO_TX_SIGNATURE
);
76 RemoveEntryList (&Token
->Link
);
77 Token
->CallBack (Token
->Packet
, NULL
, Token
->UdpToken
.Status
, Token
->Context
);
79 UdpIoFreeTxToken (Token
);
83 Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK.
85 @param Event The event signaled.
86 @param Context The UDP TX Token.
97 // Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK
99 NetLibQueueDpc (TPL_CALLBACK
, UdpIoOnDgramSentDpc
, Context
);
103 Recycle the received UDP data.
105 @param Context The UDP_RX_TOKEN
115 Token
= (UDP_RX_TOKEN
*) Context
;
116 gBS
->SignalEvent (Token
->UdpToken
.Packet
.RxData
->RecycleSignal
);
117 UdpIoFreeRxToken (Token
);
121 The event handle for UDP receive request.
123 It will build a NET_BUF from the recieved UDP data, then deliver it
126 @param Context The UDP RX token.
131 UdpIoOnDgramRcvdDpc (
135 EFI_UDP4_COMPLETION_TOKEN
*UdpToken
;
136 EFI_UDP4_RECEIVE_DATA
*UdpRxData
;
137 EFI_UDP4_SESSION_DATA
*UdpSession
;
142 Token
= (UDP_RX_TOKEN
*) Context
;
144 ASSERT ((Token
->Signature
== UDP_IO_RX_SIGNATURE
) &&
145 (Token
== Token
->UdpIo
->RecvRequest
));
148 // Clear the receive request first in case that the caller
149 // wants to restart the receive in the callback.
151 Token
->UdpIo
->RecvRequest
= NULL
;
153 UdpToken
= &Token
->UdpToken
;
154 UdpRxData
= UdpToken
->Packet
.RxData
;
156 if (EFI_ERROR (UdpToken
->Status
) || (UdpRxData
== NULL
)) {
157 if (UdpToken
->Status
!= EFI_ABORTED
) {
159 // Invoke the CallBack only if the reception is not actively aborted.
161 Token
->CallBack (NULL
, NULL
, UdpToken
->Status
, Token
->Context
);
164 UdpIoFreeRxToken (Token
);
169 // Build a NET_BUF from the UDP receive data, then deliver it up.
171 Netbuf
= NetbufFromExt (
172 (NET_FRAGMENT
*) UdpRxData
->FragmentTable
,
173 UdpRxData
->FragmentCount
,
175 (UINT32
) Token
->HeadLen
,
180 if (Netbuf
== NULL
) {
181 gBS
->SignalEvent (UdpRxData
->RecycleSignal
);
182 Token
->CallBack (NULL
, NULL
, EFI_OUT_OF_RESOURCES
, Token
->Context
);
184 UdpIoFreeRxToken (Token
);
188 UdpSession
= &UdpRxData
->UdpSession
;
189 Points
.LocalPort
= UdpSession
->DestinationPort
;
190 Points
.RemotePort
= UdpSession
->SourcePort
;
192 CopyMem (&Points
.LocalAddr
, &UdpSession
->DestinationAddress
, sizeof (IP4_ADDR
));
193 CopyMem (&Points
.RemoteAddr
, &UdpSession
->SourceAddress
, sizeof (IP4_ADDR
));
194 Points
.LocalAddr
= NTOHL (Points
.LocalAddr
);
195 Points
.RemoteAddr
= NTOHL (Points
.RemoteAddr
);
197 Token
->CallBack (Netbuf
, &Points
, EFI_SUCCESS
, Token
->Context
);
201 Request UdpIoOnDgramRcvdDpc() as a DPC at TPL_CALLBACK.
203 @param Event The UDP receive request event.
204 @param Context The UDP RX token.
215 // Request UdpIoOnDgramRcvdDpc as a DPC at TPL_CALLBACK
217 NetLibQueueDpc (TPL_CALLBACK
, UdpIoOnDgramRcvdDpc
, Context
);
221 Create a UDP_RX_TOKEN to wrap the request.
223 @param UdpIo The UdpIo to receive packets from
224 @param CallBack The function to call when receive finished.
225 @param Context The opaque parameter to the CallBack
226 @param HeadLen The head length to reserver for the packet.
228 @return The Wrapped request or NULL if failed to allocate resources or some errors happened.
233 IN UDP_IO_PORT
*UdpIo
,
234 IN UDP_IO_CALLBACK CallBack
,
242 Token
= AllocatePool (sizeof (UDP_RX_TOKEN
));
248 Token
->Signature
= UDP_IO_RX_SIGNATURE
;
249 Token
->UdpIo
= UdpIo
;
250 Token
->CallBack
= CallBack
;
251 Token
->Context
= Context
;
252 Token
->HeadLen
= HeadLen
;
254 Token
->UdpToken
.Status
= EFI_NOT_READY
;
255 Token
->UdpToken
.Packet
.RxData
= NULL
;
257 Status
= gBS
->CreateEvent (
262 &Token
->UdpToken
.Event
265 if (EFI_ERROR (Status
)) {
266 gBS
->FreePool (Token
);
274 Wrap a transmit request into a UDP_TX_TOKEN.
276 @param UdpIo The UdpIo port to send packet to
277 @param Packet The user's packet
278 @param EndPoint The local and remote access point
279 @param Gateway The overrided next hop
280 @param CallBack The function to call when transmission completed.
281 @param Context The opaque parameter to the call back
283 @return The wrapped transmission request or NULL if failed to allocate resources
289 IN UDP_IO_PORT
*UdpIo
,
291 IN UDP_POINTS
*EndPoint OPTIONAL
,
293 IN UDP_IO_CALLBACK CallBack
,
298 EFI_UDP4_COMPLETION_TOKEN
*UdpToken
;
299 EFI_UDP4_TRANSMIT_DATA
*UdpTxData
;
304 Token
= AllocatePool (sizeof (UDP_TX_TOKEN
) +
305 sizeof (EFI_UDP4_FRAGMENT_DATA
) * (Packet
->BlockOpNum
- 1));
311 Token
->Signature
= UDP_IO_TX_SIGNATURE
;
312 InitializeListHead (&Token
->Link
);
314 Token
->UdpIo
= UdpIo
;
315 Token
->CallBack
= CallBack
;
316 Token
->Packet
= Packet
;
317 Token
->Context
= Context
;
319 UdpToken
= &(Token
->UdpToken
);
320 UdpToken
->Status
= EFI_NOT_READY
;
322 Status
= gBS
->CreateEvent (
330 if (EFI_ERROR (Status
)) {
331 gBS
->FreePool (Token
);
335 UdpTxData
= &Token
->UdpTxData
;
336 UdpToken
->Packet
.TxData
= UdpTxData
;
338 UdpTxData
->UdpSessionData
= NULL
;
339 UdpTxData
->GatewayAddress
= NULL
;
341 if (EndPoint
!= NULL
) {
342 Ip
= HTONL (EndPoint
->LocalAddr
);
343 CopyMem (&Token
->UdpSession
.SourceAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
345 Ip
= HTONL (EndPoint
->RemoteAddr
);
346 CopyMem (&Token
->UdpSession
.DestinationAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
348 Token
->UdpSession
.SourcePort
= EndPoint
->LocalPort
;
349 Token
->UdpSession
.DestinationPort
= EndPoint
->RemotePort
;
350 UdpTxData
->UdpSessionData
= &Token
->UdpSession
;
354 Ip
= HTONL (Gateway
);
355 CopyMem (&Token
->Gateway
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
357 UdpTxData
->GatewayAddress
= &Token
->Gateway
;
360 UdpTxData
->DataLength
= Packet
->TotalSize
;
361 Count
= Packet
->BlockOpNum
;
362 NetbufBuildExt (Packet
, (NET_FRAGMENT
*) UdpTxData
->FragmentTable
, &Count
);
363 UdpTxData
->FragmentCount
= Count
;
369 Create a UDP_IO_PORT to access the UDP service. It will create and configure
372 The function will locate the UDP service binding prototype on the Controller
373 parameter and use it to create a UDP child (aka Udp instance). Then the UDP
374 child will be configured by calling Configure function prototype. Any failures
375 in creating or configure the UDP child will lead to the failure of UDP_IO_PORT
378 @param Controller The controller that has the UDP service binding
380 @param Image The image handle for the driver.
381 @param Configure The function to configure the created UDP child
382 @param Context The opaque parameter for the Configure funtion.
384 @return Newly-created UDP_IO_PORT or NULL if failed.
390 IN EFI_HANDLE Controller
,
392 IN UDP_IO_CONFIG Configure
,
399 ASSERT (Configure
!= NULL
);
401 UdpIo
= AllocatePool (sizeof (UDP_IO_PORT
));
407 UdpIo
->Signature
= UDP_IO_SIGNATURE
;
408 InitializeListHead (&UdpIo
->Link
);
411 UdpIo
->Controller
= Controller
;
412 UdpIo
->Image
= Image
;
414 InitializeListHead (&UdpIo
->SentDatagram
);
415 UdpIo
->RecvRequest
= NULL
;
416 UdpIo
->UdpHandle
= NULL
;
419 // Create a UDP child then open and configure it
421 Status
= NetLibCreateServiceChild (
424 &gEfiUdp4ServiceBindingProtocolGuid
,
428 if (EFI_ERROR (Status
)) {
432 Status
= gBS
->OpenProtocol (
434 &gEfiUdp4ProtocolGuid
,
435 (VOID
**) &UdpIo
->Udp
,
438 EFI_OPEN_PROTOCOL_BY_DRIVER
441 if (EFI_ERROR (Status
)) {
445 if (EFI_ERROR (Configure (UdpIo
, Context
))) {
449 Status
= UdpIo
->Udp
->GetModeData (UdpIo
->Udp
, NULL
, NULL
, NULL
, &UdpIo
->SnpMode
);
451 if (EFI_ERROR (Status
)) {
458 gBS
->CloseProtocol (UdpIo
->UdpHandle
, &gEfiUdp4ProtocolGuid
, Image
, Controller
);
461 NetLibDestroyServiceChild (
464 &gEfiUdp4ServiceBindingProtocolGuid
,
469 gBS
->FreePool (UdpIo
);
474 Cancel all the sent datagram that pass the selection criteria of ToCancel.
475 If ToCancel is NULL, all the datagrams are cancelled.
477 @param UdpIo The UDP_IO_PORT to cancel packet
478 @param IoStatus The IoStatus to return to the packet owners.
479 @param ToCancel The select funtion to test whether to cancel this
481 @param Context The opaque parameter to the ToCancel.
486 IN UDP_IO_PORT
*UdpIo
,
487 IN EFI_STATUS IoStatus
,
488 IN UDP_IO_TO_CANCEL ToCancel
, OPTIONAL
496 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &UdpIo
->SentDatagram
) {
497 Token
= NET_LIST_USER_STRUCT (Entry
, UDP_TX_TOKEN
, Link
);
499 if ((ToCancel
== NULL
) || (ToCancel (Token
, Context
))) {
500 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &Token
->UdpToken
);
506 Free the UDP_IO_PORT and all its related resources.
508 The function will cancel all sent datagram and receive request.
510 @param UdpIo The UDP_IO_PORT to free.
512 @retval EFI_SUCCESS The UDP_IO_PORT is freed.
518 IN UDP_IO_PORT
*UdpIo
521 UDP_RX_TOKEN
*RxToken
;
524 // Cancel all the sent datagram and receive requests. The
525 // callbacks of transmit requests are executed to allow the
526 // caller to release the resource. The callback of receive
527 // request are NOT executed. This is because it is most
528 // likely that the current user of the UDP IO port is closing
531 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
533 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
534 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &RxToken
->UdpToken
);
538 // Close then destory the UDP child
542 &gEfiUdp4ProtocolGuid
,
547 NetLibDestroyServiceChild (
550 &gEfiUdp4ServiceBindingProtocolGuid
,
554 if (!IsListEmpty(&UdpIo
->Link
)) {
555 RemoveEntryList (&UdpIo
->Link
);
558 gBS
->FreePool (UdpIo
);
564 Clean up the UDP_IO_PORT without freeing it. The function is called when
565 user wants to re-use the UDP_IO_PORT later.
567 It will release all the transmitted datagrams and receive request. It will
568 also configure NULL for the UDP instance.
570 @param UdpIo The UDP_IO_PORT to clean up.
576 IN UDP_IO_PORT
*UdpIo
579 UDP_RX_TOKEN
*RxToken
;
582 // Cancel all the sent datagram and receive requests.
584 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
586 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
587 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &RxToken
->UdpToken
);
590 UdpIo
->Udp
->Configure (UdpIo
->Udp
, NULL
);
594 Send a packet through the UDP_IO_PORT.
596 The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called
597 when the packet is sent. The optional parameter EndPoint overrides the default
598 address pair if specified.
600 @param UdpIo The UDP_IO_PORT to send the packet through
601 @param Packet The packet to send
602 @param EndPoint The local and remote access point. Override the
603 default address pair set during configuration.
604 @param Gateway The gateway to use
605 @param CallBack The function being called when packet is
606 transmitted or failed.
607 @param Context The opaque parameter passed to CallBack
609 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet
610 @retval EFI_SUCCESS The packet is successfully delivered to UDP for
617 IN UDP_IO_PORT
*UdpIo
,
619 IN UDP_POINTS
*EndPoint
, OPTIONAL
621 IN UDP_IO_CALLBACK CallBack
,
628 Token
= UdpIoWrapTx (UdpIo
, Packet
, EndPoint
, Gateway
, CallBack
, Context
);
631 return EFI_OUT_OF_RESOURCES
;
635 // Insert the tx token into SendDatagram list before transmitting it. Remove
636 // it from the list if the returned status is not EFI_SUCCESS.
638 InsertHeadList (&UdpIo
->SentDatagram
, &Token
->Link
);
639 Status
= UdpIo
->Udp
->Transmit (UdpIo
->Udp
, &Token
->UdpToken
);
640 if (EFI_ERROR (Status
)) {
641 RemoveEntryList (&Token
->Link
);
642 UdpIoFreeTxToken (Token
);
651 The select function to cancel a single sent datagram.
653 @param Token The UDP_TX_TOKEN to test against
654 @param Context The NET_BUF of the sent datagram
656 @retval TRUE The packet is to be cancelled.
657 @retval FALSE The packet is not to be cancelled.
660 UdpIoCancelSingleDgram (
661 IN UDP_TX_TOKEN
*Token
,
667 Packet
= (NET_BUF
*) Context
;
669 if (Token
->Packet
== Packet
) {
677 Cancel a single sent datagram.
679 @param UdpIo The UDP_IO_PORT to cancel the packet from
680 @param Packet The packet to cancel
685 UdpIoCancelSentDatagram (
686 IN UDP_IO_PORT
*UdpIo
,
690 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, UdpIoCancelSingleDgram
, Packet
);
694 Issue a receive request to the UDP_IO_PORT.
696 This function is called when upper-layer needs packet from UDP for processing.
697 Only one receive request is acceptable at a time so a common usage model is
698 to invoke this function inside its Callback function when the former packet
701 @param UdpIo The UDP_IO_PORT to receive the packet from.
702 @param CallBack The call back function to execute when the packet
704 @param Context The opaque context passed to Callback
705 @param HeadLen The length of the upper-layer's protocol header
707 @retval EFI_ALREADY_STARTED There is already a pending receive request. Only
708 one receive request is supported at a time.
709 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
710 @retval EFI_SUCCESS The receive request is issued successfully.
716 IN UDP_IO_PORT
*UdpIo
,
717 IN UDP_IO_CALLBACK CallBack
,
725 if (UdpIo
->RecvRequest
!= NULL
) {
726 return EFI_ALREADY_STARTED
;
729 Token
= UdpIoCreateRxToken (UdpIo
, CallBack
, Context
, HeadLen
);
732 return EFI_OUT_OF_RESOURCES
;
735 UdpIo
->RecvRequest
= Token
;
736 Status
= UdpIo
->Udp
->Receive (UdpIo
->Udp
, &Token
->UdpToken
);
738 if (EFI_ERROR (Status
)) {
739 UdpIo
->RecvRequest
= NULL
;
740 UdpIoFreeRxToken (Token
);