Debian Codegraph @

lib/rethtool/interface_settings.rb

raw symbols SCIP
1 require 'rethtool'
2 require 'rethtool/ethtool_cmd'
3
4 # All of the settings of a network interface. Ridiculous amounts of
5 # info is available; we only support a subset of them at present.
6 #
7 # Create an instance of this class with the interface name as the only
8 # parameter, then use the available instance methods to get the info you
9 # seek:
10 #
11 # if = Rethtool::InterfaceSettings.new("eth0")
12 # puts "Current link mode is #{if.current_mode}"
13 #
14 class Rethtool::InterfaceSettings
15 Mode = Struct.new(:speed, :duplex, :media)
16
17 # A struct to represent interface modes (supported, advertised, current)
18 #
19 # available fields are:
20 #
21 # .speed -- integer link speed, in Mb (-1 if unknown)
22 # .duplex -- :full, :half, :fec, or :unknown
23 # .media -- A string, such as 'T', 'X', 'KX', etc, or nil if unknown
24 #
25 class Mode
26 # Print out a more standard-looking representation for a mode
27 def to_s
28 if self.speed == :unknown
29 "Unknown"
30 else
31 "#{self.speed}base#{self.media}/#{self.duplex}"
32 end
33 end
34 end
35
36 # Create a new InterfaceSettings object. Simply pass it the name of the
37 # interface you want to get the settings for.
38 def initialize(interface)
39 @interface = interface
40 cmd = Rethtool::EthtoolCmd.new
41 cmd.cmd = Rethtool::ETHTOOL_CMD_GSET
42
43 @data = Rethtool.ioctl(interface, cmd)
44 end
45
46 # Return an array of the modes supported by the interface. Returns an
47 # array of Mode objects.
48 def supported_modes
49 modes(@data.supported)
50 end
51
52 # Return an array of the modes advertised by the interface. Returns an
53 # array of Mode objects. If you know the difference between 'supported'
54 # and 'advertised', you're one up on me.
55 def advertised_modes
56 modes(@data.advertising)
57 end
58
59 # Return a Mode object representing the current detected mode of the
60 # interface.
61 def current_mode
62 speed = @data.speed
63 speed = :unknown if speed == 65535
64
65 duplex = case @data.duplex
66 when 0 then :half
67 when 1 then :full
68 else :unknown
69 end
70
71 port = case @data.port
72 when 0 then 'T'
73 when 1 then 'AUI'
74 when 2 then 'MII'
75 when 3 then 'F'
76 when 4 then 'BNC'
77 when 255 then 'Other'
78 else 'Unknown'
79 end
80
81 Mode.new(speed, duplex, port)
82 end
83
84 # Return the "best" possible mode supported by this interface.
85 # This is the highest speed mode with the "best" duplex
86 # (fec > full > half).
87 def best_mode
88 modes = self.advertised_modes
89 best_speed = modes.map { |m| m.speed }.sort.last
90 high_speed_modes = modes.find_all { |m| m.speed == best_speed }
91
92 # Somewhere recently, RHEL decided to release a kernel or libc update
93 # that changes the behaviour of the ethtool ioctl so that instead of
94 # returning EOPNOTSUPP when you ask for available speeds on an interface
95 # that doesn't support that (like bonded NICs), it now returns success with
96 # an empty list. WHO DOES THAT SORT OF SHIT?!? So we've got to fake it
97 # ourselves.
98 raise Errno::EOPNOTSUPP.new("#{@interface} doesn't support enumerating speed modes") if modes.empty?
99
100 if high_speed_modes.length == 0
101 raise RuntimeError.new("Can't happen: no modes with the best speed?!?")
102 elsif high_speed_modes.length == 1
103 high_speed_modes.first
104 else
105 duplexes = high_speed_modes.map { |m| m.duplex }
106 best_duplex = if duplexes.include? :fec
107 :fec
108 elsif duplexes.include? :full
109 :full
110 else
111 :half
112 end
113 high_speed_modes.find { |m| m.duplex == best_duplex }
114 end
115 end
116
117 private
118
119 PossibleModes = {
120 1 << 0 => Mode.new(10, :half, 'T'),
121 1 << 1 => Mode.new(10, :full, 'T'),
122 1 << 2 => Mode.new(100, :half, 'T'),
123 1 << 3 => Mode.new(100, :full, 'T'),
124 1 << 4 => Mode.new(1000, :half, 'T'),
125 1 << 5 => Mode.new(1000, :full, 'T'),
126 1 << 12 => Mode.new(10000, :full, 'T'),
127 1 << 15 => Mode.new(2500, :full, 'X'),
128 1 << 17 => Mode.new(1000, :full, 'KX'),
129 1 << 18 => Mode.new(10000, :full, 'KX4'),
130 1 << 19 => Mode.new(10000, :full, 'KR'),
131 1 << 20 => Mode.new(10000, :fec, 'R')
132 }
133
134 # Turn a uint32 of bits into a list of supported modes. Sigh.
135 def modes(data)
136 PossibleModes.find_all { |m| (m[0] & data) > 0 }.map { |m| m[1] }
137 end
138 end