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[in] 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[in] 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[in] 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[in] Event The event signaled.
86 @param[in] 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[in] 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[in] 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[in] Event The UDP receive request event.
204 @param[in] 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[in] UdpIo The UdpIo to receive packets from.
224 @param[in] CallBack The function to call when receive finished.
225 @param[in] Context The opaque parameter to the CallBack.
226 @param[in] 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[in] UdpIo The UdpIo port to send packet to.
277 @param[in] Packet The user's packet.
278 @param[in] EndPoint The local and remote access point.
279 @param[in] Gateway The overrided next hop.
280 @param[in] CallBack The function to call when transmission completed.
281 @param[in] 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[in] Controller The controller that has the UDP service binding.
380 @param[in] Image The image handle for the driver.
381 @param[in] Configure The function to configure the created UDP child.
382 @param[in] 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[in] UdpIo The UDP_IO_PORT to cancel packet.
478 @param[in] IoStatus The IoStatus to return to the packet owners.
479 @param[in] ToCancel The select funtion to test whether to cancel this
481 @param[in] Context The opaque parameter to the ToCancel.
487 IN UDP_IO_PORT
*UdpIo
,
488 IN EFI_STATUS IoStatus
,
489 IN UDP_IO_TO_CANCEL ToCancel
, OPTIONAL
497 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &UdpIo
->SentDatagram
) {
498 Token
= NET_LIST_USER_STRUCT (Entry
, UDP_TX_TOKEN
, Link
);
500 if ((ToCancel
== NULL
) || (ToCancel (Token
, Context
))) {
501 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &Token
->UdpToken
);
507 Free the UDP_IO_PORT and all its related resources.
509 The function will cancel all sent datagram and receive request.
511 @param[in] UdpIo The UDP_IO_PORT to free.
513 @retval EFI_SUCCESS The UDP_IO_PORT is freed.
519 IN UDP_IO_PORT
*UdpIo
522 UDP_RX_TOKEN
*RxToken
;
525 // Cancel all the sent datagram and receive requests. The
526 // callbacks of transmit requests are executed to allow the
527 // caller to release the resource. The callback of receive
528 // request are NOT executed. This is because it is most
529 // likely that the current user of the UDP IO port is closing
532 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
534 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
535 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &RxToken
->UdpToken
);
539 // Close then destory the UDP child
543 &gEfiUdp4ProtocolGuid
,
548 NetLibDestroyServiceChild (
551 &gEfiUdp4ServiceBindingProtocolGuid
,
555 if (!IsListEmpty(&UdpIo
->Link
)) {
556 RemoveEntryList (&UdpIo
->Link
);
559 gBS
->FreePool (UdpIo
);
565 Clean up the UDP_IO_PORT without freeing it. The function is called when
566 user wants to re-use the UDP_IO_PORT later.
568 It will release all the transmitted datagrams and receive request. It will
569 also configure NULL for the UDP instance.
571 @param[in] UdpIo The UDP_IO_PORT to clean up.
577 IN UDP_IO_PORT
*UdpIo
580 UDP_RX_TOKEN
*RxToken
;
583 // Cancel all the sent datagram and receive requests.
585 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
587 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
588 UdpIo
->Udp
->Cancel (UdpIo
->Udp
, &RxToken
->UdpToken
);
591 UdpIo
->Udp
->Configure (UdpIo
->Udp
, NULL
);
595 Send a packet through the UDP_IO_PORT.
597 The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called
598 when the packet is sent. The optional parameter EndPoint overrides the default
599 address pair if specified.
601 @param[in] UdpIo The UDP_IO_PORT to send the packet through.
602 @param[in] Packet The packet to send.
603 @param[in] EndPoint The local and remote access point. Override the
604 default address pair set during configuration.
605 @param[in] Gateway The gateway to use.
606 @param[in] CallBack The function being called when packet is
607 transmitted or failed.
608 @param[in] Context The opaque parameter passed to CallBack.
610 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet.
611 @retval EFI_SUCCESS The packet is successfully delivered to UDP for
618 IN UDP_IO_PORT
*UdpIo
,
620 IN UDP_POINTS
*EndPoint
, OPTIONAL
622 IN UDP_IO_CALLBACK CallBack
,
629 Token
= UdpIoWrapTx (UdpIo
, Packet
, EndPoint
, Gateway
, CallBack
, Context
);
632 return EFI_OUT_OF_RESOURCES
;
636 // Insert the tx token into SendDatagram list before transmitting it. Remove
637 // it from the list if the returned status is not EFI_SUCCESS.
639 InsertHeadList (&UdpIo
->SentDatagram
, &Token
->Link
);
640 Status
= UdpIo
->Udp
->Transmit (UdpIo
->Udp
, &Token
->UdpToken
);
641 if (EFI_ERROR (Status
)) {
642 RemoveEntryList (&Token
->Link
);
643 UdpIoFreeTxToken (Token
);
652 The select function to cancel a single sent datagram.
654 @param[in] Token The UDP_TX_TOKEN to test against
655 @param[in] Context The NET_BUF of the sent datagram
657 @retval TRUE The packet is to be cancelled.
658 @retval FALSE The packet is not to be cancelled.
661 UdpIoCancelSingleDgram (
662 IN UDP_TX_TOKEN
*Token
,
668 Packet
= (NET_BUF
*) Context
;
670 if (Token
->Packet
== Packet
) {
678 Cancel a single sent datagram.
680 @param[in] UdpIo The UDP_IO_PORT to cancel the packet from
681 @param[in] Packet The packet to cancel
686 UdpIoCancelSentDatagram (
687 IN UDP_IO_PORT
*UdpIo
,
691 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, UdpIoCancelSingleDgram
, Packet
);
695 Issue a receive request to the UDP_IO_PORT.
697 This function is called when upper-layer needs packet from UDP for processing.
698 Only one receive request is acceptable at a time so a common usage model is
699 to invoke this function inside its Callback function when the former packet
702 @param[in] UdpIo The UDP_IO_PORT to receive the packet from.
703 @param[in] CallBack The call back function to execute when the packet
705 @param[in] Context The opaque context passed to Callback.
706 @param[in] HeadLen The length of the upper-layer's protocol header.
708 @retval EFI_ALREADY_STARTED There is already a pending receive request. Only
709 one receive request is supported at a time.
710 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
711 @retval EFI_SUCCESS The receive request is issued successfully.
717 IN UDP_IO_PORT
*UdpIo
,
718 IN UDP_IO_CALLBACK CallBack
,
726 if (UdpIo
->RecvRequest
!= NULL
) {
727 return EFI_ALREADY_STARTED
;
730 Token
= UdpIoCreateRxToken (UdpIo
, CallBack
, Context
, HeadLen
);
733 return EFI_OUT_OF_RESOURCES
;
736 UdpIo
->RecvRequest
= Token
;
737 Status
= UdpIo
->Udp
->Receive (UdpIo
->Udp
, &Token
->UdpToken
);
739 if (EFI_ERROR (Status
)) {
740 UdpIo
->RecvRequest
= NULL
;
741 UdpIoFreeRxToken (Token
);