aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/IPv6Util.py
blob: 883304487dd570957501a235166d3bfe694a8c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (c) 2020 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:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""IPv6 utilities library."""

from resources.libraries.python.InterfaceUtil import InterfaceUtil
from resources.libraries.python.IPUtil import IPUtil
from resources.libraries.python.PapiExecutor import PapiSocketExecutor
from resources.libraries.python.topology import NodeType


class IPv6Util:
    """IPv6 utilities"""

    @staticmethod
    def vpp_interface_ra_suppress(node, interface):
        """Disable sending ICMPv6 router-advertisement messages on
        an interface on a VPP node.

        :param node: VPP node.
        :param interface: Interface name.
        :type node: dict
        :type interface: str
        """
        cmd = u"sw_interface_ip6nd_ra_config"
        args = dict(
            sw_if_index=InterfaceUtil.get_interface_index(node, interface),
            suppress=1
        )
        err_msg = f"Failed to disable sending ICMPv6 router-advertisement " \
            f"messages on interface {interface}"

        with PapiSocketExecutor(node) as papi_exec:
            papi_exec.add(cmd, **args).get_reply(err_msg)

    @staticmethod
    def vpp_ra_send_after_interval(node, interface, interval=2):
        """Setup vpp router advertisement(RA) in such way it sends RA packet
        after every interval value.

        :param node: VPP node.
        :param interface: Interface name.
        :param interval: Interval in seconds for RA resend.
        :type node: dict
        :type interface: str
        :type interval: int
        """
        cmd = u"sw_interface_ip6nd_ra_config"
        args = dict(
            sw_if_index=InterfaceUtil.get_interface_index(node, interface),
            initial_interval=int(interval)
        )
        err_msg = f"Failed to set router advertisement interval " \
            f"on interface {interface}"

        with PapiSocketExecutor(node) as papi_exec:
            papi_exec.add(cmd, **args).get_reply(err_msg)

    @staticmethod
    def vpp_interfaces_ra_suppress_on_all_nodes(nodes):
        """Disable sending ICMPv6 router-advertisement messages on all
        IPv6 enabled interfaces on all VPP nodes in the topology.

        :param nodes: Nodes of the test topology.
        :type nodes: dict
        """
        for node in nodes.values():
            if node[u"type"] == NodeType.TG:
                continue
            for port_k in node[u"interfaces"].keys():
                ip6_addr_list = IPUtil.vpp_get_interface_ip_addresses(
                    node, port_k, u"ipv6"
                )
                if ip6_addr_list:
                    IPv6Util.vpp_interface_ra_suppress(node, port_k)