Skip to content

Commit 85141a2

Browse files
committed
Fix passthru performance bugs.
Using large fixed-size buffers adds significant overheads. Instead, scale the buffer to the batch size.
1 parent 3465266 commit 85141a2

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

examples/passthru/passthru.c

+17-9
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@
4747

4848
#include "windivert.h"
4949

50-
#define MAXBUF 400000
51-
#define MAXBATCH 0xFF
52-
50+
#define MTU 1500
5351
static int batch = 1;
5452

5553
static DWORD passthru(LPVOID arg);
@@ -78,7 +76,7 @@ int __cdecl main(int argc, char **argv)
7876
{
7977
batch = atoi(argv[3]);
8078
}
81-
if (batch <= 0 || batch > MAXBATCH)
79+
if (batch <= 0 || batch > WINDIVERT_BATCH_MAX)
8280
{
8381
fprintf(stderr, "error: invalid batch size\n");
8482
exit(EXIT_FAILURE);
@@ -110,7 +108,7 @@ int __cdecl main(int argc, char **argv)
110108
(LPVOID)handle, 0, NULL);
111109
if (thread == NULL)
112110
{
113-
fprintf(stderr, "error: failed to start passthru thread (%u)\n",
111+
fprintf(stderr, "error: failed to start passthru thread (%d)\n",
114112
GetLastError());
115113
exit(EXIT_FAILURE);
116114
}
@@ -125,17 +123,27 @@ int __cdecl main(int argc, char **argv)
125123
// Passthru thread.
126124
static DWORD passthru(LPVOID arg)
127125
{
128-
UINT8 packet[MAXBUF];
126+
UINT8 *packet;
129127
UINT packet_len, addr_len;
130-
WINDIVERT_ADDRESS addr[MAXBATCH];
128+
WINDIVERT_ADDRESS *addr;
131129
HANDLE handle = (HANDLE)arg;
132130

131+
packet = (UINT8 *)malloc(batch * MTU);
132+
addr = (WINDIVERT_ADDRESS *)malloc(batch * sizeof(WINDIVERT_ADDRESS));
133+
if (packet == NULL || addr == NULL)
134+
{
135+
fprintf(stderr, "error: failed to allocate buffer (%d)\n",
136+
GetLastError());
137+
exit(EXIT_FAILURE);
138+
}
139+
133140
// Main loop:
134141
while (TRUE)
135142
{
136143
// Read a matching packet.
137-
addr_len = batch * sizeof(WINDIVERT_ADDRESS);
138-
if (!WinDivertRecvEx(handle, packet, sizeof(packet), &packet_len, 0,
144+
packet_len = batch * MTU;
145+
addr_len = batch * sizeof(WINDIVERT_ADDRESS);
146+
if (!WinDivertRecvEx(handle, packet, packet_len, &packet_len, 0,
139147
addr, &addr_len, NULL))
140148
{
141149
fprintf(stderr, "warning: failed to read packet (%d)\n",

0 commit comments

Comments
 (0)