feat(job_specs): Add soak test
[csit.git] / resources / libraries / python / NsimUtil.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """VPP Network Simulator Plugin util library."""
15
16 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
17 from resources.libraries.python.InterfaceUtil import InterfaceUtil
18
19
20 class NsimUtil():
21     """VPP NSIM Plugin Keywords."""
22
23     @staticmethod
24     def configure_vpp_nsim(node, vpp_nsim_attr, interface0, interface1=None):
25         """Configure nsim on the specified VPP node.
26
27         :param node: Topology node.
28         :param vpp_nsim_attr: VPP NSIM configuration attributes
29         :param interface0: Interface name.
30         :param interface1: 2nd Interface name for cross-connect feature
31         :type node: dict
32         :type vpp_nsim_attr: dict
33         :type interface0: str or int
34         :type interface1: str or int
35         :raises RuntimeError: if no NSIM features are enabled or
36                 vpp papi command fails.
37         """
38         host = node[u"host"]
39         if not vpp_nsim_attr[u"output_nsim_enable"] \
40                 and not vpp_nsim_attr[u"xc_nsim_enable"]:
41             raise RuntimeError(f"No NSIM features enabled on host {host}:\n"
42                                f"vpp_nsim_attr = {vpp_nsim_attr}")
43         cmd = u"nsim_configure2"
44         args = dict(
45             delay_in_usec=vpp_nsim_attr[u"delay_in_usec"],
46             average_packet_size=vpp_nsim_attr[u"average_packet_size"],
47             bandwidth_in_bits_per_second=vpp_nsim_attr[
48                 u"bw_in_bits_per_second"
49             ],
50             packets_per_drop=vpp_nsim_attr[u"packets_per_drop"],
51             packets_per_reorder=vpp_nsim_attr.get(u"packets_per_reorder", 0)
52         )
53         err_msg = f"Failed to configure NSIM on host {host}"
54         try:
55             with PapiSocketExecutor(node) as papi_exec:
56                 papi_exec.add(cmd, **args).get_reply(err_msg)
57         except AssertionError:
58             # Perhaps VPP is an older version
59             old_cmd = u"nsim_configure"
60             args.pop(u"packets_per_reorder")
61             with PapiSocketExecutor(node) as papi_exec:
62                 papi_exec.add(old_cmd, **args).get_reply(err_msg)
63
64         if vpp_nsim_attr[u"output_nsim_enable"]:
65             cmd = u"nsim_output_feature_enable_disable"
66             args = dict(
67                 enable_disable=vpp_nsim_attr[u"output_nsim_enable"],
68                 sw_if_index=InterfaceUtil.get_interface_index(node, interface0),
69             )
70             err_msg = f"Failed to enable NSIM output feature on " \
71                 f"host {host} interface {interface0}"
72             with PapiSocketExecutor(node) as papi_exec:
73                 papi_exec.add(cmd, **args).get_reply(err_msg)
74
75         elif vpp_nsim_attr[u"xc_nsim_enable"]:
76             cmd = u"nsim_cross_connect_feature_enable_disable"
77             args = dict(
78                 enable_disable=vpp_nsim_attr[u"xc_nsim_enable"],
79                 sw_if_index0=InterfaceUtil.get_interface_index(node,
80                                                                interface0),
81                 sw_if_index1=InterfaceUtil.get_interface_index(node,
82                                                                interface1),
83             )
84             err_msg = f"Failed to enable NSIM output feature on " \
85                 f"host {host} interface {interface0}"
86             with PapiSocketExecutor(node) as papi_exec:
87                 papi_exec.add(cmd, **args).get_reply(err_msg)