From 85141a231c6bcc0f39195c506d2147e834c94b8a Mon Sep 17 00:00:00 2001 From: basil00 Date: Sun, 3 Mar 2019 07:56:44 +0800 Subject: [PATCH] Fix passthru performance bugs. Using large fixed-size buffers adds significant overheads. Instead, scale the buffer to the batch size. --- examples/passthru/passthru.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/examples/passthru/passthru.c b/examples/passthru/passthru.c index 4307c73..ecd39de 100644 --- a/examples/passthru/passthru.c +++ b/examples/passthru/passthru.c @@ -47,9 +47,7 @@ #include "windivert.h" -#define MAXBUF 400000 -#define MAXBATCH 0xFF - +#define MTU 1500 static int batch = 1; static DWORD passthru(LPVOID arg); @@ -78,7 +76,7 @@ int __cdecl main(int argc, char **argv) { batch = atoi(argv[3]); } - if (batch <= 0 || batch > MAXBATCH) + if (batch <= 0 || batch > WINDIVERT_BATCH_MAX) { fprintf(stderr, "error: invalid batch size\n"); exit(EXIT_FAILURE); @@ -110,7 +108,7 @@ int __cdecl main(int argc, char **argv) (LPVOID)handle, 0, NULL); if (thread == NULL) { - fprintf(stderr, "error: failed to start passthru thread (%u)\n", + fprintf(stderr, "error: failed to start passthru thread (%d)\n", GetLastError()); exit(EXIT_FAILURE); } @@ -125,17 +123,27 @@ int __cdecl main(int argc, char **argv) // Passthru thread. static DWORD passthru(LPVOID arg) { - UINT8 packet[MAXBUF]; + UINT8 *packet; UINT packet_len, addr_len; - WINDIVERT_ADDRESS addr[MAXBATCH]; + WINDIVERT_ADDRESS *addr; HANDLE handle = (HANDLE)arg; + packet = (UINT8 *)malloc(batch * MTU); + addr = (WINDIVERT_ADDRESS *)malloc(batch * sizeof(WINDIVERT_ADDRESS)); + if (packet == NULL || addr == NULL) + { + fprintf(stderr, "error: failed to allocate buffer (%d)\n", + GetLastError()); + exit(EXIT_FAILURE); + } + // Main loop: while (TRUE) { // Read a matching packet. - addr_len = batch * sizeof(WINDIVERT_ADDRESS); - if (!WinDivertRecvEx(handle, packet, sizeof(packet), &packet_len, 0, + packet_len = batch * MTU; + addr_len = batch * sizeof(WINDIVERT_ADDRESS); + if (!WinDivertRecvEx(handle, packet, packet_len, &packet_len, 0, addr, &addr_len, NULL)) { fprintf(stderr, "warning: failed to read packet (%d)\n",