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