]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeUdpIoLib/DxeUdpIoLib.c
Add () after function name so that Doxygen can create reference in doc.
[mirror_edk2.git] / MdeModulePkg / Library / DxeUdpIoLib / DxeUdpIoLib.c
CommitLineData
cbf316f2 1/** @file\r
9a3293ac 2 Help functions to access UDP service, it is used by both the DHCP and MTFTP.\r
3 \r
4Copyright (c) 2005 - 2007, Intel Corporation.<BR>\r
cbf316f2 5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
cab450cc 7which accompanies this distribution. The full text of the license may be found at<BR>\r
cbf316f2 8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
cbf316f2 12**/\r
13\r
6c5dc2b0 14#include <Uefi.h>\r
cbf316f2 15\r
16#include <Protocol/Udp4.h>\r
17\r
18#include <Library/UdpIoLib.h>\r
19#include <Library/BaseLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/UefiBootServicesTableLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
ba0f75a3 23#include <Library/BaseMemoryLib.h>\r
cbf316f2 24\r
9a3293ac 25\r
26/**\r
cab450cc 27 Free a UDP_TX_TOKEN. The TX event is closed.\r
9a3293ac 28\r
29 @param Token The UDP_TX_TOKEN to release.\r
30\r
31**/\r
32VOID\r
33UdpIoFreeTxToken (\r
34 IN UDP_TX_TOKEN *Token\r
35 )\r
36{\r
37 gBS->CloseEvent (Token->UdpToken.Event);\r
38 gBS->FreePool (Token);\r
39}\r
40\r
41/**\r
cab450cc 42 Free a UDP_RX_TOKEN. The RX event is closed.\r
9a3293ac 43\r
cab450cc 44 @param Token The UDP_RX_TOKEN to release.\r
9a3293ac 45\r
46**/\r
47VOID\r
48UdpIoFreeRxToken (\r
49 IN UDP_RX_TOKEN *Token\r
50 )\r
51{\r
52 gBS->CloseEvent (Token->UdpToken.Event);\r
53 gBS->FreePool (Token);\r
54}\r
55\r
56/**\r
57 The callback function when the packet is sent by UDP.\r
cab450cc 58 \r
9a3293ac 59 It will remove the packet from the local list then call\r
cab450cc 60 the packet owner's callback function set by UdpIoSendDatagram.\r
9a3293ac 61\r
62 @param Context The UDP TX Token.\r
63\r
64**/\r
36ee91ca 65VOID\r
66EFIAPI\r
67UdpIoOnDgramSentDpc (\r
68 IN VOID *Context\r
9a3293ac 69 )\r
70{\r
71 UDP_TX_TOKEN *Token;\r
72\r
73 Token = (UDP_TX_TOKEN *) Context;\r
74 ASSERT (Token->Signature == UDP_IO_TX_SIGNATURE);\r
75\r
76 RemoveEntryList (&Token->Link);\r
77 Token->CallBack (Token->Packet, NULL, Token->UdpToken.Status, Token->Context);\r
78\r
79 UdpIoFreeTxToken (Token);\r
80}\r
81\r
82/**\r
83 Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK.\r
84 \r
85 @param Event The event signaled.\r
86 @param Context The UDP TX Token.\r
36ee91ca 87\r
9a3293ac 88**/\r
cbf316f2 89VOID\r
90EFIAPI\r
91UdpIoOnDgramSent (\r
92 IN EFI_EVENT Event,\r
93 IN VOID *Context\r
9a3293ac 94 )\r
95{\r
96 //\r
97 // Request UdpIoOnDgramSentDpc as a DPC at TPL_CALLBACK\r
98 //\r
99 NetLibQueueDpc (TPL_CALLBACK, UdpIoOnDgramSentDpc, Context);\r
100}\r
101\r
102/**\r
103 Recycle the received UDP data.\r
104\r
105 @param Context The UDP_RX_TOKEN\r
cbf316f2 106\r
9a3293ac 107**/\r
108VOID\r
109UdpIoRecycleDgram (\r
110 IN VOID *Context\r
111 )\r
112{\r
113 UDP_RX_TOKEN *Token;\r
114\r
115 Token = (UDP_RX_TOKEN *) Context;\r
116 gBS->SignalEvent (Token->UdpToken.Packet.RxData->RecycleSignal);\r
117 UdpIoFreeRxToken (Token);\r
118}\r
119\r
120/**\r
cab450cc 121 The event handle for UDP receive request.\r
122 \r
123 It will build a NET_BUF from the recieved UDP data, then deliver it\r
9a3293ac 124 to the receiver.\r
125\r
126 @param Context The UDP RX token.\r
127\r
128**/\r
129VOID\r
130EFIAPI\r
131UdpIoOnDgramRcvdDpc (\r
132 IN VOID *Context\r
133 )\r
134{\r
135 EFI_UDP4_COMPLETION_TOKEN *UdpToken;\r
136 EFI_UDP4_RECEIVE_DATA *UdpRxData;\r
137 EFI_UDP4_SESSION_DATA *UdpSession;\r
138 UDP_RX_TOKEN *Token;\r
139 UDP_POINTS Points;\r
140 NET_BUF *Netbuf;\r
141\r
142 Token = (UDP_RX_TOKEN *) Context;\r
143\r
144 ASSERT ((Token->Signature == UDP_IO_RX_SIGNATURE) &&\r
145 (Token == Token->UdpIo->RecvRequest));\r
146\r
147 //\r
148 // Clear the receive request first in case that the caller\r
149 // wants to restart the receive in the callback.\r
150 //\r
151 Token->UdpIo->RecvRequest = NULL;\r
152\r
153 UdpToken = &Token->UdpToken;\r
154 UdpRxData = UdpToken->Packet.RxData;\r
155\r
156 if (EFI_ERROR (UdpToken->Status) || (UdpRxData == NULL)) {\r
157 if (UdpToken->Status != EFI_ABORTED) {\r
158 //\r
159 // Invoke the CallBack only if the reception is not actively aborted.\r
160 //\r
161 Token->CallBack (NULL, NULL, UdpToken->Status, Token->Context);\r
162 }\r
163\r
164 UdpIoFreeRxToken (Token);\r
165 return;\r
166 }\r
167\r
168 //\r
169 // Build a NET_BUF from the UDP receive data, then deliver it up.\r
170 //\r
171 Netbuf = NetbufFromExt (\r
172 (NET_FRAGMENT *) UdpRxData->FragmentTable,\r
173 UdpRxData->FragmentCount,\r
174 0,\r
175 (UINT32) Token->HeadLen,\r
176 UdpIoRecycleDgram,\r
177 Token\r
178 );\r
179\r
180 if (Netbuf == NULL) {\r
181 gBS->SignalEvent (UdpRxData->RecycleSignal);\r
182 Token->CallBack (NULL, NULL, EFI_OUT_OF_RESOURCES, Token->Context);\r
183\r
184 UdpIoFreeRxToken (Token);\r
185 return;\r
186 }\r
187\r
188 UdpSession = &UdpRxData->UdpSession;\r
189 Points.LocalPort = UdpSession->DestinationPort;\r
190 Points.RemotePort = UdpSession->SourcePort;\r
191\r
192 CopyMem (&Points.LocalAddr, &UdpSession->DestinationAddress, sizeof (IP4_ADDR));\r
193 CopyMem (&Points.RemoteAddr, &UdpSession->SourceAddress, sizeof (IP4_ADDR));\r
194 Points.LocalAddr = NTOHL (Points.LocalAddr);\r
195 Points.RemoteAddr = NTOHL (Points.RemoteAddr);\r
196\r
197 Token->CallBack (Netbuf, &Points, EFI_SUCCESS, Token->Context);\r
198}\r
199\r
200/**\r
ca9d3a9d 201 Request UdpIoOnDgramRcvdDpc() as a DPC at TPL_CALLBACK.\r
9a3293ac 202\r
203 @param Event The UDP receive request event.\r
204 @param Context The UDP RX token.\r
205\r
206**/\r
cbf316f2 207VOID\r
208EFIAPI\r
209UdpIoOnDgramRcvd (\r
210 IN EFI_EVENT Event,\r
211 IN VOID *Context\r
9a3293ac 212 )\r
213{\r
214 //\r
215 // Request UdpIoOnDgramRcvdDpc as a DPC at TPL_CALLBACK\r
216 //\r
217 NetLibQueueDpc (TPL_CALLBACK, UdpIoOnDgramRcvdDpc, Context);\r
218}\r
cbf316f2 219\r
9a3293ac 220/**\r
221 Create a UDP_RX_TOKEN to wrap the request.\r
222\r
223 @param UdpIo The UdpIo to receive packets from\r
224 @param CallBack The function to call when receive finished.\r
225 @param Context The opaque parameter to the CallBack\r
226 @param HeadLen The head length to reserver for the packet.\r
227\r
228 @return The Wrapped request or NULL if failed to allocate resources or some errors happened.\r
229\r
230**/\r
231UDP_RX_TOKEN *\r
232UdpIoCreateRxToken (\r
233 IN UDP_IO_PORT *UdpIo,\r
234 IN UDP_IO_CALLBACK CallBack,\r
235 IN VOID *Context,\r
236 IN UINT32 HeadLen\r
237 )\r
238{\r
239 UDP_RX_TOKEN *Token;\r
240 EFI_STATUS Status;\r
241\r
242 Token = AllocatePool (sizeof (UDP_RX_TOKEN));\r
243\r
244 if (Token == NULL) {\r
245 return NULL;\r
246 }\r
247\r
248 Token->Signature = UDP_IO_RX_SIGNATURE;\r
249 Token->UdpIo = UdpIo;\r
250 Token->CallBack = CallBack;\r
251 Token->Context = Context;\r
252 Token->HeadLen = HeadLen;\r
253\r
254 Token->UdpToken.Status = EFI_NOT_READY;\r
255 Token->UdpToken.Packet.RxData = NULL;\r
256\r
257 Status = gBS->CreateEvent (\r
258 EVT_NOTIFY_SIGNAL,\r
259 TPL_NOTIFY,\r
260 UdpIoOnDgramRcvd,\r
261 Token,\r
262 &Token->UdpToken.Event\r
263 );\r
264\r
265 if (EFI_ERROR (Status)) {\r
266 gBS->FreePool (Token);\r
267 return NULL;\r
268 }\r
269\r
270 return Token;\r
271}\r
cbf316f2 272\r
273/**\r
274 Wrap a transmit request into a UDP_TX_TOKEN.\r
275\r
276 @param UdpIo The UdpIo port to send packet to\r
277 @param Packet The user's packet\r
278 @param EndPoint The local and remote access point\r
279 @param Gateway The overrided next hop\r
280 @param CallBack The function to call when transmission completed.\r
281 @param Context The opaque parameter to the call back\r
282\r
9a3293ac 283 @return The wrapped transmission request or NULL if failed to allocate resources \r
284 or for some errors.\r
cbf316f2 285\r
286**/\r
cbf316f2 287UDP_TX_TOKEN *\r
288UdpIoWrapTx (\r
289 IN UDP_IO_PORT *UdpIo,\r
290 IN NET_BUF *Packet,\r
f51f4060 291 IN UDP_POINTS *EndPoint OPTIONAL,\r
cbf316f2 292 IN IP4_ADDR Gateway,\r
293 IN UDP_IO_CALLBACK CallBack,\r
294 IN VOID *Context\r
295 )\r
296{\r
297 UDP_TX_TOKEN *Token;\r
298 EFI_UDP4_COMPLETION_TOKEN *UdpToken;\r
299 EFI_UDP4_TRANSMIT_DATA *UdpTxData;\r
300 EFI_STATUS Status;\r
301 UINT32 Count;\r
b61439a7 302 IP4_ADDR Ip;\r
cbf316f2 303\r
e48e37fc 304 Token = AllocatePool (sizeof (UDP_TX_TOKEN) +\r
cbf316f2 305 sizeof (EFI_UDP4_FRAGMENT_DATA) * (Packet->BlockOpNum - 1));\r
306\r
307 if (Token == NULL) {\r
308 return NULL;\r
309 }\r
310\r
311 Token->Signature = UDP_IO_TX_SIGNATURE;\r
e48e37fc 312 InitializeListHead (&Token->Link);\r
cbf316f2 313\r
314 Token->UdpIo = UdpIo;\r
315 Token->CallBack = CallBack;\r
316 Token->Packet = Packet;\r
317 Token->Context = Context;\r
318\r
319 UdpToken = &(Token->UdpToken);\r
320 UdpToken->Status = EFI_NOT_READY;\r
321\r
322 Status = gBS->CreateEvent (\r
323 EVT_NOTIFY_SIGNAL,\r
e48e37fc 324 TPL_NOTIFY,\r
cbf316f2 325 UdpIoOnDgramSent,\r
326 Token,\r
327 &UdpToken->Event\r
328 );\r
329\r
330 if (EFI_ERROR (Status)) {\r
e48e37fc 331 gBS->FreePool (Token);\r
cbf316f2 332 return NULL;\r
333 }\r
334\r
335 UdpTxData = &Token->UdpTxData;\r
336 UdpToken->Packet.TxData = UdpTxData;\r
337\r
338 UdpTxData->UdpSessionData = NULL;\r
339 UdpTxData->GatewayAddress = NULL;\r
340\r
341 if (EndPoint != NULL) {\r
b61439a7 342 Ip = HTONL (EndPoint->LocalAddr);\r
e48e37fc 343 CopyMem (&Token->UdpSession.SourceAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
b61439a7 344\r
345 Ip = HTONL (EndPoint->RemoteAddr);\r
e48e37fc 346 CopyMem (&Token->UdpSession.DestinationAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
b61439a7 347\r
348 Token->UdpSession.SourcePort = EndPoint->LocalPort;\r
349 Token->UdpSession.DestinationPort = EndPoint->RemotePort;\r
350 UdpTxData->UdpSessionData = &Token->UdpSession;\r
cbf316f2 351 }\r
352\r
353 if (Gateway != 0) {\r
b61439a7 354 Ip = HTONL (Gateway);\r
e48e37fc 355 CopyMem (&Token->Gateway, &Ip, sizeof (EFI_IPv4_ADDRESS));\r
b61439a7 356\r
cbf316f2 357 UdpTxData->GatewayAddress = &Token->Gateway;\r
358 }\r
359\r
360 UdpTxData->DataLength = Packet->TotalSize;\r
361 Count = Packet->BlockOpNum;\r
362 NetbufBuildExt (Packet, (NET_FRAGMENT *) UdpTxData->FragmentTable, &Count);\r
363 UdpTxData->FragmentCount = Count;\r
364\r
365 return Token;\r
366}\r
367\r
cbf316f2 368/**\r
cab450cc 369 Create a UDP_IO_PORT to access the UDP service. It will create and configure\r
370 a UDP child.\r
371 \r
372 The function will locate the UDP service binding prototype on the Controller\r
373 parameter and use it to create a UDP child (aka Udp instance). Then the UDP\r
374 child will be configured by calling Configure function prototype. Any failures\r
375 in creating or configure the UDP child will lead to the failure of UDP_IO_PORT\r
376 creation.\r
cbf316f2 377\r
378 @param Controller The controller that has the UDP service binding\r
379 protocol installed.\r
380 @param Image The image handle for the driver.\r
381 @param Configure The function to configure the created UDP child\r
382 @param Context The opaque parameter for the Configure funtion.\r
383\r
cab450cc 384 @return Newly-created UDP_IO_PORT or NULL if failed.\r
cbf316f2 385\r
386**/\r
387UDP_IO_PORT *\r
7b414b4e 388EFIAPI\r
cbf316f2 389UdpIoCreatePort (\r
390 IN EFI_HANDLE Controller,\r
391 IN EFI_HANDLE Image,\r
392 IN UDP_IO_CONFIG Configure,\r
393 IN VOID *Context\r
394 )\r
395{\r
396 UDP_IO_PORT *UdpIo;\r
397 EFI_STATUS Status;\r
398\r
399 ASSERT (Configure != NULL);\r
400\r
e48e37fc 401 UdpIo = AllocatePool (sizeof (UDP_IO_PORT));\r
cbf316f2 402\r
403 if (UdpIo == NULL) {\r
404 return NULL;\r
405 }\r
406\r
407 UdpIo->Signature = UDP_IO_SIGNATURE;\r
e48e37fc 408 InitializeListHead (&UdpIo->Link);\r
cbf316f2 409 UdpIo->RefCnt = 1;\r
410\r
411 UdpIo->Controller = Controller;\r
412 UdpIo->Image = Image;\r
413\r
e48e37fc 414 InitializeListHead (&UdpIo->SentDatagram);\r
cbf316f2 415 UdpIo->RecvRequest = NULL;\r
416 UdpIo->UdpHandle = NULL;\r
417\r
418 //\r
419 // Create a UDP child then open and configure it\r
420 //\r
421 Status = NetLibCreateServiceChild (\r
422 Controller,\r
423 Image,\r
424 &gEfiUdp4ServiceBindingProtocolGuid,\r
425 &UdpIo->UdpHandle\r
426 );\r
427\r
428 if (EFI_ERROR (Status)) {\r
429 goto FREE_MEM;\r
430 }\r
431\r
432 Status = gBS->OpenProtocol (\r
433 UdpIo->UdpHandle,\r
434 &gEfiUdp4ProtocolGuid,\r
4eb65aff 435 (VOID **) &UdpIo->Udp,\r
cbf316f2 436 Image,\r
437 Controller,\r
438 EFI_OPEN_PROTOCOL_BY_DRIVER\r
439 );\r
440\r
441 if (EFI_ERROR (Status)) {\r
442 goto FREE_CHILD;\r
443 }\r
444\r
445 if (EFI_ERROR (Configure (UdpIo, Context))) {\r
446 goto CLOSE_PROTOCOL;\r
447 }\r
448\r
449 Status = UdpIo->Udp->GetModeData (UdpIo->Udp, NULL, NULL, NULL, &UdpIo->SnpMode);\r
450\r
451 if (EFI_ERROR (Status)) {\r
452 goto CLOSE_PROTOCOL;\r
453 }\r
454\r
455 return UdpIo;\r
456\r
457CLOSE_PROTOCOL:\r
458 gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp4ProtocolGuid, Image, Controller);\r
459\r
460FREE_CHILD:\r
461 NetLibDestroyServiceChild (\r
462 Controller,\r
463 Image,\r
464 &gEfiUdp4ServiceBindingProtocolGuid,\r
465 UdpIo->UdpHandle\r
466 );\r
467\r
468FREE_MEM:\r
e48e37fc 469 gBS->FreePool (UdpIo);\r
cbf316f2 470 return NULL;\r
471}\r
472\r
cbf316f2 473/**\r
cab450cc 474 Cancel all the sent datagram that pass the selection criteria of ToCancel.\r
cbf316f2 475 If ToCancel is NULL, all the datagrams are cancelled.\r
476\r
cab450cc 477 @param UdpIo The UDP_IO_PORT to cancel packet\r
cbf316f2 478 @param IoStatus The IoStatus to return to the packet owners.\r
479 @param ToCancel The select funtion to test whether to cancel this\r
480 packet or not.\r
481 @param Context The opaque parameter to the ToCancel.\r
482\r
cbf316f2 483**/\r
cbf316f2 484VOID\r
485UdpIoCancelDgrams (\r
486 IN UDP_IO_PORT *UdpIo,\r
487 IN EFI_STATUS IoStatus,\r
488 IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL\r
489 IN VOID *Context\r
490 )\r
491{\r
e48e37fc 492 LIST_ENTRY *Entry;\r
493 LIST_ENTRY *Next;\r
cbf316f2 494 UDP_TX_TOKEN *Token;\r
495\r
496 NET_LIST_FOR_EACH_SAFE (Entry, Next, &UdpIo->SentDatagram) {\r
497 Token = NET_LIST_USER_STRUCT (Entry, UDP_TX_TOKEN, Link);\r
498\r
499 if ((ToCancel == NULL) || (ToCancel (Token, Context))) {\r
cbf316f2 500 UdpIo->Udp->Cancel (UdpIo->Udp, &Token->UdpToken);\r
cbf316f2 501 }\r
502 }\r
503}\r
504\r
cbf316f2 505/**\r
cab450cc 506 Free the UDP_IO_PORT and all its related resources.\r
507 \r
508 The function will cancel all sent datagram and receive request.\r
cbf316f2 509\r
cab450cc 510 @param UdpIo The UDP_IO_PORT to free.\r
cbf316f2 511\r
cab450cc 512 @retval EFI_SUCCESS The UDP_IO_PORT is freed.\r
cbf316f2 513\r
514**/\r
515EFI_STATUS\r
7b414b4e 516EFIAPI\r
cbf316f2 517UdpIoFreePort (\r
518 IN UDP_IO_PORT *UdpIo\r
519 )\r
520{\r
521 UDP_RX_TOKEN *RxToken;\r
522\r
523 //\r
524 // Cancel all the sent datagram and receive requests. The\r
525 // callbacks of transmit requests are executed to allow the\r
526 // caller to release the resource. The callback of receive\r
527 // request are NOT executed. This is because it is most\r
528 // likely that the current user of the UDP IO port is closing\r
529 // itself.\r
530 //\r
531 UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r
532\r
533 if ((RxToken = UdpIo->RecvRequest) != NULL) {\r
cbf316f2 534 UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r
cbf316f2 535 }\r
536\r
537 //\r
538 // Close then destory the UDP child\r
539 //\r
540 gBS->CloseProtocol (\r
541 UdpIo->UdpHandle,\r
542 &gEfiUdp4ProtocolGuid,\r
543 UdpIo->Image,\r
544 UdpIo->Controller\r
545 );\r
546\r
547 NetLibDestroyServiceChild (\r
548 UdpIo->Controller,\r
549 UdpIo->Image,\r
550 &gEfiUdp4ServiceBindingProtocolGuid,\r
551 UdpIo->UdpHandle\r
552 );\r
553\r
687a2e5f 554 if (!IsListEmpty(&UdpIo->Link)) {\r
e48e37fc 555 RemoveEntryList (&UdpIo->Link);\r
687a2e5f 556 }\r
557\r
e48e37fc 558 gBS->FreePool (UdpIo);\r
cbf316f2 559 return EFI_SUCCESS;\r
560}\r
561\r
562\r
563/**\r
cab450cc 564 Clean up the UDP_IO_PORT without freeing it. The function is called when\r
565 user wants to re-use the UDP_IO_PORT later.\r
566 \r
567 It will release all the transmitted datagrams and receive request. It will\r
568 also configure NULL for the UDP instance.\r
cbf316f2 569\r
cab450cc 570 @param UdpIo The UDP_IO_PORT to clean up.\r
cbf316f2 571\r
cbf316f2 572**/\r
573VOID\r
7b414b4e 574EFIAPI\r
cbf316f2 575UdpIoCleanPort (\r
576 IN UDP_IO_PORT *UdpIo\r
577 )\r
578{\r
579 UDP_RX_TOKEN *RxToken;\r
580\r
581 //\r
582 // Cancel all the sent datagram and receive requests.\r
583 //\r
584 UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r
585\r
586 if ((RxToken = UdpIo->RecvRequest) != NULL) {\r
cbf316f2 587 UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r
cbf316f2 588 }\r
589\r
590 UdpIo->Udp->Configure (UdpIo->Udp, NULL);\r
591}\r
592\r
cbf316f2 593/**\r
cab450cc 594 Send a packet through the UDP_IO_PORT.\r
595 \r
596 The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called\r
597 when the packet is sent. The optional parameter EndPoint overrides the default\r
598 address pair if specified.\r
cbf316f2 599\r
cab450cc 600 @param UdpIo The UDP_IO_PORT to send the packet through\r
cbf316f2 601 @param Packet The packet to send\r
cab450cc 602 @param EndPoint The local and remote access point. Override the\r
603 default address pair set during configuration.\r
cbf316f2 604 @param Gateway The gateway to use\r
cab450cc 605 @param CallBack The function being called when packet is\r
cbf316f2 606 transmitted or failed.\r
cab450cc 607 @param Context The opaque parameter passed to CallBack\r
cbf316f2 608\r
609 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet\r
610 @retval EFI_SUCCESS The packet is successfully delivered to UDP for\r
611 transmission.\r
612\r
613**/\r
614EFI_STATUS\r
7b414b4e 615EFIAPI\r
cbf316f2 616UdpIoSendDatagram (\r
617 IN UDP_IO_PORT *UdpIo,\r
618 IN NET_BUF *Packet,\r
619 IN UDP_POINTS *EndPoint, OPTIONAL\r
620 IN IP4_ADDR Gateway,\r
621 IN UDP_IO_CALLBACK CallBack,\r
622 IN VOID *Context\r
623 )\r
624{\r
625 UDP_TX_TOKEN *Token;\r
626 EFI_STATUS Status;\r
627\r
628 Token = UdpIoWrapTx (UdpIo, Packet, EndPoint, Gateway, CallBack, Context);\r
629\r
630 if (Token == NULL) {\r
631 return EFI_OUT_OF_RESOURCES;\r
632 }\r
633\r
36ee91ca 634 //\r
635 // Insert the tx token into SendDatagram list before transmitting it. Remove\r
636 // it from the list if the returned status is not EFI_SUCCESS.\r
637 //\r
e48e37fc 638 InsertHeadList (&UdpIo->SentDatagram, &Token->Link);\r
cbf316f2 639 Status = UdpIo->Udp->Transmit (UdpIo->Udp, &Token->UdpToken);\r
cbf316f2 640 if (EFI_ERROR (Status)) {\r
e48e37fc 641 RemoveEntryList (&Token->Link);\r
cbf316f2 642 UdpIoFreeTxToken (Token);\r
643 return Status;\r
644 }\r
645\r
cbf316f2 646 return EFI_SUCCESS;\r
647}\r
648\r
649\r
650/**\r
cab450cc 651 The select function to cancel a single sent datagram.\r
cbf316f2 652\r
cab450cc 653 @param Token The UDP_TX_TOKEN to test against\r
654 @param Context The NET_BUF of the sent datagram\r
cbf316f2 655\r
9a3293ac 656 @retval TRUE The packet is to be cancelled.\r
657 @retval FALSE The packet is not to be cancelled.\r
cbf316f2 658**/\r
cbf316f2 659BOOLEAN\r
660UdpIoCancelSingleDgram (\r
661 IN UDP_TX_TOKEN *Token,\r
662 IN VOID *Context\r
663 )\r
664{\r
665 NET_BUF *Packet;\r
666\r
667 Packet = (NET_BUF *) Context;\r
668\r
669 if (Token->Packet == Packet) {\r
670 return TRUE;\r
671 }\r
672\r
673 return FALSE;\r
674}\r
675\r
cbf316f2 676/**\r
677 Cancel a single sent datagram.\r
678\r
cab450cc 679 @param UdpIo The UDP_IO_PORT to cancel the packet from\r
cbf316f2 680 @param Packet The packet to cancel\r
681\r
cbf316f2 682**/\r
683VOID\r
7b414b4e 684EFIAPI\r
cbf316f2 685UdpIoCancelSentDatagram (\r
686 IN UDP_IO_PORT *UdpIo,\r
687 IN NET_BUF *Packet\r
688 )\r
689{\r
690 UdpIoCancelDgrams (UdpIo, EFI_ABORTED, UdpIoCancelSingleDgram, Packet);\r
691}\r
692\r
cbf316f2 693/**\r
cab450cc 694 Issue a receive request to the UDP_IO_PORT.\r
695 \r
696 This function is called when upper-layer needs packet from UDP for processing.\r
697 Only one receive request is acceptable at a time so a common usage model is\r
698 to invoke this function inside its Callback function when the former packet\r
699 is processed.\r
cbf316f2 700\r
cab450cc 701 @param UdpIo The UDP_IO_PORT to receive the packet from.\r
702 @param CallBack The call back function to execute when the packet\r
703 is received.\r
704 @param Context The opaque context passed to Callback\r
705 @param HeadLen The length of the upper-layer's protocol header\r
cbf316f2 706\r
707 @retval EFI_ALREADY_STARTED There is already a pending receive request. Only\r
cab450cc 708 one receive request is supported at a time.\r
709 @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.\r
cbf316f2 710 @retval EFI_SUCCESS The receive request is issued successfully.\r
711\r
712**/\r
713EFI_STATUS\r
7b414b4e 714EFIAPI\r
cbf316f2 715UdpIoRecvDatagram (\r
716 IN UDP_IO_PORT *UdpIo,\r
717 IN UDP_IO_CALLBACK CallBack,\r
718 IN VOID *Context,\r
719 IN UINT32 HeadLen\r
720 )\r
721{\r
722 UDP_RX_TOKEN *Token;\r
723 EFI_STATUS Status;\r
724\r
725 if (UdpIo->RecvRequest != NULL) {\r
726 return EFI_ALREADY_STARTED;\r
727 }\r
728\r
729 Token = UdpIoCreateRxToken (UdpIo, CallBack, Context, HeadLen);\r
730\r
731 if (Token == NULL) {\r
732 return EFI_OUT_OF_RESOURCES;\r
733 }\r
734\r
36ee91ca 735 UdpIo->RecvRequest = Token;\r
cbf316f2 736 Status = UdpIo->Udp->Receive (UdpIo->Udp, &Token->UdpToken);\r
737\r
738 if (EFI_ERROR (Status)) {\r
36ee91ca 739 UdpIo->RecvRequest = NULL;\r
cbf316f2 740 UdpIoFreeRxToken (Token);\r
cbf316f2 741 }\r
742\r
36ee91ca 743 return Status;\r
cbf316f2 744}\r