Hi
I wrote WFP driver which redirects selected traffic to my userland application. My overall solution is working fine except reading packet from the driver takes high CPU.
High-level code is: Driver continuously writes the packets in packet queue and userland code releases one IRP at a time, driver writes a packet into the IRP and mark it complete.
My reader userland code is below, this code takes up to 100% CPU depends upon the 100mbps throughput, whereas I do not see any such CPU utilization in a standard TCP system socket read calls. I wonder if this is the best practice of reading packets from the driver at high-speed rate (100 mbps+). Please guide me and let me know if anything is missing in this query.
void PacketSinkThreadFunc()
{
HANDLE deviceHandle = CreateFile(
DEVICE_NAME,
…
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE
);
uint8_t outBuf[MAX_PKT_SIZE];
HANDLE hIoEvent = CreateEvent(NULL, FALSE, TRUE, “”);
OVERLAPPED ol;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.hEvent = hIoEvent;
HANDLE events[1];
events[0] = hIoEvent;
do {
memset(outBuf, 0, sizeof(outBuf));
if (!DeviceIoControl(m_DeviceHandle,
MY_CONTROL_CODE(FUNC_MY_ASYNC_EVENT, METHOD_OUT_DIRECT),
NULL,
0,
outBuf,
sizeof(outBuf),
&returnedCount,
&ol)
) {
if (GetLastError() != ERROR_IO_PENDING) {
goto exit;
}
}
ret = WaitForMultipleObjects(_countof(events), events, FALSE, INFINITE);
switch(ret)
{
case WAIT_OBJECT_0:
// data avialble
bytesRead = 0;
if (!GetOverlappedResult(m_DeviceHandle,
&ol,
&bytesRead,
FALSE)
) {
goto exit;
}
if (bytesRead > 0) {
// data is in outbuf
}
break;
} //switch
} while(true);
exit:
printf(“PacketSinkThread exiting”);
}
Thanks