-
Notifications
You must be signed in to change notification settings - Fork 36
/
zerocopy_linux.go
730 lines (681 loc) · 15.2 KB
/
zerocopy_linux.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Copyright (c) 2018 The Go Authors https://golang.org/AUTHORS
// Copyright (c) 2019 Andrei Tudor Călin <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zerocopy
/*
On Linux, package zerocopy uses splice(2) and tee(2) to accelerate I/O
pipelines. While most I/O system calls operate on a single file descriptor,
splice(2) and tee(2) operate on two. This makes using them from Go quite
tricky. Special precautions have to be taken in order to avoid a certain
class of deadlocks, and play nice with the outside world.
The algorithm to acheive this is rather complicated and subtle, but it is
used in a number of places in this package, with minor adaptations based on
context. This comment describes the algorithm. Because the real Go code that
implements the algorithm is much more messy, and involves more book-keeping
in terms of error handling, the algorithm is presented in pseudo-code.
Preparatory definitions
-----------------------
increfscope takes a file descriptor argument, acquires a reference to the
descriptor, runs the block of code, then releases the reference when control
exits the block (either naturally or via a goto statement).
transfer is a data transfer function, either splice(2) or tee(2).
wait waits for its file descriptor argument to be ready for an operation,
either 'r' for reading, or 'w' for writing.
The algorithm
-------------
transfered = 0
errno = nil
round1:
increfscope(rfd) {
readready = false
round1again:
increfscope(wfd) {
transfered, errno = transfer(rfd, wfd)
if errno is EAGAIN {
if readready {
goto round2
}
} else {
goto end
}
}
wait(rfd, 'r')
readready = true
goto round1again
}
round2:
increfscope(wfd) {
writeready = false
round2again:
increfscope(rfd) {
transfered, errno = transfer(rfd, wfd)
if errno is EAGAIN {
if writeready {
goto round1
}
} else {
goto end
}
}
wait(wfd, 'w')
writeready = true
goto round2again
}
end:
return transfered, errno
Restrictions, Analysis
----------------------
transfer can only be called while holding a reference to both file
descriptors. wait can only be called while holding a reference to the file
descriptor. It is forbidden to call wait on a file descriptor while also
holding a reference to another file descriptor.
Consider the naïve try:
increfscope(rfd) {
increfscope(wfd) {
again:
transfered, errno = transfer(rfd, wfd)
if errno is EAGAIN {
wait(rfd, 'r')
wait(wfd, 'w')
goto again
}
}
}
This snippet is broken and dangerous.
If we're blocked in wait(wfd, 'w'), and in another place, a caller tries to
perform any operation on rfd, such as a hypothetical close(rfd), then the
other caller is blocked, waiting for an unrelated operation, on another
file descriptor.
The algorithm is so complicated because has to take great care to not end up
in a situation equivalent to this snippet. See also golang.org/issues/25985
for an example of such a bug, from the stdlib splice implementation.
Any changes to this package must retain these properties.
*/
import (
"io"
"os"
"syscall"
"golang.org/x/sys/unix"
)
func (p *Pipe) bufferSize() (int, error) {
var (
size uintptr
errno syscall.Errno
)
err := p.wrc.Control(func(fd uintptr) {
size, _, errno = unix.Syscall(
unix.SYS_FCNTL,
fd,
unix.F_GETPIPE_SZ,
0,
)
})
if err != nil {
return 0, err
}
if errno != 0 {
return 0, os.NewSyscallError("getpipesz", errno)
}
return int(size), nil
}
func (p *Pipe) setBufferSize(n int) error {
var errno syscall.Errno
err := p.wrc.Control(func(fd uintptr) {
_, _, errno = unix.Syscall(
unix.SYS_FCNTL,
fd,
unix.F_SETPIPE_SZ,
uintptr(n),
)
})
if err != nil {
return err
}
if errno != 0 {
return os.NewSyscallError("setpipesz", errno)
}
return nil
}
func (p *Pipe) read(b []byte) (int, error) {
// There are three cases here:
//
// If p is not configured to tee data to another writer, then
// p.teepipe is nil, and p.teerd is p.r.
//
// If p is configured to tee data to an io.Writer that is not a *Pipe,
// then p.teepipe is nil, and p.teerd is an io.TeeReader of p.r and
// the io.Writer.
//
// Finally, if p is configured to tee data to another *Pipe, then
// p.teepipe is not nil, and p.teerd is p.r.
if p.teepipe == nil {
return p.teerd.Read(b)
}
// Here, we are on the tee(2) code path. When more than one stream of
// data is involved, there are usually flow control considerations to
// keep in mind, but here, we try to do the simple thing. We let the
// size of the first tee guide us.
//
// Hopefully this approach is good enough for general use. Doing
// anything else would be exceptionally complicated, and would require
// the library to be either very configurable, or very opinionated.
//
// HC SVNT DRACONES. See the comment at the top of the file.
var (
copied int64
operr error // error from tee(2)
rrcerr error // non-nil if read FD is dead
wrcerr error // non-nil if write FD is dead
waitread = false
readready = false
done = false
writeready = false
waitwrite = false
waitreadagain = false
)
again:
rrcerr = p.rrc.Read(func(prfd uintptr) bool {
wrcerr = p.teepipe.wrc.Write(func(pwfd uintptr) bool {
copied, operr = tee(prfd, pwfd, len(b))
if operr == unix.EAGAIN {
if !readready {
waitread = true
}
return true
}
done = true
operr = os.NewSyscallError("tee", operr)
return true
})
if waitread {
readready = true
waitread = false
return false
}
return true
})
if rrcerr != nil || done {
goto end
}
wrcerr = p.teepipe.wrc.Write(func(pwfd uintptr) bool {
p.rrc.Read(func(prfd uintptr) bool {
copied, operr = tee(prfd, pwfd, len(b))
if operr == unix.EAGAIN {
if writeready {
waitreadagain = true
} else {
waitwrite = true
}
return true
}
operr = os.NewSyscallError("tee", operr)
return true
})
if waitwrite {
writeready = true
waitwrite = false
return false
}
return true
})
if wrcerr != nil {
goto end
}
if waitreadagain {
goto again
}
end:
// If rrcerr is not nil, we do not report it immediately: a Read on
// a syscall.RawConn only returns an error if the file descriptor
// is closed. If the read side of the pipe we own is indeed closed,
// the next call to Read on p.teerd will observe this condition. In
// that case, we let the better error reporting of package os kick in.
//
// As for write errors on the pipe we tee to, if the target FD is
// closed, then the pipeline is dead anyway. All we've done so far
// is to try to tee from p.r. We haven't consumed anything from the
// pipe. Nevertheless, we should read from the pipe, but report
// the dead pipe file descriptor as an error, since this is what
// io.TeeReader does as well.
//
// Finally, we must be careful not to read more than we copied to
// the other pipe, otherwise we will have missed tee-ing some data.
limit := len(b)
if copied > 0 {
limit = int(copied)
}
n, err := p.teerd.Read(b[:limit])
if wrcerr != nil {
return n, wrcerr
}
if operr != nil {
return n, operr
}
return n, err
}
const maxSpliceSize = 4 << 20
func (p *Pipe) readFrom(src io.Reader) (int64, error) {
// If src is a limited reader, honor the limit.
var (
rd io.Reader
limit int64 = 1<<63 - 1
)
lr, ok := src.(*io.LimitedReader)
if ok {
rd = lr.R
limit = lr.N
} else {
rd = src
}
sc, ok := rd.(syscall.Conn)
if !ok {
return io.Copy(p.w, src)
}
rrc, err := sc.SyscallConn()
if err != nil {
return io.Copy(p.w, src)
}
var (
atEOF bool
moved int64
operr error
rrcerr error
wrcerr error
fallback = false
waitread = false
readready = false
writeready = false
waitwrite = false
waitreadagain = false
)
if lr != nil {
defer func(v *int64) {
lr.N -= *v
}(&moved)
}
again:
ok = false
max := maxSpliceSize
if int64(max) > limit {
max = int(limit)
}
rrcerr = rrc.Read(func(rfd uintptr) bool {
wrcerr = p.wrc.Write(func(pwfd uintptr) bool {
var n int
n, operr = splice(rfd, pwfd, max)
if n > 0 {
limit -= int64(n)
moved += int64(n)
}
if operr == unix.EINVAL {
fallback = true
return true
}
if operr == unix.EAGAIN {
waitread = !readready
return true
}
if operr == nil {
if n == 0 {
atEOF = true
} else {
ok = true
}
}
operr = os.NewSyscallError("splice", operr)
return true
})
if waitread {
readready = true
waitread = false
return false
}
return true
})
if fallback {
return io.Copy(p.w, src)
}
if wrcerr != nil || atEOF {
return moved, wrcerr
}
if ok {
if limit > 0 {
goto again
}
goto end
}
// If we're here, we have not spliced yet on this round, and we're
// waiting for the pipe to be ready.
wrcerr = p.wrc.Write(func(pwfd uintptr) bool {
rrcerr = rrc.Read(func(rfd uintptr) bool {
var n int
n, operr = splice(rfd, pwfd, max)
if n > 0 {
limit -= int64(n)
moved += int64(n)
}
if operr == unix.EAGAIN {
if writeready {
waitwrite = false
waitreadagain = true
} else {
waitwrite = true
}
return true
}
operr = os.NewSyscallError("splice", operr)
return true
})
if waitwrite {
writeready = true
waitwrite = false
return false
}
return true
})
if rrcerr != nil {
return moved, rrcerr
}
if wrcerr != nil {
return moved, wrcerr
}
if operr != nil {
return moved, operr
}
if limit > 0 || waitreadagain {
goto again
}
end:
return moved, nil
}
func (p *Pipe) writeTo(dst io.Writer) (int64, error) {
sc, ok := dst.(syscall.Conn)
if !ok {
return io.Copy(dst, onlyReader{p})
}
wrc, err := sc.SyscallConn()
if err != nil {
return io.Copy(dst, onlyReader{p})
}
var (
atEOF bool
moved int64
operr error
rrcerr error
wrcerr error
fallback = false
waitread = false
readready = false
writeready = false
waitwrite = false
waitreadagain = false
)
again:
ok = false
rrcerr = p.rrc.Read(func(rfd uintptr) bool {
wrcerr = wrc.Write(func(pwfd uintptr) bool {
var n int
n, operr = splice(rfd, pwfd, maxSpliceSize)
if n > 0 {
moved += int64(n)
}
if operr == unix.EINVAL {
fallback = true
return true
}
if operr == unix.EAGAIN {
if !readready {
waitread = true
}
return true
}
if operr == nil {
if n == 0 {
atEOF = true
} else {
ok = true
}
}
operr = os.NewSyscallError("splice", operr)
return true
})
if waitread {
readready = true
waitread = false
return false
}
return true
})
if fallback {
return io.Copy(dst, onlyReader{p})
}
if wrcerr != nil || atEOF {
return moved, wrcerr
}
if ok {
goto end
}
// If we're here, we have not spliced yet on this round, and we're
// waiting for the destination file descriptor to be ready.
wrcerr = wrc.Write(func(pwfd uintptr) bool {
rrcerr = p.rrc.Read(func(rfd uintptr) bool {
var n int
n, operr = splice(rfd, pwfd, maxSpliceSize)
if n > 0 {
moved += int64(n)
}
if operr == unix.EAGAIN {
if writeready {
waitreadagain = true
} else {
waitwrite = true
}
return true
}
operr = os.NewSyscallError("splice", operr)
return true
})
if waitwrite {
writeready = true
waitwrite = false
return false
}
return true
})
if rrcerr != nil {
return moved, rrcerr
}
if wrcerr != nil {
return moved, wrcerr
}
if operr != nil {
return moved, operr
}
if waitreadagain {
goto again
}
end:
return moved, nil
}
func transfer(dst io.Writer, src io.Reader) (int64, error) {
// If src is a limited reader, honor the limit.
var (
rd io.Reader
limit int64 = 1<<63 - 1
)
lr, ok := src.(*io.LimitedReader)
if ok {
rd = lr.R
limit = lr.N
} else {
rd = src
}
rsc, ok := rd.(syscall.Conn)
if !ok {
return io.Copy(dst, src)
}
rrc, err := rsc.SyscallConn()
if err != nil {
return io.Copy(dst, src)
}
wsc, ok := dst.(syscall.Conn)
if !ok {
return io.Copy(dst, src)
}
wrc, err := wsc.SyscallConn()
if err != nil {
return io.Copy(dst, src)
}
// Now, we know that dst and src are two file descriptors
// that we could try to splice to / from, but we won't know
// for sure until we actually try.
//
// See also src/internal/poll/splice_linux.go, which this code
// is a pretty direct translation of.
p, err := NewPipe()
if err != nil {
return io.Copy(dst, src)
}
var moved int64 = 0
if lr != nil {
defer func(v *int64) {
lr.N -= *v
}(&moved)
}
for limit > 0 {
max := maxSpliceSize
if int64(max) > limit {
max = int(limit)
}
inpipe, fallback, err := spliceDrain(p, rrc, max)
limit -= int64(inpipe)
if fallback {
return io.Copy(dst, src)
}
if inpipe == 0 && err == nil {
return moved, nil
}
if err != nil {
return moved, err
}
n, fallback, err := splicePump(wrc, p, inpipe)
if n > 0 {
moved += int64(n)
}
if fallback {
// dst doesn't support splicing, but we've already
// read from src, so we need to empty the pipe,
// and then switch to a regular io.Copy.
n1, err := io.CopyN(dst, p.w, int64(inpipe))
moved += n1
if err != nil {
return n1, err
}
n2, err := io.Copy(dst, src)
return n1 + n2, err
}
if err != nil {
return moved, err
}
}
return moved, nil
}
func spliceDrain(p *Pipe, rrc syscall.RawConn, max int) (int, bool, error) {
var (
moved int
rrcerr error
serr error
)
fallback := false
err := p.wrc.Write(func(pwfd uintptr) bool {
rrcerr = rrc.Read(func(rfd uintptr) bool {
var n int
n, serr = splice(rfd, pwfd, max)
moved = int(n)
if serr == unix.EINVAL {
fallback = true
return true
}
if serr == unix.EAGAIN {
return false
}
serr = os.NewSyscallError("splice", serr)
return true
})
return true
})
if err != nil {
return 0, false, err
}
if rrcerr != nil {
return 0, false, rrcerr
}
return moved, fallback, serr
}
func splicePump(wrc syscall.RawConn, p *Pipe, inpipe int) (int, bool, error) {
var (
fallback bool
moved int
wrcerr error
serr error
)
again:
err := p.rrc.Read(func(prfd uintptr) bool {
wrcerr = wrc.Write(func(wfd uintptr) bool {
var n int
n, serr = splice(prfd, wfd, inpipe)
if n > 0 {
moved += int(n)
inpipe -= int(n)
}
if serr == unix.EINVAL {
fallback = true
return true
}
if serr == unix.EAGAIN {
return false
}
serr = os.NewSyscallError("splice", serr)
return true
})
return true
})
if fallback {
return moved, true, nil
}
if err != nil {
return moved, false, err
}
if wrcerr != nil {
return moved, false, wrcerr
}
if serr != nil {
return moved, false, serr
}
if inpipe > 0 {
goto again
}
return moved, false, nil
}
func (p *Pipe) tee(w io.Writer) {
tp, ok := w.(*Pipe)
if ok {
p.teepipe = tp
} else {
p.teerd = io.TeeReader(p.r, w)
}
}
type onlyReader struct {
io.Reader
}
// tee calls tee(2) with SPLICE_F_NONBLOCK.
func tee(rfd, wfd uintptr, max int) (int64, error) {
return unix.Tee(int(rfd), int(wfd), max, unix.SPLICE_F_NONBLOCK)
}
// splice calls splice(2) with SPLICE_F_NONBLOCK.
func splice(rfd, wfd uintptr, max int) (int, error) {
n, err := unix.Splice(int(rfd), nil, int(wfd), nil, max, unix.SPLICE_F_NONBLOCK)
return int(n), err
}