Debian Codegraph @

perllib/qosient/config/interfaces.pm

raw
1 package qosient::config::interfaces;
2
3 use strict;
4 use Exporter;
5 use vars qw($VERSION @ISA @EXPORT);
6 use Parse::DMIDecode qw();
7 use Scalar::Util qw(looks_like_number);
8
9 $VERSION = "1.00";
10 @ISA = qw(Exporter);
11 @EXPORT = qw(find_pci_ethdevs
12 index_by_slotport
13 is_collector_monitor_device
14 is_sensor_monitor_device
15 find_monitor_ethdevs);
16
17 my $PCI_VENDOR_INTEL='0x8086';
18 my $PCI_DEVICE_INTEL_I350='0x1521';
19 my $PCI_DEVICE_INTEL_X710='0x1572';
20
21 # returns a hash reference containing: name, busaddr, vendor, device,
22 # acpi_index
23 sub device_info {
24 my ($devname) = @_;
25 my $sysfs = '/sys/class/net/' . $devname;
26 my $fh;
27 my %hash = ();
28 my @attrs = ( '/vendor', '/device', '/acpi_index' );
29
30 my $devlink = readlink $sysfs;
31 if ( !defined $devlink ) {
32 return;
33 }
34
35 if ( ! ( $devlink =~ /devices.pci/ ) ) {
36 # only deal with pci devices from here
37 return;
38 }
39
40 my @devlinkparts = split '/', $devlink;
41 my $busaddr = $devlinkparts[ $#devlinkparts - 2 ];
42
43 $hash{'name'} = $devname;
44 $hash{'busaddr'} = $busaddr;
45 for my $attr (@attrs) {
46 my $filename = $sysfs . "/device" . $attr;
47 if ( !open( $fh, '<', $filename ) ) {
48 next;
49 }
50 my $line = <$fh>;
51 close $fh;
52 chomp $line;
53 $hash{substr $attr, 1} = $line;
54 }
55 return \%hash;
56 }
57
58 # Use dmidecode(8) to gather vendor-supplied information about the PCIe slots.
59 # Return a hash reference with bus-address -> slot-number mappings.
60 sub find_pci_slotnums {
61 my $dmi = Parse::DMIDecode->new( nowarnings => 1 );
62 $dmi->probe;
63
64 my %hash = ();
65
66 for my $other ($dmi->get_handles(group => "slot")) {
67 my $desig = $other->keyword("slot-designation");
68
69 if ( ! ( $desig =~ /^PCIe Slot / ) ) {
70 next;
71 }
72
73 $desig =~ s/PCIe Slot //;
74 if ( ! looks_like_number($desig) ) {
75 next;
76 }
77
78 my $busaddr = $other->keyword("slot-bus-address");
79 $busaddr =~ s/\.[0-9]+$//; # remove the function number
80 $hash{$busaddr} = $desig; # map bus address -> slot number
81 }
82 return \%hash;
83 }
84
85 # Dig around in the /sys/class/net for network interfaces and build a hash
86 # table, indexed by PCI bus address, that contains hash references as
87 # returned by device_info(). Then get vendor-supplied slot descriptions
88 # from dmidecode via find_pci_slotnums() and add a slot number, if available,
89 # to each device info hash.
90 sub find_pci_ethdevs {
91 my ($systypename) = @_;
92 my %pcidevs = ();
93
94 for my $path (glob q{/sys/class/net/*}) {
95 my $devname;
96
97 ($devname = $path) =~ s/^.*\///;
98 my $dinfo = device_info( $devname );
99
100 if ( !defined $dinfo ) {
101 next;
102 }
103
104 if ( exists $dinfo->{'acpi_index'} ) {
105 $dinfo->{'port'} = $dinfo->{'acpi_index'};
106 } else {
107 my $function = $dinfo->{'busaddr'};
108 $function =~ s/^.*\.//;
109 $dinfo->{'port'} = $function; # use function number for "port" number
110 }
111 $pcidevs{ $dinfo->{'busaddr'} } = $dinfo;
112 }
113
114 my $slotnums = find_pci_slotnums();
115 if ( !defined $slotnums ) {
116 goto OUT;
117 }
118
119 # find_pci_slotnums indexes the hash by bus address, excluding the zero
120 # function number. device_info() includes a function number for
121 # the ethernet device. Knock the function numbers off of the device info and
122 # compare. If the rest of the bus address matches, add the slot number
123 # to the hash created by device_info.
124 for my $busaddr (keys %$slotnums) {
125 for my $ethdev_busaddr (keys %pcidevs) {
126 my $ethdev_nofunc = $ethdev_busaddr;
127
128 $ethdev_nofunc =~ s/\.[0-9]+$//;
129 if ( $busaddr eq $ethdev_nofunc ) {
130 $pcidevs{$ethdev_busaddr}->{'slot'} = $slotnums->{$busaddr};
131 }
132 }
133 }
134
135 # One more pass: look for entries that do not have a slot number defined
136 # and add one claiming to be in slot zero.
137 for my $ethdev_busaddr (keys %pcidevs) {
138 if ( !exists $pcidevs{$ethdev_busaddr}->{'slot'} ) {
139 $pcidevs{$ethdev_busaddr}->{'slot'} = '0';
140 }
141 }
142
143 OUT: return \%pcidevs;
144 }
145
146 # Starting with the hash table, indexed by pci bus address, generated
147 # by pci_find_ethdevs(), build a nested hash tables of pci ethernet
148 # devices indexed by slot (outer hash table) and then indexed by port
149 # (inner hash table).
150 sub index_by_slotport {
151 my ($href) = @_;
152 my %slothash = ();
153
154 for my $busaddr (keys %$href) {
155 my $slot = $href->{$busaddr}->{'slot'};
156 my $port = $href->{$busaddr}->{'port'};
157
158 if ( !exists $slothash{$slot} ) {
159 my %porthash = ();
160
161 $slothash{$slot} = \%porthash;
162 }
163 $slothash{$slot}->{$port} = $href->{$busaddr};
164 }
165 return \%slothash;
166 }
167
168 # need a third interface type? collection?
169 sub is_collector_monitor_device {
170 my ($dinfo) = @_;
171
172 # For a collector, any PCI expansion card that is an intel x710 or
173 # i350 is used to collect flow records from a sensor.
174
175 if ( ! $dinfo->{'vendor'} eq $PCI_VENDOR_INTEL ) {
176 # all monitor devices are either intel or napatech
177 return;
178 }
179
180 if ( defined $dinfo->{'acpi_index'} ) {
181 if ( $dinfo->{'device'} eq $PCI_DEVICE_INTEL_I350 ) {
182 return;
183 }
184 }
185
186 if ( ! ( $dinfo->{'device'} eq ${PCI_DEVICE_INTEL_I350} ||
187 $dinfo->{'device'} eq ${PCI_DEVICE_INTEL_X710} ) ) {
188 return;
189 }
190
191 return 1;
192 }
193
194 sub is_sensor_monitor_device {
195 my ($dinfo) = @_;
196
197 # All on-board devices are used for management by sensor.
198 # Check for acpi index.
199 if ( defined $dinfo->{'acpi_index'} ) {
200 return;
201 }
202
203 if ( ! $dinfo->{'vendor'} eq $PCI_VENDOR_INTEL ) {
204 # all monitor devices are either intel or napatech
205 return;
206 }
207
208 if ( $dinfo->{'device'} eq $PCI_DEVICE_INTEL_X710 ) {
209 # This device is used for direct connection to collector
210 return;
211 }
212
213 return 1;
214 }
215
216 # find_monitor_ethdevs:
217 # returns an array of array references. Each reference points to a
218 # result from device_info().
219 sub find_monitor_ethdevs {
220 my ($pcidevs, $systypename) = @_;
221 my @meds = [];
222 my $is_sensor_appliance = 0;
223
224 if ( $systypename eq 'sensor' ) {
225 $is_sensor_appliance = 1;
226 }
227
228 for my $busaddr (keys %$pcidevs) {
229 my $dinfo = $pcidevs->{$busaddr};
230
231 if ( $is_sensor_appliance ) {
232 if (is_sensor_monitor_ethdev($dinfo)) {
233 push @meds, $dinfo;
234 }
235 } else {
236 if (is_collector_monitor_ethdev($dinfo)) {
237 push @meds, $dinfo;
238 }
239 }
240 }
241
242 return @meds;
243 }
244
245 # Example:
246 # my $href = index_by_slotport(find_pci_ethdevs());
247 # print Dumper($href);
248
249 1;