]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c
13bf552339a1be9f2828a763bbb5766a522df757
[mirror_edk2.git] / MdeModulePkg / Universal / Network / MnpDxe / MnpIo.c
1 /** @file
2
3 Copyright (c) 2005 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 MnpIo.c
15
16 Abstract:
17
18 Implementation of Managed Network Protocol I/O functions.
19
20
21 **/
22
23 #include "MnpImpl.h"
24 #include <Library/NetLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/MemoryAllocationLib.h>
28
29
30 /**
31 Validates the Mnp transmit token.
32
33 @param Instance Pointer to the Mnp instance context data.
34 @param Token Pointer to the transmit token to check.
35
36 @return The Token is valid or not.
37
38 **/
39 BOOLEAN
40 MnpIsValidTxToken (
41 IN MNP_INSTANCE_DATA *Instance,
42 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
43 )
44 {
45 MNP_SERVICE_DATA *MnpServiceData;
46 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
47 UINT32 Index;
48 UINT32 TotalLength;
49 EFI_MANAGED_NETWORK_FRAGMENT_DATA *FragmentTable;
50
51 MnpServiceData = Instance->MnpServiceData;
52 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
53
54 TxData = Token->Packet.TxData;
55
56 if ((Token->Event == NULL) || (TxData == NULL) || (TxData->FragmentCount == 0)) {
57 //
58 // The token is invalid if the Event is NULL, or the TxData is NULL, or
59 // the fragment count is zero.
60 //
61 DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid Token.\n"));
62 return FALSE;
63 }
64
65 if ((TxData->DestinationAddress != NULL) && (TxData->HeaderLength != 0)) {
66 //
67 // The token is invalid if the HeaderLength isn't zero while the DestinationAddress
68 // is NULL (The destination address is already put into the packet).
69 //
70 DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n"));
71 return FALSE;
72 }
73
74 TotalLength = 0;
75 FragmentTable = TxData->FragmentTable;
76 for (Index = 0; Index < TxData->FragmentCount; Index++) {
77
78 if ((FragmentTable[Index].FragmentLength == 0) || (FragmentTable[Index].FragmentBuffer == NULL)) {
79 //
80 // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL.
81 //
82 DEBUG ((EFI_D_WARN, "MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n"));
83 return FALSE;
84 }
85
86 TotalLength += FragmentTable[Index].FragmentLength;
87 }
88
89 if ((TxData->DestinationAddress == NULL) && (FragmentTable[0].FragmentLength < TxData->HeaderLength)) {
90 //
91 // Media header is split between fragments.
92 //
93 return FALSE;
94 }
95
96 if (TotalLength != (TxData->DataLength + TxData->HeaderLength)) {
97 //
98 // The length calculated from the fragment information doesn't equal to the
99 // sum of the DataLength and the HeaderLength.
100 //
101 DEBUG ((EFI_D_WARN, "MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n"));
102 return FALSE;
103 }
104
105 if (TxData->DataLength > MnpServiceData->Mtu) {
106 //
107 // The total length is larger than the MTU.
108 //
109 DEBUG ((EFI_D_WARN, "MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n"));
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116
117 /**
118 Build the packet to transmit from the TxData passed in.
119
120 @param MnpServiceData Pointer to the mnp service context data.
121 @param TxData Pointer to the transmit data containing the
122 information to build the packet.
123 @param PktBuf Pointer to record the address of the packet.
124 @param PktLen Pointer to a UINT32 variable used to record the
125 packet's length.
126
127 @return None.
128
129 **/
130 VOID
131 MnpBuildTxPacket (
132 IN MNP_SERVICE_DATA *MnpServiceData,
133 IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData,
134 OUT UINT8 **PktBuf,
135 OUT UINT32 *PktLen
136 )
137 {
138 EFI_SIMPLE_NETWORK_MODE *SnpMode;
139 UINT8 *DstPos;
140 UINT16 Index;
141
142 if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) {
143 //
144 // Media header is in FragmentTable and there is only one fragment,
145 // use fragment buffer directly.
146 //
147 *PktBuf = TxData->FragmentTable[0].FragmentBuffer;
148 *PktLen = TxData->FragmentTable[0].FragmentLength;
149 } else {
150 //
151 // Either media header isn't in FragmentTable or there is more than
152 // one fragment, copy the data into the packet buffer. Reserve the
153 // media header space if necessary.
154 //
155 SnpMode = MnpServiceData->Snp->Mode;
156 DstPos = MnpServiceData->TxBuf;
157
158 *PktLen = 0;
159 if (TxData->DestinationAddress != NULL) {
160 //
161 // If dest address is not NULL, move DstPos to reserve space for the
162 // media header. Add the media header length to buflen.
163 //
164 DstPos += SnpMode->MediaHeaderSize;
165 *PktLen += SnpMode->MediaHeaderSize;
166 }
167
168 for (Index = 0; Index < TxData->FragmentCount; Index++) {
169 //
170 // Copy the data.
171 //
172 CopyMem (
173 DstPos,
174 TxData->FragmentTable[Index].FragmentBuffer,
175 TxData->FragmentTable[Index].FragmentLength
176 );
177 DstPos += TxData->FragmentTable[Index].FragmentLength;
178 }
179
180 //
181 // Set the buffer pointer and the buffer length.
182 //
183 *PktBuf = MnpServiceData->TxBuf;
184 *PktLen += TxData->DataLength + TxData->HeaderLength;
185 }
186 }
187
188
189 /**
190 Synchronously send out the packet.
191
192 @param MnpServiceData Pointer to the mnp service context data.
193 @param Packet Pointer to the pakcet buffer.
194 @param Length The length of the packet.
195 @param Token Pointer to the token the packet generated from.
196
197 @retval EFI_SUCCESS The packet is sent out.
198 @retval EFI_TIMEOUT Time out occurs, the packet isn't sent.
199 @retval EFI_DEVICE_ERROR An unexpected network error occurs.
200
201 **/
202 EFI_STATUS
203 MnpSyncSendPacket (
204 IN MNP_SERVICE_DATA *MnpServiceData,
205 IN UINT8 *Packet,
206 IN UINT32 Length,
207 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
208 )
209 {
210 EFI_STATUS Status;
211 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
212 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
213 UINT32 HeaderSize;
214 UINT8 *TxBuf;
215
216 Snp = MnpServiceData->Snp;
217 TxData = Token->Packet.TxData;
218
219 HeaderSize = Snp->Mode->MediaHeaderSize - TxData->HeaderLength;
220
221 //
222 // Start the timeout event.
223 //
224 Status = gBS->SetTimer (
225 MnpServiceData->TxTimeoutEvent,
226 TimerRelative,
227 MNP_TX_TIMEOUT_TIME
228 );
229 if (EFI_ERROR (Status)) {
230
231 goto SIGNAL_TOKEN;
232 }
233
234 for (;;) {
235 //
236 // Transmit the packet through SNP.
237 //
238 Status = Snp->Transmit (
239 Snp,
240 HeaderSize,
241 Length,
242 Packet,
243 TxData->SourceAddress,
244 TxData->DestinationAddress,
245 &TxData->ProtocolType
246 );
247 if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
248
249 Status = EFI_DEVICE_ERROR;
250 break;
251 }
252
253 //
254 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
255 // if Status is EFI_NOT_READY, the transmit engine of the network interface is busy.
256 // Both need to sync SNP.
257 //
258 TxBuf = NULL;
259 do {
260 //
261 // Get the recycled transmit buffer status.
262 //
263 Snp->GetStatus (Snp, NULL, (VOID **) &TxBuf);
264
265 if (!EFI_ERROR (gBS->CheckEvent (MnpServiceData->TxTimeoutEvent))) {
266
267 Status = EFI_TIMEOUT;
268 break;
269 }
270 } while (TxBuf == NULL);
271
272 if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {
273
274 break;
275 } else {
276 //
277 // Status is EFI_NOT_READY. Restart the timer event and call Snp->Transmit again.
278 //
279 gBS->SetTimer (
280 MnpServiceData->TxTimeoutEvent,
281 TimerRelative,
282 MNP_TX_TIMEOUT_TIME
283 );
284 }
285 }
286
287 //
288 // Cancel the timer event.
289 //
290 gBS->SetTimer (MnpServiceData->TxTimeoutEvent, TimerCancel, 0);
291
292 SIGNAL_TOKEN:
293
294 Token->Status = Status;
295 gBS->SignalEvent (Token->Event);
296
297 //
298 // Dispatch the DPC queued by the NotifyFunction of Token->Event.
299 //
300 NetLibDispatchDpc ();
301
302 return EFI_SUCCESS;
303 }
304
305
306 /**
307 Try to deliver the received packet to the instance.
308
309 @param Instance Pointer to the mnp instance context data.
310
311 @retval EFI_SUCCESS The received packet is delivered, or there is no
312 packet to deliver, or there is no available receive
313 token.
314 @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource.
315
316 **/
317 EFI_STATUS
318 MnpInstanceDeliverPacket (
319 IN MNP_INSTANCE_DATA *Instance
320 )
321 {
322 MNP_SERVICE_DATA *MnpServiceData;
323 MNP_RXDATA_WRAP *RxDataWrap;
324 NET_BUF *DupNbuf;
325 EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData;
326 EFI_SIMPLE_NETWORK_MODE *SnpMode;
327 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken;
328
329 MnpServiceData = Instance->MnpServiceData;
330 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
331
332 if (NetMapIsEmpty (&Instance->RxTokenMap) || IsListEmpty (&Instance->RcvdPacketQueue)) {
333 //
334 // No pending received data or no available receive token, return.
335 //
336 return EFI_SUCCESS;
337 }
338
339 ASSERT (Instance->RcvdPacketQueueSize != 0);
340
341 RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);
342 if (RxDataWrap->Nbuf->RefCnt > 2) {
343 //
344 // There are other instances share this Nbuf, duplicate to get a
345 // copy to allow the instance to do R/W operations.
346 //
347 DupNbuf = MnpAllocNbuf (MnpServiceData);
348 if (DupNbuf == NULL) {
349 DEBUG ((EFI_D_WARN, "MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));
350
351 return EFI_OUT_OF_RESOURCES;
352 }
353
354 //
355 // Duplicate the net buffer.
356 //
357 NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0);
358 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
359 RxDataWrap->Nbuf = DupNbuf;
360 }
361
362 //
363 // All resources are OK, remove the packet from the queue.
364 //
365 NetListRemoveHead (&Instance->RcvdPacketQueue);
366 Instance->RcvdPacketQueueSize--;
367
368 RxData = &RxDataWrap->RxData;
369 SnpMode = MnpServiceData->Snp->Mode;
370
371 //
372 // Set all the buffer pointers.
373 //
374 RxData->MediaHeader = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL);
375 RxData->DestinationAddress = RxData->MediaHeader;
376 RxData->SourceAddress = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize;
377 RxData->PacketData = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize;
378
379 //
380 // Insert this RxDataWrap into the delivered queue.
381 //
382 InsertTailList (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry);
383
384 //
385 // Get the receive token from the RxTokenMap.
386 //
387 RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL);
388
389 //
390 // Signal this token's event.
391 //
392 RxToken->Packet.RxData = &RxDataWrap->RxData;
393 RxToken->Status = EFI_SUCCESS;
394 gBS->SignalEvent (RxToken->Event);
395
396 return EFI_SUCCESS;
397 }
398
399
400 /**
401 Deliver the received packet for the instances belonging to the MnpServiceData.
402
403 @param MnpServiceData Pointer to the mnp service context data.
404
405 @return None.
406
407 **/
408 VOID
409 MnpDeliverPacket (
410 IN MNP_SERVICE_DATA *MnpServiceData
411 )
412 {
413 LIST_ENTRY *Entry;
414 MNP_INSTANCE_DATA *Instance;
415
416 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
417
418 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
419 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
420 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
421
422 //
423 // Try to deliver packet for this instance.
424 //
425 MnpInstanceDeliverPacket (Instance);
426 }
427 }
428
429
430 /**
431 Recycle the RxData and other resources used to hold and deliver the received
432 packet.
433
434 @param Event The event this notify function registered to.
435 @param Context Pointer to the context data registerd to the Event.
436
437 @return None.
438
439 **/
440 VOID
441 EFIAPI
442 MnpRecycleRxData (
443 IN EFI_EVENT Event,
444 IN VOID *Context
445 )
446 {
447 MNP_RXDATA_WRAP *RxDataWrap;
448 MNP_SERVICE_DATA *MnpServiceData;
449
450 ASSERT (Context != NULL);
451
452 RxDataWrap = (MNP_RXDATA_WRAP *) Context;
453 NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE);
454
455 ASSERT (RxDataWrap->Nbuf != NULL);
456
457 MnpServiceData = RxDataWrap->Instance->MnpServiceData;
458 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
459
460 //
461 // Free this Nbuf.
462 //
463 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
464 RxDataWrap->Nbuf = NULL;
465
466 //
467 // Close the recycle event.
468 //
469 gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent);
470
471 //
472 // Remove this Wrap entry from the list.
473 //
474 RemoveEntryList (&RxDataWrap->WrapEntry);
475
476 gBS->FreePool (RxDataWrap);
477 }
478
479
480 /**
481 Queue the received packet into instance's receive queue.
482
483 @param Instance Pointer to the mnp instance context data.
484 @param RxDataWrap Pointer to the Wrap structure containing the
485 received data and other information.
486
487 @return None.
488
489 **/
490 VOID
491 MnpQueueRcvdPacket (
492 IN MNP_INSTANCE_DATA *Instance,
493 IN MNP_RXDATA_WRAP *RxDataWrap
494 )
495 {
496 MNP_RXDATA_WRAP *OldRxDataWrap;
497
498 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
499
500 //
501 // Check the queue size. If it exceeds the limit, drop one packet
502 // from the head.
503 //
504 if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) {
505
506 DEBUG ((EFI_D_WARN, "MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n"));
507
508 //
509 // Get the oldest packet.
510 //
511 OldRxDataWrap = NET_LIST_HEAD (
512 &Instance->RcvdPacketQueue,
513 MNP_RXDATA_WRAP,
514 WrapEntry
515 );
516
517 //
518 // Recycle this OldRxDataWrap, this entry will be removed by the callee.
519 //
520 MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap);
521 Instance->RcvdPacketQueueSize--;
522 }
523
524 //
525 // Update the timeout tick using the configured parameter.
526 //
527 RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue;
528
529 //
530 // Insert this Wrap into the instance queue.
531 //
532 InsertTailList (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry);
533 Instance->RcvdPacketQueueSize++;
534 }
535
536
537 /**
538 Match the received packet with the instance receive filters.
539
540 @param Instance Pointer to the mnp instance context data.
541 @param RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.
542 @param GroupAddress Pointer to the GroupAddress, the GroupAddress is
543 non-NULL and it contains the destination multicast
544 mac address of the received packet if the packet
545 destinated to a multicast mac address.
546 @param PktAttr The received packets attribute.
547
548 @return The received packet matches the instance's receive filters or not.
549
550 **/
551 BOOLEAN
552 MnpMatchPacket (
553 IN MNP_INSTANCE_DATA *Instance,
554 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData,
555 IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
556 IN UINT8 PktAttr
557 )
558 {
559 EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData;
560 LIST_ENTRY *Entry;
561 MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk;
562
563 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
564
565 ConfigData = &Instance->ConfigData;
566
567 //
568 // Check the protocol type.
569 //
570 if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) {
571 return FALSE;
572 }
573
574 if (ConfigData->EnablePromiscuousReceive) {
575 //
576 // Always match if this instance is configured to be promiscuous.
577 //
578 return TRUE;
579 }
580
581 //
582 // The protocol type is matched, check receive filter, include unicast and broadcast.
583 //
584 if ((Instance->ReceiveFilter & PktAttr) != 0) {
585 return TRUE;
586 }
587
588 //
589 // Check multicast addresses.
590 //
591 if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) {
592
593 ASSERT (GroupAddress != NULL);
594
595 NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) {
596
597 GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry);
598 if (GroupCtrlBlk->GroupAddress == GroupAddress) {
599 //
600 // The instance is configured to receiveing packets destinated to this
601 // multicast address.
602 //
603 return TRUE;
604 }
605 }
606 }
607
608 //
609 // No match.
610 //
611 return FALSE;
612 }
613
614
615 /**
616 Analyse the received packets.
617
618 @param MnpServiceData Pointer to the mnp service context data.
619 @param Nbuf Pointer to the net buffer holding the received
620 packet.
621 @param RxData Pointer to the buffer used to save the analysed
622 result in EFI_MANAGED_NETWORK_RECEIVE_DATA.
623 @param GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to
624 pass out the address of the multicast address the
625 received packet destinated to.
626 @param PktAttr Pointer to the buffer used to save the analysed
627 packet attribute.
628
629 @return None.
630
631 **/
632 VOID
633 MnpAnalysePacket (
634 IN MNP_SERVICE_DATA *MnpServiceData,
635 IN NET_BUF *Nbuf,
636 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData,
637 OUT MNP_GROUP_ADDRESS **GroupAddress,
638 OUT UINT8 *PktAttr
639 )
640 {
641 EFI_SIMPLE_NETWORK_MODE *SnpMode;
642 UINT8 *BufPtr;
643 LIST_ENTRY *Entry;
644
645 SnpMode = MnpServiceData->Snp->Mode;
646
647 //
648 // Get the packet buffer.
649 //
650 BufPtr = NetbufGetByte (Nbuf, 0, NULL);
651 ASSERT (BufPtr != NULL);
652
653 //
654 // Set the initial values.
655 //
656 RxData->BroadcastFlag = FALSE;
657 RxData->MulticastFlag = FALSE;
658 RxData->PromiscuousFlag = FALSE;
659 *PktAttr = UNICAST_PACKET;
660
661 if (!NET_MAC_EQUAL (&SnpMode->CurrentAddress, BufPtr, SnpMode->HwAddressSize)) {
662 //
663 // This packet isn't destinated to our current mac address, it't not unicast.
664 //
665 *PktAttr = 0;
666
667 if (NET_MAC_EQUAL (&SnpMode->BroadcastAddress, BufPtr, SnpMode->HwAddressSize)) {
668 //
669 // It's broadcast.
670 //
671 RxData->BroadcastFlag = TRUE;
672 *PktAttr = BROADCAST_PACKET;
673 } else if ((*BufPtr & 0x01) == 0x1) {
674 //
675 // It's multicast, try to match the multicast filters.
676 //
677 NET_LIST_FOR_EACH (Entry, &MnpServiceData->GroupAddressList) {
678
679 *GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
680 if (NET_MAC_EQUAL (BufPtr, &((*GroupAddress)->Address), SnpMode->HwAddressSize)) {
681 RxData->MulticastFlag = TRUE;
682 break;
683 }
684 }
685
686 if (!RxData->MulticastFlag) {
687 //
688 // No match, set GroupAddress to NULL. This multicast packet must
689 // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on.
690 //
691 *GroupAddress = NULL;
692 RxData->PromiscuousFlag = TRUE;
693
694 if (MnpServiceData->PromiscuousCount == 0) {
695 //
696 // Skip the below code, there is no receiver of this packet.
697 //
698 return ;
699 }
700 }
701 } else {
702 RxData->PromiscuousFlag = TRUE;
703 }
704 }
705
706 ZeroMem (&RxData->Timestamp, sizeof (EFI_TIME));
707
708 //
709 // Fill the common parts of RxData.
710 //
711 RxData->PacketLength = Nbuf->TotalSize;
712 RxData->HeaderLength = SnpMode->MediaHeaderSize;
713 RxData->AddressLength = SnpMode->HwAddressSize;
714 RxData->DataLength = RxData->PacketLength - RxData->HeaderLength;
715 RxData->ProtocolType = NTOHS (*(UINT16 *) (BufPtr + 2 * SnpMode->HwAddressSize));
716 }
717
718
719 /**
720 Wrap the RxData.
721
722 @param Instance Pointer to the mnp instance context data.
723 @param RxData Pointer to the receive data to wrap.
724
725 @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData.
726
727 **/
728 MNP_RXDATA_WRAP *
729 MnpWrapRxData (
730 IN MNP_INSTANCE_DATA *Instance,
731 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData
732 )
733 {
734 EFI_STATUS Status;
735 MNP_RXDATA_WRAP *RxDataWrap;
736
737 //
738 // Allocate memory.
739 //
740 RxDataWrap = AllocatePool (sizeof (MNP_RXDATA_WRAP));
741 if (RxDataWrap == NULL) {
742 DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));
743 return NULL;
744 }
745
746 RxDataWrap->Instance = Instance;
747
748 //
749 // Fill the RxData in RxDataWrap,
750 //
751 CopyMem (&RxDataWrap->RxData, RxData, sizeof (RxDataWrap->RxData));
752
753 //
754 // Create the recycle event.
755 //
756 Status = gBS->CreateEvent (
757 EVT_NOTIFY_SIGNAL,
758 TPL_NOTIFY,
759 MnpRecycleRxData,
760 RxDataWrap,
761 &RxDataWrap->RxData.RecycleEvent
762 );
763 if (EFI_ERROR (Status)) {
764
765 DEBUG ((EFI_D_ERROR, "MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status));
766 gBS->FreePool (RxDataWrap);
767 return NULL;
768 }
769
770 return RxDataWrap;
771 }
772
773
774 /**
775 Enqueue the received the packets to the instances belonging to the
776 MnpServiceData.
777
778 @param MnpServiceData Pointer to the mnp service context data.
779 @param Nbuf Pointer to the net buffer representing the received
780 packet.
781
782 @return None.
783
784 **/
785 VOID
786 MnpEnqueuePacket (
787 IN MNP_SERVICE_DATA *MnpServiceData,
788 IN NET_BUF *Nbuf
789 )
790 {
791 LIST_ENTRY *Entry;
792 MNP_INSTANCE_DATA *Instance;
793 EFI_MANAGED_NETWORK_RECEIVE_DATA RxData;
794 UINT8 PktAttr;
795 MNP_GROUP_ADDRESS *GroupAddress;
796 MNP_RXDATA_WRAP *RxDataWrap;
797
798
799 GroupAddress = NULL;
800 //
801 // First, analyse the packet header.
802 //
803 MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr);
804
805 if (RxData.PromiscuousFlag && (MnpServiceData->PromiscuousCount == 0)) {
806 //
807 // No receivers, no more action need.
808 //
809 return ;
810 }
811
812 //
813 // Iterate the children to find match.
814 //
815 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
816
817 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
818 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
819
820 if (!Instance->Configured) {
821 continue;
822 }
823
824 //
825 // Check the packet against the instance receive filters.
826 //
827 if (MnpMatchPacket (Instance, &RxData, GroupAddress, PktAttr)) {
828
829 //
830 // Wrap the RxData.
831 //
832 RxDataWrap = MnpWrapRxData (Instance, &RxData);
833 if (RxDataWrap == NULL) {
834 continue;
835 }
836
837 //
838 // Associate RxDataWrap with Nbuf and increase the RefCnt.
839 //
840 RxDataWrap->Nbuf = Nbuf;
841 NET_GET_REF (RxDataWrap->Nbuf);
842
843 //
844 // Queue the packet into the instance queue.
845 //
846 MnpQueueRcvdPacket (Instance, RxDataWrap);
847 }
848 }
849 }
850
851
852 /**
853 Try to receive a packet and deliver it.
854
855 @param MnpServiceData Pointer to the mnp service context data.
856
857 @retval EFI_SUCCESS add return value to function comment
858 @retval EFI_NOT_STARTED The simple network protocol is not started.
859 @retval EFI_NOT_READY No packet received.
860 @retval EFI_DEVICE_ERROR An unexpected error occurs.
861
862 **/
863 EFI_STATUS
864 MnpReceivePacket (
865 IN MNP_SERVICE_DATA *MnpServiceData
866 )
867 {
868 EFI_STATUS Status;
869 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
870 NET_BUF *Nbuf;
871 UINT8 *BufPtr;
872 UINTN BufLen;
873 UINTN HeaderSize;
874 UINT32 Trimmed;
875
876 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
877
878 Snp = MnpServiceData->Snp;
879 if (Snp->Mode->State != EfiSimpleNetworkInitialized) {
880 //
881 // The simple network protocol is not started.
882 //
883 return EFI_NOT_STARTED;
884 }
885
886 if (IsListEmpty (&MnpServiceData->ChildrenList)) {
887 //
888 // There is no child, no need to receive packets.
889 //
890 return EFI_SUCCESS;
891 }
892
893 if (MnpServiceData->RxNbufCache == NULL) {
894 //
895 // Try to get a new buffer as there may be buffers recycled.
896 //
897 MnpServiceData->RxNbufCache = MnpAllocNbuf (MnpServiceData);
898
899 if (MnpServiceData->RxNbufCache == NULL) {
900 //
901 // No availabe buffer in the buffer pool.
902 //
903 return EFI_DEVICE_ERROR;
904 }
905
906 NetbufAllocSpace (
907 MnpServiceData->RxNbufCache,
908 MnpServiceData->BufferLength,
909 NET_BUF_TAIL
910 );
911 }
912
913 Nbuf = MnpServiceData->RxNbufCache;
914 BufLen = Nbuf->TotalSize;
915 BufPtr = NetbufGetByte (Nbuf, 0, NULL);
916 ASSERT (BufPtr != NULL);
917
918 //
919 // Receive packet through Snp.
920 //
921 Status = Snp->Receive (Snp, &HeaderSize, &BufLen, BufPtr, NULL, NULL, NULL);
922 if (EFI_ERROR (Status)) {
923
924 DEBUG_CODE (
925 if (Status != EFI_NOT_READY) {
926 DEBUG ((EFI_D_WARN, "MnpReceivePacket: Snp->Receive() = %r.\n", Status));
927 }
928 );
929
930 return Status;
931 }
932
933 //
934 // Sanity check.
935 //
936 if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) {
937
938 DEBUG (
939 (EFI_D_WARN,
940 "MnpReceivePacket: Size error, HL:TL = %d:%d.\n",
941 HeaderSize,
942 BufLen)
943 );
944 return EFI_DEVICE_ERROR;
945 }
946
947 Trimmed = 0;
948 if (Nbuf->TotalSize != BufLen) {
949 //
950 // Trim the packet from tail.
951 //
952 Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL);
953 ASSERT (Nbuf->TotalSize == BufLen);
954 }
955
956 //
957 // Enqueue the packet to the matched instances.
958 //
959 MnpEnqueuePacket (MnpServiceData, Nbuf);
960
961 if (Nbuf->RefCnt > 2) {
962 //
963 // RefCnt > 2 indicates there is at least one receiver of this packet.
964 // Free the current RxNbufCache and allocate a new one.
965 //
966 MnpFreeNbuf (MnpServiceData, Nbuf);
967
968 Nbuf = MnpAllocNbuf (MnpServiceData);
969 MnpServiceData->RxNbufCache = Nbuf;
970 if (Nbuf == NULL) {
971 DEBUG ((EFI_D_ERROR, "MnpReceivePacket: Alloc packet for receiving cache failed.\n"));
972 return EFI_DEVICE_ERROR;
973 }
974
975 NetbufAllocSpace (Nbuf, MnpServiceData->BufferLength, NET_BUF_TAIL);
976 } else {
977 //
978 // No receiver for this packet.
979 //
980 if (Trimmed > 0) {
981 NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);
982 }
983
984 goto EXIT;
985 }
986 //
987 // Deliver the queued packets.
988 //
989 MnpDeliverPacket (MnpServiceData);
990
991 //
992 // Dispatch the DPC queued by the NotifyFunction of rx token's events.
993 //
994 NetLibDispatchDpc ();
995
996 EXIT:
997
998 ASSERT (Nbuf->TotalSize == MnpServiceData->BufferLength);
999
1000 return Status;
1001 }
1002
1003
1004 /**
1005 Remove the received packets if timeout occurs.
1006
1007 @param Event The event this notify function registered to.
1008 @param Context Pointer to the context data registered to the
1009 event.
1010
1011 @return None.
1012
1013 **/
1014 VOID
1015 EFIAPI
1016 MnpCheckPacketTimeout (
1017 IN EFI_EVENT Event,
1018 IN VOID *Context
1019 )
1020 {
1021 MNP_SERVICE_DATA *MnpServiceData;
1022 LIST_ENTRY *Entry;
1023 LIST_ENTRY *RxEntry;
1024 LIST_ENTRY *NextEntry;
1025 MNP_INSTANCE_DATA *Instance;
1026 MNP_RXDATA_WRAP *RxDataWrap;
1027 EFI_TPL OldTpl;
1028
1029 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1030 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1031
1032 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
1033
1034 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
1035 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1036
1037 if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) {
1038 //
1039 // This instance is not configured or there is no receive time out,
1040 // just skip to the next instance.
1041 //
1042 continue;
1043 }
1044
1045 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1046
1047 NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) {
1048
1049 RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry);
1050
1051 if (RxDataWrap->TimeoutTick >= MNP_TIMEOUT_CHECK_INTERVAL) {
1052
1053 RxDataWrap->TimeoutTick -= MNP_TIMEOUT_CHECK_INTERVAL;
1054 } else {
1055 //
1056 // Drop the timeout packet.
1057 //
1058 DEBUG ((EFI_D_WARN, "MnpCheckPacketTimeout: Received packet timeout.\n"));
1059 MnpRecycleRxData (NULL, RxDataWrap);
1060 Instance->RcvdPacketQueueSize--;
1061 }
1062 }
1063
1064 gBS->RestoreTPL (OldTpl);
1065 }
1066 }
1067
1068
1069 /**
1070 Poll to receive the packets from Snp. This function is either called by upperlayer
1071 protocols/applications or the system poll timer notify mechanism.
1072
1073 @param Event The event this notify function registered to.
1074 @param Context Pointer to the context data registered to the
1075 event.
1076
1077 @return None.
1078
1079 **/
1080 VOID
1081 EFIAPI
1082 MnpSystemPoll (
1083 IN EFI_EVENT Event,
1084 IN VOID *Context
1085 )
1086 {
1087 MNP_SERVICE_DATA *MnpServiceData;
1088
1089 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1090 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1091
1092 //
1093 // Try to receive packets from Snp.
1094 //
1095 MnpReceivePacket (MnpServiceData);
1096
1097 NetLibDispatchDpc ();
1098 }