]>
Commit | Line | Data |
---|---|---|
cbf316f2 | 1 | /** @file\r |
9a3293ac | 2 | Help functions to access UDP service, it is used by both the DHCP and MTFTP.\r |
3 | \r | |
4 | Copyright (c) 2005 - 2007, Intel Corporation.<BR>\r | |
cbf316f2 | 5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
cab450cc | 7 | which accompanies this distribution. The full text of the license may be found at<BR>\r |
cbf316f2 | 8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT 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 |
8f5e6151 | 29 | @param[in] Token The UDP_TX_TOKEN to release.\r |
9a3293ac | 30 | \r |
31 | **/\r | |
32 | VOID\r | |
33 | UdpIoFreeTxToken (\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 |
8f5e6151 | 44 | @param[in] Token The UDP_RX_TOKEN to release.\r |
9a3293ac | 45 | \r |
46 | **/\r | |
47 | VOID\r | |
48 | UdpIoFreeRxToken (\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 |
8f5e6151 | 62 | @param[in] Context The UDP TX Token.\r |
9a3293ac | 63 | \r |
64 | **/\r | |
36ee91ca | 65 | VOID\r |
66 | EFIAPI\r | |
67 | UdpIoOnDgramSentDpc (\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 | |
8f5e6151 | 85 | @param[in] Event The event signaled.\r |
86 | @param[in] Context The UDP TX Token.\r | |
36ee91ca | 87 | \r |
9a3293ac | 88 | **/\r |
cbf316f2 | 89 | VOID\r |
90 | EFIAPI\r | |
91 | UdpIoOnDgramSent (\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 | |
8f5e6151 | 105 | @param[in] Context The UDP_RX_TOKEN.\r |
cbf316f2 | 106 | \r |
9a3293ac | 107 | **/\r |
108 | VOID\r | |
109 | UdpIoRecycleDgram (\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 | |
8f5e6151 | 126 | @param[in] Context The UDP RX token.\r |
9a3293ac | 127 | \r |
128 | **/\r | |
129 | VOID\r | |
130 | EFIAPI\r | |
131 | UdpIoOnDgramRcvdDpc (\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 |
8f5e6151 | 203 | @param[in] Event The UDP receive request event.\r |
204 | @param[in] Context The UDP RX token.\r | |
9a3293ac | 205 | \r |
206 | **/\r | |
cbf316f2 | 207 | VOID\r |
208 | EFIAPI\r | |
209 | UdpIoOnDgramRcvd (\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 | |
8f5e6151 | 223 | @param[in] UdpIo The UdpIo to receive packets from.\r |
224 | @param[in] CallBack The function to call when receive finished.\r | |
225 | @param[in] Context The opaque parameter to the CallBack.\r | |
226 | @param[in] HeadLen The head length to reserver for the packet.\r | |
9a3293ac | 227 | \r |
228 | @return The Wrapped request or NULL if failed to allocate resources or some errors happened.\r | |
229 | \r | |
230 | **/\r | |
231 | UDP_RX_TOKEN *\r | |
232 | UdpIoCreateRxToken (\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 | |
8f5e6151 | 276 | @param[in] UdpIo The UdpIo port to send packet to.\r |
277 | @param[in] Packet The user's packet.\r | |
278 | @param[in] EndPoint The local and remote access point.\r | |
279 | @param[in] Gateway The overrided next hop.\r | |
280 | @param[in] CallBack The function to call when transmission completed.\r | |
281 | @param[in] Context The opaque parameter to the call back.\r | |
cbf316f2 | 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 | 287 | UDP_TX_TOKEN *\r |
288 | UdpIoWrapTx (\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 |
8f5e6151 | 378 | @param[in] Controller The controller that has the UDP service binding.\r |
379 | protocol installed.\r | |
380 | @param[in] Image The image handle for the driver.\r | |
381 | @param[in] Configure The function to configure the created UDP child.\r | |
382 | @param[in] Context The opaque parameter for the Configure funtion.\r | |
cbf316f2 | 383 | \r |
cab450cc | 384 | @return Newly-created UDP_IO_PORT or NULL if failed.\r |
cbf316f2 | 385 | \r |
386 | **/\r | |
387 | UDP_IO_PORT *\r | |
7b414b4e | 388 | EFIAPI\r |
cbf316f2 | 389 | UdpIoCreatePort (\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 | |
457 | CLOSE_PROTOCOL:\r | |
458 | gBS->CloseProtocol (UdpIo->UdpHandle, &gEfiUdp4ProtocolGuid, Image, Controller);\r | |
459 | \r | |
460 | FREE_CHILD:\r | |
461 | NetLibDestroyServiceChild (\r | |
462 | Controller,\r | |
463 | Image,\r | |
464 | &gEfiUdp4ServiceBindingProtocolGuid,\r | |
465 | UdpIo->UdpHandle\r | |
466 | );\r | |
467 | \r | |
468 | FREE_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 | |
8f5e6151 | 477 | @param[in] UdpIo The UDP_IO_PORT to cancel packet.\r |
478 | @param[in] IoStatus The IoStatus to return to the packet owners.\r | |
479 | @param[in] ToCancel The select funtion to test whether to cancel this\r | |
480 | packet or not.\r | |
481 | @param[in] Context The opaque parameter to the ToCancel.\r | |
cbf316f2 | 482 | \r |
cbf316f2 | 483 | **/\r |
cbf316f2 | 484 | VOID\r |
e4b99ad9 | 485 | EFIAPI\r |
cbf316f2 | 486 | UdpIoCancelDgrams (\r |
487 | IN UDP_IO_PORT *UdpIo,\r | |
488 | IN EFI_STATUS IoStatus,\r | |
489 | IN UDP_IO_TO_CANCEL ToCancel, OPTIONAL\r | |
490 | IN VOID *Context\r | |
491 | )\r | |
492 | {\r | |
e48e37fc | 493 | LIST_ENTRY *Entry;\r |
494 | LIST_ENTRY *Next;\r | |
cbf316f2 | 495 | UDP_TX_TOKEN *Token;\r |
496 | \r | |
497 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &UdpIo->SentDatagram) {\r | |
498 | Token = NET_LIST_USER_STRUCT (Entry, UDP_TX_TOKEN, Link);\r | |
499 | \r | |
500 | if ((ToCancel == NULL) || (ToCancel (Token, Context))) {\r | |
cbf316f2 | 501 | UdpIo->Udp->Cancel (UdpIo->Udp, &Token->UdpToken);\r |
cbf316f2 | 502 | }\r |
503 | }\r | |
504 | }\r | |
505 | \r | |
cbf316f2 | 506 | /**\r |
cab450cc | 507 | Free the UDP_IO_PORT and all its related resources.\r |
508 | \r | |
509 | The function will cancel all sent datagram and receive request.\r | |
cbf316f2 | 510 | \r |
8f5e6151 | 511 | @param[in] UdpIo The UDP_IO_PORT to free.\r |
cbf316f2 | 512 | \r |
cab450cc | 513 | @retval EFI_SUCCESS The UDP_IO_PORT is freed.\r |
cbf316f2 | 514 | \r |
515 | **/\r | |
516 | EFI_STATUS\r | |
7b414b4e | 517 | EFIAPI\r |
cbf316f2 | 518 | UdpIoFreePort (\r |
519 | IN UDP_IO_PORT *UdpIo\r | |
520 | )\r | |
521 | {\r | |
522 | UDP_RX_TOKEN *RxToken;\r | |
523 | \r | |
524 | //\r | |
525 | // Cancel all the sent datagram and receive requests. The\r | |
526 | // callbacks of transmit requests are executed to allow the\r | |
527 | // caller to release the resource. The callback of receive\r | |
528 | // request are NOT executed. This is because it is most\r | |
529 | // likely that the current user of the UDP IO port is closing\r | |
530 | // itself.\r | |
531 | //\r | |
532 | UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r | |
533 | \r | |
534 | if ((RxToken = UdpIo->RecvRequest) != NULL) {\r | |
cbf316f2 | 535 | UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r |
cbf316f2 | 536 | }\r |
537 | \r | |
538 | //\r | |
539 | // Close then destory the UDP child\r | |
540 | //\r | |
541 | gBS->CloseProtocol (\r | |
542 | UdpIo->UdpHandle,\r | |
543 | &gEfiUdp4ProtocolGuid,\r | |
544 | UdpIo->Image,\r | |
545 | UdpIo->Controller\r | |
546 | );\r | |
547 | \r | |
548 | NetLibDestroyServiceChild (\r | |
549 | UdpIo->Controller,\r | |
550 | UdpIo->Image,\r | |
551 | &gEfiUdp4ServiceBindingProtocolGuid,\r | |
552 | UdpIo->UdpHandle\r | |
553 | );\r | |
554 | \r | |
687a2e5f | 555 | if (!IsListEmpty(&UdpIo->Link)) {\r |
e48e37fc | 556 | RemoveEntryList (&UdpIo->Link);\r |
687a2e5f | 557 | }\r |
558 | \r | |
e48e37fc | 559 | gBS->FreePool (UdpIo);\r |
cbf316f2 | 560 | return EFI_SUCCESS;\r |
561 | }\r | |
562 | \r | |
563 | \r | |
564 | /**\r | |
cab450cc | 565 | Clean up the UDP_IO_PORT without freeing it. The function is called when\r |
566 | user wants to re-use the UDP_IO_PORT later.\r | |
567 | \r | |
568 | It will release all the transmitted datagrams and receive request. It will\r | |
569 | also configure NULL for the UDP instance.\r | |
cbf316f2 | 570 | \r |
8f5e6151 | 571 | @param[in] UdpIo The UDP_IO_PORT to clean up.\r |
cbf316f2 | 572 | \r |
cbf316f2 | 573 | **/\r |
574 | VOID\r | |
7b414b4e | 575 | EFIAPI\r |
cbf316f2 | 576 | UdpIoCleanPort (\r |
577 | IN UDP_IO_PORT *UdpIo\r | |
578 | )\r | |
579 | {\r | |
580 | UDP_RX_TOKEN *RxToken;\r | |
581 | \r | |
582 | //\r | |
583 | // Cancel all the sent datagram and receive requests.\r | |
584 | //\r | |
585 | UdpIoCancelDgrams (UdpIo, EFI_ABORTED, NULL, NULL);\r | |
586 | \r | |
587 | if ((RxToken = UdpIo->RecvRequest) != NULL) {\r | |
cbf316f2 | 588 | UdpIo->Udp->Cancel (UdpIo->Udp, &RxToken->UdpToken);\r |
cbf316f2 | 589 | }\r |
590 | \r | |
591 | UdpIo->Udp->Configure (UdpIo->Udp, NULL);\r | |
592 | }\r | |
593 | \r | |
cbf316f2 | 594 | /**\r |
cab450cc | 595 | Send a packet through the UDP_IO_PORT.\r |
596 | \r | |
597 | The packet will be wrapped in UDP_TX_TOKEN. Function Callback will be called\r | |
598 | when the packet is sent. The optional parameter EndPoint overrides the default\r | |
599 | address pair if specified.\r | |
cbf316f2 | 600 | \r |
8f5e6151 | 601 | @param[in] UdpIo The UDP_IO_PORT to send the packet through.\r |
602 | @param[in] Packet The packet to send.\r | |
603 | @param[in] EndPoint The local and remote access point. Override the\r | |
604 | default address pair set during configuration.\r | |
605 | @param[in] Gateway The gateway to use.\r | |
606 | @param[in] CallBack The function being called when packet is\r | |
607 | transmitted or failed.\r | |
608 | @param[in] Context The opaque parameter passed to CallBack.\r | |
cbf316f2 | 609 | \r |
8f5e6151 | 610 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the packet.\r |
cbf316f2 | 611 | @retval EFI_SUCCESS The packet is successfully delivered to UDP for\r |
612 | transmission.\r | |
613 | \r | |
614 | **/\r | |
615 | EFI_STATUS\r | |
7b414b4e | 616 | EFIAPI\r |
cbf316f2 | 617 | UdpIoSendDatagram (\r |
618 | IN UDP_IO_PORT *UdpIo,\r | |
619 | IN NET_BUF *Packet,\r | |
620 | IN UDP_POINTS *EndPoint, OPTIONAL\r | |
621 | IN IP4_ADDR Gateway,\r | |
622 | IN UDP_IO_CALLBACK CallBack,\r | |
623 | IN VOID *Context\r | |
624 | )\r | |
625 | {\r | |
626 | UDP_TX_TOKEN *Token;\r | |
627 | EFI_STATUS Status;\r | |
628 | \r | |
629 | Token = UdpIoWrapTx (UdpIo, Packet, EndPoint, Gateway, CallBack, Context);\r | |
630 | \r | |
631 | if (Token == NULL) {\r | |
632 | return EFI_OUT_OF_RESOURCES;\r | |
633 | }\r | |
634 | \r | |
36ee91ca | 635 | //\r |
636 | // Insert the tx token into SendDatagram list before transmitting it. Remove\r | |
637 | // it from the list if the returned status is not EFI_SUCCESS.\r | |
638 | //\r | |
e48e37fc | 639 | InsertHeadList (&UdpIo->SentDatagram, &Token->Link);\r |
cbf316f2 | 640 | Status = UdpIo->Udp->Transmit (UdpIo->Udp, &Token->UdpToken);\r |
cbf316f2 | 641 | if (EFI_ERROR (Status)) {\r |
e48e37fc | 642 | RemoveEntryList (&Token->Link);\r |
cbf316f2 | 643 | UdpIoFreeTxToken (Token);\r |
644 | return Status;\r | |
645 | }\r | |
646 | \r | |
cbf316f2 | 647 | return EFI_SUCCESS;\r |
648 | }\r | |
649 | \r | |
650 | \r | |
651 | /**\r | |
cab450cc | 652 | The select function to cancel a single sent datagram.\r |
cbf316f2 | 653 | \r |
8f5e6151 | 654 | @param[in] Token The UDP_TX_TOKEN to test against\r |
655 | @param[in] Context The NET_BUF of the sent datagram\r | |
cbf316f2 | 656 | \r |
9a3293ac | 657 | @retval TRUE The packet is to be cancelled.\r |
658 | @retval FALSE The packet is not to be cancelled.\r | |
cbf316f2 | 659 | **/\r |
cbf316f2 | 660 | BOOLEAN\r |
661 | UdpIoCancelSingleDgram (\r | |
662 | IN UDP_TX_TOKEN *Token,\r | |
663 | IN VOID *Context\r | |
664 | )\r | |
665 | {\r | |
666 | NET_BUF *Packet;\r | |
667 | \r | |
668 | Packet = (NET_BUF *) Context;\r | |
669 | \r | |
670 | if (Token->Packet == Packet) {\r | |
671 | return TRUE;\r | |
672 | }\r | |
673 | \r | |
674 | return FALSE;\r | |
675 | }\r | |
676 | \r | |
cbf316f2 | 677 | /**\r |
678 | Cancel a single sent datagram.\r | |
679 | \r | |
8f5e6151 | 680 | @param[in] UdpIo The UDP_IO_PORT to cancel the packet from\r |
681 | @param[in] Packet The packet to cancel\r | |
cbf316f2 | 682 | \r |
cbf316f2 | 683 | **/\r |
684 | VOID\r | |
7b414b4e | 685 | EFIAPI\r |
cbf316f2 | 686 | UdpIoCancelSentDatagram (\r |
687 | IN UDP_IO_PORT *UdpIo,\r | |
688 | IN NET_BUF *Packet\r | |
689 | )\r | |
690 | {\r | |
691 | UdpIoCancelDgrams (UdpIo, EFI_ABORTED, UdpIoCancelSingleDgram, Packet);\r | |
692 | }\r | |
693 | \r | |
cbf316f2 | 694 | /**\r |
cab450cc | 695 | Issue a receive request to the UDP_IO_PORT.\r |
696 | \r | |
697 | This function is called when upper-layer needs packet from UDP for processing.\r | |
698 | Only one receive request is acceptable at a time so a common usage model is\r | |
699 | to invoke this function inside its Callback function when the former packet\r | |
700 | is processed.\r | |
cbf316f2 | 701 | \r |
8f5e6151 | 702 | @param[in] UdpIo The UDP_IO_PORT to receive the packet from.\r |
703 | @param[in] CallBack The call back function to execute when the packet\r | |
704 | is received.\r | |
705 | @param[in] Context The opaque context passed to Callback.\r | |
706 | @param[in] HeadLen The length of the upper-layer's protocol header.\r | |
cbf316f2 | 707 | \r |
708 | @retval EFI_ALREADY_STARTED There is already a pending receive request. Only\r | |
cab450cc | 709 | one receive request is supported at a time.\r |
710 | @retval EFI_OUT_OF_RESOURCES Failed to allocate needed resources.\r | |
cbf316f2 | 711 | @retval EFI_SUCCESS The receive request is issued successfully.\r |
712 | \r | |
713 | **/\r | |
714 | EFI_STATUS\r | |
7b414b4e | 715 | EFIAPI\r |
cbf316f2 | 716 | UdpIoRecvDatagram (\r |
717 | IN UDP_IO_PORT *UdpIo,\r | |
718 | IN UDP_IO_CALLBACK CallBack,\r | |
719 | IN VOID *Context,\r | |
720 | IN UINT32 HeadLen\r | |
721 | )\r | |
722 | {\r | |
723 | UDP_RX_TOKEN *Token;\r | |
724 | EFI_STATUS Status;\r | |
725 | \r | |
726 | if (UdpIo->RecvRequest != NULL) {\r | |
727 | return EFI_ALREADY_STARTED;\r | |
728 | }\r | |
729 | \r | |
730 | Token = UdpIoCreateRxToken (UdpIo, CallBack, Context, HeadLen);\r | |
731 | \r | |
732 | if (Token == NULL) {\r | |
733 | return EFI_OUT_OF_RESOURCES;\r | |
734 | }\r | |
735 | \r | |
36ee91ca | 736 | UdpIo->RecvRequest = Token;\r |
cbf316f2 | 737 | Status = UdpIo->Udp->Receive (UdpIo->Udp, &Token->UdpToken);\r |
738 | \r | |
739 | if (EFI_ERROR (Status)) {\r | |
36ee91ca | 740 | UdpIo->RecvRequest = NULL;\r |
cbf316f2 | 741 | UdpIoFreeRxToken (Token);\r |
cbf316f2 | 742 | }\r |
743 | \r | |
36ee91ca | 744 | return Status;\r |
cbf316f2 | 745 | }\r |