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
|
// Copyright © 2016 Zlatko Čalušić
//
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.
package sysinfo
import (
"io/ioutil"
"net"
"os"
"path"
"strconv"
"strings"
"syscall"
"unsafe"
)
// NetworkDevice information.
type NetworkDevice struct {
Name string `json:"name,omitempty"`
Driver string `json:"driver,omitempty"`
MACAddress string `json:"macaddress,omitempty"`
IPAddress []string `json:"ipaddress,omitempty"`
Port string `json:"port,omitempty"`
Speed uint `json:"speed,omitempty"` // device max supported speed in Mbps
MTU int `json:"mtu,omitempty"`
}
func getPortType(supp uint32) (port string) {
for i, p := range [...]string{"tp", "aui", "mii", "fibre", "bnc"} {
if supp&(1<<uint(i+7)) > 0 {
port += p + "/"
}
}
port = strings.TrimRight(port, "/")
return
}
func getMaxSpeed(supp uint32) (speed uint) {
// Fancy, right?
switch {
case supp&0x78000000 > 0:
speed = 56000
case supp&0x07800000 > 0:
speed = 40000
case supp&0x00600000 > 0:
speed = 20000
case supp&0x001c1000 > 0:
speed = 10000
case supp&0x00008000 > 0:
speed = 2500
case supp&0x00020030 > 0:
speed = 1000
case supp&0x0000000c > 0:
speed = 100
case supp&0x00000003 > 0:
speed = 10
}
return
}
func getSupported(name string) uint32 {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_IP)
if err != nil {
return 0
}
defer syscall.Close(fd)
// struct ethtool_cmd from /usr/include/linux/ethtool.h
var ethtool struct {
Cmd uint32
Supported uint32
Advertising uint32
Speed uint16
Duplex uint8
Port uint8
PhyAddress uint8
Transceiver uint8
Autoneg uint8
MdioSupport uint8
Maxtxpkt uint32
Maxrxpkt uint32
SpeedHi uint16
EthTpMdix uint8
Reserved2 uint8
LpAdvertising uint32
Reserved [2]uint32
}
// ETHTOOL_GSET from /usr/include/linux/ethtool.h
const GSET = 0x1
ethtool.Cmd = GSET
// struct ifreq from /usr/include/linux/if.h
var ifr struct {
Name [16]byte
Data uintptr
}
copy(ifr.Name[:], name+"\000")
ifr.Data = uintptr(unsafe.Pointer(ðtool))
// SIOCETHTOOL from /usr/include/linux/sockios.h
const SIOCETHTOOL = 0x8946
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(SIOCETHTOOL), uintptr(unsafe.Pointer(&ifr)))
if errno == 0 {
return ethtool.Supported
}
return 0
}
// readIfaceSpeed reads /sys/class/net/<iface>/speed
// inspired by https://github.com/prometheus/procfs/blob/master/sysfs/net_class.go#L56
func readIfaceSpeed(file string) (speed uint) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
if b, err := ioutil.ReadAll(f); err == nil {
if s, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && s > 0 {
speed = uint(s)
}
}
return
}
func (si *SysInfo) getNetworkInfo() {
sysClassNet := "/sys/class/net"
devices, err := ioutil.ReadDir(sysClassNet)
if err != nil {
return
}
si.Network = make([]NetworkDevice, 0)
for _, link := range devices {
fullpath := path.Join(sysClassNet, link.Name())
dev, err := os.Readlink(fullpath)
if err != nil {
continue
}
if strings.HasPrefix(dev, "../../devices/virtual/net/lo") ||
strings.HasPrefix(dev, "../../devices/virtual/net/dummy") {
continue
}
supp := getSupported(link.Name())
// get IP Address(es) of interface
inet, err := net.InterfaceByName(link.Name())
var ip_addrs []string
var mtu int
if err == nil {
ip_addrs, _ = getIPAddrByInterface(inet)
mtu = inet.MTU
}
speed := readIfaceSpeed(path.Join(fullpath, "speed"))
if speed == 0 {
speed = getMaxSpeed(supp)
}
device := NetworkDevice{
Name: link.Name(),
MACAddress: slurpFile(path.Join(fullpath, "address")),
IPAddress: ip_addrs,
Port: getPortType(supp),
Speed: speed,
MTU: mtu,
}
if driver, err := os.Readlink(path.Join(fullpath, "device", "driver")); err == nil {
device.Driver = path.Base(driver)
}
si.Network = append(si.Network, device)
}
}
func getIPAddrByInterface(inet *net.Interface) ([]string, error) {
ip_addrs := make([]string, 0)
addrs, err := inet.Addrs()
if err != nil {
return ip_addrs, err
}
for _, addr := range addrs {
ip_addrs = append(ip_addrs, addr.String())
}
return ip_addrs, err
}
|