2 Help functions to access UDP service, it is used by both the DHCP and MTFTP.
4 Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at<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>
17 #include <Protocol/Udp6.h>
19 #include <Library/UdpIoLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/DpcLib.h>
29 Free a UDP_TX_TOKEN. The TX event is closed.
31 @param[in] TxToken The UDP_TX_TOKEN to release.
36 IN UDP_TX_TOKEN
*TxToken
40 if (TxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
41 gBS
->CloseEvent (TxToken
->Token
.Udp4
.Event
);
42 } else if (TxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
) {
43 gBS
->CloseEvent (TxToken
->Token
.Udp6
.Event
);
52 Free a UDP_RX_TOKEN. The RX event is closed.
54 @param[in] RxToken The UDP_RX_TOKEN to release.
59 IN UDP_RX_TOKEN
*RxToken
62 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
63 gBS
->CloseEvent (RxToken
->Token
.Udp4
.Event
);
64 } else if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
) {
65 gBS
->CloseEvent (RxToken
->Token
.Udp6
.Event
);
74 The callback function when the packet is sent by UDP.
76 It will remove the packet from the local list then call
77 the packet owner's callback function set by UdpIoSendDatagram.
79 @param[in] Context The UDP TX Token.
88 UDP_TX_TOKEN
*TxToken
;
90 TxToken
= (UDP_TX_TOKEN
*) Context
;
91 ASSERT (TxToken
->Signature
== UDP_IO_TX_SIGNATURE
);
92 ASSERT ((TxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
93 (TxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
95 RemoveEntryList (&TxToken
->Link
);
97 if (TxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
98 TxToken
->CallBack (TxToken
->Packet
, NULL
, TxToken
->Token
.Udp4
.Status
, TxToken
->Context
);
100 TxToken
->CallBack (TxToken
->Packet
, NULL
, TxToken
->Token
.Udp6
.Status
, TxToken
->Context
);
103 UdpIoFreeTxToken (TxToken
);
107 Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK.
109 @param[in] Event The event signaled.
110 @param[in] Context The UDP TX Token.
121 // Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK
123 QueueDpc (TPL_CALLBACK
, UdpIoOnDgramSentDpc
, Context
);
127 Recycle the received UDP data.
129 @param[in] Context The UDP_RX_TOKEN.
138 UDP_RX_TOKEN
*RxToken
;
140 RxToken
= (UDP_RX_TOKEN
*) Context
;
142 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
143 gBS
->SignalEvent (RxToken
->Token
.Udp4
.Packet
.RxData
->RecycleSignal
);
144 } else if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
) {
145 gBS
->SignalEvent (RxToken
->Token
.Udp6
.Packet
.RxData
->RecycleSignal
);
150 UdpIoFreeRxToken (RxToken
);
154 The event handle for UDP receive request.
156 It will build a NET_BUF from the recieved UDP data, then deliver it
159 @param[in] Context The UDP RX token.
164 UdpIoOnDgramRcvdDpc (
172 UDP_RX_TOKEN
*RxToken
;
173 UDP_END_POINT EndPoint
;
176 RxToken
= (UDP_RX_TOKEN
*) Context
;
178 ZeroMem (&EndPoint
, sizeof(UDP_END_POINT
));
180 ASSERT ((RxToken
->Signature
== UDP_IO_RX_SIGNATURE
) &&
181 (RxToken
== RxToken
->UdpIo
->RecvRequest
));
183 ASSERT ((RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
184 (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
187 // Clear the receive request first in case that the caller
188 // wants to restart the receive in the callback.
190 RxToken
->UdpIo
->RecvRequest
= NULL
;
192 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
193 Token
= &RxToken
->Token
.Udp4
;
194 RxData
= ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Packet
.RxData
;
195 Status
= ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Status
;
197 Token
= &RxToken
->Token
.Udp6
;
198 RxData
= ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Packet
.RxData
;
199 Status
= ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Status
;
202 if (EFI_ERROR (Status
) || RxData
== NULL
) {
203 if (Status
!= EFI_ABORTED
) {
205 // Invoke the CallBack only if the reception is not actively aborted.
207 RxToken
->CallBack (NULL
, NULL
, Status
, RxToken
->Context
);
210 UdpIoFreeRxToken (RxToken
);
215 // Build a NET_BUF from the UDP receive data, then deliver it up.
217 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
219 Netbuf
= NetbufFromExt (
220 (NET_FRAGMENT
*)((EFI_UDP4_RECEIVE_DATA
*) RxData
)->FragmentTable
,
221 ((EFI_UDP4_RECEIVE_DATA
*) RxData
)->FragmentCount
,
223 (UINT32
) RxToken
->HeadLen
,
228 if (Netbuf
== NULL
) {
229 gBS
->SignalEvent (((EFI_UDP4_RECEIVE_DATA
*) RxData
)->RecycleSignal
);
230 RxToken
->CallBack (NULL
, NULL
, EFI_OUT_OF_RESOURCES
, RxToken
->Context
);
232 UdpIoFreeRxToken (RxToken
);
236 Session
= &((EFI_UDP4_RECEIVE_DATA
*) RxData
)->UdpSession
;
237 EndPoint
.LocalPort
= ((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationPort
;
238 EndPoint
.RemotePort
= ((EFI_UDP4_SESSION_DATA
*) Session
)->SourcePort
;
242 &((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationAddress
,
243 sizeof (EFI_IPv4_ADDRESS
)
247 &EndPoint
.RemoteAddr
,
248 &((EFI_UDP4_SESSION_DATA
*) Session
)->SourceAddress
,
249 sizeof (EFI_IPv4_ADDRESS
)
252 EndPoint
.LocalAddr
.Addr
[0] = NTOHL (EndPoint
.LocalAddr
.Addr
[0]);
253 EndPoint
.RemoteAddr
.Addr
[0] = NTOHL (EndPoint
.RemoteAddr
.Addr
[0]);
256 Netbuf
= NetbufFromExt (
257 (NET_FRAGMENT
*)((EFI_UDP6_RECEIVE_DATA
*) RxData
)->FragmentTable
,
258 ((EFI_UDP6_RECEIVE_DATA
*) RxData
)->FragmentCount
,
260 (UINT32
) RxToken
->HeadLen
,
265 if (Netbuf
== NULL
) {
266 gBS
->SignalEvent (((EFI_UDP6_RECEIVE_DATA
*) RxData
)->RecycleSignal
);
267 RxToken
->CallBack (NULL
, NULL
, EFI_OUT_OF_RESOURCES
, RxToken
->Context
);
269 UdpIoFreeRxToken (RxToken
);
273 Session
= &((EFI_UDP6_RECEIVE_DATA
*) RxData
)->UdpSession
;
274 EndPoint
.LocalPort
= ((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationPort
;
275 EndPoint
.RemotePort
= ((EFI_UDP6_SESSION_DATA
*) Session
)->SourcePort
;
279 &((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationAddress
,
280 sizeof (EFI_IPv6_ADDRESS
)
284 &EndPoint
.RemoteAddr
,
285 &((EFI_UDP6_SESSION_DATA
*) Session
)->SourceAddress
,
286 sizeof (EFI_IPv6_ADDRESS
)
289 Ip6Swap128 (&EndPoint
.LocalAddr
.v6
);
290 Ip6Swap128 (&EndPoint
.RemoteAddr
.v6
);
293 RxToken
->CallBack (Netbuf
, &EndPoint
, EFI_SUCCESS
, RxToken
->Context
);
297 Request UdpIoOnDgramRcvdDpc() as a DPC at TPL_CALLBACK.
299 @param[in] Event The UDP receive request event.
300 @param[in] Context The UDP RX token.
311 // Request UdpIoOnDgramRcvdDpc as a DPC at TPL_CALLBACK
313 QueueDpc (TPL_CALLBACK
, UdpIoOnDgramRcvdDpc
, Context
);
317 Create a UDP_RX_TOKEN to wrap the request.
319 @param[in] UdpIo The UdpIo to receive packets from.
320 @param[in] CallBack The function to call when receive finished.
321 @param[in] Context The opaque parameter to the CallBack.
322 @param[in] HeadLen The head length to reserver for the packet.
324 @return The Wrapped request or NULL if failed to allocate resources or some errors happened.
330 IN UDP_IO_CALLBACK CallBack
,
338 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
339 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
341 Token
= AllocatePool (sizeof (UDP_RX_TOKEN
));
347 Token
->Signature
= UDP_IO_RX_SIGNATURE
;
348 Token
->UdpIo
= UdpIo
;
349 Token
->CallBack
= CallBack
;
350 Token
->Context
= Context
;
351 Token
->HeadLen
= HeadLen
;
353 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
355 Token
->Token
.Udp4
.Status
= EFI_NOT_READY
;
356 Token
->Token
.Udp4
.Packet
.RxData
= NULL
;
358 Status
= gBS
->CreateEvent (
363 &Token
->Token
.Udp4
.Event
367 Token
->Token
.Udp6
.Status
= EFI_NOT_READY
;
368 Token
->Token
.Udp6
.Packet
.RxData
= NULL
;
370 Status
= gBS
->CreateEvent (
375 &Token
->Token
.Udp6
.Event
380 if (EFI_ERROR (Status
)) {
389 Wrap a transmit request into a new created UDP_TX_TOKEN.
391 @param[in] UdpIo The UdpIo to send packet to.
392 @param[in] Packet The user's packet.
393 @param[in] EndPoint The local and remote access point.
394 @param[in] Gateway The overrided next hop.
395 @param[in] CallBack The function to call when transmission completed.
396 @param[in] Context The opaque parameter to the call back.
398 @return The wrapped transmission request or NULL if failed to allocate resources
406 IN UDP_END_POINT
*EndPoint OPTIONAL
,
407 IN EFI_IP_ADDRESS
*Gateway OPTIONAL
,
408 IN UDP_IO_CALLBACK CallBack
,
412 UDP_TX_TOKEN
*TxToken
;
420 ASSERT (Packet
!= NULL
);
421 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
422 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
424 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
425 Size
= sizeof (UDP_TX_TOKEN
) + sizeof (EFI_UDP4_FRAGMENT_DATA
) * (Packet
->BlockOpNum
- 1);
427 Size
= sizeof (UDP_TX_TOKEN
) + sizeof (EFI_UDP6_FRAGMENT_DATA
) * (Packet
->BlockOpNum
- 1);
430 TxToken
= AllocatePool (Size
);
432 if (TxToken
== NULL
) {
436 TxToken
->Signature
= UDP_IO_TX_SIGNATURE
;
437 InitializeListHead (&TxToken
->Link
);
439 TxToken
->UdpIo
= UdpIo
;
440 TxToken
->CallBack
= CallBack
;
441 TxToken
->Packet
= Packet
;
442 TxToken
->Context
= Context
;
444 Token
= &(TxToken
->Token
);
445 Count
= Packet
->BlockOpNum
;
447 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
449 ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Status
= EFI_NOT_READY
;
451 Status
= gBS
->CreateEvent (
456 &((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Event
459 if (EFI_ERROR (Status
)) {
464 Data
= &(TxToken
->Data
.Udp4
);
465 ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Packet
.TxData
= Data
;
467 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->UdpSessionData
= NULL
;
468 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->GatewayAddress
= NULL
;
469 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->DataLength
= Packet
->TotalSize
;
473 (NET_FRAGMENT
*)((EFI_UDP4_TRANSMIT_DATA
*) Data
)->FragmentTable
,
477 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->FragmentCount
= Count
;
479 if (EndPoint
!= NULL
) {
480 Ip
= HTONL (EndPoint
->LocalAddr
.Addr
[0]);
482 &TxToken
->Session
.Udp4
.SourceAddress
,
484 sizeof (EFI_IPv4_ADDRESS
)
487 Ip
= HTONL (EndPoint
->RemoteAddr
.Addr
[0]);
489 &TxToken
->Session
.Udp4
.DestinationAddress
,
491 sizeof (EFI_IPv4_ADDRESS
)
494 TxToken
->Session
.Udp4
.SourcePort
= EndPoint
->LocalPort
;
495 TxToken
->Session
.Udp4
.DestinationPort
= EndPoint
->RemotePort
;
496 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->UdpSessionData
= &(TxToken
->Session
.Udp4
);
499 if (Gateway
!= NULL
&& (Gateway
->Addr
[0] != 0)) {
500 Ip
= HTONL (Gateway
->Addr
[0]);
501 CopyMem (&TxToken
->Gateway
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
502 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->GatewayAddress
= &TxToken
->Gateway
;
507 ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Status
= EFI_NOT_READY
;
509 Status
= gBS
->CreateEvent (
514 &((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Event
517 if (EFI_ERROR (Status
)) {
522 Data
= &(TxToken
->Data
.Udp6
);
523 ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Packet
.TxData
= Data
;
524 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->UdpSessionData
= NULL
;
525 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->DataLength
= Packet
->TotalSize
;
529 (NET_FRAGMENT
*)((EFI_UDP6_TRANSMIT_DATA
*) Data
)->FragmentTable
,
533 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->FragmentCount
= Count
;
535 if (EndPoint
!= NULL
) {
537 &TxToken
->Session
.Udp6
.SourceAddress
,
538 &EndPoint
->LocalAddr
.v6
,
539 sizeof(EFI_IPv6_ADDRESS
)
543 &TxToken
->Session
.Udp6
.DestinationAddress
,
544 &EndPoint
->RemoteAddr
.v6
,
545 sizeof(EFI_IPv6_ADDRESS
)
548 TxToken
->Session
.Udp6
.SourcePort
= EndPoint
->LocalPort
;
549 TxToken
->Session
.Udp6
.DestinationPort
= EndPoint
->RemotePort
;
550 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->UdpSessionData
= &(TxToken
->Session
.Udp6
);
558 Creates a UDP_IO to access the UDP service. It creates and configures
561 It locates the UDP service binding prototype on the Controller parameter
562 uses the UDP service binding prototype to create a UDP child (also known as
563 a UDP instance) configures the UDP child by calling Configure function prototype.
564 Any failures in creating or configuring the UDP child return NULL for failure.
566 @param[in] Controller The controller that has the UDP service binding.
568 @param[in] ImageHandle The image handle for the driver.
569 @param[in] Configure The function to configure the created UDP child.
570 @param[in] UdpVersion The UDP protocol version, UDP4 or UDP6.
571 @param[in] Context The opaque parameter for the Configure funtion.
573 @return Newly-created UDP_IO or NULL if failed.
579 IN EFI_HANDLE Controller
,
580 IN EFI_HANDLE ImageHandle
,
581 IN UDP_IO_CONFIG Configure
,
589 ASSERT (Configure
!= NULL
);
590 ASSERT ((UdpVersion
== UDP_IO_UDP4_VERSION
) || (UdpVersion
== UDP_IO_UDP6_VERSION
));
592 UdpIo
= AllocatePool (sizeof (UDP_IO
));
598 UdpIo
->UdpVersion
= UdpVersion
;
599 UdpIo
->Signature
= UDP_IO_SIGNATURE
;
600 InitializeListHead (&UdpIo
->Link
);
603 UdpIo
->Controller
= Controller
;
604 UdpIo
->Image
= ImageHandle
;
606 InitializeListHead (&UdpIo
->SentDatagram
);
607 UdpIo
->RecvRequest
= NULL
;
608 UdpIo
->UdpHandle
= NULL
;
610 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
612 // Create a UDP child then open and configure it
614 Status
= NetLibCreateServiceChild (
617 &gEfiUdp4ServiceBindingProtocolGuid
,
621 if (EFI_ERROR (Status
)) {
625 Status
= gBS
->OpenProtocol (
627 &gEfiUdp4ProtocolGuid
,
628 (VOID
**) &UdpIo
->Protocol
.Udp4
,
631 EFI_OPEN_PROTOCOL_BY_DRIVER
634 if (EFI_ERROR (Status
)) {
638 if (EFI_ERROR (Configure (UdpIo
, Context
))) {
642 Status
= UdpIo
->Protocol
.Udp4
->GetModeData (
643 UdpIo
->Protocol
.Udp4
,
650 if (EFI_ERROR (Status
)) {
656 Status
= NetLibCreateServiceChild (
659 &gEfiUdp6ServiceBindingProtocolGuid
,
663 if (EFI_ERROR (Status
)) {
667 Status
= gBS
->OpenProtocol (
669 &gEfiUdp6ProtocolGuid
,
670 (VOID
**) &UdpIo
->Protocol
.Udp6
,
673 EFI_OPEN_PROTOCOL_BY_DRIVER
676 if (EFI_ERROR (Status
)) {
680 if (EFI_ERROR (Configure (UdpIo
, Context
))) {
684 Status
= UdpIo
->Protocol
.Udp6
->GetModeData (
685 UdpIo
->Protocol
.Udp6
,
692 if (EFI_ERROR (Status
)) {
700 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
701 gBS
->CloseProtocol (UdpIo
->UdpHandle
, &gEfiUdp4ProtocolGuid
, ImageHandle
, Controller
);
703 gBS
->CloseProtocol (UdpIo
->UdpHandle
, &gEfiUdp6ProtocolGuid
, ImageHandle
, Controller
);
707 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
708 NetLibDestroyServiceChild (
711 &gEfiUdp4ServiceBindingProtocolGuid
,
715 NetLibDestroyServiceChild (
718 &gEfiUdp6ServiceBindingProtocolGuid
,
729 Cancel all the sent datagram that pass the selection criteria of ToCancel.
730 If ToCancel is NULL, all the datagrams are cancelled.
732 @param[in] UdpIo The UDP_IO to cancel packet.
733 @param[in] IoStatus The IoStatus to return to the packet owners.
734 @param[in] ToCancel The select funtion to test whether to cancel this
736 @param[in] Context The opaque parameter to the ToCancel.
743 IN EFI_STATUS IoStatus
,
744 IN UDP_IO_TO_CANCEL ToCancel
, OPTIONAL
750 UDP_TX_TOKEN
*TxToken
;
752 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
753 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
755 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &UdpIo
->SentDatagram
) {
756 TxToken
= NET_LIST_USER_STRUCT (Entry
, UDP_TX_TOKEN
, Link
);
758 if ((ToCancel
== NULL
) || (ToCancel (TxToken
, Context
))) {
760 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
761 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &TxToken
->Token
.Udp4
);
763 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &TxToken
->Token
.Udp6
);
770 Free the UDP_IO and all its related resources.
772 The function will cancel all sent datagram and receive request.
774 @param[in] UdpIo The UDP_IO to free.
776 @retval EFI_SUCCESS The UDP_IO is freed.
785 UDP_RX_TOKEN
*RxToken
;
787 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
788 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
791 // Cancel all the sent datagram and receive requests. The
792 // callbacks of transmit requests are executed to allow the
793 // caller to release the resource. The callback of receive
794 // request are NOT executed. This is because it is most
795 // likely that the current user of the UDP IO port is closing
798 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
800 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
802 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
803 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
807 // Close then destroy the Udp4 child
811 &gEfiUdp4ProtocolGuid
,
816 NetLibDestroyServiceChild (
819 &gEfiUdp4ServiceBindingProtocolGuid
,
825 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
826 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
830 // Close then destroy the Udp6 child
834 &gEfiUdp6ProtocolGuid
,
839 NetLibDestroyServiceChild (
842 &gEfiUdp6ServiceBindingProtocolGuid
,
847 if (!IsListEmpty(&UdpIo
->Link
)) {
848 RemoveEntryList (&UdpIo
->Link
);
857 Clean up the UDP_IO without freeing it. The function is called when
858 user wants to re-use the UDP_IO later.
860 It will release all the transmitted datagrams and receive request. It will
861 also configure NULL for the UDP instance.
863 @param[in] UdpIo The UDP_IO to clean up.
872 UDP_RX_TOKEN
*RxToken
;
874 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
875 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
878 // Cancel all the sent datagram and receive requests.
880 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
882 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
883 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
884 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
887 UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, NULL
);
890 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
891 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
894 UdpIo
->Protocol
.Udp6
->Configure (UdpIo
->Protocol
.Udp6
, NULL
);
899 Send a packet through the UDP_IO.
901 The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called
902 when the packet is sent. The optional parameter EndPoint overrides the default
903 address pair if specified.
905 @param[in] UdpIo The UDP_IO to send the packet through.
906 @param[in] Packet The packet to send.
907 @param[in] EndPoint The local and remote access point. Override the
908 default address pair set during configuration.
909 @param[in] Gateway The gateway to use.
910 @param[in] CallBack The function being called when packet is
911 transmitted or failed.
912 @param[in] Context The opaque parameter passed to CallBack.
914 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet.
915 @retval EFI_SUCCESS The packet is successfully delivered to UDP for
924 IN UDP_END_POINT
*EndPoint OPTIONAL
,
925 IN EFI_IP_ADDRESS
*Gateway OPTIONAL
,
926 IN UDP_IO_CALLBACK CallBack
,
930 UDP_TX_TOKEN
*TxToken
;
933 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
934 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
936 TxToken
= UdpIoCreateTxToken (UdpIo
, Packet
, EndPoint
, Gateway
, CallBack
, Context
);
938 if (TxToken
== NULL
) {
939 return EFI_OUT_OF_RESOURCES
;
943 // Insert the tx token into SendDatagram list before transmitting it. Remove
944 // it from the list if the returned status is not EFI_SUCCESS.
946 InsertHeadList (&UdpIo
->SentDatagram
, &TxToken
->Link
);
948 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
949 Status
= UdpIo
->Protocol
.Udp4
->Transmit (UdpIo
->Protocol
.Udp4
, &TxToken
->Token
.Udp4
);
951 Status
= UdpIo
->Protocol
.Udp6
->Transmit (UdpIo
->Protocol
.Udp6
, &TxToken
->Token
.Udp6
);
954 if (EFI_ERROR (Status
)) {
955 RemoveEntryList (&TxToken
->Link
);
956 UdpIoFreeTxToken (TxToken
);
965 The select function to cancel a single sent datagram.
967 @param[in] Token The UDP_TX_TOKEN to test against
968 @param[in] Context The NET_BUF of the sent datagram
970 @retval TRUE The packet is to be cancelled.
971 @retval FALSE The packet is not to be cancelled.
975 UdpIoCancelSingleDgram (
976 IN UDP_TX_TOKEN
*Token
,
982 Packet
= (NET_BUF
*) Context
;
984 if (Token
->Packet
== Packet
) {
992 Cancel a single sent datagram.
994 @param[in] UdpIo The UDP_IO to cancel the packet from
995 @param[in] Packet The packet to cancel
1000 UdpIoCancelSentDatagram (
1005 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, UdpIoCancelSingleDgram
, Packet
);
1009 Issue a receive request to the UDP_IO.
1011 This function is called when upper-layer needs packet from UDP for processing.
1012 Only one receive request is acceptable at a time so a common usage model is
1013 to invoke this function inside its Callback function when the former packet
1016 @param[in] UdpIo The UDP_IO to receive the packet from.
1017 @param[in] CallBack The call back function to execute when the packet
1019 @param[in] Context The opaque context passed to Callback.
1020 @param[in] HeadLen The length of the upper-layer's protocol header.
1022 @retval EFI_ALREADY_STARTED There is already a pending receive request. Only
1023 one receive request is supported at a time.
1024 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
1025 @retval EFI_SUCCESS The receive request is issued successfully.
1026 @retval EFI_UNSUPPORTED The UDP version in UDP_IO is not supported.
1033 IN UDP_IO_CALLBACK CallBack
,
1038 UDP_RX_TOKEN
*RxToken
;
1041 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
1042 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
1044 if (UdpIo
->RecvRequest
!= NULL
) {
1045 return EFI_ALREADY_STARTED
;
1048 RxToken
= UdpIoCreateRxToken (UdpIo
, CallBack
, Context
, HeadLen
);
1050 if (RxToken
== NULL
) {
1051 return EFI_OUT_OF_RESOURCES
;
1054 UdpIo
->RecvRequest
= RxToken
;
1055 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
1056 Status
= UdpIo
->Protocol
.Udp4
->Receive (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
1058 Status
= UdpIo
->Protocol
.Udp6
->Receive (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
1061 if (EFI_ERROR (Status
)) {
1062 UdpIo
->RecvRequest
= NULL
;
1063 UdpIoFreeRxToken (RxToken
);