Skip to content

Commit

Permalink
Removed bridge-utils dependency in favour of iproute2.
Browse files Browse the repository at this point in the history
Network devices list gathering methods' priority:

1. mirror.conf parsing (configured via setup master of CR)
2. bridge output parsing (if there are signs of CR installed)
3. ethtool directories parsing (every other normal systems)

strizhechenko#94
  • Loading branch information
strizhechenko authored Jun 28, 2017
1 parent 0c292f0 commit 3ce0f11
Show file tree
Hide file tree
Showing 28 changed files with 106 additions and 79 deletions.
29 changes: 18 additions & 11 deletions netutils_linux_hardware/netdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@
import os
from six import iteritems
from netutils_linux_hardware.interrupts import IRQQueueCounter
from netutils_linux_hardware.parsers import EthtoolBuffers, ReductorMirror, BrctlOutput, YAMLLike
from netutils_linux_hardware.parsers import EthtoolBuffers, ReductorMirror, BridgeOutput, YAMLLike, EthtoolFiles


class ReaderNet(object):

netdevs = None

def __init__(self, datadir, path):
self.datadir = datadir
self.path = path
self.net_dev_list()

def net_dev_mirror_info(self):
return ReductorMirror().parse_file_safe(self.path('mirror_info.conf'))

def net_dev_list_bridge(self):
bridges = self.path('brctl')
return BrctlOutput().parse_file_safe(bridges)
return BridgeOutput().parse_file_safe(self.path('bridge_link'))

def net_dev_mirror_info(self):
config_path = self.path('mirror_info.conf')
return ReductorMirror().parse_file_safe(config_path)
def net_dev_list_ethtool(self):
return EthtoolFiles().parse_file_safe(self.path('ethtool/i'))

def net_dev_list_buffers(self):
for netdev in self.netdevs:
buffers_path = os.path.join(self.datadir, 'ethtool/g', netdev)
self.netdevs[netdev]['buffers'] = EthtoolBuffers(
).parse_file_safe(buffers_path)
self.netdevs[netdev]['buffers'] = EthtoolBuffers().parse_file_safe(buffers_path)

def net_dev_list_drivers(self):
keys_required = (
Expand All @@ -39,13 +38,21 @@ def net_dev_list_drivers(self):
driverfile = os.path.join(self.datadir, 'ethtool/i', netdev)
driverdata = YAMLLike().parse_file_safe(driverfile)
if driverdata:
driverdata = dict((k, v) for k, v in iteritems(driverdata) if k in keys_required)
driverdata = dict((key, v) for key, v in iteritems(driverdata) if key in keys_required)
else:
driverdata = dict()
self.netdevs[netdev]['driver'] = driverdata

def net_dev_list(self):
self.netdevs = self.net_dev_list_bridge() or self.net_dev_mirror_info()
"""
Priority:
1. mirror_info.conf (collected only in case of reductor (master conf))
2. bridges output (collected only in case of reductor (manual conf))
3. ethtool information (non reductor case)
"""
self.netdevs = self.net_dev_mirror_info() or self.net_dev_list_bridge() or self.net_dev_list_ethtool()
if not self.netdevs:
return
self.net_dev_list_buffers()
self.net_dev_list_drivers()
interrupts = self.path('interrupts')
Expand Down
36 changes: 30 additions & 6 deletions netutils_linux_hardware/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ def parse(text):
output = dict()
output['conf'] = dict()
output['conf']['vlan'], output['conf']['ip'] = conf.split()
output['conf']['vlan'] = output['conf']['vlan'] == '-'
output['conf']['vlan'] = output['conf']['vlan'] != '-'
if output['conf']['ip'] == '-':
output['conf']['ip'] = ''
lines[netdev] = output

return lines


Expand Down Expand Up @@ -130,13 +131,14 @@ def parse(text):
return output


class BrctlOutput(Parser):
class BridgeOutput(Parser):
@staticmethod
def parse(text):
netdevs = dict()
netdevs_keys = [line.split()[3]
for line in text.strip().split('\n')[1:]
if len(line.split()) > 3]
# 3: eth1 state DOWN : <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 master br1 state disabled
# 0 - id, 1 - dev, 2 - _, 3 - state, 4 - _, 5 - details, 6 - _, 7 - mtu, 8 - _, 9 - master
__dev__ = 1
netdevs_keys = [line.split()[__dev__] for line in text.strip().split('\n')]
for key in netdevs_keys:
if key.count('.') == 1:
dev, _ = key.split('.')
Expand All @@ -154,6 +156,28 @@ def parse(text):
return netdevs


class EthtoolFiles(Parser):
def parse_file(self, filepath, **kwargs):
return self.parse(os.listdir(filepath))

def parse(self, netdev_keys):
netdevs = dict()
for key in netdev_keys:
if key.count('.') == 1:
dev, _ = key.split('.')
elif key.count('.') == 0:
dev = key
else:
print_('QinQ not supported yet. Device: {0}'.format(key))
raise NotImplementedError
netdevs[dev] = dict()
netdevs[dev]['conf'] = {
'vlan': key.count('.') == 1,
'ip': '',
}
return netdevs


class EthtoolBuffers(Parser):
@staticmethod
def parse(text):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def read(*paths):

setuptools.setup(
name='netutils-linux',
version='2.0.5',
version='2.0.6',
author='Oleg Strizhechenko',
author_email='[email protected]',
license='MIT',
Expand Down
7 changes: 5 additions & 2 deletions tests/server-info-show
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ run_test() {
if [ "$rc" = '0' ]; then
echo OK
else
diff -U 0 $testdata/$name/expected_output $TMPDIR/output
diff -U 5 $testdata/$name/expected_output $TMPDIR/output
echo FAIL
fi
return "$rc"
Expand All @@ -29,7 +29,10 @@ retval=0
# shellcheck disable=SC2044
for test in $(find $testdata -mindepth 1 -maxdepth 1 -type d); do
if [ -f $test/expected_output ]; then
run_test $test || retval=1
if ! run_test $test; then
retval=1
break
fi
fi
done
rm -rf $TMPDIR
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_ eth0 _
_ eth0.376 _
_ eth0.700 _
_ eth0.83 _
_ eth0.850 _
_ eth0.919 _
3 changes: 0 additions & 3 deletions tests/server-info-show.tests/1xE31240.2x82576.L2.manual/brctl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ eth0 _
_ eth1 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ eth1 _
_ eth2 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_ eth6 _
_ eth6.404 _
_ eth6.900 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_ eth0 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
_ eth1 _
_ eth2 _
_ eth4.31 _
_ eth4.1123 _
_ eth4.1223 _
_ eth5 _
_ eth3 _
_ eth3.55 _
_ eth3.76 _
_ eth3.3424 _
_ eth3.3429 _
_ eth4 _
_ eth4.25 _
_ eth4.27 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_ eth1 _
_ eth3 _
_ eth3.155 _
_ eth3.250 _
_ eth3.251 _
_ eth3.252 _
_ eth3.253 _
— eth3.254 _
_ eth3.255 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ em2 _
_ em2.904 _

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_ eth1 _
_ eth2 _
1 change: 0 additions & 1 deletion tests/server-info-show.tests/kvm.l3.masterconf/brctl

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ net:
max: 4096
conf:
ip: 10.30.30.1/24
vlan: true
vlan: false
driver:
driver: e1000
version: 7.3.21-k8-NAPI
Expand Down
7 changes: 6 additions & 1 deletion utils/server-info-collect
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ server_info() {

network_info() {
local netdev action
brctl show > $TMPDIR/brctl
MIRRORCONF_CR7=/usr/local/Reductor/userinfo/mirror_info.conf
MIRRORCONF_CR8_INCHROOT="/cfg/userinfo/mirror_info.conf"
MIRRORCONF_CR8_OUTCHROOT="/app/reductor/$MIRRORCONF_CR8_INCHROOT"
Expand All @@ -35,6 +34,12 @@ network_info() {
cp "$MIRRORCONF_CR8_INCHROOT" "$TMPDIR/mirror_info.conf"
elif [ -f "$MIRRORCONF_CR8_OUTCHROOT" ]; then
cp "$MIRRORCONF_CR8_OUTCHROOT" "$TMPDIR/mirror_info.conf"
else
# We need bridges data only in case of Carbon Reductor without mirror_info.conf
# Other systems don't depend on bridges/configs.
if [ -d /usr/local/Reductor ] || [ -d /app/reductor ]; then
bridge link > $TMPDIR/bridge_links
fi
fi

for netdev in $(grep : /proc/net/dev | awk '{print $1}' | tr -d :); do
Expand Down
3 changes: 2 additions & 1 deletion utils/server-info-rate
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# pylint: disable=C0111, C0103

import os
from six import print_
from netutils_linux_hardware.reader import Reader
from netutils_linux_hardware.assessor import Assessor

Expand All @@ -19,7 +20,7 @@ def main():
datadir = 'tests/autotune_network.tests/2xE5-2640.i350_and_82599ES.l2_mixed.masterconf'
# datadir = 'tests/autotune_network.tests/kvm.l3.masterconf'
data = Reader(datadir).info
print Assessor(data)
print_(Assessor(data))

if __name__ == '__main__':
main()
7 changes: 2 additions & 5 deletions utils/server-info-show
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ from netutils_linux_hardware.reader import Reader


def main():
datadir = os.getenv('DATADIR')
if os.path.isfile(os.path.join(os.getcwd(), 'lspci')):
datadir = os.getcwd()
else:
test_dir = 'tests/autotune_network.tests'
# test = '2xE5530.82576_and_82574L.l2_mixed.manual'
test = '2xE5-2640.i350_and_82599ES.l2_mixed.masterconf'
datadir = os.getenv('DATADIR', os.path.join(test_dir, test))
print_(Reader(datadir))


if __name__ == '__main__':
main()

0 comments on commit 3ce0f11

Please sign in to comment.