Update of VPP_STABLE_VER files + quick fix for gre create tunnel
[csit.git] / resources / libraries / python / InterfaceUtil.py
index 8e18923..3f26809 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2018 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
 
 """Interface util library."""
 
+from socket import AF_INET, AF_INET6, inet_ntop, inet_pton
+from socket import error as inet_error
 from time import time, sleep
 
 from robot.api import logger
 
+from resources.libraries.python.Constants import Constants
 from resources.libraries.python.CpuUtils import CpuUtils
 from resources.libraries.python.DUTSetup import DUTSetup
 from resources.libraries.python.PapiExecutor import PapiExecutor
-from resources.libraries.python.PapiErrors import PapiError
 from resources.libraries.python.IPUtil import convert_ipv4_netmask_prefix
 from resources.libraries.python.IPUtil import IPUtil
+from resources.libraries.python.PapiExecutor import PapiExecutor
 from resources.libraries.python.parsers.JsonParser import JsonParser
 from resources.libraries.python.ssh import SSH, exec_cmd_no_error
 from resources.libraries.python.topology import NodeType, Topology
@@ -879,28 +882,38 @@ class InterfaceUtil(object):
         :rtype: tuple
         :raises RuntimeError: If unable to create GRE tunnel interface.
         """
-        output = VatExecutor.cmd_from_template(node, "create_gre.vat",
-                                               src=source_ip,
-                                               dst=destination_ip)
-        output = output[0]
-
-        if output["retval"] == 0:
-            sw_if_idx = output["sw_if_index"]
-
-            vat_executor = VatExecutor()
-            vat_executor.execute_script_json_out("dump_interfaces.vat", node)
-            interface_dump_json = vat_executor.get_script_stdout()
-            name = VatJsonUtil.get_interface_name_from_json(
-                interface_dump_json, sw_if_idx)
 
-            if_key = Topology.add_new_port(node, "gre_tunnel")
-            Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
-            Topology.update_interface_name(node, if_key, name)
+        try:
+            src_address = inet_pton(AF_INET6, source_ip)
+            dst_address = inet_pton(AF_INET6, destination_ip)
+            is_ipv6 = 1
+        except inet_error:
+            src_address = inet_pton(AF_INET, source_ip)
+            dst_address = inet_pton(AF_INET, destination_ip)
+            is_ipv6 = 0
+
+        cmd = 'gre_tunnel_add_del'
+        tunnel = dict(type=0,
+                      instance=Constants.BITWISE_NON_ZERO,
+                      src=src_address,
+                      dst=dst_address,
+                      outer_fib_id=0,
+                      session_id=0)
+        args = dict(is_add=1,
+                    tunnel=tunnel)
+        err_msg = 'Failed to create GRE tunnel interface on host {host}'.format(
+            host=node['host'])
+        with PapiExecutor(node) as papi_exec:
+            papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg).\
+                verify_reply(err_msg=err_msg)
+
+        sw_if_idx = papi_resp['sw_if_index']
+        if_key = Topology.add_new_port(node, 'gre_tunnel')
+        Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
+        ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx)
+        Topology.update_interface_name(node, if_key, ifc_name)
 
-            return name, sw_if_idx
-        else:
-            raise RuntimeError('Unable to create GRE tunnel on node {}.'
-                               .format(node))
+        return ifc_name, sw_if_idx
 
     @staticmethod
     def vpp_create_loopback(node):
@@ -991,21 +1004,27 @@ class InterfaceUtil(object):
         Topology.update_interface_mac_address(node, if_key, ifc_mac)
 
     @staticmethod
-    def vpp_create_avf_interface(node, vf_pci_addr):
+    def vpp_create_avf_interface(node, vf_pci_addr, num_rx_queues=None):
         """Create AVF interface on VPP node.
 
         :param node: DUT node from topology.
         :param vf_pci_addr: Virtual Function PCI address.
+        :param num_rx_queues: Number of RX queues.
         :type node: dict
         :type vf_pci_addr: str
+        :type num_rx_queues: int
         :returns: Interface key (name) in topology.
         :rtype: str
         :raises RuntimeError: If it is not possible to create AVF interface on
             the node.
         """
+        num_rx_queues = 'num-rx-queues {num_rx_queues}'\
+            .format(num_rx_queues=num_rx_queues) if num_rx_queues else ''
+
         with VatTerminal(node, json_param=False) as vat:
             vat.vat_terminal_exec_cmd_from_template('create_avf_interface.vat',
-                                                    vf_pci_addr=vf_pci_addr)
+                                                    vf_pci_addr=vf_pci_addr,
+                                                    num_rx_queues=num_rx_queues)
             output = vat.vat_stdout
 
         if output is not None:
@@ -1320,18 +1339,19 @@ class InterfaceUtil(object):
         exec_cmd_no_error(node, cmd, sudo=True)
 
     @staticmethod
-    def init_avf_interface(node, ifc_key, numvfs=1, topology_type='L2'):
+    def init_avf_interface(node, ifc_key, numvfs=1, osi_layer='L2'):
         """Init PCI device by creating VFs and bind them to vfio-pci for AVF
         driver testing on DUT.
 
         :param node: DUT node.
         :param ifc_key: Interface key from topology file.
         :param numvfs: Number of VFs to initialize, 0 - disable the VFs.
-        :param topology_type: Topology type.
+        :param osi_layer: OSI Layer type to initialize TG with.
+            Default value "L2" sets linux interface spoof off.
         :type node: dict
         :type ifc_key: str
         :type numvfs: int
-        :type topology_type: str
+        :type osi_layer: str
         :returns: Virtual Function topology interface keys.
         :rtype: list
         """
@@ -1370,7 +1390,7 @@ class InterfaceUtil(object):
                 format(pci=pf_pci_addr)
             InterfaceUtil.set_linux_interface_trust_on(node, pf_dev,
                                                        vf_id=vf_id)
-            if topology_type == 'L2':
+            if osi_layer == 'L2':
                 InterfaceUtil.set_linux_interface_spoof_off(node, pf_dev,
                                                             vf_id=vf_id)
             InterfaceUtil.set_linux_interface_mac(node, pf_dev, vf_mac_addr,
@@ -1631,33 +1651,20 @@ class InterfaceUtil(object):
         :type node: dict
         :returns: Thread mapping information as a list of dictionaries.
         :rtype: list
-        :raises RuntimeError: If failed to run command on host.
-        :raises PapiError: If no API reply received.
         """
-        api_data = list()
-        for ifc in node['interfaces'].values():
-            if ifc['vpp_sw_index'] is not None:
-                api = dict(api_name='sw_interface_rx_placement_dump')
-                api_args = dict(sw_if_index=ifc['vpp_sw_index'])
-                api['api_args'] = api_args
-                api_data.append(api)
-
-        with PapiExecutor(node) as papi_executor:
-            papi_executor.execute_papi(api_data)
-            try:
-                papi_executor.papi_should_have_passed()
-                api_reply = papi_executor.get_papi_reply()
-            except AssertionError:
-                raise RuntimeError('Failed to run {api_name} on host '
-                                   '{host}!'.format(host=node['host'], **api))
-
-        if api_reply:
-            thr_mapping = [s['sw_interface_rx_placement_details'] \
-                for r in api_reply for s in r['api_reply']]
-            return sorted(thr_mapping, key=lambda k: k['sw_if_index'])
-        else:
-            raise PapiError('No reply received for {api_name} on host {host}!'.
-                            format(host=node['host'], **api))
+
+        cmd = 'sw_interface_rx_placement_dump'
+        cmd_reply = 'sw_interface_rx_placement_details'
+        err_msg = "Failed to run '{cmd}' PAPI command on host {host}!".format(
+            cmd=cmd, host=node['host'])
+        with PapiExecutor(node) as papi_exec:
+            for ifc in node['interfaces'].values():
+                if ifc['vpp_sw_index'] is not None:
+                    papi_exec.add(cmd, sw_if_index=ifc['vpp_sw_index'])
+            papi_resp = papi_exec.get_dump(err_msg)
+        thr_mapping = [s[cmd_reply] for r in papi_resp.reply
+                       for s in r['api_reply']]
+        return sorted(thr_mapping, key=lambda k: k['sw_if_index'])
 
     @staticmethod
     def vpp_sw_interface_set_rx_placement(node, sw_if_index, queue_id,
@@ -1672,28 +1679,23 @@ class InterfaceUtil(object):
         :type sw_if_index: int
         :type queue_id: int
         :type worker_id: int
-        :raises RuntimeError: If failed to run command on host.
-        :raises PapiError: If no API reply received.
+        :raises RuntimeError: If failed to run command on host or if no API
+            reply received.
         """
-        api_data = list()
-        api = dict(api_name='sw_interface_set_rx_placement')
-        api_args = dict(sw_if_index=sw_if_index, queue_id=queue_id,
-                        worker_id=worker_id)
-        api['api_args'] = api_args
-        api_data.append(api)
-
-        with PapiExecutor(node) as papi_executor:
-            papi_executor.execute_papi(api_data)
-            try:
-                papi_executor.papi_should_have_passed()
-                api_reply = papi_executor.get_papi_reply()
-            except AssertionError:
-                raise RuntimeError('Failed to run {api_name} on host '
-                                   '{host}!'.format(host=node['host'], **api))
-
-        if not api_reply:
-            raise PapiError('No reply received for {api_name} on host {host}!'.
-                            format(host=node['host'], **api))
+
+        cmd = 'sw_interface_set_rx_placement'
+        cmd_reply = 'sw_interface_set_rx_placement_reply'
+        err_msg = "Failed to run '{cmd}' PAPI command on host {host}!".format(
+            host=node['host'], cmd=cmd)
+        args = dict(sw_if_index=sw_if_index, queue_id=queue_id,
+                    worker_id=worker_id)
+        with PapiExecutor(node) as papi_exec:
+            papi_resp = papi_exec.add(cmd, **args).execute_should_pass(err_msg)
+        data = papi_resp.reply[0]['api_reply'][cmd_reply]
+        if data['retval'] != 0:
+            raise RuntimeError("Failed to set interface RX placement "
+                               "to worker on host {host}".
+                               format(host=node['host']))
 
     @staticmethod
     def vpp_round_robin_rx_placement(node, prefix):