Skip to content

Commit

Permalink
vyos.ethtool: T6083: use JSON input data for ring-buffer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
c-po committed Mar 1, 2024
1 parent beb0576 commit 170f55c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
34 changes: 7 additions & 27 deletions python/vyos/ethtool.py
@@ -1,4 +1,4 @@
# Copyright 2021-2023 VyOS maintainers and contributors <maintainers@vyos.io>
# Copyright 2021-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -16,6 +16,7 @@
import os
import re

from json import loads
from vyos.utils.process import popen

# These drivers do not support using ethtool to change the speed, duplex, or
Expand Down Expand Up @@ -49,8 +50,7 @@ class Ethtool:
# '1000': {'full': ''}
# }
_speed_duplex = {'auto': {'auto': ''}}
_ring_buffers = { }
_ring_buffers_max = { }
_ring_buffer = None
_driver_name = None
_auto_negotiation = False
_auto_negotiation_supported = None
Expand Down Expand Up @@ -111,28 +111,8 @@ def __init__(self, ifname):
'fixed' : fixed
}

out, _ = popen(f'ethtool --show-ring {ifname}')
# We are only interested in line 2-5 which contains the device maximum
# ringbuffers
for line in out.splitlines()[2:6]:
if ':' in line:
key, value = [s.strip() for s in line.strip().split(":", 1)]
key = key.lower().replace(' ', '_')
# T3645: ethtool version used on Debian Bullseye changed the
# output format from 0 -> n/a. As we are only interested in the
# tx/rx keys we do not care about RX Mini/Jumbo.
if value.isdigit():
self._ring_buffers_max[key] = value
# Now we wan't to get the current RX/TX ringbuffer values - used for
for line in out.splitlines()[7:11]:
if ':' in line:
key, value = [s.strip() for s in line.strip().split(":", 1)]
key = key.lower().replace(' ', '_')
# T3645: ethtool version used on Debian Bullseye changed the
# output format from 0 -> n/a. As we are only interested in the
# tx/rx keys we do not care about RX Mini/Jumbo.
if value.isdigit():
self._ring_buffers[key] = value
out, _ = popen(f'ethtool --json --show-ring {ifname}')
self._ring_buffer = loads(out)

# Get current flow control settings, but this is not supported by
# all NICs (e.g. vmxnet3 does not support is)
Expand Down Expand Up @@ -201,14 +181,14 @@ def get_ring_buffer_max(self, rx_tx):
# thus when it's impossible return None
if rx_tx not in ['rx', 'tx']:
ValueError('Ring-buffer type must be either "rx" or "tx"')
return self._ring_buffers_max.get(rx_tx, None)
return str(self._ring_buffer[0].get(f'{rx_tx}-max', None))

def get_ring_buffer(self, rx_tx):
# Configuration of RX/TX ring-buffers is not supported on every device,
# thus when it's impossible return None
if rx_tx not in ['rx', 'tx']:
ValueError('Ring-buffer type must be either "rx" or "tx"')
return str(self._ring_buffers.get(rx_tx, None))
return str(self._ring_buffer[0].get(rx_tx, None))

def check_speed_duplex(self, speed, duplex):
""" Check if the passed speed and duplex combination is supported by
Expand Down
28 changes: 26 additions & 2 deletions smoketest/scripts/cli/test_interfaces_ethernet.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2020-2022 VyOS maintainers and contributors
# Copyright (C) 2020-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand All @@ -17,7 +17,9 @@
import os
import re
import unittest

from glob import glob
from json import loads

from netifaces import AF_INET
from netifaces import AF_INET6
Expand All @@ -27,7 +29,6 @@
from vyos.configsession import ConfigSessionError
from vyos.ifconfig import Section
from vyos.pki import CERT_BEGIN
from vyos.template import is_ipv6
from vyos.utils.process import cmd
from vyos.utils.process import process_named_running
from vyos.utils.file import read_file
Expand Down Expand Up @@ -301,5 +302,28 @@ def test_eapol_support(self):
self.cli_delete(['pki', 'ca', name])
self.cli_delete(['pki', 'certificate', cert_name])

def test_ethtool_ring_buffer(self):
for interface in self._interfaces:
tmp = cmd(f'sudo ethtool --json --show-ring {interface}')
tmp = loads(tmp)
max_rx = str(tmp[0]['rx-max'])
max_tx = str(tmp[0]['tx-max'])

self.cli_set(self._base_path + [interface, 'ring-buffer', 'rx', max_rx])
self.cli_set(self._base_path + [interface, 'ring-buffer', 'tx', max_tx])

self.cli_commit()

for interface in self._interfaces:
tmp = cmd(f'sudo ethtool --json --show-ring {interface}')
tmp = loads(tmp)
max_rx = str(tmp[0]['rx-max'])
max_tx = str(tmp[0]['tx-max'])
rx = str(tmp[0]['rx'])
tx = str(tmp[0]['tx'])

self.assertEqual(max_rx, rx)
self.assertEqual(max_tx, tx)

if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 170f55c

Please sign in to comment.