forked from OpenVPN/ovpn-dco-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrxqueue.cpp
116 lines (96 loc) · 4.04 KB
/
rxqueue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* ovpn-dco-win OpenVPN protocol accelerator for Windows
*
* Copyright (C) 2020-2021 OpenVPN Inc <[email protected]>
*
* Author: Lev Stipakov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ntddk.h>
#include <wdf.h>
#include <netadaptercx.h>
#include <net/virtualaddress.h>
#include "driver.h"
#include "driverhelper\buffers.h"
#include "rxqueue.h"
#include "netringiterator.h"
EVT_PACKET_QUEUE_ADVANCE OvpnEvtRxQueueAdvance;
_Use_decl_annotations_
VOID
OvpnEvtRxQueueAdvance(NETPACKETQUEUE netPacketQueue)
{
POVPN_RXQUEUE queue = OvpnGetRxQueueContext(netPacketQueue);
OVPN_DEVICE* device = OvpnGetDeviceContext(queue->Adapter->WdfDevice);
OVPN_BUFFER_QUEUE bufferQueue = device->DataRxBufferQueue;
NET_RING_FRAGMENT_ITERATOR fi = NetRingGetAllFragments(queue->Rings);
NET_RING_PACKET_ITERATOR pi = NetRingGetAllPackets(queue->Rings);
while (NetFragmentIteratorHasAny(&fi)) {
OVPN_RX_BUFFER* buffer;
// nothing has arrived and decrypted yet?
if (!NT_SUCCESS(OvpnBufferQueueDequeue(bufferQueue, &buffer))) {
break;
}
NET_FRAGMENT* fragment = NetFragmentIteratorGetFragment(&fi);
fragment->ValidLength = buffer->Len;
fragment->Offset = 0;
NET_FRAGMENT_VIRTUAL_ADDRESS* virtualAddr = NetExtensionGetFragmentVirtualAddress(&queue->VirtualAddressExtension, NetFragmentIteratorGetIndex(&fi));
RtlCopyMemory(virtualAddr->VirtualAddress, buffer->Head, buffer->Len);
InterlockedExchangeAddNoFence64(&device->Stats.TunBytesReceived, buffer->Len);
NET_PACKET* packet = NetPacketIteratorGetPacket(&pi);
packet->FragmentIndex = NetFragmentIteratorGetIndex(&fi);
packet->FragmentCount = 1;
packet->Layout = {};
NetFragmentIteratorAdvance(&fi);
NetPacketIteratorAdvance(&pi);
OvpnBufferQueueReuse(bufferQueue, buffer);
InterlockedIncrementNoFence(&device->Stats.ReceivedDataPackets);
}
NetFragmentIteratorSet(&fi);
NetPacketIteratorSet(&pi);
}
_Use_decl_annotations_
VOID
OvpnEvtRxQueueSetNotificationEnabled(NETPACKETQUEUE queue, BOOLEAN notificationEnabled)
{
POVPN_RXQUEUE rxQueue = OvpnGetRxQueueContext(queue);
InterlockedExchangeNoFence(&rxQueue->NotificationEnabled, notificationEnabled);
}
_Use_decl_annotations_
VOID
OvpnEvtRxQueueCancel(NETPACKETQUEUE netPacketQueue)
{
POVPN_RXQUEUE queue = OvpnGetRxQueueContext(netPacketQueue);
// mark all packets as "ignore"
NET_RING_PACKET_ITERATOR pi = NetRingGetAllPackets(queue->Rings);
while (NetPacketIteratorHasAny(&pi)) {
NetPacketIteratorGetPacket(&pi)->Ignore = 1;
NetPacketIteratorAdvance(&pi);
}
NetPacketIteratorSet(&pi);
// return all fragments' ownership back to netadapter
NET_RING* fragmentRing = NetRingCollectionGetFragmentRing(queue->Rings);
fragmentRing->BeginIndex = fragmentRing->EndIndex;
}
_Use_decl_annotations_
VOID
OvpnRxQueueInitialize(NETPACKETQUEUE netPacketQueue, POVPN_ADAPTER adapter)
{
POVPN_RXQUEUE queue = OvpnGetRxQueueContext(netPacketQueue);
queue->Adapter = adapter;
queue->Rings = NetRxQueueGetRingCollection(netPacketQueue);
NET_EXTENSION_QUERY extension;
NET_EXTENSION_QUERY_INIT(&extension, NET_FRAGMENT_EXTENSION_VIRTUAL_ADDRESS_NAME, NET_FRAGMENT_EXTENSION_VIRTUAL_ADDRESS_VERSION_1, NetExtensionTypeFragment);
NetRxQueueGetExtension(netPacketQueue, &extension, &queue->VirtualAddressExtension);
}