Class: Rethtool::InterfaceSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/rethtool/interface_settings.rb

Overview

All of the settings of a network interface. Ridiculous amounts of info is available; we only support a subset of them at present.

Create an instance of this class with the interface name as the only parameter, then use the available instance methods to get the info you seek:

if = Rethtool::InterfaceSettings.new("eth0")
puts "Current link mode is #{if.current_mode}"

Defined Under Namespace

Classes: Mode

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ InterfaceSettings

Create a new InterfaceSettings object. Simply pass it the name of the interface you want to get the settings for.



38
39
40
41
42
43
44
# File 'lib/rethtool/interface_settings.rb', line 38

def initialize(interface)
  @interface = interface
  cmd = Rethtool::EthtoolCmd.new
  cmd.cmd = Rethtool::ETHTOOL_CMD_GSET
  
  @data = Rethtool.ioctl(interface, cmd)
end

Instance Method Details

#advertised_modesObject

Return an array of the modes advertised by the interface. Returns an array of Mode objects. If you know the difference between ‘supported’ and ‘advertised’, you’re one up on me.



55
56
57
# File 'lib/rethtool/interface_settings.rb', line 55

def advertised_modes
  modes(@data.advertising)
end

#best_modeObject

Return the “best” possible mode supported by this interface. This is the highest speed mode with the “best” duplex (fec > full > half).

Raises:

  • (Errno::EOPNOTSUPP)


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
# File 'lib/rethtool/interface_settings.rb', line 87

def best_mode
  modes = self.advertised_modes
  best_speed = modes.map { |m| m.speed }.sort.last
  high_speed_modes = modes.find_all { |m| m.speed == best_speed }

  # Somewhere recently, RHEL decided to release a kernel or libc update
  # that changes the behaviour of the ethtool ioctl so that instead of
  # returning EOPNOTSUPP when you ask for available speeds on an interface
  # that doesn't support that (like bonded NICs), it now returns success with
  # an empty list.  WHO DOES THAT SORT OF SHIT?!?  So we've got to fake it
  # ourselves.
  raise Errno::EOPNOTSUPP.new("#{@interface} doesn't support enumerating speed modes") if modes.empty?

  if high_speed_modes.length == 0
    raise RuntimeError.new("Can't happen: no modes with the best speed?!?")
  elsif high_speed_modes.length == 1
    high_speed_modes.first
  else
    duplexes = high_speed_modes.map { |m| m.duplex }
    best_duplex = if duplexes.include? :fec
      :fec
    elsif duplexes.include? :full
      :full
    else
      :half
    end
    high_speed_modes.find { |m| m.duplex == best_duplex }
  end
end

#current_modeObject

Return a Mode object representing the current detected mode of the interface.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rethtool/interface_settings.rb', line 61

def current_mode
  speed = @data.speed
  speed = :unknown if speed == 65535
  
  duplex = case @data.duplex
    when 0 then :half
    when 1 then :full
    else        :unknown
  end
  
  port = case @data.port
    when 0   then 'T'
    when 1   then 'AUI'
    when 2   then 'MII'
    when 3   then 'F'
    when 4   then 'BNC'
    when 255 then 'Other'
    else          'Unknown'
  end
  
  Mode.new(speed, duplex, port)
end

#supported_modesObject

Return an array of the modes supported by the interface. Returns an array of Mode objects.



48
49
50
# File 'lib/rethtool/interface_settings.rb', line 48

def supported_modes
  modes(@data.supported)
end