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