From b2182e0e9059c97b7951ea45c6f2aba8be222109 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 3 Jul 2019 08:44:17 +0000 Subject: [PATCH] Reorder ReturnNBL and DispatchWrite Signed-off-by: Jason A. Donenfeld --- wintun.c | 72 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/wintun.c b/wintun.c index 01b377a..3dadffb 100644 --- a/wintun.c +++ b/wintun.c @@ -753,6 +753,42 @@ cleanup_CompleteRequest: #define IRP_REFCOUNT(irp) ((volatile LONG *)&(irp)->Tail.Overlay.DriverContext[0]) #define NET_BUFFER_LIST_IRP(nbl) (NET_BUFFER_LIST_MINIPORT_RESERVED(nbl)[0]) +static MINIPORT_RETURN_NET_BUFFER_LISTS TunReturnNetBufferLists; +_Use_decl_annotations_ +static void +TunReturnNetBufferLists(NDIS_HANDLE MiniportAdapterContext, PNET_BUFFER_LIST NetBufferLists, ULONG ReturnFlags) +{ + TUN_CTX *Ctx = (TUN_CTX *)MiniportAdapterContext; + + LONG64 StatSize = 0, StatPacketsOk = 0, StatPacketsError = 0; + for (NET_BUFFER_LIST *Nbl = NetBufferLists, *NextNbl; Nbl; Nbl = NextNbl) + { + NextNbl = NET_BUFFER_LIST_NEXT_NBL(Nbl); + NET_BUFFER_LIST_NEXT_NBL(Nbl) = NULL; + + IRP *Irp = NET_BUFFER_LIST_IRP(Nbl); + if (NT_SUCCESS(NET_BUFFER_LIST_STATUS(Nbl))) + { + StatSize += NET_BUFFER_LIST_FIRST_NB(Nbl)->DataLength; + StatPacketsOk++; + } + else + StatPacketsError++; + + NdisFreeNetBufferList(Nbl); + TunCompletePause(Ctx, TRUE); + + ASSERT(InterlockedGet(IRP_REFCOUNT(Irp)) > 0); + if (InterlockedDecrement(IRP_REFCOUNT(Irp)) <= 0) + TunCompleteRequest(Ctx, Irp, STATUS_SUCCESS, IO_NETWORK_INCREMENT); + } + + InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInOctets, StatSize); + InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInUcastOctets, StatSize); + InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInUcastPkts, StatPacketsOk); + InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifInErrors, StatPacketsError); +} + _IRQL_requires_max_(APC_LEVEL) _Must_inspect_result_ static NTSTATUS @@ -909,42 +945,6 @@ cleanup_CompleteRequest: return Status; } -static MINIPORT_RETURN_NET_BUFFER_LISTS TunReturnNetBufferLists; -_Use_decl_annotations_ -static void -TunReturnNetBufferLists(NDIS_HANDLE MiniportAdapterContext, PNET_BUFFER_LIST NetBufferLists, ULONG ReturnFlags) -{ - TUN_CTX *Ctx = (TUN_CTX *)MiniportAdapterContext; - - LONG64 StatSize = 0, StatPacketsOk = 0, StatPacketsError = 0; - for (NET_BUFFER_LIST *Nbl = NetBufferLists, *NextNbl; Nbl; Nbl = NextNbl) - { - NextNbl = NET_BUFFER_LIST_NEXT_NBL(Nbl); - NET_BUFFER_LIST_NEXT_NBL(Nbl) = NULL; - - IRP *Irp = NET_BUFFER_LIST_IRP(Nbl); - if (NT_SUCCESS(NET_BUFFER_LIST_STATUS(Nbl))) - { - StatSize += NET_BUFFER_LIST_FIRST_NB(Nbl)->DataLength; - StatPacketsOk++; - } - else - StatPacketsError++; - - NdisFreeNetBufferList(Nbl); - TunCompletePause(Ctx, TRUE); - - ASSERT(InterlockedGet(IRP_REFCOUNT(Irp)) > 0); - if (InterlockedDecrement(IRP_REFCOUNT(Irp)) <= 0) - TunCompleteRequest(Ctx, Irp, STATUS_SUCCESS, IO_NETWORK_INCREMENT); - } - - InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInOctets, StatSize); - InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInUcastOctets, StatSize); - InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifHCInUcastPkts, StatPacketsOk); - InterlockedAdd64((LONG64 *)&Ctx->Statistics.ifInErrors, StatPacketsError); -} - _IRQL_requires_max_(APC_LEVEL) _Must_inspect_result_ static NTSTATUS