2015-09-18 21:56:27 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2015-10-01 15:31:47 +00:00
|
|
|
"errors"
|
2015-09-18 21:56:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-09-23 03:56:31 +00:00
|
|
|
"net"
|
2015-09-18 21:56:27 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-09-23 15:12:56 +00:00
|
|
|
// NetworkFingerprint is used to fingerprint the Network capabilities of a node
|
|
|
|
type NetworkFingerprint struct {
|
2015-11-05 21:46:02 +00:00
|
|
|
StaticFingerprinter
|
2015-10-28 22:58:40 +00:00
|
|
|
logger *log.Logger
|
|
|
|
interfaceDetector NetworkInterfaceDetector
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:05:58 +00:00
|
|
|
// An interface to isolate calls to various api in net package
|
|
|
|
// This facilitates testing where we can implement
|
|
|
|
// fake interfaces and addresses to test varios code paths
|
2015-10-28 22:58:40 +00:00
|
|
|
type NetworkInterfaceDetector interface {
|
|
|
|
Interfaces() ([]net.Interface, error)
|
|
|
|
InterfaceByName(name string) (*net.Interface, error)
|
2015-10-29 18:01:15 +00:00
|
|
|
Addrs(intf *net.Interface) ([]net.Addr, error)
|
2015-10-28 22:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 18:05:58 +00:00
|
|
|
// Implements the interface detector which calls net directly
|
2015-10-29 22:55:49 +00:00
|
|
|
type DefaultNetworkInterfaceDetector struct {
|
2015-10-28 22:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 22:55:49 +00:00
|
|
|
func (b *DefaultNetworkInterfaceDetector) Interfaces() ([]net.Interface, error) {
|
2015-10-28 22:58:40 +00:00
|
|
|
return net.Interfaces()
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:55:49 +00:00
|
|
|
func (b *DefaultNetworkInterfaceDetector) InterfaceByName(name string) (*net.Interface, error) {
|
2015-10-28 22:58:40 +00:00
|
|
|
return net.InterfaceByName(name)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 22:55:49 +00:00
|
|
|
func (b *DefaultNetworkInterfaceDetector) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
2015-10-29 18:01:15 +00:00
|
|
|
return intf.Addrs()
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:12:30 +00:00
|
|
|
// NewNetworkFingerprint returns a new NetworkFingerprinter with the given
|
2015-09-23 15:12:56 +00:00
|
|
|
// logger
|
2016-03-23 00:12:30 +00:00
|
|
|
func NewNetworkFingerprint(logger *log.Logger) Fingerprint {
|
2015-10-29 22:55:49 +00:00
|
|
|
f := &NetworkFingerprint{logger: logger, interfaceDetector: &DefaultNetworkInterfaceDetector{}}
|
2015-09-18 21:56:27 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
2015-09-23 04:22:23 +00:00
|
|
|
// newNetwork is populated and addded to the Nodes resources
|
|
|
|
newNetwork := &structs.NetworkResource{}
|
2015-10-28 18:11:13 +00:00
|
|
|
var ip string
|
2015-10-26 15:11:39 +00:00
|
|
|
|
2015-10-28 22:48:08 +00:00
|
|
|
intf, err := f.findInterface(cfg.NetworkInterface)
|
2015-10-28 18:11:13 +00:00
|
|
|
if err != nil {
|
2015-10-28 22:03:11 +00:00
|
|
|
return false, fmt.Errorf("Error while detecting network interface during fingerprinting: %v", err)
|
2015-10-28 18:11:13 +00:00
|
|
|
}
|
2015-10-25 14:30:56 +00:00
|
|
|
|
2015-11-19 03:01:39 +00:00
|
|
|
// No interface could be found
|
|
|
|
if intf == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-10-28 18:11:13 +00:00
|
|
|
if ip, err = f.ipAddress(intf); err != nil {
|
2015-10-28 22:03:11 +00:00
|
|
|
return false, fmt.Errorf("Unable to find IP address of interface: %s, err: %v", intf.Name, err)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 18:11:13 +00:00
|
|
|
newNetwork.Device = intf.Name
|
2016-01-23 02:12:16 +00:00
|
|
|
node.Attributes["unique.network.ip-address"] = ip
|
2015-10-28 18:11:13 +00:00
|
|
|
newNetwork.IP = ip
|
|
|
|
newNetwork.CIDR = newNetwork.IP + "/32"
|
|
|
|
|
2015-10-29 23:16:10 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.network: Detected interface %v with IP %v during fingerprinting", intf.Name, ip)
|
2015-10-28 18:11:13 +00:00
|
|
|
|
|
|
|
if throughput := f.linkSpeed(intf.Name); throughput > 0 {
|
2015-09-23 04:22:23 +00:00
|
|
|
newNetwork.MBits = throughput
|
2015-10-03 00:32:11 +00:00
|
|
|
} else {
|
2015-10-03 00:23:58 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.network: Unable to read link speed; setting to default %v", cfg.NetworkSpeed)
|
2015-10-03 00:32:11 +00:00
|
|
|
newNetwork.MBits = cfg.NetworkSpeed
|
2015-09-23 04:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node.Resources == nil {
|
|
|
|
node.Resources = &structs.Resources{}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 04:22:23 +00:00
|
|
|
node.Resources.Networks = append(node.Resources.Networks, newNetwork)
|
|
|
|
|
2015-09-18 21:56:27 +00:00
|
|
|
// return true, because we have a network connection
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeed(device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
// Use LookPath to find the ethtool in the systems $PATH
|
|
|
|
// If it's not found or otherwise errors, LookPath returns and empty string
|
|
|
|
// and an error we can ignore for our purposes
|
|
|
|
ethtoolPath, _ := exec.LookPath("ethtool")
|
|
|
|
if ethtoolPath != "" {
|
2015-09-23 03:04:20 +00:00
|
|
|
if speed := f.linkSpeedEthtool(ethtoolPath, device); speed > 0 {
|
2015-09-18 21:56:27 +00:00
|
|
|
return speed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back on checking a system file for link speed.
|
2015-09-22 20:59:00 +00:00
|
|
|
return f.linkSpeedSys(device)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeedSys parses link speed in Mb/s from /sys.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeedSys(device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
path := fmt.Sprintf("/sys/class/net/%s/speed", device)
|
|
|
|
|
|
|
|
// Read contents of the device/speed file
|
|
|
|
content, err := ioutil.ReadFile(path)
|
2015-09-28 23:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to read link speed from %s", path)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
lines := strings.Split(string(content), "\n")
|
|
|
|
mbs, err := strconv.Atoi(lines[0])
|
|
|
|
if err != nil || mbs <= 0 {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse link speed from %s", path)
|
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-28 23:23:30 +00:00
|
|
|
|
|
|
|
return mbs
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:33:11 +00:00
|
|
|
// linkSpeedEthtool determines link speed in Mb/s with 'ethtool'.
|
2015-09-23 15:12:56 +00:00
|
|
|
func (f *NetworkFingerprint) linkSpeedEthtool(path, device string) int {
|
2015-09-18 21:56:27 +00:00
|
|
|
outBytes, err := exec.Command(path, device).Output()
|
2015-09-28 23:23:30 +00:00
|
|
|
if err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Error calling ethtool (%s %s): %v", path, device, err)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
output := strings.TrimSpace(string(outBytes))
|
|
|
|
re := regexp.MustCompile("Speed: [0-9]+[a-zA-Z]+/s")
|
|
|
|
m := re.FindString(output)
|
|
|
|
if m == "" {
|
|
|
|
// no matches found, output may be in a different format
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse Speed in output of '%s %s'", path, device)
|
|
|
|
return 0
|
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
// Split and trim the Mb/s unit from the string output
|
|
|
|
args := strings.Split(m, ": ")
|
|
|
|
raw := strings.TrimSuffix(args[1], "Mb/s")
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-09-28 23:23:30 +00:00
|
|
|
// convert to Mb/s
|
|
|
|
mbs, err := strconv.Atoi(raw)
|
|
|
|
if err != nil || mbs <= 0 {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.network: Unable to parse Mb/s in output of '%s %s'", path, device)
|
|
|
|
return 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-28 23:23:30 +00:00
|
|
|
|
|
|
|
return mbs
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 21:03:33 +00:00
|
|
|
// Gets the ipv4 addr for a network interface
|
2015-10-27 21:14:25 +00:00
|
|
|
func (f *NetworkFingerprint) ipAddress(intf *net.Interface) (string, error) {
|
2015-10-28 22:03:11 +00:00
|
|
|
var addrs []net.Addr
|
|
|
|
var err error
|
|
|
|
|
2015-10-29 18:01:15 +00:00
|
|
|
if addrs, err = f.interfaceDetector.Addrs(intf); err != nil {
|
2015-10-27 21:14:25 +00:00
|
|
|
return "", err
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:14:25 +00:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
return "", errors.New(fmt.Sprintf("Interface %s has no IP address", intf.Name))
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
2015-10-28 20:38:28 +00:00
|
|
|
for _, addr := range addrs {
|
|
|
|
var ip net.IP
|
|
|
|
switch v := (addr).(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = v.IP
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = v.IP
|
|
|
|
}
|
|
|
|
if ip.To4() != nil {
|
2015-10-29 23:16:10 +00:00
|
|
|
return ip.String(), nil
|
2015-10-28 20:38:28 +00:00
|
|
|
}
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
2015-10-28 20:38:28 +00:00
|
|
|
|
2015-10-29 23:16:10 +00:00
|
|
|
return "", fmt.Errorf("Couldn't parse IP address for interface %s", intf.Name)
|
2015-10-28 20:38:28 +00:00
|
|
|
|
2015-10-01 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 21:03:33 +00:00
|
|
|
// Checks if the device is marked UP by the operator
|
2015-10-27 21:14:25 +00:00
|
|
|
func (f *NetworkFingerprint) isDeviceEnabled(intf *net.Interface) bool {
|
|
|
|
return intf.Flags&net.FlagUp != 0
|
|
|
|
}
|
2015-09-28 23:23:30 +00:00
|
|
|
|
2015-10-28 21:03:33 +00:00
|
|
|
// Checks if the device has any IP address configured
|
2015-10-27 21:14:25 +00:00
|
|
|
func (f *NetworkFingerprint) deviceHasIpAddress(intf *net.Interface) bool {
|
2015-10-29 23:16:10 +00:00
|
|
|
_, err := f.ipAddress(intf)
|
|
|
|
return err == nil
|
2015-10-27 21:14:25 +00:00
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2015-10-27 21:14:25 +00:00
|
|
|
func (n *NetworkFingerprint) isDeviceLoopBackOrPointToPoint(intf *net.Interface) bool {
|
2015-10-29 22:14:13 +00:00
|
|
|
return intf.Flags&(net.FlagLoopback|net.FlagPointToPoint) != 0
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-10-28 18:11:13 +00:00
|
|
|
|
2015-10-28 22:48:08 +00:00
|
|
|
// Returns the interface with the name passed by user
|
|
|
|
// If the name is blank then it iterates through all the devices
|
|
|
|
// and finds one which is routable and marked as UP
|
|
|
|
// It excludes PPP and lo devices unless they are specifically asked
|
|
|
|
func (f *NetworkFingerprint) findInterface(deviceName string) (*net.Interface, error) {
|
2015-10-29 22:14:13 +00:00
|
|
|
var interfaces []net.Interface
|
2015-10-28 22:03:11 +00:00
|
|
|
var err error
|
|
|
|
|
2015-10-28 18:11:13 +00:00
|
|
|
if deviceName != "" {
|
2015-10-28 22:58:40 +00:00
|
|
|
return f.interfaceDetector.InterfaceByName(deviceName)
|
2015-10-28 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var intfs []net.Interface
|
|
|
|
|
2015-10-28 22:58:40 +00:00
|
|
|
if intfs, err = f.interfaceDetector.Interfaces(); err != nil {
|
2015-10-28 18:11:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, intf := range intfs {
|
2015-10-28 21:44:46 +00:00
|
|
|
if f.isDeviceEnabled(&intf) && !f.isDeviceLoopBackOrPointToPoint(&intf) && f.deviceHasIpAddress(&intf) {
|
2015-10-29 22:14:13 +00:00
|
|
|
interfaces = append(interfaces, intf)
|
2015-10-28 18:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-28 22:48:08 +00:00
|
|
|
|
|
|
|
if len(interfaces) == 0 {
|
2015-11-19 03:01:39 +00:00
|
|
|
return nil, nil
|
2015-10-28 22:48:08 +00:00
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
return &interfaces[0], nil
|
2015-10-28 18:11:13 +00:00
|
|
|
}
|