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