-# Copyright (c) 2016 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:
"""Vhost-user interfaces library."""
-from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
-from resources.libraries.python.topology import NodeType
+from robot.api import logger
+
+from resources.libraries.python.PapiExecutor import PapiSocketExecutor
+from resources.libraries.python.topology import NodeType, Topology
+from resources.libraries.python.InterfaceUtil import InterfaceUtil
class VhostUser(object):
- """Vhost-user interfaces"""
+ """Vhost-user interfaces L1 library."""
+
+ @staticmethod
+ def _sw_interface_vhost_user_dump(node):
+ """Get the Vhost-user dump on the given node.
+
+ :param node: Given node to get Vhost dump from.
+ :type node: dict
+ :returns: List of Vhost-user interfaces data extracted from Papi
+ response.
+ :rtype: list
+ """
+ cmd = "sw_interface_vhost_user_dump"
+ with PapiSocketExecutor(node) as papi_exec:
+ details = papi_exec.add(cmd).get_details()
+
+ for vhost in details:
+ vhost["interface_name"] = vhost["interface_name"].rstrip('\x00')
+ vhost["sock_filename"] = vhost["sock_filename"].rstrip('\x00')
+
+ logger.debug("VhostUser details:\n{details}".format(details=details))
+
+ return details
@staticmethod
def vpp_create_vhost_user_interface(node, socket):
:param socket: Vhost-user interface socket path.
:type node: dict
:type socket: str
- :return: SW interface index.
+ :returns: SW interface index.
:rtype: int
"""
- out = VatExecutor.cmd_from_template(node, "create_vhost_user_if.vat",
- sock=socket)
- if out[0].get('retval') == 0:
- return out[0].get('sw_if_index')
- else:
- raise RuntimeError('Create Vhost-user interface failed on node '
- '"{}"'.format(node['host']))
+ cmd = 'create_vhost_user_if'
+ err_msg = 'Failed to create Vhost-user interface on host {host}'.format(
+ host=node['host'])
+ args = dict(
+ sock_filename=str(socket)
+ )
+ with PapiSocketExecutor(node) as papi_exec:
+ sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
+
+ # Update the Topology:
+ if_key = Topology.add_new_port(node, 'vhost')
+ Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
+
+ ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index)
+ Topology.update_interface_name(node, if_key, ifc_name)
+
+ ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
+ Topology.update_interface_mac_address(node, if_key, ifc_mac)
+
+ Topology.update_interface_vhost_socket(node, if_key, socket)
+
+ return sw_if_index
@staticmethod
def get_vhost_user_if_name_by_sock(node, socket):
:param socket: Vhost-user interface socket path.
:type node: dict
:type socket: str
- :return: Interface name or None if not found.
+ :returns: Interface name or None if not found.
:rtype: str
"""
for interface in node['interfaces'].values():
interface dump.
:param node: VPP node to get interface data from.
- :param sw_if_index: Idx of the specific interface.
+ :param sw_if_index: SW index of the specific interface.
:type node: dict
:type sw_if_index: str
- :return: l2_address of the given interface.
+ :returns: l2_address of the given interface.
:rtype: str
"""
- with VatTerminal(node) as vat:
- if_data = vat.vat_terminal_exec_cmd_from_template(
- "interface_dump.vat")
- for iface in if_data[0]:
- if iface["sw_if_index"] == sw_if_index:
- return ':'.join("%02x" % (b) for b in iface["l2_address"][:6])
-
- return None
+ return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
@staticmethod
def vpp_show_vhost(node):
- """Get vhost-user data for the given node.
+ """Get Vhost-user data for the given node.
:param node: VPP node to get interface data from.
:type node: dict
- :return: nothing
"""
- vat = VatExecutor()
- vat.execute_script("show_vhost.vat", node, json_out=False)
+ VhostUser._sw_interface_vhost_user_dump(node)
@staticmethod
def show_vpp_vhost_on_all_duts(nodes):
- """Show Vhost User on all DUTs.
+ """Show Vhost-user on all DUTs.
:param nodes: VPP nodes.
:type nodes: dict