Example #1
0
    def vpp_nodes_set_ipv4_addresses(nodes, nodes_addr):
        """Set IPv4 addresses on all VPP nodes in topology.

        :param nodes: Nodes of the test topology.
        :param nodes_addr: Available nodes IPv4 addresses.
        :type nodes: dict
        :type nodes_addr: dict
        :returns: Affected interfaces as list of (node, interface) tuples.
        :rtype: list
        """
        interfaces = []
        for net in nodes_addr.values():
            for port in net['ports'].values():
                host = port.get('node')
                if host is None:
                    continue
                topo = Topology()
                node = topo.get_node_by_hostname(nodes, host)
                if node is None:
                    continue
                if node['type'] != NodeType.DUT:
                    continue
                iface_key = topo.get_interface_by_name(node, port['if'])
                get_node(node).set_ip(iface_key, port['addr'], net['prefix'])
                interfaces.append((node, port['if']))

        return interfaces
Example #2
0
    def create_vlan_subinterface(node, interface, vlan):
        """Create VLAN subinterface on node.

        :param node: Node to add VLAN subinterface on.
        :param interface: Interface name on which create VLAN subinterface.
        :param vlan: VLAN ID of the subinterface to be created.
        :type node: dict
        :type interface: str
        :type vlan: int
        :returns: Name and index of created subinterface.
        :rtype: tuple
        :raises RuntimeError: if it is unable to create VLAN subinterface on the
        node.
        """
        iface_key = Topology.get_interface_by_name(node, interface)
        sw_if_index = Topology.get_interface_sw_index(node, iface_key)

        output = VatExecutor.cmd_from_template(node, "create_vlan_subif.vat",
                                               sw_if_index=sw_if_index,
                                               vlan=vlan)
        if output[0]["retval"] == 0:
            sw_subif_index = output[0]["sw_if_index"]
            logger.trace('VLAN subinterface with sw_if_index {} and VLAN ID {} '
                         'created on node {}'.format(sw_subif_index,
                                                     vlan, node['host']))
        else:
            raise RuntimeError('Unable to create VLAN subinterface on node {}'
                               .format(node['host']))

        with VatTerminal(node, False) as vat:
            vat.vat_terminal_exec_cmd('exec show interfaces')

        return '{}.{}'.format(interface, vlan), sw_subif_index
Example #3
0
    def create_subinterface(node, interface, sub_id, outer_vlan_id=None,
                            inner_vlan_id=None, type_subif=None):
        """Create sub-interface on node. It is possible to set required
        sub-interface type and VLAN tag(s).

        :param node: Node to add sub-interface.
        :param interface: Interface name on which create sub-interface.
        :param sub_id: ID of the sub-interface to be created.
        :param outer_vlan_id: Optional outer VLAN ID.
        :param inner_vlan_id: Optional inner VLAN ID.
        :param type_subif: Optional type of sub-interface. Values supported by
        VPP: [no_tags] [one_tag] [two_tags] [dot1ad] [exact_match] [default_sub]
        :type node: dict
        :type interface: str or int
        :type sub_id: int
        :type outer_vlan_id: int
        :type inner_vlan_id: int
        :type type_subif: str
        :returns: Name and index of created sub-interface.
        :rtype: tuple
        :raises RuntimeError: If it is not possible to create sub-interface.
        """

        outer_vlan_id = 'outer_vlan_id {0}'.format(outer_vlan_id)\
            if outer_vlan_id else ''

        inner_vlan_id = 'inner_vlan_id {0}'.format(inner_vlan_id)\
            if inner_vlan_id else ''

        if type_subif is None:
            type_subif = ''

        if isinstance(interface, basestring):
            iface_key = Topology.get_interface_by_name(node, interface)
            sw_if_index = Topology.get_interface_sw_index(node, iface_key)
        else:
            sw_if_index = interface

        output = VatExecutor.cmd_from_template(node, "create_sub_interface.vat",
                                               sw_if_index=sw_if_index,
                                               sub_id=sub_id,
                                               outer_vlan_id=outer_vlan_id,
                                               inner_vlan_id=inner_vlan_id,
                                               type_subif=type_subif)

        if output[0]["retval"] == 0:
            sw_subif_index = output[0]["sw_if_index"]
            logger.trace('Created subinterface with index {}'
                         .format(sw_subif_index))
        else:
            raise RuntimeError('Unable to create sub-interface on node {}'
                               .format(node['host']))

        with VatTerminal(node, json_param=False) as vat:
            vat.vat_terminal_exec_cmd('exec show interfaces')

        name = '{}.{}'.format(interface, sub_id)
        return name, sw_subif_index
Example #4
0
    def set_interface_state(node, interface, state, if_type="key"):
        """Set interface state on a node.

        Function can be used for DUTs as well as for TGs.

        :param node: Node where the interface is.
        :param interface: Interface key or sw_if_index or name.
        :param state: One of 'up' or 'down'.
        :param if_type: Interface type
        :type node: dict
        :type interface: str or int
        :type state: str
        :type if_type: str
        :returns: Nothing.
        :raises ValueError: If the interface type is unknown.
        :raises ValueError: If the state of interface is unexpected.
        :raises ValueError: If the node has an unknown node type.
        """

        if if_type == "key":
            if isinstance(interface, basestring):
                sw_if_index = Topology.get_interface_sw_index(node, interface)
                iface_name = Topology.get_interface_name(node, interface)
            else:
                sw_if_index = interface
        elif if_type == "name":
            iface_key = Topology.get_interface_by_name(node, interface)
            if iface_key is not None:
                sw_if_index = Topology.get_interface_sw_index(node, iface_key)
            iface_name = interface
        else:
            raise ValueError("if_type unknown: {}".format(if_type))

        if node['type'] == NodeType.DUT:
            if state == 'up':
                state = 'admin-up'
            elif state == 'down':
                state = 'admin-down'
            else:
                raise ValueError(
                    'Unexpected interface state: {}'.format(state))
            VatExecutor.cmd_from_template(node,
                                          'set_if_state.vat',
                                          sw_if_index=sw_if_index,
                                          state=state)
        elif node['type'] == NodeType.TG or node['type'] == NodeType.VM:
            cmd = 'ip link set {} {}'.format(iface_name, state)
            exec_cmd_no_error(node, cmd, sudo=True)
        else:
            raise ValueError('Node {} has unknown NodeType: "{}"'.format(
                node['host'], node['type']))
Example #5
0
    def l2_vlan_tag_rewrite(node,
                            interface,
                            tag_rewrite_method,
                            push_dot1q=True,
                            tag1_id=None,
                            tag2_id=None):
        """Rewrite tags in ethernet frame.

        :param node: Node to rewrite tags.
        :param interface: Interface on which rewrite tags.
        :param tag_rewrite_method: Method of tag rewrite.
        :param push_dot1q: Optional parameter to disable to push dot1q tag
            instead of dot1ad.
        :param tag1_id: Optional tag1 ID for VLAN.
        :param tag2_id: Optional tag2 ID for VLAN.
        :type node: dict
        :type interface: str or int
        :type tag_rewrite_method: str
        :type push_dot1q: bool
        :type tag1_id: int
        :type tag2_id: int
        """

        tag1_id = int(tag1_id) if tag1_id else 0
        tag2_id = int(tag2_id) if tag2_id else 0

        vtr_oper = getattr(
            L2VtrOp,
            'L2_VTR_{}'.format(tag_rewrite_method.replace('-', '_').upper()))

        if isinstance(interface, basestring):
            iface_key = Topology.get_interface_by_name(node, interface)
            sw_if_index = Topology.get_interface_sw_index(node, iface_key)
        else:
            sw_if_index = interface

        cmd = 'l2_interface_vlan_tag_rewrite'
        args = dict(sw_if_index=sw_if_index,
                    vtr_op=int(vtr_oper),
                    push_dot1q=int(push_dot1q),
                    tag1=tag1_id,
                    tag2=tag2_id)
        err_msg = 'Failed to set VLAN TAG rewrite on host {host}'.format(
            host=node['host'])
        with PapiExecutor(node) as papi_exec:
            papi_exec.add(cmd, **args).get_replies(err_msg).\
                verify_reply(err_msg=err_msg)
Example #6
0
    def l2_vlan_tag_rewrite(node,
                            interface,
                            tag_rewrite_method,
                            push_dot1q=True,
                            tag1_id=None,
                            tag2_id=None):
        """Rewrite tags in ethernet frame.

        :param node: Node to rewrite tags.
        :param interface: Interface on which rewrite tags.
        :param tag_rewrite_method: Method of tag rewrite.
        :param push_dot1q: Optional parameter to disable to push dot1q tag
         instead of dot1ad.
        :param tag1_id: Optional tag1 ID for VLAN.
        :param tag2_id: Optional tag2 ID for VLAN.
        :type node: dict
        :type interface: str or int
        :type tag_rewrite_method: str
        :type push_dot1q: bool
        :type tag1_id: int
        :type tag2_id: int
        """
        push_dot1q = 'push_dot1q 0' if not push_dot1q else ''

        tag1_id = 'tag1 {0}'.format(tag1_id) if tag1_id else ''
        tag2_id = 'tag2 {0}'.format(tag2_id) if tag2_id else ''

        if isinstance(interface, basestring):
            iface_key = Topology.get_interface_by_name(node, interface)
            sw_if_index = Topology.get_interface_sw_index(node, iface_key)
        else:
            sw_if_index = interface

        with VatTerminal(node) as vat:
            vat.vat_terminal_exec_cmd_from_template(
                "l2_vlan_tag_rewrite.vat",
                sw_if_index=sw_if_index,
                tag_rewrite_method=tag_rewrite_method,
                push_dot1q=push_dot1q,
                tag1_optional=tag1_id,
                tag2_optional=tag2_id)