From b53ea8760ef79e85316c16ea80ccea74d36ac246 Mon Sep 17 00:00:00 2001
From: Danil Golov <d.golov@samsung.com>
Date: Thu, 17 Oct 2019 14:05:42 +0300
Subject: [PATCH] Use pyroute2 to tweak vf in sriov

Since possibility to operate with virtual functions
was introduced in pyroute2 it is not necessary
to use ip program in kuryr-kubernetes anymore.

This commit adds a using of pyroute2 library
in sriov binding driver.

Partially implements: bp use-pyroute2-to-tweak-vf-in-sriov
Change-Id: I3cb2da476a6948dc98b2312a5779e47a410832bf
Signed-off-by: Danil Golov <d.golov@samsung.com>
---
 kuryr_kubernetes/cni/binding/sriov.py | 37 +++++++++++++--------------
 lower-constraints.txt                 |  2 +-
 requirements.txt                      |  2 +-
 3 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/kuryr_kubernetes/cni/binding/sriov.py b/kuryr_kubernetes/cni/binding/sriov.py
index 009079993..7408b1773 100644
--- a/kuryr_kubernetes/cni/binding/sriov.py
+++ b/kuryr_kubernetes/cni/binding/sriov.py
@@ -17,10 +17,10 @@ import os
 
 from kuryr.lib._i18n import _
 from oslo_concurrency import lockutils
-from oslo_concurrency import processutils
 from oslo_config import cfg
 from oslo_log import log as logging
 from oslo_serialization import jsonutils
+import pyroute2
 
 from kuryr_kubernetes import clients
 from kuryr_kubernetes.cni.binding import base as b_base
@@ -356,30 +356,29 @@ class VIFSriovDriver(object):
         return nvfs
 
     def _set_vf_mac(self, pf, vf_index, mac):
-        """Call `ip link set enp2s0f1 vf 3 mac fa:16:3e:87:b2:ac`"""
-
-        LOG.debug("Seting VF MAC: pf = %s, vf_index = %s, mac = %s",
+        LOG.debug("Setting VF MAC: pf = %s, vf_index = %s, mac = %s",
                   pf, vf_index, mac)
-        cmd = [
-            'ip', 'link',
-            'set', pf, 'vf', vf_index, 'mac', mac
-        ]
+
+        ip = pyroute2.IPRoute()
+        pf_index = ip.link_lookup(ifname=pf)[0]
         try:
-            return processutils.execute(*cmd, run_as_root=True)
-        except Exception:
-            LOG.exception("Unable to execute %s", cmd)
+            ip.link("set", index=pf_index, vf={"vf": vf_index, "mac": mac})
+        except pyroute2.netlink.exceptions.NetlinkError:
+            LOG.exception("Unable to set mac for VF %s on pf %s",
+                          vf_index, pf)
             raise
 
     def _set_vf_vlan(self, pf, vf_index, vlan_id):
-        """Call `ip link set enp1s0f0 vf 5 vlan 10`"""
-        cmd = [
-            'ip', 'link',
-            'set', pf, 'vf', vf_index, 'vlan', vlan_id
-        ]
+        LOG.debug("Setting VF VLAN: pf = %s, vf_index = %s, vlan_id = %s",
+                  pf, vf_index, vlan_id)
+        ip = pyroute2.IPRoute()
+        pf_index = ip.link_lookup(ifname=pf)[0]
         try:
-            return processutils.execute(*cmd, run_as_root=True)
-        except Exception:
-            LOG.exception("Unable to execute %s", cmd)
+            ip.link("set", index=pf_index, vf={"vf": vf_index,
+                                               "vlan": vlan_id})
+        except pyroute2.netlink.exceptions.NetlinkError:
+            LOG.exception("Unable to set vlan for VF %s on pf %s",
+                          vf_index, pf)
             raise
 
     def is_alive(self):
diff --git a/lower-constraints.txt b/lower-constraints.txt
index fe73e59b1..fa0e21270 100644
--- a/lower-constraints.txt
+++ b/lower-constraints.txt
@@ -95,7 +95,7 @@ Pygments==2.2.0
 pyinotify==0.9.6
 pyparsing==2.2.0
 pyperclip==1.6.0
-pyroute2==0.5.3
+pyroute2==0.5.7
 python-dateutil==2.7.0
 python-editor==1.0.3
 python-keystoneclient==3.15.0
diff --git a/requirements.txt b/requirements.txt
index 00aa88383..7d8726cbd 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -19,7 +19,7 @@ oslo.service!=1.28.1,>=1.24.0 # Apache-2.0
 oslo.utils>=3.33.0 # Apache-2.0
 os-vif!=1.8.0,>=1.7.0 # Apache-2.0
 PrettyTable<0.8,>=0.7.2  # BSD
-pyroute2>=0.5.3;sys_platform!='win32' # Apache-2.0 (+ dual licensed GPL2)
+pyroute2>=0.5.7;sys_platform!='win32' # Apache-2.0 (+ dual licensed GPL2)
 retrying!=1.3.0,>=1.2.3 # Apache-2.0
 six>=1.10.0 # MIT
 stevedore>=1.20.0 # Apache-2.0