Reduced network load on Windows networks due to Spinlock

I am writing a Windows filtering platform kernel mode driver, the purpose of the driver is to capture all traffic at a certain level and transfer this traffic back to user mode so that it can be further analyzed. The driver never needs to block any traffic, classifyOut is always set to FWP_ACTION_CONTINUE.

My Classify function uses the following code for the incoming packet queue.

classifyOut->actionType = FWP_ACTION_CONTINUE;

do
{
    if ((classifyOut->rights & FWPS_RIGHT_ACTION_WRITE) == 0)
    {
        break;
    }

    if (layerData != NULL)
    {
        PNET_BUFFER_LIST netBufferList = (PNET_BUFFER_LIST) layerData;
        PNET_BUFFER netBuffer = NET_BUFFER_LIST_FIRST_NB(netBufferList);

        if (packetQueueSize >= 2048)
        {
            ExInterlockedRemoveHeadList(&packetQueue, &packetQueueLock);
            packetQueueSize--;
        }

        ULONG netBufferSize = NET_BUFFER_DATA_LENGTH(netBuffer);
        PACKET_ITEM* allocatedPacket = InitalizePacketItem(
            netBuffer,
            netBufferSize
        );

        if (allocatedPacket == NULL)
        {
            classifyOut->actionType = FWP_ACTION_BLOCK;
            classifyOut->rights &= ~FWPS_RIGHT_ACTION_WRITE;
            break;
        }

        ExInterlockedInsertTailList(
            &packetQueue,
            &allocatedPacket->listEntry,
            &packetQueueLock
        );
        allocatedPacket = NULL;
        packetQueueSize++;
    }
} while (FALSE);

The structure is PACKET_ITEMdefined as follows

typedef struct _PACKET_ITEM {
    LIST_ENTRY listEntry;
    PVOID data;
    ULONG dataLen;
} PACKET_ITEM;

. IOCTL.

status = WdfRequestRetrieveOutputBuffer(request, 0, &buffer, &bufferSize);
if (!NT_SUCCESS(status))
{
    break;
}

PLIST_ENTRY listEntry = ExInterlockedRemoveHeadList(&packetQueue, &packetQueueLock);
if (listEntry == NULL)
{
    break;
}

PACKET_ITEM* packetItem = CONTAINING_RECORD(
    listEntry,
    struct _PACKET_ITEM,
    listEntry
);

RtlCopyMemory(
    buffer,
    packetItem->data,
    packetItem->dataLen);

status = STATUS_SUCCESS;
WdfRequestCompleteWithInformation(
    request,
    status,
    packetItem->dataLen
);

FreePacketItem(packetItem);

, , , - - -, .

, , , .

  • , - ?
    • classifyOut->actionType , . , ?
    • -, ?
  • ,
    • ?
+4

Source: https://habr.com/ru/post/1696062/


All Articles