-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathndisapi_fastio.go
185 lines (150 loc) · 5.07 KB
/
ndisapi_fastio.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//go:build windows
package ndisapi
import (
"unsafe"
)
const UnsortedMaximumBlockNum = 16
const UnsortedMaximumPacketBlock = 512
const FastIOMaximumPacketBlock = 2048 * 3
// FastIOWriteUnion defines the union type for fast I/O write.
type FastIOWriteUnion struct {
union uint32
}
// Split returns the split field of a union type containing respectively NumberOfPackets and WriteInProgressFlag.
func (h *FastIOWriteUnion) GetNumberOfPackets() uint16 {
return uint16(h.union & 0xFFFF)
}
func (h *FastIOWriteUnion) GetWriteInProgressFlag() uint16 {
return uint16((h.union >> 16) & 0xFFFF)
}
func (h *FastIOWriteUnion) SetNumberOfPackets(value uint16) {
h.union = (h.union & 0xFFFF0000) | uint32(value)
}
func (h *FastIOWriteUnion) SetWriteInProgressFlag(value uint16) {
h.union = (h.union & 0x0000FFFF) | (uint32(value) << 16)
}
// Join combines the NumberOfPackets and WriteInProgressFlag into a single uint32 value.
func (h *FastIOWriteUnion) GetJoin() *uint32 {
return &h.union
}
// SetJoin sets the FastIOWriteUnion fields from a single uint32 value.
func (h *FastIOWriteUnion) SetJoin(join uint32) {
h.union = join
}
// FastIOSectionHeader defines the header for a fast I/O section.
type FastIOSectionHeader struct {
FastIOWriteUnion FastIOWriteUnion
ReadInProgressFlag uint32
}
// FastIOSection represents a section containing intermediate buffers for fast I/O.
type InitializeFastIOSection struct {
FastIOHeader FastIOSectionHeader
FastIOPackets [AnySize]IntermediateBuffer // Assumes IntermediateBuffer is defined elsewhere
}
type FastIOSection struct {
FastIOHeader FastIOSectionHeader
FastIOPackets [FastIOMaximumPacketBlock]IntermediateBuffer // Assumes IntermediateBuffer is defined elsewhere
}
// InitializeFastIOParams defines parameters for initializing a fast I/O section.
type InitializeFastIOParams struct {
Header *InitializeFastIOSection
DataSize uint32
}
// UnsortedReadSendRequest represents a request for unsorted read/send packets
type UnsortedReadSendRequest struct {
Packets []*IntermediateBuffer
PacketsNum uint32
}
// InitializeFastIo initializes the Fast I/O shared memory section.
func (a *NdisApi) InitializeFastIo(fastIo *InitializeFastIOSection, size uint32) bool {
if size < uint32(unsafe.Sizeof(InitializeFastIOSection{})) {
return false
}
params := InitializeFastIOParams{Header: fastIo, DataSize: size}
err := a.DeviceIoControl(
IOCTL_NDISRD_INITIALIZE_FAST_IO,
unsafe.Pointer(¶ms),
uint32(unsafe.Sizeof(params)),
nil,
0,
&a.bytesReturned,
nil,
)
return err == nil
}
// AddSecondaryFastIo adds a secondary Fast I/O shared memory section.
func (a *NdisApi) AddSecondaryFastIo(fastIo *InitializeFastIOSection, size uint32) bool {
if size < uint32(unsafe.Sizeof(InitializeFastIOSection{})) {
return false
}
params := InitializeFastIOParams{Header: fastIo, DataSize: size}
err := a.DeviceIoControl(
IOCTL_NDISRD_ADD_SECOND_FAST_IO_SECTION,
unsafe.Pointer(¶ms),
uint32(unsafe.Sizeof(params)),
nil,
0,
&a.bytesReturned,
nil,
)
return err == nil
}
// ReadPacketsUnsorted reads a bunch of packets from the driver packet queues without sorting by network adapter.
func (a *NdisApi) ReadPacketsUnsorted(packets []*IntermediateBuffer, packetsNum uint32, packetsSuccess *uint32) bool {
request := UnsortedReadSendRequest{
Packets: make([]*IntermediateBuffer, packetsNum),
PacketsNum: packetsNum,
}
copy(request.Packets, packets[:packetsNum])
err := a.DeviceIoControl(
IOCTL_NDISRD_READ_PACKETS_UNSORTED,
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
&a.bytesReturned,
nil,
)
len := uint32(len(request.Packets))
copy(packets[0:len], request.Packets[0:len])
*packetsSuccess = len
return err == nil
}
// SendPacketsToAdaptersUnsorted sends a bunch of packets to the network adapters.
func (a *NdisApi) SendPacketsToAdaptersUnsorted(packets []*IntermediateBuffer, packetsNum uint32, packetSuccess *uint32) bool {
request := UnsortedReadSendRequest{
Packets: make([]*IntermediateBuffer, packetsNum),
PacketsNum: packetsNum,
}
copy(request.Packets, packets[:packetsNum])
err := a.DeviceIoControl(
IOCTL_NDISRD_SEND_PACKET_TO_ADAPTER_UNSORTED,
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
&a.bytesReturned,
nil,
)
*packetSuccess = request.PacketsNum
return err == nil
}
// SendPacketsToMstcpUnsorted indicates a bunch of packets to the MSTCP (and other upper layer network protocols).
func (a *NdisApi) SendPacketsToMstcpUnsorted(packets []*IntermediateBuffer, packetsNum uint32, packetSuccess *uint32) bool {
request := UnsortedReadSendRequest{
Packets: make([]*IntermediateBuffer, packetsNum),
PacketsNum: packetsNum,
}
copy(request.Packets, packets[:packetsNum])
err := a.DeviceIoControl(
IOCTL_NDISRD_SEND_PACKET_TO_MSTCP_UNSORTED,
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
unsafe.Pointer(&request),
uint32(unsafe.Sizeof(request)),
&a.bytesReturned,
nil,
)
*packetSuccess = request.PacketsNum
return err == nil
}