Source file src/net/splice_linux.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"internal/poll"
     9  	"io"
    10  )
    11  
    12  // spliceFrom transfers data from r to c using the splice system call to minimize
    13  // copies from and to userspace. c must be a TCP connection.
    14  // Currently, spliceFrom is only enabled if r is a TCP or a stream-oriented Unix connection.
    15  //
    16  // If spliceFrom returns handled == false, it has performed no work.
    17  func spliceFrom(c *netFD, r io.Reader) (written int64, err error, handled bool) {
    18  	var remain int64 = 1<<63 - 1 // by default, copy until EOF
    19  	lr, ok := r.(*io.LimitedReader)
    20  	if ok {
    21  		remain, r = lr.N, lr.R
    22  		if remain <= 0 {
    23  			return 0, nil, true
    24  		}
    25  	}
    26  
    27  	var s *netFD
    28  	switch v := r.(type) {
    29  	case *TCPConn:
    30  		s = v.fd
    31  	case tcpConnWithoutWriteTo:
    32  		s = v.fd
    33  	case *UnixConn:
    34  		if v.fd.net != "unix" {
    35  			return 0, nil, false
    36  		}
    37  		s = v.fd
    38  	default:
    39  		return 0, nil, false
    40  	}
    41  
    42  	written, handled, sc, err := poll.Splice(&c.pfd, &s.pfd, remain)
    43  	if lr != nil {
    44  		lr.N -= written
    45  	}
    46  	return written, wrapSyscallError(sc, err), handled
    47  }
    48  
    49  // spliceTo transfers data from c to w using the splice system call to minimize
    50  // copies from and to userspace. c must be a TCP connection.
    51  // Currently, spliceTo is only enabled if w is a stream-oriented Unix connection.
    52  //
    53  // If spliceTo returns handled == false, it has performed no work.
    54  func spliceTo(w io.Writer, c *netFD) (written int64, err error, handled bool) {
    55  	uc, ok := w.(*UnixConn)
    56  	if !ok || uc.fd.net != "unix" {
    57  		return
    58  	}
    59  
    60  	written, handled, sc, err := poll.Splice(&uc.fd.pfd, &c.pfd, 1<<63-1)
    61  	return written, wrapSyscallError(sc, err), handled
    62  }
    63  

View as plain text