]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c
aafc8433a52f8527120b04513aeef147c69eb0b8
[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 <Library/NetLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/BaseLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include "MnpImpl.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 MNP_DEBUG_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 MNP_DEBUG_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 MNP_DEBUG_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 MNP_DEBUG_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 MNP_DEBUG_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 NetCopyMem (
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 return EFI_SUCCESS;
298 }
299
300
301 /**
302 Try to deliver the received packet to the instance.
303
304 @param Instance Pointer to the mnp instance context data.
305
306 @retval EFI_SUCCESS The received packet is delivered, or there is no
307 packet to deliver, or there is no available receive
308 token.
309 @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource.
310
311 **/
312 EFI_STATUS
313 MnpInstanceDeliverPacket (
314 IN MNP_INSTANCE_DATA *Instance
315 )
316 {
317 MNP_SERVICE_DATA *MnpServiceData;
318 MNP_RXDATA_WRAP *RxDataWrap;
319 NET_BUF *DupNbuf;
320 EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData;
321 EFI_SIMPLE_NETWORK_MODE *SnpMode;
322 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken;
323
324 MnpServiceData = Instance->MnpServiceData;
325 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
326
327 if (NetMapIsEmpty (&Instance->RxTokenMap) || NetListIsEmpty (&Instance->RcvdPacketQueue)) {
328 //
329 // No pending received data or no available receive token, return.
330 //
331 return EFI_SUCCESS;
332 }
333
334 ASSERT (Instance->RcvdPacketQueueSize != 0);
335
336 RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);
337 if (RxDataWrap->Nbuf->RefCnt > 2) {
338 //
339 // There are other instances share this Nbuf, duplicate to get a
340 // copy to allow the instance to do R/W operations.
341 //
342 DupNbuf = MnpAllocNbuf (MnpServiceData);
343 if (DupNbuf == NULL) {
344 MNP_DEBUG_WARN (("MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));
345
346 return EFI_OUT_OF_RESOURCES;
347 }
348
349 //
350 // Duplicate the net buffer.
351 //
352 NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0);
353 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
354 RxDataWrap->Nbuf = DupNbuf;
355 }
356
357 //
358 // All resources are OK, remove the packet from the queue.
359 //
360 NetListRemoveHead (&Instance->RcvdPacketQueue);
361 Instance->RcvdPacketQueueSize--;
362
363 RxData = &RxDataWrap->RxData;
364 SnpMode = MnpServiceData->Snp->Mode;
365
366 //
367 // Set all the buffer pointers.
368 //
369 RxData->MediaHeader = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL);
370 RxData->DestinationAddress = RxData->MediaHeader;
371 RxData->SourceAddress = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize;
372 RxData->PacketData = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize;
373
374 //
375 // Insert this RxDataWrap into the delivered queue.
376 //
377 NetListInsertTail (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry);
378
379 //
380 // Get the receive token from the RxTokenMap.
381 //
382 RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL);
383
384 //
385 // Signal this token's event.
386 //
387 RxToken->Packet.RxData = &RxDataWrap->RxData;
388 RxToken->Status = EFI_SUCCESS;
389 gBS->SignalEvent (RxToken->Event);
390
391 return EFI_SUCCESS;
392 }
393
394
395 /**
396 Deliver the received packet for the instances belonging to the MnpServiceData.
397
398 @param MnpServiceData Pointer to the mnp service context data.
399
400 @return None.
401
402 **/
403 STATIC
404 VOID
405 MnpDeliverPacket (
406 IN MNP_SERVICE_DATA *MnpServiceData
407 )
408 {
409 NET_LIST_ENTRY *Entry;
410 MNP_INSTANCE_DATA *Instance;
411
412 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
413
414 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
415 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
416 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
417
418 //
419 // Try to deliver packet for this instance.
420 //
421 MnpInstanceDeliverPacket (Instance);
422 }
423 }
424
425
426 /**
427 Recycle the RxData and other resources used to hold and deliver the received
428 packet.
429
430 @param Event The event this notify function registered to.
431 @param Context Pointer to the context data registerd to the Event.
432
433 @return None.
434
435 **/
436 VOID
437 EFIAPI
438 MnpRecycleRxData (
439 IN EFI_EVENT Event,
440 IN VOID *Context
441 )
442 {
443 MNP_RXDATA_WRAP *RxDataWrap;
444 MNP_SERVICE_DATA *MnpServiceData;
445
446 ASSERT (Context != NULL);
447
448 RxDataWrap = (MNP_RXDATA_WRAP *) Context;
449 NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE);
450
451 ASSERT (RxDataWrap->Nbuf != NULL);
452
453 MnpServiceData = RxDataWrap->Instance->MnpServiceData;
454 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
455
456 //
457 // Free this Nbuf.
458 //
459 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
460 RxDataWrap->Nbuf = NULL;
461
462 //
463 // Close the recycle event.
464 //
465 gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent);
466
467 //
468 // Remove this Wrap entry from the list.
469 //
470 NetListRemoveEntry (&RxDataWrap->WrapEntry);
471
472 NetFreePool (RxDataWrap);
473 }
474
475
476 /**
477 Queue the received packet into instance's receive queue.
478
479 @param Instance Pointer to the mnp instance context data.
480 @param RxDataWrap Pointer to the Wrap structure containing the
481 received data and other information.
482
483 @return None.
484
485 **/
486 STATIC
487 VOID
488 MnpQueueRcvdPacket (
489 IN MNP_INSTANCE_DATA *Instance,
490 IN MNP_RXDATA_WRAP *RxDataWrap
491 )
492 {
493 MNP_RXDATA_WRAP *OldRxDataWrap;
494
495 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
496
497 //
498 // Check the queue size. If it exceeds the limit, drop one packet
499 // from the head.
500 //
501 if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) {
502
503 MNP_DEBUG_WARN (("MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n"));
504
505 //
506 // Get the oldest packet.
507 //
508 OldRxDataWrap = NET_LIST_HEAD (
509 &Instance->RcvdPacketQueue,
510 MNP_RXDATA_WRAP,
511 WrapEntry
512 );
513
514 //
515 // Recycle this OldRxDataWrap, this entry will be removed by the callee.
516 //
517 MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap);
518 Instance->RcvdPacketQueueSize--;
519 }
520
521 //
522 // Update the timeout tick using the configured parameter.
523 //
524 RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue;
525
526 //
527 // Insert this Wrap into the instance queue.
528 //
529 NetListInsertTail (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry);
530 Instance->RcvdPacketQueueSize++;
531 }
532
533
534 /**
535 Match the received packet with the instance receive filters.
536
537 @param Instance Pointer to the mnp instance context data.
538 @param RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.
539 @param GroupAddress Pointer to the GroupAddress, the GroupAddress is
540 non-NULL and it contains the destination multicast
541 mac address of the received packet if the packet
542 destinated to a multicast mac address.
543 @param PktAttr The received packets attribute.
544
545 @return The received packet matches the instance's receive filters or not.
546
547 **/
548 STATIC
549 BOOLEAN
550 MnpMatchPacket (
551 IN MNP_INSTANCE_DATA *Instance,
552 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData,
553 IN MNP_GROUP_ADDRESS *GroupAddress OPTIONAL,
554 IN UINT8 PktAttr
555 )
556 {
557 EFI_MANAGED_NETWORK_CONFIG_DATA *ConfigData;
558 NET_LIST_ENTRY *Entry;
559 MNP_GROUP_CONTROL_BLOCK *GroupCtrlBlk;
560
561 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
562
563 ConfigData = &Instance->ConfigData;
564
565 if (ConfigData->EnablePromiscuousReceive) {
566 //
567 // Always match if this instance is configured to be promiscuous.
568 //
569 return TRUE;
570 }
571 //
572 // Check the protocol type.
573 //
574 if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) {
575 return FALSE;
576 }
577
578 //
579 // The protocol type is matched, check receive filter, include unicast and broadcast.
580 //
581 if ((Instance->ReceiveFilter & PktAttr) != 0) {
582 return TRUE;
583 }
584
585 //
586 // Check multicast addresses.
587 //
588 if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) {
589
590 ASSERT (GroupAddress != NULL);
591
592 NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) {
593
594 GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry);
595 if (GroupCtrlBlk->GroupAddress == GroupAddress) {
596 //
597 // The instance is configured to receiveing packets destinated to this
598 // multicast address.
599 //
600 return TRUE;
601 }
602 }
603 }
604
605 //
606 // No match.
607 //
608 return FALSE;
609 }
610
611
612 /**
613 Analyse the received packets.
614
615 @param MnpServiceData Pointer to the mnp service context data.
616 @param Nbuf Pointer to the net buffer holding the received
617 packet.
618 @param RxData Pointer to the buffer used to save the analysed
619 result in EFI_MANAGED_NETWORK_RECEIVE_DATA.
620 @param GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to
621 pass out the address of the multicast address the
622 received packet destinated to.
623 @param PktAttr Pointer to the buffer used to save the analysed
624 packet attribute.
625
626 @return None.
627
628 **/
629 STATIC
630 VOID
631 MnpAnalysePacket (
632 IN MNP_SERVICE_DATA *MnpServiceData,
633 IN NET_BUF *Nbuf,
634 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData,
635 OUT MNP_GROUP_ADDRESS **GroupAddress,
636 OUT UINT8 *PktAttr
637 )
638 {
639 EFI_SIMPLE_NETWORK_MODE *SnpMode;
640 UINT8 *BufPtr;
641 NET_LIST_ENTRY *Entry;
642
643 SnpMode = MnpServiceData->Snp->Mode;
644
645 //
646 // Get the packet buffer.
647 //
648 BufPtr = NetbufGetByte (Nbuf, 0, NULL);
649 ASSERT (BufPtr != NULL);
650
651 //
652 // Set the initial values.
653 //
654 RxData->BroadcastFlag = FALSE;
655 RxData->MulticastFlag = FALSE;
656 RxData->PromiscuousFlag = FALSE;
657 *PktAttr = UNICAST_PACKET;
658
659 if (!NET_MAC_EQUAL (&SnpMode->CurrentAddress, BufPtr, SnpMode->HwAddressSize)) {
660 //
661 // This packet isn't destinated to our current mac address, it't not unicast.
662 //
663 *PktAttr = 0;
664
665 if (NET_MAC_EQUAL (&SnpMode->BroadcastAddress, BufPtr, SnpMode->HwAddressSize)) {
666 //
667 // It's broadcast.
668 //
669 RxData->BroadcastFlag = TRUE;
670 *PktAttr = BROADCAST_PACKET;
671 } else if ((*BufPtr & 0x01) == 0x1) {
672 //
673 // It's multicast, try to match the multicast filters.
674 //
675 NET_LIST_FOR_EACH (Entry, &MnpServiceData->GroupAddressList) {
676
677 *GroupAddress = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_ADDRESS, AddrEntry);
678 if (NET_MAC_EQUAL (BufPtr, &((*GroupAddress)->Address), SnpMode->HwAddressSize)) {
679 RxData->MulticastFlag = TRUE;
680 break;
681 }
682 }
683
684 if (!RxData->MulticastFlag) {
685 //
686 // No match, set GroupAddress to NULL. This multicast packet must
687 // be the result of PROMISUCOUS or PROMISUCOUS_MULTICAST flag is on.
688 //
689 *GroupAddress = NULL;
690 RxData->PromiscuousFlag = TRUE;
691
692 if (MnpServiceData->PromiscuousCount == 0) {
693 //
694 // Skip the below code, there is no receiver of this packet.
695 //
696 return ;
697 }
698 }
699 } else {
700 RxData->PromiscuousFlag = TRUE;
701 }
702 }
703
704 NetZeroMem (&RxData->Timestamp, sizeof (EFI_TIME));
705
706 //
707 // Fill the common parts of RxData.
708 //
709 RxData->PacketLength = Nbuf->TotalSize;
710 RxData->HeaderLength = SnpMode->MediaHeaderSize;
711 RxData->AddressLength = SnpMode->HwAddressSize;
712 RxData->DataLength = RxData->PacketLength - RxData->HeaderLength;
713 RxData->ProtocolType = NTOHS (*(UINT16 *) (BufPtr + 2 * SnpMode->HwAddressSize));
714 }
715
716
717 /**
718 Wrap the RxData.
719
720 @param Instance Pointer to the mnp instance context data.
721 @param RxData Pointer to the receive data to wrap.
722
723 @return Pointer to a MNP_RXDATA_WRAP which wraps the RxData.
724
725 **/
726 STATIC
727 MNP_RXDATA_WRAP *
728 MnpWrapRxData (
729 IN MNP_INSTANCE_DATA *Instance,
730 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData
731 )
732 {
733 EFI_STATUS Status;
734 MNP_RXDATA_WRAP *RxDataWrap;
735
736 //
737 // Allocate memory.
738 //
739 RxDataWrap = NetAllocatePool (sizeof (MNP_RXDATA_WRAP));
740 if (RxDataWrap == NULL) {
741 MNP_DEBUG_ERROR (("MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));
742 return NULL;
743 }
744
745 RxDataWrap->Instance = Instance;
746
747 //
748 // Fill the RxData in RxDataWrap,
749 //
750 CopyMem (&RxDataWrap->RxData, RxData, sizeof (EFI_MANAGED_NETWORK_RECEIVE_DATA));
751
752 //
753 // Create the recycle event.
754 //
755 Status = gBS->CreateEvent (
756 EVT_NOTIFY_SIGNAL,
757 NET_TPL_RECYCLE,
758 MnpRecycleRxData,
759 RxDataWrap,
760 &RxDataWrap->RxData.RecycleEvent
761 );
762 if (EFI_ERROR (Status)) {
763
764 MNP_DEBUG_ERROR (("MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status));
765 NetFreePool (RxDataWrap);
766 return NULL;
767 }
768
769 return RxDataWrap;
770 }
771
772
773 /**
774 Enqueue the received the packets to the instances belonging to the
775 MnpServiceData.
776
777 @param MnpServiceData Pointer to the mnp service context data.
778 @param Nbuf Pointer to the net buffer representing the received
779 packet.
780
781 @return None.
782
783 **/
784 STATIC
785 VOID
786 MnpEnqueuePacket (
787 IN MNP_SERVICE_DATA *MnpServiceData,
788 IN NET_BUF *Nbuf
789 )
790 {
791 NET_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 // First, analyse the packet header.
800 //
801 MnpAnalysePacket (MnpServiceData, Nbuf, &RxData, &GroupAddress, &PktAttr);
802
803 if (RxData.PromiscuousFlag && (MnpServiceData->PromiscuousCount == 0)) {
804 //
805 // No receivers, no more action need.
806 //
807 return ;
808 }
809
810 //
811 // Iterate the children to find match.
812 //
813 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
814
815 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
816 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
817
818 if (!Instance->Configured) {
819 continue;
820 }
821
822 //
823 // Check the packet against the instance receive filters.
824 //
825 if (MnpMatchPacket (Instance, &RxData, GroupAddress, PktAttr)) {
826
827 //
828 // Wrap the RxData.
829 //
830 CopyMem (&RxDataWrap, MnpWrapRxData (Instance, &RxData), sizeof (MNP_RXDATA_WRAP));
831 if (RxDataWrap == NULL) {
832 continue;
833 }
834
835 //
836 // Associate RxDataWrap with Nbuf and increase the RefCnt.
837 //
838 RxDataWrap->Nbuf = Nbuf;
839 NET_GET_REF (RxDataWrap->Nbuf);
840
841 //
842 // Queue the packet into the instance queue.
843 //
844 MnpQueueRcvdPacket (Instance, RxDataWrap);
845 }
846 }
847 }
848
849
850 /**
851 Try to receive a packet and deliver it.
852
853 @param MnpServiceData Pointer to the mnp service context data.
854
855 @retval EFI_SUCCESS add return value to function comment
856 @retval EFI_NOT_STARTED The simple network protocol is not started.
857 @retval EFI_NOT_READY No packet received.
858 @retval EFI_DEVICE_ERROR An unexpected error occurs.
859
860 **/
861 EFI_STATUS
862 MnpReceivePacket (
863 IN MNP_SERVICE_DATA *MnpServiceData
864 )
865 {
866 EFI_STATUS Status;
867 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
868 NET_BUF *Nbuf;
869 UINT8 *BufPtr;
870 UINTN BufLen;
871 UINTN HeaderSize;
872 UINT32 Trimmed;
873
874 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
875
876 Snp = MnpServiceData->Snp;
877 if (Snp->Mode->State != EfiSimpleNetworkInitialized) {
878 //
879 // The simple network protocol is not started.
880 //
881 return EFI_NOT_STARTED;
882 }
883
884 if (NetListIsEmpty (&MnpServiceData->ChildrenList)) {
885 //
886 // There is no child, no need to receive packets.
887 //
888 return EFI_SUCCESS;
889 }
890
891 if (MnpServiceData->RxNbufCache == NULL) {
892 //
893 // Try to get a new buffer as there may be buffers recycled.
894 //
895 MnpServiceData->RxNbufCache = MnpAllocNbuf (MnpServiceData);
896
897 if (MnpServiceData->RxNbufCache == NULL) {
898 //
899 // No availabe buffer in the buffer pool.
900 //
901 return EFI_DEVICE_ERROR;
902 }
903
904 NetbufAllocSpace (
905 MnpServiceData->RxNbufCache,
906 MnpServiceData->BufferLength,
907 NET_BUF_TAIL
908 );
909 }
910
911 Nbuf = MnpServiceData->RxNbufCache;
912 BufLen = Nbuf->TotalSize;
913 BufPtr = NetbufGetByte (Nbuf, 0, NULL);
914 ASSERT (BufPtr != NULL);
915
916 //
917 // Receive packet through Snp.
918 //
919 Status = Snp->Receive (Snp, &HeaderSize, &BufLen, BufPtr, NULL, NULL, NULL);
920 if (EFI_ERROR (Status)) {
921
922 DEBUG_CODE (
923 if (Status != EFI_NOT_READY) {
924 MNP_DEBUG_ERROR (("MnpReceivePacket: Snp->Receive() = %r.\n", Status));
925 }
926 );
927
928 return Status;
929 }
930
931 //
932 // Sanity check.
933 //
934 if ((HeaderSize != Snp->Mode->MediaHeaderSize) || (BufLen < HeaderSize)) {
935
936 MNP_DEBUG_WARN (
937 ("MnpReceivePacket: Size error, HL:TL = %d:%d.\n",
938 HeaderSize,
939 BufLen)
940 );
941 return EFI_DEVICE_ERROR;
942 }
943
944 Trimmed = 0;
945 if (Nbuf->TotalSize != BufLen) {
946 //
947 // Trim the packet from tail.
948 //
949 Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL);
950 ASSERT (Nbuf->TotalSize == BufLen);
951 }
952
953 //
954 // Enqueue the packet to the matched instances.
955 //
956 MnpEnqueuePacket (MnpServiceData, Nbuf);
957
958 if (Nbuf->RefCnt > 2) {
959 //
960 // RefCnt > 2 indicates there is at least one receiver of this packet.
961 // Free the current RxNbufCache and allocate a new one.
962 //
963 MnpFreeNbuf (MnpServiceData, Nbuf);
964
965 Nbuf = MnpAllocNbuf (MnpServiceData);
966 MnpServiceData->RxNbufCache = Nbuf;
967 if (Nbuf == NULL) {
968 MNP_DEBUG_ERROR (("MnpReceivePacket: Alloc packet for receiving cache failed.\n"));
969 return EFI_DEVICE_ERROR;
970 }
971
972 NetbufAllocSpace (Nbuf, MnpServiceData->BufferLength, NET_BUF_TAIL);
973 } else {
974 //
975 // No receiver for this packet.
976 //
977 if (Trimmed > 0) {
978 NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);
979 }
980
981 goto EXIT;
982 }
983 //
984 // Deliver the queued packets.
985 //
986 MnpDeliverPacket (MnpServiceData);
987
988 EXIT:
989
990 ASSERT (Nbuf->TotalSize == MnpServiceData->BufferLength);
991
992 return Status;
993 }
994
995
996 /**
997 Remove the received packets if timeout occurs.
998
999 @param Event The event this notify function registered to.
1000 @param Context Pointer to the context data registered to the
1001 event.
1002
1003 @return None.
1004
1005 **/
1006 VOID
1007 EFIAPI
1008 MnpCheckPacketTimeout (
1009 IN EFI_EVENT Event,
1010 IN VOID *Context
1011 )
1012 {
1013 MNP_SERVICE_DATA *MnpServiceData;
1014 NET_LIST_ENTRY *Entry;
1015 NET_LIST_ENTRY *RxEntry;
1016 NET_LIST_ENTRY *NextEntry;
1017 MNP_INSTANCE_DATA *Instance;
1018 MNP_RXDATA_WRAP *RxDataWrap;
1019 EFI_TPL OldTpl;
1020
1021 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1022 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1023
1024 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
1025
1026 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
1027 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1028
1029 if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) {
1030 //
1031 // This instance is not configured or there is no receive time out,
1032 // just skip to the next instance.
1033 //
1034 continue;
1035 }
1036
1037 OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
1038
1039 NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) {
1040
1041 RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry);
1042
1043 if (RxDataWrap->TimeoutTick >= MNP_TIMEOUT_CHECK_INTERVAL) {
1044
1045 RxDataWrap->TimeoutTick -= MNP_TIMEOUT_CHECK_INTERVAL;
1046 } else {
1047 //
1048 // Drop the timeout packet.
1049 //
1050 MNP_DEBUG_WARN (("MnpCheckPacketTimeout: Received packet timeout.\n"));
1051 MnpRecycleRxData (NULL, RxDataWrap);
1052 Instance->RcvdPacketQueueSize--;
1053 }
1054 }
1055
1056 NET_RESTORE_TPL (OldTpl);
1057 }
1058 }
1059
1060
1061 /**
1062 Poll to receive the packets from Snp. This function is either called by upperlayer
1063 protocols/applications or the system poll timer notify mechanism.
1064
1065 @param Event The event this notify function registered to.
1066 @param Context Pointer to the context data registered to the
1067 event.
1068
1069 @return None.
1070
1071 **/
1072 VOID
1073 EFIAPI
1074 MnpSystemPoll (
1075 IN EFI_EVENT Event,
1076 IN VOID *Context
1077 )
1078 {
1079 MNP_SERVICE_DATA *MnpServiceData;
1080
1081 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1082 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1083
1084 //
1085 // Try to receive packets from Snp.
1086 //
1087 MnpReceivePacket (MnpServiceData);
1088 }