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