2 Help functions to access UDP service, it is used by both the DHCP and MTFTP.
4 Copyright (c) 2005 - 2009, 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>
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.
137 UDP_RX_TOKEN
*RxToken
;
139 RxToken
= (UDP_RX_TOKEN
*) Context
;
141 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
142 gBS
->SignalEvent (RxToken
->Token
.Udp4
.Packet
.RxData
->RecycleSignal
);
143 } else if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
) {
144 gBS
->SignalEvent (RxToken
->Token
.Udp6
.Packet
.RxData
->RecycleSignal
);
149 UdpIoFreeRxToken (RxToken
);
153 The event handle for UDP receive request.
155 It will build a NET_BUF from the recieved UDP data, then deliver it
158 @param[in] Context The UDP RX token.
163 UdpIoOnDgramRcvdDpc (
171 UDP_RX_TOKEN
*RxToken
;
172 UDP_END_POINT EndPoint
;
175 RxToken
= (UDP_RX_TOKEN
*) Context
;
177 ZeroMem (&EndPoint
, sizeof(UDP_END_POINT
));
179 ASSERT ((RxToken
->Signature
== UDP_IO_RX_SIGNATURE
) &&
180 (RxToken
== RxToken
->UdpIo
->RecvRequest
));
182 ASSERT ((RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
183 (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
186 // Clear the receive request first in case that the caller
187 // wants to restart the receive in the callback.
189 RxToken
->UdpIo
->RecvRequest
= NULL
;
191 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
192 Token
= &RxToken
->Token
.Udp4
;
193 RxData
= ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Packet
.RxData
;
194 Status
= ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Status
;
196 Token
= &RxToken
->Token
.Udp6
;
197 RxData
= ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Packet
.RxData
;
198 Status
= ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Status
;
201 if (EFI_ERROR (Status
) || RxData
== NULL
) {
202 if (Status
!= EFI_ABORTED
) {
204 // Invoke the CallBack only if the reception is not actively aborted.
206 RxToken
->CallBack (NULL
, NULL
, Status
, RxToken
->Context
);
209 UdpIoFreeRxToken (RxToken
);
214 // Build a NET_BUF from the UDP receive data, then deliver it up.
216 if (RxToken
->UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
218 Netbuf
= NetbufFromExt (
219 (NET_FRAGMENT
*)((EFI_UDP4_RECEIVE_DATA
*) RxData
)->FragmentTable
,
220 ((EFI_UDP4_RECEIVE_DATA
*) RxData
)->FragmentCount
,
222 (UINT32
) RxToken
->HeadLen
,
227 if (Netbuf
== NULL
) {
228 gBS
->SignalEvent (((EFI_UDP4_RECEIVE_DATA
*) RxData
)->RecycleSignal
);
229 RxToken
->CallBack (NULL
, NULL
, EFI_OUT_OF_RESOURCES
, RxToken
->Context
);
231 UdpIoFreeRxToken (RxToken
);
235 Session
= &((EFI_UDP4_RECEIVE_DATA
*) RxData
)->UdpSession
;
236 EndPoint
.LocalPort
= ((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationPort
;
237 EndPoint
.RemotePort
= ((EFI_UDP4_SESSION_DATA
*) Session
)->SourcePort
;
241 &((EFI_UDP4_SESSION_DATA
*) Session
)->DestinationAddress
,
242 sizeof (EFI_IPv4_ADDRESS
)
246 &EndPoint
.RemoteAddr
,
247 &((EFI_UDP4_SESSION_DATA
*) Session
)->SourceAddress
,
248 sizeof (EFI_IPv4_ADDRESS
)
251 EndPoint
.LocalAddr
.Addr
[0] = NTOHL (EndPoint
.LocalAddr
.Addr
[0]);
252 EndPoint
.RemoteAddr
.Addr
[0] = NTOHL (EndPoint
.RemoteAddr
.Addr
[0]);
255 Netbuf
= NetbufFromExt (
256 (NET_FRAGMENT
*)((EFI_UDP6_RECEIVE_DATA
*) RxData
)->FragmentTable
,
257 ((EFI_UDP6_RECEIVE_DATA
*) RxData
)->FragmentCount
,
259 (UINT32
) RxToken
->HeadLen
,
264 if (Netbuf
== NULL
) {
265 gBS
->SignalEvent (((EFI_UDP6_RECEIVE_DATA
*) RxData
)->RecycleSignal
);
266 RxToken
->CallBack (NULL
, NULL
, EFI_OUT_OF_RESOURCES
, RxToken
->Context
);
268 UdpIoFreeRxToken (RxToken
);
272 Session
= &((EFI_UDP6_RECEIVE_DATA
*) RxData
)->UdpSession
;
273 EndPoint
.LocalPort
= ((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationPort
;
274 EndPoint
.RemotePort
= ((EFI_UDP6_SESSION_DATA
*) Session
)->SourcePort
;
278 &((EFI_UDP6_SESSION_DATA
*) Session
)->DestinationAddress
,
279 sizeof (EFI_IPv6_ADDRESS
)
283 &EndPoint
.RemoteAddr
,
284 &((EFI_UDP6_SESSION_DATA
*) Session
)->SourceAddress
,
285 sizeof (EFI_IPv6_ADDRESS
)
288 Ip6Swap128 (&EndPoint
.LocalAddr
.v6
);
289 Ip6Swap128 (&EndPoint
.RemoteAddr
.v6
);
292 RxToken
->CallBack (Netbuf
, &EndPoint
, EFI_SUCCESS
, RxToken
->Context
);
296 Request UdpIoOnDgramRcvdDpc() as a DPC at TPL_CALLBACK.
298 @param[in] Event The UDP receive request event.
299 @param[in] Context The UDP RX token.
310 // Request UdpIoOnDgramRcvdDpc as a DPC at TPL_CALLBACK
312 QueueDpc (TPL_CALLBACK
, UdpIoOnDgramRcvdDpc
, Context
);
316 Create a UDP_RX_TOKEN to wrap the request.
318 @param[in] UdpIo The UdpIo to receive packets from.
319 @param[in] CallBack The function to call when receive finished.
320 @param[in] Context The opaque parameter to the CallBack.
321 @param[in] HeadLen The head length to reserver for the packet.
323 @return The Wrapped request or NULL if failed to allocate resources or some errors happened.
329 IN UDP_IO_CALLBACK CallBack
,
337 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
338 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
340 Token
= AllocatePool (sizeof (UDP_RX_TOKEN
));
346 Token
->Signature
= UDP_IO_RX_SIGNATURE
;
347 Token
->UdpIo
= UdpIo
;
348 Token
->CallBack
= CallBack
;
349 Token
->Context
= Context
;
350 Token
->HeadLen
= HeadLen
;
352 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
354 Token
->Token
.Udp4
.Status
= EFI_NOT_READY
;
355 Token
->Token
.Udp4
.Packet
.RxData
= NULL
;
357 Status
= gBS
->CreateEvent (
362 &Token
->Token
.Udp4
.Event
366 Token
->Token
.Udp6
.Status
= EFI_NOT_READY
;
367 Token
->Token
.Udp6
.Packet
.RxData
= NULL
;
369 Status
= gBS
->CreateEvent (
374 &Token
->Token
.Udp6
.Event
379 if (EFI_ERROR (Status
)) {
388 Wrap a transmit request into a new created UDP_TX_TOKEN.
390 @param[in] UdpIo The UdpIo to send packet to.
391 @param[in] Packet The user's packet.
392 @param[in] EndPoint The local and remote access point.
393 @param[in] Gateway The overrided next hop.
394 @param[in] CallBack The function to call when transmission completed.
395 @param[in] Context The opaque parameter to the call back.
397 @return The wrapped transmission request or NULL if failed to allocate resources
405 IN UDP_END_POINT
*EndPoint OPTIONAL
,
406 IN EFI_IP_ADDRESS
*Gateway OPTIONAL
,
407 IN UDP_IO_CALLBACK CallBack
,
411 UDP_TX_TOKEN
*TxToken
;
419 ASSERT (Packet
!= NULL
);
420 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
421 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
423 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
424 Size
= sizeof (UDP_TX_TOKEN
) + sizeof (EFI_UDP4_FRAGMENT_DATA
) * (Packet
->BlockOpNum
- 1);
426 Size
= sizeof (UDP_TX_TOKEN
) + sizeof (EFI_UDP6_FRAGMENT_DATA
) * (Packet
->BlockOpNum
- 1);
429 TxToken
= AllocatePool (Size
);
431 if (TxToken
== NULL
) {
435 TxToken
->Signature
= UDP_IO_TX_SIGNATURE
;
436 InitializeListHead (&TxToken
->Link
);
438 TxToken
->UdpIo
= UdpIo
;
439 TxToken
->CallBack
= CallBack
;
440 TxToken
->Packet
= Packet
;
441 TxToken
->Context
= Context
;
443 Token
= &(TxToken
->Token
);
444 Count
= Packet
->BlockOpNum
;
446 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
448 ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Status
= EFI_NOT_READY
;
450 Status
= gBS
->CreateEvent (
455 &((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Event
458 if (EFI_ERROR (Status
)) {
463 Data
= &(TxToken
->Data
.Udp4
);
464 ((EFI_UDP4_COMPLETION_TOKEN
*) Token
)->Packet
.TxData
= Data
;
466 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->UdpSessionData
= NULL
;
467 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->GatewayAddress
= NULL
;
468 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->DataLength
= Packet
->TotalSize
;
472 (NET_FRAGMENT
*)((EFI_UDP4_TRANSMIT_DATA
*) Data
)->FragmentTable
,
476 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->FragmentCount
= Count
;
478 if (EndPoint
!= NULL
) {
479 Ip
= HTONL (EndPoint
->LocalAddr
.Addr
[0]);
481 &TxToken
->Session
.Udp4
.SourceAddress
,
483 sizeof (EFI_IPv4_ADDRESS
)
486 Ip
= HTONL (EndPoint
->RemoteAddr
.Addr
[0]);
488 &TxToken
->Session
.Udp4
.DestinationAddress
,
490 sizeof (EFI_IPv4_ADDRESS
)
493 TxToken
->Session
.Udp4
.SourcePort
= EndPoint
->LocalPort
;
494 TxToken
->Session
.Udp4
.DestinationPort
= EndPoint
->RemotePort
;
495 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->UdpSessionData
= &(TxToken
->Session
.Udp4
);
498 if (Gateway
!= NULL
&& (Gateway
->Addr
[0] != 0)) {
499 Ip
= HTONL (Gateway
->Addr
[0]);
500 CopyMem (&TxToken
->Gateway
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
501 ((EFI_UDP4_TRANSMIT_DATA
*) Data
)->GatewayAddress
= &TxToken
->Gateway
;
506 ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Status
= EFI_NOT_READY
;
508 Status
= gBS
->CreateEvent (
513 &((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Event
516 if (EFI_ERROR (Status
)) {
521 Data
= &(TxToken
->Data
.Udp6
);
522 ((EFI_UDP6_COMPLETION_TOKEN
*) Token
)->Packet
.TxData
= Data
;
523 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->UdpSessionData
= NULL
;
524 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->DataLength
= Packet
->TotalSize
;
528 (NET_FRAGMENT
*)((EFI_UDP6_TRANSMIT_DATA
*) Data
)->FragmentTable
,
532 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->FragmentCount
= Count
;
534 if (EndPoint
!= NULL
) {
536 &TxToken
->Session
.Udp6
.SourceAddress
,
537 &EndPoint
->LocalAddr
.v6
,
538 sizeof(EFI_IPv6_ADDRESS
)
542 &TxToken
->Session
.Udp6
.DestinationAddress
,
543 &EndPoint
->RemoteAddr
.v6
,
544 sizeof(EFI_IPv6_ADDRESS
)
547 TxToken
->Session
.Udp6
.SourcePort
= EndPoint
->LocalPort
;
548 TxToken
->Session
.Udp6
.DestinationPort
= EndPoint
->RemotePort
;
549 ((EFI_UDP6_TRANSMIT_DATA
*) Data
)->UdpSessionData
= &(TxToken
->Session
.Udp6
);
557 Creates a UDP_IO to access the UDP service. It creates and configures
560 It locates the UDP service binding prototype on the Controller parameter
561 uses the UDP service binding prototype to create a UDP child (also known as
562 a UDP instance) configures the UDP child by calling Configure function prototype.
563 Any failures in creating or configuring the UDP child return NULL for failure.
565 @param[in] Controller The controller that has the UDP service binding.
567 @param[in] ImageHandle The image handle for the driver.
568 @param[in] Configure The function to configure the created UDP child.
569 @param[in] UdpVersion The UDP protocol version, UDP4 or UDP6.
570 @param[in] Context The opaque parameter for the Configure funtion.
572 @return Newly-created UDP_IO or NULL if failed.
578 IN EFI_HANDLE Controller
,
579 IN EFI_HANDLE ImageHandle
,
580 IN UDP_IO_CONFIG Configure
,
588 ASSERT (Configure
!= NULL
);
589 ASSERT ((UdpVersion
== UDP_IO_UDP4_VERSION
) || (UdpVersion
== UDP_IO_UDP6_VERSION
));
591 UdpIo
= AllocatePool (sizeof (UDP_IO
));
597 UdpIo
->UdpVersion
= UdpVersion
;
598 UdpIo
->Signature
= UDP_IO_SIGNATURE
;
599 InitializeListHead (&UdpIo
->Link
);
602 UdpIo
->Controller
= Controller
;
603 UdpIo
->Image
= ImageHandle
;
605 InitializeListHead (&UdpIo
->SentDatagram
);
606 UdpIo
->RecvRequest
= NULL
;
607 UdpIo
->UdpHandle
= NULL
;
609 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
611 // Create a UDP child then open and configure it
613 Status
= NetLibCreateServiceChild (
616 &gEfiUdp4ServiceBindingProtocolGuid
,
620 if (EFI_ERROR (Status
)) {
624 Status
= gBS
->OpenProtocol (
626 &gEfiUdp4ProtocolGuid
,
627 (VOID
**) &UdpIo
->Protocol
.Udp4
,
630 EFI_OPEN_PROTOCOL_BY_DRIVER
633 if (EFI_ERROR (Status
)) {
637 if (EFI_ERROR (Configure (UdpIo
, Context
))) {
641 Status
= UdpIo
->Protocol
.Udp4
->GetModeData (
642 UdpIo
->Protocol
.Udp4
,
649 if (EFI_ERROR (Status
)) {
655 Status
= NetLibCreateServiceChild (
658 &gEfiUdp6ServiceBindingProtocolGuid
,
662 if (EFI_ERROR (Status
)) {
666 Status
= gBS
->OpenProtocol (
668 &gEfiUdp6ProtocolGuid
,
669 (VOID
**) &UdpIo
->Protocol
.Udp6
,
672 EFI_OPEN_PROTOCOL_BY_DRIVER
675 if (EFI_ERROR (Status
)) {
679 if (EFI_ERROR (Configure (UdpIo
, Context
))) {
683 Status
= UdpIo
->Protocol
.Udp6
->GetModeData (
684 UdpIo
->Protocol
.Udp6
,
691 if (EFI_ERROR (Status
)) {
699 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
700 gBS
->CloseProtocol (UdpIo
->UdpHandle
, &gEfiUdp4ProtocolGuid
, ImageHandle
, Controller
);
702 gBS
->CloseProtocol (UdpIo
->UdpHandle
, &gEfiUdp6ProtocolGuid
, ImageHandle
, Controller
);
706 if (UdpVersion
== UDP_IO_UDP4_VERSION
) {
707 NetLibDestroyServiceChild (
710 &gEfiUdp4ServiceBindingProtocolGuid
,
714 NetLibDestroyServiceChild (
717 &gEfiUdp6ServiceBindingProtocolGuid
,
728 Cancel all the sent datagram that pass the selection criteria of ToCancel.
729 If ToCancel is NULL, all the datagrams are cancelled.
731 @param[in] UdpIo The UDP_IO to cancel packet.
732 @param[in] IoStatus The IoStatus to return to the packet owners.
733 @param[in] ToCancel The select funtion to test whether to cancel this
735 @param[in] Context The opaque parameter to the ToCancel.
742 IN EFI_STATUS IoStatus
,
743 IN UDP_IO_TO_CANCEL ToCancel
, OPTIONAL
749 UDP_TX_TOKEN
*TxToken
;
751 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
752 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
754 NET_LIST_FOR_EACH_SAFE (Entry
, Next
, &UdpIo
->SentDatagram
) {
755 TxToken
= NET_LIST_USER_STRUCT (Entry
, UDP_TX_TOKEN
, Link
);
757 if ((ToCancel
== NULL
) || (ToCancel (TxToken
, Context
))) {
759 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
760 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &TxToken
->Token
.Udp4
);
762 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &TxToken
->Token
.Udp6
);
769 Free the UDP_IO and all its related resources.
771 The function will cancel all sent datagram and receive request.
773 @param[in] UdpIo The UDP_IO to free.
775 @retval EFI_SUCCESS The UDP_IO is freed.
784 UDP_RX_TOKEN
*RxToken
;
786 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
787 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
790 // Cancel all the sent datagram and receive requests. The
791 // callbacks of transmit requests are executed to allow the
792 // caller to release the resource. The callback of receive
793 // request are NOT executed. This is because it is most
794 // likely that the current user of the UDP IO port is closing
797 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
799 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
801 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
802 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
806 // Close then destory the Udp4 child
810 &gEfiUdp4ProtocolGuid
,
815 NetLibDestroyServiceChild (
818 &gEfiUdp4ServiceBindingProtocolGuid
,
824 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
825 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
829 // Close then destory the Udp6 child
833 &gEfiUdp6ProtocolGuid
,
838 NetLibDestroyServiceChild (
841 &gEfiUdp6ServiceBindingProtocolGuid
,
846 if (!IsListEmpty(&UdpIo
->Link
)) {
847 RemoveEntryList (&UdpIo
->Link
);
856 Clean up the UDP_IO without freeing it. The function is called when
857 user wants to re-use the UDP_IO later.
859 It will release all the transmitted datagrams and receive request. It will
860 also configure NULL for the UDP instance.
862 @param[in] UdpIo The UDP_IO to clean up.
871 UDP_RX_TOKEN
*RxToken
;
873 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
874 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
877 // Cancel all the sent datagram and receive requests.
879 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, NULL
, NULL
);
881 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
882 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
883 UdpIo
->Protocol
.Udp4
->Cancel (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
886 UdpIo
->Protocol
.Udp4
->Configure (UdpIo
->Protocol
.Udp4
, NULL
);
889 if ((RxToken
= UdpIo
->RecvRequest
) != NULL
) {
890 UdpIo
->Protocol
.Udp6
->Cancel (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
893 UdpIo
->Protocol
.Udp6
->Configure (UdpIo
->Protocol
.Udp6
, NULL
);
898 Send a packet through the UDP_IO.
900 The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called
901 when the packet is sent. The optional parameter EndPoint overrides the default
902 address pair if specified.
904 @param[in] UdpIo The UDP_IO to send the packet through.
905 @param[in] Packet The packet to send.
906 @param[in] EndPoint The local and remote access point. Override the
907 default address pair set during configuration.
908 @param[in] Gateway The gateway to use.
909 @param[in] CallBack The function being called when packet is
910 transmitted or failed.
911 @param[in] Context The opaque parameter passed to CallBack.
913 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet.
914 @retval EFI_SUCCESS The packet is successfully delivered to UDP for
923 IN UDP_END_POINT
*EndPoint OPTIONAL
,
924 IN EFI_IP_ADDRESS
*Gateway OPTIONAL
,
925 IN UDP_IO_CALLBACK CallBack
,
929 UDP_TX_TOKEN
*TxToken
;
932 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
933 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
935 TxToken
= UdpIoCreateTxToken (UdpIo
, Packet
, EndPoint
, Gateway
, CallBack
, Context
);
937 if (TxToken
== NULL
) {
938 return EFI_OUT_OF_RESOURCES
;
942 // Insert the tx token into SendDatagram list before transmitting it. Remove
943 // it from the list if the returned status is not EFI_SUCCESS.
945 InsertHeadList (&UdpIo
->SentDatagram
, &TxToken
->Link
);
947 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
948 Status
= UdpIo
->Protocol
.Udp4
->Transmit (UdpIo
->Protocol
.Udp4
, &TxToken
->Token
.Udp4
);
950 Status
= UdpIo
->Protocol
.Udp6
->Transmit (UdpIo
->Protocol
.Udp6
, &TxToken
->Token
.Udp6
);
953 if (EFI_ERROR (Status
)) {
954 RemoveEntryList (&TxToken
->Link
);
955 UdpIoFreeTxToken (TxToken
);
964 The select function to cancel a single sent datagram.
966 @param[in] Token The UDP_TX_TOKEN to test against
967 @param[in] Context The NET_BUF of the sent datagram
969 @retval TRUE The packet is to be cancelled.
970 @retval FALSE The packet is not to be cancelled.
973 UdpIoCancelSingleDgram (
974 IN UDP_TX_TOKEN
*Token
,
980 Packet
= (NET_BUF
*) Context
;
982 if (Token
->Packet
== Packet
) {
990 Cancel a single sent datagram.
992 @param[in] UdpIo The UDP_IO to cancel the packet from
993 @param[in] Packet The packet to cancel
998 UdpIoCancelSentDatagram (
1003 UdpIoCancelDgrams (UdpIo
, EFI_ABORTED
, UdpIoCancelSingleDgram
, Packet
);
1007 Issue a receive request to the UDP_IO.
1009 This function is called when upper-layer needs packet from UDP for processing.
1010 Only one receive request is acceptable at a time so a common usage model is
1011 to invoke this function inside its Callback function when the former packet
1014 @param[in] UdpIo The UDP_IO to receive the packet from.
1015 @param[in] CallBack The call back function to execute when the packet
1017 @param[in] Context The opaque context passed to Callback.
1018 @param[in] HeadLen The length of the upper-layer's protocol header.
1020 @retval EFI_ALREADY_STARTED There is already a pending receive request. Only
1021 one receive request is supported at a time.
1022 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.
1023 @retval EFI_SUCCESS The receive request is issued successfully.
1024 @retval EFI_UNSUPPORTED The UDP version in UDP_IO is not supported.
1031 IN UDP_IO_CALLBACK CallBack
,
1036 UDP_RX_TOKEN
*RxToken
;
1039 ASSERT ((UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) ||
1040 (UdpIo
->UdpVersion
== UDP_IO_UDP6_VERSION
));
1042 if (UdpIo
->RecvRequest
!= NULL
) {
1043 return EFI_ALREADY_STARTED
;
1046 RxToken
= UdpIoCreateRxToken (UdpIo
, CallBack
, Context
, HeadLen
);
1048 if (RxToken
== NULL
) {
1049 return EFI_OUT_OF_RESOURCES
;
1052 UdpIo
->RecvRequest
= RxToken
;
1053 if (UdpIo
->UdpVersion
== UDP_IO_UDP4_VERSION
) {
1054 Status
= UdpIo
->Protocol
.Udp4
->Receive (UdpIo
->Protocol
.Udp4
, &RxToken
->Token
.Udp4
);
1056 Status
= UdpIo
->Protocol
.Udp6
->Receive (UdpIo
->Protocol
.Udp6
, &RxToken
->Token
.Udp6
);
1059 if (EFI_ERROR (Status
)) {
1060 UdpIo
->RecvRequest
= NULL
;
1061 UdpIoFreeRxToken (RxToken
);