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
45
# 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)

	@driver_info = Rethtool::DriverSettings.new(interface)
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.



66
67
68
# File 'lib/rethtool/interface_settings.rb', line 66

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)


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

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

#bus_infoObject

Returns a string with the bus information of the interface.



53
54
55
# File 'lib/rethtool/interface_settings.rb', line 53

def bus_info
		@driver_info.bus_info
end

#current_modeObject

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rethtool/interface_settings.rb', line 72

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

#driverObject

Returns a string with the value of the interface driver (kernel module).



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

def driver
		@driver_info.driver
end

#supported_modesObject

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



59
60
61
# File 'lib/rethtool/interface_settings.rb', line 59

def supported_modes
	modes(@data.supported)
end