]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/MnpDxe/MnpIo.c
Import SnpDxe, Tcp4Dxe, Udp4Dxe and MnpDxe.
[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_SIMPLE_NETWORK_MODE *SnpMode;
47 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
48 UINT32 Index;
49 UINT32 TotalLength;
50 EFI_MANAGED_NETWORK_FRAGMENT_DATA *FragmentTable;
51
52 MnpServiceData = Instance->MnpServiceData;
53 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
54
55 SnpMode = MnpServiceData->Snp->Mode;
56 TxData = Token->Packet.TxData;
57
58 if ((Token->Event == NULL) || (TxData == NULL) || (TxData->FragmentCount == 0)) {
59 //
60 // The token is invalid if the Event is NULL, or the TxData is NULL, or
61 // the fragment count is zero.
62 //
63 MNP_DEBUG_WARN (("MnpIsValidTxToken: Invalid Token.\n"));
64 return FALSE;
65 }
66
67 if ((TxData->DestinationAddress != NULL) && (TxData->HeaderLength != 0)) {
68 //
69 // The token is invalid if the HeaderLength isn't zero while the DestinationAddress
70 // is NULL (The destination address is already put into the packet).
71 //
72 MNP_DEBUG_WARN (("MnpIsValidTxToken: DestinationAddress isn't NULL, HeaderLength must be 0.\n"));
73 return FALSE;
74 }
75
76 TotalLength = 0;
77 FragmentTable = TxData->FragmentTable;
78 for (Index = 0; Index < TxData->FragmentCount; Index++) {
79
80 if ((FragmentTable[Index].FragmentLength == 0) || (FragmentTable[Index].FragmentBuffer == NULL)) {
81 //
82 // The token is invalid if any FragmentLength is zero or any FragmentBuffer is NULL.
83 //
84 MNP_DEBUG_WARN (("MnpIsValidTxToken: Invalid FragmentLength or FragmentBuffer.\n"));
85 return FALSE;
86 }
87
88 TotalLength += FragmentTable[Index].FragmentLength;
89 }
90
91 if ((TxData->DestinationAddress == NULL) && (FragmentTable[0].FragmentLength < TxData->HeaderLength)) {
92 //
93 // Media header is split between fragments.
94 //
95 return FALSE;
96 }
97
98 if (TotalLength != (TxData->DataLength + TxData->HeaderLength)) {
99 //
100 // The length calculated from the fragment information doesn't equal to the
101 // sum of the DataLength and the HeaderLength.
102 //
103 MNP_DEBUG_WARN (("MnpIsValidTxData: Invalid Datalength compared with the sum of fragment length.\n"));
104 return FALSE;
105 }
106
107 if (TxData->DataLength > MnpServiceData->Mtu) {
108 //
109 // The total length is larger than the MTU.
110 //
111 MNP_DEBUG_WARN (("MnpIsValidTxData: TxData->DataLength exceeds Mtu.\n"));
112 return FALSE;
113 }
114
115 return TRUE;
116 }
117
118
119 /**
120 Build the packet to transmit from the TxData passed in.
121
122 @param MnpServiceData Pointer to the mnp service context data.
123 @param TxData Pointer to the transmit data containing the
124 information to build the packet.
125 @param PktBuf Pointer to record the address of the packet.
126 @param PktLen Pointer to a UINT32 variable used to record the
127 packet's length.
128
129 @return None.
130
131 **/
132 VOID
133 MnpBuildTxPacket (
134 IN MNP_SERVICE_DATA *MnpServiceData,
135 IN EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData,
136 OUT UINT8 **PktBuf,
137 OUT UINT32 *PktLen
138 )
139 {
140 EFI_SIMPLE_NETWORK_MODE *SnpMode;
141 UINT8 *DstPos;
142 UINT16 Index;
143
144 if ((TxData->DestinationAddress == NULL) && (TxData->FragmentCount == 1)) {
145 //
146 // Media header is in FragmentTable and there is only one fragment,
147 // use fragment buffer directly.
148 //
149 *PktBuf = TxData->FragmentTable[0].FragmentBuffer;
150 *PktLen = TxData->FragmentTable[0].FragmentLength;
151 } else {
152 //
153 // Either media header isn't in FragmentTable or there is more than
154 // one fragment, copy the data into the packet buffer. Reserve the
155 // media header space if necessary.
156 //
157 SnpMode = MnpServiceData->Snp->Mode;
158 DstPos = MnpServiceData->TxBuf;
159
160 *PktLen = 0;
161 if (TxData->DestinationAddress != NULL) {
162 //
163 // If dest address is not NULL, move DstPos to reserve space for the
164 // media header. Add the media header length to buflen.
165 //
166 DstPos += SnpMode->MediaHeaderSize;
167 *PktLen += SnpMode->MediaHeaderSize;
168 }
169
170 for (Index = 0; Index < TxData->FragmentCount; Index++) {
171 //
172 // Copy the data.
173 //
174 NetCopyMem (
175 DstPos,
176 TxData->FragmentTable[Index].FragmentBuffer,
177 TxData->FragmentTable[Index].FragmentLength
178 );
179 DstPos += TxData->FragmentTable[Index].FragmentLength;
180 }
181
182 //
183 // Set the buffer pointer and the buffer length.
184 //
185 *PktBuf = MnpServiceData->TxBuf;
186 *PktLen += TxData->DataLength + TxData->HeaderLength;
187 }
188 }
189
190
191 /**
192 Synchronously send out the packet.
193
194 @param MnpServiceData Pointer to the mnp service context data.
195 @param Packet Pointer to the pakcet buffer.
196 @param Length The length of the packet.
197 @param Token Pointer to the token the packet generated from.
198
199 @retval EFI_SUCCESS The packet is sent out.
200 @retval EFI_TIMEOUT Time out occurs, the packet isn't sent.
201 @retval EFI_DEVICE_ERROR An unexpected network error occurs.
202
203 **/
204 EFI_STATUS
205 MnpSyncSendPacket (
206 IN MNP_SERVICE_DATA *MnpServiceData,
207 IN UINT8 *Packet,
208 IN UINT32 Length,
209 IN EFI_MANAGED_NETWORK_COMPLETION_TOKEN *Token
210 )
211 {
212 EFI_STATUS Status;
213 EFI_SIMPLE_NETWORK_PROTOCOL *Snp;
214 EFI_MANAGED_NETWORK_TRANSMIT_DATA *TxData;
215 UINT32 HeaderSize;
216 UINT8 *TxBuf;
217
218 Snp = MnpServiceData->Snp;
219 TxData = Token->Packet.TxData;
220
221 HeaderSize = Snp->Mode->MediaHeaderSize - TxData->HeaderLength;
222
223 //
224 // Start the timeout event.
225 //
226 Status = gBS->SetTimer (
227 MnpServiceData->TxTimeoutEvent,
228 TimerRelative,
229 MNP_TX_TIMEOUT_TIME
230 );
231 if (EFI_ERROR (Status)) {
232
233 goto SIGNAL_TOKEN;
234 }
235
236 for (;;) {
237 //
238 // Transmit the packet through SNP.
239 //
240 Status = Snp->Transmit (
241 Snp,
242 HeaderSize,
243 Length,
244 Packet,
245 TxData->SourceAddress,
246 TxData->DestinationAddress,
247 &TxData->ProtocolType
248 );
249 if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_READY)) {
250
251 Status = EFI_DEVICE_ERROR;
252 break;
253 }
254
255 //
256 // If Status is EFI_SUCCESS, the packet is put in the transmit queue.
257 // if Status is EFI_NOT_READY, the transmit engine of the network interface is busy.
258 // Both need to sync SNP.
259 //
260 TxBuf = NULL;
261 do {
262 //
263 // Get the recycled transmit buffer status.
264 //
265 Snp->GetStatus (Snp, NULL, &TxBuf);
266
267 if (!EFI_ERROR (gBS->CheckEvent (MnpServiceData->TxTimeoutEvent))) {
268
269 Status = EFI_TIMEOUT;
270 break;
271 }
272 } while (TxBuf == NULL);
273
274 if ((Status == EFI_SUCCESS) || (Status == EFI_TIMEOUT)) {
275
276 break;
277 } else {
278 //
279 // Status is EFI_NOT_READY. Restart the timer event and call Snp->Transmit again.
280 //
281 gBS->SetTimer (
282 MnpServiceData->TxTimeoutEvent,
283 TimerRelative,
284 MNP_TX_TIMEOUT_TIME
285 );
286 }
287 }
288
289 //
290 // Cancel the timer event.
291 //
292 gBS->SetTimer (MnpServiceData->TxTimeoutEvent, TimerCancel, 0);
293
294 SIGNAL_TOKEN:
295
296 Token->Status = Status;
297 gBS->SignalEvent (Token->Event);
298
299 return EFI_SUCCESS;
300 }
301
302
303 /**
304 Try to deliver the received packet to the instance.
305
306 @param Instance Pointer to the mnp instance context data.
307
308 @retval EFI_SUCCESS The received packet is delivered, or there is no
309 packet to deliver, or there is no available receive
310 token.
311 @retval EFI_OUT_OF_RESOURCES The deliver fails due to lack of memory resource.
312
313 **/
314 EFI_STATUS
315 MnpInstanceDeliverPacket (
316 IN MNP_INSTANCE_DATA *Instance
317 )
318 {
319 MNP_SERVICE_DATA *MnpServiceData;
320 MNP_RXDATA_WRAP *RxDataWrap;
321 NET_BUF *DupNbuf;
322 EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData;
323 EFI_SIMPLE_NETWORK_MODE *SnpMode;
324 EFI_MANAGED_NETWORK_COMPLETION_TOKEN *RxToken;
325
326 MnpServiceData = Instance->MnpServiceData;
327 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
328
329 if (NetMapIsEmpty (&Instance->RxTokenMap) || NetListIsEmpty (&Instance->RcvdPacketQueue)) {
330 //
331 // No pending received data or no available receive token, return.
332 //
333 return EFI_SUCCESS;
334 }
335
336 ASSERT (Instance->RcvdPacketQueueSize != 0);
337
338 RxDataWrap = NET_LIST_HEAD (&Instance->RcvdPacketQueue, MNP_RXDATA_WRAP, WrapEntry);
339 if (RxDataWrap->Nbuf->RefCnt > 2) {
340 //
341 // There are other instances share this Nbuf, duplicate to get a
342 // copy to allow the instance to do R/W operations.
343 //
344 DupNbuf = MnpAllocNbuf (MnpServiceData);
345 if (DupNbuf == NULL) {
346 MNP_DEBUG_WARN (("MnpDeliverPacket: Failed to allocate a free Nbuf.\n"));
347
348 return EFI_OUT_OF_RESOURCES;
349 }
350
351 //
352 // Duplicate the net buffer.
353 //
354 NetbufDuplicate (RxDataWrap->Nbuf, DupNbuf, 0);
355 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
356 RxDataWrap->Nbuf = DupNbuf;
357 }
358
359 //
360 // All resources are OK, remove the packet from the queue.
361 //
362 NetListRemoveHead (&Instance->RcvdPacketQueue);
363 Instance->RcvdPacketQueueSize--;
364
365 RxData = &RxDataWrap->RxData;
366 SnpMode = MnpServiceData->Snp->Mode;
367
368 //
369 // Set all the buffer pointers.
370 //
371 RxData->MediaHeader = NetbufGetByte (RxDataWrap->Nbuf, 0, NULL);
372 RxData->DestinationAddress = RxData->MediaHeader;
373 RxData->SourceAddress = (UINT8 *) RxData->MediaHeader + SnpMode->HwAddressSize;
374 RxData->PacketData = (UINT8 *) RxData->MediaHeader + SnpMode->MediaHeaderSize;
375
376 //
377 // Insert this RxDataWrap into the delivered queue.
378 //
379 NetListInsertTail (&Instance->RxDeliveredPacketQueue, &RxDataWrap->WrapEntry);
380
381 //
382 // Get the receive token from the RxTokenMap.
383 //
384 RxToken = NetMapRemoveHead (&Instance->RxTokenMap, NULL);
385
386 //
387 // Signal this token's event.
388 //
389 RxToken->Packet.RxData = &RxDataWrap->RxData;
390 RxToken->Status = EFI_SUCCESS;
391 gBS->SignalEvent (RxToken->Event);
392
393 return EFI_SUCCESS;
394 }
395
396
397 /**
398 Deliver the received packet for the instances belonging to the MnpServiceData.
399
400 @param MnpServiceData Pointer to the mnp service context data.
401
402 @return None.
403
404 **/
405 STATIC
406 VOID
407 MnpDeliverPacket (
408 IN MNP_SERVICE_DATA *MnpServiceData
409 )
410 {
411 NET_LIST_ENTRY *Entry;
412 MNP_INSTANCE_DATA *Instance;
413
414 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
415
416 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
417 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
418 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
419
420 //
421 // Try to deliver packet for this instance.
422 //
423 MnpInstanceDeliverPacket (Instance);
424 }
425 }
426
427
428 /**
429 Recycle the RxData and other resources used to hold and deliver the received
430 packet.
431
432 @param Event The event this notify function registered to.
433 @param Context Pointer to the context data registerd to the Event.
434
435 @return None.
436
437 **/
438 VOID
439 EFIAPI
440 MnpRecycleRxData (
441 IN EFI_EVENT Event,
442 IN VOID *Context
443 )
444 {
445 MNP_RXDATA_WRAP *RxDataWrap;
446 MNP_SERVICE_DATA *MnpServiceData;
447
448 ASSERT (Context != NULL);
449
450 RxDataWrap = (MNP_RXDATA_WRAP *) Context;
451 NET_CHECK_SIGNATURE (RxDataWrap->Instance, MNP_INSTANCE_DATA_SIGNATURE);
452
453 ASSERT (RxDataWrap->Nbuf != NULL);
454
455 MnpServiceData = RxDataWrap->Instance->MnpServiceData;
456 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
457
458 //
459 // Free this Nbuf.
460 //
461 MnpFreeNbuf (MnpServiceData, RxDataWrap->Nbuf);
462 RxDataWrap->Nbuf = NULL;
463
464 //
465 // Close the recycle event.
466 //
467 gBS->CloseEvent (RxDataWrap->RxData.RecycleEvent);
468
469 //
470 // Remove this Wrap entry from the list.
471 //
472 NetListRemoveEntry (&RxDataWrap->WrapEntry);
473
474 NetFreePool (RxDataWrap);
475 }
476
477
478 /**
479 Queue the received packet into instance's receive queue.
480
481 @param Instance Pointer to the mnp instance context data.
482 @param RxDataWrap Pointer to the Wrap structure containing the
483 received data and other information.
484
485 @return None.
486
487 **/
488 STATIC
489 VOID
490 MnpQueueRcvdPacket (
491 IN MNP_INSTANCE_DATA *Instance,
492 IN MNP_RXDATA_WRAP *RxDataWrap
493 )
494 {
495 MNP_RXDATA_WRAP *OldRxDataWrap;
496
497 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
498
499 //
500 // Check the queue size. If it exceeds the limit, drop one packet
501 // from the head.
502 //
503 if (Instance->RcvdPacketQueueSize == MNP_MAX_RCVD_PACKET_QUE_SIZE) {
504
505 MNP_DEBUG_WARN (("MnpQueueRcvdPacket: Drop one packet bcz queue size limit reached.\n"));
506
507 //
508 // Get the oldest packet.
509 //
510 OldRxDataWrap = NET_LIST_HEAD (
511 &Instance->RcvdPacketQueue,
512 MNP_RXDATA_WRAP,
513 WrapEntry
514 );
515
516 //
517 // Recycle this OldRxDataWrap, this entry will be removed by the callee.
518 //
519 MnpRecycleRxData (NULL, (VOID *) OldRxDataWrap);
520 Instance->RcvdPacketQueueSize--;
521 }
522
523 //
524 // Update the timeout tick using the configured parameter.
525 //
526 RxDataWrap->TimeoutTick = Instance->ConfigData.ReceivedQueueTimeoutValue;
527
528 //
529 // Insert this Wrap into the instance queue.
530 //
531 NetListInsertTail (&Instance->RcvdPacketQueue, &RxDataWrap->WrapEntry);
532 Instance->RcvdPacketQueueSize++;
533 }
534
535
536 /**
537 Match the received packet with the instance receive filters.
538
539 @param Instance Pointer to the mnp instance context data.
540 @param RxData Pointer to the EFI_MANAGED_NETWORK_RECEIVE_DATA.
541 @param GroupAddress Pointer to the GroupAddress, the GroupAddress is
542 non-NULL and it contains the destination multicast
543 mac address of the received packet if the packet
544 destinated to a multicast mac address.
545 @param PktAttr The received packets attribute.
546
547 @return The received packet matches the instance's receive filters or not.
548
549 **/
550 STATIC
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 NET_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 if (ConfigData->EnablePromiscuousReceive) {
568 //
569 // Always match if this instance is configured to be promiscuous.
570 //
571 return TRUE;
572 }
573 //
574 // Check the protocol type.
575 //
576 if ((ConfigData->ProtocolTypeFilter != 0) && (ConfigData->ProtocolTypeFilter != RxData->ProtocolType)) {
577 return FALSE;
578 }
579
580 //
581 // The protocol type is matched, check receive filter, include unicast and broadcast.
582 //
583 if ((Instance->ReceiveFilter & PktAttr) != 0) {
584 return TRUE;
585 }
586
587 //
588 // Check multicast addresses.
589 //
590 if (ConfigData->EnableMulticastReceive && RxData->MulticastFlag) {
591
592 ASSERT (GroupAddress != NULL);
593
594 NET_LIST_FOR_EACH (Entry, &Instance->GroupCtrlBlkList) {
595
596 GroupCtrlBlk = NET_LIST_USER_STRUCT (Entry, MNP_GROUP_CONTROL_BLOCK, CtrlBlkEntry);
597 if (GroupCtrlBlk->GroupAddress == GroupAddress) {
598 //
599 // The instance is configured to receiveing packets destinated to this
600 // multicast address.
601 //
602 return TRUE;
603 }
604 }
605 }
606
607 //
608 // No match.
609 //
610 return FALSE;
611 }
612
613
614 /**
615 Analyse the received packets.
616
617 @param MnpServiceData Pointer to the mnp service context data.
618 @param Nbuf Pointer to the net buffer holding the received
619 packet.
620 @param RxData Pointer to the buffer used to save the analysed
621 result in EFI_MANAGED_NETWORK_RECEIVE_DATA.
622 @param GroupAddress Pointer to pointer to a MNP_GROUP_ADDRESS used to
623 pass out the address of the multicast address the
624 received packet destinated to.
625 @param PktAttr Pointer to the buffer used to save the analysed
626 packet attribute.
627
628 @return None.
629
630 **/
631 STATIC
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 NET_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 NetZeroMem (&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 STATIC
729 MNP_RXDATA_WRAP *
730 MnpWrapRxData (
731 IN MNP_INSTANCE_DATA *Instance,
732 IN EFI_MANAGED_NETWORK_RECEIVE_DATA *RxData
733 )
734 {
735 EFI_STATUS Status;
736 MNP_RXDATA_WRAP *RxDataWrap;
737
738 //
739 // Allocate memory.
740 //
741 RxDataWrap = NetAllocatePool (sizeof (MNP_RXDATA_WRAP));
742 if (RxDataWrap == NULL) {
743 MNP_DEBUG_ERROR (("MnpDispatchPacket: Failed to allocate a MNP_RXDATA_WRAP.\n"));
744 return NULL;
745 }
746
747 RxDataWrap->Instance = Instance;
748
749 //
750 // Fill the RxData in RxDataWrap,
751 //
752 RxDataWrap->RxData = *RxData;
753
754 //
755 // Create the recycle event.
756 //
757 Status = gBS->CreateEvent (
758 EVT_NOTIFY_SIGNAL,
759 NET_TPL_RECYCLE,
760 MnpRecycleRxData,
761 RxDataWrap,
762 &RxDataWrap->RxData.RecycleEvent
763 );
764 if (EFI_ERROR (Status)) {
765
766 MNP_DEBUG_ERROR (("MnpDispatchPacket: gBS->CreateEvent failed, %r.\n", Status));
767 NetFreePool (RxDataWrap);
768 return NULL;
769 }
770
771 return RxDataWrap;
772 }
773
774
775 /**
776 Enqueue the received the packets to the instances belonging to the
777 MnpServiceData.
778
779 @param MnpServiceData Pointer to the mnp service context data.
780 @param Nbuf Pointer to the net buffer representing the received
781 packet.
782
783 @return None.
784
785 **/
786 STATIC
787 VOID
788 MnpEnqueuePacket (
789 IN MNP_SERVICE_DATA *MnpServiceData,
790 IN NET_BUF *Nbuf
791 )
792 {
793 NET_LIST_ENTRY *Entry;
794 MNP_INSTANCE_DATA *Instance;
795 EFI_MANAGED_NETWORK_RECEIVE_DATA RxData;
796 UINT8 PktAttr;
797 MNP_GROUP_ADDRESS *GroupAddress;
798 MNP_RXDATA_WRAP *RxDataWrap;
799
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 (NetListIsEmpty (&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 MNP_DEBUG_ERROR (("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 MNP_DEBUG_WARN (
939 ("MnpReceivePacket: Size error, HL:TL = %d:%d.\n",
940 HeaderSize,
941 BufLen)
942 );
943 return EFI_DEVICE_ERROR;
944 }
945
946 Trimmed = 0;
947 if (Nbuf->TotalSize != BufLen) {
948 //
949 // Trim the packet from tail.
950 //
951 Trimmed = NetbufTrim (Nbuf, Nbuf->TotalSize - (UINT32) BufLen, NET_BUF_TAIL);
952 ASSERT (Nbuf->TotalSize == BufLen);
953 }
954
955 //
956 // Enqueue the packet to the matched instances.
957 //
958 MnpEnqueuePacket (MnpServiceData, Nbuf);
959
960 if (Nbuf->RefCnt > 2) {
961 //
962 // RefCnt > 2 indicates there is at least one receiver of this packet.
963 // Free the current RxNbufCache and allocate a new one.
964 //
965 MnpFreeNbuf (MnpServiceData, Nbuf);
966
967 Nbuf = MnpAllocNbuf (MnpServiceData);
968 MnpServiceData->RxNbufCache = Nbuf;
969 if (Nbuf == NULL) {
970 MNP_DEBUG_ERROR (("MnpReceivePacket: Alloc packet for receiving cache failed.\n"));
971 return EFI_DEVICE_ERROR;
972 }
973
974 NetbufAllocSpace (Nbuf, MnpServiceData->BufferLength, NET_BUF_TAIL);
975 } else {
976 //
977 // No receiver for this packet.
978 //
979 NetbufAllocSpace (Nbuf, Trimmed, NET_BUF_TAIL);
980 goto EXIT;
981 }
982 //
983 // Deliver the queued packets.
984 //
985 MnpDeliverPacket (MnpServiceData);
986
987 EXIT:
988
989 ASSERT (Nbuf->TotalSize == MnpServiceData->BufferLength);
990
991 return Status;
992 }
993
994
995 /**
996 Remove the received packets if timeout occurs.
997
998 @param Event The event this notify function registered to.
999 @param Context Pointer to the context data registered to the
1000 event.
1001
1002 @return None.
1003
1004 **/
1005 VOID
1006 EFIAPI
1007 MnpCheckPacketTimeout (
1008 IN EFI_EVENT Event,
1009 IN VOID *Context
1010 )
1011 {
1012 MNP_SERVICE_DATA *MnpServiceData;
1013 NET_LIST_ENTRY *Entry;
1014 NET_LIST_ENTRY *RxEntry;
1015 NET_LIST_ENTRY *NextEntry;
1016 MNP_INSTANCE_DATA *Instance;
1017 MNP_RXDATA_WRAP *RxDataWrap;
1018 EFI_TPL OldTpl;
1019
1020 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1021 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1022
1023 NET_LIST_FOR_EACH (Entry, &MnpServiceData->ChildrenList) {
1024
1025 Instance = NET_LIST_USER_STRUCT (Entry, MNP_INSTANCE_DATA, InstEntry);
1026 NET_CHECK_SIGNATURE (Instance, MNP_INSTANCE_DATA_SIGNATURE);
1027
1028 if (!Instance->Configured || (Instance->ConfigData.ReceivedQueueTimeoutValue == 0)) {
1029 //
1030 // This instance is not configured or there is no receive time out,
1031 // just skip to the next instance.
1032 //
1033 continue;
1034 }
1035
1036 OldTpl = NET_RAISE_TPL (NET_TPL_RECYCLE);
1037
1038 NET_LIST_FOR_EACH_SAFE (RxEntry, NextEntry, &Instance->RcvdPacketQueue) {
1039
1040 RxDataWrap = NET_LIST_USER_STRUCT (RxEntry, MNP_RXDATA_WRAP, WrapEntry);
1041
1042 if (RxDataWrap->TimeoutTick >= MNP_TIMEOUT_CHECK_INTERVAL) {
1043
1044 RxDataWrap->TimeoutTick -= MNP_TIMEOUT_CHECK_INTERVAL;
1045 } else {
1046 //
1047 // Drop the timeout packet.
1048 //
1049 MNP_DEBUG_WARN (("MnpCheckPacketTimeout: Received packet timeout.\n"));
1050 MnpRecycleRxData (NULL, RxDataWrap);
1051 Instance->RcvdPacketQueueSize--;
1052 }
1053 }
1054
1055 NET_RESTORE_TPL (OldTpl);
1056 }
1057 }
1058
1059
1060 /**
1061 Poll to receive the packets from Snp. This function is either called by upperlayer
1062 protocols/applications or the system poll timer notify mechanism.
1063
1064 @param Event The event this notify function registered to.
1065 @param Context Pointer to the context data registered to the
1066 event.
1067
1068 @return None.
1069
1070 **/
1071 VOID
1072 EFIAPI
1073 MnpSystemPoll (
1074 IN EFI_EVENT Event,
1075 IN VOID *Context
1076 )
1077 {
1078 MNP_SERVICE_DATA *MnpServiceData;
1079
1080 MnpServiceData = (MNP_SERVICE_DATA *) Context;
1081 NET_CHECK_SIGNATURE (MnpServiceData, MNP_SERVICE_DATA_SIGNATURE);
1082
1083 //
1084 // Try to receive packets from Snp.
1085 //
1086 MnpReceivePacket (MnpServiceData);
1087 }