2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-08-17 11:28:29 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 07:47:42 -07:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2011-08-17 11:28:29 -06:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToTCP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-04-27 04:00:09 -06:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneCache.name(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TCPAddr) family() int {
|
2011-08-24 11:59:33 -06:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-08-17 11:28:29 -06:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2013-08-02 22:32:22 -06:00
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port, a.Zone)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-10-26 19:07:52 -06:00
|
|
|
func (a *TCPAddr) toLocal(net string) sockaddr {
|
|
|
|
return &TCPAddr{loopbackIP(net), a.Port, a.Zone}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
|
net: add support for splice(2) in (*TCPConn).ReadFrom on Linux
This change adds support for the splice system call on Linux,
for the purpose of optimizing (*TCPConn).ReadFrom by reducing
copies of data from and to userspace. It does so by creating a
temporary pipe and splicing data from the source connection to the
pipe, then from the pipe to the destination connection. The pipe
serves as an in-kernel buffer for the data transfer.
No new API is added to package net, but a new Splice function is
added to package internal/poll, because using splice requires help
from the network poller. Users of the net package should benefit
from the change transparently.
This change only enables the optimization if the Reader in ReadFrom
is a TCP connection. Since splice is a more general interface, it
could, in theory, also be enabled if the Reader were a unix socket,
or the read half of a pipe.
However, benchmarks show that enabling it for unix sockets is most
likely not a net performance gain. The tcp <- unix case is also
fairly unlikely to be used very much by users of package net.
Enabling the optimization for pipes is also problematic from an
implementation perspective, since package net cannot easily get at
the *poll.FD of an *os.File. A possible solution to this would be
to dup the pipe file descriptor, register the duped descriptor with
the network poller, and work on that *poll.FD instead of the original.
However, this seems too intrusive, so it has not been done. If there
was a clean way to do it, it would probably be worth doing, since
splicing from a pipe to a socket can be done directly.
Therefore, this patch only enables the optimization for what is likely
the most common use case: tcp <- tcp.
The following benchmark compares the performance of the previous
userspace genericReadFrom code path to the new optimized code path.
The sub-benchmarks represent chunk sizes used by the writer on the
other end of the Reader passed to ReadFrom.
benchmark old ns/op new ns/op delta
BenchmarkTCPReadFrom/1024-4 4727 4954 +4.80%
BenchmarkTCPReadFrom/2048-4 4389 4301 -2.01%
BenchmarkTCPReadFrom/4096-4 4606 4534 -1.56%
BenchmarkTCPReadFrom/8192-4 5219 4779 -8.43%
BenchmarkTCPReadFrom/16384-4 8708 8008 -8.04%
BenchmarkTCPReadFrom/32768-4 16349 14973 -8.42%
BenchmarkTCPReadFrom/65536-4 35246 27406 -22.24%
BenchmarkTCPReadFrom/131072-4 72920 52382 -28.17%
BenchmarkTCPReadFrom/262144-4 149311 95094 -36.31%
BenchmarkTCPReadFrom/524288-4 306704 181856 -40.71%
BenchmarkTCPReadFrom/1048576-4 674174 357406 -46.99%
benchmark old MB/s new MB/s speedup
BenchmarkTCPReadFrom/1024-4 216.62 206.69 0.95x
BenchmarkTCPReadFrom/2048-4 466.61 476.08 1.02x
BenchmarkTCPReadFrom/4096-4 889.09 903.31 1.02x
BenchmarkTCPReadFrom/8192-4 1569.40 1714.06 1.09x
BenchmarkTCPReadFrom/16384-4 1881.42 2045.84 1.09x
BenchmarkTCPReadFrom/32768-4 2004.18 2188.41 1.09x
BenchmarkTCPReadFrom/65536-4 1859.38 2391.25 1.29x
BenchmarkTCPReadFrom/131072-4 1797.46 2502.21 1.39x
BenchmarkTCPReadFrom/262144-4 1755.69 2756.68 1.57x
BenchmarkTCPReadFrom/524288-4 1709.42 2882.98 1.69x
BenchmarkTCPReadFrom/1048576-4 1555.35 2933.84 1.89x
Fixes #10948
Change-Id: I3ce27f21f7adda8b696afdc48a91149998ae16a5
Reviewed-on: https://go-review.googlesource.com/107715
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-04-18 02:56:06 -06:00
|
|
|
if n, err, handled := splice(c.fd, r); handled {
|
|
|
|
return n, err
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
if n, err, handled := sendFile(c.fd, r); handled {
|
|
|
|
return n, err
|
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return genericReadFrom(c, r)
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
2012-02-12 21:25:55 -07:00
|
|
|
|
2018-05-29 16:53:19 -06:00
|
|
|
func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
2016-04-14 18:47:25 -06:00
|
|
|
if testHookDialTCP != nil {
|
2018-05-29 16:53:19 -06:00
|
|
|
return testHookDialTCP(ctx, sd.network, laddr, raddr)
|
2016-04-14 18:47:25 -06:00
|
|
|
}
|
2018-05-29 16:53:19 -06:00
|
|
|
return sd.doDialTCP(ctx, laddr, raddr)
|
2016-04-14 18:47:25 -06:00
|
|
|
}
|
|
|
|
|
2018-05-29 16:53:19 -06:00
|
|
|
func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
|
2012-02-12 21:25:55 -07:00
|
|
|
|
|
|
|
// TCP has a rarely used mechanism called a 'simultaneous connection' in
|
|
|
|
// which Dial("tcp", addr1, addr2) run on the machine at addr1 can
|
|
|
|
// connect to a simultaneous Dial("tcp", addr2, addr1) run on the machine
|
2016-03-01 16:21:55 -07:00
|
|
|
// at addr2, without either machine executing Listen. If laddr == nil,
|
2012-02-12 21:25:55 -07:00
|
|
|
// it means we want the kernel to pick an appropriate originating local
|
2016-03-01 16:21:55 -07:00
|
|
|
// address. Some Linux kernels cycle blindly through a fixed range of
|
|
|
|
// local ports, regardless of destination port. If a kernel happens to
|
2012-02-12 21:25:55 -07:00
|
|
|
// pick local port 50001 as the source for a Dial("tcp", "", "localhost:50001"),
|
|
|
|
// then the Dial will succeed, having simultaneously connected to itself.
|
|
|
|
// This can only happen when we are letting the kernel pick a port (laddr == nil)
|
|
|
|
// and when there is no listener for the destination address.
|
2016-03-01 16:21:55 -07:00
|
|
|
// It's hard to argue this is anything other than a kernel bug. If we
|
2012-02-12 21:25:55 -07:00
|
|
|
// see this happen, rather than expose the buggy effect to users, we
|
2016-03-01 16:21:55 -07:00
|
|
|
// close the fd and try again. If it happens twice more, we relent and
|
|
|
|
// use the result. See also:
|
2015-07-10 17:17:11 -06:00
|
|
|
// https://golang.org/issue/2690
|
2012-02-12 21:25:55 -07:00
|
|
|
// http://stackoverflow.com/questions/4949858/
|
2012-08-06 14:32:00 -06:00
|
|
|
//
|
|
|
|
// The opposite can also happen: if we ask the kernel to pick an appropriate
|
|
|
|
// originating local address, sometimes it picks one that is already in use.
|
|
|
|
// So if the error is EADDRNOTAVAIL, we have to try again too, just for
|
|
|
|
// a different reason.
|
|
|
|
//
|
|
|
|
// The kernel socket code is no doubt enjoying watching us squirm.
|
|
|
|
for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
|
|
|
|
if err == nil {
|
|
|
|
fd.Close()
|
|
|
|
}
|
2018-05-29 16:53:19 -06:00
|
|
|
fd, err = internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
|
2012-02-12 21:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:32:00 -06:00
|
|
|
func selfConnect(fd *netFD, err error) bool {
|
|
|
|
// If the connect failed, we clearly didn't connect to ourselves.
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:48:05 -07:00
|
|
|
// The socket constructor can return an fd with raddr nil under certain
|
|
|
|
// unknown conditions. The errors in the calls there to Getpeername
|
|
|
|
// are discarded, but we can't catch the problem there because those
|
|
|
|
// calls are sometimes legally erroneous with a "socket not connected".
|
|
|
|
// Since this code (selfConnect) is already trying to work around
|
|
|
|
// a problem, we make sure if this happens we recognize trouble and
|
|
|
|
// ask the DialTCP routine to try again.
|
|
|
|
// TODO: try to understand what's really going on.
|
|
|
|
if fd.laddr == nil || fd.raddr == nil {
|
|
|
|
return true
|
|
|
|
}
|
2012-02-12 21:25:55 -07:00
|
|
|
l := fd.laddr.(*TCPAddr)
|
|
|
|
r := fd.raddr.(*TCPAddr)
|
|
|
|
return l.Port == r.Port && l.IP.Equal(r.IP)
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:32:00 -06:00
|
|
|
func spuriousENOTAVAIL(err error) bool {
|
2015-07-27 21:28:09 -06:00
|
|
|
if op, ok := err.(*OpError); ok {
|
|
|
|
err = op.Err
|
|
|
|
}
|
|
|
|
if sys, ok := err.(*os.SyscallError); ok {
|
|
|
|
err = sys.Err
|
|
|
|
}
|
|
|
|
return err == syscall.EADDRNOTAVAIL
|
2012-08-06 14:32:00 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) accept() (*TCPConn, error) {
|
|
|
|
fd, err := ln.fd.accept()
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return newTCPConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) close() error {
|
|
|
|
return ln.fd.Close()
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) file() (*os.File, error) {
|
|
|
|
f, err := ln.fd.dup()
|
2015-04-18 01:53:55 -06:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return f, nil
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2012-11-12 20:56:28 -07:00
|
|
|
|
2018-05-29 16:53:19 -06:00
|
|
|
func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
|
|
|
|
fd, err := internetSocket(ctx, sl.network, laddr, nil, syscall.SOCK_STREAM, 0, "listen")
|
2012-11-12 20:56:28 -07:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
2012-12-03 04:00:50 -07:00
|
|
|
return &TCPListener{fd}, nil
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|