2012-01-10 17:53:32 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-09-18 04:17:55 -06:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
|
2012-01-10 17:53:32 -07:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Boolean to int.
|
|
|
|
func boolint(b bool) int {
|
|
|
|
if b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipv4AddrToInterface(ip IP) (*Interface, error) {
|
|
|
|
ift, err := Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ifi := range ift {
|
|
|
|
ifat, err := ifi.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ifa := range ifat {
|
|
|
|
switch v := ifa.(type) {
|
|
|
|
case *IPAddr:
|
|
|
|
if ip.Equal(v.IP) {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
|
|
|
case *IPNet:
|
|
|
|
if ip.Equal(v.IP) {
|
|
|
|
return &ifi, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ip.Equal(IPv4zero) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, errNoSuchInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func interfaceToIPv4Addr(ifi *Interface) (IP, error) {
|
|
|
|
if ifi == nil {
|
|
|
|
return IPv4zero, nil
|
|
|
|
}
|
|
|
|
ifat, err := ifi.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ifa := range ifat {
|
|
|
|
switch v := ifa.(type) {
|
|
|
|
case *IPAddr:
|
|
|
|
if v.IP.To4() != nil {
|
|
|
|
return v.IP, nil
|
|
|
|
}
|
|
|
|
case *IPNet:
|
|
|
|
if v.IP.To4() != nil {
|
|
|
|
return v.IP, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errNoSuchInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIPv4MreqToInterface(mreq *syscall.IPMreq, ifi *Interface) error {
|
|
|
|
if ifi == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ifat, err := ifi.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ifa := range ifat {
|
|
|
|
switch v := ifa.(type) {
|
|
|
|
case *IPAddr:
|
|
|
|
if a := v.IP.To4(); a != nil {
|
|
|
|
copy(mreq.Interface[:], a)
|
|
|
|
goto done
|
|
|
|
}
|
|
|
|
case *IPNet:
|
|
|
|
if a := v.IP.To4(); a != nil {
|
|
|
|
copy(mreq.Interface[:], a)
|
|
|
|
goto done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2012-03-05 14:36:05 -07:00
|
|
|
if bytesEqual(mreq.Multiaddr[:], IPv4zero.To4()) {
|
2012-01-10 17:53:32 -07:00
|
|
|
return errNoSuchMulticastInterface
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReadBuffer(fd *netFD, bytes int) error {
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
if err := fd.incref(); err != nil {
|
2012-02-13 22:40:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
defer fd.decref()
|
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setWriteBuffer(fd *netFD, bytes int) error {
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
if err := fd.incref(); err != nil {
|
2012-02-13 22:40:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
defer fd.decref()
|
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setKeepAlive(fd *netFD, keepalive bool) error {
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
if err := fd.incref(); err != nil {
|
2012-02-13 22:40:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
defer fd.decref()
|
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setLinger(fd *netFD, sec int) error {
|
|
|
|
var l syscall.Linger
|
|
|
|
if sec >= 0 {
|
|
|
|
l.Onoff = 1
|
|
|
|
l.Linger = int32(sec)
|
|
|
|
} else {
|
|
|
|
l.Onoff = 0
|
|
|
|
l.Linger = 0
|
|
|
|
}
|
net: add special netFD mutex
The mutex, fdMutex, handles locking and lifetime of sysfd,
and serializes Read and Write methods.
This allows to strip 2 sync.Mutex.Lock calls,
2 sync.Mutex.Unlock calls, 1 defer and some amount
of misc overhead from every network operation.
On linux/amd64, Intel E5-2690:
benchmark old ns/op new ns/op delta
BenchmarkTCP4Persistent 9595 9454 -1.47%
BenchmarkTCP4Persistent-2 8978 8772 -2.29%
BenchmarkTCP4ConcurrentReadWrite 4900 4625 -5.61%
BenchmarkTCP4ConcurrentReadWrite-2 2603 2500 -3.96%
In general it strips 70-500 ns from every network operation depending
on processor model. On my relatively new E5-2690 it accounts to ~5%
of network op cost.
Fixes #6074.
R=golang-dev, bradfitz, alex.brainman, iant, mikioh.mikioh
CC=golang-dev
https://golang.org/cl/12418043
2013-08-09 11:43:00 -06:00
|
|
|
if err := fd.incref(); err != nil {
|
2012-02-13 22:40:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2012-01-10 17:53:32 -07:00
|
|
|
defer fd.decref()
|
2014-05-12 09:48:19 -06:00
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptLinger(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_LINGER, &l))
|
2012-01-10 17:53:32 -07:00
|
|
|
}
|