1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:24:39 -07:00
go/src/net/interface.go

228 lines
6.1 KiB
Go
Raw Normal View History

// Copyright 2011 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.
package net
import (
"errors"
"sync"
"time"
)
// BUG(mikio): On NaCl, Plan9 and Solaris, methods and functions
// related to Interface are not implemented.
// BUG(mikio): On DragonFly BSD, NetBSD and OpenBSD, the
// MulticastAddrs method of Interface is not implemented.
var (
errInvalidInterface = errors.New("invalid network interface")
errInvalidInterfaceIndex = errors.New("invalid network interface index")
errInvalidInterfaceName = errors.New("invalid network interface name")
errNoSuchInterface = errors.New("no such network interface")
errNoSuchMulticastInterface = errors.New("no such multicast network interface")
)
// Interface represents a mapping between network interface name
// and index. It also represents network interface facility
// information.
type Interface struct {
Index int // positive integer that starts at one, zero is never used
MTU int // maximum transmission unit
Name string // e.g., "en0", "lo0", "eth0.100"
HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
Flags Flags // e.g., FlagUp, FlagLoopback, FlagMulticast
}
type Flags uint
const (
FlagUp Flags = 1 << iota // interface is up
FlagBroadcast // interface supports broadcast access capability
FlagLoopback // interface is a loopback interface
FlagPointToPoint // interface belongs to a point-to-point link
FlagMulticast // interface supports multicast access capability
)
var flagNames = []string{
"up",
"broadcast",
"loopback",
"pointtopoint",
"multicast",
}
func (f Flags) String() string {
s := ""
for i, name := range flagNames {
if f&(1<<uint(i)) != 0 {
if s != "" {
s += "|"
}
s += name
}
}
if s == "" {
s = "0"
}
return s
}
// Addrs returns interface addresses for a specific interface.
func (ifi *Interface) Addrs() ([]Addr, error) {
if ifi == nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
}
ifat, err := interfaceAddrTable(ifi)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifat, err
}
// MulticastAddrs returns multicast, joined group addresses for
// a specific interface.
func (ifi *Interface) MulticastAddrs() ([]Addr, error) {
if ifi == nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface}
}
ifat, err := interfaceMulticastAddrTable(ifi)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifat, err
}
// Interfaces returns a list of the system's network interfaces.
func Interfaces() ([]Interface, error) {
ift, err := interfaceTable(0)
if err != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
if len(ift) != 0 {
zoneCache.update(ift)
}
return ift, nil
}
// InterfaceAddrs returns a list of the system's network interface
// addresses.
func InterfaceAddrs() ([]Addr, error) {
ifat, err := interfaceAddrTable(nil)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifat, err
}
// InterfaceByIndex returns the interface specified by index.
func InterfaceByIndex(index int) (*Interface, error) {
if index <= 0 {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceIndex}
}
ift, err := interfaceTable(index)
if err != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
ifi, err := interfaceByIndex(ift, index)
if err != nil {
err = &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
return ifi, err
net: fix slow network interface manipulations This CL reduces unnecessary network facility lookups introduced by recent changes below. changeset: 15798:53a4da6a4f4a net: return correct point-to-point interface address on linux changeset: 15799:a81ef8e0cc05 net: set up IPv6 scoped addressing zone for network facilities Also adds a test case for issue 4839. Benchmark results on linux/amd64, virtual machine: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 80487 80382 -0.13% BenchmarkInterfaceByIndex-2 72013 71391 -0.86% BenchmarkInterfaceByName-2 79865 80101 +0.30% BenchmarkInterfaceAddrs-2 42071 829677 +1872.09% BenchmarkInterfacesAndAddrs-2 35016 607622 +1635.27% BenchmarkInterfacesAndMulticastAddrs-2 169849 169082 -0.45% old: 15797:9c3930413c1b, new: tip Benchmark results on linux/amd64, virtual machine: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 80487 81459 +1.21% BenchmarkInterfaceByIndex-2 72013 71512 -0.70% BenchmarkInterfaceByName-2 79865 80567 +0.88% BenchmarkInterfaceAddrs-2 42071 120108 +185.49% BenchmarkInterfacesAndAddrs-2 35016 33259 -5.02% BenchmarkInterfacesAndMulticastAddrs-2 169849 82391 -51.49% old: 15797:9c3930413c1b, new: tip+CL7400055 Benchmark results on darwin/amd64: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 34402 34231 -0.50% BenchmarkInterfaceByIndex-2 13192 12956 -1.79% BenchmarkInterfaceByName-2 34791 34388 -1.16% BenchmarkInterfaceAddrs-2 36565 63906 +74.77% BenchmarkInterfacesAndAddrs-2 17497 31068 +77.56% BenchmarkInterfacesAndMulticastAddrs-2 25276 66711 +163.93% old: 15797:9c3930413c1b, new: tip Benchmark results on darwin/amd64: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 34402 31854 -7.41% BenchmarkInterfaceByIndex-2 13192 12950 -1.83% BenchmarkInterfaceByName-2 34791 31926 -8.23% BenchmarkInterfaceAddrs-2 36565 42144 +15.26% BenchmarkInterfacesAndAddrs-2 17497 17329 -0.96% BenchmarkInterfacesAndMulticastAddrs-2 25276 24870 -1.61% old: 15797:9c3930413c1b, new: tip+CL7400055 Update #4234. Fixes #4839 (again). Fixes #4866. R=golang-dev, fullung CC=golang-dev https://golang.org/cl/7400055
2013-02-27 22:58:41 -07:00
}
func interfaceByIndex(ift []Interface, index int) (*Interface, error) {
for _, ifi := range ift {
net: fix slow network interface manipulations This CL reduces unnecessary network facility lookups introduced by recent changes below. changeset: 15798:53a4da6a4f4a net: return correct point-to-point interface address on linux changeset: 15799:a81ef8e0cc05 net: set up IPv6 scoped addressing zone for network facilities Also adds a test case for issue 4839. Benchmark results on linux/amd64, virtual machine: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 80487 80382 -0.13% BenchmarkInterfaceByIndex-2 72013 71391 -0.86% BenchmarkInterfaceByName-2 79865 80101 +0.30% BenchmarkInterfaceAddrs-2 42071 829677 +1872.09% BenchmarkInterfacesAndAddrs-2 35016 607622 +1635.27% BenchmarkInterfacesAndMulticastAddrs-2 169849 169082 -0.45% old: 15797:9c3930413c1b, new: tip Benchmark results on linux/amd64, virtual machine: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 80487 81459 +1.21% BenchmarkInterfaceByIndex-2 72013 71512 -0.70% BenchmarkInterfaceByName-2 79865 80567 +0.88% BenchmarkInterfaceAddrs-2 42071 120108 +185.49% BenchmarkInterfacesAndAddrs-2 35016 33259 -5.02% BenchmarkInterfacesAndMulticastAddrs-2 169849 82391 -51.49% old: 15797:9c3930413c1b, new: tip+CL7400055 Benchmark results on darwin/amd64: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 34402 34231 -0.50% BenchmarkInterfaceByIndex-2 13192 12956 -1.79% BenchmarkInterfaceByName-2 34791 34388 -1.16% BenchmarkInterfaceAddrs-2 36565 63906 +74.77% BenchmarkInterfacesAndAddrs-2 17497 31068 +77.56% BenchmarkInterfacesAndMulticastAddrs-2 25276 66711 +163.93% old: 15797:9c3930413c1b, new: tip Benchmark results on darwin/amd64: benchmark old ns/op new ns/op delta BenchmarkInterfaces-2 34402 31854 -7.41% BenchmarkInterfaceByIndex-2 13192 12950 -1.83% BenchmarkInterfaceByName-2 34791 31926 -8.23% BenchmarkInterfaceAddrs-2 36565 42144 +15.26% BenchmarkInterfacesAndAddrs-2 17497 17329 -0.96% BenchmarkInterfacesAndMulticastAddrs-2 25276 24870 -1.61% old: 15797:9c3930413c1b, new: tip+CL7400055 Update #4234. Fixes #4839 (again). Fixes #4866. R=golang-dev, fullung CC=golang-dev https://golang.org/cl/7400055
2013-02-27 22:58:41 -07:00
if index == ifi.Index {
return &ifi, nil
}
}
return nil, errNoSuchInterface
}
// InterfaceByName returns the interface specified by name.
func InterfaceByName(name string) (*Interface, error) {
if name == "" {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterfaceName}
}
ift, err := interfaceTable(0)
if err != nil {
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err}
}
if len(ift) != 0 {
zoneCache.update(ift)
}
for _, ifi := range ift {
if name == ifi.Name {
return &ifi, nil
}
}
return nil, &OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errNoSuchInterface}
}
// An ipv6ZoneCache represents a cache holding partial network
// interface information. It is used for reducing the cost of IPv6
// addressing scope zone resolution.
type ipv6ZoneCache struct {
sync.RWMutex // guard the following
lastFetched time.Time // last time routing information was fetched
toIndex map[string]int // interface name to its index
toName map[int]string // interface index to its name
}
var zoneCache = ipv6ZoneCache{
toIndex: make(map[string]int),
toName: make(map[int]string),
}
func (zc *ipv6ZoneCache) update(ift []Interface) {
zc.Lock()
defer zc.Unlock()
now := time.Now()
if zc.lastFetched.After(now.Add(-60 * time.Second)) {
return
}
zc.lastFetched = now
if len(ift) == 0 {
var err error
if ift, err = interfaceTable(0); err != nil {
return
}
}
zc.toIndex = make(map[string]int, len(ift))
zc.toName = make(map[int]string, len(ift))
for _, ifi := range ift {
zc.toIndex[ifi.Name] = ifi.Index
zc.toName[ifi.Index] = ifi.Name
}
}
func zoneToString(zone int) string {
if zone == 0 {
return ""
}
zoneCache.update(nil)
zoneCache.RLock()
defer zoneCache.RUnlock()
name, ok := zoneCache.toName[zone]
if !ok {
name = uitoa(uint(zone))
}
return name
}
func zoneToInt(zone string) int {
if zone == "" {
return 0
}
zoneCache.update(nil)
zoneCache.RLock()
defer zoneCache.RUnlock()
index, ok := zoneCache.toIndex[zone]
if !ok {
index, _, _ = dtoi(zone)
}
return index
}