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