forked from dshulyak/uring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
222 lines (206 loc) · 4.04 KB
/
register.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package uring
import (
"unsafe"
"golang.org/x/sys/unix"
)
const (
IORING_REGISTER_BUFFERS uintptr = iota
IORING_UNREGISTER_BUFFERS
IORING_REGISTER_FILES
IORING_UNREGISTER_FILES
IORING_REGISTER_EVENTFD
IORING_UNREGISTER_EVENTFD
IORING_REGISTER_FILES_UPDATE
IORING_REGISTER_EVENTFD_ASYNC
IORING_REGISTER_PROBE
IORING_REGISTER_PERSONALITY
IORING_UNREGISTER_PERSONALITY
)
const (
// IO_URING_OP_SUPPORTED ...
IO_URING_OP_SUPPORTED uint16 = 1 << 0
)
const (
// probeOpsSize is uintptr so that it can be passed to unix without casting
probeOpsSize = uintptr(IORING_OP_LAST) + 1
)
// Probe ...
type Probe struct {
LastOp uint8
OpsLen uint8
resv uint16
resv2 [3]uint32
Ops [probeOpsSize]ProbeOp
}
// IsSupported returns true if operation is supported.
func (p Probe) IsSupported(op uint8) bool {
for i := uint8(0); i < p.OpsLen; i++ {
if p.Ops[i].Op != op {
continue
}
return p.Ops[i].Flags&IO_URING_OP_SUPPORTED > 0
}
return false
}
// ProbeOp ...
type ProbeOp struct {
Op uint8
resv uint8
Flags uint16
resv2 uint32
}
// RegisterProbe ...
func (r *Ring) RegisterProbe(probe *Probe) error {
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_REGISTER_PROBE,
uintptr(unsafe.Pointer(probe)),
probeOpsSize, 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// RegisterFiles ...
func (r *Ring) RegisterFiles(fds []int32) error {
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_REGISTER_FILES,
uintptr(unsafe.Pointer(&fds[0])),
uintptr(len(fds)), 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// UnregisterFiles ...
func (r *Ring) UnregisterFiles() error {
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_UNREGISTER_FILES,
0, 0, 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// UpdateFiles ...
func (r *Ring) UpdateFiles(fds []int32, off uint32) error {
update := IOUringFilesUpdate{
Offset: off,
Fds: &fds[0],
}
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_REGISTER_FILES_UPDATE,
uintptr(unsafe.Pointer(&update)),
uintptr(len(fds)), 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// RegisterBuffers ...
func (r *Ring) RegisterBuffers(iovec []unix.Iovec) error {
if len(iovec) == 0 {
return nil
}
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_REGISTER_BUFFERS,
uintptr(unsafe.Pointer(&iovec[0])),
uintptr(len(iovec)), 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// UnregisterBuffers ...
func (r *Ring) UnregisterBuffers() error {
for {
_, _, errno := unix.Syscall6(
IO_URING_REGISTER,
uintptr(r.fd),
IORING_UNREGISTER_BUFFERS,
0, 0, 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
return errno
}
return nil
}
}
// SetupEventfd creates eventfd and registers it with current uring instance.
func (r *Ring) SetupEventfd() error {
if r.eventfd == 0 {
r0, _, errno := unix.Syscall(unix.SYS_EVENTFD2, 0, 0, 0)
if errno > 0 {
return errno
}
r.eventfd = r0
}
for {
_, _, errno := unix.Syscall6(IO_URING_REGISTER, uintptr(r.fd), IORING_REGISTER_EVENTFD, uintptr(unsafe.Pointer(&r.eventfd)), 1, 0, 0)
if errno > 0 {
if errno == unix.EINTR {
continue
}
_ = r.CloseEventfd()
return errno
}
return nil
}
}
// CloseEventfd unregsiters eventfd from uring istance and closes associated fd.
func (r *Ring) CloseEventfd() error {
if r.eventfd == 0 {
return nil
}
var errno unix.Errno
for {
_, _, errno = unix.Syscall6(IO_URING_REGISTER, uintptr(r.fd), IORING_UNREGISTER_EVENTFD, 0, 0, 0, 0)
if errno != unix.EINTR {
break
}
}
if err := unix.Close(int(r.eventfd)); err != nil {
return err
}
r.eventfd = 0
if errno > 0 {
return errno
}
return nil
}