From 2fc157115cb358afba89f92a761c6617159624fc Mon Sep 17 00:00:00 2001
From: DmitriyEshenko <dmitriy.eshenko@vyos.io>
Date: Wed, 16 Sep 2020 20:03:19 +0000
Subject: ethernet: T2891: Add ethernet ring-buffer CLI commands

---
 interface-definitions/interfaces-ethernet.xml.in | 31 ++++++++++++++++++++++++
 python/vyos/ifconfig/ethernet.py                 | 21 ++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/interface-definitions/interfaces-ethernet.xml.in b/interface-definitions/interfaces-ethernet.xml.in
index e8f3f09f1..0aef0d332 100644
--- a/interface-definitions/interfaces-ethernet.xml.in
+++ b/interface-definitions/interfaces-ethernet.xml.in
@@ -268,6 +268,37 @@
             </properties>
             <defaultValue>auto</defaultValue>
           </leafNode>
+          <node name="ring-buffer">
+            <properties>
+              <help>Shared buffer between the device driver and NIC</help>
+            </properties>
+            <children>
+              <leafNode name="rx">
+                <properties>
+                  <help>RX ring buffer</help>
+                  <valueHelp>
+                    <format>80-16384</format>
+                    <description>ring buffer size</description>
+                  </valueHelp>
+                  <constraint>
+                    <validator name="numeric" argument="--range 80-16384"/>
+                  </constraint>
+                </properties>
+              </leafNode>
+              <leafNode name="tx">
+                <properties>
+                  <help>TX ring buffer</help>
+                  <valueHelp>
+                    <format>80-16384</format>
+                    <description>ring buffer size</description>
+                  </valueHelp>
+                  <constraint>
+                    <validator name="numeric" argument="--range 80-16384"/>
+                  </constraint>
+                </properties>
+              </leafNode>
+            </children>
+          </node>
           #include <include/vif-s.xml.i>
           #include <include/vif.xml.i>
         </children>
diff --git a/python/vyos/ifconfig/ethernet.py b/python/vyos/ifconfig/ethernet.py
index 17c1bd64d..d4014a4dc 100644
--- a/python/vyos/ifconfig/ethernet.py
+++ b/python/vyos/ifconfig/ethernet.py
@@ -253,6 +253,22 @@ class EthernetIf(Interface):
         """
         return self.set_interface('ufo', state)
 
+    def set_ring_buffer(self, b_type, b_size):
+        """
+        Example:
+        >>> from vyos.ifconfig import EthernetIf
+        >>> i = EthernetIf('eth0')
+        >>> i.set_ring_buffer('rx', '4096')
+        """
+        cmd = '/sbin/ethtool -G {0} {1} {2}'.format(self.config['ifname'], b_type, b_size)
+        output, code = self._popen(cmd)
+        # ethtool error codes:
+        #  80 - value already setted
+        #  81 - does not possible to set value
+        if code and code != 80:
+            print('could not set {0} ring-buffer for {1}'.format(b_type, self.config['ifname']))
+        return output
+
 
     def update(self, config):
         """ General helper function which works on a dictionary retrived by
@@ -298,6 +314,11 @@ class EthernetIf(Interface):
             duplex = config.get('duplex')
             self.set_speed_duplex(speed, duplex)
 
+        # Set interface ring buffer
+        if 'ring_buffer' in config:
+            for b_type in config['ring_buffer']:
+                self.set_ring_buffer(b_type, config['ring_buffer'][b_type])
+
         # Enable/Disable of an interface must always be done at the end of the
         # derived class to make use of the ref-counting set_admin_state()
         # function. We will only enable the interface if 'up' was called as
-- 
cgit v1.2.3