-
Notifications
You must be signed in to change notification settings - Fork 10
/
ops.go
632 lines (559 loc) · 12.3 KB
/
ops.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// +build linux
package iouring
import (
"encoding/binary"
"runtime"
"syscall"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
var (
errRingUnavailable = errors.New("ring unavailable")
)
// PrepareAccept is used to prepare a SQE for an accept(2) call.
func (r *Ring) PrepareAccept(
fd int,
addr syscall.Sockaddr,
socklen uint32,
flags int,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Accept
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&addr)))
sqe.Offset = uint64(socklen)
sqe.UFlags = int32(flags)
ready()
return sqe.UserData, nil
}
// PrepareClose is used to prepare a close(2) call.
func (r *Ring) PrepareClose(fd int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Close
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
ready()
return sqe.UserData, nil
}
// Close is implements close(2).
func (r *Ring) Close(fd int) error {
id, err := r.PrepareClose(fd)
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareConnect is used to prepare a SQE for a connect(2) call.
func (r *Ring) PrepareConnect(
fd int,
addr syscall.Sockaddr,
socklen uint32,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Connect
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&addr)))
sqe.Len = socklen
ready()
return sqe.UserData, nil
}
// PrepareFadvise is used to prepare a fadvise call.
func (r *Ring) PrepareFadvise(
fd int, offset uint64, n uint32, advise int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Fadvise
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = n
sqe.Offset = offset
sqe.UFlags = int32(advise)
ready()
return sqe.UserData, nil
}
// Fadvise implements fadvise.
func (r *Ring) Fadvise(fd int, offset uint64, n uint32, advise int) error {
id, err := r.PrepareFadvise(fd, offset, n, advise)
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareFallocate is used to prepare a fallocate call.
func (r *Ring) PrepareFallocate(
fd int, mode uint32, offset int64, n int64) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Fallocate
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = uint64(n)
sqe.Len = mode
sqe.Offset = uint64(offset)
ready()
return sqe.UserData, nil
}
// Fallocate implements fallocate.
func (r *Ring) Fallocate(fd int, mode uint32, offset int64, n int64) error {
id, err := r.PrepareFallocate(fd, mode, offset, n)
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareFsync is used to prepare a fsync(2) call.
func (r *Ring) PrepareFsync(fd int, flags int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Fsync
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.UFlags = int32(flags)
ready()
return sqe.UserData, nil
}
// Fsync implements fsync(2).
func (r *Ring) Fsync(fd int, flags int) error {
id, err := r.PrepareFsync(fd, flags)
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareNop is used to prep a nop.
func (r *Ring) PrepareNop() (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Nop
sqe.UserData = r.ID()
sqe.Fd = -1
ready()
return sqe.UserData, nil
}
// Nop is a nop.
func (r *Ring) Nop() error {
id, err := r.PrepareNop()
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PollAdd is used to add a poll to a fd.
func (r *Ring) PollAdd(fd int, mask int) error {
id, err := r.PreparePollAdd(fd, mask)
if err != nil {
return err
}
errno, _ := r.complete(id)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PreparePollAdd is used to prepare a SQE for adding a poll.
func (r *Ring) PreparePollAdd(fd int, mask int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = PollAdd
sqe.Fd = int32(fd)
sqe.UFlags = int32(mask)
sqe.UserData = r.ID()
ready()
return sqe.UserData, nil
}
// PrepareReadv is used to prepare a readv SQE.
func (r *Ring) PrepareReadv(
fd int,
iovecs []*syscall.Iovec,
offset int,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Readv
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&iovecs[0])))
sqe.Len = uint32(len(iovecs))
sqe.Offset = uint64(offset)
ready()
return sqe.UserData, nil
}
// PrepareRecvmsg is used to prepare a recvmsg SQE.
func (r *Ring) PrepareRecvmsg(
fd int,
msg *syscall.Msghdr,
flags int,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = RecvMsg
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(msg)))
sqe.Len = 1
sqe.Offset = 0
sqe.UFlags = int32(flags)
ready()
return sqe.UserData, nil
}
// Splice implements splice using a ring.
func (r *Ring) Splice(
inFd int,
inOff *int64,
outFd int,
outOff *int64,
n int,
flags int,
) (int64, error) {
id, err := r.PrepareSplice(inFd, inOff, outFd, outOff, n, flags)
if err != nil {
return 0, err
}
// TODO: replace complete with something more efficient.
errno, res := r.complete(id)
if errno < 0 {
return 0, syscall.Errno(-errno)
}
runtime.KeepAlive(inOff)
runtime.KeepAlive(outOff)
return int64(res), nil
}
// PrepareSplice is used to prepare a SQE for a splice(2).
func (r *Ring) PrepareSplice(
inFd int,
inOff *int64,
outFd int,
outOff *int64,
n int,
flags int,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Splice
sqe.Fd = int32(outFd)
if inOff != nil {
sqe.Addr = uint64(uintptr(unsafe.Pointer(&inOff)))
} else {
sqe.Addr = 0
}
sqe.Len = uint32(n)
if outOff != nil {
sqe.Offset = uint64(uintptr(unsafe.Pointer(&outOff)))
} else {
sqe.Offset = 0
}
sqe.UFlags = int32(flags)
// BUG: need to convert the inFd to the union member of the SQE
anon := [24]byte{}
binary.LittleEndian.PutUint32(anon[4:], uint32(inFd))
sqe.Anon0 = anon
sqe.UserData = r.ID()
ready()
return sqe.UserData, nil
}
// Statx implements statx using a ring.
func (r *Ring) Statx(
dirfd int,
path string,
flags int,
mask int,
statx *unix.Statx_t,
) (err error) {
id, err := r.PrepareStatx(dirfd, path, flags, mask, statx)
if err != nil {
return err
}
errno, _ := r.complete(id)
// No GC until the request is done.
runtime.KeepAlive(statx)
runtime.KeepAlive(dirfd)
runtime.KeepAlive(path)
runtime.KeepAlive(mask)
runtime.KeepAlive(flags)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareStatx is used to prepare a Statx call and will return the request id
// (SQE UserData) of the SQE. After calling the returned callback function the
// ring is safe to be entered.
func (r *Ring) PrepareStatx(
dirfd int,
path string,
flags int,
mask int,
statx *unix.Statx_t,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Statx
sqe.Fd = int32(dirfd)
if path != "" {
// TODO: could probably avoid the conversion to []byte
b := saferStringToBytes(&path)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
}
sqe.Len = uint32(mask)
sqe.Offset = (uint64)(uintptr(unsafe.Pointer(statx)))
sqe.UFlags = int32(flags)
sqe.UserData = r.ID()
ready()
return sqe.UserData, nil
}
// PrepareTimeout is used to prepare a timeout SQE.
func (r *Ring) PrepareTimeout(
ts *syscall.Timespec, count int, flags int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Timeout
sqe.UserData = r.ID()
sqe.UFlags = int32(flags)
sqe.Fd = -1
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(ts)))
sqe.Len = 1
sqe.Offset = uint64(count)
ready()
return sqe.UserData, nil
}
// PrepareTimeoutRemove is used to prepare a timeout removal.
func (r *Ring) PrepareTimeoutRemove(data uint64, flags int) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = TimeoutRemove
sqe.UserData = r.ID()
sqe.UFlags = int32(flags)
sqe.Fd = -1
sqe.Addr = data
sqe.Len = 0
sqe.Offset = 0
ready()
return sqe.UserData, nil
}
// PrepareRead is used to prepare a read SQE.
func (r *Ring) PrepareRead(
fd int,
b []byte,
offset uint64,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Read
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Offset = offset
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// PrepareReadFixed is used to prepare a fixed read SQE.
func (r *Ring) PrepareReadFixed(
fd int,
b []byte,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = ReadFixed
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// PrepareWrite is used to prepare a Write SQE.
func (r *Ring) PrepareWrite(
fd int,
b []byte,
offset uint64,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Write
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Offset = offset
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// PrepareWriteFixed is used to prepare a fixed write SQE.
func (r *Ring) PrepareWriteFixed(
fd int,
b []byte,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = WriteFixed
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// PrepareWritev is used to prepare a writev SQE.
func (r *Ring) PrepareWritev(
fd int,
iovecs []*syscall.Iovec,
offset int,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Writev
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&iovecs[0])))
sqe.Len = uint32(len(iovecs))
sqe.Offset = uint64(offset)
ready()
return sqe.UserData, nil
}
// PrepareSend is used to prepare a Send SQE.
func (r *Ring) PrepareSend(
fd int,
b []byte,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Send
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// Send is used to send data to a socket.
func (r *Ring) Send(
fd int,
b []byte,
flags uint8,
) error {
id, err := r.PrepareSend(fd, b, flags)
if err != nil {
return err
}
errno, _ := r.complete(id)
// No GC until the request is done.
runtime.KeepAlive(b)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}
// PrepareRecv is used to prepare a Recv SQE.
func (r *Ring) PrepareRecv(
fd int,
b []byte,
flags uint8,
) (uint64, error) {
sqe, ready := r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Recv
sqe.UserData = r.ID()
sqe.Fd = int32(fd)
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
return sqe.UserData, nil
}
// Recv is used to recv data on a socket.
func (r *Ring) Recv(
fd int,
b []byte,
flags uint8,
) error {
id, err := r.PrepareRecv(fd, b, flags)
if err != nil {
return err
}
errno, _ := r.complete(id)
// No GC until the request is done.
runtime.KeepAlive(b)
if errno < 0 {
return syscall.Errno(-errno)
}
return nil
}